/
eigen_sqrt.cpp
50 lines (37 loc) · 1.11 KB
/
eigen_sqrt.cpp
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
#include <Rcpp.h>
#include <Eigen/Eigenvalues>
using namespace Rcpp;
using namespace Eigen;
// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//
// [[Rcpp::export]]
Eigen::MatrixXd rcppeigen_sqrt(const Eigen::Map<Eigen::MatrixXd> & A){
SelfAdjointEigenSolver<MatrixXd> es(A);
MatrixXd sqrtA = es.operatorSqrt();
return sqrtA;
}
// [[Rcpp::export]]
Eigen::MatrixXd rcppeigen_invsqrt(const Eigen::Map<Eigen::MatrixXd> & A){
SelfAdjointEigenSolver<MatrixXd> es(A);
MatrixXd invsqrtA = es.operatorInverseSqrt();
return invsqrtA;
}
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
//
/*** R
timesTwo(42)
rcppeigen_sqrt(diag(c(2, 2)))
*/