Boost logo

Boost :

From: deane_yang (Deane_Yang_at_[hidden])
Date: 2002-02-12 12:08:05


I took at look at your code; it's exactly what I want.

Couldn't the library be implemented
instead using the order of the Taylor series as a template
parameter? The 0th, 1st, and 2nd order methods could
be implemented explicitly using template specialization,
and the Nth order could be implemented using general formulas.

The interface I have in mind looks like the following:
(WARNING: completely untested code. I have no idea if the
overloads will be resolved properly.)

template<unsigned int Order, typename FloatingType>
class differential_number
{
public:
  typedef differential_number<Order, FloatingType> this_type;
  typedef FloatingType floating_type;
  
  differential_number();
  
  template <typename OtherBuiltinType>
  explicit differential_number(OtherBuiltinType a);

  // Allow explicit conversion from one order to another
  template <unsigned OtherOrder, typename OtherFloatingType>
  explicit differential_number(const differential_number<OtherOrder,
OtherFloatingType>& other);

  // Default copy constructor and assignment
  // Default destructor

  // Addition and subtraction
  const this_type& operator+=(floating_type term);
  const this_type& operator+=(const this_type& term);
  const this_type& operator-=(floating_type term);
  const this_type& operator-=(const this_type& term);

  // Multiplication and division
  const this_type& operator*=(floating_type factor);
  const this_type& operator*=(const this_type& factor);
  const this_type& operator/=(floating_type divisor);
  const this_type& operator/=(const this_type& divisor);

private:
  FloatingType f[Order];
};

template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> pow(const
differential_number<Order,FloatingType>& base,
                                           PowerType power);

template<unsigned int Order, typename FloatingType, typename BaseType>
differential_number<Order,FloatingType> pow(BaseType base,
                                           const
differential_number<Order,FloatingType>& power);

template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> pow(const
differential_number<Order,FloatingType>& base,
                                           const
differential_number<Order,FloatingType>& power);

template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> sqrt(const
differential_number<Order,FloatingType>& f);

template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> exp(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> sin(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> cos(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> tan(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> sinh(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> cosh(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> tanh(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> log(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> asin(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> acos(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> atan(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> asinh(const
differential_number<Order,FloatingType>& f);
    
template<unsigned int Order, typename FloatingType, typename
PowerType>
differential_number<Order,FloatingType> acosh(const
differential_number<Order,FloatingType>& f);


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk