RTOp Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOpPack_TOpRandomize.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
4 // Operations
5 //
6 // Copyright 2006 NTESS and the RTOp contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #ifndef RTOPPACK_TOP_RANDOMIZE_HPP
12 #define RTOPPACK_TOP_RANDOMIZE_HPP
13 
14 #include "RTOpPack_RTOpTHelpers.hpp"
15 
16 
17 namespace RTOpPack {
18 
19 
32 template<class Scalar>
33 class TOpRandomize : public RTOpT<Scalar> {
34 public:
37  static void set_static_seed( const unsigned int static_seed )
38  { static_seed_ = static_seed; }
40  static unsigned int get_static_seed() { return static_seed_; }
43  const Scalar& l = -ScalarTraits<Scalar>::one(),
44  const Scalar& u = +ScalarTraits<Scalar>::one()
45  )
46  {
47  this->setOpNameBase("TOpRandomize");
48  set_bounds(l, u);
50  ++static_seed_; // By default we will just increment the seed!
51  }
53  void set_bounds( const Scalar& l, const Scalar& u )
54  { l_ = l; u_ = u; }
56  void set_seed( const unsigned int seed ) { seed_ = seed; }
58  unsigned int get_seed() const { return seed_; }
63  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
64  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
65  const Ptr<ReductTarget> &reduct_obj_inout
66  ) const
67  {
68  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
69 
70 #ifdef TEUCHOS_DEBUG
71  validate_apply_op<Scalar>(*this, 0, 1, false,
72  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
73 #else
74  (void)sub_vecs;
75  (void)reduct_obj_inout;
76 #endif
77 
78  const index_type subDim = targ_sub_vecs[0].subDim();
79  const index_type globalOffset = targ_sub_vecs[0].globalOffset();
80 
81  iter_t z0_val = targ_sub_vecs[0].values().begin();
82  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
83 
84  // Linear coefficients for translating from [-1,+1] to [l,b]
85  const Scalar a = Scalar(0.5)*(u_ - l_);
86  const Scalar b = Scalar(0.5)*(u_ + l_);
87  for( index_type i = 0; i < subDim; ++i, z0_val += z0_s )
88  {
90  *z0_val = a * Teuchos::ScalarTraits<Scalar>::random() + b;
91  // Above should be in the range [l,b]
92  }
93  }
95 private:
96  static unsigned int static_seed_;
97  unsigned int seed_;
98  Scalar l_;
99  Scalar u_;
100 };
101 
102 
103 template<class Scalar>
104 unsigned int TOpRandomize<Scalar>::static_seed_ = 0;
105 
106 
107 } // namespace RTOpPack
108 
109 
110 #endif // RTOPPACK_TOP_RANDOMIZE_HPP
Class for a changeable sub-vector.
void set_bounds(const Scalar &l, const Scalar &u)
unsigned int get_seed() const
static unsigned int static_seed_
Teuchos_Ordinal index_type
Class for a non-changeable sub-vector.
Templated interface to vector reduction/transformation operators {abstract}.
Generate a random vector in the range [l,u]: z0[i] = 0.5*((u-l)*Teuchos::ScalarTraits&lt;Scalar&gt;::random...
TOpRandomize(const Scalar &l=-ScalarTraits< Scalar >::one(), const Scalar &u=+ScalarTraits< Scalar >::one())
static void seedrandom(unsigned int s)
void setOpNameBase(const std::string &op_name_base)
Just set the operator name.
static void set_static_seed(const unsigned int static_seed)
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
Ptr< const T > getConst() const
void set_seed(const unsigned int seed)
static unsigned int get_static_seed()