public static void RayleighQuotient(MatrixR A, double tolerance, int flag, out VectorR x, out double lambda) { int n = A.GetCols(); double delta = 0.0; Random random = new Random(); x = new VectorR(n); if (flag != 2) { for (int i = 0; i < n; i++) { x[i] = random.NextDouble(); } x.Normalize(); lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x)); } else { lambda = 0.0; Rayleigh(A, 1e-2, out x, out lambda); } double temp = lambda; MatrixR identity = new MatrixR(n, n); LinearSystem ls = new LinearSystem(); do { temp = lambda; double d = ls.LUCrout(A - lambda * identity.Identity(), x); x.Normalize(); lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x)); delta = Math.Abs((temp - lambda) / lambda); }while (delta > tolerance); }
public static void Rayleigh(MatrixR A, double tolerance, out VectorR x, out double lambda) { int n = A.GetCols(); double delta = 0.0; Random random = new Random(); x = new VectorR(n); for (int i = 0; i < n; i++) { x[i] = random.NextDouble(); } x.Normalize(); VectorR x0 = MatrixR.Transform(A, x); x0.Normalize(); lambda = VectorR.DotProduct(x, x0); double temp = lambda; do { temp = lambda; x0 = x; x0.Normalize(); x = MatrixR.Transform(A, x0); lambda = VectorR.DotProduct(x, x0); delta = Math.Abs((temp - lambda) / lambda); }while (delta > tolerance); x.Normalize(); }
public static void Inverse(MatrixR A, double s, double tolerance, out VectorR x, out double lambda) { int n = A.GetCols(); x = new VectorR(n); lambda = 0.0; double delta = 0.0; MatrixR identity = new MatrixR(n, n); A = A - s * (identity.Identity()); LinearSystem ls = new LinearSystem(); A = ls.LUInverse(A); Random random = new Random(); for (int i = 0; i < n; i++) { x[i] = random.NextDouble(); } do { VectorR temp = x; x = MatrixR.Transform(A, x); x.Normalize(); if (VectorR.DotProduct(temp, x) < 0) { x = -x; } VectorR dx = temp - x; delta = dx.GetNorm(); }while (delta > tolerance); lambda = s + 1.0 / (VectorR.DotProduct(x, MatrixR.Transform(A, x))); }
public static void Power(MatrixR A, double tolerance, out VectorR x, out double lambda) { int n = A.GetCols(); x = new VectorR(n); lambda = 0.0; double delta = 0.0; Random random = new Random(); for (int i = 0; i < n; i++) { x[i] = random.NextDouble(); } do { VectorR temp = x; x = MatrixR.Transform(A, x); x.Normalize(); if (VectorR.DotProduct(temp, x) < 0) { x = -x; } VectorR dx = temp - x; delta = dx.GetNorm(); }while (delta > tolerance); lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x)); }
public VectorR GetUnitVector() { VectorR result = new VectorR(vector); result.Normalize(); return(result); }
public static VectorR TridiagonalEigenvector(double s, double tolerance, out double lambda) { int n = Alpha.GetLength(0); double[] gamma = (double[])Beta.Clone(); double[] beta = (double[])Beta.Clone(); double[] alpha = new double[n]; for (int i = 0; i < n; i++) { alpha[i] = Alpha[i] - s; } double[] gamma1, alpha1, beta1; LUDecomposition(gamma, alpha, beta, out gamma1, out alpha1, out beta1); VectorR x = new VectorR(n); Random random = new Random(); for (int i = 0; i < n; i++) { x[i] = random.NextDouble(); } x.Normalize(); VectorR x1 = new VectorR(n);; double sign; do { x1 = x.Clone(); LUSolver(gamma1, alpha1, beta1, x); x.Normalize(); if (VectorR.DotProduct(x1, x) < 0.0) { sign = -1.0; x = -x; } else { sign = 1.0; } }while ((x - x1).GetNorm() > tolerance); lambda = s + sign / x.GetNorm(); return(x); }