public static MatrixR Minor(MatrixR m, int row, int col) { MatrixR mm = new MatrixR(m.GetRows() - 1, m.GetCols() - 1); int ii = 0, jj = 0; for (int i = 0; i < m.GetRows(); i++) { if (i == row) { continue; } jj = 0; for (int j = 0; j < m.GetCols(); j++) { if (j == col) { continue; } mm[ii, jj] = m[i, j]; jj++; } ii++; } return(mm); }
public static VectorR Transform(MatrixR m, VectorR v) { //VectorR result = new VectorR(v.GetSize()); VectorR result = new VectorR(m.GetRows()); /*if (!m.IsSquared()) * { * throw new ArgumentOutOfRangeException( * "Dimension", m.GetRows(), "The matrix must be squared!"); * }*/ if (m.GetCols() != v.GetSize()) { throw new ArgumentOutOfRangeException( "Size", v.GetSize(), "The size of the vector must be equal" + "to the number of rows of the matrix!"); } for (int i = 0; i < m.GetRows(); i++) { result[i] = 0.0; for (int j = 0; j < m.GetCols(); j++) { result[i] += m[i, j] * v[j]; } } return(result); }
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)); }
private static void Transformation(MatrixR A, MatrixR R, int I, int J, out MatrixR A1, out MatrixR R1) { int n = A.GetCols(); double t = 0.0; double da = A[J, J] - A[I, I]; if (Math.Abs(A[I, J]) < Math.Abs(da) * 1e-30) { t = A[I, J] / da; } else { double phi = da / (2.0 * A[I, J]); t = 1.0 / (Math.Abs(phi) + Math.Sqrt(1.0 + phi * phi)); if (phi < 0.0) { t = -t; } } double c = 1.0 / Math.Sqrt(Math.Abs(t * t + 1.0)); double s = t * c; double tau = s / (1.0 + c); double temp = A[I, J]; A[I, J] = 0.0; A[I, I] -= t * temp; A[J, J] += t * temp; for (int i = 0; i < I; i++) { temp = A[i, I]; A[i, I] = temp - s * (A[i, J] + tau * temp); A[i, J] += s * (temp - tau * A[i, J]); } for (int i = I + 1; i < J; i++) { temp = A[I, i]; A[I, i] = temp - s * (A[i, J] + tau * A[I, i]); A[i, J] += s * (temp - tau * A[i, J]); } for (int i = J + 1; i < n; i++) { temp = A[I, i]; A[I, i] = temp - s * (A[J, i] + tau * temp); A[J, i] += s * (temp - tau * A[J, i]); } for (int i = 0; i < n; i++) { temp = R[i, I]; R[i, I] = temp - s * (R[i, J] + tau * R[i, I]); R[i, J] += s * (temp - tau * R[i, J]); } A1 = A; R1 = R; }
public static MatrixR Adjoint(MatrixR m) { if (!m.IsSquared()) { throw new ArgumentOutOfRangeException( "Dimension", m.GetRows(), "The matrix must be squared!"); } MatrixR ma = new MatrixR(m.GetRows(), m.GetCols()); for (int i = 0; i < m.GetRows(); i++) { for (int j = 0; j < m.GetCols(); j++) { ma[i, j] = Math.Pow(-1, i + j) * (Determinant(Minor(m, i, j))); } } return(ma.GetTranspose()); }
public static bool CompareDimension(MatrixR m1, MatrixR m2) { if (m1.GetRows() == m2.GetRows() && m1.GetCols() == m2.GetCols()) { return(true); } else { return(false); } }
// Eigenvalues and eigenvectors of a tridiagonal matrix: public static void SetAlphaBeta(MatrixR T) { int n = T.GetCols(); Alpha = new double[n]; Beta = new double[n - 1]; Alpha[0] = T[0, 0]; for (int i = 1; i < n; i++) { Alpha[i] = T[i, i]; Beta[i - 1] = T[i - 1, i]; } }
public static void Jacobi(MatrixR A, double tolerance, out MatrixR x, out VectorR lambda) { MatrixR AA = A.Clone(); int n = A.GetCols(); int maxTransform = 5 * n * n; MatrixR matrix = new MatrixR(n, n); MatrixR R = matrix.Identity(); MatrixR R1 = R; MatrixR A1 = A; lambda = new VectorR(n); x = R; double maxTerm = 0.0; int I, J; do { maxTerm = MaxTerm(A, out I, out J); Transformation(A, R, I, J, out A1, out R1); A = A1; R = R1; }while (maxTerm > tolerance); x = R; for (int i = 0; i < n; i++) { lambda[i] = A[i, i]; } for (int i = 0; i < n - 1; i++) { int index = i; double d = lambda[i]; for (int j = i + 1; j < n; j++) { if (lambda[j] > d) { index = j; d = lambda[j]; } } if (index != i) { lambda = lambda.GetSwap(i, index); x = x.GetColSwap(i, index); } } }
private static double MaxTerm(MatrixR A, out int I, out int J) { int n = A.GetCols(); double result = 0.0; I = 0; J = 1; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (Math.Abs(A[i, j]) > result) { result = Math.Abs(A[i, j]); I = i; J = j; } } } return(result); }
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); }
public MatrixR(MatrixR m) { Rows = m.GetRows(); Cols = m.GetCols(); matrix = m.matrix; }