source: trunk/cppad/local/independent.hpp @ 3146

Last change on this file since 3146 was 3146, checked in by bradbell, 6 years ago

Change some player public functions better names (prepare to use them more).

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: independent.hpp 3146 2014-03-03 12:33:40Z bradbell $ */
2# ifndef CPPAD_INDEPENDENT_INCLUDED
3# define CPPAD_INDEPENDENT_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse 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---------------------------------------------------------------------------
17
18$begin Independent$$
19$spell
20        alloc
21        num
22        Cpp
23        bool
24        const
25        var
26        typename
27$$
28
29$index Independent$$
30$index start, recording$$
31$index recording, start$$
32$index variable, independent$$
33
34$section Declare Independent Variables and Start Recording$$
35
36$head Syntax$$
37$codei%Independent(%x%)%$$
38
39$head Purpose$$
40Start recording
41$cref/AD of Base/glossary/AD of Base/$$ operations
42with $icode x$$ as the independent variable vector.
43Once the
44$cref/operation sequence/glossary/Operation/Sequence/$$ is completed,
45it must be transferred to a function object; see below.
46
47$head Start Recording$$
48An operation sequence recording is started by the command
49$codei%
50        Independent(%x%)
51%$$
52
53$head Stop Recording$$
54The recording is stopped,
55and the operation sequence is transferred to the AD function object $icode f$$,
56using either the $cref/function constructor/FunConstruct/$$
57$codei%
58        ADFun<%Base%> %f%( %x%, %y%)
59%$$
60or the $cref/dependent variable specifier/Dependent/$$
61$codei%
62        %f%.Dependent( %x%, %y%)
63%$$
64The only other way to stop a recording is using
65$cref abort_recording$$.
66Between when the recording is started and when it stopped,
67we refer to the elements of $icode x$$,
68and the values that depend on the elements of $icode x$$,
69as $codei%AD<%Base%>%$$ variables.
70
71$head x$$
72The vector $icode x$$ has prototype
73$codei%
74        %VectorAD% &%x%
75%$$
76(see $icode VectorAD$$ below).
77The size of the vector $icode x$$, must be greater than zero,
78and is the number of independent variables for this
79AD operation sequence.
80
81$head VectorAD$$
82The type $icode VectorAD$$ must be a $cref SimpleVector$$ class with
83$cref/elements of type/SimpleVector/Elements of Specified Type/$$
84$codei%AD<%Base%>%$$.
85The routine $cref CheckSimpleVector$$ will generate an error message
86if this is not the case.
87
88$head Parallel Mode$$
89$index parallel, Independent$$
90$index Independent, parallel$$
91The call to $code Independent$$,
92and the corresponding call to
93$codei%
94        ADFun<%Base%> %f%( %x%, %y%)
95%$$
96or
97$codei%
98        %f%.Dependent( %x%, %y%)
99%$$
100or $cref abort_recording$$,
101must be preformed by the same thread; i.e.,
102$cref/thread_alloc::thread_num/ta_thread_num/$$ must be the same.
103
104$head Example$$
105$children%
106        example/independent.cpp
107%$$
108The file
109$cref independent.cpp$$
110contains an example and test of this operation.
111It returns true if it succeeds and false otherwise.
112
113$end
114-----------------------------------------------------------------------------
115*/
116
117//  BEGIN CppAD namespace
118namespace CppAD {
119// ---------------------------------------------------------------------------
120
121template <typename Base>
122template <typename VectorAD>
123void ADTape<Base>::Independent(VectorAD &x)
124{
125        // check VectorAD is Simple Vector class with AD<Base> elements
126        CheckSimpleVector< AD<Base>, VectorAD>();
127
128        // dimension of the domain space
129        size_t n = x.size();
130        CPPAD_ASSERT_KNOWN(
131                n > 0,
132                "Indepdendent: the argument vector x has zero size"
133        );
134        CPPAD_ASSERT_UNKNOWN( Rec_.num_var_rec() == 0 );
135
136        // mark the beginning of the tape and skip the first variable index
137        // (zero) because parameters use taddr zero
138        CPPAD_ASSERT_NARG_NRES(BeginOp, 1, 1);
139        Rec_.PutOp(BeginOp);
140        Rec_.PutArg(0);
141
142        // place each of the independent variables in the tape
143        CPPAD_ASSERT_NARG_NRES(InvOp, 0, 1);
144        size_t j;
145        for(j = 0; j < n; j++)
146        {       // tape address for this independent variable
147                x[j].taddr_ = Rec_.PutOp(InvOp);
148                x[j].tape_id_    = id_;
149                CPPAD_ASSERT_UNKNOWN( size_t(x[j].taddr_) == j+1 );
150                CPPAD_ASSERT_UNKNOWN( Variable(x[j] ) );
151        }
152
153        // done specifying all of the independent variables
154        size_independent_ = n;
155}
156
157template <typename VectorAD>
158inline void Independent(VectorAD &x)
159{       typedef typename VectorAD::value_type ADBase;
160        typedef typename ADBase::value_type   Base;
161        CPPAD_ASSERT_KNOWN(
162                ADBase::tape_ptr() == CPPAD_NULL,
163                "Independent: cannot create a new tape because\n"
164                "a previous tape is still active (for this thread).\n"
165                "AD<Base>::abort_recording() would abort this previous recording."
166        );
167        ADTape<Base>* tape = ADBase::tape_manage(tape_manage_new);
168        tape->Independent(x); 
169}
170
171
172} 
173// END CppAD namespace
174
175# endif
Note: See TracBrowser for help on using the repository browser.