cppqed-multidiagonal 0.4.0
Multidiagonal sparse operator algebra for C++QED
Loading...
Searching...
No Matches
cppqedutils Namespace Reference

Namespace alias for Boost.Hana, used for compile-time sequences and heterogeneous containers. More...

Namespaces

namespace  multiarray
 

Classes

struct  ConstReferenceMF
 Metafunction yielding the const-reference type of State. Defaults to const State&. Specialized for view types to return a const view. More...
 
struct  ConstReferenceMF< MultiArrayView< T, RANK > >
 
class  MultiArray
 Owning, move-only, rank-RANK array. Non-copyable; follows value semantics regarding constness. More...
 
class  MultiArrayView
 A non-owning, strided view over a contiguous buffer. Should be passed by value. More...
 
struct  ReferenceMF
 Metafunction yielding the reference type of State. Defaults to State&. Specialized for view types to return the view itself, since views are already reference-like. More...
 
struct  ReferenceMF< MultiArrayView< T, RANK > >
 

Concepts

concept  scalar
 Concept satisfied by scalar numeric types usable as operator coefficients: any type convertible to double or dcomp.
 

Typedefs

template<size_t RANK>
using Extents = std::array< size_t, RANK >
 Compile-time–fixed shape and stride descriptor for a rank-RANK array. Should be passed by value, but can be std::moved in class constructors.
 
template<typename T , size_t RANK>
using MultiArrayConstView = MultiArrayView< const T, RANK >
 Convenience alias: a view over immutable elements.
 
using LogTree = json::object
 Namespace alias for Boost.JSON, used for structured logging and JSON serialization.
 
template<typename State >
using Reference = typename ReferenceMF< State >::type
 Convenience alias for ReferenceMF<State>::type.
 
template<typename State >
using ConstReference = typename ConstReferenceMF< State >::type
 Convenience alias for ConstReferenceMF<State>::type.
 

Functions

template<typename Type , std::size_t... sizes>
constexpr auto concatenate (const std::array< Type, sizes > &... arrays)
 Concatenates multiple std::array instances of different sizes into one.
 
template<size_t RANK>
auto & incrementMultiIndex (Extents< RANK > &idx, Extents< RANK > extents)
 Increments a multi-index in last-index-fastest order, in-place.
 
auto & incrementMultiIndex (Extents< 0 > &idx, Extents< 0 >)
 Rank-0 no-op overload of incrementMultiIndex to satisfy template instantiation for degenerate cases.
 
template<typename T1 , typename T2 , size_t RANK>
void checkExtents (MultiArrayConstView< T1, RANK > m1, MultiArrayConstView< T2, RANK > m2, std::string message)
 Debug-mode extent check between two views. Throws std::runtime_error on mismatch.
 
template<typename T1 , typename T2 , size_t RANK>
bool isEqual (MultiArrayConstView< T1, RANK > m1, MultiArrayConstView< T2, RANK > m2)
 Element-by-element equality test. Traverses all elements via incrementMultiIndex.
 
template<typename T , size_t RANK>
void checkContiguous (MultiArrayView< T, RANK > mav, std::string message)
 Debug-mode check that a view covers its storage as an owning MultiArray of the same extents would: offset 0 and first-index-fastest strides. Throws std::invalid_argument otherwise. For functions that address the flat storage directly; a sub-block or slice view fails it.
 
template<size_t RANK>
auto vectorize (MultiArray< dcomp, RANK > &ma)
 Maps a mutable MultiArray<dcomp,RANK> to an Eigen column vector (no data copy).
 
template<size_t RANK>
auto vectorize (const MultiArray< dcomp, RANK > &ma)
 Maps a const MultiArray<dcomp,RANK> to a const Eigen column vector (no data copy).
 
template<size_t TWO_TIMES_RANK>
requires ( !(TWO_TIMES_RANK%2) )
Extents< TWO_TIMES_RANK/2 > halveExtents (Extents< TWO_TIMES_RANK > extents)
 Extracts the first half of a rank-2R extent array, verifying that both halves are equal.
 
