build/R3.6.1-win64/RcppArmadillo/tinytest/test_scipy2r.R

## py2r.R: Conversion of SciPy sparse matrix to R
##
## Copyright (C) 2017 - 2019  Binxiang Ni and Dirk Eddelbuettel
##
## This file is part of RcppArmadillo.
##
## RcppArmadillo is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## RcppArmadillo is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with RcppArmadillo.  If not, see <http://www.gnu.org/licenses/>.

## Reference: https://docs.scipy.org/doc/scipy-0.19.1/reference/sparse.html

if (!requireNamespace("Matrix", quietly=TRUE)) exit_file("Package Matrix missing")
if (!requireNamespace("reticulate", quietly=TRUE)) exit_file("Package reticulate missing")

suppressMessages({
    library(Matrix)
    library(reticulate)
})

## SciPy implies NumPy too
if (! py_module_available("scipy")) exit_file("Module scipy missing")

np <- import("numpy")
mat <- np$array(list(list(1, 0, 4), list(0, 0, 5), list(2, 3, 6)))
sp <- import("scipy.sparse")

mtxt <- c("1 0 4",
          "0 0 5",
          "2 3 6")
M <- as.matrix(read.table(text=mtxt))
dimnames(M) <- NULL

## Since 'reticulate' automatically converts CSC matrix to dgCMatrix,
## no need to convert it in RcppArmadillo

#test.csc2dgc <- function() {
csc <- sp$csc_matrix(mat)
dgC <- methods::as(M, "dgCMatrix")
expect_equal(dgC, csc)#, msg="csc2dgc")

#test.coo2dgt <- function() {
coo <- sp$coo_matrix(mat)
dgT <- new("dgTMatrix",
           i = c(0L, 0L, 1L, 2L, 2L, 2L),
           j = c(0L, 2L, 2L, 0L, 1L, 2L),
           x = c(1, 4, 5, 2, 3, 6),
           Dim = c(3L, 3L))
expect_equal(dgT, RcppArmadillo:::.SciPy2R(coo))#, msg="coo2dgt")

#test.csr2dgr <- function() {
csr <- sp$csr_matrix(mat)
dgR <- methods::as(M, "dgRMatrix")
expect_equal(dgR, RcppArmadillo:::.SciPy2R(csr))#, msg="csr2dgr")

#test.other <- function() {
bsr <- sp$bsr_matrix(list(3, 4))
expect_error(RcppArmadillo:::.SciPy2R(bsr))
kylehamilton/MAJOR documentation built on May 27, 2021, 5:48 a.m.