1 | /*---------------------------------------------------------------------------- |
---|

2 | ADOL-C -- Automatic Differentiation by Overloading in C++ |
---|

3 | File: tapeless_scalar.cpp |
---|

4 | Revision: $Id: tapeless_scalar.cpp 171 2010-10-04 13:57:19Z stefan $ |
---|

5 | Contents: computation of coordinate transform, |
---|

6 | scalar 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 | /****************************************************************************/ |
---|

19 | /* INCLUDES */ |
---|

20 | |
---|

21 | #include <iostream> |
---|

22 | using namespace std; |
---|

23 | |
---|

24 | #define ADOLC_TAPELESS |
---|

25 | #include <adolc/adouble.h> |
---|

26 | typedef adtl::adouble adouble; |
---|

27 | |
---|

28 | |
---|

29 | int main(int argc, char *argv[]) { |
---|

30 | adouble x[3], y[3]; |
---|

31 | |
---|

32 | for (int i=0; i<3;++i) // Initialize x_i |
---|

33 | x[i] = i + 1.0; |
---|

34 | |
---|

35 | cout << endl << "Compute transform from Cartesian to spherical polar coordinates" << endl << endl; |
---|

36 | |
---|

37 | // derivative of y with respect to x0 |
---|

38 | x[0].setADValue(1); |
---|

39 | |
---|

40 | y[0] = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]); |
---|

41 | y[1] = atan(sqrt(x[0]*x[0]+x[1]*x[1])/x[2]); |
---|

42 | y[2] = atan(x[1]/x[0]); |
---|

43 | |
---|

44 | cout << "cartesian coordinates: " << endl; |
---|

45 | cout << "x[0] = " << x[0].getValue() << " x[1] = " << x[1].getValue() |
---|

46 | << " x[2] = " << x[2].getValue() << endl << endl; |
---|

47 | cout << "cpherical polar coordinates: " << endl; |
---|

48 | cout << "y[0] = " << y[0].getValue() << " y[1] = " << y[1].getValue() |
---|

49 | << " y[2] = " << y[2].getValue() << endl <<endl; |
---|

50 | |
---|

51 | // "use" the derivative |
---|

52 | cout << "derivative:" << endl; |
---|

53 | cout << "dy[0]/dx[0] = " << y[0].getADValue() << " dy[1]/dx[0] = " << y[1].getADValue() |
---|

54 | << " dy[2]/dx[0] = " << y[2].getADValue() << endl; |
---|

55 | |
---|

56 | return 0; |
---|

57 | } |
---|

58 | |
---|