10 #ifndef ROL_STDVECTOR_H
11 #define ROL_STDVECTOR_H
16 #include <initializer_list>
26 template <
class Real,
class Element=Real>
40 std_vec_( makePtr<std::vector<Element>>(ilist) ) {}
48 std::invalid_argument,
49 "Error: Vectors must have the same dimension." );
52 const std::vector<Element>& xval = *ex.
getVector();
53 std::copy(xval.begin(),xval.end(),
std_vec_->begin());
59 std::invalid_argument,
60 "Error: Vectors must have the same dimension." );
63 const std::vector<Element>& xval = *ex.
getVector();
66 (*std_vec_)[i] += xval[i];
73 std::invalid_argument,
74 "Error: Vectors must have the same dimension." );
77 const std::vector<Element>& xval = *ex.
getVector();
80 (*std_vec_)[i] += alpha*xval[i];
84 void scale(
const Real alpha ) {
85 for(
auto& e : *
std_vec_ ) e *= alpha;
95 std::invalid_argument,
96 "Error: Vectors must have the same dimension." );
99 const std::vector<Element>& xval = *ex.
getVector();
106 return std::inner_product(
std_vec_->begin(),
std_vec_->end(), xval.begin(), Real(0));
111 val = std::sqrt(
dot(*
this) );
115 virtual Ptr<Vector<Real> >
clone()
const {
116 return makePtr<StdVector>( makePtr<std::vector<Element>>(
std_vec_->size(),
static_cast<Element
>(0)));
127 Ptr<Vector<Real> >
basis(
const int i )
const {
129 ROL_TEST_FOR_EXCEPTION( i >=
dimension() || i<0,
130 std::invalid_argument,
131 "Error: Basis index must be between 0 and vector dimension." );
133 (*staticPtrCast<StdVector>(e)->
getVector())[i] = 1.0;
138 return static_cast<int>(
std_vec_->size());
141 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
146 for(
auto& e : *
std_vec_ ) e = f.apply(e);
152 std::invalid_argument,
153 "Error: Vectors must have the same dimension." );
156 const std::vector<Element>& xval = *ex.
getVector();
159 (*std_vec_)[i] = f.apply((*
std_vec_)[i],xval[i]);
164 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
165 Real result = r.initialValue();
178 void randomize(
const Real l = 0.0,
const Real u = 1.0 ) {
187 auto get_rand = [a,b]( Real& e ) {
188 auto x =
static_cast<Real
>(rand())/static_cast<Real>(RAND_MAX);
194 virtual void print( std::ostream &outStream )
const {
199 for(
auto e : *
std_vec_ ) outStream << e <<
" ";
200 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)