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 183 184 185 186 187 188 189
|
/*----------------------------------------------------------------------------
ADOL-C -- Automatic Differentiation by Overloading in C++
File: sfunc_determinant.cpp
Revision: $Id$
Contents: function module containing the determinant example
Each << function module >> contains:
(1) const char* const controlFileName
(2) int indepDim;
(3) void initProblemParameters( void )
(4) void initIndependents( double* indeps )
(5) double originalScalarFunction( double* indeps )
(6) double tapingScalarFunction( int tag, double* indeps )
Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
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.
---------------------------------------------------------------------------*/
#define _SFUNC_DETERMINANT_C_
/****************************************************************************/
/* INCLUDES */
#include <adolc/adolc.h>
/****************************************************************************/
/* GLOBAL VARIABLES */
/*--------------------------------------------------------------------------*/
/* Control file name */
const char* controlFileName = "detexam.ctrl";
/*--------------------------------------------------------------------------*/
/* Dimensions */
int indepDim;
/*--------------------------------------------------------------------------*/
/* Other problem dependent parameters */
int matrixDim;
int mRec;
/****************************************************************************/
/* INIT PROBLEM PARAMETERS */
void initProblemParameters( void ) {
fprintf(stdout,"COMPUTATION OF DETERMINANTS Type 2 (ADOL-C Example)\n\n");
if (indepDim > 0)
matrixDim = indepDim;
else {
fprintf(stdout," order of matrix = ? ");
fscanf(stdin,"%d",&matrixDim);
fprintf(stdout,"\n");
}
indepDim = matrixDim * matrixDim;
}
/****************************************************************************/
/* INITIALIZE INDEPs */
void initIndependents( double* indeps ) {
int i, j;
double* iP = indeps;
mRec = 1;
for (i=0; i<matrixDim; i++) {
mRec *= 2;
for (j=0; j<matrixDim; j++)
*iP++ = j/(1.0+i);
indeps[i*matrixDim+i] += 1.0;
}
}
/****************************************************************************/
/* ORIGINAL SCALAR FUNCTION */
/*--------------------------------------------------------------------------*/
/* The recursive determinant function */
double det( int k, int m, double* indeps ) {
int i;
if (m == 0)
return 1.0;
else {
double* pt = indeps+((k-1)*matrixDim);
double t = 0;
int p = 1;
int p1, s;
if (k%2)
s = 1;
else
s = -1;
for (i=0; i<matrixDim; i++) {
p1 = 2*p;
if (m%p1 >= p) {
if (m == p) {
if (s > 0)
t += *pt;
else
t -= *pt;
} else {
if (s > 0)
t += (*pt)*det(k-1, m-p, indeps);
else
t -= (*pt)*det(k-1, m-p, indeps);
}
s = -s;
}
++pt;
p = p1;
}
return t;
}
}
/*--------------------------------------------------------------------------*/
/* The interface function */
double originalScalarFunction( double* indeps ) {
return det(matrixDim, mRec-1, indeps);
}
/****************************************************************************/
/* TAPING SCALAR FUNCTION */
/*--------------------------------------------------------------------------*/
/* The recursive determinant function */
adouble activeDet( int k, int m, adouble* indeps ) {
int i;
if (m == 0)
return 1.0;
else {
adouble* pt = indeps + ((k-1)*matrixDim);
adouble t = 0;
int p = 1;
int p1, s;
if (k%2)
s = 1;
else
s = -1;
for (i=0; i<matrixDim; i++) {
p1 = 2*p;
if (m%p1 >= p) {
if (m == p) {
if (s > 0)
t += *pt;
else
t -= *pt;
} else {
if (s > 0)
t += (*pt)*activeDet(k-1, m-p, indeps);
else
t -= (*pt)*activeDet(k-1, m-p, indeps);
}
s = -s;
}
++pt;
p = p1;
}
return t;
}
}
/*--------------------------------------------------------------------------*/
/* The interface function */
double tapingScalarFunction( int tag, double* indeps ) {
int i;
trace_on(tag);
adouble* activeIndeps = new adouble[indepDim];
adouble* aIP = activeIndeps;
double* iP = indeps;
for (i=0; i<indepDim; i++)
*aIP++ <<= *iP++;
adouble ares = activeDet(matrixDim, mRec-1, activeIndeps);
double res = 0;
ares >>= res;
trace_off();
return res;
}
#undef _SFUNC_DETERMINANT_C_
|