sacado_gradient.hpp

View page source

Calculate Gradient Using Sacado

Syntax

      # include <cmpad/sacado/gradient.hpp>
      cmpad::sacado::gradient < Algo > grad
      grad .setup ( option )
      g = grad ( x )

Purpose

This implements the cpp_gradient interface using Sacado.

Algo

see Algo for the base class.

ADVector

The type Algo ::vector_type is the ADVector type for this gradient.

vector_type

see vector_type for the base class.

scalar_type

see scalar_type for the base class.

setup

see the gradient setup for the base class.

option

This option_t object is used to specify the setup options.

Example

The file xam_gradient_sacado.cpp contains an example and test using this class.

Source Code

# if CMPAD_HAS_SACADO

# include <Sacado.hpp>
# include <cmpad/gradient.hpp>

namespace cmpad { namespace sacado { // BEGIN cmpad::sacado namespace


// gradient
template < template<class ADVector> class Algo> class gradient
: public
cmpad::gradient {
private:
   //
   // ADVector
   typedef Sacado::Rad::ADvar<double>  ADScalar;
   typedef cmpad::vector<ADScalar>     ADVector;
   //
   // option_
   option_t                option_;
   //
   // algo_
   Algo<ADVector>          algo_;
   //
   // ax_
   ADVector                ax_;
   //
   // ay_
   ADVector                ay_;
   //
   // g_
   cmpad::vector<double>   g_;
//
public:
   // scalar_type
   typedef double scalar_type;
   //
   // vector_type
   typedef cmpad::vector<double> vector_type;
   //
   // option
   const option_t& option(void) const override
   {  return option_; }
   // setup
   void setup(const option_t& option) override
   {  //
      // option_
      option_ = option;
      //
      // algo_
      algo_.setup(option);
      //
      // n, m
      size_t n = algo_.domain();
      size_t m = algo_.range();
      //
      // ax_
      ax_.resize(n);
      //
      // ay_
      ay_.resize(m);
      //
      // g_
      g_.resize(n);
   }
   // domain
   size_t domain(void) const override
   {  return algo_.domain(); };
   //
   // operator
   const cmpad::vector<double>& operator()(
      const cmpad::vector<double>& x
   ) override
   {  // ax_
      for(size_t j = 0; j < domain(); ++j)
         ax_[j] = x[j];
      //
      // ay_
      ay_ = algo_(ax_);
      //
      // az
      size_t   m  = algo_.range();
      ADScalar az = ay_[m-1] + 0.0;
      //
      // reverse mode computation of gradient for last computed value
      ADScalar::Gradcomp();
      //
      // g_
      for(size_t j = 0; j < domain(); ++j)
         g_[j] = ax_[j].adj();
      //
      return g_;
   }
};

} } // END cmpad::sacado namespace

# endif // CMPAD_HAS_SACADO