template<class Implementation, typename S, std::size_t D, std::size_t R, typename I = Index<D>>
symtensor::SymmetricTensorBase class

Symmetric tensor base-type for use with CRTP implementations.

Template parameters
Implementation CRTP subclass
S scalar type
D number of dimensions (2d, 3d, etc.)
R rank
I index type, defaults to the appropriate Index

In order to create a new type which behaves like a symmetric tensor, you can derive from this class so that operators will function correctly.

Constructors

SymmetricTensorBase() explicit constexpr
Default constructor.
SymmetricTensorBase(auto ... s) explicit constexpr
Constructor from a sequence of values.
SymmetricTensorBase(const std::array<S, NumUniqueValues>& values) explicit constexpr
Constructor from an std::array of scalars.
SymmetricTensorBase(std::initializer_list<Scalar> scalars) constexpr
(Implicit) constructor from a sequence of values.
static auto Identity() -> Implementation consteval
Identity matrix constructor.
static auto Ones() -> Implementation consteval
Unit matrix constructor.
static auto Zeros() -> Implementation consteval
Zero matrix constructor.
template<typename F>
static auto NullaryExpression(F function = {}) -> ALWAYS_INLINE Implementation constexpr
Generic constructor from an expression.
template<typename Vector>
static auto CartesianPower(const Vector& vector) -> Implementation constexpr
Constructor by repeated cartesian product.
template<indexable Vector>
static auto Diagonal(const Vector& vector) -> Implementation constexpr
Constructor with a set vector as the diagonal.
template<typename T>
static auto ConstantDiagonal(const T& value) -> Implementation constexpr
Constructor with a set constant value along the diagonal.

Member access

auto operator[](const std::array<Index, R>& indices) const -> const Scalar& constexpr
Indexed member access.
auto operator[](const std::array<Index, R>& indices) -> Scalar& constexpr
Indexed member access.
template<Index... Indices>
auto at() const -> const Scalar& constexpr
Compile-time indexed member access.
template<Index... Indices>
auto at() -> Scalar& constexpr
Compile-time indexed member access.
auto flat() const -> const auto& constexpr
Direct access to underlying data structure.
auto flat() -> auto& constexpr
Direct access to underlying data structure.

Tensor properties

auto trace() const -> Scalar constexpr
Computes the trace of the tensor.
auto diagonal() const -> auto constexpr
Produces the diagonal of the tensor.
auto norm2() const -> auto constexpr
auto norm() const -> auto constexpr

Tensor-scalar operators

auto operator+=(const Scalar& scalar) -> Implementation& constexpr
Element-wise addition of a scalar.
auto operator-=(const Scalar& scalar) -> Implementation& constexpr
Element-wise subtraction of a scalar.
auto operator*=(const Scalar& scalar) -> Implementation& constexpr
Element-wise multiplication by a scalar.
auto operator/=(const Scalar& scalar) -> Implementation& constexpr
Element-wise division by a scalar.
auto operator+(const Scalar& scalar) const -> Implementation constexpr
Element-wise addition of a scalar.
auto operator-(const Scalar& scalar) const -> Implementation constexpr
Element-wise subtraction of a scalar.
auto operator*(const Scalar& scalar) const -> Implementation constexpr
Element-wise multiplication by a scalar.
auto operator/(const Scalar& scalar) const -> Implementation constexpr
Element-wise division by a scalar.
auto operator*(const Scalar& scalar, const Self& tensor) -> Implementation constexpr
Element-wise multiplication by a scalar.

Tensor-tensor element-wise operations

auto operator+=(const Implementation& other) -> Implementation& constexpr
Element-wise addition with another tensor.
auto operator-=(const Implementation& other) -> Implementation& constexpr
Element-wise subtraction by another tensor.
auto operator+(const Implementation& other) const -> Implementation constexpr
Element-wise addition with another tensor.
auto operator-(const Implementation& other) const -> Implementation constexpr
Element-wise subtraction by another tensor.

Comparison operations

