forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 2:02 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: complex matrix inversion - funny error??
PostPosted: Wed Jan 19, 2011 6:26 pm 
Offline

Joined: Wed Jan 19, 2011 4:18 pm
Posts: 11
Hello,

In one of my projects at the university, I was searching for a linear algebra library and came across ALGLIB. The packages and subpackages with source code appeared great.

I included all the required files, to do a complex matrix inversion, in my project, and started testing it starting with a 2x2 matrix, but found that I got errors in resulting inverted matrix. Here is the gist of my code, which is just a copy of the examples given in the manual. Funny thing is that in each and every case, half of the elements in the resulting matrix, were right and the other half were wrong.

ae_int_t info;
matinvreport rep;
complex_2d_array a = "[[1i,-1],[1i,1]]"; // 2x2 complex array
complex_2d_array a1 ="[[1i,-1],[1i,1]]"; // copy of the 2x2 complex array to display in the output
cmatrixinverse(a1, info, rep);


real_2d_array r = "[[1,2],[3,4]]";
real_2d_array r1 = "[[1,2],[3,4]]";
rmatrixinverse(r1, info, rep);

And for the display, I use a1.tostring(4).c_str() and r1.tostring(4).c_str(). The code is part of a huge project, that has hundreds of source and header files, used to reconstruct data from a medical MRI scanner. And in the tailor made compiler, a few files of relevance are compiled. The above code gets compiled without any error. Display of the original matrices are good and I even tried to copy a large complexd matrix to another and it worked well.

I would have expected the inverted matrices to be [[-0.5i,-0.5i],[-0.5,0.5]] for the complex array and [[-2, 1],[1.5, -0.5]] for the real array, but the results were [[-1i, -0.5i],[0,0.5]] and [[-2,0.33],[1.5,0]].

In the complex case, the 2nd column turned out to be good, and in the real case, the first column turned out to be good.

It will be great to know where I am going wrong. I am sure its a silly error somewhere (as I have read posts in the forum where people have successfully inverted very large matrices), but I have banged my head on the monitor for nearly two hours trying to figure out the mistake..

thanks
ERUMAI


Top
 Profile  
 
 Post subject: Re: complex matrix inversion - funny error??
PostPosted: Thu Jan 20, 2011 9:48 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Hello!

Very strange error - I've tested same code on my computer, and it works and returns correct answer. Can you post here:
* information about CPU and OS
* information about compiler being used and compiler settings
* project files (if there are any)


Top
 Profile  
 
 Post subject: Re: complex matrix inversion - funny error??
PostPosted: Sat Jan 22, 2011 12:58 pm 
Offline

Joined: Wed Jan 19, 2011 4:18 pm
Posts: 11
Hi Sergey,

Thanks for the reply..I do the programming part in visual studio 98 and I compile it in a custom made environment (that uses MSDEV98..this environment comes as a part of the package for MRI sequences programming). This generates seperate .dll files for windows and linux, because a part of the package has to be run in host(which is windows based) and another part in the imager(which is linux)..well..thats how MRi sequences programming packages are organised nowadays..

Now the part in which I have this funny issue is executed in a linux 64 bit machine ..well..infact for day to day programming and compilation, we need not go to an MRI scanner..we have an emulator, where from the sequences programing environment emulates linux from windows..The compilation (which is done in windows) generates .dlls for windows and .so files for linux..and this .so file is then passed on to the linux emulator for viewing the results..

thanks
Erumai

P.S: the delay in my reply was due to the reason that i cant access my machine everyday..


Top
 Profile  
 
 Post subject: Re: complex matrix inversion - funny error??
PostPosted: Sat Jan 22, 2011 1:16 pm 
Offline

Joined: Wed Jan 19, 2011 4:18 pm
Posts: 11
Hi Sergey,

Further to my previous post, I forgot to mention a thing..

I am not much into programming, but this is what I encountered when I first included alglib library in my code..The files are included int he following order..

#include "ap.h"
#include "ap.cpp"
#include "alglibinternal.h"
#include "alglibinternal.cpp"
#include "alglibmisc.h"
#include "alglibmisc.cpp"
#include "linalg.h"
#include "linalg.cpp"

using namespace alglib;

But I encountered compilation errors like 'ae_vector_init_copy' undeclared identifier..Similarly there were lot of declaration/definition issues. So I had to add another line in your file 'ap.h', in the declaration of 'alglib' namespace as follows..

/////////////////////////////////////////////////////////////////////////
//
// THIS SECTION CONTAINS DECLARATIONS FOR C++ RELATED FUNCTIONALITY
//
/////////////////////////////////////////////////////////////////////////

namespace alglib
{

using namespace alglib_impl; //added by ERUMAI

typedef alglib_impl::ae_int_t ae_int_t;

/********************************************************************
Class forwards
********************************************************************/
class complex;

ae_int_t vlen(ae_int_t n1, ae_int_t n2);
.
.
.


Could this somehow be an issue?

thanks
Erumai


Top
 Profile  
 
 Post subject: Re: complex matrix inversion - funny error??
PostPosted: Tue Jan 25, 2011 5:17 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Code:
#include "ap.h"
#include "ap.cpp"
#include "alglibinternal.h"
#include "alglibinternal.cpp"
#include "alglibmisc.h"
#include "alglibmisc.cpp"
#include "linalg.h"
#include "linalg.cpp"


Am I right thinking that you tried to compile all ALGLIB as one unit? Those compilation errors can be caused by this. Try to compile units separately and without your modifications. I.e. ap.cpp should be compiled to obtain ap.obj, alglibmisc.cpp should be compiled to alglibmisc.obj and so on.


Top
 Profile  
 
 Post subject: Re: complex matrix inversion - funny error??
PostPosted: Wed Jan 26, 2011 4:49 pm 
Offline

Joined: Wed Jan 19, 2011 4:18 pm
Posts: 11
Hello Sergey,

Thanks for the mails till now. My matrix inversion finally works, though I had to use a circuitous route, by first using cmatrixlusolve() to obtain the LU decomposition and then using the result in cmatrixlusolvem() to solve for the equation A.X=B, setting B as a identity matrix.

And I checked the same for matrices upto 512x512 and the results are in line with matlab.

And regarding your suggestion before to compile different files seperately..well..I could not do it, as my task is part of a huge project, that has its own custom-made compiler environment and no seperate .obj files are generated for the files included. I include ALGLIB files in my code, compile it to generate one .obj file (for windows and and one .o for linux) and then link it to get .dll and .so files, which are then simulated in a MRI host/imager emulator.

Regarding my other post, I am yet to try bidiagonalization, as my next priority is to obtain pseudo-inverse (using the matrix inverse result) and eigen decomposition of matrices.

thanks
Erumai


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 35 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group