Skip to content

Feature/tinytest #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ debian
^\.Rproj\.user$
.*\.tar\.gz$
^patches
^.editorconfig$
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# Matches multiple files with brace expansion notation
# 4 space indentation
[*.{c,cpp,h,hpp,R,r}]
indent_style = space
indent_size = 4

# Tab indentation (no size specified)
[Makefile]
indent_style = tab

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ before_install:
- ./run.sh bootstrap

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

script:
- ./run.sh run_tests
Expand Down
46 changes: 46 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
2019-11-01 Dirk Eddelbuettel <edd@debian.org>

* R/unit.test.R (compile_unit_tests): Removed as no longer needed

2019-10-31 Dirk Eddelbuettel <edd@debian.org>

* inst/tinytest/test_transform.R: Switch from RUnit to tinytest
* inst/tinytest/test_wrap.R: Idem

* inst/tinytest/cpp/transform.cpp: Added using Rcpp Attributes
* inst/tinytest/cpp/wrap.cpp: Idem

* inst/tinytest/cpp/sparse.cpp: More idiomatic Eigen code
* inst/tinytest/test_sparse.R: Idem

* .editorconfig: Added

2019-10-30 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Suggests): Switch from RUnit to tinytest
* .travis.yml (install): Ditto

* tests/tinytest.R: Converted to tinytest

* test_sparse.R: Converted to tinytest
* cpp/sparse.cpp: Added using Rcpp Attributes

2019-10-29 Dirk Eddelbuettel <edd@debian.org>

* test_fastLm.R: Converted to tinytest
* test_RcppEigen.R: Idem
* test_solution.R: Idem

* cpp/rcppeigen.cpp: Added using Rcpp Attributes
* cpp/solution.cpp: Idem

2019-10-28 Dirk Eddelbuettel <edd@debian.org>

* tests/tinytest.R: Renamed from tests/doRUnit.R
* inst/tinytest/test_fastLm.R: Renamed from inst/unitTests/runit*
* inst/tinytest/test_RcppEigen.R: Idem
* inst/tinytest/test_solution.R: Idem
* inst/tinytest/test_sparse.R: Idem
* inst/tinytest/test_transform.R: Idem
* inst/tinytest/test_wrap.R: Idem

2019-10-13 Dirk Eddelbuettel <edd@debian.org>

* README.md: Added CRAN + BioConductor badges for reverse depends,
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Depends: R (>= 2.15.1)
LazyLoad: yes
LinkingTo: Rcpp
Imports: Matrix (>= 1.1-0), Rcpp (>= 0.11.0), stats, utils
Suggests: inline, RUnit, pkgKitten, microbenchmark
Suggests: inline, tinytest, pkgKitten, microbenchmark
URL: http://dirk.eddelbuettel.com/code/rcpp.eigen.html
BugReports: https://github.com/RcppCore/RcppEigen/issues
27 changes: 0 additions & 27 deletions R/unit.test.R

This file was deleted.

157 changes: 157 additions & 0 deletions inst/tinytest/cpp/rcppeigen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

#include <RcppEigen.h>

// [[Rcpp::depends(RcppEigen)]]

