There are many jokes about how to define a tensor. The definition we are giving here might not be the most correct one, but it is good enough for our use case (don't kill me please, mathematicians). A tensor $T$ of order $n$ is a multilinear application between $n$ vector spaces over a field $\mathcal{F}$.
\[T : \mathcal{F}^{\dim(1)} \times \dots \times \mathcal{F}^{\dim(n)} \mapsto \mathcal{F}\]
In layman's terms, it is a linear function whose inputs are vectors and the output is a scalar number.
\[T(\mathbf{v}^{(1)}, \dots, \mathbf{v}^{(n)}) = c \in \mathcal{F} \qquad\qquad \forall i, \mathbf{v}^{(i)} \in \mathcal{F}^{\dim(i)}\]
Tensor algebra is a higher-order generalization of linear algebra, where scalar numbers can be viewed as order-0 tensors, vectors as order-1 tensors, matrices as order-2 tensors, ...
Letters are used to identify each of the vector spaces the tensor relates to. In computer science, you would intuitively think of tensors as "n-dimensional arrays with named dimensions".
\[T_{ijk} \iff \mathtt{T[i,j,k]}\]
In Tenet
, a tensor is represented by the Tensor
type, which wraps an array and a list of symbols. As it subtypes AbstractArray
, many array operations can be dispatched to it.
You can create a Tensor
by passing an array and a list of Symbol
s that name indices.
julia> Tᵢⱼₖ = Tensor(rand(3,5,2), (:i,:j,:k))
3×5×2 Tensor{Float64, 3, Array{Float64, 3}}:
+[:, :, 1] =
+ 0.281222 0.929199 0.576898 0.98589 0.00275758
+ 0.912367 0.352526 0.484502 0.916008 0.705166
+ 0.681177 0.563615 0.633433 0.450259 0.162055
+
+[:, :, 2] =
+ 0.495671 0.157812 0.668371 0.761257 0.0103359
+ 0.0571129 0.254092 0.0866395 0.515411 0.202336
+ 0.805155 0.308397 0.25795 0.243532 0.430124
The dimensionality or size of each index can be consulted using the size
function.
Base.size(::Tensor[, i])
Return the size of the underlying array or the dimension i
(specified by Symbol
or Integer
).
sourcejulia> size(Tᵢⱼₖ)
(3, 5, 2)
julia> size(Tᵢⱼₖ, :j)
5
julia> length(Tᵢⱼₖ)
30
contract(a::Tensor[, b::Tensor]; dims=nonunique([inds(a)..., inds(b)...]))
Perform tensor contraction operation.
sourceLinearAlgebra.svd(tensor::Tensor; left_inds, right_inds, virtualind, kwargs...)
Perform SVD factorization on a tensor.
Keyword arguments
left_inds
: left indices to be used in the SVD factorization. Defaults to all indices of t
except right_inds
.right_inds
: right indices to be used in the SVD factorization. Defaults to all indices of t
except left_inds
.virtualind
: name of the virtual bond. Defaults to a random Symbol
.
sourceLinearAlgebra.qr(tensor::Tensor; left_inds, right_inds, virtualind, kwargs...)
Perform QR factorization on a tensor.
Keyword arguments
left_inds
: left indices to be used in the QR factorization. Defaults to all indices of t
except right_inds
.right_inds
: right indices to be used in the QR factorization. Defaults to all indices of t
except left_inds
.virtualind
: name of the virtual bond. Defaults to a random Symbol
.
sourceLinearAlgebra.lu(tensor::Tensor; left_inds, right_inds, virtualind, kwargs...)
Perform LU factorization on a tensor.
Keyword arguments
left_inds
: left indices to be used in the LU factorization. Defaults to all indices of t
except right_inds
.right_inds
: right indices to be used in the LU factorization. Defaults to all indices of t
except left_inds
.virtualind
: name of the virtual bond. Defaults to a random Symbol
.
source