source: trunk/test_more/erf.cpp @ 2354

Last change on this file since 2354 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: 1.7 KB
Line 
1/* $Id: erf.cpp 1370 2009-05-31 05:31:50Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-08 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/*
14Old example now just used for validation testing
15*/
16
17# include <cppad/cppad.hpp>
18
19bool Erf(void)
20{       bool ok = true;
21        using namespace CppAD;
22        using CppAD::atan;
23        using CppAD::exp;
24        using CppAD::sqrt;
25
26        // Construct function object corresponding to erf
27        CPPAD_TEST_VECTOR< AD<double> > X(1);
28        CPPAD_TEST_VECTOR< AD<double> > Y(1);
29        X[0] = 0.;
30        Independent(X);
31        Y[0] = erf(X[0]);
32        ADFun<double> Erf(X, Y);
33
34        // vectors to use with function object
35        CPPAD_TEST_VECTOR<double> x(1);
36        CPPAD_TEST_VECTOR<double> y(1);
37        CPPAD_TEST_VECTOR<double> dx(1);
38        CPPAD_TEST_VECTOR<double> dy(1);
39
40        // check value at zero
41        x[0]  = 0.;
42        y = Erf.Forward(0, x); 
43        ok &= NearEqual(0., y[0], 4e-4, 0.);
44
45        // check the derivative of error function
46        dx[0]         = 1.;
47        double pi     = 4. * atan(1.);
48        double factor = 2. / sqrt( pi );
49        int i;
50        for(i = -10; i <= 10; i++)
51        {       x[0] = i / 4.;
52                y    = Erf.Forward(0, x);       
53
54                // check derivative
55                double derf = factor * exp( - x[0] * x[0] );
56                dy          = Erf.Forward(1, dx);
57                ok         &= NearEqual(derf, dy[0], 0., 2e-3);
58
59                // test using erf with AD< AD<double> >
60                AD< AD<double> > X0 = x[0];
61                AD< AD<double> > Y0 = erf(X0);
62
63                ok &= ( y[0] == Value( Value(Y0) ) );
64        }
65        return ok;
66}
Note: See TracBrowser for help on using the repository browser.