12 #include <initializer_list>
14 #ifndef ROL_PARTITIONED_VECTOR_H
15 #define ROL_PARTITIONED_VECTOR_H
29 typedef ROL::Ptr<V>
Vp;
47 const PV &xs =
dynamic_cast<const PV&
>(x);
49 std::invalid_argument,
50 "Error: Vectors must have the same number of subvectors." );
57 const PV &xs =
dynamic_cast<const PV&
>(x);
59 std::invalid_argument,
60 "Error: Vectors must have the same number of subvectors." );
66 void scale(
const Real alpha ) {
68 vecs_[i]->scale(alpha);
72 void axpy(
const Real alpha,
const V &x ) {
73 const PV &xs =
dynamic_cast<const PV&
>(x);
75 std::invalid_argument,
76 "Error: Vectors must have the same number of subvectors." );
83 Real
dot(
const V &x )
const {
84 const PV &xs =
dynamic_cast<const PV&
>(x);
86 std::invalid_argument,
87 "Error: Vectors must have the same number of subvectors." );
100 return std::sqrt(result);
104 std::vector<Vp> clonevec;
108 return ROL::makePtr<PV>(clonevec);
120 const PV &xs =
dynamic_cast<const PV&
>(x);
122 std::invalid_argument,
123 "Error: Vectors must have the same number of subvectors." );
126 result +=
vecs_[i]->apply(*xs.
get(i));
132 ROL_TEST_FOR_EXCEPTION( i >=
dimension() || i<0,
133 std::invalid_argument,
134 "Error: Basis index must be between 0 and vector dimension." );
137 PV &eb =
dynamic_cast<PV&
>(*bvec);
140 int begin = 0, end = 0;
142 end +=
vecs_[j]->dimension();
143 if( begin<= i && i<end ) {
157 total_dim +=
vecs_[j]->dimension();
169 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
171 vecs_[i]->applyUnary(f);
176 void applyBinary(
const Elementwise::BinaryFunction<Real> &f,
const V &x ) {
177 const PV &xs =
dynamic_cast<const PV&
>(x);
180 vecs_[i]->applyBinary(f,*xs.
get(i));
184 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
185 Real result = r.initialValue();
195 vecs_[i]->setScalar(C);
199 void randomize(
const Real l = 0.0,
const Real u = 1.0 ) {
201 vecs_[i]->randomize(l,u);
205 void print( std::ostream &outStream )
const {
207 outStream <<
"V[" << i <<
"]: ";
208 vecs_[i]->print(outStream);
244 static Ptr<PartitionedVector>
create( std::initializer_list<Vp> vs ) {
245 std::vector<Vp> subvecs{vs};
246 return ROL::makePtr<PartitionedVector>( subvecs );
251 std::vector<Vp> subvecs(N);
253 return ROL::makePtr<PartitionedVector>( subvecs );
260 template<
typename Real>
265 template<
typename Real>
270 template<
typename Real>
272 return staticPtrCast<PartitionedVector<Real>>(x);
275 template<
typename Real>
277 return staticPtrCast<const PartitionedVector<Real>>(x);
283 ROL::Ptr<Vector<Real>>
286 using Vp = ROL::Ptr<Vector<Real>>;
290 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1) );
294 ROL::Ptr<const Vector<Real> >
297 using Vp = ROL::Ptr<const Vector<Real>>;
301 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+1) );
305 ROL::Ptr<Vector<Real>>
308 using Vp = ROL::Ptr<Vector<Real>>;
312 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2) );
316 ROL::Ptr<const Vector<Real> >
319 using Vp = ROL::Ptr<const Vector<Real>>;
323 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+2) );
328 ROL::Ptr<Vector<Real>>
333 using Vp = ROL::Ptr<Vector<Real>>;
337 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3) );
341 ROL::Ptr<const Vector<Real> >
346 using Vp = ROL::Ptr<const Vector<Real>>;
350 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+3) );
354 ROL::Ptr<Vector<Real> >
361 typedef ROL::Ptr<Vector<Real> > Vp;
364 Vp temp[] = {a,b,c,d};
365 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4) );
369 ROL::Ptr<const Vector<Real> >
376 using Vp = ROL::Ptr<const Vector<Real>>;
379 Vp temp[] = {a,b,c,d};
380 return ROL::makePtr<PV>( std::vector<Vp>(temp, temp+4) );
385 #endif // ROL_PARTITIONED_VECTOR_H
PartitionedVector< Real > & partition(Vector< Real > &x)
PartitionedVector(const std::vector< Vp > &vecs)
static Ptr< PartitionedVector > create(std::initializer_list< Vp > vs)
PartitionedVector< Real > PV
typename PV< Real >::size_type size_type
size_type numVectors() const
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Ptr< const Vector< Real > > get(size_type i) const
ROL::Ptr< PV > dual_pvec_
Real norm() const
Returns where .
Defines the linear algebra of vector space on a generic partitioned vector.
ROL::Ptr< Vector< Real > > CreatePartitionedVector(const ROL::Ptr< Vector< Real >> &a)
const V & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Vector< Real > & operator[](size_type i)
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const V &x)
void print(std::ostream &outStream) const
static Ptr< PartitionedVector > create(const V &x, size_type N)
Vp clone() const
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface.
Real dot(const V &x) const
Compute where .
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
void zero()
Set to zero vector.
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
void setScalar(const Real C)
Set where .
PartitionedVector< Real > PV
void set(size_type i, const V &x)
void scale(const Real alpha)
Compute where .
int dimension() const
Return dimension of the vector space.
void set(const V &x)
Set where .
Vp basis(const int i) const
Return i-th basis vector.
std::vector< Vp > dual_vecs_
const Vector< Real > & operator[](size_type i) const
std::vector< PV >::size_type size_type
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
const std::vector< Vp > vecs_
void plus(const V &x)
Compute , where .
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void axpy(const Real alpha, const V &x)
Compute where .