text:
struct Basic end

@abstract Basic getfield(m::Const{Module}, f::Const{Symbol}) =
  Const(getfield(m.value, f.value))

@abstract Basic getfield(m::T, f::Const{Symbol}) where T =
  fieldtype(T, f.value)

@abstract Basic Core.apply_type(Ts::Const...) =
  Const(Core.apply_type(map(T -> T.value, Ts)...))

@abstract Basic typeof(x::Const) = Const(widen(x))
@abstract Basic typeof(x::T) where T =
  isconcretetype(T) ? Const(T) : Type

@abstract Basic (isa)(x, ::Type{T}) where T = Const(widen(x) <: T)

@abstract Basic fieldtype(T::Const{<:Type}, f::Const{<:Union{Symbol,Integer}}) =
  Const(fieldtype(T.value, f.value))

@abstract Basic convert(::Const{Type{T}}, x::Const{<:Number}) where T<:Number =
  Const(convert(T, x.value))

@abstract Basic typeassert(x::X, ::Type{T}) where {T,X<:T} = x

@abstract Basic print(args...) = Nothing
@abstract Basic println(args...) = Nothing

@abstract Basic ifelse(c::Const{Bool}, a, b) =
  c.value ? a : b

@abstract Basic ifelse(c::Bool, a, b) = Union{widen(a),widen(b)}

@abstract Basic !(::Bool) = Bool
@abstract Basic !(x::Const{Bool}) = Const(!x.value)

effectful(::AType{typeof(print)}, args...) = true
effectful(::AType{typeof(println)}, args...) = true
effectful(::AType{typeof(setindex!)}, args...) = true

@pure Basic repr, isdefined

# Tweaked kwarg func handling

struct KwFunc{F} end

@abstract Basic Core.kwfunc(::T) where T = Const(KwFunc{T}())

instead(::Basic, args, ::AType{KwFunc{F}}, kw, f, xs...) where F =
  args, (Core.kwftype(widen(f)), kw, f, xs...)


text:
struct Radon <: AbstractProjectionAlgorithm end

_alg_method(::Radon) = IsRadonDiag()
@_defradonalgfn Radon radon_diag

"""
    radon_diag(image::AbstractMatrix, ts::AbstractVector, ϕs::AbstractVector; <keyword arguments>)

Compute the Radon transform of `image` inside a circle of
radius `hypot(rows,cols)/2` where `rows` and `cols` are the
dimensions of `image`.

See Also: [`radon_square`](@ref)
"""
function radon_diag end

@_defradonfn radon_diag begin
    @assert 0 ∈ first(ts)..last(ts)
    x₀::T = (cols + 1) / 2
    y₀::T = (rows + 1) / 2
    cθ::T = half(ts) * inv(√(1+τ^2))
    sθ::T = τ * cθ
    txs = @. T(ts * (x₀ - 1) / cθ)
    tys = @. T(ts * (y₀ - 1) / sθ)
    p = _radon_progress(length(scϕs), progress)
    Threads.@threads for iϕ ∈ eachindex(scϕs)
        @inbounds s, c = scϕs[iϕ]
        @inbounds @simd for i ∈ eachindex(ts)
            prex = txs[i] * c + x₀
            prey = tys[i] * s + y₀
            for j ∈ eachindex(ts)
                x = prex - txs[j] * s
                y = prey + tys[j] * c
                if 1 <= x <= cols && 1 <= y <= rows
                    sinog[i, iϕ] += interp(y, x)
                end
            end
        end
        next!(p)
    end
    γ::T = hypot(rows, cols) / min(rows, cols)
    δt::T = ν * γ * width(ts) / (length(ts) - 1)
    sinog .*= δt^2
end

# function radon_default(
#     image::AbstractMatrix{T},
#     ts::AbstractVector{X},
#     ϕs::AbstractVector{Y};
#     background::Optional{Z} = nothing,
#     rescaled::Bool = true,
#     interpolation::Optional{Interp} = nothing,
#     progress::Bool = true,
# ) where {
#     T <: Real,
#     X <: Real,
#     Y <: Real,
#     Z <: Real,
#     Interp <: AbstractInterp2DOrNone,
# }
#     rows, cols = size(image)
#     nd = length(ts)
#     nϕ = length(ϕs)
#     t₀::T = hypot(rows, cols) / 2
#     sθ, cθ = atan(rows, cols) |> sincos
#     x₀::T = t₀ * cθ + 1
#     y₀::T = t₀ * sθ + 1
#     scϕs = sincos.(ϕs)
#     z::T = maybe(zero(T), background)
#     rmat = similar(_atype(image), nd, nϕ)
#     fill!(rmat, z)
#     indices = Vector{NTuple{2,Int}}(undef, length(rmat))
#     tϕs = similar(indices, NTuple{2,T})
#     foreach(eachindex(rmat)) do k
#         ix = (k - 1) % nd
#         iϕ = (k - 1) ÷ nd
#         @inbounds t = ts[ix + 1]
#         @inbounds s, c = scϕs[iϕ + 1]
#         @inbounds indices[k] = k, iϕ
#         @inbounds tϕs[k] = t * c, t * s
#     end
#     rimage = rescaled ? rescale(image) : image
#     interp = isnothing(interpolation) ?
#         interpolate(rimage) : interpolation(rimage)
#     p = _radon_progress(length(rmat), progress)
#     Threads.@threads for (k, iϕ) ∈ indices
#         @inbounds tx, ty = tϕs[k]
#         prex, prey = tx + x₀, ty + y₀
#         o = iϕ * nd
#         @inbounds rmat[k] = sum(view(tϕs, o+1:o+nd)) do (ty, tx)
#             x, y = prex - tx, prey + ty
#             return x ∈ 1..cols && y ∈ 1..rows ? interp(y, x) : z
#         end
#         next!(p)
#     end
#     CTSinogram(rmat)
# end

ext: jl


text:
# Use baremodule to shave off a few KB from the serialized `.ji` file
baremodule LicenseSpring_jll
using Base
using Base: UUID
import JLLWrappers

JLLWrappers.@generate_main_file_header("LicenseSpring")
JLLWrappers.@generate_main_file("LicenseSpring", UUID("ea2c2f5b-d23d-53da-ad7e-0e62c0a9a2e9"))
end  # module LicenseSpring_jll


text:
module BouncingConesSimulation

using  ModiaLang
import Modia3D
using  Modia3D.ModiaInterface
using  Unitful

vmatRed   = Modia3D.VisualMaterial(color="Red")
vmatGreen = Modia3D.VisualMaterial(color="Green")
vmatBlue  = Modia3D.VisualMaterial(color="Blue")
vmatGrey  = Modia3D.VisualMaterial(color="Grey", transparency=0.5)

BouncingCones = Model(
    boxHeigth = 0.1,
    gravField = UniformGravityField(g=9.81, n=[0, 0, -1]),
    world = Object3D(feature=Scene(gravityField=:gravField,
                                   visualizeFrames=false,
                                   defaultFrameLength=0.2,
                                   visualizeBoundingBox = true,
                                   enableContactDetection=true,
                                   visualizeContactPoints=false,
                                   animationFile="BouncingCones.json")),
    worldFrame = Object3D(parent=:world, feature=Visual(shape=CoordinateSystem(length=0.5))),
    ground = Object3D(parent=:world,
                      translation=:[0.0, 0.0, -boxHeigth/2],
                      feature=Solid(shape=Box(lengthX=5.0, lengthY=3.0, lengthZ=:boxHeigth),
                                    visualMaterial=vmatGrey,
                                    solidMaterial="DryWood",
                                    collision=true)),
    frameX = Object3D(parent=:world,
                      translation=:[-1.0, -0.5, 1.0],
                      rotation=:[-90*u"°", 0.0, -90*u"°"],
                      feature=Visual(shape=CoordinateSystem(length=0.5))),
    coneX = Object3D(feature=Solid(shape=Cone(axis=1, diameter=0.4, length=1.0, topDiameter=0.3),
                                   visualMaterial=vmatRed,
                                   solidMaterial="DryWood",
                                   collision=true)),
    jointX = FreeMotion(obj1=:frameX, obj2=:coneX,
                        r=Var(init=[0.0, 0.0, 0.0]),
                        rot=Var(init=[0.0, -60*u"°", 0.0]),
                        v=Var(init=[0.0, 1.0, 0.0])),
    frameY = Object3D(parent=:world,
                      translation=:[0.0, -0.5, 1.0],
                      rotation=:[90*u"°", 90*u"°", 0.0],
                      feature=Visual(shape=CoordinateSystem(length=0.5))),
    coneY = Object3D(feature=Solid(shape=Cone(axis=2, diameter=0.4, length=1.0, topDiameter=0.3),
                                   visualMaterial=vmatGreen,
                                   solidMaterial="DryWood",
                                   collision=true)),
    jointY = FreeMotion(obj1=:frameY, obj2=:coneY,
                        r=Var(init=[0.0, 0.0, 0.0]),
                        rot=Var(init=[0.0, 0.0, -60*u"°"]),
                        v=Var(init=[0.0, 0.0, 1.0])),
    frameZ = Object3D(parent=:world,
                      translation=:[1.0, -0.5, 1.0],
                      feature=Visual(shape=CoordinateSystem(length=0.5))),
    coneZ = Object3D(feature=Solid(shape=Cone(axis=3, diameter=0.4, length=1.0, topDiameter=0.3),
                                   visualMaterial=vmatBlue,
                                   solidMaterial="DryWood",
                                   collision=true)),
    jointZ = FreeMotion(obj1=:frameZ, obj2=:coneZ,
                        r=Var(init=[0.0, 0.0, 0.0]),
                        rot=Var(init=[-60*u"°", 0.0, 0.0]),
                        v=Var(init=[1.0, 0.0, 0.0]))
)

bouncingCones = @instantiateModel(buildModia3D(BouncingCones), unitless=true, log=false, logStateSelection=false, logCode=false)

#@show bouncingCones.parameterExpressions
#@show bouncingCones.parameters

stopTime = 1.3
tolerance = 1e-8
requiredFinalStates = [-0.8002799390065646, 1.0059964298604027, 0.39039287407921686, -0.025430996181140066, 0.6630234043302788, 0.004309151343939569, 1.4686527860326997, 1.1368340779745558, 1.689846158106513, 3.3149879046571797, -0.06960222124441125, 0.1504261450316327, 0.3903893470590204, -0.8003514923828058, 1.0070415055199, 0.0011832492706785752, -0.055916265529015616, 0.6653193436925052, 2.717877804098592, -0.026017382314878938, -1.5277965562323037, 0.13944370362475916, 3.325930468488853, -0.09546231236269272, 1.0069646410862858, 0.3901947305115851, -0.8003618381020178, 0.6653930682717618, -6.006508633896099e-5, -0.0588692411973017, -1.6206623791265042, -0.006015411115857988, 2.717355681962454, -0.10225978390299145, 0.1363774922811096, 3.3263550311419343]
simulate!(bouncingCones, stopTime=stopTime, tolerance=tolerance, log=true, logStates=true, logEvents=true, requiredFinalStates=requiredFinalStates)

@usingModiaPlot
plot(bouncingCones, [("jointX.r", "jointY.r", "jointZ.r") ("jointX.rot", "jointY.rot", "jointZ.rot")
                     ("jointX.v", "jointY.v", "jointZ.v") ("jointX.w", "jointY.w", "jointZ.w")], figure=1)

end


text:
#=
A simple script for updating the manifest
files in all of our environments.
=#

root = dirname(@__DIR__)
dirs = (root, joinpath(root, "docs"), joinpath(root, "integration_tests"))

cd(root) do
    for dir in dirs
        cmd = `$(Base.julia_cmd()) --project=$dir -e 'import Pkg; Pkg.update()'`
        run(cmd)
    end
end

# https://github.com/JuliaLang/Pkg.jl/issues/3014
for dir in dirs
    cd(dir) do
        rm("LocalPreferences.toml"; force = true)
    end
end


text:
"""
   Partial(dist)

Creates the `Partial{dist}` distance.

`Partial{dist}`  returns the  minimum distance  between the shorter string and substrings of the longer string that have a length equal to the shorter string.

See http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/

### Examples
```julia-repl
julia> s1 = "New York Mets vs Atlanta Braves"
julia> s2 = "Atlanta Braves vs New York Mets"
julia> Partial(RatcliffObershelp())(s1, s2)
0.5483870967741935
```
"""
struct Partial{S <: SemiMetric} <: SemiMetric
    dist::S
end

function (dist::Partial)(s1, s2)
    ((s1 === missing) | (s2 === missing)) && return missing
    s1, s2 = reorder(s1, s2)
    len1, len2 = length(s1), length(s2)
    out = dist.dist(s1, s2)
    ((len1 == 0) | (len1 == len2)) && return out
    for x in qgrams(s2, len1)
        curr = dist.dist(s1, x)
        out = min(out, curr)
    end
    return out
end

function (dist::Partial{RatcliffObershelp})(s1, s2)
    ((s1 === missing) | (s2 === missing)) && return missing
    s1, s2 = reorder(s1, s2)
    len1, len2 = length(s1), length(s2)
    len1 == len2 && return dist.dist(s1, s2)
    out = 1.0
    for r in matching_blocks(s1, s2)
        # Make sure the substring of s2 has length len1
        s2_start = r[2] - r[1] + 1
        s2_end = s2_start + len1 - 1
        if s2_start < 1
            s2_end += 1 - s2_start
            s2_start += 1 - s2_start
        elseif s2_end > len2
            s2_start += len2 - s2_end
            s2_end += len2 - s2_end
        end
        curr = dist.dist(s1, _slice(s2, s2_start - 1, s2_end))
        out = min(out, curr)
    end
    return out
end

"""
   TokenSort(dist)

Creates the `TokenSort{dist}` distance.

`TokenSort{dist}` returns the distance between strings after reording words alphabetically.
See http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/

It is only defined on AbstractStrings.

### Examples
```julia-repl
julia> s1 = "New York Mets vs Atlanta Braves"
julia> s1 = "New York Mets vs Atlanta Braves"
julia> s2 = "Atlanta Braves vs New York Mets"
julia> TokenSort(RatcliffObershelp())(s1, s2)
0.0
```
"""
struct TokenSort{S <: SemiMetric} <: SemiMetric
    dist::S
end

function (dist::TokenSort)(s1::Union{AbstractString, Missing}, s2::Union{AbstractString, Missing})
    ((s1 === missing) | (s2 === missing)) && return missing
    s1 = join(sort!(split(s1)), " ")
    s2 = join(sort!(split(s2)), " ")
    out = dist.dist(s1, s2)
end

"""
   TokenSet(dist)

Creates the `TokenSet{dist}` distance.

`TokenSet{dist}` returns the minimum the distances between:
[SORTED_INTERSECTION]
[SORTED_INTERSECTION] + [SORTED_REST_OF_STRING1]
[SORTED_INTERSECTION] + [SORTED_REST_OF_STRING2]
See: http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/

It is only defined on AbstractStrings.

### Examples
```julia-repl
julia> s1 = "New York Mets vs Atlanta"
julia> s2 = "Atlanta Braves vs New York Mets"
julia> TokenSet(RatcliffObershelp())(s1, s2)
0.0
```
"""
struct TokenSet{S <: SemiMetric} <: SemiMetric
    dist::S
end

function (dist::TokenSet)(s1::Union{AbstractString, Missing}, s2::Union{AbstractString, Missing})
    ((s1 === missing) | (s2 === missing)) && return missing
    v1 = unique!(sort!(split(s1)))
    v2 = unique!(sort!(split(s2)))
    v0 = intersect(v1, v2)
    s0 = join(v0, " ")
    s1 = join(v1, " ")
    s2 = join(v2, " ")
    isempty(s0) && return dist.dist(s1, s2)
    score_01 = dist.dist(s0, s1)
    score_02 = dist.dist(s0, s2)
    score_12 = dist.dist(s1, s2)
    min(score_01, score_02, score_12)
end


text:
using Infiltrator
println(2+2)
@infiltrate

"success"


text:
### Types Used with Pose Graphs ###
### Ted Steiner ###
### September 2014 ###

abstract Pose
abstract Measurement
abstract Factor

# 2D Pose
type Pose2D <: Pose
    x
    y
    theta
end

# Measurement between 2D poses
type Meas2D <: Measurement
    dx::Float64
    dy::Float64
    dT::Float64
end

# Values of sqrt information matrix
type Inf2D
    ff      # forward-forward
    fs
    fr
    ss      # sideward-sideward
    sr
    rr      # rotate-rotate
end

# Data required for BetweenFactor (Pose2d_Pose2d_Factor) in iSAM
type BetweenFactor <: Factor
    s::Int
    t::Int
    meas::Meas2D
    info::Inf2D
    nodeS::Int
    nodeT::Int
end

# Data required for a Pose2d_Factor in iSAM
type PriorFactor <: Factor
    pose::Int
    meas::Meas2D
    info::Inf2D
    node::Int
end

# Data required to generate a posegraph from existing measurements
type PosegraphData
    waypoints::Vector{Int}      # Sequence of node IDs
    route::Vector{Int}          # Sequence of node IDs comprising route between waypoints
    poses::Vector{Pose2D}       # Truth poses
    odometry_factors::Vector{BetweenFactor}     # Odometry measurement factors
    loop_factors::Dict{(Int,Int),BetweenFactor} # Loop-closure measurement factors
    prior_meas::Dict{Int,Meas2D} # Prior measurements to first instance of each anchor in route
    data::Dict{String,Any}      # Any additional information can be stored here
end

### Dijkstra visitors, for use with Graphs.jl
# Terminate when any listed node is found
type EndWhenAnyNode <: Graphs.AbstractDijkstraVisitor
    n::Array{Graphs.KeyVertex{Int}}    # Nodes to find
end

function Graphs.include_vertex!(visitor::EndWhenAnyNode,u,v,d)
    !(v in visitor.n)
end


text:
include("plot_match.jl")

@recipe function plot(discrete_wave::DiscretePlaneWaveMode{T};
        hankel_indexes = 0:discrete_wave.basis_order,
        apply = real) where T<:AbstractFloat

    ho = discrete_wave.basis_order

    for n in hankel_indexes

        apply_field = apply.(discrete_wave.amplitudes[:,n+ho+1,1])

        @series begin
            label --> "$apply Hankel = $n"
            seriestype --> :scatter
            (discrete_wave.x, apply_field)
        end
    end
end

@recipe function plot(wave_effs::Vector{w}) where w <: AbstractWaveMode
    k_effs = [w.wavenumber for w in wave_effs]
    maxamp = maximum(norm(w.eigenvectors) for w in wave_effs)

    alphas = map(wave_effs) do w
        norm(w.eigenvectors)/maxamp
    end

    @series begin
        ylims --> (0,Inf)
        xguide --> "Re k_eff"
        yguide --> "Im k_eff"
        seriestype --> :scatter
        label --> ""
        markerstrokealpha --> 0.2
        # markercolor --> :blue
        markeralpha --> alphas
        k_effs
    end
end

@recipe function plot(x::AbstractVector{T}, wave_eff::EffectivePlaneWaveMode{T}) where T<:AbstractFloat
    @series begin
        (x, [wave_eff])
    end
end

@recipe function plot(x::AbstractVector, wave_effs::Vector{E};
        halfspace = Halfspace((Dim==2) ? [-one(T),zero(T)] : [zeros(T,Dim-1);-one(T)]),
        hankel_indexes = 0:wave_effs[1].basis_order,
        apply = real) where E<:EffectivePlaneWaveMode{T,Dim} where {T,Dim}

    avg_wave_eff = DiscretePlaneWaveMode(x, wave_effs, halfspace)
    ho = avg_wave_eff.basis_order

    for n in hankel_indexes
        apply_field = apply.(avg_wave_eff.amplitudes[:,n+ho+1,1])

        @series begin
            label --> "$apply Hankel = $n"
            (x, apply_field)
        end
    end
end

max_stars_repo_name: arturgower/EffectiveWaves


text:
    color_name()= executor(data["en"]["faker"]["color"]["allcolor"])
    safe_color_name()= executor(data["en"]["faker"]["color"]["name"])
    hex_color()= executor(data["en"]["faker"]["color"]["code"])

    rgb_color_list()="($(rand(1:255)),$(rand(1:255)),$(rand(1:255)))"
    rgb_color()="$(rand(1:255)),$(rand(1:255)),$(rand(1:255))"
    rgb_css_color()="rgb($(rand(1:255)),$(rand(1:255)),$(rand(1:255)))"


text:
##Univariate kerneldensity and regression
using Distributions
Random.seed!(2017);

linreg(x, y) = hcat(fill!(similar(x), 1), x) \ y

x=rand(Normal(10), 500)
xeval=range(minimum(x), stop=maximum(x), length=100)
h = bwlscv(x, gaussiankernel)
@test h>0
denvalues=kerneldensity(x, xeval=xeval)
@test all(denvalues .>= 0)

y=2 .* x.^2 .+ rand(Normal(0,5),500)
b0, b1 = linreg(x, y)
regfit = b0 .+ b1 .* x
yfit0=localconstant(x, y, xeval=x)
@test sum(abs2, regfit .- y) > sum(abs2, yfit0.-y)

yfit1=locallinear(x, y, xeval=x)
@test sum(abs2, regfit .- y) > sum(abs2, yfit1.-y)

yfit0=npr(x, y, xeval=x, reg=localconstant)
@test sum(abs2, regfit .- y) > sum(abs2, yfit0.-y)

yfit1=npr(x, y, xeval=x, reg=locallinear)
@test sum(abs2, regfit .- y) > sum(abs2, yfit1.-y)

#confidence band
yfit1=npr(x, y, xeval=xeval, reg=locallinear)
cb=bootstrapCB(x, y, xeval=xeval)
@test mean(vec(cb[1,:]) .<= yfit1 .<= vec(cb[2,:])) > .8

#multivariate density estimation
x = rand(Normal(10), 500, 3)
denvalues = kerneldensity(x, h = [1.0, 1.0, 1.0])
@test all(denvalues .>= 0)

#multivariate regression
x = rand(Normal(10), 500, 3)
y = x * ones(3) .+ x.^2 *ones(3)
yfit1 = localconstant(x, y, xeval=x)
regfit = x*inv(x'*x)*x'*y
@test sum(abs2, regfit .- y) > sum(abs2, yfit1.-y)

###Bounded gamma kernel density and regression
x = rand(Gamma(4,2), 500)
xeval = range(0.01, stop=20, length=100)
h = bwlscv(x, gammakernel)
@test h>0
denvalues = kerneldensity(x, xeval=xeval, kernel=gammakernel, lb=0.0)
@test all(denvalues .> 0)

y=2 .* x.^2 + x.*rand(Normal(0, 5), 500)
b0, b1 = linreg(x, y)
regfit = b0 .+ b1 .* x
yfit0=npr(x, y, xeval=x, reg=localconstant, kernel=gammakernel,lb=0.0)
@test sum(abs2, regfit .- y) > sum(abs2, yfit0.-y)

yfit1=npr(x, y, xeval=x, reg=locallinear, kernel=gammakernel, lb=0.0)
@test sum(abs2, regfit .- y) > sum(abs2, yfit1.-y)

#bounded beta kernel density and regression
x = rand(Beta(4,2), 500) * 10
xeval = range(0, stop=10, length=100)
h = bwlscv(x./10, betakernel)
@test h>0
denvalues=kerneldensity(x, xeval=xeval, kernel=betakernel,h=h, lb=0.0,ub=10.0)
@test all(denvalues .> 0)

##Bounded local regression
y=2 .* x.^2 + x.*rand(Normal(0, 5), 500)
b0, b1 = linreg(x, y)
regfit = b0 .+ b1 .* x
yfit0=npr(x, y, xeval=x, reg=localconstant, kernel=betakernel,lb=0.0, ub=10.0)
@test sum(abs2, regfit .- y) > sum(abs2, yfit0.-y)

yfit1=npr(x, y, xeval=x, reg=locallinear, kernel=betakernel, lb=0.0,ub=10.0)
@test sum(abs2, regfit .- y) > sum(abs2, yfit1.-y)

yfit1=npr(x, y, xeval=xeval, reg=locallinear, kernel=betakernel, lb=0.0,ub=10.0)
cb=bootstrapCB(x, y, xeval=xeval,reg=locallinear, kernel=betakernel, lb=0.0,ub=10.0)
@test mean(vec(cb[1,:]) .<= yfit1 .<= vec(cb[2,:])) > .8


text: @deprecate SVGP(args...) SparseVariationalApproximation(Centered(), args...)


text:
using Test

include("Tests.jl")


text:
using Documenter
using MPIMapReduce

DocMeta.setdocmeta!(MPIMapReduce, :DocTestSetup, :(using MPIMapReduce); recursive=true)

makedocs(;
    modules = [MPIMapReduce],
    authors = "Jishnu Bhattacharya",
    repo = "https://github.com/jishnub/MPIMapReduce.jl/blob/{commit}{path}#L{line}",
    sitename = "MPIMapReduce.jl",
    format = Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://jishnub.github.io/MPIMapReduce.jl",
        assets=String[],
    ),
    pages=[
        "MPIMapReduce" => "index.md",
        "Examples" => "examples.md",
        "Reference" => "api.md",
    ],
)

deploydocs(;
    repo="github.com/jishnub/MPIMapReduce.jl",
)


text:
using StaticArrays
using MieSeries
using Plots

a = 5          # sphere radius
n_terms = 80   # number of terms in Mie series
k = 5          # wavenumber
point = SVector(3.0, -5.0, 6.0)

Ex_terms, Ey_terms, Ez_terms = MieSeries.mieseries_debug(point; k, a, n_terms)
Ex_abs, Ey_abs, Ez_abs = abs.(Ex_terms), abs.(Ey_terms), abs.(Ez_terms)

plot(Ex_abs, yscale=:log10, label="Ex")
plot!(Ey_abs, label="Ey")
plot!(Ez_abs, label="Ez")

max_stars_repo_name: Riarrieta/MieSeries

max_issues_repo_name: Riarrieta/MieSeries

max_forks_repo_name: Riarrieta/MieSeries


text:
#=

Testing of utilities.jl

=#

disp = false

using ImagePhaseCongruency, Test, TestImages

if disp
    using PyPlot
end

println("Testing utilities.jl...")

function mypause()
    println("Hit return to continue ")
    a = readline()
    return nothing
end

if disp; PyPlot.set_cmap(PyPlot.ColorMap("gray")); end

# fillnan

# replacenan

img = ones(30,30)
img[5:20,10:15] .= NaN
(newimg, mask) = replacenan(img)
(newimg, mask) = replacenan(img, 2)
if disp; imshow(newimg); title("Filled image"); mypause(); end
if disp; imshow(mask); title("non-NaN regions"); mypause(); end

# hysthresh
img = zeros(30,30)
img[3:25,15] .= 5
img[8:12,15] .= 10
img[15:20,15] .= 10
img[15,15] = 20

bw = hysthresh(img, 8, 20)
if disp; imshow(img); title("Unthresholded image"); mypause(); end
if disp; imshow(bw); title("Thresholded image"); mypause(); end

max_stars_repo_name: peterkovesi/ImagePhaseCongruency

max_issues_repo_name: peterkovesi/ImagePhaseCongruency

max_forks_repo_name: peterkovesi/ImagePhaseCongruency


text:
# A few small new methods:
Base.size(U::RCholesky{T}) where{T} = (U.idxs[end][end], U.idxs[end][end])
LinearAlgebra.adjoint(U::RCholesky{T}) where{T} = Adjoint{T,RCholesky{T}}(U)
LinearAlgebra.transpose(U::RCholesky{T}) where{T} = Adjoint{T,RCholesky{T}}(U)
LinearAlgebra.logdet(U::RCholesky{T}) where{T} = sum(logdet, U.diagonals)

