Sympy 简明教程

SymPy - Quaternion

在数学中,四元数数系是由复数扩展得来的。每个四元数对象包含四个标量变量和四个维度,一个实数维度和三个虚数维度。

In mathematics, Quaternion number system is an extension of complex numbers. Each Quaternion object contains four scalar variables and four dimensions, one real dimension and three imaginary dimensions.

四元数由以下表达式表示 −

Quaternion is represented by following expression −

q=a+bi+cj+dk

其中 a, b, c 和 d 是实数, i, j, k 是四元数单位,使得 i2==j2==k2==ijk

where a, b, c and d are real numbers and i, j, k are quaternion units such that,i2==j2==k2==ijk

sympy.algebras.quaternion 模块有四元数类。

The sympy.algebras.quaternion module has Quaternion class.

>>> from sympy.algebras.quaternion import Quaternion
>>> q=Quaternion(2,3,1,4)
>>> q

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$2 + 3i + 1j + 4k$

$2 + 3i + 1j + 4k$

四元数用于纯数学中,以及应用数学、计算机图形、计算机视觉中,等等。

Quaternions are used in pure mathematics, as well as in applied mathematics, computer graphics, computer vision, etc.

>>> from sympy import *
>>> x=Symbol('x')
>>> q1=Quaternion(x**2, x**3, x) >>> q1

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$x^2 + x^3i + xj + 0k$

$x^2 + x^3i + xj + 0k$

四元数对象还可以具有虚系数

Quaternion object can also have imaginary co-efficients

>>> q2=Quaternion(2,(3+2*I), x**2, 3.5*I)
>>> q2

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$2 + (3 + 2i)i + x2j + 3.5ik$

$2 + (3 + 2i)i + x2j + 3.5ik$

add()

四元数类中提供的此方法执行两个四元数对象的加法。

This method available in Quaternion class performs addition of two Quaternion objects.

>>> q1=Quaternion(1,2,3,4)
>>> q2=Quaternion(4,3,2,1)
>>> q1.add(q2)

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$5 + 5i + 5j + 5k$

$5 + 5i + 5j + 5k$

可以在四元数对象中添加数字或符号。

It is possible to add a number or symbol in a Quaternion object.

>>> q1+2

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

$3 + 2i + 3j + 4k$

$3 + 2i + 3j + 4k$

>>> q1+x

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

$(x + 1) + 2i + 3j + 4k$

$(x + 1) + 2i + 3j + 4k$

mul()

此方法执行两个四元数对象的乘法。

This method performs multiplication of two quaternion objects.

>>> q1=Quaternion(1,2,1,2)
>>> q2=Quaternion(2,4,3,1)
>>> q1.mul(q2)

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$(-11) + 3i + 11j + 7k$

$(-11) + 3i + 11j + 7k$

inverse()

此方法返回一个四元数对象的逆。

This method returns inverse of a quaternion object.

>>> q1.inverse()

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$\frac{1}{10} + (-\frac{1}{5})i + (-\frac{1}{10})j + (-\frac{1}{5})k$

$\frac{1}{10} + (-\frac{1}{5})i + (-\frac{1}{10})j + (-\frac{1}{5})k$

pow()

此方法返回一个四元数对象的幂。

This method returns power of a quaternion object.

>>> q1.pow(2)

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

$(-8) + 4i + 2j + 4k$

$(-8) + 4i + 2j + 4k$

exp()

此方法计算一个四元数对象的指数,即eq

This method computes exponential of a Quaternion object i.e. eq

>>> q=Quaternion(1,2,4,3)
>>> q.exp()

执行上面的代码片段后获得以下输出 −

The following output is obtained after executing the above code snippet −

$e\cos(\sqrt29) + \frac{2\sqrt29e\sin(\sqrt29)}{29}i + \frac{4\sqrt29e\sin(\sqrt29)}{29}j + \frac{3\sqrt29e\sin}{29}k$

$e\cos(\sqrt29) + \frac{2\sqrt29e\sin(\sqrt29)}{29}i + \frac{4\sqrt29e\sin(\sqrt29)}{29}j + \frac{3\sqrt29e\sin}{29}k$