text:
immutable HomotheticQuasiHyperbolic{T} <: Model
    a::T
    β::T
    δ::T
end

function Base.show(io::IO, ::Type{HomotheticQuasiHyperbolic})
    return print(io, "Homothetic Quasi-Hyperbolic")
end

function Base.show(io::IO, m::HomotheticQuasiHyperbolic)
    @printf(
        io,
        "Homothetic Hyperboloid (a = %.3f, β = %.3f, δ = %.3f)",
        m.a,
        m.β,
        m.δ,
    )
end

function initial_parameters(::Type{HomotheticQuasiHyperbolic})
    return Float64[log(1.0), logit(0.5), logit(0.5)]
end

canonical_parameters(m::HomotheticQuasiHyperbolic) = (m.a, m.β, m.δ)

function HomotheticQuasiHyperbolic(parameters::Vector)
    a = exp(parameters[1])
    β = invlogit(parameters[2])
    δ = invlogit(parameters[3])
    return HomotheticQuasiHyperbolic(a, β, δ)
end

@inline function predict(
    m::HomotheticQuasiHyperbolic,
    x2::Real,
    t2::Real,
    x1::Real,
    t1::Real,
)
    a, β, δ = m.a, m.β, m.δ
    u1 = log(x1 * ifelse(t1 == 0.0, 1.0, β) * δ^t1)
    u2 = log(x2 * ifelse(t2 == 0.0, 1.0, β) * δ^t2)
    z = a * (u2 - u1)
    p = invlogit(z)
    return p
end

@inline function gradient_component(
    m::HomotheticQuasiHyperbolic,
    x2::Real,
    t2::Real,
    x1::Real,
    t1::Real,
)
    a, β, δ = m.a, m.β, m.δ
    βt2 = ifelse(t2 == 0.0, 1.0, β)
    βt1 = ifelse(t1 == 0.0, 1.0, β)
    Δβt2 = ifelse(t2 == 0.0, 0.0, 1.0)
    Δβt1 = ifelse(t1 == 0.0, 0.0, 1.0)
    gr = (
        (log(x2 * βt2 * δ^t2) - log(x1 * βt1 * δ^t1)) * a,
        a * (Δβt2 / βt2 - Δβt1 / βt1) * β * (1 - β),
        a * ((t2 - t1) / δ) * δ  * (1 - δ),
    )
    return gr
end


text:
append!(empty!(LOAD_PATH), Base.DEFAULT_LOAD_PATH)
using Pkg

################################################################################
# Generate notebooks
################################################################################
exampledir = joinpath(@__DIR__, "..", "examples")
Pkg.activate(exampledir)
Pkg.instantiate()
include(joinpath(exampledir, "generate_notebooks.jl"))

################################################################################
# Build simulation environments
################################################################################
pkgdir = joinpath(@__DIR__, "..")
Pkg.activate(pkgdir)

using JLD2
using Symbolics
using LinearAlgebra
using Scratch
using RoboDojo
using Rotations
import RoboDojo: Model, lagrangian_derivatives, IndicesZ, cone_product

# acrobot
include("../src/models/acrobot/model.jl")
include("../src/models/acrobot/simulator_impact.jl")
include("../src/models/acrobot/simulator_nominal.jl")
include("../src/models/acrobot/codegen.jl")

# cartpole
include("../src/models/cartpole/model.jl")
include("../src/models/cartpole/simulator_friction.jl")
include("../src/models/cartpole/simulator_frictionless.jl")
include("../src/models/cartpole/codegen.jl")

# hopper from RoboDojo.jl

# planar push
include("../src/models/planar_push/model.jl")
include("../src/models/planar_push/simulator.jl")
include("../src/models/planar_push/codegen.jl")

# rocket
include("../src/models/rocket/model.jl")
include("../src/models/rocket/simulator.jl")
include("../src/models/rocket/codegen.jl")

ext: jl


text:
module TextMining

using Clustering
using TextAnalysis
using MAT
using Grid
using PyPlot

export PrepQueriesCorpus,PrepDocCorpus,PreProcess!,PrepTest
export SVDModel,VSMModel,TrySVD
export KMeansModel
export plotNew_RecPrec,plotAdd_RecPrec,plot_DrDtNr
export Results, NewDataCorpus, NewDataMatrix, Normalize, CosTheta, FindResults

include("Preparation.jl")
include("utils.jl")
include("AverageRP.jl")
include("PlotResults.jl")
include("VecSpaceModel.jl")
include("LSIModel.jl")
include("KMeansModel.jl")

end


text:
# Profiler control

"""
    @profile ex

Run expressions while activating the CUDA profiler.

Note that this API is used to programmatically control the profiling granularity by allowing
profiling to be done only on selective pieces of code. It does not perform any profiling on
itself, you need external tools for that.
"""
macro profile(ex)
    quote
        Profile.start()
        local ret = $(esc(ex))
        Profile.stop()
        ret
    end
end

module Profile

using ..CUDA

const nsight = Ref{Union{Nothing,String}}(nothing)

"""
    start()

Enables profile collection by the active profiling tool for the current context. If
profiling is already enabled, then this call has no effect.
"""
function start()
    if nsight[] !== nothing
        run(`$(nsight[]) start`)
    else
        @warn("""Calling CUDA.@profile only informs an external profiler to start.
                 The user is responsible for launching Julia under a CUDA profiler like `nvprof`.

                 For improved usability, launch Julia under the Nsight Systems profiler:
                 \$ nsys launch julia""",
              maxlog=1)
    end
    CUDA.cuProfilerStart()
end

"""
    stop()

Disables profile collection by the active profiling tool for the current context. If
profiling is already disabled, then this call has no effect.
"""
function stop()
    if nsight[] !== nothing
        run(`$(nsight[]) stop`)
        @info "Profiling has finished, open the report listed above with `nsight-sys`"
    else
        CUDA.cuProfilerStop()
    end
end

if haskey(ENV, "NSYS_PROFILING_SESSION_ID") && ccall(:jl_generating_output, Cint, ()) == 1
    @warn "Precompiling while running under Nsight Systems; if you encounter segmentation faults (exit code 11), try disabling OSRT tracing."
end

function __init__()
    # find the active Nsight Systems profiler
    if haskey(ENV, "NSYS_PROFILING_SESSION_ID") && ccall(:jl_generating_output, Cint, ()) == 0
        nsight[] = if haskey(ENV, "JULIA_CUDA_NSYS")
            ENV["JULIA_CUDA_NSYS"]
        elseif haskey(ENV, "_")
            ENV["_"]
        elseif haskey(ENV, "LD_PRELOAD")
            libraries = split(ENV["LD_PRELOAD"], ':')
            filter!(isfile, libraries)
            directories = map(dirname, libraries)
            candidates = map(dir->joinpath(dir, "nsys"), directories)
            filter!(isfile, candidates)
            isempty(candidates) && error("Could not find nsys relative to LD_PRELOAD=$(ENV["LD_PRELOAD"])")
            first(candidates)
        else
            error("Running under Nsight Systems, but could not find the `nsys` binary to start the profiler. Please specify using JULIA_CUDA_NSYS=path/to/nsys, and file an issue with the contents of ENV.")
        end
        @assert isfile(nsight[])
        @info "Running under Nsight Systems, CUDA.@profile will automatically start the profiler"
    else
        nsight[] = nothing
    end
end

end


text:
"""
    DeepSplitting(nn; opt)

Deep splitting algorithm.

# Arguments
* `nn`: a [Flux.Chain](https://fluxml.ai/Flux.jl/stable/models/layers/#Flux.Chain), or more generally a [functor](https://github.com/FluxML/Functors.jl)
* `opt`: optimiser to be use. By default, `Flux.ADAM(0.1)`.
"""
struct DeepSplitting{NN,F,O,MCS} <: HighDimPDEAlgorithm
    nn::NN
    K::F
    opt::O
    mc_sample!::MCS # Monte Carlo sample
end

function DeepSplitting(nn; K=1, opt=Flux.ADAM(0.1), mc_sample::MCSampling = NoSampling())
    DeepSplitting(nn, K, opt, mc_sample)
end

function solve(
    prob::PIDEProblem,
    alg::DeepSplitting,
    dt;
    batch_size = 1,
    abstol = 1f-6,
    verbose = false,
    maxiters = 300,
    use_cuda = false,
    )
    if use_cuda
        if CUDA.functional()
            @info "Training on CUDA GPU"
            CUDA.allowscalar(false)
            _device = Flux.gpu
        else
            error("CUDA not functional, deactivate `use_cuda` and retry")
        end
    else
        @info "Training on CPU"
        _device = Flux.cpu
    end

    # unbin stuff
    u_domain = prob.u_domain |> _device # domain on which we want to approximate u, nothing if only one point wanted
    neumann_bc = prob.neumann_bc |> _device
    x0 = prob.x |> _device
    mc_sample! =  alg.mc_sample! |> _device

    d  = size(x0,1)
    K = alg.K
    opt = alg.opt
    g,f,μ,σ,p = prob.g,prob.f,prob.μ,prob.σ,prob.p
    T = eltype(x0)

    # neural network model
    nn = alg.nn |> _device
    vi = g
    # fix for deepcopy
    vj = Flux.fmap(nn) do x
        x isa AbstractArray && return copy(x)
        x
      end
    ps = Flux.params(vj)

    # output solution
    if isnothing(u_domain)
        sample_initial_points! = NoSampling()
        usol = [g(x0 |>cpu)[]]
    else
        usol = Any[g]
        sample_initial_points! = UniformSampling(u_domain[1], u_domain[2])
    end

    dt = convert(T,dt)
    ts = prob.tspan[1]:dt-eps(T):prob.tspan[2]
    N = length(ts) - 1

    # allocating
    x0_batch = repeat(x0, 1, batch_size)
    y1 = similar(x0_batch)
    y0 = similar(y1)
    z = similar(x0, d, batch_size, K) # for MC non local integration

    # checking element types
    eltype(mc_sample!) == T || !_integrate(mc_sample!) ? nothing : error("Element type of `mc_sample` not the same as element type of `x`")

    function splitting_model(y0, y1, z, t)
        # TODO: for now hardcoded because of a bug in Zygote differentiation rules for adjoints
        # vi_y1, ∇vi = Zygote.pullback(vi, y1)
        # _int = reshape(sum(f(y1, z, vi_y1, vi(z), ∇vi(y1)[1], ∇vi(z)[1], p, t), dims = 3), 1, :)
        ∇vi(x) = [0f0]
        # Monte Carlo integration
        _int = reshape(sum(f(y1, z, vi(y1), vi(z), ∇vi(y1), ∇vi(z), p, t), dims = 3), 1, :)
        vj(y0) - (vi(y1) + dt * _int / K)
    end

    function loss(y0, y1, z, t)
        u = splitting_model(y0, y1, z, t)
        return sum(u.^2) / batch_size
    end

    # calculating SDE trajectories
    function sde_loop!(y0, y1, dWall)
        randn!(dWall) # points normally distributed for brownian motion
        sample_initial_points!(y1) # points uniformly distributed for initial conditions
        for i in 1:size(dWall,3)
            # @show i
            # not sure about this one
            t = ts[N + 1 - i]
            dW = @view dWall[:,:,i]
            y0 .= y1
            y1 .= y0 .+ μ(y0,p,t) .* dt .+ σ(y0,p,t) .* sqrt(dt) .* dW
            if !isnothing(neumann_bc)
                y1 .= _reflect(y0, y1, neumann_bc[1], neumann_bc[2])
            end
        end
    end

    for net in 1:N
        # preallocate dWall
        dWall = similar(x0, d, batch_size, N + 1 - net) # for SDE

        verbose && println("Step $(net) / $(N) ")
        t = ts[net]

        # @showprogress
        for epoch in 1:maxiters
            y1 .= x0_batch
            # generating sdes
            sde_loop!(y0, y1, dWall)

            if _integrate(mc_sample!)
                # generating z for MC non local integration
                mc_sample!(z, y1)
            end

            # training
            gs = Flux.gradient(ps) do
                loss(y0, y1, z, t)
            end
            Flux.Optimise.update!(opt, ps, gs) # update parameters

            # report on training
            if epoch % 100 == 1
                l = loss(y0, y1, z, t)
                verbose && println("Current loss is: $l")
                l < abstol && break
            end
            if epoch == maxiters
                l = loss(y0, y1, z, t)
                verbose && println("Current loss is: $l")
            end
        end

        # saving
        # fix for deepcopy
        vi = Flux.fmap(vj) do x
            x isa AbstractArray && return copy(x)
            x
          end
        # vj = deepcopy(nn)
        # ps = Flux.params(vj)
        if isnothing(u_domain)
            # reshape used in the case where there are some normalisation in layers
            push!(usol, cpu(vi(reshape(x0, d, 1)))[] )
        else
            push!(usol, vi |> cpu)
        end
    end

    # return
    if isnothing(u_domain)
        # sol = DiffEqBase.build_solution(prob, alg, ts, usol)
        x0 = x0 |> cpu
        sol = x0, ts, usol
    else
        sample_initial_points!(y1)
        xgrid = [reshape(y1[:,i], d, 1) for i in 1:size(y1,2)] .|> cpu #reshape needed for batch size
        sol = xgrid, ts, usol
    end
    return sol
end


text:
using ConfigLH
using Pkg, Test

@testset "ConfigLH" begin
	@testset "Computer" begin
		currentComp = ConfigLH.current_computer()
		@test currentComp == ConfigLH.ComputerLocal

		ci = ConfigLH.computer_info(currentComp)
		@test ci.runLocal
		@test isdir(ci.baseDir)
		ci = ConfigLH.computer_info()
		@test ci.runLocal

		@test isdir(ConfigLH.docu_dir(currentComp))
		@test isdir(ConfigLH.docu_dir())

		@test ConfigLH.running_local()

		@test isdir(ConfigLH.shared_dir())
		@test isdir(ConfigLH.julia_dir())

		@test isdir(ConfigLH.project_dir(2018))
	end

	@testset "Directories" begin
		newStr = "remove/this"
		add_to_path!(newStr)
		@test in(newStr, LOAD_PATH)
		remove_from_path!(newStr);
		@test !in(newStr, LOAD_PATH)
		n = length(LOAD_PATH)
		remove_from_path!(newStr);
		@test length(LOAD_PATH) == n
	end

	@testset "Github" begin
		ps = ConfigLH.get_pkg_spec("ConfigLH");
		@test isa(ps, Pkg.Types.PackageSpec)
	end

	include("project_test.jl")
end

# ----------


text:
using DynamicalSystems
using LinearAlgebra
using Roots

signed_plane_distance(point, plane_base, plane_normal) = dot(point - plane_base, plane_normal)

function poincare_section_from_function(f, t0, tmax, bisections, plane_base, plane_normal)
    # the "sign" of the normal vector determines the direction of the poincare section recored
    plane_normal = plane_normal / norm(plane_normal) # make sure that the plane normal is a unit vector
    # the function which computes the distance of the orbit at time t to the poincare surface
    root_fn(t) = signed_plane_distance(f(t), plane_base, plane_normal)
    # the first point on the orbit
    dt = (tmax - t0) / bisections # size of each bisection interval
    t = t0
    p0 = f(t)
    spd = signed_plane_distance(p0, plane_base, plane_normal)
    ps = Dataset{length(p0), typeof(p0[1])}() # dataset to collect the points in the psos
    # iterate of each bisection interval
    for i=1:bisections
        # compute the next point in the orbit
        next_t = i*dt + t0
        next_spd = signed_plane_distance(f(next_t), plane_base, plane_normal)
        # do rootfinding in an interval if the sign of the plane distance is different on the end points
        if sign(spd) > sign(next_spd)
            t_root = find_zero(root_fn, (t, next_t), Roots.A42())
            p = f(t_root)
            push!(ps, p) # add the point on the poincare surface to the poincare section
        end
        # continue with the next interval
        spd = next_spd
        t = next_t
    end
    return ps
end

max_stars_repo_name: cosmo-jana/numerics-physics-stuff

max_issues_repo_name: cosmo-jana/numerics-physics-stuff

max_forks_repo_name: cosmo-jana/numerics-physics-stuff


text:
function dijkstra(G::DictGraph; source::Int = 0)
    Q = PriorityQueue{Int,Real}(v => typemax(Int) for v in vertices(G))
    D = SortedDict{Int, Array{Real, 1}}(v => [typemax(Int), -1, 1] for v in vertices(G))

    Q[source] = 0
    D[source][1] = 0

    function relax(u,v,w)
        if D[v][1] > D[u][1] + w
            D[v][1] = D[u][1] + w; Q[v] = D[u][1] + w;
            D[v][3] = w
            D[v][2] = u
        end
    end

    while !isempty(Q)
        u = dequeue_pair!(Q)
        for ℒ in G[u.first]
            relax(u.first, ℒ.u, ℒ.w)
        end
    end

    return D
end


text:
"""
    @reset_parallel_kernel()

Reset the ParallelKernel module.

See also: [`init_parallel_kernel`](@ref)
"""
macro reset_parallel_kernel() esc(reset_parallel_kernel(__module__)) end

function reset_parallel_kernel(caller::Module)
    if isdefined(caller, :Data) && isdefined(caller.Data, :DeviceArray) # "Clear" the Data module if it has been created by ParallelKernel (i.e. contains Data.DeviceArray).
        data_module = Data_none()
        @eval(caller, $data_module)
    end
    set_initialized(false)
    set_package(PKG_NONE)
    set_numbertype(NUMBERTYPE_NONE)
    return nothing
end


text:
function TriMesh_SquareMesh(x_l::Float64, x_r::Float64,
                            y_l::Float64, y_r::Float64,
                            etype::String,
                            n_nodesx::Int64, n_nodesy::Int64)
#= """
  TriMesh_SquareMesh.m - Generate a rectangular mesh with a prescribed density.
               This routine returns nodal coordinates, element connectivity,
               and the nodal indices of exterior nodes to assist in the
               specification of boundary conditions..

  Author: Jeff Borggaard, Virginia Tech
          part of FEMfunctions.jl

  Licensing:
     This code is distributed under the MIT license.

  Usage:
  ```julia
    x, e_conn, index_b = TriMesh_SquareMesh(x_l,x_r,y_l,y_r,etype,n_nodex,n_nodesy)
  ```

  Arguments:
  - `(x_l,y_l)`: Coordinates of lower left corner
  - `(x_r,y_r)`: Coordinates of upper right corner
  - `etype`: Element type,  'linear', 'quadratic', 'cubic'
  - `n_nodesx, n_nodesy`: Number of nodes in x and y directions
                          (must be compatible with element type)

  Output arguments:
  - `x`: Nodal coordinates of mesh
  - `e_conn`: Element connectivity of mesh
  - `index_b`: Node numbers of boundary nodes
""" =#

  #  Generate node coordinates
  nNodes = n_nodesx*n_nodesy
  x = zeros(Float64,nNodes,2)
  dx = (x_r-x_l)/(n_nodesx-1)
  dy = (y_r-y_l)/(n_nodesy-1)
  for i=1:n_nodesx
    for j=1:n_nodesy
      k = i+(j-1)*n_nodesx
      x[k,1] = x_l + dx*(i-1)
      x[k,2] = y_l + dy*(j-1)
    end
  end

  #  Generate element connectivity
  if etype == "linear"
    n_elements = 2*(n_nodesx-1)*(n_nodesy-1)

    global e_conn = zeros(Int64, n_elements, 3)
    ie     = 0
    for j=1:n_nodesy-1
      for i=1:n_nodesx-1
        ie = ie + 1
        k = i + (j-1)*n_nodesx
        e_conn[ie,:] = [k, k+1+n_nodesx, k+n_nodesx]
        ie = ie + 1
        e_conn[ie,:] = [k, k+1, k+1+n_nodesx]
      end
    end

  elseif etype == "quadratic"
    n_elements = convert(Int,2*(n_nodesx-1)*(n_nodesy-1)/4)

    e_conn = zeros(Int64, n_elements, 6)
    ie     = 0
    for j=1:2:n_nodesy-1
      for i=1:2:n_nodesx-1
        ie = ie + 1
        k = i + (j-1)*n_nodesx
        e_conn[ie,:] = [k, k+2+2*n_nodesx, k+2*n_nodesx,
                        k + 1 + n_nodesx, k+1+2*n_nodesx, k+n_nodesx ]
        ie = ie + 1
        e_conn[ie,:] = [k, k+2, k+2+2*n_nodesx,
                        k+1, k+2+n_nodesx, k+1+n_nodesx ]
      end
    end

  elseif etype == "cubic"
    error("twod_mesh: cubic elements are not currently implemented")
    return
  else
    error("twod_mesh: etype is not a valid string")
    return
  end

  index_b = [ 2:n_nodesx-1
              1:n_nodesx:(n_nodesy-1)*n_nodesx
              n_nodesx:n_nodesx:n_nodesy*n_nodesx
              (n_nodesy-1)*n_nodesx+1:nNodes-1]

  return x, e_conn, index_b

end


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

struct VirtualNetworkPeeringsApi <: SwaggerApi
    client::Swagger.Client
end

"""

Creates or updates a peering in the specified virtual network.
Param: resourceGroupName::String (required)
Param: virtualNetworkName::String (required)
Param: virtualNetworkPeeringName::String (required)
Param: VirtualNetworkPeeringParameters::VirtualNetworkPeering (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: VirtualNetworkPeering
"""
function _swaggerinternal_virtualNetworkPeeringsCreateOrUpdate(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, VirtualNetworkPeeringParameters, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "PUT", VirtualNetworkPeering, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings/{virtualNetworkPeeringName}", ["azure_auth"], VirtualNetworkPeeringParameters)
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkName", virtualNetworkName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkPeeringName", virtualNetworkPeeringName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function virtualNetworkPeeringsCreateOrUpdate(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, VirtualNetworkPeeringParameters, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsCreateOrUpdate(_api, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, VirtualNetworkPeeringParameters, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function virtualNetworkPeeringsCreateOrUpdate(_api::VirtualNetworkPeeringsApi, response_stream::Channel, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, VirtualNetworkPeeringParameters, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsCreateOrUpdate(_api, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, VirtualNetworkPeeringParameters, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

"""

Deletes the specified virtual network peering.
Param: resourceGroupName::String (required)
Param: virtualNetworkName::String (required)
Param: virtualNetworkPeeringName::String (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: Nothing
"""
function _swaggerinternal_virtualNetworkPeeringsDelete(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "DELETE", Nothing, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings/{virtualNetworkPeeringName}", ["azure_auth"])
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkName", virtualNetworkName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkPeeringName", virtualNetworkPeeringName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function virtualNetworkPeeringsDelete(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsDelete(_api, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function virtualNetworkPeeringsDelete(_api::VirtualNetworkPeeringsApi, response_stream::Channel, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsDelete(_api, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

"""

Gets the specified virtual network peering.
Param: resourceGroupName::String (required)
Param: virtualNetworkName::String (required)
Param: virtualNetworkPeeringName::String (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: VirtualNetworkPeering
"""
function _swaggerinternal_virtualNetworkPeeringsGet(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", VirtualNetworkPeering, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings/{virtualNetworkPeeringName}", ["azure_auth"])
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkName", virtualNetworkName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkPeeringName", virtualNetworkPeeringName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function virtualNetworkPeeringsGet(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsGet(_api, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function virtualNetworkPeeringsGet(_api::VirtualNetworkPeeringsApi, response_stream::Channel, resourceGroupName::String, virtualNetworkName::String, virtualNetworkPeeringName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsGet(_api, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

"""

Gets all virtual network peerings in a virtual network.
Param: resourceGroupName::String (required)
Param: virtualNetworkName::String (required)
Param: api_version::String (required)
Param: subscriptionId::String (required)
Return: VirtualNetworkPeeringListResult
"""
function _swaggerinternal_virtualNetworkPeeringsList(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", VirtualNetworkPeeringListResult, "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings", ["azure_auth"])
    Swagger.set_param(_ctx.path, "resourceGroupName", resourceGroupName)  # type String
    Swagger.set_param(_ctx.path, "virtualNetworkName", virtualNetworkName)  # type String
    Swagger.set_param(_ctx.path, "subscriptionId", subscriptionId)  # type String
    Swagger.set_param(_ctx.query, "api-version", api_version)  # type String
    Swagger.set_header_accept(_ctx, ["application/json"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json"] : [_mediaType])
    return _ctx
end

function virtualNetworkPeeringsList(_api::VirtualNetworkPeeringsApi, resourceGroupName::String, virtualNetworkName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsList(_api, resourceGroupName, virtualNetworkName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function virtualNetworkPeeringsList(_api::VirtualNetworkPeeringsApi, response_stream::Channel, resourceGroupName::String, virtualNetworkName::String, api_version::String, subscriptionId::String; _mediaType=nothing)
    _ctx = _swaggerinternal_virtualNetworkPeeringsList(_api, resourceGroupName, virtualNetworkName, api_version, subscriptionId; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

export virtualNetworkPeeringsCreateOrUpdate, virtualNetworkPeeringsDelete, virtualNetworkPeeringsGet, virtualNetworkPeeringsList


text:
"""
Iterator pattern in Julia is easy to use.
We just have to define two things here: an Iterable type and its interface `iterate`.
The type stores necessary information for iteration.
Each time you call `iterate`, it gives you a tuple (element, state).
Element is the thing you will throw out to user.
State is a counter to inidicate corresponding element.
`iterate` should output the tuple (element, state).

Using Iterable in for-loop context will be:

for element in iterable
    # body
end

It will further translate into more detailed syntax:

iter_result = iterate(iterable)
while iter_result !== nothing
    (element, state) = iter_result
    # body
    iter_result = iterate(iterable, state)
end

Heavily referenced from: https://invenia.github.io/blog/2018/07/06/iteratorsinjulia07/

We have two examples here.
"""

struct AddOne
    start::Int
    length::Int
end

function Base.iterate(iter::AddOne, state=(iter.start, 0))
    element, count = state

    if count >= iter.length
       return nothing
    end

    return (element, (element + 1, count + 1))
end

struct ArrayIterator
    array::Vector
    start::Int
    length::Int
end

function Base.iterate(iter::ArrayIterator, state=(iter.array[iter.start], iter.start))
    element, count = state

    if count - iter.start >= iter.length
       return nothing
    end

    return (element, (iter.array[count + 1], count + 1))
end


text:
export body!, content!, loadcss!, loadjs!, load!, importhtml!

function content!(o, sel, html::AbstractString; fade = true, async = true)
  if async
    @js_(o, Blink.fill($sel, $html, $fade, null, null))
  else
    @js o begin  # Use `@js` to wait until the below Promise is resolved.
      @new Promise(function (resolve, reject)
        Blink.fill($sel, $html, $fade, resolve, reject)
      end)
    end
    return o  # But still return `o` for chaining.
  end
end

content!(o, sel, html; fade = true, async = true) =
  content!(o, sel, stringmime(MIME"text/html"(), html), fade=fade, async=async)

body!(w, html; fade = true, async = true) = content!(w, "body", html, fade=fade, async=async)

function loadcss!(w, url)
  @js_ w begin
    @var link = document.createElement("link")
    link.type = "text/css"
    link.rel = "stylesheet"
    link.href = $url
    document.head.appendChild(link)
  end
end

function importhtml!(w, url; async=false)
  if async
    @js_ w begin
      @var link = document.createElement("link")
      link.rel = "import"
      link.href = $url
      document.head.appendChild(link)
    end
  else
    @js w begin
      @new Promise(function (resolve, reject)
          @var link = document.createElement("link")
          link.rel = "import"
          link.href = $url
          link.onload = (e) -> resolve(true)
          link.onerror = (e) -> reject(false)
          document.head.appendChild(link)
      end)
    end
  end
end

function loadjs!(w, url)
  @js w @new Promise(function (resolve, reject)
    @var script = document.createElement("script")
    script.src = $url
    script.onload = resolve
    script.onerror = (e) -> reject(
                               Dict("name"=>"JSLoadError",
                                    "message"=>"failed to load " + this.src)
                                    )
    document.head.appendChild(script)
  end)
end

isurl(f) = occursin(r"^https?://", f)

function load!(w, file; async=false)
  if !isurl(file)
    resource(file)
    file = basename(file)
  end
  ext = Mux.extension(file)
  if ext == "js"
    loadjs!(w, file)
  elseif ext == "css"
    loadcss!(w, file)
  elseif ext == "html"
    importhtml!(w, file; async=async)
  else
    error("Blink: Unsupported file type")
  end
end


text:
struct RaoBlackwellEstimator <: Estimator
end

string(estimator::RaoBlackwellEstimator) = "RaoBlackwellEstimator"

function (estimator::RaoBlackwellEstimator)(x1::TI, x2::TI, design::TD) where {TI<:Integer,TD<:AbstractDesign}
    x = x1 + x2
    xx1   = 0:n1(design)
    xx1   = xx1[n2.(design, xx1) .== n2(design, x1)]
    nom   = sum( binomial.(n1(design) .- 1, xx1 .- 1) .* binomial.(n2.(design, xx1), x .- xx1) )
    denom = sum( binomial.(n1(design), xx1) .* binomial.(n2.(design, xx1), x .- xx1) )
    return nom / denom
end


text:
# This file is a part of Julia. License is MIT: https://julialang.org/license

ndgrid(v::AbstractVector) = copy(v)

function ndgrid(v1::AbstractVector{T}, v2::AbstractVector{T}) where T
    m, n = length(v1), length(v2)
    v1 = reshape(v1, m, 1)
    v2 = reshape(v2, 1, n)
    (repmat(v1, 1, n), repmat(v2, m, 1))
end

function ndgrid_fill(a, v, s, snext)
    for j = 1:length(a)
        a[j] = v[div(rem(j-1, snext), s)+1]
    end
end

function ndgrid(vs::AbstractVector{T}...) where T
    n = length(vs)
    sz = map(length, vs)
    out = ntuple(i->Array{T}(sz), n)
    s = 1
    for i=1:n
        a = out[i]::Array
        v = vs[i]
        snext = s*size(a,i)
        ndgrid_fill(a, v, s, snext)
        s = snext
    end
    out
end

meshgrid(v::AbstractVector) = meshgrid(v, v)

function meshgrid(vx::AbstractVector{T}, vy::AbstractVector{T}) where T
    m, n = length(vy), length(vx)
    vx = reshape(vx, 1, n)
    vy = reshape(vy, m, 1)
    (repmat(vx, m, 1), repmat(vy, 1, n))
end

function meshgrid(vx::AbstractVector{T}, vy::AbstractVector{T},
                  vz::AbstractVector{T}) where T
    m, n, o = length(vy), length(vx), length(vz)
    vx = reshape(vx, 1, n, 1)
    vy = reshape(vy, m, 1, 1)
    vz = reshape(vz, 1, 1, o)
    om = ones(Int, m)
    on = ones(Int, n)
    oo = ones(Int, o)
    (vx[om, :, oo], vy[:, on, oo], vz[om, on, :])
end

max_stars_repo_name: caodi/julia

max_issues_repo_name: caodi/julia

max_forks_repo_name: caodi/julia


text:
module ReducedFormTests

include(joinpath(dirname(@__FILE__), "..", "src", "AndersonMoore.jl"))
# test reducedForm
using .AndersonMoore

#tweaked= True
# test reducedForm firmvalue example
function firmvalueTrue()::Bool

neq=4::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.2285714285714295      0.0  2.2131318746159373e-15
          0.0  0.40000000000000024     0.0  0.0
          0.0  1.5314075638926047e-16  0.0  0.0
          0.0  1.531407563892605e-16   0.0  1.0]

    qFull = [0.0   0.17457431218879393   0.0   0.4364357804719848    5.551115123125783e-16   -0.4364357804719848  0.0                 -0.4364357804719848
             0.0   0.2309401076758503    0.0   0.2113248654051871   -1.1102230246251565e-16  -0.5773502691896257  1.3660254037844386  -0.2113248654051871
             0.0   0.2309401076758503    0.0  -0.7886751345948129   -1.1102230246251565e-16  -0.5773502691896257  0.3660254037844386   0.7886751345948129
             0.0  -0.042021536303409414  0.0  -0.10505384075852343  -0.8089145738406297       0.5672907400960278  0.0                  0.10505384075852522]

nonsing=true::Bool

qRows=4::Int64

qCols=8::Int64

bCols=4::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= True
# test reducedForm firmvalue3Leads2Lags example
function firmvalue3Leads2LagsTrue()::Bool

neq=4::Int64;nlag=2::Int64;nlead=3::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0   0.03748035941322163    0.0  0.0  0.0  0.31178973986873604     0.0   1.4078100382803878e-15
          0.0   0.4                    0.0  0.0  0.0  1.8429478256727236e-18  0.0  -2.1575528188428242e-17
          0.0   5.948141085865766e-18  0.0  0.0  0.0  2.378039643817009e-17   0.0  -1.0787764094214123e-17
          0.0  -2.373363479909904e-19  0.0  0.0  0.0  1.4390293975577504e-18  0.0   0.9999999999999999]

    qFull = [0.0  -0.3674571928024606    0.0  0.0  0.0   0.0                   0.0   0.08135701799384851    0.0                      0.9186429820061515    -1.0  -0.08135701799384851    0.0                      5.551115123125783e-17    0.0   0.0                     0.0                     0.0                     0.0                  0.0
             0.0   0.0325428071975394    0.0  0.0  0.0   0.0                   0.0   0.08135701799384851   -1.1102230246251565e-16  -0.08135701799384851    1.0  -0.08135701799384851    0.0                      2.7755575615628914e-17   0.0   0.0                     0.0                     0.0                     0.0                  0.0
             0.0   0.0325428071975394    0.0  0.0  0.0   0.0                   0.0  -0.9186429820061515     0.0                     -0.08135701799384851    0.0   0.9186429820061515     0.0                      0.0                      0.0   0.0                     0.0                     0.0                     0.0                  0.0
             0.0   0.0                   0.0  0.0  0.0  -0.3674571928024606    0.0   0.0                    0.0                      0.0                    0.0   0.08135701799384851    0.0                      0.9186429820061515      -1.0  -0.08135701799384851     0.0                     5.551115123125783e-17   0.0                  0.0
             0.0   0.0                   0.0  0.0  0.0   0.0325428071975394    0.0   0.0                    0.0                      0.0                    0.0   0.08135701799384851   -1.1102230246251565e-16  -0.08135701799384851      1.0  -0.08135701799384851     0.0                     2.7755575615628914e-17  0.0                  0.0
             0.0   0.0                   0.0  0.0  0.0   0.0325428071975394    0.0   0.0                    0.0                      0.0                    0.0  -0.9186429820061515     0.0                     -0.08135701799384851      0.0   0.9186429820061515      0.0                     0.0                     0.0                  0.0
             0.0   0.0                   0.0  0.0  0.0   0.0                   0.0   0.0                    0.0                      0.3141101189565458     0.0   0.0                    0.0                      0.0                      0.0  -0.10915189360855129     4.965068306494546e-17  -0.7852752973913645      0.4472135954999578   0.10915189360855129
             0.0   0.0                   0.0  0.0  0.0   0.0                   0.0   0.0                    0.0                      0.19343902401445667    0.0   0.0                    0.0                      0.0                      0.0   0.03638396453618376    -9.930136612989092e-17  -0.4835975600361417      1.3416407864998738  -0.03638396453618376
             0.0   0.0                   0.0  0.0  0.0   0.0                   0.0   0.0                    0.0                      0.0325428071975394     0.0   0.0                    0.0                      0.0                      0.0  -0.9186429820061515      0.0                    -0.08135701799384851     0.0                  0.9186429820061515
             0.0   0.01872715567480699   0.0  0.0  0.0   0.01702468697709727   0.0   0.04681788918701744    0.4362022735554419      -0.031340901026019906   0.0  -0.004256171744274398   0.3965475214140374      -0.5321265586946402       0.0  -0.0038692470402495885   0.36049774674003404    -0.483751416995128       0.0                 -0.03869247040249525
             0.0  -0.024055173657485922  0.0  0.0  0.0   0.029872707553247335  0.0  -0.060137934143714704  -0.5603051324169901       0.05286123608496593    0.0   0.13481970302683288    0.6958100406840118       0.3497097030263232       0.0  -0.0928735140299901     -0.1694924895334065     -0.3770399993461673      0.0                  0.018191745146872003
             0.0  -0.024055173657485922  0.0  0.0  0.0  -0.008004367864623767  0.0  -0.06013793414371476   -0.5603051324169901       0.08729494101030333    0.0   0.04012701448215549   -0.18644173848674683      0.27700012807861196      0.0   0.08790343682803019     0.6325545824400094      0.14941583884672993     0.0                 -0.06789251716647093]

nonsing=true::Bool

qRows=12::Int64

qCols=20::Int64

bCols=8::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= True
# test reducedForm example7 example
function example7True()::Bool

neq=4::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.0  0.0  -0.6551413498221798
 0.0  0.0  0.0  -0.2945196982393082
 0.0  0.0  0.0  -0.663971668063239
 0.0  0.0  0.0   0.3360283319367609]

    qFull = [0.0  0.0  0.0   0.9714485500374334     0.0                   0.09237233811418602   0.9160251471689218    -0.9999999999999999
 0.0  0.0  0.0  -0.6885514499625665     0.0                  -1.007627661885814    -0.08397485283107814    1.0
 0.0  0.0  0.0   0.020704809034346315  -0.12636048681123602   0.6391791991927751    0.06089649715984197    0.3725746100511591
 0.0  0.0  0.0   0.004472451929227917  -0.6787380506530568    0.6391791991927751    0.013154270380082114  -0.7504036503198326]

nonsing=true::Bool

qRows=4::Int64

qCols=8::Int64

bCols=4::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= True
# test reducedForm oneEquationNoLead example
function oneEquationNoLeadTrue()::Bool

neq=1::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [-0.6666666666666666]

    qFull = [2.0  3.0]

nonsing=true::Bool

qRows=1::Int64

qCols=2::Int64

bCols=1::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= True
# test reducedForm reliablePaperExmpl example
function reliablePaperExmplTrue()::Bool

neq=5::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.0  0.0  1.1828179593111052e-16  0.0
          0.0  0.0  0.0  1.3794162794530762e-16  0.0
          0.0  0.0  0.0  0.21410138764106415     0.0
          0.0  0.0  0.0  0.3612359521871584      0.0
          0.0  0.0  0.0  0.5307470047448346      0.0]

    qFull = [0.0  0.0  0.0   0.3969847178042136    0.0  -0.1850509058906912    0.6089185297177356      -0.5557786049258994    0.39698471780421407  -0.7939694356084274
             0.0  0.0  0.0   0.17185720889240513   0.0   0.9076903413717948    0.251404759156605       -0.24060009244936742   0.17185720889240524  -0.3437144177848103
             0.0  0.0  0.0   0.17185720889240513   0.0  -0.09230965862820517   1.251404759156605       -0.24060009244936742   0.17185720889240524  -0.3437144177848103
             0.0  0.0  0.0  -0.05631323026787628   0.0  -0.4286629858284091   -3.6703258090922634e-16   0.6201279637550784    0.4662994218464173   -0.46142727279236506
             0.0  0.0  0.0  -0.013375768528939336  0.0  -0.5746503523310461    3.09255399040231e-16     0.6201279637550784   -0.5168413473817807    0.12681595303106827]

nonsing=true::Bool

qRows=5::Int64

qCols=10::Int64

bCols=5::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= True
# test reducedForm athan example
function athanTrue()::Bool

neq=9::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.8285973383130347       0.29644852356208445     -0.11369033691582552      0.2123317657422606      0.0  0.0  0.0  0.0   2.1935483080274293e-16
          0.293                    0.764                   -0.293                   -1.646036600311629e-16   0.0  0.0  0.0  0.0  -2.339515921254561e-32
          1.9501946766260694       1.3568970471241695      -0.5203806738316512      -0.4863364685154788      0.0  0.0  0.0  0.0   4.3870966160548577e-16
          -8.786979196790518e-17   -1.0192232643901078e-16   5.148028650420445e-17    0.9109999999999998      0.0  0.0  0.0  0.0   0.0
          -1.593244282256827e-17   -1.236660659626695e-17    1.0534664365541054e-17  -7.220004517462003e-17   0.0  0.0  0.0  0.0   0.0
          -4.6504144201620185e-17  -4.8878343919176405e-17   2.5345529016143423e-17  -9.103476345289971e-17   0.0  0.0  0.0  0.0   0.0
          -6.617826770686551e-17   -7.45388658530095e-17     3.518654070019197e-17   -9.103476345289971e-17   0.0  0.0  0.0  0.0   0.0
          -8.932183816406436e-17   -1.0019938778684248e-16   4.502755238424049e-17   -1.098694817311794e-16   0.0  0.0  0.0  0.0   0.0
          -1.1246540862126324e-16  -1.2585990972067555e-16   5.4868564068289023e-17  -1.1680837563508664e-16  0.0  0.0  0.0  0.0   1.0 ]

    qFull = [-0.2775204536090702    -0.7236369507076094     0.27752045360907       0.04812923809603137  0.0  0.0  0.0  0.0   0.05283121635129678   0.10566243270259362   1.0                     -0.05283121635129678  -0.10566243270259355  -0.052831216351296846  -1.0  -0.15849364905389032   0.0  -0.05283121635129678
             0.015479546390929903   0.040363049292390735  -0.015479546390929955   0.04812923809603137  0.0  0.0  0.0  0.0   0.05283121635129678  -1.894337567297406    -1.0000000000000007       0.9471687836487033    0.8943375672974065   -0.0528312163512969     0.0   1.8415063509461098    0.0  -0.05283121635129678
             0.015479546390929876   0.040363049292390735  -0.015479546390929955  -0.8628707619039687   0.0  0.0  0.0  0.0   0.05283121635129678   0.10566243270259362  -2.7755575615628914e-17  -0.05283121635129678   0.8943375672974065   -0.05283121635129696    0.0  -0.15849364905389032  -1.0  -0.05283121635129678
             0.015479546390929848   0.040363049292390735  -0.015479546390929955   0.04812923809603137  0.0  0.0  0.0  0.0   0.05283121635129678   0.10566243270259368  -2.7755575615628914e-17  -0.05283121635129678  -0.10566243270259355   0.9471687836487028     0.0  -0.15849364905389032   0.0  -0.05283121635129678
             0.01547954639092982    0.040363049292390735  -0.015479546390929955   0.04812923809603137  0.0  0.0  0.0  0.0   0.05283121635129678   0.10566243270259373  -1.3877787807814457e-17  -0.05283121635129678  -0.10566243270259355  -0.05283121635129701    1.0  -0.15849364905389032   0.0  -0.05283121635129678
             0.015479546390929792   0.040363049292390735  -0.015479546390929955   0.04812923809603137  0.0  0.0  0.0  0.0   0.05283121635129678   0.10566243270259379   0.0                     -0.05283121635129678  -0.10566243270259355  -0.05283121635129707    0.0   0.8415063509461097    0.0  -0.05283121635129678
             0.015479546390929768   0.040363049292390735  -0.015479546390929955   0.04812923809603137  0.0  0.0  0.0  0.0   0.05283121635129678   0.10566243270259384   1.3877787807814457e-17  -0.05283121635129678  -0.10566243270259355  -0.052831216351297124   0.0  -0.15849364905389032   1.0  -0.05283121635129678
             0.015479546390929744   0.040363049292390735  -0.015479546390929955   0.04812923809603137  0.0  0.0  0.0  0.0  -0.9471687836487033    0.1056624327025939    2.7755575615628914e-17  -0.05283121635129678  -0.10566243270259355  -0.05283121635129717    0.0  -0.15849364905389032   0.0   0.9471687836487033
             0.4269446548162846     0.05183310285708586   -0.01987840201194522    0.06180622605079214  0.0  0.0  0.0  0.0   0.06784437546738986  -0.3411497410831621    0.2672180406453587      -0.11412387489577791  -0.04925565470274879   0.746288130141289      0.0  -0.20353312640216958   0.0  -0.06784437546738974]

nonsing=true::Bool

qRows=9::Int64

qCols=18::Int64

bCols=9::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= True
# test reducedForm habitmod example
function habitmodTrue()::Bool

neq=12::Int64;nlag=4::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.027529882898026693   0.0   0.02046753085696043      0.0038416784150548254   0.05104660732621768     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.15012534765256844     0.0   0.08029132274149581     -0.05696336920642234      0.033222660667064294    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.007627389623834766    0.0   0.13200357610529176     -0.0009755753382329734    0.14090688019772518     0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.4095923144666124       0.531564284055886       0.28174310568613087     -0.3703087678432382      -0.00715737500057539    0.0   0.0005945790003020514   0.0  0.0  0.0  0.0   0.024731911160965715
          8.224528448630537e-17  0.0   9.520816557054137e-18    7.453817121205115e-18  -3.9765241670148345e-17  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.7127202427610209e-16  0.0  -6.705341025742166e-17   -2.165807104666132e-17   -2.7226459401225715e-17  0.0  0.0  0.0  0.0  0.0  0.0  0.0   4.2135460933816195e-17  0.0  -1.131435862862643e-16    5.961351354153149e-17   -9.388398707293645e-17   0.0  0.0  0.0  0.0  0.0  0.0  0.0    3.340781174952024e-15    0.8887899999999991     -3.0716001200390894e-16   2.283869547545813e-16    1.103016775544864e-16  0.0   4.998384155649486e-18   0.0  0.0  0.0  0.0  -1.111288237475915e-16
          -0.5280200000000002     0.0  -0.11279000000000003     -0.03183300000000002    -0.02473400000000001     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.7152600000000002      0.0  -0.04207600000000003      0.22829000000000008      0.012266999999999962    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.22214999999999996     0.0   0.14171999999999998     -0.26679                  0.11958999999999993     0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.4230800000000047       7.206741264480954e-16   0.6107600000000001       0.1094600000000002      -0.17172000000000018    0.0   6.539875250553231e-18   0.0  0.0  0.0  0.0   0.00011277000000013762
          -0.46435999999999983    0.0  -0.1613099999999999      -0.027439999999999975   -0.17086999999999988     0.0  0.0  0.0  0.0  0.0  0.0  0.0   1.3424999999999991      0.0  -0.16793999999999984      0.3581799999999998       0.09837000000000012     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.0638200000000002      0.0   0.022550000000000243    -0.51594                 -0.08948999999999971     0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.46672000000000413     -3.76129926589772e-16    0.11369000000000035      1.1065699999999994       0.26021000000000016    0.0   6.294142887653318e-18   0.0  0.0  0.0  0.0   0.0014099999999998367
          0.23292000000000046    0.0  -0.0851199999999999       0.04213000000000004    -0.14148999999999984     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.03313000000000081     0.0  -0.15222999999999973     -0.015720000000000314     0.34762000000000015     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.7055200000000003      0.0   0.24697000000000036     -0.21887999999999994      0.07805000000000041     0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.5631600000000008      -8.231197482577451e-16   0.08268000000000053      0.16053999999999874      0.6863700000000003     0.0   1.4708699678887274e-18  0.0  0.0  0.0  0.0   0.003479999999999741
          -0.048656191278170544   0.0  -0.01435815694973597     -0.003615028856506455   -0.01636808149650229     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.09639330877304267     0.0  -0.026205246565411726     0.03374738431217089     -0.009078892369143693    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.01783229151983523     0.0  -0.02764317939480294     -0.019296742740289106    -0.03208820659304275     0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.041041558161043004     0.10522232527120402    -0.0348984919572105       0.11516795458962072     -0.01250204425957194    0.0   1.566282461372474e-5    0.0  0.0  0.0  0.0   0.02657693886125512
          -7.629376414069034e-17  0.0   6.265999690868285e-18   -8.465576914002562e-18   2.2912723786601622e-17  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -5.656105025592255e-17   0.0   1.3536683430391465e-17   1.4327911872437436e-18  -5.597968562970933e-17   0.0  0.0  0.0  0.0  0.0  0.0  0.0   2.028332630419578e-16   0.0  -2.4246695492316914e-17   5.942764221637348e-17   -4.2740351316057305e-17  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.9985300000000052       8.77668866870839e-16    9.032203701225228e-18   -1.4805427544813066e-17  -2.051300958601099e-16  0.0   0.0014700000000000073   0.0  0.0  0.0  0.0   1.5572124179398154e-16
          0.026551670931544202   0.0   0.019758049108149295     0.0037021774397815537   0.04925739354393666     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.1450040846266795      0.0   0.07745120156686947     -0.05496735589389049      0.032027423331009594    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.007611617911208028    0.0   0.1273227241038996      -0.0008541124837934361    0.13588475634843994     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -25.513272789070186        0.5127561856452838      0.2717898547614637      -0.3572798542104463      -0.006907594033121081   0.0  -0.037567719944178936    0.0  0.0  0.0  0.0   0.023860003900923955
          0.5215165039074411     0.0   0.11430026751210659      0.030927610354491354    0.02828261679995843     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.7248779895191634      0.0   0.04678007238494015     -0.22915333931303963     -0.01557639803173959     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.23264594754634338     0.0  -0.14007181332786584      0.27137534987473005     -0.12210781290947532     0.0  0.0  0.0  0.0  0.0  0.0  0.0   -0.421115826526967        0.017299234728078962   -0.603894349464411       -0.11568142124178452      0.15391702208709276    0.0   2.2277488330357095e-6   0.0  0.0  0.0  0.0   0.005045517422619975
          0.02357958772568908    0.0  -0.08180944759569314      0.010805320556173021   -0.15250184879724152     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.43627139419543065     0.0  -0.2464562057075977       0.12726448730905981      0.028486524782904855    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.2487621375603254      0.0  -0.22105757972209314     -0.1108151304310498      -0.0970433656332047      0.0  0.0  0.0  0.0  0.0  0.0  0.0   25.52323581956464        -0.9479881849820687     -0.4163263367597136       0.6148597813080481       0.4201378467424025     0.0   0.037509284219326006    0.0  0.0  0.0  0.0  -0.15216162534260927
          -0.0020115574773975882  0.0   0.0024809211673566733   -0.0005814485947749562   0.004125106371835417    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.011636485827103507    0.0   0.006753093019362888    -0.0028851347666588954   -0.00242816735607223     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.009653364628498565    0.0   0.0037311171009103737    0.004470914458811475    -0.0015781360250507145   0.0  0.0  0.0  0.0  0.0  0.0  0.0   -0.9989961535612075       0.01733774213772672     0.005740454170478411    -0.010251714541460004    -0.01654283689191306    0.0  -0.0014677581599157874   0.0  0.0  0.0  0.0   0.005132605940544283
          -5.787423759131271e-22  0.0   1.7401960564532257e-17   1.199678066996682e-18   2.7753510180459757e-17  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.2896717623199186e-17  0.0   5.923851136294227e-17   -2.0724932197447792e-17   4.220403074944324e-17   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.4246172978026056e-16  0.0   1.0568971144504532e-16  -5.0765872225745494e-17   8.29393390184753e-17    0.0  0.0  0.0  0.0  0.0  0.0  0.0    1.2559316701838371e-15   2.681100716338447e-16   1.9738138486174657e-16  -1.3335444394480052e-16   3.001946037943091e-17  0.0   1.831226612735988e-18   0.0  0.0  0.0  0.0   1.0]

    qFull = [0.048224301849328854   0.0  -0.005649146396284293     0.005226518012203298   -0.02215476160154963     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.015308948646934087   0.0  -0.020587808201071183    -0.0012231602348608053   0.03455574961918122     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.11508886468270901    0.0   0.003849805355026825    -0.025663325721305353   -0.016092763906927973   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.11058349377473162     0.08577477266636502    -0.061557028897325894     0.08871511230103518    0.0984214691068183     0.0   0.00011902473873962929  0.0  0.0  0.0  0.0   0.0809692100269587      0.9432665948914909      -1.0397739505843913      -0.8164459899560503     -0.08096921002695806    -0.08096921002695859     3.2582176786916612      -0.08096921002906798   -8.071321389024888e-14   -0.6991303348016144       0.0019668724624858913    0.46608352191604363     -0.1889689039025684
             0.12184229367564271    0.0   0.01315643886009809      0.011776395875306685    0.0023242757310194748   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.22059129119164317    0.0   0.0008029917295495695   -0.06129178127680296     0.04146298156278527     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.045053126560429105   0.0   0.031417064154452355     0.04730419872232132     0.020927007510204494   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.16967163509717323     0.7513241564441493     -0.02453854989239201     -0.1345796614826893     0.0795128651273466     0.0   0.0002359986956209491   0.0  0.0  0.0  0.0   0.1605433303543872     -0.009553094281929408    -0.8357806250940358       0.045144309325711895    0.1413895852400555     -0.16054333035438648    -0.03299815857374        -0.16054333035395985    1.6431300764452317e-14   0.007080562419868097    -1.9919838332327355e-5   -0.004720340837070042    -0.15909036196827112
             0.49066964504998173    0.0   0.09954535982417251      0.03022429851128815     0.01414185846355575     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.6322622701252367     0.0   0.03051560021834706     -0.204489715485048      -0.00036338481301305056  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.15680105798698918    0.0  -0.12857418730353962      0.23014883764676913    -0.11536314620234255    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.3509238693062533      0.05588670990545325    -0.5752655919239991      -0.06006928783796028    0.18817837065877402    0.0   3.2590484883454946e-5   0.0  0.0  0.0  0.0   0.022170397879901303   -0.19933472888674186      0.13645518489380126      1.1510412890898465     -0.036640498627503154   -0.022170397879901615   -0.6885391056498145      -0.022170397878949033   3.6963487826113806e-14   0.14774291435624862     -0.00041564706222766215  -0.09849456450897717      0.0007546546080738729
             -0.1509586455410639     0.0   0.10232601434026455     -0.03481834638965288     0.15969870625813026     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.18599969898555688    0.0   0.16812232476852762     -0.04156942535375936    -0.33475383010380316     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.8310927439878807     0.0  -0.22268395045033668      0.2830795846279476     -0.048811981161980406   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.4803091015169949      0.2147997620409701     -0.0675544455608285      -0.3474368554746763    -0.6825772800634178     0.0   0.0001474001947718218   0.0  0.0  0.0  0.0   0.04819962300129733    -2.493144579673867e-14   -0.24167661881993496     -0.04753534405872386     0.18637755529090044     0.9119238998199345      1.021405182655144e-14   -0.10027224134072954    2.4855117963795692e-14  -6.245004513516506e-17   -3.469446951953614e-17   -1.1796119636642288e-16  -0.05163054996488135
             -0.03898570051968027    0.0  -0.01418689239097786     -0.0016569485305627536  -0.011804941061041139    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.08990058842161651    0.0  -0.01275032969693673      0.025579556628944945    0.01305049771901661     0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.0725132447801352     0.0   0.013500321924465132    -0.03969311179902771     0.0036665672541703422  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.8749975344470555      0.261761701981455       0.03547480534954783      0.056047435316729656   0.01937879065353837    0.0  -0.0013663767846879649   0.0  0.0  0.0  0.0  -0.03689967572960394    -2.598615767013257e-14   -0.29451467948722987     -0.04693496623507955    -0.04254741414177614    -0.023846031180842154   -4.884981308350689e-15    0.9295080167952067     2.631228568361621e-14    9.71445146547012e-17     2.7755575615628914e-16   1.8041124150158794e-16   0.0370479446281955
             -0.47360918256095863    0.0  -0.12531586189663457     -0.03500540155108815    -0.12266496414159372     0.0  0.0  0.0  0.0  0.0  0.0  0.0   1.2331447673460467     0.0  -0.11679785807542903      0.3296614679484056      0.004648976784879369    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.8052962088950498     0.0  -0.04432176261119575     -0.41668642626540536    -0.10569955396538423    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.27299934180555274     0.05264877170439268     0.06781077687905307      0.9832847944523508     0.07585481312963546    0.0  -1.216412233445487e-5    0.0  0.0  0.0  0.0   0.025205407433645053    1.2705114738054135e-14  -0.059236458223419226     0.028013880720362257   -0.9273473043277112      0.24805975279627446    -3.3306690738754696e-15   0.008274913152365726  -1.2552459072168176e-14   1.0755285551056204e-16   1.0755285551056204e-16   1.9081958235744878e-16  -0.024764254799602863
             -0.03907555039316141    0.0  -0.020259089736222164    -0.0007650944702003904  -0.020964584905621333    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.11981378735802013    0.0  -0.021956266348871697     0.03290267049808291     0.026695298956530496    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.12323522603228682    0.0   0.02023625538916286     -0.05854921534781744     0.0020779438347818535  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.229780587961909       0.2047410640889027      0.03419820667815982      0.09225981389673804    0.050567880635814015   0.0   0.00022122212856097207  0.0  0.0  0.0  0.0  -0.9446881295234946     -1.942890293094024e-14   -0.23035932457487446     -0.03513701932849023    -0.07188888962892004    -0.055211331533372834    0.0                     -0.1504912439185233     1.9539925233402755e-14  -1.1102230246251565e-16   1.1102230246251565e-16   0.0                      0.9449855906932771
             -0.0004112545407827572  0.0  -0.00019452091767833003  -9.283091397359757e-6   -0.00018253948617520996  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0010878828139805662  0.0  -0.00019733266140110496   0.0003084228372763876   0.0002484723180920137   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.001068273564831057   0.0   0.00022281921277781332  -0.0005391189322632817   5.709424981224569e-5   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0013277438621019668   0.0004812879194471024   0.00043787950864254525   0.000742736937090406   0.0004208910287716447  0.0   7.960184538364911e-7    0.0  0.0  0.0  0.0   0.0005415091522697216   0.036519389115350916     4.8533698996223395e-5    4.8786410222974706e-5  -0.0005360186767149638  -0.0005471444607045387  -0.0020638482545803746   -0.9985865404987156    -0.038465417033911195     0.00044200742490729594  -1.2435052364175794e-6   -0.0002946694873028131   -0.0004703202703287104
             -0.0008219456605379962  0.0  -0.00038877534060981407  -1.8553464907509283e-5  -0.00036482889223182416  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.05134698239100019    0.0   0.010844861708001708     0.0048221655677330895   0.012520159255426233    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.09200018304651823    0.0   0.02123890812800464     -0.032358430807675455    0.016255504651777306   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.00344808290467279     0.0009619164717453317   0.0321487710403647       0.01440147100693539    0.03214200920744539    0.0   1.590946357932515e-6    0.0  0.0  0.0  0.0   0.0010822764339676585  -0.025820019246998577    -0.09617301352461954     -0.12754424515906868    -0.12692831449922223     0.024942086393363306    0.9440926660754702       0.10876780622394171    0.0005115657303894922   -0.16190157315159914      0.0004554798011377679    0.10793360216540732     -0.02510824030314538
             0.0012963657647829749  0.0   0.0006131731888681185    2.926236840032464e-5    0.000575405052414153    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0002753826367995046  0.0   0.001417974540763322    -0.0007192854306701087  -0.00040979038513098585  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0006156946984741206  0.0   0.0005238786055672411    5.691571632816621e-5    0.0002778625350991357  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.004141688997507744   -0.0015171265478920778  -0.0005609697276626316   -0.001792180953701343  -0.0010808598097709363  0.0  -2.509227180101973e-6    0.0  0.0  0.0  0.0  -0.0017069572653753613   0.0017069572654062255    2.6540062553319025e-14   0.5543802390475164     -0.0007706325566487458   0.0028287884071704727   0.005896146826955699    -0.4376687273959885     0.0046365858575683925    0.5551797938933185       0.00462054998751237     -0.4390441408659592       0.0014814770471078987
             -0.0019453384902116924  0.0  -0.0009201333742932887   -4.391138142503959e-5   -0.0008634581584376666   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.007890789348399664   0.0  -0.00034824954346529617   0.0016467763790267288   0.0014437822550687621   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.007118978829893302   0.0   0.0019419656250573884   -0.003763663038454512    0.0006122557466490817  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.006310529388178856    0.00227661416885948     0.0026594964111798336    0.003930215017752639   0.002160054578552229   0.0   3.7653695791140037e-6   0.0  0.0  0.0  0.0   0.0025614759041618857  -0.00256147590420059      5.5703307755532746e-14   0.40185543248639555    -0.004357414610844483   -0.0017482904648955862  -0.008847812611864053    -0.5814864751997458    -0.007064011980828997     0.405250605732331       -0.007039499124940514    -0.5794185355643707      -0.0022694334155999012
             0.0020978526998442363  0.0   0.0009922716757191404    4.7354026326086806e-5   0.0009311531324949725   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.0026201588475657693  0.0   0.0009343989393719629   -0.0012940204852223325  -0.0021908789300395413   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.007913577808070466   0.0  -0.002033110041372515     0.0026123910570644325   0.0002877157089658252  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.009013040320041424   -0.0024551003358994046  -0.003154896041872389    -0.004639310717627976  -0.002427894612094447   0.0  -4.060573919363521e-6    0.0  0.0  0.0  0.0  -0.0027622951832284135   0.0025986809011005023   -0.001261855975828161     0.586802581520069       0.006222154819559864    0.007065074273563971   -0.018085447640851134    -0.3916403805172171    -0.0016703701825073246    0.5893025979309264      -0.0016578926623558748   -0.39286555974452136      0.0019098222280376582]

nonsing=true::Bool

qRows=12::Int64

qCols=60::Int64

bCols=48::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm firmvalue example
function firmvalueFalse()::Bool

neq=4::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.22857142857142856  0.0  0.0
          0.0  0.4                  0.0  0.0
          0.0  0.0                  0.0  0.0
          0.0  0.0                  0.0  1.0]

    qFull = [0.0  -0.4  0.0   0.0  0.0                  1.0                 -1.0  0.0
             0.0   0.0  0.0   0.0  0.0                  0.0                  1.0  0.0
             0.0   0.0  0.0  -1.0  0.0                  0.0                  0.0  1.0
             0.0   0.0  0.0   0.0  0.8682431421244592  -0.4961389383568338   0.0  0.0]

nonsing=true::Bool

qRows=4::Int64

qCols=8::Int64

bCols=4::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm firmvalue3Leads2Lags example
function firmvalue3Leads2LagsFalse()::Bool

neq=4::Int64;nlag=2::Int64;nlead=3::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.03748035941322162  0.0  0.0  0.0  0.31178973986873654  0.0  0.0
          0.0  0.4                  0.0  0.0  0.0  0.0                  0.0  0.0
          0.0  0.0                  0.0  0.0  0.0  0.0                  0.0  0.0
          0.0  0.0                  0.0  0.0  0.0  0.0                  0.0  1.0]

    qFull = [0.0  -0.4  0.0  0.0  0.0   0.0  0.0   0.0  0.0                   1.0  -1.0   0.0   0.0                  0.0                   0.0   0.0   0.0                   0.0                   0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.0                   0.0   1.0   0.0   0.0                  0.0                   0.0   0.0   0.0                   0.0                   0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0  -1.0  0.0                   0.0   0.0   1.0   0.0                  0.0                   0.0   0.0   0.0                   0.0                   0.0  0.0
             0.0   0.0  0.0  0.0  0.0  -0.4  0.0   0.0  0.0                   0.0   0.0   0.0   0.0                  1.0                  -1.0   0.0   0.0                   0.0                   0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.0                   0.0   0.0   0.0   0.0                  0.0                   1.0   0.0   0.0                   0.0                   0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.0                   0.0   0.0  -1.0   0.0                  0.0                   0.0   1.0   0.0                   0.0                   0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.0                  -0.4   0.0   0.0   0.0                  0.0                   0.0   0.0   0.0                   1.0                  -1.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.0                   0.0   0.0   0.0   0.0                  0.0                   0.0   0.0   0.0                   0.0                   1.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.0                   0.0   0.0   0.0   0.0                  0.0                   0.0  -1.0   0.0                   0.0                   0.0  1.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.45595531516699267   0.0   0.0   0.0   0.4145048319699937  -0.5117343604567817    0.0   0.0   0.37682257451817575  -0.4652130549607117    0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.5568038981295276    0.0   0.0   0.0  -0.691462063428306   -0.42173953480091186   0.0   0.0   0.16843336499306558   0.39276202066712307   0.0  0.0
             0.0   0.0  0.0  0.0  0.0   0.0  0.0   0.0  0.5568038981295276    0.0   0.0   0.0   0.1852767014923728  -0.25538333444596095   0.0   0.0  -0.6286018758439149   -0.21595043972258265   0.0  0.0]

nonsing=true::Bool

qRows=12::Int64

qCols=20::Int64

bCols=8::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm example7 example
function example7False()::Bool

neq=4::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.0  0.0  -0.6551413498221794
          0.0  0.0  0.0  -0.2945196982393073
          0.0  0.0  0.0  -0.663971668063238
          0.0  0.0  0.0   0.33602833193676196]

    qFull = [0.0  0.0  0.0   1.0    0.0                  0.0                 1.0  -1.0
             0.0  0.0  0.0  -0.66   0.0                 -1.1                 0.0   1.0
             0.0  0.0  0.0   0.0   -0.0975566814665525   0.6633063548617442  0.0   0.3911675266675514
             0.0  0.0  0.0   0.0   -0.6659334978845683   0.6633063548617442  0.0  -0.7169746129238466]

nonsing=true::Bool

qRows=4::Int64

qCols=8::Int64

bCols=4::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm oneEquationNoLead example
function oneEquationNoLeadFalse()::Bool

neq=1::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [-0.6666666666666666]
    qFull = [2.0  3.0]

nonsing=true::Bool

qRows=1::Int64

qCols=2::Int64

bCols=1::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm reliablePaperExmpl example
function reliablePaperExmplFalse()::Bool

neq=5::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.0  0.0  0.0   0.0                     0.0
          0.0  0.0  0.0  -6.7324540449693005e-18  0.0
          0.0  0.0  0.0   0.21410138764106365     0.0
          0.0  0.0  0.0   0.3612359521871581      0.0
          0.0  0.0  0.0   0.5307470047448345      0.0]

    qFull = [0.0  0.0  0.0  -0.5  0.0  0.0                 -1.0   0.7                 -0.5                  1.0
             0.0  0.0  0.0   0.0  0.0  1.0                  0.0   0.0                  0.0                  0.0
             0.0  0.0  0.0   0.0  0.0  0.0                  1.0   0.0                  0.0                  0.0
             0.0  0.0  0.0   0.0  0.0  0.5485719295558827   0.0  -0.5485719295558827  -0.4750371695866738   0.5446107336163881
             0.0  0.0  0.0   0.0  0.0  0.5485719295558821   0.0  -0.5485719295558827   0.5083873480804076  -0.12472567123898434]

nonsing=true::Bool

qRows=5::Int64

qCols=10::Int64

bCols=5::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm athan example
function athanFalse()::Bool

neq=9::Int64;nlag=1::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.8285973383130333  0.29644852356208407  -0.1136903369158254   0.2123317657422602  0.0  0.0  0.0  0.0  0.0
          0.293               0.764                -0.293                0.0                 0.0  0.0  0.0  0.0  0.0
          1.9501946766260663  1.3568970471241681   -0.5203806738316508  -0.4863364685154796  0.0  0.0  0.0  0.0  0.0
          0.0                 0.0                   0.0                  0.911               0.0  0.0  0.0  0.0  0.0
          0.0                 0.0                   0.0                  0.0                 0.0  0.0  0.0  0.0  0.0
          0.0                 0.0                   0.0                  0.0                 0.0  0.0  0.0  0.0  0.0
          0.0                 0.0                   0.0                  0.0                 0.0  0.0  0.0  0.0  0.0
          0.0                 0.0                   0.0                  0.0                 0.0  0.0  0.0  0.0  0.0
          0.0                 0.0                   0.0                  0.0                 0.0  0.0  0.0  0.0  1.0]

    qFull = [-0.293               -0.764  0.293   0.0    0.0  0.0  0.0  0.0   0.0   0.0                   1.0                  0.0                    0.0                   0.0                -1.0  0.0   0.0  0.0
             0.0                  0.0    0.0     0.0    0.0  0.0  0.0  0.0   0.0  -2.0                  -1.0                  1.0                    1.0                   0.0                 0.0  2.0   0.0  0.0
             0.0                  0.0    0.0    -0.911  0.0  0.0  0.0  0.0   0.0   0.0                   0.0                  0.0                    1.0                   0.0                 0.0  0.0  -1.0  0.0
             0.0                  0.0    0.0     0.0    0.0  0.0  0.0  0.0   0.0   0.0                   0.0                  0.0                    0.0                   1.0                 0.0  0.0   0.0  0.0
             0.0                  0.0    0.0     0.0    0.0  0.0  0.0  0.0   0.0   0.0                   0.0                  0.0                    0.0                   0.0                 1.0  0.0   0.0  0.0
             0.0                  0.0    0.0     0.0    0.0  0.0  0.0  0.0   0.0   0.0                   0.0                  0.0                    0.0                   0.0                 0.0  1.0   0.0  0.0
             0.0                  0.0    0.0     0.0    0.0  0.0  0.0  0.0   0.0   0.0                   0.0                  0.0                    0.0                   0.0                 0.0  0.0   1.0  0.0
             0.0                  0.0    0.0     0.0    0.0  0.0  0.0  0.0  -1.0   0.0                   0.0                  0.0                    0.0                   0.0                 0.0  0.0   0.0  1.0
             -0.3817706346518516   0.0    0.0     0.0    0.0  0.0  0.0  0.0   0.0   0.44720713758510167  -0.25061276945656796  0.043403632078137605  -0.08106203296417115  -0.763541269303705   0.0  0.0   0.0  0.0]

nonsing=true::Bool

qRows=9::Int64

qCols=18::Int64

bCols=9::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

#tweaked= False
# test reducedForm habitmod example
function habitmodFalse()::Bool

neq=12::Int64;nlag=4::Int64;nlead=1::Int64
qRows=(neq*nlead)::Int64;qCols=(neq*(nlag+nlead))::Int64

    bb = [0.027529882897901037   0.0   0.020467530857423755    0.0038416784149892594   0.051046607327035705   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.15012534765546548   0.0   0.08029132274276037   -0.056963369207146564    0.03322266066687063    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.007627389625308317  0.0   0.1320035761063429     -0.0009755753374600662   0.14090688019805095   0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.4095923144682544    0.5315642840603882     0.2817431056881394    -0.3703087678463335    -0.007157375002846328  0.0   0.0005945790003054317  0.0  0.0  0.0  0.0   0.024731911161695656
          0.0                    0.0   0.0                     0.0                     0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                   0.0   0.0                    0.0                     0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                   0.0   0.0                     0.0                     0.0                   0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.0                   0.88879                0.0                    0.0                    0.0                   0.0   0.0                    0.0  0.0  0.0  0.0   0.0
          -0.52802                0.0  -0.11279                -0.031833               -0.024734               0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.71526               0.0  -0.042076               0.22829                 0.012267               0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.22215               0.0   0.14172                -0.26679                 0.11959               0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.42308               0.0                    0.61076                0.10946               -0.17172               0.0   0.0                    0.0  0.0  0.0  0.0   0.00011277
          -0.46436                0.0  -0.16131                -0.02744                -0.17087                0.0  0.0  0.0  0.0  0.0  0.0  0.0   1.3425                0.0  -0.16794                0.35818                 0.09837                0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.06382               0.0   0.02255                -0.51594                -0.08949               0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.46672               0.0                    0.11369                1.10657                0.26021               0.0   0.0                    0.0  0.0  0.0  0.0   0.00141
          0.23292                0.0  -0.08512                 0.04213                -0.14149                0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.03313               0.0  -0.15223               -0.01572                 0.34762                0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.70552               0.0   0.24697                -0.21888                 0.07805               0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.56316               0.0                    0.08268                0.16054                0.68637               0.0   0.0                    0.0  0.0  0.0  0.0   0.00348
          -0.048656191278364076   0.0  -0.01435815694959172    -0.003615028856553198   -0.016368081496278854   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.09639330877255069   0.0  -0.026205246565025028   0.03374738431205661    -0.009078892369366393   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.01783229151921311   0.0  -0.027643179394700654   -0.019296742740011977   -0.0320882065933294    0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.04104155816098312   0.10522232527184341   -0.034898491957179305   0.11516795458939615   -0.01250204426078594   0.0   1.5662824613858076e-5  0.0  0.0  0.0  0.0   0.026576938861786678
          0.0                    0.0   0.0                     0.0                     0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                   0.0   0.0                    0.0                     0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                   0.0   0.0                     0.0                     0.0                   0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.99853               0.0                    0.0                    0.0                    0.0                   0.0   0.00147                0.0  0.0  0.0  0.0   0.0
          0.02655167093142119    0.0   0.0197580491085966      0.0037021774397180884   0.04925739354472667    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.14500408462947548   0.0   0.0774512015680859    -0.05496735589458942     0.0320274233308213     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.007611617912634973  0.0   0.1273227241049132     -0.0008541124830458252   0.13588475634875305   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -25.513272789068484     0.51275618564965       0.27178985476340195   -0.357279854213434     -0.006907594035317548  0.0  -0.0375677199441755     0.0  0.0  0.0  0.0   0.023860003901632107
          0.5215165039059954     0.0   0.11430026751385268     0.030927610354077855    0.028282616802858217   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.7248779895273255    0.0   0.046780072389684756  -0.22915333931505477    -0.015576398033474643   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.23264594755314852   0.0  -0.140071813325273       0.2713753498778849     -0.12210781291063869   0.0  0.0  0.0  0.0  0.0  0.0  0.0   -0.4211158265242967    0.01729923474036577   -0.6038943494604492    -0.11568142124889205    0.15391702207536825   0.0   2.227748839177592e-6   0.0  0.0  0.0  0.0   0.00504551742734064
          0.02357958761338304    0.0  -0.08180944745427418     0.010805320523198469   -0.15250184855880328    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.436271393571471     0.0  -0.24645620531886603    0.12726448714723773     0.02848652465019152    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.24876213695812227   0.0  -0.22105757949745672    -0.1108151301732796     -0.09704336570703259   0.0  0.0  0.0  0.0  0.0  0.0  0.0   25.523235819797833    -0.9479881839508931    -0.4163263363976896     0.6148597806956546     0.4201378458108254    0.0   0.037509284219634904   0.0  0.0  0.0  0.0  -0.15216162500603309
          -0.0020115574774817856  0.0   0.0024809211674339513  -0.0005814485947963202   0.0041251063719627695  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.011636485827417576  0.0   0.006753093019579219  -0.0028851347667365676  -0.0024281673561649332  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.009653364628833686  0.0   0.0037311171010123876   0.004470914458955043   -0.001578136025136179  0.0  0.0  0.0  0.0  0.0  0.0  0.0   -0.9989961535611305    0.017337742138220126   0.005740454170615235  -0.010251714541712925  -0.016542836892484758  0.0  -0.0014677581599157072  0.0  0.0  0.0  0.0   0.005132605941010024
          0.0                    0.0   0.0                     0.0                     0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                   0.0   0.0                    0.0                     0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                   0.0   0.0                     0.0                     0.0                   0.0  0.0  0.0  0.0  0.0  0.0  0.0    0.0                   0.0                    0.0                    0.0                    0.0                   0.0   0.0                    0.0  0.0  0.0  0.0   1.0]

    qFull = [0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                     0.0       0.0                     0.0                     0.0                     0.0   0.0      0.0  0.0  0.0  0.0   0.0   1.0                     -1.0                     -1.0                    0.0                     0.0                     3.454185377005518        0.0                   0.0                    -0.74118                0.0020851713324080146   0.49411642948057477    -0.1149
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    -0.88879   0.0                     0.0                     0.0                     0.0   0.0      0.0  0.0  0.0  0.0   0.0   0.0                      1.0                      0.0                    0.0                     0.0                     0.0                      0.0                   0.0                     0.0                    0.0                     0.0                     0.0
             0.52802  0.0  0.11279   0.031833  0.024734  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.71526                0.0   0.042076               -0.22829                 -0.012267                0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.22215                0.0  -0.14172                 0.26679                 -0.11959                0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.42308                 0.0      -0.61076                -0.10946                 0.17172                 0.0   0.0      0.0  0.0  0.0  0.0   0.0   0.0                      0.0                      1.0                    0.0                     0.0                     0.0                      0.0                   0.0                     0.0                    0.0                     0.0                    -0.00011277
             0.46436  0.0  0.16131   0.02744   0.17087   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.3425                 0.0   0.16794                -0.35818                 -0.09837                 0.0  0.0  0.0  0.0  0.0  0.0  0.0   1.06382                0.0  -0.02255                 0.51594                  0.08949                0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.46672                 0.0      -0.11369                -1.10657                -0.26021                 0.0   0.0      0.0  0.0  0.0  0.0   0.0   0.0                      0.0                      0.0                    1.0                     0.0                     0.0                      0.0                   0.0                     0.0                    0.0                     0.0                    -0.00141
             -0.23292  0.0  0.08512  -0.04213   0.14149   0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.03313                0.0   0.15223                 0.01572                 -0.34762                 0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.70552                0.0  -0.24697                 0.21888                 -0.07805                0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.56316                 0.0      -0.08268                -0.16054                -0.68637                 0.0   0.0      0.0  0.0  0.0  0.0   0.0   0.0                      0.0                      0.0                    0.0                     1.0                     0.0                      0.0                   0.0                     0.0                    0.0                     0.0                    -0.00348
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.99853                 0.0       0.0                     0.0                     0.0                     0.0  -0.00147  0.0  0.0  0.0  0.0   0.0   0.0                      0.0                      0.0                    0.0                     0.0                     0.0                      1.0                   0.0                     0.0                    0.0                     0.0                     0.0
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                     0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                    0.0   0.0                     0.0                      0.0                    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0                     0.0       0.0                     0.0                     0.0                     0.0   0.0      0.0  0.0  0.0  0.0  -1.0   0.0                      0.0                      0.0                    0.0                     0.0                     0.0                      0.0                   0.0                     0.0                    0.0                     0.0                     1.0
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   8.477325348273948e-6   0.0   1.1737551566453782e-6   6.438504491140261e-7    -3.2212814098871563e-7   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -1.0141837764993982e-5  0.0  -1.290569629952694e-7   -3.263768522769671e-6     1.4707395335235414e-6  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -2.397166450442443e-7    0.0      -8.116540081425253e-7    2.689733129477345e-6   -1.2979305662448641e-6   0.0   0.0      0.0  0.0  0.0  0.0   0.0  -0.03708042573632686     -4.855927150699948e-5    -4.881211588640728e-5  -5.493368498380228e-6    5.638277691209124e-6    0.0001934771491334187    0.9985709034657091    0.0384856845466372     -4.067309088056605e-5   1.144261354928439e-7    2.7115197984086615e-5  -6.263733239886542e-6
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.04899705783402774    0.0  -0.01120063003324614    -0.0041908808538570355   -0.011985374512245739    0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.08955474739288391    0.0  -0.020726693191700216    0.031173432412953613    -0.016086348259092616   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.000792326481880088    0.0      -0.031174251114590296   -0.012869952348978147   -0.031202323408062196    0.0   0.0      0.0  0.0  0.0  0.0   0.0   0.024657869055016407     0.09586248802299646      0.1271324275479889     0.12543970366338042    -0.02594033480294185    -0.9447706721679326      -0.10949539634129295  -0.0005099139759182568   0.1621783934696507    -0.0004562585833385613  -0.10811814771057904     0.02407773726347739
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0032969809188035095  0.0   0.0007065518814006049   0.00022528093791596984   0.0003290802651319052   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.002459648796279586   0.0   0.0010831489569940011  -0.0014603914314449918    0.0004086346563548683  0.0  0.0  0.0  0.0  0.0  0.0  0.0   3.790579464096496e-5    0.0       0.000721688330498851    0.0004926600336232699   0.00021364639192133385  0.0   0.0      0.0  0.0  0.0  0.0   0.0  -3.073009005820629e-15   -2.3534288462992786e-14   0.4898458201841746    -0.0021891779921598105   0.0009912407709166623   1.9223723553953657e-12  -0.509098441610823    -0.0007414741467501591   0.4916701875272666    -0.0007383405240319134  -0.5090201801345099     -1.8388707037398475e-5
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.003643145263829      0.0   0.0007807358318102284   0.00024893416196507495   0.00036363183117568666  0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.0027178980047612962  0.0   0.0011968734697510796  -0.0016137242699783073    0.0004515389835914196  0.0  0.0  0.0  0.0  0.0  0.0  0.0   4.1885688268979986e-5   0.0       0.0007974615223329046   0.0005443865499711582   0.00023607805436863535  0.0   0.0      0.0  0.0  0.0  0.0   0.0   3.693669555075654e-15    2.7135130248811612e-14   0.541276860203469     -0.002419029296208815    0.0010953154446671119  -1.025245807949531e-12   -0.4535625006640263    0.0035433989458416113   0.5432927757972709     0.0035284238108551826  -0.453954433367246      -2.03194166257265e-5
             0.0      0.0  0.0       0.0       0.0       0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.0029638430910210504  0.0   6.603929128961849e-5   -0.00028173972559416606   0.0009223540969435295   0.0  0.0  0.0  0.0  0.0  0.0  0.0  -0.0024054954219422026  0.0   0.0008945409686296616   0.00015390361311534015  -0.0005847564942789633  0.0  0.0  0.0  0.0  0.0  0.0  0.0   0.0022347988133203534   0.0       0.0009226800534779761   0.0008343902524113385   0.00028666846109542244  0.0   0.0      0.0  0.0  0.0  0.0   0.0   0.00016313960259124535   0.0012581950656749652   -0.5851001427047119    -0.0034498218571154976  -0.0042902958192899306   0.027546775726307662     0.3932584298341381    0.0016655240839675194  -0.5896343241644627     0.0016588259119062225   0.39308670895798836     0.000519753808703009]

nonsing=true::Bool

qRows=12::Int64

qCols=60::Int64

bCols=48::Int64

anEpsi=0.0000000001::Float64

(nonsingJulia,bbJulia)=reducedForm(qFull,qRows,qCols,bCols,neq,anEpsi)
isapprox(bbJulia,bb,rtol=0.1e-10::Float64,atol=0.0::Float64)&&
nonsingJulia==nonsing
end;

end


text:
module DynamAnim
using Luxor

struct DynamicalSystem{T,S}
  init::T
  params::S
  step::Function
end

struct AnimParams
  setup::Function
  draw::Function
  frames::Int64
  steps_per_frame::Int64
  width::Int64
  height::Int64
  name::String
end

function anim(ds::DynamicalSystem{T,S},ap::AnimParams,path) where {T,S}
  sim = [zero(ds.init) for _ in 1:ap.frames]
  sim[1] = ds.init
  for i in 1:(ap.frames-1)
    sim[i+1] = copy(sim[i])
    for j in 1:ap.steps_per_frame
      ds.step(sim[i+1],ds.params)
    end
  end
  setup = ap.setup(sim[1],ap.width,ap.height)
  function scene(s,framenumber)
    ap.draw(sim[framenumber],setup)
  end
  m = Movie(ap.width,ap.height,ap.name)
  animate(m,[Scene(m,scene,1:ap.frames)],creategif=true,pathname=path)
end

end


text:
module LGravity

include("Types.jl")
include("Preprocessing.jl")

using TextAnalysis
using StaticArrays
using Counters
using Strs
using WordTokenizers # TODO: see if I need it
using Dictionaries
using Memoize
import WordTokenizers: tokenize # for being able to extend an existing package

export Strings, RichnessBundle, ngramfirstcombine, f, g, ngram, ngramdirection, tokenizestr, bigramsinside, lexicalgravitypair

"""
    g(text::Strings; mode = "forward")

A function that takes a Vector of strings and outputs a vector of string vectors with ngram combinations of the
    first member with all the other elements of the vector. It is the first step for implementing the lexical gravity.
    For more details, cf. (2004).

# Examples
```julia-repl
julia> g(text)
2-element Vector{RichnessBundle}:
RichnessBundle(Dict("Στα" => 0.3475), ["Στα", "δικαστήρια", "θα", "αναζητήσει"], [["Στα", "δικαστήρια"], ["Στα", "θα"], ["Στα", "αναζητήσει"]], "Στα")
 RichnessBundle(Dict("δικαστήρια" => 0.3215), ["δικαστήρια", "θα", "αναζητήσει", "το"], [["δικαστήρια", "θα"], ["δικαστήρια", "αναζητήσει"], ["δικαστήρια", "το"]], "δικαστήρια")
```
"""
@memoize function g(text::Strings; mode = "forward")
    lexicalrichness = Vector{Union{RichnessBundle, Dict}}(undef, 0)
    text = lowercase(text)
    tokenized_text2 = tokenizestr(text)
    tokenized_text = unique(tokenizestr(text))
    ngrams = ngramdirection2(tokenized_text2, direction = mode)
    overallgdict = Dict()
    for word in tokenized_text
        gdict = Dict()
        bigrams = bigramsinside(word, tokenized_text2, ngrams)
        if length(bigrams) > 0
            # unique words in the context of word within a 4-gram.
            uniquecontext = unique(collect(Iterators.flatten(bigrams)))
            g = size(bigrams, 1) / (length(uniquecontext) - 1)
            println(g)
            push!(gdict, word => g)
            push!(overallgdict, word => g)
            println(typeof(uniquecontext), typeof(allbigrams), typeof(word))
            push!(lexicalrichness, RichnessBundle(gdict, uniquecontext, bigrams, word))
        end
    end
    push!(lexicalrichness, overallgdict)
    return lexicalrichness
end

# For getting the g of a specific word for the denominators in the lexicalgravitypair() function.
@memoize function g(word::String, text::Strings; mode = "forward")
    word = lowercase(word)
    return gsimple2(text, mode=mode)[end][word]
end

"""
    f(x::String, y::String, text::Strings)

A function that returns co-occurence frequency of words (x, y) within a 3-word span from x onwards in the right direction. In other words, x is the focal word. The type **Strings**
    Union{Str,String,StringDocument, Vector{String}}. For this type of frequency, plase cf. XXX(2004).

# Examples
```julia-repl
julia> f("η", "τιμή", mytext)
2
```
"""
@memoize function f(x::String, y::String, lexicalrichness::Vector{Union{RichnessBundle, Dict}})
    ind = [lowercase(x) == lexicalrichness[i].word for i=1:length(lexicalrichness)]
    freqxy = counter([x[2] for x in lexicalrichness[ind][1].bigrams])[y]
    return freqxy
end

"""
    lexicalgravitypair(x::String, y::String, text::Strings)

A function that takes a Vector of strings and outputs a vector of string vectors with ngram combinations of the
    first member with all the other elements of the vector. It is the first step for implementing the lexical gravity.
    For more details, cf. (2004).

# Examples
```julia-repl
julia> f("η", "τιμή")
2
```
"""
@memoize function lexicalgravitypair(x::String, y::String, text::Strings)
    f_xy = f(x,y,g(text)[1:end-1])
    log(f_xy/g(x, text)) + log(f_xy/g(x, text, mode = "backward"))
end

end