[poincare] Factorize troigonometry mapping on matrices

This commit is contained in:
Léa Saviot
2019-07-08 10:46:13 +02:00
committed by Émilie Feral
parent ca6f6e476d
commit eee0c815d5
8 changed files with 24 additions and 20 deletions

View File

@@ -24,6 +24,7 @@ public:
template <typename T> static std::complex<T> ConvertRadianToAngleUnit(const std::complex<T> c, Preferences::AngleUnit angleUnit);
template <typename T> static std::complex<T> RoundToMeaningfulDigits(const std::complex<T> result, const std::complex<T> input);
private:
static Expression mapIfPossible(Expression & e, ExpressionNode::ReductionContext reductionContext);
template <typename T> static T RoundToMeaningfulDigits(T result, T input);
};

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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());

View File

@@ -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<T> Trigonometry::ConvertRadianToAngleUnit(const std::complex<T> 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<typename T>
T Trigonometry::RoundToMeaningfulDigits(T result, T input) {
/* Cheat: openbsd trigonometric functions are numerical implementation and