# TODO (cg 2022/05/30 12:15): Benchmark parallelizing and see if it helps.
function LinearAlgebra.mul!(out::Matrix{Z}, Ut::Adjoint{T,RCholesky{T}},
                            v::Matrix) where{Z,T}
  U    = Ut.parent
  bufv = Array{Z}(undef, maximum(length, U.condix)*maximum(length, U.idxs), size(v,2))
  bufm = Array{Z}(undef, maximum(length, U.idxs), size(v,2))
  for (j, cj) in enumerate(U.condix)
    out_mod_chunk = view(out, U.idxs[j], :)
    #out_mod_chunk .+= U.diagonals[j]'*view(v, U.idxs[j], :)
    mul!(out_mod_chunk, U.diagonals[j]', view(v, U.idxs[j], :))
    isempty(cj) && continue
    Bjt_chunk = U.odiagonals[j]
    bufv_v = prepare_v_buf!(bufv, v, U.idxs[cj])
    bufm_v = view(bufm, 1:length(U.idxs[j]), :)
    mul!(bufm_v, Bjt_chunk', bufv_v)
    out_mod_chunk .+= bufm_v
  end
  out
end

function Base.:*(U::Adjoint{T,RCholesky{T}}, v::Matrix) where{T}
  Z    = promote_type(T, eltype(v))
  out  = zeros(Z, size(v))
  mul!(out, U, v)
end


text:
using ResumableFunctions
using LsqFit
using Statistics
include("utils.jl")

function get_angle(adjacent, hypotenuse)
    if hypotenuse > adjacent
        a = acos(adjacent/hypotenuse)
        return a
    end
    return 0.0
end

function get_length(adjacent, a)
    return adjacent/cos(a)
end

@resumable function paths(height)
    path = ones(height)
    for i in 1:(2^height)
        @yield path
        last_pos = findlast(p -> p > 0, path)
        if typeof(last_pos) != Nothing
            path[last_pos:end] = -path[last_pos:end]
        end
    end
end

function rebin(projection, bins, s)
    if length(projection) % s != 0
        throw(DomainError("s must divide the number of projections evenly"))
    end

    other_dimension = Int64(length(projection)/s)

    reshaped = reshape(projection, other_dimension, s)
    p = mean!(ones(s)', reshaped)

    reshaped = reshape(bins, other_dimension, s)
    b = mean!(ones(s)', reshaped)
    return b', p'
end

function estimate_curve(projection, bins, angles, head, r, num_points)
    ##################################### MODEL ####################################################
    @. model(x,p) = p[1]*sin(p[2]*x-p[3])+p[4]*sin(p[2]*x-2*p[3]+p[5])

    y1 = 4.0
    y2 = 0.3*y1
    #p = [y1,k,w0,y2,ϕ]
    p0 = [1,0.2,30.0,y2,10.0]
    ################################################################################################
    #estimate angles from projection
    bin_width = bins[4] - bins[3]
    abinned = projection
    angles_est = zeros(length(abinned))
    for i = 1:length(abinned)
        a = get_angle(2*r(0.0), abinned[i])
        angles_est[i] = a
    end

    #estimate length from bins and angle
    length_est = zeros(length(abinned))
    for i = 1:length(abinned)
        l = get_length(bin_width, angles_est[i])
        length_est[i] = l
    end

    #get the bin ends where it seems we're looking at complete tail
    end1, end2 = get_projection_ends(abinned, r)

    angles_est = angles_est[end1:end2]
    length_est = length_est[end1:end2]

    global best_fit = Inf
    global best_curve = zeros(length(angles),2)
    for signs in paths(length(angles_est))
        cks = Array{Complex{Float64},1}(undef,length(angles_est)+1)
        angles_est_signed = angles_est.*signs

        for i = 1:length(angles_est)
            a, l = angles_est_signed[i], length_est[i]
            cks[i+1] = l*ℯ^(im*a)
        end
        cks[1] = head[1]+head[2]*im

        angles_1 = angle.(cks[2:end])
        angles_2 = angle.(cks[1:end-1])

        difference = abs.(angles_1-angles_2)

        if any(d -> d > π/3, difference)
            continue
        end

        xy = get_xy(cks)[2:end,:]

        xdata = xy[:,1]
        ydata = xy[:,2]

        fit = curve_fit(model, xdata, ydata, p0)
        fit_curve = cat(xy[:,1], model(xy[:,1], coef(fit)), dims=2)
        outline = get_outline(fit_curve, r)[1]

        #Define angles and bin  width for forward projection
        projection_fit = parallel_forward(outline,angles,bins)
        end1, end2 = get_projection_ends(projection_fit, r)
        if typeof(end1) != Nothing && typeof(end2) != Nothing
            cost = sum(fit.resid.^2)/length(fit.resid) + sum((projection_fit[end1:end2]-projection[end1:end2]).^2)/length(projection)
            if cost < best_fit
                global best_fit = cost
                global best_curve =xy
            end
        end
    end

    #get curve as fit by model
    fit = curve_fit(model, best_curve[:,1], best_curve[:,2], p0)

    st, en = best_curve[1,1], best_curve[end,1]
    xs = range(st, en, length=num_points)
    xy = cat(xs, model(xs, coef(fit)), dims=2)

    return xy
end

max_stars_repo_name: QPImaging/xfromProjections

max_issues_repo_name: QPImaging/xfromProjections

max_forks_repo_name: QPImaging/xfromProjections


text:
am_demodulation(y2) = abs.(DSP.Util.hilbert(y2))

function gen_sync_frame(Fs2,sync_frequency)
    nbands = 7
    sync_frame = Vector{Vector{Int}}(undef,length(sync_frequency))

    i = 1;
    pulse_len = round(Int,Fs2/(2*sync_frequency[i]))
    # 7 pulses followed by silence
    sync_frame[i] = vcat(
        fill(-1,pulse_len),
        repeat(vcat(fill(-1,pulse_len),
                    fill(1,pulse_len)),
               nbands),
        fill(-1,5*pulse_len ))

    i = 2;
    pulse_len_on  = round(Int,3/5 * Fs2/(sync_frequency[i]))
    pulse_len_off = round(Int,2/5 * Fs2/(sync_frequency[i]))
    # 7 pulses followed by silence
    sync_frame[i] = vcat(
        fill(-1,pulse_len_off ),
        repeat(vcat(fill(-1,pulse_len_off),
                    fill(1,pulse_len_on)),
               nbands),
        fill(-1,pulse_len_on )) # the last -1 is as long a the previous +1

    return sync_frame
end

function find_sync(y_demod,sync_frame,frame_len)
    # minimum and maximum distance between sync frames
    mindistance = (8*frame_len) ÷ 10
    maxdistance = (12*frame_len) ÷ 10

    conv_sync_full = DSP.conv(y_demod,reverse(sync_frame));

    # skip first incomplete convolution
    conv_sync = @view conv_sync_full[length(sync_frame):end]

    # overall strongest sync frame
    # index0 marks the index of beginning the sync frame
    index0 = findmax(conv_sync)[2];

    # look for all sync frames after the strongest sync frame
    index = index0
    after_index = Int[]
    while index + maxdistance <= length(conv_sync)
        i = findmax(conv_sync[index .+ (mindistance:maxdistance)])[2] + index+mindistance-1
        push!(after_index,i)
        index = i
    end

    # look for all sync frames before the strongest sync frame
    index = index0
    before_index = Int[]
    while index - maxdistance >= 1
        i = findmax(conv_sync[index-maxdistance : index-mindistance])[2] + index-maxdistance-1
        push!(before_index,i)
        index = i
    end

    sync_frame_index = vcat(reverse(before_index),[index0],after_index)
    return sync_frame_index
end

function mark_sync(y_demod,sync_frame,frame_len)
    sync_frame_index = find_sync(y_demod,sync_frame,frame_len)
    tt = zeros(size(y_demod))
    tt[sync_frame_index] .= 1;
    return tt
end

function reshape_signal(s,frame_len)
    nscan = length(s) ÷ frame_len
    return reshape(s[1:frame_len*nscan],(frame_len,nscan))
end

"""
    datatime, (channelA,channelB), data = decode(y,Fs)

Decode the APT image in a time series `y` defined at a frequency `Fs` (in Hz).
`datatime` is the time in seconds counting from the beginning of the recording.
`channelA` and `channelB` are arrays representing the data from the two
different channels (A and B). `data` is raw data as an array including channel A and B,
as well sync markers and telemetry.

# Example
```julia
using FileIO
using APTDecoder

wavname = "gqrx_20190804_141523_137100000.wav"
y,Fs,nbits,opt = load(wavname)
datatime,(channelA,channelB),data = APTDecode.decode(y,Fs)

```
"""
function decode(y,Fs)
    # Fs2 should be a multiple of 4160 Hz and least 8320 Hz
    # 4160 is the least common multiple of 1040 and 832 (the frequency of the
    # sync A and B pulses)

    Fs2 = 3*4160

    # low and high frequency for the band-pass filter (in Hz)
    wpass = (400., 4400.)

    responsetype = DSP.Filters.Bandpass(wpass[1],wpass[2],fs = Fs);
    designmethod = DSP.Filters.Butterworth(6)

    #figure(4); clf(); psd(y[:,1],Fs = Fs, NFFT=1024)
    yf = DSP.filt(DSP.digitalfilter(responsetype, designmethod), y[:,1]);
    #figure(4); clf(); psd(yf[:,1],Fs = Fs, NFFT=1024)

    y2 = DSP.Filters.resample(yf, float(Fs2) / float(Fs) )

    # AM demodulation using the Hilbert Transform
    y_demod = am_demodulation(y2);

    # generate the syncronization frame at the frequency Fs2
    sync_frame = gen_sync_frame(Fs2,sync_frequency)

    # length of a frame
    frame_len = round(Int,Fs2/scans_per_seconds)

    sync_frame_index = find_sync(y_demod,sync_frame[1],frame_len)

    # complete frames
    complete_frame_index = Int[]
    for i = 1:length(sync_frame_index)
        if sync_frame_index[i]+frame_len-1 <= length(y_demod)
            push!(complete_frame_index,sync_frame_index[i])
        end
    end

    data = zeros(length(complete_frame_index),frame_len)
    datatime = (complete_frame_index .- 1) / Fs2

    for i = 1:length(complete_frame_index)
        data[i,:] = y_demod[complete_frame_index[i] : complete_frame_index[i]+frame_len-1]
    end

    # channel A and B
    channels = (view(data,:,259:2985), view(data,:,3380:6103))
    return datatime,channels,data
end

max_stars_repo_name: rctngle/OpenWeatherDecoder

max_issues_repo_name: rctngle/OpenWeatherDecoder

max_forks_repo_name: rctngle/OpenWeatherDecoder


text:
using OrdinaryDiffEq, OpenQuantumTools, Plots
using LaTeXStrings

function err_bound(tf, cfun)
    tsb, esb = τ_SB(cfun)
    tb, eb = τ_B(cfun, tf, tsb)
    tb / tsb
end

fc = 4; T =12; tf = 1000;
ηlist = log_uniform(1e-3, 5, 1000)
err_ratio = []
err_clist = []
err_klist = []
for η in ηlist
    bath = Ohmic(η, fc, T)
    cfun = (x)->correlation(x, bath)
    pfun = (x)->polaron_correlation(x, bath)
    err_c = err_bound(tf, cfun)
    err_k = err_bound(tf, pfun)
    push!(err_clist, err_c)
    push!(err_klist, err_k)
    push!(err_ratio, err_c/err_k)
end
idx = findfirst((x)->x>=1, err_ratio)
plot(ηlist, err_ratio, xscale=:log10, yscale=:log10, label="", linewidth=2)
vline!([ηlist[idx]], label="", linestyle=:dash, linewidth=2)
annotate!([(0.5, 1.0, Plots.text("polaron")), (0.01, 1.0, Plots.text("Redfield"))])
xlabel!(L"\eta g^2")
ylabel!("R")

plot(ηlist, err_clist, xscale=:log10, yscale=:log10, label="Redfield", linewidth=2)
plot!(ηlist, err_klist, xscale=:log10, yscale=:log10, label="PTRE", linewidth=2)
xlabel!(L"\eta g^2")
ylabel!("error")

    # assume ϵ = 1
    const Δ = 0.1
    # define the Ohmic bath in polaron transformed frame
    η = 0.5; bath = Ohmic(η, fc, T)
    K(t1, t2) = Δ^2 * polaron_correlation(t1-t2, bath)
    cfun = [nothing K; K nothing]
    pbath = CorrelatedBath(((1,2),(2,1)), correlation=cfun)
    # define coupling as σ+ and σ- operators
    σp = [0 1;0 0.0im]; σm = [0 0;1 0.0im]
    coupling = ConstantCouplings([σp, σm])
    # manually define the unitary operator
    U(t) = exp(-2.0im * π * σz * t)
    H = DenseHamiltonian([(s)->1.0], [σz])
    u0 = PauliVec[3][1]
    annealing = Annealing(H, u0, coupling = coupling, bath = pbath)
    tf = 100
    sol_ptre = solve_redfield(annealing, tf, U, alg=Tsit5(), Ta=2, reltol=1e-5)
    pop_e = [real(s[1,1]) for s in sol_ptre.u]
    plot(sol_ptre.t, pop_e, xlabel=L"t\ (\mathrm{ns})", ylabel=L"P_0(t)", label="", linewidth = 2)

H = DenseHamiltonian([(s)->1.0], [σz+0.1*σx])
coupling = ConstantCouplings(["Z"])
annealing = Annealing(H, u0, coupling = coupling, bath = bath)
tf = 100
sol_redfield = solve_redfield(annealing, tf, U, alg=Tsit5(), Ta=40, reltol=1e-5, callback=PositivityCheckCallback())
pop_e_redfield = [real(s[1,1]) for s in sol_redfield.u]
plot(sol_ptre.t, pop_e, xlabel=L"t\ (\mathrm{ns})", ylabel=L"P_0(t)", label="PTRE", linewidth = 2, legend = :right)
plot!(sol_redfield.t, pop_e_redfield, xlabel=L"t\ (\mathrm{ns})", ylabel=L"P_0(t)", label="Redfield", linewidth = 2)

t_axis = range(0, 5, length=100)
off_diag_ptre = [abs(sol_ptre(t)[1,2]) for t in t_axis]
off_diag_redfield = [abs(sol_redfield(t)[1,2]) for t in t_axis]
plot(t_axis, off_diag_ptre, xlabel=L"t\ (\mathrm{ns})", ylabel=L"\lvert \rho_{12} \rvert|(t)", label="PTRE", linewidth = 2, legend=:right)
plot!(t_axis, off_diag_redfield, xlabel=L"t\ (\mathrm{ns})", ylabel=L"|\rho_{12}(t)|", label="Redfield", linewidth = 2)


text:
###############################
##     Theoretical times     ##
###############################

"""
Computes the theoretical waiting times based on the results in Theorems 1 2 and 3
"""
# Priority to H
function theory_prioH(λ_E, λ_H, p_E, p_H)
  if λ_H < λ_E
      return log(λ_E/(λ_E - λ_H))/(λ_H * p_E * p_H)
  elseif λ_H > λ_E
      return log(2*λ_H/(λ_E + λ_H))/(λ_H * p_H^2)
  else
    error("Cannot find theory for λ_E, = $λ_E, λ_H = $λ_H,")
  end
end

# Priority to E
function theory_prioE_lower(λ_E, λ_H, p_E, p_H)
  return theory_prioH(λ_E, λ_H, p_E, p_H)
end

function theory_prioE_upper(λ_E, λ_H, p_E, p_H)
  if λ_H < λ_E
      return log(2*λ_E/(λ_E - λ_H))/(p_E * p_H*λ_H)
  elseif λ_H > λ_E
      return log(2*λ_H/(λ_E + λ_H))/(λ_H * p_H^2)
  else
      error("Cannot find theoretical upper bound for λ_E, = $λ_E, λ_H = $λ_H,")
  end
end

function conj_prioE(λ_E, λ_H, p_E, p_H)
  if λ_H < λ_E
      return log((λ_E + λ_H)/(λ_E - λ_H))/(p_E * p_H * λ_H)
  elseif λ_H > λ_E
      return log(2*λ_H/(λ_E + λ_H))/(λ_H * p_H^2)
  else
    error("Cannot find conjecture for λ_E, = $λ_E, λ_H = $λ_H,")
  end
end

# Chains
function theory_chains_upper(λ_E, λ_H, p_E, p_H, d)
    return log(λ_H/(λ_E * (1 - (1 - p_E)^d))+1)/(p_H*λ_H)
end

function conj_chains(λ_E, λ_H, p_E, p_H, d)
    return log((λ_H + λ_E )/(λ_H * (1 - (1 - p_H)^(d)) + λ_E))/(p_H*λ_H)
end


text:
##
# file sorting demo
# julia -p 5
# > require("filesort.jl")
# > filesort("test.csv", 20)

using Blocks
using Compat
using Base.FS

tmpf(workfile::AbstractString) = string(myid())*"_"*string(@compat(Int(time_ns())))*"_"*workfile

function merge_blocks(workfile::AbstractString, inio::Array)
    (length(inio) == 1) && return inio[1]
    out = tmpf(workfile)
    merge_streams(open(out, "w"), map(x->open(x), inio)...)
    map(x->rm(x), inio)
    out
end
function merge_streams(out::IO, _inio::IO...)
    inio = [_inio...]
    nio = length(inio)
    lines = map(x->readline(x), inio)
    while(length(inio) > 0)
        (str,pos) = findmin(lines)
        print(out, str)
        if eof(inio[pos])
            close(splice!(inio, pos))
            splice!(lines, pos)
        else
            lines[pos] = readline(inio[pos])
        end
    end
    close(out)
    return
end

function sort_step_2(workfile::AbstractString, block_files::Array, nway::Int=0)
    # make pairs of files for pmap
    (0 == nway) && (nway = length(block_files))
    npairs = @compat(Int(ceil(length(block_files)/nway)))
    mb = cell(npairs)
    for idx in 1:npairs
        st = (idx-1)*nway+1
        ed = min(length(block_files), st+nway-1)
        mb[idx] = block_files[st:ed]
    end
    block_files = pmap(x->merge_blocks(workfile, x), mb)
    println("\tmerge -> $(length(block_files))...")
    #for fname in block_files
    #    println("\t$(fname)")
    #end
    block_files
end

function sort_step_1(workfile::AbstractString, n::Int)
    function as_tempfile(lines)
        fname = tmpf(workfile)
        open(fname, "w") do io
            for c in lines
                write(io, c)
            end
        end
        fname
    end
    b = Block(File(workfile), n) |> as_io |> as_recordio |> as_lines |> sort |> as_tempfile
    block_files = pmap(x->x, b)
    println("\tblocks sorted -> $(length(block_files))...")
    for fname in block_files
        println("\t$(fname)")
    end
    block_files
end

function filesort(workfile::AbstractString, n::Int)
    block_files = sort_step_1(workfile, n)
    while(length(block_files) > 1)
        block_files = sort_step_2(workfile, block_files)
    end
    block_files[1]
end

if length(ARGS) > 0
    println(ARGS)
end


text:
#intended usage index 1,2,3 = x,y,z
immutable Vec{Cardinality, T}
 	v::Array{T, 1}
	function Vec(v::Array{T,1})
		Cardinality == length(v)
		new(v)
	end
end
#typealias Vec32{Cardinality} Vec{Cardinality, Float32}

Vec{T}(v::Array{T, 1}) = Vec{length(v), T}(v)
Vec(v::Real...) = Vec([v...])

Vec32{T <: Real}(v::Array{T,1}) = Vec{length(v), Float32}(float32(v))
Vec32(v::Real...) = Vec32([v...])

@show Vec([1,2])
@show Vec(1,2,3,4.0)
@show Vec([1,2,3.0])

@show Vec32(1,2,3.0)
@show Vec32([1,2,3.0])

max_stars_repo_name: dadarakt/jamon

max_issues_repo_name: dadarakt/jamon

max_forks_repo_name: dadarakt/jamon


text:
### A Pluto.jl notebook ###
# v0.17.1

using Markdown
using InteractiveUtils

# ╔═╡ 086bff3e-6324-11eb-31cc-995e82c7b175
#hide
#for sharing environments between notebooks
begin
import Pkg
# activate the shared project environment
Pkg.activate(Base.current_project())
include("../src/Documenter.jl")
end

# ╔═╡ 1bed62f7-676d-49dc-9ad6-9fe9972513ce
using PlutoTest

# ╔═╡ cc24b4ce-0eb1-11eb-38fb-55530877867a
#hide
include("../src/Export.jl")

# ╔═╡ 70e301b0-0c7e-11eb-119a-f3a0a39ff933
md"You need to have something to capture the output. This is useful when you want to have examples or capture test output in the docs.

I think the simplest way to do this is to do the following👇

* Take a notebook.
* Create a fake server, fake client etc.
* Run the notebook cell by cell and update the notebook with the output.

This is taken straight from Pluto's webserver code"

# ╔═╡ 195b8ac0-1cff-11eb-1989-07908c56d612
#export
import Pluto: Notebook, Cell, Configuration, Notebook, ServerSession, ClientSession, update_run!, Cell, WorkspaceManager

# ╔═╡ fbe459e0-1cef-11eb-0c03-ffcf886af3f3
#export
import Pluto.PlutoRunner

# ╔═╡ fb233ac0-6322-11eb-0a09-b9ad1391f893
md"## executecode "

# ╔═╡ bcb831c0-0e07-11eb-3958-4b88811cfae8
#export
#TODO:how to clean the child processes spawned by this process.
#also, executing the notebook this way prevents the tests to be
#executed in the notebook

"""
> executecode(nb::Notebook)--> This function does the following
> * Take a notebook.
> * Create a fake server, fake client etc.
> * Run the notebook cell by cell and update the notebook with the output.
"""
function executecode(nb::Notebook)
	fakeserver=ServerSession()
	fakeclient = ClientSession(:fake, nothing)
	fakeserver.connected_clients[fakeclient.id] = fakeclient

	fakeclient.connected_notebook = nb

	update_run!(fakeserver, nb, nb.cells)
	nb
end

# ╔═╡ 6660810b-b06c-4c31-bc99-90caefca5ae0
    notebook = Notebook([
        Cell("x = 1"),
        Cell("y = x"),
        Cell("f(x) = x + y"),
        Cell("f(4)"),

        Cell("""begin
            g(a) = x
            g(a,b) = y
        end"""),
        Cell("g(6) + g(6,6)"),

        Cell("import Distributed"),
        Cell("Distributed.myid()"),
    ])

# ╔═╡ 1543eba9-4050-47c2-b898-94b9a86c9ae4
nb=executecode(notebook)

# ╔═╡ cb3eb874-d4fb-4097-9d77-92b07f3f5ec7
#noop
@test nb.cells[4].output.body == "5"

# ╔═╡ 2b204642-6324-11eb-05ac-8d9484a0c19e
Documenter.showdoc(executecode)

# ╔═╡ 63f13d10-0e0d-11eb-1442-f93b1f9beed9
#hide
Export.notebook2script(joinpath("..", "nbs"), joinpath("..", "src"))

# ╔═╡ Cell order:
# ╠═70e301b0-0c7e-11eb-119a-f3a0a39ff933
# ╠═195b8ac0-1cff-11eb-1989-07908c56d612
# ╠═fbe459e0-1cef-11eb-0c03-ffcf886af3f3
# ╠═cc24b4ce-0eb1-11eb-38fb-55530877867a
# ╠═086bff3e-6324-11eb-31cc-995e82c7b175
# ╠═1bed62f7-676d-49dc-9ad6-9fe9972513ce
# ╠═fb233ac0-6322-11eb-0a09-b9ad1391f893
# ╠═bcb831c0-0e07-11eb-3958-4b88811cfae8
# ╠═6660810b-b06c-4c31-bc99-90caefca5ae0
# ╠═1543eba9-4050-47c2-b898-94b9a86c9ae4
# ╠═cb3eb874-d4fb-4097-9d77-92b07f3f5ec7
# ╠═2b204642-6324-11eb-05ac-8d9484a0c19e
# ╠═63f13d10-0e0d-11eb-1442-f93b1f9beed9


text:
module TestMI
using Test, PlmDCA
MI = PlmDCA.mutualinfo("test.fasta",pseudocount=0)
@test MI == zero(MI)
@test true
printstyled("All TestMI passed!\n",color=:light_green,bold=true)
end

max_stars_repo_name: giovact/PlmDCA

max_issues_repo_name: giovact/PlmDCA

max_forks_repo_name: giovact/PlmDCA


text:
function polymorphism_main()
    uf = UnionFinder(convert(UInt16, 2))
    union!(uf, convert(UInt16, 1), convert(UInt16, 2))
    reset!(uf)
    @test UInt16 == typeof(find!(uf, convert(UInt16, 1)))
    @test UInt16 == typeof(size!(uf, convert(UInt16, 1)))

    @test_throws MethodError union!(uf, convert(Int64, 1), convert(Int64, 2))
    @test_throws MethodError union!(uf, convert(UInt16, 1), convert(Int64, 2))
    @test_throws MethodError union!(uf, convert(Int64, 1), convert(UInt16, 2))
    @test_throws MethodError find!(uf, convert(Int64, 1))
    @test_throws MethodError size!(uf, convert(Int64, 1))

    cf = CompressedFinder(uf)
    @test UInt16 == typeof(find(cf, convert(UInt16, 1)))
    @test UInt16 == typeof(groups(cf))
    @test_throws MethodError find(cf, convert(Int64, 1))
end

polymorphism_main()


text:
module DoubleDicts

import MathOptInterface

const MOI = MathOptInterface
const MOIU = MOI.Utilities
const CI = MOI.ConstraintIndex
const AD = AbstractDict

const DataTypePair = Tuple{DataType, DataType}

abstract type AbstractDoubleDict{K, V, IK, DI<:AD{IK}, DO<:AD{K, DI}} <: AD{CI, V} end

"""
    DoubleDict{V}

Optimized dictionary to map `ConstraintIndex` (`CI`) to values of type `V`.
Works as a `AbstractDict{CI, V}` with minimal differences.
Note that `CI` is not a concrete type, opposed to `CI{MOI.SingleVariable, MOI.Integers}`,
which is a concrete type.

When optimal performance or type stability is required its possible to obtain a
fully type stable dictionary with values of type `V` and keys of type
`CI{MOI.SingleVariable, MOI.Integers}` from the dictionary `dict`, for instance:

    ``inner = dict[MOI.SingleVariable, MOI.Integers]``
"""
struct DoubleDict{V, DI, DO} <: AbstractDoubleDict{DataTypePair, V, Int64, DI, DO}
    dict::DO
    function DoubleDict{V}() where {V}
        return new{V, Dict{Int64, V}, Dict{DataTypePair, Dict{Int64, V}}}(
            Dict{DataTypePair, Dict{Int64, V}}()
        )
    end
end

"""
    IndexDoubleDict

Specialized version of `DoubleDict` in which keys and values are of type
`ConstraintIndex` (`CI`).

This is an optimized dictionary to map `ConstraintIndex` (`CI`) to values of type `CI`.
Works as a `AbstractDict{CI, V}` with minimal differences.
Note that `CI` is not a concrete type, opposed to `CI{MOI.SingleVariable, MOI.Integers}`,
which is a concrete type.

When optimal performance or type stability is required its possible to obtain a
fully type stable dictionary with values of type
`CI{MOI.SingleVariable, MOI.Integers}` and keys of type
`CI{MOI.SingleVariable, MOI.Integers}` from the dictionary `dict`, for instance:

    ``inner = dict[MOI.SingleVariable, MOI.Integers]``
"""
struct IndexDoubleDict{DI, DO} <: AbstractDoubleDict{DataTypePair, CI, Int64, DI, DO}
    dict::DO
    function IndexDoubleDict()
        return new{Dict{Int64, Int64}, Dict{DataTypePair, Dict{Int64, Int64}}}(
            Dict{DataTypePair, Dict{Int64, Int64}}()
        )
    end
end

struct FunctionSetDoubleDict{DI, DO} <: AbstractDoubleDict{DataTypePair, Tuple, Int64, DI, DO}
    dict::DO
    function FunctionSetDoubleDict()
        return new{Dict{Int64}, Dict{DataTypePair, Dict{Int64}}}(
         Dict{DataTypePair, Dict{Int64}}()
        )
    end
end

const MainIndexDoubleDict = IndexDoubleDict{
    Dict{Int64, Int64}, Dict{Tuple{DataType, DataType}, Dict{Int64, Int64}}
}

@inline function typed_value(::DoubleDict{V}, v::V, ::Type{F}, ::Type{S})::V where {V, F, S}
    return v
end
@inline function typed_value(::IndexDoubleDict, v::Int64, ::Type{F}, ::Type{S})::CI{F,S} where {F, S}
    return CI{F,S}(v)
end
@inline function typed_value(::FunctionSetDoubleDict, v::Tuple{F,S}, ::Type{F}, ::Type{S})::Tuple{F,S} where {F, S}
    return v
end

# reversing IndexDoubleDict is ok because they map CI to CI
function MOIU._reverse_dict(
    dest::IndexDoubleDict{DI}, src::IndexDoubleDict{DI}
) where DI
    for (k, v) in src.dict
        dest.dict[k] = MOIU._reverse_dict(v)
    end
end
# reversing other double dict types is not ok because the map CI fo K
# so it wont be a double dict anymore, double dict keys are always CIs.
# We keep the default fallback

function Base.sizehint!(::AbstractDoubleDict, ::Integer)
    throw(ErrorException("sizehint!(d::DoubleDict, ::Integer) has no proper" *
    " meaning for DoubleDict, use sizehint!(d[F,S], n::Integer) " *
    "instead."))
end

function Base.length(d::AbstractDoubleDict)
    len = 0
    for inner in values(d.dict)
        len += length(inner)
    end
    return len
end

function Base.haskey(dict::AbstractDoubleDict, key::CI{F,S}) where {F,S}
    inner = get(dict.dict, (F, S), nothing)
    return inner !== nothing ? haskey(inner, key.value) : false
end

function Base.get(dict::AbstractDoubleDict, key::CI{F,S}, default) where {F,S}
    inner = get(dict.dict, (F, S), nothing)
    if inner !== nothing && haskey(inner, key.value)
        return typed_value(dict, inner[key.value], F, S)
    end
    return default
end

function Base.getindex(dict::AbstractDoubleDict, key::CI{F,S}) where {F,S}
    inner = dict.dict[(F,S)]
    k_value = key.value::Int64
    return typed_value(dict, inner[k_value], F, S)
end

function init_inner_dict(::AbstractDoubleDict{K, V}, ::Type{F}, ::Type{S}) where {K,V,F,S}
    return Dict{Int64, V}()
end
function init_inner_dict(::FunctionSetDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
    return Dict{Int64, Tuple{F,S}}()
end
function init_inner_dict(::IndexDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
    return Dict{Int64, Int64}()
end

"""
    lazy_get(dict::DoubleDict, ::Type{F}, ::Type{S})

description
"""
function lazy_get(dict::AbstractDoubleDict{K, V}, ::Type{F}, ::Type{S}) where {K,V,F,S}
    inner = get(dict.dict, (F,S), nothing)
    if inner === nothing
        return dict.dict[(F,S)] = init_inner_dict(dict, F, S)
    end
    return inner
end

function Base.setindex!(dict::AbstractDoubleDict{K, V}, value::V, key::CI{F,S}) where {K,V,F,S}
    v_value = value
    k_value = key.value::Int64
    inner = lazy_get(dict, F, S)
    inner[k_value] = v_value
    return value
end
function Base.setindex!(dict::DoubleDict{V}, value::V, key::CI{F,S}) where {V,F,S}
    v_value = value
    k_value = key.value::Int64
    inner = lazy_get(dict, F, S)::Dict{Int64, V}
    inner[k_value] = v_value
    return value
end
function Base.setindex!(dict::IndexDoubleDict, value::CI{F,S}, key::CI{F,S}) where {F,S}
    v_value = value.value::Int64
    k_value = key.value::Int64
    inner = lazy_get(dict, F, S)::Dict{Int64, Int64}
    inner[k_value] = v_value
    return value
end

function Base.empty!(d::AbstractDoubleDict)::Nothing
    Base.empty!(d.dict)
    return
end

function Base.delete!(d::AbstractDoubleDict, key::CI{F,S}) where {F,S}
    k_value = key.value::Int64
    inner = lazy_get(d, F, S)
    delete!(inner, k_value)
    return d
end

function Base.isempty(d::AbstractDoubleDict)
    if isempty(d.dict)
        return true
    end
    for val in values(d.dict)
        if !isempty(val)
            return false
        end
    end
    return true
end

function Base.values(d::IndexDoubleDict)::Vector{CI}
    out = CI[]
    for ((F, S), inner) in d.dict
        append!(out, CI{F,S}.(values(inner)))
    end
    return out
end
function Base.values(d::AbstractDoubleDict{K, V})::Vector{V} where {K, V}
    out = V[]
    for ((F, S), inner) in d.dict
        append!(out, values(inner))
    end
    return out
end

function Base.keys(d::AbstractDoubleDict)
    out = CI[]
    for ((F,S), inner) in d.dict
        append!(out, CI{F,S}.(keys(inner)))
    end
    return out
end

function Base.iterate(d::AbstractDoubleDict)
    o_next = iterate(d.dict)
    if o_next === nothing
        return nothing
    end
    (o_i, o_state) = o_next
    ((F,S), inner) = o_i
    i_next = iterate(inner)
    while i_next === nothing
        o_next = iterate(d.dict, o_state)
        if o_next === nothing
            return nothing
        end
        (o_i, o_state) = o_next
        ((F,S), inner) = o_i
        i_next = iterate(inner)
    end
    (i_i, i_state) = i_next
    return CI{F,S}(i_i[1]) => typed_value(d, i_i[2], F, S), (i_state, (o_i, o_state))
end
function Base.iterate(d::AbstractDoubleDict, state)
    (i_state, (o_i, o_state)) = state
    ((F,S), inner) = o_i
    i_next = iterate(inner, i_state)
    while i_next === nothing
        o_next = iterate(d.dict, o_state)
        if o_next === nothing
            return nothing
        end
        (o_i, o_state) = o_next
        ((F,S), inner) = o_i
        i_next = iterate(inner)
    end
    (i_i, i_state) = i_next
    return CI{F,S}(i_i[1]) => typed_value(d, i_i[2], F, S), (i_state, (o_i, o_state))
end

abstract type AbstractWithType{F, S, V, DI, DD} <: AD{CI{F,S}, V} end

"""
    WithType{D<:DoubleDict, F<:MOI.AbstractFunction, S<:MOI.AbstractSet}

Used to specialize methods and iterators for a given contraint type CI{F,S}.
"""
mutable struct WithType{F, S, V, DI, DD} <: AbstractWithType{F, S, V, DI, DD}
    dict::DD
    inner::Union{DI, Nothing}
    function WithType{F, S}(d::DD) where
        {V, DI<:AD{Int64, V}, DO<:AD{DataTypePair, DI}, DD<:DoubleDict{V, DI, DO}, F, S}
        inner = get(d.dict, (F, S), nothing)
        new{F, S, V, DI, DD}(d, inner)
    end
end
mutable struct IndexWithType{F, S, V, DI, DD} <: AbstractWithType{F, S, CI{F,S}, DI, DD}
    dict::DD
    inner::Union{DI, Nothing}
    function IndexWithType{F, S}(d::DD) where
        {DI<:AD{Int64, Int64}, DO<:AD{DataTypePair, DI}, DD<:IndexDoubleDict{DI, DO}, F, S}
        inner = get(d.dict, (F, S), nothing)::Union{DI, Nothing}
        new{F, S, CI{F,S}, DI, DD}(d, inner)
    end
end
mutable struct FunctionSetWithType{F, S, V, DI, DD} <: AbstractWithType{F, S, Tuple{F,S}, DI, DD}
    dict::DD
    inner::Union{DI, Nothing}
    function FunctionSetWithType{F, S}(d::DD) where
        {F, S, DI<:AD{Int64}, DO<:AD{DataTypePair, DI}, DD<:FunctionSetDoubleDict{DI, DO}}
        inner = get(d.dict, (F, S), nothing)::Union{DI{Tuple{F,S}}, Nothing}
        new{F, S, Tuple{F,S}, DI{Tuple{F,S}}, DD}(d, inner)
    end
end

function with_type(d::DoubleDict, ::Type{F}, ::Type{S})::WithType{F, S} where {F,S}
    return WithType{F, S}(d)::WithType{F, S}
end
function with_type(d::IndexDoubleDict, ::Type{F}, ::Type{S})::IndexWithType{F, S} where {F,S}
    return IndexWithType{F, S}(d)::IndexWithType{F, S}
end
function with_type(d::FunctionSetDoubleDict, ::Type{F}, ::Type{S})::FunctionSetWithType{F, S} where {F,S}
    return FunctionSetWithType{F, S}(d)::FunctionSetWithType{F, S}
end

function Base.getindex(d::DoubleDict, ::Type{F}, ::Type{S}) where {F,S}
    return WithType{F, S}(d)
end
function Base.getindex(d::IndexDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
    return IndexWithType{F, S}(d)
end
function Base.getindex(d::FunctionSetDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
    return FunctionSetWithType{F, S}(d)
end

@inline function typed_value(::WithType{F, S, V}, v::V)::V where {V, F, S}
    v
end
@inline function typed_value(::IndexWithType{F, S}, v::Int64)::CI{F,S} where {F, S}
    CI{F,S}(v)
end
@inline function typed_value(::FunctionSetWithType{F, S, V}, v::Tuple{F,S})::Tuple{F,S} where {V, F, S}
    v
end

function initialize_inner!(d::AbstractWithType{F, S, V, D}) where {F, S, V, D}
    d.inner = D()
end

inner_is_empty(d::AbstractWithType)::Bool = d.inner === nothing
function inner(d::AbstractWithType{F, S, V, D})::D where {F, S, V, D}
    d.inner::D
end

function Base.sizehint!(d::AbstractWithType{F, S}, n::Integer) where {F, S}
    if inner_is_empty(d)
        initialize_inner!(d)
    end
    sizehint!(inner(d), n)
end

function Base.length(d::AbstractWithType{F, S}) where {F, S}
    if inner_is_empty(d)
        return 0
    end
    return length(inner(d))
end

function Base.haskey(d::AbstractWithType{F, S}, key::CI{F,S}) where {F,S}
    if inner_is_empty(d)
        return false
    end
    return haskey(inner(d), key.value)
end

function Base.getindex(d::AbstractWithType{F, S},
    key::CI{F,S}) where {F, S}
    if inner_is_empty(d)
        throw(KeyError(key))
    end
    k_value = key.value::Int64
    return typed_value(d, inner(d)[k_value])
end

function Base.setindex!(d::WithType{F, S, V}, value::V,
    key::CI{F,S})::V where {F, S, V}
    if inner_is_empty(d)
        initialize_inner!(d)
    end
    v_value = value
    k_value = key.value::Int64
    inner(d)[k_value] = v_value
    return value
end
function Base.setindex!(d::IndexWithType{F, S, V}, value::CI{F,S},
    key::CI{F,S})::CI{F,S} where {F, S, V}
    if inner_is_empty(d)
        initialize_inner!(d)
    end
    _inner = inner(d)::Dict{Int64, Int64}
    v_value = value.value::Int64
    k_value = key.value::Int64
    _inner[k_value] = v_value
    return value
end
function Base.setindex!(d::FunctionSetWithType{F, S, V}, value::Tuple{F,S},
    key::CI{F,S})::Tuple{F,S} where {F, S, V}
    if inner_is_empty(d)
        initialize_inner!(d)
    end
    _inner = inner(d)::Dict{Int64, Tuple{F,S}}
    v_value = value
    k_value = key.value::Int64
    _inner[k_value] = v_value
    return value
end

function Base.empty!(d::AbstractWithType{F, S}) where {F,S}
    if inner_is_empty(d)
        return d
    end
    return empty!(inner(d))
end

function Base.delete!(d::AbstractWithType{F, S}, key::CI{F,S}) where {F,S}
    if inner_is_empty(d)
        return d
    end
    k_value = key.value::Int64
    delete!(inner(d), k_value)
    return d
end

function Base.isempty(d::AbstractWithType{F, S}) where {F,S}
    if inner_is_empty(d)
        return true
    end
    return isempty(inner(d))
end

function Base.values(d::WithType{F, S, V})::Vector{V} where {F, S, V}
    if inner_is_empty(d)
        return V[]
    end
    return collect(values(inner(d)))
end
function Base.values(d::IndexWithType{F, S})::Vector{CI{F,S}} where {F,S}
    if inner_is_empty(d)
        return CI{F,S}[]
    end
    return CI{F,S}.(values(inner(d)))
end
function Base.values(d::FunctionSetWithType{F, S})::Vector{Tuple{F,S}} where {F,S}
    if inner_is_empty(d)
        return Tuple{F,S}[]
    end
    return collect(values(inner(d)))
end

function Base.keys(d::AbstractWithType{F, S}) where {F,S}
    if inner_is_empty(d)
        return CI{F,S}[]
    end
    return CI{F,S}.(keys(inner(d)))
end

function Base.iterate(d::AbstractWithType{F, S}) where {F, S}
    inner = d.inner
    if inner === nothing
        return nothing
    else
        next = iterate(inner)
        if next === nothing
            return nothing
        end
        (i, state) = next
        return CI{F,S}(i[1]) => typed_value(d, i[2]), (state, inner)
    end
end
function Base.iterate(d::AbstractWithType{F, S}, state) where {F, S}
    (istate, inner) = state
    next = iterate(inner, istate)
    if next === nothing
        return nothing
    end
    (i, state) = next
    return CI{F,S}(i[1]) => typed_value(d, i[2]), (state, inner)
end

end


text:
using Robotlib
using Plots
include("../src/pf_state_estimator.jl")
include("../src/simfuns.jl")
import PFstateEstimator
import PFsimulator
pyplot()
default(size=(1000,700))
import DSP

srand(1)

# Particle filter parameters
h           = 0.01
sigmaW      = 2*[1.5,  0.02*pi/180, 1e-6] # t, R, f
sigmaV      = [0.1, 0.1, 1*pi/180] # x, y, theta
dFK         = 1. # plateau width
kd          = 0*1e-6 # extension to plateau width due to force (essentialy a super simple compliance model)
sigmaVFK    = [0.1, 0.05*pi/180, dFK, kd] # xyz, theta, plateau width, norm(f)
Npart       = 200
force_filter= 0.1 # in [0,1], lower value = more filtering

# Simulation parameters
T           = 80
σt          = 0
σR          = 0
σq          = 0
σm          = 0
σf          = 0
force_ref   = zeros(6) # Jag satte ner kraften för att debugga
Kreact      = zeros(6,6)
Kcomp       = 0*eye(6) # Compliance matrix
R_TF_M      = [
-1 0 0;
0  0 1;
0  1 0]
T_TF_M      = Rt2T(R_TF_M, [0,0.,0]) # Tool flange to meta

# Construct nominal trajectories
triwave     = cumsum(sign(sin(0.05*(1:T))))
triwave   ./= maximum(triwave)
triwave     = DSP.filtfilt(ones(15),[15.], triwave)
traj_nom    = PFsimulator.traj2frames3D([20*(triwave) linspace(0,200,T) zeros(T)]')

# Setup parameter objects
simparams                   = PFsimulator.SimParams(h, T, σt, σR, σq, σm, σf, force_ref, Kreact, Kcomp, T_TF_M)

pfparams                    = PFstateEstimator.PFparams(sigmaW, sigmaV, sigmaVFK, Npart, h, force_filter)

simdata    = PFsimulator.generate_simdata(traj_nom, simparams, optimize = false)
simresult  = PFsimulator.run_tracking("Single sensor", simdata, simparams, pfparams, 1, savetrace = true)

traj_diff = simresult.simdata.traj_nom-simresult.simdata.traj_act

# PFsimulator.plot(simresult, :tracking, :resample, :meas)
# plot_traj_sub(traj_diff, title="Noise in traj_act")

# Tests
@assert var(simresult.simdata.meas) < 1e-10 "Laser Measurement incorrect"
@assert var(traj_diff[1:3,1:4,:]) < 1e-10 "traj_act is incorrect"
@assert sum([simresult.trace[i].resample for i = 1:T]) < T/2 "Lots of resampling required!"
# @assert norm(simresult.et) < 2 "Tracking error is very high"

# Modify traj_act and run new Tests
simdata2 = deepcopy(simdata)
add = linspace(0,1,T)
for (i,a) in enumerate(add)
    simdata2.traj_act[1,4,i] += a
end
simdata2.meas = PFsimulator.simulate_sensor(simdata2.traj_nom, simdata2.traj_act, simdata2.search_traj, simparams)
simresult2  = PFsimulator.run_tracking("Error in x", simdata2, simparams, pfparams, 1, savetrace = true)

@assert norm(simresult2.et) < 0.8 "norm(simresult2.et) < 0.8"

# Dual sensor tests
T_TF_M      = cat(3,T_TF_M, Rt2T(R_TF_M, [0.,20.,0]))
simparams3  = PFsimulator.SimParams(h, T, σt, σR, σq, σm, σf, force_ref, Kreact, Kcomp, T_TF_M)
simdata3    = PFsimulator.generate_simdata(traj_nom, simparams3, optimize = false)
simdata3.traj_act = simdata2.traj_act
simdata3.meas = PFsimulator.simulate_sensor(simdata3.traj_nom, simdata3.traj_act, simdata3.search_traj, simparams3)
simresult3  = PFsimulator.run_tracking("Dual sensor, error in x", simdata3, simparams3, pfparams, 1, savetrace = true)
PFsimulator.plot_trace(simresult3)

@assert norm(simresult3.et) < 0.7 "norm(simresult3.et) < 0.7 "


text:
# This file is a part of SIS3316Digitizers.jl, licensed under the MIT License (MIT).

using BitOperations
using StructArrays, ArraysOfArrays
using Tables, TypedTables

function getraw() end
export getraw

function setraw() end
function setraw!() end
export setraw, setraw!

function getval() end
export getval

function setval() end
function setval!() end
export setval, setval!

function getaccessmode() end
export getaccessmode

function getlayout() end
export getlayout

function getaddress() end
export getaddress

abstract type AccessMode end
abstract type RO <: AccessMode end
abstract type WO <: AccessMode end
abstract type RW <: AccessMode end
const Readable = Union{RO,RW}
const Writeable = Union{WO,RW}
export AccessMode, RO, WO, RW, Readable, Writeable

abstract type JKRW <: RW end
abstract type GranularRW <: RW end
export JKRW, GranularRW

abstract type BitSemantics end
export BitSemantics

function bits_to_val() end
function val_to_bits() end
export bits_to_val, val_to_bits

struct LiteralBits <: BitSemantics end
export LiteralBits

@inline bits_to_val(b::Union{Bool,Unsigned}, semantics::LiteralBits) = b
@inline val_to_bits(v::Union{Bool,Unsigned}, semantics::LiteralBits) = v

struct TransformedBits{F<:Function,FInv<:Function} <: BitSemantics
    from_bits::F
    to_bits::FInv
end
export TransformedBits

@inline bits_to_val(b::Union{Bool,Unsigned}, semantics::TransformedBits) = semantics.from_bits(b)
@inline val_to_bits(v, semantics::TransformedBits) = semantics.to_bits(v)

struct EnumBits{E<:Enum} <: BitSemantics end
export EnumBits

@inline EnumBits(::Type{E}) where {E<:Enum} = EnumBits{E}()

@inline bits_to_val(b::Union{Bool,Unsigned}, semantics::EnumBits{E}) where E = E(b)
@inline val_to_bits(v, semantics::EnumBits) = Integer(v)

abstract type BitSelection{Acc<:AccessMode} end
export BitSelection

getval(x::Unsigned, bsel::BitSelection{<:Readable}) =
    bits_to_val(getraw(x, bsel), bsel.semantics)

setval(x::Unsigned, bsel::BitSelection{<:Writeable}, v) =
    setraw(x, bsel, val_to_bits(v, bsel.semantics))

@inline getaccessmode(reg::BitSelection{Acc}) where Acc = Acc

struct Bit{Acc<:AccessMode,i,S<:BitSemantics} <: BitSelection{Acc}
    i::Val{i}
    semantics::S
end
export Bit

@inline Bit{Acc}(i::Integer, semantics::BitSemantics = LiteralBits()) where {Acc<:AccessMode} =
    Bit{Acc,convert(Int, i),typeof(semantics)}(Val(convert(Int, i)), semantics)

Base.show(io::IO, bit::Bit{Acc,i}) where {Acc,i} = print(io, "Bit{$Acc}($i, $(repr(bit.semantics)))")

Base.show(io::IO, bit::Bit{Acc,i,LiteralBits}) where {Acc,i} = print(io, "Bit{$Acc}($i)")

@inline Bit{Acc}(i::Integer, ::Type{E}) where {Acc<:AccessMode,E<:Enum} =
    Bit{Acc}(i, EnumBits(E))

Base.show(io::IO, bit::Bit{Acc,i,EnumBits{E}}) where {Acc,i,E} = print(io, "Bit{$Acc}($i, $E)")

@inline Bit{Acc}(i::Integer, trafo::Tuple{Function,Function}) where {Acc<:AccessMode} =
    Bit{Acc}(i, TransformedBits(trafo[1], trafo[2]))

Base.show(io::IO, bit::Bit{Acc,i,<:TransformedBits}) where {Acc,i} = print(io, "Bit{$Acc}($i, $((bit.semantics.from_bits, bit.semantics.to_bits)))")

@inline Base.:<<(bit::Bit{Acc,i}, n::Integer) where {Acc,i} = Bit{Acc}(i + n, bit.semantics)

@inline BitOperations.bmask(::Type{T}, bit::Bit{Acc,i}) where {T<:Unsigned,Acc,i} = bmask(T, i)

@inline getraw(x::Unsigned, bit::Bit{<:Readable, i}) where i = bget(x, i)

@inline setraw(x::Unsigned, bit::Bit{<:Writeable, i}, v::Bool) where i = bset(x, i, v)

struct BitRange{Acc<:AccessMode,r,S<:BitSemantics} <: BitSelection{Acc}
    r::Val{r}
    semantics::S
end
export BitRange

@inline BitRange{Acc}(r::UnitRange{<:Integer}, semantics::BitSemantics = LiteralBits()) where {Acc<:AccessMode} =
    BitRange{Acc,convert(UnitRange{Int}, r),typeof(semantics)}(Val(convert(UnitRange{Int}, r)), semantics)

Base.show(io::IO, bits::BitRange{Acc,r}) where {Acc,r} = print(io, "BitRange{$Acc}($r, $(repr(bits.semantics)))")

Base.show(io::IO, bits::BitRange{Acc,r,LiteralBits}) where {Acc,r} =
    print(io, "BitRange($r)")

@inline BitRange{Acc}(r::UnitRange{<:Integer}, ::Type{E}) where {Acc<:AccessMode,E<:Enum} =
    BitRange{Acc}(r, EnumBits(E))

Base.show(io::IO, bits::BitRange{Acc,r,EnumBits{E}}) where {Acc,r,E} = print(io, "BitRange{$Acc}($r, $E)")

@inline BitRange{Acc}(r::UnitRange{<:Integer}, trafo::Tuple{Function,Function}) where {Acc<:AccessMode} =
    BitRange{Acc}(r, TransformedBits(trafo[1], trafo[2]))

Base.show(io::IO, bits::BitRange{Acc,r,<:TransformedBits}) where {Acc,r} =
    print(io, "BitRange{$Acc}($r, $((bits.semantics.from_bits, bits.semantics.to_bits)))")

@inline _shift_br_range(r::AbstractUnitRange{<:Integer}, n::Integer) = (first(r) + n):(last(r) + n)

@inline Base.:<<(bit::BitRange{Acc,r}, n::Integer) where {Acc,r} = BitRange{Acc}(_shift_br_range(r, n), bit.semantics)

@inline BitOperations.bmask(::Type{T}, bits::BitRange{Acc,r}) where {T<:Unsigned,Acc,r} = bmask(T, r)

@inline getraw(x::Unsigned, bits::BitRange{<:Readable, r}) where r = bget(x, r)

@inline setraw(x::Unsigned, bits::BitRange{<:Writeable, r}, v::Unsigned) where r = bset(x, r, v)

struct BitWriteOperation{T<:Unsigned}
    value::T
    bitmask::T
    isjk::Bool
end

export BitWriteOperation

BitWriteOperation{T}(::Type{<:Writeable}) where {T<:Unsigned} = BitWriteOperation{T}(zero(T), zero(T), false)
BitWriteOperation{T}(::Type{<:JKRW}) where {T<:Unsigned} = BitWriteOperation{T}(zero(T), zero(T), true)

is_masked(op::BitWriteOperation{T}) where T = (op.bitmask != ~zero(T))
export is_masked

@inline function BitOperations.bset(x::T, op::BitWriteOperation{T}) where T
    bm = op.bitmask
    y = op.value
    (x & ~bm) | (y & bm)
end

@inline function setraw(op::BitWriteOperation{T}, bsel::BitSelection{<:Writeable}, v::Union{Unsigned,Bool}) where T
    @argcheck op.isjk == false
    mod_value = setraw(op.value, bsel, v)
    mod_bitmask = op.bitmask | bmask(T, bsel)
    BitWriteOperation{T}(mod_value, mod_bitmask, op.isjk)
end

@inline function setraw(op::BitWriteOperation{T}, bsel::BitSelection{<:JKRW}, v::Union{Unsigned,Bool}) where T
    @argcheck op.isjk == true
    op.value == op.bitmask == zero(T) || op.bitmask == ~zero(T) || throw(ArgumentError("Can't add a JK bit write to a non-empty BitWriteOperation with partial bitmask"))
    n_shift = 4 * sizeof(T)
    to_set = setraw(zero(T), bsel, v) << n_shift >>> n_shift
    to_clear = setraw(zero(T), bsel, ~v) << n_shift
    mod_value = op.value | to_set | to_clear
    BitWriteOperation{T}(mod_value, ~zero(T), op.isjk)
end

@inline function setval(op::BitWriteOperation{T}, bsel::BitSelection{<:Writeable}, v) where T
    setraw(op, bsel, val_to_bits(v, bsel.semantics))
end

@inline Base.merge(a::BitWriteOperation) = a

@inline function Base.merge(a::BitWriteOperation{T}, b::BitWriteOperation{T}, cs::BitWriteOperation{T}...) where T
    @argcheck a.isjk == b.isjk
    if !b.isjk
        mod_value = (a.value & ~b.bitmask) | (b.value & b.bitmask)
        mod_bitmask = a.bitmask | b.bitmask
        merge(BitWriteOperation(mod_value, mod_bitmask, false), cs...)
    else
        @argcheck a.bitmask == b.bitmask == ~zero(T)
        shft = 4 * sizeof(T)
        msk = ~((b.value >> shft) | (b.value << shft))
        mod_value = a.value & msk | b.value
        merge(BitWriteOperation(mod_value, ~zero(T), true), cs...)
    end
end

const NamedBits{N} = NamedTuple{names, <:NTuple{N,BitSelection}} where names
export NamedBits

getraw(x::Unsigned, nb::NamedBits) = map(bsel -> getraw(x, bsel), nb)

getval(x::Unsigned, nb::NamedBits) = map(bsel -> getval(x, bsel), nb)

Base.@pure _nt_names(x::NamedTuple{names}) where names = map(Val, names)
@inline _nt_getproperty(x::NamedTuple, ::Val{name}) where name = getproperty(x, name)

function setval(op::BitWriteOperation{T}, nb::NamedBits, v::NamedTuple) where T
    writes = map(_nt_names(v)) do name
        bsel = _nt_getproperty(nb, name)
        value = _nt_getproperty(v, name)
        setval(BitWriteOperation{T}(zero(T), zero(T), op.isjk), bsel, value)
    end
    merge(writes...)
end

@inline function Base.filter(::Type{Acc}, nbits::NamedBits) where {Acc<:AccessMode}
    if @generated
        Acc, nbits
        idxs = findall(T -> T <: BitSelection{<:Acc}, fieldtypes(nbits))
        names = fieldnames(nbits)[idxs]
        vals = Any[ :(getfield(nbits, $(QuoteNode(n)))) for n in names ]
        :( NamedTuple{$names}(($(vals...),)) )
    else
        (;filter(x -> x[2] isa BitSelection{<:Acc}, (pairs(nbits)...,))...)
    end
end

struct BitGroup{
    Acc<:AccessMode,
    NB<:NamedBits
} <: BitSelection{Acc}
    _layout::NB
end
export BitGroup

@inline BitGroup{Acc}(layout::NB) where {Acc<:AccessMode,NB<:NamedBits} = BitGroup{Acc,NB}(layout)

@inline BitGroup{Acc}(;named_bits...) where {Acc<:AccessMode} = BitGroup{Acc}(values(named_bits))

@inline getlayout(grp::BitGroup) = getfield(grp, :_layout)
@inline getaccessmode(reg::BitGroup{Acc}) where Acc = Acc

@inline function Base.getproperty(grp::BitGroup, p::Symbol)
    # May need to include internal fields of BitGroup to make Zygote happy:
    if p == :_layout
        getfield(grp, :_layout)
    else
        getproperty(getlayout(grp), p)
    end
end

@inline function Base.propertynames(grp::BitGroup, private::Bool = false)
    names = propertynames(getlayout(grp))
    private ? (names..., :_layout) : names
end

Base.merge(a::NamedTuple, grp::BitGroup{names}) where {names} = merge(a, getlayout(grp))

@inline Base.keys(grp::BitGroup) = keys(getlayout(grp))
@inline Base.values(grp::BitGroup) = values(getlayout(grp))

@inline Base.getindex(grp::BitGroup, name::Symbol) = getproperty(grp, name)
@inline Base.getindex(grp::BitGroup, i::Integer) = getlayout(grp)[i]

Base.show(io::IO, grp::BitGroup{Acc}) where {Acc,Addr,T} = print(io, "BitGroup{$Acc}($(repr(getlayout(grp))))")

function Base.show(io::IO, ::MIME"text/plain", grp::BitGroup{Acc}) where {Acc}
    println(io, "BitGroup{$Acc}(")
    for (k, v) in pairs(getlayout(grp))
        print("    ")
        print("$k = ")
        show(io, v)
        println(",")
    end
    print(io, ")")
end

@inline Base.:<<(grp::BitGroup{Acc}, n::Integer) where {Acc,r} =
    BitGroup{Acc}(map(bsel -> bsel << n, getlayout(grp)))

@inline BitOperations.bmask(::Type{T}, bsel::BitGroup) where {T<:Unsigned} =
    |(map(bsel -> bmask(T, bsel), values(bsel))...)

# ToDo:
# @inline getraw(x::Unsigned, bsel::BitGroup{<:Readable, r}) where r = bget(x, r)

# ToDo:
# @inline setraw(x::Unsigned, bsel::BitGroup{<:Writeable, r}, v::Unsigned) where r = bset(x, r, v)

getval(x::Unsigned, bsel::BitGroup{<:Readable}) = getval(x, getlayout(bsel))

# ToDo:
# setval(x::Unsigned, bsel::BitGroup{<:Writeable}, v) = ...

struct Register{Acc<:AccessMode,Addr<:Unsigned,T<:Unsigned,NB<:NamedBits}
    _address::Addr
    _layout::NB
end
export Register

Register{Acc,T}(address::Addr,layout::NB) where {Acc<:AccessMode,Addr<:Unsigned,T<:Unsigned,NB<:NamedBits} =
    Register{Acc,Addr,T,NB}(address, layout)

@inline getaddress(reg::Register) = getfield(reg, :_address)
@inline getlayout(reg::Register) = getfield(reg, :_layout)
@inline getaccessmode(reg::Register{Acc}) where Acc = Acc

_bitsel_ref(::Type{T}, addr::Addr, bsel::BitSelection) where {T,Addr<:Unsigned} = BitSelRef{T}(addr, bsel)

#!!! TODO: Reconsider - kind of a workaround, may lead to unexpexted results
# if user writes raw bits to returned pseudo-register:
_bitsel_ref(::Type{T}, addr::Addr, bsel::BitGroup{Acc}) where {T,Addr<:Unsigned,Acc} =
    Register{Acc,T}(addr, getlayout(bsel))

@inline function Base.getproperty(reg::Register{Acc,Addr,T}, p::Symbol) where {Acc,Addr,T}
    # May need to include internal fields of Register to make Zygote happy:
    if p == :_address
        getfield(reg, :_address)
    elseif p == :_layout
        getfield(reg, :_layout)
    else
        _bitsel_ref(T, getaddress(reg), getproperty(getlayout(reg), p))
    end
end

@inline function Base.propertynames(reg::Register, private::Bool = false)
    names = propertynames(getlayout(reg))
    if private
        (names..., :_address, :_layout)
    else
        names
    end
end

Base.merge(a::NamedTuple, reg::Register{names}) where {names} = merge(a, getlayout(reg))

@inline Base.keys(reg::Register) = keys(getlayout(reg))

@inline Base.getindex(reg::Register, name::Symbol) = getproperty(reg, name)

@inline Base.getindex(reg::Register{Acc,Addr,T}, i::Integer) where {Acc,Addr,T} =
    _bitsel_ref(T, getaddress(reg), getlayout(reg)[i])

@inline function Base.values(reg::Register{Acc,Addr,T}) where {Acc,Addr,T}
    addr = getaddress(reg)
    map(bsel -> BitSelRef{T}(bsel, addr), values(getlayout(reg)))
end

Base.show(io::IO, reg::Register{Acc,Addr,T}) where {Acc,Addr,T} = print(io, "Register{$Acc,$T}($(repr(getaddress(reg))), $(repr(getlayout(reg))))")

function Base.show(io::IO, ::MIME"text/plain", reg::Register{Acc,Addr,T}) where {Acc,Addr,T}
    println(io, "Register{$Acc,$T}(")
    println(io, "    $(repr(getaddress(reg))),")
    println(io, "    (")
    for (k, v) in pairs(getlayout(reg))
        print("        ")
        print("$k = ")
        show(io, v)
        println(",")
    end
    println(io, "    )")
    print(io, ")")
end

getraw(x::T, reg::Register) where {T} = map(x, filter(Readable, getlayout(reg)))

getval(x::T, reg::Register) where {T} = map(x, filter(Readable, getlayout(reg)))

struct BitSelRef{T<:Unsigned,Addr<:Unsigned,BSel<:BitSelection}
    addr::Addr
    bitsel::BSel
end
export BitSelRef

BitSelRef{T}(addr::Addr, bitsel::BSel) where {T<:Unsigned,Addr<:Unsigned,BSel<:BitSelection} =
    BitSelRef{T,Addr,BSel}(addr, bitsel)

Base.show(io::IO, bitref::BitSelRef{T}) where {T} = print(io, "BitSelRef{$T}($(repr(bitref.bitsel)), $(repr(bitref.addr)))")

getaddress(ref::BitSelRef) = ref.addr
getlayout(ref::BitSelRef) = ref.bitsel
@inline getaccessmode(bitref::BitSelRef) = getaccessmode(bitref.bitsel)


text:
"""
Writes a graph `g` to a file `f` in the [Pajek
NET](http://gephi.github.io/users/supported-graph-formats/pajek-net-format/) format.
Returns 1 (number of graphs written).
"""
function savenet(f::IO, g::SimpleGraph, gname::AbstractString = "g")
    println(f, "*Vertices $(nv(g))")
    # write edges
    if is_directed(g)
        println(f, "*Arcs")
    else
        println(f, "*Edges")
    end
    for e in edges(g)
        println(f, "$(src(e)) $(dst(e))")
    end
    return 1
end

"""Reads a graph from file `fname` in the [Pajek
 NET](http://gephi.github.io/users/supported-graph-formats/pajek-net-format/) format.
 Returns 1 (number of graphs written).
"""
function loadnet(f::IO, gname::AbstractString = "g")
    line =readline(f)
    # skip comments
    while startswith(line, "%")
        line =readline(f)
    end
    n = parse(Int, matchall(r"\d+",line)[1])
    for fline in eachline(f)
        line = fline
        (ismatch(r"^\*Arcs",line) || ismatch(r"^\*Edges",line)) && break
    end
    if ismatch(r"^\*Arcs",line)
        g = DiGraph(n)
    else
        g = Graph(n)
    end
    while ismatch(r"^\*Arcs",line)
        for fline in eachline(f)
            line = fline
            m = matchall(r"\d+",line)
            length(m) < 2 && break
            add_edge!(g, parse(Int, m[1]), parse(Int, m[2]))
        end
    end
    while ismatch(r"^\*Edges",line) # add edges in both directions
        for fline in eachline(f)
            line = fline
            m = matchall(r"\d+",line)
            length(m) < 2 && break
            i1,i2 = parse(Int, m[1]), parse(Int, m[2])
            add_edge!(g, i1, i2)
            add_edge!(g, i2, i1)
        end
    end
    return g
end

loadnet_mult(io::IO) = Dict("g" => loadnet(io))

filemap[:net] = (loadnet, loadnet_mult, savenet, NI)


text:
using FileIO

<<<<<<< HEAD
struct Vertex
=======
mutable struct Vertex
>>>>>>> 5f9517fcfc507bc9c83bc0ca5755ab4a4d01f653
  pos::AbstractVector
  norm::AbstractVector
  texC::AbstractVector
  tan::AbstractVector
  bitan::AbstractVector
end

<<<<<<< HEAD
struct Texture
=======
mutable struct Texture
>>>>>>> 5f9517fcfc507bc9c83bc0ca5755ab4a4d01f653
  id::UInt
  type::AbstractString
  path::AbstractString
end

<<<<<<< HEAD
 struct Mesh
   vertices::AbstractVector{Vertex}
   indices::AbstractVector{UInt}
   textures::AbstractVector{Texture}
   VAO::UInt

   function Mesh(verts::AbstractVector{Vertex},
     ind::AbstractVector{UInt},
     textr::AbstractVector{Texture})

     vertices = verts
     indices = ind
     textures = textr

     setUp()
   end
=======
 mutable struct Mesh
   verts::AbstractVector{Vertex}
   indices::AbstractVector{UInt}
   verts::AbstractVector{Texture}
   VAO::UInt

   Mesh()
>>>>>>> 5f9517fcfc507bc9c83bc0ca5755ab4a4d01f653


text:
using Test, LanguageFinder

test_set =  Dict("AR" => "النشوة وتمجيد الألم نشأت بالفعل، وسأعرض لك التفاصيل",
                 "CS" => "Chraň ji, nedovede-li už ona sebe chránit! Ty ji miluješ? Tedy se obětuj!",
                 "DA" => "Forældre har førsteret til at vælge den form for undervisning, som deres børn skal have.",
                 "DE" => "Vergnugt fu ri vornamen se wo burschen schuftet. Bei als ganzen musset tische tur harten nur. Es vornamen liebsten fu arbeiter sa.",
                 "EL" => "Διάφορες εκδοχές έχουν προκύψει με το πέρασμα των χρόνων, άλλες φορές κατά λάθος, άλλες φορές σκόπιμα.",
                 "EN" => "One advanced diverted domestic sex repeated bringing you old. Possible procured her trifling laughter thoughts property she met way.",
                 "ES" => "Indicarse resonaban sin reconocio complacia tio sea tormentos irascible. Esclavo glorias voz oro agujero tufillo muy ano encogia.",
                 "FA" => "مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی مورد نیاز و کاربردهای متنوع با هدف بهبود ابزارهای کاربردی می باشد",
                 "FI" => "On todistettu, että lukijaa häiritsee sivun ulkoasu lukiessaan sivua.",
                 "FR" => "Certes sol hordes globes ame raison. Au pentes je bourse me durcis la espace. Demeure ouvrent obtenue partout etaient une feu.",
                 "HE" => "היסטוריה כתב, שכל על ערכים הגולשות. מה ספורט בעברית אתנולוגיה אתה.",
                 "HI" => "होभर बाटते सहायता परिवहन ज्यादा आंतरजाल सम्पर्क संसाध सुनत सक्षम संपादक माध्यम सिद्धांत हैं। पहोचाना विश्वव्यापि प्राण उनको मुखय पहोचने",
                 "HU" => "Szüksége van egy könnyű, teljes megoldás a Magyar szöveget beszéddé?",
                 "IT" => "Quando essa veniva elogiata, tali aspetti erano visti come un miglioramento per la società",
                 "JP" => "さらに安倍さんが、「事務所が補塡した事実はない」などとしてきた答弁は、結果として虚偽だった可能性が高まっています。",
                 "KO" => "국회는 상호원조 또는 안전보장에 관한 조약. 국민에 대하여 책임을 진다. 원장은 국회의 동의를 얻어 대통령이 임명하고. 대법원장의 임기는 6년으로 하며.",
                 "NL" => "Komst deele de telde te er zeker. Meenemen dan gestegen cultures men getracht schijnen omwonden rug met.",
                 "NO" => "Så ville han Pål i veien og friste om ikke han hadde lykken til å bygge skip og vinne kongsdatteren og halve kongeriket.",
                 "PL" => "Nie chcę być niemiły, ale....nie będę mógł wyjść do toalety. Pomogę panu ją podnieść.",
                 "PT" => "Sem passeios dir penetrou dissesse arrojado absoluta sao. Frioleiras nao das recordarei excellente sao iii.",
                 "RU" => "кто. Звездны богачей неправд. Бледному принести звездной подобные теряться проблеск.",
                 "SV" => "Julklappar och presentkort till de inom vården. Men inte till timvikarierna.",
                 "TR" => "Ancak bunların büyük bir çoğunluğu mizah katılarak veya rastgele sözcükler eklenerek değiştirilmişlerdir.",
                 "UK" => "Оля, почитай щось з художньої літератури, яка краще передасть інтонації живої мови.",
                 "ZH" => "欢迎您到“跨年之城”海口参加湖南卫视跨年演唱会。根据跨年演唱会服务保障工作总体部署，为了您和大家的身体健康，请您仔细阅读本通告，严格遵守有关要求。")

L = LanguageFinder.LanguageFind

@testset "Test Random Lorem Ipsum" begin
for (key,value) in test_set
        @test lowercase(key) == L(value, 0).lang end end

max_stars_repo_name: nusretipek/LanguageFinder

max_issues_repo_name: nusretipek/LanguageFinder

max_forks_repo_name: nusretipek/LanguageFinder


text:
EuclideanDistance(x,y) = sqrt(sum(map(x-> x^2, x.-y)))
_weightedSquare(x,w)=w*x^2
WeightedEuclideanDistance(x,y,w) = sqrt(sum(map(_weightedSquare,(x.-y), w)))


text:
###############################################################################
#
#   GenericTypes.jl : Generic Types
#
###############################################################################

###############################################################################
#
#   PermGroup / perm
#
###############################################################################

const PermID = ObjectIdDict()

mutable struct PermGroup <: Nemo.Group
   n::Int

   function PermGroup(n::Int, cached=true)
      if haskey(PermID, n)
         return PermID[n]::PermGroup
      else
         z = new(n)
         if cached
            PermID[n] = z
         end
         return z
      end
   end
end

mutable struct perm <: Nemo.GroupElem
   d::Array{Int, 1}
   cycles::Vector{Vector{Int}}
   parent::PermGroup

   function perm(n::Int)
      return new(collect(1:n))
   end

   function perm(a::Array{Int, 1})
      return new(a)
   end
end

doc"""
    AllPerms(n::Int)
> Returns an iterator over arrays representing all permutations of `1:n`.
> Similar to `Combinatorics.permutations(1:n)`
"""
struct AllPerms
   n::Int
   all::Int
   AllPerms(n::Int) = new(n, factorial(n))
end

###############################################################################
#
#   Partition
#
###############################################################################

doc"""
    Partition(part::Vector{Int}, check::Bool=true)
> Partition represents integer partition into numbers in non-increasing order.
> It is a thin wrapper over `Vector{Int}`
"""
mutable struct Partition <: AbstractVector{Int}
   n::Int
   part::Vector{Int}

   function Partition(part::Vector{Int}, check::Bool=true)
      if check
         all(diff(part) .<= 0) || throw("Partition must be decreasing!")
         if length(part) > 0
            part[end] >=1 || throw("Found non-positive entry in partition!")
         end
      end
      return new(sum(part), part)
   end
end

doc"""
   Partitions(n::Int)
> Returns an iterator over all integer `Partition`s of `n`. They come in
> ascending order. See also `Combinatorics.partitions(n)`.
"""
struct Partitions
    n::Int
end

###############################################################################
#
#   SkewDiagram
#
###############################################################################

doc"""
    SkewDiagram(lambda::Partition, mu::Partition)
> Implements a skew diagram, i.e. a difference of two Young diagrams
> represented by partitions `lambda` and `mu`.
"""
struct SkewDiagram
   lam::Partition
   mu::Partition

   function SkewDiagram(lambda, mu)
      lambda.n >= mu.n || throw("Can't create SkewDiagram: $mu is partition of  $(mu.n) > $(lambda.n).")
      length(lambda) >= length(mu) || throw("Can't create SkewDiagram: $mu is longer than $(lambda)!")
      for (l, m) in zip(lambda, mu)
         l >= m || throw("a row of $mu is longer than a row of $lambda")
      end
      return new(lambda, mu)
   end
end

###############################################################################
#
#   Young Tableau
#
###############################################################################

doc"""
    YoungTableau(part::Partition, tab::Array{Int, 2})
> Returns the Young tableaux of partition `part` of `n`, filled linearly
> (row-major) by `fill` vector.
"""
struct YoungTableau <: AbstractArray{Int, 2}
   part::Partition
   tab::Array{Int,2}
end

###############################################################################
#
#   PolyRing / Poly
#
###############################################################################

mutable struct PolyRing{T <: RingElement} <: Nemo.PolyRing{T}
   base_ring::Ring
   S::Symbol

   function PolyRing{T}(R::Ring, s::Symbol, cached::Bool = true) where T <: RingElement
      if haskey(PolyID, (R, s))
         return PolyID[R, s]::PolyRing{T}
      else
         z = new{T}(R, s)
         if cached
           PolyID[R, s] = z
         end
         return z
      end
   end
end

const PolyID = Dict{Tuple{Ring, Symbol}, Ring}()

mutable struct Poly{T <: RingElement} <: Nemo.PolyElem{T}
   coeffs::Array{T, 1}
   length::Int
   parent::PolyRing{T}

   Poly{T}() where T <: RingElement = new{T}(Array{T}(0), 0)

   function Poly{T}(b::Array{T, 1}) where T <: RingElement
      z = new{T}(b)
      z.length = normalise(z, length(b))
      return z
   end

   Poly{T}(a::T) where T <: RingElement = iszero(a) ? new{T}(Array{T}(0), 0) : new{T}([a], 1)
end

###############################################################################
#
#   MPolyRing / MPoly
#
###############################################################################

# S is a Symbol which can take the values:
# :lex
# :revlex
# :deglex
# :degrevlex
#
# T is an Int which is the number of variables
# (plus one if ordered by total degree)

mutable struct MPolyRing{T <: RingElement} <: Nemo.MPolyRing{T}
   base_ring::Ring
   S::Array{Symbol, 1}
   ord::Symbol
   num_vars::Int
   N::Int

   function MPolyRing{T}(R::Ring, s::Array{Symbol, 1}, ord::Symbol, N::Int,
                         cached::Bool = true) where T <: RingElement
      if haskey(MPolyID, (R, s, ord, N))
         return MPolyID[R, s, ord, N]::MPolyRing{T}
      else
         z = new{T}(R, s, ord, length(s), N)
         if cached
           MPolyID[R, s, ord, N] = z
         end
         return z
      end
   end
end

const MPolyID = Dict{Tuple{Ring, Array{Symbol, 1}, Symbol, Int}, Ring}()

mutable struct MPoly{T <: RingElement} <: Nemo.MPolyElem{T}
   coeffs::Array{T, 1}
   exps::Array{UInt, 2}
   length::Int
   parent::MPolyRing{T}

   function MPoly{T}(R::MPolyRing) where T <: RingElement
      N = R.N
      return new{T}(Array{T}(0), Array{UInt}(N, 0), 0, R)
   end

   MPoly{T}(R::MPolyRing, a::Array{T, 1}, b::Array{UInt, 2}) where T <: RingElement = new{T}(a, b, length(a), R)

   function MPoly{T}(R::MPolyRing, a::T) where T <: RingElement
      N = R.N
      return iszero(a) ? new{T}(Array{T}(0), Array{UInt}(N, 0), 0, R) :
                                          new{T}([a], zeros(UInt, N, 1), 1, R)
   end
end

###############################################################################
#
#   SparsePolyRing / SparsePoly
#
###############################################################################

mutable struct SparsePolyRing{T <: RingElement} <: Nemo.Ring
   base_ring::Ring
   S::Symbol
   num_vars::Int

   function SparsePolyRing{T}(R::Ring, s::Symbol, cached::Bool = true) where T <: RingElement
      if haskey(SparsePolyID, (R, s))
         return SparsePolyID[R, s]::SparsePolyRing{T}
      else
         z = new{T}(R, s)
         if cached
           SparsePolyID[R, s] = z
         end
         return z
      end
   end
end

const SparsePolyID = Dict{Tuple{Ring, Symbol}, SparsePolyRing}()

mutable struct SparsePoly{T <: RingElement} <: Nemo.RingElem
   coeffs::Array{T, 1}
   exps::Array{UInt}
   length::Int
   parent::SparsePolyRing{T}

   SparsePoly{T}() where T <: RingElement = new{T}(Array{T}(0), Array{UInt}(0), 0)

   SparsePoly{T}(a::Array{T, 1}, b::Array{UInt, 1}) where T <: RingElement = new{T}(a, b, length(a))

   SparsePoly{T}(a::T) where T <: RingElement = iszero(a) ? new{T}(Array{T}(0), Array{UInt}(0), 0) :
                                               new{T}([a], [UInt(0)], 1)
end

###############################################################################
#
#   ResRing / Res
#
###############################################################################

mutable struct ResRing{T <: RingElement} <: Nemo.ResRing{T}
   base_ring::Ring
   modulus::T

   function ResRing{T}(modulus::T, cached::Bool = true) where T <: RingElement
      c = canonical_unit(modulus)
      if !isone(c)
        modulus = divexact(modulus, c)
      end
      if haskey(ModulusDict, (parent(modulus), modulus))
         return ModulusDict[parent(modulus), modulus]::ResRing{T}
      else
         z = new{T}(parent(modulus), modulus)
         if cached
            ModulusDict[parent(modulus), modulus] = z
         end
         return z
      end
   end
end

const ModulusDict = Dict{Tuple{Ring, RingElement}, Ring}()

mutable struct Res{T <: RingElement} <: Nemo.ResElem{T}
   data::T
   parent::ResRing{T}

   Res{T}(a::T) where T <: RingElement = new{T}(a)
end

###############################################################################
#
#   RelSeriesRing / RelSeries
#
###############################################################################

mutable struct RelSeriesRing{T <: RingElement} <: Nemo.SeriesRing{T}
   base_ring::Ring
   prec_max::Int
   S::Symbol

   function RelSeriesRing{T}(R::Ring, prec::Int, s::Symbol, cached::Bool = true) where T <: RingElement
      if haskey(RelSeriesID, (R, prec, s))
         return RelSeriesID[R, prec, s]::RelSeriesRing{T}
      else
         z = new{T}(R, prec, s)
         if cached
            RelSeriesID[R, prec, s] = z
         end
         return z
      end
   end
end

const RelSeriesID = Dict{Tuple{Ring, Int, Symbol}, Ring}()

mutable struct RelSeries{T <: RingElement} <: Nemo.RelSeriesElem{T}
   coeffs::Array{T, 1}
   length::Int
   prec::Int
   val::Int
   parent::RelSeriesRing{T}

   function RelSeries{T}(a::Array{T, 1}, length::Int, prec::Int, val::Int) where T <: RingElement
      new{T}(a, length, prec, val)
   end

   RelSeries{T}(a::RelSeries{T}) where T <: RingElement = a
end

###############################################################################
#
#   AbsSeriesRing / AbsSeries
#
###############################################################################

mutable struct AbsSeriesRing{T <: RingElement} <: Nemo.SeriesRing{T}
   base_ring::Ring
   prec_max::Int
   S::Symbol

   function AbsSeriesRing{T}(R::Ring, prec::Int, s::Symbol, cached::Bool = true) where T <: RingElement
      if haskey(AbsSeriesID, (R, prec, s))
         return AbsSeriesID[R, prec, s]::AbsSeriesRing{T}
      else
         z = new{T}(R, prec, s)
         if cached
            AbsSeriesID[R, prec, s] = z
         end
         return z
      end
   end
end

const AbsSeriesID = Dict{Tuple{Ring, Int, Symbol}, Ring}()

mutable struct AbsSeries{T <: RingElement} <: Nemo.AbsSeriesElem{T}
   coeffs::Array{T, 1}
   length::Int
   prec::Int
   parent::AbsSeriesRing{T}

   AbsSeries{T}(a::Array{T, 1}, length::Int, prec::Int) where T <: RingElement = new{T}(a, length, prec)
   AbsSeries{T}(a::AbsSeries{T}) where T <: RingElement = a
end

###############################################################################
#
#   LaurentSeriesRing / LarentSeriesRingElem
#
###############################################################################

mutable struct LaurentSeriesRing{T <: RingElement} <: Nemo.Ring
   base_ring::Ring
   prec_max::Int
   S::Symbol

   function LaurentSeriesRing{T}(R::Ring, prec::Int, s::Symbol, cached::Bool = true) where T <: RingElement
      if haskey(LaurentSeriesID, (R, prec, s))
         return LaurentSeriesID[R, prec, s]::LaurentSeriesRing{T}
      else
         z = new{T}(R, prec, s)
         if cached
            LaurentSeriesID[R, prec, s] = z
         end
         return z
      end
   end
end

const LaurentSeriesID = Dict{Tuple{Ring, Int, Symbol}, Ring}()

mutable struct LaurentSeriesRingElem{T <: RingElement} <: Nemo.RingElem
   coeffs::Array{T, 1}
   length::Int
   prec::Int
   val::Int
   parent::LaurentSeriesRing{T}

   function LaurentSeriesRingElem{T}(a::Array{T, 1}, length::Int, prec::Int, val::Int) where T <: RingElement
      new{T}(a, length, prec, val)
   end

   LaurentSeriesRingElem{T}(a::LaurentSeriesRingElem{T}) where T <: RingElement = a
end

###############################################################################
#
#   LaurentSeriesField / LarentSeriesFieldElem
#
###############################################################################

mutable struct LaurentSeriesField{T <: FieldElement} <: Nemo.Field
   base_ring::Field
   prec_max::Int
   S::Symbol

   function LaurentSeriesField{T}(R::Field, prec::Int, s::Symbol, cached::Bool = true) where T <: FieldElement
      if haskey(LaurentSeriesID, (R, prec, s))
         return LaurentSeriesID[R, prec, s]::LaurentSeriesField{T}
      else
         z = new{T}(R, prec, s)
         if cached
            LaurentSeriesID[R, prec, s] = z
         end
         return z
      end
   end
end

mutable struct LaurentSeriesFieldElem{T <: FieldElement} <: Nemo.FieldElem
   coeffs::Array{T, 1}
   length::Int
   prec::Int
   val::Int
   parent::LaurentSeriesField{T}

   function LaurentSeriesFieldElem{T}(a::Array{T, 1}, length::Int, prec::Int, val::Int) where T <: FieldElement
      new{T}(a, length, prec, val)
   end

   LaurentSeriesFieldElem{T}(a::LaurentSeriesFieldElem{T}) where T <: FieldElement = a
end

const LaurentSeriesElem{T} = Union{LaurentSeriesRingElem{T}, LaurentSeriesFieldElem{T}} where T <: RingElement

###############################################################################
#
#   FracField / Frac
#
###############################################################################

mutable struct FracField{T <: RingElem} <: Nemo.FracField{T}
   base_ring::Ring

   function FracField{T}(R::Ring, cached::Bool = true) where T <: RingElem
      if haskey(FracDict, R)
         return FracDict[R]::FracField{T}
      else
         z = new{T}(R)
         if cached
            FracDict[R] = z
         end
         return z
      end
   end
end

const FracDict = Dict{Ring, Ring}()

mutable struct Frac{T <: RingElem} <: Nemo.FracElem{T}
   num::T
   den::T
   parent::FracField{T}

   Frac{T}(num::T, den::T) where T <: RingElem = new{T}(num, den)
end

###############################################################################
#
#   MatSpace / Mat
#
###############################################################################

# not really a mathematical ring
mutable struct MatSpace{T <: RingElement} <: Nemo.MatSpace{T}
   rows::Int
   cols::Int
   base_ring::Ring

   function MatSpace{T}(R::Ring, r::Int, c::Int, cached::Bool = true) where T <: RingElement
      if haskey(MatDict, (R, r, c))
         return MatDict[R, r, c]::MatSpace{T}
      else
         z = new{T}(r, c, R)
         if cached
            MatDict[R, r, c] = z
         end
         return z
      end
   end
end

const MatDict = Dict{Tuple{Ring, Int, Int}, Ring}()

mutable struct Mat{T <: RingElement} <: Nemo.MatElem{T}
   entries::Array{T, 2}
   base_ring::Ring

   function Mat{T}(A::Array{T, 2}) where T <: RingElement
      return new{T}(A)
   end

   function Mat{T}(r::Int, c::Int, A::Array{T, 1}) where T <: RingElement
      t = Array{T}(r, c)
      for i = 1:r
         for j = 1:c
            t[i, j] = A[(i - 1) * c + j]
         end
      end
      return new{T}(t)
   end
end


text:
using LinearAlgebra

UTriangular(a::Matrix) = UpperTriangular(a)

## NumericExtensions is no longer supported, underoptimized implementation:
function logsumexp(x::AbstractVector{T}) where {T<:AbstractFloat}
    m = maximum(x)
    log(sum(exp.(x .- m))) + m
end
logsumexp(x::Matrix{T}, dim::Integer) where {T<:AbstractFloat} = mapslices(logsumexp, x, dims=dim)

eye(n::Int) = Matrix(1.0I,n,n)
eye(::Type{Float64}, n::Int) = Matrix(1.0I,n,n)

## Also NumericExtensions' semantics of dot() is no longer supported.
function LinearAlgebra.dot(x::Matrix{T}, y::Matrix{T}) where {T<:AbstractFloat}
    size(x) == size(y) || error("Matrix sizes must match")
    dot(vec(x), vec(y))
end
function LinearAlgebra.dot(x::Matrix{T}, y::Matrix{T}, dim::Integer) where {T<:AbstractFloat}
    size(x) == size(y) || error("Matrix sizes must match")
    if dim==1
        r = zeros(T, 1, size(x,2))
        for j in 1:size(x,2)
            for i in 1:size(x,1)
                r[j] += x[i,j]*y[i,j]
            end
        end
    else
        r = zeros(T, size(x,1), 1)
        for j in 1:size(x,2)
            for i in 1:size(x,1)
                r[i] += x[i,j]*y[i,j]
            end
        end
    end
    r
end


text:
platforms_and_sources = Dict(
    Platform("x86_64", "linux") => [
        ArchiveSource("https://developer.nvidia.com/compute/cudnn/secure/8.3.2/local_installers/10.2/cudnn-linux-x86_64-8.3.2.44_cuda10.2-archive.tar.xz",
                      "d6f56ef9ca8cf8f91eb73210ba6c3dca49ba4446c1661bfafe55c1ec40b669ac")],
    Platform("x86_64", "windows") => [
        ArchiveSource("https://developer.nvidia.com/compute/cudnn/secure/8.3.2/local_installers/10.2/cudnn-windows-x86_64-8.3.2.44_cuda10.2-archive.zip",
                      "3810b7a4313614aedd28260785f0ebaa69204bb38fa61e439f75c75a4ba07b3d")],
)

max_stars_repo_name: sharanry/Yggdrasil

max_issues_repo_name: sharanry/Yggdrasil

max_forks_repo_name: sharanry/Yggdrasil


text:
using RomanNumerals
using Base.Test

# Constructor tests
@test RomanNumeral(46) == RomanNumeral("XLVI")
@test RomanNumeral(1999) == RomanNumeral("MCMXCIX")
@test rn"XXXX" == rn"XL"
@test RomanNumeral(1) == rn"I"
@test rn"xx" == rn"XX"

# Invalid numeral tests
@test_throws InvalidRomanError RomanNumeral("nope")
@test_throws InvalidRomanError RomanNumeral("XLX")

# arithmetic tests
@test rn"I" + rn"IX" == rn"X"
@test rn"L" - RomanNumeral(1) == rn"XLIX"
@test rn"X" > rn"IX"
@test isqrt(rn"MXXIV") == rn"XXXII"
@test rn"XX" ^ rn"II" == rn"CD"