简体   繁体   English

用Stl矩阵初始化Boost矩阵

[英]Initializing boost matrix with a stl matrix

Is there a better (shorter/alternative) way to do this? 有更好的(简短/替代)方法吗?

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas;
int main () {
    int size = 2;
    matrix<double> a(size, size, 1);
    std::vector<std::vector<double>> s(size,std::vector<double>(size, 2));
    matrix<double> c(size, size);
    for (int i = 0; i < size; ++i) 
        for (int j = 0; j < size; ++j) 
            c(i, j) = s[i][j];
    std::cout << a << std::endl << std::endl;
    std::cout << c << std::endl << std::endl;
    std::cout << prod (a, c) << std::endl << std::endl;
}

Something like matrix<double> c(s); 类似于matrix<double> c(s); would be better I think. 我认为会更好。 I tried (which, interestingly, compiles if I comment out the last three lines). 我尝试了(有趣的是,如果我注释掉最后三行,则可以编译)。

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

#include <vector>

using std::copy;

using namespace boost::numeric::ublas;

int main () {
    int size = 2;
    matrix<double> a(size, size, 1);

    std::vector<std::vector<double>> s(size,std::vector<double>(size, 3));
    vector<double, std::vector<std::vector<double>> > c(s);

    std::cout << a << std::endl << std::endl;
    std::cout << c << std::endl << std::endl;
    std::cout << prod (a, c) << std::endl << std::endl;
}

but got: 但得到:

c++ -g -Wall -O3 -std=gnu++11 -I boost_1_67_0   -c -o main.o main.cpp
In file included from main.cpp:1:
In file included from boost_1_67_0/boost/numeric/ublas/matrix.hpp:18:
boost_1_67_0/boost/numeric/ublas/vector.hpp:223:18: error: no viable conversion from returned value of type 'const std::__1::__vector_base<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > >::value_type' (aka 'const std::__1::vector<double, std::__1::allocator<double> >') to function return type 'const boost::numeric::ublas::type_traits<double>::value_type' (aka 'const double')
                 return data () [i];
                        ^~~~~~~~~~~
boost_1_67_0/boost/numeric/ublas/io.hpp:58:18: note: in instantiation of member function 'boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >::operator()' requested here
            s << v () (0);
                 ^
main.cpp:18:15: note: in instantiation of function template specialization 'boost::numeric::ublas::operator<<<char, std::__1::char_traits<char>, boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > > >' requested here
    std::cout << c << std::endl << std::endl;
              ^
In file included from main.cpp:1:
In file included from boost_1_67_0/boost/numeric/ublas/matrix.hpp:18:
In file included from boost_1_67_0/boost/numeric/ublas/vector.hpp:22:
boost_1_67_0/boost/numeric/ublas/vector_expression.hpp:74:20: error: no matching function for call to object of type 'const boost::numeric::ublas::vector_reference<const boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > > >::referred_type' (aka 'const boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >')
            return expression () (i);
                   ^~~~~~~~~~~~~
boost_1_67_0/boost/numeric/ublas/functional.hpp:833:37: note: in instantiation of member function 'boost::numeric::ublas::vector_reference<const boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > > >::operator()' requested here
                t += e1 () (i, j) * e2 () (j);
                                    ^
boost_1_67_0/boost/numeric/ublas/matrix_expression.hpp:4013:34: note: in instantiation of function template specialization 'boost::numeric::ublas::matrix_vector_prod1<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > >, boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >, double>::apply<boost::numeric::ublas::matrix_reference<const boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > > >, boost::numeric::ublas::vector_reference<const boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > > > >' requested here
            return functor_type::apply (e1_, e2_, i);
                                 ^
boost_1_67_0/boost/numeric/ublas/io.hpp:58:18: note: in instantiation of member function 'boost::numeric::ublas::matrix_vector_binary1<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > >, boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >, boost::numeric::ublas::matrix_vector_prod1<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > >, boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >, double> >::operator()' requested here
            s << v () (0);
                 ^
main.cpp:19:15: note: in instantiation of function template specialization 'boost::numeric::ublas::operator<<<char, std::__1::char_traits<char>, boost::numeric::ublas::matrix_vector_binary1<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > >, boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >, boost::numeric::ublas::matrix_vector_prod1<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > >, boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >, double> > >' requested here
    std::cout << prod (a, c) << std::endl << std::endl;
              ^
boost_1_67_0/boost/numeric/ublas/vector.hpp:230:17: note: candidate function not viable: 'this' argument has type 'const boost::numeric::ublas::vector_reference<const boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > > >::referred_type' (aka 'const boost::numeric::ublas::vector<double, std::__1::vector<std::__1::vector<double, std::__1::allocator<double> >, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double> > > > >'), but method is not marked const
             reference operator () (size_type i) {
                       ^
2 errors generated.
make: *** [main.o] Error 1
[Finished in 1.1s with exit code 2]
[shell_cmd: make]

You need to flatten you std::vector<std::vector<double>> , and then you can copy into your boost::ublas::matrix 您需要展平 std::vector<std::vector<double>> ,然后可以将其复制到boost::ublas::matrix

std::copy(flatten(s.begin(), s.end()), flatten(s.end()), c.begin1()); 
// or c.begin2() if s is column major

Indeed, without a flattened range, you could just "stream" to the matrix: 实际上,如果没有展平的范围,您可以“流”到矩阵:

auto out = c.begin2();
for (auto& v : s)
    out = boost::copy(v, out);

Live On Coliru 生活在Coliru

#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/range/algorithm/copy.hpp>

using namespace boost::numeric::ublas;

int main() {
    int size = 2;
    matrix<double> a(size, size, 1);

    std::vector<std::vector<double> > s(size, std::vector<double>(size, 2));

    matrix<double> c(size, size);
    auto out = c.begin2();
    for (auto& v : s)
        out = boost::copy(v, out);

    std::cout << a << std::endl << std::endl;
    std::cout << c << std::endl << std::endl;

    std::cout << prod(a, c) << std::endl << std::endl;
}

Prints 版画

[2,2]((1,1),(1,1))

[2,2]((2,2),(2,2))

[2,2]((4,4),(4,4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM