CppAD: A C++ Algorithmic Differentiation Package 20110419
forward.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_FORWARD_INCLUDED
00003 # define CPPAD_FORWARD_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Common Public License Version 1.0.
00011 
00012 A copy of this license is included in the COPYING file of this distribution.
00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00014 -------------------------------------------------------------------------- */
00015 
00016 /*
00017 $begin Forward$$
00018 
00019 $section Forward Mode$$
00020 
00021 $childtable%
00022         omh/forward.omh%
00023         cppad/local/cap_taylor.hpp%
00024         example/forward.cpp
00025 %$$
00026 
00027 $end
00028 -----------------------------------------------------------------------------
00029 */
00030 
00031 // documened after Forward but included here so easy to see
00032 # include <cppad/local/cap_taylor.hpp>
00033 
00034 // BEGIN CppAD namespace
00035 namespace CppAD {
00036 
00037 template <typename Base>
00038 template <typename Vector>
00039 Vector ADFun<Base>::Forward(size_t p, const Vector &up)
00040 {       // temporary indices
00041         size_t i, j;
00042 
00043         // number of independent variables
00044         size_t n = ind_taddr_.size();
00045 
00046         // number of dependent variables
00047         size_t m = dep_taddr_.size();
00048 
00049         // check Vector is Simple Vector class with Base type elements
00050         CheckSimpleVector<Base, Vector>();
00051 
00052         CPPAD_ASSERT_KNOWN(
00053                 up.size() == n,
00054                 "Second argument to Forward does not have length equal to\n"
00055                 "the dimension of the domain for the corresponding ADFun."
00056         );
00057         CPPAD_ASSERT_KNOWN(
00058                 p <= taylor_per_var_,
00059                 "The number of taylor_ coefficient currently stored\n"
00060                 "in this ADFun object is less than p."
00061         );  
00062 
00063         // check if the taylor_ matrix needs more columns
00064         if( taylor_col_dim_ <= p )
00065                 capacity_taylor(p + 1);
00066         CPPAD_ASSERT_UNKNOWN( taylor_col_dim_ > p );
00067 
00068         // set the p-th order taylor_ coefficients for independent variables
00069         for(j = 0; j < n; j++)
00070         {       CPPAD_ASSERT_UNKNOWN( ind_taddr_[j] < total_num_var_ );
00071 
00072                 // ind_taddr_[j] is operator taddr for j-th independent variable
00073                 CPPAD_ASSERT_UNKNOWN( play_.GetOp( ind_taddr_[j] ) == InvOp );
00074 
00075                 // It is also variable taddr for j-th independent variable
00076                 taylor_[ind_taddr_[j] * taylor_col_dim_ + p] = up[j];
00077         }
00078 
00079         // evaluate the derivatives
00080 # if CPPAD_USE_FORWARD0SWEEP
00081         if( p == 0 ) compare_change_ = forward0sweep(
00082                 true, n, total_num_var_, &play_, taylor_col_dim_, taylor_
00083         );
00084         else 
00085         forward_sweep(
00086                 true, p, n, total_num_var_, &play_, taylor_col_dim_, taylor_
00087         );
00088 # else
00089         size_t compare_change = forward_sweep(
00090                 true, p, n, total_num_var_, &play_, taylor_col_dim_, taylor_
00091         );
00092         if( p == 0 )
00093                 compare_change_ = compare_change;
00094 # endif
00095 
00096         // return the p-th order taylor_ coefficients for dependent variables
00097         Vector vp(m);
00098         for(i = 0; i < m; i++)
00099         {       CPPAD_ASSERT_UNKNOWN( dep_taddr_[i] < total_num_var_ );
00100                 vp[i] = taylor_[dep_taddr_[i] * taylor_col_dim_ + p];
00101         }
00102 
00103         // now we have p + 1  taylor_ coefficients per variable
00104         taylor_per_var_ = p + 1;
00105 
00106         return vp;
00107 }
00108 
00109 } // END CppAD namespace
00110 
00111 
00112 # endif