44 #ifndef ROL_STDARRAY_H
45 #define ROL_STDARRAY_H
60 template<
typename Real, std::
size_t array_size, std::
size_t pool_size=100u>
68 if( getCount(vptr) < 2 ) {
73 if( is_nullPtr(
data) ) {
74 data = makePtr<std::array<Real,array_size>>();
86 const auto& ex =
_array(x);
87 std::copy(ex.begin(),ex.end(),
data->begin());
91 const auto& ex =
_array(x);
92 std::transform(ex.begin(),ex.end(),
data->begin(),
data->begin(),std::plus<Real>{});
96 const auto& ex =
_array(x);
97 std::transform(ex.begin(),ex.end(),
data->begin(),
data->begin(),[alpha](Real x, Real y){
return alpha*x+y; });
101 for(
auto& e : *
data ) e *= alpha;
106 const auto& ex =
_array(x);
107 std::inner_product(ex.begin(),ex.end(),
data->begin(),result);
112 Real norm_squared = 0;
113 for(
auto e: *
data ) norm_squared += (e*e);
114 return std::sqrt(norm_squared);
117 virtual Ptr<Vector<Real>>
clone()
const {
118 return makePtr<StdArray>();
121 Ptr<Vector<Real>>
basis(
const int i )
const {
122 auto b_ptr =
clone();
123 auto& b_ref =
static_cast<StdArray&
>(*b_ptr);
129 int dimension()
const {
return static_cast<int>(array_size); }
133 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
134 for(
auto& e : *
data ) e = f.apply(e);
139 const auto& ex =
_array(x);
140 std::transform(ex.begin(),ex.end(),
data->begin(),
data->begin(),
141 [&f](Real a, Real b){
return f.apply(a,b);});
144 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
145 Real result = r.initialValue();
146 for(
auto e: *
data ) r.reduce(e,result);
152 void randomize(
const Real l = -1.0,
const Real u = 1.0 ) {
153 std::random_device rd;
154 std::mt19937 gen(rd());
155 std::uniform_real_distribution<Real> dis(l, u);
156 for(
auto& e : *
data ) e = dis(gen);
159 virtual void print( std::ostream &outStream )
const {
160 for(
auto e: *
data ) outStream << e <<
" ";
161 outStream << std::endl;
165 for( std::size_t i=0; i<array_size; ++i )
pool_ptr[i] = makePtrFromRef(
pool[i]);
170 std::size_t count = 0u;
171 for(
auto& vptr :
pool_ptr ) count += ( getCount(vptr)>1 );
183 Ptr<std::array<Real,array_size>>
data;
186 static std::array<std::array<Real,array_size>,pool_size>
pool;
187 static std::array<Ptr<std::array<Real,array_size>>,pool_size>
pool_ptr;
191 template<
typename Real, std::
size_t array_size, std::
size_t pool_size>
194 template<
typename Real, std::
size_t array_size, std::
size_t pool_size>
static std::size_t pool_count()
void zero()
Set to zero vector.
const std::array< Real, array_size > & _array(const Vector< Real > &x) const
void set(const Vector< Real > &x)
Set where .
std::array< Real, array_size > data_type
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Defines the linear algebra or vector space interface.
StdArray(Ptr< std::array< Real, array_size >> p)
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the std::array implementation of the ROL::Vector interface.
int dimension() const
Return dimension of the vector space.
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
void plus(const Vector< Real > &x)
Compute , where .
void scale(const Real alpha)
Compute where .
static std::array< std::array< Real, array_size >, pool_size > pool
static void initialize_pool()
Ptr< std::array< Real, array_size > > data
virtual void print(std::ostream &outStream) const
void setScalar(const Real alpha)
Set where .
const std::array< Real, array_size > & get_array() const
virtual Real dot(const Vector< Real > &x) const
Compute where .
Real norm() const
Returns where .
static std::array< Ptr< std::array< Real, array_size > >, pool_size > pool_ptr
void randomize(const Real l=-1.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
std::array< Real, array_size > & get_array()
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Real & operator[](std::size_t i)