[poincare/cosine] Implemented didDerivate and unaryFunctionDifferential for Cosine

Derivation now propagates as expected on cosines.

Change-Id: I6f5af48ac7462c5b8e77ff1a6428a65bf6c0c7de
This commit is contained in:
Gabriel Ozouf
2020-05-20 11:25:30 +02:00
committed by Émilie Feral
parent 5cf85368ea
commit 407d4bce6e
3 changed files with 29 additions and 0 deletions

View File

@@ -34,6 +34,10 @@ private:
LayoutShape leftLayoutShape() const override { return LayoutShape::MoreLetters; };
LayoutShape rightLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }
// Derivation
bool didDerivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
// Evaluation
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override {
return ApproximationHelper::Map<float>(this, context, complexFormat, angleUnit,computeOnComplex<float>);
@@ -51,6 +55,9 @@ public:
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("cos", 1, &UntypedBuilderOneChild<Cosine>);
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
bool didDerivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
};
}

View File

@@ -1,7 +1,11 @@
#include <poincare/cosine.h>
#include <poincare/complex.h>
#include <poincare/derivative.h>
#include <poincare/layout_helper.h>
#include <poincare/multiplication.h>
#include <poincare/rational.h>
#include <poincare/serialization_helper.h>
#include <poincare/sine.h>
#include <cmath>
@@ -34,6 +38,14 @@ Expression CosineNode::shallowReduce(ReductionContext reductionContext) {
return Cosine(this).shallowReduce(reductionContext);
}
bool CosineNode::didDerivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
return Cosine(this).didDerivate(reductionContext, symbol, symbolValue);
}
Expression CosineNode::unaryFunctionDifferential() {
return Cosine(this).unaryFunctionDifferential();
}
Expression Cosine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
@@ -45,5 +57,13 @@ Expression Cosine::shallowReduce(ExpressionNode::ReductionContext reductionConte
return Trigonometry::shallowReduceDirectFunction(*this, reductionContext);
}
bool Cosine::didDerivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
return true;
}
Expression Cosine::unaryFunctionDifferential() {
return Multiplication::Builder(Rational::Builder(-1), Sine::Builder(childAtIndex(0).clone()));
}
}

View File

@@ -44,4 +44,6 @@ QUIZ_CASE(poicare_differential_unary_functions) {
assert_parses_and_reduces_as("diff(sin(x),x,π)","-1");
assert_parses_and_reduces_as("diff(sin(2y),y,π/12)","√(3)");
assert_parses_and_reduces_as("diff(sin(2x)+sin(3x),x,π/6)","1");
assert_parses_and_reduces_as("diff(cos(x),x,π/2)","-1");
}