#!/usr/bin/env julia
# -*- julia -*-

# remez.jl - implementation of the Remez algorithm for polynomial approximation
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

import Base.\

# ----------------------------------------------------------------------
# Helper functions to cope with different Julia versions.
if VERSION >= v"0.7.0"
    array1d(T, d) = Array{T, 1}(undef, d)
    array2d(T, d1, d2) = Array{T, 2}(undef, d1, d2)
else
    array1d(T, d) = Array(T, d)
    array2d(T, d1, d2) = Array(T, d1, d2)
end
if VERSION < v"0.5.0"
    String = ASCIIString
end
if VERSION >= v"0.6.0"
    # Use Base.invokelatest to run functions made using eval(), to
    # avoid "world age" error
    run(f, x...) = Base.invokelatest(f, x...)
else
    # Prior to 0.6.0, invokelatest doesn't exist (but fortunately the
    # world age problem also doesn't seem to exist)
    run(f, x...) = f(x...)
end

# ----------------------------------------------------------------------
# Global variables configured by command-line options.
floatsuffix = "" # adjusted by --floatsuffix
xvarname = "x" # adjusted by --variable
epsbits = 256 # adjusted by --bits
debug_facilities = Set() # adjusted by --debug
full_output = false # adjusted by --full
array_format = false # adjusted by --array
preliminary_commands = array1d(String, 0) # adjusted by --pre

# ----------------------------------------------------------------------
# Diagnostic and utility functions.

# Enable debugging printouts from a particular subpart of this
# program.
#
# Arguments:
#    facility   Name of the facility to debug. For a list of facility names,
#               look through the code for calls to debug().
#
# Return value is a BigFloat.
function enable_debug(facility)
    push!(debug_facilities, facility)
end

# Print a diagnostic.
#
# Arguments:
#    facility   Name of the facility for which this is a debug message.
#    printargs  Arguments to println() if debugging of that facility is
#               enabled.
macro debug(facility, printargs...)
    printit = quote
        print("[", $facility, "] ")
    end
    for arg in printargs
        printit = quote
            $printit
            print($(esc(arg)))
        end
    end
    return quote
        if $facility in debug_facilities
            $printit
            println()
        end
    end
end

# Evaluate a polynomial.

# Arguments:
#    coeffs   Array of BigFloats giving the coefficients of the polynomial.
#             Starts with the constant term, i.e. coeffs[i] is the
#             coefficient of x^(i-1) (because Julia arrays are 1-based).
#    x        Point at which to evaluate the polynomial.
#
# Return value is a BigFloat.
function poly_eval(coeffs::Array{BigFloat}, x::BigFloat)
    n = length(coeffs)
    if n == 0
        return BigFloat(0)
    elseif n == 1
        return coeffs[1]
    else
        return coeffs[1] + x * poly_eval(coeffs[2:n], x)
    end
end

# Evaluate a rational function.

# Arguments:
#    ncoeffs  Array of BigFloats giving the coefficients of the numerator.
#             Starts with the constant term, and 1-based, as above.
#    dcoeffs  Array of BigFloats giving the coefficients of the denominator.
#             Starts with the constant term, and 1-based, as above.
#    x        Point at which to evaluate the function.
#
# Return value is a BigFloat.
function ratfn_eval(ncoeffs::Array{BigFloat}, dcoeffs::Array{BigFloat},
                    x::BigFloat)
    return poly_eval(ncoeffs, x) / poly_eval(dcoeffs, x)
end

# Format a BigFloat into an appropriate output format.
# Arguments:
#    x        BigFloat to format.
#
# Return value is a string.
function float_to_str(x)
    return string(x) * floatsuffix
end

# Format a polynomial into an arithmetic expression, for pasting into
# other tools such as gnuplot.

# Arguments:
#    coeffs   Array of BigFloats giving the coefficients of the polynomial.
#             Starts with the constant term, and 1-based, as above.
#
# Return value is a string.
function poly_to_string(coeffs::Array{BigFloat})
    n = length(coeffs)
    if n == 0
        return "0"
    elseif n == 1
        return float_to_str(coeffs[1])
    else
        return string(float_to_str(coeffs[1]), "+", xvarname, "*(",
                      poly_to_string(coeffs[2:n]), ")")
    end
end

# Format a rational function into a string.

# Arguments:
#    ncoeffs  Array of BigFloats giving the coefficients of the numerator.
#             Starts with the constant term, and 1-based, as above.
#    dcoeffs  Array of BigFloats giving the coefficients of the denominator.
#             Starts with the constant term, and 1-based, as above.
#
# Return value is a string.
function ratfn_to_string(ncoeffs::Array{BigFloat}, dcoeffs::Array{BigFloat})
    if length(dcoeffs) == 1 && dcoeffs[1] == 1
        # Special case: if the denominator is just 1, leave it out.
        return poly_to_string(ncoeffs)
    else
        return string("(", poly_to_string(ncoeffs), ")/(",
                      poly_to_string(dcoeffs), ")")
    end
end

# Format a list of x,y pairs into a string.

# Arguments:
#    xys      Array of (x,y) pairs of BigFloats.
#
# Return value is a string.
function format_xylist(xys::Array{Tuple{BigFloat,BigFloat}})
    return ("[\n" *
            join(["  "*string(x)*" -> "*string(y) for (x,y) in xys], "\n") *
            "\n]")
end

# ----------------------------------------------------------------------
# Matrix-equation solver for matrices of BigFloat.
#
# I had hoped that Julia's type-genericity would allow me to solve the
# matrix equation Mx=V by just writing 'M \ V'. Unfortunately, that
# works by translating the inputs into double precision and handing
# off to an optimised library, which misses the point when I have a
# matrix and vector of BigFloat and want my result in _better_ than
# double precision. So I have to implement my own specialisation of
# the \ operator for that case.
#
# Fortunately, the point of using BigFloats is that we have precision
# to burn, so I can do completely naïve Gaussian elimination without
# worrying about instability.

# Arguments:
#    matrix_in    2-dimensional array of BigFloats, representing a matrix M
#                 in row-first order, i.e. matrix_in[r,c] represents the
#                 entry in row r col c.
#    vector_in    1-dimensional array of BigFloats, representing a vector V.
#
# Return value: a 1-dimensional array X of BigFloats, satisfying M X = V.
#
# Expects the input to be an invertible square matrix and a vector of
# the corresponding size, on pain of failing an assertion.
function \(matrix_in :: Array{BigFloat,2},
           vector_in :: Array{BigFloat,1})
    # Copy the inputs, because we'll be mutating them as we go.
    M = copy(matrix_in)
    V = copy(vector_in)

    # Input consistency criteria: matrix is square, and vector has
    # length to match.
    n = length(V)
    @assert(n > 0)
    @assert(size(M) == (n,n))

    @debug("gausselim", "starting, n=", n)

    for i = 1:1:n
        # Straightforward Gaussian elimination: find the largest
        # non-zero entry in column i (and in a row we haven't sorted
        # out already), swap it into row i, scale that row to
        # normalise it to 1, then zero out the rest of the column by
        # subtracting a multiple of that row from each other row.

        @debug("gausselim", "matrix=", repr(M))
        @debug("gausselim", "vector=", repr(V))

        # Find the best pivot.
        bestrow = 0
        bestval = 0
        for j = i:1:n
            if abs(M[j,i]) > bestval
                bestrow = j
                bestval = M[j,i]
            end
        end
        @assert(bestrow > 0) # make sure we did actually find one

        @debug("gausselim", "bestrow=", bestrow)

        # Swap it into row i.
        if bestrow != i
            for k = 1:1:n
                M[bestrow,k],M[i,k] = M[i,k],M[bestrow,k]
            end
            V[bestrow],V[i] = V[i],V[bestrow]
        end

        # Scale that row so that M[i,i] becomes 1.
        divisor = M[i,i]
        for k = 1:1:n
            M[i,k] = M[i,k] / divisor
        end
        V[i] = V[i] / divisor
        @assert(M[i,i] == 1)

        # Zero out all other entries in column i, by subtracting
        # multiples of this row.
        for j = 1:1:n
            if j != i
                factor = M[j,i]
                for k = 1:1:n
                    M[j,k] = M[j,k] - M[i,k] * factor
                end
                V[j] = V[j] - V[i] * factor
                @assert(M[j,i] == 0)
            end
        end
    end

    @debug("gausselim", "matrix=", repr(M))
    @debug("gausselim", "vector=", repr(V))
    @debug("gausselim", "done!")

    # Now we're done: M is the identity matrix, so the equation Mx=V
    # becomes just x=V, i.e. V is already exactly the vector we want
    # to return.
    return V
end

# ----------------------------------------------------------------------
# Least-squares fitting of a rational function to a set of (x,y)
# points.
#
# We use this to get an initial starting point for the Remez
# iteration. Therefore, it doesn't really need to be particularly
# accurate; it only needs to be good enough to wiggle back and forth
# across the target function the right number of times (so as to give
# enough error extrema to start optimising from) and not have any
# poles in the target interval.
#
# Least-squares fitting of a _polynomial_ is actually a sensible thing
# to do, and minimises the rms error. Doing the following trick with a
# rational function P/Q is less sensible, because it cannot be made to
# minimise the error function (P/Q-f)^2 that you actually wanted;
# instead it minimises (P-fQ)^2. But that should be good enough to
# have the properties described above.
#
# Some theory: suppose you're trying to choose a set of parameters a_i
# so as to minimise the sum of squares of some error function E_i.
# Basic calculus says, if you do this in one variable, just
# differentiate and solve for zero. In this case, that works fine even
# with multiple variables, because you _partially_ differentiate with
# respect to each a_i, giving a system of equations, and that system
# turns out to be linear so we just solve it as a matrix.
#
# In this case, our parameters are the coefficients of P and Q; to
# avoid underdetermining the system we'll fix Q's constant term at 1,
# so that our error function (as described above) is
#
# E = \sum (p_0 + p_1 x + ... + p_n x^n - y - y q_1 x - ... - y q_d x^d)^2
#
# where the sum is over all (x,y) coordinate pairs. Setting dE/dp_j=0
# (for each j) gives an equation of the form
#
# 0 = \sum 2(p_0 + p_1 x + ... + p_n x^n - y - y q_1 x - ... - y q_d x^d) x^j
#
# and setting dE/dq_j=0 gives one of the form
#
# 0 = \sum 2(p_0 + p_1 x + ... + p_n x^n - y - y q_1 x - ... - y q_d x^d) y x^j
#
# And both of those row types, treated as multivariate linear
# equations in the p,q values, have each coefficient being a value of
# the form \sum x^i, \sum y x^i or \sum y^2 x^i, for various i. (Times
# a factor of 2, but we can throw that away.) So we can go through the
# list of input coordinates summing all of those things, and then we
# have enough information to construct our matrix and solve it
# straight off for the rational function coefficients.

# Arguments:
#    f        The function to be approximated. Maps BigFloat -> BigFloat.
#    xvals    Array of BigFloats, giving the list of x-coordinates at which
#             to evaluate f.
#    n        Degree of the numerator polynomial of the desired rational
#             function.
#    d        Degree of the denominator polynomial of the desired rational
#             function.
#    w        Error-weighting function. Takes two BigFloat arguments x,y
#             and returns a scaling factor for the error at that location.
#             A larger value indicates that the error should be given
#             greater weight in the square sum we try to minimise.
#             If unspecified, defaults to giving everything the same weight.
#
# Return values: a pair of arrays of BigFloats (N,D) giving the
# coefficients of the returned rational function. N has size n+1; D
# has size d+1. Both start with the constant term, i.e. N[i] is the
# coefficient of x^(i-1) (because Julia arrays are 1-based). D[1] will
# be 1.
function ratfn_leastsquares(f::Function, xvals::Array{BigFloat}, n, d,
                            w = (x,y)->BigFloat(1))
    # Accumulate sums of x^i y^j, for j={0,1,2} and a range of x.
    # Again because Julia arrays are 1-based, we'll have sums[i,j]
    # being the sum of x^(i-1) y^(j-1).
    maxpow = max(n,d) * 2 + 1
    sums = zeros(BigFloat, maxpow, 3)
    for x = xvals
        y = f(x)
        weight = w(x,y)
        for i = 1:1:maxpow
            for j = 1:1:3
                sums[i,j] += x^(i-1) * y^(j-1) * weight
            end
        end
    end

    @debug("leastsquares", "sums=", repr(sums))

    # Build the matrix. We're solving n+d+1 equations in n+d+1
    # unknowns. (We actually have to return n+d+2 coefficients, but
    # one of them is hardwired to 1.)
    matrix = array2d(BigFloat, n+d+1, n+d+1)
    vector = array1d(BigFloat, n+d+1)
    for i = 0:1:n
        # Equation obtained by differentiating with respect to p_i,
        # i.e. the numerator coefficient of x^i.
        row = 1+i
        for j = 0:1:n
            matrix[row, 1+j] = sums[1+i+j, 1]
        end
        for j = 1:1:d
            matrix[row, 1+n+j] = -sums[1+i+j, 2]
        end
        vector[row] = sums[1+i, 2]
    end
    for i = 1:1:d
        # Equation obtained by differentiating with respect to q_i,
        # i.e. the denominator coefficient of x^i.
        row = 1+n+i
        for j = 0:1:n
            matrix[row, 1+j] = sums[1+i+j, 2]
        end
        for j = 1:1:d
            matrix[row, 1+n+j] = -sums[1+i+j, 3]
        end
        vector[row] = sums[1+i, 3]
    end

    @debug("leastsquares", "matrix=", repr(matrix))
    @debug("leastsquares", "vector=", repr(vector))

    # Solve the matrix equation.
    all_coeffs = matrix \ vector

    @debug("leastsquares", "all_coeffs=", repr(all_coeffs))

    # And marshal the results into two separate polynomial vectors to
    # return.
    ncoeffs = all_coeffs[1:n+1]
    dcoeffs = vcat([1], all_coeffs[n+2:n+d+1])
    return (ncoeffs, dcoeffs)
