`library(jmatrix)`

The package `jmatrix`

(Domingo (2022a)) was originally conceived as a tool for other packages, namely `parallelpam`

(Domingo (2022b)) and `scellpam`

(Domingo (2022c)) which needed to deal with very big matrices which might not fit in the memory of the computer, particularly if their elements are of type `double`

(in most modern machines, 8 bytes per element) whereas they could fit if they were matrices of other data types, in particular of floats (4 bytes per element).

Unfortunately, R is not a strongly typed language. Double is the default type in R and it is not easy to work with other data types. Trials like the package float (Schmidt (2022)) have been done, but to use them you have to coerce a matrix already loaded in R memory to a float matrix, and then you can delete it. But, what happens if you computer has not memory enough to hold the matrix in the first place?. This is the problem this package tries to address.

Our idea is to use the disk as temporarily storage of the matrix in a file with a internal binary format (`jmatrix`

format). This format has a header of 128 bytes with information like type of matrix (full, sparse or symmetric), data type of each element (char, short, int, long, float, double or long double), number of rows and columns and endianness; then comes the content as binary data (in sparse matrices zeros are not stored; in symmetric matrices only the lower-diagonal is stored) and finally the metadata (currently, names for rows/columns if needed and an optional comment).

Such files are created and loaded by functions written in C++ which are accessible from `R`

with Rcpp (Eddelbuettel and François (2011)). The file, once loaded, uses strictly the needed memory for its data type and can be processed by other C++ functions (like the PAM algorithm or any other numeric library written in C++) also from inside `R`

.

The matrix contained in a binary data file in `jmatrix`

format cannot be loaded directly in `R`

memory as a `R`

matrix (that would be impossible, anyway, since precisely this package is done for the cases in which such matrix would NOT fit into the available RAM). Nevertheless, limited access through some functions is provided to read one or more rows or one or more columns as `R`

vectors or matrices (obviously, coerced to double).

The package `jmatrix`

must not be considered as a final, finished software. Currently is mostly an instrumental solution to address our needs and we make available as a separate package just in case it could be useful for anyone else.

First of all, the package can show quite informative (but sometimes verbose) messages in the console. To turn on/off such messages you can use.

```
JMatrixSetDebug(TRUE)
#> Debugging for jmatrix package set to ON.
# Initially, state of debug is FALSE.
```

As stated before, the binary matrix files should normally be created from C++ getting the data from an external source like a data file in a format used in bioinformatics or a .csv file. These files should be read by chunks. As an example, look at function `CsvToJMat`

in package `scellpam`

.

As a convenience and only for testing purposes (to be used in this vignette), we provide the function `JWriteBin`

to write a R matrix as a `jmatrix`

file.

```
# Create a 6x8 matrix of random values
<- matrix(runif(48),nrow=6)
Rf # Set row and column names for it
rownames(Rf) <- c("A","B","C","D","E","F")
colnames(Rf) <- c("a","b","c","d","e","f","g","h")
# Let's see the matrix
Rf#> a b c d e f g
#> A 0.8916781 0.3185691 0.9033186 0.930160851 0.6899760 0.8950029 0.2372844
#> B 0.1381585 0.4894842 0.4590783 0.665864334 0.5622801 0.7316456 0.2372027
#> C 0.5650152 0.9025299 0.6415166 0.928167599 0.3457743 0.0759326 0.9449799
#> D 0.3665981 0.3869559 0.2221751 0.094421162 0.2934609 0.4085376 0.8705160
#> E 0.1084294 0.4253530 0.4477779 0.001356042 0.7782980 0.1304857 0.5046894
#> F 0.9800564 0.9055526 0.5005171 0.244410256 0.1138576 0.4649350 0.9962090
#> h
#> A 0.8546113
#> B 0.9088691
#> C 0.1691040
#> D 0.6831350
#> E 0.3412551
#> F 0.6763569
# and write it as the binary file Rfullfloat.bin
JWriteBin(Rf,"Rfullfloat.bin",dtype="float",dmtype="full",
comment="Full matrix of floats")
#> The passed matrix has row names for the 6 rows and they will be used.
#> The passed matrix has column names for the 8 columns and they will be used.
#> Writing binary matrix Rfullfloat.bin of (6x8)
#> End of block of binary data at offset 320
#> Writing row names (6 strings written, from A to F).
#> Writing column names (8 strings written, from a to h).
#> Writing comment: Full matrix of floats
# Also, you can write it with double data type:
JWriteBin(Rf,"Rfulldouble.bin",dtype="double",dmtype="full",
comment="Full matrix of doubles")
#> The passed matrix has row names for the 6 rows and they will be used.
#> The passed matrix has column names for the 8 columns and they will be used.
#> Writing binary matrix Rfulldouble.bin of (6x8)
#> End of block of binary data at offset 512
#> Writing row names (6 strings written, from A to F).
#> Writing column names (8 strings written, from a to h).
#> Writing comment: Full matrix of doubles
```

