【发布时间】:2018-07-13 09:38:26
【问题描述】:
有没有更好(更短/替代)的方法来做到这一点?
#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;
}
我认为像matrix<double> c(s); 这样的东西会更好。我试过了(有趣的是,如果我注释掉最后三行,它会编译)。
#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]
【问题讨论】:
-
取决于您的要求。
-
我有 stl 矩阵
s,我想使用 boost prod() 进行乘法运算,所以首先我需要 boost 矩阵,它是 bc。 -
定义“更好”。
-
试图在问题的最后定义它。
-
简单的选择是不要在任何地方使用
std::vector<std::vector<...>>。您可以轻松复制出std::vector<double>