3#include <symengine/symengine_exception.h>
5#ifdef HAVE_SYMENGINE_MPC
10class EvalMPCVisitor :
public BaseVisitor<EvalMPCVisitor>
17 EvalMPCVisitor(mpfr_rnd_t rnd) : rnd_{rnd} {}
19 void apply(mpc_ptr result,
const Basic &b)
21 mpc_ptr tmp = result_;
27 void bvisit(
const Integer &x)
29 mpc_set_z(result_, get_mpz_t(x.as_integer_class()), rnd_);
32 void bvisit(
const Rational &x)
34 mpc_set_q(result_, get_mpq_t(x.as_rational_class()), rnd_);
37 void bvisit(
const RealDouble &x)
39 mpc_set_d(result_, x.i, rnd_);
42 void bvisit(
const Complex &x)
44 mpc_set_q_q(result_, get_mpq_t(x.real_), get_mpq_t(x.imaginary_), rnd_);
47 void bvisit(
const ComplexDouble &x)
49 mpc_set_d_d(result_, x.i.real(), x.i.imag(), rnd_);
52 void bvisit(
const RealMPFR &x)
54 mpc_set_fr(result_, x.i.get_mpfr_t(), rnd_);
57 void bvisit(
const ComplexMPC &x)
59 mpc_set(result_, x.as_mpc().get_mpc_t(), rnd_);
62 void bvisit(
const Add &x)
65 mpc_init2(t, mpc_get_prec(result_));
67 auto d = x.get_args();
69 apply(result_, *(*p));
71 for (; p != d.end(); p++) {
73 mpc_add(result_, result_, t, rnd_);
78 void bvisit(
const Mul &x)
81 mpc_init2(t, mpc_get_prec(result_));
83 auto d = x.get_args();
85 apply(result_, *(*p));
87 for (; p != d.end(); p++) {
89 mpc_mul(result_, result_, t, rnd_);
94 void bvisit(
const Pow &x)
96 if (
eq(*x.get_base(), *E)) {
97 apply(result_, *(x.get_exp()));
98 mpc_exp(result_, result_, rnd_);
101 mpc_init2(t, mpc_get_prec(result_));
103 apply(t, *(x.get_base()));
104 apply(result_, *(x.get_exp()));
105 mpc_pow(result_, t, result_, rnd_);
111 void bvisit(
const Sin &x)
113 apply(result_, *(x.get_arg()));
114 mpc_sin(result_, result_, rnd_);
117 void bvisit(
const Cos &x)
119 apply(result_, *(x.get_arg()));
120 mpc_cos(result_, result_, rnd_);
123 void bvisit(
const Tan &x)
125 apply(result_, *(x.get_arg()));
126 mpc_tan(result_, result_, rnd_);
129 void bvisit(
const Log &x)
131 apply(result_, *(x.get_arg()));
132 mpc_log(result_, result_, rnd_);
135 void bvisit(
const Cot &x)
137 apply(result_, *(x.get_arg()));
138 mpc_tan(result_, result_, rnd_);
139 mpc_ui_div(result_, 1, result_, rnd_);
142 void bvisit(
const Csc &x)
144 apply(result_, *(x.get_arg()));
145 mpc_sin(result_, result_, rnd_);
146 mpc_ui_div(result_, 1, result_, rnd_);
149 void bvisit(
const Sec &x)
151 apply(result_, *(x.get_arg()));
152 mpc_cos(result_, result_, rnd_);
153 mpc_ui_div(result_, 1, result_, rnd_);
156 void bvisit(
const ASin &x)
158 apply(result_, *(x.get_arg()));
159 mpc_asin(result_, result_, rnd_);
162 void bvisit(
const ACos &x)
164 apply(result_, *(x.get_arg()));
165 mpc_acos(result_, result_, rnd_);
168 void bvisit(
const ASec &x)
170 apply(result_, *(x.get_arg()));
171 mpc_ui_div(result_, 1, result_, rnd_);
172 mpc_acos(result_, result_, rnd_);
175 void bvisit(
const ACsc &x)
177 apply(result_, *(x.get_arg()));
178 mpc_ui_div(result_, 1, result_, rnd_);
179 mpc_asin(result_, result_, rnd_);
182 void bvisit(
const ATan &x)
184 apply(result_, *(x.get_arg()));
185 mpc_atan(result_, result_, rnd_);
188 void bvisit(
const ACot &x)
190 apply(result_, *(x.get_arg()));
191 mpc_ui_div(result_, 1, result_, rnd_);
192 mpc_atan(result_, result_, rnd_);
195 void bvisit(
const Sinh &x)
197 apply(result_, *(x.get_arg()));
198 mpc_sinh(result_, result_, rnd_);
201 void bvisit(
const Csch &x)
203 apply(result_, *(x.get_arg()));
204 mpc_sinh(result_, result_, rnd_);
205 mpc_ui_div(result_, 1, result_, rnd_);
208 void bvisit(
const Cosh &x)
210 apply(result_, *(x.get_arg()));
211 mpc_cosh(result_, result_, rnd_);
214 void bvisit(
const Sech &x)
216 apply(result_, *(x.get_arg()));
217 mpc_cosh(result_, result_, rnd_);
218 mpc_ui_div(result_, 1, result_, rnd_);
221 void bvisit(
const Tanh &x)
223 apply(result_, *(x.get_arg()));
224 mpc_tanh(result_, result_, rnd_);
227 void bvisit(
const Coth &x)
229 apply(result_, *(x.get_arg()));
230 mpc_tanh(result_, result_, rnd_);
231 mpc_ui_div(result_, 1, result_, rnd_);
234 void bvisit(
const ASinh &x)
236 apply(result_, *(x.get_arg()));
237 mpc_asinh(result_, result_, rnd_);
240 void bvisit(
const ACsch &x)
242 apply(result_, *(x.get_arg()));
243 mpc_ui_div(result_, 1, result_, rnd_);
244 mpc_asinh(result_, result_, rnd_);
247 void bvisit(
const ACosh &x)
249 apply(result_, *(x.get_arg()));
250 mpc_acosh(result_, result_, rnd_);
253 void bvisit(
const ATanh &x)
255 apply(result_, *(x.get_arg()));
256 mpc_atanh(result_, result_, rnd_);
259 void bvisit(
const ACoth &x)
261 apply(result_, *(x.get_arg()));
262 mpc_ui_div(result_, 1, result_, rnd_);
263 mpc_atanh(result_, result_, rnd_);
266 void bvisit(
const ASech &x)
268 apply(result_, *(x.get_arg()));
269 mpc_ui_div(result_, 1, result_, rnd_);
270 mpc_acosh(result_, result_, rnd_);
273 void bvisit(
const Constant &x)
277 mpfr_init2(t, mpc_get_prec(result_));
278 mpfr_const_pi(t, rnd_);
279 mpc_set_fr(result_, t, rnd_);
281 }
else if (x.__eq__(*E)) {
283 mpfr_init2(t, mpc_get_prec(result_));
284 mpfr_set_ui(t, 1, rnd_);
285 mpfr_exp(t, t, rnd_);
286 mpc_set_fr(result_, t, rnd_);
288 }
else if (x.__eq__(*EulerGamma)) {
290 mpfr_init2(t, mpc_get_prec(result_));
291 mpfr_const_euler(t, rnd_);
292 mpc_set_fr(result_, t, rnd_);
294 }
else if (x.__eq__(*Catalan)) {
296 mpfr_init2(t, mpc_get_prec(result_));
297 mpfr_const_catalan(t, rnd_);
298 mpc_set_fr(result_, t, rnd_);
300 }
else if (x.__eq__(*GoldenRatio)) {
302 mpfr_init2(t, mpc_get_prec(result_));
303 mpfr_sqrt_ui(t, 5, rnd_);
304 mpfr_add_ui(t, t, 1, rnd_);
305 mpfr_div_ui(t, t, 2, rnd_);
306 mpc_set_fr(result_, t, rnd_);
309 throw NotImplementedError(
"Constant " + x.get_name()
310 +
" is not implemented.");
314 void bvisit(
const Gamma &x)
316 throw NotImplementedError(
"Not implemented");
319 void bvisit(
const Abs &x)
322 mpfr_init2(t, mpc_get_prec(result_));
323 apply(result_, *(x.get_arg()));
324 mpc_abs(t, result_, rnd_);
325 mpc_set_fr(result_, t, rnd_);
329 void bvisit(
const NumberWrapper &x)
331 x.eval(mpc_get_prec(result_))->accept(*
this);
334 void bvisit(
const FunctionWrapper &x)
336 x.eval(mpc_get_prec(result_))->accept(*
this);
339 void bvisit(
const UnevaluatedExpr &x)
341 apply(result_, *x.get_arg());
348 void bvisit(
const Basic &)
350 throw NotImplementedError(
"Not Implemented");
354void eval_mpc(mpc_ptr result,
const Basic &b, mpfr_rnd_t rnd)
356 EvalMPCVisitor v(rnd);
Main namespace for SymEngine package.
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b