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 )); } }
/// <summary> /// Converts conic representation of an ellipse (given the coefficients vector) to the parametric representation. /// </summary> /// <param name="conic">The implicit equation coefficients</param> /// <returns>Parametric representation</returns> private static EllipseParams Conic2Parametric(double[] conic) { var A = new SymmetricMatrix(2); A[0, 0] = conic[0]; // a A[1, 1] = conic[2]; // c A[0, 1] = A[1, 0] = 0.5 * conic[1]; // half b var B = new ColumnVector(conic[3], conic[4]); var C = conic[5]; var eig = A.Eigensystem(); Debug.Assert(eig.Eigenvalue(0) * eig.Eigenvalue(1) > 0); var D = new double[] { eig.Eigenvalue(0), eig.Eigenvalue(1) }; var Q = eig.Eigentransformation().Transpose(); var t = -0.5 * A.Inverse() * B; var c_h = t.Transpose() * A * t + B.Transpose() * t + C; return(new EllipseParams { Center = new Point(t[0], t[1]), XRadius = Math.Sqrt(-c_h / D[0]), YRadius = Math.Sqrt(-c_h / D[1]), Degrees = 180 * Math.Atan2(Q[0, 1], Q[0, 0]) / Math.PI, }); }
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))); } }