text:
@testset "grids" begin
    TAs = ((Float64,  Array),
           (Float32,  Array),
           (BigFloat, Array))
    if CUDA.has_cuda_gpu()
        TAs = (TAs..., (Float32, CuArray))
    end

    for (T, A) in TAs
        verts = adapt(A, [SVector{2,T}(0, 0),
                          SVector{2,T}(1, 0),
                          SVector{2,T}(0, 1),
                          SVector{2,T}(1, 1),
                          SVector{2,T}(2, 0),
                          SVector{2,T}(2, 1)])
        conn = adapt(A, [SVector(1, 2, 3, 4),
                         SVector(2, 5, 4, 6)])
        cell = LobattoCell{T, A}(3, 4)
        grid = NodalGrid(cell, verts, conn)

        @test floattype(grid) == T
        @test arraytype(grid) <: A
        @test celltype(grid) == typeof(cell)
        @test Base.ndims(grid) == Base.ndims(conn)
        @test size(grid) == size(conn)
        @test length(grid) == length(conn)

        @test floattype(typeof(grid)) == T
        @test arraytype(typeof(grid)) <: A
        @test celltype(typeof(grid)) == typeof(cell)
        @test Base.ndims(typeof(grid)) == Base.ndims(conn)
        @test size(typeof(grid)) == size(conn)
        @test length(typeof(grid)) == length(conn)

        @test referencecell(grid) === cell
        @test vertices(grid) === verts
        @test connectivity(grid) === conn
        @test size(points(grid)) == (length(cell), length(conn))
        D = adapt(Array, derivatives(referencecell(grid)))
        x = adapt(Array, points(grid))
        @test D[1] * x ≈
            fill(SVector(one(T)/2, zero(T)), size(points(grid)))
        @test D[2] * x ≈
            fill(SVector(zero(T), one(T)/2), size(points(grid)))

        faceindices⁻, faceindices⁺ = adapt.(Array, faceindices(grid))
        x = adapt(Array, points(grid))
        @test isapprox(x[faceindices⁻], x[faceindices⁺], atol=10eps(T))

        if T != BigFloat
            mktempdir() do tmp
                outfiles = vtk_grid(joinpath(tmp, "grid_$(T)_$(A)"), grid;
                                    append=false, ascii=true) do vtk
                    vtk["Time"] = 37.0
                end
                for file in outfiles
                    @test EzXML.readxml(file) isa EzXML.Document
                end
            end
        end

        let
            verts = adapt(A, [
                              SVector{3,T}(0, 0, 0),
                              SVector{3,T}(0, 1, 0),
                              SVector{3,T}(1, 0, 0),
                              SVector{3,T}(1, 1, 0),
                              SVector{3,T}(2, 0, 0),
                              SVector{3,T}(2, 1, 0),
                              SVector{3,T}(3, 0, 0),
                              SVector{3,T}(3, 1, 0),
                              SVector{3,T}(4, 0, 0),
                              SVector{3,T}(4, 1, 0),
                              SVector{3,T}(5, 0, 0),
                              SVector{3,T}(5, 1, 0),
                              SVector{3,T}(6, 0, 0),
                              SVector{3,T}(6, 1, 0),
                              SVector{3,T}(7, 0, 0),
                              SVector{3,T}(7, 1, 0),
                              SVector{3,T}(8, 0, 0),
                              SVector{3,T}(8, 1, 0),
                              SVector{3,T}(9, 0, 0),
                              SVector{3,T}(9, 1, 0),
                              SVector{3,T}(0, 0, 1),
                              SVector{3,T}(0, 1, 1),
                              SVector{3,T}(1, 0, 1),
                              SVector{3,T}(1, 1, 1),
                              SVector{3,T}(2, 0, 1),
                              SVector{3,T}(2, 1, 1),
                              SVector{3,T}(3, 0, 1),
                              SVector{3,T}(3, 1, 1),
                              SVector{3,T}(4, 0, 1),
                              SVector{3,T}(4, 1, 1),
                              SVector{3,T}(5, 0, 1),
                              SVector{3,T}(5, 1, 1),
                              SVector{3,T}(6, 0, 1),
                              SVector{3,T}(6, 1, 1),
                              SVector{3,T}(7, 0, 1),
                              SVector{3,T}(7, 1, 1),
                              SVector{3,T}(8, 0, 1),
                              SVector{3,T}(8, 1, 1),
                              SVector{3,T}(9, 0, 1),
                              SVector{3,T}(9, 1, 1)
                             ])
            conn = adapt(A, [
                             SA[ 1,  2, 21, 22,  3,  4, 23, 24], # 0
                             SA[ 3,  4, 23, 24,  5,  6, 25, 26], # 1
                             SA[ 6, 26,  5, 25,  8, 28,  7, 27], # 2
                             SA[27,  7, 28,  8, 29,  9, 30, 10], # 3
                             SA[30, 29, 10,  9, 32, 31, 12, 11], # 4
                             SA[34, 14, 33, 13, 32, 12, 31, 11], # 5
                             SA[33, 13, 34, 14, 35, 15, 36, 16], # 6
                             SA[18, 17, 38, 37, 16, 15, 36, 35], # 7
                             SA[17, 18, 37, 38, 19, 20, 39, 40], # 8
                            ])
            cell = LobattoCell{T,A}(4,4,5)
            grid = NodalGrid(cell, verts, conn)
            faceindices⁻, faceindices⁺ = adapt.(Array, faceindices(grid))
            x = adapt(Array, points(grid))
            @test isapprox(x[faceindices⁻], x[faceindices⁺], atol=100eps(T))
            @test size(faces(grid)[1]) == (54, 46)
            @test size(faces(grid)[2]) == (108, 76)
            @test size(faces(grid)[3]) == (72, 40)
            @test boundaryfaces(grid) == adapt(A, [1  1  1  1  1  1  1  1  1
                                                   1  1  1  1  1  1  1  1  1
                                                   1  1  1  1  1  1  1  1  1
                                                   1  1  1  1  1  1  1  1  1
                                                   1  0  0  0  0  0  0  0  0
                                                   0  0  0  0  0  0  0  0  1])
        end

        let
            verts = adapt(A, [SVector{2,T}(1, 1),
                              SVector{2,T}(2, 1),
                              SVector{2,T}(3, 1),
                              SVector{2,T}(1, 2),
                              SVector{2,T}(2, 2),
                              SVector{2,T}(3, 2),
                              SVector{2,T}(1, 3),
                              SVector{2,T}(2, 3),
                              SVector{2,T}(3, 3)])
            conn = adapt(A, [
                             SA[1, 2, 4, 5],
                             SA[5, 2, 6, 3],
                             SA[5, 8, 4, 7],
                             SA[9, 8, 6, 5],
                            ])
            cell = LobattoCell{T,A}(4,4)
            grid = NodalGrid(cell, verts, conn)
            faceindices⁻, faceindices⁺ = adapt.(Array, faceindices(grid))
            x = adapt(Array, points(grid))
            @test isapprox(x[faceindices⁻], x[faceindices⁺], atol=10eps(T))
            @test size(faces(grid)[1]) == (16, 12)
            @test size(faces(grid)[2]) == (16, 9)
            @test boundaryfaces(grid) == adapt(A, [1  0  0  1
                                                   0  1  1  0
                                                   1  0  0  1
                                                   0  1  1  0])
        end

        let
            verts = adapt(A, [SVector{1,T}(1),
                              SVector{1,T}(2),
                              SVector{1,T}(3)])
            conn = adapt(A, [
                             SA[1, 2],
                             SA[3, 2],
                            ])
            cell = LobattoCell{T,A}(6)
            grid = NodalGrid(cell, verts, conn)
            faceindices⁻, faceindices⁺ = adapt.(Array, faceindices(grid))
            x = adapt(Array, points(grid))
            @test isapprox(x[faceindices⁻], x[faceindices⁺], atol=10eps(T))
            @test size(faces(grid)[1]) == (4, 3)
            @test boundaryfaces(grid) == adapt(A, [1  1
                                                   0  0])
        end
    end
end


text:
module test_async

using Test, HTTP, JSON

@time @testset "ASync" begin
    configs = [
        Pair{Symbol, Any}[:verbose => 0, :status_exception => false],
        Pair{Symbol, Any}[:verbose => 0, :status_exception => false, :reuse_limit => 200],
        Pair{Symbol, Any}[:verbose => 0, :status_exception => false, :reuse_limit => 50]
    ]
    protocols = ["http", "https"]

    dump_async_exception(e, st) = @error "async exception: " exception=(e, st)

    @testset "HTTP.request - Headers - $config - $protocol" for config in configs, protocol in protocols
        result = []

        @sync begin
            for i = 1:100
                @async try
                    response = HTTP.request("GET", "$protocol://httpbin.org/headers", ["i" => i]; config...)
                    if response.status != 200
                        @error "non-200 response" response=response
                    else
                        response = JSON.parse(String(response.body))
                        push!(result, response["headers"]["I"] => string(i))
                    end
                catch e
                    dump_async_exception(e, stacktrace(catch_backtrace()))
                    rethrow(e)
                end
            end
        end

        for(a, b) in result
            @test a == b
        end

        HTTP.ConnectionPool.closeall()
    end

    @testset "HTTP.request - Body - $config - $protocol" for config in configs, protocol in protocols
        result = []

        @sync begin
            for i=1:100
                @async try
                    response = HTTP.request("GET", "$protocol://httpbin.org/stream/$i"; config...)
                    if response.status != 200
                        @error "non-200 response" response=response
                    else
                        response = String(response.body)
                        response = split(strip(response), "\n")
                        push!(result, length(response) => i)
                    end
                catch e
                    dump_async_exception(e, stacktrace(catch_backtrace()))
                    rethrow(e)
                end
            end
        end

        for (a,b) in result
            @test a == b
        end

        HTTP.ConnectionPool.closeall()
    end

    @testset "HTTP.open - $config - $protocol" for config in configs, protocol in protocols
        result = []

        @sync begin
            for i=1:100
                @async try
                    open_response = nothing
                    url = "$protocol://httpbin.org/stream/$i"

                    response = HTTP.open("GET", url; config...) do http
                        open_response = String(read(http))
                    end

                    if response.status != 200
                        @error "non-200 response" response=response
                    else
                        open_response = split(strip(open_response), "\n")
                        push!(result, length(open_response) => i)
                    end
                catch e
                    dump_async_exception(e, stacktrace(catch_backtrace()))
                    rethrow(e)
                end
            end
        end

        for (a,b) in result
            @test a == b
        end

        HTTP.ConnectionPool.closeall()
    end

    @testset "HTTP.request - Response Stream - $config - $protocol" for config in configs, protocol in protocols
        result = []

        @sync begin
            for i=1:100
                @async try
                    stream_response = nothing

                    # Note: url for $i will give back $i responses split on "\n"
                    url = "$protocol://httpbin.org/stream/$i"

                    try
                        stream = Base.BufferStream()
                        response = HTTP.request("GET", url; response_stream=stream, config...)
                        close(stream)

                        if response.status != 200
                            @error "non-200 response" response=response
                        else
                            stream_response = String(read(stream))
                            stream_response = split(strip(stream_response), "\n")
                            if length(stream_response) != i
                                @show join(stream_response, "\n")
                            end
                            push!(result, length(stream_response) => i)
                        end
                    catch e
                        if !HTTP.RetryRequest.isrecoverable(e)
                            rethrow(e)
                        end
                    end
                catch e
                    dump_async_exception(e, stacktrace(catch_backtrace()))
                    rethrow(e)
                end
            end
        end

        for (a,b) in result
            @test a == b
        end

        HTTP.ConnectionPool.closeall()
    end
end

end # module

ext: jl


text:
# This file is a part of Julia. License is MIT: https://julialang.org/license

@test reim(2 + 3im) == (2, 3)

for T in (Int64, Float64)
    @test real(T) == T
    @test real(Complex{T}) == T
    @test complex(T) == Complex{T}
    @test complex(Complex{T}) == Complex{T}
end

#showcompact
@test sprint(showcompact, complex(1, 0)) == "1+0im"
@test sprint(show, complex(true, true)) == "Complex(true,true)"

@testset "arithmetic" begin
    @testset for T in (Float16, Float32, Float64, BigFloat)
        t = true
        f = false

        @testset "add and subtract" begin
            @test isequal(T(+0.0) + im, Complex(T(+0.0), T(+1.0)))
            @test isequal(T(-0.0) + im, Complex(T(-0.0), T(+1.0)))
            @test isequal(T(+0.0) - im, Complex(T(+0.0), T(-1.0)))
            @test isequal(T(-0.0) - im, Complex(T(-0.0), T(-1.0)))
            @test isequal(T(+1.0) + im, Complex(T(+1.0), T(+1.0)))
            @test isequal(T(-1.0) + im, Complex(T(-1.0), T(+1.0)))
            @test isequal(T(+1.0) - im, Complex(T(+1.0), T(-1.0)))
            @test isequal(T(-1.0) - im, Complex(T(-1.0), T(-1.0)))
            @test isequal(im + T(+0.0), Complex(T(+0.0), T(+1.0)))
            @test isequal(im + T(-0.0), Complex(T(-0.0), T(+1.0)))
            @test isequal(im - T(+0.0), Complex(T(+0.0), T(+1.0)))
            @test isequal(im - T(-0.0), Complex(T(+0.0), T(+1.0)))
            @test isequal(im + T(+1.0), Complex(T(+1.0), T(+1.0)))
            @test isequal(im + T(-1.0), Complex(T(-1.0), T(+1.0)))
            @test isequal(im - T(+1.0), Complex(T(-1.0), T(+1.0)))
            @test isequal(im - T(-1.0), Complex(T(+1.0), T(+1.0)))
            @test isequal(T(f) + im, Complex(T(+0.0), T(+1.0)))
            @test isequal(T(t) + im, Complex(T(+1.0), T(+1.0)))
            @test isequal(T(f) - im, Complex(T(+0.0), T(-1.0)))
            @test isequal(T(t) - im, Complex(T(+1.0), T(-1.0)))
            @test isequal(im + T(f), Complex(T(+0.0), T(+1.0)))
            @test isequal(im + T(t), Complex(T(+1.0), T(+1.0)))
            @test isequal(im - T(f), Complex(T(+0.0), T(+1.0)))
            @test isequal(im - T(t), Complex(T(-1.0), T(+1.0)))
        end

        @testset "multiply" begin
            @test isequal(T(+0.0) * im, Complex(T(+0.0), T(+0.0)))
            @test isequal(T(-0.0) * im, Complex(T(-0.0), T(-0.0)))
            @test isequal(T(+1.0) * im, Complex(T(+0.0), T(+1.0)))
            @test isequal(T(-1.0) * im, Complex(T(-0.0), T(-1.0)))
            @test isequal(im * T(+0.0), Complex(T(+0.0), T(+0.0)))
            @test isequal(im * T(-0.0), Complex(T(-0.0), T(-0.0)))
            @test isequal(im * T(+1.0), Complex(T(+0.0), T(+1.0)))
            @test isequal(im * T(-1.0), Complex(T(-0.0), T(-1.0)))
        end

        @testset "divide" begin
            @test isequal(T(+0.0) / im, Complex(T(+0.0), T(-0.0)))
            @test isequal(T(-0.0) / im, Complex(T(-0.0), T(+0.0)))
            @test isequal(T(+1.0) / im, Complex(T(+0.0), T(-1.0)))
            @test isequal(T(-1.0) / im, Complex(T(-0.0), T(+1.0)))
        end
    end
    @test isequal(true + complex(true,false), complex(true,false) + complex(true,false))
    @test isequal(complex(true,false) + true, complex(true,false) + complex(true,false))
    @test isequal(true - complex(true,false), complex(true,false) - complex(true,false))
    @test isequal(complex(true,false) - true, complex(true,false) - complex(true,false))
    @test isequal(true * complex(true,false), complex(true,false) * complex(true,false))
    @test isequal(complex(true,false) * true, complex(true,false) * complex(true,false))
end

