Sparse matrix column/row removal

This commit is contained in:
Charles Schlosser
2023-05-24 17:04:45 +00:00
committed by Rasmus Munk Larsen
parent de1c884687
commit 67a1e881d9
2 changed files with 129 additions and 2 deletions

View File

@@ -38,6 +38,7 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
double density = (std::max)(8./(rows*cols), 0.01);
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
typedef Matrix<Scalar,Dynamic,1> DenseVector;
typedef Matrix<Scalar, Dynamic, Dynamic, SparseMatrixType::IsRowMajor ? RowMajor : ColMajor> CompatibleDenseMatrix;
Scalar eps = 1e-6;
Scalar s1 = internal::random<Scalar>();
@@ -162,6 +163,74 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
VERIFY_IS_APPROX(m2,m1);
}
// test removeOuterVectors / insertEmptyOuterVectors
{
for (int mode = 0; mode < 4; mode++) {
CompatibleDenseMatrix m1(rows, cols);
m1.setZero();
SparseMatrixType m2(rows, cols);
Vector<Index, Dynamic> reserveSizes(outer);
for (Index j = 0; j < outer; j++) reserveSizes(j) = internal::random<Index>(1, inner - 1);
m2.reserve(reserveSizes);
for (Index j = 0; j < outer; j++) {
Index i = internal::random<Index>(0, inner - 1);
Scalar val = internal::random<Scalar>();
m1.coeffRefByOuterInner(j, i) = val;
m2.insertByOuterInner(j, i) = val;
}
if (mode % 2 == 0) m2.makeCompressed();
if (mode < 2) {
Index num = internal::random<Index>(0, outer - 1);
Index start = internal::random<Index>(0, outer - num);
Index newRows = SparseMatrixType::IsRowMajor ? rows - num : rows;
Index newCols = SparseMatrixType::IsRowMajor ? cols : cols - num;
CompatibleDenseMatrix m3(newRows, newCols);
m3.setConstant(Scalar(NumTraits<RealScalar>::quiet_NaN()));
if (SparseMatrixType::IsRowMajor) {
m3.topRows(start) = m1.topRows(start);
m3.bottomRows(newRows - start) = m1.bottomRows(newRows - start);
} else {
m3.leftCols(start) = m1.leftCols(start);
m3.rightCols(newCols - start) = m1.rightCols(newCols - start);
}
SparseMatrixType m4 = m2;
m4.removeOuterVectors(start, num);
VERIFY_IS_CWISE_EQUAL(m3, m4.toDense());
} else {
Index num = internal::random<Index>(0, outer - 1);
Index start = internal::random<Index>(0, outer - 1);
Index newRows = SparseMatrixType::IsRowMajor ? rows + num : rows;
Index newCols = SparseMatrixType::IsRowMajor ? cols : cols + num;
CompatibleDenseMatrix m3(newRows, newCols);
m3.setConstant(Scalar(NumTraits<RealScalar>::quiet_NaN()));
if (SparseMatrixType::IsRowMajor) {
m3.topRows(start) = m1.topRows(start);
m3.middleRows(start, num).setZero();
m3.bottomRows(rows - start) = m1.bottomRows(rows - start);
} else {
m3.leftCols(start) = m1.leftCols(start);
m3.middleCols(start, num).setZero();
m3.rightCols(cols - start) = m1.rightCols(cols - start);
}
SparseMatrixType m4 = m2;
m4.insertEmptyOuterVectors(start, num);
VERIFY_IS_CWISE_EQUAL(m3, m4.toDense());
}
}
}
// test sort
if (inner > 1) {
bool StorageOrdersMatch = int(DenseMatrix::IsRowMajor) == int(SparseMatrixType::IsRowMajor);