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))); }
public void HouseholderTransformationComputeTest() { // first vector HouseholderTransformation transformation = new HouseholderTransformation(new Vector(new Double[] { 1, 1, 1, 1 })); transformation.Compute(); Matrix resultH = transformation.H; Matrix expectedH = new Matrix(new[, ] { { 0.5, 0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5, -0.5 }, { 0.5, -0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5, 0.5 } }); resultH.NumberOfColumns.ShouldBe(expectedH.NumberOfColumns); resultH.NumberOfRows.ShouldBe(expectedH.NumberOfRows); for (Int32 rowIndex = 0; rowIndex < resultH.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < resultH.NumberOfColumns; columnIndex++) { resultH[rowIndex, columnIndex].ShouldBe(expectedH[rowIndex, columnIndex], 0.01); } } // second vector transformation = new HouseholderTransformation(new Double[] { 1, -1 }); transformation.Compute(); resultH = transformation.H; expectedH = new Matrix(new[, ] { { 1 / Math.Sqrt(2), -1 / Math.Sqrt(2) }, { -1 / Math.Sqrt(2), -1 / Math.Sqrt(2) } }); resultH.NumberOfColumns.ShouldBe(expectedH.NumberOfColumns); resultH.NumberOfRows.ShouldBe(expectedH.NumberOfRows); for (Int32 rowIndex = 0; rowIndex < resultH.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < resultH.NumberOfColumns; columnIndex++) { resultH[rowIndex, columnIndex].ShouldBe(expectedH[rowIndex, columnIndex], 0.01); } } }
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); } } }