use ::libc;
extern "C" {
#[no_mangle]
fn sqrt(_: libc::c_double) -> libc::c_double;
#[no_mangle]
fn R_finite(_: libc::c_double) -> libc::c_int;
#[no_mangle]
fn Rprintf(_: *const libc::c_char, _: ...);
#[no_mangle]
fn REprintf(_: *const libc::c_char, _: ...);
#[no_mangle]
fn sgram_(
sg0: *mut libc::c_double,
sg1: *mut libc::c_double,
sg2: *mut libc::c_double,
sg3: *mut libc::c_double,
tb: *mut libc::c_double,
nb: *mut libc::c_int,
);
#[no_mangle]
fn stxwx_(
x: *mut libc::c_double,
z: *mut libc::c_double,
w: *mut libc::c_double,
k: *mut libc::c_int,
xknot: *mut libc::c_double,
n: *mut libc::c_int,
y: *mut libc::c_double,
hs0: *mut libc::c_double,
hs1: *mut libc::c_double,
hs2: *mut libc::c_double,
hs3: *mut libc::c_double,
);
#[no_mangle]
fn sslvrg_(
penalt: *mut libc::c_double,
dofoff: *mut libc::c_double,
x: *mut libc::c_double,
y: *mut libc::c_double,
w: *mut libc::c_double,
ssw: *mut libc::c_double,
n: *mut libc::c_int,
knot: *mut libc::c_double,
nk: *mut libc::c_int,
coef: *mut libc::c_double,
sz: *mut libc::c_double,
lev: *mut libc::c_double,
crit: *mut libc::c_double,
icrit: *mut libc::c_int,
lambda: *mut libc::c_double,
xwy: *mut libc::c_double,
hs0: *mut libc::c_double,
hs1: *mut libc::c_double,
hs2: *mut libc::c_double,
hs3: *mut libc::c_double,
sg0: *mut libc::c_double,
sg1: *mut libc::c_double,
sg2: *mut libc::c_double,
sg3: *mut libc::c_double,
abd: *mut libc::c_double,
p1ip: *mut libc::c_double,
p2ip: *mut libc::c_double,
ld4: *mut libc::c_int,
ldnk: *mut libc::c_int,
info: *mut libc::c_int,
);
#[no_mangle]
fn R_pow(x: libc::c_double, y: libc::c_double) -> libc::c_double;
#[no_mangle]
fn fsign(_: libc::c_double, _: libc::c_double) -> libc::c_double;
}
pub type Rboolean = libc::c_uint;
pub const TRUE: Rboolean = 1;
pub const FALSE: Rboolean = 0;
#[no_mangle]
pub unsafe extern "C" fn sbart_(
mut penalt: *mut libc::c_double,
mut dofoff: *mut libc::c_double,
mut xs: *mut libc::c_double,
mut ys: *mut libc::c_double,
mut ws: *mut libc::c_double,
mut ssw: *mut libc::c_double,
mut n: *mut libc::c_int,
mut knot: *mut libc::c_double,
mut nk: *mut libc::c_int,
mut coef: *mut libc::c_double,
mut sz: *mut libc::c_double,
mut lev: *mut libc::c_double,
mut crit: *mut libc::c_double,
mut icrit: *mut libc::c_int,
mut spar: *mut libc::c_double,
mut ispar: *mut libc::c_int,
mut iter: *mut libc::c_int,
mut lspar: *mut libc::c_double,
mut uspar: *mut libc::c_double,
mut tol: *mut libc::c_double,
mut eps: *mut libc::c_double,
mut Ratio: *mut libc::c_double,
mut isetup: *mut libc::c_int,
mut xwy: *mut libc::c_double,
mut hs0: *mut libc::c_double,
mut hs1: *mut libc::c_double,
mut hs2: *mut libc::c_double,
mut hs3: *mut libc::c_double,
mut sg0: *mut libc::c_double,
mut sg1: *mut libc::c_double,
mut sg2: *mut libc::c_double,
mut sg3: *mut libc::c_double,
mut abd: *mut libc::c_double,
mut p1ip: *mut libc::c_double,
mut p2ip: *mut libc::c_double,
mut ld4: *mut libc::c_int,
mut ldnk: *mut libc::c_int,
mut ier: *mut libc::c_int,
) {
let mut current_block: u64;
static mut c_Gold: libc::c_double = 0.381966011250105151795413165634f64;
static mut ratio: libc::c_double = 0.;
let mut a: libc::c_double = 0.;
let mut b: libc::c_double = 0.;
let mut d: libc::c_double = 0.;
let mut e: libc::c_double = 0.;
let mut p: libc::c_double = 0.;
let mut q: libc::c_double = 0.;
let mut r: libc::c_double = 0.;
let mut u: libc::c_double = 0.;
let mut v: libc::c_double = 0.;
let mut w: libc::c_double = 0.;
let mut x: libc::c_double = 0.;
let mut ax: libc::c_double = 0.;
let mut fu: libc::c_double = 0.;
let mut fv: libc::c_double = 0.;
let mut fw: libc::c_double = 0.;
let mut fx: libc::c_double = 0.;
let mut bx: libc::c_double = 0.;
let mut xm: libc::c_double = 0.;
let mut tol1: libc::c_double = 0.;
let mut tol2: libc::c_double = 0.;
let mut i: libc::c_int = 0;
let mut maxit: libc::c_int = 0;
let mut Fparabol: Rboolean = FALSE;
let mut tracing: Rboolean = (*ispar < 0 as libc::c_int) as libc::c_int as Rboolean;
let mut spar_is_lambda: Rboolean = FALSE;
d = 0.0f64;
fu = 0.0f64;
u = 0.0f64;
ratio = 1.0f64;
i = 0 as libc::c_int;
while i < *n {
if *ws.offset(i as isize) > 0.0f64 {
*ws.offset(i as isize) = sqrt(*ws.offset(i as isize))
}
i += 1
}
if *isetup < 0 as libc::c_int {
spar_is_lambda = TRUE
} else if *isetup != 1 as libc::c_int {
sgram_(sg0, sg1, sg2, sg3, knot, nk);
stxwx_(xs, ys, ws, n, knot, nk, xwy, hs0, hs1, hs2, hs3);
spar_is_lambda = (*isetup == 2 as libc::c_int) as libc::c_int as Rboolean;
if spar_is_lambda as u64 == 0 {
let mut t1: libc::c_double = 0.0f64;
let mut t2: libc::c_double = 0.0f64;
i = 3 as libc::c_int - 1 as libc::c_int;
while i < *nk - 3 as libc::c_int {
t1 += *hs0.offset(i as isize);
t2 += *sg0.offset(i as isize);
i += 1
}
ratio = t1 / t2
}
*isetup = 1 as libc::c_int
}
if *ispar == 1 as libc::c_int {
*lspar = if spar_is_lambda as libc::c_uint != 0 {
*spar
} else {
(ratio) * R_pow(16.0f64, *spar * 6.0f64 - 2.0f64)
};
sslvrg_(
penalt, dofoff, xs, ys, ws, ssw, n, knot, nk, coef, sz, lev, crit, icrit, lspar, xwy,
hs0, hs1, hs2, hs3, sg0, sg1, sg2, sg3, abd, p1ip, p2ip, ld4, ldnk, ier,
);
*Ratio = ratio;
return;
}
ax = *lspar;
bx = *uspar;
maxit = *iter;
*iter = 0 as libc::c_int;
a = ax;
b = bx;
v = a + c_Gold * (b - a);
w = v;
x = v;
e = 0.0f64;
*lspar = if spar_is_lambda as libc::c_uint != 0 {
x
} else {
(ratio) * R_pow(16.0f64, x * 6.0f64 - 2.0f64)
};
sslvrg_(
penalt, dofoff, xs, ys, ws, ssw, n, knot, nk, coef, sz, lev, crit, icrit, lspar, xwy, hs0,
hs1, hs2, hs3, sg0, sg1, sg2, sg3, abd, p1ip, p2ip, ld4, ldnk, ier,
);
fx = *crit;
fv = fx;
fw = fx;
while *ier == 0 as libc::c_int {
xm = (a + b) * 0.5f64;
tol1 = *eps * x.abs() + *tol / 3.0f64;
tol2 = tol1 * 2.0f64;
*iter += 1;
if tracing as u64 != 0 {
if *iter == 1 as libc::c_int {
Rprintf(b"sbart (ratio = %15.8g) iterations; initial tol1 = %12.6e :\n%11s %14s %9s %11s Kind %11s %12s\n%s\n\x00"
as *const u8 as *const libc::c_char, ratio, tol1,
b"spar\x00" as *const u8 as *const libc::c_char,
if *icrit == 1 as libc::c_int {
b"GCV\x00" as *const u8 as *const libc::c_char
} else if *icrit == 2 as libc::c_int {
b"CV\x00" as *const u8 as *const libc::c_char
} else if *icrit == 3 as libc::c_int {
b"(df0-df)^2\x00" as *const u8 as
*const libc::c_char
} else {
b"?f?\x00" as *const u8 as *const libc::c_char
}, b"b - a\x00" as *const u8 as *const libc::c_char,
b"e\x00" as *const u8 as *const libc::c_char,
b"NEW lspar\x00" as *const u8 as *const libc::c_char,
b"crit\x00" as *const u8 as *const libc::c_char,
b" -------------------------------------------------------------------------------\x00"
as *const u8 as *const libc::c_char);
}
Rprintf(
b"%11.8f %14.9g %9.4e %11.5g\x00" as *const u8 as *const libc::c_char,
x,
if *icrit == 3 as libc::c_int {
(fx) - 3.0f64
} else {
fx
},
b - a,
e,
);
Fparabol = FALSE
}
if (x - xm).abs() <= tol2 - (b - a) * 0.5f64 || *iter > maxit {
break;
}
if e.abs() <= tol1 || fx >= 1e100f64 || fv >= 1e100f64 || fw >= 1e100f64 {
current_block = 11783109083342941207;
} else {
if tracing as u64 != 0 {
Rprintf(b" FP\x00" as *const u8 as *const libc::c_char);
Fparabol = TRUE
}
r = (x - w) * (fx - fv);
q = (x - v) * (fx - fw);
p = (x - v) * q - (x - w) * r;
q = (q - r) * 2.0f64;
if q > 0.0f64 {
p = -p
}
q = q.abs();
r = e;
e = d;
if p.abs() >= (0.5f64 * q * r).abs() || q == 0.0f64 {
current_block = 11783109083342941207;
} else if p <= q * (a - x) || p >= q * (b - x) {
current_block = 11783109083342941207;
} else {
if tracing as u64 != 0 {
Rprintf(b" PI \x00" as *const u8 as *const libc::c_char);
}
d = p / q;
if R_finite(d) == 0 {
REprintf(
b" !FIN(d:=p/q): ier=%d, (v,w, p,q)= %g, %g, %g, %g\n\x00" as *const u8
as *const libc::c_char,
*ier,
v,
w,
p,
q,
);
}
u = x + d;
if u - a < tol2 || b - u < tol2 {
d = fsign(tol1, xm - x)
}
current_block = 388264277043112403;
}
}
match current_block {
11783109083342941207 =>
{
if tracing as u64 != 0 {
Rprintf(
b" GS%s \x00" as *const u8 as *const libc::c_char,
if Fparabol as libc::c_uint != 0 {
b"\x00" as *const u8 as *const libc::c_char
} else {
b" --\x00" as *const u8 as *const libc::c_char
},
);
}
if x >= xm {
e = a - x
} else {
e = b - x
}
d = c_Gold * e
}
_ => {}
}
u = x + (if d.abs() >= tol1 { d } else { fsign(tol1, d) });
*lspar = if spar_is_lambda as libc::c_uint != 0 {
u
} else {
(ratio) * R_pow(16.0f64, u * 6.0f64 - 2.0f64)
};
sslvrg_(
penalt, dofoff, xs, ys, ws, ssw, n, knot, nk, coef, sz, lev, crit, icrit, lspar, xwy,
hs0, hs1, hs2, hs3, sg0, sg1, sg2, sg3, abd, p1ip, p2ip, ld4, ldnk, ier,
);
fu = *crit;
if tracing as u64 != 0 {
Rprintf(
b"%11g %12g\n\x00" as *const u8 as *const libc::c_char,
*lspar,
if *icrit == 3 as libc::c_int {
(fu) - 3.0f64
} else {
fu
},
);
}
if R_finite(fu) == 0 {
REprintf(
b"spar-finding: non-finite value %g; using BIG value\n\x00" as *const u8
as *const libc::c_char,
fu,
);
fu = 2.0f64 * 1e100f64
}
if fu <= fx {
if u >= x {
a = x
} else {
b = x
}
v = w;
fv = fw;
w = x;
fw = fx;
x = u;
fx = fu
} else {
if u < x {
a = u
} else {
b = u
}
if fu <= fw || w == x {
v = w;
fv = fw;
w = u;
fw = fu
} else if fu <= fv || v == x || v == w {
v = u;
fv = fu
}
}
}
if tracing as u64 != 0 {
Rprintf(
b" >>> %12g %12g\n\x00" as *const u8 as *const libc::c_char,
*lspar,
if *icrit == 3 as libc::c_int {
(fx) - 3.0f64
} else {
fx
},
);
}
*Ratio = ratio;
*spar = x;
*crit = fx;
}