Prev Next Index-> contents reference index search external Up-> CppAD ADFun abs_normal abs_eval abs_eval.cpp ADFun-> record_adfun drivers Forward Reverse sparsity_pattern sparse_derivative optimize abs_normal FunCheck check_for_nan abs_normal-> abs_normal_fun abs_print_mat abs_eval simplex_method lp_box abs_min_linear min_nso_linear qp_interior qp_box abs_min_quad min_nso_quad abs_eval-> abs_eval.cpp abs_eval.hpp abs_eval.cpp Headings-> Purpose Source

$\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}} }$
abs_eval: Example and Test

Purpose
The function $f : \B{R}^3 \rightarrow \B{R}$ defined by $$f( x_0, x_1, x_2 ) = | x_0 + x_1 | + | x_1 + x_2 |$$ is affine, except for its absolute value terms. For this case, the abs_normal approximation should be equal to the function itself.

Source

# include "abs_eval.hpp"

namespace {
const CPPAD_TESTVECTOR(double)& x ,
const CPPAD_TESTVECTOR(double)& u )
{     size_t n = x.size();
size_t s = u.size();
CPPAD_TESTVECTOR(double) xu(n + s);
for(size_t j = 0; j < n; j++)
xu[j] = x[j];
for(size_t j = 0; j < s; j++)
xu[n + j] = u[j];
return xu;
}
}
bool abs_eval(void)
{     bool ok = true;
//
//
//
double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
//
size_t n = 3; // size of x
size_t m = 1; // size of y
size_t s = 2; // number of absolute value terms
//
// record the function f(x)
for(size_t j = 0; j < n; j++)
ad_x[j] = double(j + 1);
// for this example, we ensure first absolute value is | x_0 + x_1 |
// and second absolute value is | x_1 + x_2 |

// create its abs_normal representation in g, a
f.abs_normal_fun(g, a);

// check dimension of domain and range space for g
ok &= g.Domain() == n + s;
ok &= g.Range()  == m + s;

// check dimension of domain and range space for a
ok &= a.Domain() == n;
ok &= a.Range()  == s;

// --------------------------------------------------------------------
// Choose a point x_hat
d_vector x_hat(n);
for(size_t j = 0; j < n; j++)
x_hat[j] = double(j - 1);

// value of a_hat = a(x_hat)
d_vector a_hat = a.Forward(0, x_hat);

// (x_hat, a_hat)
d_vector xu_hat = join(x_hat, a_hat);

// value of g[ x_hat, a_hat ]
d_vector g_hat = g.Forward(0, xu_hat);

// Jacobian of g[ x_hat, a_hat ]
d_vector g_jac = g.Jacobian(xu_hat);

// value of delta_x
d_vector delta_x(n);
delta_x[0] =  1.0;
delta_x[1] = -2.0;
delta_x[2] = +2.0;

// value of x
d_vector x(n);
for(size_t j = 0; j < n; j++)
x[j] = x_hat[j] + delta_x[j];

// value of f(x)
d_vector y = f.Forward(0, x);

// value of g_tilde
d_vector g_tilde = CppAD::abs_eval(n, m, s, g_hat, g_jac, delta_x);

// should be equal because f is affine, except for abs terms
ok &= CppAD::NearEqual(y[0], g_tilde[0], eps99, eps99);

return ok;
}

Input File: example/abs_normal/abs_eval.cpp