"""
    _qrE!(A, E, Q, B; withQ = true)

Reduce the regular matrix pencil `A - λE` to an equivalent form `A1 - λE1 = Q1'*(A - λE)` using an
orthogonal or unitary transformation matrix `Q1` such that the transformed matrix `E1` is upper triangular.
The reduction is performed using the QR-decomposition of E.

The performed left orthogonal or unitary transformations Q1 are accumulated in the matrix `Q <- Q*Q1`
if `withQ = true`. Otherwise, `Q` is unchanged.

`Q1'*B` is returned in `B` unless `B = missing`.
"""
function _qrE!(A::AbstractMatrix{T}, E::AbstractMatrix{T}, Q::Union{AbstractMatrix{T},Nothing},
               B::Union{AbstractVecOrMat{T},Missing} = missing; withQ::Bool = true) where T <: BlasFloat

   # fast return for dimensions 0 or 1
   size(A,1) <= 1 && return
   T <: Complex ? tran = 'C' : tran = 'T'
   # compute in-place the QR-decomposition E = Q1*E1
   _, τ = LinearAlgebra.LAPACK.geqrf!(E)
   # A <- Q1'*A
   LinearAlgebra.LAPACK.ormqr!('L',tran,E,τ,A)
   # Q <- Q*Q1
   withQ && LinearAlgebra.LAPACK.ormqr!('R','N',E,τ,Q)
   # B <- Q1'*B
   ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,E,τ,B)
   triu!(E)
end
"""
    _svdlikeAE!(A, E, Q, Z, B, C; svdA = true, fast = true, atol1 = 0, atol2 = 0, rtol, withQ = true, withZ = true) -> (rE, rA22)

Reduce the regular matrix pencil `A - λE` to an equivalent form `A1 - λE1 = Q1'*(A - λE)*Z1` using
orthogonal or unitary transformation matrices `Q1` and `Z1` such that the transformed matrices `A1` and `E1`
are, for `svdA = true`, in the following SVD-like coordinate form

                   | A11-λE11 |  A12  |  A13  |
                   |----------|-------|-------|
        A1 - λE1 = |    A21   |  A22  |   0   | ,
                   |----------|-------|-------|
                   |    A31   |   0   |   0   |

where the `rE x rE` matrix `E11` and `rA22 x rA22` matrix `A22` are nosingular, and `E11` and `A22` are upper triangular,
if `fast = true`, and diagonal, if `fast = false`.

If `svdA = false`, only `E` is reduced to SVD-like form and `A1 - λE1` has the form

                   | A11-λE11 |  A12  |
        A1 - λE1 = |----------|-------| ,
                   |    A21   |  A22  |

where the `rE x rE` matrix `E11` is nonsingular upper triangular, if `fast = true`,
and diagonal, if `fast = false`, and `A22` is unreduced and has rank `rA22`.

The keyword arguments `atol1`, `atol2`, and `rtol`, specify, respectively, the absolute tolerance for the
nonzero elements of `A`, the absolute tolerance for the nonzero elements of `E`,  and the relative tolerance
for the nonzero elements of `A` and `E`.

The reduction is performed using rank decisions based on rank revealing QR-decompositions with column pivoting
if `fast = true` or the more reliable SVD-decompositions if `fast = false`.

The performed left orthogonal or unitary transformations Q1 are accumulated in the matrix `Q <- Q*Q1`
if `withQ = true`. Otherwise, `Q` is unchanged.
The performed right orthogonal or unitary transformations Z1 are accumulated in the matrix `Z <- Z*Z1` if `withZ = true`.
Otherwise, `Z` is unchanged.

`Q1'*B` is returned in `B` unless `B = missing` and `C*Z1` is returned in `C` unless `C = missing` .

"""
function _svdlikeAE!(A::AbstractMatrix{T}, E::AbstractMatrix{T},
                     Q::Union{AbstractMatrix{T},Nothing}, Z::Union{AbstractMatrix{T},Nothing},
                     B::Union{AbstractVecOrMat{T},Missing} = missing, C::Union{AbstractMatrix{T},Missing} = missing;
                     svdA::Bool = true, fast::Bool = true, atol1::Real = zero(real(T)), atol2::Real = zero(real(T)),
                     rtol::Real = (size(A,1)*eps(real(float(one(T)))))*iszero(min(atol1,atol2)),
                     withQ::Bool = true, withZ::Bool = true) where T <: BlasFloat

   # fast returns for null dimensions
   n = size(A,1)
   if n == 0
      return 0, 0
   end
   T <: Complex ? tran = 'C' : tran = 'T'

   if fast
      # compute in-place the QR-decomposition E*P1 = Q1*[E1;0] with column pivoting
      _, τ, jpvt = LinearAlgebra.LAPACK.geqp3!(E)
      tol = max(atol2, rtol*abs(E[1,1]))
      rE = count(x -> x > tol, abs.(diag(E)))
      n2 = n-rE
      i1 = 1:rE
      # A <- Q1'*A
      LinearAlgebra.LAPACK.ormqr!('L',tran,E,τ,A)
      # A <- A*P1
      A[:,:] = A[:,jpvt]
      # Q <- Q*Q1
      withQ && LinearAlgebra.LAPACK.ormqr!('R','N',E,τ,Q)
      # Z <- Z*P1
      withZ && (Z[:,:] = Z[:,jpvt])
      # B <- Q1'*B
      ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,E,τ,B)
      # E = [E1;0]
      E[:,:] = [ triu(E[i1,:]); zeros(T,n2,n) ]
      # C <- C*P1
      ismissing(C) || (C[:,:] = C[:,jpvt])

      # compute in-place the complete orthogonal decomposition E*Z1 = [E11 0; 0 0] with E11 nonsingular and UT
      E1 = view(E,i1,:)
      _, tau = LinearAlgebra.LAPACK.tzrzf!(E1)
      # A <- A*Z1
      LinearAlgebra.LAPACK.ormrz!('R',tran,E1,tau,A)
      withZ && LinearAlgebra.LAPACK.ormrz!('R',tran,E1,tau,Z)
      # C <- C*Z1
      ismissing(C) || LinearAlgebra.LAPACK.ormrz!('R',tran,E1,tau,C);
      E1[:,:] = [ triu(E[i1,i1]) zeros(T,rE,n2)  ]
      n2 == 0 && (return rE, 0)
      i22 = rE+1:n
      tolA = max(atol1, rtol*opnorm(A,1))
      svdA || (return rE, rank(view(A,i22,i22), atol = tolA))
      # assume
      #    A = [A11 A12]
      #        [A21 A22]
      # compute in-place the QR-decomposition A22*P2 = Q2*[R2;0] with column pivoting
      A22 = view(A,i22,i22)
      _, τ, jpvt = LinearAlgebra.LAPACK.geqp3!(A22)
      rA22 = count(x -> x > tolA, abs.(diag(A22)))
      n3 = n2-rA22
      i2 = rE+1:rE+rA22
      i3 = rE+rA22+1:n
      # A21 <- Q2'*A21
      LinearAlgebra.LAPACK.ormqr!('L',tran,A22,τ,view(A,i22,i1))
      # A12 <- A12*P1
      A[i1,i22] = A[i1,i22[jpvt]]
      # Q <- Q*Q2
      withQ && LinearAlgebra.LAPACK.ormqr!('R','N',A22,τ,view(Q,:,i22))
      # Z <- Z*P2
      withZ && (Z[:,i22] = Z[:,i22[jpvt]])
      # B <- Q2'*B
      ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,A22,τ,view(B,i22,:))
      # A22 = [R2;0]
      A22[:,:] = [ triu(A[i2,i22]); zeros(T,n3,n2) ]
      # R <- R*P1
      ismissing(C) || (C[:,i22] = C[:,i22[jpvt]])

      # compute in-place the complete orthogonal decomposition R2*Z2 = [A2 0; 0 0] with A22 nonsingular and UT
      A2 = view(A,i2,i22)
      _, tau = LinearAlgebra.LAPACK.tzrzf!(A2)
      # A12 <- A12*Z2
      LinearAlgebra.LAPACK.ormrz!('R',tran,A2,tau,view(A,i1,i22))
      withZ && LinearAlgebra.LAPACK.ormrz!('R',tran,A2,tau,view(Z,:,i22))
      # C <- C*Z2
      ismissing(C) || LinearAlgebra.LAPACK.ormrz!('R',tran,A2,tau,view(C,:,i22));
      A2[:,:] = [ triu(A[i2,i2]) zeros(T,rA22,n3)  ]
   else
      # compute the complete orthogonal decomposition of E using the SVD-decomposition
      U, S, Vt = LinearAlgebra.LAPACK.gesdd!('A',E)
      tolE = max(atol2, rtol*S[1])
      rE = count(x -> x > tolE, S)
      n2 = n-rE
      # A <- A*V
      A[:,:] = A[:,:]*Vt'
      # A <- U'*A
      A[:,:] = U'*A[:,:]
      # Q <- Q*U
      withQ && (Q[:,:] = Q[:,:]*U)
      # Z <- Q*V
      withZ && (Z[:,:] = Z[:,:]*Vt')
      # B <- U'*B
      ismissing(B) || (B[:,:] = U'*B[:,:])
      # C <- C*V
      ismissing(C) || (C[:,:] = C[:,:]*Vt')
      E[:,:] = [ Diagonal(S[1:rE]) zeros(T,rE,n2) ; zeros(T,n2,n) ]
      n2 == 0 && (return rE, 0)
      i22 = rE+1:n
      tolA = max(atol1, rtol*opnorm(A,1))
      svdA || (return rE, rank(view(A,i22,i22), atol = tolA))
      # assume
      #    A = [A11 A12]
      #        [A21 A22]
      # compute the complete orthogonal decomposition of A22 using the SVD-decomposition
      A22 = view(A,i22,i22)
      U, S, Vt = LinearAlgebra.LAPACK.gesdd!('A',A22)
      rA22 = count(x -> x > tolA, S)
      n3 = n2-rA22
      i1 = 1:rE
      i2 = rE+1:rE+rA22
      i3 = rE+rA22+1:n
      # A12 <- A12*V
      A[i1,i22] = A[i1,i22]*Vt'
      # A21 <- U'*A21
      A[i22,i1] = U'*A[i22,i1]
      # Q <- Q*U
      withQ && (Q[:,i22] = Q[:,i22]*U)
      # Z <- Q*V
      withZ && (Z[:,i22] = Z[:,i22]*Vt')
      # B <- U'*B
      ismissing(B) || (B[i22,:] = U'*B[i22,:])
      # C <- C*V
      ismissing(C) || (C[:,i22] = C[:,i22]*Vt')
      A22[:,:] = [ Diagonal(S[1:rA22]) zeros(T,rA22,n3); zeros(T,n3,n2) ]
   end
   return rE, rA22
end
"""
    isregular(A, E, γ; atol::Real = 0,  rtol::Real = atol > 0 ? 0 : n*ϵ) -> Bool

Test whether the matrix pencil `A-λE` is regular at `λ = γ` (i.e., `A-λE` is square and ``{\\small\\det(A-λE) \\neq 0}``).
The underlying computational procedure checks the maximal rank of `A-γE` if `γ` is finite and of `E` if
`γ` is infinite .

The keyword arguements `atol` and `rtol` specify the absolute and relative tolerances for the nonzero
elements of `A-γE`, respectively.
The default relative tolerance is `n*ϵ`, where `n` is the size of  `A`, and `ϵ` is the
machine epsilon of the element type of `A`.
"""
function isregular(A::AbstractMatrix, E::Union{AbstractMatrix,Nothing}, γ::Number; atol::Real = zero(real(eltype(A))),
                   rtol::Real = (min(size(A)...)*eps(real(float(one(eltype(A))))))*iszero(atol))

   m, n = size(A)
   m == n || (return false)
   E === nothing && (return rank(A, atol = atol, rtol = rtol) == n )
   (m,n) == size(E) || throw(DimensionMismatch("A and E must have the same dimensions"))
   if isinf(γ)
      return rank(E,atol = atol,rtol=rtol) == n
   else
      return rank(A-γ*E,atol = atol,rtol=rtol) == n
   end
end
"""
    isregular(A, E; atol1::Real = 0, atol2::Real = 0, rtol::Real=min(atol1,atol2)>0 ? 0 : n*ϵ) -> Bool

Test whether the matrix pencil `A-λE` is regular (i.e., `A-λE` is square and ``{\\small\\det(A-λE) \\not\\equiv 0}``).
The underlying computational procedure reduces the pencil `A-λE` to an appropriate Kronecker-like form,
which provides information on the rank of `A-λE`.

The keyword arguements `atol1`, `atol2` and `rtol` specify the absolute tolerance for the nonzero
elements of `A`, the absolute tolerance for the nonzero elements of `E`, and the relative tolerance
for the nonzero elements of `A` and `E`, respectively.
The default relative tolerance is `n*ϵ`, where `n` is the size of  `A`, and `ϵ` is the
machine epsilon of the element type of `A`.
"""
function isregular(A::AbstractMatrix, E::Union{AbstractMatrix,Nothing}; atol1::Real = zero(real(eltype(A))), atol2::Real = zero(real(eltype(A))),
                   rtol::Real = (min(size(A)...)*eps(real(float(one(eltype(A))))))*iszero(min(atol1,atol2)))
   mA, nA = size(A)
   mA == nA || (return false)
   E === nothing && (return rank(A, atol = atol1, rtol = rtol) == mA )
   (mA,nA) == size(E) || throw(DimensionMismatch("A and E must have the same dimensions"))
   T = promote_type(eltype(A), eltype(E))
   T <: BlasFloat || (T = promote_type(Float64,T))
   A1 = copy_oftype(A,T)
   E1 = copy_oftype(E,T)

   Q = nothing
   Z = nothing

   # Step 0: Reduce to the standard form
   n, m, p = _preduceBF!(A1, E1, Q, Z; atol = atol2, rtol = rtol, fast = false, withQ = false, withZ = false)

   mrinf = 0
   tol1 = max(atol1, rtol*opnorm(A1,1))
   while m > 0
      # Steps 1 & 2: Standard algorithm PREDUCE
      i1 = mrinf+1:mA
      τ, ρ = _preduce1!(n, m, m, view(A1,i1,i1), view(E1,i1,i1), Q, Z, tol1; fast = false, withQ = false, withZ = false)
      ρ+τ == m || (return false)
      mrinf += m
      n -= ρ
      m = ρ
   end
   return true
end
"""
    isunimodular(A, E; atol1::Real = 0, atol2::Real = 0, rtol::Real=min(atol1,atol2)>0 ? 0 : n*ϵ) -> Bool

Test whether the matrix pencil `A-λE` is unimodular (i.e., `A-λE` is square, regular and `det(A-λE) == constant`).
The underlying computational procedure reduces the pencil `A-λE` to an appropriate Kronecker-like form,
which provides information to check the full rank of `A-λE` and the lack of finite eigenvalues.

The keyword arguements `atol1`, `atol2` and `rtol` specify the absolute tolerance for the nonzero
elements of `A`, the absolute tolerance for the nonzero elements of `E`, and the relative tolerance
for the nonzero elements of `A` and `E`, respectively.
The default relative tolerance is `n*ϵ`, where `n` is the size of  `A`, and `ϵ` is the
machine epsilon of the element type of `A`.
"""
function isunimodular(A::AbstractMatrix, E::Union{AbstractMatrix,Nothing}; atol1::Real = zero(real(eltype(A))), atol2::Real = zero(real(eltype(A))),
                      rtol::Real = (size(A,1)*eps(real(float(one(eltype(A))))))*iszero(min(atol1,atol2)))

   mA, nA = size(A)
   mA == nA || (return false)
   E === nothing && (return rank(A, atol = atol1, rtol = rtol) == mA )
   (mA,nA) == size(E) || throw(DimensionMismatch("A and E must have the same dimensions"))
   mA == 0 && (return true)
   T = promote_type(eltype(A), eltype(E))
   T <: BlasFloat || (T = promote_type(Float64,T))
   A1 = copy_oftype(A,T)
   E1 = copy_oftype(E,T)

   Q = nothing
   Z = nothing

   # Step 0: Reduce to the standard form
   n, m, p = _preduceBF!(A1, E1, Q, Z; atol = atol2, rtol = rtol, fast = false, withQ = false, withZ = false)
   n == 0 && (return true)
   mrinf = 0
   tol1 = max(atol1, rtol*opnorm(A1,1))
   while m > 0
      # Steps 1 & 2: Standard algorithm PREDUCE
      i1 = mrinf+1:mA
      τ, ρ = _preduce1!(n, m, m, view(A1,i1,i1), view(E1,i1,i1), Q, Z, tol1; fast = false, withQ = false, withZ = false)
      ρ+τ == m || (return false)
      mrinf += m
      n -= ρ
      m = ρ
   end
   return n == 0
end
"""
    fisplit(A, E, B, C; fast = true, finite_infinite = false, atol1 = 0, atol2 = 0, rtol, withQ = true, withZ = true) -> (At, Et, Bt, Ct, Q, Z, ν, blkdims)

Reduce the regular matrix pencil `A - λE` to an equivalent form `At - λEt = Q'*(A - λE)*Z` using
orthogonal or unitary transformation matrices `Q` and `Z` such that the transformed matrices `At` and `Et` are in one of the
following block upper-triangular forms:

(1) if `finite_infinite = false`, then

                   | Ai-λEi |   *     |
        At - λEt = |--------|---------|,
                   |    O   | Af-λEf  |

where the `ni x ni` subpencil `Ai-λEi` contains the infinite elementary
divisors and the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular, contains the finite eigenvalues of the pencil `A-λE`.

The subpencil `Ai-λEi` is in a staircase form, with `Ai` nonsingular and upper triangular and `Ei` nilpotent and upper triangular.
The `nb`-dimensional vector `ν` contains the dimensions of the diagonal blocks
of the staircase form  `Ai-λEi` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[i]-ν[i+1]` for `i = 1, 2, ..., nb` is the number of infinite elementary divisors of degree `i`
(with `ν[nb+1] = 0`).

The dimensions of the diagonal blocks are returned in `blkdims = (ni, nf)`.

(2) if `finite_infinite = true`, then

                   | Af-λEf |   *    |
        At - λEt = |--------|--------|,
                   |   O    | Ai-λEi |

where the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular,
contains the finite eigenvalues of the pencil `A-λE` and the `ni x ni` subpencil `Ai-λEi`
contains the infinite elementary divisors.

The subpencil `Ai-λEi` is in a staircase form, with `Ai` nonsingular and upper triangular  and `Ei` nilpotent and upper triangular.
The `nb`-dimensional vectors `ν` contains the dimensions of the diagonal blocks
of the staircase form `Ai-λEi` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[nb-j+1]-ν[nb-j]` for `j = 1, 2, ..., nb` is the number of infinite elementary
divisors of degree `j` (with `ν[0] = 0`).

The dimensions of the diagonal blocks are returned in `blkdims = (nf, ni)`.

The keyword arguments `atol1`, `atol2`, and `rtol`, specify, respectively, the absolute tolerance for the
nonzero elements of `A`, the absolute tolerance for the nonzero elements of `E`,  and the relative tolerance
for the nonzero elements of `A` and `E`.

The reduction is performed using rank decisions based on rank revealing QR-decompositions with column pivoting
if `fast = true` or the more reliable SVD-decompositions if `fast = false`.

The performed left orthogonal or unitary transformations are accumulated in the matrix `Q` if `withQ = true`.
Otherwise, `Q` is set to `nothing`.
The performed right orthogonal or unitary transformations are accumulated in the matrix `Z` if `withZ = true`.
Otherwise, `Z` is set to `nothing`.

`Bt = Q'*B`, unless `B = missing`, in which case `Bt = missing` is returned, and `Ct = C*Z`,
unless `C = missing`, in which case `Ct = missing` is returned .

"""
function fisplit(A::AbstractMatrix, E::AbstractMatrix, B::Union{AbstractVecOrMat,Missing}, C::Union{AbstractMatrix,Missing};
   fast::Bool = true, finite_infinite::Bool = false,
   atol1::Real = zero(real(eltype(A))), atol2::Real = zero(real(eltype(E))),
   rtol::Real = (size(A,1)*eps(real(float(one(eltype(A))))))*iszero(min(atol1,atol2)),
   withQ::Bool = true, withZ::Bool = true)

   n = LinearAlgebra.checksquare(A)
   n == LinearAlgebra.checksquare(E) || throw(DimensionMismatch("A and E must have the same dimensions"))
   (!ismissing(B) && n != size(B,1)) && throw(DimensionMismatch("A and B must have the same number of rows"))
   (!ismissing(C) && n != size(C,2)) && throw(DimensionMismatch("A and C must have the same number of columns"))
   T = promote_type(eltype(A), eltype(E))
   ismissing(B) || (T = promote_type(T,eltype(B)))
   ismissing(C) || (T = promote_type(T,eltype(C)))
   T <: BlasFloat || (T = promote_type(Float64,T))

   A1 = copy_oftype(A,T)
   E1 = copy_oftype(E,T)
   ismissing(B) ? B1 = missing : B1 = copy_oftype(B,T)
   ismissing(C) ? C1 = missing : C1 = copy_oftype(C,T)

   withQ ? (Q = Matrix{T}(I,n,n)) : (Q = nothing)
   withZ ? (Z = Matrix{T}(I,n,n)) : (Z = nothing)

   ν, blkdims = fisplit!(A1, E1, Q, Z, B1, C1;
                        fast  = fast, finite_infinite = finite_infinite,
                        atol1 = atol1, atol2 = atol2, rtol = rtol, withQ = withQ, withZ = withZ)

   return A1, E1, B1, C1, Q, Z, ν, blkdims
end
"""
    fisplit!(A, E, Q, Z, B, C; fast = true, finite_infinite = false, atol1 = 0, atol2 = 0, rtol, withQ = true, withZ = true) -> (ν, blkdims)

Reduce the regular matrix pencil `A - λE` to an equivalent form `At - λEt = Q1'*(A - λE)*Z1` using
orthogonal or unitary transformation matrices `Q1` and `Z1` such that the transformed matrices `At` and `Et` are in one of the
following block upper-triangular forms:

(1) if `finite_infinite = false`, then

                   | Ai-λEi |   *     |
        At - λEt = |--------|---------|,
                   |    O   | Af-λEf  |

where the `ni x ni` subpencil `Ai-λEi` contains the infinite elementary
divisors and the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular, contains the finite eigenvalues of the pencil `A-λE`.

The subpencil `Ai-λEi` is in a staircase form, with `Ai` nonsingular and upper triangular and `Ei` nilpotent and upper triangular.
The `nb`-dimensional vector `ν` contains the dimensions of the diagonal blocks
of the staircase form  `Ai-λEi` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[i]-ν[i+1]` for `i = 1, 2, ..., nb` is the number of infinite elementary divisors of degree `i`
(with `ν[nb+1] = 0`).

The dimensions of the diagonal blocks are returned in `blkdims = (ni, nf)`.

(2) if `finite_infinite = true`, then

                   | Af-λEf |   *    |
        At - λEt = |--------|--------|,
                   |   O    | Ai-λEi |

where the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular,
contains the finite eigenvalues of the pencil `A-λE` and the `ni x ni` subpencil `Ai-λEi`
contains the infinite elementary divisors.

The subpencil `Ai-λEi` is in a staircase form, with `Ai` nonsingular and upper triangular and `Ei` nilpotent and upper triangular.
The `nb`-dimensional vectors `ν` contains the dimensions of the diagonal blocks
of the staircase form `Ai-λEi` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[nb-j+1]-ν[nb-j]` for `j = 1, 2, ..., nb` is the number of infinite elementary
divisors of degree `j` (with `ν[0] = 0`).

The dimensions of the diagonal blocks are returned in `blkdims = (nf, ni)`.

The reduced matrices `At` and `Et` are returned in `A` and `E`, respectively,
while `Q1'*B` is returned in `B`, unless `B = missing`, and `C*Z1`, is returned in `C`,
unless `C = missing`.

The keyword arguments `atol1`, `atol2`, and `rtol`, specify, respectively, the absolute tolerance for the
nonzero elements of `A`, the absolute tolerance for the nonzero elements of `E`,  and the relative tolerance
for the nonzero elements of `A` and `E`.

The reduction is performed using rank decisions based on rank revealing QR-decompositions with column pivoting
if `fast = true` or the more reliable SVD-decompositions if `fast = false`.

The performed left orthogonal or unitary transformations Q1 are accumulated in the matrix `Q <- Q*Q1`
if `withQ = true`. Otherwise, `Q` is unchanged.
The performed right orthogonal or unitary transformations Z1 are accumulated in the matrix `Z <- Z*Z1`
if `withZ = true`. Otherwise, `Z` is unchanged.

"""
function fisplit!(A::AbstractMatrix{T}, E::AbstractMatrix{T},
                  Q::Union{AbstractMatrix{T},Nothing}, Z::Union{AbstractMatrix{T},Nothing},
                  B::Union{AbstractVecOrMat{T},Missing} = missing, C::Union{AbstractMatrix{T},Missing} = missing;
                  fast::Bool = true, finite_infinite::Bool = false,
                  atol1::Real = zero(real(T)), atol2::Real = zero(real(T)),
                  rtol::Real = (size(A,1)*eps(real(float(one(T)))))*iszero(min(atol1,atol2)),
                  withQ::Bool = true, withZ::Bool = true) where T <: BlasFloat

   # fast returns for null dimensions
   n = size(A,1)
   ν = Vector{Int}(undef,n)
   if n == 0
      return ν, (0, 0)
   end
   T <: Complex ? tran = 'C' : tran = 'T'

   # Step 0: Reduce to the standard form
   nf, m1, p1 = _preduceBF!(A, E, Q, Z, B, C; atol = atol2, rtol = rtol, fast = fast)

   tolA = max(atol1, rtol*opnorm(A,1))

   if finite_infinite

      # Reduce A-λE to the Kronecker-like form by splitting the finite-infinite structures
      #
      #                  | Af - λ Ef |     *      |
      #      At - λ Et = |-----------|------------|,
      #                  |    0      | Ai -  λ Ei |
      #
      # where Ai - λ Ei is in a staircase form.

      i = 0
      ni = 0
      while p1 > 0
         # Step 1 & 2: Dual algorithm PREDUCE
         τ, ρ  = _preduce2!(nf, m1, p1, A, E, Q, Z, tolA, B, C; fast = fast,
                            rtrail = ni, ctrail = ni, withQ = withQ, withZ = withZ)
         ρ+τ == p1 || error("A-λE is not regular")
         ni += p1
         nf -= ρ
         i += 1
         ν[i] = p1
         p1 = ρ
         m1 -= τ
      end
      reverse!(view(ν,1:i))
      klf_left_refineinf!(view(ν,1:i), A, E, Q, B; roff = nf, coff = nf, withQ = withQ)
      return ν[1:i], (nf, ni)
   else

      # Reduce A-λE to the Kronecker-like form by splitting the infinite-finite structures
      #
      #                  | Ai - λ Ei |    *      |
      #      At - λ Et = |-----------|-----------|
      #                  |    0      | Af - λ Ef |
      #
      # where Ai - λ Ei is in a staircase form.

      i = 0
      ni = 0
      while m1 > 0
         # Steps 1 & 2: Standard algorithm PREDUCE
         τ, ρ = _preduce1!(nf, m1, p1, A, E, Q, Z, tolA, B, C; fast = fast,
                           roff = ni, coff = ni, withQ = withQ, withZ = withZ)
         ρ+τ == m1 || error("A-λE is not regular")
         ni += m1
         nf -= ρ
         i += 1
         ν[i] = m1
         m1 = ρ
         p1 -= τ
      end

      klf_right_refineinf!(view(ν,1:i), A, E, Z, C; withZ = withZ)
      # the following code can be used to make the supradiagonal blocks of E upper triangular
      # but it is not necessary in this context
      # k2 = ni
      # for k = i:-1:1
      #     nk = ν[k]
      #     k1 = k2-nk+1
      #     kk = k1:k2
      #     if nk > 1
      #        Ak = view(A,kk,kk)
      #        tau = similar(A,nk)
      #        LinearAlgebra.LAPACK.gerqf!(Ak,tau)
      #        LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(A,1:k1-1,kk))
      #        withZ && LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(Z,:,kk))
      #        LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(E,1:k1-1,kk))
      #        ismissing(C) || LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(C,:,kk))
      #        triu!(Ak)
      #     end
      #     if k > 1
      #        nk1 = ν[k-1]
      #        k1e = k1 - nk1
      #        k2e = k1 - 1
      #        kke = k1e:k2e
      #        if nk1 > 1
      #           Ek = view(E,kke,kk)
      #           tau = similar(A,nk)
      #           LinearAlgebra.LAPACK.geqrf!(Ek,tau)
      #           LinearAlgebra.LAPACK.ormqr!('L',tran,Ek,tau,view(A,kke,k1e:n))
      #           withQ && LinearAlgebra.LAPACK.ormqr!('R','N',Ek,tau,view(Q,:,kke))
      #           LinearAlgebra.LAPACK.ormqr!('L',tran,Ek,tau,view(E,kke,k2+1:n))
      #           ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,Ek,tau,view(B,kke,:))
      #           triu!(Ek)
      #        end
      #     end
      #     k2 = k1-1
      # end

      return ν[1:i], (ni, nf)
   end
end
"""
    sfisplit(A, E, B, C; fast = true, finite_infinite = false, atol1 = 0, atol2 = 0, rtol, withQ = true, withZ = true) -> (At, Et, Bt, Ct, Q, Z, ν, blkdims)

Reduce the regular matrix pencil `A - λE` to an equivalent form `At - λEt = Q'*(A - λE)*Z` using
orthogonal or unitary transformation matrices `Q` and `Z` such that the transformed matrices `At` and `Et` are in one of the
following block upper-triangular forms:

(1) if `finite_infinite = true`, then

                   | Ai1    *        *     |
        At - λEt = | O    Af-λEf     *     |
                   | O      0     Ai2-λEi2 |

where the `ni1 x ni1` upper triangular nonsingular matrix `Ai1` and the `ni2 x ni2` subpencil `Ai2-λEi2` contain the infinite elementary
divisors and the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular, contains the finite eigenvalues of the pencil `A-λE`.

The subpencil `Ai2-λEi2` is in a staircase form, with `Ai2` nonsingular and upper triangular and `Ei2` nilpotent and upper triangular.
The `nb`-dimensional vector `ν` contains the dimensions of the diagonal blocks
of the staircase form  `Ai2-λEi2` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[nb-j+2]-ν[nb-j+1]` for `j = 1, 2, ..., nb+1` is the number of infinite elementary
divisors of degree `j` (with `ν[0] := 0` and `ν[nb+1] := ni1`).

The dimensions of the diagonal blocks are returned in `blkdims = (ni1, nf, ni2)`.

(2) if `finite_infinite = false`, then

                   | Ai1-λEi1    *      *  |
        At - λEt = | O         Af-λEf   *  |
                   | O           0     Ai2 |

where the `ni1 x ni1` subpencil `Ai1-λEi1` and the upper triangular nonsingular matrix `Ai2` contain the infinite elementary
divisors and the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular, contains the finite eigenvalues of the pencil `A-λE`.

The subpencil `Ai1-λEi1` is in a staircase form, with `Ai1` nonsingular and upper triangular and `Ei1` nilpotent and upper triangular.
The `nb`-dimensional vectors `ν` contains the dimensions of the diagonal blocks
of the staircase form `Ai1-λEi1` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[i]-ν[i+1]` for `i = 0, 1, 2, ..., nb` is the number of infinite elementary divisors of degree `i`
(with `ν[nb+1] = 0` and `ν[0] = ni2`).

The dimensions of the diagonal blocks are returned in `blkdims = (ni1, nf, ni2)`.

The keyword arguments `atol1`, `atol2`, and `rtol`, specify, respectively, the absolute tolerance for the
nonzero elements of `A`, the absolute tolerance for the nonzero elements of `E`,  and the relative tolerance
for the nonzero elements of `A` and `E`.

The reduction is performed using rank decisions based on rank revealing QR-decompositions with column pivoting
if `fast = true` or the more reliable SVD-decompositions if `fast = false`.

The performed left orthogonal or unitary transformations are accumulated in the matrix `Q` if `withQ = true`.
Otherwise, `Q` is set to `nothing`.
The performed right orthogonal or unitary transformations are accumulated in the matrix `Z` if `withZ = true`.
Otherwise, `Z` is set to `nothing`.

`Bt = Q'*B`, unless `B = missing`, in which case `Bt = missing` is returned, and `Ct = C*Z`,
unless `C = missing`, in which case `Ct = missing` is returned .
"""
function sfisplit(A::AbstractMatrix, E::AbstractMatrix, B::Union{AbstractVecOrMat,Missing}, C::Union{AbstractMatrix,Missing};
   fast::Bool = true, finite_infinite::Bool = false,
   atol1::Real = zero(real(eltype(A))), atol2::Real = zero(real(eltype(E))),
   rtol::Real = (size(A,1)*eps(real(float(one(eltype(A))))))*iszero(min(atol1,atol2)),
   withQ::Bool = true, withZ::Bool = true)

   n = LinearAlgebra.checksquare(A)
   n == LinearAlgebra.checksquare(E) || throw(DimensionMismatch("A and E must have the same dimensions"))
   (!ismissing(B) && n != size(B,1)) && throw(DimensionMismatch("A and B must have the same number of rows"))
   (!ismissing(C) && n != size(C,2)) && throw(DimensionMismatch("A and C must have the same number of columns"))
   T = promote_type(eltype(A), eltype(E))
   ismissing(B) || (T = promote_type(T,eltype(B)))
   ismissing(C) || (T = promote_type(T,eltype(C)))
   T <: BlasFloat || (T = promote_type(Float64,T))

   A1 = copy_oftype(A,T)
   E1 = copy_oftype(E,T)
   ismissing(B) ? B1 = missing : B1 = copy_oftype(B,T)
   ismissing(C) ? C1 = missing : C1 = copy_oftype(C,T)

   withQ ? (Q = Matrix{T}(I,n,n)) : (Q = nothing)
   withZ ? (Z = Matrix{T}(I,n,n)) : (Z = nothing)

   ν, blkdims = sfisplit!(A1, E1, Q, Z, B1, C1;
                        fast  = fast, finite_infinite = finite_infinite,
                        atol1 = atol1, atol2 = atol2, rtol = rtol, withQ = withQ, withZ = withZ)

   return A1, E1, B1, C1, Q, Z, ν, blkdims
end
"""
    sfisplit!(A, E, Q, Z, B, C; fast = true, finite_infinite = false, atol1 = 0, atol2 = 0, rtol, withQ = true, withZ = true) -> (ν, blkdims)

Reduce the regular matrix pencil `A - λE` to an equivalent form `At - λEt = Q1'*(A - λE)*Z1` using
orthogonal or unitary transformation matrices `Q1` and `Z1` such that the transformed matrices `At` and `Et` are in one of the
following block upper-triangular forms:

(1) if `finite_infinite = true`, then

                   | Ai1    *        *     |
        At - λEt = | O    Af-λEf     *     |
                   | O      0     Ai2-λEi2 |

where the `ni1 x ni1` upper triangular nonsingular matrix `Ai1` and the `ni2 x ni2` subpencil `Ai2-λEi2` contain the infinite elementary
divisors and the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular, contains the finite eigenvalues of the pencil `A-λE`.

The subpencil `Ai2-λEi2` is in a staircase form, with `Ai2` nonsingular and upper triangular and `Ei2` nilpotent and upper triangular.
The `nb`-dimensional vector `ν` contains the dimensions of the diagonal blocks
of the staircase form  `Ai2-λEi2` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[nb-j+2]-ν[nb-j+1]` for `j = 1, 2, ..., nb+1` is the number of infinite elementary
divisors of degree `j` (with `ν[0] := 0` and `ν[nb+1] := ni1`).

The dimensions of the diagonal blocks are returned in `blkdims = (ni1, nf, ni2)`.

(2) if `finite_infinite = false`, then

                   | Ai1-λEi1    *      *  |
        At - λEt = | O         Af-λEf   *  |
                   | O           0     Ai2 |

where the `ni1 x ni1` subpencil `Ai1-λEi1` and the upper triangular nonsingular matrix `Ai2` contain the infinite elementary
divisors and the `nf x nf` subpencil `Af-λEf`, with `Ef` nonsingular and upper triangular, contains the finite eigenvalues of the pencil `A-λE`.

The subpencil `Ai1-λEi1` is in a staircase form, with `Ai1` nonsingular and upper triangular and `Ei1` nilpotent and upper triangular.
The `nb`-dimensional vectors `ν` contains the dimensions of the diagonal blocks
of the staircase form `Ai1-λEi1` such that `i`-th block has dimensions `ν[i] x ν[i]`.
The difference `ν[i]-ν[i+1]` for `i = 0, 1, 2, ..., nb` is the number of infinite elementary divisors of degree `i`
(with `ν[nb+1] = 0` and `ν[0] = ni2`).

The dimensions of the diagonal blocks are returned in `blkdims = (ni1, nf, ni2)`.

The reduced matrices `At` and `Et` are returned in `A` and `E`, respectively,
while `Q1'*B` is returned in `B`, unless `B = missing`, and `C*Z1`, is returned in `C`,
unless `C = missing`.

The keyword arguments `atol1`, `atol2`, and `rtol`, specify, respectively, the absolute tolerance for the
nonzero elements of `A`, the absolute tolerance for the nonzero elements of `E`,  and the relative tolerance
for the nonzero elements of `A` and `E`.

The reduction is performed using rank decisions based on rank revealing QR-decompositions with column pivoting
if `fast = true` or the more reliable SVD-decompositions if `fast = false`.

The performed left orthogonal or unitary transformations Q1 are accumulated in the matrix `Q <- Q*Q1`
if `withQ = true`. Otherwise, `Q` is unchanged.
The performed right orthogonal or unitary transformations Z1 are accumulated in the matrix `Z <- Z*Z1`
if `withZ = true`. Otherwise, `Z` is unchanged.
"""
function sfisplit!(A::AbstractMatrix{T}, E::AbstractMatrix{T},
                  Q::Union{AbstractMatrix{T},Nothing}, Z::Union{AbstractMatrix{T},Nothing},
                  B::Union{AbstractVecOrMat{T},Missing} = missing, C::Union{AbstractMatrix{T},Missing} = missing;
                  fast::Bool = true, finite_infinite::Bool = false,
                  atol1::Real = zero(real(T)), atol2::Real = zero(real(T)),
                  rtol::Real = (size(A,1)*eps(real(float(one(T)))))*iszero(min(atol1,atol2)),
                  withQ::Bool = true, withZ::Bool = true) where T <: BlasFloat

   # fast returns for null dimensions
   n = size(A,1)
   ν = Vector{Int}(undef,n)
   if n == 0
      return ν, (0, 0, 0)
   end
   T <: Complex ? tran = 'C' : tran = 'T'

   # Step 0: Reduce to the standard form
   nf, m1, p1 = _preduceBF!(A, E, Q, Z, B, C; atol = atol2, rtol = rtol, fast = fast, withQ = withQ, withZ = withZ)

   tolA = max(atol1, rtol*opnorm(A,1))
   Q === nothing && (withQ = false)
   Z === nothing && (withZ = false)

   if finite_infinite
      # Reduce A-λE to the form
      #
      #                  | Ai1  |    *      |
      #      At - λ Et = |------|-----------|
      #                  |  0   | A2 - λ E2 |
      #
      # where Ai1 is upper triangular and nonsingular and E2 upper triangular.
      τ, ρ = _preduce1!(nf, m1, p1, A, E, Q, Z, tolA, B, C; fast = fast,
                        roff = 0, coff = 0, withQ = withQ, withZ = withZ)
      ρ+τ == m1 || error("A-λE is not regular")
      nf -= ρ
      ni1 = m1
      m1 = ρ
      p1 -= τ
      if ni1 > 1
         kk = 1:ni1
         Ak = view(A,kk,kk)
         tau = similar(A,ni1)
         LinearAlgebra.LAPACK.gerqf!(Ak,tau)
         withZ && LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(Z,:,kk))
         ismissing(C) || LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(C,:,kk))
         triu!(Ak)
      end

      # Reduce A2-λE2 to the Kronecker-like form by splitting the finite-infinite structures
      #
      #                  | Af - λ Ef |     *       |
      #      A2 - λ E2 = |-----------|-------------|,
      #                  |    0      | Ai2 - λ Ei2 |
      #
      # where Ai2 - λ Ei2 is in a staircase form with Ai2 nonsingular and upper triangular and
      # Ei2 upper triangular and nilpotent.

      i = 0
      ni = 0
      while p1 > 0
         # Step 1 & 2: Dual algorithm PREDUCE
         τ, ρ  = _preduce2!(nf, m1, p1, A, E, Q, Z, tolA, B, C; fast = fast,
                            roff = ni1, coff = ni1, rtrail = ni, ctrail = ni, withQ = withQ, withZ = withZ)
         ρ+τ == p1 || error("A-λE is not regular")
         ni += p1
         nf -= ρ
         i += 1
         ν[i] = p1
         p1 = ρ
         m1 -= τ
      end
      k1 = ni1+nf+1
      reverse!(view(ν,1:i))
      for k = 1:i
          nk = ν[k]
          k2 = k1+nk-1
          kk = k1:k2
          if nk > 1
             Ak = view(A,kk,kk)
             tau = similar(A,nk)
             LinearAlgebra.LAPACK.geqrf!(Ak,tau)
             LinearAlgebra.LAPACK.ormqr!('L',tran,Ak,tau,view(A,kk,k2+1:n))
             withQ && LinearAlgebra.LAPACK.ormqr!('R','N',Ak,tau,view(Q,:,kk))
             LinearAlgebra.LAPACK.ormqr!('L',tran,Ak,tau,view(E,kk,k2+1:n))
             ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,Ak,tau,view(B,kk,:))
             triu!(Ak)
          end
          k1 = k2+1
      end
      return ν[1:i], (ni1, nf, ni)
   else
      # Reduce A-λE to the form
      #
      #                  | A1 - λ E1 |  *   |
      #      At - λ Et = |-----------|------|,
      #                  |    0      | Ai2  |
      #
      # where Ai2 is upper triangular and nonsingular and E1 is upper triangular.
      τ, ρ  = _preduce2!(nf, m1, p1, A, E, Q, Z, tolA, B, C; fast = fast,
                         rtrail = 0, ctrail = 0, withQ = withQ, withZ = withZ)
      ρ+τ == p1 || error("A-λE is not regular")
      ni2 = p1
      nf -= ρ
      p1 = ρ
      m1 -= τ
      if ni2 > 1
         kk = n-ni2+1:n
         Ak = view(A,kk,kk)
         tau = similar(A,ni2)
         LinearAlgebra.LAPACK.geqrf!(Ak,tau)
         #LinearAlgebra.LAPACK.ormqr!('L',tran,Ak,tau,view(A,kk,k2+1:n))
         withQ && LinearAlgebra.LAPACK.ormqr!('R','N',Ak,tau,view(Q,:,kk))
         #LinearAlgebra.LAPACK.ormqr!('L',tran,Ak,tau,view(E,kk,k2+1:n))
         ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,Ak,tau,view(B,kk,:))
         triu!(Ak)
      end

      # Reduce A1-λE1 to the Kronecker-like form by splitting the infinite-finite structures
      #
      #                  | Ai1 - λ Ei1 |    *      |
      #      A1 - λ E1 = |-------------|-----------|
      #                  |    0        | Af - λ Ef |
      #
      # where Ai1 - λ Ei1 is in a staircase form with Ai1 nonsingular and upper triangular and
      # Ei1 upper triangular and nilpotent.

      i = 0
      ni = 0
      while m1 > 0
         # Steps 1 & 2: Standard algorithm PREDUCE
         τ, ρ = _preduce1!(nf, m1, p1, A, E, Q, Z, tolA, B, C; fast = fast,
                           roff = ni, coff = ni, rtrail = ni2, ctrail = ni2, withQ = withQ, withZ = withZ)
         ρ+τ == m1 || error("A-λE is not regular")
         ni += m1
         nf -= ρ
         i += 1
         ν[i] = m1
         m1 = ρ
         p1 -= τ
      end

      k2 = ni
      for k = i:-1:1
          nk = ν[k]
          k1 = k2-nk+1
          kk = k1:k2
          if nk > 1
             Ak = view(A,kk,kk)
             tau = similar(A,nk)
             LinearAlgebra.LAPACK.gerqf!(Ak,tau)
             LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(A,1:k1-1,kk))
             withZ && LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(Z,:,kk))
             LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(E,1:k1-1,kk))
             ismissing(C) || LinearAlgebra.LAPACK.ormrq!('R',tran,Ak,tau,view(C,:,kk))
             triu!(Ak)
          end
         #  the following code can be used to make the supradiagonal blocks of E upper triangular
         #  but it is not necessary in this context
         #  if k > 1
         #     nk1 = ν[k-1]
         #     k1e = k1 - nk1
         #     k2e = k1 - 1
         #     kke = k1e:k2e
         #     if nk1 > 1
         #        Ek = view(E,kke,kk)
         #        tau = similar(A,nk)
         #        LinearAlgebra.LAPACK.geqrf!(Ek,tau)
         #        LinearAlgebra.LAPACK.ormqr!('L',tran,Ek,tau,view(A,kke,k1e:n))
         #        withQ && LinearAlgebra.LAPACK.ormqr!('R','N',Ek,tau,view(Q,:,kke))
         #        LinearAlgebra.LAPACK.ormqr!('L',tran,Ek,tau,view(E,kke,k2+1:n))
         #        ismissing(B) || LinearAlgebra.LAPACK.ormqr!('L',tran,Ek,tau,view(B,kke,:))
         #        triu!(Ek)
         #     end
         k2 = k1-1
      end

      return ν[1:i], (ni, nf, ni2)
   end
end


#=
Created on Thursday 23 July 2020
Last update: -

@author: Michiel Stock
michielfmstock@gmail.com

Illustration of Particle Swarm Optimization.
=#

# first we define a particle with a position `x` and a velocity `v`

using STMO.TestFuns

fun = TestFuns.ackley
x1lims = (-10, 10)
x2lims = (-10, 10)

mutable struct Particle{T}
    x::T
    v::T
    x_best::T
end

Particle(x) = Particle(x, zero(x), x)

init_population(n_particles, lims...) = [Particle([(u - l) * rand() + l for (l, u) in lims]) for i in 1:n_particles]

"""
    particle_swarm_optimization!(f, population, k_max;
            w=1, c1=1, c2=1, tracker=nothing)

Performs Particle Swarm Optimization to minimize a function `f`. Give an initial
vector of particles (type `Particle`) and the `k_max`, the number of iterations.

Optionally set hyperparameters `w`, `c1` and `c2` (default value of 1).
"""
function particle_swarm_optimization!(f, population::Vector{Particle{T}} where T, k_max;
        w=1, c1=1, c2=1, tracker=nothing)
    # find best point
    y_best, x_best = minimum((((f(part.x), part.x)) for part in population))
    for k in 1:k_max
        # update population
        for particle in population
            r1, r2 = rand(2)
            particle.v .= w * particle.v + c1 * r1 * (particle.x_best .- particle.x) .+
                c2 * r2 * (x_best .- particle.x)
            particle.x .+= particle.v
            fx = f(particle.x)
            # update current best
            fx < f(x_best) && (particle.x_best .= particle.x)
            # update global best
            if fx < y_best
                y_best = fx
                x_best .= particle.x
            end
        end
        tracker isa Nothing || tracker(population)
    end
    return y_best, x_best
end

population = init_population(50, x1lims, x2lims)
particle_swarm_optimization!(fun, population, 100, w=1)

function pso_animation(f, population, k_max;
        w=1, c1=1, c2=1)
    # find best point
    y_best, x_best = minimum((((f(part.x), part.x)) for part in population))
    objective = [y_best]
    # determine xvals and yvals depending on the spread of the particles
    x1min = minimum((part.x[1] for part in population))
    x1max = maximum((part.x[1] for part in population))
    x2min = minimum((part.x[2] for part in population))
    x2max = maximum((part.x[2] for part in population))
    x1steps = (x1max - x1min) / 200
    x2steps = (x2max - x2min) / 200
    anim = @animate for k in 1:k_max

        swarmplot = heatmap(x1min:x1steps:x1max, x2min:x2steps:x2max,
                        (x,y)->f((x,y)), color=:speed)
        xlims!((x1min, x1max))
        ylims!((x2min, x2max))
        # update population
        for particle in population
            r1, r2 = rand(2)
            particle.v .= w * particle.v + c1 * r1 * (particle.x_best .- particle.x) .+
                c2 * r2 * (x_best .- particle.x)
            particle.x .+= particle.v
            fx = f(particle.x)
            # update current best
            fx < f(x_best) && (particle.x_best .= particle.x)
            # update global best
            if fx < y_best
                y_best = fx
                x_best .= particle.x
            end
            scatter!(swarmplot, [particle.x[1]], [particle.x[2]],
                        color=:red, label="")
        end
        push!(objective, y_best)
        pobj = plot(0:k, objective, label="objective")
        xlabel!("iteration")
        ylabel!("best objective")
        plot(swarmplot, pobj)
    end
    return anim
end

population = init_population(50, x1lims, x2lims)
pso_no_momentum = pso_animation(fun, population, 100, w=1, c1=0.9, c2=0.9)
gif(pso_no_momentum, "figures/pso_no_momentum.gif", fps=4)

population = init_population(50, x1lims, x2lims)
pso_momentum = pso_animation(fun, population, 100, w=0.8, c1=0.9, c2=0.9)
gif(pso_momentum, "figures/pso_momentum.gif", fps=4)


# GMM estimation for a sample from Chi^2(theta)
# compare to two method of moments estimators (see chi2mm.m)
using Distributions, Plots
function main()
n = 30
theta = 3.0
reps = 1000
results = zeros(reps)
W = eye(2)
for i = 1:1000
    y = rand(Chisq(theta), n)
    obj = theta -> ([theta.-mean(y); theta.-0.5*var(y)]'W*[theta.-mean(y); theta.-0.5*var(y)])
    thetahat, junk, junk = fminunc(obj, [3.0])
    results[i] = sqrt(n)*(thetahat[1]-theta)
end
histogram(results,nbins=50)
gui()
return
end
main()


using LowRankModels
using Distributions

p = Params(0.00001,min_stepsize=0.00000000001,max_iter=5000)

# This is just to create a low-rank representation of some count data, I don't care how accurate it is.
m,n = 100, 50;
k = 2;
A = rand(Poisson(2), m, n); # Data is Poisson with mean 2
losses = convert(Array{Loss,1}, fill(PoissonLoss(10),n));
rx, ry = QuadReg(), QuadReg();
g_pre = GLRM(A, losses, rx, ry, k, scale=false, offset=false);

# let's check a different syntax works, too
g_pre = GLRM(A, PoissonLoss(), rx, ry, k, scale=false, offset=false);
X_real, Y_real, ch = fit!(g_pre, params=p);

# Now we do the actual model using the perfect data and try to recapture it.
A_real = impute(losses, X_real'*Y_real);

g = GLRM(A_real, losses, rx, ry, k, scale=true, offset=true);
X, Y, ch = fit!(g, params=p);
U = X'*Y;
A_imputed = impute(losses, X'*Y);

@show error_metric(g)
errors(Domain[l.domain for l in losses], losses, U, A_real);


module effective_interaction_rotated

# Optimized functions for calculation of Omega and Gamma

function Omega(a, θ)
    ξ = 2*pi*a
    cosθpow2 = cos(θ)^2
    cosξdivξ = cos(ξ)/ξ
    return 3.0/4*((1.0-3.0*cosθpow2) * (sin(ξ)+cosξdivξ)/ξ^2 - (1.0-cosθpow2)*cosξdivξ)
end

function Gamma(a, θ)
    ξ = 2*pi*a
    cosθpow2 = cos(θ)^2
    if ξ<1e-3
        return 3/2*(2/3 + 1/15*ξ^2 *(-2 + cosθpow2) + ξ^4*(1/140-cosθpow2/210))
    else
        sinξ = sin(ξ)/ξ
        return 3/2*((1-cosθpow2)*sinξ + (1-3*cosθpow2) * (cos(ξ)-sinξ)/ξ^2)
    end
end

function OmegaGamma(a, cosθpow2)
    #cosθpow2 = cos(θ)^2
    cosξ = cospi(2*a)
    sinξ = sinpi(2*a)
    omega = 0.75*((1.0-3.0*cosθpow2) * (sinξ+cosξ/(2*pi*a))/(2*pi*a)^2 - (1.0-cosθpow2)*cosξ/(2*pi*a))
    gamma = 1.5*((1-cosθpow2)*sinξ/(2*pi*a) + (1-3*cosθpow2) * (cosξ-sinξ/(2*pi*a))/(2*pi*a)^2)
    return omega, gamma
end

function OmegaGamma_orthogonal(a)
    cosξ = cospi(2*a)
    sinξ = sinpi(2*a)
    omega = 0.75*((sinξ+cosξ/(2*pi*a))/(2*pi*a)^2 - cosξ/(2*pi*a))
    gamma = 1.5*(sinξ/(2*pi*a) + (cosξ-sinξ/(2*pi*a))/(2*pi*a)^2)
    return omega, gamma
end

function Omega_orthogonal(a)
    ξ = 2*pi*a
    cosξdξ = cos(ξ)/ξ
    return 3.0/4.0*(-cosξdξ + (sin(ξ)+cosξdξ)/ξ^2)
end

function Gamma_orthogonal(a)
    ξ = 2*a
    sincξ = sinc(ξ)
    return 3.0/2.0*(sincξ + (cospi(ξ)-sincξ)/(pi*ξ)^2)
end

function map_effectiveinteraction(a::Vector{Float64}, f::Function)
    omega_list = Float64[]
    gamma_list = Float64[]
    for ai=a
        omega, gamma = f(ai, e_dipole)
        push!(omega_list, omega)
        push!(gamma_list, gamma)
    end
    return omega_list, gamma_list
end

# Finite symmetric systems

# function triangle_orthogonal(a::Float64)
#     omega_eff = 2*Omega_orthogonal(a)
#     gamma_eff = 2*Gamma_orthogonal(a)
#     return omega_eff, gamma_eff
# end

"""
    effective_interaction_rotated.square_orthogonal(a, Nδ)

Effective Omega and Gamma for a square.

The polarization axis is orthogonal to the square plane.

# Arguments
* `a`: Edge length.
* `Nδ`: Phase shift (Number of atoms in 2π).
"""
function square_orthogonal(a::Float64, Nδ::Int)
    @assert 0 <= Nδ < 4
    δ = 2.0*pi*Nδ/4
    Ωcos = 2*Omega_orthogonal(a)*cos(δ) + Omega_orthogonal(sqrt(2.)*a)*cos(2*δ)
    Γcos = 2*Gamma_orthogonal(a)*cos(δ) + Gamma_orthogonal(sqrt(2.)*a)*cos(2*δ)
    return Ωcos, Γcos
end

# function rectangle_orthogonal(a::Float64, b::Float64)
#     d = sqrt(a^2 + b^2)
#     omega_eff, gamma_eff = OmegaGamma_orthogonal(a)
#     om, ga = OmegaGamma_orthogonal(b)
#     omega_eff += om; gamma_eff+=ga
#     om, ga = OmegaGamma_orthogonal(d)
#     omega_eff += om; gamma_eff+=ga
#     return omega_eff, gamma_eff
# end

"""
    effective_interaction_rotated.cube_orthogonal(a, dϕ)

Effective Omega and Gamma for a cube.

The polarization axis is orthogonal to the xy faces.

# Arguments
* `a`: edge length.
* `dϕ`: Phase shift.
"""
function cube_orthogonal(a::Float64, dϕ)
    omega_eff, gamma_eff = square_orthogonal(a, 0)
    sqrt2 = sqrt(2.)
    sqrt3 = sqrt(3.)
    Θdiag = atan(sqrt2, 1.)
    omega_eff += (Omega(a, 0.) + 2*Omega(sqrt(2.)*a, pi/4.) + Omega(sqrt3*a, Θdiag))*cos(dϕ)
    gamma_eff += (Gamma(a, 0.) + 2*Gamma(sqrt(2.)*a, pi/4.) + Gamma(sqrt3*a, Θdiag))*cos(dϕ)
    return omega_eff, gamma_eff
end

# function box_orthogonal(a::Float64, b::Float64, c::Float64)
#     omega_eff, gamma_eff = rectangle_orthogonal(a, b)

#     a2 = a^2
#     b2 = b^2
#     c2 = c^2

#     om, ga = OmegaGamma(c, 1.)
#     omega_eff += om; gamma_eff+=ga

#     dxz_pow2 = a2+c2
#     om, ga = OmegaGamma(sqrt(dxz_pow2), c2/dxz_pow2)
#     omega_eff += om; gamma_eff+=ga

#     dyz_pow2 = b2+c2
#     om, ga = OmegaGamma(sqrt(dyz_pow2), c2/dyz_pow2)
#     omega_eff += om; gamma_eff+=ga

#     dxyz_pow2 = a2+b2+c2
#     om, ga = OmegaGamma(sqrt(dxyz_pow2), c2/dxyz_pow2)
#     omega_eff += om; gamma_eff+=ga
#     return omega_eff, gamma_eff
# end

# Infinite 1D symmetric systems

# function chain(a::Float64, Θ, N::Int)
#     omega_eff::Float64 = 0.
#     gamma_eff::Float64 = 0.
#     d = 0.
#     for j=1:N
#         d += a
#         omega_eff += Omega(d, Θ)
#         gamma_eff += Gamma(d, Θ)
#     end
#     return 2*omega_eff, 2*gamma_eff
# end

"""
    effective_interaction_rotated.chain_orthogonal(a, N, dϕ)

Effective Omega and Gamma for an infinite chain.

The polarization axis is orthogonal to the chain and the calculation is
done by adding N spins left and N spins right of a central spin.

# Arguments
* `a`: Spin-spin distance.
* `N`: Number of included spins.
* `dϕ`: Phase shift between neighboring spins.
"""
function chain_orthogonal(a::Float64, N::Int, dϕ::Float64)
    omega_eff::Float64 = 0.
    gamma_eff::Float64 = 0.
    d::Float64 = 0.
    ϕ::Float64 = 0.
    for j=1:N
        d += a
        ϕ += dϕ
        omega_eff += Omega_orthogonal(d)*cos(ϕ)
        gamma_eff += Gamma_orthogonal(d)*cos(ϕ)
    end
    return 2*omega_eff, 2*gamma_eff
end

# Infinite 2D symmetric systems

# function squarelattice_orthogonal(a::Float64, N::Int)
#     omega_eff::Float64 = 0.
#     gamma_eff::Float64 = 0.
#     for j=1:N
#         d = a*j
#         omega_eff += 4*Omega_orthogonal(d)
#         gamma_eff += 4*Gamma_orthogonal(d)
#         dx2 = j^2
#         for i=1:j-1
#             dy2 = i^2
#             d = a*sqrt(dx2+dy2)
#             omega_eff += 8*Omega_orthogonal(d)
#             gamma_eff += 8*Gamma_orthogonal(d)
#         end
#         d = a*sqrt(2)*j
#         omega_eff += 4*Omega_orthogonal(d)
#         gamma_eff += 4*Gamma_orthogonal(d)
#     end
#     return omega_eff, gamma_eff
# end

# function hexagonallattice_orthogonal(a::Float64, N::Int)
#     omega_eff::Float64 = 0.
#     gamma_eff::Float64 = 0.
#     for n=1:2:N
#         nv1x = -0.5*n
#         nv1y_square = 3./4*n^2
#         d = a*sqrt(nv1y_square + nv1x^2)
#         omega_eff += 6*Omega_orthogonal(d)
#         gamma_eff += 6*Gamma_orthogonal(d)
#         for i=1:div(n-1,2)
#             d = a*sqrt(nv1y_square + (nv1x+1.0*i)^2)
#             omega_eff += 12*Omega_orthogonal(d)
#             gamma_eff += 12*Gamma_orthogonal(d)
#         end
#     end
#     for n=2:2:N
#         nv1x = -0.5*n
#         nv1y_square = 3./4*n^2
#         d = a*sqrt(nv1y_square + nv1x^2)
#         omega_eff += 6*Omega_orthogonal(d)
#         gamma_eff += 6*Gamma_orthogonal(d)
#         for i=1:div(n-2,2)
#             d = a*sqrt(nv1y_square + (nv1x+1.0*i)^2)
#             omega_eff += 12*Omega_orthogonal(d)
#             gamma_eff += 12*Gamma_orthogonal(d)
#         end
#         i = div(n-2,2)+1
#         d = a*sqrt(nv1y_square + (nv1x+1.0*i)^2)
#         omega_eff += 6*Omega_orthogonal(d)
#         gamma_eff += 6*Gamma_orthogonal(d)
#     end
#     return omega_eff, gamma_eff
# end

# Infinite 3D symmetric systems

# function cubiclattice_orthogonal(a::Float64, N::Int)
#     omega_eff, gamma_eff = squarelattice_orthogonal(a, N)
#     for nx=1:N
#         for ny=0:nx
#             multiplicity = ((ny==0 || ny==nx) ? 8 : 16)
#             nrpow2 = nx^2 + ny^2
#             nr = sqrt(nrpow2)
#             for nz=1:N
#                 nzpow2 = nz^2
#                 d = a*sqrt(nrpow2+nzpow2)
#                 cosθpow2 = nzpow2/(nrpow2+nzpow2)
#                 om, ga = OmegaGamma(d, cosθpow2)
#                 omega_eff += multiplicity*om
#                 gamma_eff += multiplicity*ga
#             end
#         end
#     end
#     d::Float64 = 0.
#     for nz=1:N
#         d += a
#         om, ga = OmegaGamma(d, 1.)
#         omega_eff += 2*om
#         gamma_eff += 2*ga
#     end
#     return omega_eff, gamma_eff
# end

# function tetragonallattice_orthogonal(a::Float64, b::Float64, N::Int)
#     omega_eff, gamma_eff = squarelattice_orthogonal(a, N)
#     for nx=1:N
#         for ny=0:nx
#             multiplicity = ((ny==0 || ny==nx) ? 8 : 16)
#             nrpow2 = nx^2 + ny^2
#             nr = sqrt(nrpow2)
#             for nz=1:N
#                 nzpow2 = nz^2
#                 d = sqrt(a^2*nrpow2 + b^2*nzpow2)
#                 θ = atan2(a*nr, b*nz)
#                 omega_eff += multiplicity*Omega(d, θ)
#                 gamma_eff += multiplicity*Gamma(d, θ)
#             end
#         end
#     end
#     d::Float64 = 0.
#     for nz=1:N
#         d += b
#         omega_eff += 2*Omega(d, 0.)
#         gamma_eff += 2*Gamma(d, 0.)
#     end
#     return omega_eff, gamma_eff
# end

end # module


using Plots, Statistics, MLDatasets, LinearAlgebra, Clustering, Printf, Dates, Random
path1=realpath(dirname(@__FILE__)*"/..")
include(string(path1,"/src/ANNBN.jl"))

##### PARTIAL DIFFERENTIAL EQUATIONS
# Laplace's Equation http://www.robots.ox.ac.uk/~jmb/lectures/pdelecture5.pdf
a=1;b=1;n=50;dx=a/n;x0=zeros(n); x=Array{Float64}(undef,0); for i=1:n-1 global x0,x,dx; x=[x;x0.+(i)*dx] end; x
ny=50;dy=b/n;y=convert(Array{Float64},range(dy,b-dy,length=ny)); y=repeat(y,n-1)
xx_train=[x y];i_train=size(xx_train,1);vars=2;
# boundaries
nof_b=20;xb1=zeros(nof_b,2);xb1[:,2]=range(0,1,length=nof_b);xb2=zeros(nof_b,2);xb2[:,1]=range(0,1,length=nof_b);
xb3=ones(nof_b,2);xb3[:,2]=range(0,1,length=nof_b);xb4=ones(nof_b,2);xb4[:,1]=range(0,1,length=nof_b)
xb=[xb1;xb2;xb3;xb4];yb=zeros(size(xb,1));yb[end-nof_b+1:end].=sin.(pi.*range(0,1,length=nof_b))
# source
y_source=zeros(i_train).+0.0(rand(i_train)) # you may adjust the level of noise here

##### Clustering
neurons=50;inds_all,n_per_part=ANNBN.___clustering(neurons,xx_train,200)

##### Solve PDE
# Use # 1 Gaussian for calc_phi,calc_phi_deriv,calc_phi_deriv_deriv
cc1=1.0
a_all,a_layer1,layer1=ANNBN.train_layer_1_rbf_pde(neurons,vars,i_train,n_per_part,inds_all,xx_train,cc1,xb,yb,y_source);
predl1,layer1_train=ANNBN.predict_new_rbf(a_all,a_layer1,xx_train,i_train,neurons,n_per_part,inds_all,xx_train,vars,cc1)
scatter3d(xx_train[1:1:end,1],xx_train[1:1:end,2],predl1[1:1:end],label="ANNBN solution",
    color=:black,markershape=:xcross,legend=:topleft,markersize=1)
# Analytical solution
an_sol=sinh.(pi*xx_train[:,2]./a).*sin.(pi*xx_train[:,1]./a)./sinh(pi*b/a)
scatter3d!(xx_train[:,1],xx_train[:,2],an_sol,label="Analytical solution",
    color=:black,markershape=:star5,legend=:topleft,markersize=1)
# savefig("Laplace.pdf")
err=an_sol .- predl1;mean(abs.(err))
# scatter3d(xx_train[:,1],xx_train[:,2],err,color=:black,markershape=:xcross,legend=false,markersize=1)
# savefig("Laplace_noise_err.pdf")

# Calculate Derivatives
predl1_deriv_deriv1=ANNBN.predict_new_rbf_deriv_deriv(a_all,a_layer1,xx_train,i_train,neurons,
            n_per_part,inds_all,xx_train,vars,cc1,1)
predl1_deriv_deriv2=ANNBN.predict_new_rbf_deriv_deriv(a_all,a_layer1,xx_train,i_train,neurons,
            n_per_part,inds_all,xx_train,vars,cc1,2)
err_2nd_derivatives=predl1_deriv_deriv1+predl1_deriv_deriv2.-0.0
mean(abs.(err_2nd_derivatives))
scatter3d(xx_train[:,1],xx_train[:,2],predl1_deriv_deriv1,label="Derivative fxx",
    color=:black,markershape=:xcross,legend=:topleft,markersize=1,camera=(70, 40)
    ,xlabel="x",ylabel="y")
scatter3d!(xx_train[:,1],xx_train[:,2],predl1_deriv_deriv2,label="Derivative fyy",
    color=:black,markershape=:star5,legend=:topleft,markersize=1)
scatter3d!(xx_train[:,1],xx_train[:,2],predl1_deriv_deriv1+predl1_deriv_deriv2,label="fxx + fyy",
    color=:black,markershape=:circle,legend=:topleft,markersize=1)
# savefig("Laplace_fxx_fyy.pdf")


using LinearAlgebra, MATLAB, ForwardDiff, SparseArrays, Printf
using Convex, Hypatia
const FD = ForwardDiff
include("/Users/kevintracy/.julia/dev/CPEG/src/qp_solver.jl")

function dynamics(x,u)
    r = x[1:3]
    v = x[4:6]
    return [v;u[1:3] + [0;0;-9.81]]
end
function rk2(x,u)
    Δt = u[4]
    k1 = Δt*dynamics(x,u)
    k2 = Δt*dynamics(x + k1/2, u)
    return x + k2
end
function rollout(x0,U)
    N = length(U)+1
    X = [zeros(length(x0)) for i = 1:N]
    X[1] = x0
    for i = 1:(N-1)
        X[i+1] = rk2(X[i],U[i])
    end
    return X
end
function get_jacobians(X,U)
    N = length(X)
    A = [FD.jacobian(_x -> rk2(_x,U[i]),X[i]) for i = 1:N-1]
    B = [FD.jacobian(_u -> rk2(X[i],_u),U[i]) for i = 1:N-1]
    return A,B
end

function mpc(X,U)
    A,B = get_jacobians(X,U)
    nx = length(X[1])
    nu = length(U[1])
    N = length(X)
    alt_target = 10.0
    dt_target = 0.5

    δX = Variable(nx,N)
    δU = Variable(nu,N-1)

    J = 0

    γ = 1e3
    for i = 1:N-1
        J += γ*quadform(U[i][1:3] + δU[1:3,i],Matrix(I(3)))
        J += square(U[i][4] + δU[4,i] - dt_target)
    end

    p= minimize(J)

    for i = 1:N-1
        p.constraints += δU[4,i] >= 0.3
        p.constraints += δU[4,i] <= 0.7
    end
    p.constraints += X[N][3] + δX[3,N] == alt_target
    p.constraints += δX[:,1] == zeros(6)
    for i = 1:N-1
        # @info "here?"
        p.constraints += δX[:,i+1] == A[i]*δX[:,i] + B[i]*δU[:,i]
        # @info "nope"
    end

    solve!(p, Hypatia.Optimizer();silent_solver = true)

    δU = evaluate(δU)
    # @show norm(δU)
    U = [U[i] + vec(δU[:,i]) for i = 1:length(U)]

    return U, norm(δU)
end

function mat_from_vec(X)
    Xm = zeros(length(X[1]),length(X))
    for i = 1:length(X)
        Xm[:,i] = X[i]
    end
    return Xm
end
function tt()

    x0 = [0;0;100;0;0;-10.0]
    # u0 = [0;0;9.7]
    u1 = [0;0;9.81;0.5]

    N = 20

    U = [copy(u1) for i = 1:N-1]

    X = rollout(x0,U)

    xm = mat_from_vec(X)

    for i = 1:5

        U, ndu = mpc(X,U)

        if ndu < 1e-1
            break
        end
        X = rollout(x0,U)
        @show X[end][3]

    end

    mat"
    figure
    hold on
    %plot3($xm(1,:),$xm(2,:),$xm(3,:))
    plot($xm(1:3,:)')
    hold off
    "

    return nothing
end

tt()


module day15

# Part 1
function playgame(turns)
    numbers = [0,8,15,2,12,1,4]
    numturns = Dict(num => idx for (idx, num) in enumerate(numbers) if idx < lastindex(numbers))
    lastnum = numbers[end]
    for i in (lastindex(numbers)+1):turns
        if lastnum ∈ keys(numturns)
            num = (i - 1) - numturns[lastnum]
        else
            num = 0
        end
        numturns[lastnum] = i - 1
        lastnum = num
    end
    lastnum
end
println("Part 1: 2020th number = $(playgame(2020))")

# Part 2
println("Part 2: 30,000,000th number = $(playgame(30000000))")

end


using Revise
using Plots
using DynamicalSystems
using DifferentialEquations
using Basins

# http://dx.doi.org/10.1155/2013/256092
# Multiple Coexisting Attractors and Hysteresis in the Generalized Ueda Oscillator
@inline @inbounds function ueda(u, p, t)
    b = p[1]; F = p[2]; ω = p[3]; α = p[4]
	x=u[1]; y=u[2]; z=u[3];
    dx = y
    dy = -b*y-sign(x)*abs(x)^α+F*sin(z)
    dz = ω
    return SVector{3}(dx, dy, dz)
end

ω=1
α=4
p= [0.05, 7.5, ω, α]
ds = ContinuousDynamicalSystem(ueda, rand(3), p)
integ  = integrator(ds; alg=Tsit5(),  reltol=1e-8, save_everystep=false)

xres=100
yres=100

xg = range(-2.,2.,length=xres)
yg = range(-7.,7.,length=yres)

@time basin = Basins.basins_map2D(xg, yg, integ; T=2π/ω)

plot(xg,yg,basin', seriestype=:heatmap)


@testset "Cauchy Endgame" begin
    # Case: 1 regular solution
    @testset "Regular solution" begin
        @polyvar x y
        f = [x^2 - 4, x + 2y - 5]
        tracker, S = coretracker_startsolutions(f; seed = 1234, log_homotopy = true)
        track!(tracker, first(S), 0.0, 20.0)
        eg = HC.CauchyEndgame(tracker.state.x)
        p = copy(tracker.state.x)
        code, m = HC.predict!(p, tracker, eg)
        @test code == HC.CAUCHY_SUCCESS
        @test m == 1
        @test p ≈ [2, 1.5] atol = 1e-8
    end

    # Case 2: Univariate Singular
    @testset "Univariate singular" begin
        @polyvar x
        f = [(x - 3)^5]
        tracker, S = coretracker_startsolutions(f; seed = 1234, log_homotopy = true)
        track!(tracker, first(S), 0.0, 20.0)
        s = copy(tracker.state.x)
        eg = HC.CauchyEndgame(s)
        p = copy(tracker.state.x)
        code, m = HC.predict!(p, tracker, eg)
        @test code == HC.CAUCHY_SUCCESS
        @test m == 5
        @test p ≈ [3] atol = 1e-8
        # test that we can continute tracking from there
        track!(tracker, s, 20.0, 23.0)
        p .= 0
        code, m = HC.predict!(p, tracker, eg)
        @test code == HC.CAUCHY_SUCCESS
        @test m == 5
        @test p ≈ [3] atol = 1e-8

        # use in iterator
        f = [(x - 3)^5]
        tracker, S = coretracker_startsolutions(f; seed = 1234, log_homotopy = true)
        init!(tracker, first(S), 0.0, 25.0)
        # use as an iterator to do cauchy endgame in between,
        # make sure that we can still continue the tracking
        for _ in tracker
            if tracker.state.s > 14
                code, m = HC.predict!(p, tracker, eg)
                @test code == HC.CAUCHY_SUCCESS
                @test m == 5
            end
        end
        @test is_success(tracker.state.status)
        @test current_t(tracker) == 25
    end

    # Case 3
    @testset "Griewank-Osborne" begin
        # affine
        f = equations(griewank_osborne())
        tracker, starts = coretracker_startsolutions(f; seed = 1234, log_homotopy = true)
        S = collect(starts)
        track!(tracker, S[3], 0.0, 20.0)
        s = copy(tracker.state.x)
        eg = HC.CauchyEndgame(s)
        p = copy(tracker.state.x)
        code, m = HC.predict!(p, tracker, eg)
        @test p ≈ [0, 0] atol = 1e-8
        @test m == 3

        # projective
        f = equations(griewank_osborne())
        # random patch
        tracker, starts = coretracker_startsolutions(
            f;
            seed = 1234,
            projective_tracking = true,
            log_homotopy = true,
            patch = RandomPatch(),
        )
        S = collect(starts)
        track!(tracker, S[3], 0.0, 20.0)
        s = copy(tracker.state.x)
        eg = HC.CauchyEndgame(s)
        p = copy(tracker.state.x)
        code, m = HC.predict!(p, tracker, eg)
        @test ProjectiveVectors.affine_chart(p) ≈ [0, 0] atol = 1e-8
        @test m == 3

        # OrthogonalPatch
        tracker, starts = coretracker_startsolutions(
            f;
            seed = 1234,
            projective_tracking = true,
            log_homotopy = true,
            patch = OrthogonalPatch(),
        )
        S = collect(starts)
        track!(tracker, S[3], 0.0, 20.0)
        s = copy(tracker.state.x)
        eg = HC.CauchyEndgame(s)
        p = copy(tracker.state.x)
        code, m = HC.predict!(p, tracker, eg)
        @test ProjectiveVectors.affine_chart(p) ≈ [0, 0] atol = 1e-8
        @test m == 3
    end

    @testset "Limit accuracy error" begin
        # affine
        @polyvar x
        f = [(x - 3)^5]
        tracker, starts = coretracker_startsolutions(
            f;
            seed = 1234, min_step_size = eps()^2, log_homotopy = true,
        )
        S = collect(starts)
        tracker.options.precision = PRECISION_ADAPTIVE
        track(tracker, S[3], 0.0, 30.0)
        tracker.options.precision = PRECISION_FIXED_64
        s = copy(tracker.state.x)
        eg = HC.CauchyEndgame(s)
        p = copy(tracker.state.x)
        code, m = HC.predict!(p, tracker, eg)
        @test code == HC.CAUCHY_TERMINATED_ACCURACY_LIMIT
    end
end


function main()
    N, M = parse.(Int, split(readline()))
    ps = [parse(Int, readline()) for _ in 1:N]

    push!(ps, 0)
    pars = [p₁ + p₂ for p₁ in ps for p₂ in ps]
    sort!(pars)

    ans = 0
    for par in pars
        if par ≤ M
            idx_insert = searchsortedfirst(pars, M-par) - 1
            tmp = (idx_insert == 0) ? par : par + pars[idx_insert]
            ans = max(ans, tmp)
        end
    end

    println(ans)
end

main()


function fpi(f, x0, k)
    x = zeros(k+1)
    x[1] = x0
    for i = 1:k
        x[i+1] = f(x[i])
    end

    return x[k+1]
end


# __BEGIN_LICENSE__
#
# WaveOptics.jl
#
# Copyright (c) 2015, Stanford University
#
# All rights reserved.
#
# Redistribution and use in source and binary forms for academic and other
# non-commercial purposes with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code, including modified source code, must retain
#   the above copyright notice, this list of conditions and the following
#   disclaimer.
#
# * Redistributions in binary form or a modified form of the source code must
#   reproduce the above copyright notice, this list of conditions and the
#   following disclaimer in the documentation and/or other materials provided with
#   the distribution.
#
# * Neither the name of The Leland Stanford Junior University, any of its
#   trademarks, the names of its employees, nor contributors to the source code
#   may be used to endorse or promote products derived from this software without
#   specific prior written permission.
#
# * Where a modified version of the source code is redistributed publicly in
#   source or binary forms, the modified source code must be published in a freely
#   accessible manner, or otherwise redistributed at no charge to anyone
#   requesting a copy of the modified source code, subject to the same terms as
#   this agreement.
#
# THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF THE LELAND STANFORD JUNIOR
# UNIVERSITY "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LELAND STANFORD JUNIOR
# UNIVERSITY OR ITS TRUSTEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
#
# __END_LICENSE__

meshgrid(v::AbstractVector) = meshgrid(v, v)

"""
This function performs the same operation as MATLAB or numpy's meshgrid()
"""
function meshgrid(vx::AbstractVector{T}, vy::AbstractVector{T}) where T
    m, n = length(vy), length(vx)
    vx = reshape(vx, 1, n)
    vy = reshape(vy, m, 1)
    (repeat(vx, m, 1), repeat(vy, 1, n))
end

"""
This function performs the same operation as MATLAB or numpy's meshgrid()
"""
function meshgrid(vx::AbstractVector{T}, vy::AbstractVector{T}, vz::AbstractVector{T}) where T
    m, n, o = length(vy), length(vx), length(vz)
    vx = reshape(vx, 1, n, 1)
    vy = reshape(vy, m, 1, 1)
    vz = reshape(vz, 1, 1, o)
    om = ones(Int, m)
    on = ones(Int, n)
    oo = ones(Int, o)
    (vx[om, :, oo], vy[:, on, oo], vz[om, on, :])
end


import Base: +, -, *, /, ==, transpose, zero
import LinearAlgebra: dot, norm, normalize!
import SimilaritySearch: cosine_distance, angle_distance
export BOW, compute_bow, add!

# const BOW = Dict{Symbol,Int}
const BOW = Dict{Symbol,Float64}

"""
    compute_bow(config::TextConfig, text::String, voc::BOW)

Computes a bag of words and the maximum frequency of the bag
"""
function compute_bow(config::TextConfig, text::String, voc::BOW)
    maxfreq = 0.0
    for token in tokenize(config, text)
        sym = Symbol(token)
        m = get(voc, sym, 0.0) + 1.0
        voc[sym] = m
        maxfreq = max(m, maxfreq)
    end

    voc, maxfreq
end

"""
    compute_bow(tokenlist::AbstractVector{Symbol}, voc::BOW)::Tuple{BOW,Float64}

Updates a BOW using the given list of tokens
"""
function compute_bow(tokenlist::AbstractVector{Symbol}, voc::BOW)
    maxfreq = 0.0
    for sym in tokenlist
        m = get(voc, sym, 0.0) + 1.0
        voc[sym] = m
        maxfreq = max(m, maxfreq)
    end

    voc, maxfreq
end

# these are needed to call `compute_bow` for symbol's list but also for simplicity of the API
compute_bow(tokenlist::AbstractVector{Symbol}) = compute_bow(tokenlist, BOW())
compute_bow(config::TextConfig, tokenlist::AbstractVector{Symbol}, bow::BOW) = compute_bow(tokenlist, bow)
compute_bow(config::TextConfig, tokenlist::AbstractVector{Symbol}) = compute_bow(tokenlist, BOW())

"""
    compute_bow(config::TextConfig, text::String)

Compute a bag of words and the maximum frequency of the bag
"""
compute_bow(config::TextConfig, text::String) = compute_bow(config, text, BOW())

"""
    compute_bow(config::TextConfig, arr::AbstractVector{String})

Computes a bag of word and the maximum frequency of the bag; the input is an array of strings that represent a single object
"""
function compute_bow(config::TextConfig, arr::AbstractVector{String})
    D = BOW()
    maxfreq = 0.0
    for text in arr
       _, maxfreq = compute_bow(config, text, D)
    end

    D, maxfreq
end

"""
    normalize!(bow::BOW)

Inplace normalization of `bow`
"""
function normalize!(bow::BOW)
    s = 1.0 / norm(bow)
    for (k, v) in bow
        bow[k] = v * s
    end

    bow
end

function normalize!(matrix::AbstractVector{BOW})
    for bow in matrix
        normalize!(bow)
    end
end

function dot(a::BOW, b::BOW)
    if length(b) < length(a)
        a, b = b, a  # a must be the smallest bow
    end

    s = 0.0
    for (k, v) in a
        w = get(b, k, 0.0)
        s += v * w
    end

    s
end

function norm(a::BOW)::Float64
    s = 0.0
    for w in values(a)
        s += w * w
    end

    sqrt(s)
end

function zero(::Type{BOW})
    BOW()
end

## inplace sum
function add!(a::BOW, b::BOW)
    for (k, w) in b
        if w != 0.0
            a[k] = get(a, k, 0.0) + w
        end
    end

    a
end

## sum
function +(a::BOW, b::BOW)
    if length(a) < length(b)
        a, b = b, a  # a must be the largest bow
    end

    c = copy(a)
    for (k, w) in b
        if w != 0.0
            c[k] = get(c, k, 0.0) + w
        end
    end

    c
end

## definitions for substraction
function -(a::BOW, b::BOW)
    c = copy(a)
    for (k, w) in b
        if w != 0.0
            c[k] = get(c, k, 0.0) - w
        end
    end

    c
end

## definitions for product

function *(a::BOW, b::BOW)
    if length(b) < length(a)
        a, b = b, a  # a must be the smallest bow
    end

    c = copy(a)
    for k in keys(a)
        w = get(b, k, 0.0)
        if w == 0.0
            delete!(c, k)
        else
            c[k] *= w
        end
    end

    c
end

function *(a::BOW, b::F) where F <: Real
    c = copy(a)
    for (k, v) in a
        c[k] = v * b
    end

    c
end

function *(b::F, a::BOW) where F <: Real
    a * b
end

function /(a::BOW, b::F) where F <: Real
    c = copy(a)
    for (k, v) in a
        c[k] = v / b
    end

    c
end

"""
cosine_distance

Computes the cosine_distance between two weighted bags

It supposes that bags are normalized (see `normalize!` function)

"""
function cosine_distance(a::BOW, b::BOW)::Float64
    return 1.0 - dot(a, b)
end

"""
angle_distance

Computes the angle  between two weighted bags

It supposes that all bags are normalized (see `normalize!` function)

"""
function angle_distance(a::BOW, b::BOW)
    d = dot(a, b)

    if d <= -1.0
        return π
    elseif d >= 1.0
        return 0.0
    elseif d == 0  # turn around for zero vectors, in particular for denominator=0
        return π_2
    else
        return acos(d)
    end
end

function cosine(a::BOW, b::BOW)::Float64
    return dot(a, b) # * a.invnorm * b.invnorm # it is already normalized
end


using BoundaryValueDiffEq
using DiffEqBase, OrdinaryDiffEq, DiffEqDevTools
using Test
using NLsolve

@time @testset "Shooting Method Tests" begin
include("shooting_tests.jl")
include("orbital.jl")
end

@time @testset "Collocation Method (MIRK) Tests" begin include("mirk_convergence_tests.jl") end


using .ExamplePKG
using Test

printDate(2019,12,11)

@testset "ExamplePKG.jl" begin

    @test myFun(3,2) == 3^2
    @test myFun(22,4) == 22^4

end


#####################
# auxiliary functions
#####################

"""
Calculate new value of pixel using mask.

#Arguments
- `copied::Array{Float64,2}`: copy of an color matrice.
- `mask::Array{Float64,2}`: mask used in transition.
- `x::Int64`: first coordinate of left-upper corner of copied.
- `y::Int64`: second coordinate of left-upper corner of copied.
"""
function layingMask(copied, mask, x, y)
    dimM = size(mask)
    return sum(copied[x:x + dimM[2] - 1, y:y + dimM[1] - 1] .* mask) / sum(mask)
end

"""
Assign to the pixel minimum value from surrounding ones, determined by mask.

#Arguments
- `copied::Array{Float64,2}`: copy of an color matrice.
- `mask::Array{Float64,2}`: mask used in transition.
- `x::Int64`: first coordinate of left-upper corner of copied.
- `y::Int64`: second coordinate of left-upper corner of copied.
"""
function minimumValue(copied, mask, x, y)
    dimM = size(mask)
    return minimum(copied[x:x + dimM[2] - 1, y:y + dimM[1] - 1])
end

"""
Assign to the pixel maximum value from the surrounding ones, determined by mask.

#Arguments
- `copied::Array{Float64,2}`: copy of an color matrice .
- `mask::Array{Float64,2}`: mask used in transition.
- `x::Int64`: first coordinate of left-upper corner of copied.
- `y::Int64`: second coordinate of left-upper corner of copied.
"""
function maximumValue(copied, mask, x, y)
    dimM = size(mask)
    return maximum(copied[x:x + dimM[2] - 1, y:y + dimM[1] - 1])
end

"""
Convert given picture with specified type of mask.
Copy every RGB matrix and reproduce extreme pixels.
Create new matrix with values calculated by layingMask function and return list of new R, G, B matrices.

#Arguments
- `picture::Array{Array{Float64,2},1}`: r, g, b matrices list.
- `typeofmask::Array{Float64,2}`: mask, determined by typeofmask function.
"""
function converting(picture, typeofmask)
    mask = typeofmask
    dimR = size(picture[1])
    dimM = size(mask)
    R = copy(picture[1])
    G = copy(picture[2])
    B = copy(picture[3])
    newColorList = Array([])

    @simd for matrix in [R, G, B]
        copied = matrix

        helpHorizontalTop = copied[1,:]    # first row of matrix
        for n in 1:((dimM[1] - 1) / 2)
            copied = [helpHorizontalTop';copied] # adding upper bufor ((dimM[1]-1)/2) times
        end

        helpHorizontalBottom = copied[end,:]    # last row of matrix
        for n in 1:((dimM[1] - 1) / 2)
            copied = [copied;helpHorizontalBottom'] # adding bottom bufor ((dimM[1]-1)/2) times
        end

        helpVerticalLeft = copied[:,1]  # first column of matrix
        for i in 1:((dimM[2] - 1) / 2)
            copied = [helpVerticalLeft  copied]  # adding left bufor ((dimM[1]-1)/2) times
        end

        helpVerticalRight = copied[:,end]  # last column of matrix
        for i in 1:((dimM[2] - 1) / 2)
            copied = [copied  helpVerticalRight]  # adding right bufor ((dimM[2]-1)/2) times
        end

        dimC = size(copied)
        newPixelValues = Array{Float64}(undef, dimR[1], dimR[2])  # reserving memory
        matrix = collect(layingMask(copied, mask, x, y) for x in 1:Int64(dimC[1] - dimM[1] + 1), y in 1:Int64(dimC[2] - dimM[2] + 1))

        push!(newColorList, matrix)
    end # loop for R G B matrices
    return newColorList
end # converting

######################
# Low-pass filters
######################

"""
Generate mask to blurr the picture.

#Arguments
- `dim::Int64`: length of side of a mask.
"""
function maskAverage(dim)
    mask = ones(Float64, dim, dim)
    return mask
end

"""
Generate mask to blurr the picture.

#Arguments
- `dim::Int64`: length of side of a mask.
"""
function circle(dim)
    mask = ones(Float64, dim, dim)
    mask[1,1] = mask[1,dim] = mask[dim,1] = mask[dim,dim] = 0
    return mask
end

"""
Generate mask to blurr the picture.

#Arguments
- `dim::Int64`: length of side of a mask.
- `param k::Float64`: value of central pixel.
"""
function LP3(dim, k)
    mask = ones(Float64, dim, dim)
    mask[Int64((dim - 1) / 2 + 1),Int64((dim - 1) / 2 + 1)] = k
    return mask
end

######################
# High-pass filters
######################

"""
Generate mask to sharpen a picture.

#Arguments
- `dim::Int64`: length of side of a mask.
- `param k:Float64`: value of central pixel.
"""
function meanRemoval(dim, k)
    mask = -1(ones(Float64, dim, dim))
    mask[Int64((dim - 1) / 2 + 1),Int64((dim - 1) / 2 + 1)] = k
    return mask
end

"""
Generate mask to sharpen a picture.

#Arguments
- `dim::Int64`: length of side of a mask.
- `param k:Float64`: value of central pixel.
"""
function HP1(dim, k)
    mask = -1(ones(Float64, dim, dim))
    mask[1,1] = mask[1,dim] = mask[dim,1] = mask[dim,dim] = 0
    mask[Int64((dim - 1) / 2 + 1),Int64((dim - 1) / 2 + 1)] = k
    return mask
end

"""
Generate mask to sharpen a picture.

#Arguments
- `dim::Int64`: length of side of a mask.
- `param k:Float64`: value of central pixel.
"""
function HP2(dim, k)
    mask = -2(ones(Float64, dim, dim))
    mask[1,1] = mask[1,dim] = mask[dim,1] = mask[dim,dim] = 1
    mask[Int64((dim - 1) / 2 + 1),Int64((dim - 1) / 2 + 1)] = k
    return mask
end

#####
# Edge detection
#####
"""
Generate mask to detect edges on the picture in given direction.

#Arguments
- `dim::Int64`: length of side of a mask.
- `direction::String`: abbreviation of the name of direction:
                     "h" horizontal edges,
                     "v" vertical edges,
                     "d" diagonal edges.
"""
function detectingEdges(dim, direction)
    mask = (zeros(Float64, dim, dim))
    mask[Int64((dim - 1) / 2 + 1),Int64((dim - 1) / 2 + 1)] = 1

    if direction == "h"
        mask[1,Int64((dim - 1) / 2 + 1)] = -1
    elseif direction == "v"
       mask[Int64((dim - 1) / 2 + 1),1] = -1
    elseif direction == "d"
       mask[1,1] = -1
    end

    return mask
end

"""
Convert a given picture with a mask. Copy every RGB matrix and reproduce extreme pixels.
Create new matrix filled with values calculated by minimumValue function
and return list of of new R G and B values.

#Arguments
- `picture::Array{Array{Float64,2},1}`: r, g, b matrices list.
- `dimension::Int64`: length of side of a mask.
"""
function mini(picture, dimension)
    dimR = size(picture[1])
    dimM = dimension
    mask = zeros(dimM, dimM)
    R = copy(picture[1])
    G = copy(picture[2])
    B = copy(picture[3])
    newColorList = Array([])

    @simd for matrix in [R, G, B]
        copied = matrix

        helpHorizontalTop = copied[1,:]    # first row of matrix
        for n in 1:((dimM - 1) / 2)
            copied = [helpHorizontalTop';copied] # adding upper bufor ((dimM[1]-1)/2) times
        end

        helpHorizontalBottom = copied[end,:]    # last row of matrix
        for n in 1:((dimM - 1) / 2)
            copied = [copied;helpHorizontalBottom'] # adding bottom bufor ((dimM[1]-1)/2) times
        end

        helpVerticalLeft = copied[:,1]  # first column of matrix
        for i in 1:((dimM - 1) / 2)
            copied = [helpVerticalLeft  copied]  # adding left bufor ((dimM[1]-1)/2) times
        end

        helpVerticalRight = copied[:,end]  # last column of matrix
        for i in 1:((dimM - 1) / 2)
            copied = [copied  helpVerticalRight]  # adding right bufor ((dimM[2]-1)/2) times
        end

        dimC = size(copied)
        newPixelValues = Array{Float64}(undef, dimR[1], dimR[2])  # reserving memory
        matrix = collect(minimumValue(copied, mask, x, y) for x in 1:Int64(dimC[1] - dimM + 1), y in 1:Int64(dimC[2] - dimM + 1))

        push!(newColorList, matrix)
    end # loop for R G B matrices
    return newColorList
end # mini

"""
Convert a given picture with mask. Copy every RGB matrix and reproduce extreme pixels.
Then create new matrix with values calculated by maximumValue function
and return list of new R G and B values.

#Arguments
- `picture::Array{Array{Float64,2},1}`: r, g, b matrices list.
- `dimension::Int64`: length of a side of the mask.
"""
function maxi(picture, dimension)
    dimR = size(picture[1])
    dimM = dimension
    mask = zeros(dimM, dimM)
    R = copy(picture[1])
    G = copy(picture[2])
    B = copy(picture[3])
    newColorList = Array([])

    @simd for matrix in [R, G, B]
        copied = matrix

        helpHorizontalTop = copied[1,:]    # first row of matrix
        for n in 1:((dimM - 1) / 2)
            copied = [helpHorizontalTop';copied] # adding upper bufor ((dimM[1]-1)/2) times
        end

        helpHorizontalBottom = copied[end,:]    # last row of matrix
        for n in 1:((dimM - 1) / 2)
            copied = [copied;helpHorizontalBottom'] # adding bottom bufor ((dimM[1]-1)/2) times
        end

        helpVerticalLeft = copied[:,1]  # first column of matrix
        for i in 1:((dimM - 1) / 2)
            copied = [helpVerticalLeft  copied]  # adding left bufor ((dimM[1]-1)/2) times
        end

        helpVerticalRight = copied[:,end]  # last column of matrix
        for i in 1:((dimM - 1) / 2)
            copied = [copied  helpVerticalRight] # adding right bufor ((dimM[2]-1)/2) times
        end

        dimC = size(copied)
        newPixelValues = Array{Float64}(undef, dimR[1], dimR[2])  # reserving memory
        matrix = collect(maximumValue(copied, mask, x, y) for x in 1:Int64(dimC[1] - dimM + 1), y in 1:Int64(dimC[2] - dimM + 1))

        push!(newColorList, matrix)
    end # loop for R G B matrices
    return newColorList
end # maxi

########
# WHATIF
########
"""
Convert blue matrice of a given picture with mask. Copy every of RGB matrices
and reproduce extreme pixels of B matrix.
Then create new B matrix with values calculated by layingMask function
and return list of R G and new B values.

#Arguments
- `picture::Array{Array{Float64,2},1}`: r, g, b matrices list.
- `typeofmask::Array{Float64,2}`: mask which is a result of typeofmask function.
"""
function whatIfJustBlue(picture, typeofmask)
    mask = typeofmask
    dimR = size(picture[1])
    dimM = size(mask)
    R = copy(picture[1])
    G = copy(picture[2])
    B = copy(picture[3])
    newColorList = [R,G]

    copied = B

    helpHorizontalTop = copied[1,:]
    for n in 1:((dimM[1] - 1) / 2)
        copied = [helpHorizontalTop';copied]
    end

    helpHorizontalBottom = copied[end,:]
    for n in 1:((dimM[1] - 1) / 2)
        copied = [copied;helpHorizontalBottom']
    end

    helpVerticalLeft = copied[:,1]
    for i in 1:((dimM[2] - 1) / 2)
        copied = [helpVerticalLeft  copied]
    end

    helpVerticalRight = copied[:,end]
    for i in 1:((dimM[2] - 1) / 2)
        copied = [copied  helpVerticalRight]
    end

    dimC = size(copied)
    newPixelValues = Array{Float64}(undef, dimR[1], dimR[2]) # reserving memory
    B = collect(layingMask(copied, mask, x, y) for x in 1:Int64(dimC[1] - dimM[1] + 1), y in 1:Int64(dimC[2] - dimM[2] + 1))
    push!(newColorList, B)

    return newColorList
end # whatIfJustBlue


### A Pluto.jl notebook ###
# v0.12.15

using Markdown
using InteractiveUtils

# ╔═╡ 2bf8e4c2-339b-11eb-1d49-7f1b210a650a
using DelimitedFiles

# ╔═╡ 3e0c149a-3350-11eb-0b17-7bea5d1a28d4
md"# 2020 Day 1"

# ╔═╡ 18080fe2-339b-11eb-34f0-f393638506fc
md"## Part 1"

# ╔═╡ 4e084a4e-339b-11eb-3ffc-41118215f226
md"Find two elements that sum to 2020 and multiply them"

# ╔═╡ 24e1613c-339b-11eb-2976-59884053d1ee
input = readdlm("../data/day_1.txt", ' ', Int, '\n');

# ╔═╡ 67af36ec-339b-11eb-2bea-b1b1d06816d1
begin
	test_txt = """1721
	979
	366
	299
	675
	1456"""
	test_input = [parse(Int, x) for x in split(test_txt,"\n")]
end

# ╔═╡ c6762a40-339c-11eb-2313-a1266d053a73
17221 in test_input

# ╔═╡ bf6397c0-339b-11eb-346e-758d99ec2a5e
function find_sum_pairs(input::Array{Int},total::Int)
	for first_number in input
		second_number = total - first_number
		if second_number in input
			return [first_number, second_number]
		end
	end
end

# ╔═╡ d25fa824-339c-11eb-3cc2-75f419dc4740
begin
	test_pair = find_sum_pairs(test_input,2020)
	product_of_test_pair = test_pair[1] * test_pair[2]
	test_pair, product_of_test_pair
end

# ╔═╡ 0f8198aa-339d-11eb-0ae3-efa5f999b4f3
begin
	pair = find_sum_pairs(input,2020)
	product_of_pair = pair[1]*pair[2]
	pair, product_of_pair
end

# ╔═╡ 5f797e22-339d-11eb-1ff3-dda90f946838
md"## Part 2"

# ╔═╡ e3177eae-339e-11eb-3f9f-1f7230976832
md"Find the product of the triplet that sums to 2020"

# ╔═╡ 6b4a06e0-339d-11eb-0168-73ec316f0508
function find_sum_triplet(input::Array{Int},total::Int)
	for first_number in input
		remainder = total - first_number
		pair = find_sum_pairs(input, remainder)
		if pair != nothing
			return [first_number, pair[1], pair[2]]
		end
	end
end

# ╔═╡ 2fbb1bc0-339e-11eb-0917-376ee88ab59e
begin
	test_triplet = find_sum_triplet(test_input,2020)
	product_of_test_triplet = prod(test_triplet)
	test_triplet, product_of_test_triplet
end

# ╔═╡ 493f1fbc-339e-11eb-1d12-d1946e3f2f63
begin
	triplet = find_sum_triplet(input,2020)
	product_of_triplet = prod(triplet)
	triplet, product_of_triplet
end

# ╔═╡ Cell order:
# ╟─3e0c149a-3350-11eb-0b17-7bea5d1a28d4
# ╠═2bf8e4c2-339b-11eb-1d49-7f1b210a650a
# ╟─18080fe2-339b-11eb-34f0-f393638506fc
# ╟─4e084a4e-339b-11eb-3ffc-41118215f226
# ╠═24e1613c-339b-11eb-2976-59884053d1ee
# ╠═67af36ec-339b-11eb-2bea-b1b1d06816d1
# ╠═c6762a40-339c-11eb-2313-a1266d053a73
# ╠═bf6397c0-339b-11eb-346e-758d99ec2a5e
# ╠═d25fa824-339c-11eb-3cc2-75f419dc4740
# ╠═0f8198aa-339d-11eb-0ae3-efa5f999b4f3
# ╟─5f797e22-339d-11eb-1ff3-dda90f946838
# ╟─e3177eae-339e-11eb-3f9f-1f7230976832
# ╠═6b4a06e0-339d-11eb-0168-73ec316f0508
# ╠═2fbb1bc0-339e-11eb-0917-376ee88ab59e
# ╠═493f1fbc-339e-11eb-1d12-d1946e3f2f63