use crate::{
sexprec::{SEXP, SEXPREC, SEXPREC_ALIGN, SEXPTYPE, VECSEXP},
zeroin::R_zeroin2,
};
use ::libc;
extern "C" {
pub type R_allocator;
#[no_mangle]
fn SET_STRING_ELT(x: SEXP, i: R_xlen_t, v: SEXP);
#[no_mangle]
fn SET_VECTOR_ELT(x: SEXP, i: R_xlen_t, v: SEXP) -> SEXP;
#[no_mangle]
fn ALTREP_LENGTH(x: SEXP) -> R_xlen_t;
#[no_mangle]
fn ALTVEC_DATAPTR(x: SEXP) -> *mut libc::c_void;
#[no_mangle]
fn ALTSTRING_ELT(_: SEXP, _: R_xlen_t) -> SEXP;
#[no_mangle]
fn R_BadLongVector(_: SEXP, _: *const libc::c_char, _: libc::c_int) -> !;
#[no_mangle]
fn SETCADR(x: SEXP, y: SEXP) -> SEXP;
#[no_mangle]
fn sqrt(_: libc::c_double) -> libc::c_double;
#[no_mangle]
static mut R_NegInf: libc::c_double;
#[no_mangle]
static mut R_NaInt: libc::c_int;
#[no_mangle]
fn R_IsNA(_: libc::c_double) -> libc::c_int;
#[no_mangle]
fn error(_: *const libc::c_char, _: ...) -> !;
#[no_mangle]
fn warning(_: *const libc::c_char, _: ...);
#[no_mangle]
fn R_alloc(_: size_t, _: libc::c_int) -> *mut libc::c_char;
#[no_mangle]
fn Rprintf(_: *const libc::c_char, _: ...);
#[no_mangle]
static mut R_NilValue: SEXP;
#[no_mangle]
static mut R_NamesSymbol: SEXP;
#[no_mangle]
static mut R_ClassSymbol: SEXP;
#[no_mangle]
fn coerceVector(_: SEXP, _: SEXPTYPE) -> SEXP;
#[no_mangle]
fn asLogical(x: SEXP) -> libc::c_int;
#[no_mangle]
fn asInteger(x: SEXP) -> libc::c_int;
#[no_mangle]
fn asReal(x: SEXP) -> libc::c_double;
#[no_mangle]
fn allocMatrix(_: SEXPTYPE, _: libc::c_int, _: libc::c_int) -> SEXP;
#[no_mangle]
fn allocVector3(_: SEXPTYPE, _: R_xlen_t, _: *mut R_allocator_t) -> SEXP;
#[no_mangle]
fn cons(_: SEXP, _: SEXP) -> SEXP;
#[no_mangle]
fn eval(_: SEXP, _: SEXP) -> SEXP;
#[no_mangle]
fn getAttrib(_: SEXP, _: SEXP) -> SEXP;
#[no_mangle]
fn install(_: *const libc::c_char) -> SEXP;
#[no_mangle]
fn mkChar(_: *const libc::c_char) -> SEXP;
#[no_mangle]
fn setAttrib(_: SEXP, _: SEXP, _: SEXP) -> SEXP;
#[no_mangle]
fn R_signal_protect_error() -> !;
#[no_mangle]
fn memcpy(_: *mut libc::c_void, _: *const libc::c_void, _: libc::c_ulong) -> *mut libc::c_void;
#[no_mangle]
fn strcmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
#[no_mangle]
static mut R_PPStackSize: libc::c_int;
#[no_mangle]
static mut R_PPStackTop: libc::c_int;
#[no_mangle]
static mut R_PPStack: *mut SEXP;
#[no_mangle]
fn envlength(rho: SEXP) -> libc::c_int;
#[no_mangle]
fn PrintDefaults();
#[no_mangle]
fn fdhess(
n: libc::c_int,
x: *mut libc::c_double,
fval: libc::c_double,
fun: fcn_p,
state: *mut libc::c_void,
h: *mut libc::c_double,
nfd: libc::c_int,
step: *mut libc::c_double,
f: *mut libc::c_double,
ndigit: libc::c_int,
typx: *mut libc::c_double,
);
#[no_mangle]
fn optif9(
nr: libc::c_int,
n: libc::c_int,
x: *mut libc::c_double,
fcn_0: fcn_p,
d1fcn: fcn_p,
d2fcn: d2fcn_p,
state: *mut libc::c_void,
typsiz: *mut libc::c_double,
fscale: libc::c_double,
method: libc::c_int,
iexp: libc::c_int,
msg: *mut libc::c_int,
ndigit: libc::c_int,
itnlim: libc::c_int,
iagflg: libc::c_int,
iahflg: libc::c_int,
dlt: libc::c_double,
gradtl: libc::c_double,
stepmx: libc::c_double,
steptl: libc::c_double,
xpls: *mut libc::c_double,
fpls: *mut libc::c_double,
gpls: *mut libc::c_double,
itrmcd: *mut libc::c_int,
a: *mut libc::c_double,
wrk: *mut libc::c_double,
itncnt: *mut libc::c_int,
);
#[no_mangle]
fn dcgettext(
__domainname: *const libc::c_char,
__msgid: *const libc::c_char,
__category: libc::c_int,
) -> *mut libc::c_char;
}
pub type size_t = libc::c_ulong;
pub type ptrdiff_t = libc::c_long;
pub type R_xlen_t = ptrdiff_t;
pub type Rboolean = libc::c_uint;
pub const TRUE: Rboolean = 1;
pub const FALSE: Rboolean = 0;
pub type R_len_t = libc::c_int;
pub type R_allocator_t = R_allocator;
pub type fcn_p = Option<
unsafe extern "C" fn(
_: libc::c_int,
_: *mut libc::c_double,
_: *mut libc::c_double,
_: *mut libc::c_void,
) -> (),
>;
pub type d2fcn_p = Option<
unsafe extern "C" fn(
_: libc::c_int,
_: libc::c_int,
_: *mut libc::c_double,
_: *mut libc::c_double,
_: *mut libc::c_void,
) -> (),
>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct callinfo {
pub R_fcall: SEXP,
pub R_env: SEXP,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct function_info {
pub R_fcall: SEXP,
pub R_env: SEXP,
pub have_gradient: libc::c_int,
pub have_hessian: libc::c_int,
pub FT_size: libc::c_int,
pub FT_last: libc::c_int,
pub Ftable: *mut ftable,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct ftable {
pub fval: libc::c_double,
pub x: *mut libc::c_double,
pub grad: *mut libc::c_double,
pub hess: *mut libc::c_double,
}
#[inline]
unsafe extern "C" fn DATAPTR(mut x: SEXP) -> *mut libc::c_void {
if (*x).sxpinfo.alt() != 0 {
return ALTVEC_DATAPTR(x);
} else {
return (x as *mut SEXPREC_ALIGN).offset(1 as libc::c_int as isize) as *mut libc::c_void;
};
}
#[inline]
unsafe extern "C" fn XLENGTH_EX(mut x: SEXP) -> R_xlen_t {
return if (*x).sxpinfo.alt() as libc::c_int != 0 {
ALTREP_LENGTH(x)
} else {
(*(x as VECSEXP)).vecsxp.length
};
}
#[inline]
unsafe extern "C" fn LENGTH_EX(
mut x: SEXP,
mut file: *const libc::c_char,
mut line: libc::c_int,
) -> libc::c_int {
if x == R_NilValue {
return 0 as libc::c_int;
}
let mut len: R_xlen_t = XLENGTH_EX(x);
if len > 2147483647 as libc::c_int as libc::c_long {
R_BadLongVector(x, file, line);
}
return len as libc::c_int;
}
#[inline]
unsafe extern "C" fn REAL0(mut x: SEXP) -> *mut libc::c_double {
return (x as *mut SEXPREC_ALIGN).offset(1 as libc::c_int as isize) as *mut libc::c_void
as *mut libc::c_double;
}
#[inline]
unsafe extern "C" fn SET_SCALAR_DVAL(mut x: SEXP, mut v: libc::c_double) {
*REAL0(x).offset(0 as libc::c_int as isize) = v;
}
#[inline]
unsafe extern "C" fn STRING_ELT(mut x: SEXP, mut i: R_xlen_t) -> SEXP {
if (*x).sxpinfo.alt() != 0 {
return ALTSTRING_ELT(x, i);
} else {
let mut ps: *mut SEXP = (x as *mut SEXPREC_ALIGN).offset(1 as libc::c_int as isize)
as *mut libc::c_void as *mut SEXP;
return *ps.offset(i as isize);
};
}
#[inline]
unsafe extern "C" fn protect(mut s: SEXP) -> SEXP {
if R_PPStackTop < R_PPStackSize {
let fresh0 = R_PPStackTop;
R_PPStackTop = R_PPStackTop + 1;
let ref mut fresh1 = *R_PPStack.offset(fresh0 as isize);
*fresh1 = s
} else {
R_signal_protect_error();
}
return s;
}
#[inline]
unsafe extern "C" fn unprotect(mut l: libc::c_int) {
R_PPStackTop -= l;
}
#[inline]
unsafe extern "C" fn length(mut s: SEXP) -> R_len_t {
match (*s).sxpinfo.type_0() as libc::c_int {
0 => return 0 as libc::c_int,
10 | 13 | 14 | 15 | 16 | 9 | 19 | 20 | 24 => {
return LENGTH_EX(
s,
b"../../../include/Rinlinedfuns.h\x00" as *const u8 as *const libc::c_char,
522 as libc::c_int,
)
}
2 | 6 | 17 => {
let mut i: libc::c_int = 0 as libc::c_int;
while !s.is_null() && s != R_NilValue {
i += 1;
s = (*s).u.listsxp.cdrval
}
return i;
}
4 => return envlength(s),
_ => return 1 as libc::c_int,
};
}
#[inline]
unsafe extern "C" fn allocVector(mut type_0: SEXPTYPE, mut length: R_xlen_t) -> SEXP {
return allocVector3(type_0, length, 0 as *mut R_allocator_t);
}
#[inline]
unsafe extern "C" fn list1(mut s: SEXP) -> SEXP {
return cons(s, R_NilValue);
}
#[inline]
unsafe extern "C" fn lcons(mut car: SEXP, mut cdr: SEXP) -> SEXP {
let mut e: SEXP = cons(car, cdr);
(*e).sxpinfo.set_type_0(6 as libc::c_int as SEXPTYPE);
return e;
}
#[inline]
unsafe extern "C" fn lang2(mut s: SEXP, mut t: SEXP) -> SEXP {
protect(s);
s = lcons(s, list1(t));
unprotect(1 as libc::c_int);
return s;
}
#[inline]
unsafe extern "C" fn inherits(mut s: SEXP, mut name: *const libc::c_char) -> Rboolean {
let mut klass: SEXP = 0 as *mut SEXPREC;
let mut i: libc::c_int = 0;
let mut nclass: libc::c_int = 0;
if (*s).sxpinfo.obj() != 0 {
klass = getAttrib(s, R_ClassSymbol);
nclass = length(klass);
i = 0 as libc::c_int;
while i < nclass {
if strcmp(
(STRING_ELT(klass, i as R_xlen_t) as *mut SEXPREC_ALIGN)
.offset(1 as libc::c_int as isize) as *mut libc::c_void
as *const libc::c_char,
name,
) == 0
{
return TRUE;
}
i += 1
}
}
return FALSE;
}
#[inline]
unsafe extern "C" fn isFunction(mut s: SEXP) -> Rboolean {
return ((*s).sxpinfo.type_0() as libc::c_int == 3 as libc::c_int
|| (*s).sxpinfo.type_0() as libc::c_int == 8 as libc::c_int
|| (*s).sxpinfo.type_0() as libc::c_int == 7 as libc::c_int) as libc::c_int
as Rboolean;
}
#[inline]
unsafe extern "C" fn isInteger(mut s: SEXP) -> Rboolean {
return ((*s).sxpinfo.type_0() as libc::c_int == 13 as libc::c_int
&& inherits(s, b"factor\x00" as *const u8 as *const libc::c_char) as u64 == 0)
as libc::c_int as Rboolean;
}
#[inline]
unsafe extern "C" fn isNumeric(mut s: SEXP) -> Rboolean {
match (*s).sxpinfo.type_0() as libc::c_int {
13 => {
if inherits(s, b"factor\x00" as *const u8 as *const libc::c_char) as u64 != 0 {
return FALSE;
}
}
10 | 14 => {}
_ => return FALSE,
}
return TRUE;
}
#[inline]
unsafe extern "C" fn ScalarReal(mut x: libc::c_double) -> SEXP {
let mut ans: SEXP = allocVector(14 as libc::c_int as SEXPTYPE, 1 as libc::c_int as R_xlen_t);
SET_SCALAR_DVAL(ans, x);
return ans;
}
unsafe extern "C" fn Brent_fmin(
mut ax: libc::c_double,
mut bx: libc::c_double,
mut f: Option<unsafe extern "C" fn(_: libc::c_double, _: *mut libc::c_void) -> libc::c_double>,
mut info: *mut libc::c_void,
mut tol: libc::c_double,
) -> libc::c_double {
let c: libc::c_double = (3.0f64 - sqrt(5.0f64)) * 0.5f64;
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 t2: 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 xm: libc::c_double = 0.;
let mut eps: libc::c_double = 0.;
let mut tol1: libc::c_double = 0.;
let mut tol3: libc::c_double = 0.;
eps = 2.2204460492503131e-16f64;
tol1 = eps + 1.0f64;
eps = sqrt(eps);
a = ax;
b = bx;
v = a + c * (b - a);
w = v;
x = v;
d = 0.0f64;
e = 0.0f64;
fx = Some(f.expect("non-null function pointer")).expect("non-null function pointer")(x, info);
fv = fx;
fw = fx;
tol3 = tol / 3.0f64;
loop
{
xm = (a + b) * 0.5f64;
tol1 = eps * x.abs() + tol3;
t2 = tol1 * 2.0f64;
if (x - xm).abs() <= t2 - (b - a) * 0.5f64 {
break;
}
p = 0.0f64;
q = 0.0f64;
r = 0.0f64;
if e.abs() > tol1 {
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
} else {
q = -q
}
r = e;
e = d
}
if p.abs() >= (q * 0.5f64 * r).abs() || p <= q * (a - x) || p >= q * (b - x) {
if x < xm {
e = b - x
} else {
e = a - x
}
d = c * e
} else {
d = p / q;
u = x + d;
if u - a < t2 || b - u < t2 {
d = tol1;
if x >= xm {
d = -d
}
}
}
if d.abs() >= tol1 {
u = x + d
} else if d > 0.0f64 {
u = x + tol1
} else {
u = x - tol1
}
fu = Some(f.expect("non-null function pointer")).expect("non-null function pointer")(
u, info,
);
if fu <= fx {
if u < x {
b = x
} else {
a = x
}
v = w;
w = x;
x = u;
fv = fw;
fw = fx;
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
}
}
}
return x;
}
unsafe extern "C" fn fcn1(mut x: libc::c_double, mut info: *mut callinfo) -> libc::c_double {
let mut s: SEXP = 0 as *mut SEXPREC;
let mut sx: SEXP = 0 as *mut SEXPREC;
sx = ScalarReal(x);
protect(sx);
SETCADR((*info).R_fcall, sx);
s = eval((*info).R_fcall, (*info).R_env);
unprotect(1 as libc::c_int);
match (*s).sxpinfo.type_0() as libc::c_int {
13 => {
if !(length(s) != 1 as libc::c_int) {
if *(DATAPTR(s) as *mut libc::c_int).offset(0 as libc::c_int as isize) == R_NaInt {
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA replaced by maximum positive value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
return 1.7976931348623157e+308f64;
} else {
return *(DATAPTR(s) as *mut libc::c_int).offset(0 as libc::c_int as isize)
as libc::c_double;
}
}
}
14 => {
if !(length(s) != 1 as libc::c_int) {
if (*(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize))
.is_finite() as i32
== 0
{
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA/Inf replaced by maximum positive value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
return 1.7976931348623157e+308f64;
} else {
return *(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize);
}
}
}
_ => {}
}
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid function value in \'optimize\'\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
#[no_mangle]
pub unsafe extern "C" fn do_fmin(
mut _call: SEXP,
mut _op: SEXP,
mut args: SEXP,
mut rho: SEXP,
) -> SEXP {
let mut xmin: libc::c_double = 0.;
let mut xmax: libc::c_double = 0.;
let mut tol: libc::c_double = 0.;
let mut v: SEXP = 0 as *mut SEXPREC;
let mut res: SEXP = 0 as *mut SEXPREC;
let mut info: callinfo = callinfo {
R_fcall: 0 as *mut SEXPREC,
R_env: 0 as *mut SEXPREC,
};
args = (*args).u.listsxp.cdrval;
PrintDefaults();
v = (*args).u.listsxp.carval;
if isFunction(v) as u64 == 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"attempt to minimize non-function\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
xmin = asReal((*args).u.listsxp.carval);
if xmin.is_finite() as i32 == 0 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid \'%s\' value\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"xmin\x00" as *const u8 as *const libc::c_char,
);
}
args = (*args).u.listsxp.cdrval;
xmax = asReal((*args).u.listsxp.carval);
if xmax.is_finite() as i32 == 0 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid \'%s\' value\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"xmax\x00" as *const u8 as *const libc::c_char,
);
}
if xmin >= xmax {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"\'xmin\' not less than \'xmax\'\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
tol = asReal((*args).u.listsxp.carval);
if tol.is_finite() as i32 == 0 || tol <= 0.0f64 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid \'%s\' value\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"tol\x00" as *const u8 as *const libc::c_char,
);
}
info.R_env = rho;
info.R_fcall = lang2(v, R_NilValue);
protect(info.R_fcall);
res = allocVector(14 as libc::c_int as SEXPTYPE, 1 as libc::c_int as R_xlen_t);
protect(res);
*(DATAPTR(res) as *mut libc::c_double).offset(0 as libc::c_int as isize) = Brent_fmin(
xmin,
xmax,
::std::mem::transmute::<
Option<unsafe extern "C" fn(_: libc::c_double, _: *mut callinfo) -> libc::c_double>,
Option<unsafe extern "C" fn(_: libc::c_double, _: *mut libc::c_void) -> libc::c_double>,
>(Some(
fcn1 as unsafe extern "C" fn(_: libc::c_double, _: *mut callinfo) -> libc::c_double,
)),
&mut info as *mut callinfo as *mut libc::c_void,
tol,
);
unprotect(2 as libc::c_int);
return res;
}
unsafe extern "C" fn fcn2(mut x: libc::c_double, mut info: *mut callinfo) -> libc::c_double {
let mut s: SEXP = 0 as *mut SEXPREC;
let mut sx: SEXP = 0 as *mut SEXPREC;
sx = ScalarReal(x);
protect(sx);
SETCADR((*info).R_fcall, sx);
s = eval((*info).R_fcall, (*info).R_env);
unprotect(1 as libc::c_int);
match (*s).sxpinfo.type_0() as libc::c_int {
13 => {
if !(length(s) != 1 as libc::c_int) {
if *(DATAPTR(s) as *mut libc::c_int).offset(0 as libc::c_int as isize) == R_NaInt {
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA replaced by maximum positive value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
return 1.7976931348623157e+308f64;
} else {
return *(DATAPTR(s) as *mut libc::c_int).offset(0 as libc::c_int as isize)
as libc::c_double;
}
}
}
14 => {
if !(length(s) != 1 as libc::c_int) {
if (*(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize))
.is_finite() as i32
== 0
{
if *(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize)
== R_NegInf
{
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"-Inf replaced by maximally negative value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
return -1.7976931348623157e+308f64;
} else {
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA/Inf replaced by maximum positive value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
return 1.7976931348623157e+308f64;
}
} else {
return *(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize);
}
}
}
_ => {}
}
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid function value in \'zeroin\'\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
#[no_mangle]
pub unsafe extern "C" fn zeroin2(
mut _call: SEXP,
mut _op: SEXP,
mut args: SEXP,
mut rho: SEXP,
) -> SEXP {
let mut f_ax: libc::c_double = 0.;
let mut f_bx: libc::c_double = 0.;
let mut xmin: libc::c_double = 0.;
let mut xmax: libc::c_double = 0.;
let mut tol: libc::c_double = 0.;
let mut iter: libc::c_int = 0;
let mut v: SEXP = 0 as *mut SEXPREC;
let mut res: SEXP = 0 as *mut SEXPREC;
let mut info: callinfo = callinfo {
R_fcall: 0 as *mut SEXPREC,
R_env: 0 as *mut SEXPREC,
};
args = (*args).u.listsxp.cdrval;
PrintDefaults();
v = (*args).u.listsxp.carval;
if isFunction(v) as u64 == 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"attempt to minimize non-function\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
xmin = asReal((*args).u.listsxp.carval);
if xmin.is_finite() as i32 == 0 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid \'%s\' value\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"xmin\x00" as *const u8 as *const libc::c_char,
);
}
args = (*args).u.listsxp.cdrval;
xmax = asReal((*args).u.listsxp.carval);
if xmax.is_finite() as i32 == 0 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid \'%s\' value\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"xmax\x00" as *const u8 as *const libc::c_char,
);
}
if xmin >= xmax {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"\'xmin\' not less than \'xmax\'\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
f_ax = asReal((*args).u.listsxp.carval);
if R_IsNA(f_ax) != 0 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA value for \'%s\' is not allowed\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"f.lower\x00" as *const u8 as *const libc::c_char,
);
}
args = (*args).u.listsxp.cdrval;
f_bx = asReal((*args).u.listsxp.carval);
if R_IsNA(f_bx) != 0 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA value for \'%s\' is not allowed\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"f.upper\x00" as *const u8 as *const libc::c_char,
);
}
args = (*args).u.listsxp.cdrval;
tol = asReal((*args).u.listsxp.carval);
if tol.is_finite() as i32 == 0 || tol <= 0.0f64 {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid \'%s\' value\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
b"tol\x00" as *const u8 as *const libc::c_char,
);
}
args = (*args).u.listsxp.cdrval;
iter = asInteger((*args).u.listsxp.carval);
if iter <= 0 as libc::c_int {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"\'maxiter\' must be positive\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
info.R_env = rho;
info.R_fcall = lang2(v, R_NilValue);
protect(info.R_fcall);
res = allocVector(14 as libc::c_int as SEXPTYPE, 3 as libc::c_int as R_xlen_t);
protect(res);
*(DATAPTR(res) as *mut libc::c_double).offset(0 as libc::c_int as isize) = R_zeroin2(
xmin,
xmax,
f_ax,
f_bx,
::std::mem::transmute::<
Option<unsafe extern "C" fn(_: libc::c_double, _: *mut callinfo) -> libc::c_double>,
Option<unsafe extern "C" fn(_: libc::c_double, _: *mut libc::c_void) -> libc::c_double>,
>(Some(
fcn2 as unsafe extern "C" fn(_: libc::c_double, _: *mut callinfo) -> libc::c_double,
)),
&mut info as *mut callinfo as *mut libc::c_void,
&mut tol,
&mut iter,
);
*(DATAPTR(res) as *mut libc::c_double).offset(1 as libc::c_int as isize) =
iter as libc::c_double;
*(DATAPTR(res) as *mut libc::c_double).offset(2 as libc::c_int as isize) = tol;
unprotect(2 as libc::c_int);
return res;
}
unsafe extern "C" fn FT_init(
mut n: libc::c_int,
mut FT_size: libc::c_int,
mut state: *mut function_info,
) {
let mut i: libc::c_int = 0;
let mut j: libc::c_int = 0;
let mut have_gradient: libc::c_int = 0;
let mut have_hessian: libc::c_int = 0;
let mut Ftable: *mut ftable = 0 as *mut ftable;
have_gradient = (*state).have_gradient;
have_hessian = (*state).have_hessian;
Ftable = R_alloc(
FT_size as size_t,
::std::mem::size_of::<ftable>() as libc::c_ulong as libc::c_int,
) as *mut ftable;
i = 0 as libc::c_int;
while i < FT_size {
let ref mut fresh2 = (*Ftable.offset(i as isize)).x;
*fresh2 = R_alloc(
n as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
j = 0 as libc::c_int;
while j < n {
*(*Ftable.offset(i as isize)).x.offset(j as isize) = 1.7976931348623157e+308f64;
j += 1
}
if have_gradient != 0 {
let ref mut fresh3 = (*Ftable.offset(i as isize)).grad;
*fresh3 = R_alloc(
n as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
if have_hessian != 0 {
let ref mut fresh4 = (*Ftable.offset(i as isize)).hess;
*fresh4 = R_alloc(
(n * n) as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double
}
}
i += 1
}
(*state).Ftable = Ftable;
(*state).FT_size = FT_size;
(*state).FT_last = -(1 as libc::c_int);
}
unsafe extern "C" fn FT_store(
mut n: libc::c_int,
f: libc::c_double,
mut x: *const libc::c_double,
mut grad: *const libc::c_double,
mut hess: *const libc::c_double,
mut state: *mut function_info,
) {
let mut ind: libc::c_int = 0;
(*state).FT_last += 1;
ind = (*state).FT_last % (*state).FT_size;
(*(*state).Ftable.offset(ind as isize)).fval = f;
memcpy(
(*(*state).Ftable.offset(ind as isize)).x as *mut libc::c_void,
x as *const libc::c_void,
(n as size_t).wrapping_mul(::std::mem::size_of::<libc::c_double>() as libc::c_ulong),
);
if !grad.is_null() {
memcpy(
(*(*state).Ftable.offset(ind as isize)).grad as *mut libc::c_void,
grad as *const libc::c_void,
(n as size_t).wrapping_mul(::std::mem::size_of::<libc::c_double>() as libc::c_ulong),
);
if !hess.is_null() {
memcpy(
(*(*state).Ftable.offset(ind as isize)).hess as *mut libc::c_void,
hess as *const libc::c_void,
((n * n) as size_t)
.wrapping_mul(::std::mem::size_of::<libc::c_double>() as libc::c_ulong),
);
}
};
}
unsafe extern "C" fn FT_lookup(
mut n: libc::c_int,
mut x: *const libc::c_double,
mut state: *mut function_info,
) -> libc::c_int {
let mut ftx: *mut libc::c_double = 0 as *mut libc::c_double;
let mut i: libc::c_int = 0;
let mut j: libc::c_int = 0;
let mut ind: libc::c_int = 0;
let mut matched: libc::c_int = 0;
let mut FT_size: libc::c_int = 0;
let mut FT_last: libc::c_int = 0;
let mut Ftable: *mut ftable = 0 as *mut ftable;
FT_last = (*state).FT_last;
FT_size = (*state).FT_size;
Ftable = (*state).Ftable;
i = 0 as libc::c_int;
while i < FT_size {
ind = (FT_last - i) % FT_size;
if ind < 0 as libc::c_int {
ind += FT_size
}
ftx = (*Ftable.offset(ind as isize)).x;
if !ftx.is_null() {
matched = 1 as libc::c_int;
j = 0 as libc::c_int;
while j < n {
if *x.offset(j as isize) != *ftx.offset(j as isize) {
matched = 0 as libc::c_int;
break;
} else {
j += 1
}
}
if matched != 0 {
return ind;
}
}
i += 1
}
return -(1 as libc::c_int);
}
unsafe extern "C" fn fcn(
mut n: libc::c_int,
mut x: *const libc::c_double,
mut f: *mut libc::c_double,
mut state: *mut function_info,
) {
let mut current_block: u64;
let mut s: SEXP = 0 as *mut SEXPREC;
let mut R_fcall: SEXP = 0 as *mut SEXPREC;
let mut Ftable: *mut ftable = 0 as *mut ftable;
let mut g: *mut libc::c_double = 0 as *mut libc::c_double;
let mut h: *mut libc::c_double = 0 as *mut libc::c_double;
let mut i: libc::c_int = 0;
R_fcall = (*state).R_fcall;
Ftable = (*state).Ftable;
i = FT_lookup(n, x, state);
if i >= 0 as libc::c_int {
*f = (*Ftable.offset(i as isize)).fval;
return;
}
s = allocVector(14 as libc::c_int as SEXPTYPE, n as R_xlen_t);
SETCADR(R_fcall, s);
i = 0 as libc::c_int;
while i < n {
if (*x.offset(i as isize)).is_finite() as i32 == 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"non-finite value supplied by \'nlm\'\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
*(DATAPTR(s) as *mut libc::c_double).offset(i as isize) = *x.offset(i as isize);
i += 1
}
s = protect(eval((*state).R_fcall, (*state).R_env));
match (*s).sxpinfo.type_0() as libc::c_int {
13 => {
if length(s) != 1 as libc::c_int {
current_block = 11860839946601367817;
} else {
if *(DATAPTR(s) as *mut libc::c_int).offset(0 as libc::c_int as isize) == R_NaInt {
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA replaced by maximum positive value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
*f = 1.7976931348623157e+308f64
} else {
*f = *(DATAPTR(s) as *mut libc::c_int).offset(0 as libc::c_int as isize)
as libc::c_double
}
current_block = 5689316957504528238;
}
}
14 => {
if length(s) != 1 as libc::c_int {
current_block = 11860839946601367817;
} else {
if (*(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize))
.is_finite() as i32
== 0
{
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"NA/Inf replaced by maximum positive value\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
*f = 1.7976931348623157e+308f64
} else {
*f = *(DATAPTR(s) as *mut libc::c_double).offset(0 as libc::c_int as isize)
}
current_block = 5689316957504528238;
}
}
_ => {
current_block = 11860839946601367817;
}
}
match current_block {
11860839946601367817 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid function value in \'nlm\' optimizer\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
_ => {
if (*state).have_gradient != 0 {
g = DATAPTR(protect(coerceVector(
getAttrib(
s,
install(b"gradient\x00" as *const u8 as *const libc::c_char),
),
14 as libc::c_int as SEXPTYPE,
))) as *mut libc::c_double;
if (*state).have_hessian != 0 {
h = DATAPTR(protect(coerceVector(
getAttrib(
s,
install(b"hessian\x00" as *const u8 as *const libc::c_char),
),
14 as libc::c_int as SEXPTYPE,
))) as *mut libc::c_double
}
}
FT_store(n, *f, x, g, h, state);
unprotect(1 as libc::c_int + (*state).have_gradient + (*state).have_hessian);
return;
}
};
}
unsafe extern "C" fn Cd1fcn(
mut n: libc::c_int,
mut x: *const libc::c_double,
mut g: *mut libc::c_double,
mut state: *mut function_info,
) {
let mut ind: libc::c_int = 0;
ind = FT_lookup(n, x, state);
if ind < 0 as libc::c_int {
fcn(n, x, g, state);
ind = FT_lookup(n, x, state);
if ind < 0 as libc::c_int {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"function value caching for optimization is seriously confused\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
}
memcpy(
g as *mut libc::c_void,
(*(*state).Ftable.offset(ind as isize)).grad as *const libc::c_void,
(n as size_t).wrapping_mul(::std::mem::size_of::<libc::c_double>() as libc::c_ulong),
);
}
unsafe extern "C" fn Cd2fcn(
mut _nr: libc::c_int,
mut n: libc::c_int,
mut x: *const libc::c_double,
mut h: *mut libc::c_double,
mut state: *mut function_info,
) {
let mut j: libc::c_int = 0;
let mut ind: libc::c_int = 0;
ind = FT_lookup(n, x, state);
if ind < 0 as libc::c_int {
fcn(n, x, h, state);
ind = FT_lookup(n, x, state);
if ind < 0 as libc::c_int {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"function value caching for optimization is seriously confused\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
}
j = 0 as libc::c_int;
while j < n {
memcpy(
h.offset((j * (n + 1 as libc::c_int)) as isize) as *mut libc::c_void,
(*(*state).Ftable.offset(ind as isize))
.hess
.offset((j * (n + 1 as libc::c_int)) as isize) as *const libc::c_void,
((n - j) as size_t)
.wrapping_mul(::std::mem::size_of::<libc::c_double>() as libc::c_ulong),
);
j += 1
}
}
unsafe extern "C" fn fixparam(mut p: SEXP, mut n: *mut libc::c_int) -> *mut libc::c_double {
let mut x: *mut libc::c_double = 0 as *mut libc::c_double;
let mut i: libc::c_int = 0;
if isNumeric(p) as u64 == 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"numeric parameter expected\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
if *n != 0 {
if LENGTH_EX(
p,
b"optimize.c\x00" as *const u8 as *const libc::c_char,
599 as libc::c_int,
) != *n
{
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"conflicting parameter lengths\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
} else {
if LENGTH_EX(
p,
b"optimize.c\x00" as *const u8 as *const libc::c_char,
603 as libc::c_int,
) <= 0 as libc::c_int
{
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid parameter length\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
*n = LENGTH_EX(
p,
b"optimize.c\x00" as *const u8 as *const libc::c_char,
605 as libc::c_int,
)
}
x = R_alloc(
*n as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
match (*p).sxpinfo.type_0() as libc::c_int {
10 | 13 => {
i = 0 as libc::c_int;
while i < *n {
if *(DATAPTR(p) as *mut libc::c_int).offset(i as isize) == R_NaInt {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"missing value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
*x.offset(i as isize) =
*(DATAPTR(p) as *mut libc::c_int).offset(i as isize) as libc::c_double;
i += 1
}
}
14 => {
i = 0 as libc::c_int;
while i < *n {
if (*(DATAPTR(p) as *mut libc::c_double).offset(i as isize)).is_finite() as i32 == 0
{
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"missing value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
*x.offset(i as isize) = *(DATAPTR(p) as *mut libc::c_double).offset(i as isize);
i += 1
}
}
_ => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid parameter type\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
}
return x;
}
unsafe extern "C" fn opterror(mut nerr: libc::c_int) -> ! {
match nerr {
-1 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"non-positive number of parameters in nlm\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
-2 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"nlm is inefficient for 1-d problems\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
-3 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid gradient tolerance in nlm\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
-4 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid iteration limit in nlm\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
-5 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"minimization function has no good digits in nlm\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
-6 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"no analytic gradient to check in nlm!\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
-7 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"no analytic Hessian to check in nlm!\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
-21 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"probable coding error in analytic gradient\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
-22 => {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"probable coding error in analytic Hessian\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
_ => {
error(
dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"*** unknown error message (msg = %d) in nlm()\n*** should not happen!\x00"
as *const u8 as *const libc::c_char,
5 as libc::c_int,
),
nerr,
);
}
};
}
unsafe extern "C" fn optcode(mut code: libc::c_int) {
match code {
1 => {
Rprintf(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"Relative gradient close to zero.\n\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
Rprintf(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"Current iterate is probably solution.\n\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
2 => {
Rprintf(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"Successive iterates within tolerance.\n\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
Rprintf(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"Current iterate is probably solution.\n\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
3 => {
Rprintf(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"Last global step failed to locate a point lower than x.\n\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
Rprintf(dcgettext(b"stats\x00" as *const u8 as
*const libc::c_char,
b"Either x is an approximate local minimum of the function,\nthe function is too non-linear for this algorithm,\nor steptol is too large.\n\x00"
as *const u8 as *const libc::c_char,
5 as libc::c_int));
}
4 => {
Rprintf(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"Iteration limit exceeded. Algorithm failed.\n\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
5 => {
Rprintf(dcgettext(b"stats\x00" as *const u8 as
*const libc::c_char,
b"Maximum step size exceeded 5 consecutive times.\nEither the function is unbounded below,\nbecomes asymptotic to a finite value\nfrom above in some direction,\nor stepmx is too small.\n\x00"
as *const u8 as *const libc::c_char,
5 as libc::c_int));
}
_ => {}
}
Rprintf(b"\n\x00" as *const u8 as *const libc::c_char);
}
#[no_mangle]
pub unsafe extern "C" fn nlm(
mut _call: SEXP,
mut _op: SEXP,
mut args: SEXP,
mut rho: SEXP,
) -> SEXP {
let mut value: SEXP = 0 as *mut SEXPREC;
let mut names: SEXP = 0 as *mut SEXPREC;
let mut v: SEXP = 0 as *mut SEXPREC;
let mut R_gradientSymbol: SEXP = 0 as *mut SEXPREC;
let mut R_hessianSymbol: SEXP = 0 as *mut SEXPREC;
let mut x: *mut libc::c_double = 0 as *mut libc::c_double;
let mut typsiz: *mut libc::c_double = 0 as *mut libc::c_double;
let mut fscale: libc::c_double = 0.;
let mut gradtl: libc::c_double = 0.;
let mut stepmx: libc::c_double = 0.;
let mut steptol: libc::c_double = 0.;
let mut xpls: *mut libc::c_double = 0 as *mut libc::c_double;
let mut gpls: *mut libc::c_double = 0 as *mut libc::c_double;
let mut fpls: libc::c_double = 0.;
let mut a: *mut libc::c_double = 0 as *mut libc::c_double;
let mut wrk: *mut libc::c_double = 0 as *mut libc::c_double;
let mut dlt: libc::c_double = 0.;
let mut code: libc::c_int = 0;
let mut i: libc::c_int = 0;
let mut j: libc::c_int = 0;
let mut k: libc::c_int = 0;
let mut itnlim: libc::c_int = 0;
let mut method: libc::c_int = 0;
let mut iexp: libc::c_int = 0;
let mut omsg: libc::c_int = 0;
let mut msg: libc::c_int = 0;
let mut n: libc::c_int = 0;
let mut ndigit: libc::c_int = 0;
let mut iagflg: libc::c_int = 0;
let mut iahflg: libc::c_int = 0;
let mut want_hessian: libc::c_int = 0;
let mut itncnt: libc::c_int = 0;
let mut state: *mut function_info = 0 as *mut function_info;
args = (*args).u.listsxp.cdrval;
PrintDefaults();
state = R_alloc(
1 as libc::c_int as size_t,
::std::mem::size_of::<function_info>() as libc::c_ulong as libc::c_int,
) as *mut function_info;
v = (*args).u.listsxp.carval;
if isFunction(v) as u64 == 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"attempt to minimize non-function\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
(*state).R_fcall = lang2(v, R_NilValue);
protect((*state).R_fcall);
args = (*args).u.listsxp.cdrval;
n = 0 as libc::c_int;
x = fixparam((*args).u.listsxp.carval, &mut n);
args = (*args).u.listsxp.cdrval;
want_hessian = asLogical((*args).u.listsxp.carval);
if want_hessian == R_NaInt {
want_hessian = 0 as libc::c_int
}
args = (*args).u.listsxp.cdrval;
typsiz = fixparam((*args).u.listsxp.carval, &mut n);
args = (*args).u.listsxp.cdrval;
fscale = asReal((*args).u.listsxp.carval);
if R_IsNA(fscale) != 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
msg = asInteger((*args).u.listsxp.carval);
omsg = msg;
if msg == R_NaInt {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
ndigit = asInteger((*args).u.listsxp.carval);
if ndigit == R_NaInt {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
gradtl = asReal((*args).u.listsxp.carval);
if R_IsNA(gradtl) != 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
stepmx = asReal((*args).u.listsxp.carval);
if R_IsNA(stepmx) != 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
steptol = asReal((*args).u.listsxp.carval);
if R_IsNA(steptol) != 0 {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
args = (*args).u.listsxp.cdrval;
itnlim = asInteger((*args).u.listsxp.carval);
if itnlim == R_NaInt {
error(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"invalid NA value in parameter\x00" as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
(*state).R_env = rho;
iagflg = 0 as libc::c_int;
iahflg = 0 as libc::c_int;
(*state).have_gradient = 0 as libc::c_int;
(*state).have_hessian = 0 as libc::c_int;
R_gradientSymbol = install(b"gradient\x00" as *const u8 as *const libc::c_char);
R_hessianSymbol = install(b"hessian\x00" as *const u8 as *const libc::c_char);
v = allocVector(14 as libc::c_int as SEXPTYPE, n as R_xlen_t);
i = 0 as libc::c_int;
while i < n {
*(DATAPTR(v) as *mut libc::c_double).offset(i as isize) = *x.offset(i as isize);
i += 1
}
SETCADR((*state).R_fcall, v);
value = eval((*state).R_fcall, (*state).R_env);
protect(value);
v = getAttrib(value, R_gradientSymbol);
if v != R_NilValue {
if LENGTH_EX(
v,
b"optimize.c\x00" as *const u8 as *const libc::c_char,
790 as libc::c_int,
) == n
&& ((*v).sxpinfo.type_0() as libc::c_int == 14 as libc::c_int
|| isInteger(v) as libc::c_uint != 0)
{
iagflg = 1 as libc::c_int;
(*state).have_gradient = 1 as libc::c_int;
v = getAttrib(value, R_hessianSymbol);
if v != R_NilValue {
if LENGTH_EX(
v,
b"optimize.c\x00" as *const u8 as *const libc::c_char,
796 as libc::c_int,
) == n * n
&& ((*v).sxpinfo.type_0() as libc::c_int == 14 as libc::c_int
|| isInteger(v) as libc::c_uint != 0)
{
iahflg = 1 as libc::c_int;
(*state).have_hessian = 1 as libc::c_int
} else {
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"hessian supplied is of the wrong length or mode, so ignored\x00"
as *const u8 as *const libc::c_char,
5 as libc::c_int,
));
}
}
} else {
warning(dcgettext(
b"stats\x00" as *const u8 as *const libc::c_char,
b"gradient supplied is of the wrong length or mode, so ignored\x00" as *const u8
as *const libc::c_char,
5 as libc::c_int,
));
}
}
unprotect(1 as libc::c_int);
if msg / 4 as libc::c_int % 2 as libc::c_int != 0 && iahflg == 0 {
msg -= 4 as libc::c_int
}
if msg / 2 as libc::c_int % 2 as libc::c_int != 0 && iagflg == 0 {
msg -= 2 as libc::c_int
}
FT_init(n, 5 as libc::c_int, state);
method = 1 as libc::c_int;
iexp = if iahflg != 0 {
0 as libc::c_int
} else {
1 as libc::c_int
};
dlt = 1.0f64;
xpls = R_alloc(
n as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
gpls = R_alloc(
n as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
a = R_alloc(
(n * n) as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
wrk = R_alloc(
(8 as libc::c_int * n) as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
optif9(
n,
n,
x,
::std::mem::transmute::<
Option<
unsafe extern "C" fn(
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
>,
fcn_p,
>(Some(
fcn as unsafe extern "C" fn(
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
)),
::std::mem::transmute::<
Option<
unsafe extern "C" fn(
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
>,
fcn_p,
>(Some(
Cd1fcn
as unsafe extern "C" fn(
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
)),
::std::mem::transmute::<
Option<
unsafe extern "C" fn(
_: libc::c_int,
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
>,
d2fcn_p,
>(Some(
Cd2fcn
as unsafe extern "C" fn(
_: libc::c_int,
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
)),
state as *mut libc::c_void,
typsiz,
fscale,
method,
iexp,
&mut msg,
ndigit,
itnlim,
iagflg,
iahflg,
dlt,
gradtl,
stepmx,
steptol,
xpls,
&mut fpls,
gpls,
&mut code,
a,
wrk,
&mut itncnt,
);
if msg < 0 as libc::c_int {
opterror(msg);
}
if code != 0 as libc::c_int && omsg & 8 as libc::c_int == 0 as libc::c_int {
optcode(code);
}
if want_hessian != 0 {
value = allocVector(19 as libc::c_int as SEXPTYPE, 6 as libc::c_int as R_xlen_t);
protect(value);
names = allocVector(16 as libc::c_int as SEXPTYPE, 6 as libc::c_int as R_xlen_t);
protect(names);
fdhess(
n,
xpls,
fpls,
::std::mem::transmute::<
Option<
unsafe extern "C" fn(
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
>,
fcn_p,
>(Some(
fcn as unsafe extern "C" fn(
_: libc::c_int,
_: *const libc::c_double,
_: *mut libc::c_double,
_: *mut function_info,
) -> (),
)),
state as *mut libc::c_void,
a,
n,
&mut *wrk.offset(0 as libc::c_int as isize),
&mut *wrk.offset(n as isize),
ndigit,
typsiz,
);
i = 0 as libc::c_int;
while i < n {
j = 0 as libc::c_int;
while j < i {
*a.offset((i + j * n) as isize) = *a.offset((j + i * n) as isize);
j += 1
}
i += 1
}
} else {
value = allocVector(19 as libc::c_int as SEXPTYPE, 5 as libc::c_int as R_xlen_t);
protect(value);
names = allocVector(16 as libc::c_int as SEXPTYPE, 5 as libc::c_int as R_xlen_t);
protect(names);
}
k = 0 as libc::c_int;
SET_STRING_ELT(
names,
k as R_xlen_t,
mkChar(b"minimum\x00" as *const u8 as *const libc::c_char),
);
SET_VECTOR_ELT(value, k as R_xlen_t, ScalarReal(fpls));
k += 1;
SET_STRING_ELT(
names,
k as R_xlen_t,
mkChar(b"estimate\x00" as *const u8 as *const libc::c_char),
);
SET_VECTOR_ELT(
value,
k as R_xlen_t,
allocVector(14 as libc::c_int as SEXPTYPE, n as R_xlen_t),
);
i = 0 as libc::c_int;
while i < n {
*(DATAPTR(*(DATAPTR(value) as *mut SEXP).offset(k as isize)) as *mut libc::c_double)
.offset(i as isize) = *xpls.offset(i as isize);
i += 1
}
k += 1;
SET_STRING_ELT(
names,
k as R_xlen_t,
mkChar(b"gradient\x00" as *const u8 as *const libc::c_char),
);
SET_VECTOR_ELT(
value,
k as R_xlen_t,
allocVector(14 as libc::c_int as SEXPTYPE, n as R_xlen_t),
);
i = 0 as libc::c_int;
while i < n {
*(DATAPTR(*(DATAPTR(value) as *mut SEXP).offset(k as isize)) as *mut libc::c_double)
.offset(i as isize) = *gpls.offset(i as isize);
i += 1
}
k += 1;
if want_hessian != 0 {
SET_STRING_ELT(
names,
k as R_xlen_t,
mkChar(b"hessian\x00" as *const u8 as *const libc::c_char),
);
SET_VECTOR_ELT(
value,
k as R_xlen_t,
allocMatrix(14 as libc::c_int as SEXPTYPE, n, n),
);
i = 0 as libc::c_int;
while i < n * n {
*(DATAPTR(*(DATAPTR(value) as *mut SEXP).offset(k as isize)) as *mut libc::c_double)
.offset(i as isize) = *a.offset(i as isize);
i += 1
}
k += 1
}
SET_STRING_ELT(
names,
k as R_xlen_t,
mkChar(b"code\x00" as *const u8 as *const libc::c_char),
);
SET_VECTOR_ELT(
value,
k as R_xlen_t,
allocVector(13 as libc::c_int as SEXPTYPE, 1 as libc::c_int as R_xlen_t),
);
*(DATAPTR(*(DATAPTR(value) as *mut SEXP).offset(k as isize)) as *mut libc::c_int)
.offset(0 as libc::c_int as isize) = code;
k += 1;
SET_STRING_ELT(
names,
k as R_xlen_t,
mkChar(b"iterations\x00" as *const u8 as *const libc::c_char),
);
SET_VECTOR_ELT(
value,
k as R_xlen_t,
allocVector(13 as libc::c_int as SEXPTYPE, 1 as libc::c_int as R_xlen_t),
);
*(DATAPTR(*(DATAPTR(value) as *mut SEXP).offset(k as isize)) as *mut libc::c_int)
.offset(0 as libc::c_int as isize) = itncnt;
k += 1;
setAttrib(value, R_NamesSymbol, names);
unprotect(3 as libc::c_int);
return value;
}