7 void pow_number(
const RCP<const Basic> &in_re,
const RCP<const Basic> &in_im,
8 unsigned long n, Ptr<RCP<const Basic>> &out_re,
9 Ptr<RCP<const Basic>> &out_im)
11 unsigned long mask = 1;
13 RCP<const Basic> p_re = in_re;
14 RCP<const Basic> p_im = in_im;
21 tmp =
sub(
mul(*out_re, p_re),
mul(*out_im, p_im));
22 *out_im =
add(
mul(*out_re, p_im),
mul(*out_im, p_re));
26 if (mask > 0 and n >= mask) {
28 tmp =
sub(
mul(p_re, p_re),
mul(p_im, p_im));
29 p_im =
mul(two,
mul(p_re, p_im));
40 Ptr<RCP<const Basic>> real_, imag_;
44 const Ptr<RCP<const Basic>> &imag)
45 : real_{real}, imag_{imag}
49 void apply(
const Basic &b)
54 void bvisit(
const Mul &x)
56 RCP<const Basic> rest = one;
57 RCP<const Basic> fre_ = one, fim_ = zero;
59 for (
const auto &arg : x.
get_args()) {
63 add(
mul(fre_, *imag_),
mul(fim_, *real_)));
69 void bvisit(
const Add &x)
73 RCP<const Number> coefr = zero, coefim = zero, coef;
75 for (
const auto &arg : x.
get_args()) {
78 iaddnum(outArg(coefr), rcp_static_cast<const Number>(*real_));
84 iaddnum(outArg(coefim), rcp_static_cast<const Number>(*imag_));
95 void bvisit(
const Pow &x)
97 RCP<const Basic> exp_;
101 if (
eq(**imag_, *zero)) {
106 if (is_a<Integer>(*exp_)) {
108 auto magn =
add(
mul(*real_, *real_),
mul(*imag_, *imag_));
109 *imag_ =
neg(*imag_);
110 RCP<const Integer> expx = rcp_static_cast<const Integer>(exp_);
111 expx =
static_cast<const Integer &
>(*exp_).
neg();
112 unsigned long n = numeric_cast<unsigned long>(
113 mp_get_ui(expx->as_integer_class()));
114 RCP<const Basic> real1 = *real_, imag1 = *imag_;
115 pow_number(real1, imag1, n, real_, imag_);
116 magn = pow(magn, expx);
117 *real_ =
div(*real_, magn);
118 *imag_ =
div(*imag_, magn);
120 RCP<const Integer> expx = rcp_static_cast<const Integer>(exp_);
121 unsigned long n = numeric_cast<unsigned long>(
122 mp_get_ui(expx->as_integer_class()));
123 RCP<const Basic> real1 = *real_, imag1 = *imag_;
124 pow_number(real1, imag1, n, real_, imag_);
126 }
else if (is_a<Rational>(*exp_)) {
127 auto magn = sqrt(
add(
mul(*real_, *real_),
mul(*imag_, *imag_)));
128 auto ang =
atan2(*imag_, *real_);
129 magn = pow(magn, exp_);
130 ang =
mul(ang, exp_);
131 *real_ =
mul(magn,
cos(ang));
132 *imag_ =
mul(magn,
sin(ang));
134 throw SymEngineException(
"Not Implemented");
140 *real_ = x.real_part();
141 *imag_ = x.imaginary_part();
144 void bvisit(
const Infty &x)
146 if (
eq(x, *ComplexInf)) {
155 void bvisit(
const Sin &x)
162 void bvisit(
const Cos &x)
170 void bvisit(
const Tan &x)
173 if (
eq(**imag_, *zero)) {
178 auto twice_real_ =
mul(two, *real_), twice_imag_ =
mul(two, *imag_);
179 auto den =
add(
cos(twice_real_),
cosh(twice_imag_));
180 *real_ =
div(
sin(twice_real_), den);
181 *imag_ =
div(
sinh(twice_imag_), den);
184 void bvisit(
const Csc &x)
189 void bvisit(
const Sec &x)
194 void bvisit(
const Cot &x)
197 if (
eq(**imag_, *zero)) {
201 auto twice_real_ =
mul(two, *real_), twice_imag_ =
mul(two, *imag_);
202 auto den =
sub(
cos(twice_real_),
cosh(twice_imag_));
203 *real_ =
neg(
div(
sin(twice_real_), den));
207 void bvisit(
const Sinh &x)
214 void bvisit(
const Cosh &x)
221 void bvisit(
const Tanh &x)
224 if (
eq(**imag_, *zero)) {
228 auto sinh_re =
sinh(*real_), cos_im =
cos(*imag_);
229 auto den =
add(pow(sinh_re, two), pow(cos_im, two));
230 *real_ =
div(
mul(sinh_re,
cosh(*real_)), den);
231 *imag_ =
div(
mul(
sin(*imag_), cos_im), den);
234 void bvisit(
const Csch &x)
239 void bvisit(
const Sech &x)
244 void bvisit(
const Coth &x)
247 if (
eq(**imag_, *zero)) {
251 auto sinh_re =
sinh(*real_), sin_im =
sin(*imag_);
252 auto den =
add(pow(sinh_re, two), pow(sin_im, two));
253 *real_ =
div(
mul(sinh_re,
cosh(*real_)), den);
257 void bvisit(
const Abs &x)
265 throw SymEngineException(
266 "Not Implemented classes for real and imaginary parts");
269 void bvisit(
const Symbol &x)
271 throw SymEngineException(
272 "Not Implemented classes for real and imaginary parts");
275 void bvisit(
const Basic &x)
282 void as_real_imag(
const RCP<const Basic> &x,
const Ptr<RCP<const Basic>> &real,
283 const Ptr<RCP<const Basic>> &imag)
The base class for SymEngine.
The base class for representing addition in symbolic expressions.
static RCP< const Basic > from_dict(const RCP< const Number > &coef, umap_basic_num &&d)
Create an appropriate instance from dictionary quickly.
vec_basic get_args() const override
Returns the arguments of the Add.
static void dict_add_term(umap_basic_num &d, const RCP< const Number > &coef, const RCP< const Basic > &t)
Adds a new term to the expression.
static void as_coef_term(const RCP< const Basic > &self, const Ptr< RCP< const Number >> &coef, const Ptr< RCP< const Basic >> &term)
Converts a Basic self into the form of coefficient * term.
The lowest unit of symbolic representation.
ComplexBase Class for deriving all complex classes.
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
bool is_negative() const override
RCP< const Integer > neg() const
vec_basic get_args() const override
Returns the list of arguments.
RCP< const Basic > get_arg() const
RCP< const Basic > get_exp() const
RCP< const Basic > get_base() const
Main namespace for SymEngine package.
bool is_a_Number(const Basic &b)
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
RCP< const Basic > atan2(const RCP< const Basic > &num, const RCP< const Basic > &den)
Canonicalize ATan2:
RCP< const Basic > sub(const RCP< const Basic > &a, const RCP< const Basic > &b)
Substracts b from a.
RCP< const Basic > cosh(const RCP< const Basic > &arg)
Canonicalize Cosh:
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
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).
RCP< const Basic > sinh(const RCP< const Basic > &arg)
Canonicalize Sinh:
RCP< const Basic > neg(const RCP< const Basic > &a)
Negation.
RCP< const Basic > sin(const RCP< const Basic > &arg)
Canonicalize Sin: