Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LogicalSparseUnitTests.cpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
33 
35 
37  urand(0.0, 1.0), n(5), tol_a(1.0e-15), tol_r(1.0e-14) {}
38 
40  double absolute_tolerance,
41  double relative_tolerance) :
42  urand(0.0, 1.0),
43  n(numComponents),
44  tol_a(absolute_tolerance),
45  tol_r(relative_tolerance) {}
46 
48  double val;
49 
50  val = urand.number();
51  a_dfad = DFadType(n,val);
52  a_ls = LSType(n,val);
53 
54  val = urand.number();
55  b_dfad = DFadType(n,val);
56  b_ls = LSType(n,val);
57 
58  val = urand.number();
59  c_dfad = val;
60  c_ls = val;
61 
62  for (int i=0; i<n; i++) {
63  val = urand.number();
64  a_dfad.fastAccessDx(i) = val;
65  a_ls.fastAccessDx(i) = 1;
66 
67  val = urand.number();
68  b_dfad.fastAccessDx(i) = val;
69  b_ls.fastAccessDx(i) = 1;
70  }
71 }
72 
74 
76  const LSType& x_ls) {
77 
78  // Compare sizes
79  CPPUNIT_ASSERT(x_dfad.size() == x_ls.size());
80 
81  // Compare hasFastAccess
82  CPPUNIT_ASSERT(x_dfad.hasFastAccess() == x_ls.hasFastAccess());
83 
84  // Compare values
85  compareDoubles(x_dfad.val(), x_ls.val());
86 
87  for (int i=0; i<x_ls.size(); i++) {
88 
89  // Compare dx
90  compareDx(x_dfad.dx(i), x_ls.dx(i));
91 
92  // Compare fastAccessDx
93  compareDx(x_dfad.fastAccessDx(i), x_ls.fastAccessDx(i));
94  }
95 }
96 
98  CPPUNIT_ASSERT( fabs(a-b) < tol_a + tol_r*fabs(a) );
99 }
100 
102  CPPUNIT_ASSERT( a == b );
103 }
104 
106  CPPUNIT_ASSERT( (a && b) || !(a || b) );
107 }
108 
110  double val;
111 
112  // LFAd, LFad
113  LSType aa_ls = a_ls + 1.0;
114  c_ls = max(aa_ls, a_ls);
115  compareDoubles(c_ls.val(), aa_ls.val());
116  for (int i=0; i<n; i++) {
117  compareBools(c_ls.dx(i), aa_ls.dx(i));
118  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
119  }
120  c_ls = max(a_ls, aa_ls);
121  compareDoubles(c_ls.val(), aa_ls.val());
122  for (int i=0; i<n; i++) {
123  compareBools(c_ls.dx(i), aa_ls.dx(i));
124  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
125  }
126 
127  // Expr, LFad
128  c_ls = max(a_ls+1.0, a_ls);
129  compareDoubles(c_ls.val(), aa_ls.val());
130  for (int i=0; i<n; i++) {
131  compareBools(c_ls.dx(i), aa_ls.dx(i));
132  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
133  }
134  c_ls = max(a_ls, a_ls+1.0);
135  compareDoubles(c_ls.val(), aa_ls.val());
136  for (int i=0; i<n; i++) {
137  compareBools(c_ls.dx(i), aa_ls.dx(i));
138  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
139  }
140 
141  // Expr, Expr (same)
142  c_ls = max(a_ls+1.0, a_ls+1.0);
143  compareDoubles(c_ls.val(), aa_ls.val());
144  for (int i=0; i<n; i++) {
145  compareBools(c_ls.dx(i), aa_ls.dx(i));
146  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
147  }
148 
149  // Expr, Expr (different)
150  c_ls = max(a_ls+1.0, a_ls-1.0);
151  compareDoubles(c_ls.val(), aa_ls.val());
152  for (int i=0; i<n; i++) {
153  compareBools(c_ls.dx(i), aa_ls.dx(i));
154  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
155  }
156  c_ls = max(a_ls-1.0, a_ls+1.0);
157  compareDoubles(c_ls.val(), aa_ls.val());
158  for (int i=0; i<n; i++) {
159  compareBools(c_ls.dx(i), aa_ls.dx(i));
160  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
161  }
162 
163  // LFad, const
164  val = a_ls.val() + 1;
165  c_ls = max(a_ls, val);
166  compareDoubles(c_ls.val(), val);
167  for (int i=0; i<n; i++)
168  compareBools(c_ls.dx(i), 0);
169  val = a_ls.val() - 1;
170  c_ls = max(a_ls, val);
171  compareDoubles(c_ls.val(), a_ls.val());
172  for (int i=0; i<n; i++) {
173  compareBools(c_ls.dx(i), a_ls.dx(i));
174  compareBools(c_ls.fastAccessDx(i), a_ls.fastAccessDx(i));
175  }
176  val = b_ls.val() + 1;
177  c_ls = max(val, b_ls);
178  compareDoubles(c_ls.val(), val);
179  for (int i=0; i<n; i++)
180  compareBools(c_ls.dx(i), 0);
181  val = b_ls.val() - 1;
182  c_ls = max(val, b_ls);
183  compareDoubles(c_ls.val(), b_ls.val());
184  for (int i=0; i<n; i++) {
185  compareBools(c_ls.dx(i), b_ls.dx(i));
186  compareBools(c_ls.fastAccessDx(i), b_ls.fastAccessDx(i));
187  }
188 
189  // Expr, const
190  val = a_ls.val();
191  c_ls = max(a_ls+1.0, val);
192  compareDoubles(c_ls.val(), aa_ls.val());
193  for (int i=0; i<n; i++) {
194  compareBools(c_ls.dx(i), aa_ls.dx(i));
195  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
196  }
197  c_ls = max(val, a_ls+1.0);
198  compareDoubles(c_ls.val(), aa_ls.val());
199  for (int i=0; i<n; i++) {
200  compareBools(c_ls.dx(i), aa_ls.dx(i));
201  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
202  }
203 
204 }
205 
207  double val;
208 
209  // LFad, LFad
210  LSType aa_ls = a_ls - 1.0;
211  c_ls = min(aa_ls, a_ls);
212  compareDoubles(c_ls.val(), aa_ls.val());
213  for (int i=0; i<n; i++) {
214  compareBools(c_ls.dx(i), aa_ls.dx(i));
215  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
216  }
217  c_ls = min(a_ls, aa_ls);
218  compareDoubles(c_ls.val(), aa_ls.val());
219  for (int i=0; i<n; i++) {
220  compareBools(c_ls.dx(i), aa_ls.dx(i));
221  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
222  }
223 
224  // Expr, LFad
225  c_ls = min(a_ls-1.0, a_ls);
226  compareDoubles(c_ls.val(), aa_ls.val());
227  for (int i=0; i<n; i++) {
228  compareBools(c_ls.dx(i), aa_ls.dx(i));
229  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
230  }
231  c_ls = min(a_ls, a_ls-1.0);
232  compareDoubles(c_ls.val(), aa_ls.val());
233  for (int i=0; i<n; i++) {
234  compareBools(c_ls.dx(i), aa_ls.dx(i));
235  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
236  }
237 
238  // Expr, Expr (same)
239  c_ls = min(a_ls-1.0, a_ls-1.0);
240  compareDoubles(c_ls.val(), aa_ls.val());
241  for (int i=0; i<n; i++) {
242  compareBools(c_ls.dx(i), aa_ls.dx(i));
243  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
244  }
245 
246  // Expr, Expr (different)
247  c_ls = min(a_ls+1.0, a_ls-1.0);
248  compareDoubles(c_ls.val(), aa_ls.val());
249  for (int i=0; i<n; i++) {
250  compareBools(c_ls.dx(i), aa_ls.dx(i));
251  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
252  }
253  c_ls = min(a_ls-1.0, a_ls+1.0);
254  compareDoubles(c_ls.val(), aa_ls.val());
255  for (int i=0; i<n; i++) {
256  compareBools(c_ls.dx(i), aa_ls.dx(i));
257  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
258  }
259 
260  // LFad, const
261  val = a_ls.val() - 1;
262  c_ls = min(a_ls, val);
263  compareDoubles(c_ls.val(), val);
264  for (int i=0; i<n; i++)
265  compareBools(c_ls.dx(i), 0);
266  val = a_ls.val() + 1;
267  c_ls = min(a_ls, val);
268  compareDoubles(c_ls.val(), a_ls.val());
269  for (int i=0; i<n; i++) {
270  compareBools(c_ls.dx(i), a_ls.dx(i));
271  compareBools(c_ls.fastAccessDx(i), a_ls.fastAccessDx(i));
272  }
273  val = b_ls.val() - 1;
274  c_ls = min(val, b_ls);
275  compareDoubles(c_ls.val(), val);
276  for (int i=0; i<n; i++)
277  compareBools(c_ls.dx(i), 0);
278  val = b_ls.val() + 1;
279  c_ls = min(val, b_ls);
280  compareDoubles(c_ls.val(), b_ls.val());
281  for (int i=0; i<n; i++) {
282  compareBools(c_ls.dx(i), b_ls.dx(i));
283  compareBools(c_ls.fastAccessDx(i), b_ls.fastAccessDx(i));
284  }
285 
286  // Expr, const
287  val = a_ls.val();
288  c_ls = min(a_ls-1.0, val);
289  compareDoubles(c_ls.val(), aa_ls.val());
290  for (int i=0; i<n; i++) {
291  compareBools(c_ls.dx(i), aa_ls.dx(i));
292  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
293  }
294  c_ls = min(val, a_ls-1.0);
295  compareDoubles(c_ls.val(), aa_ls.val());
296  for (int i=0; i<n; i++) {
297  compareBools(c_ls.dx(i), aa_ls.dx(i));
298  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
299  }
300 }
void compareDoubles(double a, double b)
void compareDx(double a, bool b)
Sacado::Fad::DFad< double > DFadType
ScalarT number()
Get random number.
CPPUNIT_TEST_SUITE_REGISTRATION(DFadDoubleTest)
expr val()
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
Sacado::LFad::LogicalSparse< double, bool > LSType
Sacado::Random< double > urand
fabs(expr.val())
int n
void compareFads(const DFadType &x_dfad, const LSType &x_ls)