[R] Using libRmath.a in Fortran?

Göran Broström gb at stat.umu.se
Thu Sep 6 19:28:04 CEST 2001


On 6 Sep 2001, Peter Dalgaard BSA wrote:

> Göran Broström <gb at stat.umu.se> writes:
>
> > Is it possible to call the routines in the standalone library
> > libRmath from Fortran programs? I'm using g77 on Linux.
>
> Not directly. Look at e.g.
>
> double dweibull(double x, double shape, double scale, int give_log)
>
> this uses call-by-value semantics, where Fortran always uses
> call-by-reference, i.e. pointers. So, notwithstanding linker and
> symbol naming issues, you would at the very least have to write
> wrapper functions to the tune of
>
> double dweibw(double *x, double *shape, double *scale, int *give_log)
> {
>         return dweibull(*x, *shape, *scale, *give_log);
> }
>
>

Thanks to Peter, and also to Douglas Bates, who also gave hints to
how to solve the above-mentioned "naming issues": I came up with
the following program, which (seems to!?) work:

Fortran program (nrand.f) :
---------------------------
      program nrand

      implicit none

      integer first, second
      double precision frnorm, mean, sd, y

      first = 19609
      second = 10198

      call fsetseed(first, second)

      mean = 0.d0
      sd = 1.d0

      y = frnorm(mean, sd)
      write(*, '(/3(a, f6.3))') 'rnorm(', mean, ', ', sd, ') = ', y
C     Just to show that "y is random":
      y = frnorm(mean, sd)
      write(*, '(a, 9x, f6.3/)') 'another value: ', y

      end

And the  C  wrapper (frnorm.c) :
--------------------
#define MATHLIB_STANDALONE
#include <Rmath.h>      /* to define rnorm, etc */
#include <R_ext/RS.h>   /* to define F77_NAME */

double F77_NAME(frnorm)(double *mean, double *sd)
{
  return rnorm(*mean, *sd);
}

void F77_NAME(fsetseed)(int* one, int* two)
{
  set_seed( (unsigned int)(*one), (unsigned int)(*two) );
}

And the Makefile:
-----------------
OBJS = frnorm.o nrand.o

CFLAGS = -Wall -g -I/usr/local/lib/R/include
FFLAGS = -Wall -g -C

LIBS = -lRmath

nrand: $(OBJS)
	g77 -o nrand $(OBJS) $(LIBS)

%.o : %.f
	g77 $(FFLAGS) -c $<

%.o : %.c
	gcc $(CFLAGS) -c $<
--------------------------------------------------------------------

Is this correct? And is this a correct way of accessing the random
number generator? It is the line "A little care is needed to use the
random-number routines..." from the documentation that worries me a
little.

Thanks,

Göran
-- 
 Göran Broström                      tel: +46 90 786 5223
 professor                           fax: +46 90 786 6614
 Department of Statistics            http://www.stat.umu.se/egna/gb/
 Umeå University
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list