-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpercomplex.py
More file actions
104 lines (87 loc) · 2.7 KB
/
percomplex.py
File metadata and controls
104 lines (87 loc) · 2.7 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
import math
class PerComplex:
"""
Class of percomplex numbers where
real = real number,
imag = i, with i^2 = -1
perplex = p, with p^2 = -1
and i * p = - p * i,
which means that the commutative law is not applicative here.
"""
def __init__(self, real: float, imag: float = 0, perplex: float = 0):
self.real: float = real
self.imag: float = imag
self.perplex: float = perplex
def __add__(self, other):
return PerComplex(
self.real + other.real,
self.imag + other.imag,
self.perplex + other.perplex
)
def __abs__(self):
return math.sqrt(
self.real ** 2 +
self.imag ** 2 +
self.perplex ** 2
)
@property
def coordinates(self):
return self.real, self.imag, self.perplex
def __repr__(self):
return (
f"Real: {self.real}, "
f"Imag: {self.imag}, "
f"Perplex: {self.perplex}"
)
def __mul__(self, other):
if isinstance(other, (int, float)):
return PerComplex(
self.real * other,
self.imag * other,
self.perplex * other
)
# new perplex number must be within the group
assert self.imag * other.perplex == self.perplex * other.imag
new_real = (
self.real * other.real -
self.imag * other.imag -
self.perplex * other.perplex
)
new_imag = self.real * other.imag + self.imag * other.real
new_perplex = self.real * other.perplex + self.perplex * other.real
return PerComplex(
new_real,
new_imag,
new_perplex
)
def __truediv__(self, other):
if isinstance(other, (int, float)):
return PerComplex(
self.real / other,
self.imag / other,
self.perplex / other
)
return self * PerComplex(
other.real,
- other.imag,
- other.perplex
) / (other.real ** 2 + other.imag ** 2 + other.perplex ** 2)
def __pow__(self, power):
new_percomplex = self
for i in range(1, power):
new_percomplex *= self
return new_percomplex
def get_next_members(self, num):
"""
Starting with a PerComplex number get the next one in the group
:return:
"""
index = 1
while index < num + 1:
get_new_perplex = index * self.perplex / self.imag
yield PerComplex(
self.real,
index,
get_new_perplex
)
index += 1