MCPcopy Create free account
hub / github.com/PX4/eigen / initSparse

Function initSparse

test/sparse.h:56–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54 * and zero coefficients respectively.
55 */
56template<typename Scalar,int Opt1,int Opt2,typename StorageIndex> void
57initSparse(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
111template<typename Scalar,int Opt1,int Opt2,typename Index> void
112initSparse(double density,

Callers 3

sparse_productFunction · 0.85
initSPDFunction · 0.85
test_kronecker_productFunction · 0.85

Calls 12

swapFunction · 0.70
realFunction · 0.70
setZeroMethod · 0.45
reserveMethod · 0.45
rowsMethod · 0.45
colsMethod · 0.45
outerSizeMethod · 0.45
innerSizeMethod · 0.45
push_backMethod · 0.45
startVecMethod · 0.45
finalizeMethod · 0.45
sizeMethod · 0.45

Tested by 1

test_kronecker_productFunction · 0.68