49 using std::resetiosflags;
50 using std::setiosflags;
64 : _base_constrainer_args(defs) {
190 bool solve_linear_system(
const Matrix&
H,
217 int nconstraints =
H.num_row();
218 int nbadvars =
Y.num_row();
221 Matrix A(nconstraints + nbadvars, nconstraints + nbadvars);
224 A.sub(nconstraints + 1, nconstraints + 1,
Y);
225 A.sub(1, nconstraints + 1, By.T());
226 A.sub(nconstraints + 1, 1, By);
238 Ai =
A.inverse(ierr);
241 for (
int i = 1;
i <= nconstraints;
i++) {
243 for (
int j = 1;
j <= nconstraints;
j++) {
264 W = Ai.sub(1, nconstraints, 1, nconstraints);
266 U = Ai.sub(nconstraints + 1, nconstraints + nbadvars, nconstraints + 1, nconstraints + nbadvars);
267 V = Ai.sub(nconstraints + 1, nconstraints + nbadvars, 1, nconstraints);
268 d =
xx.sub(nconstraints + 1, nconstraints + nbadvars);
271 alpha =
xx.sub(1, nconstraints);
335 int nvars = x.num_row();
336 assert(nvars == G_i.num_col());
337 assert(nvars == xm.num_row());
339 int nbadvars = y.num_row();
340 assert(nbadvars ==
Y.num_col());
341 assert(nbadvars == ym.num_row());
348 G = G_i.inverse(ierr);
352 int nconstraints = constraint_calculator.
nconstraints();
356 Matrix Bx(nvars, nconstraints);
357 Matrix By(nbadvars, nconstraints);
369 double constraint_sum_last = -1000;
370 double chisq_last = -1000;
371 bool near_convergence =
false;
372 double last_step_cutsize = 1;
380 Matrix E(nvars, nconstraints);
381 Matrix W(nconstraints, nconstraints);
383 Matrix V(nbadvars, nconstraints);
397 if (!solve_linear_system(
H,
Y, By,
r,
alpha,
d1, W,
U,
V)) {
416 double this_step_cutsize = 1;
417 double constraint_sum = -1;
431 constraint_sum > constraint_sum_last)) {
434 cout <<
"(" << chisq <<
" " << chisq_last <<
") ";
445 cout <<
" directed step ";
451 solve_linear_system(
H,
Y, save_By, save_negF,
beta,
delta, W,
U,
V);
462 if (
call_constraint_fcn(constraint_calculator, x, y,
F, Bx, By) && (constraint_sum = norm_infinity(
F)) > 0 &&
463 (constraint_sum < constraint_sum_last)) {
466 chisq = chisq_last -
scalar((-save_negF +
r * 2) *
beta);
493 if (ncut == 1 && last_step_cutsize < 1) {
494 this_cutsize = 2 * last_step_cutsize;
500 this_step_cutsize *= this_cutsize;
510 double cutleft = 1 - this_cutsize;
511 c1 =
c1 * this_cutsize +
c * cutleft;
512 d1 =
d1 * this_cutsize +
d * cutleft;
515 if (chisq_last >= 0) {
516 chisq = this_cutsize * this_cutsize * chisq + cutleft * cutleft * chisq_last +
517 2 * this_cutsize * cutleft * psi_cut;
518 psi_cut = this_cutsize * psi_cut + cutleft * chisq_last;
524 cout << constraint_sum <<
" cut " << ncut <<
" size " << setiosflags(ios_base::scientific) << this_cutsize
525 <<
" tot size " << this_step_cutsize << resetiosflags(ios_base::scientific) <<
" " << chisq <<
"\n";
537 last_step_cutsize = this_step_cutsize;
545 cout << chisq <<
" " << chisq_b <<
"lost precision?\n";
552 cout << chisq <<
" ";
554 cout << chisq_b <<
" ";
557 double z2 =
abs(chisq - chisq_last);
560 cout << constraint_sum <<
" " <<
z2 <<
"\n";
566 constraint_sum_last = constraint_sum;
571 if (near_convergence)
573 near_convergence =
true;
575 near_convergence =
false;
595 for (
int i = 1;
i <= nvars;
i++) {
606 for (
int i = 1;
i <= nbadvars;
i++) {
607 double a = 1 -
Y(
i,
i) *
R(
i,
i);
635 s <<
" printfit: " << _args.printfit() <<
" use_G: " << _args.use_G() <<
"\n";
636 s <<
" constraint_sum_eps: " << _args.constraint_sum_eps() <<
" chisq_diff_eps: " << _args.chisq_diff_eps()
637 <<
" chisq_test_eps: " << _args.chisq_test_eps() <<
"\n";
638 s <<
" maxit: " << _args.maxit() <<
" max_cut: " << _args.max_cut()
639 <<
" min_tot_cutsize: " << _args.min_tot_cutsize() <<
" cutsize: " << _args.cutsize() <<
"\n";