operator - method

NDArray operator -(
  1. dynamic other
)

Element-wise subtraction.

Supports:

  • Scalar subtraction: subtracts a number from all elements
  • Array subtraction: element-wise subtraction with broadcasting

Example:

var arr = NDArray([[10, 20], [30, 40]]);

// Scalar subtraction
var result1 = arr - 5;
print(result1.toNestedList()); // [[5, 15], [25, 35]]

// Array subtraction
var arr2 = NDArray([[1, 2], [3, 4]]);
var result2 = arr - arr2;
print(result2.toNestedList()); // [[9, 18], [27, 36]]

Implementation

NDArray operator -(dynamic other) {
  if (other is num) {
    return map((x) => x - other);
  } else if (other is NDArray) {
    return _elementWise(other, (a, b) => a - b);
  }
  throw ArgumentError('Cannot subtract ${other.runtimeType} from NDArray');
}