text:
"""
Configurations for top-level analysis.
These configurations will be active for all the top-level entries explained in [Analysis entry points](@ref).

---
- `analyze_from_definitions::Bool = false` \\
  If `true`, JET will start analysis using signatures of top-level definitions (e.g. method signatures),
    after the top-level interpretation has been done (unless no serious top-level error has
    happened, like errors involved within a macro expansion).

  This is useful when you want to analyze a package, which usually contains only definitions
    but not top-level callsites.
  With this option, JET can enter analysis just with method or type definitions, and we don't
    need to pass a file that uses the target package.

  !!! warning
      This feature is very experimental at this point, and you may face lots of false positive
        errors, especially when trying to analyze a big package with lots of dependencies.
      If a file that contains top-level callsites (e.g. `test/runtests.jl`) is available,
        JET analysis entered from there will produce more accurate analysis results than
          using with this configuration.

  Also see: [`report_file`](@ref), [`report_and_watch_file`](@ref)
---
- `concretization_patterns::Vector{<:Any} = Expr[]` \\
  Specifies a customized top-level code concretization strategy.

  When analyzing a top-level code, JET first splits the entire code and then iterate a virtual
    top-level code execution process on each code block, in order to simulate Julia's sequential
    top-level code execution.
  However, with this approach, JET can't track the "inter-code-block" level dependencies, and
    so a partial interpretation of top-level definitions can fail if it needs an access to
    global variables defined in other code blocks that are not actually interpreted ("concretized")
    but just abstract-interpreted ("abstracted").

  For example, the issue happens when your macro accesses to a global variable during its expansion, e.g.:
  > test/fixtures/concretization_patterns.jl
  $(let
      text = read(normpath(@__DIR__, "..", "test", "fixtures", "concretization_patterns.jl"), String)
      lines = split(text, '\n')
      pushfirst!(lines, "```julia"); push!(lines, "```")
      join(lines, "\n  ")
  end)

  To circumvent this issue, JET offers the `concretization_patterns::Vector{<:Any}` configuration,
    which allows us to customize JET's top-level code concretization strategy.
  `concretization_patterns` specifies the _patterns of code_ that should be concretized.
  JET internally uses [MacroTools.jl's expression pattern match](https://fluxml.ai/MacroTools.jl/stable/pattern-matching/),
    and we can specify any expression pattern that is expected by `MacroTools.@capture` macro.
  For example, in order to solve the issue explained above, we can have:
  ```julia
  concretization_patterns = [:(GLOBAL_CODE_STORE = x_)]
  ```
  Please note that we must use `:(GLOBAL_CODE_STORE = x_)` rather than `:(const GLOBAL_CODE_STORE = x_)`.
  This is because currently the specified patterns will be matched against [the lowered code representation](https://juliadebug.github.io/JuliaInterpreter.jl/stable/ast/),
    in which `const x = y` has been lowered to the sequence of 1.) the declaration `const x`,
    2.) value computation `%2 = Dict()` and 3.) actual assignment part `x = %2`.
  Although this could be really tricky, we can effectively debug JET's top-level code concretization plan
    using [`JETLogger`](@ref)'s `toplevel_logger` with the logging level above than `$DEBUG_LOGGER_LEVEL` ("debug") level,
    where `t`-annotated statements will be concretize while `f`-annotated statements will be analyzed by abstract interpretation.
  ```julia
  julia> report_file("test/fixtures/concretization_patterns.jl";
                     concretization_patterns = [:(GLOBAL_CODE_STORE = x_)],
                     toplevel_logger = IOContext(stdout, :JET_LOGGER_LEVEL => 1))
  ```
  ```
  [toplevel-debug] entered into test/fixtures/concretization_patterns.jl
  [toplevel-debug] concretization plan:
  1 f 1 ─      const GLOBAL_CODE_STORE
  2 t │   %2 = Dict()
  3 t │        GLOBAL_CODE_STORE = %2
  4 f └──      return %2
  [toplevel-debug] concretization plan:
  1 f 1 ─      \$(Expr(:thunk, CodeInfo(
      @ none within `top-level scope'
  1 ─     return \$(Expr(:method, Symbol("@with_code_record")))
  )))
  2 t │        \$(Expr(:method, Symbol("@with_code_record")))
  3 t │   %3 = Core.Typeof(var"@with_code_record")
  4 t │   %4 = Core.svec(%3, Core.LineNumberNode, Core.Module, Core.Any)
  5 t │   %5 = Core.svec()
  6 t │   %6 = Core.svec(%4, %5, \$(QuoteNode(:(#= test/fixtures/concretization_patterns.jl:4 =#))))
  7 t │        \$(Expr(:method, Symbol("@with_code_record"), :(%6), CodeInfo(
      @ test/fixtures/concretization_patterns.jl:5 within `none'
  1 ─      \$(Expr(:meta, :nospecialize, :(a)))
  │        Base.setindex!(GLOBAL_CODE_STORE, a, __source__)
  │   @ test/fixtures/concretization_patterns.jl:6 within `none'
  │   %3 = esc(a)
  └──      return %3
  )))
  8 f └──      return var"@with_code_record"
  [toplevel-debug] concretization plan:
  1 f 1 ─      \$(Expr(:thunk, CodeInfo(
      @ none within `top-level scope'
  1 ─     return \$(Expr(:method, :foo))
  )))
  2 t │        \$(Expr(:method, :foo))
  3 t │   %3 = Core.Typeof(foo)
  4 t │   %4 = Core.svec(%3, Core.Any)
  5 t │   %5 = Core.svec()
  6 t │   %6 = Core.svec(%4, %5, \$(QuoteNode(:(#= test/fixtures/concretization_patterns.jl:11 =#))))
  7 t │        \$(Expr(:method, :foo, :(%6), CodeInfo(
      @ test/fixtures/concretization_patterns.jl:11 within `none'
  1 ─ %1 = identity(a)
  └──      return %1
  )))
  8 f └──      return foo
  [toplevel-debug] concretization plan:
  1 f 1 ─ %1 = foo(10)
  2 f └──      return %1
  [toplevel-debug]  exited from test/fixtures/concretization_patterns.jl (took 0.018 sec)
  ```

  Also see: [Logging Configurations](@ref), [`virtual_process`](@ref).
---
"""
struct ToplevelConfig
    analyze_from_definitions::Bool
    concretization_patterns::Vector{<:Any}
    @jetconfigurable ToplevelConfig(; analyze_from_definitions::Bool         = false,
                                      concretization_patterns::Vector{<:Any} = Expr[],
                                      ) =
        return new(analyze_from_definitions,
                   concretization_patterns,
                   )
end

const Actual2Virtual = Pair{Module,Module}

"""
    res::VirtualProcessResult

- `res.included_files::Set{String}`: files that have been analyzed
- `res.toplevel_error_reports::Vector{ToplevelErrorReport}`: toplevel errors found during the
    text parsing or partial (actual) interpretation; these reports are "critical" and should
    have precedence over `inference_error_reports`
- `res.inference_error_reports::Vector{InferenceErrorReport}`: possible error reports found
    by `JETInterpreter`
- `res.toplevel_signatures`: signatures of methods defined within the analyzed files
- `res.actual2virtual::$Actual2Virtual`: keeps actual and virtual module
"""
const VirtualProcessResult = @NamedTuple begin
    included_files::Set{String}
    toplevel_error_reports::Vector{ToplevelErrorReport}
    inference_error_reports::Vector{InferenceErrorReport}
    toplevel_signatures::Vector{Type}
    actual2virtual::Actual2Virtual
end

function gen_virtual_process_result(actualmod, virtualmod)
    return (; included_files          = Set{String}(),
              toplevel_error_reports  = ToplevelErrorReport[],
              inference_error_reports = InferenceErrorReport[],
              toplevel_signatures     = Type[],
              actual2virtual          = Actual2Virtual(actualmod, virtualmod),
              )::VirtualProcessResult
end

gen_virtual_module(actualmod = Main) =
    Core.eval(actualmod, :(module $(gensym(:JETVirtualModule)) end))::Module

"""
    virtual_process(s::AbstractString,
                    filename::AbstractString,
                    actualmod::Module,
                    interp::JETInterpreter,
                    config::ToplevelConfig,
                    virtualmod::Module,
                    ) -> res::VirtualProcessResult

Simulates Julia's toplevel execution and collects error points, and finally returns $(@doc VirtualProcessResult)

This function first parses `s::AbstractString` into `toplevelex::Expr` and then iterate the
  following steps on each code block (`blk`) of `toplevelex`:
1. if `blk` is a `:module` expression, recusively enters analysis into an newly defined
     virtual module
2. `lower`s `blk` into `:thunk` expression `lwr` (macros are also expanded in this step)
3. replaces self-references of the original root module (i.e. `actualmod`)
     with that of `virtualmod`: see `fix_self_references`
3. `ConcreteInterpreter` partially interprets some statements in `lwr` that should not be
     abstracted away (e.g. a `:method` definition); see also [`partially_interpret!`](@ref)
4. finally, `JETInterpreter` analyzes the remaining statements by abstract interpretation

!!! warning
    In order to process the toplevel code sequentially as Julia runtime does, `virtual_process`
      splits the entire code, and then iterate a simulation process on each code block.
    With this approach, we can't track the inter-code-block level dependencies, and so a
      partial interpretation of toplevle definitions will fail if it needs an access to global
      variables defined in other code blocks that are not interpreted but just abstracted.
    We can circumvent this issue using JET's `concretization_patterns` configuration, which
      allows us to customize JET's concretization strategy.
    See [`ToplevelConfig`](@ref) for more details.
"""
function virtual_process(x::Union{AbstractString,Expr},
                         filename::AbstractString,
                         actualmod::Module,
                         interp::JETInterpreter,
                         config::ToplevelConfig,
                         virtualmod::Module,
                         )
    res = _virtual_process!(x,
                            filename,
                            actualmod,
                            interp,
                            config,
                            virtualmod,
                            gen_virtual_process_result(actualmod, virtualmod),
                            )

    # analyze collected signatures unless critical error happened
    if config.analyze_from_definitions && isempty(res.toplevel_error_reports)
        analyze_from_definitions!(interp, res)
    end

    return res
end

function analyze_from_definitions!(interp::JETInterpreter, res::VirtualProcessResult)
    n = length(res.toplevel_signatures)
    succeeded = 0
    clearline!(io) = print(io, '\r')
    for (i, tt) in enumerate(res.toplevel_signatures)
        mms = _methods_by_ftype(tt, -1, get_world_counter())
        isa(mms, Bool) && @goto failed

        filter!(mm::MethodMatch->mm.spec_types===tt, mms)
        if length(mms) == 1
            succeeded += 1
            with_toplevel_logger(interp; pre=clearline!) do io
                (i == n ? println : print)(io, "analyzing from top-level definitions ... $succeeded/$n")
            end
            interp = JETInterpreter(interp, _CONCRETIZED, _TOPLEVELMOD)
            mm = first(mms)
            analyze_method_signature!(interp, mm.method, mm.spec_types, mm.sparams)
            append!(res.inference_error_reports, interp.reports)
            continue
        end

        @label failed
        with_toplevel_logger(interp, ≥(DEBUG_LOGGER_LEVEL); pre=clearline!) do io
            println(io, "couldn't find a single method matching the signature `$tt`")
        end
    end
end

function _virtual_process!(s::AbstractString,
                           filename::AbstractString,
                           actualmod::Module,
                           interp::JETInterpreter,
                           config::ToplevelConfig,
                           virtualmod::Module,
                           res::VirtualProcessResult,
                           )
    start = time()

    with_toplevel_logger(interp) do io
        println(io, "entered into $filename")
    end

    push!(res.included_files, filename)

    toplevelex = parse_input_line(s; filename)

    if @isexpr(toplevelex, (:error, :incomplete))
        # if there's any syntax error, try to identify all the syntax error location
        append!(res.toplevel_error_reports, collect_syntax_errors(s, filename))
    elseif isnothing(toplevelex)
        # just return if there is nothing to analyze
    else
        res = _virtual_process!(toplevelex, filename, actualmod, interp, config, virtualmod, res)
    end

    with_toplevel_logger(interp) do io
        sec = round(time() - start; digits = 3)
        println(io, " exited from $filename (took $sec sec)")
    end

    return res
end

function _virtual_process!(toplevelex::Expr,
                           filename::AbstractString,
                           actualmod::Module,
                           interp::JETInterpreter,
                           config::ToplevelConfig,
                           virtualmod::Module,
                           res::VirtualProcessResult,
                           )
    @assert @isexpr(toplevelex, :toplevel)

    local lnn::LineNumberNode = LineNumberNode(0, filename)

    function macroexpand_err_handler(err, st)
        report = is_missing_concretization(err) ?
                 MissingConcretization(err, st, filename, lnn.line) :
                 ActualErrorWrapped(err, st, filename, lnn.line)
        push!(res.toplevel_error_reports, report)
        return nothing
    end
    # `scrub_offset = 4` corresponds to `with_err_handling` -> `f` -> `macroexpand` -> kwfunc (`macroexpand`)
    macroexpand_with_err_handling(mod, x) = with_err_handling(macroexpand_err_handler, #= scrub_offset =# 4) do
        # XXX we want to non-recursive, sequential partial macro expansion here, which allows
        # us to collect more fine-grained error reports within macro expansions
        # but it can lead to invalid macro hygiene escaping because of https://github.com/JuliaLang/julia/issues/20241
        return macroexpand(mod, x; recursive = true #= but want to use `false` here =#)
    end
    function eval_err_handler(err, st)
        report = is_missing_concretization(err) ?
                 MissingConcretization(err, st, filename, lnn.line) :
                 ActualErrorWrapped(err, st, filename, lnn.line)
        push!(res.toplevel_error_reports, report)
        return nothing
    end
    # `scrub_offset = 3` corresponds to `with_err_handling` -> `f` -> `eval`
    eval_with_err_handling(mod, x) = with_err_handling(eval_err_handler, #= scrub_offset =# 3) do
        return Core.eval(mod, x)
    end
    function lower_err_handler(err, st)
        report = is_missing_concretization(err) ?
                 MissingConcretization(err, st, filename, lnn.line) :
                 ActualErrorWrapped(err, st, filename, lnn.line)
        push!(res.toplevel_error_reports, report)
        return nothing
    end
    # `scrub_offset = 3` corresponds to `with_err_handling` -> `f` -> `lower`
    lower_with_err_handling(mod, x) = with_err_handling(lower_err_handler, #= scrub_offset =# 3) do
        lwr = lower(mod, x)

        # here we should capture syntax errors found during lowering
        if @isexpr(lwr, :error)
            msg = first(lwr.args)
            push!(res.toplevel_error_reports, SyntaxErrorReport("syntax: \$(msg)", filename, lnn.line))
            return nothing
        end

        return lwr
    end

    # transform, and then analyze sequentially
    # IDEA the following code has some of duplicated work with `JuliaInterpreter.ExprSpliter` and we may want to factor them out
    exs = reverse(toplevelex.args)
    while !isempty(exs)
        x = pop!(exs)

        # with_toplevel_logger(interp, ≥(DEBUG_LOGGER_LEVEL)) do io
        #     println(io, "analyzing ", x)
        # end

        # update line info
        if isa(x, LineNumberNode)
            lnn = x
            continue
        end

        # we will end up lowering `x` later, but special case `macrocall`s and expand it here
        # this is because macros can arbitrarily generate `:toplevel` and `:module` expressions
        if @isexpr(x, :macrocall)
            newx = macroexpand_with_err_handling(virtualmod, x)
            # unless (toplevel) error happened during macro expansion, queue it and continue
            isnothing(newx) || push!(exs, newx)
            continue
        end

        # flatten container expression
        if @isexpr(x, :toplevel)
            append!(exs, reverse(x.args))
            continue
        end

        # handle `:module` definition and module usage;
        # should happen here because modules need to be loaded sequentially while
        # "toplevel definitions" inside of the loaded modules shouldn't be evaluated in a
        # context of `virtualmod`

        if @isexpr(x, :module)
            newblk = x.args[3]
            @assert @isexpr(newblk, :block)
            newtoplevelex = Expr(:toplevel, newblk.args...)

            x.args[3] = Expr(:block) # empty module's code body
            newvirtualmod = eval_with_err_handling(virtualmod, x)

            isnothing(newvirtualmod) && continue # error happened, e.g. duplicated naming

            _virtual_process!(newtoplevelex, filename, actualmod, interp, config, newvirtualmod::Module, res)

            continue
        end

        blk = Expr(:block, lnn, x) # attach line number info
        lwr = lower_with_err_handling(virtualmod, blk)

        isnothing(lwr) && continue # error happened during lowering
        @isexpr(lwr, :thunk) || continue # literal

        src = first((lwr::Expr).args)::CodeInfo

        fix_self_references!(src, actualmod, virtualmod)

        interp′ = ConcreteInterpreter(filename,
                                      lnn,
                                      eval_with_err_handling,
                                      virtualmod,
                                      actualmod,
                                      interp,
                                      config,
                                      res,
                                      )
        concretized = partially_interpret!(interp′, virtualmod, src)

        # bail out if nothing to analyze (just a performance optimization)
        all(concretized) && continue

        interp = JETInterpreter(interp, concretized, virtualmod)

        analyze_toplevel!(interp, src)

        append!(res.inference_error_reports, interp.reports) # collect error reports
    end

    return res
end

# replace self references of `actualmodsym` with `virtualmod` (as is)
function fix_self_references!(ex, actualmod, virtualmod)
    actualmodsym = Symbol(actualmod)
    function _fix_self_reference(x, scope)
        if isa(x, Symbol)
            if x === actualmodsym
                return virtualmod
            end
        elseif isa(x, GotoIfNot)
            newcond = _fix_self_reference(x.cond, scope)
            return GotoIfNot(newcond, x.dest)
        end

        return x
    end

    prewalk_and_transform!(_fix_self_reference, ex)
end

prewalk_and_transform!(args...) = walk_and_transform!(true, args...)
postwalk_and_transform!(args...) = walk_and_transform!(false, args...)
function walk_and_transform!(pre, f, @nospecialize(x), scope = Symbol[])
    x = pre ? f(x, scope) : x
    _walk_and_transform!(pre, f, x, scope)
    return pre ? x : f(x, scope)
end
function _walk_and_transform!(pre, f, ex::Expr, scope)
    push!(scope, ex.head)
    for (i, x) in enumerate(ex.args)
        ex.args[i] = walk_and_transform!(pre, f, x, scope)
    end
    pop!(scope)
end
function _walk_and_transform!(pre, f, src::CodeInfo, scope)
    for (i, x) in enumerate(src.code)
        src.code[i] = walk_and_transform!(pre, f, x, scope)
    end
end
_walk_and_transform!(pre, f, @nospecialize(_), scope) = return

# # configure user-specified concretization strategy with pattern matching on surface level AST
# # `select_statements` will use and actually apply configuration using :force_concretize_(start|end) annotations
# function annotate_force_concretizations(@nospecialize(x), config::ToplevelConfig)
#     patterns = config.concretization_patterns
#     return MacroTools.postwalk(x) do @nospecialize(x)
#         for pat in patterns
#             if @capture(x, $pat)
#                 return Expr(:block,
#                             Expr(:meta, :force_concretize_start),
#                             Expr(:local, Expr(:(=), :ret, x)),
#                             Expr(:meta, :force_concretize_end),
#                             :ret)
#             end
#         end
#         return x
#     end
# end

"""
    ConcreteInterpreter

The trait to inject code into JuliaInterpreter's interpretation process; JET.jl overloads:
- `JuliaInterpreter.step_expr!` to add error report pass for module usage expressions and
    support package analysis
- `JuliaInterpreter.evaluate_call_recurse!` to special case `include` calls
- `JuliaInterpreter.handle_err` to wrap an error happened during interpretation into
    `ActualErrorWrapped`
"""
struct ConcreteInterpreter
    filename::String
    lnn::LineNumberNode
    eval_with_err_handling::Function
    virtualmod::Module
    actualmod::Module
    interp::JETInterpreter
    config::ToplevelConfig
    res::VirtualProcessResult
end

"""
    partially_interpret!(interp::ConcreteInterpreter, mod::Module, src::CodeInfo)

Partially interprets statements in `src` using JuliaInterpreter.jl:
- concretizes "toplevel definitions", i.e. `:method`, `:struct_type`, `:abstract_type` and
    `:primitive_type` expressions and their dependencies
- concretizes user-specified toplevel code (see [`ToplevelConfig`](@ref))
- directly evaluates module usage expressions and report error of invalid module usages
  (TODO: enter into the loaded module and keep JET analysis)
- special-cases `include` calls so that top-level analysis recursively enters the included file
"""
function partially_interpret!(interp::ConcreteInterpreter, mod::Module, src::CodeInfo)
    concretize = select_statements(src, interp.config)

    with_toplevel_logger(interp.interp, ≥(DEBUG_LOGGER_LEVEL)) do io
        println(io, "concretization plan:")
        LoweredCodeUtils.print_with_code(io, src, concretize)
    end

    selective_eval_fromstart!(interp, Frame(mod, src), concretize, #= istoplevel =# true)

    return concretize
end

# select statements that should be not abstracted away, but rather actually interpreted
function select_statements(src, config)
    stmts = src.code
    edges = CodeEdges(src)

    concretize = fill(false, length(stmts))

    for (i, stmt) in enumerate(stmts)
        if begin
                ismethod(stmt)      || # don't abstract away method definitions
                istypedef(stmt)     || # don't abstract away type definitions
                ismoduleusage(stmt)    # module usages are handled by `ConcreteInterpreter`
            end
            concretize[i] = true
            continue
        end

        # apply user-specified concretization strategy
        # currently our concretization strategy is configured here with expression pattern
        # matching on lowered representation; while it can be a bit user-unfriendly, but it
        # has several benefits over the pattern matching with surface level AST,
        # especially, here in lowered representation a function definition signature
        # (`f(args...)`) is clearly distinguished from the call expression while within
        # surface AST level we should care about the scope of the expression, etc.
        local force_concretize = false
        for pat in config.concretization_patterns
            if @capture(stmt, $pat)
                force_concretize = concretize[i] = true
                break
            end
        end
        force_concretize && continue

        if @isexpr(stmt, :(=))
            lhs, rhs = stmt.args
            stmt = rhs
        end
        if @isexpr(stmt, :call)
            f = stmt.args[1]

            # special case `include` calls
            if f === :include
                concretize[i] = true
                continue
            end

            # analysis of `eval` calls are difficult, let's give up it and just evaluate
            # toplevel `eval` calls; they may contain toplevel definitions
            # adapted from
            # - https://github.com/timholy/Revise.jl/blob/266ed68d7dd3bea67c39f96513cda30bbcd7d441/src/lowered.jl#L53
            # - https://github.com/timholy/Revise.jl/blob/266ed68d7dd3bea67c39f96513cda30bbcd7d441/src/lowered.jl#L87-L88
            if begin
                    f === :eval ||
                    (callee_matches(f, Base, :getproperty) && is_quotenode_egal(stmt.args[end], :eval)) ||
                    isa(f, GlobalRef) && f.name === :eval
                end
                # statement `i` may be the equivalent of `f = Core.eval`, so require each
                # stmt that calls `eval` via `f(expr)`
                concretize[edges.succs[i]] .= true
                concretize[i] = true
            end
        end
    end

    lines_required!(concretize, src, edges)

    return concretize
end

function JuliaInterpreter.step_expr!(interp::ConcreteInterpreter, frame::Frame, @nospecialize(node), istoplevel::Bool)
    # TODO:
    # - support package analysis
    # - add report pass (report usage of undefined name, etc.)
    if ismoduleusage(node)
        for ex in to_single_usages(node::Expr)
            # NOTE: usages of abstract global variables also work here, since they are supposed
            # to be actually evaluated into `interp.virtualmod` (as `AbstractGlobal`
            # object) at this point

            interp.eval_with_err_handling(interp.virtualmod, ex)
        end
        return nothing
    end

    res = @invoke step_expr!(interp, frame, node, istoplevel::Bool)

    interp.config.analyze_from_definitions && collect_toplevel_signature!(interp, frame, node)

    return res
end

function collect_toplevel_signature!(interp::ConcreteInterpreter, frame::Frame, @nospecialize(node))
    if @isexpr(node, :method, 3)
        sigs = node.args[2]
        atype_params, sparams, _ = @lookup(frame, sigs)::SimpleVector
        # t = atype_params[1]
        # if isdefined(t, :name)
        #     # XXX ignore constructor methods, just because it can lead to false positives ...
        #     t.name === CC._TYPE_NAME && return
        # end
        atype = Tuple{(atype_params::SimpleVector)...}
        push!(interp.res.toplevel_signatures, atype)
    end
end

ismoduleusage(@nospecialize(x)) = @isexpr(x, (:import, :using, :export))

# assuming `ismoduleusage(x)` holds
function to_single_usages(x::Expr)
    if length(x.args) != 1
        # using A, B, export a, b
        return Expr.(x.head, x.args)
    else
        arg = first(x.args)
        if isa(arg, Symbol)
            # export a
            return [x]
        else
            if arg.head === :.
                # using A
                return [x]
            else
                # using A: sym1, sym2, ...
                @assert @isexpr(arg, :(:))
                a, as... = arg.args
                return Expr.(x.head, Expr.(arg.head, Ref(a), as))::Vector{Expr}
            end
        end
    end
end

# adapted from https://github.com/JuliaDebug/JuliaInterpreter.jl/blob/2f5f80034bc287a60fe77c4e3b5a49a087e38f8b/src/interpret.jl#L188-L199
# works almost same as `JuliaInterpreter.evaluate_call_compiled!`, but also added special
# cases for JET analysis
function JuliaInterpreter.evaluate_call_recurse!(interp::ConcreteInterpreter, frame::Frame, call_expr::Expr; enter_generated::Bool=false)
    # @assert !enter_generated
    pc = frame.pc
    ret = bypass_builtins(frame, call_expr, pc)
    isa(ret, Some{Any}) && return ret.value
    ret = @invokelatest maybe_evaluate_builtin(frame, call_expr, false)
    isa(ret, Some{Any}) && return ret.value
    fargs = collect_args(frame, call_expr)
    f = fargs[1]
    popfirst!(fargs)  # now it's really just `args`

    if isinclude(f)
        return handle_include(interp, fargs)
    else
        # `_virtual_process!` iteratively interpret toplevel expressions but it doesn't hit toplevel
        # we may want to make `_virtual_process!` hit the toplevel on each interation rather than
        # using `invokelatest` here, but assuming concretized calls are supposed only to be
        # used for other toplevel definitions and as such not so computational heavy,
        # I'd like to go with this simplest way
        return @invokelatest f(fargs...)
    end
end

isinclude(@nospecialize(_))           = false
isinclude(@nospecialize(f::Function)) = nameof(f) === :include

function handle_include(interp, fargs)
    filename = interp.filename
    res = interp.res
    lnn = interp.lnn
    eval_with_err_handling = interp.eval_with_err_handling
    virtualmod = interp.virtualmod

    # TODO: maybe find a way to handle two args `include` calls
    include_file = normpath(dirname(filename), first(fargs))

    # handle recursive `include`s
    if include_file in res.included_files
        report = RecursiveIncludeErrorReport(include_file, res.included_files, filename, lnn.line)
        push!(res.toplevel_error_reports, report)
        return nothing
    end

    read_ex      = :(read($(include_file), String))
    include_text = eval_with_err_handling(virtualmod, read_ex)

    isnothing(include_text) && return nothing # typically no file error

    _virtual_process!(include_text::String, include_file, interp.actualmod, interp.interp, interp.config, interp.virtualmod, interp.res)

    # TODO: actually, here we need to try to get the lastly analyzed result of the `_virtual_process!` call above
    return nothing
end

const JET_VIRTUALPROCESS_FILE = Symbol(@__FILE__)
const JULIAINTERPRETER_BUILTINS_FILE = let
    jlfile = pathof(JuliaInterpreter)::String
    Symbol(normpath(jlfile, "..", "builtins.jl"))
end

# handle errors from toplevel user code
function JuliaInterpreter.handle_err(interp::ConcreteInterpreter, frame, err)
    # catch stack trace
    bt = catch_backtrace()
    st = stacktrace(bt)

    # scrub the original stacktrace so that it only contains frames from user code
    i = 0
    for (j, frame) in enumerate(st)
        # if errors happen in `JuliaInterpreter.maybe_evaluate_builtin`, we just discard all
        # the stacktrace assuming they are enough self-explanatory (corresponding to the last logic below)
        if frame.file === JULIAINTERPRETER_BUILTINS_FILE && frame.func === :maybe_evaluate_builtin
            break # keep `i = 0`
        end

        # find an error frame that happened at `@invokelatest f(fargs...)` in the overload
        # `JuliaInterpreter.evaluate_call_recurse!(interp::ConcreteInterpreter, frame::Frame, call_expr::Expr; enter_generated::Bool=false)`
        if frame.file === JET_VIRTUALPROCESS_FILE && frame.func === :evaluate_call_recurse!
            i = j - 4 # offset: `evaluate_call_recurse` -> kwfunc (`evaluate_call_recurse`) -> `invokelatest` -> kwfunc (`invokelatest`)
            break
        end

        # other general errors may happen at `collect_args`, etc.
        # we don't show any stacktrace for those errors (by keeping the original `i = 0`)
        # since they are hopefully enough self-explanatory (XXX is it really so ?) and even
        # Julia's base error handler only shows something like `[1] top-level scope` in these cases
        continue
    end
    st = st[1:i]

    report = is_missing_concretization(err) ?
             MissingConcretization(err, st, interp.filename, interp.lnn.line) :
             ActualErrorWrapped(err, st, interp.filename, interp.lnn.line)
    push!(interp.res.toplevel_error_reports, report)

    return nothing
end

function with_err_handling(f, err_handler, scrub_offset)
    return try
        f()
    catch err
        bt = catch_backtrace()
        st = stacktrace(bt)

        # scrub the original stacktrace so that it only contains frames from user code
        i = findfirst(st) do frame
            frame.file === JET_VIRTUALPROCESS_FILE && frame.func === :with_err_handling
        end
        @assert i !== nothing
        st = st[1:(i - scrub_offset)]

        err_handler(err, st)
    end
end

function is_missing_concretization(@nospecialize(err))
    io = IOBuffer()
    showerror(io, err)
    occursin(string(AbstractGlobal), String(take!(io)))
end

function collect_syntax_errors(s, filename)
    reports = SyntaxErrorReport[]
    index = line = 1
    while begin
            ex, nextindex = _parse_string(s, filename, index, :statement)
            !isnothing(ex)
        end
        line += count(==('\n'), s[index:nextindex-1])
        report = @isexpr(ex, :error) ? SyntaxErrorReport(string("syntax: ", first(ex.args)), filename, line) :
                 @isexpr(ex, :incomplete) ? SyntaxErrorReport(first(ex.args), filename, line) :
                 nothing
        isnothing(report) || push!(reports, report)
        index = nextindex
    end
    return reports
end


text:
using CausalInference, Distributions, Random

@testset "Entropy" begin
    Random.seed!(123)

    @test isapprox(n_ball(2), π)
    @test isapprox(n_ball(3), 4/3. * π)
    N = 250000

    d = Normal(0,1)
    samples = rand(d, N)
    est = kl_entropy(collect(transpose(samples)))
    ent = entropy(d)
    @test (est-ent)/ent < 0.01

    d = Gamma(2,3)
    samples = rand(d, N)
    est = kl_entropy(collect(transpose(samples)))
    ent = entropy(d)
    @test (est-ent)/ent < 0.01

    d = Exponential(2.5)
    samples = rand(d, N)
    est = kl_entropy(collect(transpose(samples)))
    ent = entropy(d)
    @test (est-ent)/ent < 0.01

    r = 0.75
    d = MvNormal([1 r; r 1])
    samples = rand(d, N)
    X = collect(transpose(samples[1,:]))
    Y = collect(transpose(samples[2,:]))
    est = kl_mutual_information(X,Y)
    MI = -1/2. * log(1-r^2)
    @test (est-MI)/MI < 0.01
end

@testset "Permutation tests" begin
    Random.seed!(123)
    N = 10000
    p = 0.05
    x = randn(N)
    v = x + randn(N)*0.25
    w = x + randn(N)*0.25
    z = v + w +randn(N)*0.25

    @test kl_perm_mi_test(collect(transpose(v)), collect(transpose(w)))<p
    @test kl_perm_cond_mi_test(collect(transpose(v)),
                               collect(transpose(w)),
                               collect(transpose(z)))<p
    @test kl_perm_cond_mi_test(collect(transpose(v)),
                               collect(transpose(w)),
                               collect(transpose(x)))>p

end

ext: jl


text:
@testset "Simulate" begin
    @test p2 ≈ Psums[2]
    @test Psums[1] == sum(P1)
end


text:
# Conversion for STaylor1
function convert(::Type{STaylor1{N,Rational{T}}}, a::STaylor1{N,S}) where {N,T<:Integer, S<:AbstractFloat}
    STaylor1{N,T}(rationalize.(a[:]))
end
function convert(::Type{STaylor1{N,T}}, b::Array{T,1}) where {N,T<:Number}
    @assert N == length(b)
    STaylor1{N,T}(b)
end
function convert(::Type{STaylor1{N,T}}, b::Array{S,1}) where {N,T<:Number, S<:Number}
    @assert N == length(b)
    STaylor1{N,T}(convert(Array{T,1},b))
end
convert(::Type{STaylor1{N,T}}, a::STaylor1{N,T}) where {N,T<:Number} = a
convert(::Type{STaylor1{N,T}}, b::S)  where {N, T<:Number, S<:Number} = STaylor1(convert(T,b), Val(N))
convert(::Type{STaylor1{N,T}}, b::T)  where {N, T<:Number} = STaylor1(b, Val(N))

promote_rule(::Type{STaylor1{N,T}}, ::Type{STaylor1{N,T}}) where {N, T<:Number} = STaylor1{N,T}
promote_rule(::Type{STaylor1{N,T}}, ::Type{STaylor1{N,S}}) where {N, T<:Number, S<:Number} = STaylor1{N, promote_type(T,S)}
promote_rule(::Type{STaylor1{N,T}}, ::Type{Array{T,1}}) where {N, T<:Number} = STaylor1{N,T}
promote_rule(::Type{STaylor1{N,T}}, ::Type{Array{S,1}}) where {N, T<:Number, S<:Number} = STaylor1{N,promote_type(T,S)}
promote_rule(::Type{STaylor1{N,T}}, ::Type{T}) where {N, T<:Number} = STaylor1{N,T}
promote_rule(::Type{STaylor1{N,T}}, ::Type{S}) where {N, T<:Number, S<:Number} = STaylor1{N,promote_type(T,S)}


text:
using Test
using TreeTools

println("##### prunegraft #####")
global root_1 = TreeTools.read_newick("$(dirname(pathof(TreeTools)))/../test/prunegraft/tree1.nwk")
@testset "Pruning" begin
	# Pruning with modification
	root = deepcopy(root_1)
	global A = prunenode!(root.child[1].child[1])[1]
	root_ref = TreeTools.read_newick("$(dirname(pathof(TreeTools)))/../test/prunegraft/tree1_Apruned.nwk")
    @test root == root_ref
    # Pruning with copy
	root = deepcopy(root_1)
	global A2 = prunenode(root.child[1].child[1])[1]
	@test root == root_1 && A == A2
end

@testset "Grafting" begin
	root = deepcopy(root_1)
	A = prunenode!(root.child[1].child[1])[1]
	graftnode!(root.child[2].child[1], A);
	@test root == TreeTools.read_newick("$(dirname(pathof(TreeTools)))/../test/prunegraft/tree_grafttest1.nwk")
end

@testset "Deleting" begin
	root = deepcopy(root_1)
	temp = delete_node!(root.child[1])
	@test temp == root && root == TreeTools.read_newick("$(dirname(pathof(TreeTools)))/../test/prunegraft/tree_del1.nwk")
end

@testset "Deleting branches" begin
    root = TreeTools.read_newick("$(dirname(pathof(TreeTools)))/../test/prunegraft/tree_testnullbranches.nwk")
    TreeTools.delete_null_branches!(root)
    @test root == TreeTools.read_newick("$(dirname(pathof(TreeTools)))/../test/prunegraft/tree_testnullbranches_.nwk")
end

nwk1 = "(A,(B,(C,D)))"
t1 = node2tree(TreeTools.parse_newick(nwk1))

max_stars_repo_name: PierreBarrat/TreeTools

max_issues_repo_name: PierreBarrat/TreeTools

max_forks_repo_name: PierreBarrat/TreeTools


text:
function EOPHistory(arg0::IERSConventions, arg1::Collection, arg2::jboolean, arg3::TimeScales)
    return EOPHistory((IERSConventions, Collection, jboolean, TimeScales), arg0, arg1, arg2, arg3)
end

function check_eop_continuity(obj::EOPHistory, arg0::jdouble)
    return jcall(obj, "checkEOPContinuity", void, (jdouble,), arg0)
end

function get_conventions(obj::EOPHistory)
    return jcall(obj, "getConventions", IERSConventions, ())
end

function get_end_date(obj::EOPHistory)
    return jcall(obj, "getEndDate", AbsoluteDate, ())
end

function get_entries(obj::EOPHistory)
    return jcall(obj, "getEntries", List, ())
end

function get_equinox_nutation_correction(obj::EOPHistory, arg0::AbsoluteDate)
    return jcall(obj, "getEquinoxNutationCorrection", Vector{jdouble}, (AbsoluteDate,), arg0)
end

function get_equinox_nutation_correction(obj::EOPHistory, arg0::FieldAbsoluteDate)
    return jcall(obj, "getEquinoxNutationCorrection", Vector{RealFieldElement}, (FieldAbsoluteDate,), arg0)
end

function get_itrf_version(obj::EOPHistory, arg0::AbsoluteDate)
    return jcall(obj, "getITRFVersion", ITRFVersion, (AbsoluteDate,), arg0)
end

function get_lod(obj::EOPHistory, arg0::AbsoluteDate)
    return jcall(obj, "getLOD", jdouble, (AbsoluteDate,), arg0)
end

function get_lod(obj::EOPHistory, arg0::FieldAbsoluteDate)
    return jcall(obj, "getLOD", RealFieldElement, (FieldAbsoluteDate,), arg0)
end

function get_non_interpolating_eop_history(obj::EOPHistory)
    return jcall(obj, "getNonInterpolatingEOPHistory", EOPHistory, ())
end

function get_non_rotatin_origin_nutation_correction(obj::EOPHistory, arg0::AbsoluteDate)
    return jcall(obj, "getNonRotatinOriginNutationCorrection", Vector{jdouble}, (AbsoluteDate,), arg0)
end

function get_non_rotatin_origin_nutation_correction(obj::EOPHistory, arg0::FieldAbsoluteDate)
    return jcall(obj, "getNonRotatinOriginNutationCorrection", Vector{RealFieldElement}, (FieldAbsoluteDate,), arg0)
end

function get_pole_correction(obj::EOPHistory, arg0::AbsoluteDate)
    return jcall(obj, "getPoleCorrection", PoleCorrection, (AbsoluteDate,), arg0)
end

function get_pole_correction(obj::EOPHistory, arg0::FieldAbsoluteDate)
    return jcall(obj, "getPoleCorrection", FieldPoleCorrection, (FieldAbsoluteDate,), arg0)
end

function get_start_date(obj::EOPHistory)
    return jcall(obj, "getStartDate", AbsoluteDate, ())
end

function get_time_scales(obj::EOPHistory)
    return jcall(obj, "getTimeScales", TimeScales, ())
end

function get_ut1_minus_utc(obj::EOPHistory, arg0::AbsoluteDate)
    return jcall(obj, "getUT1MinusUTC", jdouble, (AbsoluteDate,), arg0)
end

function get_ut1_minus_utc(obj::EOPHistory, arg0::FieldAbsoluteDate)
    return jcall(obj, "getUT1MinusUTC", RealFieldElement, (FieldAbsoluteDate,), arg0)
end

function is_simple_eop(obj::EOPHistory)
    return jcall(obj, "isSimpleEop", jboolean, ())
end

function uses_interpolation(obj::EOPHistory)
    return jcall(obj, "usesInterpolation", jboolean, ())
end


text:
using HTTP, DataFrames, Gumbo, Cascadia, PyPlot

sorgente = HTTP.get("https://it.wikipedia.org/wiki/Leone_d%27oro_al_miglior_film");
println(sorgente.status)

data = parsehtml(String(sorgente.body))

# elimino a mano le righe degli anni in cui la mostra non è presente
#  questo passaggio dovrebbe essere fatto meglio, di fatto non è automatizzato
data[1:end .!= ""]

function pulizia(a, b) for i in b a=pulizia(a, i) end; a end;
html_data = "Anno,Film,Regista,Nazione\n" *  # specifico l'intestazione
         pulizia(
            data[findfirst(r"
"s, data)],
            ["\t"=>"",
             "\r\n"=>"",
             r"
"=>"",
             "
"=>"\n",
             r"	"=>"\",\"",
             r"<[/]?td>"=>"\"",
             r"<[/]?tbody>"=>""]);

df = readtable(IOBuffer(html_data))

df = by(df, :Nazione, nrow)

showcols(df)

x = df.Nazione
y = df.x1

plot(y, x, color="red", linewidth=2.0, linestyle="-")

savefig("vincitori.png")

max_stars_repo_name: ali-ce/j-r-p

max_issues_repo_name: ali-ce/j-r-p

max_forks_repo_name: ali-ce/j-r-p


text:
#=======================================================================
The game 2048 is a video game originally authored by Gabriele Cirulli.
See https://en.wikipedia.org/wiki/2048_(video_game) or, on Youtube,
https://gaming.youtube.com/game/UCkIzbSwpjk5eRRNaZPxTy2w
This example includes use of the Toolbar and Grid features of Gtk.jl.
========================================================================#

using Gtk
using Gtk.ShortNames

@enum Direction2048 Right Left Up Down

"""
    shifttiles!
The adding and condensing code is for a leftward shift, so if the move is not
leftward, this will rotate matrix to make move leftward, move, then undo rotation.
"""
function shifttiles!(b, siz, direction)
    if direction == Right
        tmpb = rot180(b); points, winner = leftshift!(tmpb, siz); tmpb = rot180(tmpb)
    elseif direction == Up
        tmpb = rotl90(b); points, winner = leftshift!(tmpb, siz); tmpb = rotr90(tmpb)
    elseif direction == Down
        tmpb = rotr90(b); points, winner = leftshift!(tmpb, siz); tmpb = rotl90(tmpb)
    else # left movement function as coded
        return leftshift!(b, siz) # b already modified so skip copy step below
    end
    for i in 1:siz, j in 1:siz
        b[i,j] = tmpb[i,j]   # copy tmpb contents back to b (modifies b)
    end
    points, winner
end

"""
    leftshift!
Work row by row. First, compact tiles to the left if possible. Second, find and
replace paired tiles in the row, then re-compact. Keep score of merges and return
as pointsgained. If a 2048 value tile is created, return a winner true value.
"""
function leftshift!(b, siz)
    pointsgained = 0
    winner = false
    for i in 1:siz
        tmprow = zeros(Int, siz)
        tmppos = 1
        for j in 1:siz
            if b[i,j] != 0
                tmprow[tmppos] = b[i,j]
                tmppos += 1
            end
        end
        b[i,:] = tmprow
        tmprow = zeros(Int, siz)
        tmppos = 1
        for j in 1:siz
            if j < siz && b[i,j] == b[i,j+1]
                b[i,j] = 2 * b[i,j]
                b[i,j+1] = 0
                pointsgained += b[i,j]
                if b[i,j] == 2048     # made a 2048 tile, which wins game
                    winner = true
                end
            end
            if b[i,j] != 0
                tmprow[tmppos] = b[i,j]
                tmppos += 1
            end
        end
        b[i,:] = tmprow
    end
    pointsgained, winner
end

"""
    app2048
Run game app, with boardsize (choose 4 for original game) as an argument.
"""
function app2048(bsize)
    win = Window("2048 Game", 400, 400) |> (Frame() |> (box = Box(:v)))
    toolbar = Toolbar()
    newgame = ToolButton("New Game")
    set_gtk_property!(newgame, :label, "New Game")
    set_gtk_property!(newgame, :is_important, true)
    undomove = ToolButton("Undo Move")
    set_gtk_property!(undomove, :label, "Undo Move")
    set_gtk_property!(undomove, :is_important, true)
    map(w->push!(toolbar,w),[newgame,undomove])
    grid = Grid()
    map(w -> push!(box, w),[toolbar, grid])
    buttons = Array{Gtk.GtkButtonLeaf,2}(undef, bsize, bsize)
    for i in 1:bsize, j in 1:bsize
        grid[i,j] = buttons[i,j] = Button()
        set_gtk_property!(buttons[i,j], :expand, true)
    end
    board = zeros(Int, (bsize,bsize))
    pastboardstates = []
    score = 0
    gameover = false
    condition = Condition()
    won = ""

    function update!()
        for i in 1:bsize, j in 1:bsize
            label = (board[i,j] > 0) ? board[i,j] : " "
            set_gtk_property!(buttons[i,j], :label, label)
        end
        set_gtk_property!(win, :title, "$won 2048 Game  (Score: $score)")
    end
    function newrandomtile!()
        blanks = Array{Tuple{Int,Int},1}()
        for i in 1:bsize, j in 1:bsize
            if board[i,j] == 0
                push!(blanks, (i,j))
            end
        end
        if length(blanks) == 0
            gameover = true
        else
            i,j = rand(blanks)
            board[i,j] = (rand() > 0.8) ? 4 : 2
        end
    end
    function initialize!(w)
        won = ""
        gameover = false
        for i in 1:bsize, j in 1:bsize
            board[i,j] = 0
            set_gtk_property!(buttons[i,j], :label, " ")
        end
        newrandomtile!()
        update!()
    end
    function undo!(w)
        if gameover == false && length(pastboardstates) > 0
            board = pop!(pastboardstates)
            update!()
        end
    end
    function keypress(w, event)
        presses = Dict(0xff51 => Up,      # code rotated 90 degrees
                       0xff52 => Left,    # because of Gtk coordinates
                       0xff53 => Down,
                       0xff54 => Right)
        keycode = event.keyval
        if haskey(presses, keycode) && gameover == false
            push!(pastboardstates, copy(board))
            newpoints, havewon = shifttiles!(board, bsize, presses[keycode])
            score += newpoints
            if havewon && won != "Winning"
                won = "Winning"
                info_dialog("You have won the game.")
            end
            newrandomtile!()
            update!()
            if gameover
                info_dialog("Game over.\nScore: $score")
            end
        end
    end
    endit(w) = notify(condition)
    initialize!(win)
    signal_connect(initialize!, newgame, :clicked)
    signal_connect(undo!,undomove, :clicked)
    signal_connect(endit, win, :destroy)
    signal_connect(keypress, win, "key-press-event")
    showall(win)
    wait(condition)
end

const boardsize = 4
app2048(boardsize)


text:
# Two-state Bioreactor
id = :PerezGalvan2017d

pL = [0.8; 1.1]
pU = [0.85; 1.2]
PerezGalvan2017d_x0(p::Vector{T}) where T = T[p[1]; 0.8*one(T)]
function PerezGalvan2017d_f!(du::Vector{T}, u::Vector{T}, p::Vector{T}, t) where T
    α = 0.5; k = 10.53; D = 0.36; Sf = 5.7; Ks = 7.0; k0 = 0.022
    μ = p[2]*u[2]/(Ks + u[2])
    du[1] = (μ - α*D)*u[1]
    du[2] = D*(Sf - u[2]) - k*μ*u[1]
    return
end
tspan = (0.0, 10.0)

prob = ODERelaxProb(PerezGalvan2017d_f!, tspan, PerezGalvan2017d_x0, pL, pU)
url = "https://www.sciencedirect.com/science/article/abs/pii/S0098135417300923"
source = "PerezGalvan2017"
desc = "Two-state Bioreactor"


text:
using LoopVectorization, Test

function reference_mul4!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @inbounds @fastmath for a1i ∈ eachindex(range_a),
    a2i ∈ eachindex(range_a),
    b1i ∈ eachindex(range_b),
    b2i ∈ eachindex(range_b)

    a1 = range_a[a1i]
    a2 = range_a[a2i]
    b1 = range_b[b1i]
    b2 = range_b[b2i]
    contribution = zero(eltype(target_arr))
    for i_a ∈ padded_axis_a, i_b ∈ padded_axis_b
      contribution += src[i_a, i_b] * src[i_a+a1, i_b+b1] * src[i_a+a2, i_b+b2]
    end
    target_arr[b1i, b2i] += contribution
  end
end
function mul4_turbo_v1!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @turbo for a1i ∈ eachindex(range_a),
    a2i ∈ eachindex(range_a),
    b1i ∈ eachindex(range_b),
    b2i ∈ eachindex(range_b)

    a1 = range_a[a1i]
    a2 = range_a[a2i]
    b1 = range_b[b1i]
    b2 = range_b[b2i]
    contribution = zero(eltype(target_arr))
    for i_a ∈ padded_axis_a, i_b ∈ padded_axis_b
      contribution += src[i_a, i_b] * src[i_a+a1, i_b+b1] * src[i_a+a2, i_b+b2]
    end
    target_arr[b1i, b2i] += contribution
  end
  target_arr
end
function mul4_turbo_v2!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @turbo for a1i ∈ eachindex(range_a),
    a2i ∈ eachindex(range_a),
    b1i ∈ eachindex(range_b),
    b2i ∈ eachindex(range_b)

    a1 = range_a[a1i]
    a2 = range_a[a2i]
    b1 = range_b[b1i]
    b2 = range_b[b2i]
    contribution = target_arr[b1i, b2i]
    for i_a ∈ padded_axis_a, i_b ∈ padded_axis_b
      contribution += src[i_a, i_b] * src[i_a+a1, i_b+b1] * src[i_a+a2, i_b+b2]
    end
    target_arr[b1i, b2i] = contribution
  end
end
function mul4_turbo_v3!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @turbo for a1i ∈ eachindex(range_a),
    a2i ∈ eachindex(range_a),
    b1i ∈ eachindex(range_b),
    b2i ∈ eachindex(range_b),
    i_a ∈ padded_axis_a,
    i_b ∈ padded_axis_b

    a1 = range_a[a1i]
    a2 = range_a[a2i]
    b1 = range_b[b1i]
    b2 = range_b[b2i]
    target_arr[b1i, b2i] += src[i_a, i_b] * src[i_a+a1, i_b+b1] * src[i_a+a2, i_b+b2]
  end
end
function mul4_turbo_v4!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @turbo for b1i ∈ eachindex(range_b), b2i ∈ eachindex(range_b)
    b1 = range_b[b1i]
    b2 = range_b[b2i]
    contribution = zero(eltype(target_arr))
    for i_a ∈ padded_axis_a,
      i_b ∈ padded_axis_b,
      a1i ∈ eachindex(range_a),
      a2i ∈ eachindex(range_a)

      a1 = range_a[a1i]
      a2 = range_a[a2i]
      contribution += src[i_a, i_b] * src[i_a+a1, i_b+b1] * src[i_a+a2, i_b+b2]
    end
    target_arr[b1i, b2i] += contribution
  end
end

@testset "Inner reductions" begin
  src = ones(19, 101)
  max_a = 7
  max_b = 9
  range_a = -max_a:max_a
  range_b = -max_b:max_b

  target_arr = zeros(Float64, length(range_b), length(range_b))

  padded_axis_b = (first(axes(src, 2)).+max_b):(last(axes(src, 2))-max_b)
  padded_axis_a = (first(axes(src, 1)).+max_a):(last(axes(src, 1))-max_a)

  target_ref = zero(target_arr)
  reference_mul4!(target_ref, src, range_a, range_b, padded_axis_a, padded_axis_b)

  mul4_turbo_v1!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @test target_arr ≈ target_ref
  target_arr .= 0
  mul4_turbo_v2!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @test target_arr ≈ target_ref
  target_arr .= 0
  mul4_turbo_v3!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @test target_arr ≈ target_ref
  target_arr .= 0
  mul4_turbo_v4!(target_arr, src, range_a, range_b, padded_axis_a, padded_axis_b)
  @test target_arr ≈ target_ref

end


text:
using DataFrames
using StatsBase

"""
	rxy(x::Real,y::Real)
	r -> sqrt{x^2 + y^2}
"""
function rxy(x::Real, y::Real)
    return sqrt(x^2 + y^2)
end

"""
	hixy(x::Real, y::Real)
	phi -> atan(y/x)
"""
function phixy(x::Real, y::Real)
    return atan(y,x)
end

"""
	phixy(hitdf::DataFrame)
	phi -> atan(y/x) where y and x are columns of the data frame
"""
function fphi(hitdf::DataFrame)
    return atan.(hitdf.y,hitdf.x)
end

"""
	dxyz(x1::Vector{<:Real}, x2::Vector{<:Real})

Distance between two points.
"""
function dxyz(x1::Vector{<:Real}, x2::Vector{<:Real})
    return sqrt((x1[1] - x2[1])^2 + (x1[2] - x2[2])^2 + (x1[3] - x2[3])^2)
end

"""
	gline2p(x1::Real, y1::Real, x2::Real, y2::Real)
	Return function of a line that goes through (x1, y1), (x2, y2)
"""
function gline2p(x1::Real, y1::Real, x2::Real, y2::Real)
    fxy(x::Real) = y1 + (x - x1) * (y2 - y1)/(x2 -x1)
end

"""
	mean_std(x::Vector{T}, xmin::T=typemin(T), xmax::T=typemax(T);
	         w::Union{Nothing, Vector{T}}=nothing) where T
Returns mean and std of a vector x in the interval between xmin and xmax.
Using weights w if requested.
"""
function mean_std(x::Vector{T}, xmin::T=typemin(T), xmax::T=typemax(T);
	              w::Union{Nothing, Vector{T}}=nothing) where T
	mask = broadcast(range_bound(xmin, xmax, ATools.OpenBound), x)
    xx   = x[mask]
	if isnothing(w)
		ww = ones(eltype(xx), length(xx))
	else
		ww = w[mask]
	end
    return mean_and_std(xx, FrequencyWeights(ww), corrected=true)
end

max_stars_repo_name: andLaing/ATools

max_issues_repo_name: andLaing/ATools

max_forks_repo_name: andLaing/ATools


text:
using Genie, App.Heres

### Your tests here
@test 1 == 1

max_stars_repo_name: huaxk/rlservice

max_issues_repo_name: huaxk/rlservice

max_forks_repo_name: huaxk/rlservice


text:
function selection_sort!(arr::Vector{T}) where {T}
    l = length(arr)
    for i in 1:l-1
        place = i
        for j in i+1:l
            if arr[j] < arr[place]
                place = j
            end
        end
        temp = arr[i]
        arr[i] = arr[place]
        arr[place] = temp
    end
end

max_issues_repo_name: AugustoCL/Julia

max_forks_repo_name: AugustoCL/Julia


text:
"""
spin20.jl - sampling robustness for the empty spin problem
"""

max_stars_repo_name: SchusterLab/rbqoc

max_issues_repo_name: SchusterLab/rbqoc

max_forks_repo_name: SchusterLab/rbqoc


text:
################################################################################
#  Copyright 2020, Marta Vanin, Tom Van Acker                                  #
################################################################################
# PowerModelsDistributionStateEstimation.jl                                    #
# An extention package of PowerModels(Distribution).jl for Static Power System #
# State Estimation.                                                            #
################################################################################
module PowerModelsDistributionStateEstimation

# import pkgs
import Base: maximum, minimum, rand
import CSV
import DataFrames
import Distributions
import Distributions: logpdf, gradlogpdf
import ForwardDiff
import GaussianMixtures
import InfrastructureModels
import JuMP
import LinearAlgebra
import LinearAlgebra: diag, diagm, I
import Logging, LoggingExtras
import Optim
import PowerModelsDistribution
import PowerModelsDistribution: _has_nl_expression #need this to use @smart_constraint
import Random
import SpecialFunctions
import Statistics

#import and export so that users do not need to import JuMP to use a solver
import JuMP: optimizer_with_attributes
export optimizer_with_attributes

# pkg const
const _CSV = CSV
const _DFS = DataFrames
const _DST = Distributions
const _GMM = GaussianMixtures
const _IM  = InfrastructureModels
const _PMD = PowerModelsDistribution
const _PMDSE = PowerModelsDistributionStateEstimation
const _RAN = Random
const _SF  = SpecialFunctions
const _STT = Statistics

# paths
const BASE_DIR = dirname(@__DIR__)

# fix a random seed
#_RAN.seed!(1234);

# logger for errors and warnings, etc.

include("core/logging.jl")
function __init__()
    global _DEFAULT_LOGGER = Logging.current_logger()
    global _LOGGER = Logging.ConsoleLogger(; meta_formatter = PowerModelsDistributionStateEstimation._pmdse_metafmt)

    Logging.global_logger(_LOGGER)
end

# include
include("bad_data/chi_squares_test.jl")
include("bad_data/largest_normalized_residuals.jl")
include("bad_data/measurement_functions.jl")
include("bad_data/utils.jl")

include("core/constraint.jl")
include("core/measurement_conversion.jl")
include("core/objective.jl")
include("core/start_values_methods.jl")
include("core/utils.jl")
include("core/variable.jl")

include("form/adapted_pmd_constraints.jl")
include("form/reduced_ac.jl")
include("form/reduced_ivr.jl")

include("io/distributions.jl")
include("io/measurement_parser.jl")
include("io/network_parser.jl")
include("io/postprocessing.jl")

include("prob/se.jl")

# export
include("core/export.jl")

end


text:
# This file was generated, do not modify it. # hide
r_mpi = range(mdl, :(decision_tree_classifier.max_depth), lower=1, upper=10)
r_msl = range(mdl, :(decision_tree_classifier.min_samples_leaf), lower=1, upper=50)

tm = TunedModel(
    model=mdl,
    ranges=[r_mpi, r_msl],
    tuning=Grid(resolution=8),
    resampling=CV(nfolds=5, rng=112),
    operation=predict_mode,
    measure=misclassification_rate
)
mtm = machine(tm, X, y)
fit!(mtm, rows=train)

ypred = predict_mode(mtm, rows=test)
misclassification_rate(ypred, y[test])

max_issues_repo_name: alan-turing-institute/MLJTutorials

max_forks_repo_name: alan-turing-institute/MLJTutorials


text:
using FourierCore.Domain

@testset "Domain Construction" begin
    @test Circle() isa Circle
    @test Circle(0) isa Circle
    @test Circle(0) × Circle(0) isa Torus
    @test Circle(0)^2  isa Torus
    @test Circle(0)^3 isa  Torus
    T¹ = S¹(2π) × S¹(2π)
    @test(T¹ × S¹(1π) isa Torus) # note π is an irrational type
end

@testset "Correct Values in Correct Locations" begin
    S = Circle(1.0, 2.0)
    @test S.a == 1.0
    @test S.b == 2.0
    T¹ = S¹(2, 3) × S¹(5.0, 7.0)
    @test T¹[1].a == 2
    @test T¹[1].b == 3
    @test T¹[2].a == 5.0
    @test T¹[2].b == 7.0
end

max_stars_repo_name: sandreza/FourierCore

max_issues_repo_name: sandreza/FourierCore

max_forks_repo_name: sandreza/FourierCore


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

using Markdown
using InteractiveUtils

# ╔═╡ 0bdd0fc6-9203-11eb-1ea3-9fe58aca7da0
using SymPy, LinearAlgebra

# ╔═╡ aa073e00-323c-11eb-0297-83e3db12248d
md"
# Gradijent, divergencija i rotacija

__Nabla__ ili __Hamiltonov operator__  je

$$\nabla = \frac{\partial}{\partial x} \vec{\imath}+\frac{\partial}{\partial y} \vec{\jmath} +\frac{\partial}{\partial z} \vec{k}.$$

Nabla ima svojstva derivacije i vektora.

__Gradijent__ skalarnog polja $f(x,y,z)$ je

$\mathop{\mathrm{grad}} f = \nabla \cdot f=\frac{\partial f}{\partial x} \vec{\imath}+\frac{\partial f}{\partial y} \vec{\jmath} +\frac{\partial f}{\partial z} \vec{k}.$

__Divergencija__ vektorskog polja

$\vec{w}(x,y,z)=w_x(x,y,z)\vec{\imath} + w_y(x,y,z) \vec{\jmath} + w_z(x,y,z)\vec{k}$

je

$\mathop{\mathrm{div}} \vec{w} = \nabla \cdot \vec{w}= \frac{\partial w_x}{\partial x} +\frac{\partial w_y}{\partial y} +\frac{\partial w_z}{\partial z}.$

__Rotacija__ vektorskog polja $\vec{w}(x,y,z)$ je

$\mathop{\mathrm{rot}} \vec{w} = \nabla \times \vec{w}= \begin{vmatrix} \vec{\imath} & \vec{\jmath} & \vec{k} \\ \frac{\partial}{\partial x} & \frac{\partial}{\partial y} & \frac{\partial}{\partial z} \\ w_x & w_y &w_z \end{vmatrix}.$

__Usmjerena derivacija__ skalarnog polja $f(x,y,z)$ u smjeru vektora $\vec a$ je

$$\frac{\partial f}{\partial \vec a}(x,y,z)=\vec a_0 \cdot \mathop{\mathrm{grad}} f(x,y,z).$$

__Usmjerena derivacija__ vektorskog polja $\vec w(x,y,z)$ u smjeru vektora $\vec a$ je

$$\frac{\partial \vec w}{\partial \vec a}(x,y,z)=(\vec a_0 ∇) \vec w(x,y,z).$$
"

# ╔═╡ c75d36c0-923c-11eb-0477-c3101e21f880
x,y,z=symbols(:x),symbols(:y),symbols(:z)

# ╔═╡ 1b261092-923a-11eb-01d6-7f705b30d46e
begin
	∂x(f::Sym) = diff(f,x)
	∂y(f::Sym) = diff(f,y)
	∂z(f::Sym) = diff(f,z)
	∇=[∂x,∂y,∂z]
end

# ╔═╡ 1ca28980-925d-11eb-2be1-815a5df595ff
begin
	import Base.*
	*(∂x::typeof(∂x),f::Sym)=∂x(f)
	*(∂y::typeof(∂y),f::Sym)=∂y(f)
	*(∂z::typeof(∂z),f::Sym)=∂z(f)
	*(∇::Function,f::Sym)=∇(f)
	grad(f)=∇*f
	rot(u)=∇×u
	import LinearAlgebra.⋅
	⋅(∇::Array{Function,1},w::Array{Sym,1})=∇[1]*w[1]+∇[2]*w[2]+∇[3]*w[3]
	div(u)=∇⋅u
	⋅(a::Vector,∇::Array{Function,1})=a[1]*∇[1]+a[2]*∇[2]+a[3]*∇[3]
end

# ╔═╡ c2a476e6-e948-485f-9b2e-b6f821504263
# Izvedeni operator a∇
begin
	a∂x(a::Vector,f::Sym) = a[1]*diff(f,x)
	a∂y(a::Vector,f::Sym) = a[2]*diff(f,y)
	a∂z(a::Vector,f::Sym) = a[3]*diff(f,z)
	a∇(a::Vector,f::Sym)=a∂x(a,f)+a∂y(a,f)+a∂z(a,f)
	a∇(a::Vector,w::Array{Sym,1})=[a∇(a,w[1]),a∇(a,w[2]),a∇(a,w[3])]
end

# ╔═╡ bd70ec2e-ad07-4526-aa70-5f25c86b444e
md"
## Primjeri
"

# ╔═╡ fd3305b2-923a-11eb-15c4-5b79db40bf46
begin
	# Skalarna polja
	f=x^2+3*y*z+5
	g=x*y*z
	# Vektorska polja
	w=[y*z,z*x,x*y]
	u=[x^2*y,x*y,z/x]
	# Vektor
	a=[1,1,-2]
	# Točka
	T=(1,-1/2,2)
end

# ╔═╡ 2b00c286-0807-4fa0-a9c2-6b1d56ae3716
f

# ╔═╡ 66b9df0a-06c4-409c-9ae4-79b3cc3833b5
∂x(f)

# ╔═╡ 9b816a31-675a-4dea-83e7-67430eeb741e
∂x*f

# ╔═╡ ec770eca-54d2-4394-b359-ad5e47218e92
a∇(a,f)

# ╔═╡ 3577e5e0-8fa8-4938-9119-b7c14f31e5be
a∇(a,u)

# ╔═╡ 2a47ed85-0691-4cb2-84b5-52facb4eda0b
# Gradijent
∇*f

# ╔═╡ 26292a50-9627-40a3-bacc-034de0882ac2
grad(f)

# ╔═╡ 8d4a98d1-414e-4714-9c94-6b6426388bc0
# Gradijent u zadanoj točki
subs.(∇*f,x=>T[1],y=>T[2],z=>T[3])

# ╔═╡ c62f2abe-b330-4e5b-8a73-c2ac5fe32284
# Divergencija
∇⋅u

# ╔═╡ f141e418-574d-4a0f-903c-35ad8d3e6883
div(u)

# ╔═╡ 0e012c70-afc3-410f-a73c-978783948cc3
# Divergencija u zadanoj točki
subs(∇⋅u,x=>T[1],y=>T[2],z=>T[2])

# ╔═╡ faea848e-f4d7-4476-993f-d9db5c6ca2ee
# Rotor
∇×u

# ╔═╡ e3c9e5d1-8542-4940-8dd2-c60849b152c3
rot(u)

# ╔═╡ 0496ef19-9e5c-4f5b-aa4b-22d98cfcf9a0
# Rotor u zadanoj točki
subs.(∇×u,x=>T[1],y=>T[2],z=>T[3])

# ╔═╡ ad3b7955-cd19-4aaf-8a98-b4727bafe572
# Primjer svojstva
rot(f*grad(g))

# ╔═╡ 93446240-2427-4964-9e3a-75f2de6b67e6
grad(f)×grad(g)

# ╔═╡ 44c1a521-657b-43c4-ac59-7a8e24bf25cc
# Drugi primjer svojstva
rot(w×u)

# ╔═╡ 54692583-1661-4b04-8f2c-94689630cff8
expand.(w*div(u)-u*div(w)+a∇(u,w)-a∇(w,u))

# ╔═╡ fadb40e2-6c5e-48b2-a45f-88ea4f8cc4e5
# Usmjerena derivacija skalarnog polja
a/norm(a)⋅grad(f)

# ╔═╡ 180512f7-c8e8-4aee-9872-0057d6fe9c5d
# Usmjerena derivacija skalarnog polja u točki
subs(a/norm(a)⋅grad(f),x=>T[1],y=>T[2],z=>T[3])

# ╔═╡ 258fbdc7-7c24-4ecf-bc5a-468069f5c482
# Skalarno polje najbrže raste u smjeru gradijenta
subs(grad(f)/norm(grad(f))⋅grad(f),x=>T[1],y=>T[2],z=>T[3])

# ╔═╡ eeb8e757-668e-48c1-b560-076b3f47b5bf
# Usmjerena derivacija vektorskog polja
a∇(a/norm(a),u)

# ╔═╡ 694962cc-cca2-4a4d-939d-7eb329de9917
# Usmjerena derivacija vektorskog polja u točki
subs.(a∇(a/norm(a),u),x=>T[1],y=>T[2],z=>T[3])

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"

[compat]
SymPy = "~1.0.52"
"""

# ╔═╡ 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"

[[ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "a325370b9dd0e6bf5656a6f1a7ae80755f8ccc46"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.7.2"

[[CommonEq]]
git-tree-sha1 = "d1beba82ceee6dc0fce8cb6b80bf600bbde66381"
uuid = "3709ef60-1bee-4518-9f2f-acd86f176c50"
version = "0.2.0"

[[CommonSolve]]
git-tree-sha1 = "68a0743f578349ada8bc911a5cbd5a2ef6ed6d1f"
uuid = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
version = "0.2.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"

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

[[Conda]]
deps = ["JSON", "VersionParsing"]
git-tree-sha1 = "299304989a5e6473d985212c28928899c74e9421"
uuid = "8f4d0f93-b110-5947-807f-2305c1781a2d"
version = "1.5.2"

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

[[DocStringExtensions]]
deps = ["LibGit2"]
git-tree-sha1 = "a32185f5428d3986f47c2ab78b1f216d5e6cc96f"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.8.5"

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

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

[[IrrationalConstants]]
git-tree-sha1 = "f76424439413893a832026ca355fe273e93bce94"
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
version = "0.1.0"

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

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

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

[[LogExpFunctions]]
deps = ["ChainRulesCore", "DocStringExtensions", "IrrationalConstants", "LinearAlgebra"]
git-tree-sha1 = "34dc30f868e368f8a17b728a1238f3fcda43931a"
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
version = "0.3.3"

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

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

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

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

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

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

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

[[OpenLibm_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "05823500-19ac-5b8b-9628-191a04bc5112"

[[OpenSpecFun_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1"
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
version = "0.5.5+0"

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

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

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

[[PyCall]]
deps = ["Conda", "Dates", "Libdl", "LinearAlgebra", "MacroTools", "Serialization", "VersionParsing"]
git-tree-sha1 = "169bb8ea6b1b143c5cf57df6d34d022a7b60c6db"
uuid = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
version = "1.92.3"

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

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

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

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

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

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

[[SpecialFunctions]]
deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"]
git-tree-sha1 = "793793f1df98e3d7d554b65a107e9c9a6399a6ed"
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
version = "1.7.0"

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

[[SymPy]]
deps = ["CommonEq", "CommonSolve", "LinearAlgebra", "Markdown", "PyCall", "RecipesBase", "SpecialFunctions"]
git-tree-sha1 = "1ef257ecbcab8058595a68ca36a6844b41babcbd"
uuid = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
version = "1.0.52"

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

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

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

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

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

[[VersionParsing]]
git-tree-sha1 = "80229be1f670524750d905f8fc8148e5a8c4537f"
uuid = "81def892-9a0e-5fdd-b105-ffc91e053289"
version = "1.2.0"

[[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:
# ╟─aa073e00-323c-11eb-0297-83e3db12248d
# ╠═0bdd0fc6-9203-11eb-1ea3-9fe58aca7da0
# ╠═c75d36c0-923c-11eb-0477-c3101e21f880
# ╠═1b261092-923a-11eb-01d6-7f705b30d46e
# ╠═1ca28980-925d-11eb-2be1-815a5df595ff
# ╠═c2a476e6-e948-485f-9b2e-b6f821504263
# ╟─bd70ec2e-ad07-4526-aa70-5f25c86b444e
# ╠═fd3305b2-923a-11eb-15c4-5b79db40bf46
# ╠═2b00c286-0807-4fa0-a9c2-6b1d56ae3716
# ╠═66b9df0a-06c4-409c-9ae4-79b3cc3833b5
# ╠═9b816a31-675a-4dea-83e7-67430eeb741e
# ╠═ec770eca-54d2-4394-b359-ad5e47218e92
# ╠═3577e5e0-8fa8-4938-9119-b7c14f31e5be
# ╠═2a47ed85-0691-4cb2-84b5-52facb4eda0b
# ╠═26292a50-9627-40a3-bacc-034de0882ac2
# ╠═8d4a98d1-414e-4714-9c94-6b6426388bc0
# ╠═c62f2abe-b330-4e5b-8a73-c2ac5fe32284
# ╠═f141e418-574d-4a0f-903c-35ad8d3e6883
# ╠═0e012c70-afc3-410f-a73c-978783948cc3
# ╠═faea848e-f4d7-4476-993f-d9db5c6ca2ee
# ╠═e3c9e5d1-8542-4940-8dd2-c60849b152c3
# ╠═0496ef19-9e5c-4f5b-aa4b-22d98cfcf9a0
# ╠═ad3b7955-cd19-4aaf-8a98-b4727bafe572
# ╠═93446240-2427-4964-9e3a-75f2de6b67e6
# ╠═44c1a521-657b-43c4-ac59-7a8e24bf25cc
# ╠═54692583-1661-4b04-8f2c-94689630cff8
# ╠═fadb40e2-6c5e-48b2-a45f-88ea4f8cc4e5
# ╠═180512f7-c8e8-4aee-9872-0057d6fe9c5d
# ╠═258fbdc7-7c24-4ecf-bc5a-468069f5c482
# ╠═eeb8e757-668e-48c1-b560-076b3f47b5bf
# ╠═694962cc-cca2-4a4d-939d-7eb329de9917
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002

max_stars_repo_name: ivanslapnicar/Matematika

max_issues_repo_name: ivanslapnicar/Matematika

max_forks_repo_name: ivanslapnicar/Matematika


text:
import Base.∈

export AbstractSingleton,
       element,
       an_element,
       linear_map

"""
    AbstractSingleton{N<:Real} <: AbstractHyperrectangle{N}

Abstract type for sets with a single value.

### Notes

Every concrete `AbstractSingleton` must define the following functions:
- `element(::AbstractSingleton{N})::Vector{N}` -- return the single element
- `element(::AbstractSingleton{N}, i::Int)::N` -- return the single element's
    entry in the `i`-th dimension

```jldoctest
julia> subtypes(AbstractSingleton)
2-element Array{Any,1}:
 Singleton
 ZeroSet
```
"""
abstract type AbstractSingleton{N<:Real} <: AbstractHyperrectangle{N} end

# --- AbstractHyperrectangle interface functions ---

"""
    radius_hyperrectangle(S::AbstractSingleton{N}, i::Int)::N where {N<:Real}

Return the box radius of a set with a single value in a given dimension.

### Input

- `S` -- set with a single value
- `i` -- dimension of interest

### Output

Zero.
"""
function radius_hyperrectangle(S::AbstractSingleton{N}, i::Int
                              )::N where {N<:Real}
    return zero(N)
end

"""
    radius_hyperrectangle(S::AbstractSingleton{N})::Vector{N} where {N<:Real}

Return the box radius of a set with a single value in every dimension.

### Input

- `S` -- set with a single value

### Output

The zero vector.
"""
function radius_hyperrectangle(S::AbstractSingleton{N}
                              )::Vector{N} where {N<:Real}
    return zeros(N, dim(S))
end

"""
    high(S::AbstractSingleton{N})::Vector{N} where {N<:Real}

Return the higher coordinates of a set with a single value.

### Input

- `S` -- set with a single value

### Output

A vector with the higher coordinates of the set with a single value.
"""
function high(S::AbstractSingleton{N})::Vector{N} where {N<:Real}
    return element(S)
end

"""
    high(S::AbstractSingleton{N}, i::Int)::N where {N<:Real}

Return the higher coordinate of a set with a single value in the given
dimension.

### Input

- `S` -- set with a single value
- `i` -- dimension of interest

### Output

The higher coordinate of the set with a single value in the given dimension.
"""
function high(S::AbstractSingleton{N}, i::Int)::N where {N<:Real}
    return element(S)[i]
end

"""
    low(S::AbstractSingleton{N})::Vector{N} where {N<:Real}

Return the lower coordinates of a set with a single value.

### Input

- `S` -- set with a single value

### Output

A vector with the lower coordinates of the set with a single value.
"""
function low(S::AbstractSingleton{N})::Vector{N} where {N<:Real}
    return element(S)
end

"""
    low(S::AbstractSingleton{N}, i::Int)::N where {N<:Real}

Return the lower coordinate of a set with a single value in the given
dimension.

### Input

- `S` -- set with a single value
- `i` -- dimension of interest

### Output

The lower coordinate of the set with a single value in the given dimension.
"""
function low(S::AbstractSingleton{N}, i::Int)::N where {N<:Real}
    return element(S)[i]
end

# --- AbstractCentrallySymmetric interface functions ---

"""
    center(S::AbstractSingleton{N})::Vector{N} where {N<:Real}

Return the center of a set with a single value.

### Input

- `S` -- set with a single value

### Output

The only element of the set.
"""
function center(S::AbstractSingleton{N})::Vector{N} where {N<:Real}
    return element(S)
end

# --- AbstractPolytope interface functions ---

"""
    vertices_list(S::AbstractSingleton{N})::Vector{Vector{N}} where {N<:Real}

Return the list of vertices of a set with a single value.

### Input

- `S` -- set with a single value

### Output

A list containing only a single vertex.
"""
function vertices_list(S::AbstractSingleton{N}
                      )::Vector{Vector{N}} where {N<:Real}
    return [element(S)]
end

"""
    linear_map(M::AbstractMatrix{N}, S::AbstractSingleton{N}) where {N<:Real}

Concrete linear map of an abstract singleton.

### Input

- `M` -- matrix
- `S` -- abstract singleton

### Output

The abstract singleton of the same type of ``S`` obtained by applying the
linear map to the element in ``S``.
"""
function linear_map(M::AbstractMatrix{N},
                    S::AbstractSingleton{N}) where {N<:Real}
    @assert dim(S) == size(M, 2) "a linear map of size $(size(M)) cannot be " *
                                 "applied to a set of dimension $(dim(S))"

    T = typeof(S)
    return T(M * element(S))
end

# --- LazySet interface functions ---

"""
    σ(d::AbstractVector{N}, S::AbstractSingleton{N}) where {N<:Real}

Return the support vector of a set with a single value.

### Input

- `d` -- direction
- `S` -- set with a single value

### Output

The support vector, which is the set's vector itself, irrespective of the given
direction.
"""
function σ(d::AbstractVector{N}, S::AbstractSingleton{N}) where {N<:Real}
    return element(S)
end

"""
    ∈(x::AbstractVector{N}, S::AbstractSingleton{N})::Bool where {N<:Real}

Check whether a given point is contained in a set with a single value.

### Input

- `x` -- point/vector
- `S` -- set with a single value

### Output

`true` iff ``x ∈ S``.

### Notes

This implementation performs an exact comparison, which may be insufficient with
floating point computations.
"""
function ∈(x::AbstractVector{N}, S::AbstractSingleton{N})::Bool where {N<:Real}
    return x == element(S)
end

# this operation is forbidden, but it is a common error
function ∈(S::AbstractSingleton{N}, X::LazySet{N})::Bool where {N<:Real}
    error("cannot make a point-in-set check if the left-hand side is " *
          "a set; either check for set inclusion, as in `S ⊆ X`, or check for " *
          "membership, as in `element(S) ∈ X` (the results are equivalent but " *
          "the implementations may differ)")
end

"""
    plot_recipe(S::AbstractSingleton{N}, [ε]::N=zero(N)) where {N<:Real}

Convert a singleton to a pair `(x, y)` of points for plotting.

### Input

- `S` -- singleton
- `ε` -- (optional, default: `0`) ignored, used for dispatch

### Output

A pair `(x, y)` of one point that can be plotted.
"""
function plot_recipe(S::AbstractSingleton{N}, ε::N=zero(N)) where {N<:Real}
    @assert dim(S) <= 3 "cannot plot a $(dim(S))-dimensional $(typeof(S))"

    if dim(S) == 1
        return [element(S)[1]], [zero(N)]
    else
        return [element(S)[1]], [element(S)[2]]
    end
end


text:
function goma(xs)
    return sum(x for x in xs)
end

function goma(x::T, y::T...) where {T<:Integer}
    return goma((x, y...))
end

@show goma(1, 2, 3)
@show goma((1, 2, 3))
@show goma([1, 2, 3])

max_stars_repo_name: terasakisatoshi/juliaExer

max_issues_repo_name: terasakisatoshi/juliaExer

max_forks_repo_name: terasakisatoshi/juliaExer


text:
using GeneralizedDynamicsFromData
using OrderedCollections
using JLD2
using FileIO

experiment_name = "roessler_statistics"
repetitions = 100

weight_decay = 1e-4
η = 1e-1
η_decay_rate = 0.5
η_decay_step = 100
η_limit = 1e-4

loss_weights = [1/2, 1/2]

construct_optimiser() = Flux.Optimiser(WeightDecay(weight_decay),
                                       ExpDecay(η,η_decay_rate,η_decay_step,η_limit),
                                       ADAM())
construct_loss(θ, y, predict) = polar_loss(θ, y, loss_weights, predict)

net_config = OrderedDict([
    :inputs => 3,
    :outputs => 1,
    :neurons => 16,
    :layers => 1,
    :non_lin => tanh,
    :initialization => Flux.glorot_normal
])

problem_period1 = Dict([:equation => roessler,
                        :parameters => Float64[0.1, 0.1, 4.0],
                        :u0 => Float64[1.0, 1.0, 1.0],
                        :tspan => (0.0f0, 10.0f0),
                        :ts => 0.1,
                        :solver => Tsit5,
                        :optimizer => construct_optimiser,
                        :max_iter => 1500,
                        :loss => construct_loss]
                      )

for noise in [5e-3, 1e-3, 1e-4, 1e-5]
  summary, callbacks = repeat_experiment(problem_period1,
                                        net_config,
                                        repetitions;
                                        ε = noise,
                                        progress=false)
  filename_cb = joinpath("./data/", experiment_name*"_p1_"*string(noise)*"_all.h5")
  filename_sum = joinpath("./data/", experiment_name*"_p1_"*string(noise)*".jld2")
  callbacks_to_hdf5(callbacks, filename_cb)
  save(filename_sum, summary)
end

problem_period2 = Dict([:equation => roessler,
                        :parameters => Float64[0.1, 0.1, 6.0],
                        :u0 => Float64[1.0, 1.0, 1.0],
                        :tspan => (0.0f0, 10.0f0),
                        :ts => 0.1,
                        :solver => Tsit5,
                        :optimizer => construct_optimiser,
                        :max_iter => 1500,
                        :loss => construct_loss]
                      )

for noise in [5e-3, 1e-3, 1e-4, 1e-5]
  summary, callbacks = repeat_experiment(problem_period2,
                                        net_config,
                                        repetitions;
                                        ε = noise,
                                        progress=false)
  filename_cb = joinpath("./data/", experiment_name*"_p2_"*string(noise)*"_all.h5")
  filename_sum = joinpath("./data/", experiment_name*"_p2_"*string(noise)*".jld2")
  callbacks_to_hdf5(callbacks, filename_cb)
  save(filename_sum, summary)
end

max_stars_repo_name: pnieters/GeneralizedDynamicsFromData

max_issues_repo_name: pnieters/GeneralizedDynamicsFromData

max_forks_repo_name: pnieters/GeneralizedDynamicsFromData


text:
include("./util.jl")

depths = map(l -> parse(Int, l), util.input_lines(1))

changes = depths[2:end] - depths[1:end-1]

num_increases = sum(map(d -> d > 0, changes))

println("Part 1: $num_increases")

windows = []

for i = 1:length(depths)
    if i + 3 <= length(depths)
        push!(windows, depths[i:i+3])
    end
end

window_sums = map(sum, windows)
window_changes = window_sums[2:end] - window_sums[1:end-1]
num_window_increases = sum(map(d -> d > 0, window_changes))

println("Part 2: $num_window_increases")


text:
__precompile__()

module TextParse

using DataValues
using Base.Test

include("lib/compat.jl")
include("util.jl")
include("field.jl")
include("record.jl")

include("guesstype.jl")
include("csv.jl")

end # module


text:
# for demo only.

export equal2, multiply2

# equality of 2 matrices
function equal2(A::AbstractMatrix, B::AbstractMatrix)
    A === B && return true
    size(A) != size(B) && return false
    for (i, j, v) in nziterator(A)
        v == B[i,j] || return false
    end
    for (i, j, v) in nziterator(B)
        v == A[i,j] || return false
    end
    true
end

# multiplication
function multiply2(A::AbstractMatrix{T}, B::Union{AbstractVector{T},AbstractMatrix{T}}) where T<:Number
    m, n = size(A)
    size(B, 1) == n || throw(DimensionMismatch("matrix A has dimensions $(size(A)), vector B has length $(length(B))"))

    v = zeros(T, m, size(B)[2:end]...)
    for (i, j, aij) in nziterator(A)
        v[i,:] += B[j,:] * aij
    end
    v
end

# for comparison: multiplication of a Symmetirc(A, :U)
function multiply3(A::Symmetric{T,<:SparseMatrixCSC}, B::Union{AbstractVector{T},AbstractMatrix{T}}) where T<:Number

    m, n = size(A)
    size(B, 1) == n || throw(DimensionMismatch("matrix A has dimensions $(size(A)), vector B has length $(length(B))"))
    colptr = A.data.colptr
    rowval = A.data.rowval
    nzval = A.data.nzval
    v = zeros(T, m, size(B)[2:end]...)

    for j = 1:n, k = colptr[j]:colptr[j+1]-1
        i = rowval[k]
        if i <= j
            aij = nzval[k]
            v[i,:] += B[j,:] * aij
            if i < j
                v[j,:] += B[i,:] * aij
            end
        end
    end
    v
end

# converting to a SparseMatrixCSC
function sparse2(A::AbstractMatrix{T}) where T<:Number

    Ti = Int
    m, n = size(A)
    I = Ti[]
    J = Ti[]
    Z = T[]
    colptr = zeros(Ti, n+1)
    colptr[1] = 1
    for (i, j, z) in nziterator(A)
        push!(I, i)
        push!(J, j)
        push!(Z, z)
        colptr[j+1] += 1
    end
    cumsum!(colptr, colptr)
    p = collect(1:length(I))
    p = sortperm2!(p, J, I)
    SparseMatrixCSC{T,Ti}(m, n, colptr, I[p], Z[p])
end

# sorting two integer vectors
function sortperm2!(p::Vector{Int}, v1::Vector{T}, v2::Vector{T}) where T<:Integer
    n = length(v1)
    n == length(v2) || throw(DimensionMismatch("vectors need equal lengths"))
    function lt(i::Int, j::Int)
        @inbounds begin v1[i] < v1[j] || v1[i] === v1[j] &&  v2[i] < v2[j] end
    end
    sort!(p, lt=lt)
    p
end