Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


sum

sum of array elements

Syntax

y = sum(x)
y = sum(x, outtype)
y = sum(x, orientation)
y = sum(x, orientation, outtype)

Arguments

x
Array of booleans (full or sparse), encoded integers, real or complex numbers (full or sparse), polynomials, or rationals.

orientation
it can be either
  • a character "*" (default), "r", "c" or "m"
  • a positive integer: Index of the dimension along which the sums must be computed.

outtype
string "native" or "double".

y
scalar or array

Description

For an array x, y=sum(x) returns in the scalar y the sum of all the elements of x.

y=sum(x,orientation) returns in y the sum of x along the dimension given by orientation:

  • if orientation is equal to 1 or "r" then

    { y(\mathbf{1},j) = \sum_{\mathbf{i}} x(\mathbf{i},j)}

    or

    y(\mathbf{1},j,k,\ldots) = \sum_{\mathbf{i}} x(\mathbf{i},j,k,\ldots)

  • if orientation is equal to 2 or "c" then:

    y(i,\mathbf{1}) = \sum_{\mathbf{j}} x(i,\mathbf{j})

    or

    y(i,\mathbf{1},k,\ldots) = \sum_{j} x(i,\mathbf{j},k,\ldots)

  • if orientation is equal to n then

    y(i_1,\ldots,i_{n-1},\mathbf{1},i_{n+1},\ldots) = \sum_{\mathbf{i_n}} x(i_1,\ldots,i_{n-1},\mathbf{i_n},i_{n+1},\ldots)

  • y=sum(x,"*") is equivalent to y=sum(x)

  • y=sum(x,"m") is equivalent to y=sum(x,orientation) where orientation is the index of the first dimension of x that is greater than 1.

The outtype argument rules the way the summation is done:

  • For arrays of floats, of polynomials, of rational fractions, the evaluation is always done using floating points computations. The "double" or "native" options are equivalent.

  • For arrays of integers,

    if outtype="native" the evaluation is done using integer computations (modulo 2^b, where b is the number of bits used),

    if outtype="double" the evaluation is done using floating point computations.

    The default value is outtype="native".

  • For arrays of booleans,

    if outtype="native" the evaluation is done using boolean computations ( + is replaced by |),

    if outtype="double" the evaluation is done using floating point computations (%t values are replaced by 1 and %f values by 0).

    The default value is outtype="double".

This function applies with identical rules to sparse matrices

Examples

A = [1,2 ; 3,4];
sum(A)
sum(A,1)

I = uint8([2 95 103;254 9 0])
sum(I) // native evaluation
sum(I, "double")
sum(I, 2, "double")

s = poly(0,"s");
P = [s,%i+s;s^2,1];
sum(P),
sum(P, 2)

B = [%t %t %f %f];
sum(B) //evaluation in float
sum(B, "native") //similar to or(B)

See also

  • plus — Numerical addition. Text concatenation (gluing)
  • cumsum — partial cumulative sums of the elements of an array
  • prod — product of array elements
Report an issue
<< sign Matrix operations Search and sort >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:08 CEST 2024