Skip to content

Instantly share code, notes, and snippets.

@pfitzseb
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfitzseb/4db6347cc91cacb89a19 to your computer and use it in GitHub Desktop.
Save pfitzseb/4db6347cc91cacb89a19 to your computer and use it in GitHub Desktop.

One session:

julia> function foo(z)
         c = 100
         max = 80
         for n = 1:max
           abs(z) ≥ 2 && return n-1
           z = z^2 + c
         end
         return max
       end
foo (generic function with 1 method)

julia> foo(x, y) = foo(x + y*im)
foo (generic function with 2 methods)

julia> foo_grid(n) =
         broadcast(foo,
                   linspace(-0.5, 1, n)',
                   linspace(-1, 0.5, n))
foo_grid (generic function with 1 method)

julia> foo_grid(10)
10x10 Array{Float64,2}:
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 
 julia> function foo(z)
         c = 1 + 1*im
         max = 80
         for n = 1:max
           abs(z) ≥ 2 && return n-1
           z = z^2 + c
         end
         return max
       end
foo (generic function with 2 methods)

julia> foo(x, y) = foo(x + y*im)
foo (generic function with 2 methods)

julia> foo_grid(n) =
         broadcast(foo,
                   linspace(-0.5, 1, n)',
                   linspace(-1, 0.5, n))
foo_grid (generic function with 1 method)

julia> foo_grid(10)
10x10 Array{Float64,2}:
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0

Another session:

julia> function foo(z)
         c = 1 + 1*im
         max = 80
         for n = 1:max
           abs(z) ≥ 2 && return n-1
           z = z^2 + c
         end
         return max
       end
foo (generic function with 1 method)

julia> foo(x, y) = foo(x + y*im)
foo (generic function with 2 methods)

julia> foo_grid(n) =
         broadcast(foo,
                   linspace(-0.5, 1, n)',
                   linspace(-1, 0.5, n))
foo_grid (generic function with 1 method)

julia> foo_grid(10)
10x10 Array{Float64,2}:
 1.0  2.0  5.0  80.0  3.0  3.0  3.0  3.0  3.0  80.0
 2.0  2.0  3.0   3.0  3.0  3.0  3.0  3.0  3.0   2.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  2.0   2.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  2.0   2.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  2.0   2.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  2.0   1.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  2.0   1.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  1.0   1.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  2.0  1.0   1.0
 2.0  2.0  2.0   2.0  2.0  2.0  2.0  1.0  1.0   1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment