addScalar function

Tensor<Vector> addScalar(
  1. Tensor<Vector> v,
  2. double s
)

Implementation

Tensor<Vector> addScalar(Tensor<Vector> v, double s) {
  int N = v.value.length;
  Vector outValue = [];
  for (int i = 0; i < N; i++) {
    outValue.add(v.value[i] + s);
  }
  Tensor<Vector> out = Tensor<Vector>(outValue);
  out.creator = Node(
    [v],
    () {
      for (int i = 0; i < N; i++) {
        v.grad[i] += out.grad[i];
      }
    },
    opName: 'addScalar_vector', // <-- Renamed for clarity
    extraParams: {'s': s}, // <-- CRITICAL: Storing the non-Tensor parameter
    cost: N,
  );
  return out;
}