forked from johnkerl/sack
/
quatu_tm.py
executable file
·177 lines (142 loc) · 3.41 KB
/
quatu_tm.py
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
#!/usr/bin/python -Wall
# ================================================================
# This software is released under the terms of the GNU GPL.
# Please see LICENSE.txt in the same directory as this file.
# John Kerl
# kerl.john.r@gmail.com
# 2007-05-31
# ================================================================
import re
quatu_mul_table = [
# 1 -1 i -i j -j k -k
[ 0, 1, 2, 3, 4, 5, 6, 7 ], # 1
[ 1, 0, 3, 2, 5, 4, 7, 6 ], # -1
[ 2, 3, 1, 0, 6, 7, 5, 4 ], # i
[ 3, 2, 0, 1, 7, 6, 4, 5 ], # -i
[ 4, 5, 7, 6, 1, 0, 2, 3 ], # j
[ 5, 4, 6, 7, 0, 1, 3, 2 ], # -j
[ 6, 7, 4, 5, 3, 2, 1, 0 ], # k
[ 7, 6, 5, 4, 2, 3, 0, 1 ], # -k
]
quatu_inv_table = [ 0, 1, 3, 2, 5, 4, 7, 6 ]
# 1 -1 i -i j -j k -k
class quatu_t:
#code = 0
def __init__(self, argcode):
self.code = argcode & 7
def __mul__(a,b):
c = quatu_t(quatu_mul_table[a.code][b.code]);
return c
def __eq__(a,b):
return (a.code == b.code)
def __ne__(a,b):
return not (a == b)
def __lt__(a,b):
return (a.code < b.code)
def __le__(a,b):
return (a.code <= b.code)
def __gt__(a,b):
return (a.code > b.code)
def __ge__(a,b):
return (a.code >= b.code)
def inv(a):
c = quatu_t(quatu_inv_table[a.code]);
return c
def scan(self, string):
if (string == "1"):
self.__init__(0)
elif (string == "-1"):
self.__init__(1)
elif (string == "i"):
self.__init__(2)
elif (string == "-i"):
self.__init__(3)
elif (string == "j"):
self.__init__(4)
elif (string == "-j"):
self.__init__(5)
elif (string == "k"):
self.__init__(6)
elif (string == "-k"):
self.__init__(7)
else:
raise IOError
def __str__(self):
if (self.code == 0):
return " 1"
elif (self.code == 1):
return "-1"
elif (self.code == 2):
return " i"
elif (self.code == 3):
return "-i"
elif (self.code == 4):
return " j"
elif (self.code == 5):
return "-j"
elif (self.code == 6):
return " k"
elif (self.code == 7):
return "-k"
else:
raise IOError
def __repr__(self):
return self.__str__()
def params_from_string(params_string):
# xxx check empty
return 0
def from_string(value_string, params_string):
not_used = params_from_string(params_string)
obj = quatu_t(0)
obj.scan(value_string)
return obj
#x = quatu_t(3)
#y = quatu_t(2)
#print x
#print y
#z = x * y
#print z
#z.scan("i")
#print z
#print
#for i in range(0, 8):
# for j in range(0, 8):
# x = quatu_t(i)
# y = quatu_t(j)
# z = x * y
# print x, y, z
# print
# ================================================================
import unittest
if __name__ == '__main__':
class test_cases(unittest.TestCase):
def test___init__(self):
pass # to be implemented
def test___mul__(self):
pass # to be implemented
def test___eq__(self):
pass # to be implemented
def test___ne__(self):
pass # to be implemented
def test___lt__(self):
pass # to be implemented
def test___le__(self):
pass # to be implemented
def test___gt__(self):
pass # to be implemented
def test___ge__(self):
pass # to be implemented
def test_inv(self):
pass # to be implemented
def test_scan(self):
pass # to be implemented
def test___str__(self):
pass # to be implemented
def test___repr__(self):
pass # to be implemented
def test_params_from_string(self):
pass # to be implemented
def test_from_string(self):
pass # to be implemented
# ----------------------------------------------------------------
unittest.main()