Code Monkey home page Code Monkey logo

goldensequences.jl's Introduction

GoldenSequences.jl

Generalized golden sequences, a form of low discrepancy sequence or quasi random numbers See Martin Roberts: The Unreasonable Effectiveness of Quasirandom Sequences for background.

The d-dimensional sequence follows

x[i] = (x[i-1] .+ z) .% true, x[0] = x0

where

z = [ϕ[k]^(-i) for i in 1:d]

and ϕ[k] solves ϕ[k]^(d+1) = ϕ[k] + 1 (with ϕ[1] the golden mean.)

Golden sequence

julia> GoldenSequence(0.0)[1]
0.6180339887498949

Shifted golden sequence starting in 0.5

julia>  GoldenSequence(0.5)[0]
0.5

julia>  GoldenSequence(0.5)[1]
0.1180339887498949

GoldenSequence returns an infinite iterator:

julia> collect(take(GoldenSequence(0.0), 10))
10-element Array{Float64,1}:
 0.0                
 0.6180339887498949
 0.2360679774997898
 0.8541019662496847
 0.4721359549995796
 0.09016994374947451
 0.7082039324993694
 0.3262379212492643
 0.9442719099991592
 0.5623058987490541

Random colors: Low discrepancy series are good choice for (quasi-) random colors

using Colors
n = 20
c = map(x->RGB(x...), (take(GoldenSequence(3), n))) # perfect for random colors

Colors

2D golden sequence

julia>  GoldenSequence(2)[1]
(0.7548776662466927, 0.5698402909980532)

As low discrepancy series these number are well distributed (left), better than random numbers (right):

using Makie
n = 155
x = collect(Iterators.take(GoldenSequence(2), n))
p1 = scatter(x, markersize=0.02)
y = [(rand(),rand()) for i in 1:n]
p2 = scatter(y, markersize=0.02, color=:red)
vbox(p1, p2)

Quasi-random vs. random

Cartesian Golden Sequence

With a bit of effort, one can use Golden Sequences to generate spacefilling quasirandom sequences of cartesian indices. For example GoldenCartesianSequence((m[1], m[2])) will create a 2D Cartesian sequence corresponding to (approximate) samples of the Golden sequence in 1:m[1] x 1:m[2].

For that, GoldenCartesianSequence((m[1], ..., m[d])) will create full period linear congruential generators (LCG) x[i+1] = (x[i] + c[k]) % m[k] approximating phi[d]^[-k] by c[k]/m[k] such that c[k] and m[k] are coprime.

If m[1], ..., m[d] are coprime themselves, these LCG will have together period prod(m) and the sequence will be space filling, that is sort(collect(take(GoldenCartesianSequence(m), prod(m)))) == CartesianIndices(m)[:].

This means that if m[k] is the denominator of a good rational approximation c[k]//m[k] ≈ ϕ[d]^(-k), then the indices will be well distributed even for large i.

For example if m = (2819, 3508):

Quasi-random cartesian indices

The image shows the fraction of the first 0.00005 (red) and the first 0.002 indices (black) in the GoldenCartesianSequence((2819, 3508)).

In short, good m are coprime denominators of fractions given by the function rationalize

d = 2
m = @. denominator(rationalize(GoldenSequences.phis[d]^(-(1:d)), tol=0.0000001))
julia> m
2-element Array{Int64,1}:
 2819
 3508

julia> gcd(m[1], m[2])
1

There are some connections here to Knuth's multiplicative hashing method

hash(i) = mod(i*2654435769, 2^32)

where 2654435769 is approximately 2^32*ϕ.

Interface

GoldenSequence(n::Int) # Float64 n-dimensional golden sequence
GoldenSequence(x0::Number) # 1-d golden sequence shifted by `x0`
GoldenSequence(x0) # length(x)-d golden sequence shifted/starting in 'x0'

A flower

Flower petals grow in spots not covering older petals, the new spot is at an angle given by the golden sequence.

using Colors
using Makie
n = 20
c = map(x->RGB(x...), (take(GoldenSequence(3), n))) # perfect for random colors
x = collect(take(GoldenSequence(0.0), n))
petals = [(i*cos(2pi*x), i*sin(2pi*x)) for (i,x) in  enumerate(x)]
scatter(reverse(petals), color=c, markersize=10*(n:-1:1))

Flower petals

goldensequences.jl's People

Contributors

mschauer avatar github-actions[bot] avatar juliatagbot avatar oschulz avatar

Stargazers

 avatar Kenny Falkær Olsen avatar  avatar Atiyo Ghosh avatar Elias Carvalho avatar Letícia Maria Pequeno Madureira avatar Logan Forman avatar ebigram avatar Chad Scherrer avatar Zhaozhou Li avatar Mohamed Tarek avatar Martin Roberts avatar Gustavo Goretkin avatar lua avatar Dream Scatter avatar Jeffrey Sarnoff avatar Simon Christ avatar Kusti Skytén avatar

Watchers

James Cloos avatar  avatar  avatar

goldensequences.jl's Issues

Figure out space filling (max period) full range (`NTuple{N,UInt}`) sequences

For example the 1dGoldenIntSequence(0x0000, 0xc291) has full range in UInt16, but in fact the upper and lower bits are approximations of a 2d GoldenSequence on UInt8:

T = UInt8
c = GoldenSequence(2).coeff
(0.7548776662466927, 0.5698402909980532)

r = @. (round(T, (typemax(T)+1.0)*c))
(0xc1, 0x92)

And it looks good, but close integers 0xc192 etc do not work, so we need a rule how to find such an approximation.

spacefilling

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

AbstractRNG Interface

I'd like to use GoldenSequences.jl for quasi-random (low-discrepancy) sequences, but need it to work with AbstractRNG methods such as rand; is there a way to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.