File: poly.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 (57 lines) | stat: -rw-r--r-- 1,923 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
// 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 GetStarted example now used just for validation testing
*/
// BEGIN C++

// directory where cppad/cppad.hpp is stored must be searched by compiler
# include <cppad/cppad.hpp>

bool Poly(void)
{  bool ok = true;

   // make CppAD routines visible without CppAD:: infront of names
   using namespace CppAD;

   // degree of the polynomial that we will differentiate
   size_t deg = 4;

   // vector that will hold polynomial coefficients for p(z)
   CPPAD_TESTVECTOR(AD<double>) A(deg + 1);  // AD<double> elements
   CPPAD_TESTVECTOR(double)       a(deg + 1);  //    double  elements

   // set the polynomial coefficients
   A[0] = 1.;
   size_t k;
   for(k = 1; k <= deg; k++)
      A[k] = a[k] = 1.;

   // independent variables
   CPPAD_TESTVECTOR(AD<double>) Z(1); // one independent variable
   Z[0]     = 3.;                        // value of independent variable
   Independent(Z);                       // declare independent variable

   // dependent variables
   CPPAD_TESTVECTOR(AD<double>) P(1); // one dependent variable
   P[0]     = Poly(0, A, Z[0]);    // value of polynomial at Z[0]

   // define f : Z -> P as a function mapping independent to dependent
   ADFun<double> f(Z, P);          // ADFun corresponding to polynomial

   // compute derivative of polynomial
   CPPAD_TESTVECTOR(double) z(1);  // vector length f.Domain()
   CPPAD_TESTVECTOR(double) J(1);  // vector length f.Range * f.Domain()
   z[0] = 3.;                 // point at which to compute derivative
   J    = f.Jacobian(z);      // value of derivative

   // compare with derivative as computed by Poly
   ok  &= (Poly(1, a, z[0]) == J[0]);

   return ok;
}

// END C++