C++ main module for mmsd Package  1.0
LAP_DoubleMatrix.h
Go to the documentation of this file.
1 #ifndef LAP_DoubleMatrix_H
2 #define LAP_DoubleMatrix_H
3 
5 #include "LAP_DoubleVector.h"
7 
8 
17 
19 
20  SP_OBJECT(LAP_DoubleMatrix);
21 
22 private:
23  // ATTRIBUTES
24 
25 
26 
27 
28  // ASSOCIATIONS
29 
30  SP::LAP_DoubleMatrixStorage mStorage;
31 
32  // METHOD
33 
34 
35 public:
36  // CONSTRUCTORS
39  LAP_DoubleMatrix(const tLVectorIndex& n,const tLVectorIndex& p);
40 
41 
42 
43  // DESTRUCTORS
46  virtual ~LAP_DoubleMatrix();
47 
48 
49 
50 public:
55  inline const double& operator[](const tLVectorIndex& index) const {
56  return getStorage()[index];
57  }
62  inline double& operator[](const tLVectorIndex& index) {
63  return getStorage()[index];
64  }
65 
66 
72  virtual const double& operator()(const tLVectorIndex& i,
73  const tLVectorIndex& j) const=0;
74 
80  virtual double& operator()(const tLVectorIndex& i,
81  const tLVectorIndex& j)=0;
82 
83  // NEW
84  // ---
85 
88  virtual SP::LAP_DoubleMatrix NewInstance() const=0;
89 
90  // ----------------
91  // MATRIX SETTINGS
92  // ----------------
93 public:
97  virtual tBoolean copy(const LAP_DoubleMatrix& x);
101  inline tBoolean copy(SPC::LAP_DoubleMatrix x) {
102  if (x.get()!=null) return copy(*x.get());
103  return true;
104  };
109  if (x!=null) return copy(*x);
110  return true;
111  };
112 
113 
114 public:
115 
119  inline void init(const double v) {
120  getStorage().init(v);
121  }
122 
128  inline void set(const tLVectorIndex& i,
129  const tLVectorIndex& j,
130  const double& v) {
131  (*this)(i,j)=v;
132  }
133 
139  inline void add(const tLVectorIndex& i,
140  const tLVectorIndex& j,
141  const double& v) {
142  (*this)(i,j)+=v;
143  }
144 
147  inline void setStorage(SP::LAP_DoubleMatrixStorage s) {
148  mStorage=s;
149  }
150 
153  virtual void setValues(SP::LAP_DoubleVector v) {
154  mStorage->setValuesPointer(v);
155 
156  }
159  virtual void setValues(const tLVectorIndex& n,const double* values) {
160  mStorage->setValues(n,values);
161 
162  }
163 
164  // GET Methods
165  // ===========
166 public:
167 
168 
169 
172  inline double get(const tLVectorIndex& i,
173  const tLVectorIndex& j) const {
174  return (*this)(i,j);
175  }
176 
180  if (mStorage.get()==null) {
181  throw LAP_Exception("math/linalg/core","LAP_DoubleMatrix::getStorage()",
182  "a matrix must have a storage");
183  }
184  return *mStorage.get();
185  }
187  inline const LAP_DoubleMatrixStorage& getStorage() const {
188  if (mStorage.get()==null) {
189  throw LAP_Exception("math/linalg/core","LAP_DoubleMatrix::getStorage()",
190  "a matrix must have a storage");
191  }
192  return *mStorage.get();
193  }
194 
197  virtual tBoolean isSymmetric() const=0;
200  virtual tBoolean isUpper() const=0;
201 
202 public:
205  virtual void getColumn(const tLVectorIndex& j,LAP_DoubleVector& v) const=0;
208  virtual void getColumn(const tLVectorIndex& j,SP::LAP_DoubleVector v) const {
209  if (v.get()!=null) {
210  getColumn(j,*v.get());
211  }
212  };
215  virtual void getRow(const tLVectorIndex& i,LAP_DoubleVector& v) const=0;
218  virtual void getRow(const tLVectorIndex& i,SP::LAP_DoubleVector v) const {
219  if (v.get()!=null) {
220  getRow(i,*v.get());
221  }
222  };
223 
224  // Computation methods
225  // ===================
226 
227 
228 
234  virtual tReal sum(const tFlag& d,LAP_DoubleVector& s) const=0;
240  virtual tReal sum(const tFlag& d,const tLVectorIndex& index) const=0;
241 
244  virtual tReal norm2() const=0;
247  virtual tReal norm2(LAP_DoubleVector& v) const=0;
248 
251  inline double trace() const {
252  return getStorage().trace();
253  }
254 
257  inline void addDiagonal(const double& alpha) {
258  mStorage->addDiagonal(alpha);
259  }
260 
261  /* \brief compute the bands number
262  * @param eps:absolute module to detect null value
263  * @return a pair whose firest element is the number of upper bands and the second the lower bands number
264  */
265  inline pair<tLVectorIndex,tLVectorIndex> computeBandsNumber() const {
266  return mStorage->computeBandsNumber();
267  }
268 
271  inline double getFillRate() const {
272  return mStorage->getFillRate();
273  }
277  return mStorage->getNullValuesNumber();
278  }
282  return mStorage->getValuesNumber();
283  }
284 
285 
286 public:
287  //convenience methods
288  //=====================
289 
296  inline static double zpow(const double& f,const int& n) {
297 
298  //x^0=1
299  if (n==0) return 1;
300  //0^n=0 n>0
301  if (f==0) return 0;
302  //n<0
303  if (n<0) return zpow(1./f,-n);
304  //n=1
305  if (n==1) return f;
306  //n>1
307  double ret=f;
308  for (int i=0;i<n;i++) ret*=ret;
309  return ret;
310  }
311 
312  // LINEAR Method
313  //===============
314 
315 
316 
317 
318 
319  // ----------------
320  // vector product
321  // ----------------
322 
323 public:
328  virtual SP::LAP_DoubleVector vectorProduct(const LAP_DoubleVector& X) const {
329  SP::LAP_DoubleVector Y=LAP_DoubleVector::New();
330  vectorProduct(false,X,*Y.get());
331  return Y;
332  }
337  virtual void vectorProduct(const LAP_DoubleVector& X,
338  LAP_DoubleVector& Y) const{
339  vectorProduct(false,X,Y);
340  }
341 
346  virtual void vectorProduct(SPC::LAP_DoubleVector& X,
347  SP::LAP_DoubleVector& Y) const {
348  if ((X.get()!=null) &&
349  (Y.get()!=null))
350  vectorProduct(false,*X.get(),*Y.get());
351 
352  };
353 
354 
360  virtual void vectorProduct(const tBoolean& isTrans,
361  const LAP_DoubleVector& X,
362  LAP_DoubleVector& Y) const{
365  tLVectorIndex nX=X.getSize();
366  if (isTrans) std::swap(nRows,nCols);
367  //no size
368  if (nRows*nCols==0) return;
369 
370  //verify X vector dimension
371  if (nCols!=nX) {
372  throw LAP_Exception("math/linal/core","LAP_DoubleMatrix::product","incompatible size of vector");
373  }
374  //set the size of Y
375  Y.setSize(nRows);
376  //compute the product
377  vectorProduct(false,
378  nX,X.getIncrement(),&X(0),
379  1.,0.,
380  Y.getSize(),Y.getIncrement(),&Y(0));
381  };
382 
383 
384 
385 
386 
387 
398  virtual void vectorProduct(const tBoolean& isTrans,
399  const tLVectorIndex& nX,const tLVectorIncrement& incX,const double *X,
400  const double& alpha,const double& beta,
401  const tLVectorIndex& nY,const tLVectorIncrement& incY,double *Y) const=0;
402 
403  // ----------------
404  // Matrix product
405  // ----------------
409  virtual SP::LAP_DoubleMatrix matrixProduct(const LAP_DoubleMatrix& B) const=0;
410 
411  // ----------------------
412  // eigen values & vectors
413  // ------------------------
414 public:
415 
420  virtual tBoolean computeEigenValues(LAP_DoubleVector& U) const=0;
421 
422 
423 
424 
425  // -----------------------
426  // linear system solvers
427  // -----------------------
428 
429 
430 
431 
432  // -----------------------
433  // STRING Representation
434  // ----------------------
435 public:
438  virtual tString toString() const;
439 
440 
441 
442 };
443 #endif
virtual tBoolean isUpper() const =0
return true if the matrix is upper
void set(const tLVectorIndex &i, const tLVectorIndex &j, const double &v)
set the value of the element at row i and column j
Definition: LAP_DoubleMatrix.h:128
double trace() const
return trace
Definition: LAP_DoubleMatrix.h:251
this class describes the exceptions raised for LAP package
Definition: LAP_Exception.h:14
virtual tBoolean isSymmetric() const =0
return true if the matrix is symmetric
LAP_DoubleMatrixStorage & getStorage()
set the storage
Definition: LAP_DoubleMatrix.h:179
virtual double trace() const =0
compute the trace of the storage
virtual void getRow(const tLVectorIndex &i, LAP_DoubleVector &v) const =0
get the i-th row in a vector
pair< tLVectorIndex, tLVectorIndex > computeBandsNumber() const
Definition: LAP_DoubleMatrix.h:265
virtual void vectorProduct(const tBoolean &isTrans, const LAP_DoubleVector &X, LAP_DoubleVector &Y) const
compute Y= op(This). X
Definition: LAP_DoubleMatrix.h:360
virtual ~LAP_DoubleMatrix()
destroy a matrix
Definition: LAP_DoubleMatrix.cpp:15
double & operator[](const tLVectorIndex &index)
get the element at index
Definition: LAP_DoubleMatrix.h:62
virtual tBoolean copy(const LAP_DoubleMatrix &x)
copy a matrix
Definition: LAP_DoubleMatrix.cpp:18
virtual void vectorProduct(const LAP_DoubleVector &X, LAP_DoubleVector &Y) const
compute Y= This. X
Definition: LAP_DoubleMatrix.h:337
Definition: LAP_DoubleVector.h:20
const LAP_DoubleMatrixStorage & getStorage() const
set the storage
Definition: LAP_DoubleMatrix.h:187
Definition: LAP_DoubleMatrix.h:18
virtual void setValues(const tLVectorIndex &n, const double *values)
set the values
Definition: LAP_DoubleMatrix.h:159
virtual void getRow(const tLVectorIndex &i, SP::LAP_DoubleVector v) const
get the j-th column in a vector
Definition: LAP_DoubleMatrix.h:218
virtual SP::LAP_DoubleMatrix NewInstance() const =0
create a New instance of this
tLVectorIndex getValuesNumber() const
get the number of values
Definition: LAP_DoubleMatrix.h:281
#define tBoolean
Definition: types.h:48
DEFINE_SPTR(LAP_DoubleMatrix)
void addDiagonal(const double &alpha)
This +=alpha.I.
Definition: LAP_DoubleMatrix.h:257
LAP_DoubleMatrix()
build a matrix
Definition: LAP_DoubleMatrix.cpp:6
virtual tReal norm2() const =0
return norm2 sqrt(sum_ij(aij^2))=sqrt(tr(AtA));
#define null
Definition: types.h:13
const tLVectorIncrement & getIncrement() const
get the increment of the vector
Definition: LAP_Vector.h:529
void init(const double &v)
init the values
Definition: LAP_DoubleMatrixStorage.h:124
virtual void getColumn(const tLVectorIndex &j, LAP_DoubleVector &v) const =0
get the j-th column in a vector
static double zpow(const double &f, const int &n)
compute the value of pow(f,n)=f^n
Definition: LAP_DoubleMatrix.h:296
virtual SP::LAP_DoubleMatrix matrixProduct(const LAP_DoubleMatrix &B) const =0
return C= This.B
tLVectorIndex getSize() const
get the size of the vector
Definition: LAP_Vector.h:519
virtual tReal sum(const tFlag &d, LAP_DoubleVector &s) const =0
make the sum among the direction if (d==ROW) sum all the columns of each row and size of s is the num...
virtual tBoolean computeEigenValues(LAP_DoubleVector &U) const =0
compute the eigen values of This: A is copied
virtual const double & operator()(const tLVectorIndex &i, const tLVectorIndex &j) const =0
get the element at row i & column j
void init(const double v)
init the values to v
Definition: LAP_DoubleMatrix.h:119
void add(const tLVectorIndex &i, const tLVectorIndex &j, const double &v)
add v to the view term (i,j) of the matrix
Definition: LAP_DoubleMatrix.h:139
#define tLVectorIndex
Definition: lapack_types.h:13
static SP::LAP_DoubleVector New()
create a new vector of double
Definition: LAP_DoubleVector.h:57
#define tString
Definition: types.h:49
tBoolean copy(SPC::LAP_DoubleMatrix x)
copy a matrix
Definition: LAP_DoubleMatrix.h:101
virtual void vectorProduct(SPC::LAP_DoubleVector &X, SP::LAP_DoubleVector &Y) const
rcompute Y= This X
Definition: LAP_DoubleMatrix.h:346
tLVectorIndex getNullValuesNumber() const
get the number of null values
Definition: LAP_DoubleMatrix.h:276
virtual void setValues(SP::LAP_DoubleVector v)
set the values of the matrix
Definition: LAP_DoubleMatrix.h:153
double getFillRate() const
get the fill rate of the matrix
Definition: LAP_DoubleMatrix.h:271
tBoolean copy(LAP_DoubleMatrix *x)
copy a matrix
Definition: LAP_DoubleMatrix.h:108
virtual tLVectorIndex getColumnsNumber() const
get the columns number of the matrix from view
Definition: LAP_Matrix.h:121
virtual void getColumn(const tLVectorIndex &j, SP::LAP_DoubleVector v) const
get the j-th column in a vector
Definition: LAP_DoubleMatrix.h:208
this class describes a general matrix for lapack used : the matrix is stored in column mValues={T_{0...
Definition: LAP_Matrix.h:15
#define tLVectorIncrement
Definition: lapack_types.h:16
virtual const T & get(const tLVectorIndex &i) const
get element at index i taking into account the view
Definition: LAP_Vector.h:513
void setStorage(SP::LAP_DoubleMatrixStorage s)
set the storage
Definition: LAP_DoubleMatrix.h:147
Definition: LAP_DoubleMatrixStorage.h:16
#define tReal
Definition: types.h:18
virtual tLVectorIndex getRowsNumber() const
get the lines number of the matrix from view
Definition: LAP_Matrix.h:116
virtual SP::LAP_DoubleVector vectorProduct(const LAP_DoubleVector &X) const
compute This. X
Definition: LAP_DoubleMatrix.h:328
virtual tString toString() const
print the matrix taking into account the view
Definition: LAP_DoubleMatrix.cpp:32
void setSize(const tLVectorIndex &n)
set the view to [0,n[ by 1 increment if values is too small, re-alocate it
Definition: LAP_Vector.h:360
const double & operator[](const tLVectorIndex &index) const
get the element at index
Definition: LAP_DoubleMatrix.h:55
#define tFlag
Definition: types.h:14