-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
45 lines (35 loc) · 949 Bytes
/
example.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
#include <Rcpp.h>
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
using namespace Rcpp;
using namespace Eigen;
// [[Rcpp::export]]
NumericVector mult_rcpp(NumericVector x, NumericVector y) {
return x * y;
}
// [[Rcpp::export]]
NumericMatrix add_rcpp(NumericMatrix A, NumericMatrix B) {
const Map<MatrixXd> Ae(as<Map<MatrixXd>>(A));
const Map<MatrixXd> Be(as<Map<MatrixXd>>(B));
MatrixXd sum = Ae + Be;
return(wrap(sum));
}
// [[Rcpp::export]]
double edet(NumericMatrix A){
// determinant of the square matrix, as required in statistics
const Map<MatrixXd> Ae(as<Map<MatrixXd>>(A));
MatrixXd prod = Ae*Ae;
return(prod.determinant());
}
// [[Rcpp::export]]
double sum_rcpp(NumericVector xx) {
const Map<VectorXd> x(as<Map<VectorXd>>(xx));
return x.sum();
}
// [[Rcpp::export]]
NumericVector add_rcpp_vec(NumericVector x, NumericVector y) {
return x + y;
}
/*** R
cat("It's finished!")
*/