Prev Next Index-> contents reference index search external Up-> CppAD AD ADValued unary_standard_math abs fabs.cpp ADValued-> Arithmetic unary_standard_math binary_math CondExp Discrete numeric_limits atomic unary_standard_math-> acos asin atan cos cosh exp log log10 sin sinh sqrt tan tanh abs acosh asinh atanh erf expm1 log1p sign abs-> fabs.cpp fabs.cpp Headings

$\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}} }$
AD Absolute Value Function: Example and Test
 # include <cppad/cppad.hpp> bool fabs(void) { bool ok = true; using CppAD::AD; using CppAD::NearEqual; double eps99 = 99.0 * std::numeric_limits<double>::epsilon(); // domain space vector size_t n = 1; CPPAD_TESTVECTOR(AD<double>) x(n); x[0] = 0.; // declare independent variables and start tape recording CppAD::Independent(x); // range space vector size_t m = 6; CPPAD_TESTVECTOR(AD<double>) y(m); y[0] = fabs(x[0] - 1.); y[1] = fabs(x[0]); y[2] = fabs(x[0] + 1.); // y[3] = fabs(x[0] - 1.); y[4] = fabs(x[0]); y[5] = fabs(x[0] + 1.); // create f: x -> y and stop tape recording CppAD::ADFun<double> f(x, y); // check values ok &= (y[0] == 1.); ok &= (y[1] == 0.); ok &= (y[2] == 1.); // ok &= (y[3] == 1.); ok &= (y[4] == 0.); ok &= (y[5] == 1.); // forward computation of partials w.r.t. a positive x[0] direction size_t p = 1; CPPAD_TESTVECTOR(double) dx(n), dy(m); dx[0] = 1.; dy = f.Forward(p, dx); ok &= (dy[0] == - dx[0]); ok &= (dy[1] == 0. ); // used to be (dy[1] == + dx[0]); ok &= (dy[2] == + dx[0]); // ok &= (dy[3] == - dx[0]); ok &= (dy[4] == 0. ); // used to be (dy[1] == + dx[0]); ok &= (dy[5] == + dx[0]); // forward computation of partials w.r.t. a negative x[0] direction dx[0] = -1.; dy = f.Forward(p, dx); ok &= (dy[0] == - dx[0]); ok &= (dy[1] == 0. ); // used to be (dy[1] == - dx[0]); ok &= (dy[2] == + dx[0]); // ok &= (dy[3] == - dx[0]); ok &= (dy[4] == 0. ); // used to be (dy[1] == - dx[0]); ok &= (dy[5] == + dx[0]); // reverse computation of derivative of y[0] p = 1; CPPAD_TESTVECTOR(double) w(m), dw(n); w[0] = 1.; w[1] = 0.; w[2] = 0.; w[3] = 0.; w[4] = 0.; w[5] = 0.; dw = f.Reverse(p, w); ok &= (dw[0] == -1.); // reverse computation of derivative of y[1] w[0] = 0.; w[1] = 1.; dw = f.Reverse(p, w); ok &= (dw[0] == 0.); // reverse computation of derivative of y[5] w[1] = 0.; w[5] = 1.; dw = f.Reverse(p, w); ok &= (dw[0] == 1.); // use a VecAD<Base>::reference object with abs and fabs CppAD::VecAD<double> v(1); AD<double> zero(0); v[zero] = -1; AD<double> result = fabs(v[zero]); ok &= NearEqual(result, 1., eps99, eps99); result = fabs(v[zero]); ok &= NearEqual(result, 1., eps99, eps99); return ok; } 
Input File: example/general/fabs.cpp