File: traceless_higher_order.cpp

package info (click to toggle)
adolc 2.7.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,496 kB
  • sloc: cpp: 40,481; ansic: 19,390; sh: 4,277; makefile: 551; python: 486
file content (131 lines) | stat: -rw-r--r-- 3,148 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*----------------------------------------------------------------------------
 ADOL-C -- Automatic Differentiation by Overloading in C++
 File:     traceless_higher_order.cpp
 Revision: $Id: traceless_higher_order.cpp ??? $
 Contents: computation of partial derivatives
           traceless forward mode for higher order derivatives
           described in the manual

 Copyright (c) Andrea Walther, Andreas Kowarz, Benjamin Jurgelucks
  
 This file is part of ADOL-C. This software is provided as open source.
 Any use, reproduction, or distribution of the software constitutes 
 recipient's acceptance of the terms of the accompanying license file.
 
---------------------------------------------------------------------------*/


/****************************************************************************/
/*                                                                 INCLUDES */

#define ADOLC_TRACELESS_HIGHER_ORDER
//#include <adolc/adouble.h>
//typedef adhotl::adouble adouble;
#include <iostream>
#include <complex>


#include <adolc/adtl_hov.h>
typedef adtl_hov::adouble adouble;




using namespace std;

adouble poly (adouble* z){
  adouble y;
  y =exp(z[0])*log(pow(z[0],2.0));
  return y;
}

double poly (double* z){
  double y;
  y = exp(z[0])*log(pow(z[0],2.0));
  return y;
}

adouble higher_order(adouble* z)
{
  double a=1.0;
  adouble y;
  y=z[0]*exp(a*z[0]);
  return y;
}

double higher_order(double* z)
{
  double a=1.0;
  double y; 
  y=z[0]*exp(a*z[0]);
  return y;
}

double higher_order_analytic(double* z, int n)
{
  double a=1.0;
  double y; 
  y=exp(a*z[0])*(n*pow(a,n-1)+pow(a,n)*z[0]);
  return y;
}

int factorial(int n)
{
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}


int main(){
  int d=12, n=3;
  cout << "Degree of derivative (precise up to degree 12):  ";
  cin >> d;
  cout << endl;
  
  double one[1];
  one[0]=1;
  
  //number of Taylor coefficients y_i
  adtl_hov::setDegree(d);
  
  adouble* z = new adouble[n];
  adouble y;
  double* dz = new double[n];
  double dy;
     
  //initialization of independent variables
  for(int i=0; i<n; i++)
  {
    z[i]=2.0;
    dz[i]=2.0;
  }
      
  //set Taylor coefficients x_i
  z[0].setOneADValue(0,one);
  
  double* ret;
  if(d==2)  // analytical derivative only for d=2 available
  {
      //function evaluation for case 1
      y=poly(z);
      dy= poly(dz);
      
      ret=y.getOneADValue(d-1);
      cout << "factorial(d):  " << factorial(d) << endl;
      cout << "ADOLC:    Value of primal: "<< y.getValue() <<"    Value of derivative: " <<  factorial(d)*ret[0] << endl;
      cout << "DOUBLE:   Value of primal: "<< dy <<"    Value of derivative: " <<  (exp(dz[0]) *(-2 + 4*dz[0] + dz[0]*dz[0]*log(dz[0]*dz[0])))/ (dz[0]*dz[0])  << endl;
  }
 
   //function evaluation for case 2
   y=higher_order(z);
   dy=higher_order(dz);
  
   ret=y.getOneADValue(d-1);

  cout << "ADOLC:    Value of primal: "<< y.getValue() <<"    Value of derivative: " << factorial(d)*ret[0] << endl;
  cout << "DOUBLE:   Value of primal: "<< dy <<"    Value of derivative: " <<  higher_order_analytic(dz,d)  << endl;
 
 
  delete []dz;
  delete []z;
}