| 61 | */ |
| 62 | template <typename MatrixType, typename IndexVector> |
| 63 | int coletree(const MatrixType& mat, IndexVector& parent, IndexVector& firstRowElt, typename MatrixType::StorageIndex *perm=0) |
| 64 | { |
| 65 | typedef typename MatrixType::StorageIndex StorageIndex; |
| 66 | StorageIndex nc = convert_index<StorageIndex>(mat.cols()); // Number of columns |
| 67 | StorageIndex m = convert_index<StorageIndex>(mat.rows()); |
| 68 | StorageIndex diagSize = (std::min)(nc,m); |
| 69 | IndexVector root(nc); // root of subtree of etree |
| 70 | root.setZero(); |
| 71 | IndexVector pp(nc); // disjoint sets |
| 72 | pp.setZero(); // Initialize disjoint sets |
| 73 | parent.resize(mat.cols()); |
| 74 | //Compute first nonzero column in each row |
| 75 | firstRowElt.resize(m); |
| 76 | firstRowElt.setConstant(nc); |
| 77 | firstRowElt.segment(0, diagSize).setLinSpaced(diagSize, 0, diagSize-1); |
| 78 | bool found_diag; |
| 79 | for (StorageIndex col = 0; col < nc; col++) |
| 80 | { |
| 81 | StorageIndex pcol = col; |
| 82 | if(perm) pcol = perm[col]; |
| 83 | for (typename MatrixType::InnerIterator it(mat, pcol); it; ++it) |
| 84 | { |
| 85 | Index row = it.row(); |
| 86 | firstRowElt(row) = (std::min)(firstRowElt(row), col); |
| 87 | } |
| 88 | } |
| 89 | /* Compute etree by Liu's algorithm for symmetric matrices, |
| 90 | except use (firstRowElt[r],c) in place of an edge (r,c) of A. |
| 91 | Thus each row clique in A'*A is replaced by a star |
| 92 | centered at its first vertex, which has the same fill. */ |
| 93 | StorageIndex rset, cset, rroot; |
| 94 | for (StorageIndex col = 0; col < nc; col++) |
| 95 | { |
| 96 | found_diag = col>=m; |
| 97 | pp(col) = col; |
| 98 | cset = col; |
| 99 | root(cset) = col; |
| 100 | parent(col) = nc; |
| 101 | /* The diagonal element is treated here even if it does not exist in the matrix |
| 102 | * hence the loop is executed once more */ |
| 103 | StorageIndex pcol = col; |
| 104 | if(perm) pcol = perm[col]; |
| 105 | for (typename MatrixType::InnerIterator it(mat, pcol); it||!found_diag; ++it) |
| 106 | { // A sequence of interleaved find and union is performed |
| 107 | Index i = col; |
| 108 | if(it) i = it.index(); |
| 109 | if (i == col) found_diag = true; |
| 110 | |
| 111 | StorageIndex row = firstRowElt(i); |
| 112 | if (row >= col) continue; |
| 113 | rset = internal::etree_find(row, pp); // Find the name of the set containing row |
| 114 | rroot = root(rset); |
| 115 | if (rroot != col) |
| 116 | { |
| 117 | parent(rroot) = col; |
| 118 | pp(cset) = rset; |
| 119 | cset = rset; |
| 120 | root(cset) = col; |