2 from sympy
.core
.basic
import Basic
, S
, C
, sympify
3 from sympy
.core
.function
import Function
4 from sympy
.functions
.elementary
.miscellaneous
import sqrt
6 ###############################################################################
7 ######################### REAL and IMAGINARY PARTS ############################
8 ###############################################################################
11 """Returns real part of expression. This function performs only
12 elementary analysis and so it will fail to decompose properly
13 more complicated expressions. If completely simplified result
14 is needed then use Basic.as_real_imag() or perform complex
15 expansion on instance of this function.
17 >>> from sympy import *
19 >>> x, y = symbols('x', 'y')
30 >>> re(im(x) + x*I + 2)
40 def _eval_apply_subs(self
, *args
):
44 def canonize(cls
, arg
):
53 included
, reverted
, excluded
= [], [], []
55 if isinstance(arg
, Basic
):
58 coeff
= term
.as_coefficient(S
.ImaginaryUnit
)
62 reverted
.append(coeff
)
63 elif not term
.has(S
.ImaginaryUnit
) and term
.is_real
:
68 if len(arg
[:]) != len(included
):
69 a
, b
, c
= map(lambda xs
: C
.Add(*xs
),
70 [included
, reverted
, excluded
])
72 return cls(a
) - im(b
) + c
74 def _eval_conjugate(self
):
77 def _eval_expand_complex(self
, *args
):
78 return self
.args
[0].as_real_imag()[0]
81 """Returns imaginary part of expression. This function performs
82 only elementary analysis and so it will fail to decompose
83 properly more complicated expressions. If completely simplified
84 result is needed then use Basic.as_real_imag() or perform complex
85 expansion on instance of this function.
87 >>> from sympy import *
89 >>> x, y = symbols('x', 'y')
110 def _eval_apply_subs(self
, *args
):
114 def canonize(cls
, arg
):
123 included
, reverted
, excluded
= [], [], []
125 if isinstance(arg
, Basic
):
128 coeff
= term
.as_coefficient(S
.ImaginaryUnit
)
130 if coeff
is not None:
131 if not coeff
.is_real
:
132 reverted
.append(coeff
)
134 excluded
.append(coeff
)
135 elif term
.has(S
.ImaginaryUnit
) or not term
.is_real
:
136 included
.append(term
)
138 if len(arg
[:]) != len(included
):
139 a
, b
, c
= map(lambda xs
: C
.Add(*xs
),
140 [included
, reverted
, excluded
])
142 return cls(a
) + re(b
) + c
144 def _eval_conjugate(self
):
147 def _eval_expand_complex(self
, *args
):
148 return self
.args
[0].as_real_imag()[1]
150 ###############################################################################
151 ############### SIGN, ABSOLUTE VALUE, ARGUMENT and CONJUGATION ################
152 ###############################################################################
154 class sign(Function
):
159 def canonize(cls
, arg
):
162 if arg
is S
.Zero
: return S
.Zero
163 if arg
.is_positive
: return S
.One
164 if arg
.is_negative
: return S
.NegativeOne
166 coeff
, terms
= arg
.as_coeff_terms()
167 if coeff
is not S
.One
:
168 return cls(coeff
) * cls(C
.Mul(*terms
))
172 def _eval_conjugate(self
):
175 def _eval_is_zero(self
):
176 return (self
[0] is S
.Zero
)
185 def fdiff(self
, argindex
=1):
187 return sign(self
.args
[0])
189 raise ArgumentIndexError(self
, argindex
)
192 def _eval_apply_subs(self
, *args
):
196 def canonize(cls
, arg
):
199 if arg
.is_zero
: return arg
200 if arg
.is_positive
: return arg
201 if arg
.is_negative
: return -arg
202 coeff
, terms
= arg
.as_coeff_terms()
203 if coeff
is not S
.One
:
204 return cls(coeff
) * cls(C
.Mul(*terms
))
205 if arg
.is_real
is False:
206 return sqrt( (arg
* arg
.conjugate()).expand() )
208 base
, exponent
= arg
.as_base_exp()
209 if exponent
.is_Number
:
215 def _eval_apply_evalf(cls
, arg
):
216 # XXX this is weird!!!
217 # XXX remove me when 'abs -> abs_' is done
222 return operator
.abs(float(arg
))
224 def _eval_is_nonzero(self
):
225 return self
._args
[0].is_nonzero
227 def _eval_is_positive(self
):
228 return self
.is_nonzero
230 def _eval_conjugate(self
):
233 def _eval_power(self
,other
):
234 if self
.args
[0].is_real
and other
.is_integer
:
236 return self
.args
[0]**other
240 def taylor_term(n
, x
, *previous_terms
):
248 import sage
.all
as sage
249 return sage
.abs_symbolic(self
.args
[0]._sage
_())
259 def canonize(cls
, arg
):
260 x
, y
= re(arg
), im(arg
)
265 def _eval_conjugate(self
):
268 class conjugate(Function
):
269 """Changes the sign of the imaginary part of a complex number.
271 >>> from sympy import *
281 def canonize(cls
, arg
):
282 obj
= arg
._eval
_conjugate
()
286 def _eval_conjugate(self
):
291 from sympy
.core
import basic
as _