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 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 VectorR GaussSeidel(MatrixR A, VectorR b, int MaxIterations, double tolerance) { int n = b.GetSize(); VectorR x = new VectorR(n); for (int nIteration = 0; nIteration < MaxIterations; nIteration++) { VectorR xOld = x.Clone(); for (int i = 0; i < n; i++) { double db = b[i]; double da = A[i, i]; if (Math.Abs(da) < epsilon) { throw new ArgumentException("Diagonal element is too small!"); } for (int j = 0; j < i; j++) { db -= A[i, j] * x[j]; } for (int j = i + 1; j < n; j++) { db -= A[i, j] * xOld[j]; } x[i] = db / da; } VectorR dx = x - xOld; if (dx.GetNorm() < tolerance) { //MessageBox.Show(nIteration.ToString()); return(x); } } return(x); }
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); }
public static MatrixR Tridiagonalize(MatrixR A) { int n = A.GetCols(); MatrixR A1 = new MatrixR(n, n); A1 = A.Clone(); double h, g, unorm; for (int i = 0; i < n - 2; i++) { VectorR u = new VectorR(n - i - 1); for (int j = i + 1; j < n; j++) { u[j - i - 1] = A[i, j]; } unorm = u.GetNorm(); if (u[0] < 0.0) { unorm = -unorm; } u[0] += unorm; for (int j = i + 1; j < n; j++) { A[j, i] = u[j - i - 1]; } h = VectorR.DotProduct(u, u) * 0.5; VectorR v = new VectorR(n - i - 1); MatrixR a1 = new MatrixR(n - i - 1, n - i - 1); for (int j = i + 1; j < n; j++) { for (int k = i + 1; k < n; k++) { a1[j - i - 1, k - i - 1] = A[j, k]; } } v = MatrixR.Transform(a1, u) / h; g = VectorR.DotProduct(u, v) / (2.0 * h); v -= g * u; for (int j = i + 1; j < n; j++) { for (int k = i + 1; k < n; k++) { A[j, k] = A[j, k] - v[j - i - 1] * u[k - i - 1] - u[j - i - 1] * v[k - i - 1]; } } A[i, i + 1] = -unorm; } Alpha = new double[n]; Beta = new double[n - 1]; Alpha[0] = A[0, 0]; for (int i = 1; i < n; i++) { Alpha[i] = A[i, i]; Beta[i - 1] = A[i - 1, i]; } MatrixR V = new MatrixR(n, n); V = V.Identity(); for (int i = 0; i < n - 2; i++) { VectorR u = new VectorR(n - i - 1); for (int j = i + 1; j < n; j++) { u[j - i - 1] = A.GetColVector(i)[j]; } h = VectorR.DotProduct(u, u) * 0.5; VectorR v = new VectorR(n - 1); MatrixR v1 = new MatrixR(n - 1, n - i - 1); for (int j = 1; j < n; j++) { for (int k = i + 1; k < n; k++) { v1[j - 1, k - i - 1] = V[j, k]; } } v = MatrixR.Transform(v1, u) / h; for (int j = 1; j < n; j++) { for (int k = i + 1; k < n; k++) { V[j, k] -= v[j - 1] * u[k - i - 1]; } } } return(V); }