essay on programming languages, computer science, information techonlogies and all.

Monday, January 13, 2014

Employ matrix operation in C++ using MTL4

Developing algorithm using matrix asks for number of tools. First a straight forward and proven tool is needed like Matlab. It is better to minimize unknowns when a new algorithm is tried. You have to be sure that the result is solely come from one source - mostly from maturing algorithm. And that is the reason why you sticks to a tool that you are familiar with.

After making up an algorithm with matrix, it is time to make it a working code. It is not desirable to stick to the algorithm development tool as it comes together with a big library and license. It is MTL4 's turn to play.

MTL stands for Matrix Template Library. It is C++ template based and make no dependencies on a library file nor dll. Hence it goes in a small foot print and can be deployed in any platform.

But as template library goes, the most of the work comes down to a successful compilation of the source code. There are number of things to note. First, download appropriate zip files and copies files to an appropriate folders. In Windows, you can download from here and copies whole usr folder and name it MTL4. Second there is conflict in variable names between MTL4 and WinDefs. Refer below code snippet to fix the compilation error in Windows.
// Variable name IN below conflicts with WinDefs.h. Change it to something else like InMatrix
// inlcude/boost/numeric/mtl/operaiton/eigenvalue.hpp
eigenvalue_solver(const Matrix& IN) : ncols(num_cols(IN)), nrows(num_rows(IN)) {
zero= math::zero(IN[0][0]);
one= math::one(IN[0][0]);
R = hessenberg(IN);
// inlcude/boost/numeric/mtl/operaiton/qr_givens.hpp
qr_givens_solver(const Matrix& IN) : R(IN), G(2,2), Q(num_cols(IN), num_rows(IN)) {
view raw gistfile1.txt hosted with ❤ by GitHub
There are number of warning that can be ignored - I know I am lazy on it and will pay the debt at someday but until then. The code to include the MTL4 will be like below.
#ifndef _SCL_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS // remove warning 4996
#endif
#pragma warning( push )
#pragma warning( disable: 4522 4996 4146 4355)
#include <boost\numeric\mtl\mtl.hpp>
#pragma warning( pop )
view raw include_mtl.cpp hosted with ❤ by GitHub
Now time to port the algorithm code to C++ code using MTL4. Refer below code snippet.
typedef mtl::dense2D<double> Matrix;
typedef mtl::dense_vector<double> Vector;
// matrix creation using array
double ct = cos(theta), st = sin(theta);
double m[][3]=
{ {ct, -st, x },
{st, ct, y },
{0, 0, 1 } };
Matrix R_c2p( m );
// inverse
Matrix R_p2c( inv( R_c2p ) );
// fill up matrix
Matrix M_p(3,1);
M_p[0][0] = MP_p.GetX(); M_p[1][0] = MP_p.GetY(); M_p[2][0] = 1;
// multiplication
Matrix u(3,1);
u = R_p2c * M_p;
// solve
Matrix Ut( trans(U) );
Vector UtV( Ut*V );
Matrix UtU( Ut*U );
Vector X( lu_solve( UtU, UtV ) );
As above code shows, the C++ code can be very similar to a code in the Matlab thanks to operator overloading in MTL4. In here, very limited features of MTL4 is shown and you can find more in the MTL4.

No comments: