Skip to content

Commit

Permalink
Add normally-distributed BigFloats generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrioni committed Nov 18, 2013
1 parent 3549bb8 commit 7ef6733
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ function rand!(r::Range1{BigInt}, A::Array{BigInt})
end

rand(r::Range1{BigInt}, dims::Dims) = rand!(r, Array(BigInt, dims))
rand(r::Range1{BigInt}, dims::Int...) = rand!(r, Array(BigInt, dims...))

function randu(randstate::BigRNG, k::BigInt)
z = BigInt()
Expand Down
21 changes: 20 additions & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import
gamma, lgamma, digamma, erf, erfc, zeta, log1p, airyai, iceil, ifloor,
itrunc, eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
serialize, deserialize, inf, nan, hash, cbrt, rand, rand!
serialize, deserialize, inf, nan, hash, cbrt, rand, rand!, randn, randn!

import Base.Math.lgamma_r

Expand Down Expand Up @@ -757,4 +757,23 @@ function rand!(r::BigRNG, A::Array{BigFloat})
end
rand!(A::Array{BigFloat}) = rand!(Base.GMP.DEFAULT_BIGRNG, A)

function randn(::Type{BigFloat}, randstate::BigRNG)
z = BigFloat()
ccall((:mpfr_grandom,:libmpfr), Int32,
(Ptr{BigFloat}, Ptr{BigFloat}, Ptr{BigRNG}, Int32),
&z, C_NULL, &randstate, ROUNDING_MODE[end])
z
end
randn(::Type{BigFloat}) = randn(BigFloat, Base.GMP.DEFAULT_BIGRNG)
randn(::Type{BigFloat}, dims::Dims) = randn!(Array(BigFloat, dims))
randn(::Type{BigFloat}, dims::Int...) = randn!(Array(BigFloat, dims...))

function randn!(r::BigRNG, A::Array{BigFloat})
for i = 1:length(A)
A[i] = randn(BigFloat, r)
end
A
end
randn!(A::Array{BigFloat}) = randn!(Base.GMP.DEFAULT_BIGRNG, A)

end #module

0 comments on commit 7ef6733

Please sign in to comment.