#define NRANSI
#include "nrutil.h"
#include <iostream.h>
#define TOL 2.0e-4

int ncom;
double *pcom,*xicom,(*nrfunc)(double []);

void dlinmin(double p[], double xi[], int n, double *fret, double (*func)(double []))
{
	double dbrent(double ax, double bx, double cx,
		double (*f)(double), double tol, double *xmin);
	double df1dim(double x);
	void dmnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
		double *fc, double (*func)(double));
	int j;
	double xx,xmin,fx,fb,fa,bx,ax;

	ncom=n;
	pcom=dvector(1,n);
	xicom=dvector(1,n);
	nrfunc=func;
	for (j=1;j<=n;j++) {
		pcom[j]=p[j];
		xicom[j]=xi[j];
	}
	ax=0.0;
	xx=1.0;
	dmnbrak(&ax,&xx,&bx,&fa,&fx,&fb,df1dim);
//	printf("ax: %10.6f   xx: %10.6f\n",ax,xx);
	*fret=dbrent(ax,xx,bx,df1dim,TOL,&xmin);
	for (j=1;j<=n;j++) {
		xi[j] *= xmin;
		p[j] += xi[j];
	}
//	printf("in dlinmin, xmin: %20.12f, fret: %20.12f and check: %20.12f\n",xmin, *fret, (*func)(p));
//	for(j=1;j<=n;j++) printf("%6.0f",p[j]*100000); printf("\n");
	free_dvector(xicom,1,n);
	free_dvector(pcom,1,n);
}
#undef TOL
#undef NRANSI
/* (C) Copr. 1986-92 Numerical Recipes Software 21].,t45316. */
