Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Can I use the C++ pow function on an STL vector

Options
  • 27-09-2009 2:12pm
    #1
    Hosted Moderators Posts: 7,486 ✭✭✭


    I have the following templated code:
    template <class T>
    T hurtSigmaMuscle(T f)
    {
    	T f2 = pow(f,2);
    	T f4 = pow(f,4);
    	
    	return (0.0762)+
    	(6.45e-07 * f2 / (1 + pow(f/69,2)))+
        (1.06e-10 * f2 / (1 + pow(f/43e3,2)))+
        (9.88e-13 * f4 / (1 + pow(f/670e3,2)))+
        (7.75e-18 * f2 / (1 + pow(f/230e6,2)))+
        (1.27e-19 * f2 / (1 + pow(f/20e9,2)));
    }
    

    which works fine for Double, Float etc. I tried sending in a double vector, and I get an error which appears to say that the pow math function can't do vectors. Is this true? I've tried looking around but I can't find anything concrete on it.


Comments

  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    In the "cmath" include theres:
    template<typename _Tp, typename _Up>
        inline
        typename __gnu_cxx::__promote_2<
        typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    				    && __is_arithmetic<_Up>::__value,
    				    _Tp>::__type, _Up>::__type
        pow(_Tp __x, _Up __y)
        {
          typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
          return pow(__type(__x), __type(__y));
        }
    

    Which could be the closest to what you're looking for, depends on whether vector classifies for "__is_arithmetic<>", which i'd say it doesn't, though it may contain many individual objects which do.

    Really though i'm not sure how you'd expect to get the power of a vector? Unless perhaps it gets the power of eery value in the vector and returns another vector?
    In which case not too sure but I'd say it might be easier to just overload the function?


  • Hosted Moderators Posts: 7,486 ✭✭✭Red Alert


    I just did it with a for_each; found out I need to declare and templated functions in the header - otherwise they won't get instantiated :)

    Thanks!


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Can you flag this as Solved then? thank you.


Advertisement