text:
using Match

function isLeapYear(y)
	y%4==0 && !(y%100==0 && !(y%400==0))
	# if !(y%4==0)
	# 	false
	# elseif !(y%100==0)
	# 	true
	# elseif !(y%400==0)
	# 	false
	# else
	# 	true
	# end
end

function nextday(currentdate)
	y = currentdate["year"]
	leapy = isLeapYear(y)
	m = currentdate["month"]
	nDaysInMonth = @match m begin
        1  => 31
        2  => leapy ? 29 : 28
        3  => 31
        4  => 30
        5  => 31
        6  => 30
        7  => 31
        8  => 31
        9  => 30
        10 => 31
        11 => 30
        12 => 31
        _  => error("bug")
    end
    n = currentdate["number"]
    isNewMonth = (n == nDaysInMonth)
    newn = isNewMonth ? 1 : n+1
    newm = isNewMonth ? (m==12 ? 1 : m+1) : m
    newy = (isNewMonth && m==12) ? y+1 : y

	d = currentdate["day"]
    newd = ((d+1 -1) % 7) +1
    #println("$d $n $m $y  $leapy $nDaysInMonth $isNewMonth")
    date = ["year"=>newy,"month"=>newm,"number"=>newn,"day"=>newd]
    #println("$date ")
    return date
end

function nextdayuntil(startdate, enddate, countpred)
	c = 0

	date = startdate
	while date["year"] < enddate["year"] ||
			date["month"] != enddate["month"] ||
			date["number"] != enddate["number"]

		if countpred(date)
			c = c+1
		end
		date = nextday(date)
	end
	return (c, date)
end

function main()
	# from http://kalender-365.de/leap-years.php
	listLeapYear = [1804,1808,1812,1816,1820,1824,1828,1832,1836,1840,1844,1848,1852,1856,1860,1864,1868,1872,1876,1880,1884,1888,1892,1896,
	1904,1908,1912,1916,1920,1924,1928,1932,1936,1940,1944,1948,1952,1956,1960,1964,1968,1972,1976,1980,1984,1988,1992,1996,2000,
	2004,2008,2012,2016,2020,2024,2028,2032,2036,2040,2044,2048,2052,2056,2060,2064,2068,2072,2076,2080,2084,2088,2092,2096]
	assert(all(map(isLeapYear, filter(x -> in(x,listLeapYear), 1800:2100))))
	assert(!(any(map(isLeapYear, filter(x -> !in(x,listLeapYear), 1800:2100)))))

	initdate  = ["year"=>1900,"month"=>1,"number"=>1,"day"=>1]
	startdate = ["year"=>1901,"month"=>1,"number"=>1]
	# augment startdate with day information
	(_,startdate) = nextdayuntil(initdate, startdate, x -> false)

	enddate   = ["year"=>2000,"month"=>12,"number"=>31]
	nextdayuntil(startdate, enddate, x -> x["number"] == 1 && x["day"] == 7)
end

main()

max_stars_repo_name: cmey/euler

max_issues_repo_name: cmey/euler

max_forks_repo_name: cmey/euler


text:
module Memento

using Base: StackTrace, StackFrame
using UUIDs
using Dates
using Sockets
using Distributed
using Serialization

import Syslogs
import JSON
using TimeZones

import Base: show, error, log, getindex

export info, warn, debug, notice, error, critical, alert, emergency,
       isset, isroot, ispropagating, setpropagating!,
       getlevel, setlevel!, addlevel!, setrecord!,
       getlogger, gethandlers, getfilters, format, emit,

       Logger, Record, AttributeRecord, DefaultRecord, Formatter, Handler,
       DefaultFormatter, DictFormatter, DefaultHandler, FileRoller,
       LoggerSerializationError

const DEFAULT_LOG_LEVEL = "info"

const _log_levels = Dict{AbstractString, Int}(
    "not_set" => 0,
    "debug" => 10,
    "info" => 20,
    "notice" => 30,
    "warn" => 40,
    "error" => 50,
    "critical" => 60,
    "alert" => 70,
    "emergency" => 80
)

include("io.jl")
include("records.jl")
include("filters.jl")
include("formatters.jl")
include("handlers.jl")
include("loggers.jl")
include("syslog.jl")
include("stdlib.jl")
include("config.jl")
include("exceptions.jl")
include("memento_test.jl")
include("deprecated.jl")

# Initializing at compile-time will work as long as the loggers which are added do not
# contain references to stdout.
const _loggers = Dict{AbstractString, Logger}(
    "root" => Logger("root"),
)

const LOGGER = getlogger(@__MODULE__)

function __init__()
    Memento.config!(DEFAULT_LOG_LEVEL)
    Memento.register(LOGGER)
end

end

ext: jl


text:
@testset "helper functions" begin
    @testset "_update! Array" begin
        # Hits fallback, since we can't update `Diagonal`s in place
        X = Diagonal([1, 1])
        Y = copy(X)
        @test ChainRules._update!(X, [1 2; 3 4]) == [2 2; 3 5]
        @test X == Y  # no change to X

        X = [1 2; 3 4]
        Y = copy(X)
        @test ChainRules._update!(X, Diagonal([1, 1])) == [2 2; 3 5]
        @test X != Y  # X has been updated
    end
    @testset "_update! Zero" begin
        X = [1 2; 3 4]
        @test ChainRules._update!(X, Zero()) === X
        @test ChainRules._update!(Zero(), X) === X
        @test ChainRules._update!(Zero(), Zero()) === Zero()
    end
    @testset "_update! NamedTuple" begin
        X = (A=[1 0; 0 1], B=[2 2; 2 2])
        old_X = deepcopy(X)
        Y = deepcopy(X)
        @test ChainRules._update!(X, Y, :A) == (A=[2 0; 0 2], B=[2 2; 2 2])
        @test X.A != old_X.A
        @test X.B == old_X.B
    end
    @testset "_checked_rrule" begin
        try
            @eval cool(x,y) = x + y
            # We defined a 2-arg method for `cool` but no `rrule`
            ChainRules._checked_rrule(cool, 1.0, 2.0)
        catch e
            @test e isa ArgumentError
            @test e.msg == "can't differentiate `cool(::Float64, ::Float64)`; no " *
                "matching `rrule` is defined"
        end
    end
end


text:
module SaddlePointSystems

#=
Still to do:
* Recursive saddle point systems
=#

using LinearMaps
using RecursiveArrayTools
using IterativeSolvers
using UnPack

using LinearAlgebra
import LinearAlgebra: ldiv!, mul!, *, \, I

import Base: size, eltype

export SaddleSystem, SaddleVector, state, constraint
export SchurSolverType, Direct, CG, BiCG, GMRES

include("saddlepoint/saddlesystems.jl")
include("saddlepoint/vectors.jl")
include("saddlepoint/linearmaps.jl")
include("saddlepoint/arithmetic.jl")

end


text:
push!(LOAD_PATH,"../src/")

using Documenter, ModifiedHankelFunctionsOfOrderOneThird

makedocs(
    modules=[ModifiedHankelFunctionsOfOrderOneThird],
    sitename="ModifiedHankelFunctionsOfOrderOneThird.jl",
    pages = [
        "Home" => "index.md",
        "API" => "pages/api.md",
        ],
)

deploydocs(
    repo="github.com/fgasdia/ModifiedHankelFunctionsOfOrderOneThird.jl.git"
)


text:
const OP_BIN_SCALAR_SUM = OptimizingIR.Op(+, pure=true, commutative=true, hasleftidentity=true, hasrightidentity=true, identity_element=0)
const OP_BIN_SCALAR_SUB = OptimizingIR.Op(-, pure=true, hasrightidentity=true, identity_element=0)
const OP_BIN_SCALAR_MUL = OptimizingIR.Op(*, pure=true, commutative=true, hasleftidentity=true, hasrightidentity=true, identity_element=1)
const OP_BIN_SCALAR_DIV = OptimizingIR.Op(/, pure=true, hasrightidentity=true, identity_element=1)
const OP_BIN_SCALAR_MIN = OptimizingIR.Op(min, pure=true, commutative=true)
const OP_BIN_SCALAR_MAX = OptimizingIR.Op(max, pure=true, commutative=true)
const OP_BIN_SCALAR_POW = OptimizingIR.Op(^, pure=true)

const OP_UNARY_SCALAR_SUB = OptimizingIR.Op(-, pure=true)
const OP_UNARY_SCALAR_EXP = OptimizingIR.Op(exp, pure=true)
const OP_UNARY_SCALAR_LOG = OptimizingIR.Op(log, pure=true)
const OP_UNARY_SCALAR_SQRT = OptimizingIR.Op(sqrt, pure=true)

const OP_GETINDEX = OptimizingIR.Op(Base.getindex, pure=true)

#
# Black-Scholes Model
#

const OP_BLACK_FORMULA_CALL = OptimizingIR.Op(BS.bscall, pure=true)
const OP_BLACK_FORMULA_PUT = OptimizingIR.Op(BS.bsput, pure=true)

#
# Binomial Daily Model
#

CRR_volatility_match_up_factor(σ, Δt) = exp(σ * sqrt(Δt))
const OP_CRR_VOLATILITY_MATCH_UP_FACTOR = OptimizingIR.Op(CRR_volatility_match_up_factor, pure=true)

underlying_price(s, n, i, ln_u, ln_d) = s * exp((i-1)*ln_u + (n-i)*ln_d)
const OP_BINOMIAL_UNDERLYING_PRICE_AT = OptimizingIR.Op(underlying_price, pure=true)

function risk_neutral_pricing_step(riskfree_discount_factor, risk_neutral_probability, next_step_price_up, next_step_price_down)
    df = riskfree_discount_factor
    q = risk_neutral_probability
    f_up = next_step_price_up
    f_down = next_step_price_down
    return df * ( q * f_up + (1-q) * f_down )
end
const OP_BINOMIAL_RISK_NEUTRAL_PRICING_STEP = OptimizingIR.Op(risk_neutral_pricing_step, pure=true)

function risk_neutral_probability(d, ert, u)
    @assert d <= ert <= u "No-arbitrage condition was not satisfied: d = $d, ert = $ert, u = $u."
    return ( ert - d ) / ( u - d )
end
const OP_BINOMIAL_RISK_NEUTRAL_PROBABILITY = OptimizingIR.Op(risk_neutral_probability, pure=true)


text:
using CG

const len = 10

function laplace(py::Ptr{Float64}, px::Ptr{Float64})::Cint
    x = unsafe_wrap(Array, px, len)
    y = unsafe_wrap(Array, py, len)
    c = 0.01
    y[1] = x[1] - c*x[2]
    for i = 2:len-1
        y[i] = x[i] - c*(x[i-1] + x[i+1])
    end
    y[len] = x[len] - c*x[len-1]
    return Cint(0)
end

b = ones(len)
x = zeros(len)

CG.julia_cg(@cfunction(laplace,Cint,(Ptr{Float64}, Ptr{Float64})), pointer(x), pointer(b), Csize_t(len))

max_stars_repo_name: kmsquire/libcg

max_issues_repo_name: kmsquire/libcg

max_forks_repo_name: kmsquire/libcg


text:
#-------------------------------------------------------------------
#* EMSO Model Library (EML) Copyright (C) 2004 - 2007 ALSOC.
#*
#* This LIBRARY is free software; you can distribute it and/or modify
#* it under the therms of the ALSOC FREE LICENSE as available at
#* http://www.enq.ufrgs.br/alsoc.
#*
#* EMSO Copyright (C) 2004 - 2007 ALSOC, original code
#* from http://www.rps.eng.br Copyright (C) 2002-2004.
#* All rights reserved.
#*
#* EMSO is distributed under the therms of the ALSOC LICENSE as
#* available at http://www.enq.ufrgs.br/alsoc.
#*
#*----------------------------------------------------------------------
#* Author: Paula B. Staudt
#* $Id$
#*--------------------------------------------------------------------
type reboilerReact
	reboilerReact()=begin
		PP=outers.PP
		NComp=outers.NComp
		[
			:(Level = Initial_Level),
			:(OutletLiquid.T = Initial_Temperature),
			:(OutletLiquid.z(1:NComp-1) = Initial_Composition(1:NComp-1)/sum(Initial_Composition)),
		],
		[
			"","","",
		],
		new(
			DanaPlugin(Dict{Symbol,Any}(
				:Type=>"PP"
			)),
			DanaInteger(),
			area(Dict{Symbol,Any}(
				:Brief=>"Cross Section Area of reboiler"
			)),
			volume(Dict{Symbol,Any}(
				:Brief=>"Total volume of reboiler"
			)),
			fill(DanaReal(Dict{Symbol,Any}(
				:Brief=>"Stoichiometric matrix"
			)),(NComp)),
			energy_mol(),
			length(Dict{Symbol,Any}(
				:Brief=>"Initial Level of liquid phase"
			)),
			temperature(Dict{Symbol,Any}(
				:Brief=>"Initial Temperature of Reboiler"
			)),
			fill(fraction(Dict{Symbol,Any}(
				:Brief=>"Initial Liquid Composition"
			)),(NComp)),
			stream(Dict{Symbol,Any}(
				:Brief=>"Liquid inlet stream",
				:PosX=>0,
				:PosY=>0.5254,
				:Symbol=>"_{inL}"
			)),
			liquid_stream(Dict{Symbol,Any}(
				:Brief=>"Liquid outlet stream",
				:PosX=>0.2413,
				:PosY=>1,
				:Symbol=>"_{outL}"
			)),
			vapour_stream(Dict{Symbol,Any}(
				:Brief=>"Vapour outlet stream",
				:PosX=>0.5079,
				:PosY=>0,
				:Symbol=>"_{outV}"
			)),
			power(Dict{Symbol,Any}(
				:Brief=>"Heat supplied",
				:PosX=>1,
				:PosY=>0.6123,
				:Symbol=>"_{in}"
			)),
			fill(mol(Dict{Symbol,Any}(
				:Brief=>"Molar Holdup in the tray"
			)),(NComp)),
			mol(Dict{Symbol,Any}(
				:Brief=>"Molar liquid holdup"
			)),
			mol(Dict{Symbol,Any}(
				:Brief=>"Molar vapour holdup"
			)),
			energy(Dict{Symbol,Any}(
				:Brief=>"Total Energy Holdup on tray"
			)),
			volume_mol(Dict{Symbol,Any}(
				:Brief=>"Liquid Molar Volume"
			)),
			volume_mol(Dict{Symbol,Any}(
				:Brief=>"Vapour Molar volume"
			)),
			length(Dict{Symbol,Any}(
				:Brief=>"Level of liquid phase"
			)),
			volume(),
			dens_mass(),
			reaction_mol(Dict{Symbol,Any}(
				:Brief=>"Reaction resulting ethyl acetate",
				:DisplayUnit=>"mol/l/s"
			)),
			fill(conc_mol(Dict{Symbol,Any}(
				:Brief=>"Molar concentration",
				:Lower=>-1
			)),(NComp)),
			[
				:(OutletLiquid.z = vL * C),
				:(r3 = exp(-7150*"K"/OutletLiquid.T)*(4.85e4*C(1)*C(2) - 1.23e4*C(3)*C(4)) * "l/mol/s"),
				:(diff(M)= InletLiquid.F*InletLiquid.z- OutletLiquid.F*OutletLiquid.z - OutletVapour.F*OutletVapour.z + stoic*r3*ML*vL),
				:(diff(E) = InletLiquid.F*InletLiquid.h- OutletLiquid.F*OutletLiquid.h - OutletVapour.F*OutletVapour.h + InletQ + Hr * r3 * vL*ML),
				:(M = ML*OutletLiquid.z + MV*OutletVapour.z),
				:(E = ML*OutletLiquid.h + MV*OutletVapour.h - OutletLiquid.P*V),
				:(sum(OutletLiquid.z)=1.0),
				:(vL = PP.LiquidVolume(OutletLiquid.T, OutletLiquid.P, OutletLiquid.z)),
				:(vV = PP.VapourVolume(OutletVapour.T, OutletVapour.P, OutletVapour.z)),
				:(rhoV = PP.VapourDensity(OutletVapour.T, OutletVapour.P, OutletVapour.z)),
				:(Level = ML*vL/Across),
				:(Vol = ML*vL),
				:(OutletLiquid.P = OutletVapour.P),
				:(OutletLiquid.T = OutletVapour.T),
				:(V = ML*vL + MV*vV),
				:(PP.LiquidFugacityCoefficient(OutletLiquid.T, OutletLiquid.P, OutletLiquid.z)*OutletLiquid.z = PP.VapourFugacityCoefficient(OutletVapour.T, OutletVapour.P, OutletVapour.z)*OutletVapour.z),
				:(sum(OutletLiquid.z)=sum(OutletVapour.z)),
			],
			[
				"Molar Concentration","Reaction","Component Molar Balance","Energy Balance","Molar Holdup","Energy Holdup","Mol fraction normalisation","Liquid Volume","Vapour Volume","Vapour Density","Level of liquid phase","","Mechanical Equilibrium","Thermal Equilibrium","Geometry Constraint","Chemical Equilibrium","",
			],
			[:PP,:NComp,:Across,:V,:stoic,:Hr,:Initial_Level,:Initial_Temperature,:Initial_Composition,],
			[:InletLiquid,:OutletLiquid,:OutletVapour,:InletQ,:M,:ML,:MV,:E,:vL,:vV,:Level,:Vol,:rhoV,:r3,:C,]
		)
	end
	PP::DanaPlugin
	NComp::DanaInteger
	Across::area
	V::volume
	stoic::Array{DanaReal}
	Hr::energy_mol
	Initial_Level::length
	Initial_Temperature::temperature
	Initial_Composition::Array{fraction}
	InletLiquid::stream
	OutletLiquid::liquid_stream
	OutletVapour::vapour_stream
	InletQ::power
	M::Array{mol}
	ML::mol
	MV::mol
	E::energy
	vL::volume_mol
	vV::volume_mol
	Level::length
	Vol::volume
	rhoV::dens_mass
	r3::reaction_mol
	C::Array{conc_mol}
	initials::Array{Expr,1}
	initialNames::Array{String,1}
	equations::Array{Expr,1}
	equationNames::Array{String,1}
	parameters::Array{Symbol,1}
	variables::Array{Symbol,1}
	attributes::Dict{Symbol,Any}
end
export reboilerReact
function initial(in::reboilerReact)
	addEquation(1)
	addEquation(2)
	addEquation(3)
end
function setEquationFlow(in::reboilerReact)
	addEquation(1)
	addEquation(2)
	addEquation(3)
	addEquation(4)
	addEquation(5)
	addEquation(6)
	addEquation(7)
	addEquation(8)
	addEquation(9)
	addEquation(10)
	addEquation(11)
	addEquation(12)
	addEquation(13)
	addEquation(14)
	addEquation(15)
	addEquation(16)
	addEquation(17)
end
function atributes(in::reboilerReact,_::Dict{Symbol,Any})
	fields::Dict{Symbol,Any}=Dict{Symbol,Any}()
	fields[:Pallete]=false
	fields[:Icon]="icon/Reboiler"
	fields[:Brief]="Model of a dynamic reboiler with reaction."
	fields[:Info]="== Assumptions ==
* perfect mixing of both phases;
* thermodynamics equilibrium;
* no liquid entrainment in the vapour stream;
* the reaction takes place only in the liquid phase.

== Specify ==
* the kinetics variables;
* the inlet stream;
* the liquid inlet stream;
* the outlet flows: OutletVapour.F and OutletLiquid.F;
* the heat supply.

== Initial Conditions ==
* the reboiler temperature (OutletLiquid.T);
* the reboiler liquid level (Level);
* (NoComps - 1) OutletLiquid (OR OutletVapour) compositions.
"
	drive!(fields,_)
	return fields
end
reboilerReact(_::Dict{Symbol,Any})=begin
	newModel=reboilerReact()
	newModel.attributes=atributes(newModel,_)
	newModel
end


text:
module IdentityVectorsTests

using Test
using Gridap.Arrays

l = 10
a = identity_vector(l)
b = collect(1:l)
test_array(a,b)

c = rand(l)
d = reindex(c,a)
@test d === c

end # module


text:
# See https://github.com/jump-dev/MathOptInterface.jl/issues/321,
#     https://github.com/jump-dev/MathOptInterface.jl/pull/323 and
#     https://github.com/jump-dev/MathOptInterface.jl/pull/390

using BenchmarkTools
using VecMathOptInterface
const MOI = VecMathOptInterface
const MOIU = MOI.Utilities

@MOIU.model Model () (MOI.Interval,) () () () (MOI.ScalarAffineFunction,) () ()
optimizer = MOIU.MockOptimizer(Model{Float64}())
caching_optimizer = MOIU.CachingOptimizer(Model{Float64}(), optimizer)
MOIU.reset_optimizer(caching_optimizer) # detach optimizer
v = MOI.add_variables(caching_optimizer, 2)
cf = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([0.0, 0.0], v), 0.0)
c = MOI.add_constraint(caching_optimizer, cf, MOI.Interval(-Inf, 1.0))
@btime MOI.set($caching_optimizer, $(MOI.ConstraintSet()), $c, $(MOI.Interval(0.0, 2.0)))
MOIU.attach_optimizer(caching_optimizer)
@btime MOI.set($caching_optimizer, $(MOI.ConstraintSet()), $c, $(MOI.Interval(0.0, 2.0)))
@btime MOI.set($(caching_optimizer.model_cache), $(MOI.ConstraintSet()), $c, $(MOI.Interval(0.0, 2.0)))
@btime MOI.set($(caching_optimizer.optimizer), $(MOI.ConstraintSet()), $(caching_optimizer.model_to_optimizer_map[c]), $(MOI.Interval(0.0, 2.0)))


text:
function test_transit_poly_gradient_case(u_n,test_name)

r0 = [0.01,.1,0.5,0.99,1.0,1.01,2.0,10.,100.0]; n_u = length(u_n)
#r0 = [0.01,.1,0.5,0.999999,1.0,1.000001,2.0,10.,100.0]; n_u = length(u_n)
#r0 = [1.0]; n_u = length(u_n)
#r0_name =["0.01","0.1","0.5","0.999999","1","1.000001","2","10","100"]
r0_name =["0.01","0.1","0.5","0.99","1","1.01","2","10","100"]
#r0_name =["1"]
nb = 50

epsilon = 1e-12; delta = 1e-3
dfdrbu = zeros(n_u+2)
dfdrbu_big = zeros(BigFloat,n_u+2)
label_name=["r","b","u_1","u_2","u_3","u_4","u_5","u_6","u_7","u_8","u_9","u_10","u_11","u_12","u_13"]
floor = 1e-20
if ~skip_plots
  fig,axes = subplots(3,3)
end
fgrid = zeros(length(r0),nb*13)
@testset "$test_name" begin
for i=1:length(r0)
  global r=r0[i]
  if r < 1.0
    bgrid = abs.([linearspace(0.0,epsilon,nb); linearspace(epsilon,delta,nb); linearspace(delta,r-delta,nb);
     -logarithmspace(log10(delta),log10(epsilon),nb) .+ r; linearspace(r-epsilon,r+epsilon,nb); logarithmspace(log10(epsilon),log10(delta),nb) .+ r;
     linearspace(r+delta,1-r-delta,nb); -logarithmspace(log10(delta),log10(epsilon),nb) .+ (1-r); linearspace(1-r-epsilon,1-r+epsilon,nb);
     logarithmspace(log10(epsilon),log10(delta),nb) .+ (1-r); linearspace(1-r+delta,1+r-delta,nb); -logarithmspace(log10(delta),log10(epsilon),nb) .+ (1+r);linearspace(1+r-epsilon,1+r-1e-13,nb)])
     nticks = 14
     xticknames=[L"$10^{-13}$",L"$10^{-12}$",L"$10^{-3}$",L"$r-10^{-3}$",L"$r-10^{-12}$",L"$r+10^{-12}$",L"$r+10^{-3}$",
     L"$1-r-10^{-3}$",L"$1-r-10^{-12}$",L"$1-r+10^{-12}$",L"$1-r+10^{-3}$",L"$1+r-10^{-3}$",L"$1+r-10^{-12}$",L"$1+r-10^{-13}$"]
  else
    bgrid = [r-1+1e-13; logarithmspace(log10(epsilon),log10(delta),nb) .+ (r-1); linearspace(r-1+delta,r-delta,nb);
     -logarithmspace(log10(delta),log10(epsilon),nb) .+ r; linearspace(r-epsilon,r+epsilon,nb); logarithmspace(log10(epsilon),log10(delta),nb) .+ r;
     linearspace(r+delta,r+1-delta,nb); -logarithmspace(log10(delta),log10(epsilon),nb) .+ (r+1); r+1-1e-13]
     nticks = 8
     xticknames=[L"$r-1+10^{-13}$",L"$r-1+10^{-3}$",L"$r-10^{-3}$",L"$r-10^{-12}$",L"$r+10^{-12}$",L"$r+10^{-3}$",
     L"$r+1-10^{-3}$",L"$r+1-10^{-13}$"]
  end
  nbgrid = length(bgrid)
  tp_grad_grid = zeros(nbgrid,n_u+2)
  tp_grad_array= zeros(n_u+2)
  tp_grid = zeros(nbgrid)
  tp_grid_big = zeros(nbgrid)
  tp_grad_grid_num = zeros(nbgrid,n_u+2)
  tp_grad_grid_ana = zeros(nbgrid,n_u+2)
  tp_grad_grid_big = zeros(nbgrid,n_u+2)
  for j=1:nbgrid
    tp=transit_poly(r,bgrid[j],u_n)
    tp_grid[j]=tp
    fgrid[i,j]=tp
    # Now compute with BigFloat finite difference (tests whether derivatives are coded properly):
    tp,tp_grad_array =  transit_poly_grad_num(r,bgrid[j],u_n,1e-18)
    if bgrid[j] == 0.0
      # Assign df/db exactly:
      tp_grad_array[2] =  0.0
    end
    tp_grad_grid_num[j,:] .=tp_grad_array
    tp_grid_big[j,:] .=tp
    # Compute analytic derivatives:
    tp = transit_poly!(r,bgrid[j],u_n,dfdrbu)
    tp_grad_grid_ana[j,:] .=dfdrbu
    # Compute derivatives in BigFloat (tests precision of derivatives):
    tp_big = transit_poly!(big(r),big(bgrid[j]),big.(u_n),dfdrbu_big)
    tp_grad_grid_big[j,:] .=dfdrbu_big
    @test isapprox(dfdrbu,tp_grad_array,atol=1e-6)
    if !isapprox(dfdrbu,tp_grad_array,atol=1e-6)
      println("r: ",r," bgrid: ",bgrid[j])
      println("dfdrbu: ",dfdrbu)
      println("tp_grad_array: ",tp_grad_array)
      println("norm(x-y): ",norm(dfdrbu-tp_grad_array))
      read(STDIN,Char)
    end
    @test isapprox(dfdrbu,dfdrbu_big,atol=1e-8)
  end
  if ~skip_plots
# Now, make plots:
    ax = axes[i]
    y = abs.(asinh.(tp_grid) .-asinh.(tp_grid_big)); mask = y .<= floor; y[mask] .=floor
    ax[:semilogy](y,lw=1,label="flux")
    for n=1:n_u+2
      y = abs.(asinh.(tp_grad_grid_ana[:,n]) .-asinh.(tp_grad_grid_big[:,n])); mask = y .<= floor; y[mask] .=floor
      ax[:semilogy](y,lw=1,label=string(label_name[n]," vs. big"))
      y = abs.(asinh.(tp_grad_grid_ana[:,n]) .-asinh.(tp_grad_grid_num[:,n])); mask = y .<= floor; y[mask] .=floor
      ax[:semilogy](y,lw=1,label=string(label_name[n]," vs. num"))
    end
    if mod(i,3) == 0
      ax[:set_xlabel]("b values")
    end
    ax[:set_ylabel]("Derivative Error")
    ax[:axis]([0,nbgrid,floor,1])
    ax[:set_xticks](nb*linearspace(0,nticks-1,nticks))
    ax[:set_xticklabels](xticknames,rotation=45,fontsize=6)
    ax[:set_title](string("r = ",r0[i]),fontsize=6)
    ax[:legend](loc="upper right",fontsize=6)
  end
end
end
return fgrid
end

max_stars_repo_name: tagordon/limbdark

max_issues_repo_name: tagordon/limbdark

max_forks_repo_name: tagordon/limbdark


text:
using DelayedKalmanFilter
using DelimitedFiles
using Test

@testset "Kalman Filter tests" begin
    loading_path = string(pwd(),"/data/");
    protein_at_observations = readdlm(string(loading_path,"kalman_filter_test_trace_observations.csv"),',');
    model_parameters = [10000.0,5.0,log(2)/30,log(2)/90,1.0,1.0,29.0];
    measurement_variance = 10000;
    a,b,c,d,e,f,g = kalman_filter(protein_at_observations,model_parameters,measurement_variance);

    # check arrays are correct shape
    @test size(a) == (protein_at_observations[end,1] + 1 + model_parameters[end],3)
    @test size(b) == ((protein_at_observations[end,1] + 1 + model_parameters[end])*2,(protein_at_observations[end,1] + 1 + model_parameters[end])*2)
    @test size(c) == (protein_at_observations[end,1] + 1 + model_parameters[end],length(model_parameters),2)
    @test size(d) == (length(model_parameters),(protein_at_observations[end,1] + 1 + model_parameters[end])*2,(protein_at_observations[end,1] + 1 + model_parameters[end])*2)
    @test size(e) == (size(protein_at_observations,1),3)
    @test size(f) == (size(protein_at_observations,1),length(model_parameters),2)
    @test size(g) == (size(protein_at_observations,1),length(model_parameters),2,2)
end

# using ForwardDiff
# log_likelihood_gradient(model_parameters,protein_at_observations,measurement_variance) = ForwardDiff.gradient(model_parameters -> calculate_log_likelihood_at_parameter_point(model_parameters,protein_at_observations,measurement_variance),model_parameters)
# log_likelihood_gradient(model_parameters,protein_at_observations,measurement_variance)


text:
include("./tools.jl");
include("./kalman.jl");
include("./subsampling.jl");
#include("./varima.jl");


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: kinesis_analytics_v2
using AWS.Compat
using AWS.UUIDs
"""
    AddApplicationCloudWatchLoggingOption()

Adds an Amazon CloudWatch log stream to monitor application configuration errors.

# Required Parameters
- `ApplicationName`: The Kinesis Data Analytics application name.
- `CloudWatchLoggingOption`: Provides the Amazon CloudWatch log stream Amazon Resource Name (ARN).
- `CurrentApplicationVersionId`: The version ID of the Kinesis Data Analytics application. You can retrieve the application version ID using DescribeApplication.

"""

add_application_cloud_watch_logging_option(ApplicationName, CloudWatchLoggingOption, CurrentApplicationVersionId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationCloudWatchLoggingOption", Dict{String, Any}("ApplicationName"=>ApplicationName, "CloudWatchLoggingOption"=>CloudWatchLoggingOption, "CurrentApplicationVersionId"=>CurrentApplicationVersionId); aws_config=aws_config)
add_application_cloud_watch_logging_option(ApplicationName, CloudWatchLoggingOption, CurrentApplicationVersionId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationCloudWatchLoggingOption", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CloudWatchLoggingOption"=>CloudWatchLoggingOption, "CurrentApplicationVersionId"=>CurrentApplicationVersionId), args)); aws_config=aws_config)

"""
    AddApplicationInput()

 Adds a streaming source to your SQL-based Kinesis Data Analytics application.  You can add a streaming source when you create an application, or you can use this operation to add a streaming source after you create an application. For more information, see CreateApplication. Any configuration update, including adding a streaming source using this operation, results in a new version of the application. You can use the DescribeApplication operation to find the current application version.

# Required Parameters
- `ApplicationName`: The name of your existing application to which you want to add the streaming source.
- `CurrentApplicationVersionId`: The current version of your application. You can use the DescribeApplication operation to find the current application version.
- `Input`: The Input to add.

"""

add_application_input(ApplicationName, CurrentApplicationVersionId, Input; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationInput", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "Input"=>Input); aws_config=aws_config)
add_application_input(ApplicationName, CurrentApplicationVersionId, Input, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationInput", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "Input"=>Input), args)); aws_config=aws_config)

"""
    AddApplicationInputProcessingConfiguration()

Adds an InputProcessingConfiguration to a SQL-based Kinesis Data Analytics application. An input processor pre-processes records on the input stream before the application's SQL code executes. Currently, the only input processor available is AWS Lambda.

# Required Parameters
- `ApplicationName`: The name of the application to which you want to add the input processing configuration.
- `CurrentApplicationVersionId`: The version of the application to which you want to add the input processing configuration. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `InputId`: The ID of the input configuration to add the input processing configuration to. You can get a list of the input IDs for an application using the DescribeApplication operation.
- `InputProcessingConfiguration`: The InputProcessingConfiguration to add to the application.

"""

add_application_input_processing_configuration(ApplicationName, CurrentApplicationVersionId, InputId, InputProcessingConfiguration; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationInputProcessingConfiguration", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "InputId"=>InputId, "InputProcessingConfiguration"=>InputProcessingConfiguration); aws_config=aws_config)
add_application_input_processing_configuration(ApplicationName, CurrentApplicationVersionId, InputId, InputProcessingConfiguration, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationInputProcessingConfiguration", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "InputId"=>InputId, "InputProcessingConfiguration"=>InputProcessingConfiguration), args)); aws_config=aws_config)

"""
    AddApplicationOutput()

Adds an external destination to your SQL-based Kinesis Data Analytics application. If you want Kinesis Data Analytics to deliver data from an in-application stream within your application to an external destination (such as an Kinesis data stream, a Kinesis Data Firehose delivery stream, or an AWS Lambda function), you add the relevant configuration to your application using this operation. You can configure one or more outputs for your application. Each output configuration maps an in-application stream and an external destination.  You can use one of the output configurations to deliver data from your in-application error stream to an external destination so that you can analyze the errors.   Any configuration update, including adding a streaming source using this operation, results in a new version of the application. You can use the DescribeApplication operation to find the current application version.

# Required Parameters
- `ApplicationName`: The name of the application to which you want to add the output configuration.
- `CurrentApplicationVersionId`: The version of the application to which you want to add the output configuration. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `Output`: An array of objects, each describing one output configuration. In the output configuration, you specify the name of an in-application stream, a destination (that is, a Kinesis data stream, a Kinesis Data Firehose delivery stream, or an AWS Lambda function), and record the formation to use when writing to the destination.

"""

add_application_output(ApplicationName, CurrentApplicationVersionId, Output; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationOutput", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "Output"=>Output); aws_config=aws_config)
add_application_output(ApplicationName, CurrentApplicationVersionId, Output, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationOutput", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "Output"=>Output), args)); aws_config=aws_config)

"""
    AddApplicationReferenceDataSource()

Adds a reference data source to an existing SQL-based Kinesis Data Analytics application. Kinesis Data Analytics reads reference data (that is, an Amazon S3 object) and creates an in-application table within your application. In the request, you provide the source (S3 bucket name and object key name), name of the in-application table to create, and the necessary mapping information that describes how data in an Amazon S3 object maps to columns in the resulting in-application table.

# Required Parameters
- `ApplicationName`: The name of an existing application.
- `CurrentApplicationVersionId`: The version of the application for which you are adding the reference data source. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `ReferenceDataSource`: The reference data source can be an object in your Amazon S3 bucket. Kinesis Data Analytics reads the object and copies the data into the in-application table that is created. You provide an S3 bucket, object key name, and the resulting in-application table that is created.

"""

add_application_reference_data_source(ApplicationName, CurrentApplicationVersionId, ReferenceDataSource; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationReferenceDataSource", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "ReferenceDataSource"=>ReferenceDataSource); aws_config=aws_config)
add_application_reference_data_source(ApplicationName, CurrentApplicationVersionId, ReferenceDataSource, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationReferenceDataSource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "ReferenceDataSource"=>ReferenceDataSource), args)); aws_config=aws_config)

"""
    AddApplicationVpcConfiguration()

Adds a Virtual Private Cloud (VPC) configuration to the application. Applications can use VPCs to store and access resources securely. Note the following about VPC configurations for Kinesis Data Analytics applications:   VPC configurations are not supported for SQL applications.   When a VPC is added to a Kinesis Data Analytics application, the application can no longer be accessed from the Internet directly. To enable Internet access to the application, add an Internet gateway to your VPC.

# Required Parameters
- `ApplicationName`: The name of an existing application.
- `CurrentApplicationVersionId`: The version of the application to which you want to add the VPC configuration. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `VpcConfiguration`: Description of the VPC to add to the application.

"""

add_application_vpc_configuration(ApplicationName, CurrentApplicationVersionId, VpcConfiguration; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationVpcConfiguration", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "VpcConfiguration"=>VpcConfiguration); aws_config=aws_config)
add_application_vpc_configuration(ApplicationName, CurrentApplicationVersionId, VpcConfiguration, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("AddApplicationVpcConfiguration", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "VpcConfiguration"=>VpcConfiguration), args)); aws_config=aws_config)

"""
    CreateApplication()

Creates a Kinesis Data Analytics application. For information about creating a Kinesis Data Analytics application, see Creating an Application.

# Required Parameters
- `ApplicationName`: The name of your application (for example, sample-app).
- `RuntimeEnvironment`: The runtime environment for the application (SQL-1.0, FLINK-1_6, or FLINK-1_8).
- `ServiceExecutionRole`: The IAM role used by the application to access Kinesis data streams, Kinesis Data Firehose delivery streams, Amazon S3 objects, and other external resources.

# Optional Parameters
- `ApplicationConfiguration`: Use this parameter to configure the application.
- `ApplicationDescription`: A summary description of the application.
- `CloudWatchLoggingOptions`: Use this parameter to configure an Amazon CloudWatch log stream to monitor application configuration errors.
- `Tags`: A list of one or more tags to assign to the application. A tag is a key-value pair that identifies an application. Note that the maximum number of application tags includes system tags. The maximum number of user-defined application tags is 50. For more information, see Using Tagging.
"""

create_application(ApplicationName, RuntimeEnvironment, ServiceExecutionRole; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("CreateApplication", Dict{String, Any}("ApplicationName"=>ApplicationName, "RuntimeEnvironment"=>RuntimeEnvironment, "ServiceExecutionRole"=>ServiceExecutionRole); aws_config=aws_config)
create_application(ApplicationName, RuntimeEnvironment, ServiceExecutionRole, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("CreateApplication", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "RuntimeEnvironment"=>RuntimeEnvironment, "ServiceExecutionRole"=>ServiceExecutionRole), args)); aws_config=aws_config)

"""
    CreateApplicationSnapshot()

Creates a snapshot of the application's state data.

# Required Parameters
- `ApplicationName`: The name of an existing application
- `SnapshotName`: An identifier for the application snapshot.

"""

create_application_snapshot(ApplicationName, SnapshotName; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("CreateApplicationSnapshot", Dict{String, Any}("ApplicationName"=>ApplicationName, "SnapshotName"=>SnapshotName); aws_config=aws_config)
create_application_snapshot(ApplicationName, SnapshotName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("CreateApplicationSnapshot", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "SnapshotName"=>SnapshotName), args)); aws_config=aws_config)

"""
    DeleteApplication()

Deletes the specified application. Kinesis Data Analytics halts application execution and deletes the application.

# Required Parameters
- `ApplicationName`: The name of the application to delete.
- `CreateTimestamp`: Use the DescribeApplication operation to get this value.

"""

delete_application(ApplicationName, CreateTimestamp; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplication", Dict{String, Any}("ApplicationName"=>ApplicationName, "CreateTimestamp"=>CreateTimestamp); aws_config=aws_config)
delete_application(ApplicationName, CreateTimestamp, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplication", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CreateTimestamp"=>CreateTimestamp), args)); aws_config=aws_config)

