Tensor#

template<typename T, size_t Rank>
struct Tensor : public virtual einsums::tensor_props::CoreTensorBase, public virtual einsums::tensor_props::TRBasicTensorBase<T, Rank>, public virtual einsums::tensor_props::LockableTensorBase, public virtual einsums::tensor_props::AlgebraOptimizedTensor#

Represents a general tensor.

Template Parameters:
  • T – data type of the underlying tensor data

  • Rank – the rank of the tensor

Public Types

using datatype = T#
using Vector = VectorData<T>#

Public Functions

Tensor() = default#

Construct a new Tensor object.

Default constructor.

Tensor(const Tensor&) = default#

Construct a new Tensor object.

Default copy constructor

~Tensor() = default#

Destroy the Tensor object.

template<typename ...Dims>
inline explicit Tensor(std::string name, Dims... dims)#

Construct a new Tensor object with the given name and dimensions.

Constructs a new Tensor object using the information provided in name and dims .

auto A = Tensor("A", 3, 3);

The newly constructed Tensor is NOT zeroed out for you. If you start having NaN issues in your code try calling Tensor.zero() or zero(Tensor) to see if that resolves it.

Template Parameters:

Dims – Variadic template arguments for the dimensions. Must be castable to size_t.

Parameters:
  • name – Name of the new tensor.

  • dims – The dimensions of each rank of the tensor.

template<size_t OtherRank, typename ...Dims>
inline explicit Tensor(Tensor<T, OtherRank> &&existingTensor, std::string name, Dims... dims)#

Construct a new Tensor object.

Moving existingTensor data to the new tensor.

This constructor is useful for reshaping a tensor. It does not modify the underlying tensor data. It only creates new mapping arrays for how the data is viewed.

auto A = Tensor("A", 27); // Creates a rank-1 tensor of 27 elements
auto B = Tensor(std::move(A), "B", 3, 3, 3); // Creates a rank-3 tensor of 27 elements
// At this point A is no longer valid.

Supports using -1 for one of the ranks to automatically compute the dimensional of it.

auto A = Tensor("A", 27);
auto B = Tensor(std::move(A), "B", 3, -1, 3); // Automatically determines that -1 should be 3.
Template Parameters:
  • OtherRank – The rank of existingTensor can be different than the rank of the new tensor

  • Dims – Variadic template arguments for the dimensions. Must be castable to size_t.

Parameters:
  • existingTensor – The existing tensor that holds the tensor data.

  • name – The name of the new tensor

  • dims – The dimensionality of each rank of the new tensor.

inline explicit Tensor(Dim<Rank> dims)#

Construct a new Tensor object using the dimensions given by Dim object.

Parameters:

dims – The dimensions of the new tensor in Dim form.

inline Tensor(const TensorView<T, Rank> &other)#

Construct a new Tensor object from a TensorView.

Data is explicitly copied from the view to the new tensor.

Parameters:

other – The tensor view to copy.

inline void resize(Dim<Rank> dims)#

Resize a tensor.

Parameters:

dims – The new dimensions of a tensor.

template<typename ...Dims>
inline void resize(Dims... dims)#

Resize a tensor.

Parameters:

dims – The new dimensions of a tensor.

inline void zero()#

Zeroes out the tensor data.

inline void set_all(T value)#

Set the all entries to the given value.

Parameters:

value – Value to set the elements to.

inline virtual T *data() override#

Returns a pointer to the data.

Try very hard to not use this function. Current data may or may not exist on the host device at the time of the call if using GPU backend.

Returns:

T* A pointer to the data.

inline virtual const T *data() const override#

Returns a constant pointer to the data.

Try very hard to not use this function. Current data may or may not exist on the host device at the time of the call if using GPU backend.

Returns:

const T* An immutable pointer to the data.

template<typename ...MultiIndex>
inline T *data(MultiIndex... index)#

Returns a pointer into the tensor at the given location.

auto A = Tensor("A", 3, 3, 3); // Creates a rank-3 tensor of 27 elements

double* A_pointer = A.data(1, 2, 3); // Returns the pointer to element (1, 2, 3) in A.
Template Parameters:

MultiIndex – The datatypes of the passed parameters. Must be castable to

Parameters:

index – The explicit desired index into the tensor. Must be castable to std::int64_t.

Returns:

A pointer into the tensor at the requested location.

template<typename ...MultiIndex>
inline const T &operator()(MultiIndex... index) const#

Subscripts into the tensor.

This version works when all elements are explicit values into the tensor. It does not work with the All or Range tags.

Template Parameters:

MultiIndex – Datatype of the indices. Must be castable to std::int64_t.

Parameters:

index – The explicit desired index into the tensor. Elements must be castable to std::int64_t.

Returns:

const T&

template<typename ...MultiIndex>
inline T &operator()(MultiIndex... index)#

Subscripts into the tensor.

This version works when all elements are explicit values into the tensor. It does not work with the All or Range tags.

Template Parameters:

MultiIndex – Datatype of the indices. Must be castable to std::int64_t.

Parameters:

index – The explicit desired index into the tensor. Elements must be castable to std::int64_t.

Returns:

T&

template<typename ...MultiIndex>
inline TensorView<T, count_of_type<AllT, MultiIndex...>() + count_of_type<Range, MultiIndex...>()> operator()(MultiIndex... index)#
template<typename ...MultiIndex>
inline const TensorView<T, count_of_type<AllT, MultiIndex...>() + count_of_type<Range, MultiIndex...>()> operator()(MultiIndex... index) const#
template<typename ...MultiIndex>
inline TensorView<T, Rank> operator()(MultiIndex... index) const#
template<typename Container>
inline T &operator()(const Container &index)#
template<typename Container>
inline const T &operator()(const Container &index) const#
inline Tensor<T, Rank> &operator=(const Tensor<T, Rank> &other)#
template<typename TOther>
inline Tensor<T, Rank> &operator=(const Tensor<TOther, Rank> &other)#
template<TensorConcept OtherTensor>
inline Tensor<T, Rank> &operator=(const OtherTensor &other)#
template<typename TOther>
inline Tensor<T, Rank> &operator=(const TensorView<TOther, Rank> &other)#
inline Tensor<T, Rank> &operator=(const DeviceTensor<T, Rank> &other)#
inline Tensor &operator=(const T &fill_value)#
inline Tensor<T, Rank> &operator*=(const T &b)#
inline Tensor<T, Rank> &operator*=(const Tensor<T, Rank> &b)#
inline Tensor<T, Rank> &operator/=(const T &b)#
inline Tensor<T, Rank> &operator/=(const Tensor<T, Rank> &b)#
inline Tensor<T, Rank> &operator+=(const T &b)#
inline Tensor<T, Rank> &operator+=(const Tensor<T, Rank> &b)#
inline Tensor<T, Rank> &operator-=(const T &b)#
inline Tensor<T, Rank> &operator-=(const Tensor<T, Rank> &b)#
inline virtual size_t dim(int d) const override#
inline virtual Dim<Rank> dims() const override#
inline const Vector &vector_data() const#
inline Vector &vector_data()#
inline virtual size_t stride(int d) const noexcept override#
inline virtual Stride<Rank> strides() const noexcept override#
inline TensorView<T, 1> to_rank_1_view() const#
inline size_t size() const#
inline virtual bool full_view_of_underlying() const noexcept override#
inline virtual const std::string &name() const override#
inline virtual void set_name(const std::string &new_name) override#