Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Last active December 20, 2015 18:59
Show Gist options
  • Save johnmyleswhite/6179622 to your computer and use it in GitHub Desktop.
Save johnmyleswhite/6179622 to your computer and use it in GitHub Desktop.
Probabilistically guessing whether a complex function is holomorphic
# f(z = x + iy) = u(x, y) + i * v(x, y)
function finite_difference{T <: Complex}(f::Function,
z::T,
alongx::Bool = true,
alongu::Bool = true)
epsilon = sqrt(eps(max(abs(one(T)), abs(z))))
if alongx
zplusdz = z + epsilon
else
zplusdz = z + epsilon * im
end
if alongu
return real(f(zplusdz) - f(z)) / epsilon
else
return imag(f(zplusdz) - f(z)) / epsilon
end
end
function isholomorphic(f::Function, z::Complex)
cond1 = isapprox(finite_difference(f, z, true, true),
finite_difference(f, z, false, false))
cond2 = isapprox(finite_difference(f, z, true, false),
-finite_difference(f, z, false, true))
return cond1 && cond2
end
function isholomorphic(f::Function, N::Integer = 1000)
res = 0
for i in 1:N
z = randn() + randn() * im
res += int(isholomorphic(f, z))
end
return res / N
end
isholomorphic(sin) # => 0.979
isholomorphic(cos) # => 0.992
isholomorphic(abs) # => 0.0
@gabegaster
Copy link

Hi. I'm quite new to julia. Thought I would check it out because I saw your tweet. When I try to import this code, I get the following error:

julia> import holomorphic.jl
ERROR: syntax: malformed function argument (keywords (= (:: alongx Bool) true) (= (:: alongu Bool) true))
in include_from_node1 at loading.jl:76
in reload_path at loading.jl:96
in require at loading.jl:48
at .../holomorphic/6179622/holomorphic.jl:17

I am completely unfamiliar with julia syntax. Do you not get this error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment