public static Vector operator +(Vector a, Vector b) { int n = a.GetLength(); Vector c = new Vector(n); for (int i = 0; i < n; ++i) c[i] = a[i] + b[i]; return c; }
public static Vector operator *(Matrix a, Vector b) { int n = a.GetLength(0); Vector c = new Vector(n); for (int i = 0; i < n; ++i) { double sum = 0; for (int j = 0; j < n; ++j) sum += a[i, j] * b[j]; c[i] = sum; } return c; }
public Vector(Vector a) { this.a = new double[a.GetLength()]; for (int i = 0; i < a.GetLength(); ++i) this.a[i] = a[i]; }
static void Main(string[] args) { Console.Clear(); System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\sema7\Desktop\Eigenvalues2\input.txt"); string line; line = file.ReadLine(); int n = int.Parse(line); Matrix A = new Matrix(n, n); Matrix B = new Matrix(n, n); for (int i = 0; i < n; i++) { string enterString = file.ReadLine(); string[] massiveString = enterString.Split(new Char[] { ' ' }); for (int j = 0; j < massiveString.Length; j++) { A[i, j] = double.Parse(massiveString[j]); B[i, j] = A[i, j]; } } Console.WriteLine("Matrix A of size {0}:", n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write("{0:f5} ", A[i, j]); Console.WriteLine(); } Console.WriteLine(); file.Close(); double eps = Math.Pow(10, -6); Matrix X1 = new Matrix(n, n, 1); Matrix.Eigenvalue(ref B, ref X1, eps, n); Console.WriteLine("Eigenvalue matrix A: "); for (int i = 0; i < n; i++) Console.Write("{0:f15} ", B[i, i]); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Matrix X of size {0}:", n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write("{0:f15} ", X1[i, j]); Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("____________ Power Method ___________________"); Vector X2 = new Vector(n); int k = 0; double l1 = Matrix.PowMethod(A, ref X2, false, ref k); Console.WriteLine("Number step: {0}", k); Console.WriteLine("Eigenvalue l1 of Matrix A: {0:f15}", l1); Console.WriteLine(); Console.WriteLine("Vector X1 for eigenvalue l1"); for (int i = 0; i < n; i++) Console.WriteLine("X[{0}] = {1:f15}", i + 1, X2[i]); double R = 0; Vector P = new Vector(n); P = A * X2; for (int i=0; i< n; ++i) R += Math.Abs(P[i] - l1 * X2[i]); Console.WriteLine("R = {0:f15}", R); Console.WriteLine("||X1|| = {0:f15}", X2.Norma2()); Console.WriteLine(); Console.WriteLine("____________ Scalyar Method ___________________"); Vector X3 = new Vector(n); k = 0; double l2 = Matrix.ScalyarMethod(A, ref X3, ref k); Console.WriteLine("Number step: {0}", k); Console.WriteLine("Eigenvalue l1 of Matrix A: {0:f15}", l2); Console.WriteLine(); Console.WriteLine("Vector X1 for eigenvalue l1"); for (int i = 0; i < n; i++) Console.WriteLine("X[{0}] = {1:f15}", i + 1, X3[i]); R = 0; P = new Vector(n); P = A * X3; for (int i = 0; i < n; ++i) R += Math.Abs(P[i] - l2 * X3[i]); Console.WriteLine("R = {0:f15}", R); Console.WriteLine("||X1|| = {0:f15}", X3.Norma2()); Console.WriteLine(); Vector X4 = new Vector(n); k = 0; double l3 = Matrix.OppositeEndSpectrum(A, ref X4, l1, ref k); Console.WriteLine("Number step: {0}", k); Console.WriteLine("The opposite end of the spectrum matrix A: {0:f15}", l3); Console.WriteLine("Vector X:"); for (int i = 0; i < n; i++) Console.WriteLine("X[{0}] = {1:f15}", i + 1, X4[i]); Console.WriteLine(); Vector X5 = new Vector(n); k = 0; double l4 = Matrix.MethodVilanda(A, ref X5, l1, ref k); Console.WriteLine("Number step: {0}", k); Console.WriteLine("The eigenvalue of method Vilanda matrix A: {0:f15}", l4); Console.WriteLine("Vector X:"); for (int i = 0; i < n; i++) Console.WriteLine("X[{0}] = {1:f15}", i + 1, X5[i]); R = 0; P = new Vector(n); P = A * X5; for (int i = 0; i < n; ++i) R += Math.Abs(P[i] - l4 * X5[i]); Console.WriteLine("R = {0:f15}", R); Console.ReadLine(); }