series_generic.cpp
1 #include <exception>
2 #include <iterator>
3 #include <symengine/series_visitor.h>
4 #include <symengine/symengine_exception.h>
5 
6 using SymEngine::make_rcp;
7 using SymEngine::RCP;
8 
9 namespace SymEngine
10 {
11 
12 RCP<const UnivariateSeries> UnivariateSeries::series(const RCP<const Basic> &t,
13  const std::string &x,
14  unsigned int prec)
15 {
16  UExprDict p({{1, Expression(1)}});
17  SeriesVisitor<UExprDict, Expression, UnivariateSeries> visitor(std::move(p),
18  x, prec);
19  return visitor.series(t);
20 }
21 
23 {
24  hash_t seed = SYMENGINE_UEXPRPOLY;
25  hash_combine(seed, get_degree());
26  for (const auto &it : p_.dict_) {
27  hash_t temp = SYMENGINE_UEXPRPOLY;
28  hash_combine<unsigned int>(temp, it.first);
29  hash_combine<Basic>(temp, *(it.second.get_basic()));
30  seed += temp;
31  }
32  return seed;
33 }
34 
35 int UnivariateSeries::compare(const Basic &other) const
36 {
37  SYMENGINE_ASSERT(is_a<UnivariateSeries>(other))
38  const UnivariateSeries &o_ = down_cast<const UnivariateSeries &>(other);
39  return p_.compare(o_.get_poly());
40 }
41 
42 RCP<const Basic> UnivariateSeries::as_basic() const
43 {
44  return p_.get_basic(var_);
45 }
46 
47 umap_int_basic UnivariateSeries::as_dict() const
48 {
49  umap_int_basic map;
50  for (const auto &it : p_.get_dict())
51  if (it.second != 0)
52  map[it.first] = it.second.get_basic();
53  return map;
54 }
55 
56 RCP<const Basic> UnivariateSeries::get_coeff(int deg) const
57 {
58  if (p_.get_dict().count(deg) == 0)
59  return zero;
60  else
61  return p_.get_dict().at(deg).get_basic();
62 }
63 
64 UExprDict UnivariateSeries::var(const std::string &s)
65 {
66  return UExprDict({{1, Expression(1)}});
67 }
68 
69 Expression UnivariateSeries::convert(const Basic &x)
70 {
71  return Expression(x.rcp_from_this());
72 }
73 
74 int UnivariateSeries::ldegree(const UExprDict &s)
75 {
76  return s.get_dict().begin()->first;
77 }
78 
79 UExprDict UnivariateSeries::mul(const UExprDict &a, const UExprDict &b,
80  unsigned prec)
81 {
82  map_int_Expr p;
83  for (auto &it1 : a.get_dict()) {
84  for (auto &it2 : b.get_dict()) {
85  int exp = it1.first + it2.first;
86  if (exp < (int)prec) {
87  p[exp] += it1.second * it2.second;
88  } else {
89  break;
90  }
91  }
92  }
93  return UExprDict(p);
94 }
95 
96 UExprDict UnivariateSeries::pow(const UExprDict &base, int exp, unsigned prec)
97 {
98  if (exp < 0) {
99  SYMENGINE_ASSERT(base.size() == 1)
100  map_int_Expr dict;
101  dict[-(base.get_dict().begin()->first)]
102  = 1 / base.get_dict().begin()->second;
103  return pow(UExprDict(dict), -exp, prec);
104  }
105  if (exp == 0) {
106  if (base == 0 or base.get_dict().size() == 0) {
107  throw DomainError("Error: 0**0 is undefined.");
108  } else {
109  return UExprDict(1);
110  }
111  }
112 
113  UExprDict x(base);
114  UExprDict y(1);
115  while (exp > 1) {
116  if (exp % 2 == 0) {
117  x = mul(x, x, prec);
118  exp /= 2;
119  } else {
120  y = mul(x, y, prec);
121  x = mul(x, x, prec);
122  exp = (exp - 1) / 2;
123  }
124  }
125  return mul(x, y, prec);
126 }
127 
128 Expression UnivariateSeries::find_cf(const UExprDict &s, const UExprDict &var,
129  int deg)
130 {
131  if (s.get_dict().count(deg) == 0)
132  return Expression(0);
133  else
134  return (s.get_dict()).at(deg);
135 }
136 
137 Expression UnivariateSeries::root(Expression &c, unsigned n)
138 {
139  return SymEngine::pow(c, 1 / Expression(n));
140 }
141 
142 UExprDict UnivariateSeries::diff(const UExprDict &s, const UExprDict &var)
143 {
144  if (var.get_dict().size() == 1 and var.get_dict().at(1) == Expression(1)) {
145  map_int_Expr d;
146  for (const auto &p : s.get_dict()) {
147  if (p.first != 0)
148  d[p.first - 1] = p.second * p.first;
149  }
150  return UExprDict(d);
151  } else {
152  return UExprDict({{0, Expression(0)}});
153  }
154 }
155 
156 UExprDict UnivariateSeries::integrate(const UExprDict &s, const UExprDict &var)
157 {
158  map_int_Expr dict;
159  for (auto &it : s.get_dict()) {
160  if (it.first != -1) {
161  dict.insert(std::pair<int, Expression>(it.first + 1,
162  it.second / (it.first + 1)));
163  } else {
164  throw NotImplementedError("Not Implemented");
165  }
166  }
167 
168  return UExprDict(dict);
169 }
170 
171 UExprDict UnivariateSeries::subs(const UExprDict &s, const UExprDict &var,
172  const UExprDict &r, unsigned prec)
173 {
174  UExprDict result({{1, Expression(1)}});
175 
176  for (auto &i : s.get_dict())
177  result += i.second * pow(r, i.first, prec);
178 
179  return result;
180 }
181 
182 Expression UnivariateSeries::sin(const Expression &c)
183 {
184  return SymEngine::sin(c.get_basic());
185 }
186 
187 Expression UnivariateSeries::cos(const Expression &c)
188 {
189  return SymEngine::cos(c.get_basic());
190 }
191 
192 Expression UnivariateSeries::tan(const Expression &c)
193 {
194  return SymEngine::tan(c.get_basic());
195 }
196 
197 Expression UnivariateSeries::asin(const Expression &c)
198 {
199  return SymEngine::asin(c.get_basic());
200 }
201 
202 Expression UnivariateSeries::acos(const Expression &c)
203 {
204  return SymEngine::acos(c.get_basic());
205 }
206 
207 Expression UnivariateSeries::atan(const Expression &c)
208 {
209  return SymEngine::atan(c.get_basic());
210 }
211 
212 Expression UnivariateSeries::sinh(const Expression &c)
213 {
214  return SymEngine::sinh(c.get_basic());
215 }
216 
217 Expression UnivariateSeries::cosh(const Expression &c)
218 {
219  return SymEngine::cosh(c.get_basic());
220 }
221 
222 Expression UnivariateSeries::tanh(const Expression &c)
223 {
224  return SymEngine::tanh(c.get_basic());
225 }
226 
227 Expression UnivariateSeries::asinh(const Expression &c)
228 {
229  return SymEngine::asinh(c.get_basic());
230 }
231 
232 Expression UnivariateSeries::atanh(const Expression &c)
233 {
234  return SymEngine::atanh(c.get_basic());
235 }
236 
237 Expression UnivariateSeries::exp(const Expression &c)
238 {
239  return SymEngine::exp(c.get_basic());
240 }
241 
242 Expression UnivariateSeries::log(const Expression &c)
243 {
244  return SymEngine::log(c.get_basic());
245 }
246 
247 } // namespace SymEngine
The lowest unit of symbolic representation.
Definition: basic.h:97
UnivariateSeries Class.
int compare(const Basic &o) const override
hash_t __hash__() const override
T exp(T... args)
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:1402
void hash_combine(hash_t &seed, const T &v)
Definition: basic-inl.h:95
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 > asin(const RCP< const Basic > &arg)
Canonicalize ASin:
Definition: functions.cpp:1360
RCP< const Basic > tan(const RCP< const Basic > &arg)
Canonicalize Tan:
Definition: functions.cpp:1007
RCP< const Basic > cosh(const RCP< const Basic > &arg)
Canonicalize Cosh:
Definition: functions.cpp:2212
RCP< const Basic > atan(const RCP< const Basic > &arg)
Canonicalize ATan:
Definition: functions.cpp:1524
RCP< const Basic > asinh(const RCP< const Basic > &arg)
Canonicalize ASinh:
Definition: functions.cpp:2376
RCP< const Basic > tanh(const RCP< const Basic > &arg)
Canonicalize Tanh:
Definition: functions.cpp:2290
RCP< const Basic > atanh(const RCP< const Basic > &arg)
Canonicalize ATanh:
Definition: functions.cpp:2494
RCP< const Basic > cos(const RCP< const Basic > &arg)
Canonicalize Cos:
Definition: functions.cpp:942
RCP< const Basic > log(const RCP< const Basic > &arg)
Returns the Natural Logarithm from argument arg
Definition: functions.cpp:1774
RCP< const Basic > sinh(const RCP< const Basic > &arg)
Canonicalize Sinh:
Definition: functions.cpp:2127
RCP< const Basic > sin(const RCP< const Basic > &arg)
Canonicalize Sin:
Definition: functions.cpp:874