text:
# This file is part of GenericSchur.jl, released under the MIT "Expat" license

# The methods in this file are derived from LAPACK's ztgsyl etc.
# LAPACK is released under a BSD license, and is
# Copyright:
# Univ. of Tennessee
# Univ. of California Berkeley
# Univ. of Colorado Denver
# NAG Ltd.

# Note: since this is based on the routines for complex types,
# several methods are hard-coded with 2x2 blocks

"""
    trsylvester!(A,B,C,D,E,F) => R, L, σ

solve the generalized Sylvester equation ``A R - L B = σ C``
``D R - L E = σ F``
given upper triangular and square `A,B,D` and `E`.
Overwrites `C` and `F`
with `R` and `L`, and sets `σ` to avoid overflow.
"""
function trsylvester!(A::StridedMatrix{T},B::StridedMatrix{T},
                      C::StridedVecOrMat{T},
                      D::StridedMatrix{T},E::StridedMatrix{T},
                      F::StridedVecOrMat{T}) where {T}
    m = checksquare(A)
    n = checksquare(B)
    ((size(C,1) == m) && (size(C,2) == n)) || throw(DimensionMismatch(
        "dimensions of C $(size(C)) must match A, ($m,$m), and B, ($n,$n)"))
    m1 = checksquare(D)
    n1 = checksquare(E)
    (m1 == m && n1 == n) || throw(DimensionMismatch(
        "dimensions of D and E must match A and B"))
    ((size(F,1) == m) && (size(F,2) == n)) || throw(DimensionMismatch(
        "dimensions of F $(size(F)) must match A, ($m,$m), and B, ($n,$n)"))

    rtyone = one(real(T))
    rtyzero = zero(real(T))
    scale = rtyone
    scaloc = rtyone
    dif = rtyzero
    cone = one(T)
    rdsum = rtyone
    rdscale = rtyzero
    # ztgsyl notes:
    # ijob 0: solve only; 1: solve & look-ahead dif; 2: solve & 1-est dif
    #      3: only look-ahead dif; 4: only 1-est dif
    # ifunc 1: look-ahead, 2: 1-est; else 0 (reset for 2-pass cases)
    #      ifunc => ijob in ztgsy2
    # isolve 2: solve and (some) dif; else 1
    # if solving:
    #   solve for C,F
    # if dif:
    #   if solving, save C,F
    #   zero out C,F
    #   call solver w/ ifunc
    #   if solving, restore C,F
    for j=1:n
        for i=m:-1:1
            # build 2x2 problem
            Z = [A[i,i] -B[j,j]; D[i,i] -E[j,j]]
            # TODO: replace lu with port of zgetc2
            fZ = lu(Z)
            rhs = [C[i,j]; F[i,j]]
            # TODO: use safe solver, yielding scaloc; apply scaloc to C and F
            # scale *= scaloc
            x = fZ \ rhs

            C[i,j] = x[1]
            F[i,j] = x[2]
            # substitute R[i,j], L[i,j] into remaining eq
            if i > 1
                α = -x[1]
                C[1:i-1,j] .+= α * A[1:i-1,i]
                F[1:i-1,j] .+= α * D[1:i-1,i]
            end
            if j < n
                α = x[2]
                C[i,j+1:n] .+= α * B[j,j+1:n]
                F[i,j+1:n] .+= α * E[j,j+1:n]
            end
        end
    end
    return C, F, scale
end

"""
    adjtrsylvester!(A,B,C,D,E,F) => X, σ

solve a generalized adjoint Sylvester equation ``Aᴴ X + Dᴴ Y = σ C``,
 ``X Bᴴ + Y Eᴴ = -σ F``,
for upper triangular `A, B, D,` and `E`, overwriting `C` and `F`
and setting `σ` to avoid overflow.
"""
function adjtrsylvester!(A::StridedMatrix{T},B::StridedMatrix{T},
                      C::StridedVecOrMat{T},
                      D::StridedMatrix{T},E::StridedMatrix{T},
                      F::StridedVecOrMat{T}) where {T}
    m = checksquare(A)
    n = checksquare(B)
    ((size(C,1) == m) && (size(C,2) == n)) || throw(DimensionMismatch(
        "dimensions of C $(size(C)) must match A, ($m,$m), and B, ($n,$n)"))
    m1 = checksquare(D)
    n1 = checksquare(E)
    (m1 == m && n1 == n) || throw(DimensionMismatch(
        "dimensions of D and E must match A and B"))
    ((size(F,1) == m) && (size(F,2) == n)) || throw(DimensionMismatch(
        "dimensions of F $(size(F)) must match A, ($m,$m), and B, ($n,$n)"))

    scale = one(real(T))
    scaloc = one(real(T))
    dif = zero(real(T))
    cone = one(T)
    for i=1:m
        for j=n:-1:1
            # build 2x2 problem
            Z = [A[i,i]' D[i,i]'; -B[j,j]' -E[j,j]']
            rhs = [C[i,j]; F[i,j]]
            # FIXME: use safe version and update scale
            fZ = lu(Z)
            x = fZ \ rhs
            C[i,j] = x[1]
            F[i,j] = x[2]
            # substitute R[i,j], L[i,j] into remaining eq
            for jj=1:j-1
                F[i,jj] += x[1] * B[jj,j]' + x[2] * E[jj,j]'
            end
            for ii=i+1:m
                C[ii,j] -= x[1] * A[i,ii]' + x[2] * D[i,ii]'
            end
        end
    end
    return C, F, scale
end


text:
# module Bukdu.Plug

module CSRF # Bukdu.Plug

import ..AbstractPlug

struct Protection <: AbstractPlug
end

end # module Bukdu.Plug.CSRF

function plug(::Type{CSRF.Protection}, conn::Conn)
    # TODO
    @error CSRF.Protection @__FILE__
    conn.request.response.status = 403 # 403 Forbidden
    conn.halted = true
end

# module Bukdu.Plug

ext: jl

max_stars_repo_name: wookay/TestBukdu

max_issues_repo_name: wookay/TestBukdu

max_forks_repo_name: wookay/TestBukdu


text:
function block_print(
    t::T,
    s::Vector{S};
    mute = false,
    header=y->repeat("|--",max(0,y)),
    offset=-1
    ) where {T <: Block, S <: AbstractString}
    nl = []
    level = [offset,]

    @inline make_str(a, x, l) = "\n"*repeat(" ",length(header(l)))*join(a[getfield(x,:x)], "\n"*repeat(" ",length(header(l))))
    @inline tup_str(x,a...)   = string(([getfield(x,m) for m in a]...,))

    f(x) = (push!(nl, header(level[end]) * tup_str(x,:n,:R) * (is_single(x) ? make_str(s,x,level[end]) : ""));
            push!(level,level[end]+1);
            0 )

    g(x) = (pop!(level); 0)

    DFS(t, f, g)

    if !mute  println.(nl)  end

    return nl
end

treep(t) = tree_print(t, propfunc=x->(getfield(x,:n),label(x),(is_multi(x) ? "M" : "S")), offset=0)

max_stars_repo_name: algorithmx/SmartParser

max_issues_repo_name: algorithmx/SmartParser

max_forks_repo_name: algorithmx/SmartParser


text:
using SpecialFunctions
using Random

export infbilanczos
"""
    λv,V,U=infbilanczos([eltype],nep, nept,[linsolvecreator,][linsolvertcreator,][v,][u,][σ,][γ,][tol,][neigs,][errmeasure,][logger,][maxit,][check_error_every])

Executes the Infinite Bi-Lanczos method on the problem defined by `nep::NEP`
and `nept::NEP`. `nep:NEP` is the original nonlinear eigenvalue problem and
`nept::NEP` is its (hermitian) transpose: ``M(λ^*)^H``.
 `v` and `u` are starting vectors,
`σ` is the shift and `γ` the scaling.
The iteration is continued until `neigs` Ritz pairs have converged.
This function throws a `NoConvergenceException` if the wanted eigenpairs are not computed after `maxit` iterations.
However, if `neigs` is set to `Inf` the iteration is continued until `maxit` iterations without an error being thrown.
See [`augnewton`](@ref) for other parameters.

# Example:
```julia-repl
julia> nep=nep_gallery("dep0");
julia> A=get_Av(nep); fv=get_fv(nep);
julia> At=[copy(A[1]'),copy(A[2]'),copy(A[3]')]
julia> nept=SPMF_NEP(At,fv); # Create the transposed NEP
julia> λv,V=infbilanczos(nep,nept,neigs=3,v=ones(size(nep,1)))
julia> norm(compute_Mlincomb(nep,λv[1],V[:,1]))
9.907299130783851e-15
```

# References:
* The infinite bi-Lanczos method for nonlinear eigenvalue problems, S. W. Gaaf and E. Jarlebring, SIAM J. Sci. Comput. 39:S898-S919, 2017, [preprint](https://arxiv.org/abs/1607.03454)
"""
    infbilanczos(nep::NEP,nept::NEP;params...)=infbilanczos(ComplexF64,nep,nept;params...)
    function infbilanczos(::Type{T},
                          nep::NEP,
                          nept::NEP;  # Transposed NEP
                          maxit::Integer=30,
                          linsolvercreator=DefaultLinSolverCreator(),
                          linsolvertcreator=DefaultLinSolverCreator(),
                          v::Vector=randn(real(T),size(nep,1)),
                          u::Vector=randn(real(T),size(nep,1)),
                          tol::Real=1e-12,
                          neigs=5,
                          errmeasure::ErrmeasureType = DefaultErrmeasure(nep),
                          σ::Number=0.0,
                          γ::Number=1,
                          logger=0,
                          check_error_every::Integer=1
                          ) where {T<:Number}

        @parse_logger_param!(logger)

        n=size(nep,1);
        σ=T(σ);
        v=Vector{T}(v);
        u=Vector{T}(v);

        # Linear systems solver for both M(σ) and M(σ)^H

        # Shift σ \neq 0 not implemented

        local M0inv::LinSolver = create_linsolver(linsolvercreator,nep,σ);
        local M0Tinv::LinSolver = create_linsolver(linsolvertcreator,nept,σ);

        #
        m=maxit;
        qt=lin_solve(M0Tinv,u);
        q=v;
        q=q/dot(qt,compute_Mlincomb(nep,σ,q,ones(1),1));

        Q0=zeros(T,n,m);                  # represents Q_{k-1}
        Qt0=zeros(T,n,m);                 # represents \til{Q}_{k-1}
        R1=zeros(T,n,m+1); R1[:,1]=q;       # represents R_{k}
        Rt1=zeros(T,n,m+1); Rt1[:,1]=qt;    # represents \tild{R}_{k}
        Z2=zeros(T,n,m);
        Zt2=zeros(T,n,m);
        Q_basis=zeros(T,n,m+1);
        Qt_basis=zeros(T,n,m);

        R2=zeros(T,n,m+1); # Needed?
        Rt2=zeros(T,n,m+1); # Needed?

        Q1=zeros(T,n,m); # Needed?
        Qt1=zeros(T,n,m); # Needed?

        # Vectors storing the diagonals
        alpha=zeros(T,m+1);
        beta=zeros(T,m+1);
        gamma=zeros(T,m+1);

        err = zero(T);
        λ=zeros(T,m+1); Q=zeros(T,n,m+1); TT=zeros(T,m+1,m+1);

        normq=norm(q); normqt=norm(qt);
        push_info!(logger,2,"norm(q)=$normq  norm(qt)=$normqt");

        k=1;

        for k=1:m

            # Note: conjugate required since we compute s'*r not r'*s
            omega = conj(left_right_scalar_prod(T,nep,nept,Rt1,R1,k,k,σ));

            beta[k] = sqrt(abs(omega));
            gamma[k] = conj(omega) / beta[k];

            # Step 11-12

            Q1[:,1:k]=R1[:,1:k]/beta[k];
            Qt1[:,1:k]=Rt1[:,1:k]/conj(gamma[k]);

            #@printf("\n%e %e\n", Q1[1,1],Qt1[1,1]);

            # Extra step, to compute Ritz vectors eventually
            Q_basis[:,k] = Q1[:,1];
            Qt_basis[:,k] = Qt1[:,1];

             # Step 1: Compute Z_{k+1}
            Dk=diagm(0 => 1 ./ (exp.(lfactorial.(1:k))));
            b1_tmp::AbstractVector=compute_Mlincomb(nep,σ,Q1[:,1:k]*Dk,ones(k),1);
            b1=-lin_solve(M0inv,b1_tmp);
            Z2[:,k] = b1;

            # Step 2: Compute \til{Z}_{k+1}
            bt1_tmp::AbstractVector=compute_Mlincomb(nept,conj(σ),Qt1[:,1:k]*Dk,ones(k),1);
            bt1=-lin_solve(M0Tinv,bt1_tmp);
            Zt2[:,k] = bt1

            # Step 3: Compute R_{k+1}
            R2[:,1] = Z2[:,k];
            R2[:,2:(k+1)]=Q1[:,1:k];
            if k > 1
                R2[:,1:(k-1)]=R2[:,1:(k-1)]-gamma[k]*Q0[:,1:(k-1)];
            end

            # Step 4: Compute \til{R}_{k+1}
            Rt2[:,1] = Zt2[:,k];
            Rt2[:,2:(k+1)]=Qt1[:,1:k];
            if k > 1
                Rt2[:,1:(k-1)]=Rt2[:,1:(k-1)]-conj(beta[k])*Qt0[:,1:(k-1)];
            end

            # Step 5: Compute \alpha_k
            alpha[k+1]=left_right_scalar_prod(T,nep,nept,Qt1,R2,k,k+1,σ);

            #Step 6: Compute R_{k+1}
            R2[:,1:k]=R2[:,1:k]-alpha[k+1]*Q1[:,1:k];

            #Step 7: Compute \til{R}_{k+1}
            Rt2[:,1:k]=Rt2[:,1:k]-conj(alpha[k+1])*Qt1[:,1:k];

            # shift  the matrices:
            (R1,R2)=(R2,R1);  R2[:,:] .= 0  # swap and reset forgotten variable
            (Rt1,Rt2)=(Rt2,Rt1);  Rt2[:,:] .= 0
            (Q0,Q1)=(Q1,Q0);  Q1[:,:] .= 0
            (Qt0,Qt1)=(Qt1,Qt0);  Qt1[:,:] .= 0

            if (rem(k,check_error_every)==0)||(k==m)
                # Check if we should terminate
                omega = left_right_scalar_prod(T,nep,nept,Rt1,R1,k+1,k+1,σ);
                beta[k+1] = sqrt(abs(omega));
                gamma[k+1] = conj(omega) / beta[k+1];

                alpha0=alpha[2:(k+1)];  # \alpha_1 stored in alpha(2)
                beta0=beta[2:(k+1)];    # we do not need \beta_1
                gamma0=gamma[2:(k+1)];  # we do not need \gamma_1

                TT = Matrix(spdiagm(-1 => beta0[1:k], 0 => alpha0[1:k], 1 => gamma0[1:k]))

                E = eigen(TT)
                λ = σ .+ 1 ./ E.values
                Z = E.vectors
                #@ifd(println("size(Z)=",size(Z)))
                #@ifd(println("size(TT)=",size(TT)))
                Q=Q_basis[:,1:(k+1)]*Z
                conv_eig=0;
                # compute the errors

                err=
                  map(s-> estimate_error(errmeasure,λ[s],Q[:,s]), 1:size(λ,1))
                # Log them and compute the converged
                push_iteration_info!(logger,2, k,err=err,λ=λ,v=
                                     continues=true);
                for s=1:size(λ,1)
                    if err[s]<tol;
                        conv_eig=conv_eig+1;
                        push_info!(logger,"+", continues=true);
                    elseif err[s]<tol*10
                        push_info!(logger,"=", continues=true);
                    else
                        push_info!(logger,"-", continues=true);
                    end
                end
                push_info!(logger,"");

                idx=sortperm(err[1:k]); # sort the error
                err=err[idx];

                if (conv_eig>=neigs) || (k==m)
                    nrof_eigs = Int(min(length(λ),neigs,conv_eig))
                    λ=λ[idx[1:nrof_eigs]]
                    Q=Q[:,idx[1:nrof_eigs]]
                    for i=1:nrof_eigs
                        normalize!(view(Q,1:n,i))
                    end
                    if (conv_eig>=neigs) || (neigs==Inf)
                        return λ,Q,TT
                    end
                end
            end
        end

        msg="Number of iterations exceeded. maxit=$(maxit)."
        throw(NoConvergenceException(λ,Q,err,msg))
    end

    function left_right_scalar_prod(::Type{T}, nep,nept,At,B,ma,mb,σ) where {T}
        # Compute the scalar product based on the function nep.M_lin_comb
        c=0;
        # This is the nasty double loop, which brings
        # complexity O(m^3n). Will be limiting if we do many iterations
        XX=zeros(T,size(B,1),mb); # pre-allocate
        for j=1:ma
            #dd=1 ./ factorial(j:(j+mb-1));
            dd=1 ./ exp.(lfactorial.(j:(j+mb-1)));
            XX=broadcast(*,B[:,1:mb],dd'); # diag scaling
            #XX=bsxfun(@times,B(:,1:mb),dd);  # Column scaling: Faster than constructing

            # compute Mlincomb starting from derivative j
            z::AbstractVector=-compute_Mlincomb(nep,σ,XX,ones(size(XX,2)),j);
            c=c+dot(At[:,j],z);
        end
        return c
    end


text:
@testset "DisjunctiveSet" begin
    for T in (Float64, Rational{Int})
        lbs = T[1 13//10 4; 3//2 4//3 7]
        ubs = T[5//2 6 2; 10 11 12]
        ds = @inferred DisjunctiveSet(lbs, ubs)
        @test ds.lbs == lbs
        @test ds.ubs == ubs
        @test @inferred MOI.dimension(ds) == 2
        @test @inferred DisjunctiveConstraints.num_alternatives(ds) == 3

        bad_lbs = T[1 13//10; 3//2 4//3]
        @test_throws DimensionMismatch DisjunctiveSet(bad_lbs, ubs)
    end
end

@testset "CombinatorialDisjunctiveSet" begin
    sparsity = [[1, 2], [2, 3], [3, 4]]
    cdc = @inferred CombinatorialDisjunctiveSet(sparsity)
    @test cdc.sparsity == sparsity
    @test @inferred MOI.dimension(cdc) == 4

    push!(sparsity, [1, 0])
    @test_throws ArgumentError CombinatorialDisjunctiveSet(sparsity)
end

@testset "Disjunction" begin
    f = MOI.VectorOfVariables([MOI.VariableIndex(1), MOI.VariableIndex(3)])
    lbs = [-1 -1 -1; -2 -2 -2]
    ubs = [1 1 1; 2 2 2]
    s = DisjunctiveConstraints.DisjunctiveSet(lbs, ubs)
    disjunction = @inferred DisjunctiveConstraints.Disjunction(f, s)
    @test @inferred DisjunctiveConstraints.num_alternatives(disjunction) == 3

    lbs = vcat(lbs, [-3 -3 -3])
    ubs = vcat(ubs, [3 3 3])
    s_bad = DisjunctiveConstraints.DisjunctiveSet(lbs, ubs)
    @test_throws DimensionMismatch DisjunctiveConstraints.Disjunction(f, s_bad)
end


text:
function NormalizedExponent(significand, exponent::Int32)
    significand::Uint64
    while (significand & HiddenBit(Float64)) == 0
        significand <<= 1
        exponent -= 1
    end
    return int32(exponent)
end

function bignumdtoa(v,mode,requested_digits::Int,buffer)
    significand = _significand(v)
    exponent = _exponent(v)
    lower_boundary_is_closer = lowerboundaryiscloser(v)
    need_boundary_deltas = mode == SHORTEST

    is_even = (significand & 1) == 0
    normalized_exponent = NormalizedExponent(significand, exponent)
    estimated_power = EstimatePower(normalized_exponent)

    if mode == FIXED && -estimated_power - 1 > requested_digits
        buffer[1] = 0
        len = 1
        decimal_point = -requested_digits
        return true, len, decimal_point, buffer
    end
    num = den = minus = plus = BigInt(0)
    num, den, minus, plus = InitialScaledStartValues(significand,exponent,lower_boundary_is_closer,
                              estimated_power,need_boundary_deltas,
                              num,den,minus,plus);
    num, den, minus, plus, decimal_point = FixupMultiply10(
        estimated_power,is_even,num,den,minus,plus);

    if mode == SHORTEST
        len, buffer = GenerateShortestDigits(num,den,minus,plus,is_even,buffer)
    elseif mode == FIXED
        len, decimal_point, buffer = BignumToFixed(requested_digits,num,den,buffer,decimal_point)
    elseif mode == PRECISION
        len, decimal_point, buffer = GenerateCountedDigits(requested_digits,num,den,buffer,decimal_point)
    end
    buffer[len] = 0
    return true, len, decimal_point, buffer
end

function GenerateShortestDigits(num,den,minus,plus,is_even,buffer)
    len = 1
    while true
        digit, num = divrem(num,den)
        buffer[len] = 0x30 + digit
        len += 1
        in_delta_room_minus = is_even ? num <= minus : num < minus
        in_delta_room_plus = is_even ? num + plus >= den : num + plus > den

        if !in_delta_room_minus && !in_delta_room_plus
            num *= 10
            minus *= 10
            if minus != plus
                plus *= 10
            end
        elseif in_delta_room_minus && in_delta_room_plus
            compare = num + num
            if compare < den
            elseif compare > den
                buffer[len - 1] += 1
            else
                if (buffer[len - 1] - 0x30) % 2 == 0
                else
                    buffer[len - 1] += 1
                end
            end
            return len, buffer
        elseif in_delta_room_minus
            return len, buffer
        else
            buffer[len - 1] += 1
            return len, buffer
        end
    end
end

function GenerateCountedDigits(count,num,den,buffer,decimal_point)
    for i = 1:(count-1)
        digit, num = divrem(num,den)
        buffer[i] = 0x30 + digit
        num *= 10
    end
    digit, num = divrem(num,den)
    if num + num >= den
        digit += 1
    end
    buffer[count] = 0x30 + digit
    for i = count:-1:2
        buffer[i] != 0x30 + 10 && break
        buffer[i] = 0x30
        buffer[i - 1] += 1
    end
    if buffer[1] == 0x30 + 10
        buffer[1] = 0x31
        decimal_point += 1
    end
    len = count+1
    return len, decimal_point, buffer
end

function BignumToFixed(requested_digits,num,den,buffer,decimal_point)
    if -decimal_point > requested_digits
        decimal_point = -requested_digits
        len = 1
        return len, decimal_point, buffer
    elseif -decimal_point == requested_digits
        den *= 10
        if num + num >= den
            buffer[1] = 0x31
            len = 2
            decimal_point += 1
        else
            len = 1
        end
        return len, decimal_point, buffer
    else
        needed_digits = decimal_point + requested_digits
        len, decimal_point, buffer = GenerateCountedDigits(
              needed_digits,num,den,buffer,decimal_point)
    end
    return len, decimal_point, buffer
end

const k1Log10 = 0.30102999566398114
function EstimatePower(exponent)
    kSignificandSize = SignificandSize(Float64)
    estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10)
    return estimate
end

function InitialScaledStartValuesPositiveExponent(
        significand,exponent,estimated_power,need_boundary_deltas,
        num,den,minus,plus)
    num::BigInt = BigInt(significand)
    num <<= exponent
    den::BigInt = BigInt(10)^estimated_power
    if need_boundary_deltas
        den <<= 1
        num <<= 1
        plus::BigInt = BigInt(1)
        plus <<= exponent
        minus::BigInt = BigInt(1)
        minus <<= exponent
    end
    return num, den, minus, plus
end

function InitialScaledStartValuesNegativeExponentPositivePower(
        significand,exponent,estimated_power,need_boundary_deltas,
        num,den,minus,plus)
    num::BigInt = BigInt(significand)
    den::BigInt = BigInt(10)^estimated_power
    den <<= -exponent
    if need_boundary_deltas
        den <<= 1
        num <<= 1
        plus::BigInt = 1
        minus::BigInt = 1
    end
    return num, den, minus, plus
end

function InitialScaledStartValuesNegativeExponentNegativePower(
        significand,exponent,estimated_power,need_boundary_deltas,
        num,den,minus,plus)
    num::BigInt = BigInt(10)^-estimated_power
    if need_boundary_deltas
        plus = num
        minus = num
    end
    num *= significand
    den::BigInt = BigInt(1)
    den <<= -exponent
    if need_boundary_deltas
        num <<= 1
        den <<= 1
    end
    return num, den, minus, plus
end

function InitialScaledStartValues(significand,
            exponent,lower_boundary_is_closer,estimated_power,
            need_boundary_deltas,num,den,minus,plus)
    if exponent >= 0
        num,den,minus,plus = InitialScaledStartValuesPositiveExponent(
                significand, exponent, estimated_power, need_boundary_deltas,
                num,den,minus,plus)
    elseif estimated_power >= 0
        num,den,minus,plus = InitialScaledStartValuesNegativeExponentPositivePower(
                significand, exponent, estimated_power, need_boundary_deltas,
                num,den,minus,plus)
    else
        num,den,minus,plus = InitialScaledStartValuesNegativeExponentNegativePower(
                significand, exponent, estimated_power, need_boundary_deltas,
                num,den,minus,plus)
    end
    if need_boundary_deltas && lower_boundary_is_closer
         den::BigInt <<= 1
         num::BigInt <<= 1
        plus::BigInt <<= 1
    end
    return num, den, minus, plus
end

function FixupMultiply10(estimated_power,is_even,num,den,minus,plus)
    in_range = is_even ? num + plus >= den : num + plus > den
    if in_range
        decimal_point::Int32 = estimated_power + 1
    else
        decimal_point = estimated_power
        num::BigInt *= 10
        if minus == plus
            minus::BigInt *= 10
             plus::BigInt = minus
        else
            minus *= 10
            plus *= 10
        end
    end
    return num, den, minus, plus, decimal_point
end


text:
# MIT License
#
# Copyright (c) 2018 Martin Biel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

@reexport module QuasiGradient

# Standard library
using LinearAlgebra
using SparseArrays
using Distributed
using Printf

# External libraries
using Parameters
using JuMP
using MathOptInterface
using StochasticPrograms
using StochasticPrograms: AcceptableTermination
using StochasticPrograms: UnspecifiedInstantiation, StageDecompositionStructure, AbstractScenarioProblems, ScenarioProblems, DistributedScenarioProblems, DecisionChannel
using StochasticPrograms: AbstractExecution, Serial, Synchronous, Asynchronous
using StochasticPrograms: AbstractStructuredOptimizer, set_master_optimizer!, set_subproblem_optimizer!
using StochasticPrograms: DecisionMap, get_decisions, all_decisions, set_decision!, set_stage!, SingleDecisionSet, NoSpecifiedConstraint
using StochasticPrograms: all_decisions, all_known_decisions, remove_decision!, update_known_decisions!, KnownValuesChange, NotTaken, Known, KnownDecision
using StochasticPrograms: add_subscript
using StochasticPrograms: AbstractPenaltyTerm, Quadratic, InfNorm, ManhattanNorm, initialize_penaltyterm!, update_penaltyterm!, remove_penalty!, remove_penalty_variables!, remove_penalty_constraints!
using ProgressMeter
using ProgressMeter: AbstractProgress

import Base: show, put!, wait, isready, take!, fetch, zero, +, length, size
import StochasticPrograms: supports_structure, num_iterations, default_structure, check_loadable, load_structure!, restore_structure!, optimize!, optimizer_name, master_optimizer, subproblem_optimizer, num_subproblems, remove_penalty_variables!, remove_penalty_constraints!
using ProgressMeter: Progress

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

export
    AbstractQuasiGradientAttribute,
    QuasiGradientAlgorithm,
    num_iterations,
    SubProblems,
    Unaltered,
    Smoothed,
    StepSize,
    StepParameter,
    RawStepParameter,
    set_step_attribute,
    set_step_attributes,
    Constant,
    Diminishing,
    Polyak,
    BB,
    Prox,
    ProxParameter,
    RawProxParameter,
    set_prox_attribute,
    set_prox_attributes,
    NoProx,
    Polyhedron,
    AndersonAcceleration,
    DryFriction,
    Nesterov,
    Termination,
    TerminationParameter,
    set_termination_attribute,
    set_termination_attributes,
    AfterMaximumIterations,
    AtObjectiveThreshold,
    AtGradientThreshold

# Include files
include("types/types.jl")
include("step/step.jl")
include("prox/prox.jl")
include("termination/termination.jl")
include("execution/execution.jl")
include("solver.jl")
include("MOI_wrapper.jl")

end # module


text:
try Pkg.clone("https://github.com/zsunberg/ContinuumWorld.jl") end

using ContinuumWorld
using POMDPs
using GridInterpolations
using Reel
using Plots;        pyplot()

w = CWorld()

nx = 30; ny = 30
grid = RectangleGrid(linspace(w.xlim..., nx), linspace(w.ylim..., ny))
solver = CWorldSolver(max_iters=30, m=50, grid=grid)
policy = solve(solver, w)

frames = Frames(MIME("image/png"), fps=4)
for i in 1:length(solver.value_hist)
    v = solver.value_hist[i]
    push!(frames, CWorldVis(w, f=s->evaluate(v,s), g=solver.grid, title="Value iteration step $i"))
    print(".")
end
for i in 1:10
    push!(frames, CWorldVis(w, f=s->action_ind(policy, s), g=solver.grid, title="Policy"))
    print(".")
end
println()
write("out.gif", frames)


text:
# load Statistics package for mean and median calculations
using Statistics

#=
Example code
Generates a random array and uses it to simulate coin flips
=#

randflips = rand(10);
heads = sum(randflips.>0.5)./length(randflips);
println("Heads percentage is: ",floor(heads*10000)/100,"%" )

#=
Exercise 1

Creates a file 'squares.txt' consisting of the first 5 square numbers
=#

#=
Exercise 2

Write a script which creates a new file called large_cities.txt.
The file should contain one line for each of the cities  which have a
population larger than 10,000,000., formatted as follows:
    Buenos Aires, Argentina: ?population 11862073
    Sao Paulo, Brazil: population 14433147.5
    ...
=#

#=
Exercise 3

Read data.txt given in the Public Folder and plot the results.
What do you see?

=#

#=
Exercise 4
	Plot a histogram of the longitudes of the world's cities. What is the mean and median longitude?
=#

max_stars_repo_path: slides2021/day2/images_data/Lesson 2.jl

max_stars_repo_name: oist/skillpill-julia

max_issues_repo_path: slides2019/Lesson 2.jl

max_issues_repo_name: oist/skillpill-julia

max_forks_repo_path: slides2019/Lesson 2.jl

max_forks_repo_name: oist/skillpill-julia


text:
module FixedPoint

export AbstractFixed
abstract type AbstractFixed{f, T <: Signed} <: Real end

export Fixed
struct Internal end
struct Fixed{f, T <: Signed} <: AbstractFixed{f, T}
    m::T
    function Fixed{f, T}(::Internal, m::T) where {f, T <: Signed}
        # We could handle this, but would need to special-case range
        # calculations size "sizeof" doesn't work then
        @assert T !== BigInt
        @assert typeof(f) <: Integer
        # Ensure +1 can be represented
        @assert f >= 0 && f < 8*sizeof(T)-1
        new{f, T}(m)
    end
end

Fixed{f}(::Internal, m::T) where {f, T <: Signed} = Fixed{f, T}(Internal(), m)

Base.eltype(::Type{Fixed{f, T}}) where {f, T} = T
Base.eltype(::Fixed{f, T}) where {f, T} = T
Base.precision(::Type{Fixed{f, T}}) where {f, T} = f
Base.precision(::Fixed{f}) where {f} = f

Base.numerator(x::Fixed{f, T}) where {f, T} = x.m
Base.denominator(x::Fixed{f, T}) where {f, T} = T(1) << Unsigned(f)

# Trivial conversion
function Fixed{f, T}(x::Fixed{f, T}) where {f, T <: Signed}
    x
end

# Convert between different representation types (with same number of
# fraction bits)
function Fixed{f, T}(x::Fixed{f, U}) where {f, T <: Signed, U <: Signed}
    # Note: if we convert to a smaller type, then we need to check for
    # overflow. Luckily, Julia does this automatically, so we don't
    # need to handle this.
    Fixed{f, T}(Internal(), T(x.m))
end

# Convert between different numbers of fraction bits (with same
# representation type)
function Fixed{f, T}(x::Fixed{g, T}) where {f, g, T <: Signed}
    s = Signed(f) - Signed(g)
    if s >= 0
        # No rounding necessary, just shift
        # Check for overflow
        @assert x.m >> (Unsigned(8*sizeof(T)) - Unsigned(s)) == -T(signbit(x.m))
        Fixed{f, T}(Internal(), x.m << Unsigned(s))
    else
        # Round by adding and offset before shifting

        Fg = Fixed{g, T}
        z = Fg(Internal(), T(0))
        e = Fg(Internal(), T(1))
        h = Fg(Internal(), T(1) << Unsigned(-s-1))
        o = Fg(Internal(), T(1) << Unsigned(-s))

        # Round towards -infinity
        offset_floor = z
        # Round towards +infinity
        offset_ceil = o - e
        # Break ties towards -infinity
        offset_down = h - e
        # Break ties towards +infinity
        offset_up = h
        # Break ties towards the next even number
        offset_even = h - e + Fg(Internal(), (x.m >> Unsigned(-s)) & T(1))

        y = x + offset_even
        Fixed{f, T}(Internal(), y.m >> Unsigned(-s))
    end
end

function Fixed{f}(x::Fixed{g, T}) where {f, g, T <: Signed}
    Fixed{f, T}(x)
end

# General conversion
function Fixed{f, T}(x::Fixed{g, U}) where {f, g, T <: Signed, U <: Signed}
    if sizeof(T) <= sizeof(U)
        # shift first, convert later
        Fixed{f, T}(Fixed{f, U}(x))
    else
        # convert first, shift later
        Fixed{f, T}(Fixed{g, T}(x))
    end
end

# Create from an integer
function Fixed(x::T) where {T <: Signed}
    Fixed{0, T}(Internal(), x)
end
function Fixed{f, T}(x::Integer) where {f, T <: Signed}
    Fixed{f, T}(Fixed(x))
end
function Fixed{f, T}(x::BigInt) where {f, T <: Signed}
    Fixed{f, T}(Int128(x))
end

# Create from a rational
function Fixed{f, T}(x::Rational{I}) where {f, T <: Signed, I <: Signed}
    U = promote_type(T, I)
    Fixed{f, T}(Fixed{f, U}(numerator(x)) / Fixed{f, U}(denominator(x)))
end
function Fixed{f, T}(x::Rational{BigInt}) where {f, T <: Signed}
    Fixed{f, T}(Rational{Int64}(x))
end

# Create from a floating point number
function Fixed{f, T}(x::AbstractFloat) where {f, T <: Signed}
    Fixed{f, T}(Internal(), round(T, ldexp(x, Unsigned(f))))
end

# Convert to a rational number
function (::Type{R})(x::Fixed{f, T})::R where {f, T <: Signed, R <: Rational}
    # This uses the precison of T to normalize the result, which might
    # not be sufficient
    # R(x.m,  T(1) << Unsigned(f))
    R(x.m) // (T(1) << Unsigned(f))
end

# Convert to a floating-point number
function Base.float(::Type{Fixed{f, T}})::Type where {f, T}
    # f <= 11 && return Float16
    # f <= 24 && return Float32
    f <= 53 && return Float64
    BigFloat
end

function (::Type{F})(x::Fixed{f, T})::F where
        {f, T <: Signed, F <: AbstractFloat}
    # ldexp for generic arguments produces very complicated code
    # ldexp(F(x.m), -Signed(f))
    F(x.m) * ldexp(F(1), -Signed(f))
end

# Functions without value arguments

function Base.zero(::Type{Fixed{f, T}})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), T(0))
end

function Base.one(::Type{Fixed{f, T}})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), T(1) << Unsigned(f))
end

function Base.eps(::Type{Fixed{f, T}})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), T(1))
end

function Base.typemin(::Type{Fixed{f, T}})::Fixed{f, T} where {f, T}
    # Claim a symmetric range
    Fixed{f, T}(Internal(), -typemax(T))
end

function Base.typemax(::Type{Fixed{f, T}})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), typemax(T))
end

# Unary functions

function Base. +(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), + x.m)
end

function Base. -(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), - x.m)
end

function Base. *(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), *(x.m))
end

function Base. ^(x::Fixed{f, T}, a::Unsigned)::Fixed{f, T} where {f, T}
    r = one(x)
    if isodd(a)
        r = x
    end
    a >>= 1
    while a > 0
        x *= x
        if isodd(a)
            r *= x
        end
        a >>= 1
    end
    r
end

function Base. ^(x::Fixed{f, T}, a::Signed)::Fixed{f, T} where {f, T}
    if a >= 0
        x ^ Unsigned(a)
    else
        inv(x ^ Unsigned(-a))
    end
end

function Base.abs(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), abs(x.m))
end

function Base.hash(x::Fixed{f, T}, h::UInt)::UInt where {f, T}
    hash(hash(x.m, h), 0xd8bf79c19fa81795 % UInt)
end

function Base.inv(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    one(x) / x
end

function Base.sign(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(sign(x.m))
end

function Base.signbit(x::Fixed{f, T})::Bool where {f, T}
    signbit(x.m)
end

function Base.widen(::Type{Fixed{f, T}})::Type where {f, T}
    @assert widen(T) !== BigInt
    Fixed{typeof(f)(2*f), widen(T)}
end
function Base.widen(x::Fixed{f, T}) where {f, T}
    Fixed{typeof(f)(2*f)}(Fixed{f}(Internal(), widen(x.m)))
end

# Widen, but extend only the range and not the precision
export widenRange
function widenRange(::Type{Fixed{f, T}})::Type where {f, T}
    @assert widen(T) !== BigInt
    T1 = widen(T)
    Fixed{f, T}
end
function widenRange(x::Fixed{f, T}) where {f, T}
    Fixed{f}(Internal(), widen(x.m))
end

# Widen, but extend only the precision and not the range
export widenPrecision
function widenPrecision(::Type{Fixed{f, T}})::Type where {f, T}
    @assert widen(T) !== BigInt
    T1 = widen(T)
    f1 = typeof(f)(f + 8 * (sizeof(T1) - sizeof(T)))
    @assert f1 >= f
    Fixed{f1, T1}
end
function widenPrecision(x::Fixed{f, T}) where {f, T}
    x1 = widenRange(x)
    f1 = typeof(f)(f + 8 * (sizeof(x1) - sizeof(x)))
    @assert f1 >= f
    Fixed{f1}(x1)
end

# Binary functions

function Base. +(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), x.m + y.m)
end

function Base. -(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), x.m - y.m)
end

function Base.widemul(x::Fixed{f, T}, y::Fixed{f, T}) where {f, T}
    Fixed{typeof(f)(2*f)}(Internal(), widemul(x.m, y.m))
end

function Base. *(x::Fixed{f, T}, a::T)::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), x.m * a)
end
function Base. *(a, x::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), a * x.m)
end
function Base. *(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(widemul(x, y))
end

function Base. /(x::Fixed{f, T}, a::T)::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), rdd(x.m, a))
end
function Base. /(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    # x1 = widenPrecision(x)
    x1 = widen(x)
    f1 = precision(x1)
    @assert f1 - f >= f
    # Avoid double rounding
    @assert f1 - f == f
    Fixed{f, T}(Fixed{typeof(f)(f1 - f)}(Internal(), rdd(x1.m, y.m)))
end

function Base. \(a::T, x::Fixed{f, T})::Fixed{f, T} where {f, T}
    a / x
end
function Base. \(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    y / x
end

function Base. ==(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x.m == y.m
end

function Base. !=(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x.m != y.m
end

function Base. <(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x.m < y.m
end

function Base. <=(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x.m <= y.m
end

function Base. >(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x.m > y.m
end

function Base. >=(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x.m >= y.m
end

function Base.isequal(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x == y
end

function Base.isless(x::Fixed{f, T}, y::Fixed{f, T})::Bool where {f, T}
    x < y
end

function Base.copysign(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), copysign(x.m, y.m))
end

function Base.flipsign(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), flipsign(x.m, y.m))
end

function Base.max(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), max(x.m, y.m))
end

function Base.min(x::Fixed{f, T}, y::Fixed{f, T})::Fixed{f, T} where {f, T}
    Fixed{f, T}(Internal(), min(x.m, y.m))
end

# Ternary functions

function Base.fma(x::Fixed{f, T},
                  y::Fixed{f, T},
                  z::Fixed{f, T})::Fixed{f, T} where {f, T}
    r = widemul(x, y)
    Fixed{f, T}(r + Fixed{precision(r), eltype(r)}(z))
end

function Base.muladd(x::Fixed{f, T},
                     y::Fixed{f, T},
                     z::Fixed{f, T})::Fixed{f, T} where {f, T}
    x * y + z
end

# Elementary functions

function Base.sqrt(x::Fixed{f, T})::Fixed{f, T} where {f, T}
    @assert x >= zero(x)
    # Special case
    if x == zero(x)
        return zero(x)
    end
    # Initial approximation
    r = Fixed{f, T}(sqrt(Float64(x)))
    # Iteration
    half = Fixed{f, T}(1//2)
    b = 52                      # Float64 has 53 accurate bits
    while b < f
        r = half * (r + x / r)
        b *= 2                  # Each iteration doubles the accurate bits
    end
    r
end

# Helper functions

# Correctly rounded division (breaking ties towards even)
export rdd
function rdd(x::T, y::Signed)::T where {T <: Signed}
    d, m = fldmod(x, y)::Tuple{T, T}
    a2m = abs(m << 1)
    ay = abs(y)
    if a2m > ay
        # Round up
        d += T(1)
    elseif a2m == ay
        # Break tie towards even (round up if the result is odd)
        d += d & T(1)
    end
    d
end

end


text:
#Requires id or screen_names as an option
function get_users_lookup(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/lookup.json", options))
end

# Requires id OR screen_name
function get_users_show(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/show.json", options))
end

function get_users_search(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/search.json", options))
end

function get_users_profile_banner(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/profile_banner.json", options))
end

function get_users_suggestions(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/suggestions.json", options))
end

# Requires slug
function get_users_suggestions_slug(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/suggestions/:slug.json", options))
end

# Requires slug
function get_users_suggestions_slug_members(auth::TwAuth, options::Dict)
  Requests.json(get_oauth(auth, "https://api.twitter.com/1.1/users/suggestions/:slug/members.json", options))
end

max_stars_repo_name: nick-paul/JuliaTwitter

max_issues_repo_name: nick-paul/JuliaTwitter

max_forks_repo_name: nick-paul/JuliaTwitter


text:
using ThreeBodyTB
using Test
using Suppressor

TESTDIR=ThreeBodyTB.TESTDIR

@testset "test atomicproj" begin

    @suppress begin

        units_old = ThreeBodyTB.set_units()
        ThreeBodyTB.set_units(both="atomic")

        filname = "$TESTDIR/data_forces/znse.in_vnscf_vol_2/projham_K.xml.gz"
        tbc_ref = ThreeBodyTB.TB.read_tb_crys_kspace(filname)

        fil = "$TESTDIR/data_forces/znse.in_vnscf_vol_2/"
        dft = ThreeBodyTB.QE.loadXML(fil*"/qe.save/")

        tbc, tbck, proj_warn = ThreeBodyTB.AtomicProj.projwfc_workf(dft, directory=fil, nprocs=1, writefile=missing, writefilek=missing, skip_og=true, skip_proj=true, freeze=true, localized_factor = 0.15, only_kspace = false, screening = 1.0 )

        energy_tot, tbc_, conv_flag = scf_energy(tbc)

        @test (energy_tot - dft.atomize_energy) < 1e-4

#        energy_tot_ref, tbc_, conv_flag = scf_energy(tbc_ref)
        energy_tot_ref, X = ThreeBodyTB.TB.get_energy_electron_density_kspace(tbc_ref)

        @test (energy_tot_ref - dft.atomize_energy) < 1e-4

        ThreeBodyTB.set_units(energy = units_old[1], length=units_old[2])

    end

end


text:
"""
    distplot(x; kwargs...)
    distplot!(x; kwargs...)

Plot distribution as histogram, KDE, or ECDF.

By default continuous variables are plotted using KDEs and discrete ones using histograms

# Positional Arguments
- `x`: values to plot

# Keyword Arguments
- `kind=:auto`: By default (`:auto`), continuous variables are plotted using KDEs and
    discrete ones using histograms. The following values can override this behavior:
    + `kind=:kde`: Plot using a kernel density estimate (see [`kdeplot`](@ref))
    + `kind=:hist`: Plot using a histogram (see [`Plots.histogram`]
    + `kind=:ecdf`: Plot using an empirical cumulative distribution function
        (see [`ecdfplot`](@ref))
- `circular=false`: Select input type in `(:radians, :degrees)` for circular histogram or
    KDE plot. If `true`, default input type is `:radians`. Pass `projection=:polar` to also
    plot on a polar axis; however, this will not plot correctly when `circular=:degrees`.
- `kwargs`: Additional attributes understood by Plots.jl. In particular, see keyword
    arguments for [`kdeplot`](@ref) and `histogram`.
"""
RecipesBase.@shorthands distplot

RecipesBase.@recipe function f(::Type{Val{:distplot}}, x, y, z; kind=:auto, circular=false)
    if kind == :hist || (kind === :auto && all(isinteger, y))
        bins = get(plotattributes, :bins, get_bins(y))
        if circular === false
            seriestype := :barhist
        else
            # barhist does not work correctly for polar projection for GR backend
            # see https://github.com/JuliaPlots/Plots.jl/issues/1952
            seriestype := :stephist
            fill --> true
        end
        bins := bins
        normalize --> :pdf
        orientation := isrotated(plotattributes) ? :horizontal : :vertical
    elseif kind === :kde || kind === :auto
        seriestype := :kdeplot
    elseif kind === :ecdf
        seriestype := :ecdfplot
    end
    y := y
    return ()
end


text:
## UTF-32 tests

let u8 = "\U10ffff\U1d565\U1d7f6\U00066\U2008a",
    u32 = utf32(u8)

    @test sizeof(u32) == 20
    @test length(u32) == 5
    @test utf8(u32) == u8
    @test collect(u8) == collect(u32)
    @test_throws StringError utf32(UInt8[1,2,3])
end


text:
module YetAnotherHelperRemoveLightSourcesTrigger

using ..Ahorn, Maple

@mapdef Trigger "YetAnotherHelper/RemoveLightSourcesTrigger" RemoveLightSourcesTrigger(x::Integer, y::Integer, width::Integer=Maple.defaultTriggerWidth, height::Integer=Maple.defaultTriggerHeight,
    offsetTo::Number=0.0, offsetFrom::Number=0.0, positionMode::String="NoEffect", onlyOnce::Bool=false)

const placements = Ahorn.PlacementDict(
    "Remove Light Sources (Yet Another Helper)" => Ahorn.EntityPlacement(
        RemoveLightSourcesTrigger,
        "rectangle",
    ),
)

function Ahorn.editingOptions(trigger::RemoveLightSourcesTrigger)
	return Dict{String, Any}(
		"positionMode" => Maple.trigger_position_modes
	)
end

end

max_stars_repo_name: YetAnotherHelper/YetAnotherHelper

max_issues_repo_name: YetAnotherHelper/YetAnotherHelper

max_forks_repo_name: YetAnotherHelper/YetAnotherHelper


text:
function compare_power_flows(first_network, second_network, plot_dir)
    if split(lowercase(split(first_network, "/")[end]), '.')[end] == "json"
        first_mn_data = JSON.parsefile(first_network)
    else
        first_mn_data = PowerModels.parse_file(first_network)
    end
    # println("Finished parsing the first network")
    if split(lowercase(split(second_network, "/")[end]), '.')[end] == "json"
        second_mn_data = JSON.parsefile(second_network)
    else
        second_mn_data = PowerModels.parse_file(second_network)
    end
    # println("Finished parsing the second network")

    first_ref = PowerModels.build_ref(first_mn_data)[:sn]
    # println("Finished building ref for the first network")
    second_ref = PowerModels.build_ref(second_mn_data)[:sn]
    # println("Finished building ref for the second network")

    table_string = ""
    for (subnet_idx, ref_subnet) in first_ref
        println("subnet_idx: $subnet_idx")
        if (subnet_idx) == 0 && !(subnet_idx in keys(second_ref))
            subnet_idx = 1
        end
        if (subnet_idx) == 1 && !(subnet_idx in keys(second_ref))
            subnet_idx = 0
        end
        # total, max, min, plot all
        gen_compared_idx = Array{Number}(undef, length(ref_subnet[:gen]))
        gen_pg1 = Array{Float64}(undef, length(ref_subnet[:gen]))
        gen_qg1 = Array{Float64}(undef, length(ref_subnet[:gen]))
        gen_vm1 = Array{Float64}(undef, length(ref_subnet[:gen]))
        gen_pg2 = Array{Float64}(undef, length(ref_subnet[:gen]))
        gen_qg2 = Array{Float64}(undef, length(ref_subnet[:gen]))
        gen_vm2 = Array{Float64}(undef, length(ref_subnet[:gen]))
        for (i,(gen_i,gen)) in enumerate(ref_subnet[:gen])
            if ref_subnet[:gen][gen_i]["gen_status"] == 0
                gen_pg1[i] = NaN
                gen_qg1[i] = NaN
                gen_vm1[i] = NaN
                gen_pg2[i] = NaN
                gen_qg2[i] = NaN
                gen_vm2[i] = NaN
                gen_compared_idx[i] = NaN
            else
                gen_pg1[i] = ref_subnet[:gen][gen_i]["pg"]
                gen_qg1[i] = ref_subnet[:gen][gen_i]["qg"]
                gen_vm1[i] = ref_subnet[:gen][gen_i]["vg"]
                gen_pg2[i] = second_ref[subnet_idx][:gen][gen_i]["pg"]
                gen_qg2[i] = second_ref[subnet_idx][:gen][gen_i]["qg"]
                gen_vm2[i] = second_ref[subnet_idx][:gen][gen_i]["vg"]
                gen_compared_idx[i] = gen_i
            end
        end
        filter!(e->e≠NaN,gen_pg1)
        filter!(e->e≠NaN,gen_qg1)
        filter!(e->e≠NaN,gen_vm1)
        filter!(e->e≠NaN,gen_pg2)
        filter!(e->e≠NaN,gen_qg2)
        filter!(e->e≠NaN,gen_vm2)
        filter!(e->e≠NaN,gen_compared_idx)
        gen_pg_compared = gen_pg2 - gen_pg1
        gen_qg_compared = gen_qg2 - gen_qg1
        gen_vm_compared = gen_vm2 - gen_vm1

        # bus_vm_compared = Array{Float64}(undef, length(ref_subnet[:bus]))
        # bus_va_compared = Array{Float64}(undef, length(ref_subnet[:bus]))
        bus_compared_idx = Array{Number}(undef, length(ref_subnet[:bus]))
        bus_vm1 = Array{Float64}(undef, length(ref_subnet[:bus]))
        bus_va1 = Array{Float64}(undef, length(ref_subnet[:bus]))
        bus_vm2 = Array{Float64}(undef, length(ref_subnet[:bus]))
        bus_va2 = Array{Float64}(undef, length(ref_subnet[:bus]))
        for (i,(bus_i,bus)) in enumerate(ref_subnet[:bus])
            if bus["bus_type"] ∉ [1,2,3]
                bus_vm1[i] = NaN
                bus_va1[i] = NaN
                bus_vm2[i] = NaN
                bus_va2[i] = NaN
                bus_compared_idx[i] = NaN
            else
                bus_vm1[i] = ref_subnet[:bus][bus_i]["vm"]
                bus_va1[i] = ref_subnet[:bus][bus_i]["va"]
                bus_vm2[i] = second_ref[subnet_idx][:bus][bus_i]["vm"]
                bus_va2[i] = second_ref[subnet_idx][:bus][bus_i]["va"]
                bus_compared_idx[i] = bus_i
            end
        end
        filter!(e->e≠NaN,bus_vm1)
        filter!(e->e≠NaN,bus_va1)
        filter!(e->e≠NaN,bus_vm2)
        filter!(e->e≠NaN,bus_va2)
        filter!(e->e≠NaN,bus_compared_idx)
        bus_vm_compared = bus_vm2 - bus_vm1
        bus_va_compared = bus_va2 - bus_va1

        branch_compared_idx = Array{Number}(undef, length(ref_subnet[:branch]))
        branch_pt1 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_pf1 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_qt1 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_qf1 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_pt2 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_pf2 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_qt2 = Array{Float64}(undef, length(ref_subnet[:branch]))
        branch_qf2 = Array{Float64}(undef, length(ref_subnet[:branch]))
        for (i,(branch_i,branch)) in enumerate(ref_subnet[:branch])
            if ("pt" ∉ keys(branch)) || ("pt" ∉ keys(second_ref[subnet_idx][:branch][branch_i]))
                branch_pt1[i] = NaN
                branch_pf1[i] = NaN
                branch_qt1[i] = NaN
                branch_qf1[i] = NaN
                branch_pt2[i] = NaN
                branch_pf2[i] = NaN
                branch_qt2[i] = NaN
                branch_qf2[i] = NaN
                branch_compared_idx[i] = NaN
            else
                branch_pt1[i] = ref_subnet[:branch][branch_i]["pt"]
                branch_pf1[i] = ref_subnet[:branch][branch_i]["pf"]
                branch_qt1[i] = ref_subnet[:branch][branch_i]["qt"]
                branch_qf1[i] = ref_subnet[:branch][branch_i]["qf"]
                branch_pt2[i] = second_ref[subnet_idx][:branch][branch_i]["pt"]
                branch_pf2[i] = second_ref[subnet_idx][:branch][branch_i]["pf"]
                branch_qt2[i] = second_ref[subnet_idx][:branch][branch_i]["qt"]
                branch_qf2[i] = second_ref[subnet_idx][:branch][branch_i]["qf"]
                branch_compared_idx[i] = branch_i
            end
        end
        filter!(e->e≠NaN,branch_pt1)
        filter!(e->e≠NaN,branch_pf1)
        filter!(e->e≠NaN,branch_qt1)
        filter!(e->e≠NaN,branch_qf1)
        filter!(e->e≠NaN,branch_pt2)
        filter!(e->e≠NaN,branch_pf2)
        filter!(e->e≠NaN,branch_qt2)
        filter!(e->e≠NaN,branch_qf2)
        filter!(e->e≠NaN,branch_compared_idx)
        branch_pt_compared = branch_pt2 - branch_pt1
        branch_pf_compared = branch_pf2 - branch_pf1
        branch_qt_compared = branch_qt2 - branch_qt1
        branch_qf_compared = branch_qf2 - branch_qf1

        GR.inline("pdf")

        output_dir = "$(plot_dir)/subnet$(subnet_idx)"
        if !isdir(output_dir)
            mkpath(output_dir)
        end
        upscale = 2 #upscaling in resolution
        fntsm = font("serif", pointsize=round(8.0*upscale))
        fntlg = font("serif", pointsize=round(12.0*upscale))
        default(titlefont=fntlg, guidefont=fntlg, tickfont=fntsm, legendfont=fntsm)
        default(size=(800*upscale,600*upscale)) #Plot canvas size

        gr()

        plot_data = [gen_pg_compared, gen_qg_compared,
                     bus_vm_compared, bus_va_compared,
                     branch_pt_compared, branch_pf_compared, branch_qt_compared, branch_qf_compared
                     ]
        raw_data = [gen_pg1,
                    gen_qg1,
                    bus_vm1,
                    bus_va1,
                    branch_pt1,
                    branch_pf1,
                    branch_qt1,
                    branch_qf1
                    ]
        plot_idx = [gen_compared_idx, gen_compared_idx,
                    bus_compared_idx, bus_compared_idx,
                    branch_compared_idx, branch_compared_idx, branch_compared_idx, branch_compared_idx
                    ]
        plot_labels = ["gen_pg", "gen_qg",
                       "bus_vm", "bus_va",
                       "branch_pt", "branch_pf", "branch_qt", "branch_qt"
                       ]

        for (i,series) in enumerate(plot_data)

            series_idx = plot_idx[i]

            (max_val, max_i) = findmax(series)
            (min_val, min_i) = findmin(series)
            idx_max = series_idx[max_i]
            idx_min = series_idx[min_i]

            # tol = 1e-3
            # worst = series[abs.(series).>tol]
            # idx_worst = series_idx[abs.(series).>tol]
            perm_worst = sortperm(abs.(series), rev=true)
            ten_worst = series[perm_worst][1:10]
            idx_ten_worst = series_idx[perm_worst][1:10]
            percent = ten_worst ./ raw_data[i][perm_worst][1:10]

            result_string = "$(plot_labels[i])\n==========\nMaximum difference: $(maximum(series))\nMinimum difference: $(minimum(series))\nMean absolute difference: $(mean(abs.(series)))\n"*
                            "biggest differences: $ten_worst\nat indices: $idx_ten_worst\npercent difference = $percent\n"

            println(result_string)

            open("$output_dir/comparison_log.txt", "a") do f
                write(f, result_string)
            end

            table_string = "$table_string,$(max_val),$(min_val),$(mean(abs.(series)))"

            perm = sortperm(series_idx)
            x_sorted = series_idx[perm]
            y_sorted = series[perm]
            series_label = plot_labels[i]
            plt = plot(1:length(x_sorted), y_sorted,
                label=series_label,
                legend=true,
                left_margin=(10*upscale)*mm,
                right_margin=(10*upscale)*mm, top_margin=(10*upscale)*mm, bottom_margin=(10*upscale)*mm,
                linetype=:scatter,
                marker = (:dot, 1.5*upscale, 0.6, :green, stroke(0))
            )
            xlabel!(plt, "index")
            ylabel!(plt, series_label)

            savefig(plt,"$(output_dir)/$(series_label).pdf")

            bar_plot = false
            if bar_plot
                plt = bar(x_sorted, y_sorted,
                    label=series_label,
                    legend=false,
                    left_margin=(10*upscale)*mm,
                    right_margin=(10*upscale)*mm, top_margin=(10*upscale)*mm, bottom_margin=(10*upscale)*mm
                )
                xlabel!(plt, "index")
                ylabel!(plt, series_label)
                # savefig(p,"$(folder)/$(y_label) vs $(x_label).pdf")
                if !isdir(output_dir)
                    mkpath(output_dir)
                end
                # println("saving plot at $(output_folder)/plots/$plotdir/$(y_label) vs $(x_label).pdf")
                # display(plt)
                savefig(plt,"$(output_dir)/$(series_label)_bar.pdf")
            end
        end

        plot_raw_data = [[gen_pg1, gen_pg2],
                         [gen_qg1, gen_qg1],
                         [bus_vm1, bus_vm2],
                         [bus_va1, bus_va2]
                         ]
        plot_idx = [gen_compared_idx,
                    gen_compared_idx,
                    bus_compared_idx,
                    bus_compared_idx
                    ]
        plot_labels = ["gen_pg values",
                       "gen_qg values",
                       "bus_vm values",
                       "bus_va values"
                       ]
        for (i,series) in enumerate(plot_raw_data)
            series_idx = plot_idx[i]
            perm = sortperm(series_idx)
            x_sorted = series_idx[perm]
            series_label = plot_labels[i]
            y1_sorted = series[1][perm]
            plt = plot(1:length(x_sorted), y1_sorted,
                label="solution 1",
                legend=true,
                left_margin=(10*upscale)*mm,
                right_margin=(10*upscale)*mm, top_margin=(10*upscale)*mm, bottom_margin=(10*upscale)*mm,
                linetype=:scatter,
                marker = (:dot, 1.5*upscale, 0.6, :blue, stroke(0))
            )
            y2_sorted = series[2][perm]
            plt = plot!(plt,1:length(x_sorted), y2_sorted,
                label="solution 2",
                legend=true,
                left_margin=(10*upscale)*mm,
                right_margin=(10*upscale)*mm, top_margin=(10*upscale)*mm, bottom_margin=(10*upscale)*mm,
                linetype=:scatter,
                marker = (:dot, 1.5*upscale, 0.6, :red, stroke(0))
            )
            xlabel!(plt, "index")
            ylabel!(plt, series_label)

            savefig(plt,"$(output_dir)/$(series_label).pdf")
        end
    end
    return table_string
end


text:
immutable Cauchy <: ContinuousUnivariateDistribution
    location::Float64
    scale::Float64
    function Cauchy(l::Real, s::Real)
        s > zero(s) || error("scale must be positive")
	new(float64(l), float64(s))
    end
end

Cauchy(l::Real) = Cauchy(l, 1.0)
Cauchy() = Cauchy(0.0, 1.0)

@_jl_dist_2p Cauchy cauchy

entropy(d::Cauchy) = log(d.scale) + log(4.0 * pi)

insupport(::Cauchy, x::Real) = isfinite(x)
insupport(::Type{Cauchy}, x::Real) = isfinite(x)

kurtosis(d::Cauchy) = NaN

mean(d::Cauchy) = NaN

median(d::Cauchy) = d.location

mgf(d::Cauchy, t::Real) = NaN

function cf(d::Cauchy, t::Real)
    exp(im * t * d.location - d.scale * abs(t))
end

mode(d::Cauchy) = d.location
modes(d::Cauchy) = [mode(d)]

skewness(d::Cauchy) = NaN

var(d::Cauchy) = NaN

function fit_mle{T <: Real}(::Type{Cauchy}, x::Array{T})
    l, u = iqr(x)
    Cauchy(median(x), (u - l) / 2.0)
end


text:
# File   : spherical_cylinder_approx.jl
# License: GNU v3.0
# Author : Andrei Leonard Nicusan <a.l.nicusan@bham.ac.uk>
# Date   : 28.08.2021

using DelimitedFiles
using LinearAlgebra

using GLMakie
using GeometryBasics

# Vertices and face indices generated using Autodesk; two spherical caps were created, then the
# convex hull was saved in the "spherical_cylinder_sparse.obj" file
verts = [
    -0.383022 0.321394 0.000000
    -0.375000 0.216506 -0.250000
    -0.500000 -0.000000 0.000000
    -0.426434 -0.075192 -0.250000
    -0.383022 -0.321394 -0.000000
    -0.278335 -0.331707 -0.250000
    -0.086824 -0.492404 -0.000000
    0.000000 -0.433013 -0.250000
    0.250000 -0.433013 -0.000000
    0.278335 -0.331707 -0.250000
    0.469846 -0.171010 0.000000
    0.426434 -0.075192 -0.250000
    0.469846 0.171010 0.000000
    0.375000 0.216506 -0.250000
    0.250000 0.433013 0.000000
    0.148099 0.406899 -0.250000
    -0.086824 0.492404 0.000000
    -0.148099 0.406899 -0.250000
    0.000000 -0.250000 -0.433013
    0.000000 0.000000 -0.500000
    0.160697 -0.191511 -0.433013
    0.246202 -0.043412 -0.433013
    0.216506 0.125000 -0.433013
    0.085505 0.234923 -0.433013
    -0.085505 0.234923 -0.433013
    -0.216506 0.125000 -0.433013
    -0.246202 -0.043412 -0.433013
    -0.160697 -0.191511 -0.433013
    -0.500000 -0.000000 1.000000
    -0.383022 0.321394 1.000000
    -0.086824 0.492404 1.000000
    0.250000 0.433013 1.000000
    0.469846 0.171010 1.000000
    0.469846 -0.171010 1.000000
    0.250000 -0.433013 1.000000
    -0.086824 -0.492404 1.000000
    -0.383022 -0.321394 1.000000
    -0.426434 -0.075192 1.250000
    -0.375000 0.216506 1.250000
    -0.148099 0.406899 1.250000
    0.148099 0.406899 1.250000
    0.375000 0.216506 1.250000
    0.426434 -0.075192 1.250000
    0.278335 -0.331707 1.250000
    -0.000000 -0.433013 1.250000
    -0.278335 -0.331707 1.250000
    -0.000000 -0.250000 1.433013
    -0.000000 -0.000000 1.500000
    -0.160697 -0.191511 1.433013
    -0.246202 -0.043412 1.433013
    -0.216506 0.125000 1.433013
    -0.085505 0.234923 1.433013
    0.085505 0.234923 1.433013
    0.216506 0.125000 1.433013
    0.246202 -0.043412 1.433013
    0.160697 -0.191511 1.433013
]

faces = [
    1 2 3
    3 2 4
    3 4 5
    5 4 6
    5 6 7
    7 6 8
    7 8 9
    9 8 10
    9 10 11
    11 10 12
    11 12 13
    13 12 14
    13 14 15
    15 14 16
    15 16 17
    17 16 18
    17 18 1
    1 18 2
    19 20 21
    21 20 22
    22 20 23
    24 20 25
    25 20 26
    27 20 28
    28 20 19
    8 19 10
    10 19 21
    10 21 12
    12 21 22
    12 22 14
    14 22 23
    14 23 16
    16 23 24
    16 24 18
    18 24 25
    18 25 2
    2 25 26
    2 26 4
    4 26 27
    4 27 6
    6 27 28
    6 28 8
    8 28 19
    23 20 24
    26 20 27
    5 29 3
    3 29 30
    3 30 1
    1 30 31
    1 31 17
    17 31 32
    17 32 15
    15 32 33
    15 33 13
    13 33 34
    13 34 11
    11 34 35
    11 35 9
    9 35 36
    9 36 7
    7 36 37
    7 37 5
    5 37 29
    37 38 29
    29 38 39
    29 39 30
    30 39 40
    30 40 31
    31 40 41
    31 41 32
    32 41 42
    32 42 33
    33 42 43
    33 43 34
    34 43 44
    34 44 35
    35 44 45
    35 45 36
    36 45 46
    36 46 37
    37 46 38
    47 48 49
    49 48 50
    50 48 51
    52 48 53
    53 48 54
    55 48 56
    56 48 47
    45 47 46
    46 47 49
    46 49 38
    38 49 50
    38 50 39
    39 50 51
    39 51 40
    40 51 52
    40 52 41
    41 52 53
    41 53 42
    42 53 54
    42 54 43
    43 54 55
    43 55 44
    44 55 56
    44 56 45
    45 56 47
    54 48 55
    51 48 52
]

# Index from 0, as expected by the C code
faces .-= 1

# Split vertices into the bottom and top caps
half = size(verts)[1] ÷ 2

verts_bottom = zeros(half, 3)
index_bottom = 1

verts_top = zeros(half, 3)
index_top = 1

faces_split = copy(faces)

for (i, v) in enumerate(eachrow(verts))
    if v[3] < 0.5
        verts_bottom[index_bottom, :] .= v
        faces_split[faces .== i - 1] .= index_bottom - 1

        global index_bottom += 1
    else
        verts_top[index_top, :] .= v
        faces_split[faces .== i - 1] .= index_top - 1 + half

        global index_top += 1
    end
end

verts_split = [verts_bottom ; verts_top]

# Move top cap back to (0, 0, 0) and flip it
verts_split[half + 1:end, 3] .-= 1
verts_split[half + 1:end, 3] .*= -1

# Expand vertices from diameter 2 to radius 2
verts_split .*= 2

# Write processed vertices and face indices to CSV files
open("spherical_cylinder_verts_sparse.csv", "w") do io
    writedlm(io, verts_split, ',')
end

open("spherical_cylinder_faces_sparse.csv", "w") do io
    writedlm(io, faces_split, ',')
end

# Create spherical cylinder spanning two points p1 and p2 on a particle trajectory
p1 = [5, 4, 3]
r1 = 2

p2 = [6, 7, 8]
r2 = 4

# Expand vertices to given particles' radii
verts_split[1:half, :] .*= r1
verts_split[half + 1:end, :] .*= r2

# Create rotation matrix from vertical +Z to |p2 - p1|
a = [0, 0, 1]
b = normalize(p2 .- p1)

v = cross(a, b)
c = dot(a, b)
vx = [  0    -v[3]   v[2]
       v[3]    0    -v[1]
      -v[2]   v[1]    0  ]

rot = I(3) + vx + vx * vx / (1 + c)

# Rotate all vertices
verts_split = (rot * verts_split')'

# Translate vertices
verts_split[1:half, :] .+= p1'
verts_split[half + 1:end, :] .+= p2'

# Plot mesh and direction from p1 to p2
m = GeometryBasics.Mesh(
    Point3.(eachrow(verts_split)),
    TriangleFace.(eachrow(faces_split .+ 1)),
)

wireframe(m)
lines!([p1 p2], color = :red)

current_figure()

max_stars_repo_name: anicusan/KonigCell

max_issues_repo_name: anicusan/KonigCell

max_forks_repo_name: anicusan/KonigCell


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

using Markdown
using InteractiveUtils

# ╔═╡ 65c609be-2533-11ec-2020-6b6774892850
using PlutoUI, CSV, DataFrames, HTTP

# ╔═╡ be39bcf7-442a-4033-aa7e-59fcd4076187
html"""
Data analysis of the billboard 100 data
"""

# ╔═╡ 724b1eef-a386-48f2-b8e3-8487d7ee8d93
md"""This document is part of the showcase, where I replicate the same brief and simple analyses with different tools.

This particular file focuses on data analysis (a few queries) of the billboard 100 data from the tidytuesday project.

The data can be found in https://github.com/rfordatascience/tidytuesday/tree/master/data/2021/2021-09-14. They consist of two documents: billboard.csv contains information about the songs focusing on their position in the top100 list at different weeks. audio_features.csv contains information about specific attributes of the songs from spotify.

For the specific analysis I will use Julia and DataFrames (plus Pluto notebook).

We start by loading the packages:"""

# ╔═╡ b7a4eeef-f473-4edf-a6e9-f8378e8e06a8
md"""and the billboard datset:"""

# ╔═╡ d63d223d-2cfc-4e99-b461-924a061a359d
html_billboard =
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-14/billboard.csv"

# ╔═╡ 606b48b3-e9c3-4988-ac63-3e0fb23dc4ce
billboard = CSV.read(IOBuffer(HTTP.get(html_billboard).body), DataFrame)

# ╔═╡ 133aff07-805d-45e7-9b59-937ab2144cbf
md"""We can have a look at the schema of the billboard data column names:"""

# ╔═╡ 82ee46dc-6bb1-4e42-a9d5-6dfdf0f03775
propertynames(billboard)

# ╔═╡ 15ac908d-536c-42bd-b2cb-7725dc0b7478
md"""column types:"""

# ╔═╡ 8c849c6c-9631-48ce-ae2a-bb1e5b2aa1f9
eltype.(eachcol(billboard))

# ╔═╡ 8a7e85fa-d19d-4db9-a678-1dcc39963bdf
md"""and summary statistics:"""

# ╔═╡ 5788e2a2-fc10-48d4-9bec-6f67510f228c
describe(billboard)

# ╔═╡ a619f031-6242-428d-9047-880531dde984
md"""For the first main query, our aim is to select only the songs that have reached the No 1 spot of the billboard and see how many weeks they have stayed at the billboard in total:"""

# ╔═╡ 217356ed-5dd6-4dc5-87c3-1d973251670e
top10 = filter(:peak_position => peak_position -> peak_position ==1, billboard)

# ╔═╡ 49d282ac-7dcf-4f55-837f-0db4a75f883b
select!(top10, Not([:url, :peak_position, :previous_week_position]))

# ╔═╡ 309b346d-a9bc-4676-bb7e-2dfb43f20be7
top10a = combine(groupby(top10,[:performer,:song]),:weeks_on_chart=>maximum=>:max_weeks)

# ╔═╡ 42b51b71-e09b-40d5-8fad-1ae1c1966fc0
sort!(top10a, :max_weeks, rev = true)

# ╔═╡ 92893b05-9d30-425a-b49c-0730e963ce97
first(top10a,10)

# ╔═╡ 430cff96-c69e-4201-b8d0-61c73b1e8327
md"""Now we load the audio features dataset:"""

# ╔═╡ 0e5af95f-6784-4d41-8adc-ad3257c3aa42
html_audio =
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-14/audio_features.csv"

# ╔═╡ 37cf7988-7ff6-49bf-a416-f48aa25049e9
audio_features = CSV.read(IOBuffer(HTTP.get(html_audio).body), DataFrame)

# ╔═╡ 5907712d-217e-4346-8e27-11f055514ce9
md"""And once again we look at the summary statistics:"""

# ╔═╡ 459ba1cc-0177-4a74-a0a9-16443dd1c695
describe(audio_features)

# ╔═╡ ff251902-f6fc-4034-8874-a0cffc323f28
md"""For the second main query, our aim is to derive information about the peak position a song has reached in the billboard and the main spotify information. For this, we need to join the two datasets:"""

# ╔═╡ ec860cdd-a70d-49e2-8144-09c392eec561
left_join = combine(groupby(billboard,[:performer,:song,:song_id]),:peak_position=>maximum=>:best_position)

# ╔═╡ d2d07573-535d-4aec-b27e-845c29f9e564
right_join = select(audio_features, [:song_id, :spotify_genre, :danceability, :energy, :key, :loudness, :speechiness, :acousticness, :instrumentalness, :liveness, :valence, :tempo])

# ╔═╡ 38b3b953-26b9-4c40-bd8b-465de83dc499
data_join = innerjoin(left_join, right_join, on = :song_id)

# ╔═╡ 2f8e82e3-51a6-4149-9680-096c4fed2019
sort!(data_join, :performer)

# ╔═╡ cfe70661-6b57-4554-a39c-c29d4dc31118
first(data_join,10)

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"

[compat]
CSV = "~0.9.6"
DataFrames = "~1.2.2"
HTTP = "~0.9.16"
PlutoUI = "~0.7.14"
"""

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

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

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

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

[[CSV]]
deps = ["CodecZlib", "Dates", "FilePathsBase", "InlineStrings", "Mmap", "Parsers", "PooledArrays", "SentinelArrays", "Tables", "Unicode", "WeakRefStrings"]
git-tree-sha1 = "567d865fc5702dc094e4519daeab9e9d44d66c63"
uuid = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
version = "0.9.6"

[[CodecZlib]]
deps = ["TranscodingStreams", "Zlib_jll"]
git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da"
uuid = "944b1d66-785c-5afd-91f1-9de20f533193"
version = "0.7.0"

[[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 = "31d0151f5716b655421d9d75b7fa74cc4e744df2"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.39.0"

[[Crayons]]
git-tree-sha1 = "3f71217b538d7aaee0b69ab47d9b7724ca8afa0d"
uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
version = "4.0.4"

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

[[DataFrames]]
deps = ["Compat", "DataAPI", "Future", "InvertedIndices", "IteratorInterfaceExtensions", "LinearAlgebra", "Markdown", "Missings", "PooledArrays", "PrettyTables", "Printf", "REPL", "Reexport", "SortingAlgorithms", "Statistics", "TableTraits", "Tables", "Unicode"]
git-tree-sha1 = "d785f42445b63fc86caa08bb9a9351008be9b765"
uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "1.2.2"

[[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"

[[FilePathsBase]]
deps = ["Dates", "Mmap", "Printf", "Test", "UUIDs"]
git-tree-sha1 = "7fb0eaac190a7a68a56d2407a6beff1142daf844"
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
version = "0.9.12"

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

[[Future]]
deps = ["Random"]
uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"

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

[[HypertextLiteral]]
git-tree-sha1 = "72053798e1be56026b81d4e2682dbe58922e5ec9"
uuid = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
version = "0.9.0"

[[IOCapture]]
deps = ["Logging", "Random"]
git-tree-sha1 = "f7be53659ab06ddc986428d3a9dcc95f6fa6705a"
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
version = "0.2.2"

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

[[InlineStrings]]
deps = ["Parsers"]
git-tree-sha1 = "19cb49649f8c41de7fea32d089d37de917b553da"
uuid = "842dd82b-1e85-43dc-bf29-5d0ee9dffc48"
version = "1.0.1"

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

[[InvertedIndices]]
git-tree-sha1 = "bee5f1ef5bf65df56bdd2e40447590b272a5471f"
uuid = "41ab1584-1d38-5bbf-9106-f11c6c58b48f"
version = "1.1.0"

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

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

[[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"

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

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

[[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"

[[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"

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

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

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

[[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"

[[PlutoUI]]
deps = ["Base64", "Dates", "HypertextLiteral", "IOCapture", "InteractiveUtils", "JSON", "Logging", "Markdown", "Random", "Reexport", "UUIDs"]
git-tree-sha1 = "d1fb76655a95bf6ea4348d7197b22e889a4375f4"
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
version = "0.7.14"

[[PooledArrays]]
deps = ["DataAPI", "Future"]
git-tree-sha1 = "a193d6ad9c45ada72c14b731a318bedd3c2f00cf"
uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
version = "1.3.0"

[[PrettyTables]]
deps = ["Crayons", "Formatting", "Markdown", "Reexport", "Tables"]
git-tree-sha1 = "69fd065725ee69950f3f58eceb6d144ce32d627d"
uuid = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
version = "1.2.2"

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

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

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

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

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

[[SentinelArrays]]
deps = ["Dates", "Random"]
git-tree-sha1 = "54f37736d8934a12a200edea2f9206b03bdf3159"
uuid = "91c51154-3ec4-41a3-a24f-3f23e20d615c"
version = "1.3.7"

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

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

[[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"

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

[[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 = "fed34d0e71b91734bf0a7e10eb1bb05296ddbcd0"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
version = "1.6.0"

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

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

[[TranscodingStreams]]
deps = ["Random", "Test"]
git-tree-sha1 = "216b95ea110b5972db65aa90f88d8d89dcb8851c"
uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
version = "0.9.6"

[[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"

[[WeakRefStrings]]
deps = ["DataAPI", "InlineStrings", "Parsers"]
git-tree-sha1 = "c69f9da3ff2f4f02e811c3323c22e5dfcb584cfa"
uuid = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
version = "1.4.1"

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

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

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

# ╔═╡ Cell order:
# ╠═be39bcf7-442a-4033-aa7e-59fcd4076187
# ╠═724b1eef-a386-48f2-b8e3-8487d7ee8d93
# ╠═65c609be-2533-11ec-2020-6b6774892850
# ╠═b7a4eeef-f473-4edf-a6e9-f8378e8e06a8
# ╠═d63d223d-2cfc-4e99-b461-924a061a359d
# ╠═606b48b3-e9c3-4988-ac63-3e0fb23dc4ce
# ╠═133aff07-805d-45e7-9b59-937ab2144cbf
# ╠═82ee46dc-6bb1-4e42-a9d5-6dfdf0f03775
# ╠═15ac908d-536c-42bd-b2cb-7725dc0b7478
# ╠═8c849c6c-9631-48ce-ae2a-bb1e5b2aa1f9
# ╠═8a7e85fa-d19d-4db9-a678-1dcc39963bdf
# ╠═5788e2a2-fc10-48d4-9bec-6f67510f228c
# ╠═a619f031-6242-428d-9047-880531dde984
# ╠═217356ed-5dd6-4dc5-87c3-1d973251670e
# ╠═49d282ac-7dcf-4f55-837f-0db4a75f883b
# ╠═309b346d-a9bc-4676-bb7e-2dfb43f20be7
# ╠═42b51b71-e09b-40d5-8fad-1ae1c1966fc0
# ╠═92893b05-9d30-425a-b49c-0730e963ce97
# ╠═430cff96-c69e-4201-b8d0-61c73b1e8327
# ╠═0e5af95f-6784-4d41-8adc-ad3257c3aa42
# ╠═37cf7988-7ff6-49bf-a416-f48aa25049e9
# ╠═5907712d-217e-4346-8e27-11f055514ce9
# ╠═459ba1cc-0177-4a74-a0a9-16443dd1c695
# ╠═ff251902-f6fc-4034-8874-a0cffc323f28
# ╠═ec860cdd-a70d-49e2-8144-09c392eec561
# ╠═d2d07573-535d-4aec-b27e-845c29f9e564
# ╠═38b3b953-26b9-4c40-bd8b-465de83dc499
# ╠═2f8e82e3-51a6-4149-9680-096c4fed2019
# ╠═cfe70661-6b57-4554-a39c-c29d4dc31118
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002

max_stars_repo_name: themisbo/showcase

max_issues_repo_name: themisbo/showcase

max_forks_repo_name: themisbo/showcase


text:
using WinControllerToFile
subscribe()
hids = hid_dict()
keys(hids)
h = hids["hidvid_256fpid_c62e62476a257000004d1e55b2f16f11cf88cb001111000030"]
hidvec = hid_vector()
h = hidvec[1]
WinControllerToFile.documentation(stderr, h)

import WinControllerToFile.open_hid

open_hid(h)
import WinControllerToFile.is_plugged
is_plugged(h)

import WinControllerToFile.PyCall
import WinControllerToFile.pyimport

pyhid = pyimport("pywinusb.hid")
device = h.object

reps = h.object.find_input_reports()
rep1 = reps[1]
rep1_1 = pycall("rep1.get(1)")
rep1.get_raw_data()
kys = rep.keys()

rd=rep.get_raw_data()
subscribe()
POLLINGTASKS

set_data_handler(1, h)

device = h.object
device.find_output_reports()
device.find_feature_reports()
device.find_input_reports()
reps = device.find_any_reports()
device.find_input_usage()
device.get_physical_descriptor()
device.is_active()
device._process_raw_report(2)
device.HidReport
device.get_full_usage_id(0xff00, 0x02)

device.get()
get_hid_object()
get_raw_data()
get_usages()
has_key()
items()
keys()
send()
set_raw_data()
values()
_HidReport__alloc_raw_data()
_HidReport__prepare_raw_data()
__class__()
__contains__()
__delattr__()
__dir__()
__eq__()
__format__
__ge__
__getattribute__
__getitem__
__gt____hash__
__init__
__init_subclass__
__le__
__len__
__lt__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__setitem__
__sizeof__
__str__
__subclasshook__


text:
using JSOSolvers: tron
using NLPModels, CUTEst
using Logging
using LOWDER

include("autotunning.jl")

println("Step 01 ... importing packages and modules ... OK!")

# All problems with at most bounds
#const problems = [CUTEst.select(contype="unc");
#                  CUTEst.select(contype="bounds")]

# Easy problems
PRBS = setdiff(CUTEst.select(max_var=2, contype="unc"),
                         # Remove the problem(s) below
                         ["S308NE"])[1:5]

# Code to generate several f_i's

println("Step 02 ... defining set of problems ... OK!")

MAX_TIME = 2.0

flist = @copycode tron begin

    problems = copy(PRBS)

    total_time = 0.0

    n_solved = 0

    max_time = MAX_TIME

    for p in problems

        nlp = CUTEstModel(p)

        try

            s = nothing

            with_logger(NullLogger()) do

                s = tron(nlp; μ₀=tunningvar(1), μ₁=tunningvar(2), σ=tunningvar(3), cgtol=tunningvar(4),
                         max_time=max_time,
                         # These arguments define uniquely the solver
                         use_only_objgrad=tunningexpand([true, false]),
                         max_eval=tunningexpand([-1, 1000, 5000, 10000]))

            end

            # Do something with s

            if s.status == :first_order

                total_time += s.elapsed_time
                n_solved += 1

            else

                total_time += 2 * max_time

            end

            # Finalize model
            finalize(nlp)

        catch e

            finalize(nlp)

            return Inf

        end

    end

    # Return total execution time
    return total_time
    # Return ratio of solved problems
    #return - n_solved / $(length(problems))

end

println("Step 03 ... defining hypertunning functions ... OK!")

# Bounds for μ0, μ1, σ, cgtol
l = [      1.0e-8, 3/4, 1.0 + 1.0e-3, 1.0e-8];
u = [3/4 - 1.0e-8, 1.0,        1.0e3, 9.0e-1];
;

x = zeros(Float64, 4)
copyto!(x, l)

println("Step 04 ... setting hypertunning problem ... OK!")

sol = LOWDER.lowder(flist, x, l, u; m =  (length(x) + 1), verbose = 3)


text:
@testset "matmul" begin
    x = rand(4, 3)
    y = rand(3, 7)
    z = rand(7)

    xi = IArray(x, (mrange(2.0, 5.0), mrange(2.0, 4.0)))
    yi = IArray(y)
    zi = IArray(z)

    for (x_test, y_test) in ((xi, yi),
                             (yi, zi),
                             (x, yi),
                             (xi, y),
                             (y, zi),
                             (yi, z),
                             (z, zi'),
                             (zi, z')
                            )
        @testset "$(summary(x_test)) & $(summary(y_test))" begin
            out = *(x_test, y_test)
            if x_test isa IndicesArray
                if y_test isa IndicesArray
                    @test out == *(parent(x_test), parent(y_test))
                else
                    @test out == *(parent(x_test), y_test)
                end
            else
                @test out == *(x_test, parent(y_test))
            end
            if out isa AbstractArray
                @test isa(out, IndicesArray) == true
            end
        end
    end
end


text:
using HTTP

@testset "HTTP.ascii" begin

    lc = HTTP.Messages.ascii_lc
    lceq = HTTP.Messages.ascii_lc_isequal

    for c in UInt8(1):UInt8(127)
        @test lc(c) == UInt8(lowercase(Char(c)))
    end

    for c in UInt8(1):UInt8(127)
        @test  lceq(c, c)
        @test !lceq(c, UInt8(c+1))
        @test !lceq(c, UInt8(0))
        @test !lceq(c, UInt8(128))
    end

    @test  lceq("Foo", "Foo")
    @test !lceq("Foo", "Fo")
    @test !lceq("Foo", "Foox")
    @test  lceq("Foo", "foo")
    @test  lceq("foo", "Foo")
    @test  lceq("foo", "FOO")
    @test !lceq("",    "FOO")
    @test !lceq("FOO", "")

    @test  lceq("123!", "123!")
    @test !lceq("123",  "123!")

    @test  lceq("", "")

    @test  lceq("NotAscii: 😬", "NotAscii: 😬")
    @test  lceq("notascii: 😬", "NotAscii: 😬")
end


text:
function build(model::Model.ModelData, samples::Model.Samples, streams::Streams)
    # A Soma has an Axon for output.
    axon = DirectAxon()

    # A neuron has a Soma
    soma = Soma(axon, model, samples)

    # The soma can have 1 or more dendrites of various types
    dendrite = Dendrite(soma, model)

    # The dendrite can have many types of compartments
    compartment = Compartment(soma, dendrite, model)

    # Now create synapses of two types.
    # 80% Excite and 20% Inhibit synapses.
    synapses = Model.synapses(model)

    # What percentage of the synapses are excite types
    percentage_of_excititory = Model.percent_excititory_synapses(model) / 100.0
    percentage_of_inhibitory = 1.0 - percentage_of_excititory

    excite = Int64(round((Float64(synapses) * percentage_of_excititory)))
    inhibit = Int64(round((Float64(synapses) * percentage_of_inhibitory)))

    println("Number of excititory synapses: ", excite)
    println("Number of inhibitory synapses: ", inhibit)

    firing_rate = Model.firing_rate(model)

    # Generated ids
    synID = 1

    # Currently there is only one pattern stimulus stream. Eventually there will be more.
    pat_stream = get_stimulus_stream(streams, 1)

    # Associate one of the "channels" of the pattern stream between the pattern and synapse.

    # ------------------------------------------------------
    # Start with excititory type synapses first.
    # Each synapse has a StreamMerger feeding into it.
    # The StreamMerger has two streams feeding into it: Poisson and Pattern.
    for syn in 1:excite
        synapse = Synapse(soma, dendrite, compartment, model)
        # Note: this ID is used to reference the correct synapstic prorperties
        # from the model during the synapse's initialization.
        synapse.id = synID

        # Create StreamMerger to hold Poisson and Pattern streams.
        merger = Simulation.StreamMerger() # A input to a synapse
        Simulation.add_merger_stream!(streams, merger)

        # Router the stream "into" the synapse via attachment.
        set_stream!(synapse, merger)

        # REGION ------------------------------------------------------
        # Find matching poi stream and add to merger
        poi_stream = get_poisson_stream(streams, synID)

        # Add poisson stream to merger stream
        Simulation.add_stream!(merger, poi_stream)
        # END-REGION ------------------------------------------------------

        # REGION ------------------------------------------------------
        # Add pattern stream again. The synapse's id will select a channel
        # from pattern stream
        channel = PatternChannel(pat_stream, synID)
        Simulation.add_stream!(merger, channel)
        # END-REGION ------------------------------------------------------

        synID += 1
    end
    println("Excititory synapses built.")

    # ------------------------------------------------------
    for syn in 1:inhibit
        synapse = Synapse(soma, dendrite, compartment, model)
        synapse.id = synID
        set_as_inhibit!(synapse)

        # Create StreamMerger to hold Poisson and Pattern streams.
        merger = Simulation.StreamMerger()
        Simulation.add_merger_stream!(streams, merger)

        # Attach the stream to the synapse.
        set_stream!(synapse, merger)

        # REGION ------------------------------------------------------
        # Find matching poi stream and add to merger
        poi_stream = get_poisson_stream(streams, synID)

        Simulation.add_stream!(merger, poi_stream)
        # END-REGION ------------------------------------------------------

        # REGION ------------------------------------------------------
        # Add pattern stream again. The synapse's id will select the sub-stream
        channel = PatternChannel(pat_stream, synID)
        Simulation.add_stream!(merger, channel)
        # END-REGION ------------------------------------------------------

        synID += 1
    end
    println("Inhibitory synapses built.")

    # A neuron is a cell.
    cell = Cell(soma, model, samples)

    cell
end


text:
export Chain

"""
    struct Chain{I, J, K, O} <: Conditional{I, J, K, O, Nothing, Q, SFunc{J, O, Nothing}}

A `Conditional` that chains its input `I` through a given function that returns an
`SFunc{J,O}`.
"""
struct Chain{I, J, K, O} <: Conditional{I, J, K, O, SFunc{J, O}}
    fn::Function
    """
    function Chain(I, J, O, fn)

    Chain an input through `fn`.

    The chain is an `SFunc{K,O}`, where `K` is the concatenation of tuples `I` and `J`.
    `fn` is a function that takes an argument of type `I` and returns an `SFunc{J,O}`.
    The `Chain` defines a generative conditional distribution as follows:
    - Given inputs `i` and `j`
    - Let `s::SFunc{J,O} = fn(i)`
    - Use `j` to generate a value from `s`

    For the common case, `Chain` has a special constructors where J is empty.
    """
    function Chain(I, J, O, fn)
        K = extend_tuple_type(I,J)
        new{I, J, K, O}(fn)
    end
    Chain(I, O, fn) = Chain(I, Tuple{}, O, fn)
end

function gensf(ch::Chain{I, J, K, O}, i::I) where {I, J, K, O}
    ch.fn(i)
end


text:
@testset "Apertures" begin
    @testset "Circular apertures" begin
        x = 0
        y = 0

        @test circ(1, x, y) == 1
        @test circ(0, x, y) == 1
        @test circ(0, sqrt(x^2 + y^2)) == 1

        x = 1
        y = 1
        @test circ(1, x, 0) == 1
        @test circ(1, 0, y) == 1
        @test circ(1, sqrt(x^2 + y^2)) == 0
        @test circ(1, x, y) == 0

        x = 10.0
        y = 10.0
        r = 11.0
        @test circ(r, x, 0) ≈ 1
        @test circ(r, 0, y) ≈ 1
        @test circ(r, x, y) ≈ 0
        @test circ(r, sqrt(x^2 + y^2)) ≈ 0

        arr = ones((10, 10))
        @test all(circ(arr, 10, 30) .≈ 1)

        arr = ones((3,3))
        @test all([0 1 0; 1 1 1; 0 1 0] .≈ circ(arr, 2, 1))

        arr = ones((4,4))
        @test all([0 0 1 0; 0 1 1 1; 1 1 1 1; 0 1 1 1] .≈ circ(arr, 2, 1))
        @test all(arr .≈ circ!(arr, 2, 1))
    end

    @testset "Quadratic aperture" begin

        @test quadratic(ones((5, 5)), 5, 2.5, (0.0, 1.25)) == [0.0 0.0 0.0 0.0 0.0; 0.0 0.0 1.0 1.0 1.0; 0.0 0.0 1.0 1.0 1.0; 0.0 0.0 1.0 1.0 1.0; 0.0 0.0 0.0 0.0 0.0]
        @test quadratic(ones((5, 5)), 10, 5) == [0.0 0.0 0.0 0.0 0.0; 0.0 1.0 1.0 1.0 0.0; 0.0 1.0 1.0 1.0 0.0; 0.0 1.0 1.0 1.0 0.0; 0.0 0.0 0.0 0.0 0.0]
        @test quadratic(ones((4, 4)), 10, 5) == [0.0 0.0 0.0 0.0; 0.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0]

        arr = ones(4,4)
        quadratic!(arr, 10, 5)
        @test arr == [0.0 0.0 0.0 0.0; 0.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0]

        @test quadratic(ones((5, 6)), (5, 6), (4.9, 5)) == [0.0 0.0 0.0 0.0 0.0 0.0; 0.0 1.0 1.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0 1.0 1.0; 0.0 0.0 0.0 0.0 0.0 0.0]

    end

end

max_stars_repo_name: JuliaPhysics/PhysicalOptics

max_issues_repo_name: JuliaPhysics/PhysicalOptics

max_forks_repo_name: JuliaPhysics/PhysicalOptics


text:
function part1(input)
    public_keys = parse.(Int, readlines(input))
    secret_keys = find_log7.(public_keys)
    encryption_key1 = pow(public_keys[1], secret_keys[2])
    encryption_key2 = pow(public_keys[2], secret_keys[1])
    @assert encryption_key1 == encryption_key2
    return encryption_key1
end

function part2(input)
end

function find_log7(n)
    i = 0
    m = 1
    while m != n
        m *= 7
        i += 1
        if m > 20201227
            m = mod(m, 20201227)
        end
    end
    return i
end

function pow(b, n)
    m = 1
    for i = 1:n
        m *= b
        if m > 20201227
            m = mod(m, 20201227)
        end
    end
    return m
end


text:
using FileIO

"""
    save_pcof(refFileName, pcof)

Save the parameter vector `pcof` on a JLD2 formatted file with handle `pcof`

# Arguments
- `refFileName`: String holding the name of the file.
- `pcof`: Vector of floats holding the parameters.
"""
function save_pcof(refFileName:: String, pcof:: Vector{Float64})
    save(refFileName, "pcof", pcof)
end

"""
    pcof = read_pcof(refFileName)

Read the parameter vector `pcof` from a JLD2 formatted file

# Arguments
- `refFileName`: String holding the name of the file.
"""
function read_pcof(refFileName:: String)
    dict = load(refFileName)
    pcof = dict["pcof"]
    return pcof
end


text:
"""
    bisection(f,a,b)

adalah fungsi yang mencari nilai akar persamaan dari fungsi `f(x)` pada interval
di antara `a` dan `b`.

# Examples
```jldoctest
julia> f(x) = x*sin(x)-1;

julia> c, flag, M = bisection(f,0,2);

julia> c
1.1141571998596191

julia> flag
0

julia> M
22×5 Array{Float64,2}:
  0.0  0.0      1.0      2.0      -0.158529
  1.0  1.0      1.5      2.0       0.496242
  2.0  1.0      1.25     1.5       0.186231
  3.0  1.0      1.125    1.25      0.015051
  ⋮
 18.0  1.11415  1.11415  1.11416  -3.22926e-6
 19.0  1.11415  1.11416  1.11416  -5.80313e-7
 20.0  1.11416  1.11416  1.11416   7.44159e-7
 21.0  1.11416  1.11416  1.11416   8.19227e-8
```
return solusi `c` dengan `flag` bernilai 0 jika metode bisection berhasil menemukan
solusi dan gagal jika tidak 0. Serta, matriks `M` yang berisi catatan proses tiap
iterasi `[k, a, c, b, f(c)]`

"""
function bisection(f,a,b)
    delta = 10^-7; maxi = 100; flag = 1;
    M = Array{Float64}(undef, 0, 5);
    fa = f(a); fb = f(b);
    if fa*fb>0
        c = "error: f(a) dan f(b) harus berbeda tanda";
        flag = 2;
        return;
    end
    k = 1
    while k<=maxi
        c  = (b+a)/2;
        fc = f(c);
        M = [M; [k-1 a c b fc] ];
        if fc == 0
            a = c;
            b = c;
        elseif fa*fc>0
            a = c;
            fa = fc;
        else
            b = c;
            fb = fc;
        end
        if b-a < delta || abs(fc) < delta
            flag = 0;
            break;
        end
        k+=1
    end
    return c,flag, M
end


text:
module BenchQueuePushPop

using BenchmarkTools
using ConcurrentCollections

function pushpop!(
    q;
    nrepeat::Integer = 2^15,
    nitems::Integer = 11,
    ntasks::Integer = Threads.nthreads(),
    nspins::Integer = 100,
)
    ntasks < 1 && error("require positive `ntasks`: got $ntasks")
    local tasks
    @sync begin
        tasks = map(1:ntasks) do _
            Threads.@spawn begin
                nyields = 0
                for _ in 1:nrepeat
                    for i in 1:nitems
                        push!(q, i)
                    end
                    for _ in 1:nitems
                        while true
                            y = nothing
                            for _ in 1:nspins
                                y = maybepopfirst!(q)
                                y === nothing || break
                            end
                            y === nothing || break
                            yield()
                            nyields += 1
                            if nyields > 2^20
                                @error "Too many yields!"
                                error("Too many yields!")
                            end
                        end
                    end
                end
                return nyields
            end
        end
    end
    return (; nrepeat, nitems, nyields = map(fetch, tasks))
end

function setup(; kwargs...)
    suite = BenchmarkGroup()
    suite["channel"] = @benchmarkable pushpop!(Channel{Int}(Inf); $kwargs...)
    suite["msqueue"] = @benchmarkable pushpop!(ConcurrentQueue{Int}(); $kwargs...)
    suite["lcrq"] = @benchmarkable pushpop!(LinkedConcurrentRingQueue{Int}(); $kwargs...)
    return suite
end

function clear() end

end  # module


text:
# push!(LOAD_PATH,"../src/")
include("../src/Select.jl")
using .Selects
using DataFrames,CSV
# df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"], C = 1:4, D= 1:4)

path = "cities.csv"
df = CSV.read(path,DataFrame)
name= String.(names(df))
columnas = ["4","2","5","9"]
Selects.Select(df,columnas,name)

max_stars_repo_name: MarleenMs/Covid

max_issues_repo_name: MarleenMs/Covid

max_forks_repo_name: MarleenMs/Covid


text:
println("What's your favorite letter?")
string = readline()
println("$string is my favorite letter")

max_stars_repo_name: Coalpha/lang-experimentation

max_issues_repo_name: Coalpha/lang-experimentation

max_forks_repo_name: Coalpha/lang-experimentation