SyTen
syten::SparseTensor< rank, Scalar > Class Template Reference

Sparse tensor of specified rank and scalar type. More...

#include <sparse.h>

+ Collaboration diagram for syten::SparseTensor< rank, Scalar >:

Classes

struct  FullData
 Struct holding the data for a full, non-singleton sparse tensors. More...
 

Public Types

typedef std::array< Index, rankCoord
 Coordinate type used to denote a set of indices. More...
 
typedef std::pair< Coord, Scalar > Element
 A single element of the sparse tensor, in the format <Coordinate, Value>. More...
 
typedef boost::container::flat_map< Coord, Scalar, SparseCoordComparator< rank > > ElementsVec
 Container holding the elements; currently a boost::container::flat_map (sorted vector) More...
 

Public Member Functions

template<typename RetScalar = Scalar>
ScalarBase< RetScalar >::type absSqd () const
 Squared norm of a sparse tensor, i.e. the squared 2-norm were the tensor reshaped into a vector. More...
 
MemoryUsage::MemorySize allocSize () const
 allocSize()/memory_size.h support. More...
 
void assignAdd (Coord coordinates, Scalar val)
 Adds a value at a point to the tensor if the supplied value is larger than SYTEN_ZERO_THRESHOLD. More...
 
void clear ()
 Deletes all entries in the tensor, sets it to zero. More...
 
Coord dim () const
 Returns an array specifying our dimension on each leg. More...
 
Index dim (Index which) const
 Returns the dimension of the which-th leg. More...
 
Coord dims () const
 Returns an array specifying our dimension on each leg. More...
 
void normalise ()
 Normalises *this s.t. norm(*this) == 1 More...
 
bool operator!= (SparseTensor const &b) const
 Returns false if b and *this are equal. More...
 
Scalar operator() (Coord const &coordinates) const
 Returns element at the specified coordinate by value or 0. More...
 
SparseTensor< rank, Scalar > operator* (Scalar c) const
 Multiplies a sparse tensor and a scalar value. More...
 
SparseTensor< rank, Scalar > operator*= (Scalar c)
 Scales each element of *this by the scalar value c More...
 
SparseTensor< rank, Scalar > operator+ (SparseTensor const &b) const
 Adds two sparse tensors element-wise. More...
 
SparseTensor< rank, Scalar > & operator+= (SparseTensor const &b)
 Adds b to *this element-wise. More...
 
SparseTensor< rank, Scalar > operator- (SparseTensor const &b) const
 Subtracts two sparse tensors element-wise. More...
 
SparseTensor< rank, Scalar > & operator-= (SparseTensor const &b)
 Subtracts b from *this element-wise. More...
 
SparseTensor< rank, Scalar > operator/ (Scalar c) const
 Divides a sparse tensor by a scalar value. More...
 
SparseTensor< rank, Scalar > operator/= (Scalar c)
 Divides each element of *this by the scalar value c More...
 
SparseTensoroperator= (SparseTensor &&o)
 Move assignment operator. More...
 
SparseTensoroperator= (SparseTensor const &o)
 Copy assignment operator. More...
 
bool operator== (SparseTensor const &b) const
 Returns true if b and *this are equal. More...
 
Scalar & operator[] (Coord const &coordinates)
 Returns a reference to the element at the specified coordinate. More...
 
Scalar const & operator[] (Coord const &coordinates) const
 Returns a const reference to the element at the specified coordinate or zero. More...
 
void reduce ()
 Reduces the number of data.first by dropping zero entries. More...
 
void regularise ()
 Regularises the tensor, i.e. More...
 
void reserve (Index size)
 Reserves space for at least size elements. More...
 
Index size () const
 Returns the number of nonzero elements in the tensor. More...
 
 SparseTensor ()
 Default constructor for empty tensors. More...
 
 SparseTensor (Coord dims_)
 Standard constructor used to initialise the dimensions of the tensor. More...
 
 SparseTensor (Scalar const &value)
 Constructor for singleton tensors. More...
 
 SparseTensor (SparseTensor &&o)
 Move ctor. More...
 
 SparseTensor (SparseTensor const &other)
 Copy ctor. More...
 
 SparseTensor (std::initializer_list< Index > const &il)
 Constructor for initializer lists. More...
 
 ~SparseTensor ()
 Destructor has to destroy either the singleton or the data. More...
 
