37 template<
class T,
int _Rows,
int _Cols,
int _Options>
38 class gsMatrix :
public gsEigen::Matrix<T,_Rows, _Cols, _Options>
44 typedef gsEigen::Matrix<T,_Rows, _Cols, _Options> Base;
52 typedef typename gsEigen::aligned_allocator<Self> aalloc;
58 typedef gsEigen::Block<Base> Block;
61 typedef gsEigen::Block<const Base> constBlock;
64 typedef gsEigen::Block<Base, 1, _Cols, false> Row;
67 typedef gsEigen::Block<const Base, 1, _Cols, false> constRow;
70 typedef gsEigen::Block<Base, Dynamic, _Cols, false> Rows;
73 typedef gsEigen::Block<const Base, Dynamic, _Cols, false> constRows;
76 typedef gsEigen::Block<Base, _Rows, 1, true > Column;
79 typedef gsEigen::Block<const Base, _Rows, 1, true > constColumn;
82 typedef gsEigen::Block<Base, _Rows, Dynamic, true > Columns;
85 typedef gsEigen::Block<const Base, _Rows, Dynamic, true > constColumns;
88 typedef gsEigen::Transpose<Base> Tr;
91 typedef const gsEigen::Transpose<const Base> constTr;
95 typedef gsEigen::Ref<Base> Ref;
99 typedef const gsEigen::Ref<const Base> constRef;
102 typedef memory::shared_ptr<gsMatrix>
Ptr;
105 typedef memory::unique_ptr<gsMatrix>
uPtr;
108 typedef gsMatrix< T, ChangeDim<_Rows, -1>::D, ChangeDim<_Cols, -1>::D>
112 typedef gsMatrix< T, ChangeDim<_Rows, -1>::D, _Cols>
116 typedef gsMatrix< T, _Rows, ChangeDim<_Cols, -1>::D>
120 typedef gsEigen::VectorBlock<gsEigen::Block<gsEigen::Matrix<T,_Rows,_Cols>,-1,1,
true>,3> Col3DType;
121 typedef gsEigen::VectorBlock<const gsEigen::Block<const gsEigen::Matrix<T,_Rows,_Cols>,-1,1,
true>,3> CCol3DType;
124 typedef typename gsEigen::EigenSolver<Base> EigenSolver;
126 typedef typename gsEigen::SelfAdjointEigenSolver<Base> SelfAdjEigenSolver;
128 typedef typename gsEigen::GeneralizedSelfAdjointEigenSolver<Base> GenSelfAdjEigenSolver;
131 typedef typename gsEigen::JacobiSVD<Base> JacobiSVD;
150 template<
typename OtherDerived>
151 gsMatrix(
const gsEigen::EigenBase<OtherDerived>& other) : Base(other) { }
154 template<
typename OtherDerived>
155 gsMatrix(
const gsEigen::MatrixBase<OtherDerived>& other) : Base(other) { }
158 template<
typename OtherDerived>
159 gsMatrix(
const gsEigen::ReturnByValue<OtherDerived>& other) : Base(other) { }
161 inline operator Ref () {
return Ref(*
this); }
163 inline operator const constRef () {
return constRef(*
this); }
178 void clear() { this->resize(0,0); }
194 #if !EIGEN_HAS_RVALUE_REFERENCES
196 gsMatrix & operator=(
typename gsEigen::internal::conditional<
197 -1==_Rows,gsMatrix,
const gsMatrix &>::type other)
202 this->Base::operator=(other);
207 std::pair<index_t,index_t> dim()
const
208 {
return std::make_pair(this->rows(), this->cols() ); }
245 CCol3DType
col3d(
index_t c)
const {
return this->col(c).template head<3>(); }
263 template<
class container>
267 const index_t nc = colInd.size();
268 result.resize(this->rows(), nc );
269 for (
index_t i = 0; i!= nc; ++i )
270 result.col(i) = this->col( colInd[i] );
275 template<
class container>
279 const index_t nr = rowInd.size();
280 result.resize(nr, this->cols() );
281 for (
index_t i = 0; i!= nr; ++i )
282 result.row(i) = this->row( rowInd[i] );
287 template<
class container>
289 const container & colInd,
293 const index_t nr = rowInd.size();
294 const index_t nc = colInd.size();
295 result.resize(nr, nc );
296 for (
index_t i = 0; i!= nr; ++i )
297 for (
index_t j = 0; j!= nc; ++j )
298 result(i,j) = this->coeff(rowInd[i], colInd[j] );
307 for (
index_t c = i+1; c!= cc; ++c )
308 this->col(c-1) = this->col(c);
309 this->conservativeResize(this->rows(), cc-1);
317 const index_t mrows = this->rows()-1,
318 mcols = this->cols()-1;
321 result.resize(mrows,mcols);
322 result.block(0,0,i,j) = this->block(0,0,i,j);
323 result.block(i,0,mrows-i,j) = this->block(i+1,0,mrows-i,j);
324 result.block(0,j,i,mcols-j) = this->block(0,j+1,i,mcols-j);
325 result.block(i,j,mrows-i,mcols-j) = this->block(i+1,j+1,mrows-i,mcols-j);
333 const index_t mrows = this->rows()-1;
335 result.resize(mrows, this->cols());
336 result.topRows(i) = this->topRows(i);
337 result.bottomRows(mrows-i) = this->bottomRows(mrows-i);
345 const index_t mcols = this->cols()-1;
347 result.resize(this->rows(), mcols);
348 result.leftCols(j) = this->leftCols(j);
349 result.rightCols(mcols-j) = this->rightCols(mcols-j);
354 this->conservativeResize(this->rows() + 1, this->cols());
366 for (
index_t i = this->rows() - 1; i > k+1 ; --i)
367 this->row(i).swap(this->row(i-1));
369 this->row(k+1) = this->row(k);
372 void removeNoise(
const T tol)
374 this->noalias() = this->unaryExpr(removeNoise_helper(tol));
384 return BlockView(*
this, rowSizes, colSizes);
392 index_t lastSwapDone = this->rows() - 1;
393 index_t lastCheckIdx = lastSwapDone;
399 lastCheckIdx = lastSwapDone;
401 for(
index_t i=0; i < lastCheckIdx; i++)
402 if( this->coeff(i,j) > this->coeff(i+1,j) )
404 tmp.row(0) = this->row(i);
405 this->row(i) = this->row(i+1);
406 this->row(i+1) = tmp.row(0);
417 GISMO_ASSERT(lorder.size() ==
static_cast<size_t>(this->cols()),
418 "Error in dimensions");
420 for(std::vector<index_t>::const_reverse_iterator k = lorder.rbegin();
421 k != lorder.rend(); ++k)
430 const index_t nc = this->cols();
431 const index_t nr = this->rows();
434 "The blocksize is not compatible with number of columns.");
436 if (nr == 1 || colBlock == 1)
438 this->resize(colBlock, this->size()/colBlock);
440 else if ( nr == colBlock )
442 for (
index_t j = 0; j!= nc; j+=colBlock)
443 this->middleCols(j,colBlock).transposeInPlace();
447 gsEigen::Map<Base> m(this->data(), nr, nc);
448 this->resize(colBlock, this->size()/colBlock);
451 for (
index_t j = 0; j!= nc; j+=colBlock, i+=nr)
452 this->middleCols(i,nr) = m.middleCols(j,colBlock).transpose().eval();
468 std::string printSparsity()
const
470 std::ostringstream os;
471 os <<
", sparsity: "<< std::fixed << std::setprecision(2)<<
"nnz: "<<this->size()
472 <<(double)100*(this->array() != 0).count()/this->size() <<
'%'<<
"\n";
473 for (
index_t i = 0; i!=this->rows(); ++i)
475 for (
index_t j = 0; j!=this->cols(); ++j)
476 os<< ( 0 == this->coeff(i,j) ?
"\u00B7" :
"x");
477 os<<
" "<<(this->row(i).array()!=0).count()<<
"\n";
483 template<
typename OtherDerived>
486 const index_t r = this->rows(), c = this->cols();
487 const index_t ro = other.rows(), co = other.cols();
489 for (
index_t i = 0; i != r; ++i)
490 for (
index_t j = 0; j != c; ++j)
491 result.block(i*ro, j*co, ro, co) = this->coeff(i,j) * other;
496 template<
typename OtherDerived>
499 const index_t r = this->rows(), c = this->cols();
500 const index_t ro = other.rows();
501 GISMO_ASSERT(c==other.cols(),
"Column sizes do not match.");
503 for (
index_t j = 0; j != c; ++j)
504 for (
index_t i = 0; i != ro; ++i)
505 result.block(i*r, j, r, 1) = this->coeff(i,j) * other.col(j);
512 template <
typename Derived>
513 static void rref_impl(
const gsEigen::MatrixBase<Derived>& Mat)
516 gsEigen::MatrixBase<Derived> & M =
const_cast<gsEigen::MatrixBase<Derived>&
>(Mat);
522 if (nc <= piv)
return;
524 while (0 == M(i, piv))
531 if (nc == piv)
return;
535 if (i != r) M.row(i).swap(M.row(r));
540 M.row(r).tail(bc).array() /= M(r, piv);
544 M.block(0, piv+1, r, bc).noalias() -=
545 M.col(piv).head(r) * M.row(r).tail(bc);
546 M.col(piv).head(r).setZero();
549 M.block(r+1, piv+1, br, bc).noalias() -= M.col(piv).tail(br) * M.row(r).tail(bc);
550 M.col(piv).tail(br).setZero();
557 template <
typename Derived>
558 static void ref_impl(
const gsEigen::MatrixBase<Derived>& Mat)
561 gsEigen::MatrixBase<Derived> & M =
const_cast<gsEigen::MatrixBase<Derived>&
>(Mat);
567 if (nc <= piv)
return;
569 while (0 == M(i, piv))
576 if (nc == piv)
return;
580 if (i != r) M.row(i).swap(M.row(r));
585 M.block(r+1, piv+1, br, bc).noalias() -=
586 M.col(piv).tail(br) * M.row(r).tail(bc) / M(r, piv);
587 M.col(piv).tail(br).setZero();
593 struct removeNoise_helper
595 removeNoise_helper(
const T & tol)
598 inline const T operator() (
const T & val)
const
599 {
return (
math::abs(val) < m_tol ? 0 : val ); }
612 template<
class T,
int _Rows,
int _Cols,
int _Options>
inline
613 gsMatrix<T,_Rows, _Cols, _Options>::gsMatrix(
const Base& a) : Base(a) { }
615 template<
class T,
int _Rows,
int _Cols,
int _Options>
inline
616 gsMatrix<T,_Rows, _Cols, _Options>::gsMatrix(
int rows,
int cols) : Base(rows,cols) { }
629 #ifdef GISMO_WITH_PYBIND11
634 namespace py = pybind11;
637 void pybind11_init_gsMatrix(pybind11::module &m,
const std::string & typestr)
639 using Class = gsMatrix<T>;
640 std::string pyclass_name = std::string(
"gsMatrix") + typestr;
641 py::class_<Class>(m, pyclass_name.c_str(), py::buffer_protocol(), py::dynamic_attr())
644 .def(py::init<index_t, index_t>())
646 .def(
"size", &Class::size)
647 .def(
"rows", &Class::rows)
648 .def(
"cols", &Class::cols)
653 #endif // GISMO_WITH_PYBIND11
659 namespace gsEigen {
namespace internal {
660 template<
class T,
int _Rows,
int _Cols,
int _Options>
661 struct traits<gismo::gsMatrix<T,_Rows,_Cols,_Options> > :
662 gsEigen::internal::traits<gsEigen::Matrix<T,_Rows,_Cols,_Options> > { };
gsMatrix(const gsEigen::ReturnByValue< OtherDerived > &other)
This constructor allows constructing a gsMatrix from Eigen expressions.
Definition: gsMatrix.h:159
void rowMinor(index_t i, RowMinorMatrixType &result) const
Definition: gsMatrix.h:331
gsAsConstMatrix< T, 1, Dynamic > asRowVector() const
Definition: gsMatrix.h:258
Creates a mapped object or data pointer to a matrix without copying data.
Definition: gsLinearAlgebra.h:126
gsAsConstVector< T, Dynamic > asVector() const
Returns the entries of the matrix resized to a (const) n*m vector column-wise.
Definition: gsMatrix.h:248
gsMatrix kron(const gsEigen::MatrixBase< OtherDerived > &other) const
Returns the Kronecker product of this with other.
Definition: gsMatrix.h:484
Col3DType col3d(index_t c)
Returns column c as a fixed-size 3D vector.
Definition: gsMatrix.h:244
void lexSortRows(const std::vector< index_t > &lorder)
Sorts rows of matrix by columns in vector lorder.
Definition: gsMatrix.h:415
memory::shared_ptr< gsMatrix > Ptr
Shared pointer for gsMatrix.
Definition: gsMatrix.h:102
gsMatrix khatriRao(const gsEigen::MatrixBase< OtherDerived > &other) const
Returns the Khatri-Rao product of this with other.
Definition: gsMatrix.h:497
#define index_t
Definition: gsConfig.h:32
void colMinor(index_t j, ColMinorMatrixType &result) const
Definition: gsMatrix.h:343
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition: gsMatrix.h:38
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
void rrefInPlace()
Converts the matrix to its Reduced Row Echelon Form (RREF)
Definition: gsMatrix.h:457
memory::unique_ptr< gsMatrix > uPtr
Unique pointer for gsMatrix.
Definition: gsMatrix.h:105
gsAsMatrix< T, Dynamic, Dynamic > reshape(index_t n, index_t m)
Returns the matrix resized to n x m matrix (data is not copied) This function assumes that the matrix...
Definition: gsMatrix.h:221
void submatrixCols(const container &colInd, gsMatrix< T > &result) const
Definition: gsMatrix.h:264
T at(index_t i) const
Returns the i-th element of the vectorization of the matrix.
Definition: gsMatrix.h:211
Creates a mapped object or data pointer to a vector without copying data.
Definition: gsLinearAlgebra.h:129
void removeCol(index_t i)
Definition: gsMatrix.h:303
gsAsConstMatrix< T, Dynamic, Dynamic > reshape(index_t n, index_t m) const
Returns the matrix resized to n x m matrix (data is not copied) This function assumes that the matrix...
Definition: gsMatrix.h:226
void blockTransposeInPlace(const index_t colBlock)
Transposes in place the matrix block-wise. The matrix is.
Definition: gsMatrix.h:428
gsAsMatrix< T, Dynamic, Dynamic > reshapeCol(index_t c, index_t n, index_t m)
Returns column c of the matrix resized to n x m matrix This function assumes that the matrix is size ...
Definition: gsMatrix.h:231
Creates a mapped object or data pointer to a const matrix without copying data.
Definition: gsLinearAlgebra.h:127
void cefInPlace()
Converts the matrix to a Column Echelon Form (CEF)
Definition: gsMatrix.h:466
void rcefInPlace()
Converts the matrix to its Reduced Column Echelon Form (RCEF)
Definition: gsMatrix.h:460
gsMatrix(const gsEigen::EigenBase< OtherDerived > &other)
This constructor allows constructing a gsMatrix from Eigen expressions.
Definition: gsMatrix.h:151
void submatrixRows(const container &rowInd, gsMatrix< T > &result) const
Definition: gsMatrix.h:276
gsAsConstMatrix< T, Dynamic, Dynamic > reshapeCol(index_t c, index_t n, index_t m) const
Returns column c of the matrix resized to n x m matrix This function assumes that the matrix is size ...
Definition: gsMatrix.h:236
Represents a block-view of the given matrix.
Definition: gsMatrixBlockView.h:31
BlockView blockView(const gsVector< index_t > &rowSizes, const gsVector< index_t > &colSizes)
Return a block view of the matrix with rowSizes and colSizes.
Definition: gsMatrix.h:381
uPtr moveToPtr()
This function returns a smart pointer to the matrix. After calling it, the matrix object becomes empt...
Definition: gsMatrix.h:170
Creates a mapped object or data pointer to a const vector without copying data.
Definition: gsLinearAlgebra.h:130
void refInPlace()
Converts the matrix to a Row Echelon Form (REF)
Definition: gsMatrix.h:463
EIGEN_STRONG_INLINE abs_expr< E > abs(const E &u)
Absolute value.
Definition: gsExpressions.h:4486
gsAsVector< T, Dynamic > asVector()
Returns the entries of the matrix resized to a n*m vector column-wise.
Definition: gsMatrix.h:240
T & at(index_t i)
Returns the i-th element of the vectorization of the matrix.
Definition: gsMatrix.h:214
gsAsMatrix< T, 1, Dynamic > asRowVector()
Definition: gsMatrix.h:253
void firstMinor(index_t i, index_t j, FirstMinorMatrixType &result) const
Definition: gsMatrix.h:315
void submatrix(const container &rowInd, const container &colInd, gsMatrix< T > &result) const
Definition: gsMatrix.h:288
void sortByColumn(const index_t j)
Sorts rows of matrix by column j.
Definition: gsMatrix.h:388
gsMatrix(const gsEigen::MatrixBase< OtherDerived > &other)
This constructor allows constructing a gsMatrix from Eigen expressions.
Definition: gsMatrix.h:155