22
33module RFunctions
44
5- import Rmath: libRmath
5+ using Rmath: Rmath
66
77# ## import macro
88
99function _import_rmath (rname:: Symbol , jname:: Symbol , pargs)
10- # C function names
11- if rname == :norm
12- dfun = Expr (:quote , " dnorm4" )
13- pfun = Expr (:quote , " pnorm5" )
14- qfun = Expr (:quote , " qnorm5" )
15- rfun = Expr (:quote , " rnorm" )
16- else
17- dfun = Expr (:quote , string (' d' , rname)) # density
18- pfun = Expr (:quote , string (' p' , rname)) # cumulative probability
19- qfun = Expr (:quote , string (' q' , rname)) # quantile
20- rfun = Expr (:quote , string (' r' , rname)) # random sampling
21- end
10+ # Rmath function names
11+ dfun = Symbol (:d , rname) # density
12+ pfun = Symbol (:p , rname) # cumulative probability
13+ qfun = Symbol (:q , rname) # quantile
14+ rfun = Symbol (:r , rname) # random sampling
2215
2316 # Julia function names
2417 pdf = Symbol (jname, " pdf" )
@@ -34,93 +27,76 @@ function _import_rmath(rname::Symbol, jname::Symbol, pargs)
3427 invlogcdf = Symbol (jname, " invlogcdf" )
3528 invlogccdf = Symbol (jname, " invlogccdf" )
3629
37- is_tukey = rname == :tukey
38-
3930 rand = Symbol (jname, " rand" )
40- has_rand = true
41- if rname == :nbeta || rname == :nf || rname == :nt || is_tukey
42- has_rand = false
43- end
4431
4532 # arguments & argument types
46- _pts = fill (:Cdouble , length (pargs))
47-
48- if is_tukey
49- dtypes = Expr (:tuple , :Cdouble , :Cdouble , _pts... , :Cint )
50- ptypes = Expr (:tuple , :Cdouble , :Cdouble , _pts... , :Cint , :Cint )
51- qtypes = Expr (:tuple , :Cdouble , :Cdouble , _pts... , :Cint , :Cint )
52- rtypes = Expr (:tuple , :Cdouble , _pts... )
53- else
54- dtypes = Expr (:tuple , :Cdouble , _pts... , :Cint )
55- ptypes = Expr (:tuple , :Cdouble , _pts... , :Cint , :Cint )
56- qtypes = Expr (:tuple , :Cdouble , _pts... , :Cint , :Cint )
57- rtypes = Expr (:tuple , _pts... )
58- end
59-
6033 pdecls = [Expr (:(:: ), ps, :Real ) for ps in pargs] # [:(p1::Real), :(p2::Real), ...]
6134
62- if is_tukey
35+ if rname == :tukey
6336 # ptukey and qtukey have an extra literal 1 argument
64- pargs = (1 , pargs... )
37+ pargs = (pargs... , 1 )
6538 end
6639
6740 # Function implementation
6841 return quote
69- if $ (! is_tukey )
42+ if $ (isdefined (Rmath, dfun) )
7043 function $pdf ($ (pdecls... ), x:: Real )
7144 T = float (Base. promote_typeof ($ (pargs... ), x))
72- return convert (T, ccall (( $ dfun, libRmath), Float64, $ dtypes, x, $ (pargs... ), 0 ))
45+ return convert (T, Rmath. $ dfun ( x, $ (pargs... ), false ))
7346 end
7447
7548 function $logpdf ($ (pdecls... ), x:: Real )
7649 T = float (Base. promote_typeof ($ (pargs... ), x))
77- return convert (T, ccall (( $ dfun, libRmath), Float64, $ dtypes, x, $ (pargs... ), 1 ))
50+ return convert (T, Rmath. $ dfun ( x, $ (pargs... ), true ))
7851 end
7952 end
8053
81- function $cdf ($ (pdecls... ), x:: Real )
82- T = float (Base. promote_typeof ($ (pargs... ), x))
83- return convert (T, ccall (($ pfun, libRmath), Float64, $ ptypes, x, $ (pargs... ), 1 , 0 ))
84- end
54+ if $ (isdefined (Rmath, pfun))
55+ function $cdf ($ (pdecls... ), x:: Real )
56+ T = float (Base. promote_typeof ($ (pargs... ), x))
57+ return convert (T, Rmath.$ pfun (x, $ (pargs... ), true , false ))
58+ end
8559
86- function $ccdf ($ (pdecls... ), x:: Real )
87- T = float (Base. promote_typeof ($ (pargs... ), x))
88- return convert (T, ccall (( $ pfun, libRmath), Float64, $ ptypes, x, $ (pargs... ), 0 , 0 ))
89- end
60+ function $ccdf ($ (pdecls... ), x:: Real )
61+ T = float (Base. promote_typeof ($ (pargs... ), x))
62+ return convert (T, Rmath. $ pfun ( x, $ (pargs... ), false , false ))
63+ end
9064
91- function $logcdf ($ (pdecls... ), x:: Real )
92- T = float (Base. promote_typeof ($ (pargs... ), x))
93- return convert (T, ccall (( $ pfun, libRmath), Float64, $ ptypes, x, $ (pargs... ), 1 , 1 ))
94- end
65+ function $logcdf ($ (pdecls... ), x:: Real )
66+ T = float (Base. promote_typeof ($ (pargs... ), x))
67+ return convert (T, Rmath. $ pfun ( x, $ (pargs... ), true , true ))
68+ end
9569
96- function $logccdf ($ (pdecls... ), x:: Real )
97- T = float (Base. promote_typeof ($ (pargs... ), x))
98- return convert (T, ccall (($ pfun, libRmath), Float64, $ ptypes, x, $ (pargs... ), 0 , 1 ))
70+ function $logccdf ($ (pdecls... ), x:: Real )
71+ T = float (Base. promote_typeof ($ (pargs... ), x))
72+ return convert (T, Rmath.$ pfun (x, $ (pargs... ), false , true ))
73+ end
9974 end
10075
101- function $invcdf ($ (pdecls... ), q:: Real )
102- T = float (Base. promote_typeof ($ (pargs... ), q))
103- return convert (T, ccall (($ qfun, libRmath), Float64, $ qtypes, q, $ (pargs... ), 1 , 0 ))
104- end
76+ if $ (isdefined (Rmath, qfun))
77+ function $invcdf ($ (pdecls... ), q:: Real )
78+ T = float (Base. promote_typeof ($ (pargs... ), q))
79+ return convert (T, Rmath.$ qfun (q, $ (pargs... ), true , false ))
80+ end
10581
106- function $invccdf ($ (pdecls... ), q:: Real )
107- T = float (Base. promote_typeof ($ (pargs... ), q))
108- return convert (T, ccall (( $ qfun, libRmath), Float64, $ qtypes, q, $ (pargs... ), 0 , 0 ))
109- end
82+ function $invccdf ($ (pdecls... ), q:: Real )
83+ T = float (Base. promote_typeof ($ (pargs... ), q))
84+ return convert (T, Rmath. $ qfun ( q, $ (pargs... ), false , false ))
85+ end
11086
111- function $invlogcdf ($ (pdecls... ), lq:: Real )
112- T = float (Base. promote_typeof ($ (pargs... ), lq))
113- return convert (T, ccall (( $ qfun, libRmath), Float64, $ qtypes, lq, $ (pargs... ), 1 , 1 ))
114- end
87+ function $invlogcdf ($ (pdecls... ), lq:: Real )
88+ T = float (Base. promote_typeof ($ (pargs... ), lq))
89+ return convert (T, Rmath. $ qfun ( lq, $ (pargs... ), true , true ))
90+ end
11591
116- function $invlogccdf ($ (pdecls... ), lq:: Real )
117- T = float (Base. promote_typeof ($ (pargs... ), lq))
118- return convert (T, ccall (($ qfun, libRmath), Float64, $ qtypes, lq, $ (pargs... ), 0 , 1 ))
92+ function $invlogccdf ($ (pdecls... ), lq:: Real )
93+ T = float (Base. promote_typeof ($ (pargs... ), lq))
94+ return convert (T, Rmath.$ qfun (lq, $ (pargs... ), false , true ))
95+ end
11996 end
12097
121- if $ has_rand
122- $ rand ($ (pdecls... )) =
123- ccall (($ rfun, libRmath), Float64, $ rtypes, $ (pargs... ))
98+ if $ (isdefined (Rmath, rfun))
99+ $ rand ($ (pdecls... )) = Rmath.$ rfun ($ (pargs... ))
124100 end
125101 end
126102end
0 commit comments