代数#

介绍#

SymPy的代数模块支持四元数上的基本代数运算。

四元数参考#

本节列出了代数模块实现的类。

class sympy.algebras.Quaternion(a=0, b=0, c=0, d=0, real_field=True, norm=None)[源代码]#

Provides basic quaternion operations. Quaternion objects can be instantiated as Quaternion(a, b, c, d) as in \(q = a + bi + cj + dk\).

参数:

norm : None or number

Pre-defined quaternion norm. If a value is given, Quaternion.norm returns this pre-defined value instead of calculating the norm

实例

>>> from sympy import Quaternion
>>> q = Quaternion(1, 2, 3, 4)
>>> q
1 + 2*i + 3*j + 4*k

Quaternions over complex fields can be defined as:

>>> from sympy import Quaternion
>>> from sympy import symbols, I
>>> x = symbols('x')
>>> q1 = Quaternion(x, x**3, x, x**2, real_field = False)
>>> q2 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
>>> q1
x + x**3*i + x*j + x**2*k
>>> q2
(3 + 4*I) + (2 + 5*I)*i + 0*j + (7 + 8*I)*k

Defining symbolic unit quaternions:

>>> from sympy import Quaternion
>>> from sympy.abc import w, x, y, z
>>> q = Quaternion(w, x, y, z, norm=1)
>>> q
w + x*i + y*j + z*k
>>> q.norm()
1

工具书类

add(other)[源代码]#

添加四元数。

参数:

其他 :四元数

要添加到当前(自)四元数的四元数。

返回:

四元数

将self添加到other后得到的四元数

实例

>>> from sympy import Quaternion
>>> from sympy import symbols
>>> q1 = Quaternion(1, 2, 3, 4)
>>> q2 = Quaternion(5, 6, 7, 8)
>>> q1.add(q2)
6 + 8*i + 10*j + 12*k
>>> q1 + 5
6 + 2*i + 3*j + 4*k
>>> x = symbols('x', real = True)
>>> q1.add(x)
(x + 1) + 2*i + 3*j + 4*k

复域上的四元数:

>>> from sympy import Quaternion
>>> from sympy import I
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
>>> q3.add(2 + 3*I)
(5 + 7*I) + (2 + 5*I)*i + 0*j + (7 + 8*I)*k
angle()[源代码]#

Returns the angle of the quaternion measured in the real-axis plane.

解释

Given a quaternion \(q = a + bi + cj + dk\) where \(a\), \(b\), \(c\) and \(d\) are real numbers, returns the angle of the quaternion given by

\[\theta := 2 \operatorname{atan_2}\left(\sqrt{b^2 + c^2 + d^2}, {a}\right)\]

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(1, 4, 4, 4)
>>> q.angle()
2*atan(4*sqrt(3))
arc_coplanar(other)[源代码]#

Returns True if the transformation arcs represented by the input quaternions happen in the same plane.

参数:

other : a Quaternion

返回:

True : if the planes of the two quaternions are the same, apart from its orientation/sign.

False : if the planes of the two quaternions are not the same, apart from its orientation/sign.

None : if plane of either of the quaternion is unknown.

解释

Two quaternions are said to be coplanar (in this arc sense) when their axes are parallel. The plane of a quaternion is the one normal to its axis.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q1 = Quaternion(1, 4, 4, 4)
>>> q2 = Quaternion(3, 8, 8, 8)
>>> Quaternion.arc_coplanar(q1, q2)
True
>>> q1 = Quaternion(2, 8, 13, 12)
>>> Quaternion.arc_coplanar(q1, q2)
False
axis()[源代码]#

Returns \(\mathbf{Ax}(q)\), the axis of the quaternion \(q\).

解释

