/home/coin/SVN-release/CoinAll-1.1.0/cppad/cppad/local/reverse.hpp

Go to the documentation of this file.
00001 # ifndef CPPAD_REVERSE_INCLUDED
00002 # define CPPAD_REVERSE_INCLUDED
00003 
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
00006 
00007 CppAD is distributed under multiple licenses. This distribution is under
00008 the terms of the 
00009                     Common Public License Version 1.0.
00010 
00011 A copy of this license is included in the COPYING file of this distribution.
00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00013 -------------------------------------------------------------------------- */
00014 
00015 /*
00016 $begin Reverse$$
00017 $spell
00018         typename
00019         xk
00020         xp
00021         dw
00022         Ind
00023         uj
00024         std
00025         arg
00026         const
00027         Taylor
00028 $$
00029 
00030 $section Reverse Mode$$ 
00031 
00032 $childtable%
00033         omh/reverse.omh
00034 %$$
00035 
00036 $end
00037 -----------------------------------------------------------------------------
00038 */
00039 # include <algorithm>
00040 
00041 // BEGIN CppAD namespace
00042 namespace CppAD {
00043 
00044 template <typename Base>
00045 template <typename VectorBase>
00046 VectorBase ADFun<Base>::Reverse(size_t p, const VectorBase &w) const
00047 {       // temporary indices
00048         size_t i, j, k;
00049 
00050         // number of independent variables
00051         size_t n = ind_taddr.size();
00052 
00053         // number of dependent variables
00054         size_t m = dep_taddr.size();
00055 
00056         Base *Partial = CPPAD_NULL;
00057         Partial       = CPPAD_TRACK_NEW_VEC(totalNumVar * p, Partial);
00058 
00059         // update maximum memory requirement
00060         // memoryMax = std::max( memoryMax, 
00061         //      Memory() + totalNumVar * p * sizeof(Base)
00062         // );
00063 
00064         // check VectorBase is Simple Vector class with Base type elements
00065         CheckSimpleVector<Base, VectorBase>();
00066 
00067         CPPAD_ASSERT_KNOWN(
00068                 w.size() == m,
00069                 "Argument w to Reverse does not have length equal to\n"
00070                 "the dimension of the range for the corresponding ADFun."
00071         );
00072         CPPAD_ASSERT_KNOWN(
00073                 p > 0,
00074                 "The first argument to Reverse must be greater than zero."
00075         );  
00076         CPPAD_ASSERT_KNOWN(
00077                 taylor_per_var >= p,
00078                 "Less that p Taylor coefficients are currently stored"
00079                 " in this ADFun object."
00080         );  
00081 
00082         // initialize entire Partial matrix to zero
00083         for(i = 0; i < totalNumVar; i++)
00084                 for(j = 0; j < p; j++)
00085                         Partial[i * p + j] = Base(0);
00086 
00087         // set the dependent variable direction
00088         // (use += because two dependent variables can point to same location)
00089         for(i = 0; i < m; i++)
00090         {       CPPAD_ASSERT_UNKNOWN( dep_taddr[i] < totalNumVar );
00091                 Partial[dep_taddr[i] * p + p - 1] += w[i];
00092         }
00093 
00094         // evaluate the derivatives
00095         ReverseSweep(
00096                 p - 1, totalNumVar, &Rec, TaylorColDim, Taylor, p, Partial
00097         );
00098 
00099         // return the derivative values
00100         VectorBase value(n * p);
00101         for(j = 0; j < n; j++)
00102         {       CPPAD_ASSERT_UNKNOWN( ind_taddr[j] < totalNumVar );
00103 
00104                 // independent variable taddr equals its operator taddr 
00105                 CPPAD_ASSERT_UNKNOWN( Rec.GetOp( ind_taddr[j] ) == InvOp );
00106 
00107                 // by the Reverse Identity Theorem 
00108                 // partial of y^{(k)} w.r.t. u^{(0)} is equal to
00109                 // partial of y^{(p-1)} w.r.t. u^{(p - 1 - k)}
00110                 for(k = 0; k < p; k++)
00111                         value[j * p + k ] = 
00112                                 Partial[ind_taddr[j] * p + p - 1 - k];
00113         }
00114 
00115         // done with the Partial array
00116         CPPAD_TRACK_DEL_VEC(Partial);
00117 
00118         return value;
00119 }
00120         
00121 
00122 } // END CppAD namespace
00123         
00124 
00125 # endif

Generated on Sun Nov 14 14:06:33 2010 for Coin-All by  doxygen 1.4.7