public void TestTwoDifferentRoots()     // "AAA" : Triple A
        {
            // ACT
            double[] roots = _solver.Solve(1, 1, -6);

            // ASSERT
            CollectionAssert.AreEquivalent(new [] { 2.0, -3.0 }, roots);

            // we could also test:

            // 1) CollectionAssert.AllItemsAreNotNull(roots);
            // 2) CollectionAssert.AllItemsAreUnique(roots);
        }
        public void TestTwoDifferentRoots(double a, double b, double c, double r1, double r2)
        {
            // ACT
            double[] roots = _solver.Solve(a, b, c);

            // ASSERT
            CollectionAssert.AreEquivalent(new[] { r1, r2 }, roots);

            // we could also test:

            // 1) CollectionAssert.AllItemsAreNotNull(roots);
            // 2) CollectionAssert.AllItemsAreUnique(roots);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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 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));
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 7
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());
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        public void Init_EquationSolver_should_return_message_about_infinitely_many_solutions()
        {
            double[,] _matrix = { { -3, 4,  1,  4, -1,  6 },
                                  {  0, 1,  3,  2, -1,  1 },
                                  {  4, 0, -2, -3,  4, -3 },
                                  {  5, 0, -4,  1,  1,  9 } };

            EquationSolver _solver = new EquationSolver();

            _solver.Init(_matrix);

            Assert.AreEqual("There are infinitely many solutions!", _solver.Solve());
        }
Exemplo n.º 10
0
        public void Init_EquationSolver_should_return_message_about_unique_solution()
        {
            double[,] _matrix = { {   -3, 4,  1,  4, -1 },
                                  {    0, 1,  3,  2, -1 },
                                  {    4, 0, -2, -3,  4 },
                                  { 1000, 3,  1, -5, -2 } };

            EquationSolver _solver = new EquationSolver();

            _solver.Init(_matrix);

            Assert.AreEqual("There is unique solution!", _solver.Solve());
        }
        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));
        }
Exemplo n.º 12
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());
        }
Exemplo n.º 13
0
        public void Init_EquationSolver_should_return_message_about_unique_solution2()
        {
            double[,] _matrix = { { 78, 53, 97, 43, 69, 86 },
                                  { 73, 94,  3, 90,  4, 77 },
                                  { 51, 88, 31, 94, 14, 36 },
                                  { 91, 60, 96, 38, 74, 56 },
                                  { 64, 34,  1, 28, 83, 15 } };

            EquationSolver _solver = new EquationSolver();

            _solver.Init(_matrix);

            Assert.AreEqual("There is unique solution!", _solver.Solve());
        }
        public void TestPolynomialRootsGetCalled()
        {
            var solver = new EquationSolver(_mockRoots.Object);

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

            _mockRoots.Verify(r => r.Calculate(coeffs));

            // more checks:

            // _mockRoots.Verify(r => r.Calculate(coeffs), Times.Once());
            // _mockRoots.VerifyNoOtherCalls();
        }
Exemplo n.º 15
0
        /// <summary>
        /// Returns the Target Damage to achieve a given WN8 value
        /// </summary>
        public double GetTargetDamage(double wn8)
        {
            var solution = EquationSolver.Solve(EquationSolver.Method.Brent, 0.0, 10000.0, damage => wn8 - GetWn8(damage),
                                                new EquationSolver.StopConditions(null, null, 1.0, TimeSpan.FromMilliseconds(100)));

            if (solution.Success)
            {
                return(solution.Result);
            }

            Log.Warn($"Could not find the Target Damage for WN8 of {wn8:N0} on tank {TankId}.{Name}: {solution.StopReasons}");

            return(0.0);
        }
Exemplo n.º 16
0
        private void Solve(object parameter)
        {
            var data = (MigrationViewModel)parameter;
            var enabledEnterprises = new HashSet <string>(data.Enterprises.Where(x => x.IsEnabled).Select(x => x.Id));
            var indices            =
                data.Indices.AsParallel()
                .Where(x => enabledEnterprises.Contains(x.EnterpriseId))
                .Select(x => x.ToArray())
                .ToList();

            var equationBuilder = new EquationSolver(Nodes);
            var result          = equationBuilder.Solve(indices);

            var elementIndex = 0;

            foreach (var node in Nodes)
            {
                if (!node.IsEnabled)
                {
                    elementIndex++;
                    continue;
                }
                node.Factor = result[elementIndex];
                elementIndex++;
                if (node.IsK1Enabled)
                {
                    node.FactorK1 = result[elementIndex];
                    elementIndex++;
                }
                if (node.IsK2Enabled)
                {
                    node.FactorK2 = result[elementIndex];
                    elementIndex++;
                }
                if (node.IsK3Enabled)
                {
                    node.FactorK3 = result[elementIndex];
                    elementIndex++;
                }
            }

            ModelErrors = new ModelErrorCalculator(Nodes).Calculate(indices);
        }
