47 #ifndef ROL_SIMULATED_VECTOR_H 48 #define ROL_SIMULATED_VECTOR_H 71 typedef Teuchos::RCP<V>
RCPV;
72 typedef Teuchos::RCP<BatchManager<Real> >
RCPBM;
77 Teuchos::RCP<BatchManager<Real> >
bman_;
82 typedef typename std::vector<PV>::size_type
size_type;
84 SimulatedVector(
const std::vector<RCPV> &vecs,
const RCPBM &bman ) : vecs_(vecs), bman_(bman) {
85 for( size_type i=0; i<vecs_.size(); ++i ) {
86 dual_vecs_.push_back((vecs_[i]->
dual()).
clone());
90 void set(
const V &x ) {
91 using Teuchos::dyn_cast;
92 const PV &xs = dyn_cast<
const PV>(dyn_cast<
const V>(x));
95 std::invalid_argument,
96 "Error: Vectors must have the same number of subvectors." );
98 for( size_type i=0; i<vecs_.size(); ++i ) {
99 vecs_[i]->set(*xs.
get(i));
104 using Teuchos::dyn_cast;
105 const PV &xs = dyn_cast<
const PV>(dyn_cast<
const V>(x));
108 std::invalid_argument,
109 "Error: Vectors must have the same number of subvectors." );
111 for( size_type i=0; i<vecs_.size(); ++i ) {
112 vecs_[i]->plus(*xs.
get(i));
117 for( size_type i=0; i<vecs_.size(); ++i ) {
118 vecs_[i]->scale(alpha);
122 void axpy(
const Real alpha,
const V &x ) {
123 using Teuchos::dyn_cast;
124 const PV &xs = dyn_cast<
const PV>(x);
127 std::invalid_argument,
128 "Error: Vectors must have the same number of subvectors." );
130 for( size_type i=0; i<vecs_.size(); ++i ) {
131 vecs_[i]->axpy(alpha,*xs.
get(i));
135 virtual Real
dot(
const V &x )
const {
136 using Teuchos::dyn_cast;
137 const PV &xs = dyn_cast<
const PV>(x);
140 std::invalid_argument,
141 "Error: Vectors must have the same number of subvectors." );
145 for( size_type i=0; i<vecs_.size(); ++i ) {
146 locresult += vecs_[i]->dot(*xs.
get(i));
149 bman_->sumAll(&locresult, &result, 1);
155 return std::sqrt(
dot(*
this));
162 std::vector<RCPV> clonevec;
163 for( size_type i=0; i<vecs_.size(); ++i ) {
164 clonevec.push_back(vecs_[i]->
clone());
166 return rcp(
new PV(clonevec, bman_) );
169 virtual const V&
dual(
void)
const {
172 for( size_type i=0; i<vecs_.size(); ++i ) {
173 dual_vecs_[i]->
set(vecs_[i]->
dual());
175 dual_pvec_ = rcp(
new PV( dual_vecs_, bman_ ) );
181 TEUCHOS_TEST_FOR_EXCEPTION( i >=
dimension() || i<0,
182 std::invalid_argument,
183 "Error: Basis index must be between 0 and vector dimension." );
187 using Teuchos::dyn_cast;
192 PV &eb = dyn_cast<PV>(*bvec);
198 for( size_type j=0; j<vecs_.size(); ++j ) {
200 end += vecs_[j]->dimension();
202 if( begin<= i && i<end ) {
203 eb.
set(j, *(vecs_[j]->
basis(i-begin)) );
217 for( size_type j=0; j<vecs_.size(); ++j ) {
218 total_dim += vecs_[j]->dimension();
224 for( size_type j=0; j<vecs_.size(); ++j ) {
230 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
231 for( size_type i=0; i<vecs_.size(); ++i ) {
232 vecs_[i]->applyUnary(f);
237 void applyBinary(
const Elementwise::BinaryFunction<Real> &f,
const V &x ) {
238 const PV &xs = Teuchos::dyn_cast<
const PV>(x);
240 for( size_type i=0; i<vecs_.size(); ++i ) {
241 vecs_[i]->applyBinary(f,*xs.
get(i));
245 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
246 Real result = r.initialValue();
248 for( size_type i=0; i<vecs_.size(); ++i ) {
249 r.reduce(vecs_[i]->
reduce(r),result);
258 Teuchos::RCP<const Vector<Real> >
get(size_type i)
const {
262 Teuchos::RCP<Vector<Real> >
get(size_type i) {
266 void set(size_type i,
const V &x) {
285 typedef RCP<Vector<Real> >
RCPV;
289 return rcp(
new PV( std::vector<RCPV>(temp, temp+1), bman ) );
295 const std::vector<Teuchos::RCP<Vector<Real> > >
vecs_;
296 const Teuchos::RCP<BatchManager<Real> >
bman_;
297 const Teuchos::RCP<SampleGenerator<Real> >
sampler_;
298 mutable std::vector<Teuchos::RCP<Vector<Real> > >
dual_vecs_;
306 :
SimulatedVector<Real>(vecs,bman), vecs_(vecs), bman_(bman), sampler_(sampler),
307 isDualInitialized_(false) {
308 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
309 dual_vecs_.push_back((vecs_[i]->
dual()).
clone());
317 TEUCHOS_TEST_FOR_EXCEPTION( sampler_->numMySamples() !=
static_cast<int>(xs.
numVectors()),
318 std::invalid_argument,
319 "Error: Vectors must have the same number of subvectors." );
324 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
326 Real y = sampler_->getMyWeight(i) * vecs_[i]->dot(*xs.
get(i)) - c;
327 Real t = locresult + y;
328 c = (t - locresult) - y;
332 bman_->sumAll(&locresult, &result, 1);
337 Teuchos::RCP<Vector<Real> >
clone(
void)
const {
338 std::vector<Teuchos::RCP<Vector<Real> > > clonevec;
339 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
340 clonevec.push_back(vecs_[i]->
clone());
346 if (!isDualInitialized_) {
348 isDualInitialized_ =
true;
350 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
351 dual_vecs_[i]->set(vecs_[i]->
dual());
352 dual_vecs_[i]->scale(sampler_->getMyWeight(i));
362 const std::vector<Teuchos::RCP<Vector<Real> > >
vecs_;
363 const Teuchos::RCP<BatchManager<Real> >
bman_;
364 const Teuchos::RCP<SampleGenerator<Real> >
sampler_;
373 :
SimulatedVector<Real>(vecs,bman), vecs_(vecs), bman_(bman), sampler_(sampler),
374 isPrimalInitialized_(false) {
375 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
376 primal_vecs_.push_back((vecs_[i]->
dual()).
clone());
384 TEUCHOS_TEST_FOR_EXCEPTION( sampler_->numMySamples() !=
static_cast<Real
>(xs.
numVectors()),
385 std::invalid_argument,
386 "Error: Vectors must have the same number of subvectors." );
391 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
393 Real y = vecs_[i]->dot(*xs.
get(i)) / sampler_->getMyWeight(i) - c;
394 Real t = locresult + y;
395 c = (t - locresult) - y;
399 bman_->sumAll(&locresult, &result, 1);
404 Teuchos::RCP<Vector<Real> >
clone(
void)
const {
405 std::vector<Teuchos::RCP<Vector<Real> > > clonevec;
406 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
407 clonevec.push_back(vecs_[i]->
clone());
413 if (!isPrimalInitialized_) {
415 isPrimalInitialized_ =
true;
418 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
419 primal_vecs_[i]->set(vecs_[i]->
dual());
420 primal_vecs_[i]->scale(one/sampler_->getMyWeight(i));
422 return *primal_pvec_;
431 typedef RCP<const Vector<Real> >
RCPV;
435 return rcp(
new PV( std::vector<RCPV>(temp, temp+1), bman ) );
444 typedef RCP<Vector<Real> >
RCPV;
448 return rcp(
new PV( std::vector<RCPV>(temp, temp+2), bman ) );
457 typedef RCP<const Vector<Real> >
RCPV;
461 return rcp(
new PV( std::vector<RCPV>(temp, temp+2), bman ) );
471 typedef RCP<Vector<Real> >
RCPV;
474 RCPV temp[] = {a,b,c};
475 return rcp(
new PV( std::vector<RCPV>(temp, temp+3), bman ) );
485 typedef RCP<const Vector<Real> >
RCPV;
488 RCPV temp[] = {a,b,c};
489 return rcp(
new PV( std::vector<RCPV>(temp, temp+3), bman ) );
500 typedef RCP<Vector<Real> >
RCPV;
503 RCPV temp[] = {a,b,c,d};
504 return rcp(
new PV( std::vector<RCPV>(temp, temp+4), bman ) );
515 typedef RCP<const Vector<Real> >
RCPV;
518 RCPV temp[] = {a,b,c,d};
519 return rcp(
new PV( std::vector<RCPV>(temp, temp+4), bman ) );
524 #endif // ROL_SIMULATED_VECTOR_H const Teuchos::RCP< BatchManager< Real > > bman_
const std::vector< Teuchos::RCP< Vector< Real > > > vecs_
void set(const V &x)
Set where .
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
std::vector< PV >::size_type size_type
size_type numVectors() const
Teuchos::RCP< DualSimulatedVector< Real > > dual_pvec_
Teuchos::RCP< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
std::vector< RCPV > dual_vecs_
Teuchos::RCP< Vector< Real > > CreateSimulatedVector(const Teuchos::RCP< Vector< Real > > &a, const Teuchos::RCP< BatchManager< Real > > &bman)
PrimalSimulatedVector(const std::vector< Teuchos::RCP< Vector< Real > > > &vecs, const Teuchos::RCP< BatchManager< Real > > &bman, const Teuchos::RCP< SampleGenerator< Real > > &sampler)
Real dot(const Vector< Real > &x) const
Compute where .
virtual RCPV clone() const
Clone to make a new (uninitialized) vector.
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const V &x)
virtual const V & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Defines the linear algebra or vector space interface.
const Teuchos::RCP< BatchManager< Real > > bman_
Real norm() const
Returns where .
Defines the linear algebra of a vector space on a generic partitioned vector where the individual vec...
SimulatedVector(const std::vector< RCPV > &vecs, const RCPBM &bman)
Teuchos::RCP< PV > dual_pvec_
const std::vector< RCPV > vecs_
Teuchos::RCP< BatchManager< Real > > bman_
Teuchos::RCP< PrimalSimulatedVector< Real > > primal_pvec_
const Teuchos::RCP< SampleGenerator< Real > > sampler_
std::vector< Teuchos::RCP< Vector< Real > > > primal_vecs_
std::vector< Teuchos::RCP< Vector< Real > > > dual_vecs_
virtual Real dot(const V &x) const
Compute where .
int dimension() const
Return dimension of the vector space.
const Teuchos::RCP< SampleGenerator< Real > > sampler_
RCPV basis(const int i) const
Return i-th basis vector.
void zero()
Set to zero vector.
const std::vector< Teuchos::RCP< Vector< Real > > > vecs_
void plus(const V &x)
Compute , where .
Teuchos::RCP< BatchManager< Real > > RCPBM
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
SimulatedVector< Real > PV
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void scale(const Real alpha)
Compute where .
Teuchos::RCP< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
void axpy(const Real alpha, const V &x)
Compute where .
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Real dot(const Vector< Real > &x) const
Compute where .
bool isPrimalInitialized_
virtual void set(const Vector &x)
Set where .
DualSimulatedVector(const std::vector< Teuchos::RCP< Vector< Real > > > &vecs, const Teuchos::RCP< BatchManager< Real > > &bman, const Teuchos::RCP< SampleGenerator< Real > > &sampler)
Teuchos::RCP< const Vector< Real > > get(size_type i) const