source: trunk/ADOL-C/examples/additional_examples/timing/sfunc_power.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.7 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     sfunc_power.cpp
4 Revision: $Id: sfunc_power.cpp 42 2009-07-15 18:37:17Z awalther $
5 Contents: function module containing the power example
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_POWER_C_
25
26/****************************************************************************/
27/*                                                                 INCLUDES */
28#include <adolc.h>
29
30#include <cstdlib>
31#include <time.h>
32
33
34/****************************************************************************/
35/*                                                         GLOBAL VARIABLES */
36
37/*--------------------------------------------------------------------------*/
38/*                                                        Control file name */
39const char* controlFileName = "powexam.ctrl";
40
41/*--------------------------------------------------------------------------*/
42/*                                                               Dimensions */
43int indepDim;
44
45/*--------------------------------------------------------------------------*/
46/*                                       Other problem dependent parameters */
47int exponent;
48//static unsigned short int dx[3]; /* variable needed by erand48(.) */
49
50
51/****************************************************************************/
52/*                                                  INIT PROBLEM PARAMETERS */
53void initProblemParameters( void ) {
54    fprintf(stdout,"COMPUTATION OF n-th POWER (ADOL-C Example)\n\n");
55    indepDim = 1;
56
57    fprintf(stdout,"    n = ? ");
58    fscanf(stdin,"%d",&exponent);
59    fprintf(stdout,"\n");
60
61    /* Init erand48(); */
62    struct tm s;
63    time_t t;
64    time(&t);
65    s=*localtime(&t);
66    srand(s.tm_sec*s.tm_min);
67    /*  dx[0]=rand();
68      dx[1]=rand();
69      dx[2]=rand();*/
70}
71
72
73/****************************************************************************/
74/*                                                        INITIALIZE INDEPs */
75void initIndependents( double* indeps ) {
76    indeps[0] = (double)rand();
77}
78
79
80/****************************************************************************/
81/*                                                 ORIGINAL SCALAR FUNCTION */
82
83/*--------------------------------------------------------------------------*/
84/*                                       The recursive determinant function */
85double power( double x, int n ) {
86    double z = 1;
87    if (n > 0) {
88        int nh =n/2;
89        z = power(x,nh);
90        z *= z;
91        if (2*nh != n)
92            z *= x;
93        return z;
94    } else
95        if (n == 0)
96            return z;
97        else
98            return 1.0/power(x,-n);
99}
100
101/*--------------------------------------------------------------------------*/
102/*                                                   The interface function */
103double originalScalarFunction( double* indeps ) {
104    return power(indeps[0],exponent);
105}
106
107
108/****************************************************************************/
109/*                                                   TAPING SCALAR FUNCTION */
110
111/*--------------------------------------------------------------------------*/
112/*                                             The recursive power function */
113adouble activePower( adouble x, int n) {
114    adouble z = 1;
115    if (n > 0) {
116        int nh =n/2;
117        z = activePower(x,nh);
118        z *= z;
119        if (2*nh != n)
120            z *= x;
121        return z;
122    } else
123        if (n == 0)
124            return z;
125        else
126            return 1.0/activePower(x,-n);
127}
128
129/*--------------------------------------------------------------------------*/
130/*                                                   The interface function */
131double tapingScalarFunction( int tag, double* indeps ) {
132    trace_on(tag);
133    adouble activeIndep;
134    activeIndep <<= *indeps;
135    adouble ares = activePower(activeIndep,exponent);
136    double res = 0;
137    ares >>= res;
138    trace_off();
139    return res;
140}
141
142#undef _SFUNC_POWER_C_
143
144
145
146
147
Note: See TracBrowser for help on using the repository browser.