You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

531 lines
19 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2008 Daniel Gomez Ferro <dgomezferro@gmail.com>
  6. // Copyright (C) 2013 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
  7. //
  8. // This Source Code Form is subject to the terms of the Mozilla
  9. // Public License v. 2.0. If a copy of the MPL was not distributed
  10. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  11. static long g_realloc_count = 0;
  12. #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++;
  13. #include "sparse.h"
  14. template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref)
  15. {
  16. typedef typename SparseMatrixType::StorageIndex StorageIndex;
  17. typedef Matrix<StorageIndex,2,1> Vector2;
  18. const Index rows = ref.rows();
  19. const Index cols = ref.cols();
  20. const Index inner = ref.innerSize();
  21. const Index outer = ref.outerSize();
  22. typedef typename SparseMatrixType::Scalar Scalar;
  23. enum { Flags = SparseMatrixType::Flags };
  24. double density = (std::max)(8./(rows*cols), 0.01);
  25. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  26. typedef Matrix<Scalar,Dynamic,1> DenseVector;
  27. Scalar eps = 1e-6;
  28. Scalar s1 = internal::random<Scalar>();
  29. {
  30. SparseMatrixType m(rows, cols);
  31. DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
  32. DenseVector vec1 = DenseVector::Random(rows);
  33. std::vector<Vector2> zeroCoords;
  34. std::vector<Vector2> nonzeroCoords;
  35. initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
  36. // test coeff and coeffRef
  37. for (std::size_t i=0; i<zeroCoords.size(); ++i)
  38. {
  39. VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
  40. if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
  41. VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[i].x(),zeroCoords[i].y()) = 5 );
  42. }
  43. VERIFY_IS_APPROX(m, refMat);
  44. if(!nonzeroCoords.empty()) {
  45. m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
  46. refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
  47. }
  48. VERIFY_IS_APPROX(m, refMat);
  49. // test assertion
  50. VERIFY_RAISES_ASSERT( m.coeffRef(-1,1) = 0 );
  51. VERIFY_RAISES_ASSERT( m.coeffRef(0,m.cols()) = 0 );
  52. }
  53. // test insert (inner random)
  54. {
  55. DenseMatrix m1(rows,cols);
  56. m1.setZero();
  57. SparseMatrixType m2(rows,cols);
  58. bool call_reserve = internal::random<int>()%2;
  59. Index nnz = internal::random<int>(1,int(rows)/2);
  60. if(call_reserve)
  61. {
  62. if(internal::random<int>()%2)
  63. m2.reserve(VectorXi::Constant(m2.outerSize(), int(nnz)));
  64. else
  65. m2.reserve(m2.outerSize() * nnz);
  66. }
  67. g_realloc_count = 0;
  68. for (Index j=0; j<cols; ++j)
  69. {
  70. for (Index k=0; k<nnz; ++k)
  71. {
  72. Index i = internal::random<Index>(0,rows-1);
  73. if (m1.coeff(i,j)==Scalar(0))
  74. m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
  75. }
  76. }
  77. if(call_reserve && !SparseMatrixType::IsRowMajor)
  78. {
  79. VERIFY(g_realloc_count==0);
  80. }
  81. m2.finalize();
  82. VERIFY_IS_APPROX(m2,m1);
  83. }
  84. // test insert (fully random)
  85. {
  86. DenseMatrix m1(rows,cols);
  87. m1.setZero();
  88. SparseMatrixType m2(rows,cols);
  89. if(internal::random<int>()%2)
  90. m2.reserve(VectorXi::Constant(m2.outerSize(), 2));
  91. for (int k=0; k<rows*cols; ++k)
  92. {
  93. Index i = internal::random<Index>(0,rows-1);
  94. Index j = internal::random<Index>(0,cols-1);
  95. if ((m1.coeff(i,j)==Scalar(0)) && (internal::random<int>()%2))
  96. m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
  97. else
  98. {
  99. Scalar v = internal::random<Scalar>();
  100. m2.coeffRef(i,j) += v;
  101. m1(i,j) += v;
  102. }
  103. }
  104. VERIFY_IS_APPROX(m2,m1);
  105. }
  106. // test insert (un-compressed)
  107. for(int mode=0;mode<4;++mode)
  108. {
  109. DenseMatrix m1(rows,cols);
  110. m1.setZero();
  111. SparseMatrixType m2(rows,cols);
  112. VectorXi r(VectorXi::Constant(m2.outerSize(), ((mode%2)==0) ? int(m2.innerSize()) : std::max<int>(1,int(m2.innerSize())/8)));
  113. m2.reserve(r);
  114. for (Index k=0; k<rows*cols; ++k)
  115. {
  116. Index i = internal::random<Index>(0,rows-1);
  117. Index j = internal::random<Index>(0,cols-1);
  118. if (m1.coeff(i,j)==Scalar(0))
  119. m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
  120. if(mode==3)
  121. m2.reserve(r);
  122. }
  123. if(internal::random<int>()%2)
  124. m2.makeCompressed();
  125. VERIFY_IS_APPROX(m2,m1);
  126. }
  127. // test basic computations
  128. {
  129. DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
  130. DenseMatrix refM2 = DenseMatrix::Zero(rows, cols);
  131. DenseMatrix refM3 = DenseMatrix::Zero(rows, cols);
  132. DenseMatrix refM4 = DenseMatrix::Zero(rows, cols);
  133. SparseMatrixType m1(rows, cols);
  134. SparseMatrixType m2(rows, cols);
  135. SparseMatrixType m3(rows, cols);
  136. SparseMatrixType m4(rows, cols);
  137. initSparse<Scalar>(density, refM1, m1);
  138. initSparse<Scalar>(density, refM2, m2);
  139. initSparse<Scalar>(density, refM3, m3);
  140. initSparse<Scalar>(density, refM4, m4);
  141. VERIFY_IS_APPROX(m1*s1, refM1*s1);
  142. VERIFY_IS_APPROX(m1+m2, refM1+refM2);
  143. VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3);
  144. VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2));
  145. VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2);
  146. VERIFY_IS_APPROX(m1*=s1, refM1*=s1);
  147. VERIFY_IS_APPROX(m1/=s1, refM1/=s1);
  148. VERIFY_IS_APPROX(m1+=m2, refM1+=refM2);
  149. VERIFY_IS_APPROX(m1-=m2, refM1-=refM2);
  150. if(SparseMatrixType::IsRowMajor)
  151. VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.row(0)), refM1.row(0).dot(refM2.row(0)));
  152. else
  153. VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.col(0)), refM1.col(0).dot(refM2.col(0)));
  154. DenseVector rv = DenseVector::Random(m1.cols());
  155. DenseVector cv = DenseVector::Random(m1.rows());
  156. Index r = internal::random<Index>(0,m1.rows()-2);
  157. Index c = internal::random<Index>(0,m1.cols()-1);
  158. VERIFY_IS_APPROX(( m1.template block<1,Dynamic>(r,0,1,m1.cols()).dot(rv)) , refM1.row(r).dot(rv));
  159. VERIFY_IS_APPROX(m1.row(r).dot(rv), refM1.row(r).dot(rv));
  160. VERIFY_IS_APPROX(m1.col(c).dot(cv), refM1.col(c).dot(cv));
  161. VERIFY_IS_APPROX(m1.conjugate(), refM1.conjugate());
  162. VERIFY_IS_APPROX(m1.real(), refM1.real());
  163. refM4.setRandom();
  164. // sparse cwise* dense
  165. VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4));
  166. // dense cwise* sparse
  167. VERIFY_IS_APPROX(refM4.cwiseProduct(m3), refM4.cwiseProduct(refM3));
  168. // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4);
  169. // test aliasing
  170. VERIFY_IS_APPROX((m1 = -m1), (refM1 = -refM1));
  171. VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval()));
  172. VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval()));
  173. VERIFY_IS_APPROX((m1 += -m1), (refM1 += -refM1));
  174. }
  175. // test transpose
  176. {
  177. DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
  178. SparseMatrixType m2(rows, cols);
  179. initSparse<Scalar>(density, refMat2, m2);
  180. VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
  181. VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
  182. VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint());
  183. // check isApprox handles opposite storage order
  184. typename Transpose<SparseMatrixType>::PlainObject m3(m2);
  185. VERIFY(m2.isApprox(m3));
  186. }
  187. // test prune
  188. {
  189. SparseMatrixType m2(rows, cols);
  190. DenseMatrix refM2(rows, cols);
  191. refM2.setZero();
  192. int countFalseNonZero = 0;
  193. int countTrueNonZero = 0;
  194. m2.reserve(VectorXi::Constant(m2.outerSize(), int(m2.innerSize())));
  195. for (Index j=0; j<m2.cols(); ++j)
  196. {
  197. for (Index i=0; i<m2.rows(); ++i)
  198. {
  199. float x = internal::random<float>(0,1);
  200. if (x<0.1)
  201. {
  202. // do nothing
  203. }
  204. else if (x<0.5)
  205. {
  206. countFalseNonZero++;
  207. m2.insert(i,j) = Scalar(0);
  208. }
  209. else
  210. {
  211. countTrueNonZero++;
  212. m2.insert(i,j) = Scalar(1);
  213. refM2(i,j) = Scalar(1);
  214. }
  215. }
  216. }
  217. if(internal::random<bool>())
  218. m2.makeCompressed();
  219. VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
  220. if(countTrueNonZero>0)
  221. VERIFY_IS_APPROX(m2, refM2);
  222. m2.prune(Scalar(1));
  223. VERIFY(countTrueNonZero==m2.nonZeros());
  224. VERIFY_IS_APPROX(m2, refM2);
  225. }
  226. // test setFromTriplets
  227. {
  228. typedef Triplet<Scalar,StorageIndex> TripletType;
  229. std::vector<TripletType> triplets;
  230. Index ntriplets = rows*cols;
  231. triplets.reserve(ntriplets);
  232. DenseMatrix refMat_sum = DenseMatrix::Zero(rows,cols);
  233. DenseMatrix refMat_prod = DenseMatrix::Zero(rows,cols);
  234. DenseMatrix refMat_last = DenseMatrix::Zero(rows,cols);
  235. for(Index i=0;i<ntriplets;++i)
  236. {
  237. StorageIndex r = internal::random<StorageIndex>(0,StorageIndex(rows-1));
  238. StorageIndex c = internal::random<StorageIndex>(0,StorageIndex(cols-1));
  239. Scalar v = internal::random<Scalar>();
  240. triplets.push_back(TripletType(r,c,v));
  241. refMat_sum(r,c) += v;
  242. if(std::abs(refMat_prod(r,c))==0)
  243. refMat_prod(r,c) = v;
  244. else
  245. refMat_prod(r,c) *= v;
  246. refMat_last(r,c) = v;
  247. }
  248. SparseMatrixType m(rows,cols);
  249. m.setFromTriplets(triplets.begin(), triplets.end());
  250. VERIFY_IS_APPROX(m, refMat_sum);
  251. m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
  252. VERIFY_IS_APPROX(m, refMat_prod);
  253. #if (defined(__cplusplus) && __cplusplus >= 201103L)
  254. m.setFromTriplets(triplets.begin(), triplets.end(), [] (Scalar,Scalar b) { return b; });
  255. VERIFY_IS_APPROX(m, refMat_last);
  256. #endif
  257. }
  258. // test Map
  259. {
  260. DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
  261. SparseMatrixType m2(rows, cols), m3(rows, cols);
  262. initSparse<Scalar>(density, refMat2, m2);
  263. initSparse<Scalar>(density, refMat3, m3);
  264. {
  265. Map<SparseMatrixType> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr());
  266. Map<SparseMatrixType> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr());
  267. VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
  268. VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
  269. }
  270. {
  271. MappedSparseMatrix<Scalar,SparseMatrixType::Options,StorageIndex> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr());
  272. MappedSparseMatrix<Scalar,SparseMatrixType::Options,StorageIndex> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr());
  273. VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
  274. VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
  275. }
  276. }
  277. // test triangularView
  278. {
  279. DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
  280. SparseMatrixType m2(rows, cols), m3(rows, cols);
  281. initSparse<Scalar>(density, refMat2, m2);
  282. refMat3 = refMat2.template triangularView<Lower>();
  283. m3 = m2.template triangularView<Lower>();
  284. VERIFY_IS_APPROX(m3, refMat3);
  285. refMat3 = refMat2.template triangularView<Upper>();
  286. m3 = m2.template triangularView<Upper>();
  287. VERIFY_IS_APPROX(m3, refMat3);
  288. if(inner>=outer) // FIXME this should be implemented for outer>inner as well
  289. {
  290. refMat3 = refMat2.template triangularView<UnitUpper>();
  291. m3 = m2.template triangularView<UnitUpper>();
  292. VERIFY_IS_APPROX(m3, refMat3);
  293. refMat3 = refMat2.template triangularView<UnitLower>();
  294. m3 = m2.template triangularView<UnitLower>();
  295. VERIFY_IS_APPROX(m3, refMat3);
  296. }
  297. refMat3 = refMat2.template triangularView<StrictlyUpper>();
  298. m3 = m2.template triangularView<StrictlyUpper>();
  299. VERIFY_IS_APPROX(m3, refMat3);
  300. refMat3 = refMat2.template triangularView<StrictlyLower>();
  301. m3 = m2.template triangularView<StrictlyLower>();
  302. VERIFY_IS_APPROX(m3, refMat3);
  303. // check sparse-traingular to dense
  304. refMat3 = m2.template triangularView<StrictlyUpper>();
  305. VERIFY_IS_APPROX(refMat3, DenseMatrix(refMat2.template triangularView<StrictlyUpper>()));
  306. }
  307. // test selfadjointView
  308. if(!SparseMatrixType::IsRowMajor)
  309. {
  310. DenseMatrix refMat2(rows, rows), refMat3(rows, rows);
  311. SparseMatrixType m2(rows, rows), m3(rows, rows);
  312. initSparse<Scalar>(density, refMat2, m2);
  313. refMat3 = refMat2.template selfadjointView<Lower>();
  314. m3 = m2.template selfadjointView<Lower>();
  315. VERIFY_IS_APPROX(m3, refMat3);
  316. // selfadjointView only works for square matrices:
  317. SparseMatrixType m4(rows, rows+1);
  318. VERIFY_RAISES_ASSERT(m4.template selfadjointView<Lower>());
  319. VERIFY_RAISES_ASSERT(m4.template selfadjointView<Upper>());
  320. }
  321. // test sparseView
  322. {
  323. DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
  324. SparseMatrixType m2(rows, rows);
  325. initSparse<Scalar>(density, refMat2, m2);
  326. VERIFY_IS_APPROX(m2.eval(), refMat2.sparseView().eval());
  327. }
  328. // test diagonal
  329. {
  330. DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
  331. SparseMatrixType m2(rows, cols);
  332. initSparse<Scalar>(density, refMat2, m2);
  333. VERIFY_IS_APPROX(m2.diagonal(), refMat2.diagonal().eval());
  334. VERIFY_IS_APPROX(const_cast<const SparseMatrixType&>(m2).diagonal(), refMat2.diagonal().eval());
  335. initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag);
  336. m2.diagonal() += refMat2.diagonal();
  337. refMat2.diagonal() += refMat2.diagonal();
  338. VERIFY_IS_APPROX(m2, refMat2);
  339. }
  340. // test diagonal to sparse
  341. {
  342. DenseVector d = DenseVector::Random(rows);
  343. DenseMatrix refMat2 = d.asDiagonal();
  344. SparseMatrixType m2(rows, rows);
  345. m2 = d.asDiagonal();
  346. VERIFY_IS_APPROX(m2, refMat2);
  347. SparseMatrixType m3(d.asDiagonal());
  348. VERIFY_IS_APPROX(m3, refMat2);
  349. refMat2 += d.asDiagonal();
  350. m2 += d.asDiagonal();
  351. VERIFY_IS_APPROX(m2, refMat2);
  352. }
  353. // test conservative resize
  354. {
  355. std::vector< std::pair<StorageIndex,StorageIndex> > inc;
  356. if(rows > 3 && cols > 2)
  357. inc.push_back(std::pair<StorageIndex,StorageIndex>(-3,-2));
  358. inc.push_back(std::pair<StorageIndex,StorageIndex>(0,0));
  359. inc.push_back(std::pair<StorageIndex,StorageIndex>(3,2));
  360. inc.push_back(std::pair<StorageIndex,StorageIndex>(3,0));
  361. inc.push_back(std::pair<StorageIndex,StorageIndex>(0,3));
  362. for(size_t i = 0; i< inc.size(); i++) {
  363. StorageIndex incRows = inc[i].first;
  364. StorageIndex incCols = inc[i].second;
  365. SparseMatrixType m1(rows, cols);
  366. DenseMatrix refMat1 = DenseMatrix::Zero(rows, cols);
  367. initSparse<Scalar>(density, refMat1, m1);
  368. m1.conservativeResize(rows+incRows, cols+incCols);
  369. refMat1.conservativeResize(rows+incRows, cols+incCols);
  370. if (incRows > 0) refMat1.bottomRows(incRows).setZero();
  371. if (incCols > 0) refMat1.rightCols(incCols).setZero();
  372. VERIFY_IS_APPROX(m1, refMat1);
  373. // Insert new values
  374. if (incRows > 0)
  375. m1.insert(m1.rows()-1, 0) = refMat1(refMat1.rows()-1, 0) = 1;
  376. if (incCols > 0)
  377. m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1;
  378. VERIFY_IS_APPROX(m1, refMat1);
  379. }
  380. }
  381. // test Identity matrix
  382. {
  383. DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows);
  384. SparseMatrixType m1(rows, rows);
  385. m1.setIdentity();
  386. VERIFY_IS_APPROX(m1, refMat1);
  387. for(int k=0; k<rows*rows/4; ++k)
  388. {
  389. Index i = internal::random<Index>(0,rows-1);
  390. Index j = internal::random<Index>(0,rows-1);
  391. Scalar v = internal::random<Scalar>();
  392. m1.coeffRef(i,j) = v;
  393. refMat1.coeffRef(i,j) = v;
  394. VERIFY_IS_APPROX(m1, refMat1);
  395. if(internal::random<Index>(0,10)<2)
  396. m1.makeCompressed();
  397. }
  398. m1.setIdentity();
  399. refMat1.setIdentity();
  400. VERIFY_IS_APPROX(m1, refMat1);
  401. }
  402. }
  403. template<typename SparseMatrixType>
  404. void big_sparse_triplet(Index rows, Index cols, double density) {
  405. typedef typename SparseMatrixType::StorageIndex StorageIndex;
  406. typedef typename SparseMatrixType::Scalar Scalar;
  407. typedef Triplet<Scalar,Index> TripletType;
  408. std::vector<TripletType> triplets;
  409. double nelements = density * rows*cols;
  410. VERIFY(nelements>=0 && nelements < NumTraits<StorageIndex>::highest());
  411. Index ntriplets = Index(nelements);
  412. triplets.reserve(ntriplets);
  413. Scalar sum = Scalar(0);
  414. for(Index i=0;i<ntriplets;++i)
  415. {
  416. Index r = internal::random<Index>(0,rows-1);
  417. Index c = internal::random<Index>(0,cols-1);
  418. Scalar v = internal::random<Scalar>();
  419. triplets.push_back(TripletType(r,c,v));
  420. sum += v;
  421. }
  422. SparseMatrixType m(rows,cols);
  423. m.setFromTriplets(triplets.begin(), triplets.end());
  424. VERIFY(m.nonZeros() <= ntriplets);
  425. VERIFY_IS_APPROX(sum, m.sum());
  426. }
  427. void test_sparse_basic()
  428. {
  429. for(int i = 0; i < g_repeat; i++) {
  430. int r = StormEigen::internal::random<int>(1,200), c = StormEigen::internal::random<int>(1,200);
  431. if(StormEigen::internal::random<int>(0,4) == 0) {
  432. r = c; // check square matrices in 25% of tries
  433. }
  434. EIGEN_UNUSED_VARIABLE(r+c);
  435. CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(1, 1)) ));
  436. CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(8, 8)) ));
  437. CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, ColMajor>(r, c)) ));
  438. CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, RowMajor>(r, c)) ));
  439. CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(r, c)) ));
  440. CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,ColMajor,long int>(r, c)) ));
  441. CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,RowMajor,long int>(r, c)) ));
  442. r = StormEigen::internal::random<int>(1,100);
  443. c = StormEigen::internal::random<int>(1,100);
  444. if(StormEigen::internal::random<int>(0,4) == 0) {
  445. r = c; // check square matrices in 25% of tries
  446. }
  447. CALL_SUBTEST_6(( sparse_basic(SparseMatrix<double,ColMajor,short int>(short(r), short(c))) ));
  448. CALL_SUBTEST_6(( sparse_basic(SparseMatrix<double,RowMajor,short int>(short(r), short(c))) ));
  449. }
  450. // Regression test for bug 900: (manually insert higher values here, if you have enough RAM):
  451. CALL_SUBTEST_3((big_sparse_triplet<SparseMatrix<float, RowMajor, int> >(10000, 10000, 0.125)));
  452. CALL_SUBTEST_4((big_sparse_triplet<SparseMatrix<double, ColMajor, long int> >(10000, 10000, 0.125)));
  453. // Regression test for bug 1105
  454. #ifdef EIGEN_TEST_PART_6
  455. {
  456. int n = StormEigen::internal::random<int>(200,600);
  457. SparseMatrix<std::complex<double>,0, long> mat(n, n);
  458. std::complex<double> val;
  459. for(int i=0; i<n; ++i)
  460. {
  461. mat.coeffRef(i, i%(n/10)) = val;
  462. VERIFY(mat.data().allocatedSize()<20*n);
  463. }
  464. }
  465. #endif
  466. }