utilities.h#
Header file for miscellaneous utility functions.
- Author
- Rahil Makadia makadia2@illinois.edu 
LICENSE#
Copyright (C) 2022-2025 Rahil Makadia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, see https://www.gnu.org/licenses.
Defines
- 
real#
- Real type to be used for floating point calculations. Choose between double and long double. 
- 
PI#
- Value of PI. 
- 
RAD2DEG#
- Conversion factor from radians to degrees. 
- 
DEG2RAD#
- Conversion factor from degrees to radians. 
- 
EARTH_OBLIQUITY#
- Value of Earth’s obliquity in radians. 
- 
EARTH_RAD_WGS84#
- Value of Earth’s WGS84 radius in AU. 
- 
EARTH_FLAT_WGS84#
- Value of Earth’s WGS84 flattening. 
Functions
- 
void wrap_to_2pi(real &angle)#
- Wrap an angle to the range [0, 2*pi). - Parameters:
- angle – [inout] Angle to be wrapped. 
 
- 
void rad_to_deg(const real &rad, real °)#
- Convert an angle from radians to degrees. - Parameters:
- rad – [in] Angle in radians. 
- deg – [out] Angle in degrees. 
 
 
- 
real rad_to_deg(const real rad)#
- Convert an angle from radians to degrees. - Parameters:
- rad – [in] Angle in radians. 
- Returns:
- real Angle in degrees. 
 
- 
void deg_to_rad(const real °, real &rad)#
- Convert an angle from degrees to radians. - Parameters:
- deg – [in] Angle in degrees. 
- rad – [out] Angle in radians. 
 
 
- 
real deg_to_rad(const real deg)#
- Convert an angle from degrees to radians. - Parameters:
- deg – [in] Angle in degrees. 
- Returns:
- real Angle in radians. 
 
