如何将双精度的Boost uBLAS矢量与复数双因子相乘

  • 本文关键字:双精度 Boost uBLAS c++ boost-ublas
  • 更新时间 :
  • 英文 :


是否可以计算二重的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>’)                        

通过查看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_convertable"的输出,它不是双向的,所以在这种情况下,由于没有从std::complex到double的转换,所以它不起作用。我添加了一个新的定义,只交换了这个模板中参数的顺序,它有效。。。

抱歉英语不好

最新更新