"Fossies" - the Fresh Open Source Software Archive 
Member "NZMATH-1.2.0/nzmath/imaginary.py" (19 Nov 2012, 8216 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "imaginary.py" see the
Fossies "Dox" file reference documentation.
1 from __future__ import division
2 # standard modules
3 import itertools
4 # NZMATH modules
5 import nzmath.real as real
6 import nzmath.rational as rational
7 import nzmath.ring as ring
8
9 from nzmath.plugins import MATHMODULE as math, CMATHMODULE as cmath, \
10 FLOATTYPE as Float, CHECK_REAL_OR_COMPLEX as check_real_or_complex
11
12
13 class Complex (ring.FieldElement):
14 """
15 Complex is a class for complex numbers. Each instance has a coupled
16 numbers; real and imaginary part of the number.
17 """
18 def __init__(self, re, im=None):
19 if im:
20 self.real = re
21 self.imag = im
22 elif isinstance(re, complex) or isinstance(re, Complex):
23 self.real = re.real
24 self.imag = re.imag
25 else:
26 self.real = re
27 self.imag = 0
28
29 def __add__(self, other):
30 try:
31 re = self.real + other.real
32 im = self.imag + other.imag
33 except AttributeError:
34 if other in real.theRealField:
35 re = self.real + other
36 im = +self.imag
37 else:
38 return NotImplemented
39 return self.__class__(re, im)
40
41 __radd__ = __add__
42
43 def __sub__(self, other):
44 try:
45 re = self.real - other.real
46 im = self.imag - other.imag
47 except AttributeError:
48 if other in real.theRealField:
49 re = self.real - other
50 im = +self.imag
51 else:
52 return NotImplemented
53 return self.__class__(re, im)
54
55 def __rsub__(self, other):
56 try:
57 re = other.real - self.real
58 im = other.imag - self.imag
59 except AttributeError:
60 if other in real.theRealField:
61 re = other - self.real
62 im = -self.imag
63 else:
64 return NotImplemented
65 return self.__class__(re, im)
66
67 def __mul__(self, other):
68 try:
69 re = self.real * other.real - self.imag * other.imag
70 im = self.real * other.imag + self.imag * other.real
71 except AttributeError:
72 if other in real.theRealField:
73 re = self.real * other
74 im = self.imag * other
75 else:
76 return NotImplemented
77 return self.__class__(re, im)
78
79 __rmul__ = __mul__
80
81 def __div__(self, other):
82 try:
83 denominator = other.real ** 2 + other.imag ** 2
84 re = (self.real * other.real + self.imag * other.imag) / denominator
85 im = (self.imag * other.real - self.real * other.imag) / denominator
86 except AttributeError:
87 if other in real.theRealField:
88 re = self.real / other
89 im = self.imag / other
90 else:
91 return NotImplemented
92 return self.__class__(re, im)
93
94 __truediv__ = __div__
95
96 def __rdiv__(self, other):
97 denominator = self.real ** 2 + self.imag ** 2
98 try:
99 re = (self.real * other.real + self.imag * other.imag) / denominator
100 im = (self.real * other.imag - self.imag * other.real) / denominator
101 except AttributeError:
102 if other in real.theRealField:
103 re = other * self.real / denominator
104 im = -self.imag * other / denominator
105 else:
106 return NotImplemented
107 return self.__class__(re, im)
108
109 __rtruediv__ = __rdiv__
110
111 def __pow__(self, other):
112 if rational.isIntegerObject(other):
113 if other == 0:
114 return rational.Integer(1)
115 elif other == 1:
116 return +self
117 elif other < 0:
118 return (self**(-other)).inverse()
119 elif other == 2:
120 return self.__class__(self.real ** 2 - self.imag ** 2, 2 * self.real * self.imag)
121 else:
122 return rational.Integer(other).actMultiplicative(self)
123 return exp(other * log(self))
124
125 def __eq__(self, other):
126 try:
127 return self.real == other.real and self.imag == other.imag
128 except AttributeError:
129 if other in real.theRealField:
130 return self.imag == 0 and self.real == other
131 else:
132 return NotImplemented
133
134 def __hash__(self):
135 return hash(self.real**2 + self.imag**2)
136
137 def __ne__(self, other):
138 try:
139 return self.real != other.real or self.imag != other.imag
140 except AttributeError:
141 if other in real.theRealField:
142 return self.imag != 0 or self.real != other
143 else:
144 return NotImplemented
145
146 def __abs__(self):
147 if self.imag == 0:
148 return abs(self.real)
149 if self.real == 0:
150 return abs(self.imag)
151 return math.hypot(self.real, self.imag)
152
153 def __pos__(self):
154 if self.imag == 0:
155 return +self.real
156 return self.__class__(+self.real, +self.imag)
157
158 def __neg__(self):
159 return self.__class__(-self.real, -self.imag)
160
161 def __nonzero__(self):
162 return bool(self.real or self.imag)
163
164 def __repr__(self):
165 return "Complex(" + repr(self.real) + ", " + repr(self.imag) + ")"
166
167 def __str__(self):
168 return str(self.real) + " + " + str(self.imag) + "j"
169
170 def inverse(self):
171 denominator = self.real ** 2 + self.imag ** 2
172 re = self.real / denominator
173 im = -self.imag / denominator
174 return self.__class__(re, im)
175
176 def conjugate(self):
177 return self.__class__(self.real, -self.imag)
178
179 def copy(self):
180 return self.__class__(self.real, self.imag)
181
182 ## comparisons are prohibited
183 def __lt__(self, other):
184 raise TypeError("cannot compare complex numbers using <, <=, >, >=")
185
186 def __le__(self, other):
187 raise TypeError("cannot compare complex numbers using <, <=, >, >=")
188
189 def __gt__(self, other):
190 raise TypeError("cannot compare complex numbers using <, <=, >, >=")
191
192 def __ge__(self, other):
193 raise TypeError("cannot compare complex numbers using <, <=, >, >=")
194
195 def arg(self):
196 x = self.real
197 y = self.imag
198 return math.atan2(y,x)
199
200 def __complex__(self):
201 return complex(float(self.real), float(self.imag))
202
203 def getRing(self):
204 """
205 Return the complex field instance.
206 """
207 return theComplexField
208
209
210 class ComplexField (ring.Field):
211 """
212 ComplexField is a class of the field of real numbers.
213 The class has the single instance 'theComplexField'.
214 """
215
216 def __init__(self):
217 ring.Field.__init__(self)
218 self._one = Complex(1)
219 self._zero = Complex(0)
220
221 def __str__(self):
222 return "C"
223
224 def __repr__(self):
225 return "%s()" % self.__class__.__name__
226
227 def __contains__(self, element):
228 reduced = +element
229 if reduced in real.theRealField:
230 return True
231 if isinstance(reduced, complex) or isinstance(reduced, Complex):
232 return True
233 return False ## How to know an object be complex ?
234
235 def __eq__(self, other):
236 return isinstance(other, ComplexField)
237
238 def __ne__(self, other):
239 return not (self == other)
240
241 def __hash__(self):
242 return 3
243
244 def createElement(self, seed):
245 return Complex(seed)
246
247 def _getOne(self):
248 return self._one
249
250 one = property(_getOne, None, None, "multiplicative unit.")
251
252 def _getZero(self):
253 return self._zero
254
255 zero = property(_getZero, None, None, "additive unit.")
256
257 def issubring(self, aRing):
258 if isinstance(aRing, ComplexField):
259 return True
260 elif self.issuperring(aRing):
261 return False
262 return aRing.issuperring(self)
263
264 def issuperring(self, aRing):
265 if isinstance(aRing, ComplexField):
266 return True
267 elif real.theRealField.issuperring(aRing):
268 return True
269 return aRing.issubring(self)
270
271 def getCharacteristic(self):
272 """
273 The characteristic of the real field is zero.
274 """
275 return 0
276
277
278 theComplexField = ComplexField()
279
280 j = Complex(0,1)