Classical Monte Carlo

Solver

template<typename MatType, int Flags, typename RandomEngine>
class ClassicalMC : public mocca::internal::Solver<ClassicalMC<MatType, Flags, RandomEngine>>

A solver for calculating a matrix function \(f(\gamma \mathbf{A})\) using the “traditional” Monte Carlo algorithm created by Von Neumann. Any matrix function is supported as long as it can be represented as a power series:

\[ f(\gamma \mathbf{A}) = \sum_{k = 0}^\infty{c_k (\gamma \mathbf{A})^k} \]
where \( c_k > 0\) are the coefficients associated with each term and \(\gamma \in \mathbb{R}\) is the scaling parameter. For example, \( c_k = 1 / k! \) when calculating the matrix exponential.

After initializing the solver, call the funm() method to calculate the desired matrix function. You can use either the built-in functions (mittag_leffler, expm and inverse) or provide a function object that receive the value \( k \) and returns the coefficient \( c_k \).

The funm() method also accepts a vector as argument to calculate the action of the matrix function over this vector. This is more efficient than computing the full matrix function and then multiplying by the target vector.

Template Parameters:

Public Functions

inline explicit ClassicalMC(ClassicalMCOptions param = ClassicalMCOptions())

Initialize a ClassicalMC solver with the parameters param.

Complexity: Constant

Parameters:

param[in] parameters of the solver (see ClassicalMCOptions for all the available parameters)

inline explicit ClassicalMC(MatType &A, ClassicalMCOptions param = ClassicalMCOptions())

Initialize a ClassicalMC solver with the input matrix A and parameters param for further computing the matrix function \(f(\gamma \mathbf{A})\).

Complexity: Constant

Parameters:
  • A[in] input matrix

  • param[in] parameters of the solver (see ClassicalMCOptions for all the available parameters)

inline void set_param(ClassicalMCOptions param)

Set the parameters of the ClassicalMC solver.

Complexity: Constant

Parameters:

param[in] parameters of the solver (see ClassicalMCOptions for all the available parameters)

inline index_t rows() const
Returns:

the number of rows of the underlying matrix

inline index_t cols() const
Returns:

the number of columns of the underlying matrix

inline void set_matrix(MatType &A)

Initialize the ClassicalMC solver with the matrix A for further computing the matrix function \(f(\gamma \mathbf{A})\).

Parameters:

A[in] input matrix Complexity: Constant

template<typename Func>
inline evaluator<internal::MatFuncFull> funm(Func func, value_type gamma)

Calculates the matrix function \(f(\gamma \mathbf{A})\).

Parameters:
  • func[in] expm, mittag_leffler(alpha), inverse or a functor that receives a value k and returns the coefficient \( c_k\)

  • result[out] result matrix (dense)

  • gamma[in] scale parameter

  • first_row[in] first row in the range (default: 0)

  • last_row[in] last row in the range (not included). Accepts the special value kAllEntries to compute the entire matrix. (default: kAllEntries)

template<typename Func>
inline evaluator<sparse_vec_ref, internal::MatFuncAction> funm(Func func, value_type gamma, SparseVector<value_type> &v)

Calculates the action of the matrix function \(f(\gamma \mathbf{A})\) over the vector \( \vec{v} \).

Parameters:
  • func[in] expm, mittag_leffler(alpha), inverse or a functor that receives a value k and returns the coefficient \( c_k\)

  • v[in] input vector (dense or sparse)

  • result[out] result vector (dense)

  • gamma[in] scale parameter

  • first_row[in] first row in the range (default: 0)

  • last_row[in] last row in the range (not included). Accepts the special value kAllEntries to compute the entire matrix. (default: kAllEntries)

template<typename Func, internal::TypeLayout L>
inline value_type funm_single(Func func, index_t idx, value_type gamma, Vector<value_type, L> &v)

Calculates a single entry of \(f(\gamma \mathbf{A})v\).

Parameters:
  • func[in] expm, mittag_leffler(alpha), inverse or a functor that receives a value k and returns the coefficient \( c_k\)

  • idx[in] index of the entry of the solution to be calculated

  • v[in] input vector

  • gamma[in] scale parameter

Returns:

the entry i of \(f(\gamma \mathbf{A})v\)

Options

struct ClassicalMCOptions

Options for the ClassicalMC solver.

Public Members

index_t num_samples = 1E6

The number of random samples.

int max_num_terms = 100

Maximum number of terms in the Neumann Series.

double weight_cutoff = 1E-3

Weight tolerance. The Neumann series will be truncated when the weight of the random walk is less than this value

uint64_t seed = PCG32_DEFAULT_SEED

Indicate the random seed for the random number generator (PCG64).

uint64_t stream = PCG32_DEFAULT_STREAM

Indicate the random stream of the random number generator (PCG64).