FUNCTION scalarproduct, r1, r2, cylin=cylin, sphere=sphere, rect=rect, $ degrees=degrees, angle=angle ;+ ; NAME: ; scalarproduct ; PURPOSE: ; Calculate scalar product for two vectors ; CATEGORY: ; gen/idl/toolbox/math ; CALLING SEQUENCE: ; result = scalarproduct(r1, r2 [, /cylin, /sphere, /rect, /degrees, /angle]) ; INPUTS: ; r1 arrays[3,*]; type: int or float ; r1 arrays[3,*]; type: int or float ; vectors for which the product is to be taken. ; the first dimension identifies the three coordinates: Cartesian (default), ; cylindrical or spherical. ; OPTIONAL INPUT PARAMETERS: ; /angle if set then information about the angle between the two vectors is returned: ; i.e. cos(angle) = |r1.r2|/|r1||r2| ; /cylin, /sphere, /rect ; indicates whether input vectors are in cylindrical, spherical or rectangular ; coordinates. If none is set then rectangular is assumed. ; /degrees if set, and spherical or cylindrical coordinates are used, then the angles ; are assumed to be in degrees (default: radians) ; OUTPUTS: ; result array[*] same type as input ; scalar products or angle information are returned as array[n] ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; SyncArgs, SyncDims, InitVar ; PROCEDURE: ; > The arrays r1 and r2 do not necessarily have the same dimensions, i.e. ; if r1 is an array[3,n] and r2 is an array[3] then r2 is interpreted as an array[3,n] ; with all n vectors the same (SyncArgs is used to synchronize the array dimensions). ; > Arrays r1 and r2 can have more than two dimensions, i.e. if r1 and r2 are both ; arrays[3,n,m] then the scalar products will be returned as array[n,m] ; MODIFICATION HISTORY: ; AUG-1999, Paul Hick (UCSD/CASS) ; NOV-2005, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ; Extracted from vectorproduct procedure. ;- InitVar, degrees, /key InitVar, angle , /key InitVar, sphere , /key InitVar, cylin , /key cylin = cylin AND 1-sphere InitVar, rect , /key rect = rect OR (1-sphere AND 1-cylin) rr1 = r1 rr2 = r2 SyncArgs, rr1, rr2 sz = size(rr1) rr1 = reform(rr1,sz[1],sz[sz[0]+2]/sz[1], /overwrite) rr2 = reform(rr2,sz[1],sz[sz[0]+2]/sz[1], /overwrite) CASE 1 OF sphere: BEGIN rr1 = cv_coord(from_sphere=rr1, /to_rect, degrees=degrees) rr2 = cv_coord(from_sphere=rr2, /to_rect, degrees=degrees) END cylin: BEGIN rr1 = cv_coord(from_cylin =rr1, /to_rect, degrees=degrees) rr2 = cv_coord(from_cylin =rr2, /to_rect, degrees=degrees) END ELSE: ; Nothing to do ENDCASE pp = total(rr1*rr2,1) IF angle THEN pp = pp/sqrt(total(rr1*rr1,1)*total(rr2*rr2,1)) sz = [sz[0]-1 , sz[2:sz[0]+1] , sz[sz[0]+2]/sz[1]] SyncDims, pp, sizeinfo=sz RETURN, pp & END