@testset "basic math functions" begin
    # We compare to BigFloat instead of hard-coding
    # values, assuming that BigFloat has an independent and independently
    # tested implementation.
    @testset for T in (Float32, Float64)
        x = Complex{T}(1//3 + 1//4*im)
        y = Complex{T}(1//2 + 1//5*im)
        yi = 4
        @testset "Random values" begin
            @test x^y ≈ big(x)^big(y)
            @test x^yi ≈ big(x)^yi
            @test x^true ≈ big(x)^true
            @test x^false ≈ big(x)^false
            @test x^1 ≈ big(x)^1
            @test abs(x) ≈ abs(big(x))
            @test abs2(x) ≈ abs2(big(x))
            @test acos(x) ≈ acos(big(x))
            @test acosh(1+x) ≈ acosh(1+big(x))
            @test angle(x) ≈ angle(big(x))
            @test asin(x) ≈ asin(big(x))
            @test asinh(x) ≈ asinh(big(x))
            @test atan(x) ≈ atan(big(x))
            @test atanh(x) ≈ atanh(big(x))
            @test cis(real(x)) ≈ cis(real(big(x)))
            @test cis(x) ≈ cis(big(x))
            @test cos(x) ≈ cos(big(x))
            @test cosh(x) ≈ cosh(big(x))
            @test exp(x) ≈ exp(big(x))
            @test exp10(x) ≈ exp10(big(x))
            @test exp2(x) ≈ exp2(big(x))
            @test expm1(x) ≈ expm1(big(x)) atol=eps(T)
            @test log(x) ≈ log(big(x))
            @test log10(x) ≈ log10(big(x))
            @test log1p(x) ≈ log1p(big(x))
            @test log2(x) ≈ log2(big(x))
            @test sin(x) ≈ sin(big(x))
            @test sinh(x) ≈ sinh(big(x))
            @test sqrt(x) ≈ sqrt(big(x))
            @test tan(x) ≈ tan(big(x))
            @test tanh(x) ≈ tanh(big(x))
        end
        @testset "Inverses" begin
            @test acos(cos(x)) ≈ x
            @test acosh(cosh(x)) ≈ x
            @test asin(sin(x)) ≈ x
            @test asinh(sinh(x)) ≈ x
            @test atan(tan(x)) ≈ x
            @test atanh(tanh(x)) ≈ x
            @test cos(acos(x)) ≈ x
            @test cosh(acosh(1+x)) ≈ 1+x
            @test exp(log(x)) ≈ x
            @test exp10(log10(x)) ≈ x
            @test exp2(log2(x)) ≈ x
            @test expm1(log1p(x)) ≈ x
            @test log(exp(x)) ≈ x
            @test log10(exp10(x)) ≈ x
            @test log1p(expm1(x)) ≈ x
            @test log2(exp2(x)) ≈ x
            @test sin(asin(x)) ≈ x
            @test sinh(asinh(x)) ≈ x
            @test sqrt(x)^2 ≈ x
            @test sqrt(x^2) ≈ x
            @test tan(atan(x)) ≈ x
            @test tanh(atanh(x)) ≈ x
        end
        @testset "Relations between functions" begin
            @test cosh(x) ≈ (exp(x)+exp(-x))/2
            @test cosh(x)^2-sinh(x)^2 ≈ 1
            @test sin(x)^2+cos(x)^2 ≈ 1
            @test sinh(x) ≈ (exp(x)-exp(-x))/2
            @test tan(x) ≈ sin(x)/cos(x)
            @test tanh(x) ≈ sinh(x)/cosh(x)
        end
    end
end

@testset "isinf" begin
    @test iszero(real(complex(0.0,1.0))) # isimag deprecated
    @test !iszero(real(complex(1.0,1.0))) # isimag deprecated
    @test isinf(complex(Inf,0))
    @test isinf(complex(-Inf,0))
    @test isinf(complex(0,Inf))
    @test isinf(complex(0,-Inf))
    @test !isinf(complex(0,0))
end

@testset "flipsign" begin
    @test isequal(complex( 0.0, 0.0 ), flipsign(complex( 0.0, 0.0 ), 1))
    @test isequal(complex( -0.0, -0.0 ), flipsign(complex( 0.0, 0.0 ), -1))
    @test isequal(complex( Inf, 0.0 ), flipsign(complex( Inf, 0.0 ), 1))
    @test isequal(complex( -Inf, -0.0 ), flipsign(complex( Inf, 0.0 ), -1))
    @test isequal(complex( 0.0, NaN ), flipsign(complex( 0.0, NaN ), 1.0))
    @test isequal(complex( -0.0, NaN ), flipsign(complex( 0.0, NaN ), -1.0))

    @test isequal(complex( 5.0, 4.0 ), flipsign(complex(-5.0, -4.0), -1))
    @test isequal(complex( 0.5, -0.5 ), flipsign(complex(-0.5, 0.5), -2))
end

@testset "sqrt" begin
    # tests special values from csqrt man page
    # as well as conj(sqrt(z)) = sqrt(conj(z))
    @test isequal(sqrt(complex( 0.0, 0.0)), complex( 0.0, 0.0))
    @test isequal(sqrt(complex( 0.0,-0.0)), complex( 0.0,-0.0))
    @test isequal(sqrt(complex( 0.0, Inf)), complex( Inf, Inf))
    @test isequal(sqrt(complex( 0.0,-Inf)), complex( Inf,-Inf))
    @test isequal(sqrt(complex( 0.0, NaN)), complex( NaN, NaN))
    @test isequal(sqrt(complex(-0.0, 0.0)), complex( 0.0, 0.0))
    @test isequal(sqrt(complex(-0.0,-0.0)), complex( 0.0,-0.0))

    @test isequal(sqrt(complex( 5.0, 0.0)), complex(sqrt(5.0), 0.0))
    @test isequal(sqrt(complex( 5.0,-0.0)), complex(sqrt(5.0),-0.0))
    @test isequal(sqrt(complex(-5.0, 0.0)), complex( 0.0, sqrt(5.0)))
    @test isequal(sqrt(complex(-5.0,-0.0)), complex( 0.0,-sqrt(5.0)))

    @test isequal(sqrt(complex( Inf, 0.0)), complex( Inf, 0.0))
    @test isequal(sqrt(complex( Inf,-0.0)), complex( Inf,-0.0))
    @test isequal(sqrt(complex( Inf, 5.0)), complex( Inf, 0.0))
    @test isequal(sqrt(complex( Inf,-5.0)), complex( Inf,-0.0))
    @test isequal(sqrt(complex( Inf, Inf)), complex( Inf, Inf))
    @test isequal(sqrt(complex( Inf,-Inf)), complex( Inf,-Inf))
    @test isequal(sqrt(complex( Inf, NaN)), complex( Inf, NaN))

    @test isequal(sqrt(complex(-Inf, 0.0)), complex( 0.0, Inf))
    @test isequal(sqrt(complex(-Inf,-0.0)), complex( 0.0,-Inf))
    @test isequal(sqrt(complex(-Inf, 5.0)), complex( 0.0, Inf))
    @test isequal(sqrt(complex(-Inf,-5.0)), complex( 0.0,-Inf))
    @test isequal(sqrt(complex(-Inf, Inf)), complex( Inf, Inf))
    @test isequal(sqrt(complex(-Inf,-Inf)), complex( Inf,-Inf))
    @test isequal(sqrt(complex(-Inf, NaN)), complex( NaN, Inf))

    @test isequal(sqrt(complex( NaN, 0.0)), complex( NaN, NaN))
    @test isequal(sqrt(complex( NaN, 0.0)), complex( NaN, NaN))
    @test isequal(sqrt(complex( NaN, Inf)), complex( Inf, Inf))
    @test isequal(sqrt(complex( NaN,-Inf)), complex( Inf,-Inf))
end

@testset "log(conj(z)) == conj(log(z))" begin
    @test isequal(log(complex( 0.0, 0.0)), complex(-Inf, 0.0))
    @test isequal(log(complex( 0.0,-0.0)), complex(-Inf,-0.0))
    @test isequal(log(complex( 0.0, 1.0)), complex( 0.0, pi/2))
    @test isequal(log(complex( 0.0,-1.0)), complex( 0.0,-pi/2))
    @test isequal(log(complex( 0.0, Inf)), complex( Inf, pi/2))
    @test isequal(log(complex( 0.0,-Inf)), complex( Inf,-pi/2))
    @test isequal(log(complex( 0.0, NaN)), complex( NaN, NaN))
    @test isequal(log(complex(-0.0, 0.0)), complex(-Inf, pi))
    @test isequal(log(complex(-0.0,-0.0)), complex(-Inf,-pi))

    @test isequal(log(complex( 5.0, 0.0)),complex(log(5.0), 0.0))
    @test isequal(log(complex( 5.0,-0.0)),complex(log(5.0),-0.0))

    @test isequal(log(complex( Inf, 5.0)), complex( Inf, 0.0))
    @test isequal(log(complex( Inf,-5.0)), complex( Inf,-0.0))
    @test isequal(log(complex( Inf, Inf)), complex( Inf, pi/4))
    @test isequal(log(complex( Inf,-Inf)), complex( Inf,-pi/4))
    @test isequal(log(complex( Inf, NaN)), complex( Inf, NaN))

    @test isequal(log(complex(-Inf, 5.0)), complex( Inf, pi))
    @test isequal(log(complex(-Inf,-5.0)), complex( Inf,-pi))
    @test isequal(log(complex(-Inf, Inf)), complex( Inf, 3*pi/4))
    @test isequal(log(complex(-Inf,-Inf)), complex( Inf,-3*pi/4))
    @test isequal(log(complex(-Inf, NaN)), complex( Inf, NaN))

    @test isequal(log(complex( NaN, 0.0)), complex( NaN, NaN))
    @test isequal(log(complex( NaN, Inf)), complex( Inf, NaN))
    @test isequal(log(complex( NaN,-Inf)), complex( Inf, NaN))
    @test isequal(log(complex( NaN, NaN)), complex( NaN, NaN))
end

@testset "exp(conj(z)) == conj(exp(z))" begin
    @test isequal(exp(complex( 0.0, 0.0)), complex(1.0, 0.0))
    @test isequal(exp(complex( 0.0,-0.0)), complex(1.0,-0.0))
    @test isequal(exp(complex( 0.0, Inf)), complex(NaN, NaN))
    @test isequal(exp(complex( 0.0,-Inf)), complex(NaN, NaN))
    @test isequal(exp(complex( 0.0, NaN)), complex(NaN, NaN))

    @test isequal(exp(complex(-0.0, 0.0)), complex(1.0, 0.0))
    @test isequal(exp(complex(-0.0,-0.0)), complex(1.0,-0.0))

    @test isequal(exp(complex( 5.0, Inf)), complex(NaN, NaN))

    @test isequal(exp(complex( Inf, 0.0)), complex(Inf, 0.0))
    @test isequal(exp(complex( Inf,-0.0)), complex(Inf,-0.0))
    @test isequal(exp(complex( Inf, 5.0)), complex(cos(5.0)*Inf,sin(5.0)* Inf))
    @test isequal(exp(complex( Inf,-5.0)), complex(cos(5.0)*Inf,sin(5.0)*-Inf))
    @test isequal(exp(complex( Inf, NaN)), complex(-Inf, NaN))
    @test isequal(exp(complex( Inf, Inf)), complex(-Inf, NaN))
    @test isequal(exp(complex( Inf,-Inf)), complex(-Inf, NaN))

    @test isequal(exp(complex(-Inf, 0.0)), complex(0.0, 0.0))
    @test isequal(exp(complex(-Inf,-0.0)), complex(0.0,-0.0))
    @test isequal(exp(complex(-Inf, 5.0)), complex(cos(5.0)*0.0,sin(5.0)* 0.0))
    @test isequal(exp(complex(-Inf,-5.0)), complex(cos(5.0)*0.0,sin(5.0)*-0.0))
    @test isequal(exp(complex(-Inf, Inf)), complex(-0.0, 0.0))
    @test isequal(exp(complex(-Inf,-Inf)), complex(-0.0,-0.0))
    @test isequal(exp(complex(-Inf, NaN)), complex(-0.0, 0.0))

    @test isequal(exp(complex( NaN, 0.0)), complex( NaN, 0.0))
    @test isequal(exp(complex( NaN,-0.0)), complex( NaN,-0.0))
    @test isequal(exp(complex( NaN, 5.0)), complex( NaN, NaN))
    @test isequal(exp(complex( NaN, NaN)), complex( NaN, NaN))
end

@testset "expm1(conj(z)) == conj(expm1(z))" begin
    @test isequal(expm1(complex( 0.0, 0.0)), complex(0.0, 0.0))
    @test isequal(expm1(complex( 0.0,-0.0)), complex(0.0,-0.0))
    @test isequal(expm1(complex( 0.0, Inf)), complex(NaN, NaN))
    @test isequal(expm1(complex( 0.0,-Inf)), complex(NaN, NaN))
    @test isequal(expm1(complex( 0.0, NaN)), complex(NaN, NaN))

    @test isequal(expm1(complex(-0.0, 0.0)), complex(-0.0, 0.0))
    @test isequal(expm1(complex(-0.0,-0.0)), complex(-0.0,-0.0))

    @test isequal(expm1(complex( 5.0, Inf)), complex(NaN, NaN))

    @test isequal(expm1(complex( Inf, 0.0)), complex(Inf, 0.0))
    @test isequal(expm1(complex( Inf,-0.0)), complex(Inf,-0.0))
    @test isequal(expm1(complex( Inf, 5.0)), complex(cos(5.0)*Inf,sin(5.0)* Inf))
    @test isequal(expm1(complex( Inf,-5.0)), complex(cos(5.0)*Inf,sin(5.0)*-Inf))
    @test isequal(expm1(complex( Inf, NaN)), complex(-Inf, NaN))
    @test isequal(expm1(complex( Inf, Inf)), complex(-Inf, NaN))
    @test isequal(expm1(complex( Inf,-Inf)), complex(-Inf, NaN))

    @test isequal(expm1(complex(-Inf, 0.0)), complex(-1.0, 0.0))
    @test isequal(expm1(complex(-Inf,-0.0)), complex(-1.0,-0.0))
    @test isequal(expm1(complex(-Inf, 5.0)), complex(-1.0,sin(5.0)* 0.0))
    @test isequal(expm1(complex(-Inf,-5.0)), complex(-1.0,sin(5.0)*-0.0))
    @test isequal(expm1(complex(-Inf, Inf)), complex(-1.0, 0.0))
    @test isequal(expm1(complex(-Inf,-Inf)), complex(-1.0,-0.0))
    @test isequal(expm1(complex(-Inf, NaN)), complex(-1.0, 0.0))

    @test isequal(expm1(complex( NaN, 0.0)), complex( NaN, 0.0))
    @test isequal(expm1(complex( NaN,-0.0)), complex( NaN,-0.0))
    @test isequal(expm1(complex( NaN, 5.0)), complex( NaN, NaN))
    @test isequal(expm1(complex( NaN, NaN)), complex( NaN, NaN))

    @test isequal(expm1(complex( 1e-20, 0.0)), complex(expm1( 1e-20), 0.0))
    @test isequal(expm1(complex(-1e-20, 0.0)), complex(expm1(-1e-20), 0.0))

    @test expm1(complex( 1e-20, 1e-10)) ≈ complex( 5e-21, 1e-10)
    @test expm1(complex( 1e-20,-1e-10)) ≈ complex( 5e-21,-1e-10)
    @test expm1(complex(-1e-20, 1e-10)) ≈ complex(-1.5e-20, 1e-10)
    @test expm1(complex(-1e-20,-1e-10)) ≈ complex(-1.5e-20,-1e-10)

    @test expm1(complex( 10.0, 10.0)) ≈ exp(complex( 10.0, 10.0))-1
    @test expm1(complex( 10.0,-10.0)) ≈ exp(complex( 10.0,-10.0))-1
    @test expm1(complex(-10.0, 10.0)) ≈ exp(complex(-10.0, 10.0))-1
    @test expm1(complex(-10.0,-10.0)) ≈ exp(complex(-10.0,-10.0))-1
end

import Base.Math.@horner
@testset "log1p" begin
    @test isequal(log1p(complex(Inf, 3)), complex(Inf, +0.))
    @test isequal(log1p(complex(Inf, -3)), complex(Inf, -0.))
    @test isequal(log1p(complex(-Inf, 3)), complex(Inf, +pi))
    @test isequal(log1p(complex(-Inf, -3)), complex(Inf, -pi))
    @test isequal(log1p(complex(Inf, NaN)), complex(Inf, NaN))
    @test isequal(log1p(complex(NaN, 0)), complex(NaN, NaN))
    @test isequal(log1p(complex(0, NaN)), complex(NaN, NaN))
    @test isequal(log1p(complex(-1, +0.)), complex(-Inf, +0.))
    @test isequal(log1p(complex(-1, -0.)), complex(-Inf, -0.))
    @test isequal(log1p(complex(-2, 1e-10)), log(1 + complex(-2, 1e-10)))
    @test isequal(log1p(complex(1, Inf)), complex(Inf, pi/2))
    @test isequal(log1p(complex(1, -Inf)), complex(Inf, -pi/2))

    for z in (1e-10+1e-9im, 1e-10-1e-9im, -1e-10+1e-9im, -1e-10-1e-9im)
        @test log1p(z) ≈ @horner(z, 0, 1, -0.5, 1/3, -0.25, 0.2)
    end
    for z in (15+4im, 0.2+3im, 0.08-0.9im)
        @test log1p(z) ≈ log(1+z)
    end
end

@testset "^ (cpow)" begin
    # equivalent to exp(y*log(x))
    #   except for 0^0?
    # conj(x)^conj(y) = conj(x^y)
    @test isequal(complex( 0.0, 0.0)^complex( 0.0, 0.0), complex(1.0, 0.0))
    @test isequal(complex( 0.0, 0.0)^complex( 0.0,-0.0), complex(1.0, 0.0))
    @test isequal(complex( 0.0, 0.0)^complex(-0.0, 0.0), complex(1.0,-0.0))
    @test isequal(complex( 0.0, 0.0)^complex(-0.0,-0.0), complex(1.0,-0.0))

    @test isequal(complex( 0.0,-0.0)^complex( 0.0, 0.0), complex(1.0,-0.0))
    @test isequal(complex( 0.0,-0.0)^complex( 0.0,-0.0), complex(1.0,-0.0))
    @test isequal(complex( 0.0,-0.0)^complex(-0.0, 0.0), complex(1.0, 0.0))
    @test isequal(complex( 0.0,-0.0)^complex(-0.0,-0.0), complex(1.0, 0.0))

    @test isequal(complex(-0.0, 0.0)^complex( 0.0, 0.0), complex(1.0, 0.0))
    @test isequal(complex(-0.0, 0.0)^complex( 0.0,-0.0), complex(1.0, 0.0))
    @test isequal(complex(-0.0, 0.0)^complex(-0.0, 0.0), complex(1.0,-0.0))
    @test isequal(complex(-0.0, 0.0)^complex(-0.0,-0.0), complex(1.0,-0.0))

    @test isequal(complex(-0.0,-0.0)^complex( 0.0, 0.0), complex(1.0,-0.0))
    @test isequal(complex(-0.0,-0.0)^complex( 0.0,-0.0), complex(1.0,-0.0))
    @test isequal(complex(-0.0,-0.0)^complex(-0.0, 0.0), complex(1.0, 0.0))
    @test isequal(complex(-0.0,-0.0)^complex(-0.0,-0.0), complex(1.0, 0.0))

    @test complex(0.0,1.0)^complex(2.0,0) ≈ complex(-1.0, 0.0)
    @test complex(1.0,2.0)^complex(3.0,0) ≈ complex(-11.0, -2.0)

    @test isequal(complex(0.0,0.0)^false, complex(1.0,0.0))
    @test isequal(complex(0.0,0.0)^0, complex(1.0,0.0))
end

@testset "sinh and sin" begin
    # sinh: has properties
    #  sinh(conj(z)) = conj(sinh(z))
    #  sinh(-z) = -sinh(z)

    # sin: defined in terms of sinh
    #  sin(z) = -i*sinh(i*z)
    #  i.e. if sinh(a+ib) = x+iy
    #    then  sin(b-ia) = y-ix
    #  sin(conj(z)) = conj(sin(z))
    #  sin(-z) = -sin(z)

    # @test isequal(sin(complex( 0, 10000)),complex( 0.0, Inf))
    # @test isequal(sin(complex( 0,-10000)),complex( 0.0,-Inf))
    for (x,y) in [(complex( 0.0, 0.0), complex( 0.0, 0.0)),
                  (complex( 0.0, Inf), complex( 0.0, NaN)),
                  (complex( 0.0, NaN), complex( 0.0, NaN)),
                  (complex( 7.2, Inf), complex( NaN, NaN)),
                  (complex( 7.2, NaN), complex( NaN, NaN)),
                  (complex( 7.2, 0.0), complex( sinh(7.2), 0.0)),
                  (complex( 1e5, 0.0), complex( sinh(1e5), 0.0)),
                  (complex( Inf, 0.0), complex( Inf, 0.0)),
                  (complex( Inf, 7.2), Inf*cis(7.2)),
                  (complex( Inf, Inf), complex( Inf, NaN)),
                  (complex( Inf, NaN), complex( Inf, NaN)),
                  (complex( NaN, 0.0), complex( NaN, 0.0)),
                  (complex( NaN, 7.2), complex( NaN, NaN)),
                  (complex( NaN, NaN), complex( NaN, NaN)),
                  ]
        @test isequal(sinh(x), y)
        @test isequal(sinh(conj(x)), conj(y))
        @test isequal(sinh(-x), -y)
        @test isequal(sinh(-conj(x)), -conj(y))

        xx = complex(imag(x),-real(x))
        yy = complex(imag(y),-real(y))

        @test isequal(sin(xx),yy)
        @test isequal(sin(conj(xx)), conj(yy))
        @test isequal(sin(-xx), -yy)
        @test isequal(sin(-conj(xx)), -conj(yy))

        yyy = sin(pi*xx)
        @test isequal(sinpi(xx), yyy)
        @test isequal(sinpi(conj(xx)),conj(yyy))
        @test isequal(sinpi(-xx),-yyy)
        @test isequal(sinpi(-conj(xx)),-conj(yyy))
    end
end

@testset "cosh and cos" begin
    # cosh: has properties
    #  cosh(conj(z)) = conj(cosh(z))
    #  coshh(-z) = cosh(z)

    # cos
    #  cos(z) = cosh(iz)
    #   i.e cos(b-ia) = cosh(a+ib)
    #   and cos(b+ia) = cosh(a-ib)
    #  cos(conj(z)) = conj(cos(z))
    #  cos(-z) = cos(z)
    for (x,y) in [(complex( 0.0, 0.0), complex( 1.0, 0.0)),
                  (complex( 0.0, Inf), complex( NaN, 0.0)),
                  (complex( 0.0, NaN), complex( NaN, 0.0)),
                  (complex( 7.2, Inf), complex( NaN, NaN)),
                  (complex( 7.2, NaN), complex( NaN, NaN)),
                  (complex( 7.2, 0.0), complex( cosh(7.2), 0.0)),
                  (complex( 1e5, 0.0), complex( Inf, 0.0)),
                  (complex( Inf, 0.0), complex( Inf, 0.0)),
                  (complex( Inf, 7.2), Inf*cis(7.2)),
                  (complex( Inf, Inf), complex( Inf, NaN)),
                  (complex( Inf, NaN), complex( Inf, NaN)),
                  (complex( NaN, 0.0), complex( NaN, 0.0)),
                  (complex( NaN, 7.2), complex( NaN, NaN)),
                  (complex( NaN, NaN), complex( NaN, NaN)),
                  ]
        undef_sign = isequal(x,complex( NaN, 0.0)) || isequal(x,complex( 0.0, NaN))

        @test isequal(cosh(x), y)
        if !undef_sign
            @test isequal(cosh(conj(x)), conj(y))
            @test isequal(cosh(-x), y)
            @test isequal(cosh(-conj(x)), conj(y))
        end

        xx = complex(imag(x),-real(x))
        yy = y
        @test isequal(cos(xx),yy)
        if !undef_sign
            @test isequal(cos(conj(xx)), conj(yy))
            @test isequal(cos(-xx), yy)
            @test isequal(cos(-conj(xx)), conj(yy))
        end

        yyy = cos(pi*xx)
        @test isequal(cospi(xx), yyy)
        if !undef_sign
            @test isequal(cospi(conj(xx)), conj(yyy))
            @test isequal(cospi(-xx), yyy)
            @test isequal(cospi(-conj(xx)), conj(yyy))
        end
    end
end

@testset "tanh(op(z)) == op(tanh(z)) for op in (conj, -)" begin
    @test isequal(tanh(complex( 0, 0)),complex(0.0,0.0)) #integer fallback
    @test isequal(tanh(complex( 0.0, 0.0)),complex(0.0,0.0))
    @test isequal(tanh(complex( 0.0,-0.0)),complex(0.0,-0.0))
    @test_throws DomainError  tanh(complex( 0.0, Inf))
    @test_throws DomainError  tanh(complex( 0.0,-Inf))
    @test isequal(tanh(complex( 0.0, NaN)),complex(NaN,NaN))

    @test isequal(tanh(complex(-0.0, 0.0)),complex(-0.0,0.0))
    @test isequal(tanh(complex(-0.0,-0.0)),complex(-0.0,-0.0))

    @test_throws DomainError  tanh(complex( 5.0, Inf))
    @test isequal(tanh(complex( 5.0, NaN)),complex(NaN,NaN))

    @test isequal(tanh(complex( Inf, 0.0)),complex(1.0, 0.0))
    @test isequal(tanh(complex( Inf,-0.0)),complex(1.0,-0.0))
    @test isequal(tanh(complex( Inf, 5.0)),complex(1.0,sin(2*5.0)* 0.0))
    @test isequal(tanh(complex( Inf,-5.0)),complex(1.0,sin(2*5.0)*-0.0))
    @test isequal(tanh(complex( Inf, Inf)),complex(1.0, 0.0))
    @test isequal(tanh(complex( Inf,-Inf)),complex(1.0,-0.0))
    @test isequal(tanh(complex( Inf, NaN)),complex(1.0, 0.0))

    @test isequal(tanh(complex(-Inf, 0.0)),complex(-1.0, 0.0))
    @test isequal(tanh(complex(-Inf,-0.0)),complex(-1.0,-0.0))
    @test isequal(tanh(complex(-Inf, 5.0)),complex(-1.0,sin(2*5.0)* 0.0))
    @test isequal(tanh(complex(-Inf,-5.0)),complex(-1.0,sin(2*5.0)*-0.0))
    @test isequal(tanh(complex(-Inf, Inf)),complex(-1.0, 0.0))
    @test isequal(tanh(complex(-Inf,-Inf)),complex(-1.0,-0.0))
    @test isequal(tanh(complex(-Inf, NaN)),complex(-1.0, 0.0))

    @test isequal(tanh(complex( NaN, 0.0)),complex(NaN, 0.0))
    @test isequal(tanh(complex( NaN,-0.0)),complex(NaN,-0.0))
    @test isequal(tanh(complex( NaN, 5.0)),complex(NaN, NaN))
    @test isequal(tanh(complex( NaN,-5.0)),complex(NaN, NaN))
    @test isequal(tanh(complex( NaN, NaN)),complex(NaN, NaN))
end

@testset "tan(z) == -i tanh(iz)" begin
    @test isequal(tan(complex( 0.0, Inf)),complex( 0.0, 1.0))
    @test isequal(tan(complex( 0.0,-Inf)),complex( 0.0,-1.0))
    @test isequal(tan(complex( 0.0, NaN)),complex( 0.0, NaN))
    @test isequal(tan(complex(-0.0,-Inf)),complex(-0.0,-1.0))
    @test isequal(tan(complex(-0.0, Inf)),complex(-0.0, 1.0))
    @test isequal(tan(complex(-0.0, NaN)),complex(-0.0, NaN))

    @test isequal(tan(complex( 5.0, Inf)),complex(sin(2*5.0)* 0.0, 1.0))
    @test isequal(tan(complex( 5.0,-Inf)),complex(sin(2*5.0)* 0.0,-1.0))
    @test isequal(tan(complex( 5.0, NaN)),complex( NaN, NaN))
    @test isequal(tan(complex(-5.0, Inf)),complex(sin(2*5.0)*-0.0, 1.0))
    @test isequal(tan(complex(-5.0,-Inf)),complex(sin(2*5.0)*-0.0,-1.0))
    @test isequal(tan(complex(-5.0, NaN)),complex( NaN, NaN))

    @test_throws DomainError  tan(complex( Inf, 5.0))
    @test isequal(tan(complex( Inf, Inf)),complex( 0.0, 1.0))
    @test isequal(tan(complex( Inf,-Inf)),complex( 0.0,-1.0))
    @test isequal(tan(complex(-Inf, Inf)),complex(-0.0, 1.0))
    @test isequal(tan(complex(-Inf,-Inf)),complex(-0.0,-1.0))

    @test isequal(tan(complex( NaN, 5.0)),complex( NaN, NaN))
    @test isequal(tan(complex( NaN, Inf)),complex( 0.0, 1.0))
    @test isequal(tan(complex( NaN,-Inf)),complex( 0.0,-1.0))
    @test isequal(tan(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "acosh(conj(z)) == conj(acosh(z))" begin
    @test isequal(acosh(complex( 0.0, 0.0)), complex( 0.0, pi/2))
    @test isequal(acosh(complex( 0.0,-0.0)), complex( 0.0,-pi/2))
    @test isequal(acosh(complex( 0.0, Inf)), complex( Inf, pi/2))
    @test isequal(acosh(complex( 0.0,-Inf)), complex( Inf,-pi/2))
    @test isequal(acosh(complex(-0.0, 0.0)), complex( 0.0, pi/2))
    @test isequal(acosh(complex(-0.0,-0.0)), complex( 0.0,-pi/2))
    @test isequal(acosh(complex( 5.0, Inf)), complex( Inf, pi/2))
    @test isequal(acosh(complex( 5.0,-Inf)), complex( Inf,-pi/2))
    @test isequal(acosh(complex( 5.0, NaN)), complex( NaN, NaN))

    @test isequal(acosh(complex( Inf, 0.0)), complex( Inf, 0.0))
    @test isequal(acosh(complex( Inf,-0.0)), complex( Inf,-0.0))
    @test isequal(acosh(complex( Inf, 5.0)), complex( Inf, 0.0))
    @test isequal(acosh(complex( Inf,-5.0)), complex( Inf,-0.0))
    @test isequal(acosh(complex( Inf, Inf)), complex( Inf, pi/4))
    @test isequal(acosh(complex( Inf,-Inf)), complex( Inf,-pi/4))
    @test isequal(acosh(complex( Inf, NaN)), complex( Inf, NaN))

    @test isequal(acosh(complex(-Inf, 0.0)), complex( Inf, pi))
    @test isequal(acosh(complex(-Inf,-0.0)), complex( Inf,-pi))
    @test isequal(acosh(complex(-Inf, 5.0)), complex( Inf, pi))
    @test isequal(acosh(complex(-Inf,-5.0)), complex( Inf,-pi))
    @test isequal(acosh(complex(-Inf, Inf)), complex( Inf, 3*pi/4))
    @test isequal(acosh(complex(-Inf,-Inf)), complex( Inf,-3*pi/4))
    @test isequal(acosh(complex(-Inf, NaN)), complex( Inf, NaN))

    @test isequal(acosh(complex( NaN, Inf)), complex( Inf, NaN))
    @test isequal(acosh(complex( NaN,-Inf)), complex( Inf, NaN))
    @test isequal(acosh(complex( NaN, NaN)), complex( NaN, NaN))
end

@testset "acos(conj(z)) == conj(acos(z))" begin
    @test isequal(acos(complex( 0, 0)),complex(pi/2,-0.0)) #integer fallback
    @test isequal(acos(complex( 0.0, 0.0)),complex(pi/2,-0.0))
    @test isequal(acos(complex( 0.0,-0.0)),complex(pi/2, 0.0))
    @test isequal(acos(complex( 0.0, Inf)),complex(pi/2,-Inf))
    @test isequal(acos(complex( 0.0,-Inf)),complex(pi/2, Inf))
    @test isequal(acos(complex( 0.0, NaN)),complex(pi/2, NaN))

    @test isequal(acos(complex(-0.0, 0.0)),complex(pi/2,-0.0))
    @test isequal(acos(complex(-0.0,-0.0)),complex(pi/2, 0.0))
    @test isequal(acos(complex(-0.0, NaN)),complex(pi/2, NaN))

    @test isequal(acos(complex( 5.0, Inf)),complex(pi/2,-Inf))
    @test isequal(acos(complex( 5.0,-Inf)),complex(pi/2, Inf))
    @test isequal(acos(complex( 5.0, NaN)),complex( NaN, NaN))

    @test isequal(acos(complex( Inf, 0.0)),complex( 0.0,-Inf))
    @test isequal(acos(complex( Inf,-0.0)),complex( 0.0, Inf))
    @test isequal(acos(complex( Inf, 5.0)),complex( 0.0,-Inf))
    @test isequal(acos(complex( Inf,-5.0)),complex( 0.0, Inf))
    @test isequal(acos(complex( Inf, Inf)),complex(pi/4,-Inf))
    @test isequal(acos(complex( Inf,-Inf)),complex(pi/4, Inf))
    @test isequal(acos(complex( Inf, NaN)),complex( NaN, Inf))

    @test isequal(acos(complex(-Inf, 0.0)),complex(pi,-Inf))
    @test isequal(acos(complex(-Inf,-0.0)),complex(pi, Inf))
    @test isequal(acos(complex(-Inf, 5.0)),complex(pi,-Inf))
    @test isequal(acos(complex(-Inf,-5.0)),complex(pi, Inf))
    @test isequal(acos(complex(-Inf, Inf)),complex(3*pi/4,-Inf))
    @test isequal(acos(complex(-Inf,-Inf)),complex(3*pi/4, Inf))
    @test isequal(acos(complex(-Inf, NaN)),complex( NaN, Inf))

    @test isequal(acos(complex( NaN, 0.0)),complex( NaN, NaN))
    @test isequal(acos(complex( NaN, 5.0)),complex( NaN, NaN))
    @test isequal(acos(complex( NaN, Inf)),complex( NaN,-Inf))
    @test isequal(acos(complex( NaN,-Inf)),complex( NaN, Inf))
    @test isequal(acos(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "asinh(op(z)) == op(asinh(z)) for op in (conj, -)" begin
    @test isequal(asinh(complex( 0.0, 0.0)),complex( 0.0, 0.0))
    @test isequal(asinh(complex( 0.0,-0.0)),complex( 0.0,-0.0))
    @test isequal(asinh(complex( 0.0, Inf)),complex( Inf, pi/2))
    @test isequal(asinh(complex( 0.0,-Inf)),complex( Inf,-pi/2))
    @test isequal(asinh(complex( 0.0, NaN)),complex( NaN, NaN))

    @test isequal(asinh(complex(-0.0, 0.0)),complex(-0.0, 0.0))
    @test isequal(asinh(complex(-0.0,-0.0)),complex(-0.0,-0.0))
    @test isequal(asinh(complex(-0.0, Inf)),complex(-Inf, pi/2))
    @test isequal(asinh(complex(-0.0,-Inf)),complex(-Inf,-pi/2))

    @test isequal(asinh(complex( 5.0, Inf)),complex( Inf, pi/2))
    @test isequal(asinh(complex( 5.0,-Inf)),complex( Inf,-pi/2))
    @test isequal(asinh(complex( 5.0, NaN)),complex( NaN, NaN))
    @test isequal(asinh(complex(-5.0, Inf)),complex(-Inf, pi/2))
    @test isequal(asinh(complex(-5.0,-Inf)),complex(-Inf,-pi/2))

    @test isequal(asinh(complex( Inf, Inf)),complex( Inf, pi/4))
    @test isequal(asinh(complex( Inf,-Inf)),complex( Inf,-pi/4))
    @test isequal(asinh(complex( Inf, NaN)),complex( Inf, NaN))
    @test isequal(asinh(complex(-Inf, Inf)),complex(-Inf, pi/4))
    @test isequal(asinh(complex(-Inf,-Inf)),complex(-Inf,-pi/4))
    @test isequal(asinh(complex(-Inf, NaN)),complex(-Inf, NaN))

    @test isequal(asinh(complex( NaN, 0.0)),complex( NaN, 0.0))
    @test isequal(asinh(complex( NaN,-0.0)),complex( NaN,-0.0))
    @test isequal(asinh(complex( NaN, 5.0)),complex( NaN, NaN))
    @test isequal(asinh(complex( NaN, Inf)),complex( Inf, NaN))
    @test isequal(asinh(complex( NaN,-Inf)),complex( Inf, NaN))
    @test isequal(asinh(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "asin(z) == -i*asinh(iz)" begin
    @test isequal(asin(complex( 0.0, 0.0)),complex( 0.0, 0.0))
    @test isequal(asin(complex( 0.0,-0.0)),complex( 0.0,-0.0))
    @test isequal(asin(complex(-0.0, 0.0)),complex(-0.0, 0.0))
    @test isequal(asin(complex( 0.0, NaN)),complex( 0.0, NaN))
    @test isequal(asin(complex(-0.0,-0.0)),complex(-0.0,-0.0))
    @test isequal(asin(complex(-0.0, NaN)),complex(-0.0, NaN))
    @test isequal(asin(complex( 5.0, NaN)),complex( NaN, NaN))

    @test isequal(asin(complex( Inf, 0.0)),complex( pi/2, Inf))
    @test isequal(asin(complex( Inf,-0.0)),complex( pi/2,-Inf))
    @test isequal(asin(complex( Inf, 5.0)),complex( pi/2, Inf))
    @test isequal(asin(complex( Inf,-5.0)),complex( pi/2,-Inf))
    @test isequal(asin(complex( Inf, Inf)),complex( pi/4, Inf))
    @test isequal(asin(complex( Inf,-Inf)),complex( pi/4,-Inf))
    @test isequal(asin(complex( Inf, NaN)),complex( NaN, Inf))

    @test isequal(asin(complex(-Inf, 0.0)),complex(-pi/2, Inf))
    @test isequal(asin(complex(-Inf,-0.0)),complex(-pi/2,-Inf))
    @test isequal(asin(complex(-Inf, 5.0)),complex(-pi/2, Inf))
    @test isequal(asin(complex(-Inf,-5.0)),complex(-pi/2,-Inf))
    @test isequal(asin(complex(-Inf, Inf)),complex(-pi/4, Inf))
    @test isequal(asin(complex(-Inf,-Inf)),complex(-pi/4,-Inf))
    @test isequal(asin(complex(-Inf, NaN)),complex( NaN, Inf))

    @test isequal(asin(complex( NaN, 0.0)),complex( NaN, NaN))
    @test isequal(asin(complex( NaN, 5.0)),complex( NaN, NaN))
    @test isequal(asin(complex( NaN, Inf)),complex( NaN, Inf))
    @test isequal(asin(complex( NaN,-Inf)),complex( NaN,-Inf))
    @test isequal(asin(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "atanh(op(z)) == op(atanh(z)) for op in (conj, -)" begin
    @test isequal(atanh(complex( 0, 0)),complex( 0.0, 0.0)) #integer fallback
    @test isequal(atanh(complex( 0.0, 0.0)),complex( 0.0, 0.0))
    @test isequal(atanh(complex( 0.0,-0.0)),complex( 0.0,-0.0))
    @test isequal(atanh(complex( 0.0, NaN)),complex( 0.0, NaN))
    @test isequal(atanh(complex( 0.0, Inf)),complex( 0.0, pi/2))
    @test isequal(atanh(complex( 0.0,-Inf)),complex( 0.0,-pi/2))

    @test isequal(atanh(complex(-0.0, NaN)),complex(-0.0, NaN))
    @test isequal(atanh(complex(-0.0, 0.0)),complex(-0.0, 0.0))
    @test isequal(atanh(complex(-0.0,-0.0)),complex(-0.0,-0.0))
    @test isequal(atanh(complex(-0.0, Inf)),complex(-0.0, pi/2))
    @test isequal(atanh(complex(-0.0,-Inf)),complex(-0.0,-pi/2))

    @test isequal(atanh(complex( 1.0, 0.0)),complex( Inf, 0.0))
    @test isequal(atanh(complex(-1.0, 0.0)),complex(-Inf, 0.0))
    @test isequal(atanh(complex( 5.0, Inf)),complex( 0.0, pi/2))
    @test isequal(atanh(complex( 5.0,-Inf)),complex( 0.0,-pi/2))
    @test isequal(atanh(complex( 5.0, NaN)),complex( NaN, NaN))
    @test isequal(atanh(complex(-5.0, Inf)),complex(-0.0, pi/2))
    @test isequal(atanh(complex(-5.0,-Inf)),complex(-0.0,-pi/2))
    @test isequal(atanh(complex(-5.0, NaN)),complex( NaN, NaN))

    @test isequal(atanh(complex( Inf, 0.0)),complex(0.0, pi/2))
    @test isequal(atanh(complex( Inf,-0.0)),complex(0.0,-pi/2))
    @test isequal(atanh(complex( Inf, 5.0)),complex(0.0, pi/2))
    @test isequal(atanh(complex( Inf,-5.0)),complex(0.0,-pi/2))
    @test isequal(atanh(complex( Inf, Inf)),complex(0.0, pi/2))
    @test isequal(atanh(complex( Inf,-Inf)),complex(0.0,-pi/2))
    @test isequal(atanh(complex( Inf, NaN)),complex(0.0, NaN))

    @test isequal(atanh(complex(-Inf, 0.0)),complex(-0.0, pi/2))
    @test isequal(atanh(complex(-Inf,-0.0)),complex(-0.0,-pi/2))
    @test isequal(atanh(complex(-Inf, 5.0)),complex(-0.0, pi/2))
    @test isequal(atanh(complex(-Inf,-5.0)),complex(-0.0,-pi/2))
    @test isequal(atanh(complex(-Inf, Inf)),complex(-0.0, pi/2))
    @test isequal(atanh(complex(-Inf,-Inf)),complex(-0.0,-pi/2))
    @test isequal(atanh(complex(-Inf, NaN)),complex(-0.0, NaN))

    @test isequal(atanh(complex( NaN, 0.0)),complex( NaN, NaN))
    @test isequal(atanh(complex( NaN,-0.0)),complex( NaN, NaN))
    @test isequal(atanh(complex( NaN, 5.0)),complex( NaN, NaN))
    @test isequal(atanh(complex( NaN,-5.0)),complex( NaN, NaN))
    @test isequal(atanh(complex( NaN, Inf)),complex( 0.0, pi/2))
    @test isequal(atanh(complex( NaN,-Inf)),complex( 0.0,-pi/2))
    @test isequal(atanh(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "atan(z) == -i*atanh(iz)" begin
    @test isequal(atan(complex( 0.0, 0.0)),complex( 0.0, 0.0))
    @test isequal(atan(complex( 0.0,-0.0)),complex( 0.0,-0.0))
    @test isequal(atan(complex( 0.0, 1.0)),complex( 0.0, Inf))
    @test isequal(atan(complex( 0.0, Inf)),complex( pi/2, 0.0))
    @test isequal(atan(complex( 0.0,-Inf)),complex( pi/2,-0.0))
    @test isequal(atan(complex( 0.0, NaN)),complex( NaN, NaN))

    @test isequal(atan(complex(-0.0, 0.0)),complex(-0.0, 0.0))
    @test isequal(atan(complex(-0.0,-0.0)),complex(-0.0,-0.0))
    @test isequal(atan(complex(-0.0, Inf)),complex(-pi/2, 0.0))
    @test isequal(atan(complex(-0.0,-Inf)),complex(-pi/2,-0.0))
    @test isequal(atan(complex(-0.0, NaN)),complex( NaN, NaN))

    @test isequal(atan(complex( 5.0, Inf)),complex( pi/2, 0.0))
    @test isequal(atan(complex( 5.0,-Inf)),complex( pi/2,-0.0))
    @test isequal(atan(complex( 5.0, NaN)),complex( NaN, NaN))

    @test isequal(atan(complex(-5.0, Inf)),complex(-pi/2, 0.0))
    @test isequal(atan(complex(-5.0,-Inf)),complex(-pi/2,-0.0))
    @test isequal(atan(complex(-5.0, NaN)),complex( NaN, NaN))

    @test isequal(atan(complex( Inf, 0.0)),complex( pi/2, 0.0))
    @test isequal(atan(complex( Inf,-0.0)),complex( pi/2,-0.0))
    @test isequal(atan(complex( Inf, 5.0)),complex( pi/2, 0.0))
    @test isequal(atan(complex( Inf,-5.0)),complex( pi/2,-0.0))
    @test isequal(atan(complex( Inf, Inf)),complex( pi/2, 0.0))
    @test isequal(atan(complex( Inf,-Inf)),complex( pi/2,-0.0))
    @test isequal(atan(complex( Inf, NaN)),complex( pi/2, 0.0))

    @test isequal(atan(complex(-Inf, 0.0)),complex(-pi/2, 0.0))
    @test isequal(atan(complex(-Inf,-0.0)),complex(-pi/2,-0.0))
    @test isequal(atan(complex(-Inf, 5.0)),complex(-pi/2, 0.0))
    @test isequal(atan(complex(-Inf,-5.0)),complex(-pi/2,-0.0))
    @test isequal(atan(complex(-Inf, Inf)),complex(-pi/2, 0.0))
    @test isequal(atan(complex(-Inf,-Inf)),complex(-pi/2,-0.0))
    @test isequal(atan(complex(-Inf, NaN)),complex(-pi/2, 0.0))

    @test isequal(atan(complex( NaN, 0.0)),complex( NaN, 0.0))
    @test isequal(atan(complex( NaN,-0.0)),complex( NaN,-0.0))
    @test isequal(atan(complex( NaN, 5.0)),complex( NaN, NaN))
    @test isequal(atan(complex( NaN,-5.0)),complex( NaN, NaN))
    @test isequal(atan(complex( NaN, Inf)),complex( NaN, 0.0))
    @test isequal(atan(complex( NaN,-Inf)),complex( NaN,-0.0))
    @test isequal(atan(complex( NaN, NaN)),complex( NaN, NaN))
end

@testset "lexcmp" begin
    @test lexcmp(1.0-1.0im, 1.0+0.0im) == -1
    @test lexcmp(0.0+0.0im, 0.0+0.0im) == 0
    @test lexcmp(1.0-1.0im, 0.0+0.0im) == 1
end

# misc.

@test complex(1//2,1//3)^2 === complex(5//36, 1//3)
@test complex(2,2)^2 === complex(0,8)
let p = -2
    @test_throws DomainError complex(2,2)^p
end
@test complex(2,2)^(-2) === complex(2.0,2.0)^(-2) === complex(0.0, -0.125)

@test complex.(1.0, [1.0, 1.0]) == [complex(1.0, 1.0), complex(1.0, 1.0)]
@test complex.([1.0, 1.0], 1.0) == [complex(1.0, 1.0), complex(1.0, 1.0)]
# robust division of Float64
# hard complex divisions from Fig 6 of arxiv.1210.4539
z7 = Complex{Float64}(3.898125604559113300e289, 8.174961907852353577e295)
z9 = Complex{Float64}(0.001953125, -0.001953125)
z10 = Complex{Float64}( 1.02951151789360578e-84, 6.97145987515076231e-220)
harddivs = ((1.0+im*1.0, 1.0+im*2^1023.0, 2^-1023.0-im*2^-1023.0), #1
      (1.0+im*1.0, 2^-1023.0+im*2^-1023.0, 2^1023.0+im*0.0), #2
      (2^1023.0+im*2^-1023.0, 2^677.0+im*2^-677.0, 2^346.0-im*2^-1008.0), #3
      (2^1023.0+im*2^1023.0, 1.0+im*1.0, 2^1023.0+im*0.0), #4
      (2^1020.0+im*2^-844., 2^656.0+im*2^-780.0, 2^364.0-im*2^-1072.0), #5
      (2^-71.0+im*2^1021., 2^1001.0+im*2^-323.0, 2^-1072.0+im*2^20.0), #6
      (2^-347.0+im*2^-54., 2^-1037.0+im*2^-1058.0, z7), #7
      (2^-1074.0+im*2^-1074., 2^-1073.0+im*2^-1074., 0.6+im*0.2), #8
      (2^1015.0+im*2^-989., 2^1023.0+im*2^1023.0, z9), #9
      (2^-622.0+im*2^-1071., 2^-343.0+im*2^-798.0, z10) #10
      )

# calculate "accurate bits" in range 0:53 by algorithm given in arxiv.1210.4539
function sb_accuracy(x,expected)
    min(logacc(real(x),real(expected)),
        logacc(imag(x),imag(expected)))
end
relacc(x,expected) = abs(x-expected)/abs(expected)
function logacc(x::Float64,expected::Float64)
    x == expected && (return 53)
    expected == 0 && (return 0)
    (x == Inf || x == -Inf) && (return 0)
    isnan(x) && (return 0)
    ra = relacc(BigFloat(x),BigFloat(expected))
    max(floor(Int,-log2(ra)),0)
end
# the robust division algorithm should have 53 or 52
# bits accuracy for each of the hard divisions
@test 52 <= minimum([sb_accuracy(h[1]/h[2],h[3]) for h in harddivs])

# division of non-Float64
function cdiv_test(a,b)
    c=convert(Complex{Float64},a)/convert(Complex{Float64},b)
    50 <= sb_accuracy(c,convert(Complex{Float64},a/b))
end
@test cdiv_test(complex(1//2, 3//4), complex(17//13, 4//5))
@test cdiv_test(complex(1,2), complex(8997,2432))

@testset "inv" begin
    @test inv(1e300+0im) == 1e-300 - 0.0im
    @test inv(0+1e300im) == 0.0 - 1e-300im
end

@testset "issue #7904" begin
    @test log10(10+0im) === 1.0 + 0.0im
    @test log2(2+0im) === 1.0 + 0.0im
end

@testset "sign" begin
    for T in (Float32, Float64)
        z = Complex{T}(1)
        @test typeof(sign(z)) == typeof(z)
        z = Complex{T}(0)
        @test typeof(sign(z)) == typeof(z)
    end
    for T in (Int32, Int64)
        z = Complex{T}(1)
        @test typeof(sign(z)) == typeof(float(z))
        z = Complex{T}(0)
        @test typeof(sign(z)) == typeof(float(z))
    end

    @test sign(0 + 0im) == 0
    @test sign(2 + 0im) == 1
    @test sign(-2 + 0im) == -1
    @test sign(1 + im) ≈ (1 + im) / sqrt(2)
    @test sign(1 - im) ≈ (1 - im) / sqrt(2)

    for T in (Float16, Float32, Float64)
        z = Complex(zero(T), zero(T))
        @test sign(z) === z
        @test sign(-z) === -z
        @test sign(conj(z)) === conj(z)
        @test sign(-conj(z)) === -conj(z)
    end
end

@testset "cis" begin
    @test cis(0.0+1.0im) ≈ 0.367879441171442321595523770161460867445811131031767834507836+0.0im
    @test cis(1.0+0.0im) ≈ 0.54030230586813971740093660744297660373231042061+0.84147098480789650665250232163029899962256306079im
    @test cis(pi) ≈ -1.0+0.0im
    @test cis(pi/2) ≈ 0.0+1.0im
end

@testset "exp2" begin
    @test exp2(0.0+0.0im) == 1.0+0.0im
    @test exp2(1.0+0.0im) == 2.0+0.0im
    #wolframalpha
    @test exp2(1.0+3.0im) ≈ -0.9739888359315627962096198412+1.74681016354974281701922im
    @test exp2(im) ≈ 0.7692389013639721 + 0.6389612763136348im
end

@testset "exp10" begin
    @test exp10(0.0+0.0im) == 1.0+0.0im
    @test exp10(1.0+0.0im) == 10.0+0.0im
    #wolframalpha
    @test exp10(1.0+2.0im) ≈ -1.0701348355877020772086517528518239460495529361-9.9425756941378968736161937190915602112878340717im
    @test exp10(im) ≈ -0.6682015101903132 + 0.7439803369574931im
end

@testset "round and float, PR #8291" begin
    @test round(Complex(1.125, 0.875), 2) == Complex(1.12, 0.88)
    @test round(Complex(1.5, 0.5), RoundDown, RoundUp) == Complex(1.0, 1.0)
    @test round.([1:5;] .+ im) == [1:5;] .+ im
    @test round.([1:5;] .+ 0.5im) == [1.0:5.0;]

    @test float(Complex(1, 2)) == Complex(1.0, 2.0)
    @test round(float(Complex(π, ℯ)),3) == Complex(3.142, 2.718)
end

@testset "Complex32 arithmetic, PR #10003" begin
    @test Float16(1)+Float16(1)im === Complex32(1, 1)
    @test Float16(1)-Float16(1)im === Float16(1)+Float16(-1)im === Complex32(1, -1)
    @test Float16(1)*im === Complex32(im)
    @test Float16(1)/im === Complex32(0,-1)
    @test Float16(1)^im === Complex32(1) === Float16(1)+Float16(0)im
end

# issue/PR #10148
@test typeof(Int8(1) - im) == Complex{Int8}

# issue #10926
@test typeof(π - 1im) == Complex{Float64}

@testset "issue #15969" begin
    # specialized muladd for complex types
    for x in (3, 3+13im), y in (2, 2+7im), z in (5, 5+11im)
        @test muladd(x,y,z) === x*y + z
    end
end

@testset "issue #11839" begin
    # type stability for Complex{Int64}
    let x = 1+im
        @inferred sin(x)
        @inferred cos(x)
        @inferred norm(x)
        @inferred vecnorm(x)
    end
end

@testset "issue #18785" begin
    # type stability for exp, expm1 for Complex{Int64}
    let x = 2*im
        @inferred exp(x)
        @inferred expm1(x)
    end
end

# issue #19240
@test big(1)/(10+10im) ≈ (5-5im)/big(100) ≈ big"0.05" - big"0.05"*im

@testset "Complex Irrationals, issue #21204" begin
    for x in (pi, ℯ, Base.MathConstants.catalan) # No need to test all of them
        z = Complex(x, x)
        @test typeof(z) == Complex{typeof(x)}
        @test exp(z) ≈ exp(x) * cis(x)
        @test log1p(z) ≈ log(1 + z)
        @test exp2(z) ≈ exp(z * log(2))
        @test exp10(z) ≈ exp(z * log(10))
    end
end

@testset "expm1 type stability" begin
    x = @inferred expm1(0.1im)
    @test x isa Complex128
    x = @inferred expm1(0.1f0im)
    @test x isa Complex64
end

@testset "array printing with exponent format" begin
    a = [1.0 + 1e-10im, 2.0e-15 - 2.0e-5im, 1.0e-15 + 2im, 1.0 + 2e-15im]
    @test sprint((io, x) -> show(io, MIME("text/plain"), x), a) ==
        join([
            "4-element Array{Complex{Float64},1}:",
            "     1.0 + 1.0e-10im",
            " 2.0e-15 - 2.0e-5im ",
            " 1.0e-15 + 2.0im    ",
            "     1.0 + 2.0e-15im"], "\n")
end

@testset "corner cases of division, issue #22983" begin
    # These results abide by ISO/IEC 10967-3:2006(E) and
    # mathematical definition of division of complex numbers.
    for T in (Float32, Float64, BigFloat)
        @test isequal(one(T) / zero(Complex{T}), one(Complex{T}) / zero(Complex{T}))
        @test isequal(one(T) / zero(Complex{T}), Complex{T}(NaN, NaN))
        @test isequal(one(Complex{T}) / zero(T), Complex{T}(Inf, NaN))
        @test isequal(one(Complex{T}) / one(Complex{T}), one(Complex{T}))
        @test isequal(one(T) / complex(one(T),  zero(T)), Complex(one(T), -zero(T)))
        @test isequal(one(T) / complex(one(T), -zero(T)), Complex(one(T),  zero(T)))
    end
end

@testset "complex^real, issue #14342" begin
    for T in (Float32, Float64, BigFloat), p in (T(-21//10), -21//10)
        z = T(2)+0im
        @test real(z^p) ≈ 2^p
        @test signbit(imag(z^p))
    end
    @test (2+0im)^(-21//10) === (2//1+0im)^(-21//10) === 2^-2.1 - 0.0im
end

max_stars_repo_name: ihnorton/julia

max_issues_repo_name: JuliaLangEs/julieta

max_forks_repo_name: JuliaLangEs/julieta


text:
function string_zeros_stable(s::AbstractString)
x = Array(s=="Int64"?Int64:Float64, 1_000_000)
return fill_zeros(x)
end
function fill_zeros(x)
for i in 1:length(x)
x[i] = 0
end
return x
end

max_stars_repo_path: Module 2/Chapter03/stringzero_stable.jl

max_issues_repo_path: Module 2/Chapter03/stringzero_stable.jl

max_forks_repo_path: Module 2/Chapter03/stringzero_stable.jl


text:
using DelimitedFiles

const sobol_a_file = joinpath(@__DIR__, "sobol_a.csv")
const sobol_minit_file = joinpath(@__DIR__, "sobol_minit.csv")

# we need to re-compile if these files change:
include_dependency(sobol_a_file)
include_dependency(sobol_minit_file)

#successive primitive binary-coefficient polynomials p(z)
#   = a_0 + a_1 z + a_2 z^2 + ... a_31 z^31, where a_i is the
#     i-th bit of sobol_a[j] for the j-th polynomial.
const sobol_a = vec(readdlm(sobol_a_file, ',', UInt32))

# starting direction #'s m[i] = sobol_minit[i][j] for i=0..d of the
# degree-d primitive polynomial sobol_a[j].
const sobol_minit = readdlm(sobol_minit_file, ',', UInt32)


text:
@testset "611.valid-triangle-number.jl" begin
    @test triangle_number([2, 2, 3, 4]) == 3
    @test triangle_number([41, 17, 10, 29, 28, 40, 3, 48, 52, 52, 16, 8, 31, 16, 35, 6, 58,
                           26, 19, 7, 58, 32, 10, 49, 20, 53, 39, 43, 36, 11, 40, 37, 8, 20,
                           29, 21, 54, 60, 52, 40, 54, 5, 48, 50, 42, 17, 11, 48, 18, 5]) ==
          9751
end


text:
# parameter initialization
# mostly based on: https://github.com/pytorch/pytorch/blob/d58059bc6fa9b5a0c9a3186631029e4578ca2bbd/torch/nn/init.py

function calculate_fan_in_and_fan_out(W::AbstractMatrix)
    return size(W, 2), size(W, 1)
end

function calculate_fan_in_and_fan_out(W::AbstractArray{T,4}) where T
    num_input_fmaps = size(W, 3)
    num_output_fmaps = size(W, 4)
    receptive_field_size = prod(size(W)[1:2])
    fan_in = num_input_fmaps * receptive_field_size
    fan_out = num_output_fmaps * receptive_field_size
    return fan_in, fan_out
end

function calculate_gain(nonlinearity, param=nothing)
    linear_fns = (:linear, :conv1d, :conv2d, :conv3d, :conv_transpose1d, :conv_transpose2d, :conv_transpose3d)
    if nonlinearity in linear_fns || nonlinearity == :sigmoid
        return 1
    elseif nonlinearity == :tanh
        return 5.0 / 3
    elseif nonlinearity == :relu
        return sqrt(2.0)
    elseif nonlinearity == :leaky_relu
        if param == nothing
            negative_slope = 0.01
        elseif !isa(param, Bool) && isa(param, Integer) || isa(param, Real)
            negative_slope = param
        else
            error("negative_slope $param not a valid number")
        end
        return sqrt(2.0 / (1 + negative_slope ^ 2))
    else
        error("Unsupported nonlinearity $nonlinearity")
    end
end

function init_kaiming_normal!(W::AbstractArray; mode::Symbol=:fan_in, nonlinearity=:leaky_relu, a=0)
    @assert mode in (:fan_in, :fan_out)
    fan_in, fan_out = calculate_fan_in_and_fan_out(W)
    fan = mode == :fan_in ? fan_in : fan_out
    gain = calculate_gain(nonlinearity, a)
    std = gain / sqrt(fan)
    W .= rand(Normal(0, std), size(W))
    return W
end

function init_constant!(W::AbstractArray, val)
    W .= val
end


text:
@testset "List" begin

@testset "Sum" begin
    @test string(ItemList([])) == "no objects"
    @test string(ItemList(["apples"])) == "apples"
    @test string(ItemList(["apples", "oranges"])) == "apples and oranges"
    @test string(ItemList(["apples", "oranges", "grapefruit"])) ==
        "apples, oranges, and grapefruit"
    @test string(ItemList(english.(1:5))) == "one, two, three, four, and five"
end

@testset "Conjunction" begin
    @test string(ItemList([], Conjunction())) == "unremarkable"
    @test string(ItemList(["red"], Conjunction())) == "red"
    @test string(ItemList(["red", "blue"], Conjunction())) == "red and blue"
    @test string(ItemList(["red", "blue", "yellow"], Conjunction())) ==
        "red, blue, and yellow"
end

@testset "Disjunction" begin
    @test string(ItemList([], Disjunction())) == "impossible"
    @test string(ItemList(["red"], Disjunction())) == "red"
    @test string(ItemList(["red", "blue"], Disjunction())) == "red or blue"
    @test string(ItemList(["red", "blue", "yellow"], Disjunction())) ==
        "red, blue, or yellow"
    @test string(ItemList(english.(1:5), Disjunction())) ==
        "one, two, three, four, or five"
end

end


text:
using Test
using CthulhuVision.Random

@testset "CthulhuVision.Random CPU" begin
    @testset "uniform32 is 0.0f0 <= x < 1.0f0" begin
        ks = [uniform32(x) for x in rand(UInt64, 1000)]
        for k in ks
            @test 0.0f0 <= k < 1.0f0
        end
    end

    @testset "SplitMix64 validation" begin
        # These test values were generated from the C code available at
        # http://prng.di.unimi.it/splitmix64.c
        # 2020-04-16

        # Seed 0x0000000000000000:
        fromseed0 = [
            0xe220a8397b1dcdaf, 0x6e789e6aa1b965f4, 0x06c45d188009454f, 0xf88bb8a8724c81ec, 0x1b39896a51a8749b,
            0x53cb9f0c747ea2ea, 0x2c829abe1f4532e1, 0xc584133ac916ab3c, 0x3ee5789041c98ac3, 0xf3b8488c368cb0a6,
            0x657eecdd3cb13d09, 0xc2d326e0055bdef6, 0x8621a03fe0bbdb7b, 0x8e1f7555983aa92f, 0xb54e0f1600cc4d19,
            0x84bb3f97971d80ab, 0x7d29825c75521255, 0xc3cf17102b7f7f86, 0x3466e9a083914f64, 0xd81a8d2b5a4485ac,
            0xdb01602b100b9ed7, 0xa9038a921825f10d, 0xedf5f1d90dca2f6a, 0x54496ad67bd2634c, 0xdd7c01d4f5407269,
            0x935e82f1db4c4f7b, 0x69b82ebc92233300, 0x40d29eb57de1d510, 0xa2f09dabb45c6316, 0xee521d7a0f4d3872,
            0xf16952ee72f3454f, 0x377d35dea8e40225, 0x0c7de8064963bab0, 0x05582d37111ac529, 0xd254741f599dc6f7,
            0x69630f7593d108c3, 0x417ef96181daa383, 0x3c3c41a3b43343a1, 0x6e19905dcbe531df, 0x4fa9fa7324851729,
            0x84eb4454a792922a, 0x134f7096918175ce, 0x07dc930b302278a8, 0x12c015a97019e937, 0xcc06c31652ebf438,
            0xecee65630a691e37, 0x3e84ecb1763e79ad, 0x690ed476743aae49, 0x774615d7b1a1f2e1, 0x22b353f04f4f52da,
            0xe3ddd86ba71a5eb1, 0xdf268adeb6513356, 0x2098eb73d4367d77, 0x03d6845323ce3c71, 0xc952c5620043c714,
            0x9b196bca844f1705, 0x30260345dd9e0ec1, 0xcf448a5882bb9698, 0xf4a578dccbc87656, 0xbfdeaed9a17b3c8f,
            0xed79402d1d5c5d7b, 0x55f070ab1cbbf170, 0x3e00a34929a88f1d, 0xe255b237b8bb18fb, 0x2a7b67af6c6ad50e,
            0x466d5e7f3e46f143, 0x42375cb399a4fc72, 0x8c8a1f148a8bb259, 0x32fcab5daed5bdfc, 0x9e60398c8d8553c0,
            0xee89cceb8c4064c0, 0xdb0215941d86a66f, 0x5ccde78203c367a8, 0xf1bcbc6a1ec11786, 0xef054fceee954551,
            0xdf82012d0555c6df, 0x292566ff72403c08, 0xc4dd302a1bfa1137, 0xd85f219db5c554e1, 0x6a27ff807441bcd2,
            0x96a573e9b48216e8, 0x46a9fdac40bf0048, 0x3dd12464a0ee15b4, 0x451e521296a7eea1, 0x56e4398a98f8a0fd,
            0x7b7dc2160e3335a7, 0xc679ee0bebcb1cca, 0x928d6f2d7453424e, 0x1b38994205234c6d, 0x8086d193a6f2b568,
            0x21c6e26639ac2c65, 0xd9dccac414d23c6f, 0x91cd642057e00235, 0x77fc607dc6589373, 0x05b8abe26dd3aee7,
            0x12f6436ac376cc66, 0x64952424897b2307, 0xee8c2baf6343e5c3, 0xdc4c613d9eba2304, 0x3505b7796bd1a506,
        ]
        sm1 = SplitMix64(0x0000000000000000)
        for i = 1:length(fromseed0)
            @test next(sm1) == fromseed0[i]
        end

        # Seed 0x1234567890123456:
        fromseed123 = [
            0xa747f481346acb72, 0x8ad918349ab73966, 0xbec62b7cc97a0873, 0xf4975b3f04f272de, 0x309a2a499503b323,
            0x75f0d35e440c1380, 0xa0e250c4832f2146, 0x5000ad7279d4564d, 0x527f776bf010fc54, 0xb178837968cf7887,
            0x03a2b6f219eb2ac9, 0x0c292f6dac0b89ce, 0xf406a38407ab8e94, 0x675980f999c91458, 0x888499c0bc4770c8,
            0x410c7af835212035, 0xf1a2cce43237861f, 0x1b3988311d0850c3, 0x70dc6bfcab337fc3, 0x12cec9bb9600463d,
            0xbce1012111683484, 0x63d63227c1131aef, 0x935018dead98b912, 0x50427855dd872b27, 0x5e1701eb8aed1c32,
            0x3231ecfe36e059f7, 0x3ee6eae41b75b100, 0x8640df45d78097ac, 0x6bd68158bdc48a49, 0xaa6af115f13bed47,
            0x9680ce74a4a7e996, 0x4b7d22f3cbd00a25, 0xc71682b11cc96c07, 0x663a228deabdf2b1, 0x75018ae2ecab3af5,
            0x0e6c0c485496b5db, 0x4f5f6e4b5283597f, 0x51a2c78fa67277c2, 0x7d6587ff16a97e90, 0x0226d182b0dd9a48,
            0x61b5565f8c28bd10, 0x9201bb21659aefd1, 0x3971d699e9f55c64, 0x0a3d12bae04ab182, 0xcbaf187d97a60d1e,
            0x2615a8f09a9a6b7a, 0xef5b6a1cbcabf696, 0x4dd54df163973e33, 0xe4b483b1d6762fbf, 0x3bd5b1a09a569677,
            0x49b90c90ce82c302, 0xf193d2253c266d22, 0xf885d93f2e56a330, 0xc6fdf5a84b29694a, 0x84b09a60cd8f6d50,
            0x859bdff3c39724b9, 0x8f7bf534dfdb6f8b, 0x162ade8198d9f7dd, 0x56d5af032eb87ad9, 0x961bf7f92b4da6cf,
            0x189850a17f576b2e, 0x52661a31fc19528f, 0x4486fe7a2234d24b, 0xea4d0bbe2643c140, 0xd682459a551543b1,
            0xee1c33c957b2b3c2, 0x5d3c39bcd8abf73b, 0xcfb078345aae27ae, 0xdd8cae3594569323, 0xb5c53b2f99372232,
            0x358f6866529407ca, 0xadf308a600d090a2, 0xe16cb21fb6a309a7, 0x07f86d1786ef4264, 0x317f3628ac7ccc19,
            0x8d78766af85d7ce4, 0x8d8790adea039d5e, 0x17c8ff07ebccb417, 0x93be8e429f5d8b3e, 0x8893278971571734,
            0xce4f4aab2da09bde, 0xe75a99e8d6afd288, 0xae74da6eea491702, 0xce202d270eca927c, 0x040cf01b89c75d41,
            0x2c9608b9e1138f11, 0x5a23a33b63d4cdb1, 0x5f1064ca5b023240, 0x180712765e0c7518, 0xb409002087be28db,
            0x15582962b3b3dd73, 0x1fc1c3a0880262c2, 0x99dd5b63f583202f, 0x99da0a71d47b3176, 0x7cd6a4aa5b2760c8,
            0xea5d86aa35596e3e, 0x28c3164807d7201a, 0x7203b1f76c6e2866, 0x3911c8344d36bd8b, 0x580cc832a21b96c6,
        ]

        sm2 = SplitMix64(0x1234567890123456)
        for i = 1:length(fromseed123)
            @test next(sm2) == fromseed123[i]
        end
    end

    @testset "Xoshiro256++ validation" begin
        # The seeds are taken from the SplitMix64 test vectors.
        # Seed 0xe220a8397b1dcdaf 0x6e789e6aa1b965f4 0x06c45d188009454f 0xf88bb8a8724c81ec:
        xshr256_test_1 = [
            0x53175d61490b23df, 0x61da6f3dc380d507, 0x5c0fdf91ec9a7bfc, 0x02eebf8c3bbe5e1a, 0x7eca04ebaf4a5eea,
            0x0543c37757f08d9a, 0xdb7490c75ab5026e, 0xd87343e6464bc959, 0x4b7da0a02389f0ff, 0x1300fc58c0424c16,
            0x5084843206c19968, 0x10ea073de9aa4dfc, 0x1aae554343960cc1, 0x1804139f10fae720, 0x10d790e7b8ac10fa,
            0x667d2bffdd1496f7, 0xa04620d3d0fc04a8, 0x1d50881230af9cc3, 0x53be287ded35f698, 0x673235793f7908e1,
            0x46e91feb4535fbdc, 0x216c1524cbac57c0, 0x0a53eb08063a44df, 0x45f965b948778197, 0x6f2fa9d01ba03887,
            0x60c57eba69ed4e15, 0x22c65ce977dd39cb, 0xa5d1ce0c5a7c6abf, 0xe8e26337cde13268, 0x0b4a575fdb6f8160,
            0x400feb0bae786424, 0x633e0b621080bf50, 0x5a456e5a144e059b, 0xdc75548b5cd2e8cd, 0xdf9d76f766648113,
            0x342bf8b7aec0de41, 0x831593e6b50ae928, 0x29e12b2a1872d7db, 0xb6362d8b640aec49, 0x2e78698eb5bba4a9,
            0x9064494b8287afb9, 0x4c04974c6c1b4767, 0x5863b8685408be73, 0x0e8ca571066bc302, 0x088959d638956a37,
            0x2e9392dfd5c30e86, 0x36da000d696e9d9e, 0x2a839b60548c1044, 0x3ebbaffcc5f270ca, 0x6da02738c0f92ee5,
            0x962fd83157fe1682, 0x856dcc088cece014, 0xca8717351ab24cbd, 0x231527552d018184, 0x06793b14839607ec,
            0xc54f89a7e193e5c1, 0xbacc209dd739707c, 0x7dc7053580f1ff20, 0x4ee696659cc1be91, 0xa3cb5d7769921646,
            0x9c002aaa8a687ded, 0xc0c3a216563d9ae2, 0x035b6d98ee8a1b19, 0x68d89ab6ea60f57d, 0xec45ebfcc75a43fc,
            0xc5baecd6bbbc6a26, 0x699d89305420574e, 0x48dce98c29e291a9, 0xb48e120ad1f9f23e, 0xe7c6368bd2aa145f,
            0xcf6e7679e474e800, 0x3d55f9180cf7bd5a, 0xd07064c4f0125475, 0x9784173bc20bb2cf, 0xc3296df2c4be97be,
            0x7ef38a3feaea3dcc, 0xa93ae5c3e3ac91b3, 0xc173a3156ae8e099, 0xa44373eaf0aff364, 0xb2a0bfb5000b2197,
            0xa49a7367daa7357f, 0x6be7b09a41e8061b, 0xa78c4d974c96b3fb, 0x28f7fbded613a963, 0x294d2574459f8cca,
            0xf3b457be6842a7ca, 0x9ce67b8b49364712, 0xd1b547099732fb15, 0x3538f30d9343b292, 0xc30c6e4c0dbee4c4,
            0x90b83117f578872c, 0xee166f8ef6076653, 0x671a16f58b431a4d, 0xa272fd92ddc2c04e, 0xf2bf4fd243cb8c2d,
            0xd2090a5c8c865eef, 0x7fd93d8b5401a273, 0x1ca67e32b0e47584, 0xb30ff462f8392fec, 0xb16ca64963ede86f,
        ]

        xshr1 = Xoshiro256pp(0xe220a8397b1dcdaf, 0x6e789e6aa1b965f4, 0x06c45d188009454f, 0xf88bb8a8724c81ec)

        for i = 1:length(xshr256_test_1)
            @test next(xshr1) == xshr256_test_1[i]
        end

        # Seed 0xa747f481346acb72 0x8ad918349ab73966 0xbec62b7cc97a0873 0xf4975b3f04f272de:
        xshr256_test_2 = [
            0x8764a3205cb8bb19, 0x83430004f370c12d, 0x4b25f419b4c33798, 0x8a513a4e0cb13f7d, 0x838ec104e7520e51,
            0x9d4e0fbd9efbd410, 0x4699cac599252763, 0x9d12a1920aa92ea6, 0x16f58890f28f73f0, 0xb9a3d53227a00f2a,
            0x0a13b7dbe3e75ea8, 0x79ffcf5f0c8d1100, 0x99ece4fbeb7313dc, 0xc3f87b278b8ee4e1, 0x16a11441a19431e6,
            0x67513ad840dac4c9, 0xc1e06281c8136e1c, 0x45fff01d45e28278, 0xbd34ecbec4c2dfd1, 0x8e1473a6621058bb,
            0x5a4d7b7f47cd940d, 0xc1895931810f51e4, 0xe2da937313d1c62b, 0x2457af5f33bd5dc8, 0xeee84ffaf344e299,
            0xba15cf4e32bdea22, 0xf6bdb5b21f9ec218, 0x1f57e7082a0e65e1, 0x0b0308f317319866, 0x9d7d1dd370af8c34,
            0x6dcaa675fa0b21f7, 0x81f6098b1b7fd554, 0x10b95cf55f11840f, 0x2618e9ca09439e81, 0xe3365a6aad20217a,
            0x91d9055d0163d663, 0x0e65d218a7775add, 0xd044ac77cd43fea5, 0x617118ea17e0b314, 0xc01d7ae0b8fa3785,
            0xacbe5ba699d219e2, 0xe7e11d4508e32276, 0xbfb68436cc59c458, 0x0625ba7690a02a54, 0x94a9adb4ad8289c3,
            0xfd9b4e6396f0b240, 0xb29486a69153d373, 0x541afedc53063ca5, 0x84b59a52889dddb7, 0x869d3adffcd07c97,
            0x13a2a3363624c07f, 0x9a5db06194d2cd66, 0x588fc64b9737ed95, 0xe1c08df219eb64bb, 0x361074055893ede1,
            0x9ca5480c4e77c5a8, 0x49f4b91279da85b3, 0xbb455871f21e0c38, 0x5ca49b23e994955b, 0xdb9261adea4fb9b6,
            0x1f439d23db8369a5, 0x4b23e1a8a2b567f0, 0xf7f9b8afc8a989f4, 0x2eb334a70476a1d5, 0x473826fbd4e3a603,
            0xf09a459b1860dd91, 0xc82e3ccb001f5a52, 0xd6cbdf410e30ce1d, 0x9b3bf1f29c0c524b, 0x896c2533de1e7e8a,
            0x9cdad573ce10cde1, 0xbb084c48360588db, 0x6140567a93f1c7a8, 0x66f960c75ec1a817, 0x3e6de19b9f056c71,
            0x3f563c8f3d00f8f2, 0xfa0db14f62a2aa2c, 0x4f085fd50a99a966, 0x9b4c8efdf3c99fb5, 0xbc18d391b6930fdc,
            0x75cfbab414b7cb69, 0x4c0894009af8eea7, 0x945283b2b9824dc7, 0x9db7b027499d18ca, 0x37510bb9f0fefc6f,
            0xfa2aceb93f756441, 0x604623f851ca0192, 0xebeeebf959db2c26, 0xb482490e0bdc79aa, 0x6ec59aa11bb8b941,
            0xe000f1d2445876af, 0x3810a2f6e9ca3e49, 0x54434cd018715317, 0xd243f33a759bc4ca, 0xb7859923fe236fb3,
            0x20d77e1aced47630, 0x31f0224fc82805e7, 0xcc6b181f051b66d0, 0x3c10226521a38a2c, 0x7a740af5acd8d654,
        ]

        xshr2 = Xoshiro256pp(0xa747f481346acb72, 0x8ad918349ab73966, 0xbec62b7cc97a0873, 0xf4975b3f04f272de)

        for i = 1:length(xshr256_test_2)
            @test next(xshr2) == xshr256_test_2[i]
        end
    end
end


text:
/* usage of variable initialized in both branches. */

int main () {
  int x;
  int y = 56;
  if (y + 45 <= 2) {
    x = 1;
  } else {
    x = 2;
  }
  printInt(x);
  return 0 ;

}

max_stars_repo_name: carlostome/Javalette-Compiler

max_issues_repo_name: carlostome/Javalette-Compiler

max_forks_repo_name: carlostome/Javalette-Compiler


text:
# Simulate a cell:
#    1. Randomly generate positions of two sets of molecules of given amounts m, n
#         - select b = x% of min(m, n) binding pairs, for b pairs, m - b and n - b independent molcules
#         - randomly generate the m - b and n - b molecules
#         - generate the positions of bound pairs d distance apart, then randomly generate a 3D position and squash it
#           to 2D
#             - accounts for antibody distance too
#    2. Randomly generate blinks from each molecule (Poisson?)
#         - accounts for multiple labeling too
#    3. Run the simulated cell through the algorithm and obtain median distance and fraction bound
#    4. Repeat for varying combinations of m, n, x, d
#         - x = { 0.0, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0 }
#         - m = n = { 10, 20, 50, 100, 200 }
#         - d = { 10, 20, 50, 100, 200 } nm

# Basis for choices:
#
# In a particularly dense image, I have observed 2430 molecules and 500 molecules in 25 square microns, and in a less-
# dense area, 301 and 55 molecules, respectively. Note: Using a merge radius of 200 nm practically limits density to
# ~200 molecules in an area this size. Higher density from temporally-displaced localization clouds would reduce
# detection further.
#
# 20 nm  = 10 nm distance + 5 nm nanobodies
# 80 nm  = 70 nm distance + 5 nm nanobodies OR
#          10 nm distance + 35 nm antibody stack
# 140 nm = 70 nm distance + 35 nm antibody stack
#
# For context, microtubulues are 24 nm across, ribosomes 30 nm across
#

fractionsbound = [0.0, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0]
moleculecounts = [100, 200, 500, 1000, 2000]
boundradii = [10, 20, 50, 100, 200]

cellradius = 8921 # 250 square micron circle

rootpath = "output/simulation"

using Distributed
using Statistics
using LocalizationMicroscopy
using FileIO
currentworkers = addprocs(exeflags="--project")
@everywhere using SMLMAssociationAnalysis_NCB

function generateunboundmolecules(molecules::Vector{Molecule}, moleculecount, cellradius)
    while length(molecules) < moleculecount
        unboundcoordinates = randomcoordinates2d(moleculecount - length(molecules), cellradius)
        unboundmolecules = [Molecule(Localization(length(molecules) + i, "", unboundcoordinates[1,i], unboundcoordinates[2,i], unboundcoordinates[3,i], 0, 1, 1)) for i ∈ 1:size(unboundcoordinates, 2)]
        append!(molecules, unboundmolecules)
        molecules = merge_close_molecules(molecules, 200)
        foreach(i -> molecules[i].index = i, eachindex(molecules))
    end
    molecules
end

for i ∈ 1:30
    results = ResultSimulate[]

    for moleculecount ∈ moleculecounts
        for fractionbound ∈ fractionsbound
            boundcount = Int(round(fractionbound * moleculecount))

            for boundradius ∈ boundradii
                fractionbound == 0.0 && boundradius != boundradii[1] && break

                println("Processing $moleculecount mols $fractionbound bound $boundradius radius $i")

                boundcoordinates1 = randomcoordinates2d(boundcount, cellradius)
                molecules1 = [Molecule(Localization(i, "", boundcoordinates1[1,i], boundcoordinates1[2,i], boundcoordinates1[3,i], 0, 1, 1)) for i ∈ 1:size(boundcoordinates1, 2)]

                boundcoordinates2 = boundcoordinates1 .+ randomcoordinates2d(boundcount, boundradius)
                molecules2 = [Molecule(Localization(i, "", boundcoordinates2[1,i], boundcoordinates2[2,i], boundcoordinates2[3,i], 0, 1, 1)) for i ∈ 1:size(boundcoordinates2, 2)]

                molecules1 = generateunboundmolecules(molecules1, moleculecount, cellradius)
                molecules2 = generateunboundmolecules(molecules2, moleculecount, cellradius)

                neighbors1, neighbors2, distances = exclusivenearestneighbors(molecules1, molecules2)

                percentileranks = montecarloaffinity(molecules1, molecules2, neighbors1, neighbors2, distances, 200, 10000)
                mediandistance = length(distances) > 0 ? median(distances) : NaN

                data1 = ChannelData("1", molecules1, neighbors1)
                data2 = ChannelData("2", molecules2, neighbors2)
                result = ResultSimulate("", "", 1, "$moleculecount mols $fractionbound bound $boundradius radius", i,
                                [data1, data2], distances, mediandistance, percentileranks)
                push!(results, result)
            end
        end
    end
    save(joinpath(rootpath, "simulationresults$i.jld2"), "results", results)
end

rmprocs(currentworkers)

# plot

nreplicates = 30

using StatsPlots

results = Vector{ResultSimulate}[]
for i ∈ 1:nreplicates
    push!(results, load(joinpath(rootpath, "simulationresults$i.jld2"))["results"])
end

medianmeasurements = Array{Float64,4}(undef, nreplicates, length(boundradii), length(fractionsbound), length(moleculecounts))
montecarlomeasurements = Array{Float64,4}(undef, nreplicates, length(boundradii), length(fractionsbound), length(moleculecounts))

using InvertedIndices

for i ∈ 1:nreplicates
    replicateresults = results[i]
    lessthanlimit = [(x.distances .< 200) .& (x.percentileranks .< 0.1) for x ∈ replicateresults]
    lessthan10 = count.(lessthanlimit) ./ length.(lessthanlimit)
    mediandistances = map(x -> x.mediandistance, replicateresults)

    medianmeasurements[i,:,1,:] .= reshape(repeat(mediandistances[1:36:end], inner=length(boundradii)), length(boundradii), length(moleculecounts))
    medianmeasurements[i,:,2:end,:] .= reshape(mediandistances[Not(1:36:end)], length(boundradii), length(fractionsbound)-1, length(moleculecounts))

    montecarlomeasurements[i,:,1,:] .= reshape(repeat(lessthan10[1:36:end], inner=length(boundradii)), length(boundradii), length(moleculecounts))
    montecarlomeasurements[i,:,2:end,:] .= reshape(lessthan10[Not(1:36:end)], length(boundradii), length(fractionsbound)-1, length(moleculecounts))
end

import StatsPlots.mm

# monte carlo plot
p = Array{Plots.Plot,2}(undef, length(moleculecounts), length(boundradii))
xpos = reshape(repeat(["0", "1", "2", "5", "10", "20", "50", "100"], inner=5 * nreplicates, outer=5), nreplicates, length(boundradii), length(fractionsbound), length(moleculecounts))
for i ∈ eachindex(moleculecounts)
    for j ∈ eachindex(boundradii)
        p[i,j] = boxplot(xpos[:,j,:,i], montecarlomeasurements[:,j,:,i], legend=:none, outliers=false, tickfontsize=12, seriescolor=:white, yaxis=(0:1), xrotation=60, left_margin=-0.5mm, bottom_margin=-3mm, gridopacity=0.2)
        i > 1 && plot!(p[i,j], yaxis = false, left_margin=-10mm)
        j < length(boundradii) && plot!(p[i,j], xaxis = false, bottom_margin=-10mm)
        dotplot!(p[i,j], xpos[:,j,:,i], montecarlomeasurements[:,j,:,i], legend=:none, bar_width=0.1, markerstrokewidth=0, markersize=2, markercolor=:black)
    end
end

plot(p..., layout=grid(length(moleculecounts), length(boundradii)), size=(1024,1024))
savefig(joinpath(rootpath, "simulation_montecarlo.png"))

# median plot
p = Array{Plots.Plot,2}(undef, length(moleculecounts), length(boundradii))
for i ∈ eachindex(moleculecounts)
    for j ∈ eachindex(boundradii)
        p[i,j] = boxplot(xpos[:,j,:,i], medianmeasurements[:,j,:,i], legend=:none, outliers=false, yaxis=((0, 1200), 0:200:1200), tickfontsize=12, seriescolor=:white, xrotation=60, left_margin=-0.5mm, bottom_margin=-3mm, gridopacity=0.2)
        i > 1 && plot!(p[i,j], yaxis = false, left_margin=-10mm)
        j < length(boundradii) && plot!(p[i,j], xaxis = false, bottom_margin=-10mm)
        dotplot!(p[i,j], xpos[:,j,:,i], medianmeasurements[:,j,:,i], legend=:none, bar_width=0.1, markerstrokewidth=0, markersize=2, markercolor=:black)
    end
end

plot(p..., layout=grid(length(moleculecounts), length(boundradii)), size=(1024,1024))
savefig(joinpath(rootpath,"simulation_median.png"))

# normalized monte carlo plot

zero_montecarlomeasurements = reshape(repeat(median(montecarlomeasurements[:,:,1,:], dims = 1), inner = (30, 8, 1)), 30, 5, 8, 5)
one_montecarlomeasurements = reshape(repeat(median(montecarlomeasurements[:,:,length(fractionsbound),:], dims = 1), inner = (30, 1, 1), outer = (1, 8, 1)), 30, 5, 8, 5)
normalizedmontecarlomeasurements = (montecarlomeasurements .- zero_montecarlomeasurements) ./ (one_montecarlomeasurements .- zero_montecarlomeasurements)

p = Array{Plots.Plot,2}(undef, length(moleculecounts), length(boundradii))
xpos = reshape(repeat(["0", "1", "2", "5", "10", "20", "50", "100"], inner=5 * nreplicates, outer=5), nreplicates, length(boundradii), length(fractionsbound), length(moleculecounts))
for i ∈ eachindex(moleculecounts)
    for j ∈ eachindex(boundradii)
        p[i,j] = boxplot(xpos[:,j,:,i], normalizedmontecarlomeasurements[:,j,:,i], legend=:none, outliers=false, tickfontsize=12, seriescolor=:white, yaxis=(0:1), xrotation=60, left_margin=-0.5mm, bottom_margin=-3mm, gridopacity=0.2)
        plot!(p[i,j], xpos[1,j,:,i], fractionsbound, lineopacity = 0.25, linecolor=:black, linewidth=3)
        i > 1 && plot!(p[i,j], yaxis = false, left_margin=-10mm)
        j < length(boundradii) && plot!(p[i,j], xaxis = false, bottom_margin=-10mm)
        dotplot!(p[i,j], xpos[:,j,:,i], normalizedmontecarlomeasurements[:,j,:,i], legend=:none, bar_width=0.1, markerstrokewidth=0, markersize=2, markercolor=:black)
    end
end

plot(p..., layout=grid(length(moleculecounts), length(boundradii)), size=(1024,1024))
savefig(joinpath(rootpath,"simulation_montecarlo_normalized.png"))

# normalized monte carlo plot deviation

deviations = (median(normalizedmontecarlomeasurements[:,:,eachindex(fractionsbound)[Not(1)],:], dims = 1) .- reshape(repeat(fractionsbound[Not(1)], inner = 5, outer = 5), 1, 5, 7, 5)) ./ reshape(repeat(fractionsbound[Not(1)], inner = 5, outer = 5), 1, 5, 7, 5)

p = Array{Plots.Plot,2}(undef, length(moleculecounts), length(boundradii))
xpos = reshape(repeat(["1", "2", "5", "10", "20", "50", "100"], inner=5, outer=5), length(boundradii), length(fractionsbound) - 1, length(moleculecounts))
for i ∈ eachindex(moleculecounts)
    for j ∈ eachindex(boundradii)
        p[i,j] = plot(xpos[j,:,i], deviations[1,j,:,i], legend=:none, outliers=false, tickfontsize=12, yaxis=((-5, 2), -5:1:2), xrotation=60, left_margin=-0.5mm, bottom_margin=-3mm, gridopacity=0.2, linewidth=3, linecolor=:black)
        i > 1 && plot!(p[i,j], yaxis = false, left_margin=-10mm)
        j < length(boundradii) && plot!(p[i,j], xaxis = false, bottom_margin=-10mm)
        plot!(p[i,j], xpos[j,:,i], repeat([0], 7), linecolor=:gray, linewidth=3, lineopacity=0.5)
    end
end

plot(p..., layout=grid(length(moleculecounts), length(boundradii)), size=(1024,1024))
savefig(joinpath(rootpath,"simulation_montecarlo_normalized_deviations.png"))


text:
# code for drawing interdigit capacitor
"""
    interdigit_capacitor!{T,S}(c::Cell{T,S}, terminal_width, terminal_length,
        overlap, finger_width, finger_spacing, end_gap,
        meta::Meta=GDSMeta(0,0); terminal_width2=terminal_width)
Create a positive trace of an interdigital capacitor of a specified
terminal width, terminal length, finger width, finger spacing, and end gap.
The width of the second terminal can be asymetrically defined with the
keyword argument `terminal_width2`.
"""
function interdigit_capacitor!(c::Cell{T,S}, terminal_width, terminal_length,
    overlap, finger_width, finger_spacing, end_gap, meta::Meta=GDSMeta(0,0);
    terminal_width2=terminal_width) where {T<:Coordinate,S<:GDSMeta}

    # terminal
    r1 = Rectangle(terminal_width, terminal_length)
    r2 = Rectangle(terminal_width2, terminal_length)

    # fingers
    fingers = Cell{T,S}(uniquename("fingers"))
    finger_length = overlap + end_gap

    remainder = rem(terminal_length, 2 * (finger_width + finger_spacing))

    # criterion to determine npairs and skiplast
    if remainder < finger_width
        npairs = Int(fld(terminal_length, 2 * (finger_width + finger_spacing)))
        skiplast = 0
    elseif remainder < 2 * finger_width + finger_spacing
        npairs = Int(fld(terminal_length,
            2 * (finger_width + finger_spacing)) + 1)
        skiplast = 1
    else
        npairs = Int(fld(terminal_length,
            2 * (finger_width + finger_spacing)) + 1)
        skiplast = 0
    end
    interdigit!(fingers, finger_width, finger_length, finger_spacing,
        end_gap, npairs, skiplast, meta)

    h = height(bounds(fingers))
    fingers_offset = (terminal_length - h)/2

    trans = Translation(terminal_width, fingers_offset)
    polys = [trans(p) for p in polygon.(elements(fingers))]
    for p in polys
        render!(c, p, meta)
    end
    render!(c, r1, meta)
    render!(c, r2 + Point(terminal_width + end_gap + finger_length,
        zero(T)), meta)
    return c
end


text:
using Random

include("ga_julia/simulated_annealing.jl")
include("ga_julia/functions.jl")
include("ga_julia/GA.jl")

Random.seed!(0)

x = [3.0, 3.0]
k_max = 1000

# fast annealing
T0 = 25
t(k) = T0/k

threads = Threads.nthreads()

if threads == 1
    println("\nBenchmarking Simulated Annealing baseline (Julia)...")
    @time simulated_annealing(ackley, copy(x), t, k_max)
else
    println("\nBenchmarking Simulated Annealing (Julia, $(threads) threads)...")
    @time simulated_annealing_p(ackley, copy(x), t, k_max)
end

# GA

f = x -> levy3(x)

S = TruncationSelection(10)
C = SinglePointCrossover()
M = GaussianMutation(0.1)

k_max = 10
m = 40
population = rand_population_uniform(m, [0.0, 0.0], [4.0, 4.0])

if threads == 1
    println("\nBenchmarking Genetic Algorithm baseline (Julia)...")
    @time genetic_algorithm(f, population, k_max, S, C, M)
else
    println("\nBenchmarking Genetic Algorithm (Julia, $(threads) threads)...")
    @time genetic_algorithm(f, population, k_max, S, C, M; parallel=true)
end

max_stars_repo_name: camirmas/distributed-ga

max_issues_repo_name: camirmas/distributed-ga

max_forks_repo_name: camirmas/distributed-ga


text:
# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder, Pkg

name = "rubberband"
version = v"1.9.0"

# Collection of sources required to complete build
sources = [
    GitSource("https://github.com/breakfastquay/rubberband.git", "6659d61f010e19080d62aa2fd2eae88f16e5d9c9")
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/rubberband/
./configure --prefix=${prefix} --build=${MACHTYPE} --host=${target} CXXFLAGS="${CXXFLAGS} -DHAVE_FFTW3 -DHAVE_LIBSAMPLERATE" enable_vamp=no enable_ladspa=no
make DYNAMIC_LDFLAGS="${LDFLAGS} -shared" DYNAMIC_EXTENSION=".$dlext" DYNAMIC_EXTENSION=".$dlext" PROGRAM_TARGET="bin/rubberband$exeext"
make install DYNAMIC_EXTENSION=".$dlext" PROGRAM_TARGET="bin/rubberband$exeext"
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = expand_cxxstring_abis(supported_platforms())

# The products that we will ensure are always built
products = [
    LibraryProduct("librubberband", :librubberband)
    ExecutableProduct("rubberband", :rubberband)
]

# Dependencies that must be installed before this package can be built
dependencies = Dependency[
    Dependency("FFTW_jll"),
    Dependency("libsamplerate_jll"),
    Dependency("libsndfile_jll"),
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: sharanry/Yggdrasil

max_issues_repo_name: sharanry/Yggdrasil

max_forks_repo_name: sharanry/Yggdrasil