13 #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++; 17 template<
typename SparseMatrixType>
void sparse_basic(
const SparseMatrixType&
ref)
19 typedef typename SparseMatrixType::StorageIndex StorageIndex;
29 enum { Flags = SparseMatrixType::Flags };
31 double density = (
std::max)(8./(rows*cols), 0.01);
36 Scalar s1 = internal::random<Scalar>();
38 SparseMatrixType
m(rows, cols);
39 DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
40 DenseVector
vec1 = DenseVector::Random(rows);
42 std::vector<Vector2> zeroCoords;
43 std::vector<Vector2> nonzeroCoords;
44 initSparse<Scalar>(density, refMat,
m, 0, &zeroCoords, &nonzeroCoords);
55 if(!nonzeroCoords.empty()) {
56 m.coeffRef(nonzeroCoords[0].
x(), nonzeroCoords[0].
y()) =
Scalar(5);
57 refMat.coeffRef(nonzeroCoords[0].
x(), nonzeroCoords[0].
y()) =
Scalar(5);
69 DenseMatrix
m1(rows,cols);
71 SparseMatrixType
m2(rows,cols);
72 bool call_reserve = internal::random<int>()%2;
73 Index nnz = internal::random<int>(1,
int(rows)/2);
76 if(internal::random<int>()%2)
77 m2.reserve(VectorXi::Constant(m2.outerSize(),
int(nnz)));
79 m2.reserve(m2.outerSize() * nnz);
84 for (
Index k=0; k<nnz; ++k)
86 Index i = internal::random<Index>(0,rows-1);
88 m2.insert(i,
j) =
m1(i,
j) = internal::random<Scalar>();
92 if(call_reserve && !SparseMatrixType::IsRowMajor)
103 DenseMatrix
m1(rows,cols);
105 SparseMatrixType
m2(rows,cols);
106 if(internal::random<int>()%2)
107 m2.reserve(VectorXi::Constant(m2.outerSize(), 2));
108 for (
int k=0; k<rows*
cols; ++k)
110 Index i = internal::random<Index>(0,rows-1);
111 Index j = internal::random<Index>(0,cols-1);
112 if ((m1.coeff(i,j)==
Scalar(0)) && (internal::random<int>()%2))
113 m2.insert(i,j) =
m1(i,j) = internal::random<Scalar>();
116 Scalar
v = internal::random<Scalar>();
117 m2.coeffRef(i,j) +=
v;
125 for(
int mode=0;mode<4;++mode)
127 DenseMatrix
m1(rows,cols);
129 SparseMatrixType
m2(rows,cols);
130 VectorXi r(VectorXi::Constant(m2.outerSize(), ((mode%2)==0) ?
int(m2.innerSize()) : std::max<int>(1,
int(m2.innerSize())/8)));
134 Index i = internal::random<Index>(0,rows-1);
135 Index j = internal::random<Index>(0,cols-1);
136 if (m1.coeff(i,j)==
Scalar(0))
137 m2.insert(i,j) =
m1(i,j) = internal::random<Scalar>();
141 if(internal::random<int>()%2)
148 DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
149 DenseMatrix refM2 = DenseMatrix::Zero(rows, cols);
150 DenseMatrix refM3 = DenseMatrix::Zero(rows, cols);
151 DenseMatrix refM4 = DenseMatrix::Zero(rows, cols);
152 SparseMatrixType
m1(rows, cols);
153 SparseMatrixType
m2(rows, cols);
154 SparseMatrixType m3(rows, cols);
155 SparseMatrixType m4(rows, cols);
156 initSparse<Scalar>(density, refM1,
m1);
157 initSparse<Scalar>(density, refM2,
m2);
158 initSparse<Scalar>(density, refM3, m3);
159 initSparse<Scalar>(density, refM4, m4);
161 if(internal::random<bool>())
164 Index m1_nnz = m1.nonZeros();
174 if(SparseMatrixType::IsRowMajor)
175 VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.row(0)), refM1.row(0).dot(refM2.row(0)));
177 VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.col(0)), refM1.col(0).dot(refM2.col(0)));
179 DenseVector rv = DenseVector::Random(m1.cols());
180 DenseVector cv = DenseVector::Random(m1.rows());
181 Index r = internal::random<Index>(0,m1.rows()-2);
182 Index c = internal::random<Index>(0,m1.cols()-1);
183 VERIFY_IS_APPROX(( m1.template block<1,Dynamic>(r,0,1,m1.cols()).
dot(rv)) , refM1.row(r).dot(rv));
225 if (rows>=2 && cols>=2)
232 m1 = m4; refM1 = refM4;
237 m1 = m4; refM1 = refM4;
238 VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval()));
240 m1 = m4; refM1 = refM4;
241 VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval()));
243 m1 = m4; refM1 = refM4;
246 m1 = m4; refM1 = refM4;
248 if(m1.isCompressed())
252 for(
Index j = 0;
j<m1.outerSize(); ++
j)
253 for(
typename SparseMatrixType::InnerIterator it(m1,
j); it; ++it)
254 refM1(it.row(), it.col()) += s1;
261 SpBool mb1 = m1.
real().template cast<bool>();
262 SpBool mb2 = m2.real().template cast<bool>();
263 VERIFY_IS_EQUAL(mb1.template cast<int>().sum(), refM1.real().template cast<bool>().count());
264 VERIFY_IS_EQUAL((mb1 && mb2).
template cast<int>().
sum(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count());
265 VERIFY_IS_EQUAL((mb1 || mb2).
template cast<int>().
sum(), (refM1.real().template cast<bool>() || refM2.real().template cast<bool>()).count());
266 SpBool mb3 = mb1 && mb2;
267 if(mb1.coeffs().all() && mb2.coeffs().all())
269 VERIFY_IS_EQUAL(mb3.nonZeros(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count());
276 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
277 SparseMatrixType
m2(rows, cols);
278 initSparse<Scalar>(density, refMat2,
m2);
279 std::vector<Scalar> ref_value(m2.innerSize());
280 std::vector<Index> ref_index(m2.innerSize());
281 if(internal::random<bool>())
283 for(
Index j = 0;
j<m2.outerSize(); ++
j)
285 Index count_forward = 0;
287 for(
typename SparseMatrixType::InnerIterator it(m2,
j); it; ++it)
289 ref_value[ref_value.size()-1-count_forward] = it.value();
290 ref_index[ref_index.size()-1-count_forward] = it.index();
293 Index count_reverse = 0;
294 for(
typename SparseMatrixType::ReverseInnerIterator it(m2,
j); it; --it)
297 VERIFY_IS_EQUAL( ref_index[ref_index.size()-count_forward+count_reverse] , it.index());
306 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
307 SparseMatrixType
m2(rows, cols);
308 initSparse<Scalar>(density, refMat2,
m2);
321 SparseMatrixType
m2(rows, cols);
322 DenseMatrix refM2(rows, cols);
324 int countFalseNonZero = 0;
325 int countTrueNonZero = 0;
326 m2.reserve(VectorXi::Constant(m2.outerSize(),
int(m2.innerSize())));
331 float x = internal::random<float>(0,1);
349 if(internal::random<bool>())
351 VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
352 if(countTrueNonZero>0)
355 VERIFY(countTrueNonZero==m2.nonZeros());
362 std::vector<TripletType> triplets;
364 triplets.reserve(ntriplets);
365 DenseMatrix refMat_sum = DenseMatrix::Zero(rows,cols);
366 DenseMatrix refMat_prod = DenseMatrix::Zero(rows,cols);
367 DenseMatrix refMat_last = DenseMatrix::Zero(rows,cols);
371 StorageIndex r = internal::random<StorageIndex>(0,StorageIndex(rows-1));
372 StorageIndex
c = internal::random<StorageIndex>(0,StorageIndex(cols-1));
373 Scalar
v = internal::random<Scalar>();
374 triplets.push_back(TripletType(r,c,v));
375 refMat_sum(r,c) +=
v;
377 refMat_prod(r,c) =
v;
379 refMat_prod(r,c) *=
v;
380 refMat_last(r,c) =
v;
382 SparseMatrixType
m(rows,cols);
383 m.setFromTriplets(triplets.begin(), triplets.end());
386 m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
388 #if (defined(__cplusplus) && __cplusplus >= 201103L) 389 m.setFromTriplets(triplets.begin(), triplets.end(), [] (
Scalar,Scalar
b) {
return b; });
396 DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
397 SparseMatrixType
m2(rows, cols), m3(rows, cols);
398 initSparse<Scalar>(density, refMat2,
m2);
399 initSparse<Scalar>(density, refMat3, m3);
402 Map<SparseMatrixType> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr());
413 Index i = internal::random<Index>(0,rows-1);
414 Index j = internal::random<Index>(0,cols-1);
415 m2.coeffRef(i,j) = 123;
416 if(internal::random<bool>())
421 mapMat2.coeffRef(i,j) = -123;
427 DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
428 SparseMatrixType
m2(rows, cols), m3(rows, cols);
429 initSparse<Scalar>(density, refMat2,
m2);
434 refMat3 = refMat2.template triangularView<Upper>();
435 m3 =
m2.template triangularView<Upper>();
439 refMat3 = refMat2.template triangularView<UnitUpper>();
440 m3 =
m2.template triangularView<UnitUpper>();
443 refMat3 = refMat2.template triangularView<UnitLower>();
444 m3 =
m2.template triangularView<UnitLower>();
448 refMat3 = refMat2.template triangularView<StrictlyUpper>();
449 m3 =
m2.template triangularView<StrictlyUpper>();
452 refMat3 = refMat2.template triangularView<StrictlyLower>();
453 m3 =
m2.template triangularView<StrictlyLower>();
457 refMat3 =
m2.template triangularView<StrictlyUpper>();
462 if(!SparseMatrixType::IsRowMajor)
464 DenseMatrix refMat2(rows, rows), refMat3(rows, rows);
465 SparseMatrixType
m2(rows, rows), m3(rows, rows);
466 initSparse<Scalar>(density, refMat2,
m2);
467 refMat3 = refMat2.template selfadjointView<Lower>();
468 m3 =
m2.template selfadjointView<Lower>();
471 refMat3 += refMat2.template selfadjointView<Lower>();
472 m3 +=
m2.template selfadjointView<Lower>();
475 refMat3 -= refMat2.template selfadjointView<Lower>();
476 m3 -=
m2.template selfadjointView<Lower>();
480 SparseMatrixType m4(rows, rows+1);
487 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
488 SparseMatrixType
m2(rows, rows);
489 initSparse<Scalar>(density, refMat2,
m2);
501 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
502 SparseMatrixType
m2(rows, cols);
503 initSparse<Scalar>(density, refMat2,
m2);
505 DenseVector
d = m2.diagonal();
507 d = m2.diagonal().array();
512 m2.diagonal() += refMat2.diagonal();
513 refMat2.diagonal() += refMat2.diagonal();
519 DenseVector
d = DenseVector::Random(rows);
520 DenseMatrix refMat2 = d.asDiagonal();
521 SparseMatrixType
m2(rows, rows);
524 SparseMatrixType m3(d.asDiagonal());
526 refMat2 += d.asDiagonal();
527 m2 += d.asDiagonal();
533 std::vector< std::pair<StorageIndex,StorageIndex> > inc;
534 if(rows > 3 && cols > 2)
535 inc.push_back(std::pair<StorageIndex,StorageIndex>(-3,-2));
536 inc.push_back(std::pair<StorageIndex,StorageIndex>(0,0));
537 inc.push_back(std::pair<StorageIndex,StorageIndex>(3,2));
538 inc.push_back(std::pair<StorageIndex,StorageIndex>(3,0));
539 inc.push_back(std::pair<StorageIndex,StorageIndex>(0,3));
541 for(
size_t i = 0;
i< inc.size();
i++) {
542 StorageIndex incRows = inc[
i].first;
543 StorageIndex incCols = inc[
i].second;
544 SparseMatrixType
m1(rows, cols);
545 DenseMatrix refMat1 = DenseMatrix::Zero(rows, cols);
546 initSparse<Scalar>(density, refMat1,
m1);
548 m1.conservativeResize(rows+incRows, cols+incCols);
549 refMat1.conservativeResize(rows+incRows, cols+incCols);
550 if (incRows > 0) refMat1.bottomRows(incRows).setZero();
551 if (incCols > 0) refMat1.rightCols(incCols).setZero();
557 m1.insert(m1.rows()-1, 0) = refMat1(refMat1.rows()-1, 0) = 1;
559 m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1;
569 DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows);
570 SparseMatrixType
m1(rows, rows);
573 for(
int k=0; k<rows*rows/4; ++k)
575 Index i = internal::random<Index>(0,rows-1);
576 Index j = internal::random<Index>(0,rows-1);
577 Scalar
v = internal::random<Scalar>();
578 m1.coeffRef(i,j) =
v;
579 refMat1.coeffRef(i,j) =
v;
581 if(internal::random<Index>(0,10)<2)
585 refMat1.setIdentity();
591 typedef typename SparseMatrixType::InnerIterator IteratorType;
593 DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
594 SparseMatrixType
m2(rows, cols);
595 initSparse<Scalar>(density, refMat2,
m2);
596 IteratorType static_array[2];
597 static_array[0] = IteratorType(m2,0);
598 static_array[1] = IteratorType(m2,m2.outerSize()-1);
599 VERIFY( static_array[0] || m2.innerVector(static_array[0].outer()).nonZeros() == 0 );
600 VERIFY( static_array[1] || m2.innerVector(static_array[1].outer()).nonZeros() == 0 );
601 if(static_array[0] && static_array[1])
604 static_array[1] = IteratorType(m2,0);
605 VERIFY( static_array[1] );
606 VERIFY( static_array[1].index() == static_array[0].index() );
607 VERIFY( static_array[1].outer() == static_array[0].outer() );
611 std::vector<IteratorType> iters(2);
612 iters[0] = IteratorType(m2,0);
613 iters[1] = IteratorType(m2,m2.outerSize()-1);
618 template<
typename SparseMatrixType>
620 typedef typename SparseMatrixType::StorageIndex StorageIndex;
623 std::vector<TripletType> triplets;
624 double nelements = density * rows*
cols;
627 triplets.reserve(ntriplets);
631 Index r = internal::random<Index>(0,rows-1);
632 Index c = internal::random<Index>(0,cols-1);
635 triplets.push_back(TripletType(r,c,v));
638 SparseMatrixType
m(rows,cols);
639 m.setFromTriplets(triplets.begin(), triplets.end());
640 VERIFY(m.nonZeros() <= ntriplets);
648 int r = Eigen::internal::random<int>(1,200),
c = Eigen::internal::random<int>(1,200);
649 if(Eigen::internal::random<int>(0,4) == 0) {
661 r = Eigen::internal::random<int>(1,100);
662 c = Eigen::internal::random<int>(1,100);
663 if(Eigen::internal::random<int>(0,4) == 0) {
676 #ifdef EIGEN_TEST_PART_7 678 int n = Eigen::internal::random<int>(200,600);
680 std::complex<double> val;
682 for(
int i=0;
i<
n; ++
i)
684 mat.coeffRef(
i,
i%(n/10)) = val;
685 VERIFY(mat.data().allocatedSize()<20*
n);
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
#define VERIFY_RAISES_ASSERT(a)
A versatible sparse matrix representation.
A matrix or vector expression mapping an existing array of data.
Expression of the transpose of a matrix.
void diagonal(const MatrixType &m)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
void big_sparse_triplet(Index rows, Index cols, double density)
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)
EIGEN_DEVICE_FUNC RealReturnType real() const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
NumTraits< Scalar >::Real RealScalar
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
A small structure to hold a non zero as a triplet (i,j,value).
A triangularView< Lower >().adjoint().solveInPlace(B)
void sparse_basic(const SparseMatrixType &ref)
internal::nested_eval< T, 1 >::type eval(const T &xpr)
static long g_realloc_count
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)