public static Matrix MultiplyBy(this Matrix matrix1, Matrix matrix2) { if (matrix1[0].Count != matrix2.Count) { throw new ArgumentOutOfRangeException("matrix1", "Non-conformable matrices cannot be multiplied."); } double[][] result = MatrixFactory.CreateEmpty(matrix1.Count, matrix2[0].Count); for (int i = 0; i < matrix1.Count; ++i) // each row of 1 { for (int j = 0; j < matrix2[0].Count; ++j) // each column of 2 { for (int k = 0; k < matrix1[0].Count; ++k) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } return(result); }
public static Matrix MultiplyBy(this Matrix matrix1, Matrix matrix2) { if (matrix1[0].Count != matrix2.Count) { throw new ArgumentOutOfRangeException(nameof(matrix1), "Non-conformable matrices cannot be multiplied."); } var result = MatrixFactory.CreateEmpty(matrix1.Count, matrix2[0].Count); for (var i = 0; i < matrix1.Count; ++i) // each row of 1 { for (var j = 0; j < matrix2[0].Count; ++j) // each column of 2 { for (var k = 0; k < matrix1[0].Count; ++k) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } // ReSharper disable once CoVariantArrayConversion return(result); }