Re: returning a ublas matrix from a function

Nasos Iliopoulos <[email protected]>
Newsgroups gmane.comp.lib.boost.ublas
Message-ID <[email protected]>
Sannan,
prior to diving in ublas I would suggest spending some time learning 
C++, since it is not a trivial language. As long as you have some 
programming experience a good introductory book would be "Accelerated 
C++ by Koening and Moo".

Anyway If you are using a C++11 compiler this will work and is 
preferable from the C array (please note that there are a few 
performance improvements that might take place here):
[for gcc compile with: g++ main.cpp -o myprog -std=c++11]

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <array>

using namespace boost;
using namespace boost::numeric::ublas;

typedef boost::numeric::ublas::matrix<double> BMT;

std::array<BMT, 3> fun(int w,int h)
{
     std::array<BMT, 3> M ={
         scalar_matrix<double>(w, h),
         scalar_matrix<double>(2*w, h),
         scalar_matrix<double>(3*w, h)
     };

     return M;
}

int main()
{

     unsigned w,h;
     w = 3;
     h = 3;

     auto matrices = fun( w, h);

     std::cout << matrices[0] << std::endl;
     std::cout << matrices[1] << std::endl;
     std::cout << matrices[2] << std::endl;

     return 0;
}

Best,
Nasos

On 08/05/2014 09:00 AM, sanann wrote:
> The basic thing that i want to do is
> 1. have a bunch of matrices either in a function or in a class.
> 2. these would be manipulated and the output needs to be returned inorder to
> use it for further manipulation.
> 3. so assign 3 matrices into an array.
> 4. now th problem i face is using this array in an external code.
>
> for example.
>
> #include <boost/numeric/ublas/matrix.hpp>
> #include <boost/numeric/ublas/io.hpp>
>
> using namespace boost;
> using namespace boost::numeric::ublas;
>
>
> typedef boost::numeric::ublas::matrix<double> BMT;
>
> BMT fun(int w,int h)
>    {
>
>      BMT M[3]=
>            {
>          scalar_matrix<double>(w, h),
>          scalar_matrix<double>(w, h),
>          scalar_matrix<double>(w, h)
>      };
>      std::cout<<M[0]<<std::endl;
>    return M[0];
>    }
>
> here i want to have all the matrices stored in array M how do i do that?
>
> int main()
> {
>
>      unsigned w,h;
>      w = 3;
>      h = 3;
>
>      fun(w,h);
>
>      std::cout<<fun<<std::endl;
>      return 0;
> }
>
> it would be of great help if you can guide me
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/returning-a-ublas-matrix-from-a-function-tp4666050p4666070.html
> Sent from the Boost - uBLAS mailing list archive at Nabble.com.
> _______________________________________________
> ublas mailing list
> [email protected]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.