Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_VectorStdOpsTester_def.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef THYRA_VECTOR_STD_OPS_TESTER_HPP
11 #define THYRA_VECTOR_STD_OPS_TESTER_HPP
12 
13 #include "Thyra_VectorStdOpsTester_decl.hpp"
14 #include "Thyra_TestingTools.hpp"
15 #include "RTOpPack_TOpSetAssendingValues.hpp"
16 #include "Teuchos_TestingHelpers.hpp"
17 #include "Teuchos_Assert.hpp"
18 
19 //#define THYRA_VECTOR_STD_OPS_TESTER_DUMP
20 
21 #ifdef THYRA_VECTOR_STD_OPS_TESTER_DUMP
22 # include "RTOpPack_SPMD_apply_op.hpp"
23 #endif // THYRA_VECTOR_STD_OPS_TESTER_DUMP
24 
25 
26 namespace Thyra {
27 
28 
29 // VectorStdOpsTesterComparable (using partial specialization to only do tests in some cases)
30 
31 
32 template <bool isComparable, class Scalar>
33 class VectorStdOpsTesterComparable {
34 public:
35  static bool checkComparableStdOps(
36  const VectorSpaceBase<Scalar> &vecSpc,
37  const Ptr<VectorBase<Scalar> > &z,
38  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &error_tol,
39  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &warning_tol,
40  const Ptr<std::ostream> &out,
41  const bool &dumpAll
42  )
43  {
45  }
46 };
47 
48 
49 template <class Scalar>
50 class VectorStdOpsTesterComparable<false,Scalar> {
51 public:
52  static bool checkComparableStdOps(
53  const VectorSpaceBase<Scalar> &vecSpc,
54  const Ptr<VectorBase<Scalar> > &z,
55  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &error_tol,
56  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &warning_tol,
57  const Ptr<std::ostream> &out,
58  const bool &dumpAll
59  )
60  {
61  if (nonnull(out)) *out
62  << "\nThis scalar type does not support comparable operations so"
63  << " we can not test min(), max() and other such functions.\n";
64  return true;
65  }
66 };
67 
68 
69 template <class Scalar>
70 class VectorStdOpsTesterComparable<true,Scalar> {
71 public:
72  static bool checkComparableStdOps(
73  const VectorSpaceBase<Scalar> &vecSpc,
74  const Ptr<VectorBase<Scalar> > &z,
75  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &error_tol,
76  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &warning_tol,
77  const Ptr<std::ostream> &out,
78  const bool &dumpAll
79  )
80  {
82  using Teuchos::outArg;
83 
84  bool success = true, result;
85 
86  if (nonnull(out)) *out << "\nTesting comparable operations ...\n";
87 
88  const Scalar scalarSmall(1e-5), scalarMedium(2.0), scalarLarge(100.0);
89  if (nonnull(out)) *out << "\nassign(z.ptr(),"<<scalarMedium<<");\n";
90  assign(z.ptr(),Scalar(scalarMedium));
91  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
92  if (nonnull(out)) *out << "\nset_ele(0,"<<scalarSmall<<",z.ptr());\n";
93  set_ele(0,scalarSmall,z.ptr());
94  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
95  if (nonnull(out)) *out << "\nset_ele(1,"<<scalarLarge<<",z.ptr());\n";
96  set_ele(1,scalarLarge,z.ptr());
97  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
98  if (nonnull(out)) *out << "\nset_ele(vecSpc.dim()-2,"<<scalarSmall<<",z.ptr());\n";
99  set_ele(vecSpc.dim()-2,scalarSmall,z.ptr());
100  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
101  if (nonnull(out)) *out << "\nset_ele(vecSpc.dim()-1,"<<scalarLarge<<",z.ptr());\n";
102  set_ele(vecSpc.dim()-1,scalarLarge,z.ptr());
103  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
104 
105  Scalar minEle; Ordinal minIndex;
106  Scalar maxEle; Ordinal maxIndex;
107 
108  if(!Teuchos::testRelErr<Scalar>(
109  "min(*z)",min(*z),"scalarSmall",scalarSmall
110  ,"error_tol",error_tol,"warning_tol",warning_tol,out
111  )
112  ) success=false;
113 
114  if (nonnull(out)) *out << "\nmin(*z,&minEle,&minIndex);\n";
115  minEle = ST::zero(); minIndex = 0;
116  min(*z, outArg(minEle), outArg(minIndex));
117  if(!Teuchos::testRelErr<Scalar>(
118  "minEle",minEle,"scalarSmall",scalarSmall
119  ,"error_tol",error_tol,"warning_tol",warning_tol, out
120  )
121  ) success=false;
122  result = minIndex == 0;
123  if (nonnull(out)) *out << "\nminIndex = " << minIndex << " == 0 ? " << passfail(result) << std::endl;
124  if(!result) success = false;
125 
126  if (nonnull(out)) *out << "\nminGreaterThanBound(*z,"<<scalarMedium<<",&minEle,&minIndex);\n";
127  minEle = ST::zero(); minIndex = 0;
128  minGreaterThanBound(*z, scalarMedium, outArg(minEle), outArg(minIndex));
129  if(!Teuchos::testRelErr<Scalar>(
130  "minEle",minEle,"scalarLarge",scalarLarge
131  ,"error_tol",error_tol,"warning_tol",warning_tol,out
132  )
133  ) success=false;
134  result = minIndex == 1;
135  if (nonnull(out)) *out << "\nminIndex = " << minIndex << " == 1 ? " << passfail(result) << std::endl;
136  if(!result) success = false;
137 
138  if (nonnull(out)) *out << "\nminGreaterThanBound(*z,"<<scalarLarge<<",&minEle,&minIndex);\n";
139  minEle = ST::zero(); minIndex = 0;
140  minGreaterThanBound(*z,scalarLarge, outArg(minEle), outArg(minIndex));
141  result = minIndex < 0;
142  if (nonnull(out)) *out << "\nminIndex = " << minIndex << " < 0 ? " << passfail(result) << std::endl;
143  if(!result) success = false;
144 
145  if(!Teuchos::testRelErr<Scalar>(
146  "max(*z)",max(*z),"scalarLarge",scalarLarge
147  ,"error_tol",error_tol,"warning_tol",warning_tol,out)
148  ) success=false;
149 
150  if (nonnull(out)) *out << "\nmax(*z,&maxEle,&maxIndex);\n";
151  maxEle = ST::zero(); maxIndex = 0;
152  max(*z, outArg(maxEle), outArg(maxIndex));
153  if(!Teuchos::testRelErr<Scalar>(
154  "maxEle",maxEle,"scalarLarge",scalarLarge
155  ,"error_tol",error_tol,"warning_tol",warning_tol,out)
156  ) success=false;
157  result = maxIndex == 1;
158  if (nonnull(out)) *out << "\nmaxIndex = " << maxIndex << " == 1 ? " << passfail(result) << std::endl;
159  if(!result) success = false;
160 
161  if (nonnull(out)) *out << "\nmaxLessThanBound(*z,"<<scalarMedium<<",&maxEle,&maxIndex);\n";
162  maxEle = ST::zero(); maxIndex = 0;
163  maxLessThanBound(*z, scalarMedium, outArg(maxEle), outArg(maxIndex));
164  if(!Teuchos::testRelErr<Scalar>(
165  "maxEle",maxEle,"scalarSmall",scalarSmall
166  ,"error_tol",error_tol,"warning_tol",warning_tol,out)
167  ) success=false;
168  result = maxIndex == 0;
169  if (nonnull(out)) *out << "\nmaxIndex = " << maxIndex << " == 0 ? " << passfail(result) << std::endl;
170  if(!result) success = false;
171 
172  if (nonnull(out)) *out << "\nmaxLessThanBound(*z,"<<scalarSmall<<",&maxEle,&maxIndex);\n";
173  maxEle = ST::zero(); maxIndex = 0;
174  maxLessThanBound(*z, scalarSmall, outArg(maxEle), outArg(maxIndex));
175  result = ( maxIndex < 0 );
176  if (nonnull(out)) *out << "\nmaxIndex = " << maxIndex << " < 0 ? " << passfail(result) << std::endl;
177  if(!result) success = false;
178 
179 
180  // Test pair_wise_max
181  if (nonnull(out)) *out << "\nTesting pair_wise_max(*v1, *v2, z0.ptr()) ...\n";
182  {
183  using Teuchos::as;
184  using Teuchos::tuple;
185  using Teuchos::null;
186  using Teuchos::outArg;
187  using Teuchos::inOutArg;
188  Teuchos::RCP<VectorBase<Scalar> > v1 = createMember(vecSpc);
189  Teuchos::RCP<VectorBase<Scalar> > v2 = createMember(vecSpc);
190  Teuchos::RCP<VectorBase<Scalar> > v3 = createMember(vecSpc);
191  Teuchos::RCP<VectorBase<Scalar> > z0 = createMember(vecSpc);
192  const Scalar alpha = as<Scalar>(2.0);
193  seed_randomize<Scalar>(12345);
194  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
195  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
196 
197  // z0 = alpha*max(v1_i, v2_i)
198  pair_wise_max(alpha, *v1, *v2, z0.ptr());
199 
200  V_VpV(v3.ptr(), *v1, *v2); // v3 = v1 + v2
201  Vp_V(v1.ptr(), *v2, as<Scalar>(-ST::one())); // v1 -= v2
202  abs(*v1, v2.ptr()); // v2 = abs(v1)
203  Vp_V(v3.ptr(), *v2); // v3 += v2
204 
205  // v1 + v2 + | v1 - v2 | = 2 max (v1, v2)
206  Vp_V(z0.ptr(), *v3, as<Scalar>(-ST::one())); // z0 -= v3
207  if(!testMaxErr<Scalar>(
208  "norm_2(*z0)",norm_2(*z0)
209  ,"error_tol",error_tol,"warning_tol",warning_tol,out.get()
210  )
211  ) success=false;
212  }
213 
214  return success;
215  }
216 };
217 
218 
219 // Other helpers
220 
221 
222 template<class Scalar>
223 void setEleTestCase( const Ptr<VectorBase<Scalar> > &z, const Ordinal i, int &tc,
224  std::ostream &out, bool &success)
225 {
226  using Teuchos::as;
227  out << "\n"<<tc<<") set_ele(z, "<<i<<");\n";
228  ++tc;
229  {
230  typedef ScalarTraits<Scalar> ST;
231  const Scalar val_i = as<Scalar>(i+1);
232  assign<Scalar>(z, ST::zero());
233  set_ele(i, val_i, z);
234  TEUCHOS_TEST_EQUALITY_CONST(get_ele(*z, i), val_i, out, success);
235  TEUCHOS_TEST_EQUALITY_CONST(sum(*z), val_i, out, success);
236  }
237 }
238 
239 
240 // VectorStdOpsTester
241 
242 
243 template <class Scalar>
245  const ScalarMag &warning_tol_in,
246  const ScalarMag &error_tol_in
247  )
248  :warning_tol_(warning_tol_in),
249  error_tol_(error_tol_in)
250 {}
251 
252 
253 template <class Scalar>
255  const VectorSpaceBase<Scalar> &vecSpc,
256  std::ostream *out_out,
257  const bool &dumpAll
258  )
259 {
260  using Teuchos::as;
261  using Teuchos::tuple;
262  using Teuchos::null;
263  using Teuchos::outArg;
264  using Teuchos::inOutArg;
266 
267  TEUCHOS_ASSERT(out_out);
268  std::ostream &out = *out_out;
269 
270  out << "\n*** Entering VectorStdOpsTester<"<<ST::name()<<">::checkStdOps(...) ...\n"
271  << "using a \'" << vecSpc.description() << "\' object ...\n";
272 
273  bool success = true;
274  out << "\nvecSpc.dim() = " << vecSpc.dim() << std::endl;
275 
276  const Ordinal n = vecSpc.dim();
277 
278  TEUCHOS_TEST_FOR_EXCEPTION( n < 4, std::logic_error,
279  "Error: n = "<<n<<" must be least 4 or greater to"
280  " run Thyra::VectorStdOpsTester::checkStdOps(...)!" );
281 
282  const Scalar
283  two = as<Scalar>(2.0),
284  three = as<Scalar>(3.0),
285  four = as<Scalar>(4.0);
286 
287  int tc = 0;
288 
289  out << "\nCreating vectors v1, v2, v3, v4, x and z ...\n";
291  v1 = createMember(vecSpc),
292  v2 = createMember(vecSpc),
293  v3 = createMember(vecSpc),
294  v4 = createMember(vecSpc),
295  y = createMember(vecSpc),
296  x = createMember(vecSpc),
297  z = createMember(vecSpc);
298 
299  out << "\nassign(v1.ptr(), -2.0);\n";
300  assign<Scalar>(v1.ptr(), -two);
301  out << "\nassign(v2.ptr(), -3.0);\n";
302  assign<Scalar>(v2.ptr(), -three);
303  out << "\nassign(v3.ptr(), -4.0);\n";
304  assign<Scalar>(v3.ptr(), -four);
305  out << "\ny[i] = i+1\n";
306  {
307  RTOpPack::TOpSetAssendingValues<Scalar> setAssendOp(ST::zero());
308  applyOp<Scalar>( setAssendOp,
310  tuple<Ptr<VectorBase<Scalar> > >(y.ptr())(),
311  null );
312  }
313 
314  // sum
315  out << "\n"<<tc<<") sum(*y);\n";
316  ++tc;
318  Teuchos::testRelErr<Scalar>(
319  "sum(*y)", sum(*y),
320  "0.5*(n+1)*n", as<Scalar>(0.5*(n+1)*n),
321  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
322  out, success);
323 
324  // norm_inf
325  out << "\n"<<tc<<") nom_inf(*v1);\n";
326  ++tc;
328  Teuchos::testRelErr<Scalar>(
329  "norm_inf(*v1)", norm_inf(*v1),
330  "2.0", two,
331  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
332  out, success);
333 
334  // norm_2
335  out << "\n"<<tc<<") norm_2(*v1);\n";
336  ++tc;
338  Teuchos::testRelErr<Scalar>(
339  "norm_2(*v1)", norm_2(*v1),
340  "2.0*sqrt(vecSpc.dim())", as<Scalar>(2.0)*ST::squareroot(vecSpc.dim()),
341  "error_tol", error_tol(), "warning_tol",warning_tol(), inOutArg(out)),
342  out, success);
343 
344  // norm_1
345  out << "\n"<<tc<<") norm_1(*v1);\n";
346  ++tc;
348  Teuchos::testRelErr<Scalar>(
349  "norm_1(*v1)" ,norm_1(*v1),
350  "2.0*vecSpc.dim()", as<Scalar>(2.0)*as<Scalar>(vecSpc.dim()),
351  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
352  out, success);
353 
354  // abs
355  out << "\n"<<tc<<") abs(z.ptr(),*v1);\n";
356  ++tc;
357  {
358  abs<Scalar>(*v1, z.ptr());
359  if(!Teuchos::testRelErr<Scalar>(
360  "sum(*z)", sum(*z), "2.0*vecSpc.dim()", as<Scalar>(2.0)*as<Scalar>(vecSpc.dim()),
361  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out))
362  ) success=false;
363  }
364 
365  // get_ele
366 
367  out << "\n"<<tc<<") val = get_ele(y, 0);\n";
368  ++tc;
369  {
370  const Scalar val = get_ele<Scalar>(*y, 0);
371  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(1), out, success );
372  }
373 
374  out << "\n"<<tc<<") val = get_ele<Scalar>(*y, 1);\n";
375  ++tc;
376  {
377  const Scalar val = get_ele<Scalar>(*y, 1);
378  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(2), out, success );
379  }
380 
381  out << "\n"<<tc<<") val = get_ele<Scalar>(*y, n-2);\n";
382  ++tc;
383  {
384  const Scalar val = get_ele<Scalar>(*y, n-2);
385  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(n-1), out, success );
386  }
387 
388  out << "\n"<<tc<<") val = get_ele<Scalar>(*y, n-1);\n";
389  ++tc;
390  {
391  const Scalar val = get_ele<Scalar>(*y, n-1);
392  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(n), out, success );
393  }
394 
395 #ifdef THYRA_DEBUG
396 
397  out << "\n"<<tc<<") get_ele<Scalar>(*y, -1);\n";
398  ++tc;
399  {
400  TEUCHOS_TEST_THROW(get_ele<Scalar>(*y, -1), std::out_of_range, out, success );
401  }
402 
403  out << "\n"<<tc<<") get_ele<Scalar>(*y, n);\n";
404  ++tc;
405  {
406  TEUCHOS_TEST_THROW(get_ele<Scalar>(*y, n), std::out_of_range, out, success );
407  }
408 
409 #endif // THYRA_DEBUG
410 
411  // set_ele
412 
413  setEleTestCase<Scalar>(z.ptr(), 0, tc, out, success);
414 
415  setEleTestCase<Scalar>(z.ptr(), 1, tc, out, success);
416 
417  setEleTestCase<Scalar>(z.ptr(), n-2, tc, out, success);
418 
419  setEleTestCase<Scalar>(z.ptr(), n-1, tc, out, success);
420 
421 #ifdef THYRA_DEBUG
422 
423  TEUCHOS_TEST_THROW(set_ele(-1, two, z.ptr()),
424  std::out_of_range, out, success);
425 
426  TEUCHOS_TEST_THROW(set_ele(n, two, z.ptr()),
427  std::out_of_range, out, success);
428 
429 #endif // THYRA_DEBUG
430 
431  // reciprocal
432  out << "\n"<<tc<<") reciprocal(z.ptr(),*v1);\n";
433  ++tc;
434  {
435  reciprocal(*v1, z.ptr());
436  if(!Teuchos::testRelErr<Scalar>(
437  "sum(*z)",sum(*z),"-0.5*vecSpc.dim()",as<Scalar>(-0.5)*as<Scalar>(vecSpc.dim())
438  ,"error_tol",error_tol(),"warning_tol",warning_tol(),inOutArg(out))
439  ) success=false;
440  }
441 
442  // linear_combination
443 
444  out << "\n"<<tc<<") linear_combination(2,{0.5,0.25},{v1.ptr(),v2.ptr()},0.0,z.ptr());\n";
445  ++tc;
446  {
447  linear_combination<Scalar>(
448  tuple<Scalar>(0.5, 0.25)(),
449  tuple<Ptr<const VectorBase<Scalar> > >(v1.ptr(), v2.ptr())(),
450  ST::zero(),
451  z.ptr());
453  Teuchos::testRelErr<Scalar>(
454  "sum(*z)", sum(*z),
455  "(-0.5*2.0-0.25*3.0)*vecSpc.dim()", as<Scalar>((-0.5 * 2.0 - 0.25 * 3.0) *vecSpc.dim()),
456  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
457  out, success);
458  }
459 
460  out << "\nassign(z.ptr(), 2.0);\n";
461  ++tc;
462  assign(z.ptr(), as<Scalar>(2.0));
463 
464  out << "\n"<<tc<<") linear_combination(3,{0.5,0.25,0.125},{v1.ptr(),v2.ptr(),v2.ptr()},0.5,z.ptr());\n";
465  ++tc;
466  {
467  linear_combination<Scalar>(
468  tuple<Scalar>(0.5, 0.25, 0.125)(),
469  tuple<Ptr<const VectorBase<Scalar> > >(v1.ptr(), v2.ptr(), v3.ptr())(),
470  as<Scalar>(0.5),
471  z.ptr());
472  if(!Teuchos::testRelErr<Scalar>(
473  "sum(*z)", sum(*z),
474  "(0.5*2.0-0.5*2.0-0.25*3.0-0.125*4.0)*vecSpc.dim()",
475  as<Scalar>(0.5*2.0-0.5*2.0-0.25*3.0-0.125*4.0)*as<Scalar>(vecSpc.dim()),
476  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)
477  )
478  ) success=false;
479  }
480 
481  // assgin
482  out << "\n"<<tc<<") assign(z.ptr(),2.0);\n";
483  ++tc;
484  {
485  assign(z.ptr(),as<Scalar>(2.0));
486  if(!Teuchos::testRelErr<Scalar>(
487  "norm_2(*z,*v2)",norm_2(*z,*v2)
488  ,"sqrt(2.0*3.0*3.0*vecSpc.dim())",ST::magnitude(ST::squareroot(as<Scalar>(2.0*3.0*3.0)*as<Scalar>(vecSpc.dim())))
489  ,"error_tol",error_tol(),"warning_tol",warning_tol(),inOutArg(out)
490  )
491  ) success=false;
492 
493  if(!VectorStdOpsTesterComparable<ST::isComparable,Scalar>::checkComparableStdOps(
494  vecSpc,z.ptr(),error_tol(),warning_tol(),inOutArg(out),dumpAll)
495  ) success=false;
496  }
497 
498  // Test Vt_S
499  out << "\n"<<tc<<") Testing Vt_S(z.ptr(),alpha) ...\n";
500  ++tc;
501  {
502 #ifdef THYRA_VECTOR_STD_OPS_TESTER_DUMP
503  RTOpPack::show_spmd_apply_op_dump = true;
504 #endif // THYRA_VECTOR_STD_OPS_TESTER_DUMP
505  v1 = createMember(vecSpc);
506  v2 = createMember(vecSpc);
507  const Scalar alpha = as<Scalar>(1.2345);
508  seed_randomize<Scalar>(12345);
509  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr());
510  V_V(v2.ptr(),*v1);
511  Vt_S(v1.ptr(), alpha);
512  const Scalar norm_alpha_v1 = norm_2(*v1);
513  //out << "norm_alpha_v1 = " << norm_alpha_v1 << "\n";
514  const Scalar mag_alpha = ST::magnitude(alpha);
515  //out << "mag_alpha = " << mag_alpha << "\n";
516  const Scalar norm_2_v2 = norm_2(*v2);
517  //out << "norm_2_v2 = " << norm_2_v2 << "\n";
518  const Scalar alpha_norm_v1 = mag_alpha * norm_2_v2;
519  //out << "alpha_norm_v1 = " << alpha_norm_v1 << "\n";
520  if(!testMaxErr<Scalar>(
521  "norm_alpha_v1 - alpha_norm_v1",ST::magnitude(norm_alpha_v1-alpha_norm_v1)
522  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
523  )
524  ) success=false;
525 #ifdef THYRA_VECTOR_STD_OPS_TESTER_DUMP
526  RTOpPack::show_spmd_apply_op_dump = false;
527 #endif // THYRA_VECTOR_STD_OPS_TESTER_DUMP
528  }
529 
530  // Test V_StV
531  out << "\n"<<tc<<") Testing V_StV(z.ptr(),alpha,*v) ...\n";
532  ++tc;
533  {
534  v1 = createMember(vecSpc);
535  v2 = createMember(vecSpc);
536  z = createMember(vecSpc);
537  const Scalar alpha = as<Scalar>(-1.2345);
538  seed_randomize<Scalar>(12345);
539  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr());
540  V_StV(v2.ptr(),alpha,*v1);
541  Vt_S(v1.ptr(),alpha);
542  V_V(z.ptr(),*v1);
543  Vp_V(z.ptr(),*v2,as<Scalar>(-ST::one()));
544  if(!testMaxErr<Scalar>(
545  "norm_2(*z)",norm_2(*z)
546  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
547  )
548  ) success=false;
549  }
550 
551  // Test Vp_StV
552  out << "\n"<<tc<<") Testing Vp_StV(z.ptr(),alpha,*v) ...\n";
553  ++tc;
554  {
555  v1 = createMember(vecSpc);
556  v2 = createMember(vecSpc);
557  v3 = createMember(vecSpc);
558  z = createMember(vecSpc);
559  const Scalar alpha = as<Scalar>(-1.2345);
560  seed_randomize<Scalar>(12345);
561  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
562  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
563  V_V(v3.ptr(),*v1); // v3 = v1
564  Vp_StV(v1.ptr(),alpha,*v2); // v1 += alpha*v2
565  V_StV(z.ptr(),alpha,*v2); // z = alpha*v2
566  Vp_V(z.ptr(),*v3); // z += v3
567  V_V(v3.ptr(),*v1); // v3 = v1
568  Vp_V(v3.ptr(),*z,as<Scalar>(-ST::one())); // v3 -= z
569  if(!testMaxErr<Scalar>(
570  "norm_2(*v3)",norm_2(*v3)
571  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
572  )
573  ) success=false;
574  }
575 
576  // Test ele_wise_prod
577  out << "\n"<<tc<<") Testing ele_wise_prod(alpha,*v1, *v2, z.ptr()) ...\n";
578  ++tc;
579  {
580  v1 = createMember(vecSpc);
581  v2 = createMember(vecSpc);
582  v3 = createMember(vecSpc);
583  z = createMember(vecSpc);
584  const Scalar alpha = as<Scalar>(-1.2345);
585  seed_randomize<Scalar>(12345);
586  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
587  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
588  randomize(as<Scalar>(-ST::one()),ST::one(),v3.ptr()); // v3 = rand
589  V_V(v4.ptr(), *v1); // v4 = v1
590  V_V(z.ptr(), *v2); // z = v2
591  ele_wise_prod(alpha, *v2, *v3, v1.ptr()); // v1 += alpha * v2 * v3
592  ele_wise_prod_update(alpha, *v3, z.ptr()); // z *= alpha * v3
593  Vp_V(z.ptr(), *v4); // z += v4
594  V_V(v2.ptr(), *v1); // v2 = v1
595  Vp_V(v2.ptr(), *z, as<Scalar>(-ST::one())); // v2 -= z
596  if(!testMaxErr<Scalar>(
597  "norm_2(*v2)",norm_2(*v2)
598  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
599  )
600  ) success=false;
601  }
602 
603 
604  // Test ele_wise_scale
605  out << "\n"<<tc<<") Testing ele_wise_scale(*v1, z.ptr()) ...\n";
606  ++tc;
607  {
608  v1 = createMember(vecSpc);
609  z = createMember(vecSpc);
610  V_S(v1.ptr(), as<Scalar>(2.0));
611  V_S(z.ptr(), as<Scalar>(3.0));
612  ele_wise_scale( *v1, z.ptr() );
613  if (!Teuchos::testRelErr<Scalar>(
614  "norm_2(*z)", norm_2(*z),
615  "ST::squareroot(n*sqr(3.0*2.0))", ST::squareroot(n*36.0),
616  "error_tol", error_tol(),
617  "warning_tol", warning_tol(),
618  inOutArg(out)
619  )
620  ) success=false;
621  }
622 
623  // Test Vt_StV
624  out << "\n"<<tc<<") Testing Vt_StV(z.ptr(), alpha, *v) ...\n";
625  ++tc;
626  {
627  v1 = createMember(vecSpc);
628  v2 = createMember(vecSpc);
629  v3 = createMember(vecSpc);
630  z = createMember(vecSpc);
631  const Scalar alpha = as<Scalar>(-1.2345);
632  seed_randomize<Scalar>(12345);
633  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
634  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
635  V_V(v3.ptr(),*v1); // v3 = v1
636  Vt_StV(v1.ptr(),alpha,*v2); // v1 *= alpha*v2
637  V_S(z.ptr(),ST::zero()); // z = 0
638  Vp_StVtV(z.ptr(),alpha,*v3,*v2); // z += alpha*v3*v2
639  V_V(v2.ptr(),*v1); // v2 = v1
640  Vp_V(v2.ptr(),*z,as<Scalar>(-ST::one())); // v2 -= z
641  if(!testMaxErr<Scalar>(
642  "norm_2(*v2)",norm_2(*v2)
643  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
644  )
645  ) success=false;
646  }
647 
648  // Test V_StVpV
649  out << "\n"<<tc<<") Testing V_StVpV(z.ptr(),alpha,*v1,*v2) ...\n";
650  ++tc;
651  {
652  v1 = createMember(vecSpc);
653  v2 = createMember(vecSpc);
654  v3 = createMember(vecSpc);
655  x = createMember(vecSpc);
656  z = createMember(vecSpc);
657  const Scalar alpha = as<Scalar>(1.2345);
658  seed_randomize<Scalar>(12345);
659  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v1.ptr());
660  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
661  V_StVpV(v3.ptr(),alpha,*v1,*v2);
662  V_V(z.ptr(),*v1);
663  Vp_V(z.ptr(),*v2,alpha);
664  V_V(x.ptr(),*v3);
665  Vp_V(x.ptr(),*z,as<Scalar>(-ST::one()));
666  if(!testMaxErr<Scalar>(
667  "norm_2(*x)",norm_2(*x)
668  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
669  )
670  ) success=false;
671  }
672 
673  // Test V_VpStV
674  out << "\n"<<tc<<") Testing V_VpStV(z.ptr(),*v1,alpha,*v2) ...\n";
675  ++tc;
676  {
677  v1 = createMember(vecSpc);
678  v2 = createMember(vecSpc);
679  v3 = createMember(vecSpc);
680  x = createMember(vecSpc);
681  z = createMember(vecSpc);
682  const Scalar alpha = as<Scalar>(1.2345);
683  seed_randomize<Scalar>(12345);
684  randomize<Scalar>(as<Scalar>(as<Scalar>(-10)*ST::one()),
685  as<Scalar>(as<Scalar>(10)*ST::one()), v1.ptr());
686  randomize<Scalar>(as<Scalar>(as<Scalar>(-10)*ST::one()),
687  as<Scalar>(as<Scalar>(10)*ST::one()), v2.ptr());
688  V_VpStV(outArg(*v3), *v1, alpha, *v2);
689  V_V(z.ptr(), *v1);
690  Vp_StV(z.ptr(), alpha, *v2);
691  V_VmV(outArg(*x), *z, *v3);
692  if(!testMaxErr<Scalar>(
693  "norm_2(*x)",norm_2(*x)
694  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
695  )
696  ) success=false;
697  }
698 
699  // Test V_StVpStV
700  out << "\n"<<tc<<") Testing V_StVpStV(z.ptr(),alpha,*v1,beta,*v2) ...\n";
701  ++tc;
702  {
703  v1 = createMember(vecSpc);
704  v2 = createMember(vecSpc);
705  v3 = createMember(vecSpc);
706  x = createMember(vecSpc);
707  z = createMember(vecSpc);
708  const Scalar alpha = as<Scalar>(1.2345);
709  const Scalar beta = as<Scalar>(5.4321);
710  seed_randomize<Scalar>(12345);
711  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v1.ptr());
712  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
713  V_StVpStV(v3.ptr(),alpha,*v1,beta,*v2);
714  V_StV(z.ptr(),alpha,*v1);
715  Vp_StV(z.ptr(),beta,*v2);
716  V_V(x.ptr(),*v3);
717  Vp_V(x.ptr(),*z,as<Scalar>(-ST::one()));
718  if(!testMaxErr<Scalar>(
719  "norm_2(*x)",norm_2(*x)
720  ,"10*error_tol",ScalarMag(ScalarMag(10)*error_tol()),"warning_tol",warning_tol(),&out
721  )
722  ) success=false;
723  }
724 
725  // Test Vp_V
726  out << "\n"<<tc<<") Testing Vp_V(v1.ptr(),*v2,beta) ...\n";
727  ++tc;
728  {
729  v1 = createMember(vecSpc);
730  v2 = createMember(vecSpc);
731  v3 = createMember(vecSpc);
732  x = createMember(vecSpc);
733  z = createMember(vecSpc);
734  const Scalar alpha = as<Scalar>(-2.0);
735  const Scalar beta = as<Scalar>(10.0);
736  V_S(v1.ptr(),alpha);
737  seed_randomize<Scalar>(12345);
738  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
739  Vp_V(v1.ptr(),*v2,beta);
740  V_S(v3.ptr(),alpha);
741  V_StVpV(z.ptr(),beta,*v3,*v2);
742  V_StVpV(x.ptr(),as<Scalar>(-ST::one()),*z,*v1);
743  if(!testMaxErr<Scalar>(
744  "norm_2(*x)",norm_2(*x)
745  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
746  )
747  ) success=false;
748  }
749 
750  // Test Vp_V
751  out << "\n"<<tc<<") Testing Vp_V(v1.ptr(),*v2) ...\n";
752  ++tc;
753  {
754  v1 = createMember(vecSpc);
755  v2 = createMember(vecSpc);
756  v3 = createMember(vecSpc);
757  x = createMember(vecSpc);
758  z = createMember(vecSpc);
759  const Scalar alpha = as<Scalar>(-2.0);
760  V_S(v1.ptr(),alpha);
761  seed_randomize<Scalar>(12345);
762  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
763  Vp_V(v1.ptr(),*v2);
764  V_S(v3.ptr(),alpha);
765  V_StVpV(z.ptr(),ST::one(),*v3,*v2);
766  V_StVpV(x.ptr(),as<Scalar>(-ST::one()),*z,*v1);
767  if(!testMaxErr<Scalar>(
768  "norm_2(*x)",norm_2(*x)
769  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
770  )
771  ) success=false;
772  }
773 
774  // Test V_S
775  out << "\n"<<tc<<") Testing V_S(v1.ptr(),alpha) ...\n";
776  ++tc;
777  {
778  v1 = createMember(vecSpc);
779  v2 = createMember(vecSpc);
780  z = createMember(vecSpc);
781  const Scalar alpha = as<Scalar>(1.2345);
782  assign(v1.ptr(),alpha);
783  V_S(v2.ptr(),alpha);
784  V_StVpV(z.ptr(),as<Scalar>(-ST::one()),*v1,*v2);
785  if(!testMaxErr<Scalar>(
786  "norm_2(*z)",norm_2(*z)
787  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
788  )
789  ) success=false;
790  }
791 
792  // Test V_V
793  out << "\n"<<tc<<") Testing V_V(v1.ptr(),*v2) ...\n";
794  ++tc;
795  {
796  v1 = createMember(vecSpc);
797  v2 = createMember(vecSpc);
798  z = createMember(vecSpc);
799  seed_randomize<Scalar>(12345);
800  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v1.ptr());
801  V_V(v2.ptr(),*v1);
802  V_StVpV(z.ptr(),as<Scalar>(-ST::one()),*v1,*v2);
803  if(!testMaxErr<Scalar>(
804  "norm_2(*z)",norm_2(*z)
805  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
806  )
807  ) success=false;
808  }
809 
810  // ToDo: Add tests for *all* standard operators!
811 
812  out << "\n*** Leaving VectorStdOpsTester<"<<ST::name()<<">::checkStdOps(...) ...\n";
813 
814  return success;
815 
816 
817 }
818 
819 
820 } // namespace Thyra
821 
822 
823 #endif // THYRA_VECTOR_STD_OPS_TESTER_HPP
VectorStdOpsTester(const ScalarMag &warning_tol=0, const ScalarMag &error_tol=0)
const std::string passfail(const bool result)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Abstract interface for objects that represent a space for vectors.
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
#define TEUCHOS_TEST_ASSERT(v1, out, success)
Ptr< T > ptr() const
virtual std::string description() const
Abstract interface for finite-dimensional dense vectors.
Teuchos::ScalarTraits< Scalar >::magnitudeType ScalarMag
bool nonnull(const boost::shared_ptr< T > &p)
TypeTo as(const TypeFrom &t)
bool checkStdOps(const VectorSpaceBase< Scalar > &vecSpc, std::ostream *out=0, const bool &dumpAll=false)
Run the tests using a vector space.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_EQUALITY_CONST(v1, v2, out, success)
#define TEUCHOS_TEST_THROW(code, ExceptType, out, success)
virtual Ordinal dim() const =0
Return the dimension of the vector space.