Implement matlab code in C++? - c++

I have a certain code written in Matlab. I wanted to obtain similar results using a c++ code. I wanted to know whether a c++ library exists for matrix manipulations? My current Matlab code consists of matrix multiplications,inverse and complements.

There are dozens of linear algebra libraries for C++. Here is another one not mentioned in the other links so far:
http://www.alglib.net/

I have used Newmat in a similar situation.

Not sure if this would help you, or your company/university have licenses for it but matlabcoder can export your matlab code to c++ without much extra work.
http://www.mathworks.com/products/matlab-coder/

Related

Eigenvalue library for c++, recommendations, how to implement, looking for something similar to dgeev from fortran

I am new to C++, with some training using fortran95. Trying to convert my knowledge into the new syntax but have run into a snag.
Many of my programs use modules with subroutines, and subroutines within subroutines and use of functions from a library described by NAG.com which are readily available and searchable.
I am currently looking for a c++ version of
http://www.nag.com/numeric/FL/manual/pdf/F08/f08naf.pdf
From what I have read so far, these libraries exist for c++ and I have used some simple ones thus far(like vector, cmath, math.h) but only ones that are already included in my Xcode package for my mac.
I haven't seen anybody mention one of these which my be included with my Xcode, and I am lacking in how to implement outside libraries I find. I am interested particularly in using:
http://www.alglib.net/download.php
Thus far I have been using subrutines as void type functions and simply including them in all of my code. But my code is becoming exceedingly cumbersome and I would like to make something similar to a fortran module to do chebyshev calculations. And I would much rather find a good library of eignevalue calculators and maybe even chebyshev calculators, . . . which I can use.
Essentially my question is, how do I implement external libraries I find and does anybody have a recommendation for a good one? How can I make my own code which contains a callable set of functions and then call it from within another piece of code?
If I understand right, the part of the NAG library you're using in Fortran is basically LAPACK. There is a C interface to LAPACK, called LAPACKE (http://www.netlib.org/lapack/lapacke.html). You can use it in a C++ program.
I didn't understand the other parts of your question.

MATLAB Coder: sparse matrix

MATLAB Coder seems to be fancy and can speed up MATLAB code much by converting the code into C/C++ or MEX.. But it seems that it does not support sparse matrix, or the matlab function sparse which is essential to my code. Does anyone have any idea about how to overcome this problem? Many thanks!
The eigen library can be used to handle the Matlab function sparse and it is in c language.
As said by "libill", You could also just generate MEX code for the portions of your MATLAB code which doesn't use sparse to speed those portions up.
And for sparse part use above library.
My understanding of Matlab coder maybe wrong but I think it is a tool that convert your Matlab codes to C/C++ codes. I am not sure performance optimization is the goal of Matlab coder.
Take a look at suitesparse. It's a C Library that handle a lot of sparse operations. The Matlab "\" operator in the case of sparse matrix actually uses suitesparse. So I think it may be easier for you to modify the Matlab generated C codes.
HTH
I just am not sure what do you mean by converting. I assume you mean writing your own mex function.
Sparse packages usually deal sparse matrices differently and Matlab has also its unique way to treat them.
However mostly they use CSC (or CSR) format. Matlab is also saving the matrix as CSC format.
You can look at lots of sparse packages mex functions to see how it is done.
The point is Matlab will not let you save explicit zeros in its sparse data structure.
DO NOT try to save explicit zeros on Matlab mex function. Matlab will choke on you!

Implementing MATLAB function in c++ using Intel C++ compiler

I have developed a MATLAB program with Visual C++. I am using IntelĀ® Integrated Performance Primitives because speed of program is important issue and I have done a lot of efforts for implementing some MATLAB functions. For example, for Min and Max functions over a vector i use ippsMaxIndx_32f; but, there is function in MATLAB like Find.
Here is a description of the Find method in MATLAB:
Description
I need a function which implements this find function of MATLAB with high speed.
Are there any functions inside Intel Ipp, that works like the Find function in MATLAB?
I've never heard of a comprehensive port of matlab functionality to C++. That being said, almost everything matlab does exists within a C/C++ library somewhere, some off the top of my head:
LAPACK, BLAS, and there are a few good implementations, the most notable (free) one being ATLAS.
FFT is implemented in matlab via the fftw library
There are loads of fast open-source image libraries out there, ie. interpolation, filtering.
There are really good OOP matrix libraries out there, boost has a nice one.\
After that, well figure out what you need and there is a good chance someone has implemented it in C/C++.
You can check to these to see if you can find the function you are looking for! As i am not sure for that.
I've used ippsFind_* and they have worked just fine.

Matrices/Vectors in C++ with the R math standalone library?

All,
I have been playing with the R math standalone library in C++. I quite like being able to generate random numbers and use distribution functions that I am comfortable with from R. My question is: is it possible to use the matrix operations (multiplication, transpose, inverse, Chol, etc) that are available in R in a standalone library? I don't see them in Rmath.h.
If matrix operations are available to use in standalone C++ code, it seems that the R API becomes the perfect open source computational engine. Are people using it in this fashion?
Thanks so much for your guidance and suggestions!
Most, if not all, of the things you mention are provided by LAPACK or BLAS Fortran code that R links to, not something R provides new code for itself.
If you are interested in using C++ with R, look at the Rcpp package by Dirk Eddelbuettel and Romain Francois. Dirk has also written the RcppArmadillo package as an interface to the Armadillo C++ linear algebra library that can do the matrix operations you mention.
Whether this is of use will depend on whether you are wanting to write separate C++ code that is accessed outside R, or interfacing C++ code that you access within R. Rcpp facilitates (greatly) the latter. Take a look at Armadillo directly if it is the former situation.
I think the original questions starts at the wrong end. There is no C++ in R, and no C++ API in R itself.
So if you want to access R matrix functions, you have to go through the R API -- which is a C layer and very much not C++.
Gavin in his answer (and comments) and Josh are spot-on: You probably want something like Armadillo for high-level matrix algebra. Armadillo (just like related libraries) eventually goes to BLAS and LAPACK --- as does R. [ Doug Bates usually reminds us that there is one important difference related to pivoted decompositions; this is touched upon in the fastLm() implementation and example in RcppArmadillo. ]
Lastly, Rcpp can help with both cases:
whether you want to extend R by calling C++ code you wrote, where Rcpp makes it easy to pass objects back and forth, or
whether you want to embed R inside your C++ application using RInside as Rcpp once again provides the glue
The rcpp-devel list is a good place for more in-depth discussions and examples.

Call MATLAB APIs in C/C++

I just heard from somewhere that for numerical computation, "MATLAB does offer some user-friendly APIs. If you call these APIs in your C/C++ code, you can speed up computation dramatically."
But I did not find such information in MATLAB documents like http://www.mathworks.com/support/tech-notes/1600/1622.html and http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html. All I learned from these websites is that MATLAB can be called in C and C++ by Matlab engine or by compiling M-files into libraries by mcc. They don't mention any built-in numerical MATLAB APIs that can be called in C/C++.
Can someone please clarify?
Thanks and regards!
You want the "Engine" routines. This allows you to start up a background MATLAB process from C and execute calculations on it: relevant MATLAB documentation.
It works pretty well, have a look at the examples. I would say the most annoying thing getting it working is marshaling the data between C and MATLAB. But that's always a problem when doing this kind of thing.
It sounds like you're looking for the code generation tools in the embedded matlab toolbox or real time workshop.
Do doc eml and look for a the LMS (least mean square) equalizer demo.
The code generator is quite good, it will give you a make file that will build a static library. It's easy to use with your stand alone C/C++ code.
There could be a few things that quote is referencing, I assume that it is referring to the MATLAB Compiler. So going from MATLAB -> C++ you can use the compiler to build standalone "faster" applications. However, when speed testing the improvement, I've noticed it to be negligible. Honestly, you're probably far better off coding your work in C from the get-go, the code that the compiler generates is spaghetti and non-object oriented. I should also mention that this is an expensive extension to Matlab.
You can use the MCR in your own c++ project as a stand-alone library (details)... but you might get similar results using Numerical Recipes.
Disclaimer: I used this product 2-3 years ago, things could be different now.