auto operator==(const Self& other) const -> bool defaulted constexpr
Comparison with another symmetric tensor.
auto operator<=(const Implementation& lhs, const Implementation& rhs) -> auto
Inequality comparison between symmetric tensors.
auto operator<(const Implementation& lhs, const Implementation& rhs) -> auto
Inequality comparison between symmetric tensors.
auto operator>=(const Implementation& lhs, const Implementation& rhs) -> auto
Inequality comparison between symmetric tensors.
auto operator>(const Implementation& lhs, const Implementation& rhs) -> auto
Inequality comparison between symmetric tensors.

Function documentation

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::SymmetricTensorBase() explicit constexpr

Default constructor.

Initializes all indices to 0.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::SymmetricTensorBase(auto ... s) explicit constexpr

Constructor from a sequence of values.

Parameters
s a sequence of scalar values to initialize the tensor.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
static Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::Identity() consteval

Identity matrix constructor.

Returns a symmetric tensor with a value of 1 along the diagonal, 0 elsewhere.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
static Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::Ones() consteval

Unit matrix constructor.

Returns a symmetric tensor with a value of 1 at every index.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
static Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::Zeros() consteval

Zero matrix constructor.

Returns a symmetric tensor with a value of 0 at every index.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I> template<typename F>
static ALWAYS_INLINE Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::NullaryExpression(F function = {}) constexpr

Generic constructor from an expression.

Parameters
function a functor which returns a scalar type given an index
Returns a symmetric tensor with each value set by evaluating the function.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I> template<typename Vector>
static Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::CartesianPower(const Vector& vector) constexpr

Constructor by repeated cartesian product.

Template parameters
Vector vector-like type which provides a subscript operator, with size D
Parameters
vector vector which will be multiplied with itself
Returns a symmetric tensor produced by the repeated outer product of vector.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I> template<indexable Vector>
static Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::Diagonal(const Vector& vector) constexpr

Constructor with a set vector as the diagonal.

Template parameters
Vector vector-like type which provides a subscript operator, with size D
Parameters
vector vector which will define the diagonal of the tensor
Returns a symmetric tensor with diagonal equal to vector, zero elsewhere.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I> template<typename T>
static Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::ConstantDiagonal(const T& value) constexpr

Constructor with a set constant value along the diagonal.

Template parameters
T scalar-like type
Parameters
value the value to be placed along the diagonal of the tensor
Returns a symmetric tensor with elements of the diagonal diagonal equal to value, zero elsewhere.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
const Scalar& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator[](const std::array<Index, R>& indices) const constexpr

Indexed member access.

Parameters
indices Array of R Index values which specify an element of the tensor. Indices may be in arbitrary order, because the tensor is symmetric.
Returns the scalar element at the requested index

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Scalar& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator[](const std::array<Index, R>& indices) constexpr

Indexed member access.

Parameters
indices Array of R Index values which specify an element of the tensor. Indices may be in arbitrary order, because the tensor is symmetric.
Returns the scalar element at the requested index

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I> template<Index... Indices>
const Scalar& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::at() const constexpr

Compile-time indexed member access.

Template parameters
Indices Sequence of R Index values which specify an element of the tensor. Indices may be in arbitrary order, because the tensor is symmetric.
Returns the scalar element at the requested index

The index sequence is converted to a flat index at compile-time, allowing for performance equivalent to hand-written member access. mytensor.at<X, X, X>() should be equivalent to mytensor.flat()[0].

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I> template<Index... Indices>
Scalar& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::at() constexpr

Compile-time indexed member access.

Template parameters
Indices Sequence of R Index values which specify an element of the tensor. Indices may be in arbitrary order, because the tensor is symmetric.
Returns the scalar element at the requested index

The index sequence is converted to a flat index at compile-time, allowing for performance equivalent to hand-written member access. mytensor.at<X, X, X>() should be equivalent to mytensor.flat()[0].

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
const auto& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::flat() const constexpr

Direct access to underlying data structure.

