Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tradvec_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // tradvec_example
11 //
12 // usage:
13 // tradvec_example
14 //
15 // output:
16 // prints the results of differentiating two simple functions and their
17 // sum with reverse mode AD using functions Outvar_Gradcomp and
18 // Weighted_GradcompVec in the Sacado::RadVec::ADvar class.
19 
20 /* Simple test of Outvar_Gradcomp and Weighted_GradcompVec. */
21 
22 #include "Sacado_tradvec.hpp"
23 #include <stdio.h>
24 
25 #ifdef _MSC_VER
26 # define snprintf _snprintf
27 #endif
28 
30 
31  ADVar
32 foo(double d, ADVar x, ADVar y)
33 { return d*x*y; }
34 
35  ADVar
36 goo(double d, ADVar x, ADVar y)
37 { return d*(x*x + y*y); }
38 
39  struct
41  const char *name;
42  double v; // value of name
43  double dvdx; // partial w.r.t. x
44  double dvdy; // partial w.r.t. y
45  };
46 
47  static ExpectedAnswer expected[4] = {
48  { "a", 6., 3., 2. },
49  { "b", 13., 4., 6. },
50  { "c", 19., 7., 8. },
51  { "(a + b + c)", 38., 14., 16. }};
52 
53  int
54 botch(ExpectedAnswer *e, const char *partial, double got, double wanted)
55 {
56  char buf[32];
57  const char *what;
58 
59  what = e->name;
60  if (partial) {
61  snprintf(buf, sizeof(buf), "d%s/d%s", what, partial);
62  what = buf;
63  }
64  fprintf(stderr, "Expected %s = %g, but got %g\n", what, wanted, got);
65  return 1;
66  }
67 
68  int
69 acheck(int k, double d, double v, double dvdx, double dvdy)
70 {
71  ExpectedAnswer *e = &expected[k];
72  int nbad = 0;
73 
74  /* There should be no round-off error in this simple example, so we */
75  /* use exact comparisons, rather than, say, relative differences. */
76 
77  if (v != d*e->v)
78  nbad += botch(e, 0, v, d*e->v);
79  if (dvdx != d*e->dvdx)
80  nbad += botch(e, "x", dvdx, d*e->dvdx);
81  if (dvdy != d*e->dvdy)
82  nbad += botch(e, "y", dvdy, d*e->dvdy);
83  return nbad;
84  }
85 
86  int
87 main(void)
88 {
89  double d, z[4];
90  int i, nbad;
91 
92  static ADVar a, b, c, x, y, *v[3] = {&a, &b, &c};
93  static ADVar **V[4] = {v, v+1, v+2, v};
94  static size_t np[4] = {1, 1, 1, 3};
95  static double w[3] = { 1., 1., 1. };
96  static double *W[4] = {w, w, w, w};
97 
98  nbad = 0;
99  for(d = 1.; d <= 2.; ++d) {
100  printf("\nd = %g\n", d);
101  x = 2;
102  y = 3;
103  a = foo(d,x,y);
104  b = goo(d,x,y);
105  c = a + b;
106 
108  printf("a = %g\n", a.val());
109  printf("da/dx = %g\n", x.adj());
110  printf("da/dy = %g\n", y.adj());
111  nbad += acheck(0, d, a.val(), x.adj(), y.adj());
112  z[0] = a.val();
113 
115  printf("b = %g\n", b.val());
116  printf("db/dx = %g\n", x.adj());
117  printf("db/dy = %g\n", y.adj());
118  nbad += acheck(1, d, b.val(), x.adj(), y.adj());
119  z[1] = b.val();
120 
122  printf("c = %g (should be a + b)\n", c.val());
123  printf("dc/dx = %g\n", x.adj());
124  printf("dc/dy = %g\n", y.adj());
125  nbad += acheck(2, d, c.val(), x.adj(), y.adj());
126  z[2] = c.val();
127  z[3] = z[0] + z[1] + z[2];
128 
129  ADVar::Weighted_GradcompVec(4,np,V,W);
130  for(i = 0; i < 4; ++i) {
131  printf("w %d:\td/dx = %g\td/dy = %g\n", i, x.adj(i), y.adj(i));
132  nbad += acheck(i, d, z[i], x.adj(i), y.adj(i));
133  }
134  }
135  if (nbad == 0)
136  printf("\nExample passed!\n");
137  else
138  printf("\nSomething is wrong, example failed!\n");
139  return nbad > 0;
140  }
int botch(ExpectedAnswer *e, const char *partial, double got, double wanted)
Sacado::RadVec::ADvar< double > ADVar
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
static void Outvar_Gradcomp(ADvar &v)
int main()
Definition: ad_example.cpp:171
ADVar foo(double d, ADVar x, ADVar y)
int acheck(int k, double d, double v, double dvdx, double dvdy)
Uncopyable z
const char * name
static void Weighted_GradcompVec(size_t n, size_t *np, ADvar ***v, Double **w)
const double y
ADVar goo(double d, ADVar x, ADVar y)
static ExpectedAnswer expected[4]