## Sum

### Function Type:

The sum function is a mathematical transformation function.

### Function Purpose:

The sum function will sum up long strings of numbers. Because of this, the sum function can be particularly useful when addition needs to be carried out on arrays. Additionally, the way in which sum carries this out can be altered using the dim and mask attributes.

### Required Arguments:

sum (array)

Array can either be an array variable of any rank or a string of numbers enclosed in back slashes. The results of a sum with only the array argument specified will be a scalar.

### Optional Arguments:

If dim is specified, the result of the sum will be an array of rank n-1. For instance, if a two dimensional array is passed to the sum function and dim=1 is specified, the results will be an array of rank one. The values of each element in that resultant array will then contain the sum of all the values in the corresponding column of the original array.

The mask specifier can be any logical expression that you might want to use to limit the way that the sum function works. Just as a quick example, if mask= a.gt.0 is specified, it will limit the sum function to adding up only those values in array in which a ( this can either be the array that you passed to the function or some other array with the same shape) is greater than zero.

Please note that both mask and dim can be used in conjunction.

### Results:

The following two arrays, numbers and ndata, are going to have various sums done on them.
```		Numbers = -9  6  8  3   ndata = 2 -8 -2 11
10  0 -5 -1          -1 -7 15 -1
-3  4  2 11          12 -5 -2 17
```
For the first sum, we are going to add up all the values in numbers. To do this the following is the only line of code that is required:
```		res1 = sum(numbers)
```
This summation will result in res1 equaling twenty six. Now, if you were interested in summing up the values in each row of ndata the following line of code is what would be required.
```		res2 = sum( ndata, dim=2)
```
With this sum, res2 will be a one dimensional array containing three elements. The values of these three elements will be three, six and twenty two. The following example illustrates summation for each column. A further restriction is imposed by the mask option so that only numbers less than or equal to two contribute to the sum computed for any given column.
```		res3 = sum ( numbers,dim=1,mask=numbers.le.2 )
```
The result of this summation is a one dimensional array containing four elements whose values would be -12,0,-3,-1. Finally, as a last example, we find the sum of all elements in ndata which are greater than three. To do this, the line of code required is the following.
```		res4 = sum (ndata,mask=numbers.gt.3)
```
This time, res4 will be a scalar with the value of one associated with it.

### For additional information and examples see:

lectures nineteen and thirty six

examples: dimtest.f, speed.f and lsq3.f

## Up one level / Home

Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu