mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Insert from triplets
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
990a282fc4
commit
0d12fcc34e
@@ -453,43 +453,106 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
|
||||
VERIFY_IS_APPROX(m2, refM2);
|
||||
}
|
||||
|
||||
// test setFromTriplets
|
||||
// test setFromTriplets / insertFromTriplets
|
||||
{
|
||||
typedef Triplet<Scalar,StorageIndex> TripletType;
|
||||
Index ntriplets = rows * cols;
|
||||
|
||||
std::vector<TripletType> triplets;
|
||||
Index ntriplets = rows*cols;
|
||||
|
||||
triplets.reserve(ntriplets);
|
||||
DenseMatrix refMat_sum = DenseMatrix::Zero(rows,cols);
|
||||
DenseMatrix refMat_prod = DenseMatrix::Zero(rows,cols);
|
||||
DenseMatrix refMat_last = DenseMatrix::Zero(rows,cols);
|
||||
|
||||
for(Index i=0;i<ntriplets;++i)
|
||||
{
|
||||
StorageIndex r = internal::random<StorageIndex>(0,StorageIndex(rows-1));
|
||||
StorageIndex c = internal::random<StorageIndex>(0,StorageIndex(cols-1));
|
||||
for (Index i = 0; i < ntriplets; ++i) {
|
||||
StorageIndex r = internal::random<StorageIndex>(0, StorageIndex(rows - 1));
|
||||
StorageIndex c = internal::random<StorageIndex>(0, StorageIndex(cols - 1));
|
||||
Scalar v = internal::random<Scalar>();
|
||||
triplets.push_back(TripletType(r,c,v));
|
||||
refMat_sum(r,c) += v;
|
||||
if(std::abs(refMat_prod(r,c))==0)
|
||||
refMat_prod(r,c) = v;
|
||||
triplets.push_back(TripletType(r, c, v));
|
||||
refMat_sum(r, c) += v;
|
||||
if (std::abs(refMat_prod(r, c)) == 0)
|
||||
refMat_prod(r, c) = v;
|
||||
else
|
||||
refMat_prod(r,c) *= v;
|
||||
refMat_last(r,c) = v;
|
||||
refMat_prod(r, c) *= v;
|
||||
refMat_last(r, c) = v;
|
||||
}
|
||||
|
||||
std::vector<TripletType> moreTriplets;
|
||||
moreTriplets.reserve(ntriplets);
|
||||
DenseMatrix refMat_sum_more = refMat_sum;
|
||||
DenseMatrix refMat_prod_more = refMat_prod;
|
||||
DenseMatrix refMat_last_more = refMat_last;
|
||||
|
||||
for (Index i = 0; i < ntriplets; ++i) {
|
||||
StorageIndex r = internal::random<StorageIndex>(0, StorageIndex(rows - 1));
|
||||
StorageIndex c = internal::random<StorageIndex>(0, StorageIndex(cols - 1));
|
||||
Scalar v = internal::random<Scalar>();
|
||||
moreTriplets.push_back(TripletType(r, c, v));
|
||||
refMat_sum_more(r, c) += v;
|
||||
if (std::abs(refMat_prod_more(r, c)) == 0)
|
||||
refMat_prod_more(r, c) = v;
|
||||
else
|
||||
refMat_prod_more(r, c) *= v;
|
||||
refMat_last_more(r, c) = v;
|
||||
}
|
||||
|
||||
SparseMatrixType m(rows,cols);
|
||||
|
||||
// test setFromTriplets / insertFromTriplets
|
||||
|
||||
m.setFromTriplets(triplets.begin(), triplets.end());
|
||||
VERIFY_IS_APPROX(m, refMat_sum);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
VERIFY(m.isCompressed());
|
||||
m.insertFromTriplets(moreTriplets.begin(), moreTriplets.end());
|
||||
VERIFY_IS_APPROX(m, refMat_sum_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
|
||||
VERIFY_IS_APPROX(m, refMat_prod);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
VERIFY(m.isCompressed());
|
||||
m.insertFromTriplets(moreTriplets.begin(), moreTriplets.end(), std::multiplies<Scalar>());
|
||||
VERIFY_IS_APPROX(m, refMat_prod_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromTriplets(triplets.begin(), triplets.end(), [] (Scalar,Scalar b) { return b; });
|
||||
VERIFY_IS_APPROX(m, refMat_last);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
m.setFromTriplets(triplets.begin(), triplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
VERIFY(m.isCompressed());
|
||||
m.insertFromTriplets(moreTriplets.begin(), moreTriplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
VERIFY_IS_APPROX(m, refMat_last_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
// test setFromSortedTriplets
|
||||
// insert into an uncompressed matrix
|
||||
|
||||
VectorXi reserveSizes(m.outerSize());
|
||||
for (Index i = 0; i < m.outerSize(); i++) reserveSizes[i] = internal::random<int>(1, 7);
|
||||
|
||||
m.setFromTriplets(triplets.begin(), triplets.end());
|
||||
m.reserve(reserveSizes);
|
||||
VERIFY(!m.isCompressed());
|
||||
m.insertFromTriplets(moreTriplets.begin(), moreTriplets.end());
|
||||
VERIFY_IS_APPROX(m, refMat_sum_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
|
||||
m.reserve(reserveSizes);
|
||||
VERIFY(!m.isCompressed());
|
||||
m.insertFromTriplets(moreTriplets.begin(), moreTriplets.end(), std::multiplies<Scalar>());
|
||||
VERIFY_IS_APPROX(m, refMat_prod_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromTriplets(triplets.begin(), triplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
m.reserve(reserveSizes);
|
||||
VERIFY(!m.isCompressed());
|
||||
m.insertFromTriplets(moreTriplets.begin(), moreTriplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
VERIFY_IS_APPROX(m, refMat_last_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
// test setFromSortedTriplets / insertFromSortedTriplets
|
||||
|
||||
struct triplet_comp {
|
||||
inline bool operator()(const TripletType& a, const TripletType& b) {
|
||||
@@ -501,20 +564,56 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
|
||||
// stable_sort is only necessary when the reduction functor is dependent on the order of the triplets
|
||||
// this is the case with refMat_last
|
||||
// for most cases, std::sort is sufficient and preferred
|
||||
std::stable_sort(triplets.begin(), triplets.end(), triplet_comp());
|
||||
|
||||
m.setZero();
|
||||
std::stable_sort(triplets.begin(), triplets.end(), triplet_comp());
|
||||
std::stable_sort(moreTriplets.begin(), moreTriplets.end(), triplet_comp());
|
||||
|
||||
m.setFromSortedTriplets(triplets.begin(), triplets.end());
|
||||
VERIFY_IS_APPROX(m, refMat_sum);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
VERIFY(m.isCompressed());
|
||||
m.insertFromSortedTriplets(moreTriplets.begin(), moreTriplets.end());
|
||||
VERIFY_IS_APPROX(m, refMat_sum_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromSortedTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
|
||||
VERIFY_IS_APPROX(m, refMat_prod);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
VERIFY(m.isCompressed());
|
||||
m.insertFromSortedTriplets(moreTriplets.begin(), moreTriplets.end(), std::multiplies<Scalar>());
|
||||
VERIFY_IS_APPROX(m, refMat_prod_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromSortedTriplets(triplets.begin(), triplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
VERIFY_IS_APPROX(m, refMat_last);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
VERIFY(m.isCompressed());
|
||||
m.insertFromSortedTriplets(moreTriplets.begin(), moreTriplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
VERIFY_IS_APPROX(m, refMat_last_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
// insert into an uncompressed matrix
|
||||
|
||||
m.setFromSortedTriplets(triplets.begin(), triplets.end());
|
||||
m.reserve(reserveSizes);
|
||||
VERIFY(!m.isCompressed());
|
||||
m.insertFromSortedTriplets(moreTriplets.begin(), moreTriplets.end());
|
||||
VERIFY_IS_APPROX(m, refMat_sum_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromSortedTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
|
||||
m.reserve(reserveSizes);
|
||||
VERIFY(!m.isCompressed());
|
||||
m.insertFromSortedTriplets(moreTriplets.begin(), moreTriplets.end(), std::multiplies<Scalar>());
|
||||
VERIFY_IS_APPROX(m, refMat_prod_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
|
||||
m.setFromSortedTriplets(triplets.begin(), triplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
m.reserve(reserveSizes);
|
||||
VERIFY(!m.isCompressed());
|
||||
m.insertFromSortedTriplets(moreTriplets.begin(), moreTriplets.end(), [](Scalar, Scalar b) { return b; });
|
||||
VERIFY_IS_APPROX(m, refMat_last_more);
|
||||
VERIFY_IS_EQUAL(m.innerIndicesAreSorted(), m.outerSize());
|
||||
}
|
||||
|
||||
// test Map
|
||||
|
||||
Reference in New Issue
Block a user