| 54 | * and zero coefficients respectively. |
| 55 | */ |
| 56 | template<typename Scalar,int Opt1,int Opt2,typename StorageIndex> void |
| 57 | initSparse(double density, |
| 58 | Matrix<Scalar,Dynamic,Dynamic,Opt1>& refMat, |
| 59 | SparseMatrix<Scalar,Opt2,StorageIndex>& sparseMat, |
| 60 | int flags = 0, |
| 61 | std::vector<Matrix<StorageIndex,2,1> >* zeroCoords = 0, |
| 62 | std::vector<Matrix<StorageIndex,2,1> >* nonzeroCoords = 0) |
| 63 | { |
| 64 | enum { IsRowMajor = SparseMatrix<Scalar,Opt2,StorageIndex>::IsRowMajor }; |
| 65 | sparseMat.setZero(); |
| 66 | //sparseMat.reserve(int(refMat.rows()*refMat.cols()*density)); |
| 67 | sparseMat.reserve(VectorXi::Constant(IsRowMajor ? refMat.rows() : refMat.cols(), int((1.5*density)*(IsRowMajor?refMat.cols():refMat.rows())))); |
| 68 | |
| 69 | for(Index j=0; j<sparseMat.outerSize(); j++) |
| 70 | { |
| 71 | //sparseMat.startVec(j); |
| 72 | for(Index i=0; i<sparseMat.innerSize(); i++) |
| 73 | { |
| 74 | Index ai(i), aj(j); |
| 75 | if(IsRowMajor) |
| 76 | std::swap(ai,aj); |
| 77 | Scalar v = (internal::random<double>(0,1) < density) ? internal::random<Scalar>() : Scalar(0); |
| 78 | if ((flags&ForceNonZeroDiag) && (i==j)) |
| 79 | { |
| 80 | // FIXME: the following is too conservative |
| 81 | v = internal::random<Scalar>()*Scalar(3.); |
| 82 | v = v*v; |
| 83 | if(numext::real(v)>0) v += Scalar(5); |
| 84 | else v -= Scalar(5); |
| 85 | } |
| 86 | if ((flags & MakeLowerTriangular) && aj>ai) |
| 87 | v = Scalar(0); |
| 88 | else if ((flags & MakeUpperTriangular) && aj<ai) |
| 89 | v = Scalar(0); |
| 90 | |
| 91 | if ((flags&ForceRealDiag) && (i==j)) |
| 92 | v = numext::real(v); |
| 93 | |
| 94 | if (v!=Scalar(0)) |
| 95 | { |
| 96 | //sparseMat.insertBackByOuterInner(j,i) = v; |
| 97 | sparseMat.insertByOuterInner(j,i) = v; |
| 98 | if (nonzeroCoords) |
| 99 | nonzeroCoords->push_back(Matrix<StorageIndex,2,1> (ai,aj)); |
| 100 | } |
| 101 | else if (zeroCoords) |
| 102 | { |
| 103 | zeroCoords->push_back(Matrix<StorageIndex,2,1> (ai,aj)); |
| 104 | } |
| 105 | refMat(ai,aj) = v; |
| 106 | } |
| 107 | } |
| 108 | //sparseMat.finalize(); |
| 109 | } |
| 110 | |
| 111 | template<typename Scalar,int Opt1,int Opt2,typename Index> void |
| 112 | initSparse(double density, |