- 
void sort_vector(std::vector<real> &v, const bool &ascending, std::vector<size_t> &sortedIdx)#
- Sort a vector in ascending or descending order. - Parameters:
- v – [inout] Vector to be sorted. 
- ascending – [in] True for ascending order, false for descending order. 
- sortedIdx – [out] Vector of sorted indices from the original vector. 
 
 
- 
void sort_vector_by_idx(std::vector<std::vector<real>> &v, const std::vector<size_t> &sortedIdx)#
- Sort a vector of vectors based on the indices of another vector. - Parameters:
- v – [inout] Vector of vectors to be sorted. 
- sortedIdx – [in] Vector of sorted indices. 
 
 
- 
void vdot(const std::vector<real> &v1, const std::vector<real> &v2, real &dot)#
- Dot product of two vectors. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- dot – [out] Dot product of v1 and v2. 
 
 
- 
void vdot(const real *v1, const real *v2, const size_t &dim, real &dot)#
- Dot product of two vectors. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- dim – [in] Dimension of the vectors. 
- dot – [out] Dot product of v1 and v2. 
 
 
- 
void vnorm(const std::vector<real> &v, real &norm)#
- Norm of a vector. - Parameters:
- v – [in] Vector. 
- norm – [out] Norm of v. 
 
 
- 
void vnorm(const real *v, const size_t &dim, real &norm)#
- Norm of a vector. - Parameters:
- v – [in] Vector. 
- dim – [in] Dimension of the vector. 
- norm – [out] Norm of v. 
 
 
- 
void vunit(const std::vector<real> &v, std::vector<real> &unit)#
- Unit vector of a vector. - Parameters:
- v – [in] Vector. 
- vunit – [out] Unit vector of v. 
 
 
- 
void vunit(const real *v, const size_t &dim, real *unit)#
- Unit vector of a vector. - Parameters:
- v – [in] Vector. 
- dim – [in] Dimension of the vector. 
- unit – [out] Unit vector of v. 
 
 
- 
void vcross(const std::vector<real> &v1, const std::vector<real> &v2, std::vector<real> &cross)#
- Cross product of two vectors. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- v3 – [out] Cross product of v1 and v2. 
 
 
- 
void vcross(const real *v1, const real *v2, real *cross)#
- Cross product of two vectors. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- v3 – [out] Cross product of v1 and v2. 
 
 
- 
void vadd(const std::vector<real> &v1, const std::vector<real> &v2, std::vector<real> &sum)#
- Add two vectors. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- v3 – [out] Sum of v1 and v2. 
 
 
- 
void vsub(const std::vector<real> &v1, const std::vector<real> &v2, std::vector<real> &diff)#
- Subtract two vectors. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- v3 – [out] Difference of v1 and v2. 
 
 
- 
void vcmul(const std::vector<real> &v, const real &c, std::vector<real> &vc)#
- Multiply a vector by a constant. - Parameters:
- v – [in] Vector. 
- c – [in] Constant. 
- vc – [out] Product of v and c. 
 
 
- 
void vvmul(const std::vector<real> &v1, const std::vector<real> &v2, std::vector<real> &v3)#
- Multiply two vectors element-wise. - Parameters:
- v1 – [in] Vector 1. 
- v2 – [in] Vector 2. 
- v3 – [out] Element-wise product of v1 and v2. 
 
 
- 
void vabs_max(const std::vector<real> &v, real &max)#
- Find the maximum absolute value in a vector. - Parameters:
- v – [in] Vector. 
- max – [out] Maximum absolute value in v. 
 
 
- 
void vabs_max(const real *v, const size_t &dim, real &max)#
- Find the maximum absolute value in a vector. - Parameters:
- v – [in] Vector. 
- dim – [in] Dimension of the vector. 
- max – [out] Maximum absolute value in v. 
 
 
- 
void mat_vec_mul(const std::vector<std::vector<real>> &A, const std::vector<real> &v, std::vector<real> &Av)#
- Multiply a matrix by a vector. - Parameters:
- A – [in] Matrix. 
- v – [in] Vector. 
- Av – [out] Product of A and v. 
 
 
- 
void vec_mat_mul(const std::vector<real> &v, real **A, const size_t &dim, std::vector<real> &vA)#
- Multiply a vector by a matrix. - Parameters:
- v – [in] Vector. 
- A – [in] Matrix. 
- dim – [in] Dimension of the vector and matrix. 
- vA – [out] Product of v and A. 
 
 
- 
void mat_mat_mul(const std::vector<std::vector<real>> &A, const std::vector<std::vector<real>> &B, std::vector<std::vector<real>> &AB)#
- Multiply two matrices. - Parameters:
- A – [in] Matrix 1. 
- B – [in] Matrix 2. 
- AB – [out] Product of A and B. 
 
 
- 
void mat3_inv(const std::vector<std::vector<real>> &A, std::vector<std::vector<real>> &Ainv)#
- Invert a 3x3 matrix. - Parameters:
- A – [in] Matrix to be inverted. 
- Ainv – [out] Inverted matrix. 
 
 
- 
void mat3_mat3_mul(const real *A, const real *B, real *prod)#
- Multiply two 3x3 matrices. - Parameters:
- A – [in] Matrix 1. 
- B – [in] Matrix 2. 
- prod – [out] Product of A and B. 
 
 
- 
void mat3_mat3_add(const real *A, const real *B, real *sum)#
- Add two 3x3 matrices. - Parameters:
- A – [in] Matrix 1. 
- B – [in] Matrix 2. 
- sum – [out] Sum of A and B. 
 
 
- 
void rot_mat_x(const real &theta, std::vector<std::vector<real>> &R)#
- Rotation matrix about the x-axis. - Parameters:
- theta – [in] Angle of rotation in radians. 
- R – [out] Rotation matrix. 
 
 
- 
void rot_mat_y(const real &theta, std::vector<std::vector<real>> &R)#
- Rotation matrix about the y-axis. - Parameters:
- theta – [in] Angle of rotation in radians. 
- R – [out] Rotation matrix. 
 
 
- 
void rot_mat_z(const real &theta, std::vector<std::vector<real>> &R)#
- Rotation matrix about the z-axis. - Parameters:
- theta – [in] Angle of rotation in radians. 
- R – [out] Rotation matrix. 
 
 
- 
void LU_decompose(std::vector<std::vector<real>> &A, const size_t &N, const real &tol, size_t *P)#
- Decompose a matrix into its LU form. - Parameters:
- A – [inout] Matrix to be decomposed. 
- N – [in] Size of the matrix. 
- tol – [in] Tolerance for degeneracy. 
- P – [out] Permutation matrix. 
 
 
- 
void LU_inverse(std::vector<std::vector<real>> &A, const size_t *P, const size_t &N, std::vector<std::vector<real>> &AInv)#
- Invert a matrix using its LU decomposition. - Parameters:
- A – [in] Matrix to be inverted. 
- P – [in] Permutation matrix. 
- N – [in] Size of the matrix. 
- AInv – [out] Inverted matrix. 
 
 
- 
void mat_inv(std::vector<std::vector<real>> mat, std::vector<std::vector<real>> &matInv, const real &tol = 1.0e-16L)#
- Invert a matrix. - Parameters:
- mat – [in] Matrix to be inverted. 
- matInv – [out] Inverted matrix. 
- tol – [in] Tolerance for degeneracy.