New upstream version 2021.00.00.8
Barak A. Pearlmutter
2 years ago
24 | 24 | endif() |
25 | 25 | # |
26 | 26 | # cppad_version is used by version.sh to get the version number. |
27 | SET(cppad_version "20210000.4") | |
27 | SET(cppad_version "20210000.8") | |
28 | 28 | SET(cppad_url "https://coin-or.github.io/CppAD" ) |
29 | 29 | SET(cppad_description "Differentiation of C++ Algorithms" ) |
30 | 30 | # |
0 | 0 | #! /bin/bash -e |
1 | 1 | # ----------------------------------------------------------------------------- |
2 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
2 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
3 | 3 | # |
4 | 4 | # CppAD is distributed under the terms of the |
5 | 5 | # Eclipse Public License Version 2.0. |
71 | 71 | exit 1 |
72 | 72 | fi |
73 | 73 | 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 | ) | |
75 | 79 | if [ "$count" != '0' ] |
76 | 80 | then |
77 | 81 | head "$top_srcdir/check_all.warn" |
0 | 0 | #! /bin/bash -e |
1 | 1 | # ----------------------------------------------------------------------------- |
2 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
2 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
3 | 3 | # |
4 | 4 | # CppAD is distributed under the terms of the |
5 | 5 | # Eclipse Public License Version 2.0. |
201 | 201 | fi |
202 | 202 | # ----------------------------------------------------------------------------- |
203 | 203 | # cppad_prefix |
204 | cmake_args="$cmake_args -D cppad_prefix=$(pwd)/build/prefix" | |
204 | cmake_args="$cmake_args -D cppad_prefix=$prefix" | |
205 | 205 | # |
206 | 206 | # cmake_install_includedirs |
207 | 207 | if [ -d '/usr/include' ] |
0 | 0 | #! /bin/sh |
1 | 1 | # 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. | |
3 | 3 | # |
4 | 4 | # Report bugs to <cppad@list.coin-or.org>. |
5 | 5 | # |
578 | 578 | # Identity of this package. |
579 | 579 | PACKAGE_NAME='cppad' |
580 | 580 | 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' | |
583 | 583 | PACKAGE_BUGREPORT='cppad@list.coin-or.org' |
584 | 584 | PACKAGE_URL='' |
585 | 585 | |
1351 | 1351 | # Omit some internal or obsolete options to make the list less imposing. |
1352 | 1352 | # This message is too long to be a string in the A/UX 3.1 sh. |
1353 | 1353 | 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. | |
1355 | 1355 | |
1356 | 1356 | Usage: $0 [OPTION]... [VAR=VALUE]... |
1357 | 1357 | |
1421 | 1421 | |
1422 | 1422 | if test -n "$ac_init_help"; then |
1423 | 1423 | case $ac_init_help in |
1424 | short | recursive ) echo "Configuration of cppad 20210000.4:";; | |
1424 | short | recursive ) echo "Configuration of cppad 20210000.8:";; | |
1425 | 1425 | esac |
1426 | 1426 | cat <<\_ACEOF |
1427 | 1427 | |
1543 | 1543 | test -n "$ac_init_help" && exit $ac_status |
1544 | 1544 | if $ac_init_version; then |
1545 | 1545 | cat <<\_ACEOF |
1546 | cppad configure 20210000.4 | |
1546 | cppad configure 20210000.8 | |
1547 | 1547 | generated by GNU Autoconf 2.69 |
1548 | 1548 | |
1549 | 1549 | Copyright (C) 2012 Free Software Foundation, Inc. |
1916 | 1916 | This file contains any messages produced by compilers while |
1917 | 1917 | running configure, to aid debugging if configure makes a mistake. |
1918 | 1918 | |
1919 | It was created by cppad $as_me 20210000.4, which was | |
1919 | It was created by cppad $as_me 20210000.8, which was | |
1920 | 1920 | generated by GNU Autoconf 2.69. Invocation command line was |
1921 | 1921 | |
1922 | 1922 | $ $0 $@ |
2806 | 2806 | |
2807 | 2807 | # Define the identity of the package. |
2808 | 2808 | PACKAGE='cppad' |
2809 | VERSION='20210000.4' | |
2809 | VERSION='20210000.8' | |
2810 | 2810 | |
2811 | 2811 | |
2812 | 2812 | cat >>confdefs.h <<_ACEOF |
7648 | 7648 | # report actual input values of CONFIG_FILES etc. instead of their |
7649 | 7649 | # values after options handling. |
7650 | 7650 | 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 | |
7652 | 7652 | generated by GNU Autoconf 2.69. Invocation command line was |
7653 | 7653 | |
7654 | 7654 | CONFIG_FILES = $CONFIG_FILES |
7705 | 7705 | cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 |
7706 | 7706 | ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`" |
7707 | 7707 | ac_cs_version="\\ |
7708 | cppad config.status 20210000.4 | |
7708 | cppad config.status 20210000.8 | |
7709 | 7709 | configured by $0, generated by GNU Autoconf 2.69, |
7710 | 7710 | with options \\"\$ac_cs_config\\" |
7711 | 7711 |
10 | 10 | # ----------------------------------------------------------------------------- |
11 | 11 | dnl Process this file with autoconf to produce a configure script. |
12 | 12 | 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]) | |
14 | 14 | AM_SILENT_RULES([no]) |
15 | 15 | |
16 | 16 | dnl By defalut disable maintainer mode when running configure; |
0 | 0 | # ifndef CPPAD_CORE_ATAN2_HPP |
1 | 1 | # define CPPAD_CORE_ATAN2_HPP |
2 | 2 | /* -------------------------------------------------------------------------- |
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-18 Bradley M. Bell | |
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
4 | 4 | |
5 | 5 | CppAD is distributed under the terms of the |
6 | 6 | Eclipse Public License Version 2.0. |
90 | 90 | // BEGIN CondExp |
91 | 91 | template <class Base> |
92 | 92 | 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 | |
97 | 95 | AD<Base> zero(0.); |
98 | 96 | AD<Base> pi2(2. * atan(1.)); |
99 | 97 | 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; | |
120 | 133 | } |
121 | 134 | // END CondExp |
122 | 135 |
1 | 1 | # define CPPAD_CORE_GRAPH_TO_GRAPH_HPP |
2 | 2 | |
3 | 3 | /* -------------------------------------------------------------------------- |
4 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
4 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
5 | 5 | |
6 | 6 | CppAD is distributed under the terms of the |
7 | 7 | Eclipse Public License Version 2.0. |
203 | 203 | // unary operators |
204 | 204 | |
205 | 205 | case local::abs_dyn: |
206 | case local::fabs_dyn: | |
206 | 207 | graph_op = abs_graph_op; |
207 | 208 | break; |
208 | 209 |
0 | 0 | # ifndef CPPAD_CORE_REVERSE_HPP |
1 | 1 | # define CPPAD_CORE_REVERSE_HPP |
2 | 2 | /* -------------------------------------------------------------------------- |
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-19 Bradley M. Bell | |
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
4 | 4 | |
5 | 5 | CppAD is distributed under the terms of the |
6 | 6 | Eclipse Public License Version 2.0. |
149 | 149 | Partial[dep_taddr_[i] * q + q - 1] += w[i]; |
150 | 150 | else |
151 | 151 | { 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 ]; | |
154 | 153 | } |
155 | 154 | } |
156 | 155 |
0 | 0 | # ifndef CPPAD_LOCAL_OPTIMIZE_OPTIMIZE_RUN_HPP |
1 | 1 | # define CPPAD_LOCAL_OPTIMIZE_OPTIMIZE_RUN_HPP |
2 | 2 | /* -------------------------------------------------------------------------- |
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
4 | 4 | |
5 | 5 | CppAD is distributed under the terms of the |
6 | 6 | Eclipse Public License Version 2.0. |
205 | 205 | ); |
206 | 206 | |
207 | 207 | // number of variables in the player |
208 | # ifndef NDEBUG | |
208 | 209 | const size_t num_var = play->num_var_rec(); |
210 | # endif | |
209 | 211 | |
210 | 212 | // number of parameter in the player |
211 | 213 | const size_t num_par = play->num_par_rec(); |
296 | 298 | ); |
297 | 299 | // ----------------------------------------------------------------------- |
298 | 300 | |
299 | // nan with type Base | |
300 | Base base_nan = Base( std::numeric_limits<double>::quiet_NaN() ); | |
301 | ||
302 | // ------------------------------------------------------------- | |
303 | 301 | // conditional expression information |
304 | 302 | // |
305 | 303 | // Size of the conditional expression information structure. |
374 | 372 | new_par[i_par] = rec->put_con_par(par); |
375 | 373 | } |
376 | 374 | } |
375 | ||
376 | // index corresponding to the parameter zero | |
377 | addr_t zero_par_index = rec->put_con_par( Base(0) ); | |
377 | 378 | |
378 | 379 | // set new_par for the dependent dynamic parameters |
379 | 380 | size_t i_dyn = num_dynamic_ind; // dynamic parmaeter index |
1183 | 1184 | CPPAD_ASSERT_UNKNOWN( previous == 0 ); |
1184 | 1185 | CPPAD_ASSERT_NARG_NRES(op, 1, 0); |
1185 | 1186 | 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]); | |
1190 | 1190 | new_op[i_op] = addr_t( rec->num_op_rec() ); |
1191 | 1191 | rec->PutOp(FunapOp); |
1192 | 1192 | CPPAD_ASSERT_UNKNOWN( atom_state == arg_atom ); |
1199 | 1199 | CPPAD_ASSERT_UNKNOWN( previous == 0 ); |
1200 | 1200 | CPPAD_ASSERT_NARG_NRES(op, 1, 0); |
1201 | 1201 | 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 ) | |
1203 | 1204 | { rec->PutArg(new_arg[0]); |
1204 | 1205 | new_op[i_op] = addr_t( rec->num_op_rec() ); |
1205 | 1206 | rec->PutOp(FunavOp); |
1207 | 1208 | else |
1208 | 1209 | { // This argument does not affect the result and |
1209 | 1210 | // 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; | |
1211 | 1212 | rec->PutArg(new_arg[0]); |
1212 | 1213 | new_op[i_op] = addr_t( rec->num_op_rec() ); |
1213 | 1214 | rec->PutOp(FunapOp); |
1222 | 1223 | CPPAD_ASSERT_UNKNOWN( previous == 0 ); |
1223 | 1224 | CPPAD_ASSERT_NARG_NRES(op, 1, 0); |
1224 | 1225 | 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 ) | |
1230 | 1227 | { // This parameter is not used here or anywhere. |
1231 | 1228 | 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 | // | |
1234 | 1233 | new_op[i_op] = addr_t( rec->num_op_rec() ); |
1235 | 1234 | rec->PutOp(FunrpOp); |
1236 | 1235 | CPPAD_ASSERT_UNKNOWN( atom_state == ret_atom ); |
1249 | 1248 | { // change FunrvOp -> FunrpOp to avoid creating new variable |
1250 | 1249 | CPPAD_ASSERT_UNKNOWN( op_usage[i_op] == usage_t(no_usage) ); |
1251 | 1250 | CPPAD_ASSERT_NARG_NRES(FunrpOp, 1, 0); |
1252 | rec->PutArg(0); // result not used | |
1251 | rec->PutArg( zero_par_index ); | |
1253 | 1252 | rec->PutOp(FunrpOp); |
1254 | 1253 | } |
1255 | 1254 |
64 | 64 | |
65 | 65 | $comment bin/version assumes that : follows cppad version number here$$ |
66 | 66 | $section |
67 | cppad-20210000.4: A C++ Algorithmic Differentiation Package$$ | |
67 | cppad-20210000.8: A C++ Algorithmic Differentiation Package$$ | |
68 | 68 | |
69 | 69 | $comment =================================================================== $$ |
70 | 70 | $align middle$$ |
0 | 0 | # ----------------------------------------------------------------------------- |
1 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
1 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
2 | 2 | # |
3 | 3 | # CppAD is distributed under the terms of the |
4 | 4 | # Eclipse Public License Version 2.0. |
121 | 121 | # and is set by cmake to the top soruce directory. |
122 | 122 | # |
123 | 123 | # cppad_includedir |
124 | LIST(GET cmake_install_libdirs 0 cppad_includedir) | |
124 | LIST(GET cmake_install_includedirs 0 cppad_includedir) | |
125 | 125 | # |
126 | 126 | # cppad_libdir |
127 | 127 | LIST(GET cmake_install_libdirs 0 cppad_libdir) |
128 | 128 | # ----------------------------------------------------------------------------- |
129 | 129 | # 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 "") | |
131 | 132 | SET(cppad_libs_private "") |
132 | 133 | SET(cppad_requires_private "") |
133 | 134 | # |
143 | 144 | IF( NOT colpack_libdir ) |
144 | 145 | MESSAGE(FATAL_ERROR "Cannit find libColPack.* below ${colpack_prefix}") |
145 | 146 | 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" | |
148 | 149 | ) |
149 | 150 | ENDIF( cppad_has_colpack ) |
150 | 151 | # |
151 | 152 | # Ipopt has a pkgconfig file. |
152 | 153 | IF( cppad_has_ipopt ) |
153 | SET(cppad_requires_private "${cppad_requires} ipopt") | |
154 | SET(cppad_requires "${cppad_requires} ipopt") | |
154 | 155 | SET(cppad_lib_list "${cppad_lib_list} -lcppad_ipopt") |
155 | 156 | ENDIF( cppad_has_ipopt ) |
156 | 157 | # ----------------------------------------------------------------------------- |
158 | 159 | CONFIGURE_FILE( |
159 | 160 | ${CMAKE_CURRENT_SOURCE_DIR}/cppad.pc.in |
160 | 161 | ${CMAKE_CURRENT_BINARY_DIR}/cppad.pc |
162 | @ONLY | |
161 | 163 | ) |
162 | 164 | # cppad-uninstalled.pc |
163 | 165 | CONFIGURE_FILE( |
164 | 166 | ${CMAKE_CURRENT_SOURCE_DIR}/cppad-uninstalled.pc.in |
165 | 167 | ${CMAKE_CURRENT_BINARY_DIR}/cppad-uninstalled.pc |
168 | @ONLY | |
166 | 169 | ) |
167 | 170 | # During install copy cppad.pc to datadir |
168 | 171 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/cppad.pc |
0 | 0 | # ----------------------------------------------------------------------------- |
1 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
1 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
2 | 2 | # |
3 | 3 | # CppAD is distributed under the terms of the |
4 | 4 | # Eclipse Public License Version 2.0. |
23 | 23 | URL: @cppad_url@ |
24 | 24 | # |
25 | 25 | Cflags: -I${includedir} |
26 | Libs: -L{libdir} -l@cppad_lib_list@ | |
27 | Requires: | |
26 | Libs: @cppad_lib_list@ | |
27 | Requires: @cppad_requires@ | |
28 | 28 | Libs.private @cppad_libs_private@ |
29 | 29 | Requires.private: @cppad_requires_private@ |
0 | 0 | # ----------------------------------------------------------------------------- |
1 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
1 | # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
2 | 2 | # |
3 | 3 | # CppAD is distributed under the terms of the |
4 | 4 | # Eclipse Public License Version 2.0. |
27 | 27 | URL: @cppad_url@ |
28 | 28 | # |
29 | 29 | Cflags: -I${includedir} |
30 | Libs: -L{libdir} -l@cppad_lib_list@ | |
31 | Requires: | |
30 | Libs: @cppad_lib_list@ | |
31 | Requires: @cppad_requires@ | |
32 | 32 | Libs.private @cppad_libs_private@ |
33 | 33 | Requires.private: @cppad_requires_private@ |
0 | 0 | /* -------------------------------------------------------------------------- |
1 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-19 Bradley M. Bell | |
1 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
2 | 2 | |
3 | 3 | CppAD is distributed under the terms of the |
4 | 4 | Eclipse Public License Version 2.0. |
20 | 20 | namespace { // start empty namespace |
21 | 21 | using CppAD::vector; // abbreviate CppAD::vector using vector |
22 | 22 | |
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> { | |
26 | 27 | public: |
27 | 28 | // 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 | |
29 | 30 | CppAD::atomic_three<double>(name) // inform base class of name |
30 | 31 | { } |
31 | 32 | |
96 | 97 | |
97 | 98 | return ok; |
98 | 99 | } |
99 | }; // End of atomic_optimize class | |
100 | }; // End of dynamic_optimize class | |
100 | 101 | |
101 | 102 | // --------------------------------------------------------------------------- |
102 | 103 | bool optimize_dynamic_one(void) |
104 | 105 | using CppAD::AD; |
105 | 106 | using CppAD::NearEqual; |
106 | 107 | double eps = 10. * CppAD::numeric_limits<double>::epsilon(); |
107 | atomic_optimize afun("atomic_optimize"); | |
108 | dynamic_optimize afun("dynamic_optimize"); | |
108 | 109 | // |
109 | 110 | // constant parameter |
110 | 111 | double c_0 = 2.0; |
153 | 154 | // sequence properties |
154 | 155 | ok &= f.size_dyn_ind() == 1; // p |
155 | 156 | 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 | |
157 | 158 | ok &= f.size_par() == 6; |
158 | 159 | // Normal variables: u, p * u, u * u |
159 | 160 | // Extra variables: phanton at index 0, y[0], y[1] |
165 | 166 | // sequence properties |
166 | 167 | ok &= f.size_dyn_ind() == 1; // p |
167 | 168 | 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 | ||
170 | 173 | // Normal variables: u, p * u |
171 | 174 | // Extra variables: phanton at index 0, y[0], y[1] |
172 | 175 | ok &= f.size_var() == 5; |
206 | 209 | using CppAD::AD; |
207 | 210 | using CppAD::NearEqual; |
208 | 211 | double eps = 10. * CppAD::numeric_limits<double>::epsilon(); |
209 | atomic_optimize afun("atomic_optimize"); | |
212 | dynamic_optimize afun("dynamic_optimize"); | |
210 | 213 | // |
211 | 214 | // indepndent dynamic parameter vector |
212 | 215 | size_t np = 1; |
253 | 256 | // sequence properties |
254 | 257 | ok &= f.size_dyn_ind() == 1; // p |
255 | 258 | 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 | |
257 | 260 | ok &= f.size_par() == 6; |
258 | 261 | |
259 | 262 | |
263 | 266 | // sequence properties |
264 | 267 | ok &= f.size_dyn_ind() == 1; // p |
265 | 268 | 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; | |
268 | 271 | |
269 | 272 | // check |
270 | 273 | double check; |
297 | 300 | using CppAD::AD; |
298 | 301 | using CppAD::NearEqual; |
299 | 302 | double eps = 10. * CppAD::numeric_limits<double>::epsilon(); |
300 | atomic_optimize afun("atomic_optimize"); | |
303 | dynamic_optimize afun("dynamic_optimize"); | |
301 | 304 | // |
302 | 305 | // indepndent dynamic parameter vector |
303 | 306 | size_t np = 1; |
344 | 347 | // sequence properties |
345 | 348 | ok &= f.size_dyn_ind() == 1; // p |
346 | 349 | 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 | |
348 | 351 | ok &= f.size_par() == 6; |
349 | 352 | |
350 | 353 | |
354 | 357 | // sequence properties |
355 | 358 | ok &= f.size_dyn_ind() == 1; // p |
356 | 359 | 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; | |
359 | 362 | |
360 | 363 | // check |
361 | 364 | double check; |
384 | 387 | |
385 | 388 | return ok; |
386 | 389 | } |
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 | |
387 | 496 | // --------------------------------------------------------------------------- |
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 | } | |
388 | 561 | } // End empty namespace |
389 | 562 | |
390 | 563 | bool atomic_three(void) |
392 | 565 | ok &= optimize_dynamic_one(); |
393 | 566 | ok &= optimize_dynamic_two(); |
394 | 567 | ok &= optimize_dynamic_three(); |
568 | ok &= optimize_variable_one(); | |
395 | 569 | return ok; |
396 | 570 | } |
0 | 0 | /* -------------------------------------------------------------------------- |
1 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell | |
1 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-21 Bradley M. Bell | |
2 | 2 | |
3 | 3 | CppAD is distributed under the terms of the |
4 | 4 | Eclipse Public License Version 2.0. |
619 | 619 | f.Dependent(ax, ay); |
620 | 620 | ok &= f.size_dyn_ind() == num_dyn_ind; |
621 | 621 | 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 | |
623 | 623 | ok &= f.size_par() == num_dyn_ind + 2 + 4; |
624 | 624 | // ------------------------------------------------------------- |
625 | 625 | // vectors used for new_dynamic and Forward. |
635 | 635 | ok &= NearEqual(y[1], check, eps99, eps99); |
636 | 636 | // ------------------------------------------------------------- |
637 | 637 | // optimize and re-test |
638 | f.optimize(); | |
638 | f.optimize(); // add the zero parameter | |
639 | 639 | ok &= f.size_dyn_ind() == num_dyn_ind; |
640 | 640 | 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; | |
642 | 643 | dynamic[0] = 0.3; |
643 | 644 | dynamic[1] = 0.4; |
644 | 645 | f.new_dynamic(dynamic); |
787 | 788 | f.Dependent(ax, ay); |
788 | 789 | ok &= f.size_dyn_ind() == nd; |
789 | 790 | 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; | |
792 | 793 | // ------------------------------------------------------------- |
793 | 794 | // vectors used for new_dynamic and Forward. |
794 | 795 | CPPAD_TESTVECTOR(double) dynamic(nd), x(nx), y(ny); |
802 | 803 | ok &= NearEqual(y[1], check, eps99, eps99); |
803 | 804 | // ------------------------------------------------------------- |
804 | 805 | // optimize and re-test |
805 | f.optimize(); | |
806 | f.optimize(); // adds the zero parameter | |
806 | 807 | ok &= f.size_dyn_ind() == nd; |
807 | 808 | 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; | |
809 | 811 | dynamic[0] = 6.0; |
810 | 812 | f.new_dynamic(dynamic); |
811 | 813 | y = f.Forward(0, x); |