rustr/
rmath.rs

1//! R Math Functions
2//!
3//!
4//!
5
6
7// see R's Rmath.h as well as Writing R Extension
8use std::os::raw::{c_double as cd, c_int};
9use rdll::*;
10
11// should call get set RNG first
12
13// Random Number Generators
14pub unsafe fn norm_rand() -> cd {
15
16    ::rdll::norm_rand()
17
18}
19pub unsafe fn unif_rand() -> cd {
20
21    ::rdll::unif_rand()
22
23}
24pub unsafe fn exp_rand() -> cd {
25
26    ::rdll::exp_rand()
27
28}
29
30// Normal Distribution
31pub fn dnorm(x: cd, mu: cd, sigma: cd, lg: c_int) -> cd {
32    unsafe { Rf_dnorm4(x, mu, sigma, lg) }
33}
34pub fn pnorm(x: cd, mu: cd, sigma: cd, lt: c_int, lg: c_int) -> cd {
35    unsafe { Rf_pnorm5(x, mu, sigma, lt, lg) }
36}
37pub fn qnorm(p: cd, mu: cd, sigma: cd, lt: c_int, lg: c_int) -> cd {
38    unsafe { Rf_qnorm5(p, mu, sigma, lt, lg) }
39}
40pub unsafe fn rnorm(mu: cd, sigma: cd) -> cd {
41
42    Rf_rnorm(mu, sigma)
43
44}
45
46pub fn pnorm_both(x: cd, cum: *mut cd, ccum: *mut cd, lt: c_int, lg: c_int) {
47    unsafe { Rf_pnorm_both(x, cum, ccum, lt, lg) }
48}
49
50// Uniform Distribution
51pub fn dunif(x: cd, a: cd, b: cd, lg: c_int) -> cd {
52    unsafe { Rf_dunif(x, a, b, lg) }
53}
54pub fn punif(x: cd, a: cd, b: cd, lt: c_int, lg: c_int) -> cd {
55    unsafe { Rf_punif(x, a, b, lt, lg) }
56}
57pub fn qunif(p: cd, a: cd, b: cd, lt: c_int, lg: c_int) -> cd {
58    unsafe { Rf_qunif(p, a, b, lt, lg) }
59}
60pub unsafe fn runif(a: cd, b: cd) -> cd {
61    {
62        Rf_runif(a, b)
63    }
64}
65
66// Gamma Distribution
67pub fn dgamma(x: cd, shp: cd, scl: cd, lg: c_int) -> cd {
68    unsafe { Rf_dgamma(x, shp, scl, lg) }
69}
70pub fn pgamma(x: cd, al: cd, scl: cd, lt: c_int, lg: c_int) -> cd {
71    unsafe { Rf_pgamma(x, al, scl, lt, lg) }
72}
73pub fn qgamma(p: cd, al: cd, scl: cd, lt: c_int, lg: c_int) -> cd {
74    unsafe { Rf_qgamma(p, al, scl, lt, lg) }
75}
76pub unsafe fn rgamma(a: cd, scl: cd) -> cd {
77    {
78        Rf_rgamma(a, scl)
79    }
80}
81
82pub fn log1pmx(x: cd) -> cd {
83    unsafe { Rf_log1pmx(x) }
84}
85// pub unsafe fn rf_log1pexp(x: cd) -> cd {
86//
87//     log1pexp(x)
88//
89// }  // <-- ../nmath/plogis.c
90pub fn lgamma1p(a: cd) -> cd {
91    unsafe { Rf_lgamma1p(a) }
92}
93pub fn logspace_add(lx: cd, ly: cd) -> cd {
94    unsafe { Rf_logspace_add(lx, ly) }
95}
96pub fn logspace_sub(lx: cd, ly: cd) -> cd {
97    unsafe { Rf_logspace_sub(lx, ly) }
98}
99
100// Beta Distribution
101pub fn dbeta(x: cd, a: cd, b: cd, lg: c_int) -> cd {
102    unsafe { Rf_dbeta(x, a, b, lg) }
103}
104pub fn pbeta(x: cd, p: cd, q: cd, lt: c_int, lg: c_int) -> cd {
105    unsafe { Rf_pbeta(x, p, q, lt, lg) }
106}
107pub fn qbeta(a: cd, p: cd, q: cd, lt: c_int, lg: c_int) -> cd {
108    unsafe { Rf_qbeta(a, p, q, lt, lg) }
109}
110pub unsafe fn rbeta(a: cd, b: cd) -> cd {
111    {
112        Rf_rbeta(a, b)
113    }
114}
115
116// Lognormal Distribution
117pub fn dlnorm(x: cd, ml: cd, sl: cd, lg: c_int) -> cd {
118    unsafe { Rf_dlnorm(x, ml, sl, lg) }
119}
120pub fn plnorm(x: cd, ml: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
121    unsafe { Rf_plnorm(x, ml, sl, lt, lg) }
122}
123pub fn qlnorm(p: cd, ml: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
124    unsafe { Rf_qlnorm(p, ml, sl, lt, lg) }
125}
126pub unsafe fn rlnorm(ml: cd, sl: cd) -> cd {
127    {
128        Rf_rlnorm(ml, sl)
129    }
130}
131
132// Chi-squared Distribution
133pub fn dchisq(x: cd, df: cd, lg: c_int) -> cd {
134    unsafe { Rf_dchisq(x, df, lg) }
135}
136pub fn pchisq(x: cd, df: cd, lt: c_int, lg: c_int) -> cd {
137    unsafe { Rf_pchisq(x, df, lt, lg) }
138}
139pub fn qchisq(p: cd, df: cd, lt: c_int, lg: c_int) -> cd {
140    unsafe { Rf_qchisq(p, df, lt, lg) }
141}
142pub unsafe fn rchisq(df: cd) -> cd {
143    {
144        Rf_rchisq(df)
145    }
146}
147
148// Non-central Chi-squared Distribution
149pub fn dnchisq(x: cd, df: cd, n: cd, lg: c_int) -> cd {
150    unsafe { Rf_dnchisq(x, df, n, lg) }
151}
152pub fn pnchisq(x: cd, df: cd, n: cd, lt: c_int, lg: c_int) -> cd {
153    unsafe { Rf_pnchisq(x, df, n, lt, lg) }
154}
155pub fn qnchisq(p: cd, df: cd, n: cd, lt: c_int, lg: c_int) -> cd {
156    unsafe { Rf_qnchisq(p, df, n, lt, lg) }
157}
158pub unsafe fn rnchisq(df: cd, lb: cd) -> cd {
159    {
160        Rf_rnchisq(df, lb)
161    }
162}
163
164// F Distibution
165pub fn df(x: cd, df1: cd, df2: cd, lg: c_int) -> cd {
166    unsafe { Rf_df(x, df1, df2, lg) }
167}
168pub fn pf(x: cd, df1: cd, df2: cd, lt: c_int, lg: c_int) -> cd {
169    unsafe { Rf_pf(x, df1, df2, lt, lg) }
170}
171pub fn qf(p: cd, df1: cd, df2: cd, lt: c_int, lg: c_int) -> cd {
172    unsafe { Rf_qf(p, df1, df2, lt, lg) }
173}
174pub unsafe fn rf(df1: cd, df2: cd) -> cd {
175
176    Rf_rf(df1, df2)
177
178}
179
180// Student t Distibution
181pub fn dt(x: cd, n: cd, lg: c_int) -> cd {
182    unsafe { Rf_dt(x, n, lg) }
183}
184pub fn pt(x: cd, n: cd, lt: c_int, lg: c_int) -> cd {
185    unsafe { Rf_pt(x, n, lt, lg) }
186}
187pub fn qt(p: cd, n: cd, lt: c_int, lg: c_int) -> cd {
188    unsafe { Rf_qt(p, n, lt, lg) }
189}
190pub unsafe fn rt(n: cd) -> cd {
191
192    Rf_rt(n)
193
194}
195
196// Binomial Distribution
197pub fn dbinom(x: cd, n: cd, p: cd, lg: c_int) -> cd {
198    unsafe { Rf_dbinom(x, n, p, lg) }
199}
200pub fn pbinom(x: cd, n: cd, p: cd, lt: c_int, lg: c_int) -> cd {
201    unsafe { Rf_pbinom(x, n, p, lt, lg) }
202}
203pub fn qbinom(p: cd, n: cd, m: cd, lt: c_int, lg: c_int) -> cd {
204    unsafe { Rf_qbinom(p, n, m, lt, lg) }
205}
206pub unsafe fn rbinom(n: cd, p: cd) -> cd {
207
208    Rf_rbinom(n, p)
209
210}
211
212// Multnomial Distribution
213pub unsafe fn rmultinom(n: c_int, prob: *mut cd, k: c_int, rn: *mut c_int) {
214
215    Rf_rmultinom(n, prob, k, rn)
216
217}
218
219// Cauchy Distribution
220pub fn dcauchy(x: cd, lc: cd, sl: cd, lg: c_int) -> cd {
221    unsafe { Rf_dcauchy(x, lc, sl, lg) }
222}
223pub fn pcauchy(x: cd, lc: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
224    unsafe { Rf_pcauchy(x, lc, sl, lt, lg) }
225}
226pub fn qcauchy(p: cd, lc: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
227    unsafe { Rf_qcauchy(p, lc, sl, lt, lg) }
228}
229pub unsafe fn rcauchy(lc: cd, sl: cd) -> cd {
230
231    Rf_rcauchy(lc, sl)
232
233}
234
235// Exponential Distribution
236pub fn dexp(x: cd, sl: cd, lg: c_int) -> cd {
237    unsafe { Rf_dexp(x, sl, lg) }
238}
239pub fn pexp(x: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
240    unsafe { Rf_pexp(x, sl, lt, lg) }
241}
242pub fn qexp(p: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
243    unsafe { Rf_qexp(p, sl, lt, lg) }
244}
245pub unsafe fn rexp(sl: cd) -> cd {
246
247    Rf_rexp(sl)
248
249}
250
251// Geometric Distribution
252pub fn dgeom(x: cd, p: cd, lg: c_int) -> cd {
253    unsafe { Rf_dgeom(x, p, lg) }
254}
255pub fn pgeom(x: cd, p: cd, lt: c_int, lg: c_int) -> cd {
256    unsafe { Rf_pgeom(x, p, lt, lg) }
257}
258pub fn qgeom(p: cd, pb: cd, lt: c_int, lg: c_int) -> cd {
259    unsafe { Rf_qgeom(p, pb, lt, lg) }
260}
261pub unsafe fn rgeom(p: cd) -> cd {
262
263    Rf_rgeom(p)
264
265}
266
267// Hypergeometric Distibution
268pub fn dhyper(x: cd, r: cd, b: cd, n: cd, lg: c_int) -> cd {
269    unsafe { Rf_dhyper(x, r, b, n, lg) }
270}
271pub fn phyper(x: cd, r: cd, b: cd, n: cd, lt: c_int, lg: c_int) -> cd {
272    unsafe { Rf_phyper(x, r, b, n, lt, lg) }
273}
274pub fn qhyper(p: cd, r: cd, b: cd, n: cd, lt: c_int, lg: c_int) -> cd {
275    unsafe { Rf_qhyper(p, r, b, n, lt, lg) }
276}
277pub unsafe fn rhyper(r: cd, b: cd, n: cd) -> cd {
278
279    Rf_rhyper(r, b, n)
280
281}
282
283// Negative Binomial Distribution
284pub fn dnbinom(x: cd, sz: cd, p: cd, lg: c_int) -> cd {
285    unsafe { Rf_dnbinom(x, sz, p, lg) }
286}
287pub fn pnbinom(x: cd, sz: cd, p: cd, lt: c_int, lg: c_int) -> cd {
288    unsafe { Rf_pnbinom(x, sz, p, lt, lg) }
289}
290pub fn qnbinom(p: cd, sz: cd, pb: cd, lt: c_int, lg: c_int) -> cd {
291    unsafe { Rf_qnbinom(p, sz, pb, lt, lg) }
292}
293pub unsafe fn rnbinom(sz: cd, p: cd) -> cd {
294
295    Rf_rnbinom(sz, p)
296
297}
298
299
300
301pub fn dnbinom_mu(x: cd, sz: cd, mu: cd, lg: c_int) -> cd {
302    unsafe { Rf_dnbinom_mu(x, sz, mu, lg) }
303}
304pub fn pnbinom_mu(x: cd, sz: cd, mu: cd, lt: c_int, lg: c_int) -> cd {
305    unsafe { Rf_pnbinom_mu(x, sz, mu, lt, lg) }
306}
307pub fn qnbinom_mu(x: cd, sz: cd, mu: cd, lt: c_int, lg: c_int) -> cd {
308    unsafe { Rf_qnbinom_mu(x, sz, mu, lt, lg) }
309}
310
311pub unsafe fn rnbinom_mu(sz: cd, mu: cd) -> cd {
312    {
313        Rf_rnbinom_mu(sz, mu)
314    }
315}
316
317
318// Poisson Distribution
319pub fn dpois(x: cd, lb: cd, lg: c_int) -> cd {
320    unsafe { Rf_dpois(x, lb, lg) }
321}
322pub fn ppois(x: cd, lb: cd, lt: c_int, lg: c_int) -> cd {
323    unsafe { Rf_ppois(x, lb, lt, lg) }
324}
325pub fn qpois(p: cd, lb: cd, lt: c_int, lg: c_int) -> cd {
326    unsafe { Rf_qpois(p, lb, lt, lg) }
327}
328pub unsafe fn rpois(mu: cd) -> cd {
329
330    Rf_rpois(mu)
331
332}
333
334// Weibull Distribution
335pub fn dweibull(x: cd, sh: cd, sl: cd, lg: c_int) -> cd {
336    unsafe { Rf_dweibull(x, sh, sl, lg) }
337}
338pub fn pweibull(x: cd, sh: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
339    unsafe { Rf_pweibull(x, sh, sl, lt, lg) }
340}
341pub fn qweibull(p: cd, sh: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
342    unsafe { Rf_qweibull(p, sh, sl, lt, lg) }
343}
344pub unsafe fn rweibull(sh: cd, sl: cd) -> cd {
345
346    Rf_rweibull(sh, sl)
347
348}
349
350// Logistic Distribution
351pub fn dlogis(x: cd, lc: cd, sl: cd, lg: c_int) -> cd {
352    unsafe { Rf_dlogis(x, lc, sl, lg) }
353}
354pub fn plogis(x: cd, lc: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
355    unsafe { Rf_plogis(x, lc, sl, lt, lg) }
356}
357pub fn qlogis(p: cd, lc: cd, sl: cd, lt: c_int, lg: c_int) -> cd {
358    unsafe { Rf_qlogis(p, lc, sl, lt, lg) }
359}
360pub unsafe fn rlogis(lc: cd, sl: cd) -> cd {
361
362    Rf_rlogis(lc, sl)
363
364}
365
366// Non-central Beta Distribution
367pub fn dnbeta(x: cd, a: cd, b: cd, n: cd, lg: c_int) -> cd {
368    unsafe { Rf_dnbeta(x, a, b, n, lg) }
369}
370pub fn pnbeta(x: cd, a: cd, b: cd, n: cd, lt: c_int, lg: c_int) -> cd {
371    unsafe { Rf_pnbeta(x, a, b, n, lt, lg) }
372}
373pub fn qnbeta(p: cd, a: cd, b: cd, n: cd, lt: c_int, lg: c_int) -> cd {
374    unsafe { Rf_qnbeta(p, a, b, n, lt, lg) }
375}
376// pub  fn rnbeta(a: cd, b: cd, np: cd) -> cd {
377//
378//     Rf_rnbeta(a, b, np)
379//
380// }
381
382// Non-central F Distribution
383pub fn dnf(x: cd, df1: cd, df2: cd, n: cd, lg: c_int) -> cd {
384    unsafe { Rf_dnf(x, df1, df2, n, lg) }
385}
386pub fn pnf(x: cd, df1: cd, df2: cd, n: cd, lt: c_int, lg: c_int) -> cd {
387    unsafe { Rf_pnf(x, df1, df2, n, lt, lg) }
388}
389pub fn qnf(p: cd, df1: cd, df2: cd, n: cd, lt: c_int, lg: c_int) -> cd {
390    unsafe { Rf_qnf(p, df1, df2, n, lt, lg) }
391}
392
393// Non-central Student t Distribution
394pub fn dnt(x: cd, df: cd, n: cd, lg: c_int) -> cd {
395    unsafe { Rf_dnt(x, df, n, lg) }
396}
397pub fn pnt(x: cd, df: cd, n: cd, lt: c_int, lg: c_int) -> cd {
398    unsafe { Rf_pnt(x, df, n, lt, lg) }
399}
400pub fn qnt(p: cd, df: cd, n: cd, lt: c_int, lg: c_int) -> cd {
401    unsafe { Rf_qnt(p, df, n, lt, lg) }
402}
403
404// Studentized Range Distribution
405pub fn ptukey(q: cd, rr: cd, cc: cd, df: cd, lt: c_int, lg: c_int) -> cd {
406    unsafe { Rf_ptukey(q, rr, cc, df, lt, lg) }
407}
408pub fn qtukey(p: cd, rr: cd, cc: cd, df: cd, lt: c_int, lg: c_int) -> cd {
409    unsafe { Rf_qtukey(p, rr, cc, df, lt, lg) }
410}
411
412// Wilcoxon Rank Sum Distribution
413pub fn dwilcox(x: cd, m: cd, n: cd, lg: c_int) -> cd {
414    unsafe { Rf_dwilcox(x, m, n, lg) }
415}
416pub fn pwilcox(q: cd, m: cd, n: cd, lt: c_int, lg: c_int) -> cd {
417    unsafe { Rf_pwilcox(q, m, n, lt, lg) }
418}
419pub fn qwilcox(x: cd, m: cd, n: cd, lt: c_int, lg: c_int) -> cd {
420    unsafe { Rf_qwilcox(x, m, n, lt, lg) }
421}
422pub unsafe fn rwilcox(m: cd, n: cd) -> cd {
423
424    Rf_rwilcox(m, n)
425
426}
427
428// Wilcoxon Signed Rank Distribution
429pub fn dsignrank(x: cd, n: cd, lg: c_int) -> cd {
430    unsafe { Rf_dsignrank(x, n, lg) }
431}
432pub fn psignrank(x: cd, n: cd, lt: c_int, lg: c_int) -> cd {
433    unsafe { Rf_psignrank(x, n, lt, lg) }
434}
435pub fn qsignrank(x: cd, n: cd, lt: c_int, lg: c_int) -> cd {
436    unsafe { Rf_qsignrank(x, n, lt, lg) }
437}
438pub unsafe fn rsignrank(n: cd) -> cd {
439
440    Rf_rsignrank(n)
441
442}
443
444// Gamma and Related Functions
445pub fn gammafn(x: cd) -> cd {
446    unsafe { Rf_gammafn(x) }
447}
448pub fn lgammafn(x: cd) -> cd {
449    unsafe { Rf_lgammafn(x) }
450}
451pub fn lgammafn_sign(x: cd, sgn: *mut c_int) -> cd {
452    unsafe { Rf_lgammafn_sign(x, sgn) }
453}
454
455pub fn dpsifn(x: cd,
456              n: c_int,
457              k: c_int,
458              m: c_int,
459              ans: *mut cd,
460              nz: *mut c_int,
461              ierr: *mut c_int) {
462    unsafe { Rf_dpsifn(x, n, k, m, ans, nz, ierr) }
463}
464pub fn psigamma(x: cd, deriv: cd) -> cd {
465    unsafe { Rf_psigamma(x, deriv) }
466}
467pub fn digamma(x: cd) -> cd {
468    unsafe { Rf_digamma(x) }
469}
470pub fn trigamma(x: cd) -> cd {
471    unsafe { Rf_trigamma(x) }
472}
473pub fn tetragamma(x: cd) -> cd {
474    unsafe { Rf_tetragamma(x) }
475}
476pub fn pentagamma(x: cd) -> cd {
477    unsafe { Rf_pentagamma(x) }
478}
479
480pub fn beta(a: cd, b: cd) -> cd {
481    unsafe { Rf_beta(a, b) }
482}
483pub fn lbeta(a: cd, b: cd) -> cd {
484    unsafe { Rf_lbeta(a, b) }
485}
486
487pub fn choose(n: cd, k: cd) -> cd {
488    unsafe { Rf_choose(n, k) }
489}
490pub fn lchoose(n: cd, k: cd) -> cd {
491    unsafe { Rf_lchoose(n, k) }
492}
493
494// Bessel Functions
495pub fn bessel_i(x: cd, al: cd, ex: cd) -> cd {
496    unsafe { Rf_bessel_i(x, al, ex) }
497}
498pub fn bessel_j(x: cd, al: cd) -> cd {
499    unsafe { Rf_bessel_j(x, al) }
500}
501pub fn bessel_k(x: cd, al: cd, ex: cd) -> cd {
502    unsafe { Rf_bessel_k(x, al, ex) }
503}
504pub fn bessel_y(x: cd, al: cd) -> cd {
505    unsafe { Rf_bessel_y(x, al) }
506}
507pub fn bessel_i_ex(x: cd, al: cd, ex: cd, bi: *mut cd) -> cd {
508    unsafe { Rf_bessel_i_ex(x, al, ex, bi) }
509}
510pub fn bessel_j_ex(x: cd, al: cd, bj: *mut cd) -> cd {
511    unsafe { Rf_bessel_j_ex(x, al, bj) }
512}
513pub fn bessel_k_ex(x: cd, al: cd, ex: cd, bk: *mut cd) -> cd {
514    unsafe { Rf_bessel_k_ex(x, al, ex, bk) }
515}
516pub fn bessel_y_ex(x: cd, al: cd, by: *mut cd) -> cd {
517    unsafe { Rf_bessel_y_ex(x, al, by) }
518}
519
520
521// General Support Functions
522// #ifndef HAVE_HYPOT
523// pub unsafe fn hypot(a: cd, b: cd) -> cd {
524//
525//         Rf_hypot(a, b)
526//
527// }
528// #endif
529
530// pub unsafe fn pythag(a: cd, b: cd) -> cd {
531//
532//     Rf_pythag(a, b)
533//
534// }
535
536// #ifndef HAVE_EXPM1
537// pub unsafe fn expm1(x: cd) -> cd /* = exp(x)-1 {care for small x} */ {
538//
539//         Rf_expm1(x)
540//
541// }
542// #endif
543
544// #ifndef HAVE_LOG1P
545// pub unsafe fn log1p(x: cd) -> cd /* = log(1+x) {care for small x} */ {
546//
547//         Rf_log1p(x)
548//
549// }
550// #endif
551
552pub fn imax2(x: c_int, y: c_int) -> c_int {
553    unsafe { Rf_imax2(x, y) }
554}
555pub fn imin2(x: c_int, y: c_int) -> c_int {
556    unsafe { Rf_imin2(x, y) }
557}
558
559pub fn fmax2(x: cd, y: cd) -> cd {
560    unsafe { Rf_fmax2(x, y) }
561}
562pub fn fmin2(x: cd, y: cd) -> cd {
563    unsafe { Rf_fmin2(x, y) }
564}
565pub fn sign(x: cd) -> cd {
566    unsafe { Rf_sign(x) }
567}
568pub fn fprec(x: cd, dg: cd) -> cd {
569    unsafe { Rf_fprec(x, dg) }
570}
571pub fn fround(x: cd, dg: cd) -> cd {
572    unsafe { Rf_fround(x, dg) }
573}
574pub fn fsign(x: cd, y: cd) -> cd {
575    unsafe { Rf_fsign(x, y) }
576}
577pub fn ftrunc(x: cd) -> cd {
578    unsafe { Rf_ftrunc(x) }
579}