Multiplies together all elements in an entire array, or selected elements from all vectors in a specified dimension of an array.
Transformational function
If DIM is present, the result is an array of rank rank(ARRAY)-1 and the same data type and kind type parameter as ARRAY. If DIM is missing, or if MASK has a rank of one, the result is a scalar.
If DIM is also specified and ARRAY has rank greater than one, the result is a new array in which dimension DIM has been eliminated. Each new array element is the product of elements from a corresponding vector within ARRAY. The index values of that vector, in all dimensions except DIM, match those of the output element. The output element is the product of those vector elements that have a corresponding .TRUE. array element in MASK.
! Multiply all elements in an array.
RES = PRODUCT( (/2, 3, 4/) )
! The result is 24 because (2 * 3 * 4) = 24.
! Do the same for a two-dimensional array A, where
! A is the array | 2 3 4 |
! | 4 5 6 |
RES = PRODUCT(A)
! The result is 2880. All elements are multiplied.
! A is the array (/ -3, -7, -5, 2, 3 /)
! Multiply all elements of the array that are > -5.
RES = PRODUCT(A, MASK = A .GT. -5)
! The result is -18 because (-3 * 2 * 3) = -18.
! A is the array | -2 5 7 |
! | 3 -4 3 |
! Find the product of each column in A.
RES = PRODUCT(A, DIM = 1)
! The result is | -6 -20 21 | because (-2 * 3) = -6
! ( 5 * -4 ) = -20
! ( 7 * 3 ) = 21
! Find the product of each row in A.
RES = PRODUCT(A, DIM = 2)
! The result is | -70 -36 |
! because (-2 * 5 * 7) = -70
! (3 * -4 * 3) = -36
! Find the product of each row in A, considering
! only those elements greater than zero.
RES = PRODUCT(A, DIM = 2, MASK = A .GT. 0)
! The result is | 35 9 | because ( 5 * 7) = 35
! (3 * 3) = 9