diff --git a/apps/graph/cartesian_function.cpp b/apps/graph/cartesian_function.cpp index 99a34cc6a..15f749e07 100644 --- a/apps/graph/cartesian_function.cpp +++ b/apps/graph/cartesian_function.cpp @@ -26,6 +26,17 @@ double CartesianFunction::approximateDerivative(double x, Poincare::Context * co return derivative.approximateToScalar(*context); } +double CartesianFunction::sumBetweenBounds(double start, double end, Poincare::Context * context) const { + Poincare::Complex x = Poincare::Complex::Float(start); + Poincare::Complex y = Poincare::Complex::Float(end); + Poincare::Expression * args[3] = {expression(context), &x, &y}; + Poincare::Integral integral(args, true); + /* TODO: when we will simplify integral, we might want to simplify the + * integral here. However, we might want to do it once for all x (to avoid + * lagging in the derivative table. */ + return integral.approximateToScalar(*context); +} + char CartesianFunction::symbol() const { return 'x'; } diff --git a/apps/graph/cartesian_function.h b/apps/graph/cartesian_function.h index 42ee9e508..9d4ed6f28 100644 --- a/apps/graph/cartesian_function.h +++ b/apps/graph/cartesian_function.h @@ -12,6 +12,7 @@ public: bool displayDerivative(); void setDisplayDerivative(bool display); double approximateDerivative(double x, Poincare::Context * context) const; + double sumBetweenBounds(double start, double end, Poincare::Context * context) const override; char symbol() const override; private: bool m_displayDerivative; diff --git a/apps/sequence/graph/term_sum_controller.cpp b/apps/sequence/graph/term_sum_controller.cpp index e367a7db1..e40e92af4 100644 --- a/apps/sequence/graph/term_sum_controller.cpp +++ b/apps/sequence/graph/term_sum_controller.cpp @@ -27,11 +27,6 @@ bool TermSumController::moveCursorHorizontallyToPosition(double position) { return SumGraphController::moveCursorHorizontallyToPosition(std::round(position)); } -double TermSumController::computeSum(double start, double end) { - App * myApp = static_cast(app()); - return static_cast(m_function)->sumOfTermsBetweenAbscissa(m_startSum, m_endSum, myApp->localContext()); -} - I18n::Message TermSumController::legendMessageAtStep(Step step) { switch(step) { case Step::FirstParameter: diff --git a/apps/sequence/graph/term_sum_controller.h b/apps/sequence/graph/term_sum_controller.h index aa6615783..a5472f8d4 100644 --- a/apps/sequence/graph/term_sum_controller.h +++ b/apps/sequence/graph/term_sum_controller.h @@ -14,7 +14,6 @@ public: const char * title() override; private: bool moveCursorHorizontallyToPosition(double position) override; - double computeSum(double start, double end) override; I18n::Message legendMessageAtStep(Step step) override; double cursorNextStep(double position, int direction) override; }; diff --git a/apps/sequence/sequence.cpp b/apps/sequence/sequence.cpp index b0bfe3ae2..289f97212 100644 --- a/apps/sequence/sequence.cpp +++ b/apps/sequence/sequence.cpp @@ -357,7 +357,7 @@ T Sequence::approximateToNextRank(int n, SequenceContext * sqctx) const { } } -double Sequence::sumOfTermsBetweenAbscissa(double start, double end, Context * context) { +double Sequence::sumBetweenBounds(double start, double end, Context * context) const { double result = 0.0; if (end-start > k_maxNumberOfTermsInSum || start + 1.0 == start) { return NAN; diff --git a/apps/sequence/sequence.h b/apps/sequence/sequence.h index 6ae589aec..12a3ecf44 100644 --- a/apps/sequence/sequence.h +++ b/apps/sequence/sequence.h @@ -53,7 +53,7 @@ public: return templatedApproximateAtAbscissa(x, static_cast(context)); } template T approximateToNextRank(int n, SequenceContext * sqctx) const; - double sumOfTermsBetweenAbscissa(double start, double end, Poincare::Context * context); + double sumBetweenBounds(double start, double end, Poincare::Context * context) const override; void tidy() override; constexpr static int k_initialRankNumberOfDigits = 3; // m_initialRank is capped by 999 private: diff --git a/apps/shared/function.h b/apps/shared/function.h index c01525e83..a750ff36d 100644 --- a/apps/shared/function.h +++ b/apps/shared/function.h @@ -33,6 +33,7 @@ public: virtual double evaluateAtAbscissa(double x, Poincare::Context * context) const { return templatedApproximateAtAbscissa(x, context); } + virtual double sumBetweenBounds(double start, double end, Poincare::Context * context) const = 0; virtual void tidy(); private: constexpr static size_t k_dataLengthInBytes = (TextField::maxBufferSize()+2)*sizeof(char)+2; diff --git a/apps/shared/sum_graph_controller.cpp b/apps/shared/sum_graph_controller.cpp index 153128bcb..7acaa31f9 100644 --- a/apps/shared/sum_graph_controller.cpp +++ b/apps/shared/sum_graph_controller.cpp @@ -192,7 +192,8 @@ bool SumGraphController::handleEnter() { return true; } m_step = (Step)((int)m_step+1); - double sum = computeSum(m_startSum, m_endSum); + TextFieldDelegateApp * myApp = static_cast(app()); + double sum = m_function->sumBetweenBounds(m_startSum, m_endSum, myApp->localContext()); m_legendView.setSumSymbol(m_step, m_startSum, m_endSum, sum, m_function->name()); m_legendView.setLegendMessage(I18n::Message::Default, m_step); m_graphView->setAreaHighlightColor(true); diff --git a/apps/shared/sum_graph_controller.h b/apps/shared/sum_graph_controller.h index 53fa0a60e..699e458f7 100644 --- a/apps/shared/sum_graph_controller.h +++ b/apps/shared/sum_graph_controller.h @@ -38,7 +38,6 @@ private: constexpr static float k_cursorRightMarginRatio = 0.04f; // (cursorWidth/2)/graphViewWidth constexpr static float k_cursorBottomMarginRatio = 0.28f; // (cursorHeight/2+bannerHeigh)/graphViewHeight constexpr static float k_cursorLeftMarginRatio = 0.04f; // (cursorWidth/2)/graphViewWidth - virtual double computeSum(double start, double end) = 0; virtual I18n::Message legendMessageAtStep(Step step) = 0; virtual double cursorNextStep(double position, int direction) = 0; Shared::InteractiveCurveViewRange * interactiveCurveViewRange() override { return m_graphRange; }