SERVING THE QUANTITATIVE FINANCE COMMUNITY

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

// 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]

outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

### Re: C++ quiz - Maths and acccuracy

Maybe post is to r/cpp (Reddit) ?
Who?
You, so that you get some proffessional feedback, which is what you're looking for, no?

outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

### Re: C++ quiz - Maths and acccuracy

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?

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

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!

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

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.

outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

### Re: C++ quiz - Maths and acccuracy

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?

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

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++?

outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

### Re: C++ quiz - Maths and acccuracy

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?

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Why no std::coth() in C++? Did the implementors forget?

Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

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

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

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

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Is this a rant or does it have a point?

http://yosefk.com/c++fqa/defective.html

Posts: 23951
Joined: September 20th, 2002, 8:30 pm

### Re: C++ quiz - Maths and acccuracy

Both rant & true:

(It seems like a fundamental trade-off in language design and evolution.)

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

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.

Cuchulainn
Topic Author
Posts: 62149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: C++ quiz - Maths and acccuracy

Both rant & true:

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

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...

 JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...

GZIP: On