From 7ef67334b88862873456b2e723df7cdfb3d710a9 Mon Sep 17 00:00:00 2001 From: Alessandro Andrioni Date: Mon, 18 Nov 2013 14:17:41 -0200 Subject: [PATCH] Add normally-distributed BigFloats generation --- base/gmp.jl | 1 + base/mpfr.jl | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/base/gmp.jl b/base/gmp.jl index a040d9a2c8c88..46152df8f7513 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -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() diff --git a/base/mpfr.jl b/base/mpfr.jl index 34213eaef88cc..3264126bde89e 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -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 @@ -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