File: sparse.R

package info (click to toggle)
e1071 1.5-16-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 896 kB
  • ctags: 258
  • sloc: cpp: 2,684; ansic: 908; sh: 26; makefile: 1
file content (101 lines) | stat: -rw-r--r-- 2,956 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
read.matrix.csr <- function(file, fac = TRUE, ncol = NULL)
{
  if(!require("methods")) stop("Could not load package 'methods'.")
  if(!require("SparseM")) stop("Could not load package 'SparseM'.")

  l <- strsplit(readLines(file(file)), "[ ]+")

  ## extract y-values, if any
  y <- if (length(grep(":",l[[1]][1])))
    NULL
  else
    sapply(l, function(x) x[1])
  
  ## x-values
  rja <- do.call("rbind",
                 lapply(l, function(x)
                        do.call("rbind",
                                strsplit(if (is.null(y)) x else x[-1], ":")
                                )
                        )
                 )
  ja <- as.integer(rja[,1])
  ia <- cumsum(c(1, sapply(l, length) - !is.null(y)))

  max.ja <- max(ja)
  dimension <- c(length(l), if (is.null(ncol)) max.ja else max(ncol, max.ja))
  x = methods::new("matrix.csr", ra = as.numeric(rja[,2]), ja = ja,
    ia = as.integer(ia), dimension = as.integer(dimension))
  if (length(y)) 
    list(x = x, y = if (fac) as.factor(y) else as.numeric(y))
  else x
}

## old version: slow, but uses less memory
# read.matrix.csr <- function (file, fac = TRUE, ncol = NULL) 
# {
#     library(methods)
#     if (!require(SparseM)) 
#         stop("Need `SparseM' package!")
#     con <- file(file)
#     open(con)
#     y <- vector()
#     ia <- 1
#     ra <- ja <- c()
#     i <- 1
#     maxcol <- 1
#     while (isOpen(con) & length(buf <- readLines(con, 1)) > 0) {
#         s <- strsplit(buf, "[ ]+", extended = TRUE)[[1]]

#         ## y
#         if (length(grep(":", s[1])) == 0) {
#             y[i] <- if (fac) 
#                 s[1]
#             else as.numeric(s[1])
#             s <- s[-1]
#         }

#         ## x
#         if (length(s)) {
#             tmp <- do.call("rbind", strsplit(s, ":"))
#             ra <- c(ra, as.numeric(tmp[, 2]))
#             ja <- c(ja, as.numeric(tmp[, 1]))
#         }
        
#         i <- i + 1
#         ia[i] <- ia[i - 1] + length(s)
#     }
#     dimension <- c(i - 1, if (is.null(ncol)) max(ja) else max(ncol, ja))
#     x = new("matrix.csr", ra, as.integer(ja), as.integer(ia), as.integer(dimension))
#     if (length(y)) 
#         list(x = x, y = if (fac) as.factor(y) else y)
#     else x
# }

write.matrix.csr <- function (x, file="out.dat", y=NULL) {
  on.exit(sink())
  if(!require("methods")) stop("Could not load package 'methods'.")
  if (!is.null(y) & (length(y) != nrow(x)))
    stop(paste("Length of y (=", length(y),
                 ") does not match number of rows of x (=",
                 nrow(x), ")!", sep=""))
  sink(file)
  for (i in 1:nrow(x)) {
    if (!is.null(y)) cat (y[i],"")
    for (j in x@ia[i]:(x@ia[i+1] - 1))
      cat(x@ja[j], ":", x@ra[j], " ", sep="")
    cat("\n")
  }
}

na.fail.matrix.csr <- function(object, ...) {
  if(!require("methods")) stop("Could not load package 'methods'.")
  if (any(is.na(object@ra)))
    stop("missing values in object") else return(object)
}