Exemple #1
0
        public void TridiagonalMatrixFibinacciDeterminant()
        {
            // The n X n tri-diagonal matrix with 1s on the diagonal,
            // 1s on the super-diagonal, and -1s on the sub-diagonal
            // has determinant equal to the (n+1)th Fibonacci number.

            foreach (int n in TestUtilities.GenerateIntegerValues(2, 128, 4))
            {
                TridiagonalMatrix T = new TridiagonalMatrix(n);
                for (int i = 0; i < n; i++)
                {
                    T[i, i] = 1.0;
                }
                for (int i = 1; i < n; i++)
                {
                    T[i - 1, i] = 1.0;
                    T[i, i - 1] = -1.0;
                }

                Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                  T.Determinant(),
                                  AdvancedIntegerMath.FibonacciNumber(n + 1)
                                  ));
            }
        }
        public void TridiagonalMatrixLUDecompositionTest()
        {
            for (int d = 3; d < 100; d = 2 * d)
            {
                Console.WriteLine("d={0}", d);

                TridiagonalMatrix T = CreateRandomTridiagonalMatrix(d);
                Assert.IsTrue(T.Dimension == d);


                TridiagonalLUDecomposition LU = T.LUDecomposition();
                Assert.IsTrue(LU.Dimension == d);

                // check determinant
                Assert.IsTrue(TestUtilities.IsNearlyEqual(LU.Determinant(), T.Determinant()));

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

                //SquareMatrixTest.PrintMatrix(T);
                //SquareMatrixTest.PrintMatrix(L);
                //SquareMatrixTest.PrintMatrix(U);
                //SquareMatrixTest.PrintMatrix(L * U);


                // check solution to decomposition
                ColumnVector b = new ColumnVector(d);
                for (int i = 0; i < d; i++)
                {
                    b[i] = i;
                }
                ColumnVector x = LU.Solve(b);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(T * x, b));

                // test inverse
                SquareMatrix TI = LU.Inverse();
                SquareMatrix I  = new SquareMatrix(d);
                for (int i = 0; i < d; i++)
                {
                    I[i, i] = 1.0;
                }
                Assert.IsTrue(TestUtilities.IsNearlyEqual(TI * T, I));
            }
        }