Tomographer  v4.0
Tomographer C++ Framework Documentation
Tomographer::MathTools Namespace Reference

General math routines. More...

Namespaces

 SolveCLyap
 Definitions for solving the Complex Lyapunov Equation.
 
 SphCoords
 Utilities for hyperspherical coordinates.
 

Functions

template<typename Der1 , typename Der2 , typename fnType , typename ErrorStream >
bool check_derivatives (const Eigen::ArrayBase< Der1 > &derivatives, const Eigen::MatrixBase< Der2 > &point, fnType fn, std::size_t valdims, const typename Eigen::MatrixBase< Der2 >::Scalar delta=1e-6, const typename Eigen::MatrixBase< Der1 >::Scalar tol=1e-6, ErrorStream &error_stream=std::cerr)
 Check given derivatives against numerically-calculated finite differences. More...
 
template<typename VectorType >
void forcePosVecKeepSum (Eigen::Ref< VectorType > vec, const typename Eigen::NumTraits< typename VectorType::Scalar >::Real tolerance=1e-8)
 Make sure that the given vector is numerically positive. More...
 
template<typename MatrixType , typename Der >
MatrixType forcePosSemiDef (const Eigen::MatrixBase< Der > &rho, const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real tolerance=1e-8)
 Make sure that rho is numerically positive semidefinite. More...
 
template<typename MatrixType >
MatrixType safeOperatorSqrt (const Eigen::Ref< const MatrixType > &A, const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real tolerance=1e-8)
 Safe version of operator square root for positive semidefinite matrices. More...
 
template<typename MatrixType >
MatrixType safeOperatorInvSqrt (const Eigen::Ref< const MatrixType > &A, const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real tolerance=1e-8)
 Safe version of operator inverse square root for positive semidefinite matrices. More...
 
template<typename DerU , typename Rng , typename Log >
void randomUnitary (Eigen::MatrixBase< DerU > &U, Rng &rng, Log &logger)
 Generate a Haar-distributed random unitary. More...
 
template<typename Der1 , typename Rng >
void randomUnitary (Eigen::MatrixBase< Der1 > &U, Rng &rng)
 Overload of randomUnitary(U, rng, logger) which discards all logging messages.
 

Detailed Description

General math routines.

Function Documentation

§ check_derivatives()

template<typename Der1 , typename Der2 , typename fnType , typename ErrorStream >
bool Tomographer::MathTools::check_derivatives ( const Eigen::ArrayBase< Der1 > &  derivatives,
const Eigen::MatrixBase< Der2 > &  point,
fnType  fn,
std::size_t  valdims,
const typename Eigen::MatrixBase< Der2 >::Scalar  delta = 1e-6,
const typename Eigen::MatrixBase< Der1 >::Scalar  tol = 1e-6,
ErrorStream &  error_stream = std::cerr 
)

Check given derivatives against numerically-calculated finite differences.

This debugging utility is useful for making sure an analytical expression for the derivatives of a function is correct.

The function may be any general vector function, i.e.

\[ f : \mathbb{R}^{\texttt{xdims}} \rightarrow \mathbb{R}^{\texttt{valdims}} , \]

where we denote by \( f_i \) the i -th component of the value calculated by \( f \).

Parameters
derivativesthe values claimed to be the derivatives of fn at the given point, and which are to be checked against numerically-calculated finite differences. derivatives is an Eigen Array type such that

\[ \texttt{derivatives(i,k)} = \frac{\partial f_i}{\partial x_k} \]

pointthe point at which to calculate the function derivatives. It should be a column vector; the number of items in this vector (xdims above) should match the number of columns in derivatives.
fna callable which calculates the values of the function at given points. The first argument is a reference to a vector which should be filled with the function values, the second argument is the point at which to evaluate the function. The object or function fn will be called as
Eigen::VectorXd x(xdims) = ...;
Eigen::VectorXd val(valdims);
fn(val, x);
// val[i] should now store the value of f_i(x)
valdimsthe number of values calculated by fn, or, equivalently, the dimension of the output space of \( f \).
deltathe step to deviate from point to calculate a finite difference derivative in a particular direction.
tolthe tolerance we should accept for the difference between the numerically calculated derivative with finite differences, and the given value in derivatives.
error_streamIf the derivatives don't match, a report is also written to the given stream error_stream, which defaults to std::cerr.
Returns
This function returns true if all checked derivatives were within the given tolerance, or false if a failure was detected.

Definition at line 101 of file check_derivatives.h.

§ forcePosSemiDef()

template<typename MatrixType , typename Der >
MatrixType Tomographer::MathTools::forcePosSemiDef ( const Eigen::MatrixBase< Der > &  rho,
const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real  tolerance = 1e-8 
)
inline

Make sure that rho is numerically positive semidefinite.

This function replaces all eigenvalues that are less than tolerance by the value tolerance, in such a way that the trace of the matrix is preserved.

The original object rho is untouched, and the fixed version is returned.

Definition at line 96 of file pos_semidef_util.h.

§ forcePosVecKeepSum()

template<typename VectorType >
void Tomographer::MathTools::forcePosVecKeepSum ( Eigen::Ref< VectorType >  vec,
const typename Eigen::NumTraits< typename VectorType::Scalar >::Real  tolerance = 1e-8 
)
inline

Make sure that the given vector is numerically positive.

This function replaces all values in vec that are less than tolerance by the value tolerance, in such a way that the sum of all the elements of the vector is preserved.

The original vector vec is modified.

Definition at line 57 of file pos_semidef_util.h.

§ randomUnitary()

template<typename DerU , typename Rng , typename Log >
void Tomographer::MathTools::randomUnitary ( Eigen::MatrixBase< DerU > &  U,
Rng &  rng,
Log &  logger 
)
inline

Generate a Haar-distributed random unitary.

Parameters
Ureference to an Eigen type (already sized to a square matrix)
rnga std::random random number generator (such as std::mt19937)
loggera reference to a logger (Logging and Loggers) where we can log what we're doing.

Definition at line 62 of file random_unitary.h.

§ safeOperatorInvSqrt()

template<typename MatrixType >
MatrixType Tomographer::MathTools::safeOperatorInvSqrt ( const Eigen::Ref< const MatrixType > &  A,
const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real  tolerance = 1e-8 
)
inline

Safe version of operator inverse square root for positive semidefinite matrices.

This function first makes sure that the object A is positive semidefinite (à la forcePosSemiDef()), before taking the operator inverse square root.

A must be hermitian.

Definition at line 145 of file pos_semidef_util.h.

§ safeOperatorSqrt()

template<typename MatrixType >
MatrixType Tomographer::MathTools::safeOperatorSqrt ( const Eigen::Ref< const MatrixType > &  A,
const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real  tolerance = 1e-8 
)
inline

Safe version of operator square root for positive semidefinite matrices.

This function first makes sure that the object A is positive semidefinite (à la forcePosSemiDef()), before taking the operator square root.

A must be hermitian.

Definition at line 121 of file pos_semidef_util.h.