operator * method

Matrix operator *(
  1. dynamic other
)

Multiply an Matrix with a Matrix or a scalar

Matrix a = Matrix.eye(2);
Matrix b = Matrix.fill(2,2, 10);

print( a * b );

Throws an MatrixUnsupportedOperation error if the requested multiplication is not valid. For example if matrices with the wrong dimensions are passed in.

Implementation

Matrix operator *(dynamic other) {
  double cutDot(Matrix other, int thisRow, int otherColumn) {
    double sum = 0.0;
    for (int i = 0; i < this.n; i++) {
      sum += this[thisRow][i] * other[i][otherColumn];
    }
    return sum;
  }

  if (other is Vector) {
    other = other.toMatrix();
  }

  if (other is Matrix) {
    if (this.n == other.m) {
      Matrix toReturn = Matrix.fill(this.m, other.n);
      for (int r = 0; r < toReturn.m; r++) {
        for (int c = 0; c < toReturn.n; c++) {
          toReturn[r][c] = cutDot(other, r, c);
        }
      }
      return toReturn;
    } else {
      throw MatrixInvalidDimensions();
    }
  } else if (other is num) {
    // scalar
    return this.map((x) => x * (other as num));
  } else
    throw MatrixUnsupportedOperation();
}