MBsysPy.mbsyspy.utilities.algebra module¶
Define algebra functions for package MBsysPy.
Summary¶
Defines the usefull functions that are not specific to a MBS analysis. Mainly calls corresponding numpy function but ignoring first element of array.
-
MBsysPy.mbsyspy.utilities.algebra.
cross_product
(v1, v2, vres=None, index_1=True)¶ Compute and return the cross product of 2 vectors of 3 components.
The vectors are assumed to store the values in the indices [1:]. If not it must be specified (see parameter ‘index_1’).
- Parameters
v1 (numpy.ndarray) – First vector of the cross product with first element (index_1) unused by default. The number of (used) component must be 3.
v2 (numpy.ndarray) – Second vector of the cross product with first element (index_1) unused by default. The number of (used) component must be 3.
vres (numpy.ndarray, optional) – Vector to store the result if not None. It is allocated if not provided, the used indices are the same as the input vector v1 and v2. default: None
index_1 (bool, optional) – If False, the value v1[0] and v2[0] are included in the computation. default: True.
- Returns
vres – Cross product between v1 and v2 (v1 x v2), the used indices are the same as the input vectors.
- Return type
numpy.ndarray
Notes
The function numpy.cross() is not used as it is not optimized for such small vector.
-
MBsysPy.mbsyspy.utilities.algebra.
matrix_matrix_product
(matrix_1, matrix_2)¶ Compute the product of 2 matrices with first index is 1.
- Parameters
matrix_1 (np.ndarray) – Matrix of shape (4, 4), the first line and first row are disregarded. The computation only consider matrix_1[1:4, 1:4].
matrix_2 (np.ndarray) – Matrix of shape (4, 4), the first line and first row are disregarded. The computation only consider matrix_2[1:4, 1:4].
- Returns
matrix_res – Result of the matrix product. The shape of the matrix is (4, 4), the first line and first row are disregarded.
- Return type
numpy.ndarray
See also
matrix_product()
Slower but more versatile implementation of this functions. It accept both vector or matrix as second parameter and both index 0 or index 1 array.
matrix_vector_product()
Fast implementation restricted to matrix product between a matrix and a vector both with first index unused.
Notes
Globally the functions simply calls numpy.dot(matrix_1[1:4, 1:4], matrix_2[1:4, 1:4])
-
MBsysPy.mbsyspy.utilities.algebra.
matrix_product
(M1, M2, res=None, index_1=True)¶ Compute the product of 2 matrices.
The vector are assumed to store the values in the indices [1:4] while the matrices in [1:4, 1:4]. If not it must be specified (see parameter ‘index_1’), then the indices are [0:3].
- Parameters
M1 (numpy.ndarray) – Matrix to be multiplied by M2. The first line and row are unused by default (parameter ‘index_1’).
M2 (numpy.ndarray) – Second matrix or vector of the product. For a matrix, the first lines and rows are unused by default (parameter ‘index_1’). For a vector the first element is unused by default (parameter ‘index_1’).
res (numpy.ndarray, optional) – Matrice or vector to store the result if not None. It is allocated if not provided, the used indices are the same as the input arrays. If provided, it must have the right dimensions. default: None
index_1 (bool, optional) – If False, the components [0:3] are considered for the computation (instead of [1:4]). default: True.
- Raises
ValueError – If M1 is not a matrix.
- Returns
res – Matrix product between M1 and M2 (M1 . M2). The array is allocated if it was not provided, the used indices are the same as the input arrays.
- Return type
numpy.ndarray
See also
matrix_matrix_product()
Faster implementation restricted to matrix product between 2 matrices with first index unused.
matrix_vector_product()
Faster implementation restricted to matrix product between a matrix and a vector both with first index unused.
Notes
Due to the checks on input parameters this function is slow. Faster implementation, each specific to a product (matrix with vector or matrix with matrix) exist.
-
MBsysPy.mbsyspy.utilities.algebra.
matrix_vector_product
(matrix, vector)¶ Compute the product of a matrix and a vector with first index are 1.
- Parameters
matrix (np.ndarray) – Matrix of shape (4, 4), the first line and first row are disregarded. The computation only consider matrix[1:4, 1:4].
vector (np.ndarray) – Vector of shape (4, ), the first lelement is disregarded. The computation only consider vector[1:4].
- Returns
vector_res – Result of the matrix product. The shape of the vector is (4, ). The first element is disregarded.
- Return type
numpy.ndarray
See also
matrix_product()
Slower but more versatile implementation of this functions. It accept both vector or matrix as second parameter and both index 0 or index 1 array.
matrix_matrix_product()
Fasr implementation restricted to matrix product between 2 matrices with first index unused.
Notes
Globally the functions simply calls numpy.dot(matrix[1:4, 1:4], vector[1:4])
-
MBsysPy.mbsyspy.utilities.algebra.
norm
(v, index_1=True)¶ Compute the norm of a vector.
The vector is assumed to store the values in the indices [1:]. If not it must be specified (see parameter ‘index_1’).
- Parameters
v (numpy.ndarray) – Vector with first element (index_1) unused by default.
index_1 (bool, optional) – If False, the value v[0] is included in the computation. default: True.
- Returns
Norm of vector v starting at index 1.
- Return type
float
-
MBsysPy.mbsyspy.utilities.algebra.
normalize
(v, vres=None, index_1=True)¶ Normalize the provided vector.
The vector is assumed to store the values in the indices [1:]. If not it must be specified (see parameter ‘index_1’).
- Parameters
v (numpy.ndarray) – Vector to be normalized with first element (index_1) unused by default.
vres (numpy.ndarray, optional) – Vector to store the normalized vector. It is allocated if not provided, the used indices are the same as the input vector v. default: None
index_1 (bool, optional) – If False, the value v[0] is included in the computation. default: True.
- Raises
ZeroDivisionError – If the norm of v is equal to zero.
- Returns
vres – The normalized vector v, the used indices are the same as the input vector v.
- Return type
numpy.ndarray
-
MBsysPy.mbsyspy.utilities.algebra.
rotation_matrix
(rot_type, angle, Rres=None)¶ Compute the rotation matrix for a specified angle around a specific axis.
The rotation matrices uses the MBsysC convention wich means that the first index is 1.
- Parameters
rot_type (int) – Integer for axis selection 1: rotation along x-axis. 2: rotation along y-axis. 3: rotation along z-axis.
angle (float) – Rotation angle expressed in radian.
Mres (numpy.ndarray, optional) – Matrix containing the result if not None. default: None
- Raises
ValueError – If rot_type different from 1, 2 or 3.
- Returns
Rres – Rotation matrix.
- Return type
numpy.ndarray
-
MBsysPy.mbsyspy.utilities.algebra.
scalar_product
(v1, v2, index_1=True)¶ Compute and return the scalar product of 2 vectors.
The vectors are assumed to store the values in the indices [1:]. If not it must be specified (see parameter ‘index_1’).
- Parameters
v1 (numpy.ndarray) – First vector of the product with first element (index_1) unused by default.
v2 (numpy.ndarray) – Second vector of the product with first element (index_1) unused by default.
index_1 (bool, optional) – If False, the value v1[0] and v2[0] are included in the computation. default: True.
- Returns
Scalar product between v1 and v2.
- Return type
float
-
MBsysPy.mbsyspy.utilities.algebra.
vector_diff
(v1, v2, vres=None, index_1=True)¶ Compute and return the substraction of 2 vectors.
The vectors are assumed to store the values in the indices [1:]. If not it must be specified (see parameter ‘index_1’).
- Parameters
v1 (numpy.ndarray) – First vector of the substraction with first element (index_1) unused by default.
v2 (list or numpy.ndarray) – Vector to be substracted with first element (index_1) unused by default.
vres (numpy.ndarray, optional) – Vector to store the result of v1-v2. It is allocated if not provided, the used indices are the same as the input vector v1 and v2. default: None
index_1 (bool, optional) – If False, the value v1[0] and v2[0] are included in the computation. default: True.
- Returns
vres – Substraction of v1 and v2 (v1 - v2), the used indices are the same as the input vectors.
- Return type
numpy.ndarray
-
MBsysPy.mbsyspy.utilities.algebra.
vector_sum
(v1, v2, vres=None, index_1=True)¶ Compute and return the sum of 2 vectors.
The vectors are assumed to store the values in the indices [1:]. If not it must be specified (see parameter ‘index_1’).
- Parameters
v1 (numpy.ndarray) – First vector of the addition with first element (index_1) unused by default.
v2 (numpy.ndarray) – Second vector of the addition with first element (index_1) unused by default.
vres (numpy.ndarray, optional) – Vector to store the result of v1+v2. It is allocated if not provided, the used indices are the same as the input vector v1 and v2. default: None
index_1 (bool, optional) – If False, the value v1[0] and v2[0] are included in the computation. default: True.
- Returns
vres – Sum of v1 and v2 (v1 + v2), the used indices are the same as the input vectors.
- Return type
numpy.ndarray