SCM

SCM Repository

[matrix] Annotation of /pkg/R/Auxiliaries.R
ViewVC logotype

Annotation of /pkg/R/Auxiliaries.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 949 - (view) (download)

1 : maechler 632 #### "Namespace private" Auxiliaries such as method functions
2 :     #### (called from more than one place --> need to be defined early)
3 :    
4 : maechler 656 ## For %*% (M = Matrix; v = vector (double or integer {complex maybe?}):
5 :     .M.v <- function(x, y) callGeneric(x, as.matrix(y))
6 :     .v.M <- function(x, y) callGeneric(rbind(x), y)
7 : maechler 632
8 : maechler 656 .has.DN <- ## has non-trivial Dimnames slot?
9 :     function(x) !identical(list(NULL,NULL), x@Dimnames)
10 :    
11 : maechler 949 .bail.out.1 <- function(fun, cl) {
12 :     stop(gettextf('not-yet-implemented method for %s(<%s>)', fun, cl),
13 :     call. = FALSE)
14 :     }
15 :     .bail.out.2 <- function(fun, cl1, cl2) {
16 :     stop(gettextf('not-yet-implemented method for %s(<%s>, <%s>)',
17 :     fun, cl1, cl2), call. = FALSE)
18 :     }
19 :    
20 : maechler 632 ## chol() via "dpoMatrix"
21 :     cholMat <- function(x, pivot, LINPACK) {
22 :     px <- as(x, "dpoMatrix")
23 : bates 703 if (isTRUE(validObject(px, test=TRUE))) chol(px)
24 : maechler 632 else stop("'x' is not positive definite -- chol() undefined.")
25 :     }
26 : maechler 908
27 :     rowCheck <- function(a, b) {
28 :     da <- dim(a)
29 :     db <- dim(b)
30 :     if(da[1] != db[1])
31 :     stop(gettextf("Matrices must have same number of rows in %s",
32 :     deparse(sys.call(sys.parent()))),
33 :     call. = FALSE)
34 :     ## return the common nrow()
35 :     da[1]
36 :     }
37 :    
38 :     colCheck <- function(a, b) {
39 :     da <- dim(a)
40 :     db <- dim(b)
41 :     if(da[2] != db[2])
42 :     stop(gettextf("Matrices must have same number of columns in %s",
43 :     deparse(sys.call(sys.parent()))),
44 :     call. = FALSE)
45 :     ## return the common ncol()
46 :     da[2]
47 :     }
48 :    
49 :    
50 : maechler 919 prTriang <- function(x, digits = getOption("digits"),
51 :     justify = "none", right = TRUE)
52 :     {
53 :     ## modeled along stats:::print.dist
54 :     diag <- TRUE
55 :     upper <- x@uplo == "U"
56 :    
57 :     m <- as(x, "matrix")
58 :     cf <- format(m, digits = digits, justify = justify)
59 :     if(upper)
60 :     cf[row(cf) > col(cf)] <- "."
61 :     else
62 :     cf[row(cf) < col(cf)] <- "."
63 :     print(cf, quote = FALSE, right = right)
64 :     invisible(x)
65 :     }
66 :    
67 :     prMatrix <- function(x, digits = getOption("digits")) {
68 :     d <- dim(x)
69 :     cl <- class(x)
70 :     cat(sprintf('%d x %d Matrix of class "%s"\n', d[1], d[2], cl))
71 :     maxp <- getOption("max.print")
72 :     if(prod(d) <= maxp) {
73 :     if(is(x, "triangularMatrix"))
74 :     prTriang(x, digits = digits)
75 :     else
76 :     print(as(x, "matrix"), digits = digits)
77 :     }
78 :     else { ## d[1] > maxp / d[2] >= nr :
79 :     m <- as(x, "matrix")
80 :     nr <- maxp %/% d[2]
81 :     n2 <- ceiling(nr / 2)
82 :     print(head(m, max(1, n2)))
83 :     cat("\n ..........\n\n")
84 :     print(tail(m, max(1, nr - n2)))
85 :     }
86 :     ## DEBUG: cat("str(.):\n") ; str(x)
87 :     invisible(x)# as print() S3 methods do
88 :     }
89 :    
90 :     ## For sparseness handling
91 :     non0ind <- function(x) {
92 :     if(is.numeric(x))
93 :     return(if((n <- length(x))) (0:(n-1))[x != 0] else integer(0))
94 :    
95 :     ## else return a (i,j) matrix of non-zero-indices
96 :    
97 :     stopifnot(is(x, "sparseMatrix"))
98 : maechler 925 if(is(x, "TsparseMatrix"))
99 : maechler 919 stop("'x' must be column- or row-compressed 'sparseMatrix'")
100 :     isCol <- function(M) any("i" == slotNames(M))
101 :     .Call("compressed_non_0_ij", x, isCol(x), PACKAGE = "Matrix")
102 :     }
103 :    
104 : maechler 925 ### There is a test on this in ../tests/dgTMatrix.R !
105 : maechler 919 uniq <- function(x) {
106 : maechler 925 if(is(x, "TsparseMatrix")) {
107 : maechler 919 ## Purpose: produce a *unique* triplet representation:
108 :     ## by having (i,j) sorted and unique
109 :     ## -----------------------------------------------------------
110 :     ## The following is *not* efficient {but easy to program}:
111 :     if(is(x, "dgTMatrix")) as(as(x, "dgCMatrix"), "dgTMatrix")
112 :     else if(is(x, "lgTMatrix")) as(as(x, "lgCMatrix"), "lgTMatrix")
113 :     else stop("not implemented for class", class(x))
114 :    
115 :     } else x # not 'gT' ; i.e. "uniquely" represented in any case
116 :     }
117 :    
118 :     if(FALSE) ## try an "efficient" version
119 :     uniq_gT <- function(x)
120 :     {
121 :     ## Purpose: produce a *unique* triplet representation:
122 :     ## by having (i,j) sorted and unique
123 :     ## ----------------------------------------------------------------------
124 :     ## Arguments: a "gT" Matrix
125 :     stopifnot(is(x, "gTMatrix"))
126 :     if((n <- length(x@i)) == 0) return(x)
127 :     ii <- order(x@i, x@j)
128 :     if(any(ii != 1:n)) {
129 :     x@i <- x@i[ii]
130 :     x@j <- x@j[ii]
131 :     x@x <- x@x[ii]
132 :     }
133 :     ij <- x@i + nrow(x) * x@j
134 :     if(any(dup <- duplicated(ij))) {
135 :    
136 :     }
137 :     ### We should use a .Call() based utility for this!
138 :    
139 :     }
140 :    
141 : maechler 946 t_geMatrix <- function(x) {
142 :     x@x <- as.vector(t(array(x@x, dim = x@Dim))) # no dimnames here
143 :     x@Dim <- x@Dim[2:1]
144 :     x@Dimnames <- x@Dimnames[2:1]
145 :     ## FIXME: how to set factors?
146 :     x
147 :     }
148 :    
149 :     ## t( [dl]trMatrix ) and t( [dl]syMatrix ) :
150 :     t_trMatrix <- function(x) {
151 :     x@x <- as.vector(t(as(x, "matrix")))
152 :     x@Dim <- x@Dim[2:1]
153 :     x@Dimnames <- x@Dimnames[2:1]
154 :     x@uplo <- if (x@uplo == "U") "L" else "U"
155 :     # and keep x@diag
156 :     x
157 :     }

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge