Beispiel #1
0
        public void TC03_A0_B0_C0()
        {
            var rs = EquationSolver.Solve(0, 1, 5);

            Assert.AreEqual(ResultType.OneSolution, rs.ResultType);
            Assert.AreEqual(-5, rs.x1);
        }
        public void RowElementContainsTest()
        {
            int    a = 2;
            double x = 0.1;

            Assert.AreEqual(0.2, EquationSolver.GetRowSequenceElement(a, 1, x));
        }
        public void IterationsAlgoritmSystemIsWrong()
        {
            // Arrange
            double[]       firstEquationCoeffs     = new double[] { 4, -3, 2, -1 };
            double         firstEquationFreeMember = 8;
            LinearEquation equation1 = new LinearEquation(firstEquationCoeffs, firstEquationFreeMember);

            double[]       secondEquationCoeffs     = new double[] { 3, -2, 1, -3 };
            double         secondEquationFreeMember = 7;
            LinearEquation equation2 = new LinearEquation(secondEquationCoeffs, secondEquationFreeMember);

            double[]       thirdEquationCoeffs     = new double[] { 5, -3, 1, -8 };
            double         thirdEquationFreeMember = 1;
            LinearEquation equation3 = new LinearEquation(thirdEquationCoeffs, thirdEquationFreeMember);

            LinearEquation[]     equations      = new LinearEquation[] { equation1, equation2, equation3 };
            LinearEquationSystem equationSystem = new LinearEquationSystem(equations);

            double[] answer = new double[equations.Length];

            bool isExceptionThrown = false;

            try
            {
                answer = EquationSolver.Iterations(equationSystem);
            }
            catch (Exception)
            {
                isExceptionThrown = true;
            }
            finally
            {
                Assert.AreEqual(isExceptionThrown, true);
            }
        }
Beispiel #4
0
        public static void DisplayMatrix(List <LinearEquation> i_equations, DataGridView i_table)
        {
            i_table.Columns.Clear();

            i_table.RowCount    = i_equations.Count;
            i_table.ColumnCount = EquationSolver.MaxCoeffCount(i_equations) + 1;

            for (int i = 0; i < i_table.ColumnCount; i++)
            {
                if (i < i_table.ColumnCount - 1)
                {
                    i_table.Columns[i].HeaderText = "X" + (i + 1);
                }
                else
                {
                    i_table.Columns[i].HeaderText = "Sum";
                }

                for (int j = 0; j < i_table.RowCount; j++)
                {
                    if (i < i_table.ColumnCount - 1)
                    {
                        i_table[i, j].Value = Math.Round(i_equations[j].GetCoeff(i), 2);
                    }
                    else
                    {
                        i_table[i, j].Value = Math.Round(i_equations[j].Sum, 2);
                    }
                }
            }
        }
Beispiel #5
0
        // solves equation f(sin(x), cos(x), tan(x), cot(x)) for x
        internal static Set SolveLinear(Entity expr, VariableEntity variable)
        {
            var replacement = Utils.FindNextIndex(expr, variable.Name);

            expr = ReplaceTrigonometry(expr, variable, replacement);

            // if there is still original variable after replacements,
            // equation is not in a form f(sin(x), cos(x), tan(x), cot(x))
            if (expr.FindSubtree(variable) != null)
            {
                return(null);
            }

            var solutions = EquationSolver.Solve(expr, replacement);

            if (solutions == null)
            {
                return(null);
            }

            var actualSolutions = new Set();

            // TODO: make check for infinite solutions
            foreach (var solution in solutions.FiniteSet())
            {
                var sol = TreeAnalyzer.FindInvertExpression(MathS.Pow(MathS.e, MathS.i * variable), solution, variable);
                if (sol != null)
                {
                    actualSolutions.AddRange(sol);
                }
            }
            return(actualSolutions);
        }
Beispiel #6
0
        public void SystemOfTwoEquationsTest()
        {
            //equation 4x + 5y = 47
            //         2x + 9y = 69
            var solution = EquationSolver.SystemOfTwoEquations(4, 5, 2, 9, 47, 69);

            Assert.AreEqual(solution, new Vector(3, 7));
        }
Beispiel #7
0
        public void RowSummaryTest()
        {
            int    a       = 2;
            double x       = 2;
            double epsilon = 0.001;

            Assert.AreEqual(4, EquationSolver.GetSumAndCountOfSteps(a, x, epsilon)[1], epsilon);
        }
Beispiel #8
0
        public void StepsCountTest()
        {
            int    a       = 2;
            double x       = 0.1;
            double epsilon = 0.001;

            Assert.AreEqual(8, EquationSolver.GetSumAndCountOfSteps(a, x, epsilon)[0]);
        }
Beispiel #9
0
        public static void Coefficients_ShouldBeEqualToDiscriminant()
        {
            int[] coefficients = new int[] { 1, 5, 4 };
            int   discriminant = EquationSolver.reternDiscriminant(coefficients);
            int   actualNumber = 9;

            Assert.AreEqual(actualNumber, discriminant);
        }
Beispiel #10
0
        public void QuadraticEquationExTest()
        {
            //equation 2x^2+3x-1
            var result = EquationSolver.QuadraticEquationEx(2, 3, -1);

            Assert.IsTrue(result[0].ApproximatelyEquals(-1.78077640640442, 1e1 - 0));
            Assert.IsTrue(result[1].ApproximatelyEquals(0.280776406404415, 1e-10));
        }
Beispiel #11
0
        public void SolveEquation_CallsParser()
        {
            var equationParserStub = new EquationParserStub();
            var equationSolver     = new EquationSolver(equationParserStub);

            var solution = equationSolver.SolveEquation("blahblahblahblah");

            Assert.IsTrue(equationParserStub.called);
        }
        public void TestPolynomialRootsGetCalled()
        {
            FakeRoots      rootsEvaluator = new FakeRoots();
            EquationSolver solver         = new EquationSolver(rootsEvaluator);

            double[] roots = solver.Solve(new double[] { 1, 3, -5, -15, 4, 12 });

            Assert.That(rootsEvaluator.SolvedState, Is.EqualTo(true));
        }
Beispiel #13
0
        public void Init_EquationSolver_should_return_message_if_matrix_is_empty()
        {
            double[,] _matrix = { { } };

            EquationSolver _solver = new EquationSolver();

            _solver.Init(_matrix);

            Assert.AreEqual("Entering matrix is empty!", _solver.Solve());
        }
Beispiel #14
0
        public void FactorialTest()
        {
            var numbers    = new[] { 1, 2, 3, 4, 5 };
            var factorials = new[] { 1, 2, 6, 24, 120 };

            for (int i = 0; i < numbers.Length; i++)
            {
                Assert.AreEqual(factorials[i], EquationSolver.GetFactorial(numbers[i]));
            }
        }
Beispiel #15
0
        public void EquationSolverTest()
        {
            EquationSolver eqs       = new EquationSolver("2 x + 3y =0 , 2y + 3x = 10");
            List <char>    variables = new List <char>()
            {
                'x', 'y'
            };

            CollectionAssert.AreEqual(variables, eqs.AllVariables);
        }
Beispiel #16
0
        public void CreateMatrixTest()
        {
            EquationSolver  eqs            = new EquationSolver("2x+3y+4z=0,5z+3y+2x=10,6x+7y+8z=13");
            Matrix <double> exceptedMatrix = new Matrix <double>(new List <Vector <double> >()
            {
                new Vector <double>(3)
                {
                    2, 3, 4
                },
                new Vector <double>(3)
                {
                    2, 3, 5
                },
                new Vector <double>(3)
                {
                    6, 7, 8
                }
            });

            Assert.AreEqual(exceptedMatrix, eqs.CreateMatrix());
            eqs            = new EquationSolver("2x+3y=0,2x=10,6x+7y+8z=13");
            exceptedMatrix = new Matrix <double>(new List <Vector <double> >()
            {
                new Vector <double>(3)
                {
                    2, 3, 0
                },
                new Vector <double>(3)
                {
                    2, 0, 0
                },
                new Vector <double>(3)
                {
                    6, 7, 8
                }
            });
            Assert.AreEqual(exceptedMatrix, eqs.CreateMatrix());
            eqs            = new EquationSolver("2x+3y=0,6x+7y+8z=13");
            exceptedMatrix = new Matrix <double>(new List <Vector <double> >()
            {
                new Vector <double>(3)
                {
                    2, 3, 0
                },
                new Vector <double>(3)
                {
                    6, 7, 8
                },
                new Vector <double>(3)
                {
                    0, 0, 0
                }
            });
            Assert.AreEqual(exceptedMatrix, eqs.CreateMatrix());
        }
Beispiel #17
0
        static void Main()
        {
            _mSolver = new EquationSolver();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var _mainForm = new Form1();

            _mainForm.StartPosition = FormStartPosition.CenterScreen;
            Application.Run(_mainForm);
        }
        //получение собственных значений матрицы
        private static double[] GetEigenValues(double[] p, int numberOfRoots)
        {
            //по формуле, при построении полинома знаки коэфициентов около всех степеней, кроме старшей, меняются
            var SignChangedCoefs = p.Select(v => - v).ToList();

            //добавляем 1 для старшей степени
            SignChangedCoefs.Insert(0, 1);

            //return EquationSolver.GetRoots(SignChangedCoefs.ToArray(), 0.00000001, 0.1);
            return(EquationSolver.GetRoots(SignChangedCoefs.ToArray(), numberOfRoots));
        }
Beispiel #19
0
        public void IsEquationQuadratic_QuadraticEquationWithFirstAndThirdArguments()
        {
            //arrange
            string         equation       = "2x^2+7,15";
            EquationSolver equationSolver = new EquationSolver();

            //act
            bool isQuadratic = equationSolver.IsEquationQuadratic(equation);

            //assert
            Assert.AreEqual(true, isQuadratic);
        }
Beispiel #20
0
        public void IsEquationQuadratic_QuadraticEquationWithOneArgument()
        {
            //arrange
            string         equation       = "3,14x^2";
            EquationSolver equationSolver = new EquationSolver();

            //act
            bool isQuadratic = equationSolver.IsEquationQuadratic(equation);

            //assert
            Assert.AreEqual(true, isQuadratic);
        }
Beispiel #21
0
        public void Parse_ThreeUnitCoefficients()
        {
            //arrange
            string         equation       = "x^2 + x - 1";
            EquationSolver equationSolver = new EquationSolver();

            //act
            double[] coefficients = equationSolver.Parse(equation);

            //assert
            Assert.AreEqual(new double[] { 1, 1, -1 }, coefficients);
        }
Beispiel #22
0
        public void Parse_OnlyFirstAndSecondCoefficients()
        {
            //arrange
            string         equation       = "3x^2 + 4x";
            EquationSolver equationSolver = new EquationSolver();

            //act
            double[] coefficients = equationSolver.Parse(equation);

            //assert
            Assert.AreEqual(new double[] { 3, 4, 0 }, coefficients);
        }
Beispiel #23
0
        public void TestPolynomialRootsGetCalled()
        {
            var solver = new EquationSolver(_mockRoots);

            double[] coeffs = { 1, 3, -5, -15, 4, 12 };
            double[] roots  = solver.Solve(coeffs);

            _mockRoots.Received().Calculate(coeffs);

            // equivalent to:
            // _mockRoots.Received(1).Calculate(coeffs);
        }
Beispiel #24
0
        public void IsEquationQuadratic_FullQuadraticEquation()
        {
            //arrange
            string         equation       = "2x^2-4x+5";
            EquationSolver equationSolver = new EquationSolver();

            //act
            bool isQuadratic = equationSolver.IsEquationQuadratic(equation);

            //assert
            Assert.AreEqual(true, isQuadratic);
        }
Beispiel #25
0
        public void Init_EquationSolver_should_return_message_about_unique_solution3()
        {
            double[,] _matrix = { { 8432, 4825, 4305, 6171 },
                                  {  643, 4399, 7976,    0 },
                                  { 8822, 7372, 9169,    0 } };

            EquationSolver _solver = new EquationSolver();

            _solver.Init(_matrix);

            Assert.AreEqual("There is unique solution!", _solver.Solve());
        }
Beispiel #26
0
        public void GetDeterminant_ThreeNegativeCoefficients_ReturnCorrectDeterminant()
        {
            //arrange
            double[]       coefficients   = new[] { -1.0, -4, -2 };
            EquationSolver equationSolver = new EquationSolver();

            //act
            double determinant = equationSolver.GetDeterminant(coefficients);

            //assert
            Assert.AreEqual(8, determinant);
        }
        static void Main(string[] args)
        {
            int a = int.Parse(Console.ReadLine());

            double
                x       = double.Parse(Console.ReadLine()),
                epsilon = double.Parse(Console.ReadLine());

            double[] data = EquationSolver.GetSumAndCountOfSteps(a, x, epsilon);

            Console.WriteLine("Номер шага: {0}\nСумма ряда: {1}", data[0], data[1]);
        }
Beispiel #28
0
        public void Init_EquationSolver_should_return_message_about_no_solutions()
        {
            double[,] _matrix = { { -3, 4,  1,  4, -1 },
                                  {  0, 1,  3,  2, -1 },
                                  {  4, 0, -2, -3,  4 },
                                  {  0, 0,  0,  0, -2 } };

            EquationSolver _solver = new EquationSolver();

            _solver.Init(_matrix);

            Assert.AreEqual("There are no solutions!", _solver.Solve());
        }
        public void ParseValidEquationString_ReturnsCoefficients()
        {
            var equation     = "4x^2-12x+4=0";
            var aCoefficient = 4;
            var bCoefficient = -12;
            var cCoefficient = 4;

            var actualCoefficients = EquationSolver.GetCoefficients(equation);

            Assert.AreEqual(aCoefficient, actualCoefficients[0]);
            Assert.AreEqual(bCoefficient, actualCoefficients[1]);
            Assert.AreEqual(cCoefficient, actualCoefficients[2]);
        }
        public void TestMoreThatThreeCoefficients()
        {
            double[] coeffs     = { 1, 3, -5, -15, 4, 12 };
            double[] validRoots = { -3, -2, -1, 1, 2 };

            _mockRoots.Setup(r => r.Calculate(coeffs)).Returns(validRoots);

            var solver = new EquationSolver(_mockRoots.Object);

            double[] roots = solver.Solve(coeffs);

            Assert.That(roots, Is.EquivalentTo(validRoots));
        }
Beispiel #31
0
 public ActionResult Index(SolutionViewModel solutionViewModel)
 {
     var resultViewModel = new ResultViewModel();
     var solver = new EquationSolver();
     if (solver.IsInCorrectFormat(solutionViewModel.Solution)) {
         if (solver.Solve(solutionViewModel.Solution)) {
             resultViewModel.HasSolution = true;
             resultViewModel.Message = "Well done :) ";
             resultViewModel.Time = TimeSpan.FromSeconds(solutionViewModel.Duration);
             resultViewModel.Equation = solutionViewModel.Equation;
             resultViewModel.Solution = solver.Format(solutionViewModel.Solution);
         }
         else {
             resultViewModel.Message = "Sides not equal - try again!";
         }
     }
     else {
         resultViewModel.Message = "Format error - try again!";
     }
     return View(resultViewModel);
 }
Beispiel #32
0
        private static bool Solve(string equation)
        {
            var solver = new EquationSolver();
            if (!solver.IsInCorrectFormat(equation))
                return false;

            return solver.Solve(equation);
        }