G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsMatrixBlockView.h
Go to the documentation of this file.
1
14#pragma once
15
16// Assuming that the Eigen library is included already
17
18namespace gismo {
19
29// isConst .. to do
30template <typename MatrixType, bool isConst = false>
32{
33public:
34 typedef gsEigen::Block<MatrixType> block_t ;
35 typedef gsEigen::Block<MatrixType> * block_ptr_t;
36
37 typedef gsEigen::Matrix<index_t,gsEigen::Dynamic, 1, gsEigen::ColMajor> Vector_t;
38
39public:
40
41 gsMatrixBlockView() : m_rowSize(0), m_colSize(0)
42 { }
43
53 gsMatrixBlockView(MatrixType & matrix,
54 const Vector_t & rowSizes,
55 const Vector_t & colSizes)
56 : m_rowSize(rowSizes.size()),
57 m_colSize(colSizes.size())
58 {
59 GISMO_ASSERT( rowSizes.sum() == matrix.rows() &&
60 colSizes.sum() == matrix.cols() ,
61 "Invalid block structure.");
62
63 // NB: SparseMatrix blocks are not assignable, therefore we
64 // use pointers
65 block_ptr_t tmp;
66
67 m_blocks.reserve(m_rowSize*m_colSize);
68 index_t row, col(0);
69
70 for ( index_t j = 0; j<m_colSize; ++j )
71 {
72 row = 0;
73 for ( index_t i = 0; i<m_rowSize; ++i )
74 {
75 tmp = new block_t( matrix.block(
76 row , col ,
77 rowSizes[i], colSizes[j])
78 );
79
80 m_blocks.push_back(tmp);
81
82 row += rowSizes[i];
83 }
84 col += colSizes[j];
85 }
86 }
87
97 gsMatrixBlockView(MatrixType & matrix,
98 const Vector_t & rowSizes)
99 : m_rowSize(rowSizes.size()),
100 m_colSize(1)
101 {
102 GISMO_ASSERT( rowSizes.sum() == matrix.rows(),
103 "Invalid block structure.");
104
105 // NB: SparseMatrix blocks are not assignable, therefore we
106 // use pointers
107 block_ptr_t tmp;
108
109 m_blocks.reserve(m_rowSize*m_colSize);
110 const index_t cols = matrix.cols();
111 index_t row(0);
112
113 for ( index_t i = 0; i<m_rowSize; ++i )
114 {
115 tmp = new block_t( matrix.block(row, 0, rowSizes[i], cols ) );
116
117 m_blocks.push_back(tmp);
118
119 row += rowSizes[i];
120 }
121 }
122
124 gsMatrixBlockView(const MatrixType & matrix)
125 : m_rowSize(1),
126 m_colSize(1)
127 {
128 m_blocks.push_back( new block_t(matrix.topRows(matrix.rows())) );
129 }
130
133 {
134 freeAll(m_blocks);
135 m_blocks.clear();
136 m_rowSize = other.m_rowSize;
137 m_colSize = other.m_colSize;
138
139 block_ptr_t tmp;
140
141 for ( typename std::vector<block_ptr_t>::const_iterator it
142 = other.m_blocks.begin(); it != other.m_blocks.end(); ++it )
143 {
144 tmp = new block_t(**it);
145 m_blocks.push_back(tmp);
146 }
147 }
148
150 {
151 freeAll(m_blocks);
152 }
153
154public:
155
157 size_t numBlocks() const { return m_blocks.size(); }
158
160 size_t numRowBlocks() const { return m_rowSize; }
161
163 size_t numColBlocks() const { return m_colSize; }
164
166 block_t & operator () ( index_t i, index_t j = 0) const
167 {
168 GISMO_ASSERT( i < m_rowSize && j < m_colSize ,
169 "Invalid block requested.");
170
171 return *m_blocks[j*m_rowSize+i];
172 }
173
174
175 gsMatrixBlockView & operator = (gsMatrixBlockView other)
176 {
177 if(this != &other)
178 {
179 m_blocks.swap(other.m_blocks);
180 m_rowSize = other.m_rowSize;
181 m_colSize = other.m_colSize;
182 }
183
184 return *this;
185 }
186
188 template<typename OtherDerived>
189 void assign(index_t i, index_t j, const gsEigen::EigenBase<OtherDerived>& other)
190 {
191 GISMO_ASSERT( i < m_rowSize && j < m_colSize ,
192 "Assign to invalid block requested.");
193
194 // Works for dense Eigen matrices
195 *m_blocks[j*m_colSize+i] = other;
196 }
197
199 friend std::ostream & operator << (std::ostream & os, const gsMatrixBlockView & mv)
200 {
201 os<< "Matrix block-view size: "<< mv.m_rowSize <<"x"<< mv.m_colSize
202 <<" blocks. Structure:\n";
203
204 for ( index_t i = 0; i<mv.m_rowSize; ++i )
205 {
206 for ( index_t j = 0; j<mv.m_colSize; ++j )
207 {
208 block_t & bl = mv(i,j);
209 os<< bl.rows() <<"x"<<bl.cols() <<" ";
210 }
211 os<<"\n";
212 }
213
214 return os;
215 }
216
217private:
218
219 std::vector<block_ptr_t> m_blocks;
220
221 index_t m_rowSize;
222
223 index_t m_colSize;
224
225};
226
227
228
229
230
231} // namespace gismo
Represents a block-view of the given matrix.
Definition gsMatrixBlockView.h:32
gsMatrixBlockView(const MatrixType &matrix)
Combatibility constuctor giving the matrix as a block.
Definition gsMatrixBlockView.h:124
size_t numRowBlocks() const
Returns the number of row-blocks.
Definition gsMatrixBlockView.h:160
size_t numColBlocks() const
Returns the number of col-blocks.
Definition gsMatrixBlockView.h:163
gsMatrixBlockView(const gsMatrixBlockView &other)
Copy constructor.
Definition gsMatrixBlockView.h:132
gsMatrixBlockView(MatrixType &matrix, const Vector_t &rowSizes, const Vector_t &colSizes)
Creates a block-view of the given matrix.
Definition gsMatrixBlockView.h:53
void assign(index_t i, index_t j, const gsEigen::EigenBase< OtherDerived > &other)
Overwrites the contents of block (i,j) with matrix other.
Definition gsMatrixBlockView.h:189
friend std::ostream & operator<<(std::ostream &os, const gsMatrixBlockView &mv)
Prints the block structure.
Definition gsMatrixBlockView.h:199
gsMatrixBlockView(MatrixType &matrix, const Vector_t &rowSizes)
Creates a block-view of the given matrix, using only one column piece.
Definition gsMatrixBlockView.h:97
size_t numBlocks() const
Returns the number of blocks.
Definition gsMatrixBlockView.h:157
block_t & operator()(index_t i, index_t j=0) const
Returns the block indexed i (row) and j (column)
Definition gsMatrixBlockView.h:166
#define index_t
Definition gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
The G+Smo namespace, containing all definitions for the library.
void freeAll(It begin, It end)
Frees all pointers in the range [begin end)
Definition gsMemory.h:312