diff --git a/src/fintamath/expressions/unary/TrigExpression.cpp b/src/fintamath/expressions/unary/TrigExpression.cpp index 1a7d52b8a..9dd2ec966 100644 --- a/src/fintamath/expressions/unary/TrigExpression.cpp +++ b/src/fintamath/expressions/unary/TrigExpression.cpp @@ -103,11 +103,14 @@ ArgumentPtr TrigExpression::trigTableSimplify(const IFunction &func, const Ratio static const TrigonometryFunctionsTable trigTable = { {Sin().toString(), &trigTableSinSimplify}, {Cos().toString(), &trigTableCosSimplify}, - {Tan().toString(), &trigTableTanSimplify}, - {Cot().toString(), &trigTableCotSimplify}, }; - Rational rhsShifted = phaseShiftPeriod(rhs); - return trigTable.at(func.toString())(rhsShifted); + + if (auto iter = trigTable.find(func.toString()); iter != trigTable.end()) { + Rational rhsShifted = phaseShiftPeriod(rhs); + return iter->second(rhsShifted); + } + + return {}; } ArgumentPtr TrigExpression::trigTableSinSimplify(const Rational &rhs) { @@ -142,38 +145,6 @@ ArgumentPtr TrigExpression::trigTableCosSimplify(const Rational &rhs) { return findValue(trigTable, rhsShifted, isNegated); } -ArgumentPtr TrigExpression::trigTableTanSimplify(const Rational &rhs) { - static const TrigonometryTable trigTable = { - {Rational(0), Integer(0).clone()}, // 0 | 0 - {Rational(1, 6), mulExpr(Rational(1, 3).clone(), sqrtExpr(Integer(3)))}, // π/6 | √3/3 - {Rational(1, 4), Integer(1).clone()}, // π/4 | 1 - {Rational(1, 3), sqrtExpr(Integer(3))}, // π/3 | √3 - {Rational(1, 2), ComplexInf().clone()}, // π/2 | ComplexInf - {Rational(2, 3), negExpr(sqrtExpr(Integer(3)))}, // 2π/3 | -√3 - {Rational(3, 4), Integer(-1).clone()}, // 3π/4 | -1 - {Rational(5, 6), mulExpr(Rational(-1, 3).clone(), sqrtExpr(Integer(3)))}, // 5π/6 | -√3/3 - {Rational(1), Integer(0).clone()}, // π | 0 - }; - auto [rhsShifted, isNegated] = phaseShiftTan(rhs); - return findValue(trigTable, rhsShifted, isNegated); -} - -ArgumentPtr TrigExpression::trigTableCotSimplify(const Rational &rhs) { - static const TrigonometryTable trigTable = { - {Rational(0), ComplexInf().clone()}, // 0 | ComplexInf - {Rational(1, 6), sqrtExpr(Integer(3))}, // π/6 | √3 - {Rational(1, 4), Integer(1).clone()}, // π/4 | 1 - {Rational(1, 3), mulExpr(Rational(1, 3).clone(), sqrtExpr(Integer(3)))}, // π/3 | √3/3 - {Rational(1, 2), Integer(0).clone()}, // π/2 | 0 - {Rational(2, 3), mulExpr(Rational(-1, 3).clone(), sqrtExpr(Integer(3)))}, // 2π/3 | -√3/3 - {Rational(3, 4), Integer(-1).clone()}, // 3π/4 | -1 - {Rational(5, 6), negExpr(sqrtExpr(Integer(3)))}, // 5π/6 | -√3 - {Rational(1), ComplexInf().clone()}, // π | ComplexInf - }; - auto [rhsShifted, isNegated] = phaseShiftCot(rhs); - return findValue(trigTable, rhsShifted, isNegated); -} - std::tuple TrigExpression::phaseShiftSin(const Rational &rhs) { Rational rhsShifted = rhs; bool isNegated = false; @@ -207,26 +178,6 @@ std::tuple TrigExpression::phaseShiftCos(const Rational &rhs) { return {rhsShifted, isNegated}; } -std::tuple TrigExpression::phaseShiftTan(const Rational &rhs) { - Rational rhsShifted = rhs; - bool isNegated = false; - - if (rhsShifted < 0) { - rhsShifted = -rhsShifted; - isNegated = !isNegated; - } - - if (rhsShifted.numerator() > rhsShifted.denominator()) { - rhsShifted = Rational(rhsShifted.numerator() % rhsShifted.denominator(), rhsShifted.denominator()); - } - - return {rhsShifted, isNegated}; -} - -std::tuple TrigExpression::phaseShiftCot(const Rational &rhs) { - return phaseShiftTan(rhs); -} - Rational TrigExpression::phaseShiftPeriod(const Rational &rhs) { return Rational(rhs.numerator() % (rhs.denominator() * 2), rhs.denominator()); } diff --git a/src/fintamath/expressions/unary/TrigExpression.hpp b/src/fintamath/expressions/unary/TrigExpression.hpp index efd25f028..23134b78d 100644 --- a/src/fintamath/expressions/unary/TrigExpression.hpp +++ b/src/fintamath/expressions/unary/TrigExpression.hpp @@ -34,18 +34,10 @@ class TrigExpression : public IUnaryExpressionCRTP { static ArgumentPtr trigTableCosSimplify(const Rational &rhs); - static ArgumentPtr trigTableTanSimplify(const Rational &rhs); - - static ArgumentPtr trigTableCotSimplify(const Rational &rhs); - static std::tuple phaseShiftSin(const Rational &rhs); static std::tuple phaseShiftCos(const Rational &rhs); - static std::tuple phaseShiftTan(const Rational &rhs); - - static std::tuple phaseShiftCot(const Rational &rhs); - static Rational phaseShiftPeriod(const Rational &rhs); static std::shared_ptr getOppositeFunction(const IFunction &function);