#include <deal.II/base/tensor.h>
Public Types | |
using | value_type |
using | array_type |
using | tensor_type = Tensor<rank_, dim, Number> |
Public Member Functions | |
constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE | Tensor () |
constexpr | Tensor (const array_type &initializer) |
template<typename ElementType, typename MemorySpace> | |
constexpr | Tensor (const ArrayView< ElementType, MemorySpace > &initializer) |
template<typename OtherNumber> | |
constexpr | Tensor (const Tensor< rank_, dim, OtherNumber > &initializer) |
template<typename OtherNumber> | |
constexpr | Tensor (const Tensor< 1, dim, Tensor< rank_ - 1, dim, OtherNumber > > &initializer) |
template<typename OtherNumber> | |
constexpr | operator Tensor< 1, dim, Tensor< rank_ - 1, dim, OtherNumber > > () const |
constexpr value_type & | operator[] (const unsigned int i) |
constexpr const value_type & | operator[] (const unsigned int i) const |
constexpr const Number & | operator[] (const TableIndices< rank_ > &indices) const |
constexpr Number & | operator[] (const TableIndices< rank_ > &indices) |
Number * | begin_raw () |
const Number * | begin_raw () const |
Number * | end_raw () |
const Number * | end_raw () const |
template<typename OtherNumber> | |
constexpr Tensor & | operator= (const Tensor< rank_, dim, OtherNumber > &rhs) |
constexpr Tensor & | operator= (const Number &d) & |
constexpr Tensor & | operator= (const Number &d) &&=delete |
template<typename OtherNumber> | |
constexpr bool | operator== (const Tensor< rank_, dim, OtherNumber > &) const |
template<typename OtherNumber> | |
constexpr bool | operator!= (const Tensor< rank_, dim, OtherNumber > &) const |
template<typename OtherNumber> | |
constexpr Tensor & | operator+= (const Tensor< rank_, dim, OtherNumber > &) |
template<typename OtherNumber> | |
constexpr Tensor & | operator-= (const Tensor< rank_, dim, OtherNumber > &) |
template<typename OtherNumber> | |
constexpr Tensor & | operator*= (const OtherNumber &factor) |
template<typename OtherNumber> | |
constexpr Tensor & | operator/= (const OtherNumber &factor) |
constexpr Tensor | operator- () const |
constexpr void | clear () |
numbers::NumberTraits< Number >::real_type | norm () const |
constexpr numbers::NumberTraits< Number >::real_type | norm_square () const |
template<typename OtherNumber> | |
void | unroll (Vector< OtherNumber > &result) const |
template<class Iterator> | |
void | unroll (const Iterator begin, const Iterator end) const |
template<class Archive> | |
void | serialize (Archive &ar, const unsigned int version) |
Static Public Member Functions | |
static constexpr unsigned int | component_to_unrolled_index (const TableIndices< rank_ > &indices) |
static constexpr TableIndices< rank_ > | unrolled_to_component_indices (const unsigned int i) |
static constexpr std::size_t | memory_consumption () |
Static Public Attributes | |
static constexpr unsigned int | dimension = dim |
static constexpr unsigned int | rank = rank_ |
static constexpr unsigned int | n_independent_components |
Private Member Functions | |
template<typename ArrayLike, std::size_t... Indices> | |
constexpr | Tensor (const ArrayLike &initializer, std::index_sequence< Indices... >) |
Private Attributes | |
std::conditional_t< rank_==1, std::array< Number, dim >, std::array< Tensor< rank_ - 1, dim, Number >, dim > > | values |
Friends | |
template<int, int, typename> | |
class | Tensor |
class | Point< dim, Number > |
Related Symbols | |
(Note that these are not member symbols.) | |
template<int rank, int dim, typename Number> | |
Tensor< rank, dim, Number > | sum (const Tensor< rank, dim, Number > &local, const MPI_Comm mpi_communicator) |
Output functions for Tensor objects | |
template<int rank_, int dim, typename Number> | |
std::ostream & | operator<< (std::ostream &out, const Tensor< rank_, dim, Number > &p) |
template<int dim, typename Number> | |
std::ostream & | operator<< (std::ostream &out, const Tensor< 0, dim, Number > &p) |
Vector space operations on Tensor objects | |
template<int dim, typename Number, typename Other> | |
constexpr ProductType< Other, Number >::type | operator* (const Other &object, const Tensor< 0, dim, Number > &t) |
template<int dim, typename Number, typename Other> | |
constexpr ProductType< Number, Other >::type | operator* (const Tensor< 0, dim, Number > &t, const Other &object) |
template<int dim, typename Number, typename OtherNumber> | |
constexpr ProductType< Number, OtherNumber >::type | operator* (const Tensor< 0, dim, Number > &src1, const Tensor< 0, dim, OtherNumber > &src2) |
template<int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< 0, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > | operator/ (const Tensor< 0, dim, Number > &t, const OtherNumber &factor) |
template<int dim, typename Number, typename OtherNumber> | |
constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > | operator+ (const Tensor< 0, dim, Number > &p, const Tensor< 0, dim, OtherNumber > &q) |
template<int dim, typename Number, typename OtherNumber> | |
constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > | operator- (const Tensor< 0, dim, Number > &p, const Tensor< 0, dim, OtherNumber > &q) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > | operator* (const Tensor< rank, dim, Number > &t, const OtherNumber &factor) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank, dim, typename ProductType< typename EnableIfScalar< Number >::type, OtherNumber >::type > | operator* (const Number &factor, const Tensor< rank, dim, OtherNumber > &t) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank, dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > | operator/ (const Tensor< rank, dim, Number > &t, const OtherNumber &factor) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank, dim, typename ProductType< Number, OtherNumber >::type > | operator+ (const Tensor< rank, dim, Number > &p, const Tensor< rank, dim, OtherNumber > &q) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank, dim, typename ProductType< Number, OtherNumber >::type > | operator- (const Tensor< rank, dim, Number > &p, const Tensor< rank, dim, OtherNumber > &q) |
template<int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< 0, dim, typename ProductType< Number, OtherNumber >::type > | schur_product (const Tensor< 0, dim, Number > &src1, const Tensor< 0, dim, OtherNumber > &src2) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank, dim, typename ProductType< Number, OtherNumber >::type > | schur_product (const Tensor< rank, dim, Number > &src1, const Tensor< rank, dim, OtherNumber > &src2) |
Contraction operations and the outer product for tensor objects | |
template<int rank_1, int rank_2, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank_1+rank_2-2, dim, typenameProductType< Number, OtherNumber >::type >::tensor_type | operator* (const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2) |
template<int index_1, int index_2, int rank_1, int rank_2, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank_1+rank_2-2, dim, typenameProductType< Number, OtherNumber >::type >::tensor_type | contract (const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2) |
template<int index_1, int index_2, int index_3, int index_4, int rank_1, int rank_2, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank_1+rank_2-4, dim, typenameProductType< Number, OtherNumber >::type >::tensor_type | double_contract (const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2) |
template<int rank, int dim, typename Number, typename OtherNumber> | |
constexpr ProductType< Number, OtherNumber >::type | scalar_product (const Tensor< rank, dim, Number > &left, const Tensor< rank, dim, OtherNumber > &right) |
template<template< int, int, typename > class TensorT1, template< int, int, typename > class TensorT2, template< int, int, typename > class TensorT3, int rank_1, int rank_2, int dim, typename T1, typename T2, typename T3> | |
constexpr ProductType< T1, typenameProductType< T2, T3 >::type >::type | contract3 (const TensorT1< rank_1, dim, T1 > &left, const TensorT2< rank_1+rank_2, dim, T2 > &middle, const TensorT3< rank_2, dim, T3 > &right) |
template<int rank_1, int rank_2, int dim, typename Number, typename OtherNumber> | |
constexpr Tensor< rank_1+rank_2, dim, typename ProductType< Number, OtherNumber >::type > | outer_product (const Tensor< rank_1, dim, Number > &src1, const Tensor< rank_2, dim, OtherNumber > &src2) |
Special operations on tensors of rank 1 | |
template<int dim, typename Number> | |
constexpr Tensor< 1, dim, Number > | cross_product_2d (const Tensor< 1, dim, Number > &src) |
template<int dim, typename Number1, typename Number2> | |
constexpr Tensor< 1, dim, typename ProductType< Number1, Number2 >::type > | cross_product_3d (const Tensor< 1, dim, Number1 > &src1, const Tensor< 1, dim, Number2 > &src2) |
Special operations on tensors of rank 2 | |
template<int dim, typename Number> | |
constexpr Number | determinant (const Tensor< 2, dim, Number > &t) |
template<typename Number> | |
constexpr Number | determinant (const Tensor< 2, 1, Number > &t) |
template<typename Number> | |
constexpr Number | determinant (const Tensor< 2, 2, Number > &t) |
template<typename Number> | |
constexpr Number | determinant (const Tensor< 2, 3, Number > &t) |
template<int dim, typename Number> | |
constexpr Number | trace (const Tensor< 2, dim, Number > &d) |
template<int dim, typename Number> | |
constexpr Tensor< 2, dim, Number > | invert (const Tensor< 2, dim, Number > &) |
template<int dim, typename Number> | |
constexpr Tensor< 2, dim, Number > | transpose (const Tensor< 2, dim, Number > &t) |
template<int dim, typename Number> | |
constexpr Tensor< 2, dim, Number > | adjugate (const Tensor< 2, dim, Number > &t) |
template<int dim, typename Number> | |
constexpr Tensor< 2, dim, Number > | cofactor (const Tensor< 2, dim, Number > &t) |
template<int dim, typename Number> | |
Tensor< 2, dim, Number > | project_onto_orthogonal_tensors (const Tensor< 2, dim, Number > &A) |
template<int dim, typename Number> | |
Number | l1_norm (const Tensor< 2, dim, Number > &t) |
template<int dim, typename Number> | |
Number | linfty_norm (const Tensor< 2, dim, Number > &t) |
A general tensor class with an arbitrary rank, i.e. with an arbitrary number of indices. The Tensor class provides an indexing operator and a bit of infrastructure, but most functionality is recursively handed down to tensors of rank 1 or put into external templated functions, e.g. the contract
family.
The rank of a tensor specifies which types of physical quantities it can represent:
dim
components and it can represent vector quantities such as velocity, displacement, electric field, etc. They can also describe the gradient of a scalar field. The notation used for rank-1 tensors is bold-faced lower-case Latin letters e.g. dim-1
. rank
. For rank-4 tensors, a symmetric variant called SymmetricTensor<4,dim> exists. Using this tensor class for objects of rank 2 has advantages over matrices in many cases since the dimension is known to the compiler as well as the location of the data. It is therefore possible to produce far more efficient code than for matrices with runtime-dependent dimension. It also makes the code easier to read because of the semantic difference between a tensor (an object that relates to a coordinate system and has transformation properties with regard to coordinate rotations and transforms) and matrices (which we consider as operators on arbitrary vector spaces related to linear algebra things).
rank_ | An integer that denotes the rank of this tensor. A specialization of this class exists for rank-0 tensors. |
dim | An integer that denotes the dimension of the space in which this tensor operates. This of course equals the number of coordinates that identify a point and rank-1 tensor. |
Number | The data type in which the tensor elements are to be stored. This will, in almost all cases, simply be the default double , but there are cases where one may want to store elements in a different (and always scalar) type. It can be used to base tensors on float or complex numbers or any other data type that implements basic arithmetic operations. Another example would be a type that allows for Automatic Differentiation (see, for example, the Sacado type used in step-33) and thereby can generate analytic (spatial) derivatives of a function that takes a tensor as argument. |
Type of objects encapsulated by this container and returned by operator[](). This is a tensor of lower rank for a general tensor, and a scalar number type for rank-1 tensors.
Declare an array type which can be used to initialize an object of this type statically. For rank-1 tensors, this array is simply an array of length dim
of scalars of type Number
. For higher-rank tensors, it is an array of length dim
of the array_type
of the next lower-rank tensor.
This class is occasionally instantiated for dim == 0
. C++ does not allow the creation of zero-length arrays. As a consequence, if dim==0
, then all arrays that should have length dim
are instead declared as having length 1 to avoid compiler errors.
Internal type declaration that is used to specialize the return type of operator[]() for Tensor<1,dim,Number>
|
constexpr |
Constructor. Initialize all entries to zero.
|
explicitconstexpr |
A constructor where the data is copied from a C-style array.
|
explicitconstexpr |
A constructor where the data is copied from an ArrayView object. Obviously, the ArrayView object must represent a stretch of data of size dim
rank
. The sequentially ordered elements of the argument initializer
are interpreted as described by unrolled_to_component_indices().
This constructor obviously requires that the ElementType
type is either equal to Number
, or is convertible to Number
.
|
constexpr |
Constructor from tensors with different underlying scalar type. This obviously requires that the OtherNumber
type is convertible to Number
.
|
constexpr |
Constructor that converts from a "tensor of tensors".
|
constexprprivate |
This constructor is for internal use. It provides a way to create constexpr constructors for Tensor<rank, dim, Number>
|
constexpr |
Conversion operator to tensor of tensors.
|
constexpr |
Read-Write access operator.
|
constexpr |
Read-only access operator.
|
constexpr |
Read access using TableIndices indices
|
constexpr |
Read and write access using TableIndices indices
Return a pointer to the first element of the underlying storage.
const Number * Tensor< rank_, dim, Number >::begin_raw | ( | ) | const |
Return a const pointer to the first element of the underlying storage.
Return a pointer to the element past the end of the underlying storage.
const Number * Tensor< rank_, dim, Number >::end_raw | ( | ) | const |
Return a pointer to the element past the end of the underlying storage.
|
constexpr |
Assignment operator from tensors with different underlying scalar type. This obviously requires that the OtherNumber
type is convertible to Number
.
This operator assigns a scalar to a tensor. To avoid confusion with what exactly it means to assign a scalar value to a tensor, zero is the only value allowed for d
, allowing the intuitive notation t=0
to reset all elements of the tensor to zero.
Assign a scalar to the current object. This overload is used for rvalue references; because it does not make sense to assign something to a temporary, the function is deleted.
|
constexpr |
Test for equality of two tensors.
|
constexpr |
Test for inequality of two tensors.
|
constexpr |
Add another tensor.
|
constexpr |
Subtract another tensor.
|
constexpr |
Scale the tensor by factor
, i.e. multiply all components by factor
.
|
constexpr |
Scale the vector by 1/factor
.
Unary minus operator. Negate all entries of a tensor.
|
constexpr |
Reset all values to zero.
Note that this is partly inconsistent with the semantics of the clear()
member functions of the standard library containers and of several other classes within deal.II, which not only reset the values of stored elements to zero, but release all memory and return the object into an empty state. However, since the size of objects of the present type is determined by its template parameters, resizing is not an option, and indeed the state where all elements have a zero value is the state right after construction of such an object.
numbers::NumberTraits< Number >::real_type Tensor< rank_, dim, Number >::norm | ( | ) | const |
Return the Frobenius-norm of a tensor, i.e. the square root of the sum of the absolute squares of all entries. For the present case of rank-1 tensors, this equals the usual l2
norm of the vector.
|
constexpr |
Return the square of the Frobenius-norm of a tensor, i.e. the sum of the absolute squares of all entries.
void Tensor< rank_, dim, Number >::unroll | ( | Vector< OtherNumber > & | result | ) | const |
Fill a vector with all tensor elements.
This function unrolls all tensor entries into a single, linearly numbered vector. As usual in C++, the rightmost index of the tensor marches fastest.
void Tensor< rank_, dim, Number >::unroll | ( | const Iterator | begin, |
const Iterator | end ) const |
Fill a range with all tensor elements.
This function unrolls all tensor entries into a single, linearly numbered sequence. The order of the elements is the one given by component_to_unrolled_index().
The template type Number must be convertible to the type of *begin
.
|
staticconstexpr |
Return an unrolled index in the range
|
staticconstexpr |
Opposite of component_to_unrolled_index: For an index in the range
|
staticconstexpr |
Determine an estimate for the memory consumption (in bytes) of this object.
void Tensor< rank_, dim, Number >::serialize | ( | Archive & | ar, |
const unsigned int | version ) |
Read or write the data of this object to or from a stream for the purpose of serialization using the BOOST serialization library.
|
friend |
|
Perform an MPI sum of the entries of a tensor.
|
|
|
|
|
|
|
|
Multiplication of a tensor of general rank with a scalar number from the right.
Only multiplication with a scalar number type (i.e., a floating point number, a complex floating point number, etc.) is allowed, see the documentation of EnableIfScalar for details.
|
Multiplication of a tensor of general rank with a scalar number from the left.
Only multiplication with a scalar number type (i.e., a floating point number, a complex floating point number, etc.) is allowed, see the documentation of EnableIfScalar for details.
|
Division of a tensor of general rank with a scalar number. See the discussion on operator*() above for more information about template arguments and the return type.
|
|
Entrywise multiplication of two tensor objects of general rank.
This multiplication is also called "Hadamard-product" (c.f. https://en.wikipedia.org/wiki/Hadamard_product_(matrices)), and generates a new tensor of size <rank, dim>:
rank | The rank of both tensors. |
|
The dot product (single contraction) for tensors. This function return a tensor of rank src1
of rank rank_1
with the first index of a tensor src2
of rank rank_2:
operator*()
performs a double contraction. The origin of the difference in how operator*()
is implemented between Tensor and SymmetricTensor is that for the former, the product between two Tensor objects of same rank and dimension results in another Tensor object – that it, operator*()
corresponds to the multiplicative group action within the group of tensors. On the other hand, there is no corresponding multiplicative group action with the set of symmetric tensors because, in general, the product of two symmetric tensors is a nonsymmetric tensor. As a consequence, for a mathematician, it is clear that operator*()
for symmetric tensors must have a different meaning: namely the dot or scalar product that maps two symmetric tensors of rank 2 to a scalar. This corresponds to the double-dot (colon) operator whose meaning is then extended to the product of any two even-ranked symmetric tensors.rank_1==rank_2==1
, then a scalar number is returned as an unwrapped number type.
|
Generic contraction of a pair of indices of two tensors of arbitrary rank: Return a tensor of rank index_1
of a tensor src1
of rank rank_1
with the index index_2
of a tensor src2
of rank rank_2:
If for example the first index (index_1==0
) of a tensor t1
shall be contracted with the third index (index_2==2
) of a tensor t2
, this function should be invoked as
|
Generic contraction of two pairs of indices of two tensors of arbitrary rank: Return a tensor of rank index_1
with index index_2
, and index index_3
with index index_4
of a tensor src1
of rank rank_1
and a tensor src2
of rank rank_2:
If for example the first index (index_1==0
) shall be contracted with the third index (index_2==2
), and the second index (index_3==1
) with the first index (index_4==0
) of a tensor t2
, this function should be invoked as
|
|
Full contraction of three tensors: Return a scalar number that is the result of a full contraction of a tensor left
of rank rank_1
, a tensor middle
of rank right
of rank rank_2:
|
|
Return the cross product in 2d. This is just a rotation by 90 degrees clockwise to compute the outer normal from a tangential vector. This function is defined for all space dimensions to allow for dimension independent programming (e.g. within switches over the space dimension), but may only be called if the actual dimension of the arguments is two (e.g. from the dim==2
case in the switch).
|
Return the cross product of 2 vectors in 3d. This function is defined for all space dimensions to allow for dimension independent programming (e.g. within switches over the space dimension), but may only be called if the actual dimension of the arguments is three (e.g. from the dim==3
case in the switch).
|
|
|
|
Return the nearest orthogonal matrix
This is a (nonlinear) projection operation since when applied twice, we have
due to the fact that the
A | The tensor for which to find the closest orthogonal tensor. |
Number | The type used to store the entries of the tensor. Must be either float or double . |
A
must not be singular. This is because, conceptually, the problem to be solved here is trying to find a matrix Provide a way to get the dimension of an object without explicit knowledge of it's data type. Implementation is this way instead of providing a function dimension()
because now it is possible to get the dimension at compile time without the expansion and preevaluation of an inlined function; the compiler may therefore produce more efficient code and you may use this value to declare other data types.
Number of independent components of a tensor of current rank. This is dim times the number of independent components of each sub-tensor, which equates to dim^rank
.
This number can be used to loop over all of the entries of a tensor, using the unrolled_to_component_indices() function: