text:
# """

#     VEM

# A module for posterior and hyper-parameter estimates using a variational expectiation-maximization method.

# """
# module VEM

# using Statistics, LinearAlgebra, Random, StatsBase, Distributions, Distributed

export postG!, emG, postB!,emB,mStep!,ELBO,emGLMM,emGLM, Result, Null_est, ResGLM

#E-step

#posterior

#g for a full model
function postG!(ghat::Vector{Float64},Vg::Vector{Float64},λ::Vector{Float64},
yt::Vector{Float64},Xt::Matrix{Float64},Xt₀::Matrix{Float64},S::Vector{Float64},
        β::Vector{Float64},ξ::Vector{Float64},τ2::Float64,A0::Matrix{Float64},B0::Matrix{Float64})

    λ[:]= Lambda.(ξ)

    #posterior
    Vg[:]= 1.0./(λ+1.0./(τ2*S))

    ghat[:]= Diagonal(Vg)*(yt-λ.*(getXy('N',Xt₀,β) + getXy('N',Xt,sum(A0.*B0,dims=2)[:,1])))

end

# for initial values
function postG!(ghat::Vector{Float64},Vg::Vector{Float64},λ::Vector{Float64},
yt::Vector{Float64},Xt₀::Matrix{Float64},S::Vector{Float64},
        β::Vector{Float64},ξ::Vector{Float64},τ2::Float64)

    λ[:]= Lambda.(ξ)

    #posterior
    Vg[:]= 1.0./(λ+1.0./(τ2*S))

    ghat[:]= Diagonal(Vg)*(yt-λ.*(getXy('N',Xt₀,β) ))

end

#EM for g
function emG(Vg::Vector{Float64},ghat::Vector{Float64},S::Vector{Float64})

    ghat2=zeros(axes(S)); τ2_new=zero(eltype(S));
    #e-step : update the second moment
    ghat2= Vg+ghat.^2
    #m-step for τ²
    τ2_new= mean(ghat2./S)

    return ghat2, τ2_new

end

#Xy = getXy(Xt,yt)

# priors : Π,σ0,
#b's
#update adding k<l, B1,A1, k>l, B0,A0
function postB!(A1::Matrix{Float64}, B1::Matrix{Float64}, Sig1::Matrix{Float64}, λ::Vector{Float64},ghat::Vector{Float64},
yt::Vector{Float64},Xt::Matrix{Float64},Xt₀::Matrix{Float64},β::Vector{Float64},σ0::Vector{Float64},A0::Matrix{Float64},B0::Matrix{Float64},Π::Vector{Float64},L::Int64)

    pidx=axes(B0,1)
    ϕ = zeros(pidx); Z=zeros(pidx);

    Z0= similar(Z, axes(yt));

    ϕ= getXy('T', Xt.^2,λ) # mle of precision
    AB0= A0.*B0;  # #old α_l*b_l

            Sig1[:,:] =  1.0./(1.0./σ0'.+ ϕ)

     for l= 1: L

            Z0= yt - λ.*(getXy('N',Xt₀,β) + getXy('N',Xt,sum(dropCol(AB0,l),dims=2)[:,1])+ghat)
            Z= getXy('T',Xt,Z0)

            B1[:,l] = Diagonal(Sig1[:,l])*Z #posterial b_l
             # compute α_l
            # A0[:,l] = Π.*exp.(Z./(1.0.+ϕ/σ0[l]))./sqrt.(ϕ.^(-1).+1)
            # A1[:,l] = log.(Π)+ Z./(1.0.+ϕ/σ0[l]) - 0.5*log.(σ0[l]*ϕ.^(-1).+1)
            A1[:,l] = log.(Π)+ 0.5*Z.^2 .*Sig1[:,l] + 0.5*log.(Sig1[:,l])

            A1[:,l] = exp.(A1[:,l].-maximum(A1[:,l]))
            A1[:,l] = A1[:,l]/sum(A1[:,l])
            AB0[:,l]= A1[:,l].*B1[:,l]
      end

end

function emB(A1::Matrix{Float64}, B1::Matrix{Float64}, Sig1::Matrix{Float64},L::Int64)

    σ0_new = zeros(L); AB2=zeros(axes(B1));
       # compute the second moment of b_l & update the hyper-parameter σ0
        for l= 1:L
        AB2[:,l]= A1[:,l].*(B1[:,l].^2+ Sig1[:,l])
        σ0_new[l]= sum(AB2[:,l])
        end

      return σ0_new, AB2

end

#for GLM
#b's
#update adding k<l, B1,A1, k>l, B0,A0
function postB!(A1::Matrix{Float64}, B1::Matrix{Float64}, Sig1::Matrix{Float64}, λ::Vector{Float64},
    y::Vector{Float64},X::Matrix{Float64},X₀::Matrix{Float64},β::Vector{Float64},σ0::Vector{Float64},A0::Matrix{Float64},B0::Matrix{Float64},Π::Vector{Float64},L::Int64)

        pidx=axes(B0,1)
        ϕ = zeros(pidx); Z=zeros(pidx);

        Z0= similar(Z, axes(y));

        ϕ= getXy('T', X.^2,λ) # mle of precision
        AB0= A0.*B0;  # #old α_l*b_l

        Sig1[:,:] =  1.0./(1.0./σ0'.+ ϕ) #posterior Σ_l

         for l= 1: L

                # Z0= y - λ.*(getXy('N',X₀,β) + getXy('N',X,sum(hcat(AB1[:,1:l-1],AB0[:,l+1:end]),dims=2)[:,1]))
                Z0= (y - λ.*(getXy('N',X₀,β) + getXy('N',X,sum(dropCol(AB0,l),dims=2)[:,1])))
                Z =  getXy('T',X,Z0)

                B1[:,l] = Diagonal(Sig1[:,l])*Z #posterior b_l
                  # compute α_1
                # A1[:,l] = exp.(log.(Π)-0.5*(Z.^2)./(1.0.+ϕ./σ0[l]) +0.5*log.(σ0[l]*ϕ.^(-1).+1))

                A1[:,l] = log.(Π)+ 0.5*Z.^2 .*Sig1[:,l] + 0.5*log.(Sig1[:,l])
                A1[:,l] = exp.(A1[:,l].-maximum(A1[:,l])) # eliminate max for numerical stability
                A1[:,l] = A1[:,l]/sum(A1[:,l]) # scale to 0< α_1<1
                AB0[:,l]= A1[:,l].*B1[:,l] #update α_1*b_1
          end

end

# M-step: H1
function mStep!(ξ_new::Vector{Float64},β_new::Vector{Float64},A1::Matrix{Float64},B1::Matrix{Float64},AB2::Matrix{Float64},
        ghat::Vector{Float64},ghat2::Vector{Float64},λ::Vector{Float64},
        yt::Vector{Float64},Xt::Matrix{Float64},Xt₀::Matrix{Float64},β::Vector{Float64},L::Int64)

    # ξ_new= zeros(axes(yt))
    # β_new= zeros(axes(Xt₀,2))
      U=zeros(L,L)
      ŷ₀ = getXy('N',Xt₀,β)
      AB= getXX('N',Xt,'N',A1.*B1)
      B2= sum(AB,dims=2)[:,1]

      ξ_new[:] = getXy('N',Xt.^2.0,(sum(AB2,dims=2))[:,1]) # E(Xb)^2

      for j in eachindex(yt)
         U= AB[j,:]*AB[j,:]'
         ξ_new[j]  = ξ_new[j] +sum(U)-tr(U)
      end

    temp= ξ_new + ŷ₀.^2 + ghat2 + 2(ŷ₀ +ghat).*B2+ 2(ŷ₀.*ghat);
    tidx=findall(temp.<0.0)
    if (!isempty(tidx))
        # writedlm("./test/err-beta.txt",β)
        writedlm("./test/err-b.txt",[AB B2])
        writedlm("./test/domain-err-h1.txt",[repeat([myid()],length(tidx)) tidx temp[tidx] ξ_new[tidx] ghat2[tidx] ghat[tidx] ŷ₀[tidx] ((ŷ₀ +ghat).*B2)[tidx] ])
    #    temp[tidx].= 0.000001
       temp.= 0.00001
    end
    # ξ_new[:] = sqrt.(ξ_new + ŷ₀.^2 + ghat2 + 2(ŷ₀ +ghat).*B2+ 2(ŷ₀.*ghat))
      ξ_new[:] = sqrt.(temp)

    # β_new[:]= symXX('T',sqrt.(λ).*Xt₀)\getXy('T',Xt₀,(yt- λ.*(AB + ghat)))
     β_new[:]= getXX('T',Xt₀,'N',(λ.*Xt₀))\getXy('T',Xt₀,(yt- λ.*(B2 + ghat)))

end

#M-step: H0 for initial values
function mStep!(ξ_new::Vector{Float64},β_new::Vector{Float64},
        ghat::Vector{Float64},ghat2::Vector{Float64},λ::Vector{Float64},
        yt::Vector{Float64},Xt₀::Matrix{Float64},β::Vector{Float64})

    ŷ₀ = getXy('N',Xt₀,β)

    temp= ŷ₀.^2 + ghat2 + 2(ŷ₀.*ghat)
    tidx =findall(temp.<0.0)
    if (!isempty(tidx))
        writedlm("./test/err_beta_h0.txt",β)
        writedlm("./test/domain_error_h0.txt",[myid() tidx temp[tidx] ghat2[tidx] ŷ₀[tidx] ghat[tidx] ])
        # temp[tidx].= 0.000001
        temp.= 0.00001
    end

    # ξ_new[:] = sqrt.(ŷ₀.^2 + ghat2 + 2(ŷ₀.*ghat))  # check for debugging!
    ξ_new[:] = sqrt.(temp)
    # λ= 2Lambda.(ξ_new) #for check
    β_new[:]=  getXX('T',Xt₀,'N',(λ.*Xt₀))\getXy('T',Xt₀,(yt- λ.*ghat))

end

# M-step for GLM
function mStep!(ξ_new::Vector{Float64},β_new::Vector{Float64},A1::Matrix{Float64},B1::Matrix{Float64},AB2::Matrix{Float64},
    λ::Vector{Float64},y::Vector{Float64},X::Matrix{Float64},X₀::Matrix{Float64},β::Vector{Float64},L::Int64;nitr=0)

# ξ_new= zeros(axes(yt))
# β_new= zeros(axes(Xt₀,2))
   U = zeros(L,L)
   ŷ₀ = getXy('N',X₀,β)
#   AB= getXy('N',X,sum(A1.*B1,dims=2)[:,1])
    AB = getXX('N',X,'N',A1.*B1) # nxL
    B2= sum(AB,dims=2)[:,1]
    ξ_new[:] = getXy('N',X.^2.0,sum(AB2,dims=2)[:,1])

    for j in eachindex(y)
        U = AB[j,:]*AB[j,:]'
        ξ_new[j] = ξ_new[j]+ sum(U)-tr(U)
    end

   ξ_new[:] = sqrt.(ξ_new + ŷ₀.^2  + 2(ŷ₀.*B2))
#    a=findall(ξ_new.<0.0)
#     println("ξ_new at $(nitr) iteration")
#     println(a)
#     display(ξ_new[a])

 β_new[:]= getXX('T',X₀,'N',(λ.*X₀))\getXy('T',X₀,(y- λ.*B2))

end

# for a full model
function ELBO(L::Int64,ξ_new::Vector{Float64},β_new::Vector{Float64},σ0_new::Vector{Float64},τ2_new::Float64,
        A1::Matrix{Float64},B1::Matrix{Float64},AB2::Matrix{Float64},Sig1::Matrix{Float64},Π::Vector{Float64},ghat::Vector{Float64},
        ghat2::Vector{Float64},Vg::Vector{Float64},S::Vector{Float64},yt::Vector{Float64},Xt::Matrix{Float64},Xt₀::Matrix{Float64})

    n=length(yt); p = size(B1,1); lnb =zeros(L);

    AB1=getXy('N',Xt,sum(A1.*B1,dims=2)[:,1])
     elbo0= ELBO(ξ_new,β_new,τ2_new,ghat,ghat2,Vg,AB1,S,yt,Xt₀)#null part
     # susie part
    for l= 1: L
        if(sum(A1[:,l].==0.0)>0) #avoid NaN by log(0)
            lnb[l] = 0.0
        else
            lnb[l]  = A1[:,l]'*(log.(A1[:,l])-log.(Π) - 0.5*log.(Sig1[:,l]))
        end
    end

      bl= sum(lnb)+0.5*(sum(log.(σ0_new))- L)+ 0.5*sum(sum(AB2,dims=1)'./σ0_new)

    return elbo0-bl

end

# For initial values : H0 w/o susie
function ELBO(ξ_new::Vector{Float64},β_new::Vector{Float64},τ2_new::Float64,ghat::Vector{Float64},
        ghat2::Vector{Float64},Vg::Vector{Float64},S::Vector{Float64},yt::Vector{Float64},Xt₀::Matrix{Float64})

    n=length(yt);
    ll= sum(log.(logistic.(ξ_new))- 0.5*ξ_new)+ yt'*(getXy('N',Xt₀,β_new)+ghat) #lik
    gl = -0.5*(n*log(τ2_new)+ sum(log.(S)-log.(Vg))- 1.0 +sum(ghat2./S)/τ2_new)# g

    return ll+gl

end

# for H0 with susie
function ELBO(ξ_new::Vector{Float64},β_new::Vector{Float64},τ2_new::Float64,ghat::Vector{Float64},
    ghat2::Vector{Float64},Vg::Vector{Float64},AB1::Vector{Float64},S::Vector{Float64},yt::Vector{Float64},Xt₀::Matrix{Float64})

n=length(yt);
ll= sum(log.(logistic.(ξ_new))- 0.5*ξ_new)+ yt'*(getXy('N',Xt₀,β_new) + AB1 + ghat) #lik
gl = -0.5*(n*log(τ2_new)+ sum(log.(S)-log.(Vg))- 1.0 - sum(ghat2./S)/τ2_new) # g

return ll+gl

end

# for GLM
function ELBO(L::Int64,ξ_new::Vector{Float64},β_new::Vector{Float64},σ0_new::Vector{Float64},
    A1::Matrix{Float64},B1::Matrix{Float64},AB2::Matrix{Float64},Sig1::Matrix{Float64},Π::Vector{Float64},
    y::Vector{Float64},X::Matrix{Float64},X₀::Matrix{Float64})

# n=length(y); p = size(B1,1);
 lnb =zeros(L);

 ll= sum(log.(logistic.(ξ_new))- 0.5*ξ_new)+ y'*(getXy('N',X₀,β_new)+ getXy('N',X,sum(A1.*B1,dims=2)[:,1])) #lik
 # susie part
 for l= 1: L
    if(sum(A1[:,l].==0.0)>0) #avoid NaN by log(0)
        lnb[l] = 0.0
    else
        lnb[l]  = A1[:,l]'*(log.(A1[:,l])-log.(Π) - 0.5*log.(Sig1[:,l]))
    end
end

bl= sum(lnb)  +0.5*(sum(log.(σ0_new))- L)+ 0.5*sum(sum(AB2,dims=1)'./σ0_new)

return ll-bl

end

struct Result
    ξ::Vector{Float64}
    β::Vector{Float64}
    σ0::Vector{Float64}
    τ2::Float64
    elbo::Float64
    α::Matrix{Float64}
    ν::Matrix{Float64}
    # ν2::Matrix{Float64}
    Σ::Matrix{Float64}
end

# EM for a full model
function emGLMM(L::Int64,yt::Vector{Float64},Xt::Matrix{Float64},Xt₀::Matrix{Float64},S::Vector{Float64},τ2::Float64,
        β::Vector{Float64},ξ::Vector{Float64},σ0::Vector{Float64},Π::Vector{Float64};tol::Float64=1e-4)

    n, p = size(Xt)
    ghat =zeros(n); Vg = zeros(n); λ = zeros(n)
    A0 =repeat(Π,outer=(1,L)) ;
    B0=zeros(p,L); AB2=zeros(p,L)

    A1 =copy(A0); B1=copy(B0); Sig1=zeros(p,L)
    ghat2=zeros(axes(S)); τ2_new=zero(eltype(S));
    σ0_new = zeros(L); ξ_new = zeros(n); β_new=zeros(axes(β))

    crit =1.0; el0=0.0;numitr=1

    while (crit>=tol)
        ###check again!
         postG!(ghat,Vg,λ,yt,Xt,Xt₀,S,β,ξ,τ2,A0,B0)
         ghat2, τ2_new = emG(Vg,ghat,S)
         postB!(A1, B1, Sig1, λ,ghat,yt,Xt,Xt₀,β,σ0,A0,B0,Π,L)
         σ0_new, AB2 = emB(A1, B1, Sig1,L)

         mStep!(ξ_new,β_new,A1,B1,AB2,ghat,ghat2,λ,yt,Xt,Xt₀,β,L)

         el1=ELBO(L,ξ_new,β_new,σ0_new,τ2_new,A1,B1,AB2,Sig1,Π,ghat,ghat2,Vg,S,yt,Xt,Xt₀)
         if (isnan(el1))
           writedlm("./test/elbo_nan1.txt",[β_new σ0_new τ2_new])
           writedlm("./test/elbo_nan_xi.txt",ξ_new)

         end

         crit=abs(el1-el0)
         #check later for performance
        #crit=norm(ξ_new-ξ)+norm(β_new-β)+abs(τ2_new-τ2)+norm(B1-B0)+norm(σ0_new-σ0)

         ξ=ξ_new;β=β_new;σ0=σ0_new; τ2=τ2_new;el0=el1;A0[:,:]=A1;B0[:,:]=B1

          numitr +=1

    end
    println(numitr)
    return Result(ξ,β,σ0,τ2,el0,A1, B1, Sig1)

end

struct Null_est
    ξ::Vector{Float64}
    β::Vector{Float64}
    μ::Vector{Float64}
    τ2::Float64
    elbo::Float64
end

#EM for initial values (H0)
function emGLMM(yt,Xt₀,S,τ2,β,ξ;tol::Float64=1e-4)

    n = length(yt)
    ghat =zeros(n); Vg = zeros(n); λ = zeros(n)

    ghat2=zeros(axes(S)); τ2_new=zero(eltype(S));
    ξ_new = zeros(n); β_new=zeros(axes(β))

    crit =1.0; el0=0.0;numitr=1

    while (crit>=tol)
        ###check again!
         postG!(ghat,Vg,λ,yt,Xt₀,S,β,ξ,τ2)
         ghat2, τ2_new = emG(Vg,ghat,S)

         mStep!(ξ_new,β_new,ghat,ghat2,λ,yt,Xt₀,β)

         el1=ELBO(ξ_new,β_new,τ2_new,ghat,ghat2,Vg,S,yt,Xt₀)

         crit=abs(el1-el0)
        #  crit=norm(ξ_new-ξ)+norm(β_new-β)+abs(τ2_new-τ2)+abs(el1-el0)

         ξ=ξ_new;β=β_new; τ2=τ2_new;el0=el1

          numitr +=1
    end
    println(numitr)
    return Null_est(ξ,β,ghat,τ2,el0)

end

struct ResGLM
    ξ::Vector{Float64}
    β::Vector{Float64}
    σ0::Vector{Float64}
    elbo::Float64
    α::Matrix{Float64}
    ν::Matrix{Float64}
    # ν2::Matrix{Float64}
    Σ::Matrix{Float64}
end

# EM for GLM
function emGLM(L::Int64,y::Vector{Float64},X::Matrix{Float64},X₀::Matrix{Float64},
        β::Vector{Float64},ξ::Vector{Float64},σ0::Vector{Float64},Π::Vector{Float64};tol::Float64=1e-5)

    n, p = size(X)
    λ = zeros(n)
    A0 =repeat(Π,outer=(1,L)) ;
    B0=zeros(p,L); AB2=zeros(p,L)

    A1 =copy(A0); B1=copy(B0); Sig1=zeros(p,L)
    σ0_new = zeros(L); ξ_new = zeros(n); β_new=zeros(axes(β))

    crit =1.0; el0=0.0;numitr=1

    while (crit>=tol)
        ###check again!
        λ= Lambda.(ξ)
        # println("inter=$(numitr) and λ:")
        # println(λ)
        postB!(A1, B1, Sig1, λ,y,X,X₀,β,σ0,A0,B0,Π,L)
        # println("A1,B1,Sig1")
        # display(A1)
        # display(B1)
        # display(Sig1)
         σ0_new, AB2 = emB(A1, B1, Sig1,L)
        #  println("σ0,AB2")
        #  display(σ0_new)
        #  display(AB2)
         mStep!(ξ_new,β_new,A1,B1,AB2,λ,y,X,X₀,β,L;nitr=numitr)
        #  println("new ξ, β")
        #  display(ξ_new)
        #  display(β)

         el1=ELBO(L,ξ_new,β_new,σ0_new,A1,B1,AB2,Sig1,Π,y,X,X₀)
        #  println("elbo = $(el1)")

         crit=abs(el1-el0)
         #check later for performance
        #  crit=norm(ξ_new-ξ)+norm(β_new-β)+abs(el1-el0) +norm(B1-B0)

         ξ=ξ_new;β=β_new;σ0=σ0_new;el0=el1;A0[:,:]=A1;B0[:,:]=B1

          numitr +=1

    end

    return ResGLM(ξ,β,σ0,el0,A1, B1, Sig1)

end


text:
using TransportBasedInference: evaluate

@testset "Test active_dim for ProHermiteBasis" begin
    for B in [ProHermiteBasis(6); PhyHermiteBasis(6)]
        idx = reshape([0], (1, 1))
        @test active_dim(idx, B) == [1]

        idx = reshape([0; 1; 2; 3], (4, 1))
        @test active_dim(idx, B) == [1]

        idx = [0 0; 0 1; 1 0; 1 1; 1 2]
        @test active_dim(idx, B) == [1; 2]

        idx = [0 0 0]
        @test active_dim(idx, B) == [1; 2; 3]

        idx = [0 0 0 0; 0 2 0 1; 0 2 3 0; 0 2 2 1; 0 0 1 2; 0 2 0 2;0 2 2 2]
        @test active_dim(idx, B) == [1;2;3;4]

        idx = [5 0 0 0; 4 2 0 1; 3 2 3 0; 2 2 2 1; 2 0 1 2; 2 2 0 2;2 2 2 2]
        @test active_dim(idx, B) == [1; 2;3;4]
    end
end

@testset "Test active_dim for ProHermiteBasis" begin
    for B in [CstProHermiteBasis(6); CstPhyHermiteBasis(6); CstLinProHermiteBasis(6); CstLinPhyHermiteBasis(6)]
        idx = reshape([0], (1, 1))
        @test active_dim(idx, B) == [1]

        idx = reshape([0; 1; 2; 3], (4, 1))
        @test active_dim(idx, B) == [1]

        idx = [0 0; 0 1; 1 0; 1 1; 1 2]
        @test active_dim(idx, B) == [1; 2]

        idx = [0 0 0]
        @test active_dim(idx, B) == [3]

        idx = [0 0 0 0; 0 2 0 1; 0 2 3 0; 0 2 2 1; 0 0 1 2; 0 2 0 2;0 2 2 2]
        @test active_dim(idx, B) == [2;3;4]

        idx = [5 0 0 0; 4 2 0 1; 3 2 3 0; 2 2 2 1; 2 0 1 2; 2 2 0 2;2 2 2 2]
        @test active_dim(idx, B) == [1; 2;3;4]
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 1 I" begin

    Nx = 1
    Ne = 500
    X = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = reshape([0], (1, 1))
        truncidx = idx[1:2:end,:]

        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f,X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 1 II" begin

    Nx = 1
    Ne = 500
    X = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = reshape([0; 1; 2; 3], (4, 1))
        truncidx = idx[1:2:end,:]

        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f, X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 2 I" begin

    Nx = 2
    Ne = 500
    X = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = [0 0]
        truncidx = idx[1:2:end,:]

        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f, X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 2 II" begin

    Nx = 2
    Ne = 500
    X = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = [0 0; 0 1; 1 0; 1 1; 1 2]
        truncidx = idx[1:2:end,:]

        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f, X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 3 I" begin

    Nx = 3
    Ne = 500
    X = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = [0 0 0; 2 0 1; 0 1 0; 0 2 1; 0 1 2; 1 0 0; 2 2 2]
        truncidx = idx[1:2:end,:]
        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f, X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 3 II with only some active terms" begin

    Nx = 3
    Ne = 500
    X  = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = [0 0 0; 0 0 1; 0 1 0; 0 2 1; 0 1 2; 0  3 0; 0 2 2]
        truncidx = idx[1:2:end,:]
        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f, X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

@testset "Test evaluation, gradient and hessian of expanded function Nx = 3 III" begin

    Nx = 3
    Ne = 500
    X = randn(Nx, Ne)

    Blist = [ProHermiteBasis(8); PhyHermiteBasis(8); CstProHermiteBasis(8); CstPhyHermiteBasis(8); CstLinProHermiteBasis(8); CstLinPhyHermiteBasis(8)]
    for b in Blist
        B = MultiBasis(b, Nx)

        idx = [0 0 0]
        truncidx = idx[1:2:end,:]
        coeff =  randn(size(idx,1))

        f = ExpandedFunction(B, idx, coeff)

        ψt_basis, dψt_basis, d2ψt_basis = alleval(f, X)

        ψ_basis = evaluate_basis(f, X)
        dψ_basis = grad_x_basis(f, X)
        d2ψ_basis = hess_x_basis(f, X);
        @test norm(ψt_basis - ψ_basis)<1e-8
        @test norm(dψt_basis - dψ_basis)<1e-8
        @test norm(d2ψt_basis - d2ψ_basis)<1e-8

        # For truncated basis
        ψtrunc_basis = evaluate_basis(f, X, truncidx)
        dψtrunc_basis = grad_x_basis(f, X, truncidx)
        d2ψtrunc_basis = hess_x_basis(f, X, truncidx);

        @test norm(ψ_basis[:,1:2:end] - ψtrunc_basis)<1e-8
        @test norm(dψ_basis[:,1:2:end,:] - dψtrunc_basis)<1e-8
        @test norm(d2ψt_basis[:,1:2:end,:,:] - d2ψtrunc_basis)<1e-8

        # Verify function evaluation
        @test norm(map(i->f(X[:,i]),1:Ne) - evaluate(f, X))<1e-8

        #  Verify gradient
        dψ = grad_x(f, X)
        @test norm(hcat(map(i->ForwardDiff.gradient(f, X[:,i]), 1:Ne)...)' - dψ)<1e-8

        # Verify hessian
        d2ψ = hess_x(f, X)

        for i=1:Ne
            d2ψt = ForwardDiff.hessian(f, X[:,i])
            @test norm(d2ψt - d2ψ[i,:,:])<1e-8
        end

        # Verify grad_xd
        dψxd = grad_xd(f, X)

        for i=1:Ne
            dψxd_t = ForwardDiff.gradient(f, X[:,i])[end]
            @test abs(dψxd[i] - dψxd_t)<1e-10
        end

        # Verify hess_xd
        d2ψxd = hess_xd(f, X)

        for i=1:Ne
            d2ψxd_t = ForwardDiff.hessian(f, X[:,i])[end,end]
            @test abs(d2ψxd[i] - d2ψxd_t)<1e-10
        end

        # Verify grad_x_grad_xd
        dxdxkψ = grad_x_grad_xd(f, X)

        for i=1:Ne
            dxdxkψ_t = ForwardDiff.hessian(f, X[:,i])
            for j=1:Nx
                @test abs(dxdxkψ[i,j] - dxdxkψ_t[j,end])<1e-10
            end
        end

        # Verify hess_x_grad_xd
        dxidxjdxkψ = hess_x_grad_xd(f, X, f.idx)

        for i=1:Ne
            dxidxjdxkψ_t = ForwardDiff.hessian(xi ->ForwardDiff.gradient(f, xi)[Nx], X[:,i])
            @test norm(dxidxjdxkψ[i,:,:] - dxidxjdxkψ_t)<1e-10
        end

        # Verify grad_coeff
        dψcoeff  = grad_coeff(f, X)
        dψcoefftrunc  = grad_coeff(f, X, collect(1:2:size(idx,1)))
        @test norm(dψcoeff[:,1:2:end] - dψcoefftrunc)<1e-10

        @test norm(dψcoeff - ψ_basis)<1e-10
        @test norm(dψcoefftrunc - ψtrunc_basis)<1e-10

        # Verify hess_coeff
        d2ψcoeff  = hess_coeff(f, X)
        d2ψcoefftrunc  = hess_coeff(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeff)<1e-10

        # Verify grad_coeff_grad_xd
        dψcoeffxd = grad_coeff_grad_xd(f, X)
        dψcoeffxdtrunc = grad_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(dψcoeffxd[:,1:2:end] - dψcoeffxdtrunc)<1e-10
        @test norm(dψcoeffxd - grad_xk_basis(f, X, 1, Nx))<1e-10
        @test norm(dψcoeffxdtrunc - grad_xk_basis(f, X, 1, Nx, f.idx[collect(1:2:size(idx,1)),:]))<1e-10

        # Verify hess_coeff_grad_xd
        d2ψcoeffxd = hess_coeff_grad_xd(f, X)
        d2ψcoeffxdtrunc = hess_coeff_grad_xd(f, X, collect(1:2:size(idx,1)))

        @test norm(d2ψcoeffxd)<1e-10
        @test norm(d2ψcoeffxdtrunc)<1e-10
    end
end

ext: jl


text:
function main(::Robot)
    A=[]
    B=[]
    help_main_2(r,Ost)
    help_main_1(r, Nord)
    help_main_1(r, West)
    help_main_2(r, Sud)
end

function obhod(::Robot,side1::HorizonSide,side2::HorizonSide,A,B)
    while isborder(r,side1)
        move!(r,side2),push!(A,side2)
    end
    while length(B)>0
        side=pop!(B)
        move!(r,inverse(side)),push!(A,inverse(side))
    end
        putmarker!(r)
end
function go_back_position(::Robot,A)
    while length(A)>0
        side=pop!(A)
        move!(r,inverse(side))
    end
end

function help_main_1(r, side)
    A = []
    B = []
    if !isborder(r,side)
        move!(r,side)
        putmarker!(r)
        move!(r,inverse(side))
    else
        if isborder(r,po_chasovoy(side))
            side3=inverse(po_chasovoy(side))
        else
            side3=po_chasovoy(side)
        end
        while isborder(r,side) && !isborder(r,side3)
            move!(r,side3),push!(A,side3),push!(B,side3)
        end
        if !isborder(r,side3) && !isborder(r,side)
            move!(r,side),push!(A,side)
            obhod(r,inverse(side3),side,A,B)
            putmarker!(r)
            go_back_position(r,A)
        else
            go_back_position(r,A)
            B=[]
        end
    end
end

function help_main_2(r, side)
    A = []
    B = []
    if !isborder(r,side)
        move!(r,side)
        putmarker!(r)
        move!(r,inverse(side))
    else
        if isborder(r,protiv_chasovoy(side))
            side4=inverse(protiv_chasovoy(side))
        else
            side4=protiv_chasovoy(side)
        end
        while isborder(r,side) && !isborder(r,side4)
            move!(r,side4),push!(A,side4),push!(B,side4)
        end
        if !isborder(r,side4) && !isborder(r,side)
            move!(r,side),push!(A,side)
            obhod(r,inverse(side4),side,A,B)
            putmarker!(r)
            go_back_position(r,A)
        else
            go_back_position(r,A)
            B=[]
        end
    end
end

protiv_chasovoy(side) = HorizonSide(mod(Int(side)+1, 4))
po_chasovoy(side) = HorizonSide(mod(Int(side)- 1 , 4))
inverse(side::HorizonSide) = HorizonSide(mod(Int(side)+2,4))

max_stars_repo_name: VladislavBukin/mirea-progs

max_issues_repo_name: VladislavBukin/mirea-progs

max_forks_repo_name: VladislavBukin/mirea-progs


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

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

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

using HPAT
using MPI
using DocOpt

@acc hpat function kmeans(numCenter, iterNum, file_name)
    points = DataSource(Matrix{Float64},HDF5,"/points", file_name)
    D,N = size(points) # number of features, instances
    centroids = rand(D, numCenter)

    for l in 1:iterNum
        dist::Array{Array{Float64,1},1} = [ Float64[sqrt(sum((points[:,i]-centroids[:,j]).^2)) for j in 1:numCenter] for i in 1:N]
        labels::Array{Int,1} = [indmin(dist[i]) for i in 1:N]
        centroids::Array{Float64,2} = [ sum(points[j,labels.==i])/sum(labels.==i) for j in 1:D, i in 1:numCenter]
    end
    return centroids
end

function main()
    doc = """K-means clustering algorithm.

Usage:
  kmeans.jl -h | --help
  kmeans.jl [--iterations=<iterations>] [--file=<file>] [--centers=<centers>]

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

"""
    arguments = docopt(doc)

    if (arguments["--iterations"] != nothing)
        iterations = parse(Int, arguments["--iterations"])
    else
        iterations = 20
    end

    if (arguments["--file"] != nothing)
        file_name::ASCIIString = arguments["--file"]
    else
        file_name = HPAT.getDefaultDataPath()*"kmeans_large.hdf5"
    end

    if (arguments["--centers"] != nothing)
        numCenter = parse(Int, arguments["--centers"])
    else
        numCenter = 5
    end

    srand(0)
    rank = MPI.Comm_rank(MPI.COMM_WORLD)
    pes = MPI.Comm_size(MPI.COMM_WORLD)

    if rank==0 println("iterations = ", iterations) end
    if rank==0 println("file= ", file_name) end
    if rank==0 println("centers= ", numCenter) end

    tic()
    kmeans(numCenter, 2, file_name)
    time = toq()
    if rank==0 println("SELFPRIMED ", time) end
    MPI.Barrier(MPI.COMM_WORLD)

    tic()
    centroids_out = kmeans(numCenter, iterations, file_name)
    time = toq()
    if rank==0 println("result = ", centroids_out) end
    if rank==0 println("rate = ", iterations / time, " iterations/sec") end
    if rank==0 println("SELFTIMED ", time) end

end

main()


text:
function tokenize(str)
    tokens = String[]
    token = ""
    for c in str
        if c == ' '
            token != "" && push!(tokens, token)
            token = ""
        elseif c == '+' || c == '*'
            token != "" && push!(tokens, token)
            push!(tokens, string(c))
        elseif c == '(' || c == ')'
            token != "" && push!(tokens, token)
            push!(tokens, string(c))
            token = ""
        else
            token *= c
        end
    end
    token != "" && push!(tokens, token)

    tokens
end

function part1(tokens::AbstractVector{<:AbstractString})
    n = 0
    op = +
    i = 1
    while i ≤ length(tokens)
        if tokens[i] == "("
            j, m = part1((@view tokens[i+1:end]))
            i += j + 1
            n = op(n, m)
        elseif tokens[i] == ")"
            break
        elseif tokens[i] == "+"
            op = +
            i += 1
        elseif tokens[i] == "*"
            op = *
            i += 1
        else
            n = op(n, parse(Int, tokens[i]))
            i += 1
        end
    end
    i, n
end
part1(str::AbstractString) = last(part1(tokenize(str)))

abstract type AbstractExpression end

mutable struct Expression <: AbstractExpression
    op::Function
    left::AbstractExpression
    right::AbstractExpression
end

mutable struct Value <: AbstractExpression
    val::Int
end

function parseexpr(tokens::AbstractVector{<:AbstractString})
    i, op, exp = 1, +, Value(0)
    while i ≤ length(tokens)
        if tokens[i] == "("
            j, right = parseexpr(@view tokens[i+1:end])
            i += j + 1
            if exp isa Value
                exp = Expression(op, exp, right)
            elseif op === +
                exp.right = Expression(op, exp.right, right)
            else
                exp = Expression(op, exp, right)
            end
        elseif tokens[i] == ")"
            break
        elseif tokens[i] == "+"
            op = +
            i += 1
        elseif tokens[i] == "*"
            op = *
            i += 1
        else
            leaf = Value(parse(Int, tokens[i]))
            if exp isa Value
                exp = Expression(op, exp, leaf)
            elseif op === +
                exp.right = Expression(op, exp.right, leaf)
            else
                exp = Expression(op, exp, leaf)
            end
            i += 1
        end
    end
    i, exp
end
parseexpr(str::AbstractString) = last(parseexpr(tokenize(str)))

evaluate(expr::Expression) = expr.op(evaluate(expr.left), evaluate(expr.right))
evaluate(expr::Value) = expr.val

part2(str::AbstractString) = evaluate(parseexpr(str))

let lines = readlines("input.txt")
    @info "Solution" sum(map(part1, lines)) sum(map(part2, lines))
end

max_stars_repo_name: dglmoore/advent-of-code

max_issues_repo_name: dglmoore/advent-of-code

max_forks_repo_name: dglmoore/advent-of-code


text:
# This version is written for a shared memory implementation.
# The matrix A is local to the first Worker, which allocates work to other Workers
# All updates to A are carried out by the first Worker. Thus A is not distributed

using LinearAlgebra, LinearAlgebra.BLAS
using Printf

function hpl_shared(A::Matrix, b::Vector, blocksize::Integer, run_parallel::Bool)
    global tseq

    n = size(A,1)
    A = [A b]

    if blocksize < 1
       throw(ArgumentError("hpl_shared: invalid blocksize: $blocksize < 1"))
    end

    B_rows = collect(range(0, n, step=blocksize))
    B_rows[end] = n
    B_cols = [B_rows; [n+1]]
    nB = length(B_rows)
    depend =  Array{Any}(undef, nB, nB)

    ## Small matrix case
    if nB <= 1
        x = A[1:n, 1:n] \ A[:,n+1]
        return x
    end

    ## Add a ghost row of dependencies to boostrap the computation
    for j=1:nB; depend[1,j] = true; end
    for i=2:nB, j=1:nB; depend[i,j] = false; end

    tseq=0

    for i=1:(nB-1)
        #println("A=$A") #####
        ## Threads for panel factorizations
        tseq += @elapsed begin
            I = (B_rows[i]+1):B_rows[i+1]
            K = I[1]:n
            A_KI = @view A[K,I]
            panel_p = panel_factor!(A_KI, depend[i,i])

            ## Panel permutation
            #panel_p = K[panel_p]
            depend[i+1,i] = true

            ## Apply permutation from pivoting
            J = (B_cols[i+1]+1):B_cols[nB+1]

            ## Swap (Use DLASWP to avoid allocation)
            #A[K, J] = A[panel_p, J]
            dlaswp!((@view A[K,J]), (@view panel_p[1:length(I)]))

            ## Threads for trailing updates
            #L_II = tril(A[I,I], -1) + LinearAlgebra.I
            L_II = UnitLowerTriangular(@view A[I,I])
            K = (I[length(I)]+1):n
            A_KI = @view A[K,I]

            ## Compute all blocks of U
            ldiv!(L_II, @view A[I,J])
        end

        for j=(i+1):nB
            J = (B_cols[j]+1):B_cols[j+1]

            ## Do the trailing update (Compute U, and DGEMM - all flops are here)
            A_IJ = @view A[I,J]
            A_KJ = @view A[K,J]

            if run_parallel
                depend[i+1,j] = Threads.@spawn trailing_update!($A_IJ, $A_KI, $A_KJ,
                                                                $depend[i+1,i], $depend[i,j])
            else
                depend[i+1,j] = trailing_update!(A_IJ, A_KI, A_KJ,
                                                 depend[i+1,i], depend[i,j])
            end
        end

        # Wait for all trailing updates to complete, and write back to A
        for j=(i+1):nB
            run_parallel && fetch(depend[i+1,j])
            depend[i+1,j] = true
        end

    end

    ## Completion of the last diagonal block signals termination
    @assert depend[nB, nB]

    ## Solve the triangular system
    x = UpperTriangular(@view A[1:n,1:n]) \ @view A[:,n+1]

    return x

end ## hpl()

### Panel factorization ###

function panel_factor!(A_KI, col_dep)

    @assert col_dep

    ## Factorize a panel
    panel_p = lu!(A_KI).ipiv

end ## panel_factor_par()

### Trailing update ###

function trailing_update!(A_IJ, A_KI, A_KJ, row_dep, col_dep)

    @assert row_dep
    @assert col_dep

    ## Trailing submatrix update - All flops are here
    #A_KJ = A_KJ - A_KI*A_IJ
    !isempty(A_KJ) && BLAS.gemm!('N','N',-1.0,A_KI,A_IJ,1.0,A_KJ)

end ## trailing_update_par()

function dlaswp!(A::AbstractMatrix, ipiv::AbstractVector)

    ccall((:dlaswp_64_, :libopenblas64_),  Cvoid,
          (Ref{Int64}, Ptr{Float64}, Ref{Int64}, Ref{Int64}, Ref{Int64},
           Ref{Int64}, Ref{Int64}),
          size(A,2), A, max(1,stride(A,2)), 1, length(ipiv), ipiv, 1)

end

hpl_shared(A::Matrix, b::Vector) = hpl_shared(A, b, max(1, div(maximum(size(A)),4)), par)

par = true
n = 4096
blocksize=64

tseq=0.
a = rand(n,n);
b = rand(n);
x = hpl_shared(a, b, blocksize, par);
@printf "Total time = %4.2f sec\n" @elapsed x = hpl_shared(a, b, blocksize, par);
@printf "  Seq time = %4.2f sec\n" tseq
@show norm(a*x-b)


text:
const freeptrs = [Dict{Int,Vector{CUdeviceptr}}() for i=1:devcount()]

type CuPtr{T}
    ptr::CUdeviceptr
    bytelen::Int
    dev::Int

    function CuPtr(ptr, bytelen, dev)
        p = new(ptr, bytelen, dev)
        finalizer(p, free)
        p
    end
end

Base.convert{T}(::Type{Ptr{T}}, p::CuPtr) = Ptr{T}(p.ptr)
Base.convert(::Type{CUdeviceptr}, p::CuPtr) = p.ptr
Base.unsafe_convert{T}(::Type{Ptr{T}}, p::CuPtr) = Ptr{T}(p.ptr)
Base.unsafe_convert(::Type{CUdeviceptr}, p::CuPtr) = p.ptr

function alloc{T}(::Type{T}, len::Int)
    dev = device()
    bytelen = (sizeof(T)*len - 1) >> 10
    bytelen = (bytelen+1) << 10

    ptrs = freeptrs[dev+1]
    if haskey(ptrs,bytelen) && length(ptrs[bytelen]) > 0
        ptr = pop!(ptrs[bytelen])
    else
        ptrs[bytelen] = CUdeviceptr[]
        ref = CUdeviceptr[0]
        cuMemAlloc(ref, bytelen)
        ptr = ref[1]
    end
    CuPtr{T}(ptr, bytelen, dev)
end

free(p::CuPtr) = push!(freeptrs[p.dev+1][p.bytelen], p.ptr)

# not tested
function devicegc()
    gc()
    _dev = device()
    for dev = 0:ndevices()-1
        setdevice(dev)
        for (id,ptrs) in freeptrs[dev+1]
            for p in ptrs
                cuMemFree(p)
            end
            empty!(ptrs)
        end
    end
    setdevice(_dev)
    run(`nvidia-smi`)
end


text:
using Distributions
using Base.Test

@testset "non-central T-Distributions" begin
    ν = 50
    λ = -5

    d = NoncentralT(ν, λ)
    @test_throws MethodError NoncentralT(ν, complex(λ))
    @test d == typeof(d)(params(d)...)
    @test mean(d) ≈ -5.0766 atol = 1e-5

    λ = 5
    d = NoncentralT(ν, λ)
    @test mean(d) ≈ 5.0766 atol=1e-5

    λ = 0
    d = NoncentralT(ν, λ)
    @test var(d) == var(TDist(ν))
end


text:
# Copyright 2019 Morten Piibeleht
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or
#substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
using FileWatching

function all_subdirectories(d)
    vcat(d, mapreduce(vcat, walkdir(joinpath(@__DIR__, "scss"))) do (root, dirs, _)
        [joinpath(root, d) for d in dirs]
    end)
end

# Setup
const SASSBUILDER_RUNNING = Ref(false)
const SASSBUILDER_TASKS = Ref(Task[])
const SASSBUILDER_OUTDIR = Ref(@__DIR__)
const SASSBUILDER_LOADPATH = Ref(joinpath(@__DIR__, "scss"))
const SASSBUILDER_FILES = Ref(String[
    joinpath(@__DIR__, "scss", "documenter.scss"),
    joinpath(@__DIR__, "scss", "documenter-dark.scss"),
    joinpath(@__DIR__, "scss", "documenter-rainbow.scss"),
    joinpath(@__DIR__, "scss", "darkly.scss"),
])
const SASSBUILDER_WATCHING = Ref(all_subdirectories(joinpath(@__DIR__, "scss")))
const SASSBUILDER_DEBUG = Ref(false)

# API
function compile(filepath)
    loadpath = SASSBUILDER_LOADPATH[]
    filename = basename(filepath)
    ofile = replace(filename, r".scss$" => ".css")
    opath = joinpath(SASSBUILDER_OUTDIR[], ofile)
    cmd = `sassc -I $(loadpath) $(filepath) $(opath)`
    io = IOBuffer()
    if success(pipeline(cmd, stdout=io, stderr=io))
        @info "Compilation of $(filename) successful"
        if SASSBUILDER_DEBUG[]
            sassoutput = String(take!(io))
            @info """Compiled $(filename) successfully
            Command:

            $(cmd)

            With following stdout + stderr:

            $(sassoutput)
            """
        end
        return true
    else
        sassoutput = String(take!(io))
        @warn """Compilation of $(filename) failed.
        Command:

        $(cmd)

        With following stdout + stderr:

        $(sassoutput)
        """
        return false
    end
end
compile_all() = all(map(compile, SASSBUILDER_FILES[]))

function watch_directory(dir)
    @info "Registering watcher for: $(dir)"
    while true
        (f, reason) = watch_folder(dir, 2)
        if reason.changed
            @info "Change: $(f)" dir
            if endswith(f, ".scss")
                compile_all()
            else
                @info "Ignoring $(f) -- not Sass"
            end
        elseif reason.timedout && !SASSBUILDER_RUNNING[]
            @info "Exiting watcher for $(dir)"
            return
        end
    end
end

function watch_all()
    for d in SASSBUILDER_WATCHING[]
        t = @async watch_directory(d)
        push!(SASSBUILDER_TASKS[], t)
    end
end

# Run
compile_all() # to compile all Sass files by hand

# To automatically compile everything on every change:
SASSBUILDER_RUNNING[] = true; watch_all()

# Debugging
SASSBUILDER_DEBUG[] = true # enable
SASSBUILDER_DEBUG[] = false # disable

# To stop all the watchers:
SASSBUILDER_RUNNING[] = false


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: cognito_identity

using AWS.Compat
using AWS.UUIDs
"""
    CreateIdentityPool()

Creates a new identity pool. The identity pool is a store of user identity information that is specific to your AWS account. The keys for SupportedLoginProviders are as follows:   Facebook: graph.facebook.com    Google: accounts.google.com    Amazon: www.amazon.com    Twitter: api.twitter.com    Digits: www.digits.com    You must use AWS Developer credentials to call this API.

# Required Parameters
- `AllowUnauthenticatedIdentities`: TRUE if the identity pool supports unauthenticated logins.
- `IdentityPoolName`: A string that you provide.

# Optional Parameters
- `AllowClassicFlow`: Enables or disables the Basic (Classic) authentication flow. For more information, see Identity Pools (Federated Identities) Authentication Flow in the Amazon Cognito Developer Guide.
- `CognitoIdentityProviders`: An array of Amazon Cognito user pools and their client IDs.
- `DeveloperProviderName`: The \"domain\" by which Cognito will refer to your users. This name acts as a placeholder that allows your backend and the Cognito service to communicate about the developer provider. For the DeveloperProviderName, you can use letters as well as period (.), underscore (_), and dash (-). Once you have set a developer provider name, you cannot change it. Please take care in setting this parameter.
- `IdentityPoolTags`: Tags to assign to the identity pool. A tag is a label that you can apply to identity pools to categorize and manage them in different ways, such as by purpose, owner, environment, or other criteria.
- `OpenIdConnectProviderARNs`: A list of OpendID Connect provider ARNs.
- `SamlProviderARNs`: An array of Amazon Resource Names (ARNs) of the SAML provider for your identity pool.
- `SupportedLoginProviders`: Optional key:value pairs mapping provider names to provider app IDs.
"""

create_identity_pool(AllowUnauthenticatedIdentities, IdentityPoolName; aws_config::AWSConfig=global_aws_config()) = cognito_identity("CreateIdentityPool", Dict{String, Any}("AllowUnauthenticatedIdentities"=>AllowUnauthenticatedIdentities, "IdentityPoolName"=>IdentityPoolName); aws_config=aws_config)
create_identity_pool(AllowUnauthenticatedIdentities, IdentityPoolName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("CreateIdentityPool", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("AllowUnauthenticatedIdentities"=>AllowUnauthenticatedIdentities, "IdentityPoolName"=>IdentityPoolName), args)); aws_config=aws_config)

"""
    DeleteIdentities()

Deletes identities from an identity pool. You can specify a list of 1-60 identities that you want to delete. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityIdsToDelete`: A list of 1-60 identities that you want to delete.

"""

delete_identities(IdentityIdsToDelete; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DeleteIdentities", Dict{String, Any}("IdentityIdsToDelete"=>IdentityIdsToDelete); aws_config=aws_config)
delete_identities(IdentityIdsToDelete, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DeleteIdentities", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityIdsToDelete"=>IdentityIdsToDelete), args)); aws_config=aws_config)

"""
    DeleteIdentityPool()

Deletes an identity pool. Once a pool is deleted, users will not be able to authenticate with the pool. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.

"""

delete_identity_pool(IdentityPoolId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DeleteIdentityPool", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId); aws_config=aws_config)
delete_identity_pool(IdentityPoolId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DeleteIdentityPool", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId), args)); aws_config=aws_config)

"""
    DescribeIdentity()

Returns metadata related to the given identity, including when the identity was created and any associated linked logins. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityId`: A unique identifier in the format REGION:GUID.

"""

describe_identity(IdentityId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DescribeIdentity", Dict{String, Any}("IdentityId"=>IdentityId); aws_config=aws_config)
describe_identity(IdentityId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DescribeIdentity", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityId"=>IdentityId), args)); aws_config=aws_config)

"""
    DescribeIdentityPool()

Gets details about a particular identity pool, including the pool name, ID description, creation date, and current number of users. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.

"""

describe_identity_pool(IdentityPoolId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DescribeIdentityPool", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId); aws_config=aws_config)
describe_identity_pool(IdentityPoolId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("DescribeIdentityPool", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId), args)); aws_config=aws_config)

"""
    GetCredentialsForIdentity()

Returns credentials for the provided identity ID. Any provided logins will be validated against supported login providers. If the token is for cognito-identity.amazonaws.com, it will be passed through to AWS Security Token Service with the appropriate role for the token. This is a public API. You do not need any credentials to call this API.

# Required Parameters
- `IdentityId`: A unique identifier in the format REGION:GUID.

# Optional Parameters
- `CustomRoleArn`: The Amazon Resource Name (ARN) of the role to be assumed when multiple roles were received in the token from the identity provider. For example, a SAML-based identity provider. This parameter is optional for identity providers that do not support role customization.
- `Logins`: A set of optional name-value pairs that map provider names to provider tokens. The name-value pair will follow the syntax \"provider_name\": \"provider_user_identifier\". Logins should not be specified when trying to get credentials for an unauthenticated identity. The Logins parameter is required when using identities associated with external identity providers such as FaceBook. For examples of Logins maps, see the code examples in the External Identity Providers section of the Amazon Cognito Developer Guide.
"""

get_credentials_for_identity(IdentityId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetCredentialsForIdentity", Dict{String, Any}("IdentityId"=>IdentityId); aws_config=aws_config)
get_credentials_for_identity(IdentityId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetCredentialsForIdentity", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityId"=>IdentityId), args)); aws_config=aws_config)

"""
    GetId()

Generates (or retrieves) a Cognito ID. Supplying multiple logins will create an implicit linked account. This is a public API. You do not need any credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.

# Optional Parameters
- `AccountId`: A standard AWS account ID (9+ digits).
- `Logins`: A set of optional name-value pairs that map provider names to provider tokens. The available provider names for Logins are as follows:   Facebook: graph.facebook.com    Amazon Cognito user pool: cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>, for example, cognito-idp.us-east-1.amazonaws.com/us-east-1_123456789.    Google: accounts.google.com    Amazon: www.amazon.com    Twitter: api.twitter.com    Digits: www.digits.com
"""

get_id(IdentityPoolId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetId", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId); aws_config=aws_config)
get_id(IdentityPoolId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetId", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId), args)); aws_config=aws_config)

"""
    GetIdentityPoolRoles()

Gets the roles for an identity pool. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.

"""

get_identity_pool_roles(IdentityPoolId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetIdentityPoolRoles", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId); aws_config=aws_config)
get_identity_pool_roles(IdentityPoolId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetIdentityPoolRoles", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId), args)); aws_config=aws_config)

"""
    GetOpenIdToken()

Gets an OpenID token, using a known Cognito ID. This known Cognito ID is returned by GetId. You can optionally add additional logins for the identity. Supplying multiple logins creates an implicit link. The OpenId token is valid for 10 minutes. This is a public API. You do not need any credentials to call this API.

# Required Parameters
- `IdentityId`: A unique identifier in the format REGION:GUID.

# Optional Parameters
- `Logins`: A set of optional name-value pairs that map provider names to provider tokens. When using graph.facebook.com and www.amazon.com, supply the access_token returned from the provider's authflow. For accounts.google.com, an Amazon Cognito user pool provider, or any other OpenId Connect provider, always include the id_token.
"""

get_open_id_token(IdentityId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetOpenIdToken", Dict{String, Any}("IdentityId"=>IdentityId); aws_config=aws_config)
get_open_id_token(IdentityId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetOpenIdToken", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityId"=>IdentityId), args)); aws_config=aws_config)

"""
    GetOpenIdTokenForDeveloperIdentity()

Registers (or retrieves) a Cognito IdentityId and an OpenID Connect token for a user authenticated by your backend authentication process. Supplying multiple logins will create an implicit linked account. You can only specify one developer provider as part of the Logins map, which is linked to the identity pool. The developer provider is the \"domain\" by which Cognito will refer to your users. You can use GetOpenIdTokenForDeveloperIdentity to create a new identity and to link new logins (that is, user credentials issued by a public provider or developer provider) to an existing identity. When you want to create a new identity, the IdentityId should be null. When you want to associate a new login with an existing authenticated/unauthenticated identity, you can do so by providing the existing IdentityId. This API will create the identity in the specified IdentityPoolId. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.
- `Logins`: A set of optional name-value pairs that map provider names to provider tokens. Each name-value pair represents a user from a public provider or developer provider. If the user is from a developer provider, the name-value pair will follow the syntax \"developer_provider_name\": \"developer_user_identifier\". The developer provider is the \"domain\" by which Cognito will refer to your users; you provided this domain while creating/updating the identity pool. The developer user identifier is an identifier from your backend that uniquely identifies a user. When you create an identity pool, you can specify the supported logins.

# Optional Parameters
- `IdentityId`: A unique identifier in the format REGION:GUID.
- `TokenDuration`: The expiration time of the token, in seconds. You can specify a custom expiration time for the token so that you can cache it. If you don't provide an expiration time, the token is valid for 15 minutes. You can exchange the token with Amazon STS for temporary AWS credentials, which are valid for a maximum of one hour. The maximum token duration you can set is 24 hours. You should take care in setting the expiration time for a token, as there are significant security implications: an attacker could use a leaked token to access your AWS resources for the token's duration.  Please provide for a small grace period, usually no more than 5 minutes, to account for clock skew.
"""

get_open_id_token_for_developer_identity(IdentityPoolId, Logins; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetOpenIdTokenForDeveloperIdentity", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId, "Logins"=>Logins); aws_config=aws_config)
get_open_id_token_for_developer_identity(IdentityPoolId, Logins, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("GetOpenIdTokenForDeveloperIdentity", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId, "Logins"=>Logins), args)); aws_config=aws_config)

"""
    ListIdentities()

Lists the identities in an identity pool. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.
- `MaxResults`: The maximum number of identities to return.

# Optional Parameters
- `HideDisabled`: An optional boolean parameter that allows you to hide disabled identities. If omitted, the ListIdentities API will include disabled identities in the response.
- `NextToken`: A pagination token.
"""

list_identities(IdentityPoolId, MaxResults; aws_config::AWSConfig=global_aws_config()) = cognito_identity("ListIdentities", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId, "MaxResults"=>MaxResults); aws_config=aws_config)
list_identities(IdentityPoolId, MaxResults, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("ListIdentities", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId, "MaxResults"=>MaxResults), args)); aws_config=aws_config)

"""
    ListIdentityPools()

Lists all of the Cognito identity pools registered for your account. You must use AWS Developer credentials to call this API.

# Required Parameters
- `MaxResults`: The maximum number of identities to return.

# Optional Parameters
- `NextToken`: A pagination token.
"""

list_identity_pools(MaxResults; aws_config::AWSConfig=global_aws_config()) = cognito_identity("ListIdentityPools", Dict{String, Any}("MaxResults"=>MaxResults); aws_config=aws_config)
list_identity_pools(MaxResults, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("ListIdentityPools", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("MaxResults"=>MaxResults), args)); aws_config=aws_config)

"""
    ListTagsForResource()

Lists the tags that are assigned to an Amazon Cognito identity pool. A tag is a label that you can apply to identity pools to categorize and manage them in different ways, such as by purpose, owner, environment, or other criteria. You can use this action up to 10 times per second, per account.

# Required Parameters
- `ResourceArn`: The Amazon Resource Name (ARN) of the identity pool that the tags are assigned to.

"""

list_tags_for_resource(ResourceArn; aws_config::AWSConfig=global_aws_config()) = cognito_identity("ListTagsForResource", Dict{String, Any}("ResourceArn"=>ResourceArn); aws_config=aws_config)
list_tags_for_resource(ResourceArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("ListTagsForResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceArn"=>ResourceArn), args)); aws_config=aws_config)

"""
    LookupDeveloperIdentity()

Retrieves the IdentityID associated with a DeveloperUserIdentifier or the list of DeveloperUserIdentifier values associated with an IdentityId for an existing identity. Either IdentityID or DeveloperUserIdentifier must not be null. If you supply only one of these values, the other value will be searched in the database and returned as a part of the response. If you supply both, DeveloperUserIdentifier will be matched against IdentityID. If the values are verified against the database, the response returns both values and is the same as the request. Otherwise a ResourceConflictException is thrown.  LookupDeveloperIdentity is intended for low-throughput control plane operations: for example, to enable customer service to locate an identity ID by username. If you are using it for higher-volume operations such as user authentication, your requests are likely to be throttled. GetOpenIdTokenForDeveloperIdentity is a better option for higher-volume operations for user authentication. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.

# Optional Parameters
- `DeveloperUserIdentifier`: A unique ID used by your backend authentication process to identify a user. Typically, a developer identity provider would issue many developer user identifiers, in keeping with the number of users.
- `IdentityId`: A unique identifier in the format REGION:GUID.
- `MaxResults`: The maximum number of identities to return.
- `NextToken`: A pagination token. The first call you make will have NextToken set to null. After that the service will return NextToken values as needed. For example, let's say you make a request with MaxResults set to 10, and there are 20 matches in the database. The service will return a pagination token as a part of the response. This token can be used to call the API again and get results starting from the 11th match.
"""

