Tools functions#
- class rocketpy.tools.Vector(components)[source]#
Pure python basic R3 vector class designed for simple operations.
Notes
Instances of the Vector class are immutable. Real and complex components are supported.
Examples
Creating a Vector instance requires passing its components as an iterable: >>> v = Vector([1, 2, 3]) >>> v Vector(1, 2, 3)
Vector components can be accessed by x, y and z or by indexing: >>> v.x, v.y, v.z (1, 2, 3) >>> v[0], v[1], v[2] (1, 2, 3)
Vector instances can be added, subtracted, multiplied by a scalar, divided by a scalar, negated, and cross and dot product can be computed: >>> v + v Vector(2, 4, 6) >>> v - v Vector(0, 0, 0) >>> v * 2 Vector(2, 4, 6) >>> v / 2 Vector(0.5, 1.0, 1.5) >>> -v Vector(-1, -2, -3) >>> v @ v # Dot product 14
Cross products need to be wrapped in parentheses to ensure the ^ operator precedence: >>> (v ^ v) Vector(0, 0, 0)
Vector instances can be called as functions if their elements are callable: >>> v = Vector([lambda x: x**2, lambda x: x**3, lambda x: x**4]) >>> v(2) Vector(4, 8, 16)
Vector instances magnitudes can be accessed as its absolute value: >>> v = Vector([1, 2, 3]) >>> abs(v) 3.7416573867739413
Vector instances can be normalized: >>> v.unit_vector Vector(0.2672612419124244, 0.5345224838248488, 0.8017837257372732)
Vector instances can be compared for equality: >>> v = Vector([1, 2, 3]) >>> u = Vector([1, 2, 3]) >>> v == u True >>> v != u False
And last, but not least, it is also possible to check if two vectors are parallel or orthogonal: >>> v = Vector([1, 2, 3]) >>> u = Vector([2, 4, 6]) >>> v.is_parallel_to(u) True >>> v.is_orthogonal_to(u) False
Vector class constructor.
- Parameters:
components (array-like, iterable) – An iterable with length equal to 3, corresponding to x, y and z components.
Examples
>>> v = Vector([1, 2, 3]) >>> v Vector(1, 2, 3)
- cross_matrix[source]#
Skew symmetric matrix used for cross product.
Notes
The cross product between two vectors can be computed as the matrix product between the cross matrix of the first vector and the second vector.
Examples
>>> v = Vector([1, 7, 3]) >>> u = Vector([2, 5, 6]) >>> (v ^ u) == v.cross_matrix @ u True
- is_parallel_to(other)[source]#
Returns True if self is parallel to R3 vector other. False otherwise.
- Parameters:
other (Vector) – R3 vector to be compared with self.
- Returns:
True if self and other are parallel. False otherwise.
- Return type:
bool
Notes
Two R3 vectors are parallel if their cross product is the zero vector. Python’s cmath.isclose function is used to assert this.
- is_orthogonal_to(other)[source]#
Returns True if self is perpendicular to R3 vector other. False otherwise.
- Parameters:
other (Vector) – R3 vector to be compared with self.
- Returns:
True if self and other are perpendicular. False otherwise.
- Return type:
bool
Notes
Two R3 vectors are perpendicular if their dot product is zero. Python’s cmath.isclose function is used to assert this with absolute tolerance of 1e-9.
- element_wise(operation)[source]#
Element wise operation.
- Parameters:
operation (callable) – Callable with a single argument, which should take an element and return the result of the desired operation.
Examples
>>> v = Vector([1, 7, 3]) >>> v.element_wise(lambda x: x**2) Vector(1, 49, 9)
- class rocketpy.tools.Matrix(components)[source]#
Pure Python 3x3 Matrix class for simple matrix-matrix and matrix-vector operations.
Notes
Instances of the Matrix class are immutable. Real and complex components are supported.
Examples
Creating a Matrix instance requires passing its components as a nested iterable: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M Matrix([1, 2, 3],
[4, 5, 6], [7, 8, 9])
Matrix instances can be indexed and sliced like lists: >>> M[0] [1, 2, 3] >>> M[0][0] 1 >>> M[0, 0] 1 >>> M[0, 0:2] [1, 2]
Matrix instances components can be accessed as attributes: >>> M.xx, M.xy, M.xz (1, 2, 3)
Matrix instances can be called as functions, if their elements are callable: >>> M = Matrix([[lambda x: x**1, lambda x: x**2, lambda x: x**3], … [lambda x: x**4, lambda x: x**5, lambda x: x**6], … [lambda x: x**7, lambda x: x**8, lambda x: x**9]]) >>> M(2) Matrix([2, 4, 8],
[16, 32, 64], [128, 256, 512])
Matrix instances can be added, subtracted, multiplied and divided by scalars: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M + M Matrix([2, 4, 6],
[8, 10, 12], [14, 16, 18])
>>> M - M Matrix([0, 0, 0], [0, 0, 0], [0, 0, 0]) >>> M * 2 Matrix([2, 4, 6], [8, 10, 12], [14, 16, 18]) >>> M / 2 Matrix([0.5, 1.0, 1.5], [2.0, 2.5, 3.0], [3.5, 4.0, 4.5])
Matrix instances can be multiplied (inner product) by other matrices: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M @ N Matrix([30, 36, 42],
[66, 81, 96], [102, 126, 150])
Matrix instances can be used to transform vectors by the inner product: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> v = Vector([1, 2, 3]) >>> M @ v Vector(14, 32, 50)
Matrix instances can be transposed and inverted: >>> M = Matrix([[1, 2, 3], [4, 0, 6], [7, 8, 9]]) >>> M.transpose Matrix([1, 4, 7],
[2, 0, 8], [3, 6, 9])
>>> M.inverse Matrix([-0.8, 0.1, 0.2], [0.1, -0.2, 0.1], [0.5333333333333333, 0.1, -0.13333333333333333])
Matrix instances can be element-wise operated on by callables: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.element_wise(lambda x: x**2) Matrix([1, 4, 9],
[16, 25, 36], [49, 64, 81])
Determinants can be calculated: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.det 0 >>> abs(M) 0
Matrices can be compared for equality: >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M == N True >>> M != N False
Matrix class constructor.
- Parameters:
components (3x3 array-like) – 3x3 array-like with matrix components. Indexing must be [row, column].
Examples
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9])
- is_diagonal[source]#
Boolean indicating if matrix is diagonal.
- Parameters:
tol (float, optional) – Tolerance used to determine if non-diagonal elements are negligible. Defaults to 1e-6.
- Returns:
True if matrix is diagonal, False otherwise.
- Return type:
bool
Examples
>>> M = Matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]]) >>> M.is_diagonal True
>>> M = Matrix([[1, 0, 0], [0, 2, 0], [0, 1e-7, 3]]) >>> M.is_diagonal True
>>> M = Matrix([[1, 0, 0], [0, 2, 0], [0, 1e-5, 3]]) >>> M.is_diagonal False
- inverse[source]#
Matrix inverse.
- Returns:
Inverse of the matrix.
- Return type:
Notes
If the matrix is diagonal, the inverse is computed by inverting its diagonal elements. If not, the inverse is computed using the adjugate matrix.
- Raises:
ZeroDivisionError – If the matrix is singular.
- element_wise(operation)[source]#
Element wise operation.
- Parameters:
operation (callable) – Callable with a single argument, which should take an element and return the result of the desired operation.
- Returns:
The result of the element wise operation.
- Return type:
Examples
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.element_wise(lambda x: x ** 2) Matrix([1, 4, 9], [16, 25, 36], [49, 64, 81])
- dot(other)[source]#
Dot product between two 3x3 matrices or between 3x3 matrix and R3 vector.
See also
Matrix.__matmul__
- static transformation(quaternion)[source]#
Returns the transformation Matrix from frame B to frame A, where B is rotated by the quaternion q with respect to A.
- Parameters:
q (tuple of 4 floats) – The quaternion representing the rotation from frame A to frame B. Example: (cos(phi/2), 0, 0, sin(phi/2)) represents a rotation of phi around the z-axis. Note: the quaternion must be normalized.
- Returns:
The transformation matrix from frame B to frame A.
- Return type:
- rocketpy.tools.tuple_handler(value)[source]#
Transforms the input value into a tuple that represents a range. If the input is an input or float, the output is a tuple from zero to the input value. If the input is a tuple or list, the output is a tuple with the same range.
- Parameters:
value (int, float, tuple, list) – Input value.
- Returns:
Tuple that represents the inputted range.
- Return type:
tuple
- rocketpy.tools.bilinear_interpolation(x, y, x1, x2, y1, y2, z11, z12, z21, z22)[source]#
Bilinear interpolation. It considers the values of the four points around the point to be interpolated and returns the interpolated value. Made with a lot of help from GitHub Copilot.
- Parameters:
x (float) – x coordinate to which the value will be interpolated.
y (float) – y coordinate to which the value will be interpolated.
x1 (float) – x coordinate of the first point.
x2 (float) – x coordinate of the second point.
y1 (float) – y coordinate of the first point.
y2 (float) – y coordinate of the second point.
z11 (float) – Value at the first point.
z12 (float) – Value at the second point.
z21 (float) – Value at the third point.
z22 (float) – Value at the fourth point.
- Returns:
Interpolated value.
- Return type:
float
Examples
>>> from rocketpy.tools import bilinear_interpolation >>> bilinear_interpolation(0.5, 0.5, 0, 1, 0, 1, 0, 1, 1, 0) 0.5
- rocketpy.tools.find_two_closest_integers(number)[source]#
Find the two closest integer factors of a number.
- Parameters:
number (int) –
- Returns:
Two closest integer factors of the number.
- Return type:
tuple
Examples
>>> from rocketpy.tools import find_two_closest_integers >>> find_two_closest_integers(10) (2, 5) >>> find_two_closest_integers(12) (3, 4) >>> find_two_closest_integers(13) (1, 13) >>> find_two_closest_integers(150) (10, 15)
- rocketpy.tools.time_num_to_date_string(time_num, units, timezone, calendar='gregorian')[source]#
Convert time number (usually hours before a certain date) into two strings: one for the date (example: 2022.04.31) and one for the hour (example: 14). See cftime.num2date for details on units and calendar. Automatically converts time number from UTC to local timezone based on lat, lon coordinates. This function was created originally for the EnvironmentAnalysis class.
- Parameters:
time_num (float) – Time number to be converted.
units (str) – Units of the time number. See cftime.num2date for details.
timezone (pytz.timezone) – Timezone to which the time number will be converted. See pytz.timezone for details.
calendar (str, optional) – Calendar to be used. See cftime.num2date for details.
- Returns:
date_string (str) – Date string.
hour_string (str) – Hour string.
date_time (datetime.datetime) – Datetime object.
- rocketpy.tools.geopotential_to_height_asl(geopotential, radius=63781370, g=9.80665)[source]#
Compute height above sea level from geopotential.
Source: https://en.wikipedia.org/wiki/Geopotential
- Parameters:
geopotential (float) – Geopotential in m^2/s^2. It is the geopotential value at a given pressure level, to be converted to height above sea level.
radius (float, optional) – Earth radius in m. Default is 63781370 m.
g (float, optional) – Gravity acceleration in m/s^2. Default is 9.80665 m/s^2.
- Returns:
geopotential_to_height_asl – Height above sea level in m
- Return type:
float
Examples
>>> from rocketpy.tools import geopotential_to_height_asl >>> geopotential_to_height_asl(0) 0.0 >>> geopotential_to_height_asl(100000) 10198.792680243916 >>> geopotential_to_height_asl(200000) 20400.84750449947
- rocketpy.tools.geopotential_to_height_agl(geopotential, elevation, radius=63781370, g=9.80665)[source]#
Compute height above ground level from geopotential and elevation.
- Parameters:
geopotential (float) – Geopotential in m^2/s^2. It is the geopotential value at a given pressure level, to be converted to height above ground level.
elevation (float) – Surface elevation in m
radius (float, optional) – Earth radius in m. Default is 63781370 m.
g (float, optional) – Gravity acceleration in m/s^2. Default is 9.80665 m/s^2.
- Returns:
height_above_ground_level – Height above ground level in m
- Return type:
float
Examples
>>> from rocketpy.tools import geopotential_to_height_agl >>> geopotential_to_height_agl(0, 0) 0.0 >>> geopotential_to_height_agl(100000, 0) 10198.792680243916 >>> geopotential_to_height_agl(100000, 1000) 9198.792680243916