Skip to content

Commit

Permalink
Translate ziggurat algorithms for normal and exponential variates to …
Browse files Browse the repository at this point in the history
…Julia.
  • Loading branch information
andreasnoack committed Apr 16, 2014
1 parent 7cb2f13 commit 1cbb6d0
Show file tree
Hide file tree
Showing 10 changed files with 649 additions and 943 deletions.
571 changes: 530 additions & 41 deletions base/librandom.jl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ randbool!(B::BitArray) = rand!(B)
# The Ziggurat Method for generating random variables - Marsaglia and Tsang
# Paper and reference code: http://www.jstatsoft.org/v05/i08/

randn() = randmtzig_gv_randn()
randn() = randmtzig_randn()
randn(rng::MersenneTwister) = randmtzig_randn(rng.state)
randn!(A::Array{Float64}) = randmtzig_gv_fill_randn!(A)
randn!(rng::MersenneTwister, A::Array{Float64}) = randmtzig_fill_randn!(rng.state, A)
randn!(A::Array{Float64}) = (for i = 1:length(A);A[i] = randmtzig_randn();end;A)
randn!(rng::MersenneTwister, A::Array{Float64}) = (for i = 1:length(A);A[i] = randmtzig_randn(rng.state);end;A)
randn(dims::Dims) = randn!(Array(Float64, dims))
randn(dims::Int...) = randn!(Array(Float64, dims...))

Expand Down
42 changes: 21 additions & 21 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ end
# reduction along dims
include("reducedim.jl") # macros in this file relies on string.jl

# random number generation and statistics
include("statistics.jl")
include("librandom.jl")
include("random.jl")
importall .Random

# basic data structures
include("ordering.jl")
importall .Order
Expand All @@ -165,6 +159,27 @@ include("sort.jl")
importall .Sort
include("combinatorics.jl")

# rounding utilities
include("rounding.jl")
importall .Rounding

# BigInts and BigFloats
include("gmp.jl")
importall .GMP
include("mpfr.jl")
importall .MPFR
big(n::Integer) = convert(BigInt,n)
big(x::FloatingPoint) = convert(BigFloat,x)
big(q::Rational) = big(num(q))//big(den(q))
big(z::Complex) = complex(big(real(z)),big(imag(z)))
@vectorize_1arg Number big

# random number generation and statistics
include("statistics.jl")
include("librandom.jl")
include("random.jl")
importall .Random

# distributed arrays and memory-mapped arrays
include("darray.jl")
include("mmap.jl")
Expand Down Expand Up @@ -209,21 +224,6 @@ include("fftw.jl")
include("dsp.jl")
importall .DSP

# rounding utilities
include("rounding.jl")
importall .Rounding

# BigInts and BigFloats
include("gmp.jl")
importall .GMP
include("mpfr.jl")
importall .MPFR
big(n::Integer) = convert(BigInt,n)
big(x::FloatingPoint) = convert(BigFloat,x)
big(q::Rational) = big(num(q))//big(den(q))
big(z::Complex) = complex(big(real(z)),big(imag(z)))
@vectorize_1arg Number big

# (s)printf macros
include("printf.jl")
importall .Printf
Expand Down
20 changes: 9 additions & 11 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ install-openspecfun: $(OPENSPECFUN_OBJ_TARGET)
## LIBRANDOM ##

LIBRANDOM_OBJ_TARGET = $(build_shlibdir)/librandom.$(SHLIB_EXT)
LIBRANDOM_OBJ_SOURCE = random/librandom.$(SHLIB_EXT)
LIBRANDOM_OBJ_SOURCE = librandom.$(SHLIB_EXT)

LIBRANDOM_CFLAGS = $(CFLAGS) -DNDEBUG -DDSFMT_MEXP=19937 $(fPIC) -DDSFMT_DO_NOT_USE_OLD_NAMES
ifneq ($(USEMSVC), 1)
Expand All @@ -627,29 +627,27 @@ ifeq ($(ARCH), x86_64)
LIBRANDOM_CFLAGS += -msse2 -DHAVE_SSE2
endif

random/dsfmt-$(DSFMT_VER).tar.gz:
dsfmt-$(DSFMT_VER).tar.gz:
$(JLDOWNLOAD) $@ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/dSFMT-src-$(DSFMT_VER).tar.gz
touch -c $@
random/dsfmt-$(DSFMT_VER)/config.status: random/dsfmt-$(DSFMT_VER).tar.gz
cd random && \
dsfmt-$(DSFMT_VER)/config.status: dsfmt-$(DSFMT_VER).tar.gz
mkdir -p dsfmt-$(DSFMT_VER) && \
$(TAR) -C dsfmt-$(DSFMT_VER) --strip-components 1 -xf dsfmt-$(DSFMT_VER).tar.gz && \
cd dsfmt-$(DSFMT_VER) && patch < ../dSFMT.h.patch && patch < ../dSFMT.c.patch
echo 1 > $@
$(LIBRANDOM_OBJ_SOURCE): random/jl_random.c random/randmtzig.c random/dsfmt-$(DSFMT_VER)/config.status
cd random && \
$(CC) $(CPPFLAGS) $(LIBRANDOM_CFLAGS) $(LDFLAGS) jl_random.c -o librandom.$(SHLIB_EXT) && \
$(LIBRANDOM_OBJ_SOURCE): dsfmt-$(DSFMT_VER)/config.status dsfmt-$(DSFMT_VER)/dSFMT.c
$(CC) $(CPPFLAGS) $(LIBRANDOM_CFLAGS) $(LDFLAGS) dsfmt-$(DSFMT_VER)/dSFMT.c -o librandom.$(SHLIB_EXT) && \
$(INSTALL_NAME_CMD)librandom.$(SHLIB_EXT) librandom.$(SHLIB_EXT)
$(LIBRANDOM_OBJ_TARGET): $(LIBRANDOM_OBJ_SOURCE)
cp $< $@

clean-random:
-rm -f random/librandom.$(SHLIB_EXT)
-rm -f librandom.$(SHLIB_EXT)
distclean-random: clean-random
-cd random && rm -rf *.tar.gz dsfmt-$(DSFMT_VER)
-rf *.tar.gz dsfmt-$(DSFMT_VER)

get-random: random/dsfmt-$(DSFMT_VER).tar.gz
configure-random: random/dsfmt-$(DSFMT_VER)/config.status
get-random: dsfmt-$(DSFMT_VER).tar.gz
configure-random: dsfmt-$(DSFMT_VER)/config.status
compile-random: $(LIBRANDOM_OBJ_SOURCE)
check-random: compile-random
install-random: $(LIBRANDOM_OBJ_TARGET)
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions deps/random/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions deps/random/jl_random.c

This file was deleted.

Loading

0 comments on commit 1cbb6d0

Please sign in to comment.