Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Gauss
- {
- class Program
- {
- static List<List<double>> GetMatrix(List<double> b)
- {
- StreamReader fin = new StreamReader("input.txt");
- int n = int.Parse(fin.ReadLine());
- List<List<double>> matrix = new List<List<double>>();
- for (int i = 0; i < n; i++)
- {
- List<double> tmp = new List<double>();
- string[] line = fin.ReadLine().Split(' ');
- for(int j = 0; j < n; j++)
- tmp.Add(double.Parse(line[j]));
- matrix.Add(tmp);
- }
- string[] vector = fin.ReadLine().Split(' ');
- for (int j = 0; j < n; j++)
- b.Add(double.Parse(vector[j]));
- fin.Close();
- return matrix;
- }
- static void IterationMethod()
- {
- List<double> vector = new List<double>();
- List<List<double>> matrix = GetMatrix(vector);
- List<double>bVector = new List<double>();
- List<List<double>> x = new List<List<double>>();
- List<double> steepVector = new List<double>();
- List<List<double>> multiplyMatrix = new List<List<double>>();
- double e = 0.000001;
- multiplyMatrix = matrix;
- for (int i = 0; i < matrix.Count; i++)
- {
- bVector.Add(vector[i] / matrix[i][i]);
- for (int j = 0; j < matrix.Count; j++)
- if (i == j) multiplyMatrix[i][j] = 0;
- else multiplyMatrix[i][j] = -matrix[i][j] / matrix[i][i];
- }
- for (int i = 0; i < vector.Count; i++)
- steepVector.Add(vector[i]);
- int count = 0;
- x.Add(steepVector);
- PrintVector(x[x.Count - 1], ++count);
- steepVector = multiplyMatrixWithVector(matrix, x[x.Count - 1]);
- SumVectors(steepVector, vector);
- x.Add(steepVector);
- PrintVector(x[x.Count - 1], ++count);
- while (vectorsNorm(x[x.Count - 2], x[x.Count - 1]) > e)
- {
- steepVector = multiplyMatrixWithVector(matrix, x[x.Count - 1]);
- SumVectors(steepVector, vector);
- x.Add(steepVector);
- PrintVector(x[x.Count - 1], ++count);
- }
- Console.Write("Finally result: ");
- for(int i = 0; i < x[x.Count - 1].Count; i++)
- Console.Write("{0} ", x[x.Count - 1][i]);
- Console.WriteLine();
- }
- static void PrintVector(List<double> vector, int count)
- {
- Console.Write("Step #{0}: ", count);
- for (int i = 0; i < vector.Count; i++)
- Console.Write("{0} ", vector[i]);
- Console.WriteLine();
- }
- static List<double> multiplyMatrixWithVector(List<List<double>> matrix, List<double> vector)
- {
- List<double> resultVector = new List<double>();
- for (int i = 0; i < matrix.Count; i++)
- {
- resultVector.Add(0);
- for (int j = 0; j < vector.Count; j++)
- resultVector[i] += matrix[i][j] * vector[j];
- }
- return resultVector;
- }
- static void SumVectors(List<double> firstVector, List<double> secondVector)
- {
- for (int i = 0; i < firstVector.Count; i++)
- firstVector[i] += secondVector[i];
- }
- public static double vectorsNorm(List<double> firstVector, List<double> secondVector)
- {
- double maxNorm = -1;
- for (int i = 0; i < firstVector.Count; i++)
- if (Math.Abs(secondVector[i] - firstVector[i]) > maxNorm)
- maxNorm = Math.Abs(secondVector[i] - firstVector[i]);
- return maxNorm;
- }
- static void Main(string[] args)
- {
- IterationMethod();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement