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>();
45 SparseMatrixType
m(rows, cols);
46 DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
47 DenseVector
vec1 = DenseVector::Random(rows);
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);
76 DenseMatrix
m1(rows,cols);
78 SparseMatrixType
m2(rows,cols);
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)
93 Index i = internal::random<Index>(0,rows-1);
95 m2.insert(i,
j) =
m1(i,
j) = internal::random<Scalar>();
99 if(call_reserve && !SparseMatrixType::IsRowMajor)
110 DenseMatrix
m1(rows,cols);
112 SparseMatrixType
m2(rows,cols);
113 if(internal::random<int>()%2)
114 m2.reserve(VectorXi::Constant(m2.outerSize(), 2));
115 for (
int k=0; k<rows*
cols; ++k)
117 Index i = internal::random<Index>(0,rows-1);
118 Index j = internal::random<Index>(0,cols-1);
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;
134 DenseMatrix
m1(rows,cols);
136 SparseMatrixType
m2(rows,cols);
137 VectorXi r(VectorXi::Constant(m2.outerSize(), ((
mode%2)==0) ?
int(m2.innerSize()) : std::max<int>(1,
int(m2.innerSize())/8)));
141 Index i = internal::random<Index>(0,rows-1);
142 Index j = internal::random<Index>(0,cols-1);
143 if (m1.coeff(i,j)==
Scalar(0))
144 m2.insert(i,j) =
m1(i,j) = internal::random<Scalar>();
148 if(internal::random<int>()%2)
155 DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
156 DenseMatrix refM2 = DenseMatrix::Zero(rows, cols);
157 DenseMatrix refM3 = DenseMatrix::Zero(rows, cols);
158 DenseMatrix refM4 = DenseMatrix::Zero(rows, cols);
159 SparseMatrixType
m1(rows, cols);
160 SparseMatrixType
m2(rows, cols);
161 SparseMatrixType m3(rows, cols);
162 SparseMatrixType m4(rows, cols);
163 initSparse<Scalar>(density, refM1,
m1);
164 initSparse<Scalar>(density, refM2,
m2);
165 initSparse<Scalar>(density, refM3, m3);
166 initSparse<Scalar>(density, refM4, m4);
168 if(internal::random<bool>())
171 Index m1_nnz = m1.nonZeros();
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)));
186 DenseVector rv = DenseVector::Random(m1.cols());
187 DenseVector cv = DenseVector::Random(m1.rows());
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));
253 if (rows>=2 && cols>=2)
260 m1 = m4; refM1 = refM4;
265 m1 = m4; refM1 = refM4;
266 VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval()));
268 m1 = m4; refM1 = refM4;
269 VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval()));
271 m1 = m4; refM1 = refM4;
274 m1 = m4; refM1 = refM4;
276 if(m1.isCompressed())
280 for(
Index j = 0;
j<m1.outerSize(); ++
j)
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());
304 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
305 SparseMatrixType
m2(rows, cols);
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>())
311 for(
Index j = 0;
j<m2.outerSize(); ++
j)
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());
334 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
335 SparseMatrixType
m2(rows, cols);
336 initSparse<Scalar>(density, refMat2,
m2);
349 SparseMatrixType
m2(rows, cols);
350 DenseMatrix refM2(rows, cols);
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);
393 DenseMatrix refMat_sum = DenseMatrix::Zero(rows,cols);
394 DenseMatrix refMat_prod = DenseMatrix::Zero(rows,cols);
395 DenseMatrix refMat_last = DenseMatrix::Zero(rows,cols);
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;
410 SparseMatrixType
m(rows,cols);
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; });
424 DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
425 SparseMatrixType
m2(rows, cols), m3(rows, cols);
426 initSparse<Scalar>(density, refMat2,
m2);
427 initSparse<Scalar>(density, refMat3, m3);
430 Map<SparseMatrixType> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr());
441 Index i = internal::random<Index>(0,rows-1);
442 Index j = internal::random<Index>(0,cols-1);
443 m2.coeffRef(i,j) = 123;
444 if(internal::random<bool>())
449 mapMat2.coeffRef(i,j) = -123;
455 DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
456 SparseMatrixType
m2(rows, cols), m3(rows, cols);
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)
492 DenseMatrix refMat2(rows, rows), refMat3(rows, rows);
493 SparseMatrixType
m2(rows, rows), m3(rows, rows);
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>();
508 SparseMatrixType m4(rows, rows+1);
515 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
516 SparseMatrixType
m2(rows, rows);
517 initSparse<Scalar>(density, refMat2,
m2);
529 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
530 SparseMatrixType
m2(rows, cols);
531 initSparse<Scalar>(density, refMat2,
m2);
533 DenseVector
d = m2.diagonal();
535 d = m2.diagonal().array();
540 m2.diagonal() += refMat2.diagonal();
541 refMat2.diagonal() += refMat2.diagonal();
547 DenseVector
d = DenseVector::Random(rows);
548 DenseMatrix refMat2 = d.asDiagonal();
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;
584 if(rows > 3 && cols > 2)
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;
597 SparseMatrixType
m1(rows, cols);
598 DenseMatrix refMat1 = DenseMatrix::Zero(rows, cols);
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;
627 DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows);
628 SparseMatrixType
m1(rows, rows);
631 for(
int k=0; k<rows*rows/4; ++k)
633 Index i = internal::random<Index>(0,rows-1);
634 Index j = internal::random<Index>(0,rows-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;
651 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
652 SparseMatrixType
m2(rows, cols);
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;
690 double nelements = density * rows*
cols;
693 triplets.reserve(ntriplets);
697 Index r = internal::random<Index>(0,rows-1);
698 Index c = internal::random<Index>(0,cols-1);
701 triplets.push_back(TripletType(r,c,v));
704 SparseMatrixType
m(rows,cols);
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;
721 VERIFY(mat.data().allocatedSize()<20*
n);
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) {
EIGEN_DEVICE_FUNC RealReturnType real() const
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
#define VERIFY_RAISES_ASSERT(a)
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
A versatible sparse matrix representation.
A matrix or vector expression mapping an existing array of data.
#define CALL_SUBTEST_3(FUNC)
Expression of the transpose of a matrix.
#define CALL_SUBTEST_7(FUNC)
void diagonal(const MatrixType &m)
static long g_dense_op_sparse_count
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
void big_sparse_triplet(Index rows, Index cols, double density)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Matrix< Scalar, Dynamic, 1 > DenseVector
#define VERIFY_IS_APPROX(a, b)
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
#define VERIFY_IS_EQUAL(a, b)
#define CALL_SUBTEST_1(FUNC)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Array< int, Dynamic, 1 > v
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
NumTraits< Scalar >::Real RealScalar
Reference counting helper.
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
EIGEN_DECLARE_TEST(sparse_basic)
A small structure to hold a non zero as a triplet (i,j,value).
#define CALL_SUBTEST_5(FUNC)
A triangularView< Lower >().adjoint().solveInPlace(B)
void sparse_basic(const SparseMatrixType &ref)
#define CALL_SUBTEST_2(FUNC)
internal::nested_eval< T, 1 >::type eval(const T &xpr)
static long g_realloc_count
static const DiscreteKey mode(modeKey, 2)
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
#define EIGEN_UNUSED_VARIABLE(var)