Beispiel #1
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));
            }
        }
Beispiel #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()}");
        }
        public static Double Det(Double[,] matrix)
        {
            var rows    = matrix.GetLength(0);
            var columns = matrix.GetLength(1);

            if (rows == columns)
            {
                switch (rows)
                {
                case 0: return(0.0);

                case 1: return(matrix[0, 0]);

                case 2: return(Helpers.Det2(matrix));

                case 3: return(Helpers.Det3(matrix));

                case 4: return(Helpers.Det4(matrix));
                }

                return(LUDecomposition.Determinant(matrix));
            }

            return(0.0);
        }
Beispiel #4
0
        public void Simple()
        {
            var matrix = new Matrix(3, 3);

            // [ 3,  1,  8 ]
            // [ 2, -5,  4 ]
            // [-1,  6, -2 ]
            // Determinant = 14

            matrix[0, 0] = 3;
            matrix[0, 1] = 1;
            matrix[0, 2] = 8;

            matrix[1, 0] = 2;
            matrix[1, 1] = -5;
            matrix[1, 2] = 4;

            matrix[2, 0] = -1;
            matrix[2, 1] = 6;
            matrix[2, 2] = -2;

            var decomposition = new LUDecomposition(matrix);

            Assert.AreEqual(14.0d, decomposition.Determinant(), 0.00000001d);
        }
Beispiel #5
0
 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));
     }
 }
