2 from sympy
.core
.basic
import Basic
, S
, cache_it
, cache_it_immutable
3 from sympy
.core
.function
import SingleValuedFunction
5 ###############################################################################
6 ######################### REAL and IMAGINARY PARTS ############################
7 ###############################################################################
9 class re(SingleValuedFunction
):
10 """Returns real part of expression. This function performs only
11 elementary analysis and so it will fail to decompose properly
12 more complicated expressions. If completely simplified result
13 is needed then use Basic.as_real_imag() or perform complex
14 expansion on instance of this function.
16 >>> from sympy import *
18 >>> x, y = symbols('x', 'y')
29 >>> re(im(x) + x*I + 2)
39 def _eval_apply_subs(self
, *args
):
43 def canonize(cls
, arg
):
44 arg
= Basic
.sympify(arg
)
46 if isinstance(arg
, Basic
.NaN
):
51 if not isinstance(arg
, Basic
.Add
):
54 included
, reverted
, excluded
= [], [], []
57 coeff
= term
.as_coefficient(S
.ImaginaryUnit
)
61 reverted
.append(coeff
)
62 elif not term
.has(S
.ImaginaryUnit
) and term
.is_real
:
67 if len(arg
[:]) != len(included
):
68 a
, b
, c
= map(lambda xs
: Basic
.Add(*xs
),
69 [included
, reverted
, excluded
])
71 return cls(a
) - im(b
) + c
73 def _eval_conjugate(self
):
76 def _eval_is_real(self
):
79 def _eval_expand_complex(self
, *args
):
80 return self
.func(self
[0].as_real_imag()[0])
82 class im(SingleValuedFunction
):
83 """Returns imaginary part of expression. This function performs
84 only elementary analysis and so it will fail to decompose
85 properly more complicated expressions. If completely simplified
86 result is needed then use Basic.as_real_imag() or perform complex
87 expansion on instance of this function.
89 >>> from sympy import *
91 >>> x, y = symbols('x', 'y')
112 def _eval_apply_subs(self
, *args
):
116 def canonize(cls
, arg
):
117 arg
= Basic
.sympify(arg
)
119 if isinstance(arg
, Basic
.NaN
):
124 if not isinstance(arg
, Basic
.Add
):
127 included
, reverted
, excluded
= [], [], []
130 coeff
= term
.as_coefficient(S
.ImaginaryUnit
)
132 if coeff
is not None:
133 if not coeff
.is_real
:
134 reverted
.append(coeff
)
136 excluded
.append(coeff
)
137 elif term
.has(S
.ImaginaryUnit
) or not term
.is_real
:
138 included
.append(term
)
140 if len(arg
[:]) != len(included
):
141 a
, b
, c
= map(lambda xs
: Basic
.Add(*xs
),
142 [included
, reverted
, excluded
])
144 return cls(a
) + re(b
) + c
146 def _eval_conjugate(self
):
149 def _eval_is_real(self
):
152 def _eval_expand_complex(self
, *args
):
153 return self
.func(self
[0].as_real_imag()[1])
155 ###############################################################################
156 ############### SIGN, ABSOLUTE VALUE, ARGUMENT and CONJUGATION ################
157 ###############################################################################
159 class sign(SingleValuedFunction
):
164 def canonize(cls
, arg
):
165 if isinstance(arg
, Basic
.NaN
):
167 if isinstance(arg
, Basic
.Zero
): return S
.One
168 if arg
.is_positive
: return S
.One
169 if arg
.is_negative
: return S
.NegativeOne
170 if isinstance(arg
, Basic
.Mul
):
171 coeff
, terms
= arg
.as_coeff_terms()
172 if not isinstance(coeff
, Basic
.One
):
173 return cls(coeff
) * cls(Basic
.Mul(*terms
))
177 def _eval_conjugate(self
):
180 def _eval_is_zero(self
):
181 return isinstance(self
[0], Basic
.Zero
)
183 class abs(SingleValuedFunction
):
187 def fdiff(self
, argindex
=1):
191 raise ArgumentIndexError(self
, argindex
)
194 def _eval_apply_subs(self
, *args
):
198 def canonize(cls
, arg
):
199 if isinstance(arg
, Basic
.NaN
):
201 if arg
.is_positive
: return arg
202 if arg
.is_negative
: return -arg
203 coeff
, terms
= arg
.as_coeff_terms()
204 if not isinstance(coeff
, Basic
.One
):
205 return cls(coeff
) * cls(Basic
.Mul(*terms
))
206 if arg
.is_real
is False:
207 return Basic
.sqrt( (arg
* arg
.conjugate()).expand() )
211 def _eval_apply_evalf(cls
, arg
):
212 # XXX this is weird!!!
213 # XXX remove me when 'abs -> abs_' is done
216 if isinstance(arg
, Basic
.Number
):
218 return operator
.abs(float(arg
))
220 def _eval_is_zero(self
):
221 return isinstance(self
[0], Basic
.Zero
)
223 def _eval_conjugate(self
):
226 class arg(SingleValuedFunction
):
232 def _eval_conjugate(self
):
235 def _eval_is_real(self
):
238 class conjugate(SingleValuedFunction
):
243 def canonize(cls
, arg
):
244 obj
= arg
._eval
_conjugate
()
248 def _eval_conjugate(self
):