help-gsl
[Top][All Lists]
Advanced

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

Re: [Help-gsl] calling a gsl_function from a structure of functions


From: Benjamin Blumer
Subject: Re: [Help-gsl] calling a gsl_function from a structure of functions
Date: Tue, 02 Aug 2011 14:55:29 -0600
User-agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20110627 Thunderbird/5.0

It shouldn't be a problem. gsl_function, which is defined in gsl_math.h, is a structure containing a pointer to a function that takes a double, and a pointer to a structure that contains the parameters. Structures are allowed to contain pointers to functions - although not functions themselves.

I chopped an example out of something I've been working on:

    #include <stdio.h>
    #include <stdlib.h>
    #include <gsl/gsl_math.h>

    typedef struct
    {
        double Constant;
    } parameters;


    typedef struct
    {
        gsl_function addConstant;
        gsl_function subtractConstant;
    } structureOfFunctions;

    double function1 (double x, void * params)
    {
        parameters * f1params = (parameters*) params;
        double C = (f1params->Constant);
        return x + C;
    }
    double function2 (double x, void * params)
    {

        parameters * f2params = (parameters*) params;
        double C = (f2params->Constant);
        return x - C;
    }



    int main()
    {
        structureOfFunctions firstGroupOfFunctions;

        parameters first = {5};
        firstGroupOfFunctions.addConstant.function = &function1;
        firstGroupOfFunctions.addConstant.params = &first;

        parameters second = {2};
        firstGroupOfFunctions.subtractConstant.function = &function2;
        firstGroupOfFunctions.subtractConstant.params = &second;

printf("\n%lf", GSL_FN_EVAL(&firstGroupOfFunctions.addConstant, 10)); printf("\n%lf", GSL_FN_EVAL(&firstGroupOfFunctions.subtractConstant, 10));
        printf("\n");
        return 0;
    }


On 11-08-02 01:19 PM, Laura Florescu wrote:
Hi, I have a structure
field_integrand fld_integrand(double kz, void *p) in which I am
computing a few functions and returning them in a structure. Then I
want to integrate those functions so I have a function field
fld(double r, double z) in which I'd like to integrate those
functions, and it hasn't really worked out to declare them in this fld
function.

Does anybody know if I can declare gsl_functions from a structure of functions?

Thanks a lot,
Laura

_______________________________________________
Help-gsl mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-gsl






reply via email to

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