beetlebug▾
/

ztcl::tensor sum

Gives the sum of a tensor or of one axis.

ztcl::tensor sum TENSOR ?-axis N? ?-keepdims? ?-fast?

Description

Gives the sum of all elements, or along the axis N with -axis. A negative N counts from the end. With -keepdims, the reduced axis stays with the size 1. A float sum goes from the first element to the last in an f64 accumulator. An i32 sum wraps, and an i32 mean goes toward zero. -fast adds all elements in 16 lanes and then a fixed tree: the order is fixed, so the bits are the same in each build, but they can differ from the plain sum in the last place.

Returns

A tensor.

Examples

Each example of this page runs after package require ztcl::tensor.

% set a [ztcl::tensor of {{1 2 3} {4 5 6}}]; ztcl::tensor sum $a
tensor f32 {} 21.0

% set a [ztcl::tensor of {{1 2 3} {4 5 6}}]; ztcl::tensor sum $a -axis 1
tensor f32 2 {6.0 15.0}

% set a [ztcl::tensor of {{1 2 3} {4 5 6}}]; ztcl::tensor sum $a -axis 0 -keepdims
tensor f32 {1 3} {5.0 7.0 9.0}

% ztcl::tensor sum [ztcl::tensor of {1 2 3} -dtype f64] -fast
tensor f64 {} 6.0

Errors

% set a [ztcl::tensor of {{1 2 3} {4 5 6}}]; ztcl::tensor sum $a -axis 2
tensor sum: axis is outside the rank 2
-errorcode ZTCL TENSOR

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.4
Compute cost
A cost for each element operation by its kind (a copy, an add, a division, a math function, a multiply-add of a matmul), set so that one compute unit takes about the time of one unit of plain Tcl. docs/modules/tensor.md has the table.