text:
###
  Name     : c7_17_optim_example.jl
  Book     : Hands-on Data Science with Anaconda )
  Publisher: Packt Publishing Ltd.
  Author   : Yuxing Yan and James Yan
  Date     : 3/15/2018
  email    : yany@canisius.edu
             paulyxy@hotmail.com
###

using Optim
f(x) = (1.0 - x[1])^2 + 200.0 * (x[2] - x[1]^2)^2
optimize(f, [0.0, 0.0])


text:
const default_registries = Pkg.RegistrySpec[Pkg.RegistrySpec(name = "General",
                                                             uuid = "23338594-aafe-5451-b93e-139f81909106",
                                                             url = "https://github.com/JuliaRegistries/General.git")]

function main(env::AbstractDict = ENV,
              ci_cfg::CIService = auto_detect_ci_service(; env = env);
              registries::Vector{Pkg.RegistrySpec} = default_registries,
              keep_existing_compat::Bool = true,
              drop_existing_compat::Bool = false,
              bump_compat_containing_equality_specifier = true,
              master_branch::Union{DefaultBranch, AbstractString} = DefaultBranch(),
              pr_title_prefix::String = "",
              subdirs::AbstractVector{<:AbstractString} = [""],
              hostname_for_api::String = "https://api.github.com",
              hostname_for_clone::String = "github.com",
              use_pkg_server::Bool = true,
              include_jll::Bool = false)
    if !keep_existing_compat && !drop_existing_compat
        throw(ArgumentError("At least one of keep_existing_compat, drop_existing_compat must be true"))
    end

    if compathelper_priv_is_defined(env)
        let _ = nothing
            _decode_ssh_private_key(env["COMPATHELPER_PRIV"])
            nothing
        end
        # we found the SSH deploy key
        @info("CompatHelper found your SSH deploy key in the `COMPATHELPER_PRIV` environment variable.")
    else
        # we did not find the SSH deploy key
        @info("CompatHelper did not find a valid SSH deploy key in the `COMPATHELPER_PRIV` environment variable.")
    end

    api = GitHub.GitHubWebAPI(HTTP.URI(hostname_for_api))
    clone_hostname = HostnameForClones(hostname_for_clone)
    GITHUB_TOKEN = github_token(ci_cfg; env = env)
    GITHUB_REPOSITORY = github_repository(ci_cfg; env = env)
    auth = my_retry(() -> GitHub.authenticate(api, GITHUB_TOKEN))
    repo = my_retry(() -> GitHub.repo(api, GITHUB_REPOSITORY; auth = auth))

    _all_open_prs = get_all_pull_requests(api,
                                          repo,
                                          "open";
                                          auth = auth)
    _nonforked_prs = exclude_pull_requests_from_forks(repo, _all_open_prs)
    my_username = get_my_username(ci_cfg; auth = auth, env = env)
    pr_list = only_my_pull_requests(_nonforked_prs; my_username = my_username)
    pr_titles = Vector{String}(undef, length(pr_list))
    for i = 1:length(pr_list)
        pr_titles[i] = convert(String, strip(pr_list[i].title))::String
    end

    for subdir in subdirs
        dep_to_current_compat_entry,
            dep_to_current_compat_entry_verbatim,
            dep_to_latest_version,
            deps_with_missing_compat_entry = get_project_deps(api,
                                                              clone_hostname,
                                                              repo;
                                                              auth,
                                                              master_branch,
                                                              subdir,
                                                              include_jll)
        get_latest_version_from_registries!(dep_to_latest_version,
                                            registries,
                                            use_pkg_server = use_pkg_server)

        make_pr_for_new_version(api,
                                clone_hostname,
                                repo,
                                dep_to_current_compat_entry,
                                dep_to_current_compat_entry_verbatim,
                                dep_to_latest_version,
                                deps_with_missing_compat_entry,
                                pr_list,
                                pr_titles,
                                ci_cfg;
                                auth = auth,
                                env = env,
                                keep_existing_compat = keep_existing_compat,
                                drop_existing_compat = drop_existing_compat,
                                bump_compat_containing_equality_specifier = bump_compat_containing_equality_specifier,
                                master_branch = master_branch,
                                subdir = subdir,
                                pr_title_prefix = pr_title_prefix,
                                registries = registries)
    end
    return nothing
end

ext: jl


text:
"""
    delete_chunk!(schunk::SChunk, nchunk)

Delete a chunk at a `nchunk` position (1-indexed) from a super-chunk.

Return the number of chunks in super-chunk. If some problem is detected, this number will be negative.
"""
function delete_chunk!(schunk::SChunk, nchunk)
    @boundscheck checkchunksbounds(schunk, nchunk)
    return Lib.blosc2_schunk_delete_chunk(schunk.ptr, nchunk - 1)
end


text:
## runtests.jl
## (c) 2013--2014 David A. van Leeuwen

## Julia type that implements a drop-in replacement of Array with named dimensions.

## This code is licensed under the MIT license
## See the file LICENSE.md in this distribution

using NamedArrays
using Test
using DataStructures
using Combinatorics
using LinearAlgebra
using SparseArrays
using DelimitedFiles
using Statistics
using Random

using KahanSummation

include("test.jl")


text:
add_node!{T}(net::ForwardNet{T}, node::Node{T}, parents::Vector{Int}=Int[]) = push!(net, node, parents)
add_node!{T}(net::ForwardNet{T}, node::Node{T}, parent::Int) = push!(net, node, Int[parent])


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

# install packages
info("Install Packages...")
Pkg.init()
cp(joinpath(@__DIR__, "REQUIRE"), Pkg.dir("REQUIRE"); remove_destination = true)
Pkg.update()
Pkg.resolve()
info("Packages done.")

# include package
info("Include all...")
try
  include("../source/0.6/src/App.jl")
catch e # do not exit this run!
  warn(e)
end
info("Include done.")

info("Create Docs...")
using Documenter, App

makedocs(
  build     = joinpath(@__DIR__, "../docs"),
  modules   = [App],
  clean     = true,
  doctest   = true, # :fix
  #linkcheck = true,
  strict    = false,
  checkdocs = :none,
  format    = :html, #:latex
  sitename  = "JGE",
  authors   = "Gilga",
  html_prettyurls = true,
  #html_canonical = "https://gilga.github.io/JGE/",
)

deploydocs(
  deps   = Deps.pip("mkdocs", "python-markdown-math"), #, "curl"
  repo = "https://github.com/Gilga/JGE.git",
  branch = "gh-pages",
  julia  = "0.6.2",
)

info("Docs done.")

max_stars_repo_name: Gilga/JGE

max_issues_repo_name: Gilga/JGE

max_forks_repo_name: Gilga/JGE


text:
# -------------------------------------------------------------------
# JULIA MODULE FOR EXPORTING INTERACTION MATRICES AS C++-HEADER FILES
# AS USED IN KITAEV_QMC AND KITAEV_QMC_KPM
# Author: Tim Eschmann
# Date: 2017-01-26
# Modified for new version of LatticePhysics: 2019-01-31
#
# This module requires the Julia package "LatticePhysics.jl"
# (Copyright (c) 2017: Jan Attig) which is available under
# https://github.com/janattig/LatticePhysics.jl
# -------------------------------------------------------------------

module MatrixCreator

using JLD
using LatticePhysics

#####################################################################
# Generate Hamiltonian for all-bond sampling in QMC
function writehpp(lat, plq, plaq_length, matrix, filename)
	N1 = size(matrix,1)
	N2 = size(matrix,2)
	epsil = 0.0001

	file = open(filename, "w")

	# write the headerstring
	write(file, getHeaderString())
	write(file, declare_def_matrix(matrix))

	for j1 in (1:N1)
		for j2 in (j1:N2)
			if abs(matrix[j1,j2]) > epsil
				#if (lat.positions_indices[j1] == 2.0)
				#	write(file, giveMinusEntries(matrix, j1, j2))
				#else
				write(file, giveEntries(matrix, j1, j2))
				#end
			end
		end
	end

	write(file, endofDec())
	write(file, declare_coord())

	for j1 in (1:N1)
	    for j2 in (j1:N2)
			if matrix[j1,j2] < 0
			   write(file, giveCoordinates(N1, j1, j2))
		    end
		end
	end

	write(file, endofCooDec())

	write(file, declare_plaquettes(length(plq), plaq_length))

	for j1 in (1:length(plq))
		for j2 in (1:plaq_length)
			write(file, givePlaquetteCoords(N1, plq, j1, j2, Int(plq[j1][j2]), Int(plq[j1][j2+1])))
		end
	end

	write(file, endof_plaquettes())

	close(file)
end

#####################################################################
# Header string for .hpp-file:
function getHeaderString()
	#include <iostream>
	headerstring = """
	#include <armadillo>
	#include <complex>

	using namespace arma;

	"""

	return headerstring
end

# Declaration for matrix generating function:
function declare_def_matrix(matrix)
	N1 = size(matrix,1)
	N2 = size(matrix,2)
	str = """cx_mat def_matrix()
	{
	   cx_mat A($(N1),$(N2), fill::zeros);
	"""
	return str
end

# Fill matrix A with entries:
function giveEntries(matrix, i1, i2)
	str = """
             A($(i1-1), $(i2-1)) = std::complex<double>(0.0, 2.0*$(matrix[i1, i2]));
             A($(i2-1), $(i1-1)) = -std::complex<double>(0.0, 2.0*$(matrix[i1, i2]));
          """
	return str;
end

function giveMinusEntries(matrix, i1, i2)
	str = """
             A($(i1-1), $(i2-1)) = -std::complex<double>(0.0, 2.0*$(matrix[i1, i2]));
             A($(i2-1), $(i1-1)) = std::complex<double>(0.0, 2.0*$(matrix[i1, i2]));
          """
	return str;
end

# Close first function:
function endofDec()
	str = """
             return A;
          }

          """
end

# Second function: Create vector with matrix coordinates != 0:
function declare_coord()
	str = """std::vector<int> non_zeros()
{
    std::vector<int> nz;
    int coo;
"""
end

# Fill vector:
function giveCoordinates(N, i1, i2)
	str = """
              coo = $(N)*$(i1-1) + $(i2-1);
              nz.push_back(coo);
          """

end

# Close second function:
function endofCooDec()
	str = """
              return nz;
          }

          """
end

# Third function: Create matrix with coordinates of elementary plaquettes:

# Declaration for matrix generating function:
function declare_plaquettes(size1, size2)

	str = """Mat<int> create_plaquettes()
	{
	   Mat<int> P($(size1),$(size2));
	"""
	return str

end

# Fill matrix with encoded coordinates:
function givePlaquetteCoords(N, plq, j1, j2, z1, z2)
	str = """
             P($(j1-1), $(j2-1)) = $(N)*$(z1-1)+$(z2-1);
          """
	return str
end

# Close third function:
function endof_plaquettes()
	str = """
			 return P;
		  }
		  """
end

#####################################################################

export writehpp
export writehpp_zz
export getHeaderString

end

max_stars_repo_name: timeschmann/LatticeFilesForKitaevQMC

max_issues_repo_name: timeschmann/LatticeFilesForKitaevQMC

max_forks_repo_name: timeschmann/LatticeFilesForKitaevQMC


text:
# import DataFrames.DataFrames
using Tables
using SparseArrays
using Statistics
using ArgCheck

abstract type AbstractDataset{T<:Number} end

struct Dataset{T<:Number} <: AbstractDataset{T}
    ratings::SparseMatrixCSC{AbstractRating{T}, Int}
    preference::Preference{T}
    users::Int
    items::Int
end

sample(dataset::Dataset, index::AbstractArray) = Persa.Dataset(dataset[index], dataset.users, dataset.items, dataset.preference)

Base.string(x::Dataset) = string("""Collaborative Filtering Dataset
                                    - # users: $(users(x))
                                    - # items: $(items(x))
                                    - # ratings: $(length(x))
                                    - Ratings Preference: $(x.preference)
                                    """)
Base.print(io::IO, x::Dataset) = print(io, string(x))
Base.show(io::IO, x::Dataset) = print(io, x)

struct UserPreference{T}
    user::Int
    item::Int
    rating::AbstractRating{T}
end

Base.string(x::UserPreference) = string("(user: ", user(x), ", item: ", item(x), ", rating: ", rating(x), ")")
Base.print(io::IO, x::UserPreference) = print(io, string(x))
Base.show(io::IO, x::UserPreference) = print(io, x)

user(x::UserPreference) = x.user
item(x::UserPreference) = x.item
rating(x::UserPreference) = x.rating

Base.iterate(p::UserPreference, state=1) = length(fieldnames(typeof(p))) < state ? nothing : (p[state], state+1)

Base.getindex(p::UserPreference{T}, i::Int) where T = length(fieldnames(typeof(p))) < i ? nothing : getfield(p, i)

function Dataset(table)
    Tables.istable(table) || throw(ArgumentError("The df argument passed does not implement the Tables.jl interface."))
    Tables.isrowtable(table) ? (df = Tables.columntable(table)) : (df = table)

    columns = Tables.columnnames(df)
    @argcheck in(:user, columns)
    @argcheck in(:item, columns)
    @argcheck in(:rating, columns)

    user_col = Tables.getcolumn(df, :user)
    item_col = Tables.getcolumn(df, :item)
    rating_col = Tables.getcolumn(df, :rating)

    users = maximum(user_col)
    items = maximum(item_col)
    pref = Preference(rating_col)

    ratings = convert(rating_col, pref)
    matrix = sparse(user_col, item_col, ratings, users, items, rerating)

    return Dataset(matrix, pref, users, items)
end

function Dataset(table, users::Int, items::Int)
    Tables.istable(table) || throw(ArgumentError("The df argument passed does not implement the Tables.jl interface."))
    Tables.isrowtable(table) ? (df = Tables.columntable(table)) : (df = table)

    columns = Tables.columnnames(df)
    @argcheck in(:user, columns)
    @argcheck in(:item, columns)
    @argcheck in(:rating, columns)

    user_col = Tables.getcolumn(df, :user)
    item_col = Tables.getcolumn(df, :item)
    rating_col = Tables.getcolumn(df, :rating)

    @argcheck users >= maximum(user_col)
    @argcheck items >= maximum(item_col)

    pref = Preference(rating_col)

    ratings = convert(rating_col, pref)
    matrix = sparse(user_col, item_col, ratings, users, items, rerating)

    return Dataset(matrix, pref, users, items)
end

function Dataset(userprefs::Vector{UserPreference{T}}, users::Int, items::Int, preference::Preference) where T
    userslist = Vector{Int}(undef, length(userprefs))
    itemslist = Vector{Int}(undef, length(userprefs))
    ratingslist = Vector{AbstractRating{T}}(undef, length(userprefs))

    for i = 1:length(userprefs)
        u, v, r = userprefs[i]
        userslist[i] = u
        itemslist[i] = v
        ratingslist[i] = r
    end

    matrix = sparse(userslist, itemslist, ratingslist, users, items)

    return Dataset(matrix, preference, users, items)
end

users(dataset::AbstractDataset) = dataset.users
items(dataset::AbstractDataset) = dataset.items
preference(dataset::AbstractDataset) = dataset.preference

Base.size(dataset::AbstractDataset) = (users(dataset), items(dataset))
Base.length(dataset::AbstractDataset) = length(nonzeros(dataset.ratings))

function Base.Array(dataset::Dataset{T})::Matrix{Union{Missing, T}} where T <: Number
    matrix = Array{Union{Missing, T}}(missing, Persa.users(dataset), Persa.items(dataset))

    users = rowvals(dataset.ratings)
    ratings = nonzeros(dataset.ratings)
    for item = 1:items(dataset)
        for j in nzrange(dataset.ratings, item)
            user = users[j]
            rating = ratings[j]
            matrix[user, item] = value(rating)
        end
    end

    return matrix
end

function Base.getindex(dataset::AbstractDataset, user::Int, item::Int)::AbstractRating
    @argcheck user >= 0 && user <= users(dataset)
    @argcheck item >= 0 && item <= items(dataset)

    dataset.ratings[user, item]
end

function Base.getindex(dataset::AbstractDataset{T}, i::Int)::UserPreference{T} where T
    if i > length(dataset)
        throw(ArgumentError("index must satisfy 1 <= i <= length(dataset)"))
    end

    users = rowvals(dataset.ratings)
    ratings = nonzeros(dataset.ratings)

    for item = 1:items(dataset)
        if i in nzrange(dataset.ratings, item)
            user = users[i]
            rating = ratings[i]

            return UserPreference{T}(user, item, rating)
        end
    end
end

function Base.getindex(dataset::AbstractDataset{T}, user::Int, c::Colon) where T
    elements = Persa.UserPreference{T}[]

    for i=1:items(dataset)
        value = dataset[user, i]
        if !isnan(value)
            push!(elements, UserPreference(user, i, value))
        end
    end

    return elements
end

function Base.getindex(dataset::AbstractDataset{T}, user::Int, index::UnitRange{Int}) where T
    elements = Persa.UserPreference{T}[]

    for i=1:length(index)
        value = dataset[user, index[i]]
        if !isnan(value)
            push!(elements, UserPreference(user, index[i], value))
        end
    end

    return elements
end

function Base.getindex(dataset::AbstractDataset{T}, c::Colon, item::Int) where T
    elements = Persa.UserPreference{T}[]

    for i=1:users(dataset)
        value = dataset[i, item]
        if !isnan(value)
            push!(elements, UserPreference(i, item, value))
        end
    end

    return elements
end

function Base.getindex(dataset::AbstractDataset{T}, index::UnitRange{Int}, item::Int) where T
    elements = Persa.UserPreference{T}[]

    for i=1:length(index)
        value = dataset[index[i], item]
        if !isnan(value)
            push!(elements, UserPreference(index[i], item, value))
        end
    end

    return elements
end

function Base.getindex(dataset::AbstractDataset{T}, index::Vector{Int}) where T
    elements = Vector{UserPreference{T}}(undef, length(index))

    for i = 1:length(index)
        elements[i] = dataset[index[i]]
    end

    return elements
end

function Base.getindex(dataset::AbstractDataset{T}, index::UnitRange{Int}) where T
    elements = Vector{UserPreference{T}}(undef, length(index))
    j = 1

    for i in index
        elements[j] = dataset[i]
        j += 1
    end

    return elements
end

function Base.getindex(dataset::AbstractDataset{T}, users::UnitRange{Int}, items::UnitRange{Int}) where T
    elements = UserPreference{T}[]

    for user in users, item in items
        value = dataset[user, item]
        if !isnan(value)
            push!(elements, UserPreference(user, item, value))
        end
    end

    return elements
end

Base.getindex(dataset::AbstractDataset, c1::Colon, c2::Colon) = dataset[1:end, 1:end]

Base.getindex(dataset::AbstractDataset, c::Colon) = dataset[1:length(dataset)]

Base.iterate(dataset::AbstractDataset, state = 1) = state > length(dataset) ? nothing : (dataset[state], state+1)

Base.lastindex(dataset::AbstractDataset, d::Int) = d == 1 ? users(dataset) : items(dataset)

function Statistics.mean(dataset::AbstractDataset)
    μ = 0
    total = 0
    for (u, v, r) in dataset
        if !isnan(r)
            μ += r
            total += 1
        end
    end
    return μ ./ total
end


text:
# implementation of the n-dimensional cubature rule (n ≥ 2) from
# A. C. Genz and A. A. Malik, "An adaptive algorithm for numeric
# integration over an N-dimensional rectangular region," J. Comput. Appl. Math.,
# vol. 6 (no. 4), 295-302 (1980).

"""
    combos(k, λ, Val{n}())

Return an array of SVector{n} of all n-component vectors
with k components equal to λ and other components equal to zero.
"""
function combos(k::Integer, λ::T, ::Val{n}) where {n, T<:Number}
    combos = Combinatorics.combinations(1:n, k)
    p = Vector{SVector{n,T}}(undef, length(combos))
    v = MVector{n,T}(undef)
    for (i,c) in enumerate(combos)
        v .= 0
        v[c] .= λ
        p[i] = v
    end
    return p
end

"""
    signcombos(k, λ, Val{n}())

Return an array of SVector{n} of all n-component vectors
with k components equal to ±λ and other components equal to zero
(with all possible signs).
"""
function signcombos(k::Integer, λ::T, ::Val{n}) where {n, T<:Number}
    combos = Combinatorics.combinations(1:n, k)
    twoᵏ = 1 << k
    p = Vector{SVector{n,T}}(undef, length(combos) * twoᵏ)
    v = MVector{n,T}(undef)
    for (i,c) in enumerate(combos)
        j = (i-1)*twoᵏ + 1
        v .= 0
        v[c] .= λ
        p[j] = v
        # use a gray code to flip one sign at a time
        graycode = 0
        for s = 1:twoᵏ-1
            graycode′ = s ⊻ (s >> 1)
            graycomp = c[trailing_zeros(graycode ⊻ graycode′) + 1]
            graycode = graycode′
            v[graycomp] = -v[graycomp]
            p[j+s] = v
        end
    end
    return p
end

"""
`GenzMalik{n,T}` holds the points and weights corresponding
to an `n`-dimensional Genz-Malik cubature rule over coordinates
of type `T`.
"""
struct GenzMalik{n,T<:AbstractFloat}
    p::NTuple{4,Vector{SVector{n,T}}} # points for the last 4 G-M weights
    w::NTuple{5,T}  # weights for the 5 terms in the G-M rule
    w′::NTuple{4,T} # weights for the embedded lower-degree rule
end

# cache the Genz-Malik rules so that we don't reconstruct them every time;
# this mainly matters for simple integrands (low-degree polynomials) that
# don't require refinement.
const gmcache = Dict{Tuple{Int,Type}, GenzMalik}()

"""
    GenzMalik(Val{n}(), T=Float64)

Construct an n-dimensional Genz-Malik rule for coordinates of type `T`.
"""
function GenzMalik(v::Val{n}, ::Type{T}=Float64) where {n, T<:AbstractFloat}
    haskey(gmcache, (n,T)) && return gmcache[n,T]::GenzMalik{n,T}

    n < 2 && throw(ArgumentError("invalid dimension $n: GenzMalik rule requires dimension > 2"))

    λ₄ = sqrt(9/T(10))
    λ₂ = sqrt(9/T(70))
    λ₃ = λ₄
    λ₅ = sqrt(9/T(19))

    twoⁿ = 1 << n
    w₁ = twoⁿ * ((12824 - 9120n + 400n^2) / T(19683))
    w₂ = twoⁿ * (980 / T(6561))
    w₃ = twoⁿ * ((1820 - 400n) / T(19683))
    w₄ = twoⁿ * (200 / T(19683))
    w₅ = 6859/T(19683)
    w₄′ = twoⁿ * (25/T(729))
    w₃′ = twoⁿ * ((265 - 100n)/T(1458))
    w₂′ = twoⁿ * (245/T(486))
    w₁′ = twoⁿ * ((729 - 950n + 50n^2)/T(729))

    p₂ = combos(1, λ₂, v)
    p₃ = combos(1, λ₃, v)
    p₄ = signcombos(2, λ₄, v)
    p₅ = signcombos(n, λ₅, v)

    g = GenzMalik{n,T}((p₂,p₃,p₄,p₅), (w₁,w₂,w₃,w₄,w₅), (w₁′,w₂′,w₃′,w₄′))
    gmcache[n,T] = g
    return g
end

countevals(g::GenzMalik{n}) where {n} = 1 + 4n + 2*n*(n-1) + (1<<n)

"""
    genzmalik(f, a, b, norm=vecnorm)

Evaluate `genzmalik::GenzMalik` for the box with min/max corners `a` and `b`
for an integrand `f`.  Returns the estimated integral `I`, the estimated
error `E` (via the given `norm`), and the suggested coordinate `k` ∈ `1:n`
to subdivide next.
"""
function (g::GenzMalik{n,T})(f, a::SVector{n}, b::SVector{n}, norm=vecnorm) where {n,T}
    c = T(0.5).*(a.+b)
    Δ = T(0.5).*abs.(b.-a)
    V = prod(Δ)

    f₁ = f(c)

    f₂ = zero(f₁)
    f₃ = zero(f₁)
    twelvef₁ = 12f₁
    maxdivdiff = zero(norm(f₁))
    divdiff = MVector{n,typeof(maxdivdiff)}(undef)
    for i = 1:n
        p₂ = Δ .* g.p[1][i]
        f₂ᵢ = f(c + p₂) + f(c - p₂)
        p₃ = Δ .* g.p[2][i]
        f₃ᵢ = f(c + p₃) + f(c - p₃)
        f₂ += f₂ᵢ
        f₃ += f₃ᵢ
        # fourth divided difference: f₃ᵢ-2f₁ - 7*(f₂ᵢ-2f₁),
        # where 7 = (λ₃/λ₂)^2 [see van Dooren and de Ridder]
        divdiff[i] = norm(f₃ᵢ + twelvef₁ - 7*f₂ᵢ)
    end

    f₄ = zero(f₁)
    for p in g.p[3]
        f₄ += f(c .+ Δ .* p)
    end

    f₅ = zero(f₁)
    for p in g.p[4]
        f₅ += f(c .+ Δ .* p)
    end

    I = V * (g.w[1]*f₁ + g.w[2]*f₂ + g.w[3]*f₃ + g.w[4]*f₄ + g.w[5]*f₅)
    I′ = V * (g.w′[1]*f₁ + g.w′[2]*f₂ + g.w′[3]*f₃ + g.w′[4]*f₄)
    E = norm(I - I′)

    # choose axis
    kdivide = 1
    δf = E / (10^n * V)
    for i = 1:n
        if (δ = divdiff[i] - maxdivdiff) > δf
            kdivide = i
            maxdivdiff = divdiff[i]
        elseif abs(δ) < δf && Δ[i] > Δ[kdivide]
            kdivide = i
        end
    end

    return I, E, kdivide
end


text:
# using Test, PhyloNetworks
# using PhyloPlots
# using Debugger

@testset "tree component" begin

    treestr = "(A:3.0,(B:2.0,(C:1.0,D:1.0):1.0):1.0);"
    tree = readTopology(treestr)
    for e in tree.edge e.containRoot=false; end # wrong, on purpose
    @test collect(values(treeedgecomponents(tree))) == repeat([1], inner=7)
    rcomp = checkroot!(tree)
    @test rcomp == 1
    @test all([e.containRoot for e = tree.edge])

    netstr = "(#H1:::0.1,#H2:::0.2,(((b)#H1)#H2,a));"
    net = readTopology(netstr)
    for e in net.edge e.containRoot=false; end # wrong, on purpose
    node2comp = treeedgecomponents(net) # e.g. [1,1,1,2,2,3] or [2,2,2,1,1,3]
    compsize = [count(isequal(i), values(node2comp)) for i in 1:3]
    @test sort(compsize) == [1,2,3]
    rcompID = checkroot!(net, node2comp)
    @test compsize[rcompID] == 3
    # plot(net, :R, showNodeNumber=true, showEdgeNumber=true);
    rcomp = keys(filter(p -> p.second == rcompID, node2comp))
    @test Set(n.number for n in rcomp) == Set([-2 -3 4])
    @test Set(e.number for e in net.edge if e.containRoot) == Set([7 6 5 2 1])
    for e in net.edge e.containRoot=true; end # wrong, on purpose
    net.root = 2 # node number 1, also H1: not in the root component on purpose
    checkroot!(net)
    @test [e.number for e in net.edge if e.containRoot] == [1,2,5,6,7]
    @test net.root == 5 # i.e. node number -3. only 1 other option: 6, i.e. nn -2

    # test semidirected cycle case
    net.edge[1].isChild1 = !net.edge[1].isChild1
    net.edge[7].isChild1 = !net.edge[7].isChild1
    net.edge[7].hybrid = true
    net.edge[7].gamma  = net.edge[4].gamma
    net.edge[4].gamma  = 1.0
    net.edge[4].hybrid = false
    net.root = 5
    net.node[6].hybrid = true
    net.node[6].name = "H3"
    net.node[2].hybrid = false
    net.hybrid[1] = net.node[6]
    mem = treeedgecomponents(net)
    nnum2comp = Dict(n.number => uc for (n,uc) in mem)
    @test nnum2comp[4] == nnum2comp[-3]
    @test nnum2comp[1] == nnum2comp[2] == nnum2comp[3]
    @test length(unique(nnum2comp[nn] for nn in [4,1,-2])) == 3
    @test_throws PhyloNetworks.RootMismatch checkroot!(net, mem)
    try checkroot!(net, mem)
    catch e
        @test occursin("Semidirected cycle", sprint(showerror, e))
    end

    # test multiple entry points case
    str_level1 = "(((S8,S9),((((S1,S4),(S5)#H1),(#H1,(S6,S7))))#H2),(#H2,S10));"
    netl1 = readTopology(str_level1)
    P = PhyloNetworks # binding P: local to test set
    root = netl1.node[19] # 19 = findfirst(n -> n.number == -2, netl1.node)
    e1 = netl1.edge[20]   # 20 = findfirst(e -> e.number == 20, netl1.edge)
    e2 = netl1.edge[17]   # 17 = findfirst(e -> e.number == 17, netl1.edge)
    P.deleteEdge!(netl1, e1)
    P.deleteEdge!(netl1, e2)
    P.deleteNode!(netl1, root)
    P.removeEdge!(netl1.node[18], e1) # 18 = P.getIndexNode(-12, netl1)
    P.removeEdge!(netl1.node[16], e2) # 16 = P.getIndexNode(-3, netl1)
    # nodenumber2UC = Dict(n.number => uc for (n,uc) in treeedgecomponents(netl1))
    mem = treeedgecomponents(netl1)
    @test_throws PhyloNetworks.RootMismatch checkroot!(netl1, mem)
    try checkroot!(netl1, mem)
    catch e
        @test occursin("no common ancestor", sprint(showerror, e))
    end

    # test undirected cycle case
    netl1 = readTopology(str_level1)
    n1 = netl1.node[14] # 14 = P.getIndexNode(-6, netl1)
    n2 = netl1.node[6]  #  6 = P.getIndexNode(-8, netl1)
    e = P.Edge(21,1.0,false,1.0) # 21 = length(netl1.edge) + 1
    P.setNode!(e, n1)
    P.setNode!(e, n2)
    push!(n1.edge, e)
    push!(n2.edge, e)
    push!(net.edge, e)
    @test_throws PhyloNetworks.RootMismatch treeedgecomponents(netl1)
    try treeedgecomponents(netl1)
    catch e
        @test occursin("Undirected cycle", sprint(showerror, e))
    end

end


text:
"""
Sparse pole representation
"""
struct SparsePoleRepresentation <: Basis
    o::PyObject
    u::PiecewiseLegendrePoly
    uhat::PiecewiseLegendreFT
    statistics::Statistics
    size::Int64
end

Base.size(spr::SparsePoleRepresentation)::Int64 = spr.o.size
statistics(spr::SparsePoleRepresentation)::Statistics = spr.o.statistics

function SparsePoleRepresentation(o::PyObject)
    return SparsePoleRepresentation(o, o.u, o.uhat,
                                    o.statistics == "F" ? fermion : boson,
                                    o.size)
end

function SparsePoleRepresentation(basis::FiniteTempBasis,
                                  sampling_points::Vector{Float64}=default_omega_sampling_points(basis))
    return SparsePoleRepresentation(sparse_ir.spr.SparsePoleRepresentation(basis.o,
                                                                           sampling_points))
end

"""
From IR to SPR

gl:
    Expansion coefficients in IR
"""
function from_IR(spr::SparsePoleRepresentation,
                 gl::Array{T,N}, axis::Int64=1)::Array{ComplexF64,N} where {T,N}
    return spr.o.from_IR(gl, axis - 1)
end

"""
From SPR to IR

g_spr:
    Expansion coefficients in SPR
"""
function to_IR(spr::SparsePoleRepresentation,
               g_spr::Array{T,N}, axis::Int64=1)::Array{ComplexF64,N} where {T,N}
    return spr.o.to_IR(g_spr, axis - 1)
end


text:
#Declaration of GLOBALS

const global B_INCR = 0.293
const global B_DECR = -0.293
const global C_INCR = 0.733
const global N_SCALAR = -0.74
const global NORMALIZATION_ALPHA = 15.0

NEGATION_TOKENS = ["aint", "arent", "cannot", "cant", "couldnt", "darent", "didnt", "doesnt",
                   "ain't", "aren't", "can't", "couldn't", "daren't", "didn't", "doesn't",
                   "dont", "hadnt", "hasnt", "havent", "isnt", "mightnt", "mustnt", "neither",
                   "don't", "hadn't", "hasn't", "haven't", "isn't", "mightn't", "mustn't",
                   "neednt", "needn't", "never", "none", "nope", "nor", "not", "nothing", "nowhere",
                   "oughtnt", "shant", "shouldnt", "uhuh", "wasnt", "werent",
                   "oughtn't", "shan't", "shouldn't", "uh-uh", "wasn't", "weren't",
                   "without", "wont", "wouldnt", "won't", "wouldn't", "rarely", "seldom", "despite"]

BOOSTER_DICT = Dict("absolutely"=> B_INCR, "amazingly"=> B_INCR, "awfully"=> B_INCR,
                    "completely"=> B_INCR, "considerable"=> B_INCR, "considerably"=> B_INCR,
                    "decidedly"=> B_INCR, "deeply"=> B_INCR, "effing"=> B_INCR, "enormous"=> B_INCR, "enormously"=> B_INCR,
                    "entirely"=> B_INCR, "especially"=> B_INCR, "exceptional"=> B_INCR, "exceptionally"=> B_INCR,
                    "extreme"=> B_INCR, "extremely"=> B_INCR,"fabulously"=> B_INCR, "flipping"=> B_INCR, "flippin"=> B_INCR,
                    "frackin"=> B_INCR, "fracking"=> B_INCR, "fricking"=> B_INCR, "frickin"=> B_INCR, "frigging"=> B_INCR,
                    "friggin"=> B_INCR, "fully"=> B_INCR, "fuckin"=> B_INCR, "fucking"=> B_INCR, "fuggin"=> B_INCR, "fugging"=> B_INCR,
                    "greatly"=> B_INCR, "hella"=> B_INCR, "highly"=> B_INCR, "hugely"=> B_INCR, "incredible"=> B_INCR,
                    "incredibly"=> B_INCR, "intensely"=> B_INCR, "major"=> B_INCR, "majorly"=> B_INCR, "more"=> B_INCR, "most"=> B_INCR,
                    "particularly"=> B_INCR, "purely"=> B_INCR, "quite"=> B_INCR, "really"=> B_INCR, "remarkably"=> B_INCR,
                    "so"=> B_INCR, "substantially"=> B_INCR, "thoroughly"=> B_INCR, "total"=> B_INCR, "totally"=> B_INCR,
                    "tremendous"=> B_INCR, "tremendously"=> B_INCR, "uber"=> B_INCR, "unbelievably"=> B_INCR,
                    "unusually"=> B_INCR, "utter"=> B_INCR, "utterly"=> B_INCR, "very"=> B_INCR, "almost"=> B_DECR,
                    "barely"=> B_DECR, "hardly"=> B_DECR, "just enough"=> B_DECR, "kind of"=> B_DECR, "kinda"=> B_DECR,
                    "kindof"=> B_DECR, "kind-of"=> B_DECR, "less"=> B_DECR, "little"=> B_DECR, "marginal"=> B_DECR,
                    "marginally"=> B_DECR, "occasional"=> B_DECR, "occasionally"=> B_DECR, "partly"=> B_DECR,
                    "scarce"=> B_DECR, "scarcely"=> B_DECR, "slight"=> B_DECR, "slightly"=> B_DECR, "somewhat"=> B_DECR,
                    "sort of"=> B_DECR, "sorta"=> B_DECR, "sortof"=> B_DECR, "sort-of"=> B_DECR)

SPECIAL_CASES = Dict("the shit"=> 3, "the bomb"=> 3, "bad ass"=> 1.5, "badass"=> 1.5,
                     "bus stop"=> 0.0, "yeah right"=> -2, "kiss of death"=> -1.5,
                     "to die for"=> 3, "beating heart"=> 3.1, "broken heart"=> -2.9)

#STRUCTS

struct SentiText
    text;words_and_emoticons;is_cap_diff

    function _strip_punc_if_word(token)
        stripped = replace(token, r"^[[:punct:]]+|[[:punct:]]+$" => "")
        if (length(stripped) <= 2)
            return token
        else
            return stripped
        end
    end

    function allcap_differential(words::Array)
        allcap_words = 0
        for word in words
            replaced_word = replace(word, r"[^A-Za-z]" => "")
            if ((length(replaced_word) > 0) && (all(isuppercase, collect(replaced_word))))
                allcap_words += 1
            end
        end
        cap_differential = size(words)[1] - allcap_words
        if (cap_differential < size(words)[1] && 0 < cap_differential)
            return true
        else
            return false
        end
    end

    function SentiText(text)
        if (typeof(text) != String)
            text = string(text)
        end
        temp_words_and_emoticons = map(_strip_punc_if_word, split(text, r"\s+"))
        new(text, temp_words_and_emoticons, allcap_differential(temp_words_and_emoticons))
    end
end

mutable struct SentimentIntensityAnalyzer
    polarity_scores::Dict{<:AbstractString ,<:Float64}

    #Read Lexicons
    function read_file(InputFile::AbstractString)
        f = open(InputFile)
        file_lines = readlines(f)
        close(f)
        return file_lines
    end
    function lex_dict(file::AbstractString)
        lex_dict = Dict{String, Float64}()
        for j in read_file(file)
            push!(lex_dict, split(j, "\t")[1] => parse(Float64, split(j, "\t")[2])) end
        return lex_dict
    end

    function emoji_dict(file::AbstractString)
        emoji_dict = Dict{String, String}()
        for j in read_file(file)
            push!(emoji_dict, split(j, "\t")[1] => split(j, "\t")[2]) end
        return emoji_dict
    end

    lexicon_dictionary = lex_dict(joinpath(dirname(pathof(VaderSentiment)), "lexicons", "vader_lexicon.txt"))
    emoji_dictionary = emoji_dict(joinpath(dirname(pathof(VaderSentiment)), "lexicons", "emoji_utf8_lexicon.txt"))

    function is_negated(input_words::Array, include_nt::Bool = true)
        input_words = [lowercase(string(i)) for i in input_words]
        for word in NEGATION_TOKENS
            if (word in input_words)
                return true end
        end
        if (include_nt)
            for word in input_words
                if occursin("n't", word)
                    return true end
            end
        end
        return false
    end

    function normalize(score::Real)
        norm_score = score/sqrt((score*score)+NORMALIZATION_ALPHA)
        if (norm_score < -1.0)
            return -1.0
        elseif (norm_score > 1.0)
            return 1.0
        else
            return norm_score
        end
    end

    function allcap_differential(words::Array)
        allcap_words = 0
        for word in words
            replaced_word = replace(word, r"[^A-Za-z]" => "")
            if ((length(replaced_word) > 0) && (all(isuppercase, collect(replaced_word))))
                allcap_words += 1
            end
        end
        cap_differential = size(words)[1] - allcap_words
        if (cap_differential < size(words)[1] && 0 < cap_differential)
            return true
        else
            return false
        end
    end

    function scalar_inc_dec(word::AbstractString, valence::Real, has_mixed_caps::Bool)
        scalar = 0.0
        word_lower = lowercase(word)
        if haskey(BOOSTER_DICT, word_lower)
            scalar = BOOSTER_DICT[word_lower]
            if (valence < 0)
                scalar *= -1
            end
            replaced_word = replace(word, r"[^A-Za-z]" => "")
            if (has_mixed_caps && (length(replaced_word) > 0) && all(isuppercase, collect(replaced_word)))
                if (valence > 0)
                    scalar += C_INCR
                else
                    scalar -= C_INCR
                end
            end
        end
        return scalar
    end

    #Calculate Polarity Scores
    function _least_check(valence::Real, words_and_emoticons::Array, i::Integer)
        if (i > 2 && !(haskey(lexicon_dictionary, lowercase(words_and_emoticons[i - 1]))) && lowercase(words_and_emoticons[i - 1]) == "least")
            if (lowercase(words_and_emoticons[i - 2]) != "at" && lowercase(words_and_emoticons[i - 2]) != "very")
                valence = valence * N_SCALAR
            end
        elseif (i > 1 && !(haskey(lexicon_dictionary, lowercase(words_and_emoticons[i - 1]))) && lowercase(words_and_emoticons[i - 1]) == "least")
            valence = valence * N_SCALAR
        end
        return valence
    end

    function _but_check(words_and_emoticons::Array, sentiments::Array)
        words_and_emoticons_lower = [lowercase(string(s)) for s in words_and_emoticons]
        if ("but" in words_and_emoticons_lower)
            but_index = findfirst(x -> x == "but", words_and_emoticons_lower)
            for sentiment in sentiments
                sentiment_index = findfirst(x -> x == sentiment, sentiments)
                if (sentiment_index < but_index)
                    deleteat!(sentiments, sentiment_index)
                    insert!(sentiments, sentiment_index, sentiment*0.5)
                elseif (sentiment_index > but_index)
                    deleteat!(sentiments, sentiment_index)
                    insert!(sentiments, sentiment_index, sentiment*1.5)
                end
            end
        end
        return sentiments
        end

    function _special_idioms_check(valence::Real, words_and_emoticons::Array, i::Integer)
        words_and_emoticons_lower = [lowercase(string(s)) for s in words_and_emoticons]
        onezero = words_and_emoticons_lower[i-1] * " " * words_and_emoticons_lower[i]
        twoonezero = words_and_emoticons_lower[i-2] * " " * words_and_emoticons_lower[i-1] * " " * words_and_emoticons_lower[i]
        twoone = words_and_emoticons_lower[i-2] * " " * words_and_emoticons_lower[i-1]
        threetwoone = words_and_emoticons_lower[i-3] * " " * words_and_emoticons_lower[i-2] * " " * words_and_emoticons_lower[i-1]
        threetwo = words_and_emoticons_lower[i-3] * " " * words_and_emoticons_lower[i-2]
        sequences = [onezero, twoonezero, twoone, threetwoone, threetwo]
        for sequence in sequences
            if haskey(SPECIAL_CASES, sequence)
                valence = SPECIAL_CASES[sequence]
                break
            end
        end
        if (size(words_and_emoticons_lower)[1]-1 > i)
            zeroone = words_and_emoticons_lower[i] * " " * words_and_emoticons_lower[i+1]
            if haskey(SPECIAL_CASES, zeroone)
                valence = SPECIAL_CASES[zeroone]
            end
        end
        if (size(words_and_emoticons_lower)[1]-1 > i+1)
            zeroonetwo = words_and_emoticons_lower[i] * " " * words_and_emoticons_lower[i+1] * " " * words_and_emoticons_lower[i+2]
            if haskey(SPECIAL_CASES, zeroonetwo)
                valence = SPECIAL_CASES[zeroonetwo]
            end
        end
        n_grams = [threetwoone, threetwo, twoone]
        for n_gram in n_grams
            if haskey(BOOSTER_DICT, n_gram)
                valence += BOOSTER_DICT[n_gram]
            end
        end
        return valence
        end

    function _amplify_ep(text::AbstractString)
        c = count(x->(x=='!'), text)
        if (c > 4)
            return 4 * 0.292
        else
            return c * 0.292
        end
    end

    function _amplify_qm(text::AbstractString)
        q = count(x->(x=='?'), text)
        if (q > 1 && q <= 3)
            return q * 0.18
        elseif (q > 1)
            return 0.96
        else
            return 0
        end
    end

    function _punctuation_emphasis(text::AbstractString)
        return _amplify_ep(text) + _amplify_qm(text)
    end

    function _sift_sentiment_scores(sentiments::Array)
        positive_sum = 0.0
        negative_sum = 0.0
        neutral_count = 0
        for sentiment_score in sentiments
            if (sentiment_score > 0)
                positive_sum += (sentiment_score + 1)
            end
            if (sentiment_score < 0)
                negative_sum += (sentiment_score - 1)
            end
            if (sentiment_score == 0)
                neutral_count += 1
            end
        end
        return positive_sum, negative_sum, neutral_count
    end

    function score_valence(sentiments::Array, text::AbstractString)
        if (size(sentiments)[1] > 0)
            sum_s = sum(sentiments)
            if (sum_s > 0)
                sum_s += _punctuation_emphasis(text)
            elseif (sum_s < 0)
                sum_s -= _punctuation_emphasis(text)
            end
            compound = normalize(sum_s)
            pos_sum, neg_sum, neu_count = _sift_sentiment_scores(sentiments)
            if (pos_sum > abs(float(neg_sum)))
                pos_sum += _punctuation_emphasis(text)
            elseif (pos_sum < abs(float(neg_sum)))
                neg_sum -= _punctuation_emphasis(text)
            end
            total = pos_sum + abs(float(neg_sum)) + neu_count
            pos = abs(float(pos_sum / total))
            neg = abs(float(neg_sum / total))
            neu = abs(float(neu_count / total))
        else
            compound = 0.0
            pos = 0.0
            neg = 0.0
            neu = 0.0
        end
        sentiment_dict = Dict("neg"=> round(neg, digits = 3),
                              "neu"=> round(neu, digits = 3),
                              "pos"=> round(pos, digits = 3),
                              "compound"=> round(compound, digits = 4))
        return sentiment_dict
    end

    function _negation_check(valence::Real, words_and_emoticons::Array, start_i::Integer, i::Integer)
        words_and_emoticons_lower = [lowercase(string(s)) for s in words_and_emoticons]
        if (start_i == 1)
            if (is_negated([words_and_emoticons_lower[i - (start_i)]]))
                valence *= N_SCALAR
            end
        end
        if (start_i == 2)
            if (words_and_emoticons_lower[i-2] == "never" && (words_and_emoticons_lower[i-1] == "so" || words_and_emoticons_lower[i-1] == "this"))
                valence *= 1.25
            elseif (words_and_emoticons_lower[i-2] == "without" && words_and_emoticons_lower[i-1] == "doubt")
                valence = valence
            elseif is_negated([words_and_emoticons_lower[i - (start_i)]])
                valence *= N_SCALAR
            end
        end
        if (start_i == 3)
            if (words_and_emoticons_lower[i-3] == "never" && (words_and_emoticons_lower[i-2] == "so" || words_and_emoticons_lower[i-2] == "this") || (words_and_emoticons_lower[i-1] == "so" || words_and_emoticons_lower[i - 1] == "this"))
                valence *= 1.25
            elseif (words_and_emoticons_lower[i-3] == "without" && (words_and_emoticons_lower[i-2] == "doubt" || words_and_emoticons_lower[i-1] == "doubt"))
                valence = valence
            elseif is_negated([words_and_emoticons_lower[i - (start_i)]])
                valence *= N_SCALAR
            end
        end
        return valence
    end

    function sentiment_valence(valence::Real, sentitext::SentiText, item::AbstractString, i::Integer, sentiments::Array)
        is_cap_diff = sentitext.is_cap_diff
        words_and_emoticons = sentitext.words_and_emoticons
        item_lowercase = lowercase(item)
        if haskey(lexicon_dictionary, item_lowercase)
            valence = lexicon_dictionary[item_lowercase]
            if (item_lowercase == "no" && i != size(words_and_emoticons)[1] && haskey(lexicon_dictionary, lowercase(words_and_emoticons[i+1])))
                valence = 0.0
            end
            if ((i > 1 && lowercase(words_and_emoticons[i - 1]) == "no") || (i > 2 && lowercase(words_and_emoticons[i - 2]) == "no") || (i > 3 && lowercase(words_and_emoticons[i - 3]) == "no" && lowercase(words_and_emoticons[i - 1]) in ["or", "nor"]))
                valence = lexicon_dictionary[item_lowercase] * N_SCALAR
            end

            replaced_item = replace(item, r"[^A-Za-z]" => "")
            if (is_cap_diff && (length(replaced_item) > 0) && all(isuppercase, collect(replaced_item)))
                if (valence > 0)
                    valence += C_INCR
                else
                    valence -= C_INCR
                end
            end

            for start_i in 1:3
                if (i > start_i && !(haskey(lexicon_dictionary, lowercase(words_and_emoticons[i - (start_i)]))))
                    s = scalar_inc_dec(words_and_emoticons[i - (start_i)], valence, is_cap_diff)
                    if (start_i == 2 && s != 0)
                        s *= 0.95
                    end
                    if (start_i == 3 && s != 0)
                        s *= 0.9
                    end
                    valence += s
                    valence = _negation_check(valence, words_and_emoticons, start_i, i)
                    if (start_i == 3)
                        valence = _special_idioms_check(valence, words_and_emoticons, i)
                    end
                end
            end
            valence = _least_check(valence, words_and_emoticons, i)

        end
        push!(sentiments, valence)
        return sentiments
    end

    function polarity_scores(text::AbstractString)
        text_no_emoji = ""
        prev_space = true
        for chr in split(text, "")
            if haskey(emoji_dictionary, chr)
                description = emoji_dictionary[chr]
                if (!prev_space)
                    text_no_emoji *= " "
                end
                text_no_emoji *= description
                prev_space = false
            else
                text_no_emoji *= chr
                prev_space = (chr == " ")
            end
        end
        text = strip(text_no_emoji)
        sentitext = SentiText(text)

        sentiments = []
        words_and_emoticons = sentitext.words_and_emoticons
        for (i, item) in enumerate(words_and_emoticons)
            valence = 0
            if haskey(BOOSTER_DICT, lowercase(item))
                push!(sentiments, valence)
                continue
            end
            if (i < size(words_and_emoticons)[1]-1 && lowercase(item) == "kind" && lowercase(words_and_emoticons[i+1]) == "of")
                push!(sentiments, valence)
                continue
            end
            sentiments = sentiment_valence(valence, sentitext, item, i, sentiments)
        end

        sentiments = _but_check(words_and_emoticons, sentiments)
        valence_dict = score_valence(sentiments, text)
        return valence_dict
    end

    function SentimentIntensityAnalyzer(text::AbstractString)
        new(polarity_scores(text))
    end
end

max_stars_repo_name: nusretipek/JuliaVader

max_issues_repo_name: nusretipek/JuliaVader


text:
"""
    F1 = evalF1(f1, z, phi)

Calculate the values of the `F1` filter (tranformed prototype filter) of a
Saramaki HBF at the given points.
"""
function evalF1(f1, z, phi=1.0)
    z = z / phi
    F1 = 0.5 * ones(length(z))
    for i = 1:length(f1)
        F1 = F1 + f1[i] * z .^ (2*i - 1)
    end
    return F1
end

max_stars_repo_name: Mahmoud-Kharsa/DeltaSigma

max_issues_repo_name: Mahmoud-Kharsa/DeltaSigma

max_forks_repo_name: Mahmoud-Kharsa/DeltaSigma


text:
using Documenter, JuAFEM

include("generate.jl")
EXAMPLEDIR = joinpath(@__DIR__, "src", "examples")
EXAMPLES = ["heat_equation.jl",]
GENERATEDEXAMPLES = generate.(joinpath.(EXAMPLEDIR, EXAMPLES))

# Build documentation.
makedocs(
    format = :html,
    sitename = "JuAFEM.jl",
    doctest = true,
    strict = VERSION.minor == 6 && sizeof(Int) == 8, # only strict mode on 0.6 and Int64
    pages = Any[
        "Home" => "index.md",
        "man/fe_intro.md",
        "man/getting_started.md",
        "Library" => ["lib/maintypes.md",
                      "lib/utility_functions.md"],
        "Examples" => GENERATEDEXAMPLES
        ]
        )

# Deploy built documentation from Travis.
deploydocs(
    repo = "github.com/KristofferC/JuAFEM.jl.git",
    target = "build",
    julia = "0.6", # deploy from release bot
    deps = nothing,
    make = nothing,
)


text:
function get_julia_bib()
    import_bibtex(joinpath(@__DIR__, "julia.bib"))
end

function add_texttt(pkg_name)
    "\\texttt{" * pkg_name * "}"
end


text:
function vec_expt_est(n)
    # Estimate method. Not used, but could substitute for vec_expt_full.
    Eₙ = sqrt(2 / (π*(n - 1/2)))
    return Eₙ
end

function vec_expt_full(n)
    if isodd(n)
        Eₙ = prod(range(1, n-2, step=2)) / prod(range(2, n-1, step=2))
    else
        Eₙ = (2/π) * prod(range(2, n-2, step=2)) / prod(range(1, n-1, step=2))
    end
    return Eₙ
end

function orth_vec(n)
    mvn_1 = Distributions.MvNormal(n, 1)
    z₁ = Distributions.rand(mvn_1)
    z₁ = z₁ / LinearAlgebra.norm(z₁)
    z₁_null = LinearAlgebra.nullspace(Matrix(z₁'))
    mvn_2 = Distributions.MvNormal(n-1, 1)
    v₂ = Distributions.rand(mvn_2)
    v₂ = v₂ / LinearAlgebra.norm(v₂)
    z₂ = vec(sum(v₂' .* z₁_null, dims=2))
    return z₁, z₂
end


text:
export bmm
using Base: has_offset_axes, unsafe_convert
using LinearAlgebra: chkstride1, BlasInt
using LinearAlgebra.BLAS: libblas, @blasfunc
using AutoGrad: AutoGrad, @primitive1

"""
    bmm(A, B ; transA=false, transB=false)

Perform a batch matrix-matrix product of matrices stored in `A` and `B`. size(A,2) ==
size(B,1) and size(A)[3:end] and size(B)[3:end] must match.  If A is a (m,n,b...) tensor, B is
a (n,k,b...) tensor, and the output is a (m,k,b...)  tensor.

"""
function bmm(A, B; transA::Bool = false, transB::Bool = false)
    sa, sb = size(A), size(B)
    m, k   = transA ? (sa[2],sa[1]) : (sa[1],sa[2])
    kb, n  = transB ? (sb[2],sb[1]) : (sb[1],sb[2])
    @assert kb == k && sa[3:end]==sb[3:end]
    a3, b3 = reshape(A,sa[1],sa[2],:), reshape(B,sb[1],sb[2],:)
    C = similar(A, m, n, size(a3,3))
    bmm!((transA ? 'T' : 'N'), (transB ? 'T' : 'N'), 1, a3, b3, 0, C)
    reshape(C,m,n,sb[3:end]...)
end

for (gemm, elty) in
    ((:dgemm_,:Float64),
     (:sgemm_,:Float32),)
    @eval begin
        function bmm!(transA::AbstractChar,
                      transB::AbstractChar,
                      alpha::Number,
                      A::AbstractArray{$elty, 3},
                      B::AbstractArray{$elty, 3},
                      beta::Number,
                      C::AbstractArray{$elty, 3})
            @assert !has_offset_axes(A, B, C)
            @assert size(A, 3) == size(B, 3) == size(C, 3) "batch size mismatch"
            m = size(A, transA == 'N' ? 1 : 2)
            ka = size(A, transA == 'N' ? 2 : 1)
            kb = size(B, transB == 'N' ? 1 : 2)
            n = size(B, transB == 'N' ? 2 : 1)
            if ka != kb || m != size(C,1) || n != size(C,2)
                throw(DimensionMismatch("A has size ($m,$ka), B has size ($kb,$n), C has size $(size(C))"))
            end
            chkstride1(A)
            chkstride1(B)
            chkstride1(C)

            ptrA = unsafe_convert(Ptr{$elty}, A)
            ptrB = unsafe_convert(Ptr{$elty}, B)
            ptrC = unsafe_convert(Ptr{$elty}, C)

            for k in 1:size(A, 3)
                ccall((@blasfunc($gemm), libblas), Cvoid,
                    (Ref{UInt8}, Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt},
                     Ref{BlasInt}, Ref{$elty}, Ptr{$elty}, Ref{BlasInt},
                     Ptr{$elty}, Ref{BlasInt}, Ref{$elty}, Ptr{$elty},
                     Ref{BlasInt}),
                     transA, transB, m, n,
                     ka, alpha, ptrA, max(1,stride(A,2)),
                     ptrB, max(1,stride(B,2)), beta, ptrC,
                     max(1,stride(C,2)))

                ptrA += size(A, 1) * size(A, 2) * sizeof($elty)
                ptrB += size(B, 1) * size(B, 2) * sizeof($elty)
                ptrC += size(C, 1) * size(C, 2) * sizeof($elty)
            end

            C
        end
    end
end

@primitive1 bmm(x1,x2; transA::Bool=false, transB::Bool=false),dy,y (transA ? bmm(x2, dy; transA=transB , transB=true) :  bmm(dy, x2;  transA=false, transB=!transB) )    (transB ? bmm(dy,x1; transA=true , transB=transA) :  bmm(x1, dy;  transA=!transA , transB=false))


text:
using Test
using ScottishTaxBenefitModel
using Observables
using .ModelHousehold: count,Household, le_age, ge_age
using .Results: aggregate!, init_household_result
using ScottishTaxBenefitModel.Runner: do_one_run
using .Intermediate: MTIntermediate, make_intermediate
using .UBI: calc_UBI!,make_ubi_post_adjustments!
using .STBParameters
using .STBIncomes
using .ExampleHelpers
using .Monitor: Progress
using .TheEqualiser
using .STBOutput
using PrettyTables
using CSV

function load_system()::TaxBenefitSystem
	sys = load_file( joinpath( Definitions.MODEL_PARAMS_DIR, "sys_2021_22.jl" ))
	#
	# Note that as of Budget21 removing these doesn't actually happen till May 2022.
	#
	load_file!( sys, joinpath( Definitions.MODEL_PARAMS_DIR, "sys_2021-uplift-removed.jl"))
	# uc taper to 55
	load_file!( sys, joinpath( Definitions.MODEL_PARAMS_DIR, "budget_2021_uc_changes.jl"))
	weeklyise!( sys )
	return sys
end

settings = Settings()
settings.do_marginal_rates = false
settings.poverty_line=100.0 # arbit

# observer = Observer(Progress("",0,0,0))
tot = 0
obs = Observable( Progress(settings.uuid,"",0,0,0,0))
of = on(obs) do p
    global tot
    println(p)

    tot += p.step
    println(tot)
end

@testset "Eq UBI Tests" begin
    base = load_system()
    sys = load_system()
    sys.ubi.abolished = false
    sys.it.personal_allowance = 0.0
    make_ubi_pre_adjustments!( sys )
    base_res = do_one_run(
        settings,
        [base],
        obs )
    summary = summarise_frames(base_res,settings)
    base_cost = summary.income_summary[1][1,:net_cost]

    eq = equalise(
        eq_it,
        sys,
        settings,
        base_cost,
        obs )
    sys.it.non_savings_rates .+= eq
    ubi_res = do_one_run(
        settings,
        [sys],
        obs )
    ubi_summary = summarise_frames(ubi_res,settings)
    ubi_cost = ubi_summary.income_summary[1][1,:net_cost]

    println( "needs tax rise of $eq")
    net_cost = ubi_cost - base_cost
    println( "net_cost=$net_cost" )
    println( "taxrates $(sys.it.non_savings_rates)")
    println("ubi summary")
    CSV.write( "ubi_summary.income_summary.csv", ubi_summary.income_summary[1] )
    # pretty_table( ubi_summary.income_summary[1][1,:] )

end

max_stars_repo_name: grahamstark/ScottishTaxBenefitModel

max_issues_repo_name: grahamstark/ScottishTaxBenefitModel

max_forks_repo_name: grahamstark/ScottishTaxBenefitModel


text:
using ONNX
using Test

include("readwrite.jl")


text:
using MLDatasets: MNIST
using Flux: onehotbatch
using ProtoGrad: Linear, Dropout, Compose, relu, softmax
using ProtoGrad: SupervisedObjective, cross_entropy, class_error
using ProtoGrad: GradientDescent, forever, within_training_mode
using ProgressMeter: @showprogress
using StatsBase: sample
using Serialization: serialize, deserialize

train_x, train_y = MNIST.traindata(Float32, dir=joinpath(".", "datasets", "MNIST"))
test_x, test_y = MNIST.testdata(Float32, dir=joinpath(".", "datasets", "MNIST"))

train_x_flat = reshape(train_x, prod(size(train_x)[1:2]), size(train_x)[3])
train_y_onehot = onehotbatch(train_y, 0:9)

input_size, dataset_size = size(train_x_flat)
hidden_size = 100
num_classes = 10

m = Compose(
    Dropout(0.1),
    Linear(input_size=>hidden_size),
    x -> relu.(x),
    Dropout(0.1),
    Linear(hidden_size=>num_classes),
    softmax,
)

batch_size = 128

training_batches = forever() do
    idx = sample(1:size(train_x_flat)[end], batch_size, replace = false)
    return (train_x_flat[:, idx], train_y_onehot[:, idx])
end

f = SupervisedObjective(cross_entropy, training_batches)

optimizer = GradientDescent(stepsize=1e-1)
iterations = Iterators.Stateful(optimizer(m, f))

num_epochs = 100
num_batches_per_epoch = 500

within_training_mode() do
    @showprogress for epoch in 1:num_epochs
        for output in Iterators.take(iterations, num_batches_per_epoch)
            global m = output.solution
        end
    end
end

model_filename = "./serialized_mlp_model"
println("saving model to $(model_filename)")
serialize(model_filename, m)

m_deserialized = deserialize(model_filename)

test_x_flat = reshape(test_x, prod(size(test_x)[1:2]), size(test_x)[3])
test_y_onehot = onehotbatch(test_y, 0:9)

err = class_error(m_deserialized(test_x_flat), test_y_onehot)

println("test error = $(err * 100)%")


text:
# Detect how to link a shared library:
@static if Sys.isapple()
    libtool = "/usr/bin/libtool"
    println("Linker: $libtool")
    ipplib = ENV["IPPROOT"] * "/lib"

    link(objfile, libfile, linkfiles) =
        run(`$libtool "-L$ipplib $linkfiles" -dynamic -o "$libfile" "$objfile"`)
end

@static if Sys.islinux()
    gcc = strip(read(`which gcc`, String))
    gcc == "" && error("libtool or gcc is required")
    println("Linker: $gcc")
    ipplib = ENV["IPPROOT"] * "/lib/intel64"

    # push!(Libdl.DL_LOAD_PATH,"path_to_libmean.so")

    link(objfile, libfile, linkfiles) = run(
        `$gcc "-L$ipplib $linkfiles" -shared -Wl,-export-dynamic "$objfile" -o "$libfile"`,
    )
end

@static if Sys.iswindows()
    error("Not implemented: don't know how to create a shared lib on Windows")
end

"""
Runs `gcc` on `code`, links with `ipp`, creates a shared library and returns a library
handle `lib` for use with `Libdl.dlsym(lib, symbol)`.
"""
function load_ipp(code, options = ``)

    function load(tmpdir)
        objfile = "$tmpdir/ipp.o"
        libfile = "$tmpdir/ipp.so"
        linkfiles = "-lippi -lippdc -lipps -lippcore"

        # Pipe the input program to gcc:
        gcc_cmd = `gcc -c $code -o "$objfile" -fPIC $options`
        println(gcc_cmd)
        run(gcc_cmd)

        # Create a shared library:
        link(objfile, libfile, linkfiles)
        return Libc.Libdl.dlopen(libfile)
    end

    load(mktempdir())
end

# macOS tests

# compiler
# gcc -c ../src/ipp.c -o test.o -fPIC -O3

# linker
# /usr/bin/libtool -dynamic -o test.so test.o -L/opt/intel/oneapi/ipp/2021.1.1/lib -lippi -lippdc -lipps -lippcore


text:
mutable struct support_data_t <: LCMType
    timestamp::Int64
    body_name::String
    num_contact_pts::Int32
    contact_pts::Matrix{Float64}
    total_normal_force_upper_bound::Float32
    total_normal_force_lower_bound::Float32
    support_logic_map::SVector{4, Bool}
    use_support_surface::Bool
    support_surface::SVector{4, Float32}
    mu::Float64
end

@lcmtypesetup(support_data_t,
    contact_pts => (3, num_contact_pts),
)


text:
module ChemicalEngineeringDesign

using NLsolve
using ForwardDiff

include("pipes.jl")
include("substances.jl")
include("streams.jl")
include("hx_core.jl")

end # module


text:
#-------------------------------------------------------------------
#* EMSO Model Library (EML) Copyright (C) 2004 - 2007 ALSOC.
#*
#* This LIBRARY is free software; you can distribute it and/or modify
#* it under the therms of the ALSOC FREE LICENSE as available at
#* http://www.enq.ufrgs.br/alsoc.
#*
#* EMSO Copyright (C) 2004 - 2007 ALSOC, original code
#* from http://www.rps.eng.br Copyright (C) 2002-2004.
#* All rights reserved.
#*
#* EMSO is distributed under the therms of the ALSOC LICENSE as
#* available at http://www.enq.ufrgs.br/alsoc.
#*
#*--------------------------------------------------------------------
#* Model of basic streams
#*----------------------------------------------------------------------
#* Author: Paula B. Staudt and Rafael de P. Soares
#* $Id$
#*---------------------------------------------------------------------
type sourceNoFlow
	sourceNoFlow()=begin
		PP=outers.PP
		NComp=outers.NComp
		new(
			DanaPlugin(Dict{Symbol,Any}(
				:Brief=>"External Physical Properties",
				:Type=>"PP"
			)),
			DanaInteger(Dict{Symbol,Any}(
				:Brief=>"Number of chemical components",
				:Lower=>1
			)),
			stream(Dict{Symbol,Any}(
				:Brief=>"Outlet stream",
				:PosX=>1,
				:PosY=>0.5256,
				:Symbol=>"_{out}",
				:Protected=>true
			)),
			[
				:(Outlet.z = 1/NComp),
				:(Outlet.h = 0 * "J/mol"),
				:(Outlet.T = 300 * "K"),
				:(Outlet.F = 0 * "kmol/h"),
				:(Outlet.P = 1 * "atm"),
				:(Outlet.v = 0),
			],
			[
				"Stream Molar Composition","Stream Molar Enthalpy","Stream Temperature","Stream Molar Flow","Stream Pressure","Stream Vapour Fraction",
			],
			[:PP,:NComp,],
			[:Outlet,]
		)
	end
	PP::DanaPlugin
	NComp::DanaInteger
	Outlet::stream
	equations::Array{Expr,1}
	equationNames::Array{String,1}
	parameters::Array{Symbol,1}
	variables::Array{Symbol,1}
	attributes::Dict{Symbol,Any}
end
export sourceNoFlow
function setEquationFlow(in::sourceNoFlow)
	addEquation(1)
	addEquation(2)
	addEquation(3)
	addEquation(4)
	addEquation(5)
	addEquation(6)
end
function atributes(in::sourceNoFlow,_::Dict{Symbol,Any})
	fields::Dict{Symbol,Any}=Dict{Symbol,Any}()
	fields[:Pallete]=true
	fields[:Icon]="icon/SourceNoFlow"
	fields[:Brief]="Simple Material stream source with no flow."
	fields[:Info]="
	This model should be used for boundary streams.
	Usually these streams are known and come from another process
	units."
	drive!(fields,_)
	return fields
end
sourceNoFlow(_::Dict{Symbol,Any})=begin
	newModel=sourceNoFlow()
	newModel.attributes=atributes(newModel,_)
	newModel
end


text:
#=
Given a string, find the longest palindromic contiguous substring. If there are more than one with the maximum length, return any one.

For example, the longest palindromic substring of "aabcdcb" is "bcdcb". The longest palindromic substring of "bananas" is "anana".
=#

function longestpalindromicsubstring(s::String)
    longestpalindrome = ""
    for index in eachindex(s)
        current_palindrome = longestpalindromecenter(s, index)
        if length(current_palindrome) >= length(longestpalindrome)
            longestpalindrome = current_palindrome
        end
    end
    return longestpalindrome
end

function longestpalindromecenter(s::String, index::Int)
    longestpalindrome = s[index:index]
    i = 1
    if index != 1 && index != length(s)
        while index-i >= 1 && index+i <= length(s) && ispalindrome(s[index-i:index+i])
            longestpalindrome = s[index-i:index+i]
            i = i + 1
        end
    end
    i = i-1
    if index+i < length(s)
        if ispalindrome(s[index-i:index+i+1])
            longestpalindrome = s[index-i:index+i+1]
        end
    end
    if index-i > 1
        if ispalindrome(s[index-i-1:index+i])
            longestpalindrome = s[index-i-1:index+i]
        end
    end
    return longestpalindrome
end

function ispalindrome(s::String)
    if isodd(length(s))
        half = floor(Int8, length(s)/2)
        is_palindrome = s[1:half] == reverse(s[half+2:end])
    else
        half = floor(Int8, length(s)/2)
        is_palindrome = s[1:half] == reverse(s[half+1:end])
    end
    return is_palindrome
end

max_stars_repo_name: DominiqueCaron/daily-coding-problem

max_issues_repo_name: DominiqueCaron/daily-coding-problem

max_forks_repo_name: DominiqueCaron/daily-coding-problem


text:
"""
This package provides provides a set of functions to facilitate the field-theoretic
treatment of monolayer graphene using the tight-binding model. The Hamiltonian
employed by this package includes only the nearest-neighbor hopping term with
``t = 2.8``eV. The derivation of the formalism is available
[here](https://arxiv.org/pdf/2007.06984.pdf).

# Example:

Pick a few graphene coordinates:
```
a1 = graphene_A(0, 17)
a2 = graphene_A(1, 1)
a3 = graphene_B(3, -2)
a4 = graphene_A(2, 7)
a5 = graphene_A(0, 1)
```

Next, initialize a couple of impurities and couple them to the coordinates:

```
ϵ1 = 1.7
ϵ2 = -0.2

V1 = 0.4
V2 = 2.0
V3 = -7.1

imp1 = ImpurityState(ϵ1, [(V1, a4), (V2, a3)])
imp2 = ImpurityState(ϵ2, [(V3, a5)])
```

Create a few coupling integrals `c` and use them to create some perturbations
```
c1 = 3.0
c2 = 5.0
c3 = -9.0

p1 = (a1, a1, c1)
p2 = (a2, a1, c2)
p3 = (a3, a1, c3)
```

Finally, initialize the system:
```
μ = 0.2
T = 0.0

my_system = mkGrapheneSystem(μ, T, [imp1, imp2], [p1, p2, p3])
```

`my_system` can now be used to calculate the relevant Green's functions
([`δG_R`](@ref), [`G_R`](@ref), [`δΓ`](@ref), [`Γ`](@ref)), from which
quantities of interest can be computed.
"""
module GrapheneQFT

using Cubature
using Interpolations
using LinearAlgebra
using MLStyle
using QuadGK

export GrapheneCoord,
    graphene_A,
    graphene_B,
    graphene_neighbors,
    crystal_to_cartesian,
    Coupling,
    ImpurityState,
    GrapheneSystem,
    mkGrapheneSystem,
    δG_R,
    G_R,
    δΓ,
    Γ,
    Ψ_pz,
    coulomb_potential_pz,
    coulomb_potential_pz_interp,
    coulomb_energy_pz_pz

include("computed_quantities.jl")
include("orbitals.jl")

end # module


text:
@testset "Error Constructor" begin
    @test_throws CubicEoSDatabase.NotFoundError throw(CubicEoSDatabase.NotFoundError("methane", "foo database"))
end


text:
using Test
using VPTrees
using Random
using StringDistances
using Aqua

Aqua.test_all(VPTrees)

@testset "VPTree" begin include("test_vantage_point_tree.jl") end


text:
function _delimcase(cur_string::AbstractString, cur_delim::AbstractString)
  replace(
    _defaultcase(cur_string),
    " " => cur_delim
  )
end


text:
#=
Copyright (c) 2015, Intel Corporation
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
=#

using HPAT
using MPI
using DocOpt

@acc hpat function calc1Dsum(file_name)
    arr = DataSource(Vector{Float64},HDF5,"/labels",file_name)
    return sum(arr)
end

function main()
    doc = """Sum a large array read from file (stress test).

Usage:
  1D_sum.jl -h | --help
  1D_sum.jl [--file=<file>]

Options:
  -h --help                  Show this screen.
  --file=<file>              Specify input file; defaults to HPAT's default generated data file.

"""
    arguments = docopt(doc)

    file_name = HPAT.getDefaultDataPath()*"1D_large.hdf5"
    if (arguments["--file"] != nothing)
        file_name= arguments["--file"]
    end

    S = calc1Dsum(file_name)
    if MPI.Comm_rank(MPI.COMM_WORLD)==0 println("result ", S) end
end

main()


text:
#!/usr/bin/env julia

"""
# Problem 6: Sum square differences

The sum of the squares of the first ten natural numbers is,

    1² + 2² + ... + 10² = 385

The square of the sum of the first ten natural numbers is,

    (1 + 2 + ... + 10)² = 55² = 3025

Hence the difference between the sum of the squares of the first ten natural
numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first `n` natural
numbers and the square of the sum.
"""
module Problem006

include("Sequences.jl")

"""
    solve(; n::Integer = 100)

Returns the solution for [`Problem006`](@ref) with the given parameters.
"""
function solve(; n::Integer = 100)
    return Sequences.triangular(n)^2 - Sequences.sum_of_squares(n)
end

end

max_stars_repo_name: curtislb/ProjectEuler

max_issues_repo_name: curtislb/ProjectEuler

max_forks_repo_name: curtislb/ProjectEuler


text:
@testset "SimpleDiff tests" begin
    @test deepdiff(1, 2) == deepdiff(1, 2)
end


text:
export rule

@rule GCV(:z, Marginalisation) (q_y_x::Any, q_κ::Any, q_ω::Any, meta::GCVMetadata) = begin

    y_x_mean, y_x_v = mean_cov(q_y_x)
    κ_mean, κ_var   = mean_var(q_κ)
    ω_mean, ω_var   = mean_var(q_ω)

    psi = @inbounds (y_x_mean[1] - y_x_mean[2]) ^ 2 + y_x_v[1,1] + y_x_v[2,2] - y_x_v[1,2] - y_x_v[2,1]
    A = exp(-ω_mean + ω_var / 2)

    a = κ_mean
    b = psi * A
    c = -κ_mean
    d = κ_var

    return ExponentialLinearQuadratic(get_approximation(meta), a, b, c, d)
end

@rule GCV(:z, Marginalisation) (q_y::Any, q_x::Any, q_κ::Any, q_ω::Any, meta::GCVMetadata) = begin

    y_mean, y_var = mean_var(q_y)
    x_mean, x_var = mean_var(q_x)
    κ_mean, κ_var = mean_var(q_κ)
    ω_mean, ω_var = mean_var(q_ω)

    psi = (y_mean - x_mean) ^ 2 + y_var+ x_var
    A = exp(-ω_mean + ω_var / 2)

    a = κ_mean
    b = psi * A
    c = -κ_mean
    d = κ_var

    return ExponentialLinearQuadratic(get_approximation(meta), a, b, c, d)
end


text:
module Hive

using Compat
using Thrift
using DataFrames

import Base: close, isready, show, eof, start, next, done

export HiveSession, HiveAuth, HiveAuthSASLPlain
export eof, close, isready, result, cancel, get_info, show
export catalogs, schemas, tables, tabletypes, functions, columns, execute
export dataframe, dataframes, records
export start, next, done

export InfoType

# enable logging only during debugging
#using Logging
#const logger = Logging.configure(level=DEBUG)
##const logger = Logging.configure(filename="/tmp/hive$(getpid()).log", level=DEBUG)
#macro logmsg(s)
#    quote
#        debug($(esc(s)))
#    end
#end
macro logmsg(s)
end

# package code goes here
include("HS2/HS2.jl")
using .HS2
include("sess.jl")
include("types.jl")
include("resultset.jl")
include("util.jl")
include("api.jl")

end # module


text:
types = {
    Bool, Char,
    Int8, Uint8, Int16, Uint16, Int32, Uint32, Int64, Uint64, Float32, Float64,
    Rational{Int8}, Rational{Uint8}, Rational{Int16}, Rational{Uint16},
    Rational{Int32}, Rational{Uint32}, Rational{Int64}, Rational{Uint64}
}
vals = [
    typemin(Int64),
    -int64(maxintfloat(Float64))+Int64[-4:1],
    typemin(Int32),
    -integer(maxintfloat(Float32))+(-4:1),
    -2:2,
    integer(maxintfloat(Float32))+(-1:4),
    typemax(Int32),
    int64(maxintfloat(Float64))+Int64[-1:4],
    typemax(Int64),
]

for T=types, S=types, x=vals
    a = convert(T,x)
    b = convert(S,x)
    if (isa(a,Char) && !is_valid_char(a)) || (isa(b,Char) && !is_valid_char(b))
        continue
    end
    #println("$(typeof(a)) $a")
    #println("$(typeof(b)) $b")
    @test isequal(a,b) == (hash(a)==hash(b))
    # for y=vals
    #     println("T=$T; S=$S; x=$x; y=$y")
    #     c = convert(T,x//y)
    #     d = convert(S,x//y)
    #     @test !isequal(a,b) || hash(a)==hash(b)
    # end
end

# hashing collections (e.g. issue #6870)
vals = {[1,2,3,4], [1 3;2 4], {1,2,3,4}, [1,3,2,4],
        [1,0], [true,false], bitpack([true,false]),
        Set([1,2,3,4]),
        Set([1:10]),                 # these lead to different key orders
        Set([7,9,4,10,2,3,5,8,6,1]), #
        [42 => 101, 77 => 93], {42 => 101, 77 => 93},
        (1,2,3,4), (1.0,2.0,3.0,4.0), (1,3,2,4),
        ("a","b"), (SubString("a",1,1), SubString("b",1,1)),
        # issue #6900
        [x => x for x in 1:10],
        [7=>7,9=>9,4=>4,10=>10,2=>2,3=>3,8=>8,5=>5,6=>6,1=>1]}

for a in vals, b in vals
    @test isequal(a,b) == (hash(a)==hash(b))
end

@test hash(RopeString("1","2")) == hash("12")
@test hash(SubString("--hello--",3,7)) == hash("hello")
@test hash(:(X.x)) == hash(:(X.x))
@test hash(:(X.x)) != hash(:(X.y))

@test hash([1,2]) == hash(sub([1,2,3,4],1:2))

max_stars_repo_name: Aerlinger/julia

max_issues_repo_name: Aerlinger/julia

max_forks_repo_name: Aerlinger/julia


text:
            #====================#
            #=== Medium State ===#
            #====================#

#=== elastic 2d ===#
 type elastic2d{T1,T2,T3,T4,T5,T6,T7<:Real}
    pvel::Array{Float64,2}
    svel::Array{Float64,2}
    rho::Array{Float64,2}
    lambda::Array{Float64,2}
    mu::Array{Float64,2}
    dx::T1
    dz::T2
    dt::T3
    DZ::T4
    HX::T5
    nDZ::Int64
    nHX::Int64
    BDnDZ::Int64
    BDnHX::Int64
    T::T6 # Total Simulation Time
    nT::Int64 # Total discretized sampling time
    pkf::T7
    ext::Int64
    iflag::Int64
end
#=== acoustic 2d ===#
 type acoustic2d{T1,T2,T3,T4,T5,T6,T7<:Real}
    pvel::Array{Float64,2}
    rho::Array{Float64,2}
    lambda::Array{Float64,2}
    dx::T1
    dz::T2
    dt::T3
    DZ::T4
    HX::T5
    nDZ::Int64
    nHX::Int64
    BDnDZ::Int64
    BDnHX::Int64
    T::T6 # Total Simulation Time
    nT::Int64 # Total discretized sampling time
    pkf::T7
    ext::Int64
    iflag::Int64
end

#=== elastic 3d ===#
 type elastic3d{T1,T2,T3,T4,T5,T6,T7,T8,T9<:Real}
    pvel::Array{Float64,3}
    svel::Array{Float64,3}
    rho::Array{Float64,3}
    lambda::Array{Float64,3}
    mu::Array{Float64,3}
    dx::T1
    dy::T2
    dz::T3
    dt::T4
    DZ::T5
    HX::T6
    HY::T7
    nDZ::Int64
    nHX::Int64
    nHY::Int64
    BDnDZ::Int64
    BDnHX::Int64
    BDnHY::Int64
    T::T8 # Total Simulation Time
    nT::Int64 # Total discretized sampling time
    pkf::T9
    ext::Int64
    iflag::Int64
end

#=== acoustic 3d ===#
 type acoustic3d{T1,T2,T3,T4,T5,T6,T7,T8,T9<:Real}
    pvel::Array{Float64,3}
    rho::Array{Float64,3}
    lambda::Array{Float64,3}
    dx::T1
    dy::T2
    dz::T3
    dt::T4
    DZ::T5
    HX::T6
    HY::T7
    nDZ::Int64
    nHX::Int64
    nHY::Int64
    BDnDZ::Int64
    BDnHX::Int64
    BDnHY::Int64
    T::T8 # Total Simulation Time
    nT::Int64 # Total discretized sampling time
    pkf::T9
    ext::Int64
    iflag::Int64
end

            #==========================#
            #=== Obsorbing boundary ===#
            #==========================#

#=== elastic 2d ===#
 type elbd2d
    bfull::Array{Float64,1}
    bhalf::Array{Float64,1}
    afull::Array{Float64,1}
    ahalf::Array{Float64,1}
    PVxBTxx::Array{Float64,2}
    PVxBTxz::Array{Float64,2}
    PVzBTzz::Array{Float64,2}
    PVzBTxz::Array{Float64,2}
    PTxxBVx::Array{Float64,2}
    PTxxBVz::Array{Float64,2}
    PTzzBVx::Array{Float64,2}
    PTzzBVz::Array{Float64,2}
    PTxzBVx::Array{Float64,2}
    PTxzBVz::Array{Float64,2}
end
#=== acoustic 2d ===#
 type acbd2d
    bfull::Array{Float64,1}
    bhalf::Array{Float64,1}
    afull::Array{Float64,1}
    ahalf::Array{Float64,1}
    PVxBTpp::Array{Float64,2}
    PVzBTpp::Array{Float64,2}
    PTppBVx::Array{Float64,2}
    PTppBVz::Array{Float64,2}
end

#=== elastic 3d ===#
 type elbd3d
    bfull::Array{Float64,1}
    bhalf::Array{Float64,1}
    afull::Array{Float64,1}
    ahalf::Array{Float64,1}
    PVzBTzz::Array{Float64,3}
    PVzBTxz::Array{Float64,3}
    PVzBTyz::Array{Float64,3}
    PVxBTxx::Array{Float64,3}
    PVxBTxz::Array{Float64,3}
    PVxBTxy::Array{Float64,3}
    PVyBTyy::Array{Float64,3}
    PVyBTyz::Array{Float64,3}
    PVyBTxy::Array{Float64,3}
    PTzzBVz::Array{Float64,3}
    PTzzBVx::Array{Float64,3}
    PTzzBVy::Array{Float64,3}
    PTxxBVz::Array{Float64,3}
    PTxxBVx::Array{Float64,3}
    PTxxBVy::Array{Float64,3}
    PTyyBVz::Array{Float64,3}
    PTyyBVx::Array{Float64,3}
    PTyyBVy::Array{Float64,3}
    PTxzBVx::Array{Float64,3}
    PTxzBVz::Array{Float64,3}
    PTyzBVz::Array{Float64,3}
    PTyzBVy::Array{Float64,3}
    PTxyBVx::Array{Float64,3}
    PTxyBVy::Array{Float64,3}
end
#=== ascoustic 3d ===#
 type acbd3d
    bfull::Array{Float64,1}
    bhalf::Array{Float64,1}
    afull::Array{Float64,1}
    ahalf::Array{Float64,1}
    PVzBTpp::Array{Float64,3}
    PVxBTpp::Array{Float64,3}
    PVyBTpp::Array{Float64,3}
    PTppBVz::Array{Float64,3}
    PTppBVx::Array{Float64,3}
    PTppBVy::Array{Float64,3}
end

            #========================#
            #=== wavefield define ===#
            #========================#

#=== non split elastic wavefield 2d ===#
 type elwf2d
    vx :: Array{Float64,2}
    vz :: Array{Float64,2}
    txx :: Array{Float64,2}
    tzz :: Array{Float64,2}
    txz :: Array{Float64,2}
end

#=== non split acoustic wavefield 2d ===#
 type acwf2d
    vx :: Array{Float64,2}
    vz :: Array{Float64,2}
    tpp :: Array{Float64,2}
end

#=== non split elastic wavefield 3d ===#
 type elwf3d
  vx :: Array{Float64,3}
  vy :: Array{Float64,3}
  vz :: Array{Float64,3}
  txx :: Array{Float64,3}
  tyy :: Array{Float64,3}
  tzz :: Array{Float64,3}
  txz :: Array{Float64,3}
  tyz :: Array{Float64,3}
  txy :: Array{Float64,3}
end

#=== non split acoustic wavefield 3d ===#
 type acwf3d
  vx :: Array{Float64,3}
  vy :: Array{Float64,3}
  vz :: Array{Float64,3}
  tpp :: Array{Float64,3}
end

            #======================#
            #=== Wavefield Size ===#
            #======================#

#=== elastic 2d ===#
 type nelwf2d
  BDntpp :: Array{Int64,1}
  BDntxz :: Array{Int64,1}
  BDnvx :: Array{Int64,1}
  BDnvz :: Array{Int64,1}
  ntpp :: Array{Int64,1}
  ntxz :: Array{Int64,1}
  nvx :: Array{Int64,1}
  nvz :: Array{Int64,1}
end
#=== acoustic 2d ===#
 type nacwf2d
  BDntpp :: Array{Int64,1}
  BDnvx :: Array{Int64,1}
  BDnvz :: Array{Int64,1}
  ntpp :: Array{Int64,1}
  nvx :: Array{Int64,1}
  nvz :: Array{Int64,1}
end
#=== elastic 3d ===#
 type nelwf3d
    BDnvz :: Array{Int64,1}
    BDnvx :: Array{Int64,1}
    BDnvy :: Array{Int64,1}
    BDntpp :: Array{Int64,1}
    BDntxz :: Array{Int64,1}
    BDntxy :: Array{Int64,1}
    BDntyz :: Array{Int64,1}
    nvz :: Array{Int64,1}
    nvx :: Array{Int64,1}
    nvy :: Array{Int64,1}
    ntpp :: Array{Int64,1}
    ntxz :: Array{Int64,1}
    ntxy :: Array{Int64,1}
    ntyz :: Array{Int64,1}
end
#=== acoustic 3d ===#
 type nacwf3d
    BDnvz :: Array{Int64,1}
    BDnvx :: Array{Int64,1}
    BDnvy :: Array{Int64,1}
    BDntpp :: Array{Int64,1}
    nvz :: Array{Int64,1}
    nvx :: Array{Int64,1}
    nvy :: Array{Int64,1}
    ntpp :: Array{Int64,1}
end

            #========================#
            #=== Simulation Model ===#
            #========================#

#=== non split elastic 2d ===#
type elmod2d
    medium::elastic2d
    wf::elwf2d
    nwf::nelwf2d
    fdc::Array{Float64,1}
    pml::elbd2d
end

#=== non split acoustic 2d ===#
type acmod2d
    medium::acoustic2d
    wf::acwf2d
    nwf::nacwf2d
    fdc::Array{Float64,1}
    pml::acbd2d
end

#=== non split elastic 3d ===#
type elmod3d
    medium::elastic3d
    wf::elwf3d
    nwf::nelwf3d
    fdc::Array{Float64,1}
    pml::elbd3d
end

#=== non split acoustic 3d ===#
type acmod3d
    medium::acoustic3d
    wf::acwf3d
    nwf::nacwf3d
    fdc::Array{Float64,1}
    pml::acbd3d
end


text:
module backgroundbox_

using IUP

export
    backgroundbox

# example html/examples/C/IupBackgroundBox.c
function backgroundbox()
    IupOpen()
    # Creates a backgroundbox
    btn = IupBackgroundBox(IupVbox(IupButton("This button does nothing"), IupText(), C_NULL))

    # Creates dialog
    dlg = IupDialog(IupVbox(btn, C_NULL));

    IupSetAttributes(dlg, "MARGIN=10x10, GAP=10, TITLE = \"IupBackgroundBox Example\"");
    IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
    IupMainLoop();

    IupClose()
end

end     # module


text:
### Dataframes

"merge_columns! merges data from from two columns of a dataframe into the first given column and drops the latter column. It will throw an error if data is conflicting, or if preference is given it will pick the preference column."
function merge_columns!(dataframe, keep,drop ; preference = :none)
	@assert preference == :none || preference == keep || preference == drop
	dataframe[keep] = map(1:nrow(dataframe)) do i
		a=dataframe[i,keep]
		b=dataframe[i,drop]
		if (isna(a) && isna(b))
			NA
		elseif(isna(a))
			b
		elseif(isna(b))
			a
		elseif a==b
			a
		elseif preference == :none
			throw(error(string("Cannot merge conflicting values: ", a, " with ", b)))
		else
			dataframe[i,preference]
		end
	end
	delete!(dataframe, drop)
end

"indexer returns a function (closure with a dict) that returns the row-index of the element in the dataframe in constant time"
function indexer(df::DataFrames.DataFrame, column::Symbol)
	d=Dict(map(x->(df[x,column],x), 1:nrow(df)))
	column_indexer_function(i) = if i in keys(d) d[i] else NA end
end


text:
module TestBasic

using Test
using UrlDownload
using DataFrames

@testset "Standard CSVs" begin
    df = DataFrame(x = [1, 3], y = [2, 4])
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/ext.csv"
    res = urldownload(url) |> DataFrame
    @test df == res

    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/ext.tsv"
    res = urldownload(url) |> DataFrame
    @test df == res

    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/noextcsv"
    res = urldownload(url, format = :CSV) |> DataFrame
    @test df == res

    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/noexttsv"
    res = urldownload(url, format = :TSV) |> DataFrame
    @test df == res
end

@testset "Force format CSVs" begin
    df = DataFrame(x = [1, 3], y = [2, 4])
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/semicolon.csv"
    res = urldownload(url) |> DataFrame
    @test df == res

    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/semicolonnoextcsv"
    res = urldownload(url, format = :CSV) |> DataFrame
    @test df == res
end

@testset "Pics" begin
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/test.jpg"
    res = urldownload(url)

    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/test.png"
    res = urldownload(url)
end

@testset "Feather" begin
    df = DataFrame(x = [1, 3], y = [2, 4])
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/test.feather"
    res = urldownload(url) |> DataFrame

    @test df == res
end

@testset "Json" begin
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/test.json"
    res = urldownload(url)
end

@testset "Progress meter" begin
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/ext.csv"
    res = urldownload(url, true)
end

@testset "Keyword arguments" begin
    df = DataFrame(x = [1, 3], y = [2, 4])
    url = "https://raw.githubusercontent.com/Arkoniak/UrlDownload.jl/master/data/semicolon.csv"
    res = urldownload(url, delim = ';') |> DataFrame
    @test res == df
end

end # module


text:
using PlutoGrid
using Test
using HypertextLiteral
using DataFrames

@testset "PlutoGrid.jl" begin
    df = DataFrame(x=1:10, y=10:-1:1)
	@test readonly_table(df) isa HypertextLiteral.Result

    nt = [(a=1, b=0.5, c="hello"), (a=2, b=0.9, c="world"), (a=3, b=5.5, c="!")]
    @test readonly_table(nt) isa HypertextLiteral.Result

    @test editable_table(nt) isa HypertextLiteral.Result
    @test editable_table(df, ["x"]) isa HypertextLiteral.Result
    @test_throws ErrorException editable_table(df, ["zzz"])

    @test create_dataframe(nothing) == DataFrame()
    @test create_dataframe(Dict(:a => 1, :b => 2)) == DataFrame(a=1, b=2)
    @test create_dataframe([Dict(:a => 1, :b => 2), Dict(:a => 3, :b => 4)]) == DataFrame(a=[1, 3], b=[2, 4])

end


text:
module ServoSystemSimulation

println("\nServoSystem: Demonstrating the ability to simulate hierarchical mixed domain models")

include("../models/Blocks.jl")
include("../models/Electric.jl")
include("../models/Rotational.jl")

using TinyModia
using DifferentialEquations
@usingModiaPlot

setLogMerge(false)

Gear = Model(
    flange_a = Flange,
    flange_b = Flange,
    gear     = IdealGear_withSupport | Map(ratio = 105.0),
    fixed    = Fixed,
    spring   = Spring | Map(c=5.84e5u"N*m/rad"),
    damper1  = Damper | Map(d=500.0u"N*m*s/rad"),
    damper2  = Damper | Map(d=100.0u"N*m*s/rad"),

    connect = :[
        (flange_a     , gear.flange_a)
        (fixed.flange , gear.support, damper2.flange_b)
        (gear.flange_b, damper2.flange_a, spring.flange_a, damper1.flange_a)
        (flange_b     , spring.flange_b, damper1.flange_b)]
)

ControlledMotor = Model(
  refCurrent = input,
  flange = Flange,
  feedback      = Feedback,
  PI            = PI | Map(k=30, T=1.0u"s"),
  firstOrder    = FirstOrder | Map(k=1.0, T=0.001u"s"),
  signalVoltage = UnitlessSignalVoltage,
  resistor      = Resistor | Map(R=13.8u"Ω"),
  inductor      = Inductor | Map(L=0.061u"H"),
  emf           = EMF | Map(k=1.016u"N*m/A"),
  ground        = Ground,
  currentSensor = UnitlessCurrentSensor,
  motorInertia  = Inertia | Map(J=0.0025u"kg*m^2"),

  connect = :[
    (refCurrent, feedback.u1)
    (feedback.y, PI.u)
    (PI.y, firstOrder.u)
    (firstOrder.y, signalVoltage.v)

    (signalVoltage.p, resistor.p)
    (resistor.n, inductor.p)
    (inductor.n, emf.p)
    (emf.n, ground.p, currentSensor.p)
    (currentSensor.n, signalVoltage.n)
    (currentSensor.i, feedback.u2)

    (emf.flange, motorInertia.flange_a)
    (motorInertia.flange_b, flange) ]
)

SpeedController = Model(
    refSpeed = input,
    motorSpeed = input,
    refCurrent = output,
    gain     = Gain | Map(k=105.0),
    PI       = PI | Map(T=1.0u"s", k=1.0),
    feedback = Feedback,

    connect = :[
        (refSpeed  , gain.u)
        (gain.y    , feedback.u1)
        (motorSpeed, feedback.u2)
        (feedback.y, PI.u)
        (PI.y      , refCurrent)]
)

Servo = Model(
    refSpeed        = input,
    flange_b        = Flange,
    speedController = SpeedController | Map(ks=1.0, Ts=1.0u"s", ratio=105.0),
    motor           = ControlledMotor | Map(km=30.0, Tm=0.005u"s"),
    gear            = Gear | Map(ratio=105.0),
    speedSensor1    = UnitlessSpeedSensor,
    speedSensor2    = UnitlessSpeedSensor,
    speedError      = Feedback,

    connect = :[
        (refSpeed                  , speedController.refSpeed, speedError.u1)
        (speedController.refCurrent, motor.refCurrent)
        (motor.flange              , gear.flange_a, speedSensor1.flange)
        (speedSensor1.w            , speedController.motorSpeed)
        (gear.flange_b             , flange_b, speedSensor2.flange)
        (speedSensor2.w            , speedError.u2)]
)

TestServo = Model(
    ks    = 0.8,
    Ts    = 0.08u"s",
    ramp  = Ramp  | Map(duration=1.18u"s", height=2.95),
    servo = Servo | Map(ks=:(up.ks), Ts=:(up.Ts)),
    load  = Inertia | Map(J=170u"kg*m^2"),
    equations =:[load.flange_b.tau = 0u"N*m"],
    connect = :[
        (ramp.y        , servo.refSpeed)
        (servo.flange_b, load.flange_a) ]
)

plotVariables = [("ramp.y", "load.w")         "servo.speedError.y";
                 "servo.gear.spring.phi_rel"  "servo.motor.currentSensor.i"]

testServo1 = @instantiateModel(TestServo)
println("Simulate")
@time simulate!(testServo1, Tsit5(), stopTime=2.0, tolerance=1e-6, requiredFinalStates =
    [7.320842067204029, 9.346410309487013, 355.30389168655955, 2.792544498835712, 429.42665751348284, 311.7812493890421, 4.089776248793499, 2.969353608933471])
plot(testServo1, plotVariables, figure=1)

println("\nServo with uncertainties")
using Measurements
TestServoWithUncertainties = TestServo | Map(
    load  = Map(J=(110.0 ± 20)u"kg*m^2"),
    servo = Map(motor = Map(resistor = Map(R=(13.8±1.0)u"Ω"))))
testServo2 = @instantiateModel(TestServoWithUncertainties , FloatType = Measurement{Float64})
@time simulate!(testServo2, Tsit5(), stopTime=2.0, tolerance=1e-6)
plot(testServo2, plotVariables, figure=2)

println("\nServo with Monte Carlo simulation")
import MonteCarloMeasurements
using Distributions
const nparticles = 100
uniform(vmin,vmax) = MonteCarloMeasurements.StaticParticles(nparticles,Distributions.Uniform(vmin,vmax))

TestServoWithMonteCarlo = TestServo | Map(
     load  = Map(J = uniform(50,170)),
     servo = Map(motor = Map(resistor = Map(R = uniform(12.8,14.8)))))
testServo3 = @instantiateModel(TestServoWithMonteCarlo, FloatType = MonteCarloMeasurements.StaticParticles{Float64,nparticles}, unitless=true)
@time simulate!(testServo3, Tsit5(), stopTime=2.0, tolerance=1e-6)
plot(testServo3, plotVariables, figure=3)

end


text:
module FileManager

using FileIO
#using SignalManager
using Reactive

export FileSource
export FileSourcePart
export splitFileName
export fileGetContents
export readFileSource
export watchFileUpdate
export registerFileUpdate
export runOnFileUpdate

type FileSource
  path  ::String
	cache ::String

  FileSource(path="") = new(path,"")
end

type FileSourcePart
	source ::FileSource
  range  ::UnitRange{Int64}
	cache ::String

	FileSourcePart(source::FileSource,range=range(1,stat(source.path).size)) = new(source,range,"")
end

type FileUpdateEntry
  source::FileSource
  OnUpdate::Function
	args::Tuple
	editedTime::Float64

  FileUpdateEntry(source::FileSource, OnUpdate::Function, args) = new(source, OnUpdate, args, 0)
end

function getCache(source::FileSource)
	if source.cache == ""
		reload(source)
	end
	source.cache
end

reload(source::FileSource) = (source.cache=fileGetContents(source.path))

FILES_TO_UPDATE = Dict{String, FileUpdateEntry}()

readFileSource(path::String) = FileSource(path,fileGetContents(path))

function splitFileName(path::String)
  (dir, filename) = splitdir(path) #splitdrive
  (name, ext) = splitext(filename)
  (addDirSlash(dir),name,ext)
end

function waitForFileReady(path::String, func::Function, tryCount=100, tryWait=0.1)
	result=false
	for i = 1:tryCount

		#try reading file
		if stat(path).size > 0
			open(path) do file
				 result=func(file)
			end
			if result break end
		end

		Libc.systemsleep(tryWait) #wait
	end
	result
end

function fileGetContents(path::String, tryCount=100, tryWait=0.1)
	content=nothing
	waitForFileReady(path,(x)->(content=readstring(x); content != nothing),tryCount,tryWait)
	content
end

addDirSlash(path::String) = string(path,(path != "" && path[length(path)] != '/') ? "/" : "")

registerFileUpdate(source::FileSource, OnUpdate::Function, args...) = FILES_TO_UPDATE[source.path]=FileUpdateEntry(source,OnUpdate,(source, args...))
unregisterFileUpdate(source::FileSource) = delete!(FILES_TO_UPDATE, source.path)

runOnFileUpdate(threshold=0.01) = for (key,entry) in FILES_TO_UPDATE
	time_edited = mtime(entry.source.path)
	edited = time_edited - entry.editedTime
	#if edited!=0
	#println(entry.source.path,": ",time_edited)
	#end
	if edited > threshold  #!isapprox(0.0, entry.editedTime - time_edited)
		#println("Time: ", entry.editedTime, " == ", time_edited)
		entry.OnUpdate(entry.args...)
		entry.editedTime=time_edited
	end
end

#reads from the file and updates the source whenever the file gets edited
function watchFileUpdate(path::String, OnUpdate::Function)
  preserve(map( isUpdated(query(path)) ) do _unused OnUpdate(path) end)
  #map( isUpdated(query(path))) do _unused OnUpdate(path) end
end

watchFileUpdate(path::FileSource, OnUpdate::Function) = watchFileUpdate(o.path, OnUpdate)

function isUpdated(file::File, tickrate=0.001)
  fn = filename(file)
  ticks=fpswhen(Signal(true), 1.0/tickrate)
  file_edited=foldp((false, mtime(fn)), ticks) do v0, v1
    time_edited = mtime(fn)
    (!isapprox(0.0, v0[2] - time_edited), time_edited)
  end
  filter(identity, true, map(first, file_edited))
end

end # FileManager


text:
module HarmonicTrapTrajectories
export Trajectory, LinearRamp, MinimumJerk, LewisRiesenfeld, trajectory;

"""
    abstract type Trajectory

Transport trajectories in harmonic traps.

Subtypes will contain at least the following parameters:
* `t` - total motional time for the trajectory.
* `d` - total distance moved during the motion.

See also: [`LinearRamp`](@ref), [`MinimumJerk`](@ref), [`LewisRiesenfeld`](@ref)
"""
abstract type Trajectory end;

"""
    LinearRamp(t::Number, d::Number) <: Trajectory

Simple linear trajectory where particle moves at constant speed between start
and end points.

See also: [`Trajectory`](@ref), [`trajectory`](@ref)
"""
struct LinearRamp <: Trajectory
  t::Number;
  d::Number;
end

"""
    MinimumJerk(t::Number, d::Number) <: Trajectory

Trajectory minimising jerk throughout the motion.

See also: [`Trajectory`](@ref), [`trajectory`](@ref)
"""
struct MinimumJerk <: Trajectory
  t::Number;
  d::Number;
end

"""
    LewisRiesenfeld{T}(t::Number, d::Number, classical::T) where T <: Trajectory

Use Lewis-Riesenfeld invariants reverse engineering method to get optimum
trajectory based on the `classical` trajectory.

Refer to E. Torrontegui et al. Phys. Rev. A 83, 013415, 2011 (doi:
10.1103/PhysRevA.83.013415) for details.

See also: [`Trajectory`](@ref), [`trajectory`](@ref)
"""
struct LewisRiesenfeld{T<:Trajectory} <: Trajectory
  t::Number;
  d::Number;

  "Classical trajectory."
  classical::T;

  function LewisRiesenfeld{T}(t::Number, d::Number, classical::T) where T <: Trajectory
    if t !== classical.t || d !== classical.d
      throw(ArgumentError("classical trajectory must have the same `t` and `d` parameters."));
    end

    new(t, d, classical);
  end
end

"""
    LewisRiesenfeld{T}(t::Number, d::Number) where T<:Trajectory

Implicitly construct classical trajectory object from the given type.
"""
LewisRiesenfeld{T}(t::Number, d::Number) where T<:Trajectory = LewisRiesenfeld{T}(t, d, T(t, d));

"""
    trajectory(trajectory::T, t::Number, start::Number, params::Vararg) where T<:Trajectory

Return the location at time `t` for a particle on `trajectory` starting at a
position given by `start`.

Additional parameters required by the `fractional_trajectory()` corresponding to
the relevant `trajectory` type should be passed as additional parameters.

See also: [`fractional_trajectory`](@ref)
"""
function trajectory(trajectory::T, t::Number, start::Number, params::Vararg) where T<:Trajectory
  return start + trajectory.d * fractional_trajectory(trajectory, t, params...);
end

"""
    fractional_trajectory(trajectory::T, t::Number) where T <: Trajectory
    fractional_trajectory(trajectory::LinearRamp, t::Number)
    fractional_trajectory(trajectory::MinimumJerk, t::Number)

Positions for particles on `trajectory` at the time `t` as a fraction of the
total distance which should be travelled on the trajectory.
"""
fractional_trajectory(trajectory::LinearRamp, t::Number) = t/trajectory.t;

function fractional_trajectory(trajectory::MinimumJerk, t::Number)
  s = t / trajectory.t;
  return 10s^3 - 15s^4 + 6s^5;
end

"""
    fractional_trajectory(trajectory::LewisRiesenfeld{MinimumJerk}, t::Number, ω::Number)

Implementation for use of method of reverse engineered Lewis-Riesenfeld invariants where the
classical trajectory is the `MinimumJerk` trajectory and the harmonic trap frequency is `ω`.
"""
function fractional_trajectory(trajectory::LewisRiesenfeld{MinimumJerk}, t::Number, ω::Number)
  s = t / trajectory.t;
  return (60s - 180s^2 + 120s^3) / (trajectory.t * ω)^2 + fractional_trajectory(trajectory.classical, t);
end
end # module


text:
VERSION >= v"0.4.0-dev+6521" && __precompile__()

module ControlToolbox

using PyCall
using Polynomials
using OrdinaryDiffEq

import Base: +, -, *, /
import Base: display, print, step, show

export StateSpace
export TransferFunction
export change_lsim_tolerances

################################################################################
#                                    Types
################################################################################

#                                 State-Space
# ==============================================================================

"""
Space state definition.
"""
immutable StateSpace
    A::Array
    B::Array
    C::Array
    D::Array

    """
    ### function StateSpace(A::Matrix, B::Matrix, C::Matrix, D::Matrix)

    Initialize a state space with the following form:

    x = Ax + Bu
    y = Cx + Du

    ##### Args

    * A: Matrix A.
    * B: Matrix B.
    * C: Matrix C.
    * D: Matrix D.

    ##### Returns

    The new state space.

    """

    function StateSpace(A::Array, B::Array, C::Array, D::Array)

        # Check if the dimensions are consistent.
        #
        # dx/dt = Ax + Bu
        #     y = Cx + Du
        #

        num_states = size(A,1)
        num_measurements = size(C,1)
        num_inputs = size(B,2)

        ( isempty(B) ) && (B = zeros(num_states); num_inputs = 1)
        ( isempty(D) ) && (D = zeros(num_measurements, num_inputs) )

        ( num_states != size(A,2) ) &&
        error("Matrix A must be square.")

        ( num_states != size(B,1) ) &&
        error("Inconsistent dimensions between matrices A and B.")

        ( num_states != size(C,2) ) &&
        error("Inconsistent dimensions between matrices A and C.")

        ( num_measurements != size(D,1) ) &&
        error("Inconsistent dimensions between matrices C and D.")

        ( (num_inputs != size(D,2)) ) &&
        error("Inconsistent dimensions between matrices B and D.")

        new(A,B,C,D)
    end
end

#                              Transfer Function
# ==============================================================================

"""
Transfer function definition.
"""

immutable TransferFunction
    num::Poly
    den::Poly

    """
    ### function TransferFunction(num::Poly, den::Poly)

    Initialize a new transfer function.

    ##### Args

    * num: Numerator (see Poly).
    * den: Denominator (see Poly).

    ##### Returns

    The new transfer function.

    """

    function TransferFunction(num::Poly, den::Poly)
        ( ( length(num.a) == 0 ) || ( length(den.a) == 0 ) ) &&
        error("Numerator or denominator cannot be empty.")

        if (num.a[end] == 0)

            # Check if the numerator is 0.
            if length(num.a) == 1
                new(Poly(0), den)
            else
                TransferFunction(Poly(num.a[1:end-1]), den)
            end
        elseif (den.a[end] == 0)
            TransferFunction(num, Poly(den.a[1:end-1]))
        else
            new(num, den)
        end
    end
end

################################################################################
#                                   Includes
################################################################################

include("aux.jl")

include("analysis/bode.jl")
include("analysis/gain_poles_zeros.jl")
include("analysis/margin.jl")
include("analysis/minreal.jl")
include("analysis/nyquist.jl")
include("analysis/pole_info.jl")
include("analysis/rlocus.jl")
include("simulation/impulse.jl")
include("simulation/lsim.jl")
include("simulation/ramp.jl")
include("simulation/step.jl")
include("plots/plot.jl")
include("types/ss.jl")
include("types/tf.jl")

end # module


text:
using Base.Test

@testset "nets/" begin
    include("nets/sequential.jl")
end


text: println(sum(1:100) ^ 2 - sum(map(x -> x^2, 1:100)))

max_stars_repo_name: sujimodern/project-euler-jl

max_issues_repo_name: sujimodern/project-euler-jl

max_forks_repo_name: sujimodern/project-euler-jl