I am a full-time consultant and provide services related to the design, implementation and deployment of mathematical programming, optimization and data-science applications. I also teach courses and workshops. Usually I cannot blog about projects I am doing, but there are many technical notes I'd like to share. Not in the least so I have an easy way to search and find them again myself. You can reach me at erwin@amsterdamoptimization.com.
In some recent projects, I was working on using algorithms implemented in C++ from R and Python. Basically the idea is: Python and R are great languages for scripting but they are slow as molasses. So, it may make sense to develop the time consuming algorithms in C++ while driving the algorithm from R or Python.
R and C++: Rcpp.
The standard way to build interfaces between R and C++ code is to use Rcpp [1,2].
It is possible to directly interface R with low-level C code, This will require a lot of code and knowledge of R internals. Rcpp automates a lot of this. E.g. Rcpp will take care of translating an R vector into a C++ vector.
Rcpp supports both small fragments of C++ code passed on as an R string to more coarse-grained file based approach [3]. For Windows, you need to download the GNU compilers [4].
If you are new to both Rcpp and to building your own R packages [5] things may be a bit overwhelming.
Rstudio can help a lot. It supports a lot of very useful tasks:
Syntax coloring for C++ code.
Building projects.
Git version control.
Documentation tools (rmarkdown and bookdown). My documentation is also a good test: it executes almost all of the code when building the document.
Editing C++ code in RStudio
Basically I never have to leave RStudio.
I have added an alternative driver file for my C++ code so I can debug it in Visual Studio. I used it only a few times: most of the time I just used RStudio.
Python and C++: pybind11
pybind11 [6] is in many respects similar to Rcpp, although it requires a little bit more programming to bridge the gap between Python and C++.
In the beginning of the above Youtube video [7], the presenter compares pybind11 with some of the alternatives:
SWIG: author of SWIG says: don't use it
ctypes: call c functions but not c++
CFFI: call c functions
Boost.Python: support for older C++ standards, but not much maintained
pybind11: modern
As with Rcpp, calling the compiler is done through running a build or setup script. For Rcpp I used the GNU compilers, while pybind11/pip install supports the Visual Studio C++ compiler. This also means that if you have little experience with both pybind11 and creating packages, the learning curve may be steep.