R
Rex_chaos
Hi all,
Here is a std::vector containing some std::complex<double>. I would
like to take the amplitude of the complex array.
i.e. v = [c1, c2, c3, c4 ...]
amp of v = [abs(c1)^2, abs(c2)^2, abs(c3)^2, abs(c4)^2,
abs(c5)^2]
I try a template function but didn't work
std::vector< complex<double> > v(5);
std::vector< double > av(5);
// initialize v here
....
transform( v.begin(), v.end(), av.begin(), ptr_fun(abs) );
Here abs is a template function defined in std. The error message is
" no matching function for call to `ptr_fun(<unknown type>)' "
It seems that the system don't know the function abs, so I try this
template <typename T>
T myabs(complex<T> c)
{
return abs(c);
}
transform( v.begin(), v.end(), av.begin(), ptr_fun(myabs) );
Now, it works fine. I wonder why it fails in the first transform
???????
Anyway, what I get now is only the abs of the complex element (|u|)
but not the amplitude (|u|^2). The problem can obviously be solved by
taking the transform again like
transform( av.begin(), av.end(), av.begin(), bind2nd(ptr_fun(pow),
2.0) );
However, it really take times when the above code is running. If it's
possible to obtain the last result with only one trasnform? And how?
Thanks in advance.
Here is a std::vector containing some std::complex<double>. I would
like to take the amplitude of the complex array.
i.e. v = [c1, c2, c3, c4 ...]
amp of v = [abs(c1)^2, abs(c2)^2, abs(c3)^2, abs(c4)^2,
abs(c5)^2]
I try a template function but didn't work
std::vector< complex<double> > v(5);
std::vector< double > av(5);
// initialize v here
....
transform( v.begin(), v.end(), av.begin(), ptr_fun(abs) );
Here abs is a template function defined in std. The error message is
" no matching function for call to `ptr_fun(<unknown type>)' "
It seems that the system don't know the function abs, so I try this
template <typename T>
T myabs(complex<T> c)
{
return abs(c);
}
transform( v.begin(), v.end(), av.begin(), ptr_fun(myabs) );
Now, it works fine. I wonder why it fails in the first transform
???????
Anyway, what I get now is only the abs of the complex element (|u|)
but not the amplitude (|u|^2). The problem can obviously be solved by
taking the transform again like
transform( av.begin(), av.end(), av.begin(), bind2nd(ptr_fun(pow),
2.0) );
However, it really take times when the above code is running. If it's
possible to obtain the last result with only one trasnform? And how?
Thanks in advance.