To get information about the stored file the function `JMatInfo`

is provided. Of course, this funcion does not read the complete file in memory but just the header.

```
# Information about the float binary file
JMatInfo("Rfullfloat.bin")
#> File: Rfullfloat.bin
#> Matrix type: FullMatrix
#> Number of elements: 48
#> Data type: float
#> Endianness: little endian (same as this machine)
#> Number of rows: 6
#> Number of columns: 8
#> Metadata: Stored names of rows and columns.
#> Metadata comment: "Full matrix of floats"
# Same information about the double binary file
JMatInfo("Rfulldouble.bin")
#> File: Rfulldouble.bin
#> Matrix type: FullMatrix
#> Number of elements: 48
#> Data type: double
#> Endianness: little endian (same as this machine)
#> Number of rows: 6
#> Number of columns: 8
#> Metadata: Stored names of rows and columns.
#> Metadata comment: "Full matrix of doubles"
```

As stated before, no function is provided to read the whole matrix in memory which would contradict the philosophy of this package, but you can get rows or columns from a file.

```
# Reads row 1 into vector vf. Float values inside the file are
# promoted to double.
<-GetJRow("Rfullfloat.bin",1))
(vf#> a b c d e f g h
#> 0.8916781 0.3185691 0.9033186 0.9301609 0.6899760 0.8950028 0.2372843 0.8546113
```

Obviously, storage in float provokes a loosing of precision. We have observed this not to be relevant for `PAM`

(partitioning around medoids) algorihm but it can be important in other cases. It is the price to pay for halving the needed space.

```
# Checks the precision lost
max(abs(Rf[1,]-vf))
#> [1] 2.956949e-08
```

Nevertheless, storing as double obviously keeps the data intact.

```
<-GetJRow("Rfulldouble.bin",1)
vdmax(abs(Rf[1,]-vd))
#> [1] 0
```

Now, let us see examples of some functions to read rows or columns by number or by name, or to read several rows/columns as a R matrix. In all examples numbers for rows and columns are in R-convention (i.e. starting at 1)

