Read matrices stored in the Harwell-Boeing or MatrixMarket formats or write sparseMatrix objects to one of these formats.

readHB(file)
readMM(file)
writeMM(obj, file, ...)

Arguments

obj

a real sparse matrix

file

for writeMM - the name of the file to be written. For readHB and readMM the name of the file to read, as a character scalar. The names of files storing matrices in the Harwell-Boeing format usually end in ".rua" or ".rsa". Those storing matrices in the MatrixMarket format usually end in ".mtx".

Alternatively, readHB and readMM accept connection objects.

...

optional additional arguments. Currently none are used in any methods.

Value

The readHB and readMM functions return an object that inherits from the "Matrix" class. Methods for the writeMM generic functions usually return NULL and, as a side effect, the matrix obj is written to file in the MatrixMarket format (writeMM).

Note

The Harwell-Boeing format is older and less flexible than the MatrixMarket format. The function writeHB was deprecated and has now been removed. Please use writeMM instead.

Note that these formats do not know anything about dimnames, hence these are dropped by writeMM().

A very simple way to export small sparse matrices S, is to use summary(S) which returns a data.frame with columns i, j, and possibly x, see summary in sparseMatrix-class, and an example below.

Examples

str(pores <- readMM(system.file("external/pores_1.mtx", package = "Matrix")))
#> Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
#>   ..@ i       : int [1:180] 0 1 2 3 10 11 0 1 2 3 ...
#>   ..@ j       : int [1:180] 0 0 0 0 0 0 1 1 1 1 ...
#>   ..@ Dim     : int [1:2] 30 30
#>   ..@ Dimnames:List of 2
#>   .. ..$ : NULL
#>   .. ..$ : NULL
#>   ..@ x       : num [1:180] -9.48e+02 -7.18e+06 4.73 3.57e+04 9.46e+02 ...
#>   ..@ factors : list()
str(utm   <- readHB(system.file("external/utm300.rua" , package = "Matrix")))
#> Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#>   ..@ i       : int [1:3155] 0 50 0 1 5 50 51 55 0 2 ...
#>   ..@ p       : int [1:301] 0 2 8 12 16 19 21 27 31 35 ...
#>   ..@ Dim     : int [1:2] 300 300
#>   ..@ Dimnames:List of 2
#>   .. ..$ : NULL
#>   .. ..$ : NULL
#>   ..@ x       : num [1:3155] -0.7071 0.7071 -0.0844 -0.697 -0.0844 ...
#>   ..@ factors : list()
str(lundA <- readMM(system.file("external/lund_a.mtx" , package = "Matrix")))
#> Formal class 'dsTMatrix' [package "Matrix"] with 7 slots
#>   ..@ i       : int [1:1298] 0 1 7 8 9 10 1 2 8 9 ...
#>   ..@ j       : int [1:1298] 0 0 0 0 0 0 1 1 1 1 ...
#>   ..@ Dim     : int [1:2] 147 147
#>   ..@ Dimnames:List of 2
#>   .. ..$ : NULL
#>   .. ..$ : NULL
#>   ..@ x       : num [1:1298] 75000000 961539 -12179486 -2617521 28846144 ...
#>   ..@ uplo    : chr "L"
#>   ..@ factors : list()
str(lundA <- readHB(system.file("external/lund_a.rsa" , package = "Matrix")))
#> Formal class 'dsCMatrix' [package "Matrix"] with 7 slots
#>   ..@ i       : int [1:1298] 0 1 7 8 9 10 1 2 8 9 ...
#>   ..@ p       : int [1:148] 0 6 14 22 30 38 46 52 57 67 ...
#>   ..@ Dim     : int [1:2] 147 147
#>   ..@ Dimnames:List of 2
#>   .. ..$ : NULL
#>   .. ..$ : NULL
#>   ..@ x       : num [1:1298] 75000000 961539 -12179486 -2617521 28846144 ...
#>   ..@ uplo    : chr "L"
#>   ..@ factors : list()
## https://math.nist.gov/MatrixMarket/data/Harwell-Boeing/counterx/counterx.htm
str(jgl   <- readMM(system.file("external/jgl009.mtx" , package = "Matrix")))
#> Formal class 'ngTMatrix' [package "Matrix"] with 5 slots
#>   ..@ i       : int [1:50] 0 1 3 4 5 6 7 8 1 2 ...
#>   ..@ j       : int [1:50] 0 0 0 0 0 0 0 0 1 1 ...
#>   ..@ Dim     : int [1:2] 9 9
#>   ..@ Dimnames:List of 2
#>   .. ..$ : NULL
#>   .. ..$ : NULL
#>   ..@ factors : list()

## NOTE: The following examples take quite some time
## ----  even on a fast internet connection:
if(FALSE) {
## The URL has been corrected, but we need an untar step:
u. <- url("https://www.cise.ufl.edu/research/sparse/RB/Boeing/msc00726.tar.gz")
str(sm <- readHB(gzcon(u.)))
}

data(KNex, package = "Matrix")
## Store as MatrixMarket (".mtx") file, here inside temporary dir./folder:
(MMfile <- file.path(tempdir(), "mmMM.mtx"))
#> [1] "/tmp/Rtmpy43tGg/mmMM.mtx"
writeMM(KNex$mm, file=MMfile)
#> NULL
file.info(MMfile)[,c("size", "ctime")] # (some confirmation of the file's)
#>                            size               ctime
#> /tmp/Rtmpy43tGg/mmMM.mtx 179265 2025-03-17 17:53:42

## very simple export - in triplet format - to text file:
data(CAex, package = "Matrix")
s.CA <- summary(CAex)
s.CA # shows  (i, j, x)  [columns of a data frame]
#> 72 x 72 sparse Matrix of class "dgCMatrix", with 216 entries
#>      i  j             x
#> 1    1  1  9.999980e-01
#> 2   25  1 -1.311884e-03
#> 3   49  1 -5.265519e-04
#> 4    2  2  9.999990e-01
#> 5   26  2 -9.034753e-04
#> 6   50  2 -3.812964e-04
#> 7    3  3  9.999991e-01
#> 8   27  3 -8.961401e-04
#> 9   51  3 -3.731561e-04
#> 10   4  4  9.999984e-01
#> 11  28  4 -1.147774e-03
#> 12  52  4 -5.260632e-04
#> 13   5  5  9.999981e-01
#> 14  29  5 -1.207952e-03
#> 15  53  5 -6.318516e-04
#> 16   6  6  9.999978e-01
#> 17  30  6 -1.391858e-03
#> 18  54  6 -5.198773e-04
#> 19   7  7  9.999980e-01
#> 20  31  7 -1.270809e-03
#> 21  55  7 -6.037332e-04
#> 22   8  8  9.999981e-01
#> 23  32  8 -1.238147e-03
#> 24  56  8 -6.384804e-04
#> 25   9  9  9.999983e-01
#> 26  33  9 -1.155710e-03
#> 27  57  9 -5.720348e-04
#> 28  10 10  9.999986e-01
#> 29  34 10 -1.064001e-03
#> 30  58 10 -5.441404e-04
#> 31  11 11  9.999991e-01
#> 32  35 11 -8.293441e-04
#> 33  59 11 -4.185556e-04
#> 34  12 12  9.999994e-01
#> 35  36 12 -6.887925e-04
#> 36  60 12 -2.979323e-04
#> 37  13 13  9.999992e-01
#> 38  37 13 -7.860880e-04
#> 39  61 13 -4.467988e-04
#> 40  14 14  9.999980e-01
#> 41  38 14 -1.252142e-03
#> 42  62 14 -6.295083e-04
#> 43  15 15  9.999955e-01
#> 44  39 15 -1.917600e-03
#> 45  63 15 -9.281638e-04
#> 46  16 16  9.999905e-01
#> 47  40 16 -2.825865e-03
#> 48  64 16 -1.230082e-03
#> 49  17 17  9.999852e-01
#> 50  41 17 -3.493069e-03
#> 51  65 17 -1.602529e-03
#> 52  18 18  9.999802e-01
#> 53  42 18 -3.930709e-03
#> 54  66 18 -2.073992e-03
#> 55  19 19  9.992581e-01
#> 56  43 19 -2.126081e-02
#> 57  67 19  1.700865e-02
#> 58  20 20  9.997528e-01
#> 59  44 20 -1.511505e-02
#> 60  68 20 -4.318587e-03
#> 61  21 21  9.985212e-01
#> 62  45 21 -3.319184e-02
#> 63  69 21  1.936191e-02
#> 64  22 22  9.739877e-01
#> 65  46 22  7.730061e-02
#> 66  70 22 -1.391411e-01
#> 67  23 23  9.979232e-01
#> 68  47 23 -3.219107e-02
#> 69  71 23  3.219107e-02
#> 70  24 24  9.999478e-01
#> 71  48 24 -2.594571e-03
#> 72  72 24 -6.745885e-03
#> 73   1 25 -1.311884e-03
#> 74  25 25  1.387484e-01
#> 75  49 25 -3.456813e-01
#> 76   2 26 -9.034753e-04
#> 77  26 26  1.511850e-01
#> 78  50 26 -3.582279e-01
#> 79   3 27 -8.961401e-04
#> 80  27 27  1.477708e-01
#> 81  51 27 -3.548715e-01
#> 82   4 28 -1.147774e-03
#> 83  28 28  1.736025e-01
#> 84  52 28 -3.787655e-01
#> 85   5 29 -1.207952e-03
#> 86  29 29  2.148314e-01
#> 87  53 29 -4.107036e-01
#> 88   6 30 -1.391858e-03
#> 89  30 30  1.224333e-01
#> 90  54 30 -3.277826e-01
#> 91   7 31 -1.270809e-03
#> 92  31 31  1.841406e-01
#> 93  55 31 -3.875967e-01
#> 94   8 32 -1.238147e-03
#> 95  32 32  2.100620e-01
#> 96  56 32 -4.073505e-01
#> 97   9 33 -1.155710e-03
#> 98  33 33  1.967816e-01
#> 99  57 33 -3.975642e-01
#> 100 10 34 -1.064001e-03
#> 101 34 34  2.073189e-01
#> 102 58 34 -4.053846e-01
#> 103 11 35 -8.293441e-04
#> 104 35 35  2.030003e-01
#> 105 59 35 -4.022319e-01
#> 106 12 36 -6.887925e-04
#> 107 36 36  1.576068e-01
#> 108 60 36 -3.643712e-01
#> 109 13 37 -7.860880e-04
#> 110 37 37  2.441764e-01
#> 111 61 37 -4.295971e-01
#> 112 14 38 -1.252142e-03
#> 113 38 38  2.017594e-01
#> 114 62 38 -4.013116e-01
#> 115 15 39 -1.917600e-03
#> 116 39 39  1.898140e-01
#> 117 63 39 -3.921492e-01
#> 118 16 40 -2.825865e-03
#> 119 40 40  1.593052e-01
#> 120 64 40 -3.659495e-01
#> 121 17 41 -3.493069e-03
#> 122 41 41  1.738891e-01
#> 123 65 41 -3.789980e-01
#> 124 18 42 -3.930709e-03
#> 125 42 42  2.177889e-01
#> 126 66 42 -4.127244e-01
#> 127 19 43 -2.126081e-02
#> 128 43 43  4.523577e-04
#> 129 67 43 -3.618862e-04
#> 130 20 44 -1.511505e-02
#> 131 44 44  2.285213e-04
#> 132 68 44  6.529181e-05
#> 133 21 45 -3.319184e-02
#> 134 45 45  1.103330e-03
#> 135 69 45 -6.436091e-04
#> 136 22 46  7.730061e-02
#> 137 46 46  6.134969e-03
#> 138 70 46 -1.104294e-02
#> 139 23 47 -3.219107e-02
#> 140 47 47  1.038422e-03
#> 141 71 47 -1.038422e-03
#> 142 24 48 -2.594571e-03
#> 143 48 48  6.732152e-06
#> 144 72 48  1.750359e-05
#> 145  1 49 -5.265519e-04
#> 146 25 49 -3.456813e-01
#> 147 49 49  8.612536e-01
#> 148  2 50 -3.812964e-04
#> 149 26 50 -3.582279e-01
#> 150 50 50  8.488160e-01
#> 151  3 51 -3.731561e-04
#> 152 27 51 -3.548715e-01
#> 153 51 51  8.522302e-01
#> 154  4 52 -5.260632e-04
#> 155 28 52 -3.787655e-01
#> 156 52 52  8.263991e-01
#> 157  5 53 -6.318516e-04
#> 158 29 53 -4.107036e-01
#> 159 53 53  7.851704e-01
#> 160  6 54 -5.198773e-04
#> 161 30 54 -3.277826e-01
#> 162 54 54  8.775689e-01
#> 163  7 55 -6.037332e-04
#> 164 31 55 -3.875967e-01
#> 165 55 55  8.158614e-01
#> 166  8 56 -6.384804e-04
#> 167 32 56 -4.073505e-01
#> 168 56 56  7.899400e-01
#> 169  9 57 -5.720348e-04
#> 170 33 57 -3.975642e-01
#> 171 57 57  8.032200e-01
#> 172 10 58 -5.441404e-04
#> 173 34 58 -4.053846e-01
#> 174 58 58  7.926825e-01
#> 175 11 59 -4.185556e-04
#> 176 35 59 -4.022319e-01
#> 177 59 59  7.970006e-01
#> 178 12 60 -2.979323e-04
#> 179 36 60 -3.643712e-01
#> 180 60 60  8.423938e-01
#> 181 13 61 -4.467988e-04
#> 182 37 61 -4.295971e-01
#> 183 61 61  7.558244e-01
#> 184 14 62 -6.295083e-04
#> 185 38 62 -4.013116e-01
#> 186 62 62  7.982426e-01
#> 187 15 63 -9.281638e-04
#> 188 39 63 -3.921492e-01
#> 189 63 63  8.101905e-01
#> 190 16 64 -1.230082e-03
#> 191 40 64 -3.659495e-01
#> 192 64 64  8.407043e-01
#> 193 17 65 -1.602529e-03
#> 194 41 65 -3.789980e-01
#> 195 65 65  8.261256e-01
#> 196 18 66 -2.073992e-03
#> 197 42 66 -4.127244e-01
#> 198 66 66  7.822308e-01
#> 199 19 67  1.700865e-02
#> 200 43 67 -3.618862e-04
#> 201 67 67  2.895089e-04
#> 202 20 68 -4.318587e-03
#> 203 44 68  6.529181e-05
#> 204 68 68  1.865480e-05
#> 205 21 69  1.936191e-02
#> 206 45 69 -6.436091e-04
#> 207 69 69  3.754387e-04
#> 208 22 70 -1.391411e-01
#> 209 46 70 -1.104294e-02
#> 210 70 70  1.987730e-02
#> 211 23 71  3.219107e-02
#> 212 47 71 -1.038422e-03
#> 213 71 71  1.038422e-03
#> 214 24 72 -6.745885e-03
#> 215 48 72  1.750359e-05
#> 216 72 72  4.550935e-05
message("writing to ", outf <- tempfile())
#> writing to /tmp/Rtmpy43tGg/file2ff4377fa0608a
write.table(s.CA, file = outf, row.names=FALSE)
## and read it back -- showing off  sparseMatrix():
str(dd <- read.table(outf, header=TRUE))
#> 'data.frame':	216 obs. of  3 variables:
#>  $ i: int  1 25 49 2 26 50 3 27 51 4 ...
#>  $ j: int  1 1 1 2 2 2 3 3 3 4 ...
#>  $ x: num  0.999998 -0.001312 -0.000527 0.999999 -0.000903 ...
## has columns (i, j, x) -> we can use via do.call() as arguments to sparseMatrix():
mm <- do.call(sparseMatrix, dd)
stopifnot(all.equal(mm, CAex, tolerance=1e-15))