public void MatrixMultiplyTest() { Matrix[] mulExpected = new Matrix[] { MatrixFactory.CreateSquare(), MatrixFactory.CreateSquare(0), MatrixFactory.CreateSquare(0, 0, 0, 0), MatrixFactory.CreateSquare(12, 20, 26, 17, 29, 38, 3, 7, 12), MatrixFactory.CreateSquare(206, 562, 728, 102, 34, 98, 112, 40, 106, 423, 437, 127, 50, 167, 191, 42) }; for (Int32 i = 0; i < _matrix.Length; i++) { Matrix mulMatrix = _matrix[i] * _matrix[i]; for (Int32 j = 0; j < mulMatrix.NumberOfRows; j++) { for (Int32 k = 0; k < mulMatrix.NumberOfColumns; k++) { Assert.AreEqual(mulExpected[i][j, k], mulMatrix[j, k]); } } } }
public void SetUp() { _matrix = new Matrix[] { MatrixFactory.CreateSquare(18, 22, 54, 42, 22, 70, 86, 62, 54, 86, 174, 134, 42, 62, 134, 106) }; _lExpected = new Matrix[] { MatrixFactory.CreateSquare(4.24264, 0.00000, 0.00000, 0.00000, 5.18545, 6.56591, 0.00000, 0.00000, 12.72792, 3.04604, 1.64974, 0.00000, 9.89949, 1.62455, 1.84971, 1.39262) }; _ltExpected = new Matrix[] { MatrixFactory.CreateSquare(4.24264, 5.18545, 12.72792, 9.89949, 0.00000, 6.56591, 3.04604, 1.62455, 0.00000, 0.00000, 1.64974, 1.84971, 0.00000, 0.00000, 0.00000, 1.39262) }; }
public void QRDecompositionComputeTest() { Matrix matrix = MatrixFactory.CreateSquare(12, -51, 4, 6, 167, -68, -4, 24, -41); Matrix qExpected = MatrixFactory.CreateSquare(6.0 / 7, 69.0 / 175, -58.0 / 175, 3.0 / 7, -158.0 / 175, 6.0 / 175, -2.0 / 7, -6.0 / 35, -33.0 / 35); Matrix rExpected = MatrixFactory.CreateSquare(14, 21, -14, 0, -175, 70, 0, 0, 35); QRDecomposition decomposition = new QRDecomposition(matrix); decomposition.Compute(); for (Int32 j = 0; j < matrix.NumberOfRows; j++) { for (Int32 k = 0; k < matrix.NumberOfColumns; k++) { Assert.AreEqual(qExpected[j, k], decomposition.Q[j, k], 0.001); Assert.AreEqual(rExpected[j, k], decomposition.R[j, k], 0.001); } } }
public void Setup() { this.matrices = new Matrix[] { MatrixFactory.CreateSquare(1, 3, 5, 2, 4, 7, 1, 1, 0), MatrixFactory.CreateSquare(11, 9, 24, 2, 1, 5, 2, 6, 3, 17, 18, 1, 2, 5, 7, 1) }; this.expectedL = new Matrix[] { MatrixFactory.CreateSquare(1, 0, 0, 0.5, 1, 0, 0.5, -1, 1), MatrixFactory.CreateSquare(1, 0, 0, 0, 0.272727272727273, 1, 0, 0, 0.0909090909090909, 0.28750, 1, 0, 0.181818181818182, 0.23125, 0.00359712230215807, 1) }; this.expectedU = new Matrix[] { MatrixFactory.CreateSquare(2, 4, 7, 0, 1, 1.5, 0, 0, -2), MatrixFactory.CreateSquare(11, 9, 24, 2, 0, 14.54545, 11.45455, 0.45455, 0, 0, -3.47500, 5.68750, 0, 0, 0, 0.51079) }; this.expectedP = new Matrix[] { MatrixFactory.CreateSquare(0, 1, 0, 1, 0, 0, 0, 0, 1), MatrixFactory.CreateSquare(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1) }; this.expectedDeterminant = new Double[] { 4, 284 }; this.expectedInverse = new Matrix[] { MatrixFactory.CreateSquare(-1.75, 1.25, 0.25, 1.75, -1.25, 0.75, -0.5, 0.5, -0.5), MatrixFactory.CreateSquare(0.72, 0.46, 1.02, -5.23, 0.29, 0.24, 0.60, -2.58, -0.38, -0.30, -0.65, 3.20, -0.23, 0.00, -0.45, 1.96) }; }
public void SetUp() { _matrix = new Matrix[] { MatrixFactory.CreateSquare(), MatrixFactory.CreateSquare(0), MatrixFactory.CreateSquare(0, 0, 0, 0), MatrixFactory.CreateSquare(1, 3, 5, 2, 4, 7, 1, 1, 0), MatrixFactory.CreateSquare(11, 9, 24, 2, 1, 5, 2, 6, 3, 17, 18, 1, 2, 5, 7, 1) }; }
public void HouseholderTridiagonalizeTest() { Matrix matrix = MatrixFactory.CreateSquare(4, 1, -2, 2, 1, 2, 0, 1, -2, 0, 3, -2, 2, 1, -2, -1); Matrix expected = MatrixFactory.CreateSquare(4, -3, 0, 0, -3, 10.0 / 3, -5.0 / 3, 0, 0, -5.0 / 3, -33.0 / 25, 68.0 / 75, 0, 0, 68.0 / 75, 149.0 / 75); Matrix tridiag = HouseholderTransformation.Tridiagonalize(matrix); for (Int32 j = 0; j < matrix.NumberOfRows; j++) { for (Int32 k = 0; k < matrix.NumberOfColumns; k++) { Assert.AreEqual(expected[j, k], tridiag[j, k], 0.01); } } }
public void MatrixMultiplicationTest() { // multiply with scalar Double scalar = Math.PI; for (Int32 i = 0; i < this.matrices.Length; i++) { Matrix product = scalar * this.matrices[i]; for (Int32 rowIndex = 0; rowIndex < product.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < product.NumberOfColumns; columnIndex++) { product[rowIndex, columnIndex].ShouldBe(scalar * this.matrices[i][rowIndex, columnIndex]); } } product = this.matrices[i] * scalar; for (Int32 rowIndex = 0; rowIndex < product.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < product.NumberOfColumns; columnIndex++) { product[rowIndex, columnIndex].ShouldBe(scalar * this.matrices[i][rowIndex, columnIndex]); } } } // multiply with matrix Matrix[] expectedMultiple = new Matrix[] { MatrixFactory.CreateSquare(), MatrixFactory.CreateSquare(0), MatrixFactory.CreateSquare(0.25, 0, 0, 0.25), MatrixFactory.CreateSquare(12, 20, 26, 17, 29, 38, 3, 7, 12), MatrixFactory.CreateSquare(206, 562, 728, 102, 34, 98, 112, 40, 106, 423, 437, 127, 50, 167, 191, 42) }; for (Int32 i = 0; i < 4; i++) { Matrix mulMatrix = this.matrices[i] * this.matrices[i]; for (Int32 rowIndex = 0; rowIndex < mulMatrix.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < mulMatrix.NumberOfColumns; columnIndex++) { mulMatrix[rowIndex, columnIndex].ShouldBe(expectedMultiple[i][rowIndex, columnIndex]); } } } Matrix matrix = null; Should.Throw <ArgumentNullException>(() => matrix = 1 * matrix); Should.Throw <ArgumentNullException>(() => matrix = matrix * 1); Should.Throw <ArgumentNullException>(() => matrix = this.matrices[3] * matrix); Should.Throw <ArgumentNullException>(() => matrix = matrix * this.matrices[3]); Should.Throw <ArgumentException>(() => matrix = this.matrices[3] * this.matrices[6]); }
public void HouseholderTransformationTridiagonalizeTest() { // first matrix Matrix matrix = new Matrix(new[, ] { { 4.0, 1, -2, 2 }, { 1, 2, 0, 1 }, { -2, 0, 3, -2 }, { 2, 1, -2, -1 } }); Matrix expected = new Matrix(new[, ] { { 4, -3, 0, 0 }, { -3, 10.0 / 3, -5.0 / 3, 0 }, { 0, -5.0 / 3, -33.0 / 25, 68.0 / 75 }, { 0, 0, 68.0 / 75, 149.0 / 75 } }); Matrix result = HouseholderTransformation.Tridiagonalize(matrix); for (Int32 rowIndex = 0; rowIndex < matrix.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < matrix.NumberOfColumns; columnIndex++) { result[rowIndex, columnIndex].ShouldBe(expected[rowIndex, columnIndex], 0.01); } } // second matrix matrix = new Matrix(new[, ] { { 4.0, 2, 2, 1 }, { 2, -3, 1, 1 }, { 2, 1, 3, 1 }, { 1, 1, 1, 2 } }); expected = new Matrix(new[, ] { { 4, -3, 0, 0 }, { -3, 2, 3.1623, 0 }, { 0, 3.1623, -1.4, -0.2 }, { 0, 0, -0.2, 1.4 } }); result = HouseholderTransformation.Tridiagonalize(matrix); for (Int32 rowIndex = 0; rowIndex < matrix.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < matrix.NumberOfColumns; columnIndex++) { result[rowIndex, columnIndex].ShouldBe(expected[rowIndex, columnIndex], 0.01); } } // exceptions Should.Throw <ArgumentNullException>(() => HouseholderTransformation.Tridiagonalize(null)); Should.Throw <ArgumentException>(() => HouseholderTransformation.Tridiagonalize(new Matrix(2, 3))); Should.Throw <ArgumentException>(() => HouseholderTransformation.Tridiagonalize(MatrixFactory.CreateSquare(1, 2, 3, 4))); }