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>