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
|
/*----------------------------------------------------------------------------
ADOL-C -- Automatic Differentiation by Overloading in C++
File: inversexam.cpp
Revision: $Id$
Contents: Test driver 'inverse_tensor_eval(..)' allows to
compute higher order derivatives of inverse
functions
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,n,d,p,dim;
/*--------------------------------------------------------------------------*/
cout << "INVERSEXAM (ADOL-C Example)\n\n"; /* inputs */
cout << " Number of independents = ?\n ";
cin >> n;
// number of dependents = number of independents !!
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[n];
double** S = new double*[n];
double* test = new double[n];
double** tensoren;
adouble* x = new adouble[n];
adouble* y = new adouble[n];
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;
}
for (i=0; i<d; i++)
multi[i] = 0;
/*--------------------------------------------------------------------------*/
trace_on(1); /* tracing the function */
for (i=0; i<n; i++) {
x[i] <<= xp[i];
y[i] = (i+1)*x[i];
}
y[0] += sqrt(x[0]);
for (i=0; i<n; i++)
y[i] >>= yp[i] ;
trace_off();
/*--------------------------------------------------------------------------*/
d = d-1; /* 1. inverse_tensor_eval */
dim = binomi(p+d,d);
tensoren = myalloc2(n,dim);
cout <<"TASK 1:\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
inverse_tensor_eval(1,n,d,p,xp,tensoren,S);
for (i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,n,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<n; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
/*--------------------------------------------------------------------------*/
d = d+1; /* 2. inverse_tensor_eval */
dim = binomi(p+d,d);
cout <<"TASK 2:\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensoren = myalloc2(n,dim);
inverse_tensor_eval(1,n,d,p,xp,tensoren,S);
for (i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,n,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<n; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
/*--------------------------------------------------------------------------*/
xp[0] = 2*xp[0]; /* 3. inverse_tensor_eval */
cout <<"TASK 3:\n";
cout <<" NEW independend values !!!\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
inverse_tensor_eval(1,n,d,p,xp,tensoren,S);
for (i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,n,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<n; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
/*--------------------------------------------------------------------------*/
d = d-1; /* 4. inverse_tensor_eval */
dim = binomi(p+d,d);
cout <<"TASK 4:\n";
cout <<" d = "<<d<<", dim = "<<dim<<"\n";
tensoren = myalloc2(n,dim);
inverse_tensor_eval(1,n,d,p,xp,tensoren,S);
for (i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,n,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<n; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
/*--------------------------------------------------------------------------*/
d = d+1; /* 5. inverse_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(n,dim);
inverse_tensor_eval(1,n,d,p,xp,tensoren,S);
for (i=0; i<p; i++) {
multi[0] = i+1;
tensor_value(d,n,test,tensoren,multi);
cout << i+1 << ": ";
for (j=0; j<n; j++)
cout << " " << test[j] << " ";
cout << "\n";
}
myfree2(tensoren);
return 1;
}
/****************************************************************************/
/* THAT'S ALL */
|