Skip to content

Commit 3f822bb

Browse files
authored
Merge pull request #81 from RcppCore/feature/tinytest
Feature/tinytest
2 parents 754b880 + 526c173 commit 3f822bb

25 files changed

+909
-1088
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ debian
88
^\.Rproj\.user$
99
.*\.tar\.gz$
1010
^patches
11+
^.editorconfig$

.editorconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
# Matches multiple files with brace expansion notation
13+
# 4 space indentation
14+
[*.{c,cpp,h,hpp,R,r}]
15+
indent_style = space
16+
indent_size = 4
17+
18+
# Tab indentation (no size specified)
19+
[Makefile]
20+
indent_style = tab
21+

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ before_install:
1111
- ./run.sh bootstrap
1212

1313
install:
14-
- ./run.sh install_aptget r-cran-rcpp r-cran-matrix r-cran-inline r-cran-runit r-cran-pkgkitten r-cran-microbenchmark
14+
- ./run.sh install_aptget r-cran-rcpp r-cran-matrix r-cran-inline r-cran-tinytest r-cran-pkgkitten r-cran-microbenchmark
1515

1616
script:
1717
- ./run.sh run_tests

ChangeLog

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
2019-11-01 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* R/unit.test.R (compile_unit_tests): Removed as no longer needed
4+
5+
2019-10-31 Dirk Eddelbuettel <edd@debian.org>
6+
7+
* inst/tinytest/test_transform.R: Switch from RUnit to tinytest
8+
* inst/tinytest/test_wrap.R: Idem
9+
10+
* inst/tinytest/cpp/transform.cpp: Added using Rcpp Attributes
11+
* inst/tinytest/cpp/wrap.cpp: Idem
12+
13+
* inst/tinytest/cpp/sparse.cpp: More idiomatic Eigen code
14+
* inst/tinytest/test_sparse.R: Idem
15+
16+
* .editorconfig: Added
17+
18+
2019-10-30 Dirk Eddelbuettel <edd@debian.org>
19+
20+
* DESCRIPTION (Suggests): Switch from RUnit to tinytest
21+
* .travis.yml (install): Ditto
22+
23+
* tests/tinytest.R: Converted to tinytest
24+
25+
* test_sparse.R: Converted to tinytest
26+
* cpp/sparse.cpp: Added using Rcpp Attributes
27+
28+
2019-10-29 Dirk Eddelbuettel <edd@debian.org>
29+
30+
* test_fastLm.R: Converted to tinytest
31+
* test_RcppEigen.R: Idem
32+
* test_solution.R: Idem
33+
34+
* cpp/rcppeigen.cpp: Added using Rcpp Attributes
35+
* cpp/solution.cpp: Idem
36+
37+
2019-10-28 Dirk Eddelbuettel <edd@debian.org>
38+
39+
* tests/tinytest.R: Renamed from tests/doRUnit.R
40+
* inst/tinytest/test_fastLm.R: Renamed from inst/unitTests/runit*
41+
* inst/tinytest/test_RcppEigen.R: Idem
42+
* inst/tinytest/test_solution.R: Idem
43+
* inst/tinytest/test_sparse.R: Idem
44+
* inst/tinytest/test_transform.R: Idem
45+
* inst/tinytest/test_wrap.R: Idem
46+
147
2019-10-13 Dirk Eddelbuettel <edd@debian.org>
248

349
* README.md: Added CRAN + BioConductor badges for reverse depends,

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ Depends: R (>= 2.15.1)
2525
LazyLoad: yes
2626
LinkingTo: Rcpp
2727
Imports: Matrix (>= 1.1-0), Rcpp (>= 0.11.0), stats, utils
28-
Suggests: inline, RUnit, pkgKitten, microbenchmark
28+
Suggests: inline, tinytest, pkgKitten, microbenchmark
2929
URL: http://dirk.eddelbuettel.com/code/rcpp.eigen.html
3030
BugReports: https://github.com/RcppCore/RcppEigen/issues

R/unit.test.R

Lines changed: 0 additions & 27 deletions
This file was deleted.

inst/tinytest/cpp/rcppeigen.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
#include <RcppEigen.h>
3+
4+
// [[Rcpp::depends(RcppEigen)]]
5+
6+
// [[Rcpp::export]]
7+
Rcpp::List fx() {
8+
Rcpp::List vecs = Rcpp::List::create(
9+
Rcpp::_["Vec<complex>"] = Eigen::VectorXcd::Zero(5),
10+
Rcpp::_["Vec<double>"] = Eigen::VectorXd::Zero(5),
11+
Rcpp::_["Vec<float>"] = Eigen::VectorXf::Zero(5),
12+
Rcpp::_["Vec<int>"] = Eigen::VectorXi::Zero(5)
13+
);
14+
15+
// A VectorX<T> behaves as a matrix with one column but is converted to
16+
// a vector object in R, not a matrix of one column. The distinction is
17+
// that VectorX<T> objects are defined at compile time to have one column,
18+
// whereas a MatrixX<T> has a dynamic number of columns that is set to 1
19+
// during execution of the code. A MatrixX<T> object can be resized to have
20+
// a different number of columns. A VectorX<T> object cannot.
21+
Rcpp::List cols = Rcpp::List::create(
22+
Rcpp::_["Col<complex>"] = Eigen::MatrixXcd::Zero(5, 1),
23+
Rcpp::_["Col<double>"] = Eigen::MatrixXd::Zero(5, 1),
24+
Rcpp::_["Col<float>"] = Eigen::MatrixXf::Zero(5, 1),
25+
Rcpp::_["Col<int>"] = Eigen::MatrixXi::Zero(5, 1)
26+
);
27+
28+
Rcpp::List rows = Rcpp::List::create(
29+
Rcpp::_["Row<complex>"] = Eigen::RowVectorXcd::Zero(5),
30+
Rcpp::_["Row<double>"] = Eigen::RowVectorXd::Zero(5),
31+
Rcpp::_["Row<float>"] = Eigen::RowVectorXf::Zero(5),
32+
Rcpp::_["Row<int>"] = Eigen::RowVectorXi::Zero(5)
33+
);
34+
35+
Rcpp::List matrices = Rcpp::List::create(
36+
Rcpp::_["Mat<complex>"] = Eigen::MatrixXcd::Identity(3, 3),
37+
Rcpp::_["Mat<double>"] = Eigen::MatrixXd::Identity(3, 3),
38+
Rcpp::_["Mat<float>"] = Eigen::MatrixXf::Identity(3, 3),
39+
Rcpp::_["Mat<int>"] = Eigen::MatrixXi::Identity(3, 3)
40+
);
41+
42+
// ArrayXX<t> objects have the same structure as matrices but allow
43+
// componentwise arithmetic. A * B is matrix multiplication for
44+
// matrices and componentwise multiplication for arrays.
45+
Rcpp::List arrays2 = Rcpp::List::create(
46+
Rcpp::_["Arr2<complex>"] = Eigen::ArrayXXcd::Zero(3, 3),
47+
Rcpp::_["Arr2<double>"] = Eigen::ArrayXXd::Zero(3, 3),
48+
Rcpp::_["Arr2<float>"] = Eigen::ArrayXXf::Zero(3, 3),
49+
Rcpp::_["Arr2<int>"] = Eigen::ArrayXXi::Zero(3, 3)
50+
);
51+
52+
// ArrayX<t> objects have the same structure as VectorX<T> objects
53+
// but allow componentwise arithmetic, including functions like exp, log,
54+
// sqrt, ...
55+
Rcpp::List arrays1 = Rcpp::List::create(
56+
Rcpp::_["Arr1<complex>"] = Eigen::ArrayXcd::Zero(5),
57+
Rcpp::_["Arr1<double>"] = Eigen::ArrayXd::Zero(5),
58+
Rcpp::_["Arr1<float>"] = Eigen::ArrayXf::Zero(5),
59+
Rcpp::_["Arr1<int>"] = Eigen::ArrayXi::Zero(5)
60+
);
61+
62+
Rcpp::List operations = Rcpp::List::create(
63+
Rcpp::_["Op_seq"] = Eigen::ArrayXd::LinSpaced(6, 1, 10), // arguments are length.out, start, end
64+
Rcpp::_["Op_log"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).log(),
65+
Rcpp::_["Op_exp"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).exp(),
66+
Rcpp::_["Op_sqrt"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).sqrt(),
67+
Rcpp::_["Op_cos"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).cos()
68+
);
69+
70+
Rcpp::List output = Rcpp::List::create(
71+
Rcpp::_["vectors : VectorX<T>"] = vecs,
72+
Rcpp::_["matrices : MatrixX<T>"] = matrices,
73+
Rcpp::_["rows : RowVectorX<T>"] = rows,
74+
Rcpp::_["columns : MatrixX<T>"] = cols,
75+
Rcpp::_["arrays2d : ArrayXX<T>"] = arrays2,
76+
Rcpp::_["arrays1d : ArrayX<T>"] = arrays1,
77+
Rcpp::_["operations : ArrayXd"] = operations
78+
);
79+
80+
return output ;
81+
}
82+
83+
// [[Rcpp::export]]
84+
Rcpp::List fx2(Rcpp::List input) {
85+
Eigen::VectorXi m1 = input[0] ; /* implicit as */
86+
Eigen::VectorXd m2 = input[1] ; /* implicit as */
87+
Eigen::Matrix<unsigned int, Eigen::Dynamic, 1> m3 = input[0] ; /* implicit as */
88+
Eigen::VectorXf m4 = input[1] ; /* implicit as */
89+
90+
Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum(), m3.sum(), m4.sum());
91+
92+
return res ;
93+
}
94+
95+
96+
// [[Rcpp::export]]
97+
Rcpp::List fx3(Rcpp::List input) {
98+
99+
const Eigen::Map<Eigen::VectorXi> m1 = input[0] ; // maps share storage and do not allow conversion
100+
const Eigen::Map<Eigen::VectorXd> m2 = input[1] ;
101+
102+
Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());
103+
104+
return res ;
105+
}
106+
107+
// [[Rcpp::export]]
108+
Rcpp::List fx4(Rcpp::List input) {
109+
110+
const Eigen::Map<Eigen::RowVectorXi> m1 = input[0] ; // maps share storage, do not allow conversion
111+
const Eigen::Map<Eigen::RowVectorXd> m2 = input[1] ;
112+
113+
Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());
114+
115+
return res ;
116+
}
117+
118+
119+
// [[Rcpp::export]]
120+
Rcpp::List fx5(Rcpp::List input) {
121+
const Eigen::Map<Eigen::MatrixXi> m1 = input[0]; // maps share storage, do not allow conversion
122+
const Eigen::Map<Eigen::MatrixXd> m2 = input[1] ;
123+
// FIXME: Write a version of as specifically for complex matrices.
124+
// const Eigen::Map<Eigen::MatrixXcd> m3 = input[2] ;
125+
126+
Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());//, m3.sum());
127+
128+
return res ;
129+
}
130+
131+
132+
// [[Rcpp::export]]
133+
Rcpp::List fx6(Rcpp::List input) {
134+
const Eigen::MappedSparseMatrix<double> m1 = input[0]; // maps share storage and do not allow conversion
135+
136+
Rcpp::List res = Rcpp::List::create(Rcpp::_["nnz"] = double(m1.nonZeros()),
137+
Rcpp::_["nr"] = double(m1.rows()),
138+
Rcpp::_["nc"] = double(m1.cols()),
139+
Rcpp::_["inSz"] = double(m1.innerSize()),
140+
Rcpp::_["outSz"] = double(m1.outerSize()),
141+
Rcpp::_["sum"] = m1.sum());
142+
143+
return res ;
144+
}
145+
146+
147+
// [[Rcpp::export]]
148+
Rcpp::List fx7(Rcpp::List input) {
149+
const Eigen::SparseMatrix<double> m1 = input[0];
150+
Rcpp::List res = Rcpp::List::create(Rcpp::_["nnz"] = double(m1.nonZeros()),
151+
Rcpp::_["nr"] = double(m1.rows()),
152+
Rcpp::_["nc"] = double(m1.cols()),
153+
Rcpp::_["inSz"] = double(m1.innerSize()),
154+
Rcpp::_["outSz"] = double(m1.outerSize()),
155+
Rcpp::_["sum"] = m1.sum());
156+
return res ;
157+
}

