11 TripletEntry(
int r_,
int c_, g_type x_) : r(r_), c(c_), x(x_) {}
15 bool operator()(
const TripletEntry& e1,
const TripletEntry& e2)
const
17 return e1.c < e2.c || (e1.c == e2.c && e1.r < e2.r);
21 template<
class T1,
class T2,
class Pred = std::less<T1> >
23 bool operator()(
const std::pair<T1,T2>& left,
const std::pair<T1,T2>& right) {
24 return Pred()(left.first, right.first);
29 template <
class MatrixType>
38 template <
class MatrixType>
40 _blockCols(0), _hasStorage(true)
44 template <
class MatrixType>
50 for (
int i=0; i < static_cast<int>(
_blockCols.size()); ++i)
67 template <
class MatrixType>
68 SparseBlockMatrix<MatrixType>::~SparseBlockMatrix(){
73 template <
class MatrixType>
99 template <
class MatrixType>
101 typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it =
_blockCols[c].find(r);
104 return &(it->second);
110 template <
class MatrixType>
111 template <
class MatrixTransposedType>
143 template <
class MatrixType>
174 template <
class MatrixType>
175 template <
class MatrixResultType,
class MatrixFactorType >
195 typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator rbt=
_blockCols[it->first].begin();
211 template <
class MatrixType>
219 Eigen::Map<VectorX<g_type>> destVec(dest,
rows());
220 const Eigen::Map<const VectorX<g_type>> srcVec(src,
cols());
235 template <
class MatrixType>
244 Eigen::Map<VectorX<g_type>> destVec(dest,
rows());
245 const Eigen::Map<const VectorX<g_type>> srcVec(src,
cols());
249 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[i].begin(); it!=
_blockCols[i].end(); ++it){
250 const auto& a = it->second;
252 if (destOffset > srcOffset)
256 if (destOffset < srcOffset)
257 internal::atxpy(a, srcVec, destOffset, destVec, srcOffset);
262 template <
class MatrixType>
267 dest=
new g_type [ destSize ];
268 memset(dest,0, destSize*
sizeof(g_type));
272 Eigen::Map<VectorX<g_type>> destVec(dest, destSize);
273 Eigen::Map<const VectorX<g_type>> srcVec(src,
rows());
276# pragma omp parallel for default (shared) schedule(dynamic, 10)
278 for (
int i=0; i < static_cast<int>(
_blockCols.size()); ++i){
280 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[i].begin();
286 internal::atxpy(a, srcVec, srcOffset, destVec, destOffset);
292 template <
class MatrixType>
295 lib_assert(a_ > -200.0f && a_ < 200.0f,
"Bad scale");
303 template <
class MatrixType>
309 for (
int i=1; i<m; ++i){
315 for (
int i=1; i<n; ++i)
320 for (
int i=0; i<n; ++i){
322 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[mc].begin(); it!=
_blockCols[mc].end(); ++it){
323 if (it->first >= rmin && it->first < rmax){
332 template <
class MatrixType>
340 template <
class MatrixType>
343 if constexpr (MatrixType::SizeAtCompileTime != Eigen::Dynamic)
363 template <
class MatrixType>
375 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=m.
blockCols()[i].begin(); it!=m.
blockCols()[i].end(); ++it){
377 os <<
"BLOCK: " << it->first <<
" " << i << std::endl;
378 os << b << std::endl;
384 template <
class MatrixType>
392 for (
uint04 i=1; i<n; ++i){
398 for (
uint04 i=0; i<n; ++i){
399 pBlockIndices[pinv[i]]=blockSizes[i];
401 for (
uint04 i=1; i<n; ++i){
402 pBlockIndices[i]+=pBlockIndices[i-1];
412 for (
uint04 i=0; i<n; ++i){
421 for (
uint04 i=0; i<n; ++i){
424 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[i].begin();
426 int pj=pinv[it->first];
431 if (! upperTriangle || pj<=pi) {
432 b=dest->
block(pj,pi,
true);
437 b=dest->
block(pi,pj,
true);
451 template <
class MatrixType>
454 assert(Cx &&
"Target destination is NULL");
455 g_type* CxStart = Cx;
459 for (
int c=0; c<csize; ++c) {
460 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[i].begin(); it!=
_blockCols[i].end(); ++it){
461 const auto& b=it->second;
465 if (upperTriangle && rstart == cstart)
467 memcpy(Cx, b.data() + c * b.rows(), elemsToCopy *
sizeof(g_type));
476 template <
class MatrixType>
479 assert(Cp && Ci && Cx &&
"Target destination is NULL");
484 for (
int c=0; c<csize; ++c) {
486 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[i].begin(); it!=
_blockCols[i].end(); ++it){
490 int elemsToCopy = b.
rows();
491 if (upperTriangle && rstart == cstart)
493 for (
int r=0; r<elemsToCopy; ++r){
506 template <
class MatrixType>
518 for (
int i = 0; i < static_cast<int>(
_blockCols.size()); ++i){
521 for (
typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it=
_blockCols[i].begin(); it!=
_blockCols[i].end(); ++it) {
522 const int& r = it->first;
534 template <
class MatrixType>
542 auto& dest = blockCCS.
columns()[i];
546 for (
auto it = row.begin(); it != row.end(); ++it)
555 template <
class MatrixType>
564 for (
auto it = row.begin(); it != row.end(); ++it)
566 auto& dest = blockCCS.
columns()[it->first];
574 template <
class MatrixType>
579 typedef std::pair<int, MatrixType> SparseColumnPair;
580 typedef typename SparseBlockMatrixHashMap<MatrixType>::SparseColumn HashSparseColumn;
583 HashSparseColumn& column = hashMatrix.
columns()[i];
584 if (column.size() == 0)
587 sparseRowSorted.ensureCapacity(column.size());
588 for (
auto it = column.begin(); it != column.end(); ++it)
589 sparseRowSorted.
add(*it);
590 std::sort(sparseRowSorted.begin(), sparseRowSorted.end(), CmpPairFirst<int, MatrixType>());
594 IntBlockMap& destColumnMap =
blockCols()[i];
596 for (
uint04 j = 0; j < sparseRowSorted.size(); ++j)
600 destColumnMap.insert(sparseRowSorted[j]);
The equivelent of std::vector but with a bit more control.
void add(t_type &&object)
Adds object to the end of the buffer.
void insert(t_index_type location, const t_type &object)
Adds an object to a specific location.
representing the structure of a matrix in column compressed structure (only the upper triangular part...
int * Aii
row indices of A, of size nz = Ap [n]
int * Ap
column pointers for A, of size n+1
int m
A is m-by-n. m must be >= 0.
void alloc(int n_, int nz)
allocate space for the Matrix Structure.
Sparse matrix which uses blocks.
const Buffer< SparseColumn > & columns() const
the block matrices per block-column
Sparse matrix which uses blocks based on hash structures.
const Buffer< SparseColumn > & columns() const
the block matrices per block-column
Sparse matrix which uses blocks.
const Buffer< int > & rowBlockIndices() const
indices of the row blocks
void scale(g_type a)
*this *= a
int cols() const
columns of the matrix
int fillSparseBlockMatrixCCS(SparseBlockMatrixCCS< MatrixType > &blockCCS) const
copy into CCS structure
bool symmPermutation(SparseBlockMatrix< MatrixType > *&dest, const int *pinv, bool onlyUpper=false) const
writes in dest a block permutaton specified by pinv.
int fillCCS(int *Cp, int *Ci, g_type *Cx, bool upperTriangle=false) const
fill the CCS arrays of a matrix, arrays have to be allocated beforehand
SparseBlockMatrix * slice(int rmin, int rmax, int cmin, int cmax, bool alloc=true) const
returns a view or a copy of the block matrix
Buffer< IntBlockMap > _blockCols
array of maps of blocks.
const Buffer< IntBlockMap > & blockCols() const
the block matrices per block-column
int rows() const
rows of the matrix
void multiplySymmetricUpperTriangle(g_type *&dest, const g_type *src) const
compute dest = (*this) * src However, assuming that this is a symmetric matrix where only the upper t...
bool _hasStorage
Whether this matrix owns its block storage (true) or is a view (false).
Buffer< int > _rowBlockIndices
vector of the indices of the blocks along the rows.
bool add(SparseBlockMatrix< MatrixType > &dest) const
adds the current matrix to the destination
SparseMatrixBlock * block(int r, int c, bool alloc=false)
returns the block at location r,c. if alloc=true he block is created if it does not exist
uint04 nonZeroBlocks() const
number of allocated blocks
void fillBlockStructure(MatrixStructure &ms) const
exports the non zero blocks in the structure matrix ms
bool multiply(SparseBlockMatrix< MatrixResultType > *&dest, const SparseBlockMatrix< MatrixFactorType > *M) const
dest = (*this) * M
void rightMultiply(g_type *&dest, const g_type *src) const
dest = M * (*this)
const Buffer< int > & colBlockIndices() const
indices of the column blocks
void takePatternFromHash(SparseBlockMatrixHashMap< MatrixType > &hashMatrix)
take over the memory and matrix pattern from a hash matrix.
uint04 nonZeros() const
number of non-zero elements
int rowsOfBlock(int r) const
how many rows does the block at block-row r has?
int fillSparseBlockMatrixCCSTransposed(SparseBlockMatrixCCS< MatrixType > &blockCCS) const
copy as transposed into a CCS structure
bool transpose(SparseBlockMatrix< MatrixTransposedType > *&dest) const
transposes a block matrix, The transposed type should match the argument false on failure
MatrixType SparseMatrixBlock
this is the type of the elementary block, it is an Eigen::Matrix.
SparseBlockMatrix(const int *rbi, const int *cbi, int rb, int cb, bool hasStorage=true)
constructs a sparse block matrix having a specific layout
int rowBaseOfBlock(int r) const
where does the row at block-row r starts?
void clear(bool dealloc=false)
this zeroes all the blocks. If dealloc=true the blocks are removed from memory
int colsOfBlock(int c) const
how many cols does the block at block-col c has?
int colBaseOfBlock(int c) const
where does the col at block-col r starts?
Buffer< int > _colBlockIndices
vector of the indices of the blocks along the cols
void axpy(const MatrixType &A, const Eigen::Map< const Eigen::VectorX< g_type > > &x, int xoff, Eigen::Map< Eigen::VectorX< g_type > > &y, int yoff)
Computes y[yoff..] += A * x[xoff..] for fixed-size blocks.
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.