1 # ifndef CPPAD_CORE_AD_FUN_HPP
2 # define CPPAD_CORE_AD_FUN_HPP
3 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
5 
6 CppAD is distributed under the terms of the
7              Eclipse Public License Version 2.0.
8 
9 This Source Code may also be made available under the following
10 Secondary License when the conditions for such availability set forth
11 in the Eclipse Public License, Version 2.0 are satisfied:
12       GNU General Public License, Version 2.0 or later.
13 ---------------------------------------------------------------------------- */
14 /*
15 $begin ADFun$$
16 $spell
17     xk
18     Ind
19     bool
20     taylor_
21     sizeof
22     const
23     std
24     ind_taddr_
25     dep_taddr_
26 $$
27 
28 $spell
29 $$
30 
31 $section ADFun Objects$$
32 
33 
34 $head Purpose$$
35 An AD of $icode Base$$
36 $cref/operation sequence/glossary/Operation/Sequence/$$
37 is stored in an $code ADFun$$ object by its $cref FunConstruct$$.
38 The $code ADFun$$ object can then be used to calculate function values,
39 derivative values, and other values related to the corresponding function.
40 
41 $childtable%
42     omh/adfun.omh%
43     include/cppad/core/optimize.hpp%
44     include/cppad/core/fun_check.hpp%
45     include/cppad/core/check_for_nan.hpp
46 %$$
47 
48 $end
49 */
50 # include <cppad/core/graph/cpp_graph.hpp>
51 # include <cppad/local/subgraph/info.hpp>
52 # include <cppad/local/graph/cpp_graph_op.hpp>
53 
54 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
55 /*!
56 \file ad_fun.hpp
57 File used to define the ADFun<Base> class.
58 */
59 
60 /*!
61 Class used to hold function objects
62 
63 \tparam Base
64 A function object has a recording of <tt>AD<Base></tt> operations.
65 It does it calculations using Base operations.
66 */
67 
68 
69 template <class Base, class RecBase>
70 class ADFun {
71     // ADFun<Base> must be a friend of ADFun< AD<Base> > for base2ad to work.
72     template <class Base2, class RecBase2> friend class ADFun;
73 private:
74     // ------------------------------------------------------------
75     // Private member variables
76     // ------------------------------------------------------------
77 
78     /// name of this function (so far only json operations use this value)
79     std::string function_name_;
80 
81     /// Did the previous optimzation exceed the collision limit
82     bool exceed_collision_limit_;
83 
84     /// Has this ADFun object been optmized
85     bool has_been_optimized_;
86 
87     /// Check for nan's and report message to user (default value is true).
88     bool check_for_nan_;
89 
90     /// If zero, ignoring comparison operators. Otherwise is the
91     /// compare change count at which to store the operator index.
92     size_t compare_change_count_;
93 
94     /// If compare_change_count_ is zero, compare_change_number_ is also zero.
95     /// Otherwise, it is set to the number of comparison operations that had a
96     /// different result during the subsequent zero order forward.
97     size_t compare_change_number_;
98 
99     /// If compare_change_count is zero, compare_change_op_index_ is also
100     /// zero. Otherwise it is the operator index for the comparison operator
101     //// that corresponded to the number changing from count-1 to count.
102     size_t compare_change_op_index_;
103 
104     /// number of orders stored in taylor_
105     size_t num_order_taylor_;
106 
107     /// maximum number of orders that will fit in taylor_
108     size_t cap_order_taylor_;
109 
110     /// number of directions stored in taylor_
111     size_t num_direction_taylor_;
112 
113     /// number of variables in the recording (play_)
114     size_t num_var_tape_;
115 
116     /// tape address for the independent variables
117     local::pod_vector<size_t> ind_taddr_;
118 
119     /// tape address and parameter flag for the dependent variables
120     local::pod_vector<size_t> dep_taddr_;
121 
122     /// which dependent variables are actually parameters
123     local::pod_vector<bool> dep_parameter_;
124 
125     /// which operations can be conditionally skipped
126     /// Set during forward pass of order zero
127     local::pod_vector<bool> cskip_op_;
128 
129     /// Variable on the tape corresponding to each vecad load operation
130     /// (if zero, the operation corresponds to a parameter).
131     local::pod_vector<addr_t> load_op2var_;
132 
133     /// results of the forward mode calculations
134     local::pod_vector_maybe<Base> taylor_;
135 
136     /// used for subgraph reverse mode calculations.
137     /// Declared here to avoid reallocation for each call to subgraph_reverse.
138     /// Not in subgraph_info_ because it depends on Base.
139     local::pod_vector_maybe<Base> subgraph_partial_;
140 
141     /// the operation sequence corresponding to this object
142     local::player<Base> play_;
143 
144     /// subgraph information for this object
145     local::subgraph::subgraph_info subgraph_info_;
146 
147     /// Packed results of the forward mode Jacobian sparsity calculations.
148     /// for_jac_sparse_pack_.n_set() != 0  implies other sparsity results
149     /// are empty
150     local::sparse::pack_setvec for_jac_sparse_pack_;
151 
152     /// Set results of the forward mode Jacobian sparsity calculations
153     /// for_jac_sparse_set_.n_set() != 0  implies for_sparse_pack_ is empty.
154     local::sparse::list_setvec for_jac_sparse_set_;
155 
156 
157     // ------------------------------------------------------------
158     // Private member functions
159     // ------------------------------------------------------------
160 
161     /// change the operation sequence corresponding to this object
162     template <class ADvector>
163     void Dependent(local::ADTape<Base> *tape, const ADvector &y);
164 
165     // vector of bool version of ForSparseJac
166     // (doxygen in cppad/core/for_sparse_jac.hpp)
167     template <class SetVector>
168     void ForSparseJacCase(
169         bool               set_type  ,
170         bool               transpose ,
171         bool               dependency,
172         size_t             q         ,
173         const SetVector&   r         ,
174         SetVector&         s
175     );
176 
177     // vector of std::set<size_t> version of ForSparseJac
178     // (doxygen in cppad/core/for_sparse_jac.hpp)
179     template <class SetVector>
180     void ForSparseJacCase(
181         const std::set<size_t>&  set_type  ,
182         bool                     transpose ,
183         bool                     dependency,
184         size_t                   q         ,
185         const SetVector&         r         ,
186         SetVector&               s
187     );
188 
189     // vector of bool version of RevSparseJac
190     // (doxygen in cppad/core/rev_sparse_jac.hpp)
191     template <class SetVector>
192     void RevSparseJacCase(
193         bool               set_type  ,
194         bool               transpose ,
195         bool               dependency,
196         size_t             p         ,
197         const SetVector&   s         ,
198         SetVector&         r
199     );
200 
201     // vector of std::set<size_t> version of RevSparseJac
202     // (doxygen in cppad/core/rev_sparse_jac.hpp)
203     template <class SetVector>
204     void RevSparseJacCase(
205         const std::set<size_t>&  set_type  ,
206         bool                     transpose ,
207         bool                     dependency,
208         size_t                   p         ,
209         const SetVector&         s         ,
210         SetVector&               r
211     );
212 
213     // vector of bool version of ForSparseHes
214     // (doxygen in cppad/core/for_sparse_hes.hpp)
215     template <class SetVector>
216     void ForSparseHesCase(
217         bool               set_type  ,
218         const SetVector&   r         ,
219         const SetVector&   s         ,
220         SetVector&         h
221     );
222 
223     // vector of std::set<size_t> version of ForSparseHes
224     // (doxygen in cppad/core/for_sparse_hes.hpp)
225     template <class SetVector>
226     void ForSparseHesCase(
227         const std::set<size_t>&  set_type  ,
228         const SetVector&         r         ,
229         const SetVector&         s         ,
230         SetVector&               h
231     );
232 
233     // vector of bool version of RevSparseHes
234     // (doxygen in cppad/core/rev_sparse_hes.hpp)
235     template <class SetVector>
236     void RevSparseHesCase(
237         bool               set_type  ,
238         bool               transpose ,
239         size_t             q         ,
240         const SetVector&   s         ,
241         SetVector&         h
242     );
243 
244     // vector of std::set<size_t> version of RevSparseHes
245     // (doxygen in cppad/core/rev_sparse_hes.hpp)
246     template <class SetVector>
247     void RevSparseHesCase(
248         const std::set<size_t>&  set_type  ,
249         bool                     transpose ,
250         size_t                   q         ,
251         const SetVector&         s         ,
252         SetVector&               h
253     );
254 
255     // Forward mode version of SparseJacobian
256     // (doxygen in cppad/core/sparse_jacobian.hpp)
257     template <class BaseVector, class SetVector, class SizeVector>
258     size_t SparseJacobianFor(
259         const BaseVector&           x               ,
260               SetVector&            p_transpose     ,
261         const SizeVector&           row             ,
262         const SizeVector&           col             ,
263               BaseVector&           jac             ,
264               sparse_jacobian_work& work
265     );
266 
267     // Reverse mode version of SparseJacobian
268     // (doxygen in cppad/core/sparse_jacobian.hpp)
269     template <class BaseVector, class SetVector, class SizeVector>
270     size_t SparseJacobianRev(
271         const BaseVector&           x               ,
272               SetVector&            p               ,
273         const SizeVector&           row             ,
274         const SizeVector&           col             ,
275               BaseVector&           jac             ,
276               sparse_jacobian_work& work
277     );
278 
279     // combined sparse_list and sparse_pack version of SparseHessian
280     // (doxygen in cppad/core/sparse_hessian.hpp)
281     template <class BaseVector, class SetVector, class SizeVector>
282     size_t SparseHessianCompute(
283         const BaseVector&              x           ,
284         const BaseVector&              w           ,
285               SetVector&               sparsity    ,
286         const SizeVector&              row         ,
287         const SizeVector&              col         ,
288               BaseVector&              hes         ,
289               sparse_hessian_work&     work
290     );
291 
292 public:
293     /// default constructor
294     ADFun(void);
295 
296     /// copy constructor
297     ADFun(const ADFun& g) = delete;
298 
299     // assignment operator
300     // (doxygen in cppad/core/fun_construct.hpp)
301     void operator=(const ADFun& f);
302 
303     // swap
304     void swap(ADFun& f);
305 
306     // move semenatics copy
307     ADFun(ADFun&& f);
308 
309     // move semantics assignment
310     void operator=(ADFun&& f);
311 
312     // create from Json or C++ AD graph
313     void from_json(const std::string& json);
314     void from_graph(const cpp_graph& graph_obj);
315     void from_graph(
316         const cpp_graph&    graph_obj  ,
317         const vector<bool>& dyn2var    ,
318         const vector<bool>& var2dyn
319     );
320 
321     // create a Json or C++ AD graph
322     std::string to_json(void);
323     void to_graph(cpp_graph& graph_obj);
324 
325     // create ADFun< AD<Base> > from this ADFun<Base>
326     // (doxygen in cppad/core/base2ad.hpp)
327     ADFun< AD<Base>, RecBase > base2ad(void) const;
328 
329     /// sequence constructor
330     template <class ADvector>
331     ADFun(const ADvector &x, const ADvector &y);
332 
333     /// destructor
334     ~ADFun(void);
335 
336     /// set check_for_nan
337     void check_for_nan(bool value);
338 
339     /// get check_for_nan
340     bool check_for_nan(void) const;
341 
342     /// assign a new operation sequence
343     template <class ADvector>
344     void Dependent(const ADvector &x, const ADvector &y);
345 
346     /// new_dynamic user API
347     template <class BaseVector>
348     void new_dynamic(const BaseVector& dynamic);
349 
350     /// forward mode user API, one order multiple directions.
351     template <class BaseVector>
352     BaseVector Forward(size_t q, size_t r, const BaseVector& x);
353 
354     /// forward mode user API, multiple orders one direction.
355     template <class BaseVector>
356     BaseVector Forward(
357         size_t q, const BaseVector& xq, std::ostream& s = std::cout
358     );
359 
360     /// reverse mode sweep
361     template <class BaseVector>
362     BaseVector Reverse(size_t p, const BaseVector &v);
363 
364     // forward Jacobian sparsity pattern
365     // (doxygen in cppad/core/for_sparse_jac.hpp)
366     template <class SetVector>
367     SetVector ForSparseJac(
368         size_t q, const SetVector &r, bool transpose = false,
369         bool dependency = false
370     );
371 
372     // reverse Jacobian sparsity pattern
373     // (doxygen in cppad/core/rev_sparse_jac.hpp)
374     template <class SetVector>
375     SetVector RevSparseJac(
376         size_t q, const SetVector &s, bool transpose = false,
377         bool dependency = false
378     );
379 
380     // subgraph_reverse: select domain
381     // (doxygen in cppad/core/subgraph_reverse.hpp)
382     template <class BoolVector>
383     void subgraph_reverse(
384         const BoolVector&                   select_domain
385     );
386 
387     // subgraph_reverse: compute derivative
388     // (doxygen in cppad/core/subgraph_reverse.hpp)
389     template <class Addr, class BaseVector, class SizeVector>
390     void subgraph_reverse_helper(
391         size_t                               q         ,
392         size_t                               ell       ,
393         SizeVector&                          col       ,
394         BaseVector&                          dw
395     );
396 
397     // subgraph_reverse: compute derivative
398     // (doxygen in cppad/core/subgraph_reverse.hpp)
399     template <class BaseVector, class SizeVector>
400     void subgraph_reverse(
401         size_t                               q         ,
402         size_t                               ell       ,
403         SizeVector&                          col       ,
404         BaseVector&                          dw
405     );
406 
407     // subgraph_jac_rev: compute Jacobian
408     // (doxygen in cppad/core/subgraph_jac_rev.hpp)
409     template <class SizeVector, class BaseVector>
410     void subgraph_jac_rev(
411         const BaseVector&                    x         ,
412         sparse_rcv<SizeVector, BaseVector>&  subset
413     );
414 
415     // subgraph_jac_rev: compute Jacobian
416     // (doxygen missing in cppad/core/subgraph_jac_rev.hpp)
417     template <class BoolVector, class SizeVector, class BaseVector>
418     void subgraph_jac_rev(
419         const BoolVector&                    select_domain ,
420         const BoolVector&                    select_range  ,
421         const BaseVector&                    x             ,
422         sparse_rcv<SizeVector, BaseVector>&  matrix_out
423     );
424 
425 
426     // compute sparse Jacobian using forward mode
427     // (doxygen in cppad/core/sparse_jac.hpp)
428     template <class SizeVector, class BaseVector>
429     size_t sparse_jac_for(
430         size_t                               group_max ,
431         const BaseVector&                    x         ,
432         sparse_rcv<SizeVector, BaseVector>&  subset    ,
433         const sparse_rc<SizeVector>&         pattern   ,
434         const std::string&                   coloring  ,
435         sparse_jac_work&                     work
436     );
437 
438     // compute sparse Jacobian using reverse mode
439     // (doxygen in cppad/core/sparse_jac.hpp)
440     template <class SizeVector, class BaseVector>
441     size_t sparse_jac_rev(
442         const BaseVector&                    x        ,
443         sparse_rcv<SizeVector, BaseVector>&  subset   ,
444         const sparse_rc<SizeVector>&         pattern  ,
445         const std::string&                   coloring ,
446         sparse_jac_work&                     work
447     );
448 
449     // compute sparse Hessian
450     // (doxygen in cppad/core/sparse_hes.hpp)
451     template <class SizeVector, class BaseVector>
452     size_t sparse_hes(
453         const BaseVector&                    x        ,
454         const BaseVector&                    w        ,
455         sparse_rcv<SizeVector, BaseVector>&  subset   ,
456         const sparse_rc<SizeVector>&         pattern  ,
457         const std::string&                   coloring ,
458         sparse_hes_work&                     work
459     );
460 
461     // compute sparsity pattern using subgraphs
462     // (doxygen in cppad/core/subgraph_sparsity.hpp)
463     template <class BoolVector, class SizeVector>
464     void subgraph_sparsity(
465         const BoolVector&            select_domain    ,
466         const BoolVector&            select_range     ,
467         bool                         transpose        ,
468         sparse_rc<SizeVector>&       pattern_out
469     );
470 
471 
472     // forward mode Jacobian sparsity pattern
473     // (doxygen in cppad/core/for_jac_sparsity.hpp)
474     template <class SizeVector>
475     void for_jac_sparsity(
476         const sparse_rc<SizeVector>& pattern_in       ,
477         bool                         transpose        ,
478         bool                         dependency       ,
479         bool                         internal_bool    ,
480         sparse_rc<SizeVector>&       pattern_out
481     );
482 
483     // reverse mode Jacobian sparsity pattern
484     // (doxygen in cppad/core/for_jac_sparsity.hpp)
485     template <class SizeVector>
486     void rev_jac_sparsity(
487         const sparse_rc<SizeVector>& pattern_in       ,
488         bool                         transpose        ,
489         bool                         dependency       ,
490         bool                         internal_bool    ,
491         sparse_rc<SizeVector>&       pattern_out
492     );
493 
494     // reverse mode Hessian sparsity pattern
495     // (doxygen in cppad/core/rev_hes_sparsity.hpp)
496     template <class BoolVector, class SizeVector>
497     void rev_hes_sparsity(
498         const BoolVector&            select_range     ,
499         bool                         transpose        ,
500         bool                         internal_bool    ,
501         sparse_rc<SizeVector>&       pattern_out
502     );
503 
504     // forward mode Hessian sparsity pattern
505     // (doxygen in cppad/core/for_hes_sparsity.hpp)
506     template <class BoolVector, class SizeVector>
507     void for_hes_sparsity(
508         const BoolVector&            select_domain    ,
509         const BoolVector&            select_range     ,
510         bool                         internal_bool    ,
511         sparse_rc<SizeVector>&       pattern_out
512     );
513 
514     // forward mode Hessian sparsity pattern
515     // (see doxygen in cppad/core/for_sparse_hes.hpp)
516     template <class SetVector>
517     SetVector ForSparseHes(
518         const SetVector &r, const SetVector &s
519     );
520 
521     // internal set sparsity version of ForSparseHes
522     // (used by checkpoint functions only)
523     void ForSparseHesCheckpoint(
524         vector<bool>&                 r         ,
525         vector<bool>&                 s         ,
526         local::sparse::list_setvec&   h
527     );
528 
529     // reverse mode Hessian sparsity pattern
530     // (see doxygen in cppad/core/rev_sparse_hes.hpp)
531     template <class SetVector>
532     SetVector RevSparseHes(
533         size_t q, const SetVector &s, bool transpose = false
534     );
535 
536     // internal set sparsity version of RevSparseHes
537     // (doxygen in cppad/core/rev_sparse_hes.hpp)
538     // (used by checkpoint functions only)
539     void RevSparseHesCheckpoint(
540         size_t                        q         ,
541         vector<bool>&                 s         ,
542         bool                          transpose ,
543         local::sparse::list_setvec&   h
544     );
545 
546     // internal set sparsity version of RevSparseJac
547     // (doxygen in cppad/core/rev_sparse_jac.hpp)
548     // (used by checkpoint functions only)
549     void RevSparseJacCheckpoint(
550         size_t                        q          ,
551         const local::sparse::list_setvec&     r          ,
552         bool                          transpose  ,
553         bool                          dependency ,
554         local::sparse::list_setvec&   s
555     );
556 
557     // internal set sparsity version of RevSparseJac
558     // (doxygen in cppad/core/for_sparse_jac.hpp)
559     // (used by checkpoint functions only)
560     void ForSparseJacCheckpoint(
561     size_t                             q          ,
562     const local::sparse::list_setvec&  r          ,
563     bool                               transpose  ,
564     bool                               dependency ,
565     local::sparse::list_setvec&        s
566     );
567 
568     /// did previous optimization exceed the collision limit
exceed_collision_limit(void) const569     bool exceed_collision_limit(void) const
570     {   return exceed_collision_limit_; }
571 
572     /// amount of memory used for boolean Jacobain sparsity pattern
size_forward_bool(void) const573     size_t size_forward_bool(void) const
574     {   return for_jac_sparse_pack_.memory(); }
575 
576     /// free memory used for Jacobain sparsity pattern
size_forward_bool(size_t zero)577     void size_forward_bool(size_t zero)
578     {   CPPAD_ASSERT_KNOWN(
579             zero == 0,
580             "size_forward_bool: argument not equal to zero"
581         );
582         for_jac_sparse_pack_.resize(0, 0);
583     }
584 
585     /// amount of memory used for vector of set Jacobain sparsity pattern
size_forward_set(void) const586     size_t size_forward_set(void) const
587     {   return for_jac_sparse_set_.memory(); }
588 
589     /// free memory used for Jacobain sparsity pattern
size_forward_set(size_t zero)590     void size_forward_set(size_t zero)
591     {   CPPAD_ASSERT_KNOWN(
592             zero == 0,
593             "size_forward_bool: argument not equal to zero"
594         );
595         for_jac_sparse_set_.resize(0, 0);
596     }
597 
598     /// number of operators in the operation sequence
size_op(void) const599     size_t size_op(void) const
600     {   return play_.num_op_rec(); }
601 
602     /// number of operator arguments in the operation sequence
size_op_arg(void) const603     size_t size_op_arg(void) const
604     {   return play_.num_op_arg_rec(); }
605 
606     /// amount of memory required for the operation sequence
size_op_seq(void) const607     size_t size_op_seq(void) const
608     {   return play_.size_op_seq(); }
609 
610     /// amount of memory currently allocated for random access
611     /// of the operation sequence
size_random(void) const612     size_t size_random(void) const
613     {   return play_.size_random(); }
614 
615     /// number of parameters in the operation sequence
size_par(void) const616     size_t size_par(void) const
617     {   return play_.num_par_rec(); }
618 
619     /// number of independent dynamic parameters
size_dyn_ind(void) const620     size_t size_dyn_ind(void) const
621     {   return play_.num_dynamic_ind(); }
622 
623     /// number of dynamic parameters
size_dyn_par(void) const624     size_t size_dyn_par(void) const
625     {   return play_.num_dynamic_par(); }
626 
627     /// number of dynamic parameters arguments
size_dyn_arg(void) const628     size_t size_dyn_arg(void) const
629     {   return play_.num_dynamic_arg(); }
630 
631     /// number taylor coefficient orders calculated
size_order(void) const632     size_t size_order(void) const
633     {   return num_order_taylor_; }
634 
635     /// number taylor coefficient directions calculated
size_direction(void) const636     size_t size_direction(void) const
637     {   return num_direction_taylor_; }
638 
639     /// number of characters in the operation sequence
size_text(void) const640     size_t size_text(void) const
641     {   return play_.num_text_rec(); }
642 
643     /// number of variables in opertion sequence
size_var(void) const644     size_t size_var(void) const
645     {   return num_var_tape_; }
646 
647     /// number of VecAD indices in the operation sequence
size_VecAD(void) const648     size_t size_VecAD(void) const
649     {   return play_.num_var_vecad_ind_rec(); }
650 
651     /// set number of orders currently allocated (user API)
652     void capacity_order(size_t c);
653 
654     /// set number of orders and directions currently allocated
655     void capacity_order(size_t c, size_t r);
656 
657     /// number of variables in conditional expressions that can be skipped
658     size_t number_skip(void);
659 
660     /// number of independent variables
Domain(void) const661     size_t Domain(void) const
662     {   return ind_taddr_.size(); }
663 
664     /// number of dependent variables
Range(void) const665     size_t Range(void) const
666     {   return dep_taddr_.size(); }
667 
668     /// is variable a parameter
Parameter(size_t i)669     bool Parameter(size_t i)
670     {   CPPAD_ASSERT_KNOWN(
671             i < dep_taddr_.size(),
672             "Argument to Parameter is >= dimension of range space"
673         );
674         return dep_parameter_[i];
675     }
676 
677     /// Deprecated: number of comparison operations that changed
678     /// for the previous zero order forward (than when function was recorded)
CompareChange(void) const679     size_t CompareChange(void) const
680     {   return compare_change_number_; }
681 
682     /// count as which to store operator index
compare_change_count(size_t count)683     void compare_change_count(size_t count)
684     {   compare_change_count_    = count;
685         compare_change_number_   = 0;
686         compare_change_op_index_ = 0;
687     }
688 
689     /// number of comparison operations that changed
compare_change_number(void) const690     size_t compare_change_number(void) const
691     {   return compare_change_number_; }
692 
693     /// operator index for the count-th  comparison change
compare_change_op_index(void) const694     size_t compare_change_op_index(void) const
695     {   if( has_been_optimized_ )
696             return 0;
697         return compare_change_op_index_;
698     }
699 
700     /// calculate entire Jacobian
701     template <class BaseVector>
702     BaseVector Jacobian(const BaseVector &x);
703 
704     /// calculate Hessian for one component of f
705     template <class BaseVector>
706     BaseVector Hessian(const BaseVector &x, const BaseVector &w);
707     template <class BaseVector>
708     BaseVector Hessian(const BaseVector &x, size_t i);
709 
710     /// forward mode calculation of partial w.r.t one domain component
711     template <class BaseVector>
712     BaseVector ForOne(
713         const BaseVector   &x ,
714         size_t              j );
715 
716     /// reverse mode calculation of derivative of one range component
717     template <class BaseVector>
718     BaseVector RevOne(
719         const BaseVector   &x ,
720         size_t              i );
721 
722     /// forward mode calculation of a subset of second order partials
723     template <class BaseVector, class SizeVector_t>
724     BaseVector ForTwo(
725         const BaseVector   &x ,
726         const SizeVector_t &J ,
727         const SizeVector_t &K );
728 
729     /// reverse mode calculation of a subset of second order partials
730     template <class BaseVector, class SizeVector_t>
731     BaseVector RevTwo(
732         const BaseVector   &x ,
733         const SizeVector_t &I ,
734         const SizeVector_t &J );
735 
736     /// calculate sparse Jacobians
737     template <class BaseVector>
738     BaseVector SparseJacobian(
739         const BaseVector &x
740     );
741     template <class BaseVector, class SetVector>
742     BaseVector SparseJacobian(
743         const BaseVector &x ,
744         const SetVector  &p
745     );
746     template <class BaseVector, class SetVector, class SizeVector>
747     size_t SparseJacobianForward(
748         const BaseVector&     x     ,
749         const SetVector&      p     ,
750         const SizeVector&     r     ,
751         const SizeVector&     c     ,
752         BaseVector&           jac   ,
753         sparse_jacobian_work& work
754     );
755     template <class BaseVector, class SetVector, class SizeVector>
756     size_t SparseJacobianReverse(
757         const BaseVector&     x    ,
758         const SetVector&      p    ,
759         const SizeVector&     r    ,
760         const SizeVector&     c    ,
761         BaseVector&           jac  ,
762         sparse_jacobian_work& work
763     );
764 
765     /// calculate sparse Hessians
766     template <class BaseVector>
767     BaseVector SparseHessian(
768         const BaseVector&    x  ,
769         const BaseVector&    w
770     );
771     template <class BaseVector, class BoolVector>
772     BaseVector SparseHessian(
773         const BaseVector&    x  ,
774         const BaseVector&    w  ,
775         const BoolVector&    p
776     );
777     template <class BaseVector, class SetVector, class SizeVector>
778     size_t SparseHessian(
779         const BaseVector&    x   ,
780         const BaseVector&    w   ,
781         const SetVector&     p   ,
782         const SizeVector&    r   ,
783         const SizeVector&    c   ,
784         BaseVector&          hes ,
785         sparse_hessian_work& work
786     );
787 
788     // Optimize the tape
789     // (see doxygen documentation in optimize.hpp)
790     void optimize( const std::string& options = "" );
791 
792     // create abs-normal representation of the function f(x)
793     void abs_normal_fun( ADFun& g, ADFun& a ) const;
794 
795     // clear all subgraph information
796     void clear_subgraph(void);
797     // ------------------- Deprecated -----------------------------
798 
799     /// deprecated: assign a new operation sequence
800     template <class ADvector>
801     void Dependent(const ADvector &y);
802 
803     /// Deprecated: number of variables in opertion sequence
Size(void) const804     size_t Size(void) const
805     {   return num_var_tape_; }
806 
807     /// Deprecated: # taylor_ coefficients currently stored
808     /// (per variable,direction)
Order(void) const809     size_t Order(void) const
810     {   return num_order_taylor_ - 1; }
811 
812     /// Deprecated: amount of memory for this object
813     /// Note that an approximation is used for the std::set<size_t> memory
Memory(void) const814     size_t Memory(void) const
815     {   size_t pervar  = cap_order_taylor_ * sizeof(Base)
816         + for_jac_sparse_pack_.memory()
817         + for_jac_sparse_set_.memory();
818         size_t total   = num_var_tape_  * pervar;
819         total         += play_.size_op_seq();
820         total         += play_.size_random();
821         total         += subgraph_info_.memory();
822         return total;
823     }
824 
825     /// Deprecated: # taylor_ coefficient orderss stored
826     /// (per variable,direction)
taylor_size(void) const827     size_t taylor_size(void) const
828     {   return num_order_taylor_; }
829 
830     /// Deprecated: Does this AD operation sequence use
831     /// VecAD<Base>::reference operands
use_VecAD(void) const832     bool use_VecAD(void) const
833     {   return play_.num_var_vecad_ind_rec() > 0; }
834 
835     /// Deprecated: # taylor_ coefficient orders calculated
836     /// (per variable,direction)
size_taylor(void) const837     size_t size_taylor(void) const
838     {   return num_order_taylor_; }
839 
840     /// Deprecated: set number of orders currently allocated
841     /// (per variable,direction)
842     void capacity_taylor(size_t per_var);
843 };
844 // ---------------------------------------------------------------------------
845 
846 } // END_CPPAD_NAMESPACE
847 
848 // non-user interfaces
849 # include <cppad/local/sweep/forward0.hpp>
850 # include <cppad/local/sweep/forward1.hpp>
851 # include <cppad/local/sweep/forward2.hpp>
852 # include <cppad/local/sweep/reverse.hpp>
853 # include <cppad/local/sweep/for_jac.hpp>
854 # include <cppad/local/sweep/rev_jac.hpp>
855 # include <cppad/local/sweep/rev_hes.hpp>
856 # include <cppad/local/sweep/for_hes.hpp>
857 # include <cppad/core/graph/from_graph.hpp>
858 # include <cppad/core/graph/to_graph.hpp>
859 
860 // user interfaces
861 # include <cppad/core/parallel_ad.hpp>
862 # include <cppad/core/independent/independent.hpp>
863 # include <cppad/core/dependent.hpp>
864 # include <cppad/core/fun_construct.hpp>
865 # include <cppad/core/base2ad.hpp>
866 # include <cppad/core/abort_recording.hpp>
867 # include <cppad/core/fun_eval.hpp>
868 # include <cppad/core/drivers.hpp>
869 # include <cppad/core/fun_check.hpp>
870 # include <cppad/core/omp_max_thread.hpp>
871 # include <cppad/core/optimize.hpp>
872 # include <cppad/core/abs_normal_fun.hpp>
873 # include <cppad/core/graph/from_json.hpp>
874 # include <cppad/core/graph/to_json.hpp>
875 
876 # endif
877