text:
################################################
#
# Replication of the state-level blueNOTE model
#
################################################

using SLiDE
using CSV
using JuMP
using Complementarity
using DataFrames

##########
# FUNCTIONS
##########

#replace here with "collect"
function key_to_vec(d::Dict,index_num::Int64)
  return [k[index_num] for k in keys(d)]
end

function fill_zero(source::Dict,tofill::Dict)
  for k in keys(source)
      if !haskey(tofill,k)
          push!(tofill,k=>0)
      end
  end
end

function fill_zero(source::Tuple, tofill::Dict)
# Assume all possible permutations of keys should be present
# and determine which are missing.
  allkeys = vcat(collect(Base.Iterators.product(source...))...)
  missingkeys = setdiff(allkeys, collect(keys(tofill)))

# Add
  [push!(tofill, fill=>0) for fill in missingkeys]
  return tofill
end

#function here simplifies the loading and subsequent subsetting of the dataframes
function read_data_temp(file::String,year::Int64,dir::String,desc::String)
  df = SLiDE.read_file(dir,CSVInput(name=string(file,".csv"),descriptor=desc))
  df = df[df[!,:yr].==year,:]
  return df
end

function df_to_dict(df::DataFrame,remove_columns::Vector{Symbol},value_column::Symbol)
  colnames = setdiff(names(df),[remove_columns; value_column])
  return Dict(tuple(row[colnames]...)=>row[:Val] for row in eachrow(df))
end

############
# LOAD DATA
############

# year for the model to be based off of
mod_year = 2016

#specify the path where the dumped csv files are stored
data_temp_dir = abspath(joinpath(dirname(Base.find_package("SLiDE")), "..", "model", "data_temp"))

#blueNOTE contains a dictionary of the parameters needed to specify the model
blueNOTE = Dict(
    :ys0 => df_to_dict(read_data_temp("ys0",mod_year,data_temp_dir,"Sectoral supply"),[:yr],:Val),
    :id0 => df_to_dict(read_data_temp("id0",mod_year,data_temp_dir,"Intermediate demand"),[:yr],:Val),
    :ld0 => df_to_dict(read_data_temp("ld0",mod_year,data_temp_dir,"Labor demand"),[:yr],:Val),
    :kd0 => df_to_dict(read_data_temp("kd0",mod_year,data_temp_dir,"Capital demand"),[:yr],:Val),
    :ty0 => df_to_dict(read_data_temp("ty0",mod_year,data_temp_dir,"Production tax"),[:yr],:Val),
    :m0 => df_to_dict(read_data_temp("m0",mod_year,data_temp_dir,"Imports"),[:yr],:Val),
    :x0 => df_to_dict(read_data_temp("x0",mod_year,data_temp_dir,"Exports of goods and services"),[:yr],:Val),
    :rx0 => df_to_dict(read_data_temp("rx0",mod_year,data_temp_dir,"Re-exports of goods and services"),[:yr],:Val),
    :md0 => df_to_dict(read_data_temp("md0",mod_year,data_temp_dir,"Total margin demand"),[:yr],:Val),
    :nm0 => df_to_dict(read_data_temp("nm0",mod_year,data_temp_dir,"Margin demand from national market"),[:yr],:Val),
    :dm0 => df_to_dict(read_data_temp("dm0",mod_year,data_temp_dir,"Margin supply from local market"),[:yr],:Val),
    :s0 => df_to_dict(read_data_temp("s0",mod_year,data_temp_dir,"Aggregate supply"),[:yr],:Val),
    :a0 => df_to_dict(read_data_temp("a0",mod_year,data_temp_dir,"Armington supply"),[:yr],:Val),
    :ta0 => df_to_dict(read_data_temp("ta0",mod_year,data_temp_dir,"Tax net subsidy rate on intermediate demand"),[:yr],:Val),
    :tm0 => df_to_dict(read_data_temp("tm0",mod_year,data_temp_dir,"Import tariff"),[:yr],:Val),
    :cd0 => df_to_dict(read_data_temp("cd0",mod_year,data_temp_dir,"Final demand"),[:yr],:Val),
    :c0 => df_to_dict(read_data_temp("c0",mod_year,data_temp_dir,"Aggregate final demand"),[:yr],:Val),
    :yh0 => df_to_dict(read_data_temp("yh0",mod_year,data_temp_dir,"Household production"),[:yr],:Val),
    :bopdef0 => df_to_dict(read_data_temp("bopdef0",mod_year,data_temp_dir,"Balance of payments"),[:yr],:Val),
    :hhadj => df_to_dict(read_data_temp("hhadj",mod_year,data_temp_dir,"Household adjustment"),[:yr],:Val),
    :g0 => df_to_dict(read_data_temp("g0",mod_year,data_temp_dir,"Government demand"),[:yr],:Val),
    :i0 => df_to_dict(read_data_temp("i0",mod_year,data_temp_dir,"Investment demand"),[:yr],:Val),
    :xn0 => df_to_dict(read_data_temp("xn0",mod_year,data_temp_dir,"Regional supply to national market"),[:yr],:Val),
    :xd0 => df_to_dict(read_data_temp("xd0",mod_year,data_temp_dir,"Regional supply to local market"),[:yr],:Val),
    :dd0 => df_to_dict(read_data_temp("dd0",mod_year,data_temp_dir,"Regional demand from local  market"),[:yr],:Val),
    :nd0 => df_to_dict(read_data_temp("nd0",mod_year,data_temp_dir,"Regional demand from national market"),[:yr],:Val)
)

###############
# -- SETS --
###############

# read sets from their dumped CSVs
# these are converted to a vector of strings such that we can use them to populate variable indices
# and to use them as as conditionals (e.g. see the use of goods_margins)
regions = convert(Vector{String},SLiDE.read_file(data_temp_dir,CSVInput(name=string("set_r.csv"),descriptor="region set"))[!,:Dim1]);
sectors = convert(Vector{String},SLiDE.read_file(data_temp_dir,CSVInput(name=string("set_s.csv"),descriptor="sector set"))[!,:Dim1]);
goods = sectors;
margins = convert(Vector{String},SLiDE.read_file(data_temp_dir,CSVInput(name=string("set_m.csv"),descriptor="margin set"))[!,:Dim1]);
goods_margins = convert(Vector{String},SLiDE.read_file(data_temp_dir,CSVInput(name=string("set_gm.csv"),descriptor="goods with margins set"))[!,:g]);

# need to fill in zeros to avoid missing keys
fill_zero(tuple(regions,sectors,goods),blueNOTE[:ys0])
fill_zero(tuple(regions,goods,sectors),blueNOTE[:id0])
fill_zero(tuple(regions,sectors),blueNOTE[:ld0])
fill_zero(tuple(regions,sectors),blueNOTE[:kd0])
fill_zero(tuple(regions,sectors),blueNOTE[:ty0])
fill_zero(tuple(regions,goods),blueNOTE[:m0])
fill_zero(tuple(regions,goods),blueNOTE[:x0])
fill_zero(tuple(regions,goods),blueNOTE[:rx0])
fill_zero(tuple(regions,margins,goods),blueNOTE[:md0])
fill_zero(tuple(regions,goods,margins),blueNOTE[:nm0])
fill_zero(tuple(regions,goods,margins),blueNOTE[:dm0])
fill_zero(tuple(regions,goods),blueNOTE[:s0])
fill_zero(tuple(regions,goods),blueNOTE[:a0])
fill_zero(tuple(regions,goods),blueNOTE[:ta0])
fill_zero(tuple(regions,goods),blueNOTE[:tm0])
fill_zero(tuple(regions,goods),blueNOTE[:cd0])
fill_zero(tuple(regions),blueNOTE[:c0])
fill_zero(tuple(regions,goods),blueNOTE[:yh0])
fill_zero(tuple(regions),blueNOTE[:bopdef0])
fill_zero(tuple(regions),blueNOTE[:hhadj])
fill_zero(tuple(regions,goods),blueNOTE[:g0])
fill_zero(tuple(regions,goods),blueNOTE[:i0])
fill_zero(tuple(regions,goods),blueNOTE[:xn0])
fill_zero(tuple(regions,goods),blueNOTE[:xd0])
fill_zero(tuple(regions,goods),blueNOTE[:dd0])
fill_zero(tuple(regions,goods),blueNOTE[:nd0])

a_set = Dict()
[a_set[r,g] = blueNOTE[:a0][r,g] + blueNOTE[:rx0][r,g] for r in regions for g in goods]
# y_check is used to make sure the r/s combination is a valid output
y_check = Dict()
[y_check[r,s] = sum(blueNOTE[:ys0][r,s,g] for g in goods) for r in regions for s in sectors]

##############
# PARAMETERS
##############

alpha_kl = Dict() #value share of labor in the K/L nest for regions and sectors
alpha_x  = Dict() #export value share
alpha_d  = Dict() #local/domestic supply share
alpha_n  = Dict() #national supply share
theta_n  = Dict() #national share of domestic Absorption
theta_m  = Dict() #domestic share of absorption

for k in keys(blueNOTE[:ld0])
    val = blueNOTE[:ld0][k] / (blueNOTE[:kd0][k] + blueNOTE[:ld0][k])
    push!(alpha_kl,k=>val)
end

for k in keys(blueNOTE[:x0])
    val = (blueNOTE[:x0][k] - blueNOTE[:rx0][k]) / blueNOTE[:s0][k]
    push!(alpha_x,k=>val)
end

for k in keys(blueNOTE[:xd0])
    val = blueNOTE[:xd0][k] / blueNOTE[:s0][k]
    push!(alpha_d,k=>val)
end

for k in keys(blueNOTE[:xn0])
    val = blueNOTE[:xn0][k] / blueNOTE[:s0][k]
    push!(alpha_n,k=>val)
end

for k in keys(blueNOTE[:nd0])
    val = blueNOTE[:nd0][k] / (blueNOTE[:nd0][k] + blueNOTE[:dd0][k])
    push!(theta_n,k=>val)
end

for k in keys(blueNOTE[:tm0])
    val = (1+blueNOTE[:tm0][k]) * blueNOTE[:m0][k] / (blueNOTE[:nd0][k]+blueNOTE[:dd0][k]+(1+blueNOTE[:tm0][k]) * blueNOTE[:m0][k])
    push!(theta_m,k=>val)
end

################
# VARIABLES
################

cge = MCPModel();

# small value that acts as a lower limit to variable values
# default is zero
sv = 0.00

#sectors
@variable(cge,Y[r in regions,s in sectors]>=sv,start=1)
@variable(cge,X[r in regions,g in goods]>=sv,start=1)
@variable(cge,A[r in regions,g in goods]>=sv,start=1)
@variable(cge,C[r in regions]>=sv,start=1)
@variable(cge,MS[r in regions,m in margins]>=sv,start=1)

#commodities:
@variable(cge,PA[r in regions,g in goods]>=sv,start=1) # Regional market (input)
@variable(cge,PY[r in regions,g in goods]>=sv,start=1) # Regional market (output)
@variable(cge,PD[r in regions,g in goods]>=sv,start=1) # Local market price
@variable(cge,PN[g in goods]>=sv,start=1) # National market
@variable(cge,PL[r in regions]>=sv,start=1) # Wage rate
@variable(cge,PK[r in regions,s in sectors]>=sv,start=1) # Rental rate of capital
@variable(cge,PM[r in regions,m in margins]>=sv,start=1) # Margin price
@variable(cge,PC[r in regions]>=sv,start=1) # Consumer price index
@variable(cge,PFX>=sv,start=1) # Foreign exchange

#consumer:
@variable(cge,RA[r in regions]>=sv,start=blueNOTE[:c0][(r,)]) # Representative agent

###############################
# -- PLACEHOLDER VARIABLES --
###############################

@NLexpression(cge,CVA[r in regions,s in sectors],
  PL[r]^alpha_kl[r,s] * PK[r,s] ^(1-alpha_kl[r,s]) );

@NLexpression(cge,AL[r in regions, s in sectors],
  blueNOTE[:ld0][r,s] * CVA[r,s] / PL[r] );

@NLexpression(cge,AK[r in regions,s in sectors],
  blueNOTE[:kd0][r,s] * CVA[r,s] / PK[r,s] );

  ###

@NLexpression(cge,RX[r in regions,g in goods],
  (alpha_x[r,g]*PFX^5+alpha_n[r,g]*PN[g]^5+alpha_d[r,g]*PD[r,g]^5)^(1/5) );

@NLexpression(cge,AX[r in regions,g in goods],
  (blueNOTE[:x0][r,g] - blueNOTE[:rx0][r,g])*(PFX/RX[r,g])^4 );

@NLexpression(cge,AN[r in regions,g in goods],
  blueNOTE[:xn0][r,g]*(PN[g]/(RX[r,g]))^4 );

@NLexpression(cge,AD[r in regions,g in goods],
  blueNOTE[:xd0][r,g] * (PD[r,g] / (RX[r,g]))^4 );

  ###

@NLexpression(cge,CDN[r in regions,g in goods],
  (theta_n[r,g]*PN[g]^(1-2)+(1-theta_n[r,g])*PD[r,g]^(1-2))^(1/(1-2)) );

#!!!! here replaced first :tm with :tm0
@NLexpression(cge,CDM[r in regions,g in goods],
  ((1-theta_m[r,g])*CDN[r,g]^(1-4)+theta_m[r,g]*
  (PFX*(1+blueNOTE[:tm0][r,g])/(1+blueNOTE[:tm0][r,g]))^(1-4))^(1/(1-4))
  );

  ###

@NLexpression(cge,DN[r in regions,g in goods],
  blueNOTE[:nd0][r,g]*(CDN[r,g]/PN[g])^2*(CDM[r,g]/CDN[r,g])^4 );

@NLexpression(cge,DD[r in regions,g in goods],
  blueNOTE[:dd0][r,g]*(CDN[r,g]/PD[r,g])^2*(CDM[r,g]/CDN[r,g])^4 );

@NLexpression(cge,MD[r in regions,g in goods],
  blueNOTE[:m0][r,g]*(CDM[r,g]*(1+blueNOTE[:tm0][r,g])/(PFX*(1+blueNOTE[:tm0][r,g])))^4 );

@NLexpression(cge,CD[r in regions,g in goods],
  blueNOTE[:cd0][r,g]*PC[r] / PA[r,g] );

###############################
# -- Zero Profit Conditions --
###############################

@mapping(cge,profit_y[r in regions,s in sectors],
                sum(PA[r,g] * blueNOTE[:id0][r,g,s] for g in goods)
                + PL[r] * AL[r,s]
                + PK[r,s] * AK[r,s]
                -
                sum(PY[r,g] * blueNOTE[:ys0][r,s,g] for g in goods) * (1-blueNOTE[:ty0][r,s])
);

@mapping(cge,profit_x[r in regions,g in goods],
                  PY[r,g] * blueNOTE[:s0][r,g]
                  -
                  (PFX * AX[r,g]
                + PN[g] * AN[r,g]
                + PD[r,g] * AD[r,g])
);

@mapping(cge,profit_a[r in regions,g in goods],
                  PN[g] * DN[r,g]
                + PD[r,g] * DD[r,g]
                + PFX * (1+blueNOTE[:tm0][r,g]) * MD[r,g]
                + sum(PM[r,m] * blueNOTE[:md0][r,m,g] for m in margins)
                  - (
                  PA[r,g] * (1-blueNOTE[:ta0][r,g]) * blueNOTE[:a0][r,g]
                + PFX * blueNOTE[:rx0][r,g]
));

@mapping(cge,profit_c[r in regions],
                  sum(PA[r,g] * CD[r,g] for g in goods)
                  -
                  PC[r] * blueNOTE[:c0][(r,)]
);

@mapping(cge,profit_ms[r in regions, m in margins],
    sum(PN[gm]   * blueNOTE[:nm0][r,gm,m] for gm in goods_margins)
  + sum(PD[r,gm] * blueNOTE[:dm0][r,gm,m] for gm in goods_margins)
    -
    PM[r,m] * sum(blueNOTE[:md0][r,m,gm] for gm in goods_margins)
);

###################################
# -- Market Clearing Conditions --
###################################

@mapping(cge,market_pa[r in regions, g in goods],
        A[r,g] * blueNOTE[:a0][r,g]
        - (
        blueNOTE[:g0][r,g]
        + blueNOTE[:i0][r,g]
        + C[r] * CD[r,g]
        + sum(Y[r,s] * blueNOTE[:id0][r,g,s] for s in sectors if (y_check[r,s] > 0))
        )
);

@mapping(cge,market_py[r in regions, g in goods],
        sum(Y[r,s] * blueNOTE[:ys0][r,s,g] for s in sectors)
      + blueNOTE[:yh0][r,g]
        -
        X[r,g] * blueNOTE[:s0][r,g]
);

@mapping(cge,market_pd[r in regions, g in goods],
        X[r,g] * AD[r,g]
        - (
        A[r,g] * DD[r,g]
        + sum(MS[r,m] * blueNOTE[:dm0][r,g,m] for m in margins if (g in goods_margins ) )
        )
);

@mapping(cge,market_pn[g in goods],
        sum(X[r,g] * AN[r,g] for r in regions)
        - (
        sum(A[r,g] * DN[r,g] for r in regions)
        + sum(MS[r,m] * blueNOTE[:nm0][r,g,m] for r in regions for m in margins if (g in goods_margins) )
));

@mapping(cge,market_pl[r in regions],
        sum(blueNOTE[:ld0][r,s] for s in sectors)
        -
        sum(Y[r,s] * AL[r,s] for s in sectors)
);

@mapping(cge,market_pk[r in regions, s in sectors],
        blueNOTE[:kd0][r,s]
        - Y[r,s] * AK[r,s]
);

@mapping(cge,market_pm[r in regions, m in margins],
        MS[r,m] * sum(blueNOTE[:md0][r,m,gm] for gm in goods_margins)
        - (
        sum(A[r,g] * blueNOTE[:md0][r,m,g] for g in goods)
));

@mapping(cge,market_pc[r in regions],
        C[r] * blueNOTE[:c0][(r,)] - ( RA[r] / PC[r]
));

@mapping(cge,market_pfx,
        sum(blueNOTE[:bopdef0][(r,)] for r in regions)
        + sum(X[r,g] * AX[r,g] for r in regions for g in goods)
# add a set here
        + sum(A[r,g] * blueNOTE[:rx0][r,g] for r in regions for g in goods if (a_set[r,g] != 0))
#need a_set here
        - sum(A[r,g] * MD[r,g] for r in regions for g in goods)
);

@mapping(cge,income_ra[r in regions],
        RA[r] - (
        sum(PY[r,g]*blueNOTE[:yh0][r,g] for g in goods)
#will fix reference here...
        + PFX * (blueNOTE[:bopdef0][(r,)] + blueNOTE[:hhadj][(r,)])
        - sum(PA[r,g] * (blueNOTE[:g0][r,g] + blueNOTE[:i0][r,g]) for g in goods)
        + PL[r] * sum(blueNOTE[:ld0][r,s] for s in sectors)
        + sum(PK[r,s] * blueNOTE[:kd0][r,s] for s in sectors)
#changed tm to tm0 here...
        + sum(A[r,g] * MD[r,g]* PFX * blueNOTE[:tm0][r,g] for g in goods if (a_set[r,g] != 0))
#change ta to ta0 here
        + sum(A[r,g] * blueNOTE[:a0][r,g]*PA[r,g]*blueNOTE[:ta0][r,g] for g in goods if (a_set[r,g] != 0) )
        + sum(Y[r,s] * blueNOTE[:ys0][r,s,g] * blueNOTE[:ty0][r,s] for s in sectors for g in goods)
));

####################################
# -- Complementarity Conditions --
####################################

# equations with conditions cannot be paired
# see workaround here: https://github.com/chkwon/Complementarity.jl/issues/37
[fix(PK[r,s],1;force=true) for r in regions for s in sectors if !(blueNOTE[:kd0][r,s] > 0)]
[fix(PA[r,g],1,force=true) for r in regions for g in goods if !(blueNOTE[:a0][r,g]>0)]
[fix(PY[r,g],1,force=true) for r in regions for g in goods if !(y_check[r,g]>0)]

[fix(Y[r,s],1,force=true) for r in regions for s in sectors if !(y_check[r,s] > 0)]
[fix(X[r,g],1,force=true) for r in regions for g in goods if !(blueNOTE[:s0][r,g] > 0)]

# following lines are used in the GAMS version but not here
# and result in a non-zero residual for the benchmark
#a_fix = Dict()
#[a_fix[r,g] = blueNOTE[:a0][r,g] + blueNOTE[:rx0][r,g] for r in regions for g in goods]
#[fix(PD[r,g],0,force=true) for r in regions for g in goods if !(blueNOTE[:xd0][r,g] != 0)]
#[fix(A[r,g],0,force=true) for r in regions for g in goods if !(a_fix[r,g] != 0)]

@complementarity(cge,profit_y,Y);
@complementarity(cge,profit_x,X);
@complementarity(cge,profit_a,A);
@complementarity(cge,profit_c,C);
@complementarity(cge,profit_ms,MS);
@complementarity(cge,market_pa,PA);
@complementarity(cge,market_py,PY);
@complementarity(cge,market_pd,PD);
@complementarity(cge,market_pn,PN);
@complementarity(cge,market_pl,PL);
@complementarity(cge,market_pk,PK);
@complementarity(cge,market_pm,PM);
@complementarity(cge,market_pc,PC);
@complementarity(cge,market_pfx,PFX);
@complementarity(cge,income_ra,RA);

####################
# -- Model Solve --
####################

#set up the options for the path solver
PATHSolver.options(convergence_tolerance=1e-8, output=:yes, time_limit=3600)

# export the path license string to the environment
# this is now done in the SLiDE initiation steps
ENV["PATH_LICENSE_STRING"]="2617827524&Courtesy&&&USR&64785&11_12_2017&1000&PATH&GEN&31_12_2020&0_0_0&5000&0_0"

# solve the model
status = solveMCP(cge)

max_stars_repo_name: matbesancon/SLiDE

max_issues_repo_name: matbesancon/SLiDE

max_forks_repo_name: matbesancon/SLiDE


text:
using Test
using ImportAll

@testset "Approximation Type" begin

 @test true

end

ext: jl


text:
data_dir = withenv("DATADEPS_ALWAY_ACCEPT"=>"true") do
    datadep"PubMed"
end

@testset "PubMed" begin
    data  = PubMed.dataset()

    @test data.num_nodes == 19717
    @test data.num_edges == 88648
    @test size(data.node_features) == (500, data.num_nodes)
    @test size(data.node_labels) == (data.num_nodes,)
    @test size(data.train_indices) == (60,)
    @test size(data.val_indices) == (500,)
    @test size(data.test_indices) == (1000,)
    @test size(data.adjacency_list) == (data.num_nodes, )
    @test sum(length.(data.adjacency_list)) == (data.num_edges)
    @test minimum(minimum.(data.adjacency_list)) == 1
    @test maximum(maximum.(data.adjacency_list)) == data.num_nodes

end


text:
using DataGraphs
using Dictionaries
using Graphs
using Test

@testset "DataGraphs.jl" begin
  g = grid((4,))
  dg = DataGraph{String,Symbol}(g)
  @test !isassigned(dg, Edge(1, 2))
  @test !isassigned(dg, 1 => 2)
  @test !isassigned(dg, Edge(1 => 2))
  @test !isassigned(dg, 1 => 3)
  @test !isassigned(dg, 1)
  @test !isassigned(dg, 2)
  @test !isassigned(dg, 3)
  @test !isassigned(dg, 4)

  @test has_edge(dg, 1, 2)
  @test has_edge(dg, 1 => 2)
  @test !has_edge(dg, 1, 3)
  @test !has_edge(dg, 1 => 3)
  @test has_vertex(dg, 1)
  @test has_vertex(dg, 4)
  @test !has_vertex(dg, 0)
  @test !has_vertex(dg, 5)

  dg[1] = "V1"
  dg[2] = "V2"
  dg[3] = "V3"
  dg[4] = "V4"
  @test isassigned(dg, 1)
  @test dg[1] == "V1"
  @test dg[2] == "V2"
  @test dg[3] == "V3"
  @test dg[4] == "V4"

  dg[1 => 2] = :E12
  dg[2 => 3] = :E23
  dg[Edge(3, 4)] = :E34
  #@test isassigned(dg, (1, 2))
  @test isassigned(dg, Edge(2, 3))
  @test isassigned(dg, 3 => 4)
  @test dg[Edge(1, 2)] == :E12
  @test dg[2 => 3] == :E23
  @test dg[3 => 4] == :E34

  vdata = dictionary([1 => "V1", 2 => "V2", 3 => "V3", 4 => "V4"])
  edata = dictionary([Edge(1, 2) => "E12", Edge(2, 3) => "E23", Edge(3, 4) => "E34"])
  dg = DataGraph(g, vdata, edata)

  @test dg[1] == "V1"
  @test dg[2] == "V2"
  @test dg[3] == "V3"
  @test dg[4] == "V4"

  @test dg[1 => 2] == "E12"
  @test dg[2 => 3] == "E23"
  @test dg[3 => 4] == "E34"

  @test DataGraph(g) isa DataGraph{Any,Any}
  @test DataGraph{String}(g) isa DataGraph{String,Any}
  @test DataGraph{Any,String}(g) isa DataGraph{Any,String}

  # TODO: is this needed?
  #@test DataGraph{<:Any,String}(g) isa DataGraph{Any,String}

  ## vdata = [1 => "V1", 2 => "V2", 3 => "V3", 4 => "V4"]
  ## edata = [(1, 2) => "E12", (2 => 3) => "E23", Edge(3, 4) => "E34"]
  ## dg = DataGraph(g, vdata, edata)

  ## @test dg[1] == "V1"
  ## @test dg[2] == "V2"
  ## @test dg[3] == "V3"
  ## @test dg[4] == "V4"

  ## @test dg[1 => 2] == "E12"
  ## @test dg[2 => 3] == "E23"
  ## @test dg[3 => 4] == "E34"

  ## vdata = ["V1", "V2", "V3", "V4"]
  ## edata = [(1, 2) => "E12", (2 => 3) => "E23", Edge(3, 4) => "E34"]
  ## dg = DataGraph(g, vdata, edata)

  ## @test dg[1] == "V1"
  ## @test dg[2] == "V2"
  ## @test dg[3] == "V3"
  ## @test dg[4] == "V4"

  ## @test dg[1 => 2] == "E12"
  ## @test dg[2 => 3] == "E23"
  ## @test dg[3 => 4] == "E34"

  ## vdata = ["V1", "V2", "V3", "V4"]
  ## edata = ["E12", "E23", "E34"]
  ## dg = DataGraph(g, vdata, edata)

  ## @test dg[1] == "V1"
  ## @test dg[2] == "V2"
  ## @test dg[3] == "V3"
  ## @test dg[4] == "V4"

  ## @test dg[1 => 2] == "E12"
  ## @test dg[2 => 3] == "E23"
  ## @test dg[3 => 4] == "E34"

  ## vdata = ["V1", "V2", "V3", "V4"]
  ## edata = [Edge(1, 2) => "E12", Edge(2, 3) => "E23"]
  ## dg = DataGraph(g; vertex_data=vdata, edge_data=edata)

  ## @test dg[1] == "V1"
  ## @test dg[2] == "V2"
  ## @test dg[3] == "V3"
  ## @test dg[4] == "V4"

  ## @test dg[1 => 2] == "E12"
  ## @test dg[2 => 3] == "E23"
  ## @test !isassigned(dg, 3 => 4)

  ## vdata = ["V1", "V2", "V3", "V4"]
  ## dg = DataGraph(g; vertex_data=vdata)

  ## @test dg[1] == "V1"
  ## @test dg[2] == "V2"
  ## @test dg[3] == "V3"
  ## @test dg[4] == "V4"

  ## @test !isassigned(dg, 1 => 2)
  ## @test !isassigned(dg, 2 => 3)
  ## @test !isassigned(dg, 3 => 4)
end


text:
# ### [`ToEltype`](#)

"""
    ToEltype(T)

Converts any `AbstractArrayItem` to an `AbstractArrayItem{N, T}`.

Supports `apply!`.

## Examples

{cell=ToEltype}
```julia
using DataAugmentation

tfm = ToEltype(Float32)
item = ArrayItem(rand(Int, 10))
apply(tfm, item)
```
"""
struct ToEltype{T} <: Transform end
ToEltype(T::Type) = ToEltype{T}()

apply(::ToEltype{T}, item::AbstractArrayItem{N, <:T}; randstate = nothing) where {N, T} = item
function apply(::ToEltype{T1}, item::AbstractArrayItem{N, T2}; randstate = nothing) where {N, T1, T2}
    newdata = map(x -> convert(T1, x), itemdata(item))
    item = setdata(item, newdata)
    return item
end

function apply!(buf, ::ToEltype, item::AbstractArrayItem; randstate = nothing)
    ## copy! does type conversion under the hood
    copy!(itemdata(buf), itemdata(item))
    return buf
end

# ### [`Normalize`](#)

"""
    Normalize(means, stds)

Normalizes the last dimension of an `AbstractArrayItem{N}`.

Supports `apply!`.

## Examples

Preprocessing a 3D image with 3 color channels.

{cell=Normalize}
```julia
using DataAugmentation, Images
image = Image(rand(RGB, 20, 20, 20))
tfms = ImageToTensor() |> Normalize((0.1, -0.2, -0.1), (1,1,1.))
apply(tfms, image)
```

"""
struct Normalize{N} <: Transform
    means::SVector{N}
    stds::SVector{N}
end

function Normalize(means, stds)
    length(means) == length(stds) || error("`means` and `stds` must have same length")
    N = length(means)
    return Normalize{N}(SVector{N}(means), SVector{N}(stds))
end

function apply(tfm::Normalize, item::ArrayItem{N, T}; randstate = nothing) where {N, T}
    means = reshape(convert.(T, tfm.means), (1 for _ = 2:N)..., N)
    stds = reshape(convert.(T, tfm.stds), (1 for _ = 2:N)..., N)
    return ArrayItem(normalize(itemdata(item), means, stds))
end

function apply!(buf, tfm::Normalize, item::ArrayItem; randstate = nothing)
    copy!(itemdata(buf), itemdata(item))
    means = reshape(convert.(T, tfm.means), (1 for _ = 2:N)..., N)
    stds = reshape(convert.(T, tfm.stds), (1 for _ = 2:N)..., N)
    normalize!(itemdata(buf), means, stds)
    return buf
end

function normalize!(a, means, stds)
    a .-= means
    a ./= stds
    return a
end

normalize(a, means, stds) = normalize!(copy(a), means, stds)

function Normalize(array)
    slices = ones(Bool, size(array))
    means = mean(array[slices])
    stds = std(array[slices])
    array[slices] = (array[slices] .- means) / stds
    return array
end

function denormalize!(a, means, stds)
    a .*= stds
    a .+= means
    return a
end

denormalize(a, means, stds) = denormalize!(copy(a), means, stds)

# ### [`ImageToTensor`]

"""
    ImageToTensor()

Expands an `Image{N, T}` of size `sz` to an `ArrayItem{N+1}` with
size `(sz..., ch)` where `ch` is the number of color channels of `T`.

Supports `apply!`.

## Examples

{cell=ImageToTensor}
```julia
image = Image(rand(RGB, 50, 50))
tfm = ImageToTensor()
apply(tfm, image)
```

"""
struct ImageToTensor{T} <: Transform end

ImageToTensor(T::Type{<:Number} = Float32) = ImageToTensor{T}()

function apply(::ImageToTensor{T}, image::Image; randstate = nothing) where T
    return ArrayItem(imagetotensor(itemdata(image), T))
end

function apply!(buf::I, ::ImageToTensor, image::I; randstate = nothing) where {N, I<:Image{N}}
    imagetotensor!(buf.data, image.data)
    return buf
end

function imagetotensor(image::AbstractArray{C, N}, T = Float32) where {C<:Color, N}
    T.(permuteddimsview(channelview(image), ((i for i in 2:N+1)..., 1)))
end

function imagetotensor(image::AbstractArray{C, N}, T = Float32) where {TC, C<:Color{TC, 1}, N}
    return T.(channelview(image))
end

function imagetotensor!(buf, image::AbstractArray{<:AbstractRGB, N}) where N
    permutedims!(
        buf,
        channelview(image),
        (2, 3, 1))
end
tensortoimage(a::AbstractArray{T, 3}) where T = colorview(RGB, permuteddimsview(a, (3, 1, 2)))
tensortoimage(a::AbstractArray{T, 2}) where T = colorview(Gray, a)

function onehot(T, x::Int, n::Int)
    v = fill(zero(T), n)
    v[x] = one(T)
    return v
end
onehot(x, n) = onehot(Float32, x, n)


text:
"""
    ispangram(input)

Return `true` if `input` contains every alphabetic character (case insensitive).

"""
function ispangram_orig(input)
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    input = lowercase(input)
    for letter ∈ alphabet
        if letter ∉ input
            return false
        end
    end
    return true
end

function ispangram1(input)
    return all(letter -> (letter ∈ lowercase(input)), 'a':'z')
end

ispangram2(input) = 'a':'z' ⊆ lowercase(input)

ispangram3(input) = all(in(lowercase(input)), 'a':'z')

ispangram4(input) = all(c -> in(c, input) || in(uppercase(c), input), 'a':'z')

function ispangram5(input)
    utf8 = transcode(UInt8, input)
    present = zeros(Bool, 256)
    @inbounds for byte in utf8
        # byte | 0x20 will make uppercase ASCII into lowercase ASCII.
        # Other characters will be mangled, but we don't care about that.
        present[(byte | 0x20) + 1] = true
    end
    all(present[char + 1] for char in UInt8('a'):UInt8('z'))
end

max_stars_repo_name: vladflore/exercism

max_issues_repo_name: vladflore/exercism

max_forks_repo_name: vladflore/exercism


text:
#=
    CODE FOR SIMULATION AND CONTINUATION OF
    SWIFT-HOHENBERG with Non-local RAMAN response:

        ∂u/∂t = η + μu - u³ + β ∂²u/∂τ² - 4/3 ∂⁴u/∂τ⁴ + ∫χ(τ-τ')u(τ')dτ'

    Author: Martin Bataille-Gonzalez.

    -------------------------------
    Where,
        χ(τ) = √3/2 a fᵣ * exp(-τ₀τ/τ₂) * sin(τ₀τ/τ₁)
    and,
        a = τ₀(τ₁² + τ₂²) / (τ₁τ₂²)

    Parameters will be,
        τ₀ = 14 fs
        fᵣ = 0.18
        β = -1.5
        y = 0.12 = η (?)
        σ = -0.35 ⟹ μ = σ - √3/2fᵣ
    From, Parra-Rivas et al (2021) PRA.
        fᵣ = 0.18
        τ₁ = 12.2 fs
        τ₂ = 32 fs
=#
using DifferentialEquations

using Plots

function χ(τ::Number, p)
    return sqrt(3) / 2 * p[:a] * p[:fᵣ] * exp(-p[:τ₀]/p[:τ₂] * τ) * sin.(p[:τ₀]/p[:τ₁] * τ)
end

function χ(τ::AbstractArray, p)
    [χ(τᵢ, p) for τᵢ=τ]
end

function spectral_derivative(u, Δx, order)
    N = length(u)

    k = fftfreq(N) * N
    L = (N-1) * Δx

    return ifft( (2π / L * im) ^ order * k.^ order .* fft(u))
end

∫χu(u) = ifft(ft_coupling * fft(u))

∂ₓₓ(u, Δx) = spectral_derivative(u, Δx, 2)
∂ₓₓₓₓ(u, Δx) = spectral_derivative(u, Δx, 4)

# Parameters
L = 300
Δx = 0.1
τ = 0:Δx:L
τ₀ = 14
fᵣ = 0.18
β = -1
η = 0.0
σ = -0.35
μ = σ - sqrt(3)/2 * fᵣ
τ₁ = 12.2
τ₂ = 32
a = τ₀ * (τ₁ ^ 2 + τ₂ ^ 2) / (τ₁ * τ₂ ^ 2)

p = (τ₀ = τ₀, fᵣ = fᵣ, β = β, η = η, μ = μ, τ₁ = τ₁, τ₂ = τ₂, a=a, L=L, Δx = Δx)

coupling = χ(τ, p)

plot(τ, coupling)

using FFTW

FFTW.set_provider!("mkl")
ft_coupling = fft(coupling)

coupling[200]
decay = τ₂ / τ₀

function SHRaman(u, p, t)
    return p[:η] .+ p[:μ] * u .- u .^ 3 .+ p[:β] * ∂ₓₓ(u, p[:Δx])
            .- 4 / 3 * ∂ₓₓₓₓ(u, p[:Δx]) .+ ∫χu(u)
end

u = im * zeros(length(τ))

tspan = (0.0, 1.0)
prob = ODEProblem(SHRaman, u, tspan, p)
sol = solve(prob, RK4())

plot(τ, real.(sol(1.0)))

u = SHRaman(u, p, 0)


text:
"""
    module Interpolation

Module implementing various interpolation schemes commonly used in finite
element and boundary element methods.
"""
module Interpolation

using StaticArrays
using LinearAlgebra

using WavePropBase
using WavePropBase.Utils
using WavePropBase.Geometry

WavePropBase.@import_interface

export
    # abstract types
    AbstractElement,
    AbstractHyperRectangle,
    # structs
    ParametricElement,
    LagrangeElement,
    LagrangePoint,
    LagrangeLine,
    LagrangeTriangle,
    LagrangeSquare,
    LagrangeTetrahedron,
    Pk,
    TensorLagInterp,
    ChebInterp,
    MonomialBasis,
    PolynomialBasis,
    LagrangeBasis,
    HyperRectangle,
    HyperCube,
    # methods
    reference_nodes,
    monomial_basis,
    lagrange_basis,
    barycentric_lagrange_matrix,
    barycentric_lagrange_weights,
    vandermond,
    lagrange_basis,
    gradLagrangeBasis,
    interpolation_nodes,
    trace,
    cheb1nodes,
    cheb1nodes_iter,
    cheb1weights,
    cheb2nodes,
    cheb2nodes_iter,
    cheb2weights,
    grad,
    vals,
    low_corner,
    high_corner,
    precompute_nodes_and_weights!

include("polynomials.jl")
include("element.jl")
include("hyperrectangle.jl")
include("tensorlaginterp.jl")

end # module Interpolation

max_stars_repo_name: WaveProp/WavePropBase

max_issues_repo_name: WaveProp/WavePropBase

max_forks_repo_name: WaveProp/WavePropBase


text:
# Chase Algorithm
#################

add_srctgt(x::Symbol) = Symbol("src_$(x)") => Symbol("tgt_$(x)")

"""Create a ACSet instance modeling a C-Rel from an instance of an ACSet"""
function crel(x::StructACSet{S}) where {S}
  name = Symbol("Rel_$(typeof(x).name.name)")
  os, hs, _, _, src, tgt = S.parameters
  pres = Presentation(FreeSchema)
  nv = length(os)
  alledge = Symbol[]
  xobs = [Ob(FreeSchema, sym) for sym in vcat(os...,hs...)]
  for x in xobs
    add_generator!(pres, x)
  end
  for (i,h) in enumerate(hs)
    s, t = add_srctgt(h)
    add_generator!(pres, Hom(s, xobs[nv+i], xobs[src[h]]))
    add_generator!(pres, Hom(t, xobs[nv+i], xobs[tgt[h]]))
    append!(alledge, [s,t])
  end
  expr = struct_acset(name, StructACSet, pres, index=alledge)
  eval(expr)
  rel = eval(name)
  J = Base.invokelatest(rel)

  # initialize the C-Rel with x which satisfies the laws
  for o in ob(S)
    add_parts!(J, o, nparts(x, o))
  end
  src, tgt = dom(S), codom(S)
  for (i, d) in enumerate(hom(S))
    hs, ht = add_srctgt(d)
    add_parts!(J, d, nparts(x, src[i]))
    set_subpart!(J, hs, parts(x, src[i]))
    set_subpart!(J, ht, x[d])
  end
  return J
end

"""Convert a C-Rel back to a C-Set, fail if relations are not functional"""
function uncrel(J::StructACSet, I::StructACSet{S}) where {S}
  res = typeof(I)()
  for o in ob(S)
    add_parts!(res, o, nparts(J, o))
  end
  for m in hom(S)
    msrc, mtgt = add_srctgt(m)
    md, mcd = zip(sort(collect(zip(J[msrc], J[mtgt])))...)
    collect(md) == collect(1:length(md)) || error("nonfunctional $J")
    set_subpart!(res, m, mcd)
  end
  return res
end

"""Evaluate a possibly composite composition on a subset of the domain"""
function eval_path(pth, J::StructACSet, xs::Set{Int})::Set{Int}
  pthkind = typeof(pth).parameters[1]  # Warning: uses implementation details
  if pthkind == :id
    return xs
  elseif pthkind == :generator
    args = [pth]
  elseif pthkind == :compose
    args = pth.args
  else
    error(pthkind)
  end
  # Iteratively update X to Y by searching through all (x,y) in R ⊆ X×Y
  for m in map(Symbol, args)
    msrc, mtgt = add_srctgt(m)
    if isempty(xs)
      return xs
    else
      newxs = Set{Int}()
      for (jx, jy) in zip(J[msrc], J[mtgt])
        if jx ∈ xs
          push!(newxs, jy)
        end
      end
      xs = newxs
    end
  end
  return xs
end

"""
Chase algorithm as described by Spivak and Wisnesky in "Fast Left-Kan Extensions
Using The Chase"

Todo: handle attributes
"""
function chase(F::Functor, I::StructACSet{IS}, codomI::StructACSet{S};
               n::Int=8, verbose::Bool=false)::StructACSet{S} where {IS, S}
  J = crel(codomI) # pre-model data structure, initialized with codomI
  η = Dict() # mapping into J from I

  # Further initialize J using the domain instance, I
  for c in generators(dom(F), :Ob)
    η[c] = add_parts!(J, Symbol(Ob(F)[c]), nparts(I, Symbol(c)))
  end
  changed = false # iterate until no change or n is reached
  for counter in 1:n
    if verbose
      println("----- ITERATION $counter ------")
    end

    # Initialize equivalence relations for each component
    eqclasses = Dict([o=>IntDisjointSets(nparts(J, o)) for o in ob(S)])
    changed = false

    # Action α: add new elements when a function is dangling
    fresh = Dict{Symbol, Set{Int}}([o=>Set{Int}() for o in ob(S)])
    for (d, ddom, dcodom) in zip(hom(S), dom(S), codom(S))
      dsrc, dtgt = add_srctgt(d)
      xs = setdiff(parts(J, ddom), J[dsrc] ∪ fresh[ddom])
      for x in xs  # no y such that (x,y) ∈ J(d) and no fresh x
        gx = add_part!(J, dcodom) # add a fresh symbol
        if verbose
          println("α: $d($ddom#$x) ADDED $dcodom $gx")
        end
        add_part!(J, d; Dict([dsrc=>x, dtgt=>gx])...)
        gx_ = push!(eqclasses[dcodom])
        push!(fresh[dcodom], gx)
        gx == gx_ || error("Unexpected mismatch: $eqclasses \n$J")
        changed = true
      end
    end

    # Action βd: add all coincidences induced by D (i.e. fire EGDs)
    for (p, q) in equations(codom(F))
      for x in parts(J, Symbol(dom(p)))
        for px in eval_path(p, J, Set([x]))
          for qx in eval_path(q, J, Set([x]))
            union!(eqclasses[Symbol(codom(p))], px, qx)
            if verbose && px != qx
              println("βF: $p=$q MERGED $(codom(p)) $px = $qx")
            end
            changed |= px != qx # flip iff changed = ⊥ AND px != qx
          end
        end
      end
    end

    # Action βF: add all coincidences induced by F. Naturality square constraint
    for mg in generators(dom(F), :Hom)
      m, Fm, Fmcodom = map(Symbol, [mg, Hom(F)[mg], Ob(F)[codom(mg)]])
      is_id = typeof(Hom(F)[mg]).parameters[1] == :id
      for (x, mx) in enumerate(I[m])
        mα = η[codom(mg)][mx]
        Fms, Fmt = add_srctgt(Fm)
        αx = η[dom(mg)][x]
        αm = is_id ? αx : J[Fmt][incident(J, αx, Fms)[1]]
        union!(eqclasses[Fmcodom], mα, αm)
        if verbose && mα != αm
          println("βF: $mg ($(dom(mg))#$x) MERGED $Fmcodom $mα = $αm")
        end
        changed |= mα != αm # flip iff changed = ⊥ AND mα != αm
      end
    end

    # Action δ: add all coincidences induced by functionality
    for (d, ddom, dcodom) in zip(hom(S), dom(S), codom(S))
      dsrc, dtgt = add_srctgt(d)
      for x in parts(J, ddom)
        eq_ys = collect(Set(J[dtgt][incident(J, x, dsrc)]))
        for (y1, y2) in zip(eq_ys, eq_ys[2:end])
          union!(eqclasses[dcodom], y1, y2)
          if verbose
            println("δ: $d($ddom#$x) MERGED $dcodom $y1 = $y2")
          end
          changed = true
        end
      end
    end

    # Action γ: merge coincidentally equal elements
    μ = Dict{Symbol, Vector{Pair{Int,Int}}}([o=>Pair{Int,Int}[] for o in ob(S)])
    delob = Dict{Symbol, Vector{Int}}([o=>Int[] for o in ob(S)])
    for (o, eq) in pairs(eqclasses)
      eqsets = Dict{Int,Set{Int}}()
      # Recover the equivalence classes from the union-find structure
      for x in parts(J, o)
        eqrep = find_root(eq, x)
        if haskey(eqsets, eqrep)
          push!(eqsets[eqrep], x)
        else
          eqsets[eqrep] = Set([x])
        end
      end
      if verbose
        println("eqset $o $(values(eqsets))")
      end
      # Minimum element is the representative
      for vs in values(eqsets)
        m = minimum(vs)
        delete!(vs, m)
        append!(μ[o], [v=>m for v in vs])
        append!(delob[o], collect(vs))
      end
    end
    # Replace all instances of a class with its representative in J and η
    for (d, ddom, dcodom) in zip(hom(S), dom(S), codom(S))
      dsrc, dtgt = add_srctgt(d)
      isempty(μ[ddom]) || set_subpart!(J, dsrc, replace(J[dsrc], μ[ddom]...))
      isempty(μ[dcodom]) || set_subpart!(J, dtgt, replace(J[dtgt], μ[dcodom]...))
      # remove redundant duplicate relation rows
      seen = Set()
      for (i, st) in enumerate(zip(J[dsrc], J[dtgt]))
        if st ∈ seen
          rem_part!(J, d, i)
        else
          push!(seen, st)
        end
      end
    end
    for (Iob, mapping) in collect(η)
      reps = μ[Symbol(Ob(F)[Iob])]
      if !isempty(reps)
        η[Iob] = replace(mapping, reps...)
      end
    end
    for (o, vs) in collect(delob)
      isempty(vs) || rem_parts!(J, o, sort(vs))
    end
    if !changed
      break # termination condition
    end
  end

  if changed
    error("Chase did not terminate with $n steps: $J")
  else
    return uncrel(J, codomI)
  end
end

# end # module


text:
module Mime_ext

export MimeExt

MimeExt = {
  "ez" => "application/andrew-inset",
  "anx" => "application/annodex",
  "atom" => "application/atom+xml",
  "atomcat" => "application/atomcat+xml",
  "atomsrv" => "application/atomserv+xml",
  "lin" => "application/bbolin",
  "cu" => "application/cu-seeme",
  "davmount" => "application/davmount+xml",
  "dcm" => "application/dicom",
  "tsp" => "application/dsptype",
  "es" => "application/ecmascript",
  "spl" => "application/futuresplash",
  "hta" => "application/hta",
  "jar" => "application/java-archive",
  "ser" => "application/java-serialized-object",
  "class" => "application/java-vm",
  "js" => "application/javascript",
  "json" => "application/json",
  "m3g" => "application/m3g",
  "hqx" => "application/mac-binhex40",
  "cpt" => "application/mac-compactpro",
  "nb" => "application/mathematica",
  "nbp" => "application/mathematica",
  "mbox" => "application/mbox",
  "mdb" => "application/msaccess",
  "doc" => "application/msword",
  "dot" => "application/msword",
  "mxf" => "application/mxf",
  "bin" => "application/octet-stream",
  "oda" => "application/oda",
  "ogx" => "application/ogg",
  "one" => "application/onenote",
  "onetoc2" => "application/onenote",
  "onetmp" => "application/onenote",
  "onepkg" => "application/onenote",
  "pdf" => "application/pdf",
  "pgp" => "application/pgp-encrypted",
  "key" => "application/pgp-keys",
  "sig" => "application/pgp-signature",
  "prf" => "application/pics-rules",
  "ps" => "application/postscript",
  "ai" => "application/postscript",
  "eps" => "application/postscript",
  "epsi" => "application/postscript",
  "epsf" => "application/postscript",
  "eps2" => "application/postscript",
  "eps3" => "application/postscript",
  "rar" => "application/rar",
  "rdf" => "application/rdf+xml",
  "rtf" => "application/rtf",
  "stl" => "application/sla",
  "smi" => "application/smil",
  "smil" => "application/smil",
  "xhtml" => "application/xhtml+xml",
  "xht" => "application/xhtml+xml",
  "xml" => "application/xml",
  "xsl" => "application/xml",
  "xsd" => "application/xml",
  "xspf" => "application/xspf+xml",
  "zip" => "application/zip",
  "apk" => "application/vnd.android.package-archive",
  "cdy" => "application/vnd.cinderella",
  "kml" => "application/vnd.google-earth.kml+xml",
  "kmz" => "application/vnd.google-earth.kmz",
  "xul" => "application/vnd.mozilla.xul+xml",
  "xls" => "application/vnd.ms-excel",
  "xlb" => "application/vnd.ms-excel",
  "xlt" => "application/vnd.ms-excel",
  "xlam" => "application/vnd.ms-excel.addin.macroEnabled.12",
  "xlsb" => "application/vnd.ms-excel.sheet.binary.macroEnabled.12",
  "xlsm" => "application/vnd.ms-excel.sheet.macroEnabled.12",
  "xltm" => "application/vnd.ms-excel.template.macroEnabled.12",
  "eot" => "application/vnd.ms-fontobject",
  "thmx" => "application/vnd.ms-officetheme",
  "cat" => "application/vnd.ms-pki.seccat",
  "ppt" => "application/vnd.ms-powerpoint",
  "pps" => "application/vnd.ms-powerpoint",
  "ppam" => "application/vnd.ms-powerpoint.addin.macroEnabled.12",
  "pptm" => "application/vnd.ms-powerpoint.presentation.macroEnabled.12",
  "sldm" => "application/vnd.ms-powerpoint.slide.macroEnabled.12",
  "ppsm" => "application/vnd.ms-powerpoint.slideshow.macroEnabled.12",
  "potm" => "application/vnd.ms-powerpoint.template.macroEnabled.12",
  "docm" => "application/vnd.ms-word.document.macroEnabled.12",
  "dotm" => "application/vnd.ms-word.template.macroEnabled.12",
  "odc" => "application/vnd.oasis.opendocument.chart",
  "odb" => "application/vnd.oasis.opendocument.database",
  "odf" => "application/vnd.oasis.opendocument.formula",
  "odg" => "application/vnd.oasis.opendocument.graphics",
  "otg" => "application/vnd.oasis.opendocument.graphics-template",
  "odi" => "application/vnd.oasis.opendocument.image",
  "odp" => "application/vnd.oasis.opendocument.presentation",
  "otp" => "application/vnd.oasis.opendocument.presentation-template",
  "ods" => "application/vnd.oasis.opendocument.spreadsheet",
  "ots" => "application/vnd.oasis.opendocument.spreadsheet-template",
  "odt" => "application/vnd.oasis.opendocument.text",
  "odm" => "application/vnd.oasis.opendocument.text-master",
  "ott" => "application/vnd.oasis.opendocument.text-template",
  "oth" => "application/vnd.oasis.opendocument.text-web",
  "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  "sldx" => "application/vnd.openxmlformats-officedocument.presentationml.slide",
  "ppsx" => "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
  "potx" => "application/vnd.openxmlformats-officedocument.presentationml.template",
  "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  "xltx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
  "xltx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
  "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  "dotx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
  "cod" => "application/vnd.rim.cod",
  "mmf" => "application/vnd.smaf",
  "sdc" => "application/vnd.stardivision.calc",
  "sds" => "application/vnd.stardivision.chart",
  "sda" => "application/vnd.stardivision.draw",
  "sdd" => "application/vnd.stardivision.impress",
  "sdf" => "application/vnd.stardivision.math",
  "sdw" => "application/vnd.stardivision.writer",
  "sgl" => "application/vnd.stardivision.writer-global",
  "sxc" => "application/vnd.sun.xml.calc",
  "stc" => "application/vnd.sun.xml.calc.template",
  "sxd" => "application/vnd.sun.xml.draw",
  "std" => "application/vnd.sun.xml.draw.template",
  "sxi" => "application/vnd.sun.xml.impress",
  "sti" => "application/vnd.sun.xml.impress.template",
  "sxm" => "application/vnd.sun.xml.math",
  "sxw" => "application/vnd.sun.xml.writer",
  "sxg" => "application/vnd.sun.xml.writer.global",
  "stw" => "application/vnd.sun.xml.writer.template",
  "sis" => "application/vnd.symbian.install",
  "cap" => "application/vnd.tcpdump.pcap",
  "pcap" => "application/vnd.tcpdump.pcap",
  "vsd" => "application/vnd.visio",
  "wbxml" => "application/vnd.wap.wbxml",
  "wmlc" => "application/vnd.wap.wmlc",
  "wmlsc" => "application/vnd.wap.wmlscriptc",
  "wpd" => "application/vnd.wordperfect",
  "wp5" => "application/vnd.wordperfect5.1",
  "wk" => "application/x-123",
  "7z" => "application/x-7z-compressed",
  "abw" => "application/x-abiword",
  "dmg" => "application/x-apple-diskimage",
  "bcpio" => "application/x-bcpio",
  "torrent" => "application/x-bittorrent",
  "cab" => "application/x-cab",
  "cbr" => "application/x-cbr",
  "cbz" => "application/x-cbz",
  "cdf" => "application/x-cdf",
  "cda" => "application/x-cdf",
  "vcd" => "application/x-cdlink",
  "pgn" => "application/x-chess-pgn",
  "mph" => "application/x-comsol",
  "cpio" => "application/x-cpio",
  "csh" => "application/x-csh",
  "deb" => "application/x-debian-package",
  "udeb" => "application/x-debian-package",
  "dcr" => "application/x-director",
  "dir" => "application/x-director",
  "dxr" => "application/x-director",
  "dms" => "application/x-dms",
  "wad" => "application/x-doom",
  "dvi" => "application/x-dvi",
  "pfa" => "application/x-font",
  "pfb" => "application/x-font",
  "gsf" => "application/x-font",
  "pcf" => "application/x-font",
  "pcf.Z" => "application/x-font",
  "woff" => "application/x-font-woff",
  "mm" => "application/x-freemind",
  "spl" => "application/x-futuresplash",
  "gan" => "application/x-ganttproject",
  "gnumeric" => "application/x-gnumeric",
  "sgf" => "application/x-go-sgf",
  "gcf" => "application/x-graphing-calculator",
  "gtar" => "application/x-gtar",
  "tgz" => "application/x-gtar-compressed",
  "taz" => "application/x-gtar-compressed",
  "hdf" => "application/x-hdf",
  "hwp" => "application/x-hwp",
  "ica" => "application/x-ica",
  "info" => "application/x-info",
  "ins" => "application/x-internet-signup",
  "isp" => "application/x-internet-signup",
  "iii" => "application/x-iphone",
  "iso" => "application/x-iso9660-image",
  "jam" => "application/x-jam",
  "jnlp" => "application/x-java-jnlp-file",
  "jmz" => "application/x-jmol",
  "chrt" => "application/x-kchart",
  "kil" => "application/x-killustrator",
  "skp" => "application/x-koan",
  "skd" => "application/x-koan",
  "skt" => "application/x-koan",
  "skm" => "application/x-koan",
  "kpr" => "application/x-kpresenter",
  "kpt" => "application/x-kpresenter",
  "ksp" => "application/x-kspread",
  "kwd" => "application/x-kword",
  "kwt" => "application/x-kword",
  "latex" => "application/x-latex",
  "lha" => "application/x-lha",
  "lyx" => "application/x-lyx",
  "lzh" => "application/x-lzh",
  "lzx" => "application/x-lzx",
  "frm" => "application/x-maker",
  "maker" => "application/x-maker",
  "frame" => "application/x-maker",
  "fm" => "application/x-maker",
  "fb" => "application/x-maker",
  "book" => "application/x-maker",
  "fbdoc" => "application/x-maker",
  "md5" => "application/x-md5",
  "mif" => "application/x-mif",
  "m3u8" => "application/x-mpegURL",
  "wmd" => "application/x-ms-wmd",
  "wmz" => "application/x-ms-wmz",
  "com" => "application/x-msdos-program",
  "exe" => "application/x-msdos-program",
  "bat" => "application/x-msdos-program",
  "dll" => "application/x-msdos-program",
  "msi" => "application/x-msi",
  "nc" => "application/x-netcdf",
  "pac" => "application/x-ns-proxy-autoconfig",
  "dat" => "application/x-ns-proxy-autoconfig",
  "nwc" => "application/x-nwc",
  "o" => "application/x-object",
  "oza" => "application/x-oz-application",
  "p7r" => "application/x-pkcs7-certreqresp",
  "crl" => "application/x-pkcs7-crl",
  "pyc" => "application/x-python-code",
  "pyo" => "application/x-python-code",
  "qgs" => "application/x-qgis",
  "shp" => "application/x-qgis",
  "shx" => "application/x-qgis",
  "qtl" => "application/x-quicktimeplayer",
  "rdp" => "application/x-rdp",
  "rpm" => "application/x-redhat-package-manager",
  "rss" => "application/x-rss+xml",
  "rb" => "application/x-ruby",
  "sci" => "application/x-scilab",
  "sce" => "application/x-scilab",
  "xcos" => "application/x-scilab-xcos",
  "sh" => "application/x-sh",
  "sha1" => "application/x-sha1",
  "shar" => "application/x-shar",
  "swf" => "application/x-shockwave-flash",
  "swfl" => "application/x-shockwave-flash",
  "scr" => "application/x-silverlight",
  "sql" => "application/x-sql",
  "sit" => "application/x-stuffit",
  "sitx" => "application/x-stuffit",
  "sv4cpio" => "application/x-sv4cpio",
  "sv4crc" => "application/x-sv4crc",
  "tar" => "application/x-tar",
  "tcl" => "application/x-tcl",
  "gf" => "application/x-tex-gf",
  "pk" => "application/x-tex-pk",
  "texinfo" => "application/x-texinfo",
  "texi" => "application/x-texinfo",
  "~" => "application/x-trash",
  "%" => "application/x-trash",
  "bak" => "application/x-trash",
  "old" => "application/x-trash",
  "sik" => "application/x-trash",
  "t" => "application/x-troff",
  "tr" => "application/x-troff",
  "roff" => "application/x-troff",
  "man" => "application/x-troff-man",
  "me" => "application/x-troff-me",
  "ms" => "application/x-troff-ms",
  "ustar" => "application/x-ustar",
  "src" => "application/x-wais-source",
  "wz" => "application/x-wingz",
  "crt" => "application/x-x509-ca-cert",
  "xcf" => "application/x-xcf",
  "fig" => "application/x-xfig",
  "xpi" => "application/x-xpinstall",
  "amr" => "audio/amr",
  "awb" => "audio/amr-wb",
  "amr" => "audio/amr",
  "awb" => "audio/amr-wb",
  "axa" => "audio/annodex",
  "au" => "audio/basic",
  "snd" => "audio/basic",
  "csd" => "audio/csound",
  "orc" => "audio/csound",
  "sco" => "audio/csound",
  "flac" => "audio/flac",
  "mid" => "audio/midi",
  "midi" => "audio/midi",
  "kar" => "audio/midi",
  "mpga" => "audio/mpeg",
  "mpega" => "audio/mpeg",
  "mp2" => "audio/mpeg",
  "mp3" => "audio/mpeg",
  "m4a" => "audio/mpeg",
  "m3u" => "audio/mpegurl",
  "oga" => "audio/ogg",
  "ogg" => "audio/ogg",
  "spx" => "audio/ogg",
  "sid" => "audio/prs.sid",
  "aif" => "audio/x-aiff",
  "aiff" => "audio/x-aiff",
  "aifc" => "audio/x-aiff",
  "gsm" => "audio/x-gsm",
  "m3u" => "audio/x-mpegurl",
  "wma" => "audio/x-ms-wma",
  "wax" => "audio/x-ms-wax",
  "ra" => "audio/x-pn-realaudio",
  "rm" => "audio/x-pn-realaudio",
  "ram" => "audio/x-pn-realaudio",
  "ra" => "audio/x-realaudio",
  "pls" => "audio/x-scpls",
  "sd2" => "audio/x-sd2",
  "wav" => "audio/x-wav",
  "alc" => "chemical/x-alchemy",
  "cac" => "chemical/x-cache",
  "cache" => "chemical/x-cache",
  "csf" => "chemical/x-cache-csf",
  "cbin" => "chemical/x-cactvs-binary",
  "cascii" => "chemical/x-cactvs-binary",
  "ctab" => "chemical/x-cactvs-binary",
  "cdx" => "chemical/x-cdx",
  "cer" => "chemical/x-cerius",
  "c3d" => "chemical/x-chem3d",
  "chm" => "chemical/x-chemdraw",
  "cif" => "chemical/x-cif",
  "cmdf" => "chemical/x-cmdf",
  "cml" => "chemical/x-cml",
  "cpa" => "chemical/x-compass",
  "bsd" => "chemical/x-crossfire",
  "csml" => "chemical/x-csml",
  "csm" => "chemical/x-csml",
  "ctx" => "chemical/x-ctx",
  "cxf" => "chemical/x-cxf",
  "cef" => "chemical/x-cxf",
  "emb" => "chemical/x-embl-dl-nucleotide",
  "embl" => "chemical/x-embl-dl-nucleotide",
  "spc" => "chemical/x-galactic-spc",
  "inp" => "chemical/x-gamess-input",
  "gam" => "chemical/x-gamess-input",
  "gamin" => "chemical/x-gamess-input",
  "fch" => "chemical/x-gaussian-checkpoint",
  "fchk" => "chemical/x-gaussian-checkpoint",
  "cub" => "chemical/x-gaussian-cube",
  "gau" => "chemical/x-gaussian-input",
  "gjc" => "chemical/x-gaussian-input",
  "gjf" => "chemical/x-gaussian-input",
  "gal" => "chemical/x-gaussian-log",
  "gcg" => "chemical/x-gcg8-sequence",
  "gen" => "chemical/x-genbank",
  "hin" => "chemical/x-hin",
  "istr" => "chemical/x-isostar",
  "ist" => "chemical/x-isostar",
  "jdx" => "chemical/x-jcamp-dx",
  "dx" => "chemical/x-jcamp-dx",
  "kin" => "chemical/x-kinemage",
  "mcm" => "chemical/x-macmolecule",
  "mmd" => "chemical/x-macromodel-input",
  "mmod" => "chemical/x-macromodel-input",
  "mol" => "chemical/x-mdl-molfile",
  "rd" => "chemical/x-mdl-rdfile",
  "rxn" => "chemical/x-mdl-rxnfile",
  "sd" => "chemical/x-mdl-sdfile",
  "sdf" => "chemical/x-mdl-sdfile",
  "tgf" => "chemical/x-mdl-tgf",
  "mcif" => "chemical/x-mmcif",
  "mol2" => "chemical/x-mol2",
  "b" => "chemical/x-molconn-Z",
  "gpt" => "chemical/x-mopac-graph",
  "mop" => "chemical/x-mopac-input",
  "mopcrt" => "chemical/x-mopac-input",
  "mpc" => "chemical/x-mopac-input",
  "zmt" => "chemical/x-mopac-input",
  "moo" => "chemical/x-mopac-out",
  "mvb" => "chemical/x-mopac-vib",
  "asn" => "chemical/x-ncbi-asn1",
  "prt" => "chemical/x-ncbi-asn1-ascii",
  "ent" => "chemical/x-ncbi-asn1-ascii",
  "val" => "chemical/x-ncbi-asn1-binary",
  "aso" => "chemical/x-ncbi-asn1-binary",
  "asn" => "chemical/x-ncbi-asn1-spec",
  "pdb" => "chemical/x-pdb",
  "ent" => "chemical/x-pdb",
  "ros" => "chemical/x-rosdal",
  "sw" => "chemical/x-swissprot",
  "vms" => "chemical/x-vamas-iso14976",
  "vmd" => "chemical/x-vmd",
  "xtel" => "chemical/x-xtel",
  "xyz" => "chemical/x-xyz",
  "gif" => "image/gif",
  "ief" => "image/ief",
  "jpeg" => "image/jpeg",
  "jpg" => "image/jpeg",
  "jpe" => "image/jpeg",
  "pcx" => "image/pcx",
  "png" => "image/png",
  "svg" => "image/svg+xml",
  "svgz" => "image/svg+xml",
  "tiff" => "image/tiff",
  "tif" => "image/tiff",
  "djvu" => "image/vnd.djvu",
  "djv" => "image/vnd.djvu",
  "ico" => "image/vnd.microsoft.icon",
  "wbmp" => "image/vnd.wap.wbmp",
  "cr2" => "image/x-canon-cr2",
  "crw" => "image/x-canon-crw",
  "ras" => "image/x-cmu-raster",
  "cdr" => "image/x-coreldraw",
  "pat" => "image/x-coreldrawpattern",
  "cdt" => "image/x-coreldrawtemplate",
  "cpt" => "image/x-corelphotopaint",
  "erf" => "image/x-epson-erf",
  "art" => "image/x-jg",
  "jng" => "image/x-jng",
  "bmp" => "image/x-ms-bmp",
  "nef" => "image/x-nikon-nef",
  "orf" => "image/x-olympus-orf",
  "psd" => "image/x-photoshop",
  "pnm" => "image/x-portable-anymap",
  "pbm" => "image/x-portable-bitmap",
  "pgm" => "image/x-portable-graymap",
  "ppm" => "image/x-portable-pixmap",
  "rgb" => "image/x-rgb",
  "xbm" => "image/x-xbitmap",
  "xpm" => "image/x-xpixmap",
  "xwd" => "image/x-xwindowdump",
  "eml" => "message/rfc822",
  "igs" => "model/iges",
  "iges" => "model/iges",
  "msh" => "model/mesh",
  "mesh" => "model/mesh",
  "silo" => "model/mesh",
  "wrl" => "model/vrml",
  "vrml" => "model/vrml",
  "x3dv" => "model/x3d+vrml",
  "x3d" => "model/x3d+xml",
  "x3db" => "model/x3d+binary",
  "appcache" => "text/cache-manifest",
  "ics" => "text/calendar",
  "icz" => "text/calendar",
  "css" => "text/css",
  "csv" => "text/csv",
  "323" => "text/h323",
  "html" => "text/html",
  "htm" => "text/html",
  "shtml" => "text/html",
  "uls" => "text/iuls",
  "mml" => "text/mathml",
  "asc" => "text/plain",
  "txt" => "text/plain",
  "text" => "text/plain",
  "pot" => "text/plain",
  "brf" => "text/plain",
  "srt" => "text/plain",
  "rtx" => "text/richtext",
  "sct" => "text/scriptlet",
  "wsc" => "text/scriptlet",
  "tm" => "text/texmacs",
  "tsv" => "text/tab-separated-values",
  "jad" => "text/vnd.sun.j2me.app-descriptor",
  "wml" => "text/vnd.wap.wml",
  "wmls" => "text/vnd.wap.wmlscript",
  "bib" => "text/x-bibtex",
  "boo" => "text/x-boo",
  "h++" => "text/x-c++hdr",
  "hpp" => "text/x-c++hdr",
  "hxx" => "text/x-c++hdr",
  "hh" => "text/x-c++hdr",
  "c++" => "text/x-c++src",
  "cpp" => "text/x-c++src",
  "cxx" => "text/x-c++src",
  "cc" => "text/x-c++src",
  "h" => "text/x-chdr",
  "htc" => "text/x-component",
  "csh" => "text/x-csh",
  "c" => "text/x-csrc",
  "d" => "text/x-dsrc",
  "diff" => "text/x-diff",
  "patch" => "text/x-diff",
  "hs" => "text/x-haskell",
  "java" => "text/x-java",
  "ly" => "text/x-lilypond",
  "lhs" => "text/x-literate-haskell",
  "moc" => "text/x-moc",
  "p" => "text/x-pascal",
  "pas" => "text/x-pascal",
  "gcd" => "text/x-pcs-gcd",
  "pl" => "text/x-perl",
  "pm" => "text/x-perl",
  "py" => "text/x-python",
  "scala" => "text/x-scala",
  "etx" => "text/x-setext",
  "sfv" => "text/x-sfv",
  "sh" => "text/x-sh",
  "tcl" => "text/x-tcl",
  "tk" => "text/x-tcl",
  "tex" => "text/x-tex",
  "ltx" => "text/x-tex",
  "sty" => "text/x-tex",
  "cls" => "text/x-tex",
  "vcs" => "text/x-vcalendar",
  "vcf" => "text/x-vcard",
  "3gp" => "video/3gpp",
  "axv" => "video/annodex",
  "dl" => "video/dl",
  "dif" => "video/dv",
  "dv" => "video/dv",
  "fli" => "video/fli",
  "gl" => "video/gl",
  "mpeg" => "video/mpeg",
  "mpg" => "video/mpeg",
  "mpe" => "video/mpeg",
  "ts" => "video/MP2T",
  "mp4" => "video/mp4",
  "qt" => "video/quicktime",
  "mov" => "video/quicktime",
  "ogv" => "video/ogg",
  "webm" => "video/webm",
  "mxu" => "video/vnd.mpegurl",
  "flv" => "video/x-flv",
  "lsf" => "video/x-la-asf",
  "lsx" => "video/x-la-asf",
  "mng" => "video/x-mng",
  "asf" => "video/x-ms-asf",
  "asx" => "video/x-ms-asf",
  "wm" => "video/x-ms-wm",
  "wmv" => "video/x-ms-wmv",
  "wmx" => "video/x-ms-wmx",
  "wvx" => "video/x-ms-wvx",
  "avi" => "video/x-msvideo",
  "movie" => "video/x-sgi-movie",
  "mpv" => "video/x-matroska",
  "mkv" => "video/x-matroska",
  "ice" => "x-conference/x-cooltalk",
  "sisx" => "x-epoc/x-sisx-app",
  "vrm" => "x-world/x-vrml",
  "vrml" => "x-world/x-vrml",
  "wrl" => "x-world/x-vrml",
  "" => ""
}

end


text:
function PartialDerivativesEquations(arg0::JString, arg1::NumericalPropagator)
    return PartialDerivativesEquations((JString, NumericalPropagator), arg0, arg1)
end

function get_mapper(obj::PartialDerivativesEquations)
    return jcall(obj, "getMapper", JacobiansMapper, ())
end


text:
"""
    __new__(T, args...)

User-level version of the `new()` pseudofunction.
Can be used to construct most Julia types, including structs
without default constructors, closures, etc.
"""
@generated function __new__(T, args...)
    return Expr(:splatnew, :T, :args)
end

"Check if an object is of a struct type, i.e. not a number or array"
isstruct(::Type{T}) where T = !isempty(fieldnames(T))
isstruct(obj) = isstruct(typeof(obj))

if !isdefined(@__MODULE__, :__EXPRESSION_HASHES__)
    __EXPRESSION_HASHES__ = Set{AbstractString}()
end

function get_type_parameters(sig)
    if sig isa UnionAll
        return get_type_parameters(sig.body)
    elseif sig isa DataType
        return sig.parameters
    else
        error("Unsupported type: $sig")
    end
end

function map_type_parameters(fn, sig)
    if sig isa UnionAll
        new_body = map_type_parameters(fn, sig.body)
        return UnionAll(sig.var, new_body)
    elseif sig isa DataType
        params = sig.parameters
        return Tuple{fn(params)...}
    else
        error("Unsupported type: $sig")
    end
end

remove_first_parameter(sig) = map_type_parameters(ps -> ps[2:end], sig)
kwfunc_signature(sig) = map_type_parameters(sig) do ps
    F = ps[1]
    isabstracttype(F) && return []
    Ts = ps[2:end]
    kw_F = Core.kwftype(F)
    return [kw_F, Any, F, Ts...]
end


text:
type B <: A
    x
    y
end

type C <: A
    w
    z
end


text:
@testset "Test generated concrete generator functions" begin

    @testset "Type conversion for default type" begin
        def_T = Float64

        Types_changed = [Bool, Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128]
        Types_unchanged = [ComplexF32, ComplexF64, Complex{Int}, Float32, Float64]

        for T in Types_changed
            IndexFunArrays.default_type(T, Float64) = Float64
            for T2 in Types_changed
                @test IndexFunArrays.default_type(T, def_T) == def_T
            end
        end

        for T in Types_unchanged
            for T2 in Types_changed
                @test IndexFunArrays.default_type(T, T2) == T
            end
        end
    end

    @testset "Test rr method" begin

        offsets = [(3,3), (5,2)]
        scales = [(1.0,1.0), (2.0,0.5)]
        s = ((7,7))
        @test rr(s, offset=offsets, scale=scales) ≈ reduce(.+, rr(s, offset=o, scale=sc) for (o,sc) in zip(offsets,scales))

        @test rr2(s, offset=offsets, scale=scales) ≈ reduce(.+, rr2(s, offset=o, scale=sc) for (o,sc) in zip(offsets,scales))

        @test rr((5,)) == [2.0, 1.0, 0.0, 1.0, 2.0]
        @test rr(Int, (5,)) == [2, 1, 0, 1, 2]
        @test rr((4, 4)) == [2.8284271247461903 2.23606797749979 2.0 2.23606797749979; 2.23606797749979 1.4142135623730951 1.0 1.4142135623730951; 2.0 1.0 0.0 1.0; 2.23606797749979 1.4142135623730951 1.0 1.4142135623730951]

        @test rr((2, 3), offset=(1, 1)) == [0.0 1.0 2.0; 1.0 1.4142135623730951 2.23606797749979]

        @test rr((2, 3), offset=(1, 1)) == [0.0 1.0 2.0; 1.0 1.4142135623730951 2.23606797749979]

        @test rr(Float64, (3, 5), offset = CtrCorner, scale = ScaUnit) == [0.0 1.0 2.0 3.0 4.0; 1.0 1.4142135623730951 2.23606797749979 3.1622776601683795 4.123105625617661; 2.0 2.23606797749979 2.8284271247461903 3.605551275463989 4.47213595499958]

        @test rr2(Int, (10, )) == [25; 16; 9; 4; 1; 0; 1; 4; 9; 16]
        @test rr2(Int, (10, 1)) == reshape([25; 16; 9; 4; 1; 0; 1; 4; 9; 16], (10, 1))
        @test rr2(Int, (10, 1)) == reshape([25; 16; 9; 4; 1; 0; 1; 4; 9; 16], (10, 1))
    end

    @testset "Test rr2 method for an array as input" begin
        x = rand((1:100), (10,))
        @test rr2(x) == [25; 16; 9; 4; 1; 0; 1; 4; 9; 16]
        x = rand((1:100), (10,1))
        @test rr2(x) == reshape([25; 16; 9; 4; 1; 0; 1; 4; 9; 16], (10, 1))

    end

    @testset "Test xx, yy, zz, ee and tt methods" begin
        a = xx(Float64, (3, 5), offset = CtrCorner, scale = ScaUnit)
        b = yy(Float64, (5, 3), offset = CtrCorner, scale = ScaUnit)
        c = zz(Float64, (1, 5, 3), offset = CtrCorner, scale = ScaUnit)
        d = ee(Float64, (1, 1, 5, 3), offset = CtrCorner, scale = ScaUnit)
        e = tt(Float64, (1, 1, 1, 5, 3), offset = CtrCorner, scale = ScaUnit)
        @test a == [0.0 0.0 0.0 0.0 0.0; 1.0 1.0 1.0 1.0 1.0; 2.0 2.0 2.0 2.0 2.0]
        @test a == b'
        @test (b.+0)[:] == (c.+0)[:]
        @test (b.+0)[:] == (d.+0)[:]
        @test (b.+0)[:] == (e.+0)[:]
        @test size(c) == (1,5,3)
        @test size(d) == (1,1,5,3)
        @test size(e) == (1,1,1,5,3)

        @test xx(Int, (3, 5), offset = CtrCorner, scale = ScaUnit) == [0 0 0 0 0; 1 1 1 1 1; 2 2 2 2 2]
    end

    @testset "Test idx_min and idx_max methods" begin
        sz = (13, 15)
        a = idx_min(Int, sz , offset = CtrCorner, scale = ScaUnit)
        b = idx_max(Int, sz, offset = CtrCorner, scale = ScaUnit)
        a == min.(xx(sz, offset = CtrCorner), yy(sz, offset = CtrCorner))
        b == max.(xx(sz, offset = CtrCorner), yy(sz, offset = CtrCorner))
    end

    @testset "Test the ramp method" begin
        a = yy(Float64, (1, 5), offset = CtrCorner, scale = ScaUnit)
        b = ramp(Int64, 2, 5, offset = CtrCorner, scale = ScaUnit)
        @test a == [0,1,2,3,4]'
        @test size(ramp(7,7)) == (1,1,1,1,1,1,7)
    end

    @testset "Test delta method" begin
        a = delta(Float64, (3, 5), offset = CtrCorner, scale = ScaUnit)
        @test  a == [1.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0]
    end

    @testset "Test different scalings" begin
        @test rr((10,), scale = ScaNorm) == [0.5555555555555556, 0.4444444444444444, 0.3333333333333333, 0.2222222222222222, 0.1111111111111111, 0.0, 0.1111111111111111, 0.2222222222222222, 0.3333333333333333, 0.4444444444444444]
        @test rr((10,), scale = ScaFT) == [0.5, 0.4, 0.30000000000000004, 0.2, 0.1, 0.0, 0.1, 0.2, 0.30000000000000004, 0.4]

        @test rr((11,), scale = ScaFTEdge) == [ 0.9090909090909092, 0.7272727272727273, 0.5454545454545454, 0.36363636363636365, 0.18181818181818182, 0.0, 0.18181818181818182, 0.36363636363636365, 0.5454545454545454, 0.7272727272727273, 0.9090909090909092]
        # 1.0, 0.8, 0.6000000000000001, 0.4, 0.2, 0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0]

        @test rr((10,), scale = ScaFTEdge) == [1.0, 0.8, 0.6000000000000001, 0.4, 0.2, 0.0, 0.2, 0.4, 0.6000000000000001, 0.8]

        @test rr((11,), scale = (5,)) == [25.0, 20.0, 15.0, 10.0, 5.0, 0.0, 5.0, 10.0, 15.0, 20.0, 25.0]

        @test rr((4,), scale = (5,)) == [10.0, 5.0, 0.0, 5.0]
    end

    @testset "Test rr for different offsets" begin
        @test rr2(Int, (5, 5), offset = CtrFFT) == [0 1 4 9 16; 1 2 5 10 17; 4 5 8 13 20; 9 10 13 18 25; 16 17 20 25 32]
        @test rr2(Int, (4, 4), offset = CtrFFT) == [0 1 4 9; 1 2 5 10; 4 5 8 13; 9 10 13 18]
        @test rr2((5, 5), offset = CtrMid) == [8.0 5.0 4.0 5.0 8.0; 5.0 2.0 1.0 2.0 5.0; 4.0 1.0 0.0 1.0 4.0; 5.0 2.0 1.0 2.0 5.0; 8.0 5.0 4.0 5.0 8.0]
        @test rr2((4, 4), offset = CtrMid) == [4.5 2.5 2.5 4.5; 2.5 0.5 0.5 2.5; 2.5 0.5 0.5 2.5; 4.5 2.5 2.5 4.5]

        @test rr2((4, 4), offset = CtrEnd) == [18.0 13.0 10.0 9.0; 13.0 8.0 5.0 4.0; 10.0 5.0 2.0 1.0; 9.0 4.0 1.0 0.0]
        @test rr2((5, 6), offset = CtrEnd) == [41.0 32.0 25.0 20.0 17.0 16.0; 34.0 25.0 18.0 13.0 10.0 9.0; 29.0 20.0 13.0 8.0 5.0 4.0; 26.0 17.0 10.0 5.0 2.0 1.0; 25.0 16.0 9.0 4.0 1.0 0.0]

    end
end

@testset "Test idx method" begin
    @test idx(Float32, (5,)) == Tuple{Float32}[(-2.0,), (-1.0,), (0.0,), (1.0,), (2.0,)]
    @test idx(Int8, (5, 2)) == Tuple{Int8, Int8}[(-2, -1) (-2, 0); (-1, -1) (-1, 0); (0, -1) (0, 0); (1, -1) (1, 0); (2, -1) (2, 0)]

    @test idx(randn((5,2))) == Tuple{Int8, Int8}[(-2, -1) (-2, 0); (-1, -1) (-1, 0); (0, -1) (0, 0); (1, -1) (1, 0); (2, -1) (2, 0)]
end

@testset "Test cpx method" begin
    @test cpx(Float32, (5,)) == [-2.0f0 + 0.0f0im, -1.0f0 + 0.0f0im, 0.0f0 + 0.0f0im,1.0f0 + 0.0f0im,2.0f0 + 0.0f0im]
    @test cpx(Int8, (5, 2)) == Complex{Int8}[(-2-1im) (-2+0im);(-1-1im) (-1+0im);(0-1im) (0+0im);(1-1im) (1+0im);(2-1im) (2+0im)]

    @test cpx(randn((5,2))) == Complex{Int8}[(-2-1im) (-2+0im);(-1-1im) (-1+0im);(0-1im) (0+0im);(1-1im) (1+0im);(2-1im) (2+0im)]
    @test cpx((5,2)) == Complex{Int8}[(-2-1im) (-2+0im);(-1-1im) (-1+0im);(0-1im) (0+0im);(1-1im) (1+0im);(2-1im) (2+0im)]
end

@testset "Test exp_ikx method" begin
    @test exp_ikx(ComplexF32, (5,)) ≈ ComplexF32[0.3090170656104166 - 0.9510564931493436im, -0.8090170163879177 + 0.5877852219942177im, 1.0 - 0.0im, -0.8090170163879177 - 0.5877852219942177im, 0.3090170656104166 + 0.9510564931493436im]
    @test exp_ikx(randn((2,2))) ≈ [1.0 (-1.0);-1.0 1.0]
    @test exp_ikx(((2,2))) ≈ [1.0 (-1.0);-1.0 1.0]
    @test eltype(exp_ikx(ComplexF32, ((2,2)))) == ComplexF32
    @test exp_ikx(ComplexF32, ((2,2)))[1] isa ComplexF32
end

@testset "Check dims argument" begin
    @test abs.(rr((5,5), dims=1)) == abs.(xx((5,5)))
    @test abs.(rr((6,6), dims=1)) == abs.(xx((6,6)))

    @test idx((3, 3), dims = 2) == [(-0.0, -1.0) (-0.0, 0.0) (-0.0, 1.0); (0.0, -1.0) (0.0, 0.0) (0.0, 1.0); (0.0, -1.0) (0.0, 0.0) (0.0, 1.0)]
    @test idx((4, 4), dims = (1, 2)) == [(-2.0, -2.0) (-2.0, -1.0) (-2.0, 0.0) (-2.0, 1.0); (-1.0, -2.0) (-1.0, -1.0) (-1.0, 0.0) (-1.0, 1.0); (0.0, -2.0) (0.0, -1.0) (0.0, 0.0) (0.0, 1.0); (1.0, -2.0) (1.0, -1.0) (1.0, 0.0) (1.0, 1.0)]
end

@testset "Test window functions" begin

    # simple window_hanning test
    @testset "window_hanning window test" begin
        window_hanning((10, ), border_in=0, border_out=1) ≈ sinpi.(range(0, 1, length=11)[1:end-1]).^2
        window_hanning((10, ), border_in=0.4, border_out=0.499) ≈ [0, 0, 0, 1, 1, 1, 1, 1, 0, 0]

        x = window_hanning(Int, (10, ), border_in=0.4, border_out=0.499)
        @test x ≈ [0, 0, 0, 1, 1, 1, 1, 1, 0, 0]
        @test typeof(x) <: IndexFunArray{Int, 1, T} where T

        x = window_hanning(ComplexF32, (10, ), border_in=0.4, border_out=0.499)
        @test x ≈ [0, 0, 0, 1, 1, 1, 1, 1, 0, 0]
        @test typeof(x) <: IndexFunArray{ComplexF32, 1, T} where T

        x = window_hanning(Float32.(randn((10, ))), border_in=0.4, border_out=0.499)
        @test x ≈ [0, 0, 0, 1, 1, 1, 1, 1, 0, 0]
        @test typeof(x) <: IndexFunArray{Float32, 1, T} where T

    end

    @testset "window_linear and window_radial_linear" begin

        @test window_linear((4, 4), border_in = 0, border_out = 1) == [0.0 0.0 0.0 0.0; 0.0 0.25 0.5 0.25; 0.0 0.5 1.0 0.5; 0.0 0.25 0.5 0.25]
        @test window_linear((4, 4), border_in = 0, border_out = 1, offset = CtrCorner, scale = ScaNorm) == [1.0 0.6666666666666667 0.33333333333333337 0.0; 0.6666666666666667 0.44444444444444453 0.22222222222222227 0.0; 0.33333333333333337 0.22222222222222227 0.11111111111111113 0.0; 0.0 0.0 0.0 0.0]

        @test window_radial_linear((4, 4), border_in = 0, border_out = 1, offset = CtrCorner, scale = ScaNorm) == [1.0 0.6666666666666667 0.33333333333333337 0.0; 0.6666666666666667 0.5285954792089683 0.2546440075000701 0.0; 0.33333333333333337 0.2546440075000701 0.057190958417936644 0.0; 0.0 0.0 0.0 0.0]
    end

    @testset "window_half_cos" begin
        @test sum(abs.(window_half_cos((1, 14), border_in = 0, border_out = 1) .- [0.0  0.222521  0.433884  0.62349  0.781831  0.900969  0.974928  1.0  0.974928  0.900969  0.781831  0.62349  0.433884  0.222521])) < 1e-5
    end

    @testset "box and disc" begin
        sz = (77,202)
        boxsize = (14,99)
        sc = (1.3,2.3)
        @test box(sz,boxsize) == (abs.(xx(sz)) .< (boxsize[1]./2)) .* (abs.(yy(sz)) .< (boxsize[2]./2))
        @test box(zeros(sz),boxsize,scale=sc) == (abs.(xx(sz,scale=sc)) .< (boxsize[1]./2)) .* (abs.(yy(sz,scale=sc)) .< (boxsize[2]./2))
        @test box(sz,boxsize, offset=CtrCorner) == (abs.(xx(sz, offset=CtrCorner)) .< (boxsize[1]./2)) .* (abs.(yy(sz, offset=CtrCorner)) .< (boxsize[2]./2))
        @test box(zeros(sz),boxsize, offset=CtrCorner) == (abs.(xx(sz, offset=CtrCorner)) .< (boxsize[1]./2)) .* (abs.(yy(sz, offset=CtrCorner)) .< (boxsize[2]./2))
        disc_rad = 33.0
        @test disc(sz,disc_rad) ≈ (rr(sz) .<= disc_rad)
        @test disc(zeros(sz),disc_rad,scale=sc) ≈ (rr(sz, scale=sc) .<= disc_rad)
        @test disc(sz,disc_rad, offset=CtrCorner) ≈ (rr(sz, offset=CtrCorner) .<= disc_rad)
        @test disc(zeros(sz),disc_rad, offset=CtrCorner) ≈ (rr(sz, offset=CtrCorner) .<= disc_rad)
    end
    @testset "axes1d" begin
        sz = (2,3,4)
        x,y,z = axes1d(sz, offset=(0,0,0))
        @test x == 1:sz[1]
        @test y == 1:sz[2]
        @test z == 1:sz[3]
        x,y,z = axes1d(sz, offset=CtrCorner, keepdims=true)
        @test size(x) == (sz[1],)
        @test size(y) == (1,sz[2])
        @test size(z) == (1,1,sz[3])
        @test x[:] == 0:sz[1]-1
        @test y[:] == 0:sz[2]-1
        @test z[:] == 0:sz[3]-1
    end
end


text:
module SymmetricPolynomials

using TupleTools
using DataStructures

export semi_elementary_monomial,decompose

include("utilities.jl")
include("semi elementary monomial.jl")
include("semi elementary polynomial.jl")
include("evaluate.jl")

end

max_stars_repo_name: yushoteke/SymmetricPolynomials

max_issues_repo_name: yushoteke/SymmetricPolynomials

max_forks_repo_name: yushoteke/SymmetricPolynomials


text:
import TableCellCounts
import Test

import CategoricalArrays
import DataFrames
import TypedTables

using Test: @testset, @test, @test_throws

@testset "TableCellCounts.jl" begin
    @test TableCellCounts._hello_world() == "hello world"
end


text:
#*******************************************************/
#* Copyright(c) 2018 by Artelys                        */
#* This source code is subject to the terms of the     */
#* MIT Expat License (see LICENSE.md)                  */
#*******************************************************/

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This example demonstrates how to use Knitro to solve the following
# simple nonlinear optimization problem.  This model is test problem
# HS40 from the Hock & Schittkowski collection.
#
# max   x0*x1*x2*x3        (obj)
# s.t.  x0^3 + x1^2 = 1    (c0)
#       x0^2*x3 - x2 = 0   (c1)
#       x3^2 - x1 = 0      (c2)
#
# This example also shows show to use the "newpt" callback, which
# can be used to perform some user-defined task every time Knitro
# iterates to a new solution estimate(e.g. it can be used to define
# a customized stopping condition).
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

using KNITRO, Test

#*------------------------------------------------------------------*
#*     FUNCTION callbackEvalFC                                      *
#*------------------------------------------------------------------*
# The signature of this function matches KNITRO.KN_eval_callback in knitro.h.
# Only "obj" and "c" are set in the KNITRO.KN_eval_result structure.
function callbackEvalFC(kc, cb, evalRequest, evalResult, userParams)
    x = evalRequest.x

    # Evaluate nonlinear term in objective
    evalResult.obj[1] = x[1] * x[2] * x[3] * x[4]

    # Evaluate nonlinear terms in constraints
    evalResult.c[1] = x[1] * x[1] * x[1]
    evalResult.c[2] = x[1] * x[1] * x[4]

    return 0
end

#*------------------------------------------------------------------*
#*     FUNCTION callbackEvalGA                                      *
#*------------------------------------------------------------------*
# The signature of this function matches KNITRO.KN_eval_callback in knitro.h.
# Only "objGrad" and "jac" are set in the KNITRO.KN_eval_result structure.
function callbackEvalGA(kc, cb, evalRequest, evalResult, userParams)
    x = evalRequest.x

    # Evaluate nonlinear term in objective gradient
    evalResult.objGrad[1] = x[2] * x[3] * x[4]
    evalResult.objGrad[2] = x[1] * x[3] * x[4]
    evalResult.objGrad[3] = x[1] * x[2] * x[4]
    evalResult.objGrad[4] = x[1] * x[2] * x[3]

    # Evaluate nonlinear terms in constraint gradients(Jacobian)
    evalResult.jac[1] = 3.0 * x[1] * x[1] # derivative of x0^3 term  wrt x0
    evalResult.jac[2] = 2.0 * x[1] * x[4] # derivative of x0^2 * x3 term  wrt x0
    evalResult.jac[3] = x[1] * x[1]       # derivative of x0^2 * x3 terms wrt x3

    return 0
end

#*------------------------------------------------------------------*
#*     FUNCTION callbackEvalH                                       *
#*------------------------------------------------------------------*
# The signature of this function matches KNITRO.KN_eval_callback in knitro.h.
# Only "hess" or "hessVec" are set in the KNITRO.KN_eval_result structure.
function callbackEvalH(kc, cb, evalRequest, evalResult, userParams)
    x = evalRequest.x
    lambda_ = evalRequest.lambda
    # Scale objective component of hessian by sigma
    sigma = evalRequest.sigma

    # Evaluate nonlinear term in the Hessian of the Lagrangian.
    # Note: If sigma=0, some computations can be avoided.
    if sigma > 0.0 # Evaluate the full Hessian of the Lagrangian
        evalResult.hess[1] = lambda_[1] * 6.0 * x[1] + lambda_[2] * 2.0 * x[4]
        evalResult.hess[2] = sigma * x[3] * x[4]
        evalResult.hess[3] = sigma * x[2] * x[4]
        evalResult.hess[4] = sigma * x[2] * x[3] + lambda_[2] * 2.0 * x[1]
        evalResult.hess[5] = sigma * x[1] * x[4]
        evalResult.hess[6] = sigma * x[1] * x[3]
        evalResult.hess[7] = sigma * x[1] * x[2]
    else # sigma=0, do not include objective component
        evalResult.hess[1] = lambda_[1] * 6.0 * x[1] + lambda_[2] * 2.0 * x[4]
        evalResult.hess[2] = 0.0
        evalResult.hess[3] = 0.0
        evalResult.hess[4] = lambda_[2] * 2.0 * x[1]
        evalResult.hess[5] = 0.0
        evalResult.hess[6] = 0.0
        evalResult.hess[7] = 0.0
    end

    return 0
end

#*------------------------------------------------------------------*
#*     FUNCTION callbackNewPoint                                    *
#*------------------------------------------------------------------*
# The signature of this function matches KNITRO.KN_user_callback in
# knitro.h.  Nothing should be modified.  This example printlns out
# that Knitro has iterated to a new point(x, lambda) that it
# considers an improvement over the previous iterate, and printlns
# out the current feasibility error and number of evaluations.
# To exercise it, edit "knitro.opt" and set the the "newpoint"
# option to "user".  The demonstration looks best if the "outlev"
# option is set to 5 or 6.
function callbackNewPoint(kc, x, lambda_, userParams)

    # Get the number of variables in the model
    n = KNITRO.KN_get_number_vars(kc)

    println(">> New point computed by Knitro:(", x, ")")

    # Query information about the current problem.
    dFeasError = KNITRO.KN_get_abs_feas_error(kc)
    println("Number FC evals= ", KNITRO.KN_get_number_FC_evals(kc))
    println("Current feasError= " , dFeasError)

    # Demonstrate user-defined termination
    #(Uncomment to activate)
    if KNITRO.KN_get_obj_value(kc) > 0.2 && dFeasError <= 1.0e-4
        return KNITRO.KN_RC_USER_TERMINATION
    end

    return 0
end

#*------------------------------------------------------------------*
#*     main                                                         *
#*------------------------------------------------------------------*

# Create a new Knitro solver instance.
kc = KNITRO.KN_new()

# Initialize Knitro with the problem definition.

# Add the variables and specify initial values for them.
# Note: any unset lower bounds are assumed to be
# unbounded below and any unset upper bounds are
# assumed to be unbounded above.
xIndices = KNITRO.KN_add_vars(kc, 4)
for x in xIndices
    KNITRO.KN_set_var_primal_init_values(kc, x, 0.8)
end

# Add the constraints and set the rhs and coefficients
KNITRO.KN_add_cons(kc, 3)
KNITRO.KN_set_con_eqbnds(kc, [1.0, 0.0, 0.0])

# Coefficients for 2 linear terms
lconIndexCons = Int32[1, 2]
lconIndexVars = Int32[2, 1]
lconCoefs = [-1.0, -1.0]
KNITRO.KN_add_con_linear_struct(kc, lconIndexCons, lconIndexVars, lconCoefs)

# Coefficients for 2 quadratic terms

# 1st term:  x1^2 term in c0
# 2nd term:  x3^2 term in c2
qconIndexCons = Int32[0, 2]
qconIndexVars1 = Int32[1, 3]
qconIndexVars2 = Int32[1, 3]
qconCoefs = [1.0, 1.0]

KNITRO.KN_add_con_quadratic_struct(kc, qconIndexCons, qconIndexVars1, qconIndexVars2, qconCoefs)

# Add callback to evaluate nonlinear(non-quadratic) terms in the model:
#    x0*x1*x2*x3  in the objective
#    x0^3         in first constraint c0
#    x0^2*x3      in second constraint c1
cb = KNITRO.KN_add_eval_callback(kc, true, Int32[0, 1], callbackEvalFC)

# Set obj. gradient and nonlinear jac provided through callbacks.
# Mark objective gradient as dense, and provide non-zero sparsity
# structure for constraint Jacobian terms.
cbjacIndexCons = Int32[0, 1, 1]
cbjacIndexVars = Int32[0, 0, 3]
KNITRO.KN_set_cb_grad(kc, cb, callbackEvalGA, jacIndexCons=cbjacIndexCons, jacIndexVars=cbjacIndexVars)

# Set nonlinear Hessian provided through callbacks. Since the
# Hessian is symmetric, only the upper triangle is provided.
# The upper triangular Hessian for nonlinear callback structure is:
#    # lambda0*6*x0 + lambda1*2*x3     x2*x3    x1*x3    x1*x2 + lambda1*2*x0
#              0                    0      x0*x3         x0*x2
#                                            0           x0*x1
#                                                         0
#(7 nonzero elements)
cbhessIndexVars1 = Int32[0, 0, 0, 0, 1, 1, 2]
cbhessIndexVars2 = Int32[0, 1, 2, 3, 2, 3, 3]
KNITRO.KN_set_cb_hess(kc, cb, length(cbhessIndexVars1), callbackEvalH,
                      hessIndexVars1=cbhessIndexVars1,  hessIndexVars2=cbhessIndexVars2)

# Set minimize or maximize(if not set, assumed minimize)
KNITRO.KN_set_obj_goal(kc, KNITRO.KN_OBJGOAL_MAXIMIZE)

# Demonstrate setting a "newpt" callback.  the callback function
# "callbackNewPoint" passed here is invoked after Knitro computes
# a new estimate of the solution.
KNITRO.KN_set_newpt_callback(kc, callbackNewPoint)

# Set option to println output after every iteration.
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_OUTLEV, KNITRO.KN_OUTLEV_ITER)

# Solve the problem.
#
# Return status codes are defined in "knitro.h" and described
# in the Knitro manual.
nStatus = KNITRO.KN_solve(kc)

println()
println("Knitro converged with final status = ", nStatus)

# An example of obtaining solution information.
nStatus, objSol, x, lambda_ =  KNITRO.KN_get_solution(kc)
println("  optimal objective value  = ", objSol)
println("  optimal primal values x  = ", x)
println("  feasibility violation    = ", KNITRO.KN_get_abs_feas_error(kc))
println("  KKT optimality violation = ", KNITRO.KN_get_abs_opt_error(kc))

# Delete the Knitro solver instance.
KNITRO.KN_free(kc)

@testset "Example HS40 nlp2" begin
    @test nStatus == KNITRO.KN_RC_USER_TERMINATION
    @test objSol ≈ 0.25 atol=1e-4
    @test x ≈ [0.793701, 0.707107, 0.529732, 0.840896] atol=1e-4
end


text:
#!/bin/julia

#############################################
# Garamond script for CLI client operations #
#############################################

module garc  # The Garamond client

using Sockets
using ArgParse
using Logging
using JSON

# Support for parsing to Symbol for the ArgParse package
import ArgParse: parse_item
function ArgParse.parse_item(::Type{Symbol}, x::AbstractString)
    return Symbol(x)
end

# Function that parses Garamond's unix-socket client arguments
function get_unix_socket_client_arguments(args::Vector{String})
	s = ArgParseSettings()
	@add_arg_table! s begin
        "query"
            help = "the search query"
            arg_type = String
            default = ""
        "--log-level"
            help = "logging level"
            default = "warn"
        "--unix-socket", "-u"
            help = "UNIX socket for data communication"
            default = ""
            arg_type = String
        "--return-fields"
            help = "List of fields to return (ignores wrong names)"
            nargs = '*'
        "--sort-fields"
            help = "List of fields to sort by when filtering (ignores wrong names)"
            nargs = '*'
        "--sort-reverse"
            help = "Reverse the filter sort order"
            arg_type = Bool
            default = false
        "--pretty"
            help = "output is a pretty print of the results"
            action = :store_true
        "--max-matches"
            help = "maximum number of results for internal neighbor searches"
            arg_type = Int
            default = 10
        "--response-size"
            help = "maximum number of results to return"
            arg_type = Int
            default = 10
        "--response-page"
            help = "index of page with results to return"
            arg_type = Int
            default = 1
        "--search-method"
            help = "type of match done during search"
            arg_type = Symbol
            default = :exact
        "--max-suggestions"
            help = "How many suggestions to return for each mismatched query term"
            arg_type = Int
            default = 0
        "--id-key"
            help = "The linear ID key"
            arg_type = String
            default = "garamond_linear_id"
        "--kill", "-k"
            help = "Kill the search engine server"
            action = :store_true
        "--env-operation"
            help = "Environment operation"
            arg_type = String
            nargs = 2
        "--ranker"
            help = "The ranker to use; avalilable: noop_ranker"
            arg_type = String
            default = "noop_ranker"
        "--input-parser"
            help = "The input parser to use; available: noop_input_parser, base_input_parser"
            arg_type = String
            default = "noop_input_parser"
	end
	return parse_args(args,s)
end

# Function that constructs a search server JSON request
# (can be a search request or a command) from a set of arguments
# that come usually from parsed client input arguments
function construct_json_request(args)
    # Construct the basic dictionary corresponding to the JSON request
    dictreq = Dict("operation" => "",
                   "query" => "",
                   "max_matches" => args["max-matches"],
                   "max_suggestions" => args["max-suggestions"],
                   "search_method" => args["search-method"],
                   "return_fields" => args["return-fields"],
                   "sort_fields" => args["sort-fields"],
                   "sort_reverse" => args["sort-reverse"],
                   "custom_weights" =>Dict(),
                   "request_id_key" => args["id-key"],
                   "response_size" => args["response-size"],
                   "response_page" => args["response-page"],
                   "ranker" => args["ranker"],
                   "input_parser" => args["input-parser"],
                   "recommender" => "noop_recommender",
                   "searchable_filters" => [])
    # Kill request
    if args["kill"]
        dictreq["operation"] = "kill"

    # Environment operation
    elseif !isempty(args["env-operation"])
        dictreq["operation"] = "envop"
        dictreq["query"] = "{\"cmd\":\"$(args["env-operation"][1])\","*
                            "\"cmd_argument\":\"$(args["env-operation"][2])\"}"
    else
        dictreq["operation"] = "search"
        dictreq["query"] = args["query"]
    end
    return JSON.json(dictreq)
end

# Deconstruct response is a pass through right now
deconstruct_json_response(response) = response

# Function that performs a simple search i.e. sends data to
# a socket, reads from the socket and outputs to STDOUT
function iosearch(connection, request, pretty=false)
    # Checks
    if isopen(connection)
        println(connection, request)
        @debug ">>> Request sent."
        response = readline(connection, keep=true)
        @debug "<<< Search results received."
    else
        @error "Connection is closed."
    end
    # Deconstruct response
    data = deconstruct_json_response(response)
    if pretty
        try
            jr = JSON.parse(data)  # builds a Dict
            # Print search results in a way similar to that
            # of the web client (`garw` web page)
            println("Elapsed search time: $(jr["elapsed_time"])s.")
            println(jr["n_searchers_w_results"], "/", jr["n_searchers"],
                    " search ensemble yielded ", jr["n_total_results"], " results.")
            for (id, result) in jr["results"]
                !isempty(result) && println("$id")
                for entry in result
                    printable = "["*string(entry["score"])*"] ~ "
                    for (k, v) in entry
                        k!="score" && (printable*= (k * ": " *string(v) * " "))
                    end
                    println(stdout, printable)
                end
            end
            # Print suggestions
            suggestions = jr["suggestions"]["d"]
            ns = length(suggestions)
            ns > 0 && println("$ns suggestions:")
            for (keyword, suggest) in suggestions
                print("  \"$keyword\": ")
                println("$(join(map(x->x[2], suggest), ", "))")
            end
        catch
            @warn "Pretty printing failed, dumping data as is..."
            println(stdout, data)
        end
    else
        println(stdout, data)
    end
    return nothing
end

########################
# Main module function #
########################
function julia_main()::Cint
    try
        real_main()
    catch
        Base.invokelatest(Base.display_error, Base.catch_stack())
        return 1
    end
    return 0
end

function real_main()
    # Parse command line arguments
    args = get_unix_socket_client_arguments(ARGS)

    # Logging
    log_levels = Dict("debug" => Logging.Debug,
                      "info" => Logging.Info,
                      "warning" => Logging.Warn,
                      "error" => Logging.Error)
    logger = ConsoleLogger(stdout,
                get(log_levels, lowercase(args["log-level"]), Logging.Info))
    global_logger(logger)

    # Start client
    @debug "~ GARAMOND~ (unix-socket client)"
    unixsocket = args["unix-socket"]
    if issocket(unixsocket)
        conn = connect(unixsocket)
        if isempty(args["query"]) && !args["kill"]
            @warn "Empty query, nothing to search. Exiting..."
        else
            # Construct Garamond request
            request = construct_json_request(args)
            # Search
            iosearch(conn, request, args["pretty"])
        end
        # Close connection and exit
        close(conn)
    else
        @warn "$unixsocket is not a proper UNIX socket. Exiting..."
    end
    return 0
end

##############
# Run client #
##############
if abspath(PROGRAM_FILE) == @__FILE__
    real_main()
end

end  # module


text:
using LinearAlgebra

""" y = Du
LinearFeedthrough """
struct LinearFeedthrough{F} <: Feedthrough
    input_dim::Int
    output_dim::Int
    paramsum::Int
    index_dict::Dict{String,UnitRange{Int}}
    initial_params::F
    function LinearFeedthrough(input_dim::Int, output_dim::Int; initW = Flux.glorot_uniform)
        # compute indices
        i = 1
        index_dict = Dict{String,UnitRange{Int}}()
        index_dict["D"] = i:i+output_dim*input_dim-1
        i += output_dim * input_dim

        # initialize initial parameters
        initial_params() = vec(initW(output_dim, input_dim))

        new{typeof(initial_params)}(
            input_dim,
            output_dim,
            i - 1,
            index_dict,
            initial_params,
        )
    end
end

function unpack_params(f::LinearFeedthrough, p)
    D = reshape(p[f.index_dict["D"]], f.output_dim, f.input_dim)
    return D
end

function (f::LinearFeedthrough)(ȳ, u, p)
    D = reshape(p[f.index_dict["D"]], f.output_dim, f.input_dim)
    Du = reshape(D * reshape(u, size(u, 1), :), :, size(u)[2:end]...)
    return ȳ .+ Du
end

""" y = [D 0]u where D is square diagonal
ResistiveFeedthrough """
struct ResistiveFeedthrough{F} <: Feedthrough
    input_dim::Int
    output_dim::Int
    paramsum::Int
    index_dict::Dict{String,UnitRange{Int}}
    initial_params::F
    function ResistiveFeedthrough(
        input_dim::Int,
        output_dim::Int;
        initW = Flux.glorot_uniform,
    )
        # compute indices
        i = 1
        index_dict = Dict{String,UnitRange{Int}}()
        index_dict["D"] = i:i+output_dim-1
        i += output_dim

        # initialize initial parameters
        initial_params() = vec(initW(output_dim))

        new{typeof(initial_params)}(
            input_dim,
            output_dim,
            i - 1,
            index_dict,
            initial_params,
        )
    end
end

function unpack_params(f::ResistiveFeedthrough, p)
    D = hcat(
        diagm(p[f.index_dict["D"]] .^ 2),
        zeros(f.output_dim, max(0, f.input_dim - f.output_dim)),
    )
    return D
end

function (f::ResistiveFeedthrough)(ȳ, u, p)
    D = p[f.index_dict["D"]] .^ 2
    ul = @view u[1:f.output_dim, :, :]
    Du = reshape(D .* reshape(ul, size(ul, 1), :), :, size(ul)[2:end]...)
    return ȳ .+ Du
end

max_stars_repo_name: syanga/ISSNeuralODE

max_issues_repo_name: syanga/ISSNeuralODE

max_forks_repo_name: syanga/ISSNeuralODE


text:
@testset "Retrieve" begin
    datadir = joinpath(@__DIR__,"data")

    @testset "ERA5 monthly preasure data" begin
        filepath = joinpath(datadir, "era5.grib")
        response = CDSAPI.retrieve("reanalysis-era5-pressure-levels-monthly-means",
            py2ju("""{
                'format': 'grib',
                'product_type': 'monthly_averaged_reanalysis',
                'variable': 'divergence',
                'pressure_level': '1',
                'year': '2020',
                'month': '06',
                'area': [
                    90, -180, -90,
                    180,
                ],
                'time': '00:00',
            }"""),
            filepath)

        @test typeof(response) <: Dict
        @test response["content_type"] == "application/x-grib"
        @test isfile(filepath)

        GribFile(filepath) do datafile
            data = Message(datafile)
            @test data["name"] == "Divergence"
            @test data["level"] == 1
            @test data["year"] == 2020
            @test data["month"] == 6
        end
        rm(filepath)
    end

    @testset "Europe water quantity data" begin
        filepath = joinpath(datadir, "ewq.zip")
        response = CDSAPI.retrieve("sis-water-quantity-swicca",
            py2ju("""{
                'variable': 'river_flow',
                'time_aggregation': 'annual_maximum',
                'horizontal_aggregation': 'catchments',
                'emissions_scenario': 'rcp_2_6',
                'period': '2071_2100',
                'return_period': '100',
                'format': 'zip',
            }"""),
            filepath)

        @test typeof(response) <: Dict
        @test response["content_type"] == "application/zip"
        @test isfile(filepath)

        # extract contents
        zip_reader = ZipFile.Reader(filepath)
        ewq_fileio = zip_reader.files[1]
        ewq_file = joinpath(datadir, ewq_fileio.name)
        write(ewq_file, read(ewq_fileio))
        close(zip_reader)

        # test file contents
        @test ncgetatt(ewq_file, "Global", "time_coverage_start") == "20710101"
        @test ncgetatt(ewq_file, "Global", "time_coverage_end") == "21001231"
        @test ncgetatt(ewq_file, "Global", "invar_experiment_name") == "rcp26"

        # cleanup
        rm(filepath)
        rm(ewq_file)
    end

    @testset "European energy sector cimate" begin
        filepath = joinpath(datadir, "ees.tar.gz")
        response = CDSAPI.retrieve("sis-european-energy-sector",
            py2ju("""{
                'variable': 'precipitation',
                'time_aggregation': '1_year_average',
                'vertical_level': '0_m',
                'bias_correction': 'bias_adjustment_based_on_gamma_distribution',
                'format': 'tgz',
            }"""),
            filepath)

        @test typeof(response) <: Dict
        @test response["content_type"] == "application/gzip"
        @test isfile(filepath)

        # extract contents
        gzip_io = GZip.open(filepath)
        eesfile_dir = Tar.extract(gzip_io, joinpath(datadir, "ees"))
        ees_file = joinpath(eesfile_dir, readdir(eesfile_dir)[1])
        close(gzip_io)

        # test file contents
        @test ncgetatt(ees_file, "Global", "frequency") == "year"
        @test ncgetatt(ees_file, "tp", "long_name") == "precip total"

        # cleanup
        rm(filepath)
        rm(ees_file)
        rm(eesfile_dir)
    end
end


text:
# __precompile__()

module LowRankModels

using Printf
using SharedArrays
using SparseArrays
using Random
using Statistics
using DataFrames

import LinearAlgebra: dot, norm, Diagonal, rmul!, mul!
import Base: show
import StatsBase: fit!, mode, mean, var, std

# define losses, regularizers, convergence history
include("domains.jl")
include("losses.jl")
include("impute_and_err.jl")
include("regularizers.jl")
include("convergence.jl")

# define basic data type(s)
include("glrm.jl")
include("shareglrm.jl")

# modify models (eg scaling and offsets) and evaluate fit
include("modify_glrm.jl")
include("evaluate_fit.jl")

# fitting algorithms
include("fit.jl")
if Threads.nthreads() > 1
  include("algorithms/proxgrad_multithread.jl")
else
  include("algorithms/proxgrad.jl")
end
include("algorithms/sparse_proxgrad.jl")
include("algorithms/quad_streaming.jl")

# initialization methods
include("rsvd.jl")
include("initialize.jl")

# fancy fun on top of low rank models
include("simple_glrms.jl")
include("cross_validate.jl")
include("fit_dataframe.jl")
include("sample.jl")
# this takes to long to load for normal use
# include("plot.jl")

# utilities
include("utilities/conveniencemethods.jl")
include("utilities/deprecated.jl")

# ScikitLearn.jl compatibility
include("scikitlearn.jl")

end # module