Exemplo n.º 17
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);
 }
Exemplo n.º 18
0
        public void TC02_A0_B0_C0()
        {
            var rs = EquationSolver.Solve(0, 0, 0);

            Assert.AreEqual(ResultType.NoSolution, rs.ResultType);
        }
Exemplo n.º 19
0
        public void TC01_A0_B0_C0()
        {
            var rs = EquationSolver.Solve(0, 0, 0);

            Assert.AreEqual(ResultType.Infinity, rs.ResultType);
        }
Exemplo n.º 20
0
 /// <summary>
 /// Solves one equation over one variable
 /// </summary>
 /// <param name="equation"></param>
 /// <param name="var"></param>
 /// <returns></returns>
 public static Set SolveEquation(Entity equation, VariableEntity var)
 => EquationSolver.Solve(equation, var);
Exemplo n.º 21
0
        private static bool Solve(string equation)
        {
            var solver = new EquationSolver();
            if (!solver.IsInCorrectFormat(equation))
                return false;

            return solver.Solve(equation);
        }
        public void TestMoreThatThreeCoefficients()
        {
            double[] roots = _solver.Solve(new double[] { 1, 3, -5, -15, 4, 12 });

            Assert.That(roots, Is.EquivalentTo(new double[] { -3, -2, -1, 1, 2 }));
        }
Exemplo n.º 23
0
 /// <summary>
 /// Attempt to find analytical roots of a custom equation
 /// </summary>
 /// <param name="x"></param>
 /// <returns>
 /// Returns Set. Work with it as with a list
 /// </returns>
 public Set SolveEquation(VariableEntity x) => EquationSolver.Solve(this, x);
Exemplo n.º 24
0
        public static async Task Main(string[] args)
        {
            MatrixProvider provider = new MatrixProvider(ImagePath);
            ResultsSaver   saver    = new ResultsSaver();

            IEquationSolver sequentialSolver = new EquationSolver();
            IEquationSolver parallelSolver   = new ParallelEquationSolver();

            IEquationSolver choleskySolver         = new CholeskySolver();
            IEquationSolver parallelCholeskySolver = new ParallelCholeskySolver();

            IEquationSolver diagonalSolver         = new DiagonalSolver();
            IEquationSolver parallelDiagonalSolver = new ParallelDiagonalSolver();

            IEquationSolver jacobiSolver         = new JacobiSolver();
            IEquationSolver parallelJacobiSolver = new ParallelJacobiSolver();

            int[] sizes = { 10, 100, 500 };

            sequentialSolver.Solve(GetTestMatrix());

            // Console.WriteLine("Sequential Cramer:");
            // await PerformTesting(sizes, provider, saver, sequentialSolver);
            //
            // Console.WriteLine();
            // Console.WriteLine("Parallel Cramer:");
            // await PerformTesting(sizes, provider, saver, parallelSolver);

            // Console.WriteLine();
            // Console.WriteLine("Sequential Cholesky:");
            // choleskySolver.Solve(GetCholeskyTestMatrix());
            // await PerformTesting(sizes, provider, saver, choleskySolver);
            //
            // Console.WriteLine();
            // Console.WriteLine("Parallel Cholesky:");
            // parallelSolver.Solve(GetCholeskyTestMatrix());
            // await PerformTesting(sizes, provider, saver, parallelCholeskySolver);
            //
            // Console.WriteLine();
            // Console.WriteLine("Sequential K-Diagonal:");
            // diagonalSolver.Solve(GetDiagonalTestMatrix());
            // await PerformTesting(sizes, provider, saver, diagonalSolver);
            //
            // Console.WriteLine();
            // Console.WriteLine("Parallel K-Diagonal:");
            // parallelDiagonalSolver.Solve(GetDiagonalTestMatrix2());
            // await PerformTesting(sizes, provider, saver, parallelDiagonalSolver);

            Console.WriteLine();
            Console.WriteLine("Sequential Jacobi on common matrix:");
            jacobiSolver.Solve(GetDiagonalTestMatrix2());
            await PerformTesting(sizes, provider, saver, jacobiSolver);

            Console.WriteLine();
            Console.WriteLine("Sequential Jacobi on chess matrix:");
            jacobiSolver.Solve(GetDiagonalTestMatrix2());
            await PerformTesting(sizes, provider, saver, jacobiSolver, matrix => matrix.ToChessZero());

            Console.WriteLine();
            Console.WriteLine("Parallel Jacobi on common matrix:");
            parallelJacobiSolver.Solve(GetDiagonalTestMatrix2());
            await PerformTesting(sizes, provider, saver, parallelJacobiSolver);

            Console.WriteLine();
            Console.WriteLine("Parallel Jacobi on chess matrix:");
            parallelJacobiSolver.Solve(GetDiagonalTestMatrix2());
            await PerformTesting(sizes, provider, saver, parallelJacobiSolver, matrix => matrix.ToChessZero());
        }