NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
tarray.hxx
Go to the documentation of this file.
1 #ifndef NUMCXX_TARRAY_H
6 #define NUMCXX_TARRAY_H
7 #include <ostream>
8 #include <typeinfo>
9 #include <memory>
10 #include <stdexcept>
11 #include <functional>
12 #include "expression.ixx"
13 
14 
15 namespace numcxx
16 {
17  template<typename T> class TArray;
18 
19 
20 
21  template<typename T>
22  inline std::ostream & operator << (std::ostream & s, TArray<T> &A);
23 
26  template<typename T> class TArray
27  {
28  public:
29 
30  typedef T value_type;
31 
36  T & operator()(const index i0) { return _data[_idx(i0)];};
37  const T & operator()(const index i0) const { return _data[_idx(i0)];};
38 
44  T & operator()(const index i0, const index i1) { return _data[_idx(i0,i1)];};
45  const T & operator()(const index i0, const index i1) const { return _data[_idx(i0,i1)];};
46 
47 
52  index ndim() const {return _ndim;}
53 
58  size_t size() const { return _size;}
59 
68  index shape(const index dim) const {return _shape[dim];}
69 
73  template <typename VAL>
74  void operator+=(const VAL & a) {xadd(*this,a);}
75 
79  template <typename VAL>
80  void operator-=(const VAL& a) {xsub(*this,a);}
81 
82 
86  template <typename VAL>
87  void operator*=(const VAL& a) {xmul(*this,a);}
88 
89 
93  template <typename VAL>
94  void operator/=(const VAL & a) {xdiv(*this,a);}
95 
96 
97 
103  static void operate(std::function< void ( T& a, T&b)> f, TArray<T> & A, TArray<T> & B);
104 
111  static void operate(std::function< void ( T& a, T&b,T&c)> f, TArray<T> & A, TArray<T> & B,TArray<T> & C);
112 
113 
115  T & operator[](const index i) { return _data[i];}
116 
118  const T & operator[](const index i) const { return _data[i];};
119 
121  template <typename VAL>
122  TArray<T>& operator=(const VAL &expr) {return assign(*this,expr);}
123 
128  T*data() const { return _data;}
129 
130 
132  void resize(size_t n);
133 
134 
136  TArray(const TArray<T>& A)=delete;
137 
138  void savetxt(std::ostream &s) const;
139 
140  private:
141 
143  const index _ndim;
144 
146  size_t _size;
147 
149  index _shape[3]={0,0,0};
150 
151 
161  void _check_bounds(index acc_dim, index acc_ndim, index acc_idx) const;
162 
163 
164  protected:
165 
172  std::function<void(T*p)> _deleter=nullptr;
173 
183  std::shared_ptr<void>_datamanager =nullptr;
184 
186  T* _data=nullptr;
187 
188 
192  void _assert_square() const;
193 
195  index _idx(index i0) const;
196 
198  index _idx(index i0,index i1) const;
199 
201  index _idx(index i0,index i1,index i2) const;
202 
204  TArray();
205 
207  void _nullify();
208 
210  void _setshape(index shape0);
211 
214  TArray(index n0);
215 
217  TArray(const std::initializer_list<T> &il );
218 
219 
225  TArray(index n0, T*data, std::function<void(T*p)> deleter);
226 
233  TArray(index n0, T*data, std::shared_ptr<void> datamanager);
234 
239  TArray(index n0, index n1);
240 
242  TArray(const std::initializer_list<std::initializer_list<T>> &il );
243 
244 
251  TArray(index n0, index n1, T*data,std::function<void(T*p)> deleter);
252 
260  TArray(index n0, index n1, T*data,std::shared_ptr<void> datamanager);
261 
262 
264  ~TArray();
265 
267  friend std::ostream & operator<< <T>(std::ostream & s, TArray<T> &A);
268 
269 
270  };
271 
272 
273 
275  template<typename T> class TLinSolver
276  {
277  public:
279  virtual void solve( TArray<T> & sol, const TArray<T> & rhs) const {};
280  virtual void update(void){};
281  };
282 
284  template<typename T> class TLinOperator
285  {
286  public:
288  virtual void apply( const TArray<T> & sol, TArray<T> & rhs) const {};
289  };
290 
291 
292 }
293 
294 
295 #include "tarray.ixx"
296 #endif
TArray< T > & operator=(const VAL &expr)
Expression template compatible assignment operator.
Definition: tarray.hxx:122
Inline metho definitions for numcxx::TArray.
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145
void savetxt(std::ostream &s) const
Definition: tarray.ixx:351
Implementation of expression templates.
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
virtual void solve(TArray< T > &sol, const TArray< T > &rhs) const
Definition: tarray.hxx:279
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
void xdiv(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:111
std::function< void(T *p)> _deleter
Deleter method.
Definition: tarray.hxx:172
TArray is the common template base class for arrays and dense matrices of the numcxx project...
Definition: tarray.hxx:17
size_t size() const
Obtain size of array.
Definition: tarray.hxx:58
void operator*=(const VAL &a)
Multiply all elements by value.
Definition: tarray.hxx:87
T & operator()(const index i0)
Access operator for 1D arrays.
Definition: tarray.hxx:36
~TArray()
Destructor.
Definition: tarray.ixx:234
void xadd(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:54
void _check_bounds(index acc_dim, index acc_ndim, index acc_idx) const
Bounds checker.
Definition: tarray.ixx:128
Base class for linear solvers and preconditioners.
Definition: tarray.hxx:275
void operator/=(const VAL &a)
Divide each element by value.
Definition: tarray.hxx:94
void _nullify()
Nullify contents of array (for move constructors)
Definition: tarray.ixx:254
virtual void update(void)
Definition: tarray.hxx:280
TArray()
Construct an zero length 1D array.
Definition: tarray.ixx:245
void xmul(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:92
unsigned int index
Definition: numcxx.hxx:21
virtual void apply(const TArray< T > &sol, TArray< T > &rhs) const
Definition: tarray.hxx:288
const T & operator[](const index i) const
Const reference to entry for use in expression templates.
Definition: tarray.hxx:118
T & operator[](const index i)
Alternative access operator for 1D arrays.
Definition: tarray.hxx:115
const T & operator()(const index i0, const index i1) const
Definition: tarray.hxx:45
const T & operator()(const index i0) const
Definition: tarray.hxx:37
static void operate(std::function< void(T &a, T &b)> f, TArray< T > &A, TArray< T > &B)
Binary operation on arrays.
Definition: tarray.ixx:314
T & operator()(const index i0, const index i1)
Access operator for 2D arrays.
Definition: tarray.hxx:44
void xsub(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:73
size_t _size
Size of array.
Definition: tarray.hxx:146
index ndim() const
Obtain tensor dimension of array.
Definition: tarray.hxx:52
T * _data
Data pointer.
Definition: tarray.hxx:186
void _setshape(index shape0)
Set shape of 1D array (for move constructors)
Definition: tarray.ixx:266
void resize(size_t n)
Resize array.
Definition: tarray.ixx:282
double B(double x)
Numcxx template library.
Definition: expression.ixx:41
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68
void operator+=(const VAL &a)
Add value to all elements.
Definition: tarray.hxx:74
void operator-=(const VAL &a)
Subtract value from all elements.
Definition: tarray.hxx:80
Base class for linear operators (matrices, sparse matrices)
Definition: tarray.hxx:284
std::shared_ptr< void > _datamanager
Data manager.
Definition: tarray.hxx:183
const index _ndim
Tensor dimension.
Definition: tarray.hxx:143
T * data() const
Obtain C-pointer of data array.
Definition: tarray.hxx:128