Authors:

Liuhang Zhang (zhanglhjostey@gmail.com)

Yin Zhu (zhuyin.nju@gmail.com)

 

The Standalone RMath library

R (www.r-project.org/) is probably the most widely used statistical software. It has been developed and improved over many years by best programmers and statisticians in related fields. One of the foundations in R is its math library, called RMath. Rmath implements commonly used special functions, e.g. Normal distribution functions, ?norm, in R actually call the corresponding functions in RMath library.

RMath is implemented in the good-old ANSI C. It is fast and it is nearly bug free after being widely tested for many years. It implements functions that are easy to implement in our working language at hand, e.g. the normal distribution function; it also implements functions that are very hard to implement correctly, e.g. the Bessel family functions. Because of its correctness and speed, other program other than R itself may benefit from this math library. Actually there are various methods to call not only RMath but also other R functions from other languages, say Rserve and R.Net. For RMath functions, since they all have nice C interfaces, accessing the R dynamic runtime is probably the best. This is also the basic method used in other higher-level R wrappers, say R.Net.

R’s official documentation, Writing R Extensions, has a section on calling R’s dynamic runtime (R.dll in Windows). However, accessing functions in R.dll requires two initialization functions:

     int Rf_initEmbeddedR(int ac, char **av);
     void Rf_endEmbeddedR(int fatal);

They seem to be annoying.

Fortunately, the dynamic library of RMath can be compiled in a standalone DLL. R Installation and Administration has the full guidance for how to do this. As make-from-the-source is never the tradition in Windows platform, we have done the job and released the dynamic runtime library (DLL) of RMath (both 32bit and 64bit) here.

 

RMath.Net

The story ends at the successful compilation of the library if we only use C/C++ to access RMath. However, our observation is that it is .Net programmers who need a great library for some statistical functions, while C/C++ programmers have more choices besides RMath. So most of our work in this project has been focused on writing a C# wrapper for RMath native library so that .Net languages can access RMath.

The wrapper is pretty easy (just a brunch of simple functions) to use and as far as we can say it is thread-safe. Please read the detailed documentation and the example C# program in the release for more information.

 

The following picture shows how to use this library in an F# script file.

 

image

Last edited Apr 22, 2012 at 1:45 PM by yinz, version 6