sum function

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

Implementation

Tensor<Scalar> sum(Tensor<Vector> v) {
  int N = v.value.length;
  Scalar total = 0.0;
  for (int i = 0; i < N; i++) {
    total += v.value[i];
  }
  Tensor<Scalar> out = Tensor<Scalar>(total);
  out.creator = Node(
    [v],
    () {
      for (int i = 0; i < v.value.length; i++) {
        v.grad[i] += out.grad * 1.0;
      }
    },
    opName: 'sum_vector', // <-- Renamed for clarity
    cost: N,
  );
  return out;
}