CMLibs Maths
CMLibs Maths is a Python package containing math utility functions. These utility functions are general utilities commonly used by downstream packages.
This package provides two modules
vectorops
algorithms
These modules are surfaced under the namespace package cmlibs within the maths package. To use these modules the following import statement can be used:
import cmlibs.maths.vectorops
import cmlibs.maths.algorithms
Vector Operations
The vectorops module is a collection of functions that operate on python lists as if they were vectors. A basic implementation to forgo the need to use numpy.
Algorithms
The algorithms module is a collection of functions that perform calculations on python lists. These functions are commonly used in other CMLibs packages.
Package API
Vector Operations Module
A collection of functions that operate on python lists as if they were vectors. A basic implementation to forgo the need to use numpy.
- cmlibs.maths.vectorops.add_vectors(vectors, scalars=None)
returns s1*v1+s2*v2+… where scalars = [s1, s2, …] and vectors=[v1, v2, …]. :return: Resultant vector
- cmlibs.maths.vectorops.angle(u, v)
Calculate the angle between two non-zero vectors. :return: The angle between them in radians.
- cmlibs.maths.vectorops.axis_angle_to_quaternion(axis, theta)
- Parameters:
axis – Unit vector axis of rotation.
theta – Angle of rotation in right hand sense around axis, in radians.
- Returns:
Quaternion representing rotation.
- cmlibs.maths.vectorops.axis_angle_to_rotation_matrix(axis, theta)
Convert axis angle to a rotation matrix.
- Parameters:
axis – Unit vector axis of rotation.
theta – Angle of rotation in right hand sense around axis, in radians.
- Returns:
3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v’ = Mv
- cmlibs.maths.vectorops.div(v, s)
Calculate v / s :param v: Vector. :param s: Scalar. :return: [v_1 / s, v_2 / s, …, v_n / s]
- cmlibs.maths.vectorops.euler_to_rotation_matrix(euler_angles)
From Zinc graphics_library.cpp, with matrix transposed to row major. Matrix is product RzRyRx, giving rotation about x, then y, then z with positive angles rotating by right hand rule about axis. :param euler_angles: 3 angles in radians, components: 0 = azimuth (about z) 1 = elevation (about y) 2 = roll (about x) :return: 3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v’ = Mv
- cmlibs.maths.vectorops.identity_matrix(size)
Create an identity matrix of size x size.
- cmlibs.maths.vectorops.magnitude(v)
return: scalar magnitude of vector v
- cmlibs.maths.vectorops.matrix_constant_mult(m, c)
Multiply components of matrix m by constant c
- cmlibs.maths.vectorops.matrix_inv(a)
Invert a square matrix by compouting the determinant and cofactor matrix.
- cmlibs.maths.vectorops.matrix_mult(a, b)
Multiply 2 matrices: first index is down row, second is across column. Assumes sizes are compatible (number of columns of a == number of rows of b).
- cmlibs.maths.vectorops.matrix_vector_mult(m, v)
Post multiply matrix m by vector v
- cmlibs.maths.vectorops.mult(v, s)
Calculate s * v :param v: Vector. :param s: Scalar. :return: [s * v_1, s * v_2, …, s * v_n]
- cmlibs.maths.vectorops.projection(v1, v2)
Calculate vector projection of v1 on v2 :return: A projection vector.
- cmlibs.maths.vectorops.quaternion_to_rotation_matrix(quaternion)
This method takes a quaternion representing a rotation and turns it into a rotation matrix. :return: 3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v’ = Mv
- cmlibs.maths.vectorops.rejection(v1, v2)
Calculate vector rejection of v1 on v2 :return: A rejection vector.
- cmlibs.maths.vectorops.rotate_about_z_axis(v, theta)
Rotate the given vector v about the z-axis by theta radians. :param v: vector to be rotated. :param theta: angle of rotation. :return rotated vector.
- cmlibs.maths.vectorops.rotate_vector_around_vector(v, k, a)
Rotate vector v, by an angle a (right-hand rule) in radians around vector k. :return: rotated vector.
- cmlibs.maths.vectorops.rotation_matrix_to_euler(matrix)
From Zinc graphics_library.cpp, with matrix transposed to row major. Inverse function to euler_to_rotation_matrix.
- cmlibs.maths.vectorops.scalar_projection(v1, v2)
- Returns:
Scalar projection of v1 onto v2.
- cmlibs.maths.vectorops.set_magnitude(v, mag)
return: Vector v with magnitude set to mag.
- cmlibs.maths.vectorops.vector_matrix_mult(v, m)
Premultiply matrix m by vector v
Algorithms Module
- cmlibs.maths.algorithms.calculateExtents(values)
Calculate the maximum and minimum for each coordinate x, y, and z Return the max’s and min’s as:
[x_min, x_max, y_min, y_max, z_min, z_max]
- cmlibs.maths.algorithms.calculate_centroid(data_points)
Calculates the centroid of a list of point coordinates.
- Parameters:
data_points – A list containing ‘n’ lists (coordinates) of size ‘m’. With ‘n’ denoting the number of points and ‘m’ denoting the number of dimensions of the coordinates.
- Returns:
An m-dimensional list containing the coordinates of the centroid.
- cmlibs.maths.algorithms.calculate_extents(values)
Calculate the maximum and minimum for each coordinate x, y, and z Return the max’s and min’s as:
[x_min, x_max, y_min, y_max, z_min, z_max]