An S4 class for a tensor with arbitrary number of modes. The Tensor class extends the base 'array' class to include additional tensor manipulation (folding, unfolding, reshaping, subsetting) as well as a formal class definition that enables more explicit tensor algebra.
number of modes (integer)
vector of modes (integer), aka sizes/extents/dimensions
actual data of the tensor, which can be 'array' or 'vector'
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(.Object = "Tensor")
: ...
signature(tnsr1 = "Tensor", tnsr2 = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(e1 = "array", e2 = "Tensor")
: ...
signature(e1 = "numeric", e2 = "Tensor")
: ...
signature(e1 = "Tensor", e2 = "array")
: ...
signature(e1 = "Tensor", e2 = "numeric")
: ...
signature(e1 = "Tensor", e2 = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
signature(tnsr = "Tensor")
: ...
This can be seen as a wrapper class to the base array
class. While it is possible to create an instance using new
, it is also possible to do so by passing the data into as.tensor
.
Each slot of a Tensor instance can be obtained using @
.
The following methods are overloaded for the Tensor class: dim-methods
, head-methods
, tail-methods
, print-methods
, show-methods
, element-wise array operations, array subsetting (extract via `['), array subset replacing (replace via `[<-'), and tperm-methods
, which is a wrapper around the base aperm
method.
To sum across any one mode of a tenor, use the function modeSum-methods
. To compute the mean across any one mode, use modeMean-methods
.
You can always unfold any Tensor into a matrix, and the unfold-methods
, k_unfold-methods
, and matvec-methods
methods are for that purpose. The output can be kept as a Tensor with 2 modes or a matrix
object. The vectorization function is also provided as vec
. See the attached vignette for a visualization of the different unfoldings.
Conversion from array
/matrix
to Tensor is facilitated via as.tensor
. To convert from a Tensor instance, simply invoke @data
.
The Frobenius norm of the Tensor is given by fnorm-methods
, while the inner product between two Tensors (of equal modes) is given by innerProd-methods
. You can also sum through any one mode to obtain the K-1 Tensor sum using modeSum-methods
. modeMean-methods
provides similar functionality to obtain the K-1 Tensor mean. These are primarily meant to be used internally but may be useful in doing statistics with Tensors.
For Tensors with 3 modes, we also overloaded t
(transpose) defined by Kilmer et.al (2013). See t-methods
.
To create a Tensor with i.i.d. random normal(0, 1) entries, see rand_tensor
.
James Li, Jacob Bien, Martin T. Wells (2018). rTensor: An R Package for Multidimensional Array (Tensor) Unfolding, Multiplication, and Decomposition. Journal of Statistical Software, 87(10), 1-31. URL http://www.jstatsoft.org/v087/i10/.
# NOT RUN {
tnsr <- rand_tensor()
class(tnsr)
tnsr
print(tnsr)
dim(tnsr)
tnsr@num_modes
tnsr@data
# }
Run the code above in your browser using DataLab