lookup_developer_identity(IdentityPoolId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("LookupDeveloperIdentity", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId); aws_config=aws_config)
lookup_developer_identity(IdentityPoolId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("LookupDeveloperIdentity", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId), args)); aws_config=aws_config)

"""
    MergeDeveloperIdentities()

Merges two users having different IdentityIds, existing in the same identity pool, and identified by the same developer provider. You can use this action to request that discrete users be merged and identified as a single user in the Cognito environment. Cognito associates the given source user (SourceUserIdentifier) with the IdentityId of the DestinationUserIdentifier. Only developer-authenticated users can be merged. If the users to be merged are associated with the same public provider, but as two different users, an exception will be thrown. The number of linked logins is limited to 20. So, the number of linked logins for the source user, SourceUserIdentifier, and the destination user, DestinationUserIdentifier, together should not be larger than 20. Otherwise, an exception will be thrown. You must use AWS Developer credentials to call this API.

# Required Parameters
- `DestinationUserIdentifier`: User identifier for the destination user. The value should be a DeveloperUserIdentifier.
- `DeveloperProviderName`: The \"domain\" by which Cognito will refer to your users. This is a (pseudo) domain name that you provide while creating an identity pool. This name acts as a placeholder that allows your backend and the Cognito service to communicate about the developer provider. For the DeveloperProviderName, you can use letters as well as period (.), underscore (_), and dash (-).
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.
- `SourceUserIdentifier`: User identifier for the source user. The value should be a DeveloperUserIdentifier.

"""

