ug4
lapack_densematrix_inverse.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2014: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__SMALL_ALGEBRA__DENSEMATRIX_INVERT_H__
34 #define __H__UG__SMALL_ALGEBRA__DENSEMATRIX_INVERT_H__
35 
36 #include "../small_matrix/densematrix.h"
37 #include "../small_matrix/densevector.h"
38 #include "../../common/operations.h"
39 
40 namespace ug
41 {
43 
49 template<typename TStorage>
51 {
52 private: // storage
54  std::vector<lapack_int> interchange;
55 
56 public:
57  inline size_t num_cols() const
58  {
59  return densemat.num_cols();
60  }
61 
62  inline size_t num_rows() const
63  {
64  return densemat.num_rows();
65  }
66 
67  inline void resize(size_t k)
68  {
69  densemat.resize(k,k);
70  densemat = 0.0;
71  }
72  inline void resize(size_t k, size_t k2)
73  {
74  UG_COND_THROW(k!=k2, "only square matrices supported");
75  resize(k);
76  }
77 
78  double &operator()(int r, int c)
79  {
80  return densemat(r,c);
81  }
82  const double &operator()(int r, int c) const
83  {
84  return densemat(r,c);
85  }
86 
87 public:
90  {
91  UG_COND_THROW(mat.num_rows() != mat.num_cols(), "only for square matrices");
92 
93  densemat = mat;
94  return invert();
95  }
96 
97  bool invert()
98  {
99  if(densemat.num_rows() == 0) return false;
100 
101  interchange.resize(densemat.num_rows());
102  int info = getrf(densemat.num_rows(), densemat.num_cols(), &densemat(0,0),
103  densemat.num_rows(), &interchange[0]);
104  if(info != 0)
105  {
106  if(info > 0)
107  {UG_THROW("ERROR in 'DenseMatrixInverse::invert': Matrix singular in U(i,i), with i="<<info<<"\n");}
108  if(info < 0)
109  {UG_THROW("ERROR in 'DenseMatrixInverse::invert': i-th argument had had illegal value, with i="<<info<<"\n");}
110  }
111  return info == 0;
112  }
113 
114  template<typename vector_t>
115  void apply(DenseVector<vector_t> &vec) const
116  {
117  if(vec.size() == 0) return;
118  int info = getrs(ModeNoTrans, num_rows(), 1, &densemat(0,0), num_rows(), &interchange[0], &vec[0], num_rows());
119  (void) info;
120  UG_COND_THROW(info != 0, "DenseMatrixInverse::mat_mult: getrs failed.");
121  }
122 
123  // todo: implement operator *=
124 
125  template<typename T> friend std::ostream &operator << (std::ostream &out, const DenseMatrixInverse<T> &mat);
126 };
127 
128 
129 template<typename T>
130 std::ostream &operator << (std::ostream &out, const DenseMatrixInverse<T> &mat)
131 {
132  out << "[ ";
133  for(size_t r=0; r<mat.num_rows(); ++r)
134  {
135  for(size_t c=0; c<mat.num_cols(); ++c)
136  out << mat.densemat(r, c) << " ";
137  if(r != mat.num_rows()-1) out << "| ";
138  }
139  out << "]";
140  out << " (DenseMatrixInverse " << mat.num_rows() << "x" << mat.num_cols() << ", " << ((T::ordering == ColMajor) ? "ColMajor)" : "RowMajor)");
141 
142  return out;
143 }
144 
145 
146 template<typename T>
148 {
149  static const int type = MATRIX_USE_GLOBAL_FUNCTIONS;
150 };
151 
152 }
153 #endif
Definition: densematrix.h:57
Definition: lapack_densematrix_inverse.h:51
double & operator()(int r, int c)
Definition: lapack_densematrix_inverse.h:78
void resize(size_t k, size_t k2)
Definition: lapack_densematrix_inverse.h:72
void resize(size_t k)
Definition: lapack_densematrix_inverse.h:67
bool invert()
Definition: lapack_densematrix_inverse.h:97
std::vector< lapack_int > interchange
Definition: lapack_densematrix_inverse.h:54
void apply(DenseVector< vector_t > &vec) const
Definition: lapack_densematrix_inverse.h:115
size_t num_cols() const
Definition: lapack_densematrix_inverse.h:57
DenseMatrix< TStorage > densemat
Definition: lapack_densematrix_inverse.h:53
size_t num_rows() const
Definition: lapack_densematrix_inverse.h:62
bool set_as_inverse_of(const DenseMatrix< TStorage > &mat)
initializes this object as inverse of mat
Definition: lapack_densematrix_inverse.h:89
friend std::ostream & operator<<(std::ostream &out, const DenseMatrixInverse< T > &mat)
Definition: lapack_densematrix_inverse.h:130
const double & operator()(int r, int c) const
Definition: lapack_densematrix_inverse.h:82
Definition: densevector.h:101
std::ostream & operator<<(std::ostream &outStream, const ug::MathMatrix< 2, 2 > &m)
Definition: math_matrix.cpp:38
#define UG_THROW(msg)
Definition: error.h:57
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
the ug namespace
@ ColMajor
Definition: storage.h:49
@ ModeNoTrans
Definition: lapack.h:80
@ MATRIX_USE_GLOBAL_FUNCTIONS
Definition: matrix_algebra_types.h:69
lapack_int getrs(eTransposeMode transposeMode, lapack_int n, lapack_int nrOfRHS, const float *pColMajorMatrix, lapack_int lda, const lapack_int *pPivot, lapack_float *pRHS, lapack_int ldb)
Definition: lapack.h:231
lapack_int getrf(lapack_int rows, lapack_int cols, lapack_float *pColMajorMatrix, lapack_int lda, lapack_int *pPivot)
Definition: lapack.h:161
Definition: matrix_algebra_types.h:79
static const int type
Definition: matrix_algebra_types.h:80