Beispiel #6
0
        public void MatrixLUDecomposition2()
        {
            /*
             * MATLAB:
             * mc2x2 = [1 2;3 4]
             * [L_mc, U_mc, P_mc] = lu(mc2x2)
             * P_mcv = P_mc * [0:1:length(P_mc)-1]'
             * det(mc2x2)
             * [L_mch, U_mch, P_mch] = lu(mc2x2')
             * P_mchv = P_mch * [0:1:length(P_mch)-1]'
             * det(mc2x2')
             */

            Converter <int, double> int2Double = delegate(int i) { return(i); };

            Matrix mc2X2 = new Matrix(new double[][]
            {
                new double[] { 1, 2 },
                new double[] { 3, 4 }
            });

            LUDecomposition mcLU = mc2X2.LUDecomposition;
            Matrix          mcL  = new Matrix(new double[][] {
                new double[] { 1, 0 },
                new double[] { 1d / 3d, 1 }
            });

            Matrix mcU = new Matrix(new double[][] {
                new double[] { 3, 4 },
                new double[] { 0, 2d / 3d }
            });

            Matrix mcP = new Matrix(new double[][] {
                new double[] { 0, 1 },
                new double[] { 1, 0 }
            });

            Vector mcPv = new Vector(new double[] { 1, 0 });

            Assert.That(mcLU.L, NumericIs.AlmostEqualTo(mcL), "real LU L-matrix");
            Assert.That(mcLU.U, NumericIs.AlmostEqualTo(mcU), "real LU U-matrix");
            Assert.That(mcLU.PermutationMatrix, NumericIs.AlmostEqualTo(mcP), "real LU permutation matrix");
            Assert.That(mcLU.PivotVector, NumericIs.AlmostEqualTo(mcPv), "real LU pivot");
            Assert.That((Vector)Array.ConvertAll(mcLU.Pivot, int2Double), NumericIs.AlmostEqualTo(mcPv), "real LU pivot II");
            Assert.That(mcLU.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant");
            Assert.That(mc2X2.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant II");
            Assert.That(mcLU.IsNonSingular, "real LU non-singular");
            Assert.That(mcLU.L * mcLU.U, NumericIs.AlmostEqualTo(mcLU.PermutationMatrix * mc2X2), "real LU product");

            Matrix          mc2X2H = Matrix.Transpose(mc2X2);
            LUDecomposition mchLU  = mc2X2H.LUDecomposition;
            Matrix          mchL   = new Matrix(new double[][] {
                new double[] { 1, 0 },
                new double[] { 0.5, 1 }
            });

            Matrix mchU = new Matrix(new double[][] {
                new double[] { 2, 4 },
                new double[] { 0, 1 }
            });

            Matrix mchP = new Matrix(new double[][] {
                new double[] { 0, 1 },
                new double[] { 1, 0 }
            });

            Vector mchPv = new Vector(new double[] { 1, 0 });

            Assert.That(mchLU.L, NumericIs.AlmostEqualTo(mchL), "real LU L-matrix (H)");
            Assert.That(mchLU.U, NumericIs.AlmostEqualTo(mchU), "real LU U-matrix (H)");
            Assert.That(mchLU.PermutationMatrix, NumericIs.AlmostEqualTo(mchP), "real LU permutation matrix (H)");
            Assert.That(mchLU.PivotVector, NumericIs.AlmostEqualTo(mchPv), "real LU pivot (H)");
            Assert.That((Vector)Array.ConvertAll(mchLU.Pivot, int2Double), NumericIs.AlmostEqualTo(mchPv), "real LU pivot II (H)");
            Assert.That(mchLU.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant (H)");
            Assert.That(mc2X2H.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant II (H)");
            Assert.That(mchLU.IsNonSingular, "real LU non-singular (H)");
            Assert.That(mchLU.L * mchLU.U, NumericIs.AlmostEqualTo(mchLU.PermutationMatrix * mc2X2H), "real LU product (H)");
        }
Beispiel #7
0
        public void TestMatrix_LUDecomposition()
        {
            /*
             * MATLAB:
             * [L_mc, U_mc, P_mc] = lu(mc2x2)
             * P_mcv = P_mc * [0:1:length(P_mc)-1]'
             * det(mc2x2)
             * [L_mch, U_mch, P_mch] = lu(mc2x2')
             * P_mchv = P_mch * [0:1:length(P_mch)-1]'
             * det(mc2x2')
             */

            Converter <int, double> int2double = delegate(int i) { return(i); };

            LUDecomposition LU   = mc2x2.LUDecomposition;
            Matrix          L_mc = new Matrix(new double[][] {
                new double[] { 1, 0 },
                new double[] { 1d / 3d, 1 }
            });
            Matrix U_mc = new Matrix(new double[][] {
                new double[] { 3, 4 },
                new double[] { 0, 2d / 3d }
            });
            Matrix P_mc = new Matrix(new double[][] {
                new double[] { 0, 1 },
                new double[] { 1, 0 }
            });
            Vector P_mcv = new Vector(new double[] { 1, 0 });

            NumericAssert.AreAlmostEqual(L_mc, LU.L, "real LU L-matrix");
            NumericAssert.AreAlmostEqual(U_mc, LU.U, "real LU U-matrix");
            NumericAssert.AreAlmostEqual(P_mc, LU.PermutationMatrix, "real LU permutation matrix");
            NumericAssert.AreAlmostEqual(P_mcv, LU.PivotVector, "real LU pivot");
            NumericAssert.AreAlmostEqual(P_mcv, Array.ConvertAll(LU.Pivot, int2double), "real LU pivot II");
            NumericAssert.AreAlmostEqual(-2, LU.Determinant(), "real LU determinant");
            NumericAssert.AreAlmostEqual(-2, mc2x2.Determinant(), "real LU determinant II");
            Assert.IsTrue(LU.IsNonSingular, "real LU non-singular");
            NumericAssert.AreAlmostEqual(LU.PermutationMatrix * mc2x2, LU.L * LU.U, "real LU product");

            Matrix          mc2x2h = Matrix.Transpose(mc2x2);
            LUDecomposition LUH    = mc2x2h.LUDecomposition;
            Matrix          L_mch  = new Matrix(new double[][] {
                new double[] { 1, 0 },
                new double[] { 0.5, 1 }
            });
            Matrix U_mch = new Matrix(new double[][] {
                new double[] { 2, 4 },
                new double[] { 0, 1 }
            });
            Matrix P_mch = new Matrix(new double[][] {
                new double[] { 0, 1 },
                new double[] { 1, 0 }
            });
            Vector P_mchv = new Vector(new double[] { 1, 0 });

            NumericAssert.AreAlmostEqual(L_mch, LUH.L, "real LU L-matrix (H)");
            NumericAssert.AreAlmostEqual(U_mch, LUH.U, "real LU U-matrix (H)");
            NumericAssert.AreAlmostEqual(P_mch, LUH.PermutationMatrix, "real LU permutation matrix (H)");
            NumericAssert.AreAlmostEqual(P_mchv, LUH.PivotVector, "real LU pivot (H)");
            NumericAssert.AreAlmostEqual(P_mchv, Array.ConvertAll(LUH.Pivot, int2double), "real LU pivot II (H)");
            NumericAssert.AreAlmostEqual(-2, LUH.Determinant(), "real LU determinant (H)");
            NumericAssert.AreAlmostEqual(-2, mc2x2h.Determinant(), "real LU determinant II (H)");
            Assert.IsTrue(LUH.IsNonSingular, "real LU non-singular (H)");
            NumericAssert.AreAlmostEqual(LUH.PermutationMatrix * mc2x2h, LUH.L * LUH.U, "real LU product (H)");
        }