1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
####----------- Minimal conversion utilities <--> "SparseM"
### I. The "natural pairs" between the two packages:
setAs("matrix.csr", "dgRMatrix",
function(from) {
new("dgRMatrix",
x = from@ra, j = from@ja - 1:1, p = from@ia - 1:1,
Dim = from@dimension)
})
setAs("dgRMatrix", "matrix.csr",
function(from) {
new("matrix.csr",
ra = from@x, ja = from@j + 1:1, ia = from@p + 1:1,
dimension = from@Dim)
})
setAs("matrix.csc", "dgCMatrix",
function(from) {
new("dgCMatrix",
x = from@ra, i = from@ja - 1:1, p = from@ia - 1:1,
Dim = from@dimension)
})
setAs( "dgCMatrix", "matrix.csc",
function(from) {
new("dgCMatrix",
ra = from@x, ja = from@i + 1:1, ia = from@p + 1:1,
Dim = from@dimension)
})
setAs("matrix.coo", "dgTMatrix",
function(from) {
new("dgTMatrix",
x = from@ra, i = from@ia - 1:1, j = from@ja - 1:1,
Dim = from@dimension)
})
setAs("dgTMatrix", "matrix.coo",
function(from) {
new("dgTMatrix",
ra = from@x, ia = from@i + 1:1, ja = from@j + 1:1,
Dim = from@dimension)
})
### II. Enable coercion to the ``favorite'' of each package;
### --- ----------------------------
### i.e., "dgCMatrix" and "matrix.csr"
setAs("dsparseMatrix", "matrix.csr",
function(from) as(as(from, "dgRMatrix"), "matrix.csr"))
##
setAs("matrix.csr", "dgCMatrix",
function(from) as(as(from, "dgRMatrix"), "dgCMatrix"))
setAs("matrix.coo", "dgCMatrix",
function(from) as(as(from, "dgTMatrix"), "dgCMatrix"))
## Easy coercion: just always use as( <SparseM.mat>, "Matrix") :
setAs("matrix.csr", "Matrix", function(from) as(from, "dgCMatrix")) # we favor 'dgC' so much ..
setAs("matrix.coo", "Matrix", function(from) as(from, "dgTMatrix"))
setAs("matrix.csc", "Matrix", function(from) as(from, "dgCMatrix"))
|