help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] Numerical Gradient Function


From: David Doria
Subject: Re: [Help-gsl] Numerical Gradient Function
Date: Fri, 14 Mar 2008 10:28:39 -0400

Haha I knew someone was going to say that!  I'm not an ace c++ programmer,
but I thought you can't do this:
double VarList[v->size]
because v->size is not defined at compile time?

Also, I just found OOL which has this function as well as a numerical
hessian function as well as a constrained minimization function set!  The
GSL guys should definitely include OOL in the next release (or at least
mention it somewhere haha)!

Thanks,
Dave

On Fri, Mar 14, 2008 at 10:25 AM, ddneilson <address@hidden> wrote:

>
> Hey there David,
>  Speed-wise, you're probably going to want to replace the
> "vector<double> VarList" with just a "double VarList[v->size]" or even
> just scrap the VarList thing entirely and just call gsl_vector_get(v,
> i). The vector's going to be slower (element-accesses will be slower,
> plus the calls to the constructor, destructor, and memory allocator),
> and take up more memory (arguably the memory's not a big deal on the
> stack) than either option.
>
> -Daniel
>
> David Doria wrote:
> > The only thing I saw in the GSL reference manual was a single
> dimensional
> > function numerical derivative.  I thought this may be helpful to some
> folks
> > who have a multidimensional function that cannot be differentiated.
> >
> > void NumericalGradient(const gsl_vector *v, void *params, gsl_vector
> *df)
> > {
> >     double CurrentF = my_f(v, params);
> >     int NumVars = int(v->size);
> >     vector<double> VarList;
> >     gsl_vector *temp;
> >     temp = gsl_vector_alloc (NumVars);
> >     double epsilon=1e-6;
> >
> >     //get initial values
> >     for (int i=0; i < NumVars ; i++)
> >     {
> >         VarList.push_back(gsl_vector_get(v, i));
> >     }
> >
> >     for (int counter=0; counter < NumVars ; counter++)
> >     {
> >         for(int j=0; j< NumVars; j++)
> >         {
> >             gsl_vector_set(temp, j, VarList[j]);
> >         }
> >         gsl_vector_set(temp,counter,gsl_vector_get(temp,counter) +
> epsilon);
> >
> >         gsl_vector_set(df, counter, (my_f(temp, params) -
>  CurrentF)/epsilon
> > );
> >     }
> >
> > }
> >
> >
>
>


-- 
Thanks,

David


reply via email to

[Prev in Thread] Current Thread [Next in Thread]