vt::Tensor#
-
template<size_t N>
size_t vt::get_size(const Shape<N> &shape)# Helper function to calculate the size of a tensor.
- Parameters:
shape – Shape of the tensor.
- Template Parameters:
N – Number of dimensions of the tensor.
- Returns:
size: Size of the tensor.
-
template<size_t N>
Shape<N> vt::get_strides(const Shape<N> &shape, const Order order)# Helper function to calculate the strides of a tensor.
- Parameters:
shape – Shape of the tensor.
order – Order of the tensor.
- Template Parameters:
N – Number of dimensions of the tensor.
- Returns:
strides: Strides of the tensor.
-
template<size_t N, typename ...Args>
Shape<sizeof...(Args) + N> vt::expand_shape(const Shape<N> &shape, Args... args)# Helper function to expand the shape.
- Parameters:
shape – Shape
args – New dimensions to be expanded.
- Template Parameters:
N – Shape dimensions.
Args – Variadic template for mulitple arguments.
- Returns:
Shape: Expanded shape.
-
template<typename T, size_t N>
class Tensor# Forward declaration of the Tensor class.
Tensor class. It is used to represent a tensor from thrust device vector. The concept of the tesnor is similar to the numpy array in Python. It stored a shared pointer to the data, and shape, strides, start index of the tensor.
- Template Parameters:
T – Data type of the tensor.
N – Number of dimensions of the tensor.
Subclassed by vt::TensorCondProxy< T, N >, vt::TensorSliceProxy< T, N >
Public Functions
-
inline Tensor(const Shape<N> &shape, const Order order)#
Construct a new tensor object from a shape.
- Parameters:
shape – Shape of the tensor.
order – Order of the tensor.
Construct a new tensor object from a shared vector and a shape.
- Parameters:
data – A shared vector
shape – Shape of the tensor.
order – Order of the tensor.
Construct a new tensor object from a shared vector and a shape, strides, start index.
- Parameters:
data – A shared vector
shape – Shape of the tensor.
strides – Strides of the tensor.
start – Start index of the tensor.
order – Order of the tensor.
contiguous – contiguous flag of the tensor.
-
template<typename ...Args>
inline Tensor<T, sizeof...(Args)> reshape(Args... args) const# Reshape the array to a new shape. If the tensor has been contiguous, the method will copy the tensor and reshape the new tensor.
- Template Parameters:
Args – Variadic template for mulitple arguments.
- Parameters:
args – New shape for the tensor.
- Returns:
Tensor: The reshaped tensor.
-
inline Tensor<T, N - 1> operator[](size_t index) const#
Operator for indexing the tensor along the last axis (row-major order). Notice that the method doesn’t gauranttee the contiguity of the tensor. If the tensor has been transposed, the return slice would not be contiguous.
- Parameters:
index – The index of the tensor.
- Returns:
Tensor: The sliced tensor.
-
inline TensorCondProxy<T, N> operator[](const Tensor<bool, N> &cond) const#
Operator for indexing the tensor based on the condition.
- Parameters:
cond – A boolean tensor with the same shape as the tensor.
- Returns:
TensorCondProxy: The tensor conditional proxy object.
-
template<typename ...Args>
inline TensorSliceProxy<T, N> operator()(Args... args) const# Operator for slicing the tensor. It could receive multiple slices through explicit Slice constructor.
- Parameters:
args – Slices to be applied to the tensor.
- Template Parameters:
Args – Variadic template for mulitple arguments.
- Returns:
TensorSliceProxy: The tensor slice proxy object.
-
inline TensorSliceProxy<T, N> operator()(std::array<Slice, N> &slices) const#
Operator for slicing the tensor.
- Parameters:
slices – An array of slices to be applied to the tensor.
- Returns:
TensorSliceProxy: The tensor slice proxy object.
-
inline TensorSliceProxy<T, 1> operator()(Slice s) const#
Operator for slicing the tensor. It supports one Slice implicit conversion for tensor.
- Parameters:
s – Slice to be applied to the tensor.
- Returns:
TensorSliceProxy: The tensor slice proxy object.
-
inline TensorSliceProxy<T, 2> operator()(Slice s1, Slice s2) const#
Operator for slicing the tensor. It supports two Slice implicit conversion for tensor.
- Parameters:
- Returns:
TensorSliceProxy: The tensor slice proxy object.
-
inline TensorSliceProxy<T, 3> operator()(Slice s1, Slice s2, Slice s3) const#
Operator for slicing the tensor. It supports three Slice implicit conversion for tensor.
- Parameters:
- Returns:
TensorSliceProxy: The tensor slice proxy object.
-
inline TensorSliceProxy<T, N> operator()(const EllipsisT &e, Slice s) const#
Operator for slicing the tensor. It supports Ellipsis and Slice implicit conversion for tensor.
- Parameters:
e – Ellipsis to be applied to the tensor.
s – Slice to be applied to the tensor.
- Returns:
TensorSliceProxy: The tensor slice proxy object.
-
inline Tensor<T, N + 1> operator()(const EllipsisT &e, const NewAxisT &n) const#
Operator to add a new axis at the last axis to the tensor.
- Parameters:
e – Ellipsis to be applied to the tensor.
n – NewAxis to be applied to the tensor.
- Returns:
Tensor: The new tensor object.
-
inline Tensor<T, N + 1> operator()(const NewAxisT &n, const EllipsisT &e) const#
Operator to add a new axis at the first axis to the tensor.
- Parameters:
n – NewAxis to be applied to the tensor.
e – Ellipsis to be applied to the tensor.
- Returns:
Tensor: The new tensor object.
-
inline Tensor operator+=(const T value) const#
Operator for in-place addition of a vector and a scalar.
- Parameters:
value – The scalar value to be added.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator-=(const T value) const#
Operator for in-place minus of a vector and a scalar.
- Parameters:
value – The scalar value to be minus.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator*=(const T value) const#
Operator for in-place multiplication of a vector and a scalar.
- Parameters:
value – The scalar value to be multiplied.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator/=(const T value) const#
Operator for in-place division of a vector and a scalar.
- Parameters:
value – The scalar value to be divided.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator+=(const Tensor &other) const#
Operator for in-place addition of two vectors.
- Parameters:
other – The other tensor to be added.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator-=(const Tensor &other) const#
Operator for in-place minus of two vectors.
- Parameters:
other – The other tensor to be minus.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator*=(const Tensor &other) const#
Operator for in-place multiplication of two vectors.
- Parameters:
other – The other tensor to be multiplied.
- Returns:
Tensor: The left-hand side tensor object.
-
inline Tensor operator/=(const Tensor &other) const#
Operator for in-place division of two vectors.
- Parameters:
other – The other tensor to be divided.
- Returns:
Tensor: The left-hand side tensor object.
-
inline TensorIterator<typename vector_type::iterator, N> begin() const#
Return the begin iterator for the Tensor.
- Returns:
TensorIterator: The iterator points to the 1st index of the tensor.
-
inline TensorIterator<typename vector_type::iterator, N> end() const#
Return the end iterator for the Tensor.
- Returns:
TensorIterator: The iterator points to the last index of the tensor.
-
inline Tensor apply_slices(const std::array<Slice, N> &slices) const#
Apply slices the tensor and return the new tensor object.
- Parameters:
slices – The slices to be applied to the tensor.
- Returns:
Tensor: The new tensor object.
-
template<typename U>
inline Tensor<U, N> astype()# Casts the array to given data type.
- Template Parameters:
U – The data type to cast.
- Returns:
Tensor<U, N>: The new tensor object.
-
inline T *raw_ptr() const#
Return the raw pointer of the tensor.
- Returns:
T*: The raw pointer of the tensor.
-
inline bool contiguous() const#
Return the contiguous flag of the tensor.
- Returns:
bool: The contiguous flag of the tensor.
-
inline Order order() const#
Return the order of the tensor.
- Returns:
Order: The order of the tensor.
-
inline size_t size() const#
Return the size of the tensor.
- Returns:
size_t: The size of the tensor.
-
inline size_t start() const#
Return the start index of the tensor.
- Returns:
size_t: The start index of the tensor.
-
inline Shape<N> shape() const#
Return the shape of the tensor.
- Returns:
Shape: The shape of the tensor.
-
inline Shape<N> strides() const#
Return the strides of the tensor.
- Returns:
Shape: The strides of the tensor.
-
inline std::shared_ptr<vector_type> data() const#
Return the shared pointer of the data.
- Returns:
std::shared_ptr<vector_type>: The shared pointer of the data.