# Switching from Matlab to C++

#### ezbentley

##### Active Member
I am fairly proficient in Matlab/R, but would like to gradually transition my research framework to C++ as a learning experience. My research involves mostly econometrics, multivariate time series, and portfolio optimization, i.e. more emphasis on portfolio management and less on pricing. The matrix-based framework embedded in Matlab/R makes a lot of these analyses a breeze.

I wonder if anyone has made similar transition and can comment on the most effective way to create a development framework that allows prototyping in C++ as rapidly as in Matlab/R. Are there some "standard" C++ libraries that people use to perform Matlab/R functions? Or is it necessary to create everything from scratch?

#### alain

##### Older and Wiser
You are going to be under a lot of pain. You will hate it and what use to take you minutes or hours to finish will change to days and weeks. You will spend more time writing C++ than solving the problems.

In today's terms, there is no such a thing as prototyping in C++. Actually C++ is not meant to build prototypes but production systems by today's standards.

Good luck!! It might be easier to still use Matlab and R, and use C++ for bits and pieces that need some speed up after you have profiled your code.

#### ezbentley

##### Active Member
I agree it's going to be a lot of pain, but the fact is that 90% of the quant research(not developer) job postings require C++, not Matlab. So it seems like they use C++ for both research and production. So I guess the pain is unavoidable. That's why I wonder whether there already exists some libraries that make research more friendly.

#### Michael M

##### Active Member
C++
I agree - I've mainly used Matlab for the past 10 years, even though I originally started in C and C++, and am now relearning C++ for the same reasons. I always loved programming, but seemed confused by the existence of a plethora of languages out there, with new ones coming in all the time, and industrial demands for particular languages. And then each one just adds features from other languages it didn't have originally, making them even more "similar".

I liken the whole process to the electoral college - it makes sense historically (and industrially) speaking, but if we had to start over from scratch today, we'd create a much simpler system, maybe with 5 languages at the most, if not 1!

#### Polter

##### Well-Known Member
Get some numerical linear algebra lib.
I think you might like Eigen: http://eigen.tuxfamily.org/dox-devel/
A short ASCII reference with Matlab translations: http://eigen.tuxfamily.org/dox-devel/AsciiQuickReference.txt
Active and helpful community forum: http://forum.kde.org/viewforum.php?f=74

Blaze is perhaps worth looking into: http://code.google.com/p/blaze-lib/

Note that preferably you want to focus on the libraries that beat (or at least match up to) Intel MKL, since that's what MATLAB's relying on for its vectorized operations (of course you're going to easily beat it for the non-vectorized ones, but why settle for less), and it seems sub-optimal to use anything slower; that's also the reason I'm suggesting either Eigen or Blaze:
http://eigen.tuxfamily.org/index.php?title=Benchmark

// From the above benchmarks, the Armadillo+MKL combination doesn't look bad either, but note that you *must* get an external BLAS lib (like MKL), otherwise Armadillo is not going to be very useful or fast (it takes a different design approach, relying on an external optimized BLAS instead of providing optimized numerical linear algebra implementation itself -- discussing the relative merits & trade-offs is beyond the scope of this post, but be aware that what it means for the end-user (you) is that you need to get it separately & set it up):
http://arma.sourceforge.net/faq.html#dependencies

From what I can tell, the same is true for Blaze:
"Additionally, for maximum performance Blaze expects you to have a BLAS library installed (Intel MKL, ACML, Atlas, Goto, ...). If you don't have a BLAS library installed on your system, Blaze will still work and will not be reduced in functionality, but performance may be severely limited. Thus it is strongly recommended to install a BLAS library."

In contrast, Eigen is a standalone library, highly competitive with MKL on its own.

For more choices, see this thread:

It's worth mentioning that recently the Numerical Template Toolbox (NT²) has restarted its development:
http://github.com/MetaScale/nt2
It's interesting, since "provide a simple, MATLAB-like interface for users" is one of the guiding goals.
For instance, it supports select-all-rows/columns operator (colon in MATLAB, underscore in NT2) and vectorized functions:
Code:
MATLAB: B = I(:,:,3); ...; V = min(abs(0.5.*R-0.419.*G-0.081.*B),240);
C++ w/NT2: B = I(_,_,3); ...; V = min(abs(0.5*R-0.419*G-0.081*B),240);
Presentation:
[PDF] https://github.com/boostcon/2010_presentations/raw/master/thu/falcou_lapreste_boostcon.pdf
[Video] http://blip.tv/boostcon/joel-falcou...ibrary-for-high-performance-computing-4204932
However, it's a relatively new (revision of the) library under active development, so for starters you may want to consider one of the aforementioned ones at first.

#### alain

##### Older and Wiser
I agree it's going to be a lot of pain, but the fact is that 90% of the quant research(not developer) job postings require C++, not Matlab. So it seems like they use C++ for both research and production. So I guess the pain is unavoidable. That's why I wonder whether there already exists some libraries that make research more friendly.
I can tell you that today, they use C++ for production, very little use in research, too much of a pain.

#### alain

##### Older and Wiser
One more thing, if you are going to learn C++, learn the new standard. It will make your life way easier.

#### Daniel Duffy

##### C++ author, trainer
I can tell you that today, they use C++ for production, very little use in research, too much of a pain.
No pain, no gain.

On the other hand, knowing a couple of languages (C#, Python) is useful. But knowing C++ means learning another language is relatively easy.

#### alain

##### Older and Wiser
No pain, no gain.
I don't think the pain justify the gain in this case.

#### ezbentley

##### Active Member
On the other hand, knowing a couple of languages (C#, Python) is useful. But knowing C++ means learning another language is relatively easy.
I agree. In fact, I used C++ extensively 10 years ago. It's just that Matlab/R make data analysis too convenient that my memory of C++ has been fading. Now I need to re-learn C++ to do the things that I can easily do in Matlab/R just because the demand for C++ quant is much higher than Matlab quant.

#### Daniel Duffy

##### C++ author, trainer
I don't think the pain justify the gain in this case.
We will agree to disagree.

The real answer is for each developer to decide for himself or herself. The answer is not a binary true/false.

##### Well-Known Member
But knowing C++ means learning another language is relatively easy.
Does it make learning Scheme or ML easier?

#### ExSan

##### Well-Known Member
One more thing, if you are going to learn C++, learn the new standard. It will make your life way easier.
and what is the new standard? can you please point out with a link. Thnks

#### Daniel Duffy

##### C++ author, trainer
Does it make learning Scheme or ML easier?
Good question! I'm going to sit right down and write an answer. Need a few days.

Generalising "does C++ prepare you for the functional programming onslaught?"

#### Daniel Duffy

##### C++ author, trainer
and what is the new standard? can you please point out with a link. Thnks
Type in

"C++ 11 Wiki"

#### Martin.T

##### Member
Just to add to the discussion, from an industry perspective (here in Canada), what I realised is that most of the firms that used C++ are HFT firms and this is mainly for speed reason. Here, on the buy side and mainly in the big pension funds, MATLAB/R/Python are the primary languages used in research since speed is not a real issue. I worked last summer in Market Risks and MATLAB was the language used by the team.

Since you seems to be more research oriented, I personal think a good way of learning C++ to start would be to interface MATLAB and C++ in some of your programs. It would make your life easier and the learning curve might not be as stepped.

Replies
1
Views
873
Replies
4
Views
763
Replies
5
Views
1K
Replies
9
Views
2K
Replies
0
Views
281