2014-09-01 46 views
0

是否有可能计算一个双精度双精度uBLAS向量的元素乘积?下面的代码无法编译,因为它找不到重载操作符*。我希望它能起作用,因为将双精度乘以一个复数双精确定义。如何将倍数的Boost uBLAS向量乘以复数双因子?

#include <complex> 

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

int main(int argc, char **argv) 
{ 
    using namespace boost::numeric::ublas; 

    vector<double> v(3); 
    for (unsigned i = 0; i < v.size(); ++i) 
    { 
     v (i) = i; 
    } 

    vector<std::complex<double> > w = v * std::complex<double>(3.0, -1.0); 

    return 0; 
} 

这个编译使用GCC 4.6和Boost 1.55.0产生如下:

error: no match for ‘operator*’ (operand types are ‘boost::numeric::ublas::vector<double>’ and ‘std::complex<double>’)       
+0

你如何定义(0,1,2)*(3 - 1)? – Nelfeal 2014-09-01 09:40:37

回答

1

通过vector_expression.hpp看重载运算符*:

// (t * v) [i] = t * v [i] 
template<class T1, class E2> 
BOOST_UBLAS_INLINE 
typename enable_if< is_convertible<T1, typename E2::value_type >,  
typename vector_binary_scalar1_traits<const T1, E2, scalar_multiplies<T1, typename E2::value_type> >::result_type 
>::type 
operator * (const T1 &e1, 
      const vector_expression<E2> &e2) { 
    typedef typename vector_binary_scalar1_traits<const T1, E2, scalar_multiplies<T1, typename E2::value_type> >::expression_type expression_type; 
    return expression_type (e1, e2()); 
} 

它看起来像问题是“is_convertible”的输出,它不是双向的,所以在这种情况下没有将std :: complex转换为double,所以它不起作用。我添加了一个新的定义只交换的参数的顺序在此模板和它的作品...

对不起,我的英文不好