operator - method

  1. @override
Matrix<double> operator -(
  1. Matrix<double> other
)
override

Returns the difference of two matrices.

An exception is thrown if the size of the source matrix does not match the size of the other matrix.

Implementation

@override
Matrix<double> operator -(Matrix<double> other) {
  if (rowCount != other.rowCount || columnCount != other.columnCount) {
    throw const MatrixException('The two matrices must have the same size!');
  }

  // Performing the difference
  final flatMatrix = List.generate(
    rowCount * columnCount,
    (_) => 0.0,
    growable: false,
  );

  for (var i = 0; i < rowCount; ++i) {
    for (var j = 0; j < columnCount; ++j) {
      _setDataAt(flatMatrix, i, j, this(i, j) - other(i, j));
    }
  }

  // Building the new matrix
  return RealMatrix.fromFlattenedData(
    rows: rowCount,
    columns: columnCount,
    data: flatMatrix,
  );
}