472,782 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Easy question about redefining a function

I have a C question, which looks very easy, but no one here seems to
know an easy answer.

I have a function "powell" (from Numerical Recipes) which takes an
argument of the type

"double (*f)(float[])"

But I want to be able to pass a

"double (*f)(float[], double)"

instead, where minimization is with respect to the the first argument
and
the second argument is a parameter.

Below is a very easy version of the problem.

// Problem: Minimize function with additional parameters

// function you want to minimize, example below is trivial
// and 1 dimensional

double g(float x[],double a)
{ return (x[1]+a)*(x[1]+a); }

// so minimization should result in x[1]=-a.
int main (void)
{
//define other parameters required for the function powell
int n=1;
float *p,**xi;
p=vector(1,n);
p[1]=0.2;
xi=matrix(1,n,1,n);
xi[1][1]=1;
float ftol=0.000001;
int iter=0;
float fret;

double a=(double) rand();

//How to pass the parameter a into the minimization function powell.
//While the parameter a is defined during the running time.
//i.e., one likes to enter g(.,a) in te function

powell(p,xi,n,ftol,&iter,&fret, );
//where the last argument should be a function of the required form

}

Any help is appreciated.

Martin

Jul 6 '06 #1
6 2226
In article <11*********************@m38g2000cwc.googlegroups. com>,
Martin Bootsma <ma***********@gmail.comwrote:
>
I have a function "powell" (from Numerical Recipes) which takes an
argument of the type

"double (*f)(float[])"

But I want to be able to pass a

"double (*f)(float[], double)"
.... for example:
>double g(float x[],double a)
{ return (x[1]+a)*(x[1]+a); }
Change powell() to make it accept a parameter which it
will pass to the function f.

Therefore, let:

typedef double (*ObjFunc)(float[], double *);

and change powell's declaration to:

int powell(whaterver, ObjFunc f, double param);

Then within the definition of powell(), change all calls to
f(x) to f(x, param).

Now you can call powell() as:

powell(whatever, g, 3.14);

--
Rouben Rostamian
Jul 6 '06 #2
Martin Bootsma wrote:
I have a C question, which looks very easy, but no one here seems to
know an easy answer.

I have a function "powell" (from Numerical Recipes) which takes an
argument of the type

"double (*f)(float[])"

But I want to be able to pass a

"double (*f)(float[], double)"

instead, where minimization is with respect to the the first argument
and
the second argument is a parameter.

Below is a very easy version of the problem.

// Problem: Minimize function with additional parameters

// function you want to minimize, example below is trivial
// and 1 dimensional

double g(float x[],double a)
{ return (x[1]+a)*(x[1]+a); }

// so minimization should result in x[1]=-a.
int main (void)
{
//define other parameters required for the function powell
int n=1;
float *p,**xi;
p=vector(1,n);
p[1]=0.2;
xi=matrix(1,n,1,n);
xi[1][1]=1;
float ftol=0.000001;
int iter=0;
float fret;

double a=(double) rand();

//How to pass the parameter a into the minimization function powell.
//While the parameter a is defined during the running time.
//i.e., one likes to enter g(.,a) in te function

powell(p,xi,n,ftol,&iter,&fret, );
//where the last argument should be a function of the required form

}

Do you mean something like this ...

float lastArg(float fa[], double d)
{
...
}

float (*func)(float [], double) = lastArg;

powell(p, xi, n, ftol, iter, fret, func);
--
==============
Not a pedant
==============
Jul 6 '06 #3
"Martin Bootsma" <ma***********@gmail.comwrote in message
news:11*********************@m38g2000cwc.googlegro ups.com...
>I have a C question, which looks very easy, but no one here seems to
know an easy answer.

I have a function "powell" (from Numerical Recipes) which takes an
argument of the type

"double (*f)(float[])"

But I want to be able to pass a

"double (*f)(float[], double)"

instead, where minimization is with respect to the the first argument
and the second argument is a parameter.
You can't do that without modifying the "powell" function to accept the
value of the extra parameter, to have the correct function pointer type
parameter, and to pass the extra parameter to the function.

A common, generic solution for functions that have a function pointer
parameter is for them to have an additional parameter of type void *, which
is passed to the callback function (often as the first argument). For
example:

void powell(/* ..., */ double (*f)(void *, float *), void *vp) {
/* ... */
f(vp, pointer_to_float);
/* ... */
}

double g(void *vp, float *x) {
double *pa = vp;
double a = *pa;
return (x[1] + a) * (x[1] + a);
}

In the calling code:
double a = some_value;
powell(/* ..., */ g, &a);

Without modifying the "powell" function, your only option is to use a
global, eg:

double a;

double g(float *x) {
return (x[1] + a) * (x[1] + a);
}

Then do:

a = some_value;
powell(/* ..., */ g);

Alex
Jul 6 '06 #4
Thanks all, I was hoping there was some kind of function that would
produce the equivalent of the mathematical notation

f(.)=g(.,a)

so that I could call powell with the function f in it, but according to
the reactions, there is no clever way to arrange this. I am going to
change the powell code to make the minimization possible.

Jul 6 '06 #5
Martin Bootsma wrote:
Thanks all, I was hoping there was some kind of function that would
produce the equivalent of the mathematical notation

f(.)=g(.,a)

so that I could call powell with the function f in it, but according to
the reactions, there is no clever way to arrange this. I am going to
change the powell code to make the minimization possible.
One other possibility, not especially attractive but
perhaps expedient, is to pass the "parameter" to f() by
storing its value in a static variable:

static double fake_param;

double f(float vector[]) {
return vector[0] + vector[1] * fake_param;
}

...
fake_param = a;
powell (f);
...

--
Eric Sosman
es*****@acm-dot-org.invalid
Jul 6 '06 #6
In article <11**********************@m79g2000cwm.googlegroups .com>,
Martin Bootsma <ma***********@gmail.comwrote:
>Thanks all, I was hoping there was some kind of function that would
produce the equivalent of the mathematical notation

f(.)=g(.,a)

so that I could call powell with the function f in it, but according to
the reactions, there is no clever way to arrange this. I am going to
change the powell code to make the minimization possible.
The equivalent of setting f(.)=g(.,a) is a central feature
of functional programming languages such as Lisp, where the
process of creating the function f out g is known as "currying".

Unfortunately currying is not possible in the current version
of C where functions do not have first class object status.
Let's hope that in some future version of C this will become
possible.

--
Rouben Rostamian
Jul 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
2
by: Pernell Williams | last post by:
Hi all: Thank you for your responses. I have a more specific question about "file.seek() and file.readline()" versus "file.seek() and file.xreadlines". When I have the following code:
1
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms....
3
by: notme | last post by:
I have code with lots of "new int..." etc.. I'd like to replace every instance with "new(allocInfo) int" (that is, using placement new) I tried doing: #define new (new(allocInfo)) but that...
36
by: Peng Jian | last post by:
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) I'm a beginner learning C. I can't see what *(a) == *(b) is for. If either a or b is a null pointer, this may cause crash. And if...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
6
by: pookiebearbottom | last post by:
Let's say I have headers Sal.h with this class class Sal { public: int doit() { return 1;} }; now I know that the compilier can choose NOT to inline this function. So if I include this in...
4
by: Belebele | last post by:
The following code fails to compile. My intention is to provide different definitions for a nested class for a class template partial specialization. Here it is: template <typename , int class...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.