Boost logo

Ublas :

Subject: Re: [ublas] symmetric matrices, row and column-major
From: Tiago Requeijo (tiago.requeijo.dev_at_[hidden])
Date: 2009-02-24 16:16:24


The code below is a test case to make sure the problem shouldn't come from
anything on symmetric_matrix, but from triangular_matrix:

-----------------------------------------------------------------------------------------

#include <iostream>
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/triangular.hpp>
using namespace std;
namespace ublas = boost::numeric::ublas;

int main(int argc, char* argv[])
{
    int sz = 4;
    ublas::symmetric_matrix<int, ublas::upper, ublas::column_major> UpCol
(sz, sz);
    ublas::symmetric_matrix<int, ublas::upper, ublas::row_major> UpRow
(sz, sz);
    ublas::symmetric_matrix<int, ublas::lower, ublas::column_major> LoCol
(sz, sz);
    ublas::symmetric_matrix<int, ublas::lower, ublas::row_major> LoRow
(sz, sz);

    ublas::triangular_matrix<int, ublas::upper, ublas::column_major>
TrUpCol (sz, sz);
    ublas::triangular_matrix<int, ublas::upper, ublas::row_major>
TrUpRow (sz, sz);
    ublas::triangular_matrix<int, ublas::lower, ublas::column_major>
TrLoCol (sz, sz);
    ublas::triangular_matrix<int, ublas::lower, ublas::row_major>
TrLoRow (sz, sz);

    for(int i=0; i<sz; ++i)
        for(int j=i; j<sz; ++j)
        {
            // Symmetric
            UpCol(i,j) = 10*i + j;
            UpRow(i,j) = 10*i + j;
            LoCol(i,j) = 10*i + j;
            LoRow(i,j) = 10*i + j;
            // Triangular
            TrUpCol(i,j) = 10*i + j;
            TrUpRow(i,j) = 10*i + j;
            TrLoCol(j,i) = 10*i + j;
            TrLoRow(j,i) = 10*i + j;
        }

    //get pointers to data
    int* uc = &(UpCol.data()[0]);
    int* ur = &(UpRow.data()[0]);
    int* lc = &(LoCol.data()[0]);
    int* lr = &(LoRow.data()[0]);
    int* tuc = &(TrUpCol.data()[0]);
    int* tur = &(TrUpRow.data()[0]);
    int* tlc = &(TrLoCol.data()[0]);
    int* tlr = &(TrLoRow.data()[0]);

    // upper, column_major
    // storage should be: 0 1 11 2 12 22 3 13 23 33
    int uc_correct[] = {0, 1, 11, 2, 12, 22, 3, 13, 23, 33};

    // upper, row_major
    // storage should be: 0 1 2 3 11 12 13 22 23 33
    int ur_correct[] = {0, 1, 2, 3, 11, 12, 13, 22, 23, 33};

    // lower, column_major
    // storage should be: 0 1 2 3 11 12 13 22 23 33
    int lc_correct[] = {0, 1, 2, 3, 11, 12, 13, 22, 23, 33};

    // lower, row_major
    // storage should be: 0 1 11 2 12 22 3 13 23 33
    int lr_correct[] = {0, 1, 11, 2, 12, 22, 3, 13, 23, 33};

    // Test Symmetric
    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(uc[i] != uc_correct[i])
        {
            cout << "Storage error (Symmetric, Upper, Column major)" <<
endl;
            break;
        }

    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(ur[i] != ur_correct[i])
        {
            cout << "Storage error (Symmetric, Upper, Row major)" << endl;
            break;
        }

    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(lc[i] != lc_correct[i])
        {
            cout << "Storage error (Symmetric, Lower, Column major)" <<
endl;
            break;
        }

    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(lr[i] != lr_correct[i])
        {
            cout << "Storage error (Symmetric, Lower, Row major)" << endl;
            break;
        }

    // Test Triangular
    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(tuc[i] != uc_correct[i])
        {
            cout << "Storage error (Triangular, Upper, Column major)" <<
endl;
            break;
        }

    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(tur[i] != ur_correct[i])
        {
            cout << "Storage error (Triangular, Upper, Row major)" << endl;
            break;
        }

    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(tlc[i] != lc_correct[i])
        {
            cout << "Storage error (Triangular, Lower, Column major)" <<
endl;
            break;
        }

    for(int i=0; i<sz*(sz+1)/2; ++i)
        if(tlr[i] != lr_correct[i])
        {
            cout << "Storage error (Triangular, Lower, Row major)" << endl;
            break;
        }

        return 0;
}

