sum(3f) - [FORTRAN:INTRINSIC:ARRAY REDUCTION] sum the elements of an array
Syntax
Description
Arguments
Return Value
Example
Standard
Class
See Also
Calling sequence:
result = sum(array[, mask]) result = sum(array, dim[, mask])
Adds the elements of ARRAY along dimension DIM if the corresponding element in MASK is TRUE.
array - Shall be an array of type INTEGER, REAL or COMPLEX. dim - (Optional) shall be a scalar of type INTEGER with a value in the range from 1 to n, where n equals the rank of ARRAY. mask - (Optional) shall be of type LOGICAL and either be a scalar or an array of the same shape as ARRAY.
The result is of the same type as ARRAY.
If dim(3f) is absent, a scalar with the sum of all elements in ARRAY is returned. 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.
Sample program:
program demo_sum integer :: x(5) = (/ 1, 2, 3, 4 ,5 /) print *, sum(x) ! all elements, sum = 15 print *, sum(x, mask=mod(x, 2)==1) ! odd elements, sum = 9 end program demo_sumDemonstrate Fortran 90 SUM function with MASK option
! John Mahaffy 2/16/96 implicit none integer nd,ndh,nduh, j parameter (nd=10,ndh=nd/2,nduh=nd-ndh) real csum,cpsum,cbpsum real, dimension(nd):: c=(/(j,j=-1,nd-2)/), b data b/ndh*-1.0,nduh*2.0/ csum= sum(c(1:nd)) cpsum= sum (c(1:nd),mask=c.gt.0) cbpsum= sum(c(1:nd),mask=b.gt.0.0) print *, Sum of all elements in c = , csum print *, Sum of Positive elements in c = , cpsum print *, Sum of elements in c when corresponding elements in b>0 , =,cbpsum end
[[Fortran 95]] and later
Transformational function
intrinsics
sum (3) | March 19, 2019 |