source: branches/opt_cond_exp/cppad/local/cskip_op.hpp @ 2969

Last change on this file since 2969 was 2969, checked in by bradbell, 7 years ago

First versiont that actually skips operations (forward0sweep only).

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1// $Id: cskip_op.hpp 2969 2013-10-18 22:21:31Z bradbell $
2# ifndef CPPAD_CSKIP_OP_INCLUDED
3# define CPPAD_CSKIP_OP_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 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
16namespace CppAD { // BEGIN_CPPAD_NAMESPACE
17/*!
18\defgroup cskip_op_hpp cskip_op.hpp
19\{
20\file cskip_op.hpp
21Zero order forward mode set which operations to skip.
22*/
23
24/*!
25Zero order forward mode execution of op = CSkipOp.
26
27\tparam Base
28base type for the operator; i.e., this operation was recorded
29using AD< \a Base > and computations by this routine are done using type
30\a Base.
31
32\param i_z
33variable index corresponding to the result of the previous operation.
34This is used for error checking. To be specific,
35the left and right operands for the CExpOp operation must have indexes
36less than or equal this value.
37
38\param arg [in]
39\n
40\a arg[0]
41is static cast to size_t from the enum type
42\verbatim
43        enum CompareOp {
44                CompareLt,
45                CompareLe,
46                CompareEq,
47                CompareGe,
48                CompareGt,
49                CompareNe
50        }
51\endverbatim
52for this operation.
53Note that arg[0] cannot be equal to CompareNe.
54\n
55\n
56\a arg[1] & 1
57\n
58If this is zero, left is a a parameter. Otherwise it is a variable.
59\n
60\n
61\a arg[1] & 2
62\n
63If this is zero, right is a parameter. Otherwise it is a variable.
64\n
65\a arg[2]
66is the index corresponding to left in comparision.
67\n
68\a arg[3]
69is the index corresponding to right in comparision.
70\n
71\a arg[4]
72is the number of operations to skip if the comparision result is true.
73\n
74\a arg[5]
75is the number of operations to skip if the comparision result is false.
76\n
77<tt>arg[5+i]</tt>
78for <tt>i = 1 , ... , arg[4]</tt> are the operations to skip if the
79comparision result is true.
80\n
81<tt>arg[5+arg[4]+i]</tt>
82for <tt>i = 1 , ... , arg[5]</tt> are the operations to skip if the
83comparision result is false.
84
85\param num_par [in]
86is the total number of values in the vector \a parameter.
87
88\param parameter [in]
89If left is a parameter,
90<code>parameter [ arg[2] ]</code> is its value.
91If right is a parameter,
92<code>parameter [ arg[3] ]</code> is its value.
93
94\param nc_taylor [in]
95number of columns in the matrix containing the Taylor coefficients.
96
97\param taylor [in]
98If left is a variable,
99<code>taylor [ arg[2] * nc_taylor + 0 ]</code>
100is the zeroth order Taylor coefficient corresponding to left.
101If right is a variable,
102<code>taylor [ arg[3] * nc_taylor + 0 ]</code>
103is the zeroth order Taylor coefficient corresponding to right.
104
105\param \cskip_var [in,out]
106is vector specifying which operations are at this point are know to be
107unecessary and can be skipped.
108This is both an input and an output.
109*/
110template <class Base>
111inline void forward_cskip_op_0(
112        size_t               i_z            ,
113        const addr_t*        arg            ,
114        size_t               num_par        ,
115        const Base*          parameter      ,
116        size_t               nc_taylor      ,
117        Base*                taylor         ,
118        CppAD::vector<bool>& cskip_var      )
119{
120        CPPAD_ASSERT_UNKNOWN( size_t(arg[0]) < size_t(CompareNe) );
121        CPPAD_ASSERT_UNKNOWN( arg[1] != 0 );
122
123        Base left, right;
124        if( arg[1] & 1 )
125        {       CPPAD_ASSERT_UNKNOWN( size_t(arg[2]) <= i_z );
126                left = taylor[ arg[2] * nc_taylor + 0 ];
127                CPPAD_ASSERT_UNKNOWN( IdenticalPar(left) );
128        }
129        else
130        {       CPPAD_ASSERT_UNKNOWN( size_t(arg[2]) < num_par );
131                left = parameter[ arg[2] ];
132                CPPAD_ASSERT_UNKNOWN( IdenticalPar(left) );
133        }
134        if( arg[1] & 2 )
135        {       CPPAD_ASSERT_UNKNOWN( size_t(arg[3]) <= i_z );
136                right = taylor[ arg[3] * nc_taylor + 0 ];
137                CPPAD_ASSERT_UNKNOWN( IdenticalPar(right) );
138        }
139        else
140        {       CPPAD_ASSERT_UNKNOWN( size_t(arg[3]) < num_par );
141                right = parameter[ arg[3] ];
142                CPPAD_ASSERT_UNKNOWN( IdenticalPar(right) );
143        }
144
145
146        // initialize to avoid compiler warning
147        bool true_case = false;
148        Base diff      = left - right;
149        switch( CompareOp( arg[0] ) )
150        {
151                case CompareLt:
152                true_case = LessThanZero(diff);
153                break;
154
155                case CompareLe:
156                true_case = LessThanOrZero(diff);
157                break;
158
159                case CompareEq:
160                true_case = IdenticalZero(diff);
161                break;
162
163                case CompareGe:
164                true_case = GreaterThanOrZero(diff);
165                break;
166
167                case CompareGt:
168                true_case = GreaterThanZero(diff);
169                break;
170
171                case CompareNe:
172                true_case = ! IdenticalZero(diff);
173                break;
174
175                default:
176                CPPAD_ASSERT_UNKNOWN(false);
177        }
178        if( true_case )
179        {       for(size_t i = 0; i < size_t(arg[4]); i++)
180                        cskip_var[ arg[6+i] ] = true; 
181        }
182        else
183        {       for(size_t i = 0; i < size_t(arg[5]); i++)
184                        cskip_var[ arg[6+arg[4]+i] ] = true; 
185        }
186        return;
187}
188/*! \} */
189} // END_CPPAD_NAMESPACE
190# endif
191
Note: See TracBrowser for help on using the repository browser.