Re: another quick question

Sven Olsen <[email protected]> Tue, 02 Dec 2003 23:38:07 -0500
Newsgroups gmane.comp.lib.mtl.devel
Message-ID <[email protected]>
At 07:49 PM 12/2/2003 -0500, you wrote:
>I think this example code should swap the two rows in constant time.

Hmm.  Well, the example code I gave crashes in terrible ways...  I assumed 
it was breaking because I wasn't using the library properly, but if you 
think it should work, then something is going wrong...

I've done a little investigation, and it looks like the problems that I've 
been running into with row swapping are most likely due to the fact that 
the mtl::swap functions will swap the value vectors of two compressed 
arrays, but not the index vectors (which leads to all sorts of interesting 
problems).
I have started hacking around with a solution to use in my own code, but I 
don't know my way around the MTL source, so even if I get something 
working, it's probably not going to be pretty.

 From what I've seen, this is something that anyone who knew the MTL source 
fairly well would have little trouble fixing.

Here's the code that crashes in terrible ways:

#include "mtl/matrix.h"
#include "mtl/mtl.h"
#include "mtl/utils.h"

using namespace mtl;

typedef matrix< double, rectangle<>, array< compressed<> >, 
row_major >::type Matrix;

int main ()
{
   using std::cout;
   using std::endl;

   //initialize the matrix
   const int size=4;
   Matrix A(size,size);
   A(0,0)=1;
   A(1,2)=2;
   A(3,1)=5;
   A(0,3)=3;

   //swap the first two rows
   mtl::swap(A[0],A[1]);

   //output the results
   for (int ii=0; ii<A.nrows(); ii++) {
     for (int j=0; j<A.ncols(); j++) {
       cout.width(6);
       cout << A(ii, j) << " ";
     }
         cout << endl;
   }

   return 0;
}

The output that I get is:

2       0       0       2.10535e-314
0       0       1       0
0       0       0       0
0       5       0       0

I then crash while trying to execute the cleanup function "_free_dbg".

A little more experimenting indicates that while I am swapping the value 
vectors, I'm not swapping the index vectors-
So the matrix defined by
   Matrix A(2,2);
   A(0,0)=1;
   A(1,2)=2;
Swaps rows without trouble (which makes sense because the index vectors are 
the same for each row)

While swapping the rows in
   Matrix A(2,4);
   A(0,0)=1;
   A(1,2)=2;
   A(1,1)=6;
   A(0,3)=3;

Yeilds
6       0       0       2
0       1       3       0

when it clearly should give
0       6       2       0
1       0       0       3

Thanks for your help,
         Sven


>On Dec 2, 2003, at 5:31 PM, Sven Olsen wrote:
>
>>If I am working with a matrix of type:
>>
>>typedef matrix< double, rectangle<>, array< compressed<> >, 
>>row_major >::type Matrix;
>>
>>what is the easiest way to swap two of the rows?
>>
>>I.E., I want to do something like
>>Matrix A(4,4);
>>/*initialize A*/
>>swap(A[0],A[1]);
>>
>>Thanks,
>>         Sven
>>
>>_______________________________________________
>>This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/
>
>_______________________________________________
>This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/

_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/