File: rev_depend.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (112 lines) | stat: -rw-r--r-- 3,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-23 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin atomic_four_vector_rev_depend.cpp}

Example Optimizing Atomic Vector Usage
######################################

f(u, v)
*******
For this example,
:math:`f : \B{R}^{3m} \rightarrow \B{R}`
is defined by :math:`f(u, v, w) = - ( u_0 + v_0 ) * w_0`.
where *u* , *v* , and *w* are in :math:`\B{R}^m`.

Source
******
{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end atomic_four_vector_rev_depend.cpp}
*/
// BEGIN C++
# include <cppad/cppad.hpp>
# include <cppad/example/atomic_four/vector/vector.hpp>
bool rev_depend(void)
{  bool ok = true;
   using CppAD::NearEqual;
   using CppAD::AD;
   //
   // vec_op
   // atomic vector_op object
   CppAD::atomic_vector<double> vec_op("atomic_vector");
   //
   // m
   // size of u, v, and w
   size_t m = 6;
   //
   // n
   size_t n = 3 * m;
   //
   // add_op, mul_op, neg_op
   typedef CppAD::atomic_vector<double>::op_enum_t op_enum_t;
   op_enum_t add_op = CppAD::atomic_vector<double>::add_enum;
   op_enum_t mul_op = CppAD::atomic_vector<double>::mul_enum;
   op_enum_t neg_op = CppAD::atomic_vector<double>::neg_enum;
   // -----------------------------------------------------------------------
   // Record f(u, v, w) = - (u + v) * w
   // -----------------------------------------------------------------------
   // Independent variable vector
   CPPAD_TESTVECTOR( CppAD::AD<double> ) a_ind(n);
   for(size_t j = 0; j < n; ++j)
      a_ind[j] = AD<double>(1 + j);
   CppAD::Independent(a_ind);
   //
   // au, av, aw
   CPPAD_TESTVECTOR( CppAD::AD<double> ) au(m), av(m), aw(m);
   for(size_t i = 0; i < m; ++i)
   {  au[i] = a_ind[0 * m + i];
      av[i] = a_ind[1 * m + i];
      aw[i] = a_ind[2 * m + i];
   }
   //
   // ax = (au, av)
   CPPAD_TESTVECTOR( CppAD::AD<double> ) ax(2 * m);
   for(size_t i = 0; i < m; ++i)
   {  ax[i]     = au[i];
      ax[m + i] = av[i];
   }
   //
   // ay = u + v
   CPPAD_TESTVECTOR( CppAD::AD<double> ) ay(m);
   vec_op(add_op, ax, ay);
   //
   // ax = (ay, aw)
   for(size_t i = 0; i < m; ++i)
   {  ax[i]     = ay[i];
      ax[m + i] = aw[i];
   }
   //
   // az = ay * w
   CPPAD_TESTVECTOR( CppAD::AD<double> ) az(m);
   vec_op(mul_op, ax, az);
   //
   // ay = - az
   vec_op(neg_op, az, ay);
   //
   // f
   CPPAD_TESTVECTOR( CppAD::AD<double> ) a_dep(1);
   a_dep[0] = ay[0];
   CppAD::ADFun<double> f(a_ind, a_dep);
   //
   // size_var
   // phantom variable, independent variables, operator results
   ok   &= f.size_var() == 1 + n + 3 * m;
   //
   // optimize
   // The atomic function rev_depend routine is called by optimizer
   f.optimize("val_graph no_conditional_skip");
   //
   // size_var
   // phantom variablem, independent variables, operator variables
   ok &= f.size_var() == 1 + n + 3;
   //
   //
   return ok;
}
// END C++