-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathratcomplex.py
More file actions
287 lines (255 loc) · 9.55 KB
/
ratcomplex.py
File metadata and controls
287 lines (255 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- coding:utf-8 -*-
"""Rational Complex Number Class
This file is a part of S-expression Library for Python (sxprlib.py).
"""
ratcomplex_version = "1.1"
##################################################################################
# This library is released under the MIT license. #
# ------------------------------------------------------- #
# Copyright (c) 2023 Kitanokitsune #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
##################################################################################
from fractions import Fraction
from weakref import WeakValueDictionary as _WVDic
# data type: Complex
class Complex:
"""Definition of Complex data type"""
# str_format_spec:
# {0}:value_real, {1}:value_imag
# {2}:sign_real, {3}:sign_imag
# {4}:abs_real, {5}:abs_imag
str_format_spec = "{0}{3}{5}i"
repr_format_spec = "Complex({}, {})"
__ComplexInstance = _WVDic({})
def __new__(self, real=0, imag=0):
v1 = real
v2 = imag
if type(v1) in {complex, Complex}:
if type(v2) in {complex, Complex}:
real = v1.real - v2.imag
imag = v1.imag + v2.real
else:
real = v1.real
imag = v1.imag + v2
else:
if type(v2) in {complex, Complex}:
real = v1 - v2.imag
imag = v2.real
else:
real = v1
imag = v2
real = Complex.__reduce_frac(real)
imag = Complex.__reduce_frac(imag)
if imag == 0:
return real
if type(real) is Fraction:
rnu = real.numerator
rde = real.denominator
else:
rnu = real
rde = 1
if type(imag) is Fraction:
inu = imag.numerator
ide = imag.denominator
else:
inu = imag
ide = 1
v = (rnu, rde, inu, ide)
obj = Complex.__ComplexInstance.get(v)
if obj is None:
obj = super().__new__(self)
obj.real = real
obj.imag = imag
Complex.__ComplexInstance[v] = obj
return obj
def __str__(self):
### str_format_spec:
### {0}:value_real, {1}:value_imag
### {2}:sign_real, {3}:sign_imag
### {4}:abs_real, {5}:abs_imag
if self.real < 0:
realsgn = "-"
else:
realsgn = "+"
if self.imag < 0:
imagsgn = "-"
else:
imagsgn = "+"
real = str(self.real)
realabs = str(abs(self.real))
imag = str(self.imag)
imagabs = str(abs(self.imag))
return Complex.str_format_spec.format(
real, imag, realsgn, imagsgn, realabs, imagabs
)
def __repr__(self):
return Complex.repr_format_spec.format(repr(self.real), repr(self.imag))
def __setattr__(self, name, value):
if name in {"real", "imag"} and name in self.__dict__:
raise PermissionError("Complex: {} is read only!".format(name))
self.__dict__[name] = value
def __delattr__(self, name):
if name in {"real", "imag"}:
raise PermissionError(name)
else:
super().__delattr__(name)
def __hash__(self):
if self.imag == 0:
return hash(self.real)
elif self == complex(self):
return hash(complex(self))
else:
return hash((self.real, self.imag))
def __eq__(self, v):
if type(v) in {complex, Complex}:
return self.real == v.real and self.imag == v.imag
else:
return self.real == v and self.imag == 0
def __ne__(self, v):
if type(v) in {complex, Complex}:
return self.real != v.real or self.imag != v.imag
else:
return self.real != v or self.imag != 0
def __complex__(self):
return float(self.real) + 1.0j * float(self.imag)
def __abs__(self):
if self.real == 0:
return abs(self.imag)
elif self.imag == 0: # this case might not occur
return abs(self.real)
else:
return abs(float(self.real) + 1.0j * float(self.imag))
def __pos__(self):
return Complex.__reduce_comp(self.real, self.imag)
def __neg__(self):
return Complex.__reduce_comp(-self.real, -self.imag)
def __add__(self, v):
if type(v) in {complex, Complex}:
real = self.real + v.real
imag = self.imag + v.imag
else:
real = self.real + v
imag = self.imag
return Complex.__reduce_comp(real, imag)
def __radd__(self, v):
if type(v) in {complex, Complex}:
real = self.real + v.real
imag = self.imag + v.imag
else:
real = self.real + v
imag = self.imag
return Complex.__reduce_comp(real, imag)
def __sub__(self, v):
if type(v) in {complex, Complex}:
real = self.real - v.real
imag = self.imag - v.imag
else:
real = self.real - v
imag = self.imag
return Complex.__reduce_comp(real, imag)
def __rsub__(self, v):
if type(v) in {complex, Complex}:
real = -self.real + v.real
imag = -self.imag + v.imag
else:
real = -self.real + v
imag = -self.imag
return Complex.__reduce_comp(real, imag)
def __mul__(self, v):
if type(v) in {complex, Complex}:
real = self.real * v.real - self.imag * v.imag
imag = self.real * v.imag + self.imag * v.real
else:
real = self.real * v
imag = self.imag * v
return Complex.__reduce_comp(real, imag)
def __rmul__(self, v):
if type(v) in {complex, Complex}:
real = self.real * v.real - self.imag * v.imag
imag = self.real * v.imag + self.imag * v.real
else:
real = self.real * v
imag = self.imag * v
return Complex.__reduce_comp(real, imag)
def __truediv__(self, v):
if type(v) in {complex, Complex}:
d = v.real * v.real + v.imag * v.imag
real = (self.real * v.real + self.imag * v.imag) / d
imag = (self.imag * v.real - self.real * v.imag) / d
else:
real = self.real / v
imag = self.imag / v
return Complex.__reduce_comp(real, imag)
def __rtruediv__(self, v):
if type(v) in {complex, Complex}:
d = self.real * self.real + self.imag * self.imag
real = (self.real * v.real + self.imag * v.imag) / d
imag = (-self.imag * v.real + self.real * v.imag) / d
else:
d = self.real * self.real + self.imag * self.imag
real = self.real * v / d
imag = -self.imag * v / d
return Complex.__reduce_comp(real, imag)
def __pow__(self, v):
if type(v) is int:
if v < 0:
sgn = -1
v = -v
else:
sgn = 1
b = bin(v).replace("0b", "")
s = 1
for d in b:
s = s * s
if d == "1":
s = s * self
if sgn == -1:
s = 1 / s
return s
else:
s = complex(self)
if type(v) is Complex:
v = complex(v)
return s**v
def __rpow__(self, v):
s = complex(self)
if type(v) is Complex:
v = complex(v)
return v**s
def conjugate(self):
return Complex(self.real, -self.imag)
@staticmethod
def __reduce_frac(x):
if type(x) is Fraction:
if x.denominator == 1:
return x.numerator
return x
@staticmethod
def __reduce_comp(real, imag):
real = Complex.__reduce_frac(real)
imag = Complex.__reduce_frac(imag)
if imag == 0:
return real
else:
return Complex(real, imag)
@staticmethod
def listall():
return [x for x in Complex.__ComplexInstance]
# [EOF]