Given a quaternion \(q = a + bi + cj + dk\), returns \(\mathbf{Ax}(q)\) i.e., the versor of the vector part of that quaternion equal to \(\mathbf{U}[\mathbf{V}(q)]\). The axis is always an imaginary unit with square equal to \(-1 + 0i + 0j + 0k\).

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(1, 1, 1, 1)
>>> q.axis()
0 + sqrt(3)/3*i + sqrt(3)/3*j + sqrt(3)/3*k

参见

vector_part

exp()[源代码]#

Returns the exponential of \(q\), given by \(e^q\).

返回:

四元数

The exponential of the quaternion.

实例

>>> from sympy import Quaternion
>>> q = Quaternion(1, 2, 3, 4)
>>> q.exp()
E*cos(sqrt(29))
+ 2*sqrt(29)*E*sin(sqrt(29))/29*i
+ 3*sqrt(29)*E*sin(sqrt(29))/29*j
+ 4*sqrt(29)*E*sin(sqrt(29))/29*k
classmethod from_Matrix(elements)[源代码]#

Returns quaternion from elements of a column vector`. If vector_only is True, returns only imaginary part as a Matrix of length 3.

参数:

elements : Matrix, list or tuple of length 3 or 4. If length is 3,

assume real part is zero. Default value: False

返回:

四元数

A quaternion created from the input elements.

实例

>>> from sympy import Quaternion
>>> from sympy.abc import a, b, c, d
>>> q = Quaternion.from_Matrix([a, b, c, d])
>>> q
a + b*i + c*j + d*k
>>> q = Quaternion.from_Matrix([b, c, d])
>>> q
0 + b*i + c*j + d*k
classmethod from_axis_angle(vector, angle)[源代码]#

返回给定轴和旋转角度的旋转四元数。

参数:

矢量 :三个数字的元组

给定轴的矢量表示。

:编号

轴旋转的角度(以弧度为单位)。

返回:

四元数

从给定的轴和旋转角度计算的归一化旋转四元数。

实例

>>> from sympy import Quaternion
>>> from sympy import pi, sqrt
>>> q = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), 2*pi/3)
>>> q
1/2 + 1/2*i + 1/2*j + 1/2*k
classmethod from_euler(angles, seq)[源代码]#

Returns quaternion equivalent to rotation represented by the Euler angles, in the sequence defined by seq.

参数:

angles : list, tuple or Matrix of 3 numbers

The Euler angles (in radians).

seq : string of length 3

Represents the sequence of rotations. For extrinsic rotations, seq must be all lowercase and its elements must be from the set {'x', 'y', 'z'} For intrinsic rotations, seq must be all uppercase and its elements must be from the set {'X', 'Y', 'Z'}

返回:

四元数

The normalized rotation quaternion calculated from the Euler angles in the given sequence.

实例

>>> from sympy import Quaternion
>>> from sympy import pi
>>> q = Quaternion.from_euler([pi/2, 0, 0], 'xyz')
>>> q
sqrt(2)/2 + sqrt(2)/2*i + 0*j + 0*k
>>> q = Quaternion.from_euler([0, pi/2, pi] , 'zyz')
>>> q
0 + (-sqrt(2)/2)*i + 0*j + sqrt(2)/2*k
>>> q = Quaternion.from_euler([0, pi/2, pi] , 'ZYZ')
>>> q
0 + sqrt(2)/2*i + 0*j + sqrt(2)/2*k
classmethod from_rotation_matrix(M)[源代码]#

返回矩阵的等效四元数。只有当矩阵是特殊正交(正交且det(M)=1)时,四元数才会被规范化。

参数:

M :矩阵

要转换为等效四元数的输入矩阵。M必须是特殊正交(正交且det(M)=1)才能使四元数标准化。

返回:

四元数

与给定矩阵等价的四元数。

实例

>>> from sympy import Quaternion
>>> from sympy import Matrix, symbols, cos, sin, trigsimp
>>> x = symbols('x')
>>> M = Matrix([[cos(x), -sin(x), 0], [sin(x), cos(x), 0], [0, 0, 1]])
>>> q = trigsimp(Quaternion.from_rotation_matrix(M))
>>> q
sqrt(2)*sqrt(cos(x) + 1)/2 + 0*i + 0*j + sqrt(2 - 2*cos(x))*sign(sin(x))/2*k
index_vector()[源代码]#

Returns the index vector of the quaternion.

返回:

Quaternion: representing index vector of the provided quaternion.

解释

The index vector is given by \(\mathbf{T}(q)\), the norm (or magnitude) of the quaternion \(q\), multiplied by \(\mathbf{Ax}(q)\), the axis of \(q\).

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(2, 4, 2, 4)
>>> q.index_vector()
0 + 4*sqrt(10)/3*i + 2*sqrt(10)/3*j + 4*sqrt(10)/3*k

参见

axis, norm

integrate(*args)[源代码]#

计算四元数的积分。

返回:

四元数

四元数(self)与给定变量的积分。

实例

四元数不定积分:

>>> from sympy import Quaternion
>>> from sympy.abc import x
>>> q = Quaternion(1, 2, 3, 4)
>>> q.integrate(x)
x + 2*x*i + 3*x*j + 4*x*k

四元数定积分:

>>> from sympy import Quaternion
>>> from sympy.abc import x
>>> q = Quaternion(1, 2, 3, 4)
>>> q.integrate((x, 1, 5))
4 + 8*i + 12*j + 16*k
inverse()[源代码]#

返回四元数的倒数。

is_pure()[源代码]#

Returns true if the quaternion is pure, false if the quaternion is not pure or returns none if it is unknown.

解释

A pure quaternion (also a vector quaternion) is a quaternion with scalar part equal to 0.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(0, 8, 13, 12)
>>> q.is_pure()
True

参见

scalar_part

is_zero_quaternion()[源代码]#

Returns true if the quaternion is a zero quaternion or false if it is not a zero quaternion and None if the value is unknown.

解释

A zero quaternion is a quaternion with both scalar part and vector part equal to 0.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(1, 0, 0, 0)
>>> q.is_zero_quaternion()
False
>>> q = Quaternion(0, 0, 0, 0)
>>> q.is_zero_quaternion()
True
mensor()[源代码]#

Returns the natural logarithm of the norm(magnitude) of the quaternion.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(2, 4, 2, 4)
>>> q.mensor()
log(2*sqrt(10))
>>> q.norm()
2*sqrt(10)

参见

norm

mul(other)[源代码]#

乘以四元数。

参数:

其他 :四元数或符号

要乘以当前(自身)四元数的四元数。

返回:

四元数

自相乘后得到的四元数

实例

>>> from sympy import Quaternion
>>> from sympy import symbols
>>> q1 = Quaternion(1, 2, 3, 4)
>>> q2 = Quaternion(5, 6, 7, 8)
>>> q1.mul(q2)
(-60) + 12*i + 30*j + 24*k
>>> q1.mul(2)
2 + 4*i + 6*j + 8*k
>>> x = symbols('x', real = True)
>>> q1.mul(x)
x + 2*x*i + 3*x*j + 4*x*k

复域上的四元数:

>>> from sympy import Quaternion
>>> from sympy import I
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
>>> q3.mul(2 + 3*I)
(2 + 3*I)*(3 + 4*I) + (2 + 3*I)*(2 + 5*I)*i + 0*j + (2 + 3*I)*(7 + 8*I)*k
norm()[源代码]#

返回四元数的范数。

normalize()[源代码]#

返回四元数的规范化形式。

orthogonal(other)[源代码]#

Returns the orthogonality of two quaternions.

参数:

other : a Quaternion

返回:

True : if the two pure quaternions seen as 3D vectors are orthogonal.

False : if the two pure quaternions seen as 3D vectors are not orthogonal.

None : if the two pure quaternions seen as 3D vectors are orthogonal is unknown.

解释

Two pure quaternions are called orthogonal when their product is anti-commutative.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(0, 4, 4, 4)
>>> q1 = Quaternion(0, 8, 8, 8)
>>> q.orthogonal(q1)
False
>>> q1 = Quaternion(0, 2, 2, 0)
>>> q = Quaternion(0, 2, -2, 0)
>>> q.orthogonal(q1)
True
parallel(other)[源代码]#

Returns True if the two pure quaternions seen as 3D vectors are parallel.

参数:

other : a Quaternion

返回:

True : if the two pure quaternions seen as 3D vectors are parallel.

False : if the two pure quaternions seen as 3D vectors are not parallel.

None : if the two pure quaternions seen as 3D vectors are parallel is unknown.

解释

Two pure quaternions are called parallel when their vector product is commutative which implies that the quaternions seen as 3D vectors have same direction.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(0, 4, 4, 4)
>>> q1 = Quaternion(0, 8, 8, 8)
>>> q.parallel(q1)
True
>>> q1 = Quaternion(0, 8, 13, 12)
>>> q.parallel(q1)
False
pow(p)[源代码]#

找到四元数的pth幂。

参数:

p :内景

要应用于四元数的幂。

返回:

四元数

返回当前四元数的p次幂。如果p=-1,则返回倒数。

实例

>>> from sympy import Quaternion
>>> q = Quaternion(1, 2, 3, 4)
>>> q.pow(4)
668 + (-224)*i + (-336)*j + (-448)*k
pow_cos_sin(p)[源代码]#

以cos-sin形式计算pth幂。

参数:

p :内景

要应用于四元数的幂。

返回:

四元数

余弦形式的p次方。

实例

>>> from sympy import Quaternion
>>> q = Quaternion(1, 2, 3, 4)
>>> q.pow_cos_sin(4)
900*cos(4*acos(sqrt(30)/30))
+ 1800*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*i
+ 2700*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*j
+ 3600*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*k
property product_matrix_left#

Returns 4 x 4 Matrix equivalent to a Hamilton product from the left. This can be useful when treating quaternion elements as column vectors. Given a quaternion \(q = a + bi + cj + dk\) where a, b, c and d are real numbers, the product matrix from the left is:

\[\begin{split}M = \begin{bmatrix} a &-b &-c &-d \\ b & a &-d & c \\ c & d & a &-b \\ d &-c & b & a \end{bmatrix}\end{split}\]

实例

>>> from sympy import Quaternion
>>> from sympy.abc import a, b, c, d
>>> q1 = Quaternion(1, 0, 0, 1)
>>> q2 = Quaternion(a, b, c, d)
>>> q1.product_matrix_left
Matrix([
[1, 0,  0, -1],
[0, 1, -1,  0],
[0, 1,  1,  0],
[1, 0,  0,  1]])
>>> q1.product_matrix_left * q2.to_Matrix()
Matrix([
[a - d],
[b - c],
[b + c],
[a + d]])

This is equivalent to:

>>> (q1 * q2).to_Matrix()
Matrix([
[a - d],
[b - c],
[b + c],
[a + d]])
property product_matrix_right#

Returns 4 x 4 Matrix equivalent to a Hamilton product from the right. This can be useful when treating quaternion elements as column vectors. Given a quaternion \(q = a + bi + cj + dk\) where a, b, c and d are real numbers, the product matrix from the left is:

\[\begin{split}M = \begin{bmatrix} a &-b &-c &-d \\ b & a & d &-c \\ c &-d & a & b \\ d & c &-b & a \end{bmatrix}\end{split}\]

实例

>>> from sympy import Quaternion
>>> from sympy.abc import a, b, c, d
>>> q1 = Quaternion(a, b, c, d)
>>> q2 = Quaternion(1, 0, 0, 1)
>>> q2.product_matrix_right
Matrix([
[1, 0, 0, -1],
[0, 1, 1, 0],
[0, -1, 1, 0],
[1, 0, 0, 1]])

Note the switched arguments: the matrix represents the quaternion on the right, but is still considered as a matrix multiplication from the left.

>>> q2.product_matrix_right * q1.to_Matrix()
Matrix([
[ a - d],
[ b + c],
[-b + c],
[ a + d]])

This is equivalent to:

>>> (q1 * q2).to_Matrix()
Matrix([
[ a - d],
[ b + c],
[-b + c],
[ a + d]])
static rotate_point(pin, r)[源代码]#

Returns the coordinates of the point pin (a 3 tuple) after rotation.

参数:

pin :元组

一个需要旋转的点的三元坐标元组。

r :四元数或元组

轴和旋转角度。

需要注意的是,当r是元组时,它的形式必须是(轴、角)

返回:

元组

旋转后点的坐标。

实例

>>> from sympy import Quaternion
>>> from sympy import symbols, trigsimp, cos, sin
>>> x = symbols('x')
>>> q = Quaternion(cos(x/2), 0, 0, sin(x/2))
>>> trigsimp(Quaternion.rotate_point((1, 1, 1), q))
(sqrt(2)*cos(x + pi/4), sqrt(2)*sin(x + pi/4), 1)
>>> (axis, angle) = q.to_axis_angle()
>>> trigsimp(Quaternion.rotate_point((1, 1, 1), (axis, angle)))
(sqrt(2)*cos(x + pi/4), sqrt(2)*sin(x + pi/4), 1)
scalar_part()[源代码]#

Returns scalar part(\(\mathbf{S}(q)\)) of the quaternion q.

解释

Given a quaternion \(q = a + bi + cj + dk\), returns \(\mathbf{S}(q) = a\).

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(4, 8, 13, 12)
>>> q.scalar_part()
4
set_norm(norm)[源代码]#

Sets norm of an already instantiated quaternion.

参数:

norm : None or number

Pre-defined quaternion norm. If a value is given, Quaternion.norm returns this pre-defined value instead of calculating the norm

实例

>>> from sympy import Quaternion
>>> from sympy.abc import a, b, c, d
>>> q = Quaternion(a, b, c, d)
>>> q.norm()
sqrt(a**2 + b**2 + c**2 + d**2)

Setting the norm:

>>> q.set_norm(1)
>>> q.norm()
1

Removing set norm:

>>> q.set_norm(None)
>>> q.norm()
sqrt(a**2 + b**2 + c**2 + d**2)
to_Matrix(vector_only=False)[源代码]#

Returns elements of quaternion as a column vector. By default, a Matrix of length 4 is returned, with the real part as the first element. If vector_only is True, returns only imaginary part as a Matrix of length 3.

参数:

vector_only : bool

If True, only imaginary part is returned. Default value: False

返回:

矩阵

A column vector constructed by the elements of the quaternion.

实例

>>> from sympy import Quaternion
>>> from sympy.abc import a, b, c, d
>>> q = Quaternion(a, b, c, d)
>>> q
a + b*i + c*j + d*k
>>> q.to_Matrix()
Matrix([
[a],
[b],
[c],
[d]])
>>> q.to_Matrix(vector_only=True)
Matrix([
[b],
[c],
[d]])
to_axis_angle()[源代码]#

Returns the axis and angle of rotation of a quaternion.

返回:

元组

元组(轴、角度)

实例

>>> from sympy import Quaternion
>>> q = Quaternion(1, 1, 1, 1)
>>> (axis, angle) = q.to_axis_angle()
>>> axis
(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3)
>>> angle
2*pi/3
to_euler(seq, angle_addition=True, avoid_square_root=False)[源代码]#

Returns Euler angles representing same rotation as the quaternion, in the sequence given by seq. This implements the method described in [R3].

For degenerate cases (gymbal lock cases), the third angle is set to zero.

参数:

seq : string of length 3

Represents the sequence of rotations. For extrinsic rotations, seq must be all lowercase and its elements must be from the set {'x', 'y', 'z'} For intrinsic rotations, seq must be all uppercase and its elements must be from the set {'X', 'Y', 'Z'}

angle_addition : bool

When True, first and third angles are given as an addition and subtraction of two simpler atan2 expressions. When False, the first and third angles are each given by a single more complicated atan2 expression. This equivalent expression is given by:

\[\operatorname{atan_2} (b,a) \pm \operatorname{atan_2} (d,c) = \operatorname{atan_2} (bc\pm ad, ac\mp bd)\]

Default value: True

avoid_square_root : bool

When True, the second angle is calculated with an expression based on acos, which is slightly more complicated but avoids a square root. When False, second angle is calculated with atan2, which is simpler and can be better for numerical reasons (some numerical implementations of acos have problems near zero). Default value: False

返回:

元组

The Euler angles calculated from the quaternion

实例

>>> from sympy import Quaternion
>>> from sympy.abc import a, b, c, d
>>> euler = Quaternion(a, b, c, d).to_euler('zyz')
>>> euler
(-atan2(-b, c) + atan2(d, a),
 2*atan2(sqrt(b**2 + c**2), sqrt(a**2 + d**2)),
 atan2(-b, c) + atan2(d, a))

工具书类

to_rotation_matrix(v=None, homogeneous=True)[源代码]#

Returns the equivalent rotation transformation matrix of the quaternion which represents rotation about the origin if v is not passed.

参数:

v :元组或无

默认值:无

homogeneous : bool

When True, gives an expression that may be more efficient for symbolic calculations but less so for direct evaluation. Both formulas are mathematically equivalent. Default value: True

返回:

元组

返回四元数的等效旋转变换矩阵,该矩阵表示未传递v时绕原点的旋转。

实例

>>> from sympy import Quaternion
>>> from sympy import symbols, trigsimp, cos, sin
>>> x = symbols('x')
>>> q = Quaternion(cos(x/2), 0, 0, sin(x/2))
>>> trigsimp(q.to_rotation_matrix())
Matrix([
[cos(x), -sin(x), 0],
[sin(x),  cos(x), 0],
[     0,       0, 1]])

Generates a 4x4 transformation matrix (used for rotation about a point other than the origin) if the point(v) is passed as an argument.

classmethod vector_coplanar(q1, q2, q3)[源代码]#

Returns True if the axis of the pure quaternions seen as 3D vectors q1, q2, and q3 are coplanar.

参数:

q1

A pure Quaternion.

q2

A pure Quaternion.

q3

A pure Quaternion.

返回:

True : if the axis of the pure quaternions seen as 3D vectors

q1, q2, and q3 are coplanar.

False : if the axis of the pure quaternions seen as 3D vectors

q1, q2, and q3 are not coplanar.

None : if the axis of the pure quaternions seen as 3D vectors

q1, q2, and q3 are coplanar is unknown.

解释

Three pure quaternions are vector coplanar if the quaternions seen as 3D vectors are coplanar.

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q1 = Quaternion(0, 4, 4, 4)
>>> q2 = Quaternion(0, 8, 8, 8)
>>> q3 = Quaternion(0, 24, 24, 24)
>>> Quaternion.vector_coplanar(q1, q2, q3)
True
>>> q1 = Quaternion(0, 8, 16, 8)
>>> q2 = Quaternion(0, 8, 3, 12)
>>> Quaternion.vector_coplanar(q1, q2, q3)
False

参见

axis, is_pure

vector_part()[源代码]#

Returns \(\mathbf{V}(q)\), the vector part of the quaternion \(q\).

解释

Given a quaternion \(q = a + bi + cj + dk\), returns \(\mathbf{V}(q) = bi + cj + dk\).

实例

>>> from sympy.algebras.quaternion import Quaternion
>>> q = Quaternion(1, 1, 1, 1)
>>> q.vector_part()
0 + 1*i + 1*j + 1*k
>>> q = Quaternion(4, 8, 13, 12)
>>> q.vector_part()
0 + 8*i + 13*j + 12*k