end

# ----------------------------------------------------------------------
# Golden-section search to find a maximum of a function.

# Arguments:
#    f        Function to be maximised/minimised. Maps BigFloat -> BigFloat.
#    a,b,c    BigFloats bracketing a maximum of the function.
#
# Expects:
#    a,b,c are in order (either a<=b<=c or c<=b<=a)
#    a != c             (but b can equal one or the other if it wants to)
#    f(a) <= f(b) >= f(c)
#
# Return value is an (x,y) pair of BigFloats giving the extremal input
# and output. (That is, y=f(x).)
function goldensection(f::Function, a::BigFloat, b::BigFloat, c::BigFloat)
    # Decide on a 'good enough' threshold.
    threshold = abs(c-a) * 2^(-epsbits/2)

    # We'll need the golden ratio phi, of course. Or rather, in this
    # case, we need 1/phi = 0.618...
    one_over_phi = 2 / (1 + sqrt(BigFloat(5)))

    # Flip round the interval endpoints so that the interval [a,b] is
    # at least as large as [b,c]. (Then we can always pick our new
    # point in [a,b] without having to handle lots of special cases.)
    if abs(b-a) < abs(c-a)
        a,  c  = c,  a
    end

    # Evaluate the function at the initial points.
    fa = f(a)
    fb = f(b)
    fc = f(c)

    @debug("goldensection", "starting")

    while abs(c-a) > threshold
        @debug("goldensection", "a: ", a, " -> ", fa)
        @debug("goldensection", "b: ", b, " -> ", fb)
        @debug("goldensection", "c: ", c, " -> ", fc)

        # Check invariants.
        @assert(a <= b <= c || c <= b <= a)
        @assert(fa <= fb >= fc)

        # Subdivide the larger of the intervals [a,b] and [b,c]. We've
        # arranged that this is always [a,b], for simplicity.
        d = a + (b-a) * one_over_phi

        # Now we have an interval looking like this (possibly
        # reversed):
        #
        #    a            d       b            c
        #
        # and we know f(b) is bigger than either f(a) or f(c). We have
        # two cases: either f(d) > f(b), or vice versa. In either
        # case, we can narrow to an interval of 1/phi the size, and
        # still satisfy all our invariants (three ordered points,
        # [a,b] at least the width of [b,c], f(a)<=f(b)>=f(c)).
        fd = f(d)
        @debug("goldensection", "d: ", d, " -> ", fd)
        if fd > fb
            a,  b,  c  = a,  d,  b
            fa, fb, fc = fa, fd, fb
            @debug("goldensection", "adb case")
        else
            a,  b,  c  = c,  b,  d
            fa, fb, fc = fc, fb, fd
            @debug("goldensection", "cbd case")
        end
    end

    @debug("goldensection", "done: ", b, " -> ", fb)
    return (b, fb)
end

# ----------------------------------------------------------------------
# Find the extrema of a function within a given interval.

# Arguments:
#    f         The function to be approximated. Maps BigFloat -> BigFloat.
#    grid      A set of points at which to evaluate f. Must be high enough
#              resolution to make extrema obvious.
#
# Returns an array of (x,y) pairs of BigFloats, with each x,y giving
# the extremum location and its value (i.e. y=f(x)).
function find_extrema(f::Function, grid::Array{BigFloat})
    len = length(grid)
    extrema = array1d(Tuple{BigFloat, BigFloat}, 0)
    for i = 1:1:len
        # We have to provide goldensection() with three points
        # bracketing the extremum. If the extremum is at one end of
        # the interval, then the only way we can do that is to set two
        # of the points equal (which goldensection() will cope with).
        prev = max(1, i-1)
        next = min(i+1, len)

        # Find our three pairs of (x,y) coordinates.
        xp, xi, xn = grid[prev], grid[i], grid[next]
        yp, yi, yn = f(xp), f(xi), f(xn)

        # See if they look like an extremum, and if so, ask
        # goldensection() to give a more exact location for it.
        if yp <= yi >= yn
            push!(extrema, goldensection(f, xp, xi, xn))
        elseif yp >= yi <= yn
            x, y = goldensection(x->-f(x), xp, xi, xn)
            push!(extrema, (x, -y))
        end
    end
    return extrema
end

# ----------------------------------------------------------------------
# Winnow a list of a function's extrema to give a subsequence of a
# specified length, with the extrema in the subsequence alternating
# signs, and with the smallest absolute value of an extremum in the
# subsequence as large as possible.
#
# We do this using a dynamic-programming approach. We work along the
# provided array of extrema, and at all times, we track the best set
# of extrema we have so far seen for each possible (length, sign of
# last extremum) pair. Each new extremum is evaluated to see whether
# it can be added to any previously seen best subsequence to make a
# new subsequence that beats the previous record holder in its slot.

# Arguments:
#    extrema   An array of (x,y) pairs of BigFloats giving the input extrema.
#    n         Number of extrema required as output.
#
# Returns a new array of (x,y) pairs which is a subsequence of the
# original sequence. (So, in particular, if the input was sorted by x
# then so will the output be.)
function winnow_extrema(extrema::Array{Tuple{BigFloat,BigFloat}}, n)
    # best[i,j] gives the best sequence so far of length i and with
    # sign j (where signs are coded as 1=positive, 2=negative), in the
    # form of a tuple (cost, actual array of x,y pairs).
    best = fill((BigFloat(0), array1d(Tuple{BigFloat,BigFloat}, 0)), n, 2)

    for (x,y) = extrema
        if y > 0
            sign = 1
        elseif y < 0
            sign = 2
        else
            # A zero-valued extremum cannot possibly contribute to any
            # optimal sequence, so we simply ignore it!
            continue
        end

        for i = 1:1:n
            # See if we can create a new entry for best[i,sign] by
            # appending our current (x,y) to some previous thing.
            if i == 1
                # Special case: we don't store a best zero-length
                # sequence :-)
                candidate = (abs(y), [(x,y)])
            else
                othersign = 3-sign # map 1->2 and 2->1
                oldscore, oldlist = best[i-1, othersign]
                newscore = min(abs(y), oldscore)
                newlist = vcat(oldlist, [(x,y)])
                candidate = (newscore, newlist)
            end
            # If our new candidate improves on the previous value of
            # best[i,sign], then replace it.
            if candidate[1] > best[i,sign][1]
                best[i,sign] = candidate
            end
        end
    end

    # Our ultimate return value has to be either best[n,1] or
    # best[n,2], but it could be either. See which one has the higher
    # score.
    if best[n,1][1] > best[n,2][1]
        ret = best[n,1][2]
    else
        ret = best[n,2][2]
    end
    # Make sure we did actually _find_ a good answer.
    @assert(length(ret) == n)
    return ret
end

# ----------------------------------------------------------------------
# Construct a rational-function approximation with equal and
# alternating weighted deviation at a specific set of x-coordinates.

# Arguments:
#    f         The function to be approximated. Maps BigFloat -> BigFloat.
#    coords    An array of BigFloats giving the x-coordinates. There should
#              be n+d+2 of them.
#    n, d      The degrees of the numerator and denominator of the desired
#              approximation.
#    prev_err  A plausible value for the alternating weighted deviation.
#              (Required to kickstart a binary search in the nonlinear case;
#              see comments below.)
#    w         Error-weighting function. Takes two BigFloat arguments x,y
#              and returns a scaling factor for the error at that location.
#              The returned approximation R should have the minimum possible
#              maximum value of abs((f(x)-R(x)) * w(x,f(x))). Optional
#              parameter, defaulting to the always-return-1 function.
#
# Return values: a pair of arrays of BigFloats (N,D) giving the
# coefficients of the returned rational function. N has size n+1; D
# has size d+1. Both start with the constant term, i.e. N[i] is the
# coefficient of x^(i-1) (because Julia arrays are 1-based). D[1] will
# be 1.
function ratfn_equal_deviation(f::Function, coords::Array{BigFloat},
                               n, d, prev_err::BigFloat,
                               w = (x,y)->BigFloat(1))
    @debug("equaldev", "n=", n, " d=", d, " coords=", repr(coords))
    @assert(length(coords) == n+d+2)

    if d == 0
        # Special case: we're after a polynomial. In this case, we
        # have the particularly easy job of just constructing and
        # solving a system of n+2 linear equations, to find the n+1
        # coefficients of the polynomial and also the amount of
        # deviation at the specified coordinates. Each equation is of
        # the form
        #
        #   p_0 x^0 + p_1 x^1 + ... + p_n x^n ± e/w(x) = f(x)
        #
        # in which the p_i and e are the variables, and the powers of
        # x and calls to w and f are the coefficients.

        matrix = array2d(BigFloat, n+2, n+2)
        vector = array1d(BigFloat, n+2)
        currsign = +1
        for i = 1:1:n+2
            x = coords[i]
            for j = 0:1:n
                matrix[i,1+j] = x^j
            end
            y = f(x)
            vector[i] = y
            matrix[i, n+2] = currsign / w(x,y)
            currsign = -currsign
        end

        @debug("equaldev", "matrix=", repr(matrix))
        @debug("equaldev", "vector=", repr(vector))

        outvector = matrix \ vector

        @debug("equaldev", "outvector=", repr(outvector))

        ncoeffs = outvector[1:n+1]
        dcoeffs = [BigFloat(1)]
        return ncoeffs, dcoeffs
    else
        # For a nontrivial rational function, the system of equations
        # we need to solve becomes nonlinear, because each equation
        # now takes the form
        #
        #   p_0 x^0 + p_1 x^1 + ... + p_n x^n
        #   --------------------------------- ± e/w(x) = f(x)
        #     x^0 + q_1 x^1 + ... + q_d x^d
        #
        # and multiplying up by the denominator gives you a lot of
        # terms containing e × q_i. So we can't do this the really
        # easy way using a matrix equation as above.
        #
        # Fortunately, this is a fairly easy kind of nonlinear system.
        # The equations all become linear if you switch to treating e
        # as a constant, so a reasonably sensible approach is to pick
        # a candidate value of e, solve all but one of the equations
        # for the remaining unknowns, and then see what the error
        # turns out to be in the final equation. The Chebyshev
        # alternation theorem guarantees that that error in the last
        # equation will be anti-monotonic in the input e, so we can
        # just binary-search until we get the two as close to equal as
        # we need them.

        function try_e(e)
            # Try a given value of e, derive the coefficients of the
            # resulting rational function by setting up equations
            # based on the first n+d+1 of the n+d+2 coordinates, and
            # see what the error turns out to be at the final
            # coordinate.
            matrix = array2d(BigFloat, n+d+1, n+d+1)
            vector = array1d(BigFloat, n+d+1)
            currsign = +1
            for i = 1:1:n+d+1
                x = coords[i]
                y = f(x)
                y_adj = y - currsign * e / w(x,y)
                for j = 0:1:n
                    matrix[i,1+j] = x^j
                end
                for j = 1:1:d
                    matrix[i,1+n+j] = -x^j * y_adj
                end
                vector[i] = y_adj
                currsign = -currsign
            end

            @debug("equaldev", "trying e=", e)
            @debug("equaldev", "matrix=", repr(matrix))
            @debug("equaldev", "vector=", repr(vector))

            outvector = matrix \ vector

            @debug("equaldev", "outvector=", repr(outvector))

            ncoeffs = outvector[1:n+1]
            dcoeffs = vcat([BigFloat(1)], outvector[n+2:n+d+1])

            x = coords[n+d+2]
            y = f(x)
            last_e = (ratfn_eval(ncoeffs, dcoeffs, x) - y) * w(x,y) * -currsign

            @debug("equaldev", "last e=", last_e)

            return ncoeffs, dcoeffs, last_e
        end

        threshold = 2^(-epsbits/2) # convergence threshold

        # Start by trying our previous iteration's error value. This
        # value (e0) will be one end of our binary-search interval,
        # and whatever it caused the last point's error to be, that
        # (e1) will be the other end.
        e0 = prev_err
        @debug("equaldev", "e0 = ", e0)
        nc, dc, e1 = try_e(e0)
        @debug("equaldev", "e1 = ", e1)
        if abs(e1-e0) <= threshold
            # If we're _really_ lucky, we hit the error right on the
            # nose just by doing that!
            return nc, dc
        end
        s = sign(e1-e0)
        @debug("equaldev", "s = ", s)

        # Verify by assertion that trying our other interval endpoint
        # e1 gives a value that's wrong in the other direction.
        # (Otherwise our binary search won't get a sensible answer at
        # all.)
        nc, dc, e2 = try_e(e1)
        @debug("equaldev", "e2 = ", e2)
        @assert(sign(e2-e1) == -s)

        # Now binary-search until our two endpoints narrow enough.
        local emid
        while abs(e1-e0) > threshold
            emid = (e1+e0)/2
            nc, dc, enew = try_e(emid)
            if sign(enew-emid) == s
                e0 = emid
            else
                e1 = emid
            end
        end

        @debug("equaldev", "final e=", emid)
        return nc, dc
    end
