Manual Reference Pages  - product (3fortran)

NAME

PRODUCT(3) - [ARRAY:REDUCTION] Product of array elements

SYNOPSIS

result = product(array [,dim] [,mask])

         NUMERIC function product(array, dim, mask)

NUMERIC,intent(in) :: array(..) integer(kind=**),intent(in),optional :: dim logical(kind=**),intent(in),optional :: mask(..)

CHARACTERISTICS

o a kind designated as ** may be any supported kind for the type
o NUMERIC is any numeric type and kind.

DESCRIPTION

PRODUCT(3) multiplies together all the selected elements of ARRAY, or along dimension DIM if the corresponding element in MASK is .true..

If DIM is absent, a scalar with the product of all elements in ARRAY is returned. (Note a zero-sized ARRAY returns 1).

When DIM is present, If the masked array has a dimension of one (ie. is a vector) the result is a scalar. Otherwise, an array of rank N-1, where N equals the rank of ARRAY, and a shape similar to that of ARRAY with dimension DIM dropped is returned.

OPTIONS

o ARRAY : Shall be an array of type integer, real or complex.
o DIM : shall be a scalar of type integer with a value in the range from 1 TO N, where N equals the rank of ARRAY.
o MASK : shall be of type logical and either be a scalar or an array of the same shape as ARRAY.

RESULT

The result is of the same type as ARRAY.

EXAMPLES

Sample program:

    program demo_product
    implicit none
    character(len=*),parameter :: all=’(*(g0,1x))’ ! a handy format
    character(len=1),parameter :: nl=new_line(’a’)

NO_DIM: block ! If DIM is not specified, the result is the product of all the ! selected array elements. integer :: i,n, p1, p2 integer,allocatable :: array(:) ! all elements are selected by default do n=1,10 print all, ’factorial of ’,n,’ is ’, product([(real(i),i=1,n)]) enddo

! using a mask array=[10,12,13,15,20,25,30] p1=product(array, mask=mod(array, 2)==1) ! only odd elements p2=product(array, mask=mod(array, 2)/=1) ! only even elements print all, nl,’product of all elements’,product(array) ! all elements print all, ’ odd * even =’,nl,p1,’*’,p2,’=’,p1*p2

! NOTE: If ARRAY is a zero-sized array, the result is equal to one print all print all, ’zero-sized array=>’,product([integer :: ]) ! NOTE: If nothing in the mask is true, this also results in a null ! array print all, ’all elements have a false mask=>’, & & product(array,mask=.false.)

endblock NO_DIM

WITH_DIM: block integer :: rect(2,3) integer :: box(2,3,4)

! lets fill a few arrays rect = reshape([ & 1, 2, 3, & 4, 5, 6 & ],shape(rect),order=[2,1]) call print_matrix_int(’rect’,rect)

! Find the product of each column in RECT. print all, ’product of columns=’,product(rect, dim = 1)

! Find the product of each row in RECT. print all, ’product of rows=’,product(rect, dim = 2)

! now lets try a box box(:,:,1)=rect box(:,:,2)=rect*(+10) box(:,:,3)=rect*(-10) box(:,:,4)=rect*2 ! lets look at the values call print_matrix_int(’box 1’,box(:,:,1)) call print_matrix_int(’box 2’,box(:,:,2)) call print_matrix_int(’box 3’,box(:,:,3)) call print_matrix_int(’box 4’,box(:,:,4))

! remember without dim= even a box produces a scalar print all, ’no dim gives a scalar’,product(real(box))

! only one plane has negative values, so note all the "1" values ! for vectors with no elements call print_matrix_int(’negative values’, & & product(box,mask=box < 0,dim=1))

! If DIM is specified and ARRAY has rank greater than one, the ! result is a new array in which dimension DIM has been eliminated.

! pick a dimension to multiply though call print_matrix_int(’dim=1’,product(box,dim=1))

call print_matrix_int(’dim=2’,product(box,dim=2))

call print_matrix_int(’dim=3’,product(box,dim=3))

endblock WITH_DIM

contains

subroutine print_matrix_int(title,arr) implicit none

!@(#) print small 2d integer arrays in row-column format

character(len=*),intent(in) :: title integer,intent(in) :: arr(:,:) integer :: i character(len=:),allocatable :: biggest

print all print all, trim(title),’:(’,shape(arr),’)’ ! print title biggest=’ ’ ! make buffer to write integer into ! find how many characters to use for integers write(biggest,’(i0)’)ceiling(log10(max(1.0,real(maxval(abs(arr))))))+2 ! use this format to write a row biggest=’(" > [",*(i’//trim(biggest)//’:,","))’ ! print one row of array at a time do i=1,size(arr,dim=1) write(*,fmt=biggest,advance=’no’)arr(i,:) write(*,’(" ]")’) enddo

end subroutine print_matrix_int

end program demo_product

Results:

    factorial of  1  is  1.000000
    factorial of  2  is  2.000000
    factorial of  3  is  6.000000
    factorial of  4  is  24.00000
    factorial of  5  is  120.0000
    factorial of  6  is  720.0000
    factorial of  7  is  5040.000
    factorial of  8  is  40320.00
    factorial of  9  is  362880.0
    factorial of  10  is  3628800.

product of all elements 351000000 odd * even = 4875 * 72000 = 351000000

zero-sized array=> 1 all elements have a false mask=> 1

rect :( 2 3 ) > [ 1, 2, 3 ] > [ 4, 5, 6 ] product of columns= 4 10 18 product of rows= 6 120

box 1 :( 2 3 ) > [ 1, 2, 3 ] > [ 4, 5, 6 ]

box 2 :( 2 3 ) > [ 10, 20, 30 ] > [ 40, 50, 60 ]

box 3 :( 2 3 ) > [ -10, -20, -30 ] > [ -40, -50, -60 ]

box 4 :( 2 3 ) > [ 2, 4, 6 ] > [ 8, 10, 12 ] no dim gives a scalar .1719927E+26

negative values :( 3 4 ) > [ 1, 1, 400, 1 ] > [ 1, 1, 1000, 1 ] > [ 1, 1, 1800, 1 ]

dim=1 :( 3 4 ) > [ 4, 400, 400, 16 ] > [ 10, 1000, 1000, 40 ] > [ 18, 1800, 1800, 72 ]

dim=2 :( 2 4 ) > [ 6, 6000, -6000, 48 ] > [ 120, 120000, -120000, 960 ]

dim=3 :( 2 3 ) > [ -200, -3200, -16200 ] > [ -51200, -125000, -259200 ]

STANDARD

Fortran 95

SEE ALSO

SUM(3), note that an element by element multiplication is done directly using the star character.

fortran-lang intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 product (3fortran) April 28, 2024
Generated by manServer 1.08 from af3c8a35-37b1-4ea8-80de-76cc0a2cd487 using man macros.