-
Notifications
You must be signed in to change notification settings - Fork 299
/
CompressedRowSparseMatrixMechanical.h
1425 lines (1234 loc) · 55 KB
/
CompressedRowSparseMatrixMechanical.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
/******************************************************************************
* Contributors:
* - InSimo
*******************************************************************************/
#pragma once
#include <sofa/linearalgebra/config.h>
#include <sofa/linearalgebra/CompressedRowSparseMatrixGeneric.h>
#include <sofa/type/trait/is_vector.h>
#include <sofa/helper/narrow_cast.h>
namespace sofa::linearalgebra
{
/// Mechanical policy type, showing the types and flags to give to CompressedRowSparseMatrixMechanical
/// for its second template type.
class CRSMechanicalPolicy : public CRSDefaultPolicy
{
public:
static constexpr bool CompressZeros = false; // keep old behavior
static constexpr bool IsAlwaysSquare = true;
static constexpr bool IsAlwaysSymmetric = true;
static constexpr bool OrderedInsertion = false; // keep old behavior
static constexpr bool StoreLowerTriangularBlock = true;
static constexpr int matrixType = 1;
};
template<typename TBlock, typename TPolicy = CRSMechanicalPolicy >
class CompressedRowSparseMatrixMechanical final // final is used to allow the compiler to inline virtual methods
: public CompressedRowSparseMatrixGeneric<TBlock, TPolicy>, public sofa::linearalgebra::BaseMatrix
{
public:
typedef CompressedRowSparseMatrixMechanical<TBlock, TPolicy> Matrix;
typedef CompressedRowSparseMatrixGeneric<TBlock, TPolicy> CRSMatrix;
typedef typename CRSMatrix::Policy Policy;
using Block = TBlock;
using VecBlock = typename CRSBlockTraits<Block>::VecBlock;
using VecIndex = typename CRSBlockTraits<Block>::VecIndex;
using VecFlag = typename CRSBlockTraits<Block>::VecFlag;
using Index = typename VecIndex::value_type;
typedef typename CRSMatrix::Block Data;
typedef typename CRSMatrix::Range Range;
typedef typename CRSMatrix::traits traits;
typedef typename CRSMatrix::Real Real;
typedef typename CRSMatrix::Index KeyType;
typedef typename CRSMatrix::IndexedBlock IndexedBlock;
typedef typename CRSMatrix::VecIndexedBlock VecIndexedBlock;
typedef Matrix Expr;
typedef CompressedRowSparseMatrixMechanical<double> matrix_type;
enum { category = MATRIX_SPARSE };
enum { operand = 1 };
enum { NL = CRSMatrix::NL }; ///< Number of rows of a block
enum { NC = CRSMatrix::NC }; ///< Number of columns of a block
/// Size
Index nRow,nCol; ///< Mathematical size of the matrix, in scalars
static_assert(!Policy::AutoSize,
"CompressedRowSparseMatrixMechanical cannot use AutoSize policy to make sure block-based and scalar-based sizes match");
CompressedRowSparseMatrixMechanical()
: CRSMatrix()
, nRow(0), nCol(0)
{
}
CompressedRowSparseMatrixMechanical(Index nbRow, Index nbCol)
: CRSMatrix((nbRow + NL-1) / NL, (nbCol + NC-1) / NC)
, nRow(nbRow), nCol(nbCol)
{
}
static void split_row_index(Index& index, Index& modulo) { bloc_index_func<NL, Index>::split(index, modulo); }
static void split_col_index(Index& index, Index& modulo) { bloc_index_func<NC, Index>::split(index, modulo); }
void compress() override
{
CRSMatrix::compress();
}
void swap(Matrix& m)
{
Index t;
t = nRow; nRow = m.nRow; m.nRow = t;
t = nCol; nCol = m.nCol; m.nCol = t;
CRSMatrix::swap(m);
}
/// Make sure all diagonal entries are present even if they are zero
template< typename = typename std::enable_if< Policy::IsAlwaysSquare> >
void fullDiagonal()
{
compress();
Index ndiag = 0;
for (Index r = 0; r < static_cast<Index>(this->rowIndex.size()); ++r)
{
Index i = this->rowIndex[r];
Index b = this->rowBegin[r];
Index e = this->rowBegin[r+1];
Index t = b;
while (b < e && this->colsIndex[t] != i)
{
if (this->colsIndex[t] < i)
b = t+1;
else
e = t;
t = (b+e)>>1;
}
if (b<e) ++ndiag;
}
if (ndiag == this->nBlockRow) return;
this->oldRowIndex.swap(this->rowIndex);
this->oldRowBegin.swap(this->rowBegin);
this->oldColsIndex.swap(this->colsIndex);
this->oldColsValue.swap(this->colsValue);
this->rowIndex.resize(this->nBlockRow);
this->rowBegin.resize(this->nBlockRow + 1);
this->colsIndex.resize(this->oldColsIndex.size() + this->nBlockRow-ndiag);
this->colsValue.resize(this->oldColsValue.size() + this->nBlockRow-ndiag);
Index nv = 0;
for (Index i = 0; i < this->nBlockRow; ++i) this->rowIndex[i] = i;
Index j = 0;
for (Index i = 0; i < static_cast<Index>(this->oldRowIndex.size()); ++i)
{
for (; j < this->oldRowIndex[i]; ++j)
{
this->rowBegin[j] = nv;
this->colsIndex[nv] = j;
traits::clear(this->colsValue[nv]);
++nv;
}
this->rowBegin[j] = nv;
Index b = this->oldRowBegin[i];
Index e = this->oldRowBegin[i+1];
for (; b < e && this->oldColsIndex[b] < j; ++b)
{
this->colsIndex[nv] = this->oldColsIndex[b];
this->colsValue[nv] = this->oldColsValue[b];
++nv;
}
if (b >= e || this->oldColsIndex[b] > j)
{
this->colsIndex[nv] = j;
traits::clear(this->colsValue[nv]);
++nv;
}
for (; b < e; ++b)
{
this->colsIndex[nv] = this->oldColsIndex[b];
this->colsValue[nv] = this->oldColsValue[b];
++nv;
}
++j;
}
for (; j < this->nBlockRow; ++j)
{
this->rowBegin[j] = nv;
this->colsIndex[nv] = j;
traits::clear(this->colsValue[nv]);
++nv;
}
this->rowBegin[j] = nv;
}
///< Mathematical size of the matrix
Index rowSize() const override
{
return nRow;
}
///< Mathematical size of the matrix
Index colSize() const override
{
return nCol;
}
/// This override classic resizeBlock to fill nRow and nCol values.
void resizeBlock(Index nbBRow, Index nbBCol) override
{
CRSMatrix::resizeBlock(nbBRow, nbBCol);
nRow = NL * nbBRow;
nCol = NC * nbBCol;
}
void resize(Index nbRow, Index nbCol) override
{
this->resizeBlock((nbRow + NL-1) / NL, (nbCol + NC-1) / NC);
nRow = nbRow;
nCol = nbCol;
}
void extend(Index nbRow, Index nbCol)
{
nRow = nbRow;
nCol = nbCol;
this->nBlockRow = (nbRow + NL-1) / NL;
this->nBlockCol = (nbCol + NC-1) / NC;
}
/**
* \brief get scalar element i, j of matrix
**/
SReal element(Index i, Index j) const override
{
if constexpr (Policy::AutoCompress)
{
const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
}
if constexpr (!Policy::StoreLowerTriangularBlock) if ((i / NL) > (j / NC))
{
std::swap(i, j);
}
Index bi=0, bj=0; split_row_index(i, bi); split_col_index(j, bj);
return traits::v(this->block(i, j), bi, bj);
}
/**
* \brief set scalar element i, j of matrix
**/
void set(Index i, Index j, double v) override
{
if constexpr (!Policy::StoreLowerTriangularBlock)
{
if ((i / NL) > (j / NC))
return;
}
Index bi=0, bj=0; split_row_index(i, bi); split_col_index(j, bj);
traits::vset(*this->wblock(i, j, true), bi, bj, static_cast<Real>(v) );
}
/**
* \brief add scalar v at element i, j of matrix
**/
void add(Index i, Index j, double v) override
{
if constexpr (!Policy::StoreLowerTriangularBlock)
{
if ((i / NL) > (j / NC))
return;
}
Index bi=0, bj=0; split_row_index(i, bi); split_col_index(j, bj);
traits::vadd(*this->wblock(i,j,true), bi, bj, static_cast<Real>(v) );
}
/**
* \brief set scalar element i, j of matrix when rowId and colId are known
**/
void set(Index i, Index j, int& rowId, int& colId, double v)
{
if constexpr (!Policy::StoreLowerTriangularBlock) if ((i / NL) > (j / NC)) return;
Index bi=0, bj=0; split_row_index(i, bi); split_col_index(j, bj);
traits::vset(*this->wblock(i,j,rowId,colId,true), bi, bj, static_cast<Real>(v) );
}
/**
* \brief add scalar v at element i, j when rowId and colId are known
**/
template <typename T = Block, typename std::enable_if_t<!std::is_same_v<T, double> && !std::is_same_v<T, float>, int > = 0 >
void add(Index i, Index j, int& rowId, int& colId, double v)
{
if constexpr (!Policy::StoreLowerTriangularBlock)
{
if ((i / NL) > (j / NC))
return;
}
Index bi=0, bj=0; split_row_index(i, bi); split_col_index(j, bj);
traits::vadd(*this->wblock(i,j,rowId,colId,true), bi, bj, static_cast<Real>(v) );
}
/**
* \brief clear scalar at element i, j of matrix
**/
void clear(Index i, Index j) override
{
if constexpr (Policy::AutoCompress) this->compress();
Index bi=0, bj=0; split_row_index(i, bi); split_col_index(j, bj);
Block* b = this->wblock(i,j,false);
if (b)
traits::vset(*b, bi, bj, 0);
}
void add(Index i, Index j, const type::Mat3x3d& _M) override
{
BaseMatrix::add(i, j, _M);
}
void add(Index i, Index j, const type::Mat3x3f& _M) override
{
BaseMatrix::add(i, j, _M);
}
/**
* \brief Clear row scalar method. Clear all col of this line.
* @param i : Line index considering size of matrix in scalar.
* \warning If you want to clear all value of a block, it is better to call clearRowBlock
**/
void clearRow(Index i) override
{
if constexpr (Policy::AutoCompress) this->compress(); /// If AutoCompress policy is activated, we neeed to be sure not missing btemp registered value.
Index bi=0; split_row_index(i, bi);
Index rowId = Index(i * this->rowIndex.size() / this->nBlockRow);
if (this->sortedFind(this->rowIndex, i, rowId))
{
Range rowRange(this->rowBegin[rowId], this->rowBegin[rowId+1]);
for (Index xj = rowRange.begin(); xj < rowRange.end(); ++xj)
{
Block& b = this->colsValue[xj];
for (Index bj = 0; bj < NC; ++bj)
traits::vset(b, bi, bj, 0);
}
}
}
/**
* \brief Clear col scalar method. Clear this col in all row of matrix.
* @param j : Col index considering size of matrix in scalar.
* \warning If you want to clear all value of a block, it is better to call clearColBlock
**/
void clearCol(Index j) override
{
/// If AutoCompress policy is activated, we neeed to be sure not missing btemp registered value.
if constexpr (Policy::AutoCompress) this->compress();
Index bj = 0; split_col_index(j, bj);
for (Index i = 0; i < this->nBlockRow; ++i)
{
Block* b = this->wblock(i,j,false);
if (b)
{
for (Index bi = 0; bi < NL; ++bi)
traits::vset(*b, bi, bj, 0);
}
}
}
/**
* \brief Clear both row i and column i in a square matrix
* @param i : Row and Col index considering size of matrix in scalar.
**/
void clearRowCol(Index i) override
{
if constexpr (!Policy::IsAlwaysSquare || !Policy::StoreLowerTriangularBlock)
{
clearRow(i);
clearCol(i);
}
else
{
/// If AutoCompress policy is activated, we need to be sure that we are not missing btemp registered value.
if constexpr (Policy::AutoCompress) this->compress();
Index bi=0; split_row_index(i, bi);
Index rowId = Index(i * this->rowIndex.size() / this->nBlockRow);
if (this->sortedFind(this->rowIndex, i, rowId))
{
Range rowRange(this->rowBegin[rowId], this->rowBegin[rowId+1]);
for (Index xj = rowRange.begin(); xj < rowRange.end(); ++xj)
{
Block* b = &this->colsValue[xj];
// first clear (i,j)
for (Index bj = 0; bj < NC; ++bj)
traits::vset(*b, bi, bj, 0);
// then clear (j,i)
Index j = this->colsIndex[xj];
if (j != i)
{
Range jrowRange(this->rowBegin[j], this->rowBegin[j + 1]);
Index colId = 0;
// look for column i
if (this->sortedFind(this->colsIndex, jrowRange, i, colId))
{
b = &this->colsValue[colId];
}
}
for (Index bj = 0; bj < NL; ++bj)
traits::vset(*b, bj, bi, 0);
}
}
}
}
/**
* \brief Completely clear the matrix
**/
void clear() override /// Need implement clear to override BaseMatrix one.
{
CRSMatrix::clear();
}
/// @name BlockMatrixWriter operators
/// @{
/// Override CRSMatrix add method to avoid mis-understanding by compiler with other add method overriding BaseMatrix.
template <typename T = Block, typename std::enable_if_t<!std::is_same_v<T, double> && !std::is_same_v<T, float>, int > = 0 >
void add(unsigned int bi, unsigned int bj, const Block& b)
{
CRSMatrix::add(bi, bj, b);
}
/// @}
/// @name Get information about the content and structure of this matrix (diagonal, band, sparse, full, block size, ...)
/// @{
/// @return type of elements stored in this matrix
virtual ElementType getElementType() const override { return traits::getElementType(); }
/// @return size of elements stored in this matrix
virtual std::size_t getElementSize() const override { return sizeof(Real); }
/// @return the category of this matrix
virtual MatrixCategory getCategory() const override { return MATRIX_SPARSE; }
/// @return the number of rows in each block, or 1 of there are no fixed block size
virtual Index getBlockRows() const override { return NL; }
/// @return the number of columns in each block, or 1 of there are no fixed block size
virtual Index getBlockCols() const override { return NC; }
/// @return the number of rows of blocks
virtual Index bRowSize() const override{ return this->rowBSize(); }
/// @return the number of columns of blocks
virtual Index bColSize() const override { return this->colBSize(); }
/// @return the width of the band on each side of the diagonal (only for band matrices)
virtual Index getBandWidth() const override { return NC-1; }
/// @}
/// @name Filtering-out part of a matrix
/// @{
typedef bool filter_fn (Index i , Index j , Block& val, const Real ref );
static bool nonzeros(Index /*i*/, Index /*j*/, Block& val, const Real /*ref*/) { return (!traits::empty(val)); }
static bool nonsmall(Index /*i*/, Index /*j*/, Block& val, const Real ref )
{
for (Index bi = 0; bi < NL; ++bi)
for (Index bj = 0; bj < NC; ++bj)
if (type::rabs(traits::v(val, bi, bj)) >= ref) return true;
return false;
}
static bool upper(Index i , Index j , Block& val, const Real /*ref*/)
{
if (NL>1 && i*NL == j*NC)
{
for (Index bi = 1; bi < NL; ++bi)
for (Index bj = 0; bj < bi; ++bj)
traits::vset(val, bi, bj, 0);
}
return i*NL <= j*NC;
}
static bool lower(Index i , Index j , Block& val, const Real /*ref*/)
{
if (NL>1 && i*NL == j*NC)
{
for (Index bi = 0; bi < NL-1; ++bi)
for (Index bj = bi+1; bj < NC; ++bj)
traits::vset(val, bi, bj, 0);
}
return i*NL >= j*NC;
}
static bool upper_nonzeros(Index i , Index j , Block& val, const Real ref ) { return upper(i,j,val,ref) && nonzeros(i,j,val,ref); }
static bool lower_nonzeros(Index i , Index j , Block& val, const Real ref ) { return lower(i,j,val,ref) && nonzeros(i,j,val,ref); }
static bool upper_nonsmall(Index i , Index j , Block& val, const Real ref ) { return upper(i,j,val,ref) && nonsmall(i,j,val,ref); }
static bool lower_nonsmall(Index i , Index j , Block& val, const Real ref ) { return lower(i,j,val,ref) && nonsmall(i,j,val,ref); }
template<class TMatrix>
void filterValues(TMatrix& srcMatrix, filter_fn* filter = &nonzeros, const Real ref = Real(), bool keepEmptyRows=false)
{
static constexpr auto SrcBlockRows = TMatrix::NL;
static constexpr auto SrcBlockColumns = TMatrix::NC;
static constexpr auto DstBlockRows = NL;
static constexpr auto DstBlockColumns = NC;
static_assert(SrcBlockRows % DstBlockRows == 0);
static_assert(SrcBlockColumns % DstBlockColumns == 0);
static constexpr auto NbBlocksRows = SrcBlockRows / DstBlockRows;
static constexpr auto NbBlocksColumns = SrcBlockColumns / DstBlockColumns;
if constexpr (TMatrix::Policy::AutoCompress)
{
/// \warning this violates the const-ness of TMatrix !
const_cast<std::remove_const_t<TMatrix>*>(&srcMatrix)->compress();
}
this->nRow = srcMatrix.nBlockRow * SrcBlockRows;
this->nCol = srcMatrix.nBlockCol * SrcBlockColumns;
this->nBlockRow = srcMatrix.nBlockRow * NbBlocksRows;
this->nBlockCol = srcMatrix.nBlockCol * NbBlocksColumns;
this->rowIndex.clear();
this->rowBegin.clear();
this->colsIndex.clear();
this->colsValue.clear();
this->skipCompressZero = true;
this->btemp.clear();
this->rowIndex.reserve(srcMatrix.rowIndex.size() * NbBlocksRows);
this->rowBegin.reserve(srcMatrix.rowBegin.size() * NbBlocksRows);
this->colsIndex.reserve(srcMatrix.colsIndex.size() * NbBlocksRows * NbBlocksColumns);
this->colsValue.reserve(srcMatrix.colsValue.size() * NbBlocksRows * NbBlocksColumns);
Index vid = 0;
for (Index srcRowId = 0; srcRowId < static_cast<Index>(srcMatrix.rowIndex.size()); ++srcRowId)
{
// row id if blocks were scalars
const Index scalarRowId = srcMatrix.rowIndex[srcRowId] * SrcBlockRows;
Range rowRange(srcMatrix.rowBegin[srcRowId], srcMatrix.rowBegin[srcRowId+1]);
for (Index subRow = 0; subRow < sofa::helper::narrow_cast<Index>(NbBlocksRows); ++subRow)
{
const auto oldVid = vid;
for (std::size_t xj = static_cast<std::size_t>(rowRange.begin()); xj < static_cast<std::size_t>(rowRange.end()); ++xj)
{
// col id if blocks were scalars
const Index scalarColId = srcMatrix.colsIndex[xj] * SrcBlockColumns;
const typename TMatrix::Block& srcBlock = srcMatrix.colsValue[xj];
for (Index subCol = 0; subCol < sofa::helper::narrow_cast<Index>(NbBlocksColumns); ++subCol)
{
Block subBlock;
matrix_bloc_traits<typename TMatrix::Block, sofa::SignedIndex>::subBlock(srcBlock, subRow * DstBlockRows, subCol * DstBlockColumns, subBlock);
if ((*filter)(scalarRowId / DstBlockRows + subRow, scalarColId / DstBlockColumns + subCol, subBlock, ref))
{
this->colsIndex.push_back(scalarColId / DstBlockColumns + subCol);
this->colsValue.push_back(subBlock);
++vid;
}
}
}
if (oldVid != vid) //check in case all sub-blocks have been filtered out
{
this->rowIndex.push_back(scalarRowId / DstBlockRows + subRow);
this->rowBegin.push_back(oldVid);
}
}
if (!keepEmptyRows && !this->rowBegin.empty() && this->rowBegin.back() == vid) // row was empty
{
this->rowIndex.pop_back();
this->rowBegin.pop_back();
}
}
this->rowBegin.push_back(vid); // end of last row
}
template <class TMatrix>
void copyNonZeros(TMatrix& M, bool keepEmptyRows=false)
{
filterValues(M, nonzeros, Real(), keepEmptyRows);
}
template <class TMatrix>
void copyNonSmall(TMatrix& M, const Real ref, bool keepEmptyRows=false)
{
filterValues(M, nonsmall, ref, keepEmptyRows);
}
void copyUpper(Matrix& M, bool keepEmptyRows=false)
{
filterValues(M, upper, Real(), keepEmptyRows);
}
void copyLower(Matrix& M, bool keepEmptyRows=false)
{
filterValues(M, lower, Real(), keepEmptyRows);
}
template <class TMatrix>
void copyUpperNonZeros(TMatrix& M, bool keepEmptyRows=false)
{
filterValues(M, upper_nonzeros, Real(), keepEmptyRows);
}
template <class TMatrix>
void copyLowerNonZeros(TMatrix& M, bool keepEmptyRows=false)
{
filterValues(M, lower_nonzeros, Real(), keepEmptyRows);
}
void copyUpperNonSmall(Matrix& M, const Real ref, bool keepEmptyRows=false)
{
filterValues(M, upper_nonsmall, ref, keepEmptyRows);
}
void copyLowerNonSmall(Matrix& M, const Real ref, bool keepEmptyRows=false)
{
filterValues(M, lower_nonsmall, ref, keepEmptyRows);
}
/// @}
/// @name Virtual iterator classes and methods
/// @{
protected:
virtual void bAccessorDelete(const InternalBlockAccessor* /*b*/) const override {}
virtual void bAccessorCopy(InternalBlockAccessor* /*b*/) const override {}
virtual SReal bAccessorElement(const InternalBlockAccessor* b, Index i, Index j) const override
{
//return element(b->row * getBlockRows() + i, b->col * getBlockCols() + j);
Index index = b->data;
const Block& data = (index >= 0) ? this->colsValue[index] : this->btemp[-index-1].value;
return static_cast<SReal>(traits::v(data, i, j));
}
virtual void bAccessorSet(InternalBlockAccessor* b, Index i, Index j, double v) override
{
//set(b->row * getBlockRows() + i, b->col * getBlockCols() + j, v);
Index index = b->data;
Block& data = (index >= 0) ? this->colsValue[index] : this->btemp[-index-1].value;
traits::vset(data, i, j, static_cast<Real>(v) );
}
virtual void bAccessorAdd(InternalBlockAccessor* b, Index i, Index j, double v) override
{
//add(b->row * getBlockRows() + i, b->col * getBlockCols() + j, v);
Index index = b->data;
Block& data = (index >= 0) ? this->colsValue[index] : this->btemp[-index-1].value;
traits::vadd(data, i, j, static_cast<Real>(v) );
}
template<class T>
const T* bAccessorElementsCSRImpl(const InternalBlockAccessor* b, T* buffer) const
{
Index index = b->data;
const Block& data = (index >= 0) ? this->colsValue[index] : this->btemp[-index-1].value;
for (Index l=0; l<NL; ++l)
for (Index c=0; c<NC; ++c)
buffer[l*NC+c] = static_cast<T>(traits::v(data, l, c));
return buffer;
}
virtual const float* bAccessorElements(const InternalBlockAccessor* b, float* buffer) const override
{
return bAccessorElementsCSRImpl<float>(b, buffer);
}
virtual const double* bAccessorElements(const InternalBlockAccessor* b, double* buffer) const override
{
return bAccessorElementsCSRImpl<double>(b, buffer);
}
virtual const int* bAccessorElements(const InternalBlockAccessor* b, int* buffer) const override
{
return bAccessorElementsCSRImpl<int>(b, buffer);
}
template<class T>
void bAccessorSetCSRImpl(InternalBlockAccessor* b, const T* buffer)
{
Index index = b->data;
Block& data = (index >= 0) ? this->colsValue[index] : this->btemp[-index-1].value;
for (Index l=0; l<NL; ++l)
for (Index c=0; c<NC; ++c)
traits::vset(data, l, c, static_cast<Real>(buffer[l*NC+c]) );
}
virtual void bAccessorSet(InternalBlockAccessor* b, const float* buffer) override
{
bAccessorSetCSRImpl<float>(b, buffer);
}
virtual void bAccessorSet(InternalBlockAccessor* b, const double* buffer) override
{
bAccessorSetCSRImpl<double>(b, buffer);
}
virtual void bAccessorSet(InternalBlockAccessor* b, const int* buffer) override
{
bAccessorSetCSRImpl<int>(b, buffer);
}
template<class T>
void bAccessorAddCSRImpl(InternalBlockAccessor* b, const T* buffer)
{
Index index = b->data;
Block& data = (index >= 0) ? this->colsValue[index] : this->btemp[-index-1].value;
for (Index l=0; l<NL; ++l)
for (Index c=0; c<NC; ++c)
traits::vadd(data, l, c,static_cast<Real>(buffer[l*NC+c]) );
}
virtual void bAccessorAdd(InternalBlockAccessor* b, const float* buffer) override
{
bAccessorAddCSRImpl<float>(b, buffer);
}
virtual void bAccessorAdd(InternalBlockAccessor* b, const double* buffer) override
{
bAccessorAddCSRImpl<double>(b, buffer);
}
virtual void bAccessorAdd(InternalBlockAccessor* b, const int* buffer) override
{
bAccessorAddCSRImpl<int>(b, buffer);
}
public:
/// Get read access to a block
virtual BlockConstAccessor blockGet(Index i, Index j) const
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
Index rowId = Index(i * this->rowIndex.size() / this->nBlockRow);
if (this->sortedFind(this->rowIndex, i, rowId))
{
Range rowRange(this->rowBegin[rowId], this->rowBegin[rowId+1]);
Index colId = rowRange.begin() + j * rowRange.size() / this->nBlockCol;
if (this->sortedFind(this->colsIndex, rowRange, j, colId))
{
return createBlockConstAccessor(i, j, colId);
}
}
return createBlockConstAccessor(-1-i, -1-j, static_cast<Index>(0));
}
/// Get write access to a block
virtual BlockAccessor blockGetW(Index i, Index j)
{
if constexpr (Policy::AutoCompress) compress();
Index rowId = Index(i * this->rowIndex.size() / this->nBlockRow);
if (this->sortedFind(this->rowIndex, i, rowId))
{
Range rowRange(this->rowBegin[rowId], this->rowBegin[rowId+1]);
Index colId = rowRange.begin() + j * rowRange.size() / this->nBlockCol;
if (this->sortedFind(this->colsIndex, rowRange, j, colId))
{
return createBlockAccessor(i, j, colId);
}
}
return createBlockAccessor(-1-i, -1-j, static_cast<Index>(0));
}
/// Get write access to a block, possibly creating it
virtual BlockAccessor blockCreate(Index i, Index j)
{
Index rowId = Index(i * this->rowIndex.size() / this->nBlockRow);
if (this->sortedFind(this->rowIndex, i, rowId))
{
Range rowRange(this->rowBegin[rowId], this->rowBegin[rowId+1]);
Index colId = rowRange.begin() + j * rowRange.size() / this->nBlockCol;
if (this->sortedFind(this->colsIndex, rowRange, j, colId))
{
return createBlockAccessor(i, j, colId);
}
}
if (this->btemp.empty() || this->btemp.back().l != i || this->btemp.back().c != j)
{
this->btemp.push_back(IndexedBlock(i,j));
traits::clear(this->btemp.back().value);
}
return createBlockAccessor(i, j, -static_cast<Index>(this->btemp.size()));
}
protected:
virtual void itCopyColBlock(InternalColBlockIterator* /*it*/) const override {}
virtual void itDeleteColBlock(const InternalColBlockIterator* /*it*/) const override {}
virtual void itAccessColBlock(InternalColBlockIterator* it, BlockConstAccessor* b) const override
{
Index index = it->data;
setMatrix(b);
getInternal(b)->row = it->row;
getInternal(b)->data = index;
getInternal(b)->col = this->colsIndex[index];
}
virtual void itIncColBlock(InternalColBlockIterator* it) const override
{
Index index = it->data;
++index;
it->data = index;
}
virtual void itDecColBlock(InternalColBlockIterator* it) const override
{
Index index = it->data;
--index;
it->data = index;
}
virtual bool itEqColBlock(const InternalColBlockIterator* it, const InternalColBlockIterator* it2) const override
{
Index index = it->data;
Index index2 = it2->data;
return index == index2;
}
virtual bool itLessColBlock(const InternalColBlockIterator* it, const InternalColBlockIterator* it2) const override
{
Index index = it->data;
Index index2 = it2->data;
return index < index2;
}
public:
/// Get the iterator corresponding to the beginning of the given row of blocks
virtual ColBlockConstIterator bRowBegin(Index ib) const override
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
Index rowId = Index(ib * this->rowIndex.size() / this->nBlockRow);
Index index = 0;
if (this->sortedFind(this->rowIndex, ib, rowId))
{
index = this->rowBegin[rowId];
}
return createColBlockConstIterator(ib, index);
}
/// Get the iterator corresponding to the end of the given row of blocks
virtual ColBlockConstIterator bRowEnd(Index ib) const override
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
Index rowId = Index(ib * this->rowIndex.size() / this->nBlockRow);
Index index2 = 0;
if (this->sortedFind(this->rowIndex, ib, rowId))
{
index2 = this->rowBegin[rowId+1];
}
return createColBlockConstIterator(ib, index2);
}
/// Get the iterators corresponding to the beginning and end of the given row of blocks
virtual std::pair<ColBlockConstIterator, ColBlockConstIterator> bRowRange(Index ib) const override
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
Index rowId = Index(ib * this->rowIndex.size() / this->nBlockRow);
Index index = 0, index2 = 0;
if (this->sortedFind(this->rowIndex, ib, rowId))
{
index = this->rowBegin[rowId];
index2 = this->rowBegin[rowId+1];
}
return std::make_pair(createColBlockConstIterator(ib, index ),
createColBlockConstIterator(ib, index2));
}
protected:
virtual void itCopyRowBlock(InternalRowBlockIterator* /*it*/) const override {}
virtual void itDeleteRowBlock(const InternalRowBlockIterator* /*it*/) const override {}
virtual Index itAccessRowBlock(InternalRowBlockIterator* it) const override
{
Index rowId = it->data[0];
return this->rowIndex[rowId];
}
virtual ColBlockConstIterator itBeginRowBlock(InternalRowBlockIterator* it) const override
{
Index rowId = it->data[0];
Index row = this->rowIndex[rowId];
Index index = this->rowBegin[rowId];
return createColBlockConstIterator(row, index);
}
virtual ColBlockConstIterator itEndRowBlock(InternalRowBlockIterator* it) const override
{
Index rowId = it->data[0];
Index row = this->rowIndex[rowId];
Index index2 = this->rowBegin[rowId+1];
return createColBlockConstIterator(row, index2);
}
virtual std::pair<ColBlockConstIterator, ColBlockConstIterator> itRangeRowBlock(InternalRowBlockIterator* it) const override
{
Index rowId = it->data[0];
Index row = this->rowIndex[rowId];
Index index = this->rowBegin[rowId];
Index index2 = this->rowBegin[rowId+1];
return std::make_pair(createColBlockConstIterator(row, index ),
createColBlockConstIterator(row, index2));
}
virtual void itIncRowBlock(InternalRowBlockIterator* it) const override
{
Index rowId = it->data[0];
++rowId;
it->data[0] = rowId;
}
virtual void itDecRowBlock(InternalRowBlockIterator* it) const override
{
Index rowId = it->data[0];
--rowId;
it->data[0] = rowId;
}
virtual bool itEqRowBlock(const InternalRowBlockIterator* it, const InternalRowBlockIterator* it2) const override
{
Index rowId = it->data[0];
Index rowId2 = it2->data[0];
return rowId == rowId2;
}
virtual bool itLessRowBlock(const InternalRowBlockIterator* it, const InternalRowBlockIterator* it2) const override
{
Index rowId = it->data[0];
Index rowId2 = it2->data[0];
return rowId < rowId2;
}
public:
/// Get the iterator corresponding to the beginning of the rows of blocks
virtual RowBlockConstIterator bRowsBegin() const override
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
return createRowBlockConstIterator(0, 0);
}
/// Get the iterator corresponding to the end of the rows of blocks
virtual RowBlockConstIterator bRowsEnd() const override
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
return createRowBlockConstIterator(Index(this->rowIndex.size()), 0);
}
/// Get the iterators corresponding to the beginning and end of the given row of blocks
virtual std::pair<RowBlockConstIterator, RowBlockConstIterator> bRowsRange() const override
{
if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
return std::make_pair(createRowBlockConstIterator(0, 0),
createRowBlockConstIterator(Index(this->rowIndex.size()), 0));
}
/// @}
protected:
/// @name setter/getter & product methods on template vector types
/// @{
template<class Vec> static Real vget(const Vec& vec, Index i, Index j, Index k) { return vget( vec, i*j+k ); }
template<class Vec> static Real vget(const type::vector<Vec>&vec, Index i, Index /*j*/, Index k) { return vec[i][k]; }
static Real vget(const BaseVector& vec, Index i) { return static_cast<Real>(vec.element(i)); }
template<class Real2> static Real2 vget(const FullVector<Real2>& vec, Index i) { return vec[i]; }
template<class Vec> static void vset(Vec& vec, Index i, Index j, Index k, Real v) { vset( vec, i*j+k, v ); }
template<class Vec> static void vset(type::vector<Vec>&vec, Index i, Index /*j*/, Index k, Real v) { vec[i][k] = v; }
static void vset(BaseVector& vec, Index i, Real v) { vec.set(i, v); }
template<class Real2> static void vset(FullVector<Real2>& vec, Index i, Real2 v) { vec[i] = v; }
template<class Vec> static void vadd(Vec& vec, Index i, Index j, Index k, Real v) { vadd( vec, i*j+k, v ); }
template<class Vec> static void vadd(type::vector<Vec>&vec, Index i, Index /*j*/, Index k, Real v) { vec[i][k] += v; }
static void vadd(BaseVector& vec, Index i, Real v) { vec.add(i, v); }
template<class Real2> static void vadd(FullVector<Real2>& vec, Index i, Real2 v) { vec[i] += v; }
template<class Vec> static void vresize(Vec& vec, Index /*blockSize*/, Index totalSize) { vec.resize( totalSize ); }
template<class Vec> static void vresize(type::vector<Vec>&vec, Index blockSize, Index /*totalSize*/) { vec.resize( blockSize ); }
/** Product of the matrix with a templated vector res = this * vec*/
//template<class Real2, class V1, class V2>
//void tmul(V1& res, const V2& vec) const
//{
// assert( vec.size() % bColSize() == 0 ); // vec.size() must be a multiple of block size.
// if constexpr (Policy::AutoCompress) const_cast<Matrix*>(this)->compress(); /// \warning this violates the const-ness of the method !
// vresize( res, this->rowBSize(), rowSize() );
// for (Index xi = 0; xi < static_cast<Index>(this->rowIndex.size()); ++xi) // for each non-empty block row
// {
// Range rowRange(this->rowBegin[xi], this->rowBegin[xi+1]);
// for (Index xj = rowRange.begin(); xj < rowRange.end(); ++xj)
// {
// sofa::type::Vec<NL,Real2> vi;
// const Block& b = this->colsValue[xj];
// Index rowIndex = this->rowIndex[xi] * NL;
// Index colIndex = this->colsIndex[xj] * NC;
// std::copy(vec.begin() + colIndex, vec.begin() + colIndex + NC, vi.begin());