I am currently developing with my own C++/Mex code and Matlab, but my project is getting big and I am considering switching to a proper linear algebra library. I have read very good things about Armadillo, but I can't find a few essential functions I need for my project.
I understand Armadillo links to LAPACK and BLAS libraries, but I couldn't find the matrix exponential function in Armdaillo's API, nor in the LAPACK functions.
Can anyone tell me if there is an add-on to compute matrix exponentials with Armadillo? If so, a short example code would be much appreciated.
The matrix exponential is something Matlab has. So Octave implemented it. So other Free Software projects looked at what Octave has and reimplemented it by borrowing this implementation.
I work a lot with R and Armadillo via the RcppArmadillo package (for which I'm a co-author). In one piece of recent work I needed expm() and borrowed it for use by Armadillo from the R package exmp.
The code goes like this:
arma::mat expm(arma::mat x) {
arma::mat z(x.n_rows, x.n_cols);
(*expmat)(x.begin(), x.n_rows, z.begin(), Ward_2);
return z;
}
but it hinges of course on the fact that I get the function pointer to expmat from the
R package exmp. The full file is here on Github which has the enum Ward_2 as well.
This has been added as expmat to the latest release, see
http://arma.sourceforge.net/docs.html#expmat
Here's a port of John Burkardt's c/c++ implementation of one of the 19 dubious to Armadillo...
https://gist.github.com/tesch1/0c03e43885cd66eceabe
Related
I am using the GNU scientific library and I was wondering what was the differences between those two function to copy a vector to another :
gsl_blas_dcopy and gsl_vector_memcpy
do you have any idea which one would be the fastest ?
In the GSL manual, section 8.3.6, it says
However, it is useful to have a small number of utility functions
which do not require the full blas code. The following functions fall
into this category.
int gsl_vector_memcpy
So both are basically the same. If you already need BLAS functionality, use gsl_blas_dcopy.
Rumors say a BLAS implementation for you specific CPU might be the fastest.
In the most recent magma linear algebra library (version 1.6.1), http://icl.cs.utk.edu/magma/software/, in the testing code exercising dgemm functionality (source code: testing_dgemm.cpp), there was a call to functions magma_dgemm and magmablas_dgemm. Can someone clarify the difference between the two? Which one is more general (not tied to just GPU)?
Wirawan
An inspection of the source code reveals that magmablas_Xgemm is actually a C function that launches an appropriate gemm kernel on the GPU. Thus magmablas_Xgemm is a GPU-specific routine. On the other hand, magma_Xgemm is intended to be accelerator-agnostic routine that (currently) can be used for either GPU (NVIDIA/AMD, ...) or MIC.
Ref files, relative to MAGMA source directory (the CUDA edition):
./magmablas/dgemm_fermi.cu
./interface_cuda/blas_d.cpp
So, basically MAGMA includes gemm, both magma_gemm that wraps cublasgemm, and magmablas_*gemm that is Magma's open-source implementation.
I need a C++ algebra library in order to use in my project. At the beginning I thought I can write one, but then I realized I was unsuccessfully trying to reinvent the wheel and wasting my precious time.
For arithmetic issues I found GMP Library (you know, for unlimited arithmetic calculations), and tools for other kind of tasks (standard C++ library seemed quite enough for pseudo-random number generation). However, I couldn't find a suitable one for algebraic works.
There are linear algebra libraries (such as Armadillo) but I'm not sure I need such a library. I want to summarize my needs.
#include <string>
#include <somelibrary.h>
int main(){
std::string str = "3*x^3+2*x^2+x+sqrt(x)*x^(1/3)";
algebraic_expression* exp = new algebraic_expression(str);
}
I want to have a tree from such an expression. Lets say it will return a std::vector or C style array with some information. For example (considering the example above) exp[0] will be "3*x^3", or maybe exp[0]["base"]="x".And why do I need this? Actually I can do the similar things by means of using RegEx, but sometimes I cannot handle it for example 3*x^0 is simply 3, I cannot print 3*x^0 because it is meaningless I want to have 3 (just like 3*x^1 is 3*x). Or (3-3)*5*2 will return 0, etc...
Thank you for your help.
You should look for 'CAS' (Computer Algebra System) . I can suggest you two:
Ginac http://www.ginac.de/
Giac: http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
An example program with Giac: http://www-fourier.ujf-grenoble.fr/~parisse/giac_us.html#First%20example
Giac also comes with a GUI application called XCAS. It's a very powerful tool you should give it a try.
I've used LAPACK for my Master thesis program for 3D algebra calculations.
link : http://www.netlib.org/lapack/ link of c++ version of
library : http://www.netlib.org/lapack++/
I am working on an image processing project using Matlab. We should run our program (intended to be an application) on a cell phone.We were then asked to convert our code into C or C++ language so we get a feel of how long it would take for execution and then choose a platform. So far we didn't figure out how to do this conversion.. Any ideas of what to do to convert Matlab to C or C++??
The first thing you need to realise is that porting code from one language to another (especially languages as different as Matlab and C++) is generally non-trivial and time-consuming. You need to know both languages well, and you need to have similar facilities available in both. In the case of Matlab and C++, Matlab gives you a lot of stuff that you just won't have available in C++ without using libraries. So the first thing to do is identify which libraries you're going to need to use in C++. (You can write some of the stuff yourself, but you'll be there a long time if you write all of it yourself.)
If you're doing image processing, I highly recommend looking into something like ITK at http://www.itk.org -- I've written my image processing software twice in C++, once without ITK (coding everything myself) and once with, and the version that used ITK was finished faster, performed better and was ten times more fun to work on. FWIW.
Matlab can gererate C code for you.
See:
http://www.mathworks.com/products/featured/embeddedmatlab/
The generated code does however depend on matlab libraries. So you probably can't use it for a cell phone. But it might save you some time anyways.
I also used the MATLAB Coder to convert some functions consisting of a few hundred lines of MATLAB into C. This included using MATLAB's eigenvalue solver and matrix inversion functions.
Although Coder was able to produce C code (which theoretically was identical), it was very convoluted, bloated, impossible to decipher, and appeared to be extremely inefficient. It literally created about 10x as many lines of code as it should have needed. I ended up converting it all by hand so that I would actually be able to comprehend the C code later and make further changes/updates. This task however, can be very tedious/dangerous, as the array indexing in Matlab is 1-based and in C it's 0-based. You're likely to add bugs into the code, as I experienced. you'll also have to convert any vector/matrix arithmetic into loops that handle scalars (or use some type of C matrix algebra package)
The MathWorks provides a product called MATLAB Coder that claims to generate "readable and portable C and C++ code from MATLABĀ® code". I haven't tried it myself, so I can't comment on how well it accomplishes these goals.
With regard to the Image Processing Toolbox, this list (presumably for R2016b) shows which functions have been enabled for code generation and any limitations they may have.
Matlab has a tool called "Matlab Coder" which can convert your matlab file to C code or mex file. My code is relatively simple so it works fine. Speed up gain is about 10 times faster. This saves me time coding a few hundreds lines. Hope it's helpful for you too
Quick Start Guide for MATLAB Coder Confirmation
The links describe the process of converting your code in 3 major steps:
First you need to make a few simplifications in your present code so that it would be simple enough for the Coder to translate.
Second, you will use the tool to generate a mex file and test if everything is actually working.
Finally you would change some setting and generate the C code. In my case, the C code has about 700 lines including all the original matlab code (about 150 lines) as comments. I think it's quite readable and could be improve upon. However, I already get a 10 times speed up gain from the mex file anyway. So this is definitely a good thing.
We can't not be sure that this will work in all case but it's definitely worth trying.
I remember there is a tool to export m-files as c(++)-files. But I could never get that running. You need to add some obscure MATLAB-headers in the c/c++code, ... And I think it is also not recommended.
If you have running MATLAB-code, it shouldn't take too much effort to do the conversion "by hand". I have been working on several project where MATLAB was used and it was never consider to use any tools to convert the code to C/C++. It was always done "by hand".
I believe to have been the only one who ever investigate into using a tool.
Well there is not straight conversion from matlab to c/c++ You will need to understand the language and the differences between matlab and c/c++ and then start coding it in c/c++. Code a little test a little until it works.
I'm wondering about the best way to deploy R. Matlab has the "matlab compiler" (MCR). There has been discussion about something similar in the past for R that would compile R into C or C++. Does anyone have any experience with the R to C Compiler (RCC) that was developed by John Garvin at Rice?
I've looked into it, and it seems to be the only project that worked on compiling R code into executable code. And as far as I can tell, it isn't still being used.
[Edit 1:]: To be clear, I know that there are C and C++ (and Java, Python, etc.) interfaces to R (rJava, rcpp, Rpy, etc.). I'm wondering about specific ways to compile and deploy R code without installing R in advance.
[Edit 2:]: John Mellor-Crummey tells me that they're still working on RCC and hope to make it available in 4 months or so (at the earliest). I'll update this further if I find anything else out.
A byte code compiler will be part of the R 2.13 release. By default it is not used in this release but it is available; I expect the 2.14 release will by default byte compile all base and recommended packages. The compiler::compile help page and the R Installation and Administration Manual give some more details.
I had forgotten about the Rice project, it has been a while. I think the operational term here is stated at the top of the project page: Last Updated 3/8/06.
And we all know R changes a lot. So I have only the standard few pointers for you:
Luke Tierney, who not only knows a lot about R internals but equally about byte compilers, has been working on such a project. Nothing ready yet, and it would still work in conjunction with the standard R engine.
Stephen Milborrow has the Ra extension to R that works with his just-in-time compiler package jit
my Introduction to High-Performance Computing with R tutorials (most recent tutorial slides from UseR! 2009) covers the profiling, compiling extentions, parallel computing with R, ... part, including
Rcpp and and a bit about
RInside.
In short: there is no way have what you desire specific ways to compile and deploy R code without installing R in advance. Sorry.
Edit/Update (April 2011): Luke's new compiler package will be part of R 2.13.0 (to be released April 2011) but not 'activated' by default which is expected for R 2.14.0 expected for October 2011.
Edit/Update (December 2011): Prof Tierney just release a massive 100+ page paper on the byte-code compiler.
Why do people get the fear when deploying R? I'm fairly sure I've seen this question before.
Installing R is a piece of cake (you don't actually say which OS you care about). For Windows its one .exe. file, run it, say "yes" a few times and its done. I suspect the installer exe probably has flags for unattended installation too.
You may check out the P compiler which implements a subset of R. Especially, lists, matrices, vectors etc. are implemented as well as lsfit, chol, svd, ...
You can download a free version at
www.ptechnologies.org
It speeds up computations substantially.
Best,
AS
I haven't used Garvin's package and don't know what is possible along those lines. However:
Typically people just write computationally intensive functions directly in C/C++/Fortran, after profiling to find the bottlenecks. See the RCpp interface or Calling C functions from R using .C and .Call for examples. The Scythe Statistical Library is also very nice for R users since the syntax/function names are similar.