Connecting Tech Pros Worldwide Forums | Help | Site Map

template functio need help

Rex_chaos
Guest
 
Posts: n/a
#1: Jul 19 '05
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.

Julián Albo
Guest
 
Posts: n/a
#2: Jul 19 '05

re: template functio need help


Rex_chaos escribió:
[color=blue]
> // 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>)' "[/color]

Try this:

std::transform( v.begin(), v.end(), av.begin(), std::ptr_fun (&
std::abs<double> ) );
[color=blue]
> 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?[/color]

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

Regards.
Rex_chaos
Guest
 
Posts: n/a
#3: Jul 19 '05

re: template functio need help


> std::transform( v.begin(), v.end(), av.begin(), std::ptr fun (&[color=blue]
> std::abs<double> ) );[/color]
Are u sure it's ok? Actually, I have already add 'using namespace
std;' at the beginning of the source, still no help.
Julián Albo
Guest
 
Posts: n/a
#4: Jul 19 '05

re: template functio need help


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

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.
Rex_chaos
Guest
 
Posts: n/a
#5: Jul 19 '05

re: template functio need help


Julián Albo <JULIANALBO@terra.es> wrote in message news:<3F7C4F1E.25F38075@terra.es>...[color=blue]
> Rex chaos escribi :
>[color=green][color=darkred]
> > > std::transform( v.begin(), v.end(), av.begin(), std::ptr fun (&
> > > std::abs<double> ) );[/color]
> > Are u sure it's ok? Actually, I have already add 'using namespace
> > std;' at the beginning of the source, still no help.[/color]
>
> 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.[/color]

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 ??????
Julián Albo
Guest
 
Posts: n/a
#6: Jul 19 '05

re: template functio need help


Rex_chaos escribió:
[color=blue]
> 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 ??????[/color]

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

Regards.
Rex_chaos
Guest
 
Posts: n/a
#7: Jul 19 '05

re: template functio need help


Julián Albo <JULIANALBO@terra.es> wrote in message news:<3F7DBC48.C99FEF40@terra.es>...[color=blue]
> Rex chaos escribi :
>[color=green]
> > template <typename A>
> > class Tester
> > {
> > ...
> > A myfun(A a) {...}
> >[/color]
>[color=green]
> >[/color]
>[color=green]
> > template <typename FunObj>
> > void mytransform(FunObj fun)
> > {
> > transform( v.begin(), v.end(), v.begin(), fun);
> > }
> >[/color]
>[color=green]
> > void testing(void)
> > {
> > mytransform( myfun );
> > }
> > }
> >[/color]
>[color=green]
> > when calling testing(), error occurs. it's all right unless I replace
> > myfun with an external function ??????[/color]
>
> You can't use a member function directly with a standard algorithm, they
> expect something that can be called directly.[/color]

It's annoything. I am trying to have an inner-class casting a function
object. However, no help.
tom_usenet
Guest
 
Posts: n/a
#8: Jul 19 '05

re: template functio need help


On 7 Oct 2003 04:25:17 -0700, rex_chaos@21cn.com (Rex_chaos) wrote:
[color=blue]
>Julián Albo <JULIANALBO@terra.es> wrote in message news:<3F7DBC48.C99FEF40@terra.es>...[color=green]
>> Rex chaos escribi :
>>[color=darkred]
>> > template <typename A>
>> > class Tester
>> > {
>> > ...
>> > A myfun(A a) {...}
>> >[/color]
>>[color=darkred]
>> >[/color]
>>[color=darkred]
>> > template <typename FunObj>
>> > void mytransform(FunObj fun)
>> > {
>> > transform( v.begin(), v.end(), v.begin(), fun);
>> > }
>> >[/color]
>>[color=darkred]
>> > void testing(void)
>> > {
>> > mytransform( myfun );
>> > }
>> > }
>> >[/color]
>>[color=darkred]
>> > when calling testing(), error occurs. it's all right unless I replace
>> > myfun with an external function ??????[/color]
>>
>> You can't use a member function directly with a standard algorithm, they
>> expect something that can be called directly.[/color]
>
>It's annoything. I am trying to have an inner-class casting a function
>object. However, no help.[/color]

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

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

Tom
Closed Thread