end

# ----------------------------------------------------------------------
# Top-level function to find a minimax rational-function approximation.

# Arguments:
#    f         The function to be approximated. Maps BigFloat -> BigFloat.
#    interval  A pair of BigFloats giving the endpoints of the interval
#              (in either order) on which to approximate f.
#    n, d      The degrees of the numerator and denominator of the desired
#              approximation.
#    w         Error-weighting function. Takes two BigFloat arguments x,y
#              and returns a scaling factor for the error at that location.
#              The returned approximation R should have the minimum possible
#              maximum value of abs((f(x)-R(x)) * w(x,f(x))). Optional
#              parameter, defaulting to the always-return-1 function.
#
# Return values: a tuple (N,D,E,X), where

#    N,D       A pair of arrays of BigFloats giving the coefficients
#              of the returned rational function. N has size n+1; D
#              has size d+1. Both start with the constant term, i.e.
#              N[i] is the coefficient of x^(i-1) (because Julia
#              arrays are 1-based). D[1] will be 1.
#    E         The maximum weighted error (BigFloat).
#    X         An array of pairs of BigFloats giving the locations of n+2
#              points and the weighted error at each of those points. The
#              weighted error values will have alternating signs, which
#              means that the Chebyshev alternation theorem guarantees
#              that any other function of the same degree must exceed
#              the error of this one at at least one of those points.
function ratfn_minimax(f::Function, interval::Tuple{BigFloat,BigFloat}, n, d,
                       w = (x,y)->BigFloat(1))
    # We start off by finding a least-squares approximation. This
    # doesn't need to be perfect, but if we can get it reasonably good
    # then it'll save iterations in the refining stage.
    #
    # Least-squares approximations tend to look nicer in a minimax
    # sense if you evaluate the function at a big pile of Chebyshev
    # nodes rather than uniformly spaced points. These values will
    # also make a good grid to use for the initial search for error
    # extrema, so we'll keep them around for that reason too.

    # Construct the grid.
    lo, hi = minimum(interval), maximum(interval)
    local grid
    let
        mid = (hi+lo)/2
        halfwid = (hi-lo)/2
        nnodes = 16 * (n+d+1)
        pi = 2*asin(BigFloat(1))
        grid = [ mid - halfwid * cos(pi*i/nnodes) for i=0:1:nnodes ]
    end

    # Find the initial least-squares approximation.
    (nc, dc) = ratfn_leastsquares(f, grid, n, d, w)
    @debug("minimax", "initial leastsquares approx = ",
           ratfn_to_string(nc, dc))

    # Threshold of convergence. We stop when the relative difference
    # between the min and max (winnowed) error extrema is less than
    # this.
    #
    # This is set to the cube root of machine epsilon on a more or
    # less empirical basis, because the rational-function case will
    # not converge reliably if you set it to only the square root.
    # (Repeatable by using the --test mode.) On the assumption that
    # input and output error in each iteration can be expected to be
    # related by a simple power law (because it'll just be down to how
    # many leading terms of a Taylor series are zero), the cube root
    # was the next thing to try.
    threshold = 2^(-epsbits/3)

    # Main loop.
    while true
        # Find all the error extrema we can.
        function compute_error(x)
            real_y = f(x)
            approx_y = ratfn_eval(nc, dc, x)
            return (approx_y - real_y) * w(x, real_y)
        end
        extrema = find_extrema(compute_error, grid)
        @debug("minimax", "all extrema = ", format_xylist(extrema))

        # Winnow the extrema down to the right number, and ensure they
        # have alternating sign.
        extrema = winnow_extrema(extrema, n+d+2)
        @debug("minimax", "winnowed extrema = ", format_xylist(extrema))

        # See if we've finished.
        min_err = minimum([abs(y) for (x,y) = extrema])
        max_err = maximum([abs(y) for (x,y) = extrema])
        variation = (max_err - min_err) / max_err
        @debug("minimax", "extremum variation = ", variation)
        if variation < threshold
            @debug("minimax", "done!")
            return nc, dc, max_err, extrema
        end

        # If not, refine our function by equalising the error at the
        # extrema points, and go round again.
        (nc, dc) = ratfn_equal_deviation(f, map(x->x[1], extrema),
                                         n, d, max_err, w)
        @debug("minimax", "refined approx = ", ratfn_to_string(nc, dc))
    end
end

# ----------------------------------------------------------------------
# Check if a polynomial is well-conditioned for accurate evaluation in
# a given interval by Horner's rule.
#
# This is true if at every step where Horner's rule computes
# (coefficient + x*value_so_far), the constant coefficient you're
# adding on is of larger magnitude than the x*value_so_far operand.
# And this has to be true for every x in the interval.
#
# Arguments:
#    coeffs    The coefficients of the polynomial under test. Starts with
#              the constant term, i.e. coeffs[i] is the coefficient of
#              x^(i-1) (because Julia arrays are 1-based).
#    lo, hi    The bounds of the interval.
#
# Return value: the largest ratio (x*value_so_far / coefficient), at
# any step of evaluation, for any x in the interval. If this is less
# than 1, the polynomial is at least somewhat well-conditioned;
# ideally you want it to be more like 1/8 or 1/16 or so, so that the
# relative rounding error accumulated at each step are reduced by
# several factors of 2 when the next coefficient is added on.

function wellcond(coeffs, lo, hi)
    x = max(abs(lo), abs(hi))
    worst = 0
    so_far = 0
    for i = length(coeffs):-1:1
        coeff = abs(coeffs[i])
        so_far *= x
        if coeff != 0
            thisval = so_far / coeff
            worst = max(worst, thisval)
            so_far += coeff
        end
    end
    return worst
end

# ----------------------------------------------------------------------
# Small set of unit tests.

function test()
    passes = 0
    fails = 0

    function approx_eq(x, y, limit=1e-6)
        return abs(x - y) < limit
    end

    function test(condition)
        if condition
            passes += 1
        else
            println("fail")
            fails += 1
        end
    end

    # Test Gaussian elimination.
    println("Gaussian test 1:")
    m = BigFloat[1 1 2; 3 5 8; 13 34 21]
    v = BigFloat[1, -1, 2]
    ret = m \ v
    println("  ",repr(ret))
    test(approx_eq(ret[1], 109/26))
    test(approx_eq(ret[2], -105/130))
    test(approx_eq(ret[3], -31/26))

    # Test leastsquares rational functions.
    println("Leastsquares test 1:")
    n = 10000
    a = array1d(BigFloat, n+1)
    for i = 0:1:n
        a[1+i] = i/BigFloat(n)
    end
    (nc, dc) = ratfn_leastsquares(x->exp(x), a, 2, 2)
    println("  ",ratfn_to_string(nc, dc))
    for x = a
        test(approx_eq(exp(x), ratfn_eval(nc, dc, x), 1e-4))
    end

    # Test golden section search.
    println("Golden section test 1:")
    x, y = goldensection(x->sin(x),
                              BigFloat(0), BigFloat(1)/10, BigFloat(4))
    println("  ", x, " -> ", y)
    test(approx_eq(x, asin(BigFloat(1))))
    test(approx_eq(y, 1))

    # Test extrema-winnowing algorithm.
    println("Winnow test 1:")
    extrema = [(x, sin(20*x)*sin(197*x))
               for x in BigFloat(0):BigFloat(1)/1000:BigFloat(1)]
    winnowed = winnow_extrema(extrema, 12)
    println("  ret = ", format_xylist(winnowed))
    prevx, prevy = -1, 0
    for (x,y) = winnowed
        test(x > prevx)
        test(y != 0)
        test(prevy * y <= 0) # tolerates initial prevx having no sign
        test(abs(y) > 0.9)
        prevx, prevy = x, y
    end

    # Test actual minimax approximation.
    println("Minimax test 1 (polynomial):")
    (nc, dc, e, x) = ratfn_minimax(x->exp(x), (BigFloat(0), BigFloat(1)), 4, 0)
    println("  ",e)
    println("  ",ratfn_to_string(nc, dc))
    test(0 < e < 1e-3)
    for x = 0:BigFloat(1)/1000:1
        test(abs(ratfn_eval(nc, dc, x) - exp(x)) <= e * 1.0000001)
    end

    println("Minimax test 2 (rational):")
    (nc, dc, e, x) = ratfn_minimax(x->exp(x), (BigFloat(0), BigFloat(1)), 2, 2)
    println("  ",e)
    println("  ",ratfn_to_string(nc, dc))
    test(0 < e < 1e-3)
    for x = 0:BigFloat(1)/1000:1
        test(abs(ratfn_eval(nc, dc, x) - exp(x)) <= e * 1.0000001)
    end

    println("Minimax test 3 (polynomial, weighted):")
    (nc, dc, e, x) = ratfn_minimax(x->exp(x), (BigFloat(0), BigFloat(1)), 4, 0,
                                   (x,y)->1/y)
    println("  ",e)
    println("  ",ratfn_to_string(nc, dc))
    test(0 < e < 1e-3)
    for x = 0:BigFloat(1)/1000:1
        test(abs(ratfn_eval(nc, dc, x) - exp(x))/exp(x) <= e * 1.0000001)
    end

    println("Minimax test 4 (rational, weighted):")
    (nc, dc, e, x) = ratfn_minimax(x->exp(x), (BigFloat(0), BigFloat(1)), 2, 2,
                                   (x,y)->1/y)
    println("  ",e)
    println("  ",ratfn_to_string(nc, dc))
    test(0 < e < 1e-3)
    for x = 0:BigFloat(1)/1000:1
        test(abs(ratfn_eval(nc, dc, x) - exp(x))/exp(x) <= e * 1.0000001)
    end

    println("Minimax test 5 (rational, weighted, odd degree):")
    (nc, dc, e, x) = ratfn_minimax(x->exp(x), (BigFloat(0), BigFloat(1)), 2, 1,
                                   (x,y)->1/y)
    println("  ",e)
    println("  ",ratfn_to_string(nc, dc))
    test(0 < e < 1e-3)
    for x = 0:BigFloat(1)/1000:1
        test(abs(ratfn_eval(nc, dc, x) - exp(x))/exp(x) <= e * 1.0000001)
    end

    total = passes + fails
    println(passes, " passes ", fails, " fails ", total, " total")
end

# ----------------------------------------------------------------------
# Online help.
function help()
    print("""
Usage:

    remez.jl [options] <lo> <hi> <n> <d> <expr> [<weight>]

Arguments:

    <lo>, <hi>

        Bounds of the interval on which to approximate the target
        function. These are parsed and evaluated as Julia expressions,
        so you can write things like '1/BigFloat(6)' to get an
        accurate representation of 1/6, or '4*atan(BigFloat(1))' to
        get pi. (Unfortunately, the obvious 'BigFloat(pi)' doesn't
        work in Julia.)

    <n>, <d>

        The desired degree of polynomial(s) you want for your
        approximation. These should be non-negative integers. If you
        want a rational function as output, set <n> to the degree of
        the numerator, and <d> the denominator. If you just want an
        ordinary polynomial, set <d> to 0, and <n> to the degree of
        the polynomial you want.

    <expr>

        A Julia expression giving the function to be approximated on
        the interval. The input value is predefined as 'x' when this
        expression is evaluated, so you should write something along
        the lines of 'sin(x)' or 'sqrt(1+tan(x)^2)' etc.

    <weight>

        If provided, a Julia expression giving the weighting factor
        for the approximation error. The output polynomial will
        minimise the largest absolute value of (P-f) * w at any point
        in the interval, where P is the value of the polynomial, f is
        the value of the target function given by <expr>, and w is the
        weight given by this function.

        When this expression is evaluated, the input value to P and f
        is predefined as 'x', and also the true output value f(x) is
        predefined as 'y'. So you can minimise the relative error by
        simply writing '1/y'.

        If the <weight> argument is not provided, the default
        weighting function always returns 1, so that the polynomial
        will minimise the maximum absolute error |P-f|.

Computation options:

    --pre=<predef_expr>

        Evaluate the Julia expression <predef_expr> before starting
        the computation. This permits you to pre-define variables or
        functions which the Julia expressions in your main arguments
        can refer to. All of <lo>, <hi>, <expr> and <weight> can make
        use of things defined by <predef_expr>.

        One internal remez.jl function that you might sometimes find
        useful in this expression is 'goldensection', which finds the
        location and value of a maximum of a function. For example,
        one implementation strategy for the gamma function involves
        translating it to put its unique local minimum at the origin,
        in which case you can write something like this

            --pre='(m,my) = goldensection(x -> -gamma(x),
                  BigFloat(1), BigFloat(1.5), BigFloat(2))'

        to predefine 'm' as the location of gamma's minimum, and 'my'
        as the (negated) value that gamma actually takes at that
        point, i.e. -gamma(m).

        (Since 'goldensection' always finds a maximum, we had to
        negate gamma in the input function to make it find a minimum
        instead. Consult the comments in the source for more details
        on the use of this function.)

        If you use this option more than once, all the expressions you
        provide will be run in sequence.

    --bits=<bits>

        Specify the accuracy to which you want the output polynomial,
        in bits. Default 256, which should be more than enough.

    --bigfloatbits=<bits>

        Turn up the precision used by Julia for its BigFloat
        evaluation. Default is Julia's default (also 256). You might
        want to try setting this higher than the --bits value if the
        algorithm is failing to converge for some reason.

Output options:

    --full

        Instead of just printing the approximation function itself,
        also print auxiliary information:
         - the locations of the error extrema, and the actual
           (weighted) error at each of those locations
         - the overall maximum error of the function
         - a 'well-conditioning quotient', giving the worst-case ratio
           between any polynomial coefficient and the largest possible
           value of the higher-order terms it will be added to.

        The well-conditioning quotient should be less than 1, ideally
        by several factors of two, for accurate evaluation in the
        target precision. If you request a rational function, a
        separate well-conditioning quotient will be printed for the
        numerator and denominator.

        Use this option when deciding how wide an interval to
        approximate your function on, and what degree of polynomial
        you need.

    --variable=<identifier>

        When writing the output polynomial or rational function in its
        usual form as an arithmetic expression, use <identifier> as
        the name of the input variable. Default is 'x'.

    --suffix=<suffix>

        When writing the output polynomial or rational function in its
        usual form as an arithmetic expression, write <suffix> after
        every floating-point literal. For example, '--suffix=F' will
        generate a C expression in which the coefficients are literals
        of type 'float' rather than 'double'.

    --array

        Instead of writing the output polynomial as an arithmetic
        expression in Horner's rule form, write out just its
        coefficients, one per line, each with a trailing comma.
        Suitable for pasting into a C array declaration.

        This option is not currently supported if the output is a
        rational function, because you'd need two separate arrays for
        the numerator and denominator coefficients and there's no
        obviously right way to provide both of those together.

Debug and test options:

    --debug=<facility>

        Enable debugging output from various parts of the Remez
        calculation. <facility> should be the name of one of the
        classes of diagnostic output implemented in the program.
        Useful values include 'gausselim', 'leastsquares',
        'goldensection', 'equaldev', 'minimax'. This is probably
        mostly useful to people debugging problems with the script, so
        consult the source code for more information about what the
        diagnostic output for each of those facilities will be.

        If you want diagnostics from more than one facility, specify
        this option multiple times with different arguments.

    --test

        Run remez.jl's internal test suite. No arguments needed.

Miscellaneous options:

    --help

        Display this text and exit. No arguments needed.

""")
end

# ----------------------------------------------------------------------
# Main program.

function main()
    nargs = length(argwords)
    if nargs != 5 && nargs != 6
        error("usage: remez.jl <lo> <hi> <n> <d> <expr> [<weight>]\n" *
              "       run 'remez.jl --help' for more help")
    end

    for preliminary_command in preliminary_commands
        eval(Meta.parse(preliminary_command))
    end

    lo = BigFloat(eval(Meta.parse(argwords[1])))
    hi = BigFloat(eval(Meta.parse(argwords[2])))
    n = parse(Int,argwords[3])
    d = parse(Int,argwords[4])
    f = eval(Meta.parse("x -> " * argwords[5]))

    # Wrap the user-provided function with a function of our own. This
    # arranges to detect silly FP values (inf,nan) early and diagnose
    # them sensibly, and also lets us log all evaluations of the
    # function in case you suspect it's doing the wrong thing at some
    # special-case point.
    function func(x)
        y = run(f,x)
        @debug("f", x, " -> ", y)
        if !isfinite(y)
            error("f(" * string(x) * ") returned non-finite value " * string(y))
        end
        return y
    end

    if nargs == 6
        # Wrap the user-provided weight function similarly.
        w = eval(Meta.parse("(x,y) -> " * argwords[6]))
        function wrapped_weight(x,y)
            ww = run(w,x,y)
            if !isfinite(ww)
                error("w(" * string(x) * "," * string(y) *
                      ") returned non-finite value " * string(ww))
            end
            return ww
        end
        weight = wrapped_weight
    else
        weight = (x,y)->BigFloat(1)
    end

    (nc, dc, e, extrema) = ratfn_minimax(func, (lo, hi), n, d, weight)
    if array_format
        if d == 0
            functext = join([string(x)*",\n" for x=nc],"")
        else
            # It's unclear how you should best format an array of
            # coefficients for a rational function, so I'll leave
            # implementing this option until I have a use case.
            error("--array unsupported for rational functions")
        end
    else
        functext = ratfn_to_string(nc, dc) * "\n"
    end
    if full_output
        # Print everything you might want to know about the function
        println("extrema = ", format_xylist(extrema))
        println("maxerror = ", string(e))
        if length(dc) > 1
            println("wellconditioning_numerator = ",
                    string(wellcond(nc, lo, hi)))
            println("wellconditioning_denominator = ",
                    string(wellcond(dc, lo, hi)))
        else
            println("wellconditioning = ", string(wellcond(nc, lo, hi)))
        end
        print("function = ", functext)
    else
        # Just print the text people will want to paste into their code
        print(functext)
    end
end

# ----------------------------------------------------------------------
# Top-level code: parse the argument list and decide what to do.

what_to_do = main

doing_opts = true
argwords = array1d(String, 0)
for arg = ARGS
    global doing_opts, what_to_do, argwords
    global full_output, array_format, xvarname, floatsuffix, epsbits
    if doing_opts && startswith(arg, "-")
        if arg == "--"
            doing_opts = false
        elseif arg == "--help"
            what_to_do = help
        elseif arg == "--test"
            what_to_do = test
        elseif arg == "--full"
            full_output = true
        elseif arg == "--array"
            array_format = true
        elseif startswith(arg, "--debug=")
            enable_debug(arg[length("--debug=")+1:end])
        elseif startswith(arg, "--variable=")
            xvarname = arg[length("--variable=")+1:end]
        elseif startswith(arg, "--suffix=")
            floatsuffix = arg[length("--suffix=")+1:end]
        elseif startswith(arg, "--bits=")
            epsbits = parse(Int,arg[length("--bits=")+1:end])
        elseif startswith(arg, "--bigfloatbits=")
            set_bigfloat_precision(
                parse(Int,arg[length("--bigfloatbits=")+1:end]))
        elseif startswith(arg, "--pre=")
            push!(preliminary_commands, arg[length("--pre=")+1:end])
        else
            error("unrecognised option: ", arg)
        end
    else
        push!(argwords, arg)
    end
end

what_to_do()


module Adapt

  include("stats.jl")
  export l1_norm, l2_norm, lp_norm

end


struct SplitHLAData <: AbstractHLAData
    name::String
    records::Vector{FASTX.FASTA.Record}
    hla_types::Vector{HLAType}
    tree::Union{Missing, PhylogeneticTree}
    stan_input::Dict{String, Any}
    allele_depth::Int
    replacement_mincount::Int
    original_data::HLAData
    split::UnitRange{Int64}
    split_idx::Int

    function SplitHLAData(
        name::String,
        records::Vector{FASTX.FASTA.Record},
        hla_types::Vector{HLAType},
        tree::Union{Missing, PhylogeneticTree},
        stan_input::Union{Missing, Dict{String, Any}},
        allele_depth::Int,
        replacement_mincount::Int,
        original_data::HLAData,
        split::UnitRange{Int64},
        split_idx::Int
    )

        if length(hla_types) != length(records)
            msg = "Vector of HLATypes has size $(length(hla_types)), " *
                "expected $(length(records))"
            error(msg)
        end

        if !ismissing(tree)
            m = Escape.matching(tree, records)
            m isa Exception && throw(m)
        end

        if !ismissing(stan_input)
            m = ErrorException("stan_input is not in a valid format.")
            is_valid(stan_input) || throw(m)
        end

        new(name, records, hla_types, tree, stan_input, allele_depth, replacement_mincount,
            original_data, split, split_idx)
    end
end

