text:
# extending base functionality and basic utilities

import Base: +, *, .+, .*, ./, .^, .-

typealias RealArray{T<:Real,N} AbstractArray{T,N}
typealias RealVector{T<:Real} AbstractArray{T,1}
typealias RealMatrix{T<:Real} AbstractArray{T,2}

typealias ComplexArray{T<:Complex,N} AbstractArray{T,N}
typealias ComplexVector{T<:Complex} AbstractArray{T,1}
typealias ComplexMatrix{T<:Complex} AbstractArray{T,2}

typealias IntegerArray{T<:Integer,N} AbstractArray{T,N}
typealias IntegerVector{T<:Integer} AbstractArray{T,1}
typealias IntegerMatrix{T<:Integer} AbstractArray{T,2}

# if catdims are not specificed, concatenate along new axis on end
Base.cat(A::AbstractArray...) = cat(maximum(map(ndims,A))+1, A...)
Base.cat{T}(catdims, A::AbstractArray{T}...) = catdims < 0 ? Base.cat_t(maximum(map(ndims,A))+catdims+1, T, A...) : Base.cat_t(catdims, T, A...)

function memsize(a)
    f = fieldnames(a)
    length(f) > 0 ? sum([memsize(getfield(a,k)) for k in f]) : sizeof(a)
end

memsize(a::Function) = sizeof(a)

function memsize(a::AbstractArray)
    f = fieldnames(a)
    length(f) > 0 ? sum([memsize(getfield(a,k)) for k in f]) :
        (length(a) > 0 ? sum([isdefined(a,i) ? memsize(a[i]) : 0 for i in 1:length(a)]) : 0)
end

memsize(a::Associative) = sum([memsize(a[k]) for k in keys(a)])

# function flatten(x::AbstractArray, dim::Int64)
#     if dim > 0
#         n = size(x)[1:dim]
#         d = prod(size(x)[dim+1:end])
#         return reshape(x,tuple(n...,d))
#     else
#         n = prod(size(x)[1:end+dim])
#         d = size(x)[end+dim+1:end]
#         return reshape(x,tuple(n,d...))
#     end
# end

# mat(x::AbstractArray) = flatten(x,-1)

@inline mat(x::AbstractMatrix) = x

@generated function mat(x::AbstractArray)
    d = ndims(x)
    si = [_ref(:s,i) for i in 1:d-1]             # [s[1], ..., s[d-1]]
    n = Expr(:call,:*,si...)                    # s[1] * s[2] * ... * s[d-1]
    m = _ref(:s,d)                               # s[d]
    quote
        s = size(x)
        reshape(x,($n,$m))
    end
end

macro defaults(d, expr)
    d = esc(d)
    out = Expr(:block)
    for line in expr.args
        if line.head == :(=)
            lhs = line.args[1]
            s = (:head in fieldnames(lhs) && lhs.head == :(::)) ? lhs.args[1] : lhs
            t = (:head in fieldnames(lhs) && lhs.head == :(::)) ? esc(lhs.args[2]) : :Any
            k = QuoteNode(s)
            s = esc(s)
            v = esc(line.args[2])
            push!(out.args,
                :(if $k in keys($d); $s = $d[$k]::$t; else $s = $d[$k] = ($v)::$t end)
            )
        end
    end
    out
end

struct(; kv...) = Dict{Symbol,Any}(kv)
struct(s::Dict{Symbol,Any}; kv...) = merge(s, Dict{Symbol,Any}(kv))

# function expandshape(As::Union{AbstractArray,Number}...)
#     d = maximum(map(ndims,As))
#     shps = [map(size,As)...]
#     d = maximum(map(length,shps))
#     lshps = filter(shp->length(shp)==d, shps) |> collect
#     tshp = [maximum([shp[i] for shp in lshps]) for i in 1:d]
#     nshps = Array{Array{Int64}}(length(As))

#     for (i,s) in enumerate(shps)
#         shp = nshps[i] = fill(1, max(length(s),d))
#         ind = 1
#         for (j,l) in filter(a->a[2]>1, enumerate(s))
#             k = findnext(tshp,l,max(ind,j))
#             if k == 0
#                 k = findnext(tshp,1,max(ind,j))
#             end
#             if k < j || k == 0
#                 throw(DimensionMismatch("array could not be broadcast to match destination"))
#             end
#             shp[k] = l
#             ind = k+1
#         end
#     end

#     tuple([nshps[i] != [] ? reshape(A, tuple(nshps[i]...)) : A for (i,A) in enumerate(As)]...)
# end

# (.*)(As::AbstractArray...) = broadcast(*, expandshape(As...)...)
# (.*)(A::AbstractArray, B::AbstractArray) = broadcast(*, expandshape(A,B)...)

# function .+(As::AbstractArray...)
#     As_ = expandshape(As...)
#     broadcast!(+, Array(Base.Broadcast.eltype_plus(As_...),
#         Base.Broadcast.broadcast_shape(As_...)), As_...)
# end

# function ./(A::AbstractArray, B::AbstractArray)
#     A_, B_ = expandshape(A,B)
#     broadcast!(/, Array(Base.Broadcast.type_div(eltype(A_), eltype(B_)), Base.Broadcast.broadcast_shape(A_, B_)), A_, B_)
# end

# function .^(A::AbstractArray, B::AbstractArray)
#     A_, B_ = expandshape(A,B)
#     broadcast!(^, Array(Base.Broadcast.type_pow(eltype(A_), eltype(B_)), Base.Broadcast.broadcast_shape(A_, B_)), A_, B_)
# end

# function .-(A::AbstractArray, B::AbstractArray)
#     A_, B_ = expandshape(A,B)
#     broadcast!(-, Array(Base.Broadcast.type_minus(eltype(A_), eltype(B_)), Base.Broadcast.broadcast_shape(A_,B_)), A_, B_)
# end

# function Base.(:*){T}(A::AbstractArray{T,2}, B::AbstractArray{T,3})
#     shp = size(B)
#     reshape(A*reshape(B, (shp[1],prod(shp[2:end]))), (size(A,1),shp[2:end]...))
# end

# function Base.(:*){T}(A::AbstractArray{T,4}, B::AbstractArray{T,2})
#     shp = size(A)
#     reshape(reshape(A, (prod(shp[1:end-1]),shp[end]))*B, (shp[1:end-1]...,size(B,2)))
# end

# function Base.(:*){T}(A::AbstractArray{T,3}, B::AbstractArray{T,2})
#     shp = size(A)
#     reshape(reshape(A, (prod(shp[1:end-1]),shp[end]))*B, (shp[1:end-1]...,size(B,2)))
# end

# function Base.(:*){T}(A::AbstractArray{T,4}, B::AbstractArray{T,1})
#     shp = size(A)
#     reshape(reshape(A, (prod(shp[1:end-1]),shp[end]))*B, (shp[1:end-1]...))
# end

_ref(sym,i) = Expr(:ref,sym,i)

@generated function Base.(:*)(x::AbstractArray, y::AbstractArray)
    dx = ndims(x)
    dy = ndims(y)
    sxi = [_ref(:sx,i) for i in 1:dx-1]
    syi = [_ref(:sy,i) for i in 2:dy]
    n = Expr(:call,:*,sxi...)
    m = _ref(:sx,dx)
    k = _ref(:sy,1)
    l = Expr(:call,:*,syi...)
    s = Expr(:tuple, sxi..., syi...)
    xm = dx > 2    ?    :(reshape(x,($n,$m)))    :    :x
    ym = dy > 2    ?    :(reshape(y,($k,$l)))    :    :y
    quote
        sx = size(x)
        sy = size(y)
        reshape($xm * $ym, $s)
    end
end

function Base.SparseMatrix.sparse{Tv}(A::AbstractArray{Tv,2}, B::SparseMatrixCSC)
    I,J = findn(B)
    ind = I+(J-1)*size(A,1)
    SparseMatrixCSC(B.m,B.n,B.colptr,B.rowval,A[ind])
end

# reverse slice
function rslice{T,N}(A::AbstractArray{T,N}, I::Base.ViewIndex...)
    n = ndims(A)
    m = length(I)
    if m < n
        I = tuple(I..., fill(Colon(),n-m)...)
    end
    slice(A, reverse(I)...)
end

# reverse sub
function rsub{T,N}(A::AbstractArray{T,N}, I::Base.ViewIndex...)
    n = ndims(A)
    m = length(I)
    if m < n
        I = tuple(I..., fill(Colon(),n-m)...)
    end
    sub(A, reverse(I)...)
end

function squeezeall(A::AbstractArray)
    s = tuple(filter(d -> d>1, size(A))...)  # select all dimensions greater than 1
    s == () ? reshape(A, s)[] : reshape(A, s)  # reshape to remove those dimensions
end

fermidirac(E::Union{Real,RealVector},mu::Real,T::Real) = 1./(exp((E-mu)./T) + 1)

# # converge a function f(x) wrt some parameter x
# # assuming an error approx c0 + c1 x + c2 x^2 + ... as x->0

# function converge(f_,x0,xiter;ftol=1e-6,xmin=1e-6,maxsteps=100,limit=:zero)
#     local p
#     x = Float64[x0]
#     f = Float64[]
#     ftol = 1e-6
#     i = 0
#     while true
#         push!(f,f_(x[end]))
#         if i > maxsteps
#             warn("did not converge. stopping")
#             break
#         end
#         if length(x) > 2
#             if limit == :zero
#                 p = poly_fit(x[end-2:end],f[end-2:end],2)
#             elseif limit == :infinity
#                 p = poly_fit(1./x[end-2:end],f[end-2:end],2)
#             end
#             df = p[1] - f[end]
#             # println("x=$(x[end]), f=$(f[end]), df=$(df)")
#             if abs(df) < ftol
#                 break
#             end
#         end
#         push!(x,xiter(x[end]))
#         i += 1
#     end
#     x[end], p[1], p[1] - f[end]
# end

max_stars_repo_name: johncsnyder/Element


text:
using LazyStrings
using Test

@testset "CharMappedString" begin
    @test lmap(lowercase, "HallO") == "hallo"
    @test lmap(uppercase, "HallO") == "HALLO"
    @test lmap(lowercase,"JuliaCon")[1] ==  map(lowercase,"JuliaCon")[1]

    @test SubString(lmap(lowercase,"JuliaCon"),1:5) ==  SubString(map(lowercase,"JuliaCon"),1:5)
end

@testset "ReversedString" begin
    @testset "prevind, nextind" begin
        s = "abc"
        rs = ReversedString(s)
        for n in 1:5
            i = 0
            @test_throws BoundsError prevind(s,i,n)
            @test_throws BoundsError prevind(rs,i,n)
            for i in 1:4
                if i==1
                    @test prevind(s,i) == prevind(rs,i)
                else
                    @test prevind(s,i,n) == prevind(rs,i,n)
                end
            end
            for i in 0:3
                if i==1
                    @test nextind(s,i) == nextind(rs,i)
                else
                    @test nextind(s,i,n) == nextind(rs,i,n)
                end
            end
            i = 5
            @test_throws BoundsError nextind(s,i,n)
            @test_throws BoundsError nextind(rs,i,n)
        end

        s="aän"
        rs = ReversedString(s)
        rs |> collect

        @test s[nextind(s,1)] == rs[nextind(rs,1)]
    end

    @test [ c for c in reverse("JuliaCon") ] == [ c for c in reversed("JuliaCon") ]
    @test reversed(reversed("JuliaCon")) == "JuliaCon"
    @test firstindex(reversed("JuliaCon")) == 1
    @test ncodeunits(reverse("JuliaCon")) == ncodeunits(reversed("JuliaCon"))

    @testset "thisind and unicode" begin
        jr = reverse("Gödel")
        @test thisind(jr,5) == 4
        @test jr[thisind(jr,5)] == 'ö'

        jr = reversed("Gödel")
        @test thisind(jr,5) == 5
        @test jr[thisind(jr,5)] == 'ö'
    end

    @test isvalid(reverse("JuliaCon"),1) == isvalid(reversed("JuliaCon"),1)
    @test codeunit(reverse("JuliaCon"),1) == codeunit(reversed("JuliaCon"),1)
    @test isvalid(reverse("JuliaCon"),10) == isvalid(reversed("JuliaCon"),10)

    @test reverse("JuliaCon")[2:4] == reversed("JuliaCon")[2:4]

    @test SubString(reverse("JuliaCon"),2:4) == SubString(reversed("JuliaCon"),2:4)
    @test SubString(reverse("JuliaCon"),2,4) == SubString(reversed("JuliaCon"),2,4)

    @test LazyStrings.reverse_index("JuliaCon",1) == 1

end

ext: jl


text:
struct Gaussian{K} <: WhittleLikelihoodInference.UnknownAcvTimeSeriesModel{1,Float64}
    α::Float64
    ωₚ::Float64
    κ::Float64

    norm::Float64
    halfκ⁻²::Float64
    function Gaussian{K}(α,ωₚ,κ) where {K}
        α > 0 || throw(ArgumentError("α must be > 0."))
        ωₚ > 0 || throw(ArgumentError("ωₚ must be > 0."))
        κ > 0 || throw(ArgumentError("κ must be > 0."))
        new(α,ωₚ,κ,α/(κ*sqrt(2π)),inv(2κ^2))
    end
    function Gaussian{K}(x) where {K}
        @boundscheck checkparameterlength(x,Gaussian{K})
        @inbounds Gaussian{K}(x[1],x[2],x[3])
    end
end

@fastmath function WhittleLikelihoodInference.sdf(model::Gaussian, ω::Real)
    return model.norm*exp(-(model.ωₚ-abs(ω))^2*model.halfκ⁻²)
end

WhittleLikelihoodInference.npars(::Type{Gaussian{K}}) where {K} = 3
WhittleLikelihoodInference.nalias(::Gaussian{K}) where {K} = K

WhittleLikelihoodInference.lowerbounds(::Type{Gaussian{K}}) where {K} = [0,  0, 0]
WhittleLikelihoodInference.upperbounds(::Type{Gaussian{K}}) where {K} = [Inf,Inf,Inf]

Gaussian(x::AbstractVector{Float64}) = Gaussian(1,1,1)
Gaussian(α,ωₚ,κ) = error("Gaussian process requires the ammount of aliasing specified as a type parameter. Use Gaussian{K}() where K ∈ N.")

Base.@propagate_inbounds @fastmath function WhittleLikelihoodInference.grad_add_sdf!(out, model::Gaussian, ω::Real)
    @boundscheck checkbounds(out,1:3)
    @inbounds begin
        ω = abs(ω)
        sdf = model.norm*exp(-(model.ωₚ-ω)^2 * model.halfκ⁻²)
        out[1] += sdf/model.α
        out[2] += 2(ω-model.ωₚ) * model.halfκ⁻² * sdf
        out[3] += sdf * ((ω-model.ωₚ)^2/model.κ^3-1/model.κ)
    end
    nothing
end


text:
module Sessions

using Genie, SHA, HttpServer, Cookies, App, Helpers, Router

mutable struct Session
  id::String
  data::Dict{Symbol,Any}
end
Session(id::String) = Session(id, Dict{Symbol,Any}())

export Session

if IS_IN_APP
  const session_adapter_name = string(App.config.session_storage) * "SessionAdapter"
  eval(parse("using $session_adapter_name"))
  const SessionAdapter = eval(parse(session_adapter_name))
end

"""
    id() :: String
    id(req::Request) :: String
    id(req::Request, res::Response) :: String

Generates a unique session id.
"""
function id() :: String
  try
    App.SECRET_TOKEN * ":" * bytes2hex(sha1(string(Dates.now()))) * ":" * string(rand()) * ":" * string(hash(Genie)) |> sha256 |> bytes2hex
  catch ex
    Logger.log("Session error", :err)
    Logger.log("$(@__FILE__):$(@__LINE__)", :err)
    error("Can't compute session id - please make sure SECRET_TOKEN is defined in config/secrets.jl")
  end
end
function id(req::Request) :: String
  ! isnull(Cookies.get(req, App.config.session_key_name)) &&
    ! isempty(Base.get(Cookies.get(req, App.config.session_key_name))) &&
      return Base.get(Cookies.get(req, App.config.session_key_name))

  id()
end
function id(req::Request, res::Response) :: String
  ! isnull(Cookies.get(res, App.config.session_key_name)) &&
    ! isempty(Base.get(Cookies.get(res, App.config.session_key_name))) &&
      return Base.get(Cookies.get(res, App.config.session_key_name))

  ! isnull(Cookies.get(req, App.config.session_key_name)) &&
    ! isempty(Base.get(Cookies.get(req, App.config.session_key_name))) &&
      return Base.get(Cookies.get(req, App.config.session_key_name))

  id()
end

"""
    start(session_id::String, req::Request, res::Response; options = Dict{String,String}()) :: Session
    start(req::Request, res::Response) :: Session

Initiates a session.
"""
function start(session_id::String, req::Request, res::Response; options = Dict{String,String}()) :: Session
  options = merge(Dict("Path" => "/", "HttpOnly" => "", "Expires" => "0"), options)
  Cookies.set!(res, App.config.session_key_name, session_id, options)

  load(session_id)
end
function start(req::Request, res::Response) :: Session
  start(id(req, res), req, res)
end

"""
    set!(s::Session, key::Symbol, value::Any) :: Session

Stores `value` as `key` on the `Session` `s`.
"""
function set!(s::Session, key::Symbol, value::Any) :: Session
  s.data[key] = value

  s
end

"""
    get(s::Session, key::Symbol) :: Nullable

Returns the value stored on the `Session` `s` as `key`, wrapped in a `Nullable`.
"""
function get(s::Session, key::Symbol) :: Nullable
  return  if haskey(s.data, key)
            Nullable(s.data[key])
          else
            Nullable()
          end
end

"""
    get!!(s::Session, key::Symbol)

Attempts to read the value stored on the `Session` `s` as `key` - throws an exception if the `key` does not exist.
"""
function get!!(s::Session, key::Symbol)
  s.data[key]
end

"""
    unset!(s::Session, key::Symbol) :: Session

Removes the value stored on the `Session` `s` as `key`.
"""
function unset!(s::Session, key::Symbol) :: Session
  delete!(s.data, key)

  s
end

"""
    is_set(s::Session, key::Symbol) :: Bool

Checks wheter or not `key` exists on the `Session` `s`.
"""
function is_set(s::Session, key::Symbol) :: Bool
  haskey(s.data, key)
end

"""
    persist(s::Session) :: Session

Generic method for persisting session data - delegates to the underlying `SessionAdapter`.
"""
function persist(s::Session) :: Session
  SessionAdapter.write(s)

  s
end

"""
    load(session_id::String) :: Session

Loads session data from persistent storage - delegates to the underlying `SessionAdapter`.
"""
function load(session_id::String) :: Session
  session = SessionAdapter.read(session_id)

  if isnull(session)
    return Session(session_id)
  else
    return Base.get(session)
  end
end

end


text:
module Coin

export
       # keys.jl
       Keys,
       generate_keys,
       get_pub_key,

       # addresses.jl
       private2wif,
       wif2private,
       wif_check_sum,
       pub2base58,

       # base58.jl
       encode58,
       decode58,
       decode58_to_array,

       # utils.jl
       reverse_endian,
       get_checksum,
       to_varint,
       to_varstring,
       bytearray

export BITCOIN_PROTOCOL_VERSION,
       SERVICES_NODE_NETWORK

##############################################################################
##
## Message exports
##
##############################################################################

export
       # messages.jl,
       Message,
       create_header,

       # tx.jl
       Tx,
       Tx_Input,
       OutPoint,
       Tx_Output,
       create_tx,
       get_tx,

       # version.jl
       NetworkAddress,
       Version

export magic_mainnet,
       magic_testnet,
       magic_testnet3,
       magic_namecoin

##############################################################################
##
## Dependencies
##
##############################################################################

using Crypto

##############################################################################
##
## Load files
##
##############################################################################

include("utils.jl")
include("base58.jl")
include("keys.jl")
include("addresses.jl")
include("messages/messages.jl")
include("signatures.jl")
include("op.jl")
include("server.jl")

Crypto.init()

# Version 0.9.3
const BITCOIN_PROTOCOL_VERSION = 93000

const SERVICES_NODE_NETWORK = 1

end # module Coin


text:
# Copyright (c) 2014: SCS.jl contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

# We don't want this file formatted because it contains lots of data.
#! format: off

using SparseArrays

# Random, feasible conic problem (no exponential or SDP cones)
# Problem data taken from https://github.com/cvxgrp/scs/blob/master/examples/raw/demo_data
function feasible_basic_conic(T)
    n = 11
    m = 112

    z = 0
    l = 100
    bu = Float64[]
    bl = Float64[]
    ep = 0
    ed = 0
    q = [12]
    s = Int[]

    b = [
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5]

    c = [
    -3.687045235791087894, -3.143065318037093103,  2.289111017442006180,
     1.817854752120232842,  1.515395029041534336, -0.621501577833336483,
     0.896216656096898490, -0.946791055758523736,  1.016669273321816114,
    -0.487162812452204275,  1.000000000000000000]

    colptr = [0, 101, 202, 303, 404, 505, 606, 707, 808, 909, 1010, 1012]

    rowval = [
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
    19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
    38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
    57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
    76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
    95, 96, 97, 98, 99, 101, 0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
    34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
    53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
    72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
    91, 92, 93, 94, 95, 96, 97, 98, 99, 102, 0,  1,  2,  3,  4,  5,  6,  7,  8,
     9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
    28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
    47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
    66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
    85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 103, 0,  1,  2,
     3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
    22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
    60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
    79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
    98, 99, 104, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
    35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
    54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
    73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
    92, 93, 94, 95, 96, 97, 98, 99, 105, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
    10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
    29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
    67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
    86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 106, 0,  1,  2,  3,
     4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
    23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
    42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
    61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
    80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
    99, 107, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
    36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
    55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
    74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
    93, 94, 95, 96, 97, 98, 99, 108, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
    11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
    49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
    68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
    87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 109, 0,  1,  2,  3,  4,
     5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
    24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
    43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
    62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
    81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
    110, 100, 111]

    values = [
     0.267905121455780493, -0.267905121455780493,  1.027917777157112633,
    -1.027917777157112633, -2.297179456574058865,  2.297179456574058865,
    -0.236264446681208273,  0.236264446681208273, -1.867877520107915412,
     1.867877520107915412,  0.162954278135808150, -0.162954278135808150,
     0.235147192773076164, -0.235147192773076164,  0.683684798830484719,
    -0.683684798830484719,  0.392483177863985011, -0.392483177863985011,
     0.924930282229331158, -0.924930282229331158, -2.159603434889884266,
     2.159603434889884266,  0.032954349530355143, -0.032954349530355143,
    -1.102218022704213984,  1.102218022704213984,  1.089305235192274290,
    -1.089305235192274290, -0.205884695396086725,  0.205884695396086725,
     0.662421406463353013, -0.662421406463353013,  0.693092774331567685,
    -0.693092774331567685, -0.477841384181042939,  0.477841384181042939,
     1.491230998886413417, -1.491230998886413417,  0.967521567131197524,
    -0.967521567131197524,  2.911450906122811766, -2.911450906122811766,
    -0.458525517948777994,  0.458525517948777994,  1.523410490461409861,
    -1.523410490461409861, -0.005653356349895766,  0.005653356349895766,
    -0.850527244660144954,  0.850527244660144954, -0.914427729368161080,
     0.914427729368161080, -0.366414500628216067,  0.366414500628216067,
     0.845776230367680237, -0.845776230367680237, -2.167303234827925085,
     2.167303234827925085, -0.311201875432801589,  0.311201875432801589,
    -1.343057018434765437,  1.343057018434765437,  0.329227476201254532,
    -0.329227476201254532,  0.937892636329581530, -0.937892636329581530,
    -1.292149283676578220,  1.292149283676578220,  0.727475864828183827,
    -0.727475864828183827, -0.865828722236433812,  0.865828722236433812,
    -0.712336606310262854,  0.712336606310262854, -1.416761181304521111,
     1.416761181304521111,  0.065959446153038476, -0.065959446153038476,
     0.538493444004808364, -0.538493444004808364, -0.117595299877949297,
     0.117595299877949297, -0.280761797896251886,  0.280761797896251886,
    -0.375803478543587532,  0.375803478543587532,  0.359440188545873895,
    -0.359440188545873895,  0.102984464805364467, -0.102984464805364467,
     0.677264337807959915, -0.677264337807959915,  1.688890914825171841,
    -1.688890914825171841,  1.415133737581891982, -1.415133737581891982,
    -0.260722295562210549,  0.260722295562210549,  0.105168432618356816,
    -0.105168432618356816, -1.000000000000000000,  1.889555946293464350,
    -1.889555946293464350, -0.360701715826208469,  0.360701715826208469,
    -0.791169413590648185,  0.791169413590648185, -0.750549444160155899,
     0.750549444160155899, -0.607358855980370871,  0.607358855980370871,
    -0.277021092833852400,  0.277021092833852400, -0.203825835441055647,
     0.203825835441055647, -0.090812111090438824,  0.090812111090438824,
     0.840648343850716073, -0.840648343850716073,  2.366017834555732335,
    -2.366017834555732335,  0.311512251535443596, -0.311512251535443596,
     1.294136754697726044, -1.294136754697726044,  1.721966354400402466,
    -1.721966354400402466, -0.394704235668438097,  0.394704235668438097,
    -0.866913724584800427,  0.866913724584800427, -0.521584096752930937,
     0.521584096752930937, -1.603058260481896147,  1.603058260481896147,
     1.131290495226984216, -1.131290495226984216, -0.025983716418195180,
     0.025983716418195180,  1.032351087100518372, -1.032351087100518372,
    -2.224637786217645186,  2.224637786217645186, -0.162734496391128941,
     0.162734496391128941,  0.273673555167950888, -0.273673555167950888,
    -2.459010134987294371,  2.459010134987294371, -0.918195490875242326,
     0.918195490875242326,  3.349596084069234259, -3.349596084069234259,
     0.687195476960778762, -0.687195476960778762, -1.052718851466527328,
     1.052718851466527328,  0.050624437594229355, -0.050624437594229355,
     0.918266891203016855, -0.918266891203016855, -0.402689924407274802,
     0.402689924407274802,  0.172930030148746722, -0.172930030148746722,
    -0.729742764543249600,  0.729742764543249600, -1.328294149765913312,
     1.328294149765913312,  0.074352549621726965, -0.074352549621726965,
     0.382668985189349498, -0.382668985189349498,  1.390238993188259498,
    -1.390238993188259498, -0.729630799636320049,  0.729630799636320049,
    -1.335330064808758044,  1.335330064808758044,  1.080203042138974112,
    -1.080203042138974112,  0.614815150749306483, -0.614815150749306483,
     0.948846205447098190, -0.948846205447098190,  0.537516306246460296,
    -0.537516306246460296,  0.518670152004236606, -0.518670152004236606,
     0.322774153261093044, -0.322774153261093044, -0.078317251196179785,
     0.078317251196179785, -0.638547209457334453,  0.638547209457334453,
    -0.310091732599216852,  0.310091732599216852, -1.401041709751936981,
     1.401041709751936981,  0.444484264649852523, -0.444484264649852523,
    -1.000000000000000000, -0.822571831606042192,  0.822571831606042192,
     1.149442322385366033, -1.149442322385366033,  0.691743212128930307,
    -0.691743212128930307, -0.724535264426504177,  0.724535264426504177,
    -0.770046062316186330,  0.770046062316186330, -1.007402320427817077,
     1.007402320427817077, -0.866896266710699970,  0.866896266710699970,
     0.157127863727930334, -0.157127863727930334, -0.683933911229745561,
     0.683933911229745561,  0.083157607370303074, -0.083157607370303074,
    -0.035297284818969786,  0.035297284818969786,  1.595145009715114970,
    -1.595145009715114970,  0.406340236572520330, -0.406340236572520330,
     0.313725728340701338, -0.313725728340701338,  1.802698956145747822,
    -1.802698956145747822,  0.913926791275037065, -0.913926791275037065,
    -1.724161676159960077,  1.724161676159960077,  1.110418687944275673,
    -1.110418687944275673,  0.461906200032139447, -0.461906200032139447,
    -1.098377187481418327,  1.098377187481418327,  0.561283369614635874,
    -0.561283369614635874, -1.440822605093403652,  1.440822605093403652,
     0.982226376107772414, -0.982226376107772414,  0.143410505344264055,
    -0.143410505344264055,  0.190094441263810476, -0.190094441263810476,
    -0.674859862131981347,  0.674859862131981347,  0.361191784792336323,
    -0.361191784792336323,  0.855409038211248252, -0.855409038211248252,
     0.247664495777954352, -0.247664495777954352, -2.624827919838180978,
     2.624827919838180978, -0.292697699274345136,  0.292697699274345136,
     1.816454414571746945, -1.816454414571746945, -1.762366799993794864,
     1.762366799993794864, -0.242523030834476544,  0.242523030834476544,
    -0.116759392895071146,  0.116759392895071146, -1.459080445461180098,
     1.459080445461180098,  0.418859403675993669, -0.418859403675993669,
     0.182519919352751275, -0.182519919352751275,  0.317673288988851388,
    -0.317673288988851388,  0.894501008979574186, -0.894501008979574186,
     0.240779273237376745, -0.240779273237376745,  1.063124478382744265,
    -1.063124478382744265, -0.182203074499369566,  0.182203074499369566,
     0.335540465956989664, -0.335540465956989664,  0.679568586001081942,
    -0.679568586001081942, -0.630620458948692009,  0.630620458948692009,
    -0.528865322760086354,  0.528865322760086354,  0.629959392835118481,
    -0.629959392835118481, -0.609885742617505167,  0.609885742617505167,
     1.774739631265793260, -1.774739631265793260, -1.000000000000000000,
     0.506497876068581654, -0.506497876068581654,  0.000326691662373364,
    -0.000326691662373364,  1.543000152238471312, -1.543000152238471312,
    -0.187951520155494600,  0.187951520155494600, -0.466908966395864100,
     0.466908966395864100, -2.168190935795662355,  2.168190935795662355,
     0.568204320157958742, -0.568204320157958742,  1.162818994214767310,
    -1.162818994214767310,  0.219565016076267133, -0.219565016076267133,
     0.846238367236053057, -0.846238367236053057,  0.444152474144777987,
    -0.444152474144777987,  0.432732607599171037, -0.432732607599171037,
     1.350501505879705411, -1.350501505879705411, -0.796531305332793926,
     0.796531305332793926, -1.500064621977569068,  1.500064621977569068,
     0.013262735417032061, -0.013262735417032061, -0.919545740399544842,
     0.919545740399544842, -0.720599621073430674,  0.720599621073430674,
     0.169023573624101114, -0.169023573624101114, -0.415746346580660175,
     0.415746346580660175,  1.447138808881193173, -1.447138808881193173,
     1.260731770052782030, -1.260731770052782030, -0.226090396655164727,
     0.226090396655164727,  0.412205318621164107, -0.412205318621164107,
     0.147480060602125151, -0.147480060602125151,  0.273383713767828274,
    -0.273383713767828274, -0.356808074324790336,  0.356808074324790336,
     1.013981805928862112, -1.013981805928862112,  0.269343540845133511,
    -0.269343540845133511, -0.131014546908719692,  0.131014546908719692,
     1.346897709728866133, -1.346897709728866133, -1.077804826671031835,
     1.077804826671031835, -2.166977323932548494,  2.166977323932548494,
     4.082665741988060759, -4.082665741988060759, -0.158050142170322011,
     0.158050142170322011,  0.826308257196147311, -0.826308257196147311,
     1.364531401616584372, -1.364531401616584372,  0.447820002980886034,
    -0.447820002980886034, -0.800383328883453427,  0.800383328883453427,
     0.638742958438863195, -0.638742958438863195,  1.284767930527563218,
    -1.284767930527563218, -1.777775442492336433,  1.777775442492336433,
    -0.479920638682654332,  0.479920638682654332, -0.000486121785261532,
     0.000486121785261532,  0.915943372086077567, -0.915943372086077567,
     0.281169314576583562, -0.281169314576583562, -0.240045850376803122,
     0.240045850376803122,  0.678876421485956394, -0.678876421485956394,
    -0.308976088076634348,  0.308976088076634348,  0.798465100342247380,
    -0.798465100342247380, -1.000000000000000000,  1.087159846595101786,
    -1.087159846595101786, -2.099121773424734716,  2.099121773424734716,
     0.955342180681646336, -0.955342180681646336,  0.805784538992392330,
    -0.805784538992392330,  0.372022079078506707, -0.372022079078506707,
     0.131125010573828527, -0.131125010573828527, -0.428233847101695153,
     0.428233847101695153,  0.239263979475934280, -0.239263979475934280,
     0.495824604297952665, -0.495824604297952665, -1.974307955242914581,
     1.974307955242914581,  1.603706793378771422, -1.603706793378771422,
     0.009079468662080679, -0.009079468662080679, -0.340801415852529821,
     0.340801415852529821, -0.040371866286134914,  0.040371866286134914,
     1.252279885978765384, -1.252279885978765384, -0.480496637097164170,
     0.480496637097164170,  0.025086653875817752, -0.025086653875817752,
     1.133441378387318510, -1.133441378387318510,  1.685216101290169188,
    -1.685216101290169188, -0.377749910040516390,  0.377749910040516390,
    -0.885742663004153163,  0.885742663004153163, -0.763260238467797114,
     0.763260238467797114, -2.779910894557495293,  2.779910894557495293,
    -0.639811141540734374,  0.639811141540734374,  0.187441762332883177,
    -0.187441762332883177, -0.284330518550652722,  0.284330518550652722,
    -0.822012436267475666,  0.822012436267475666, -1.334746162474092834,
     1.334746162474092834, -0.669135345166555928,  0.669135345166555928,
     2.095458091555622460, -2.095458091555622460, -1.489693812460274769,
     1.489693812460274769, -1.445874828327142092,  1.445874828327142092,
     0.885203250913691675, -0.885203250913691675, -0.130365900983153593,
     0.130365900983153593, -1.227811794109990418,  1.227811794109990418,
     1.887461183070288540, -1.887461183070288540, -0.372231541771805063,
     0.372231541771805063,  0.854005807614880763, -0.854005807614880763,
     0.233301251116458497, -0.233301251116458497,  0.128510580408158021,
    -0.128510580408158021,  0.317037146153163529, -0.317037146153163529,
    -0.072474722564739330,  0.072474722564739330, -0.902410880592201314,
     0.902410880592201314, -0.135741998878747605,  0.135741998878747605,
    -0.191584542689922477,  0.191584542689922477, -0.691971672385520598,
     0.691971672385520598, -0.740985851503054516,  0.740985851503054516,
     0.777878742705346071, -0.777878742705346071,  2.316421323974115154,
    -2.316421323974115154,  0.442592141778819392, -0.442592141778819392,
    -1.000000000000000000,  0.057300638002260783, -0.057300638002260783,
    -0.765880201091782453,  0.765880201091782453,  0.018658948070564667,
    -0.018658948070564667, -0.884852857487954947,  0.884852857487954947,
    -0.119099061784913321,  0.119099061784913321,  0.110823913892580964,
    -0.110823913892580964, -0.628128807378484644,  0.628128807378484644,
    -0.958779880814327745,  0.958779880814327745,  0.335534085713524222,
    -0.335534085713524222, -1.589119342626025944,  1.589119342626025944,
    -0.339290158527664643,  0.339290158527664643,  0.683350252109244716,
    -0.683350252109244716, -1.341947200350975145,  1.341947200350975145,
    -1.094209818133984768,  1.094209818133984768, -1.303705781186516477,
     1.303705781186516477,  0.205495234849035424, -0.205495234849035424,
     2.150190108595682936, -2.150190108595682936,  0.504922856191401204,
    -0.504922856191401204,  0.202643337241746463, -0.202643337241746463,
     0.368081470693641333, -0.368081470693641333,  0.559511763513307891,
    -0.559511763513307891,  0.621090751359941073, -0.621090751359941073,
    -0.437128202629269713,  0.437128202629269713, -0.320080245358165705,
     0.320080245358165705,  0.676285408860144432, -0.676285408860144432,
     0.294205824825271300, -0.294205824825271300,  1.086292218045164404,
    -1.086292218045164404,  0.131665472918444160, -0.131665472918444160,
     0.913562447586867998, -0.913562447586867998, -0.441621943495057401,
     0.441621943495057401,  0.175143091644549165, -0.175143091644549165,
    -0.057950142634599001,  0.057950142634599001,  1.272633458197298228,
    -1.272633458197298228, -0.739156567900068118,  0.739156567900068118,
    -1.172962844785649050,  1.172962844785649050,  0.690349067302703512,
    -0.690349067302703512,  1.261164997660849663, -1.261164997660849663,
    -0.578273293013085943,  0.578273293013085943,  0.705747556964109291,
    -0.705747556964109291,  1.199347759194084206, -1.199347759194084206,
    -0.561175893138310178,  0.561175893138310178,  0.418058667159627584,
    -0.418058667159627584,  0.775233786194539753, -0.775233786194539753,
    -1.005165576137645900,  1.005165576137645900, -0.248095930746197624,
     0.248095930746197624,  0.968389188406472812, -0.968389188406472812,
     0.052644854909029225, -0.052644854909029225,  0.459974856433697621,
    -0.459974856433697621, -0.764234718892679465,  0.764234718892679465,
    -0.924252504077109682,  0.924252504077109682, -1.000000000000000000,
    -2.285037536291396609,  2.285037536291396609,  0.330256994776636892,
    -0.330256994776636892,  0.781166851577503984, -0.781166851577503984,
     1.091855391225416527, -1.091855391225416527,  0.303546598398528267,
    -0.303546598398528267,  0.883205583346516998, -0.883205583346516998,
    -0.200432630370076942,  0.200432630370076942,  0.103227093467374034,
    -0.103227093467374034, -0.536598825966995818,  0.536598825966995818,
    -0.114094330489527385,  0.114094330489527385,  0.352868274327804876,
    -0.352868274327804876,  1.184201821029249313, -1.184201821029249313,
    -0.281707700532443983,  0.281707700532443983,  1.299996056001185840,
    -1.299996056001185840,  1.288620739680663041, -1.288620739680663041,
    -0.124775257491364810,  0.124775257491364810,  2.676893190719289439,
    -2.676893190719289439,  0.393601576244927809, -0.393601576244927809,
    -0.860157510975565920,  0.860157510975565920, -0.162471839540177465,
     0.162471839540177465,  0.870279361361823600, -0.870279361361823600,
    -0.150491539662725049,  0.150491539662725049, -1.332287989766246072,
     1.332287989766246072,  0.065469192080399141, -0.065469192080399141,
    -1.317974937874615282,  1.317974937874615282,  0.509029000143902399,
    -0.509029000143902399,  0.368719922900173025, -0.368719922900173025,
     0.646131466361322016, -0.646131466361322016, -0.791339007906923575,
     0.791339007906923575, -0.438686479862148759,  0.438686479862148759,
     1.538459427564934812, -1.538459427564934812, -0.258833298230582876,
     0.258833298230582876, -1.504378351654444890,  1.504378351654444890,
    -1.036168922551806260,  1.036168922551806260, -1.956795979420658416,
     1.956795979420658416,  1.136562859433311701, -1.136562859433311701,
    -1.534637925681490822,  1.534637925681490822, -0.794072741149785766,
     0.794072741149785766,  1.371420904545398223, -1.371420904545398223,
     0.507731197862471229, -0.507731197862471229, -0.301106820285413468,
     0.301106820285413468, -1.017488307329072361,  1.017488307329072361,
    -0.614101742707810261,  0.614101742707810261, -0.781398901846928728,
     0.781398901846928728,  0.053926155630600305, -0.053926155630600305,
    -0.528096222539175431,  0.528096222539175431,  1.913029349947439561,
    -1.913029349947439561, -0.179790546417506469,  0.179790546417506469,
    -0.683750983213183505,  0.683750983213183505, -1.021241699433223404,
     1.021241699433223404, -1.000000000000000000, -2.049890895857636064,
     2.049890895857636064, -1.417369090725334679,  1.417369090725334679,
    -0.850006203774544056,  0.850006203774544056,  0.462163832152883536,
    -0.462163832152883536, -1.576268635512163341,  1.576268635512163341,
    -1.441553193165409752,  1.441553193165409752,  0.528158464893624058,
    -0.528158464893624058, -0.356925550618781839,  0.356925550618781839,
     1.025569158189527652, -1.025569158189527652,  0.449741824505590193,
    -0.449741824505590193,  0.832324652288013667, -0.832324652288013667,
    -1.071955389109310985,  1.071955389109310985, -1.634162605612502706,
     1.634162605612502706,  0.505770152619639646, -0.505770152619639646,
    -0.529050505036871677,  0.529050505036871677,  2.096511832225588901,
    -2.096511832225588901,  0.761631727644857093, -0.761631727644857093,
    -1.192983489646781026,  1.192983489646781026,  2.417357758103033216,
    -2.417357758103033216, -0.054862528716085407,  0.054862528716085407,
    -0.911480249686436483,  0.911480249686436483, -0.754953036467722605,
     0.754953036467722605, -0.765582251486999321,  0.765582251486999321,
     0.336077875984923224, -0.336077875984923224,  0.627961935625664491,
    -0.627961935625664491, -0.048378255106885454,  0.048378255106885454,
     0.324089280340306485, -0.324089280340306485,  0.867067055827554611,
    -0.867067055827554611,  0.482964936776505871, -0.482964936776505871,
     0.913512419073207127, -0.913512419073207127, -0.986143056174743848,
     0.986143056174743848,  0.106234733647867743, -0.106234733647867743,
     0.600980235748239666, -0.600980235748239666, -0.586035595820676769,
     0.586035595820676769, -0.480578250645586014,  0.480578250645586014,
    -1.153384062348248751,  1.153384062348248751, -1.171239036967019365,
     1.171239036967019365, -0.322604826885027784,  0.322604826885027784,
     0.136050508789379743, -0.136050508789379743,  1.065246013053737562,
    -1.065246013053737562, -0.601401671319884090,  0.601401671319884090,
     0.095447184573948712, -0.095447184573948712,  2.265102206815573727,
    -2.265102206815573727, -0.205975175263903992,  0.205975175263903992,
     0.427509540011176081, -0.427509540011176081,  0.520441665490702587,
    -0.520441665490702587, -2.895556119432680209,  2.895556119432680209,
     0.620757007249324810, -0.620757007249324810, -0.005251762123618039,
     0.005251762123618039,  1.083172978556625754, -1.083172978556625754,
    -1.000000000000000000,  0.365201243908861106, -0.365201243908861106,
     1.250190028804355702, -1.250190028804355702,  0.224932134413901919,
    -0.224932134413901919, -0.902251105448115265,  0.902251105448115265,
    -0.368544518082968842,  0.368544518082968842, -0.234274596888654818,
     0.234274596888654818, -1.199930348307246541,  1.199930348307246541,
    -1.573215897391668561,  1.573215897391668561,  1.509172423622493930,
    -1.509172423622493930, -0.092660720516525047,  0.092660720516525047,
     0.436287483119169650, -0.436287483119169650,  2.405987267352854619,
    -2.405987267352854619,  0.831037192863362639, -0.831037192863362639,
     0.142705011000706122, -0.142705011000706122,  1.491265436361660868,
    -1.491265436361660868,  0.351484213706070359, -0.351484213706070359,
    -1.520421211131988359,  1.520421211131988359, -0.990651120099914628,
     0.990651120099914628,  0.755067493672610501, -0.755067493672610501,
    -0.475209396547241036,  0.475209396547241036, -1.267282701791565547,
     1.267282701791565547,  0.419704110261967744, -0.419704110261967744,
    -0.766570120470680960,  0.766570120470680960, -1.021465600725925960,
     1.021465600725925960, -0.521511779651205876,  0.521511779651205876,
     0.374754396256747835, -0.374754396256747835,  0.785689533273488006,
    -0.785689533273488006, -0.158926489386465586,  0.158926489386465586,
     1.132984272112302016, -1.132984272112302016,  0.957264734680358442,
    -0.957264734680358442, -0.404576492822195655,  0.404576492822195655,
     0.589263426043603378, -0.589263426043603378, -1.033631870548684217,
     1.033631870548684217,  0.598534902235807076, -0.598534902235807076,
     0.919327935687768494, -0.919327935687768494, -0.224031230235764156,
     0.224031230235764156, -0.611669152956875184,  0.611669152956875184,
     1.482839170994118660, -1.482839170994118660, -0.094214130236777896,
     0.094214130236777896, -0.902166146440054773,  0.902166146440054773,
    -0.248954946055154891,  0.248954946055154891, -3.062190912378776719,
     3.062190912378776719, -0.941533647749983493,  0.941533647749983493,
    -2.160942710456435822,  2.160942710456435822,  0.108026826423921113,
    -0.108026826423921113,  2.075626678312805673, -2.075626678312805673,
     0.873962816238563089, -0.873962816238563089, -0.408815616016248828,
     0.408815616016248828, -0.129621031340232146,  0.129621031340232146,
     1.406273548026924392, -1.406273548026924392, -1.000000000000000000,
    -0.915804710938513011,  0.915804710938513011, -1.221871953979475789,
     1.221871953979475789, -0.300753582392023655,  0.300753582392023655,
     0.075403833175464410, -0.075403833175464410,  0.131548564249591410,
    -0.131548564249591410, -0.071935913309509686,  0.071935913309509686,
     0.767128678708612011, -0.767128678708612011,  1.191450868677329078,
    -1.191450868677329078, -1.613612865536336205,  1.613612865536336205,
    -0.441167481116289062,  0.441167481116289062,  3.280667499979234236,
    -3.280667499979234236, -1.201795592615207742,  1.201795592615207742,
     2.951958398205375200, -2.951958398205375200, -1.276239289802024945,
     1.276239289802024945, -1.132028638493809636,  1.132028638493809636,
     2.436513002643889259, -2.436513002643889259,  0.068454839838739664,
    -0.068454839838739664,  0.258744697658441825, -0.258744697658441825,
     0.912475983862615192, -0.912475983862615192,  0.079789089547489894,
    -0.079789089547489894, -0.095257282931539802,  0.095257282931539802,
     0.032098955389624276, -0.032098955389624276, -0.446625802398977478,
     0.446625802398977478,  0.066898135600142933, -0.066898135600142933,
     0.187007172038859970, -0.187007172038859970, -0.111876955691751587,
     0.111876955691751587, -1.135249689741995027,  1.135249689741995027,
     0.855383920893877270, -0.855383920893877270, -0.584066032000734703,
     0.584066032000734703,  0.043977907469829255, -0.043977907469829255,
    -1.235073226970094584,  1.235073226970094584,  0.903396805744995368,
    -0.903396805744995368, -1.419367800674472990,  1.419367800674472990,
    -0.669079320858145477,  0.669079320858145477,  1.846743883979396816,
    -1.846743883979396816, -0.197864347556086784,  0.197864347556086784,
     0.178086679323349178, -0.178086679323349178, -0.726541129368893035,
     0.726541129368893035,  0.102548417306546763, -0.102548417306546763,
     0.101435234812034022, -0.101435234812034022, -0.607294919138270162,
     0.607294919138270162, -0.232543338993652815,  0.232543338993652815,
     1.024873011278554902, -1.024873011278554902, -0.367070741104161269,
     0.367070741104161269,  0.015584587754633259, -0.015584587754633259,
     0.893352765523915404, -0.893352765523915404, -0.318598602767036110,
     0.318598602767036110, -0.565309322249061386,  0.565309322249061386,
    -0.869520326873197580,  0.869520326873197580,  1.087601931698385149,
    -1.087601931698385149, -1.000000000000000000, -1.000000000000000000,
    -1.000000000000000000]

    A = SparseMatrixCSC(m, n, colptr .+ 1, rowval .+ 1, vec(values))
    P = spzeros(n, n)

    sol = scs_solve(T, m, n, A, P, b, c, z, l, bu, bl, q, s, ep, ed, Float64[])
    @test sol.ret_val == 1
    @test SCS.raw_status(sol.info) == "solved"
end

# Feasible conic problem with exponential cones (no SDP cones)
# Problem data taken from https://github.com/cvxgrp/scs/blob/master/examples/raw/randomConeFeasible
function feasible_exponential_conic(T)
    n = 24
    m = 71
    z = 10
    l = 10
    bu = Float64[]
    bl = Float64[]
    ep = 10
    ed = 5
    q = [0,1,0,2,3]
    s = Int[]

    b = [
    -0.883995908378185202,  0.247092008779755457,  0.000000000000000000,
    -0.908862166109580594,  0.493908919902298627,  0.215008446366246719,
    -0.756619621167477718, -0.244711519515723036, -2.185656700286640941,
    -1.777454077348217920,  1.191220488872941186, -0.072631913036035556,
    -0.884888299606113149, -3.097264426296667761, -0.491714244502094944,
     0.820612279439398518, -5.027612089838516596, -0.218768971520485273,
    -1.779020210515877221,  0.000000000000000000,  1.049908711514835780,
     0.423746551400514093, -0.078381703192688112, -0.387520318558316479,
     0.959884766721948446, -0.080140759738691447,  1.300716537987696597,
    -0.217623259095584554, -0.772345449741623336, -0.014345948596158398,
     2.002325668310177775,  2.019795133016943467,  1.285967533112210681,
    -0.503689333815666940,  3.004968290408826626, -1.438380292815098382,
    -0.801360722211995502,  0.731209144814713885, -2.639460367287462361,
     1.255602400085220394, -0.301008710647552635,  0.478491654540030953,
    -0.984259121708334739,  0.276318763804415002,  0.631004508865874447,
     1.808914085971600416,  0.379215625232032671, -0.627707287528726510,
    -1.823589846592805763,  0.000000000000000000,  0.376255266738256766,
     0.577717062577191354,  1.119478389802739615,  0.977520182221270906,
     0.000000000000000000,  3.417141674635829851, -0.093825387556597556,
    -1.780472058822893722,  1.589707484189159858,  0.133747881756402398,
     1.316803247114450848, -0.153152367127352751, -1.712195041092097192,
    -0.151457896071195447,  1.216498217552836492,  0.610011299767568738,
     4.095401319683990238, -0.761957853948331154, -0.485158172250699793,
    -1.990510613776122817,  1.151843011380725867]

    c = [
     1.126284961639569149,  0.474697659805094763, -2.123033959497108825,
     4.908405709073863754,  1.205680866991396316, -0.305211910012288978,
    -6.293810724494532494, -0.698794364791622713,  2.279737335846038437,
    -0.945550079347313055, -0.727867522608193762, -1.869349988323733891,
    -3.748045451887451307,  1.125456539838578518,  0.750188573436658457,
     1.255406629974515686, -4.887321053728165055,  4.021564105931883404,
     0.000000000000000000, -5.091850958321861853, -0.068986990722853969,
    -1.715673120575787136,  0.036366854219751360, -3.625427280331933400]

    colptr = [0, 7, 12, 18, 24, 34, 37, 43, 47, 54, 59, 68, 79, 87, 93, 101, 111, 119, 127, 128, 139, 141, 150, 154, 162]

    rowval = [
     5, 17, 22, 31, 55, 65, 69, 12, 18, 30, 34, 58,  9, 11, 13, 18, 30, 66,  0,
    10, 17, 18, 44, 58,  5,  6,  7, 26, 29, 31, 53, 57, 61, 67, 39, 43, 68,  3,
     9, 13, 14, 16, 21,  7, 36, 38, 55,  4, 12, 21, 27, 40, 46, 66,  3, 26, 34,
    44, 53,  5, 11, 17, 27, 31, 37, 42, 45, 63, 13, 22, 24, 33, 36, 40, 50, 56,
    58, 64, 65,  6, 10, 13, 16, 18, 23, 41, 44, 17, 31, 41, 46, 55, 58, 16, 20,
    33, 38, 42, 58, 62, 64,  7, 13, 18, 21, 26, 36, 45, 52, 56, 60,  8, 15, 21,
    30, 33, 48, 53, 64,  1,  5, 11, 16, 23, 24, 28, 39, 37,  5,  8, 28, 30, 41,
    55, 57, 59, 61, 64, 67, 56, 70,  0,  3, 10, 18, 22, 34, 37, 42, 65, 16, 25,
    51, 65,  5, 13, 24, 28, 32, 36, 40, 55]

    values = [
     0.155488995903893978, -0.062791225718324961, -0.723631125289072807,
    -0.238301504589733010, -1.222593380186629552,  0.176577794642316321,
     0.705885019349415832, -1.327043149520291010, -0.054886129988677361,
     0.595357673884101768, -0.608557444738319409, -0.345065971567320995,
     0.312023828329273611,  0.600142508808629960,  0.401844497039814952,
    -1.118732002452724972,  1.046832784305231856,  2.003390610862264776,
    -0.289963040800027938, -0.723121479570418058, -2.021958930051789505,
    -0.626378538867138679,  1.223062551733806957,  1.012801864262980134,
     0.818551368521000522, -0.493009815316450606,  0.045841105713705040,
     0.571247629714476068, -0.131820352915893585,  0.229596893220313791,
     0.245804851893843240, -0.300111005615675885, -0.438141355602143678,
    -0.432003843727780446,  2.787335227813435345,  1.778255899291004472,
    -0.360076301153971601,  1.174116751493714972,  1.804493771724186901,
     1.470201280848519421,  0.397466995795308642,  1.516266896645952711,
     1.808862620519253950, -0.063783120022332648,  0.910482579647111989,
     0.439952188872439920,  0.316500360797716096, -1.481399071578779925,
    -1.441013597638914456, -1.079866250738828137,  0.759568325914783404,
    -1.854299082689693901,  0.066756911436865754,  0.950993499549085408,
     0.126947068043645916,  0.412796010311391848, -0.737059771697805455,
    -1.283256104604766579,  0.070045209416807905, -0.292588130834394000,
     0.593930795648752730, -0.982131525779047831, -0.657201299098350344,
     0.439997904822629349,  0.183703423091249041,  0.541334435719376117,
     0.901931466951714444, -0.297697144009373027, -0.326814228785906158,
    -0.593250315010803608, -0.373070658631004248,  0.601102032468295078,
     0.867082552947325391, -1.140681144669632330, -0.069214254022048366,
    -0.418903195029848852,  0.629334584931418806,  1.143678910770957513,
     0.970737823554688517, -0.180739356415037467,  0.526547037749559022,
     0.812323004640795987, -0.032566509194480744,  0.249517740562795132,
     0.942133319236942435, -0.433609296474823602, -2.328954516283341825,
     0.612511298166949270, -0.616865928889227422, -0.168469878275448348,
     0.035479485837577006, -1.342869236621930140, -0.213015082641054704,
     1.635999657278292441, -0.640709506726057532,  0.092307951238962266,
     0.101662443700341151,  0.389266203843263880, -0.865697308360523721,
     0.983545235205556256, -0.531620117507069279,  0.611335194065235954,
     0.545540103526115816, -0.993019006549600625,  0.199189444075264455,
    -0.986961883351521907, -0.079892839058037096, -1.835638683735192345,
     0.235809672576244050, -0.140321722056366061, -0.270068812648098844,
     0.109317693774870342, -0.751894738681891606, -1.521026561768954677,
    -0.197958632611841989,  1.729841391572364273,  2.227168078167871546,
    -0.608580510079949288,  0.972565728008653152,  0.475424811707271355,
    -0.540786416488525767, -2.186021612748235476, -0.425058490610030815,
     0.300485967595730263,  0.815488508092498399, -0.603918481376169214,
    -1.166665030194641073,  0.290790134884453599, -0.308641815280113085,
     1.814015450280375585,  0.176946822329411252,  0.327678163907200726,
    -0.218533560026580531, -1.032184344072171189,  1.029365712103099240,
    -1.043108301337626775, -0.408674314796325655, -0.522250484993548869,
     0.648940737363586706,  0.899822328897223467,  1.415849068436585423,
     1.261550718141148275, -0.656815928948082495, -0.260250861706116776,
     0.974950224811311550,  0.401336339818801069, -1.749879306387625455,
     0.112944717021051230,  0.751228984688332879, -0.413972274974230170,
     0.589433366718220664,  0.120205281956355367, -0.507323064614498387,
    -0.438270518204901538, -1.096593301525472075, -1.051632309520848407,
     0.798886992156715880, -0.307503469862750567,  0.274836786911666187,
     0.898475989377141793, -1.093343456239604494,  1.331215885064973348]

    A = SparseMatrixCSC(m, n, colptr .+ 1, rowval .+ 1, vec(values))
    P = spzeros(n, n)

    sol = scs_solve(T, m, n, A, P, b, c, z, l, bu, bl, q, s, ep, ed, Float64[])
    @assert sol.ret_val == 1
    @test SCS.raw_status(sol.info) == "solved"
end

# Feasible conic problem with exponential and SDP cones
# BLAS and LAPACK must be linked
# Problem data taken from https://github.com/cvxgrp/scs/blob/master/examples/raw/randomConeFeasibleSDP
function feasible_sdp_conic(T)
    n = 15
    m = 46
    z = 3
    bu= Float64[]
    bl= Float64[]
    l = 3
    ep = 2
    ed = 2
    q = [2,3,4]
    s = [2,3,4]

    b = [
    -0.757244022903780567,  0.000000000000000000, -1.892151461056349238,
    -0.163643541176375057,  0.000000000000000000, -1.081834184990333014,
     0.795686782662209935, -0.459554179861930256,  0.000000000000000000,
    -0.021401100284139062,  0.221822131770426145,  0.624165617064677769,
     0.103992209313486683,  0.383906342184775817,  0.220548856781228653,
     0.207179552343107032, -0.251552024267611252, -0.940985378402204531,
     0.143203475268773417, -0.637990380166112425, -0.047725168844638927,
    -1.400142610548834821, -0.518640886812063995, -0.466344476203716551,
    -0.306977592331722393, -0.733266642509912470,  1.636614964482892454,
     2.801678431332372821,  0.542022232315772401,  0.561733632830300200,
    -2.792150430434448882,  0.679252001011406148,  0.060935700608246268,
     1.017184097064012693, -1.472726175247485836,  0.298927350489858801,
     0.283700004781893000, -5.946289176766917706,  0.563505302268017871,
     1.728907659188164914,  0.783139219605003434,  0.241447041607357943,
    -0.100769851306178021, -0.312858596637428432,  0.715583273231235939,
     0.030051296196268559]

    c = [
    -0.159052113684804763,  1.195574970454808206,  0.354942838344536771,
    -0.144003787891437030,  0.031108987439201756, -6.024128094309364911,
    -6.684198760055235056, -2.875770597510225901, -1.958442177032014397,
    -3.231061172173861795, -0.047201649069278366, -1.907845683517030544,
     0.605113199091042553,  2.360164164701050726, -0.367619298880400125]
    colptr = [0, 3, 11, 18, 24, 25, 30, 36, 39, 44, 47, 48, 51, 56, 61, 66]
    rowval = [
     7, 26, 27,  3,  5,  9, 15, 26, 28, 31, 34,  7, 12, 21, 25, 32, 38, 44, 10,
    11, 16, 35, 36, 40, 10, 11, 14, 24, 26, 34, 11, 12, 16, 20, 38, 42, 13, 34,
    42,  3,  6, 11, 15, 29, 30, 34, 37, 31,  2,  5, 42, 11, 20, 22, 24, 34,  6,
    17, 23, 25, 44,  0, 21, 30, 32, 42];
    values = [
    -0.067865553542687335,  1.530072514424096086,  1.603457298120044117,
    -0.334886938964047698,  1.039090653504955997, -0.217606350143191934,
     0.307061919146703444, -0.249024742513713787,  1.234679146890777846,
    -0.155941035724768945,  0.391894209432449070, -0.195221197898754362,
     0.625190357087625714, -0.532011376808820713, -0.192239517539274762,
    -0.261163645776478603, -0.457014640871582556, -1.565056014150725039,
    -0.303107621351740908,  0.051290355848774699,  0.515246335524848553,
    -0.320575506600239257,  0.012469041361617951, -1.066701398984749583,
     0.023045624425105282,  0.826062790211595455,  0.949221831131022542,
    -0.712004549027422495, -1.064213412889326804, -1.250678906826407477,
     1.526976686733372723,  0.183227263001436963,  0.261406324055382666,
    -0.162337672803827743,  1.242448406390738391,  0.933728162671238482,
    -1.029767543566621146, -0.947960922331432032,  0.350321001356111605,
     0.552783345944550142,  1.260658709120896281,  0.466914435684700035,
     0.135174942099455658, -0.229626450963180490, -1.506159703979718989,
    -0.741106093940411492, -3.029177341404145629,  0.276068253931536167,
    -0.848709379933659025, -1.117638683265208099, -0.029005763708726321,
    -0.209713338388736709, -0.146054634331526184, -0.875729346160017297,
    -1.174212331456816250, -0.507817550278173657,  0.660143141046977688,
    -0.941485770955433732, -0.483815050110121103, -0.274070229932602216,
    -0.084539479817724195,  0.455029556444334349,  1.682103594663178825,
    -0.444627816446985402,  0.443421912904091331,  0.182452167505983420]
    A = SparseMatrixCSC(m, n, colptr .+ 1, rowval .+ 1, vec(values))
    P = spzeros(n, n)
    sol = scs_solve(T, m, n, A, P, b, c, z, l, bu, bl, q, s, ep, ed, Float64[])
    @test sol.ret_val == 1
    @test SCS.raw_status(sol.info) == "solved"
end

# Feasible conic problem with power cones
function feasible_pow_conic(T)
    n = 9
    m = 28
    z = 5
    l = 5
    bu = Float64[]
    bl = Float64[]
    ep = 0
    ed = 0
    q = Int[]
    s = Int[]
    p = [-3.0e-01, 2.5e-01, 7.5e-01, -4.0e-01, 8.7e-01, -1.2e-01]
    b = [
    -0.887386390749058451,  1.868842541924223610, -0.755288651776904296,
     0.000000000000000000,  0.000000000000000000,  0.000000000000000000,
    -0.242379421582613414,  0.000000000000000000,  0.000000000000000000,
     0.000000000000000000,  2.000257252427916743,  0.841870176542910631,
    -1.245982120437818974,  0.644136460056986104, -0.068037985179701416,
     0.041769615282582828,  1.081088773255733271,  1.417941447788496845,
     0.955133254978043800,  2.477002457942629299,  0.326964373865085811,
     0.322926943542406031,  0.765172439731569787,  1.673745381540497679,
     0.056224953164704453,  0.024308325296160227, -0.492458408194795028,
    -0.193643481641372683]
    c = [
     0.581324380640174887,  0.000000000000000000, -1.274914899429569370,
     1.437959547820705897,  0.178499636115030086, -2.834480531039399676,
    -0.465471220509626749,  0.440187271984058437,  1.655006387767208542];
    colptr = [0, 2, 2, 8, 11, 12, 15, 21, 22, 25]
    rowval = [11, 19, 10, 11, 14, 18, 19, 26, 1, 19, 23, 2, 1, 14, 19, 0, 1, 10, 14, 23, 26, 16, 0, 6, 18]
    values = [
    -0.511172207780700494, -0.557093642241281661, -0.322939921204497105,
    -0.002041345349432955,  1.606510961119237413, -0.070499387778693703,
    -0.336705699002853465,  1.122647857944874650,  0.375041023696103593,
     0.415227462723155749, -2.444298897865559539, -2.377454293765433491,
     1.125161817875028225,  0.847648634500925313,  1.557813537123208025,
     0.400486023191097329,  0.728641591773904751,  0.317987915650739417,
     0.268100811901575309, -1.098195387799324019,  0.581667258045274083,
    -0.923489085784077401, -1.341380722378574086, -0.273782415743900387,
     0.147891351014747152]
    A = SparseMatrixCSC(m, n, colptr .+ 1, rowval .+ 1, vec(values))
    P = spzeros(n, n)
    sol = scs_solve(T, m, n, A, P, b, c, z, l, bu, bl, q, s, ep, ed, p)
    @test sol.ret_val == 1
    @test SCS.raw_status(sol.info) == "solved"
    return
end

function feasible_basic_problems(solver)
    A = reshape([1.0], (1, 1))
    P = spzeros(1, 1)
    solution = scs_solve(
        solver,
        1,
        1,
        A,
        P,
        [1.0],
        [1.0],
        1,
        0,
        Float64[],
        Float64[],
        Int[],
        Int[],
        0,
        0,
        Float64[],
    );
    @test solution.ret_val == 1
    feasible_basic_conic(solver)
    feasible_exponential_conic(solver)
    feasible_sdp_conic(solver)
    feasible_pow_conic(solver)
    return
