MathUtils
A library of mathematical utilities.
Loading...
Searching...
No Matches
funs.hpp File Reference

Numerical functions. More...

#include "mathutils/log_factorial_lookup_table.hpp"
#include "mathutils/simple_generator.hpp"
#include "mathutils/spherical_harmonics_index_lookup_table.hpp"
#include <Eigen/Dense>
#include <array>
#include <cmath>
#include <complex>
#include <coroutine>
#include <cstdint>
#include <stdexcept>
Include dependency graph for funs.hpp:

Go to the source code of this file.

Functions

template<typename T>
int mathutils::sign (T x)
 Compute the sign (1,-1, or 0) of a value.
int mathutils::special::minus_one_to_int_pow (int n)
 Compute \((-1)^n\) for integer n.
double mathutils::special::log_factorial (uint64_t n)
 Compute \(\ln(n!)\) for a non-negative integer n. Uses a lookup table for small values and lgamma for larger values.
template<typename T>
double mathutils::special::Heaviside (T x)
 The Heaviside step function. Returns 0.0 for negative values, 1.0 for positive values, and 0.5 for zero.
template<typename T>
double mathutils::special::ReLog (const T &x)
 Compute the real part of the complex logarithm \(Re(Log(x))= ln(|x|)\). Returns \(Re(Log(0))=-\infty\) for \(x=0\).
template<typename T>
std::pair< double, int > mathutils::special::ReLogRe_ImLogRe_over_pi (const T &x)
 Compute \(Re(Log(Re(x)))\) and \(Im(Log(Re(x)))/\pi\).
template<typename T>
std::pair< double, int > mathutils::special::ReLogRe_ImLogRe_over_pi_of_x_to_pow (const T &x, int n)
int mathutils::special::spherical_harmonic_index_n_LM (int l, int m)
 Compute the index of (l,m) in [(0,0), (1,-1), (1,0), (1,1), (2,-2), ...].
std::array< int, 2 > mathutils::special::spherical_harmonic_index_lm_N (int n)
 Compute the n-th element of the sequence [(0,0), (1,-1), (1,0), (1,1), (2,-2), ...].
double mathutils::special::reduced_spherical_Pmm (int m, double theta)
 Compute the associated Legendre polynomial with spherical harmonics normalization \(P_{m m}(\theta) = e^{-im\phi}Y_{m m}(\theta, \phi)\) for \(0\leq\theta\leq\pi/2\) and \(m\geq 0\). Used to initialize the recursion.
mathutils::SimpleGenerator< double > mathutils::special::generate_reduced_spherical_Plm_recursion_three_term_upward_ell (int l_start, int l_end, double theta)
 Generate the associated Legendre polynomial with spherical harmonics normalization \(P_{\ell m}(\theta) = e^{-im\phi}Y_{\ell m}(\theta, \phi)\) for \(\ell = \ell_{start}, \ell_{start} + 1, \ldots, \ell_{end}\) and \(m=\ell_{start}\).
mathutils::SimpleGenerator< std::pair< int, double > > mathutils::special::enumerate_reduced_spherical_Plm_recursion_three_term_upward_ell (int l_start, int l_end, double theta)
double mathutils::special::reduced_spherical_Plm (int l, int m, double theta)
 Compute the associated Legendre polynomial with spherical harmonics normalization \(P_{\ell m}(\theta) = e^{-im\phi}Y_{\ell m}(\theta, \phi)\) for \(0\leq\theta\leq\pi/2\) and \(m\geq 0\).
double mathutils::special::spherical_Plm (int l, int m, double theta)
 Compute the associated Legendre polynomial with spherical harmonics normalization \(P_{\ell m}(\theta) = e^{-im\phi}Y_{\ell m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
double mathutils::special::recursive_real_Ylm (int l, int m, double theta, double phi)
 Compute the real spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
double mathutils::special::real_Ylm (int l, int m, double theta, double phi)
 Compute the real spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
Eigen::VectorXd mathutils::special::real_Ylm (int l, int m, const Eigen::MatrixXd &thetaphi_coord_P)
 Compute the real spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
Eigen::MatrixXd mathutils::special::compute_all_real_Ylm (int l_max, const Eigen::MatrixXd &thetaphi_coord_P)
 Compute real spherical harmonics \(y_{l m}(\theta, \phi)\) for \(\ell=0,...,\ell_{max}\) at the given coordinates.
Eigen::MatrixXcd mathutils::special::compute_all_Ylm (int l_max, const Eigen::MatrixXd &thetaphi_coord_P)
std::complex< double > mathutils::special::recursive_Ylm (int l, int m, double theta, double phi)
 Compute the spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
std::complex< double > mathutils::special::Ylm (int l, int m, double theta, double phi)
 Compute the spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
Eigen::VectorXcd mathutils::special::Ylm (int l, int m, const Eigen::MatrixXd &thetaphi_coord_P)
 Compute the spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).
double mathutils::special::series_real_Ylm (int l, int m, double theta, double phi)
Eigen::VectorXd mathutils::special::series_real_Ylm (int l, int m, const Eigen::MatrixXd &thetaphi_coord_P)
std::complex< double > mathutils::special::series_Ylm (int l, int m, double theta, double phi)
Eigen::VectorXcd mathutils::special::series_Ylm (int l, int m, const Eigen::MatrixXd &thetaphi_coord_P)
Eigen::MatrixXd mathutils::special::compute_all_series_real_Ylm (int l_max, const Eigen::MatrixXd &thetaphi_coord_P)
 Compute real spherical harmonics \(y_{l m}(\theta, \phi)\) for \(\ell=0,...,\ell_{max}\) at the given coordinates.
Eigen::MatrixXcd mathutils::special::compute_all_series_Ylm (int l_max, const Eigen::MatrixXd &thetaphi_coord_P)
 Compute spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(\ell=0,...,\ell_{max}\) at the given coordinates.
double mathutils::special::phi_independent_Ylm (int l, int m, double theta)
 Compute Ylm(theta, phi)/exp(i*theta*phi) for m>=0 and 0<=theta<=pi.
std::complex< double > mathutils::special::old_Ylm (int l, int m, double theta, double phi)
double mathutils::special::old_real_Ylm (int l, int m, double theta, double phi)
Eigen::MatrixXd mathutils::special::old_compute_all_real_Ylm (int l_max, const Eigen::MatrixXd &thetaphi_coord_P)

Detailed Description

Numerical functions.

Function Documentation

◆ compute_all_real_Ylm()

Eigen::MatrixXd mathutils::special::compute_all_real_Ylm ( int l_max,
const Eigen::MatrixXd & thetaphi_coord_P )

Compute real spherical harmonics \(y_{l m}(\theta, \phi)\) for \(\ell=0,...,\ell_{max}\) at the given coordinates.

Parameters
l_maxThe maximum order of the spherical harmonics (non-negative integer).
thetaphi_coord_PA matrix of shape (num_points, 2) where each row contains the polar angle \(\theta\) and azimuthal angle \(\phi\) in radians.
Returns
A matrix of shape (num_points, l_max * (l_max + 2) + 1)

◆ compute_all_series_real_Ylm()

Eigen::MatrixXd mathutils::special::compute_all_series_real_Ylm ( int l_max,
const Eigen::MatrixXd & thetaphi_coord_P )

Compute real spherical harmonics \(y_{l m}(\theta, \phi)\) for \(\ell=0,...,\ell_{max}\) at the given coordinates.

Parameters
l_maxThe maximum order of the spherical harmonics (non-negative integer).
thetaphi_coord_PA matrix of shape (num_points, 2) where each row contains the polar angle \(\theta\) and azimuthal angle \(\phi\) in radians.
Returns
A matrix of shape (num_points, l_max * (l_max + 2) + 1)

◆ compute_all_series_Ylm()

Eigen::MatrixXcd mathutils::special::compute_all_series_Ylm ( int l_max,
const Eigen::MatrixXd & thetaphi_coord_P )

Compute spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(\ell=0,...,\ell_{max}\) at the given coordinates.

Parameters
l_maxThe maximum order of the spherical harmonics (non-negative integer).
thetaphi_coord_PA matrix of shape (num_points, 2) where each row contains the polar angle \(\theta\) and azimuthal angle \(\phi\) in radians.
Returns
A matrix of shape (num_points, l_max * (l_max + 2) + 1)

◆ Heaviside()

template<typename T>
double mathutils::special::Heaviside ( T x)
inline

The Heaviside step function. Returns 0.0 for negative values, 1.0 for positive values, and 0.5 for zero.

Template Parameters
TThe type of the input value (must be comparable to T{0}).
Parameters
xThe input value.
Returns
The Heaviside step function value.

◆ log_factorial()

double mathutils::special::log_factorial ( uint64_t n)
inline

Compute \(\ln(n!)\) for a non-negative integer n. Uses a lookup table for small values and lgamma for larger values.

Parameters
nThe non-negative integer for which to compute log(n!).
Returns
The logarithm of the factorial of n.

◆ phi_independent_Ylm()

double mathutils::special::phi_independent_Ylm ( int l,
int m,
double theta )
inline

Compute Ylm(theta, phi)/exp(i*theta*phi) for m>=0 and 0<=theta<=pi.

Parameters
lDegree of the spherical harmonic.
mOrder of the spherical harmonic.
thetaPolar angle in radians.
Returns
Value of Ylm(theta, phi)/exp(i*theta*phi)

◆ real_Ylm() [1/2]

Eigen::VectorXd mathutils::special::real_Ylm ( int l,
int m,
const Eigen::MatrixXd & thetaphi_coord_P )

Compute the real spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the spherical harmonic ( \(\ell=0,1,\ldots\)).
mOrder of the spherical harmonic ( \(m=-\ell,\ldots,\ell\)).
thetaphi_coord_PMatrix containing the polar and azimuthal angles.
Returns
Eigen::VectorXd Values of the real spherical harmonics.

◆ real_Ylm() [2/2]

double mathutils::special::real_Ylm ( int l,
int m,
double theta,
double phi )
inline

Compute the real spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the spherical harmonic ( \(\ell=0,1,\ldots\)).
mOrder of the spherical harmonic ( \(m=-\ell,\ldots,\ell\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi\)).
phiAzimuthal angle in radians ( \(0\leq\phi<2\pi\)).
Returns
double Value of the real spherical harmonic.

◆ recursive_real_Ylm()

double mathutils::special::recursive_real_Ylm ( int l,
int m,
double theta,
double phi )
inline

Compute the real spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the spherical harmonic ( \(\ell=0,1,\ldots\)).
mOrder of the spherical harmonic ( \(m=-\ell,\ldots,\ell\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi\)).
phiAzimuthal angle in radians ( \(0\leq\phi<2\pi\)).
Returns
double Value of the real spherical harmonic.

◆ recursive_Ylm()

std::complex< double > mathutils::special::recursive_Ylm ( int l,
int m,
double theta,
double phi )
inline

Compute the spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the spherical harmonic ( \(\ell=0,1,\ldots\)).
mOrder of the spherical harmonic ( \(m=-\ell,\ldots,\ell\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi\)).
phiAzimuthal angle in radians ( \(0\leq\phi<2\pi\)).
Returns
std::complex<double> Value of the spherical harmonic.

◆ reduced_spherical_Plm()

double mathutils::special::reduced_spherical_Plm ( int l,
int m,
double theta )
inline

Compute the associated Legendre polynomial with spherical harmonics normalization \(P_{\ell m}(\theta) = e^{-im\phi}Y_{\ell m}(\theta, \phi)\) for \(0\leq\theta\leq\pi/2\) and \(m\geq 0\).

Parameters
lDegree of the polynomial ( \(\ell=0,1,\ldots\)).
mOrder of the polynomial ( \(m=0,1,\ldots,\ell\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi/2\)).
Returns
double Value of the associated normalized Legendre polynomial.

◆ reduced_spherical_Pmm()

double mathutils::special::reduced_spherical_Pmm ( int m,
double theta )
inline

Compute the associated Legendre polynomial with spherical harmonics normalization \(P_{m m}(\theta) = e^{-im\phi}Y_{m m}(\theta, \phi)\) for \(0\leq\theta\leq\pi/2\) and \(m\geq 0\). Used to initialize the recursion.

Parameters
mDegree/order of the polynomial ( \(m=0,1,\ldots\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi/2\)).
Returns
double Value of the associated normalized Legendre polynomial.

◆ ReLog()

template<typename T>
double mathutils::special::ReLog ( const T & x)
inline

Compute the real part of the complex logarithm \(Re(Log(x))= ln(|x|)\). Returns \(Re(Log(0))=-\infty\) for \(x=0\).

\[\mathbb{C}\rightarrow \mathbb{R}\cup\lbrace-\infty\rbrace\\ x\mapsto Re(Log(x)) \]

Parameters
xThe input value (can be floating-point or integral).
Returns
The logarithm of the input value.

◆ ReLogRe_ImLogRe_over_pi()

template<typename T>
std::pair< double, int > mathutils::special::ReLogRe_ImLogRe_over_pi ( const T & x)
inline

Compute \(Re(Log(Re(x)))\) and \(Im(Log(Re(x)))/\pi\).

Parameters
xThe input value (can be floating-point or integral).
Returns
std::pair<double, int> {re_log, arg_over_pi}

◆ sign()

template<typename T>
int mathutils::sign ( T x)
inline

Compute the sign (1,-1, or 0) of a value.

Template Parameters
TThe type of the input value (must be comparable to T{0}).
Parameters
xThe input value.
Returns
The sign of the input value.

◆ spherical_harmonic_index_lm_N()

std::array< int, 2 > mathutils::special::spherical_harmonic_index_lm_N ( int n)
inline

Compute the n-th element of the sequence [(0,0), (1,-1), (1,0), (1,1), (2,-2), ...].

Parameters
nThe linear index (must be non-negative).
Returns
std::array<int, 2> The (l,m) pair.

◆ spherical_harmonic_index_n_LM()

int mathutils::special::spherical_harmonic_index_n_LM ( int l,
int m )
inline

Compute the index of (l,m) in [(0,0), (1,-1), (1,0), (1,1), (2,-2), ...].

Parameters
lThe degree (non-negative).
mThe order (must be in the range [-l, l]).
Returns
int The spherical harmonic index.

◆ spherical_Plm()

double mathutils::special::spherical_Plm ( int l,
int m,
double theta )
inline

Compute the associated Legendre polynomial with spherical harmonics normalization \(P_{\ell m}(\theta) = e^{-im\phi}Y_{\ell m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the polynomial ( \(\ell=0,1,\ldots\)).
mOrder of the polynomial ( \(m=-\ell,\ldots,\ell\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi\)).
Returns
double Value of the associated normalized Legendre polynomial.

◆ Ylm() [1/2]

Eigen::VectorXcd mathutils::special::Ylm ( int l,
int m,
const Eigen::MatrixXd & thetaphi_coord_P )

Compute the spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the spherical harmonic ( \(\ell=0,1,\ldots\)).
mOrder of the spherical harmonic ( \(m=-\ell,\ldots,\ell\)).
thetaphi_coord_PMatrix containing the polar and azimuthal angles.
Returns
Eigen::VectorXcd Values of the spherical harmonics.

◆ Ylm() [2/2]

std::complex< double > mathutils::special::Ylm ( int l,
int m,
double theta,
double phi )
inline

Compute the spherical harmonics \(Y_{l m}(\theta, \phi)\) for \(0\leq\theta\leq\pi\) and \(-\ell \leq m\geq \ell\).

Parameters
lDegree of the spherical harmonic ( \(\ell=0,1,\ldots\)).
mOrder of the spherical harmonic ( \(m=-\ell,\ldots,\ell\)).
thetaPolar angle in radians ( \(0\leq\theta\leq\pi\)).
phiAzimuthal angle in radians ( \(0\leq\phi<2\pi\)).
Returns
std::complex<double> Value of the spherical harmonic.