Short function definition (one-liner):
jump() = rand( (-1, +1) )
jump (generic function with 1 method)
Exactly the same:
function jump()
return rand( (-1, +1) )
end
jump (generic function with 1 method)
jump
jump (generic function with 1 method)
jump()
1
rand( [-1, +1] )
1
t = (-1, +1)
(-1, 1)
typeof(t)
Tuple{Int64,Int64}
t[1] = 10
MethodError: no method matching setindex!(::Tuple{Int64,Int64}, ::Int64, ::Int64) Stacktrace: [1] top-level scope at In[10]:1
Side remark: Simple bernoulli function:
bernoulli(p) = rand() < p
bernoulli (generic function with 1 method)
bernoulli(0.25)
false
jump()
-1
[jump() for i in 1:10]
10-element Array{Int64,1}: 1 -1 1 1 1 1 -1 1 -1 1
function walk(n)
x = 0
for i in 1:n
x += jump() # x = x + jump()
end
return x
end
walk (generic function with 1 method)
walk(10)
2
function trajectory(n)
x = 0
xs = [x]
for i in 1:n
x += jump() # x = x + jump()
end
return x
end
trajectory (generic function with 1 method)
v = []
0-element Array{Any,1}
push!(v, 10)
1-element Array{Any,1}: 10
v
1-element Array{Any,1}: 10
push!(v, "hellohellohellohellohellohello", 20)
3-element Array{Any,1}: 10 "hellohellohellohellohellohello" 20
v = Int64[]
0-element Array{Int64,1}
push!(v, 10)
1-element Array{Int64,1}: 10
push!(v, "hello")
MethodError: Cannot `convert` an object of type String to an object of type Int64 Closest candidates are: convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6 convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7 convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23 ... Stacktrace: [1] push!(::Array{Int64,1}, ::String) at ./array.jl:912 [2] top-level scope at In[24]:1
function trajectory(n)
x = 0
xs = [x] # try to initialize array with an initial value;
# determines the element type of array
for i in 1:n
x += jump() # x = x + jump()
push!(xs, x)
end
return xs
end
trajectory (generic function with 1 method)
v = [10]
1-element Array{Int64,1}: 10
traj = trajectory(100);
plot(traj)
UndefVarError: plot not defined Stacktrace: [1] top-level scope at In[28]:1
using Plots
traj = trajectory(100);
plot(traj, m=:o)
traj = trajectory(100);
plot(traj, m=:o)
traj = trajectory(100);
plot(traj, m=:o, size=(300, 200))
hline!([0], ls=:dash) # ! functions add to the existing plot
num_walkers = 1000
num_steps = 100
p = plot(size=(500, 400), leg=false)
for i in 1:num_walkers
traj = trajectory(num_steps)
plot!(traj)
end
p
using Interact
Unable to load WebIO. Please make sure WebIO works for your Jupyter client. For troubleshooting, please see the WebIO/IJulia documentation.
@manipulate for n in 1:20
traj = trajectory(n)
plot(traj)
end
n = 20
traj = trajectory(n)
@manipulate for i in slider(1:n, value=1)
plot(traj[1:i]) # plot up to time i
xlims!(0, length(traj))
ylims!(minimum(traj), maximum(traj))
end
n = 20
traj_1 = trajectory(n)
traj_2 = trajectory(n)
traj_3 = trajectory(n)
21-element Array{Int64,1}: 0 1 0 1 0 1 0 1 0 -1 -2 -1 -2 -1 -2 -3 -4 -3 -2 -1 0
Rule: NEVER copy and paste (except occasionally)
Instead: Automate!
n = 20
traj = [trajectory(n)]
1-element Array{Array{Int64,1},1}: [0, -1, -2, -1, -2, -3, -4, -3, -4, -5 … -3, -2, -3, -2, -1, -2, -1, -2, -3, -4]
push!(traj, trajectory(n))
2-element Array{Array{Int64,1},1}: [0, -1, -2, -1, -2, -3, -4, -3, -4, -5 … -3, -2, -3, -2, -1, -2, -1, -2, -3, -4] [0, 1, 2, 3, 4, 3, 4, 5, 6, 7 … 5, 4, 5, 4, 3, 4, 3, 2, 3, 2]
traj[1]
21-element Array{Int64,1}: 0 -1 -2 -1 -2 -3 -4 -3 -4 -5 -4 -3 -2 -3 -2 -1 -2 -1 -2 -3 -4
?push!
search: push! pushfirst! pushdisplay
push!(collection, items...) -> collection
Insert one or more items
in collection
. If collection
is an ordered container, the items are inserted at the end (in the given order).
jldoctest
julia> push!([1, 2, 3], 4, 5, 6)
6-element Array{Int64,1}:
1
2
3
4
5
6
If collection
is ordered, use append!
to add all the elements of another collection to it. The result of the preceding example is equivalent to append!([1, 2, 3], [4, 5, 6])
. For AbstractSet
objects, union!
can be used instead.
push!(q::Deque{T}, x)
Add an element to the back
push!(s::IntDisjointSets{T})
Make a new subset with an automatically chosen new element x. Returns the new element. Throw an ArgumentError
if the capacity of the set would be exceeded.
push!(s::DisjointSets{T}, x::T)
Make a new subset with an automatically chosen new element x. Returns the new element.
push!(sc, k=>v)
Argument sc
is a SortedDict or SortedMultiDict and k=>v
is a key-value pair. This inserts the key-value pair into the container. If the key is already present, this overwrites the old value. The return value is sc
. Time: O(c log n)
push!(sc, k=>v)
Argument sc
is a SortedDict or SortedMultiDict and k=>v
is a key-value pair. This inserts the key-value pair into the container. If the key is already present, this overwrites the old value. The return value is sc
. Time: O(c log n)
push!(sc, k)
Argument sc
is a SortedSet and k
is a key. This inserts the key into the container. If the key is already present, this overwrites the old value. (This is not necessarily a no-op; see below for remarks about the customizing the sort order.) The return value is sc
. Time: O(c log n)
push!(cb::CircularBuffer, data)
Add an element to the back and overwrite front if full.
v = [1, 2, 3]
3-element Array{Int64,1}: 1 2 3
push!(v, 4)
4-element Array{Int64,1}: 1 2 3 4
push!(v, push!(v, [4, 5])4, 5)
6-element Array{Int64,1}: 1 2 3 4 4 5
push!(v, [4, 5])
MethodError: Cannot `convert` an object of type Array{Int64,1} to an object of type Int64 Closest candidates are: convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6 convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7 convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23 ... Stacktrace: [1] push!(::Array{Int64,1}, ::Array{Int64,1}) at ./array.jl:912 [2] top-level scope at In[54]:1
append!(v, [4, 5])
8-element Array{Int64,1}: 1 2 3 4 4 5 4 5
traj
2-element Array{Array{Int64,1},1}: [0, -1, -2, -1, -2, -3, -4, -3, -4, -5 … -3, -2, -3, -2, -1, -2, -1, -2, -3, -4] [0, 1, 2, 3, 4, 3, 4, 5, 6, 7 … 5, 4, 5, 4, 3, 4, 3, 2, 3, 2]
push!(traj, trajectory(n))
3-element Array{Array{Int64,1},1}: [0, -1, -2, -1, -2, -3, -4, -3, -4, -5 … -3, -2, -3, -2, -1, -2, -1, -2, -3, -4] [0, 1, 2, 3, 4, 3, 4, 5, 6, 7 … 5, 4, 5, 4, 3, 4, 3, 2, 3, 2] [0, 1, 2, 1, 0, 1, 2, 1, 0, 1 … 3, 2, 3, 4, 3, 2, 1, 2, 1, 2]
walkers = [trajectory(n) for i in 1:10]
10-element Array{Array{Int64,1},1}: [0, 1, 2, 1, 0, -1, 0, -1, -2, -1 … 1, 0, 1, 0, -1, -2, -1, 0, -1, -2] [0, -1, 0, 1, 2, 3, 4, 3, 4, 5 … 3, 2, 3, 2, 3, 4, 3, 2, 1, 2] [0, -1, -2, -1, 0, -1, 0, 1, 2, 1 … 1, 2, 1, 0, -1, -2, -1, 0, -1, -2] [0, -1, 0, -1, -2, -1, -2, -3, -4, -5 … -7, -6, -7, -6, -7, -8, -9, -10, -11, -10] [0, 1, 0, -1, 0, 1, 2, 3, 4, 5 … 5, 4, 5, 4, 3, 4, 5, 6, 7, 8] [0, 1, 0, 1, 2, 3, 4, 3, 2, 3 … 3, 2, 1, 0, 1, 0, -1, 0, -1, 0] [0, 1, 2, 1, 0, 1, 2, 3, 4, 3 … 3, 4, 3, 2, 1, 2, 1, 2, 3, 2] [0, 1, 2, 3, 2, 1, 2, 3, 4, 3 … 5, 4, 3, 2, 3, 4, 3, 4, 5, 4] [0, 1, 0, 1, 2, 3, 2, 3, 2, 3 … 3, 2, 1, 0, -1, -2, -3, -4, -3, -4] [0, 1, 2, 3, 4, 3, 2, 1, 2, 3 … 1, 2, 1, 2, 3, 4, 5, 4, 3, 4]
n
20
single_traj = trajectory(n);
length(single_traj)
21
# single_traj[n]
-3
single_traj[end]
-4
In a Markdown cell, backticks: x = 3
i = 10
[ traj[i] for traj in walkers ] # iterating through `walkers`, which
# is a `Vector` of `Vector`s
10-element Array{Int64,1}: -1 5 1 -5 5 3 3 3 3 3
walkers
10-element Array{Array{Int64,1},1}: [0, 1, 2, 1, 0, -1, 0, -1, -2, -1 … 1, 0, 1, 0, -1, -2, -1, 0, -1, -2] [0, -1, 0, 1, 2, 3, 4, 3, 4, 5 … 3, 2, 3, 2, 3, 4, 3, 2, 1, 2] [0, -1, -2, -1, 0, -1, 0, 1, 2, 1 … 1, 2, 1, 0, -1, -2, -1, 0, -1, -2] [0, -1, 0, -1, -2, -1, -2, -3, -4, -5 … -7, -6, -7, -6, -7, -8, -9, -10, -11, -10] [0, 1, 0, -1, 0, 1, 2, 3, 4, 5 … 5, 4, 5, 4, 3, 4, 5, 6, 7, 8] [0, 1, 0, 1, 2, 3, 4, 3, 2, 3 … 3, 2, 1, 0, 1, 0, -1, 0, -1, 0] [0, 1, 2, 1, 0, 1, 2, 3, 4, 3 … 3, 4, 3, 2, 1, 2, 1, 2, 3, 2] [0, 1, 2, 3, 2, 1, 2, 3, 4, 3 … 5, 4, 3, 2, 3, 4, 3, 4, 5, 4] [0, 1, 0, 1, 2, 3, 2, 3, 2, 3 … 3, 2, 1, 0, -1, -2, -3, -4, -3, -4] [0, 1, 2, 3, 4, 3, 2, 1, 2, 3 … 1, 2, 1, 2, 3, 4, 5, 4, 3, 4]
Convert Vector of Vectors into a matrix:
hcat(walkers[1], walkers[2])
21×2 Array{Int64,2}: 0 0 1 -1 2 0 1 1 0 2 -1 3 0 4 -1 3 -2 4 -1 5 0 4 1 3 0 2 1 3 0 2 -1 3 -2 4 -1 3 0 2 -1 1 -2 2
hcat(walkers) # wrong
10×1 Array{Array{Int64,1},2}: [0, 1, 2, 1, 0, -1, 0, -1, -2, -1 … 1, 0, 1, 0, -1, -2, -1, 0, -1, -2] [0, -1, 0, 1, 2, 3, 4, 3, 4, 5 … 3, 2, 3, 2, 3, 4, 3, 2, 1, 2] [0, -1, -2, -1, 0, -1, 0, 1, 2, 1 … 1, 2, 1, 0, -1, -2, -1, 0, -1, -2] [0, -1, 0, -1, -2, -1, -2, -3, -4, -5 … -7, -6, -7, -6, -7, -8, -9, -10, -11, -10] [0, 1, 0, -1, 0, 1, 2, 3, 4, 5 … 5, 4, 5, 4, 3, 4, 5, 6, 7, 8] [0, 1, 0, 1, 2, 3, 4, 3, 2, 3 … 3, 2, 1, 0, 1, 0, -1, 0, -1, 0] [0, 1, 2, 1, 0, 1, 2, 3, 4, 3 … 3, 4, 3, 2, 1, 2, 1, 2, 3, 2] [0, 1, 2, 3, 2, 1, 2, 3, 4, 3 … 5, 4, 3, 2, 3, 4, 3, 4, 5, 4] [0, 1, 0, 1, 2, 3, 2, 3, 2, 3 … 3, 2, 1, 0, -1, -2, -3, -4, -3, -4] [0, 1, 2, 3, 4, 3, 2, 1, 2, 3 … 1, 2, 1, 2, 3, 4, 5, 4, 3, 4]
hcat(walkers...) # "splat" # equiv to hcat(walkers[1], ..., walkers[10])
21×10 Array{Int64,2}: 0 0 0 0 0 0 0 0 0 0 1 -1 -1 -1 1 1 1 1 1 1 2 0 -2 0 0 0 2 2 0 2 1 1 -1 -1 -1 1 1 3 1 3 0 2 0 -2 0 2 0 2 2 4 -1 3 -1 -1 1 3 1 1 3 3 0 4 0 -2 2 4 2 2 2 2 -1 3 1 -3 3 3 3 3 3 1 -2 4 2 -4 4 2 4 4 2 2 -1 5 1 -5 5 3 3 3 3 3 0 4 2 -6 4 4 4 4 2 2 1 3 1 -7 5 3 3 5 3 1 0 2 2 -6 4 2 4 4 2 2 1 3 1 -7 5 1 3 3 1 1 0 2 0 -6 4 0 2 2 0 2 -1 3 -1 -7 3 1 1 3 -1 3 -2 4 -2 -8 4 0 2 4 -2 4 -1 3 -1 -9 5 -1 1 3 -3 5 0 2 0 -10 6 0 2 4 -4 4 -1 1 -1 -11 7 -1 3 5 -3 3 -2 2 -2 -10 8 0 2 4 -4 4
walkers_matrix = reduce(hcat, walkers) # more efficient when have a lot of vectors
21×10 Array{Int64,2}: 0 0 0 0 0 0 0 0 0 0 1 -1 -1 -1 1 1 1 1 1 1 2 0 -2 0 0 0 2 2 0 2 1 1 -1 -1 -1 1 1 3 1 3 0 2 0 -2 0 2 0 2 2 4 -1 3 -1 -1 1 3 1 1 3 3 0 4 0 -2 2 4 2 2 2 2 -1 3 1 -3 3 3 3 3 3 1 -2 4 2 -4 4 2 4 4 2 2 -1 5 1 -5 5 3 3 3 3 3 0 4 2 -6 4 4 4 4 2 2 1 3 1 -7 5 3 3 5 3 1 0 2 2 -6 4 2 4 4 2 2 1 3 1 -7 5 1 3 3 1 1 0 2 0 -6 4 0 2 2 0 2 -1 3 -1 -7 3 1 1 3 -1 3 -2 4 -2 -8 4 0 2 4 -2 4 -1 3 -1 -9 5 -1 1 3 -3 5 0 2 0 -10 6 0 2 4 -4 4 -1 1 -1 -11 7 -1 3 5 -3 3 -2 2 -2 -10 8 0 2 4 -4 4
walkers_matrix[1, end] # last element of first row
0
walkers_matrix[1:2, end] # last element of first and second rows
2-element Array{Int64,1}: 0 1
walkers_matrix[1:end, end] # last element of all rows
21-element Array{Int64,1}: 0 1 2 3 4 3 2 1 2 3 2 1 2 1 2 3 4 5 4 3 4
final_positions = walkers_matrix[:, end] # last element of first and second rows
21-element Array{Int64,1}: 0 1 2 3 4 3 2 1 2 3 2 1 2 1 2 3 4 5 4 3 4
walkers_matrix[end]
4
walkers = []
scatter(final_positions)
n = 20
walkers = [trajectory(n) for i in 1:1000]
final_positions = [traj[end] for traj in walkers];
scatter(final_positions)
using StatsBase # do have to install
counts = countmap(final_positions)
Dict{Int64,Int64} with 15 entries: 2 => 171 0 => 166 -4 => 123 10 => 18 8 => 32 6 => 78 -6 => 73 4 => 105 -8 => 37 -10 => 17 14 => 1 -12 => 6 -2 => 166 -14 => 1 12 => 6
scatter(counts)
n = 100
num_walkers = 10_000
walkers = [trajectory(n) for i in 1:num_walkers]
final_positions = [traj[end] for traj in walkers];
counts = countmap(final_positions)
scatter(counts)
countmap()