SpECTRE  2021.08.02
Tensor Expressions

Namespaces

namespace  TensorExpressions
 

Classes

struct  TensorExpressions::TensorContract< FirstContractedIndexPos, SecondContractedIndexPos, T, X, Symm, IndexList, ArgsList >
 
struct  TensorExpressions::LhsTensorSymmAndIndices< RhsTensorIndexList, LhsTensorIndexList, RhsSymmetry, RhsTensorIndexTypeList, NumIndices, IndexSequence >
 Determines and stores a LHS tensor's symmetry and index list from a RHS tensor expression and desired LHS index order. More...
 
struct  TensorExpressions::NumberAsExpression
 Defines an expression representing a double More...
 
struct  TensorExpressions::OuterProduct< T1, T2, IndexList1, IndexList2, ArgsList1, ArgsList2 >
 Defines the tensor expression representing the outer product of two tensor expressions. More...
 
struct  TensorExpressions::OuterProduct< T1, T2, IndexList1< Indices1... >, IndexList2< Indices2... >, ArgsList1< Args1... >, ArgsList2< Args2... > >::GetOpTensorMultiIndex< OperandTensorIndexList >
 Helper struct for computing the multi-index of a component of an operand of the outer product from the multi-index of a component of the outer product. More...
 
struct  TensorExpressions::SquareRoot< T >
 Defines the tensor expression representing the square root of a tensor expression that evaluates to a rank 0 tensor. More...
 
struct  TensorExpressions::TensorAsExpression< T, ArgsList >
 Defines an expression representing a Tensor. More...
 
struct  Expression
 Marks a class as being a TensorExpression. More...
 
struct  TensorExpression< Derived, DataType, Symm, tmpl::list< Indices... >, ArgsList< Args... > >
 The base class all tensor expression implementations derive from. More...
 
struct  TensorIndex< I, >
 Represents the geeric indices in a TensorExpression. More...
 
struct  tt::is_tensor_index< T >
 Check if a type T is a TensorIndex used in TensorExpressions. More...
 

Typedefs

template<auto &... TensorIndices>
using make_tensorindex_list = typename TensorIndex_detail::make_tensorindex_list_impl< TensorIndices... >::type
 Creates a TensorIndex type list from a list of TensorIndex objects. More...
 

Functions

template<typename T1 , typename T2 , typename X1 , typename X2 , typename Symm1 , typename Symm2 , typename IndexList1 , typename IndexList2 , typename Args1 , typename Args2 >
auto operator+ (const TensorExpression< T1, X1, Symm1, IndexList1, Args1 > &t1, const TensorExpression< T2, X2, Symm2, IndexList2, Args2 > &t2)
 
template<typename T1 , typename T2 , typename X1 , typename X2 , typename Symm1 , typename Symm2 , typename IndexList1 , typename IndexList2 , typename Args1 , typename Args2 >
auto operator- (const TensorExpression< T1, X1, Symm1, IndexList1, Args1 > &t1, const TensorExpression< T2, X2, Symm2, IndexList2, Args2 > &t2)
 
template<auto &... LhsTensorIndices, typename X , typename LhsSymmetry , typename LhsIndexList , typename Derived , typename RhsSymmetry , typename RhsIndexList , typename... RhsTensorIndices>
void TensorExpressions::evaluate (const gsl::not_null< Tensor< X, LhsSymmetry, LhsIndexList > * > lhs_tensor, const TensorExpression< Derived, X, RhsSymmetry, RhsIndexList, tmpl::list< RhsTensorIndices... > > &rhs_tensorexpression)
 Evaluate a RHS tensor expression to a tensor with the LHS index order set in the template parameters. More...
 
template<auto &... LhsTensorIndices, typename RhsTE , Requires< std::is_base_of_v< Expression, RhsTE > > = nullptr>
auto TensorExpressions::evaluate (const RhsTE &rhs_tensorexpression)
 Evaluate a RHS tensor expression to a tensor with the LHS index order set in the template parameters. More...
 
template<typename... LhsTensorIndices>
static constexpr std::array< size_t, sizeof...(OperandTensorIndices)> TensorExpressions::OuterProduct< T1, T2, IndexList1< Indices1... >, IndexList2< Indices2... >, ArgsList1< Args1... >, ArgsList2< Args2... > >::GetOpTensorMultiIndex< tmpl::list< OperandTensorIndices... > >::apply (const std::array< size_t, num_tensor_indices > &lhs_tensor_multi_index)
 Computes the multi-index of a component of an operand of the outer product from the multi-index of a component of the outer product. More...
 
template<typename T1 , typename T2 , typename ArgsList1 , typename ArgsList2 >
auto operator* (const TensorExpression< T1, typename T1::type, typename T1::symmetry, typename T1::index_list, ArgsList1 > &t1, const TensorExpression< T2, typename T2::type, typename T2::symmetry, typename T2::index_list, ArgsList2 > &t2)
 Returns the tensor expression representing the product of two tensor expressions. More...
 
template<typename T , typename X , typename ArgsList >
auto operator/ (const TensorExpression< T, X, typename T::symmetry, typename T::index_list, ArgsList > &t, const double number)
 Returns the tensor expression representing the quotient of a tensor expression and a double More...
 
template<typename T >
auto sqrt (const TensorExpression< T, typename T::type, tmpl::list<>, tmpl::list<>, tmpl::list<> > &t)
 Returns the tensor expression representing the square root of a tensor expression that evaluates to a rank 0 tensor. More...
 
template<typename T , typename X >
auto operator+ (const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &t, const double number)
 Returns the tensor expression representing the sum of a tensor expression and a double More...
 
template<typename T , typename X >
auto operator+ (const double number, const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &t)
 Returns the tensor expression representing the sum of a tensor expression and a double More...
 
template<typename T , typename X >
auto operator- (const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &t, const double number)
 Returns the tensor expression representing the difference of a tensor expression and a double More...
 
template<typename T , typename X >
auto operator- (const double number, const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &t)
 Returns the tensor expression representing the difference of a tensor expression and a double More...
 
template<typename T , typename X , typename ArgsList >
auto operator* (const TensorExpression< T, X, typename T::symmetry, typename T::index_list, ArgsList > &t, const double number)
 Returns the tensor expression representing the product of a tensor expression and a double More...
 
template<typename T , typename X , typename ArgsList >
auto operator* (const double number, const TensorExpression< T, X, typename T::symmetry, typename T::index_list, ArgsList > &t)
 Returns the tensor expression representing the product of a tensor expression and a double More...
 

Detailed Description

Tensor Expressions allow writing expressions of tensors in a way similar to what is used with pen and paper.

Tensor expressions are implemented using (smart) expression templates. This allows a domain specific language making expressions such as

auto T = evaluate<Indices::_a_t, Indices::_b_t>(F(Indices::_b,
Indices::_a));

possible.

Typedef Documentation

◆ make_tensorindex_list

template<auto &... TensorIndices>
using make_tensorindex_list = typename TensorIndex_detail::make_tensorindex_list_impl< TensorIndices...>::type

Creates a TensorIndex type list from a list of TensorIndex objects.

Template Parameters
TensorIndiceslist of generic index objects, e.g. ti_a, ti_b

Function Documentation

◆ apply()

template<typename T1 , typename T2 , template< typename... > class IndexList1, typename... Indices1, template< typename... > class IndexList2, typename... Indices2, template< typename... > class ArgsList1, typename... Args1, template< typename... > class ArgsList2, typename... Args2>
template<typename... OperandTensorIndices>
template<typename... LhsTensorIndices>
static constexpr std::array< size_t, sizeof...(OperandTensorIndices)> TensorExpressions::OuterProduct< T1, T2, IndexList1< Indices1... >, IndexList2< Indices2... >, ArgsList1< Args1... >, ArgsList2< Args2... > >::GetOpTensorMultiIndex< tmpl::list< OperandTensorIndices... > >::apply ( const std::array< size_t, num_tensor_indices > &  lhs_tensor_multi_index)
inlinestaticconstexpr

Computes the multi-index of a component of an operand of the outer product from the multi-index of a component of the outer product.

Details

Example: Let's say we are evaluating \(L_abc = R_{b} * S_{ca}\). Let ti_a_t denote the type of ti_a, and apply the same convention for other generic indices. Let LhsTensorIndices == ti_a_t, ti_b_t, ti_c_t, and OperandTensorIndices is either ti_b_t or ti_c_t, ti_a_t. Let lhs_tensor_multi_index == [0, 1, 2], representing the multi-index of the component \(L_{012}\). If OperandTensorIndices == ti_c_t, ti_a_t, this function will return the tensor multi-index representing the component \(S_{20}\), which is [2, 0].

Template Parameters
LhsTensorIndicesthe TensorIndexs of the outer product tensor
Parameters
lhs_tensor_multi_indexthe tensor multi-index of a component in the outer product tensor

Returns: the tensor multi-index of an operand of the outer product

◆ evaluate() [1/2]

template<auto &... LhsTensorIndices, typename X , typename LhsSymmetry , typename LhsIndexList , typename Derived , typename RhsSymmetry , typename RhsIndexList , typename... RhsTensorIndices>
void TensorExpressions::evaluate ( const gsl::not_null< Tensor< X, LhsSymmetry, LhsIndexList > * >  lhs_tensor,
const TensorExpression< Derived, X, RhsSymmetry, RhsIndexList, tmpl::list< RhsTensorIndices... > > &  rhs_tensorexpression 
)

Evaluate a RHS tensor expression to a tensor with the LHS index order set in the template parameters.

Details

Uses the right hand side (RHS) TensorExpression's index ordering (RhsTE::args_list) and the desired left hand side (LHS) tensor's index ordering (LhsTensorIndices) to fill the provided LHS Tensor with that LHS index ordering. This can carry out the evaluation of a RHS tensor expression to a LHS tensor with the same index ordering, such as \(L_{ab} = R_{ab}\), or different ordering, such as \(L_{ba} = R_{ab}\).

The symmetry of the provided LHS Tensor need not match the symmetry determined from evaluating the RHS TensorExpression according to its order of operations. This allows one to specify LHS symmetries (via lhs_tensor) that may not be preserved by the RHS expression's order of operations, which depends on how the expression is written and implemented.

Example usage

Given two rank 2 Tensors R and S with index order (a, b), add them together and fill the provided resultant LHS Tensor L with index order (b, a):

TensorExpressions::evaluate<ti_b, ti_a>(
make_not_null(&L), R(ti_a, ti_b) + S(ti_a, ti_b));

This represents evaluating: \(L_{ba} = R_{ab} + S_{ab}\)

Note: LhsTensorIndices must be passed by reference because non-type template parameters cannot be class types until C++20.

Template Parameters
LhsTensorIndicesthe TensorIndexs of the Tensor on the LHS of the tensor expression, e.g. ti_a, ti_b, ti_c
Parameters
lhs_tensorpointer to the resultant LHS Tensor to fill
rhs_tensorexpressionthe RHS TensorExpression to be evaluated

◆ evaluate() [2/2]

template<auto &... LhsTensorIndices, typename RhsTE , Requires< std::is_base_of_v< Expression, RhsTE > > = nullptr>
auto TensorExpressions::evaluate ( const RhsTE &  rhs_tensorexpression)

Evaluate a RHS tensor expression to a tensor with the LHS index order set in the template parameters.

Details

Uses the right hand side (RHS) TensorExpression's index ordering (RhsTE::args_list) and the desired left hand side (LHS) tensor's index ordering (LhsTensorIndices) to construct a LHS Tensor with that LHS index ordering. This can carry out the evaluation of a RHS tensor expression to a LHS tensor with the same index ordering, such as \(L_{ab} = R_{ab}\), or different ordering, such as \(L_{ba} = R_{ab}\).

