MCPcopy Create free account
hub / github.com/coin-or/CppAD / get_started

Function get_started

example/abs_normal/get_started.cpp:65–140  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63 }
64}
65bool get_started(void)
66{ bool ok = true;
67 //
68 using CppAD::AD;
69 using CppAD::ADFun;
70 //
71 size_t n = 3; // size of x
72 size_t m = 1; // size of y
73 size_t s = 2; // size of u and z
74 //
75 // record the function f(x)
76 CPPAD_TESTVECTOR( AD<double> ) ax(n), ay(m);
77 for(size_t j = 0; j < n; j++)
78 ax[j] = double(j + 1);
79 Independent( ax );
80 // for this example, we ensure first absolute value is | x_0 + x_1 |
81 AD<double> a0 = abs( ax[0] + ax[1] );
82 // and second absolute value is | x_1 + x_2 |
83 AD<double> a1 = abs( ax[1] + ax[2] );
84 ay[0] = a0 + a1;
85 ADFun<double> f(ax, ay);
86
87 // create its abs_normal representation in g, a
88 ADFun<double> g, a;
89 f.abs_normal_fun(g, a);
90
91 // check dimension of domain and range space for g
92 ok &= g.Domain() == n + s;
93 ok &= g.Range() == m + s;
94
95 // check dimension of domain and range space for a
96 ok &= a.Domain() == n;
97 ok &= a.Range() == s;
98
99 // --------------------------------------------------------------------
100 // a(x) has all the operations used to compute f(x), but the sum of the
101 // absolute values is not needed for a(x), so optimize it out.
102 size_t n_op = f.size_op();
103 ok &= a.size_op() == n_op;
104 a.optimize();
105 ok &= a.size_op() < n_op;
106
107 // --------------------------------------------------------------------
108 // zero order forward mode calculation using g(x, u)
109 CPPAD_TESTVECTOR(double) x(n), u(s), xu(n+s), yz(m+s);
110 for(size_t j = 0; j < n; j++)
111 x[j] = double(j + 2);
112 for(size_t j = 0; j < s; j++)
113 u[j] = double(j + n + 2);
114 xu = join(x, u);
115 yz = g.Forward(0, xu);
116
117 // check y_0(x, u)
118 double y0 = u[0] + u[1];
119 ok &= y0 == yz[0];
120
121 // check z_0 (x, u)
122 double z0 = x[0] + x[1];

Callers

nothing calls this directly

Calls 9

abs_normal_funMethod · 0.80
DomainMethod · 0.80
RangeMethod · 0.80
size_opMethod · 0.80
optimizeMethod · 0.80
ForwardMethod · 0.80
joinFunction · 0.70
IndependentFunction · 0.50
absFunction · 0.50

Tested by

no test coverage detected