Skip to content

Commit

Permalink
Sin constants simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Aug 9, 2023
1 parent e36f813 commit a4c1ffc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
30 changes: 29 additions & 1 deletion src/fintamath/expressions/unary/TrigExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,35 @@ ArgumentPtr TrigExpression::trigTableSimplify(const IFunction &func, const Ratio
return trigTable.at(func.toString())(rhs);
}

ArgumentPtr TrigExpression::trigTableSinSimplify(Rational /*rhs*/) {
ArgumentPtr TrigExpression::trigTableSinSimplify(Rational rhs) {
static const TrigonometryTable trigTable = {
{Rational(0), Integer(0).clone()}, // 0 | 0
{Rational(1, 6), Rational(1, 2).clone()}, // π/6 | 1/2
{Rational(1, 4), makeExpr(Div(), *makeExpr(Sqrt(), Integer(2)), Integer(2))}, // π/4 | √2/2
{Rational(1, 3), makeExpr(Div(), *makeExpr(Sqrt(), Integer(3)), Integer(2))}, // π/3 | √3/2
{Rational(1, 2), Integer(1).clone()}, // π/2 | 1
{Rational(2, 3), makeExpr(Div(), *makeExpr(Sqrt(), Integer(3)), Integer(2))}, // 2π/3 | √3/2
{Rational(3, 4), makeExpr(Div(), *makeExpr(Sqrt(), Integer(2)), Integer(2))}, // 3π/4 | √2/2
{Rational(5, 6), Rational(1, 2).clone()}, // 5π/6 | 1/2
{Rational(1), Integer(0).clone()}, // π | 0
};

bool isResNegated = false;

if (rhs < 0) {
rhs = -rhs;
isResNegated = !isResNegated;
}

if (rhs.numerator() > rhs.denominator()) {
rhs = Rational(rhs.numerator() % rhs.denominator(), rhs.denominator());
isResNegated = !isResNegated;
}

if (auto res = trigTable.find(rhs); res != trigTable.end()) {
return isResNegated ? makeExpr(Neg(), res->second) : res->second;
}

return {};
}

Expand Down
4 changes: 2 additions & 2 deletions tests/src/expressions/ExpressionFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ TEST(ExpressionFunctionsTests, lgTest) {

TEST(ExpressionFunctionsTests, sinTest) {
// TODO trigonometry
EXPECT_EQ(sin(Expression("5*Pi")).toString(), "sin(5 Pi)");
EXPECT_EQ(sin(Expression("Pi/2")).toString(), "sin(Pi/2)");
EXPECT_EQ(sin(Expression("5*Pi")).toString(), "0");
EXPECT_EQ(sin(Expression("Pi/2")).toString(), "1");
EXPECT_EQ(sin(Expression("a+b")).toString(), "sin(a + b)");
}

Expand Down
43 changes: 41 additions & 2 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("sin1^2").toString(), "sin(1)^2");
EXPECT_EQ(Expression("sin(10^30)").toString(), "sin(1000000000000000000000000000000)");
EXPECT_EQ(Expression("sin(1)^2+cos(1)^2").toString(), "cos(1)^2 + sin(1)^2");
EXPECT_EQ(Expression("sin(Pi/3)").toString(), "sin(Pi/3)");
EXPECT_EQ(Expression("cos(Pi/3)").toString(), "cos(Pi/3)");
EXPECT_EQ(Expression("2!*E").toString(), "2 E");
EXPECT_EQ(Expression("E*2!").toString(), "2 E");
EXPECT_EQ(Expression("sqrt((1-cos(2*(Pi/3)))/2)").toString(), "sqrt(-cos((2 Pi)/3)/2 + 1/2)");
Expand Down Expand Up @@ -684,6 +682,47 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("atanh(tanh(x))").toString(), "atanh(tanh(x))");
EXPECT_EQ(Expression("acoth(coth(x))").toString(), "acoth(coth(x))");

EXPECT_EQ(Expression("sin(-13Pi/6)").toString(), "-1/2");
EXPECT_EQ(Expression("sin(-2Pi)").toString(), "0");
EXPECT_EQ(Expression("sin(-11Pi/6)").toString(), "1/2");
EXPECT_EQ(Expression("sin(-7Pi/4)").toString(), "sqrt(2)/2");
EXPECT_EQ(Expression("sin(-5Pi/3)").toString(), "sqrt(3)/2");
EXPECT_EQ(Expression("sin(-3Pi/2)").toString(), "1");
EXPECT_EQ(Expression("sin(-4Pi/3)").toString(), "sqrt(3)/2");
EXPECT_EQ(Expression("sin(-5Pi/4)").toString(), "sqrt(2)/2");
EXPECT_EQ(Expression("sin(-7Pi/6)").toString(), "1/2");
EXPECT_EQ(Expression("sin(-Pi)").toString(), "0");
EXPECT_EQ(Expression("sin(-5Pi/6)").toString(), "-1/2");
EXPECT_EQ(Expression("sin(-3Pi/4)").toString(), "-sqrt(2)/2");
EXPECT_EQ(Expression("sin(-2Pi/3)").toString(), "-sqrt(3)/2");
EXPECT_EQ(Expression("sin(-Pi/2)").toString(), "-1");
EXPECT_EQ(Expression("sin(-Pi/3)").toString(), "-sqrt(3)/2");
EXPECT_EQ(Expression("sin(-Pi/4)").toString(), "-sqrt(2)/2");
EXPECT_EQ(Expression("sin(-Pi/6)").toString(), "-1/2");
EXPECT_EQ(Expression("sin(0)").toString(), "0");
EXPECT_EQ(Expression("sin(Pi/6)").toString(), "1/2");
EXPECT_EQ(Expression("sin(Pi/4)").toString(), "sqrt(2)/2");
EXPECT_EQ(Expression("sin(Pi/3)").toString(), "sqrt(3)/2");
EXPECT_EQ(Expression("sin(Pi/2)").toString(), "1");
EXPECT_EQ(Expression("sin(2Pi/3)").toString(), "sqrt(3)/2");
EXPECT_EQ(Expression("sin(3Pi/4)").toString(), "sqrt(2)/2");
EXPECT_EQ(Expression("sin(5Pi/6)").toString(), "1/2");
EXPECT_EQ(Expression("sin(Pi)").toString(), "0");
EXPECT_EQ(Expression("sin(7Pi/6)").toString(), "-1/2");
EXPECT_EQ(Expression("sin(5Pi/4)").toString(), "-sqrt(2)/2");
EXPECT_EQ(Expression("sin(4Pi/3)").toString(), "-sqrt(3)/2");
EXPECT_EQ(Expression("sin(3Pi/2)").toString(), "-1");
EXPECT_EQ(Expression("sin(5Pi/3)").toString(), "-sqrt(3)/2");
EXPECT_EQ(Expression("sin(7Pi/4)").toString(), "-sqrt(2)/2");
EXPECT_EQ(Expression("sin(11Pi/6)").toString(), "-1/2");
EXPECT_EQ(Expression("sin(2Pi)").toString(), "0");
EXPECT_EQ(Expression("sin(13Pi/6)").toString(), "1/2");

EXPECT_EQ(Expression("sin(E Pi)").toString(), "sin(E Pi)");
EXPECT_EQ(Expression("cos(E Pi)").toString(), "cos(E Pi)");
EXPECT_EQ(Expression("tan(E Pi)").toString(), "tan(E Pi)");
EXPECT_EQ(Expression("cot(E Pi)").toString(), "cot(E Pi)");

// EXPECT_EQ(Expression("sin(x)^2 + cos(x)^2").toString(), "1"); // TODO: implement
// EXPECT_EQ(Expression("tan(x)*cot(x)").toString(), "1"); // TODO: implement
// EXPECT_EQ(Expression("tanh(x)*coth(x)").toString(), "1"); // TODO: implement
Expand Down

0 comments on commit a4c1ffc

Please sign in to comment.