end

function test_options(T)
    A = [
        1.0 1.0 0.0 0.0 0.0
        0.0 1.0 0.0 0.0 1.0
        0.0 0.0 1.0 1.0 1.0
        -1.0 0.0 0.0 0.0 0.0
        0.0 -1.0 0.0 0.0 0.0
        0.0 0.0 -1.0 0.0 0.0
        0.0 0.0 0.0 -1.0 0.0
        0.0 0.0 0.0 0.0 -1.0
    ]
    m, n = size(A)
    args = (
        m = m,
        n = n,
        A = A,
        P = zeros(n, n),
        b = [5.0, 3.0, 9.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        c = -[3.0, 4.0, 4.0, 9.0, 5.0],
        z = 0,
        l = 8,
        bu = Float64[],
        bl = Float64[],
        q = Int64[],
        s = Int64[],
        ep = 0,
        ed = 0,
        p = Float64[],
    )
    solution = SCS.scs_solve(T, args...)
    @test isapprox(solution.x' * args.c, -99.0; rtol = 1e-4)
    @test !isapprox(solution.x' * args.c, -99.0; rtol = 1e-7)
    solution = SCS.scs_solve(T, args...; eps_abs = 1e-10, eps_rel = 1e-10)
    @test isapprox(solution.x' * args.c, -99.0; rtol = 1e-10)
    solution = SCS.scs_solve(
        T,
        args...,
        solution.x,
        solution.y,
        solution.s;
        max_iters = 1,
        warm_start = true,
        eps_abs = 1e-10,
        eps_rel = 1e-10,
    )
    @test isapprox(solution.x' * args.c, -99.0; rtol = 1e-9)
    @test_throws(ArgumentError, SCS.scs_solve(T, args...; eps = 1e-12))
    err = try
        SCS.scs_solve(T, args...; eps_abs = 1e-12, eps = 1e-12)
    catch ex
        ex
    end
    @test err.msg == "Unrecognized option passed to SCS solver: eps"

    tmpf = tempname()
    @test !isfile(tmpf)
    SCS.scs_solve(T, args...; eps_abs = 1e-12, write_data_filename = tmpf)
    @test isfile(tmpf)

    @test_throws ErrorException SCS.scs_solve(T, args...; write_data_filename = @view tmpf[1:end])
    return
end


text:
using sem, Feather, ModelingToolkit, Statistics, LinearAlgebra,
    Optim, SparseArrays, Test, Zygote, LineSearches, ForwardDiff,
    BenchmarkTools, ProfileView

## Observed Data #############################################################

data_small = Feather.read("test/comparisons/data_unique_small.feather")
data_big = Feather.read("test/comparisons/data_unique_big.feather")
data_huge = Feather.read("test/comparisons/data_unique_huge.feather")

data_def_small = Feather.read("test/comparisons/data_def_unique_small.feather")
data_def_big = Feather.read("test/comparisons/data_def_unique_big.feather")
data_def_huge = Feather.read("test/comparisons/data_def_unique_huge.feather")

pars_small = Feather.read("test/comparisons/pars_unique_small.feather")
pars_big = Feather.read("test/comparisons/pars_unique_big.feather")
pars_huge = Feather.read("test/comparisons/pars_unique_huge.feather")

## small ############################################################

semobserved_small =
    SemObsCommon(data = Matrix(data_small); meanstructure = true)

@ModelingToolkit.variables x[1:7], m[1:2], load_t[1:4]

S =[x[1]  0     0     0     0     0
    0     x[2]  0     0     0     0
    0     0     x[3]  0     0     0
    0     0     0     x[4]  0     0
    0     0     0     0     x[5]  x[7]
    0     0     0     0     x[7]  x[6]]

F =[1.0 0 0 0 0 0
    0 1 0 0 0 0
    0 0 1 0 0 0
    0 0 0 1 0 0]

A =[0  0  0  0  1.0  load_t[1]
    0  0  0  0  1  load_t[2]
    0  0  0  0  1  load_t[3]
    0  0  0  0  1  load_t[4]
    0  0  0  0  0  0
    0  0  0  0  0  0]

M = [0.0, 0, 0, 0, m[1:2]...]

S = sparse(S)

#F
F = sparse(F)

#A
A = sparse(A)

data_def_small = Matrix(data_def_small)

start_val = [1.0, 1, 1, 1, 1, 1, 1, 1.0, 1.0]

imply_small = ImplySymbolicDefinition(
    A,
    S,
    F,
    M,
    [x[1:7]..., m[1:2]...],
    load_t,
    start_val,
    data_def_small
)

loss_small = Loss([SemDefinition(semobserved_small, imply_small, 0.0, 0.0)])

## big ############################################################

semobserved_big =
    SemObsCommon(data = Matrix(data_big); meanstructure = true)

diff_fin_big = SemFiniteDiff(LBFGS(), Optim.Options(x_tol = 1e-9))

@ModelingToolkit.variables x[1:18], m[1:2], load_t[1:15]

F = zeros(15, 17)

diag(F)
F[diagind(F)] .= 1.0

M = zeros(Num, 17, 1)
M .= [zeros(15)..., m[1:2]...]
M = sparse(M)

#S
Ind = [1:17..., 16, 17]; J = [1:17..., 17, 16]; V = [x[1:17]..., x[18], x[18]]
S = sparse(Ind, J, V)

#F
F = sparse(F)

#A
Ind = [1:15..., 1:15...]; J = [fill(16, 15)..., fill(17, 15)...]; V = [ones(15)..., load_t...]
A = sparse(Ind, J, V, 17, 17)

data_def_big = Matrix(data_def_big)

start_val = ones(20) #[ones(15)..., 0.05, 0.05, 0.0, 1.0, 1.0]

imply_big = ImplySymbolicDefinition(
    A,
    S,
    F,
    M,
    [x[1:18]..., m[1:2]...],
    load_t,
    start_val,
    data_def_big
)

loss_big = Loss([SemDefinition(semobserved_big, imply_big, 0.0, 0.0)])

## huge ############################################################

semobserved_huge =
    SemObsCommon(data = Matrix(data_huge); meanstructure = true)

diff_fin_huge = SemFiniteDiff(BFGS(), Optim.Options())

@ModelingToolkit.variables x[1:33], m[1:2], load_t[1:30]

F = zeros(30, 32)

diag(F)
F[diagind(F)] .= 1.0

M = [zeros(30)..., m[1:2]...]

#S
Ind = [1:32..., 31, 32]; J = [1:32..., 32, 31];
V = [x[1:32]..., x[33], x[33]];

S = sparse(Ind, J, V)

#F
F = sparse(F)

#A
Ind = [1:30..., 1:30...]; J = [fill(31, 30)..., fill(32, 30)...];
V = [ones(30)..., load_t...];
A = sparse(Ind, J, V, 32, 32)

data_def_huge = Matrix(data_def_huge)

start_val = ones(35)#[ones(30)..., 0.05, 0.05, 0.0, 1.0, 1.0]

imply_huge = ImplySymbolicDefinition(
    A,
    S,
    F,
    M,
    [x[1:33]..., m[1:2]...],
    load_t,
    start_val,
    data_def_huge
)

loss_huge = Loss([SemDefinition(semobserved_huge, imply_huge, 0.0, 0.0)])

### diff objects ##########################################################

## Tolerances
# what about x_tol and g_tol?
omx_abstol = 6.3e-12
objective_value = 41856.14242753569
objective_value = 2.272870e+04
rel_tol = omx_abstol/(objective_value+1)

f_tol = rel_tol #6.3e-12
time_limit = 40

## initial step length
#InitialPrevious (Use the step length from the previous optimization iteration)
#InitialStatic (Use the same initial step length each time) (scaled = TRUE?)
#InitialHagerZhang (Taken from Hager and Zhang, 2006)
#InitialQuadratic (Propose initial step length based on a quadratic interpolation) (proposed for grad. descent/conj. grad)
#InitialConstantChange (Propose initial step length assuming constant change in step length)

## Line Search
#HagerZhang (Taken from the Conjugate Gradient implementation by Hager and Zhang, 2006)
#MoreThuente (From the algorithm in More and Thuente, 1994)
#BackTracking (Described in Nocedal and Wright, 2006) (quadratic or cubic)
#StrongWolfe (Nocedal and Wright)
#Static (Takes the proposed initial step length.)

step_length = (InitialPrevious, InitialStatic, InitialQuadratic, InitialHagerZhang, InitialConstantChange)
line_search = (HagerZhang, MoreThuente, BackTracking, StrongWolfe, Static)
algo = (BFGS, LBFGS, Newton)

sl_names = String.(Symbol.(step_length))
ls_names = String.(Symbol.(line_search))
algo_names = String.(Symbol.(algo))
m = [10, 50]

#combinations = Array{String, 1}(undef, 25)

#solutions_BFGS = Dict{String, Any}()
#solutions_LBFGS = Dict{String, Any}()
#solutions_newton = Dict{String, Any}()

using DataFrames, CSV

solutions =
    DataFrame(
        algo = String[],
        line_search = String[],
        step_length = String[],
        solution = Any[],
        m = Int64[],
        n = Int64[])

#push!(solutions, "hi" => 1.0)

for i = 1:length(step_length)
    for j = 1:length(line_search)
        for k = 1:length(algo)
            if algo[k] == LBFGS
                for l = 1:length(m)
                    m_ = m[l]
                    diff = SemFiniteDiff(
                        algo[k](;
                            m = m_,
                            alphaguess = step_length[i](),
                            linesearch = line_search[j]()),
                        Optim.Options(f_tol = f_tol, time_limit = time_limit))
                    model = Sem(semobserved_big, imply_big, loss_big, diff)
                    solution = sem_fit(model)
                    push!(solutions, (algo_names[k], ls_names[j], sl_names[i], solution, m_, size(solutions)[1]+1))
                end
            else
            m_ = 1
            diff = SemFiniteDiff(
                algo[k](;
                    alphaguess = step_length[i](),
                    linesearch = line_search[j]()),
                Optim.Options(f_tol = f_tol, time_limit = time_limit))
            model = Sem(semobserved_big, imply_big, loss_big, diff)
            solution = sem_fit(model)
            push!(solutions, (algo_names[k], ls_names[j], sl_names[i], solution, m_, size(solutions)[1]+1))
            end
        end
    end
end

#times_BFGS = Dict{String, Any}()

function compare_solutions(mypars, truepars, order)
    all(
        abs.(mypars .- truepars[order]
            ) .< 0.001*abs.(truepars[order]))
end

solutions.minimum = getproperty.(solutions.solution, :minimum)
solutions.minimizer = getproperty.(solutions.solution, :minimizer)
solutions.time = getproperty.(solutions.solution, :time_run)
solutions.f_calls = getproperty.(solutions.solution, :f_calls)
solutions.g_calls = getproperty.(solutions.solution, :g_calls)
solutions.h_calls = getproperty.(solutions.solution, :h_calls)
solutions.truepars =
    broadcast(x -> compare_solutions(
        x,
        pars_big.Estimate,
        par_order), solutions.minimizer)

select!(solutions, #Not(:solution),
    Not(:minimizer))

Feather.write(
    "test/comparisons/benchmark_small.feather",
    solutions)

CSV.write("test/comparisons/benchmark_big.csv", solutions)

### benchmarks ############################################################

model_fin_small = Sem(semobserved_small, imply_small, loss_small, diff_fin_small)

solution_small = sem_fit(model_fin_small)

par_order = [collect(1:5); 7; 6; 8; 9]

all(
    abs.(solution_small.minimizer .- pars_small.Estimate[par_order]
        ) .< 0.001*abs.(pars_small.Estimate[par_order]))

@benchmark sem_fit(model_fin_small)

##

diff_fin_big = SemFiniteDiff(
    LBFGS(),
    Optim.Options(f_tol = f_tol))#, time_limit = 20))

diff_fin_big = SemFiniteDiff(
    LBFGS(alphaguess = InitialHagerZhang(),
        linesearch = HagerZhang()),
    Optim.Options(f_tol = f_tol, time_limit = time_limit))

model_fin_big = Sem(semobserved_big, imply_big, loss_big, diff_fin_big)

solution_big = sem_fit(model_fin_big)

par_order = [collect(1:16); 18; 17; 19; 20]

all(
    abs.(solution_big.minimizer .- pars_big.Estimate[par_order]
        ) .< 0.001*abs.(pars_big.Estimate[par_order]))

@benchmark sem_fit(model_fin_big)

##
diff_fin_huge = SemFiniteDiff(
    LBFGS(; m = 50,
    alphaguess = InitialHagerZhang()),
    Optim.Options(f_tol = f_tol))

model_fin_huge = Sem(semobserved_huge, imply_huge, loss_huge, diff_fin_huge)

solution_huge = sem_fit(model_fin_huge)

par_order = [collect(1:31); 33; 32; 34; 35]

all(
    abs.(solution_huge.minimizer .- pars_huge.Estimate[par_order]
        ) .< 0.001*abs.(pars_huge.Estimate[par_order]))

@benchmark sem_fit(model_fin_huge)


text:
# make sure the right packages are installed
try
    @assert Pkg.installed("ChromNet") != nothing
catch
    Pkg.clone("https://github.com/slundberg/ChromNet.jl.git")
end
try
    @assert Pkg.installed("SamIO") != nothing
catch
    Pkg.clone("https://github.com/slundberg/SamIO.jl.git")
end

# This is just a simple link to the real script that is installed in the julia package.
# By keeping this file as just a link, we can maintain the script using the Julia
# package manager (i.e. Pkg.update())
include("$(Pkg.dir())/ChromNet/scripts/build_bundle.jl")


text:
########################### Interfaces ########################################################

get_variable_name(variabletype, d) = error("Not Implemented")
get_variable_binary(pv, t::Type{<:PSY.Component}, _) =
    error("`get_variable_binary` must be implemented for $pv and $t")
get_variable_expression_name(_, ::Type{<:PSY.Component}) = nothing
get_variable_initial_value(_, d::PSY.Component, __) = nothing
get_variable_lower_bound(_, d::PSY.Component, __) = nothing
get_variable_upper_bound(_, d::PSY.Component, __) = nothing


text:
function contour_estimate_eig(A::AbstractMatrix, contour::Contour, B=I;
                samples::Integer=min(100, size(A,1)), ϵ=1e-12, debug=false,
                mixed_prec=false, factorizer=lu, left_divider=ldiv!)

    N, m₀ = size(A,1), samples
    Ctype = if mixed_prec ComplexF32 else ComplexF64 end
    # X = if typeof(A) <: AbstractSparseMatrix
    #     sprandn(Ctype, N, m₀, min(1.0, m₀^2/N))
    # else
    #     randn(Ctype, N, m₀)
    # end
    ### Using sparse X does not work, possibly due to not being i.i.d. mean zero RV
    X = randn(Ctype, N, m₀)

    temp = zeros(Ctype, N, m₀)
    P = zeros(ComplexF64, m₀, m₀)
    ZmA = similar(A, Ctype)
    nodes = size(contour.nodes, 1)
    est = 0.0

    for i=1:nodes
        ZmA .= Ctype.(B*contour.nodes[i] - A)
        left_divider(temp, factorizer(ZmA), X)
        mul!(P, X', temp)
        est += tr(P)*contour.weights[i]/samples
        if debug print(".") end
    end
    if debug println() end

    return real(est)
end


text:
using Nuklear.LibNuklear
using Test

@testset "trivial" begin
    ctx = nk_create_context()
    GC.@preserve ctx begin
        @test Bool(nk_init_default(ctx, C_NULL))
        nk_free(ctx)
    end
end


text:
include("Include.jl")

# load the data dictionary -
data_dictionary = DataDictionary(0,0,0)
TXTL_dictionary = TXTLDictionary(0,0,0)
number_of_fluxes = length(data_dictionary["default_flux_bounds_array"][:,1])
number_of_species = length(data_dictionary["species_bounds_array"][:,1])
#Set objective reaction
data_dictionary["objective_coefficient_array"][171] = -1;
#-------------------------------------------------------------------------------------------------#Define case number
# 1 = Amino Acid Uptake & Synthesis
# 2 = Amino Acid Uptake w/o Synthesis
# 3 = Amino Acid Synthesis w/o Uptake
case = 1
if case == 1
  data_dictionary["AASyn"] = 100;
  data_dictionary["AAUptake"] = 30
  data_dictionary["AASecretion"] = 0;
end
if case == 2
  data_dictionary["AASyn"] = 0;
  data_dictionary["AAUptake"] = 30
  data_dictionary["AASecretion"] = 0;
end
if case == 3
  data_dictionary["AASyn"] = 100;
  data_dictionary["AAUptake"] = 0
  data_dictionary["AASecretion"] = 100;
end
#-------------------------------------------------------------------------------------------------
volume = TXTL_dictionary["volume"]
plasmid_concentration = 5;
gene_copy_number = (volume/1e9)*(6.02e23)*plasmid_concentration;
TXTL_dictionary["gene_copies"] = gene_copy_number
data_dictionary = DataDictionary(0,0,0)
#Set objective reaction
data_dictionary["objective_coefficient_array"][194] = -1;
data_dictionary["AASyn"] = 100;
data_dictionary["AAUptake"] = 30
data_dictionary["AASecretion"] = 0;
data_dictionary["GlcUptake"] = 30#*rand(1)[1];
data_dictionary["Oxygen"] = 30#+2*rand(1)[1];
TXTL_dictionary["RNAP_concentration_nM"] = 75 #+ (80-60)*rand(1)[1];
TXTL_dictionary["RNAP_elongation_rate"] = 25 #+ (30-20)*rand(1)[1];
TXTL_dictionary["RIBOSOME_concentration"] = 0.0016# + (0.0018-0.0012)*rand(1)[1];
TXTL_dictionary["RIBOSOME_elongation_rate"] = 2# + (3-1.5)*rand(1)[1];
data_dictionary["Oxygen"] = 100;
data_dictionary["AAUptake"] = 1 #+ (1.5-0.5)*rand(1)[1];
data_dictionary["GlcUptake"] = 30# + (38-28)*rand(1)[1];
data_dictionary["AC"] = 8# + (12-6)*rand(1)[1];
data_dictionary["SUCC"] = 2# + (3-1)*rand(1)[1];
data_dictionary["PYR"] = 7# + (10-5)*rand(1)[1];
data_dictionary["MAL"] = 3 #+ (5-2)*rand(1)[1];
data_dictionary["LAC"] = 8 #+ (12-8)*rand(1)[1];
data_dictionary["ENERGY"] = 1#1*rand(1)[1];
data_dictionary["ALA"] = 2 #+ (4-3)*rand(1)[1];
data_dictionary["ASP"] = 1#+rand(1)[1];
data_dictionary["GLN"] = 0.25#*rand(1)[1];
data_dictionary["LysUptake"] = 1.5#*rand(1)[1];
data_dictionary["LysSecretion"] = 0.5#1*rand(1)[1];
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
t0_exp = 0
tf_exp = 3
tstep_exp = 0.01
experimental_time_exp = [t0_exp:tstep_exp:tf_exp;]

species_list = readdlm("./data/species_list")
Mean_raw = readdlm("./data/Mean")
Std_raw = readdlm("./data/Std")
Upper_raw = readdlm("./data/Upper")
Lower_raw = readdlm("./data/Lower")
Mean = Dict()
Std = Dict()
Upper = Dict()
Lower = Dict()
for i in 1:length(species_list)
	rxn_idx = species_list[i]
	Mean[rxn_idx] = Mean_raw[:,i]
	Std[rxn_idx] = Std_raw[:,i]
	Upper[rxn_idx] = Upper_raw[:,i]
	Lower[rxn_idx] = Lower_raw[:,i]
end

data_dictionary["species_drift_amplitude"] = 2 # percent allowed to drift for unconstraint species for each iteration (0.01 hr)
data_dictionary["species_drift_decay_rate"] = 1
metabolite_list = data_dictionary["list_of_metabolite_symbols"]
rxn_list = data_dictionary["list_of_reaction_strings"]

(n_species,n_rxn) = size(data_dictionary["stoichiometric_matrix"])
t0 = 0
tf = 3
tstep = 0.01
experimental_time = t0:tstep:tf
length_time = length(experimental_time)
data_dictionary["tstep"] = tstep

tRNA_index = Int64[]
for i in 1:length(metabolite_list)
	rxn_idx = metabolite_list[i]
	if length(rxn_idx) > 5
		if rxn_idx[end-4:end] == "_tRNA"
			push!(tRNA_index,i)
		end
	end
end

initial_conditions = zeros(n_species,1)
for species_index in 1:n_species
	if in(species_index,tRNA_index)
		initial_conditions[species_index] = Mean[metabolite_list[species_index]][2]
	else
		initial_conditions[species_index] = Mean[metabolite_list[species_index]][1]
	end
end
#initial_conditions[142] = .001
initial_conditions[143] = .001
initial_conditions[144] = .0001
data_dictionary["initial_conditions"] = initial_conditions

# this determines which parameters are fitted to data
#dataset_index = [15; 18; 22; 24; 27; 29; 31; 34; 38; 41; 42; 59; 60; 61; 66; 69; 70; 76; 79; 83; 84; 86; 88; 89; 106; 110; 114; 121; 123; 126; 128; 130; 132; 133; 134; 135; 140]
dataset_index = [15; 18; 22; 24; 27; 29; 31; 34; 38; 41; 42; 59; 60; 61; 66; 69; 70; 76; 79; 83; 84; 86; 88; 89; 106; 110; 114; 121; 123; 126; 128; 130; 132; 133; 134; 135]
species_constraint_index = copy(dataset_index)

species_list = data_dictionary["list_of_metabolite_symbols"]

syn_data_upper = zeros(n_species,length_time)
syn_data_lower = zeros(n_species,length_time)
syn_mean = zeros(n_species,length_time)
for syn_index in species_constraint_index
	itp = interpolate((experimental_time_exp,),Mean[metabolite_list[syn_index]],Gridded(Linear()))
	Mean_itp = itp[experimental_time]
	itp = interpolate((experimental_time_exp,),Std[metabolite_list[syn_index]],Gridded(Linear()))
	Std_itp = itp[experimental_time]
	itp = interpolate((experimental_time_exp,),Lower[metabolite_list[syn_index]],Gridded(Linear()))
	Lower_itp = itp[experimental_time]
	itp = interpolate((experimental_time_exp,),Upper[metabolite_list[syn_index]],Gridded(Linear()))
	Upper_itp = itp[experimental_time]
	syn_data_upper[syn_index,:] = Upper_itp
	syn_data_lower[syn_index,:] = Lower_itp
	syn_mean[syn_index,:] = Mean_itp
end
syn_data = Dict{AbstractString,Any}()
syn_data["mean"] = syn_mean
syn_data["upper"] = syn_data_upper
syn_data["lower"] = syn_data_lower

#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
if !isdir("TXTL")
	mkdir("TXTL")
end

if !isfile("TXTL/num_dir")
	num_dir = 0
else
	num_dir = Int64(readdlm("TXTL/num_dir")[1])
end

# Original values:
RNAP_IC = 1e-3
RIBO_IC = 0.002
RNAP_elongation_rate = 25
plasmid_saturation_coefficient = 116e-6
polysome_amplification = 5
RIBOSOME_elongation_rate = 2
mRNA_saturation_coefficient = 0.045
mRNA_degradation_rate = 10

params = vcat(RNAP_IC, RIBO_IC, RNAP_elongation_rate, plasmid_saturation_coefficient, polysome_amplification, RIBOSOME_elongation_rate, mRNA_saturation_coefficient, mRNA_degradation_rate)

param_bounds = [8e-4 12e-4
				0.0015 0.003
				20 30
				116e-7 116e-5
				5 15
				1.5 3
				0.0045 0.45
				8 12]

data_dictionary["initial_conditions"][143] = params[1]
data_dictionary["initial_conditions"][141] = params[2]
TXTL_dictionary["RNAP_elongation_rate"] = params[3]
TXTL_dictionary["plasmid_saturation_coefficient"] = params[4]
TXTL_dictionary["polysome_amplification"] = params[5]
TXTL_dictionary["RIBOSOME_elongation_rate"] = params[6]
TXTL_dictionary["mRNA_saturation_coefficient"] = params[7]
TXTL_dictionary["mRNA_degradation_rate"] = params[8]

alpha = 25
#variance_min = .1
#variance_max = 10
failed_tps_threshold = 0
CAT_threshold = 5/1000 # base-case is 0.02326

for i in 1:1e10

	params_new = copy(params)
	for p in 1:length(params_new)
		if in(p,[4;7])
			params_new[p] = exp(log(param_bounds[p,1])+rand().*(log(param_bounds[p,2])-log(param_bounds[p,1])))
		else
			params_new[p] = param_bounds[p,1]+rand().*(param_bounds[p,2]-param_bounds[p,1])
		end
	end

	data_dictionary_new = deepcopy(data_dictionary)
	TXTL_dictionary_new = deepcopy(TXTL_dictionary)
	data_dictionary_new["initial_conditions"][143] = params_new[1]
	data_dictionary_new["initial_conditions"][141] = params_new[2]
	TXTL_dictionary_new["RNAP_elongation_rate"] = params_new[3]
	TXTL_dictionary_new["plasmid_saturation_coefficient"] = params_new[4]
	TXTL_dictionary_new["polysome_amplification"] = params_new[5]
	TXTL_dictionary_new["RIBOSOME_elongation_rate"] = params_new[6]
	TXTL_dictionary_new["mRNA_saturation_coefficient"] = params_new[7]
	TXTL_dictionary_new["mRNA_degradation_rate"] = params_new[8]

	avg_perturbation = exp(mean(abs(log(params_new./params))))
	avg_perturbation_round = round(avg_perturbation,3)

	time_state_array_new,error_new,num_failed_tps = RunFBA(data_dictionary_new, TXTL_dictionary_new)
	cost_new = sum(error_new)

	CAT_end = time_state_array_new[140,end]
	CAT_end_round = round(CAT_end,5)

	if (num_failed_tps <= failed_tps_threshold) && (CAT_end > CAT_threshold)
		num_dir += 1
		mkdir("TXTL/$num_dir")
		writedlm("TXTL/$num_dir/params",params_new)
		writedlm("TXTL/$num_dir/time_state_array",time_state_array_new)
		writedlm("TXTL/$num_dir/num_failed_tps",num_failed_tps)
		writedlm("TXTL/num_dir",num_dir)
		println("num_failed_tps = $num_failed_tps, CAT_end = $CAT_end_round, ACCEPTED")
	else
		println("num_failed_tps = $num_failed_tps, CAT_end = $CAT_end_round")
	end
end


text:
using Gadfly, RDatasets

set_default_plot_size(10cm, 15cm)

d=dataset("mlmRev", "Chem97")
idx = d[:Age] .>0
plot(d[idx,:],
     x=:GCSEScore,
     ygroup=:Age,
     color=:Gender,
     Geom.subplot_grid(Geom.histogram(bincount=20), free_y_axis=true))


text:
abstract type NoTranslationModel <: TranslationModel end

struct NoTranslationParam <: EoSParam
end

@newmodelsimple NoTranslation NoTranslationModel NoTranslationParam

export NoTranslation
function NoTranslation(components::Vector{String}; userlocations::Vector{String}=String[], verbose::Bool=false)
    model = NoTranslation(NoTranslationParam())
    return model
end

function translation(model::CubicModel,V,T,z,translation_model::NoTranslation)
    return FillArrays.Zeros{Float64}(length(z))
end

is_splittable(::NoTranslation) = false


text:
include("utils.jl")

@testset "Test Notation" begin
    M = Sphere(2)
    p1 = [1.0, 0.0, 0.0]
    @test (p1 ∈ M) == is_manifold_point(M, p1)
    @test (2 * p1 ∈ M) == is_manifold_point(M, 2 * p1)
    X1 = [0.0, 1.0, 0.0]
    X2 = [1.0, 0.0, 0.0]
    TpM = TangentSpaceAtPoint(M, p1)
    @test (X1 ∈ TpM) == is_tangent_vector(M, p1, X1)
    @test (X2 ∈ TpM) == is_tangent_vector(M, p1, X2)
end


text:
using BacenSGS
using Documenter

DocMeta.setdocmeta!(BacenSGS, :DocTestSetup, :(using BacenSGS); recursive=true)

makedocs(;
    modules=[BacenSGS],
    authors="Gustavo H T Cardoso <ghtcardoso@icloud.com> and contributors",
    repo="https://github.com/gustavohtc/BacenSGS.jl/blob/{commit}{path}#{line}",
    sitename="BacenSGS.jl",
    format=Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://gustavohtc.github.io/BacenSGS.jl",
        assets=String[],
    ),
    pages=[
        "Home" => "index.md",
    ],
)

deploydocs(;
    repo="github.com/gustavohtc/BacenSGS.jl",
)


text:
module RefFields

using Base: RefValue, fieldindex

export gepindex, gepfield

struct RefField{T} <: Ref{T}
    base
    offset::UInt
    @inline function RefField(x, idx::Integer)
        typeof(x).mutable || error("Tried to take reference to immutable type $(typeof(x))")
        Tfield = fieldtype(typeof(x), idx)
        isbitstype(Tfield) || error("Can only take interior references that are inline (e.g. isbits). Tried to access field \"$(fieldname(typeof(x), idx))\" of type $Tfield")
        new{Tfield}(x, fieldoffset(typeof(x), idx))
    end
    @inline function RefField(x::RefField{T}, idx::Integer) where {T}
        Tfield = fieldtype(T, idx)
        isbitstype(Tfield) || error("Can only take interior references that are inline (e.g. isbits). Tried to access field \"$(fieldname(T, idx))\" of type $Tfield")
        new{Tfield}(x.base, x.offset + fieldoffset(T, idx))
    end
end
@inline RefField(x, sym::Symbol) = RefField(x, Base.fieldindex(typeof(x), sym))
@inline RefField(x::RefField{T}, sym::Symbol) where {T} = gepfield(x, Base.fieldindex(T, sym))

@inline gepfield(x, idx::Integer) = RefField(x, idx)
@inline gepfield(x, idx::Symbol) = RefField(x, idx)

function Base.setindex!(x::RefField{T}, v::T) where T
    unsafe_store!(Ptr{T}(pointer_from_objref(x.base)+x.offset), v)
    v
end
Base.setindex!(x::RefField{T}, v::S) where {T,S} = setindex!(x, convert(T, v)::T)

Base.getindex(x::RefField{T}) where {T} = unsafe_load(Ptr{T}(pointer_from_objref(x.base)+x.offset))

####
#### Base
####

@inline gepindex(x::Tuple, i) = gepfield(x, i)
@inline gepindex(x::RefField{<:Tuple}, idx) = gepfield(x, idx)

@inline function gepfield(r::RefValue{T}, idx::Integer) where T
    if T.mutable
        RefField(r.x, idx)
    else
        RefField(RefField(r, 1), idx)
    end

end
@inline gepfield(r::RefValue{T}, sym::Symbol) where {T} = gepfield(r, fieldindex(T, sym))
#@inline gepindex(r::RefValue, idxs...) = gepindex(RefField(r, 1), idxs...)

Base.copy(x::RefValue{T}) where {T} = RefValue{T}(x.x)

end # module


text:
"""
Check global mean temperature at every vertical level from the m=n=1 coefficient
of the associated Legendre polynomials.
"""
function check_global_mean_temperature( Tabs::Array{Complex{T},3},
                                        P::Parameters) where T
    @unpack print_dialog = P

    # The m=n=1 coefficent in spectral space is the mean value
    # √2 is due to the normalization of the associated Legendre polynomials
    gmt = sqrt(0.5)*Float64.(real.(Tabs[1,1,:]))

    if print_dialog
        println(gmt)
    end
end

# # Prints global means of eddy kinetic energy and temperature.
# # Also stops the integration if the computed diagnostics are outside of
# # allowable ranges.
# function check_diagnostics(geometry::Geometry, spectral_trans::SpectralTrans, ξ, D, Tₐ, step,
#                            print_diag=true)
#     @unpack nlev, mx, nx = geometry
#
#     diagnostics = zeros(Float64, nlev, 3)
#
#     # 1. Get global-mean temperature and compute eddy kinetic energy
#     for k in 1:nlev
#         diagnostics[k,3] = √(0.5)*real(Tₐ[1,1,k])
#
#         temp = ∇⁻²(spectral_trans, ξ[:,:,k])
#
#         for m in 2:mx
#             for n in 1:nx
#                 diagnostics[k,1] = diagnostics[k,1] - real(temp[m,n]*conj(ξ[m,n,k]))
#             end
#         end
#
#         temp = ∇⁻²(spectral_trans, D[:,:,k])
#
#         for m in 2:mx
#             for n in 1:nx
#                 diagnostics[k,2] = diagnostics[k,2] - real(temp[m,n]*conj(D[m,n,k]))
#             end
#         end
#     end
#
#     # 2. Print results to screen
#     if print_diag
#         println("Step = $step")
#         println(diagnostics[:,1])
#         println(diagnostics[:,2])
#         println(diagnostics[:,3])
#     end
#
#     # 3. Stop integration if model variables are out of range
#     for k in 1:nlev
#         if diagnostics[k,1] > 500.0 || diagnostics[k,2] > 500.0 ||
#             diagnostics[k,3] < 180.0 || diagnostics[k,3] > 320.0
#
#             println(diagnostics[:,1])
#             println(diagnostics[:,2])
#             println(diagnostics[:,3])
#
#             throw("Model variables out of accepted range")
#         end
#     end
# end


text:
@testset "integration tests" begin
    println("             > INTEGRATION TESTS")
    @testset "convergence" begin
        N = 10
        p = 0.85
        q = 1.0
        σ = 0.2
        τ = 0.2
        sim = NestedFilterSimulation(
                N, p, q, σ, τ,
                1:20,
                LinRange(0.00, 1.00, 100),
                LinRange(0.00, 2.00, 100),
                LinRange(0.05, 2.00, 100),
                LinRange(0.05, 2.00, 100),
                2048, 512, 12
              )
        times, epsps = run!(sim, T = 1000)
        map = MAP(sim)

        @test abs(map.N - N) <= 6
        @test abs(map.p - p) <= 0.1
        @test abs(map.q - q) <= 0.1
        @test abs(map.σ - σ) <= 0.3
        @test abs(map.τ - τ) <= 0.3
    end
end


text:
@defcomp co2forcing begin
    c_CO2concentration = Parameter(index=[time], unit="ppbv")
    f0_CO2baseforcing = Parameter(unit="W/m2", default=1.68)
    fslope_CO2forcingslope = Parameter(unit="W/m2", default=5.5)
    c0_baseCO2conc = Parameter(unit="ppbv", default=400859.5833)
    f_CO2forcing = Variable(index=[time], unit="W/m2")

    function run_timestep(p, v, d, t)

        # eq.13 in Hope 2006
        v.f_CO2forcing[t] = p.f0_CO2baseforcing + p.fslope_CO2forcingslope * log(p.c_CO2concentration[t] / p.c0_baseCO2conc)
    end
end


text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

                     @variable(m, -1 <= x[i=1:4] <= 1)

                     @variable(m, -6.563131467197746 <= q <= 9.310824610097802)

                     add_NL_constraint(m, :(1/(1 + exp(-(0.5323461867533106 + 0.6233251355511138*1/(1 + exp(-(0.9117705078314118 + -0.4325489455874667*$(x[1]) + 0.44461795780922087*$(x[2]) + -0.310674968881143*$(x[3]) + 0.3874148238299093*$(x[4])))) + 0.13135733952437967*1/(1 + exp(-(0.4793544889478012 + 0.21125743039643652*$(x[1]) + -0.625324840825729*$(x[2]) + -0.9496988785997882*$(x[3]) + 0.8662818947925528*$(x[4])))) + -0.0038770043009219712*1/(1 + exp(-(0.7417071748063861 + 0.06375566706279434*$(x[1]) + -0.03899151888275032*$(x[2]) + 0.7226457302550271*$(x[3]) + 0.006005128865761655*$(x[4]))))))) + 1/(1 + exp(-(0.48577545639109676 + -0.6684827900810446*1/(1 + exp(-(0.9117705078314118 + -0.4325489455874667*$(x[1]) + 0.44461795780922087*$(x[2]) + -0.310674968881143*$(x[3]) + 0.3874148238299093*$(x[4])))) + 0.9262921195438665*1/(1 + exp(-(0.4793544889478012 + 0.21125743039643652*$(x[1]) + -0.625324840825729*$(x[2]) + -0.9496988785997882*$(x[3]) + 0.8662818947925528*$(x[4])))) + 0.29789654203123295*1/(1 + exp(-(0.7417071748063861 + 0.06375566706279434*$(x[1]) + -0.03899151888275032*$(x[2]) + 0.7226457302550271*$(x[3]) + 0.006005128865761655*$(x[4]))))))) + 1/(1 + exp(-(0.2863540742777313 + -0.41164742912319063*1/(1 + exp(-(0.9117705078314118 + -0.4325489455874667*$(x[1]) + 0.44461795780922087*$(x[2]) + -0.310674968881143*$(x[3]) + 0.3874148238299093*$(x[4])))) + 0.6047572744817185*1/(1 + exp(-(0.4793544889478012 + 0.21125743039643652*$(x[1]) + -0.625324840825729*$(x[2]) + -0.9496988785997882*$(x[3]) + 0.8662818947925528*$(x[4])))) + -0.7133357903136108*1/(1 + exp(-(0.7417071748063861 + 0.06375566706279434*$(x[1]) + -0.03899151888275032*$(x[2]) + 0.7226457302550271*$(x[3]) + 0.006005128865761655*$(x[4]))))))) - $q <= 0.0))

                     @objective(m, Min, q)

                     return m

max_stars_repo_name: PSORLab/RSActivationFunctions

max_issues_repo_name: PSORLab/RSActivationFunctions

max_forks_repo_name: PSORLab/RSActivationFunctions


text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

                     @variable(m, -1 <= x[i=1:3] <= 1)

                     @variable(m, -3.1276627592672863 <= q <= 3.1323678679599185)

                     add_NL_constraint(m, :(1/(1 + exp(-(0.7015310798579062 + -0.563498653088923*$(x[1]) + -0.9918531495953298*$(x[2]) + 0.03337339686246876*$(x[3])))) + 1/(1 + exp(-(-0.6991785255115901 + -0.8453063373771403*$(x[1]) + 0.408983421114415*$(x[2]) + 0.28700035557532555*$(x[3])))) - $q <= 0.0))

                     @objective(m, Min, q)

                     return m