source: trunk/test_more/fun_check.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by bradbell, 8 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/* $Id: fun_check.cpp 2506 2012-10-24 19:36:49Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Eclipse 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/*
13old example, now just used for testing deprecated syntax f.Dependent(y)
14*/
15
16// BEGIN C++
17# include <cppad/cppad.hpp>
18
19namespace { // -----------------------------------------------------------
20// define the template function object Fun<Type,Vector> in empty namespace
21template <class Type, class Vector>
22class Fun {
23private:
24        size_t n;
25public:
26        // function constructor
27        Fun(size_t n_) : n(n_)
28        { }
29        // function evaluator
30        Vector operator() (const Vector &x)
31        {       Vector y(n);
32                size_t i;
33                for(i = 0; i < n; i++)
34                {       // This operaiton sequence depends on x
35                        if( x[i] >= 0 ) 
36                                y[i] = exp(x[i]);
37                        else    y[i] = exp(-x[i]);
38                }
39                return y;
40        }       
41};
42// template function FunCheckCases<Vector, ADVector> in empty namespace
43template <class Vector, class ADVector>
44bool FunCheckCases(void)
45{       bool ok = true;
46        using CppAD::AD;
47        using CppAD::ADFun;
48        using CppAD::Independent;
49
50        // use the ADFun default constructor
51        ADFun<double> f;
52
53        // domain space vector
54        size_t n = 2;
55        ADVector X(n);
56        X[0] = -1.;
57        X[1] = 1.;
58
59        // declare independent variables and starting recording
60        Independent(X);
61
62        // create function object to use with AD<double>
63        Fun< AD<double>, ADVector > G(n);
64
65        // range space vector
66        size_t m = n;
67        ADVector Y(m);
68        Y = G(X);
69
70        // stop tape and store operation sequence in f : X -> Y
71        f.Dependent(Y);
72        ok &= (f.size_taylor() == 0);  // no implicit forward operation
73
74        // create function object to use with double
75        Fun<double, Vector> g(n);
76
77        // function values should agree when the independent variable
78        // values are the same as during recording
79        Vector x(n);
80        size_t j;
81        for(j = 0; j < n; j++)
82                x[j] = Value(X[j]);
83        double r = 1e-10; 
84        double a = 1e-10;
85        ok      &= FunCheck(f, g, x, a, r);
86
87        // function values should not agree when the independent variable
88        // values are the negative of values during recording
89        for(j = 0; j < n; j++)
90                x[j] = - Value(X[j]);
91        ok      &= ! FunCheck(f, g, x, a, r);
92
93        // re-tape to obtain the new AD of double operation sequence
94        for(j = 0; j < n; j++)
95                X[j] = x[j];
96        Independent(X);
97        Y = G(X);
98
99        // stop tape and store operation sequence in f : X -> Y
100        f.Dependent(Y);
101        ok &= (f.size_taylor() == 0);  // no implicit forward with this x
102
103        // function values should agree now
104        ok      &= FunCheck(f, g, x, a, r);
105
106        return ok;
107}
108} // End empty namespace
109# include <vector>
110# include <valarray>
111bool FunCheck(void)
112{       bool ok = true;
113        typedef CppAD::vector<double>                Vector1;
114        typedef CppAD::vector< CppAD::AD<double> > ADVector1;
115        typedef   std::vector<double>                Vector2;
116        typedef   std::vector< CppAD::AD<double> > ADVector2;
117        typedef std::valarray<double>                Vector3;
118        typedef std::valarray< CppAD::AD<double> > ADVector3;
119        // Run with Vector and ADVector equal to three different cases
120        // all of which are Simple Vectors with elements of type
121        // double and AD<double> respectively.
122        ok &= FunCheckCases< Vector1, ADVector2 >();
123        ok &= FunCheckCases< Vector2, ADVector3 >();
124        ok &= FunCheckCases< Vector3, ADVector1 >();
125        return ok;
126}
127// END C++
Note: See TracBrowser for help on using the repository browser.