;+ ; NAME: ; PROD ; PURPOSE: ; Compute the product of the elements of an array. ; CATEGORY: ; Array operations. ; CALLING SEQUENCE: ; RESULT = PROD(X) ; INPUTS: ; X: Array of NON-ZERO elements, the product of which ; is desired. ; OPTIONAL INPUTS: ; None. ; KEYWORD PARAMETERS: ; None. ; OUTPUTS: ; RESULT: The product of the elements of X. ; OPTIONAL OUTPUTS: ; None. ; COMMON BLOCKS: ; None. ; SIDE EFFECTS: ; None. ; RESTRICTIONS: ; No zeroes in X. ; PROCEDURE: ; If the number of elements of X is greater than 20, X is ; broken into three segments which are then multiplied by ; each other to yield a new array, shorter by about 3X than ; the original. This procedure is repeated as necessary ; until the number of elements of the latest array is less ; than 20. The final array is then multiplied in a loop. ; Care is taken to handle arrays not MOD 3 correctly. ; EXAMPLE: ; test = PROD(RANDOMU(seed,1000)) ; SEE ALSO: ; MTOTAL.PRO ; MODIFICATION HISTORY: ; Written by: Rick White, 28 April 1995 ; Documented by: Dave Steele, 2 May 1995 ;- FUNCTION prod,x ; Compute product of elements of x. Returns result as double. ; R. White, 1995 April 28 n = N_ELEMENTS(x) y = DOUBLE(x) mm = 1.0D0 ; takes about log3(n) passes through this loop ; 20 elements is about the right break point where the direct loop becomes ; fastest WHILE (n GT 20) DO BEGIN mval = n MOD 3 CASE mval OF ; changed from IF to CASE, DPS 1: mm = mm*y(n-1) 2: mm = mm*y(n-1)*y(n-2) ELSE: ENDCASE n = n/3 y = y(0:n-1)*y(n:2*n-1)*y(2*n:3*n-1) ENDWHILE FOR i=0,n-1 DO mm=mm*y(i) RETURN, mm END