using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnitTestSharp; using Azimuth.DenseLinearAlgebra; namespace Azimuth.UnitTests.DenseLinearAlgebra { public class LUDecompositionTests : TestFixture { public class Ctor : TestFixture { public void DoesNotMakeACopy() { var matrix = new SquareMatrix(new Scalar[,] { {1, 2, }, {3, 10 }, }); var originalMatrix = matrix.Clone(); LUDecomposition decomp = new LUDecomposition(matrix); CheckNotEqual(originalMatrix, matrix); CheckEqual(decomp, matrix); } public void PreferredWayToMakeACopy() { var matrix = new SquareMatrix(new Scalar[,] { {1, 2, }, {3, 10 }, }); var originalMatrix = matrix.Clone(); LUDecomposition decomp = new LUDecomposition(matrix.Clone()); CheckEqual(originalMatrix, matrix); CheckNotEqual(decomp, matrix); } } public class StaticDeconstruct : TestFixture { public void Basic2x2() { var matrix = new SquareMatrix(new Scalar[,] { {1, 2, }, {3, 10 }, }); var expected = new SquareMatrix(new Scalar[,] { {1, 2, }, {3, 4 }, }); LUDecomposition decomp = new LUDecomposition(matrix); DenseVector permutation = new Scalar[] { 0, 1 }; CheckEqual(expected, decomp); CheckEqual(permutation[0], decomp.RowPermutations[0]); CheckEqual(permutation[1], decomp.RowPermutations[1]); } public void Basic2x2_Flipped() { var matrix = new SquareMatrix(new Scalar[,] { {3, 10 }, {1, 2, }, }); var expected = new SquareMatrix(new Scalar[,] { {1, 2, }, {3, 4 }, }); LUDecomposition decomp = new LUDecomposition(matrix); DenseVector permutation = new Scalar[] { 1, 1 }; CheckEqual(expected, decomp); CheckEqual(permutation[0], decomp.RowPermutations[0]); CheckEqual(permutation[1], decomp.RowPermutations[1]); } } public class StaticSolve : TestFixture { public void Works() { var LU = new SquareMatrix(new Scalar[,] { { 1, 2 }, { 3, 4 }, }); int[] rowPermutations = { 0, 1, }; DenseVector b = new Scalar[] { 1.0, 2.0 }; DenseVector x = new Scalar[] { 1.5, -.25 }; b = LUDecomposition.Solve(LU, rowPermutations, b); CheckEqual(x, b); } public void JumbledRows() { var LU = new SquareMatrix(new Scalar[,] { { 1, 2 }, { 3, 4 }, }); int[] rowPermutations = { 1, 1, }; DenseVector b = new Scalar[] { 2.0, 1.0 }; DenseVector x = new Scalar[] { 1.5, -.25}; b = LUDecomposition.Solve(LU, rowPermutations, b); CheckEqual(x, b); } } public class Solve : TestFixture { public void NoSolution() { var equations = new Scalar[,] { { 1, 2 }, { 2, 4 }, }; var b = new Scalar[] { 3, 7, }; var matrix = new SquareMatrix(equations); CheckThrow(typeof(Exception)); var decompose = new LUDecomposition(matrix); } public void ZeroRow() { var equations = new Scalar[,] { { 1, 2 }, { 0, 0 }, }; var b = new Scalar[] { 3, 7, }; var matrix = new SquareMatrix(equations); CheckThrow(typeof(Exception)); var decompose = new LUDecomposition(matrix); } public void Works_2x2() { var equations = new Scalar[,] { { 1, 2 }, { 4, 3 }, }; DenseVector b = new Scalar[] { 10, -5, }; DenseVector answer = new Scalar[] { -8.0, 9.0 }; var matrix = new SquareMatrix(equations); var decompose = new LUDecomposition(matrix); CheckEqual(answer, decompose.Solve(b)); } public void Works() { var equations = new Scalar[,] { { 3, 2, 1 }, { 1, 10, 3 }, { 2, -5, -2 }, }; var b = new Scalar[] { 10, -5, 2 }; DenseVector answer = new Scalar[] { 53.0/24.0, -13.0/3.0, 289.0/24.0 }; var matrix = new SquareMatrix(equations); var decompose = new LUDecomposition(matrix); CheckEqual(answer, decompose.Solve(b)); } public void UnfortunateOrdering() { // This test will fail without pivoting var equations = new Scalar[,] { { 0, 1 }, { 1, 0 }, }; var b = new Scalar[] { 3, 7, }; DenseVector expected = new Scalar[] { 7, 3, }; var matrix = new SquareMatrix(equations); var decompose = new LUDecomposition(matrix); var actual = decompose.Solve(b); CheckEqual(expected, actual); } } public class SizeEtAl : TestFixture { SquareMatrix matrix = new Scalar[,] { {1, 2, 3}, {1, 2, 4}, {2, 1, 0}, }; LUDecomposition lu = null; public override void FixtureSetup() { lu = new LUDecomposition(matrix); } public void Size() { CheckEqual(3, lu.Size); } public void RowCount() { CheckEqual(3, lu.RowCount); } public void ColumnCount() { CheckEqual(3, lu.ColumnCount); } } } }