queso-0.55.0
GslMatrix.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #ifndef UQ_GSL_MATRIX_H
26 #define UQ_GSL_MATRIX_H
27 
32 #include <queso/Defines.h>
33 #include <queso/GslVector.h>
34 #include <queso/Matrix.h>
35 
36 #include <gsl/gsl_matrix.h>
37 #include <gsl/gsl_permutation.h>
38 
39 namespace QUESO {
40 
49 class GslMatrix : public Matrix
50 {
51 public:
53 
54 
57  const Map& map,
58  unsigned int numCols);
59 
61  GslMatrix(const BaseEnvironment& env,
62  const Map& map,
63  double diagValue); // MATLAB eye
64 
66  GslMatrix(const GslVector& v,
67  double diagValue); // MATLAB eye
68 
70 
71  GslMatrix(const GslVector& v); // MATLAB diag
72 
74 
75  GslMatrix(const GslMatrix& B);
76 
78  ~GslMatrix();
80 
81 
83 
84  GslMatrix& operator= (const GslMatrix& rhs);
86 
88  GslMatrix& operator*=(double a);
89 
91  GslMatrix& operator/=(double a);
92 
94  GslMatrix& operator+=(const GslMatrix& rhs);
95 
97  GslMatrix& operator-=(const GslMatrix& rhs);
99 
100 
102 
103  double& operator()(unsigned int i, unsigned int j)
105  {
106  // Changing the matrix changes its decomposition(s). We'll
107  // invalidate for now; recalculate later if needed.
108  this->resetLU();
109  queso_require_less_msg(i, m_mat->size1, "i is too large");
110  queso_require_less_msg(j, m_mat->size2, "j is too large");
111  return *gsl_matrix_ptr(m_mat,i,j);
112  }
113 
115  const double& operator()(unsigned int i, unsigned int j) const
116  {
117  queso_require_less_msg(i, m_mat->size1, "i is too large");
118  queso_require_less_msg(j, m_mat->size2, "j is too large");
119  return *gsl_matrix_ptr(m_mat,i,j);
120  }
121 
122 
124 
126 
127  unsigned int numRowsLocal () const;
129 
131  unsigned int numRowsGlobal () const;
132 
134  unsigned int numCols () const;
135 
137  double max () const;
138 
140 
141  unsigned int rank (double absoluteZeroThreshold, double relativeZeroThreshold) const;
142 
144  GslMatrix transpose () const;
145 
147  GslMatrix inverse () const;
148 
149 
151  double determinant () const;
152 
154  double lnDeterminant () const;
155 
157 
159 
160  double normFrob () const;
162 
164  double normMax () const;
166 
167 
168 
169 
170 
172 
173 
175 
176  int chol ();
177 
179  int svd (GslMatrix& matU, GslVector& vecS, GslMatrix& matVt) const;
180 
182 
183  const GslMatrix& svdMatU () const;
184 
186 
187  const GslMatrix& svdMatV () const;
188 
190  int svdSolve (const GslVector& rhsVec, GslVector& solVec) const;
191 
193  int svdSolve (const GslMatrix& rhsMat, GslMatrix& solMat) const;
194 
195 
196 
198  GslVector multiply (const GslVector& x) const;
199 
201 
202  GslVector invertMultiply (const GslVector& b) const;
203 
205 
207  void invertMultiply (const GslVector& b, GslVector& x) const;
208 
210 
211  GslMatrix invertMultiply (const GslMatrix& B) const;
212 
214 
216  void invertMultiply (const GslMatrix& B, GslMatrix& X) const;
217 
219 
221  GslVector invertMultiplyForceLU (const GslVector& b) const;
222 
224 
225  void invertMultiplyForceLU (const GslVector& b, GslVector& x) const;
226 
228  void getColumn (const unsigned int column_num, GslVector& column) const;
229 
231  GslVector getColumn (const unsigned int column_num) const;
232 
234  void setColumn (const unsigned int column_num, const GslVector& column);
235 
237  void getRow (const unsigned int row_num, GslVector& row) const;
238 
240  GslVector getRow (const unsigned int row_num) const;
241 
243  void setRow (const unsigned int row_num, const GslVector& row);
244 
246  void eigen (GslVector& eigenValues, GslMatrix* eigenVectors) const;
247 
249  void largestEigen (double& eigenValue, GslVector& eigenVector) const;
250 
252  void smallestEigen (double& eigenValue, GslVector& eigenVector) const;
253 
254 
256 
258 
259 
261  void cwSet (double value);
262 
264  void cwSet (unsigned int rowId, unsigned int colId, const GslMatrix& mat);
265 
266  void cwExtract (unsigned int rowId, unsigned int colId, GslMatrix& mat) const;
267 
269 
271  void zeroLower (bool includeDiagonal = false);
272 
274 
276  void zeroUpper (bool includeDiagonal = false);
277 
279 
280  void filterSmallValues (double thresholdValue);
281 
283 
284  void filterLargeValues (double thresholdValue);
285 
287  void fillWithTranspose (unsigned int rowId,
288  unsigned int colId,
289  const GslMatrix& mat,
290  bool checkForExactNumRowsMatching,
291  bool checkForExactNumColsMatching);
292 
294  void fillWithBlocksDiagonally (unsigned int rowId,
295  unsigned int colId,
296  const std::vector<const GslMatrix* >& matrices,
297  bool checkForExactNumRowsMatching,
298  bool checkForExactNumColsMatching);
299 
301  void fillWithBlocksDiagonally (unsigned int rowId,
302  unsigned int colId,
303  const std::vector< GslMatrix* >& matrices,
304  bool checkForExactNumRowsMatching,
305  bool checkForExactNumColsMatching);
306 
308  void fillWithBlocksHorizontally(unsigned int rowId,
309  unsigned int colId,
310  const std::vector<const GslMatrix* >& matrices,
311  bool checkForExactNumRowsMatching,
312  bool checkForExactNumColsMatching);
313 
315  void fillWithBlocksHorizontally(unsigned int rowId,
316  unsigned int colId,
317  const std::vector< GslMatrix* >& matrices,
318  bool checkForExactNumRowsMatching,
319  bool checkForExactNumColsMatching);
320 
322  void fillWithBlocksVertically (unsigned int rowId,
323  unsigned int colId,
324  const std::vector<const GslMatrix* >& matrices,
325  bool checkForExactNumRowsMatching,
326  bool checkForExactNumColsMatching);
327 
329  void fillWithBlocksVertically (unsigned int rowId,
330  unsigned int colId,
331  const std::vector< GslMatrix* >& matrices,
332  bool checkForExactNumRowsMatching,
333  bool checkForExactNumColsMatching);
334 
336  void fillWithTensorProduct (unsigned int rowId,
337  unsigned int colId,
338  const GslMatrix& mat1,
339  const GslMatrix& mat2,
340  bool checkForExactNumRowsMatching,
341  bool checkForExactNumColsMatching);
342 
344  void fillWithTensorProduct (unsigned int rowId,
345  unsigned int colId,
346  const GslMatrix& mat1,
347  const GslVector& vec2,
348  bool checkForExactNumRowsMatching,
349  bool checkForExactNumColsMatching);
351 
352 
354 
355  gsl_matrix* data ();
357 
358  void mpiSum (const MpiComm& comm, GslMatrix& M_global) const;
359 
360  void matlabLinearInterpExtrap (const GslVector& x1Vec, const GslMatrix& y1Mat, const GslVector& x2Vec);
362 
363 
364 
366 
367 
369  void print (std::ostream& os) const;
370 
372  void subWriteContents (const std::string& varNamePrefix,
373  const std::string& fileName,
374  const std::string& fileType,
375  const std::set<unsigned int>& allowedSubEnvIds) const;
376 
378  void subReadContents (const std::string& fileName,
379  const std::string& fileType,
380  const std::set<unsigned int>& allowedSubEnvIds);
382 
383 private:
385 
386  GslMatrix();
387 
389  void copy (const GslMatrix& src);
390 
392  void resetLU ();
393 
395  void multiply (const GslVector& x, GslVector& y) const;
396 
398  int internalSvd () const;
399 
401  gsl_matrix* m_mat;
402 
404  mutable gsl_matrix* m_LU;
405 
408 
410  mutable Map* m_svdColMap;
411 
413 
417 
419 
424 
426 
430 
432 
436 
438  mutable double m_determinant;
439 
441  mutable double m_lnDeterminant;
442 
444 
447  mutable gsl_permutation* m_permutation;
448 
450 
453  mutable int m_signum;
454 
456  mutable bool m_isSingular;
457 };
458 
459 GslMatrix operator* (double a, const GslMatrix& mat);
460 GslVector operator* (const GslMatrix& mat, const GslVector& vec);
461 GslMatrix operator* (const GslMatrix& m1, const GslMatrix& m2 );
462 GslMatrix operator+ (const GslMatrix& m1, const GslMatrix& m2 );
463 GslMatrix operator- (const GslMatrix& m1, const GslMatrix& m2 );
464 GslMatrix matrixProduct (const GslVector& v1, const GslVector& v2 );
465 
466 // Row \c i of the returned matrix is equal to row \c i of \c mat multiplied by element \c i of \c v.
467 GslMatrix leftDiagScaling (const GslVector& vec, const GslMatrix& mat);
468 
469 // Column \c j of the returned matrix is equal to column \c j of \c mat multiplied by element \c j of \c v.
470 GslMatrix rightDiagScaling(const GslMatrix& mat, const GslVector& vec);
471 std::ostream& operator<< (std::ostream& os, const GslMatrix& obj);
472 
473 } // End namespace QUESO
474 
475 #endif // UQ_GSL_MATRIX_H
GslMatrix transpose() const
This function calculated the transpose of this matrix (square).
Definition: GslMatrix.C:693
void cwSet(double value)
Component-wise set all values to this with value.
Definition: GslMatrix.C:347
int chol()
Computes Cholesky factorization of a real symmetric positive definite matrix this.
Definition: GslMatrix.C:408
const GslMatrix & svdMatU() const
This function calls private member GslMatrix::internalSvd() to set a M-by-N orthogonal matrix U of th...
Definition: GslMatrix.C:512
double normFrob() const
Returns the Frobenius norm of this matrix.
Definition: GslMatrix.C:293
void mpiSum(const MpiComm &comm, GslMatrix &M_global) const
Definition: GslMatrix.C:1663
void setRow(const unsigned int row_num, const GslVector &row)
This function copies vector row into the row_num-th column of this matrix.
Definition: GslMatrix.C:1629
GslMatrix & operator-=(const GslMatrix &rhs)
Stores in this the coordinate-wise subtraction of this by rhs.
Definition: GslMatrix.C:207
GslMatrix & operator*=(double a)
Stores in this the coordinate-wise multiplication of this and a.
Definition: GslMatrix.C:177
GslMatrix()
Default Constructor.
void setColumn(const unsigned int column_num, const GslVector &column)
This function copies vector column into the column_num-th column of this matrix.
Definition: GslMatrix.C:1646
Map * m_svdColMap
Mapping for matrices involved in the singular value decomposition (svd) routine.
Definition: GslMatrix.h:410
void zeroUpper(bool includeDiagonal=false)
This function sets all the entries above the main diagonal of this matrix to zero.
Definition: GslMatrix.C:619
double m_determinant
The determinant of this matrix.
Definition: GslMatrix.h:438
Class for matrix operations (virtual).
Definition: Matrix.h:46
void subWriteContents(const std::string &varNamePrefix, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
Write contents of subenvironment in file fileName.
Definition: GslMatrix.C:1766
bool m_isSingular
Indicates whether or not this matrix is singular.
Definition: GslMatrix.h:456
A class for partitioning vectors and matrices.
Definition: Map.h:49
void cwExtract(unsigned int rowId, unsigned int colId, GslMatrix &mat) const
Definition: GslMatrix.C:385
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
unsigned int numCols() const
Returns the column dimension of this matrix.
Definition: GslMatrix.C:287
const Map & map() const
Definition: Matrix.C:54
int m_signum
m_signum stores the sign of the permutation of the LU decomposition PA = LU.
Definition: GslMatrix.h:453
void zeroLower(bool includeDiagonal=false)
This function sets all the entries bellow the main diagonal of this matrix to zero.
Definition: GslMatrix.C:591
GslMatrix matrixProduct(const GslVector &v1, const GslVector &v2)
Definition: GslMatrix.C:2001
unsigned int numRowsLocal() const
Returns the local row dimension of this matrix.
Definition: GslMatrix.C:275
void fillWithBlocksVertically(unsigned int rowId, unsigned int colId, const std::vector< const GslMatrix * > &matrices, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function fills this matrix vertically with const block matrices.
Definition: GslMatrix.C:887
GslMatrix operator*(double a, const GslMatrix &mat)
Definition: GslMatrix.C:1947
void print(std::ostream &os) const
Print method. Defines the behavior of the ostream &lt;&lt; operator inherited from the Object class...
Definition: GslMatrix.C:1737
double m_lnDeterminant
The natural logarithm of the determinant of this matrix.
Definition: GslMatrix.h:441
int svd(GslMatrix &matU, GslVector &vecS, GslMatrix &matVt) const
Checks for the dimension of this matrix, matU, VecS and matVt, and calls the protected routine intern...
Definition: GslMatrix.C:435
double lnDeterminant() const
Calculates the ln(determinant) of this matrix.
Definition: GslMatrix.C:1077
int internalSvd() const
This function factorizes the M-by-N matrix A into the singular value decomposition A = U S V^T for M ...
Definition: GslMatrix.C:532
void fillWithTensorProduct(unsigned int rowId, unsigned int colId, const GslMatrix &mat1, const GslMatrix &mat2, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function calculates the tensor product of matrices mat1 and mat2 and stores it in this matrix...
Definition: GslMatrix.C:951
GslMatrix * m_svdVmat
m_svdVmat stores the N-by-N orthogonal square matrix V after the singular value decomposition of a ma...
Definition: GslMatrix.h:429
void matlabLinearInterpExtrap(const GslVector &x1Vec, const GslMatrix &y1Mat, const GslVector &x2Vec)
Definition: GslMatrix.C:1706
GslVector * m_svdSvec
m_svdSvec stores the diagonal of the N-by-N diagonal matrix of singular values S after the singular v...
Definition: GslMatrix.h:423
GslMatrix leftDiagScaling(const GslVector &vec, const GslMatrix &mat)
Definition: GslMatrix.C:2017
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
void fillWithTranspose(unsigned int rowId, unsigned int colId, const GslMatrix &mat, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function stores the transpose of this matrix into this matrix.
Definition: GslMatrix.C:1012
GslMatrix rightDiagScaling(const GslMatrix &mat, const GslVector &vec)
Definition: GslMatrix.C:2038
gsl_matrix * m_mat
GSL matrix, also referred to as this matrix.
Definition: GslMatrix.h:401
Class for matrix operations using GSL library.
Definition: GslMatrix.h:49
gsl_matrix * m_LU
GSL matrix for the LU decomposition of m_mat.
Definition: GslMatrix.h:404
double & operator()(unsigned int i, unsigned int j)
Element access method (non-const).
Definition: GslMatrix.h:104
const BaseEnvironment & env() const
Definition: Matrix.C:47
double max() const
Returns the maximum element value of the matrix.
Definition: GslMatrix.C:329
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
void filterLargeValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are greater than thresholdValue...
Definition: GslMatrix.C:670
gsl_permutation * m_permutation
The permutation matrix of a LU decomposition.
Definition: GslMatrix.h:447
GslMatrix * m_inverse
Inverse matrix of this.
Definition: GslMatrix.h:407
void smallestEigen(double &eigenValue, GslVector &eigenVector) const
This function finds smallest eigenvalue, namely eigenValue, of this matrix and its corresponding eige...
Definition: GslMatrix.C:1480
GslVector invertMultiplyForceLU(const GslVector &b) const
This function calculates the inverse of this matrix and multiplies it with vector b...
Definition: GslMatrix.C:1338
unsigned int numRowsGlobal() const
Returns the global row dimension of this matrix.
Definition: GslMatrix.C:281
double normMax() const
Returns the Frobenius norm of this matrix.
Definition: GslMatrix.C:311
void fillWithBlocksDiagonally(unsigned int rowId, unsigned int colId, const std::vector< const GslMatrix * > &matrices, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function fills this matrix diagonally with const block matrices.
Definition: GslMatrix.C:751
gsl_matrix * data()
Returns this matrix.
Definition: GslMatrix.C:1731
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
Definition: GslMatrix.C:1994
GslMatrix inverse() const
This function calculated the inverse of this matrix (square).
Definition: GslMatrix.C:711
void subReadContents(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds)
Read contents of subenvironment from file fileName.
Definition: GslMatrix.C:1806
void filterSmallValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are smaller than thresholdValue...
Definition: GslMatrix.C:647
GslMatrix & operator+=(const GslMatrix &rhs)
Stores in this the coordinate-wise addition of this and rhs.
Definition: GslMatrix.C:196
void getRow(const unsigned int row_num, GslVector &row) const
This function gets the row_num-th column of this matrix and stores it into vector row...
Definition: GslMatrix.C:1577
GslVector multiply(const GslVector &x) const
This function multiplies this matrix by vector x and returns the resulting vector.
Definition: GslMatrix.C:1153
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
Definition: GslMatrix.C:1987
void fillWithBlocksHorizontally(unsigned int rowId, unsigned int colId, const std::vector< const GslMatrix * > &matrices, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function fills this matrix horizontally with const block matrices.
Definition: GslMatrix.C:823
const double & operator()(unsigned int i, unsigned int j) const
Element access method (const).
Definition: GslMatrix.h:115
GslMatrix * m_svdUmat
m_svdUmat stores the M-by-N orthogonal matrix U after the singular value decomposition of a matrix...
Definition: GslMatrix.h:416
Class for vector operations using GSL library.
Definition: GslVector.h:49
const GslMatrix & svdMatV() const
This function calls private member GslMatrix::internalSvd() to set a N-by-N orthogonal square matrix ...
Definition: GslMatrix.C:522
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
void copy(const GslMatrix &src)
In this function this matrix receives a copy of matrix src.
Definition: GslMatrix.C:220
GslMatrix & operator=(const GslMatrix &rhs)
Copies values from matrix rhs to this.
Definition: GslMatrix.C:169
void resetLU()
In this function resets the LU decomposition of this matrix, as well as deletes the private member po...
Definition: GslMatrix.C:232
unsigned int rank(double absoluteZeroThreshold, double relativeZeroThreshold) const
This function returns the number of singular values of this matrix (rank).
Definition: GslMatrix.C:1118
void getColumn(const unsigned int column_num, GslVector &column) const
This function gets the column_num-th column of this matrix and stores it into vector column...
Definition: GslMatrix.C:1551
GslVector invertMultiply(const GslVector &b) const
This function calculates the inverse of this matrix and multiplies it with vector b...
Definition: GslMatrix.C:1187
GslMatrix & operator/=(double a)
Stores in this the coordinate-wise division of this by a.
Definition: GslMatrix.C:187
GslMatrix * m_svdVTmat
m_svdVmatT stores the transpose of N-by-N orthogonal square matrix V, namely V^T, after the singular ...
Definition: GslMatrix.h:435
void largestEigen(double &eigenValue, GslVector &eigenVector) const
This function finds largest eigenvalue, namely eigenValue, of this matrix and its corresponding eigen...
Definition: GslMatrix.C:1411
void eigen(GslVector &eigenValues, GslMatrix *eigenVectors) const
This function computes the eigenvalues of a real symmetric matrix.
Definition: GslMatrix.C:1385
double determinant() const
Calculates the determinant of this matrix.
Definition: GslMatrix.C:1036
int svdSolve(const GslVector &rhsVec, GslVector &solVec) const
This function solves the system A x = b using the singular value decomposition (U, S, V) of A which must have been computed previously with GslMatrix::svd (x=solVec, b=rhsVec).
Definition: GslMatrix.C:456
~GslMatrix()
Destructor.
Definition: GslMatrix.C:161

Generated on Fri Jun 17 2016 14:17:40 for queso-0.55.0 by  doxygen 1.8.5