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 288
|
import math, types
try:
import Numeric
NUMERIC = 1
except ImportError:
NUMERIC = 0
class quaternion:
def __init__(self, a = 0.0, b = 0.0, c = 0.0, d = 0.0, real = None, i = None, imag = None, j = None, k = None):
if real is not None:
a = real
if imag is not None:
b = imag
if i is not None:
b = i
if j is not None:
c = j
if k is not None:
d = k
if isinstance(a, quaternion):
self.__dict__['a'] = a.a
self.__dict__['b'] = a.b
self.__dict__['c'] = a.c
self.__dict__['d'] = a.d
elif type(a) is types.ComplexType:
self.__dict__['a'] = a.real
self.__dict__['b'] = a.imag
self.__dict__['c'] = 0.0
self.__dict__['d'] = 0.0
else:
self.__dict__['a'] = float(a)
self.__dict__['b'] = 0.0
self.__dict__['c'] = 0.0
self.__dict__['d'] = 0.0
if isinstance(b, quaternion):
self.__dict__['a'] = self.a - b.b
self.__dict__['b'] = self.b + b.a
self.__dict__['c'] = self.c - b.d
self.__dict__['d'] = self.d + b.c
elif type(b) is types.ComplexType:
self.__dict__['a'] = self.a - b.imag
self.__dict__['b'] = self.b + b.real
else:
self.__dict__['b'] = self.b + float(b)
if isinstance(c, quaternion):
self.__dict__['a'] = self.a - c.c
self.__dict__['b'] = self.b + c.d
self.__dict__['c'] = self.c + c.a
self.__dict__['d'] = self.d - c.b
elif type(c) is types.ComplexType:
self.__dict__['c'] = self.c + c.real
self.__dict__['d'] = self.d - c.imag
else:
self.__dict__['c'] = self.c + float(c)
if isinstance(d, quaternion):
self.__dict__['a'] = self.a - d.d
self.__dict__['b'] = self.b - d.c
self.__dict__['c'] = self.c + d.b
self.__dict__['d'] = self.d + d.a
elif type(d) is types.ComplexType:
self.__dict__['c'] = self.c + d.imag
self.__dict__['d'] = self.d + d.real
else:
self.__dict__['d'] = self.d + float(d)
def __getattr__(self, name):
global NUMERIC
if name == 'real':
return self.a
elif name in ('imag', 'i'):
return self.b
elif name == 'j':
return self.c
elif name == 'k':
return self.d
elif name == 'matrix3':
self.__dict__['matrix3'] = [[1.0 - 2.0*(self.c**2 + self.d**2),
2.0*(self.b*self.c - self.d*self.a),
2.0*(self.d*self.b + self.c*self.a)],
[2.0*(self.b*self.c + self.d*self.a),
1.0 - 2.0*(self.d**2 + self.b**2),
2.0*(self.c*self.d - self.b*self.a)],
[2.0*(self.d*self.b - self.c*self.a),
2.0*(self.c*self.d + self.b*self.a),
1.0 - 2.0 * (self.c**2 + self.b**2)]]
if NUMERIC:
self.__dict__['matrix3'] = Numeric.array(self.__dict__['matrix3'])
return self.__dict__['matrix3']
elif name == 'matrix4':
self.__dict__['matrix4'] = [[1.0 - 2.0*(self.c**2 + self.d**2),
2.0*(self.b*self.c - self.d*self.a),
2.0*(self.d*self.b + self.c*self.a),
0.0],
[2.0*(self.b*self.c + self.d*self.a),
1.0 - 2.0*(self.d**2 + self.b**2),
2.0*(self.c*self.d - self.b*self.a),
0.0],
[2.0*(self.d*self.b - self.c*self.a),
2.0*(self.c*self.d + self.b*self.a),
1.0 - 2.0 * (self.c**2 + self.b**2),
0.0],
[0.0,
0.0,
0.0,
1.0]]
if NUMERIC:
self.__dict__['matrix4'] = Numeric.array(self.__dict__['matrix4'])
return self.__dict__['matrix4']
else:
raise AttributeError, 'Attribute "%s" not found' % name
def __reset(self):
if self.__dict__.has_key('matrix3'):
del self.__dict__['matrix3']
if self.__dict__.has_key('matrix4'):
del self.__dict__['matrix4']
def __setattr__(self, name, value):
if name in ('a', 'real'):
self.__reset()
if isinstance(value, quaternion):
self.__dict__['a'] = value.a
self.__dict__['b'] = self.b + value.b
self.__dict__['c'] = self.c + value.c
self.__dict__['d'] = self.d + value.d
elif type(value) is types.ComplexType:
self.__dict__['a'] = value.real
self.__dict__['b'] = self.b + value.imag
else:
self.__dict__['a'] = float(value)
elif name in ('b', 'imag', 'i'):
self.__reset()
if isinstance(value, quaternion):
self.__dict__['a'] = self.a - value.b
self.__dict__['b'] = value.a
self.__dict__['c'] = self.c - value.d
self.__dict__['d'] = self.d + value.c
elif type(value) is types.ComplexType:
self.__dict__['a'] = self.a - value.imag
self.__dict__['b'] = value.real
else:
self.__dict__['b'] = float(value)
elif name in ('c', 'j'):
self.__reset()
if isinstance(value, quaternion):
self.__dict__['a'] = self.a - value.c
self.__dict__['b'] = self.b + value.d
self.__dict__['c'] = value.a
self.__dict__['d'] = self.d - value.b
elif type(value) is types.ComplexType:
self.__dict__['c'] = value.real
self.__dict__['d'] = self.d - value.imag
else:
self.__dict__['c'] = float(value)
elif name in ('d', 'k'):
self.__reset()
if isinstance(value, quaternion):
self.__dict__['a'] = self.a - value.d
self.__dict__['b'] = self.b - value.c
self.__dict__['c'] = self.c + value.b
self.__dict__['d'] = value.a
elif type(value) is types.ComplexType:
self.__dict__['c'] = self.c + value.imag
self.__dict__['d'] = value.real
else:
self.__dict__['d'] = float(value)
elif name in ('matrix3', 'matrix4'):
raise AttributeError, 'Attribute "%s" is read-only.' % name
else:
self.__dict__[name] = value
def __len__(self):
return 4
def __setitem__(self, index, value):
if index == 0:
self.a = value
elif index == 1:
self.b = value
elif index == 2:
self.c = value
elif index == 3:
self.d = value
else:
raise IndexError, 'Index %s out of range' % index
def __getitem__(self, index):
if index == 0:
return self.a
elif index == 1:
return self.b
elif index == 2:
return self.c
elif index == 3:
return self.d
else:
raise IndexError, 'Index %s out of range' % index
def __coerce__(self, x):
if isinstance(x, quaternion):
return (self, x)
x = complex(x)
return (self, quaternion(x.real, x.imag))
def __add__(self, x):
return quaternion(self.a + x.a, self.b + x.b, self.c + x.c, self.d + x.d)
def __sub__(self, x):
return quaternion(self.a - x.a, self.b - x.b, self.c - x.c, self.d - x.d)
def __mul__(self, x):
return quaternion(self.a*x.a - self.b*x.b - self.c*x.c - self.d*x.d,
self.a*x.b + self.b*x.a + self.c*x.d - self.d*x.c,
self.a*x.c + self.c*x.a + self.d*x.b - self.b*x.d,
self.a*x.d + self.d*x.a + self.b*x.c - self.c*x.b)
def __div__(self, x):
return self*x.conj()*(1.0/(x*x.conj()).a)
def __repr__(self):
return 'quaternion(%g, %g, %g, %g)' % (self.a, self.b, self.c, self.d)
def __str__(self):
m = []
for value, post in [(self.a, ''), (self.b, 'i'), (self.c, 'j'), (self.d, 'k')]:
if value:
m.append((value, post))
if len(m):
if m[0][0] == 1 and len(m[0][1]):
x = m[0][1]
else:
x = '%g%s' % m[0]
for value, post in m[1:]:
if value < 0:
x = x + ' - '
else:
x = x + ' + '
if value == 1:
x = x + post
else:
x = '%s%g%s' % (x, abs(value), post)
return x
return '0'
def __abs__(self):
return math.sqrt(self.a**2 + self.b**2 + self.c**2 + self.d**2)
def __pos__(self):
return quaternion(self.a, self.b, self.c, self.d)
def __neg__(self):
return quaternion(-self.a, -self.b, -self.c, -self.d)
def conj(self):
return quaternion(self.a, -self.b, -self.c, -self.d)
def normalize(self):
length = abs(self)
if length:
return self/length
else:
return self
|