12 #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA
14 #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++;
17 #define EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN g_dense_op_sparse_count++;
18 #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN g_dense_op_sparse_count+=10;
19 #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN g_dense_op_sparse_count+=20;
24 template<
typename SparseMatrixType>
void sparse_basic(
const SparseMatrixType&
ref)
26 typedef typename SparseMatrixType::StorageIndex StorageIndex;
36 enum { Flags = SparseMatrixType::Flags };
43 Scalar s1 = internal::random<Scalar>();
49 std::vector<Vector2> zeroCoords;
50 std::vector<Vector2> nonzeroCoords;
51 initSparse<Scalar>(
density, refMat,
m, 0, &zeroCoords, &nonzeroCoords);
62 if(!nonzeroCoords.empty()) {
63 m.coeffRef(nonzeroCoords[0].
x(), nonzeroCoords[0].
y()) =
Scalar(5);
64 refMat.coeffRef(nonzeroCoords[0].
x(), nonzeroCoords[0].
y()) =
Scalar(5);
79 bool call_reserve = internal::random<int>()%2;
80 Index nnz = internal::random<int>(1,
int(
rows)/2);
83 if(internal::random<int>()%2)
84 m2.reserve(VectorXi::Constant(
m2.outerSize(),
int(nnz)));
86 m2.reserve(
m2.outerSize() * nnz);
91 for (
Index k=0; k<nnz; ++k)
95 m2.insert(
i,
j) =
m1(
i,
j) = internal::random<Scalar>();
99 if(call_reserve && !SparseMatrixType::IsRowMajor)
113 if(internal::random<int>()%2)
114 m2.reserve(VectorXi::Constant(
m2.outerSize(), 2));
119 if ((
m1.coeff(
i,
j)==
Scalar(0)) && (internal::random<int>()%2))
120 m2.insert(
i,
j) =
m1(
i,
j) = internal::random<Scalar>();
123 Scalar v = internal::random<Scalar>();
124 m2.coeffRef(
i,
j) +=
v;
137 VectorXi r(VectorXi::Constant(
m2.outerSize(), ((
mode%2)==0) ?
int(
m2.innerSize()) : std::max<int>(1,
int(
m2.innerSize())/8)));
144 m2.insert(
i,
j) =
m1(
i,
j) = internal::random<Scalar>();
148 if(internal::random<int>()%2)
166 initSparse<Scalar>(
density, refM4, m4);
168 if(internal::random<bool>())
181 if(SparseMatrixType::IsRowMajor)
182 VERIFY_IS_APPROX(
m1.innerVector(0).dot(refM2.row(0)), refM1.row(0).dot(refM2.row(0)));
184 VERIFY_IS_APPROX(
m1.innerVector(0).dot(refM2.col(0)), refM1.col(0).dot(refM2.col(0)));
188 Index r = internal::random<Index>(0,
m1.rows()-2);
189 Index c = internal::random<Index>(0,
m1.cols()-1);
190 VERIFY_IS_APPROX((
m1.template block<1,Dynamic>(r,0,1,
m1.cols()).dot(rv)) , refM1.row(r).dot(rv));
260 m1 = m4; refM1 = refM4;
265 m1 = m4; refM1 = refM4;
268 m1 = m4; refM1 = refM4;
271 m1 = m4; refM1 = refM4;
274 m1 = m4; refM1 = refM4;
276 if(
m1.isCompressed())
281 for(
typename SparseMatrixType::InnerIterator it(
m1,
j); it; ++it)
282 refM1(it.row(), it.col()) += s1;
289 SpBool mb1 =
m1.real().template cast<bool>();
290 SpBool mb2 =
m2.real().template cast<bool>();
291 VERIFY_IS_EQUAL(mb1.template cast<int>().sum(), refM1.real().template cast<bool>().count());
292 VERIFY_IS_EQUAL((mb1 && mb2).
template cast<int>().sum(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count());
293 VERIFY_IS_EQUAL((mb1 || mb2).
template cast<int>().sum(), (refM1.real().template cast<bool>() || refM2.real().template cast<bool>()).count());
294 SpBool mb3 = mb1 && mb2;
295 if(mb1.coeffs().all() && mb2.coeffs().all())
297 VERIFY_IS_EQUAL(mb3.nonZeros(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count());
306 initSparse<Scalar>(
density, refMat2,
m2);
307 std::vector<Scalar> ref_value(
m2.innerSize());
308 std::vector<Index> ref_index(
m2.innerSize());
309 if(internal::random<bool>())
313 Index count_forward = 0;
315 for(
typename SparseMatrixType::InnerIterator it(
m2,
j); it; ++it)
317 ref_value[ref_value.size()-1-count_forward] = it.value();
318 ref_index[ref_index.size()-1-count_forward] = it.index();
321 Index count_reverse = 0;
322 for(
typename SparseMatrixType::ReverseInnerIterator it(
m2,
j); it; --it)
325 VERIFY_IS_EQUAL( ref_index[ref_index.size()-count_forward+count_reverse] , it.index());
336 initSparse<Scalar>(
density, refMat2,
m2);
352 int countFalseNonZero = 0;
353 int countTrueNonZero = 0;
354 m2.reserve(VectorXi::Constant(
m2.outerSize(),
int(
m2.innerSize())));
359 float x = internal::random<float>(0,1);
377 if(internal::random<bool>())
379 VERIFY(countFalseNonZero+countTrueNonZero ==
m2.nonZeros());
380 if(countTrueNonZero>0)
383 VERIFY(countTrueNonZero==
m2.nonZeros());
390 std::vector<TripletType> triplets;
392 triplets.reserve(ntriplets);
399 StorageIndex r = internal::random<StorageIndex>(0,StorageIndex(
rows-1));
400 StorageIndex
c = internal::random<StorageIndex>(0,StorageIndex(
cols-1));
401 Scalar v = internal::random<Scalar>();
402 triplets.push_back(TripletType(r,
c,
v));
403 refMat_sum(r,
c) +=
v;
405 refMat_prod(r,
c) =
v;
407 refMat_prod(r,
c) *=
v;
408 refMat_last(r,
c) =
v;
411 m.setFromTriplets(triplets.begin(), triplets.end());
414 m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
416 #if (EIGEN_COMP_CXXVER >= 11)
417 m.setFromTriplets(triplets.begin(), triplets.end(), [] (
Scalar,
Scalar b) { return b; });
426 initSparse<Scalar>(
density, refMat2,
m2);
427 initSparse<Scalar>(
density, refMat3,
m3);
443 m2.coeffRef(
i,
j) = 123;
444 if(internal::random<bool>())
449 mapMat2.coeffRef(
i,
j) = -123;
457 initSparse<Scalar>(
density, refMat2,
m2);
462 refMat3 = refMat2.template triangularView<Upper>();
463 m3 =
m2.template triangularView<Upper>();
467 refMat3 = refMat2.template triangularView<UnitUpper>();
468 m3 =
m2.template triangularView<UnitUpper>();
471 refMat3 = refMat2.template triangularView<UnitLower>();
472 m3 =
m2.template triangularView<UnitLower>();
476 refMat3 = refMat2.template triangularView<StrictlyUpper>();
477 m3 =
m2.template triangularView<StrictlyUpper>();
480 refMat3 = refMat2.template triangularView<StrictlyLower>();
481 m3 =
m2.template triangularView<StrictlyLower>();
485 refMat3 =
m2.template triangularView<StrictlyUpper>();
490 if(!SparseMatrixType::IsRowMajor)
494 initSparse<Scalar>(
density, refMat2,
m2);
495 refMat3 = refMat2.template selfadjointView<Lower>();
496 m3 =
m2.template selfadjointView<Lower>();
499 refMat3 += refMat2.template selfadjointView<Lower>();
500 m3 +=
m2.template selfadjointView<Lower>();
503 refMat3 -= refMat2.template selfadjointView<Lower>();
504 m3 -=
m2.template selfadjointView<Lower>();
517 initSparse<Scalar>(
density, refMat2,
m2);
531 initSparse<Scalar>(
density, refMat2,
m2);
535 d =
m2.diagonal().array();
540 m2.diagonal() += refMat2.diagonal();
541 refMat2.diagonal() += refMat2.diagonal();
552 SparseMatrixType
m3(
d.asDiagonal());
554 refMat2 +=
d.asDiagonal();
555 m2 +=
d.asDiagonal();
557 m2.setZero();
m2 +=
d.asDiagonal();
558 refMat2.setZero(); refMat2 +=
d.asDiagonal();
560 m2.setZero();
m2 -=
d.asDiagonal();
561 refMat2.setZero(); refMat2 -=
d.asDiagonal();
564 initSparse<Scalar>(
density, refMat2,
m2);
566 m2 +=
d.asDiagonal();
567 refMat2 +=
d.asDiagonal();
570 initSparse<Scalar>(
density, refMat2,
m2);
574 res(
i) = internal::random<int>(0,3);
576 m2 -=
d.asDiagonal();
577 refMat2 -=
d.asDiagonal();
583 std::vector< std::pair<StorageIndex,StorageIndex> > inc;
585 inc.push_back(std::pair<StorageIndex,StorageIndex>(-3,-2));
586 inc.push_back(std::pair<StorageIndex,StorageIndex>(0,0));
587 inc.push_back(std::pair<StorageIndex,StorageIndex>(3,2));
588 inc.push_back(std::pair<StorageIndex,StorageIndex>(3,0));
589 inc.push_back(std::pair<StorageIndex,StorageIndex>(0,3));
590 inc.push_back(std::pair<StorageIndex,StorageIndex>(0,-1));
591 inc.push_back(std::pair<StorageIndex,StorageIndex>(-1,0));
592 inc.push_back(std::pair<StorageIndex,StorageIndex>(-1,-1));
594 for(
size_t i = 0;
i< inc.size();
i++) {
595 StorageIndex incRows = inc[
i].first;
596 StorageIndex incCols = inc[
i].second;
599 initSparse<Scalar>(
density, refMat1,
m1);
601 SparseMatrixType
m2 =
m1;
604 m1.conservativeResize(
rows+incRows,
cols+incCols);
605 m2.conservativeResize(
rows+incRows,
cols+incCols);
606 refMat1.conservativeResize(
rows+incRows,
cols+incCols);
607 if (incRows > 0) refMat1.bottomRows(incRows).setZero();
608 if (incCols > 0) refMat1.rightCols(incCols).setZero();
615 m1.insert(
m1.rows()-1, 0) = refMat1(refMat1.rows()-1, 0) = 1;
617 m1.insert(0,
m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1;
635 Scalar v = internal::random<Scalar>();
636 m1.coeffRef(
i,
j) =
v;
637 refMat1.coeffRef(
i,
j) =
v;
639 if(internal::random<Index>(0,10)<2)
643 refMat1.setIdentity();
649 typedef typename SparseMatrixType::InnerIterator IteratorType;
653 initSparse<Scalar>(
density, refMat2,
m2);
654 IteratorType static_array[2];
655 static_array[0] = IteratorType(
m2,0);
656 static_array[1] = IteratorType(
m2,
m2.outerSize()-1);
657 VERIFY( static_array[0] ||
m2.innerVector(static_array[0].outer()).nonZeros() == 0 );
658 VERIFY( static_array[1] ||
m2.innerVector(static_array[1].outer()).nonZeros() == 0 );
659 if(static_array[0] && static_array[1])
662 static_array[1] = IteratorType(
m2,0);
663 VERIFY( static_array[1] );
664 VERIFY( static_array[1].index() == static_array[0].index() );
665 VERIFY( static_array[1].outer() == static_array[0].outer() );
669 std::vector<IteratorType> iters(2);
670 iters[0] = IteratorType(
m2,0);
671 iters[1] = IteratorType(
m2,
m2.outerSize()-1);
676 SparseMatrixType
m1(0,
cols);
677 m1.reserve(ArrayXi::Constant(
m1.outerSize(),1));
678 SparseMatrixType
m2(
rows,0);
679 m2.reserve(ArrayXi::Constant(
m2.outerSize(),1));
684 template<
typename SparseMatrixType>
686 typedef typename SparseMatrixType::StorageIndex StorageIndex;
689 std::vector<TripletType> triplets;
693 triplets.reserve(ntriplets);
697 Index r = internal::random<Index>(0,
rows-1);
701 triplets.push_back(TripletType(r,
c,
v));
705 m.setFromTriplets(triplets.begin(), triplets.end());
706 VERIFY(
m.nonZeros() <= ntriplets);
714 int n = Eigen::internal::random<int>(200,600);
716 std::complex<double> val;
718 for(
int i=0;
i<
n; ++
i)
720 mat.coeffRef(
i,
i%(
n/10)) = val;
725 #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA
731 int r = Eigen::internal::random<int>(1,200),
c = Eigen::internal::random<int>(1,200);
732 if(Eigen::internal::random<int>(0,4) == 0) {
744 r = Eigen::internal::random<int>(1,100);
745 c = Eigen::internal::random<int>(1,100);
746 if(Eigen::internal::random<int>(0,4) == 0) {