source: trunk/cppad/local/ad_fun.hpp @ 1532

Last change on this file since 1532 was 1532, checked in by bradbell, 11 years ago

trunk: Add option for sparse Jacobians to used vector_set for calculations.

for_sparse_jac.cpp: test forward Jacobian sparsity using vector_set.
rev_sparse_jac.cpp: test reverse Jacobian sparsity using vector_set.
whats_new_09.omh: user's view of the changes.
ad_fun.hpp: add for_jac_sparse_set to private data (move private to front).
for_sparse_jac.hpp: add packed (true or false) option.
rev_sparse_jac.hpp: add packed (true or false) option.

  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1/* $Id: ad_fun.hpp 1532 2009-09-28 11:55:30Z bradbell $ */
2# ifndef CPPAD_AD_FUN_INCLUDED
3# define CPPAD_AD_FUN_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Common Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15/*
16$begin ADFun$$
17$spell
18        xk
19        Ind
20        bool
21        taylor_
22        sizeof
23        const
24        std
25        ind_taddr_
26        dep_taddr_
27$$
28
29$spell
30$$
31
32$section ADFun Objects$$
33
34$index ADFun, object$$
35$index object, ADFun$$
36
37$head Purpose$$
38An AD of $italic Base$$
39$xref/glossary/Operation/Sequence/operation sequence/1/$$
40is stored in an $code ADFun$$ object by its $xref/FunConstruct/$$.
41The $code ADFun$$ object can then be used to calculate function values,
42derivative values, and other values related to the corresponding function.
43
44$childtable%
45        cppad/local/independent.hpp%
46        cppad/local/fun_construct.hpp%
47        cppad/local/dependent.hpp%
48        cppad/local/abort_recording.hpp%
49        omh/seq_property.omh%
50        cppad/local/fun_eval.hpp%
51        cppad/local/drivers.hpp%
52        cppad/local/fun_check.hpp%
53        cppad/local/omp_max_thread.hpp%
54        cppad/local/optimize.hpp%
55        omh/fun_deprecated.omh
56%$$
57
58$end
59*/
60
61
62// BEGIN CppAD namespace
63namespace CppAD {
64
65template <class Base>
66class ADFun {
67public:
68// ------------------------------------------------------------
69private:
70
71        // debug checking number of comparision operations that changed
72        size_t compare_change_;
73
74        // number of taylor_ coefficieint per variable (currently stored)
75        size_t taylor_per_var_;
76
77        // number of columns currently allocated for taylor_ array
78        size_t taylor_col_dim_;
79
80        // number of rows (variables) in the recording (play_)
81        size_t total_num_var_;
82
83        // tape address for the independent variables
84        CppAD::vector<size_t> ind_taddr_;
85
86        // tape address and parameter flag for the dependent variables
87        CppAD::vector<size_t> dep_taddr_;
88
89        // which dependent variables are actually parameters
90        CppAD::vector<bool>   dep_parameter_;
91
92        // the operation sequence corresponding to this object
93        player<Base> play_;
94
95        // results of the forward mode calculations
96        Base *taylor_;
97
98        // results of the forward mode Jacobian sparsity calculations
99        // (n_set() for one of these two will always be zero)
100        vector_pack      for_jac_sparsity_;
101        vector_set       for_jac_sparse_set_;
102
103        // change the operation sequence corresponding to this object
104        template <typename ADvector>
105        void Dependent(ADTape<Base> *tape, const ADvector &y);
106
107// ------------------------------------------------------------
108public:
109        // default constructor
110        ADFun(void) 
111        : total_num_var_(0), taylor_(CPPAD_NULL)
112        { }
113
114        // sequence constructor
115        template <typename ADvector>
116        ADFun(const ADvector &x, const ADvector &y);
117
118        // destructor
119        ~ADFun(void)
120        {       if( taylor_ != CPPAD_NULL )
121                        CPPAD_TRACK_DEL_VEC(taylor_);
122        }
123
124        // assign a new operation sequence
125        template <typename ADvector>
126        void Dependent(const ADvector &y);  // deprecated
127
128        template <typename ADvector>
129        void Dependent(const ADvector &x, const ADvector &y);
130
131        // forward mode sweep
132        template <typename VectorBase>
133        VectorBase Forward(size_t p, const VectorBase &u);
134
135        // reverse mode sweep
136        template <typename VectorBase>
137        VectorBase Reverse(size_t p, const VectorBase &v);
138
139        // forward mode Jacobian sparsity
140        template <typename VectorBool>
141        VectorBool ForSparseJac(
142                size_t q, const VectorBool &Px, bool packed = true
143        );
144        // reverse mode Jacobian sparsity
145        template <typename VectorBool>
146        VectorBool RevSparseJac(
147                size_t q, const VectorBool &Py, bool packed = true
148        );
149        // reverse mode Hessian sparsity
150        template <typename VectorBool>
151        VectorBool RevSparseHes(size_t q, const VectorBool &Py);
152
153        // does this AD operation sequence use VecAD<Base>::reference operands
154        bool use_VecAD(void) const
155        {       return play_.num_rec_vecad_ind() > 0; }
156
157        // number of variables in opertion sequence
158        size_t size_var(void) const
159        {       return total_num_var_; }
160
161        // number of parameters in the operation sequence
162        size_t size_par(void) const
163        {       return play_.num_rec_par(); }
164
165        // number of VecAD indices in the operation sequence
166        size_t size_VecAD(void) const
167        {       return play_.num_rec_vecad_ind(); }
168
169        // number of taylor_ coefficients currently calculated (per variable)
170        size_t size_taylor(void) const
171        {       return taylor_per_var_; } 
172
173        // set number of coefficients currently allocated (per variable)
174        void capacity_taylor(size_t per_var);   
175
176        // number of independent variables
177        size_t Domain(void) const
178        {       return ind_taddr_.size(); }
179
180        // number of dependent variables
181        size_t Range(void) const
182        {       return dep_taddr_.size(); }
183
184        // is variable a parameter
185        bool Parameter(size_t i)
186        {       CPPAD_ASSERT_KNOWN(
187                        i < dep_taddr_.size(),
188                        "Argument to Parameter is >= dimension of range space"
189                );
190                return dep_parameter_[i]; 
191        }
192
193# ifndef NDEBUG
194        // in not NDEBUG case, number of comparison operations that change
195        size_t CompareChange(void) const
196        {       return compare_change_; }
197# endif
198
199        // calculate entire Jacobian
200        template <typename VectorBase>
201        VectorBase Jacobian(const VectorBase &x); 
202
203        // calculate Hessian for one component of f
204        template <typename VectorBase>
205        VectorBase Hessian(const VectorBase &x, const VectorBase &w); 
206        template <typename VectorBase>
207        VectorBase Hessian(const VectorBase &x, size_t i); 
208
209        // forward mode calculation of partial w.r.t one domain component
210        template <typename VectorBase>
211        VectorBase ForOne(
212                const VectorBase   &x ,
213                size_t              j );
214
215        // reverse mode calculation of derivative of one range component
216        template <typename VectorBase>
217        VectorBase RevOne(
218                const VectorBase   &x ,
219                size_t              i );
220
221        // forward mode calculation of a subset of second order partials
222        template <typename VectorBase, typename VectorSize_t>
223        VectorBase ForTwo(
224                const VectorBase   &x ,
225                const VectorSize_t &J ,
226                const VectorSize_t &K );
227
228        // reverse mode calculation of a subset of second order partials
229        template <typename VectorBase, typename VectorSize_t>
230        VectorBase RevTwo(
231                const VectorBase   &x ,
232                const VectorSize_t &I ,
233                const VectorSize_t &J );
234
235        // calculate sparse Jacobians
236        template <typename VectorBase>
237        VectorBase SparseJacobian(const VectorBase &x); 
238
239        template <typename VectorBase, typename VectorBool>
240        VectorBase SparseJacobian(const VectorBase &x, const VectorBool &p); 
241
242        // calculate sparse Hessians
243        template <typename VectorBase>
244        VectorBase SparseHessian(const VectorBase &x, const VectorBase &w); 
245
246        template <typename VectorBase, typename VectorBool>
247        VectorBase SparseHessian(
248                const VectorBase &x, const VectorBase &w, const VectorBool &p
249        ); 
250
251        // Optimize the tape
252        void optimize(void);
253        // ------------------- Deprecated -----------------------------
254
255        // number of variables in opertion sequence
256        size_t Size(void) const
257        {       return total_num_var_; }
258
259        // number of taylor_ coefficients currently stored (per variable)
260        size_t Order(void) const
261        {       return taylor_per_var_ - 1; }
262
263        // amount of memory for each variable
264        size_t Memory(void) const
265        {       size_t pervar  = taylor_col_dim_ * sizeof(Base)
266                + for_jac_sparsity_.memory();
267                size_t total   = total_num_var_ * pervar + play_.Memory();
268                return total;
269        }
270
271        // number of taylor_ coefficients currently calculated (per variable)
272        size_t taylor_size(void) const
273        {       return taylor_per_var_; } 
274};
275// ---------------------------------------------------------------------------
276
277} // END CppAD namespace
278
279// non-user interfaces
280# include <cppad/local/forward0sweep.hpp>
281# include <cppad/local/forward_sweep.hpp>
282# include <cppad/local/reverse_sweep.hpp>
283# include <cppad/local/for_jac_sweep.hpp>
284# include <cppad/local/rev_jac_sweep.hpp>
285# include <cppad/local/rev_hes_sweep.hpp>
286
287// user interfaces
288# include <cppad/local/independent.hpp>
289# include <cppad/local/dependent.hpp>
290# include <cppad/local/fun_construct.hpp>
291# include <cppad/local/abort_recording.hpp>
292# include <cppad/local/fun_eval.hpp>
293# include <cppad/local/drivers.hpp>
294# include <cppad/local/fun_check.hpp>
295# include <cppad/local/omp_max_thread.hpp>
296# include <cppad/local/optimize.hpp>
297
298# endif
Note: See TracBrowser for help on using the repository browser.