Sums and Products (GNU Octave (version 11.2.0))

### 17.4 Sums and Products ¶

`y =` sum `(x)` ¶
`y =` sum `(x, dim)` ¶
`y =` sum `(x, vecdim)` ¶
`y =` sum `(x, "all")` ¶
`y =` sum `(…, outtype)` ¶
`y =` sum `(…, nanflag)` ¶

  Compute the sum of the elements of x.

  If x is a vector, then `sum (x)` returns the sum of the elements in x.

  If x is a matrix, then `sum (x)` returns a row vector with each element containing the sum of the corresponding column in x.

  If x is an array, then `sum(x)` computes the sum along the first non-singleton dimension of x.

  The optional input dim specifies the dimension to operate on and must be a positive integer. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will return x.

  Specifying multiple dimensions with input vecdim, a vector of non-repeating dimensions, will operate along the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension in vecdim greater than `ndims (x)` is ignored.

  Specifying the dimension as `"all"` will cause `sum` to operate on all elements of x, and is equivalent to `cumsum (x(:))`.

  The optional input outtype specifies the data type that is returned as well as the class of the variable used for calculations. outtype can take the following values:

  `"default"`

    Operations on floating point inputs (double or single) are performed in their native data type, while operations on integer, logical, and character data types are performed using doubles. Output is of type double, unless the input is single in which case the output is of type single.

  `"double"`

    Operations are performed in double precision even for single precision inputs. Output is of type double.

  `"extra"`

    For double precision inputs, `sum` will use a more accurate algorithm than straightforward summation. For single precision inputs, `"extra"` is the same as `"double"`. For all other data types, `"extra"` has no effect.

  `"native"`

    Operations are performed in their native data types and output is of the same type as the input as reported by (`class (x)`). When the input is logical, `sum (x, "native")` is equivalent to `any (x)`.

  The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is `"includenan"` which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to `"omitnan"`. The output will be 0, if x consists of all NaN values in the operating dimension.

  See also: cumsum, sumsq, prod.

`y =` prod `(x)` ¶
`y =` prod `(x, dim)` ¶
`y =` prod `(x, vecdim)` ¶
`y =` prod `(x, "all")` ¶
`y =` prod `(…, outtype)` ¶
`y =` prod `(…, nanflag)` ¶

  Compute the product of the elements of x.

  If x is a vector, then `prod (x)` returns the product of the elements in x.

  If x is a matrix, then `prod (x)` returns a row vector with each element containing the product of the corresponding column in x.

  If x is an array, then `prod(x)` computes the product along the first non-singleton dimension of x.

  The optional input dim specifies the dimension to operate on and must be a positive integer. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will return x.

  Specifying multiple dimensions with input vecdim, a vector of non-repeating dimensions, will operate along the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension in vecdim greater than `ndims (x)` is ignored.

  Specifying the dimension as `"all"` will cause `prod` to operate on all elements of x, and is equivalent to `prod (x(:))`.

  The optional input outtype specifies the data type that is returned as well as the class of the variable used for calculations. outtype can take the following values:

  `"default"`

    Operations on floating point inputs (double or single) are performed in their native data type; while operations on integer, logical, and character data types are performed using doubles. Output is of type double, unless the input is single in which case the output is of type single.

  `"double"`

    Operations are performed in double precision even for single precision inputs. Output is of type double.

  `"native"`

    Operations are performed in their native data types and output is of the same type as the input as reported by (`class (x)`). When the input is logical, `prod (x, "native")` is equivalent to `all (x)`.

  The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is `"includenan"` which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to `"omitnan"`. The output will be 1, if x consists of all NaN values in the operating dimension.

  See also: cumprod, sum.

