G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsAsMatrix.h
Go to the documentation of this file.
1 
14 # pragma once
15 
16 // Assumes that Eigen library has been already included
17 
18 namespace gismo
19 {
20 
30 template<class T, int _Rows, int _Cols>
31 class gsAsMatrix : public gsEigen::Map< gsEigen::Matrix<T,_Rows,_Cols> >
32 {
33 public:
34  typedef gsEigen::Map< gsEigen::Matrix<T,_Rows,_Cols> > Base;
35 
36  // type of first minor matrix: rows and cols reduced by one
37  typedef typename gsMatrix< T, _Rows, _Cols>::FirstMinorMatrixType FirstMinorMatrixType;
38 
39  // type of row minor matrix: rows reduced by one
40  typedef typename gsMatrix< T, _Rows, _Cols>::RowMinorMatrixType RowMinorMatrixType;
41 
42  // type of col minor matrix: cols reduced by one
43  typedef typename gsMatrix< T, _Rows, _Cols>::ColMinorMatrixType ColMinorMatrixType;
44 
45  // Type pointing to the transpose of the matrix
46  typedef gsEigen::Transpose<Base> Tr;
47 
48  // Type pointing to the (const) transpose of the matrix
49  typedef const gsEigen::Transpose<const Base> constTr;
50 
51 public:
52  gsAsMatrix( std::vector<T> & v, index_t n, index_t m)
53  : Base( v.data(), n, m)
54  {
55  //GISMO_ASSERT( v.size() != 0, "Tried to map an empty vector." );
56  GISMO_ASSERT( m*n <= (index_t)(v.size()), "Not enough coefficients in vector to map." );
57  }
58 
59  gsAsMatrix( std::vector<T> & v)
60  : Base( v.data(), 1, v.size() )
61  {
62  //GISMO_ASSERT( v.size() != 0, "Tried to map an empty vector." );
63  }
64 
65  gsAsMatrix( T * pt, unsigned n, unsigned m)
66  : Base( pt, n, m) { }
67 
68  gsMatrix<T> move()
69  {
70  gsMatrix<T> a;
71  a.swap(*this);
72  return a;
73  }
74 
75 #ifdef _MSC_VER
76  template <class EigenExpr>
77  gsAsMatrix& operator= (const EigenExpr & other)
78  {
79  this->Base::operator=(other);
80  return *this;
81  }
82 #else
83  using Base::operator=;
84 #endif
85 
89  void firstMinor(index_t i, index_t j, FirstMinorMatrixType & result ) const
90  {
91  const index_t mrows = this->rows()-1,
92  mcols = this->cols()-1;
93  GISMO_ASSERT( i <= mrows, "Invalid row." );
94  GISMO_ASSERT( j <= mcols, "Invalid column." );
95  result.resize(mrows,mcols);
96  result.block(0,0,i,j) = this->block(0,0,i,j);
97  result.block(i,0,mrows-i,j) = this->block(i+1,0,mrows-i,j);
98  result.block(0,j,i,mcols-j) = this->block(0,j+1,i,mcols-j);
99  result.block(i,j,mrows-i,mcols-j) = this->block(i+1,j+1,mrows-i,mcols-j);
100  }
101 
105  void rowMinor(index_t i, RowMinorMatrixType & result ) const
106  {
107  const index_t mrows = this->rows()-1;
108  GISMO_ASSERT( 0 <= i && i <= mrows, "Invalid row." );
109  result.resize(mrows, this->cols());
110  result.topRows(i) = this->topRows(i);
111  result.bottomRows(mrows-i) = this->bottomRows(mrows-i);
112  }
113 
117  void colMinor(index_t j, ColMinorMatrixType & result ) const
118  {
119  const index_t mcols = this->cols()-1;
120  GISMO_ASSERT( 0 <= j && j <= mcols, "Invalid column." );
121  result.resize( this->rows(), mcols);
122  result.leftCols(j) = this->leftCols(j);
123  result.rightCols(mcols-j) = this->rightCols(mcols-j);
124  }
125 
126 private:
127  gsAsMatrix();
128 };
129 
139 template<class T, int _Rows, int _Cols>
140 class gsAsConstMatrix : public gsEigen::Map< const gsEigen::Matrix<T,_Rows,_Cols> >
141 {
142 public:
143  typedef gsEigen::Map<const gsEigen::Matrix<T,_Rows,_Cols> > Base;
144 
145  // Type pointing to the transpose of the matrix
146  typedef gsEigen::Transpose<Base> Tr;
147 
148  // Type pointing to the (const) transpose of the matrix
149  typedef const gsEigen::Transpose<const Base> constTr;
150 
151  // type of first minor matrix: rows and cols reduced by one
152  typedef typename gsMatrix< T, _Rows, _Cols>::FirstMinorMatrixType FirstMinorMatrixType;
153 
154  // type of row minor matrix: rows reduced by one
155  typedef typename gsMatrix< T, _Rows, _Cols>::RowMinorMatrixType RowMinorMatrixType;
156 
157  // type of col minor matrix: cols reduced by one
158  typedef typename gsMatrix< T, _Rows, _Cols>::ColMinorMatrixType ColMinorMatrixType;
159 
160 public:
161 
162  gsAsConstMatrix( const std::vector<T> & v, index_t n, index_t m)
163  : Base( v.data(), n, m)
164  {
165  GISMO_ASSERT( m*n <= (index_t)(v.size()), "Not enough coefficients in vector to map." );
166  }
167 
168  gsAsConstMatrix( const std::vector<T> & v)
169  : Base( v.data(), 1, v.size() )
170  {
171  //GISMO_ASSERT( v.size() != 0, "Tried to map an empty vector." );
172  }
173 
174  gsAsConstMatrix( const T * pt, unsigned n, unsigned m)
175  : Base( pt, n, m) { }
176 
177  gsAsConstMatrix(const gsEigen::Map< gsEigen::Matrix<T,_Rows,_Cols> > & mat)
178  : Base( mat.data(), mat.rows(), mat.cols())
179  { }
180 
181 public:
182 
186  void firstMinor(index_t i, index_t j, FirstMinorMatrixType & result ) const
187  {
188  const index_t mrows = this->rows()-1,
189  mcols = this->cols()-1;
190  GISMO_ASSERT( i <= mrows, "Invalid row." );
191  GISMO_ASSERT( j <= mcols, "Invalid column." );
192  result.resize(mrows,mcols);
193  result.block(0,0,i,j) = this->block(0,0,i,j);
194  result.block(i,0,mrows-i,j) = this->block(i+1,0,mrows-i,j);
195  result.block(0,j,i,mcols-j) = this->block(0,j+1,i,mcols-j);
196  result.block(i,j,mrows-i,mcols-j) = this->block(i+1,j+1,mrows-i,mcols-j);
197  }
198 
202  void rowMinor(index_t i, RowMinorMatrixType & result ) const
203  {
204  const index_t mrows = this->rows()-1;
205  GISMO_ASSERT( 0 <= i && i <= mrows, "Invalid row." );
206  result.resize(mrows, this->cols());
207  result.topRows(i) = this->topRows(i);
208  result.bottomRows(mrows-i) = this->bottomRows(mrows-i);
209  }
210 
214  void colMinor(index_t j, ColMinorMatrixType & result ) const
215  {
216  const index_t mcols = this->cols()-1;
217  GISMO_ASSERT( 0 <= j && j <= mcols, "Invalid column." );
218  result.resize( this->rows(), mcols);
219  result.leftCols(j) = this->leftCols(j);
220  result.rightCols(mcols-j) = this->rightCols(mcols-j);
221  }
222 
223 private:
224  gsAsConstMatrix() { }
225 };
226 
236 template<class T, int _Rows>
237 class gsAsVector : public gsAsMatrix<T,_Rows,1>
238 //class gsAsVector : public gsAsMatrix<T,_Rows,(_Rows==1?1:0)>
239 {
240 public:
241  //typedef gsEigen::Map< gsEigen::Matrix<T,_Rows,1> > Base;
242  //typedef gsAsMatrix<T,_Rows,(_Rows==1?1:0)> Base;
243  typedef gsAsMatrix<T,_Rows,1> Base;
244 
245  // Type for treating a vector as a permutation matrix
246  typedef gsEigen::PermutationMatrix<_Rows> Permutation;
247 
248 public:
249  gsAsVector( std::vector<T> & v)
250  : Base( v.data(), v.size(), 1 )
251  {
252  //GISMO_ASSERT( v.size() != 0, "Tried to map an empty vector." );
253  }
254 
255  gsAsVector( T * pt, unsigned n)
256  : Base( pt, n, 1) { }
257 
258 #ifdef _MSC_VER
259  template <class EigenExpr>
260  gsAsVector& operator= (const EigenExpr & other)
261  {
262  this->Base::operator=(other);
263  return *this;
264  }
265 #else
266  using Base::operator=;
267 #endif
268 
269 private:
270  gsAsVector() { }
271 };
272 
282 template<class T, int _Rows>
283 //class gsAsConstVector : public gsAsConstMatrix<T,_Rows,(_Rows==1?1:0)>
284 class gsAsConstVector : public gsAsConstMatrix<T,_Rows,1>
285 {
286 public:
287  //typedef gsEigen::Map<const gsEigen::Matrix<T,_Rows,1> > Base;
288 
289  typedef gsAsConstMatrix<T,_Rows,1> Base;
290  //typedef gsAsConstMatrix<T,_Rows,(_Rows==1?1:0)> Base;
291 public:
292 
293  gsAsConstVector( const std::vector<T> & v)
294  : Base( v.data(), v.size(), 1)
295  {
296  //GISMO_ASSERT( v.size() != 0, "Tried to map an empty vector." );
297  }
298 
299  gsAsConstVector( const T * pt, unsigned n)
300  : Base( pt, n, 1) { }
301 
302 private:
303  gsAsConstVector() { }
304 };
305 
306 
308 template<class T, class iterator>
310 {
311  gsMatrix<T> result(n,m);
312  for ( index_t i = 0; i!=n; ++i)
313  for ( index_t j = 0; j!=m; ++j)
314  result(i,j)= *(it++);
315  return result;
316 }
317 
318 
319 } // namespace gismo
void firstMinor(index_t i, index_t j, FirstMinorMatrixType &result) const
Definition: gsAsMatrix.h:186
Creates a mapped object or data pointer to a matrix without copying data.
Definition: gsLinearAlgebra.h:126
void rowMinor(index_t i, RowMinorMatrixType &result) const
Definition: gsAsMatrix.h:202
#define index_t
Definition: gsConfig.h:32
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition: gsMatrix.h:38
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
gsMatrix< T > makeMatrix(iterator it, index_t n, index_t m)
Utility to make a matrix out of an iterator to values.
Definition: gsAsMatrix.h:309
Creates a mapped object or data pointer to a const matrix without copying data.
Definition: gsLinearAlgebra.h:127
void colMinor(index_t j, ColMinorMatrixType &result) const
Definition: gsAsMatrix.h:214
void colMinor(index_t j, ColMinorMatrixType &result) const
Definition: gsAsMatrix.h:117
void firstMinor(index_t i, index_t j, FirstMinorMatrixType &result) const
Definition: gsAsMatrix.h:89
void rowMinor(index_t i, RowMinorMatrixType &result) const
Definition: gsAsMatrix.h:105