File: pMatrix.R

package info (click to toggle)
rmatrix 0.999375-43-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,068 kB
  • ctags: 2,395
  • sloc: ansic: 37,941; makefile: 216; sh: 128
file content (146 lines) | stat: -rw-r--r-- 5,436 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#### Permutation Matrices -- Coercion and Methods

## The typical   'constructor' : coerce from  'index'
setAs("integer", "pMatrix",
      function(from) {
          n <- length(from)
          nn <- names(from)
          new("pMatrix", Dim = rep.int(n, 2), Dimnames = list(nn,nn),
              perm = from)
      })

setAs("numeric", "pMatrix",
      function(from)
	  if(all(from == (i <- as.integer(from)))) as(i, "pMatrix")
	  else stop("coercion to 'pMatrix' only works from integer numeric"))

setAs("pMatrix", "matrix",
      function(from) {
	  fp <- from@perm
	  r <- ldiag(n = length(fp))[fp,]
	  if(.has.DN(from)) dimnames(r) <- from@Dimnames
	  r
      })

## coerce to 0/1 sparse matrix, i.e. sparse pattern
setAs("pMatrix", "ngTMatrix",
      function(from) {
          d <- from@Dim
	  new("ngTMatrix", i = seq_len(d[1]) - 1L, j = from@perm - 1L,
              Dim = d, Dimnames = from@Dimnames)
      })

setAs("pMatrix", "TsparseMatrix", function(from) as(from, "ngTMatrix"))
setAs("pMatrix", "nMatrix",	  function(from) as(from, "ngTMatrix"))
setAs("pMatrix", "lMatrix", function(from) as(as(from, "nMatrix"), "lMatrix"))
setAs("pMatrix", "dMatrix", function(from) as(as(from, "nMatrix"), "dMatrix"))
setAs("pMatrix", "dsparseMatrix", function(from) as(from, "dMatrix"))
setAs("pMatrix", "nsparseMatrix", function(from) as(from, "nMatrix"))
setAs("pMatrix", "CsparseMatrix",
      function(from) as(as(from, "ngTMatrix"), "CsparseMatrix"))
setAs("pMatrix", "ngeMatrix", function(from) as(as(from, "ngTMatrix"),"ngeMatrix"))

setAs("nMatrix", "pMatrix",
      function(from) {
	  from <- as(as(from, "TsparseMatrix"), "ngTMatrix")
	  n <- (d <- from@Dim)[1]
	  if(n != d[2]) stop("not a square matrix")
	  if(length(i <- from@i) != n)
	      stop("the number of non-zero entries differs from nrow(.)")
	  if((need.sort <- is.unsorted(i))) {
	      ii <- sort.list(i)
	      i <- i[ii]
	  }
	  if(n >= 1 && !identical(i, 0:(n - 1)))
	      stop("must have exactly one non-zero entry per row")
	  new("pMatrix", ## validity checking checks the 'perm' slot:
	      perm = 1L + if(need.sort) from@j[ii] else from@j,
	      Dim = d, Dimnames = from@Dimnames)
      })

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

setAs("sparseMatrix", "pMatrix", function(from)
      as(as(from, "nsparseMatrix"), "pMatrix"))

setMethod("is.na", signature(x = "pMatrix"), is.na_nsp)

## how much faster would this be in C? -- less than a factor of two?
.inv.perm <- function(p) { p[p] <- seq_along(p) ; p }

setMethod("solve", signature(a = "pMatrix", b = "missing"),
	  function(a, b, ...) {
              a@perm <- .inv.perm(a@perm)
              a@Dimnames <- a@Dimnames[2:1]
              a
          })

setMethod("solve", signature(a = "Matrix", b = "pMatrix"),
	  function(a, b, ...)
	  ## Or alternatively  solve(a, as(b, "CsparseMatrix"))
	  solve(a)[, .inv.perm(b@perm)])


setMethod("determinant", signature(x = "pMatrix", logarithm = "logical"),
	  function(x, logarithm, ...)
	  mkDet(logarithm=logarithm, ldet = 0, sig = signPerm(x@perm)))

## t(pM) is == the inverse  pM^(-1):
setMethod("t", signature(x = "pMatrix"), function(x) solve(x))


setMethod("%*%", signature(x = "matrix", y = "pMatrix"),
	  function(x, y) { mmultCheck(x,y); x[, .inv.perm(y@perm)] })
setMethod("%*%", signature(x = "Matrix", y = "pMatrix"),
	  function(x, y) { mmultCheck(x,y); x[, .inv.perm(y@perm)] })

setMethod("%*%", signature(x = "pMatrix", y = "matrix"),
	  function(x, y) { mmultCheck(x,y); y[x@perm ,] })
setMethod("%*%", signature(x = "pMatrix", y = "Matrix"),
	  function(x, y) { mmultCheck(x,y); y[x@perm ,] })

setMethod("%*%", signature(x = "pMatrix", y = "pMatrix"),
	  function(x, y) {
	      stopifnot(identical(d <- x@Dim, y@Dim))
	      ## n <- d[1]
	      ## FIXME: dimnames dealing: as with S3 matrix's  %*%
	      x@perm <- x@perm[y@perm]
	      x
	  })

setMethod("crossprod", signature(x = "pMatrix", y = "matrix"),
	  function(x, y) { mmultCheck(x,y, 2L); y[.inv.perm(x@perm) ,]})
setMethod("crossprod", signature(x = "pMatrix", y = "Matrix"),
	  function(x, y) { mmultCheck(x,y, 2L); y[.inv.perm(x@perm) ,]})
setMethod("crossprod", signature(x = "pMatrix", y = "pMatrix"),
	  function(x, y) {
	      stopifnot(identical(x@Dim, y@Dim))
	      x@perm <- .inv.perm(x@perm)[y@perm]
	      x
	  })

setMethod("tcrossprod", signature(x = "matrix", y = "pMatrix"),
	  function(x, y) { mmultCheck(x,y, 3L); x[, y@perm] })
setMethod("tcrossprod", signature(x = "Matrix", y = "pMatrix"),
	  function(x, y) { mmultCheck(x,y, 3L); x[, y@perm] })
setMethod("tcrossprod", signature(x = "pMatrix", y = "pMatrix"),
	  function(x, y) {
	      stopifnot(identical(x@Dim, y@Dim))
	      x@perm <- x@perm[.inv.perm(y@perm)]
	      x
	  })


setMethod("crossprod", signature(x = "pMatrix", y = "missing"),
          function(x, y=NULL) Diagonal(nrow(x)))
setMethod("tcrossprod", signature(x = "pMatrix", y = "missing"),
          function(x, y=NULL) Diagonal(nrow(x)))


.pMat.nosense <- function (x, i, j, ..., value)
    stop('replacing "pMatrix" entries is not allowed, as rarely sensible')
setReplaceMethod("[", signature(x = "pMatrix", i = "index"), .pMat.nosense)
setReplaceMethod("[", signature(x = "pMatrix", i = "missing", j = "index"),
		 .pMat.nosense) ##   explicit  ^^^^^^^^^^^^ for disambiguation
setReplaceMethod("[", signature(x = "pMatrix", i = "missing", j = "missing"),
		 .pMat.nosense)