`y =` cumsum `(x)` ¶
`y =` cumsum `(x, dim)` ¶
`y =` cumsum `(x, vecdim)` ¶
`y =` cumsum `(…, "all")` ¶
`y =` cumsum `(…, direction)` ¶
`y =` cumsum `(…, nanflag)` ¶

  Compute the cumulative sum of elements in x.

  If x is a vector, then `cumsum (x)` returns a vector of the same size with the cumulative sum of x.

  If x is a matrix, then `cumsum (x)` returns a matrix of the same size with the cumulative sum along each column of x.

  If x is an array, then `cumsum(x)` returns an array of the same size with the cumulative sum along the first non-singleton dimension of x.

  The class of output y is the same as the class of input x, unless x is logical, in which case y is double.

  The optional input dim specifies the dimension to operate on and must be a positive integer. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will return x.

  Specifying multiple dimensions with input vecdim, a vector of non-repeating dimensions, will operate along the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension in vecdim greater than `ndims (x)` is ignored.

  Specifying the dimension as `"all"` will cause `cumsum` to operate on all elements of x, and is equivalent to `cumsum (x(:))`.

  The optional input direction specifies how the operating dimension is traversed and can take the following values:

  `"forward"` (default)

    The cumulative sum is computed from beginning (index 1) to end along the operating dimension.

  `"reverse"`

    The cumulative sum is computed from end to beginning along the operating dimension.

  The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is `"includenan"` which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to `"omitnan"`. The output will still contain NaN values if x consists of all NaN values in the operating dimension.

  See also: sum, cumprod.

`y =` cumprod `(x)` ¶
`y =` cumprod `(x, dim)` ¶
`y =` cumprod `(x, vecdim)` ¶
`y =` cumprod `(…, "all")` ¶
`y =` cumprod `(…, direction)` ¶
`y =` cumprod `(…, nanflag)` ¶

  Compute the cumulative product of elements in x.

  If x is a vector, then `cumprod (x)` returns a vector of the same size with the cumulative product of x.

  If x is a matrix, then `cumprod (x)` returns a matrix of the same size with the cumulative product along each column of x.

  If x is an array, then `cumprod(x)` returns an array of the same size with the cumulative product along the first non-singleton dimension of x.

  The class of output y is the same as the class of input x, unless x is logical, in which case y is double.

  The optional input dim specifies the dimension to operate on and must be a positive integer. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will return x.

  Specifying multiple dimensions with input vecdim, a vector of non-repeating dimensions, will operate along the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension in vecdim greater than `ndims (x)` is ignored.

  Specifying the dimension as `"all"` will cause `cumprod` to operate on all elements of x, and is equivalent to `cumprod (x(:))`.

  The optional input direction specifies how the operating dimension is traversed and can take the following values:

  `"forward"` (default)

    The cumulative product is computed from beginning (index 1) to end along the operating dimension.

  `"reverse"`

    The cumulative product is computed from end to beginning along the operating dimension.

  The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is `"includenan"` which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to `"omitnan"`. The output will still contain NaN values if x consists of all NaN values in the operating dimension.

  See also: prod, cumsum.

`y =` sumsq `(x)` ¶
`y =` sumsq `(x, dim)` ¶
`y =` sumsq `(x, vecdim)` ¶
`y =` sumsq `(x, "all")` ¶
`y =` sumsq `(…, outtype)` ¶
`y =` sumsq `(…, nanflag)` ¶

  Compute the sum of squares of the elements of x.

  If x is a vector, then `sumsq (x)` returns the sum of the squares of the elements in x.

  If x is a matrix, then `sumsq (x)` returns a row vector with each element containing the sum of squares of the corresponding column in x.

  If x is an array, then `sumsq(x)` computes the sum of squares along the first non-singleton dimension of x.

  This function is conceptually equivalent to computing

      sum (x .* conj (x))

  but it uses less memory and avoids calling `conj` if x is real.

  The optional input dim specifies the dimension to operate on and must be a positive integer. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will return a sum of squares equal to `x.^2`.

  Specifying multiple dimensions with input vecdim, a vector of non-repeating dimensions, will operate along the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension in vecdim greater than `ndims (x)` is ignored.

  Specifying the dimension as `"all"` will cause `prod` to operate on all elements of x, and is equivalent to `sumsq (x(:))`.

  The optional input outtype specifies the data type that is returned as well as the class of the variable used for calculations. outtype can take the following values:

  `"default"`

    Operations on floating point inputs (double or single) are performed in their native data type; while operations on integer, logical, and character data types are performed using doubles. Output is of type double, unless the input is single in which case the output is of type single.

  `"double"`

    Operations are performed in double precision even for single precision inputs. Output is of type double.

  `"native"`

    Operations are performed in their native data types and output is of the same type as the input as reported by (`class (x)`). When the input is logical, `sumsq (x, "native")` is equivalent to `all (x)`.

  The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is `"includenan"` which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to `"omitnan"`. The output will be 0, if x consists of all NaN values in the operating dimension.

  See also: sum, prod.