The symmetry of the returned LHS Tensor depends on the order of operations in the RHS TensorExpression, i.e. how the expression is written. If you would like to specify the symmetry of the LHS Tensor instead of it being determined by the order of operations in the RHS expression, please use the other evaluate overload that takes an empty LHS Tensor as its first argument.

Example usage

Given two rank 2 Tensors R and S with index order (a, b), add them together and generate the resultant LHS Tensor L with index order (b, a):

auto L = TensorExpressions::evaluate<ti_b, ti_a>(
R(ti_a, ti_b) + S(ti_a, ti_b));

Returns: Tensor

This represents evaluating: \(L_{ba} = R_{ab} + S_{ab}\)

Note: If a generic spatial index is used for a spacetime index in the RHS tensor, its corresponding index in the LHS tensor type will be a spatial index with the same valence, frame, and number of spatial dimensions.

Note: LhsTensorIndices must be passed by reference because non-type template parameters cannot be class types until C++20.

Template Parameters
LhsTensorIndicesthe TensorIndexs of the Tensor on the LHS of the tensor expression, e.g. ti_a, ti_b, ti_c
Parameters
rhs_tensorexpressionthe RHS TensorExpression to be evaluated

Returns: the resultant LHS Tensor with index order specified by LhsTensorIndices

◆ operator*() [1/3]

template<typename T , typename X , typename ArgsList >
auto operator* ( const double  number,
const TensorExpression< T, X, typename T::symmetry, typename T::index_list, ArgsList > &  t 
)

Returns the tensor expression representing the product of a tensor expression and a double

Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the product
Xthe type of data stored in the tensor expression operand of the product
ArgsListthe TensorIndexs of the tensor expression operand of the product
Parameters
tthe tensor expression operand of the product
numberthe double operand of the product

Returns: the tensor expression representing the product of a tensor expression and a double

◆ operator*() [2/3]

template<typename T , typename X , typename ArgsList >
auto operator* ( const TensorExpression< T, X, typename T::symmetry, typename T::index_list, ArgsList > &  t,
const double  number 
)

Returns the tensor expression representing the product of a tensor expression and a double

Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the product
Xthe type of data stored in the tensor expression operand of the product
ArgsListthe TensorIndexs of the tensor expression operand of the product
Parameters
tthe tensor expression operand of the product
numberthe double operand of the product

Returns: the tensor expression representing the product of a tensor expression and a double

◆ operator*() [3/3]

template<typename T1 , typename T2 , typename ArgsList1 , typename ArgsList2 >
auto operator* ( const TensorExpression< T1, typename T1::type, typename T1::symmetry, typename T1::index_list, ArgsList1 > &  t1,
const TensorExpression< T2, typename T2::type, typename T2::symmetry, typename T2::index_list, ArgsList2 > &  t2 
)

Returns the tensor expression representing the product of two tensor expressions.

Details

If the two operands have N pairs of indices that need to be contracted, the returned expression will be an OuterProduct expression nested inside N TensorContract expressions. This represents computing the inner product of the outer product of the two operands. If the operands do not have any indices to be contracted, the returned expression will be an OuterProduct.

The two arguments are expressions that contain the two operands of the product, where the types of the operands are T1 and T2.

Template Parameters
T1the derived TensorExpression type of the first operand of the product
T2the derived TensorExpression type of the second operand of the product
ArgsList1the TensorIndexs of the first operand
ArgsList2the TensorIndexs of the second operand
Parameters
t1first operand expression of the product
t2the second operand expression of the product

Returns: the tensor expression representing the product of two tensor expressions

◆ operator+() [1/2]

template<typename T , typename X >
auto operator+ ( const double  number,
const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &  t 
)

Returns the tensor expression representing the sum of a tensor expression and a double

Details

