div method Null safety

  1. @override
void div(
  1. Tensor<double> right,
  2. {bool noNan = false,
  3. bool swapArguments = false}
)
override

Calculates fraction of two tensors.

The formula for result element i is:

x[i] = x[i] / scalar;

If swapArguments is true, the formula is:

x[i] = scalar / x[i];

If denominator is 0.0, the result is double.nan when using floating-point numbers and 0 when using integers. If noNan is true, the result is 0 when using floating-point numbers.

Throws ArgumentError if tensor shapes are not equal.

Throws ArgumentError if tensor shapes are not equal.

Implementation

@override
void div(Tensor<double> right,
    {bool noNan = false, bool swapArguments = false}) {
  _checkSameShape(right);
  final elements = this.elements;
  final rightElements = right.elements;
  final n = length;
  final nan = noNan ? 0.0 : double.nan;
  if (swapArguments) {
    for (var i = 0; i < n; i++) {
      final nominator = rightElements[i];
      final denominator = elements[i];
      elements[i] = denominator == 0.0 ? nan : nominator / denominator;
    }
  } else {
    for (var i = 0; i < n; i++) {
      final nominator = elements[i];
      final denominator = rightElements[i];
      elements[i] = denominator == 0.0 ? nan : nominator / denominator;
    }
  }
}