21 #define USE_ADJOINT_CODE
24 static void xxx(
int i){;}
30 for (
int i=mmin;i<=mmax;i++)
35 cout <<
"not used" <<
endl;
54 hs_smatrix
cs_multiply(
const hs_smatrix &A,
const hs_smatrix &B);
63 for (i = 0 ; i < n ; i++)
78 if (mark < 2 || (mark + lemax < 0))
80 for (k = 0 ; k < n ; k++)
if (w [k] != 0) w [k] = 1 ;
105 for (p = 0 ; p < Ap [n] ; p++)
108 for (j = 0 ; j < n ; j++)
110 for (p = Ap [j] ; p < Ap [j+1] ; p++)
112 Ci [q = w [Ai [p]]++] = j ;
137 for (p = 0 ; p < Ap [n] ; p++)
140 for (j = 0 ; j < n ; j++)
142 for (p = Ap [j] ; p < Ap [j+1] ; p++)
144 Ci [q = w [Ai [p]]++] = j ;
158 for (
int i=1;i<=nz;i++)
160 tmp(
M(1,i),
M(2,i))=
M(i);
161 if (
M(1,i) !=
M(2,i))
163 tmp(
M(2,i),
M(1,i))=
M(i);
176 for (
int i=1;i<=nz;i++)
178 tmp(
M(1,i),
M(2,i))=
M(i);
179 if (
M(1,i) !=
M(2,i))
181 tmp(
M(2,i),
M(1,i))=
M(i);
194 for (
int i=1;i<=nz;i++)
196 tmp(
M(1,i),
M(2,i))=
M(i);
209 for (
int i=1;i<=nz;i++)
211 tmp(
M(1,i),
M(2,i))=
M(i);
223 for (
int i=1;i<=nz;i++)
225 tmp(
M(1,i),
M(2,i))=
M(i);
226 if (
M(1,i) !=
M(2,i))
228 tmp(
M(2,i),
M(1,i))=
M(i);
238 for (
int i=1;i<=nz;i++)
240 tmp(
M(1,i),
M(2,i))=
M(i);
241 if (
M(1,i) !=
M(2,i))
243 tmp(
M(2,i),
M(1,i))=
M(i);
257 int nz =
M.indexmax()-
M.indexmin() + 1;
278 if(
min(Ti)<0 ||
max(Ti)>(n-1) ||
max(Tj)>(n-1) ||
min(Tj)<0)
279 cout << "Error
#2 in hs_smatrix::hs_smatrix" << endl;
282 for (k = 0 ; k < nz ; k++)
283 lower_tri += Ti[k]>Tj[k];
285 cout <<
"hs_smatrix::hs_smatrix: M must be upper triangular" <<
endl;
290 i.allocate(0,nzmax-1);
291 x.allocate(0,nzmax-1);
301 for (k = 0 ; k < nz ; k++)
w [Tj [k]]++ ;
303 for (k = 0 ; k < nz ; k++)
318 int nz =
M.indexmax()-
M.indexmin() + 1;
339 if(
min(Ti)<0 ||
max(Ti)>(n-1) ||
max(Tj)>(m-1) ||
min(Tj)<0)
340 cout << "Error
#2 in hs_smatrix::hs_smatrix" << endl;
343 for (k = 0 ; k < nz ; k++)
344 lower_tri += Ti[k]>Tj[k];
346 cout <<
"hs_smatrix::hs_smatrix: M must be upper triangular" <<
endl;
351 i.allocate(0,nzmax-1);
352 x.allocate(0,nzmax-1);
362 for (k = 0 ; k < nz ; k++)
w [Tj [k]]++ ;
364 for (k = 0 ; k < nz ; k++)
378 int nz =
M.indexmax()-
M.indexmin() + 1;
399 if(
min(Ti)<0 ||
max(Ti)>(n-1) ||
max(Tj)>(n-1) ||
min(Tj)<0)
400 cout << "Error
#2 in dvar_hs_smatrix::dvar_hs_smatrix" << endl;
403 for (k = 0 ; k < nz ; k++)
404 lower_tri += Ti[k]>Tj[k];
406 cout <<
"dvar_hs_smatrix::dvar_hs_smatrix: M must be upper triangular"
412 i.allocate(0,nzmax-1);
413 x.allocate(0,nzmax-1);
423 for (k = 0 ; k < nz ; k++)
w [Tj [k]]++ ;
425 for (k = 0 ; k < nz ; k++)
433 hs_smatrix::hs_smatrix(
int _n,
int _nzmax)
441 i.allocate(0,nzmax-1);
443 x.allocate(0,nzmax-1);
447 dvar_hs_smatrix::dvar_hs_smatrix(
int _n,
int _nzmax)
455 i.allocate(0,nzmax-1);
457 x.allocate(0,nzmax-1);
462 hs_smatrix::hs_smatrix(
XCONST cs *A)
470 for (j = 0 ; j <= n ; j++)
473 i.allocate(0,nzmax-1);
474 for (j = 0 ; j < nzmax ; j++)
477 x.allocate(0,nzmax-1);
478 for (j = 0 ; j < nzmax ; j++)
483 void hs_smatrix::reallocate(
int _nzmax)
488 i.allocate(0,_nzmax-1);
489 i(nzmax,_nzmax-1) = 0;
495 x.allocate(0,_nzmax-1);
496 x(nzmax,_nzmax-1) = 0.0;
502 void dvar_hs_smatrix::reallocate(
int _nzmax)
507 i.allocate(0,_nzmax-1);
508 i(nzmax,_nzmax-1) = 0;
514 x.allocate(0,_nzmax-1);
515 x(nzmax,_nzmax-1) = 0.0;
521 dvar_hs_smatrix::dvar_hs_smatrix(
XCONST dvar_hs_smatrix &A)
528 i.allocate(0,nzmax-1);
529 x.allocate(0,nzmax-1);
535 hs_smatrix::hs_smatrix(
XCONST hs_smatrix &A)
542 i.allocate(0,nzmax-1);
543 x.allocate(0,nzmax-1);
549 hs_smatrix& hs_smatrix::operator=(
XCONST hs_smatrix &A)
551 if((n != A.n)||(nzmax != A.nzmax))
552 cout <<
"hs_smatrix&: lhs and rhs dimensions differ" <<
endl;
563 dvar_hs_smatrix& dvar_hs_smatrix::operator=(
XCONST dvar_hs_smatrix &A)
565 if((n != A.n)||(nzmax != A.nzmax))
566 cout <<
"dvar_hs_smatrix&: lhs and rhs dimensions differ" <<
endl;
585 i.allocate(0,nzmax-1);
586 x.allocate(0,nzmax-1);
596 i.allocate(0,nzmax-1);
597 x.allocate(0,nzmax-1);
601 double hs_smatrix::trace_log(
int &
sgn)
605 for (
int j = 0 ; j < n ; j++)
607 for (
int k = p [j] ; k < p [j+1] ; k++)
621 cerr <<
"!!!! trace_log: zero diagonal element " <<
endl;
634 dvariable dvar_hs_smatrix::trace_log(
int & sgn)
638 for (
int j = 0 ; j < n ; j++)
640 for (
int k = p [j] ; k < p [j+1] ; k++)
654 cerr <<
"!!!! trace_log: zero diagonal element " <<
endl;
676 return M.trace_log(sgn);
683 return M.trace_log(sgn);
688 return M.trace_log(sgn);
691 int hs_smatrix::print()
693 cout <<
"Sparse matrix in compressed form (i,x):" <<
endl;
694 for (
int j = 0 ; j < n ; j++)
696 cout <<
" col " << j <<
" : locations " << p[j] <<
" to " << p[j+1]-1
698 for (
int P = p [j] ; P < p [j+1] ; P++)
699 cout << i [P] <<
" " <<
x [P] << endl;
704 int hs_smatrix::print_pattern()
706 cout <<
"Sparseness pattern:" <<
endl;
707 for (
int j = 0 ; j < n ; j++)
709 for (
int k = 0 ; k < n ; k++)
712 for (
int kk = p[k] ; kk < p[k+1] ; kk++)
725 int hs_smatrix::print_trans_zeros()
727 for(
int k = 0 ; k < n ; k++)
730 for(
int j = 0 ; j < n ; j++)
733 cout <<
x[kk] <<
"\t";
748 int i, p, n, len, top;
757 for (p = Ap [k] ; p < Ap [k+1] ; p++)
760 if (i > k) continue ;
761 for (len = 0 ; !CS_MARKED (w,i) ; i = parent [i])
766 while (len > 0) s [--top] = s [--len] ;
768 for (p = top ; p < n ; p++) CS_MARK (w, s [p]) ;
777 int i, p, n, len, top;
786 for (p = Ap [k] ; p < Ap [k+1] ; p++)
789 if (i > k) continue ;
790 for (len = 0 ; !CS_MARKED (w,i) ; i = parent [i])
795 while (len > 0) s [--top] = s [--len] ;
797 for (p = top ; p < n ; p++) CS_MARK (w, s [p]) ;
806 int i, j, p, q, i2, j2;
818 for (j = 0 ; j < n ; j++)
820 j2 = (pinv[0]!=-1) ? pinv [j] : j ;
821 for (p = Ap [j] ; p < Ap [j+1] ; p++)
824 if (i > j) continue ;
825 i2 = (pinv[0]!=-1) ? pinv [i] : i ;
826 w [CS_MAX (i2, j2)]++ ;
830 for (j = 0 ; j < n ; j++)
832 j2 = (pinv[0]!=-1) ? pinv [j] : j ;
833 for (p = Ap [j] ; p < Ap [j+1] ; p++)
836 if (i > j) continue ;
837 i2 = (pinv[0]!=-1) ? pinv [i] : i ;
839 q = w [CS_MAX (i2, j2)]++;
840 Ci [q] = CS_MIN (i2, j2) ;
849 int i, j, p, q, i2, j2;
861 for (j = 0 ; j < n ; j++)
863 j2 = (pinv[0]!=-1) ? pinv [j] : j ;
864 for (p = Ap [j] ; p < Ap [j+1] ; p++)
867 if (i > j) continue ;
868 i2 = (pinv[0]!=-1) ? pinv [i] : i ;
869 w [CS_MAX (i2, j2)]++ ;
873 for (j = 0 ; j < n ; j++)
875 j2 = (pinv[0]!=-1) ? pinv [j] : j ;
876 for (p = Ap [j] ; p < Ap [j+1] ; p++)
879 if (i > j) continue ;
880 i2 = (pinv[0]!=-1) ? pinv [i] : i ;
882 q = w [CS_MAX (i2, j2)]++;
883 Ci [q] = CS_MIN (i2, j2) ;
892 for (p = Lpi1 ; p < ci ; p++)
894 x[Li[p]] -= Lx[p]*lki ;
931 for (k = 0 ; k < n ; k++)
932 Lp [k] = c [k] = cp [k] ;
934 for (k = 0 ; k < n ; k++)
939 for (p = Cp [k] ; p < Cp [k+1] ; p++)
941 if (Ci [p] <= k) x [Ci [p]] = Cx [p] ;
946 for ( ; top < n ; top++)
949 lki = x [i] / Lx [Lp [i]] ;
955 for (p = Lpi1; p < ci ; p++)
957 x [Li [p]] -= Lx [p] * lki ;
966 if (d <= 0)
return (0) ;
1021 for (k = 0 ; k < n ; k++)
1023 Lp [k] = c [k] = cp [k] ;
1026 for (k = 0 ; k < n ; k++)
1031 x (k,xcount(k)) = 0 ;
1032 for (p = Cp [k] ; p < Cp [k+1] ; p++)
1037 x(Ci[p],xcount(Ci[p])) = Cx[p] ;
1040 d =
x(k,xcount[k]) ;
1043 x(k,xcount[k]) = 0 ;
1045 for ( ; top < n ; top++)
1049 lki =
x (i,xcount[i]) / Lx [Lp [i]] ;
1052 x (i,xcount[i]) = 0.0 ;
1053 for (p = Lp [i] + 1 ; p < c [i] ; p++)
1055 x [Li [p]] -= Lx [p] * lki ;
1064 cerr <<
"Error unhandled case in chol" <<
endl;
1070 cerr <<
"Error unhandled case in chol" <<
endl;
1075 if (d <= 0)
return (0) ;
1082 cerr <<
"Error unhandled case in chol" <<
endl;
1088 cerr <<
"Error unhandled case in chol" <<
endl;
1112 for (
int k = 0 ; k < n ; k++)
1127 for (
int k = 0 ; k < n ; k++)
1141 for (
int k = 0 ; k < n ; k++)
1151 hs_smatrix& L = (hs_smatrix&)LL;
1162 for (j = 0 ; j < n ; j++)
1164 x [j] /= Lx [Lp [j]] ;
1165 for (p = Lp [j]+1 ; p < Lp [j+1] ; p++)
1167 x [Li [p]] -= Lx [p] * x [j] ;
1176 dvar_hs_smatrix& L = (dvar_hs_smatrix&)LL;
1187 for (j = 0 ; j < n ; j++)
1189 x [j] /= Lx [Lp [j]] ;
1190 for (p = Lp [j]+1 ; p < Lp [j+1] ; p++)
1192 x [Li [p]] -= Lx [p] * x [j] ;
1201 dvar_hs_smatrix& L = (dvar_hs_smatrix&)LL;
1212 for (j = 0 ; j < n ; j++)
1214 x [j] /= Lx [Lp [j]] ;
1215 for (p = Lp [j]+1 ; p < Lp [j+1] ; p++)
1217 x [Li [p]] -= Lx [p] * x [j] ;
1227 hs_smatrix& L = (hs_smatrix&)LL;
1237 for (j = n-1 ; j >= 0 ; j--)
1239 for (p = Lp [j]+1 ; p < Lp [j+1] ; p++)
1241 x [j] -= Lx [p] * x [Li [p]] ;
1243 x [j] /= Lx [Lp [j]] ;
1251 dvar_hs_smatrix& L = (dvar_hs_smatrix&)LL;
1261 for (j = n-1 ; j >= 0 ; j--)
1263 for (p = Lp [j]+1 ; p < Lp [j+1] ; p++)
1265 x [j] -= Lx [p] * x [Li [p]] ;
1267 x [j] /= Lx [Lp [j]] ;
1273 int cs_diag(
int i,
int j,
double aij,
void *other) {
return (i != j) ; }
1275 {
return (i != j) ; }
1278 int cs_fkeep (hs_smatrix &A,
int (*fkeep) (
int,
int,
double,
void *),
1288 for (j = 0 ; j < n ; j++)
1292 for ( ; p < Ap [j+1] ; p++)
1294 if (fkeep (Ai [p], j, Ax [p], other))
1297 Ai [nz++] = Ai [p] ;
1306 void *),
void *other)
1315 for (j = 0 ; j < n ; j++)
1319 for ( ; p < Ap [j+1] ; p++)
1321 if (fkeep (Ai [p], j, Ax [p], other))
1324 Ai [nz++] = Ai [p] ;
1334 dvector &
x,
int mark, hs_smatrix &C,
int nz)
1337 hs_smatrix& A = (hs_smatrix&)AA;
1343 for (p = Ap [j] ; p < Ap [j+1] ; p++)
1350 x [i] = beta * Ax [p] ;
1352 else x [i] += beta * Ax [p] ;
1383 dvar_hs_smatrix& A = (dvar_hs_smatrix&)AA;
1389 for (p = Ap [j] ; p < Ap [j+1] ; p++)
1396 x [i] = beta * Ax [p] ;
1398 else x [i] += beta * Ax [p] ;
1409 hs_smatrix& A = (hs_smatrix&)AA;
1410 hs_smatrix& B = (hs_smatrix&)BB;
1411 int p, j, nz = 0, anz,m, n, bnz;
1413 if (A.m != B.m || A.n != B.n)
1415 cout <<
"!!!! Error in cs_add: A.m != B.m || A.n != B.n" <<
endl;
1419 m = A.m ; anz = A.p [A.n] ;
1431 hs_smatrix C(n,anz + bnz);
1436 for (j = 0 ; j < n ; j++)
1439 nz =
cs_scatter (A, j, alpha, w, x, j+1, C, nz) ;
1440 nz =
cs_scatter (B, j, beta, w, x, j+1, C, nz) ;
1441 for (p = Cp [j] ; p < nz ; p++)
1442 Cx [p] = x [Ci [p]] ;
1450 double alpha,
double beta)
1454 dvar_hs_smatrix& A = (dvar_hs_smatrix&)AA;
1455 dvar_hs_smatrix& B = (dvar_hs_smatrix&)BB;
1456 int p, j, nz = 0, anz,m, n, bnz;
1458 if (A.m != B.m || A.n != B.n)
1460 cout <<
"!!!! Error in cs_add: A.m != B.m || A.n != B.n" <<
endl;
1464 m = A.m ; anz = A.p [A.n] ;
1476 dvar_hs_smatrix C(n,anz + bnz);
1481 for (j = 0 ; j < n ; j++)
1484 nz =
cs_scatter (A, j, alpha, w, x, j+1, C, nz) ;
1485 nz =
cs_scatter (B, j, beta, w, x, j+1, C, nz) ;
1486 for (p = Cp [j] ; p < nz ; p++)
1487 Cx [p] = x [Ci [p]] ;
1511 head [p] = next [i] ;
1524 int d, dk, dext, lemax = 0, e, elenk, eln, i, j, k, k1,
1525 k2, k3, jlast, ln, dense, nzmax, mindeg = 0, nvi, nvj, nvk, mark, wnvi,
1526 ok, cnz, nel = 0, p, p1, p2, p3, p4, pj, pk, pk1, pk2, pn, q, t ;
1532 hs_smatrix AT(n,A.nzmax);
1536 dense = CS_MAX (16, (
int)(10.0 *
sqrt((
double)n)));
1537 dense = CS_MIN (n-2, dense) ;
1539 hs_smatrix C =
cs_add(A,AT);
1544 t = cnz + cnz/5 + 2*n ;
1561 for (k = 0 ; k < n ; k++) len [k] = Cp [k+1] - Cp [k] ;
1565 for (i = 0 ; i <= n ; i++)
1574 degree [i] = len [i] ;
1581 for (i = 0 ; i < n ; i++)
1596 Cp [i] = CS_FLIP (n) ;
1601 if (head [d] != -1) last [head [d]] = i ;
1602 next [i] = head [d] ;
1609 for (k = -1 ; mindeg < n && (k = head [mindeg]) == -1 ; mindeg++) ;
1610 if (next [k] != -1) last [next [k]] = -1 ;
1611 head [mindeg] = next [k] ;
1616 if (elenk > 0 && cnz + mindeg >= nzmax)
1618 for (j = 0 ; j < n ; j++)
1620 if ((p = Cp [j]) >= 0)
1623 Ci [p] = CS_FLIP (j) ;
1626 for (q = 0, p = 0 ; p < cnz ; )
1628 if ((j = CS_FLIP (Ci [p++])) >= 0)
1632 for (k3 = 0 ; k3 < len [j]-1 ; k3++) Ci [q++] = Ci [p++] ;
1641 pk1 = (elenk == 0) ? p : cnz ;
1643 for (k1 = 1 ; k1 <= elenk + 1 ; k1++)
1649 ln = len [k] - elenk ;
1657 for (k2 = 1 ; k2 <= ln ; k2++)
1660 if ((nvi = nv [i]) <= 0)
continue ;
1664 if (next [i] != -1) last [next [i]] = last [i] ;
1667 next [last [i]] = next [i] ;
1671 head [degree [i]] = next [i] ;
1676 Cp [e] = CS_FLIP (k) ;
1680 if (elenk != 0) cnz = pk2 ;
1683 len [k] = pk2 - pk1 ;
1687 for (pk = pk1 ; pk < pk2 ; pk++)
1690 if ((eln = elen [i]) <= 0) continue ;
1693 for (p = Cp [i] ; p <= Cp [i] + eln - 1 ; p++)
1700 else if (w [e] != 0)
1702 w [e] = degree [e] + wnvi ;
1707 for (pk = pk1 ; pk < pk2 ; pk++)
1711 p2 = p1 + elen [i] - 1 ;
1713 for (h = 0, d = 0, p = p1 ; p <= p2 ; p++)
1718 dext = w [e] - mark ;
1727 Cp [e] = CS_FLIP (k) ;
1732 elen [i] = pn - p1 + 1 ;
1735 for (p = p2 + 1 ; p < p4 ; p++)
1738 if ((nvj = nv [j]) <= 0) continue ;
1745 Cp [i] = CS_FLIP (k) ;
1755 degree [i] = CS_MIN (degree [i], d) ;
1759 len [i] = pn - p1 + 1 ;
1761 next [i] = hhead [h] ;
1767 lemax = CS_MAX (lemax, dk) ;
1768 mark =
cs_wclear (mark+lemax, lemax, w, n) ;
1770 for (pk = pk1 ; pk < pk2 ; pk++)
1773 if (nv [i] >= 0) continue ;
1777 for ( ; i != -1 && next [i] != -1 ; i = next [i], mark++)
1781 for (p = Cp [i]+1 ; p <= Cp [i] + ln-1 ; p++) w [Ci [p]] = mark;
1783 for (j = next [i] ; j != -1 ; )
1785 ok = (len [j] == ln) && (elen [j] == eln) ;
1786 for (p = Cp [j] + 1 ; ok && p <= Cp [j] + ln - 1 ; p++)
1788 if (w [Ci [p]] != mark) ok = 0 ;
1792 Cp [j] = CS_FLIP (i) ;
1808 for (p = pk1, pk = pk1 ; pk < pk2 ; pk++)
1811 if ((nvi = -nv [i]) <= 0) continue ;
1813 d = degree [i] + dk - nvi ;
1814 d = CS_MIN (d, n - nel - nvi) ;
1815 if (head [d] != -1) last [head [d]] = i ;
1816 next [i] = head [d] ;
1819 mindeg = CS_MIN (mindeg, d) ;
1824 if ((len [k] = p-pk1) == 0)
1829 if (elenk != 0) cnz = p ;
1832 for (i = 0 ; i < n ; i++) Cp [i] = CS_FLIP (Cp [i]) ;
1833 for (j = 0 ; j <= n ; j++) head [j] = -1 ;
1834 for (j = n ; j >= 0 ; j--)
1836 if (nv [j] > 0) continue ;
1837 next [j] = head [Cp [j]] ;
1840 for (e = n ; e >= 0 ; e--)
1842 if (nv [e] <= 0) continue ;
1845 next [e] = head [Cp [e]] ;
1849 for (k = 0, i = 0 ; i <= n ; i++)
1851 if (Cp [i] == -1) k =
cs_tdfs (i, k, head, next, P, w) ;
1861 int d, dk, dext, lemax = 0, e, elenk, eln, i, j, k, k1,
1862 k2, k3, jlast, ln, dense, nzmax, mindeg = 0, nvi, nvj, nvk, mark, wnvi,
1863 ok, cnz, nel = 0, p, p1, p2, p3, p4, pj, pk, pk1, pk2, pn, q, t ;
1869 dvar_hs_smatrix AT(n,A.nzmax);
1872 dense = CS_MAX (16, (
int)(10.0 *
sqrt((
double)n)));
1873 dense = CS_MIN (n-2, dense) ;
1875 dvar_hs_smatrix C =
cs_add(A,AT);
1880 t = cnz + cnz/5 + 2*n ;
1897 for (k = 0 ; k < n ; k++) len [k] = Cp [k+1] - Cp [k] ;
1901 for (i = 0 ; i <= n ; i++)
1910 degree [i] = len [i] ;
1917 for (i = 0 ; i < n ; i++)
1932 Cp [i] = CS_FLIP (n) ;
1937 if (head [d] != -1) last [head [d]] = i ;
1938 next [i] = head [d] ;
1945 for (k = -1 ; mindeg < n && (k = head [mindeg]) == -1 ; mindeg++) ;
1946 if (next [k] != -1) last [next [k]] = -1 ;
1947 head [mindeg] = next [k] ;
1952 if (elenk > 0 && cnz + mindeg >= nzmax)
1954 for (j = 0 ; j < n ; j++)
1956 if ((p = Cp [j]) >= 0)
1959 Ci [p] = CS_FLIP (j) ;
1962 for (q = 0, p = 0 ; p < cnz ; )
1964 if ((j = CS_FLIP (Ci [p++])) >= 0)
1968 for (k3 = 0 ; k3 < len [j]-1 ; k3++) Ci [q++] = Ci [p++] ;
1977 pk1 = (elenk == 0) ? p : cnz ;
1979 for (k1 = 1 ; k1 <= elenk + 1 ; k1++)
1985 ln = len [k] - elenk ;
1993 for (k2 = 1 ; k2 <= ln ; k2++)
1996 if ((nvi = nv [i]) <= 0)
continue ;
2000 if (next [i] != -1) last [next [i]] = last [i] ;
2003 next [last [i]] = next [i] ;
2007 head [degree [i]] = next [i] ;
2012 Cp [e] = CS_FLIP (k) ;
2016 if (elenk != 0) cnz = pk2 ;
2019 len [k] = pk2 - pk1 ;
2023 for (pk = pk1 ; pk < pk2 ; pk++)
2026 if ((eln = elen [i]) <= 0) continue ;
2029 for (p = Cp [i] ; p <= Cp [i] + eln - 1 ; p++)
2036 else if (w [e] != 0)
2038 w [e] = degree [e] + wnvi ;
2043 for (pk = pk1 ; pk < pk2 ; pk++)
2047 p2 = p1 + elen [i] - 1 ;
2049 for (h = 0, d = 0, p = p1 ; p <= p2 ; p++)
2054 dext = w [e] - mark ;
2063 Cp [e] = CS_FLIP (k) ;
2068 elen [i] = pn - p1 + 1 ;
2071 for (p = p2 + 1 ; p < p4 ; p++)
2074 if ((nvj = nv [j]) <= 0) continue ;
2081 Cp [i] = CS_FLIP (k) ;
2091 degree [i] = CS_MIN (degree [i], d) ;
2095 len [i] = pn - p1 + 1 ;
2097 next [i] = hhead [h] ;
2103 lemax = CS_MAX (lemax, dk) ;
2104 mark =
cs_wclear (mark+lemax, lemax, w, n) ;
2106 for (pk = pk1 ; pk < pk2 ; pk++)
2109 if (nv [i] >= 0) continue ;
2113 for ( ; i != -1 && next [i] != -1 ; i = next [i], mark++)
2117 for (p = Cp [i]+1 ; p <= Cp [i] + ln-1 ; p++) w [Ci [p]] = mark;
2119 for (j = next [i] ; j != -1 ; )
2121 ok = (len [j] == ln) && (elen [j] == eln) ;
2122 for (p = Cp [j] + 1 ; ok && p <= Cp [j] + ln - 1 ; p++)
2124 if (w [Ci [p]] != mark) ok = 0 ;
2128 Cp [j] = CS_FLIP (i) ;
2144 for (p = pk1, pk = pk1 ; pk < pk2 ; pk++)
2147 if ((nvi = -nv [i]) <= 0) continue ;
2149 d = degree [i] + dk - nvi ;
2150 d = CS_MIN (d, n - nel - nvi) ;
2151 if (head [d] != -1) last [head [d]] = i ;
2152 next [i] = head [d] ;
2155 mindeg = CS_MIN (mindeg, d) ;
2160 if ((len [k] = p-pk1) == 0)
2165 if (elenk != 0) cnz = p ;
2168 for (i = 0 ; i < n ; i++) Cp [i] = CS_FLIP (Cp [i]) ;
2169 for (j = 0 ; j <= n ; j++) head [j] = -1 ;
2170 for (j = n ; j >= 0 ; j--)
2172 if (nv [j] > 0) continue ;
2173 next [j] = head [Cp [j]] ;
2176 for (e = n ; e >= 0 ; e--)
2178 if (nv [e] <= 0) continue ;
2181 next [e] = head [Cp [e]] ;
2185 for (k = 0, i = 0 ; i <= n ; i++)
2187 if (Cp [i] == -1) k =
cs_tdfs (i, k, head, next, P, w) ;
2207 for (k = 0 ; k < n ; k++)
2211 for (p = Ap [k] ; p < Ap [k+1] ; p++)
2214 for ( ; i != -1 && i < k ; i = inext)
2216 inext = ancestor [i] ;
2218 if (inext == -1) parent [i] = k ;
2239 for (k = 0 ; k < n ; k++)
2243 for (p = Ap [k] ; p < Ap [k+1] ; p++)
2246 for ( ; i != -1 && i < k ; i = inext)
2248 inext = ancestor [i] ;
2250 if (inext == -1) parent [i] = k ;
2270 for (j = 0 ; j < n ; j++) head [j] = -1 ;
2271 for (j = n-1 ; j >= 0 ; j--)
2273 if (parent [j] == -1) continue ;
2274 next [j] = head [parent [j]] ;
2275 head [parent [j]] = j ;
2277 for (j = 0 ; j < n ; j++)
2279 if (parent [j] != -1) continue ;
2280 k =
cs_tdfs (j, k, head, next, post, stack) ;
2290 int q, s, sparent, jprev ;
2292 if (i <= j || first [j] <= maxfirst [i])
return (-1) ;
2293 maxfirst [i] = first [j] ;
2294 jprev = prevleaf [i] ;
2296 *jleaf = (jprev == -1) ? 1: 2 ;
2297 if (*jleaf == 1)
return (i) ;
2298 for (q = jprev ; q != ancestor [q] ; q = ancestor [q]) ;
2299 for (s = jprev ; s != q ; s = sparent)
2301 sparent = ancestor [s] ;
2311 int i, j, k, J, p, q, jleaf;
2318 hs_smatrix AT(n,A.nzmax);
2330 for (k = 0 ; k < n ; k++)
2333 delta [j] = (first [j] == -1) ? 1 : 0 ;
2334 for ( ; j != -1 && first [j] == -1 ; j = parent [j]) first [j] = k ;
2340 for (i = 0 ; i < n ; i++) ancestor [i] = i ;
2341 for (k = 0 ; k < n ; k++)
2344 if (parent [j] != -1) delta [parent [j]]-- ;
2345 for (J = j ; J != -1 ; J = -1)
2347 for (p = ATp [J] ; p < ATp [J+1] ; p++)
2350 q =
cs_leaf (i, j, first, maxfirst, prevleaf, ancestor, &jleaf);
2351 if (jleaf >= 1) delta [j]++ ;
2352 if (jleaf == 2) delta [q]-- ;
2355 if (parent [j] != -1) ancestor [j] = parent [j] ;
2357 for (j = 0 ; j < n ; j++)
2359 if (parent [j] != -1) colcount [parent [j]] += colcount [j] ;
2367 int i, j, k, J, p, q, jleaf;
2374 dvar_hs_smatrix AT(n,A.nzmax);
2386 for (k = 0 ; k < n ; k++)
2389 delta [j] = (first [j] == -1) ? 1 : 0 ;
2390 for ( ; j != -1 && first [j] == -1 ; j = parent [j]) first [j] = k ;
2396 for (i = 0 ; i < n ; i++) ancestor [i] = i ;
2397 for (k = 0 ; k < n ; k++)
2400 if (parent [j] != -1) delta [parent [j]]-- ;
2401 for (J = j ; J != -1 ; J = -1)
2403 for (p = ATp [J] ; p < ATp [J+1] ; p++)
2406 q =
cs_leaf (i, j, first, maxfirst, prevleaf, ancestor, &jleaf);
2407 if (jleaf >= 1) delta [j]++ ;
2408 if (jleaf == 2) delta [q]-- ;
2411 if (parent [j] != -1) ancestor [j] = parent [j] ;
2413 for (j = 0 ; j < n ; j++)
2415 if (parent [j] != -1) colcount [parent [j]] += colcount [j] ;
2426 for (k = 0 ; k < n ; k++) pinv [p [k]] = k ;
2486 if (order != 0 && order != 1 )
2488 cout <<
"!!! hs_symbolic: only order = 0,1 allowed" <<
endl;
2526 if (order != 0 && order != 1 )
2528 cout <<
"!!! hs_symbolic: only order = 0,1 allowed" <<
endl;
2532 dvar_hs_smatrix A(_n,T);
2541 dvar_hs_smatrix C(A);
2614 for (
int i=mmin;i<=mmax;i++)
2627 for (
int i=mmin;i<=mmax;i++)
2641 hs_smatrix HS(n,st);
2644 hs_smatrix * PL =
new hs_smatrix(S);
2648 PL->set_symbolic(S);
2658 dvar_hs_smatrix HS(n,st);
2661 dvar_hs_smatrix * PL =
new dvar_hs_smatrix(S);
2665 PL->set_symbolic(S);
2675 hs_smatrix HS(n,st);
2713 dvar_hs_smatrix& L= *PL;
2735 for (
int i=1;i<=nz;i++)
2737 st(i)=Hess(st(1,i),st(2,i));
2740 hs_smatrix HS(n,st);
2765 for (
int i=1;i<=nz;i++)
2767 st(i)=Hess(st(1,i),st(2,i));
2770 hs_smatrix HS(n,st);
2802 hs_smatrix HS(n,st);
2829 hs_smatrix HS(n,st);
2868 dvar_hs_smatrix H(n,VM);
2870 dvar_hs_smatrix L(S);
2871 int ierr=
chol(H,S,L);
2874 cerr <<
"Error matrix not positrive definite in chol" <<
endl;
2885 dvar_hs_smatrix H(n,VM);
2887 dvar_hs_smatrix L(S);
2888 int ierr=
chol(H,S,L);
2891 cerr <<
"Error matrix not positive definite in chol" <<
endl;
2907 dvar_hs_smatrix H(n,VM);
2909 dvar_hs_smatrix L(S);
2921 cerr <<
"Error matrix not positive definite in chol" <<
endl;
2943 int ierr=
chol(H,S,L);
2946 cerr <<
"Error matrix not positrive definite in chol" <<
endl;
2962 int ierr=
chol(H,S,L);
2965 cerr <<
"Error matrix not positrive definite in chol" <<
endl;
2979 hs_smatrix& A = (hs_smatrix&)AA;
2980 hs_smatrix& L = (hs_smatrix&)LL;
2982 int top, i, p, k, n;
3008 for (k = 0 ; k < n ; k++)
3009 Lp [k] = c [k] = cp [k] ;
3011 for (k = 0 ; k < n ; k++)
3016 for (p = Cp [k] ; p < Cp [k+1] ; p++)
3018 if (Ci [p] <= k) x [Ci [p]] = Cx [p] ;
3023 for ( ; top < n ; top++)
3026 lki = x [i] / Lx [Lp [i]] ;
3035 myacc(p,Lpi1,ci,Li,x,Lx,lki);
3050 if (d <= 0)
return (0) ;
3072 dvar_hs_smatrix& A = (dvar_hs_smatrix&)AA;
3073 dvar_hs_smatrix& L = (dvar_hs_smatrix&)LL;
3077 int top, i, p, k, n;
3087 dvar_hs_smatrix C(A);
3103 for (k = 0 ; k < n ; k++)
3105 Lp [k] = c [k] = cp [k] ;
3108 for (k = 0 ; k < n ; k++)
3112 for (p = Cp [k] ; p < Cp [k+1] ; p++)
3114 if (Ci[p] <= k) x [Ci[p]] = Cx[p] ;
3118 for ( ; top < n ; top++)
3121 lki = x[i] / Lx[Lp[i]] ;
3126 for (p = Lp [i] + 1 ; p < c [i] ; p++)
3128 x[Li[p]] -= Lx[p] * lki ;
3136 if (d <= 0)
return (0) ;
3146 for (
int ii=mmin;ii<=mmax;ii++)
3148 value(L.x(ii))=Lx(ii);
3150 int nxcount=txcount;
3151 int nccount=tccount;
3152 int nlkicount=lkicount;
3194 hs_smatrix A(sparse_triplet2->
get_n(),
3195 *(sparse_triplet2) );
3202 int top, i, p, k, n;
3260 ccount.initialize();
3261 Licount.initialize();
3262 Lxcount.initialize();
3263 xcount.initialize();
3267 for (k = 0 ; k < n ; k++)
3269 Lp [k] = c [k] = cp [k] ;
3275 for (k = 0 ; k < n ; k++)
3278 Top(k) =
cs_ereach (C, k, parent, s, c) ;
3283 cerr <<
"This can't happen" <<
endl;
3290 ssave(k)=s(Top(k),n-1);
3293 for (p = Cp [k] ; p < Cp [k+1] ; p++)
3307 for ( ; top < n ; top++)
3311 lkisave(lkicount++)=lki;
3312 lki = x [i] / Lx [Lp [i]] ;
3313 xsave(txcount++)=
x(i);
3315 for (p1 = Lp [i] + 1 ; p1 < c [i] ; p1++)
3317 x [Li [p1]] -= Lx [p1] * lki ;
3320 csave(tccount++)=c[i];
3329 cerr <<
"Error unhandled case in chol" <<
endl;
3335 cerr <<
"Error unhandled case in chol" <<
endl;
3340 if (d <= 0) return ;
3341 csave(tccount++)=c[k];
3351 cerr <<
"Error unhandled case in chol" <<
endl;
3357 cerr <<
"Error unhandled case in chol" <<
endl;
3366 for (k = n-1 ; k >=0 ; k--)
3368 c[k]=csave(--tccount);
3370 s(ssave(k).indexmin(),ssave(k).indexmax())=ssave(k);
3376 dfd+=dfLx(p)/(2.0*Lx(p));
3382 for (top=n-1 ; top >=Top[k] ; top--)
3386 c[i]=csave(--tccount);
3393 for (p1 = Lp [i] + 1 ; p1 < c [i] ; p1++)
3396 dflki-=dfx(Li(p1))*Lx(p1);
3397 dfLx(p1)-=dfx(Li(p1))*lki;
3401 x(i)=xsave(--txcount);
3403 dfx(i)+=dflki/Lx(Lp(i));
3404 dfLx(Lp(i))-=dflki*
x(i)/
square(Lx(Lp(i)));
3407 lki=lkisave(--lkicount);
3414 for (p1 = Cp [k+1]-1 ; p1 >= Cp [k] ; p1--)
3419 dfCx[p1]+=dfx[Ci[p1]];
3433 dvar_hs_smatrix &LL)
3438 dvar_hs_smatrix& A = (dvar_hs_smatrix&)AA;
3440 dvar_hs_smatrix& L = (dvar_hs_smatrix&)LL;
3444 int top, i, p, k, n;
3455 dvar_hs_smatrix C(A);
3470 for (k = 0 ; k < n ; k++)
3472 Lp [k] = c [k] = cp [k] ;
3475 for (k = 0 ; k < n ; k++)
3479 for (p = Cp [k] ; p < Cp [k+1] ; p++)
3481 if (Ci[p] <= k) x [Ci[p]] = Cx[p] ;
3485 for ( ; top < n ; top++)
3488 lki = x[i] / Lx[Lp[i]] ;
3491 for (p1 = Lp [i] + 1 ; p1 < c [i] ; p1++)
3493 x[Li[p1]] -= Lx[p1] * lki ;
3500 if (d <= 0)
return (0) ;
3540 void dvar_hs_smatrix::set_symbolic(
hs_symbolic& s)
3579 hs_smatrix HS(n,st);
3585 ofs1 << usize << xsize;
3596 << setw(14) << u(i) <<
" " <<
sqrt(
x(i-1)) <<
endl;;
3608 hs_smatrix& A = (hs_smatrix&)AA;
3609 hs_smatrix& B = (hs_smatrix&)BB;
3610 int p, j, nz = 0, anz, m, n, bnz;
3616 if (A.n != B.m)
return (NULL) ;
3617 m = A.m ; anz = A.p[A.n] ;
3619 dvector & Bx = B.x ; bnz = Bp[n] ;
3625 hs_smatrix C(n,anz + bnz) ;
3628 for (j = 0 ; j < n ; j++)
3639 for (p = Bp [j] ; p < Bp [j+1] ; p++)
3641 nz =
cs_scatter (A, Bi [p], Bx[p], w, x, j+1, C, nz) ;
3643 for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ;
3661 int _jmin=
M(mmin).indexmin();
3662 int _jmax=
M(mmax).indexmax();
3663 int m=_jmax-_jmin+1;
3665 for (
int i=mmin;i<=mmax;i++)
3667 int jmin=
M(i).indexmin();
3668 int jmax=
M(i).indexmax();
3669 for (
int j=jmin;j<=jmax;j++)
3671 if (
M(i,j) !=0) ii++;
3676 for (
int i=mmin;i<=mmax;i++)
3678 int jmin=
M(i).indexmin();
3679 int jmax=
M(i).indexmax();
3680 for (
int j=jmin;j<=jmax;j++)
3711 ofs <<
"nrows " << M.m <<
" ncols " << M.n <<
" nzmax " << M.nzmax
3713 ofs <<
"p = " << M.p <<
endl;
3714 ofs <<
"i = " << M.i <<
endl;
3715 ofs <<
"x = " << M.x <<
endl;
3726 for (
int j=1;j<=m;j++)
3728 for (
int i=M.p(j-1);i<M.p(j);i++)
3730 tmp(M.i(ii)+1,j)=M.x(ii);
Base class for dvariable.
int cs_fkeep(hs_smatrix &A, int(*fkeep)(int, int, double, void *), void *other)
void * restore_ad_pointer(void)
Description not yet available.
ivector cs_amd(XCONST hs_smatrix &A)
p = amd(A+A') if symmetric is true, or amd(A'A) otherwise
dvar_matrix make_sdvar_matrix(dvar_compressed_triplet &M)
void allocate(void)
Does not allocate, but initializes imatrix members.
dvector cs_lsolve(XCONST hs_smatrix &LL, XCONST dvector &y)
dvector cs_ipvec(XCONST ivector &p, XCONST dvector &b)
Description not yet available.
dvar_vector & shift(int min)
Description not yet available.
void allocate(int mmin, int mmax, int n, int m)
dvector cs_pvec(XCONST ivector &p, XCONST dvector &b)
dvar_matrix make_dvar_matrix(dvar_compressed_triplet &M)
dcompressed_triplet make_dcompressed_triplet(const dmatrix &M)
void deallocate()
Deallocate imatrix memory.
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Vector of double precision numbers.
int indexmin() const
Get minimum valid index.
void cs_transpose(XCONST hs_smatrix &_A, int values, hs_smatrix &C)
ivector cs_post(XCONST ivector &parent, int n)
hs_smatrix cs_add(XCONST hs_smatrix &AA, XCONST hs_smatrix &BB, double alpha, double beta)
void allocate(int ncl, int ncu)
Allocate memory for a dvector.
void initialize(void)
Zero initialize allocated dvar_matrix, then saves adjoint function and position data.
int cholnew(XCONST hs_smatrix &A, XCONST hs_symbolic &S, hs_smatrix &L)
dcompressed_triplet(int mmin, int mmax, int n, int m)
void save_dvector_derivatives(const dvar_vector_position &pos) const
Puts the derivative values in a dvector into a dvar_vector's guts.
Description not yet available.
ivector cs_pinv(XCONST ivector &p, int n)
void verify_identifier_string(const char *)
Verifies gradient stack string.
static void dfcholeski_sparse(void)
void save_int_value(int x)
int cs_tdfs(int j, int k, ivector &head, XCONST ivector &next, ivector &post, ivector &stack)
int cs_diag(int i, int j, double aij, void *other)
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
int varchol(XCONST dvar_hs_smatrix &A, XCONST hs_symbolic &S, dvar_hs_smatrix &L, dcompressed_triplet &sparse_triplet2)
static void xxxv(ivector &x)
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
void allocate(int mmin, int mmax, int n, int m)
prescientific setscientific(void)
Description not yet available.
hs_smatrix make_hs_smatrix(const dmatrix &M)
dvar_compressed_triplet(int mmin, int mmax, int n, int m)
void report_derivatives(const dvar_vector &x)
int cs_scatter(XCONST hs_smatrix &AA, int j, double beta, ivector &w, dvector &x, int mark, hs_smatrix &C, int nz)
prnstream & endl(prnstream &)
void myacc(int &p, int Lpi1, int ci, const ivector &Li, dvector &x, const dvector &Lx, const double &lki)
Array of integers(int) with indexes from index_min to indexmax.
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int cs_wclear(int mark, int lemax, ivector &w, int n)
hs_smatrix cs_multiply(const hs_smatrix &A, const hs_smatrix &B)
dvector return_choleski_decomp_solve(dcompressed_triplet &dct, dvector &eps)
dvar_vector_position restore_dvar_vector_position()
void RETURN_ARRAYS_INCREMENT()
Description not yet available.
int indexmax() const
Get maximum valid index.
ivector cs_etree(XCONST hs_smatrix &_A)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int tmpxchol1(XCONST hs_smatrix &A, XCONST hs_symbolic &T, hs_smatrix &L, ivector &nxcount)
Description not yet available.
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
static _THREAD gradient_structure * _instance
double cs_cumsum(ivector &p, ivector &c, int n)
Description not yet available.
void initialize(void)
Initialze all elements of dvector to zero.
dvector & shift(int min)
Shift valid range of subscripts.
static void xxx(ivector re_list, ivector fe_list)
int save_identifier_string(const char *)
Writes a gradient stack verification string.
Description not yet available.
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
void hs_symperm(XCONST hs_smatrix &_A, XCONST ivector &pinv, hs_smatrix &C)
void save_ad_pointer(void *p)
Description not yet available.
int cs_leaf(int i, int j, XCONST ivector &first, ivector &maxfirst, ivector &prevleaf, ivector &ancestor, int *jleaf)
void save_dvar_vector_position(const dvar_vector &v)
void allocate(int, int)
Allocate dvar_vector with indexmin = ncl and indexmax = nch.
void initialize(void)
Description not yet available.
static _THREAD DF_FILE * fp
dmatrix make_dmatrix(dcompressed_triplet &M)
void reallocate(double size)
Reallocate size of array.
istream & operator>>(const istream &input, const d3_array &arr3)
Read values from input stream into arr3.
ostream & operator<<(const ostream &_s, preshowpoint p)
Description not yet available.
ivector cs_counts(XCONST hs_smatrix &A, XCONST ivector &parent, XCONST ivector &post)
int chol(XCONST hs_smatrix &A, XCONST hs_symbolic &T, hs_smatrix &L)
Description not yet available.
int tmpxchol(XCONST hs_smatrix &A, XCONST hs_symbolic &S, hs_smatrix &L, ivector &xcount)
Class definition of matrix with derivitive information .
Stores the adjoint gradient data that will be processed by gradcalc.
dvector restore_dvar_vector_derivatives(const dvar_vector_position &tmp)
Description not yet available.
void get_inverse_sparse_hessian(dcompressed_triplet &st, hs_symbolic &S, uostream &ofs1, ofstream &ofs, int usize, int xsize, dvector &u)
void deallocate()
Deallocate dvar_vector memory.
void RETURN_ARRAYS_DECREMENT()
void initialize(const dvector &ww)
Description not yet available.
dvector value(const df1_one_vector &v)
static _THREAD grad_stack * GRAD_STACK1
void report_dvar_vector_derivatives(void)
dvector cs_ltsolve(XCONST hs_smatrix &LL, XCONST dvector &y)
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
dvector return_choleski_factor_solve(hs_smatrix *PL, dvector &eps)
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
hs_smatrix * return_choleski_decomp(dcompressed_triplet &st)
dmatrix make_sdmatrix(dcompressed_triplet &M)
void allocate(const ad_integer &ncl, const index_type &ncu)
Allocate vector of integers with dimension [_ncl to _nch].
double square(const double value)
Return square of value; constant object.
Fundamental data type for reverse mode automatic differentiation.
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int cs_ereach(XCONST hs_smatrix &_A, int k, XCONST ivector &parent, ivector &s, ivector &w)
void deallocate(void)
Called by destructor to deallocate memory for a dvector object.