source: trunk/test_more/poly.cpp @ 1581

Last change on this file since 1581 was 1370, checked in by bradbell, 11 years ago

trunk: Fix svn_add_id.sh and use it set Id property for some missed files.

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/* $Id: poly.cpp 1370 2009-05-31 05:31:50Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Common Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
11-------------------------------------------------------------------------- */
12
13/*
14// Old GetStarted example now used just for validation testing
15*/
16// BEGIN PROGRAM
17
18// directory where cppad/cppad.hpp is stored must be searched by compiler
19# include <cppad/cppad.hpp>
20
21bool Poly(void)
22{       bool ok = true;
23
24        // make CppAD routines visible without CppAD:: infront of names
25        using namespace CppAD;
26
27        // degree of the polynomial that we will differentiate
28        size_t deg = 4;
29
30        // vector that will hold polynomial coefficients for p(z)
31        CPPAD_TEST_VECTOR< AD<double> > A(deg + 1);  // AD<double> elements
32        CPPAD_TEST_VECTOR<double>       a(deg + 1);  //    double  elements
33
34        // set the polynomial coefficients
35        A[0] = 1.;
36        size_t k;
37        for(k = 1; k <= deg; k++)
38                A[k] = a[k] = 1.;
39
40        // independent variables
41        CPPAD_TEST_VECTOR< AD<double> > Z(1); // one independent variable
42        Z[0]     = 3.;                        // value of independent variable
43        Independent(Z);                       // declare independent variable
44
45        // dependent variables
46        CPPAD_TEST_VECTOR< AD<double> > P(1); // one dependent variable
47        P[0]     = Poly(0, A, Z[0]);    // value of polynomial at Z[0]
48
49        // define f : Z -> P as a function mapping independent to dependent
50        ADFun<double> f(Z, P);          // ADFun corresponding to polynomial
51
52        // compute derivative of polynomial
53        CPPAD_TEST_VECTOR<double> z(1);  // vector length f.Domain()
54        CPPAD_TEST_VECTOR<double> J(1);  // vector length f.Range * f.Domain()
55        z[0] = 3.;                 // point at which to compute derivative
56        J    = f.Jacobian(z);      // value of derivative
57
58        // compare with derivative as computed by Poly
59        ok  &= (Poly(1, a, z[0]) == J[0]);
60
61        return ok;
62}
63
64// END PROGRAM
Note: See TracBrowser for help on using the repository browser.