```
# Read column number 3
<-GetJCol("Rfullfloat.bin",3))
(vf#> A B C D E F
#> 0.9033186 0.4590783 0.6415166 0.2221751 0.4477780 0.5005171
# Test precision
max(abs(Rf[,3]-vf))
#> [1] 2.537854e-08
# Read row with name C
<-GetJRowByName("Rfullfloat.bin","C"))
(vf#> a b c d e f g h
#> 0.5650151 0.9025299 0.6415166 0.9281676 0.3457743 0.0759326 0.9449799 0.1691041
# Read column with name c
<-GetJColByName("Rfullfloat.bin","c"))
(vf#> A B C D E F
#> 0.9033186 0.4590783 0.6415166 0.2221751 0.4477780 0.5005171
# Get the names of all rows or columns as vectors of R strings
<-GetJRowNames("Rfullfloat.bin"))
(rn#> [1] "A" "B" "C" "D" "E" "F"
<-GetJColNames("Rfullfloat.bin"))
(cn#> [1] "a" "b" "c" "d" "e" "f" "g" "h"
# Get the names of rows and columns simultaneosuly as a list of two elements
<-GetJNames("Rfullfloat.bin"))
(l#> $rownames
#> [1] "A" "B" "C" "D" "E" "F"
#>
#> $colnames
#> [1] "a" "b" "c" "d" "e" "f" "g" "h"
# Get several rows at once. The returned matrix has the rows in the
# same order as the passed list,
# and this list can contain even repeated values
<-GetJManyRows("Rfullfloat.bin",c(1,4)))
(vm#> a b c d e f g
#> A 0.8916781 0.3185691 0.9033186 0.93016088 0.6899760 0.8950028 0.2372843
#> D 0.3665981 0.3869559 0.2221751 0.09442116 0.2934609 0.4085376 0.8705160
#> h
#> A 0.8546113
#> D 0.6831350
# Of course, columns can be extrated equally
<-GetJManyCols("Rfulldouble.bin",c(1,4)))
(vc#> a d
#> A 0.8916781 0.930160851
#> B 0.1381585 0.665864334
#> C 0.5650152 0.928167599
#> D 0.3665981 0.094421162
#> E 0.1084294 0.001356042
#> F 0.9800564 0.244410256
# and similar functions are provided for extracting by names:
<-GetJManyRowsByNames("Rfulldouble.bin",c("A","D")))
(vm#> a b c d e f g
#> A 0.8916781 0.3185691 0.9033186 0.93016085 0.6899760 0.8950029 0.2372844
#> D 0.3665981 0.3869559 0.2221751 0.09442116 0.2934609 0.4085376 0.8705160
#> h
#> A 0.8546113
#> D 0.6831350
<-GetJManyColsByNames("Rfulldouble.bin",c("a","d")))
(vc#> a d
#> A 0.8916781 0.930160851
#> B 0.1381585 0.665864334
#> C 0.5650152 0.928167599
#> D 0.3665981 0.094421162
#> E 0.1084294 0.001356042
#> F 0.9800564 0.244410256
```

The package can manage and store sparse and symmetric matrices, too.

```
# Generation of a 6x8 sparse matrix
<- matrix(rep(0,48),nrow=6)
Rsp <- 0.1
sparsity <- round(48*sparsity)
nnz <- floor(47*runif(nnz))
where <- runif(nnz)
val for (i in 1:nnz)
{floor(where[i]/8)+1,(where[i]%%8)+1] <- val[i]
Rsp[
}rownames(Rsp) <- c("A","B","C","D","E","F")
colnames(Rsp) <- c("a","b","c","d","e","f","g","h")
# Let's see the matrix
Rsp#> a b c d e f g h
#> A 0 0 0 0.0000000 0.0000000 0.0000000 0 0
#> B 0 0 0 0.0000000 0.0000000 0.0000000 0 0
#> C 0 0 0 0.4933078 0.0000000 0.0000000 0 0
#> D 0 0 0 0.0000000 0.8571928 0.0000000 0 0
#> E 0 0 0 0.0000000 0.0000000 0.7329517 0 0
#> F 0 0 0 0.4325866 0.0000000 0.0000000 0 0
# Write the matrix as sparse with type float
JWriteBin(Rsp,"Rspafloat.bin",dtype="float",dmtype="sparse",
comment="Sparse matrix of floats")
#> The passed matrix has row names for the 6 rows and they will be used.
#> The passed matrix has column names for the 8 columns and they will be used.
#> Writing binary matrix Rspafloat.bin of (6x8)
#> End of block of binary data at offset 184
#> Writing row names (6 strings written, from A to F).
#> Writing column names (8 strings written, from a to h).
#> Writing comment: Sparse matrix of floats
```

Notice that the condition of being a sparse matrix and the storage space used can be known with the matrix info.

```
JMatInfo("Rspafloat.bin")
#> File: Rspafloat.bin
#> Matrix type: SparseMatrix
#> Number of elements: 48
#> Data type: float
#> Endianness: little endian (same as this machine)
#> Number of rows: 6
#> Number of columns: 8
#> Metadata: Stored names of rows and columns.
#> Metadata comment: "Sparse matrix of floats"
#> Binary data size: 56 bytes, which is 29.1667% of the full matrix size.
```

Be careful: trying to store as sparse a matrix which is not (it has not a majority of 0-entries) works, but produces a matrix larger than the corresponding full matrix.

With respect to symmetric matrices, `JWriteBin`

works the same way. Let us generate a \(7 \times 7\) symmetric matrix.

```
<- matrix(runif(49),nrow=7)
Rns <- 0.5*(Rns+t(Rns))
Rsym rownames(Rsym) <- c("A","B","C","D","E","F","G")
colnames(Rsym) <- c("a","b","c","d","e","f","g")
# Let's see the matrix
Rsym#> a b c d e f g
#> A 0.4886096 0.5816990 0.2954847 0.5870229 0.299098786 0.6982338 0.8262601
#> B 0.5816990 0.5964974 0.8637848 0.2615010 0.229130659 0.1168863 0.3678568
#> C 0.2954847 0.8637848 0.2408428 0.2827592 0.520818837 0.3671106 0.5024719
#> D 0.5870229 0.2615010 0.2827592 0.4207262 0.179653616 0.7700122 0.1586397
#> E 0.2990988 0.2291307 0.5208188 0.1796536 0.002806714 0.5712779 0.5998378
#> F 0.6982338 0.1168863 0.3671106 0.7700122 0.571277853 0.6747386 0.5570667
#> G 0.8262601 0.3678568 0.5024719 0.1586397 0.599837752 0.5570667 0.1173063
# Write the matrix as symmetric with type float
JWriteBin(Rsym,"Rsymfloat.bin",dtype="float",dmtype="symmetric",
comment="Symmetric matrix of floats")
#> The passed matrix has row names for the 7 rows and they will be used.
#> Writing binary matrix Rsymfloat.bin
#> End of block of binary data at offset 240
#> Writing row names (7 strings written, from A to G).
#> Writing comment: Symmetric matrix of floats
# Get the information
JMatInfo("Rsymfloat.bin")
#> File: Rsymfloat.bin
#> Matrix type: SymmetricMatrix
#> Number of elements: 49 (28 really stored)
#> Data type: float
#> Endianness: little endian (same as this machine)
#> Number of rows: 7
#> Number of columns: 7
#> Metadata: Stored only names of rows.
#> Metadata comment: "Symmetric matrix of floats"
```

Notice that if you store a R matrix which is NOT symmetric as a symmetric `jmatrix`

, only the lower triangular part (including the main diagonal) will be saved. The upper-triangular part will be lost.

The functions to read rows/colums stated before works equally independently of the matrix character (full, sparse or symmetric) so you can play with them using the `Rspafloat.bin`

and `Rsymfloat.bin`

file to check they work.

Finally, there is a function to get the lower-diagonal part of a symmetric matrix stored in a `jmatrix`

file as a R vector, ordered by columns (i.e.: column 1 from M(2,1) to M(n,1), followed by column 2 from M(3,2) to M(n,2) and so on, up to M(n,n-1) so d is a vector of \(n(n-1)/2\) components). This is to get the data of a dissimilarity/distance matrix in the format used by the Partitioning around medoids (PAM) algorithm implemented in the `cluster`

(Maechler et al. (2022)) package. The purpose is to compare such implementation with our own implementation of PAM. If you are interested, see the documentation of the `parallelpam`

(Domingo (2022b)) package.

```
<-GetSubdiag("Rsymfloat.bin"))
(d#> [1] 0.5816990 0.2954848 0.5870229 0.2990988 0.6982338 0.8262601 0.8637848
#> [8] 0.2615010 0.2291307 0.1168863 0.3678568 0.2827592 0.5208188 0.3671106
#> [15] 0.5024719 0.1796536 0.7700122 0.1586397 0.5712779 0.5998378 0.5570667
```

Domingo, Juan. 2022a. *Jmatrix: Read from/Write in Disks Matrices with Any Data Type in a Binary Format*.

———. 2022b. *Parallelpam: Applies the Partitioning-Around-Medoids (PAM) Clustering Algortihm to Big Sets of Data Using Parallel Implementation, If Several Cores Are Available*.

———. 2022c. *Scellpam: Applying Partitioning Around Medoids to Single Cell with High Number of Cells*.

Eddelbuettel, Dirk, and Romain François. 2011. “Rcpp: Seamless R and C++ Integration.” *Journal of Statistical Software* 40 (8): 1–18. https://doi.org/10.18637/jss.v040.i08.

Maechler, Martin, Peter Rousseeuw, Anja Struyf, Mia Hubert, and Kurt Hornik. 2022. *Cluster: Cluster Analysis Basics and Extensions*. https://CRAN.R-project.org/package=cluster.

Schmidt, Drew. 2022. “float: 32-Bit Floats.” https://cran.r-project.org/package=float.