The PRODUCT function returns the product of elements within an array. The product of the array elements over a given dimension is returned if the Dimension argument is present. Because the product can easily overflow, the product is computed using double-precision arithmetic and the Result is double precision.
Tip: If your array has a mix of very large and very small values, the product may underflow or overflow during the computation, even though the final result would be within double-precision limits. In this case, do not use PRODUCT, but instead compute the product by taking the logarithm, using the TOTAL function, and then taking the exponential: Result = EXP(TOTAL(ALOG(Array))).
Examples
To find the product of all elements in a one-dimensional array:
array = [20, 10, 5, 5, 3]
prod = PRODUCT(array)
PRINT, 'Product of Array = ', prod
IDL prints:
Product of Array = 15000.000
Now find the product of elements in a two-dimensional array:
array = FINDGEN(4,4) + 1
prodAll = PRODUCT(array)
prod1 = PRODUCT(array, 1)
prod2 = PRODUCT(array, 2)
PRINT, 'Product of all elements = ', prodAll
PRINT, 'Product along first dimension: '
PRINT, prod1
PRINT, 'Product along second dimension: '
PRINT, prod2
IDL prints:
Product of all elements 2.0922790e+013
Product along first dimension:
24.000000 1680.0000 11880.000 43680.000
Product along second dimension:
585.00000 1680.0000 3465.0000 6144.0000
Syntax
Result = PRODUCT(Array [, Dimension] [, /CUMULATIVE] [, /INTEGER] [, /NAN] [, /PRESERVE_TYPE] )
Return Value
Returns the product of the elements of Array.
Arguments
Array
The array for which to compute the product. This array can be of any basic type except string.
Dimension
An optional argument specifying the dimension over which to compute the product, starting at one. If this argument is not present or zero, the product of all the array elements is returned. If this argument is present, the result is an array with one less dimension than Array.
For example, if the dimensions of Array are N1, N2, N3, and Dimension is 2, the dimensions of the result are (N1, N3), and element [i,j] of the result contains the product:
Keywords
CUMULATIVE
If this keyword is set, the result is an array of the same size as the input, with each element, i, containing the product of the input array elements 0 to i. This keyword also works with the Dimension parameter, in which case the cumulative product is performed over the given dimension. Note that if the input only has a single element, then a scalar is returned.
Tip: If the input array is a temporary variable or an expression, and the result type matches the input type (for example by using the PRESERVE_TYPE keyword), then PRODUCT will perform the cumulative product in place and no additional memory will be used.
INTEGER
Set this keyword to perform the PRODUCT using integer arithmetic, and to return an integer result. If Array is of type ULONG64 then unsigned 64-bit integers are used for the computation and the Result is of type ULONG64, otherwise signed 64-bit integers are used and the Result is of type LONG64. If Array is complex and INTEGER is set, then only the real part of each value is used for the computation.
Note: If Array has a large number of values or the values themselves are large, then the PRODUCT with the INTEGER keyword may easily overflow the largest 64-bit integer and return an incorrect result. In this case you may want to avoid using the INTEGER keyword.
NAN
Set this keyword to cause the routine to check for occurrences of the IEEE floating-point values NaN or Infinity in the input data. Elements with the value NaN or Infinity are treated as missing data with the value 1.
PRESERVE_TYPE
Set this keyword to perform the PRODUCT using the input type, and to return a result of the same type. The INTEGER keyword is ignored if PRESERVE_TYPE is set.
Note: For byte or integer data the PRODUCT with PRESERVE_TYPE may easily overflow the largest value for that type. In these cases you may want to use the INTEGER keyword instead.
Thread Pool Keywords
This routine is written to make use of IDL's thread pool, which can increase execution speed on systems with multiple CPUs. The values stored in the !CPU system variable control whether IDL uses the thread pool for a given computation. In addition, you can use the thread pool keywords TPOOL_MAX_ELTS, TPOOL_MIN_ELTS, and TPOOL_NOTHREAD to override the defaults established by !CPU for a single invocation of this routine. See Thread Pool Keywords.
Version History
5.6 |
Introduced |
6.1 |
Added INTEGER and PRESERVE_TYPE keywords
|
See Also
FACTORIAL, TOTAL, IDL_Number