forked from RcppCore/RcppEigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sparse.R
More file actions
136 lines (108 loc) · 4.78 KB
/
test_sparse.R
File metadata and controls
136 lines (108 loc) · 4.78 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/r -t
#
# Copyright (C) 2011 Douglas Bates, Dirk Eddelbuettel and Romain Francois
#
# This file is part of RcppEigen
#
# RcppEigen 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.
#
# RcppEigen 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 RcppEigen. If not, see <http://www.gnu.org/licenses/>.
Rcpp::sourceCpp("cpp/sparse.cpp")
library(Matrix)
#test.wrapSparse.double.R <- function(){
res <- wrapSparseDouble()
rr <- Matrix::t(as(gl(3,3), "sparseMatrix"))
colnames(rr) <- NULL
expect_equal(res, rr, info = "wrap<SparseMatrix<double> >")
#test.wrapSparse.double.ColMajor.R <- function(){
res <- wrapSparseDoubleColumnMajor()
rr <- Matrix::t(as(gl(3,3), "sparseMatrix"))
colnames(rr) <- NULL
expect_equal(res, rr, info = "wrap<SparseMatrix<double, Eigen::ColMajor> >")
## test.wrapSparse.int.ColMajor.R <- function(){ ## classes not yet exported from Matrix
## fx <- cxxfunction( , '
## Eigen::SparseMatrix<int, Eigen::ColMajor> mm(9,3);
## mm.reserve(9);
## for (int j = 0; j < 3; ++j) {
## mm.startVec(j);
## for (int i = 3 * j; i < 3 * (j + 1); ++i)
## mm.insertBack(i, j) = 1;
## }
## mm.finalize();
## return wrap(mm);
## ' , plugin = "RcppEigen" )
## #res <- fx()
## #rr <- Matrix::t(as(gl(3,3), "sparseMatrix"))
## #colnames(rr) <- NULL
## #expect_equal( res, rr, info = "wrap<SparseMatrix<double, Eigen::ColMajor> >")
## checkException( fx(), info = "wrap<SparseMatrix<int, Eigen::ColMajor> >" )
## }
#test.wrapSparse.double.RowMajor.R <- function(){
res <- wrapSparseDoubleRowMajor()
rr <- new( "dgRMatrix", j=rep(0L:2L, each=3), p=0L:9L, x=as.numeric(9:1), Dim=c(9L,3L) )
colnames(rr) <- NULL
expect_equal( res, rr, info = "wrap<SparseMatrix<double, Eigen::RowMajor> >")
## test.wrapSparse.int.RowMajor.R <- function(){
## fx <- cxxfunction( , '
## Eigen::SparseMatrix<int, Eigen::RowMajor> mm(9,3);
## mm.reserve(9);
## for (int irow = 0; irow < 9; ++irow) {
## mm.startVec(irow);
## mm.insertBack(irow, irow / 3) = 9 - irow;
## }
## mm.finalize();
## return wrap(mm);
## ' , plugin = "RcppEigen" )
## #res <- fx()
## #rr <- new( "igRMatrix", j=rep(0L:2L, each=3), p=0L:9L, x=9L:1L, Dim=c(9L,3L) )
## #colnames(rr) <- NULL
## #expect_equal( res, rr, info = "wrap<SparseMatrix<int, Eigen::RowMajor> >")
## checkException( fx(), info = "wrap<SparseMatrix<int, Eigen::RowMajor> >" )
## }
#test.asSparse.double.ColMajor.R <- function(){
rr <- Matrix::t(as(gl(3,3), "sparseMatrix"))
colnames(rr) <- NULL
res <- asSparseDoubleColumnMajor( rr )
expect_equal( res, rr, info = "as<SparseMatrix<double, Eigen::ColMajor> >")
#test.asMappedSparse.double.ColMajor.R <- function(){
rr <- Matrix::t(as(gl(3,3), "sparseMatrix"))
colnames(rr) <- NULL
res <- asMappedSparseDoubleColMajor( rr )
expect_equal( res, sum(rr), info = "as<Map<SparseMatrix<double, Eigen::ColMajor> > >")
#test.asMappedSparse.deprecated.double.ColMajor.R <- function(){
fx <- asMappedSparseDeprecatedDoubleColMajor
rr <- Matrix::t(as(gl(3,3), "sparseMatrix"))
colnames(rr) <- NULL
res <- fx( rr )
expect_equal( res, sum(rr), info = "as<MappedSparseMatrix<double, Eigen::ColMajor> >")
#test.asSparse.double.RowMajor.R <- function(){
rr <- new( "dgRMatrix", j=rep(0L:2L, each=3), p=0L:9L, x=as.numeric(9:1), Dim=c(9L,3L) )
colnames(rr) <- NULL
res <- asSparseDoubleRowMajor( rr )
expect_equal( res, sum(rr), info = "as<SparseMatrix<double, Eigen::RowMajor> >")
#test.asMappedSparse.double.RowMajor.R <- function(){
rr <- new( "dgRMatrix", j=rep(0L:2L, each=3), p=0L:9L, x=as.numeric(9:1), Dim=c(9L,3L) )
colnames(rr) <- NULL
res <- asMappedSparseDoubleRowMajor( rr )
expect_equal( res, sum(rr), info = "as<Map<SparseMatrix<double, Eigen::RowMajor> > >")
#test.asMappedSparse.deprecated.double.RowMajor.R <- function(){
rr <- new( "dgRMatrix", j=rep(0L:2L, each=3), p=0L:9L, x=as.numeric(9:1), Dim=c(9L,3L) )
colnames(rr) <- NULL
res <- asMappedSparseDeprecatedDoubleRowMajor( rr )
expect_equal( res, sum(rr), info = "as<MappedSparseMatrix<double, Eigen::RowMajor> >")
# test.sparseCholesky.R <- function() {
suppressMessages(require("Matrix", character.only=TRUE))
data("KNex", package = "Matrix")
rr <- sparseCholesky(KNex)
expect_equal(rr[[1]],
as.vector(solve(crossprod(KNex[[1]]), crossprod(KNex[[1]], KNex[[2]])), mode="numeric"),
info = "Cholmod solution")