4. Sums and Products


y = 1953 sum (x {,d})
computes the sum of the elements of x, optionally with respect to dimension d
y = 1956 cumsum(x {,d})
computes the cumulative sum of the elements of x, optionally with respect to dimension d
y = 1959 prod(x {,d})
computes the product of the elements of x, optionally with respect to dimension d
y = 1962 cumprod(x {,d})
computes the cumulative product of the elements of x, optionally with respect to dimension d

The function 1965 sum computes the sum of the elements of an array with respect to a given dimension. The default dimension is the first one, i.e. the elements of the matrix are summed columnwise. The following and all other examples of this section can be found in 1968matrix06.xpl.

  x=#(1,3)~#(2,4)
  sum(x)
displays
  Contents of sum
  [1,]        4        6
while
  sum(x,2)
gives
  [1,]        3
  [2,]        7

Similarly, the function 1971 cumsum computes the cumulative sum of the elements of an array with respect to a given dimension:

  cumsum(#(5,4,3)~#(1,2,3))
yields
  Contents of cumsum
  [1,]        5        1 
  [2,]        9        3 
  [3,]       12        6

The functions 1974 prod and  1977 cumprod evaluate respectively the product and cumulative product of the elements of a matrix with respect to a given dimension. The syntax of these functions is the same as the functions 1980 sum and 1983 cumsum. Thus,

  prod(#(5,4,3)~#(1,2,3))
returns
  Contents of mul
  [1,]       60        6
while
  cumprod(#(5,4,3)~#(1,2,3))
gives
  Contents of cumprod
  [1,]        5        1 
  [2,]       20        2 
  [3,]       60        6



Method and Data Technologies   MD*TECH Method and Data Technologies
  http://www.mdtech.de  mdtech@mdtech.de