Returns a reference to the underlying flat storage.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::flat() constexpr

Direct access to underlying data structure.

Returns a reference to the underlying flat storage.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Scalar symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::trace() const constexpr

Computes the trace of the tensor.

Returns the sum of the scalar values at the diagonal positions of the tensor

This is equivalent to the sum of the diagonal elements.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::diagonal() const constexpr

Produces the diagonal of the tensor.

Returns A vector containing the elements along the diagonal of the tensor.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::norm2() const constexpr

Returns Norm2 of the tensor, with the same type as the tensor's elements.

The squared Frobenius norm of the tensor.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::norm() const constexpr

Returns Norm of the tensor, with the same type as the tensor's elements.

The Frobenius norm of the tensor.

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator+=(const Scalar& scalar) constexpr

Element-wise addition of a scalar.

Parameters
scalar value to add to each element of the tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator-=(const Scalar& scalar) constexpr

Element-wise subtraction of a scalar.

Parameters
scalar value to subtract from each element of the tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator*=(const Scalar& scalar) constexpr

Element-wise multiplication by a scalar.

Parameters
scalar value to multiply each element of the tensor by.
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator/=(const Scalar& scalar) constexpr

Element-wise division by a scalar.

Parameters
scalar value to divide each element of the tensor by.
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator+(const Scalar& scalar) const constexpr

Element-wise addition of a scalar.

Parameters
scalar value to add to each element of the tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator-(const Scalar& scalar) const constexpr

Element-wise subtraction of a scalar.

Parameters
scalar value to subtract from each element of the tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator*(const Scalar& scalar) const constexpr

Element-wise multiplication by a scalar.

Parameters
scalar value to multiply each element of the tensor by.
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator/(const Scalar& scalar) const constexpr

Element-wise division by a scalar.

Parameters
scalar value to divide each element of the tensor by.
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator*(const Scalar& scalar, const Self& tensor) constexpr

Element-wise multiplication by a scalar.

Parameters
scalar value to multiply each element of the tensor by.
tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator+=(const Implementation& other) constexpr

Element-wise addition with another tensor.

Parameters
other tensor to add with this tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation& symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator-=(const Implementation& other) constexpr

Element-wise subtraction by another tensor.

Parameters
other tensor to subtract from this tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator+(const Implementation& other) const constexpr

Element-wise addition with another tensor.

Parameters
other tensor to add with this tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
Implementation symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator-(const Implementation& other) const constexpr

Element-wise subtraction by another tensor.

Parameters
other tensor to subtract from this tensor
Returns the modified tensor

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
bool symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator==(const Self& other) const defaulted constexpr

Comparison with another symmetric tensor.

Parameters
other symmetric tensor to compare with
Returns true if all elements of the tensors are equivalent, false otherwise

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator<=(const Implementation& lhs, const Implementation& rhs)

Inequality comparison between symmetric tensors.

Parameters
lhs tensor on the left hand side
rhs tensor on the right hand side
Returns true if all elements of lhs are less than or equal to their corresponding elements in rhs

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator<(const Implementation& lhs, const Implementation& rhs)

Inequality comparison between symmetric tensors.

Parameters
lhs tensor on the left hand side
rhs tensor on the right hand side
Returns true if all elements of lhs are less than their corresponding elements in rhs

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator>=(const Implementation& lhs, const Implementation& rhs)

Inequality comparison between symmetric tensors.

Parameters
lhs tensor on the left hand side
rhs tensor on the right hand side
Returns true if all elements of lhs are greater than or equal to their corresponding elements in rhs

template<class Implementation, typename S, std::size_t D, std::size_t R, typename I>
auto symtensor::SymmetricTensorBase<Implementation, S, D, R, I>::operator>(const Implementation& lhs, const Implementation& rhs)

Inequality comparison between symmetric tensors.

Parameters
lhs tensor on the left hand side
rhs tensor on the right hand side
Returns true if all elements of lhs are greater their corresponding elements in rhs