Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 9:02 pm

// Lagrangian multipliers using higher-order functions
 Function<double, double> f1 = [](double x) { return x; };
 Function<double, double> f2 = [](double x) { return x*x; };
 Function<double, double> f3 = [&](double x) { return f2(x)*f2(x); };
 std::vector<double> lambda = { 0.25, 0.5, 0.25 };
 FunctionSet<double, double> funSet = { lambda[0] * f1, lambda[1] * f2, lambda[2] * f3 };

 Function<double, double> funSum = Sum(funSet);
 std::cout << funSum(5.0) << '\n'; // 170

 Function<double, double> f4 = [](double x) { return std::exp(x); };
 auto funSum2 = f4 + funSum;

 std::cout << funSum2(5.0) << '\n'; // 318.413
Here's another way to build up functions, in this case Lagrangian multipliers (n-dim also)

































































































































































































































































































































































































































































[img]//%20Lagrangian%20multipliers%20using%20higher-order%20functions%20 Function<double,%20double>%20f1%20=%20[](double%20x)%20{%20return%20x;%20};%20 Function<double,%20double>%20f2%20=%20[](double%20x)%20{%20return%20x*x;%20};%20 Function<double,%20double>%20f3%20=%20[&](double%20x)%20{%20return%20f2(x)*f2(x);%20};%20 std::vector<double>%20lambda%20=%20{%200.25,%200.5,%200.25%20};%20 FunctionSet<double,%20double>%20funSet%20=%20{%20lambda[0]%20*%20f1,%20lambda[1]%20*%20f2,%20lambda[2]%20*%20f3%20};%20%20 Function<double,%20double>%20funSum%20=%20Sum(funSet);%20 std::cout%20<<%20funSum(5.0)%20<<%20'\n';%20//%20170%20%20 Function<double,%20double>%20f4%20=%20[](double%20x)%20{%20return%20std::exp(x);%20};%20 auto%20funSum2%20=%20f4%20+%20funSum;%20%20 std::cout%20<<%20funSum2(5.0)%20<<%20'\n';%20//%20318.413[/img]
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 9:18 pm

Maybe post is to r/cpp (Reddit) ?
Who?
You, so that you get some proffessional feedback, which is what you're looking for, no?
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 9:23 pm

A quiz, without requirements? Why?

"you can assemble complex functions from simpler ones in any dimension."

This works:

double complex_function (double x) {return f(x) + g(x) + h(x)};

And we can easily add more terms, just keep on typing..

what requirement is missing here?
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 9:38 pm

Maybe post is to r/cpp (Reddit) ?
Who?
You, so that you get some proffessional feedback, which is what you're looking for, no?
I know how it all works. Has nobody built such a library? 
The code is feasible. All it needs is a TMP guy to spruce it and then you have APL!
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 9:42 pm

A quiz, without requirements? Why?

"you can assemble complex functions from simpler ones in any dimension."

This works:

double complex_function (double x) {return f(x) + g(x) + h(x)};

And we can easily add more terms, just keep on typing..

what requirement is missing here?
That's old C and it not a higher-order function. And its certainly not complex.  It leads to code bloat. I can say 

f3 = f + h + g*your_C_funtction; Like in mathematics.It is a delayed function, yours is eager in FP jargon.
And try these.
auto h1 = f + g;
 FType<double> h2 = f + exp(e);
 FType<double> h3 = g + f;

 // Experimenting 
 FType<double> h4 = exp(exp(f));
 FType<double> h5 = exp(exp(f) + g);
 FType<double> h6 = log(exp(h5) + h5);
 FType<double> h7 = h6 + 1.0;
Last edited by Cuchulainn on January 15th, 2018, 9:51 pm, edited 4 times in total.
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 9:45 pm

How about you suggest your library to boost?

You'll get good feedback about the design, how to make it better?

Or maybe it's just plain vanilla lambda, ..not a library really?
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 10:01 pm

How about you suggest your library to boost?

You'll get good feedback about the design, how to make it better?

Or maybe it's just plain vanilla lambda, ..not a library really?
Lambda is the secret ingredient indeed. It is needed for quite a few problem and w/o it we have to copy, paste and edit code.
I am interested in the maths requirements,. then it is a question to design it in Boost. 
Other languages can do it, so why not C++?
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz - Maths and acccuracy

January 15th, 2018, 10:07 pm

So your requirement of "leads to code bloath" led you to this design?

The simple C version will not have code bloath (why?). Perhaps there is a different requirement you can come up with?
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

February 18th, 2018, 2:58 pm

Why no std::coth() in C++? Did the implementors forget?
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Re: C++ quiz - Maths and acccuracy

February 18th, 2018, 3:38 pm

Why no std::coth() in C++? Did the implementors forget?
They got lost in the 57 ways to compute e^x.
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

February 18th, 2018, 7:29 pm

Why no std::coth() in C++? Did the implementors forget?
They got lost in the 57 ways to compute e^x.
That's close enough.

http://en.cppreference.com/w/cpp/numeric/math
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

February 23rd, 2018, 3:42 pm

Is this a rant or does it have a point?

http://yosefk.com/c++fqa/defective.html
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Re: C++ quiz - Maths and acccuracy

February 23rd, 2018, 5:38 pm

Both rant & true:
Image

(It seems like a fundamental trade-off in language design and evolution.)
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

February 24th, 2018, 3:38 pm

Is this a rant or does it have a point?

http://yosefk.com/c++fqa/defective.html
With all statements we should not only consider the contents, the author and the context in which the statements are applicable.

Interesting to note that lack of support for modules is not mentioned. 
 
User avatar
Cuchulainn
Topic Author
Posts: 20255
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz - Maths and acccuracy

February 24th, 2018, 3:42 pm

Both rant & true:
Image

(It seems like a fundamental trade-off in language design and evolution.)
True, haven't heard that since donkey's years.