public static double CalculatePrecision(MyMatrix matrix, MyVector vector, Algorithm algo) { LinearEquation le = new LinearEquation(matrix, vector); MyVector x; switch (algo) { case Algorithm.PartialGauss: x = le.SolveEquationPartialPivotingAlgorithm(false); break; case Algorithm.OptimizedPartialGauss: x = le.SolveEquationPartialPivotingAlgorithm(true); break; case Algorithm.GaussSeidel: x = new MyVector(le.GaussSeidelIterationAlgorithm(0)); break; default: x = le.SolveEquationPartialPivotingAlgorithm(true); break; } MyVector b = matrix * x; return(MyVector.VectorNorm(vector - b)); }
public object Clone() { MyVector newVector = new MyVector(Length); for (int i = 0; i < Length; i++) { newVector.Values[i] = Values[i]; } return(newVector); }
public LinearEquation(MyMatrix matrix, MyVector vector) { if (matrix.Rows != vector.Length) { throw new System.Exception(String.Format("Matrix has {0} rows, but the vector has {1} values", matrix.Rows, vector.Length)); } Matrix = (MyMatrix)matrix.Clone(); Vector = (MyVector)vector.Clone(); }
public static double VectorNorm(MyVector vec) { double maxValue = (vec.Values[0] < 0) ? (vec.Values[0] * -1) : vec.Values[0]; for (int i = 1; i < vec.Length; i++) { double currentVal = (vec.Values[i] < 0) ? (vec.Values[i] * -1) : vec.Values[i]; if (currentVal > maxValue) { maxValue = currentVal; } } return(maxValue); }
public static MyVector operator -(MyVector vec1, MyVector vec2) { if (vec1.Length != vec2.Length) { throw new Exception("Vectors should have the same length"); } MyVector result = new MyVector(vec1.Length); for (int i = 0; i < vec1.Length; i++) { result.Values[i] = vec1.Values[i] as dynamic - vec2.Values[i]; } return(result); }
public MyVector CalculateResultVector() { MyVector vec = new MyVector(Vector.Length); for (int i = Vector.Length - 1; i >= 0; i--) { double sum = 0; for (int j = i + 1; j < Matrix.Columns; j++) { sum += (Matrix.Matrix[i, j]) * vec.Values[j] * (-1); } vec.Values[i] = (Vector.Values[i] + sum) / (Matrix.Matrix[i, i]); } return(vec); }
public static MyVector operator *(MyMatrix matrix, MyVector vector) { if (matrix.Columns != vector.Length) { throw new Exception("The number of columns of the 1st matrix must equal the number of rows of the 2nd matrix"); } MyVector newVector = new MyVector(matrix.Rows); double sum; for (int i = 0; i < matrix.Rows; i++) { sum = 0; for (int j = 0; j < matrix.Columns; j++) { sum += (matrix.Matrix[i, j]) * vector.Values[j]; } newVector.Values[i] = sum; } return(newVector); }
public LinearEquation(int agents) { if (agents < 2) { throw new System.ArgumentException("Too few agents"); } int size = 1; for (int i = 0; i < agents; i++) { size += i + 2; } Size = size; Agents = agents; double[,] matrix = new double[size, size]; double[] vector = new double[size]; int row = 0; double combinationProbability = 2.0 / (Agents * (Agents - 1)); for (int x = 0; x <= Agents; x++) { for (int y = 0; y <= Agents - x; y++) { matrix[row, row] = 1; if (!((x == 0 && y == 0) || (x == 0 && y == Agents) || (x == Agents && y == 0))) { Agent[] arr = GenerateArrayOfAgents(x, y); int index; for (int ind1 = 0; ind1 < Agents - 1; ind1++) { for (int ind2 = ind1 + 1; ind2 < Agents; ind2++) { if (arr[ind1] == Agent.Y && arr[ind2] == Agent.U) { index = TranslateCoordinates(new Point(x + 1, y)); matrix[row, index] -= combinationProbability; } else if (arr[ind1] == Agent.Y && arr[ind2] == Agent.N) { index = TranslateCoordinates(new Point(x - 1, y - 1)); matrix[row, index] -= combinationProbability; } else if (arr[ind1] == Agent.N && arr[ind2] == Agent.U) { index = TranslateCoordinates(new Point(x, y + 1)); matrix[row, index] -= combinationProbability; } else { index = TranslateCoordinates(new Point(x, y)); matrix[row, index] -= combinationProbability; } } } } else if (x == Agents && y == 0) { vector[row] = 1; } row++; } } Matrix = new MyMatrix(matrix); Vector = new MyVector(vector); }