sum method

double sum()

Returns the sum of all elements in this tensor.

final tensor = TensorBuffer.fromFloat32List(
  Float32List.fromList([1, 2, 3, 4]),
  [2, 2],
);
print(tensor.sum()); // 10.0

Implementation

double sum() {
  double result = 0;
  final indices = List<int>.filled(rank, 0);

  for (int i = 0; i < numel; i++) {
    int offset = storageOffset;
    for (int d = 0; d < rank; d++) {
      offset += indices[d] * strides[d];
    }
    result += storage.getAsDouble(offset);
    _incrementIndices(indices);
  }

  return result;
}