37template<
class T,
int _Rows,
int _Cols,
int _Options>
38class 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 std::vector<index_t> permutation;
420 index_t lastSwapDone = this->rows() - 1;
421 index_t lastCheckIdx = lastSwapDone;
423 for(
index_t i=0; i < this->rows(); i++)
424 permutation.push_back(i);
431 lastCheckIdx = lastSwapDone;
433 for(
index_t i=0; i < lastCheckIdx; i++)
434 if( this->coeff(i,j) > this->coeff(i+1,j) )
438 tmp.row(0) = this->row(i);
441 this->row(i) = this->row(i+1);
442 permutation[i] = permutation[i+1];
444 this->row(i+1) = tmp.row(0);
445 permutation[i+1] = tdx;
457 GISMO_ASSERT(lorder.size() ==
static_cast<size_t>(this->cols()),
458 "Error in dimensions");
460 for(std::vector<index_t>::const_reverse_iterator k = lorder.rbegin();
461 k != lorder.rend(); ++k)
470 const index_t nc = this->cols();
471 const index_t nr = this->rows();
474 "The blocksize is not compatible with number of columns.");
476 if (nr == 1 || colBlock == 1)
478 this->resize(colBlock, this->size()/colBlock);
480 else if ( nr == colBlock )
482 for (
index_t j = 0; j!= nc; j+=colBlock)
483 this->middleCols(j,colBlock).transposeInPlace();
487 gsEigen::Map<Base> m(this->data(), nr, nc);
488 this->resize(colBlock, this->size()/colBlock);
491 for (
index_t j = 0; j!= nc; j+=colBlock, i+=nr)
492 this->middleCols(i,nr) = m.middleCols(j,colBlock).transpose().eval();
508 std::string printSparsity()
const
510 std::ostringstream os;
511 os <<
", sparsity: "<< std::fixed << std::setprecision(2)<<
"nnz: "<<this->size()
512 <<(double)100*(this->array() != 0).count()/this->size() <<
'%'<<
"\n";
513 for (
index_t i = 0; i!=this->rows(); ++i)
515 for (
index_t j = 0; j!=this->cols(); ++j)
516 os<< ( 0 == this->coeff(i,j) ?
"\u00B7" :
"x");
517 os<<
" "<<(this->row(i).array()!=0).count()<<
"\n";
523 template<
typename OtherDerived>
526 const index_t r = this->rows(), c = this->cols();
527 const index_t ro = other.rows(), co = other.cols();
529 for (
index_t i = 0; i != r; ++i)
530 for (
index_t j = 0; j != c; ++j)
531 result.block(i*ro, j*co, ro, co) = this->coeff(i,j) * other;
536 template<
typename OtherDerived>
539 const index_t r = this->rows(), c = this->cols();
540 const index_t ro = other.rows();
541 GISMO_ASSERT(c==other.cols(),
"Column sizes do not match.");
543 for (
index_t j = 0; j != c; ++j)
544 for (
index_t i = 0; i != ro; ++i)
545 result.block(i*r, j, r, 1) = this->coeff(i,j) * other.col(j);
552 template <
typename Derived>
553 static void rref_impl(
const gsEigen::MatrixBase<Derived>& Mat)
556 gsEigen::MatrixBase<Derived> & M =
const_cast<gsEigen::MatrixBase<Derived>&
>(Mat);
562 if (nc <= piv)
return;
564 while (0 == M(i, piv))
571 if (nc == piv)
return;
575 if (i != r) M.row(i).swap(M.row(r));
580 M.row(r).tail(bc).array() /= M(r, piv);
584 M.block(0, piv+1, r, bc).noalias() -=
585 M.col(piv).head(r) * M.row(r).tail(bc);
586 M.col(piv).head(r).setZero();
589 M.block(r+1, piv+1, br, bc).noalias() -= M.col(piv).tail(br) * M.row(r).tail(bc);
590 M.col(piv).tail(br).setZero();
597 template <
typename Derived>
598 static void ref_impl(
const gsEigen::MatrixBase<Derived>& Mat)
601 gsEigen::MatrixBase<Derived> & M =
const_cast<gsEigen::MatrixBase<Derived>&
>(Mat);
607 if (nc <= piv)
return;
609 while (0 == M(i, piv))
616 if (nc == piv)
return;
620 if (i != r) M.row(i).swap(M.row(r));
625 M.block(r+1, piv+1, br, bc).noalias() -=
626 M.col(piv).tail(br) * M.row(r).tail(bc) / M(r, piv);
627 M.col(piv).tail(br).setZero();
633 struct removeNoise_helper
635 removeNoise_helper(
const T & tol)
638 inline const T operator() (
const T & val)
const
639 {
return ( math::abs(val) < m_tol ? 0 : val ); }
652template<
class T,
int _Rows,
int _Cols,
int _Options>
inline
653gsMatrix<T,_Rows, _Cols, _Options>::gsMatrix(
const Base& a) : Base(a) { }
655template<
class T,
int _Rows,
int _Cols,
int _Options>
inline
656gsMatrix<T,_Rows, _Cols, _Options>::gsMatrix(
int rows,
int cols) : Base(rows,cols) { }
669#ifdef GISMO_WITH_PYBIND11
675 void pybind11_init_gsMatrix(pybind11::module &m,
const std::string & typestr)
677 using Class = gsMatrix<T>;
678 std::string pyclass_name = std::string(
"gsMatrix") + typestr;
679 pybind11::class_<Class>(m, pyclass_name.c_str(), pybind11::buffer_protocol(), pybind11::dynamic_attr())
681 .def(pybind11::init<>())
682 .def(pybind11::init<index_t, index_t>())
684 .def(
"size", &Class::size)
685 .def(
"rows", &Class::rows)
686 .def(
"cols", &Class::cols)
697namespace gsEigen {
namespace internal {
698template<
class T,
int _Rows,
int _Cols,
int _Options>
699struct traits<
gismo::gsMatrix<T,_Rows,_Cols,_Options> > :
700gsEigen::internal::traits<gsEigen::Matrix<T,_Rows,_Cols,_Options> > { };
Creates a mapped object or data pointer to a const matrix without copying data.
Definition gsAsMatrix.h:141
Creates a mapped object or data pointer to a const vector without copying data.
Definition gsAsMatrix.h:285
Creates a mapped object or data pointer to a matrix without copying data.
Definition gsAsMatrix.h:32
Creates a mapped object or data pointer to a vector without copying data.
Definition gsAsMatrix.h:239
Represents a block-view of the given matrix.
Definition gsMatrixBlockView.h:32
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
void colMinor(index_t j, ColMinorMatrixType &result) const
Definition gsMatrix.h:343
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
uPtr moveToPtr()
This function returns a smart pointer to the matrix. After calling it, the matrix object becomes empt...
Definition gsMatrix.h:170
memory::unique_ptr< gsMatrix > uPtr
Unique pointer for gsMatrix.
Definition gsMatrix.h:105
gsMatrix(const gsEigen::EigenBase< OtherDerived > &other)
This constructor allows constructing a gsMatrix from Eigen expressions.
Definition gsMatrix.h:151
void firstMinor(index_t i, index_t j, FirstMinorMatrixType &result) const
Definition gsMatrix.h:315
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
Col3DType col3d(index_t c)
Returns column c as a fixed-size 3D vector.
Definition gsMatrix.h:244
void rcefInPlace()
Converts the matrix to its Reduced Column Echelon Form (RCEF)
Definition gsMatrix.h:500
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 removeCol(index_t i)
Definition gsMatrix.h:303
void refInPlace()
Converts the matrix to a Row Echelon Form (REF)
Definition gsMatrix.h:503
T at(index_t i) const
Returns the i-th element of the vectorization of the matrix.
Definition gsMatrix.h:211
gsMatrix khatriRao(const gsEigen::MatrixBase< OtherDerived > &other) const
Returns the Khatri-Rao product of this with other.
Definition gsMatrix.h:537
gsAsConstMatrix< T, 1, Dynamic > asRowVector() const
Definition gsMatrix.h:258
void cefInPlace()
Converts the matrix to a Column Echelon Form (CEF)
Definition gsMatrix.h:506
T & at(index_t i)
Returns the i-th element of the vectorization of the matrix.
Definition gsMatrix.h:214
gsAsVector< T, Dynamic > asVector()
Returns the entries of the matrix resized to a n*m vector column-wise.
Definition gsMatrix.h:240
void rowMinor(index_t i, RowMinorMatrixType &result) const
Definition gsMatrix.h:331
memory::shared_ptr< gsMatrix > Ptr
Shared pointer for gsMatrix.
Definition gsMatrix.h:102
void sortByColumn(const index_t j)
Sorts rows of matrix by column j.
Definition gsMatrix.h:388
std::vector< index_t > idxByColumn(const index_t j)
Returns the vector permutation of the rows of the matrix by column j.
Definition gsMatrix.h:415
void submatrixCols(const container &colInd, gsMatrix< T > &result) const
Definition gsMatrix.h:264
void lexSortRows(const std::vector< index_t > &lorder)
Sorts rows of matrix by columns in vector lorder.
Definition gsMatrix.h:455
gsAsConstVector< T, Dynamic > asVector() const
Returns the entries of the matrix resized to a (const) n*m vector column-wise.
Definition gsMatrix.h:248
void rrefInPlace()
Converts the matrix to its Reduced Row Echelon Form (RREF)
Definition gsMatrix.h:497
void submatrix(const container &rowInd, const container &colInd, gsMatrix< T > &result) const
Definition gsMatrix.h:288
void blockTransposeInPlace(const index_t colBlock)
Transposes in place the matrix block-wise. The matrix is.
Definition gsMatrix.h:468
void submatrixRows(const container &rowInd, gsMatrix< T > &result) const
Definition gsMatrix.h:276
gsMatrix(const gsEigen::MatrixBase< OtherDerived > &other)
This constructor allows constructing a gsMatrix from Eigen expressions.
Definition gsMatrix.h:155
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
gsMatrix(const gsEigen::ReturnByValue< OtherDerived > &other)
This constructor allows constructing a gsMatrix from Eigen expressions.
Definition gsMatrix.h:159
gsAsMatrix< T, 1, Dynamic > asRowVector()
Definition gsMatrix.h:253
gsMatrix kron(const gsEigen::MatrixBase< OtherDerived > &other) const
Returns the Kronecker product of this with other.
Definition gsMatrix.h:524
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
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition gsVector.h:37
#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.