You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-13Lines changed: 36 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,7 @@ The names and letter-abbreviations were taken from [this image][3] ([mirror][4])
95
95
96
96
## Usage Examples
97
97
98
-
This list follows [example.py](example.py) exactly and documents nearly all the things you can do with the hypercomplex numbers created by this package.
98
+
This list follows [examples.py](examples.py) exactly and documents nearly all the things you can do with the hypercomplex numbers created by this package.
99
99
100
100
Every example assumes the appropriate imports are already done, e.g. `from hypercomplex import*`.
101
101
@@ -137,31 +137,42 @@ Every example assumes the appropriate imports are already done, e.g. `from hyper
137
137
print(R(2).inverse(), 1/ R(2)) # -> (0.5) (0.5)
138
138
```
139
139
140
-
5. `conjugate` gives the conjugate of the number.
140
+
5. Numbers can be raised to integer powers, a shortcut for repeated multiplication or division.
7. Numbers are considered equal if their coefficients all match. Non-existent coefficients are 0.
167
+
8. Numbers are considered equal if their coefficients all match. Non-existent coefficients are 0.
157
168
158
169
```py
159
170
print(R(999) == V(999)) # -> True
160
171
print(C(1, 2) == Q(1, 2)) # -> True
161
172
print(C(1, 2) == Q(1, 2, 0.1)) # -> False
162
173
```
163
174
164
-
8. `coefficients` gives a tuple of the components of the number in their base type (`float` by default). The properties `real`and`imag` are shortcuts for the first two components. Indexing can also be used (but is inefficient).
175
+
9. `coefficients` gives a tuple of the components of the number in their base type (`float` by default). The properties `real`and`imag` are shortcuts for the first two components. Indexing can also be used (but is inefficient).
165
176
166
177
```py
167
178
print(R(100).coefficients()) # -> (100.0,)
@@ -171,15 +182,15 @@ Every example assumes the appropriate imports are already done, e.g. `from hyper
9. `e(index)` of a number class gives the unit hypercomplex number where the index coefficient is1andall others are 0.
185
+
10. `e(index)` of a number class gives the unit hypercomplex number where the index coefficient is1andall others are 0.
175
186
176
187
```py
177
188
print(C.e(0)) # -> (1 0)
178
189
print(C.e(1)) # -> (0 1)
179
190
print(O.e(3)) # -> (0 0 0 1 0 0 0 0)
180
191
```
181
192
182
-
10. `e_matrix` of a number class gives the multiplication table of `e(i)*e(j)`. Set `string=False` to get a 2Dlist instead of a string. Set `raw=True` to get the raw hypercomplex numbers.
193
+
11. `e_matrix` of a number class gives the multiplication table of `e(i)*e(j)`. Set `string=False` to get a 2Dlist instead of a string. Set `raw=True` to get the raw hypercomplex numbers.
183
194
184
195
```py
185
196
print(O.e_matrix()) # -> e1 e2 e3 e4 e5 e6 e7
@@ -194,7 +205,7 @@ Every example assumes the appropriate imports are already done, e.g. `from hyper
11. A number is considered truthy if it has has non-zero coefficients. Conversion to `int`, `float`and`complex` are only valid when the coefficients beyond the dimension of those types are all0.
208
+
12. A number is considered truthy if it has has non-zero coefficients. Conversion to `int`, `float`and`complex` are only valid when the coefficients beyond the dimension of those types are all0.
198
209
199
210
```py
200
211
print(bool(Q())) # -> False
@@ -205,30 +216,30 @@ Every example assumes the appropriate imports are already done, e.g. `from hyper
205
216
# print(float(C(1, 2))) <- invalid
206
217
```
207
218
208
-
12. Any usual format spec for the base type can be given in an f-string.
219
+
13. Any usual format spec for the base type can be given in an f-string.
I wrote this package for the novelty of it andas a math and programming exercise. The operations it can perform on hypercomplex numbers are not particularly efficient due to the recursive nature of the Cayley-Dickson construction.
Copy file name to clipboardExpand all lines: examples_to_markdown.py
+20Lines changed: 20 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -52,3 +52,23 @@ def examples_to_markdown():
52
52
53
53
if__name__=="__main__":
54
54
examples_to_markdown()
55
+
56
+
# This is the only multiline example and examples_to_markdown can't handle it. Saving it here to avoid manually making it again.
57
+
e_matrix_example="""
58
+
59
+
11. `e_matrix` of a number class gives the multiplication table of `e(i)*e(j)`. Set `string=False` to get a 2D list instead of a string. Set `raw=True` to get the raw hypercomplex numbers.
Copy file name to clipboardExpand all lines: hypercomplex/examples.py
+31-12Lines changed: 31 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -45,44 +45,53 @@
45
45
print(R(2).inverse(), 1/R(2))
46
46
47
47
48
-
# %% 5. `conjugate` gives the conjugate of the number.
48
+
# %% 5. Numbers can be raised to integer powers, a shortcut for repeated multiplication or division.
49
+
q=Q(0, 3, 4, 0)
50
+
print(q**5)
51
+
print(q*q*q*q*q)
52
+
print(q**-1)
53
+
print(1/q)
54
+
print(q**0)
55
+
56
+
57
+
# %% 6. `conjugate` gives the conjugate of the number.
49
58
print(R(9).conjugate())
50
59
print(C(9, 8).conjugate())
51
60
print(Q(9, 8, 7, 6).conjugate())
52
61
53
62
54
-
# %% 6. `norm` gives the absolute value as the base type (`float` by default). There is also `norm_squared`.
63
+
# %% 7. `norm` gives the absolute value as the base type (`float` by default). There is also `norm_squared`.
55
64
print(O(3, 4).norm(), type(O(3, 4).norm()))
56
65
print(abs(O(3, 4)))
57
66
print(O(3, 4).norm_squared())
58
67
59
68
60
-
# %% 7. Numbers are considered equal if their coefficients all match. Non-existent coefficients are 0.
69
+
# %% 8. Numbers are considered equal if their coefficients all match. Non-existent coefficients are 0.
61
70
print(R(999) ==V(999))
62
71
print(C(1, 2) ==Q(1, 2))
63
72
print(C(1, 2) ==Q(1, 2, 0.1))
64
73
65
74
66
-
# %% 8. `coefficients` gives a tuple of the components of the number in their base type (`float` by default). The properties `real` and `imag` are shortcuts for the first two components. Indexing can also be used (but is inefficient).
75
+
# %% 9. `coefficients` gives a tuple of the components of the number in their base type (`float` by default). The properties `real` and `imag` are shortcuts for the first two components. Indexing can also be used (but is inefficient).
67
76
print(R(100).coefficients())
68
77
q=Q(2, 3, 4, 5)
69
78
print(q.coefficients())
70
79
print(q.real, q.imag)
71
80
print(q[0], q[1], q[2], q[3])
72
81
73
82
74
-
# %% 9. `e(index)` of a number class gives the unit hypercomplex number where the index coefficient is 1 and all others are 0.
83
+
# %% 10. `e(index)` of a number class gives the unit hypercomplex number where the index coefficient is 1 and all others are 0.
75
84
print(C.e(0))
76
85
print(C.e(1))
77
86
print(O.e(3))
78
87
79
88
80
-
# %% 10. `e_matrix` of a number class gives the multiplication table of `e(i)*e(j)`. Set `string=False` to get a 2D list instead of a string. Set `raw=True` to get the raw hypercomplex numbers.
89
+
# %% 11. `e_matrix` of a number class gives the multiplication table of `e(i)*e(j)`. Set `string=False` to get a 2D list instead of a string. Set `raw=True` to get the raw hypercomplex numbers.
81
90
print(O.e_matrix())
82
91
print(C.e_matrix(string=False, raw=True))
83
92
84
93
85
-
# %% 11. A number is considered truthy if it has has non-zero coefficients. Conversion to `int`, `float` and `complex` are only valid when the coefficients beyond the dimension of those types are all 0.
94
+
# %% 12. A number is considered truthy if it has has non-zero coefficients. Conversion to `int`, `float` and `complex` are only valid when the coefficients beyond the dimension of those types are all 0.
86
95
print(bool(Q()))
87
96
print(bool(Q(0, 0, 0.01, 0)))
88
97
@@ -91,35 +100,45 @@
91
100
# print(float(C(1, 2))) <- invalid
92
101
93
102
94
-
# %% 12. Any usual format spec for the base type can be given in an f-string.
103
+
# %% 13. Any usual format spec for the base type can be given in an f-string.
95
104
o=O(0.001, 1, -2, 3.3333, 4e5)
96
105
print(f"{o:.2f}")
97
106
print(f"{R(23.9):04.0f}")
98
107
99
108
100
-
# %% 13. The `len` of a number is its hypercomplex dimension, i.e. the number of components or coefficients it has.
109
+
# %% 14. The `len` of a number is its hypercomplex dimension, i.e. the number of components or coefficients it has.
101
110
print(len(R()))
102
111
print(len(C(7, 7)))
103
112
print(len(U()))
104
113
105
114
106
-
# %% 14. Using `in` behaves the same as if the number were a tuple of its coefficients.
115
+
# %% 15. Using `in` behaves the same as if the number were a tuple of its coefficients.
107
116
print(3inQ(1, 2, 3, 4))
108
117
print(5inQ(1, 2, 3, 4))
109
118
110
119
111
-
# %% 15. `copy` can be used to duplicate a number (but should generally not be needed).
120
+
# %% 16. `copy` can be used to duplicate a number (but should generally never be needed as all operations create a new number).
112
121
x=O(9, 8, 7)
113
122
y=x.copy()
114
123
print(x==y)
115
124
print(xisy)
116
125
117
126
118
-
# %% 16. `base` on a number class will return the base type the entire numbers are built upon.
127
+
# %% 17. `base` on a number class will return the base type the entire numbers are built upon.
119
128
print(R.base())
120
129
print(V.base())
121
130
A=cayley_dickson_algebra(20, int)
122
131
print(A.base())
123
132
124
133
134
+
# %% 18. Hypercomplex numbers are weird, so be careful! Here two non-zero sedenions multiply to give zero because sedenions and beyond have zero devisors.
0 commit comments