Class nuTens::Tensorο
-
class Tensorο
Subclassed by nuTens::BaseMatterSolver::EigenvalTensor, nuTens::BaseMatterSolver::EigenvecTensor
Constructors
Use these methods to construct tensors
-
inline Tensor()ο
Default constructor with no initialisation.
-
Tensor(const std::vector<float> &values, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)ο
Construct a 1-d array with specified values.
values The values to include in the tensor
-
virtual ~Tensor() = defaultο
Destructor.
-
static Tensor TensorComplex(const std::vector<std::complex<float>> &values, dtypes::scalarType type = dtypes::kComplexFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)ο
Construct a 1-d array with specified complex values.
values The values to include in the tensor
Warning
This can be quite slow due to internal conversions between complex types. Avoid using it for anything performance critical!!!
-
static Tensor eye(int n, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)ο
Construct an identity tensor (has to be a 2d square tensor)
n The size of one of the sides of the tensor
type The data type of the tensor
-
static Tensor rand(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = false)ο
Construct a tensor with entries randomly initialised in the range [0, 1].
shape The desired shape of the intitalised tensor
type The data type of the tensor
-
static Tensor diag(const Tensor &diag)ο
Construct a tensor diag values along the diagonal, and zero elsewhere.
diag A 1-d tensor which represents the desired diagonal values
Setters
Set the underlying data type of this tensor
-
Tensor &dType(dtypes::scalarType type)ο
-
Tensor &device(dtypes::deviceType device)ο
Set the device that this tensor lives on.
Getters
The underlying data type of this tensor
-
inline dtypes::scalarType getDType() constο
-
inline dtypes::deviceType getDevice() constο
The device that this tensor lives on.
-
inline bool getRequiresGrad() constο
Whether the tensor requires a gradient.
-
inline bool isInitialised() constο
Check if tensor has been initialised.
Matrix Arithmetic
Generally there are static functions with the pattern <function>(Mat1, Mat2) which will return a new matrix and inline equivalents with the pattern <function>_(Mat2) which will affect the object they are called by
-
void matmul_(const Tensor &tensor2)ο
Inplace matrix multiplication.
tensor2 Right hand matrix to multiply with this one
-
void scale_(float scalar)ο
Inplace matrix scaling.
scalar The scalar
-
void pow_(float scalar)ο
Inplace raise to scalar power.
scalar The scalar
-
void exp_()ο
Inplace element-wise exponential.
-
void transpose_(int dim0, int dim1)ο
Inplace transpose.
dim0 The first dimension to swap
dim1 The second dimension to swap
-
static Tensor matmul(const Tensor &tensor1, const Tensor &tensor2)ο
Multiply two matrices together.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor outer(const Tensor &tensor1, const Tensor &tensor2)ο
Outer product of two 1D tensors.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor mul(const Tensor &tensor1, const Tensor &tensor2)ο
Element-wise multiplication of two tensors.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor add(const Tensor &tensor1, const Tensor &tensor2)ο
Element-wise addition of two tensors.
tensor1 Left hand tensor
tensor2 Right hand tensor
-
static Tensor div(const Tensor &tensor1, const Tensor &tensor2)ο
Element-wise division of two tensors.
tensor1 Numerator
tensor2 Denominator
-
static Tensor pow(const Tensor &tensor, float scalar)ο
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static Tensor pow(const Tensor &tensor, double scalar)ο
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static Tensor pow(const Tensor &tensor, std::complex<float> scalar)ο
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static Tensor pow(const Tensor &tensor, std::complex<double> scalar)ο
Raise a matrix to a scalar power.
tensor The tensor
scalar The scalar
-
static inline Tensor square(const Tensor &tensor)ο
Element wise square, slightly faster than pow(tensor, 2.0)
tensor The tensor
-
static Tensor sqrt(const Tensor &tensor)ο
Get element-wise square root of tensor.
tensor The tensor
-
static Tensor transpose(const Tensor &tensor, int dim0, int dim1)ο
Get the transpose of a tensor.
tensor The tensor
dim0 The first dimension to swap
dim1 The second dimension to swap
-
static Tensor scale(const Tensor &tensor, float scalar)ο
Scale a matrix by some scalar.
scalar The scalar
tensor The tensor
-
static Tensor scale(const Tensor &tensor, double scalar)ο
Scale a matrix by some scalar.
scalar The scalar
tensor The tensor
Mathematical
mathematical function overrides, generally work as expected, unless otherwise noted
Gradients
-
void backward() constο
Compute gradients of this tensor with respect to leaves Those can then be accessed using gradient()
-
void zeroGrad()ο
Set the accumulated gradient for this tensor to zero.
Warning
This should be done any time you reuse a leaf tensor to calculate another gradient. Otherwise you will get the sum of all accumulated gradients
-
Tensor grad() constο
Return a tensor containing the accumulated gradients calculated for this tensor after calling backward()
Linear Algebra
-
static void eig(const Tensor &tensor, Tensor &eVals, Tensor &eVecs)ο
Get eigenvalues and vectors of a tensor ordering of the eigenvalues is not guarenteed for eigh!!! AAARRRGGHHH.
- Todo:
: figure out a way to deal with that!
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues
eVecs β [out] The eigenvectors
-
static void eigh(const Tensor &tensor, Tensor &eVals, Tensor &eVecs)ο
Get eigenvalues and vectors of a hermitian matrix.
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues
eVecs β [out] The eigenvectors This is in general faster and more stable than Tensor::eig and should be preferred in basically all cases where it can be used
-
static void eigvals(const Tensor &tensor, Tensor &eVals)ο
Get eigenvalues of a tensor ordering of the eigenvalues is not guarenteed for eigh!!! AAARRRGGHHH.
- Todo:
: figure out a way to deal with that!
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues
-
static void eigvalsh(const Tensor &tensor, Tensor &eVals)ο
Get eigenvalues of a hermitian matrix.
tensor The tensor
- Parameters:
eVals β [out] The eigenvalues This is in general faster and more stable than Tensor::eigvals and should be preferred in basically all cases where it can be used
Trigonometric
Public Types
Public Functions
-
Tensor &addBatchDim()ο
If the tensor does not already have a batch dimension (as set by hasBatchDim()) this will add one.
-
Tensor conj() constο
Get the complex conjugate of this tensor. If the underlying tensor is not complex, this will just return the tensor.
-
Tensor cumsum(int dim) constο
Get the cumulative sum over some dimension.
- Parameters:
dim β The dimension to sum over
-
Tensor sum(const std::vector<long int> &dims) constο
Get the result of summing this tensor over all dimensions.
- Parameters:
dims β The dimensions to sum over
-
void setValue(const std::vector<indexType> &indices, const Tensor &value)ο
Set the value at a particular index of the tensor.
indices The indices of the value to set
value The value to set it to
-
Tensor getValues(const std::vector<indexType> &indices) constο
Get the value at a certain entry in the tensor.
- Parameters:
indices β The index of the entry to get
-
variantType getVariantValue(const std::vector<int> &indices) constο
Get the value at a certain entry in the tensor as an std::variant.
This mainly exists so we can get the values of a tensor in python as pybind11 DOES NOT like templated functions If using the c++ interface it is probably easier, faster and safer to use the templated getValue() function.
-
size_t getNdim() constο
Get the number of dimensions in the tensor.
-
int getBatchDim() constο
Get the size of the batch dimension of the tensor.
-
bool getHasBatchDim() constο
Check if tensor has batch dimension prepended.
Public Static Functions
-
static bool gpuAvailable()ο
Check if there is a gpu available to use.
-
static inline Tensor cumsum(const Tensor &tensor, int dim)ο
Get the cumulative sum over some dimension.
- Parameters:
dim β The dimension to sum over
-
static inline Tensor sum(const Tensor &tensor)ο
Get the result of summing this tensor over all dimensions.
-
inline Tensor()ο