/// <inheritdoc /> IMathematicalMatrix IMathematicalMatrix.Multiply(IMathematicalMatrix matrix) { #region Validation Guard.ArgumentNotNull(matrix, "matrix"); // Check the dimensions to make sure the operation is a valid one. if (noOfColumns != matrix.Rows) { throw new ArgumentException(IncompatibleMatricesTimes, "matrix"); } #endregion var ret = new Matrix(noOfRows, matrix.Columns); for (var i = 0; i < noOfRows; i++) { for (var j = 0; j < matrix.Columns; j++) { double sum = 0; for (var k = 0; k < noOfColumns; k++) { sum += (GetValue(i, k) * matrix[k, j]); } ret.SetValue(i, j, sum); } } return(ret); }
public void ConcatenateInterface() { IMathematicalMatrix matrix1 = MatrixTest.GetTestMatrix(); IMathematicalMatrix matrix2 = GetTestMinusMatrix(); var concat = matrix1.Concatenate(matrix2); Assert.AreEqual(concat.Rows, matrix1.Rows); Assert.AreEqual(concat.Columns, matrix1.Columns + matrix2.Columns); for (var i = 0; i < matrix1.Rows; i++) { for (var j = 0; j < matrix1.Columns; j++) { Assert.AreEqual(concat[i, j], matrix1[i, j]); } } for (var i = 0; i < matrix2.Rows; i++) { for (var j = 0; j < matrix2.Columns; j++) { Assert.AreEqual(concat[i, j + matrix1.Columns], matrix2[i, j]); } } }
public bool Equals(IMathematicalMatrix other, double precision) { if (other == null) { return(false); } if (other.Rows != Rows) { return(false); } if (other.Columns != Columns) { return(false); } for (var i = 0; i < Rows; i++) { for (var j = 0; j < Columns; j++) { if (!GetValue(i, j).IsSimilarTo(other[i, j], precision)) { return(false); } } } return(true); }
/// <inheritdoc /> IMathematicalMatrix IMathematicalMatrix.Subtract(IMathematicalMatrix matrix) { #region Validation Guard.ArgumentNotNull(matrix, "matrix"); if ((noOfRows != matrix.Rows) || (noOfColumns != matrix.Columns)) { throw new ArgumentException(IncompatibleMatrices, "matrix"); } #endregion var ret = new Matrix(noOfRows, noOfColumns); for (var i = 0; i < noOfRows; i++) { for (var j = 0; j < noOfColumns; j++) { ret.SetValue(i, j, GetValue(i, j) - matrix[i, j]); } } return(ret); }
public void MinorArbitraryInterface() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); var rows = matrix.Rows; var columns = matrix.Columns; var minor = matrix.Minor(2, 3); Assert.AreEqual(minor.Columns, columns - 1); Assert.AreEqual(minor.Rows, rows - 1); for (var i = 0; i < minor.Rows; i++) { for (var j = 0; j < minor.Columns; j++) { var addAmount = 0; if (i >= 2) { addAmount++; } if (j >= 3) { addAmount++; } Assert.AreEqual(minor[i, j], i + j + addAmount); } } Assert.IsTrue(MatrixTest.IsOriginalTestMatrix(matrix)); }
public bool Equals(IMathematicalMatrix other) { if (other == null) { return(false); } if (other.Rows != Rows) { return(false); } if (other.Columns != Columns) { return(false); } for (var i = 0; i < Rows; i++) { for (var j = 0; j < Columns; j++) { if (GetValue(i, j) != other[i, j]) { return(false); } } } return(true); }
/// <summary> /// Adds to matrices according to the linear algebra operator +. /// </summary> /// <param name="matrix"> The result of the addition. </param> /// <returns> The result of the plus operation. </returns> IMathematicalMatrix IMathematicalMatrix.Plus(IMathematicalMatrix matrix) { if (matrix.GetType() != typeof(Matrix)) { throw new ArgumentException(Resources.InvalidOperationWrongMatrixType); } return(Plus((Matrix)matrix)); }
public void ExceptionIncompatibleMatrices() { IMathematicalMatrix matrix1 = new Matrix(2, 3); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[0, 2] = -4; matrix1[1, 0] = 0; matrix1[1, 1] = 3; matrix1[1, 2] = -1; IMathematicalMatrix matrix2 = MatrixTest.GetTestMatrix(); matrix1.Add(matrix2); }
public static bool IsOriginalTestMatrix(IMathematicalMatrix matrix) { for (var i = 0; i < matrix.Rows; i++) { for (var j = 0; j < matrix.Columns; j++) { if (matrix[i, j] != i + j) { return(false); } } } return(true); }
public static bool IsOriginalTestMatrix(IMathematicalMatrix matrix) { for (var i = 0; i < matrix.Rows; i++) { for (var j = 0; j < matrix.Columns; j++) { if (matrix[i, j] != i + j) { return false; } } } return true; }
public void ExceptionIncompatibleMatrices2() { IMathematicalMatrix matrix1 = new Matrix(2, 3); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[0, 2] = -4; matrix1[1, 0] = 0; matrix1[1, 1] = 3; matrix1[1, 2] = -1; IMathematicalMatrix matrix2 = MatrixTest.GetTestMatrix(); Assert.Throws <ArgumentException>(() => matrix1.Subtract(matrix2)); }
public void ColumnArbitraryInterface() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); matrix.MultiplyColumn(3, 3); for (var i = 0; i < matrix.Rows; i++) { for (var j = 0; j < matrix.Columns; j++) { if (j == 3) { Assert.AreEqual(matrix[i, j], 3 * (i + j)); } else { Assert.AreEqual(matrix[i, j], i + j); } } } }
public void MinorBottomRightInterface() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); var rows = matrix.Rows; var columns = matrix.Columns; var minor = matrix.Minor(matrix.Rows - 1, matrix.Columns - 1); Assert.AreEqual(minor.Columns, columns - 1); Assert.AreEqual(minor.Rows, rows - 1); for (var i = 0; i < minor.Rows; i++) { for (var j = 0; j < minor.Columns; j++) { Assert.AreEqual(minor[i, j], i + j); } } Assert.IsTrue(MatrixTest.IsOriginalTestMatrix(matrix)); }
public void ExceptionInterfaceTimesNull() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); matrix.Multiply(null); }
public void ExceptionPlusNull() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); matrix.Add(null); }
public void ExceptionInterfaceTimesNull() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); Assert.Throws <ArgumentNullException>(() => matrix.Multiply(null)); }
public void ExceptionInterfaceMinusNull() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); matrix.Subtract(null); }
public void ExceptionInterfaceMinusNull() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); Assert.Throws <ArgumentNullException>(() => matrix.Subtract(null)); }
public void ExceptionPlusNull() { IMathematicalMatrix matrix = MatrixTest.GetTestMatrix(); Assert.Throws <ArgumentNullException>(() => matrix.Add(null)); }
/// <inheritdoc /> IMathematicalMatrix IMathematicalMatrix.Add(IMathematicalMatrix matrix) { return(AddInternal(this, matrix)); }
/// <inheritdoc /> IMathematicalMatrix IMathematicalMatrix.Concatenate(IMathematicalMatrix rightMatrix) { return(ConcatenateInternal(this, rightMatrix)); }