merge_developer_identities(DestinationUserIdentifier, DeveloperProviderName, IdentityPoolId, SourceUserIdentifier; aws_config::AWSConfig=global_aws_config()) = cognito_identity("MergeDeveloperIdentities", Dict{String, Any}("DestinationUserIdentifier"=>DestinationUserIdentifier, "DeveloperProviderName"=>DeveloperProviderName, "IdentityPoolId"=>IdentityPoolId, "SourceUserIdentifier"=>SourceUserIdentifier); aws_config=aws_config)
merge_developer_identities(DestinationUserIdentifier, DeveloperProviderName, IdentityPoolId, SourceUserIdentifier, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("MergeDeveloperIdentities", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("DestinationUserIdentifier"=>DestinationUserIdentifier, "DeveloperProviderName"=>DeveloperProviderName, "IdentityPoolId"=>IdentityPoolId, "SourceUserIdentifier"=>SourceUserIdentifier), args)); aws_config=aws_config)

"""
    SetIdentityPoolRoles()

Sets the roles for an identity pool. These roles are used when making calls to GetCredentialsForIdentity action. You must use AWS Developer credentials to call this API.

# Required Parameters
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.
- `Roles`: The map of roles associated with this pool. For a given role, the key will be either \"authenticated\" or \"unauthenticated\" and the value will be the Role ARN.

# Optional Parameters
- `RoleMappings`: How users for a specific identity provider are to mapped to roles. This is a string to RoleMapping object map. The string identifies the identity provider, for example, \"graph.facebook.com\" or \"cognito-idp-east-1.amazonaws.com/us-east-1_abcdefghi:app_client_id\". Up to 25 rules can be specified per identity provider.
"""

