def euler(axis, angle): """ The quaternion representing of an Euler rotation. For example, if axis=2 the computed quaternion(s) will have components: q = (cos(angle/2), 0, 0, sin(angle/2)) Parameters ---------- axis : {0, 1, 2} The index of the cartesian axis of the rotation (x, y, z). angle : float or 1-d float array Angle of rotation, in radians. Returns ------- quat or G3VectorQuat, depending on ndim(angle). """ # Either angle or axis or both can be vectors. angle = np.asarray(angle) shape = np.broadcast(axis, angle).shape + (4,) c, s = np.cos(angle/2), np.sin(angle/2) q = np.zeros(shape) q[..., 0] = c q[..., axis+1] = s if len(shape) == 1: return quat(*q) return G3VectorQuat(q)
def ang_to_quat(alpha, delta, start=None, stop=None): """ Convert a set of angles (or vector of them) specified as a (longitude, latitude) pair to a pointing quaternion (or vector of them). If start and stop are defined, the return value for vectors will be a G3TimestreamQuat with start and stop times set to the provided values. """ alpha = numpy.asarray(alpha) delta = numpy.asarray(delta) # Copied from C code c_delta = numpy.cos(delta / G3Units.rad) q = numpy.column_stack( ( 0 * c_delta, # 0s with the right shape c_delta * numpy.cos(alpha / G3Units.rad), c_delta * numpy.sin(alpha / G3Units.rad), numpy.sin(delta / G3Units.rad), ) ) if len(q) == 1: return quat(q[0, 0], q[0, 1], q[0, 2], q[0, 3]) else: if start is not None: out = G3TimestreamQuat(q) out.start = start out.stop = stop else: return G3VectorQuat(q)
def ang_to_quat(alpha, delta): """ Convert a set of angles (or vector of them) specified as a (longitude, latitude) pair to a pointing quaternion (or vector of them). """ alpha = numpy.asarray(alpha) delta = numpy.asarray(delta) # Copied from C code c_delta = numpy.cos(delta / G3Units.rad) q = numpy.column_stack(( 0 * c_delta, # 0s with the right shape c_delta * numpy.cos(alpha / G3Units.rad), c_delta * numpy.sin(alpha / G3Units.rad), numpy.sin(delta / G3Units.rad), )) if len(q) == 1: return quat(q[0, 0], q[0, 1], q[0, 2], q[0, 3]) else: return G3VectorQuat(q)
#!/usr/bin/env python from spt3g import core import numpy as np a = core.quat(2,3,4,5) assert(a+a == 2*a) assert(a+a == a*2) assert(a*a == a**2) assert(a*a*a == a**3) b = core.G3VectorQuat([a, a**2, 2*a]) assert(b[0].a == 2) assert(b[0].b == 3) assert(b[0].c == 4) assert(b[0].d == 5) assert(b[1].a == -46) assert(b[1].b == 12) assert(b[1].c == 16) assert(b[1].d == 20) assert(b[2].c == 8) c = np.asarray(b) assert(c.shape == (3,4)) assert(core.quat(*c[0]) == a) assert(core.quat(*c[1]) == b[1])
#!/usr/bin/env python from spt3g import core, maps import numpy as np a = core.quat(2, 3, 4, 5) a2 = core.quat(2, 1, 8, 5) b = core.G3VectorQuat([a, a**2, 2 * a, a2]) assert (np.allclose(maps.quat_to_ang(a), maps.c_quat_to_ang_(a))) assert (np.allclose( maps.quat_to_ang(b), np.asarray([maps.c_quat_to_ang_(x) for x in b]).transpose())) angle = (.3, 0.4) angles = ((.3, 0.8), (0.4, 0.2)) q = maps.ang_to_quat(angle[0], angle[1]) p = maps.c_ang_to_quat_(angle[0], angle[1]) assert (np.allclose(core.G3VectorQuat([q]), core.G3VectorQuat([p]))) alpha, delta = list(zip(angles[0], angles[1])) assert (np.allclose( maps.ang_to_quat(alpha, delta), core.G3VectorQuat([maps.c_ang_to_quat_(a[0], a[1]) for a in angles]))) assert (np.allclose(maps.quat_to_ang(maps.c_ang_to_quat_(angle[0], angle[1])), angle))
#!/usr/bin/env python from spt3g import core import numpy as np a = core.quat(2, 3, 4, 5) assert (a + a == 2 * a) assert (a + a == a * 2) assert (a * a == a**2) assert (a * a * a == a**3) b = core.G3VectorQuat([a, a**2, 2 * a]) assert (b[0].a == 2) assert (b[0].b == 3) assert (b[0].c == 4) assert (b[0].d == 5) assert (b[1].a == -46) assert (b[1].b == 12) assert (b[1].c == 16) assert (b[1].d == 20) assert (b[2].c == 8) c = np.asarray(b) assert (c.shape == (3, 4)) assert (core.quat(*c[0]) == a) assert (core.quat(*c[1]) == b[1])