source: trunk/ADOL-C/examples/additional_examples/pow/powexam.cpp @ 171

Last change on this file since 171 was 171, checked in by kulshres, 9 years ago

Squashed merge branch 'master' of 'gitclone' into svn

  • 'master' of 'gitclone': (84 commits) adjust example makefiles and include paths get rid of the symlink in the src subdirectory

details of the commits:
commit c9e4bc332d2363f737fc2e8a8fcfc2e43ddb9d15
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Oct 4 15:43:47 2010 +0200

adjust example makefiles and include paths

include paths in example sources were wrong for some time now
simplify makefile rules too, there is really no need for checking SPARSE
adjust include paths in makefiles.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit e6e1963e41e097fd5b4a79cd1611c12f6868dc94
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Oct 4 15:41:25 2010 +0200

get rid of the symlink in the src subdirectory

windows doesn't like symlinks and make infinite depth directories
we now create a symlink for build in the directory parallel to src
adjust all makefiles.am accordingly for build

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     powexam.cpp
4 Revision: $Id: powexam.cpp 171 2010-10-04 13:57:19Z kulshres $
5 Contents: example for computation of n-th power
6
7 Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
8               Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
9 
10 This file is part of ADOL-C. This software is provided as open source.
11 Any use, reproduction, or distribution of the software constitutes
12 recipient's acceptance of the terms of the accompanying license file.
13 
14---------------------------------------------------------------------------*/
15
16/****************************************************************************/
17/*                                                                 INCLUDES */
18
19#include <adolc/adolc.h>               // use of ALL ADOL-C interfaces
20
21#include <iostream>
22using namespace std;
23
24#include <math.h>
25
26/****************************************************************************/
27/*                                                             MAIN PROGRAM */
28int main() {
29    int i,tag = 1;
30    int n;
31    double e;
32
33    cout << "COMPUTATION OF x^e\n\n";
34    cout << "e=? \n";    // input the desired degree
35    cin >> e;
36
37    n = (int) e;
38    if (n < 0)
39        n = -n;
40    // allocations and initializations
41    double** X;
42    double** Y;
43    X = myalloc2(1,n+4);
44    Y = myalloc2(1,n+4);
45    cout << "value of x=? \n";
46    cin >> X[0][0];                  // function value = 0. coefficient
47    X[0][1] = 1.0;                   // first derivative = 1. coefficient
48    X[0][2] = 0.0;                   // second derivative = 2. coefficient
49    for(i=1; i<n+2; i++)
50        X[0][i+2] = 0;                 // further coefficients
51    double** Z;                      // used for checking consistency
52    Z = myalloc2(1,n+2);             // between forward and reverse
53
54    adouble y,x;                     // declare active variables
55    // beginning of active section
56    trace_on(tag);                   // tag = 1 and keep = 0
57    x <<= X[0][0];                 // only one independent var
58    y = 1;
59    for(i=0;i<n;i++)
60        y *= x;                        // actual function call
61    y >>= Y[0][0];                 // only one dependent adouble
62    trace_off();                     // no global adouble has died
63    // end of active section */
64    double u[1];                     // weighting vector
65    u[0]=1;                          // for reverse call
66    for(i=0; i<n+2; i++)             // note that keep = i+1 in call
67    { forward(tag,1,1,i,i+1,X,Y);    // evaluate the i-the derivative
68        cout << "Result: " << Y[0][0] << "\n";
69        if (i==0)
70            cout << i << " " << Y[0][i] << " - " << y.value() << " = " << Y[0][i]-y.value()
71            << " (should be 0)\n";
72        else {
73            Z[0][i] = Z[0][i-1]/i;       // scale derivative to Taylorcoeff.
74            cout << i << " " << Y[0][i] << " - " << Z[0][i] << " = " << Y[0][i]-Z[0][i]
75            << " (should be 0)\n";
76        }
77        reverse(tag,1,1,i,u,Z);        // evaluate the (i+1)-st deriv.
78    } // end for
79
80    cout << "\n\n";
81    trace_on(tag);                   // tag = 1 and keep = 0
82    x <<= X[0][0];                 // only one independent var
83    y = pow(x,e);                  // actual function call
84    y >>= Y[0][0];                 // only one dependent adouble
85    trace_off();                     // no global adouble has died
86    // end of active section */
87    u[0]=1;                          // for reverse call
88    for(i=0; i<n+2; i++)             // note that keep = i+1 in call
89    { forward(tag,1,1,i,i+1,X,Y);    // evaluate the i-the derivative
90        cout << "Result: " << Y[0][0] << "\n";
91        if (i==0)
92            cout << i << " " << Y[0][i] << " - " << y.value() << " = " << Y[0][i]-y.value()
93            << " (should be 0)\n";
94        else {
95            Z[0][i] = Z[0][i-1]/i;       // scale derivative to Taylorcoeff.
96            cout << i << " " << Y[0][i] << " - " << Z[0][i] << " = " << Y[0][i]-Z[0][i]
97            << " (should be 0)\n\n";
98        }
99        reverse(tag,1,1,i,u,Z);        // evaluate the (i+1)-st deriv.
100    } // end for
101
102    return 1;
103} // end main
104
Note: See TracBrowser for help on using the repository browser.