refine.cpp
1 #include <symengine/refine.h>
2 
3 namespace SymEngine
4 {
5 
6 void RefineVisitor::bvisit(const Abs &x)
7 {
8  auto farg = x.get_arg();
9  auto newarg = apply(farg);
10  if (is_true(is_nonnegative(*newarg, assumptions_))) {
11  result_ = newarg;
12  } else if (is_true(is_nonpositive(*newarg, assumptions_))) {
13  result_ = neg(newarg);
14  } else if (is_a<Conjugate>(*newarg)) {
15  result_ = abs(down_cast<const Conjugate &>(*newarg).get_arg());
16  } else {
17  result_ = abs(newarg);
18  }
19 }
20 
21 void RefineVisitor::bvisit(const Sign &x)
22 {
23  auto farg = x.get_arg();
24  auto newarg = apply(farg);
25  if (is_true(is_positive(*newarg, assumptions_))) {
26  result_ = integer(1);
27  } else if (is_true(is_negative(*newarg, assumptions_))) {
28  result_ = integer(-1);
29  } else if (is_true(is_zero(*newarg, assumptions_))) {
30  result_ = integer(0);
31  } else {
32  result_ = sign(newarg);
33  }
34 }
35 
36 void RefineVisitor::bvisit(const Floor &x)
37 {
38  auto farg = x.get_arg();
39  auto newarg = apply(farg);
40  if (is_true(is_integer(*newarg, assumptions_))) {
41  result_ = newarg;
42  } else if (could_extract_minus(*newarg)) {
43  result_ = neg(ceiling(neg(newarg)));
44  } else {
45  result_ = floor(newarg);
46  }
47 }
48 
49 void RefineVisitor::bvisit(const Ceiling &x)
50 {
51  auto farg = x.get_arg();
52  auto newarg = apply(farg);
53  if (is_true(is_integer(*newarg, assumptions_))) {
54  result_ = newarg;
55  } else if (could_extract_minus(*newarg)) {
56  result_ = neg(floor(neg(newarg)));
57  } else {
58  result_ = ceiling(newarg);
59  }
60 }
61 
62 void RefineVisitor::bvisit(const Conjugate &x)
63 {
64  auto farg = x.get_arg();
65  auto newarg = apply(farg);
66  if (is_true(is_real(*newarg, assumptions_))) {
67  result_ = newarg;
68  } else {
69  result_ = conjugate(newarg);
70  }
71 }
72 
73 RCP<const Basic> refine(const RCP<const Basic> &x,
74  const Assumptions *assumptions)
75 {
76  RefineVisitor b(assumptions);
77  return b.apply(x);
78 }
79 
80 } // namespace SymEngine
Main namespace for SymEngine package.
Definition: add.cpp:19
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
Definition: integer.h:200
RCP< const Basic > sign(const RCP< const Basic > &arg)
Canonicalize Sign.
Definition: functions.cpp:484
RCP< const Basic > ceiling(const RCP< const Basic > &arg)
Canonicalize Ceiling:
Definition: functions.cpp:662
RCP< const Basic > abs(const RCP< const Basic > &arg)
Canonicalize Abs:
Definition: functions.cpp:3449
bool could_extract_minus(const Basic &arg)
Definition: functions.cpp:282
RCP< const Basic > floor(const RCP< const Basic > &arg)
Canonicalize Floor:
Definition: functions.cpp:568
tribool is_zero(const Basic &b, const Assumptions *assumptions=nullptr)
Check if a number is zero.
RCP< const Basic > neg(const RCP< const Basic > &a)
Negation.
Definition: mul.cpp:449
RCP< const Basic > conjugate(const RCP< const Basic > &arg)
Canonicalize Conjugate.
Definition: functions.cpp:106