t_shallow: Transpose a sparse matrix by changing its format

View source: R/trans.R

t_shallowR Documentation

Transpose a sparse matrix by changing its format

Description

Transposes a sparse matrix in CSC (a.k.a. "CsparseMatrix") or CSR (a.k.a. "RsparseMatrix") formats by converting it to the opposite format (i.e. CSC -> CSR, CSR -> CSC).

This implies only a shallow copy (i.e. it's much faster), as the only necessary thing to make such transpose operation is to swap the number of rows and columns and change the class of the object (all data remains the same), avoiding any deep copying and format conversion as when e.g. creating a CSC transpose of a CSC matrix.

If the input is neither a CSR not CSC matrix, it will just call the generic 't()' method.

Also provided is a function 't_deep' which outputs a transpose with the same storage order.

Usage

t_shallow(x)

t_deep(x)

## S4 method for signature 'RsparseMatrix'
t(x)

## S4 method for signature 'CsparseMatrix'
t(x)

## S4 method for signature 'TsparseMatrix'
t(x)

## S4 method for signature 'dgCMatrix'
t(x)

## S4 method for signature 'ngCMatrix'
t(x)

## S4 method for signature 'lgCMatrix'
t(x)

## S4 method for signature 'dtCMatrix'
t(x)

## S4 method for signature 'ntCMatrix'
t(x)

## S4 method for signature 'ltCMatrix'
t(x)

## S4 method for signature 'dsCMatrix'
t(x)

## S4 method for signature 'nsCMatrix'
t(x)

## S4 method for signature 'lsCMatrix'
t(x)

## S4 method for signature 'sparseVector'
t(x)

Arguments

x

A sparse matrix. If 'x' is of a different type, will just invoke its generic 't()' method.

Details

Important: When loading this package ('library(MatrixExtra)'), it will change the behavior of 't(sparseMatrix)' towards calling 't_shallow'.

This makes it more efficient, but has the potential of breaking existing code in other packages, particularly in the 'Matrix' package itself when calling some arbitrary function or method which would internally transpose a CSC matrix and rely on the assumption that its output is also CSC.

This behavior can be changed through restore_old_matrix_behavior or the package options (e.g. 'options("MatrixExtra.fast_transpose" = FALSE)' - ee MatrixExtra-options) to have 't_deep' as the default, just like in 'Matrix'.

Additionally, under the new behavior ('t_shallow' as the default for 't'), transposing a 'sparseVector' object will yield a CSR matrix ("RsparseMatrix"), which differs from 'Matrix' that would yield a COO matrix ("TsparseMatrix").

Value

The transpose of 'x' (rows become columns and columns become rows), but in the opposite format (CSC -> CSR, CSR -> CSC); or the same format if calling 't_deep'.

Examples

library(Matrix)
library(MatrixExtra)
set.seed(1)
X <- rsparsematrix(3, 4, .5, repr="C")
inherits(X, "CsparseMatrix")
Xtrans <- t_shallow(X)
inherits(Xtrans, "RsparseMatrix")
nrow(X) == ncol(Xtrans)
ncol(X) == nrow(Xtrans)

Xorig <- t_shallow(Xtrans)
inherits(Xorig, "CsparseMatrix")
inherits(t_deep(Xtrans), "RsparseMatrix")

### Important!!!
### This package makes 't_shallow' the default
set_new_matrix_behavior()
inherits(X, "CsparseMatrix")
inherits(t(X), "RsparseMatrix")

### Can be changed back to 't_deep' like this:
restore_old_matrix_behavior()
inherits(t(X), "CsparseMatrix")

MatrixExtra documentation built on May 29, 2024, 2:53 a.m.