Ejemplo n.º 1
0
        public void SquareRandomMatrixQRDecomposition()
        {
            for (int d = 1; d <= 100; d += 11)
            {
                Console.WriteLine("d={0}", d);

                SquareMatrix M = CreateSquareRandomMatrix(d);

                // QR decompose the matrix
                SquareQRDecomposition QRD = M.QRDecomposition();

                // the dimension should be right
                Assert.IsTrue(QRD.Dimension == M.Dimension);

                // test that the decomposition works
                SquareMatrix Q = QRD.QMatrix;
                SquareMatrix R = QRD.RMatrix;
                Assert.IsTrue(TestUtilities.IsNearlyEqual(Q * R, M));

                // check that the inverse works
                SquareMatrix MI = QRD.Inverse();
                SquareMatrix I  = TestUtilities.CreateSquareUnitMatrix(d);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, I));

                // test that a solution works
                ColumnVector t = new ColumnVector(d);
                for (int i = 0; i < d; i++)
                {
                    t[i] = i;
                }
                ColumnVector s = QRD.Solve(t);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M * s, t));
            }
        }
Ejemplo n.º 2
0
        public void SquareUnitMatrixEigensystem()
        {
            int                d = 3;
            SquareMatrix       I = TestUtilities.CreateSquareUnitMatrix(d);
            ComplexEigensystem E = I.Eigensystem();

            Assert.IsTrue(E.Dimension == d);
            for (int i = 0; i < d; i++)
            {
                Complex val = E.Eigenvalue(i);
                Assert.IsTrue(val == 1.0);
                Complex[] vec = E.Eigenvector(i);
                for (int j = 0; j < d; j++)
                {
                    if (i == j)
                    {
                        Assert.IsTrue(vec[j] == 1.0);
                    }
                    else
                    {
                        Assert.IsTrue(vec[j] == 0.0);
                    }
                }
            }
        }
        public void RandomRectangularSVD()
        {
            for (int c = 1; c < 64; c += 11)
            {
                Console.WriteLine(c);

                RectangularMatrix R = GenerateRandomMatrix(64, c);

                SingularValueDecomposition SVD = R.SingularValueDecomposition();

                Assert.IsTrue(SVD.RowCount == R.RowCount);
                Assert.IsTrue(SVD.ColumnCount == SVD.ColumnCount);
                Assert.IsTrue(SVD.Dimension == SVD.ColumnCount);

                SquareMatrix U = SVD.LeftTransformMatrix();
                Assert.IsTrue(U.Dimension == R.RowCount);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(U.Transpose() * U, TestUtilities.CreateSquareUnitMatrix(U.Dimension)));

                SquareMatrix V = SVD.RightTransformMatrix();
                Assert.IsTrue(V.Dimension == R.ColumnCount);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(V.Transpose() * V, TestUtilities.CreateSquareUnitMatrix(V.Dimension)));

                RectangularMatrix S = U.Transpose() * R * V;
                for (int i = 0; i < SVD.Dimension; i++)
                {
                    double w = SVD.SingularValue(i);
                    Console.WriteLine("  {0} {1}", w, S[i, i]);
                    Assert.IsTrue(w >= 0.0);
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(S[i, i], w));
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(R * SVD.RightSingularVector(i), w * SVD.LeftSingularVector(i)));
                }
            }
        }
Ejemplo n.º 4
0
 public void SquareRandomMatrixInverse()
 {
     for (int d = 1; d <= 100; d = d + 11)
     {
         SquareMatrix I  = TestUtilities.CreateSquareUnitMatrix(d);
         SquareMatrix M  = CreateSquareRandomMatrix(d, 1);
         SquareMatrix MI = M.Inverse();
         Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, I));
     }
 }
Ejemplo n.º 5
0
 public void SymmetricRandomMatrixInverse()
 {
     for (int d = 1; d <= 100; d = d + 11)
     {
         Console.WriteLine("d={0}", d);
         SquareMatrix    I  = TestUtilities.CreateSquareUnitMatrix(d);
         SymmetricMatrix M  = TestUtilities.CreateSymmetricRandomMatrix(d, 1);
         SymmetricMatrix MI = M.Inverse();
         Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, I));
     }
 }
Ejemplo n.º 6
0
 public void SquareUnitMatrixLUDecomposition()
 {
     for (int d = 1; d <= 10; d++)
     {
         SquareMatrix I = TestUtilities.CreateSquareUnitMatrix(d);
         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));
     }
 }
Ejemplo n.º 7
0
 public void SymmetricHilbertMatrixInverse()
 {
     for (int d = 1; d <= 4; d++)
     {
         Console.WriteLine("d={0}", d);
         SquareMatrix    I  = TestUtilities.CreateSquareUnitMatrix(d);
         SymmetricMatrix H  = TestUtilities.CreateSymmetricHilbertMatrix(d);
         SymmetricMatrix HI = H.Inverse();
         Assert.IsTrue(TestUtilities.IsNearlyEqual(H * HI, I));
     }
     // fails for d > 4! look into this
 }
Ejemplo n.º 8
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));
            }
        }
Ejemplo n.º 9
0
        public void SymmetricMatrixDecomposition()
        {
            for (int d = 1; d <= 4; d++)
            {
                SymmetricMatrix H = TestUtilities.CreateSymmetricHilbertMatrix(d);

                CholeskyDecomposition CD = H.CholeskyDecomposition();
                Assert.IsTrue(CD != null, String.Format("d={0} not positive definite", d));
                Assert.IsTrue(CD.Dimension == d);
                SymmetricMatrix HI = CD.Inverse();
                SquareMatrix    I  = TestUtilities.CreateSquareUnitMatrix(d);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(H * HI, I));
            }
        }
Ejemplo n.º 10
0
 public void SquareVandermondeMatrixInverse()
 {
     for (int d = 1; d <= 8; d++)
     {
         SquareMatrix I      = TestUtilities.CreateSquareUnitMatrix(d);
         SquareMatrix H      = CreateVandermondeMatrix(d);
         DateTime     start  = DateTime.Now;
         SquareMatrix HI     = H.Inverse();
         DateTime     finish = DateTime.Now;
         Console.WriteLine("d={0} t={1} ms", d, (finish - start).Milliseconds);
         PrintMatrix(HI);
         PrintMatrix(H * HI);
         Assert.IsTrue(TestUtilities.IsNearlyEqual(H * HI, I));
     }
 }
Ejemplo n.º 11
0
 public void SquareRandomMatrixEigenvalues()
 {
     for (int d = 1; d <= 67; d = d + 11)
     {
         SquareMatrix       I      = TestUtilities.CreateSquareUnitMatrix(d);
         SquareMatrix       M      = CreateSquareRandomMatrix(d, d);
         double             tr     = M.Trace();
         DateTime           start  = DateTime.Now;
         ComplexEigensystem E      = M.Eigensystem();
         DateTime           finish = DateTime.Now;
         Console.WriteLine("d={0} t={1} ms", d, (finish - start).Milliseconds);
         Assert.IsTrue(E.Dimension == d);
         Complex[] es = new Complex[d];
         for (int i = 0; i < d; i++)
         {
             es[i] = E.Eigenvalue(i);
             Complex[] v = E.Eigenvector(i);
             Assert.IsTrue(TestUtilities.IsNearlyEigenpair(M, v, es[i]));
         }
         Assert.IsTrue(TestUtilities.IsSumNearlyEqual(es, tr));
     }
 }
        public void RectangularQRDecomposition()
        {
            RectangularMatrix M = GenerateRandomMatrix(30, 10);

            QRDecomposition QRD = M.QRDecomposition();

            Assert.IsTrue(QRD.RowCount == M.RowCount);
            Assert.IsTrue(QRD.ColumnCount == M.ColumnCount);

            SquareMatrix Q = QRD.QMatrix;

            Assert.IsTrue(Q.Dimension == M.RowCount);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(Q * Q.Transpose(), TestUtilities.CreateSquareUnitMatrix(Q.Dimension)));

            RectangularMatrix R = QRD.RMatrix;

            Assert.IsTrue(R.RowCount == M.RowCount);
            Assert.IsTrue(R.ColumnCount == M.ColumnCount);

            RectangularMatrix QR = Q * R;

            Assert.IsTrue(TestUtilities.IsNearlyEqual(QR, M));
        }
Ejemplo n.º 13
0
        public void SquareRandomMatrixLUDecomposition()
        {
            for (int d = 1; d <= 256; d += 11)
            {
                Console.WriteLine("d={0}", d);

                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();
                SquareMatrix I  = TestUtilities.CreateSquareUnitMatrix(d);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, I));

                // 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));
            }
        }