Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include nmath.h?

Tags:

r

I would like to include the header nmath.h for my C code (within an R package) to find R_FINITE and ML_ERR_return_NAN. I found that one cannot include nmath.h directly. For R_FINITE to be found, I could include R_ext/libextern.h. But I don't know what to include so that ML_ERR_return_NAN is found. Any ideas? I found here that Prof. Brian Ripley referred to Writing R Extensions, but I couldn't find nmath.h being addressed there (where exactly?)

like image 313
Marius Hofert Avatar asked May 29 '26 15:05

Marius Hofert


1 Answers

On Debian or Ubuntu:

 sudo apt-get install r-mathlib

after which you can build test programs such as this:

// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; 
//          compile-command: "gcc -s -Wall -O3 \
//               -I/usr/share/R/include -o rmath_rnorm \
//               rmath_rnorm.c -lRmath -lm" -*-

// Compare to 
//    $ Rscript -e "RNGkind('Marsaglia'); \
//                 .Random.seed[2:3] <- c(123L, 456L); rnorm(2)"
//    [1] -0.2934974 -0.3343770

#include <stdio.h>

#define MATHLIB_STANDALONE 1
#include <Rmath.h>

int main(void) {

    set_seed(123, 456);
    printf("rnorm: %f %f\n", rnorm(0.0, 1.0), rnorm(0.0, 1.0));

    return 0;
}

Note: The first four lines should be one-line in the file you safe, then M-x compile build the program for your. Ditto for the Rscript invocation: one line.

Edit: Drats. Answered the wrong question :) nmath.h appears to not be exported from src/nmath/nmath.h but this R Mathlibrary is what is exported by R Core for use by others. Where as the nmath.h file has

/* Private header file for use during compilation of Mathlib */
#ifndef MATHLIB_PRIVATE_H
#define MATHLIB_PRIVATE_H

so you are not supposed to rely on it.

like image 149
Dirk Eddelbuettel Avatar answered Jun 01 '26 07:06

Dirk Eddelbuettel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!