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
);
}
}
_______________________________________________
Help-gsl mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-gsl