Codebase list cppad / 5bc9590
New upstream version 2021.00.00.8 Barak A. Pearlmutter 2 years ago
15 changed file(s) with 298 addition(s) and 103 deletion(s). Raw diff Collapse all Expand all
2424 endif()
2525 #
2626 # cppad_version is used by version.sh to get the version number.
27 SET(cppad_version "20210000.4")
27 SET(cppad_version "20210000.8")
2828 SET(cppad_url "https://coin-or.github.io/CppAD" )
2929 SET(cppad_description "Differentiation of C++ Algorithms" )
3030 #
00 #! /bin/bash -e
11 # -----------------------------------------------------------------------------
2 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
2 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
33 #
44 # CppAD is distributed under the terms of the
55 # Eclipse Public License Version 2.0.
7171 exit 1
7272 fi
7373 check_all_warn
74 count=`wc -l $top_srcdir/check_all.warn | sed -e 's|^\([0-9]*\) .*|\1|'`
74 # ignore CMake Warning about old CMAKE_MINIMUM_REQUIRED
75 count=$( sed -e '/^CMake Deprecation Warning/d' -e '/^ *$/d' \
76 $top_srcdir/check_all.warn | \
77 wc -l | sed -e 's|^\([0-9]*\) .*|\1|'
78 )
7579 if [ "$count" != '0' ]
7680 then
7781 head "$top_srcdir/check_all.warn"
00 #! /bin/bash -e
11 # -----------------------------------------------------------------------------
2 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
2 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
33 #
44 # CppAD is distributed under the terms of the
55 # Eclipse Public License Version 2.0.
201201 fi
202202 # -----------------------------------------------------------------------------
203203 # cppad_prefix
204 cmake_args="$cmake_args -D cppad_prefix=$(pwd)/build/prefix"
204 cmake_args="$cmake_args -D cppad_prefix=$prefix"
205205 #
206206 # cmake_install_includedirs
207207 if [ -d '/usr/include' ]
00 #! /bin/sh
11 # Guess values for system-dependent variables and create Makefiles.
2 # Generated by GNU Autoconf 2.69 for cppad 20210000.4.
2 # Generated by GNU Autoconf 2.69 for cppad 20210000.8.
33 #
44 # Report bugs to <cppad@list.coin-or.org>.
55 #
578578 # Identity of this package.
579579 PACKAGE_NAME='cppad'
580580 PACKAGE_TARNAME='cppad'
581 PACKAGE_VERSION='20210000.4'
582 PACKAGE_STRING='cppad 20210000.4'
581 PACKAGE_VERSION='20210000.8'
582 PACKAGE_STRING='cppad 20210000.8'
583583 PACKAGE_BUGREPORT='cppad@list.coin-or.org'
584584 PACKAGE_URL=''
585585
13511351 # Omit some internal or obsolete options to make the list less imposing.
13521352 # This message is too long to be a string in the A/UX 3.1 sh.
13531353 cat <<_ACEOF
1354 \`configure' configures cppad 20210000.4 to adapt to many kinds of systems.
1354 \`configure' configures cppad 20210000.8 to adapt to many kinds of systems.
13551355
13561356 Usage: $0 [OPTION]... [VAR=VALUE]...
13571357
14211421
14221422 if test -n "$ac_init_help"; then
14231423 case $ac_init_help in
1424 short | recursive ) echo "Configuration of cppad 20210000.4:";;
1424 short | recursive ) echo "Configuration of cppad 20210000.8:";;
14251425 esac
14261426 cat <<\_ACEOF
14271427
15431543 test -n "$ac_init_help" && exit $ac_status
15441544 if $ac_init_version; then
15451545 cat <<\_ACEOF
1546 cppad configure 20210000.4
1546 cppad configure 20210000.8
15471547 generated by GNU Autoconf 2.69
15481548
15491549 Copyright (C) 2012 Free Software Foundation, Inc.
19161916 This file contains any messages produced by compilers while
19171917 running configure, to aid debugging if configure makes a mistake.
19181918
1919 It was created by cppad $as_me 20210000.4, which was
1919 It was created by cppad $as_me 20210000.8, which was
19201920 generated by GNU Autoconf 2.69. Invocation command line was
19211921
19221922 $ $0 $@
28062806
28072807 # Define the identity of the package.
28082808 PACKAGE='cppad'
2809 VERSION='20210000.4'
2809 VERSION='20210000.8'
28102810
28112811
28122812 cat >>confdefs.h <<_ACEOF
76487648 # report actual input values of CONFIG_FILES etc. instead of their
76497649 # values after options handling.
76507650 ac_log="
7651 This file was extended by cppad $as_me 20210000.4, which was
7651 This file was extended by cppad $as_me 20210000.8, which was
76527652 generated by GNU Autoconf 2.69. Invocation command line was
76537653
76547654 CONFIG_FILES = $CONFIG_FILES
77057705 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
77067706 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
77077707 ac_cs_version="\\
7708 cppad config.status 20210000.4
7708 cppad config.status 20210000.8
77097709 configured by $0, generated by GNU Autoconf 2.69,
77107710 with options \\"\$ac_cs_config\\"
77117711
1010 # -----------------------------------------------------------------------------
1111 dnl Process this file with autoconf to produce a configure script.
1212 dnl package version bug-report
13 AC_INIT([cppad], [20210000.4], [cppad@list.coin-or.org])
13 AC_INIT([cppad], [20210000.8], [cppad@list.coin-or.org])
1414 AM_SILENT_RULES([no])
1515
1616 dnl By defalut disable maintainer mode when running configure;
00 # ifndef CPPAD_CORE_ATAN2_HPP
11 # define CPPAD_CORE_ATAN2_HPP
22 /* --------------------------------------------------------------------------
3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-18 Bradley M. Bell
3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
44
55 CppAD is distributed under the terms of the
66 Eclipse Public License Version 2.0.
9090 // BEGIN CondExp
9191 template <class Base>
9292 AD<Base> atan2 (const AD<Base> &y, const AD<Base> &x)
93 { AD<Base> alpha;
94 AD<Base> beta;
95 AD<Base> theta;
96
93 { //
94 // zero, pi2, pi
9795 AD<Base> zero(0.);
9896 AD<Base> pi2(2. * atan(1.));
9997 AD<Base> pi(2. * pi2);
100
101 AD<Base> ax = fabs(x);
102 AD<Base> ay = fabs(y);
103
104 // if( ax > ay )
105 // theta = atan(ay / ax);
106 // else theta = pi2 - atan(ax / ay);
107 alpha = atan(ay / ax);
108 beta = pi2 - atan(ax / ay);
109 theta = CondExpGt(ax, ay, alpha, beta); // use of CondExp
110
111 // if( x <= 0 )
112 // theta = pi - theta;
113 theta = CondExpLe(x, zero, pi - theta, theta); // use of CondExp
114
115 // if( y <= 0 )
116 // theta = - theta;
117 theta = CondExpLe(y, zero, -theta, theta); // use of CondExp
118
119 return theta;
98 //
99 // abs_x, abs_y
100 // Not using fabs because its derivative is zero at zero
101 AD<Base> abs_x = CondExpGe(x, zero, x, -x);
102 AD<Base> abs_y = CondExpGe(y, zero, y, -y);
103 //
104 // first
105 // This is the result for first quadrant: x >= 0 , y >= 0
106 AD<Base> alpha = atan(abs_y / abs_x);
107 AD<Base> beta = pi2 - atan(abs_x / abs_y);
108 AD<Base> first = CondExpGt(abs_x, abs_y, alpha, beta);
109 //
110 // second
111 // This is the result for second quadrant: x <= 0 , y >= 0
112 AD<Base> second = pi - first;
113 //
114 // third
115 // This is the result for third quadrant: x <= 0 , y <= 0
116 AD<Base> third = - pi + first;
117 //
118 // fourth
119 // This is the result for fourth quadrant: x >= 0 , y <= 0
120 AD<Base> fourth = - first;
121 //
122 // alpha
123 // This is the result for x >= 0
124 alpha = CondExpGe(y, zero, first, fourth);
125 //
126 // beta
127 // This is the result for x <= 0
128 beta = CondExpGe(y, zero, second, third);
129 //
130 //
131 AD<Base> result = CondExpGe(x, zero, alpha, beta);
132 return result;
120133 }
121134 // END CondExp
122135
11 # define CPPAD_CORE_GRAPH_TO_GRAPH_HPP
22
33 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
55
66 CppAD is distributed under the terms of the
77 Eclipse Public License Version 2.0.
203203 // unary operators
204204
205205 case local::abs_dyn:
206 case local::fabs_dyn:
206207 graph_op = abs_graph_op;
207208 break;
208209
00 # ifndef CPPAD_CORE_REVERSE_HPP
11 # define CPPAD_CORE_REVERSE_HPP
22 /* --------------------------------------------------------------------------
3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-19 Bradley M. Bell
3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
44
55 CppAD is distributed under the terms of the
66 Eclipse Public License Version 2.0.
149149 Partial[dep_taddr_[i] * q + q - 1] += w[i];
150150 else
151151 { for(k = 0; k < q; k++)
152 // ? should use += here, first make test to demonstrate bug
153 Partial[ dep_taddr_[i] * q + k ] = w[i * q + k ];
152 Partial[ dep_taddr_[i] * q + k ] += w[i * q + k ];
154153 }
155154 }
156155
00 # ifndef CPPAD_LOCAL_OPTIMIZE_OPTIMIZE_RUN_HPP
11 # define CPPAD_LOCAL_OPTIMIZE_OPTIMIZE_RUN_HPP
22 /* --------------------------------------------------------------------------
3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
44
55 CppAD is distributed under the terms of the
66 Eclipse Public License Version 2.0.
205205 );
206206
207207 // number of variables in the player
208 # ifndef NDEBUG
208209 const size_t num_var = play->num_var_rec();
210 # endif
209211
210212 // number of parameter in the player
211213 const size_t num_par = play->num_par_rec();
296298 );
297299 // -----------------------------------------------------------------------
298300
299 // nan with type Base
300 Base base_nan = Base( std::numeric_limits<double>::quiet_NaN() );
301
302 // -------------------------------------------------------------
303301 // conditional expression information
304302 //
305303 // Size of the conditional expression information structure.
374372 new_par[i_par] = rec->put_con_par(par);
375373 }
376374 }
375
376 // index corresponding to the parameter zero
377 addr_t zero_par_index = rec->put_con_par( Base(0) );
377378
378379 // set new_par for the dependent dynamic parameters
379380 size_t i_dyn = num_dynamic_ind; // dynamic parmaeter index
11831184 CPPAD_ASSERT_UNKNOWN( previous == 0 );
11841185 CPPAD_ASSERT_NARG_NRES(op, 1, 0);
11851186 new_arg[0] = new_par[ arg[0] ];
1186 if( new_arg[0] != addr_t_max )
1187 rec->PutArg(new_arg[0]);
1188 else
1189 rec->PutArg(0); // argument not used
1187 if( new_arg[0] == addr_t_max )
1188 new_arg[0] = zero_par_index;
1189 rec->PutArg(new_arg[0]);
11901190 new_op[i_op] = addr_t( rec->num_op_rec() );
11911191 rec->PutOp(FunapOp);
11921192 CPPAD_ASSERT_UNKNOWN( atom_state == arg_atom );
11991199 CPPAD_ASSERT_UNKNOWN( previous == 0 );
12001200 CPPAD_ASSERT_NARG_NRES(op, 1, 0);
12011201 new_arg[0] = new_var[ random_itr.var2op(size_t(arg[0])) ];
1202 if( size_t(new_arg[0]) < num_var )
1202 CPPAD_ASSERT_UNKNOWN( size_t(new_arg[0]) < num_var );
1203 if( new_arg[0] != 0 )
12031204 { rec->PutArg(new_arg[0]);
12041205 new_op[i_op] = addr_t( rec->num_op_rec() );
12051206 rec->PutOp(FunavOp);
12071208 else
12081209 { // This argument does not affect the result and
12091210 // has been optimized out so use nan in its place.
1210 new_arg[0] = rec->put_con_par( base_nan );
1211 new_arg[0] = zero_par_index;
12111212 rec->PutArg(new_arg[0]);
12121213 new_op[i_op] = addr_t( rec->num_op_rec() );
12131214 rec->PutOp(FunapOp);
12221223 CPPAD_ASSERT_UNKNOWN( previous == 0 );
12231224 CPPAD_ASSERT_NARG_NRES(op, 1, 0);
12241225 new_arg[0] = new_par[ arg[0] ];
1225 if( new_arg[0] != addr_t_max )
1226 { // This parameter is used, but may not by this operation
1227 rec->PutArg(new_arg[0]);
1228 }
1229 else
1226 if( new_arg[0] == addr_t_max )
12301227 { // This parameter is not used here or anywhere.
12311228 CPPAD_ASSERT_UNKNOWN( op_usage[i_op] == usage_t(no_usage) );
1232 rec->PutArg(0); // result not used
1233 }
1229 new_arg[0] = zero_par_index;
1230 }
1231 rec->PutArg(new_arg[0]);
1232 //
12341233 new_op[i_op] = addr_t( rec->num_op_rec() );
12351234 rec->PutOp(FunrpOp);
12361235 CPPAD_ASSERT_UNKNOWN( atom_state == ret_atom );
12491248 { // change FunrvOp -> FunrpOp to avoid creating new variable
12501249 CPPAD_ASSERT_UNKNOWN( op_usage[i_op] == usage_t(no_usage) );
12511250 CPPAD_ASSERT_NARG_NRES(FunrpOp, 1, 0);
1252 rec->PutArg(0); // result not used
1251 rec->PutArg( zero_par_index );
12531252 rec->PutOp(FunrpOp);
12541253 }
12551254
6464
6565 $comment bin/version assumes that : follows cppad version number here$$
6666 $section
67 cppad-20210000.4: A C++ Algorithmic Differentiation Package$$
67 cppad-20210000.8: A C++ Algorithmic Differentiation Package$$
6868
6969 $comment =================================================================== $$
7070 $align middle$$
00 # -----------------------------------------------------------------------------
1 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
1 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
22 #
33 # CppAD is distributed under the terms of the
44 # Eclipse Public License Version 2.0.
121121 # and is set by cmake to the top soruce directory.
122122 #
123123 # cppad_includedir
124 LIST(GET cmake_install_libdirs 0 cppad_includedir)
124 LIST(GET cmake_install_includedirs 0 cppad_includedir)
125125 #
126126 # cppad_libdir
127127 LIST(GET cmake_install_libdirs 0 cppad_libdir)
128128 # -----------------------------------------------------------------------------
129129 # initialize
130 SET(cppad_lib_list "-l${cppad_lib}")
130 SET(cppad_lib_list "-L${cppad_prefix}/${cppad_libdir} -l${cppad_lib}")
131 SET(cppad_requires "")
131132 SET(cppad_libs_private "")
132133 SET(cppad_requires_private "")
133134 #
143144 IF( NOT colpack_libdir )
144145 MESSAGE(FATAL_ERROR "Cannit find libColPack.* below ${colpack_prefix}")
145146 ENDIF( NOT colpack_libdir )
146 SET(cppad_libs_private
147 "${cppad_libs_private} -L${colpack_libdir} -lColPack"
147 SET(cppad_lib_list
148 "${cppad_lib_list} -L${colpack_libdir} -lColPack"
148149 )
149150 ENDIF( cppad_has_colpack )
150151 #
151152 # Ipopt has a pkgconfig file.
152153 IF( cppad_has_ipopt )
153 SET(cppad_requires_private "${cppad_requires} ipopt")
154 SET(cppad_requires "${cppad_requires} ipopt")
154155 SET(cppad_lib_list "${cppad_lib_list} -lcppad_ipopt")
155156 ENDIF( cppad_has_ipopt )
156157 # -----------------------------------------------------------------------------
158159 CONFIGURE_FILE(
159160 ${CMAKE_CURRENT_SOURCE_DIR}/cppad.pc.in
160161 ${CMAKE_CURRENT_BINARY_DIR}/cppad.pc
162 @ONLY
161163 )
162164 # cppad-uninstalled.pc
163165 CONFIGURE_FILE(
164166 ${CMAKE_CURRENT_SOURCE_DIR}/cppad-uninstalled.pc.in
165167 ${CMAKE_CURRENT_BINARY_DIR}/cppad-uninstalled.pc
168 @ONLY
166169 )
167170 # During install copy cppad.pc to datadir
168171 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/cppad.pc
00 # -----------------------------------------------------------------------------
1 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
1 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
22 #
33 # CppAD is distributed under the terms of the
44 # Eclipse Public License Version 2.0.
2323 URL: @cppad_url@
2424 #
2525 Cflags: -I${includedir}
26 Libs: -L{libdir} -l@cppad_lib_list@
27 Requires:
26 Libs: @cppad_lib_list@
27 Requires: @cppad_requires@
2828 Libs.private @cppad_libs_private@
2929 Requires.private: @cppad_requires_private@
00 # -----------------------------------------------------------------------------
1 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
1 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
22 #
33 # CppAD is distributed under the terms of the
44 # Eclipse Public License Version 2.0.
2727 URL: @cppad_url@
2828 #
2929 Cflags: -I${includedir}
30 Libs: -L{libdir} -l@cppad_lib_list@
31 Requires:
30 Libs: @cppad_lib_list@
31 Requires: @cppad_requires@
3232 Libs.private @cppad_libs_private@
3333 Requires.private: @cppad_requires_private@
00 /* --------------------------------------------------------------------------
1 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-19 Bradley M. Bell
1 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
22
33 CppAD is distributed under the terms of the
44 Eclipse Public License Version 2.0.
2020 namespace { // start empty namespace
2121 using CppAD::vector; // abbreviate CppAD::vector using vector
2222
23 // ---------------------------------------------------------------------------
24 // start definition of atomic derived class using atomic_three interface
25 class atomic_optimize : public CppAD::atomic_three<double> {
23 // ============================================================================
24 // Testing dynamic parameters in atomic_three fucntions.
25 // ============================================================================
26 class dynamic_optimize : public CppAD::atomic_three<double> {
2627 public:
2728 // can use const char* name when calling this constructor
28 atomic_optimize(const std::string& name) : // can have more arguments
29 dynamic_optimize(const std::string& name) : // can have more arguments
2930 CppAD::atomic_three<double>(name) // inform base class of name
3031 { }
3132
9697
9798 return ok;
9899 }
99 }; // End of atomic_optimize class
100 }; // End of dynamic_optimize class
100101
101102 // ---------------------------------------------------------------------------
102103 bool optimize_dynamic_one(void)
104105 using CppAD::AD;
105106 using CppAD::NearEqual;
106107 double eps = 10. * CppAD::numeric_limits<double>::epsilon();
107 atomic_optimize afun("atomic_optimize");
108 dynamic_optimize afun("dynamic_optimize");
108109 //
109110 // constant parameter
110111 double c_0 = 2.0;
153154 // sequence properties
154155 ok &= f.size_dyn_ind() == 1; // p
155156 ok &= f.size_dyn_par() == 3; // p, r, c * p
156 // Three constant parameters, phantom at index 0, c, c * c
157 // Three constant parameters: nan, c, c * c
157158 ok &= f.size_par() == 6;
158159 // Normal variables: u, p * u, u * u
159160 // Extra variables: phanton at index 0, y[0], y[1]
165166 // sequence properties
166167 ok &= f.size_dyn_ind() == 1; // p
167168 ok &= f.size_dyn_par() == 2; // c * p
168 // Three constant parameters, phantom at index 0, c, c * c
169 ok &= f.size_par() == 5;
169
170 // Four constant parameters: nan, zero, c, c * c
171 ok &= f.size_par() == 6;
172
170173 // Normal variables: u, p * u
171174 // Extra variables: phanton at index 0, y[0], y[1]
172175 ok &= f.size_var() == 5;
206209 using CppAD::AD;
207210 using CppAD::NearEqual;
208211 double eps = 10. * CppAD::numeric_limits<double>::epsilon();
209 atomic_optimize afun("atomic_optimize");
212 dynamic_optimize afun("dynamic_optimize");
210213 //
211214 // indepndent dynamic parameter vector
212215 size_t np = 1;
253256 // sequence properties
254257 ok &= f.size_dyn_ind() == 1; // p
255258 ok &= f.size_dyn_par() == 4; // p, r, r * p, r * r
256 // Two constant parameters, phantom at index 0, 2.0 in computation of r
259 // Two constant parameters: nan, 2.0 in computation of r
257260 ok &= f.size_par() == 6;
258261
259262
263266 // sequence properties
264267 ok &= f.size_dyn_ind() == 1; // p
265268 ok &= f.size_dyn_par() == 1; // p
266 // One constant parameter, phantom at index 0
267 ok &= f.size_par() == 2;
269 // Two constant paramete: nan, zero
270 ok &= f.size_par() == 3;
268271
269272 // check
270273 double check;
297300 using CppAD::AD;
298301 using CppAD::NearEqual;
299302 double eps = 10. * CppAD::numeric_limits<double>::epsilon();
300 atomic_optimize afun("atomic_optimize");
303 dynamic_optimize afun("dynamic_optimize");
301304 //
302305 // indepndent dynamic parameter vector
303306 size_t np = 1;
344347 // sequence properties
345348 ok &= f.size_dyn_ind() == 1; // p
346349 ok &= f.size_dyn_par() == 4; // p, r, r * p, p * p
347 // Two constant parameters, phantom at index 0, 2.0 in computation of r
350 // Two constant parameters: nan, 2.0 in computation of r
348351 ok &= f.size_par() == 6;
349352
350353
354357 // sequence properties
355358 ok &= f.size_dyn_ind() == 1; // p
356359 ok &= f.size_dyn_par() == 2; // p, r
357 // Two constant parameters, phantom at index 0, 2.0 in computation of r
358 ok &= f.size_par() == 4;
360 // Three constant parameters: nan, zero, 2.0 in computation of r
361 ok &= f.size_par() == 5;
359362
360363 // check
361364 double check;
384387
385388 return ok;
386389 }
390 // ============================================================================
391 // Testing Varialbes that get removed
392 // ============================================================================
393 class variable_optimize : public CppAD::atomic_three<double> {
394 public:
395 // can use const char* name when calling this constructor
396 variable_optimize(const std::string& name) : // can have more arguments
397 CppAD::atomic_three<double>(name) // inform base class of name
398 { }
399
400 private:
401 // calculate type_y
402 virtual bool for_type(
403 const vector<double>& parameter_x ,
404 const vector<CppAD::ad_type_enum>& type_x ,
405 vector<CppAD::ad_type_enum>& type_y )
406 { assert( parameter_x.size() == type_x.size() );
407 bool ok = type_x.size() == 2; // n
408 ok &= type_y.size() == 2; // m
409 if( ! ok )
410 return false;
411 type_y[0] = type_x[0];
412 type_y[1] = std::max( type_x[0], type_x[1] );
413 return true;
414 }
415 // calculate depend_x
416 virtual bool rev_depend(
417 const vector<double>& parameter_x ,
418 const vector<CppAD::ad_type_enum>& type_x ,
419 vector<bool>& depend_x ,
420 const vector<bool>& depend_y )
421 { assert( parameter_x.size() == depend_x.size() );
422 bool ok = depend_x.size() == 2; // n
423 ok &= depend_y.size() == 2; // m
424 if( ! ok )
425 return false;
426 depend_x[0] = depend_y[0] | depend_y[1];
427 depend_x[1] = depend_y[1];
428 return true;
429 }
430 virtual bool forward(
431 const vector<double>& parameter_x ,
432 const vector<CppAD::ad_type_enum>& type_x ,
433 size_t need_y ,
434 size_t order_low ,
435 size_t order_up ,
436 const vector<double>& taylor_x ,
437 vector<double>& taylor_y
438 )
439 {
440 # ifndef NDEBUG
441 size_t n = taylor_x.size() / (order_up + 1);
442 size_t m = taylor_y.size() / (order_up + 1);
443 # endif
444 assert( n == 2 );
445 assert( m == 2 );
446 assert( order_low <= order_up );
447
448 // return flag
449 bool ok = order_up == 0;
450 if( ! ok )
451 return ok;
452
453 // g_0 = exp( x_0 )
454 taylor_y[0] = std::exp( taylor_x[0] );
455 // g_1 = exp( x_0 * x_1 )
456 taylor_y[1] = std::exp( taylor_x[0] * taylor_x[1] );
457
458 return ok;
459 }
460 virtual bool reverse(
461 const vector<double>& parameter_x ,
462 const vector<CppAD::ad_type_enum>& type_x ,
463 size_t order_up ,
464 const vector<double>& taylor_x ,
465 const vector<double>& taylor_y ,
466 vector<double>& partial_x ,
467 const vector<double>& partial_y
468 )
469 {
470 size_t q1 = order_up + 1;
471 size_t n = taylor_x.size() / q1;
472 # ifndef NDEBUG
473 size_t m = taylor_y.size() / q1;
474 # endif
475 assert( n == 2 );
476 assert( m == 2 );
477
478 // return flag
479 bool ok = order_up == 0;
480 if( ! ok )
481 return ok;
482
483 // initialize summation as zero
484 for(size_t j = 0; j < n; ++j)
485 partial_x[j] = 0.0;
486
487 // g_0 = exp( x_0 )
488 partial_x[0] += partial_y[0] * taylor_y[0];
489 // g_1 = exp( x_0 * x_1 )
490 partial_x[0] += partial_y[1] * taylor_y[1] * taylor_x[1];
491 partial_x[1] += partial_y[1] * taylor_y[1] * taylor_x[0];
492 //
493 return ok;
494 }
495 }; // End of variable_optimize class
387496 // ---------------------------------------------------------------------------
497 bool optimize_variable_one(void)
498 { bool ok = true;
499 using CppAD::AD;
500 using CppAD::NearEqual;
501 double eps = 10. * CppAD::numeric_limits<double>::epsilon();
502 variable_optimize afun("variable_optimize");
503 //
504 // independent variable vector
505 size_t nu = 2;
506 CPPAD_TESTVECTOR( AD<double> ) au(nu);
507 for(size_t j = 0; j < nu; ++j)
508 au[j] = double(j + 1);
509
510 // declare independent variables and start tape recording
511 CppAD::Independent(au);
512
513 // call atomic function and store result in ay
514 CPPAD_TESTVECTOR( AD<double> ) ax(nu), av(nu);
515 for(size_t j = 0; j < nu; ++j)
516 ax[j] = au[j] / 2.0; // x = u / 2
517 afun(ax, av);
518
519 // check type of result
520 for(size_t j = 0; j < nu; ++j)
521 ok &= Variable( av[j] );
522
523 // range space vector
524 size_t ny = 1;
525 CPPAD_TESTVECTOR( AD<double> ) ay(ny);
526 //
527 // only the first component of av affects the function value
528 ay[0] = av[0];
529
530 // create f: u -> y and stop tape recording
531 CppAD::ADFun<double> f;
532 f.Dependent (au, ay); // f(u) = exp( u[0] / 2 )
533
534 // optimize
535 f.optimize();
536
537 // check
538 double check;
539
540 // check zero order forward mode
541 CPPAD_TESTVECTOR( double ) u(nu), y(ny);
542 for(size_t j = 0; j < nu; ++j)
543 u[j] = double(j + 1) / double(nu);
544 y = f.Forward(0, u);
545 check = std::exp( u[0] / 2.0 );
546 ok &= NearEqual(y[0] , check, eps, eps);
547
548 // Check first order reverse mode. This test would vaile when
549 // nan was used for argument and function values that were optimized out
550 // because they were not used.
551 CPPAD_TESTVECTOR( double ) w(ny), dw(nu);
552 w[0] = 1.0;
553 dw = f.Reverse(1, w);
554 check = std::exp( u[0] / 2.0 ) / 2.0;
555 ok &= NearEqual(dw[0] , check, eps, eps);
556 check = 0.0;
557 ok &= NearEqual(dw[1] , check, eps, eps);
558 //
559 return ok;
560 }
388561 } // End empty namespace
389562
390563 bool atomic_three(void)
392565 ok &= optimize_dynamic_one();
393566 ok &= optimize_dynamic_two();
394567 ok &= optimize_dynamic_three();
568 ok &= optimize_variable_one();
395569 return ok;
396570 }
00 /* --------------------------------------------------------------------------
1 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
1 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell
22
33 CppAD is distributed under the terms of the
44 Eclipse Public License Version 2.0.
619619 f.Dependent(ax, ay);
620620 ok &= f.size_dyn_ind() == num_dyn_ind;
621621 ok &= f.size_dyn_par() == num_dyn_ind + 2;
622 // note that there is one phantom parameter at index zero that is not used
622 // constant parameters: nan, 3.0, 4.0, 5.0
623623 ok &= f.size_par() == num_dyn_ind + 2 + 4;
624624 // -------------------------------------------------------------
625625 // vectors used for new_dynamic and Forward.
635635 ok &= NearEqual(y[1], check, eps99, eps99);
636636 // -------------------------------------------------------------
637637 // optimize and re-test
638 f.optimize();
638 f.optimize(); // add the zero parameter
639639 ok &= f.size_dyn_ind() == num_dyn_ind;
640640 ok &= f.size_dyn_par() == num_dyn_ind + 1;
641 ok &= f.size_par() == num_dyn_ind + 1 + 2;
641 // constant parameters: nan, zero, (3.0 - 4.0 + 5.0)
642 ok &= f.size_par() == num_dyn_ind + 1 + 3;
642643 dynamic[0] = 0.3;
643644 dynamic[1] = 0.4;
644645 f.new_dynamic(dynamic);
787788 f.Dependent(ax, ay);
788789 ok &= f.size_dyn_ind() == nd;
789790 ok &= f.size_dyn_par() == nd + 5;
790 // note that there is a phantom parameter at index zero that is not used
791 ok &= f.size_par() == nd + 5 + 2; // one constant parameter 3.0
791 // constant parameters: nan, 3.0
792 ok &= f.size_par() == nd + 5 + 2;
792793 // -------------------------------------------------------------
793794 // vectors used for new_dynamic and Forward.
794795 CPPAD_TESTVECTOR(double) dynamic(nd), x(nx), y(ny);
802803 ok &= NearEqual(y[1], check, eps99, eps99);
803804 // -------------------------------------------------------------
804805 // optimize and re-test
805 f.optimize();
806 f.optimize(); // adds the zero parameter
806807 ok &= f.size_dyn_ind() == nd;
807808 ok &= f.size_dyn_par() == nd + 2;
808 ok &= f.size_par() == nd + 2 + 2;
809 // constant parameters: nan, zero, 3.0
810 ok &= f.size_par() == nd + 2 + 3;
809811 dynamic[0] = 6.0;
810812 f.new_dynamic(dynamic);
811813 y = f.Forward(0, x);