template functio need help

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.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Rex_chaos escribió:
// 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>)' "

Try this:

std::transform( v.begin(), v.end(), av.begin(), std::ptr_fun (&
std::abs said:
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?

Do the same you make with myabs, write a myamplitude functor.

Regards.
 
R

Rex_chaos

std::transform( v.begin(), v.end(), av.begin(), std::ptr fun (&
std::abs<double> ) );
Are u sure it's ok? Actually, I have already add 'using namespace
std;' at the beginning of the source, still no help.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Rex_chaos escribió:
Are u sure it's ok? Actually, I have already add 'using namespace
std;' at the beginning of the source, still no help.

I put the std:: for clarity (and it seems I obscured things instead),
but the key is to use abs<double> instead of abs alone.

Regards.
 
R

Rex_chaos

Julián Albo said:
Rex chaos escribi :


I put the std:: for clarity (and it seems I obscured things instead),
but the key is to use abs<double> instead of abs alone.

It's confusing. Take a look at this class

template <typename A>
class Tester
{
...
A myfun(A a) {...}


template <typename FunObj>
void mytransform(FunObj fun)
{
transform( v.begin(), v.end(), v.begin(), fun);
}

void testing(void)
{
mytransform( myfun );
}
}

when calling testing(), error occurs. it's all right unless I replace
myfun with an external function ??????
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Rex_chaos escribió:
template <typename A>
class Tester
{
...
A myfun(A a) {...}


template <typename FunObj>
void mytransform(FunObj fun)
{
transform( v.begin(), v.end(), v.begin(), fun);
}

void testing(void)
{
mytransform( myfun );
}
}

when calling testing(), error occurs. it's all right unless I replace
myfun with an external function ??????

You can't use a member function directly with a standard algorithm, they
expect something that can be called directly.

Regards.
 
R

Rex_chaos

Julián Albo said:
Rex chaos escribi :




You can't use a member function directly with a standard algorithm, they
expect something that can be called directly.

It's annoything. I am trying to have an inner-class casting a function
object. However, no help.
 
T

tom_usenet

It's annoything. I am trying to have an inner-class casting a function
object. However, no help.

You can use a binder, from the <functional> header:

void testing()
{
mytransform(std::bind1st(std::mem_fun(&Tester<A>::myfun), this));
}

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top