Problems with identity_matrix<> / lu_substitute()

View: New views
4 Messages — Rating Filter:   Alert me  

Problems with identity_matrix<> / lu_substitute()

by Pietro.Mele :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a problem compiling this code when instead of a numerical constant I use a variable or a const int as a size parameter to B(identity_matrix<float>(size)).
And even when it compiles using the numerical value, it runs only with values in the 1-4 range. A 5 will make it crash (Floating exception).

Thank you for your help.

Pietro


#include ...
using namespace boost::numeric::ublas;

int main()
{
    int size = 4;
  
//    matrix<float>  A(4, 4),  B(identity_matrix<float>(4));                     // OK with 1, 2, 3, 4.  Crash with 5, 6, ... (Floating exception)

    matrix<float>  A(size, size),  B(identity_matrix<float>(size));          // I will get a compile time error here ***, even if size is a const int
   
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            A(i,j) =
rand() % 10;
       
    permutation_matrix<> pm(size);
   
    int result = lu_factorize<matrix<float>, permutation_matrix<> >(A, pm);
   
    if ( 0 == result ) {
        lu_substitute(A, pm, B);           // *** Here is where the error is generated
    }
    else
        cout << "Error at row " << result << endl;
}


Error message:

./testLUboost.cpp: In function `int main()':
./testLUboost.cpp:41: warning: the address of `boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > B(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >)', will always evaluate as `true'
./testLUboost.cpp:59: warning: the address of `boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > B(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >)', will always evaluate as `true'
/cadusr/cfd/pmele/libraries/boost_1_40_0/boost/numeric/ublas/lu.hpp: In function `void boost::numeric::ublas::lu_substitute(const M&, const boost::numeric::ublas::permutation_matrix<PMT, PMA>&, MV&) [with M = boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > >, PMT = size_t, PMA = boost::numeric::ublas::unbounded_array<size_t, std::allocator<size_t> >, MV = boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > ()(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >)]':
./testLUboost.cpp:56:   instantiated from here
/cadusr/cfd/pmele/libraries/boost_1_40_0/boost/numeric/ublas/lu.hpp:300: error: no matching function for call to `lu_substitute(const boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > >&, boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > (&)(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >))'
/cadusr/cfd/pmele/libraries/boost_1_40_0/boost/numeric/ublas/lu.hpp: In function `void boost::numeric::ublas::swap_rows(const PM&, MV&) [with PM = boost::numeric::ublas::permutation_matrix<size_t, boost::numeric::ublas::unbounded_array<size_t, std::allocator<size_t> > >, MV = boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > ()(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >)]':
/cadusr/cfd/pmele/libraries/boost_1_40_0/boost/numeric/ublas/lu.hpp:299:   instantiated from `void boost::numeric::ublas::lu_substitute(const M&, const boost::numeric::ublas::permutation_matrix<PMT, PMA>&, MV&) [with M = boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > >,PMT = size_t, PMA = boost::numeric::ublas::unbounded_array<size_t, std::allocator<size_t> >, MV = boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > ()(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >)]'
./testLUboost.cpp:56:   instantiated from here
/cadusr/cfd/pmele/libraries/boost_1_40_0/boost/numeric/ublas/lu.hpp:85: error: `boost::numeric::ublas::matrix<float, boost::numeric::ublas::basic_row_major<size_t, ptrdiff_t>, boost::numeric::ublas::unbounded_array<float, std::allocator<float> > > ()(boost::numeric::ublas::identity_matrix<float, std::allocator<float> >)' is not a class, struct, or union type


Boost 1.40.0
O.S.:  Linux (RedHat)
gcc:  3.4.6
kernel:  2.6.9-55.ELsmp









Pietro Mele
CFD Software Developer
T. +44 1235 777700
F. +44 1235 764705
attwilliams.com
____________________

Williams Grand Prix Engineering Limited. Registered in England no. 1297497. VAT no. GB292559325. This email is confidential. If you are not the addressee, please contact us by reply.


_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...

Re: Problems with identity_matrix<> / lu_substitute()

by Marco Guazzone :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 8, 2009 at 8:03 PM,  <Pietro.Mele@...> wrote:

> I have a problem compiling this code when instead of a numerical constant I
> use a variable or a const int as a size parameter to
> B(identity_matrix<float>(size)).
> And even when it compiles using the numerical value, it runs only with
> values in the 1-4 range. A 5 will make it crash (Floating exception).
>
> Thank you for your help.
>
> Pietro
>
>
> #include ...
> using namespace boost::numeric::ublas;
>
> int main()
> {
>     int size = 4;
>
> //    matrix<float>  A(4, 4),
> B(identity_matrix<float>(4));                     // OK with 1, 2, 3, 4.
> Crash with 5, 6, ... (Floating exception)
>     matrix<float>  A(size, size),  B(identity_matrix<float>(size));
> // I will get a compile time error here ***, even if size is a const int
Try to use a vector instead of a matrix for B.

See the attached code for an example.
Compiled with GCC 4.4.1 + Boost 1.37:
  $ g++ -Wall -Wextra -ansi -pedantic -o pietro pietro.cpp

No warning from compiler output.

Cheers,

-- Marco

[pietro.cpp]

#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <cstdlib>

int main()
{
        //using namespace boost::numeric::ublas;
        //using namespace std;

    int size = 4;
 
//    matrix<float>  A(4, 4),  B(identity_matrix<float>(4));                     // OK with 1, 2, 3, 4.  Crash with 5, 6, ... (Floating exception)
    //matrix<float>  A(size, size),  B(identity_matrix<float>(size));          // I will get a compile time error here ***, even if size is a const int
    boost::numeric::ublas::matrix<float>  A(size, size);
        boost::numeric::ublas::vector<float> B(boost::numeric::ublas::scalar_vector<float>(size, 1));
   
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            A(i,j) = std::rand() % 10;
       
    boost::numeric::ublas::permutation_matrix<> pm(size);
   
    int result = boost::numeric::ublas::lu_factorize<boost::numeric::ublas::matrix<float>, boost::numeric::ublas::permutation_matrix<> >(A, pm);
   
    if ( 0 == result ) {
        boost::numeric::ublas::lu_substitute(A, pm, B);           // *** Here is where the error is generated
    }
    else
        std::cout << "Error at row " << result << std::endl;
}




_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...

Re: Problems with identity_matrix<> / lu_substitute()

by Pietro.Mele :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Marco,

with the vector instead of the matrix it works correctly.

Thank you,
Pietro




Pietro Mele
CFD Software Developer
T. +44 1235 777700
F. +44 1235 764705
attwilliams.com
____________________

Williams Grand Prix Engineering Limited. Registered in England no. 1297497. VAT no. GB292559325. This email is confidential. If you are not the addressee, please contact us by reply.

-----<ublas-bounces@...> wrote: -----

To: ublas mailing list <ublas@...>
From: Marco Guazzone <marco.guazzone@...>
Sent by: <ublas-bounces@...>
Date: 10/09/2009 08:06AM
Subject: Re: [ublas] Problems with identity_matrix<> / lu_substitute()

On Thu, Oct 8, 2009 at 8:03 PM,  <Pietro.Mele@...> wrote:

> I have a problem compiling this code when instead of a numerical constant I
> use a variable or a const int as a size parameter to
> B(identity_matrix<float>(size)).
> And even when it compiles using the numerical value, it runs only with
> values in the 1-4 range. A 5 will make it crash (Floating exception).
>
> Thank you for your help.
>
> Pietro
>
>
> #include ...
> using namespace boost::numeric::ublas;
>
> int main()
> {
>     int size = 4;
>
> //    matrix<float>  A(4, 4),
> B(identity_matrix<float>(4));                     // OK with 1, 2, 3, 4.
> Crash with 5, 6, ... (Floating exception)
>     matrix<float>  A(size, size),  B(identity_matrix<float>(size));
> // I will get a compile time error here ***, even if size is a const int

Try to use a vector instead of a matrix for B.

See the attached code for an example.
Compiled with GCC 4.4.1 + Boost 1.37:
  $ g++ -Wall -Wextra -ansi -pedantic -o pietro pietro.cpp

No warning from compiler output.

Cheers,

-- Marco
_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: pietro.mele@...

_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...

Re: Problems with identity_matrix<> / lu_substitute()

by Marco Guazzone :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 9, 2009 at 12:58 PM,  <Pietro.Mele@...> wrote:
> Marco,
>
> with the vector instead of the matrix it works correctly.
>
> Thank you,
> Pietro
>

There is a case in which you have to pass a matrix to lu_substitute
(instead of a vector); this is for matrix inversion.

If you need to compute the matrix inverse, take a look at

http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?LU_Matrix_Inversion

or in your boost distribution at:

<boost-home>/libs/numeric/ublas/test/test_lu.cpp


Ciao!!

-- Marco
_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...