public void SquareMatrixDifficultEigensystem() { // this requires > 30 iterations to converge, looses more accuracy than it should // this example throws a NonConvergenceException without the ad hoc shift but works once the ad hoc shift is included SquareMatrix M = new SquareMatrix(4); M[0, 1] = 2; M[0, 3] = -1; M[1, 0] = 1; M[2, 1] = 1; M[3, 2] = 1; // The eigenvalues of this matrix are -1, -1, 1, 1. // There are only two distinct eigenvectors: (1, -1, 1, -1) with eigenvalue -1, and (1, 1, 1, 1) with eigenvalue 1. ComplexEigensystem E = M.Eigensystem(); for (int i = 0; i < E.Dimension; i++) { Console.WriteLine(E.Eigenvalue(i)); for (int j = 0; j < E.Dimension; j++) { Console.Write("{0} ", E.Eigenvector(i)[j]); } Console.WriteLine(); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(M, E.Eigenvector(i), E.Eigenvalue(i))); } }
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 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)); } }
public void Bug7208() { // this matrix has two eigenpairs with the same eigenvalue but distinct eigenvectors // we would report the same eigenvector for both, making the matrix of eigenvectors // non-invertible int n = 4; SquareMatrix matrix = new SquareMatrix(n + 1); double d = (3 + 2 * Math.Cos(2 * Math.PI / n)); double w = (64 * n) / (40 - d * d) - n; double w1 = w / (w + n); double w2 = 1 / (w + n); matrix[0, 0] = w1; for (int i = 1; i < n; i++) { matrix[0, i + 1] = w2; matrix[i + 1, 0] = 3.0 / 8; matrix[i + 1, i + 1] = 3.0 / 8; matrix[i, i + 1] = 1.0 / 8; matrix[i + 1, i] = 1.0 / 8; } matrix[0, 1] = w2; matrix[1, 0] = 3.0 / 8; matrix[1, 1] = 3.0 / 8; matrix[1, n] = 1.0 / 8; matrix[n, 1] = 1.0 / 8; ComplexEigensystem ces = matrix.Eigensystem(); Console.WriteLine("output"); SquareMatrix V = new SquareMatrix(n + 1); for (int i = 0; i < n + 1; i++) { Console.WriteLine("#{0}: {1}", i, ces.Eigenvalue(i)); for (int j = 0; j < n + 1; j++) { V[i, j] = ces.Eigenvector(i)[j].Re; Console.WriteLine(V[i, j]); } } SquareMatrix inv = V.Inverse(); }
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++; } } ComplexEigensystem S = A.Eigensystem(); for (int i = 0; i < S.Dimension; i++) { TestUtilities.IsNearlyEigenpair(A, S.Eigenvector(i), S.Eigenvalue(i)); } 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); }
public void DegenerateEigenvalues() { double[,] souce = new double[, ] { { 1, 0, 1 }, { 0, 2, 0 }, { 1, 0, 1 } }; SquareMatrix A = new SquareMatrix(3); for (int r = 0; r < souce.GetLength(0); r++) { for (int c = 0; c < souce.GetLength(1); c++) { A[r, c] = souce[r, c]; } } ComplexEigensystem e = A.Eigensystem(); }
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 CompanionMatrixEigenvalues() { SquareMatrix CM = new SquareMatrix(3); CM[0, 2] = -1.0; CM[1, 0] = 1.0; CM[1, 2] = -3.0; CM[2, 1] = 1.0; CM[2, 2] = -3.0; ComplexEigensystem EM = CM.Eigensystem(); for (int i = 0; i < EM.Dimension; i++) { Console.WriteLine("! {0}", EM.Eigenvalue(i)); } for (int d = 2; d <= 8; d++) { Console.WriteLine(d); SquareMatrix C = new SquareMatrix(d); for (int r = 1; r < d; r++) { C[r, r - 1] = 1.0; } for (int r = 0; r < d; r++) { C[r, d - 1] = -AdvancedIntegerMath.BinomialCoefficient(d, r); } ComplexEigensystem e = C.Eigensystem(); for (int i = 0; i < e.Dimension; i++) { Console.WriteLine("!! {0}", e.Eigenvalue(i)); } } }
public void SquareMatrixStochasticEigensystem() { // this is a simplifed form of a Markov matrix that arose in the Monopoly problem // and failed to converge int n = 12; // originally failed for n=12 due to lack of 2x2 detection at top // now tested for n up to 40, but n=14 appears to still be a problem, // probably due to a six-times degenerate eigenvalue SquareMatrix R = new SquareMatrix(n); for (int c = 0; c < n; c++) { R[(c + 2) % n, c] = 1.0 / 36.0; R[(c + 3) % n, c] = 2.0 / 36.0; R[(c + 4) % n, c] = 3.0 / 36.0; R[(c + 5) % n, c] = 4.0 / 36.0; R[(c + 6) % n, c] = 5.0 / 36.0; R[(c + 7) % n, c] = 6.0 / 36.0; R[(c + 8) % n, c] = 5.0 / 36.0; R[(c + 9) % n, c] = 4.0 / 36.0; R[(c + 10) % n, c] = 3.0 / 36.0; R[(c + 11) % n, c] = 2.0 / 36.0; R[(c + 12) % n, c] = 1.0 / 36.0; } //Complex[] v = R.Eigenvalues(); ComplexEigensystem E = R.Eigensystem(); for (int i = 0; i < E.Dimension; i++) { Console.WriteLine(E.Eigenvalue(i)); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(R, E.Eigenvector(i), E.Eigenvalue(i))); } }