00001 # ifndef CPPAD_CAP_TAYLOR_INCLUDED 00002 # define CPPAD_CAP_TAYLOR_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 capacity_taylor$$ 00017 $spell 00018 var 00019 Taylor 00020 $$ 00021 00022 $index Forward, capacity$$ 00023 $index capacity_taylor$$ 00024 $index capacity, Forward$$ 00025 $index memory, control$$ 00026 00027 $section Controlling Taylor Coefficients Memory Allocation$$ 00028 00029 $head Syntax$$ 00030 $syntax%%f%.capacity_taylor(%c%)%$$ 00031 00032 $head Purpose$$ 00033 The Taylor coefficients calculated by Forward mode calculations 00034 are retained in an $xref/ADFun/$$ object for subsequent use during 00035 $xref/Reverse/$$ mode or higher order Forward mode calculations. 00036 This operation allow you to control that amount of memory 00037 that is retained by an AD function object 00038 (for subsequent calculations). 00039 00040 $head f$$ 00041 The object $italic f$$ has prototype 00042 $syntax% 00043 ADFun<%Base%> %f% 00044 %$$ 00045 00046 $head c$$ 00047 The argument $italic c$$ has prototype 00048 $syntax% 00049 size_t %c% 00050 %$$ 00051 It specifies the number of Taylor coefficients that are allocated for 00052 each variable in the AD operation sequence corresponding to $italic f$$. 00053 00054 $head Discussion$$ 00055 A call to $xref/ForwardAny//Forward/$$ with the syntax 00056 $syntax% 00057 %y_p% = %f%.Forward(%p%, %x_p%) 00058 %$$ 00059 uses the lower order Taylor coefficients and 00060 computes the $th p$$ order Taylor coefficients for all 00061 the variables in the operation sequence corresponding to $italic f$$. 00062 (You can determine the number of variables in the operation sequence 00063 using the $xref/SeqProperty/size_var/size_var/$$ function.) 00064 00065 $subhead Pre-Allocating Memory$$ 00066 If you plan to make calls to $code Forward$$ with the maximum value of 00067 $italic p$$ equal to $italic q$$, 00068 it should be faster to pre-allocate memory for these calls using 00069 $syntax% 00070 %f%.capacity_taylor(%c%) 00071 %$$ 00072 with $italic c$$ equal to $latex q + 1$$. 00073 If you do no do this, $code Forward$$ will automatically allocate memory 00074 and will copy the results to a larger buffer, when necessary. 00075 00076 $subhead Freeing Memory$$ 00077 If you no longer need the Taylor coefficients of order $italic q$$ 00078 and higher (that are stored in $italic f$$), 00079 you can reduce the memory allocated to $italic f$$ using 00080 $syntax% 00081 %f%.capacity_taylor(%c%) 00082 %$$ 00083 with $italic c$$ equal to $italic q$$. 00084 00085 $subhead Original State$$ 00086 If $italic f$$ is $cref/constructed/FunConstruct/$$ with the syntax 00087 $syntax% 00088 ADFun<%Base%> %f%(%x%, %y%) 00089 %$$, 00090 there is an implicit call to $code Forward$$ with $italic p$$ equal to zero 00091 and $italic x_p$$ equal to 00092 the value of the 00093 $cref/independent variables/glossary/Tape/Independent Variable/$$ 00094 when the AD operation sequence was recorded. 00095 00096 $head Example$$ 00097 The file 00098 $xref/Forward.cpp/$$ 00099 contains an example and test of these operations. 00100 It returns true if it succeeds and false otherwise. 00101 00102 $end 00103 ----------------------------------------------------------------------------- 00104 */ 00105 00106 // BEGIN CppAD namespace 00107 namespace CppAD { 00108 00109 template <typename Base> 00110 void ADFun<Base>::capacity_taylor(size_t c) 00111 { // temporary indices 00112 size_t i, j, p; 00113 00114 // taylor_per_var, 00115 00116 if( c == TaylorColDim ) 00117 return; 00118 00119 if( c == 0 ) 00120 { if( Taylor != CPPAD_NULL ) 00121 CPPAD_TRACK_DEL_VEC(Taylor); 00122 Taylor = CPPAD_NULL; 00123 taylor_per_var = 0; 00124 return; 00125 } 00126 00127 // Allocate new matrix will requested number of columns 00128 size_t newlen = c * totalNumVar; 00129 Base *newptr = CPPAD_NULL; 00130 newptr = CPPAD_TRACK_NEW_VEC(newlen, newptr); 00131 00132 // number of columns to copy 00133 p = std::min(taylor_per_var, c); 00134 00135 // copy the old data into the new matrix 00136 CPPAD_ASSERT_UNKNOWN( (taylor_per_var == 0) | (Taylor != CPPAD_NULL) ); 00137 for(j = 0; j < p; j++) 00138 { for(i = 0; i < totalNumVar; i++) 00139 { newptr[i * c + j] = Taylor[i * TaylorColDim + j]; 00140 } 00141 } 00142 // free the old memory 00143 if( Taylor != CPPAD_NULL ) 00144 CPPAD_TRACK_DEL_VEC(Taylor); 00145 00146 // use the new pointer 00147 Taylor = newptr; 00148 TaylorColDim = c; 00149 taylor_per_var = p; 00150 00151 return; 00152 } 00153 00154 } // END CppAD namespace 00155 00156 00157 # endif