-----------------------------------------------------------------------------------------

On Tue, Feb 24, 2009 at 2:53 PM, Tiago Requeijo <
tiago.requeijo.dev_at_[hidden]> wrote:

> Here it is some code that should do the trick. I tested it with boost
> 1.32, 1.37 and 1.38. It fails on 1.37 and 1.38.
>
>
>
>
> -----------------------------------------------------------------------------------------
>
>
> #include <iostream>
> #include <boost/numeric/ublas/symmetric.hpp>
> using namespace std;
> namespace ublas = boost::numeric::ublas;
>
> int main(int argc, char* argv[])
> {
> int sz = 4;
> ublas::symmetric_matrix<int, ublas::upper, ublas::column_major> UpCol
> (sz, sz);
> ublas::symmetric_matrix<int, ublas::upper, ublas::row_major> UpRow
> (sz, sz);
> ublas::symmetric_matrix<int, ublas::lower, ublas::column_major> LoCol
> (sz, sz);
> ublas::symmetric_matrix<int, ublas::lower, ublas::row_major> LoRow
> (sz, sz);
>
> for(int i=0; i<sz; ++i)
> {
> for(int j=i; j<sz; ++j)
> {
> UpCol(i,j) = 10*i + j;
> UpRow(i,j) = 10*i + j;
> LoCol(i,j) = 10*i + j;
> LoRow(i,j) = 10*i + j;
> }
> }
>
> //get pointers to data
> int* uc = &(UpCol.data()[0]);
> int* ur = &(UpRow.data()[0]);
> int* lc = &(LoCol.data()[0]);
> int* lr = &(LoRow.data()[0]);
>
> // upper, column_major
> // storage should be: 0 1 11 2 12 22 3 13 23 33
> int uc_correct[] = {0, 1, 11, 2, 12, 22, 3, 13, 23, 33};
>
> // upper, row_major
> // storage should be: 0 1 2 3 11 12 13 22 23 33
> int ur_correct[] = {0, 1, 2, 3, 11, 12, 13, 22, 23, 33};
>
> // lower, column_major
> // storage should be: 0 1 2 3 11 12 13 22 23 33
> int lc_correct[] = {0, 1, 2, 3, 11, 12, 13, 22, 23, 33};
>
> // lower, row_major
> // storage should be: 0 1 11 2 12 22 3 13 23 33
> int lr_correct[] = {0, 1, 11, 2, 12, 22, 3, 13, 23, 33};
>
> // Test
> for(int i=0; i<sz*(sz+1)/2; ++i)
> if(uc[i] != uc_correct[i])
> {
> cout << "Storage error (Upper, Column major)" << endl;
> break;
> }
>
> for(int i=0; i<sz*(sz+1)/2; ++i)
> if(ur[i] != ur_correct[i])
> {
> cout << "Storage error (Upper, Row major)" << endl;
> break;
> }
>
> for(int i=0; i<sz*(sz+1)/2; ++i)
> if(lc[i] != lc_correct[i])
> {
> cout << "Storage error (Lower, Column major)" << endl;
> break;
> }
>
> for(int i=0; i<sz*(sz+1)/2; ++i)
> if(lr[i] != lr_correct[i])
> {
> cout << "Storage error (Lower, Row major)" << endl;
> break;
> }
>
> return 0;
> }
>
>
> -----------------------------------------------------------------------------------------
>
>
>
>
> On Tue, Feb 24, 2009 at 2:13 PM, Gunter Winkler <guwi17_at_[hidden]> wrote:
>
>> Tiago Requeijo schrieb:
>> > I'm having a problem with row and column major in symmetric matrices.
>> > I'm probably not understanding correctly how the storage takes place
>> > in memory. I assumed upper+column_major storage was the same as
>> > lower+row_major. Here's an example:
>> >
>> It would be very helpful if you could write a short example program that
>> compares the actual storage with the expected one. I can add this
>> program to the set of unit tests and fix the problem.
>>
>> mfg
>> Gunter
>>
>> _______________________________________________
>> ublas mailing list
>> ublas_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/ublas
>>
>
>