From dfaden at gmail.com  Fri Apr 14 19:30:49 2006
From: dfaden at gmail.com (David Faden)
Date: Fri, 14 Apr 2006 12:30:49 -0500
Subject: [Rd] bus error on calling nmmin
In-Reply-To: <Pine.LNX.4.64.0604140727520.20826@gannet.stats.ox.ac.uk>
References: <6730abff0604131547s1e774aadgf91fa93ea0ee8205@mail.gmail.com>
	<Pine.LNX.4.64.0604140727520.20826@gannet.stats.ox.ac.uk>
Message-ID: <6730abff0604141030k70aba079j3e9b67cfd1c6608d@mail.gmail.com>

2006/4/14, Prof Brian Ripley <ripley at stats.ox.ac.uk>:
> You appear to be trying to call nmmin from a standalone program.
> You linked against -lR, but failed to initialize R, hence the segfault.
>
> nmmin is not a part of R that is made available except to a running R
> process: it is documented in `Writing R Extensions' for use in R packages.

Thanks. Initializing R does seem to have fixed the problem. In this, I
have followed the code in tests/Embedding, in particular
embeddedRCall.c. I was led to this code by Section 7.1 of "Writing R
Extensions."

Below is the corrected code and an example session:

#include <R_ext/Applic.h>
#include <stdio.h>

double parabola(int n, double *par, void *ex) {
  double xm = par[0] - 1;
  return xm * xm + 13;
}

/*
 * Copied from tests/Embedded/embeddedRCall.c:
 */
extern int Rf_initEmbeddedR(int argc, char *argv[]);

int main()
{
  char *argv[]= {"nmminDemo", "--gui=none", "--silent"};
  const int argc = 3;

  double initial[1] = {1.5};
  double result[1];
  double value;
  int convergenceCode;
  /*
   * The following values are based on the help
   * page for optim.
   */
  const double abstol = 1e-16;
  const double reltol = 1e-8;
  const double alpha = 1.0; /* reflection factor */
  const double beta = 0.5; /* contraction factor */
  const double gamm = 2.0; /* expansion factor */
  const int trace = 0; /* tracing on */
  int fncount;
  const int maxit = 10000;

  Rf_initEmbeddedR(argc, argv);

  nmmin(1, initial, result, &value, parabola,
	&convergenceCode, abstol, reltol,
	NULL, alpha, beta, gamm,
	trace, &fncount, maxit);
  printf("fncount: %d\n", fncount);
  printf("convergence code: %d\n", convergenceCode);
  printf("min of %f at x = %f\n", value, result[0]);
  return 0;
}

----

$ gcc nmminDemo.c -g
-I/Library/Frameworks/R.framework/Versions/2.1.1/Resources/include
-L/Library/Frameworks/R.framework/Versions/2.1.1/Resources/lib -lR
$ R CMD ./a.out
fncount: 24
convergence code: 0
min of 13.000000 at x = 1.000195

--
David Faden, dfaden at iastate.edu
AIM: pitulx



[Rd] bus error on calling nmmin
David Faden