Cadabra
Computer algebra system for field theory problems
Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
NTensor Class Reference

Description

Class to hold numerical values structured in tensor form, that is, a multi-dimensional array.

#include <NTensor.hh>

Public Member Functions

 NTensor (const std::vector< size_t > &shape, double val)
 Initialise by giving the dimension for each index. More...
 
 NTensor (const std::vector< double > &vals)
 Initialise as a vector of doubles; sets shape automatically. More...
 
 NTensor (double)
 Initialise as a scalar; sets shape automatically. More...
 
 NTensor (const NTensor &)
 Copy constructor. More...
 
NTensoroperator= (const NTensor &)
 Assignment operator. More...
 
NTensoroperator+= (const NTensor &)
 Addition operator. This requires the shapes to match. More...
 
NTensoroperator*= (const NTensor &)
 Element-wise multiplication operator. This requires the shapes to match. More...
 
double at (const std::vector< size_t > &indices) const
 Get the value of the tensor at the indicated component. More...
 
double & at (const std::vector< size_t > &indices)
 Get the value of the tensor at the indicated component. More...
 
NTensor broadcast (std::vector< size_t > new_shape, size_t pos) const
 Expand the shape of the tensor to the specified shape by broadcasting to the other dimensions. More...
 
NTensorapply (double(*fun)(double))
 Apply a scalar function fun to all elements, return a reference to itself. More...
 

Static Public Member Functions

static NTensor linspace (double from, double to, size_t steps)
 Create equally spaced values in a range. More...
 
static NTensor outer_product (const NTensor &a, const NTensor &b)
 Outer product of two NTensors. More...
 

Public Attributes

std::vector< size_t > shape
 
std::vector< double > values
 

Friends

std::ostream & operator<< (std::ostream &, const NTensor &)
 

Constructor & Destructor Documentation

◆ NTensor() [1/4]

NTensor::NTensor ( const std::vector< size_t > &  shape,
double  val 
)

Initialise by giving the dimension for each index.

Storage is generalised row-major. Dispay follows that convention: we use maths matrix conventions for printing, that is, earlier indices are more major, and are iterated over in a more outer loop.

◆ NTensor() [2/4]

NTensor::NTensor ( const std::vector< double > &  vals)

Initialise as a vector of doubles; sets shape automatically.

◆ NTensor() [3/4]

NTensor::NTensor ( double  val)

Initialise as a scalar; sets shape automatically.

◆ NTensor() [4/4]

NTensor::NTensor ( const NTensor other)

Copy constructor.

Member Function Documentation

◆ apply()

NTensor & NTensor::apply ( double(*)(double)  fun)

Apply a scalar function fun to all elements, return a reference to itself.

◆ at() [1/2]

double & NTensor::at ( const std::vector< size_t > &  indices)

Get the value of the tensor at the indicated component.

◆ at() [2/2]

double NTensor::at ( const std::vector< size_t > &  indices) const

Get the value of the tensor at the indicated component.

◆ broadcast()

NTensor NTensor::broadcast ( std::vector< size_t >  new_shape,
size_t  pos 
) const

Expand the shape of the tensor to the specified shape by broadcasting to the other dimensions.

Effectively,

A_{i} -> A_{k i l}

shape {2} tensor [3,4] to shape {4,2} pos 1:

-> [[3,4], [3,4]]

For now only works if the original shape is one-dimensional, that is, a vector (as above).

◆ linspace()

NTensor NTensor::linspace ( double  from,
double  to,
size_t  steps 
)
static

Create equally spaced values in a range.

◆ operator*=()

NTensor & NTensor::operator*= ( const NTensor other)

Element-wise multiplication operator. This requires the shapes to match.

◆ operator+=()

NTensor & NTensor::operator+= ( const NTensor other)

Addition operator. This requires the shapes to match.

◆ operator=()

NTensor & NTensor::operator= ( const NTensor other)

Assignment operator.

◆ outer_product()

NTensor NTensor::outer_product ( const NTensor a,
const NTensor b 
)
static

Outer product of two NTensors.

The shape becomes the concatenation of the two shapes, with the shape of a coming first.

a b { 3 } x { 4 } -> { 3, 4}.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  ,
const NTensor  
)
friend

Member Data Documentation

◆ shape

std::vector<size_t> NTensor::shape

◆ values

std::vector<double> NTensor::values

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