The tensor expression operand must represent an expression that, when evaluated, would be a rank 0 tensor. For example, if R and S are Tensors, here is a non-exhaustive list of some of the acceptable forms that the tensor expression operand could take:

  • R()
  • R(ti_A, ti_a)
  • (R(ti_A, ti_B) * S(ti_a, ti_b))
Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the sum
Xthe type of data stored in the tensor expression operand of the sum
Parameters
tthe tensor expression operand of the sum
numberthe double operand of the sum

Returns: the tensor expression representing the sum of a tensor expression and a double

◆ operator+() [2/2]

template<typename T , typename X >
auto operator+ ( const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &  t,
const double  number 
)

Returns the tensor expression representing the sum of a tensor expression and a double

Details

The tensor expression operand must represent an expression that, when evaluated, would be a rank 0 tensor. For example, if R and S are Tensors, here is a non-exhaustive list of some of the acceptable forms that the tensor expression operand could take:

  • R()
  • R(ti_A, ti_a)
  • (R(ti_A, ti_B) * S(ti_a, ti_b))
Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the sum
Xthe type of data stored in the tensor expression operand of the sum
Parameters
tthe tensor expression operand of the sum
numberthe double operand of the sum

Returns: the tensor expression representing the sum of a tensor expression and a double

◆ operator-() [1/2]

template<typename T , typename X >
auto operator- ( const double  number,
const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &  t 
)

Returns the tensor expression representing the difference of a tensor expression and a double

Details

The tensor expression operand must represent an expression that, when evaluated, would be a rank 0 tensor. For example, if R and S are Tensors, here is a non-exhaustive list of some of the acceptable forms that the tensor expression operand could take:

  • R()
  • R(ti_A, ti_a)
  • (R(ti_A, ti_B) * S(ti_a, ti_b))
Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the difference
Xthe type of data stored in the tensor expression operand of the difference
Parameters
tthe tensor expression operand of the difference
numberthe double operand of the difference

Returns: the tensor expression representing the difference of a tensor expression and a double

◆ operator-() [2/2]

template<typename T , typename X >
auto operator- ( const TensorExpression< T, X, tmpl::list<>, tmpl::list<>, tmpl::list<> > &  t,
const double  number 
)

Returns the tensor expression representing the difference of a tensor expression and a double

Details

The tensor expression operand must represent an expression that, when evaluated, would be a rank 0 tensor. For example, if R and S are Tensors, here is a non-exhaustive list of some of the acceptable forms that the tensor expression operand could take:

  • R()
  • R(ti_A, ti_a)
  • (R(ti_A, ti_B) * S(ti_a, ti_b))
Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the difference
Xthe type of data stored in the tensor expression operand of the difference
Parameters
tthe tensor expression operand of the difference
numberthe double operand of the difference

Returns: the tensor expression representing the difference of a tensor expression and a double

◆ operator/()

template<typename T , typename X , typename ArgsList >
auto operator/ ( const TensorExpression< T, X, typename T::symmetry, typename T::index_list, ArgsList > &  t,
const double  number 
)

Returns the tensor expression representing the quotient of a tensor expression and a double

Note
The implementation instead uses the operation, t * (1.0 / number)
Template Parameters
Tthe derived TensorExpression type of the tensor expression operand of the quotient
Xthe type of data stored in the tensor expression operand of the quotient
ArgsListthe TensorIndexs of the tensor expression operand of the quotient
Parameters
tthe tensor expression operand of the quotient
numberthe double operand of the quotient

Returns: the tensor expression representing the quotient of a tensor expression and a double

◆ sqrt()

template<typename T >
auto sqrt ( const TensorExpression< T, typename T::type, tmpl::list<>, tmpl::list<>, tmpl::list<> > &  t)

Returns the tensor expression representing the square root of a tensor expression that evaluates to a rank 0 tensor.

Details

t must be an expression that, when evaluated, would be a rank 0 tensor. For example, if R and S are Tensors, here is a non-exhaustive list of some of the acceptable forms that t could take:

  • R()
  • R(ti_A, ti_a)
  • (R(ti_A, ti_B) * S(ti_a, ti_b))
Parameters
tthe type of the tensor expression of which to take the square root