Access to the singleton, don't call valSingleton without

checking for singletony-ness first!

bool isSingleton () const
 Returns true if this tensor is a singleton tensor. More...
 
Scalar const & valSingleton () const
 Returns the singleton value; only valid if the tensor is a singleton tensor. More...
 
Scalar & valSingleton ()
 Returns the singleton value; only valid if the tensor is a singleton tensor. More...
 
Iteration over stored elements, don't use without checking

for singletony-ness first!

Elementoperator[] (Index i)
 Returns the i-th stored element. More...
 
Element const & operator[] (Index i) const
 Returns the i-th stored element. More...
 
ElementsVec::iterator begin ()
 Iterator to the first element. More...
 
ElementsVec::const_iterator begin () const
 Const iterator to the first element. More...
 
ElementsVec::const_iterator cbegin () const
 Const iterator to the first element. More...
 
ElementsVec::iterator end ()
 Iterator past the last element. More...
 
ElementsVec::const_iterator end () const
 Const iterator past the last element. More...
 
ElementsVec::const_iterator cend () const
 Const iterator past the last element. More...
 
void insert (Element const &b)
 Adds an element without checking for duplicity. More...
 
void insert (Element &&b)
 Adds an element without checking for duplicity. More...
 
template<typename... Args>
ElementsVec::iterator emplace_hint (typename ElementsVec::const_iterator hint, Args &&... args)
 Constructs an element in-place using the hint. More...
 
ElementsVec::const_iterator lower_bound (Coord const &lb) const
 Returns the lower-bound iterator pointing at least at element key. More...
 
ElementsVec::const_iterator upper_bound (Coord const &lb) const
 Returns the upper-bound iterator pointing at most at element key. More...
 
Boost serialisation
template<typename Archive >
void save (Archive &ar, const unsigned int) const
 Serialisation. More...
 
template<typename Archive >
void load (Archive &ar, const unsigned int in_version)
 Deserialisation. More...
 

Static Public Attributes

static constexpr unsigned int version = 2
 Boost serialisation version. More...
 
static const Scalar zero
 Zero-value to return when operator[] const has to return a const reference to a nonexistant value. More...
 

Private Attributes

union {
   FullData   fullData
 Data used if this is not a singleton tensor. More...
 
   Scalar   singletonValue
 Value of the single element if this is a singleton tensor. More...
 
}; 
 
Scalar normSquared {0}
 Contains the squared norm of this tensor. More...
 
std::recursive_mutex normSquaredMtx
 Protects normSquared. More...
 
bool normSquaredVld {false}
 True iff normSquared contains a valid value. More...
 
bool singleton = false
 If true, the sparse tensor is a singleton with all dimensions equal to 1 and just one value. More...
 

Detailed Description

template<Rank rank, typename Scalar = CDef>
class syten::SparseTensor< rank, Scalar >

Sparse tensor of specified rank and scalar type.

Represented as a vector of pairs of sets of indices and values.

That means for a tensor of rank \( r \) with each leg of dimension \( d \), the sparse tensor needs \( (8r + c) f d^r \) bytes of storage, where \( c \) is the number of bytes used by each scalar and \( f \in [0, 1] \) is the filling factor. In comparison, a dense tensor needs \( c d^r \) bytes of storage.

Hence for a sparse tensor of rank 3 to be advantageous of a dense tensor of \( r = 3\), assuming std::complex<double> with size \( c = 16 \), we need the filling factor to be below \( f_{max,16} = 0.4 \) for a sparse tensor to be advantageous. If we set \( c = 80 \) as is the case for the 100 digit mp float from boost, we get \( f_{max,80} \approx 0.77 \).

Note however that this only concerns space, not time advantages. Dense tensors will be considerably faster considerably sooner than they are smaller.

As there are many singleton sparse tensors due to \( U(1) \) symmetries resulting in these trivial CGC spaces, there is a singleton optimisation whereby we directly store the singleton value instead of using a one-element vector of elements.

This class caches its squared norm internally. Hence, do not directly manipulate its data types unless you know very precisely what you're doing.


The documentation for this class was generated from the following file: