pylib.mathematics module

Mathematical functions and objects.

Date

2019-12-12

lcm(a, b)[source]

Compute the lowest common multiple of a and b

class matrix[source]

Bases: list

Use/create matrix like list of lists

__getitem__(index)[source]

For index return value, for range return new vector object.

Example

>>> m = matrix([[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0],                     [0, 0, 0, 0]])
>>> print(m[2])
[7, 8, 9, 0]
>>> print(m[:, 1:3])
[[2, 3], [5, 6], [8, 9], [0, 0]]
>>> print(m[0:2, 1:3])
[[2, 3], [5, 6]]
>>> print(m[::2, ::2])
[[1, 3], [7, 9]]
__mul__(other)[source]

Scalar multiplication, dot product (inner product) or matrix-vector multiplication. (new object)

\[\begin{split}\mathbf{C} &= \mathbf{A} \cdot b \\ \mathbf{c} &= \mathbf{A} \cdot \mathbf{b} \\ \mathbf{C} &= \mathbf{A} \cdot \mathbf{B}\end{split}\]

Note

No size checking will be conducted, therefore no exceptions for wrong usage (result will be nonsense).

Example

>>> m = matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], \
                [0, 0, 0, 1]]) * 5
>>> print(m)
[[5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60], [0, 0, 0, 5]]
>>> m = matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], \
                [0, 0, 0, 1]]) * 5.
>>> print(m)
[[5.0, 10.0, 15.0, 20.0], [25.0, 30.0, 35.0, 40.0], [45.0, 50.0, 55.0, 60.0], [0.0, 0.0, 0.0, 5.0]]
>>> v = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) * \
    vector([12, 12, 13])
>>> print(v)
[75, 186, 297]
>>> m = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) * \
    matrix([[12, 12, 13], [14, 15, 16], [17, 18, 19]])
>>> print(m)
[[91, 96, 102], [220, 231, 246], [349, 366, 390]]

See also

__rmul__()

__repr__()[source]

Return repr(self).

__rmul__(other)[source]

Scalar multiplication, dot product (inner product) or vector-matrix multiplication. (new object)

\[\begin{split}\mathbf{C} &= a \cdot \mathbf{B} \\ \mathbf{c} &= \mathbf{a} \cdot \mathbf{B} \\ \mathbf{C} &= \mathbf{A} \cdot \mathbf{B}\end{split}\]

Note

No size checking will be conducted, therefore no exceptions for wrong usage (result will be nonsense).

Example

>>> m = 5 * matrix([[1, 2, 3, 4], [5, 6, 7, 8], \
                    [9, 10, 11, 12], [0, 0, 0, 1]])
>>> print(m)
[[5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60], [0, 0, 0, 5]]
>>> m = 5. * matrix([[1, 2, 3, 4], [5, 6, 7, 8], \
                     [9, 10, 11, 12], [0, 0, 0, 1]])
>>> print(m)
[[5.0, 10.0, 15.0, 20.0], [25.0, 30.0, 35.0, 40.0], [45.0, 50.0, 55.0, 60.0], [0.0, 0.0, 0.0, 5.0]]

See also

__mul__() and vector.__mul__() for vector * matrix

__str__()[source]

Return str(self).

rotate_x(theta)[source]

Rotation about the x dirction.

\[\begin{split}\begin{bmatrix} xx & xy & xz & t_x \\ yx' & yy' & yz' & t_y' \\ zx' & zy' & zz' & t_z' \\ 0 & 0 & 0 & h \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta & 0 \\ 0 & \sin \theta & \cos \theta & 0 \\ 0 & 0 & 0 & h \end{bmatrix} \begin{bmatrix} xx & xy & xz & t_x \\ yx & yy & yz & t_y \\ zx & zy & zz & t_z \\ 0 & 0 & 0 & h \end{bmatrix}\end{split}\]
rotate_y(theta)[source]

Rotation about the y dirction.

\[\begin{split}\begin{bmatrix} xx' & xy' & xz' & t_x' \\ yx & yy & yz & t_y \\ zx' & zy' & zz' & t_z' \\ 0 & 0 & 0 & h \end{bmatrix} = \begin{bmatrix} \cos \theta & 0 & \sin \theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin \theta & 0 & \cos \theta & 0 \\ 0 & 0 & 0 & h \end{bmatrix} \begin{bmatrix} xx & xy & xz & t_x \\ yx & yy & yz & t_y \\ zx & zy & zz & t_z \\ 0 & 0 & 0 & h \end{bmatrix}\end{split}\]
rotate_z(theta)[source]

Rotation about the z dirction.

\[\begin{split}\begin{bmatrix} xx' & xy' & xz' & t_x' \\ yx' & yy' & yz' & t_y' \\ zx & zy & zz & t_z \\ 0 & 0 & 0 & h \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta & 0 & 0 \\ \sin \theta & \cos \theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} xx & xy & xz & t_x \\ yx & yy & yz & t_y \\ zx & zy & zz & t_z \\ 0 & 0 & 0 & h \end{bmatrix}\end{split}\]
static rx(theta)[source]

Rotation matrix about the x direction.

Rotates the coordinate system of vectors

\[\begin{split}\mathbf{R}_{x}(\theta) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta & 0 \\ 0 & \sin \theta & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
static ry(theta)[source]

Rotation matrix about the y direction.

Rotates the coordinate system of vectors

\[\begin{split}\mathbf{R}_{y}(\theta) = \begin{bmatrix} \cos \theta & 0 & \sin \theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin \theta & 0 & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
static rz(theta)[source]

Rotation matrix about the z direction.

Rotates the coordinate system of vectors

\[\begin{split}\mathbf{R}_{z}(\theta) = \begin{bmatrix} \cos \theta & -\sin \theta & 0 & 0 \\ \sin \theta & \cos \theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Example

\[\begin{split}\mathbf{R}_{z}(\theta) \begin{bmatrix}1 \\ 0 \\ 0 \\ 1\end{bmatrix} &= \begin{bmatrix} \cos 90° & -\sin 90° & 0 & 0 \\ \sin 90° & \cos 90° & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}1 \\ 0 \\ 0 \\ 1\end{bmatrix} \\ &= \begin{bmatrix} 0 & -1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}1 \\ 0 \\ 0 \\ 1\end{bmatrix} = \begin{bmatrix}0 \\ 1 \\ 0 \\ 1\end{bmatrix}\end{split}\]
static s(sx, sy=None, sz=None)[source]

Scaling matrix

uniform scaling if sx=sy=sz=s. Note that scaling happens around the origin, so objects not centered at the origin will have their centers move. To avoid this, either scale the object when it’s located at the origin, or perform a translation afterwards to move the object back to where it should be.

\[\begin{split}\mathbf{S} = \begin{bmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
scale(sx, sy=None, sz=None)[source]

Scaling

uniform scaling if sx=sy=sz=s. Note that scaling happens around the origin, so objects not centered at the origin will have their centers move. To avoid this, either scale the object when it’s located at the origin, or perform a translation afterwards to move the object back to where it should be.

\[\begin{split}\begin{bmatrix} xx' & xy' & xz' & t_x' \\ yx' & yy' & yz' & t_y' \\ zx' & zy' & zz' & t_z' \\ 0 & 0 & 0 & h \end{bmatrix} = \begin{bmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} xx & xy & xz & t_x \\ yx & yy & yz & t_y \\ zx & zy & zz & t_z \\ 0 & 0 & 0 & h \end{bmatrix}\end{split}\]
static t(tx, ty, tz)[source]

Translation matrix

\[\begin{split}\mathbf{T} = \begin{bmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
translate(tx, ty, tz)[source]

Translation

\[\begin{split}\begin{bmatrix} xx & xy & xz & t_x' \\ yx & yy & yz & t_y' \\ zx & zy & zz & t_z' \\ 0 & 0 & 0 & h \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} xx & xy & xz & t_x \\ yx & yy & yz & t_y \\ zx & zy & zz & t_z \\ 0 & 0 & 0 & h \end{bmatrix}\end{split}\]
class vector[source]

Bases: list

Use/create vector like lists

__iter__

>>> [i*2 for i in vector([1, 2, 3])]
[2, 4, 6]
__setitem__

>>> v = vector([1, 2, 3, 4, 5])
>>> v[3:5] = [1, 2]
>>> print(v)
[1, 2, 3, 1, 2]
>>> isinstance(v, vector)
True
__eq__(a, b)

>>> vector([1, 2, 3, 1, 2]) == vector([1, 2, 3, 1, 2])
True
>>> vector([1, 2, 3, 1, 2]) == vector([1, 2, 3, 2, 1])
False
__ne__(a, b)

>>> vector([1, 2, 3, 1, 2]) != vector([1, 2, 3, 1, 2])
False
>>> vector([1, 2, 3, 1, 2]) != vector([1, 2, 3, 2, 1])
True
__contains__

>>> 2 in vector([1, 2, 3])
True
__isub__

>>> v = vector([1, 2, 3])
>>> v -= vector([3, 3, 3])
>>> print(v)
[-2, -1, 0]
__imul__

>>> v = vector([1, 2, 3])
>>> v *= vector([3, 3, 3])
>>> print(v)
18
__imatmul__

>>> m = vector([1, 2, 3])
>>> m *= vector([3, 3, 3])
>>> print(v)
[[3, 3, 3], [6, 6, 6], [9, 9, 9]]
__abs__()[source]

Magnitude / norm of a vector

\[b = |\mathbf{a}| = \sqrt{\sum a_i^2} = \sqrt{\mathbf{a} \cdot \mathbf{a}}\]
Example

>>> v = vector([3, 4])
>>> abs(v)
5.0
__add__(other)[source]

a + b (new object)

\[\mathbf{c} = \mathbf{a} + \mathbf{b}\]
Example

>>> v = vector([1, 2, 3]) + vector([1, 2, 3])
>>> print(v)
[2, 4, 6]
__ge__(other)[source]

Test if this object is greater (larger) than or equal the other object.

\[|\mathbf{a}| \ge |\mathbf{b}|\]
Example

>>> vector([1, 2, 3]) >= vector([3, 2, 1])
True
>>> vector([1, 2, 3]) >= vector([4, 2, 1])
False
__getitem__(index)[source]

For index return value, for range return new vector object.

Example

>>> v = vector([1, 2, 3, 4, 5])
>>> v[1:3]
[2, 3]
>>> v = vector([1, 2, 3, 4, 5])
>>> v[3]
4
__gt__(other)[source]

Test if this object is greater (larger) than the other object.

\[|\mathbf{a}| \gt |\mathbf{b}|\]
Example

>>> vector([1, 2, 3]) > vector([3, 2, 1])
False
>>> vector([1, 2, 3]) > vector([2, 2, 1])
True
__iadd__(other)[source]

a += b (new object)

Example

>>> v = vector([1, 2, 3])
>>> v += vector([3, 3, 3])
>>> print(v)
[4, 5, 6]
__le__(other)[source]

Test if this object is lower (smaller) than or equal the other object.

\[|\mathbf{a}| \le |\mathbf{b}|\]
Example

>>> vector([3, 2, 1]) <= vector([1, 2, 3])
True
>>> vector([3, 2, 1]) <= vector([1, 2, 2])
False
__lt__(other)[source]

Test if this object is lower (smaller) than the other object.

\[|\mathbf{a}| \lt |\mathbf{b}|\]
Example

>>> vector([3, 2, 1]) < vector([1, 2, 3])
False
>>> vector([3, 2, 1]) < vector([1, 2, 4])
True
__matmul__(other)[source]

Outer product a @ b (new object)

\[\begin{split}\mathbf{c} = \mathbf{a} \otimes \mathbf{b} = \begin{pmatrix} a_{1}\\ a_{2}\\ a_{3} \end{pmatrix} \otimes \begin{pmatrix} b_{1}\\ b_{2}\\ b_{3} \end{pmatrix} = \begin{pmatrix} a_{1}b_{1}&a_{1}b_{2}&a_{1}b_{3}\\ a_{2}b_{1}&a_{2}b_{2}&a_{2}b_{3}\\ a_{3}b_{1}&a_{3}b_{2}&a_{3}b_{3} \end{pmatrix}\end{split}\]
Example

>>> m = vector([1, 2, 3]) @ vector([1, 2, 4])
>>> print(m)
[[1, 2, 4], [2, 4, 8], [3, 6, 12]]
__mul__(other)[source]

Scalar multiplication, dot product (inner product) or vector-matrix multiplication. (new object)

\[\begin{split}\mathbf{c} &= \mathbf{a} \cdot b \\ \mathbf{c} &= \mathbf{a} \cdot \mathbf{b} \\ \mathbf{c} &= \mathbf{a} \cdot \mathbf{B}\end{split}\]

Note

No size checking will be conducted, therefore no exceptions for wrong usage (result will be nonsense).

Example

>>> v = vector([1, 2, 3, 4, 5]) * 3
>>> print(v)
[3, 6, 9, 12, 15]
>>> v = vector([1, 2, 3, 4, 5]) * 3.
>>> print(v)
[3.0, 6.0, 9.0, 12.0, 15.0]
>>> s = vector([1, 2, 3]) * vector([1, 2, 3])
>>> print(s)
14

See also

__rmul__()

__neg__()[source]

- a (new object)

\[\mathbf{b} = -\mathbf{a}\]
__pos__()[source]

+ a (new object)

\[\mathbf{b} = +\mathbf{a}\]
__repr__()[source]

Return repr(self).

__rmul__(other)[source]

Scalar multiplication, dot product (inner product) or matrix-vector multiplication. (new object)

\[\begin{split}\mathbf{c} &= a \cdot \mathbf{b} \\ \mathbf{c} &= \mathbf{a} \cdot \mathbf{b} \\ \mathbf{c} &= \mathbf{A} \cdot \mathbf{b}\end{split}\]

Note

No size checking will be conducted, therefore no exceptions for wrong usage (result will be nonsense).

Example

>>> v = 3 * vector([1, 2, 3, 4, 5])
>>> print(v)
[3, 6, 9, 12, 15]
>>> v = 3. * vector([1, 2, 3, 4, 5])
>>> print(v)
[3.0, 6.0, 9.0, 12.0, 15.0]

See also

__mul__() and matrix.__mul__() for matrix * vector

__str__()[source]

Return str(self).

__sub__(other)[source]

a - b (new object)

\[\mathbf{c} = \mathbf{a} - \mathbf{b}\]
Example

>>> v = vector([1, 2, 3]) - vector([1, 2, 3])
>>> print(v)
[0, 0, 0]
static abs(a)[source]

Return modulus parts of a complex vector

static ang(a, b)[source]
static arg(a)[source]

Return phase parts of a complex vector

ch_cs(cs)[source]

Transform this vector from its defined coordinate system to a new coordinate system, defined by the given coordinate system (u, v and w direction vectors).

\[\begin{split}\begin{bmatrix}x' \\ y' \\ z' \\ h'\end{bmatrix} = \begin{bmatrix} u_x & u_y & u_z & 0 \\ v_x & v_y & v_z & 0 \\ w_x & w_y & w_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}x \\ y \\ z \\ h\end{bmatrix}\end{split}\]
static conjugate(a)[source]

New vector object

static cross(a, b)[source]

Cross product

c is orthogonal to both a and b. The direction of c can be found with the right-hand rule.

\[\mathbf{c} = \mathbf{a} \times \mathbf{b}\]
static full(length, fill_value)[source]

Returns a vector of length m or matrix of size m rows, n columns filled with v.

Parameters
  • length (int) – length of the vector, e. g. 3

  • fill_value – Fill value

Type fill_value

scalar

Example

>>> v = vector.full(3, 7)
>>> print(v)
[7, 7, 7]
static im(a)[source]

Return the imaginary parts of a complex vector

static normalize(a)[source]

Normalize a vector (i. e. the vector has a length of 1)

\[\mathbf{\hat{a}} = \frac{\mathbf{a}}{|\mathbf{a}|}\]

See also

__abs__() for a norm (magnitude) of a vector

static ones(length)[source]

Returns a vector of length m or matrix of size rows, n columns filled with ones.

Parameters

length (int) – lhape of the vector, e. g. 3

Example

>>> v = ones(3)
>>> print(v)
[1.0, 1.0, 1.0]
static random(shape, lmin=0.0, lmax=1.0)[source]

Returns a random vector of length n or matrix of size m rows, n columns filled with random numbers.

Example

>>> v = random(3)
>>> print(v)
[0.9172905912930438, 0.8908124278322492, 0.5256002790725927]
>>> v = random(3, 1, 2)
>>> print(v)
[1.2563665665080803, 1.9270454509964547, 1.2381672401270487]
static re(a)[source]

Return the real parts of a complex vector

rotate_x(theta)[source]

Rotation about the x dirction.

\[\begin{split}\begin{bmatrix}x' \\ y' \\ z' \\ h'\end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta & 0 \\ 0 & \sin \theta & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}x \\ y \\ z \\ h\end{bmatrix}\end{split}\]
rotate_y(theta)[source]

Rotation about the y dirction.

\[\begin{split}\begin{bmatrix}x' \\ y' \\ z' \\ h'\end{bmatrix} = \begin{bmatrix} \cos \theta & 0 & \sin \theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin \theta & 0 & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}x \\ y \\ z \\ h\end{bmatrix}\end{split}\]
rotate_z(theta)[source]

Rotation about the z dirction.

\[\begin{split}\begin{bmatrix}x' \\ y' \\ z' \\ h'\end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta & 0 & 0 \\ \sin \theta & \cos \theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}x \\ y \\ z \\ h\end{bmatrix}\end{split}\]
scale(sx, sy=None, sz=None)[source]

Scaling

uniform scaling if sx=sy=sz=s. Note that scaling happens around the origin, so objects not centered at the origin will have their centers move. To avoid this, either scale the object when it’s located at the origin, or perform a translation afterwards to move the object back to where it should be.

\[\begin{split}\begin{bmatrix}x' \\ y' \\ z' \\ h'\end{bmatrix} = \begin{bmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}x \\ y \\ z \\ h\end{bmatrix}\end{split}\]
translate(tx, ty, tz)[source]

Translation

\[\begin{split}\begin{bmatrix}x' \\ y' \\ z' \\ h'\end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix}x \\ y \\ z \\ h\end{bmatrix}\end{split}\]
static zeros(length)[source]

Returns a zero vector of length m or matrix of size rows, n columns filled with zeros.

Parameters

length (int) – length of the vector, e. g. 3

Example

>>> v = zeros(3)
>>> print(v)
[0.0, 0.0, 0.0]