cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

sparse_basic.cpp (29343B)


      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 
     12 #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA
     13 static long g_realloc_count = 0;
     14 #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++;
     15 
     16 static long g_dense_op_sparse_count = 0;
     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;
     20 #endif
     21 
     22 #include "sparse.h"
     23 
     24 template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref)
     25 {
     26   typedef typename SparseMatrixType::StorageIndex StorageIndex;
     27   typedef Matrix<StorageIndex,2,1> Vector2;
     28   
     29   const Index rows = ref.rows();
     30   const Index cols = ref.cols();
     31   //const Index inner = ref.innerSize();
     32   //const Index outer = ref.outerSize();
     33 
     34   typedef typename SparseMatrixType::Scalar Scalar;
     35   typedef typename SparseMatrixType::RealScalar RealScalar;
     36   enum { Flags = SparseMatrixType::Flags };
     37 
     38   double density = (std::max)(8./(rows*cols), 0.01);
     39   typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
     40   typedef Matrix<Scalar,Dynamic,1> DenseVector;
     41   Scalar eps = 1e-6;
     42 
     43   Scalar s1 = internal::random<Scalar>();
     44   {
     45     SparseMatrixType m(rows, cols);
     46     DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
     47     DenseVector vec1 = DenseVector::Random(rows);
     48 
     49     std::vector<Vector2> zeroCoords;
     50     std::vector<Vector2> nonzeroCoords;
     51     initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
     52 
     53     // test coeff and coeffRef
     54     for (std::size_t i=0; i<zeroCoords.size(); ++i)
     55     {
     56       VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
     57       if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value)
     58         VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[i].x(),zeroCoords[i].y()) = 5 );
     59     }
     60     VERIFY_IS_APPROX(m, refMat);
     61 
     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);
     65     }
     66 
     67     VERIFY_IS_APPROX(m, refMat);
     68 
     69       // test assertion
     70       VERIFY_RAISES_ASSERT( m.coeffRef(-1,1) = 0 );
     71       VERIFY_RAISES_ASSERT( m.coeffRef(0,m.cols()) = 0 );
     72     }
     73 
     74     // test insert (inner random)
     75     {
     76       DenseMatrix m1(rows,cols);
     77       m1.setZero();
     78       SparseMatrixType m2(rows,cols);
     79       bool call_reserve = internal::random<int>()%2;
     80       Index nnz = internal::random<int>(1,int(rows)/2);
     81       if(call_reserve)
     82       {
     83         if(internal::random<int>()%2)
     84           m2.reserve(VectorXi::Constant(m2.outerSize(), int(nnz)));
     85         else
     86           m2.reserve(m2.outerSize() * nnz);
     87       }
     88       g_realloc_count = 0;
     89       for (Index j=0; j<cols; ++j)
     90       {
     91         for (Index k=0; k<nnz; ++k)
     92         {
     93           Index i = internal::random<Index>(0,rows-1);
     94           if (m1.coeff(i,j)==Scalar(0))
     95             m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
     96         }
     97       }
     98       
     99       if(call_reserve && !SparseMatrixType::IsRowMajor)
    100       {
    101         VERIFY(g_realloc_count==0);
    102       }
    103       
    104       m2.finalize();
    105       VERIFY_IS_APPROX(m2,m1);
    106     }
    107 
    108     // test insert (fully random)
    109     {
    110       DenseMatrix m1(rows,cols);
    111       m1.setZero();
    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)
    116       {
    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>();
    121         else
    122         {
    123           Scalar v = internal::random<Scalar>();
    124           m2.coeffRef(i,j) += v;
    125           m1(i,j) += v;
    126         }
    127       }
    128       VERIFY_IS_APPROX(m2,m1);
    129     }
    130     
    131     // test insert (un-compressed)
    132     for(int mode=0;mode<4;++mode)
    133     {
    134       DenseMatrix m1(rows,cols);
    135       m1.setZero();
    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)));
    138       m2.reserve(r);
    139       for (Index k=0; k<rows*cols; ++k)
    140       {
    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>();
    145         if(mode==3)
    146           m2.reserve(r);
    147       }
    148       if(internal::random<int>()%2)
    149         m2.makeCompressed();
    150       VERIFY_IS_APPROX(m2,m1);
    151     }
    152 
    153   // test basic computations
    154   {
    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);
    167 
    168     if(internal::random<bool>())
    169       m1.makeCompressed();
    170 
    171     Index m1_nnz = m1.nonZeros();
    172 
    173     VERIFY_IS_APPROX(m1*s1, refM1*s1);
    174     VERIFY_IS_APPROX(m1+m2, refM1+refM2);
    175     VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3);
    176     VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2));
    177     VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2);
    178     VERIFY_IS_APPROX(m4=m1/s1, refM1/s1);
    179     VERIFY_IS_EQUAL(m4.nonZeros(), m1_nnz);
    180 
    181     if(SparseMatrixType::IsRowMajor)
    182       VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.row(0)), refM1.row(0).dot(refM2.row(0)));
    183     else
    184       VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.col(0)), refM1.col(0).dot(refM2.col(0)));
    185 
    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));
    191     VERIFY_IS_APPROX(m1.row(r).dot(rv), refM1.row(r).dot(rv));
    192     VERIFY_IS_APPROX(m1.col(c).dot(cv), refM1.col(c).dot(cv));
    193 
    194     VERIFY_IS_APPROX(m1.conjugate(), refM1.conjugate());
    195     VERIFY_IS_APPROX(m1.real(), refM1.real());
    196 
    197     refM4.setRandom();
    198     // sparse cwise* dense
    199     VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4));
    200     // dense cwise* sparse
    201     VERIFY_IS_APPROX(refM4.cwiseProduct(m3), refM4.cwiseProduct(refM3));
    202 //     VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4);
    203 
    204     // mixed sparse-dense
    205     VERIFY_IS_APPROX(refM4 + m3, refM4 + refM3);
    206     VERIFY_IS_APPROX(m3 + refM4, refM3 + refM4);
    207     VERIFY_IS_APPROX(refM4 - m3, refM4 - refM3);
    208     VERIFY_IS_APPROX(m3 - refM4, refM3 - refM4);
    209     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3);
    210     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3*RealScalar(0.5)).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3);
    211     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3.cwiseProduct(m3)).eval(), RealScalar(0.5)*refM4 + refM3.cwiseProduct(refM3));
    212 
    213     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3);
    214     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3*RealScalar(0.5)).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3);
    215     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (m3+m3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3));
    216     VERIFY_IS_APPROX(((refM3+m3)+RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM3 + (refM3+refM3));
    217     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (refM3+m3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3));
    218     VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (m3+refM3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3));
    219 
    220 
    221     VERIFY_IS_APPROX(m1.sum(), refM1.sum());
    222 
    223     m4 = m1; refM4 = m4;
    224 
    225     VERIFY_IS_APPROX(m1*=s1, refM1*=s1);
    226     VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz);
    227     VERIFY_IS_APPROX(m1/=s1, refM1/=s1);
    228     VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz);
    229 
    230     VERIFY_IS_APPROX(m1+=m2, refM1+=refM2);
    231     VERIFY_IS_APPROX(m1-=m2, refM1-=refM2);
    232 
    233     refM3 = refM1;
    234     
    235     VERIFY_IS_APPROX(refM1+=m2, refM3+=refM2);
    236     VERIFY_IS_APPROX(refM1-=m2, refM3-=refM2);
    237 
    238     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =m2+refM4, refM3 =refM2+refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,10);
    239     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=m2+refM4, refM3+=refM2+refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    240     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=m2+refM4, refM3-=refM2+refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    241     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =refM4+m2, refM3 =refM2+refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    242     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=refM4+m2, refM3+=refM2+refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    243     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=refM4+m2, refM3-=refM2+refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    244 
    245     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =m2-refM4, refM3 =refM2-refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,20);
    246     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=m2-refM4, refM3+=refM2-refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    247     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=m2-refM4, refM3-=refM2-refM4);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    248     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =refM4-m2, refM3 =refM4-refM2);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    249     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=refM4-m2, refM3+=refM4-refM2);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    250     g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=refM4-m2, refM3-=refM4-refM2);  VERIFY_IS_EQUAL(g_dense_op_sparse_count,1);
    251     refM3 = m3;
    252 
    253     if (rows>=2 && cols>=2)
    254     {
    255       VERIFY_RAISES_ASSERT( m1 += m1.innerVector(0) );
    256       VERIFY_RAISES_ASSERT( m1 -= m1.innerVector(0) );
    257       VERIFY_RAISES_ASSERT( refM1 -= m1.innerVector(0) );
    258       VERIFY_RAISES_ASSERT( refM1 += m1.innerVector(0) );
    259     }
    260     m1 = m4; refM1 = refM4;
    261 
    262     // test aliasing
    263     VERIFY_IS_APPROX((m1 = -m1), (refM1 = -refM1));
    264     VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz);
    265     m1 = m4; refM1 = refM4;
    266     VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval()));
    267     VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz);
    268     m1 = m4; refM1 = refM4;
    269     VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval()));
    270     VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz);
    271     m1 = m4; refM1 = refM4;
    272     VERIFY_IS_APPROX((m1 += -m1), (refM1 += -refM1));
    273     VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz);
    274     m1 = m4; refM1 = refM4;
    275 
    276     if(m1.isCompressed())
    277     {
    278       VERIFY_IS_APPROX(m1.coeffs().sum(), m1.sum());
    279       m1.coeffs() += s1;
    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;
    283       VERIFY_IS_APPROX(m1, refM1);
    284     }
    285 
    286     // and/or
    287     {
    288       typedef SparseMatrix<bool, SparseMatrixType::Options, typename SparseMatrixType::StorageIndex> SpBool;
    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())
    296       {
    297         VERIFY_IS_EQUAL(mb3.nonZeros(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count());
    298       }
    299     }
    300   }
    301 
    302   // test reverse iterators
    303   {
    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>())
    310       m2.makeCompressed();
    311     for(Index j = 0; j<m2.outerSize(); ++j)
    312     {
    313       Index count_forward = 0;
    314 
    315       for(typename SparseMatrixType::InnerIterator it(m2,j); it; ++it)
    316       {
    317         ref_value[ref_value.size()-1-count_forward] = it.value();
    318         ref_index[ref_index.size()-1-count_forward] = it.index();
    319         count_forward++;
    320       }
    321       Index count_reverse = 0;
    322       for(typename SparseMatrixType::ReverseInnerIterator it(m2,j); it; --it)
    323       {
    324         VERIFY_IS_APPROX( std::abs(ref_value[ref_value.size()-count_forward+count_reverse])+1, std::abs(it.value())+1);
    325         VERIFY_IS_EQUAL( ref_index[ref_index.size()-count_forward+count_reverse] , it.index());
    326         count_reverse++;
    327       }
    328       VERIFY_IS_EQUAL(count_forward, count_reverse);
    329     }
    330   }
    331 
    332   // test transpose
    333   {
    334     DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
    335     SparseMatrixType m2(rows, cols);
    336     initSparse<Scalar>(density, refMat2, m2);
    337     VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
    338     VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
    339 
    340     VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint());
    341     
    342     // check isApprox handles opposite storage order
    343     typename Transpose<SparseMatrixType>::PlainObject m3(m2);
    344     VERIFY(m2.isApprox(m3));
    345   }
    346 
    347   // test prune
    348   {
    349     SparseMatrixType m2(rows, cols);
    350     DenseMatrix refM2(rows, cols);
    351     refM2.setZero();
    352     int countFalseNonZero = 0;
    353     int countTrueNonZero = 0;
    354     m2.reserve(VectorXi::Constant(m2.outerSize(), int(m2.innerSize())));
    355     for (Index j=0; j<m2.cols(); ++j)
    356     {
    357       for (Index i=0; i<m2.rows(); ++i)
    358       {
    359         float x = internal::random<float>(0,1);
    360         if (x<0.1f)
    361         {
    362           // do nothing
    363         }
    364         else if (x<0.5f)
    365         {
    366           countFalseNonZero++;
    367           m2.insert(i,j) = Scalar(0);
    368         }
    369         else
    370         {
    371           countTrueNonZero++;
    372           m2.insert(i,j) = Scalar(1);
    373           refM2(i,j) = Scalar(1);
    374         }
    375       }
    376     }
    377     if(internal::random<bool>())
    378       m2.makeCompressed();
    379     VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
    380     if(countTrueNonZero>0)
    381       VERIFY_IS_APPROX(m2, refM2);
    382     m2.prune(Scalar(1));
    383     VERIFY(countTrueNonZero==m2.nonZeros());
    384     VERIFY_IS_APPROX(m2, refM2);
    385   }
    386 
    387   // test setFromTriplets
    388   {
    389     typedef Triplet<Scalar,StorageIndex> TripletType;
    390     std::vector<TripletType> triplets;
    391     Index ntriplets = rows*cols;
    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);
    396 
    397     for(Index i=0;i<ntriplets;++i)
    398     {
    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;
    404       if(std::abs(refMat_prod(r,c))==0)
    405         refMat_prod(r,c) = v;
    406       else
    407         refMat_prod(r,c) *= v;
    408       refMat_last(r,c) = v;
    409     }
    410     SparseMatrixType m(rows,cols);
    411     m.setFromTriplets(triplets.begin(), triplets.end());
    412     VERIFY_IS_APPROX(m, refMat_sum);
    413 
    414     m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>());
    415     VERIFY_IS_APPROX(m, refMat_prod);
    416 #if (EIGEN_COMP_CXXVER >= 11)
    417     m.setFromTriplets(triplets.begin(), triplets.end(), [] (Scalar,Scalar b) { return b; });
    418     VERIFY_IS_APPROX(m, refMat_last);
    419 #endif
    420   }
    421   
    422   // test Map
    423   {
    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);
    428     {
    429       Map<SparseMatrixType> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr());
    430       Map<SparseMatrixType> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr());
    431       VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
    432       VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
    433     }
    434     {
    435       MappedSparseMatrix<Scalar,SparseMatrixType::Options,StorageIndex> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr());
    436       MappedSparseMatrix<Scalar,SparseMatrixType::Options,StorageIndex> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr());
    437       VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
    438       VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3);
    439     }
    440 
    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>())
    445       m2.makeCompressed();
    446     Map<SparseMatrixType> mapMat2(rows, cols, m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(),  m2.innerNonZeroPtr());
    447     VERIFY_IS_EQUAL(m2.coeff(i,j),Scalar(123));
    448     VERIFY_IS_EQUAL(mapMat2.coeff(i,j),Scalar(123));
    449     mapMat2.coeffRef(i,j) = -123;
    450     VERIFY_IS_EQUAL(m2.coeff(i,j),Scalar(-123));
    451   }
    452 
    453   // test triangularView
    454   {
    455     DenseMatrix refMat2(rows, cols), refMat3(rows, cols);
    456     SparseMatrixType m2(rows, cols), m3(rows, cols);
    457     initSparse<Scalar>(density, refMat2, m2);
    458     refMat3 = refMat2.template triangularView<Lower>();
    459     m3 = m2.template triangularView<Lower>();
    460     VERIFY_IS_APPROX(m3, refMat3);
    461 
    462     refMat3 = refMat2.template triangularView<Upper>();
    463     m3 = m2.template triangularView<Upper>();
    464     VERIFY_IS_APPROX(m3, refMat3);
    465 
    466     {
    467       refMat3 = refMat2.template triangularView<UnitUpper>();
    468       m3 = m2.template triangularView<UnitUpper>();
    469       VERIFY_IS_APPROX(m3, refMat3);
    470 
    471       refMat3 = refMat2.template triangularView<UnitLower>();
    472       m3 = m2.template triangularView<UnitLower>();
    473       VERIFY_IS_APPROX(m3, refMat3);
    474     }
    475 
    476     refMat3 = refMat2.template triangularView<StrictlyUpper>();
    477     m3 = m2.template triangularView<StrictlyUpper>();
    478     VERIFY_IS_APPROX(m3, refMat3);
    479 
    480     refMat3 = refMat2.template triangularView<StrictlyLower>();
    481     m3 = m2.template triangularView<StrictlyLower>();
    482     VERIFY_IS_APPROX(m3, refMat3);
    483 
    484     // check sparse-triangular to dense
    485     refMat3 = m2.template triangularView<StrictlyUpper>();
    486     VERIFY_IS_APPROX(refMat3, DenseMatrix(refMat2.template triangularView<StrictlyUpper>()));
    487   }
    488   
    489   // test selfadjointView
    490   if(!SparseMatrixType::IsRowMajor)
    491   {
    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>();
    497     VERIFY_IS_APPROX(m3, refMat3);
    498 
    499     refMat3 += refMat2.template selfadjointView<Lower>();
    500     m3 += m2.template selfadjointView<Lower>();
    501     VERIFY_IS_APPROX(m3, refMat3);
    502 
    503     refMat3 -= refMat2.template selfadjointView<Lower>();
    504     m3 -= m2.template selfadjointView<Lower>();
    505     VERIFY_IS_APPROX(m3, refMat3);
    506 
    507     // selfadjointView only works for square matrices:
    508     SparseMatrixType m4(rows, rows+1);
    509     VERIFY_RAISES_ASSERT(m4.template selfadjointView<Lower>());
    510     VERIFY_RAISES_ASSERT(m4.template selfadjointView<Upper>());
    511   }
    512   
    513   // test sparseView
    514   {
    515     DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
    516     SparseMatrixType m2(rows, rows);
    517     initSparse<Scalar>(density, refMat2, m2);
    518     VERIFY_IS_APPROX(m2.eval(), refMat2.sparseView().eval());
    519 
    520     // sparse view on expressions:
    521     VERIFY_IS_APPROX((s1*m2).eval(), (s1*refMat2).sparseView().eval());
    522     VERIFY_IS_APPROX((m2+m2).eval(), (refMat2+refMat2).sparseView().eval());
    523     VERIFY_IS_APPROX((m2*m2).eval(), (refMat2.lazyProduct(refMat2)).sparseView().eval());
    524     VERIFY_IS_APPROX((m2*m2).eval(), (refMat2*refMat2).sparseView().eval());
    525   }
    526 
    527   // test diagonal
    528   {
    529     DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols);
    530     SparseMatrixType m2(rows, cols);
    531     initSparse<Scalar>(density, refMat2, m2);
    532     VERIFY_IS_APPROX(m2.diagonal(), refMat2.diagonal().eval());
    533     DenseVector d = m2.diagonal();
    534     VERIFY_IS_APPROX(d, refMat2.diagonal().eval());
    535     d = m2.diagonal().array();
    536     VERIFY_IS_APPROX(d, refMat2.diagonal().eval());
    537     VERIFY_IS_APPROX(const_cast<const SparseMatrixType&>(m2).diagonal(), refMat2.diagonal().eval());
    538     
    539     initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag);
    540     m2.diagonal()      += refMat2.diagonal();
    541     refMat2.diagonal() += refMat2.diagonal();
    542     VERIFY_IS_APPROX(m2, refMat2);
    543   }
    544   
    545   // test diagonal to sparse
    546   {
    547     DenseVector d = DenseVector::Random(rows);
    548     DenseMatrix refMat2 = d.asDiagonal();
    549     SparseMatrixType m2;
    550     m2 = d.asDiagonal();
    551     VERIFY_IS_APPROX(m2, refMat2);
    552     SparseMatrixType m3(d.asDiagonal());
    553     VERIFY_IS_APPROX(m3, refMat2);
    554     refMat2 += d.asDiagonal();
    555     m2 += d.asDiagonal();
    556     VERIFY_IS_APPROX(m2, refMat2);
    557     m2.setZero();       m2 += d.asDiagonal();
    558     refMat2.setZero();  refMat2 += d.asDiagonal();
    559     VERIFY_IS_APPROX(m2, refMat2);
    560     m2.setZero();       m2 -= d.asDiagonal();
    561     refMat2.setZero();  refMat2 -= d.asDiagonal();
    562     VERIFY_IS_APPROX(m2, refMat2);
    563 
    564     initSparse<Scalar>(density, refMat2, m2);
    565     m2.makeCompressed();
    566     m2 += d.asDiagonal();
    567     refMat2 += d.asDiagonal();
    568     VERIFY_IS_APPROX(m2, refMat2);
    569 
    570     initSparse<Scalar>(density, refMat2, m2);
    571     m2.makeCompressed();
    572     VectorXi res(rows);
    573     for(Index i=0; i<rows; ++i)
    574       res(i) = internal::random<int>(0,3);
    575     m2.reserve(res);
    576     m2 -= d.asDiagonal();
    577     refMat2 -= d.asDiagonal();
    578     VERIFY_IS_APPROX(m2, refMat2);
    579   }
    580   
    581   // test conservative resize
    582   {
    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));
    593 
    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);
    600 
    601         SparseMatrixType m2 = m1;
    602         m2.makeCompressed();
    603 
    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();
    609 
    610         VERIFY_IS_APPROX(m1, refMat1);
    611         VERIFY_IS_APPROX(m2, refMat1);
    612 
    613         // Insert new values
    614         if (incRows > 0) 
    615           m1.insert(m1.rows()-1, 0) = refMat1(refMat1.rows()-1, 0) = 1;
    616         if (incCols > 0) 
    617           m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1;
    618 
    619         VERIFY_IS_APPROX(m1, refMat1);
    620 
    621 
    622       }
    623   }
    624 
    625   // test Identity matrix
    626   {
    627     DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows);
    628     SparseMatrixType m1(rows, rows);
    629     m1.setIdentity();
    630     VERIFY_IS_APPROX(m1, refMat1);
    631     for(int k=0; k<rows*rows/4; ++k)
    632     {
    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;
    638       VERIFY_IS_APPROX(m1, refMat1);
    639       if(internal::random<Index>(0,10)<2)
    640         m1.makeCompressed();
    641     }
    642     m1.setIdentity();
    643     refMat1.setIdentity();
    644     VERIFY_IS_APPROX(m1, refMat1);
    645   }
    646 
    647   // test array/vector of InnerIterator
    648   {
    649     typedef typename SparseMatrixType::InnerIterator IteratorType;
    650 
    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])
    660     {
    661       ++(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() );
    666       VERIFY( static_array[1].value() == static_array[0].value() );
    667     }
    668 
    669     std::vector<IteratorType> iters(2);
    670     iters[0] = IteratorType(m2,0);
    671     iters[1] = IteratorType(m2,m2.outerSize()-1);
    672   }
    673 
    674   // test reserve with empty rows/columns
    675   {
    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));
    680   }
    681 }
    682 
    683 
    684 template<typename SparseMatrixType>
    685 void big_sparse_triplet(Index rows, Index cols, double density) {
    686   typedef typename SparseMatrixType::StorageIndex StorageIndex;
    687   typedef typename SparseMatrixType::Scalar Scalar;
    688   typedef Triplet<Scalar,Index> TripletType;
    689   std::vector<TripletType> triplets;
    690   double nelements = density * rows*cols;
    691   VERIFY(nelements>=0 && nelements < static_cast<double>(NumTraits<StorageIndex>::highest()));
    692   Index ntriplets = Index(nelements);
    693   triplets.reserve(ntriplets);
    694   Scalar sum = Scalar(0);
    695   for(Index i=0;i<ntriplets;++i)
    696   {
    697     Index r = internal::random<Index>(0,rows-1);
    698     Index c = internal::random<Index>(0,cols-1);
    699     // use positive values to prevent numerical cancellation errors in sum
    700     Scalar v = numext::abs(internal::random<Scalar>());
    701     triplets.push_back(TripletType(r,c,v));
    702     sum += v;
    703   }
    704   SparseMatrixType m(rows,cols);
    705   m.setFromTriplets(triplets.begin(), triplets.end());
    706   VERIFY(m.nonZeros() <= ntriplets);
    707   VERIFY_IS_APPROX(sum, m.sum());
    708 }
    709 
    710 template<int>
    711 void bug1105()
    712 {
    713   // Regression test for bug 1105
    714   int n = Eigen::internal::random<int>(200,600);
    715   SparseMatrix<std::complex<double>,0, long> mat(n, n);
    716   std::complex<double> val;
    717 
    718   for(int i=0; i<n; ++i)
    719   {
    720     mat.coeffRef(i, i%(n/10)) = val;
    721     VERIFY(mat.data().allocatedSize()<20*n);
    722   }
    723 }
    724 
    725 #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA
    726 
    727 EIGEN_DECLARE_TEST(sparse_basic)
    728 {
    729   g_dense_op_sparse_count = 0;  // Suppresses compiler warning.
    730   for(int i = 0; i < g_repeat; i++) {
    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) {
    733       r = c; // check square matrices in 25% of tries
    734     }
    735     EIGEN_UNUSED_VARIABLE(r+c);
    736     CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(1, 1)) ));
    737     CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(8, 8)) ));
    738     CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, ColMajor>(r, c)) ));
    739     CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, RowMajor>(r, c)) ));
    740     CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(r, c)) ));
    741     CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,ColMajor,long int>(r, c)) ));
    742     CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,RowMajor,long int>(r, c)) ));
    743     
    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) {
    747       r = c; // check square matrices in 25% of tries
    748     }
    749     
    750     CALL_SUBTEST_6(( sparse_basic(SparseMatrix<double,ColMajor,short int>(short(r), short(c))) ));
    751     CALL_SUBTEST_6(( sparse_basic(SparseMatrix<double,RowMajor,short int>(short(r), short(c))) ));
    752   }
    753 
    754   // Regression test for bug 900: (manually insert higher values here, if you have enough RAM):
    755   CALL_SUBTEST_3((big_sparse_triplet<SparseMatrix<float, RowMajor, int> >(10000, 10000, 0.125)));
    756   CALL_SUBTEST_4((big_sparse_triplet<SparseMatrix<double, ColMajor, long int> >(10000, 10000, 0.125)));
    757 
    758   CALL_SUBTEST_7( bug1105<0>() );
    759 }
    760 #endif