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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*----------------------------------------------------------------------------
ADOL-C -- Automatic Differentiation by Overloading in C++
File: taylorexam.cpp
Revision: $Id$
Contents: Test driver 'tensor_eval(..)' to compute
higher order derivatives
Copyright (c) Andrea Walther, Andreas Griewank
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 */
#include <adolc/adolc.h>
#include <cstdlib>
#include <iostream>
using namespace std;
/****************************************************************************/
/* MAIN */
int main() {
int i,j,m,n,d,p,dim;
/*--------------------------------------------------------------------------*/
cout << "TAYLOREXAM (ADOL-C Example)\n\n"; /* inputs */
cout << " Number of indenpendents = ?\n ";
cin >> n;
cout << " Number of dependents = (<=n) ?\n ";
cin >> m;
cout << " Degree = ?\n ";
cin >> d;
cout << " Number of directions = ?\n ";
cin >> p;
/*--------------------------------------------------------------------------*/
int* multi = new int[d]; /* allocations and inits */
double* xp = new double[n];
double* yp = new double[m];
double** S = new double*[n];
double* test = new double[m];
double** tensoren;
adouble* x = new adouble[n];
adouble* y = new adouble[m];
for (i=0; i<d; i++)
multi[i] = 0;
for (i=0; i<n; i++) {
xp[i] = (i+1.0)/(2.0+i);
S[i] = new double[p];
for (j=0; j<p; j++)
S[i][j] = (i==j)?1.0:0.0;
}
/*--------------------------------------------------------------------------*/
trace_on(1); /* tracing the function */
// adouble* x = new adouble[n];
// adouble* y = new adouble[m];
y[0] = 1;
for (i=0; i<n; i++) {
x[i] <<= xp[i];
y[0] *= x[i];
}
for (i=1; i<m; i++)
y[i] = x[i];
for (i=0; i<m; i++)
y[i] >>= yp[i] ;
trace_off();
/*--------------------------------------------------------------------------*/
d = d-1; /* 1. tensor_eval */
dim = binomi(p+d,d);
cout <<"TASK 1:\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensoren = myalloc2(m,dim);
tensor_eval(1,m,n,d,p,xp,tensoren,S);
for (i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,m,test,tensoren,multi);
cout << i+1 << ": ";
for(j=0; j<m; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
/*--------------------------------------------------------------------------*/
d = d+1; /* 2. tensor_eval */
dim = binomi(p+d,d);
cout <<"TASK 2:\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensoren = myalloc2(m,dim);
tensor_eval(1,m,n,d,p,xp,tensoren,S);
for(i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,m,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<m; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
cout << "\n";
/*--------------------------------------------------------------------------*/
xp[0] = 2*xp[0]; /* 3. tensor_eval */
cout <<"TASK 3:\n";
cout <<" NEW independend values !!!\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensor_eval(1,m,n,d,p,xp,tensoren,S);
for(i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,m,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<m; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
/*--------------------------------------------------------------------------*/
d = d-1; /* 4. tensor_eval */
dim = binomi(p+d,d);
cout <<"TASK 4:\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensoren = myalloc2(m,dim);
tensor_eval(1,m,n,d,p,xp,tensoren,S);
for(i=0;i<p;i++) {
multi[0] = i+1;
tensor_value(d,m,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<m; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
/*--------------------------------------------------------------------------*/
d = d+1; /* 5. tensor_eval */
dim = binomi(p+d,d);
xp[0] = 0.5*xp[0];
cout <<"TASK 5:\n";
cout <<" OLD independend values !!!\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensoren = myalloc2(m,dim);
tensor_eval(1,m,n,d,p,xp,tensoren,S);
for(i=0;i<p;i++) {
multi[0] = i+1;
tensor_value(d,m,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<m; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
return 1;
}
/****************************************************************************/
/* THAT'S ALL */
|