template functio need help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rex_chaos

    template functio need help

    Hi all,
    Here is a std::vector containing some std::complex<do uble>. 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(<unkno wn 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

    #2
    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(<unkno wn 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.

    Comment

    • Rex_chaos

      #3
      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.

      Comment

      • Julián Albo

        #4
        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.

        Comment

        • Rex_chaos

          #5
          Re: template functio need help

          Julián Albo <JULIANALBO@ter ra.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(Fun Obj 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 ??????

          Comment

          • Julián Albo

            #6
            Re: template functio need help

            Rex_chaos escribió:
            [color=blue]
            > template <typename A>
            > class Tester
            > {
            > ...
            > A myfun(A a) {...}
            >
            >
            > template <typename FunObj>
            > void mytransform(Fun Obj 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.

            Comment

            • Rex_chaos

              #7
              Re: template functio need help

              Julián Albo <JULIANALBO@ter ra.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(Fun Obj 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.

              Comment

              • tom_usenet

                #8
                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@ter ra.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(Fun Obj 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

                Comment

                Working...