Ton van den Heuvel
Ton van den Heuvel

Reputation: 10528

How to multiply a Boost uBLAS vector of doubles with a complex double factor?

Is it possible to calculate the elementwise product of an uBLAS vector of doubles with a complex double? The following code fails to compile since it can not find an overloaded operator *. I would expect it to work since multiplying a double with a complex double is well defined.

#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;
}

Compiling this using GCC 4.6 and Boost 1.55.0 yields the following:

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

Upvotes: 0

Views: 845

Answers (1)

manu
manu

Reputation: 26

By looking at the overloaded * operator in 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 ());
}

It looks like the problem is the output of "is_convertible", it doesn't go both ways, so as there is no conversion from std::complex to double in this case, it doesn't work. I added a new definition only swapping the order of the arguments in this template and it works...

Sorry for bad english

Upvotes: 1

Related Questions