| 09-06-2007, 09:22 PM | #1 |
|
Member
Join Date: Sep 2007
Posts: 9
Thanks: 3 Thanked 0 Times in 0 Posts |
In implemeting an implicit finite difference method, I need to use some kind of higher-order functions in functional programming. But I don't know how to.
I have a root-finding method (Newton-Raphson) that takes function pointers as parameters (f and derivative of f). These are functions on 1 variable. However, in my finite difference method, I have functions that depend on many variables. When I fix n-1 variables (like time variable), they functionally depend on 1 variable and I want to call my Newton-Raphson method. How would I do it in C++? I've read on wiki that we can have functional programming (higher-order functions, list processing, etc.) in C++ by using yet-another-library. I try not to depend too much on libraries and I want to have a simple solution for this problem only. Wiki says that we can use function pointers to implement higher-order functions. How does that work? Thanks a lot. Last edited by learner; 09-06-2007 at 09:27 PM. |
|
|
|
| 09-06-2007, 10:18 PM | #2 |
|
Core Member
Corporal
Join Date: Aug 2007
Posts: 58
Thanks: 1 Thanked 1 Time in 1 Post |
pointer function/ abstract base class enable you to have functional programming approach.
http://www.newty.de/fpt/fpt.html I do not see your logic here. Functional Programming enter here because when you search for root of a function, you can only fix the form of the function a priori and not the concrete implementation of the function, i.e. You want to find solution of the class of function double f(double x) then you would work with the following pointer function datatype pDoubleToDoubleFunction: typedef double (*pDoubleToDoubleFunction)(double); |
|
|
|
| 09-07-2007, 08:04 AM | #3 |
|
Member
Join Date: Sep 2007
Posts: 9
Thanks: 3 Thanked 0 Times in 0 Posts |
After (re)thinking for awhile, I guess that you misunderstood my question. Let me be more concrete.
Suppose I have a Newton-Raphson method like this: Code:
double newton(DoubleToDouble f, DoubleToDouble df) {...}
Code:
...
t = 1.5;
//need to find y such that myF(t,y)=0, where my f has the type
//double myF(double, double)
printf("How do I pass myF into the newton method?");
Code:
double myG(double,double, double,double) Or am I wrong? |
|
|
|
| 09-07-2007, 03:51 PM | #4 |
|
Member
Sergeant
Join Date: Jul 2007
Posts: 148
Thanks: 13 Thanked 7 Times in 6 Posts |
you should define doubletodouble anothefMyfwithfixedt from double MyF(double t=1.5,double) and pass it in newton
|
|
|
|
| 09-07-2007, 10:02 PM | #5 |
|
Member
Join Date: Sep 2007
Posts: 9
Thanks: 3 Thanked 0 Times in 0 Posts |
That's my question: how do you define such a function dynamically (on the fly)?
I'm sorry if I'm slow here. I didn't have much experience with C++ before. ![]() |
|
|
|
| 09-07-2007, 10:11 PM | #6 |
|
Core Member
Corporal
Join Date: Aug 2007
Posts: 58
Thanks: 1 Thanked 1 Time in 1 Post |
it is parser contruction, you should define atomique operator like +, *, exp, .... but i think you should not do it now. A more advance technique is Code Injection which enable you to create source code, compile in the run and inject the Dynamically Load Library into the memory. If you are good at symbolic software and you are good at C++ I can hire you right now if you want.
Son |
|
|
|
| 09-07-2007, 10:47 PM | #7 |
|
Member
Sergeant
Join Date: Jul 2007
Posts: 148
Thanks: 13 Thanked 7 Times in 6 Posts |
you can define
doubletodouble myF2(double t=1.5, double y) { double fff = myF(t,y); return new doubletodouble(y,fff); } Hope it helps. My C++ sucks. |
|
|
|
| 09-07-2007, 11:22 PM | #8 | |
|
Quote:
Well, I worked on AOP (I used AspectJ as I was programming with Java/J2EE) more than 3 years ago. AOP is a beautiful addition to OOP (at least in my opinion) and is particularly useful in industries where it's too costly to dig into the code of the current software system but written 15 years ago (like in banking industry). However, this technique is probably too cumbersome in the problem setting of Tú. My C++ also sucks so I can't suggest anything. Quân, your code doesn't work due to a few reasons: 1. You cannot define 10000 such functions for 10000 values of t. 2. Your code is syntactically incorrect as you can't declare "myF2(double t=1.5, ...)". 3. myF2 takes 2 parameters and should return a double, not a function pointer DoubleToDouble. p/s: ở đây có bác Hưng (và nhiều bác khác) chuyên trị C++ với Computer Science nói chung. Bác Hưng (và các bác khác) vào cho ý kiến cái nhểy. ps2: Tú, what "extra libraries" did you mean here? Last edited by Khoa Tran; 09-08-2007 at 03:11 AM. |
||
|
|
|
| 09-08-2007, 12:05 AM | #9 |
|
Member
Sergeant
Join Date: Jul 2007
Posts: 148
Thanks: 13 Thanked 7 Times in 6 Posts |
oh, OK, thank you Khoa
. I just disagree with the point 2. 1.5 is the default value of t, why can't we declare like that? ![]() |
|
|
|
| 09-08-2007, 07:06 AM | #10 |
|
Member
Join Date: Sep 2007
Posts: 9
Thanks: 3 Thanked 0 Times in 0 Posts |
Because of the deadline and because the homework equation is too simple, I solved myF (linear function) analytically and didn't use my newton method. But I think it's helpful for me in the future (and for some people here, maybe) to learn how to resolve this problem.
@anh Son: I'm neither good at symbolic software nor C++ . What is a symbolic software?@anh Khoa: I meant this library but it looks complicated to use. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|