template<size_t TWO_TIMES_RANK>
auto matricize (MultiArray< dcomp, TWO_TIMES_RANK > &ma)
 Maps a mutable rank-2R MultiArray<dcomp> to an Eigen square matrix (no data copy). The matrix dimension is prod(extents[0..R-1]); requires equal first and second half extents.
 
template<size_t TWO_TIMES_RANK>
auto matricize (const MultiArray< dcomp, TWO_TIMES_RANK > &ma)
 Maps a const rank-2R MultiArray<dcomp> to a const Eigen square matrix (no data copy).
 
template<size_t RANK1, size_t RANK2>
auto directProduct (const MultiArray< dcomp, RANK1 > &m1, const MultiArray< dcomp, RANK2 > &m2, std::function< dcomp(dcomp, dcomp)> func=std::multiplies< dcomp >{})
 Tensor (outer) product of two complex multi-arrays.
 
template<size_t RANK>
MultiArray< dcomp, RANK > & conj (MultiArray< dcomp, RANK > &ma)
 In-place complex conjugation of all elements of ma.
 
template<size_t RANK>
double frobeniusNormSqr (const MultiArray< dcomp, RANK > &ma)
 Squared Frobenius norm: \(\sum_{i}|a_i|^2\). Returns 0 for an empty array.
 
template<size_t RANK>
double frobeniusNorm (const MultiArray< dcomp, RANK > &ma)
 Frobenius norm: \(\sqrt{\sum_{i}|a_i|^2}\).
 
template<size_t TWO_TIMES_RANK>
void hermitianConjugateSelf (MultiArrayView< dcomp, TWO_TIMES_RANK > mav)
 In-place Hermitian conjugation of a rank-2R matrix view mav.
 
template<size_t TWO_TIMES_RANK>
void twoTimesRealPartOfSelf (MultiArrayView< dcomp, TWO_TIMES_RANK > mav)
 In-place replacement of a rank-2R matrix view mav with M + M† (twice its Hermitian part).
 
std::string toStringJSON (auto &&v)
 Serializes any JSON-convertible value to a string via json::value_from.
 

Variables

template<typename T , size_t RANK>
constexpr auto passByValue_v< MultiArrayView< T, RANK > > = true
 MultiArrayView is cheap to copy and should always be passed by value.
 
template<typename T , size_t RANK>
constexpr auto passByValue_v< MultiArray< T, RANK > > = false
 MultiArray owns its data and must not be passed by value.
 
template<typename T >
constexpr auto passByValue_v =std::nullopt
 Primary template for the pass-by-value trait. Specializations are true for view types (e.g. MultiArrayView) and false for owning types (e.g. MultiArray). The primary template returns std::nullopt to signal the absence of a specialization.
 

Detailed Description

Namespace alias for Boost.Hana, used for compile-time sequences and heterogeneous containers.

Typedef Documentation

◆ ConstReference

Convenience alias for ConstReferenceMF<State>::type.

◆ Extents

template<size_t RANK>
using cppqedutils::Extents = typedef std::array<size_t,RANK>

Compile-time–fixed shape and stride descriptor for a rank-RANK array. Should be passed by value, but can be std::moved in class constructors.

◆ LogTree

Namespace alias for Boost.JSON, used for structured logging and JSON serialization.

Alias for a JSON object used as a structured log tree. Passed through the trajectory and structure layers to accumulate human-readable, machine-parseable log data.

◆ MultiArrayConstView

Convenience alias: a view over immutable elements.

◆ Reference

Convenience alias for ReferenceMF<State>::type.

Function Documentation

◆ checkContiguous()

template<typename T , size_t RANK>
void cppqedutils::checkContiguous ( MultiArrayView< T, RANK >  mav,
std::string  message 
)

Debug-mode check that a view covers its storage as an owning MultiArray of the same extents would: offset 0 and first-index-fastest strides. Throws std::invalid_argument otherwise. For functions that address the flat storage directly; a sub-block or slice view fails it.

◆ checkExtents()

template<typename T1 , typename T2 , size_t RANK>
void cppqedutils::checkExtents ( MultiArrayConstView< T1, RANK >  m1,
MultiArrayConstView< T2, RANK >  m2,
std::string  message 
)

Debug-mode extent check between two views. Throws std::runtime_error on mismatch.

◆ concatenate()

template<typename Type , std::size_t... sizes>
constexpr auto cppqedutils::concatenate ( const std::array< Type, sizes > &...  arrays)
constexpr

Concatenates multiple std::array instances of different sizes into one.

Overcomes the limitation that std::views::join cannot join std::array instances of different sizes, since the size is part of the type. Uses C++17 fold expressions.

Template Parameters
TypeThe element type, common to all input arrays.
sizesThe sizes of the input arrays, deduced from the arguments.
Parameters
arraysThe arrays to concatenate.
Returns
A new std::array<Type, (sizes+...)> containing all elements in order.
See also
http://stackoverflow.com/a/42774523/1171157

◆ conj()

template<size_t RANK>
MultiArray< dcomp, RANK > & cppqedutils::conj ( MultiArray< dcomp, RANK > &  ma)

In-place complex conjugation of all elements of ma.

◆ directProduct()

template<size_t RANK1, size_t RANK2>
auto cppqedutils::directProduct ( const MultiArray< dcomp, RANK1 > &  m1,
const MultiArray< dcomp, RANK2 > &  m2,
std::function< dcomp(dcomp, dcomp)>  func = std::multiplies<dcomp>{} 
)

Tensor (outer) product of two complex multi-arrays.

Returns a MultiArray<dcomp, RANK1+RANK2> with extents concatenate(m1.extents, m2.extents). Element (i,j) of the result is func(m1[i], m2[j]); defaults to pointwise multiplication. Storage layout: result[i + stride*j] where stride = m1.dataView.size().

◆ frobeniusNorm()

template<size_t RANK>
double cppqedutils::frobeniusNorm ( const MultiArray< dcomp, RANK > &  ma)

Frobenius norm: \(\sqrt{\sum_{i}|a_i|^2}\).

◆ frobeniusNormSqr()

template<size_t RANK>
double cppqedutils::frobeniusNormSqr ( const MultiArray< dcomp, RANK > &  ma)

Squared Frobenius norm: \(\sum_{i}|a_i|^2\). Returns 0 for an empty array.

◆ halveExtents()

template<size_t TWO_TIMES_RANK>
requires ( !(TWO_TIMES_RANK%2) )
Extents< TWO_TIMES_RANK/2 > cppqedutils::halveExtents ( Extents< TWO_TIMES_RANK >  extents)

Extracts the first half of a rank-2R extent array, verifying that both halves are equal.

A MultiArray<dcomp, 2R> with equal first and second half extents represents a square matrix (or tensor product of square matrices) of total dimension prod(extents[0..R-1]). Throws std::invalid_argument in debug mode if the two halves differ.

◆ hermitianConjugateSelf()

template<size_t TWO_TIMES_RANK>
void cppqedutils::hermitianConjugateSelf ( MultiArrayView< dcomp, TWO_TIMES_RANK >  mav)

In-place Hermitian conjugation of a rank-2R matrix view mav.

Conjugates the diagonal, and swaps (i,j) and (j,i) while conjugating both. Operates directly on flat storage, so mav must be full and contiguous (checked by checkContiguous in debug builds); pass an owning array as ma.mutableView().

◆ incrementMultiIndex() [1/2]

auto & cppqedutils::incrementMultiIndex ( Extents< 0 > &  idx,
Extents< 0 >   
)
inline

Rank-0 no-op overload of incrementMultiIndex to satisfy template instantiation for degenerate cases.

◆ incrementMultiIndex() [2/2]

template<size_t RANK>
auto & cppqedutils::incrementMultiIndex ( Extents< RANK > &  idx,
Extents< RANK >  extents 
)

Increments a multi-index in last-index-fastest order, in-place.

Note
The iteration order is the opposite of the memory order: storage is first-index-fastest (see multiarray::calculateStrides), so successive multi-indices are not adjacent in memory unless all axes but the last have extent 1.

The iteration pattern is: increment the last index; on overflow, reset it to zero and carry to the previous index. The loop termination guard is idx[0] != extents[0] — once the leading index overflows the array is exhausted. The final increment leaves idx in an out-of-bounds state, consistent with how unchecked iterators work.

Note
TODO: formulate as a std::ranges-compliant range; see SliceIterator.h for a draft.

◆ isEqual()

template<typename T1 , typename T2 , size_t RANK>
bool cppqedutils::isEqual ( MultiArrayConstView< T1, RANK >  m1,
MultiArrayConstView< T2, RANK >  m2 
)

Element-by-element equality test. Traverses all elements via incrementMultiIndex.

Note
Not spelled operator== — this can be an expensive operation.

◆ matricize() [1/2]

template<size_t TWO_TIMES_RANK>
auto cppqedutils::matricize ( const MultiArray< dcomp, TWO_TIMES_RANK > &  ma)

Maps a const rank-2R MultiArray<dcomp> to a const Eigen square matrix (no data copy).

◆ matricize() [2/2]

template<size_t TWO_TIMES_RANK>
auto cppqedutils::matricize ( MultiArray< dcomp, TWO_TIMES_RANK > &  ma)

Maps a mutable rank-2R MultiArray<dcomp> to an Eigen square matrix (no data copy). The matrix dimension is prod(extents[0..R-1]); requires equal first and second half extents.

◆ toStringJSON()

std::string cppqedutils::toStringJSON ( auto &&  v)

Serializes any JSON-convertible value to a string via json::value_from.

Parameters
vAny value for which boost::json::value_from is defined.
Returns
A JSON string representation of v.

◆ twoTimesRealPartOfSelf()

template<size_t TWO_TIMES_RANK>
void cppqedutils::twoTimesRealPartOfSelf ( MultiArrayView< dcomp, TWO_TIMES_RANK >  mav)

In-place replacement of a rank-2R matrix view mav with M + M† (twice its Hermitian part).

The diagonal becomes 2*re(mav(i,i)). For each pair i < j, mav(i,j) becomes mav(i,j) + conj(mav(j,i)), and mav(j,i) its conjugate. C++QED's master equation uses it to complete \((H_\text{eff}/i)\rho\) to \((H_\text{eff}/i)\rho + \rho\,(H_\text{eff}/i)^\dagger\), and to make \(\rho\) exactly Hermitian after each step. Operates directly on flat storage, so mav must be full and contiguous (checked by checkContiguous in debug builds).

◆ vectorize() [1/2]

template<size_t RANK>
auto cppqedutils::vectorize ( const MultiArray< dcomp, RANK > &  ma)

Maps a const MultiArray<dcomp,RANK> to a const Eigen column vector (no data copy).

◆ vectorize() [2/2]

template<size_t RANK>
auto cppqedutils::vectorize ( MultiArray< dcomp, RANK > &  ma)

Maps a mutable MultiArray<dcomp,RANK> to an Eigen column vector (no data copy).

Variable Documentation

◆ passByValue_v

template<typename T >
constexpr auto cppqedutils::passByValue_v =std::nullopt
constexpr

Primary template for the pass-by-value trait. Specializations are true for view types (e.g. MultiArrayView) and false for owning types (e.g. MultiArray). The primary template returns std::nullopt to signal the absence of a specialization.

◆ passByValue_v< MultiArray< T, RANK > >

MultiArray owns its data and must not be passed by value.

◆ passByValue_v< MultiArrayView< T, RANK > >

MultiArrayView is cheap to copy and should always be passed by value.