From 4b40099a9ebc8538aa5f35d86574dfd10e259cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 12 Jun 2018 15:05:16 +0200 Subject: [PATCH] [solver] Clean equation store --- apps/solver/equation_store.cpp | 11 ++++++----- apps/solver/equation_store.h | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/apps/solver/equation_store.cpp b/apps/solver/equation_store.cpp index 1b31e9af3..488f36acd 100644 --- a/apps/solver/equation_store.cpp +++ b/apps/solver/equation_store.cpp @@ -143,15 +143,16 @@ EquationStore::Error EquationStore::exactSolve(Poincare::Context * context) { char x = m_variables[0]; Expression * polynomialCoefficients[Expression::k_maxNumberOfPolynomialCoefficients]; int degree = definedModelAtIndex(0)->standardForm(context)->getPolynomialCoefficients(x, polynomialCoefficients, *context); - if (degree < 0) { - /* 3- Monovariable non-polynomial */ + if (degree == 2) { + /* Polynomial degree <= 2*/ + m_type = Type::PolynomialMonovariable; + error = oneDimensialPolynomialSolve(exactSolutions, polynomialCoefficients, degree, context); + } else { + /* 3- Monovariable non-polynomial or polynomial with degree > 2 */ m_type = Type::Monovariable; m_intervalApproximateSolutions[0] = -10; m_intervalApproximateSolutions[1] = 10; return Error::RequireApproximateSolution; - } else { - m_type = Type::PolynomialMonovariable; - error = oneDimensialPolynomialSolve(exactSolutions, polynomialCoefficients, degree, context); } } /* Turn the results in layouts */ diff --git a/apps/solver/equation_store.h b/apps/solver/equation_store.h index f761c72ab..842c8a1fc 100644 --- a/apps/solver/equation_store.h +++ b/apps/solver/equation_store.h @@ -21,6 +21,7 @@ public: NonLinearSystem = -3, RequireApproximateSolution = -4 }; + /* EquationStore */ EquationStore(); ~EquationStore(); Equation * modelAtIndex(int i) override { @@ -39,7 +40,18 @@ public: int numberOfSolutions() const { return m_numberOfSolutions; } + /* Exact resolution */ + Error exactSolve(Poincare::Context * context); + /* The exact solutions are displayed in a table with 2 layouts: an exact + * Layout and an approximate layout. For example, 'sqrt(2)' and '1.414213'. + * The boolean exactLayout indicates if we want the exact layout or the + * approximate one. */ Poincare::ExpressionLayout * exactSolutionLayoutAtIndex(int i, bool exactLayout); + /* Exact layout and approximate layout of an exact solution can be: + * - identical: for instance, 5 and 5 + * - equal: for instance 1/2 and 0.5 + * - non-equal: for instance 1/3 and 0.333. + */ bool exactSolutionLayoutsAtIndexAreIdentical(int i) { assert(m_type != Type::Monovariable && i >= 0 && (i < m_numberOfSolutions || (i == m_numberOfSolutions && m_type == Type::PolynomialMonovariable))); return m_exactSolutionIdentity[i]; @@ -48,13 +60,14 @@ public: assert(m_type != Type::Monovariable && i >= 0 && (i < m_numberOfSolutions || (i == m_numberOfSolutions && m_type == Type::PolynomialMonovariable))); return m_exactSolutionEquality[i]; } + /* Approximate resolution */ double intervalBound(int index) const; void setIntervalBound(int index, double value); double approximateSolutionAtIndex(int i); - void tidy() override; void approximateSolve(Poincare::Context * context); bool haveMoreApproximationSolutions(Poincare::Context * context); - Error exactSolve(Poincare::Context * context); + + void tidy() override; static constexpr int k_maxNumberOfExactSolutions = Poincare::Expression::k_maxNumberOfVariables > Poincare::Expression::k_maxPolynomialDegree + 1? Poincare::Expression::k_maxNumberOfVariables : Poincare::Expression::k_maxPolynomialDegree + 1; static constexpr int k_maxNumberOfApproximateSolutions = 10; static constexpr int k_maxNumberOfSolutions = k_maxNumberOfExactSolutions > k_maxNumberOfApproximateSolutions ? k_maxNumberOfExactSolutions : k_maxNumberOfApproximateSolutions;