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 (50 lines) | stat: -rw-r--r-- 991 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
// 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
// ----------------------------------------------------------------------------

/*
{xrst_begin poly.cpp}

Polynomial Evaluation: Example and Test
#######################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end poly.cpp}
*/
// BEGIN C++

# include <cppad/cppad.hpp>
# include <cmath>

bool poly(void)
{  bool ok = true;

   // degree of the polynomial
   size_t deg = 3;

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

   // evaluate this polynomial
   size_t k = 0;
   double z = 2.;
   double p = CppAD::Poly(k, a, z);
   ok      &= (p == 1. + z + z*z + z*z*z);

   // evaluate derivative
   k = 1;
   p = CppAD::Poly(k, a, z);
   ok &= (p == 1 + 2.*z + 3.*z*z);

   return ok;
}

// END C++