public static double MaxAbsoluteEigenvalue2(Matrix D, double accuracy, int N) { int m = D.Height; if (m!=D.Width) throw new Exception("Dimensions do not match"); ASampler Sampler = new ASampler(); Vector x = new Vector(m); Sampler.FillWithNormals(ref x); x /= Math.Sqrt(x*x); double eigenval = Math.Abs(x*D*x); int n = 0; while (true) { Vector y = new Vector(m); Sampler.FillWithNormals(ref y); y = y - x*(x*y); y /= Math.Sqrt(y*y); double xy = x*y; double yy = y*y; double xDx = x*D*x; double xDy = x*D*y; double yDx = y*D*x; double yDy = y*D*y; double M = Math.Sqrt(Sq(xDy + yDx) + Sq(xDx - yDy)); double sin_tau = (xDy+yDx)/M; double cos_tau = (xDx-yDy)/M; double sin_arc1 = 0; double cos_arc1 = 1; if (cos_tau>0) { cos_arc1 = Math.Sqrt(0.5*(1.0+cos_tau)); sin_arc1 = sin_tau/2/cos_arc1; } else { sin_arc1 = Math.Sqrt(0.5*(1.0-cos_tau)); cos_arc1 = sin_tau/2/sin_arc1; } double cos_arc2 = -sin_arc1; double sin_arc2 = cos_arc1; Vector x1 = x*cos_arc1 + y*sin_arc1; Vector x2 = x*cos_arc2 + y*sin_arc2; y =(Math.Abs(x1*D*x1)>Math.Abs(x2*D*x2)) ? x1 : x2; double dlug = Math.Sqrt(y*y); y /= dlug; double eig = Math.Abs(y*D*y); n = (eig-eigenval<accuracy) ? n+1 : 0; x = y; eigenval = eig; if (n>=N) { Vector t1 = D*x; return eigenval; } } }
public static double MaxAbsoluteEigenvalue(Matrix D, double accuracy, int N) { int m = D.Height; if (m!=D.Width) throw new Exception("Dimensions do not match"); ASampler Sampler = new ASampler(); Matrix DD = D*D; Vector x = new Vector(m); Sampler.FillWithNormals(ref x); x /= Math.Sqrt(x*x); double eigenval = Math.Abs(x*D*x); int n = 0; for (int i=0; true; i++) { Vector y = DD*x; x = y/Math.Sqrt(y*y); double eig = Math.Abs(x*D*x); n = (Math.Abs(eig-eigenval)<accuracy) ? n+1 : 0; eigenval = eig; if (i>=m*2 && n>=N) return (eigenval); } }