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));
            }
        }
Exemple #2
0
        public static void LUDecomposition()
        {
            SquareMatrix A = new SquareMatrix(new double[, ] {
                { 1, -2, 3 },
                { 2, -5, 12 },
                { 0, 2, -10 }
            });

            ColumnVector b = new ColumnVector(2, 8, -4);

            LUDecomposition lud = A.LUDecomposition();
            ColumnVector    x   = lud.Solve(b);

            PrintMatrix("x", x);
            PrintMatrix("Ax", A * x);

            SquareMatrix L = lud.LMatrix();
            SquareMatrix U = lud.UMatrix();
            SquareMatrix P = lud.PMatrix();

            PrintMatrix("LU", L * U);
            PrintMatrix("PA", P * A);

            SquareMatrix AI = lud.Inverse();

            PrintMatrix("A * AI", A * AI);

            Console.WriteLine($"det(a) = {lud.Determinant()}");
        }
Exemple #3
0
        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));
            }
        }