text:
import SynapseClient: createentity, downloadentity, getentity, updateentity,
                      local_state
version_check = SynapseClient.synapseclient.version_check.version_check # TODO: put in public API???
SystemExit = pybuiltin(:SystemExit)

# def test_login():
#     try:
#         # Test that we fail gracefully with wrong user
#         assert_raises(SynapseAuthenticationError, syn.login, 'asdf', 'notarealpassword')

#         config = configparser.ConfigParser()
#         config.read(client.CONFIG_FILE)
#         username = config.get('authentication', 'username')
#         password = config.get('authentication', 'password')
#         sessionToken = syn._getSessionToken(username, password)

#         # Simple login with ID + PW
#         syn.login(username, password, silent=True)

#         # Login with ID + API key
#         syn.login(email=username, apiKey=base64.b64encode(syn.apiKey), silent=True)
#         syn.logout(forgetMe=True)

#         # Config file is read-only for the client, so it must be mocked!
#         if (sys.version < '3'):
#             configparser_package_name = 'ConfigParser'
#         else:
#             configparser_package_name = 'configparser'
#         with patch("%s.ConfigParser.has_option" % configparser_package_name) as config_has_mock, patch("synapseclient.Synapse._readSessionCache") as read_session_mock:

#             config_has_mock.return_value = False
#             read_session_mock.return_value = {}

#             # Login with given bad session token,
#             # It should REST PUT the token and fail
#             # Then keep going and, due to mocking, fail to read any credentials
#             assert_raises(SynapseAuthenticationError, syn.login, sessionToken="Wheeeeeeee")
#             assert config_has_mock.called

#             # Login with no credentials
#             assert_raises(SynapseAuthenticationError, syn.login)

#             config_has_mock.reset_mock()
#             config_has_mock.side_effect = lambda section, option: section == "authentication" and option == "sessiontoken"
#             with patch("%s.ConfigParser.get" % configparser_package_name) as config_get_mock:

#                 # Login with a session token from the config file
#                 config_get_mock.return_value = sessionToken
#                 syn.login(silent=True)

#                 # Login with a bad session token from the config file
#                 config_get_mock.return_value = "derp-dee-derp"
#                 assert_raises(SynapseAuthenticationError, syn.login)

#         # Login with session token
#         syn.login(sessionToken=sessionToken, rememberMe=True, silent=True)

#         # Login as the most recent user
#         with patch('synapseclient.Synapse._readSessionCache') as read_session_mock:
#             dict_mock = MagicMock()
#             read_session_mock.return_value = dict_mock
#             dict_mock.__contains__.side_effect = lambda x: x == '<mostRecent>'
#             dict_mock.__getitem__.return_value = syn.username
#             syn.login(silent=True)
#             dict_mock.__getitem__.assert_called_once_with('<mostRecent>')

#         # Login with ID only
#         syn.login(username, silent=True)
#         syn.logout(forgetMe=True)
#     except configparser.Error:
#         print("To fully test the login method, please supply a username and password in the configuration file")

#     finally:
#         # Login with config file
#         syn.login(rememberMe=True, silent=True)

# def testCustomConfigFile():
#     if os.path.isfile(client.CONFIG_FILE):
#         configPath='./CONFIGFILE'
#         shutil.copyfile(client.CONFIG_FILE, configPath)
#         schedule_for_cleanup(configPath)

#         syn2 = synapseclient.Synapse(configPath=configPath)
#         syn2.login()
#     else:
#         print("To fully test the login method a configuration file is required")

println(project)
@testset "entity_version" begin
    # Make an Entity and make sure the version is one
    entity = File(parent=project.id)
    entity.path = utils.make_bogus_data_file()
    schedule_for_cleanup(entity.path)
    entity = createentity(syn, entity)

    setannotations(syn, entity, Dict("fizzbuzz"=>111222))
    entity = getentity(syn, entity)
    @test entity.versionNumber == 1

    # Update the Entity and make sure the version is incremented
    entity.foo = 998877
    entity.name = "foobarbat"
    entity.description = "This is a test entity..."
    entity = updateentity(syn, entity, incrementVersion=true, versionLabel="Prada remix")
    @test entity.versionNumber == 2

    # Get the older data and verify the random stuff is still there
    annotations = getannotations(syn, entity, version=1)
    @test annotations["fizzbuzz"][1] == 111222
    returnEntity = getentity(syn, entity, version=1)
    @test returnEntity.versionNumber == 1
    @test returnEntity.fizzbuzz[1] == 111222
    @test hasattr(returnEntity,"foo") == false

    # Try the newer Entity
    returnEntity = getentity(syn, entity)
    @test returnEntity.versionNumber == 2
    @test returnEntity.foo[1] == 998877
    @test returnEntity.name == "foobarbat"
    @test returnEntity.description == "This is a test entity..."
    @test returnEntity.versionLabel == "Prada remix"

    # Try the older Entity again
    returnEntity = downloadentity(syn, entity, version=1)
    @test returnEntity.versionNumber == 1
    @test returnEntity.fizzbuzz[1] == 111222
    @test hasattr(returnEntity,"foo") == false

    # Delete version 2
    delete(syn, entity, version=2)
    returnEntity = getentity(syn, entity)
    @test returnEntity.versionNumber == 1
end
@testset "md5_query" begin
    # Add the same Entity several times
    path = utils.make_bogus_data_file()
    schedule_for_cleanup(path)
    repeated = File(path, parent=project.id, description="Same data over and over again")

    # Retrieve the data via MD5
    num = 5
    stored = String[]
    for i = 1:num
        repeated.name = "Repeated data $i.dat"
        push!(stored, store(syn,repeated).id)
    end
    # Although we expect num results, it is possible for the MD5 to be non-unique
    results = md5query(syn, utils.md5_for_file(path).hexdigest())
    @test string(sort([res["id"] for res in results])) == string(sort(stored))
    @test length(results) == num
end

@testset "uploadFile_given_dictionary" begin
    # Make a Folder Entity the old fashioned way
    folder = Dict("concreteType"=> SynapseClient.synapseclient.Folder._synapse_entity_type,
            "parentId"  => project.id,
            "name"      => "fooDictionary",
            "foo"       => 334455)
    entity = store(syn, folder)

    # Download and verify that it is the same file
    entity = get(syn, entity)
    @test entity.parentId == project.id
    @test entity.foo[1] == 334455

    # Update via a dictionary
    path = utils.make_bogus_data_file()
    schedule_for_cleanup(path)
    rareCase = Dict()
    # rareCase.update(entity.annotations)
    # rareCase.update(entity.properties)
    # rareCase.update(local_state(entity))
    merge!(rareCase,entity.annotations)
    merge!(rareCase,entity.properties)
    merge!(rareCase,local_state(entity))
    rareCase["description"] = "Updating with a plain dictionary should be rare."

    # Verify it works
    entity = store(syn, rareCase)
    @test entity.description == rareCase["description"]
    @test entity.name == "fooDictionary"
    entity = get(syn, entity.id)
end

# @testset "uploadFileEntity" begin
#     # Create a FileEntity
#     # Dictionaries default to FileEntity as a type
#     fname = utils.make_bogus_data_file()
#     schedule_for_cleanup(fname)
#     entity = Dict("name"        => "fooUploadFileEntity",
#                   "description" => "A test file entity",
#                   "parentId"    => project.id)
#     entity = uploadfile(syn, entity, fname)

#     # Download and verify
#     entity = downloadentity(syn, entity)

#     print(entity.files)
#     @test entity.files[1] == splitdir(fname)[2]
#     # @test filecmp.cmp(fname, entity.path)

#     # Check if we upload the wrong type of file handle
#     fh = restget(syn, "/entity/$(entity.id)/filehandles")["list"][1]
#     @test fh["concreteType"] == "org.sagebionetworks.repo.model.file.S3FileHandle"

#     # Create a different temporary file
#     fname = utils.make_bogus_data_file()
#     schedule_for_cleanup(fname)

#     # Update existing FileEntity
#     entity = uploadfile(syn, entity, fname)

#     # Download and verify that it is the same file
#     entity = downloadentity(syn, entity)
#     print(entity.files)
#     @test entity.files[1] == splitdir(fname)[2]
#     # @test filecmp.cmp(fname, entity.path)
# end

@testset "test_downloadFile" begin
    # See if the a "wget" works
    filename = utils.download_file("http://dev-versions.synapse.sagebase.org/sage_bionetworks_logo_274x128.png")
    schedule_for_cleanup(filename)
    @test isfile(filename) == true
end

@testset "test_version_check" begin
    # Check current version against dev-synapsePythonClient version file
    version_check(version_url="http://dev-versions.synapse.sagebase.org/synapsePythonClient")

    # Should be higher than current version and return true
    @test version_check(current_version="999.999.999", version_url="http://dev-versions.synapse.sagebase.org/synapsePythonClient") == true

    # Test out of date version
    @test version_check(current_version="0.0.1", version_url="http://dev-versions.synapse.sagebase.org/synapsePythonClient") == false

    # Test blacklisted version
    # @test_pythrows SystemExit version_check(current_version="0.0.0", version_url="http://dev-versions.synapse.sagebase.org/ynapsePythonClient")

    # Test bad URL
    @test version_check(current_version="999.999.999", version_url="http://dev-versions.synapse.sagebase.org/bad_filename_doesnt_exist") == false
end

# @testset "provenance" begin
#     # Create a File Entity
#     fname = utils.make_bogus_data_file()
#     schedule_for_cleanup(fname)
#     data_entity = store(syn, File(fname, parent=project.id))

#     # Create a File Entity of Code
#     path = splitext(tempname())[1] * ".py"
#     open(path, "w") do f
#         write(f, utils.normalize_lines("""
#             ## Chris's fabulous random data generator
#             ############################################################
#             import random
#             random.seed(12345)
#             data = [random.gauss(mu=0.0, sigma=1.0) for i in range(100)]
#             """))
#     end
#     schedule_for_cleanup(path)
#     code_entity = store(syn, File(path, parent=project.id))
#     # Create a new Activity asserting that the Code Entity was "used"
#     activity = Activity(name="random.gauss", description="Generate some random numbers")
#     used(activity, code_entity, wasExecuted=true)
#     used(activity, Dict("name"=>"Superhack", "url"=>"https://github.com/joe_coder/Superhack"), wasExecuted=true)
#     activity = setprovenance(syn, data_entity, activity)

#     # Retrieve and verify the saved Provenance record
#     retrieved_activity = getprovenance(syn, data_entity)
#     @test retrieved_activity == activity

#     # Test Activity update
#     new_description = "Generate random numbers like a gangsta"
#     retrieved_activity["description"] = new_description
#     updated_activity = updateactivity(syn, retrieved_activity)
#     @test updated_activity["name"] == retrieved_activity["name"]
#     @test updated_activity["description"] == new_description

#     # Test delete
#     deleteprovenance(syn, data_entity)
#     @test_pythrows SynapseHTTPError getprovenance(syn,  data_entity.id)
# end

@testset "annotations" begin
    # Get the annotations of an Entity
    entity = store(syn, Folder(parent=project.id))
    anno = getannotations(syn, entity)
    @test hasattr(anno, "id")
    @test hasattr(anno, "etag")
    @test anno.id == entity.id
    @test anno.etag == entity.etag

    # Set the annotations, with keywords too
    anno["bogosity"] = "total"
    setannotations(syn, entity, anno, wazoo="Frank", label="Barking Pumpkin", shark=16776960)

    # Check the update
    annote = getannotations(syn, entity)
    @test annote["bogosity"] == ["total"]
    @test annote["wazoo"] == ["Frank"]
    @test annote["label"] == ["Barking Pumpkin"]
    @test annote["shark"] == [16776960]

    # More annotation setting
    # annote["primes"] = [2,3,5,7,11,13,17,19,23,29] # NB: PyCall converts this to array (not list) and synapseclient stores it as an array of strings.
    annote["phat_numbers"] = [1234.5678, 8888.3333, 1212.3434, 6677.8899]
    annote["goobers"] = ["chris", "jen", "jane"]
    annote["present_time"] = now()
    setannotations(syn, entity, annote)

    # Check it again
    annotation = getannotations(syn, entity)
    # @test annotation["primes"] == [2,3,5,7,11,13,17,19,23,29] # NB: Disabled because of type problems (see above)
    @test annotation["phat_numbers"] == [1234.5678, 8888.3333, 1212.3434, 6677.8899]
    @test annotation["goobers"] == ["chris", "jen", "jane"]
    # @test annotation["present_time"][1].strftime("%Y-%m-%d %H:%M:%S") == annote["present_time"].strftime("%Y-%m-%d %H:%M:%S")
    @test string(annotation["present_time"][1]) == string(annote["present_time"])
end
@testset "get_user_profile" begin
    p1 = getuserprofile(syn)

    ## get by name
    p2 = getuserprofile(syn, p1["userName"])
    @test p2["userName"] == p1["userName"]

    ## get by user ID
    p2 = getuserprofile(syn, p1["ownerId"])
    @test p2["userName"] == p1["userName"]
