public static GaussLegendreCoefficients GaussLegendre(int N) { var beta = from i in 1.To(N - 1) select 0.5 / Math.Sqrt(1.0 - (2.0 * i).Pow(-2)); SymmetricMatrix T = new SymmetricMatrix(N); // how to use TridiagonalMatrix? beta.ForEach((b, i) => { T[i + 1, i] = b; T[i, i + 1] = b; }); RealEigensystem e = T.Eigensystem(); var indices = from i in 0.To(N - 1) orderby e.Eigenvalue(i) select i; return(new GaussLegendreCoefficients { mu = indices.Select(i => e.Eigenvalue(i)).ToArray(), wt = indices.Select(i => 2 * e.Eigenvector(i)[0].Pow(2)).ToArray() }); }
public void SymmetricRandomMatrixEigenvectors() { for (int d = 1; d <= 100; d = d + 11) { Console.WriteLine("d={0}", d); SymmetricMatrix M = CreateSymmetricRandomMatrix(d, 1); double tr = M.Trace(); RealEigensystem E = M.Eigensystem(); Assert.IsTrue(E.Dimension == M.Dimension); SquareMatrix D = new SquareMatrix(E.Dimension); double[] es = new double[E.Dimension]; for (int i = 0; i < E.Dimension; i++) { double e = E.Eigenvalue(i); ColumnVector v = E.Eigenvector(i); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(M, v, e)); D[i, i] = e; es[i] = e; } Assert.IsTrue(TestUtilities.IsSumNearlyEqual(es, tr)); Assert.IsTrue(TestUtilities.IsNearlyEqual( D, E.TransformMatrix.Transpose() * M * E.TransformMatrix )); } }
public void RealEigenvalueOrdering() { int d = 10; Random rng = new Random(d + 1); SymmetricMatrix A = new SymmetricMatrix(d); A.Fill((int r, int c) => - 1.0 + 2.0 * rng.NextDouble()); RealEigensystem E = A.Eigensystem(); for (int i = 0; i < E.Dimension; i++) { Console.WriteLine(E.Eigenvalue(i)); } E.Sort(OrderBy.ValueAscending); for (int i = 1; i < E.Dimension; i++) { Assert.IsTrue(E.Eigenvalue(i - 1) <= E.Eigenvalue(i)); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(A, E.Eigenvector(i), E.Eigenvalue(i))); } E.Sort(OrderBy.ValueDescending); for (int i = 1; i < E.Dimension; i++) { Assert.IsTrue(E.Eigenvalue(i - 1) >= E.Eigenvalue(i)); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(A, E.Eigenvector(i), E.Eigenvalue(i))); } E.Sort(OrderBy.MagnitudeAscending); for (int i = 1; i < E.Dimension; i++) { Assert.IsTrue(Math.Abs(E.Eigenvalue(i - 1)) <= Math.Abs(E.Eigenvalue(i))); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(A, E.Eigenvector(i), E.Eigenvalue(i))); } E.Sort(OrderBy.MagnitudeDescending); for (int i = 1; i < E.Dimension; i++) { Assert.IsTrue(Math.Abs(E.Eigenvalue(i - 1)) >= Math.Abs(E.Eigenvalue(i))); Assert.IsTrue(TestUtilities.IsNearlyEigenpair(A, E.Eigenvector(i), E.Eigenvalue(i))); } }