15 for (
auto x = il.begin() ; x != il.end(); x++,i++)
_data[i]= *x;
25 for (
auto jl = il.begin() ; jl != il.end(); jl++,i++)
28 for (
auto x = jl->begin() ; x != jl->end(); x++,j++)
33 template <
typename T,
typename EXPR,
34 typename=
typename std::enable_if<std::is_class<EXPR>::value, EXPR>::type>
39 for(
index i=0; i<expr.size(); i++ ) data[i] = expr[i];
43 template <
typename T,
typename VAL,
44 typename=
typename std::enable_if<!std::is_class<VAL>::value, VAL>::type>
48 for(
index i=0; i<A.
size(); i++ ) data[i] = a;
52 template <
typename T,
typename EXPR,
53 typename=
typename std::enable_if<std::is_class<EXPR>::value, EXPR>::type>
58 for(
index i=0; i<expr.size(); i++ ) data[i] += expr[i];
62 template <
typename T,
typename VAL,
63 typename=
typename std::enable_if<!std::is_class<VAL>::value, VAL>::type>
67 for(
index i=0; i<A.
size(); i++ )data[i] += a;
71 template <
typename T,
typename EXPR,
72 typename=
typename std::enable_if<std::is_class<EXPR>::value, EXPR>::type>
77 for(
index i=0; i<expr.size(); i++ ) data[i] -= expr[i];
81 template <
typename T,
typename VAL,
82 typename=
typename std::enable_if<!std::is_class<VAL>::value, VAL>::type>
86 for(
index i=0; i<A.
size(); i++ )data[i] -= a;
90 template <
typename T,
typename EXPR,
91 typename=
typename std::enable_if<std::is_class<EXPR>::value, EXPR>::type>
96 for(
index i=0; i<expr.size(); i++ ) data[i] *= expr[i];
100 template <
typename T,
typename VAL,
101 typename=
typename std::enable_if<!std::is_class<VAL>::value, VAL>::type>
105 for(
index i=0; i<A.
size(); i++ )data[i] *= a;
109 template <
typename T,
typename EXPR,
110 typename=
typename std::enable_if<std::is_class<EXPR>::value, EXPR>::type>
115 for(
index i=0; i<expr.size(); i++ ) data[i] /= expr[i];
119 template <
typename T,
typename VAL,
120 typename=
typename std::enable_if<!std::is_class<VAL>::value, VAL>::type>
124 for(
index i=0; i<A.
size(); i++ )data[i] /= a;
127 template <
typename T>
133 snprintf(errormsg,80,
"numcxx::TArray::_check_bounds: attempt of %uD access of %uD array",acc_ndim,
_ndim);
134 throw std::out_of_range(errormsg);
136 if ((acc_idx<0) || (acc_idx>=
_shape[acc_dim]))
139 snprintf(errormsg,80,
"numcxx::TArray::_check_bounds: _shape[%u]=%u but i%u=%u",acc_dim,
_shape[acc_dim],acc_dim,acc_idx);
140 throw std::out_of_range(errormsg);
144 template <
typename T>
150 snprintf(errormsg,80,
"numcxx::TArray::_assert_square: unexpected non-equal array dimensions\n");
151 throw std::length_error(errormsg);
155 template <
typename T>
158 #ifdef NUMCXX_CHECK_BOUNDS 164 template <
typename T>
167 #ifdef NUMCXX_CHECK_BOUNDS 174 template <
typename T>
177 #ifdef NUMCXX_CHECK_BOUNDS 185 template <
typename T>
189 _size((
size_t)n0*(
size_t)n1),
192 {
if (
_data==
nullptr)
throw std::runtime_error(
"numcxx: TArray::TArray(): Memory allocation failed"); };
194 template <
typename T>
199 _data((T*)malloc(
sizeof(T)*_size)),
201 {
if (
_data==
nullptr)
throw std::runtime_error(
"numcxx: TArray::TArray(): Memory allocation failed"); };
203 template <
typename T>
212 template <
typename T>
216 template <
typename T>
228 template <
typename T>
230 TArray(n0,n1,data, [](T*p){;})
233 template <
typename T>
244 template <
typename T>
253 template <
typename T>
265 template <
typename T>
271 snprintf(errormsg,80,
"numcxx::TArray::resize: unable to set 1D shape for 2D array.\n");
272 throw std::runtime_error(errormsg);
281 template <
typename T>
284 if (_size==n)
return;
289 snprintf(errormsg,80,
"numcxx::TArray::resize: unable to resize 2D Array to 1D.\n");
290 throw std::runtime_error(errormsg);
296 _data=(T*)malloc(
sizeof(T)*n);
297 if (
_data==
nullptr)
throw std::runtime_error(
"numcxx: TArray::resize(): Memory allocation failed");
306 snprintf(errormsg,80,
"numcxx::TArray::resize: unable to resize - data managed by different object.\n");
307 throw std::runtime_error(errormsg);
313 template <
typename T>
319 template <
typename T>
328 inline std::ostream & operator << (std::ostream & s, TArray<T> &A)
331 for (
index i=0;i<A.size();i++) s <<
"[" << i <<
"]: " <<A(i) << std::endl << std::flush;
335 for (
index j=0;j<A.shape(1);j++)
336 s <<
"[" << j <<
"] ";
338 for (
index i=0;i<A.shape(0);i++)
340 s <<
"[" << i <<
"]: ";
341 for (
index j=0;j<A.shape(1);j++)
350 template <
typename T>
354 for (
index i=0;i<
size();i++) s <<
_data[i] << std::endl << std::flush;
void _assert_square() const
Check if all shapes are the same.
void savetxt(std::ostream &s) const
index _shape[3]
Shape vector.
index _idx(index i0) const
1D Array index calculation with optional bounds check.
void xdiv(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
std::function< void(T *p)> _deleter
Deleter method.
TArray is the common template base class for arrays and dense matrices of the numcxx project...
size_t size() const
Obtain size of array.
void xadd(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
void _check_bounds(index acc_dim, index acc_ndim, index acc_idx) const
Bounds checker.
void _nullify()
Nullify contents of array (for move constructors)
TArray()
Construct an zero length 1D array.
void xmul(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
static void operate(std::function< void(T &a, T &b)> f, TArray< T > &A, TArray< T > &B)
Binary operation on arrays.
void xsub(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
size_t _size
Size of array.
index ndim() const
Obtain tensor dimension of array.
void _setshape(index shape0)
Set shape of 1D array (for move constructors)
void resize(size_t n)
Resize array.
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
index shape(const index dim) const
Obtain shape of array for given dimension.
std::shared_ptr< void > _datamanager
Data manager.
const index _ndim
Tensor dimension.
T * data() const
Obtain C-pointer of data array.