// [[Rcpp::export]]
Rcpp::List fx() {
Rcpp::List vecs = Rcpp::List::create(
Rcpp::_["Vec<complex>"] = Eigen::VectorXcd::Zero(5),
Rcpp::_["Vec<double>"] = Eigen::VectorXd::Zero(5),
Rcpp::_["Vec<float>"] = Eigen::VectorXf::Zero(5),
Rcpp::_["Vec<int>"] = Eigen::VectorXi::Zero(5)
);

// A VectorX<T> behaves as a matrix with one column but is converted to
// a vector object in R, not a matrix of one column. The distinction is
// that VectorX<T> objects are defined at compile time to have one column,
// whereas a MatrixX<T> has a dynamic number of columns that is set to 1
// during execution of the code. A MatrixX<T> object can be resized to have
// a different number of columns. A VectorX<T> object cannot.
Rcpp::List cols = Rcpp::List::create(
Rcpp::_["Col<complex>"] = Eigen::MatrixXcd::Zero(5, 1),
Rcpp::_["Col<double>"] = Eigen::MatrixXd::Zero(5, 1),
Rcpp::_["Col<float>"] = Eigen::MatrixXf::Zero(5, 1),
Rcpp::_["Col<int>"] = Eigen::MatrixXi::Zero(5, 1)
);

Rcpp::List rows = Rcpp::List::create(
Rcpp::_["Row<complex>"] = Eigen::RowVectorXcd::Zero(5),
Rcpp::_["Row<double>"] = Eigen::RowVectorXd::Zero(5),
Rcpp::_["Row<float>"] = Eigen::RowVectorXf::Zero(5),
Rcpp::_["Row<int>"] = Eigen::RowVectorXi::Zero(5)
);

Rcpp::List matrices = Rcpp::List::create(
Rcpp::_["Mat<complex>"] = Eigen::MatrixXcd::Identity(3, 3),
Rcpp::_["Mat<double>"] = Eigen::MatrixXd::Identity(3, 3),
Rcpp::_["Mat<float>"] = Eigen::MatrixXf::Identity(3, 3),
Rcpp::_["Mat<int>"] = Eigen::MatrixXi::Identity(3, 3)
);

// ArrayXX<t> objects have the same structure as matrices but allow
// componentwise arithmetic. A * B is matrix multiplication for
// matrices and componentwise multiplication for arrays.
Rcpp::List arrays2 = Rcpp::List::create(
Rcpp::_["Arr2<complex>"] = Eigen::ArrayXXcd::Zero(3, 3),
Rcpp::_["Arr2<double>"] = Eigen::ArrayXXd::Zero(3, 3),
Rcpp::_["Arr2<float>"] = Eigen::ArrayXXf::Zero(3, 3),
Rcpp::_["Arr2<int>"] = Eigen::ArrayXXi::Zero(3, 3)
);

// ArrayX<t> objects have the same structure as VectorX<T> objects
// but allow componentwise arithmetic, including functions like exp, log,
// sqrt, ...
Rcpp::List arrays1 = Rcpp::List::create(
Rcpp::_["Arr1<complex>"] = Eigen::ArrayXcd::Zero(5),
Rcpp::_["Arr1<double>"] = Eigen::ArrayXd::Zero(5),
Rcpp::_["Arr1<float>"] = Eigen::ArrayXf::Zero(5),
Rcpp::_["Arr1<int>"] = Eigen::ArrayXi::Zero(5)
);

Rcpp::List operations = Rcpp::List::create(
Rcpp::_["Op_seq"] = Eigen::ArrayXd::LinSpaced(6, 1, 10), // arguments are length.out, start, end
Rcpp::_["Op_log"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).log(),
Rcpp::_["Op_exp"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).exp(),
Rcpp::_["Op_sqrt"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).sqrt(),
Rcpp::_["Op_cos"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).cos()
);

Rcpp::List output = Rcpp::List::create(
Rcpp::_["vectors : VectorX<T>"] = vecs,
Rcpp::_["matrices : MatrixX<T>"] = matrices,
Rcpp::_["rows : RowVectorX<T>"] = rows,
Rcpp::_["columns : MatrixX<T>"] = cols,
Rcpp::_["arrays2d : ArrayXX<T>"] = arrays2,
Rcpp::_["arrays1d : ArrayX<T>"] = arrays1,
Rcpp::_["operations : ArrayXd"] = operations
);

return output ;
}

// [[Rcpp::export]]
Rcpp::List fx2(Rcpp::List input) {
Eigen::VectorXi m1 = input[0] ; /* implicit as */
Eigen::VectorXd m2 = input[1] ; /* implicit as */
Eigen::Matrix<unsigned int, Eigen::Dynamic, 1> m3 = input[0] ; /* implicit as */
Eigen::VectorXf m4 = input[1] ; /* implicit as */

Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum(), m3.sum(), m4.sum());

return res ;
}


// [[Rcpp::export]]
Rcpp::List fx3(Rcpp::List input) {

const Eigen::Map<Eigen::VectorXi> m1 = input[0] ; // maps share storage and do not allow conversion
const Eigen::Map<Eigen::VectorXd> m2 = input[1] ;

Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());

return res ;
}

// [[Rcpp::export]]
Rcpp::List fx4(Rcpp::List input) {

const Eigen::Map<Eigen::RowVectorXi> m1 = input[0] ; // maps share storage, do not allow conversion
const Eigen::Map<Eigen::RowVectorXd> m2 = input[1] ;

Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());

return res ;
}


// [[Rcpp::export]]
Rcpp::List fx5(Rcpp::List input) {
const Eigen::Map<Eigen::MatrixXi> m1 = input[0]; // maps share storage, do not allow conversion
const Eigen::Map<Eigen::MatrixXd> m2 = input[1] ;
// FIXME: Write a version of as specifically for complex matrices.
// const Eigen::Map<Eigen::MatrixXcd> m3 = input[2] ;

Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());//, m3.sum());

return res ;
}


// [[Rcpp::export]]
Rcpp::List fx6(Rcpp::List input) {
const Eigen::MappedSparseMatrix<double> m1 = input[0]; // maps share storage and do not allow conversion

Rcpp::List res = Rcpp::List::create(Rcpp::_["nnz"] = double(m1.nonZeros()),
Rcpp::_["nr"] = double(m1.rows()),
Rcpp::_["nc"] = double(m1.cols()),
Rcpp::_["inSz"] = double(m1.innerSize()),
Rcpp::_["outSz"] = double(m1.outerSize()),
Rcpp::_["sum"] = m1.sum());

return res ;
}


// [[Rcpp::export]]
Rcpp::List fx7(Rcpp::List input) {
const Eigen::SparseMatrix<double> m1 = input[0];
Rcpp::List res = Rcpp::List::create(Rcpp::_["nnz"] = double(m1.nonZeros()),
Rcpp::_["nr"] = double(m1.rows()),
Rcpp::_["nc"] = double(m1.cols()),
Rcpp::_["inSz"] = double(m1.innerSize()),
Rcpp::_["outSz"] = double(m1.outerSize()),
Rcpp::_["sum"] = m1.sum());
return res ;
}
37 changes: 37 additions & 0 deletions inst/tinytest/cpp/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

#include <RcppEigen.h>

// [[Rcpp::depends(RcppEigen)]]

typedef Eigen::ArrayXd Ar1;
typedef Eigen::Map<Ar1> MAr1;
typedef Eigen::ArrayXXd Ar2;
typedef Eigen::Map<Ar2> MAr2;
typedef Eigen::MatrixXd Mat;
typedef Eigen::Map<Mat> MMat;
typedef Eigen::VectorXd Vec;
typedef Eigen::Map<Vec> MVec;
typedef Eigen::PartialPivLU<Mat> PPLU;
typedef Eigen::ColPivHouseholderQR<Mat> CPQR;


// [[Rcpp::export]]
Rcpp::List dense_PPLU(MMat A, MVec b) {
PPLU lu(A);
Mat Ainv(lu.inverse());
Vec x(lu.solve(b));

return Rcpp::List::create(Rcpp::Named("A", A),
Rcpp::Named("Ainv", Ainv),
Rcpp::Named("b", b),
Rcpp::Named("x", x));
}

// [[Rcpp::export]]
Rcpp::List dense_CPQR(MMat A, MVec b) {
CPQR qr(A);
Mat Ainv(qr.inverse());
Vec x(qr.solve(b));
return Rcpp::List::create(Rcpp::Named("Ainv", Ainv),
Rcpp::Named("x", x));
}
Loading