mathematics module¶
Mathematical functions and objects.
- Date
2019-12-12
-
class
matrix[source]¶ Bases:
listUse/create matrix like list of lists
-
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}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}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}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}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}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}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:
listUse/create vector like lists
size -> len(a)
abs -> abs(a)
dot -> a * b
outer -> a @ b
use super constructor
use super __iter__
use super __setitem__
>>> v = vector([1,2,3,4,5]) >>> v[3:5] = [1,2] >>> print(v) [1, 2, 3, 1, 2] >>> isinstance(v, vector) True
use super __lt__(a, b)
use super __le__(a, b)
use super __eq__(a, b)
>>> v = vector([1,2,3,1,2]) >>> v2 = vector([1,2,3,1,2]) >>> v == v2 True
use super __ne__(a, b)
use super __ge__(a, b)
use super __gt__(a, b)
use super __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]]
-
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
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.
\[\vec{c} = \vec{a} \times \vec{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
normalize(a)[source]¶ Normalize a vector (i. e. the vector has a length of 1)
\[\vec{e}_a = \frac{\vec{a}}{|\vec{a}|}\]See also
norm()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]
-
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}\]