queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
TeuchosMatrix.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-2017 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_TEUCHOS_MATRIX_H
26 #define UQ_TEUCHOS_MATRIX_H
27 
28 #ifdef QUESO_HAS_TRILINOS
29 #include <queso/TeuchosVector.h>
30 #include <Teuchos_SerialDenseVector.hpp>
31 #include <Teuchos_SerialDenseMatrix.hpp>
32 #include <Teuchos_LAPACK.hpp>
33 #endif
34 
35 #include <queso/Matrix.h>
36 
37 #ifdef QUESO_HAS_TRILINOS
38 
39 namespace QUESO {
40 
53 class TeuchosMatrix : public Matrix
54 {
55 public:
57 
58 
61  const Map& map,
62  unsigned int numCols);
63 
65  TeuchosMatrix(const BaseEnvironment& env,
66  const Map& map,
67  double diagValue);
68 
71  double diagValue);
72 
74 
75  TeuchosMatrix(const TeuchosVector& v);
76 
78  TeuchosMatrix(const TeuchosMatrix& B);
79 
83 
85 
88 
90  TeuchosMatrix& operator*=(double a);
91 
93  TeuchosMatrix& operator/=(double a);
94 
100 
102 
103  double& operator()(unsigned int i, unsigned int j);
105 
107  const double& operator()(unsigned int i, unsigned int j) const;
109 
111 
112  unsigned int numRowsLocal () const;
114 
116  unsigned int numRowsGlobal () const;
117 
119  unsigned int numCols () const;
120 
122  double* values () ;//added by Kemelli on 12/04/12
123 
125  int stride () ;//added by Kemelli on 12/04/12
126 
128  double max () const;
129 
131 
132  unsigned int rank (double absoluteZeroThreshold, double relativeZeroThreshold) const;
133 
135  TeuchosMatrix transpose () const;
136 
138  TeuchosMatrix inverse () const;
139 
141  double determinant () const;
142 
144  double lnDeterminant () const;
146 
148 
149  double normFrob () const;
151 
153  double normMax () const;
155 
157 
158 
160  int chol () ;
161 
163  int svd (TeuchosMatrix& matU, TeuchosVector& vecS, TeuchosMatrix& matVt) const;
164 
166 
167  const TeuchosMatrix& svdMatU () const;
168 
170 
171  const TeuchosMatrix& svdMatV () const;
172 
174 
178  int svdSolve (const TeuchosVector& rhsVec, TeuchosVector& solVec) const;
179 
181 
184  int svdSolve (const TeuchosMatrix& rhsMat, TeuchosMatrix& solMat) const;
185 
186 
187 
189  TeuchosVector multiply (const TeuchosVector& x) const;
190 
192 
194  void invertMultiply (const TeuchosVector& b, TeuchosVector& x) const;
195 
197 
198  TeuchosVector invertMultiply (const TeuchosVector& b) const;
199 
201 
202  void invertMultiply (const TeuchosMatrix& B, TeuchosMatrix& X) const;
203 
205 
206  TeuchosMatrix invertMultiply (const TeuchosMatrix& B) const;
207 
209 
210  void invertMultiplyForceLU (const TeuchosVector& b, TeuchosVector& x) const;
211 
213 
216 
218  void eigen (TeuchosVector& eigenValues, TeuchosMatrix* eigenVectors) const;
219 
221  void largestEigen (double& eigenValue, TeuchosVector& eigenVector) const;
222 
224  void smallestEigen (double& eigenValue, TeuchosVector& eigenVector) const;
225 
227 
229 
230 
232  void cwSet (double value);
233 
235  void cwSet (unsigned int rowId, unsigned int colId, const TeuchosMatrix& mat);
236 
237 
239  void getColumn (const unsigned int column_num, TeuchosVector& column) const;
240 
242  TeuchosVector getColumn (const unsigned int column_num) const;
243 
245  void setColumn (const unsigned int column_num, const TeuchosVector& column);
246 
248  void getRow (const unsigned int row_num, TeuchosVector& row) const;
249 
251  TeuchosVector getRow (const unsigned int row_num) const;
252 
254  void setRow (const unsigned int row_num, const TeuchosVector& row);
255 
257 
259  void zeroLower (bool includeDiagonal = false);
260 
262 
264  void zeroUpper (bool includeDiagonal = false);
265 
267 
268  void filterSmallValues (double thresholdValue);
269 
271 
272  void filterLargeValues (double thresholdValue);
273 
275  void fillWithTranspose (const TeuchosMatrix& mat);
276 
278  void fillWithBlocksDiagonally (const std::vector<const TeuchosMatrix* >& matrices);
279 
281  void fillWithBlocksDiagonally (const std::vector< TeuchosMatrix* >& matrices);
282 
284  void fillWithBlocksHorizontally(const std::vector<const TeuchosMatrix* >& matrices);
285 
287  void fillWithBlocksHorizontally(const std::vector< TeuchosMatrix* >& matrices);
288 
290  void fillWithBlocksVertically (const std::vector<const TeuchosMatrix* >& matrices);
291 
293  void fillWithBlocksVertically (const std::vector< TeuchosMatrix* >& matrices);
294 
296  void fillWithTensorProduct (const TeuchosMatrix& mat1, const TeuchosMatrix& mat2);
297 
299  void fillWithTensorProduct (const TeuchosMatrix& mat1, const TeuchosVector& vec2);
301 
302 
304 
305 
306  void mpiSum (const MpiComm& comm, TeuchosMatrix& M_global) const;
307 
308  void matlabLinearInterpExtrap (const TeuchosVector& x1Vec, const TeuchosMatrix& y1Mat, const TeuchosVector& x2Vec);
310 
312 
313  void print (std::ostream& os) const;
315 
317  void subReadContents (const std::string& fileName,
318  const std::string& fileType,
319  const std::set<unsigned int>& allowedSubEnvIds);
320 
322  void subWriteContents (const std::string& varNamePrefix,
323  const std::string& fileName,
324  const std::string& fileType,
325  const std::set<unsigned int>& allowedSubEnvIds) const;
327 
328 private:
330 
331  TeuchosMatrix();
332 
334  void copy (const TeuchosMatrix& src);
335 
337  void resetLU ();
338 
340  void multiply (const TeuchosVector& x, TeuchosVector& y) const;
341 
343 
349  int internalSvd () const;
350 
352  Teuchos::SerialDenseMatrix<int,double> m_mat;
353 
355  mutable Teuchos::SerialDenseMatrix<int,double> m_LU;
356 
359 
361  mutable Map* m_svdColMap;
362 
365 
368 
371 
374 
376  mutable double m_determinant;
377 
379  mutable double m_lnDeterminant;
380 
382 
384  mutable int* v_pivoting;
385 
386 
387  mutable int m_signum;
388 
390  mutable bool m_isSingular;
391 };
392 
393 TeuchosMatrix operator* (double a, const TeuchosMatrix& mat);
394 TeuchosVector operator* (const TeuchosMatrix& mat, const TeuchosVector& vec);
395 TeuchosMatrix operator* (const TeuchosMatrix& m1, const TeuchosMatrix& m2 );
396 TeuchosMatrix operator+ (const TeuchosMatrix& m1, const TeuchosMatrix& m2 );
397 TeuchosMatrix operator- (const TeuchosMatrix& m1, const TeuchosMatrix& m2 );
401 std::ostream& operator<< (std::ostream& os, const TeuchosMatrix& obj);
402 
403 } // End namespace QUESO
404 
405 #endif // ifdef QUESO_HAS_TRILINOS
406 
407 #endif // UQ_TEUCHOS_MATRIX_H
void cwSet(double value)
Component-wise set all values to this with value.
TeuchosMatrix & operator-=(const TeuchosMatrix &rhs)
Stores in this the coordinate-wise subtraction of this by rhs.
unsigned int numRowsLocal() const
Returns the local row dimension of this matrix.
void fillWithBlocksHorizontally(const std::vector< const TeuchosMatrix * > &matrices)
This function fills this matrix horizontally with const block matrices.
TeuchosMatrix()
Default Constructor.
double normFrob() const
Returns the Frobenius norm of this matrix.
int svd(TeuchosMatrix &matU, TeuchosVector &vecS, TeuchosMatrix &matVt) const
Checks for the dimension of this matrix, matU, VecS and matVt, and calls the protected routine intern...
int stride()
Returns the stride between the columns of this matrix in memory.
A class for partitioning vectors and matrices.
Definition: Map.h:49
const BaseEnvironment & env() const
Definition: Matrix.C:47
const TeuchosMatrix & svdMatV() const
This function calls private member TeuchosMatrix::internalSvd() to set a N-by-N orthogonal square mat...
Map * m_svdColMap
Mapping for matrices involved in the singular value decomposition (svd) routine.
void invertMultiplyForceLU(const TeuchosVector &b, TeuchosVector &x) const
This function calculates the inverse of this matrix, multiplies it with vector b and stores the resul...
void eigen(TeuchosVector &eigenValues, TeuchosMatrix *eigenVectors) const
This function computes the eigenvalues of a real symmetric matrix.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:198
Teuchos::SerialDenseMatrix< int, double > m_LU
Teuchos matrix for the LU decomposition of m_mat.
TeuchosMatrix * m_svdVmat
m_svdVmat stores the N-by-N orthogonal square matrix V after the singular value decomposition of a ma...
TeuchosMatrix * m_svdUmat
m_svdUmat stores the M-by-N orthogonal matrix U after the singular value decomposition of a matrix...
double m_lnDeterminant
The natural logarithm of the determinant of this matrix.
~TeuchosMatrix()
Destructor.
const TeuchosMatrix & svdMatU() const
This function calls private member TeuchosMatrix::internalSvd() to set a M-by-N orthogonal matrix U o...
int chol()
Computes Cholesky factorization of this, a real symmetric positive definite matrix.
void setColumn(const unsigned int column_num, const TeuchosVector &column)
This function copies vector column into the column_num-th column of this matrix.
TeuchosMatrix & operator*=(double a)
Stores in this the coordinate-wise multiplication of this and a.
std::ostream & operator<<(std::ostream &os, const SequenceStatisticalOptions &obj)
TeuchosMatrix inverse() const
This function calculates the inverse of this matrix (square).
unsigned int numRowsGlobal() const
Returns the global row dimension of this matrix.
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
Definition: GslMatrix.C:2029
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.
void fillWithTranspose(const TeuchosMatrix &mat)
This function stores the transpose of this matrix into this matrix.
TeuchosMatrix * m_inverse
Stores the inverse of this matrix.
TeuchosMatrix * m_permutation
void setRow(const unsigned int row_num, const TeuchosVector &row)
This function copies vector row into the row_num-th column of this matrix.
void zeroUpper(bool includeDiagonal=false)
This function sets all the entries bellow the main diagonal (inclusive or not) of this matrix to zero...
void largestEigen(double &eigenValue, TeuchosVector &eigenVector) const
This function finds largest eigenvalue, namely eigenValue, of this matrix and its corresponding eigen...
TeuchosVector multiply(const TeuchosVector &x) const
This function multiplies this matrix by vector x and returns a vector.
void getRow(const unsigned int row_num, TeuchosVector &row) const
This function gets the row_num-th column of this matrix and stores it into vector row...
double determinant() const
Calculates the determinant of this matrix.
void getColumn(const unsigned int column_num, TeuchosVector &column) const
This function gets the column_num-th column of this matrix and stores it into vector column...
double normMax() const
Returns the Frobenius norm of this matrix.
int internalSvd() const
This function factorizes the M-by-N matrix A into the singular value decomposition A = U S V^T for M ...
void zeroLower(bool includeDiagonal=false)
This function sets all the entries above the main diagonal (inclusive or not) of this matrix to zero...
double * values()
Returns a pointer to the first element of this matrix.
TeuchosMatrix & operator+=(const TeuchosMatrix &rhs)
Stores in this the coordinate-wise addition of this and rhs.
int * v_pivoting
The pivoting vector of a LU decomposition.
Class for matrix operations (virtual).
Definition: Matrix.h:46
TeuchosVector * m_svdSvec
m_svdSvec stores the diagonal of the N-by-N diagonal matrix of singular values S after the singular v...
GslMatrix operator*(double a, const GslMatrix &mat)
Definition: GslMatrix.C:1982
void matlabLinearInterpExtrap(const TeuchosVector &x1Vec, const TeuchosMatrix &y1Mat, const TeuchosVector &x2Vec)
void filterSmallValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are smaller than thresholdValue...
Teuchos::SerialDenseMatrix< int, double > m_mat
Teuchos matrix, also referred to as this matrix.
TeuchosMatrix & operator=(const TeuchosMatrix &rhs)
Copies values from matrix rhs to this.
TeuchosMatrix transpose() const
This function calculates the transpose of this matrix (square).
Class for vector operations using Teuchos (Trilinos).
Definition: TeuchosVector.h:55
void resetLU()
In this function resets the LU decomposition of this matrix, as well as deletes the private member po...
TeuchosMatrix * m_svdVTmat
m_svdVmatT stores the transpose of N-by-N orthogonal square matrix V, namely V^T, after the singular ...
bool m_isSingular
Indicates whether or not this matrix is singular.
void fillWithBlocksVertically(const std::vector< const TeuchosMatrix * > &matrices)
This function fills this matrix vertically with const block matrices.
double m_determinant
The determinant of this matrix.
GslMatrix matrixProduct(const GslVector &v1, const GslVector &v2)
Definition: GslMatrix.C:2036
void fillWithTensorProduct(const TeuchosMatrix &mat1, const TeuchosMatrix &mat2)
This function calculates the tensor product of matrices mat1 and mat2 and stores it in this matrix...
double max() const
Returns the maximum element value of the matrix.
void mpiSum(const MpiComm &comm, TeuchosMatrix &M_global) const
double lnDeterminant() const
Calculates the ln(determinant) of this matrix.
unsigned int numCols() const
Returns the column dimension of this matrix.
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
void print(std::ostream &os) const
Print method. Defines the behavior of the ostream &lt;&lt; operator inherited from the Object class...
void subReadContents(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds)
Read contents of subenvironment from file fileName.
TeuchosMatrix & operator/=(double a)
Stores in this the coordinate-wise division of this by a.
void copy(const TeuchosMatrix &src)
In this function this matrix receives a copy of matrix src.
double & operator()(unsigned int i, unsigned int j)
Element access method (non-const).
GslMatrix leftDiagScaling(const GslVector &vec, const GslMatrix &mat)
Definition: GslMatrix.C:2052
int svdSolve(const TeuchosVector &rhsVec, TeuchosVector &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 TeuchosMatrix::svd (x=solVec, b=rhsVec).
const Map & map() const
Definition: Matrix.C:54
unsigned int rank(double absoluteZeroThreshold, double relativeZeroThreshold) const
This function returns the number of singular values of this matrix (rank).
GslMatrix rightDiagScaling(const GslMatrix &mat, const GslVector &vec)
Definition: GslMatrix.C:2073
void smallestEigen(double &eigenValue, TeuchosVector &eigenVector) const
This function finds smallest eigenvalue, namely eigenValue, of this matrix and its corresponding eige...
void invertMultiply(const TeuchosVector &b, TeuchosVector &x) const
This function calculates the inverse of this matrix, multiplies it with vector b and stores the resul...
Class for matrix operations using Teuchos (Trilinos).
Definition: TeuchosMatrix.h:53
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
Definition: GslMatrix.C:2022
void filterLargeValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are greater than thresholdValue...
void fillWithBlocksDiagonally(const std::vector< const TeuchosMatrix * > &matrices)
This function fills this matrix diagonally with const block matrices.

Generated on Tue Jun 5 2018 19:48:54 for queso-0.57.1 by  doxygen 1.8.5