File: pow_int.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 (55 lines) | stat: -rw-r--r-- 1,575 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
// 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
// ----------------------------------------------------------------------------

/*
Old example now just used for validation testing.
*/

# include <cppad/cppad.hpp>

bool PowInt(void)
{  bool ok = true;
   using CppAD::pow;
   using CppAD::exp;
   using CppAD::log;
   using namespace CppAD;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();


   // independent variable vector, indices, values, and declaration
   CPPAD_TESTVECTOR(AD<double>) U(1);
   U[0]     = 2.;
   Independent(U);

   // dependent variable vector and indices
   CPPAD_TESTVECTOR(AD<double>) Z(2);

   // dependent variable values
   Z[0]         = pow(U[0], 5);     // x = u^5
   Z[1]         = pow(U[0], -5);    // y = u^{-5}

   // create f: U -> Z and vectors used for derivative calculations
   ADFun<double> f(U, Z);
   CPPAD_TESTVECTOR(double) v( f.Domain() );
   CPPAD_TESTVECTOR(double) w( f.Range() );

   /*
   x_u = 5 * u^4
   y_u = - 5 * u^{-6}
   */

   // check function values values
   double u = Value(U[0]);
   ok &= NearEqual(Z[0] , exp( log(u) * 5.),              eps99 , eps99);
   ok &= NearEqual(Z[1] , exp( - log(u) * 5.),            eps99 , eps99);

   // forward computation of partials
   v[0] = 1.;
   w = f.Forward(1, v);
   ok &= NearEqual(w[0] , 5. * exp( log(u) * 4.),         eps99 , eps99);
   ok &= NearEqual(w[1] , - 5. * exp( - log(u) * 6.),     eps99 , eps99);

   return ok;
}