TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTK::Array< DataType > Class Template Reference

Multi-dimensional array. More...

#include <Array.h>

Inheritance diagram for TBTK::Array< DataType >:
TBTK::Serializable TBTK::AnnotatedArray< DataType, AxesType >

Public Member Functions

 Array ()
 
 Array (const std::initializer_list< unsigned int > &ranges)
 
 Array (const std::initializer_list< unsigned int > &ranges, const DataType &fillValue)
 
 Array (const std::vector< DataType > &vector)
 
 Array (const std::vector< std::vector< DataType >> &vector)
 
 Array (const Range &range)
 
 Array (const std::string &serialization, Mode mode)
 
template<typename CastType >
 operator Array< CastType > () const
 
DataType & operator[] (const std::vector< unsigned int > &index)
 
const DataType & operator[] (const std::vector< unsigned int > &index) const
 
DataType & operator[] (unsigned int n)
 
const DataType & operator[] (unsigned int n) const
 
Array operator+ (const Array &rhs) const
 
Array operator- (const Array &rhs) const
 
Array operator* (const DataType &rhs) const
 
Array operator* (const Array &rhs) const
 
Array operator/ (const DataType &rhs) const
 
bool operator== (const Array &rhs) const
 
Array< DataType > getSlice (const std::vector< Subindex > &index) const
 
Array< DataType > getArrayWithPermutedIndices (const std::vector< unsigned int > &permutation) const
 
Array< DataType > getArrayWithReversedIndices () const
 
const std::vector< unsigned int > & getRanges () const
 
CArray< DataType > & getData ()
 
const CArray< DataType > & getData () const
 
unsigned int getSize () const
 
std::string serialize (Mode mode) const
 
- Public Member Functions inherited from TBTK::Serializable
template<>
std::string serialize (const bool &data, Mode mode)
 
template<>
std::string serialize (const double &data, Mode mode)
 
template<>
std::string serialize (const std::complex< double > &data, Mode mode)
 
template<>
std::string serialize (const int &data, Mode mode)
 
template<>
std::string serialize (const unsigned int &data, Mode mode)
 
template<>
std::string serialize (const SpinMatrix &data, Mode mode)
 
template<>
std::string serialize (const Statistics &data, Mode mode)
 
template<>
std::string serialize (const std::vector< std::complex< double >> &data, Mode mode)
 
template<>
int deserialize (const std::string &serialization, Mode mode)
 

Static Public Member Functions

static Array create (const std::vector< unsigned int > &ranges)
 
static Array create (const std::vector< unsigned int > &ranges, const DataType &fillValue)
 
static Array contract (const Array &array0, const std::vector< Subindex > &pattern0, const Array &array1, const std::vector< Subindex > &pattern1)
 
- Static Public Member Functions inherited from TBTK::Serializable
static bool hasID (const std::string &serialization, Mode mode)
 
static std::string getID (const std::string &serialization, Mode mode)
 
static std::string extractComponent (const std::string &serialization, const std::string &containerID, const std::string &componentID, const std::string &componentName, Mode mode)
 

Friends

class Math::ArrayAlgorithms< DataType >
 
Array operator* (const DataType &lhs, const Array &rhs)
 
template<typename DT >
std::ostream & operator<< (std::ostream &stream, const Array< DT > &array)
 

Additional Inherited Members

- Public Types inherited from TBTK::Serializable
enum  Mode { Debug, Binary, XML, JSON }
 
- Static Protected Member Functions inherited from TBTK::Serializable
static bool validate (const std::string &serialization, const std::string &id, Mode mode)
 
static std::string getContent (const std::string &serialization, Mode mode)
 
static std::vector< std::string > split (const std::string &content, Mode mode)
 
template<typename DataType >
static std::enable_if<!std::is_pointer< DataType >::value, std::string >::type serialize (const DataType &data, Mode mode)
 
template<typename DataType >
static std::enable_if< std::is_pointer< DataType >::value, std::string >::type serialize (const DataType &data, Mode mode)
 
template<typename DataType >
static DataType deserialize (const std::string &serialization, Mode mode)
 
static std::string extract (const std::string &serialization, Mode mode, std::string component)
 

Detailed Description

template<typename DataType>
class TBTK::Array< DataType >

Multi-dimensional array.

The Array provides a convenient interface for handling multi-dimensional array.

Indexing

An Array is created using

Array<DataType> array({SIZE_X, SIZE_Y, SIZE_Z});

or alternatively

Array<DataType> array({SIZE_X, SIZE_Y, SIZE_Z}, value);

to initialize each element to 'value'.

The curly braces determines the Array ranges. Any number of dimensions is possible. Similarly, elements can be accessed using

array[{x, y, z}] = 10;
DataType value = array[{x, y, z}];

Arithmetics

It is possible to add and subtract Arrays with the same ranges.

Array<DataType> sum = array0 + array1;
Array<DataType> difference = array0 - array1;

It is also possible to multiply and divide an Array by a value with the same DataType as the Array elements.

DataType value = 10;
Array<DataType> product = value*array;
Array<DataType> quotient = array/value;

Slicing

Consider the code

Array<DataType> array({SIZE_X, SIZE_Y, SIZE_Z});
//Fill array with some values here.
//...
Array<DataType> slice = array.getSlice({_a_, 2, _a_});

Here slice will be an Array with ranges {SIZE_X, SIZE_Z} and satsify *slice[{x, z}] = array[{x, 2, z}].

Note: If you write library code for TBTK, use IDX_ALL instead of _a_.

Example

#include "TBTK/Array.h"
#include "TBTK/Streams.h"
#include "TBTK/TBTK.h"
#include <vector>
using namespace std;
using namespace TBTK;
int main(){
//Create Arrays.
Array<unsigned int> array0({2, 3, 4});
Array<unsigned int> array1({2, 3, 4});
Array<unsigned int> array2({2, 3, 4});
//Fill Arrays with values.
for(unsigned int x = 0; x < 2; x++){
for(unsigned int y = 0; y < 3; y++){
for(unsigned int z = 0; z < 4; z++){
array0[{x, y, z}] = x;
array1[{x, y, z}] = 2*y;
array2[{x, y, z}] = 3*z;
}
}
}
//Perform arithmetic operations on Arrays.
Array<unsigned int> result = array0 - array1/2 + 3*array2;
//Get the ranges for the result.
const vector<unsigned int> &ranges = result.getRanges();
Streams::out << "Result dimension: " << ranges.size() << "\n";
Streams::out << "Result ranges: ";
for(unsigned int n = 0; n < ranges.size(); n++)
Streams::out << ranges[n] << "\t";
Streams::out << "\n";
//Get slice containing data for all indices of the form {_a_, 0, _a_},
//where _a_ is a wildcard.
Array<unsigned int> slice = result.getSlice({_a_, 0, _a_});
//Get the ranges for the slice.
const vector<unsigned int> &sliceRanges = slice.getRanges();
Streams::out << "Slice dimension: " << sliceRanges.size() << "\n";
Streams::out << "Slice ranges: ";
for(unsigned int n = 0; n < sliceRanges.size(); n++)
Streams::out << sliceRanges[n] << "\t";
Streams::out << "\n";
//Print the values in the slice.
Streams::out << "Slice values:\n";
for(unsigned int x = 0; x < 2; x++){
for(unsigned int z = 0; z < 4; z++)
Streams::out << "\t" << slice[{x, z}];
Streams::out << "\n";
}
}

Output

Result dimension: 3
Result ranges: 2 3 4
Slice dimension: 2
Slice ranges: 2 4
Slice values:
0 9 18 27
1 10 19 28

Constructor & Destructor Documentation

◆ Array() [1/7]

template<typename DataType >
TBTK::Array< DataType >::Array

Constructor.

◆ Array() [2/7]

template<typename DataType >
TBTK::Array< DataType >::Array ( const std::initializer_list< unsigned int > &  ranges)
explicit

Constructor.

Parameters
rangesThe ranges of the Array.

◆ Array() [3/7]

template<typename DataType >
TBTK::Array< DataType >::Array ( const std::initializer_list< unsigned int > &  ranges,
const DataType &  fillValue 
)

Constructor.

Parameters
rangesThe ranges of the Array.
fillValueValue to fill the Array with.

◆ Array() [4/7]

template<typename DataType >
TBTK::Array< DataType >::Array ( const std::vector< DataType > &  vector)

Constructs an Array from an std::vector.

Parameters
vectorThe std::vector to copy from.

◆ Array() [5/7]

template<typename DataType >
TBTK::Array< DataType >::Array ( const std::vector< std::vector< DataType >> &  vector)

Constructs an Array from an std::vector.

Parameters
vectorThe std::vector to copy from.

◆ Array() [6/7]

template<typename DataType >
TBTK::Array< DataType >::Array ( const Range range)

Construct an Array from a Range. The created Array has the same number of elements as the resolution of the Range. The elements are initialized so that the first and last elements corresponds to the upper and lower bound of the range, and the intermediate values are equispaced between these bounds.

Parameters
rangeA Range object that specifies the number of elements and lower and upper bound.

◆ Array() [7/7]

template<typename DataType >
TBTK::Array< DataType >::Array ( const std::string &  serialization,
Mode  mode 
)

Constructs an Array from a serialization string.

Parameters
serializationSerialization string from which to construct the Array.
modeThe mode with which the string has been serialized.

Member Function Documentation

◆ contract()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::contract ( const Array< DataType > &  array0,
const std::vector< Subindex > &  pattern0,
const Array< DataType > &  array1,
const std::vector< Subindex > &  pattern1 
)
static

Contract two Arrays by summing over one or more common indices.

\( A_{ijk} = \sum_{ab}B_{ijab}C_{akb}\)

The function takes two Arrays and two patterns. The patterns must have the same number of Subindices as the corresponding Array and contain either wildcards or labeled wildcards (see Subindex).

Labeled wildcards in the two patterns are identified and the corresponding indices are summed over. The Subindices of the resulting Array is ordered in the same order as the original Arrays, with the Subindices of the first Array coming before those of the second.

If B and C are two @linkArray Arrays with four and three Subindices each, the expression above is calculated using

Array<DataType> A = Array<DataType>::contract(
B,
{_a_, _a_, _aX_(0), _aX_(1)}},
C,
{_aX_(0), _a_, _aX_(1)}
);
Parameters
array0The first array.
pattern0The pattern associated with the first Array.
array1The second Array.
pattern1The pattern associated with the second Array.
Returns
A new Array resulting from contracting the labled wildcards in the two Array.

◆ create() [1/2]

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::create ( const std::vector< unsigned int > &  ranges)
static

Create an array with a vector of ranges. Identical to calling the constructor using an std::initializer_list, but using a std::vector instead. Allows for the creation of an Arrays with dynamically assigned ranges.

Parameters
rangesThe ranges of the Array.

◆ create() [2/2]

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::create ( const std::vector< unsigned int > &  ranges,
const DataType &  fillValue 
)
static

Create an array with a vector of ranges. Identical to calling the constructor using an std::initializer_list, but using a std::vector instead. Allows for the creation of an Arrays with dynamically assigned ranges.

Parameters
rangesThe ranges of the Array.
fillValueValue to fill the Array with.

◆ getArrayWithPermutedIndices()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::getArrayWithPermutedIndices ( const std::vector< unsigned int > &  permutation) const

Get a new Array with permuted indices.

Parameters
permutationA list of integers from 0 to N-1, where N is the number of indices.
Returns
A new Array where the nth Subindex corresponds to the original Subindex in position permutation[n].

◆ getArrayWithReversedIndices()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::getArrayWithReversedIndices

Get a new Array with the indices in reverse order.

Returns
A new Array where the indices occurs in reverse order.

◆ getData() [1/2]

template<typename DataType >
const CArray< DataType > & TBTK::Array< DataType >::getData
inline

Get raw data. If the array has ranges {SIZE_X, SIZE_Y, SIZE_Z}, rawData[SIZE_Z*(SIZE_Y*x + y) + z = array[{x, y, z}].

Returns
The raw data as a linear CArray.

◆ getData() [2/2]

template<typename DataType >
const CArray<DataType>& TBTK::Array< DataType >::getData ( ) const

Get raw data. If the array has ranges {SIZE_X, SIZE_Y, SIZE_Z}, rawData[SIZE_Z*(SIZE_Y*x + y) + z = array[{x, y, z}].

Returns
The raw data as a linear CArray.

◆ getRanges()

template<typename DataType >
const std::vector< unsigned int > & TBTK::Array< DataType >::getRanges
inline

Get ranges.

Returns
The Arrays ranges.

◆ getSize()

template<typename DataType >
unsigned int TBTK::Array< DataType >::getSize
inline

Get the number of elements in the Array.

◆ getSlice()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::getSlice ( const std::vector< Subindex > &  index) const

Get a subset of the Array that results from setting one or multiple indices equal to given values.

Example

Array array({5, 5, 5});
Array slice = array.slice({_a_, 2, _a_});

The result is a two-dimensional Array for which slice({x, z}) = array({x, 2, z}).

Parameters
indexIndex into the Array.
Returns
An Array of lower dimension.

◆ operator Array< CastType >()

template<typename DataType >
template<typename CastType >
TBTK::Array< DataType >::operator Array< CastType >
inline

Type cast operator. Creates a copy of the Array with the data type changed to the cast type.

Returns
A new Array with the data type of the elements changed to CastType.

◆ operator*() [1/2]

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::operator* ( const Array< DataType > &  rhs) const

Multiplication operator. Multiplies two Arrays of rank one or two. If \(u_i\) and \(v_i\) are Arrays with a single Subindex and \(M_{ij}\) and \(N_{ij}\) are Arrays with two Subindices, the possible products are

Rank 1 times rank 1:

\(\sum_{i}u_{i}v_{i}\)

The result is an Array with rank 1 and a single element.

Rank 1 times rank 2:

\(\sum_{i}u_{i}M_{ij}\)

The result is an Array with rank 1.

Rank 2 times rank 1:

\(\sum_{j}M_{ij}u_{j}\)

The result is an Array with rank 1.

Rank 2 times rank 2:

\(\sum_{j}M_{ij}N_{jk}\)

The result is an Array with rank 2.

Parameters
rhsThe right hand side of the expression.
Returns
An Array containing the product of the two Arrays.

◆ operator*() [2/2]

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::operator* ( const DataType &  rhs) const
inline

Multiplication operator.

Parameters
rhsThe right hand side of the expression.
Returns
The product of the left and right hand side.

◆ operator+()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::operator+ ( const Array< DataType > &  rhs) const
inline

Addition operator.

Parameters
rhsThe right hand side of the expression.
Returns
The sum of the left and right hand side.

◆ operator-()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::operator- ( const Array< DataType > &  rhs) const
inline

Subtraction operator.

Parameters
rhsThe right hand side of the expression.
Returns
The sum of the left and right hand side.

◆ operator/()

template<typename DataType >
Array< DataType > TBTK::Array< DataType >::operator/ ( const DataType &  rhs) const
inline

Division operator.

Parameters
rhsThe right hand side of the expression.
Returns
The quotient between the left and right hand side.

◆ operator==()

template<typename DataType >
bool TBTK::Array< DataType >::operator== ( const Array< DataType > &  rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side of the expression.
Returns
True if the size and individual entries of the left and right hand sides are equal, otherwise false.

◆ operator[]() [1/4]

template<typename DataType >
DataType & TBTK::Array< DataType >::operator[] ( const std::vector< unsigned int > &  index)
inline

Array subscript operator.

Parameters
indexIndex to get the value for.
Returns
The value for the given index.

◆ operator[]() [2/4]

template<typename DataType >
const DataType & TBTK::Array< DataType >::operator[] ( const std::vector< unsigned int > &  index) const
inline

Array subscript operator.

Parameters
indexIndex to get the value for.
Returns
The value for the given index.

◆ operator[]() [3/4]

template<typename DataType >
DataType & TBTK::Array< DataType >::operator[] ( unsigned int  n)
inline

Array subscript operator. Imediate access for optimization. For an array with size {SIZE_A, SIZE_B, SIZE_C}, the supplied index should be calculated as n = SIZE_C*SIZE_B*a + SIZE_C*b + c.

Parameters
nEntry in the array.
Returns
The value of entry n.

◆ operator[]() [4/4]

template<typename DataType >
const DataType & TBTK::Array< DataType >::operator[] ( unsigned int  n) const
inline

Array subscript operator. Imediate access for optimization. For an array with size {SIZE_A, SIZE_B, SIZE_C}, the supplied index should be calculated as n = SIZE_C*SIZE_B*a + SIZE_C*b + c.

Parameters
nEntry in the array.
Returns
The value of entry n.

◆ serialize()

template<typename DataType >
std::string TBTK::Array< DataType >::serialize ( Mode  mode) const
inlinevirtual

Friends And Related Function Documentation

◆ Math::ArrayAlgorithms< DataType >

template<typename DataType >
friend class Math::ArrayAlgorithms< DataType >
friend

Friend class.

◆ operator*

template<typename DataType >
Array operator* ( const DataType &  lhs,
const Array< DataType > &  rhs 
)
friend

Multiplication operator.

Parameters
lhsThe left hand side of the expression.
rhsThe right hand side of the expression.
Returns
The product of the left and right hand side.

◆ operator<<

template<typename DataType >
template<typename DT >
std::ostream& operator<< ( std::ostream &  stream,
const Array< DT > &  array 
)
friend

ostream operator.


The documentation for this class was generated from the following file:
TBTK::Array
Multi-dimensional array.
Definition: Array.h:98
Streams.h
Streams for TBTK output.
Array.h
Multi-dimensional array.
TBTK::Initialize
void Initialize()
TBTK::Array::getRanges
const std::vector< unsigned int > & getRanges() const
Definition: Array.h:1228
TBTK::Streams::out
static std::ostream out
Definition: Streams.h:70
TBTK::Array::contract
static Array contract(const Array &array0, const std::vector< Subindex > &pattern0, const Array &array1, const std::vector< Subindex > &pattern1)
Definition: Array.h:810
TBTK::Array::getSlice
Array< DataType > getSlice(const std::vector< Subindex > &index) const
Definition: Array.h:1072
TBTK.h
TBTK initialization.
TBTK::Array::Array
Array()
Definition: Array.h:444