NZMATH  1.2.0
About: NZMATH is a Python based number theory oriented calculation system.
  Fossies Dox: NZMATH-1.2.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

imaginary.py
Go to the documentation of this file.
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 
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 
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 
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
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)
nzmath.imaginary.Complex.getRing
def getRing(self)
Definition: imaginary.py:203
nzmath.imaginary.Complex.inverse
def inverse(self)
Definition: imaginary.py:170
nzmath.ring
Definition: ring.py:1
nzmath.imaginary.Complex.__rsub__
def __rsub__(self, other)
Definition: imaginary.py:55
nzmath.imaginary.Complex.__abs__
def __abs__(self)
Definition: imaginary.py:146
nzmath.imaginary.Complex.__mul__
def __mul__(self, other)
Definition: imaginary.py:67
nzmath.imaginary.Complex.imag
imag
Definition: imaginary.py:21
nzmath.imaginary.ComplexField.issubring
def issubring(self, aRing)
Definition: imaginary.py:257
nzmath.imaginary.Complex.__repr__
def __repr__(self)
Definition: imaginary.py:164
nzmath.ecpp.log
log
Definition: ecpp.py:20
nzmath.imaginary.Complex.__complex__
def __complex__(self)
Definition: imaginary.py:200
nzmath.imaginary.ComplexField._getZero
def _getZero(self)
Definition: imaginary.py:252
nzmath.ring.Field
Definition: ring.py:190
nzmath.imaginary.Complex.__str__
def __str__(self)
Definition: imaginary.py:167
nzmath.imaginary.Complex.__eq__
def __eq__(self, other)
Definition: imaginary.py:125
nzmath.imaginary.Complex.__add__
def __add__(self, other)
Definition: imaginary.py:29
nzmath.imaginary.ComplexField.__hash__
def __hash__(self)
Definition: imaginary.py:241
nzmath.rational
Definition: rational.py:1
nzmath.imaginary.Complex
Definition: imaginary.py:13
nzmath.imaginary.Complex.__ne__
def __ne__(self, other)
Definition: imaginary.py:137
nzmath.imaginary.ComplexField.createElement
def createElement(self, seed)
Definition: imaginary.py:244
nzmath.imaginary.ComplexField._one
_one
Definition: imaginary.py:218
nzmath.imaginary.Complex.copy
def copy(self)
Definition: imaginary.py:179
nzmath.imaginary.ComplexField
Definition: imaginary.py:210
nzmath.imaginary.Complex.real
real
Definition: imaginary.py:20
nzmath.imaginary.Complex.__rdiv__
def __rdiv__(self, other)
Definition: imaginary.py:96
nzmath.imaginary.Complex.__div__
def __div__(self, other)
Definition: imaginary.py:81
nzmath.imaginary.ComplexField.__init__
def __init__(self)
Definition: imaginary.py:216
nzmath.imaginary.ComplexField.getCharacteristic
def getCharacteristic(self)
Definition: imaginary.py:271
nzmath.imaginary.Complex.__le__
def __le__(self, other)
Definition: imaginary.py:186
nzmath.imaginary.Complex.__gt__
def __gt__(self, other)
Definition: imaginary.py:189
nzmath.imaginary.Complex.arg
def arg(self)
Definition: imaginary.py:195
nzmath.imaginary.ComplexField.__str__
def __str__(self)
Definition: imaginary.py:221
nzmath.imaginary.ComplexField.__eq__
def __eq__(self, other)
Definition: imaginary.py:235
nzmath.plugins
Definition: plugins.py:1
nzmath.real
Definition: real.py:1
nzmath.imaginary.ComplexField.__contains__
def __contains__(self, element)
Definition: imaginary.py:227
nzmath.rational.Integer
Definition: rational.py:639
nzmath.imaginary.ComplexField._getOne
def _getOne(self)
Definition: imaginary.py:247
nzmath.imaginary.ComplexField.__repr__
def __repr__(self)
Definition: imaginary.py:224
nzmath.imaginary.Complex.__pos__
def __pos__(self)
Definition: imaginary.py:153
nzmath.imaginary.Complex.__pow__
def __pow__(self, other)
Definition: imaginary.py:111
nzmath.imaginary.Complex.__init__
def __init__(self, re, im=None)
Definition: imaginary.py:18
nzmath.imaginary.Complex.__neg__
def __neg__(self)
Definition: imaginary.py:158
nzmath.imaginary.Complex.conjugate
def conjugate(self)
Definition: imaginary.py:176
nzmath.imaginary.ComplexField.__ne__
def __ne__(self, other)
Definition: imaginary.py:238
nzmath.imaginary.ComplexField._zero
_zero
Definition: imaginary.py:219
nzmath.ring.FieldElement
Definition: ring.py:343
nzmath.imaginary.ComplexField.issuperring
def issuperring(self, aRing)
Definition: imaginary.py:264
nzmath.imaginary.Complex.__ge__
def __ge__(self, other)
Definition: imaginary.py:192
nzmath.imaginary.Complex.__lt__
def __lt__(self, other)
comparisons are prohibited
Definition: imaginary.py:183
nzmath.imaginary.Complex.__sub__
def __sub__(self, other)
Definition: imaginary.py:43
nzmath.imaginary.Complex.__nonzero__
def __nonzero__(self)
Definition: imaginary.py:161
nzmath.imaginary.Complex.__hash__
def __hash__(self)
Definition: imaginary.py:134