[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-gsl] Gsl-1.8 compiled and running under MSYS-1.10+second order
From: |
Rene Girard |
Subject: |
Re: [Help-gsl] Gsl-1.8 compiled and running under MSYS-1.10+second order derivative |
Date: |
Sun, 5 Nov 2006 19:34:31 -0500 (EST) |
Brian,
I did as you recommended i.e. I tried
f_x.function = (double (*) (double, void *)) &g_x;
and
f_x.function = &g_x;
but I got the following when I compiled the
program given below:
rm -f ./t_dn2.o
rm -f ./t_dn2
gcc -O2 -Wall -ansi -I/usr/local/include -c -o t_dn2.o t_dn2.c
t_dn2.c: In function g_x:
t_dn2.c:61: warning: dereferencing void * pointer
t_dn2.c:61: error: void value not ignored as it ought to be
make: *** [t_dn2.o] Error 1
If instead of
- - - - - - - program follows - - - - - - - -
/*
* Programme t_dn2.c: Le programme t_dn2.c donne une methode d'utilisation
* des fonctions de la librarie GSL pour le calcul de la
* derivee partielle du premier ordre d'une fonction a deux
variables
* z = f(x,y) par rapport a x. Dans ce programme on
considere
* la fonction test:
*
* f(x,y) = (sin(pi*x/2)*sin(pi*y/2))^2
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_deriv.h>
double g_x(double, void *);
int main(void)
{
gsl_function f_x;
const double pi = 4.0*atan(1.0);
double ro,r1,dx,er1,x,y;
double p1[2];
int i;
y = 0.65;
dx = 0.1;
p1[0] = 1.0e-04;
p1[1] = y;
/* f_x.function = (double (*) ()) &g_x; */
f_x.function = &g_x;
f_x.params = &p1[0];
x = -dx;
printf("f(x,y) = (sin(pi*x/2)*sin(pi*y/2))^2\n");
for(i = 1; i <= 11; i++)
{
x += dx;
gsl_deriv_central(&f_x,x,p1[0],&r1,&er1);
ro = pi*(sin(pi*p1[1]/2.0)*sin(pi*p1[1]/2.0))*
cos(pi*x/2.0)*sin(pi*x/2.0);
printf("Pour x = %11.6f et y = %5.2f\n",x,y);
printf("f_x = %14.10f +/- %14.10f\n",r1,er1);
printf("Valeur exacte = %14.10f\n",ro);
}
return 0;
} /* fin du programme t_dn2.c */
double g_x(double x, void *params)
{
const double pi = 4.0*atan(1.0);
double yo,y1;
yo = (sin(pi*x/2.0)*sin(pi*params[1]/2));
y1 = yo*yo;
return y1;
}
- - - - - - - - - - - - - - - - - - - - - - -
If I go back to the verison that worked and put the cast
(double (*)(double, double *))
in the following statement
f_x.function = (double (*) (double, double *)) &g_x;
I get this
t_dn2.c: In function main:
t_dn2.c:35: warning: assignment from incompatible pointer type
gcc -o ./t_dn2 ./t_dn2.o -I/usr/local/include -L/usr/local/lib -s
-lgsl -lgslcblas -lm
Again if I revert to the cast
f_x.function = (double (*) ( )) &g_x;
No problem! It compiles and gives the correct result.
Now I am wondering if I am not understanding correctly the purpose and usage
of the argument "void *params" which for me is to be able to pass parameters to
the function used to evaluate the numerical derivative.
This use of the argument "void *params" allows me to calculate cross derivative
of a 2 variables function: d_xy f(x,y). I tried it and it works.
I wish to be able to follow your recommendation but as you can see there is
something perhaps that I am not understanding.
Note: I use gcc 4.1.1 to compile the program
Regards
Rene
Brian Gough <address@hidden> wrote: Rene Girard wrote:
> double g1(double, double *);
>
> I would like to bring to your attention that when I
> compile that simple example and
> I set the pointer to the function from we want to have
> the derivative with the following
> statement:
>
> f.function = &g1; (like in the example in Chap. 27)
>
> I get the following warning:
>
> "warning: assignment from incompatible pointer type"
>
> to remove the warning I had to cast the pointer as
> follow:
>
> f.function = (double (*) ()) &g1;
Hello,
Thanks for your email. I think your warning can be avoided without a
cast by using the prototype double g1(double, void *) which matches the
definition in GSL. Otherwise, the appropriate cast would be (double
(*)(double, void *)). See the header file gsl_math.h for the definition
of the gsl_function type.
--
best regards,
Brian Gough
(GSL Maintainer)
Network Theory Ltd,
Publishing the GSL Manual - http://www.network-theory.co.uk/gsl/manual/
---------------------------------
The best gets better. See why everyone is raving about the All-new Yahoo! Mail.