text:
using Primes

# 1d Cooley-Tukey FFTs, using an FFTW-like (version 1) approach: automatic
# generation of fixed-size FFT kernels (with and without twiddle factors)
# which are combined to make arbitrary-size FFTs (plus generic base
# cases for large prime factors).

#############################################################################
# Combining pregenerated kernels into generic-size FFT plans:

# now, we define a CTPlan (Cooley-Tukey plan) as a sequence of twiddle steps
# followed by a nontwiddle step:

abstract type TwiddleStep{T} end
abstract type NontwiddleStep{T} end

mutable struct CTPlan{T,forward,Tt,Tn} <: Plan{T}
    n::Int
    tsteps::Tt # a tuple of TwiddleSteps
    nstep::Tn # a NontwiddleStep
    pinv::ScaledPlan{T}
    CTPlan{T,forward,Tt,Tn}(n,tsteps,nstep) where {T,forward,Tt,Tn} = new(n,tsteps,nstep)
end

summary(p::CTPlan{T,forw}) where {T,forw} = string(forw ? "for" : "back",
                                            "ward CTPlan{$T} of size ", p.n)
function show(io::IO, p::CTPlan)
    if p.n == 0
        print(io, "(no transform)") # untransformed dims in multi-dim plans
    else
        ns = length(p.tsteps)
        print(io, summary(p), ":\n    ", length(p.tsteps),
              " Cooley-Tukey step", ns == 0 ? "s." : ns == 1 ? ": " : "s: ",
              join(map(string, p.tsteps), ", "), "\n    base case: ", p.nstep)
    end
end

size(p::CTPlan) = (p.n,)

# unscaled inverse:
invCT(p::CTPlan{T,forward,Tt,Tn}) where {T,forward,Tt,Tn} =
    CTPlan{T,!forward,Tuple{map(inv,Tt.parameters)...},inv(Tn)}(p.n, map(inv, p.tsteps), inv(p.nstep))

plan_inv(p::CTPlan{T}) where T =
    ScaledPlan(invCT(p), normalization(real(T), p.n, 1))

# steps for pregenerated kernels:
function ωpow(T::Type{<:Complex}, n, i)
    Tr = promote_type(Float64, fieldtype(T, 1))
    twopi_n = -2(π/convert(Tr,n))
    exp((twopi_n*i)*im)
end
struct TwiddleKernelStep{T,r,forward} <: TwiddleStep{T} # radix-r
    m::Int # n / r
    W::Matrix{T}
    TwiddleKernelStep{T,r,forward}(m::Int, W::Matrix{T}) where {T,r,forward} = new(m, W)
    function TwiddleKernelStep{T,r,forward}(n::Int, ωpow=ωpow) where {T,r,forward}
        m = div(n, r)
        W = T[ωpow(T, n, mod(j1*k2,n)) for j1=1:r-1, k2=0:m-1]
        new(m, W)
    end
end
length(ts::TwiddleKernelStep{T,r}) where {T,r} = r
applystep(ts::TwiddleKernelStep{T}, y, y0, ys) where T =
    applystep(ts, ts.m, y, y0, ts.m * ys, ys, ts.W)
inv(ts::TwiddleKernelStep{T,r,forward}) where {T,r,forward} =
    TwiddleKernelStep{T,r,!forward}(ts.m, ts.W)
inv(::Type{TwiddleKernelStep{T,r,forward}}) where {T,r,forward} =
    TwiddleKernelStep{T,r,!forward}
show(io::IO, ts::TwiddleKernelStep{T,r}) where {T,r} = print(io, "radix ", r)

struct NontwiddleKernelStep{T,n,forward} <: NontwiddleStep{T} end
length(ns::NontwiddleKernelStep{T,n}) where {T,n} = n
applystep(ns::NontwiddleKernelStep{T,n}, r::Integer,
          x, x0::Integer, xs::Integer,
          y, y0::Integer, ys::Integer) where {T,n} =
    applystep(ns, r, x,x0,xs*r,xs, y,y0,ys,ys*n)
inv(ns::NontwiddleKernelStep{T,n,forward}) where {T,n,forward} =
    NontwiddleKernelStep{T,n,!forward}()
inv(::Type{NontwiddleKernelStep{T,n,forward}}) where {T,n,forward} =
    NontwiddleKernelStep{T,n,!forward}
show(io::IO, ns::NontwiddleKernelStep{T,n}) where {T,n} =
    print(io, "size ", n, " kernel")

# create null plans for untransformed dimensions and 0-size arrays
struct NullNontwiddleStep{T} <: NontwiddleStep{T} end
applystep(::NullNontwiddleStep, r, x, x0, xs, y, y0, ys) = nothing
show(io::IO, ::NullNontwiddleStep) = print(io, "null transform")
inv(s::NullNontwiddleStep) = s
inv(::Type{NullNontwiddleStep}) = NullNontwiddleStep
CTPlan(T,forward) =
    CTPlan{T,forward,(),NullNontwiddleStep{T}}(0, (), NullNontwiddleStep{T}())

function CTPlan(T::Type, forward::Bool, n::Int; ωpow=ωpow)
    n == 0 && return CTPlan(T,forward)
    factors = fft_factors(T, n)
    m = n
    tsteps = Array{TwiddleStep{T}}(undef, length(factors)-1)
    for i = 1:length(tsteps)
        tsteps[i] = Twiddle(T, m, factors[i], forward; ωpow=ωpow)
        m = tsteps[i].m
    end
    @assert m == factors[end]
    tsteps_ = tuple(tsteps...)
    nt = Nontwiddle(T, m, forward)
    CTPlan{T,forward,Tuple{map(typeof,tsteps_)...},typeof(nt)}(n, tsteps_, nt)
end

plan_fft(x::AbstractVector{Complex{Tr}}) where Tr<:AbstractFloat =
    CTPlan(Complex{Tr}, true, length(x))::CTPlan{Complex{Tr},true}
plan_bfft(x::AbstractVector{Complex{Tr}}) where Tr<:AbstractFloat =
    CTPlan(Complex{Tr}, false, length(x))::CTPlan{Complex{Tr},false}

function applystep(p::CTPlan{T},
                   x, x0, xs,
                   y, y0, ys,
                   step::Int) where T
    nsteps = length(p.tsteps)
    if step > nsteps
        applystep(p.nstep, 1, x,x0,xs, y,y0,ys)
    else
        # decimation in time: perform r DFTs of length m
        tstep = p.tsteps[step]
        m = tstep.m
        r = length(tstep)
        if step == nsteps
            applystep(p.nstep, r, x,x0,xs, y,y0,ys)
        else
            xs_ = xs*r
            x0_ = x0
            y0_ = y0
            for i = 1:r-1
                applystep(p, x,x0_,xs_, y,y0_,ys, step+1)
                x0_ += xs
                y0_ += m
            end
            applystep(p, x,x0_,xs_, y,y0_,ys, step+1)
        end
        # combine sub-transforms with twiddle step:
        applystep(tstep, y,y0,ys)
    end
end

function mul!(y::AbstractVector{T}, p::CTPlan{T}, x::AbstractVector{T}) where T
    p.n == length(y) == length(x) || throw(BoundsError())
    applystep(p, x, firstindex(x), 1, y, firstindex(y), 1, 1)
    return y
end

*(p::CTPlan{T}, x::AbstractVector{T}) where T = mul!(similar(x), p, x)

#############################################################################
# FFT code generation:

# Choose the default radix for pregenerated FFT of length `n`.  To get
# a cache-oblivious algorithm (for register usage), we will choose the
# biggest factor of `n` that is $\le \sqrt{n}$.  However, we also prefer
# relatively prime factors since they allow us to use PFA.
function choose_radix(n)
    # biggest relatively prime factor
    for r = isqrt(n):-1:2
        if n % r == 0 && gcd(r, div(n, r)) == 1
            return r
        end
    end
    # biggest factor <= sqrt(n)
    for r = isqrt(n):-1:2
        if n % r == 0
            return r
        end
    end
    return 1
end

# `twiddle(T, forward, n, k, x)` multiplies the expression `x` by
# $\omega_n^k$, where $\omega_n$ is the `n`-th primitive root of unity
# for the field `T`, returning a new expression.  For complex `T`,
# $\omega_n = \exp(s*2\pi i/n)$, where `s=-1` for `forward=true` and
# `s=+1` for `forward=false`.  (These are traditionally called
# "twiddle factors" in FFT algorithms.) Note also that algebraic
# simplifications occur for several `k`.
#
# (In FFTW's generator, we first generate all expressions in terms of
# real arithmetic and then apply a simplifier to eliminate
# multiplications by `1` and similar.  That is a more powerful
# approach, but this is quicker to implement.)
function twiddle(T, ω_n, forward::Bool, n::Integer, k::Integer, x)
    k == 0 && return x
    2k == n && return :(-$x)
    factor = ω_n^k
    :($factor * $x)
end

function twiddle(T::Type{Complex{Tr}}, ω_n, forward::Bool, n::Integer, k::Integer, x) where Tr<:AbstractFloat
    k == 0 && return x
    2k == n && return :(-$x)
    if 4k == n || 4k == 3n
        tmpvar = gensym(:twiddle)
        if (4k == n && !forward) || (4k == 3n && forward) # ω = +im
            return :(let $tmpvar=$x; complex(-imag($tmpvar), real($tmpvar)); end)
        else  # ω = -im
            return :(let $tmpvar=$x; complex(imag($tmpvar), -real($tmpvar)); end)
        end
    end
    if 8k == n || 8k == 3n || 8k == 5n || 8k == 7n # ω = [(1,-1), (-1,-1), (-1,1), or (1,1)] * sqrt(0.5)
        sqrthalf = sqrt(one(Tr)/2)
        tmpvar = gensym("twiddle")
        if (8k == n && forward) || (8k == 7n && !forward)
            return :(let $tmpvar=$x; $sqrthalf*complex(real($tmpvar)+imag($tmpvar), imag($tmpvar)-real($tmpvar)); end)
        elseif (8k == 3n && forward) || (8k == 5n && !forward)
            return :(let $tmpvar=$x; $sqrthalf*complex(imag($tmpvar)-real($tmpvar), -real($tmpvar)-imag($tmpvar)); end)
        elseif (8k == 5n && forward) || (8k == 3n && !forward)
            return :(let $tmpvar=$x; $sqrthalf*complex(-real($tmpvar)-imag($tmpvar), real($tmpvar)-imag($tmpvar)); end)
        elseif (8k == 7n && forward) || (8k == n && !forward)
            return :(let $tmpvar=$x; $sqrthalf*complex(real($tmpvar)-imag($tmpvar), real($tmpvar)+imag($tmpvar)); end)
        end
    end
    # For complex, we ignore ω_n and compute ω_n^k correctly rounded in high
    # precision instead.
    c, s = setprecision(2*precision(one(Tr))) do
        φ = (big(2) * k) / n
        convert(Tr, cospi(φ)), convert(Tr, sinpi(φ))
    end
    ω = complex(c, forward ? -s : s)
    return :($ω * $x)
end

# Like `fftgen`, below, but generates the naïve $\Theta(n^2)$ DFT algorithm:
function dftgen(T, ω_n, forward::Bool, n::Integer, x, y)
    n == 1 && return :($(y(0)) = $(x(0)))
    tmpvars = Symbol[ gensym(string("dftgen_", j)) for j in 0:n-1 ]
    # JuliaLang/julia #21774
    n == 2 && return Expr(:let,
                          Expr(:block, :($(tmpvars[1]) = $(x(0))), :($(tmpvars[2]) = $(x(1)))),
                          Expr(:block, :($(y(0)) = $(tmpvars[1]) + $(tmpvars[2])), :($(y(1)) = $(tmpvars[1]) - $(tmpvars[2]))))
    # JuliaLang/julia #21774
    Expr(:let,
         Expr(:block, [:($(tmpvars[j+1]) = $(x(j))) for j = 0:n-1]...),
         Expr(:block, [:($(y(k)) = $(Expr(:call, :+, [twiddle(T, ω_n, forward, n, j*k, tmpvars[j+1]) for j in 0:n-1]...))) for k=0:n-1]...))
end

# `fftgen(n, true, x, y)` generates an expression (`Expr`) for an FFT
# of length `n` with inputs `x(i)` and outputs `y(i)` for `i=0:n-1`.
# For `forward=false`, the unscaled backward transform is returned.
# Note that `x` and `y` are *functions* returning expressions.  `T` is
# the type of the field over which we perform the DFT,
# e.g. `Complex{Float64}`.
function fftgen(T, ω_n, forward::Bool, n::Integer, x, y)
    r = choose_radix(n)
    r == 1 && return dftgen(T, ω_n, forward, n, x, y)
    m = div(n, r)

    # store results of first r sub-transforms in r*m temporary variables
    z = Symbol[ gensym(string("fftgen_", j1, "_", k2))
               for j1 = 0:r-1, k2 = 0:m-1 ]

    if gcd(r,m) == 1 # radix-r Prime-Factor (PFA) step
        ri = invmod(r, m)*r
        mi = invmod(m, r)*m
        # get expressions to perform r sub-FFTs of length m
        Fm = Expr[ fftgen(T, ω_n, forward, m,
                          j2 -> x(mod(r*j2+m*j1, n)),
                          k2 -> z[j1+1,k2+1])
                   for j1 = 0:r-1 ]
        # get expressions to perform m sub-FFTs of length r
        Fr = Expr[ fftgen(T, ω_n, forward, r,
                          j1 -> z[j1+1,k2+1],
                          k1 -> y(mod(mi*k1+ri*k2, n)))
                   for k2 in 0:m-1 ]
    else # radix-r Cooley-Tukey step
        # get expressions to perform r sub-FFTs of length m
        Fm = Expr[ fftgen(T, ω_n, forward, m,
                          j2 -> x(r*j2+j1), k2 -> z[j1+1,k2+1])
                   for j1 = 0:r-1 ]
        # get expressions to perform m sub-FFTs of length r
        Fr = Expr[ fftgen(T, ω_n, forward, r,
                          j1 -> twiddle(T, ω_n, forward, n, j1*k2, z[j1+1,k2+1]),
                          k1 -> y(m*k1+k2))
                   for k2 in 0:m-1 ]
    end
    Expr(:block, [Expr(:local, Z) for Z in z]..., Fm..., Fr...)
end
fftgen(T, ω_n, forward::Bool, n::Integer, X::Symbol, Y::Symbol) = fftgen(T, ω_n, forward, n, j -> :($X[$(j+1)]), k -> :($Y[$(k+1)]))

# Analogous to FFTW's nontwiddle codelets (direct solvers), we
# generate a bunch of solvers for small fixed sizes.  Each solver is
# of the form `fft_N(vn, X, x0, xs, xvs, Y, y0, ys, yvs)` and
# computes `i in 0:vn-1` transforms, with the `i`-th transform
# performing `X[x0 + xvs*i + (0:N-1)*xs] = fft(Y[x0 + yvs*i +
# (0:N-1)*ys])`.  Each such solver is generated by the
# `@nontwiddle(T,forward,n)` macro:
macro nontwiddle(args...)
    if length(args) == 4
        T, ω_n, forward, n = args
    else
        T, forward, n = args
        ω_n = ωpow(T, n, 1)
        forward || (ω_n = conj(ω_n))
    end
    @assert isa(T, Type)
    quote
        function FourierTransforms.applystep(ns::NontwiddleKernelStep{T,$n,$forward},
                                  vn::Integer,
                                  X::AbstractArray{T},
                                  x0::Integer, xs::Integer, xvs::Integer,
                                  Y::AbstractArray{T},
                                  y0::Integer, ys::Integer, yvs::Integer) where {T<:$T}
            @inbounds @simd for i in 0:vn-1
                $(fftgen(T, ω_n, forward, n,
                         j -> :(X[(x0 + xvs*i) + xs*$j]),
                         k -> :(Y[(y0 + yvs*i) + ys*$k])))
            end
            Y
        end
    end
end

# compute conj(z) * w
mulconj(z::Complex, w::Complex) =
    Complex(real(z) * real(w) + imag(z) * imag(w),
            real(z) * imag(w) - imag(z) * real(w))

# Analogous to FFTW's twiddle codelets, we also generate solvers that
# perform *in-place* FFTs of small fixed sizes where the data is
# pre-multipled by a precomputed 2d array `W[j+1,i+1]` of twiddle
# factors (with `W[1,_] = 1`).  These are of the form `twiddle_N(vn, X,
# x0, xs, xvs, W)`, and the meaning of the parameter is otherwise
# identical to the nontwiddle codelets with `Y=X`.   The twiddle array
# is the same for both forward and backward transforms, and is conjugated
# as needed.
macro twiddle(args...)
    if length(args) == 4
        T, ω_n, forward, n = args
    else
        T, forward, n = args
        ω_n = ωpow(T, n, 1)
        forward || (ω_n = conj(ω_n))
    end
    @assert isa(T, Type)
    quote
        function FourierTransforms.applystep(ts::TwiddleKernelStep{T,$n,$forward},
                                   vn::Integer,
                                   X::AbstractArray{T},
                                   x0::Integer, xs::Integer, xvs::Integer,
                                   W::AbstractMatrix{T}) where {T<:$T}
            @inbounds @simd for i in 0:vn-1
                $(fftgen(T, ω_n, forward, n,
                         j -> j == 0 ? :(X[(x0 + xvs*i) + xs*$j]) :
                              forward ? :(W[$j,i+1] * X[(x0 + xvs*i) + xs*$j]) : :(mulconj(W[$j,i+1], X[(x0 + xvs*i) + xs*$j])),
                         j -> :(X[(x0 + xvs*i) + xs*$j])))
            end
            X
        end
    end
end

# Now, we will generate nontwiddle and twiddle kernels for a set of
# fixed sizes, to be composed to build an arbitrary-$n$ FFT algorithm.
const fft_kernel_sizes = Set([3, 5:10..., 12, 14, 15, 16, 20, 25, 32])
for forward in (true,false)
    for n in fft_kernel_sizes
        for T in (Float32, Float64)
            @eval @nontwiddle($(Complex{T}), $forward, $n)
            @eval @twiddle($(Complex{T}), $forward, $n)
        end
    end
end
const fft_kernel_sizes_sorted = sort!(Int[n for n in fft_kernel_sizes],
                                      rev=true)

const CTComplex = Union{Complex{Float32},Complex{Float64}} # types of pregenerated kernels

# The above kernels are only for single- and double-precision, since they
# include hard-coded transcendental constants.  The following kernels
# are generic to any floating-point type.
const generic_kernel_sizes = Set([1,2,4])
for n in (1,2,4)
    @eval @nontwiddle($(Complex{<:AbstractFloat}), true, $n)
    @eval @nontwiddle($(Complex{<:AbstractFloat}), false, $n)
end
for forward in (true,false)
    @eval @twiddle($(Complex{<:AbstractFloat}), $forward, 2)
    @eval @twiddle($(Complex{<:AbstractFloat}), $forward, 4)
end

#############################################################################
# Generic solver for arbitrary prime (or nonprime) factors using
# Bluestein's algorithm.  For a reasonable description of this
# algorithm (written mostly by SGJ), see e.g.
#      http://en.wikipedia.org/wiki/Bluestein%27s_FFT_algorithm
# We follow the same notation for the arrays a and b as in that article.

# compute the b_j = b[j+1] array used in Bluestein's algorithm
function bluestein_b(T, ωpow, forward, n, n2)
    b = zeros(T, n2)
    if T <: Complex
        Tr = promote_type(Float64, real(T))
    else
        Tr = T
    end
    # embed inverse-DFT scaling factor 1/n2 in b array:
    scale = inv(cbrt(convert(Tr,n2)))
    b[1] = scale
    for i = 1:n-1
        b[i+1] = b[n2-i+1] = ωpow(T, 2n, (-1)^forward * mod(i*i, 2n)) * scale
    end
    return b
end

struct NontwiddleBluesteinStep{T} <: NontwiddleStep{T}
    n::Int   # DFT size to be computed
    n2::Int  # nextpow(2, 2n-1)
    p::Plan{T}  # plan for DFT of length n2

    # the following arrays are of length n2, used to compute the convolution
    a::Vector{T} # storage for x[j] * b[j]', zero-padded
    A::Vector{T} # storage to be used for DFT of a
    b::Vector{T} # b[j+1] = exp(πi j^2 / n), wrapped symmetrically
    B::Vector{T} # DFT(b)

    forward::Bool

    function NontwiddleBluesteinStep{T}(n::Int, ωpow, forward::Bool) where T
        n2 = nextpow(2, 2n-1)
        a = Array{T}(undef, n2)
        A = Array{T}(undef, n2)
        p = CTPlan(T, true, n2; ωpow=ωpow)
        b = bluestein_b(T, ωpow, forward, n, n2)
        B = p * b
        new(n, n2, p, a, A, b, B, forward)
    end
end

length(s::NontwiddleBluesteinStep) = s.n

inv(s::NontwiddleBluesteinStep{T}) where T =
    NontwiddleBluesteinStep{T}(s.n, ωpow, !s.forward)
inv(S::Type{NontwiddleBluesteinStep{T}}) where T = S

show(io::IO, s::NontwiddleBluesteinStep) =
    print(io, "size ", s.n, " Bluestein-", s.n2)

function applystep(ns::NontwiddleBluesteinStep{T}, r,
                   x::AbstractArray{T}, x0, xs,
                   y::AbstractArray{T}, y0, ys) where T
    a = ns.a
    b = ns.b
    A = ns.A
    B = ns.B
    z = zero(T)
    xs_ = xs*r
    ys_ = ys*ns.n
    for i = 1:r
        @inbounds for j = 1:ns.n
            a[j] = x[x0 + xs_*(j-1)] * b[j]'
        end
        @inbounds for j = ns.n+1:ns.n2
            a[j] = z
        end
        # conv(a,b) = ifft(fft(a) .* B), where ifft -> bfft because
        # the 1/n2 scaling was included in b and B, and we use the
        # identity bfft(x) = conj(fft(conj(x))) so that we can re-use ns.p:
        mul!(A, ns.p, a)
        @inbounds for j = 1:ns.n2
            A[j] = (A[j] * B[j])'
        end
        mul!(a, ns.p, A)
        @inbounds for j = 1:ns.n
            y[y0 + ys*(j-1)] = (b[j] * a[j])'
        end
        x0 += xs
        y0 += ys_
    end
end

# Similar to above, but a Bluestein-based twiddle step:

struct TwiddleBluesteinStep{T} <: TwiddleStep{T}
    r::Int # radix
    m::Int # n / r
    W::Array{T} # twiddle factors

    r2::Int  # nextpow(2, 2r-1)
    p::Plan{T}  # plan for DFT of length r2

    # the following arrays are of length r2, used to compute the convolution
    a::Vector{T} # storage for x[j] * b[j]', zero-padded
    A::Vector{T} # storage to be used for DFT of a
    b::Vector{T} # b[j+1] = exp(πi j^2 / n), wrapped symmetrically
    B::Vector{T} # DFT(b)

    forward::Bool

    function TwiddleBluesteinStep{T}(n::Int, ωpow, r::Int, forward::Bool) where T
        m = div(n, r)
        Tr = promote_type(Float64, real(T))
        r2 = nextpow(2, 2r-1)
        a = Array{T}(undef, r2)
        A = Array{T}(undef, r2)
        p = CTPlan(T, true, r2; ωpow=ωpow)
        b = bluestein_b(T, ωpow, forward, r, r2)
        B = p * b
        new(r, m,
            T[ωpow(T, n, (-1)^forward * mod(j1*k2,n)) for j1=1:r-1, k2=0:m-1],
            r2, p, a, A, b, B, forward)
    end
end

length(s::TwiddleBluesteinStep) = s.r

show(io::IO, s::TwiddleBluesteinStep) =
    print(io, "radix ", s.r, " Bluestein-", s.r2)

inv(s::TwiddleBluesteinStep{T}) where T =
    TwiddleBluesteinStep{T}(s.r*s.m, ωpow, s.r, !s.forward)
inv(S::Type{TwiddleBluesteinStep{T}}) where T = S

function applystep(ts::TwiddleBluesteinStep{T}, y::AbstractArray{T}, y0,ys) where T
    W = ts.W
    a = ts.a
    b = ts.b
    A = ts.A
    B = ts.B
    z = zero(T)
    ys_ = ys*ts.m
    for i = 1:ts.m
        a[1] = y[y0] * b[1]'
        @inbounds for j = 2:ts.r
            a[j] = W[j-1,i] * y[y0 + ys_*(j-1)] * b[j]'
        end
        @inbounds for j = ts.r+1:ts.r2
            a[j] = z
        end
        # conv(a,b) = ifft(fft(a) .* B), where ifft -> bfft because
        # the 1/n2 scaling was included in b and B, and we use the
        # identity bfft(x) = conj(fft(conj(x))) so that we can re-use ts.p:
        mul!(A, ts.p, a)
        @inbounds for j = 1:ts.r2
            A[j] = (A[j] * B[j])'
        end
        mul!(a, ts.p, A)
        @inbounds for j = 1:ts.r
            y[y0 + ys_*(j-1)] = (b[j] * a[j])'
        end
        y0 += ys
    end
end

#############################################################################
# Step selection for CTPlan:

Nontwiddle(T, n::Int, forw::Bool; ωpow=ωpow) = (T <: CTComplex && n in fft_kernel_sizes) || n in generic_kernel_sizes ? NontwiddleKernelStep{T,n,forw}() : NontwiddleBluesteinStep{T}(n, ωpow, forw)
Twiddle(T, n::Int, r::Int, forw::Bool; ωpow=ωpow) = (T <: CTComplex && r in fft_kernel_sizes) || r == 4 || r == 2 ? TwiddleKernelStep{T,r,forw}(n, ωpow) : TwiddleBluesteinStep{T}(n, ωpow, r, forw)

# Break n into a series of factors for type T, avoiding small kernels
# if possible
function fft_factors(T::Type, n::Integer)
    factors = Int[]
    if n == 1
        push!(factors, 1)
    else
        m = n
        if T <: CTComplex
            for r in fft_kernel_sizes_sorted
                while m % r == 0
                    push!(factors, r)
                    m = div(m, r)
                end
                m == 1 && break
            end
        end
        # generic radix-4 and radix-2 twiddle kernels:
        while m % 4 == 0
            push!(factors, 4)
            m = div(m, 4)
        end
        if iseven(m)
            push!(factors, 2)
            m = div(m, 2)
        end
        # sometimes there will be a small factor (e.g. 2) left over at the end;
        # try to combine this with larger earlier factors if possible:
        if length(factors) > 1
            for i = 1:length(factors)-1
                factors[end] >= 16 && break
                while factors[i] % 2 == 0 && div(factors[i], 2) > factors[end]
                    factors[i] = div(factors[i], 2)
                    factors[end] *= 2
                end
            end
        end
        # get any leftover prime factors:
        for (f,k) in factor(m)
            for i=1:k
                push!(factors, f)
            end
        end
    end
    factors
end


text:
using PyPlot
using JLD
function plot_unstable_sens()
	X = load("../data/obj_erg_avg/cos4y_s1.jld")
	s1_arr = X["s1"]
	J_arr = X["J"]
	fig, ax = subplots(1,1)
	ax.plot(s1_arr, J_arr, ".", ms=10.0)
    ax.xaxis.set_tick_params(labelsize=32)
    ax.yaxis.set_tick_params(labelsize=32)
    ax.set_xlabel(L"$s_1$",fontsize=32)
    ax.set_ylabel(L"$\langle J\rangle$",fontsize=32)
	ax.grid(true)

	X = load("../data/unstable_sens/dJds1_test.jld")
	dJds = X["dJds"]
	s1 = X["s1"]

	X = load("../data/obj_erg_avg/cos4y_s1_sens.jld")
	J = X["J"]

	eps = 2e-2
	n = size(dJds)[1]
	J_pts = reshape([J .- eps*dJds J .+ eps*dJds], n,
					2)'
	s_pts = reshape([s1 .- eps s1 .+ eps], n,
					2)'

	ax.plot(s_pts[:,1:2:end-1], J_pts[:,1:2:end-1], "k",lw=6.0)
	ax.plot(s_pts[:,end], J_pts[:,end],"k",lw=6.0,label=L"$d_s\langle J\rangle$ from S3")
	ax.legend(fontsize=32)
	return dJds

end

ext: jl

max_stars_repo_name: nishaChandramoorthy/linearResponse

max_issues_repo_name: nishaChandramoorthy/linearResponse

max_forks_repo_name: nishaChandramoorthy/linearResponse


text:
#=
Takes two sets of scenarios: demand and DR reliability - and combines them.
Creates pro, vdr, and vdem csv files.
Inputs:
dr_scenarios: the response of DR in each timestep for each scenario, as a fraction of desired response
dr_p: the probability of each scenario in dr_scenarios
nd_scenarios: total demand in each timestep for each scenario
nd_p: probability of each scenario in nd_scenarios
This function is called by the main model (ercot_stoch.jl)
combine_scenarios.jl
Sept 2019, Patricia Levi
=#
function combine_scenarios(dr_scenarios, dr_p, nd_scenarios, nd_p)

    if size(dr_scenarios,1) != size(nd_scenarios,1)
        error("DR and Demand scenarios must cover same number of timesteps")
    end

    timesteps = size(dr_scenarios,1)
    n_new_scenarios = size(dr_scenarios,2) * size(nd_scenarios,2)
    print("number of new scenarios is",n_new_scenarios)

    vdr = Array{Float64}(timesteps,n_new_scenarios)
    vnd = Array{Float64}(timesteps,n_new_scenarios)
    prob_array = Array{Float64}(n_new_scenarios)

    dr_ind_array = repeat(1:size(dr_scenarios,2),outer = size(nd_scenarios,2))
    nd_ind_array = repeat(1:size(nd_scenarios,2),inner = size(dr_scenarios,2))
    for i in 1:n_new_scenarios
        println("i is ",i)
        #ID which dr scenario belongs here
        dr_ind = dr_ind_array[i]
        println("dr ind is ", dr_ind)
        #ID which nd scenario belongs here
        nd_ind = nd_ind_array[i]
        println("nd_ind is ", nd_ind)
        #
        vdr[:,i] = dr_scenarios[:,dr_ind]
        vnd[:,i] = nd_scenarios[:,nd_ind]
        prob_array[i] = dr_p[dr_ind] * nd_p[nd_ind]

    end

    # normalize prob_array
    prob_array = prob_array/sum(prob_array)

    return(vdr,vnd,prob_array)
end

max_stars_repo_name: pajalevi/Stoch-UC-model-for-DR

max_issues_repo_name: pajalevi/Stoch-UC-model-for-DR

max_forks_repo_name: pajalevi/Stoch-UC-model-for-DR


text:
# SPD-License-Identifier: MIT

using AutoGrad

function _lfmmi_loss(ϕ, numfsm, denfsm, seqlengths)
    GC.gc()
    CUDA.reclaim()
    ϕ = clamp.(ϕ, -30, 30)
    γ_num, ttl_num = pdfposteriors(numfsm, ϕ, seqlengths)
    γ_den, ttl_den = pdfposteriors(denfsm, ϕ, seqlengths)
    K, N, B = size(ϕ)
    loss = -sum((ttl_num .- ttl_den))
    loss, γ_num, γ_den
end

function _∇lfmmi_loss(dy, loss, γ_num, γ_den)
    retval = -(γ_num .- γ_den)
end

@primitive1 _lfmmi_loss(ϕ, numf, denf, slen),dy,y (dy[1] * _∇lfmmi_loss(dy, y...))

lfmmi_loss(ϕ, numfsm, denfsm, seqlengths) =
    _lfmmi_loss(ϕ, numfsm, denfsm, seqlengths)[1] # return the loss only

max_stars_repo_name: MartinKocour/SpeechLab

max_issues_repo_name: MartinKocour/SpeechLab

max_forks_repo_name: MartinKocour/SpeechLab


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

using Markdown
using InteractiveUtils

# ╔═╡ 06a7ec74-0684-4974-aa00-1c12dc3acc7b
begin
	using Plots
	using DelimitedFiles
	using Random; Random.seed!()
	using Printf
	"using packages"
end

# ╔═╡ ce825258-41b1-4c40-91f7-05d851b248e2
function Masubara_freq(n::Int64, β::Real; type::Symbol= :f)
    if type == :b  N = 2n
    elseif type == :f  N = 2n + 1
    else @error "type should be :b for bosons and :f for fermions"
    end
    return N*π/β
end

# ╔═╡ 9cbcd233-c37f-49f6-a6bf-5d92cf744972
function masubara(n::Int64, ϵ::Real, β::Real)
	ωn = Masubara_freq(n,β)
	return 1/(1.0im * ωn - ϵ)
end

# ╔═╡ 74ae31c5-545b-4b82-b9f0-1d2b7cccaa04
function Masubara_GF(n::Int64, A::Function, β::Real, type::Symbol;
    Λ::Float64=10., L::Int64=1000000)
    # G(iωn) = 1/2π ∫dΩ A(Ω)/(iωn - Ω)
    ωn = Masubara_freq(n, β, type=type)
    dL = 2*Λ/L
    Gn = 0.
    for n = 0:L
        ω = -Λ + dL*n
        Gn += A(ω)/(1.0im*ωn-ω) * dL
    end
    return Gn/(2π)
end

# ╔═╡ 1474d231-7d61-4a6d-80f8-2dd89f039168
function delta(x::Real; c::Real = 0., η::Real = 0.05)
    num = η
    den = ((x-c)^2 + η^2) * π
    return num/den
end

# ╔═╡ 7cda46b1-0a59-47f0-b612-6b877de3ecb6
function gaussian(x::Real, fac::AbstractVector)
    # f(x) = 1/(σ√(2π)) exp(-((x-μ)/σ)^2 /2 )
    μ = fac[1]; σ2 = fac[2]
    σ2 <= 0. ? (@error "σ2 should be positive.") :
    norm = √(2π * σ2)
    m = (x-μ)^2 / σ2
    return 1/norm*exp(-m/2)
end

# ╔═╡ c1758648-ae67-4ce6-9f5f-7d1b8512445e
function multi_gaussian(N::Int64; ms::Vector{Float64}=[-3.,3.], smax::Real=3.)
    fac = rand(N,2)
    fac[:,1] = rand(N) .* (ms[2] - ms[1]) .+ ms[1]
    fac[:,2] = rand(N) .* smax
    function f(x::Real)
        res = 0.
        for i = 1: N
            res += gaussian(x, fac[i,:])
        end
        res/N
    end
    return f, fac
end

# ╔═╡ e25846d0-fe93-4f2e-b500-9f218998af08
β = 100

# ╔═╡ e720ee46-b4c8-4539-b598-5655959f51f5
ϵ = 1.0

# ╔═╡ 2d0673e6-1cbf-4435-9d29-dd4303f36358
N = [i for i=1:36]

# ╔═╡ 5fec2475-8abf-4572-8d4c-58838e54a170
freq = [Masubara_freq(i,β,type=:f) for i in N]

# ╔═╡ e3ae6751-dcad-49d4-9ff9-8dbc7e42fcb8
md"""
Generate data:

$$H = \epsilon c^\dagger c$$
$$G(i\omega_n) = \frac{1}{i\omega_n - \epsilon}$$
$$A(\omega) = 2\pi \delta(\omega - \epsilon)$$
"""

# ╔═╡ f1228fa1-a310-496d-8a86-6ba8de8b4575
g1 = [1/(1.0im*w - ϵ) for w in freq]

# ╔═╡ 37ae6364-6465-4368-9bbd-b05e3b30497b
begin
	open("../References/run/input_delta.txt", "w") do file
		istring = @sprintf "idelta.txt %i odelta.txt" length(N)
		write(file,istring)
	end
	"input file"
end

# ╔═╡ c333b147-9776-4a79-a50e-564590115a17
begin
	open("../References/run/idelta.txt", "w") do file
		for i =1:length(N)
			writedlm(file,[freq[i] real(g1[i]) imag(g1[i])])
		end
	end
	"ifile.txt"
end

# ╔═╡ ebd94ab6-bdc8-49fd-bde0-ac573c5b3bae
md"""
Gaussian
"""

# ╔═╡ 3428ee85-399c-441e-a3e9-e3acac392734
omega=[i for i in range(-5,5,length=100)]

# ╔═╡ 8403d091-a0b4-4540-9fea-4c8d9e7e9fbe
A1, fac1 = multi_gaussian(1)

# ╔═╡ 541dc0b0-2fac-49b3-bfdd-8575aa2c056b
begin
	y1 = [A1(x) for x in omega]
	plot(omega, y1, lw=2, label=false)
	plot!(xlabel="ω", ylabel = "A(ω)")
end

# ╔═╡ 65938662-b78e-4713-bb21-4715105aeed4
begin
	open("../References/run/Ag1.txt", "w") do file
		for i =1:length(omega)
			writedlm(file,[omega[i] y1[i]])
		end
	end
	"Afile.txt"
end

# ╔═╡ dc06a8da-da64-4468-8e56-c544ef71d408
begin
	open("../References/run/input_g1.txt", "w") do file
		istring = @sprintf "ig1.txt %i og1.txt" length(N)
		write(file,istring)
	end
	"input file"
end

# ╔═╡ 8245d16b-81d7-41a6-9197-0c7aaabbeaad
g2 = [Masubara_GF(i,A1,β,:f) for i in N]

# ╔═╡ 944d8b32-73bb-44b9-af06-c8aa6202bf5c
begin
	open("../References/run/ig1.txt", "w") do file
		for i =1:length(N)
			writedlm(file,[freq[i] real(g2[i]) imag(g2[i])])
		end
	end
	"ifile.txt"
end

# ╔═╡ 5a46bfa5-f0b0-45de-bcaf-2e1619cd9c49
md"""
N=3 Gaussian
"""

# ╔═╡ 74686cc2-9214-42d6-9d35-2ed4bf63b43a
A3, fac3 = multi_gaussian(3)

# ╔═╡ c13a426a-2805-41ae-b95f-259baacf6d33
begin
	y3 = [A3(x) for x in omega]
	plot(omega, y3, lw=2, label=false)
	plot!(xlabel="ω", ylabel = "A(ω)")
end

# ╔═╡ bd203a88-79d3-42ff-ac53-9b264d53f1f7
begin
	open("../References/run/Ag3.txt", "w") do file
		for i =1:length(omega)
			writedlm(file,[omega[i] y3[i]])
		end
	end
	"Afile.txt"
end

# ╔═╡ 21b0b5ae-b058-4239-813b-bd7f6967d7c1
begin
	open("../References/run/input_g3.txt", "w") do file
		istring = @sprintf "ig3.txt %i og3.txt" length(N)
		write(file,istring)
	end
	"input file"
end

# ╔═╡ 824527d8-e9f4-4363-8db4-d1de26b2d256
g3 = [Masubara_GF(i,A3,β,:f) for i in N]

# ╔═╡ fc8f5f1d-0541-49f2-93d0-6badc50c6a19
begin
	open("../References/run/ig3.txt", "w") do file
		for i =1:length(N)
			writedlm(file,[freq[i] real(g3[i]) imag(g3[i])])
		end
	end
	"ifile.txt"
end

# ╔═╡ 959b5fd8-84d3-46cd-baed-c3d25d8c187f
md"""
$G(iω_n) = \frac{1}{2π} ∫dΩ \frac{A(Ω)}{(iω_n - Ω)}$

如何写积分？
"""

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Plots = "~1.22.0"
"""

# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised

[[Adapt]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "84918055d15b3114ede17ac6a7182f68870c16f7"
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
version = "3.3.1"

[[ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"

[[Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[Bzip2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2"
uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
version = "1.0.8+0"

[[Cairo_jll]]
deps = ["Artifacts", "Bzip2_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "f2202b55d816427cd385a9a4f3ffb226bee80f99"
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
version = "1.16.1+0"

[[ColorSchemes]]
deps = ["ColorTypes", "Colors", "FixedPointNumbers", "Random"]
git-tree-sha1 = "9995eb3977fbf67b86d0a0a0508e83017ded03f2"
uuid = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
version = "3.14.0"

[[ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "024fe24d83e4a5bf5fc80501a314ce0d1aa35597"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.11.0"

[[Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "Reexport"]
git-tree-sha1 = "417b0ed7b8b838aa6ca0a87aadf1bb9eb111ce40"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.12.8"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "4866e381721b30fac8dda4c8cb1d9db45c8d2994"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.37.0"

[[CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"

[[Contour]]
deps = ["StaticArrays"]
git-tree-sha1 = "9f02045d934dc030edad45944ea80dbd1f0ebea7"
uuid = "d38c429a-6771-53c6-b99e-75d170b6e991"
version = "0.5.7"

[[DataAPI]]
git-tree-sha1 = "bec2532f8adb82005476c141ec23e921fc20971b"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.8.0"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "7d9d316f04214f7efdbb6398d545446e246eff02"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.10"

[[DataValueInterfaces]]
git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6"
uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464"
version = "1.0.0"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[EarCut_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "3f3a2501fa7236e9b911e0f7a588c657e822bb6d"
uuid = "5ae413db-bbd1-5e63-b57d-d24a61df00f5"
version = "2.2.3+0"

[[Expat_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "b3bfd02e98aedfa5cf885665493c5598c350cd2f"
uuid = "2e619515-83b5-522b-bb60-26c02a35a201"
version = "2.2.10+0"

[[FFMPEG]]
deps = ["FFMPEG_jll"]
git-tree-sha1 = "b57e3acbe22f8484b4b5ff66a7499717fe1a9cc8"
uuid = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
version = "0.4.1"

[[FFMPEG_jll]]
deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "JLLWrappers", "LAME_jll", "Libdl", "Ogg_jll", "OpenSSL_jll", "Opus_jll", "Pkg", "Zlib_jll", "libass_jll", "libfdk_aac_jll", "libvorbis_jll", "x264_jll", "x265_jll"]
git-tree-sha1 = "d8a578692e3077ac998b50c0217dfd67f21d1e5f"
uuid = "b22a6f82-2f65-5046-a5b2-351ab43fb4e5"
version = "4.4.0+0"

[[FixedPointNumbers]]
deps = ["Statistics"]
git-tree-sha1 = "335bfdceacc84c5cdf16aadc768aa5ddfc5383cc"
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.8.4"

[[Fontconfig_jll]]
deps = ["Artifacts", "Bzip2_jll", "Expat_jll", "FreeType2_jll", "JLLWrappers", "Libdl", "Libuuid_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "21efd19106a55620a188615da6d3d06cd7f6ee03"
uuid = "a3f928ae-7b40-5064-980b-68af3947d34b"
version = "2.13.93+0"

[[Formatting]]
deps = ["Printf"]
git-tree-sha1 = "8339d61043228fdd3eb658d86c926cb282ae72a8"
uuid = "59287772-0a20-5a39-b81b-1366585eb4c0"
version = "0.4.2"

[[FreeType2_jll]]
deps = ["Artifacts", "Bzip2_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "87eb71354d8ec1a96d4a7636bd57a7347dde3ef9"
uuid = "d7e528f0-a631-5988-bf34-fe36492bcfd7"
version = "2.10.4+0"

[[FriBidi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "aa31987c2ba8704e23c6c8ba8a4f769d5d7e4f91"
uuid = "559328eb-81f9-559d-9380-de523a88c83c"
version = "1.0.10+0"

[[GLFW_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libglvnd_jll", "Pkg", "Xorg_libXcursor_jll", "Xorg_libXi_jll", "Xorg_libXinerama_jll", "Xorg_libXrandr_jll"]
git-tree-sha1 = "dba1e8614e98949abfa60480b13653813d8f0157"
uuid = "0656b61e-2033-5cc2-a64a-77c0f6c09b89"
version = "3.3.5+0"

[[GR]]
deps = ["Base64", "DelimitedFiles", "GR_jll", "HTTP", "JSON", "Libdl", "LinearAlgebra", "Pkg", "Printf", "Random", "Serialization", "Sockets", "Test", "UUIDs"]
git-tree-sha1 = "c2178cfbc0a5a552e16d097fae508f2024de61a3"
uuid = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
version = "0.59.0"

[[GR_jll]]
deps = ["Artifacts", "Bzip2_jll", "Cairo_jll", "FFMPEG_jll", "Fontconfig_jll", "GLFW_jll", "JLLWrappers", "JpegTurbo_jll", "Libdl", "Libtiff_jll", "Pixman_jll", "Pkg", "Qt5Base_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "ef49a187604f865f4708c90e3f431890724e9012"
uuid = "d2c73de3-f751-5644-a686-071e5b155ba9"
version = "0.59.0+0"

[[GeometryBasics]]
deps = ["EarCut_jll", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"]
git-tree-sha1 = "58bcdf5ebc057b085e58d95c138725628dd7453c"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
version = "0.4.1"

[[Gettext_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046"
uuid = "78b55507-aeef-58d4-861c-77aaff3498b1"
version = "0.21.0+0"

[[Glib_jll]]
deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "7bf67e9a481712b3dbe9cb3dac852dc4b1162e02"
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
version = "2.68.3+0"

[[Graphite2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "344bf40dcab1073aca04aa0df4fb092f920e4011"
uuid = "3b182d85-2403-5c21-9c21-1e1f0cc25472"
version = "1.3.14+0"

[[Grisu]]
git-tree-sha1 = "53bb909d1151e57e2484c3d1b53e19552b887fb2"
uuid = "42e2da0e-8278-4e71-bc24-59509adca0fe"
version = "1.0.2"

[[HTTP]]
deps = ["Base64", "Dates", "IniFile", "Logging", "MbedTLS", "NetworkOptions", "Sockets", "URIs"]
git-tree-sha1 = "60ed5f1643927479f845b0135bb369b031b541fa"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
version = "0.9.14"

[[HarfBuzz_jll]]
deps = ["Artifacts", "Cairo_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "Graphite2_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg"]
git-tree-sha1 = "8a954fed8ac097d5be04921d595f741115c1b2ad"
uuid = "2e76f6c2-a576-52d4-95c1-20adfe4de566"
version = "2.8.1+0"

[[IniFile]]
deps = ["Test"]
git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8"
uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f"
version = "0.5.0"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[IterTools]]
git-tree-sha1 = "05110a2ab1fc5f932622ffea2a003221f4782c18"
uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
version = "1.3.0"

[[IteratorInterfaceExtensions]]
git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856"
uuid = "82899510-4779-5014-852e-03e436cf321d"
version = "1.0.0"

[[JLLWrappers]]
deps = ["Preferences"]
git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.3.0"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "8076680b162ada2a031f707ac7b4953e30667a37"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.2"

[[JpegTurbo_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "d735490ac75c5cb9f1b00d8b5509c11984dc6943"
uuid = "aacddb02-875f-59d6-b918-886e6ef4fbf8"
version = "2.1.0+0"

[[LAME_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "f6250b16881adf048549549fba48b1161acdac8c"
uuid = "c1c5ebd0-6772-5130-a774-d5fcae4a789d"
version = "3.100.1+0"

[[LZO_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "e5b909bcf985c5e2605737d2ce278ed791b89be6"
uuid = "dd4b983a-f0e5-5f8d-a1b7-129d4a5fb1ac"
version = "2.10.1+0"

[[LaTeXStrings]]
git-tree-sha1 = "c7f1c695e06c01b95a67f0cd1d34994f3e7db104"
uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
version = "1.2.1"

[[Latexify]]
deps = ["Formatting", "InteractiveUtils", "LaTeXStrings", "MacroTools", "Markdown", "Printf", "Requires"]
git-tree-sha1 = "a4b12a1bd2ebade87891ab7e36fdbce582301a92"
uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
version = "0.15.6"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[Libffi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "761a393aeccd6aa92ec3515e428c26bf99575b3b"
uuid = "e9f186c6-92d2-5b65-8a66-fee21dc1b490"
version = "3.2.2+0"

[[Libgcrypt_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgpg_error_jll", "Pkg"]
git-tree-sha1 = "64613c82a59c120435c067c2b809fc61cf5166ae"
uuid = "d4300ac3-e22c-5743-9152-c294e39db1e4"
version = "1.8.7+0"

[[Libglvnd_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll", "Xorg_libXext_jll"]
git-tree-sha1 = "7739f837d6447403596a75d19ed01fd08d6f56bf"
uuid = "7e76a0d4-f3c7-5321-8279-8d96eeed0f29"
version = "1.3.0+3"

[[Libgpg_error_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "c333716e46366857753e273ce6a69ee0945a6db9"
uuid = "7add5ba3-2f88-524e-9cd5-f83b8a55f7b8"
version = "1.42.0+0"

[[Libiconv_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.16.1+1"

[[Libmount_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "9c30530bf0effd46e15e0fdcf2b8636e78cbbd73"
uuid = "4b2f31a3-9ecc-558c-b454-b3730dcb73e9"
version = "2.35.0+0"

[[Libtiff_jll]]
deps = ["Artifacts", "JLLWrappers", "JpegTurbo_jll", "Libdl", "Pkg", "Zlib_jll", "Zstd_jll"]
git-tree-sha1 = "340e257aada13f95f98ee352d316c3bed37c8ab9"
uuid = "89763e89-9b03-5906-acba-b20f662cd828"
version = "4.3.0+0"

[[Libuuid_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "7f3efec06033682db852f8b3bc3c1d2b0a0ab066"
uuid = "38a345b3-de98-5d2b-a5d3-14cd9215e700"
version = "2.36.0+0"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[MacroTools]]
deps = ["Markdown", "Random"]
git-tree-sha1 = "5a5bc6bf062f0f95e62d0fe0a2d99699fed82dd9"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.8"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MbedTLS]]
deps = ["Dates", "MbedTLS_jll", "Random", "Sockets"]
git-tree-sha1 = "1c38e51c3d08ef2278062ebceade0e46cefc96fe"
uuid = "739be429-bea8-5141-9913-cc70e7f3736d"
version = "1.0.3"

[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[Measures]]
git-tree-sha1 = "e498ddeee6f9fdb4551ce855a46f54dbd900245f"
uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
version = "0.3.1"

[[Missings]]
deps = ["DataAPI"]
git-tree-sha1 = "bf210ce90b6c9eed32d25dbcae1ebc565df2687f"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "1.0.2"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[NaNMath]]
git-tree-sha1 = "bfe47e760d60b82b66b61d2d44128b62e3a369fb"
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
version = "0.3.5"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[Ogg_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "7937eda4681660b4d6aeeecc2f7e1c81c8ee4e2f"
uuid = "e7412a2a-1a6e-54c0-be00-318e2571c051"
version = "1.3.5+0"

[[OpenSSL_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "15003dcb7d8db3c6c857fda14891a539a8f2705a"
uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95"
version = "1.1.10+0"

[[Opus_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "51a08fb14ec28da2ec7a927c4337e4332c2a4720"
uuid = "91d4177d-7536-5919-b921-800302f37372"
version = "1.3.2+0"

[[OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.1"

[[PCRE_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "b2a7af664e098055a7529ad1a900ded962bca488"
uuid = "2f80f16e-611a-54ab-bc61-aa92de5b98fc"
version = "8.44.0+0"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "438d35d2d95ae2c5e8780b330592b6de8494e779"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.0.3"

[[Pixman_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "b4f5d02549a10e20780a24fce72bea96b6329e29"
uuid = "30392449-352a-5448-841d-b1acce4e97dc"
version = "0.40.1+0"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[PlotThemes]]
deps = ["PlotUtils", "Requires", "Statistics"]
git-tree-sha1 = "a3a964ce9dc7898193536002a6dd892b1b5a6f1d"
uuid = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a"
version = "2.0.1"

[[PlotUtils]]
deps = ["ColorSchemes", "Colors", "Dates", "Printf", "Random", "Reexport", "Statistics"]
git-tree-sha1 = "2537ed3c0ed5e03896927187f5f2ee6a4ab342db"
uuid = "995b91a9-d308-5afd-9ec6-746e21dbc043"
version = "1.0.14"

[[Plots]]
deps = ["Base64", "Contour", "Dates", "Downloads", "FFMPEG", "FixedPointNumbers", "GR", "GeometryBasics", "JSON", "Latexify", "LinearAlgebra", "Measures", "NaNMath", "PlotThemes", "PlotUtils", "Printf", "REPL", "Random", "RecipesBase", "RecipesPipeline", "Reexport", "Requires", "Scratch", "Showoff", "SparseArrays", "Statistics", "StatsBase", "UUIDs"]
git-tree-sha1 = "b1a708d607125196ea1acf7264ee1118ce66931b"
uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
version = "1.22.0"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.2"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[Qt5Base_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Fontconfig_jll", "Glib_jll", "JLLWrappers", "Libdl", "Libglvnd_jll", "OpenSSL_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libxcb_jll", "Xorg_xcb_util_image_jll", "Xorg_xcb_util_keysyms_jll", "Xorg_xcb_util_renderutil_jll", "Xorg_xcb_util_wm_jll", "Zlib_jll", "xkbcommon_jll"]
git-tree-sha1 = "ad368663a5e20dbb8d6dc2fddeefe4dae0781ae8"
uuid = "ea2cea3b-5b76-57ae-a6ef-0a8af62496e1"
version = "5.15.3+0"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[RecipesBase]]
git-tree-sha1 = "44a75aa7a527910ee3d1751d1f0e4148698add9e"
uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
version = "1.1.2"

[[RecipesPipeline]]
deps = ["Dates", "NaNMath", "PlotUtils", "RecipesBase"]
git-tree-sha1 = "7ad0dfa8d03b7bcf8c597f59f5292801730c55b8"
uuid = "01d81517-befc-4cb6-b9ec-a95719d0359c"
version = "0.4.1"

[[Reexport]]
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "1.2.2"

[[Requires]]
deps = ["UUIDs"]
git-tree-sha1 = "4036a3bd08ac7e968e27c203d45f5fff15020621"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.1.3"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Scratch]]
deps = ["Dates"]
git-tree-sha1 = "0b4b7f1393cff97c33891da2a0bf69c6ed241fda"
uuid = "6c6a2e73-6563-6170-7368-637461726353"
version = "1.1.0"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Showoff]]
deps = ["Dates", "Grisu"]
git-tree-sha1 = "91eddf657aca81df9ae6ceb20b959ae5653ad1de"
uuid = "992d4aef-0814-514b-bc4d-f2e9a6c4116f"
version = "1.0.3"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SortingAlgorithms]]
deps = ["DataStructures"]
git-tree-sha1 = "b3363d7460f7d098ca0912c69b082f75625d7508"
uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
version = "1.0.1"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "3240808c6d463ac46f1c1cd7638375cd22abbccb"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.2.12"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[StatsAPI]]
git-tree-sha1 = "1958272568dc176a1d881acb797beb909c785510"
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
version = "1.0.0"

[[StatsBase]]
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics", "StatsAPI"]
git-tree-sha1 = "8cbbc098554648c84f79a463c9ff0fd277144b6c"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.33.10"

[[StructArrays]]
deps = ["Adapt", "DataAPI", "StaticArrays", "Tables"]
git-tree-sha1 = "f41020e84127781af49fc12b7e92becd7f5dd0ba"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.2"

[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[TableTraits]]
deps = ["IteratorInterfaceExtensions"]
git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39"
uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
version = "1.0.1"

[[Tables]]
deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"]
git-tree-sha1 = "1162ce4a6c4b7e31e0e6b14486a6986951c73be9"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
version = "1.5.2"

[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[URIs]]
git-tree-sha1 = "97bbe755a53fe859669cd907f2d96aee8d2c1355"
uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
version = "1.3.0"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[Wayland_jll]]
deps = ["Artifacts", "Expat_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "3e61f0b86f90dacb0bc0e73a0c5a83f6a8636e23"
uuid = "a2964d1f-97da-50d4-b82a-358c7fce9d89"
version = "1.19.0+0"

[[Wayland_protocols_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Wayland_jll"]
git-tree-sha1 = "2839f1c1296940218e35df0bbb220f2a79686670"
uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91"
version = "1.18.0+4"

[[XML2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "1acf5bdf07aa0907e0a37d3718bb88d4b687b74a"
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"
version = "2.9.12+0"

[[XSLT_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "Pkg", "XML2_jll", "Zlib_jll"]
git-tree-sha1 = "91844873c4085240b95e795f692c4cec4d805f8a"
uuid = "aed1982a-8fda-507f-9586-7b0439959a61"
version = "1.1.34+0"

[[Xorg_libX11_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxcb_jll", "Xorg_xtrans_jll"]
git-tree-sha1 = "5be649d550f3f4b95308bf0183b82e2582876527"
uuid = "4f6342f7-b3d2-589e-9d20-edeb45f2b2bc"
version = "1.6.9+4"

[[Xorg_libXau_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "4e490d5c960c314f33885790ed410ff3a94ce67e"
uuid = "0c0b7dd1-d40b-584c-a123-a41640f87eec"
version = "1.0.9+4"

[[Xorg_libXcursor_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXfixes_jll", "Xorg_libXrender_jll"]
git-tree-sha1 = "12e0eb3bc634fa2080c1c37fccf56f7c22989afd"
uuid = "935fb764-8cf2-53bf-bb30-45bb1f8bf724"
version = "1.2.0+4"

[[Xorg_libXdmcp_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "4fe47bd2247248125c428978740e18a681372dd4"
uuid = "a3789734-cfe1-5b06-b2d0-1dd0d9d62d05"
version = "1.1.3+4"

[[Xorg_libXext_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "b7c0aa8c376b31e4852b360222848637f481f8c3"
uuid = "1082639a-0dae-5f34-9b06-72781eeb8cb3"
version = "1.3.4+4"

[[Xorg_libXfixes_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "0e0dc7431e7a0587559f9294aeec269471c991a4"
uuid = "d091e8ba-531a-589c-9de9-94069b037ed8"
version = "5.0.3+4"

[[Xorg_libXi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll", "Xorg_libXfixes_jll"]
git-tree-sha1 = "89b52bc2160aadc84d707093930ef0bffa641246"
uuid = "a51aa0fd-4e3c-5386-b890-e753decda492"
version = "1.7.10+4"

[[Xorg_libXinerama_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll"]
git-tree-sha1 = "26be8b1c342929259317d8b9f7b53bf2bb73b123"
uuid = "d1454406-59df-5ea1-beac-c340f2130bc3"
version = "1.1.4+4"

[[Xorg_libXrandr_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll"]
git-tree-sha1 = "34cea83cb726fb58f325887bf0612c6b3fb17631"
uuid = "ec84b674-ba8e-5d96-8ba1-2a689ba10484"
version = "1.5.2+4"

[[Xorg_libXrender_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "19560f30fd49f4d4efbe7002a1037f8c43d43b96"
uuid = "ea2f1a96-1ddc-540d-b46f-429655e07cfa"
version = "0.9.10+4"

[[Xorg_libpthread_stubs_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "6783737e45d3c59a4a4c4091f5f88cdcf0908cbb"
uuid = "14d82f49-176c-5ed1-bb49-ad3f5cbd8c74"
version = "0.1.0+3"

[[Xorg_libxcb_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "XSLT_jll", "Xorg_libXau_jll", "Xorg_libXdmcp_jll", "Xorg_libpthread_stubs_jll"]
git-tree-sha1 = "daf17f441228e7a3833846cd048892861cff16d6"
uuid = "c7cfdc94-dc32-55de-ac96-5a1b8d977c5b"
version = "1.13.0+3"

[[Xorg_libxkbfile_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "926af861744212db0eb001d9e40b5d16292080b2"
uuid = "cc61e674-0454-545c-8b26-ed2c68acab7a"
version = "1.1.0+4"

[[Xorg_xcb_util_image_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "0fab0a40349ba1cba2c1da699243396ff8e94b97"
uuid = "12413925-8142-5f55-bb0e-6d7ca50bb09b"
version = "0.4.0+1"

[[Xorg_xcb_util_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxcb_jll"]
git-tree-sha1 = "e7fd7b2881fa2eaa72717420894d3938177862d1"
uuid = "2def613f-5ad1-5310-b15b-b15d46f528f5"
version = "0.4.0+1"

[[Xorg_xcb_util_keysyms_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "d1151e2c45a544f32441a567d1690e701ec89b00"
uuid = "975044d2-76e6-5fbe-bf08-97ce7c6574c7"
version = "0.4.0+1"

[[Xorg_xcb_util_renderutil_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "dfd7a8f38d4613b6a575253b3174dd991ca6183e"
uuid = "0d47668e-0667-5a69-a72c-f761630bfb7e"
version = "0.3.9+1"

[[Xorg_xcb_util_wm_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "e78d10aab01a4a154142c5006ed44fd9e8e31b67"
uuid = "c22f9ab0-d5fe-5066-847c-f4bb1cd4e361"
version = "0.4.1+1"

[[Xorg_xkbcomp_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxkbfile_jll"]
git-tree-sha1 = "4bcbf660f6c2e714f87e960a171b119d06ee163b"
uuid = "35661453-b289-5fab-8a00-3d9160c6a3a4"
version = "1.4.2+4"

[[Xorg_xkeyboard_config_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xkbcomp_jll"]
git-tree-sha1 = "5c8424f8a67c3f2209646d4425f3d415fee5931d"
uuid = "33bec58e-1273-512f-9401-5d533626f822"
version = "2.27.0+4"

[[Xorg_xtrans_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "79c31e7844f6ecf779705fbc12146eb190b7d845"
uuid = "c5fb5394-a638-5e4d-96e5-b29de1b5cf10"
version = "1.4.0+3"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[Zstd_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "cc4bf3fdde8b7e3e9fa0351bdeedba1cf3b7f6e6"
uuid = "3161d3a3-bdf6-5164-811a-617609db77b4"
version = "1.5.0+0"

[[libass_jll]]
deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "HarfBuzz_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "5982a94fcba20f02f42ace44b9894ee2b140fe47"
uuid = "0ac62f75-1d6f-5e53-bd7c-93b484bb37c0"
version = "0.15.1+0"

[[libfdk_aac_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "daacc84a041563f965be61859a36e17c4e4fcd55"
uuid = "f638f0a6-7fb0-5443-88ba-1cc74229b280"
version = "2.0.2+0"

[[libpng_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "94d180a6d2b5e55e447e2d27a29ed04fe79eb30c"
uuid = "b53b4c65-9356-5827-b1ea-8c7a1a84506f"
version = "1.6.38+0"

[[libvorbis_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Ogg_jll", "Pkg"]
git-tree-sha1 = "c45f4e40e7aafe9d086379e5578947ec8b95a8fb"
uuid = "f27f6e37-5d2b-51aa-960f-b287f2bc3b7a"
version = "1.3.7+0"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"

[[x264_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "4fea590b89e6ec504593146bf8b988b2c00922b2"
uuid = "1270edf5-f2f9-52d2-97e9-ab00b5d0237a"
version = "2021.5.5+0"

[[x265_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "ee567a171cce03570d77ad3a43e90218e38937a9"
uuid = "dfaa095f-4041-5dcd-9319-2fabd8486b76"
version = "3.5.0+0"

[[xkbcommon_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Wayland_jll", "Wayland_protocols_jll", "Xorg_libxcb_jll", "Xorg_xkeyboard_config_jll"]
git-tree-sha1 = "ece2350174195bb31de1a63bea3a41ae1aa593b6"
uuid = "d8fb68d0-12a3-5cfd-a85a-d49703b185fd"
version = "0.9.1+5"
"""

# ╔═╡ Cell order:
# ╟─9cbcd233-c37f-49f6-a6bf-5d92cf744972
# ╟─74ae31c5-545b-4b82-b9f0-1d2b7cccaa04
# ╟─ce825258-41b1-4c40-91f7-05d851b248e2
# ╟─1474d231-7d61-4a6d-80f8-2dd89f039168
# ╟─7cda46b1-0a59-47f0-b612-6b877de3ecb6
# ╟─c1758648-ae67-4ce6-9f5f-7d1b8512445e
# ╟─e25846d0-fe93-4f2e-b500-9f218998af08
# ╟─e720ee46-b4c8-4539-b598-5655959f51f5
# ╟─2d0673e6-1cbf-4435-9d29-dd4303f36358
# ╟─5fec2475-8abf-4572-8d4c-58838e54a170
# ╟─e3ae6751-dcad-49d4-9ff9-8dbc7e42fcb8
# ╟─f1228fa1-a310-496d-8a86-6ba8de8b4575
# ╟─37ae6364-6465-4368-9bbd-b05e3b30497b
# ╟─c333b147-9776-4a79-a50e-564590115a17
# ╟─ebd94ab6-bdc8-49fd-bde0-ac573c5b3bae
# ╟─3428ee85-399c-441e-a3e9-e3acac392734
# ╟─8403d091-a0b4-4540-9fea-4c8d9e7e9fbe
# ╟─541dc0b0-2fac-49b3-bfdd-8575aa2c056b
# ╟─65938662-b78e-4713-bb21-4715105aeed4
# ╟─dc06a8da-da64-4468-8e56-c544ef71d408
# ╟─8245d16b-81d7-41a6-9197-0c7aaabbeaad
# ╟─944d8b32-73bb-44b9-af06-c8aa6202bf5c
# ╟─5a46bfa5-f0b0-45de-bcaf-2e1619cd9c49
# ╟─74686cc2-9214-42d6-9d35-2ed4bf63b43a
# ╟─c13a426a-2805-41ae-b95f-259baacf6d33
# ╟─bd203a88-79d3-42ff-ac53-9b264d53f1f7
# ╠═21b0b5ae-b058-4239-813b-bd7f6967d7c1
# ╠═824527d8-e9f4-4363-8db4-d1de26b2d256
# ╟─fc8f5f1d-0541-49f2-93d0-6badc50c6a19
# ╟─06a7ec74-0684-4974-aa00-1c12dc3acc7b
# ╟─959b5fd8-84d3-46cd-baed-c3d25d8c187f
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002


text:
# ClusterManager for HTCondor

export HTCManager, addprocs_htc

immutable HTCManager <: ClusterManager
end

function condor_script(portnum::Integer, np::Integer, config::Dict)
    exehome = config[:dir]
    exename = config[:exename]
    exeflags = config[:exeflags]
    #TODO: make this a configurable option
    priority = 15
	if "HOME" in keys(ENV)
		home = ENV["HOME"]
	else
		home = homedir()
	end
	if "HOSTNAME" in keys(ENV)
		hostname = ENV["HOSTNAME"]
	else
		hostname = gethostname()
	end
    jobname = "julia-$(getpid())"
    tdir = "$home/.julia-htc"
    run(`mkdir -p $tdir`)

	script_fname = "$tdir/$jobname.sh"
    scriptf = open(script_fname, "w")
    println(scriptf, "#!/bin/sh")
    println(scriptf, "$exehome/$exename --worker | /usr/bin/nc $hostname $portnum")
    close(scriptf)
	run(`chmod u+x $script_fname`)
    subf = open("$tdir/$jobname.sub", "w")
    println(subf, "executable = $tdir/$jobname.sh")
    println(subf, "universe = vanilla")
    println(subf, "should_transfer_files = yes")
	println(subf, "getenv = True")
    println(subf, "Notification = Error")
    println(subf, "priority = $priority")
    for i = 1:np
        println(subf, "output = $tdir/$jobname-$i.o")
        println(subf, "error= $tdir/$jobname-$i.e")
        println(subf, "queue")
    end
    close(subf)

    "$tdir/$jobname.sub"
end

function launch(manager::HTCManager, np::Integer, config::Dict, instances_arr::Array, c::Condition)
    try
        portnum = rand(8000:9000)
        server = listen(portnum)

        script = condor_script(portnum, np, config)
        out,proc = open(`condor_submit $script`)
        if !success(proc)
            println("batch queue not available (could not run condor_submit)")
            return
        end
        print(readline(out))
        print("Waiting for $np workers: ")

        io_objs = cell(np)
        configs = cell(np)
        for i=1:np
            conn = accept(server)
            io_objs[i] = conn
            configs[i] = merge(config, {:conn => conn, :server => server})
            print("$i ")
        end
        println(".")

        push!(instances_arr, collect(zip(io_objs, configs)))
        notify(c)
   catch e
        println("Error launching condor")
        println(e)
   end
end

function manage(manager::HTCManager, id::Integer, config::Dict, op::Symbol)
    if op == :finalize
        close(config[:conn])
#     elseif op == :interrupt
#         job = config[:job]
#         task = config[:task]
#         # this does not currently work
#         if !success(`qsig -s 2 -t $task $job`)
#             println("Error sending a Ctrl-C to julia worker $id (job: $job, task: $task)")
#         end
    end
end

addprocs_htc(np::Integer;kvs...) = addprocs(np, cman=HTCManager();kvs...)


text:
type CuDNNPoolingState
  pooling_desc :: CuDNN.PoolingDescriptor
  inputs_desc  :: Vector{CuDNN.Tensor4dDescriptor}
  outputs_desc :: Vector{CuDNN.Tensor4dDescriptor}
end

function setup_etc(backend::GPUBackend, layer::PoolingLayer, inputs,
    pooled_width, pooled_height)

  dtype = eltype(inputs[1])

  if isa(layer.pooling, Pooling.Max)
    pooling_mode = CuDNN.CUDNN_POOLING_MAX
  elseif isa(layer.pooling, Pooling.Mean)
    pooling_mode = CuDNN.CUDNN_POOLING_AVERAGE
  else
    error("TODO: pooling mode $(layer.pooling) not supported by CuDNN")
  end
  pooling_desc = CuDNN.create_pooling_descriptor(pooling_mode, layer.kernel, layer.stride, layer.pad)
  inputs_desc = Array(CuDNN.Tensor4dDescriptor, length(inputs))
  outputs_desc = Array(CuDNN.Tensor4dDescriptor, length(inputs))

  for i = 1:length(inputs)
    width,height,channels,num = size(inputs[i])
    inputs_desc[i] = CuDNN.create_tensor4d_descriptor(dtype,(width,height,channels,num))
    outputs_desc[i] = CuDNN.create_tensor4d_descriptor(dtype,
        (pooled_width[i],pooled_height[i],channels,num))
  end
  etc = CuDNNPoolingState(pooling_desc, inputs_desc, outputs_desc)
  return etc
end

function shutdown(backend::GPUBackend, state::PoolingLayerState)
  map(destroy, state.blobs)
  map(destroy, state.blobs_diff)
  CuDNN.destroy_pooling_descriotpr(state.etc.pooling_desc)
  map(CuDNN.destroy_tensor4d_descriptor, state.etc.inputs_desc)
  map(CuDNN.destroy_tensor4d_descriptor, state.etc.outputs_desc)
end

function forward(backend::GPUBackend, state::PoolingLayerState, inputs::Vector{Blob})
  layer = state.layer
  alpha = one(eltype(inputs[1]))
  beta = zero(eltype(inputs[1]))
  for i = 1:length(inputs)
    CuDNN.pooling_forward(backend.cudnn_ctx, state.etc.pooling_desc, alpha,
        state.etc.inputs_desc[i], inputs[i].ptr, beta,
        state.etc.outputs_desc[i], state.blobs[i].ptr)
  end
end

function backward(backend::GPUBackend, state::PoolingLayerState, inputs::Vector{Blob}, diffs::Vector{Blob})
  layer = state.layer
  alpha = one(eltype(inputs[1]))
  beta = zero(eltype(inputs[1]))
  for i = 1:length(inputs)
    if isa(diffs[i], CuTensorBlob)
      CuDNN.pooling_backward(backend.cudnn_ctx, state.etc.pooling_desc, alpha,
          state.etc.outputs_desc[i], state.blobs[i].ptr,
          state.etc.outputs_desc[i], state.blobs_diff[i].ptr,
          state.etc.inputs_desc[i], inputs[i].ptr,
          beta, state.etc.inputs_desc[i], diffs[i].ptr)
    end
  end
end


text:
module FrostHelperCustomRisingLava

using ..Ahorn, Maple

@mapdef Entity "FrostHelper/CustomRisingLava" FHCusRisingLava(x::Integer, y::Integer, intro::Bool=false, speed::Integer=-30, coldColor1::String="33ffe7", coldColor2::String="4ca2eb", coldColor3::String="0151d0", hotColor1::String="ff8933", hotColor2::String="f25e29", hotColor3::String="d01c01", reverseCoreMode::Bool=false, doRubberbanding::Bool=true)

const placements = Ahorn.PlacementDict(
    "Custom Rising Lava (Frost Helper)" => Ahorn.EntityPlacement(
        FHCusRisingLava
    )
)

function Ahorn.selection(entity::FHCusRisingLava)
    x, y = Ahorn.position(entity)

    return Ahorn.Rectangle(x - 12, y - 12, 24, 24)
end

Ahorn.render(ctx::Ahorn.Cairo.CairoContext, entity::FHCusRisingLava, room::Maple.Room) = Ahorn.drawImage(ctx, Ahorn.Assets.risingLava, -12, -12)

end

max_stars_repo_name: JaThePlayer/FrostHelper

max_issues_repo_name: JaThePlayer/FrostHelper

max_forks_repo_name: JaThePlayer/FrostHelper


text:
using StatsPlots, Revise
include("binary_simulation.jl")
#coverage results printed here
results = run_simulation(2500, 10000)
#also can plot smoothed empirical distribution versus actual
density(results[:, 1:2], label = ["Direct Effect" "Indirect Effect"])
μ = mean(results, dims=1)
println("Estimated: ", μ[3], " Indirect: ", μ[4])
println("Simulated: ", std(results[:, 1]), " ", std(results[:, 2]))
plot!(Normal(μ[1], μ[3]), label="Approx Direct")
plot!(Normal(μ[2], μ[4]), label="Approx Indirect")
#plot(values, seriestype=:scatterhist, linestyle=:solid, size=(600,150))

max_stars_repo_name: evanmunro/market-equilibrium

max_issues_repo_name: evanmunro/market-equilibrium

max_forks_repo_name: evanmunro/market-equilibrium


text:
using ParameterisedModule
using Test

Functor = Function

    # Write your own tests here.

    # :(@sig struct S{A}
#     x :: Int
#     y :: A
#     struct K end
# end) |> (x -> macroexpand(ParameterisedModule, x)) |> println

@testset "I'm here?" begin

@sig struct S{A}
    x :: Int
    y :: A
    struct K end
end

println(S)

# :(@structure struct S{String}
#     struct K
#         x :: Int
#     end
#     x = 2
#     y = "2"
# end) |> (x -> macroexpand(ParameterisedModule, x)) |> println

mod1 = @structure struct S{String}
    struct K
        x :: Int
    end
    x = 2
    y = "2"
end

@test mod1.x == 2
@test mod1.y == "2"
@test_throws Any mod1.x = 3

@test_throws Any begin
    @structure struct S{Nothing}
    struct K_ end
        K = K_
        x = 0
        y = "2" # should be nothing
    end
end

@sig struct Numeric
    (+) :: Function
    (-) :: Function
end

mod3 = @structure struct Numeric
    a + b = "$a + $b"
    a - b = "$a - $b"
end

@test mod3.:+(1, 2) == "1 + 2"
@test mod3.:-(1, 2) == "1 - 2"

@open Numeric mod3 begin
    @test 1 + 2 == "1 + 2"
end

using ParameterisedModule

# this is the module type declaration
@sig struct NatAlgebra
    struct Eltype end # this is type declaration
    succ :: Function
    zero :: Eltype
end

# make a module `num_nat`, whose module type is NatAlgebra
num_nat = @structure struct NatAlgebra
    Eltype  = Int
    succ(x) = x + 1
    zero    = 0
end

@open NatAlgebra num_nat begin
    println(succ(succ(zero))) # 2
end

@test_throws UndefVarError println(succ(succ(zero)))
# ERROR: UndefVarError: succ not defined

str_nat = @structure struct NatAlgebra
    Eltype  = String
    succ(x) = "succ($x)"
    zero    = "zero"
end

@open NatAlgebra str_nat begin
    println(succ(succ(zero))) # succ(succ(zero))
end

@sig struct TF{Eltype}
    e :: Eltype
end

TFZero(nat :: NatAlgebra) =
    @structure struct TF{nat.Eltype}
        e = nat.zero
    end

word_algebra =
    @structure struct NatAlgebra
        Eltype = Functor
        zero = TFZero
        succ(T1) =
            function (N::NatAlgebra)
                @structure struct TF{N.Eltype}
                    e = N.succ(T1(N).e)
                end
            end
    end

@sig struct H
    h :: Functor
end

word_algebra.succ(TFZero)(num_nat) |> println

HTFC(N::NatAlgebra) =
    @structure struct H
        h(T) = T(N).e
    end

@open H HTFC(num_nat) begin
    @test h(word_algebra.zero) == num_nat.zero
    case(x::Functor) =
        h(word_algebra.succ(x)) == num_nat.succ(h(x))

    words = Functor[TFZero]
    for i = 1:100
        push!(words, word_algebra.succ(words[end]))
    end
    @test all(words) do x; case(x) end
end
end


text:
"""
## rigid_jointed!

This subroutine forms the global stiffness matrix of a
general pin-joionted structural element (1-, 2- or 3-d).

### Function
```julia
rigid_jointed!(km, prop, gamma, etype, iel, coord)
```

### Arguments
```julia
* km::Matrix{Float64}       : Element stiffness matrix (Updated)
* prop::Matrix{Float64}     : Element properties
* gamma::Vector{Float64}    : Element orientations (3D)
* etype::Vector{Int}        : Element type vector
* iel::Int                  : Element number
* coord::Matrix{Float64}}   : Element nodal coordinates
```
"""
function rigid_jointed!(km::Matrix{Float64}, prop::Matrix{Float64},
  gamma::Vector{Float64}, etype::Vector{Int}, iel::Int, coord::LinearAlgebra.Adjoint{Float64,Array{Float64,2}})
#
# This function forms the stiffness matrix of a
# general beam/column fin_el (1-, 2- or 3-d).
#
 ndim=size(coord,2)
 @assert 1 <= ndim <= 3
 ndof=size(km,1)
 r0=zeros(3,3)
 t1=zeros(12,12)
 t2=zeros(12,12)
 cc=zeros(12,12)

 if ndim ==1
   ei=prop[etype[iel],1]
   ell=coord[2,1]-coord[1,1]
   km[1,1]=12.0*ei/(ell*ell*ell)
   km[3,3]=km[1,1]
   km[1,2]=6.0*ei/(ell*ell)
   km[2,1]=km[1,2]
   km[1,4]=km[1,2]
   km[4,1]=km[1,4]
   km[1,3]=-km[1,1]
   km[3,1]=km[1,3]
   km[3,4]=-km[1,2]
   km[4,3]=km[3,4]
   km[2,3]=km[3,4]
   km[3,2]=km[2,3]
   km[2,2]=4.0*ei/ell
   km[4,4]=km[2,2]
   km[2,4]=2.0*ei/ell
   km[4,2]=km[2,4]
 elseif ndim == 2
   ea=prop[etype[iel],1]
   ei=prop[etype[iel], 2]
   x1=coord[1,1]
   y1=coord[1,2]
   x2=coord[2,1]
   y2=coord[2,2]
   ell=sqrt((y2-y1)^2+(x2-x1)^2)
   c=(x2-x1)/ell
   s=(y2-y1)/ell
   e1=ea/ell
   e2=12.0*ei/(ell*ell*ell)
   e3=ei/ell
   e4=6.0*ei/(ell*ell)
   km[1,1]=c*c*e1+s*s*e2
   km[4,4]=km[1,1]
   km[1,2]=s*c*(e1-e2)
   km[2,1]=km[1,2]
   km[4,5]=km[1,2]
   km[5,4]=km[4,5]
   km[1,3]=-s*e4
   km[3,1]=km[1,3]
   km[1,6]=km[1,3]
   km[6,1]=km[1,6]
   km[3,4]=s*e4
   km[4,3]=km[3,4]
   km[4,6]=km[3,4]
   km[6,4]=km[4,6]
   km[1,4]=-km[1,1]
   km[4,1]=km[1,4]
   km[1,5]=s*c*(-e1+e2)
   km[5,1]=km[1,5]
   km[2,4]=km[1,5]
   km[4,2]=km[2,4]
   km[2,2]=s*s*e1+c*c*e2
   km[5,5]=km[2,2]
   km[2,5]=-km[2,2]
   km[5,2]=km[2,5]
   km[2,3]=c*e4
   km[3,2]=km[2,3]
   km[2,6]=km[2,3]
   km[6,2]=km[2,6]
   km[3,3]=4.0*e3
   km[6,6]=km[3,3]
   km[3,5]=-c*e4
   km[5,3]=km[3,5]
   km[5,6]=km[3,5]
   km[6,5]=km[5,6]
   km[3,6]=2.0*e3
   km[6,3]=km[3,6]
 else
   ea=prop[etype[iel],1]
   eiy=prop[etype[iel],2]
   eiz=prop[etype[iel],3]
   gj=prop[etype[iel],4]
   x1=coord[1,1]
   y1=coord[1,2]
   z1=coord[1,3]
   x2=coord[2,1]
   y2=coord[2,2]
   z2=coord[2,3]
   xl=x2-x1
   yl=y2-y1
   zl=z2-z1
   ell=sqrt(xl*xl+yl*yl+zl*zl)
   km=zeros(ndof,ndof)
   a1=ea/ell
   a2=12.0*eiz/(ell*ell*ell)
   a3=12.0*eiy/(ell*ell*ell)
   a4=6.0*eiz/(ell*ell)
   a5=6.0*eiy/(ell*ell)
   a6=4.0*eiz/ell
   a7=4.0*eiy/ell
   a8=gj/ell
   km[1,1]=a1
   km[7,7]=a1
   km[1,7]=-a1
   km[7,1]=-a1
   km[2,2]=a2
   km[8,8]=a2
   km[2,8]=-a2
   km[8,2]=-a2
   km[3,3]=a3
   km[9,9]=a3
   km[3,9]=-a3
   km[9,3]=-a3
   km[4,4]=a8
   km[10,10]=a8
   km[4,10]=-a8
   km[10,4]=-a8
   km[5,5]=a7
   km[11,11]=a7
   km[5,11]=0.5*a7
   km[11,5]=0.5*a7
   km[6,6]=a6
   km[12,12]=a6
   km[6,12]=0.5*a6
   km[12,6]=0.5*a6
   km[2,6]=a4
   km[6,2]=a4
   km[2,12]=a4
   km[12,2]=a4
   km[6,8]=-a4
   km[8,6]=-a4
   km[8,12]=-a4
   km[12,8]=-a4
   km[5,9]=a5
   km[9,5]=a5
   km[9,11]=a5
   km[11,9]=a5
   km[3,5]=-a5
   km[5,3]=-a5
   km[3,11]=-a5
   km[11,3]=-a5
   gamrad=gamma[iel]*pi/180.0
   cg=cos(gamrad)
   sg=sin(gamrad)
   den=ell*sqrt(xl*xl+zl*zl)
   if den !== 0.0
     r0[1,1]=xl/ell
     r0[1,2]=yl/ell
     r0[1,3]=zl/ell
     r0[2,1]=(-xl*yl*cg-ell*zl*sg)/den
     r0[2,2]=den*cg/(ell*ell)
     r0[2,3]=(-yl*zl*cg+ell*xl*sg)/den
     r0[3,1]=(xl*yl*sg-ell*zl*cg)/den
     r0[3,2]=-den*sg/(ell*ell)
     r0[3,3]=(yl*zl*sg+ell*xl*cg)/den
   else
     r0[1,1]=0.0
     r0[1,3]=0.0
     r0[2,2]=0.0
     r0[3,2]=0.0
     r0[1,2]=1.0
     r0[2,1]=-cg
     r0[3,3]=cg
     r0[2,3]=sg
     r0[3,1]=sg
   end
   for i in 1:3
     for j in 1:3
       x=r0[i,j]
       for k in 0:3:9
         t1[i+k,j+k]=x
         t2[j+k,i+k]=x
       end
     end
   end
   for i in 1:12
     for j in 1:12
       tsum=0.0
       for k in 1:12
         tsum += km[i,k]*t1[k,j]
       end
       cc[i,j]=tsum
     end
   end
   for i in 1:12
     for j in 1:12
       tsum=0.0
       for k in 1:12
         tsum += t2[i,k]*cc[k,j]
       end
       km[i,j]=tsum
     end
   end
 end        # if ndim statement
 km
end