Manual Reference Pages  - sum (3fortran)

NAME

SUM(3) - [ARRAY:REDUCTION] Sum the elements of an array

SYNOPSIS

result = sum(array [,dim[,mask]] | [mask] )

         TYPE(kind=KIND) function sum(array, dim, mask)

TYPE(kind=KIND),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 ARRAY may be of any numeric type - integer, real or complex.
o DIM is an integer
o MASK is logical and conformable with ARRAY.
o The result is of the same type and kind as ARRAY. It is scalar if DIM is not present or ARRAY is a vector, else it is an array.

DESCRIPTION

SUM(3) adds the elements of ARRAY.

When only ARRAY is specified all elements are summed, but groups of sums may be returned along the dimension specified by DIM and/or elements to add may be selected by a logical mask.

No method is designated for how the sum is conducted, so whether or not accumulated error is compensated for is processor-dependent.

OPTIONS

o ARRAY : an array containing the elements to add
o DIM : a value in the range from 1 to n, where n equals the rank (the number of dimensions) of ARRAY. DIM designates the dimension along which to create sums. When absent a scalar sum of the elements optionally selected by MASK is returned.
o MASK : an array of the same shape as ARRAY that designates which elements to add. If absent all elements are used in the sum(s).

RESULT

If DIM is absent, a scalar with the sum of all selected 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. Since a vector has a rank of one, the result is a scalar (if n==1, n-1 is zero; and a rank of zero means a scalar).

EXAMPLES

Sample program:

    program demo_sum
    implicit none
    integer :: vector(5) , matrix(3,4), box(5,6,7)

vector = [ 1, 2, -3, 4, 5 ]

matrix(1,:)=[ -1, 2, -3, 4 ] matrix(2,:)=[ 10, -20, 30, -40 ] matrix(3,:)=[ 100, 200, -300, 400 ]

box=11

! basics print *, ’sum all elements:’,sum(vector) print *, ’real :’,sum([11.0,-5.0,20.0]) print *, ’complex :’,sum([(1.1,-3.3),(4.0,5.0),(8.0,-6.0)]) ! with MASK option print *, ’sum odd elements:’,sum(vector, mask=mod(vector, 2)==1) print *, ’sum positive values:’, sum(vector, mask=vector>0)

call printi(’the input array’, matrix ) call printi(’sum of all elements in matrix’, sum(matrix) ) call printi(’sum of positive elements’, sum(matrix,matrix>=0) ) ! along dimensions call printi(’sum along rows’, sum(matrix,dim=1) ) call printi(’sum along columns’, sum(matrix,dim=2) ) call printi(’sum of a vector is always a scalar’, sum(vector,dim=1) ) call printi(’sum of a volume by row’, sum(box,dim=1) ) call printi(’sum of a volume by column’, sum(box,dim=2) ) call printi(’sum of a volume by depth’, sum(box,dim=3) )

contains ! CONVENIENCE ROUTINE; NOT DIRECTLY CONNECTED TO SPREAD(3) subroutine printi(title,a) use, intrinsic :: iso_fortran_env, only : stderr=>ERROR_UNIT,& & stdin=>INPUT_UNIT, stdout=>OUTPUT_UNIT implicit none

