16.4 Sums and Products


y = 37409 sum (x {,d})
computes the sum of the elements of x, optionally with respect to dimension d
y = 37412 cumsum (x {,d})
computes the cumulative sum of the elements of x, optionally with respect to dimension d
y = 37415 prod (x {,d})
computes the product of the elements of x, optionally with respect to dimension d
y = 37418 cumprod (x {,d})
computes the cumulative product of the elements of x, optionally with respect to dimension d

The function 37421 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 37424 XLGmatrix06.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 37427 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 37430 prod and   37433 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 37436 sum and 37439 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