本文整理汇总了Python中quaternion.Quaternion.norm方法的典型用法代码示例。如果您正苦于以下问题:Python Quaternion.norm方法的具体用法?Python Quaternion.norm怎么用?Python Quaternion.norm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类quaternion.Quaternion
的用法示例。
在下文中一共展示了Quaternion.norm方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_norm
# 需要导入模块: from quaternion import Quaternion [as 别名]
# 或者: from quaternion.Quaternion import norm [as 别名]
def test_norm(self):
r = randomElements()
q1 = Quaternion(*r)
q2 = Quaternion.random()
self.assertEqual(q1.norm(), np.linalg.norm(np.array(r)))
self.assertEqual(q1.magnitude(), np.linalg.norm(np.array(r)))
# Multiplicative norm
self.assertAlmostEqual((q1 * q2).norm(), q1.norm() * q2.norm(), ALMOST_EQUAL_TOLERANCE)
# Scaled norm
for s in [30.0, 0.3, -2, -4.7]:
self.assertAlmostEqual((q1 * s).norm(), q1.norm() * abs(s), ALMOST_EQUAL_TOLERANCE)
示例2: test_init_from_explicit_rotation_params
# 需要导入模块: from quaternion import Quaternion [as 别名]
# 或者: from quaternion.Quaternion import norm [as 别名]
def test_init_from_explicit_rotation_params(self):
vx = random()
vy = random()
vz = random()
theta = random() * 2.0 * pi
v1 = (vx, vy, vz) # tuple format
v2 = [vx, vy, vz] # list format
v3 = np.array(v2) # array format
q1 = Quaternion(axis=v1, angle=theta)
q2 = Quaternion(axis=v2, angle=theta)
q3 = Quaternion(axis=v3, angle=theta)
# normalise v to a unit vector
v3 = v3 / np.linalg.norm(v3)
q4 = Quaternion(angle=theta, axis=v3)
# Construct the true quaternion
t = theta / 2.0
a = cos(t)
b = v3[0] * sin(t)
c = v3[1] * sin(t)
d = v3[2] * sin(t)
truth = Quaternion(a, b, c, d)
self.assertEqual(q1, truth)
self.assertEqual(q2, truth)
self.assertEqual(q3, truth)
self.assertEqual(q4, truth)
# Result should be a versor (Unit Quaternion)
self.assertAlmostEqual(q1.norm(), 1.0, ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(q2.norm(), 1.0, ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(q3.norm(), 1.0, ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(q4.norm(), 1.0, ALMOST_EQUAL_TOLERANCE)
with self.assertRaises(ValueError):
q = Quaternion(angle=theta)
q = Quaternion(axis=v1)
q = Quaternion(axis=[b, c], angle=theta)
q = Quaternion(axis=(b, c, d, d), angle=theta)
with self.assertRaises(ZeroDivisionError):
q = Quaternion(axis=[0., 0., 0.], angle=theta)
示例3: test_normalisation
# 需要导入模块: from quaternion import Quaternion [as 别名]
# 或者: from quaternion.Quaternion import norm [as 别名]
def test_normalisation(self): # normalise to unit quaternion
r = randomElements()
q1 = Quaternion(*r)
v = q1.unit()
n = q1.normalised()
if q1 == Quaternion(0): # small chance with random generation
return # a 0 quaternion does not normalise
# Test normalised objects are unit quaternions
np.testing.assert_almost_equal(v.q, q1.elements() / q1.norm(), decimal=ALMOST_EQUAL_TOLERANCE)
np.testing.assert_almost_equal(n.q, q1.elements() / q1.norm(), decimal=ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(v.norm(), 1.0, ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(n.norm(), 1.0, ALMOST_EQUAL_TOLERANCE)
# Test axis and angle remain the same
np.testing.assert_almost_equal(q1.axis(), v.axis(), decimal=ALMOST_EQUAL_TOLERANCE)
np.testing.assert_almost_equal(q1.axis(), n.axis(), decimal=ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(q1.angle(), v.angle(), ALMOST_EQUAL_TOLERANCE)
self.assertAlmostEqual(q1.angle(), n.angle(), ALMOST_EQUAL_TOLERANCE)
# Test special case where q is zero
q2 = Quaternion(0)
self.assertEqual(q2, q2.normalised())
示例4: print
# 需要导入模块: from quaternion import Quaternion [as 别名]
# 或者: from quaternion.Quaternion import norm [as 别名]
print()
print("Products of components:")
print("o*i = {0}\ti*o = {1}".format(o*i, i*o))
print("o*j = {0}\tj*o = {1}".format(o*j, j*o))
print("o*k = {0}\tk*o = {1}".format(o*k, k*o))
print("i*j = {0}\tj*i = {1}".format(i*j, j*i))
print("i*k = {0}\tk*i = {1}".format(i*k, k*i))
print("j*k = {0}\tk*j = {1}".format(j*k, k*j))
print()
p = Quaternion(i)
p.setScalar(-3).setJ(2).setK(-1)
print("p=({0}, {1}, {2}, {3})".format(p.getScalar(), p.getI(), p.getJ(), p.getK()))
# sqrt(15) = 3.87298
print("||p|| = {0} (correct: 3.87298)".format(p.norm()))
q = p.reciprocal()
qc = "-0.2-0.0666666666667i-0.133333333333j+0.0666666666667k"
print("p**(-1) = {0}\n(correct: {1})".format(q, qc))
print("p*p**(-1) = {0}".format(p*q))
print("p**(-1)*p = {0}".format(q*p))
print()
q = Quaternion(1, -2, 3, -4)
print("q = {0}".format(q))
print("p+q = {0}".format(p+q))
print("p-q = {0}".format(p-q))
print("p*q = {0}".format(p*q)) # -11+2*i-j+18*k
print("q*p = {0}".format(q*p)) # -11+12*i-13*j+4*k