ParMooN
 All Classes Functions Variables Friends Pages
Matrix.h
1 // =======================================================================
2 // @(#)Matrix.h 1.2 11/20/98
3 //
4 // Class: TMatrix
5 //
6 // Purpose: store a matrix (ansatz != test space)
7 //
8 // Author: Gunar Matthies
9 //
10 // History: 26.08.1998 start implementation
11 //
12 // =======================================================================
13 
14 #ifndef __MATRIX__
15 #define __MATRIX__
16 
17 #include <Structure.h>
18 #include <string>
19 #include <map>
20 
21 class TMatrix
22 {
23  protected:
26 
28  double *Entries;
29 
30  public:
33 
36 
40 
42  TMatrix(int nRows, int nCols);
43 
45  ~TMatrix();
46 
48  void Reset();
49 
51  int GetN_Rows() const
52  { return structure->GetN_Rows(); }
53 
55  int GetN_Columns() const
56  { return structure->GetN_Columns(); }
57 
59  int GetN_Entries() const
60  { return structure->GetN_Entries(); }
61 
63  int GetHangingN_Entries() const
64  { return structure->GetHangingN_Entries(); }
65 
67  int *GetKCol() const
68  { return structure->GetKCol(); }
69 
71  int *GetHangingKCol() const
72  { return structure->GetHangingKCol(); }
73 
75  int *GetHangingRowPtr() const
76  { return structure->GetHangingRowPtr(); }
77 
79  int *GetRowPtr() const
80  { return structure->GetRowPtr(); }
81 
84  { return structure; }
85 
87  double *GetEntries() const
88  { return Entries; }
89 
98  double GetNorm(int p=-1) const;
99 
101  int Write(const char *filename) const;
102 
104  void Print(const char *name = "a") const;
105 
110  void PrintFull(std::string name="", int fieldWidth=4) const;
111 
112  // add a value at selected entry
113  void add(int i,int j, double val);
114  // add values in row 'i' given by the map 'vals', multiplied by 'factor'
115  // this should be faster than adding all values in 'vals' individually
116  void add(int i, std::map<int,double> vals, double factor = 1.0);
117  // add values 'vals[i][j]' to this matrix at the positions (i,j) for all
118  // i,j defined in the map 'vals'
119  void add(std::map<int, std::map<int,double> > vals, double factor = 1.0);
120  // set a value at selected entry
121  void set(int i, int j, double val);
122  // get a value at selected entry
123  const double& get(int i,int j) const;
124  // get a value at selected entry (you may change that value (similar to set)
125  double& get(int i,int j);
126 
127  // set the whole elements array
128  void setEntries(double* entries) {
129  this->Entries = entries;
130  }
131 
138  TMatrix* GetTransposed() const;
139 
154  void changeRows(std::map<int,std::map<int,double> > entries,
155  bool deleteOldArrays=false);
156 
157 
162  friend double* operator*(const TMatrix & A, const double* x);
163 
169  virtual TMatrix & operator+=(const TMatrix * A);
175  virtual TMatrix & operator-=(const TMatrix * A);
182  virtual TMatrix & operator+=(const TMatrix & A)
183  { *this += &A; return *this; }
185  virtual TMatrix & operator*=(const double a);
191  TMatrix & operator=(const TMatrix& A);
192 
202  void multiply(const double * const x, double *y, double a = 1.0) const;
203 
216  TMatrix* multiply(const TMatrix * const B, double a = 1.0) const;
217 
236  void scale(const double * const factor, bool from_left = true);
237 
250  void remove_zeros(double tol = 0.0);
251 
256  double & operator()(const int i, const int j);
261  const double & operator()(const int i, const int j) const;
262 };
263 
264 #endif
int GetN_Columns() const
Definition: Matrix.h:55
int * GetHangingRowPtr() const
Definition: Structure.h:91
void Print(const char *name="a") const
Print matrix into the shell.
Definition: Matrix.C:61
int * GetHangingRowPtr() const
Definition: Matrix.h:75
int GetN_Rows() const
Definition: Structure.h:63
Definition: Matrix.h:21
int GetN_Rows() const
Definition: Matrix.h:51
void SetStructure(TStructure *structure)
reset the structure, this may mean that the entries need to be reallocated
Definition: Matrix.C:41
virtual TMatrix & operator+=(const TMatrix &A)
add another matrix to this one
Definition: Matrix.h:182
void PrintFull(std::string name="", int fieldWidth=4) const
print the full matrix, including all zeros
Definition: Matrix.C:82
int GetHangingN_Entries() const
Definition: Structure.h:75
double * GetEntries() const
Definition: Matrix.h:87
void scale(const double *const factor, bool from_left=true)
scale a matrix using a vector
Definition: Matrix.C:438
void remove_zeros(double tol=0.0)
remove all entries from sparsity structure where a zero is stored
Definition: Matrix.C:406
int GetN_Entries() const
Definition: Matrix.h:59
int * GetRowPtr() const
Definition: Matrix.h:79
void changeRows(std::map< int, std::map< int, double > > entries, bool deleteOldArrays=false)
replace several rows in the matrix with new entries.
Definition: Matrix.C:476
double GetNorm(int p=-1) const
Definition: Matrix.C:197
int Write(const char *filename) const
int * GetRowPtr() const
Definition: Structure.h:87
int * GetHangingKCol() const
Definition: Matrix.h:71
int * GetKCol() const
Definition: Structure.h:79
TStructure * structure
Definition: Matrix.h:25
double * Entries
Definition: Matrix.h:28
~TMatrix()
Definition: Matrix.C:55
virtual TMatrix & operator+=(const TMatrix *A)
add another matrix to this one
Definition: Matrix.C:274
void multiply(const double *const x, double *y, double a=1.0) const
compute y += a * A*x
Definition: Matrix.C:334
int * GetHangingKCol() const
Definition: Structure.h:83
friend double * operator*(const TMatrix &A, const double *x)
compute y = A*x (Matrix-Vector-Multiplication)
Definition: Matrix.C:247
TStructure * GetStructure() const
Definition: Matrix.h:83
virtual TMatrix & operator*=(const double a)
scale matrix by a factor
Definition: Matrix.C:469
int * GetKCol() const
Definition: Matrix.h:67
int GetN_Entries() const
Definition: Structure.h:71
TMatrix * GetTransposed() const
return a new TMatrix which is the transposed of this matrix
Definition: Structure.h:19
void Reset()
Definition: Matrix.C:50
TMatrix & operator=(const TMatrix &A)
copy entries from A to this
Definition: Matrix.C:313
virtual TMatrix & operator-=(const TMatrix *A)
substract another matrix to this one
Definition: Matrix.C:294
int GetHangingN_Entries() const
Definition: Matrix.h:63
int GetN_Columns() const
Definition: Structure.h:67
double & operator()(const int i, const int j)
get/set a specific matrix entry
Definition: Matrix.C:187
TMatrix(TStructure *structure)
Definition: Matrix.C:22