C++ 用stl矩阵初始化boost矩阵
有没有更好的更短/替代方法C++ 用stl矩阵初始化boost矩阵,c++,c++11,matrix,boost,C++,C++11,Matrix,Boost,有没有更好的更短/替代方法 #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
#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;
}
类似于矩阵cs;我想这样会更好。有趣的是,我试过,如果我注释掉最后三行,它会编译
#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;
}
但是得到:
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]
您需要创建std::vector,然后可以复制到boost::ublas::matrix中
事实上,如果没有平坦的范围,您可以直接流到矩阵:
auto out = c.begin2();
for (auto& v : s)
out = boost::copy(v, out);
取决于你的要求。我有stl矩阵s,我想用boost prod乘以它,所以首先我需要boost矩阵,它将被定义为b c。定义得更好。试着在问题的末尾定义它。简单的选择是不在任何地方使用std::vector。您可以轻松地从std::vector中复制出来
auto out = c.begin2();
for (auto& v : s)
out = boost::copy(v, out);
#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;
}
[2,2]((1,1),(1,1))
[2,2]((2,2),(2,2))
[2,2]((4,4),(4,4))