예제 #1
0
 public void SquareVandermondeMatrixEigenvalues()
 {
     for (int d = 1; d <= 8; d++)
     {
         SquareMatrix       H   = CreateVandermondeMatrix(d);
         double             tr  = H.Trace();
         ComplexEigensystem E   = H.Eigensystem();
         double             sum = 0.0;
         for (int i = 0; i < d; i++)
         {
             Console.WriteLine(E.Eigenvalue(i));
             double e = E.Eigenvalue(i).Re;
             sum += e;
             Complex[]    vc = E.Eigenvector(i);
             ColumnVector v  = new ColumnVector(d);
             for (int j = 0; j < d; j++)
             {
                 //Console.WriteLine("  {0}", vc[j]);
                 v[j] = vc[j].Re;
             }
             ColumnVector Hv = H * v;
             ColumnVector ev = e * v;
             Assert.IsTrue(TestUtilities.IsNearlyEigenpair(H, v, e));
         }
         Assert.IsTrue(TestUtilities.IsNearlyEqual(tr, sum));
     }
 }
예제 #2
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));
     }
 }
예제 #3
0
        public void Bug8021()
        {
            // User presented a matrix with a large number (138) of zero eigenvalues.
            // QR algorithm got tripped up on high degeneracy, but eigenvalues could
            // be revealed before QR by simple index permutation. Added code to isolate
            // these "cheap" eigenvalues before starting QR algorithm.

            int          n = 276;
            SquareMatrix A = new SquareMatrix(n);

            using (System.IO.StreamReader reader = System.IO.File.OpenText(@"Bug8021.csv")) {
                int r = 0;
                while (!reader.EndOfStream)
                {
                    string   line  = reader.ReadLine();
                    string[] cells = line.Split(',');
                    for (int c = 0; c < cells.Length; c++)
                    {
                        string cell  = cells[c];
                        double value = Double.Parse(cell);
                        A[r, c] = value;
                    }
                    r++;
                }
            }

            ComplexEigendecomposition S = A.Eigendecomposition();

            foreach (ComplexEigenpair pair in S.Eigenpairs)
            {
                TestUtilities.IsNearlyEigenpair(A, pair.Eigenvector, pair.Eigenvalue);
            }

            Complex[] eigenvalues = A.Eigenvalues();
            double    trace       = A.Trace();
            Complex   sum         = 0.0;

            for (int i = 0; i < eigenvalues.Length; i++)
            {
                sum += eigenvalues[i];
            }
            TestUtilities.IsNearlyEqual(trace, sum);
        }
예제 #4
0
 public void SquareRandomMatrixEigenvalues()
 {
     for (int d = 1; d <= 67; d = d + 11)
     {
         SquareMatrix M              = CreateSquareRandomMatrix(d, d);
         double       tr             = M.Trace();
         DateTime     start          = DateTime.Now;
         ComplexEigendecomposition E = M.Eigendecomposition();
         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.Eigenpairs[i].Eigenvalue;
             ComplexColumnVector v = E.Eigenpairs[i].Eigenvector;
             Assert.IsTrue(TestUtilities.IsNearlyEigenpair(M, v, es[i]));
         }
         Assert.IsTrue(TestUtilities.IsSumNearlyEqual(es, tr));
     }
 }
예제 #5
0
 public void SquareVandermondeMatrixEigenvalues()
 {
     for (int d = 1; d <= 8; d++)
     {
         SquareMatrix H              = CreateVandermondeMatrix(d);
         double       tr             = H.Trace();
         ComplexEigendecomposition E = H.Eigendecomposition();
         double sum = 0.0;
         foreach (ComplexEigenpair pair in E.Eigenpairs)
         {
             double e = pair.Eigenvalue.Re;
             sum += e;
             ComplexColumnVector vc = pair.Eigenvector;
             ColumnVector        v  = new ColumnVector(d);
             for (int j = 0; j < d; j++)
             {
                 v[j] = vc[j].Re;
             }
             Assert.IsTrue(TestUtilities.IsNearlyEigenpair(H, v, e));
         }
         Assert.IsTrue(TestUtilities.IsNearlyEqual(tr, sum));
     }
 }