Tomographer  v1.0a
Tomographer C++ Framework Documentation
Tomographer::Tools Namespace Reference

Various useful tools. More...

Classes

class  bad_fmts_format
 Exception for bad printf format. More...
 
struct  complex_real_scalar
 The Real scalar type corresponding to a std::complex type. More...
 
struct  complex_real_scalar< std::complex< RealScalar > >
 Implementation of complex_real_scalar for complex types. More...
 
class  conststr
 A constexpr string type. More...
 
class  eigen_assert_exception
 Exception that is thrown upon failed eigen_assert. More...
 
struct  eigen_std_vector
 Use this if you need an std::vector of any Eigen type. More...
 
struct  is_complex
 statically determine whether a type is complex More...
 
struct  SigHandlerTaskDispatcherStatusReporter
 A generic handler which requests a status report from an OMPTaskDispatcher. More...
 
struct  SignalHandler
 An abstract signal handler, in class form. More...
 
class  static_or_dynamic
 A type which stores a value possibly known at compile-time. More...
 
class  static_or_dynamic< T_, Eigen::Dynamic >
 Template Specialization – see static_or_dynamic<T_,Value> More...
 
struct  store_if_enabled
 Utility that stores a data type if a compile-time flag is enabled. More...
 
struct  store_if_enabled< T_, true >
 Specialization of store_if_enabled<T_,enabled> for if we're storing a value. More...
 

Functions

template<typename VectorType >
void force_pos_vec_keepsum (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 force_pos_semidef (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 safe_operator_sqrt (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 safe_operator_inv_sqrt (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 X >
std::enable_if< std::is_unsigned< X >::value, bool >::type is_positive (const X)
 Test whether the given value is positive or zero. More...
 
template<typename X >
std::enable_if<!std::is_unsigned< X >::value, bool >::type is_positive (const X val)
 See is_positive()
 
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, typename Eigen::MatrixBase< Der2 >::Scalar delta=1e-6, typename Eigen::MatrixBase< Der1 >::Scalar tol=1e-6, ErrorStream &error_stream=std::cerr)
 Check given derivatives against numerically-calculated finite differences. More...
 
std::string vfmts (const char *fmt, va_list vl)
 printf- formatting to a std::string, with va_list pointer More...
 
std::string fmts (const char *fmt,...)
 printf- format to a std::string More...
 
std::string fmt_duration (double seconds)
 Format a number of seconds into a human-readable string. More...
 
template<typename Rep , typename Period >
std::string fmt_duration (std::chrono::duration< Rep, Period > dt)
 Format a std::chrono::duration into a human-readable string. More...
 
template<typename TaskDispatcher , typename LoggerT >
SigHandlerTaskDispatcherStatusReporter< TaskDispatcher, LoggerT > makeSigHandlerTaskDispatcherStatusReporter (TaskDispatcher *tasks, LoggerT &logger)
 
template<typename SigHandler >
void installSignalStatusReportHandler (int signum, SigHandler *sobj)
 Installs the given signal handler to catch the signal signum. More...
 
template<typename F >
tomo_internal::FinalAction< F > finally (F f)
 implementation of a finally clause, somewhat like in Python More...
 
template<typename T >
std::ostreamoperator<< (std::ostream &str, store_if_enabled< T, false >)
 C++ Stream operators for store_if_enabled<T,enabled> More...
 
template<typename T >
std::ostreamoperator<< (std::ostream &str, store_if_enabled< T, true > val)
 C++ Stream operators for store_if_enabled<T,enabled> More...
 
constexpr bool is_power_of_two (int N)
 Return true if the argument is a power of two, false otherwise.
 
constexpr conststr extractFuncName (const conststr &funcname)
 Extract the function name from its signature. More...
 

Detailed Description

Various useful tools.

Function Documentation

template<typename Der1 , typename Der2 , typename fnType , typename ErrorStream >
bool Tomographer::Tools::check_derivatives ( const Eigen::ArrayBase< Der1 > &  derivatives,
const Eigen::MatrixBase< Der2 > &  point,
fnType  fn,
std::size_t  valdims,
typename Eigen::MatrixBase< Der2 >::Scalar  delta = 1e-6,
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 98 of file check_derivatives.h.

constexpr conststr Tomographer::Tools::extractFuncName ( const conststr funcname)
inline

Extract the function name from its signature.

The argument is parsed as a function signature, and the name of the function (including scope such as namespaces and classes) is returned.

Definition at line 350 of file util.h.

template<typename F >
tomo_internal::FinalAction<F> Tomographer::Tools::finally ( f)
inline

implementation of a finally clause, somewhat like in Python

Example Usage:

SomeResource * ptr = new SomeResource(..)
auto delete_ptr = finally([ptr]() { delete ptr; });
// Now, the pointer ptr will be 'delete'd at end of the current block.
...

Definition at line 117 of file util.h.

std::string Tomographer::Tools::fmt_duration ( double  seconds)
inline

Format a number of seconds into a human-readable string.

This formats the duration, given in seconds, into a human-readable string with hours, minutes seconds and milliseconds.

Parameters
secondsthe duration in seconds

Definition at line 170 of file fmt.h.

template<typename Rep , typename Period >
std::string Tomographer::Tools::fmt_duration ( std::chrono::duration< Rep, Period >  dt)
inline

Format a std::chrono::duration into a human-readable string.

This formats the duration into a human-readable string with hours, minutes seconds and milliseconds.

Parameters
dtthe duration, a std::chrono::duration

See also fmt_duration(double)

Definition at line 190 of file fmt.h.

std::string Tomographer::Tools::fmts ( const char *  fmt,
  ... 
)
inline

printf- format to a std::string

Does safe allocation, etc.

Definition at line 122 of file fmt.h.

template<typename MatrixType , typename Der >
MatrixType Tomographer::Tools::force_pos_semidef ( 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 93 of file pos_semidef_util.h.

template<typename VectorType >
void Tomographer::Tools::force_pos_vec_keepsum ( 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 54 of file pos_semidef_util.h.

template<typename SigHandler >
void Tomographer::Tools::installSignalStatusReportHandler ( int  signum,
SigHandler *  sobj 
)

Installs the given signal handler to catch the signal signum.

Warning
This will replace any previous signal handlers set, including for other signals!
Todo:
Don't replace the signal handlers for other signals. (Well ok, we only catch SIGINT anyway so this isn't a problem now.)

Definition at line 178 of file signal_status_report.h.

template<typename X >
std::enable_if<std::is_unsigned<X>::value, bool>::type Tomographer::Tools::is_positive ( const X  )
inline

Test whether the given value is positive or zero.

This helper is useful to silence warnings in templates about `comparision of unsigned >= 0 is always true'.

Definition at line 96 of file util.h.

template<typename T >
std::ostream& Tomographer::Tools::operator<< ( std::ostream str,
store_if_enabled< T, false >   
)
inline

C++ Stream operators for store_if_enabled<T,enabled>

Produces human-readable output.

Definition at line 257 of file util.h.

template<typename T >
std::ostream& Tomographer::Tools::operator<< ( std::ostream str,
store_if_enabled< T, true >  val 
)
inline

C++ Stream operators for store_if_enabled<T,enabled>

Produces human-readable output.

Definition at line 266 of file util.h.

template<typename MatrixType >
MatrixType Tomographer::Tools::safe_operator_inv_sqrt ( 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 force_pos_semidef()), before taking the operator inverse square root.

A must be hermitian.

Definition at line 142 of file pos_semidef_util.h.

template<typename MatrixType >
MatrixType Tomographer::Tools::safe_operator_sqrt ( 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 force_pos_semidef()), before taking the operator square root.

A must be hermitian.

Definition at line 118 of file pos_semidef_util.h.

std::string Tomographer::Tools::vfmts ( const char *  fmt,
va_list  vl 
)
inline

printf- formatting to a std::string, with va_list pointer

Does safe allocation, etc.

Definition at line 85 of file fmt.h.