It seems that I’m doing here something terribly wrong. Can you help me? The aim is to use inner_product for complex vectors.
#include<iostream>
#include<vector>
#include <numeric>
#include <complex>
using namespace std;
template<class T>
complex< T > complex_prod(complex< T > a, complex< T > b)
{
return conj<T>(a)*b;
}
template<class T>
complex< T > add_c(complex< T > a, complex< T > b)
{
return a+b;
}
int main()
{
complex<double> c1(1.,3.);
complex<double> c2(2.,4.);
vector<complex<double> > C1(3,c1);
vector<complex<double> > C2(3,c2);
cout<<inner_product(C1.begin(),C2.end(),C2.begin(),0.,add_c<double>,complex_prod<double>) <<endl;
return 0;
}
I don’t see why there is a conversion problem, everything seems to be defined and the iteration should make no problem.
The problem is that
inner_productneeds to know the type of the initial value, so you need to pass it anstd::complexinstead of a0.:or simply,
Although an
std::complex<double>can be implicitly constructed from a single numeric typethe
inner_producttemplate looks likeso there is a template parameter for the
value. So there is no way the compiler can know you meanstd::complex<double>here, it just interprets0.asdouble.