inst/tinytest/cpp/solution.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#include <RcppEigen.h>
3+
4+
// [[Rcpp::depends(RcppEigen)]]
5+
6+
typedef Eigen::ArrayXd Ar1;
7+
typedef Eigen::Map<Ar1> MAr1;
8+
typedef Eigen::ArrayXXd Ar2;
9+
typedef Eigen::Map<Ar2> MAr2;
10+
typedef Eigen::MatrixXd Mat;
11+
typedef Eigen::Map<Mat> MMat;
12+
typedef Eigen::VectorXd Vec;
13+
typedef Eigen::Map<Vec> MVec;
14+
typedef Eigen::PartialPivLU<Mat> PPLU;
15+
typedef Eigen::ColPivHouseholderQR<Mat> CPQR;
16+
17+
18+
// [[Rcpp::export]]
19+
Rcpp::List dense_PPLU(MMat A, MVec b) {
20+
PPLU lu(A);
21+
Mat Ainv(lu.inverse());
22+
Vec x(lu.solve(b));
23+
24+
return Rcpp::List::create(Rcpp::Named("A", A),
25+
Rcpp::Named("Ainv", Ainv),
26+
Rcpp::Named("b", b),
27+
Rcpp::Named("x", x));
28+
}
29+
30+
// [[Rcpp::export]]
31+
Rcpp::List dense_CPQR(MMat A, MVec b) {
32+
CPQR qr(A);
33+
Mat Ainv(qr.inverse());
34+
Vec x(qr.solve(b));
35+
return Rcpp::List::create(Rcpp::Named("Ainv", Ainv),
36+
Rcpp::Named("x", x));
37+
}

0 commit comments

Comments
 (0)