Skip to content

Commit

Permalink
add expand_as_exp() for trignometric functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjithkumar007 committed Jul 6, 2017
1 parent 9e536b6 commit 9e02473
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
48 changes: 48 additions & 0 deletions symengine/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,14 @@ bool Sin::is_canonical(const RCP<const Basic> &arg) const
return true;
}

RCP<const Basic> Sin::expand_as_exp() const
{
auto expo = mul(I, get_arg());
RCP<const Basic> a = exp(expo);
RCP<const Basic> b = exp(mul(minus_one, expo));
return div(sub(a, b), mul(integer(2), I));
}

RCP<const Basic> sin(const RCP<const Basic> &arg)
{
if (eq(*arg, *zero))
Expand Down Expand Up @@ -783,6 +791,14 @@ bool Cos::is_canonical(const RCP<const Basic> &arg) const
return true;
}

RCP<const Basic> Cos::expand_as_exp() const
{
auto expo = mul(I, get_arg());
RCP<const Basic> a = exp(expo);
RCP<const Basic> b = exp(mul(minus_one, expo));
return div(add(a, b), integer(2));
}

RCP<const Basic> cos(const RCP<const Basic> &arg)
{
if (eq(*arg, *zero))
Expand Down Expand Up @@ -848,6 +864,14 @@ bool Tan::is_canonical(const RCP<const Basic> &arg) const
return true;
}

RCP<const Basic> Tan::expand_as_exp() const
{
auto expo = mul(I, get_arg());
RCP<const Basic> a = exp(expo);
RCP<const Basic> b = exp(mul(minus_one, expo));
return div(sub(a, b), mul(I, add(a, b)));
}

RCP<const Basic> tan(const RCP<const Basic> &arg)
{
if (eq(*arg, *zero))
Expand Down Expand Up @@ -914,6 +938,14 @@ bool Cot::is_canonical(const RCP<const Basic> &arg) const
return true;
}

RCP<const Basic> Cot::expand_as_exp() const
{
auto expo = mul(I, get_arg());
RCP<const Basic> a = exp(expo);
RCP<const Basic> b = exp(mul(minus_one, expo));
return div(mul(I, add(a, b)), sub(a, b));
}

RCP<const Basic> cot(const RCP<const Basic> &arg)
{
if (is_a_Number(*arg) and not down_cast<const Number &>(*arg).is_exact()) {
Expand Down Expand Up @@ -979,6 +1011,14 @@ bool Csc::is_canonical(const RCP<const Basic> &arg) const
return true;
}

RCP<const Basic> Csc::expand_as_exp() const
{
auto expo = mul(I, get_arg());
RCP<const Basic> a = exp(expo);
RCP<const Basic> b = exp(mul(minus_one, expo));
return div(mul(I, integer(2)), sub(a, b));
}

RCP<const Basic> csc(const RCP<const Basic> &arg)
{
if (is_a_Number(*arg) and not down_cast<const Number &>(*arg).is_exact()) {
Expand Down Expand Up @@ -1043,6 +1083,14 @@ bool Sec::is_canonical(const RCP<const Basic> &arg) const
return true;
}

RCP<const Basic> Sec::expand_as_exp() const
{
auto expo = mul(I, get_arg());
RCP<const Basic> a = exp(expo);
RCP<const Basic> b = exp(mul(minus_one, expo));
return div(integer(2), add(a, b));
}

RCP<const Basic> sec(const RCP<const Basic> &arg)
{
if (is_a_Number(*arg) and not down_cast<const Number &>(*arg).is_exact()) {
Expand Down
12 changes: 12 additions & 0 deletions symengine/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ class Sin : public TrigFunction
bool is_canonical(const RCP<const Basic> &arg) const;
//! \return Canonicalized sin
virtual RCP<const Basic> create(const RCP<const Basic> &arg) const;
//! expands sin in terms of exp function
virtual RCP<const Basic> expand_as_exp() const;
};

//! Canonicalize Sin:
Expand All @@ -305,6 +307,8 @@ class Cos : public TrigFunction
bool is_canonical(const RCP<const Basic> &arg) const;
//! \return Canonicalized cos
virtual RCP<const Basic> create(const RCP<const Basic> &arg) const;
//! expands cos in terms of exp function
virtual RCP<const Basic> expand_as_exp() const;
};

//! Canonicalize Cos:
Expand All @@ -321,6 +325,8 @@ class Tan : public TrigFunction
bool is_canonical(const RCP<const Basic> &arg) const;
//! \return Canonicalized tan
virtual RCP<const Basic> create(const RCP<const Basic> &arg) const;
//! expands tan in terms of exp function
virtual RCP<const Basic> expand_as_exp() const;
};
//! Canonicalize Tan:
RCP<const Basic> tan(const RCP<const Basic> &arg);
Expand All @@ -336,6 +342,8 @@ class Cot : public TrigFunction
bool is_canonical(const RCP<const Basic> &arg) const;
//! \return Canonicalized cot
virtual RCP<const Basic> create(const RCP<const Basic> &arg) const;
//! expands cot in terms of exp function
virtual RCP<const Basic> expand_as_exp() const;
};
//! Canonicalize Cot:
RCP<const Basic> cot(const RCP<const Basic> &arg);
Expand All @@ -351,6 +359,8 @@ class Csc : public TrigFunction
bool is_canonical(const RCP<const Basic> &arg) const;
//! \return Canonicalized csc
virtual RCP<const Basic> create(const RCP<const Basic> &arg) const;
//! expands csc in terms of exp function
virtual RCP<const Basic> expand_as_exp() const;
};
//! Canonicalize Csc:
RCP<const Basic> csc(const RCP<const Basic> &arg);
Expand All @@ -366,6 +376,8 @@ class Sec : public TrigFunction
bool is_canonical(const RCP<const Basic> &arg) const;
//! \return Canonicalized sec
virtual RCP<const Basic> create(const RCP<const Basic> &arg) const;
//! expands sec in terms of exp function
virtual RCP<const Basic> expand_as_exp() const;
};
//! Canonicalize Sec:
RCP<const Basic> sec(const RCP<const Basic> &arg);
Expand Down
26 changes: 26 additions & 0 deletions symengine/tests/basic/test_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ TEST_CASE("Sin: functions", "[functions]")
REQUIRE(not(r4->is_canonical(mul(pi, i2))));
REQUIRE(not(r4->is_canonical(add(mul(pi, i2), div(pi, i2)))));
REQUIRE(not(r4->is_canonical(real_double(2.0))));

