Vietnam Quantitative Finance Society

User Name
Password
Go Back   Vietnam Quantitative Finance Society > Forums List > IT & Software
Reply
09-06-2007, 09:22 PM   #1
learner

Member
No Avatar
 
Join Date: Sep 2007
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts




Default Functional Programming in C++ without extra libraries

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.
learner is offline Reply With Quote
09-06-2007, 10:18 PM   #2
nguyenxuanson

Core Member
Corporal
No Avatar
 
Join Date: Aug 2007
Posts: 58
Thanks: 1
Thanked 1 Time in 1 Post




Default

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);
nguyenxuanson is offline Reply With Quote
09-07-2007, 08:04 AM   #3
learner

Member
No Avatar
 
Join Date: Sep 2007
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts




Default

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) {...}
In my main method, I have
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?");
I have another function of the type
Code:
double myG(double,double, double,double)
and I may need to solve myG(fixed,fixed,y,fixed)=0.


Or am I wrong?
learner is offline Reply With Quote
09-07-2007, 03:51 PM   #4
shinichi9htv

Member
Sergeant
No Avatar
 
Join Date: Jul 2007
Posts: 148
Thanks: 13
Thanked 7 Times in 6 Posts




Default

you should define doubletodouble anothefMyfwithfixedt from double MyF(double t=1.5,double) and pass it in newton
shinichi9htv is offline Reply With Quote
09-07-2007, 10:02 PM   #5
learner

Member
No Avatar
 
Join Date: Sep 2007
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts




Default

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.
learner is offline Reply With Quote
09-07-2007, 10:11 PM   #6
nguyenxuanson

Core Member
Corporal
No Avatar
 
Join Date: Aug 2007
Posts: 58
Thanks: 1
Thanked 1 Time in 1 Post




Default

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
nguyenxuanson is offline Reply With Quote
09-07-2007, 10:47 PM   #7
shinichi9htv

Member
Sergeant
No Avatar
 
Join Date: Jul 2007
Posts: 148
Thanks: 13
Thanked 7 Times in 6 Posts




Default

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.
shinichi9htv is offline Reply With Quote
09-07-2007, 11:22 PM   #8
Khoa Tran

Core Member
Master Sergeant
 
Khoa Tran's Avatar
 
Join Date: Jun 2007
Posts: 211
Thanks: 28
Thanked 19 Times in 16 Posts




Default

Quote:
Originally Posted by nguyenxuanson View Post
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.
Bro, You're talking about Aspect-Oriented Programming (AOP), aren't You?

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.
Khoa Tran is offline Reply With Quote
09-08-2007, 12:05 AM   #9
shinichi9htv

Member
Sergeant
No Avatar
 
Join Date: Jul 2007
Posts: 148
Thanks: 13
Thanked 7 Times in 6 Posts




Default

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?
shinichi9htv is offline Reply With Quote
09-08-2007, 07:06 AM   #10
learner

Member
No Avatar
 
Join Date: Sep 2007
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts




Default

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.
learner is offline Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Save or bookmark this site with: Del.icio.usStumble It!AddThis.com
All times are GMT +7. The time now is 06:26 AM.
Powered by vBulletin Version 3.7.2