end

# @testset "teams" begin
#     unique_name = "Team Gnarly Rad " * string(UUIDs.uuid4())
#     team = Team(name=unique_name, description="A gnarly rad team", canPublicJoin=true)
#     team = store(syn, team)

#     team2 = getteam(syn, team["id"])
#     @test team == team2

#     ## Asynchronously populates index, so wait 'til it's there
#     retry = 0
#     backoff = 0.1
#     while retry < 5
#         retry += 1
#         sleep(backoff)
#         backoff *= 2
#         found_teams = collect(_findteam(syn, team.name))
#         if length(found_teams) > 0
#             break
#         end
#     end
#     @test team == found_teams[1]
#     delete(syn, team)
# end


text:
module SampleArrays
# inspired by SampledSignals
using Reexport
using FileIO
using DataStructures
using DSP
import DSP: unwrap, unwrap!, resample, finddelay, shiftsignal, shiftsignal!
using FFTW
import FFTW: rfft, irfft
using Interpolations
@reexport using IntervalSets
using Unitful
using Unitful: ns, ms, µs, s, Hz, kHz, MHz, GHz, THz

include("units.jl")

include("magphase.jl")

include("abstracttypes.jl")

include("samplearray.jl")

include("rfftspectrumarray.jl")

include("spectrumarray.jl")

include("impexp.jl")

# ----- FFT -----------------------------
FFTW.rfft(x::SampleArray) = RFFTSpectrumArray(FFTW.rfft(data(x), 1), rate(x), nframes(x), names(x))
FFTW.irfft(X::RFFTSpectrumArray{<:Complex}, d::Int) = SampleArray(FFTW.irfft(data(X), d, 1), rate(X), names(X))
FFTW.irfft(X::RFFTSpectrumArray{MagPhase{E}}, d::Int) where E = SampleArray(FFTW.irfft(Complex{E}.(data(X)), d, 1), rate(X), names(X))
FFTW.irfft(X::RFFTSpectrumArray) = irfft(X, ntimeframes(X))

# ----- UTILS ---------------------------
function DSP.unwrap!(Y::AbstractArray{T}, X::AbstractArray{T}; range=2E(pi), kwargs...) where {E, T <: MagPhase{E}}
    mags = abs.(X)
    phis = unwrap(angle.(X); dims=1, kwargs...)
    Y .= (MagPhase(m, p) for (m, p) in zip(mags, phis))
    Y
end

DSP.unwrap!(Y::AbstractArray{T2}, X::AbstractArray{T}; range=2E(pi), kwargs...) where {E, T <: Complex{E}, T2 <: MagPhase{E}} =
    unwrap!(Y, MagPhase.(X); dims=1, kwargs...)

DSP.unwrap!(Y::AbstractArray{T}, X::AbstractArray{T}; range=2E(pi), kwargs...) where {E, T <: Complex{E}} =
    throw(ArgumentError("unwrapping in complex plane!"))

DSP.unwrap(X::AbstractSpectrumArray; kwargs...) = invoke(unwrap, Tuple{AbstractArray}, X; dims=1, kwargs...)
DSP.unwrap(X::AbstractSpectrumArray{<: Complex}; kwargs...) = unwrap(MagPhase.(X))
DSP.unwrap!(X::AbstractSpectrumArray; kwargs...) = invoke(unwrap!, Tuple{AbstractArray}, X; dims=1, kwargs...)

function DSP.finddelay(x::SampleArray, y::SampleArray)
    @assert nchannels(x) == 1
    @assert nchannels(y) == 1
    finddelay(data(x)[:, 1], data(y)[:, 1])
end

function DSP.shiftsignal!(x::SampleArray, s::Integer)
    l = nframes(x)
    if abs(s) > l
        throw(ArgumentError("The absolute value of s must not be greater than the number of frames of x"))
    end
    if s > 0
        x[s + 1:l, :] = x[1:l - s, :]
        x[1:s, :] .= 0
    elseif s < 0
        x[1:l + s, :] = x[1 - s:l, :]
        x[l + s + 1:l, :] .= 0
    end
    x
end

DSP.shiftsignal(x::SampleArray, s::Integer) = shiftsignal!(copy(x), s)

end

ext: jl


text:
module Shapes # like your Geometry module

export Sphere, Lens, Retina

using Vector3D

abstract Sphere

type Lens <: Sphere # the lens type
    c::Vec # center
    r::Float64 # radius
    ri::Function # the Refractive Index Gradient (RIG) that describes the distribution of refractive indices inside the lens. We restrict ourselves to spherically symmetrical distributions: so at a set distance form the lens center, the refractive index is equal in all directions (kind of like an onion?).
    ε::Float64 # the step
end
Lens(a::Vector{Float64},b::Float64,c::Function,d::Float64) = Lens(Vec(a),b,c,d) # this is a constructor function

type Retina <: Sphere # the retina type
    c::Vec # retina center
    r::Float64 # radius
end
Retina(a::Vector{Float64},b::Float64) = Retina(Vec(a),b)

end

max_stars_repo_name: JuliaTagBot/borat

max_issues_repo_name: JuliaTagBot/borat

max_forks_repo_name: JuliaTagBot/borat


text:
" Rock-salt lattice "
rock_salt(T::Type=Float64; unit=u"nm") = Crystal(
    eye(T, 3) * unit,
    position=T[0.5, 0.5, 0.5] * unit,
    position=T[0, 0, 0] * unit,
    species=['A', 'B']
)

" Zinc-blende lattice "
zinc_blende(T::Type=Float64; unit=u"nm") = Crystal(
    T[0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0] * unit,
    tposition=T[0 0 0; 0.25 0.25 0.25] * unit,
    species=['A', 'B']
)

" Diamond lattice "
diamond(T::Type=Float64; unit=u"nm") = Crystal(
    T[0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0] * unit,
    tposition=T[0 0 0; 0.25 0.25 0.25] * unit
)

" Wurtzite lattice "
wurtzite(T::Type=Float64; unit=u"nm") = Crystal(
    T[0.5 0.5 0; -sqrt(3)/2 sqrt(3)/2 0; 0 0 1] * unit,
    tposition=T[0.5  sqrt(3)/6 0;
                0.5 -sqrt(3)/6 0.5;
                0.5  sqrt(3)/6 0.25;
                0.5 -sqrt(3)/6 0.75] * unit,
    species=['A', 'A', 'B', 'B']
)


text:
abstract type RKAlphaModel <: AlphaModel end

struct RKAlphaParam <: EoSParam
end

@newmodelsimple RKAlpha RKAlphaModel RKAlphaParam
is_splittable(::RKAlpha) = false

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

function α_function(model::CubicModel,V,T,z,alpha_model::RKAlphaModel)
    Tc = model.params.Tc.values
    α = @. 1 /√(T/Tc)
    return α
end


text:
# Author: Gabrio Rizzuti, rizzuti.gabrio@gatech.edu
# Date: August, 2020
# Copyright: Georgia Institute of Technology, 2020
#
# Run MALA to sample from Gaussian mixture in 2D

## Module loading

using DrWatson
@quickactivate "UncertaintyQuantificationAVP"
using UncertaintyQuantificationAVP
using LinearAlgebra, PyPlot, Statistics
using Random; Random.seed!(1)

## Setting negative log density for Gaussian mixture

# Gaussian modes parameters
nmodes = 7
muComplex = Complex{Float32}.(exp.(1im*2f0*pi*collect(range(0f0, 1f0; length=nmodes+1))))
mu = [real.(muComplex[1:end-1]) imag.(muComplex[1:end-1])]
sigma2 = zeros(Float32, nmodes); sigma2 .= Float32(0.025)

# Sampling function
sample = function sample_gaussmixture(nsamples::Int64) # Direct sampling
    x = Array{Float32, 2}(undef, (nsamples, 2))
    for i = 1:nsamples
        imode = rand(1:nmodes)
        x[i, :] = mu[imode, :]+sqrt(sigma2[imode])*randn(Float32, 2)
    end
    return x
end
x = sample(2^10)
figure()
plot(x[:, 1], x[:, 2], "*")
title("Sample from Gaussian mixture distribution")

# Negative log density
function negLogDensity_gaussmixture(x::Array{Float32, 2})
    p = zeros(Float32, size(x, 1))
    g = zeros(Float32, size(x))
    for i = 1:nmodes
        p_i = exp.(-((x[:, 1].-mu[i, 1]).^2+(x[:, 2].-mu[i, 2]).^2)/(2*sigma2[i]))/(nmodes*2f0*pi*sigma2[i])
        p += p_i
        g += -p_i.*(x.-mu[i:i, :])/sigma2[i]
    end
    return -log.(p), -g./p
end
function neglogp(x::Array{Float32, 1})
    f, g = negLogDensity_gaussmixture(reshape(x, 1, 2))
    return f[1], reshape(g, size(x))
end
function meshgrid(x, y)
    X = [i for i in x, j in 1:length(y)]
    Y = [j for i in 1:length(x), j in y]
    return X, Y
end

## MALA sampling

# Starting input
x0 = randn(Float32, 2)

# Parameters
nsamples = 2^10
ϵ = 0.02f0
flag_MALA = true

# Run MALA
time = @elapsed fval, xLD = langevinSampler(neglogp, nsamples, x0, ϵ; flag_MALA = flag_MALA)
figure()
plot(x[:, 1], x[:, 2], "r*")
plot(xLD[1,:], xLD[2,:], ".")
title("MALA samples")


text:
#!/bin/bash
# -*- mode: julia -*-
#=
exec julia --project=@. --color=yes --startup-file=no -e "include(popfirst!(ARGS))" "${BASH_SOURCE[0]}" "$@"
=#

#import Pkg
#Pkg.activate(".")
#Pkg.instantiate()

import GuillotineModels
import TimerOutputs
# IMPORTANT: the GuillotineModels.CommandLine.run expects a solver to be
# specified (by its julia package name, case-sensitive) in the command-line
# arguments passed to this script. Such solver needs to be imported below, or
# GuillotineModels.CommandLine.run will throw an exception. The solvers are
# not imported inside the GuillotineModels package either because then all
# solvers would become required dependencies (and users will probably have
# just a subset of such solvers installed).

# Supported solvers as of Tue 28 Jan 2020 02:06:06 AM -03:
import CPLEX
import Gurobi
#import Cbc
import GLPK

# New solvers may be added by defining a function
# empty_configured_model(::Val{:SolverName}, p_args) (which return a
# JuMP.direct_model with the specified solver already configured, see
# SolversArgs.jl file for more info), and by importing the solver package
# here, as it is done with the already supported solvers.

GuillotineModels.CommandLine.run()
TimerOutputs.print_timer(GuillotineModels.TIMER; allocations = false)
println()


text:
using SARISA
using Test

@testset "GridDensities.jl" begin
  @test 1 == 1
end


text:
##############################################################################
##
## Default values for unspecified objects
##
## Sometimes needed when dealing with NA's for which some value must exist in
## the underlying data vector
##
##############################################################################

baseval{T <: String}(s::Type{T}) = ""
baseval(x::Any) = zero(x)

# slow, but maintains order and seems to work:
function _setdiff(a::Vector, b::Vector)
    idx = Int[]
    for i in 1:length(a)
        if !(a[i] in b)
            push!(idx, i)
        end
    end
    a[idx]
end

## Issue: this doesn't maintain the order in a:
## setdiff(a::Vector, b::Vector) = elements(Set(a...) - Set(b...))

function _uniqueofsorted(x::Vector)
    idx = fill(true, length(x))
    lastx = x[1]
    for i = 2:length(x)
        if lastx == x[i]
            idx[i] = false
        else
            lastx = x[i]
        end
    end
    x[idx]
end

function make_unique{S<:ByteString}(names::Vector{S})
    x = Index()
    names = copy(names)
    dups = Int[]
    for i in 1:length(names)
        if haskey(x, names[i])
            push!(dups, i)
        else
            push!(x, names[i])
        end
    end
    for i in dups
        nm = names[i]
        newnm = nm
        k = 1
        while true
            newnm = "$(nm)_$k"
            if !haskey(x, newnm)
                push!(x, newnm)
                break
            end
            k += 1
        end
        names[i] = newnm
    end
    names
end

function generate_column_names(n::Int)
    convert(Vector{ByteString}, map(i -> "x" * string(i), 1:n))
end

function ancestors(t::Type)
    a = {t}
    while t != Any
        t = super(t)
        push!(a, t)
    end
    return a
end

function common_ancestors(s::Type, t::Type)
    return filter(e -> (e in ancestors(s)), ancestors(t))
end

earliest_common_ancestor(s::Type, t::Type) = first(common_ancestors(s, t))

# Need to do type inference
# Like earliest_common_ancestor, but ignores NA
function _dv_most_generic_type(vals)
    # iterate over vals tuple to find the most generic non-NA type
    toptype = None
    for i = 1:length(vals)
        if !isna(vals[i])
            toptype = promote_type(toptype, typeof(vals[i]))
        end
    end
    if !method_exists(baseval, (toptype, ))
        error("No baseval exists for type: $(toptype)")
    end
    return toptype
end


text:
function main(args)
    @show args
end
meta_key_file, num_workers, this = main(ARGS)
println(meta_key_file)
println(num_workers)
println(this)

sleep(15)


text:
"""

    lta(setting; exact = false)

Perform the Hawkins & Olive (1999) algorithm (Least Trimmed Absolute Deviations)
for the given regression setting.

# Arguments
- `setting::RegressionSetting`: RegressionSetting object with a formula and dataset.
- `exact::Bool`: Consider all possible subsets of p or not where p is the number of regression parameters.

# Examples
```julia-repl
julia> reg0001 = createRegressionSetting(@formula(calls ~ year), phones);
julia> lta(reg0001)
Dict{Any,Any} with 2 entries:
  "betas"     => [-55.5, 1.15]
  "objective" => 5.7

julia> lta(reg0001, exact = true)
Dict{Any,Any} with 2 entries:
  "betas"     => [-55.5, 1.15]
  "objective" => 5.7
```

# References
Hawkins, Douglas M., and David Olive. "Applications and algorithms for least trimmed sum of
absolute deviations regression." Computational Statistics & Data Analysis 32.2 (1999): 119-134.
"""
function lta(setting::RegressionSetting; exact=false)
    X, y = @extractRegressionSetting setting
    return lta(X, y, exact=exact)
end

"""

    lta(X, y; exact = false)

Perform the Hawkins & Olive (1999) algorithm (Least Trimmed Absolute Deviations)
for the given regression setting.

# Arguments
- `X::Array{Float64, 2}`: Design matrix of linear regression model.
- `y::Array{Float64, 1}`: Response vector of linear regression model.
- `exact::Bool`: Consider all possible subsets of p or not where p is the number of regression parameters.

# References
Hawkins, Douglas M., and David Olive. "Applications and algorithms for least trimmed sum of
absolute deviations regression." Computational Statistics & Data Analysis 32.2 (1999): 119-134.
"""
function lta(X::Array{Float64,2}, y::Array{Float64,1}; exact=false)
    n, p = size(X)
    h = Int(floor((n + p + 1.0) / 2.0))

    if exact
        psubsets = combinations(1:n, p)
    else
        iters = p * 3000
        psubsets = [sample(1:n, p, replace=false) for i in 1:iters]
    end

    function lta_cost(subsetindices::Array{Int,1})::Tuple{Float64,Array{Float64,1}}
        try
            subX = X[subsetindices,:]
            suby = y[subsetindices]
            olsreg = ols(subX, suby)
            betas = coef(olsreg)
            res_abs = abs.(y .- X * betas)
            ordered_res = sort(res_abs)
            cost = sum(ordered_res[1:h])
            return (cost, betas)
        catch
            return (Inf64, [])
        end
    end

    allpairs = asyncmap(lta_cost, psubsets)
    allcosts = map(x -> first(x), allpairs)
    ordering_allcosts = sortperm(allcosts)
    best_pair = allpairs[ordering_allcosts[1]]
    betas = best_pair[2]
    cost = best_pair[1]

    result = Dict()
    result["betas"] = betas
    result["objective"] = cost
    return result
end

max_stars_repo_name: angeris/LinRegOutliers

max_issues_repo_name: angeris/LinRegOutliers

max_forks_repo_name: angeris/LinRegOutliers


text:
include("basic_setup.jl")

function perf_est(subsample)
    algorithm = AnnealingEstimate(subsample, 50, 100)
    Trajectories.simulate(algorithm, initial, system)
end

results = perf_est.(1:5:26)

using Statistics
using Plots

plot(1:5:26, Trajectories.log_marginal.(results), yerr=sqrt.(var.(results)))
plot(1:5:26, sqrt.(var.(results)))

plot(result.inv_temps[1:end - 1], mean(result.acceptance, dims=2))
plot(result.inv_temps[begin + 1:end - 1], diff(mean(result.weights, dims=2), dims=1))

histogram(result.weights[end, :])

Trajectories.log_marginal(result)
sqrt(var(result))

mean(result.weights[end, :])

var(result.weights[end, :]) / 10_000


text:
# MIT license
# Copyright (c) Microsoft Corporation. All rights reserved.
# See LICENSE in the project root for full license information.

using OpticSim:area,Rectangle,ParaxialLensRect,virtualpoint
using Unitful.DefaultSymbols

@testset "ParaxialAnalysis" begin
    @testset "Lens" begin
        lens = ParaxialLensRect(10.0,100.0,100.0,[0.0,0.0,1.0],[0.0,0.0,0.0])
        displaypoint = [0.0,0.0,-8.0]
        r1 = Ray(displaypoint,[1.0,0.0,1.0])
        intsct = OpticSim.surfaceintersection(lens,r1)
        intsctpt = point(intsct)
        # compute the refracted ray and project this backwards to its intersection with the optical axis. This should be the same position as returned by virtualpoint()
        refrac,_,_ = OpticSim.processintersection(OpticSim.interface(lens),OpticSim.point(intsct),OpticSim.normal(lens),OpticalRay(r1,1.0,.55),OpticSim.TEMP_REF,OpticSim.PRESSURE_REF,false)
        # compute intersection of ray with optical axis. this should match the position of the virtual point
        slope = refrac[1]/refrac[3]
        virtptfromslope = [0.0,0.0,-intsctpt[1]/slope]
        @test isapprox(virtptfromslope, point(OpticSim.virtualpoint(lens,displaypoint)))
    end

    @testset "Projection" begin
        focallength = 10.0
        lens = ParaxialLensRect(focallength,100.0,100.0,[0.0,0.0,1.0],[0.0,0.0,0.0])
        display = OpticSim.Repeat.Display(1000,1000,1.0μm,1.0μm,translation(0.0,0.0,-focallength))
        lenslet = OpticSim.Repeat.LensletAssembly(lens,identitytransform(),display)
        displaypoint = SVector(0.0,0.0,-8.0)
        pupilpoints = SMatrix{3,2}(10.0,10.0,10.0,-10.0,-10.0,20.0)
        Repeat.project(lenslet,displaypoint,pupilpoints)
    end

    @testset "BeamEnergy" begin
        focallength = 10.0
        lens = ParaxialLensRect(focallength,1.0,1.0,[0.0,0.0,1.0],[0.0,0.0,0.0])
        display = OpticSim.Repeat.Display(1000,1000,1.0μm,1.0μm,translation(0.0,0.0,-focallength))
        lenslet = OpticSim.Repeat.LensletAssembly(lens,identitytransform(),display)
        displaypoint = SVector(0.0,0.0,-8.0)
        #pupil is placed so that only 1/4 of it (approximately) is illuminated by lens beam
        pupil = Rectangle(1.0,1.0,SVector(0.0,0.0,-1.0),SVector(2.0,2.0,40.0))
        energy,centroid = OpticSim.Repeat.beamenergy(lenslet,displaypoint,Geometry.vertices3d(pupil))
        @test isapprox(1/16, energy,atol = 1e-4)
        @test isapprox([.75,.75,0.0],centroid)
    end

    @testset "SphericalPolygon" begin
        """creates a circular polygon that subtends a half angle of θ"""
        function sphericalcircle(θ, nsides = 10)
            temp = MMatrix{3,nsides,Float64}(undef)
            for i in 0:1:(nsides-1)
                ϕ = i*2π/nsides
                temp[1,i+1] = sin(θ)*cos(ϕ)
                temp[2,i+1] = cos(θ)
                temp[3,i+1] = sin(θ)*sin(ϕ)
            end
            return SphericalPolygon(SMatrix{3,nsides,Float64}(temp),SVector(0.0,0.0,0.0),1.0)
        end

        oneeigthsphere() = SphericalTriangle(SMatrix{3,3,Float64}(
            0.0,1.0,0.0,
            1.0,0.0,0.0,
            0.0,0.0,1.0),
            SVector(0.0,0.0,0.0),
            1.0)

        onesixteenthphere() = SphericalTriangle(SMatrix{3,3,Float64}(
            0.0,1.0,0.0,
            1.0,1.0,0.0,
            0.0,0.0,1.0),
            SVector(0.0,0.0,0.0),
            1.0)

        onesixteenthspherepoly() = SphericalPolygon(SMatrix{3,3,Float64}(
            0.0,1.0,0.0,
            1.0,1.0,0.0,
            0.0,0.0,1.0),
            SVector(0.0,0.0,0.0),
            1.0)

        threesidedpoly() = SphericalPolygon(SMatrix{3,3,Float64}(
            0.0,1.0,0.0,
            1.0,0.0,0.0,
            0.0,0.0,1.0),
            SVector(0.0,0.0,0.0),
            1.0)

        foursidedpoly() = SphericalPolygon(SMatrix{3,4,Float64}(
            0.0,1.0,0.0,
            1.0,1.0,-.9,
            1.0,0.0,0.0,
            0.0,0.0,1.0),
            SVector(0.0,0.0,0.0),
            1.0)

        #test that spherical triangle and spherical polygon area algorithms return the same values for the same spherical areas
        @test isapprox(area(oneeigthsphere()), area(threesidedpoly()))
        @test isapprox(area(onesixteenthphere()),area(onesixteenthspherepoly()))

        #halve the spherical area and make sure area function computes 1/2 the area
        @test isapprox(area(onesixteenthphere()), area(oneeigthsphere())/2.0)
        @test isapprox(area(sphericalcircle(π/8.0,1000))/4.0,area(sphericalcircle(π/16.0,1000)), atol = 1e-2)
    end
end


text:
################################################################################
# MathProgBase solver interface - MDEstimator
################################################################################

function MathProgBase.initialize(d::MDEstimator, rf::Vector{Symbol})
    for feat in rf
        if !(feat in [:Grad, :Jac, :Hess])
            error("Unsupported feature $feat")
        end
    end
end

MathProgBase.isobjlinear(e::MDEstimator) = false
MathProgBase.isobjquadratic(e::MDEstimator) = false
MathProgBase.isconstrlinear(e::MDEstimator, i::Int64) = false

MathProgBase.features_available(e::MDEstimator) = [:Grad, :Jac, :Hess]

function MathProgBase.eval_f(e::MDEstimator{M, V, S, T}, u) where {M, V, S, T<:Unweighted}
    Divergences.evaluate(e.div, u[1:nobs(e)])
end

function MathProgBase.eval_grad_f(e::MDEstimator{M, V, S, T}, grad_f, u) where {M, V, S, T<:Unweighted}
    n, k, m = size(e)
    Divergences.gradient!(grad_f, e.div, u)
    @simd for j=(n+1):(n+k)
        @inbounds grad_f[j] = 0.0
    end
end

function MathProgBase.eval_g(e::MDEstimator{M, V,S,T}, g, u) where {M, V, S<:Unconstrained, T<:Unweighted}
    n, k, m = size(e)
    p = u[1:n]
    θ = u[(n+1):(n+k)]
    @inbounds g[1:m]  = e.mf.s(θ)'*p
    @inbounds g[m+1]  = sum(p)
end

function MathProgBase.jac_structure(e::MDEstimator{M, V, S, T}) where {M, V, S<:Unconstrained, T<:Unweighted}
    n, k, m = size(e)
    rows = Array{Int64}(undef, e.gele)
    cols = Array{Int64}(undef, e.gele)
    for j = 1:m+1, r = 1:n+k
        if !((r > n) && (j==m+1))
            @inbounds rows[r+(j-1)*(n+k)] = j
            @inbounds cols[r+(j-1)*(n+k)] = r
        end
    end
    rows, cols
end

function MathProgBase.eval_jac_g(e::MDEstimator{M, V,S,T}, J, u) where {M<:FADMomFun, V, S<:Unconstrained, T<:Unweighted}
    n, k, m = size(e)
    p  = u[1:n]
    θ  = u[(n+1):(n+k)]
    g  = e.mf.s(θ)::Matrix{Float64}
    ws(θ) = e.mf.s(θ)'*p
    Dws = ForwardDiff.jacobian(ws, θ)::Matrix{Float64}
    @inbounds for j=1:m+1, i=1:n+k
        if(j<=m && i<=n)
            J[i+(j-1)*(n+k)] = g[i+(j-1)*n]
        elseif (j<=m && i>n)
            J[i+(j-1)*(n+k)] = Dws[j, i-n]
        elseif (j>m && i<=n)
            J[i+(j-1)*(n+k)] = 1.0
        end
     end
end

function MathProgBase.eval_jac_g(e::MDEstimator{M, V,S,T}, J, u) where {M <: AnaMomFun, V, S<:Unconstrained, T<:Unweighted}
    n, k, m = size(e)
    p  = u[1:n]
    θ  = u[(n+1):(n+k)]
    g  = e.mf.s(θ)
    Dws = e.mf.Dws(θ, p)
    @inbounds for j=1:m+1, i=1:n+k
        if(j<=m && i<=n)
            J[i+(j-1)*(n+k)] = g[i+(j-1)*n]
        elseif (j<=m && i>n)
            J[i+(j-1)*(n+k)] = Dws[j, i-n]
        elseif (j>m && i<=n)
            J[i+(j-1)*(n+k)] = 1.0
        end
    end
end

function MathProgBase.hesslag_structure(e::MDEstimator{M, V, S, T}) where {M, V, S<:Unconstrained, T<:Unweighted}
    n, k, m = size(e)
    rows = Array{Int64}(undef, e.hele)
    cols = Array{Int64}(undef, e.hele)
    @simd for j = 1:n
        @inbounds rows[j] = j
        @inbounds cols[j] = j
    end
    idx = n+1

    for s = 1:n
        for j = 1:k
            @inbounds rows[idx] = n+j
            @inbounds cols[idx] = s
            idx += 1
        end
    end

    for j = 1:k
        for s = 1:j
            @inbounds rows[idx] = n+j
            @inbounds cols[idx] = n+s
            idx += 1
        end
    end
    rows, cols
end

function MathProgBase.eval_hesslag(e::MDEstimator{M, V, S, T}, H, u, σ, λ) where {M<:FADMomFun, V, S<:Unconstrained, T}
    n, k, m = size(e)
    p = u[1:n]
    θ = u[(n+1):(n+k)]
    if σ==0
        @simd for j=1:n
            @inbounds H[j] = 0.0
        end
    else
        Divergences.hessian!(H, e.div, u[1:n])
        @simd for j in 1:n
            @inbounds H[j] = σ*H[j]
        end
    end
    sl(θ)  = e.mf.s(θ)*λ[1:m]
    wsl(θ) = (p'*e.mf.s(θ)*λ[1:m])[1]
    @inbounds H[n+1:n*k+n] = transpose(ForwardDiff.jacobian(sl, θ)::Matrix{Float64})
    @inbounds H[n*k+n+1:e.hele] = gettril(ForwardDiff.hessian(wsl, θ)::Matrix{Float64})
end

function MathProgBase.eval_hesslag(e::MDEstimator{M, V, S, T}, H, u, σ, λ) where {M<:AnaGradMomFun, V, S<:Unconstrained, T}
    n, k, m = size(e)
    p = u[1:n]
    θ = u[(n+1):(n+k)]
    if σ==0
        @simd for j=1:n
            @inbounds H[j] = 0.0
        end
    else
        Divergences.hessian!(H, e.div, u[1:n])
        @simd for j in 1:n
            @inbounds H[j] = σ*H[j]
        end
    end
    Dsl  = e.mf.Dsl(θ, λ[1:m])
    wsl(θ) = (p'*e.mf.s(θ)*λ[1:m])[1]
    @inbounds H[n+1:n*k+n] = Dsl'
    @inbounds H[n*k+n+1:e.hele] = gettril(ForwardDiff.hessian(wsl, θ)::Matrix{Float64})
end

function MathProgBase.eval_hesslag(e::MDEstimator{M, V, S, T}, H, u, σ, λ) where {M<:AnaFullMomFun, V, S<:Unconstrained, T}
    n, k, m = size(e)
    p = u[1:n]
    θ = u[(n+1):(n+k)]
    if σ==0
        @simd for j=1:n
            @inbounds H[j] = 0.0
        end
    else
        Divergences.hessian!(H, e.div, u[1:n])
        @simd for j in 1:n
            @inbounds H[j] = σ*H[j]
        end
    end
    Dsl  = e.mf.Dsl(θ, λ[1:m])
    Hwsl = e.mf.Hwsl(θ, p, λ[1:m])
    @inbounds H[n+1:n*k+n] = Dsl'
    @inbounds H[n*k+n+1:e.hele] = gettril(Hwsl)
end


text:
using ArrayInterface
using ArrayInterfaceOffsetArrays
using OffsetArrays
using Static
using Test

A = zeros(3, 4, 5);
O = OffsetArray(A, 3, 7, 10);
Op = PermutedDimsArray(O,(3,1,2));
@test @inferred(ArrayInterface.offsets(O)) === (4, 8, 11)
@test @inferred(ArrayInterface.offsets(Op)) === (11, 4, 8)

@test @inferred(ArrayInterface.to_indices(O, (:, :, :))) == (4:6, 8:11, 11:15)
@test @inferred(ArrayInterface.to_indices(Op, (:, :, :))) == (11:15, 4:6, 8:11)

@test @inferred(ArrayInterface.offsets((1,2,3))) === (StaticInt(1),)
o = OffsetArray(vec(A), 8);
@test @inferred(ArrayInterface.offset1(o)) === 9

@test @inferred(ArrayInterface.device(OffsetArray(view(PermutedDimsArray(A, (3,1,2)), 1, :, 2:4)', 3, -173))) === ArrayInterface.CPUPointer()
@test @inferred(ArrayInterface.device(view(OffsetArray(A,2,3,-12), 4, :, -11:-9))) === ArrayInterface.CPUPointer()
@test @inferred(ArrayInterface.device(view(OffsetArray(A,2,3,-12), 3, :, [-11,-10,-9])')) === ArrayInterface.CPUIndex()

@test @inferred(ArrayInterface.indices(OffsetArray(view(PermutedDimsArray(A, (3,1,2)), 1, :, 2:4)', 3, -173),1)) === Base.Slice(ArrayInterface.OptionallyStaticUnitRange(4,6))
@test @inferred(ArrayInterface.indices(OffsetArray(view(PermutedDimsArray(A, (3,1,2)), 1, :, 2:4)', 3, -173),2)) === Base.Slice(ArrayInterface.OptionallyStaticUnitRange(-172,-170))

@test @inferred(ArrayInterface.device(OffsetArray(1:10))) === ArrayInterface.CPUIndex()
@test @inferred(ArrayInterface.device(OffsetArray(@view(reshape(1:8, 2,2,2)[1,1:2,:]),-3,4))) === ArrayInterface.CPUIndex()
@test @inferred(ArrayInterface.device(OffsetArray(zeros(2,2,2),8,-2,-5))) === ArrayInterface.CPUPointer()

offset_view = @view OffsetArrays.centered(zeros(eltype(A), 5, 5))[:, begin]; # SubArray of OffsetArray
@test @inferred(ArrayInterface.offsets(offset_view)) == (-2,)

B = OffsetArray(PermutedDimsArray(rand(2,3,4), (2,3,1)));
@test @inferred(ArrayInterface.StrideIndex(B)) === ArrayInterface.StrideIndex{3, (2, 3, 1), 3}((2, 6, static(1)), (1, 1, 1))


text:
#this will hold all attributes relating to portfolio construction
struct PortfolioConstruction{T}
  Fval::Symbol #the field that holds the values
  Fnval::Symbol #this field will hold a count in the final portfolio
  Fquantile::Symbol #the field that holds the quantiles
  Fgroup::Symbol #the field that holds the group values
  Fweight::Symbol#the field that holds the weights

  valtype::Type
  breakpoints::T #holds a tuple of breakpints
  groupweights::T #holds a tuple of weighting coefficents (after market cap normalization)

  Ngroups::Int #total number of groups
end

#main constructor
#most items are pulled from constants
function PortfolioConstruction(::Type{T}, Fval::Symbol;
  factorbreakpoints::Dict = FACTOR_BREAKPOINTS,
  factorgroupweights::Dict = FACTOR_GROUP_WEIGHTS,
  factorvaluetypes::Dict = FACTOR_VALUE_TYPES) where T

  Fnval = Symbol("N_", Fval)
  Fquantile = Symbol(:Q, Fval)
  Fgroup = Symbol(:G, Fval)
  Fweight = Symbol(:W, Fval)

  valtype::Type = factorvaluetypes[Fval]
  breakpoints::T = factorbreakpoints[Fval]
  groupweights::T = factorgroupweights[Fval]

  return PortfolioConstruction{T}(Fval, Fnval, Fquantile, Fgroup,
    Fweight, valtype, breakpoints, groupweights, length(breakpoints))
end

#constructor which does not require a type
function PortfolioConstruction(Fval::Symbol;
  factorngroups::Dict = FACTOR_N_GROUPS)

  T::Type = NTuple{factorngroups[Fval], Float64}

  return PortfolioConstruction(T, Fval)
end

#define eltype for portfolio construction
Base.eltype(::Type{PortfolioConstruction{T where T}}) = T
Base.eltype(pc::PortfolioConstruction) = eltype(typeof(pc))

#########################
# Portfolio Construction
##########################

#create a container structure for all of the portfolio constructions
struct PortfolioConstructions
  pcs::Vector{PortfolioConstruction}
  idx::Dict{Symbol, PortfolioConstruction}
  N::Int
end

#main constructor
function PortfolioConstructions(factorfields::Vector{Symbol} = FACTOR_FIELDS)
  pcs::Vector{PortfolioConstruction} = (PortfolioConstruction).(factorfields)
  idx::Dict = Dict(pc.Fval=>pc for pc ∈ pcs)

  return PortfolioConstructions(pcs, idx, length(pcs))
end

#make indexing work
Base.getindex(pcs::PortfolioConstructions, syms::Symbol...) = (s::Symbol->
  pcs.idx[s]).(syms)
Base.getindex(pcs::PortfolioConstructions, sym::Symbol) = pcs.idx[sym]
Base.getindex(pcs::PortfolioConstructions, indices::Int...) = pcs.pcs[indices]
Base.getindex(pcs::PortfolioConstructions, indice::Int) = pcs.pcs[indice]

#make length work
Base.length(pcs::PortfolioConstructions)::Int = length(pcs.pcs)

#make iteration work
function Base.iterate(pcs::PortfolioConstructions, state::Int = 1)
  (state > pcs.N) && return nothing
  return (pcs[state], state + 1)
end

Fvals(pcs::PortfolioConstructions)::Vector{Symbol} = (
  pc::PortfolioConstruction->pc.Fval).(pcs)
Fquantiles(pcs::PortfolioConstructions)::Vector{Symbol} = (
  pc::PortfolioConstruction->pc.Fquantile).(pcs)
Fgroups(pcs::PortfolioConstructions)::Vector{Symbol} = (
  pc::PortfolioConstruction->pc.Fgroup).(pcs)
Fweights(pcs::PortfolioConstructions)::Vector{Symbol} = (
  pc::PortfolioConstruction->pc.Fweight).(pcs)

function computesorts(pcs::PortfolioConstructions)
  Nsorts::Int = prod((pc->pc.Ngroups).(pcs))

  sorts::Vector{NTuple{pcs.N, Int}} = Vector{NTuple{pcs.N, Int}}(undef, Nsorts)
  sortlabels::Vector{Vector{Int}} = (pc->collect(1:pc.Ngroups)).(pcs)
  println(sortlabels)

  ctr::Int = 0
  for p ∈ Iterators.product(sortlabels...)
    ctr+=1
    sorts[ctr] = NTuple{pcs.N, Int}(p)
  end

  return sorts
end

max_stars_repo_name: clintonTE/CCA

max_issues_repo_name: clintonTE/CCA

max_forks_repo_name: clintonTE/CCA


text:
struct Initialization <: FEMContainer
    num_dim::Int64
    num_nodes::Int64
    num_elem::Int64
    num_elem_blk::Int64
    num_node_sets::Int64
    num_side_sets::Int64
    function Initialization(exo_id::ExoID)
        num_dim, num_nodes, num_elem,
        num_elem_blk, num_node_sets, num_side_sets, title =
        read_initialization_parameters(exo_id)
        return new(num_dim, num_nodes, num_elem,
                   num_elem_blk, num_node_sets, num_side_sets)
    end
end
Base.show(io::IO, init::Initialization) =
print(io, "Initialization:\n",
          "\tNum dim       = ", init.num_dim, "\n",
          "\tNum nodes     = ", init.num_nodes, "\n",
          "\tNum elem      = ", init.num_elem, "\n",
          "\tNum blocks    = ", init.num_elem_blk, "\n",
          "\tNum node sets = ", init.num_node_sets, "\n",
          "\tNum side sets = ", init.num_side_sets, "\n")

function read_initialization_parameters(exo_id::ExoID)
    num_dim = Ref{Int64}(0)
    num_nodes = Ref{Int64}(0)
    num_elem = Ref{Int64}(0)
    num_elem_blk = Ref{Int64}(0)
    num_node_sets = Ref{Int64}(0)
    num_side_sets = Ref{Int64}(0)
    error = Ref{Int64}(0)

    title = Vector{UInt8}(undef, MAX_LINE_LENGTH)
    error = ccall((:ex_get_init, libexodus), ExodusError,
                  (ExoID, Ptr{UInt8},
                   Ref{Int64}, Ref{Int64}, Ref{Int64},
                   Ref{Int64}, Ref{Int64}, Ref{Int64}),
                  exo_id, title,
                  num_dim, num_nodes, num_elem,
                  num_elem_blk, num_node_sets, num_side_sets)
    title = unsafe_string(pointer(title))
    exodus_error_check(error, "read_initialization_parameterss")

    return num_dim[], num_nodes[], num_elem[],
           num_elem_blk[], num_node_sets[], num_side_sets[], title
end


text:
# full kth partial derivative
function ∇ϕ(x, k, A, y)
    res  = 0.0
    for j in 1:size(A)[1]
        res += ∇ϕs(x, k, j, A, y)
    end
    res
end

# full in  place gradient
function ∇ϕ!(G, x, X, y)
    for k in 1:length(x)
        G[k] = ∇ϕ(x, k, X, y)
    end
    G
end

println("Preprocessing data")
ξref = randn(d)
println("Distance before optimization: $(norm(ξref - ξtrue))")
results = optimize(x -> ϕ(x, X, y), (G, x) -> ∇ϕ!(G, x, X, y),  ξref)
println("Optimization success: $(results.ls_success)")
ξref = Optim.minimizer(results)
println("Distance after optimization: $(norm(ξref - ξtrue))")


text:
# Learning Phases to make evaluation easier

abstract type AbstractLearningPhase end
struct TrainingPhase <: AbstractLearningPhase end
struct EvaluationPhase <: AbstractLearningPhase end

mutable struct UpdateState{LT, GT, PST, OPT}
    loss::LT
    grads::GT
    params::PST
    opt::OPT
end

# Update introspections.

abstract type AbstractIntrospection end

apply(tpl::Tuple{Vararg{T} where {T<:AbstractIntrospection}}, us::UpdateState) =
    [apply(interspect, us) for interspect ∈ tpl]
struct L1GradIntrospection <: AbstractIntrospection end
struct L2GradIntrospection <: AbstractIntrospection end
struct LossIntrospection <: AbstractIntrospection end

apply(::AbstractIntrospection, ::UpdateState) = nothing
apply(::L1GradIntrospection, us::UpdateState) = begin
    l1_grad = 0.0f0
    for (k, v) ∈ us.grads
        l1_grad += sum(abs.(v))
    end
    l1_grad
end
apply(::L2GradIntrospection, us::UpdateState) = begin
    l2_grad = 0.0f0
    for (k, v) ∈ us.grads
        l2_grad += dot(v, v)
    end
    l2_grad
end
apply(::LossIntrospection, us::UpdateState) = us.loss


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

module LoadBalancerClient

using Random
using Dates
using HTTP
using Swagger
import Swagger: field_name, property_type, hasproperty, validate_property, SwaggerApi, SwaggerModel
import Base: convert, propertynames

include("modelincludes.jl")

include("api_LoadBalancersApi.jl")

# export models
export convert, BackendAddressPoolPropertiesFormat
export convert, FrontendIPConfigurationPropertiesFormat
export convert, InboundNatPoolPropertiesFormat
export convert, InboundNatRulePropertiesFormat
export convert, LoadBalancerListResult
export convert, LoadBalancerPropertiesFormat
export convert, LoadBalancingRulePropertiesFormat
export convert, OutboundNatRulePropertiesFormat
export convert, ProbePropertiesFormat
export convert, Resource
export convert, SubResource
export convert, BackendAddressPool
export convert, FrontendIPConfiguration
export convert, InboundNatPool
export convert, InboundNatRule
export convert, LoadBalancer
export convert, LoadBalancingRule
export convert, NetworkInterfaceIPConfiguration
export convert, OutboundNatRule
export convert, Probe
export convert, PublicIPAddress
export convert, Subnet

# export operations
export convert, LoadBalancersApi

export check_required, field_name, property_type, hasproperty, propertynames, validate_property, convert

end


text:
# Functions to proccess parameters table for parameters estimation problem (fitting).
# Parameters table is based on PETab format:
# https://petab.readthedocs.io/en/latest/documentation_data_format.html#id3

const PARAMS_FIELDS_DICT = Base.ImmutableDict(
  :parameter => Symbol, # parametersId
  :scale => Symbol,     # parameterScale
  :lower => Float64,    # lowerBound
  :upper => Float64,    # upperBound
  :nominal => Float64,  # nominalValue
  :estimate => Bool
)

const PARAMS_FIELDS_OPT_DICT = Base.ImmutableDict(
  :parameterName => Symbol,
)

# read parameters csv file and output DataFrame
function read_parameters(filepath::String; kwargs...)
  params_df = CSV.read(
    filepath, DataFrame;
    types = merge(PARAMS_FIELDS_DICT,PARAMS_FIELDS_OPT_DICT), kwargs...)
  assert_params(params_df, keys(PARAMS_FIELDS_DICT))
  return params_df
end

function assert_params(df, colnames)
  names_df = propertynames(df)
  for f in colnames
    @assert f ∈ names_df "Required column name \"$f\" is not found in the table."
  end
  return nothing
end


text:
include("../src/bootstrap.jl")

using .Bootstrap.Emit

# add other gcp Apis here (if needed)
Emit.getEmit(:bigquery)

max_stars_repo_name: ProfLeao/mais

max_issues_repo_name: ProfLeao/mais

max_forks_repo_name: ProfLeao/mais


text:
"""
$(TYPEDEF)
$(TYPEDFIELDS)

"""
struct QuantLogNormal <: LogNormalQuantDist
    mu::NestedFloat
    sigma::NestedFloat
    q::NestedFloat
end

function quantlognormal(mu::Float64, sigma::Float64, q::Float64)::Float64
    return round(exp(normal(mu, sigma)) / q) * q
end

lognormalquant(x::QuantLogNormal, mu, sigma, q) = quantlognormal(mu, sigma, q)


text:
# syntax: proto2
using Compat
using ProtoBuf
import ProtoBuf.meta

# service methods for ApplicationMasterProtocolService
const _ApplicationMasterProtocolService_methods = MethodDescriptor[
        MethodDescriptor("registerApplicationMaster", 1, hadoop.yarn.RegisterApplicationMasterRequestProto, hadoop.yarn.RegisterApplicationMasterResponseProto),
        MethodDescriptor("finishApplicationMaster", 2, hadoop.yarn.FinishApplicationMasterRequestProto, hadoop.yarn.FinishApplicationMasterResponseProto),
        MethodDescriptor("allocate", 3, hadoop.yarn.AllocateRequestProto, hadoop.yarn.AllocateResponseProto)
    ] # const _ApplicationMasterProtocolService_methods
const _ApplicationMasterProtocolService_desc = ServiceDescriptor("hadoop.yarn.ApplicationMasterProtocolService", 1, _ApplicationMasterProtocolService_methods)

ApplicationMasterProtocolService(impl::Module) = ProtoService(_ApplicationMasterProtocolService_desc, impl)

mutable struct ApplicationMasterProtocolServiceStub <: AbstractProtoServiceStub{false}
    impl::ProtoServiceStub
    ApplicationMasterProtocolServiceStub(channel::ProtoRpcChannel) = new(ProtoServiceStub(_ApplicationMasterProtocolService_desc, channel))
end # mutable struct ApplicationMasterProtocolServiceStub

mutable struct ApplicationMasterProtocolServiceBlockingStub <: AbstractProtoServiceStub{true}
    impl::ProtoServiceBlockingStub
    ApplicationMasterProtocolServiceBlockingStub(channel::ProtoRpcChannel) = new(ProtoServiceBlockingStub(_ApplicationMasterProtocolService_desc, channel))
end # mutable struct ApplicationMasterProtocolServiceBlockingStub

registerApplicationMaster(stub::ApplicationMasterProtocolServiceStub, controller::ProtoRpcController, inp::hadoop.yarn.RegisterApplicationMasterRequestProto, done::Function) = call_method(stub.impl, _ApplicationMasterProtocolService_methods[1], controller, inp, done)
registerApplicationMaster(stub::ApplicationMasterProtocolServiceBlockingStub, controller::ProtoRpcController, inp::hadoop.yarn.RegisterApplicationMasterRequestProto) = call_method(stub.impl, _ApplicationMasterProtocolService_methods[1], controller, inp)

finishApplicationMaster(stub::ApplicationMasterProtocolServiceStub, controller::ProtoRpcController, inp::hadoop.yarn.FinishApplicationMasterRequestProto, done::Function) = call_method(stub.impl, _ApplicationMasterProtocolService_methods[2], controller, inp, done)
finishApplicationMaster(stub::ApplicationMasterProtocolServiceBlockingStub, controller::ProtoRpcController, inp::hadoop.yarn.FinishApplicationMasterRequestProto) = call_method(stub.impl, _ApplicationMasterProtocolService_methods[2], controller, inp)

allocate(stub::ApplicationMasterProtocolServiceStub, controller::ProtoRpcController, inp::hadoop.yarn.AllocateRequestProto, done::Function) = call_method(stub.impl, _ApplicationMasterProtocolService_methods[3], controller, inp, done)
allocate(stub::ApplicationMasterProtocolServiceBlockingStub, controller::ProtoRpcController, inp::hadoop.yarn.AllocateRequestProto) = call_method(stub.impl, _ApplicationMasterProtocolService_methods[3], controller, inp)

export ApplicationMasterProtocolService, ApplicationMasterProtocolServiceStub, ApplicationMasterProtocolServiceBlockingStub, registerApplicationMaster, finishApplicationMaster, allocate


text:
function convert_function(cursor::LibClang.CXCursor, indent::Int)
	typ = LibClang.clang_getCursorType(cursor)

	((pre, t, post), comments) = convert_type(cursor, LibClang.clang_getResultType(typ), indent)

	num = LibClang.clang_Cursor_getNumArguments(cursor)
	args = map(1:num) do i
		arg = LibClang.clang_Cursor_getArgument(cursor, i-1)

		argName = convert_name(arg)
		argName = isempty(argName) ? "var\"?$(i)?\"" : argName

		((argPre, argT, argPost), argComments) = convert_type(arg, LibClang.clang_getCursorType(arg), indent)
		merge_comments!(comments, argComments)

		decl = LibClang.clang_getTypeDeclaration(argT)
		if decl.kind == LibClang.CXCursor_EnumDecl && decl in arg
			convert_decl = convert_enum
		elseif decl.kind == LibClang.CXCursor_StructDecl && decl in arg
			convert_decl = convert_struct
		elseif decl.kind == LibClang.CXCursor_UnionDecl && decl in arg
			convert_decl = convert_union
		else
			convert_decl = (_, _) -> Converted(convert_name(argT), Dict{String, Comment}())
		end

		cvt = convert_decl(decl, indent)
		merge_comments!(comments, cvt.comments)

		expr = startswith(cvt.expr, "𝐣𝐥.@") && startswith(argPost, '[') ? "($(cvt.expr))" : cvt.expr
		expr = "$(argPre)$(expr)$(argPost)"
		if startswith(expr, "𝐣𝐥.@") && num > 1
			expr = "($(expr))"
		end
		return "$(argName)::$(expr)"
	end
	Bool(LibClang.clang_Cursor_isVariadic(cursor)) && push!(args, "var\"?vararg?\"...")
	args = join(args, ", ")

	conv = convert_convention(typ)
	conv = "__$(lowercase(conv))__"

	# TODO: use calling convention
	(pre, post) = ("($(args))::"*pre, post)

	return ((pre, t, post), comments)
end


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

@test_throws TypeError NamedTuple{1,Tuple{}}
@test_throws TypeError NamedTuple{(),1}
@test_throws TypeError NamedTuple{(:a,1),Tuple{Int}}
@test_throws ErrorException NamedTuple{(:a,:b),Tuple{Int}}
@test_throws ErrorException NamedTuple{(:a,:b),Tuple{Int,Vararg{Int}}}
@test_throws ErrorException NamedTuple{(:a,),Union{Tuple{Int},Tuple{String}}}
@test_throws ErrorException NamedTuple{(:a,:a),Tuple{Int,Int}}
@test_throws ErrorException NamedTuple{(:a,:a)}((1,2))
@test_throws ErrorException NamedTuple{(:a, :b, :a), NTuple{3, Int}}((1, 2, 3))
@test_throws ArgumentError NamedTuple{(:a, :b, :c), NTuple{3, Int}}((1, 2))

@test fieldcount(NamedTuple{(:a,:b,:c)}) == 3
@test fieldcount(NamedTuple{<:Any,Tuple{Int,Int}}) == 2
@test_throws ArgumentError fieldcount(NamedTuple)
@test_throws ArgumentError fieldcount(NamedTuple{<:Any,<:Tuple{Int,Vararg{Int}}})

@test (a=1,).a == 1
@test (a=2,)[1] == 2
@test (a=3,)[:a] == 3
@test (x=4, y=5, z=6).y == 5
@test (x=4, y=5, z=6).z == 6
@test (x=4, y=5, z=6)[(:x, :y)] == (x=4, y=5)
@test (x=4, y=5, z=6)[(:x,)] == (x=4,)
@test (x=4, y=5, z=6)[[:x, :y]] == (x=4, y=5)
@test (x=4, y=5, z=6)[[:x]] == (x=4,)
@test (x=4, y=5, z=6)[()] == NamedTuple()
@test NamedTuple()[()] == NamedTuple()
@test_throws ErrorException (x=4, y=5, z=6).a
@test_throws BoundsError (a=2,)[0]
@test_throws BoundsError (a=2,)[2]
@test_throws ErrorException (x=4, y=5, z=6)[(:a,)]
@test_throws ErrorException (x=4, y=5, z=6)[(:x, :a)]
@test_throws ErrorException (x=4, y=5, z=6)[[:a]]
@test_throws ErrorException (x=4, y=5, z=6)[[:x, :a]]
@test_throws ErrorException (x=4, y=5, z=6)[(:x, :x)]

@test length(NamedTuple()) == 0
@test length((a=1,)) == 1
@test length((a=1, b=0)) == 2

@test firstindex((a=1, b=0)) == 1
@test firstindex((a=1,)) == 1
@test firstindex(NamedTuple()) == 1
@test lastindex((a=1, b=0)) == 2
@test lastindex((a=1,)) == 1
@test lastindex(NamedTuple()) == 0

@test isempty(NamedTuple())
@test !isempty((a=1,))
@test empty((a=1,)) === NamedTuple()
@test isempty(empty((a=1,)))

@test (a=1,b=2) === (a=1,b=2)
@test (a=1,b=2) !== (b=1,a=2)

@test (a=1,b=2) == (a=1,b=2)
@test (a=1,b=2) != (b=1,a=2)
@test NamedTuple() === NamedTuple()
@test NamedTuple() != (a=1,)
@test !isequal(NamedTuple(), (a=1,))

@test string((a=1,)) == "(a = 1,)"
@test string((name="", day=:today)) == "(name = \"\", day = :today)"
@test string(NamedTuple()) == "NamedTuple()"

@test hash((a = 1, b = "hello")) == hash(NamedTuple{(:a,:b),Tuple{Int,String}}((1, "hello")))
@test hash((a = 1, b = "hello")) != hash(NamedTuple{(:a,:c),Tuple{Int,String}}((1, "hello")))
@test hash((a = 1, b = "hello")) != hash(NamedTuple{(:a,:b),Tuple{Int,String}}((1, "helo")))

@test NamedTuple{(:a,:b),Tuple{Int8,Int16}}((1,2)) === (a=Int8(1), b=Int16(2))
@test convert(NamedTuple{(:a,:b),Tuple{Int8,Int16}}, (a=3,b=4)) === (a=Int8(3), b=Int16(4))
let NT = NamedTuple{(:a,:b),Tuple{Int8,Int16}}, nt = (x=3,y=4)
    @test_throws MethodError convert(NT, nt)
end

@test NamedTuple{(:a,:c)}((b=1,z=2,c=3,aa=4,a=5)) === (a=5, c=3)
@test NamedTuple{(:a,)}(NamedTuple{(:b, :a), Tuple{Int, Union{Int,Nothing}}}((1, 2))) ===
    NamedTuple{(:a,), Tuple{Union{Int,Nothing}}}((2,))

@test eltype((a=[1,2], b=[3,4])) === Vector{Int}
@test eltype(NamedTuple{(:x, :y),Tuple{Union{Missing, Int},Union{Missing, Float64}}}(
    (missing, missing))) === Union{Real, Missing}

@test Tuple((a=[1,2], b=[3,4])) == ([1,2], [3,4])
@test Tuple(NamedTuple()) === ()
@test Tuple((x=4, y=5, z=6)) == (4,5,6)
@test collect((x=4, y=5, z=6)) == [4,5,6]
@test Tuple((a=1, b=2, c=3)) == (1, 2, 3)

@test isless((a=1,b=2), (a=1,b=3))
@test_throws MethodError isless((a=1,), (a=1,b=2))
@test !isless((a=1,b=2), (a=1,b=2))
@test !isless((a=2,b=1), (a=1,b=2))
@test_throws MethodError isless((a=1,), (x=2,))

@test (a=1,b=2) < (a=1,b=3)
@test_throws MethodError (a=1,) < (a=1,b=2)
@test !((a=1,b=2) < (a=1,b=2))
@test !((a=2,b=1) < (a=1,b=2))
@test_throws MethodError (a=1,) < (x=2,)
@test !((a=-0.0,) < (a=0.0,))
@test ismissing((a=missing,) < (a=1,))
@test ismissing((a=missing,) < (a=missing,))

@test map(-, (x=1, y=2)) == (x=-1, y=-2)
@test map(+, (x=1, y=2), (x=10, y=20)) == (x=11, y=22)
@test_throws ArgumentError map(+, (x=1, y=2), (y=10, x=20))
@test map(string, (x=1, y=2)) == (x="1", y="2")
@test map(round, (x=UInt, y=Int), (x=3.1, y=2//3)) == (x=UInt(3), y=1)

@test merge((a=1, b=2), (a=10,)) == (a=10, b=2)
@test merge((a=1, b=2), (a=10, z=20)) == (a=10, b=2, z=20)
@test merge((a=1, b=2), (z=20,)) == (a=1, b=2, z=20)
@test merge(NamedTuple(), (a=2, b=1)) == (a=2, b=1)
@test merge((a=2, b=1), NamedTuple()) == (a=2, b=1)
@test merge(NamedTuple(), NamedTuple()) == NamedTuple()
# `merge` should preserve element types
let nt = merge(NamedTuple{(:a,:b),Tuple{Int32,Union{Int32,Nothing}}}((1,Int32(2))),
               NamedTuple{(:a,:c),Tuple{Union{Int8,Nothing},Float64}}((nothing,1.0)))
    @test typeof(nt) == NamedTuple{(:a,:b,:c),Tuple{Union{Int8,Nothing},Union{Int32,Nothing},Float64}}
    @test repr(nt) == "NamedTuple{(:a, :b, :c), Tuple{Union{Nothing, Int8}, Union{Nothing, Int32}, Float64}}((nothing, 2, 1.0))"
end

@test merge(NamedTuple(), [:a=>1, :b=>2, :c=>3, :a=>4, :c=>5]) == (a=4, b=2, c=5)
@test merge((c=0, z=1), [:a=>1, :b=>2, :c=>3, :a=>4, :c=>5]) == (c=5, z=1, a=4, b=2)

@test keys((a=1, b=2, c=3)) == (:a, :b, :c)
@test keys(NamedTuple()) == ()
@test keys((a=1,)) == (:a,)
@test values((a=1, b=2, c=3)) == (1, 2, 3)
@test values(NamedTuple()) == ()
@test values((a=1,)) == (1,)
@test haskey((a=1, b=2, c=3), :a)
@test !haskey(NamedTuple(), :a)
@test !haskey((a=1,), :b)
@test get((a=1, b=2, c=3), :a, 0) == 1
@test get(NamedTuple(), :a, 0) == 0
@test get((a=1,), :b, 0) == 0
@test get(()->0, (a=1, b=2, c=3), :a) == 1
@test get(()->0, NamedTuple(), :a) == 0
@test get(()->0, (a=1,), :b) == 0
@test Base.tail((a = 1, b = 2.0, c = 'x')) ≡ (b = 2.0, c = 'x')
@test Base.tail((a = 1, )) ≡ NamedTuple()
@test Base.front((a = 1, b = 2.0, c = 'x')) ≡ (a = 1, b = 2.0)
@test Base.front((a = 1, )) ≡ NamedTuple()
@test_throws ArgumentError Base.tail(NamedTuple())
@test_throws ArgumentError Base.front(NamedTuple())

# syntax errors

@test Meta.lower(Main, Meta.parse("(a=1, 0)")) == Expr(:error, "invalid named tuple element \"0\"")
@test Meta.lower(Main, Meta.parse("(a=1, f(x))")) == Expr(:error, "invalid named tuple element \"f(x)\"")
@test Meta.lower(Main, Meta.parse("(a=1,a=2)")) == Expr(:error, "field name \"a\" repeated in named tuple")
@test Meta.lower(Main, Meta.parse("(a=1,b=0,a=2)")) == Expr(:error, "field name \"a\" repeated in named tuple")
@test Meta.lower(Main, Meta.parse("(c=1,a=1,b=0,a=2)")) == Expr(:error, "field name \"a\" repeated in named tuple")

@test Meta.lower(Main, Meta.parse("(; f(x))")) == Expr(:error, "invalid named tuple element \"f(x)\"")
@test Meta.lower(Main, Meta.parse("(;1=0)")) == Expr(:error, "invalid named tuple field name \"1\"")

@test eval(Expr(:tuple, Expr(:parameters))) === NamedTuple()
@test Meta.lower(Main, Meta.parse("(1,;2)")) == Expr(:error, "unexpected semicolon in tuple")

# splatting

let d = [:a=>1, :b=>2, :c=>3]   # use an array to preserve order
    @test (d..., a=10) == (a=10, b=2, c=3)
    @test (a=0, b=0, z=1, d..., x=4, y=5) == (a=1, b=2, z=1, c=3, x=4, y=5)
    @test (a=0, (b=2,a=1)..., c=3) == (a=1, b=2, c=3)

    t = (x=1, y=20)
    @test (;d...) == (a=1, b=2, c=3)
    @test (;d..., :z=>20) == (a=1, b=2, c=3, z=20)
    @test (;a=10, d..., :c=>30) == (a=1, b=2, c=30)
    y = (w=30, z=40)
    @test (;t..., y...) == (x=1, y=20, w=30, z=40)
    @test (;t..., y=0, y...) == (x=1, y=0, w=30, z=40)

    @test NamedTuple(d) === (a=1, b=2, c=3)
end

# inference tests

namedtuple_get_a(x) = x.a
@test Base.return_types(namedtuple_get_a, (NamedTuple,)) == Any[Any]
@test Base.return_types(namedtuple_get_a, (typeof((b=1,a="")),)) == Any[String]

namedtuple_fieldtype_a(x) = fieldtype(typeof(x), :a)
@test Base.return_types(namedtuple_fieldtype_a, (NamedTuple,)) == Any[Union{Type, TypeVar}]
@test Base.return_types(namedtuple_fieldtype_a, (typeof((b=1,a="")),)) == Any[Type{String}]
namedtuple_fieldtype__(x, y) = fieldtype(typeof(x), y)
@test Base.return_types(namedtuple_fieldtype__, (typeof((b=1,a="")),Symbol))[1] >: Union{Type{Int}, Type{String}}

namedtuple_nfields(x) = nfields(x) === 0 ? 1 : ""
@test Union{Int,String} <: Base.return_types(namedtuple_nfields, (NamedTuple,))[1]

function nt_from_abstractly_typed_array()
    a = NamedTuple[(a=3,b=5)]
    (getfield(a[1],1), getfield(a[1],2))
end
@test nt_from_abstractly_typed_array() === (3,5)

let T = NamedTuple{(:a, :b), Tuple{Int64, Union{Float64, Nothing}}}, nt = T((1, nothing))
    @test nt == (a=1, b=nothing)
    @test typeof(nt) == T
    @test convert(T, (a=1, b=nothing)) == nt
    @test typeof(convert(T, (a=1, b=nothing))) === T
end

function abstr_nt_22194()
    a = NamedTuple[(a=1,), (b=2,)]
    return (a[1].a, a[2].b)
end
@test abstr_nt_22194() == (1, 2)
@test Base.return_types(abstr_nt_22194, ()) == Any[Tuple{Any,Any}]
function abstr_nt_22194_2()
    a = NamedTuple[(a=1,), (b=2,)]
    return a[1].b
end
@test_throws ErrorException abstr_nt_22194_2()
@test Base.return_types(abstr_nt_22194_2, ()) == Any[Any]

mutable struct HasAbstractNamedTuples
    x::NamedTuple{(:a,:b)}
end

function abstr_nt_22194_3()
    s = HasAbstractNamedTuples((a="",b=8))
    @test s.x.a === ""
    @test s.x.b === 8
    s.x = (a=1,b=:b)
    @test s.x.a === 1
    @test s.x.b === :b
    @test isdefined(s.x, :a)
    @test isdefined(s.x, :b)
    @test !isdefined(s.x, :c)
    @test nfields(s) == 1
    @test isdefined(s, :x)
    @test fieldtype(typeof(s), 1) == fieldtype(typeof(s), :x) == NamedTuple{(:a,:b)}
    @test fieldtype(typeof(s.x), :a) === Int
    @test fieldtype(typeof(s.x), :b) === Symbol
    return s.x.b
end
abstr_nt_22194_3()
@test Base.return_types(abstr_nt_22194_3, ()) == Any[Any]

@test Base.structdiff((a=1, b=2), (b=3,)) == (a=1,)
@test Base.structdiff((a=1, b=2, z=20), (b=3,)) == (a=1, z=20)
@test Base.structdiff((a=1, b=2, z=20), (b=3, q=20, z=1)) == (a=1,)
@test Base.structdiff((a=1, b=2, z=20), (b=3, q=20, z=1, a=0)) == NamedTuple()
@test Base.structdiff((a=1, b=2, z=20), NamedTuple{(:b,)}) == (a=1, z=20)
@test typeof(Base.structdiff(NamedTuple{(:a, :b), Tuple{Int32, Union{Int32, Nothing}}}((1, Int32(2))),
                             (a=0,))) === NamedTuple{(:b,), Tuple{Union{Int32, Nothing}}}

@test findall(isequal(1), (a=1, b=2)) == [:a]
@test findall(isequal(1), (a=1, b=1)) == [:a, :b]
@test isempty(findall(isequal(1), NamedTuple()))
@test isempty(findall(isequal(1), (a=2, b=3)))
@test findfirst(isequal(1), (a=1, b=2)) == :a
@test findlast(isequal(1), (a=1, b=2)) == :a
@test findfirst(isequal(1), (a=1, b=1)) == :a
@test findlast(isequal(1), (a=1, b=1)) == :b
@test findfirst(isequal(1), ()) === nothing
@test findlast(isequal(1), ()) === nothing
@test findfirst(isequal(1), (a=2, b=3)) === nothing
@test findlast(isequal(1), (a=2, b=3)) === nothing

# Test map with Nothing and Missing
for T in (Nothing, Missing)
    x = [(a=1, b=T()), (a=1, b=2)]
    y = map(v -> (a=v.a, b=v.b), [(a=1, b=T()), (a=1, b=2)])
    @test y isa Vector{NamedTuple{(:a,:b), T} where T<:Tuple}
    @test isequal(x, y)
end
y = map(v -> (a=v.a, b=v.a + v.b), [(a=1, b=missing), (a=1, b=2)])
@test y isa Vector{NamedTuple{(:a,:b), T} where T<:Tuple}
@test isequal(y, [(a=1, b=missing), (a=1, b=3)])

# issue #27187
@test reduce(merge,[(a = 1, b = 2), (c = 3, d = 4)]) == (a = 1, b = 2, c = 3, d = 4)
@test typeintersect(NamedTuple{()}, NamedTuple{names, Tuple{Int,Int}} where names) == Union{}

# Iterator constructor
@test NamedTuple{(:a, :b), Tuple{Int, Float64}}(Any[1.0, 2]) === (a=1, b=2.0)
@test NamedTuple{(:a, :b)}(Any[1.0, 2]) === (a=1.0, b=2)

# Left-associative merge, issue #29215
@test merge((a=1, b=2), (b=3, c=4), (c=5,)) === (a=1, b=3, c=5)
@test merge((a=1, b=2), (b=3, c=(d=1,)), (c=(d=2,),)) === (a=1, b=3, c=(d=2,))
@test merge((a=1, b=2)) === (a=1, b=2)

# issue #33270
let n = NamedTuple{(:T,), Tuple{Type{Float64}}}((Float64,))
    @test n isa NamedTuple{(:T,), Tuple{Type{Float64}}}
    @test n.T === Float64
end

# setindex
let nt0 = NamedTuple(), nt1 = (a=33,), nt2 = (a=0, b=:v)
    @test Base.setindex(nt0, 33, :a) == nt1
    @test Base.setindex(Base.setindex(nt1, 0, :a), :v, :b) == nt2
    @test Base.setindex(nt1, "value", :a) == (a="value",)
    @test Base.setindex(nt1, "value", :a) isa NamedTuple{(:a,),<:Tuple{AbstractString}}

    @test Base.setindex(nt1, 42, 1) == (a=42,)
    @test Base.setindex(nt1, "value", 1) isa NamedTuple{(:a,),<:Tuple{AbstractString}}
    @test Base.setindex(nt2, "v", 2) == (a=0, b="v")
    @test_throws BoundsError Base.setindex(nt1, 42, 0)
    @test_throws BoundsError Base.setindex(nt1, 42, 2)
    @test_throws BoundsError Base.setindex(nt0, 45, 1)
end

# @NamedTuple
@testset "@NamedTuple" begin
    @test @NamedTuple{a::Int, b::String} === NamedTuple{(:a, :b),Tuple{Int,String}} ===
        @NamedTuple begin
            a::Int
            b::String
        end
    @test @NamedTuple{a::Int, b} === NamedTuple{(:a, :b),Tuple{Int,Any}}
    @test_throws LoadError include_string(Main, "@NamedTuple{a::Int, b, 3}")
    @test_throws LoadError include_string(Main, "@NamedTuple(a::Int, b)")
end

# issue #29333, implicit names
let x = 1, y = 2
    @test (;y) === (y = 2,)
    a = (; x, y)
    @test a === (x=1, y=2)
    @test (; a.y, a.x) === (y=2, x=1)
    y = 3
    @test Meta.lower(Main, Meta.parse("(; a.y, y)")) == Expr(:error, "field name \"y\" repeated in named tuple")
    @test (; a.y, x) === (y=2, x=1)
end

# issue #37926
@test nextind((a=1,), 1) == nextind((1,), 1) == 2
@test prevind((a=1,), 2) == prevind((1,), 2) == 1

# issue #43045
@test merge(NamedTuple(), Iterators.reverse(pairs((a=1,b=2)))) === (b = 2, a = 1)

max_stars_repo_name: Xnartharax/julia

max_issues_repo_name: Xnartharax/julia

max_forks_repo_name: Xnartharax/julia


text:
scene, layout = layoutscene()
lscene = layout[1:4,2:4] = Axis(scene)

timeslider = Slider(scene, range = collect(1:50), startvalue = 1)
timenode = timeslider.value
state = @lift(ut[:, 60, $timenode])
scatter!(lscene, state)
@lift(GLMakie.AbstractPlotting.ylims!(lscene, extrema($state)))
layout[2,1] = vgrid!(
    Label(scene, "My slider"),
    timeslider,
)
display(scene)

## MiniTemplate
scene, layout = layoutscene()
lscene = layout[1:4,2:4] = Axis(scene)
times = [1, 25, 50]
timenode = Node(times[1]) # timeslider.value
state = @lift(ut[:, 60, $timenode])
scatter!(lscene, state)
@lift(GLMakie.AbstractPlotting.ylims!(lscene, extrema($state)))
timemenu = Menu(scene, options = zip(string.(times), times))
on(timemenu.selection) do s
    timenode[] = s
end
layout[1,1] = vgrid!(
    Label(scene, "My Menu"),
    timemenu,
)
display(scene)


text:
# Julia wrapper for header: /usr/local/include/libavfilter/vsrc_buffer.h
# Automatically generated using Clang.jl wrap_c, version 0.0.0

export
    av_vsrc_buffer_add_frame

function av_vsrc_buffer_add_frame(buffer_filter,frame,pts::Int64,pixel_aspect::AVRational)
    ccall((:av_vsrc_buffer_add_frame,libavfilter),Cint,(Ptr{AVFilterContext},Ptr{AVFrame},Int64,AVRational),buffer_filter,frame,pts,pixel_aspect)
end


text:
#https://www.mathworks.com/help/matlab/ref/plot.html
function plot(y;options=Dict())
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("plot(y"*optionsString*")")
end

function plot(x::Array{T},y;options=Dict()) where T<:Number
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("plot(x,y"*optionsString*")")
end

function plot(x::Array{T},y,lineSpec::String;options=Dict()) where T<:Number
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("plot(x,y"*",\"$lineSpec\""*optionsString*")")
end

#https://www.mathworks.com/help/matlab/ref/semilogx.html
function semilogx(y;options=Dict())
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("semilogx(y"*optionsString*")")
end

function semilogx(x,y;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("semilogx(x,y"*",\"$lineSpec\""*optionsString*")")
end

function semilogx(x,y,lineSpec::String;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("semilogx(x,y"*",\"$lineSpec\""*optionsString*")")
end

#https://www.mathworks.com/help/matlab/ref/semilogy.html
function semilogy(y;options=Dict())
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("semilogy(y"*optionsString*")")
end

function semilogy(x,y;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("semilogy(x,y"*",\"$lineSpec\""*optionsString*")")
end

function semilogy(x,y,lineSpec::String;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("semilogy(x,y"*",\"$lineSpec\""*optionsString*")")
end

#https://www.mathworks.com/help/matlab/ref/loglog.html
function loglog(y;options=Dict())
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("loglog(y"*optionsString*")")
end

function loglog(x,y;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("loglog(x,y"*",\"$lineSpec\""*optionsString*")")
end

function loglog(x,y,lineSpec::String;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("loglog(x,y"*",\"$lineSpec\""*optionsString*")")
end

#https://www.mathworks.com/help/matlab/ref/stairs.html
function stairs(y;options=Dict())
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("stairs(y"*optionsString*")")
end

function stairs(x,y;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("stairs(x,y"*",\"$lineSpec\""*optionsString*")")
end

function stairs(x,y,lineSpec::String;options=Dict())
    MATLAB.@mput x
    MATLAB.@mput y
    optionsString = namevaluepairs(options)
    MATLAB.eval_string("stairs(x,y"*",\"$lineSpec\""*optionsString*")")
end


text:
module FundamentalsNumericalComputation

export FNC
FNC = FundamentalsNumericalComputation

using Reexport

@info "Re-exporting multiple packages..."

@reexport using LinearAlgebra
@reexport using Statistics
@reexport using SparseArrays
@reexport using Polynomials
@reexport using NLsolve
@reexport using DifferentialEquations
@reexport using Plots
@reexport using PrettyTables
@reexport using Dierckx: Spline1D
@reexport using QuadGK
@reexport using MatrixDepot
@reexport using GraphRecipes
@reexport using Images
@reexport using TestImages
@reexport using Arpack
@reexport using IterativeSolvers
@reexport using LinearMaps
@reexport using IncompleteLU
@reexport using Preconditioners
@reexport using FFTW
@reexport using FileIO
@reexport using SpecialFunctions
@reexport using LaTeXStrings
@reexport using Printf
@reexport using JLD2
@reexport using Printf

include("chapter01.jl")
include("chapter02.jl")
include("chapter03.jl")
include("chapter04.jl")
include("chapter05.jl")
include("chapter06.jl")
include("chapter08.jl")
include("chapter09.jl")
include("chapter10.jl")
include("chapter11.jl")
include("chapter13.jl")

end


text:
using LinearAlgebra: dot
using Distributions: gamma_mle_update
using SpecialFunctions: digamma, trigamma

function mod_fit(::Type{<:Beta}, x_bar, v_bar)
    temp = ((x_bar * (one(x_bar) - x_bar)) / v_bar) - one(x_bar)
    α = x_bar * temp
    β = (one(x_bar) - x_bar) * temp
    return Beta(α, β)
end

function mod_fit_mle(::Beta, g₁, g₂, x_bar, v_bar;
                    maxiter::Int=1000, tol::Float64=1e-12)
    α, β = params(mod_fit(Beta, x_bar, v_bar))
    θ= [α ; β ]

    converged = false
    t=0
    while !converged && t < maxiter #newton method
        t+=1
        temp1 = digamma(θ[1]+θ[2])
        temp2 = trigamma(θ[1]+θ[2])
        grad = [g₁+temp1-digamma(θ[1])
               temp1+g₂-digamma(θ[2])]
        hess = [temp2-trigamma(θ[1]) temp2
                temp2 temp2-trigamma(θ[2])]
        Δθ = hess\grad #newton step
        θ .-= Δθ
        converged = dot(Δθ,Δθ) < 2*tol #stopping criterion
    end

    return Beta(θ[1], θ[2])
end

function mod_fit_mle(start::Gamma, mx, mlogx;
    alpha0::Float64=NaN, maxiter::Int=1000, tol::Float64=1e-16)

    logmx = log(mx)

    a::Float64 = isnan(alpha0) ? (logmx - mlogx)/2 : alpha0
    converged = false

    t = 0
    while !converged && t < maxiter
        t += 1
        a_old = a
        a = gamma_mle_update(logmx, mlogx, a)
        converged = abs(a - a_old) <= tol
    end

    Gamma(a, mx / a)
end

safelog1mx(x, reg=1e-12) = log(clamp(one(x) - x, reg, 1 - reg))

function emstep(comp::Beta, samples::AbstractVector, weights::AbstractWeights, zz::AbstractVector; reg=1e-12, kwargs...)
    Z = sum(i -> zz[i] * weights[i], 1:length(samples))
    mlogx = sum(i -> zz[i] * weights[i] * log(samples[i] + reg), 1:length(samples)) / Z
    mlog1mx = sum(i -> zz[i] * weights[i] * safelog1mx(samples[i], reg), 1:length(samples)) / Z

    mx = sum(i -> zz[i] * weights[i] * samples[i], 1:length(samples)) / Z
    mx2 = sum(i -> zz[i] * weights[i] * samples[i] ^ 2, 1:length(samples)) / Z

    mod_fit_mle(comp, mlogx, mlog1mx, mx, mx2 - mx^2; kwargs...)
end

function emstep(comp::Normal, samples::AbstractVector, weights::AbstractWeights, zz::AbstractVector; kwargs...)
    Z = sum(i -> zz[i] * weights[i], 1:length(samples))
    mx = sum(i -> zz[i] * weights[i] * samples[i], 1:length(samples)) / Z
    mx2 = sum(i -> zz[i] * weights[i] * samples[i] ^ 2, 1:length(samples)) / Z

    Normal(mx, sqrt(mx2 - mx^2))
end

function emstep(::Exponential, samples::AbstractVector, weights::AbstractWeights, zz::AbstractVector; kwargs...)
    Z = sum(i -> zz[i] * weights[i], 1:length(samples))
    mx = sum(i -> zz[i] * weights[i] * samples[i], 1:length(samples)) / Z

    Exponential(mx)
end

function emstep(comp::Gamma, samples::AbstractVector, weights::AbstractWeights, zz::AbstractVector; reg=1e-12, kwargs...)
    Z = sum(i -> zz[i] * weights[i], 1:length(samples))
    mx = sum(i -> zz[i] * weights[i] * samples[i], 1:length(samples)) / Z
    mlogx = sum(i -> zz[i] * weights[i] * log(samples[i] + reg), 1:length(samples)) / Z

    mod_fit_mle(comp, mx, mlogx)
end

function emstep(comp::Erlang, samples::AbstractVector, weights::AbstractWeights, zz::AbstractVector; kwargs...)
    Z = sum(i -> zz[i] * weights[i], 1:length(samples))
    mx = sum(i -> zz[i] * weights[i] * samples[i], 1:length(samples)) / Z

    Erlang(comp.α, mx / comp.α)
end


text:
struct InequalityData
	q::Vector{Float64}
	r::Vector{Float64}
	s::Vector{Float64}
	t::Vector{Float64}
	isline::BitVector
	isparabola::BitVector
end

mutable struct InequalityDecomp
	U::Array{Float64, 2}
	Σ::Vector{Float64}
	Vt::Array{Float64}
	Dx::Vector{Float64}
	Dy::Vector{Float64}
	S::Vector{Float64}
	Jct::Array{Float64, 2}
	rank::Int
end

struct InequalityDecompAdjoint
	idecomp::InequalityDecomp
end

struct InequalityDecompProject
	idecomp::InequalityDecomp
end

struct InequalityDecompProjectAdjoint
	idecomp::InequalityDecomp
end

Base.adjoint(ip::InequalityDecompProject) = InequalityDecompProjectAdjoint(ip.idecomp)
Base.adjoint(idecomp::InequalityDecomp) = InequalityDecompAdjoint(idecomp)
Base.adjoint(ipadj::InequalityDecompProjectAdjoint) = InequalityDecompProject(ipadj.idecomp)
Base.adjoint(idecompadj::InequalityDecompAdjoint) = idecompadj.idecomp

# construct InequalityData struct using lower and upper bounds for x
function InequalityData(xl, xu)
	n = length(xl)

	if length(xu) != n
		error("xl and xu are of different lengths")
	end

	# allocate vectors
	q = Array{Float64}(undef, n)
	r = Array{Float64}(undef, n)
	s = Array{Float64}(undef, n)
	t = Array{Float64}(undef, n)
	isline = falses(n)
	isparabola = falses(n)

	@inbounds for i in 1:n
		linf = isinf(xl[i])
		uinf = isinf(xu[i])

		if linf && uinf
			q[i] = 0
			r[i] = 0
			s[i] = 0
			t[i] = 0
			isline[i] = true
		elseif !linf && uinf
			q[i] = 0
			r[i] = xl[i]
			s[i] = -1.0
			t[i] = xl[i]
			isparabola[i] = true
		elseif linf && !uinf
			q[i] = 0
			r[i] = xu[i]
			s[i] = 1.0
			t[i] = xu[i]
			isparabola[i] = true
		else
			q[i] = 1.0
			r[i] = (xu[i] + xl[i])/2
			s[i] = 1.0
			t[i] = (xu[i] - xl[i])^2 / 4
		end
	end

	return InequalityData(q, r, s, t, isline, isparabola)
end

# empty inequality data
InequalityData() = InequalityData(zeros(0), zeros(0))
InequalityDecomp() = InequalityDecomp(zeros(0,0), [zeros(0) for i in 1:5]..., zeros(0,0), 0)

# fills the second half of xaug (equal to y) with initial values that satisfy constraint manifold
function generate_initial_y!(xaug, idata::InequalityData)
	n = length(idata.q)

	x = view(xaug, 1:n)
	y = view(xaug, n+1:2*n)

	@inbounds for i in 1:n
		if idata.isline[i]
			y[i] = x[i]
		elseif idata.isparabola[i]
			y[i] = sqrt(max(-(x[i] - idata.t[i])/idata.s[i], 0.0)) + idata.r[i]
		else # is circle
			y[i] = sqrt(max(idata.t[i] - (x[i] - idata.r[i])^2, 0.0)) + idata.r[i]
		end
	end

	return xaug
end

# overwrites the first n values of cvalaug with the values of the bound constraints
function calculate_h!(cvalaug, x, idata::InequalityData)
	n = length(x) ÷ 2

	xview = view(x, 1:n)
	yview = view(x, n+1:2*n)

	cvalaug[1:n] .= idata.q.*(xview .- idata.r).^2 .+ (1.0 .- idata.q.^2).*xview .+
				idata.s.*(yview .- idata.r).^2 .- (1.0 .- idata.s.^2).*yview .- idata.t

	return cvalaug
end

# overwrites Dx, Dy, and S using inequality values
function inequality_gradient!(idecomp::InequalityDecomp, x, idata::InequalityData)
	n = length(x) ÷ 2

	# unpack
	Dx = idecomp.Dx
	Dy = idecomp.Dy
	S = idecomp.S

	# calculate gradients
	Dx .= 2.0.*idata.q.*(view(x, 1:n) .- idata.r) .+ (idata.q .== 0.0)
	Dy .= 2.0.*idata.s.*(view(x, n+1:2*n) .- idata.r) .- (idata.s .== 0.0)

	# normalize columns of Dx and Dy
	S .= sqrt.(Dx.*Dx .+ Dy.*Dy)
	Dx ./= S
	Dy ./= S
end

# function that defines map used in projected CG Newton steps
function augmented_hess_lag_vec!(dest, src, hess_lag_vec!, x, λ_kkt, λy_kkt, idata::InequalityData)
	n = length(x) ÷ 2

	src_x = view(src, 1:n)
	src_y = view(src, n+1:2*n)
	dest_x = view(dest, 1:n)
	dest_y = view(dest, n+1:2*n)

	# calculate Hessian action with respect to x
	hess_lag_vec!(dest_x, src_x, view(x, 1:n), λ_kkt)

	# calculate Hessian action with respect to y
	dest_x .+= 2 .* λy_kkt .* idata.q .* src_x
	dest_y .= 2 .* λy_kkt .* idata.s .* src_y
end

# define projection operators for InequalityDecompProject
function LinearAlgebra.mul!(dest, ip::InequalityDecompProject, v)
	idecomp = ip.idecomp
	n = length(idecomp.Dx)
	rank = idecomp.rank
	Dx = idecomp.Dx
	Dy = idecomp.Dy

	# dest = U[:, 1:rank]*v[n+1:n+rank]
	mul!(dest, view(idecomp.U, :, 1:rank), view(v, n+1:n+rank))

	# dest = dest + [Dx; Dy]*v[1:n]
	dest[1:n] .+= Dx .* view(v, 1:n)
	dest[n+1:2*n] .+= Dy .* view(v, 1:n)

	return dest
end

# dest = idecomp*v*a + dest*b
function LinearAlgebra.mul!(dest, ip::InequalityDecompProject, v, a, b)
	idecomp = ip.idecomp
	n = length(idecomp.Dx)
	rank = idecomp.rank
	Dx = idecomp.Dx
	Dy = idecomp.Dy

	# dest = a*U*v[n+1:n+rank] + b*dest
	mul!(dest, view(idecomp.U, :, 1:rank), view(v, n+1:n+rank), a, b)

	# dest = dest + a*[Dx; Dy]*v[1:n]
	dest[1:n] .+= a .* Dx .* view(v, 1:n)
	dest[n+1:2*n] .+= a .* Dy .* view(v, 1:n)

	return dest
end

# transpose projection
function LinearAlgebra.mul!(dest, ipa::InequalityDecompProjectAdjoint, v)
	idecomp = ipa.idecomp
	n = length(idecomp.Dx)
	rank = idecomp.rank
	Dx = idecomp.Dx
	Dy = idecomp.Dy

	# dest[1:n] = [Dx; Dy]*v[1:n]
	dest[1:n] .= Dx .* view(v, 1:n)
	dest[1:n] .+= Dy .* view(v, n+1:2*n)

	# dest[n+1:n+rank] = U'*v
	mul!(view(dest, n+1:n+rank), view(idecomp.U, :, 1:rank)', v)

	return dest
end

# define multiplication operators for InequalityDecomp
function LinearAlgebra.mul!(dest, idecomp::InequalityDecomp, v)
	n = length(idecomp.Dx)
	Jct = idecomp.Jct
	m = size(Jct, 2)
	Dx = idecomp.Dx
	Dy = idecomp.Dy
	S = idecomp.S

	# dest[1:n] = Jct*v[n+1:n+m]
	mul!(view(dest, 1:n), Jct, view(v, n+1:n+m))

	# dest += [Dx; Dy]*S * v[1:n]
	dest[1:n] .+= Dx .* S .* view(v, 1:n)
	dest[n+1:2*n] .= Dy .* S .* view(v, 1:n)

	return dest
end

# dest = idecomp*v*a + dest*b
function LinearAlgebra.mul!(dest, idecomp::InequalityDecomp, v, a, b)
	n = length(idecomp.Dx)
	Jct = idecomp.Jct
	m = size(Jct, 2)
	Dx = idecomp.Dx
	Dy = idecomp.Dy
	S = idecomp.S

	# dest[1:n] = a*Jct*v[n+1:n+m] + b*dest
	mul!(view(dest, 1:n), Jct, view(v, n+1:n+m), a, b)

	# dest += a*[Dx; Dy]*S * v[1:n]
	dest[1:n] .+= a .* Dx .* S .* view(v, 1:n)
	dest[n+1:2*n] .*= b
	dest[n+1:2*n] .+= a .* Dy .* S .* view(v, 1:n)

	return dest
end

# transpose projection
function LinearAlgebra.mul!(dest, ida::InequalityDecompAdjoint, v)
	idecomp = ida.idecomp
	n = length(idecomp.Dx)
	Jct = idecomp.Jct
	m = size(Jct, 2)
	Dx = idecomp.Dx
	Dy = idecomp.Dy
	S = idecomp.S

	# dest[1:n] = [Dx; Dy]*v[1:n]
	dest[1:n] .= S .* Dx .* view(v, 1:n)
	dest[1:n] .+= S .* Dy .* view(v, n+1:2*n)

	# dest[n+1:n+rank] = U'*v
	mul!(view(dest, n+1:n+m), Jct', view(v, 1:n))

	return dest
end

#=
Calculates Lagrange multipliers for projected gradient

INPUTS
Qt∇f - n+m vector of coefficients of projection of negative gradient
idecomp - decomposition
idata - inequality data structure describing constraints

OVERWRITTEN
λ_kkt and λy_kkt with Lagrange multipliers
Qt∇f for work

=#
function calculate_λ_kkt!(λ_kkt, λy_kkt, Qt∇f, idecomp::InequalityDecomp)
	n = length(idecomp.Dx)
	rank = idecomp.rank
	Σ = idecomp.Σ
	m = length(Σ)

	# Σ^{-1}
	@inbounds @simd for j = 1:rank
		Qt∇f[n+j] /= Σ[j]
	end
	@inbounds @simd for j = rank+1:m
		Qt∇f[n+j] = 0.0
	end

	mul!(λ_kkt, idecomp.Vt', view(Qt∇f, n+1:n+m)) # λ = -V Σ^{-1} U' g

	# λy = -S^{-1} R V Σ^{-1}Qt∇f[1:n] + S^{-1} Qt∇f[n+1:n+m]
	mul!(λy_kkt, idecomp.Jct, λ_kkt)
	λy_kkt .*= -1.0 .* idecomp.Dx ./ idecomp.S
	λy_kkt .+= view(Qt∇f, 1:n) ./ idecomp.S

	return λ_kkt, λy_kkt
end