operator * method

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

Returns the product of two matrices.

An exception is thrown if the column count of the source matrix does not match the row count of the other matrix.

Implementation

@override
Matrix<double> operator *(Matrix<double> other) {
  if (columnCount != other.rowCount) {
    throw const MatrixException(
      'Matrices shapes mismatch! The column count '
      'of the source matrix must match the row count of the other.',
    );
  }

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

  // Performing the multiplication
  for (var i = 0; i < rowCount; i++) {
    for (var j = 0; j < other.columnCount; j++) {
      var sum = 0.0;
      for (var k = 0; k < other.rowCount; k++) {
        sum += this(i, k) * other(k, j);
      }
      flatMatrix[other.columnCount * i + j] = sum;
    }
  }

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