diff --git a/poincare/include/poincare/trigonometry.h b/poincare/include/poincare/trigonometry.h index 7f2f1b5e7..11553b48b 100644 --- a/poincare/include/poincare/trigonometry.h +++ b/poincare/include/poincare/trigonometry.h @@ -24,6 +24,7 @@ public: template static std::complex ConvertRadianToAngleUnit(const std::complex c, Preferences::AngleUnit angleUnit); template static std::complex RoundToMeaningfulDigits(const std::complex result, const std::complex input); private: + static Expression mapIfPossible(Expression & e, ExpressionNode::ReductionContext reductionContext); template static T RoundToMeaningfulDigits(T result, T input); }; diff --git a/poincare/src/arc_cosine.cpp b/poincare/src/arc_cosine.cpp index 9980d0bd5..6c0c75d65 100644 --- a/poincare/src/arc_cosine.cpp +++ b/poincare/src/arc_cosine.cpp @@ -56,9 +56,6 @@ Expression ArcCosine::shallowReduce(ExpressionNode::ReductionContext reductionCo return e; } } - if (childAtIndex(0).type() == ExpressionNode::Type::Matrix) { - return mapOnMatrixFirstChild(reductionContext); - } return Trigonometry::shallowReduceInverseFunction(*this, reductionContext); } diff --git a/poincare/src/arc_sine.cpp b/poincare/src/arc_sine.cpp index f62eaa7c9..f6e3195cc 100644 --- a/poincare/src/arc_sine.cpp +++ b/poincare/src/arc_sine.cpp @@ -56,9 +56,6 @@ Expression ArcSine::shallowReduce(ExpressionNode::ReductionContext reductionCont return e; } } - if (childAtIndex(0).type() == ExpressionNode::Type::Matrix) { - return mapOnMatrixFirstChild(reductionContext); - } return Trigonometry::shallowReduceInverseFunction(*this, reductionContext); } diff --git a/poincare/src/arc_tangent.cpp b/poincare/src/arc_tangent.cpp index 1f33c83fe..facefcddb 100644 --- a/poincare/src/arc_tangent.cpp +++ b/poincare/src/arc_tangent.cpp @@ -55,9 +55,6 @@ Expression ArcTangent::shallowReduce(ExpressionNode::ReductionContext reductionC return e; } } - if (childAtIndex(0).type() == ExpressionNode::Type::Matrix) { - return mapOnMatrixFirstChild(reductionContext); - } return Trigonometry::shallowReduceInverseFunction(*this, reductionContext); } diff --git a/poincare/src/cosine.cpp b/poincare/src/cosine.cpp index a47356564..4b92d0529 100644 --- a/poincare/src/cosine.cpp +++ b/poincare/src/cosine.cpp @@ -41,10 +41,6 @@ Expression Cosine::shallowReduce(ExpressionNode::ReductionContext reductionConte return e; } } - Expression c = childAtIndex(0); - if (c.type() == ExpressionNode::Type::Matrix) { - return mapOnMatrixFirstChild(reductionContext); - } return Trigonometry::shallowReduceDirectFunction(*this, reductionContext); } diff --git a/poincare/src/sine.cpp b/poincare/src/sine.cpp index c7b5f902f..dd2dd22d3 100644 --- a/poincare/src/sine.cpp +++ b/poincare/src/sine.cpp @@ -42,9 +42,6 @@ Expression Sine::shallowReduce(ExpressionNode::ReductionContext reductionContext return e; } } - if (childAtIndex(0).type() == ExpressionNode::Type::Matrix) { - return mapOnMatrixFirstChild(reductionContext); - } return Trigonometry::shallowReduceDirectFunction(*this, reductionContext); } diff --git a/poincare/src/tangent.cpp b/poincare/src/tangent.cpp index c731e78c8..66c03d9a7 100644 --- a/poincare/src/tangent.cpp +++ b/poincare/src/tangent.cpp @@ -46,10 +46,6 @@ Expression Tangent::shallowReduce(ExpressionNode::ReductionContext reductionCont } } - if (childAtIndex(0).type() == ExpressionNode::Type::Matrix) { - return mapOnMatrixFirstChild(reductionContext); - } - Expression newExpression = Trigonometry::shallowReduceDirectFunction(*this, reductionContext); if (newExpression.type() == ExpressionNode::Type::Tangent) { Sine s = Sine::Builder(newExpression.childAtIndex(0).clone()); diff --git a/poincare/src/trigonometry.cpp b/poincare/src/trigonometry.cpp index 7ae73d855..3b834770b 100644 --- a/poincare/src/trigonometry.cpp +++ b/poincare/src/trigonometry.cpp @@ -96,6 +96,14 @@ bool Trigonometry::ExpressionIsEquivalentToTangent(const Expression & e) { Expression Trigonometry::shallowReduceDirectFunction(Expression & e, ExpressionNode::ReductionContext reductionContext) { assert(isDirectTrigonometryFunction(e)); + // Step 0. Map on matrix child if possible + { + Expression mapped = mapIfPossible(e, reductionContext); + if (!mapped.isUninitialized()) { + return mapped; + } + } + // Step 1. Try finding an easy standard calculation reduction Expression lookup = TrigonometryCheatTable::Table()->simplify(e.childAtIndex(0), e.type(), reductionContext); if (!lookup.isUninitialized()) { @@ -254,6 +262,14 @@ Expression Trigonometry::shallowReduceDirectFunction(Expression & e, ExpressionN Expression Trigonometry::shallowReduceInverseFunction(Expression & e, ExpressionNode::ReductionContext reductionContext) { assert(isInverseTrigonometryFunction(e)); + // Step 0. Map on matrix child if possible + { + Expression mapped = mapIfPossible(e, reductionContext); + if (!mapped.isUninitialized()) { + return mapped; + } + } + float pi = reductionContext.angleUnit() == Preferences::AngleUnit::Radian ? M_PI : 180; // Step 1. Look for an expression of type "acos(cos(x))", return x @@ -360,6 +376,13 @@ std::complex Trigonometry::ConvertRadianToAngleUnit(const std::complex c, return c; } +Expression Trigonometry::mapIfPossible(Expression & e, ExpressionNode::ReductionContext reductionContext) { + if (e.childAtIndex(0).type() == ExpressionNode::Type::Matrix) { + return e.mapOnMatrixFirstChild(reductionContext); + } + return Expression(); +} + template T Trigonometry::RoundToMeaningfulDigits(T result, T input) { /* Cheat: openbsd trigonometric functions are numerical implementation and