• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Functional Programming

More than two levels of f(g) is becoming academic.

I don't know about that. Genetic programming off the top of my head seems like it could conceivably use arbitrary levels of compositions.

In fact, this caught my attention because this is what they were trying to do at the last (FT) job I had: The boss had an idea that objects representing things such as stochastic processes could be built up from simple "building blocks" (and it certainly is possible). The thing was they were using C# objects, so that every composition of two objects to form a new one necessarily added an additional level of indirection (since all C# objects are polymorphic).

I redid it using boost::accumulator, which through TMP is able to compose operations with no indirection (though it probably implicitly relies on inlining as in @Polter 's example above).

So this is apparently an example of something than can be done feasibly in C++ but not in C#.
 
@MiloRambaldi:
...

This is a good question, actually, since inlining is important for a lot of modern C++ in general.
Say, also tag dispatching: http://www.boost.org/community/generic_programming.html#tag_dispatching
Note how `advance` always dispatches to an implementation in `advance_dispatch` -- which is itself a form of optimization (matching the best algorithm given the iterator category). If this introduced the cost of function calls (due to lack of inlining), then that could defeat the purpose.

...

Interesting. Implicit inlining is relied on more than I realized.
 
[QUOTE="MiloRambaldi, post: 155489, member: 7682

So this is apparently an example of something than can be done feasibly in C++ but not in C#.[/QUOTE]
C# has been lagging behind C++ in this regard for some time.

Ex. has anyone written a generic matrix class<T> with +, - and so on? You need

1. Reflection to emit code

or

2. Use dynamic type (jikes)
 
Not the Java compiler. I should have written "a term used to show shock or extreme surprise."

Normally, I use "meshuggah" which is used in Amsterdam.

Seriously, using run-time types to emulate generics is just bad IMO.
 
C# has been lagging behind C++ in this regard for some time.

Ex. has anyone written a generic matrix class<T> with +, - and so on? You need

1. Reflection to emit code

or

2. Use dynamic type (jikes)

I meant "not doable in C#" at a deeper level. In your example, you have the option of avoiding generics (e.g. MatrixDouble, MatrixInt, etc...) at the expense of code maintainability.

But it seems the entire concept of arbitrary combinations of objects for the purpose of computation is not doable in C#, even resorting to horrible code---on second though I guess reflection emit would make this feasible, but it's truly horrible.
 
Last edited:
Actually, f(g) works direct

C++:
    auto fg = compose(f, g);
    std::cout << fg(2.0) << std::endl;
    std::cout << f(g(2.0)) << std::endl;
 
Numeric pipeline, really works as you would expect in maths

** std::cout << fScalar(fFilter(fVectorValued(A))) << std::endl;

///

C++:
// Composition of vector and vector-valued functions djd
    // ?? like a monad/continuation?
    //#include <boost/numeric/ublas/io.hpp>
    //#include <boost/numeric/ublas/matrix.hpp>
    //#include <boost/numeric/ublas/matrix_proxy.hpp>
    typedef boost::numeric::ublas::matrix<double> Matrix;

    auto fVectorValued = [](const Matrix& m) -> Matrix
    {
        // Temp objects..
        Matrix A(m.size1(), m.size2()); double fac = 1.0;
        for (size_t i = 0; i < A.size1(); ++i)
        {
            for (size_t j = 0; j < A.size2(); ++j)
            {
                A(i, j) = m(i,j)*fac;
            }
        }
    
        return A;
    };

    auto fFilter = [](const Matrix& m) -> Matrix
    {
        return boost::numeric::ublas::subrange(m, 0, 1, 0, 1);
    };

    auto fScalar = [](const Matrix& m) -> double { return norm_frobenius(m); };

    Matrix A(3,3);
    for (size_t i = 0; i < A.size1(); ++i)
    {
        for (size_t j = 0; j < A.size2(); ++j)
        {
            A(i, j) = 0.0;
        }
        A(i, i) = 1.0;
    }

    std::cout << fScalar(fFilter(fVectorValued(A))) << std::endl;
    std::cout << fScalar(fVectorValued(A)) << std::endl;
 
Last edited:
Back
Top