File: fixpoint_exam.cpp

package info (click to toggle)
adolc 2.7.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,496 kB
  • sloc: cpp: 40,481; ansic: 19,390; sh: 4,277; makefile: 551; python: 486
file content (115 lines) | stat: -rw-r--r-- 2,393 bytes parent folder | download | duplicates (3)
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
/*----------------------------------------------------------------------------
 ADOL-C -- Automatic Differentiation by Overloading in C++
 File:     fixpoint_exam.cpp
 Revision: $Id$
 Contents: example for differentiation of fixpoint iterations

 Copyright (c) Andrea Walther
  
 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.
 
---------------------------------------------------------------------------*/
#include <math.h>
#include <adolc/adolc.h>

#define imax 100
#define imax_deriv 100

// fixpoint iteration

// double version 
int fixpoint_iter(double *x, double *u, double *y, int dim_x, int dim_u);
double norm(double *x, int dim_x);

// adouble version 
int fixpoint_iter_act(adouble *x, adouble *u, adouble *y, int dim_x, int dim_u);
adouble norm(adouble *x, int dim_x);


int tag_full, tag_part, tag_fixpoint;


int main()
{
  adouble x[1];
  adouble u[1];
  adouble y[1];
  adouble diff[1];
  double eps=1.0e-6;
  
  double up[1];
  double yp[1];
  double grad[1];

  int i;
  
  tag_full = 1;
  tag_part = 2;
  tag_fixpoint = 3;

  trace_on(tag_full);
    i = 0;
    u[0] <<= 0.5;
    y[0] = 1.57079632679;

    do
      {
	i++;
	x[0] = y[0];
        fixpoint_iter_act(x,u,y,1,1);
	printf(" i = %3d y = %12.9f\n",i,y[0].value());
	diff[0] = x[0]-y[0];
      }
    while((norm(diff,1)>eps) && (i<imax));
    y[0] >>= yp[0];
  trace_off(1);

  up[0] = 0.5;
  gradient(tag_full,1,up,grad);

  printf("\n full taping:\n gradient = ( %f )\n",grad[0]);

  printf("\n taping with fixpoint facility:\n\n");
  trace_on(tag_part);
    i = 0;
    u[0] <<= 0.5;
    x[0] = 1.57079632679;

    fp_iteration(tag_fixpoint,fixpoint_iter,fixpoint_iter_act,norm,norm,eps,eps,imax,imax_deriv,x,u,y,1,1);

    y[0] >>= yp[0];
  trace_off(1);

  up[0] = 0.5;
  gradient(tag_part,1,up,grad);

  printf("\n=> gradient = ( %f )\n",grad[0]);
  return 0;
}

int fixpoint_iter(double *x, double *u, double *y, int dim_x, int dim_u)
{  
  y[0] = u[0]*(x[0]+cos(x[0]));
  
  return 0;
}

double norm(double *x, int dim_x)
{
  return fabs(x[0]);
}

int fixpoint_iter_act(adouble *x, adouble *u, adouble *y, int dim_x, int dim_u)
{  
  y[0] = u[0]*(x[0]+cos(x[0]));
  
  return 0;
}

adouble norm(adouble *x, int dim_x)
{
  return fabs(x[0]);
}