Loading...
Searching...
No Matches
rewrite.cpp
1#include <symengine/visitor.h>
2#include <symengine/basic.h>
3
4namespace SymEngine
5{
6
7class RewriteAsExp : public BaseVisitor<RewriteAsExp, TransformVisitor>
8{
9public:
10 using TransformVisitor::bvisit;
11
13
14 void bvisit(const Sin &x)
15 {
16 auto farg = x.get_arg();
17 auto newarg = apply(farg);
18 auto expo = mul(I, newarg);
19 auto a = exp(expo);
20 auto b = exp(neg(expo));
21 result_ = div(sub(a, b), mul(integer(2), I));
22 }
23
24 void bvisit(const Cos &x)
25 {
26 auto farg = x.get_arg();
27 auto newarg = apply(farg);
28 auto expo = mul(I, newarg);
29 auto a = exp(expo);
30 auto b = exp(neg(expo));
31 result_ = div(add(a, b), integer(2));
32 }
33
34 void bvisit(const Tan &x)
35 {
36 auto farg = x.get_arg();
37 auto newarg = apply(farg);
38 auto expo = mul(I, newarg);
39 auto a = exp(expo);
40 auto b = exp(neg(expo));
41 result_ = div(sub(a, b), mul(I, add(a, b)));
42 }
43
44 void bvisit(const Cot &x)
45 {
46 auto farg = x.get_arg();
47 auto newarg = apply(farg);
48 auto expo = mul(I, newarg);
49 auto a = exp(expo);
50 auto b = exp(neg(expo));
51 result_ = div(mul(I, add(a, b)), sub(a, b));
52 }
53
54 void bvisit(const Csc &x)
55 {
56 auto farg = x.get_arg();
57 auto newarg = apply(farg);
58 auto expo = mul(I, newarg);
59 auto a = exp(expo);
60 auto b = exp(neg(expo));
61 result_ = div(mul(I, integer(2)), sub(a, b));
62 }
63
64 void bvisit(const Sec &x)
65 {
66 auto farg = x.get_arg();
67 auto newarg = apply(farg);
68 auto expo = mul(I, newarg);
69 auto a = exp(expo);
70 auto b = exp(neg(expo));
71 result_ = div(integer(2), add(a, b));
72 }
73
74 void bvisit(const Sinh &x)
75 {
76 auto farg = x.get_arg();
77 auto newarg = apply(farg);
78 result_ = div(sub(exp(newarg), exp(neg(newarg))), integer(2));
79 }
80
81 void bvisit(const Cosh &x)
82 {
83 auto farg = x.get_arg();
84 auto newarg = apply(farg);
85 result_ = div(add(exp(newarg), exp(neg(newarg))), integer(2));
86 }
87
88 void bvisit(const Tanh &x)
89 {
90 auto farg = x.get_arg();
91 auto newarg = apply(farg);
92 auto pos_exp = exp(newarg);
93 auto neg_exp = exp(neg(newarg));
94 result_ = div(sub(pos_exp, neg_exp), add(pos_exp, neg_exp));
95 }
96
97 void bvisit(const Csch &x)
98 {
99 auto farg = x.get_arg();
100 auto newarg = apply(farg);
101 auto pos_exp = exp(newarg);
102 auto neg_exp = exp(neg(newarg));
103 result_ = div(integer(2), sub(pos_exp, neg_exp));
104 }
105
106 void bvisit(const Sech &x)
107 {
108 auto farg = x.get_arg();
109 auto newarg = apply(farg);
110 auto pos_exp = exp(newarg);
111 auto neg_exp = exp(neg(newarg));
112 result_ = div(integer(2), add(pos_exp, neg_exp));
113 }
114
115 void bvisit(const Coth &x)
116 {
117 auto farg = x.get_arg();
118 auto newarg = apply(farg);
119 auto pos_exp = exp(newarg);
120 auto neg_exp = exp(neg(newarg));
121 result_ = div(add(pos_exp, neg_exp), sub(pos_exp, neg_exp));
122 }
123};
124
125RCP<const Basic> rewrite_as_exp(const RCP<const Basic> &x)
126{
127 RewriteAsExp b;
128 return b.apply(x);
129}
130
131class RewriteAsSin : public BaseVisitor<RewriteAsSin, TransformVisitor>
132{
133public:
134 using TransformVisitor::bvisit;
135
137
138 void bvisit(const Cos &x)
139 {
140 auto farg = x.get_arg();
141 auto newarg = apply(farg);
142 result_ = sin(unevaluated_expr(add(newarg, div(pi, integer(2)))));
143 }
144
145 void bvisit(const Tan &x)
146 {
147 auto farg = x.get_arg();
148 auto newarg = apply(farg);
149 result_ = div(mul(integer(2), pow(sin(newarg), integer(2))),
150 sin(mul(integer(2), newarg)));
151 }
152
153 void bvisit(const Cot &x)
154 {
155 auto farg = x.get_arg();
156 auto newarg = apply(farg);
157 result_ = div(sin(mul(integer(2), newarg)),
158 mul(integer(2), pow(sin(newarg), integer(2))));
159 }
160
161 void bvisit(const Csc &x)
162 {
163 auto farg = x.get_arg();
164 auto newarg = apply(farg);
165 result_ = div(integer(1), sin(newarg));
166 }
167
168 void bvisit(const Sec &x)
169 {
170 auto farg = x.get_arg();
171 auto newarg = apply(farg);
172 result_ = div(integer(1),
173 sin(unevaluated_expr(add(newarg, div(pi, integer(2))))));
174 }
175};
176
177RCP<const Basic> rewrite_as_sin(const RCP<const Basic> &x)
178{
179 RewriteAsSin b;
180 return b.apply(x);
181}
182
183class RewriteAsCos : public BaseVisitor<RewriteAsCos, TransformVisitor>
184{
185public:
186 using TransformVisitor::bvisit;
187
189
190 void bvisit(const Sin &x)
191 {
192 auto farg = x.get_arg();
193 auto newarg = apply(farg);
194 result_ = cos(unevaluated_expr(sub(newarg, div(pi, integer(2)))));
195 }
196
197 void bvisit(const Tan &x)
198 {
199 auto farg = x.get_arg();
200 auto newarg = apply(farg);
201 result_ = div(cos(unevaluated_expr(sub(newarg, div(pi, integer(2))))),
202 cos(newarg));
203 }
204
205 void bvisit(const Cot &x)
206 {
207 auto farg = x.get_arg();
208 auto newarg = apply(farg);
209 result_ = div(cos(newarg),
210 cos(unevaluated_expr(sub(newarg, div(pi, integer(2))))));
211 }
212
213 void bvisit(const Csc &x)
214 {
215 auto farg = x.get_arg();
216 auto newarg = apply(farg);
217 result_ = div(integer(1),
218 cos(unevaluated_expr(sub(newarg, div(pi, integer(2))))));
219 }
220
221 void bvisit(const Sec &x)
222 {
223 auto farg = x.get_arg();
224 auto newarg = apply(farg);
225 result_ = div(integer(1), cos(newarg));
226 }
227};
228
229RCP<const Basic> rewrite_as_cos(const RCP<const Basic> &x)
230{
231 RewriteAsCos b;
232 return b.apply(x);
233}
234
235} // namespace SymEngine
The base class for SymEngine.
RCP< const Basic > get_arg() const
Definition functions.h:36
Main namespace for SymEngine package.
Definition add.cpp:19
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
Definition mul.cpp:431
void hash_combine(hash_t &seed, const T &v)
Definition basic-inl.h:95
RCP< const Basic > sub(const RCP< const Basic > &a, const RCP< const Basic > &b)
Substracts b from a.
Definition add.cpp:495
RCP< const Basic > exp(const RCP< const Basic > &x)
Returns the natural exponential function E**x = pow(E, x)
Definition pow.cpp:271
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
Definition mul.cpp:352
RCP< const Basic > cos(const RCP< const Basic > &arg)
Canonicalize Cos:
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
Definition add.cpp:425
std::enable_if< std::is_integral< T >::value, RCP< constInteger > >::type integer(T i)
Definition integer.h:197
RCP< const Basic > neg(const RCP< const Basic > &a)
Negation.
Definition mul.cpp:443
RCP< const Basic > sin(const RCP< const Basic > &arg)
Canonicalize Sin: