bwlewis/irlba

Un-deprecate mult?

LTLA opened this issue · 4 comments

LTLA commented

Is it possible to un-deprecate the mult argument? I would like to use a custom matrix multiplication algorithm (in this case, parallelized with BiocParallel) on matrix representations that already have their own %*% methods. This is currently easy to achieve with mult - but if mult were to become unavailable, I would have to derive a new class for the sole purpose of implementing a new %*% method. This is not always easy in situations where the class of the input matrix is not known in advance. The example in ?irlba subclasses from an ordinary matrix, but one could imagine that the input is a sparse matrix instead, and I'm not sure how to define a single class during package compilation that accommodates all possible input representations.

I guess I could do something like:

setClass("myclass", slots=c(data="list"))

... store the matrix as an element of data, and then define %*% to retrieve the matrix from data when needed. But this seems a lot harder to write and understand compared to just calling mult.

LTLA commented

Actually, this turned out to be easier than I thought - just data="ANY" and then define the specialized matrix multiplication functions. Just to confirm: we only need to define %*% for any custom class, right? It seems that A is only ever used for %*%, so this should be necessary and sufficient for custom classes.

(The exception is in the "tiny problem" section, where A undergoes a number of arithmetic operations and is used in svd(); a call to as.matrix at the top of this section might be desirable.)

In any case, I'll close this.

Yes, sorry for the delay on responding, been away for a while. You need methods for multiplying matrix * vector and vector * matrix, so two methods are required.

Also I forgot to add that, although mult is deprecated and it will eventually disappear from the formal arguments, my approach so far with this code has to be demote deprecated functions to informal arguments in ... and still support them if its possible.

So mult will be available for the foreseeable future. This is a very long-term package and I'm trying to keep changes slow and non-abrupt.

LTLA commented

Thanks @bwlewis. Yep, I've got the two %*% methods covered, so I think I'm good to go without using mult. The only outstanding issue is that of the "tiny problem" handling for custom matrices:

library(irlba)
example(irlba, echo=FALSE) # setting it up.

smallA <- A[1:5,1:5]
smallscale <- col_scale[1:5]
a <- new("scaled_matrix", smallA, scale=smallscale)
set.seed(100)
irlba(a, 2, verbose=TRUE)$d
## [1] 2.6907650 0.8022033

ref <- sweep(smallA, 2, smallscale, "/") # should be the same result as 'a'.
set.seed(100)
irlba(ref, 2, verbose=TRUE)$d
## [1] 1.0438201 0.3027121

This demonstrates how the tiny problem section doesn't work for custom matrices, requiring definition of further arithmetic operators and as.matrix. Might be safer to only use it in the fastpath mode?