static int cdf_dchisq (lua_State *L) { /* stack should contain x, df and opt. pnonc */ lua_Number x = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number pnonc = 0; lua_Number d; check_chisq(L, 1, x, df, pnonc); /* compute central dchisq */ d = df/2; d = exp((d-1)*log(x)-x/2-d*M_LN2-dlngam(&d)); lua_pushnumber(L, d); return 1; }
static int cdf_pchisq (lua_State *L) { /* stack should contain x, df and opt. pnonc */ lua_Number x = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number pnonc = luaL_optnumber(L, 3, 0); lua_Number p, q, bound; int which = 1; int status; check_chisq(L, 1, x, df, pnonc); if (pnonc==0) /* central? */ cdfchi(&which, &p, &q, &x, &df, &status, &bound); else /* non-central */ cdfchn(&which, &p, &q, &x, &df, &pnonc, &status, &bound); check_status(status, bound); lua_pushnumber(L, p); return 1; }
static int stat_dchisq (lua_State *L) { /* stack should contain x, df and opt. pnonc */ lua_Number x = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number pnonc = luaL_optnumber(L, 3, 0); lua_Number d; check_chisq(L, 1, x, df, pnonc); /* compute central dchisq */ d = df / 2; d = exp((d - 1) * log(x) - x / 2 - d * M_LN2 - dlngam(&d)); /* compute non-central if that's the case */ if (pnonc != 0) { /* non-central? */ /* evaluate weighted series */ int i; lua_Number t = d *= exp(-pnonc / 2); /* first term */ for (i = 1; i < MAXITER && d > LBOUND && t > DBL_EPSILON * d; i++) d += t *= x * pnonc / (2 * i * (df + 2 * (i - 1))); } lua_pushnumber(L, d); return 1; }
static int cdf_qchisq (lua_State *L) { /* stack should contain p, df and opt. pnonc */ lua_Number p = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number pnonc = luaL_optnumber(L, 3, 0); lua_Number x; check_chisq(L, 2, p, df, pnonc); if (p==0 || p==1) x = (p==0) ? 0 : HUGE_VAL; else { lua_Number q = 1-p; lua_Number bound; int which = 2; int status; if (pnonc==0) /* central? */ cdfchi(&which, &p, &q, &x, &df, &status, &bound); else /* non-central */ cdfchn(&which, &p, &q, &x, &df, &pnonc, &status, &bound); check_status(status, bound); } lua_pushnumber(L, x); return 1; }