NeoPZ
simple.cc
Go to the documentation of this file.
1 // Emacs will be in -*- Mode: c++ -*-
2 //
3 // ************ DO NOT REMOVE THIS BANNER ****************
4 //
5 // Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
6 // http://www.ann.jussieu.fr/~dicesare
7 //
8 // Tiny benchmark of TinyFad with and without
9 // exression template(ET)
10 //
11 //********************************************************
12 //
13 // This is a simple example to show how to use the
14 // library
15 //
16 //********************************************************
17 
18 
19 #include <iostream>
20 
21 // Fad with expression template
22 #include <Fad/fad.h>
23 // Fixed size Fad
24 #include <TinyFad/tinyfad.h>
25 // Fixed size Fad with expression template
26 #include <TinyFadET/tfad.h>
27 
28 //*****************************************
29 //
30 // Compute the derivative of y = x*x with
31 // respect to x
32 //
33 //*****************************************
34 int main()
35 {
36 
37 
38  float x = 2.f;
39  const int nvar = 1;
40 
41  Fad<float> x1(x), y1;
42 
43  TinyFad<nvar,float> x2(x), y2;
44 
45  TFad<nvar,float> x3(x), y3;
46 
47  // activate xi variables :
48  // the first argument is the order number
49  // of the activated variable and the second
50  // is the number of activated variables. It's
51  // the same interface for the 3 classes
52  x1.diff(0,nvar);
53 
54  x2.diff(0,nvar);
55 
56  x3.diff(0,nvar);
57 
58  TinyFad<2, double> variavel1, variavel2, y;
59 
60  variavel1 = 4.;
61  variavel2 = 5.;
62 
63 
64 
65  variavel1.diff(0, 3);
66  variavel2.diff(1,3);
67 
68  // funcao y = v1²*v2 + v2*3.;
69 
70  y = variavel1 * variavel1 * variavel2 + variavel2 * 3.;
71 
72  cout <<"teste" << y << endl << endl;
73 
74  // do computation
75  y1 = x1*x1;
76 
77  y2 = x2*x2;
78 
79  y3 = x3*x3;
80 
81  cout << "Fad : " << y1 << endl
82  << "TinyFad : " << y2 << endl
83  << "TFad : " << y3 << endl;
84  /*
85  const int N =1;
86 
87  TFad<N,TFad<N,TFad<N,float> > > xthird, result;
88  TFad<N,TFad<N,float> > xsecnd;
89  TFad<N,float> xfirst(4.);
90 
91  xfirst.diff(0,N);
92 
93  xsecnd=xfirst;
94  xsecnd.diff(0,N);
95 
96  xthird=xsecnd;
97  xthird.diff(0,N);
98 
99  result = xthird*xthird*xthird;
100  */
101 
102  const int N = 1;
103 
104  /*
105 
106  TFad<N,TFad<N,float> > xsecnd, result;
107  TFad<N,float> xfirst(7.);
108 
109  xfirst.fastAccessDx(0)=5;
110 
111  xsecnd=xfirst;
112  xsecnd.fastAccessDx(0)=5;
113 
114  result = xsecnd * xsecnd;
115 
116  cout << "\n\nTest: first(50x) and second (50) derivatives of (5x)^2 with x=.1.4\n" << result;
117  cout << "\nx= " << xsecnd;
118 
119 
120  result *= xsecnd;
121  cout << "\n\nTest: first(3x^2) and second (6x) derivatives of x^3 with x=4.\n" << result;
122  cout << "\nx= " << xsecnd;
123 
124  result *= xsecnd;
125  cout << "\n\nTest: first(4x^3) and second (12x^2x) derivatives of x^4 with x=4.\n" << result;
126  cout << "\nx= " << xsecnd;
127 
128  result *= xsecnd;
129  cout << "\n\nTest: first(5x^4) and second (20x^3) derivatives of x^5 with x=4.\n" << result;
130  cout << "\nx= " << xsecnd;
131 
132  result *= xsecnd;
133  cout << "\n\nTest: first(6x^5) and second (30x^4) derivatives of x^6 with x=4.\n" << result;
134  cout << "\nx= " << xsecnd;
135  */
136 /*
137  Fad<Fad<float> > xsecnd, result;
138  Fad<float> xfirst(7.), init;
139  Fad<Fad<Fad<float> > > divertido(1,0,Fad<Fad<float > > (1,0,Fad<float> (1,0,3))), resultado;
140 
141  cout << divertido;
142 
143  resultado = divertido * divertido;
144 
145  cout << "\ndivertido ao quadrado: " << resultado;
146 
147 
148  resultado = resultado * divertido;
149 
150  cout << "\ndivertido ao cubo: " << resultado;
151 
152  resultado = resultado * divertido;
153 
154  cout << "\ndivertido ^4: " << resultado;*/
155 /*
156  xfirst.diff(0, N);
157  xfirst.fastAccessDx(0)=5.;
158 
159  init.diff(0,N);
160  init = 5.;
161 
162  xsecnd=xfirst;
163  xsecnd.diff(0,N);
164  xsecnd.fastAccessDx(0)=init;
165 
166  result = xsecnd * xsecnd;
167 
168  cout << "\n\nTest: first(50x) and second (50) derivatives of (5x)^2 with x=.1.4\n" << result;
169  cout << "\nx= " << xsecnd;
170 
171 
172  result *= xsecnd;
173  cout << "\n\nTest: first(3x^2) and second (6x) derivatives of x^3 with x=4.\n" << result;
174  cout << "\nx= " << xsecnd;
175 
176  result *= xsecnd;
177  cout << "\n\nTest: first(4x^3) and second (12x^2x) derivatives of x^4 with x=4.\n" << result;
178  cout << "\nx= " << xsecnd;
179 
180  result *= xsecnd;
181  cout << "\n\nTest: first(5x^4) and second (20x^3) derivatives of x^5 with x=4.\n" << result;
182  cout << "\nx= " << xsecnd;
183 
184  result *= xsecnd;
185  cout << "\n\nTest: first(6x^5) and second (30x^4) derivatives of x^6 with x=4.\n" << result;
186  cout << "\nx= " << xsecnd;
187  */
188 
189  Fad<float> all(3,0,0);
190  all.fastAccessDx(1)=1.;
191  all.fastAccessDx(2)=1.;
192  cout << "\n all differentiable = " << all;
193 
194  Fad<Fad<float> > teste(3,0,Fad<float> (3,0,7));
195 
196 
197  Fad<Fad<float> > multipl(3,0, all), res;
198  multipl.fastAccessDx(1)=1.;
199  multipl.fastAccessDx(2)=1.;
200 
201  res *= 3.;
202 
203  res = teste * multipl;
204 
205  cout << "\nteste" << teste;
206 
207  cout << "\nmultipl" << multipl;
208 
209  cout << "\nteste * multipl" << res;
210 
211  return 1;
212 }
int main()
Definition: simple.cc:34
T & fastAccessDx(int i)
Definition: fad.h:117
Definition: fad.h:54
void diff(const int ith, const int n)
Definition: tinyfad.h:158
TPZPlasticIntegrMem< REAL, 4 > teste
Definition: tfad.h:64
string res
Definition: test.py:151
void diff(const int ith, const int n)
Definition: tfad.h:196