Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Folder full of pertinent coursework

1666 views
Kernel: Julia
""" juliamap(c,z; maxiter) : Implement the iteration algorithm for a Julia Set. **Returns:** integer number of iterations, or zero if the iteration never diverges. - c : complex constant definining the set - z : complex number being iterated - maxiter : maximum iteration number, defaults to 100 """ function juliamap(c, z; maxiter=100) for n = 1:maxiter z = z^2 + c if abs(z) > 2 return n end end return 0 end @doc juliamap
juliamap(c,z; maxiter) : Implement the iteration algorithm for a Julia Set. **Returns:** integer number of iterations, or zero if the iteration never diverges. * c : complex constant definining the set * z : complex number being iterated * maxiter : maximum iteration number, defaults to 100
# Specialize juliamap to c=0 j0(z) = juliamap(0,z) # Vectorize j0 over arrays of Complex numbers @vectorize_1arg Complex j0 # List the available methods for j0 for different types methods(j0)
# 4 methods for generic function "j0": j0{T<:Complex{T<:Real}}(::AbstractArray{T<:Complex{T<:Real},1}) at operators.jl:380 j0{T<:Complex{T<:Real}}(::AbstractArray{T<:Complex{T<:Real},2}) at operators.jl:381 j0{T<:Complex{T<:Real}}(::AbstractArray{T<:Complex{T<:Real},N}) at operators.jl:383 j0(z) at In[2]:2
# Create a complex plane function complex_plane(xmin=-2, xmax=2, ymin=-2, ymax=2; xpoints=2000, ypoints=2000) # y is a column vector y = linspace(ymin, ymax, ypoints) # x uses a transpose, yielding a row vector x = linspace(xmin, xmax, xpoints)' # z uses broadcasted addition and multiplication to create a plane z = x .+ y.*im; # The final line of a block is treated as the return value, in the absence # of an explicit return statement end
complex_plane (generic function with 5 methods)
# The vectorized function can be applied directly to the plane @time cp = complex_plane() @time j0p = j0(cp)
0.904490 seconds (500.31 k allocations: 82.868 MB, 2.36% gc time)
2000x2000 Array{Int64,2}: 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1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1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
immutable ComplexPlane x :: LinSpace{Float64} y :: LinSpace{Float64} z :: Array{Complex{Float64},2} function ComplexPlane(xmin=-2, xmax=2, ymin=-2, ymax=2; xpoints=2000, ypoints=2000) x = linspace(xmin, xmax, xpoints) y = linspace(ymin, ymax, ypoints) z = x' .+ y.*im new(x,y,z) end end
1.285804 seconds (37.81 k allocations: 32.262 MB, 0.85% gc time)
cp = ComplexPlane(xpoints=200,ypoints=200); typeof(cp)
ComplexPlane
print(typeof(cp.x)) j0(cp.z)
LinSpace{
200x200 Array{Int64,2}: 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Pkg.add("PyPlot") # This downloads the package via git and installs it automatically Pkg.build("PyCall") using PyPlot # This loads the package into the current namespace j(z) = juliamap(-1.037 + 0.17im, z) @vectorize_1arg Complex j cp = ComplexPlane(1.4, 1.45, 0.0, 0.05, xpoints=2000, ypoints=2000) jp = j(cp.z); plt = pcolormesh(cp.x, cp.y, jp, cmap=PyPlot.cm_get_cmap("hot")) savefig("julia.png")
INFO: Nothing to be done INFO: Building PyCall INFO: PyCall is using /projects/sage/sage-6.9/local/bin/python (Python 2.7.9) at /projects/sage/sage-6.9/local/bin/python, libpython = /projects/sage/sage-6.9/local/lib/libpython2.7.so