Skip to content

Commit 851929e

Browse files
authored
Merge pull request #423 from MethodicalAcceleratorDesign/dev
sync master
2 parents 75136be + ead7372 commit 851929e

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

src/mad_tpsa.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,20 +569,26 @@ FUN(cycle) (const T *t, idx_t i, ssz_t n, ord_t m_[n], NUM *v_)
569569
{
570570
assert(t); DBGFUN(->); DBGTPSA(t);
571571
const D *d = t->d;
572-
i += 1;
573-
ensure(0 <= i && i <= d->nc, "index %d out of bounds", i);
572+
if (++i == d->nc) { DBGFUN(<-); return -1; }
573+
ensure(0 <= i && i < d->nc, "index %d out of bounds", i);
574574

575575
const idx_t *o2i = d->ord2idx;
576-
idx_t ni = o2i[MIN(t->hi,d->to)+1];
577-
for (i = MAX(i,o2i[t->lo]); i < ni && t->coef[i] == 0; ++i) ;
576+
ord_t lo = MAX(t->lo, d->ords[i]);
577+
idx_t hi = MIN(t->hi, d->to);
578+
579+
for (ord_t o = lo; o <= hi ; ++o) {
580+
if (!mad_bit_tst(t->nz,o)) continue;
581+
for (i = MAX(i,o2i[o]); i < o2i[o+1] && !t->coef[i]; ++i) ;
582+
if (i < o2i[o+1] && t->coef[i]) break;
583+
}
578584

579-
if (i >= ni) { DBGFUN(<-); return -1; }
585+
if (i >= o2i[hi+1]) { DBGFUN(<-); return -1; }
580586

587+
if (v_) *v_ = t->coef[i];
581588
if (m_) {
582589
ensure(0 <= n && n <= d->nn, "invalid monomial length %d", n);
583590
mad_mono_copy(n, d->To[i], m_);
584591
}
585-
if (v_) *v_ = t->coef[i];
586592

587593
DBGFUN(<-); return i;
588594
}
@@ -604,7 +610,7 @@ FUN(geti) (const T *t, idx_t i)
604610
const D *d = t->d;
605611
ensure(0 <= i && i < d->nc, "index %d out of bounds", i);
606612
ord_t o = d->ords[i];
607-
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) ? t->coef[i] : 0;
613+
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) && mad_bit_tst(t->nz,o) ? t->coef[i] : 0;
608614
DBGFUN(<-); return ret;
609615
}
610616

@@ -617,7 +623,7 @@ FUN(gets) (const T *t, ssz_t n, str_t s)
617623
idx_t i = mad_desc_idxs(d,n,s);
618624
ensure(i >= 0, "invalid monomial");
619625
ord_t o = d->ords[i];
620-
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) ? t->coef[i] : 0;
626+
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) && mad_bit_tst(t->nz,o) ? t->coef[i] : 0;
621627
DBGFUN(<-); return ret;
622628
}
623629

@@ -629,7 +635,7 @@ FUN(getm) (const T *t, ssz_t n, const ord_t m[n])
629635
idx_t i = mad_desc_idxm(d,n,m);
630636
ensure(i >= 0, "invalid monomial");
631637
ord_t o = d->ords[i];
632-
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) ? t->coef[i] : 0;
638+
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) && mad_bit_tst(t->nz,o) ? t->coef[i] : 0;
633639
DBGFUN(<-); return ret;
634640
}
635641

@@ -642,7 +648,7 @@ FUN(getsm) (const T *t, ssz_t n, const idx_t m[n])
642648
idx_t i = mad_desc_idxsm(d,n,m);
643649
ensure(i >= 0, "invalid monomial");
644650
ord_t o = d->ords[i];
645-
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) ? t->coef[i] : 0;
651+
NUM ret = t->lo <= o && o <= MIN(t->hi,d->to) && mad_bit_tst(t->nz,o) ? t->coef[i] : 0;
646652
DBGFUN(<-); return ret;
647653
}
648654

@@ -658,7 +664,7 @@ FUN(getv) (const T *t, idx_t i, ssz_t n, NUM v[n])
658664
const ord_t *ord = d->ords+i;
659665
const NUM *coef = t->coef+i;
660666
for (idx_t j = 0; j < n; ++j)
661-
v[j] = t->lo <= ord[j] && ord[j] <= hi ? coef[j] : 0;
667+
v[j] = t->lo <= ord[j] && ord[j] <= hi && mad_bit_tst(t->nz,ord[j]) ? coef[j] : 0;
662668

663669
DBGFUN(<-);
664670
}

src/mad_tpsa_ops.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,16 @@ num_t
675675
FUN(nrm) (const T *a)
676676
{
677677
assert(a); DBGFUN(->); DBGTPSA(a);
678-
679-
num_t nrm = 0;
680678
ord_t hi = MIN(a->hi, a->d->to);
679+
num_t nrm = 0;
680+
681681
if (mad_bit_hcut(a->nz,hi)) {
682682
const idx_t *o2i = a->d->ord2idx;
683-
for (idx_t i = o2i[a->lo]; i < o2i[hi+1]; ++i)
684-
nrm += fabs(a->coef[i]);
683+
for (ord_t o = a->lo; o <= hi ; ++o) {
684+
if (!mad_bit_tst(a->nz,o)) continue;
685+
for (idx_t i = o2i[o]; i < o2i[o+1]; ++i)
686+
nrm += fabs(a->coef[i]);
687+
}
685688
}
686689

687690
DBGFUN(<-); return nrm;

src/madl_lsopt.mad

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ local function ld_lmdif (env)
265265
local jstra, jiter, jtau, bisec, rcond, ncond in obj
266266

267267
-- default setup
268-
jstra, jiter, jtau, bisec, rcond = jstra or 1, max(1, jiter or 10),
269-
max(eps, jtau or 1e-3), bisec or obj.exec and 0 or 3,
270-
rcond or 1e-12
268+
jstra, jiter, jtau, bisec, rcond, ncond = jstra or 1, max(1, jiter or 10),
269+
max(eps, jtau or 1e-3), bisec or obj.exec and 0 or 3,
270+
rcond or 1e-12, ncond or 0
271271

272272
if env.info >= 3 then
273-
printf("nlopt: jstra=%d, jiter=%d, jtau=%g, bisec=%d, rcond=%g\n",
274-
jstra, jiter, jtau, bisec, rcond)
273+
printf("nlopt: jstra=%d, jiter=%d, jtau=%g, bisec=%d, rcond=%g, ncond=%d\n",
274+
jstra, jiter, jtau, bisec, rcond, ncond)
275275
end
276276

277277
-- adjust variables (i.e. adjust x to fit bbox if any)
@@ -420,13 +420,13 @@ local function ld_jacobian (env)
420420
local jstra, jiter, bisec, rcond, ncond in obj
421421

422422
-- default setup
423-
jstra, jiter, bisec, rcond = jstra or 1, max(1, jiter or 10),
424-
bisec or obj.exec and 0 or 3,
425-
rcond or 1e-12
423+
jstra, jiter, bisec, rcond, ncond = jstra or 1, max(1, jiter or 10),
424+
bisec or obj.exec and 0 or 3,
425+
rcond or 1e-12, ncond or 0
426426

427427
if env.info >= 3 then
428-
printf("nlopt: jstra=%d, jiter=%d, bisec=%d, rcond=%g\n",
429-
jstra, jiter, bisec, rcond)
428+
printf("nlopt: jstra=%d, jiter=%d, bisec=%d, rcond=%g, ncond=%d\n",
429+
jstra, jiter, bisec, rcond, ncond)
430430
end
431431

432432
-- adjust variables (i.e. adjust x to fit bbox if any)

src/madl_match.mad

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,18 @@ local function compute_fgrd (env)
560560
-- backup states
561561
backup(var, bak)
562562

563-
-- last step norm
563+
-- estimate dh from last step or current point
564564
local hn = h:norm()
565+
local dh = 1e-4*hn
566+
if dh == 0 then dh = 1e-8*x:norm() end
567+
if dh == 0 then dh = 1e-10 end
568+
569+
if trc then printf("nlopt: computing derivatives with dh=%-.5e\n", dh) end
565570

566571
-- Broyden's Jacobian update
567572
local bro, Bc = env.objective.broyden and prv.c ~= nil and hn ~= 0
568573
if bro then
569-
if trc then printf("nlopt: Broyden's update with |hn|=%-.8e\n", hn) end
574+
if trc then printf("nlopt: Broyden's update with |hn|=%-.5e\n", hn) end
570575
Bc = cjac + (c - prv.c - cjac*h)*h:t()/hn^2
571576
end
572577

@@ -575,12 +580,13 @@ local function compute_fgrd (env)
575580
if bro and h[iv] >= 0.8*hn then -- Broyden's rank one update
576581
cjac:setcol(iv, Bc:getcol(iv))
577582
if trc then
578-
printf("nlopt: Broyden's update for variable %d (%-.8e)\n", iv, h[iv])
583+
printf("nlopt: Broyden's update for variable %d (%-.5e)\n", iv, h[iv])
579584
end
580585
else -- finite difference required
581-
local ih = x[iv] ~= 0 and x[iv]*xstp[iv] or 1e-12
586+
local ih = x[iv]*xstp[iv]
587+
if ih == 0 then ih = dh end
582588
x[iv] = x[iv]+ih ; var.fval = fval
583-
if x[iv] < xmin[iv] or x[iv] > xmax[iv] then
589+
if x[iv] < xmin[iv] or x[iv] > xmax[iv] or ih*xslp[iv] < 0 then
584590
x[iv], ih = x[iv]-2*ih, -ih -- take -ih if bbox are violated
585591
end
586592

@@ -814,7 +820,7 @@ local function exec (self)
814820
local v = variables[i] or {}
815821
if v.get then var.x0[i] = v.get(env) end
816822
var.x [i] = var.x0[i]
817-
var.xstp[i] = v.rstp or variables.rstp or 1e-8
823+
var.xstp[i] = v.rstp or variables.rstp or 0
818824
var.xslp[i] = v.slope or variables.slope or 0
819825
var.xtol[i] = v.tol or variables.tol or 0
820826
var.xmin[i] = v.min or variables.min or -inf

0 commit comments

Comments
 (0)