本文整理汇总了Python中numpy.quaternion方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.quaternion方法的具体用法?Python numpy.quaternion怎么用?Python numpy.quaternion使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.quaternion方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: slerp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def slerp(R1, R2, t1, t2, t_out):
"""Spherical linear interpolation of rotors
This function uses a simpler interface than the more fundamental
`slerp_evaluate` and `slerp_vectorized` functions. The latter
are fast, being implemented at the C level, but take input `tau`
instead of time. This function adjusts the time accordingly.
Parameters
----------
R1: quaternion
Quaternion at beginning of interpolation
R2: quaternion
Quaternion at end of interpolation
t1: float
Time corresponding to R1
t2: float
Time corresponding to R2
t_out: float or array of floats
Times to which the rotors should be interpolated
"""
tau = (t_out-t1)/(t2-t1)
return np.slerp_vectorized(R1, R2, tau)
示例2: as_rotation_vector
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def as_rotation_vector(q):
"""Convert input quaternion to the axis-angle representation
Note that if any of the input quaternions has norm zero, no error is
raised, but NaNs will appear in the output.
Parameters
----------
q: quaternion or array of quaternions
The quaternion(s) need not be normalized, but must all be nonzero
Returns
-------
rot: float array
Output shape is q.shape+(3,). Each vector represents the axis of
the rotation, with norm proportional to the angle of the rotation in
radians.
"""
return as_float_array(2*np.log(np.normalized(q)))[..., 1:]
示例3: from_rotation_vector
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def from_rotation_vector(rot):
"""Convert input 3-vector in axis-angle representation to unit quaternion
Parameters
----------
rot: (Nx3) float array
Each vector represents the axis of the rotation, with norm
proportional to the angle of the rotation in radians.
Returns
-------
q: array of quaternions
Unit quaternions resulting in rotations corresponding to input
rotations. Output shape is rot.shape[:-1].
"""
rot = np.array(rot, copy=False)
quats = np.zeros(rot.shape[:-1]+(4,))
quats[..., 1:] = rot[...]/2
quats = as_quat_array(quats)
return np.exp(quats)
示例4: mean_rotor_in_chordal_metric
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def mean_rotor_in_chordal_metric(R, t=None):
"""Return rotor that is closest to all R in the least-squares sense
This can be done (quasi-)analytically because of the simplicity of
the chordal metric function. It is assumed that the input R values
all are normalized (or at least have the same norm).
Note that the `t` argument is optional. If it is present, the
times are used to weight the corresponding integral. If it is not
present, a simple sum is used instead (which may be slightly
faster). However, because a spline is used to do this integral,
the number of input points must be at least 4 (one more than the
degree of the spline).
"""
import numpy as np
from . import as_float_array
from .calculus import definite_integral
if t is None:
return np.sum(R).normalized()
if len(t) < 4 or len(R) < 4:
raise ValueError('Input arguments must have length greater than 3; their lengths are {0} and {1}.'.format(len(R), len(t)))
mean = definite_integral(as_float_array(R), t)
return np.quaternion(*mean).normalized()
示例5: test_isclose
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_isclose():
from quaternion import x, y
assert np.array_equal(quaternion.isclose([1e10*x, 1e-7*y], [1.00001e10*x, 1e-8*y], rtol=1.e-5, atol=2.e-8),
np.array([True, False]))
assert np.array_equal(quaternion.isclose([1e10*x, 1e-8*y], [1.00001e10*x, 1e-9*y], rtol=1.e-5, atol=2.e-8),
np.array([True, True]))
assert np.array_equal(quaternion.isclose([1e10*x, 1e-8*y], [1.0001e10*x, 1e-9*y], rtol=1.e-5, atol=2.e-8),
np.array([False, True]))
assert np.array_equal(quaternion.isclose([x, np.nan*y], [x, np.nan*y]),
np.array([True, False]))
assert np.array_equal(quaternion.isclose([x, np.nan*y], [x, np.nan*y], equal_nan=True),
np.array([True, True]))
np.random.seed(1234)
a = quaternion.as_quat_array(np.random.random((3, 5, 4)))
assert quaternion.allclose(1e10 * a, 1.00001e10 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
assert quaternion.allclose(1e-7 * a, 1e-8 * a, rtol=1.e-5, atol=2.e-8) == False
assert quaternion.allclose(1e10 * a, 1.00001e10 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
assert quaternion.allclose(1e-8 * a, 1e-9 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
assert quaternion.allclose(1e10 * a, 1.0001e10 * a, rtol=1.e-5, atol=2.e-8) == False
assert quaternion.allclose(1e-8 * a, 1e-9 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
assert quaternion.allclose(np.nan * a, np.nan * a) == False
assert quaternion.allclose(np.nan * a, np.nan * a, equal_nan=True, verbose=True) == True
示例6: test_from_spherical_coords
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_from_spherical_coords():
np.random.seed(1843)
random_angles = [[np.random.uniform(-np.pi, np.pi), np.random.uniform(-np.pi, np.pi)]
for i in range(5000)]
for vartheta, varphi in random_angles:
q = quaternion.from_spherical_coords(vartheta, varphi)
assert abs((np.quaternion(0, 0, 0, varphi / 2.).exp() * np.quaternion(0, 0, vartheta / 2., 0).exp())
- q) < 1.e-15
xprime = q * quaternion.x * q.inverse()
yprime = q * quaternion.y * q.inverse()
zprime = q * quaternion.z * q.inverse()
nhat = np.quaternion(0.0, math.sin(vartheta)*math.cos(varphi), math.sin(vartheta)*math.sin(varphi),
math.cos(vartheta))
thetahat = np.quaternion(0.0, math.cos(vartheta)*math.cos(varphi), math.cos(vartheta)*math.sin(varphi),
-math.sin(vartheta))
phihat = np.quaternion(0.0, -math.sin(varphi), math.cos(varphi), 0.0)
assert abs(xprime - thetahat) < 1.e-15
assert abs(yprime - phihat) < 1.e-15
assert abs(zprime - nhat) < 1.e-15
assert np.max(np.abs(quaternion.from_spherical_coords(random_angles)
- np.array([quaternion.from_spherical_coords(vartheta, varphi)
for vartheta, varphi in random_angles]))) < 1.e-15
示例7: test_as_euler_angles
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_as_euler_angles():
np.random.seed(1843)
random_angles = [[np.random.uniform(-np.pi, np.pi),
np.random.uniform(-np.pi, np.pi),
np.random.uniform(-np.pi, np.pi)]
for i in range(5000)]
for alpha, beta, gamma in random_angles:
R1 = quaternion.from_euler_angles(alpha, beta, gamma)
R2 = quaternion.from_euler_angles(*list(quaternion.as_euler_angles(R1)))
d = quaternion.rotation_intrinsic_distance(R1, R2)
assert d < 6e3*eps, ((alpha, beta, gamma), R1, R2, d) # Can't use allclose here; we don't care about rotor sign
q0 = quaternion.quaternion(0, 0.6, 0.8, 0)
assert q0.norm() == 1.0
assert abs(q0 - quaternion.from_euler_angles(*list(quaternion.as_euler_angles(q0)))) < 1.e-15
# Unary bool returners
示例8: test_quaternion_parity_conjugates
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_quaternion_parity_conjugates(Qs):
for q in Qs[Qs_finite]:
assert q.x_parity_conjugate() == np.quaternion(q.w, q.x, -q.y, -q.z)
assert q.y_parity_conjugate() == np.quaternion(q.w, -q.x, q.y, -q.z)
assert q.z_parity_conjugate() == np.quaternion(q.w, -q.x, -q.y, q.z)
assert q.parity_conjugate() == np.quaternion(q.w, q.x, q.y, q.z)
assert np.array_equal(np.x_parity_conjugate(Qs[Qs_finite]),
np.array([q.x_parity_conjugate() for q in Qs[Qs_finite]]))
assert np.array_equal(np.y_parity_conjugate(Qs[Qs_finite]),
np.array([q.y_parity_conjugate() for q in Qs[Qs_finite]]))
assert np.array_equal(np.z_parity_conjugate(Qs[Qs_finite]),
np.array([q.z_parity_conjugate() for q in Qs[Qs_finite]]))
assert np.array_equal(np.parity_conjugate(Qs[Qs_finite]), np.array([q.parity_conjugate() for q in Qs[Qs_finite]]))
# Quaternion-quaternion binary quaternion returners
示例9: test_quaternion_multiply_ufunc
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_quaternion_multiply_ufunc(Qs):
ufunc_binary_utility(np.array([quaternion.one]), Qs[Qs_finite], operator.mul)
ufunc_binary_utility(Qs[Qs_finite], np.array([quaternion.one]), operator.mul)
ufunc_binary_utility(np.array([1.0]), Qs[Qs_finite], operator.mul)
ufunc_binary_utility(Qs[Qs_finite], np.array([1.0]), operator.mul)
ufunc_binary_utility(np.array([1]), Qs[Qs_finite], operator.mul)
ufunc_binary_utility(Qs[Qs_finite], np.array([1]), operator.mul)
ufunc_binary_utility(np.array([0.0]), Qs[Qs_finite], operator.mul)
ufunc_binary_utility(Qs[Qs_finite], np.array([0.0]), operator.mul)
ufunc_binary_utility(np.array([0]), Qs[Qs_finite], operator.mul)
ufunc_binary_utility(Qs[Qs_finite], np.array([0]), operator.mul)
ufunc_binary_utility(np.array([-3, -2.3, -1.2, -1.0, 0.0, 0, 1.0, 1, 1.2, 2.3, 3]),
Qs[Qs_finite], operator.mul)
ufunc_binary_utility(Qs[Qs_finite],
np.array([-3, -2.3, -1.2, -1.0, 0.0, 0, 1.0, 1, 1.2, 2.3, 3]), operator.mul)
ufunc_binary_utility(Qs[Qs_finite], Qs[Qs_finite], operator.mul)
示例10: test_quaternion_divide_ufunc
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_quaternion_divide_ufunc(Qs):
ufunc_binary_utility(np.array([quaternion.one]), Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(Qs[Qs_finite], np.array([quaternion.one]), operator.truediv)
ufunc_binary_utility(np.array([1.0]), Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(Qs[Qs_finite], np.array([1.0]), operator.truediv)
ufunc_binary_utility(np.array([1]), Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(Qs[Qs_finite], np.array([1]), operator.truediv)
ufunc_binary_utility(np.array([0.0]), Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(np.array([0]), Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(np.array([-3, -2.3, -1.2, -1.0, 0.0, 0, 1.0, 1, 1.2, 2.3, 3]),
Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(Qs[Qs_finitenonzero],
np.array([-3, -2.3, -1.2, -1.0, 1.0, 1, 1.2, 2.3, 3]), operator.truediv)
ufunc_binary_utility(Qs[Qs_finite], Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(Qs[Qs_finite], Qs[Qs_finitenonzero], operator.floordiv)
ufunc_binary_utility(Qs[Qs_finitenonzero], Qs[Qs_finitenonzero], operator.truediv)
ufunc_binary_utility(Qs[Qs_finitenonzero], Qs[Qs_finitenonzero], operator.floordiv)
示例11: clean_color
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def clean_color(color):
if color == 'red':
color = [255, 0, 0]
elif color == 'green':
color = [0, 255, 0]
elif color == 'blue':
color = [0, 0, 255]
elif color == 'yellow':
color = [255, 255, 0]
elif color == 'pink':
color = [255, 0, 255]
elif color == 'cyan':
color = [0, 255, 255]
elif color == 'white':
color = [255, 255, 255]
return color
####### Dataset generation
# https://stackoverflow.com/questions/31600717/how-to-generate-a-random-quaternion-quickly
# http://planning.cs.uiuc.edu/node198.html
示例12: test_Wigner_D_element_values
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_Wigner_D_element_values(special_angles, ell_max):
LMpM = sf.LMpM_range_half_integer(0, ell_max // 2)
# Compare with more explicit forms given in Euler angles
print("")
for alpha in special_angles:
print("\talpha={0}".format(alpha)) # Need to show some progress to Travis
for beta in special_angles:
print("\t\tbeta={0}".format(beta))
for gamma in special_angles:
a = np.conjugate(np.array([slow_Wigner_D_element(alpha, beta, gamma, ell, mp, m) for ell,mp,m in LMpM]))
b = sf.Wigner_D_element(quaternion.from_euler_angles(alpha, beta, gamma), LMpM)
# if not np.allclose(a, b,
# atol=ell_max ** 6 * precision_Wigner_D_element,
# rtol=ell_max ** 6 * precision_Wigner_D_element):
# for i in range(min(a.shape[0], 100)):
# print(LMpM[i], "\t", abs(a[i]-b[i]), "\t\t", a[i], "\t", b[i])
assert np.allclose(a, b,
atol=ell_max ** 6 * precision_Wigner_D_element,
rtol=ell_max ** 6 * precision_Wigner_D_element)
示例13: test_SWSH_spin_behavior
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_SWSH_spin_behavior(Rs, special_angles, ell_max):
# We expect that the SWSHs behave according to
# sYlm( R * exp(gamma*z/2) ) = sYlm(R) * exp(-1j*s*gamma)
# See http://moble.github.io/spherical_functions/SWSHs.html#fn:2
# for a more detailed explanation
# print("")
for i, R in enumerate(Rs):
# print("\t{0} of {1}: R = {2}".format(i, len(Rs), R))
for gamma in special_angles:
for ell in range(ell_max + 1):
for s in range(-ell, ell + 1):
LM = np.array([[ell, m] for m in range(-ell, ell + 1)])
Rgamma = R * np.quaternion(math.cos(gamma / 2.), 0, 0, math.sin(gamma / 2.))
sYlm1 = sf.SWSH(Rgamma, s, LM)
sYlm2 = sf.SWSH(R, s, LM) * cmath.exp(-1j * s * gamma)
# print(R, gamma, ell, s, np.max(np.abs(sYlm1-sYlm2)))
assert np.allclose(sYlm1, sYlm2, atol=ell ** 6 * precision_SWSH, rtol=ell ** 6 * precision_SWSH)
示例14: test_SWSH_grid
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def test_SWSH_grid(special_angles, ell_max):
LM = sf.LM_range(0, ell_max)
# Test flat array arrangement
R_grid = np.array([quaternion.from_euler_angles(alpha, beta, gamma).normalized()
for alpha in special_angles
for beta in special_angles
for gamma in special_angles])
for s in range(-ell_max + 1, ell_max):
values_explicit = np.array([sf.SWSH(R, s, LM) for R in R_grid])
values_grid = sf.SWSH_grid(R_grid, s, ell_max)
assert np.array_equal(values_explicit, values_grid)
# Test nested array arrangement
R_grid = np.array([[[quaternion.from_euler_angles(alpha, beta, gamma)
for alpha in special_angles]
for beta in special_angles]
for gamma in special_angles])
for s in range(-ell_max + 1, ell_max):
values_explicit = np.array([[[sf.SWSH(R, s, LM) for R in R1] for R1 in R2] for R2 in R_grid])
values_grid = sf.SWSH_grid(R_grid, s, ell_max)
assert np.array_equal(values_explicit, values_grid)
示例15: quaternion_from_two_vectors
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import quaternion [as 别名]
def quaternion_from_two_vectors(v0: np.array, v1: np.array) -> np.quaternion:
r"""Computes the quaternion representation of v1 using v0 as the origin.
"""
v0 = v0 / np.linalg.norm(v0)
v1 = v1 / np.linalg.norm(v1)
c = v0.dot(v1)
# Epsilon prevents issues at poles.
if c < (-1 + EPSILON):
c = max(c, -1)
m = np.stack([v0, v1], 0)
_, _, vh = np.linalg.svd(m, full_matrices=True)
axis = vh.T[:, 2]
w2 = (1 + c) * 0.5
w = np.sqrt(w2)
axis = axis * np.sqrt(1 - w2)
return np.quaternion(w, *axis)
axis = np.cross(v0, v1)
s = np.sqrt((1 + c) * 2)
return np.quaternion(s * 0.5, *(axis / s))