File: ngTMatrix.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 (76 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (4)
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
#### Nonzero Pattern Sparse Matrices in triplet format

### contains = "nsparseMatrix"
###             ============= ---> superclass methods in ./nsparseMatrix.R


setAs("ngTMatrix", "lgeMatrix",
      function(from) .Call(lgTMatrix_to_lgeMatrix, as(from,"lgTMatrix")))
setAs("ngTMatrix", "ngeMatrix",
      function(from) as(as(from, "lgeMatrix"), "nMatrix"))

setAs("ngTMatrix", "generalMatrix", function(from) as(from, "ngeMatrix"))

setAs("ngTMatrix", "matrix",
      function(from) .Call(lgTMatrix_to_matrix, as(from, "lgTMatrix")))
## setAs("ngTMatrix", "matrix", # go via fast C code:
##       function(from) as(as(from, "ngCMatrix"), "matrix"))

setAs("matrix", "ngTMatrix",
      function(from) {
	  if(!is.logical(from))
	      storage.mode(from) <- "logical"
	  if(anyNA(from))
	      stop("cannot coerce 'NA's to \"nsparseMatrix\"")
          dn <- dimnames(from)
          if(is.null.DN(dn))
              dn <- list(NULL,NULL)
          else dimnames(from) <- NULL # such that which(.) does not see any:
	  ij <- which(from, arr.ind = TRUE, useNames = FALSE) - 1L
	  if(length(ij) == 0) ij <- matrix(ij, 0, 2)
	  new("ngTMatrix",
	      i = ij[,1],
	      j = ij[,2],
	      Dim = as.integer(dim(from)),
	      Dimnames = dn)
	  })

setAs("matrix", "nMatrix", function(from) as(from, "ngTMatrix"))

setAs("ngTMatrix", "dgTMatrix",
      function(from)
      ## more efficient than
      ## as(as(as(sM, "ngCMatrix"), "dgCMatrix"), "dgTMatrix")
      new("dgTMatrix", i = from@i, j = from@j,
	  x = rep.int(1, length(from@i)),
	  ## cannot copy factors, but can we use them?
	  Dim = from@Dim, Dimnames= from@Dimnames))
setAs("ngTMatrix", "dMatrix", function(from) as(from, "dgTMatrix"))
setAs("ngTMatrix", "dsparseMatrix", function(from) as(from, "dgTMatrix"))


setAs("ngTMatrix", "lgTMatrix",
      function(from)
      new("lgTMatrix", i = from@i, j = from@j,
	  x = rep.int(TRUE, length(from@i)),
	  ## cannot copy factors, but can we use them?
	  Dim = from@Dim, Dimnames= from@Dimnames))
setAs("ngTMatrix", "lMatrix", function(from) as(from, "lgTMatrix"))

setAs("ngTMatrix", "triangularMatrix",
      function(from) check.gT2tT(from, toClass = "ntTMatrix", do.n=TRUE))
setAs("ngTMatrix", "ntTMatrix",
      function(from) check.gT2tT(from, toClass = "ntTMatrix", do.n=TRUE))
setAs("ngTMatrix", "symmetricMatrix",
      function(from) check.gT2sT(from, toClass = "nsTMatrix", do.n=TRUE))
## We favor coercion to super-classes, here, "symmetricMatrix"
## setAs("ngTMatrix", "nsTMatrix",
##       function(from) check.gT2sT(from, toClass = "nsTMatrix", do.n=TRUE))


if(FALSE) ## unneeded: use t.<TsparseMatrix>
setMethod("t", signature(x = "ngTMatrix"),
	  function(x) new("ngTMatrix", i = x@j, j = x@i,
			  Dim = x@Dim[2:1],
			  Dimnames= x@Dimnames[2:1]),
	  valueClass = "ngTMatrix")