public void SquareMatrixNotInvertable() { SquareMatrix A = new SquareMatrix(2); A[1, 1] = 1.0; // Inverting should throw try { A.Inverse(); Assert.IsTrue(false); } catch (DivideByZeroException) { } // LU Decomposing should throw try { A.LUDecomposition(); Assert.IsTrue(false); } catch (DivideByZeroException) { } // SVD should succeed, and give infinite condition number SingularValueDecomposition SVD = A.SingularValueDecomposition(); Assert.IsTrue(Double.IsInfinity(SVD.ConditionNumber)); }
public void SquareRandomMatrixLUDecomposition() { for (int d = 1; d <= 256; d += 11) { SquareMatrix M = CreateSquareRandomMatrix(d); // LU decompose the matrix //Stopwatch sw = Stopwatch.StartNew(); LUDecomposition LU = M.LUDecomposition(); //sw.Stop(); //Console.WriteLine(sw.ElapsedMilliseconds); Assert.IsTrue(LU.Dimension == d); // test that the decomposition works SquareMatrix P = LU.PMatrix(); SquareMatrix L = LU.LMatrix(); SquareMatrix U = LU.UMatrix(); Assert.IsTrue(TestUtilities.IsNearlyEqual(P * M, L * U)); // check that the inverse works SquareMatrix MI = LU.Inverse(); Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, UnitMatrix.OfDimension(d))); // test that a solution works ColumnVector t = new ColumnVector(d); for (int i = 0; i < d; i++) { t[i] = i; } ColumnVector s = LU.Solve(t); Assert.IsTrue(TestUtilities.IsNearlyEqual(M * s, t)); } }
public void SquareUnitMatrixLUDecomposition() { for (int d = 1; d <= 10; d++) { SquareMatrix I = UnitMatrix.OfDimension(d).ToSquareMatrix(); Assert.IsTrue(I.Trace() == d); LUDecomposition LU = I.LUDecomposition(); Assert.IsTrue(LU.Determinant() == 1.0); SquareMatrix II = LU.Inverse(); Assert.IsTrue(TestUtilities.IsNearlyEqual(II, I)); } }
public void SquareVandermondeMatrixLUDecomposition() { // fails now for d = 8 because determinant slightly off for (int d = 1; d <= 7; d++) { Console.WriteLine("d={0}", d); double[] x = new double[d]; for (int i = 0; i < d; i++) { x[i] = i; } double det = 1.0; for (int i = 0; i < d; i++) { for (int j = 0; j < i; j++) { det = det * (x[i] - x[j]); } } // LU decompose the matrix SquareMatrix V = CreateVandermondeMatrix(d); LUDecomposition LU = V.LUDecomposition(); // test that the decomposition works SquareMatrix P = LU.PMatrix(); SquareMatrix L = LU.LMatrix(); SquareMatrix U = LU.UMatrix(); Assert.IsTrue(TestUtilities.IsNearlyEqual(P * V, L * U)); // check that the determinant agrees with the analytic expression Console.WriteLine("det {0} {1}", LU.Determinant(), det); Assert.IsTrue(TestUtilities.IsNearlyEqual(LU.Determinant(), det)); // check that the inverse works SquareMatrix VI = LU.Inverse(); //PrintMatrix(VI); //PrintMatrix(V * VI); SquareMatrix I = TestUtilities.CreateSquareUnitMatrix(d); Assert.IsTrue(TestUtilities.IsNearlyEqual(V * VI, I)); // test that a solution works ColumnVector t = new ColumnVector(d); for (int i = 0; i < d; i++) { t[i] = 1.0; } ColumnVector s = LU.Solve(t); Assert.IsTrue(TestUtilities.IsNearlyEqual(V * s, t)); } }
public void SparseSquareMatrixSolutionAgreement() { Random rng = new Random(2); int n = 16; //for (int n = 8; n < 100; n+= 11) { // create dense and sparse matrices with the same entries SquareMatrix M = new SquareMatrix(n); SparseSquareMatrix S = new SparseSquareMatrix(n); for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { if (rng.NextDouble() < 0.5) { M[r, c] = rng.NextDouble(); S[r, c] = M[r, c]; } } } // pick a RHS ColumnVector b = new ColumnVector(n); for (int i = 0; i < n; i++) { b[i] = rng.NextDouble(); } // solve each ColumnVector Mx = M.LUDecomposition().Solve(b); ColumnVector Sx = S.Solve(b); // the solutions should be the same Assert.IsTrue(TestUtilities.IsNearlyEqual(Mx, Sx, TestUtilities.TargetPrecision * 100.0)); //} }