sum

Syntax

sum(X)

Details

If X is a vector, calculates the sum of all the elements in X.

If X is a matrix, calculates the sum of each column of X and return a vector.

If X is a table, calculates the sum of each column of X and return a table.

As with all aggregate functions, null values are not included in the calculation.

If all elements of a calculation are null values, the result is NULL.

Note:

DolphinDB sum, numpy.sum, and Python built-in sum are all used for summation. The differences are as follows:

  • DolphinDB sum is an aggregate function that supports scalars, vectors, matrices, and tables. For matrices and tables, summation is performed column-wise, and NULL values are ignored.

  • numpy.sum is an array reduction function. By default, it sums all elements, and dimensions can be specified with the axis parameter. It also supports parameters such as dtype, out, keepdims, initial, and where.

  • Python built-in sum only accumulates elements in an iterable from left to right. It does not support axis-based computation and does not automatically ignore missing values.

Parameters

X is a scalar/vector/matrix/table.

Examples

sum(1 2 3 NULL 4);
// output: 10

m=matrix(1 2 3, 4 5 6);
m;
#0 #1
1 4
2 5
3 6
sum(m);
// output: [6,15]