series_piranha.cpp
1 #include <symengine/series_visitor.h>
2 
3 namespace SymEngine
4 {
5 
6 URatPSeriesPiranha::URatPSeriesPiranha(pp_t p, const std::string varname,
7  const unsigned degree)
8  : SeriesBase(std::move(p), varname, degree){SYMENGINE_ASSIGN_TYPEID()} RCP<
9  const URatPSeriesPiranha> URatPSeriesPiranha::
10  series(const RCP<const Basic> &t, const std::string &x,
11  unsigned int prec)
12 {
13  SeriesVisitor<pp_t, piranha::rational, URatPSeriesPiranha> visitor(pp_t(x),
14  x, prec);
15  return visitor.series(t);
16 }
17 
18 hash_t URatPSeriesPiranha::__hash__() const
19 {
20  hash_t seed = SYMENGINE_URATPSERIESPIRANHA;
21  hash_combine(seed, p_.hash());
22  hash_combine(seed, var_);
23  hash_combine(seed, degree_);
24  return seed;
25 }
26 
27 int URatPSeriesPiranha::compare(const Basic &o) const
28 {
29  SYMENGINE_ASSERT(is_a<URatPSeriesPiranha>(o))
30  const URatPSeriesPiranha &s = down_cast<const URatPSeriesPiranha &>(o);
31  if (var_ != s.var_)
32  return (var_ < s.var_) ? -1 : 1;
33  if (degree_ != s.degree_)
34  return (degree_ < s.degree_) ? -1 : 1;
35  if (p_ == s.p_)
36  return 0;
37  return (p_.hash() < s.p_.hash()) ? -1 : 1;
38 }
39 
40 piranha::integer URatPSeriesPiranha::convert(const Integer &x)
41 {
42  return piranha::integer(get_mpz_t(x.as_integer_class()));
43 }
44 
45 piranha::rational URatPSeriesPiranha::convert(const rational_class &x)
46 {
47  piranha::integer i1(get_mpz_t(get_num(x)));
48  piranha::integer i2(get_mpz_t(get_den(x)));
49  piranha::rational r(i1);
50  r /= i2;
51  return r;
52 }
53 
54 pp_t URatPSeriesPiranha::var(const std::string &s)
55 {
56  return pp_t(s);
57 }
58 
59 piranha::rational URatPSeriesPiranha::convert(const Rational &x)
60 {
61  return convert(x.as_rational_class());
62 }
63 
64 piranha::rational URatPSeriesPiranha::convert(const Basic &x)
65 {
66  throw NotImplementedError("Not Implemented");
67 }
68 
69 RCP<const Basic> URatPSeriesPiranha::as_basic() const
70 {
71  RCP<const Symbol> x = symbol(var_);
72  RCP<const Number> zcoef = integer(0);
73  umap_basic_num dict_;
74  for (const auto &it : p_) {
75  if (it.first != 0) {
76  rational_class cl_rat(it.first.get_mpq_view());
77  canonicalize(cl_rat);
78  RCP<const Number> co_basic = Rational::from_mpq(std::move(cl_rat));
79  auto term = SymEngine::mul(
80  SymEngine::pow(x, SymEngine::integer(it.second.degree())),
81  co_basic);
82  if (it.second.degree() == 0)
83  zcoef = co_basic;
84  Add::coef_dict_add_term(outArg(co_basic), dict_, one, term);
85  }
86  }
87  return Add::from_dict(zcoef, std::move(dict_));
88 }
89 
90 umap_int_basic URatPSeriesPiranha::as_dict() const
91 {
92  umap_int_basic map;
93  for (const auto &it : p_) {
94  if (it.first != 0) {
95  rational_class cl_rat(it.first.get_mpq_view());
96  RCP<const Basic> basic = Rational::from_mpq(std::move(cl_rat));
97  map[it.second.degree()] = basic;
98  }
99  }
100  return map;
101 }
102 
103 RCP<const Basic> URatPSeriesPiranha::get_coeff(int i) const
104 {
105  rational_class cl_rat(p_.find_cf({i}).get_mpq_view());
106  return Rational::from_mpq(std::move(cl_rat));
107 }
108 
109 pp_t URatPSeriesPiranha::mul(const pp_t &s, const pp_t &r, unsigned prec)
110 {
111  pp_t::set_auto_truncate_degree(prec - 1);
112  pp_t ret = s * r;
113  pp_t::unset_auto_truncate_degree();
114  return ret;
115 }
116 
117 pp_t URatPSeriesPiranha::pow(const pp_t &s, int n, unsigned prec)
118 {
119  pp_t::set_auto_truncate_degree(prec - 1);
120  pp_t::clear_pow_cache();
121  pp_t ret = s.pow(n);
122  pp_t::unset_auto_truncate_degree();
123  return ret;
124 }
125 
126 unsigned URatPSeriesPiranha::ldegree(const pp_t &s)
127 {
128  return s.ldegree();
129 }
130 
131 piranha::rational URatPSeriesPiranha::find_cf(const pp_t &s, const pp_t &var,
132  unsigned deg)
133 {
134  return s.find_cf({deg});
135 }
136 
137 piranha::rational URatPSeriesPiranha::root(piranha::rational &c, unsigned n)
138 {
139  rational_class cl_rat(c.get_mpq_view());
140  bool res;
141  if (get_den(cl_rat) == 1) {
142  // integer constant
143  rational_class cl_root;
144  res = mp_root(get_num(cl_root), get_num(cl_rat), n);
145  if (not res)
146  throw SymEngineException("constant term is not an nth power");
147  return convert(cl_root);
148  } else {
149  RCP<const Rational> cterm = make_rcp<const Rational>(std::move(cl_rat));
150  RCP<const Number> cout;
151  res = cterm->nth_root(outArg(cout), n);
152  if (not res)
153  throw SymEngineException("constant term is not an nth power");
154  return convert(down_cast<const Rational &>(*cout).as_rational_class());
155  }
156 }
157 
158 pp_t URatPSeriesPiranha::diff(const pp_t &s, const pp_t &var)
159 {
160  return s.partial(var.get_symbol_set()[0].get_name());
161 }
162 
163 pp_t URatPSeriesPiranha::integrate(const pp_t &s, const pp_t &var)
164 {
165  return s.integrate(var.get_symbol_set()[0].get_name());
166 }
167 
168 pp_t URatPSeriesPiranha::subs(const pp_t &s, const pp_t &var, const pp_t &r,
169  unsigned prec)
170 {
171  pp_t::set_auto_truncate_degree(prec - 1);
172  pp_t ret = s.subs(var.get_symbol_set()[0].get_name(), r);
173  pp_t::unset_auto_truncate_degree();
174  return ret;
175 }
176 
177 UPSeriesPiranha::UPSeriesPiranha(p_expr p, const std::string varname,
178  const unsigned degree)
179  : SeriesBase(std::move(p), varname, degree){SYMENGINE_ASSIGN_TYPEID()}
180 
181  RCP<const UPSeriesPiranha> UPSeriesPiranha::series(
182  const RCP<const Basic> &t, const std::string &x, unsigned int prec)
183 {
184  SeriesVisitor<p_expr, Expression, UPSeriesPiranha> visitor(p_expr(x), x,
185  prec);
186  return visitor.series(t);
187 }
188 
189 hash_t UPSeriesPiranha::__hash__() const
190 {
191  hash_t seed = SYMENGINE_URATPSERIESPIRANHA;
192  hash_combine(seed, p_.hash());
193  hash_combine(seed, var_);
194  hash_combine(seed, degree_);
195  return seed;
196 }
197 
198 int UPSeriesPiranha::compare(const Basic &o) const
199 {
200  SYMENGINE_ASSERT(is_a<UPSeriesPiranha>(o))
201  const UPSeriesPiranha &s = down_cast<const UPSeriesPiranha &>(o);
202  if (var_ != s.var_)
203  return (var_ < s.var_) ? -1 : 1;
204  if (degree_ != s.degree_)
205  return (degree_ < s.degree_) ? -1 : 1;
206  if (p_ == s.p_)
207  return 0;
208  return (p_.hash() < s.p_.hash()) ? -1 : 1;
209 }
210 
211 p_expr UPSeriesPiranha::var(const std::string &s)
212 {
213  return p_expr(s);
214 }
215 
216 Expression UPSeriesPiranha::convert(const Basic &x)
217 {
218  return Expression(x.rcp_from_this());
219 }
220 
221 RCP<const Basic> UPSeriesPiranha::as_basic() const
222 {
223  RCP<const Symbol> x = symbol(var_);
224  umap_basic_num dict_;
225  for (const auto &it : p_) {
226  if (it.first != 0) {
227  auto term = SymEngine::mul(
228  it.first.get_basic(),
229  SymEngine::pow(x, SymEngine::integer(it.second.degree())));
230  RCP<const Number> coef;
231  coef = zero;
232  Add::coef_dict_add_term(outArg(coef), dict_, one, term);
233  }
234  }
235  return Add::from_dict(one, std::move(dict_));
236 }
237 
238 umap_int_basic UPSeriesPiranha::as_dict() const
239 {
240  umap_int_basic map;
241  for (const auto &it : p_) {
242  if (it.first != 0) {
243  map[it.second.degree()] = it.first.get_basic();
244  }
245  }
246  return map;
247 }
248 
249 RCP<const Basic> UPSeriesPiranha::get_coeff(int i) const
250 {
251  return p_.find_cf({i}).get_basic();
252 }
253 
254 p_expr UPSeriesPiranha::mul(const p_expr &s, const p_expr &r, unsigned prec)
255 {
256  p_expr::set_auto_truncate_degree(prec - 1);
257  p_expr ret = s * r;
258  p_expr::unset_auto_truncate_degree();
259  return ret;
260 }
261 
262 p_expr UPSeriesPiranha::pow(const p_expr &s, int n, unsigned prec)
263 {
264  p_expr::set_auto_truncate_degree(prec - 1);
265  p_expr::clear_pow_cache();
266  p_expr ret = s.pow(n);
267  p_expr::unset_auto_truncate_degree();
268  return ret;
269 }
270 
271 unsigned UPSeriesPiranha::ldegree(const p_expr &s)
272 {
273  return s.ldegree();
274 }
275 
276 Expression UPSeriesPiranha::find_cf(const p_expr &s, const p_expr &var,
277  unsigned deg)
278 {
279  return s.find_cf({deg});
280 }
281 
282 Expression UPSeriesPiranha::root(Expression &c, unsigned n)
283 {
284  return SymEngine::pow(c, 1 / Expression(n));
285 }
286 
287 p_expr UPSeriesPiranha::diff(const p_expr &s, const p_expr &var)
288 {
289  return s.partial(var.get_symbol_set()[0].get_name());
290 }
291 
292 p_expr UPSeriesPiranha::integrate(const p_expr &s, const p_expr &var)
293 {
294  return s.integrate(var.get_symbol_set()[0].get_name());
295 }
296 
297 p_expr UPSeriesPiranha::subs(const p_expr &s, const p_expr &var,
298  const p_expr &r, unsigned prec)
299 {
300  p_expr::set_auto_truncate_degree(prec - 1);
301  p_expr ret = s.subs(var.get_symbol_set()[0].get_name(), r);
302  p_expr::unset_auto_truncate_degree();
303  return ret;
304 }
305 
306 Expression UPSeriesPiranha::sin(const Expression &c)
307 {
308  return SymEngine::sin(c.get_basic());
309 }
310 
311 Expression UPSeriesPiranha::cos(const Expression &c)
312 {
313  return SymEngine::cos(c.get_basic());
314 }
315 
316 Expression UPSeriesPiranha::tan(const Expression &c)
317 {
318  return SymEngine::tan(c.get_basic());
319 }
320 
321 Expression UPSeriesPiranha::asin(const Expression &c)
322 {
323  return SymEngine::asin(c.get_basic());
324 }
325 
326 Expression UPSeriesPiranha::acos(const Expression &c)
327 {
328  return SymEngine::acos(c.get_basic());
329 }
330 
331 Expression UPSeriesPiranha::atan(const Expression &c)
332 {
333  return SymEngine::atan(c.get_basic());
334 }
335 
336 Expression UPSeriesPiranha::sinh(const Expression &c)
337 {
338  return SymEngine::sinh(c.get_basic());
339 }
340 
341 Expression UPSeriesPiranha::cosh(const Expression &c)
342 {
343  return SymEngine::cosh(c.get_basic());
344 }
345 
346 Expression UPSeriesPiranha::tanh(const Expression &c)
347 {
348  return SymEngine::tanh(c.get_basic());
349 }
350 
351 Expression UPSeriesPiranha::asinh(const Expression &c)
352 {
353  return SymEngine::asinh(c.get_basic());
354 }
355 
356 Expression UPSeriesPiranha::atanh(const Expression &c)
357 {
358  return SymEngine::atanh(c.get_basic());
359 }
360 
361 Expression UPSeriesPiranha::exp(const Expression &c)
362 {
363  return SymEngine::exp(c.get_basic());
364 }
365 
366 Expression UPSeriesPiranha::log(const Expression &c)
367 {
368  return SymEngine::log(c.get_basic());
369 }
370 } // namespace SymEngine
T move(T... args)
Main namespace for SymEngine package.
Definition: add.cpp:19
RCP< const Basic > acos(const RCP< const Basic > &arg)
Canonicalize ACos:
Definition: functions.cpp:1359
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
Definition: integer.h:200
RCP< const Symbol > symbol(const std::string &name)
inline version to return Symbol
Definition: symbol.h:82
void hash_combine(hash_t &seed, const T &v)
Definition: basic-inl.h:137
RCP< const Basic > exp(const RCP< const Basic > &x)
Returns the natural exponential function E**x = pow(E, x)
Definition: pow.cpp:270
RCP< const Basic > asin(const RCP< const Basic > &arg)
Canonicalize ASin:
Definition: functions.cpp:1317
RCP< const Basic > tan(const RCP< const Basic > &arg)
Canonicalize Tan:
Definition: functions.cpp:964
RCP< const Basic > cosh(const RCP< const Basic > &arg)
Canonicalize Cosh:
Definition: functions.cpp:2169
RCP< const Basic > atan(const RCP< const Basic > &arg)
Canonicalize ATan:
Definition: functions.cpp:1481
RCP< const Basic > asinh(const RCP< const Basic > &arg)
Canonicalize ASinh:
Definition: functions.cpp:2333
RCP< const Basic > tanh(const RCP< const Basic > &arg)
Canonicalize Tanh:
Definition: functions.cpp:2247
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
Definition: mul.cpp:358
RCP< const Basic > atanh(const RCP< const Basic > &arg)
Canonicalize ATanh:
Definition: functions.cpp:2451
RCP< const Basic > cos(const RCP< const Basic > &arg)
Canonicalize Cos:
Definition: functions.cpp:899
RCP< const Basic > log(const RCP< const Basic > &arg)
Returns the Natural Logarithm from argument arg
Definition: functions.cpp:1731
RCP< const Number > rational(long n, long d)
convenience creator from two longs
Definition: rational.h:328
RCP< const Basic > sinh(const RCP< const Basic > &arg)
Canonicalize Sinh:
Definition: functions.cpp:2084
RCP< const Basic > sin(const RCP< const Basic > &arg)
Canonicalize Sin:
Definition: functions.cpp:831
STL namespace.