text:
using BinaryBuilder

name = "Bzip2"
version = v"1.0.6"

# Collection of sources required to build bzip2
sources = [
    GitSource("https://github.com/enthought/bzip2-1.0.6.git",
              "288acf97a15d558f96c24c89f578b724d6e06b0c"),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/bzip2-*/

# Welp, auto-patching an include because otherwise win32/64 bzip2 won't cross-compile
sed -i 's/sys\\stat\.h/sys\/stat\.h/g' bzip2.c

# Override stubborn makevars
CFLAGS="-Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64 -fPIC"
OBJS="blocksort.o huffman.o crctable.o randtable.o compress.o decompress.o bzlib.o"
make CFLAGS="${CFLAGS}" -j${nproc} ${OBJS}
make CFLAGS="${CFLAGS}" PREFIX=${prefix} install

# Build dynamic library
if [[ "${target}" == *-darwin* ]]; then
    $CC -shared -current_version 1.0.6 -compatibility_version 1.0 -o libbz2.1.0.6.dylib $LDFLAGS $OBJS
    ln -s libbz2.1.0.6.dylib libbz2.1.0.dylib
    ln -s libbz2.1.0.6.dylib libbz2.1.dylib
    ln -s libbz2.1.0.6.dylib libbz2.dylib
    mv libbz2*.dylib ${prefix}/lib/
elif [[ "${target}" == *-mingw* ]]; then
    $CC -shared -o libbz2-1.dll $LDFLAGS $OBJS
    ln -s libbz2-1.dll libbz2.dll
    mv libbz2*.dll ${prefix}/bin/
else
    $CC -shared -Wl,-soname -Wl,libbz2.so.1.0 -o libbz2.so.1.0.6 $LDFLAGS $OBJS
    ln -s libbz2.so.1.0.6 libbz2.so.1.0
    ln -s libbz2.so.1.0.6 libbz2.so.1
    ln -s libbz2.so.1.0.6 libbz2.so
    mv libbz2.so* ${prefix}/lib/
fi

# Add pkg-config file
mkdir -p ${prefix}/lib/pkgconfig
cat << EOF > $prefix/lib/pkgconfig/bzip2.pc
prefix=${prefix}
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
sharedlibdir=\${libdir}
includedir=\${prefix}/include

Name: bzip2
Description: bzip2 compression library
Version: 1.0.6

Requires:
Libs: -L\${libdir} -L\${sharedlibdir} -lbz2
Cflags: -I\${includedir}
EOF
"""

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

# The products that we will ensure are always built
products = [
    LibraryProduct("libbz2", :libbzip2),
]

# Dependencies that must be installed before this package can be built
dependencies = [
]

build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: jpsamaroo/Yggdrasil

max_issues_repo_name: jpsamaroo/Yggdrasil

max_forks_repo_name: jpsamaroo/Yggdrasil


text:
using Base: root_module
using Pkg
Pkg.activate("Fastnet.jl")
using Fastnet
using Documenter

makedocs(
    root="C://Users//Thilo//Documents//DVLP//FastnetDev//Fastnet//Fastnet.jl//docs",
    modules     = [Fastnet],
    format = Documenter.HTML(
        prettyurls = false,
        canonical = "https://bridgewalker.github.io/Fastnet.jl/stable/",
        assets = ["assets//favicon.ico","assets//myjs.js","assets/mystyle.css"],
    ),
    sitename    = "Fastnet.jl",
    pages       = Any[
        "Welcome"             => "index.md",
        "Tutorial"            => "tutorial.md",
#        "Examples"            => "example.md",
        "Key Concepts"        => "concepts.md",
        "Function Reference"  => "reference.md",
        "Background"          => "background.md",
        "FAQs"                => "faq.md",
        "Customization"       => "customization.md",
        "About"               => "about.md"
    ]
)

ext: jl


text:
#
# Syntax tree and parsing.
#

# Parsed query.
abstract AbstractSyntax

# Parse a query.
syntax(syntax::AbstractSyntax) = syntax

# Literal node.
typealias Scalar Union{Void,Int,Float64,AbstractString,Regex}

immutable LiteralSyntax <: AbstractSyntax
    val::Scalar
end

show(io::IO, syntax::LiteralSyntax) = print(io, syntax.val == nothing ? "null" : syntax.val)

# Combinator constructor.
immutable ApplySyntax <: AbstractSyntax
    fn::Symbol
    args::Vector{AbstractSyntax}
    postfix::Bool
end

ApplySyntax(fn) = ApplySyntax(fn, [], false)
ApplySyntax(fn, args) = ApplySyntax(fn, args, false)

show(io::IO, syntax::ApplySyntax) =
    if isempty(syntax.args)
        print(io, syntax.fn)
    elseif syntax.postfix
        if length(syntax.args) == 1
            print(io, syntax.args[1], ":", syntax.fn)
        else
            print(io, syntax.args[1], ":", syntax.fn, "(", join(syntax.args[2:end], ","), ")")
        end
    else
        print(io, syntax.fn, "(", join(syntax.args, ","), ")")
    end

# Composition of combinators.
immutable ComposeSyntax <: AbstractSyntax
    f::AbstractSyntax
    g::AbstractSyntax
end

show(io::IO, syntax::ComposeSyntax) = print(io, syntax.f, ".", syntax.g)

# Parsing.
syntax(str::AbstractString) =
    ex2syn(parse(string("(", str, ")")))
syntax(ex::Union{Symbol,QuoteNode,Scalar,Expr}) =
    ex2syn(parse(string("(", ex, ")")))

ex2syn(ex::Symbol) = ex == :null ? LiteralSyntax(nothing) : ApplySyntax(ex, [])
ex2syn(ex::QuoteNode) = ApplySyntax(ex.value, [])
ex2syn(ex::Scalar) = LiteralSyntax(ex)

function ex2syn(ex::Expr)
    if ex.head == :(.) && length(ex.args) == 2
        return ComposeSyntax(map(ex2syn, ex.args)...)
    elseif ex.head == :(:) && length(ex.args) >= 2
        syn = ex2syn(ex.args[1])
        for ex in ex.args[2:end]
            tail = nothing
            while isa(ex, Expr) && ex.head == :(.) && length(ex.args) == 2
                tail = tail == nothing ? ex2syn(ex.args[2]) : ComposeSyntax(ex2syn(ex.args[2]), tail)
                ex = ex.args[1]
            end
            while isa(ex, Expr) && ex.head == :call && length(ex.args) == 2 &&
                    isa(ex.args[1], Expr) && ex.args[1].head == :(.) && length(ex.args[1].args) == 2
                tail =
                    tail == nothing ?
                        ex2syn(Expr(:call, ex.args[1].args[2], ex.args[2])) :
                        ComposeSyntax(ex2syn(Expr(:call, ex.args[1].args[2], ex.args[2])), tail)
                ex = ex.args[1].args[1]
            end
            if isa(ex, Symbol)
                syn = ApplySyntax(ex, [syn], true)
            elseif isa(ex, Expr) && ex.head == :call && isa(ex.args[1], Symbol)
                args = AbstractSyntax[syn]
                append!(args, map(ex2syn, ex.args[2:end]))
                syn = ApplySyntax(ex.args[1], args, true)
            else
                dump(ex)
                error("invalid query postfix notation: $ex")
            end
            if tail != nothing
                syn = ComposeSyntax(syn, tail)
            end
        end
        return syn
    elseif ex.head == :call && isa(ex.args[1], Symbol)
        return ApplySyntax(ex.args[1], map(ex2syn, ex.args[2:end]))
    elseif ex.head == :(=>)
        return ApplySyntax(:(=>), map(ex2syn, ex.args))
    elseif ex.head == :(&&)
        return ApplySyntax(:(&), map(ex2syn, ex.args))
    elseif ex.head == :(||)
        return ApplySyntax(:(|), map(ex2syn, ex.args))
    elseif ex.head == :ref
        return ApplySyntax(:get, map(ex2syn, ex.args))
    elseif ex.head == :vect
        return ApplySyntax(:array, map(ex2syn, ex.args))
    elseif ex.head == :call
        return ex2syn(pushcall(ex.args[1], ex.args[2:end]))
    elseif ex.head == :comparison && length(ex.args) >= 3 && isa(ex.args[2], Symbol)
        op = ApplySyntax(ex.args[2], AbstractSyntax[ex2syn(ex.args[1]), ex2syn(ex.args[3])])
        k = 3
        while length(ex.args) >= k+2
            op = ApplySyntax(
                :(&),
                AbstractSyntax[
                    op,
                    ApplySyntax(ex.args[k+1], AbstractSyntax[ex2syn(ex.args[k]), ex2syn(ex.args[k+2])])])
            k = k+2
        end
        return op
    elseif ex.head == :macrocall && length(ex.args) == 2 && ex.args[1] == Symbol("@r_str")
        return LiteralSyntax(eval(ex))
    else
        dump(ex)
        println(ex.head == :macrocall)
        println(length(ex.args) == 2)
        println(ex.args[1] == :@r_str)
        error("invalid query notation: $ex")
    end
end

pushcall(ex::QuoteNode, args) =
    Expr(:call, ex.value, args...)

pushcall(ex::Expr, args) =
    Expr(ex.head, ex.args[1:end-1]..., pushcall(ex.args[end], args))

pushcall(ex, args) =
    error("invalid query call notation: $ex")

max_stars_repo_name: rbt-lang/rbt-proto

max_issues_repo_name: rbt-lang/rbt-proto

max_forks_repo_name: rbt-lang/rbt-proto


text:
## MIT license (c) 2019 by Andrew Lyasoff
##
## Julia 1.1.0.
## This code implements on single CPUs the numerical program for computing the exercise boundary
## and the price of an American-style call option described in Sec. 18.4 of
## "Stochastic Methods in Asset Pricing" (pp. 514-516).
## The same program is implemented in Python in Appendix B.3 of SMAP.
##

begin
    ## The number if grid points in the time domain is set below.
    ## 5000 is excessively large and is meant to test Julia's capabilities.
    total_grid_no=5000 #this number must be divisible by pnmb
    include("Num-Prog-US-Call-Julia-functions.jl")
    nodes,weights=gausslegendre( 200 );
end

#set the parameters in the model
begin
    KK=40.0; # strike price
    sigma=0.3;
    delta=0.07; # dividend rate
    rr=0.02; # interest rate
    TT=0.5; # time to maturity in years
    DLT=TT/(total_grid_no); # distance between two grid points in the time domain
    ABSC=range(0.0,length=total_grid_no+1,stop=TT) # the entire grid on the time domain
    VAL=[max(KK,KK*(rr/delta)) for x in ABSC]; # first guess for the exercise boundary
end

## run the main routine
## The third argument is an upper bound on the total iterations to run.
## The last two arguments control FastGaussQuadrature.
## masterF(start_iter,nmb_of_iter,conv_tol,K,σ,δ,r,T,Δ,no_grd_pnts,vls,nds,wghts)
##
@time ABSC,VAL,conv,iterations=mainF(0,100,1.0e-5,KK,sigma,delta,rr,TT,DLT,total_grid_no,VAL,nodes,weights);
## 15.649206 seconds (406.60 M allocations: 9.109 GiB, 4.51% gc time)

# The call to masterF can be repeated with the most recent VAL
# and the second argument set to the number of already performed iterations.

conv #The achived convergence
#9.63110706209136e-6

iterations
#23

f=CubicSplineInterpolation(ABSC,VAL,extrapolation_bc = Interpolations.Line());

begin
    using Plots
    pyplot()
end

begin
    plotgrid=ABSC[1]:.001:ABSC[end];
    pval=[f(x) for x in plotgrid];
    plot(plotgrid,pval,label="exercise boundary")
    xlabel!("real time")
    ylabel!("underlying spot price")
end

f(0)
#53.58270125587188

## The price of the EU at-the-money call with 0.5 years to expiry.
EUcall(KK,TT,KK,sigma,rr,delta)
#2.8378265020118754

## The early exercise premium for a US at-the-money call with 0.5 years to expiry.

begin
    an=[(0.5*(TT-0.0))*exp(-delta*((1/2)*(TT-0.0)*s+(1/2)*(0.0+TT)-0.0))*(KK*delta/2)*F(1,0.0,(1/2)*(TT-0.0)*s+(1/2)*(0.0+TT),f((1/2)*(TT-0.0)*s+(1/2)*(0.0+TT))/KK,rr,delta,sigma) for s in nodes]
    bn=[(0.5*(TT-0.0))*exp(-rr*((1/2)*(TT-0.0)*s+(1/2)*(0.0+TT)-0.0))*(rr*KK/2)*F(-1,0.0,(1/2)*(TT-0.0)*s+(1/2)*(0.0+TT),f((1/2)*(TT-0.0)*s+(1/2)*(0.0+TT))/KK,rr,delta,sigma)  for s in nodes]
    aaa=weights'*an;
    bbb=weights'*bn;
    EEP=aaa-bbb;
end

EEP
#0.10081943488743587

## The early exercise premium for a US at-the-money call with 0.5 years to expiry.
EUcall(KK,TT,KK,sigma,rr,delta)+EEP
#2.938645936899311

max_stars_repo_name: AndrewLyasoff/SMAP

max_issues_repo_name: lhyzh/SMAP

max_forks_repo_name: lhyzh/SMAP


text:
using Deconvolution
using Test, Random, FFTW

Random.seed!(42) # Fixed random seed

##### Wiener deconvolution

x = range(0, stop = 10, length = 15)
s = sinpi.(x) .- 1.5cos.(x)
n = rand(length(s))

@testset "No blurring" begin
    @test wiener(s + n, s, 0.5) ≈
        [-1.5300909616077885,-0.4607553796475224,-1.73313390900302,0.8555916473930066,
         2.2692599396286277,0.7989152977213652,1.1431959877178226,-0.8118586562136341,
         -1.6519310564150556,-0.4882369786466372,-1.0387206070662094,
         -0.8324097927723406,1.8208048770862535,0.7427114498907227,1.0587917379150844]
end

@testset "Blurring" begin
    blurring_ft  = exp.(-0.001 * (range(-div(length(x), 2), length = length(x)) .^ 2) .^ (5 // 6))
    blurred_s_ft = fftshift(blurring_ft) .* fft(s) .+ fft(n)
    blurred_s    = real(ifft(blurred_s_ft))
    blurring     = ifft(fftshift(blurring_ft))
    @test wiener(blurred_s, s, 0.5, blurring) ≈
        [-1.5288865581538547,-0.4592713125163481,-1.7360819833552192,0.8525544925861452,
         2.272966823544607,0.8021732396525801,1.14091217864045,-0.8166101187683248,
         -1.6455701546601862,-0.4926325667795202,-1.0323127293402468,
         -0.8380228416032021,1.8214493186523952,0.7447075584686396,1.0566048110361044]
end


text:
@discord_object struct ActivityTimestamps
    start::Union{Int, DateTime}
    end_::Union{Int, DateTime}
end

StructTypes.names(::Type{ActivityTimestamps}) = ((:end_, :end),)


text:
# ------------------------------------------------------------------
# Licensed under the ISC License. See LICENSE in the project root.
# ------------------------------------------------------------------

# ----------------
# IMPLEMENTATIONS
# ----------------
include("statistics/data.jl")
include("statistics/solutions.jl")


text: include("NRI.jl")


text:
@safetestset "JS rendering" begin
  @safetestset "JSON rendering" begin
    @safetestset "JSON view rendering with vars" begin
      using Genie, Genie.Renderer, Genie.Renderer.Json

      jsonview = raw"
      Dict(vars(:root) => Dict(lang => greet for (lang,greet) in vars(:greetings)))
      "

      viewfile = mktemp()
      write(viewfile[2], jsonview)
      close(viewfile[2])

      words = Dict(:en => "Hello", :es => "Hola", :pt => "Ola", :it => "Ciao")

      r = json(Genie.Renderer.Path(viewfile[1]), root = "greetings", greetings = words)

      @test String(r.body) == """{"greetings":{"en":"Hello","it":"Ciao","pt":"Ola","es":"Hola"}}"""

      Genie.Renderer.clear_task_storage()
    end;
  end;
end;


text:
abstract type Grid_Agent <: Agent end

@with_kw mutable struct Conv_Generator <: Grid_Agent
    OPEX_DA::Float64 = 0.0
    OPEX_RT::Float64 = 0.0
end

@with_kw mutable struct Res_Generator <: Grid_Agent
    attributes::Any
end

@with_kw mutable struct Load_Shed <: Grid_Agent
    VoLL::Float64
end

@with_kw mutable struct Central_Opt
    Conv_Generators::Any = Conv_Generator()
    Res_Generators::Any = Res_Generator()
end

@with_kw mutable struct Aggregator <: Grid_Agent
    name::String = "anonymus"
    battery::Any = Battery() #OWN ESS
    pv::Any = PV() #OWN PV
    connection_limit::Float64  = 0.0 #Line power limit on the aggregated exchange
    preferences::Preferences = Preferences()
    DER_agents::Any = DER_Agent()
    max_DA::Float64 = 100.0
    max_RT::Float64 = 100.0
end


text:
# In this simulation, we allow different products to have different mean utilities
# and also individual specific utility

using Random
using Distributions
using StatsBase
using JLD2

Random.seed!(42)

N = 10_000  # num of consumers
J = 10      # num of products
K = 5       # num of char dim

X = randn(J, K) # Product characteristics
ξ = rand(J-1)
β_mean = [1, -2, 3, -4, 5] .* 0.1
σ = [1, 2, 1, 2, 1] .* 0.1
β = zeros(N, K)
for i in 1:K
    β[:,i] = randn(N) .* σ[i] .+ β_mean[i]
end

index = β*X' .+ ones(N)*vcat(0, ξ)' # N-by-J
Y = zeros(N)
y_id = 1:J
for i in 1:N
    y_prob = exp.(index[i,:]) ./ sum(exp.(index[i,:]))
    Y[i] = sample(y_id, Weights(y_prob))
end

y_prob_emp = [mean(Y.==i) for i in 1:J ]
@show y_prob .- y_prob_emp |> maximum

@save "example/data/hetero.jld2" Y X β_mean ξ σ


text:
export emerge

function recompile_packages()
    for pkg in keys(Pkg.installed())
        try
            info("Compiling: $pkg")
            @eval using $(Symbol(pkg))
        catch err
            warn("Unable to precompile: $pkg")
            warn(err)
        finally
            println(SEPARATOR)
        end
    end
end

function update_winrpm()
    @static if is_windows()
        @eval using WinRPM
        WinRPM.update()
    end
end

# include(joinpath(JULIA_HOME, Base.DATAROOTDIR, "julia", "build_sysimg.jl"))

#=
function rebuild_userimg()
    img_path = joinpath(
        homedir(),
        ".julia",
        "v" * string(VERSION)[1:3],
        "userimg"
    )
    build_sysimg(
        img_path,
        "native",
        joinpath(homedir(), "userimg.jl"),
        force = true
    )
end
=#

function emerge()
    tic()
    Pkg.update()
    update_winrpm()
    Pkg.build()
    recompile_packages()
    # rebuild_userimg()
    toc()
end


text:
# Autogenerated wrapper script for libspatialite_jll for armv7l-linux-musleabihf-cxx11
export libspatialite, mod_spatialite

using SQLite_jll
using GEOS_jll
using PROJ_jll
using Libiconv_jll
using Zlib_jll
using XML2_jll
JLLWrappers.@generate_wrapper_header("libspatialite")
JLLWrappers.@declare_library_product(libspatialite, "libspatialite.so.7")
JLLWrappers.@declare_library_product(mod_spatialite, "mod_spatialite.so.7")
function __init__()
    JLLWrappers.@generate_init_header(SQLite_jll, GEOS_jll, PROJ_jll, Libiconv_jll, Zlib_jll, XML2_jll)
    JLLWrappers.@init_library_product(
        libspatialite,
        "lib/libspatialite.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        mod_spatialite,
        "lib/mod_spatialite.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
using EventStreamGLM
using EventStreamMatrix
using Test
using LinearAlgebra
using IterativeSolvers
using GLM
using BSplines
using StatsFuns

@testset "EventStreamGLM.jl" begin
    X1 = randn(1000, 10)
    lp1 = 1.0*X1[:, 1] + 2.0 * X1[:, 2]
    y_cts = lp1 + randn(1000)./sqrt(2)
    u = rand(length(lp1))
    y_bin = u .<= logistic.(lp1)
    pp0 = GLM.DensePredChol(X1, false) # Base GLM fitting
    rr0 = GLM.GlmResp(y_bin, Binomial(), LogitLink(), repeat([0.0], length(y_bin)), repeat([1.0], length(y_bin)))
    mod0 = GeneralizedLinearModel(rr0, pp0, false)
    fit!(mod0)
    evnts = rand(Float64, 150)
    labs = repeat([1, 2], 75)
    eventstream = sort(collect(zip(evnts, labs)))
    X2 = FirstOrderBSplineEventStreamMatrix(eventstream, ["a", "b"], 0.001, 1.0, 4, [0.0, 0.1, 0.2], true)
    Q  = Matrix(X2)
    lp2 = 1.0*Q[:, 1] + 2.0*Q[:, 2]
    y_cts2 = lp2 + randn(1000)./sqrt(2)
    u2 = rand(length(lp2))
    y_bin2 = u2.<logistic.(lp2)
    @testset DensePredConjGrad begin
        pp1 = DensePredConjGrad(X1)
        rr1 = GLM.GlmResp(y_cts, Normal(), IdentityLink(),
             repeat([0.0], length(y_cts)), repeat([1.0], length(y_cts)))
        mod1 = GeneralizedLinearModel(rr1, pp1, false)
        fit!(mod1)
        @test abs(coef(mod1)[1] - 1.0) <= 0.05
        @test abs(coef(mod1)[2] - 2.0) <= 0.05
        @test abs(coef(mod1)[3]) <= 0.05
        pp2 = DensePredConjGrad(X1)
        rr2 = GLMResp(y_bin, Binomial(), LogitLink(), repeat([0.0], length(y_bin)),
            repeat([1.0], length(y_bin)))
        mod2 = GeneralizedLinearModel(rr2, pp2, false)
        fit!(mod2)
        @test abs(coef(mod2)[1] - 1.0) <= 0.1
        @test abs(coef(mod2)[2] - 2.0) <= 0.1
        @test abs(coef(mod2)[3] - 0.0) <= 0.1
        @test sum(abs.(coef(mod2) - coef(mod0))) <= 0.0001
    end
    @testset EventStreamPredConjGrad begin
        pp3 = EventStreamPredConjGrad(X2)
        rr3 = GLM.GlmResp(y_cts2, Normal(), IdentityLink(),
            repeat([0.0], length(y_cts2)), repeat([1.0], length(y_cts2)))
        mod3 = GeneralizedLinearModel(rr3, pp3, false)
        fit!(mod3)

        pp4 = EventStreamPredConjGrad(E)
        rr4 = GLMResp(y_bin2)
        #TODO finish these tests
    end
    @testset simulation begin
        basis = BSplineBasis(4, [0.0, 10.0, 20.0])
        other_events = 100000 .* rand(Float64, 10000)
        labels = repeat([1, 2], 5000)
        eventstream = sort(collect(zip(other_events, labels)))
        other_coefs = [randn(length(basis))/4 for _ in 1:2]
        self_coefs = randn(length(basis))
        other_kernels = [Spline(basis, c) for c in other_coefs]
        self_kernel = Spline(basis, min.(0, self_coefs))
        λ_0 = 0.4
        P1 = EventStreamProcess(eventstream, ["a", "b"], 100000.0,
            basis, 20.0, λ_0, other_kernels, self_kernel)
    end

end


text:
""" Verify that the data is self-consistent and consistent with the likelihood """
function check_data!(X::AbstractArray{T1,N1},y::AbstractArray{T2,N2},likelihood::Likelihood) where {T1<:Real,T2,N1,N2}
    @assert (size(y,1)==size(X,1)) "There is not the same number of samples in X and y";
    @assert N1 <= 2 "The input matrix X can only be a vector or a matrix"
    if N1 == 1 #Reshape a X vector as a matrix
        X = reshape(X,length(X),1);
    end
    y,nLatent,likelihood = treat_labels!(y,likelihood)
    return X,y,nLatent,likelihood
end

""" Verify that the likelihood and inference are compatible (are implemented) """
function check_implementation(model::Symbol,likelihood::L,inference::I) where {I<:Inference,L<:Likelihood}
    if isa(likelihood,GaussianLikelihood)
        if model == :GP && inference isa Analytic
            return true
        elseif model == :SVGP && inference isa AnalyticVI
            return true
        else
            return false
        end
    elseif likelihood isa StudentTLikelihood
        if inference isa AnalyticVI
            return true
        elseif model == :VGP && inference isa GibbsSampling
            return true
        else
            return false
        end
    elseif likelihood isa LaplaceLikelihood
        if inference isa AnalyticVI
            return true
        else
            return false
        end
    elseif likelihood isa LogisticLikelihood
        if inference isa AnalyticVI
            return true
        elseif model == :VGP && inference isa GibbsSampling
            return true
        else
            return false
        end
    elseif likelihood isa BayesianSVM
        if inference isa AnalyticVI
            return true
        else
            return false
        end
    elseif likelihood isa SoftMaxLikelihood
        if inference isa MCIntegrationVI
            return true
        else
            return false
        end
    elseif likelihood isa LogisticSoftMaxLikelihood
        if inference isa AnalyticVI
            return true
        elseif model == :VGP && inference isa GibbsSampling
            return true
        elseif inference isa MCIntegrationVI
            return true
        else
            return false
        end
    elseif likelihood isa PoissonLikelihood
        if inference isa AnalyticVI
            return true
        else
            return false
        end
    else
        return false
    end
end


text:
using Jecco.AdS5_3_1

grid = SpecCartGrid3D(
    x_min            = -5.0,
    x_max            =  5.0,
    x_nodes          =  32,
    y_min            = -5.0,
    y_max            =  5.0,
    y_nodes          =  64,
    u_outer_min      =  0.1,
    u_outer_max      =  1.003,
    u_outer_domains  =  3,
    u_outer_nodes    =  24,
    u_inner_nodes    =  12,
    fd_order         =  4,
    sigma_diss       =  0.2,
)

id = BlackBranePert(
    B1_amp  = 0.005,
    xmax = grid.x_max,
    xmin = grid.x_min,
    ymax = grid.y_max,
    ymin = grid.y_min,
)

evoleq = AffineNull(
    phi0           = 0.0,
    potential      = ZeroPotential(),
    gaugecondition = ConstantAH(u_AH = 1.0),
)

io = InOut(
    out_boundary_every  = 10,
    out_bulk_every      = 200,
    out_gauge_every     = 10,
)

integration = Integration(
    dt              = 0.002,
    tmax            = 10.0,
    ODE_method      = AdS5_3_1.AB3(),
    filter_poststep = true,
)

run_model(grid, id, evoleq, integration, io)


text:
export gmm_probability

import ..GMM

using mlpack._Internal.io

import mlpack_jll
const gmm_probabilityLibrary = mlpack_jll.libmlpack_julia_gmm_probability

# Call the C binding of the mlpack gmm_probability binding.
function gmm_probability_mlpackMain()
  success = ccall((:gmm_probability, gmm_probabilityLibrary), Bool, ())
  if !success
    # Throw an exception---false means there was a C++ exception.
    throw(ErrorException("mlpack binding error; see output"))
  end
end

" Internal module to hold utility functions. "
module gmm_probability_internal
  import ..gmm_probabilityLibrary

import ...GMM

# Get the value of a model pointer parameter of type GMM.
function IOGetParamGMM(paramName::String)::GMM
  GMM(ccall((:IO_GetParamGMMPtr, gmm_probabilityLibrary), Ptr{Nothing}, (Cstring,), paramName))
end

# Set the value of a model pointer parameter of type GMM.
function IOSetParamGMM(paramName::String, model::GMM)
  ccall((:IO_SetParamGMMPtr, gmm_probabilityLibrary), Nothing, (Cstring, Ptr{Nothing}), paramName, model.ptr)
end

# Serialize a model to the given stream.
function serializeGMM(stream::IO, model::GMM)
  buf_len = UInt[0]
  buf_ptr = ccall((:SerializeGMMPtr, gmm_probabilityLibrary), Ptr{UInt8}, (Ptr{Nothing}, Ptr{UInt}), model.ptr, Base.pointer(buf_len))
  buf = Base.unsafe_wrap(Vector{UInt8}, buf_ptr, buf_len[1]; own=true)
  write(stream, buf)
end
# Deserialize a model from the given stream.
function deserializeGMM(stream::IO)::GMM
  buffer = read(stream)
  GMM(ccall((:DeserializeGMMPtr, gmm_probabilityLibrary), Ptr{Nothing}, (Ptr{UInt8}, UInt), Base.pointer(buffer), length(buffer)))
end
end # module

"""
    gmm_probability(input, input_model; [verbose])

This program calculates the probability that given points came from a given GMM
(that is, P(X | gmm)).  The GMM is specified with the `input_model` parameter,
and the points are specified with the `input` parameter.  The output
probabilities may be saved via the `output` output parameter.

So, for example, to calculate the probabilities of each point in `points` coming
from the pre-trained GMM `gmm`, while storing those probabilities in `probs`,
the following command could be used:

```julia
julia> using CSV
julia> points = CSV.read("points.csv")
julia> probs = gmm_probability(points, gmm)
```

# Arguments

 - `input::Array{Float64, 2}`: Input matrix to calculate probabilities
      of.
 - `input_model::GMM`: Input GMM to use as model.
 - `verbose::Bool`: Display informational messages and the full list of
      parameters and timers at the end of execution.  Default value `false`.

# Return values

 - `output::Array{Float64, 2}`: Matrix to store calculated probabilities
      in.

"""
function gmm_probability(input,
                         input_model::GMM;
                         verbose::Union{Bool, Missing} = missing,
                         points_are_rows::Bool = true)
  # Force the symbols to load.
  ccall((:loadSymbols, gmm_probabilityLibrary), Nothing, ());

  IORestoreSettings("GMM Probability Calculator")

  # Process each input argument before calling mlpackMain().
  IOSetParamMat("input", input, points_are_rows)
  gmm_probability_internal.IOSetParamGMM("input_model", convert(GMM, input_model))
  if verbose !== nothing && verbose === true
    IOEnableVerbose()
  else
    IODisableVerbose()
  end

  IOSetPassed("output")
  # Call the program.
  gmm_probability_mlpackMain()

  return IOGetParamMat("output", points_are_rows)
end


text:
export apopcount

# count the number of ones in the binary representation of a
# the function operates on 8 bytes at a time, so we truncate if necessary.
# From the libprimesieve comment:
# The 64-bit tree merging popcount algorithm is due to
# Cédric Lauradoux, it is described in his paper:
# http://perso.citi.insa-lyon.fr/claurado/ham/overview.pdf
# http://perso.citi.insa-lyon.fr/claurado/hamming.html
function apopcount(a::Array)
    sz = div(sizeof(a),8)
    ret = ccall((:primesieve_popcount,libccountname), UInt64, (Ptr{Void}, UInt64), a, sz)
    convert(Int,ret)
end


text:
module TMP

using ChainRulesCore
import ChainRulesCore: rrule

function f(x)
  x[1]+x[2]*x[3]
end

const A = [1 1 1; 0 1 1; 0 0 1]

function g(q)
  A\q
end

function h(q)
  x = g(q)
  y = f(x)
  y
end

function rrule(::typeof(f),x)
  function f_pullback(dy)
    NO_FIELDS, dy*[1,x[3],x[2]]
  end
  f(x), f_pullback
end

function rrule(::typeof(g),q)
  function g_pullback(dx)
    NO_FIELDS, transpose(A)\dx
  end
  g(q), g_pullback
end

q = [1,2,3]
x, g_pullback = rrule(g,q)
y, f_pullback = rrule(f,x)

dy = 1
_, dx = f_pullback(dy)
_, dq = g_pullback(dx)

@show q
@show x
@show y
@show dy
@show dx
@show dq

using ForwardDiff
dq3 = ForwardDiff.gradient(h,q)
@show dq3

using Zygote
dq2, = Zygote.gradient(h,q)
@show dq2

end # module

max_stars_repo_name: santiagobadia/GridapGeosciences

max_issues_repo_name: santiagobadia/GridapGeosciences

max_forks_repo_name: santiagobadia/GridapGeosciences


text:
# Copyright 2013 Peter Chronz
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

using Base.Test
require("../of-controller.jl")
using OpenFlow

info("Starting tests...")
tic()

# OfpHeader
# Test the constructor.
header = OfpHeader(OFPT_HELLO, uint16(8))
@test header.protoversion == 0x01
@test header.msgtype == OFPT_HELLO
@test header.msglen == uint16(8)
@test header.msgidx == uint32(0)
# Test the serialization.
headerbyts = b"\x01\x00\x00\x08\x00\x00\x00\x00"
@test bytes(header) == headerbyts
@test give_length(header) == 8

# OfpQueueGetConfigRequest
# Test the constructor.
header = OfpHeader(OFPT_QUEUE_GET_CONFIG_REQUEST)
qgcreq = OfpQueueGetConfigRequest(header, 0xAB)
@test qgcreq.port == 0xAB
# Test the serialization.
qgcreqbyts = b"\x01\x14\x00\x0C\x00\x00\x00\x00\x00\xAB\x00\x00"
@test bytes(qgcreq) == qgcreqbyts
# Test the deserialization.
qgcrdeser = OfpQueueGetConfigRequest(header, qgcreqbyts[9:end])
@test qgcrdeser.port == 0xAB
# Test length.
@test give_length(qgcreq) == 12
@test give_length(qgcrdeser) == 12

# OfpQueuePropHeader
# Test the constructor.
qpheader = OfpQueuePropHeader(uint16(OFPQT_MIN_RATE), uint16(8))
@test qpheader.property == OFPQT_MIN_RATE
@test qpheader.len == 8
# Test the serialization.
qphbyts = b"\x00\x01\x00\x08\x00\x00\x00\x00"
@test bytes(qpheader) == qphbyts
# Test the deserialization.
qpheaderdeser = OfpQueuePropHeader(qphbyts)
@test qpheaderdeser.property == OFPQT_MIN_RATE
@test qpheaderdeser.len == 8
# Test length.
@test give_length(qpheader) == 8
@test give_length(qpheaderdeser) == 8

# OfpQueuePropNone
# Test the constructor.
qpheader = OfpQueuePropHeader(uint16(OFPQT_NONE), uint16(8))
qpnone = OfpQueuePropNone(qpheader)
@test qpnone.header == qpheader
# Test the serialization.
qpnonebyts = b"\x00\x00\x00\x08\x00\x00\x00\x00"
@test bytes(qpnone) == qpnonebyts
# Test the deserialization.
qpnonedeser = OfpQueuePropNone(qpnonebyts)
@test qpnonedeser.header.property == qpheader.property
@test qpnonedeser.header.len == qpheader.len
# Test length.
@test give_length(qpnone) == 8
@test give_length(qpnonedeser) == 8

# OfpQueuePropMinRate
qpheader = OfpQueuePropHeader(uint16(OFPQT_MIN_RATE), uint16(16))
# Test the constructor.
qpminrate = OfpQueuePropMinRate(qpheader, uint16(10))
@test qpminrate.rate == 10
# Test the serialization.
qpminratebyts = b"\x00\x01\x00\x10\x00\x00\x00\x00\x00\x0A\x00\x00\x00\x00\x00\x00"
@test bytes(qpminrate) == qpminratebyts
# Test the deserialization.
qpminratedeser = OfpQueuePropMinRate(qpminratebyts)
@test qpminratedeser.rate == 10
# Test length.
@test give_length(qpminrate) == 16
@test give_length(qpminratedeser) == 16

# OfpPacketQueue
# Test the constructor.
pqueue = OfpPacketQueue(uint32(1), uint16(32), [qpminrate, qpnone])
@test pqueue.queue_id == 1
@test pqueue.len == 32
@test contains(pqueue.properties, qpminrate)
@test contains(pqueue.properties, qpnone)
# Test the serialization.
pqueuebyts = b"\x00\x00\x00\x01\x00\x20\x00\x00\x00\x01\x00\x10\x00\x00\x00\x00\x00\x0A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00"
@test bytes(pqueue) == pqueuebyts
# Test the deserialization.
pqueuedeser = OfpPacketQueue(pqueuebyts)
@test pqueuedeser.queue_id == 1
@test pqueuedeser.len == 32
@test pqueuedeser.properties[1].rate == qpminrate.rate
@test pqueuedeser.properties[2].header.property == qpnone.header.property
# Test length.
@test give_length(pqueue) == 32
@test give_length(pqueuedeser) == 32

# OfpQueueGetConfigReply
header = OfpHeader(OFPT_QUEUE_GET_CONFIG_REPLY)
# Test the constructor.
qgcreply = OfpQueueGetConfigReply(header, uint16(1), [pqueue, pqueue])
@test qgcreply.header == header
@test qgcreply.port == 1
@test qgcreply.queues == [pqueue, pqueue]
# Test the serialization.
qgcrbyts = b"\x01\x15\x00\x50\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"
pqueuebyts = b"\x00\x00\x00\x01\x00\x20\x00\x00\x00\x01\x00\x10\x00\x00\x00\x00\x00\x0A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00"
append!(qgcrbyts, pqueuebyts)
append!(qgcrbyts, pqueuebyts)
@test bytes(qgcreply) == qgcrbyts
# Test the deserialization.
qgcrdeser = OfpQueueGetConfigReply(header, qgcrbyts[9:end])
@test qgcrdeser.header.msgtype == header.msgtype
@test qgcrdeser.header.msglen == header.msglen
@test qgcrdeser.port == 1
@test qgcrdeser.queues[1].queue_id == 1
@test qgcrdeser.queues[1].len == 32
@test qgcrdeser.queues[2].queue_id == 1
@test qgcrdeser.queues[2].len == 32
# Test length.
@test give_length(qgcreply) == length(qgcrbyts)
@test give_length(qgcrdeser) == length(qgcrbyts)

# OfpError
header = OfpHeader(OFPT_ERROR)
# Test the constructor.
teststr = ascii("Something went really wrong over here! I think we should do
something about it, quickly.")
err = OfpError(header, uint16(OFPET_BAD_REQUEST), uint16(OFPBRC_BAD_VERSION),
    convert(Bytes, teststr))
convert(Bytes, teststr)
@test err.header == header
@test err.typ == OFPET_BAD_REQUEST
@test err.code == OFPBRC_BAD_VERSION
@test ascii(err.data) == teststr
# Test the serialization.
errbyts = bytes(header)
append!(errbyts, b"\x00\x01\x00\x00")
append!(errbyts, convert(Bytes, teststr))
@test bytes(err) == errbyts
# Test the deserialization.
errdeser = OfpError(header, errbyts[9:end])
@test errdeser.header == header
@test errdeser.typ == OFPET_BAD_REQUEST
@test errdeser.code == OFPBRC_BAD_VERSION
@test ascii(errdeser.data) == teststr
# Test length.
@test 100 == give_length(err)
@test 100 == give_length(errdeser)

# OfpPhyPort
# Test the constructor.
hw_addr = Array(Uint8, OFP_MAX_ETH_ALEN)
name = Array(Uint8, OFP_MAX_PORT_NAME_LEN)
phyport = OfpPhyPort(uint16(1), hw_addr, name, OFPC_FLOW_STATS, OFPPS_LINK_DOWN,
    OFPPF_10MB_HD, OFPPF_100MB_HD, OFPPF_1GB_HD, OFPPF_COPPER)
@test phyport.port_no == 1
@test phyport.hw_addr == hw_addr
@test phyport.name == name
@test phyport.config == OFPC_FLOW_STATS
@test phyport.state == OFPPS_LINK_DOWN
@test phyport.curr ==  OFPPF_10MB_HD
@test phyport.advertised == OFPPF_100MB_HD
@test phyport.supported == OFPPF_1GB_HD
@test phyport.peer == OFPPF_COPPER
# Test the serialization.
phyportbyts = b"\x00\x01"
append!(phyportbyts, hw_addr)
append!(phyportbyts, name)
append!(phyportbyts, b"\x00\x00\x00\x01")
append!(phyportbyts, b"\x00\x00\x00\x01")
append!(phyportbyts, b"\x00\x00\x00\x01")
append!(phyportbyts, b"\x00\x00\x00\x04")
append!(phyportbyts, b"\x00\x00\x00\x10")
append!(phyportbyts, b"\x00\x00\x00\x80")
@test bytes(phyport) == phyportbyts
# Test the deserialization.
phyportdeser = OfpPhyPort(phyportbyts)
@test phyportdeser.port_no == 1
@test phyportdeser.hw_addr == hw_addr
@test phyportdeser.name == name
@test phyportdeser.config == OFPC_FLOW_STATS
@test phyportdeser.state == OFPPS_LINK_DOWN
@test phyportdeser.curr ==  OFPPF_10MB_HD
@test phyportdeser.advertised == OFPPF_100MB_HD
@test phyportdeser.supported == OFPPF_1GB_HD
@test phyportdeser.peer == OFPPF_COPPER
# Test length.
@test length(phyportbyts) == give_length(phyport)
@test length(phyportbyts) == give_length(phyportdeser)

# OfpSwitchFeatures
# Test the constructor.
header = OfpHeader(OFPT_FEATURES_REPLY)
phyport = OfpPhyPort(uint16(1), hw_addr, name, OFPC_FLOW_STATS, OFPPS_LINK_DOWN,
    OFPPF_10MB_HD, OFPPF_100MB_HD, OFPPF_1GB_HD, OFPPF_COPPER)
swfeat = OfpSwitchFeatures(header, uint64(42), uint32(15), uint8(3),
    uint32(OFPC_FLOW_STATS), uint32(OFPAT_SET_VLAN_VID), [phyport, phyport])
@test swfeat.header == header
@test swfeat.datapath_id == 42
@test swfeat.n_buffers == 15
@test swfeat.n_tables == 3
@test swfeat.capabilities == OFPC_FLOW_STATS
@test swfeat.actions == OFPAT_SET_VLAN_VID
@test swfeat.ports == [phyport, phyport]
# Test the serialization.
swfeatbyts = bytes(header)
append!(swfeatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x2A")
append!(swfeatbyts, b"\x00\x00\x00\x0F")
append!(swfeatbyts, b"\x03")
append!(swfeatbyts, b"\x00\x00\x00")
append!(swfeatbyts, b"\x00\x00\x00\x01")
append!(swfeatbyts, b"\x00\x00\x00\x01")
append!(swfeatbyts, bytes(phyport))
append!(swfeatbyts, bytes(phyport))
@test bytes(swfeat) == swfeatbyts
# Test the deserialization.
swfeatdeser = OfpSwitchFeatures(header, swfeatbyts[9:end])
@test swfeatdeser.header == header
@test swfeatdeser.datapath_id == 42
@test swfeatdeser.n_buffers == 15
@test swfeatdeser.n_tables == 3
@test swfeatdeser.capabilities == OFPC_FLOW_STATS
@test swfeatdeser.actions == OFPAT_SET_VLAN_VID
@test swfeatdeser.ports[1].port_no == 1
# Test length.
@test give_length(swfeat) == length(swfeatbyts)
@test give_length(swfeatdeser) == length(swfeatbyts)

# OfpSwitchConfig
header = OfpHeader(OFPT_GET_CONFIG_REPLY)
# Test the constructor.
swconf = OfpSwitchConfig(header, OFPC_FLOW_STATS, uint16(64))
@test swconf.header == header
@test swconf.flags == OFPC_FLOW_STATS
@test swconf.miss_send_len == 64
# Test the serialization.
swconfbyts = bytes(header)
append!(swconfbyts, b"\x00\x01")
append!(swconfbyts, b"\x00\x40")
@test bytes(swconf) == swconfbyts
# Test the deserialization.
swconfdeser = OfpSwitchConfig(header, swconfbyts[9:end])
@test swconfdeser.header == header
@test swconfdeser.flags == OFPC_FLOW_STATS
@test swconfdeser.miss_send_len == 64
# Test length.
@test give_length(swconf) == 12
@test give_length(swconfdeser) == 12

# OfpMatch
# Test the constructor.
dl_src = Array(Uint8, OFP_MAX_ETH_ALEN)
dl_dst = Array(Uint8, OFP_MAX_ETH_ALEN)
match = OfpMatch(uint32(0), uint16(13), dl_src, dl_dst, uint16(13), uint16(13),
    uint16(3), uint8(1), uint8(3), uint32(13), uint32(14), uint16(3), uint16(4))
@test match.wildcards == 0
@test match.in_port == 13
@test match.dl_src == dl_src
@test match.dl_dst == dl_dst
@test match.dl_vlan == 13
@test match.dl_vlan_pcp == 13
@test match.dl_type == 3
@test match.nw_tos == 1
@test match.nw_proto == 3
@test match.nw_src == 13
@test match.nw_dst == 14
@test match.tp_src == 3
@test match.tp_dst == 4
# Test the serialization.
matchbyts = b"\x00\x00\x00\x00"
append!(matchbyts, b"\x00\x0D")
append!(matchbyts, dl_src)
append!(matchbyts, dl_dst)
append!(matchbyts, b"\x00\x0D")
append!(matchbyts, b"\x0D")
append!(matchbyts, b"\x00")
append!(matchbyts, b"\x00\x03")
append!(matchbyts, b"\x01")
append!(matchbyts, b"\x03")
append!(matchbyts, b"\x00\x00")
append!(matchbyts, b"\x00\x00\x00\x0D")
append!(matchbyts, b"\x00\x00\x00\x0E")
append!(matchbyts, b"\x00\x03")
append!(matchbyts, b"\x00\x04")
@test bytes(match) == matchbyts
# Test the deserialization.
matchdeser = OfpMatch(matchbyts)
@test matchdeser.wildcards == 0
@test matchdeser.in_port == 13
@test matchdeser.dl_src == dl_src
@test matchdeser.dl_dst == dl_dst
@test matchdeser.dl_vlan == 13
@test matchdeser.dl_vlan_pcp == 13
@test matchdeser.dl_type == 3
@test matchdeser.nw_tos == 1
@test matchdeser.nw_proto == 3
@test matchdeser.nw_src == 13
@test matchdeser.nw_dst == 14
@test matchdeser.tp_src == 3
@test matchdeser.tp_dst == 4
# Test length.
@test give_length(match) == length(matchbyts)
@test give_length(matchdeser) == length(matchbyts)

# OfpActionEmpty
# Test the constructor.
actempty = OfpActionEmpty(OFPAT_STRIP_VLAN, uint16(4))
@test actempty.typ == OFPAT_STRIP_VLAN
@test actempty.len == 4
# Test the serialization.
actemptybyts = b"\x00\x03\x00\x04"
@test bytes(actempty) == actemptybyts
# Test the deserialization.
actemptydeser = OfpActionEmpty(actemptybyts)
@test actemptydeser.typ == OFPAT_STRIP_VLAN
@test actemptydeser.len == 4
# Test length.
@test give_length(actempty) == 4
@test give_length(actemptydeser) == 4

# OfpActionOutput
# Test the constructor.
actout = OfpActionOutput(OFPAT_OUTPUT, uint16(8), uint16(3), uint16(64))
@test actout.typ == OFPAT_OUTPUT
@test actout.len == 8
@test actout.port == 3
@test actout.max_len == 64
# Test the serialization.
actoutbyts = b"\x00\x00\x00\x08\x00\x03\x00\x40"
@test bytes(actout) == actoutbyts
# Test the deserialization.
actoutdeser = OfpActionOutput(actoutbyts)
@test actoutdeser.typ == OFPAT_OUTPUT
@test actoutdeser.len == 8
@test actoutdeser.port == 3
@test actoutdeser.max_len == 64
# Test length.
@test give_length(actout) == 8
@test give_length(actoutdeser) == 8

# OfpActionEnqueue
# Test the constructor.
actenq = OfpActionEnqueue(OFPAT_ENQUEUE, uint16(16), uint16(3), uint32(16))
@test actenq.typ == OFPAT_ENQUEUE
@test actenq.len == 16
@test actenq.port == 3
@test actenq.queue_id == 16
# Test the serialization.
actenqbyts = b"\x00\x0b\x00\x10\x00\x03"
append!(actenqbyts, b"\x00\x00\x00\x00\x00\x00")
append!(actenqbyts, b"\x00\x00\x00\x10")
@test bytes(actenq) == actenqbyts
# Test the deserialization.
actenqdeser = OfpActionEnqueue(actenqbyts)
@test actenqdeser.typ == OFPAT_ENQUEUE
@test actenqdeser.len == 16
@test actenqdeser.port == 3
@test actenqdeser.queue_id == 16
# Test length.
@test give_length(actenq) == 16
@test give_length(actenqdeser) == 16

# OfpActionVlanVid
# Test the constructor.
actvid = OfpActionVlanVid(OFPAT_SET_VLAN_VID, uint16(8), uint16(13))
@test actvid.typ == OFPAT_SET_VLAN_VID
@test actvid.len == 8
@test actvid.vlan_vid == 13
# Test the serialization.
actvidbyts = b"\x00\x01\x00\x08\x00\x0D\x00\x00"
@test bytes(actvid) == actvidbyts
# Test the deserialization.
actviddeser = OfpActionVlanVid(actvidbyts)
@test actviddeser.typ == OFPAT_SET_VLAN_VID
@test actviddeser.len == 8
@test actviddeser.vlan_vid == 13
# Test length.
@test give_length(actvid) == 8
@test give_length(actviddeser) == 8

# OfpActionVlanPcp
# Test the constructor.
actvpcp = OfpActionVlanPcp(OFPAT_SET_VLAN_PCP, uint16(8), uint8(3))
@test actvpcp.typ == OFPAT_SET_VLAN_PCP
@test actvpcp.len == 8
@test actvpcp.vlan_pcp == 3
# Test the serialization.
actvpcpbyts = b"\x00\x02\x00\x08\x03\x00\x00\x00"
@test bytes(actvpcp) == actvpcpbyts
# Test the deserialization.
actvpcpdeser = OfpActionVlanPcp(actvpcpbyts)
@test actvpcpdeser.typ == OFPAT_SET_VLAN_PCP
@test actvpcpdeser.len == 8
@test actvpcpdeser.vlan_pcp == 3
# Test length.
@test give_length(actvpcp) == 8
@test give_length(actvpcpdeser) == 8

# OfpActionDlAddress
# Test the constructor.
dl_addr = Array(Uint8, OFP_MAX_ETH_ALEN)
actdl = OfpActionDlAddress(OFPAT_SET_DL_SRC, uint16(16), dl_addr)
@test actdl.typ == OFPAT_SET_DL_SRC
@test actdl.len == 16
@test actdl.dl_addr == dl_addr
# Test the serialization.
actdlbyts = b"\x00\x04\x00\x10"
append!(actdlbyts, dl_addr)
append!(actdlbyts, b"\x00\x00\x00\x00\x00\x00")
@test bytes(actdl) == actdlbyts
# Test the deserialization.
actdldeser = OfpActionDlAddress(actdlbyts)
@test actdldeser.typ == OFPAT_SET_DL_SRC
@test actdldeser.len == 16
@test actdldeser.dl_addr == dl_addr
# Test length.
@test give_length(actdl) == 16
@test give_length(actdldeser) == 16

# OfpActionNwAddress
# Test the constructor.
actnw = OfpActionNwAddress(OFPAT_SET_NW_SRC, uint16(8), uint32(13))
@test actnw.typ == OFPAT_SET_NW_SRC
@test actnw.len == 8
@test actnw.nw_addr == 13
# Test the serialization.
actnwbyts = b"\x00\x06\x00\x08\x00\x00\x00\x0D"
@test bytes(actnw) == actnwbyts
# Test the deserialization.
actnwdeser = OfpActionNwAddress(actnwbyts)
@test actnwdeser.typ == OFPAT_SET_NW_SRC
@test actnwdeser.len == 8
@test actnwdeser.nw_addr == 13
# Test length.
@test give_length(actnw) == 8
@test give_length(actnwdeser) == 8

# OfpActionNwTos
# Test the constructor.
actnwtos = OfpActionNwTos(OFPAT_SET_NW_TOS, uint16(8), uint8(3))
@test actnwtos.typ == OFPAT_SET_NW_TOS
@test actnwtos.len == 8
@test actnwtos.nw_tos == 3
# Test the serialization.
actnwtosbyts = b"\x00\x08\x00\x08\x03\x00\x00\x00"
@test bytes(actnwtos) == actnwtosbyts
# Test the deserialization.
actnwtosdeser = OfpActionNwTos(actnwtosbyts)
@test actnwtosdeser.typ == OFPAT_SET_NW_TOS
@test actnwtosdeser.len == 8
@test actnwtosdeser.nw_tos == 3
# Test length.
@test give_length(actnwtos) == 8
@test give_length(actnwtosdeser) == 8

# OfpActionTpPort
# Test the constructor.
acttp = OfpActionTpPort(OFPAT_SET_TP_SRC, uint16(8), uint16(13))
@test acttp.typ == OFPAT_SET_TP_SRC
@test acttp.len == 8
@test acttp.tp_port == 13
# Test the serialization.
acttpbyts = b"\x00\x09\x00\x08\x00\x0D\x00\x00"
@test bytes(acttp) == acttpbyts
# Test the deserialization.
acttpdeser = OfpActionTpPort(acttpbyts)
@test acttpdeser.typ == OFPAT_SET_TP_SRC
@test acttpdeser.len == 8
@test acttpdeser.tp_port == 13
# Test length.
@test give_length(acttp) == 8
@test give_length(acttpdeser) == 8

# OfpActionVendor
# Test the constructor.
actven = OfpActionVendor(OFPAT_VENDOR, uint16(24), uint32(63), ones(Uint8, 16))
@test actven.typ == OFPAT_VENDOR
@test actven.len == 24
@test actven.vendor == 63
@test actven.body == ones(Uint8, 16)
# Test the serialization.
actvenbyts = b"\xff\xff\x00\x18\x00\x00\x00\x3f"
append!(actvenbyts, b"\x01\x01\x01\x01\x01\x01\x01\x01")
append!(actvenbyts, b"\x01\x01\x01\x01\x01\x01\x01\x01")
@test bytes(actven) == actvenbyts
# Test the deserialization.
actvendeser = OfpActionVendor(actvenbyts)
@test actvendeser.typ == OFPAT_VENDOR
@test actvendeser.len == 24
@test actvendeser.vendor == 63
@test actvendeser.body == ones(Uint8, 16)
# Test length.
@test give_length(actven) == 24
@test give_length(actvendeser) == 24

# OfpPacketIn
header = OfpHeader(OFPT_PACKET_IN)
# Test the constructor.
packin = OfpPacketIn(header, uint32(13), uint16(42), uint16(2), OFPR_NO_MATCH,
    zeros(Uint8, 16))
@test packin.header ==  header
@test packin.buffer_id == 13
@test packin.total_len == 42
@test packin.in_port == 2
@test packin.reason == OFPR_NO_MATCH
@test packin.data == zeros(Uint8, 16)
# Test the serialization.
packinbyts = bytes(header)
append!(packinbyts, b"\x00\x00\x00\x0D")
append!(packinbyts, b"\x00\x2A")
append!(packinbyts, b"\x00\x02")
append!(packinbyts, b"\x00")
append!(packinbyts, b"\x00")
append!(packinbyts, b"\x00\x00\x00\x00\x00\x00\x00\x00")
append!(packinbyts, b"\x00\x00\x00\x00\x00\x00\x00\x00")
@test bytes(packin) == packinbyts
# Test the deserialization.
packindeser = OfpPacketIn(header, packinbyts[9:end])
@test packindeser.header ==  header
@test packindeser.buffer_id == 13
@test packindeser.total_len == 42
@test packindeser.in_port == 2
@test packindeser.reason == OFPR_NO_MATCH
@test packindeser.data == zeros(Uint8, 16)
# Test length.
@test give_length(packin) == length(packinbyts)
@test give_length(packindeser) == length(packinbyts)

# OfpPortStatus
header = OfpHeader(OFPT_PORT_STATUS)
# Test the constructor.
hw_addr = Array(Uint8, OFP_MAX_ETH_ALEN)
name = Array(Uint8, OFP_MAX_PORT_NAME_LEN)
phyport = OfpPhyPort(uint16(1), hw_addr, name, OFPC_FLOW_STATS, OFPPS_LINK_DOWN,
    OFPPF_10MB_HD, OFPPF_100MB_HD, OFPPF_1GB_HD, OFPPF_COPPER)
portstatus = OfpPortStatus(header, OFPPR_ADD, phyport)
@test portstatus.header == header
@test portstatus.reason == OFPPR_ADD
@test portstatus.desc == phyport
# Test the serialization.
portstatusbyts = bytes(header)
append!(portstatusbyts, b"\x00")
append!(portstatusbyts, b"\x00\x00\x00\x00\x00\x00\x00")
append!(portstatusbyts, bytes(phyport))
@test bytes(portstatus) == portstatusbyts
# Test the deserialization.
portstatusdeser = OfpPortStatus(header, portstatusbyts[9:end])
@test portstatusdeser.header == header
@test portstatusdeser.reason == OFPPR_ADD
@test portstatusdeser.desc.port_no == 1
@test portstatusdeser.desc.hw_addr == hw_addr
@test portstatusdeser.desc.name == name
@test portstatusdeser.desc.config == OFPC_FLOW_STATS
@test portstatusdeser.desc.state == OFPPS_LINK_DOWN
# Test length.
@test give_length(portstatus) == length(portstatusbyts)
@test give_length(portstatusdeser) == length(portstatusbyts)

# OfpFlowMod
header = OfpHeader(OFPT_FLOW_MOD)
dl_src = Array(Uint8, OFP_MAX_ETH_ALEN)
dl_dst = Array(Uint8, OFP_MAX_ETH_ALEN)
match = OfpMatch(uint32(0), uint16(13), dl_src, dl_dst, uint16(13), uint16(13),
    uint16(3), uint8(1), uint8(3), uint32(13), uint32(14), uint16(3), uint16(4))
actout = OfpActionOutput(OFPAT_OUTPUT, uint16(8), uint16(3), uint16(64))
actven = OfpActionVendor(OFPAT_VENDOR, uint16(24), uint32(63), ones(Uint8, 16))
acttp = OfpActionTpPort(OFPAT_SET_TP_SRC, uint16(8), uint16(13))
# Test the constructor.
flomod = OfpFlowMod(header, match, uint64(13), OFPFC_DELETE, uint16(5),
    uint16(7), uint16(1), uint32(15), uint16(2), OFPFF_SEND_FLOW_REM, [actout,
    actven, acttp])
@test flomod.header == header
@test flomod.match == match
@test flomod.cookie == 13
@test flomod.command == OFPFC_DELETE
@test flomod.idle_timeout == 5
@test flomod.hard_timeout == 7
@test flomod.priority == 1
@test flomod.buffer_id == 15
@test flomod.out_port == 2
@test flomod.flags == OFPFF_SEND_FLOW_REM
@test flomod.actions == [actout, actven, acttp]
# Test the serialization.
flomodbyts = bytes(header)
append!(flomodbyts, bytes(match))
append!(flomodbyts, b"\x00\x00\x00\x00\x00\x00\x00\x0D")
append!(flomodbyts, b"\x00\x03")
append!(flomodbyts, b"\x00\x05")
append!(flomodbyts, b"\x00\x07")
append!(flomodbyts, b"\x00\x01")
append!(flomodbyts, b"\x00\x00\x00\x0F")
append!(flomodbyts, b"\x00\x02")
append!(flomodbyts, b"\x00\x01")
append!(flomodbyts, reduce((els, el)->([els, bytes(el)]), Uint8[], [actout,
    actven,acttp]))
@test bytes(flomod) == flomodbyts
# Test the deserialization.
flomoddeser = OfpFlowMod(header, flomodbyts[9:end])
@test flomoddeser.header == header
@test flomoddeser.match.wildcards == 0
@test flomoddeser.match.in_port == 13
@test flomoddeser.match.dl_src == dl_src
@test flomoddeser.match.dl_dst == dl_dst
@test flomoddeser.match.dl_vlan == 13
@test flomoddeser.match.dl_vlan_pcp == 13
@test flomoddeser.match.dl_type == 3
@test flomoddeser.match.nw_tos == 1
@test flomoddeser.match.nw_proto == 3
@test flomoddeser.match.nw_src == 13
@test flomoddeser.match.nw_dst == 14
@test flomoddeser.match.tp_src == 3
@test flomoddeser.match.tp_dst == 4
@test flomoddeser.cookie == 13
@test flomoddeser.command == OFPFC_DELETE
@test flomoddeser.idle_timeout == 5
@test flomoddeser.hard_timeout == 7
@test flomoddeser.priority == 1
@test flomoddeser.buffer_id == 15
@test flomoddeser.out_port == 2
@test flomoddeser.flags == OFPFF_SEND_FLOW_REM
@test bytes(flomoddeser.actions[1]) == bytes(actout)
@test bytes(flomoddeser.actions[2]) == bytes(actven)
@test bytes(flomoddeser.actions[3]) == bytes(acttp)
# Test length.
@test give_length(flomod) == length(flomodbyts)
@test give_length(flomoddeser) == length(flomodbyts)

# OfpPortMod
header = OfpHeader(OFPT_PORT_MOD)
# Test the constructor.
hw_addr = Array(Uint8, OFP_MAX_ETH_ALEN)
pormod = OfpPortMod(header, uint16(11), hw_addr, OFPPC_PORT_DOWN, OFPPC_NO_STP,
    uint32(0))
@test pormod.header == header
@test pormod.port_no == 11
@test pormod.hw_addr == hw_addr
@test pormod.config == OFPPC_PORT_DOWN
@test pormod.mask == OFPPC_NO_STP
@test pormod.advertise == 0
# Test the serialization.
pormodbyts = bytes(header)
append!(pormodbyts, b"\x00\x0B")
append!(pormodbyts, hw_addr)
append!(pormodbyts, b"\x00\x00\x00\x01")
append!(pormodbyts, b"\x00\x00\x00\x02")
append!(pormodbyts, b"\x00\x00\x00\x00")
append!(pormodbyts, b"\x00\x00\x00\x00")
@test bytes(pormod) == pormodbyts
# Test the deserialization.
pormoddeser = OfpPortMod(header, pormodbyts[9:end])
@test pormoddeser.header == header
@test pormoddeser.port_no == 11
@test pormoddeser.hw_addr == hw_addr
@test pormoddeser.config == OFPPC_PORT_DOWN
@test pormoddeser.mask == OFPPC_NO_STP
@test pormoddeser.advertise == 0
# Test length.
@test give_length(pormod) == length(pormodbyts)
@test give_length(pormoddeser) == length(pormodbyts)

# OfpEmptyMessage
header = OfpHeader(OFPT_STATS_REPLY)
# Test the constructor.
empty = OfpEmptyMessage(header)
@test empty.header == header
# Test the serialization.
emptybyts = bytes(header)
@test bytes(empty) == emptybyts
# Test the deserialization.
emptydeser = OfpEmptyMessage(header, Uint8[])
@test emptydeser.header == header
# Test length.
@test give_length(empty) == 8
@test give_length(emptydeser) == 8

# OfpDescStats
# Test the constructor.
mfr_desc = Array(Uint8, DESC_STR_LEN)
hw_desc = Array(Uint8, DESC_STR_LEN)
sw_desc = Array(Uint8, DESC_STR_LEN)
serial_num = Array(Uint8, SERIAL_NUM_LEN)
dp_desc = Array(Uint8, DESC_STR_LEN)
descstats = OfpDescStats(mfr_desc, hw_desc, sw_desc, serial_num, dp_desc)
@test descstats.mfr_desc == mfr_desc
@test descstats.hw_desc == hw_desc
@test descstats.sw_desc == sw_desc
@test descstats.serial_num == serial_num
@test descstats.dp_desc == dp_desc
# Test the serialization.
descstatsbyts = copy(mfr_desc)
append!(descstatsbyts, hw_desc)
append!(descstatsbyts, sw_desc)
append!(descstatsbyts, serial_num)
append!(descstatsbyts, dp_desc)
@test bytes(descstats) == descstatsbyts
# Test the deserialization.
descstatsdeser = OfpDescStats(descstatsbyts)
@test descstatsdeser.mfr_desc == mfr_desc
@test descstatsdeser.hw_desc == hw_desc
@test descstatsdeser.sw_desc == sw_desc
@test descstatsdeser.serial_num == serial_num
@test descstatsdeser.dp_desc == dp_desc
# Test length.
@test give_length(descstats) == length(descstatsbyts)
@test give_length(descstatsdeser) == length(descstatsbyts)

# OfpFlowStatsRequest
# Test the constructor.
dl_src = Array(Uint8, OFP_MAX_ETH_ALEN)
dl_dst = Array(Uint8, OFP_MAX_ETH_ALEN)
match = OfpMatch(uint32(0), uint16(13), dl_src, dl_dst, uint16(13), uint16(13),
    uint16(3), uint8(1), uint8(3), uint32(13), uint32(14), uint16(3), uint16(4))
flostare = OfpFlowStatsRequest(match, 0xff, OFPP_NONE)
@test flostare.match == match
@test flostare.table_id == 0xff
@test flostare.out_port == OFPP_NONE
# Test the serialization.
flostarebyts = bytes(match)
append!(flostarebyts, [0xff])
append!(flostarebyts, b"\x00")
append!(flostarebyts, b"\xff\xff")
@test bytes(flostare) == flostarebyts
# Test the deserialization.
flostaredeser = OfpFlowStatsRequest(flostarebyts)
@test flostaredeser.match.wildcards == 0
@test flostaredeser.match.in_port == 13
@test flostaredeser.match.dl_src == dl_src
@test flostaredeser.match.dl_dst == dl_dst
@test flostaredeser.match.dl_vlan == 13
@test flostaredeser.match.dl_vlan_pcp == 13
@test flostaredeser.match.dl_type == 3
@test flostaredeser.match.nw_tos == 1
@test flostaredeser.match.nw_proto == 3
@test flostaredeser.match.nw_src == 13
@test flostaredeser.match.nw_dst == 14
@test flostaredeser.match.tp_src == 3
@test flostaredeser.match.tp_dst == 4
@test flostaredeser.table_id == 0xff
@test flostaredeser.out_port == OFPP_NONE
# Test length.
@test give_length(flostare) == length(flostarebyts)
@test give_length(flostaredeser) == length(flostarebyts)

# OfpFlowStats
dl_src = Array(Uint8, OFP_MAX_ETH_ALEN)
dl_dst = Array(Uint8, OFP_MAX_ETH_ALEN)
match = OfpMatch(uint32(0), uint16(13), dl_src, dl_dst, uint16(13), uint16(13),
    uint16(3), uint8(1), uint8(3), uint32(13), uint32(14), uint16(3), uint16(4))
actout = OfpActionOutput(OFPAT_OUTPUT, uint16(8), uint16(3), uint16(64))
actven = OfpActionVendor(OFPAT_VENDOR, uint16(24), uint32(63), ones(Uint8, 16))
acttp = OfpActionTpPort(OFPAT_SET_TP_SRC, uint16(8), uint16(13))
# Test the constructor.
flowstats = OfpFlowStats(0x0080, 0x01, match, uint32(10), uint32(50), uint16(3),
    uint16(15), uint16(17), uint64(42), uint64(15), uint64(78), [actout, actven,
    acttp])
@test flowstats.length == 48 + give_length(match) + reduce((l, x)->l +
    give_length(x), 0, [actout, actven, acttp])
@test flowstats.table_id == 1
@test flowstats.match == match
@test flowstats.duration_sec == 10
@test flowstats.duration_nsec == 50
@test flowstats.priority == 3
@test flowstats.idle_timeout == 15
@test flowstats.hard_timeout == 17
@test flowstats.cookie == 42
@test flowstats.packet_count == 15
@test flowstats.byte_count == 78
@test flowstats.actions[1] == actout
@test flowstats.actions[2] == actven
@test flowstats.actions[3] == acttp
# Test the serialization.
flowstatsbyts = b"\x00\x80"
append!(flowstatsbyts, b"\x01\x00")
append!(flowstatsbyts, bytes(match))
append!(flowstatsbyts, b"\x00\x00\x00\x0A")
append!(flowstatsbyts, b"\x00\x00\x00\x32")
append!(flowstatsbyts, b"\x00\x03")
append!(flowstatsbyts, b"\x00\x0F")
append!(flowstatsbyts, b"\x00\x11")
append!(flowstatsbyts, b"\x00\x00\x00\x00\x00\x00")
append!(flowstatsbyts, b"\x00\x00\x00\x00\x00\x00\x00\x2A")
append!(flowstatsbyts, b"\x00\x00\x00\x00\x00\x00\x00\x0F")
append!(flowstatsbyts, b"\x00\x00\x00\x00\x00\x00\x00\x4E")
reduce((bs, act)->append!(bs, bytes(act)), flowstatsbyts, [actout, actven, acttp])
@test bytes(flowstats) == flowstatsbyts
# Test the deserialization.
flowstatsdeser = OfpFlowStats(flowstatsbyts)
@test flowstatsdeser.length == 48 + give_length(match) + reduce((l, x)->l +
    give_length(x), 0, [actout, actven, acttp])
@test flowstatsdeser.table_id == 1
@test flowstatsdeser.match.wildcards == 0
@test flowstatsdeser.match.in_port == 13
@test flowstatsdeser.match.dl_src == dl_src
@test flowstatsdeser.match.dl_dst == dl_dst
@test flowstatsdeser.match.dl_vlan == 13
@test flowstatsdeser.match.dl_vlan_pcp == 13
@test flowstatsdeser.match.dl_type == 3
@test flowstatsdeser.match.nw_tos == 1
@test flowstatsdeser.match.nw_proto == 3
@test flowstatsdeser.match.nw_src == 13
@test flowstatsdeser.match.nw_dst == 14
@test flowstatsdeser.match.tp_src == 3
@test flowstatsdeser.match.tp_dst == 4
@test flowstatsdeser.duration_sec == 10
@test flowstatsdeser.duration_nsec == 50
@test flowstatsdeser.priority == 3
@test flowstatsdeser.idle_timeout == 15
@test flowstatsdeser.hard_timeout == 17
@test flowstatsdeser.cookie == 42
@test flowstatsdeser.packet_count == 15
@test flowstatsdeser.byte_count == 78
@test isa(flowstatsdeser.actions[1], OfpActionOutput)
@test isa(flowstatsdeser.actions[2], OfpActionVendor)
@test isa(flowstatsdeser.actions[3], OfpActionTpPort)
# Test length.
@test give_length(flowstats) == length(flowstatsbyts)
@test give_length(flowstatsdeser) == length(flowstatsbyts)

# OfpAggregateStatsRequest
dl_src = Array(Uint8, OFP_MAX_ETH_ALEN)
dl_dst = Array(Uint8, OFP_MAX_ETH_ALEN)
match = OfpMatch(uint32(0), uint16(13), dl_src, dl_dst, uint16(13), uint16(13),
    uint16(3), uint8(1), uint8(3), uint32(13), uint32(14), uint16(3), uint16(4))
# Test the constructor.
aggstatreq = OfpAggregateStatsRequest(match, 0xff, OFPP_NONE)
@test aggstatreq.match == match
@test aggstatreq.table_id == 0xff
@test aggstatreq.out_port == OFPP_NONE
# Test the serialization.
aggstatreqbyts = bytes(match)
append!(aggstatreqbyts, b"\xff")
append!(aggstatreqbyts, b"\x00")
append!(aggstatreqbyts, b"\xff\xff")
@test bytes(aggstatreq) == aggstatreqbyts
# Test the deserialization.
aggstatreqdeser = OfpAggregateStatsRequest(aggstatreqbyts)
@test aggstatreqdeser.match.wildcards == 0
@test aggstatreqdeser.match.in_port == 13
@test aggstatreqdeser.match.dl_src == dl_src
@test aggstatreqdeser.match.dl_dst == dl_dst
@test aggstatreqdeser.match.dl_vlan == 13
@test aggstatreqdeser.match.dl_vlan_pcp == 13
@test aggstatreqdeser.match.dl_type == 3
@test aggstatreqdeser.match.nw_tos == 1
@test aggstatreqdeser.match.nw_proto == 3
@test aggstatreqdeser.match.nw_src == 13
@test aggstatreqdeser.match.nw_dst == 14
@test aggstatreqdeser.match.tp_src == 3
@test aggstatreqdeser.match.tp_dst == 4
@test aggstatreq.table_id == 0xff
@test aggstatreq.out_port == OFPP_NONE
# Test length.
@test give_length(aggstatreq) == length(aggstatreqbyts)
@test give_length(aggstatreqdeser) == length(aggstatreqbyts)

# OfpAggregateStatsReply
# Test the constructor.
aggstatrep = OfpAggregateStatsReply(uint64(3), uint64(14), uint32(13))
@test aggstatrep.packet_count == 3
@test aggstatrep.byte_count == 14
@test aggstatrep.flow_count == 13
# Test the serialization.
aggstatrepbyts = b"\x00\x00\x00\x00\x00\x00\x00\x03"
append!(aggstatrepbyts, b"\x00\x00\x00\x00\x00\x00\x00\x0E")
append!(aggstatrepbyts, b"\x00\x00\x00\x0D")
append!(aggstatrepbyts, b"\x00\x00\x00\x00")
@test bytes(aggstatrep) == aggstatrepbyts
# Test the deserialization.
aggstatrepdeser = OfpAggregateStatsReply(aggstatrepbyts)
@test aggstatrepdeser.packet_count == 3
@test aggstatrepdeser.byte_count == 14
@test aggstatrepdeser.flow_count == 13
# Test length.
@test give_length(aggstatrep) == length(aggstatrepbyts)
@test give_length(aggstatrepdeser) == length(aggstatrepbyts)

# OfpTableStats
# Test the constructor.
name = Array(Uint8, OFP_MAX_TABLE_NAME_LEN)
tablstat = OfpTableStats(0x01, name, OFPFW_IN_PORT, uint32(3),
    uint32(15), uint64(38), uint64(98))
@test tablstat.table_id == 1
@test tablstat.name == name
@test tablstat.wildcards == OFPFW_IN_PORT
@test tablstat.max_entries == 3
@test tablstat.active_count == 15
@test tablstat.lookup_count == 38
@test tablstat.matched_count == 98
# Test the serialization.
tablstatbyts = b"\x01\x00\x00\x00"
append!(tablstatbyts, name)
append!(tablstatbyts, b"\x00\x00\x00\x01")
append!(tablstatbyts, b"\x00\x00\x00\x03")
append!(tablstatbyts, b"\x00\x00\x00\x0F")
append!(tablstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x26")
append!(tablstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x62")
@test bytes(tablstat) == tablstatbyts
# Test the deserialization.
tablstatdeser = OfpTableStats(tablstatbyts)
@test tablstatdeser.table_id == 1
@test tablstatdeser.name == name
@test tablstatdeser.wildcards == OFPFW_IN_PORT
@test tablstatdeser.max_entries == 3
@test tablstatdeser.active_count == 15
@test tablstatdeser.lookup_count == 38
@test tablstatdeser.matched_count == 98
# Test length.
@test give_length(tablstat) == length(tablstatbyts)
@test give_length(tablstatdeser) == length(tablstatbyts)

# OfpPortStatsRequest
# Test the constructor.
portstatreq = OfpPortStatsRequest(uint16(13))
@test portstatreq.port_no == 13
# Test the serialization.
portstatreqbyts = b"\x00\x0D\x00\x00\x00\x00\x00\x00"
@test bytes(portstatreq) == portstatreqbyts
# Test the deserialization.
portstatreqdeser = OfpPortStatsRequest(portstatreqbyts)
@test portstatreqdeser.port_no == 13
# Test length.
@test give_length(portstatreq) == length(portstatreqbyts)
@test give_length(portstatreqdeser) == length(portstatreqbyts)

# OfpPortStats
# Test the constructor.
portstat = OfpPortStats(0x0003, uint64(1), uint64(2), uint64(3), uint64(4),
    uint64(5), uint64(6), uint64(7), uint64(8), uint64(9), uint64(10),
    uint64(11), uint64(12))
@test portstat.port_no == 3
@test portstat.rx_packets == 1
@test portstat.tx_packets == 2
@test portstat.rx_bytes == 3
@test portstat.tx_bytes == 4
@test portstat.rx_dropped == 5
@test portstat.tx_dropped == 6
@test portstat.rx_errors == 7
@test portstat.tx_errors == 8
@test portstat.rx_frame_err == 9
@test portstat.rx_over_err == 10
@test portstat.rx_crc_err == 11
@test portstat.collisions == 12
# Test the serialization.
portstatbyts = b"\x00\x03\x00\x00\x00\x00\x00\x00"
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x01")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x02")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x03")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x04")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x05")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x06")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x07")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x08")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x09")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x0A")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x0B")
append!(portstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x0C")
@test bytes(portstat) == portstatbyts
# Test the deserialization.
portstatdeser = OfpPortStats(portstatbyts)
@test portstatdeser.port_no == 3
@test portstatdeser.rx_packets == 1
@test portstatdeser.tx_packets == 2
@test portstatdeser.rx_bytes == 3
@test portstatdeser.tx_bytes == 4
@test portstatdeser.rx_dropped == 5
@test portstatdeser.tx_dropped == 6
@test portstatdeser.rx_errors == 7
@test portstatdeser.tx_errors == 8
@test portstatdeser.rx_frame_err == 9
@test portstatdeser.rx_over_err == 10
@test portstatdeser.rx_crc_err == 11
@test portstatdeser.collisions == 12
# Test length.
@test give_length(portstat) == length(portstatbyts)
@test give_length(portstatdeser) == length(portstatbyts)

# OfpQueueStatsRequest
# Test the constructor.
qstatreq = OfpQueueStatsRequest(uint16(1), uint32(2))
@test qstatreq.port_no == 1
@test qstatreq.queue_id == 2
# Test the serialization.
qstatreqbyts = b"\x00\x01"
append!(qstatreqbyts, b"\x00\x00")
append!(qstatreqbyts, b"\x00\x00\x00\x02")
@test bytes(qstatreq) == qstatreqbyts
# Test the deserialization.
qstatreqdeser = OfpQueueStatsRequest(qstatreqbyts)
@test qstatreqdeser.port_no == 1
@test qstatreqdeser.queue_id == 2
# Test length.
@test give_length(qstatreq) == length(qstatreqbyts)
@test give_length(qstatreqdeser) == length(qstatreqbyts)

# OfpQueueStats
# Test the constructor.
qstat = OfpQueueStats(uint16(1), uint32(2), uint64(3), uint64(4), uint64(5))
@test qstat.port_no == 1
@test qstat.queue_id == 2
@test qstat.tx_bytes == 3
@test qstat.tx_packets == 4
@test qstat.tx_errors == 5
# Test the serialization.
qstatbyts = b"\x00\x01"
append!(qstatbyts, b"\x00\x00")
append!(qstatbyts, b"\x00\x00\x00\x02")
append!(qstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x03")
append!(qstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x04")
append!(qstatbyts, b"\x00\x00\x00\x00\x00\x00\x00\x05")
@test bytes(qstatbyts) == qstatbyts
# Test the deserialization.
qstatdeser = OfpQueueStats(qstatbyts)
@test qstatdeser.port_no == 1
@test qstatdeser.queue_id == 2
@test qstatdeser.tx_bytes == 3
@test qstatdeser.tx_packets == 4
@test qstatdeser.tx_errors == 5
# Test length.
@test give_length(qstat) == length(qstatbyts)
@test give_length(qstatdeser) == length(qstatbyts)

# OfpVendorStatsRequest
# Test the constructor.
venstatreq = OfpVendorStatsRequest(b"\x00\x00\x00\x01", b"\x00\x02")
@test venstatreq.vendor_id == b"\x00\x00\x00\x01"
@test venstatreq.body == b"\x00\x02"
# Test the serialization.
venstatreqbyts = b"\x00\x00\x00\x01\x00\x02"
@test bytes(venstatreq) == venstatreqbyts
# Test the deserialization.
venstatreqdeser = OfpVendorStatsRequest(venstatreqbyts)
@test venstatreqdeser.vendor_id == b"\x00\x00\x00\x01"
@test venstatreqdeser.body == b"\x00\x02"
# Test length.
@test give_length(venstatreq) == length(venstatreqbyts)
@test give_length(venstatreqdeser) == length(venstatreqbyts)

# OfpVendorStatsReply
# Test the constructor.
venstatrep = OfpVendorStatsReply(b"\x00\x00\x00\x01", b"\x00\x02")
@test venstatrep.vendor_id == b"\x00\x00\x00\x01"
@test venstatrep.body == b"\x00\x02"
# Test the serialization.
venstatrepbyts = b"\x00\x00\x00\x01\x00\x02"
@test bytes(venstatrep) == venstatrepbyts
# Test the deserialization.
venstatrepdeser = OfpVendorStatsReply(venstatrepbyts)
@test venstatrepdeser.vendor_id == b"\x00\x00\x00\x01"
@test venstatrepdeser.body == b"\x00\x02"
# Test length.
@test give_length(venstatrep) == length(venstatrepbyts)
@test give_length(venstatrepdeser) == length(venstatrepbyts)

# OfpStatsRequest
header = OfpHeader(OFPT_STATS_REQUEST)
venstatreq = OfpVendorStatsRequest(b"\x00\x00\x00\x01", b"\x00\x02")
# Test the constructor.
statreq = OfpStatsRequest{OfpVendorStatsRequest}(header, OFPST_VENDOR, uint16(13),
    venstatreq)
@test statreq.header == header
@test statreq.typ == OFPST_VENDOR
@test statreq.flags == 13
@test statreq.body == venstatreq
# Test the serialization.
statreqbyts = bytes(header)
append!(statreqbyts, b"\xff\xff")
append!(statreqbyts, b"\x00\x0D")
append!(statreqbyts, bytes(venstatreq))
@test bytes(statreq) == statreqbyts
# Test the deserialization.
statreqdeser = OfpStatsRequest(header, statreqbyts[9:end])
@test statreqdeser.header == header
@test statreqdeser.typ == OFPST_VENDOR
@test statreqdeser.flags == 13
@test statreqdeser.body.vendor_id == b"\x00\x00\x00\x01"
@test statreqdeser.body.body == b"\x00\x02"
# Test length.
@test give_length(statreq) == length(statreqbyts)
@test give_length(statreqdeser) == length(statreqbyts)

# OfpStatsReply
header = OfpHeader(OFPT_STATS_REPLY)
venstatrep = OfpVendorStatsReply(b"\x00\x00\x00\x01", b"\x00\x02")
# Test the constructor.
statrep = OfpStatsReply{OfpVendorStatsReply}(header, OFPST_VENDOR, uint16(13), venstatrep)
@test statrep.header == header
@test statrep.typ == OFPST_VENDOR
@test statrep.flags == 13
@test statrep.body == venstatrep
# Test the serialization.
statrepbyts = bytes(header)
append!(statrepbyts, b"\xff\xff")
append!(statrepbyts, b"\x00\x0D")
append!(statrepbyts, bytes(venstatrep))
@test bytes(statrep) == statrepbyts
# Test the deserialization.
statrepdeser = OfpStatsReply(header, statrepbyts[9:end])
@test statrepdeser.header == header
@test statrepdeser.typ == OFPST_VENDOR
@test statrepdeser.flags == 13
@test statrepdeser.body.vendor_id == b"\x00\x00\x00\x01"
@test statrepdeser.body.body == b"\x00\x02"
# Test length.
@test give_length(statrep) == length(statrepbyts)
@test give_length(statrepdeser) == length(statrepbyts)

# OfpPacketOut
actout = OfpActionOutput(OFPAT_OUTPUT, uint16(8), uint16(3), uint16(64))
actven = OfpActionVendor(OFPAT_VENDOR, uint16(24), uint32(63), ones(Uint8, 16))
acttp = OfpActionTpPort(OFPAT_SET_TP_SRC, uint16(8), uint16(13))
actlen = mapreduce(give_length, +, [actout, actven, acttp])
header = OfpHeader(OFPT_PACKET_OUT)
# Test the constructor.
packout = OfpPacketOut(header, uint32(15), uint16(16), uint16(actlen), [actout,
    actven, acttp])
@test packout.header == header
@test packout.buffer_id == 15
@test packout.in_port == 16
@test packout.actions_len == actlen
@test packout.actions[1] == actout
@test packout.actions[2] == actven
@test packout.actions[3] == acttp
# Test the serialization.
packoutbyts = bytes(header)
append!(packoutbyts, b"\x00\x00\x00\x0F")
append!(packoutbyts, b"\x00\x10")
append!(packoutbyts, b"\x00\x28")
packoutbyts = reduce((bs, act)->append!(bs, bytes(act)), packoutbyts, [actout, actven, acttp])
@test bytes(packout) == packoutbyts
# Test the deserialization.
packoutdeser = OfpPacketOut(header, packoutbyts[9:end])
@test packoutdeser.header == header
@test packoutdeser.buffer_id == 15
@test packoutdeser.in_port == 16
@test packoutdeser.actions_len == actlen
@test bytes(packout.actions[1]) == bytes(actout)
@test bytes(packout.actions[2]) == bytes(actven)
@test bytes(packout.actions[3]) == bytes(acttp)
# Test length.
@test give_length(packout) == length(packoutbyts)
@test give_length(packoutdeser) == length(packoutbyts)

# OfpFlowRemoved
dl_src = Array(Uint8, OFP_MAX_ETH_ALEN)
dl_dst = Array(Uint8, OFP_MAX_ETH_ALEN)
match = OfpMatch(uint32(0), uint16(13), dl_src, dl_dst, uint16(13), uint16(13),
    uint16(3), uint8(1), uint8(3), uint32(13), uint32(14), uint16(3), uint16(4))
header = OfpHeader(OFPT_FLOW_REMOVED)
# Test the constructor.
florem = OfpFlowRemoved(header, match, uint64(1), uint16(1), OFPRR_IDLE_TIMEOUT,
    uint32(10), uint32(10), uint16(15), uint64(13), uint64(26))
@test florem.header == header
@test florem.match == match
@test florem.cookie == 1
@test florem.priority == 1
@test florem.reason == OFPRR_IDLE_TIMEOUT
@test florem.duration_sec == 10
@test florem.duration_nsec == 10
@test florem.idle_timeout == 15
@test florem.packet_count == 13
@test florem.byte_count == 26
# Test the serialization.
florembyts = bytes(header)
append!(florembyts, bytes(match))
append!(florembyts, b"\x00\x00\x00\x00\x00\x00\x00\x01")
append!(florembyts, b"\x00\x01")
append!(florembyts, b"\x00\x00")
append!(florembyts, b"\x00\x00\x00\x0A")
append!(florembyts, b"\x00\x00\x00\x0A")
append!(florembyts, b"\x00\x0F\x00\x00")
append!(florembyts, b"\x00\x00\x00\x00\x00\x00\x00\x0D")
append!(florembyts, b"\x00\x00\x00\x00\x00\x00\x00\x1A")
@test bytes(florem) == florembyts
# Test the deserialization.
floremdeser = OfpFlowRemoved(header, florembyts[9:end])
@test floremdeser.header == header
@test floremdeser.match.wildcards == 0
@test floremdeser.match.in_port == 13
@test floremdeser.match.dl_src == dl_src
@test floremdeser.match.dl_dst == dl_dst
@test floremdeser.match.dl_vlan == 13
@test floremdeser.match.dl_vlan_pcp == 13
@test floremdeser.match.dl_type == 3
@test floremdeser.match.nw_tos == 1
@test floremdeser.match.nw_proto == 3
@test floremdeser.match.nw_src == 13
@test floremdeser.match.nw_dst == 14
@test floremdeser.match.tp_src == 3
@test floremdeser.match.tp_dst == 4
@test floremdeser.cookie == 1
@test floremdeser.priority == 1
@test floremdeser.reason == OFPRR_IDLE_TIMEOUT
@test floremdeser.duration_sec == 10
@test floremdeser.duration_nsec == 10
@test floremdeser.idle_timeout == 15
@test floremdeser.packet_count == 13
@test floremdeser.byte_count == 26
# Test length.
@test give_length(florem) == length(florembyts)
@test give_length(floremdeser) == length(florembyts)

# OfpVendorHeader
header = OfpHeader(OFPT_VENDOR)
# Test the constructor.
venhed = OfpVendorHeader(header, uint32(3), b"\x11\x11\x11\x11")
@test venhed.header == header
@test venhed.vendor == 3
@test venhed.body == b"\x11\x11\x11\x11"
# Test the serialization.
venhedbyts = bytes(header)
append!(venhedbyts, b"\x00\x00\x00\x03")
append!(venhedbyts, b"\x11\x11\x11\x11")
@test bytes(venhed) == venhedbyts
# Test the deserialization.
venheddeser = OfpVendorHeader(header, venhedbyts[9:end])
@test venheddeser.header == header
@test venheddeser.vendor == 3
@test venheddeser.body == b"\x11\x11\x11\x11"
# Test length.
@test give_length(venhed) == 16
@test give_length(venheddeser) == 16

toc()
println("It seems that all went fine, congratulations!")

max_stars_repo_name: pchronz/juliaflow

max_issues_repo_name: pchronz/juliaflow

max_forks_repo_name: pchronz/juliaflow


text:
include("taxi_models_and_data.jl")
ld = 2
Gconv = BSON.load("conv_generator_results/BCE_BS256_LR0.0007/conv_generator_ld$(ld).bson")[:G]
Gconv = TaxiGConv(Gconv.g_labels |> gpu, Gconv.g_latent |>gpu, Gconv.g_common |> gpu)

D = BSON.load("conv_generator_results/BCE_BS256_LR0.0007/conv_discriminator_ld$(ld).bson")[:D]
D = TaxiDConvSpectral(D.d_labels |> gpu, D.d_common |> gpu)

## Parameters
batch_size = 256
iter = 50000
verbose_freq = 1000

for λ in [0f0, 1f-4, 1f-3, 1f-2, 1f-1, 1f0] # Parameter for the discriminator output
	for lr in [7f-3, 7f-4] # Learning rate
		s = Settings(latent_dim=ld, nclasses=2, G=nothing,D=nothing,loss=nothing, img_fun=nothing, rand_input=nothing, batch_size=batch_size)
		m = TaxiGMLP(Chain(Dense(s.latent_dim + s.nclasses, 256, relu, init=Flux.orthogonal), Dense(256, 256, relu, init=Flux.orthogonal), Dense(256, 256, relu, init=Flux.orthogonal), Dense(256, 256, relu, init=Flux.orthogonal), Dense(256, 16*8, init=Flux.orthogonal), x -> reshape(x, 16,8,1,:)) |> gpu)
		# m = TaxiGMLP(Chain(Dense(s.latent_dim + s.nclasses, 128, relu, init=Flux.orthogonal), Dense(128, 128, relu, init=Flux.orthogonal), Dense(128, 16*8, init=Flux.orthogonal), x -> reshape(x, 16,8,1,:)) |> gpu) # Smaller mlp
		output_dir = "mlp_generator_results/MLP128x2_mae_adv_ld$(ld)_λ$(λ)_LR$(lr)"

		data, fixed_noise, fixed_labels = gen_Taxi_images(s)
		θ = Flux.params(m)
		opt = ADAM(lr)
		losses = []
		for i=0:iter
			x, y = Taxi_input(s)
			l, back = Flux.pullback(()-> begin
				xtilde = m(x,y)
				Flux.mae(Gconv(x,y), xtilde) - λ*mean(tanh.(D(xtilde, y))) + orthogonal_regularization(m)
			end, θ)
			if i % verbose_freq == 0
				name = @sprintf("mlpgan_iter_%06d.png", i)
				save(string(output_dir, "/", name), to_image(m, fixed_noise, fixed_labels, s))
				println("iter: $i, loss: ", l)
			end
			push!(losses, l)
			update!(opt, θ, back(1f0))
		end
		G = TaxiGMLP(m.net |> cpu)
		BSON.@save "$(output_dir)/mlp_generator.bson" G
		BSON.@save "$(output_dir)/losses.bson" losses
	end
end

max_stars_repo_name: sisl/VerifyGAN

max_issues_repo_name: sisl/VerifyGAN

max_forks_repo_name: sisl/VerifyGAN


text:
@enum IndexType::UInt8 NotAnIndex=0 LoopIndex=1 ComputedIndex=2 SymbolicIndex=3

Base.:|(u::Unsigned, it::IndexType) = u | UInt8(it)
Base.:(==)(u::Unsigned, it::IndexType) = (u % UInt8) == UInt8(it)

"""
    ArrayRefStruct

A condensed representation of an [`ArrayReference`](@ref).
It supports array-references with up to 8 indexes, where the data for each consecutive index is packed into corresponding 8-bit fields
of `index_types` (storing the enum `IndexType`), `indices` (the `id` for each index symbol), and `offsets` (currently unused).
"""
struct ArrayRefStruct{array,ptr}
    index_types::UInt64
    indices::UInt64
    offsets::UInt64
    strides::UInt64
end
array(ar::ArrayRefStruct{a,p}) where {a,p} = a
ptr(ar::ArrayRefStruct{a,p}) where {a,p}   = p

function findindoradd!(v::Vector{T}, s::T) where {T}
    ind = findfirst(==(s), v)
    ind === nothing || return ind
    push!(v, s)
    length(v)
end
function ArrayRefStruct(ls::LoopSet, mref::ArrayReferenceMeta, arraysymbolinds::Vector{Symbol}, ids::Vector{Int})
    index_types = zero(UInt64)
    indices = zero(UInt64)
    offsets = zero(UInt64)
    strides = zero(UInt64)
    @unpack loopedindex, ref = mref
    indv = ref.indices
    offv = ref.offsets
    strv = ref.strides
    # we can discard that the array was considered discontiguous, as it should be recovered from type information
    start = 1 + (first(indv) === DISCONTIGUOUS)
    for (n,ind) ∈ enumerate(@view(indv[start:end]))
        index_types <<= 8
        indices <<= 8
        offsets <<= 8
        offsets |= (offv[n] % UInt8)
        strides <<= 8
        strides |= (strv[n] % UInt8)
        if loopedindex[n]
            index_types |= LoopIndex
            if strv[n] ≠ 0
                indices |= getloopid(ls, ind)
            end
        else
            parent = get(ls.opdict, ind, nothing)
            @assert !(parent === nothing) "Index $ind not found in array."
            # if parent === nothing
            #     index_types |= SymbolicIndex
            #     indices |= findindoradd!(arraysymbolinds, ind)
            # else
            index_types |= ComputedIndex
            indices |= ids[identifier(parent)]
            # end
        end
    end
    ArrayRefStruct{mref.ref.array,mref.ptr}( index_types, indices, offsets, strides )
end

"""
    OperationStruct

A condensed representation of an [`Operation`](@ref).
"""
struct OperationStruct <: AbstractLoopOperation
    # instruction::Instruction
    loopdeps::UInt64
    reduceddeps::UInt64
    childdeps::UInt64
    parents::UInt64
    node_type::OperationType
    array::UInt8
    symid::UInt8
end
optype(os) = os.node_type

function findmatchingarray(ls::LoopSet, mref::ArrayReferenceMeta)
    id = 0x01
    for r ∈ ls.refs_aliasing_syms
        r == mref && return id
        id += 0x01
    end
    0x00
end
# filled_4byte_chunks(u::UInt64) = 16 - (leading_zeros(u) >>> 2)
filled_8byte_chunks(u::UInt64) = 8 - (leading_zeros(u) >>> 3)

# num_loop_deps(os::OperationStruct) = filled_4byte_chunks(os.loopdeps)
# num_reduced_deps(os::OperationStruct) = filled_4byte_chunks(os.reduceddeps)
# num_child_deps(os::OperationStruct) = filled_4byte_chunks(os.childdeps)
# num_parents(os::OperationStruct) = filled_4byte_chunks(os.parents)

function shifted_loopset(ls::LoopSet, loopsyms::Vector{Symbol})
    ld = zero(UInt64) # leading_zeros(ld) >> 2 yields the number of loopdeps
    for d ∈ loopsyms
        ld <<= 4
        ld |= getloopid(ls, d)::Int
    end
    ld
end
loopdeps_uint(ls::LoopSet, op::Operation) = shifted_loopset(ls, loopdependencies(op))
reduceddeps_uint(ls::LoopSet, op::Operation) = shifted_loopset(ls, reduceddependencies(op))
childdeps_uint(ls::LoopSet, op::Operation) = shifted_loopset(ls, reducedchildren(op))
function parents_uint(ls::LoopSet, op::Operation)
    p = zero(UInt64)
    for parent ∈ parents(op)
        p <<= 8
        p |= identifier(parent)
    end
    p
end
function OperationStruct!(varnames::Vector{Symbol}, ids::Vector{Int}, ls::LoopSet, op::Operation)
    instr = instruction(op)
    ld = loopdeps_uint(ls, op)
    rd = reduceddeps_uint(ls, op)
    cd = childdeps_uint(ls, op)
    p = parents_uint(ls, op)
    array = accesses_memory(op) ? findmatchingarray(ls, op.ref) : 0x00
    ids[identifier(op)] = id = findindoradd!(varnames, name(op))
    OperationStruct(
        ld, rd, cd, p, op.node_type, array, id
    )
end
## turn a LoopSet into a type object which can be used to reconstruct the LoopSet.

function loop_boundary!(q::Expr, loop::Loop)
    if isstaticloop(loop) || loop.rangesym === Symbol("")
        call = Expr(:call, :(:))
        pushexpr!(call, first(loop))
        unitstep(loop) || pushexpr!(call, step(loop))
        pushexpr!(call, last(loop))
        push!(q.args, call)
    else
        push!(q.args, loop.rangesym)
    end
end

function loop_boundaries(ls::LoopSet)
    lbd = Expr(:tuple)
    for loop ∈ ls.loops
        loop_boundary!(lbd, loop)
    end
    lbd
end

tuple_expr(v) = tuple_expr(identity, v)
function tuple_expr(f, v)
    t = Expr(:tuple)
    for vᵢ ∈ v
        push!(t.args, f(vᵢ))
    end
    t
end

function argmeta_and_consts_description(ls::LoopSet, arraysymbolinds)
    Expr(
        :tuple,
        length(arraysymbolinds),
        tuple_expr(ls.outer_reductions),
        tuple_expr(first, ls.preamble_symsym),
        tuple_expr(ls.preamble_symint),
        tuple_expr(ls.preamble_symfloat),
        tuple_expr(ls.preamble_zeros),
        tuple_expr(ls.preamble_funcofeltypes)
    )
end

function loopset_return_value(ls::LoopSet, ::Val{extract}) where {extract}
    @assert !iszero(length(ls.outer_reductions))
    if isone(length(ls.outer_reductions))
        op = getop(ls, ls.outer_reductions[1])
        if extract
            # if (isu₁unrolled(op) | isu₂unrolled(op))
                Expr(:call, :data, Symbol(mangledvar(op), "##onevec##"))
            # else
                # Expr(:call, :data, mangledvar(op))
            # end
        else
            Symbol(mangledvar(op), "##onevec##")
        end
    else#if length(ls.outer_reductions) > 1
        ret = Expr(:tuple)
        ops = operations(ls)
        for or ∈ ls.outer_reductions
            op = ops[or]
            if extract
                push!(ret.args, Expr(:call, :data, Symbol(mangledvar(op), "##onevec##")))
            else
                push!(ret.args, Symbol(mangledvar(ops[or]), "##onevec##"))
            end
        end
        ret
    end
end

function add_reassigned_syms!(q::Expr, ls::LoopSet)
    for op ∈ operations(ls)
        if isconstant(op)
            instr = instruction(op)
            (instr == LOOPCONSTANT || instr.mod === :numericconstant) || push!(q.args, instr.instr)
        end
    end
end
function add_external_functions!(q::Expr, ls::LoopSet)
    for op ∈ operations(ls)
        if iscompute(op)
            instr = instruction(op)
            if instr.mod !== :LoopVectorization
                push!(q.args, instr.instr)
            end
        end
    end
end

function check_if_empty(ls::LoopSet, q::Expr)
    lb = loop_boundaries(ls)
    Expr(:if, Expr(:call, :!, Expr(:call, :any, :isempty, lb)), q)
end

val(x) = Expr(:call, Expr(:curly, :Val, x))

function add_grouped_strided_pointer!(extra_args::Expr, ls::LoopSet)
    gsp = Expr(:call, lv(:grouped_strided_pointer))
    tgarrays = Expr(:tuple)
    i = 0
    preserve_assignment = Expr(:tuple); preserve = Symbol[];
    @unpack equalarraydims, refs_aliasing_syms = ls
    # duplicate_map = collect(1:length(refs_aliasing_syms))
    duplicate_map = Vector{Int}(undef, length(refs_aliasing_syms))
    for (j,ref) ∈ enumerate(refs_aliasing_syms)
        vpref = vptr(ref)
        duplicate = false
        for k ∈ 1:j-1 # quadratic, but should be short enough so that this is faster than O(1) algs
            if vptr(refs_aliasing_syms[k]) === vpref
                duplicate = true
                # duplicate_map[j] = k
                break
            end
        end
        duplicate && continue
        # duplicate_map[j] == j || continue
        duplicate_map[j] = (i += 1)
        found = false
        # for (vptrs,dims) ∈ equalarraydims
        #     _id = findfirst(==(vpref), vptrs)
        #     _id === nothing && continue
        #     id::Int = _id
        #     found = true
        #     if vptr(ref.ref.array) === vpref
        #         push!(tgarrays.args, ref.ref.array)
        #     else
        #         push!(tgarrays.args, vpref)
        #         push!(preserve, ref.ref.array)
        #     end
        #     break
        # end
        # if !found
        pres = gensym!(ls, "#preserve#")
        push!(preserve_assignment.args, pres)
        if vptr(ref.ref.array) === vpref
            push!(tgarrays.args, ref.ref.array)
            push!(preserve, pres)
        else
            push!(tgarrays.args, vpref)
            push!(preserve, ref.ref.array)
        end
    end
    push!(gsp.args, tgarrays)
    matcheddims = Expr(:tuple)
    for (vptrs,dims) ∈ equalarraydims
        t = Expr(:tuple)
        for (vp,d) ∈ zip(vptrs,dims)
            _id = findfirst(Base.Fix2(===,vp) ∘ vptr, refs_aliasing_syms)
            _id === nothing && continue
            push!(t.args, Expr(:tuple, duplicate_map[_id], d))
        end
        length(t.args) > 1 && push!(matcheddims.args, t)
    end
    push!(gsp.args, val(matcheddims))
    gsps = gensym!(ls, "#grouped#strided#pointer#")
    push!(extra_args.args, gsps)
    pushpreamble!(ls, Expr(:(=), Expr(:tuple, gsps, preserve_assignment), gsp))
    preserve
end

# first_cache() = ifelse(gt(num_cache_levels(), StaticInt{2}()), StaticInt{2}(), StaticInt{1}())
# function _first_cache_size(::StaticInt{FCS}) where {FCS}
#     L1inclusive = StaticInt{FCS}() - VectorizationBase.cache_size(One())
#     ifelse(eq(first_cache(), StaticInt(2)) & VectorizationBase.cache_inclusive(StaticInt(2)), L1inclusive, StaticInt{FCS}())
# end
# _first_cache_size(::Nothing) = StaticInt(262144)
# first_cache_size() = _first_cache_size(cache_size(first_cache()))

@generated function _avx_config_val(
    ::Val{inline}, ::Val{u₁}, ::Val{u₂}, ::Val{thread}, ::StaticInt{W},
    ::StaticInt{RS}, ::StaticInt{AR}, ::StaticInt{NT},
    ::StaticInt{CLS}, ::StaticInt{L1}, ::StaticInt{L2}, ::StaticInt{L3}
) where {inline,u₁,u₂,thread,W,RS,AR,CLS,L1,L2,L3,NT}
    nt = min(thread, NT % UInt)
    t = Expr(:tuple, inline, u₁, u₂, W, RS, AR, CLS, L1,L2,L3, nt)
    Expr(:call, Expr(:curly, :Val, t))
end
@inline function avx_config_val(::Val{inline}, ::Val{u₁}, ::Val{u₂}, ::Val{thread}, ::StaticInt{W}) where {inline,u₁,u₂,thread,W}
    _avx_config_val(
        Val{inline}(), Val{u₁}(), Val{u₂}(), Val{thread}(), StaticInt{W}(),
        register_size(), available_registers(), lv_max_num_threads(),
        cache_linesize(), cache_size(StaticInt(1)), cache_size(StaticInt(2)), cache_size(StaticInt(3))
    )
end
# Try to condense in type stable manner
function generate_call(ls::LoopSet, (inline,u₁,u₂)::Tuple{Bool,Int8,Int8}, thread::UInt, debug::Bool = false)
    operation_descriptions = Expr(:tuple)
    varnames = Symbol[]; ids = Vector{Int}(undef, length(operations(ls)))
    for op ∈ operations(ls)
        instr = instruction(op)
        push!(operation_descriptions.args, QuoteNode(instr.mod))
        push!(operation_descriptions.args, QuoteNode(instr.instr))
        push!(operation_descriptions.args, OperationStruct!(varnames, ids, ls, op))
    end
    arraysymbolinds = Symbol[]
    arrayref_descriptions = Expr(:tuple)
    duplicate_ref = fill(false, length(ls.refs_aliasing_syms))
    for (j,ref) ∈ enumerate(ls.refs_aliasing_syms)
        vpref = vptr(ref)
        # duplicate_ref[j] ≠ 0 && continue
        duplicate_ref[j] && continue
        push!(arrayref_descriptions.args, ArrayRefStruct(ls, ref, arraysymbolinds, ids))
    end
    argmeta = argmeta_and_consts_description(ls, arraysymbolinds)
    loop_bounds = loop_boundaries(ls)
    loop_syms = tuple_expr(QuoteNode, ls.loopsymbols)
    func = debug ? lv(:_avx_loopset_debug) : lv(:_avx_!)
    lbarg = debug ? Expr(:call, :typeof, loop_bounds) : loop_bounds
    unroll_param_tup = Expr(:call, lv(:avx_config_val), :(Val{$inline}()), :(Val{$u₁}()), :(Val{$u₂}()), :(Val{$thread}()), VECTORWIDTHSYMBOL)
    q = Expr(:call, func, unroll_param_tup, val(operation_descriptions), val(arrayref_descriptions), val(argmeta), val(loop_syms))
    # debug && deleteat!(q.args, 2)
    vargs_as_tuple = true#!debug
    vargs_as_tuple || push!(q.args, lbarg)
    extra_args = vargs_as_tuple ? Expr(:tuple) : q
    preserve = add_grouped_strided_pointer!(extra_args, ls)
    for is ∈ ls.preamble_symsym
        push!(extra_args.args, last(is))
    end
    append!(extra_args.args, arraysymbolinds)
    add_reassigned_syms!(extra_args, ls)
    add_external_functions!(extra_args, ls)
    # debug && return q
    vargs_as_tuple && push!(q.args, Expr(:tuple, lbarg, extra_args))
    vecwidthdefq = Expr(:block)
    define_eltype_vec_width!(vecwidthdefq, ls, nothing)
    Expr(:block, vecwidthdefq, q), preserve
end

"""
    check_args(::Vararg{AbstractArray})

LoopVectorization will optimize an `@avx` loop if `check_args` on each on the indexed abstract arrays returns true.
It returns true for `AbstractArray{T}`s when `check_type(T) == true` and the array or its parent is a `StridedArray` or `AbstractRange`.

To provide support for a custom array type, ensure that `check_args` returns true, either through overloading it or subtyping `DenseArray`.
Additionally, define `pointer` and `stride` methods.
"""
@inline function check_args(A::AbstractArray{T}) where {T}
    check_type(T) && check_device(ArrayInterface.device(A))
end
@inline check_args(A::BitVector) = true
@inline check_args(A::BitArray) = iszero(size(A,1) & 7)
@inline check_args(::VectorizationBase.AbstractStridedPointer) = true
@inline function check_args(x)
    @info "`LoopVectorization.check_args(::$(typeof(x))) == false`, therefore compiling a probably slow `@inbounds @fastmath` fallback loop." maxlog=1
    false
end
@inline check_args(A, B, C::Vararg{Any,K}) where {K} = check_args(A) && check_args(B, C...)
@inline check_args(::AbstractRange{T}) where {T} = check_type(T)
@inline check_args(::Type{T}) where {T} = check_type(T)
"""
    check_type(::Type{T}) where {T}

Returns true if the element type is supported.
"""
@inline check_type(::Type{T}) where {T <: NativeTypes} = true
function check_type(::Type{T}) where {T}
    @info """`LoopVectorization.check_type` returned `false`, because `LoopVectorization.check_type(::$(T)) == false`.
        `LoopVectorization` currently only supports `T <: $(NativeTypes)`.
        Therefore compiling a probably slow `@inbounds @fastmath` fallback loop.""" maxlog=1
    false
end
@inline check_device(::ArrayInterface.CPUPointer) = true
@inline check_device(::ArrayInterface.CPUTuple) = true
function check_device(x)
    @info """`LoopVectorization.check_args` returned `false`, because `ArrayInterface.device(::$(typeof(x))) == $x`
        `LoopVectorization` normally requires `ArrayInterface.CPUPointer` (exceptions include ranges, `BitVector`s, and
        `BitArray`s whose number of rows is a multiple of 8). Therefore compiling a probably slow `@inbounds @fastmath` fallback loop.""" maxlog=1
    false
end

function check_args_call(ls::LoopSet)
    q = Expr(:call, lv(:check_args))
    append!(q.args, ls.includedactualarrays)
    for r ∈ ls.outer_reductions
        push!(q.args, Expr(:call, :typeof, name(ls.operations[r])))
    end
    q
end

make_fast(q) = Expr(:macrocall, Symbol("@fastmath"), LineNumberNode(@__LINE__,Symbol(@__FILE__)), q)
make_crashy(q) = Expr(:macrocall, Symbol("@inbounds"), LineNumberNode(@__LINE__,Symbol(@__FILE__)), q)

@inline vecmemaybe(x::NativeTypes) = x
@inline vecmemaybe(x::VectorizationBase._Vec) = Vec(x)
@inline vecmemaybe(x::Tuple) = VectorizationBase.VecUnroll(x)

function gc_preserve(call::Expr, preserve::Vector{Symbol})
    q = Expr(:gc_preserve, call)
    append!(q.args, preserve)
    q
end

function setup_call_inline(ls::LoopSet, inline::Bool, u₁::Int8, u₂::Int8, thread::Int)
    call, preserve = generate_call(ls, (inline,u₁,u₂), thread % UInt, false)
    if iszero(length(ls.outer_reductions))
        pushpreamble!(ls, gc_preserve(call, preserve))
        push!(ls.preamble.args, nothing)
        return ls.preamble
    end
    retv = loopset_return_value(ls, Val(false))
    outer_reducts = Expr(:local)
    q = Expr(:block,gc_preserve(Expr(:(=), retv, call), preserve))
    for or ∈ ls.outer_reductions
        op = ls.operations[or]
        var = name(op)
        # push!(call.args, Symbol("##TYPEOF##", var))
        mvar = mangledvar(op)
        instr = instruction(op)
        out = Symbol(mvar, "##onevec##")
        push!(outer_reducts.args, out)
        push!(q.args, Expr(:(=), var, Expr(:call, lv(reduction_scalar_combine(instr)), Expr(:call, lv(:vecmemaybe), out), var)))
    end
    pushpreamble!(ls, outer_reducts)
    append!(ls.preamble.args, q.args)
    ls.preamble
end
function setup_call_debug(ls::LoopSet)
    # avx_loopset(instr, ops, arf, AM, LB, vargs)
    pushpreamble!(ls, first(generate_call(ls, (false,zero(Int8),zero(Int8)), zero(UInt), true)))
    Expr(:block, ls.prepreamble, ls.preamble)
end
function setup_call(
    ls::LoopSet, q::Expr, source::LineNumberNode, inline::Bool, check_empty::Bool, u₁::Int8, u₂::Int8, thread::Int
)
    # We outline/inline at the macro level by creating/not creating an anonymous function.
    # The old API instead was based on inlining or not inline the generated function, but
    # the generated function must be inlined into the initial loop preamble for performance reasons.
    # Creating an anonymous function and calling it also achieves the outlining, while still
    # inlining the generated function into the loop preamble.
    lnns = extract_all_lnns(q)
    pushfirst!(lnns, source)
    call = setup_call_inline(ls, inline, u₁, u₂, thread)
    call = check_empty ? check_if_empty(ls, call) : call
    result = Expr(:block, ls.prepreamble, Expr(:if, check_args_call(ls), call, make_crashy(make_fast(q))))
    prepend_lnns!(result, lnns)
    return result
end


text:
using RecipesBase: apply_recipe

@testset "plotting - $K" for K in (Gaussian, AiryDisk, Moffat)
    psf = K(x=0, y=1, fwhm=5)
    recipes = apply_recipe(Dict{Symbol,Any}(), psf)
    for rec in recipes
        @test rec.args[1] === psf
        ys, xs = axes(psf)
        @test rec.args[2] == ys
        @test rec.args[3] == xs
    end

    recipes_full = apply_recipe(Dict{Symbol,Any}(), psf, axes(psf)...)
    for rec in recipes_full
        @test rec.plotattributes == Dict{Symbol,Any}(
            :seriestype => :heatmap,
            :xlims => (-8, 8),
            :ylims => (-7, 9),
            :aspect_ratio => 1
        )

        xs = rec.args[1]
        ys = rec.args[2]
        _psf = rec.args[3]

        @test xs == -8:8
        @test ys == -7:9
        @test _psf == collect(psf)
    end
end


text:
#Estimate from CDC COVID-19 Response Team. Severe Outcomes Among Patients with Coronavirus Disease 2019 (COVID-19) - United States, February 12-March 16, 2020. MMWR Morb. Mortal. Wkly. Rep. 69, 343–346 (2020).
hosp_rate_CDC = [mean([1.6,2.5]),#0-19 year olds
                 mean([14.3,20.8]),#20-44 yos
                 mean([21.2,28.3]),#45-54
                 mean([20.5,30.1]),#55-64
                 mean([28.6,43.5]),#65-74
                 mean([30.5,58.7]),#75-84
                 mean([31.3,70.3])]./100 #85+

ICU_rate_CDC = [0.,#0-19 year olds
              mean([2.,4.2]),#20-44 yos
              mean([5.4,10.4]),#45-54
              mean([4.7,11.2]),#55-64
              mean([8.1,18.8]),#65-74
              mean([10.5,31.0]),#75-84
              mean([6.3,29.])]./100 #85+

hosp_rate_by_age = [hosp_rate_CDC[1],hosp_rate_CDC[1],hosp_rate_CDC[1],hosp_rate_CDC[1],
                    hosp_rate_CDC[2],hosp_rate_CDC[2],hosp_rate_CDC[2],hosp_rate_CDC[2],hosp_rate_CDC[2],
                    hosp_rate_CDC[3],hosp_rate_CDC[3],
                    hosp_rate_CDC[4],hosp_rate_CDC[4],
                    hosp_rate_CDC[5],hosp_rate_CDC[5],
                    hosp_rate_CDC[6],hosp_rate_CDC[7]]

ICU_rate_by_age = [ICU_rate_CDC[1],ICU_rate_CDC[1],ICU_rate_CDC[1],ICU_rate_CDC[1],
                    ICU_rate_CDC[2],ICU_rate_CDC[2],ICU_rate_CDC[2],ICU_rate_CDC[2],ICU_rate_CDC[2],
                    ICU_rate_CDC[3],ICU_rate_CDC[3],
                    ICU_rate_CDC[4],ICU_rate_CDC[4],
                    ICU_rate_CDC[5],ICU_rate_CDC[5],
                    ICU_rate_CDC[6],ICU_rate_CDC[7]]

ICU_rate_by_age_cond_hosp = ICU_rate_by_age./hosp_rate_by_age
ICU_rate_by_age_cond_hosp.*0.625

bar(hosp_rate_by_age,lab = "",xticks = (1:2:17,))


text:
using OrdinaryDiffEq, Test, Random
Random.seed!(100)
setprecision(400)

f = (u,p,t) -> (2u)
#(::typeof(f))(::Type{Val{:analytic}},u0,p,t) = u0*exp(t)
prob = ODEProblem(f,1/2,(0.0,1.0))

sol3 =solve(prob,RK4(),dt=1/2^(6),abstol=1,reltol=0)

prob = ODEProblem(f,BigInt(1)//BigInt(2),(BigInt(0)//BigInt(1),BigInt(1)//BigInt(1)))

integrator = init(prob,RK4(),dt=BigInt(1)//BigInt(2)^(6),abstol=1,reltol=0)

sol =solve(prob,RK4(),dt=BigInt(1)//BigInt(2)^(6),abstol=1,reltol=0)
sol2 =solve(prob,RK4(),dt=BigInt(1)/BigInt(2)^(6),abstol=1,reltol=0)

sol.u
sol2.u
sol3.u
@test eltype(sol.u) == Rational{BigInt}
@test eltype(sol2.u) == Rational{BigInt}
@test eltype(sol3.u) == Float64

sol4 =solve(prob,DP5(),dt=BigInt(1)//BigInt(2)^(3),adaptive=false)

@test eltype(sol4.u) == Rational{BigInt}

tabalg = ExplicitRK(tableau = constructDormandPrince(Rational{BigInt}))

integrator = init(prob,tabalg,dt=BigInt(1)//BigInt(2)^(3),abstol=1,reltol=0,adaptive=false)
sol5 =solve(prob,tabalg,dt=BigInt(1)//BigInt(2)^(3),abstol=1,reltol=0,adaptive=false)

@test typeof(sol5.u[end]) == Rational{BigInt}

prob = ODEProblem(f,1/2+1/2im,(0.0,1.0))


text:
#! /usr/bin/julia

# Rosetta Code, Digital root

function digitalroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
    -1 < n && 1 < bs || throw(DomainError())
    ds = n
    pers = 0
    while bs <= ds
        ds = sum(digits(ds, bs))
        pers += 1
    end
    return (pers, ds)
end

for i in {627615, 39390, 588225, 393900588225, big(2)^100}
    (pers, ds) = digitalroot(i)
    println(i, " has persistence ", pers, " and digital root ", ds)
end

max_stars_repo_name: MichaeLeroy/rosetta-code

max_issues_repo_name: MichaeLeroy/rosetta-code

max_forks_repo_name: MichaeLeroy/rosetta-code