44 #ifndef ROL_STDVECTOR_H
45 #define ROL_STDVECTOR_H
50 #include <initializer_list>
60 template <
class Real,
class Element=Real>
74 std_vec_( makePtr<std::vector<Element>>(ilist) ) {}
82 std::invalid_argument,
83 "Error: Vectors must have the same dimension." );
86 const std::vector<Element>& xval = *ex.
getVector();
87 std::copy(xval.begin(),xval.end(),
std_vec_->begin());
93 std::invalid_argument,
94 "Error: Vectors must have the same dimension." );
97 const std::vector<Element>& xval = *ex.
getVector();
100 (*std_vec_)[i] += xval[i];
107 std::invalid_argument,
108 "Error: Vectors must have the same dimension." );
111 const std::vector<Element>& xval = *ex.
getVector();
114 (*std_vec_)[i] += alpha*xval[i];
119 for(
auto& e : *
std_vec_ ) e *= alpha;
129 std::invalid_argument,
130 "Error: Vectors must have the same dimension." );
133 const std::vector<Element>& xval = *ex.
getVector();
140 return std::inner_product(
std_vec_->begin(),
std_vec_->end(), xval.begin(), Real(0));
145 val = std::sqrt(
dot(*
this) );
149 virtual Ptr<Vector<Real> >
clone()
const {
150 return makePtr<StdVector>( makePtr<std::vector<Element>>(
std_vec_->size(),
static_cast<Element
>(0)));
161 Ptr<Vector<Real> >
basis(
const int i )
const {
163 ROL_TEST_FOR_EXCEPTION( i >=
dimension() || i<0,
164 std::invalid_argument,
165 "Error: Basis index must be between 0 and vector dimension." );
167 (*staticPtrCast<StdVector>(e)->
getVector())[i] = 1.0;
172 return static_cast<int>(
std_vec_->size());
175 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
180 for(
auto& e : *
std_vec_ ) e = f.apply(e);
186 std::invalid_argument,
187 "Error: Vectors must have the same dimension." );
190 const std::vector<Element>& xval = *ex.
getVector();
193 (*std_vec_)[i] = f.apply((*
std_vec_)[i],xval[i]);
198 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
199 Real result = r.initialValue();
212 void randomize(
const Real l = 0.0,
const Real u = 1.0 ) {
221 auto get_rand = [a,b]( Real& e ) {
222 auto x =
static_cast<Real
>(rand())/static_cast<Real>(RAND_MAX);
228 virtual void print( std::ostream &outStream )
const {
233 for(
auto e : *
std_vec_ ) outStream << e <<
" ";
234 outStream << std::endl;
typename PV< Real >::size_type size_type
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void scale(const Real alpha)
Compute where .
StdVector(const int dim, const Element val=0.0)
virtual int dimension() const
Return dimension of the vector space.
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
StdVector(const Ptr< std::vector< Element >> &std_vec)
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Ptr< const std::vector< Element > > getVector() const
typename std::vector< Real >::size_type size_type
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Defines the linear algebra or vector space interface.
virtual void print(std::ostream &outStream) const
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void plus(const Vector< Real > &x)
Compute , where .
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Real reduce(const Elementwise::ReductionOp< Real > &r) const
int dimension() const
Return dimension of the vector space.
void set(const Vector< Real > &x)
Set where .
void setScalar(const Real C)
Set where .
Ptr< std::vector< Element > > std_vec_
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
virtual Real dot(const Vector< Real > &x) const
Compute where .
Ptr< std::vector< Element > > getVector()
StdVector(std::initializer_list< Element > ilist)