47 #ifndef ROL_SIMULATED_VECTOR_H 48 #define ROL_SIMULATED_VECTOR_H 71 typedef ROL::Ptr<V>
Vp;
72 typedef ROL::Ptr<BatchManager<Real> >
VBMp;
77 ROL::Ptr<BatchManager<Real> >
bman_;
84 SimulatedVector(
const std::vector<Vp> &vecs,
const VBMp &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 ) {
92 const PV &xs =
dynamic_cast<const PV&
>(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));
105 const PV &xs =
dynamic_cast<const PV&
>(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 ) {
124 const PV &xs =
dynamic_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 {
137 const PV &xs =
dynamic_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<Vp> clonevec;
163 for( size_type i=0; i<vecs_.size(); ++i ) {
164 clonevec.push_back(vecs_[i]->
clone());
166 return ROL::makePtr<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());
181 ROL_TEST_FOR_EXCEPTION( i >=
dimension() || i<0,
182 std::invalid_argument,
183 "Error: Basis index must be between 0 and vector dimension." );
187 PV &eb =
dynamic_cast<PV&
>(*bvec);
193 for( size_type j=0; j<vecs_.size(); ++j ) {
195 end += vecs_[j]->dimension();
197 if( begin<= i && i<end ) {
198 eb.
set(j, *(vecs_[j]->
basis(i-begin)) );
212 for( size_type j=0; j<vecs_.size(); ++j ) {
213 total_dim += vecs_[j]->dimension();
219 for( size_type j=0; j<vecs_.size(); ++j ) {
225 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
226 for( size_type i=0; i<vecs_.size(); ++i ) {
227 vecs_[i]->applyUnary(f);
232 void applyBinary(
const Elementwise::BinaryFunction<Real> &f,
const V &x ) {
233 const PV &xs =
dynamic_cast<const PV&
>(x);
235 for( size_type i=0; i<vecs_.size(); ++i ) {
236 vecs_[i]->applyBinary(f,*xs.
get(i));
240 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
241 Real result = r.initialValue();
243 for( size_type i=0; i<vecs_.size(); ++i ) {
244 r.reduce(vecs_[i]->
reduce(r),result);
250 for( size_type i=0; i<vecs_.size(); ++i ) {
251 vecs_[i]->setScalar(C);
256 for( size_type i=0; i<vecs_.size(); ++i ) {
257 vecs_[i]->randomize(l,u);
265 ROL::Ptr<const Vector<Real> >
get(size_type i)
const {
269 ROL::Ptr<Vector<Real> >
get(size_type i) {
273 void set(size_type i,
const V &x) {
289 ROL::Ptr<Vector<Real> >
293 typedef ROL::Ptr<Vector<Real> >
Vp;
297 return ROL::makePtr<PV>(std::vector<Vp>(temp, temp+1), bman );
303 const std::vector<ROL::Ptr<Vector<Real>>>
vecs_;
304 const ROL::Ptr<BatchManager<Real>>
bman_;
314 :
SimulatedVector<Real>(vecs,bman), vecs_(vecs), bman_(bman), sampler_(sampler),
315 isDualInitialized_(false) {
316 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
317 dual_vecs_.push_back((vecs_[i]->
dual()).
clone());
325 ROL_TEST_FOR_EXCEPTION( sampler_->numMySamples() !=
static_cast<int>(xs.
numVectors()),
326 std::invalid_argument,
327 "Error: Vectors must have the same number of subvectors." );
332 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
334 Real y = sampler_->getMyWeight(i) * vecs_[i]->dot(*xs.
get(i)) - c;
335 Real t = locresult + y;
336 c = (t - locresult) - y;
340 bman_->sumAll(&locresult, &result, 1);
345 ROL::Ptr<Vector<Real> >
clone(
void)
const {
346 std::vector<ROL::Ptr<Vector<Real> > > clonevec;
347 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
348 clonevec.push_back(vecs_[i]->
clone());
350 return ROL::makePtr<PrimalSimulatedVector<Real>>(clonevec,
bman_, sampler_);
354 if (!isDualInitialized_) {
355 dual_pvec_ = ROL::makePtr<DualSimulatedVector<Real>>(
dual_vecs_,
bman_, sampler_);
356 isDualInitialized_ =
true;
358 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
359 dual_vecs_[i]->set(vecs_[i]->
dual());
360 dual_vecs_[i]->scale(sampler_->getMyWeight(i));
370 const std::vector<ROL::Ptr<Vector<Real> > >
vecs_;
371 const ROL::Ptr<BatchManager<Real> >
bman_;
381 :
SimulatedVector<Real>(vecs,bman), vecs_(vecs), bman_(bman), sampler_(sampler),
382 isPrimalInitialized_(false) {
383 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
384 primal_vecs_.push_back((vecs_[i]->
dual()).
clone());
392 ROL_TEST_FOR_EXCEPTION( sampler_->numMySamples() !=
static_cast<Real
>(xs.
numVectors()),
393 std::invalid_argument,
394 "Error: Vectors must have the same number of subvectors." );
399 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
401 Real y = vecs_[i]->dot(*xs.
get(i)) / sampler_->getMyWeight(i) - c;
402 Real t = locresult + y;
403 c = (t - locresult) - y;
407 bman_->sumAll(&locresult, &result, 1);
412 ROL::Ptr<Vector<Real> >
clone(
void)
const {
413 std::vector<ROL::Ptr<Vector<Real> > > clonevec;
414 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
415 clonevec.push_back(vecs_[i]->
clone());
417 return ROL::makePtr<DualSimulatedVector<Real>>(clonevec,
bman_, sampler_);
421 if (!isPrimalInitialized_) {
422 primal_pvec_ = ROL::makePtr<PrimalSimulatedVector<Real>>(primal_vecs_,
bman_, sampler_);
423 isPrimalInitialized_ =
true;
426 for(
int i=0; i<sampler_->numMySamples(); ++i ) {
427 primal_vecs_[i]->set(vecs_[i]->
dual());
428 primal_vecs_[i]->scale(one/sampler_->getMyWeight(i));
430 return *primal_pvec_;
436 ROL::Ptr<const Vector<Real> >
440 typedef ROL::Ptr<const Vector<Real> >
Vp;
444 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1), bman );
448 ROL::Ptr<Vector<Real> >
453 typedef ROL::Ptr<Vector<Real> >
Vp;
457 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2), bman );
461 ROL::Ptr<const Vector<Real> >
467 typedef ROL::Ptr<const Vector<Real> >
Vp;
471 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2), bman );
475 ROL::Ptr<Vector<Real> >
482 typedef ROL::Ptr<Vector<Real> >
Vp;
486 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3), bman );
490 ROL::Ptr<const Vector<Real> >
497 typedef ROL::Ptr<const Vector<Real> >
Vp;
501 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3), bman );
505 ROL::Ptr<Vector<Real> >
513 typedef ROL::Ptr<Vector<Real> >
Vp;
516 Vp temp[] = {a,b,c,d};
517 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4), bman );
521 ROL::Ptr<const Vector<Real> >
529 typedef ROL::Ptr<const Vector<Real> >
Vp;
532 Vp temp[] = {a,b,c,d};
533 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4), bman );
538 #endif // ROL_SIMULATED_VECTOR_H
ROL::Ptr< BatchManager< Real > > VBMp
typename PV< Real >::size_type size_type
PrimalSimulatedVector(const std::vector< ROL::Ptr< Vector< Real >>> &vecs, const ROL::Ptr< BatchManager< Real >> &bman, const ROL::Ptr< SampleGenerator< Real >> &sampler)
void set(const V &x)
Set where .
const ROL::Ptr< SampleGenerator< Real > > sampler_
DualSimulatedVector(const std::vector< ROL::Ptr< Vector< Real > > > &vecs, const ROL::Ptr< BatchManager< Real > > &bman, const ROL::Ptr< SampleGenerator< Real > > &sampler)
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
ROL::Ptr< PV > dual_pvec_
Real dot(const Vector< Real > &x) const
Compute where .
void setScalar(const Real C)
Set where .
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...
ROL::Ptr< BatchManager< Real > > bman_
const ROL::Ptr< BatchManager< Real > > bman_
Defines the linear algebra or vector space interface.
const ROL::Ptr< BatchManager< Real > > bman_
SimulatedVector(const std::vector< Vp > &vecs, const VBMp &bman)
Real norm() const
Returns where .
Defines the linear algebra of a vector space on a generic partitioned vector where the individual vec...
ROL::Ptr< Vector< Real > > CreateSimulatedVector(const ROL::Ptr< Vector< Real >> &a, const ROL::Ptr< BatchManager< Real >> &bman)
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
const std::vector< ROL::Ptr< Vector< Real > > > vecs_
virtual Real dot(const V &x) const
Compute where .
int dimension() const
Return dimension of the vector space.
std::vector< ROL::Ptr< Vector< Real > > > dual_vecs_
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
void zero()
Set to zero vector.
const ROL::Ptr< SampleGenerator< Real > > sampler_
void plus(const V &x)
Compute , where .
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 .
Vp basis(const int i) const
Return i-th basis vector.
const std::vector< Vp > vecs_
void axpy(const Real alpha, const V &x)
Compute where .
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
std::vector< Vp > dual_vecs_
Real dot(const Vector< Real > &x) const
Compute where .
std::vector< ROL::Ptr< Vector< Real > > > primal_vecs_
ROL::Ptr< DualSimulatedVector< Real > > dual_pvec_
bool isPrimalInitialized_
virtual void set(const Vector &x)
Set where .
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
virtual Vp clone() const
Clone to make a new (uninitialized) vector.
ROL::Ptr< PrimalSimulatedVector< Real > > primal_pvec_
ROL::Ptr< const Vector< Real > > get(size_type i) const
const std::vector< ROL::Ptr< Vector< Real > > > vecs_