source: trunk/ADOL-C/examples/additional_examples/timing/sfunc_experimental.cpp @ 42

Last change on this file since 42 was 42, checked in by awalther, 10 years ago

set svn keywords property

  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     sfunc_experimental.cpp
4 Revision: $Id: sfunc_experimental.cpp 42 2009-07-15 18:37:17Z awalther $
5 Contents: function module containing an experimental function
6 
7   Each << function module >> contains:
8         
9     (1) const char* const controlFileName
10     (2) int indepDim;
11     (3) void initProblemParameters( void )
12     (4) void initIndependents( double* indeps )
13     (5) double originalScalarFunction( double* indeps )
14     (6) double tapingScalarFunction( int tag, double* indeps )   
15 
16 Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
17               Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
18 
19 This file is part of ADOL-C. This software is provided as open source.
20 Any use, reproduction, or distribution of the software constitutes
21 recipient's acceptance of the terms of the accompanying license file.
22 
23---------------------------------------------------------------------------*/
24#define _SFUNC_EXPERIMENTAL_C_
25
26
27/****************************************************************************/
28/*                                                                 INCLUDES */
29#include <adolc.h>
30
31#include <time.h>
32#include <cmath>
33
34
35/****************************************************************************/
36/*                                                         GLOBAL VARIABLES */
37
38/*--------------------------------------------------------------------------*/
39/*                                                        Control file name */
40const char* controlFileName = "experimental.ctrl";
41
42/*--------------------------------------------------------------------------*/
43/*                                                               Dimensions */
44int indepDim;
45
46/*--------------------------------------------------------------------------*/
47/*                                       Other problem dependent parameters */
48//#define CODE sqrt(indeps[i])
49#define CODE sin(indeps[i])
50//#define CODE indeps[i]*indeps[i]
51
52
53/****************************************************************************/
54/*                                                  INIT PROBLEM PARAMETERS */
55void initProblemParameters( void ) {
56    fprintf(stdout,"EXPERIMENTAL EXAMPLE (ADOL-C Example)\n\n");
57    if (indepDim <= 0) {
58        fprintf(stdout,"    number of independent variables = ? ");
59        fscanf(stdin,"%d",&indepDim);
60        fprintf(stdout,"\n");
61    }
62}
63
64
65/****************************************************************************/
66/*                                                        INITIALIZE INDEPs */
67void initIndependents( double* indeps ) {
68    int i;
69    for (i=0; i<indepDim; i++)
70        indeps[i] = (i+1.0)/(2.0+i);
71}
72
73
74/****************************************************************************/
75/*                                                 ORIGINAL SCALAR FUNCTION */
76
77/*--------------------------------------------------------------------------*/
78/*                                                    experimental function */
79double experimental( int dim, double* indeps ) {
80    int i;
81    double y = 1.0;
82    for (i=0; i<dim; i++)
83        y += CODE;
84    return y;
85}
86
87/*--------------------------------------------------------------------------*/
88/*                                                   The interface function */
89double originalScalarFunction( double* indeps ) {
90    return experimental(indepDim, indeps);
91}
92
93
94/****************************************************************************/
95/*                                                   TAPING SCALAR FUNCTION */
96
97/*--------------------------------------------------------------------------*/
98/*                                             active experimental function */
99adouble activeExperimental( int dim, adouble* indeps ) {
100    int i;
101    adouble y = 1.0;
102    for (i=0; i<dim; i++)
103        y += CODE;
104    return y;
105}
106
107/*--------------------------------------------------------------------------*/
108/*                                                   The interface function */
109double tapingScalarFunction( int tag, double* indeps ) {
110    int i;
111    trace_on(tag);
112    adouble* activeIndeps = new adouble[indepDim];
113    adouble* aIP = activeIndeps;
114    double*  iP  = indeps;
115    for (i=0; i<indepDim; i++)
116        *aIP++ <<= *iP++;
117    adouble ares = activeExperimental(indepDim, activeIndeps);
118    double res = 0;
119    ares >>= res;
120    trace_off();
121    return res;
122}
123
124#undef _SFUNC_EXPERIMENTAL_C_
125
126
127
128
129
130
131
Note: See TracBrowser for help on using the repository browser.