sum function

Tensor<Scalar> sum(
  1. Tensor<Vector> v
)

Implementation

Tensor<Scalar> sum(Tensor<Vector> v) {
  int N = v.data.length;
  double total = 0.0;
  for (int i = 0; i < N; i = i + 1) {
    total = total + v.data[i];
  }
  Tensor<Scalar> out = Tensor<Scalar>(total);
  out.creator = Node(
    [v],
        () {
      for (int i = 0; i < N; i = i + 1) {
        v.grad[i] = v.grad[i] + out.grad[0];
      }
    },
    opName: 'sum_vector',
    cost: N,
  );
  return out;
}