!@(#) print small 2d integer scalar, vector, matrix in row-column format

character(len=*),intent(in) :: title integer,intent(in) :: a(..)

character(len=*),parameter :: all=’(" ",*(g0,1x))’ character(len=20) :: row integer,allocatable :: b(:,:) integer :: i write(*,all,advance=’no’)trim(title) ! copy everything to a matrix to keep code simple select rank(a) rank (0); write(*,’(a)’)’ (a scalar)’; b=reshape([a],[1,1]) rank (1); write(*,’(a)’)’ (a vector)’; b=reshape(a,[size(a),1]) rank (2); write(*,’(a)’)’ (a matrix)’; b=a rank default; stop ’*printi* unexpected rank’ end select ! find how many characters to use for integers write(row,’(i0)’)ceiling(log10(max(1.0,real(maxval(abs(b))))))+2 ! use this format to write a row row=’(" > [",*(i’//trim(row)//’:,","))’ do i=1,size(b,dim=1) write(*,fmt=row,advance=’no’)b(i,:) write(*,’(" ]")’) enddo write(*,all) ’>shape=’,shape(a),’,rank=’,rank(a),’,size=’,size(a) write(*,*) end subroutine printi end program demo_sum

Results:

     >  sum all elements:           9
     >  real :   26.0000000   
     >  complex :            (13.1000004,-4.30000019)
     >  sum odd elements:           6
     >  sum positive values:          12
     >  the input array  (a matrix)
     >  > [   -1,    2,   -3,    4 ]
     >  > [   10,  -20,   30,  -40 ]
     >  > [  100,  200, -300,  400 ]
     >  >shape= 3 4 ,rank= 2 ,size= 12
     >
     >  sum of all elements in matrix  (a scalar)
     >  > [  382 ]
     >  >shape= ,rank= 0 ,size= 1
     >
     >  sum of positive elements  (a scalar)
     >  > [  746 ]
     >  >shape= ,rank= 0 ,size= 1
     >
     >  sum along rows  (a vector)
     >  > [  109 ]
     >  > [  182 ]
     >  > [ -273 ]
     >  > [  364 ]
     >  >shape= 4 ,rank= 1 ,size= 4
     >
     >  sum along columns  (a vector)
     >  > [    2 ]
     >  > [  -20 ]
     >  > [  400 ]
     >  >shape= 3 ,rank= 1 ,size= 3
     >
     >  sum of a vector is always a scalar  (a scalar)
     >  > [  9 ]
     >  >shape= ,rank= 0 ,size= 1
     >
     >  sum of a volume by row  (a matrix)
     >  > [  55,  55,  55,  55,  55,  55,  55 ]
     >  > [  55,  55,  55,  55,  55,  55,  55 ]
     >  > [  55,  55,  55,  55,  55,  55,  55 ]
     >  > [  55,  55,  55,  55,  55,  55,  55 ]
     >  > [  55,  55,  55,  55,  55,  55,  55 ]
     >  > [  55,  55,  55,  55,  55,  55, 113 ]
     >  >shape= 6 7 ,rank= 2 ,size= 42
     >
     >  sum of a volume by column  (a matrix)
     >  > [  66,  66,  66,  66,  66,  66,  66 ]
     >  > [  66,  66,  66,  66,  66,  66,  66 ]
     >  > [  66,  66,  66,  66,  66,  66,  66 ]
     >  > [  66,  66,  66,  66,  66,  66,  66 ]
     >  > [  66,  66,  66,  66,  66,  66,**** ]
     >  >shape= 5 7 ,rank= 2 ,size= 35
     >
     >  sum of a volume by depth  (a matrix)
     >  > [  77,  77,  77,  77,  77,  77 ]
     >  > [  77,  77,  77,  77,  77,  77 ]
     >  > [  77,  77,  77,  77,  77,  77 ]
     >  > [  77,  77,  77,  77,  77,  77 ]
     >  > [  77,  77,  77,  77,  77,4193 ]
     >  >shape= 5 6 ,rank= 2 ,size= 30
     >

STANDARD

Fortran 95

SEE ALSO

o ALL(3) - Determines if all the values are true
o ANY(3) - Determines if any of the values in the logical array are true.
o COUNT(3) - Count true values in an array
o MAXVAL(3) - Determines the maximum value in an array
o MINVAL(3) - Minimum value of an array
o PRODUCT(3) - Product of array elements
o MERGE(3) - Merge variables
Fortran intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 sum (3fortran) November 02, 2024
Generated by manServer 1.08 from 717d4c45-8781-44d0-922e-9626d46c72cb using man macros.