set_identity_pool_roles(IdentityPoolId, Roles; aws_config::AWSConfig=global_aws_config()) = cognito_identity("SetIdentityPoolRoles", Dict{String, Any}("IdentityPoolId"=>IdentityPoolId, "Roles"=>Roles); aws_config=aws_config)
set_identity_pool_roles(IdentityPoolId, Roles, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("SetIdentityPoolRoles", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityPoolId"=>IdentityPoolId, "Roles"=>Roles), args)); aws_config=aws_config)

"""
    TagResource()

Assigns a set of tags to an Amazon Cognito identity pool. A tag is a label that you can use to categorize and manage identity pools in different ways, such as by purpose, owner, environment, or other criteria. Each tag consists of a key and value, both of which you define. A key is a general category for more specific values. For example, if you have two versions of an identity pool, one for testing and another for production, you might assign an Environment tag key to both identity pools. The value of this key might be Test for one identity pool and Production for the other. Tags are useful for cost tracking and access control. You can activate your tags so that they appear on the Billing and Cost Management console, where you can track the costs associated with your identity pools. In an IAM policy, you can constrain permissions for identity pools based on specific tags or tag values. You can use this action up to 5 times per second, per account. An identity pool can have as many as 50 tags.

# Required Parameters
- `ResourceArn`: The Amazon Resource Name (ARN) of the identity pool to assign the tags to.
- `Tags`: The tags to assign to the identity pool.

"""

tag_resource(ResourceArn, Tags; aws_config::AWSConfig=global_aws_config()) = cognito_identity("TagResource", Dict{String, Any}("ResourceArn"=>ResourceArn, "Tags"=>Tags); aws_config=aws_config)
tag_resource(ResourceArn, Tags, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("TagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceArn"=>ResourceArn, "Tags"=>Tags), args)); aws_config=aws_config)

"""
    UnlinkDeveloperIdentity()

Unlinks a DeveloperUserIdentifier from an existing identity. Unlinked developer users will be considered new identities next time they are seen. If, for a given Cognito identity, you remove all federated identities as well as the developer user identifier, the Cognito identity becomes inaccessible. You must use AWS Developer credentials to call this API.

# Required Parameters
- `DeveloperProviderName`: The \"domain\" by which Cognito will refer to your users.
- `DeveloperUserIdentifier`: A unique ID used by your backend authentication process to identify a user.
- `IdentityId`: A unique identifier in the format REGION:GUID.
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.

"""

unlink_developer_identity(DeveloperProviderName, DeveloperUserIdentifier, IdentityId, IdentityPoolId; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UnlinkDeveloperIdentity", Dict{String, Any}("DeveloperProviderName"=>DeveloperProviderName, "DeveloperUserIdentifier"=>DeveloperUserIdentifier, "IdentityId"=>IdentityId, "IdentityPoolId"=>IdentityPoolId); aws_config=aws_config)
unlink_developer_identity(DeveloperProviderName, DeveloperUserIdentifier, IdentityId, IdentityPoolId, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UnlinkDeveloperIdentity", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("DeveloperProviderName"=>DeveloperProviderName, "DeveloperUserIdentifier"=>DeveloperUserIdentifier, "IdentityId"=>IdentityId, "IdentityPoolId"=>IdentityPoolId), args)); aws_config=aws_config)

"""
    UnlinkIdentity()

Unlinks a federated identity from an existing account. Unlinked logins will be considered new identities next time they are seen. Removing the last linked login will make this identity inaccessible. This is a public API. You do not need any credentials to call this API.

# Required Parameters
- `IdentityId`: A unique identifier in the format REGION:GUID.
- `Logins`: A set of optional name-value pairs that map provider names to provider tokens.
- `LoginsToRemove`: Provider names to unlink from this identity.

"""

unlink_identity(IdentityId, Logins, LoginsToRemove; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UnlinkIdentity", Dict{String, Any}("IdentityId"=>IdentityId, "Logins"=>Logins, "LoginsToRemove"=>LoginsToRemove); aws_config=aws_config)
unlink_identity(IdentityId, Logins, LoginsToRemove, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UnlinkIdentity", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("IdentityId"=>IdentityId, "Logins"=>Logins, "LoginsToRemove"=>LoginsToRemove), args)); aws_config=aws_config)

"""
    UntagResource()

Removes the specified tags from an Amazon Cognito identity pool. You can use this action up to 5 times per second, per account

# Required Parameters
- `ResourceArn`: The Amazon Resource Name (ARN) of the identity pool that the tags are assigned to.
- `TagKeys`: The keys of the tags to remove from the user pool.

"""

untag_resource(ResourceArn, TagKeys; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UntagResource", Dict{String, Any}("ResourceArn"=>ResourceArn, "TagKeys"=>TagKeys); aws_config=aws_config)
untag_resource(ResourceArn, TagKeys, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UntagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceArn"=>ResourceArn, "TagKeys"=>TagKeys), args)); aws_config=aws_config)

"""
    UpdateIdentityPool()

Updates an identity pool. You must use AWS Developer credentials to call this API.

# Required Parameters
- `AllowUnauthenticatedIdentities`: TRUE if the identity pool supports unauthenticated logins.
- `IdentityPoolId`: An identity pool ID in the format REGION:GUID.
- `IdentityPoolName`: A string that you provide.

# Optional Parameters
- `AllowClassicFlow`: Enables or disables the Basic (Classic) authentication flow. For more information, see Identity Pools (Federated Identities) Authentication Flow in the Amazon Cognito Developer Guide.
- `CognitoIdentityProviders`: A list representing an Amazon Cognito user pool and its client ID.
- `DeveloperProviderName`: The \"domain\" by which Cognito will refer to your users.
- `IdentityPoolTags`: The tags that are assigned to the identity pool. A tag is a label that you can apply to identity pools to categorize and manage them in different ways, such as by purpose, owner, environment, or other criteria.
- `OpenIdConnectProviderARNs`: A list of OpendID Connect provider ARNs.
- `SamlProviderARNs`: An array of Amazon Resource Names (ARNs) of the SAML provider for your identity pool.
- `SupportedLoginProviders`: Optional key:value pairs mapping provider names to provider app IDs.
"""

update_identity_pool(AllowUnauthenticatedIdentities, IdentityPoolId, IdentityPoolName; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UpdateIdentityPool", Dict{String, Any}("AllowUnauthenticatedIdentities"=>AllowUnauthenticatedIdentities, "IdentityPoolId"=>IdentityPoolId, "IdentityPoolName"=>IdentityPoolName); aws_config=aws_config)
update_identity_pool(AllowUnauthenticatedIdentities, IdentityPoolId, IdentityPoolName, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cognito_identity("UpdateIdentityPool", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("AllowUnauthenticatedIdentities"=>AllowUnauthenticatedIdentities, "IdentityPoolId"=>IdentityPoolId, "IdentityPoolName"=>IdentityPoolName), args)); aws_config=aws_config)