A modern tensor class for R that provides MATLAB Tensor Toolbox compatibility
with high-performance operations via xtensor C++ backend.
Convenience function to create a Tensor object
Usage
tensor(data, dims = NULL)
Arguments
- data
A vector, matrix, array, or numeric value
- dims
The dimensions of the tensor. If NULL, inferred from data
Value
A new Tensor object
Public fields
data
The underlying array data
dims
The dimensions of the tensor
Initialize a new tensor
Methods
Tensor$new()
Usage
Tensor$new(data = NULL, dims = NULL, fast = FALSE)
Arguments
data
A vector, matrix, array, or numeric value
dims
The dimensions of the tensor. If NULL, inferred from data
fast
If TRUE, bypasses all checks. Used internally for performance.
Returns
A new Tensor object
Get tensor dimensions
Tensor$dim()
Returns
Integer vector of dimensions
Get number of elements
Tensor$length()
Returns
Integer number of elements
Get number of dimensions
Tensor$ndims()
Returns
Integer number of dimensions
Convert to R array
Tensor$as_array()
Returns
R array representation
Print tensor information
Tensor$print()
Returns
Invisible self
Show tensor (alias for print)
Tensor$show()
Returns
Invisible self
Clone the tensor
Tensor$clone_tensor()
Returns
A new Tensor object with copied data
Reshape the tensor
Tensor$reshape()
Returns
Self (in-place operation)
Squeeze the tensor by removing singleton dimensions
Tensor$squeeze()
Returns
New Tensor object with singleton dimensions removed
Permute tensor dimensions
Tensor$permute()
Returns
New Tensor object
Count nonzero entries
Tensor$nnz()
Returns
Integer count
Find nonzero entries
Tensor$find()
Usage
Tensor$find(values = FALSE)
Arguments
values
Logical; include values if TRUE
Returns
Matrix of subscripts or list with subs and vals
Vectorize tensor
Tensor$vec()
Returns
Numeric vector
Element-wise addition
Tensor$add()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise subtraction
Tensor$subtract()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise multiplication
Tensor$multiply()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise division
Tensor$divide()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise modulo
Tensor$modulo()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise integer division
Tensor$integer_divide()
Usage
Tensor$integer_divide(other)
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise power
Tensor$power()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise equality comparison
Tensor$equal()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise inequality comparison
Tensor$not_equal()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise less than comparison
Tensor$less_than()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise less than or equal comparison
Tensor$less_equal()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise greater than comparison
Tensor$greater_than()
Usage
Tensor$greater_than(other)
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise greater than or equal comparison
Tensor$greater_equal()
Usage
Tensor$greater_equal(other)
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise logical NOT
Tensor$logical_not()
Returns
Self (in-place operation)
Element-wise logical AND
Tensor$logical_and()
Usage
Tensor$logical_and(other)
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Element-wise logical OR
Tensor$logical_or()
Arguments
other
Another Tensor or numeric value
Returns
Self (in-place operation)
Sum along dimensions
Tensor$sum()
Arguments
dims
Dimensions to sum along (NULL for all)
Returns
New Tensor with reduced dimensions
Khatri-Rao product with another Tensor or matrix
Tensor$khatri_rao()
Usage
Tensor$khatri_rao(other, reverse = FALSE)
Arguments
other
Another Tensor or matrix
reverse
Logical indicating if reverse product should be computed
Returns
A matrix representing the Khatri-Rao product
Kronecker product with another Tensor or matrix
Tensor$kronecker()
Arguments
other
Another Tensor or matrix
Returns
A new Tensor
Hadamard (element-wise) product
Tensor$hadamard()
Arguments
other
Another Tensor or matrix
Returns
Self (in-place operation)
Frobenius norm
Tensor$clone()
The objects of this class are cloneable with this method.
Usage
Tensor$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
Examples
# Create a tensor from a matrix
t <- Tensor$new(matrix(1:6, nrow = 2, ncol = 3))
print(t)
#> <Tensor object>
#> A tensor of order 2 with dimensions: 2 x 3
# Create a tensor with specific dimensions
t2 <- Tensor$new(1:24, c(2, 3, 4))
# Mathematical operations using method syntax
t3 <- t2$clone_tensor()$add(t2)
# Mathematical operations using operator syntax
t4 <- t2 + t2
t5 <- t2 * 2
t6 <- 5 - t2
t7 <- t2 / 3
# Create a tensor from an array
t1 <- tensor(array(1:24, dim = c(3, 4, 2)))
# Create a tensor by specifying dimensions
t2 <- tensor(1:24, dims = c(3, 4, 2))
# Create a tensor filled with a single value
t3 <- tensor(0, dims = c(3, 4, 2))