source: trunk/test_more/atan2.cpp @ 3779

Last change on this file since 3779 was 3779, checked in by bradbell, 4 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: cf5ca238670f67655fbbf3e23727437f7ed7b182
end hash code: 5661e2a629cceb77e7b703225bc1f3f8cf422129

commit 5661e2a629cceb77e7b703225bc1f3f8cf422129
Author: Brad Bell <bradbell@…>
Date: Fri Jan 1 04:23:41 2016 -0700

push_git2svn.py: Include changes in copyright date when checking for difference.

commit f0e9d149dc00ccf410a268389e3dcfd95499b863
Author: Brad Bell <bradbell@…>
Date: Fri Jan 1 03:57:09 2016 -0700

Advance copyright year to 2016.

commit 01d25679b7baddca878129635b0758f4ebb263fb
Author: Brad Bell <bradbell@…>
Date: Fri Jan 1 03:54:41 2016 -0700

download.omh: improve the no documentation download instructions.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: atan2.cpp 3779 2016-01-01 11:26:11Z bradbell $
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 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
13/*
14Old example and test now just used for validation testing.
15*/
16
17# include <cppad/cppad.hpp>
18# include <cmath>
19
20namespace { // begin empty namespace
21
22bool ad_ad(void)
23{       bool ok = true;
24        using CppAD::AD;
25
26        AD< AD<double> > x(2.), y(2.);
27        AD< AD<double> > z = atan2(y, x);
28        CppAD::NearEqual( Value( Value(z) ), atan(1.), 1e-10, 1e-10 );
29
30        return ok;
31}
32
33bool general(void)
34{       bool ok = true;
35
36        using CppAD::atan;
37        using CppAD::sin;
38        using CppAD::cos;
39        using namespace CppAD;
40
41        // independent variable vector
42        CPPAD_TESTVECTOR(AD<double>) U(1);
43        U[0]     = 1.;
44        Independent(U);
45
46        // a temporary values
47        AD<double> x = cos(U[0]);
48        AD<double> y = sin(U[0]);
49
50        // dependent variable vector
51        CPPAD_TESTVECTOR(AD<double>) Z(1);
52        Z[0] = atan2(y, x);
53
54        // create f: U -> Z and vectors used for derivative calculations
55        ADFun<double> f(U, Z);
56        CPPAD_TESTVECTOR(double) v(1);
57        CPPAD_TESTVECTOR(double) w(1);
58
59        // check original value (u in first quadrant)
60        ok &= NearEqual(U[0] , Z[0],  1e-10 , 1e-10);
61
62        // check case where u is in second quadrant
63        v[0] = 3.;
64        w    = f.Forward(0, v);
65        ok  &= NearEqual(w[0] , v[0],  1e-10 , 1e-10);
66
67        // check case where u is in third quadrant
68        v[0] = -3.;
69        w    = f.Forward(0, v);
70        ok  &= NearEqual(w[0] , v[0],  1e-10 , 1e-10);
71
72        // check case where u is in fourth quadrant
73        v[0] = -1.;
74        w    = f.Forward(0, v);
75        ok  &= NearEqual(w[0] , v[0],  1e-10 , 1e-10);
76
77        // forward computation of partials w.r.t. u
78        size_t j;
79        size_t p     = 5;
80        double jfac  = 1.;
81        double value = 1.;
82        v[0]         = 1.;
83        for(j = 1; j < p; j++)
84        {       jfac *= j;
85                w     = f.Forward(j, v);
86                ok &= NearEqual(jfac*w[0], value, 1e-10 , 1e-10); // d^jz/du^j
87                v[0]  = 0.;
88                value = 0.;
89        }
90
91        // reverse computation of partials of Taylor coefficients
92        CPPAD_TESTVECTOR(double) r(p);
93        w[0]  = 1.;
94        r     = f.Reverse(p, w);
95        jfac  = 1.;
96        value = 1.;
97        for(j = 0; j < p; j++)
98        {       ok &= NearEqual(jfac*r[j], value, 1e-10 , 1e-10); // d^jz/du^j
99                jfac *= (j + 1);
100                value = 0.;
101        }
102
103        return ok;
104}
105
106} // end empty namespace
107
108bool atan2(void)
109{       bool ok = true;
110        ok     &= ad_ad();
111        ok     &= general();
112
113        return ok;
114}
Note: See TracBrowser for help on using the repository browser.