simplify.cpp
1 #include <symengine/simplify.h>
2 #include <symengine/refine.h>
3 
4 namespace SymEngine
5 {
6 
7 void SimplifyVisitor::bvisit(const OneArgFunction &x)
8 {
9  auto farg = x.get_arg();
10  auto newarg = apply(farg);
11  result_ = x.create(newarg);
12 }
13 
14 void SimplifyVisitor::bvisit(const Pow &x)
15 {
16  auto e = apply(x.get_exp());
17  auto base = apply(x.get_base());
18  auto pair = simplify_pow(e, base);
19  result_ = pow(pair.second, pair.first);
20 }
21 
22 std::pair<RCP<const Basic>, RCP<const Basic>>
23 SimplifyVisitor::simplify_pow(const RCP<const Basic> &e,
24  const RCP<const Basic> &b)
25 {
26  if (is_a<Csc>(*b) and eq(*e, *minus_one)) {
27  // csc(expr) ** -1 = sin(expr)
28  return std::make_pair(
29  one, sin(down_cast<const OneArgFunction &>(*b).get_arg()));
30  } else if (is_a<Sec>(*b) and eq(*e, *minus_one)) {
31  // sec(expr) ** -1 = cos(expr)
32  return std::make_pair(
33  one, cos(down_cast<const OneArgFunction &>(*b).get_arg()));
34  } else if (is_a<Cot>(*b) and eq(*e, *minus_one)) {
35  // cot(expr) ** -1 = tan(expr)
36  return std::make_pair(
37  one, tan(down_cast<const OneArgFunction &>(*b).get_arg()));
38  } else {
39  return std::make_pair(e, b);
40  }
41 }
42 
43 void SimplifyVisitor::bvisit(const Mul &x)
44 {
45  map_basic_basic map;
46  for (const auto &p : x.get_dict()) {
47  auto base = apply(p.first);
48  auto newpair = simplify_pow(p.second, base);
49  Mul::dict_add_term(map, newpair.first, newpair.second);
50  }
51  result_ = Mul::from_dict(x.get_coef(), std::move(map));
52 }
53 
54 RCP<const Basic> simplify(const RCP<const Basic> &x,
55  const Assumptions *assumptions)
56 {
57  auto expr = refine(x, assumptions);
58  SimplifyVisitor b;
59  return b.apply(expr);
60 }
61 
62 } // namespace SymEngine
static void dict_add_term(map_basic_basic &d, const RCP< const Basic > &exp, const RCP< const Basic > &t)
Add terms to dict.
Definition: mul.cpp:150
static RCP< const Basic > from_dict(const RCP< const Number > &coef, map_basic_basic &&d)
Create a Mul from a dict.
Definition: mul.cpp:115
T make_pair(T... args)
T move(T... args)
Main namespace for SymEngine package.
Definition: add.cpp:19
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21
RCP< const Basic > tan(const RCP< const Basic > &arg)
Canonicalize Tan:
Definition: functions.cpp:1007
RCP< const Basic > cos(const RCP< const Basic > &arg)
Canonicalize Cos:
Definition: functions.cpp:942
RCP< const Basic > sin(const RCP< const Basic > &arg)
Canonicalize Sin:
Definition: functions.cpp:874
T pow(T... args)