source: trunk/ADOL-C/examples/tapeless_vector.cpp @ 160

Last change on this file since 160 was 42, checked in by awalther, 10 years ago

set svn keywords property

  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     tapeless_vector.cpp
4 Revision: $Id: tapeless_vector.cpp 42 2009-07-15 18:37:17Z kulshres $
5 Contents: computation of coordinate transform,
6           vector tapeless forward mode
7           described in the manual
8
9 Copyright (c) Andrea Walther, Andreas Kowarz
10 
11 This file is part of ADOL-C. This software is provided as open source.
12 Any use, reproduction, or distribution of the software constitutes
13 recipient's acceptance of the terms of the accompanying license file.
14 
15---------------------------------------------------------------------------*/
16
17/****************************************************************************/
18/*                                                                 INCLUDES */
19
20#include <iostream>
21using namespace std;
22
23#define ADOLC_TAPELESS
24#define NUMBER_DIRECTIONS 3
25#include <adouble.h>
26typedef adtl::adouble adouble;
27
28ADOLC_TAPELESS_UNIQUE_INTERNALS
29
30int main(int argc, char *argv[]) {
31    adouble x[3], y[3];
32
33    for (int i=0; i<3;++i)          // Initialize x_i
34    {
35        x[i] = i + 1.0;
36        for (int j=0; j<3;++j)
37            if (i==j)
38                x[i].setADValue(j,1);
39    }
40
41    cout << endl << "Transform from Cartesian to spherical polar coordinates" << endl << endl;
42
43    y[0] = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
44    y[1] = atan(sqrt(x[0]*x[0]+x[1]*x[1])/x[2]);
45    y[2] = atan(x[1]/x[0]);
46
47    cout << "cartesian coordinates: " << endl;
48    cout << "x[0] = " << x[0].getValue() << "  x[1] = " << x[1].getValue()
49    << "  x[2] = " << x[2].getValue() << endl << endl;
50    cout << "cpherical polar coordinates: " << endl;
51    cout << "y[0] = " << y[0].getValue() << "  y[1] = " << y[1].getValue()
52    << "  y[2] = " << y[2].getValue() << endl <<endl;
53
54    // "use" the derivative
55    cout << "derivative:" << endl;
56    for (int i=0; i<3;++i) {
57        for (int j=0; j<3;++j)
58            cout << y[i].getADValue(j) << "  ";
59        cout << endl;
60    }
61    cout << endl;
62
63    return 0;
64}
65
Note: See TracBrowser for help on using the repository browser.