Skip to contents

S4 methods for kronecker and the %x% operator that keep the result as an amatrix. Without them, kronecker(A, B) and A %x% B dispatch to the Matrix methods for the parent dgeMatrix / dgCMatrix classes and silently demote to a plain (non-amatrix) result, discarding backend-dispatch metadata.

Usage

# S4 method for class 'adgeMatrix,adgeMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'adgeMatrix,adgCMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'adgCMatrix,adgeMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'adgCMatrix,adgCMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'adgeMatrix,matrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'matrix,adgeMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'adgCMatrix,matrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

# S4 method for class 'matrix,adgCMatrix'
kronecker(X, Y, FUN = "*", make.dimnames = FALSE, ...)

Arguments

X, Y

Kronecker factors. At least one is an aMatrix subclass; the other may be an amatrix, a base matrix, or a Matrix object.

FUN

Function (or its name) applied to the outer products; passed to the underlying Matrix method. Defaults to "*".

make.dimnames

Logical; construct dimnames from the factors. Passed to the underlying method.

...

Further arguments passed to the underlying method.

Value

An adgeMatrix (dense) or adgCMatrix (sparse).

Details

The product itself is computed by Matrix's own Kronecker methods on the materialized host contents, so values are identical to base::kronecker() on the dense contents. The result is re-wrapped as an adgCMatrix when it is sparse and an adgeMatrix otherwise, inheriting the preferred backend, policy, and precision of the first amatrix operand.

Examples

A <- adgeMatrix(matrix(1:4, 2, 2))
B <- adgeMatrix(diag(2))
kronecker(A, B)
#> An amatrix dense matrix [cpu|policy=auto|precision=strict]
#> 4 x 4 Matrix of class "adgeMatrix"
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    3    0
#> [2,]    0    1    0    3
#> [3,]    2    0    4    0
#> [4,]    0    2    0    4
A %x% B
#> An amatrix dense matrix [cpu|policy=auto|precision=strict]
#> 4 x 4 Matrix of class "adgeMatrix"
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    3    0
#> [2,]    0    1    0    3
#> [3,]    2    0    4    0
#> [4,]    0    2    0    4