10 #ifndef ROL_STDARRAY_H
11 #define ROL_STDARRAY_H
27 template<
typename Real, std::
size_t array_size, std::
size_t pool_size=100u>
35 if( getCount(vptr) < 2 ) {
40 if( is_nullPtr(
data) ) {
41 data = makePtr<std::array<Real,array_size>>();
53 const auto& ex =
_array(x);
54 std::copy(ex.begin(),ex.end(),
data->begin());
58 const auto& ex =
_array(x);
59 std::transform(ex.begin(),ex.end(),
data->begin(),
data->begin(),std::plus<Real>{});
63 const auto& ex =
_array(x);
64 std::transform(ex.begin(),ex.end(),
data->begin(),
data->begin(),[alpha](Real x, Real y){
return alpha*x+y; });
67 void scale(
const Real alpha ) {
68 for(
auto& e : *
data ) e *= alpha;
73 const auto& ex =
_array(x);
74 std::inner_product(ex.begin(),ex.end(),
data->begin(),result);
79 Real norm_squared = 0;
80 for(
auto e: *
data ) norm_squared += (e*e);
81 return std::sqrt(norm_squared);
84 virtual Ptr<Vector<Real>>
clone()
const {
85 return makePtr<StdArray>();
88 Ptr<Vector<Real>>
basis(
const int i )
const {
90 auto& b_ref =
static_cast<StdArray&
>(*b_ptr);
96 int dimension()
const {
return static_cast<int>(array_size); }
100 void applyUnary(
const Elementwise::UnaryFunction<Real> &f ) {
101 for(
auto& e : *
data ) e = f.apply(e);
106 const auto& ex =
_array(x);
107 std::transform(ex.begin(),ex.end(),
data->begin(),
data->begin(),
108 [&f](Real a, Real b){
return f.apply(a,b);});
111 Real
reduce(
const Elementwise::ReductionOp<Real> &r )
const {
112 Real result = r.initialValue();
113 for(
auto e: *
data ) r.reduce(e,result);
119 void randomize(
const Real l = -1.0,
const Real u = 1.0 ) {
120 std::random_device rd;
121 std::mt19937 gen(rd());
122 std::uniform_real_distribution<Real> dis(l, u);
123 for(
auto& e : *
data ) e = dis(gen);
126 virtual void print( std::ostream &outStream )
const {
127 for(
auto e: *
data ) outStream << e <<
" ";
128 outStream << std::endl;
132 for( std::size_t i=0; i<array_size; ++i )
pool_ptr[i] = makePtrFromRef(
pool[i]);
137 std::size_t count = 0u;
138 for(
auto& vptr :
pool_ptr ) count += ( getCount(vptr)>1 );
150 Ptr<std::array<Real,array_size>>
data;
153 static std::array<std::array<Real,array_size>,pool_size>
pool;
154 static std::array<Ptr<std::array<Real,array_size>>,pool_size>
pool_ptr;
158 template<
typename Real, std::
size_t array_size, std::
size_t pool_size>
161 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)