protected override IFormResult <Vector <double> > InnerHandle(string[] form) { var equations = form.Select(Equation.Parse); var equationSystem = new EquationsSystem(equations); var result = solver.Solve(equationSystem); return(FormResult <Vector <double> > .SuccessResult(result)); }
public void GetResultsVectorReturnsFreeTermsVectorForSystem() { var equations = new List <Equation> { new Equation("3x+2y-5z=2"), new Equation("5x+3y-8z=7"), new Equation("6x+8y-3z=1") }; var equationsSystem = new EquationsSystem(equations); var expectedVector = Vector.Build.Dense(new double[] { 2, 7, 1 }); var resultsVector = equationsSystem.GetFreeTermsVector(); Assert.Equal(expectedVector, resultsVector); }
public void SolveByCramersMethodReturnsCorrectResult() { var equations = new List<Equation> { new Equation("2x+y=3"), new Equation("y=4") }; var equationsSystem = new EquationsSystem(equations); var solver = new Solver(); var expectedResult = Vector.Build.Dense(new[] { -0.5, 4 }); var result = solver.Solve(equationsSystem); Assert.Equal(expectedResult.ToString(), result.ToString()); }
public void GetResultsVectorReturnsFreeTermsVectorForSystem() { var equations = new List<Equation> { new Equation("3x+2y-5z=2"), new Equation("5x+3y-8z=7"), new Equation("6x+8y-3z=1") }; var equationsSystem = new EquationsSystem(equations); var expectedVector = Vector.Build.Dense(new double[] {2, 7, 1}); var resultsVector = equationsSystem.GetFreeTermsVector(); Assert.Equal(expectedVector,resultsVector); }
public void SolveByCramersMethodReturnsCorrectResult() { var equations = new List <Equation> { new Equation("2x+y=3"), new Equation("y=4") }; var equationsSystem = new EquationsSystem(equations); var solver = new Solver(); var expectedResult = Vector.Build.Dense(new[] { -0.5, 4 }); var result = solver.Solve(equationsSystem); Assert.Equal(expectedResult.ToString(), result.ToString()); }
public void Solver_Run_VariablesCountMismatch_ExceptionThrown() { var kernel = new StandardKernel(new ConfigModule()); EquationsSystem eqSystem = new EquationsSystem() { EquationFunctions = new List <Func <double[], double[], double> >() { new Func <double[], double[], double>((x, alpha) => alpha[0] * x[0] + alpha[1] * x[1] + alpha[2]), new Func <double[], double[], double>((x, alpha) => alpha[0] * x[0] + alpha[1] * x[1] + alpha[2]) }, EquationsCoefficients = new List <double[]>() { new double[] { 2.0, 3.0, -5.0 }, new double[] { 1.0, 4.0, -5.0 } }, VariableNames = new string[] { "x", "y" } }; ISolverSettings solverSettings = kernel.Get <ISolverSettings>(); solverSettings.InitialValues = new double[] { 0.5 }; solverSettings.MaxIterations = 100; solverSettings.Precision = 1e-5; try { ISolveMethod solutionMethod = kernel.Get <ISolveMethod> (new ConstructorArgument("system", eqSystem), new ConstructorArgument("settings", solverSettings)); EquationsSystemSolver solver = new EquationsSystemSolver(solutionMethod); solver.Run(); } catch (VariablesCountMismatchException) { } catch (Exception) { Assert.Fail(); } }
public void SolveSimplifiesSystemAndReturnsCorrectResult() { var equations = new List<Equation> { new Equation("x+2y+1y-2z-1=4"), new Equation("x+2x+5y+6z+2=9"), new Equation("2x+4y+3z=8") }; var equationsSystem = new EquationsSystem(equations); var solver = new Solver(); var expectedResult = Vector.Build.Dense(new double[] { -15, 8, 2 }); var result = solver.Solve(equationsSystem); Assert.Equal(expectedResult.ToString(), result.ToString()); }
public void SolveReturnsCorrectResult() { var equations = new List <Equation> { new Equation("x+3y-2z=5"), new Equation("3x+5y+6z=7"), new Equation("2x+4y+3z=8") }; var equationsSystem = new EquationsSystem(equations); var solver = new Solver(); var expectedResult = Vector.Build.Dense(new double[] { -15, 8, 2 }); var result = solver.Solve(equationsSystem); Assert.Equal(expectedResult.ToString(), result.ToString()); }
public void GetMatrixReturnsDenseMatrixOfExpectedSizeIfCoefficientsMissing() { var equations = new List <Equation> { new Equation("2x+y=3"), new Equation("y=4") }; var equationsSystem = new EquationsSystem(equations); var expectedMatrix = DenseMatrix.OfArray(new double[, ] { { 2, 1 }, { 0, 1 } }); var equationMatrix = equationsSystem.GetMatrix(); Assert.Equal(expectedMatrix, equationMatrix); }
public void GetMatrixReturnsDenseMatrixOfExpectedSizeIfCoefficientsMissing() { var equations = new List<Equation> { new Equation("2x+y=3"), new Equation("y=4") }; var equationsSystem = new EquationsSystem(equations); var expectedMatrix = DenseMatrix.OfArray(new double[,] { {2,1}, {0,1} }); var equationMatrix = equationsSystem.GetMatrix(); Assert.Equal(expectedMatrix, equationMatrix); }
private void CalculateEquation() { const int lines = 3; const int columns = 4; var matrix = new List <List <double> >(); for (var i = 0; i < lines; i++) { matrix.Add(new List <double>()); for (var n = 0; n < columns; n++) { matrix[i].Add(new double()); } } matrix[0][0] = double.Parse(textBox0_0.Text); matrix[1][0] = double.Parse(textBox1_0.Text); matrix[2][0] = double.Parse(textBox2_0.Text); matrix[0][1] = double.Parse(textBox0_1.Text); matrix[1][1] = double.Parse(textBox1_1.Text); matrix[2][1] = double.Parse(textBox2_1.Text); matrix[0][2] = double.Parse(textBox0_2.Text); matrix[1][2] = double.Parse(textBox1_2.Text); matrix[2][2] = double.Parse(textBox2_2.Text); matrix[0][3] = double.Parse(textBox0_3.Text); matrix[1][3] = double.Parse(textBox1_3.Text); matrix[2][3] = double.Parse(textBox2_3.Text); var equation = new EquationsSystem(matrix); richTextBox1.Clear(); ShowSolutionStep(equation); equation.FirstTransformation(); ShowSolutionStep(equation); equation.StairsTransformation(); ShowSolutionStep(equation); equation.InverseGauss(); double x = equation.Result.X; double y = equation.Result.Y; double z = equation.Result.Z; richTextBox1.Text += $"x = {x}\n y = {y}\n z = {z}\n"; }
private void ShowSolutionStep(EquationsSystem equation) { var text = $"|\t{equation.Matrix[0][0]}" + $"\t{equation.Matrix[0][1]}" + $"\t{equation.Matrix[0][2]}\t" + $"|\t{equation.Matrix[0][3]}\t|\n"; richTextBox1.Text += text; text = $"|\t{equation.Matrix[1][0]}" + $"\t{equation.Matrix[1][1]}" + $"\t{equation.Matrix[1][2]}\t" + $"|\t{equation.Matrix[1][3]}\t| -->\n"; richTextBox1.Text += text; text = $"|\t{equation.Matrix[2][0]}" + $"\t{equation.Matrix[2][1]}" + $"\t{equation.Matrix[2][2]}\t" + $"|\t{equation.Matrix[2][3]}\t|\n\n"; richTextBox1.Text += text; }
public void LinearSystem_Solved() { var kernel = new StandardKernel(new ConfigModule()); EquationsSystem eqSystem = new EquationsSystem() { EquationFunctions = new List <Func <double[], double[], double> >() { new Func <double[], double[], double>((x, alpha) => alpha[0] * x[0] + alpha[1] * x[1] + alpha[2]), new Func <double[], double[], double>((x, alpha) => alpha[0] * x[0] + alpha[1] * x[1] + alpha[2]), }, EquationsCoefficients = new List <double[]>() { new double[] { 2.0, 3.0, -5.0 }, new double[] { 1.0, 4.0, -5.0 } }, VariableNames = new string[] { "x", "y" } }; ISolverSettings solverSettings = kernel.Get <ISolverSettings>(); solverSettings.InitialValues = new double[] { 0.5, 0.5 }; solverSettings.MaxIterations = 100; solverSettings.Precision = 1e-5; ISolveMethod solutionMethod = kernel.Get <ISolveMethod> (new ConstructorArgument("system", eqSystem), new ConstructorArgument("settings", solverSettings)); EquationsSystemSolver solver = new EquationsSystemSolver(solutionMethod); solver.Run(); bool isCorrectSolution = solver.Solution.All(x => Math.Abs(x - 1.0) < 0.001); Assert.AreEqual(true, isCorrectSolution); }
public void GetMatrixReturnsDenseMatrixOfExpectedSize() { var equations = new List <Equation> { new Equation("3x+2y-5z=2"), new Equation("5x+3y-8z=7"), new Equation("6x+8y-3z=1") }; var equationsSystem = new EquationsSystem(equations); var expectedMatrix = DenseMatrix.OfArray(new double[, ] { { 3, 2, -5 }, { 5, 3, -8 }, { 6, 8, -3 } }); var equationMatrix = equationsSystem.GetMatrix(); Assert.Equal(expectedMatrix, equationMatrix); }
public void GetMatrixReturnsDenseMatrixOfExpectedSize() { var equations = new List<Equation> { new Equation("3x+2y-5z=2"), new Equation("5x+3y-8z=7"), new Equation("6x+8y-3z=1") }; var equationsSystem = new EquationsSystem(equations); var expectedMatrix = DenseMatrix.OfArray(new double[,] { {3,2,-5}, {5,3,-8}, {6,8,-3} }); var equationMatrix = equationsSystem.GetMatrix(); Assert.Equal(expectedMatrix, equationMatrix); }