File: SparseM-conv.R

package info (click to toggle)
rmatrix 1.3-2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,024 kB
  • sloc: ansic: 42,435; makefile: 330; sh: 180
file content (83 lines) | stat: -rw-r--r-- 3,045 bytes parent folder | download | duplicates (5)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
####-----------  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 - 1L, p = from@ia - 1L,
	      Dim = from@dimension)
      })
setAs("dgRMatrix", "matrix.csr",
      function(from) {
	  new("matrix.csr",
	      ra = from@x, ja = from@j + 1L, ia = from@p + 1L,
	      dimension = from@Dim)
      })


setAs("matrix.csc", "dgCMatrix",
      function(from) {
	  new("dgCMatrix",
	      x = from@ra, i = from@ja - 1L, p = from@ia - 1L,
	      Dim = from@dimension)
      })
setAs("dgCMatrix", "matrix.csc",
      function(from) {
	  new("matrix.csc",
	      ra = from@x, ja = from@i + 1L, ia = from@p + 1L,
	      dimension = from@Dim)
      })

setAs("matrix.coo", "dgTMatrix",
      function(from) {
	  new("dgTMatrix",
	      x = from@ra, i = from@ia - 1L, j = from@ja - 1L,
	      Dim = from@dimension)
      })
setAs("dgTMatrix", "matrix.coo",
      function(from) {
	  new("matrix.coo",
	      ra = from@x, ia = from@i + 1L, ja = from@j + 1L,
	      dimension = from@Dim)
      })

### II.  Enable coercion to the ``favorite'' of each package;
### ---         ----------------------------
###      i.e.,  "dgCMatrix" and  "matrix.csr"

setAs("dsparseMatrix", "matrix.csr",
      function(from) as(as(as(from, "RsparseMatrix"), "dgRMatrix"), "matrix.csr"))

##
setAs("matrix.csr", "dgCMatrix",
      function(from) as(as(from, "dgRMatrix"), "CsparseMatrix"))
setAs("matrix.coo", "dgCMatrix",
      function(from) as(as(from, "dgTMatrix"), "dgCMatrix"))

### also define the virtual coercions that we (should) advertize:
setAs("matrix.csr", "RsparseMatrix", function(from) as(from, "dgRMatrix"))
setAs("matrix.csc", "CsparseMatrix", function(from) as(from, "dgCMatrix"))
setAs("matrix.coo", "TsparseMatrix", function(from) as(from, "dgTMatrix"))
## to "Csparse*" and "Tsparse*" should work for all sparse:
setAs("matrix.csr", "CsparseMatrix",
      function(from) as(as(from, "dgRMatrix"), "CsparseMatrix"))
setAs("matrix.coo", "CsparseMatrix",
      function(from) as(as(from, "dgTMatrix"), "CsparseMatrix"))
setAs("matrix.csc", "TsparseMatrix",
      function(from) as(as(from, "dgCMatrix"), "TsparseMatrix"))
setAs("matrix.csr", "TsparseMatrix",
      function(from) as(as(from, "dgRMatrix"), "TsparseMatrix"))
## Also *from* (our favorite) Csparse should work to all 3 SparseM
setAs("CsparseMatrix", "matrix.csr",
      function(from) as(as(from, "RsparseMatrix"), "matrix.csr"))
setAs("CsparseMatrix", "matrix.coo",
      function(from) as(as(from, "TsparseMatrix"), "matrix.coo"))
setAs("CsparseMatrix", "matrix.csc",
      function(from) as(as(from, "dgCMatrix"), "matrix.csc"))

## Easy coercion: just always use as( <SparseM.mat>, "Matrix") :

setAs("matrix.csr", "Matrix", function(from) as(from, "CsparseMatrix")) # we favor!
setAs("matrix.coo", "Matrix", function(from) as(from, "TsparseMatrix"))
setAs("matrix.csc", "Matrix", function(from) as(from, "CsparseMatrix"))