File: fabs.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 (67 lines) | stat: -rw-r--r-- 1,766 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
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
Test of directional derivative in AD< AD< double > > case.
*/

# include <cppad/cppad.hpp>


bool fabs(void)
{  // test if CppAD::abs uses if statement during forward computations
   bool ok = true;

   using CppAD::Independent;
   using CppAD::ADFun;
   typedef CppAD::AD<double>      ADdouble;
   typedef CppAD::AD< ADdouble > ADDdouble;

   // af(x) = |x|
   CPPAD_TESTVECTOR( ADDdouble ) aax(1), aay(1);
   aax[0] = ADDdouble(0.);
   CppAD::Independent(aax);
   aay[0] = fabs(aax[0]);
   CppAD::ADFun< ADdouble > af(aax, aay);

   // f(x) = |x|
   CPPAD_TESTVECTOR( ADdouble ) ax(1), ay(1);
   ax[0] = ADdouble(0.);
   CppAD::Independent(ax);
   ay    = af.Forward(0, ax);
   CppAD::ADFun<double> f(ax, ay);

   // compute derivative of af at a positive argument
   CPPAD_TESTVECTOR( ADdouble ) adx(1), ady(1);
   ax[0]  = 1.;
   ay     = af.Forward(0, ax);
   adx[0] = 1;
   ady    = af.Forward(1, adx);
   ok    &= (ady[0] == 1.);

   // compute derivative of af at a zero argument
   ax[0]  = 0.;
   ay     = af.Forward(0, ax);
   adx[0] = 1;
   ady    = af.Forward(1, adx);
   ok    &= (ady[0] == 0.);

   // compute derivative of f at zero argument
   CPPAD_TESTVECTOR(double) x(1), y(1), dx(1), dy(1);
   x[0]  = 0.;
   y     = f.Forward(0, x);
   dx[0] = 1;
   dy    = f.Forward(1, dx);
   ok    &= (dy[0] == 0.);

   // compute derivative of af at a negative argument
   x[0]  = -1.;
   y     = f.Forward(0, x);
   dx[0] = 1;
   dy    = f.Forward(1, dx);
   ok    &= (dy[0] == -1.);

   return ok;
}