function split_hla_data(data::HLAData, n::Int = 10)
    split_data = SplitHLAData[]
    replacements = Escape.replacements(data)
    stan_input = Escape.stan_input(data)
    R = stan_input["R"]
    splits = Iterators.partition(1:R, ceil(Int, R // n))

    for (i, split) in collect(enumerate(splits))
        stan_input_split = copy(stan_input)
        in_split = findall(x -> x in split, stan_input_split["rs"])

        stan_input_split["Z"] = stan_input_split["Z"][split, :]
        stan_input_split["rs"] = map(x -> findfirst(y -> y == x, split),
        stan_input_split["rs"][in_split])
        stan_input_split["idx"] = stan_input_split["idx"][in_split]
        stan_input_split["y"] = stan_input_split["y"][in_split]
        stan_input_split["N"] = length(in_split)
        stan_input_split["R"] = length(split)
        stan_input_split["phy"] = stan_input_split["phy"][split, :]

        split_data_ = SplitHLAData(data.name, data.records, data.hla_types, data.tree,
            stan_input_split, allele_depth(data), replacement_mincount(data), data, split, i)

        @assert length(Escape.replacements(split_data_)) == stan_input_split["R"]

        push!(split_data, split_data_)
    end

    return split_data
end


"""
    apply!(σ, changes, [changes_r])

Applies the changes `changes` to the `σ`.

If `state isa DoubleState` then single-value changes
are applied to the columns of the state (in order to
compute matrix-operator products). Otherwise it should
be a tuple with changes of row and columns.

If the state is double but there is only 1 element of changes,
it's applied to the rows.

If changes is nothing, does nothing.
"""
apply!(σ::AbstractArray, cngs::Nothing) = σ

@inline function apply!(σ::ADoubleState, cngs_l::Union{StateChanges,Nothing})
    apply!(row(σ), cngs_l)
    return σ
end

@inline function apply!(σ::ADoubleState, (cngs_l, cngs_r))
    apply!(row(σ), cngs_l)
    apply!(col(σ), cngs_r)
    return σ
end

function apply!(σ::ADoubleState, cngs_l, cngs_r)
    apply!(row(σ), cngs_l)
    apply!(col(σ), cngs_r)
    return σ
end

@inline apply!(σ::AbstractVector, cngs::Nothing) = σ
function apply!(σ::AbstractVector, cngs)
    for (site, val)=cngs
        σ[site] = val
    end
    return σ
end

"""
    apply(state, cngs)

Applies the changes `cngs` to the state `σ`, by allocating a
copy.

See also @ref(apply!)
"""
apply(σ::AbstractState, cngs) = apply!(deepcopy(σ), cngs)


function general_header(op::Symbol, n::Integer, es::Integer, arity::Integer, status = false)
  prefix = "  "
  output = status ? "int" : p(n,es)
  ident  = string(p(n,es,op), status ? "" : "_e")
  statusarg = status ? ["$(p(n,es)) * res"] : []

  args   = join(vcat(statusarg, ["const $(p(n,es)) $(sym[idx])" for idx = 1:arity]),", ")
  "$prefix $output $ident($args)"
end

const binops = Dict(:add => :+, :sub => :-, :mul => :*, :div => :/, :lt => :<, :lte => :(<=) )

function cplusplus_header(op::Symbol, n::Integer, es::Integer, arity::Integer, status = false)
  if op in keys(binops)
    "extern $(c(n,es)) operator $(binops[op])($(c(n,es)) lhs, $(c(n,es)) rhs)"
  elseif op == :addinv
    "extern $(c(n,es)) operator -($(c(n,es)) arg)"
  else
    arglist = join(["$(c(n,es)) $(sym[n])" for n = 1:arity], ", ")
    "extern $(c(n,es)) $op($arglist)"
  end
end

header_set(op::Symbol, arity::Integer) = [(n, es) -> general_header(op, n, es, arity),
                                          (n, es) -> general_header(op, n, es, arity, true)]
#headers for special functions
boolean_header_c(op::Symbol) =   (n, es) -> "  bool $(p(n,es,op))(const $(p(n,es)) a,const  $(p(n,es)) b)"
boolean_header_cpp(op::Symbol) = (n, es) -> "extern bool operator $(binops[op])(const $(c(n,es)) a, const $(c(n,es)) b)"

fdp_header_c(op::Symbol) =       (n, es) -> "  $(p(n,es)) $(p(n,es,op))(fdp_cache$(n)e$(es)_t *fc, const $(p(n,es)) a, const $(p(n,es)) b)"
fdp_header_cpp(op::Symbol) =     (n, es) -> "extern $(c(n,es)) fdp(fdp_cache$(n)e$(es) *fc, const $(c(n,es)) a, const $(c(n,es)) b)"

dpi_header_c(op::Symbol) =       (n, es) -> "  void $(p(n,es,op))(fdp_cache$(n)e$(es)_t *fc)"
dpi_header_cpp(op::Symbol) =     (n, es) -> "extern void dpi(fdp_cache$(n)e$(es) *fc)"

newc_header(op::Symbol) =        (n, es) -> "  fdp_cache$(n)e$(es)_t* newcache$(n)e$(es)()"

const ordered_oplist =  [:add, :mul, :sub, :addinv, :lt, :lte, :gt, :gte, :eq, :div,
                         :mulinv, :log2, :exp2, :fma, :fms, :nfma, :nfms, :fas, :fcp, :fdp, :dpi, :newc,
                         :sqrt, :log1p, :log10, :log, :exp, :pow, :logx, :sin, :cos, :atan, :atan2]

#the ops dictionary lists

const ops = Dict(#required for all posit implementations
                 :add    => header_set(:add, 2),
                 :mul    => header_set(:mul, 2),
                 :sub    => header_set(:sub, 2),
                 :addinv => header_set(:addinv, 1),
                 :lt     => [nothing, boolean_header_c(:lt)],
                 :lte    => [nothing, boolean_header_c(:lte)],
                 :gt     => [nothing, boolean_header_c(:gt)],
                 :gte    => [nothing, boolean_header_c(:gte)],
                 :eq     => [nothing, boolean_header_c(:eq)],
                 #full posit required functions
                 :div    => header_set(:div, 2),
                 :mulinv => header_set(:mulinv, 1),
                 :log2   => header_set(:log2, 1),
                 :exp2   => header_set(:exp2, 1),
                 :fma    => header_set(:fma, 3),
                 :fms    => header_set(:fms, 3),
                 :nfma   => header_set(:nfma, 3),
                 :nfms   => header_set(:nfms, 3),
                 :fas    => header_set(:fas, 3),
                 :fcp    => header_set(:fcp, 4),
                 :fdp    => [nothing, fdp_header_c(:fdp)],
                 :dpi    => [nothing, dpi_header_c(:dpi)],
                 :newc   => [nothing, newc_header(:newc)],
                 #extended functions,
                 :sqrt   => header_set(:sqrt, 1),
                 :log1p  => header_set(:log1p, 1),
                 :log    => header_set(:log, 1),
                 :log10  => header_set(:log10, 1),
                 :exp    => header_set(:exp, 1),
                 :pow    => header_set(:pow, 2),
                 :logx   => header_set(:logx, 2),
                 :sin    => header_set(:sin, 1),
                 :cos    => header_set(:cos, 1),
                 :atan   => header_set(:atan, 1),
                 :atan2  => header_set(:atan2, 2))

function writefndef(io, op, n, es, version)
  op_fn = ops[op][version]
  write(io, op_fn(n, es))
end

function generate_posit_ops_status_h(io, posit_defs)
  write(io, """

    #ifndef __POSIT_OPS_STATUS_H
    #define __POSIT_OPS_STATUS_H

    #include "posit.h"

    extern "C"{

    """)
  for op in ordered_oplist
    for n in sort(collect(keys(posit_defs)))
      for es in posit_defs[n]
        writefndef(io, op, n, es, 2)
        write(io, ";\n")
      end
    end

    write(io, "\n")
  end
  write(io, """
  }

  #endif
  """)
end

function generate_posit_ops_errno_h(io, posit_defs)
  write(io, """

    #ifndef __POSIT_OPS_ERRNO_H
    #define __POSIT_OPS_ERRNO_H

    #include "posit.h"

    extern "C"{

    """)
  for op in ordered_oplist
    for n in sort(collect(keys(posit_defs)))
      for es in posit_defs[n]
        if ops[op][1] != nothing
          writefndef(io, op, n, es, 1)
          write(io, ";\n")
        end
      end
    end

    write(io, "\n")
  end
  write(io, """
  }

  #endif
  """)
end


struct RandomQs <: QueryStrategy end

function qs_score(qs::RandomQs, x::Array{T, 2}, labels::Dict{Symbol, Array{Int, 1}})::Array{Float64, 1} where T <: Real
    return rand(size(x, 2))
end


using Test

@testset "relu.jl" begin

@testset "ReLU" begin
    @testset "Initialize without tightening algorithm" begin
        p = ReLU()
        @test p.tightening_algorithm === nothing
    end

    @testset "Initialize with tightening algorithm" begin
        p = ReLU(MIPVerify.mip)
        @test !(p.tightening_algorithm === nothing)
        @test p.tightening_algorithm == MIPVerify.mip
    end

    @testset "Base.show" begin
        p = ReLU()
        io = IOBuffer()
        Base.show(io, p)
        @test String(take!(io)) == "ReLU()"
    end
end

end


using MaskArrays
using Test

@testset "MaskArrays.jl" begin
    include("../examples/imputation.jl")

end


# Visualization

function plot_state_city(state)
    qiskit.visualization.plot_state_city(state)
end

function plot_histogram(data)
    qiskit.visualization.plot_histogram(data)
end


## A small driver file to run the portfolio tests and combine
#run like this
#julia -p 4 -L testHarness_Paper.jl tests_port.jl
#ARGS[1] is numRun per batch

using Distributed

const spath = "./Results/portExpOrig"
const param_path = "./Results/param_portExp_mtn2.csv"

n_grid = [2^i for i = 5:17]
numRuns = parse(Int, ARGS[1])

##VG
##original experimentswere run with GammaMin, Gammamax = 1, 20.
##  The "Big" Experiment is 5, 40 step = .01
## The Big100 Experiment is 10, 100 step = .5
start_time = time_ns()
a = @spawn test_ReadData(spath, numRuns, n_grid, 8675309, param_path, Gamma_min=1, Gamma_max=20., Gamma_step =.01)
b = @spawn test_ReadData(spath, numRuns, n_grid, 5164174290, param_path, Gamma_min=1, Gamma_max=20., Gamma_step=.01)
c = @spawn test_ReadData(spath, numRuns, n_grid, 123456, param_path, Gamma_min=1, Gamma_max=20., Gamma_step =.01)
d = @spawn test_ReadData(spath, numRuns, n_grid, 5167462266, param_path, Gamma_min=1, Gamma_max=20., Gamma_step =.01)

######
file_a = fetch(a)
file_b = fetch(b)
file_c = fetch(c)
file_d = fetch(d)

time_stamp = (time_ns() - start_time) * 1e-9

##read everyone in, throw away a line
data, header = readdlm(file_a, ',', header=true)

data_t = readdlm(file_b, ',', skipstart=1)

data_t[:, 1] .+= numRuns
data = vcat(data, data_t)

data_t = readdlm(file_c, ',', skipstart=1)
data_t[:, 1] .+= 2numRuns
data = vcat(data, data_t)

data_t = readdlm(file_d, ',', skipstart=1)
data_t[:, 1] .+= 3numRuns
data = vcat(data, data_t)

#strip the name of file_a to make the numbers better
println("Filea \t", file_a)
println("spath \t", spath)

#indx = search(file_a, spath)[end] + 1
indx =  findfirst(spath, file_a)[end] + 1

f = open("$(spath)_$(file_a[indx:end])_full_$(4numRuns).csv", "w")
writedlm(f,  header, ',')
writedlm(f,  data, ',')
close(f)

println("Num Paths: \t $(numRuns) \t Time:", time_stamp )


@testset "examples" begin
    for size in [3, 792, 40_000]
        for generator in [hyper_pa]
            @test example(generator, size) isa Vector{Vector{Int}}
        end
        for generator in [DCHSBM_sampler, Kronecker_sampler]
            @test example(generator, size) isa Vector{<:NTuple}
        end
    end
    println("\nMillions of entries per second")
    display(MEPS(size=10_000, trials=6))
    println("\n")
end


using Test
using Liblet

# Constructors

test_production_wrong_lhs() = Production(1, ["a"])

test_production_nonempty_lhs() = Production("",[""])

test_production_wrong_rhs() = Production("a",[1])

test_production_nonemptystr_rhs() = Production("a",["a","","c"])

test_production_nonempty_rhs() = Production("a",[])

test_production_inset() = begin
    P = Production("a", ["b", "c"])
    Q = Production("a", ("b", "c"))
    P == Q
end

test_production_aε() = Production("A", ["a", "ε"])

# Operators

test_production_unpack() = begin
    lhs, rhs = Production("a", ["b", "c"])
    ("a", ["b", "c"]) == (lhs, rhs)
end

test_production_totalorder() = Production("a", ["b"]) > Production("a", ["a"])

test_production_from_string_cf() = parseproduction("A B -> c", true)

# astype0

test_production_astype0() = Production(["a"], ["b"]) == astype0(Production("a", ["b"]))

# suchthat

test_production_such_that_lhs() = suchthat(left = "X")(Production("X", ["x"]))

test_production_such_that_rhs() = suchthat(right = "x")(Production("X", ["x"]))

test_production_such_that_rhs_len() = suchthat(rightlen = 2)(Production("X", ["x", "y"]))

test_production_such_that_rhs_is_suffix_of() = suchthat(right_is_suffix_of = ["a", "x"])(Production("X", ["x"]))

# Tests

function runproductiontests()
    @testset "production_tests" begin
        @test_throws ArgumentError test_production_wrong_lhs()
        @test_throws ArgumentError test_production_nonempty_lhs()
        @test_throws ArgumentError test_production_wrong_rhs()
        @test_throws ArgumentError test_production_nonemptystr_rhs()
        @test_throws ArgumentError test_production_nonempty_rhs()
        @test test_production_inset()
        @test_throws ArgumentError test_production_aε()

        @test test_production_unpack()
        @test test_production_totalorder()

        @test test_production_astype0()

        @test test_production_such_that_lhs()
        @test test_production_such_that_rhs()
        @test test_production_such_that_rhs_len()
        @test test_production_such_that_rhs_is_suffix_of()
    end
end


using Arrow
using BenchmarkTools

const L = 10^7

function wrap(::Type{T}, len::Integer, v::Vector{UInt8}) where T
    ptr = convert(Ptr{T}, pointer(v))
    unsafe_wrap(Array, ptr, len)
end

function randmissings(::Type{T}, len::Integer) where T

end

function benches1()
    A = convert(Vector{UInt8}, reinterpret(UInt8, rand(Int64, L)))

    @info("performing wrap benchmark...")
    global b_wrap = @benchmark wrap(Int64, $L, $A)

    @info("performing wrap benchmark with missings...")
    global b_wrap_missing = @benchmark convert(Vector{Union{Int,Missing}}, wrap(Int, $L, $A))

    @info("performing reinterpret benchmark...")
    global b_reint = @benchmark reinterpret(Int64, $A)

    p = Primitive{Int64}(A, 1, L)

    bmask = zeros(UInt8, Arrow.bitmaskbytes(L))
    A2 = vcat(bmask, A)
    println(pointer(A2))
    p2 = NullablePrimitive{Int64}(A2, 1, length(bmask)+1, L)
    println(Arrow.valuespointer(p2))

    @info("performing Arrow benchmark...")
    global b_arrow = @benchmark Arrow.getindex($p, 1:($L-1))

    @info("performing Arrow nullable benchmark...")
    global b_arrow2 = @benchmark Arrow.getindex($p2, 1:($L-1))

    global b_idx = @benchmark Arrow.rawvalueindex($p, 1:length($p))
end

function benches2()
    A = String[randstring(rand(4:12)) for i ∈ 1:L]

    l = NullableList(A)

    info("performing Arrow benchmark...")
    global b_arrow = @benchmark Arrow.getindex($l, 1:$L)
end

benches1()
# benches2()


@testset "clock" begin
    @testset "basic" begin
        @system SClock(Controller)
        s = instance(SClock)
        @test s.context.clock.time' == 0u"hr"
        @test s.context.clock.tick' === 0
        update!(s)
        @test s.context.clock.time' == 1u"hr"
        @test s.context.clock.tick' === 1
        update!(s)
        @test s.context.clock.time' == 2u"hr"
        @test s.context.clock.tick' === 2
    end

    @testset "config" begin
        @system SClockConfig(Controller)
        o = :Clock => (#=:init => 5,=# :step => 10)
        s = instance(SClockConfig; config=o)
        @test s.context.clock.time' == 0u"hr"
        @test s.context.clock.tick' === 0
        update!(s)
        @test s.context.clock.time' == 10u"hr"
        @test s.context.clock.tick' === 1
        update!(s)
        @test s.context.clock.time' == 20u"hr"
        @test s.context.clock.tick' === 2
    end

    @testset "daily" begin
        @system SClockDaily{Context => Cropbox.DailyContext}(Controller) begin
            a => 1 ~ accumulate
        end
        s = instance(SClockDaily)
        @test s.context isa Cropbox.DailyContext
        @test s.context.clock isa Cropbox.DailyClock
        @test s.context.clock.time' == 0u"d"
        @test s.context.clock.tick' === 0
        update!(s)
        @test s.context.clock.time' == 1u"d"
        @test s.context.clock.tick' === 1
        @test s.a' == 1
    end
end


using TicketToRide
using Test

r = rand_state(10)
@test copy(r) |> put(10, 2=>NoGate()) ≈ r


using PyPlot
using RollingFunctions

function plot_acceptance(updts::Vector{<:ParamUpdate{MetropolisHastingsUpdt}})
    fig, ax = plt.subplots(2, length(updts), figsize=(3*length(updts), 5))
    for (i,updt) in enumerate(updts)
        ax[1,i].plot(updt.readjust_history, color="steelblue", linewidth=1.0)
        ax[2,i].plot(rollmean(updt.accpt_history, 100), color="steelblue", linewidth=1.0)
        coord = indices(updt.updt_coord)[1]
        print(coord)
        ax[1,i].set_title(string("Parameter ", string(coord)))
    end
    ax[1,1].set_ylabel("dispersion")
    ax[2,1].set_ylabel("acceptance rate")
    plt.tight_layout()
    ax
end

function plot_acceptance(updts::Vector{<:Imputation}, adaptations=nothing, adpt_intv=100)
    max_num_segm = maximum([length(updt.ρs) for updt in updts])
    fig, ax = plt.subplots(2*length(updts), max_num_segm, figsize=(3*max_num_segm, 5*length(updts)))
    if adaptations !== nothing
        adaptations = cumsum(adaptations)
    end
    for (i,updt) in enumerate(updts)
        for j in 1:length(updt.ρs_history)
            ρ = [ρs[j] for ρs in updt.ρs_history]
            ah = [a[j] for a in updt.accpt_history]
            ax[2*(i-1)+1,j].plot(ρ, color="steelblue", linewidth=1.0)
            ax[2*(i-1)+2,j].plot(rollmean(ah, adpt_intv), color="steelblue", linewidth=1.0)
            ylimA = ax[2*(i-1)+1,j].get_ylim()
            ylimB = ax[2*(i-1)+2,j].get_ylim()
            if adaptations !== nothing
                for adpt in adaptations
                    ax[2*(i-1)+1,j].plot([adpt/adpt_intv, adpt/adpt_intv], ylimA, color="red", linestyle="dashed", linewidth=1.0)
                    ax[2*(i-1)+2,j].plot([adpt, adpt], ylimB, color="red", linestyle="dashed", linewidth=1.0)
                end
            end
        end
        ax[2*(i-1)+1,1].set_ylabel("dispersion")
        ax[2*(i-1)+2,1].set_ylabel("acceptance rate")
    end
    plt.tight_layout()
    ax
end

function plot_chains(ws::MCMCWorkspace,indices=nothing;truth=nothing,figsize=(15,10),
                     ylims=nothing)
    θs = ws.θ_chain
    num_steps = length(θs)
    if indices === nothing
        indices = 1:length(θs[1])
    end
    n_chains = length(indices)

    fig, ax = plt.subplots(n_chains, 1, figsize=figsize, sharex=true)

    for (i,ind) in enumerate(indices)
        ax[i].plot([θ[ind] for θ in θs],color="steelblue", linewidth=0.5)
    end
    if truth !== nothing
        for (i,ind) in enumerate(indices)
            ax[i].plot([1,length(θs)],[truth[ind],truth[ind]],color="black",
                       linestyle="--")
        end
    end
    if ylims !== nothing
        for (i,axis) in enumerate(ax)
            if ylims[i] !== nothing
                axis.set_ylim(ylims[i])
            end
        end
    end
    plt.tight_layout()
    ax
end

function plot_paths(ws::Workspace, ws_mcmc::MCMCWorkspace, schedule, coords=nothing; transf=nothing,
                    figsize=(12,8), alpha=0.5, obs=nothing,
                    path_indices=1:length(ws.paths), ylims=nothing, θ_fixed=nothing)
    yy, tt = ws.paths, ws.time
    if coords === nothing
        coords = 1:length(yy[1][1])
    end
    if transf === nothing
        transf = [(x,θ)->x for _ in coords]
    end
    n_coords = length(coords)
    if θ_fixed !== nothing
        θs = fill(θ_fixed, length(yy))
    else
        θs = θs_for_transform(ws_mcmc, schedule)
    end

    fig, ax = plt.subplots(n_coords, 1, figsize=figsize, sharex=true)
    M = min(length(yy), length(path_indices))
    cmap = plt.get_cmap("BuPu")(range(0, 1, length=M)) #"cividis"
    for (i,ind) in enumerate(coords)
        cc = 1
        for j in 1:length(yy)
            if j in path_indices
                path = [transf[i](y, θs[j])[ind] for y in yy[j]]
                ax[i].plot(tt, path, color=cmap[cc,1:end], alpha=alpha, linewidth=0.5)
                cc += 1
            end
        end
    end

    _i = 1
    if obs !== nothing
        for (i,ind) in enumerate(coords)
            if ind in obs.indices
                ax[i].plot(obs.times, [o[_i] for o in obs.vals], mfc="orange",
                           mec="orange", marker="o", linestyle="none",
                           markersize=4)
                _i += 1
            end
        end
    end
    if ylims !== nothing
        for (i,axis) in enumerate(ax)
            if ylims[i] !== nothing
                axis.set_ylim(ylims[i])
            end
        end
    end
    plt.tight_layout()
    ax
end

function θs_for_transform(ws::Any) # old implementation
    θs = ws.θ_chain.θ_chain
    if ws.θ_chain.counter < 2
        θs = [params(ws.P[1].Target) for i in 1:length(ws.paths)]
    else
        warm_up, save_iter = ws.action_tracker.warm_up, ws.action_tracker.save_iter
        num_updts = ws.accpt_tracker.updt_len
        N = Int64((length(θs)-1)/num_updts) + warm_up
        θs = θs[[num_updts*(i-warm_up) for i in 1:N
                 if (i % save_iter == 0) && i > warm_up ]]
    end
    θs
end

function θs_for_transform(ws::MCMCWorkspace, schedule)
    θs = ws.θ_chain
    warm_up, save_iter = schedule.actions.warm_up, schedule.actions.save
    num_updts = length(schedule.updt_idx[1])-1
    N = Int64((length(θs)-1)/num_updts) + warm_up
    θs = θs[[num_updts*(i-warm_up) for i in 1:N
            if (i % save_iter == 0) && i > warm_up ]]
    θs
end


module EvolutionaryStrategies

using Cambrian
using Random
using LinearAlgebra

abstract type ESState end

include("individual.jl")
include("exponential_nes.jl")
include("separable_nes.jl")
include("cmaes.jl")
include("random_search.jl")

end


push!(LOAD_PATH, "../src")
using Base.Test
using PumiInterface
using ODLCommonTools
using SummationByParts
using PdePumiInterface

function not_isapprox(args...; kwargs...)
  return !isapprox(args...; kwargs...)
end

include("defs.jl")
include("common_functions.jl")
include("test_funcs.jl")
include("test_adapt.jl")

  dmg_name = "parallel2.dmg"
  smb_name = "./parallel2.smb"
  order = 1
@testset "Testing PUMIInterface.jl" begin

  m_ptr, dim = apf.loadMesh(dmg_name, smb_name, order)
  mshape_ptr, num_Entities, n_arr = apf.initMesh(m_ptr)
#  num_Entities, m_ptr, mshape_ptr = apf.init2(dmg_name, smb_name, order)

  myrank = MPI.Comm_rank(MPI.COMM_WORLD)
  @test ( num_Entities[1] )== 6
  @test ( num_Entities[2] )== 9
  @test ( num_Entities[3] )== 4

  @test ( apf.countPeers(m_ptr, apf.VERTEX) )== 1
  peers = Array{Int32}(1)
  apf.getPeers(m_ptr, peers)

  @test ( peers[1] )== 1-myrank

  verts = Array{Ptr{Void}}(num_Entities[1])
  edges = Array{Ptr{Void}}(num_Entities[2])
  faces = Array{Ptr{Void}}(num_Entities[3])

  for (i, e) in enumerate(apf.MeshIterator(m_ptr, 0))
    verts[i] = e
  end

  for (i, e) in enumerate(apf.MeshIterator(m_ptr, 1))
    edges[i] = e
  end

  for (i, e) in enumerate(apf.MeshIterator(m_ptr, 2))
    faces[i] = e
  end

  # check the apf.countRemotes function
  # the only thing that can really be tested is the number of total remotes
  remote_cnt = 0 # count the total number of remotes
  remote_sum = 0 # sum of the remote values
  part_nums = Array{Cint}(1)
  entities = Array{Ptr{Void}}(1)

  sleep(5*myrank)
  for i=1:length(verts)
    nremotes = apf.countRemotes(m_ptr, verts[i])
    remote_cnt += Int(nremotes)
    if nremotes != 0
      apf.getRemotes(part_nums, entities)
      remote_sum += Int(part_nums[1])
    end
  end

  @test ( remote_cnt )== 3
  @test ( remote_sum )== 3*(1 - myrank)

  remote_cnt = 0
  remote_sum = 0
  for i=1:length(edges)
    nremotes =  apf.countRemotes(m_ptr, edges[i])
    remote_cnt += Int(nremotes)
    if nremotes != 0
      apf.getRemotes(part_nums, entities)
      remote_sum += Int(part_nums[1])
    end
  end

  @test ( remote_cnt )== 2
  @test ( remote_sum )== 2*(1-myrank)
end

@testset "----- Testing PdePumiInterfaceDG -----" begin
  opts = Dict{Any, Any}(
    "dmg_name" => dmg_name,
    "smb_name" => smb_name,
    "coloring_distance" => 2,
    "order" => order,
    "numBC" => 1,
    "BC1" =>  [0,1,2,3],
    "run_type" => 1,
    "verify_coloring" => true,
    "use_edge_res" => false,
    "write_edge_vertnums" => true,
    "write_face_vertnums" => true,
    "write_boundarynums" => true,
    "write_dxidx" => false,
    "write_coords" => false,
    "write_sparsity" => false,
    "write_offsets" => false,
    "write_counts" => false,
    "write_sparsity_nodebnds" => false,
    "write_dofs" => false,
    "write_interfaces" => false,
    "write_boundaries" => false,
    "write_sharedboundaries" => false,
    "use_linear_metrics" => true,
    )

  sbp = getTriSBPOmega(degree=order)
#  sbp = TriSBP{Float64}(degree=order, internal=true)

  vtx = [-1. 1 -1; -1 -1 1]
#  interp_op = SummationByParts.buildinterpolation(sbp, vtx)
  sbpface = TriFace{Float64}(order, sbp.cub, vtx.')
  mesh_c = PumiMeshDG2(Complex128, sbp, opts, sbpface)
#  mesh_c = PumiMeshDG2{Complex128, typeof(sbpface)}(dmg_name, smb_name, order, sbp, opts, sbpface)
  mesh = PumiMeshDG2(Float64, sbp, opts, sbpface)
#  mesh = PumiMeshDG2{Float64, typeof(sbpface)}(dmg_name, smb_name, order, sbp, opts, sbpface)

  @test ( mesh.numVert )== 6
  @test ( mesh.numEdge )== 9
  @test ( mesh.numEl )== 4
  @test ( mesh.numInterfaces )== 3
  @test ( mesh.myrank )== MPI.Comm_rank(mesh.comm)
  @test ( mesh.commsize )== 2  # these tests only work for 2 ranks
  @test ( mesh.peer_parts[1] )== 1-(mesh.myrank)
  @test ( mesh.peer_face_counts[1] )== 2
  @test ( length(mesh.bndries_local[1]) )== 2
  @test ( length(mesh.bndries_remote[1]) )== 2
  @test ( length(mesh.shared_interfaces[1]) )== 2

  # check the interfaces
  for i=1:length(mesh.shared_interfaces[1])
    interface_i = mesh.shared_interfaces[1][i]
    @test  interface_i.elementL  > 0
    @test  interface_i.elementL  < mesh.numEl + 1
    @test  interface_i.elementR  > mesh.numEl
    @test  interface_i.elementR  < mesh.numEl + 3
    @test ( interface_i.orient )== 1
  end

  @test ( mesh.shared_element_offsets[1] )== (mesh.numEl + 1)
  @test ( mesh.shared_element_offsets[2] )== (mesh.numEl + 3)

  # check the gathering of the elements on the boundaries
  for i=1:mesh.npeers
    peernum = mesh.peer_parts[i]
    local_els = mesh.local_element_lists[i]
    for j = 1:length(local_els)
      el = local_els[j]
      el_ptr = mesh.elements[el]
      down, tmp = PumiInterface.apf.getDownward(mesh.m_ptr, el_ptr, apf.EDGE)
      cnt = 0 # count number of shared edges
      for k=1:3
        if apf.isShared(mesh.m_ptr, down[k])
          nremotes = PumiInterface.apf.countRemotes(mesh.m_ptr, down[k])
          partnums = Array{Cint}(nremotes)
          entities = Array{Ptr{Void}}(nremotes)
          PumiInterface.apf.getRemotes(partnums, entities)
          for p=1:nremotes
            if partnums[p] == peernum
              cnt += 1
            end  # end if
          end  # end loop p
        end  # end if apf.isShared
      end  # end loop k

      @test  cnt  > 0
    end  # end loop j

    # check element numbers are unique
    local_elnums = copy(local_els)
    sort!(local_elnums)
    @test ( local_elnums )== unique(local_elnums)
  end  # end loop i

  # check the interpolation
  # use the fact that dxidx, jac are constant across the element
  for p=1:mesh.npeers
    interfaces_p = mesh.shared_interfaces[p]
    dxidx_p = mesh.dxidx_sharedface[p]
    jac_p = mesh.jac_sharedface[p]
    for i=1:mesh.peer_face_counts[p]
      interface_i = interfaces_p[i]
      dxidx_ref = mesh.dxidx[:, :, 1, interface_i.elementL]
      jac_ref= mesh.jac[1, interface_i.elementL]

      for j=1:mesh.sbpface.numnodes
        for k=1:2
          for m=1:2
            @test isapprox( dxidx_p[k, m, j, i], dxidx_p[k,m]) atol=1e-12
          end
        end
        @test isapprox( jac_p[j, i], jac_ref) atol=1e-12
      end
    end
  end

  # check the adjacency dictonary
  adj_dict, revadj = PdePumiInterface.getLocalAdjacency(mesh)
  @test ( length(adj_dict) )== mesh.numSharedEl
  # now check that no element appears more than twice
  count_dict = Dict{Int, Int}()
  for (key, val) in adj_dict
    el1 = val[1]
    el2 = val[2]
    for elnums = 1:2
      val_i = val[elnums]
      if !haskey(count_dict, val_i)
        count_dict[val_i] = 1
      else
        count_dict[val_i] = count_dict[val_i] + 1
      end
    end
  end

  for (key, val) in count_dict
    @test  val   < 3
    @test ( val[1] )!=0
  end

  # check the revadj
  count_dict2 = Dict{Int, Int}()
  for i=1:mesh.numSharedEl
    val1 = revadj[i, 1]
    val2 = revadj[i, 2]
    @test ( val1 )!=0
    if !haskey(count_dict2, val1)
      count_dict2[val1] = 1
    else
      count_dict2[val1] += 1
    end

    if val2 != 0
      if !haskey(count_dict2, val2)
        count_dict2[val1] = 1
      else
        count_dict2[val1] += 1
      end
    end

  end

  # check no value appears more than twice
  for (key, val) in count_dict2
    @test  val  < 3
    @test ( val )!=0
  end

  # check neighbor_colors for uniqueness
  myrank = mesh.myrank
  for i=1:mesh.numEl
    colors_i = mesh.neighbor_colors[:, i]
    sort!(colors_i)
    nzs = countnz(colors_i)
    nz_start = length(colors_i) - nzs + 1
    colors_i2 = colors_i[nz_start:end]
    @test ( colors_i2 )== unique(colors_i2)
  end

  for i=1:mesh.npeers
    numel_i = mesh.shared_element_offsets[i+1] - mesh.shared_element_offsets[i]
    masks = mesh.shared_element_colormasks[i]
    for j = 1:numel_i
      val = 0
      for k = 1:mesh.numColors
        val += masks[k][j]
      end
      @test ( val )== 1  # each element should have exactly 1 color
    end
  end

  # check dof numbers for non-local elements
  npeer_els = mesh.shared_element_offsets[end] = mesh.shared_element_offsets[1]
  dof_min = mesh.numDofPerNode*mesh.numNodesPerElement*mesh.numEl
  dof_max = mesh.numDofPerNode*mesh.numNodesPerElement*(npeer_els + mesh.numEl)  + 1
  for i=(mesh.numEl + 1):size(mesh.dofs, 3)
    for j=1:mesh.numNodesPerElement
      for k=1:mesh.numDofPerNode
        if mesh.myrank == 0
          @test  (mesh.dofs[k, j, i] + mesh.dof_offset)  > dof_min
          @test  (mesh.dofs[k, j, i] + mesh.dof_offset)  < dof_max
        else
          @test  mesh.dofs[k, j, i]  < 1
        end
      end
    end
  end

  vshare = mesh.vert_sharing
  @test ( vshare.npeers )== 1
  @test ( vshare.peer_nums )== [1 - mesh.myrank]
  @test ( vshare.counts[1] )== 3
  @test ( length(vshare.vert_nums[1]) )== 3
  @test ( length(keys(vshare.rev_mapping)) )== 3

  # check vert_nums and rev_mapping in more detail
  for i=1:3
    @test  vshare.vert_nums[1][i]  > 0
    @test  vshare.vert_nums[1][i]  < mesh.numVert + 1
  end

  for (key, val) in vshare.rev_mapping
    @test ( length(val.first) )== 1
    @test ( length(val.second) )== 1
    @test ( val.first[1] )== 1-mesh.myrank  # part number
    @test  val.second[1]  > 0
    @test  val.second[1]  < 4# number of shared vertices + 1
  end

  compare_meshes(mesh, mesh_c)
  test_metrics_rev(mesh, mesh_c, sbp, opts)

  MPI.Barrier(mesh.comm)

end

@testset "----- Testing PdePumiInterface3DG -----" begin

  degree = 1
  Tsbp = Float64
  sbp = getTetSBPOmega(degree=degree)
#  sbp = TetSBP{Tsbp}(degree=degree, internal=true)
  ref_verts = sbp.vtx
  interp_op = SummationByParts.buildinterpolation(sbp, ref_verts.')
  face_verts = SummationByParts.SymCubatures.getfacevertexindices(sbp.cub)
  topo = ElementTopology{3}(face_verts)
  sbpface = TetFace{Tsbp}(degree, sbp.cub, ref_verts)

  dmg_name = ".null"
  smb_name = "pcube2.smb"

  opts = PdePumiInterface.get_defaults()
  opts["dmg_name"] = dmg_name
  opts["smb_name"] = smb_name
  opts["use_linear_metrics"] = true
  opts["coloring_distance"] = 2
  opts["order"] = degree
  opts["numBC"] = 1
  opts["BC1"] = [0,1,2,3,4,5]

#  interp_op = eye(4)
  mesh_c = PumiMeshDG3(Complex128, sbp, opts, sbpface, topo)
#  mesh_c = PumiMeshDG3{Complex128, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)
  mesh = PumiMeshDG3(Float64, sbp, opts, sbpface, topo)
#  mesh = PumiMeshDG3{Float64, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)

  @test ( mesh.numEl )== 12
  @test ( mesh.numGlobalEl )== 16
#  @test ( mesh.numBoundaryFaces )==#  @fact mesh.numBoundaryFaces --> 4
  @test ( mesh.numInterfaces )== 14
  @test ( mesh.numBoundaryFaces )== 16
  @test ( mesh.myrank )== MPI.Comm_rank(mesh.comm)
  @test ( mesh.commsize )== MPI.Comm_size(mesh.comm)

  if mesh.myrank == 0
    @test ( mesh.peer_parts )== [1]
  else
    @test ( mesh.peer_parts )== [0]
  end

  @test ( mesh.peer_face_counts[1] )== 4

  # TODO: test dxidx, jac sharedface
  # check interface array
  for i=1:mesh.numInterfaces
    iface_i = mesh.interfaces[i]
    @test  iface_i.elementL  > 0
    @test  iface_i.elementL  < mesh.numEl+1
    @test  iface_i.elementR  > 0
    @test  iface_i.elementR  < mesh.numEl+1
    @test  iface_i.faceL  > 0
    @test  iface_i.faceL  < 5
    @test  iface_i.faceR  > 0
    @test  iface_i.faceR  < 5
    @test  iface_i.orient  > 0
    @test  iface_i.orient  < 4
  end

  # check boundary array
  for i=1:mesh.numBoundaryFaces
    bndry_i = mesh.bndryfaces[i]
    @test  bndry_i.element  > 0
    @test  bndry_i.element  < mesh.numEl + 1
    @test  bndry_i.face  > 0
    @test  bndry_i.face  < 5
  end

  # check mapping interpolation
  # should be constant within an element for straight-sided elements
  for i=1:mesh.numInterfaces
    iface_i = mesh.interfaces[i]
    el_i = iface_i.elementL
    dxidx_el = mesh.dxidx[:, :, 1, el_i]
    jac_el = mesh.jac[:, el_i]
    jac_face = mesh.jac_face[:, i]

    for j=1:mesh.numNodesPerFace
      dxidx_face = mesh.dxidx_face[:, :, j, i]
      for k=1:3
        for p=1:3
          @test isapprox( dxidx_face[p, k], dxidx_el[p, k]) atol=1e-13
        end
      end
      @test isapprox( jac_face[j], jac_el[j]) atol=1e-13
    end
  end  # end loop over interfaces

  # check mapping interpolation
  for i=1:mesh.npeers

    dxidx_peer = mesh.dxidx_sharedface[i]
    jac_peer = mesh.jac_sharedface[i]
    for j=1:mesh.peer_face_counts[i]
      iface_i = mesh.shared_interfaces[i][j]
      el_j = iface_i.elementL
      dxidx_el = mesh.dxidx[:, :, 1, el_j]
      jac_el = mesh.jac[:, el_j]
      jac_face = jac_peer[:, j]

      for k=1:mesh.numNodesPerFace
        dxidx_face = dxidx_peer[:, :, k, j]
        for p=1:3
          for n=1:3
            @test isapprox( dxidx_face[n, p], dxidx_el[n, p]) atol=1e-13
          end
        end
        @test isapprox( jac_face[k], jac_el[k]) atol=1e-13
      end
    end   # end loop over peer faces
  end  # end loop over peers

  # test curvilinear metrics reverse mode
  compare_meshes(mesh, mesh_c)
  test_metrics_rev(mesh, mesh_c, sbp, opts)

  # test periodic things
  smb_name = "tet2_pxz_p2_.smb"

  opts = PdePumiInterface.get_defaults()
  opts["smb_name"] = smb_name
  opts["dmg_name"] = dmg_name
  opts["use_linear_metrics"] = true
  opts["coloring_distance"] = 2
  opts["order"] = degree
  opts["numBC"] = 1
  opts["BC1"] = [0,2,4,5]

  mesh = PumiMeshDG3(Float64, sbp, opts, sbpface, topo)
#  mesh = PumiMeshDG3{Float64, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)

  @test ( mesh.numPeriodicInterfaces )== 4
  @test ( mesh.numInterfaces )== (mesh.numFace - 8 - 3*4 - 8)
  @test ( mesh.peer_face_counts[1] )== 8

  smb_name = "tet2_pxy_p2_.smb"
  opts["smb_name"] = smb_name
  opts["BC1"] = [1,2,3,4]
  mesh = PumiMeshDG3(Float64, sbp, opts, sbpface, topo)
#  mesh = PumiMeshDG3{Float64, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)

  @test ( mesh.numPeriodicInterfaces )== 0
  @test ( mesh.numInterfaces )== (mesh.numFace - 2*8 - 4*4)
  @test ( mesh.peer_face_counts[1] )== 16

  vshare = mesh.vert_sharing
  @test ( vshare.npeers )== 1
  @test ( vshare.peer_nums )== [1 - mesh.myrank]
  @test ( vshare.counts[1] )== 18
  @test ( length(vshare.vert_nums[1]) )== 18
  @test ( length(keys(vshare.rev_mapping)) )== 18

  # check vert_nums and rev_mapping in more detail
  for i=1:18
    @test  vshare.vert_nums[1][i]  > 0
    @test  vshare.vert_nums[1][i]  < mesh.numVert + 1
  end

  for (key, val) in vshare.rev_mapping
    @test ( length(val.first) )== 1
    @test ( length(val.second) )== 1
    @test ( val.first[1] )== 1-mesh.myrank  # part number
    @test  val.second[1]  > 0
    @test  val.second[1]  < 19# number of shared vertices + 1
  end

  MPI.Barrier(mesh.comm)

  # check assertion for ambiguous parallel things
  dmg_name = ".null"
  smb_name = "tet111_.smb"

  opts = PdePumiInterface.get_defaults()
  opts["dmg_name"] = dmg_name
  opts["smb_name"] = smb_name
  opts["order"] = degree
  opts["numBC"] = 0
  opts["coloring_distance"] = 2

  @test_throws Exception  PumiMeshDG3(Float64, sbp, opts, sbpface, topo)
#  @test_throws Exception #  @fact_throws PumiMeshDG3{Float64, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)

  # check 2 x 2 x 2 mesh works
  dmg_name = ".null"
  smb_name = "tet222_.smb"

  opts["dmg_name"] = dmg_name
  opts["smb_name"] = smb_name

  PumiMeshDG3(Float64, sbp, opts, sbpface, topo)
#  PumiMeshDG3{Float64, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)

  # this doesn't work because each partition is a 1 x 1 x 1 cube with periodic faces
  dmg_name = ".null"
  smb_name = "tet211_.smb"
  opts["dmg_name"] = dmg_name
  opts["smb_name"] = smb_name

  @test_throws Exception  PumiMeshDG3(Float64, sbp, opts, sbpface, topo)
#  @test_throws Exception #  @fact_throws PumiMeshDG3{Float64, typeof(sbpface)}(dmg_name, smb_name, degree, sbp, opts, sbpface, topo)

end

MPI.Barrier(MPI.COMM_WORLD)
if MPI.Initialized()
  MPI.Finalize()
end


module Ensemble

include("Stats.jl")

include("Acor.jl")
include("EnsembleSampler.jl")
include("EnsembleKombine.jl")
include("EnsembleNest.jl")
include("EnsembleGibbs.jl")
include("EnsemblePTSampler.jl")
include("Optimize.jl")
include("Parameterizations.jl")

using .Acor
using .EnsembleSampler
using .EnsembleGibbs
using .EnsembleKombine
using .EnsembleNest
using .EnsemblePTSampler
using .Optimize
using .Parameterizations
using .Stats

export Acor, EnsembleSampler, EnsembleGibbs, EnsembleKombine, EnsembleNest, EnsemblePTSampler, Optimize, Parameterizations, Stats

end


using Luxor, Colors, ProfileSVG
using ProfileSVG: FGConfig, bgcolor, fontcolor,
    write_svgheader, write_svgfooter, write_svgflamerect

_depth(ev::Dict) = haskey(ev, :children) ? 1 + _depth(ev[:children]) : 1
_depth(evs::Vector) = isempty(evs) ? 0 : maximum(_depth(ev) for ev in evs)

function drawevent(ev::Dict, y, offset, δ, colormap, ystep, cols)
    c = get!(colormap, ev[:name], rand(cols))
    sethue(c)
    x₁ = δ*(ev[:start] - offset)
    x₂ = δ*(ev[:stop] - offset)
    points = [Point(x₁, y), Point(x₂, y), Point(x₂, y + ystep), Point(x₁, y + ystep)]
    poly(points, :fill)
    haskey(ev, :children) && drawevent(ev[:children], y + ystep, offset, δ, colormap, ystep, cols)
end

function drawevent(evs::Vector, y, offset, δ, colormap, ystep, cols)
    !isempty(evs) && foreach(ev -> drawevent(ev, y, offset, δ, colormap, ystep, cols), evs)
end

"""
    export2luxor(filename::String; width = 2000, height = 800)

    Export the execution profile to a figure using Luxor.
    Return colormap mapping name of the function to colors, which
    I do not know, how to export
"""
function export2luxor(filename::String; width = 2000, height = 800)
    thev = map(LoggingProfiler.tape2structure, LoggingProfiler.to)
    start = minimum(first(e)[:start] for e in thev)
    stop = maximum(last(e)[:stop] for e in thev)
    δ =  width / (stop - start)
    colormap = Dict()
    cols = distinguishable_colors(100)
    depths = [_depth(ev) for ev in thev]
    ystep = fld(height, sum(d + 1 for d in depths) - 1)

    yoffset = 0
    Drawing(width, height, filename)
    background("white")
    for (ev, d) in zip(thev, depths)
        drawevent(ev, yoffset, start, δ, colormap, ystep, cols)
        yoffset += (d + 1) * ystep
    end
    finish()
    colormap
end

function drawevent_svg(io::IO, ev::Dict, y, offset, δ, colormap, ystep, cols)
    color = get!(colormap, ev[:name], rand(cols))
    x = δ*(ev[:start] - offset)
    w =  δ*(ev[:stop]  - ev[:start])
    shortinfo = "$(ev[:name])"
    dirinfo = "$(ev[:name])"
    roundradius = 2
    write_svgflamerect(io, x, y, w, ystep, roundradius, shortinfo, dirinfo, color, true)
    haskey(ev, :children) && drawevent_svg(io, ev[:children], y + ystep, offset, δ, colormap, ystep, cols)
end

function drawevent_svg(io::IO, evs::Vector, y, offset, δ, colormap, ystep, cols)
    !isempty(evs) && foreach(ev -> drawevent_svg(io, ev, y, offset, δ, colormap, ystep, cols), evs)
end

"""
    export2svg(filename::String; width = 2000, height = 800)

    Export the execution profile to an SVG hijacking the ProfileSVG
    infrastructure.
"""
function export2svg(filename::String; width = 2000, height = 800, fontsize = 12)
    fg = ProfileSVG.FGConfig()
    thev = map(LoggingProfiler.tape2structure, LoggingProfiler.to)
    start = minimum(first(e)[:start] for e in thev)
    stop = maximum(last(e)[:stop] for e in thev)
    δ =  width / (stop - start)
    colormap = Dict()
    cols = distinguishable_colors(100)
    depths = [_depth(ev) for ev in thev]
    ystep = fld(height, sum(d + 1 for d in depths) - 1)
    fig_id = "profilerview"
    open(filename, "w") do io
        write_svgheader(io, fig_id, width, height,
                bgcolor(fg), fontcolor(fg), fg.frameopacity,
                fg.font, fg.fontsize, fg.notext, δ, fg.timeunit, fg.delay)

        yoffset = 0
        for (ev, d) in zip(thev, depths)
            drawevent_svg(io, ev, yoffset, start, δ, colormap, ystep, cols)
            yoffset += (d + 1) * ystep
        end
        write_svgfooter(io, fig_id)
    end
end


# This file is part of the IntervalArithmetic.jl package; MIT licensed

module Complementary

using ..IntervalArithmetic

import ..IntervalArithmetic:
    contains_zero, zero_in, nonnegatives, unit_ball, extended_div,
    isunbounded, isbounded, mid

export
    contains_zero, zero_in, nonnegatives, unit_ball, extended_div,
    isunbounded, isbounded, mid

import Base: setdiff

export setdiff, distance, bisect

include("sets.jl")
include("rand.jl")

end # module


module ProxSDP

import Arpack
import KrylovKit
import MathOptInterface
import TimerOutputs
import TimerOutputs: @timeit

import Printf
import SparseArrays
import LinearAlgebra
import LinearAlgebra: BlasInt
import Random
import Logging

include("structs.jl")
include("options.jl")
include("util.jl")
include("printing.jl")
include("scaling.jl")
include("equilibration.jl")
include("pdhg.jl")
include("residuals.jl")
include("eigsolver.jl")
include("prox_operators.jl")

include("MOI_wrapper.jl")

end


#=
This file implements StatsModels constructors for TableCols.  That is,
it allows one to pass a StatsModels object directly to the TableCol
constructor (or, since the Table constructor falls back to TableCols on
a vararg, the Table constructor).
=#

########################################################################
#################### General Regression API Implementation #############
########################################################################

RegCol{M} = TableCol{3,M}

function RegCol(header::Printable)
    return TableCol(TableIndex(1, header), TableDict{3, FormattedNumber}())
end

"""
```
getnext(t::TableCol{3,M}, group::Int, level::Int) where M
```
In a TableCol `t` of row depth 3, computes the next index on the third level
given that the first dimension of the index is `group` and the second is
`level`.
"""
function getnext(t::RegCol, group::Int, level::Int)
    max_idx = 0
    for ridx in keys(t.data)
        ridx.idx[1] != group && continue
        ridx.idx[2] != level && continue
        max_idx = max(max_idx, ridx.idx[3])
    end
    return max_idx + 1
end

const block_labs = Dict(:setcoef! =>"Coefficient",
                        :setmeta! =>"Metadata",
                        :setstats! =>"Statistics")

for (block, fname) in zip([:1,:2,:3], [:setcoef!, :setmeta!, :setstats!])

    """
    ```
    $fname(t::$RegCol, key, val[, se]; level=1, name="")
    $fname(t::$RegCol, key=>val; level=1, name="")
    $fname(t::$RegCol, kv::AbstractDict)
    ```
    Inserts into `t` a key/value pair (possibly with a standard error) within
    the block.  Like the `TableCol` constructor, the pairs can be passed as
    either individual key/value[/se] tuples or pairs, as several vectors of
    key/value[/se] pairs, or as an associative.

    To add additional sub-blocks, use the `level` keyword argument.  Integers
    less than 0 will appears in blocks above the standard block, and integers
    greater than 1 will appear below it.

    To name the block or sub-block, pass a nonempty string as the `name` keyword
    argument.
    """
    @eval function ($fname)(t::RegCol, key::Printable, val; level=1, name="")
        next_idx = getnext(t, $block, level)
        index    = TableIndex(($block, level, next_idx), ("", name, key))
        t[index] = val
    end

    @eval function ($fname)(t::RegCol, key::Printable, val, se; level=1, name="")
        next_idx = getnext(t, $block, level)
        index    = TableIndex(($block, level, next_idx), ("", name, key))
        t[index] = val, se
    end

    @eval function ($fname)(t::RegCol, p::Pair; level=1, name="")
        next_idx = getnext(t, $block, level)
        key      = p.first
        val      = p.second
        index    = TableIndex(($block, level, next_idx), ("", name, key))
        t[index] = val
    end

    # Handle associatives
    @eval function ($fname)(t::RegCol, args...)
        for kv in zip(args)
            ($fname)(t, kv...)
        end
    end

    @eval function ($fname)(t::RegCol, ps::AbstractDict; level=1, name="")
        ($fname)(t, ps...)
    end
end

########################################################################
#################### Linear Model Interface ############################
########################################################################

function TableCol(header, m::RegressionModel;
                  stats=(:N=>Int∘nobs, "\$R^2\$"=>r2),
                  meta=(), stderror::Function=StatsBase.stderror, kwargs...)

    # Compute p-values
    pval(m) = ccdf.(FDist(1, dof_residual(m)),
                    abs2.(coef(m)./stderror(m)))

    # Initialize the column
    col  = RegCol(header)

    # Add the coefficients
    for (name, val, se, p) in zip(coefnames(m), coef(m), stderror(m), pval(m))
        setcoef!(col, name, val, se)
        0.05 <  p <= .1  && star!(col[name], 1)
        0.01 <  p <= .05 && star!(col[name], 2)
                p <= .01 && star!(col[name], 3)
    end

    # Add in the fit statistics
    setstats!(col, OrderedDict(p.first=>p.second(m) for p in tuplefy(stats)))

    # Add in the metadata
    setmeta!(col, OrderedDict(p.first=>p.second(m) for p in tuplefy(meta)))

    return col
end

########################################################################
#################### regtable Interface ###############################
########################################################################

TableAble = Union{RegressionModel, TexTable, Pair, Tuple}

function regtable(args::Vararg{TableAble}; num=1, kwargs...)
    cols = TexTable[]
    for arg in args
        new_tab = regtable(arg; num=num, kwargs...)
        n, m = size(new_tab)
        num += m
        push!(cols, new_tab)
    end
    return hcat(cols...)
end

function regtable(t::RegressionModel; num=1, kwargs...)
    return TableCol("($num)", t; kwargs...)
end

function regtable(t::TexTable; kwargs...)
    return t
end

function regtable(p::Pair; kwargs...)
    return join_table(p.first=>regtable(p.second; kwargs...))
end

function regtable(p::Tuple; kwargs...)
    return regtable(p...; kwargs...)
end


abstract AbstractLabeledArray{T,N,AXES,TN} <: AbstractArray{T,N}
abstract AbstractDictArray{K,N,VS,SV} <: AbstractArray{LDict{K,SV},N}


#__precompile__(false)
"""
Main module for `LikelihoodProfiler.jl`.

Four functions are exported from this module for public use:

- [`get_endpoint`](@ref). Computes lower or upper endpoints of confidence interval.
- [`get_interval`](@ref). Computes confidence interval.
- [`profile`](@ref). Generates the profile function based on `loss_func`
- [`update_profile_points!`](@ref). Updates confidence interval with likelihood profile points.

"""
module LikelihoodProfiler

using NLopt, ForwardDiff
using Calculus
using LinearAlgebra
using RecipesBase
import PlotUtils.adapted_grid
using ProgressMeter

# include
include("structures.jl")
include("get_endpoint.jl")
include("get_interval.jl")
include("cico_one_pass.jl")
include("method_lin_extrapol.jl")
include("method_quadr_extrapol.jl")
include("profile.jl")
include("plot_interval.jl")
include("show.jl")
include("get_optimal.jl")

# export
export get_right_endpoint,
    get_endpoint,
    scaling,
    unscaling,
    ProfilePoint,
    EndPoint,
    ParamIntervalInput,
    ParamInterval,
    get_interval,
    profile,
    update_profile_points!,
    get_optimal
end #module