Skip to main content
added 985 characters in body
Source Link
1174
  • 131
  • 4
  • 18

Got rid of redundancy, might not be the most technical solution but:

boost::function<double (const double)> bindCPricer(OptionPricer &pricer) { return boost::bind(&OptionPricer::CallPrice, &pricer, _1); } boost::function<double (const double)> bindPPricer(OptionPricer &pricer) { return boost::bind(&OptionPricer::PutPrice, &pricer, _1); } cout << "Mesh1 Call: " << pricer1.Mesher(start, end, h, bindCPricer(pricer1)) << " Mesh1 Put: " << pricer1.Mesher(start, end, h, bindPPricer(pricer1)) << endl; cout << "Mesh2 Call: " << pricer2.Mesher(start, end, h, bindCPricer(pricer2)) << " Mesh2 Put: " << pricer2.Mesher(start, end, h, bindPPricer(pricer2)) << endl; 

Thank you very much for your input!

Btw. I have been programming Java for more than 10 years, I am wondering why I haven not learnt C++ earlier, so far it looks much more powerful to me, boost is awesome, templating is also more sophisticated than Java Generics.

Got rid of redundancy, might not be the most technical solution but:

boost::function<double (const double)> bindCPricer(OptionPricer &pricer) { return boost::bind(&OptionPricer::CallPrice, &pricer, _1); } boost::function<double (const double)> bindPPricer(OptionPricer &pricer) { return boost::bind(&OptionPricer::PutPrice, &pricer, _1); } cout << "Mesh1 Call: " << pricer1.Mesher(start, end, h, bindCPricer(pricer1)) << " Mesh1 Put: " << pricer1.Mesher(start, end, h, bindPPricer(pricer1)) << endl; cout << "Mesh2 Call: " << pricer2.Mesher(start, end, h, bindCPricer(pricer2)) << " Mesh2 Put: " << pricer2.Mesher(start, end, h, bindPPricer(pricer2)) << endl; 

Thank you very much for your input!

Btw. I have been programming Java for more than 10 years, I am wondering why I haven not learnt C++ earlier, so far it looks much more powerful to me, boost is awesome, templating is also more sophisticated than Java Generics.

added 1189 characters in body
Source Link
1174
  • 131
  • 4
  • 18

I got this now, after advise from Mike Seymour:

Why I did not want to use an instance was because I would then need to bind it to all 3 pricer instances, they contain the data to build the mesh from e.g. they differ in their instance values, I think I might need to make the bind more dynamic somehow.

E.g. above function is not correct since I use only pricer1 function bindings on pricer2 and pricer3 instances.

What I would do at current stage is duplicating that for pricer2, pricer3 etc. Actually I wanted to get rid of redundancy and not duplicate the mesher since it is just iterating over a range, now I still have kind of duplication but definitely better than having a kind of duplication on the function side.

boost::function<double (const double)> functCP = boost::bind(&OptionPricer::CallPrice, &pricer2, _1); boost::function<double (const double)> functPP = boost::bind(&OptionPricer::PutPrice, &pricer2, _1); 

I got this now:

Why I did not want to use an instance was because I would then need to bind it to all 3 pricer instances, they contain the data to build the mesh from e.g. they differ in their instance values, I think I might need to make the bind more dynamic somehow.

I got this now, after advise from Mike Seymour:

Why I did not want to use an instance was because I would then need to bind it to all 3 pricer instances, they contain the data to build the mesh from e.g. they differ in their instance values, I think I might need to make the bind more dynamic somehow.

E.g. above function is not correct since I use only pricer1 function bindings on pricer2 and pricer3 instances.

What I would do at current stage is duplicating that for pricer2, pricer3 etc. Actually I wanted to get rid of redundancy and not duplicate the mesher since it is just iterating over a range, now I still have kind of duplication but definitely better than having a kind of duplication on the function side.

boost::function<double (const double)> functCP = boost::bind(&OptionPricer::CallPrice, &pricer2, _1); boost::function<double (const double)> functPP = boost::bind(&OptionPricer::PutPrice, &pricer2, _1); 
added 1189 characters in body
Source Link
1174
  • 131
  • 4
  • 18

Thank you for the comments, I tried the const double as well before.

Given the advise I tried this now, which works, out of that another question arises.

I got this now:

 // Calculate meshes double start = 0, end = 20, h = 0.5; boost::function<double (const double)> functCP = boost::bind(&OptionPricer::CallPrice, &pricer1, _1); boost::function<double (const double)> functPP = boost::bind(&OptionPricer::PutPrice, &pricer1, _1); cout << "Calculate meshes:" << endl; cout << "Mesh1 Call: " << pricer1.Mesher(start, end, h, functCP) << " Mesh1 Put: " << pricer1.Mesher(start, end, h, functPP) << endl; cout << "Mesh2 Call: " << pricer2.Mesher(start, end, h, functCP) << " Mesh2 Put: " << pricer2.Mesher(start, end, h, functPP) << endl; cout << "Mesh3 Call: " << pricer3.Mesher(start, end, h, functCP) << " Mesh3 Put: " << pricer3.Mesher(start, end, h, functPP) << endl; 

Why I did not want to use an instance was because I would then need to bind it to all 3 pricer instances, they contain the data to build the mesh from e.g. they differ in their instance values, I think I might need to make the bind more dynamic somehow.

Thank you for the comments, I tried the const double as well before.

Given the advise I tried this now, which works, out of that another question arises.

I got this now:

 // Calculate meshes double start = 0, end = 20, h = 0.5; boost::function<double (const double)> functCP = boost::bind(&OptionPricer::CallPrice, &pricer1, _1); boost::function<double (const double)> functPP = boost::bind(&OptionPricer::PutPrice, &pricer1, _1); cout << "Calculate meshes:" << endl; cout << "Mesh1 Call: " << pricer1.Mesher(start, end, h, functCP) << " Mesh1 Put: " << pricer1.Mesher(start, end, h, functPP) << endl; cout << "Mesh2 Call: " << pricer2.Mesher(start, end, h, functCP) << " Mesh2 Put: " << pricer2.Mesher(start, end, h, functPP) << endl; cout << "Mesh3 Call: " << pricer3.Mesher(start, end, h, functCP) << " Mesh3 Put: " << pricer3.Mesher(start, end, h, functPP) << endl; 

Why I did not want to use an instance was because I would then need to bind it to all 3 pricer instances, they contain the data to build the mesh from e.g. they differ in their instance values, I think I might need to make the bind more dynamic somehow.

Source Link
1174
  • 131
  • 4
  • 18
Loading