Algebras¶
Introduction¶
The Algebras module for SymPy provides support for basic algebraic operations on Quaternions.
Quaternion Reference¶
This section lists the classes implemented by the Algebras module.
- class sympy.algebras.Quaternion(a=0, b=0, c=0, d=0, real_field=True)[source]¶
Provides basic quaternion operations. Quaternion objects can be instantiated as Quaternion(a, b, c, d) as in (a + b*i + c*j + d*k).
Examples
>>> from sympy.algebras.quaternion 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.algebras.quaternion 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
- add(other)[source]¶
Adds quaternions.
- Parameters
other : Quaternion
The quaternion to add to current (self) quaternion.
- Returns
Quaternion
The resultant quaternion after adding self to other
Examples
>>> from sympy.algebras.quaternion 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
Quaternions over complex fields :
>>> from sympy.algebras.quaternion 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
- exp()[source]¶
Returns the exponential of q (e^q).
- Returns
Quaternion
Exponential of q (e^q).
Examples
>>> from sympy.algebras.quaternion 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_axis_angle(vector, angle)[source]¶
Returns a rotation quaternion given the axis and the angle of rotation.
- Parameters
vector : tuple of three numbers
The vector representation of the given axis.
angle : number
The angle by which axis is rotated (in radians).
- Returns
Quaternion
The normalized rotation quaternion calculated from the given axis and the angle of rotation.
Examples
>>> from sympy.algebras.quaternion 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_rotation_matrix(M)[source]¶
Returns the equivalent quaternion of a matrix. The quaternion will be normalized only if the matrix is special orthogonal (orthogonal and det(M) = 1).
- Parameters
M : Matrix
Input matrix to be converted to equivalent quaternion. M must be special orthogonal (orthogonal and det(M) = 1) for the quaternion to be normalized.
- Returns
Quaternion
The quaternion equivalent to given matrix.
Examples
>>> from sympy.algebras.quaternion 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
- integrate(*args)[source]¶
Computes integration of quaternion.
- Returns
Quaternion
Integration of the quaternion(self) with the given variable.
Examples
Indefinite Integral of quaternion :
>>> from sympy.algebras.quaternion 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
Definite integral of quaternion :
>>> from sympy.algebras.quaternion 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
- mul(other)[source]¶
Multiplies quaternions.
- Parameters
other : Quaternion or symbol
The quaternion to multiply to current (self) quaternion.
- Returns
Quaternion
The resultant quaternion after multiplying self with other
Examples
>>> from sympy.algebras.quaternion 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
Quaternions over complex fields :
>>> from sympy.algebras.quaternion 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
- pow(p)[source]¶
Finds the pth power of the quaternion.
- Parameters
p : int
Power to be applied on quaternion.
- Returns
Quaternion
Returns the p-th power of the current quaternion. Returns the inverse if p = -1.
Examples
>>> from sympy.algebras.quaternion import Quaternion >>> q = Quaternion(1, 2, 3, 4) >>> q.pow(4) 668 + (-224)*i + (-336)*j + (-448)*k
- pow_cos_sin(p)[source]¶
Computes the pth power in the cos-sin form.
- Parameters
p : int
Power to be applied on quaternion.
- Returns
Quaternion
The p-th power in the cos-sin form.
Examples
>>> from sympy.algebras.quaternion 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
- static rotate_point(pin, r)[source]¶
Returns the coordinates of the point pin(a 3 tuple) after rotation.
- Parameters
pin : tuple
A 3-element tuple of coordinates of a point which needs to be rotated.
r : Quaternion or tuple
Axis and angle of rotation.
It’s important to note that when r is a tuple, it must be of the form (axis, angle)
- Returns
tuple
The coordinates of the point after rotation.
Examples
>>> from sympy.algebras.quaternion 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)
- to_axis_angle()[source]¶
Returns the axis and angle of rotation of a quaternion
- Returns
tuple
Tuple of (axis, angle)
Examples
>>> from sympy.algebras.quaternion 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_rotation_matrix(v=None)[source]¶
Returns the equivalent rotation transformation matrix of the quaternion which represents rotation about the origin if v is not passed.
- Parameters
v : tuple or None
Default value: None
- Returns
tuple
Returns the equivalent rotation transformation matrix of the quaternion which represents rotation about the origin if v is not passed.
Examples
>>> from sympy.algebras.quaternion 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((1, 1, 1))) Matrix([ [cos(x), -sin(x), 0, sin(x) - cos(x) + 1], [sin(x), cos(x), 0, -sin(x) - cos(x) + 1], [ 0, 0, 1, 0], [ 0, 0, 0, 1]])