def test_constuctor_error(self): with self.assertRaises(IndexError): Q([[2, 3], [4, 5, 6]]) with self.assertRaises(ValueError): Q(['a', 'v', 'b']) with self.assertRaises(IndexError): Q([1, 2, 3, 4, 5])
def test_muldiv(self): self.assertEqual(a * b, Q([82, -76, -25, -15])) self.assertEqual(a * 0.9, Q([1.8, 3.6, 6.3, 8.1])) self.assertEqual(0.9 * a, Q([1.8, 3.6, 6.3, 8.1])) self.assertEqual(a / 2, Q([1, 2, 3.5, 4.5]))
def test_addsub(self): self.assertEqual(b + a, Q([-1, 0, 7, 1])) self.assertEqual(b - a, -Q([5, 8, 7, 17]))
def test_inverse(self): res = [0.0133333333333, -0.0266666666667, -0.0466666666667, -0.06] zero = a.inv() - Q(res) dtol = 0.000000000001 self.assertTrue(abs(zero) < dtol) self.assertEqual(a * a.inv(), one)
def test_conjugate(self): self.assertEqual(a.conj(), Q([2, -4, -7, -9]))
from __init__ import Q import unittest a = Q([2, 4, 7, 9]) b = Q([-3, -4, -0, -8]) c = Q([0, 0, 0, 0]) d = Q([]) i = Q([0, 1, 0, 0]) j = Q([0, 0, 1, 0]) k = Q([0, 0, 0, 1]) one = Q([1, 0, 0, 0]) # Constructor errors class TestConstructor(unittest.TestCase): def test_constuctor_error(self): with self.assertRaises(IndexError): Q([[2, 3], [4, 5, 6]]) with self.assertRaises(ValueError): Q(['a', 'v', 'b']) with self.assertRaises(IndexError): Q([1, 2, 3, 4, 5]) # Sequence operations class TestSequence(unittest.TestCase): def test_getitem(self): self.assertEqual(a[2], 7.0) def test_getitem_error(self):
print a % b # Calls mod, computes a cross product c = vector([0, 0, 0]) d = vector([]) # Comparisons and boolean outputs print bool(c), bool(d) # Calls nonzero, False for 0D and 0 mag vectors print bool(a) print a > b # Calls gt. Only mag compared. Others similar. print a == b # Calls eq. True only if all coeffs are same x = a.tonumpy('float32') print x # x is a numpy object print vector(x) # You can use numpy 1D arrays to construct a vector # Quarternions q0 = Q([2, 3, 4]) # Real part 0 print q0 q1 = Q([1, 2, 3, 4]) # Q using 4 floatable values print q1 print +q1 print -q1 print q1.conj() # Conjugate print q1.inv() # Inverse q2 = Q([4, 6, 7, 8]) print q1 * q2 print q1 * 9 print 9 * q1 print q1 / 8 print vector(q1)