"""
    DeleteApplicationCloudWatchLoggingOption()

Deletes an Amazon CloudWatch log stream from an Kinesis Data Analytics application.

# Required Parameters
- `ApplicationName`: The application name.
- `CloudWatchLoggingOptionId`: The CloudWatchLoggingOptionId of the Amazon CloudWatch logging option to delete. You can get the CloudWatchLoggingOptionId by using the DescribeApplication operation.
- `CurrentApplicationVersionId`: The version ID of the application. You can retrieve the application version ID using DescribeApplication.

"""

delete_application_cloud_watch_logging_option(ApplicationName, CloudWatchLoggingOptionId, CurrentApplicationVersionId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationCloudWatchLoggingOption", Dict{String, Any}("ApplicationName"=>ApplicationName, "CloudWatchLoggingOptionId"=>CloudWatchLoggingOptionId, "CurrentApplicationVersionId"=>CurrentApplicationVersionId); aws_config=aws_config)
delete_application_cloud_watch_logging_option(ApplicationName, CloudWatchLoggingOptionId, CurrentApplicationVersionId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationCloudWatchLoggingOption", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CloudWatchLoggingOptionId"=>CloudWatchLoggingOptionId, "CurrentApplicationVersionId"=>CurrentApplicationVersionId), args)); aws_config=aws_config)

"""
    DeleteApplicationInputProcessingConfiguration()

Deletes an InputProcessingConfiguration from an input.

# Required Parameters
- `ApplicationName`: The name of the application.
- `CurrentApplicationVersionId`: The application version. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `InputId`: The ID of the input configuration from which to delete the input processing configuration. You can get a list of the input IDs for an application by using the DescribeApplication operation.

"""

delete_application_input_processing_configuration(ApplicationName, CurrentApplicationVersionId, InputId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationInputProcessingConfiguration", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "InputId"=>InputId); aws_config=aws_config)
delete_application_input_processing_configuration(ApplicationName, CurrentApplicationVersionId, InputId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationInputProcessingConfiguration", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "InputId"=>InputId), args)); aws_config=aws_config)

"""
    DeleteApplicationOutput()

Deletes the output destination configuration from your SQL-based Kinesis Data Analytics application's configuration. Kinesis Data Analytics will no longer write data from the corresponding in-application stream to the external output destination.

# Required Parameters
- `ApplicationName`: The application name.
- `CurrentApplicationVersionId`: The application version. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `OutputId`: The ID of the configuration to delete. Each output configuration that is added to the application (either when the application is created or later) using the AddApplicationOutput operation has a unique ID. You need to provide the ID to uniquely identify the output configuration that you want to delete from the application configuration. You can use the DescribeApplication operation to get the specific OutputId.

"""

delete_application_output(ApplicationName, CurrentApplicationVersionId, OutputId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationOutput", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "OutputId"=>OutputId); aws_config=aws_config)
delete_application_output(ApplicationName, CurrentApplicationVersionId, OutputId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationOutput", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "OutputId"=>OutputId), args)); aws_config=aws_config)

"""
    DeleteApplicationReferenceDataSource()

Deletes a reference data source configuration from the specified SQL-based Kinesis Data Analytics application's configuration. If the application is running, Kinesis Data Analytics immediately removes the in-application table that you created using the AddApplicationReferenceDataSource operation.

# Required Parameters
- `ApplicationName`: The name of an existing application.
- `CurrentApplicationVersionId`: The current application version. You can use the DescribeApplication operation to get the current application version. If the version specified is not the current version, the ConcurrentModificationException is returned.
- `ReferenceId`: The ID of the reference data source. When you add a reference data source to your application using the AddApplicationReferenceDataSource, Kinesis Data Analytics assigns an ID. You can use the DescribeApplication operation to get the reference ID.

"""

delete_application_reference_data_source(ApplicationName, CurrentApplicationVersionId, ReferenceId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationReferenceDataSource", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "ReferenceId"=>ReferenceId); aws_config=aws_config)
delete_application_reference_data_source(ApplicationName, CurrentApplicationVersionId, ReferenceId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationReferenceDataSource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "ReferenceId"=>ReferenceId), args)); aws_config=aws_config)

"""
    DeleteApplicationSnapshot()

Deletes a snapshot of application state.

# Required Parameters
- `ApplicationName`: The name of an existing application.
- `SnapshotCreationTimestamp`: The creation timestamp of the application snapshot to delete. You can retrieve this value using or .
- `SnapshotName`: The identifier for the snapshot delete.

"""

delete_application_snapshot(ApplicationName, SnapshotCreationTimestamp, SnapshotName; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationSnapshot", Dict{String, Any}("ApplicationName"=>ApplicationName, "SnapshotCreationTimestamp"=>SnapshotCreationTimestamp, "SnapshotName"=>SnapshotName); aws_config=aws_config)
delete_application_snapshot(ApplicationName, SnapshotCreationTimestamp, SnapshotName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationSnapshot", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "SnapshotCreationTimestamp"=>SnapshotCreationTimestamp, "SnapshotName"=>SnapshotName), args)); aws_config=aws_config)

"""
    DeleteApplicationVpcConfiguration()

Removes a VPC configuration from a Kinesis Data Analytics application.

# Required Parameters
- `ApplicationName`: The name of an existing application.
- `CurrentApplicationVersionId`: The current application version ID. You can retrieve the application version ID using DescribeApplication.
- `VpcConfigurationId`: The ID of the VPC configuration to delete.

"""

delete_application_vpc_configuration(ApplicationName, CurrentApplicationVersionId, VpcConfigurationId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationVpcConfiguration", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "VpcConfigurationId"=>VpcConfigurationId); aws_config=aws_config)
delete_application_vpc_configuration(ApplicationName, CurrentApplicationVersionId, VpcConfigurationId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DeleteApplicationVpcConfiguration", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId, "VpcConfigurationId"=>VpcConfigurationId), args)); aws_config=aws_config)

"""
    DescribeApplication()

Returns information about a specific Kinesis Data Analytics application. If you want to retrieve a list of all applications in your account, use the ListApplications operation.

# Required Parameters
- `ApplicationName`: The name of the application.

# Optional Parameters
- `IncludeAdditionalDetails`: Displays verbose information about a Kinesis Data Analytics application, including the application's job plan.
"""

describe_application(ApplicationName; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DescribeApplication", Dict{String, Any}("ApplicationName"=>ApplicationName); aws_config=aws_config)
describe_application(ApplicationName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DescribeApplication", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName), args)); aws_config=aws_config)

"""
    DescribeApplicationSnapshot()

Returns information about a snapshot of application state data.

# Required Parameters
- `ApplicationName`: The name of an existing application.
- `SnapshotName`: The identifier of an application snapshot. You can retrieve this value using .

"""

describe_application_snapshot(ApplicationName, SnapshotName; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DescribeApplicationSnapshot", Dict{String, Any}("ApplicationName"=>ApplicationName, "SnapshotName"=>SnapshotName); aws_config=aws_config)
describe_application_snapshot(ApplicationName, SnapshotName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DescribeApplicationSnapshot", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "SnapshotName"=>SnapshotName), args)); aws_config=aws_config)

"""
    DiscoverInputSchema()

Infers a schema for a SQL-based Kinesis Data Analytics application by evaluating sample records on the specified streaming source (Kinesis data stream or Kinesis Data Firehose delivery stream) or Amazon S3 object. In the response, the operation returns the inferred schema and also the sample records that the operation used to infer the schema.  You can use the inferred schema when configuring a streaming source for your application. When you create an application using the Kinesis Data Analytics console, the console uses this operation to infer a schema and show it in the console user interface.

# Required Parameters
- `ServiceExecutionRole`: The ARN of the role that is used to access the streaming source.

# Optional Parameters
- `InputProcessingConfiguration`: The InputProcessingConfiguration to use to preprocess the records before discovering the schema of the records.
- `InputStartingPositionConfiguration`: The point at which you want Kinesis Data Analytics to start reading records from the specified streaming source discovery purposes.
- `ResourceARN`: The Amazon Resource Name (ARN) of the streaming source.
- `S3Configuration`: Specify this parameter to discover a schema from data in an Amazon S3 object.
"""

discover_input_schema(ServiceExecutionRole; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DiscoverInputSchema", Dict{String, Any}("ServiceExecutionRole"=>ServiceExecutionRole); aws_config=aws_config)
discover_input_schema(ServiceExecutionRole, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("DiscoverInputSchema", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServiceExecutionRole"=>ServiceExecutionRole), args)); aws_config=aws_config)

"""
    ListApplicationSnapshots()

Lists information about the current application snapshots.

# Required Parameters
- `ApplicationName`: The name of an existing application.

# Optional Parameters
- `Limit`: The maximum number of application snapshots to list.
- `NextToken`: Use this parameter if you receive a NextToken response in a previous request that indicates that there is more output available. Set it to the value of the previous call's NextToken response to indicate where the output should continue from.
"""

list_application_snapshots(ApplicationName; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("ListApplicationSnapshots", Dict{String, Any}("ApplicationName"=>ApplicationName); aws_config=aws_config)
list_application_snapshots(ApplicationName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("ListApplicationSnapshots", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName), args)); aws_config=aws_config)

"""
    ListApplications()

Returns a list of Kinesis Data Analytics applications in your account. For each application, the response includes the application name, Amazon Resource Name (ARN), and status.  If you want detailed information about a specific application, use DescribeApplication.

# Optional Parameters
- `Limit`: The maximum number of applications to list.
- `NextToken`: If a previous command returned a pagination token, pass it into this value to retrieve the next set of results. For more information about pagination, see Using the AWS Command Line Interface's Pagination Options.
"""

list_applications(; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("ListApplications"; aws_config=aws_config)
list_applications(args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("ListApplications", args; aws_config=aws_config)

"""
    ListTagsForResource()

Retrieves the list of key-value tags assigned to the application. For more information, see Using Tagging.

# Required Parameters
- `ResourceARN`: The ARN of the application for which to retrieve tags.

"""

list_tags_for_resource(ResourceARN; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("ListTagsForResource", Dict{String, Any}("ResourceARN"=>ResourceARN); aws_config=aws_config)
list_tags_for_resource(ResourceARN, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("ListTagsForResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceARN"=>ResourceARN), args)); aws_config=aws_config)

"""
    StartApplication()

Starts the specified Kinesis Data Analytics application. After creating an application, you must exclusively call this operation to start your application.

# Required Parameters
- `ApplicationName`: The name of the application.
- `RunConfiguration`: Identifies the run configuration (start parameters) of a Kinesis Data Analytics application.

"""

start_application(ApplicationName, RunConfiguration; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("StartApplication", Dict{String, Any}("ApplicationName"=>ApplicationName, "RunConfiguration"=>RunConfiguration); aws_config=aws_config)
start_application(ApplicationName, RunConfiguration, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("StartApplication", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "RunConfiguration"=>RunConfiguration), args)); aws_config=aws_config)

"""
    StopApplication()

Stops the application from processing data. You can stop an application only if it is in the running state. You can use the DescribeApplication operation to find the application state.

# Required Parameters
- `ApplicationName`: The name of the running application to stop.

# Optional Parameters
- `Force`: Set to true to force the application to stop. If you set Force to true, Kinesis Data Analytics stops the application without taking a snapshot. You can only force stop a Flink-based Kinesis Data Analytics application. You can't force stop a SQL-based Kinesis Data Analytics application. The application must be in the STARTING, UPDATING, STOPPING, AUTOSCALING, or RUNNING state.
"""

stop_application(ApplicationName; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("StopApplication", Dict{String, Any}("ApplicationName"=>ApplicationName); aws_config=aws_config)
stop_application(ApplicationName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("StopApplication", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName), args)); aws_config=aws_config)

"""
    TagResource()

Adds one or more key-value tags to a Kinesis Data Analytics application. Note that the maximum number of application tags includes system tags. The maximum number of user-defined application tags is 50. For more information, see Using Tagging.

# Required Parameters
- `ResourceARN`: The ARN of the application to assign the tags.
- `Tags`: The key-value tags to assign to the application.

"""

tag_resource(ResourceARN, Tags; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("TagResource", Dict{String, Any}("ResourceARN"=>ResourceARN, "Tags"=>Tags); aws_config=aws_config)
tag_resource(ResourceARN, Tags, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("TagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceARN"=>ResourceARN, "Tags"=>Tags), args)); aws_config=aws_config)

"""
    UntagResource()

Removes one or more tags from a Kinesis Data Analytics application. For more information, see Using Tagging.

# Required Parameters
- `ResourceARN`: The ARN of the Kinesis Data Analytics application from which to remove the tags.
- `TagKeys`: A list of keys of tags to remove from the specified application.

"""

untag_resource(ResourceARN, TagKeys; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("UntagResource", Dict{String, Any}("ResourceARN"=>ResourceARN, "TagKeys"=>TagKeys); aws_config=aws_config)
untag_resource(ResourceARN, TagKeys, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("UntagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceARN"=>ResourceARN, "TagKeys"=>TagKeys), args)); aws_config=aws_config)

"""
    UpdateApplication()

Updates an existing Kinesis Data Analytics application. Using this operation, you can update application code, input configuration, and output configuration.  Kinesis Data Analytics updates the ApplicationVersionId each time you update your application.   You cannot update the RuntimeEnvironment of an existing application. If you need to update an application's RuntimeEnvironment, you must delete the application and create it again.

# Required Parameters
- `ApplicationName`: The name of the application to update.
- `CurrentApplicationVersionId`: The current application version ID. You can retrieve the application version ID using DescribeApplication.

# Optional Parameters
- `ApplicationConfigurationUpdate`: Describes application configuration updates.
- `CloudWatchLoggingOptionUpdates`: Describes application Amazon CloudWatch logging option updates. You can only update existing CloudWatch logging options with this action. To add a new CloudWatch logging option, use AddApplicationCloudWatchLoggingOption.
- `RunConfigurationUpdate`: Describes updates to the application's starting parameters.
- `ServiceExecutionRoleUpdate`: Describes updates to the service execution role.
"""

update_application(ApplicationName, CurrentApplicationVersionId; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("UpdateApplication", Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId); aws_config=aws_config)
update_application(ApplicationName, CurrentApplicationVersionId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = kinesis_analytics_v2("UpdateApplication", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ApplicationName"=>ApplicationName, "CurrentApplicationVersionId"=>CurrentApplicationVersionId), args)); aws_config=aws_config)


text:
"""
    function sim_QTL(base, nqtl...; d = Laplace())
---
Given the population, a dictionary of SNP loci and SNP genotypes,
this function tries to:
1. sample QTL from the loci
2. sampe QTL effects
   - by default from `Laplace()`
   - an alternative is `Γ(0.4, 1)`,
   - or from `N(0, 1)`, if `shape < 0`
   - effects are scaled such that σₐ² of base = 1.
3. the potential, or the upper limit of the population, or the ideal population is also calculated.

Note:
- A problem with `Γ(0.4, 1)` is that two traits will have an expectation correlation ~0.2.
- Randomly flip the sign of the effects may be a solution.
"""
function sim_QTL(base, nqtl...; d = Laplace(), ϵ = 1e-5)
    @debug "Sample QTL locations and effects" "With the QTL effects, mean ≈ 0, var(BV) ≈ 1"
    pos = base.pos
    GT  = base.hap              # short hands
    tsnp = size(pos)[1]
    ntrt = length(nqtl)

    qinfo = QTL[]
    for n in nqtl
        loci = sort(randperm(tsnp)[1:n])
        Q  = qtl_gt(GT, loci)
        a = rand(d, n) .* rand([-1, 1], n) # random effect directions
        y = Q'a
        m, s = mean(y), std(y)
        while abs(m) > ϵ || abs(s-1) > ϵ
            a .-= 2m/n
            a ./= s
            y = Q'a
            m, s = mean(y), std(y)
        end
        push!(qinfo, QTL(loci, a))
    end
    qinfo
end

"""
    function qtl_gt(snp, qtl)
---
Given population genotypes `snp`, this function returns the QTL genotypes `Q`,
according to the loci specified in `qtl`.
"""
function qtl_gt(snp, qtl)
    nid = size(snp)[2] ÷ 2
    nlc = length(qtl)
    Q = zeros(Int8, nlc, nid)   # QTL genotypes 0/1 => 0, 1, 2
    for id in 1:nid
        Q[:, id] = snp[qtl, id*2 - 1] + snp[qtl, id*2]
    end
    Q
end

"""
    function sim_pt_QTL(base, nqtl, d)
---
Simulation of pleiotropic QTL, where `pt` is a shorthand of pleiotropic.
That is, sample QTL for two traits that are all of the same positions.
The effects of this set of QTL for the two traits,
however, are correlated.
For example, they can be reasonably negative correlated.

An example of `d` is `MvNormal(zeros(2), [1 0; 0 1.])`.
"""
function sim_pt_QTL(base, nqtl, d)
    @debug "Sample pleiotropic QTL for two trait" "QTL effects are define by `d`"
    pos = base.pos
    GT  = base.hap              # short hands
    tsnp = size(pos)[1]
    qinfo = QTL[]
    # two traits share below QTL
    lqtl = sort(randperm(tsnp)[1:nqtl])
    eqtl = rand(d, nqtl)'       # effects for each trait in column
    Q = qtl_gt(GT, lqtl)        # QTL genotypes
    for i in 1:2
        a = eqtl[:, i]
        y = Q'a
        m = mean(y)             # base population mean
        v = var(y)
        a ./= sqrt(v)
        push!(qinfo, QTL(lqtl, a))
    end
    qinfo
end


text:
using Test

include("../src/REISE.jl")

@testset "gencost linearization" begin
    include("./test_linearize_gencost.jl")
end


text:
module NMF
    using StatsBase
    using Statistics
    using Printf
    using LinearAlgebra

    export nnmf

    include("common.jl")
    include("utils.jl")

    include("initialization.jl")
    include("multupd.jl")
    include("projals.jl")
    include("alspgrad.jl")
    include("coorddesc.jl")

    include("interf.jl")

end # module


text:
include("make.jl")

deploydocs(
    repo = "github.com/DCMLab/Pitches.jl.git",
    devbranch = "main",
)


text:
importall Bukdu

# from phoenix/test/phoenix/router/scope_test.exs

module Api
module V1

importall Bukdu

type UserController <: ApplicationController
end

show(::UserController) = "api v1 users show"
delete(::UserController) = "api v1 users delete"
edit(::UserController) = "api v1 users edit"
foo_host(c::UserController) ="foo request from $(c[:host])"
baz_host(c::UserController) = "baz request from $(c[:host])"

type VenueController <: ApplicationController
end

end # module Api.V1
end # module Api

Base.rem(k::Symbol, v::String) = Dict{Symbol,Any}(k=>v)
Router() do
    foo_host = Api.V1.foo_host
    baz_host = Api.V1.baz_host

    scope("/admin", host= "baz.") do
        get("/users/:id", Api.V1.UserController, baz_host)
    end

    scope(host= "foobar.com") do
        scope("/admin") do
            get("/users/:id", Api.V1.UserController, foo_host)
        end
    end

    scope("/admin") do
        get("/users/:id", Api.V1.UserController, show)
    end

    scope("/api") do
        scope("/v1") do
            get("/users/:id", Api.V1.UserController, show)
        end
    end

    scope("/api", Api, private= %(:private_token, "foo")) do
        V1 = Api.V1
        get("/users", V1.UserController, show)
        get("/users/:id", V1.UserController, show, private= %(:private_token, "bar"))

        scope("/v1", alias= V1) do
            UserController = V1.UserController
            resources("/users", UserController, only= [delete], private= %(:private_token, "baz"))
        end
    end

    scope("/assigns", Api, assigns= %(:assigns_token, "foo")) do
        V1 = Api.V1
        get("/users", V1.UserController, show)
        get("/users/:id", V1.UserController, show, assigns= %(:assigns_token, "bar"))

        scope("/v1", alias= V1) do
            UserController = V1.UserController
            resources("/users", UserController, only= [delete], assigns= %(:assigns_token, "baz"))
        end
    end

    scope("/host", host= "baz.") do
        get("/users/:id", Api.V1.UserController, baz_host)
    end

    scope(host= "foobar.com") do
        scope("/host") do
            get("/users/:id", Api.V1.UserController, foo_host)
        end
    end

    scope("/api") do
        scope("/v1", Api) do
            V1 = Api.V1
            resources("/venues", V1.VenueController, only= [show], alias= V1) do
                UserController = V1.UserController
                resources("/users", UserController, only= [edit])
            end
        end
    end
end

Endpoint() do
    plug(Plug.Logger, level=:error)
    plug(Router)
end

Logger.set_level(false)

using Base.Test

@testset "single scope for single routes" begin
    conn = (Router)(get, "/admin/users/1")
    @test conn.status == 200
    @test conn.resp_body == "api v1 users show"
    @test conn.params["id"] == "1"

    conn = (Router)(get, "/api/users/13")
    @test conn.status == 200
    @test conn.resp_body == "api v1 users show"
    @test conn.params["id"] == "13"
end

@testset "double scope for single routes" begin
    conn = (Router)(get, "/api/v1/users/1")
    @test conn.status == 200
    @test conn.resp_body == "api v1 users show"
    @test conn.params["id"] == "1"
end

@testset "scope for resources" begin
    conn = (Router)(delete, "/api/v1/users/12")
    @test conn.status == 200
    @test conn.resp_body == "api v1 users delete"
    @test conn.params["id"] == "12"
end

@testset "scope for double nested resources" begin
    conn = (Router)(get, "/api/v1/venues/12/users/13/edit")
    @test conn.status == 200
    @test conn.resp_body == "api v1 users edit"
    @test conn.params["venue_id"] == "12"
    @test conn.params["id"] == "13"
end

@testset "host scopes routes based on conn.host" begin
    conn = (Router)(get, "http://foobar.com/admin/users/1")
    @test conn.status == 200
    @test conn.resp_body == "foo request from foobar.com"
    @test conn.params["id"] == "1"
end

@testset "host scopes allows partial host matching" begin
    conn = (Router)(get, "http://baz.bing.com/admin/users/1")
    @test conn.status == 200
    @test conn.resp_body == "baz request from baz.bing.com"

    conn = (Router)(get, "http://baz.pang.com/admin/users/1")
    @test conn.status == 200
    @test conn.resp_body == "baz request from baz.pang.com"
end

@testset "host 404s when failed match" begin
    conn = (Router)(get, "http://foobar.com/host/users/1")
    @test conn.status == 200

    conn = (Router)(get, "http://baz.pang.com/host/users/1")
    @test conn.status == 200

    @test_throws NoRouteError (Router)(get, "http://foobar.com.br/host/users/1")
    @test_throws NoRouteError (Router)(get, "http://ba.pang.com/host/users/1")
end

@testset "private data in scopes" begin
    conn = (Router)(get, "/api/users")
    @test conn.status == 200
    @test conn.private[:private_token] == "foo"

    conn = (Router)(get, "/api/users/13")
    @test conn.status == 200
    @test conn.private[:private_token] == "bar"

    conn = (Router)(delete, "/api/v1/users/13")
    @test conn.status == 200
    @test conn.private[:private_token] == "baz"
  end

@testset "assigns data in scopes" begin
    conn = (Router)(get, "/assigns/users")
    @test conn.status == 200
    @test conn.assigns[:assigns_token] == "foo"

    conn = (Router)(get, "/assigns/users/13")
    @test conn.status == 200
    @test conn.assigns[:assigns_token] == "bar"

    conn = (Router)(delete, "/assigns/v1/users/13")
    @test conn.status == 200
    @test conn.assigns[:assigns_token] == "baz"
end


text:
#!/usr/bin/env julia
##
# julia --compile=all day09_b.jl

doteach = 20000 #1000 #250000
start_tm = round(time())

function assertmsg(assertion, msg)
  if !assertion
    println("assert-ERROR: $msg")
    exit(1)
  else
    println("assert-OK: $msg")
  end
end

# format thousands
#  see <
function fts(i)
  #return reverse(join('_', unpack("(A3)*", reverse(shift()))));
  string(">", i, "<")
end

## expects string, returns decoded string
function process(inp)
  rest = inp
  outpBuf = IOBuffer()
  loop = 0
  #println("  input=$inp")
  regex = r"^(.*?)\((\d+)x(\d+)\)(.*)$"
  last_llen = 0; last_rlen = 0
  start_tm = round(time())
  last_tm = start_tm
  m = match(regex, rest)
  while m != nothing
    #println("regex matched for input $inp, loop=$loop")
    pre, len, rept, remain = m.captures
    #println(string("mod=", mod(loop, doteach)))
    if mod(loop, doteach) == 0
      llen = -1; rlen = length(rest)
      tm = round(time())
      println(string(
        ". left-len=$(fts(llen)); right-len=$(fts(rlen))",
        "; dlt-left=$(fts(llen-last_llen)); dlt-right=$(fts(rlen-last_rlen))",
        "; time=$(tm-last_tm)",
        "; sumTM=$(fts(tm-start_tm))",
        "; loops=$(fts(loop))"
      ))
      last_llen = llen; last_rlen = rlen; last_tm = tm
    end
    loop += 1
    leni = parse(Int64, len)
    repti = parse(Int64, rept)
    #println("  found pre=$pre, cmd(len=$len, repeat=$rept), remain=$remain")
      #outp = string(outp, pre)
    print(outpBuf, pre)
    #println("  outp=$outp; prepended")
    #println("  torepeat=$torepeat; insrt=$insrt")
    #outp = string(outp, insrt)
    #println("  outp=$outp; inserted")
    rest = string(repeat(remain[1:leni], repti), remain[leni+1:end])
    #println("  new rest=$rest")
    m = match(regex, rest)
  end
    #outp = string(outp, rest)
  print(outpBuf, rest)
  takebuf_string(outpBuf)
end

#** MAIN

assertmsg(true, "success message")
#assertmsg(false, "fail message")
#assertmsg(true, "success message")

inp = "ABC"
expct = "ABC"
res = process(inp)
assertmsg(res == expct, "result $res of input $inp should be $expct")

tests = [
  ["(3x3)XYZ" "XYZXYZXYZ"];
  ["X(8x2)(3x3)ABCY" "XABCABCABCABCABCABCY"];
  ["(27x12)(20x12)(13x14)(7x10)(1x12)A"  repeat("A", 241920)]
]

println(typeof(tests))
println(string(tests[1,1], " => ", tests[1,2]))
for i = 1:size(tests,1)
  println(string(tests[i,1], " => ", tests[i,2]))

  inp = tests[i,1]
  expct = tests[i,2]
  res = process(inp)
  assertmsg(res == expct, "result $res of input $inp should be $expct")
  println(string("TestRESult-length=", length(res)))
end

start_tm = round(time())
println("started $start_tm")

open("day09_data.txt") do f
  global data = readstring(f)
  #println("data-file=>$data<")
end
data = strip(data)
#println("INPUT data-str=>$data<")
println(string("INPUT data-str=>", data[1:16], "...<"))
res = process(data)
println(string("RESult=>", res[1:16], "...<"))
println(string("RESult length = ", length(res)))


text:
# Autogenerated wrapper script for GMP_jll for i686-w64-mingw32-cxx11
export libgmp, libgmpxx

JLLWrappers.@generate_wrapper_header("GMP")
JLLWrappers.@declare_library_product(libgmp, "libgmp-10.dll")
JLLWrappers.@declare_library_product(libgmpxx, "libgmpxx-4.dll")
function __init__()
    JLLWrappers.@generate_init_header()
    JLLWrappers.@init_library_product(
        libgmp,
        "bin\\libgmp-10.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libgmpxx,
        "bin\\libgmpxx-4.dll",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
module Assemblers

using SparseArrays: sparse
using LinearAlgebra
using ..LocalAssemblers: LocalMatrixAssembler, init!

"""
    AbstractSysmatAssembler

Abstract type of system-matrix assembler.
"""
abstract type AbstractSysmatAssembler end;

"""
    SysmatAssemblerSparse{T<:Number} <: AbstractSysmatAssembler

Type for assembling a sparse global matrix from individual entries.
"""
mutable struct SysmatAssemblerSparse{T<:Number} <: AbstractSysmatAssembler
    row::Vector{Int64}
    col::Vector{Int64}
    val::Vector{T}
    nrow::Int64; ncol::Int64;
end

"""
    SysmatAssemblerSparse(zero::T=0.0) where {T<:Number}

Construct blank system matrix assembler. The matrix entries are of type `T`.

# Example

This is how a sparse matrix is assembled from two rectangular dense matrices.
```
a = SysmatAssemblerSparse(0.0)
start!(a, 7, 7)
m = [0.24406   0.599773    0.833404  0.0420141
0.786024  0.00206713  0.995379  0.780298
0.845816  0.198459    0.355149  0.224996]
gi = [1 7 5]
gj = [5 2 1 4]
for j in 1:size(m, 2), i in 1:size(m, 1)
    assemble!(a, gi[i], gj[j], m[i, j])
end
m = [0.146618  0.53471   0.614342    0.737833
0.479719  0.41354   0.00760941  0.836455
0.254868  0.476189  0.460794    0.00919633
0.159064  0.261821  0.317078    0.77646
0.643538  0.429817  0.59788     0.958909]
gi =  [2 3 1 7 5]
gj = [6 7 3 4]
for j in 1:size(m, 2), i in 1:size(m, 1)
    assemble!(a, gi[i], gj[j], m[i, j])
end
A = finish!(a)
```
"""
function SysmatAssemblerSparse(zero::T=0.0) where {T<:Number}
    return SysmatAssemblerSparse{T}(fill(0, 0), fill(0, 0), fill(zero, 0), 0, 0)
end

"""
    start!(self::SysmatAssemblerSparse{T}, nrow, ncol) where {T<:Number}

Start the assembly of a global matrix.
"""
function start!(self::SysmatAssemblerSparse{T}, nrow, ncol) where {T<:Number}
    nexpected = Int64(round(0.00001 * max(nrow, ncol, 10000)^2))
    self.row = fill(0, 0)
    sizehint!(self.row, nexpected)
    self.col = fill(0, 0)
    sizehint!(self.col, nexpected)
    self.val = fill(zero(T), 0)
    sizehint!(self.val, nexpected)
    self.nrow = nrow;
    self.ncol = ncol;
    return self
end

"""
    assemble!(self::SysmatAssemblerSparse{T}, r, c, v::T) where {T<:Number}

Assemble a single entry of a rectangular matrix.
"""
function assemble!(self::SysmatAssemblerSparse{T}, r, c, v::T) where {T<:Number}
    push!(self.row, r)
    push!(self.col, c)
    push!(self.val, v)
    return self
end

"""
    assemble!(self::SysmatAssemblerSparse{T}, lma::LocalMatrixAssembler{IT, T}) where {IT<:Integer, T<:Number}

Assemble the row numbers, column numbers, and values from a local assembler.
"""
function assemble!(self::SysmatAssemblerSparse{T}, lma::LocalMatrixAssembler{IT, T}) where {IT<:Integer, T<:Number}
    append!(self.row, lma.row)
    append!(self.col, lma.col)
    append!(self.val, lma.M)
    return self
end

"""
    assemble!(self::SysmatAssemblerSparse{T}, lma::Transpose{T,LocalMatrixAssembler{IT,T}}) where {IT<:Integer, T<:Number}

Assemble the row numbers, column numbers, and values from a local assembler.
"""
function assemble!(self::SysmatAssemblerSparse{T}, lma::Transpose{T,LocalMatrixAssembler{IT,T}}) where {IT<:Integer, T<:Number}
    append!(self.row, transpose(lma.parent.col))
    append!(self.col, transpose(lma.parent.row))
    append!(self.val, transpose(lma.parent.M))
    return self
end

"""
    finish!(self::SysmatAssemblerSparse)

Make a sparse matrix.
"""
function finish!(self::SysmatAssemblerSparse)
    return sparse(self.row, self.col, self.val, self.nrow, self.ncol);
end

"""
    AbstractSysvecAssembler

Abstract type of system vector assembler.
"""
abstract type AbstractSysvecAssembler end;

"""
    start!(self::SV,  nrow) where {SV<:AbstractSysvecAssembler, T<:Number}

Start assembly.

The method makes the buffer for the vector assembly. It must be called before
the first call to the method assemble.

`nrow`= Total number of degrees of freedom.
"""
function start!(self::SV,  nrow) where {SV<:AbstractSysvecAssembler, T<:Number}
end

"""
    assemble!(self::SV, i, val::T) where {SV<:AbstractSysvecAssembler, T<:Number}

Assemble an elementwise vector.

The method assembles a column element vector using the vector of degree of
freedom numbers for the rows.
"""
function assemble!(self::SV, i, val::T) where {SV<:AbstractSysvecAssembler, T<:Number}
end

"""
    makevector!(self::SysvecAssembler)

Make the global vector.
"""
function finish!(self::SV) where {SV<:AbstractSysvecAssembler}
end

"""
    SysvecAssembler

Assembler for the system vector.
"""
mutable struct SysvecAssembler{T<:Number} <: AbstractSysvecAssembler
    val::Vector{T};
    ndofs::Int64
end

"""
    SysvecAssembler(zero::T=0.0) where {T<:Number}

Construct blank system vector assembler. The vector entries are of type `T`.
"""
function SysvecAssembler(zero::T=0.0) where {T<:Number}
    return SysvecAssembler([zero], 0)
end

"""
    start!(self::SysvecAssembler{T},  nrow::Int64) where {T<:Number}

Start assembly.

The method makes the buffer for the vector assembly. It must be called before
the first call to the method assemble.

`nrow`= Total number of degrees of freedom.
"""
function start!(self::SysvecAssembler{T},  nrow::Int64) where {T<:Number}
    self.ndofs = nrow
    self.val = zeros(T, self.ndofs);
    return self
end

"""
    assemble!(self::SysvecAssembler{T}, i, val::T) where {T<:Number}

Assemble a single value into the row `i`.
"""
function assemble!(self::SysvecAssembler{T}, i, val::T) where {T<:Number}
    self.val[i] = self.val[i] + val;
    return self
end

"""
    assemble!(self::SysvecAssembler{T}, lva) where {T<:Number}

Assemble local vector assembler.
"""
function assemble!(self::SysvecAssembler{T}, lva) where {T<:Number}
    for i in 1:length(lva.row)
        gi = lva.row[i]
        self.val[gi] += lva.V[i];
    end
    return self
end

"""
    finish!(self::SysvecAssembler)

Make the global vector.
"""
function finish!(self::SysvecAssembler)
  return deepcopy(self.val);
end

end


text:
@debugcb @cxxdereference function jl_managedAccounts(x::IBString)
    return nothing
end

@debugcb @cxxdereference function jl_accountSummary(
    reqId::CxxInt, account::IBString, tag::IBString, value::IBString, currency::IBString
)
    return nothing
end

@debugcb @cxxdereference function jl_accountSummaryEnd(reqId::CxxInt)
    return nothing
end

function JLClient()
    return JLClient(
        @cfunction(jl_managedAccounts, Nothing, (IBString,)),
        @cfunction(jl_accountSummary, Nothing, (CxxInt, IBString, IBString, IBString, IBString)),
        @cfunction(jl_accountSummaryEnd, Nothing, (CxxInt, )),
    )
end


text:
# returns solution of part 1
function x()
	f = open("13.txt")
	n = parse(Int, readline(f))
	l = readline(f)
	min = 0
	found = false
	minline = 0
	for line in split(l, ",")
		m = 0
		try
			m = parse(Int, line)
		catch
			continue
		end
		q = n ÷ m + 1
		if !found
			min = q*m
			minline = m
			found = true
		elseif q*m < min
			min = q*m
			minline = m
		end
	end
	(min - n) * minline
end

# prints solution of part 2
function y()
	f = open("13.txt")
	n = parse(Int, readline(f))
	l = readline(f)
	t = split(l, ",")
	time = 1
	maxok = 0
	incr = 1
	while true
		ok = true
		for i = 1:length(t)
			if t[i] == "x"
				continue
			end
			id = parse(Int, t[i])
			if (time+i) % id != 0
				ok = false
				break
			elseif i > maxok
				incr *= id
				println(time+1, " ", id, " OK")
				maxok = i
			end
		end
		if ok
			println(time+1)
			break
		end
		time += incr
	end
end


text:
module CASIM

# force recompiling by deleting the cached lib
# use /home/lisheng/.julia/lib/v0.6 on SISL server
# if ispath("/Users/shengli/.julia/lib/v0.6/CAS_QMDP.ji")
# 	rm("/Users/shengli/.julia/lib/v0.6/CAS_QMDP.ji")
# end
# if ispath("/Users/shengli/.julia/lib/v0.6/DoubleUAVs_module.ji")
# 	rm("/Users/shengli/.julia/lib/v0.6/DoubleUAVs_module.ji")
# end
# if ispath("/Users/shengli/.julia/lib/v0.6/Simulation_Env_module.ji")
# 	rm("/Users/shengli/.julia/lib/v0.6/Simulation_Env_module.ji")
# end

push!(LOAD_PATH, "../src")

using CAS_module
using Aircraft_module
using Airspace_Control_module
using Stats_module
using Constants_module

push!(LOAD_PATH, "../../DeepCorrection")
using DeepQLearning
push!(LOAD_PATH, "../../DeepCorrection/src")
using Simulation_Env_module

export Dynamics, Observation, Sensor, Aircraft
export Nearest_Intruder_Tracker, Multi_Threat_Tracker

export Airspace_Controller, Constant_Number_Controller, Constant_Spawn_Rate_Controller
export CAS_Property_Controller, Uniform_CAS, Random_CAS
export Sensor_Property_Controller, Uniform_Sensor, Random_Sensor
export airspace_initialization, airspace_control

export CAS
export ACAS_Xu_Network, Simple_CAS, No_CAS, VICAS, Corrected_VICAS, VICASWeighted
export CAS_DICT
export acas_xu_network_data

export Stats, run_stats

export get_advisory
export get_observation
export update_all_dynamics

export sensing_range
export airspace_dim
export dest_range
export collision_range

export Render_Option, Render_Airspace, Not_Render

export simulate

export correction_version, correction_type, correction_weight, VICAS_policyname, policy

"""
	convert ft to m
"""
function ft2m(ft::Float64)
	return ft * 0.3048
end

include("update_dynamics.jl")
include("check_status.jl")
include("get_advisory.jl")
include("get_observation.jl")
include("write_stats.jl")
include("render.jl")
# include("plots_animation.jl")

function simulate(
	exp::AbstractString,
	scheme::AbstractString,
	sim_horizon::Int64,
	airspace_controller::Airspace_Controller,
	cas_ctrl::CAS_Property_Controller,
	sensor_ctrl::Sensor_Property_Controller,
	render_option::Render_Option;
	correction::Bool=false)

	T = time()
	stats = Stats(T)
	t = 0
	All_AC = airspace_initialization(stats, airspace_controller.ac_number, cas_ctrl, sensor_ctrl, t)

	for t = 1 : sim_horizon
		render_airspace(t, render_option, All_AC)
		# Step:
		if t % 1000 == 0
			println(string(t) * " : " * string(length(All_AC)))
		end
		update_all_dynamics(All_AC, correction=correction)
		run_stats(All_AC, stats, t)
		All_AC = airspace_control(stats, All_AC, airspace_controller, cas_ctrl, sensor_ctrl, t)
	end
	write_stats(exp, scheme, stats, sim_horizon, airspace_controller, cas_ctrl, sensor_ctrl)

end

end # module

max_stars_repo_name: parachutel/DeepCorrectionCAS


text: if true 1 else if true 2 else 3 end end

max_stars_repo_name: gabriel-doriath-dohler/pjulia

max_issues_repo_name: gabriel-doriath-dohler/pjulia

max_forks_repo_name: gabriel-doriath-dohler/pjulia


text:
include(joinpath(pwd(), "examples/implicit_dynamics/utils.jl"))

"""
    Double pendulum
"""

struct DoublePendulum{T}
    dim::Dimensions

    m1::T    # mass link 1
    J1::T    # inertia link 1
    l1::T    # length link 1
    lc1::T   # length to COM link 1

    m2::T    # mass link 2
    J2::T    # inertia link 2
    l2::T    # length link 2
    lc2::T   # length to COM link 2

    g::T     # gravity

    b1::T    # joint friction
    b2::T
end

lagrangian(model::DoublePendulum, q, q̇) = 0.0

function kinematics(model::DoublePendulum, x)
    @SVector [model.l1 * sin(x[1]) + model.l2 * sin(x[1] + x[2]),
              -1.0 * model.l1 * cos(x[1]) - model.l2 * cos(x[1] + x[2])]
end

function kinematics_elbow(model::DoublePendulum, x)
    @SVector [model.l1 * sin(x[1]),
              -1.0 * model.l1 * cos(x[1])]
end

function M_func(model::DoublePendulum, x)
    a = (model.J1 + model.J2 + model.m2 * model.l1 * model.l1
         + 2.0 * model.m2 * model.l1 * model.lc2 * cos(x[2]))

    b = model.J2 + model.m2 * model.l1 * model.lc2 * cos(x[2])

    c = model.J2

    @SMatrix [a b;
              b c]
end

function τ_func(model::DoublePendulum, x)
    a = (-1.0 * model.m1 * model.g * model.lc1 * sin(x[1])
         - model.m2 * model.g * (model.l1 * sin(x[1])
         + model.lc2 * sin(x[1] + x[2])))

    b = -1.0 * model.m2 * model.g * model.lc2 * sin(x[1] + x[2])

    @SVector [a,
              b]
end

function c_func(model::DoublePendulum, q, q̇)
    a = -2.0 * model.m2 * model.l1 * model.lc2 * sin(q[2]) * q̇[2]
    b = -1.0 * model.m2 * model.l1 * model.lc2 * sin(q[2]) * q̇[2]
    c = model.m2 * model.l1 * model.lc2 * sin(q[2]) * q̇[1]
    d = 0.0

    @SMatrix [a b;
              c d]
end

function B_func(model::DoublePendulum, x)
    @SMatrix [0.0;
              1.0]
end

function C_func(model::DoublePendulum, q, q̇)
    c_func(model, q, q̇) * q̇ - τ_func(model, q)
end

function ϕ_func(model, q)
    # SVector{model.dim.c}([q[1], 0.5 * π - q[2], q[2] + 0.5 * π])
    SVector{model.dim.c}([0.5 * π - q[2], q[2] + 0.5 * π])
end

function P_func(model, q)
    ϕ(z) = ϕ_func(model, z)
    ForwardDiff.jacobian(ϕ, q)
end

model = DoublePendulum(Dimensions(2, 1, 0, 2),
    1.0, 0.333, 1.0, 0.5, 1.0, 0.333, 1.0, 0.5, 9.81, 0.0, 0.0)

model_no_impact = DoublePendulum(Dimensions(2, 1, 0, 0),
    1.0, 0.333, 1.0, 0.5, 1.0, 0.333, 1.0, 0.5, 9.81, 0.0, 0.0)

function lagrangian_derivatives(model::DoublePendulum, q, v)
	D1L = -1.0 * C_func(model, q, v)
    D2L = M_func(model, q) * v
	return D1L, D2L
end

function dynamics(model::DoublePendulum, h, q0, q1, u1, λ1, q2)
	# evalutate at midpoint
	qm1 = 0.5 * (q0 + q1)
    vm1 = (q1 - q0) / h[1]
    qm2 = 0.5 * (q1 + q2)
    vm2 = (q2 - q1) / h[1]

	D1L1, D2L1 = lagrangian_derivatives(model, qm1, vm1)
	D1L2, D2L2 = lagrangian_derivatives(model, qm2, vm2)

	return (0.5 * h[1] * D1L1 + D2L1 + 0.5 * h[1] * D1L2 - D2L2
		+ B_func(model, qm2) * u1
        + transpose(P_func(model, q2)) * λ1
        - h[1] * 0.5 .* vm2) # damping
end

function dynamics_no_impact(model::DoublePendulum, h, q0, q1, u1, λ1, q2)
	# evalutate at midpoint
	qm1 = 0.5 * (q0 + q1)
    vm1 = (q1 - q0) / h[1]
    qm2 = 0.5 * (q1 + q2)
    vm2 = (q2 - q1) / h[1]

	D1L1, D2L1 = lagrangian_derivatives(model, qm1, vm1)
	D1L2, D2L2 = lagrangian_derivatives(model, qm2, vm2)

	return (0.5 * h[1] * D1L1 + D2L1 + 0.5 * h[1] * D1L2 - D2L2
		+ B_func(model, qm2) * u1
        - h[1] * 0.5 .* vm2) # damping
end

function residual(model::DoublePendulum, z, θ, κ)
    nq = model.dim.q
    nu = model.dim.u
    nc = model.dim.c

    q0 = θ[1:nq]
    q1 = θ[nq .+ (1:nq)]
    u1 = θ[2nq .+ (1:nu)]
    h = θ[2nq + nu .+ (1:1)]

    q2 = z[1:nq]
    λ1 = z[nq .+ (1:nc)]
    s1 = z[nq + nc .+ (1:nc)]

    [
     dynamics(model, h, q0, q1, u1, λ1, q2);
     s1 .- ϕ_func(model, q2);
     λ1 .* s1 .- κ;
    ]

end

function residual_no_impact(model::DoublePendulum, z, θ, κ)
    nq = model.dim.q
    nu = model.dim.u
    nc = model.dim.c

    q0 = θ[1:nq]
    q1 = θ[nq .+ (1:nq)]
    u1 = θ[2nq .+ (1:nu)]
    h = θ[2nq + nu .+ (1:1)]

    q2 = z[1:nq]
    return dynamics_no_impact(model, h, q0, q1, u1, zeros(model.dim.c), q2)
end

# generate residual methods
nq = model.dim.q
nu = model.dim.u
nc = model.dim.c
nz = nq + nc + nc
nz_no_impact = nq
nθ = nq + nq + nu + 1

# Declare variables
@variables z[1:nz]
@variables z_no_impact[1:nz_no_impact]
@variables θ[1:nθ]
@variables κ[1:1]

# Residual
r = residual(model, z, θ, κ)
r = Symbolics.simplify.(r)
rz = Symbolics.jacobian(r, z, simplify = true)
rθ = Symbolics.jacobian(r, θ, simplify = true)

# Build function
r_func = eval(build_function(r, z, θ, κ)[2])
rz_func = eval(build_function(rz, z, θ)[2])
rθ_func = eval(build_function(rθ, z, θ)[2])

rz_array = similar(rz, Float64)
rθ_array = similar(rθ, Float64)

@save joinpath(@__DIR__, "dynamics/residual.jl") r_func rz_func rθ_func rz_array rθ_array
@load joinpath(@__DIR__, "dynamics/residual.jl") r_func rz_func rθ_func rz_array rθ_array

# Residual
r_no_impact = residual_no_impact(model, z_no_impact, θ, κ)
r_no_impact = Symbolics.simplify.(r_no_impact)
rz_no_impact = Symbolics.jacobian(r_no_impact, z_no_impact, simplify = true)
rθ_no_impact = Symbolics.jacobian(r_no_impact, θ, simplify = true)

# Build function
r_no_impact_func = eval(build_function(r_no_impact, z_no_impact, θ, κ)[2])
rz_no_impact_func = eval(build_function(rz_no_impact, z_no_impact, θ)[2])
rθ_no_impact_func = eval(build_function(rθ_no_impact, z_no_impact, θ)[2])

rz_no_impact_array = similar(rz_no_impact, Float64)
rθ_no_impact_array = similar(rθ_no_impact, Float64)

@save joinpath(@__DIR__, "dynamics/residual_no_impact.jl") r_no_impact_func rz_no_impact_func rθ_no_impact_func rz_no_impact_array rθ_no_impact_array
@load joinpath(@__DIR__, "dynamics/residual_no_impact.jl") r_no_impact_func rz_no_impact_func rθ_no_impact_func rz_no_impact_array rθ_no_impact_array

idx_ineq = collect([nq .+ (1:(nc + nc))]...)

# q0 = [0.5 * π; 0.0]
# q1 = [0.5 * π; 0.0]
# u1 = [0.0]
# h = 0.1
#
# z0 = copy([q1; ones(nc + nc)])
# θ0 = [q0; q1; u1; h]
#
# # test dynamics
# include_implicit_dynamics()
#
# # options
# opts = InteriorPointOptions(
#    κ_init = 0.1,
#    κ_tol = 1.0e-4,
#    r_tol = 1.0e-8,
#    diff_sol = true)
#
#
# # solver
# ip = interior_point(z0, θ0,
#    r! = r_func, rz! = rz_func,
#    rz = similar(rz, Float64),
#    rθ! = rθ_func,
#    rθ = similar(rθ, Float64),
#    idx_ineq = idx_ineq,
#    opts = opts)
#
# ip.methods.r! = r_func
# ip.methods.rz! = rz_func
# ip.methods.rθ! = rθ_func
#
# # simulate
# T = 50
# q_hist = [q0, q1]
#
# for t = 1:T
#     ip.z .= copy([q_hist[end]; 0.1 * ones(nc + nc)])
#     ip.θ .= copy([q_hist[end-1]; q_hist[end]; u1; h])
#     status = interior_point_solve!(ip)
#
#     if status
#         push!(q_hist, ip.z[1:nq])
#     else
#         println("dynamics failure t = $t")
#         println("res norm = $(norm(ip.r, Inf))")
#         break
#     end
# end
#
# include(joinpath(@__DIR__, "visuals.jl"))
# include(joinpath(pwd(), "models/visualize.jl"))
#
# vis = Visualizer()
# render(vis)
# default_background!(vis)
# settransform!(vis["/Cameras/default"],
#         compose(Translation(0.0, -95.0, -1.0), LinearMap(RotY(0.0 * π) * RotZ(-π / 2.0))))
# setprop!(vis["/Cameras/default/rotated/<object>"], "zoom", 30)
#
# visualize!(vis, model, q_hist, Δt = h)

max_issues_repo_name: thowell/DirectMotionPlanning


text:
abstract type PenelouxTranslationModel <: TranslationModel end

struct PenelouxTranslationParam <: EoSParam
    Vc::SingleParam{Float64}
end

@newmodelsimple PenelouxTranslation PenelouxTranslationModel PenelouxTranslationParam

export PenelouxTranslation
function PenelouxTranslation(components::Vector{String}; userlocations::Vector{String}=String[], verbose::Bool=false)
    params = getparams(components, ["properties/critical.csv"]; userlocations=userlocations, verbose=verbose)
    Vc = params["vc"]
    packagedparams = PenelouxTranslationParam(Vc)
    model = PenelouxTranslation(packagedparams, verbose=verbose)
    return model
end

function translation(model::CubicModel,V,T,z,translation_model::PenelouxTranslation)
    Tc = model.params.Tc.values
    Pc = model.params.Pc.values
    Vc = translation_model.params.Vc.values
    c = zeros(eltype(Tc),length(Tc))
    for i ∈ @comps
        Tci = Tc[i]
        Pci = Pc[i]
        RT = Tci*R̄
        Zc = Pci*Vc[i]/RT
        c[i] = -0.252*RT/Pci*(1.5448*Zc-0.4024)
    end
    return c
end


text:
export spConnect

function spConnect(model;pre=[],post=[],expr="",p=0.1)
  var=match(r"^.*(?==)",expr).match
  value=match(r"(?<==).*",expr).match
  if !contains(model.connection,var)
    N=model.groups[:N]
    model.connection*="$var=spzeros(N,N)\n"
  end

  if pre==[] || post==[]
   model.connection*="$var[1:N,1:N]=$value*sprand(N,N,$p)\n"
  elseif pre=="all" && post=="all"
    model.connection*="$var[1:N,1:N]=$value*sprand(N,N,$p)\n"
  elseif pre=="all" && post!="all"
    for postTemp in [post;]
      i=model.groups[postTemp][:localindex]
      model.connection*="$var[$i,1:N]=$value*sprand($(length(i)),N,$p)\n"
    end
  elseif pre!="all" && post=="all"
    for preTemp in [pre;]
      j=model.groups[preTemp][:localindex]
      model.connection*="$var[1:N,$j]=$value*sprand(N,$(length(j)),$p)\n"
    end
  else
    for preTemp in [pre;],postTemp in [post;]
      i=model.groups[postTemp][:localindex]
      j=model.groups[preTemp][:localindex]
      model.connection*="$var[$i,$j]=$value*sprand($(length(i)),$(length(j)),$p)\n"
    end
  end

end # end of function spConnect


text:
# ------------------------------------------------------------------------------------------
# # Character based RNN language model
# Based on http://karpathy.github.io/2015/05/21/rnn-effectiveness
# ------------------------------------------------------------------------------------------

# ------------------------------------------------------------------------------------------
# ## Setup
# 1-Adds required packages to Julia.
# 2-Loads the data and a pretrained model.
# ------------------------------------------------------------------------------------------

include("charlm.jl")

# ------------------------------------------------------------------------------------------
# ## Sample Data-1
# A random subset of the Shakespeare training data
# ------------------------------------------------------------------------------------------

LEN = 500
r = rand(1:length(shake_text)-LEN)
println(shake_text[r:r+LEN])
flush(STDOUT)

# ------------------------------------------------------------------------------------------
# ## Sample Generation-1
# Random Shakespeare style text generated by the model
# ------------------------------------------------------------------------------------------

generate(shake_model, LEN)

# ------------------------------------------------------------------------------------------
# ## Sample Data-2
# A random subset of the code in Julia base
# ------------------------------------------------------------------------------------------

r = rand(1:length(julia_text)-LEN)
println(julia_text[r:r+LEN])
flush(STDOUT)

# ------------------------------------------------------------------------------------------
# ## Sample Generation-2
# Random Julia code generated by the model
# ------------------------------------------------------------------------------------------

generate(julia_model, LEN)

max_stars_repo_name: grenkoca/JuliaTutorials

max_issues_repo_name: grenkoca/JuliaTutorials

max_forks_repo_name: grenkoca/JuliaTutorials


text:
#Steps to read and modify the SKyTEM headerfile
using DataFrames
using CSV

#function to read the *.dfn file and its column number and write it as a *.hdr file

function dfn2hdr(dfnfile::String,hdrfile::String)
    df = CSV.read(dfnfile, DataFrame; header=false);
    df[!, "Combined"] = string.(df[!, "Column3"], ":", df[!, "Column4"],":", df[!, "Column5"],":", df[!, "Column6"]);

    for i in 2:size(df,1)
        regex_literal = r"NAME="
        a1 = df[i,:].Column1[6:7]
        a2 = split(string(df[i,:].Combined), regex_literal)
        a2_r= first.(split.(a2[2], ":"))
        if occursin(";END DEFN", a2_r)
            a2_r = first.(split.(a2_r, ";"))
        end
        CSV.write(hdrfile,(Col_No = [a1], Col_Name = [a2_r]), append=true)
    end
end

dfn2hdr("GAB_10074_EM_Final.dfn","dfn2hdr.csv")


text:
using OpenSSH
using Test

@testset "OpenSSH.jl" begin

    @testset "documenter_keygen-added" begin
        using Example
        documenter_keygen(user="JuliaLang", repo="git@github.com:JuliaLang/Example.jl.git")
    end

    @testset "documenter_keygen-deved" begin
        using Pkg;
        Pkg.develop("Example")
        using Example
        documenter_keygen(Example)
    end

end


text:
"""
Hu, C., Zhou, B. and Hu, J., 2014, July.
Fast Support Vector Data Description training using edge detection on large datasets.
In 2014 International Joint Conference on Neural Networks (IJCNN) (pp. 2176-2182). IEEE.

Density-angle based edge detection sampling (DAEDS)
Parameter:
k: nearest neighborhood (experiments k ∈ [30, 35])
eps: ratio of candidate set points (experiments eps ∈ [0.1, 0.3, 0.4])
delta: ratio of complement set points (experiments delta ∈ [0.3, 0.4, 0.5, 0.6])
"""
mutable struct DAEDS <: Sampler
    k::Int
    eps::Real
    delta::Real
    function DAEDS(k::Int=30, eps::Real=0.1, delta::Real=0.3)
        k > 0 || throw(ArgumentError("Non-positive number of neighbors to consider $(k)."))
        eps >= 0 || throw(ArgumentError("Negative ratio of candidate points $(eps)."))
        delta >= 0 || throw(ArgumentError("Negative ratio of complement points $(delta)."))
        return new(k, eps, delta)
    end
end

function reachability_distance(data, neighbor_indices)
    reach_dist = zeros(size(data, 2), size(data, 2))
    d_ij = Distances.pairwise(Distances.Euclidean(), data, dims=2)
    for i in 1:size(data, 2)
        for j in 1:size(data, 2)
            k_dist = d_ij[i, neighbor_indices[i][end]]
            reach_dist[i, j] = max(k_dist, d_ij[i, j])
        end
    end
    return reach_dist
end

function local_reachability_distance(data, neighbor_indices)
    reach_dist = reachability_distance(data, neighbor_indices)
    lrd = zeros(size(reach_dist, 2))
    for i in 1:size(reach_dist, 2)
        lrd[i] = mean(reach_dist[i, neighbor_indices[i]])
    end
    return lrd
end

function local_outlier_factor(data, neighbor_indices)
    lrd = local_reachability_distance(data, neighbor_indices)
    lof = zeros(size(data, 2))
    for i in 1:size(data, 2)
        lof[i] = mean(lrd[j] / lrd[i] for j in neighbor_indices[i])
    end
    return lof
end

function support_neighbor(data, neighbor_indices, lof)
    sn = zeros(Int, size(data, 2))
    for i in 1:size(data, 2)
        _, idx = findmin(lof[neighbor_indices[i]])
        sn[i] = idx
    end
    return sn
end

function angle_variance(data, neighbor_indices, sn, C1)
    v_i = zeros(length(C1))
    for (i, c_i) in enumerate(C1)
        angles = [cosine_similarity(data[:, sn[c_i]] .- data[:, c_i],
                                    data[:, j] .- data[:, c_i]) for j in neighbor_indices[c_i]]
        v_i[i] = var(angles)
    end
    return v_i
end

function sample(sampler::DAEDS, data::DataSet, labels::Vector{Symbol})::BitArray{1}
    n = size(data, 2)
    sampler.k = min(n - 1, sampler.k)
    # Step 1: choose candidate sets C1 and C2 based on LOF
    tree = KDTree(data)
    neighbor_indices = [knn(tree, data[:, i], sampler.k + 1, true)[1][2:end] for i in 1:n]
    lof = local_outlier_factor(data, neighbor_indices)
    lof_sorted_indices = sortperm(lof)
    C1 = lof_sorted_indices[end-floor(Int, sampler.delta * n)+1:end]
    C2 = lof_sorted_indices[1:floor(Int, sampler.eps * n)]
    # Step 2: determine support neighbors
    sn = support_neighbor(data, neighbor_indices, lof)
    # Step 3: detect edges
    v_i = angle_variance(data, neighbor_indices, sn, C1)
    v_i_sorted_indices = sortperm(v_i, rev=true)
    A = v_i_sorted_indices[1:floor(Int, sampler.eps * n)]
    # Step 4: combine A and C2
    sample_mask = falses(n)
    sample_mask[A] .= true
    sample_mask[C2] .= true
    return sample_mask
end


text:
function estimate(m₀,μ,ξ,Ω)
    n = (length(μ)-3)÷2

    if n < 100
        μ .= Ω \ ξ
    else
        @inbounds for i ∈ m₀
            ind = [2*i+2,2*i+3]
            μ[ind] = Ω[ind,ind] \ (ξ[ind]-Ω[ind,:]*μ+Ω[ind,ind]*μ[ind])
        end
        μ[1:3] = Ω[1:3,1:3] \ (ξ[1:3]-Ω[1:3,:]*μ+Ω[1:3,1:3]*μ[1:3])
    end
end