r1 = sin(x)->expand_as_exp();
r2 = div(sub(exp(mul(I, x)), exp(mul(neg(I), x))), mul(integer(2), I));
REQUIRE(eq(*r1, *r2));
}

TEST_CASE("Cos: functions", "[functions]")
Expand Down Expand Up @@ -436,6 +440,10 @@ TEST_CASE("Cos: functions", "[functions]")
REQUIRE(not(r4->is_canonical(mul(pi, i2))));
REQUIRE(not(r4->is_canonical(add(mul(pi, i2), div(pi, i2)))));
REQUIRE(not(r4->is_canonical(real_double(2.0))));

r1 = cos(x)->expand_as_exp();
r2 = div(add(exp(mul(I, x)), exp(mul(neg(I), x))), integer(2));
REQUIRE(eq(*r1, *r2));
}

TEST_CASE("Tan: functions", "[functions]")
Expand Down Expand Up @@ -557,6 +565,11 @@ TEST_CASE("Tan: functions", "[functions]")
REQUIRE(not(r4->is_canonical(mul(pi, i2))));
REQUIRE(not(r4->is_canonical(add(mul(pi, i2), div(pi, i2)))));
REQUIRE(not(r4->is_canonical(real_double(2.0))));

r1 = tan(x)->expand_as_exp();
r2 = div(sub(exp(mul(I, x)), exp(mul(neg(I), x))),
mul(add(exp(mul(I, x)), exp(mul(neg(I), x))), I));
REQUIRE(eq(*r1, *r2));
}

TEST_CASE("Cot: functions", "[functions]")
Expand Down Expand Up @@ -674,6 +687,11 @@ TEST_CASE("Cot: functions", "[functions]")
REQUIRE(not(r4->is_canonical(mul(pi, i2))));
REQUIRE(not(r4->is_canonical(add(mul(pi, i2), div(pi, i2)))));
REQUIRE(not(r4->is_canonical(real_double(2.0))));

r1 = cot(x)->expand_as_exp();
r2 = div(mul(add(exp(mul(I, x)), exp(mul(neg(I), x))), I),
sub(exp(mul(I, x)), exp(mul(neg(I), x))));
REQUIRE(eq(*r1, *r2));
}

TEST_CASE("Csc: functions", "[functions]")
Expand Down Expand Up @@ -793,6 +811,10 @@ TEST_CASE("Csc: functions", "[functions]")
REQUIRE(not(r4->is_canonical(mul(pi, i2))));
REQUIRE(not(r4->is_canonical(add(mul(pi, i2), div(pi, i2)))));
REQUIRE(not(r4->is_canonical(real_double(2.0))));

r1 = csc(x)->expand_as_exp();
r2 = div(mul(integer(2), I), sub(exp(mul(I, x)), exp(mul(neg(I), x))));
REQUIRE(eq(*r1, *r2));
}

TEST_CASE("Sec: functions", "[functions]")
Expand Down Expand Up @@ -914,6 +936,10 @@ TEST_CASE("Sec: functions", "[functions]")
REQUIRE(not(r4->is_canonical(mul(pi, i2))));
REQUIRE(not(r4->is_canonical(add(mul(pi, i2), div(pi, i2)))));
REQUIRE(not(r4->is_canonical(real_double(2.0))));

r1 = sec(x)->expand_as_exp();
r2 = div(integer(2), add(exp(mul(I, x)), exp(mul(neg(I), x))));
REQUIRE(eq(*r1, *r2));
}

TEST_CASE("TrigFunction: trig_to_sqrt", "[functions]")
Expand Down

0 comments on commit 9e02473

Please sign in to comment.