C++ main module for gpm Package  1.0
CORE_Matrix.h
Go to the documentation of this file.
1 #ifndef CORE_Matrix_H
2 #define CORE_Matrix_H
3 #include "CORE_List.h"
4 #include "types.h"
5 #include <vector>
7 
8 #include "CORE_String.h"
9 #include "CORE_Vector.h"
10 using namespace std;
11 
12 
13 
18 template <class T>
19 class CORE_Matrix : public CORE_Object {
20 
21 
22  private:
23  vector< boost::shared_ptr<CORE_Vector<T> > > mMatrix;
24 
25 
26 
27 
28 
29  // CONSTRUCTORS
30  public:
33  CORE_Matrix();
34 
37  CORE_Matrix(const int& n);
38 
39 
40 
41 
42 
43  // DESTRUCTORS
44  public:
47  virtual ~CORE_Matrix();
48 
49 
50  public:
51  // NEW operators
55  static inline boost::shared_ptr<CORE_Matrix<T> > New(const boost::shared_ptr<CORE_Matrix<T> >& v ) {
56  boost::shared_ptr<CORE_Matrix<T> > p(new CORE_Matrix<T>(),
58  p->setThis(p);
59  p->copy(v);
60  return p;
61  };
65  static inline boost::shared_ptr<CORE_Matrix<T> > New(const CORE_Matrix<T>& v ) {
66  boost::shared_ptr<CORE_Matrix<T> > p(new CORE_Matrix<T>(),
68  p->setThis(p);
69  p->copy(v);
70  return p;
71  };
75  static inline boost::shared_ptr<CORE_Matrix<T> > New(const CORE_Matrix<T>* v ) {
76  boost::shared_ptr<CORE_Matrix<T> > p(new CORE_Matrix<T>(),
78  p->setThis(p);
79  if (v!=null) p->copy(*v);
80  return p;
81  };
84  static inline boost::shared_ptr<CORE_Matrix<T> > New() {
85  boost::shared_ptr<CORE_Matrix<T> > p(new CORE_Matrix<T>(),
87  p->setThis(p);
88  return p;
89  };
93  static inline boost::shared_ptr<CORE_Matrix<T> > New(const int& dim) {
94  boost::shared_ptr<CORE_Matrix<T> > p(new CORE_Matrix<T>(),
96  p->setThis(p);
97  p->setSize(dim);
98  return p;
99  };
100  // OPERATORS
101  public:
104  void getSharedPointer(boost::shared_ptr<CORE_Matrix<T> >& p){
105  SP::CORE_Object r;
107  p=boost::dynamic_pointer_cast<CORE_Matrix<T> >(r);
108  };
111  void getSharedPointer( boost::shared_ptr<const CORE_Matrix<T> >& p) const{
112  SPC::CORE_Object r;
114  p=boost::dynamic_pointer_cast<const CORE_Matrix<T> >(r);
115  };
116  private:
119  inline boost::shared_ptr<CORE_Matrix<T> > getThis() {
120  boost::shared_ptr<CORE_Matrix<T> > p;
121  getSharedPointer(p);
122  return p;
123  };
126  inline boost::shared_ptr<const CORE_Matrix<T> > getThis() const {
127  boost::shared_ptr<const CORE_Matrix<T> > p;
128  getSharedPointer(p);
129  return p;
130  };
131  public:
132 
137  inline const CORE_Vector<T>& operator[](const int& i) const {
138  ASSERT_IN(i>-1);
139  ASSERT_IN(i<((int)mMatrix.size()));
140  return *(mMatrix[i].get());
141  };
142 
147  inline CORE_Vector<T>& operator[](const int& i) {
148  ASSERT_IN(i>-1);
149  ASSERT_IN(i<((int)mMatrix.size()));
150  return *(mMatrix[i].get());
151  };
152 
153 
154  // SET
157  inline void setVector(const int& i,boost::shared_ptr<CORE_Vector<T> > v) {
158  mMatrix[i]=v;
159  };
160 
163  inline void setValue(const int& i,const int& j,const T& v) {
164  (*(mMatrix[i]).get())[j]=v;
165  };
166 
169  inline void addValue(const int& i,const T& v) {
170  mMatrix[i]->add(v);
171  };
172 
175  inline void setSize(const int& n){
176  mMatrix.resize(n);
177  };
178 
181  inline void initValues(const T& v) {
182  int n=mMatrix.size();
183  for(int i=0;i<n;i++) mMatrix[i]->initValues(v);
184  };
185 
186 
189  inline void add(const boost::shared_ptr<const CORE_Vector<T> > v) {
190  boost::shared_ptr<CORE_Vector<T> >c=CORE_Vector<T>::New(v);
191  mMatrix.pusch_back(c);
192  };
195  inline void add(boost::shared_ptr<CORE_Vector<T> >v) {
196  mMatrix.puch_back(v);
197  };
198 
201  template<class Y> void copy(const CORE_Matrix<Y>& cmat) {
202  int n=cmat.getSize();
203  mMatrix.resize(n);
204  for (int i=0;i<n;i++) {
205  boost::shared_ptr<CORE_Vector<T> > c=CORE_Vector<T>::New();
206  c->copy(cmat);
207  mMatrix[i]=c;
208  }
209  }
212  template<class Y> void copy(const CORE_Matrix<Y>* array) {
213  if (array!=null) copy(*array);
214  else setSize(0);
215  }
218  template<class Y> void copy(const boost::shared_ptr<CORE_Matrix<T> >& array) {
219  copy(array.get());
220  }
223  tBoolean set(const int i,const int& j,const T& v) {
224  if (i>=mMatrix.size()) return false;
225  CORE_Vector<T> *vect=mMatrix[i].get();
226  if (vect==null) return false;
227  if (vect->getSize()<=j) return false;
228  (*vect[j])=v;
229  return true;
230  };
231 
234  inline void clear(){
235  setSize(0);
236  };
237 
238  // GET
239 
244  inline const T& get(const int& i,const int& j) const {
245  ASSERT_IN(i>-1);
246  ASSERT_IN(i<((int)mMatrix.size()));
247  return *(mMatrix[i].get())[j];
248  };
249 
254  inline T& get(const int& i,const int& j) {
255  ASSERT_IN(i>-1);
256  ASSERT_IN(i<((int)mMatrix.size()));
257  return *(mMatrix[i].get())[j];
258  };
259 
261  inline int size() const {return mMatrix.size();};
262 
264  inline int getSize() const {return mMatrix.size();};
265 
267  inline int getSize(const int& i) const {return mMatrix[i]->getSize();};
268 
270  inline int getDimension(const int& i) const {return mMatrix[i]->getSize();};
271 
273  inline int getDimension() const {return mMatrix.size();};
274 
275 
278  inline const T& getValue(const int& i,const int& j) const {return* (mMatrix[i].get())[j];};
279 
280 
281 
284  inline CORE_Vector<T>& getVector(const int& index) {
285  return *(mMatrix[index].get());
286  };
289  inline const CORE_Vector<T>& getVector(const int& index) const{
290  return *(mMatrix[index].get());
291  };
292 
293 
294 
297  virtual tString toString() const;
298 
299 
300 };
301 
302 #include "CORE_Matrix.hpp"
303 
314 
315 //share pointer of primitive type vector
326 
327 
328 #endif
void clear()
clear the array
Definition: CORE_Matrix.h:234
tBoolean set(const int i, const int &j, const T &v)
set the pointer at the index i
Definition: CORE_Matrix.h:223
static boost::shared_ptr< CORE_Matrix< T > > New(const int &dim)
create a shared pointer of vector of size dim
Definition: CORE_Matrix.h:93
this class describes an array
Definition: CORE_Vector.h:18
CORE_Matrix< tComplex > CORE_ComplexMatrix
Definition: CORE_Matrix.h:308
int getSize() const
return the size of the vector
Definition: CORE_Matrix.h:264
void setValue(const int &i, const int &j, const T &v)
set value
Definition: CORE_Matrix.h:163
CORE_Matrix< tShort > CORE_ShortMatrix
Definition: CORE_Matrix.h:310
TYPEDEF_SPTR(CORE_BooleanMatrix)
int size() const
return the size of the vector
Definition: CORE_Matrix.h:261
const CORE_Vector< T > & getVector(const int &index) const
get vector
Definition: CORE_Matrix.h:289
#define tBoolean
Definition: types.h:35
void addValue(const int &i, const T &v)
set value
Definition: CORE_Matrix.h:169
void copy(const CORE_Matrix< Y > *array)
copy
Definition: CORE_Matrix.h:212
this class describes a vector of boost vectors
Definition: CORE_Matrix.h:19
int getSize(const int &i) const
return the size of the vector
Definition: CORE_Matrix.h:267
#define null
Definition: types.h:13
static boost::shared_ptr< CORE_Matrix< T > > New(const CORE_Matrix< T > *v)
create a shared pointer of vector which is a copy of vector v
Definition: CORE_Matrix.h:75
static boost::shared_ptr< CORE_Matrix< T > > New(const CORE_Matrix< T > &v)
create a shared pointer of vector which is a copy of vector v
Definition: CORE_Matrix.h:65
void add(boost::shared_ptr< CORE_Vector< T > >v)
init the value to v
Definition: CORE_Matrix.h:195
CORE_Matrix< tCharacter > CORE_CharacterMatrix
Definition: CORE_Matrix.h:305
int getDimension() const
return the dimension of the vector
Definition: CORE_Matrix.h:273
CORE_Matrix< tReal > CORE_RealMatrix
Definition: CORE_Matrix.h:307
int getDimension(const int &i) const
return the size of the vector
Definition: CORE_Matrix.h:270
static boost::shared_ptr< CORE_Matrix< T > > New()
create a shared pointer of vector
Definition: CORE_Matrix.h:84
CORE_Matrix< tString > CORE_StringMatrix
Definition: CORE_Matrix.h:311
void getSharedPointer(boost::shared_ptr< const CORE_Matrix< T > > &p) const
get the shared pointer into P
Definition: CORE_Matrix.h:111
const CORE_Vector< T > & operator[](const int &i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mMatrix.size()));
Definition: CORE_Matrix.h:137
CORE_Vector< T > & operator[](const int &i)
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mMatrix.size()));
Definition: CORE_Matrix.h:147
CORE_Vector< T > & getVector(const int &index)
get vector
Definition: CORE_Matrix.h:284
void copy(const boost::shared_ptr< CORE_Matrix< T > > &array)
copy
Definition: CORE_Matrix.h:218
void getSharedPointer(SP::CORE_Object &p)
get the shared pointer of this class into p
Definition: CORE_Object.h:65
void getSharedPointer(boost::shared_ptr< CORE_Matrix< T > > &p)
get the shared pointer into P
Definition: CORE_Matrix.h:104
static boost::shared_ptr< CORE_Matrix< T > > New(const boost::shared_ptr< CORE_Matrix< T > > &v)
create a shared pointer of vector which is a copy of vector v
Definition: CORE_Matrix.h:55
void copy(const CORE_Matrix< Y > &cmat)
copy
Definition: CORE_Matrix.h:201
int getSize() const
return the size of the vector
Definition: CORE_Vector.h:387
abstract base class for most classes.
Definition: CORE_Object.h:30
#define tString
Definition: types.h:36
static boost::shared_ptr< CORE_Vector< T > > New()
create a shared pointer of vector
Definition: CORE_Vector.h:84
const T & get(int i) const
get the pointer at index i ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_Vector.h:367
CORE_Matrix< tBoolean > CORE_BooleanMatrix
Definition: CORE_Matrix.h:304
void setSize(const int &n)
set the size of the array
Definition: CORE_Matrix.h:175
void add(const boost::shared_ptr< const CORE_Vector< T > > v)
init the value to v
Definition: CORE_Matrix.h:189
CORE_Matrix< tInteger > CORE_IntegerMatrix
Definition: CORE_Matrix.h:312
CORE_Matrix< tRelativeInteger > CORE_RelativeIntegerMatrix
Definition: CORE_Matrix.h:313
CORE_Matrix< tFlag > CORE_FlagMatrix
Definition: CORE_Matrix.h:309
CORE_Matrix< int > CORE_IntMatrix
Definition: CORE_Matrix.h:306
void initValues(const T &v)
init the value to v
Definition: CORE_Matrix.h:181
const T & getValue(const int &i, const int &j) const
get value
Definition: CORE_Matrix.h:278
void setVector(const int &i, boost::shared_ptr< CORE_Vector< T > > v)
set value
Definition: CORE_Matrix.h:157
#define ASSERT_IN(a)
Definition: types.h:82
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106