public void RectangularMatrixEquality() { RectangularMatrix A = new RectangularMatrix(new double[, ] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } }); RectangularMatrix AC = A.Copy(); RectangularMatrix B = 2.0 * A; // Equality operator Assert.IsTrue(A == AC); Assert.IsTrue(AC == A); Assert.IsFalse(A == B); Assert.IsFalse(B == A); Assert.IsFalse(A == null); Assert.IsFalse(null == A); Assert.IsTrue((RectangularMatrix)null == (RectangularMatrix)null); // Inequality operator Assert.IsFalse(A != AC); Assert.IsFalse(AC != A); Assert.IsTrue(A != B); Assert.IsTrue(B != A); Assert.IsTrue(A != null); Assert.IsTrue(null != A); Assert.IsFalse((RectangularMatrix)null != (RectangularMatrix)null); // Equals method Assert.IsTrue(A.Equals(AC)); Assert.IsFalse(A.Equals(B)); Assert.IsFalse(A.Equals(new object())); Assert.IsFalse(A.Equals(null)); }
public void RectangularMatrixAccess() { // create a matrix via outer product ColumnVector cv = new ColumnVector(new double[] { 1, 2 }); RowVector rv = new RowVector(new double[] { 3, 4, 5 }); RectangularMatrix M = cv * rv; // check dimensions Assert.IsTrue(M.RowCount == cv.Dimension); Assert.IsTrue(M.ColumnCount == rv.Dimension); // check values for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { Assert.IsTrue(M[r, c] == cv[r] * rv[c]); } } // extract a column ColumnVector mc = M.Column(1); Assert.IsTrue(mc.Dimension == M.RowCount); for (int i = 0; i < mc.Dimension; i++) { Assert.IsTrue(mc[i] == M[i, 1]); } // extract a row RowVector mr = M.Row(1); Assert.IsTrue(mr.Dimension == M.ColumnCount); for (int i = 0; i < mr.Dimension; i++) { Assert.IsTrue(mr[i] == M[1, i]); } // test clone RectangularMatrix MC = M.Copy(); Assert.IsTrue(MC.RowCount == M.RowCount); Assert.IsTrue(MC.ColumnCount == M.ColumnCount); // test equality of clone Assert.IsTrue(MC == M); Assert.IsFalse(MC != M); // test independence of clone MC[0, 0] += 1.0; Assert.IsFalse(MC == M); Assert.IsTrue(MC != M); }
public RectangularMatrix ScaliarMultiplication(RectangularMatrix source, RectangularMatrix multyplicator, Func <double, double> func) { if (source.RowCount != multyplicator.RowCount || source.ColumnCount != multyplicator.ColumnCount) { throw new InvalidOperationException("Cant mutiplicate"); } var sourceClone = source.Copy(); for (int i = 0; i < sourceClone.RowCount; i++) { for (int j = 0; j < sourceClone.ColumnCount; j++) { var multy = func == null ? multyplicator[i, j] : func(multyplicator[i, j]); sourceClone[i, j] = sourceClone[i, j] * multy; } } return(sourceClone); }