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.929652 0.6907 0.241669 0.971429 0.342728
+ 0.957389 0.230249 0.946741 0.129696 0.927405
+ 0.153166 0.35612 0.213165 0.919537 0.711201
+
+[:, :, 2] =
+ 0.373375 0.130328 0.817179 0.831223 0.951725
+ 0.377761 0.0318606 0.57634 0.817418 0.406801
+ 0.445968 0.271757 0.386771 0.240182 0.520884
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