rTensor (version 1.4)

Tensor-class: S4 Class for a Tensor

Description

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.

Arguments

Slots

num_modes

number of modes (integer)

modes

vector of modes (integer), aka sizes/extents/dimensions

data

actual data of the tensor, which can be 'array' or 'vector'

Methods

[

signature(tnsr = "Tensor"): ...

[<-

signature(tnsr = "Tensor"): ...

matvec

signature(tnsr = "Tensor"): ...

dim

signature(tnsr = "Tensor"): ...

fnorm

signature(tnsr = "Tensor"): ...

head

signature(tnsr = "Tensor"): ...

initialize

signature(.Object = "Tensor"): ...

innerProd

signature(tnsr1 = "Tensor", tnsr2 = "Tensor"): ...

modeMean

signature(tnsr = "Tensor"): ...

modeSum

signature(tnsr = "Tensor"): ...

Ops

signature(e1 = "array", e2 = "Tensor"): ...

Ops

signature(e1 = "numeric", e2 = "Tensor"): ...

Ops

signature(e1 = "Tensor", e2 = "array"): ...

Ops

signature(e1 = "Tensor", e2 = "numeric"): ...

Ops

signature(e1 = "Tensor", e2 = "Tensor"): ...

print

signature(tnsr = "Tensor"): ...

k_unfold

signature(tnsr = "Tensor"): ...

show

signature(tnsr = "Tensor"): ...

t

signature(tnsr = "Tensor"): ...

tail

signature(tnsr = "Tensor"): ...

unfold

signature(tnsr = "Tensor"): ...

tperm

signature(tnsr = "Tensor"): ...

image

signature(tnsr = "Tensor"): ...

Details

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.

References

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/.

See Also

as.tensor

Examples

Run this code
# 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 DataCamp Workspace