@(@\newcommand{\W}[1]{ \; #1 \; }
\newcommand{\R}[1]{ {\rm #1} }
\newcommand{\B}[1]{ {\bf #1} }
\newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} }
\newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} }
\newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} }
\newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@This is cppad-20221105 documentation. Here is a link to its
current documentation
.
Atomic Euclidean Norm Squared: Example and Test
Function
This example demonstrates using atomic_three
to define the operation
@(@
g : \B{R}^n \rightarrow \B{R}^m
@)@ where
@(@
n = 2
@)@, @(@
m = 1
@)@, where
@[@
g(x) = x_0^2 + x_1^2
@]@
# include <cppad/cppad.hpp>
namespace { // isolate items below to this fileusing CppAD::vector; // abbreivate CppAD::vector as vector//class atomic_norm_sq : public CppAD::atomic_three<double> {
// reverse mode Jacobian sparstiy pattern
CppAD::sparse_rc< CPPAD_TESTVECTOR(size_t) > pattern_in, pattern_out;
pattern_in.resize(m, m, m);
for(size_t i = 0; i < m; ++i)
pattern_in.set(i, i, i);
bool transpose = false;
bool dependency = false;
bool internal_bool = false;
f.rev_jac_sparsity(
pattern_in, transpose, dependency, internal_bool, pattern_out
);
CPPAD_TESTVECTOR(size_t) row_major = pattern_out.row_major();
//// first element in row major order is (0, 0)
size_t k = 0;
size_t r = pattern_out.row()[ row_major[k] ];
size_t c = pattern_out.col()[ row_major[k] ];
ok &= r == 0 && c == 0;
//// second element in row major order is (0, 1)
++k;
r = pattern_out.row()[ row_major[k] ];
c = pattern_out.col()[ row_major[k] ];
ok &= r == 0 && c == 1;
//// k + 1 should be number of values in sparsity pattern
ok &= k + 1 == pattern_out.nnz();
// forward mode Hessian sparsity patternCPPAD_TESTVECTOR(bool) select_x(n), select_y(m);
for(size_t j = 0; j < n; ++j)
select_x[j] = true;
for(size_t i = 0; i < m; ++i)
select_y[i] = true;
f.for_hes_sparsity(
select_x, select_y, internal_bool, pattern_out
);
CPPAD_TESTVECTOR(size_t) order = pattern_out.row_major();
//// first element in row major order is (0, 0)
k = 0;
r = pattern_out.row()[ order[k] ];
c = pattern_out.col()[ order[k] ];
ok &= r == 0 && c == 0;
//// second element in row major order is (1, 1)
++k;
r = pattern_out.row()[ order[k] ];
c = pattern_out.col()[ order[k] ];
ok &= r == 1 && c == 1;
//// k + 1 should be number of values in sparsity pattern
ok &= k + 1 == pattern_out.nnz();
//return ok;
}