rcpp

Rcpp How to generate random multivariate normal vector in Rcpp?

吃可爱长大的小学妹 提交于 2019-12-23 06:42:10
问题 I would like to generate some large random multivariate (more than 6 dimensions) normal samples. In R, many packages can do this such as rmnorm, rmvn... But the problem is the speed! So I tried to write some C code through Rcpp. I went through some tutorial online but it seems there is no "sugar" for multivariate distribution, neither in STL library. Any help is appreciated! Thanks! 回答1: I'm not sure that Rcpp will help unless you find a good algorithm to approximate your multivariate

Rcpp How to generate random multivariate normal vector in Rcpp?

半世苍凉 提交于 2019-12-23 06:42:01
问题 I would like to generate some large random multivariate (more than 6 dimensions) normal samples. In R, many packages can do this such as rmnorm, rmvn... But the problem is the speed! So I tried to write some C code through Rcpp. I went through some tutorial online but it seems there is no "sugar" for multivariate distribution, neither in STL library. Any help is appreciated! Thanks! 回答1: I'm not sure that Rcpp will help unless you find a good algorithm to approximate your multivariate

Creating a Templated Function to Fill a Vector with another depending on Size

若如初见. 提交于 2019-12-23 04:26:54
问题 Is there a base function in Rcpp that: Fills entirely by a single value if size of a vector is 1. Fills the other vector completely if same length. Fills with an NA value if neither Vector are the same length nor a vector is of size 1. I've written the above criteria as a function below using a NumericVector as an example. If there isn't a base function in Rcpp that performs said operations there should be a way to template the function so that given any type of vector (e.g. numeric ,

Rcpp: Eliminating a column and a row from a matrix [duplicate]

喜你入骨 提交于 2019-12-23 03:16:38
问题 This question already has answers here : Rcpp NumericMatrix - how to erase a row / column? (2 answers) Closed 4 years ago . I am trying to create a function that takes a matrix, n x p , and an index, e , and returns the sub-matrix obtained by eliminating the e-th column and the e-th row from X. I thought that the easiest thing was to create an n -1 x p - 1 matrix and insert the corners around the cross formed by the e-th row and column in it. The same approach works fine with EigenRcpp using

Convert individual Rcpp::IntegerVector element to a character

做~自己de王妃 提交于 2019-12-23 02:42:42
问题 I have to convert individual elements of Rcpp::IntegerVector into their string form so I can add another string to them. My code looks like this: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] Rcpp::String int_to_char_single_fun(int x){ // Obtain environment containing function Rcpp::Environment base("package:base"); // Make function callable from C++ Rcpp::Function int_to_string = base["as.character"]; // Call the function and receive its list output Rcpp::String res = int_to

Rcpp fast statistical mode function with vector input of any type

纵然是瞬间 提交于 2019-12-22 18:37:09
问题 I'm trying to build a super fast mode function for R to use for aggregating large categorical datasets. The function should take vector input of all supported R types and return the mode. I have read This post, This Help-page and others, but I was not able to make the function take in all R data types. My code now works for numeric vectors, I am relying on Rcpp sugar wrapper functions: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] int Mode(NumericVector x, bool narm = false) {

R: C symbol name not in load table - error in linking with external .c files

半城伤御伤魂 提交于 2019-12-22 18:33:52
问题 I am trying to use the Rsundials package to solve a system of Ordinary Differential Equations (ODEs) using SUNDIALS. First, I am trying to run the example in the manual. Rsundials can be used to solve the ODEs when the right hand side of the ODEs is described in a C file. First, the ODEs are described in a C file (example program from the package) #include "include/nvector/nvector_serial.h" #include "include/sundials/sundials_dense.h" #define Ith(v,i) ( NV_DATA_S(v)[i - 1] ) int rhs(realtype

Keep XPtr for multiple sessions

吃可爱长大的小学妹 提交于 2019-12-22 18:25:24
问题 I have a R function that creates a Primebase Cpp Class and then returns a XPtr<Primebase> pointer. As the construction process takes a significant amount of time I'd like to save the instance of Primebase to my session, so that the next time I open up R I can directly access the Primebase instance. Unfortunately the underlying Object gets deleted as soon as I close R and the XPtr turns into a null pointer . Is there a way to prevent R from deleting the object or any other way to save the

Use function defined in one cpp file in function defined in another cpp file in Rcpp

强颜欢笑 提交于 2019-12-22 15:54:24
问题 I have a c++ function called add defined in file add.cpp (content of add.cpp below): #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double add(double a, double b) { return a + b; } I have another c++ function called multiplyandadd defined in multiplyandadd.cpp file (content of multiplyandadd.cpp below) that calls add function from above: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double multiplyandadd(double a, double b, double c) { return c*add(a, b); } In order

Exposing a C struct with Rcpp?

寵の児 提交于 2019-12-22 14:59:36
问题 I am looking for advice on how to write a wrapper package for the GLFW C library using Rcpp. I have already started, and I've managed to wrap a few simple functions from the GLFW library. Now I was looking at the function glfwCreateWindow : GLFWwindow* glfwCreateWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share) As can be seen from its prototype, it accepts these arguments that are pointers to structs. Because this function needs to be used by the user,