29 #include "../Core/util/NonMPL2.h"
31 #ifndef EIGEN_SPARSE_AMD_H
32 #define EIGEN_SPARSE_AMD_H
38 template<
typename T>
inline T amd_flip(
const T& i) {
return -i-2; }
39 template<
typename T>
inline T amd_unflip(
const T& i) {
return i<0 ? amd_flip(i) : i; }
40 template<
typename T0,
typename T1>
inline bool amd_marked(
const T0* w,
const T1& j) {
return w[j]<0; }
41 template<
typename T0,
typename T1>
inline void amd_mark(
const T0* w,
const T1& j) {
return w[j] = amd_flip(w[j]); }
44 template<
typename Index>
45 static int cs_wclear (Index mark, Index lemax, Index *w, Index n)
48 if(mark < 2 || (mark + lemax < 0))
50 for(k = 0; k < n; k++)
59 template<
typename Index>
60 Index cs_tdfs(Index j, Index k, Index *head,
const Index *next, Index *post, Index *stack)
63 if(!head || !next || !post || !stack)
return (-1);
90 template<
typename Scalar,
typename Index>
91 void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, PermutationMatrix<Dynamic,Dynamic,Index>& perm)
95 int d, dk, dext, lemax = 0, e, elenk, eln, i, j, k, k1,
96 k2, k3, jlast, ln, dense, nzmax, mindeg = 0, nvi, nvj, nvk, mark, wnvi,
97 ok, nel = 0, p, p1, p2, p3, p4, pj, pk, pk1, pk2, pn, q, t;
101 dense = std::max<Index> (16, Index(10 * sqrt(
double(n))));
102 dense = std::min<Index> (n-2, dense);
104 Index cnz = C.nonZeros();
106 t = cnz + cnz/5 + 2*n;
109 Index* W =
new Index[8*(n+1)];
111 Index* nv = W + (n+1);
112 Index* next = W + 2*(n+1);
113 Index* head = W + 3*(n+1);
114 Index* elen = W + 4*(n+1);
115 Index* degree = W + 5*(n+1);
116 Index* w = W + 6*(n+1);
117 Index* hhead = W + 7*(n+1);
118 Index* last = perm.indices().data();
121 Index* Cp = C.outerIndexPtr();
122 Index* Ci = C.innerIndexPtr();
123 for(k = 0; k < n; k++)
124 len[k] = Cp[k+1] - Cp[k];
128 for(i = 0; i <= n; i++)
139 mark = internal::cs_wclear<Index>(0, 0, w, n);
145 for(i = 0; i < n; i++)
147 bool has_diag =
false;
148 for(p = Cp[i]; p<Cp[i+1]; ++p)
163 else if(d > dense || !has_diag)
168 Cp[i] = amd_flip (n);
173 if(head[d] != -1) last[head[d]] = i;
186 for(k = -1; mindeg < n && (k = head[mindeg]) == -1; mindeg++) {}
187 if(next[k] != -1) last[next[k]] = -1;
188 head[mindeg] = next[k];
194 if(elenk > 0 && cnz + mindeg >= nzmax)
196 for(j = 0; j < n; j++)
201 Ci[p] = amd_flip (j);
204 for(q = 0, p = 0; p < cnz; )
206 if((j = amd_flip (Ci[p++])) >= 0)
210 for(k3 = 0; k3 < len[j]-1; k3++) Ci[q++] = Ci[p++];
220 pk1 = (elenk == 0) ? p : cnz;
222 for(k1 = 1; k1 <= elenk + 1; k1++)
236 for(k2 = 1; k2 <= ln; k2++)
239 if((nvi = nv[i]) <= 0)
continue;
243 if(next[i] != -1) last[next[i]] = last[i];
246 next[last[i]] = next[i];
250 head[degree[i]] = next[i];
255 Cp[e] = amd_flip (k);
259 if(elenk != 0) cnz = pk2;
266 mark = internal::cs_wclear<Index>(mark, lemax, w, n);
267 for(pk = pk1; pk < pk2; pk++)
270 if((eln = elen[i]) <= 0)
continue;
273 for(p = Cp[i]; p <= Cp[i] + eln - 1; p++)
282 w[e] = degree[e] + wnvi;
288 for(pk = pk1; pk < pk2; pk++)
292 p2 = p1 + elen[i] - 1;
294 for(h = 0, d = 0, p = p1; p <= p2; p++)
308 Cp[e] = amd_flip (k);
313 elen[i] = pn - p1 + 1;
316 for(p = p2 + 1; p < p4; p++)
319 if((nvj = nv[j]) <= 0)
continue;
326 Cp[i] = amd_flip (k);
336 degree[i] = std::min<Index> (degree[i], d);
340 len[i] = pn - p1 + 1;
348 lemax = std::max<Index>(lemax, dk);
349 mark = internal::cs_wclear<Index>(mark+lemax, lemax, w, n);
352 for(pk = pk1; pk < pk2; pk++)
355 if(nv[i] >= 0)
continue;
359 for(; i != -1 && next[i] != -1; i = next[i], mark++)
363 for(p = Cp[i]+1; p <= Cp[i] + ln-1; p++) w[Ci[p]] = mark;
365 for(j = next[i]; j != -1; )
367 ok = (len[j] == ln) && (elen[j] == eln);
368 for(p = Cp[j] + 1; ok && p <= Cp[j] + ln - 1; p++)
370 if(w[Ci[p]] != mark) ok = 0;
374 Cp[j] = amd_flip (i);
391 for(p = pk1, pk = pk1; pk < pk2; pk++)
394 if((nvi = -nv[i]) <= 0)
continue;
396 d = degree[i] + dk - nvi;
397 d = std::min<Index> (d, n - nel - nvi);
398 if(head[d] != -1) last[head[d]] = i;
402 mindeg = std::min<Index> (mindeg, d);
407 if((len[k] = p-pk1) == 0)
412 if(elenk != 0) cnz = p;
416 for(i = 0; i < n; i++) Cp[i] = amd_flip (Cp[i]);
417 for(j = 0; j <= n; j++) head[j] = -1;
418 for(j = n; j >= 0; j--)
420 if(nv[j] > 0)
continue;
421 next[j] = head[Cp[j]];
424 for(e = n; e >= 0; e--)
426 if(nv[e] <= 0)
continue;
429 next[e] = head[Cp[e]];
433 for(k = 0, i = 0; i <= n; i++)
435 if(Cp[i] == -1) k = internal::cs_tdfs<Index>(i, k, head, next, perm.indices().data(), w);
438 perm.indices().conservativeResize(n);
447 #endif // EIGEN_SPARSE_AMD_H
Definition: Eigen_Colamd.h:54