using QuantumAlgebra
using QuantumAlgebra: δ, prodtuples, prodtuple, sumtuple, distribute_indices!
using Test

@testset "QuantumAlgebra.jl" begin
    for with_σpm in (false,true)
        QuantumAlgebra.use_σpm(with_σpm)

        @test QuantumAlgebra.using_σpm == with_σpm

        @test QuantumAlgebra.SpatialIndex(QuantumAlgebra.x) == QuantumAlgebra.x

        # all equal numbers should be equal scalars (ignore type)
        @test scal(0) == scal(0.0)
        @test scal(0) == scal(0im)
        @test scal(1.5) == scal(3//2)

        # test params and their complex conjugation
        @test param(:ω) == param(:ω,()) == param(:ω,'n') == param(:ω,'n',())
        @test param(:ω,'n',(:i)) == param(:ω,:i)
        @test param(:ω,'n',(:i,:j,2)) == param(:ω,:i,:j,2)
        @test_throws ErrorException param(:ω,'g')
        @test_throws MethodError param(:ω,2,:i,"a")
        @test_throws MethodError a("a")

        @test adjoint(param(:g,'r')) == param(:g,'r')
        @test adjoint(param(:g,'n')) == param(:g,'c')
        @test adjoint(param(:g,:i)) == param(:g,'c',:i)
        @test adjoint(param(:g,'c',(:i,1,:m))) == param(:g,:i,1,:m)

        tmp1 = param(:g,2)*param(:g,1)*param(:g,'c',3)*param(:b)*param(:a)*param(:d,'c',3)*param(:f,'r',1,:i)
        tmpc = param(:g,'c',2)*param(:g,'c',1)*param(:g,'n',3)*param(:b,'c')*param(:a,'c')*param(:d,'n',3)*param(:f,'r',(1,:i))
        tmp2 = param(:a)*param(:b)*param(:g,1)*param(:g,2)*param(:g,'c',3)*param(:d,'c',3)*param(:f,'r',(1,:i))
        @test tmp1 == tmp2
        @test tmp1' == tmpc

        @test a()*5 == 5*a() == scal(5)*a()
        @test a()+5 == 5+a() == scal(5)+a()
        @test 5-a() == scal(5)+scal(-1)*a()
        @test a()-5 == scal(-5)+a()

        @test_throws MethodError σx(:i,"a")
        @test σx(:i,:b) == σx((:i,:b))
        @test σx(:i,:j) != σx(:i,:b)

        @test σx() == σp() + σm()
        @test σx(:i) == σp(:i) + σm(:i)
        @test σx(:i,:j) == σp((:i,:j)) + σm((:i,:j))

        @test σy(:i) == scal(-1im)*(σp(:i) - σm(:i))
        @test σz(:i) == scal(2)*σp(:i)*σm(:i) - scal(1)

        for s=(σx,σy,σz)
            @test s(:i)*s(:i) == scal(1)
            @test s(:i)' == s(:i)
        end
        @test σx(:i)*σy(:i) == scal(1im)*σz(:i)
        @test σy(:i)*σx(:i) == scal(-1im)*σz(:i)
        @test σy()*σz() == scal(1im)*σx()
        @test σx(:i,:j)*σz(:i,:j) == scal(-1im)*σy(:i,:j)
        @test σx(:i,:j)*σz(:i,:j) != scal(-1im)*σy(:i,:k)

        @test a()' == adag()
        @test a(:m)*adag(:m) == adag(:m)*a(:m) + scal(1)
        @test (a(:m)*a(1))' == adag(:m)*adag(1)
        @test (a(1,2,:k)*a(:m))' == adag(1,2,:k)*adag(:m)

        @test fdag(:a)*f(:b) + f(:b)*fdag(:a) == δ(:a,:b)
        @test f(:a)*f(:b) + f(:b)*f(:a) == scal(0)
        @test fdag(:a)*fdag(:b) + fdag(:b)*fdag(:a) == scal(0)

        @test f()' == fdag()
        @test fdag()' == f()
        @test (fdag()*f())' == fdag()*f()
        @test comm(fdag(),f()) == -comm(f(),fdag())

        @test scal(1+1im) < scal(2-1im)
        @test scal(1+1im) > scal(1-1im)
        @test a(1) < σx(1)
        @test adag(1) < σx(1)
        @test !(a(1) < adag(1))
        @test adag(1) < a(1)
        @test a(5) < a(:i)
        @test !(adag(:m) < adag(2))
        @test fdag() < f()
        @test adag() < fdag()
        @test fdag(:i,:j) < fdag(:k)

        @test a(1) * (σy(1) * a(1))' == a(1) * (adag(1) * σy(1)) == adag(1)*a(1)*σy(1) + σy(1)

        @test ∑(:j,a(:j))*∑(:i,a(:i)) == ∑(:i,∑(:j,a(:i)*a(:j)))

        tmp = OpSumAnalytic(:i,adag(:i)*a(:i))
        @test tmp' == tmp
        @test a(:i)*OpSumAnalytic(:i,a(:i)) == OpSumAnalytic(:i_1,a(:i_1)*a(:i))
        @test adag(:n)*tmp == OpSumAnalytic(:i,adag(:n)*adag(:i)*a(:i))
        @test a(:n)   *tmp == OpSumAnalytic(:i,adag(:i)*a(:n)*a(:i)) + a(:n)
        @test param(:g,:i)*OpSumAnalytic(:i,a(:i)) == OpSumAnalytic(:i_1,param(:g,:i)*a(:i_1))
        @test param(:g,:i_1)*a(:i)*OpSumAnalytic(:i,a(:i)) == OpSumAnalytic(:i_2,param(:g,:i_1)*a(:i)*a(:i_2))
        @test param(:g,:n)*OpSumAnalytic(:i,a(:i)) == ∑(:i,param(:g,:n)*a(:i))

        @test Pr"gz_i,mu" == param(:gz,'r',(:i,:mu))
        @test Pc"gz_i,mu" == param(:gz,'n',(:i,:mu))
        @test Pc"gz_i,mu"' == param(:gz,'c',(:i,:mu))

        @test prodtuple(a(5)) == (a(5),)
        # tuples come out ordered!
        @test prodtuple(a(5)*a(4)) == (a(4),a(5))
        @test prodtuple(a(5)*a(4)) != (a(5),a(4))

        @test sumtuple(a(5)) == (a(5),)
        # tuples come out ordered!
        @test sumtuple(a(5)+a(4)) == (a(4),a(5))
        @test sumtuple(a(5)+a(4)) != (a(5),a(4))

        @test_throws ErrorException prodtuples(a(5)+a(4))
        # tuples come out ordered!
        if QuantumAlgebra.using_σpm
            tmp1 = scal(3)*param(:ω)*param(:g)*ExpVal(σp(:k))*σp(:k)*adag(5)*a(5)
            tmp2 = ( (scal(3),param(:g),param(:ω)), (ExpVal(σp(:k)),), (adag(5),a(5),σp(:k)) )
            @test prodtuples(tmp1) == tmp2
        else
            tmp1 = scal(3)*param(:ω)*param(:g)*ExpVal(σz(:k))*σz(:k)*adag(5)*a(5)
            tmp2 = ( (scal(3),param(:g),param(:ω)), (ExpVal(σz(:k)),), (adag(5),a(5),σz(:k)) )
            @test prodtuples(tmp1) == tmp2
        end

        @test δ(:i,:k)*a(:k) == a(:i)*δ(:k,:i)
        @test δ(:i,:k)*a(:k,:i) == a(:i,:i)*δ(:k,:i)
        @test δ(:i,:k)*δ(:i,:j) == δ(:k,:i)*δ(:j,:k)
        # k cannot be equal to 1 and 3 at the same time
        @test δ(1,:k)*δ(:k,3)*σx(:k) == scal(0)
        @test δ(1,:k)*δ(:k,1)*σx(:k) == δ(1,:k)*σx(1)

        @test comm(σx(5),σy(3)) == scal(0)
        @test comm(σx(5),σx(5)) == scal(0)
        @test comm(σx(1),σz(1)) == scal(-2im)*σy(1)
        @test comm(σx(:mu),σy(:muuu)) == scal(2im)*δ(:mu,:muuu)*σz(:mu)
        @test scal(1//2im)*comm(σx(:m),σy(:m)) == σz(:m)
        @test σx(:a)*σy(:a)*σz(:a) == scal(1im)

        @test comm(param(:g),a(5)+a(3)) == scal(0)
        @test comm(param(:g),a(5)*a(3)) == scal(0)
        @test comm(a(5)+a(3),param(:g)) == scal(0)
        @test comm(a(5)*a(3),param(:g)) == scal(0)

        @test comm(a(5)+a(3),adag(5)) == comm(a(5),adag(5))+ comm(a(3),adag(5))
        @test comm(a(5)+a(3),adag(5)*a(3)) == comm(a(5),adag(5)*a(3))+ comm(a(3),adag(5)*a(3))

        @test adag(2)*σy(:i) - scal(14)*param(:ω) == scal(-14)*param(:ω) + σy(:i)*adag(2)
        @test adag(2)*σy(:i) == σy(:i)*adag(2)

        @test σp(1) * σm(1) == scal(1//2)*σz(1) + scal(1//2)
        @test σm(1)*σp(1) == scal(1) - σp(1)*σm(1)
        @test σm(1)*σp(1) == σp(1)*σm(1) + comm(σm(1),σp(1))
        @test σz(1) == σp(1)*σm(1) - σm(1)*σp(1)

        @test comm(σp(1),σp(1)) == scal(0)
        @test comm(σp(:n),σm(:n)) == σz(:n)

        @test comm(a(1),   adag(1)*a(1)) == a(1)
        @test comm(adag(1),adag(1)*a(1)) == -adag(1)

        @test ExpVal(scal(3)) == scal(3)
        @test Corr(scal(3)) == scal(3)
        @test Corr(scal(3)+adag(2)) == scal(3) + Corr(adag(2))

        @test ascorr(scal(3)) == scal(3)
        @test ascorr(a(2)) == ExpVal(a(2))
        @test ascorr(scal(3)*param(:g)) == scal(3)*param(:g)
        @test ascorr(scal(3)*a(2)) == scal(3)*ExpVal(a(2))
        @test ascorr(scal(3)+adag(2)) == scal(3) + ExpVal(adag(2))

        @test ascorr(a(2)*a(2)) == Corr(a(2)*a(2)) + ExpVal(a(2))*ExpVal(a(2))
        @test ascorr(a(2)*a(:m))' == Corr(adag(2)*adag(:m)) + ExpVal(adag(:m))*ExpVal(adag(2))
        tmpas = a.(1:3)
        @test *(tmpas...) == a(1)*a(2)*a(3)
        tmpEVs = ExpVal.(tmpas)
        @test ascorr(*(tmpas...)) == Corr(*(tmpas...)) + *(tmpEVs...) + tmpEVs[1]*Corr(tmpas[2]*tmpas[3]) + tmpEVs[2]*Corr(tmpas[1]*tmpas[3]) + tmpEVs[3]*Corr(tmpas[1]*tmpas[2])

        @test a(1) < ascorr(a(1)*a(2)*a(3)*a(4))
        @test_throws ErrorException ascorr(a(1)*a(2)*a(3)*a(4)*a(5))

        if QuantumAlgebra.using_σpm
            @test ascorr(scal(-1)*param(:g,'r',1)*σp(1)) == -param(:g,'r',1)*ExpVal(σp(1))
            @test ascorr(OpSumAnalytic(:i,σp(:i)*σm(:n))) == OpSumAnalytic(:i,Corr(σp(:i)*σm(:n))) + OpSumAnalytic(:i,ExpVal(σp(:i))*ExpVal(σm(:n)))
        else
            @test ascorr(scal(-1)*param(:g,'r',1)*σz(1)) == -param(:g,'r',1)*ExpVal(σz(1))
            @test ascorr(OpSumAnalytic(:i,σy(:i)*σy(:n))) == OpSumAnalytic(:i,Corr(σy(:i)*σy(:n))) + OpSumAnalytic(:i,ExpVal(σy(:i))*ExpVal(σy(:n))) - ExpVal(σy(:n))*ExpVal(σy(:n))
        end

        @test CorrOrExp(a(5)) == ExpVal(a(5))
        @test CorrOrExp(a(5)*a(:i)) == Corr(a(5)*a(:i))

        H = ∑(:i,param(:ω,'r',:i)*adag(:i)*a(:i))
        @test comm(a(:i),H) == param(:ω,'r',:i)*a(:i)
        @test comm(a(:n),H) == param(:ω,'r',:n)*a(:n)
        @test comm(H,a(:n)) == -param(:ω,'r',:n)*a(:n)
        @test comm(adag(:n),H) == -param(:ω,'r',:n)*adag(:n)
        @test comm(adag(:n)*a(:m),H) == (param(:ω,'r',:m)-param(:ω,'r',:n))*adag(:n)*a(:m)

        @test a()*H == ∑(:i,param(:ω,'r',:i)*adag(:i)*a(:i)*a())
        @test a(:k)*H == param(:ω,'r',:k)*a(:k) + ∑(:i,param(:ω,'r',:i)*adag(:i)*a(:i)*a(:k))
        HH = ∑(:i,param(:ω,'r',:i,:i)*adag(:i,:i)*a(:i,:i))
        @test a(:k,:k)*HH == param(:ω,'r',:k,:k)*a(:k,:k) + ∑(:i,param(:ω,'r',:i,:i)*adag(:i,:i)*a(:i,:i)*a(:k,:k))

        @test Avac(H) == scal(0)
        @test vacA(H) == scal(0)
        @test vacA(adag(3)*σp(1)*σm(1)) == scal(0)
        @test vacA(fdag(:n)) == scal(0)
        @test vacA(f(:n)) == f(:n)
        @test vacA(a(:n)) == a(:n)
        @test Avac(fdag(:n)) == fdag(:n)
        @test Avac(f(:n)) == scal(0)
        @test Avac(a(3)*σp(1)*σm(1)) == scal(0)
        @test Avac(σm(1)*σp(1)) == scal(1)
        @test Avac(σp(1)*σm(1)) == scal(0)
        @test Avac(σp(1)) == σp(1)
        @test vacA(σm(1)) == σm(1)
        if QuantumAlgebra.using_σpm
            @test Avac(σm(1)) == scal(0)
            @test vacA(σp(1)) == scal(0)
        else
            @test Avac(σx(1)) == vacA(σx(1)) == σx(1)
        end
        @test vacExpVal(σx(1)) == scal(0)
        @test vacExpVal(σp(1)) == scal(0)
        @test vacExpVal(OpSumAnalytic(:i,σp(:i))) == scal(0)
        @test vacExpVal(σp(:i)*σm(:k)) == scal(0)

        @test a(:n)*adag(:n)*a(:n)*adag(:n) == scal(1) + scal(3)*adag(:n)*a(:n) + adag(:n)*adag(:n)*a(:n)*a(:n)

        S = scal(1/√(2*6))*adag(:n)*adag(:n)*adag(:n) + scal(1/√2)*adag(:m)
        for (A,val) in [(scal(1),scal(1)),
                        (adag(:n)*a(:n),scal(1.5) + 0.5 * δ(:n,:m)),
                        (adag(:n)*adag(:n)*a(:n)*a(:n),scal(3))]
            @test vacExpVal(A,S) ≈ val
        end

        tmp = scal(1+2im)*OpSumAnalytic(:i,a(:i)*adag(:i)*ascorr(adag(:n)*a(:m)))
        @test latex(scal(1+2im)) == "(1+2i)"
        @test latex(scal(1+2im//5)) == "\\left(1+\\frac{2}{5}i\\right)"

        tmp = OpSumAnalytic(:i,ascorr(adag(:n)*a(:i)))
        tmplatex = "\\sum_{i}\\langle a_{n}^\\dagger a_{i} \\rangle_{c} + \\sum_{i}\\langle a_{n}^\\dagger \\rangle \\langle a_{i} \\rangle"
        @test latex(tmp) == tmplatex
        @test ascorr(tmp) == tmp
        @test sprint(show,"text/latex",tmp) == "\$$(tmplatex)\$"
        if QuantumAlgebra.using_σpm
            @test latex(σp()) == "\\sigma^+"
        else
            @test latex(σz()) == "\\sigma_{z}"
        end

        inds = [:a,:b,:c,:d,:e,:f,:g,:h,:i,:j,:k,:l,:m,:n]
        tmp1 = param(:ω,:y)*a(1)*adag(1)*a(3)*adag(4)*ExpVal(a(5))*Corr(adag(5)*a(9))
        tmp2 = param(:ω,:a)*ExpVal(a(:b))*Corr(adag(:c)*a(:d))*adag(:e)*a(:f) + param(:ω,:g)*ExpVal(a(:h))*Corr(adag(:i)*a(:j))*adag(:k)*adag(:l)*a(:m)*a(:n)
        @test distribute_indices!(copy(inds),tmp1) == tmp2
        if QuantumAlgebra.using_σpm
            tmp1 = a(1,:n)*adag()*σm(1,:n)*σp()
            @test distribute_indices!(copy(inds),tmp1) == adag()*a(:a,:b)*σp()*σm(:c,:d)
        else
            tmp1 = a(1,:n)*adag()*σz(1,:n)*σy()
            @test distribute_indices!(copy(inds),tmp1) == adag()*a(:a,:b)*σy()*σz(:c,:d)
        end

        @test_throws MethodError distribute_indices!(copy(inds),OpSumAnalytic(:i,a(:i)))
        @test_throws ArgumentError distribute_indices!([:a,:b],tmp1)

        @test QuantumAlgebra.exchange_inds(adag(:j)*a(:k),:k,:j) == adag(:k)*a(:j)
        @test QuantumAlgebra.extindices(∑(:i,adag(:i)*a(:k))) == [:k]
        @test QuantumAlgebra.symmetric_index_nums(adag(:i)*adag(:j)*a(:k)*a(:l)) == [2,2]

        @test string(OpSumAnalytic(:i,a(:i)) * adag(:n)) == "1 + ∑_i a†(n) a(i)"
        if QuantumAlgebra.using_σpm
            @test string(a(5)*adag(5)*σp(3)*ascorr(adag(5,:i)*a(5))) == "⟨a†(5i)⟩ ⟨a(5)⟩ σ+(3) + ⟨a†(5i) a(5)⟩c σ+(3) + ⟨a†(5i)⟩ ⟨a(5)⟩ a†(5) a(5) σ+(3) + ⟨a†(5i) a(5)⟩c a†(5) a(5) σ+(3)"
        else
            @test string(a(5)*adag(5)*σz(3)*ascorr(adag(5,:i)*a(5))) == "⟨a†(5i)⟩ ⟨a(5)⟩ σz(3) + ⟨a†(5i) a(5)⟩c σz(3) + ⟨a†(5i)⟩ ⟨a(5)⟩ a†(5) a(5) σz(3) + ⟨a†(5i) a(5)⟩c a†(5) a(5) σz(3)"
        end
    end
end


#!/usr/bin/env julia

include("PassiveCable.jl")

using JSON
using SIUnits.ShortUnits
using PassiveCable

# This should run the same effective model
# as rallpack1, but with differing
# electrical parameters (see below).

function run_cable(x_prop, ts)
    # Physical properties:

    # f is a fudge factor. rM needs to be the same
    # the same as in arbor, where we cannot yet set
    # the membrane conductance parameter. Scaling
    # other parameters proportionally, however,
    # gives the same dynamics.

    f = 0.1/4

    diam =   1.0µm        # cable diameter
    L    =   1.0mm        # cable length
    I    =   0.1nA     /f # current injection
    rL   =   1.0Ω*m    *f # bulk resistivity
    erev = -65.0mV        # (passive) reversal potential
    rM   =   4Ω*m^2    *f # membrane resistivity
    cM   =   0.01F/m^2 /f # membrane specific capacitance

    # convert to linear resistivity, length and time constants
    area = pi*diam^2/4
    r    = rL/area

    lambda = sqrt(diam/4 * rM/rL)
    tau = cM*rM

    # compute solutions
    tol = 1e-8mV
    return [cable(L*x_prop, t, L, lambda, tau, r, erev, -I, tol=tol) for t in ts]
end

function run_rallpack1(x_prop, ts)
    return [rallpack1(0.001*x_prop, t/s)*V for t in ts]
end

# Generate traces at x=0, x=0.3L, x=L

ts = collect(0s: 0.025ms: 250ms)
trace = Dict(
    :name => "membrane voltage",
    :sim => "numeric",
    :model => "rallpack1",
    :units => "mV",
    :data => Dict(
        :time => map(t->t/ms, ts),
        Symbol("cable.x0.0") => map(v->v/mV, run_cable(0, ts)),
        Symbol("cable.x0.3") => map(v->v/mV, run_cable(0.3, ts)),
        Symbol("cable.x1.0") => map(v->v/mV, run_cable(1.0, ts))
    )
)

println(JSON.json([trace]))


@testset "Plastic" begin
    # constructor
    m = Plastic(E=200e3, ν=0.3, σ_y=200., H=50., r=0.5, κ_∞=13., α_∞=13.)
    cache = get_cache(m)

    # initial state
    state = initial_material_state(m)

    # strain at yield point for uniaxial stress
    ε11_yield = m.σ_y / m.E

    # elastic branch
    Δε = SymmetricTensor{2,3,Float64}((i,j) -> i==1 && j==1 ? 0.5ε11_yield : (i == j ? -0.5ε11_yield*m.ν : 0.0))
    σ, ∂σ∂ε, temp_state = material_response(m, Δε, state; cache=cache)
    @test σ ≈ SymmetricTensor{2,3,Float64}((i,j) -> i==1 && j==1 ? 0.5ε11_yield*m.E : 0.0)
    @test ∂σ∂ε == m.Eᵉ
    @test temp_state.κ == 0.0
    @test temp_state.α == zero(SymmetricTensor{2,3})
    @test temp_state.μ == 0.0

    # yield point
    Δε = Δε = SymmetricTensor{2,3,Float64}((i,j) -> i==1 && j==1 ? ε11_yield : (i == j ? -ε11_yield*m.ν : 0.0))
    σ, ∂σ∂ε, temp_state = material_response(m, Δε, state; cache=cache)
    @test sqrt(3/2)*norm(dev(σ-temp_state.α)) ≈ m.σ_y

    # plastic branch
    Δε = Δε = SymmetricTensor{2,3,Float64}((i,j) -> i==1 && j==1 ? 2ε11_yield : (i == j ? -2ε11_yield*m.ν : 0.0))
    σ, ∂σ∂ε, temp_state = material_response(m, Δε, state; cache=cache)
    @test sqrt(3/2)*norm(dev(σ-temp_state.α)) > m.σ_y
end

function get_Plastic_loading()
    strain1 = range(0.0,  0.005, length=5)
    strain2 = range(0.005, 0.001, length=5)
    strain3 = range(0.001, 0.007, length=5)

    _C = [strain1[2:end]..., strain2[2:end]..., strain3[2:end]...]
    ε = [SymmetricTensor{2,3}((x, x/10, 0.0, 0.0, 0.0, 0.0)) for x in _C]

    return ε
end

@testset "Plastic jld2" begin
    m = Plastic(E=200e3, ν=0.3, σ_y=200., H=50., r=0.5, κ_∞=13., α_∞=13.)

    loading = get_Plastic_loading()
    check_jld2(m, loading, "Plastic1")#, debug_print=true, OVERWRITE_JLD2=true)
end


using FastMarching
using Base.Test
using LinearAlgebra

function eye(n)
  fill(0,(n,n))+I
end

function testeye(n::Integer,endpoint=[1.,1.],stepsize=0.1)
  speedmap = eye(n) .* 1000 .+ 0.001
  source = [float(size(speedmap,1)), float(size(speedmap,2))]
  distancemap = FastMarching.msfm(speedmap,source,true,true)
  endpoint = [1.,1.]
  shortestline = FastMarching.shortestpath(distancemap,endpoint,source,0.1)
end

function runtests()
  const endpoint = [1.,1.]
  for t in 2:2:10, stepsize in [0.1,0.01]
    println("size: $(lpad(t,2," ")) stepsize: $(rpad(string(stepsize),4,"0"))")
    shortestline = testeye(t,endpoint,stepsize)
    @test sum(shortestline[:,2]-shortestline[:,1]) == 0
    @test sqrt((shortestline[1,1]-1.)^2-(shortestline[1,2]-1.)^2) <= stepsize
  end

  try
    rm("multiplestarts.png")
  end
  include(joinpath(Pkg.dir("FastMarching"),"examples/multiplestart.jl"))
  @test isfile("multiplestart.png")

end

runtests()


using Base.Iterators
using Scapin.Brick

function expected_avg_gradient_operator(h::NTuple{d,T}) where {d,T<:Number}
    ℒ = cell_vertices(d)
    D = zeros(T, d, fill(2, d)...)
    for i = 1:d, p in ℒ
        D[i, p] = (-1)^p[i] / 2^(d - 1) / h[i]
    end
    return D
end

function expected_avg_strain_displacement_operator(h::NTuple{d,T}) where {d,T<:Number}
    ℒ = cell_vertices(d)
    D = expected_avg_gradient_operator(h)
    B = zeros(T, d, d, d, fill(2, d)...)
    for i = 1:d, j = 1:d, k = 1:d, p in ℒ
        B[i, j, k, p] = (T(j == k) * D[i, p] + T(i == k) * D[j, p]) / 2
    end
    return B
end

function expected_K_λ(h::NTuple{d,T}) where {d,T<:Number}
    K_λ = zeros(Float64, d, fill(2, d)..., d, fill(2, d)...)
    if d == 2
        value = -1 / 4
        K_λ[1, 1, 1, 2, 1, 2] = value
        K_λ[1, 1, 1, 2, 2, 2] = value
        K_λ[1, 1, 2, 2, 1, 2] = value
        K_λ[1, 1, 2, 2, 2, 2] = value
        K_λ[1, 2, 1, 2, 1, 1] = value
        K_λ[1, 2, 1, 2, 2, 1] = value
        K_λ[1, 2, 2, 2, 1, 1] = value
        K_λ[1, 2, 2, 2, 2, 1] = value
        K_λ[2, 1, 1, 1, 2, 1] = value
        K_λ[2, 1, 1, 1, 2, 2] = value
        K_λ[2, 1, 2, 1, 1, 1] = value
        K_λ[2, 1, 2, 1, 1, 2] = value
        K_λ[2, 2, 1, 1, 2, 1] = value
        K_λ[2, 2, 1, 1, 2, 2] = value
        K_λ[2, 2, 2, 1, 1, 1] = value
        K_λ[2, 2, 2, 1, 1, 2] = value

        value = 1 / 4
        K_λ[1, 1, 1, 2, 1, 1] = value
        K_λ[1, 1, 1, 2, 2, 1] = value
        K_λ[1, 1, 2, 2, 1, 1] = value
        K_λ[1, 1, 2, 2, 2, 1] = value
        K_λ[1, 2, 1, 2, 1, 2] = value
        K_λ[1, 2, 1, 2, 2, 2] = value
        K_λ[1, 2, 2, 2, 1, 2] = value
        K_λ[1, 2, 2, 2, 2, 2] = value
        K_λ[2, 1, 1, 1, 1, 1] = value
        K_λ[2, 1, 1, 1, 1, 2] = value
        K_λ[2, 1, 2, 1, 2, 1] = value
        K_λ[2, 1, 2, 1, 2, 2] = value
        K_λ[2, 2, 1, 1, 1, 1] = value
        K_λ[2, 2, 1, 1, 1, 2] = value
        K_λ[2, 2, 2, 1, 2, 1] = value
        K_λ[2, 2, 2, 1, 2, 2] = value

        value = -h[1] / (3 * h[2])
        K_λ[2, 1, 1, 2, 1, 2] = value
        K_λ[2, 1, 2, 2, 1, 1] = value
        K_λ[2, 2, 1, 2, 2, 2] = value
        K_λ[2, 2, 2, 2, 2, 1] = value

        value = -h[1] / (6 * h[2])
        K_λ[2, 1, 1, 2, 2, 2] = value
        K_λ[2, 1, 2, 2, 2, 1] = value
        K_λ[2, 2, 1, 2, 1, 2] = value
        K_λ[2, 2, 2, 2, 1, 1] = value

        value = h[1] / (6 * h[2])
        K_λ[2, 1, 1, 2, 2, 1] = value
        K_λ[2, 1, 2, 2, 2, 2] = value
        K_λ[2, 2, 1, 2, 1, 1] = value
        K_λ[2, 2, 2, 2, 1, 2] = value

        value = h[1] / (3 * h[2])
        K_λ[2, 1, 1, 2, 1, 1] = value
        K_λ[2, 1, 2, 2, 1, 2] = value
        K_λ[2, 2, 1, 2, 2, 1] = value
        K_λ[2, 2, 2, 2, 2, 2] = value

        value = -h[2] / (3 * h[1])
        K_λ[1, 1, 1, 1, 2, 1] = value
        K_λ[1, 1, 2, 1, 2, 2] = value
        K_λ[1, 2, 1, 1, 1, 1] = value
        K_λ[1, 2, 2, 1, 1, 2] = value

        value = -h[2] / (6 * h[1])
        K_λ[1, 1, 1, 1, 2, 2] = value
        K_λ[1, 1, 2, 1, 2, 1] = value
        K_λ[1, 2, 1, 1, 1, 2] = value
        K_λ[1, 2, 2, 1, 1, 1] = value

        value = h[2] / (6 * h[1])
        K_λ[1, 1, 1, 1, 1, 2] = value
        K_λ[1, 1, 2, 1, 1, 1] = value
        K_λ[1, 2, 1, 1, 2, 2] = value
        K_λ[1, 2, 2, 1, 2, 1] = value

        value = h[2] / (3 * h[1])
        K_λ[1, 1, 1, 1, 1, 1] = value
        K_λ[1, 1, 2, 1, 1, 2] = value
        K_λ[1, 2, 1, 1, 2, 1] = value
        K_λ[1, 2, 2, 1, 2, 2] = value
    elseif d == 3
        value = -h[1] / 12
        K_λ[2, 1, 1, 1, 3, 1, 1, 2] = value
        K_λ[2, 1, 1, 1, 3, 1, 2, 2] = value
        K_λ[2, 1, 1, 2, 3, 1, 1, 2] = value
        K_λ[2, 1, 1, 2, 3, 1, 2, 2] = value
        K_λ[2, 1, 2, 1, 3, 1, 1, 1] = value
        K_λ[2, 1, 2, 1, 3, 1, 2, 1] = value
        K_λ[2, 1, 2, 2, 3, 1, 1, 1] = value
        K_λ[2, 1, 2, 2, 3, 1, 2, 1] = value
        K_λ[2, 2, 1, 1, 3, 2, 1, 2] = value
        K_λ[2, 2, 1, 1, 3, 2, 2, 2] = value
        K_λ[2, 2, 1, 2, 3, 2, 1, 2] = value
        K_λ[2, 2, 1, 2, 3, 2, 2, 2] = value
        K_λ[2, 2, 2, 1, 3, 2, 1, 1] = value
        K_λ[2, 2, 2, 1, 3, 2, 2, 1] = value
        K_λ[2, 2, 2, 2, 3, 2, 1, 1] = value
        K_λ[2, 2, 2, 2, 3, 2, 2, 1] = value
        K_λ[3, 1, 1, 1, 2, 1, 2, 1] = value
        K_λ[3, 1, 1, 1, 2, 1, 2, 2] = value
        K_λ[3, 1, 1, 2, 2, 1, 1, 1] = value
        K_λ[3, 1, 1, 2, 2, 1, 1, 2] = value
        K_λ[3, 1, 2, 1, 2, 1, 2, 1] = value
        K_λ[3, 1, 2, 1, 2, 1, 2, 2] = value
        K_λ[3, 1, 2, 2, 2, 1, 1, 1] = value
        K_λ[3, 1, 2, 2, 2, 1, 1, 2] = value
        K_λ[3, 2, 1, 1, 2, 2, 2, 1] = value
        K_λ[3, 2, 1, 1, 2, 2, 2, 2] = value
        K_λ[3, 2, 1, 2, 2, 2, 1, 1] = value
        K_λ[3, 2, 1, 2, 2, 2, 1, 2] = value
        K_λ[3, 2, 2, 1, 2, 2, 2, 1] = value
        K_λ[3, 2, 2, 1, 2, 2, 2, 2] = value
        K_λ[3, 2, 2, 2, 2, 2, 1, 1] = value
        K_λ[3, 2, 2, 2, 2, 2, 1, 2] = value

        value = -h[1] / 24
        K_λ[2, 1, 1, 1, 3, 2, 1, 2] = value
        K_λ[2, 1, 1, 1, 3, 2, 2, 2] = value
        K_λ[2, 1, 1, 2, 3, 2, 1, 2] = value
        K_λ[2, 1, 1, 2, 3, 2, 2, 2] = value
        K_λ[2, 1, 2, 1, 3, 2, 1, 1] = value
        K_λ[2, 1, 2, 1, 3, 2, 2, 1] = value
        K_λ[2, 1, 2, 2, 3, 2, 1, 1] = value
        K_λ[2, 1, 2, 2, 3, 2, 2, 1] = value
        K_λ[2, 2, 1, 1, 3, 1, 1, 2] = value
        K_λ[2, 2, 1, 1, 3, 1, 2, 2] = value
        K_λ[2, 2, 1, 2, 3, 1, 1, 2] = value
        K_λ[2, 2, 1, 2, 3, 1, 2, 2] = value
        K_λ[2, 2, 2, 1, 3, 1, 1, 1] = value
        K_λ[2, 2, 2, 1, 3, 1, 2, 1] = value
        K_λ[2, 2, 2, 2, 3, 1, 1, 1] = value
        K_λ[2, 2, 2, 2, 3, 1, 2, 1] = value
        K_λ[3, 1, 1, 1, 2, 2, 2, 1] = value
        K_λ[3, 1, 1, 1, 2, 2, 2, 2] = value
        K_λ[3, 1, 1, 2, 2, 2, 1, 1] = value
        K_λ[3, 1, 1, 2, 2, 2, 1, 2] = value
        K_λ[3, 1, 2, 1, 2, 2, 2, 1] = value
        K_λ[3, 1, 2, 1, 2, 2, 2, 2] = value
        K_λ[3, 1, 2, 2, 2, 2, 1, 1] = value
        K_λ[3, 1, 2, 2, 2, 2, 1, 2] = value
        K_λ[3, 2, 1, 1, 2, 1, 2, 1] = value
        K_λ[3, 2, 1, 1, 2, 1, 2, 2] = value
        K_λ[3, 2, 1, 2, 2, 1, 1, 1] = value
        K_λ[3, 2, 1, 2, 2, 1, 1, 2] = value
        K_λ[3, 2, 2, 1, 2, 1, 2, 1] = value
        K_λ[3, 2, 2, 1, 2, 1, 2, 2] = value
        K_λ[3, 2, 2, 2, 2, 1, 1, 1] = value
        K_λ[3, 2, 2, 2, 2, 1, 1, 2] = value

        value = h[1] / 24
        K_λ[2, 1, 1, 1, 3, 2, 1, 1] = value
        K_λ[2, 1, 1, 1, 3, 2, 2, 1] = value
        K_λ[2, 1, 1, 2, 3, 2, 1, 1] = value
        K_λ[2, 1, 1, 2, 3, 2, 2, 1] = value
        K_λ[2, 1, 2, 1, 3, 2, 1, 2] = value
        K_λ[2, 1, 2, 1, 3, 2, 2, 2] = value
        K_λ[2, 1, 2, 2, 3, 2, 1, 2] = value
        K_λ[2, 1, 2, 2, 3, 2, 2, 2] = value
        K_λ[2, 2, 1, 1, 3, 1, 1, 1] = value
        K_λ[2, 2, 1, 1, 3, 1, 2, 1] = value
        K_λ[2, 2, 1, 2, 3, 1, 1, 1] = value
        K_λ[2, 2, 1, 2, 3, 1, 2, 1] = value
        K_λ[2, 2, 2, 1, 3, 1, 1, 2] = value
        K_λ[2, 2, 2, 1, 3, 1, 2, 2] = value
        K_λ[2, 2, 2, 2, 3, 1, 1, 2] = value
        K_λ[2, 2, 2, 2, 3, 1, 2, 2] = value
        K_λ[3, 1, 1, 1, 2, 2, 1, 1] = value
        K_λ[3, 1, 1, 1, 2, 2, 1, 2] = value
        K_λ[3, 1, 1, 2, 2, 2, 2, 1] = value
        K_λ[3, 1, 1, 2, 2, 2, 2, 2] = value
        K_λ[3, 1, 2, 1, 2, 2, 1, 1] = value
        K_λ[3, 1, 2, 1, 2, 2, 1, 2] = value
        K_λ[3, 1, 2, 2, 2, 2, 2, 1] = value
        K_λ[3, 1, 2, 2, 2, 2, 2, 2] = value
        K_λ[3, 2, 1, 1, 2, 1, 1, 1] = value
        K_λ[3, 2, 1, 1, 2, 1, 1, 2] = value
        K_λ[3, 2, 1, 2, 2, 1, 2, 1] = value
        K_λ[3, 2, 1, 2, 2, 1, 2, 2] = value
        K_λ[3, 2, 2, 1, 2, 1, 1, 1] = value
        K_λ[3, 2, 2, 1, 2, 1, 1, 2] = value
        K_λ[3, 2, 2, 2, 2, 1, 2, 1] = value
        K_λ[3, 2, 2, 2, 2, 1, 2, 2] = value

        value = h[1] / 12
        K_λ[2, 1, 1, 1, 3, 1, 1, 1] = value
        K_λ[2, 1, 1, 1, 3, 1, 2, 1] = value
        K_λ[2, 1, 1, 2, 3, 1, 1, 1] = value
        K_λ[2, 1, 1, 2, 3, 1, 2, 1] = value
        K_λ[2, 1, 2, 1, 3, 1, 1, 2] = value
        K_λ[2, 1, 2, 1, 3, 1, 2, 2] = value
        K_λ[2, 1, 2, 2, 3, 1, 1, 2] = value
        K_λ[2, 1, 2, 2, 3, 1, 2, 2] = value
        K_λ[2, 2, 1, 1, 3, 2, 1, 1] = value
        K_λ[2, 2, 1, 1, 3, 2, 2, 1] = value
        K_λ[2, 2, 1, 2, 3, 2, 1, 1] = value
        K_λ[2, 2, 1, 2, 3, 2, 2, 1] = value
        K_λ[2, 2, 2, 1, 3, 2, 1, 2] = value
        K_λ[2, 2, 2, 1, 3, 2, 2, 2] = value
        K_λ[2, 2, 2, 2, 3, 2, 1, 2] = value
        K_λ[2, 2, 2, 2, 3, 2, 2, 2] = value
        K_λ[3, 1, 1, 1, 2, 1, 1, 1] = value
        K_λ[3, 1, 1, 1, 2, 1, 1, 2] = value
        K_λ[3, 1, 1, 2, 2, 1, 2, 1] = value
        K_λ[3, 1, 1, 2, 2, 1, 2, 2] = value
        K_λ[3, 1, 2, 1, 2, 1, 1, 1] = value
        K_λ[3, 1, 2, 1, 2, 1, 1, 2] = value
        K_λ[3, 1, 2, 2, 2, 1, 2, 1] = value
        K_λ[3, 1, 2, 2, 2, 1, 2, 2] = value
        K_λ[3, 2, 1, 1, 2, 2, 1, 1] = value
        K_λ[3, 2, 1, 1, 2, 2, 1, 2] = value
        K_λ[3, 2, 1, 2, 2, 2, 2, 1] = value
        K_λ[3, 2, 1, 2, 2, 2, 2, 2] = value
        K_λ[3, 2, 2, 1, 2, 2, 1, 1] = value
        K_λ[3, 2, 2, 1, 2, 2, 1, 2] = value
        K_λ[3, 2, 2, 2, 2, 2, 2, 1] = value
        K_λ[3, 2, 2, 2, 2, 2, 2, 2] = value

        value = -h[2] / 12
        K_λ[1, 1, 1, 1, 3, 1, 1, 2] = value
        K_λ[1, 1, 1, 1, 3, 2, 1, 2] = value
        K_λ[1, 1, 1, 2, 3, 1, 1, 2] = value
        K_λ[1, 1, 1, 2, 3, 2, 1, 2] = value
        K_λ[1, 1, 2, 1, 3, 1, 2, 2] = value
        K_λ[1, 1, 2, 1, 3, 2, 2, 2] = value
        K_λ[1, 1, 2, 2, 3, 1, 2, 2] = value
        K_λ[1, 1, 2, 2, 3, 2, 2, 2] = value
        K_λ[1, 2, 1, 1, 3, 1, 1, 1] = value
        K_λ[1, 2, 1, 1, 3, 2, 1, 1] = value
        K_λ[1, 2, 1, 2, 3, 1, 1, 1] = value
        K_λ[1, 2, 1, 2, 3, 2, 1, 1] = value
        K_λ[1, 2, 2, 1, 3, 1, 2, 1] = value
        K_λ[1, 2, 2, 1, 3, 2, 2, 1] = value
        K_λ[1, 2, 2, 2, 3, 1, 2, 1] = value
        K_λ[1, 2, 2, 2, 3, 2, 2, 1] = value
        K_λ[3, 1, 1, 1, 1, 2, 1, 1] = value
        K_λ[3, 1, 1, 1, 1, 2, 1, 2] = value
        K_λ[3, 1, 1, 2, 1, 1, 1, 1] = value
        K_λ[3, 1, 1, 2, 1, 1, 1, 2] = value
        K_λ[3, 1, 2, 1, 1, 2, 2, 1] = value
        K_λ[3, 1, 2, 1, 1, 2, 2, 2] = value
        K_λ[3, 1, 2, 2, 1, 1, 2, 1] = value
        K_λ[3, 1, 2, 2, 1, 1, 2, 2] = value
        K_λ[3, 2, 1, 1, 1, 2, 1, 1] = value
        K_λ[3, 2, 1, 1, 1, 2, 1, 2] = value
        K_λ[3, 2, 1, 2, 1, 1, 1, 1] = value
        K_λ[3, 2, 1, 2, 1, 1, 1, 2] = value
        K_λ[3, 2, 2, 1, 1, 2, 2, 1] = value
        K_λ[3, 2, 2, 1, 1, 2, 2, 2] = value
        K_λ[3, 2, 2, 2, 1, 1, 2, 1] = value
        K_λ[3, 2, 2, 2, 1, 1, 2, 2] = value

        value = -h[2] / 24
        K_λ[1, 1, 1, 1, 3, 1, 2, 2] = value
        K_λ[1, 1, 1, 1, 3, 2, 2, 2] = value
        K_λ[1, 1, 1, 2, 3, 1, 2, 2] = value
        K_λ[1, 1, 1, 2, 3, 2, 2, 2] = value
        K_λ[1, 1, 2, 1, 3, 1, 1, 2] = value
        K_λ[1, 1, 2, 1, 3, 2, 1, 2] = value
        K_λ[1, 1, 2, 2, 3, 1, 1, 2] = value
        K_λ[1, 1, 2, 2, 3, 2, 1, 2] = value
        K_λ[1, 2, 1, 1, 3, 1, 2, 1] = value
        K_λ[1, 2, 1, 1, 3, 2, 2, 1] = value
        K_λ[1, 2, 1, 2, 3, 1, 2, 1] = value
        K_λ[1, 2, 1, 2, 3, 2, 2, 1] = value
        K_λ[1, 2, 2, 1, 3, 1, 1, 1] = value
        K_λ[1, 2, 2, 1, 3, 2, 1, 1] = value
        K_λ[1, 2, 2, 2, 3, 1, 1, 1] = value
        K_λ[1, 2, 2, 2, 3, 2, 1, 1] = value
        K_λ[3, 1, 1, 1, 1, 2, 2, 1] = value
        K_λ[3, 1, 1, 1, 1, 2, 2, 2] = value
        K_λ[3, 1, 1, 2, 1, 1, 2, 1] = value
        K_λ[3, 1, 1, 2, 1, 1, 2, 2] = value
        K_λ[3, 1, 2, 1, 1, 2, 1, 1] = value
        K_λ[3, 1, 2, 1, 1, 2, 1, 2] = value
        K_λ[3, 1, 2, 2, 1, 1, 1, 1] = value
        K_λ[3, 1, 2, 2, 1, 1, 1, 2] = value
        K_λ[3, 2, 1, 1, 1, 2, 2, 1] = value
        K_λ[3, 2, 1, 1, 1, 2, 2, 2] = value
        K_λ[3, 2, 1, 2, 1, 1, 2, 1] = value
        K_λ[3, 2, 1, 2, 1, 1, 2, 2] = value
        K_λ[3, 2, 2, 1, 1, 2, 1, 1] = value
        K_λ[3, 2, 2, 1, 1, 2, 1, 2] = value
        K_λ[3, 2, 2, 2, 1, 1, 1, 1] = value
        K_λ[3, 2, 2, 2, 1, 1, 1, 2] = value

        value = h[2] / 24
        K_λ[1, 1, 1, 1, 3, 1, 2, 1] = value
        K_λ[1, 1, 1, 1, 3, 2, 2, 1] = value
        K_λ[1, 1, 1, 2, 3, 1, 2, 1] = value
        K_λ[1, 1, 1, 2, 3, 2, 2, 1] = value
        K_λ[1, 1, 2, 1, 3, 1, 1, 1] = value
        K_λ[1, 1, 2, 1, 3, 2, 1, 1] = value
        K_λ[1, 1, 2, 2, 3, 1, 1, 1] = value
        K_λ[1, 1, 2, 2, 3, 2, 1, 1] = value
        K_λ[1, 2, 1, 1, 3, 1, 2, 2] = value
        K_λ[1, 2, 1, 1, 3, 2, 2, 2] = value
        K_λ[1, 2, 1, 2, 3, 1, 2, 2] = value
        K_λ[1, 2, 1, 2, 3, 2, 2, 2] = value
        K_λ[1, 2, 2, 1, 3, 1, 1, 2] = value
        K_λ[1, 2, 2, 1, 3, 2, 1, 2] = value
        K_λ[1, 2, 2, 2, 3, 1, 1, 2] = value
        K_λ[1, 2, 2, 2, 3, 2, 1, 2] = value
        K_λ[3, 1, 1, 1, 1, 1, 2, 1] = value
        K_λ[3, 1, 1, 1, 1, 1, 2, 2] = value
        K_λ[3, 1, 1, 2, 1, 2, 2, 1] = value
        K_λ[3, 1, 1, 2, 1, 2, 2, 2] = value
        K_λ[3, 1, 2, 1, 1, 1, 1, 1] = value
        K_λ[3, 1, 2, 1, 1, 1, 1, 2] = value
        K_λ[3, 1, 2, 2, 1, 2, 1, 1] = value
        K_λ[3, 1, 2, 2, 1, 2, 1, 2] = value
        K_λ[3, 2, 1, 1, 1, 1, 2, 1] = value
        K_λ[3, 2, 1, 1, 1, 1, 2, 2] = value
        K_λ[3, 2, 1, 2, 1, 2, 2, 1] = value
        K_λ[3, 2, 1, 2, 1, 2, 2, 2] = value
        K_λ[3, 2, 2, 1, 1, 1, 1, 1] = value
        K_λ[3, 2, 2, 1, 1, 1, 1, 2] = value
        K_λ[3, 2, 2, 2, 1, 2, 1, 1] = value
        K_λ[3, 2, 2, 2, 1, 2, 1, 2] = value

        value = h[2] / 12
        K_λ[1, 1, 1, 1, 3, 1, 1, 1] = value
        K_λ[1, 1, 1, 1, 3, 2, 1, 1] = value
        K_λ[1, 1, 1, 2, 3, 1, 1, 1] = value
        K_λ[1, 1, 1, 2, 3, 2, 1, 1] = value
        K_λ[1, 1, 2, 1, 3, 1, 2, 1] = value
        K_λ[1, 1, 2, 1, 3, 2, 2, 1] = value
        K_λ[1, 1, 2, 2, 3, 1, 2, 1] = value
        K_λ[1, 1, 2, 2, 3, 2, 2, 1] = value
        K_λ[1, 2, 1, 1, 3, 1, 1, 2] = value
        K_λ[1, 2, 1, 1, 3, 2, 1, 2] = value
        K_λ[1, 2, 1, 2, 3, 1, 1, 2] = value
        K_λ[1, 2, 1, 2, 3, 2, 1, 2] = value
        K_λ[1, 2, 2, 1, 3, 1, 2, 2] = value
        K_λ[1, 2, 2, 1, 3, 2, 2, 2] = value
        K_λ[1, 2, 2, 2, 3, 1, 2, 2] = value
        K_λ[1, 2, 2, 2, 3, 2, 2, 2] = value
        K_λ[3, 1, 1, 1, 1, 1, 1, 1] = value
        K_λ[3, 1, 1, 1, 1, 1, 1, 2] = value
        K_λ[3, 1, 1, 2, 1, 2, 1, 1] = value
        K_λ[3, 1, 1, 2, 1, 2, 1, 2] = value
        K_λ[3, 1, 2, 1, 1, 1, 2, 1] = value
        K_λ[3, 1, 2, 1, 1, 1, 2, 2] = value
        K_λ[3, 1, 2, 2, 1, 2, 2, 1] = value
        K_λ[3, 1, 2, 2, 1, 2, 2, 2] = value
        K_λ[3, 2, 1, 1, 1, 1, 1, 1] = value
        K_λ[3, 2, 1, 1, 1, 1, 1, 2] = value
        K_λ[3, 2, 1, 2, 1, 2, 1, 1] = value
        K_λ[3, 2, 1, 2, 1, 2, 1, 2] = value
        K_λ[3, 2, 2, 1, 1, 1, 2, 1] = value
        K_λ[3, 2, 2, 1, 1, 1, 2, 2] = value
        K_λ[3, 2, 2, 2, 1, 2, 2, 1] = value
        K_λ[3, 2, 2, 2, 1, 2, 2, 2] = value

        value = -(h[1] * h[2]) / (9 * h[3])
        K_λ[3, 1, 1, 1, 3, 1, 1, 2] = value
        K_λ[3, 1, 1, 2, 3, 1, 1, 1] = value
        K_λ[3, 1, 2, 1, 3, 1, 2, 2] = value
        K_λ[3, 1, 2, 2, 3, 1, 2, 1] = value
        K_λ[3, 2, 1, 1, 3, 2, 1, 2] = value
        K_λ[3, 2, 1, 2, 3, 2, 1, 1] = value
        K_λ[3, 2, 2, 1, 3, 2, 2, 2] = value
        K_λ[3, 2, 2, 2, 3, 2, 2, 1] = value

        value = -(h[1] * h[2]) / (18 * h[3])
        K_λ[3, 1, 1, 1, 3, 1, 2, 2] = value
        K_λ[3, 1, 1, 1, 3, 2, 1, 2] = value
        K_λ[3, 1, 1, 2, 3, 1, 2, 1] = value
        K_λ[3, 1, 1, 2, 3, 2, 1, 1] = value
        K_λ[3, 1, 2, 1, 3, 1, 1, 2] = value
        K_λ[3, 1, 2, 1, 3, 2, 2, 2] = value
        K_λ[3, 1, 2, 2, 3, 1, 1, 1] = value
        K_λ[3, 1, 2, 2, 3, 2, 2, 1] = value
        K_λ[3, 2, 1, 1, 3, 1, 1, 2] = value
        K_λ[3, 2, 1, 1, 3, 2, 2, 2] = value
        K_λ[3, 2, 1, 2, 3, 1, 1, 1] = value
        K_λ[3, 2, 1, 2, 3, 2, 2, 1] = value
        K_λ[3, 2, 2, 1, 3, 1, 2, 2] = value
        K_λ[3, 2, 2, 1, 3, 2, 1, 2] = value
        K_λ[3, 2, 2, 2, 3, 1, 2, 1] = value
        K_λ[3, 2, 2, 2, 3, 2, 1, 1] = value

        value = -(h[1] * h[2]) / (36 * h[3])
        K_λ[3, 1, 1, 1, 3, 2, 2, 2] = value
        K_λ[3, 1, 1, 2, 3, 2, 2, 1] = value
        K_λ[3, 1, 2, 1, 3, 2, 1, 2] = value
        K_λ[3, 1, 2, 2, 3, 2, 1, 1] = value
        K_λ[3, 2, 1, 1, 3, 1, 2, 2] = value
        K_λ[3, 2, 1, 2, 3, 1, 2, 1] = value
        K_λ[3, 2, 2, 1, 3, 1, 1, 2] = value
        K_λ[3, 2, 2, 2, 3, 1, 1, 1] = value

        value = (h[1] * h[2]) / (36 * h[3])
        K_λ[3, 1, 1, 1, 3, 2, 2, 1] = value
        K_λ[3, 1, 1, 2, 3, 2, 2, 2] = value
        K_λ[3, 1, 2, 1, 3, 2, 1, 1] = value
        K_λ[3, 1, 2, 2, 3, 2, 1, 2] = value
        K_λ[3, 2, 1, 1, 3, 1, 2, 1] = value
        K_λ[3, 2, 1, 2, 3, 1, 2, 2] = value
        K_λ[3, 2, 2, 1, 3, 1, 1, 1] = value
        K_λ[3, 2, 2, 2, 3, 1, 1, 2] = value

        value = (h[1] * h[2]) / (18 * h[3])
        K_λ[3, 1, 1, 1, 3, 1, 2, 1] = value
        K_λ[3, 1, 1, 1, 3, 2, 1, 1] = value
        K_λ[3, 1, 1, 2, 3, 1, 2, 2] = value
        K_λ[3, 1, 1, 2, 3, 2, 1, 2] = value
        K_λ[3, 1, 2, 1, 3, 1, 1, 1] = value
        K_λ[3, 1, 2, 1, 3, 2, 2, 1] = value
        K_λ[3, 1, 2, 2, 3, 1, 1, 2] = value
        K_λ[3, 1, 2, 2, 3, 2, 2, 2] = value
        K_λ[3, 2, 1, 1, 3, 1, 1, 1] = value
        K_λ[3, 2, 1, 1, 3, 2, 2, 1] = value
        K_λ[3, 2, 1, 2, 3, 1, 1, 2] = value
        K_λ[3, 2, 1, 2, 3, 2, 2, 2] = value
        K_λ[3, 2, 2, 1, 3, 1, 2, 1] = value
        K_λ[3, 2, 2, 1, 3, 2, 1, 1] = value
        K_λ[3, 2, 2, 2, 3, 1, 2, 2] = value
        K_λ[3, 2, 2, 2, 3, 2, 1, 2] = value

        value = (h[1] * h[2]) / (9 * h[3])
        K_λ[3, 1, 1, 1, 3, 1, 1, 1] = value
        K_λ[3, 1, 1, 2, 3, 1, 1, 2] = value
        K_λ[3, 1, 2, 1, 3, 1, 2, 1] = value
        K_λ[3, 1, 2, 2, 3, 1, 2, 2] = value
        K_λ[3, 2, 1, 1, 3, 2, 1, 1] = value
        K_λ[3, 2, 1, 2, 3, 2, 1, 2] = value
        K_λ[3, 2, 2, 1, 3, 2, 2, 1] = value
        K_λ[3, 2, 2, 2, 3, 2, 2, 2] = value

        value = -h[3] / 12
        K_λ[1, 1, 1, 1, 2, 1, 2, 1] = value
        K_λ[1, 1, 1, 1, 2, 2, 2, 1] = value
        K_λ[1, 1, 1, 2, 2, 1, 2, 2] = value
        K_λ[1, 1, 1, 2, 2, 2, 2, 2] = value
        K_λ[1, 1, 2, 1, 2, 1, 2, 1] = value
        K_λ[1, 1, 2, 1, 2, 2, 2, 1] = value
        K_λ[1, 1, 2, 2, 2, 1, 2, 2] = value
        K_λ[1, 1, 2, 2, 2, 2, 2, 2] = value
        K_λ[1, 2, 1, 1, 2, 1, 1, 1] = value
        K_λ[1, 2, 1, 1, 2, 2, 1, 1] = value
        K_λ[1, 2, 1, 2, 2, 1, 1, 2] = value
        K_λ[1, 2, 1, 2, 2, 2, 1, 2] = value
        K_λ[1, 2, 2, 1, 2, 1, 1, 1] = value
        K_λ[1, 2, 2, 1, 2, 2, 1, 1] = value
        K_λ[1, 2, 2, 2, 2, 1, 1, 2] = value
        K_λ[1, 2, 2, 2, 2, 2, 1, 2] = value
        K_λ[2, 1, 1, 1, 1, 2, 1, 1] = value
        K_λ[2, 1, 1, 1, 1, 2, 2, 1] = value
        K_λ[2, 1, 1, 2, 1, 2, 1, 2] = value
        K_λ[2, 1, 1, 2, 1, 2, 2, 2] = value
        K_λ[2, 1, 2, 1, 1, 1, 1, 1] = value
        K_λ[2, 1, 2, 1, 1, 1, 2, 1] = value
        K_λ[2, 1, 2, 2, 1, 1, 1, 2] = value
        K_λ[2, 1, 2, 2, 1, 1, 2, 2] = value
        K_λ[2, 2, 1, 1, 1, 2, 1, 1] = value
        K_λ[2, 2, 1, 1, 1, 2, 2, 1] = value
        K_λ[2, 2, 1, 2, 1, 2, 1, 2] = value
        K_λ[2, 2, 1, 2, 1, 2, 2, 2] = value
        K_λ[2, 2, 2, 1, 1, 1, 1, 1] = value
        K_λ[2, 2, 2, 1, 1, 1, 2, 1] = value
        K_λ[2, 2, 2, 2, 1, 1, 1, 2] = value
        K_λ[2, 2, 2, 2, 1, 1, 2, 2] = value

        value = -h[3] / 24
        K_λ[1, 1, 1, 1, 2, 1, 2, 2] = value
        K_λ[1, 1, 1, 1, 2, 2, 2, 2] = value
        K_λ[1, 1, 1, 2, 2, 1, 2, 1] = value
        K_λ[1, 1, 1, 2, 2, 2, 2, 1] = value
        K_λ[1, 1, 2, 1, 2, 1, 2, 2] = value
        K_λ[1, 1, 2, 1, 2, 2, 2, 2] = value
        K_λ[1, 1, 2, 2, 2, 1, 2, 1] = value
        K_λ[1, 1, 2, 2, 2, 2, 2, 1] = value
        K_λ[1, 2, 1, 1, 2, 1, 1, 2] = value
        K_λ[1, 2, 1, 1, 2, 2, 1, 2] = value
        K_λ[1, 2, 1, 2, 2, 1, 1, 1] = value
        K_λ[1, 2, 1, 2, 2, 2, 1, 1] = value
        K_λ[1, 2, 2, 1, 2, 1, 1, 2] = value
        K_λ[1, 2, 2, 1, 2, 2, 1, 2] = value
        K_λ[1, 2, 2, 2, 2, 1, 1, 1] = value
        K_λ[1, 2, 2, 2, 2, 2, 1, 1] = value
        K_λ[2, 1, 1, 1, 1, 2, 1, 2] = value
        K_λ[2, 1, 1, 1, 1, 2, 2, 2] = value
        K_λ[2, 1, 1, 2, 1, 2, 1, 1] = value
        K_λ[2, 1, 1, 2, 1, 2, 2, 1] = value
        K_λ[2, 1, 2, 1, 1, 1, 1, 2] = value
        K_λ[2, 1, 2, 1, 1, 1, 2, 2] = value
        K_λ[2, 1, 2, 2, 1, 1, 1, 1] = value
        K_λ[2, 1, 2, 2, 1, 1, 2, 1] = value
        K_λ[2, 2, 1, 1, 1, 2, 1, 2] = value
        K_λ[2, 2, 1, 1, 1, 2, 2, 2] = value
        K_λ[2, 2, 1, 2, 1, 2, 1, 1] = value
        K_λ[2, 2, 1, 2, 1, 2, 2, 1] = value
        K_λ[2, 2, 2, 1, 1, 1, 1, 2] = value
        K_λ[2, 2, 2, 1, 1, 1, 2, 2] = value
        K_λ[2, 2, 2, 2, 1, 1, 1, 1] = value
        K_λ[2, 2, 2, 2, 1, 1, 2, 1] = value

        value = h[3] / 24
        K_λ[1, 1, 1, 1, 2, 1, 1, 2] = value
        K_λ[1, 1, 1, 1, 2, 2, 1, 2] = value
        K_λ[1, 1, 1, 2, 2, 1, 1, 1] = value
        K_λ[1, 1, 1, 2, 2, 2, 1, 1] = value
        K_λ[1, 1, 2, 1, 2, 1, 1, 2] = value
        K_λ[1, 1, 2, 1, 2, 2, 1, 2] = value
        K_λ[1, 1, 2, 2, 2, 1, 1, 1] = value
        K_λ[1, 1, 2, 2, 2, 2, 1, 1] = value
        K_λ[1, 2, 1, 1, 2, 1, 2, 2] = value
        K_λ[1, 2, 1, 1, 2, 2, 2, 2] = value
        K_λ[1, 2, 1, 2, 2, 1, 2, 1] = value
        K_λ[1, 2, 1, 2, 2, 2, 2, 1] = value
        K_λ[1, 2, 2, 1, 2, 1, 2, 2] = value
        K_λ[1, 2, 2, 1, 2, 2, 2, 2] = value
        K_λ[1, 2, 2, 2, 2, 1, 2, 1] = value
        K_λ[1, 2, 2, 2, 2, 2, 2, 1] = value
        K_λ[2, 1, 1, 1, 1, 1, 1, 2] = value
        K_λ[2, 1, 1, 1, 1, 1, 2, 2] = value
        K_λ[2, 1, 1, 2, 1, 1, 1, 1] = value
        K_λ[2, 1, 1, 2, 1, 1, 2, 1] = value
        K_λ[2, 1, 2, 1, 1, 2, 1, 2] = value
        K_λ[2, 1, 2, 1, 1, 2, 2, 2] = value
        K_λ[2, 1, 2, 2, 1, 2, 1, 1] = value
        K_λ[2, 1, 2, 2, 1, 2, 2, 1] = value
        K_λ[2, 2, 1, 1, 1, 1, 1, 2] = value
        K_λ[2, 2, 1, 1, 1, 1, 2, 2] = value
        K_λ[2, 2, 1, 2, 1, 1, 1, 1] = value
        K_λ[2, 2, 1, 2, 1, 1, 2, 1] = value
        K_λ[2, 2, 2, 1, 1, 2, 1, 2] = value
        K_λ[2, 2, 2, 1, 1, 2, 2, 2] = value
        K_λ[2, 2, 2, 2, 1, 2, 1, 1] = value
        K_λ[2, 2, 2, 2, 1, 2, 2, 1] = value

        value = h[3] / 12
        K_λ[1, 1, 1, 1, 2, 1, 1, 1] = value
        K_λ[1, 1, 1, 1, 2, 2, 1, 1] = value
        K_λ[1, 1, 1, 2, 2, 1, 1, 2] = value
        K_λ[1, 1, 1, 2, 2, 2, 1, 2] = value
        K_λ[1, 1, 2, 1, 2, 1, 1, 1] = value
        K_λ[1, 1, 2, 1, 2, 2, 1, 1] = value
        K_λ[1, 1, 2, 2, 2, 1, 1, 2] = value
        K_λ[1, 1, 2, 2, 2, 2, 1, 2] = value
        K_λ[1, 2, 1, 1, 2, 1, 2, 1] = value
        K_λ[1, 2, 1, 1, 2, 2, 2, 1] = value
        K_λ[1, 2, 1, 2, 2, 1, 2, 2] = value
        K_λ[1, 2, 1, 2, 2, 2, 2, 2] = value
        K_λ[1, 2, 2, 1, 2, 1, 2, 1] = value
        K_λ[1, 2, 2, 1, 2, 2, 2, 1] = value
        K_λ[1, 2, 2, 2, 2, 1, 2, 2] = value
        K_λ[1, 2, 2, 2, 2, 2, 2, 2] = value
        K_λ[2, 1, 1, 1, 1, 1, 1, 1] = value
        K_λ[2, 1, 1, 1, 1, 1, 2, 1] = value
        K_λ[2, 1, 1, 2, 1, 1, 1, 2] = value
        K_λ[2, 1, 1, 2, 1, 1, 2, 2] = value
        K_λ[2, 1, 2, 1, 1, 2, 1, 1] = value
        K_λ[2, 1, 2, 1, 1, 2, 2, 1] = value
        K_λ[2, 1, 2, 2, 1, 2, 1, 2] = value
        K_λ[2, 1, 2, 2, 1, 2, 2, 2] = value
        K_λ[2, 2, 1, 1, 1, 1, 1, 1] = value
        K_λ[2, 2, 1, 1, 1, 1, 2, 1] = value
        K_λ[2, 2, 1, 2, 1, 1, 1, 2] = value
        K_λ[2, 2, 1, 2, 1, 1, 2, 2] = value
        K_λ[2, 2, 2, 1, 1, 2, 1, 1] = value
        K_λ[2, 2, 2, 1, 1, 2, 2, 1] = value
        K_λ[2, 2, 2, 2, 1, 2, 1, 2] = value
        K_λ[2, 2, 2, 2, 1, 2, 2, 2] = value

        value = -(h[1] * h[3]) / (9 * h[2])
        K_λ[2, 1, 1, 1, 2, 1, 2, 1] = value
        K_λ[2, 1, 1, 2, 2, 1, 2, 2] = value
        K_λ[2, 1, 2, 1, 2, 1, 1, 1] = value
        K_λ[2, 1, 2, 2, 2, 1, 1, 2] = value
        K_λ[2, 2, 1, 1, 2, 2, 2, 1] = value
        K_λ[2, 2, 1, 2, 2, 2, 2, 2] = value
        K_λ[2, 2, 2, 1, 2, 2, 1, 1] = value
        K_λ[2, 2, 2, 2, 2, 2, 1, 2] = value

        value = -(h[1] * h[3]) / (18 * h[2])
        K_λ[2, 1, 1, 1, 2, 1, 2, 2] = value
        K_λ[2, 1, 1, 1, 2, 2, 2, 1] = value
        K_λ[2, 1, 1, 2, 2, 1, 2, 1] = value
        K_λ[2, 1, 1, 2, 2, 2, 2, 2] = value
        K_λ[2, 1, 2, 1, 2, 1, 1, 2] = value
        K_λ[2, 1, 2, 1, 2, 2, 1, 1] = value
        K_λ[2, 1, 2, 2, 2, 1, 1, 1] = value
        K_λ[2, 1, 2, 2, 2, 2, 1, 2] = value
        K_λ[2, 2, 1, 1, 2, 1, 2, 1] = value
        K_λ[2, 2, 1, 1, 2, 2, 2, 2] = value
        K_λ[2, 2, 1, 2, 2, 1, 2, 2] = value
        K_λ[2, 2, 1, 2, 2, 2, 2, 1] = value
        K_λ[2, 2, 2, 1, 2, 1, 1, 1] = value
        K_λ[2, 2, 2, 1, 2, 2, 1, 2] = value
        K_λ[2, 2, 2, 2, 2, 1, 1, 2] = value
        K_λ[2, 2, 2, 2, 2, 2, 1, 1] = value

        value = -(h[1] * h[3]) / (36 * h[2])
        K_λ[2, 1, 1, 1, 2, 2, 2, 2] = value
        K_λ[2, 1, 1, 2, 2, 2, 2, 1] = value
        K_λ[2, 1, 2, 1, 2, 2, 1, 2] = value
        K_λ[2, 1, 2, 2, 2, 2, 1, 1] = value
        K_λ[2, 2, 1, 1, 2, 1, 2, 2] = value
        K_λ[2, 2, 1, 2, 2, 1, 2, 1] = value
        K_λ[2, 2, 2, 1, 2, 1, 1, 2] = value
        K_λ[2, 2, 2, 2, 2, 1, 1, 1] = value

        value = (h[1] * h[3]) / (36 * h[2])
        K_λ[2, 1, 1, 1, 2, 2, 1, 2] = value
        K_λ[2, 1, 1, 2, 2, 2, 1, 1] = value
        K_λ[2, 1, 2, 1, 2, 2, 2, 2] = value
        K_λ[2, 1, 2, 2, 2, 2, 2, 1] = value
        K_λ[2, 2, 1, 1, 2, 1, 1, 2] = value
        K_λ[2, 2, 1, 2, 2, 1, 1, 1] = value
        K_λ[2, 2, 2, 1, 2, 1, 2, 2] = value
        K_λ[2, 2, 2, 2, 2, 1, 2, 1] = value

        value = (h[1] * h[3]) / (18 * h[2])
        K_λ[2, 1, 1, 1, 2, 1, 1, 2] = value
        K_λ[2, 1, 1, 1, 2, 2, 1, 1] = value
        K_λ[2, 1, 1, 2, 2, 1, 1, 1] = value
        K_λ[2, 1, 1, 2, 2, 2, 1, 2] = value
        K_λ[2, 1, 2, 1, 2, 1, 2, 2] = value
        K_λ[2, 1, 2, 1, 2, 2, 2, 1] = value
        K_λ[2, 1, 2, 2, 2, 1, 2, 1] = value
        K_λ[2, 1, 2, 2, 2, 2, 2, 2] = value
        K_λ[2, 2, 1, 1, 2, 1, 1, 1] = value
        K_λ[2, 2, 1, 1, 2, 2, 1, 2] = value
        K_λ[2, 2, 1, 2, 2, 1, 1, 2] = value
        K_λ[2, 2, 1, 2, 2, 2, 1, 1] = value
        K_λ[2, 2, 2, 1, 2, 1, 2, 1] = value
        K_λ[2, 2, 2, 1, 2, 2, 2, 2] = value
        K_λ[2, 2, 2, 2, 2, 1, 2, 2] = value
        K_λ[2, 2, 2, 2, 2, 2, 2, 1] = value

        value = (h[1] * h[3]) / (9 * h[2])
        K_λ[2, 1, 1, 1, 2, 1, 1, 1] = value
        K_λ[2, 1, 1, 2, 2, 1, 1, 2] = value
        K_λ[2, 1, 2, 1, 2, 1, 2, 1] = value
        K_λ[2, 1, 2, 2, 2, 1, 2, 2] = value
        K_λ[2, 2, 1, 1, 2, 2, 1, 1] = value
        K_λ[2, 2, 1, 2, 2, 2, 1, 2] = value
        K_λ[2, 2, 2, 1, 2, 2, 2, 1] = value
        K_λ[2, 2, 2, 2, 2, 2, 2, 2] = value

        value = -(h[2] * h[3]) / (9 * h[1])
        K_λ[1, 1, 1, 1, 1, 2, 1, 1] = value
        K_λ[1, 1, 1, 2, 1, 2, 1, 2] = value
        K_λ[1, 1, 2, 1, 1, 2, 2, 1] = value
        K_λ[1, 1, 2, 2, 1, 2, 2, 2] = value
        K_λ[1, 2, 1, 1, 1, 1, 1, 1] = value
        K_λ[1, 2, 1, 2, 1, 1, 1, 2] = value
        K_λ[1, 2, 2, 1, 1, 1, 2, 1] = value
        K_λ[1, 2, 2, 2, 1, 1, 2, 2] = value

        value = -(h[2] * h[3]) / (18 * h[1])
        K_λ[1, 1, 1, 1, 1, 2, 1, 2] = value
        K_λ[1, 1, 1, 1, 1, 2, 2, 1] = value
        K_λ[1, 1, 1, 2, 1, 2, 1, 1] = value
        K_λ[1, 1, 1, 2, 1, 2, 2, 2] = value
        K_λ[1, 1, 2, 1, 1, 2, 1, 1] = value
        K_λ[1, 1, 2, 1, 1, 2, 2, 2] = value
        K_λ[1, 1, 2, 2, 1, 2, 1, 2] = value
        K_λ[1, 1, 2, 2, 1, 2, 2, 1] = value
        K_λ[1, 2, 1, 1, 1, 1, 1, 2] = value
        K_λ[1, 2, 1, 1, 1, 1, 2, 1] = value
        K_λ[1, 2, 1, 2, 1, 1, 1, 1] = value
        K_λ[1, 2, 1, 2, 1, 1, 2, 2] = value
        K_λ[1, 2, 2, 1, 1, 1, 1, 1] = value
        K_λ[1, 2, 2, 1, 1, 1, 2, 2] = value
        K_λ[1, 2, 2, 2, 1, 1, 1, 2] = value
        K_λ[1, 2, 2, 2, 1, 1, 2, 1] = value

        value = -(h[2] * h[3]) / (36 * h[1])
        K_λ[1, 1, 1, 1, 1, 2, 2, 2] = value
        K_λ[1, 1, 1, 2, 1, 2, 2, 1] = value
        K_λ[1, 1, 2, 1, 1, 2, 1, 2] = value
        K_λ[1, 1, 2, 2, 1, 2, 1, 1] = value
        K_λ[1, 2, 1, 1, 1, 1, 2, 2] = value
        K_λ[1, 2, 1, 2, 1, 1, 2, 1] = value
        K_λ[1, 2, 2, 1, 1, 1, 1, 2] = value
        K_λ[1, 2, 2, 2, 1, 1, 1, 1] = value

        value = (h[2] * h[3]) / (36 * h[1])
        K_λ[1, 1, 1, 1, 1, 1, 2, 2] = value
        K_λ[1, 1, 1, 2, 1, 1, 2, 1] = value
        K_λ[1, 1, 2, 1, 1, 1, 1, 2] = value
        K_λ[1, 1, 2, 2, 1, 1, 1, 1] = value
        K_λ[1, 2, 1, 1, 1, 2, 2, 2] = value
        K_λ[1, 2, 1, 2, 1, 2, 2, 1] = value
        K_λ[1, 2, 2, 1, 1, 2, 1, 2] = value
        K_λ[1, 2, 2, 2, 1, 2, 1, 1] = value

        value = (h[2] * h[3]) / (18 * h[1])
        K_λ[1, 1, 1, 1, 1, 1, 1, 2] = value
        K_λ[1, 1, 1, 1, 1, 1, 2, 1] = value
        K_λ[1, 1, 1, 2, 1, 1, 1, 1] = value
        K_λ[1, 1, 1, 2, 1, 1, 2, 2] = value
        K_λ[1, 1, 2, 1, 1, 1, 1, 1] = value
        K_λ[1, 1, 2, 1, 1, 1, 2, 2] = value
        K_λ[1, 1, 2, 2, 1, 1, 1, 2] = value
        K_λ[1, 1, 2, 2, 1, 1, 2, 1] = value
        K_λ[1, 2, 1, 1, 1, 2, 1, 2] = value
        K_λ[1, 2, 1, 1, 1, 2, 2, 1] = value
        K_λ[1, 2, 1, 2, 1, 2, 1, 1] = value
        K_λ[1, 2, 1, 2, 1, 2, 2, 2] = value
        K_λ[1, 2, 2, 1, 1, 2, 1, 1] = value
        K_λ[1, 2, 2, 1, 1, 2, 2, 2] = value
        K_λ[1, 2, 2, 2, 1, 2, 1, 2] = value
        K_λ[1, 2, 2, 2, 1, 2, 2, 1] = value

        value = (h[2] * h[3]) / (9 * h[1])
        K_λ[1, 1, 1, 1, 1, 1, 1, 1] = value
        K_λ[1, 1, 1, 2, 1, 1, 1, 2] = value
        K_λ[1, 1, 2, 1, 1, 1, 2, 1] = value
        K_λ[1, 1, 2, 2, 1, 1, 2, 2] = value
        K_λ[1, 2, 1, 1, 1, 2, 1, 1] = value
        K_λ[1, 2, 1, 2, 1, 2, 1, 2] = value
        K_λ[1, 2, 2, 1, 1, 2, 2, 1] = value
        K_λ[1, 2, 2, 2, 1, 2, 2, 2] = value
    else
    end
    return K_λ
end

function expected_K_μ(h::NTuple{d,T}) where {d,T<:Number}
    K_μ = zeros(Float64, d, fill(2, d)..., d, fill(2, d)...)
    if d == 2
        value = -1 / 4
        K_μ[1, 1, 1, 2, 2, 1] = value
        K_μ[1, 1, 1, 2, 2, 2] = value
        K_μ[1, 1, 2, 2, 1, 1] = value
        K_μ[1, 1, 2, 2, 1, 2] = value
        K_μ[1, 2, 1, 2, 2, 1] = value
        K_μ[1, 2, 1, 2, 2, 2] = value
        K_μ[1, 2, 2, 2, 1, 1] = value
        K_μ[1, 2, 2, 2, 1, 2] = value
        K_μ[2, 1, 1, 1, 1, 2] = value
        K_μ[2, 1, 1, 1, 2, 2] = value
        K_μ[2, 1, 2, 1, 1, 2] = value
        K_μ[2, 1, 2, 1, 2, 2] = value
        K_μ[2, 2, 1, 1, 1, 1] = value
        K_μ[2, 2, 1, 1, 2, 1] = value
        K_μ[2, 2, 2, 1, 1, 1] = value
        K_μ[2, 2, 2, 1, 2, 1] = value

        value = 1 / 4
        K_μ[1, 1, 1, 2, 1, 1] = value
        K_μ[1, 1, 1, 2, 1, 2] = value
        K_μ[1, 1, 2, 2, 2, 1] = value
        K_μ[1, 1, 2, 2, 2, 2] = value
        K_μ[1, 2, 1, 2, 1, 1] = value
        K_μ[1, 2, 1, 2, 1, 2] = value
        K_μ[1, 2, 2, 2, 2, 1] = value
        K_μ[1, 2, 2, 2, 2, 2] = value
        K_μ[2, 1, 1, 1, 1, 1] = value
        K_μ[2, 1, 1, 1, 2, 1] = value
        K_μ[2, 1, 2, 1, 1, 1] = value
        K_μ[2, 1, 2, 1, 2, 1] = value
        K_μ[2, 2, 1, 1, 1, 2] = value
        K_μ[2, 2, 1, 1, 2, 2] = value
        K_μ[2, 2, 2, 1, 1, 2] = value
        K_μ[2, 2, 2, 1, 2, 2] = value

        value = h[1] / (6 * h[2]) - (2 * h[2]) / (3 * h[1])
        K_μ[1, 1, 1, 1, 2, 1] = value
        K_μ[1, 1, 2, 1, 2, 2] = value
        K_μ[1, 2, 1, 1, 1, 1] = value
        K_μ[1, 2, 2, 1, 1, 2] = value

        value = (-h[2] / (3 * h[1])) - h[1] / (6 * h[2])
        K_μ[1, 1, 1, 1, 2, 2] = value
        K_μ[1, 1, 2, 1, 2, 1] = value
        K_μ[1, 2, 1, 1, 1, 2] = value
        K_μ[1, 2, 2, 1, 1, 1] = value

        value = h[1] / (3 * h[2]) - h[2] / (3 * h[1])
        K_μ[2, 1, 1, 2, 2, 1] = value
        K_μ[2, 1, 2, 2, 2, 2] = value
        K_μ[2, 2, 1, 2, 1, 1] = value
        K_μ[2, 2, 2, 2, 1, 2] = value

        value = (-h[2] / (6 * h[1])) - h[1] / (3 * h[2])
        K_μ[2, 1, 1, 2, 2, 2] = value
        K_μ[2, 1, 2, 2, 2, 1] = value
        K_μ[2, 2, 1, 2, 1, 2] = value
        K_μ[2, 2, 2, 2, 1, 1] = value

        value = h[2] / (6 * h[1]) - (2 * h[1]) / (3 * h[2])
        K_μ[2, 1, 1, 2, 1, 2] = value
        K_μ[2, 1, 2, 2, 1, 1] = value
        K_μ[2, 2, 1, 2, 2, 2] = value
        K_μ[2, 2, 2, 2, 2, 1] = value

        value = h[2] / (3 * h[1]) - h[1] / (3 * h[2])
        K_μ[1, 1, 1, 1, 1, 2] = value
        K_μ[1, 1, 2, 1, 1, 1] = value
        K_μ[1, 2, 1, 1, 2, 2] = value
        K_μ[1, 2, 2, 1, 2, 1] = value

        value = h[2] / (3 * h[1]) + (2 * h[1]) / (3 * h[2])
        K_μ[2, 1, 1, 2, 1, 1] = value
        K_μ[2, 1, 2, 2, 1, 2] = value
        K_μ[2, 2, 1, 2, 2, 1] = value
        K_μ[2, 2, 2, 2, 2, 2] = value

        value = (2 * h[2]) / (3 * h[1]) + h[1] / (3 * h[2])
        K_μ[1, 1, 1, 1, 1, 1] = value
        K_μ[1, 1, 2, 1, 1, 2] = value
        K_μ[1, 2, 1, 1, 2, 1] = value
        K_μ[1, 2, 2, 1, 2, 2] = value
    elseif d == 3
        value = -h[1] / 12
        K_μ[2, 1, 1, 1, 3, 1, 2, 1] = value
        K_μ[2, 1, 1, 1, 3, 1, 2, 2] = value
        K_μ[2, 1, 1, 2, 3, 1, 1, 1] = value
        K_μ[2, 1, 1, 2, 3, 1, 1, 2] = value
        K_μ[2, 1, 2, 1, 3, 1, 2, 1] = value
        K_μ[2, 1, 2, 1, 3, 1, 2, 2] = value
        K_μ[2, 1, 2, 2, 3, 1, 1, 1] = value
        K_μ[2, 1, 2, 2, 3, 1, 1, 2] = value
        K_μ[2, 2, 1, 1, 3, 2, 2, 1] = value
        K_μ[2, 2, 1, 1, 3, 2, 2, 2] = value
        K_μ[2, 2, 1, 2, 3, 2, 1, 1] = value
        K_μ[2, 2, 1, 2, 3, 2, 1, 2] = value
        K_μ[2, 2, 2, 1, 3, 2, 2, 1] = value
        K_μ[2, 2, 2, 1, 3, 2, 2, 2] = value
        K_μ[2, 2, 2, 2, 3, 2, 1, 1] = value
        K_μ[2, 2, 2, 2, 3, 2, 1, 2] = value
        K_μ[3, 1, 1, 1, 2, 1, 1, 2] = value
        K_μ[3, 1, 1, 1, 2, 1, 2, 2] = value
        K_μ[3, 1, 1, 2, 2, 1, 1, 2] = value
        K_μ[3, 1, 1, 2, 2, 1, 2, 2] = value
        K_μ[3, 1, 2, 1, 2, 1, 1, 1] = value
        K_μ[3, 1, 2, 1, 2, 1, 2, 1] = value
        K_μ[3, 1, 2, 2, 2, 1, 1, 1] = value
        K_μ[3, 1, 2, 2, 2, 1, 2, 1] = value
        K_μ[3, 2, 1, 1, 2, 2, 1, 2] = value
        K_μ[3, 2, 1, 1, 2, 2, 2, 2] = value
        K_μ[3, 2, 1, 2, 2, 2, 1, 2] = value
        K_μ[3, 2, 1, 2, 2, 2, 2, 2] = value
        K_μ[3, 2, 2, 1, 2, 2, 1, 1] = value
        K_μ[3, 2, 2, 1, 2, 2, 2, 1] = value
        K_μ[3, 2, 2, 2, 2, 2, 1, 1] = value
        K_μ[3, 2, 2, 2, 2, 2, 2, 1] = value

        value = -h[1] / 24
        K_μ[2, 1, 1, 1, 3, 2, 2, 1] = value
        K_μ[2, 1, 1, 1, 3, 2, 2, 2] = value
        K_μ[2, 1, 1, 2, 3, 2, 1, 1] = value
        K_μ[2, 1, 1, 2, 3, 2, 1, 2] = value
        K_μ[2, 1, 2, 1, 3, 2, 2, 1] = value
        K_μ[2, 1, 2, 1, 3, 2, 2, 2] = value
        K_μ[2, 1, 2, 2, 3, 2, 1, 1] = value
        K_μ[2, 1, 2, 2, 3, 2, 1, 2] = value
        K_μ[2, 2, 1, 1, 3, 1, 2, 1] = value
        K_μ[2, 2, 1, 1, 3, 1, 2, 2] = value
        K_μ[2, 2, 1, 2, 3, 1, 1, 1] = value
        K_μ[2, 2, 1, 2, 3, 1, 1, 2] = value
        K_μ[2, 2, 2, 1, 3, 1, 2, 1] = value
        K_μ[2, 2, 2, 1, 3, 1, 2, 2] = value
        K_μ[2, 2, 2, 2, 3, 1, 1, 1] = value
        K_μ[2, 2, 2, 2, 3, 1, 1, 2] = value
        K_μ[3, 1, 1, 1, 2, 2, 1, 2] = value
        K_μ[3, 1, 1, 1, 2, 2, 2, 2] = value
        K_μ[3, 1, 1, 2, 2, 2, 1, 2] = value
        K_μ[3, 1, 1, 2, 2, 2, 2, 2] = value
        K_μ[3, 1, 2, 1, 2, 2, 1, 1] = value
        K_μ[3, 1, 2, 1, 2, 2, 2, 1] = value
        K_μ[3, 1, 2, 2, 2, 2, 1, 1] = value
        K_μ[3, 1, 2, 2, 2, 2, 2, 1] = value
        K_μ[3, 2, 1, 1, 2, 1, 1, 2] = value
        K_μ[3, 2, 1, 1, 2, 1, 2, 2] = value
        K_μ[3, 2, 1, 2, 2, 1, 1, 2] = value
        K_μ[3, 2, 1, 2, 2, 1, 2, 2] = value
        K_μ[3, 2, 2, 1, 2, 1, 1, 1] = value
        K_μ[3, 2, 2, 1, 2, 1, 2, 1] = value
        K_μ[3, 2, 2, 2, 2, 1, 1, 1] = value
        K_μ[3, 2, 2, 2, 2, 1, 2, 1] = value

        value = h[1] / 24
        K_μ[2, 1, 1, 1, 3, 2, 1, 1] = value
        K_μ[2, 1, 1, 1, 3, 2, 1, 2] = value
        K_μ[2, 1, 1, 2, 3, 2, 2, 1] = value
        K_μ[2, 1, 1, 2, 3, 2, 2, 2] = value
        K_μ[2, 1, 2, 1, 3, 2, 1, 1] = value
        K_μ[2, 1, 2, 1, 3, 2, 1, 2] = value
        K_μ[2, 1, 2, 2, 3, 2, 2, 1] = value
        K_μ[2, 1, 2, 2, 3, 2, 2, 2] = value
        K_μ[2, 2, 1, 1, 3, 1, 1, 1] = value
        K_μ[2, 2, 1, 1, 3, 1, 1, 2] = value
        K_μ[2, 2, 1, 2, 3, 1, 2, 1] = value
        K_μ[2, 2, 1, 2, 3, 1, 2, 2] = value
        K_μ[2, 2, 2, 1, 3, 1, 1, 1] = value
        K_μ[2, 2, 2, 1, 3, 1, 1, 2] = value
        K_μ[2, 2, 2, 2, 3, 1, 2, 1] = value
        K_μ[2, 2, 2, 2, 3, 1, 2, 2] = value
        K_μ[3, 1, 1, 1, 2, 2, 1, 1] = value
        K_μ[3, 1, 1, 1, 2, 2, 2, 1] = value
        K_μ[3, 1, 1, 2, 2, 2, 1, 1] = value
        K_μ[3, 1, 1, 2, 2, 2, 2, 1] = value
        K_μ[3, 1, 2, 1, 2, 2, 1, 2] = value
        K_μ[3, 1, 2, 1, 2, 2, 2, 2] = value
        K_μ[3, 1, 2, 2, 2, 2, 1, 2] = value
        K_μ[3, 1, 2, 2, 2, 2, 2, 2] = value
        K_μ[3, 2, 1, 1, 2, 1, 1, 1] = value
        K_μ[3, 2, 1, 1, 2, 1, 2, 1] = value
        K_μ[3, 2, 1, 2, 2, 1, 1, 1] = value
        K_μ[3, 2, 1, 2, 2, 1, 2, 1] = value
        K_μ[3, 2, 2, 1, 2, 1, 1, 2] = value
        K_μ[3, 2, 2, 1, 2, 1, 2, 2] = value
        K_μ[3, 2, 2, 2, 2, 1, 1, 2] = value
        K_μ[3, 2, 2, 2, 2, 1, 2, 2] = value

        value = h[1] / 12
        K_μ[2, 1, 1, 1, 3, 1, 1, 1] = value
        K_μ[2, 1, 1, 1, 3, 1, 1, 2] = value
        K_μ[2, 1, 1, 2, 3, 1, 2, 1] = value
        K_μ[2, 1, 1, 2, 3, 1, 2, 2] = value
        K_μ[2, 1, 2, 1, 3, 1, 1, 1] = value
        K_μ[2, 1, 2, 1, 3, 1, 1, 2] = value
        K_μ[2, 1, 2, 2, 3, 1, 2, 1] = value
        K_μ[2, 1, 2, 2, 3, 1, 2, 2] = value
        K_μ[2, 2, 1, 1, 3, 2, 1, 1] = value
        K_μ[2, 2, 1, 1, 3, 2, 1, 2] = value
        K_μ[2, 2, 1, 2, 3, 2, 2, 1] = value
        K_μ[2, 2, 1, 2, 3, 2, 2, 2] = value
        K_μ[2, 2, 2, 1, 3, 2, 1, 1] = value
        K_μ[2, 2, 2, 1, 3, 2, 1, 2] = value
        K_μ[2, 2, 2, 2, 3, 2, 2, 1] = value
        K_μ[2, 2, 2, 2, 3, 2, 2, 2] = value
        K_μ[3, 1, 1, 1, 2, 1, 1, 1] = value
        K_μ[3, 1, 1, 1, 2, 1, 2, 1] = value
        K_μ[3, 1, 1, 2, 2, 1, 1, 1] = value
        K_μ[3, 1, 1, 2, 2, 1, 2, 1] = value
        K_μ[3, 1, 2, 1, 2, 1, 1, 2] = value
        K_μ[3, 1, 2, 1, 2, 1, 2, 2] = value
        K_μ[3, 1, 2, 2, 2, 1, 1, 2] = value
        K_μ[3, 1, 2, 2, 2, 1, 2, 2] = value
        K_μ[3, 2, 1, 1, 2, 2, 1, 1] = value
        K_μ[3, 2, 1, 1, 2, 2, 2, 1] = value
        K_μ[3, 2, 1, 2, 2, 2, 1, 1] = value
        K_μ[3, 2, 1, 2, 2, 2, 2, 1] = value
        K_μ[3, 2, 2, 1, 2, 2, 1, 2] = value
        K_μ[3, 2, 2, 1, 2, 2, 2, 2] = value
        K_μ[3, 2, 2, 2, 2, 2, 1, 2] = value
        K_μ[3, 2, 2, 2, 2, 2, 2, 2] = value

        value = -h[2] / 12
        K_μ[1, 1, 1, 1, 3, 2, 1, 1] = value
        K_μ[1, 1, 1, 1, 3, 2, 1, 2] = value
        K_μ[1, 1, 1, 2, 3, 1, 1, 1] = value
        K_μ[1, 1, 1, 2, 3, 1, 1, 2] = value
        K_μ[1, 1, 2, 1, 3, 2, 2, 1] = value
        K_μ[1, 1, 2, 1, 3, 2, 2, 2] = value
        K_μ[1, 1, 2, 2, 3, 1, 2, 1] = value
        K_μ[1, 1, 2, 2, 3, 1, 2, 2] = value
        K_μ[1, 2, 1, 1, 3, 2, 1, 1] = value
        K_μ[1, 2, 1, 1, 3, 2, 1, 2] = value
        K_μ[1, 2, 1, 2, 3, 1, 1, 1] = value
        K_μ[1, 2, 1, 2, 3, 1, 1, 2] = value
        K_μ[1, 2, 2, 1, 3, 2, 2, 1] = value
        K_μ[1, 2, 2, 1, 3, 2, 2, 2] = value
        K_μ[1, 2, 2, 2, 3, 1, 2, 1] = value
        K_μ[1, 2, 2, 2, 3, 1, 2, 2] = value
        K_μ[3, 1, 1, 1, 1, 1, 1, 2] = value
        K_μ[3, 1, 1, 1, 1, 2, 1, 2] = value
        K_μ[3, 1, 1, 2, 1, 1, 1, 2] = value
        K_μ[3, 1, 1, 2, 1, 2, 1, 2] = value
        K_μ[3, 1, 2, 1, 1, 1, 2, 2] = value
        K_μ[3, 1, 2, 1, 1, 2, 2, 2] = value
        K_μ[3, 1, 2, 2, 1, 1, 2, 2] = value
        K_μ[3, 1, 2, 2, 1, 2, 2, 2] = value
        K_μ[3, 2, 1, 1, 1, 1, 1, 1] = value
        K_μ[3, 2, 1, 1, 1, 2, 1, 1] = value
        K_μ[3, 2, 1, 2, 1, 1, 1, 1] = value
        K_μ[3, 2, 1, 2, 1, 2, 1, 1] = value
        K_μ[3, 2, 2, 1, 1, 1, 2, 1] = value
        K_μ[3, 2, 2, 1, 1, 2, 2, 1] = value
        K_μ[3, 2, 2, 2, 1, 1, 2, 1] = value
        K_μ[3, 2, 2, 2, 1, 2, 2, 1] = value

        value = -h[2] / 24
        K_μ[1, 1, 1, 1, 3, 2, 2, 1] = value
        K_μ[1, 1, 1, 1, 3, 2, 2, 2] = value
        K_μ[1, 1, 1, 2, 3, 1, 2, 1] = value
        K_μ[1, 1, 1, 2, 3, 1, 2, 2] = value
        K_μ[1, 1, 2, 1, 3, 2, 1, 1] = value
        K_μ[1, 1, 2, 1, 3, 2, 1, 2] = value
        K_μ[1, 1, 2, 2, 3, 1, 1, 1] = value
        K_μ[1, 1, 2, 2, 3, 1, 1, 2] = value
        K_μ[1, 2, 1, 1, 3, 2, 2, 1] = value
        K_μ[1, 2, 1, 1, 3, 2, 2, 2] = value
        K_μ[1, 2, 1, 2, 3, 1, 2, 1] = value
        K_μ[1, 2, 1, 2, 3, 1, 2, 2] = value
        K_μ[1, 2, 2, 1, 3, 2, 1, 1] = value
        K_μ[1, 2, 2, 1, 3, 2, 1, 2] = value
        K_μ[1, 2, 2, 2, 3, 1, 1, 1] = value
        K_μ[1, 2, 2, 2, 3, 1, 1, 2] = value
        K_μ[3, 1, 1, 1, 1, 1, 2, 2] = value
        K_μ[3, 1, 1, 1, 1, 2, 2, 2] = value
        K_μ[3, 1, 1, 2, 1, 1, 2, 2] = value
        K_μ[3, 1, 1, 2, 1, 2, 2, 2] = value
        K_μ[3, 1, 2, 1, 1, 1, 1, 2] = value
        K_μ[3, 1, 2, 1, 1, 2, 1, 2] = value
        K_μ[3, 1, 2, 2, 1, 1, 1, 2] = value
        K_μ[3, 1, 2, 2, 1, 2, 1, 2] = value
        K_μ[3, 2, 1, 1, 1, 1, 2, 1] = value
        K_μ[3, 2, 1, 1, 1, 2, 2, 1] = value
        K_μ[3, 2, 1, 2, 1, 1, 2, 1] = value
        K_μ[3, 2, 1, 2, 1, 2, 2, 1] = value
        K_μ[3, 2, 2, 1, 1, 1, 1, 1] = value
        K_μ[3, 2, 2, 1, 1, 2, 1, 1] = value
        K_μ[3, 2, 2, 2, 1, 1, 1, 1] = value
        K_μ[3, 2, 2, 2, 1, 2, 1, 1] = value

        value = h[2] / 24
        K_μ[1, 1, 1, 1, 3, 1, 2, 1] = value
        K_μ[1, 1, 1, 1, 3, 1, 2, 2] = value
        K_μ[1, 1, 1, 2, 3, 2, 2, 1] = value
        K_μ[1, 1, 1, 2, 3, 2, 2, 2] = value
        K_μ[1, 1, 2, 1, 3, 1, 1, 1] = value
        K_μ[1, 1, 2, 1, 3, 1, 1, 2] = value
        K_μ[1, 1, 2, 2, 3, 2, 1, 1] = value
        K_μ[1, 1, 2, 2, 3, 2, 1, 2] = value
        K_μ[1, 2, 1, 1, 3, 1, 2, 1] = value
        K_μ[1, 2, 1, 1, 3, 1, 2, 2] = value
        K_μ[1, 2, 1, 2, 3, 2, 2, 1] = value
        K_μ[1, 2, 1, 2, 3, 2, 2, 2] = value
        K_μ[1, 2, 2, 1, 3, 1, 1, 1] = value
        K_μ[1, 2, 2, 1, 3, 1, 1, 2] = value
        K_μ[1, 2, 2, 2, 3, 2, 1, 1] = value
        K_μ[1, 2, 2, 2, 3, 2, 1, 2] = value
        K_μ[3, 1, 1, 1, 1, 1, 2, 1] = value
        K_μ[3, 1, 1, 1, 1, 2, 2, 1] = value
        K_μ[3, 1, 1, 2, 1, 1, 2, 1] = value
        K_μ[3, 1, 1, 2, 1, 2, 2, 1] = value
        K_μ[3, 1, 2, 1, 1, 1, 1, 1] = value
        K_μ[3, 1, 2, 1, 1, 2, 1, 1] = value
        K_μ[3, 1, 2, 2, 1, 1, 1, 1] = value
        K_μ[3, 1, 2, 2, 1, 2, 1, 1] = value
        K_μ[3, 2, 1, 1, 1, 1, 2, 2] = value
        K_μ[3, 2, 1, 1, 1, 2, 2, 2] = value
        K_μ[3, 2, 1, 2, 1, 1, 2, 2] = value
        K_μ[3, 2, 1, 2, 1, 2, 2, 2] = value
        K_μ[3, 2, 2, 1, 1, 1, 1, 2] = value
        K_μ[3, 2, 2, 1, 1, 2, 1, 2] = value
        K_μ[3, 2, 2, 2, 1, 1, 1, 2] = value
        K_μ[3, 2, 2, 2, 1, 2, 1, 2] = value

        value = h[2] / 12
        K_μ[1, 1, 1, 1, 3, 1, 1, 1] = value
        K_μ[1, 1, 1, 1, 3, 1, 1, 2] = value
        K_μ[1, 1, 1, 2, 3, 2, 1, 1] = value
        K_μ[1, 1, 1, 2, 3, 2, 1, 2] = value
        K_μ[1, 1, 2, 1, 3, 1, 2, 1] = value
        K_μ[1, 1, 2, 1, 3, 1, 2, 2] = value
        K_μ[1, 1, 2, 2, 3, 2, 2, 1] = value
        K_μ[1, 1, 2, 2, 3, 2, 2, 2] = value
        K_μ[1, 2, 1, 1, 3, 1, 1, 1] = value
        K_μ[1, 2, 1, 1, 3, 1, 1, 2] = value
        K_μ[1, 2, 1, 2, 3, 2, 1, 1] = value
        K_μ[1, 2, 1, 2, 3, 2, 1, 2] = value
        K_μ[1, 2, 2, 1, 3, 1, 2, 1] = value
        K_μ[1, 2, 2, 1, 3, 1, 2, 2] = value
        K_μ[1, 2, 2, 2, 3, 2, 2, 1] = value
        K_μ[1, 2, 2, 2, 3, 2, 2, 2] = value
        K_μ[3, 1, 1, 1, 1, 1, 1, 1] = value
        K_μ[3, 1, 1, 1, 1, 2, 1, 1] = value
        K_μ[3, 1, 1, 2, 1, 1, 1, 1] = value
        K_μ[3, 1, 1, 2, 1, 2, 1, 1] = value
        K_μ[3, 1, 2, 1, 1, 1, 2, 1] = value
        K_μ[3, 1, 2, 1, 1, 2, 2, 1] = value
        K_μ[3, 1, 2, 2, 1, 1, 2, 1] = value
        K_μ[3, 1, 2, 2, 1, 2, 2, 1] = value
        K_μ[3, 2, 1, 1, 1, 1, 1, 2] = value
        K_μ[3, 2, 1, 1, 1, 2, 1, 2] = value
        K_μ[3, 2, 1, 2, 1, 1, 1, 2] = value
        K_μ[3, 2, 1, 2, 1, 2, 1, 2] = value
        K_μ[3, 2, 2, 1, 1, 1, 2, 2] = value
        K_μ[3, 2, 2, 1, 1, 2, 2, 2] = value
        K_μ[3, 2, 2, 2, 1, 1, 2, 2] = value
        K_μ[3, 2, 2, 2, 1, 2, 2, 2] = value

        value = -h[3] / 12
        K_μ[1, 1, 1, 1, 2, 2, 1, 1] = value
        K_μ[1, 1, 1, 1, 2, 2, 2, 1] = value
        K_μ[1, 1, 1, 2, 2, 2, 1, 2] = value
        K_μ[1, 1, 1, 2, 2, 2, 2, 2] = value
        K_μ[1, 1, 2, 1, 2, 1, 1, 1] = value
        K_μ[1, 1, 2, 1, 2, 1, 2, 1] = value
        K_μ[1, 1, 2, 2, 2, 1, 1, 2] = value
        K_μ[1, 1, 2, 2, 2, 1, 2, 2] = value
        K_μ[1, 2, 1, 1, 2, 2, 1, 1] = value
        K_μ[1, 2, 1, 1, 2, 2, 2, 1] = value
        K_μ[1, 2, 1, 2, 2, 2, 1, 2] = value
        K_μ[1, 2, 1, 2, 2, 2, 2, 2] = value
        K_μ[1, 2, 2, 1, 2, 1, 1, 1] = value
        K_μ[1, 2, 2, 1, 2, 1, 2, 1] = value
        K_μ[1, 2, 2, 2, 2, 1, 1, 2] = value
        K_μ[1, 2, 2, 2, 2, 1, 2, 2] = value
        K_μ[2, 1, 1, 1, 1, 1, 2, 1] = value
        K_μ[2, 1, 1, 1, 1, 2, 2, 1] = value
        K_μ[2, 1, 1, 2, 1, 1, 2, 2] = value
        K_μ[2, 1, 1, 2, 1, 2, 2, 2] = value
        K_μ[2, 1, 2, 1, 1, 1, 2, 1] = value
        K_μ[2, 1, 2, 1, 1, 2, 2, 1] = value
        K_μ[2, 1, 2, 2, 1, 1, 2, 2] = value
        K_μ[2, 1, 2, 2, 1, 2, 2, 2] = value
        K_μ[2, 2, 1, 1, 1, 1, 1, 1] = value
        K_μ[2, 2, 1, 1, 1, 2, 1, 1] = value
        K_μ[2, 2, 1, 2, 1, 1, 1, 2] = value
        K_μ[2, 2, 1, 2, 1, 2, 1, 2] = value
        K_μ[2, 2, 2, 1, 1, 1, 1, 1] = value
        K_μ[2, 2, 2, 1, 1, 2, 1, 1] = value
        K_μ[2, 2, 2, 2, 1, 1, 1, 2] = value
        K_μ[2, 2, 2, 2, 1, 2, 1, 2] = value

        value = -h[3] / 24
        K_μ[1, 1, 1, 1, 2, 2, 1, 2] = value
        K_μ[1, 1, 1, 1, 2, 2, 2, 2] = value
        K_μ[1, 1, 1, 2, 2, 2, 1, 1] = value
        K_μ[1, 1, 1, 2, 2, 2, 2, 1] = value
        K_μ[1, 1, 2, 1, 2, 1, 1, 2] = value
        K_μ[1, 1, 2, 1, 2, 1, 2, 2] = value
        K_μ[1, 1, 2, 2, 2, 1, 1, 1] = value
        K_μ[1, 1, 2, 2, 2, 1, 2, 1] = value
        K_μ[1, 2, 1, 1, 2, 2, 1, 2] = value
        K_μ[1, 2, 1, 1, 2, 2, 2, 2] = value
        K_μ[1, 2, 1, 2, 2, 2, 1, 1] = value
        K_μ[1, 2, 1, 2, 2, 2, 2, 1] = value
        K_μ[1, 2, 2, 1, 2, 1, 1, 2] = value
        K_μ[1, 2, 2, 1, 2, 1, 2, 2] = value
        K_μ[1, 2, 2, 2, 2, 1, 1, 1] = value
        K_μ[1, 2, 2, 2, 2, 1, 2, 1] = value
        K_μ[2, 1, 1, 1, 1, 1, 2, 2] = value
        K_μ[2, 1, 1, 1, 1, 2, 2, 2] = value
        K_μ[2, 1, 1, 2, 1, 1, 2, 1] = value
        K_μ[2, 1, 1, 2, 1, 2, 2, 1] = value
        K_μ[2, 1, 2, 1, 1, 1, 2, 2] = value
        K_μ[2, 1, 2, 1, 1, 2, 2, 2] = value
        K_μ[2, 1, 2, 2, 1, 1, 2, 1] = value
        K_μ[2, 1, 2, 2, 1, 2, 2, 1] = value
        K_μ[2, 2, 1, 1, 1, 1, 1, 2] = value
        K_μ[2, 2, 1, 1, 1, 2, 1, 2] = value
        K_μ[2, 2, 1, 2, 1, 1, 1, 1] = value
        K_μ[2, 2, 1, 2, 1, 2, 1, 1] = value
        K_μ[2, 2, 2, 1, 1, 1, 1, 2] = value
        K_μ[2, 2, 2, 1, 1, 2, 1, 2] = value
        K_μ[2, 2, 2, 2, 1, 1, 1, 1] = value
        K_μ[2, 2, 2, 2, 1, 2, 1, 1] = value

        value = h[3] / 24
        K_μ[1, 1, 1, 1, 2, 1, 1, 2] = value
        K_μ[1, 1, 1, 1, 2, 1, 2, 2] = value
        K_μ[1, 1, 1, 2, 2, 1, 1, 1] = value
        K_μ[1, 1, 1, 2, 2, 1, 2, 1] = value
        K_μ[1, 1, 2, 1, 2, 2, 1, 2] = value
        K_μ[1, 1, 2, 1, 2, 2, 2, 2] = value
        K_μ[1, 1, 2, 2, 2, 2, 1, 1] = value
        K_μ[1, 1, 2, 2, 2, 2, 2, 1] = value
        K_μ[1, 2, 1, 1, 2, 1, 1, 2] = value
        K_μ[1, 2, 1, 1, 2, 1, 2, 2] = value
        K_μ[1, 2, 1, 2, 2, 1, 1, 1] = value
        K_μ[1, 2, 1, 2, 2, 1, 2, 1] = value
        K_μ[1, 2, 2, 1, 2, 2, 1, 2] = value
        K_μ[1, 2, 2, 1, 2, 2, 2, 2] = value
        K_μ[1, 2, 2, 2, 2, 2, 1, 1] = value
        K_μ[1, 2, 2, 2, 2, 2, 2, 1] = value
        K_μ[2, 1, 1, 1, 1, 1, 1, 2] = value
        K_μ[2, 1, 1, 1, 1, 2, 1, 2] = value
        K_μ[2, 1, 1, 2, 1, 1, 1, 1] = value
        K_μ[2, 1, 1, 2, 1, 2, 1, 1] = value
        K_μ[2, 1, 2, 1, 1, 1, 1, 2] = value
        K_μ[2, 1, 2, 1, 1, 2, 1, 2] = value
        K_μ[2, 1, 2, 2, 1, 1, 1, 1] = value
        K_μ[2, 1, 2, 2, 1, 2, 1, 1] = value
        K_μ[2, 2, 1, 1, 1, 1, 2, 2] = value
        K_μ[2, 2, 1, 1, 1, 2, 2, 2] = value
        K_μ[2, 2, 1, 2, 1, 1, 2, 1] = value
        K_μ[2, 2, 1, 2, 1, 2, 2, 1] = value
        K_μ[2, 2, 2, 1, 1, 1, 2, 2] = value
        K_μ[2, 2, 2, 1, 1, 2, 2, 2] = value
        K_μ[2, 2, 2, 2, 1, 1, 2, 1] = value
        K_μ[2, 2, 2, 2, 1, 2, 2, 1] = value

        value = h[3] / 12
        K_μ[1, 1, 1, 1, 2, 1, 1, 1] = value
        K_μ[1, 1, 1, 1, 2, 1, 2, 1] = value
        K_μ[1, 1, 1, 2, 2, 1, 1, 2] = value
        K_μ[1, 1, 1, 2, 2, 1, 2, 2] = value
        K_μ[1, 1, 2, 1, 2, 2, 1, 1] = value
        K_μ[1, 1, 2, 1, 2, 2, 2, 1] = value
        K_μ[1, 1, 2, 2, 2, 2, 1, 2] = value
        K_μ[1, 1, 2, 2, 2, 2, 2, 2] = value
        K_μ[1, 2, 1, 1, 2, 1, 1, 1] = value
        K_μ[1, 2, 1, 1, 2, 1, 2, 1] = value
        K_μ[1, 2, 1, 2, 2, 1, 1, 2] = value
        K_μ[1, 2, 1, 2, 2, 1, 2, 2] = value
        K_μ[1, 2, 2, 1, 2, 2, 1, 1] = value
        K_μ[1, 2, 2, 1, 2, 2, 2, 1] = value
        K_μ[1, 2, 2, 2, 2, 2, 1, 2] = value
        K_μ[1, 2, 2, 2, 2, 2, 2, 2] = value
        K_μ[2, 1, 1, 1, 1, 1, 1, 1] = value
        K_μ[2, 1, 1, 1, 1, 2, 1, 1] = value
        K_μ[2, 1, 1, 2, 1, 1, 1, 2] = value
        K_μ[2, 1, 1, 2, 1, 2, 1, 2] = value
        K_μ[2, 1, 2, 1, 1, 1, 1, 1] = value
        K_μ[2, 1, 2, 1, 1, 2, 1, 1] = value
        K_μ[2, 1, 2, 2, 1, 1, 1, 2] = value
        K_μ[2, 1, 2, 2, 1, 2, 1, 2] = value
        K_μ[2, 2, 1, 1, 1, 1, 2, 1] = value
        K_μ[2, 2, 1, 1, 1, 2, 2, 1] = value
        K_μ[2, 2, 1, 2, 1, 1, 2, 2] = value
        K_μ[2, 2, 1, 2, 1, 2, 2, 2] = value
        K_μ[2, 2, 2, 1, 1, 1, 2, 1] = value
        K_μ[2, 2, 2, 1, 1, 2, 2, 1] = value
        K_μ[2, 2, 2, 2, 1, 1, 2, 2] = value
        K_μ[2, 2, 2, 2, 1, 2, 2, 2] = value

        value =
            (-(2 * h[2] * h[3]) / (9 * h[1])) +
            (h[1] * h[3]) / (18 * h[2]) +
            (h[1] * h[2]) / (18 * h[3])
        K_μ[1, 1, 1, 1, 1, 2, 1, 1] = value
        K_μ[1, 1, 1, 2, 1, 2, 1, 2] = value
        K_μ[1, 1, 2, 1, 1, 2, 2, 1] = value
        K_μ[1, 1, 2, 2, 1, 2, 2, 2] = value
        K_μ[1, 2, 1, 1, 1, 1, 1, 1] = value
        K_μ[1, 2, 1, 2, 1, 1, 1, 2] = value
        K_μ[1, 2, 2, 1, 1, 1, 2, 1] = value
        K_μ[1, 2, 2, 2, 1, 1, 2, 2] = value

        value =
            (-(h[2] * h[3]) / (9 * h[1])) - (h[1] * h[3]) / (18 * h[2]) +
            (h[1] * h[2]) / (36 * h[3])
        K_μ[1, 1, 1, 1, 1, 2, 2, 1] = value
        K_μ[1, 1, 1, 2, 1, 2, 2, 2] = value
        K_μ[1, 1, 2, 1, 1, 2, 1, 1] = value
        K_μ[1, 1, 2, 2, 1, 2, 1, 2] = value
        K_μ[1, 2, 1, 1, 1, 1, 2, 1] = value
        K_μ[1, 2, 1, 2, 1, 1, 2, 2] = value
        K_μ[1, 2, 2, 1, 1, 1, 1, 1] = value
        K_μ[1, 2, 2, 2, 1, 1, 1, 2] = value

        value =
            (-(h[2] * h[3]) / (9 * h[1])) + (h[1] * h[3]) / (36 * h[2]) -
            (h[1] * h[2]) / (18 * h[3])
        K_μ[1, 1, 1, 1, 1, 2, 1, 2] = value
        K_μ[1, 1, 1, 2, 1, 2, 1, 1] = value
        K_μ[1, 1, 2, 1, 1, 2, 2, 2] = value
        K_μ[1, 1, 2, 2, 1, 2, 2, 1] = value
        K_μ[1, 2, 1, 1, 1, 1, 1, 2] = value
        K_μ[1, 2, 1, 2, 1, 1, 1, 1] = value
        K_μ[1, 2, 2, 1, 1, 1, 2, 2] = value
        K_μ[1, 2, 2, 2, 1, 1, 2, 1] = value

        value =
            (-(h[2] * h[3]) / (9 * h[1])) +
            (h[1] * h[3]) / (18 * h[2]) +
            (h[1] * h[2]) / (9 * h[3])
        K_μ[3, 1, 1, 1, 3, 2, 1, 1] = value
        K_μ[3, 1, 1, 2, 3, 2, 1, 2] = value
        K_μ[3, 1, 2, 1, 3, 2, 2, 1] = value
        K_μ[3, 1, 2, 2, 3, 2, 2, 2] = value
        K_μ[3, 2, 1, 1, 3, 1, 1, 1] = value
        K_μ[3, 2, 1, 2, 3, 1, 1, 2] = value
        K_μ[3, 2, 2, 1, 3, 1, 2, 1] = value
        K_μ[3, 2, 2, 2, 3, 1, 2, 2] = value

        value =
            (-(h[2] * h[3]) / (9 * h[1])) +
            (h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (18 * h[3])
        K_μ[2, 1, 1, 1, 2, 2, 1, 1] = value
        K_μ[2, 1, 1, 2, 2, 2, 1, 2] = value
        K_μ[2, 1, 2, 1, 2, 2, 2, 1] = value
        K_μ[2, 1, 2, 2, 2, 2, 2, 2] = value
        K_μ[2, 2, 1, 1, 2, 1, 1, 1] = value
        K_μ[2, 2, 1, 2, 2, 1, 1, 2] = value
        K_μ[2, 2, 2, 1, 2, 1, 2, 1] = value
        K_μ[2, 2, 2, 2, 2, 1, 2, 2] = value

        value =
            (-(h[2] * h[3]) / (18 * h[1])) - (h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (36 * h[3])
        K_μ[2, 1, 1, 1, 2, 2, 2, 1] = value
        K_μ[2, 1, 1, 2, 2, 2, 2, 2] = value
        K_μ[2, 1, 2, 1, 2, 2, 1, 1] = value
        K_μ[2, 1, 2, 2, 2, 2, 1, 2] = value
        K_μ[2, 2, 1, 1, 2, 1, 2, 1] = value
        K_μ[2, 2, 1, 2, 2, 1, 2, 2] = value
        K_μ[2, 2, 2, 1, 2, 1, 1, 1] = value
        K_μ[2, 2, 2, 2, 2, 1, 1, 2] = value

        value =
            (-(h[2] * h[3]) / (18 * h[1])) - (h[1] * h[3]) / (18 * h[2]) +
            (h[1] * h[2]) / (18 * h[3])
        K_μ[3, 1, 1, 1, 3, 2, 2, 1] = value
        K_μ[3, 1, 1, 2, 3, 2, 2, 2] = value
        K_μ[3, 1, 2, 1, 3, 2, 1, 1] = value
        K_μ[3, 1, 2, 2, 3, 2, 1, 2] = value
        K_μ[3, 2, 1, 1, 3, 1, 2, 1] = value
        K_μ[3, 2, 1, 2, 3, 1, 2, 2] = value
        K_μ[3, 2, 2, 1, 3, 1, 1, 1] = value
        K_μ[3, 2, 2, 2, 3, 1, 1, 2] = value

        value =
            (-(h[2] * h[3]) / (18 * h[1])) - (h[1] * h[3]) / (36 * h[2]) -
            (h[1] * h[2]) / (36 * h[3])
        K_μ[1, 1, 1, 1, 1, 2, 2, 2] = value
        K_μ[1, 1, 1, 2, 1, 2, 2, 1] = value
        K_μ[1, 1, 2, 1, 1, 2, 1, 2] = value
        K_μ[1, 1, 2, 2, 1, 2, 1, 1] = value
        K_μ[1, 2, 1, 1, 1, 1, 2, 2] = value
        K_μ[1, 2, 1, 2, 1, 1, 2, 1] = value
        K_μ[1, 2, 2, 1, 1, 1, 1, 2] = value
        K_μ[1, 2, 2, 2, 1, 1, 1, 1] = value

        value =
            (-(h[2] * h[3]) / (18 * h[1])) + (h[1] * h[3]) / (36 * h[2]) -
            (h[1] * h[2]) / (9 * h[3])
        K_μ[3, 1, 1, 1, 3, 2, 1, 2] = value
        K_μ[3, 1, 1, 2, 3, 2, 1, 1] = value
        K_μ[3, 1, 2, 1, 3, 2, 2, 2] = value
        K_μ[3, 1, 2, 2, 3, 2, 2, 1] = value
        K_μ[3, 2, 1, 1, 3, 1, 1, 2] = value
        K_μ[3, 2, 1, 2, 3, 1, 1, 1] = value
        K_μ[3, 2, 2, 1, 3, 1, 2, 2] = value
        K_μ[3, 2, 2, 2, 3, 1, 2, 1] = value

        value =
            (-(h[2] * h[3]) / (18 * h[1])) + (h[1] * h[3]) / (18 * h[2]) -
            (h[1] * h[2]) / (18 * h[3])
        K_μ[2, 1, 1, 1, 2, 2, 1, 2] = value
        K_μ[2, 1, 1, 2, 2, 2, 1, 1] = value
        K_μ[2, 1, 2, 1, 2, 2, 2, 2] = value
        K_μ[2, 1, 2, 2, 2, 2, 2, 1] = value
        K_μ[2, 2, 1, 1, 2, 1, 1, 2] = value
        K_μ[2, 2, 1, 2, 2, 1, 1, 1] = value
        K_μ[2, 2, 2, 1, 2, 1, 2, 2] = value
        K_μ[2, 2, 2, 2, 2, 1, 2, 1] = value

        value =
            (-(h[2] * h[3]) / (36 * h[1])) - (h[1] * h[3]) / (18 * h[2]) -
            (h[1] * h[2]) / (36 * h[3])
        K_μ[2, 1, 1, 1, 2, 2, 2, 2] = value
        K_μ[2, 1, 1, 2, 2, 2, 2, 1] = value
        K_μ[2, 1, 2, 1, 2, 2, 1, 2] = value
        K_μ[2, 1, 2, 2, 2, 2, 1, 1] = value
        K_μ[2, 2, 1, 1, 2, 1, 2, 2] = value
        K_μ[2, 2, 1, 2, 2, 1, 2, 1] = value
        K_μ[2, 2, 2, 1, 2, 1, 1, 2] = value
        K_μ[2, 2, 2, 2, 2, 1, 1, 1] = value

        value =
            (-(h[2] * h[3]) / (36 * h[1])) - (h[1] * h[3]) / (36 * h[2]) -
            (h[1] * h[2]) / (18 * h[3])
        K_μ[3, 1, 1, 1, 3, 2, 2, 2] = value
        K_μ[3, 1, 1, 2, 3, 2, 2, 1] = value
        K_μ[3, 1, 2, 1, 3, 2, 1, 2] = value
        K_μ[3, 1, 2, 2, 3, 2, 1, 1] = value
        K_μ[3, 2, 1, 1, 3, 1, 2, 2] = value
        K_μ[3, 2, 1, 2, 3, 1, 2, 1] = value
        K_μ[3, 2, 2, 1, 3, 1, 1, 2] = value
        K_μ[3, 2, 2, 2, 3, 1, 1, 1] = value

        value =
            (h[2] * h[3]) / (36 * h[1]) - (h[1] * h[3]) / (9 * h[2]) -
            (h[1] * h[2]) / (18 * h[3])
        K_μ[2, 1, 1, 1, 2, 1, 2, 2] = value
        K_μ[2, 1, 1, 2, 2, 1, 2, 1] = value
        K_μ[2, 1, 2, 1, 2, 1, 1, 2] = value
        K_μ[2, 1, 2, 2, 2, 1, 1, 1] = value
        K_μ[2, 2, 1, 1, 2, 2, 2, 2] = value
        K_μ[2, 2, 1, 2, 2, 2, 2, 1] = value
        K_μ[2, 2, 2, 1, 2, 2, 1, 2] = value
        K_μ[2, 2, 2, 2, 2, 2, 1, 1] = value

        value =
            (h[2] * h[3]) / (36 * h[1]) - (h[1] * h[3]) / (18 * h[2]) -
            (h[1] * h[2]) / (9 * h[3])
        K_μ[3, 1, 1, 1, 3, 1, 2, 2] = value
        K_μ[3, 1, 1, 2, 3, 1, 2, 1] = value
        K_μ[3, 1, 2, 1, 3, 1, 1, 2] = value
        K_μ[3, 1, 2, 2, 3, 1, 1, 1] = value
        K_μ[3, 2, 1, 1, 3, 2, 2, 2] = value
        K_μ[3, 2, 1, 2, 3, 2, 2, 1] = value
        K_μ[3, 2, 2, 1, 3, 2, 1, 2] = value
        K_μ[3, 2, 2, 2, 3, 2, 1, 1] = value

        value =
            (h[2] * h[3]) / (18 * h[1]) - (2 * h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (18 * h[3])
        K_μ[2, 1, 1, 1, 2, 1, 2, 1] = value
        K_μ[2, 1, 1, 2, 2, 1, 2, 2] = value
        K_μ[2, 1, 2, 1, 2, 1, 1, 1] = value
        K_μ[2, 1, 2, 2, 2, 1, 1, 2] = value
        K_μ[2, 2, 1, 1, 2, 2, 2, 1] = value
        K_μ[2, 2, 1, 2, 2, 2, 2, 2] = value
        K_μ[2, 2, 2, 1, 2, 2, 1, 1] = value
        K_μ[2, 2, 2, 2, 2, 2, 1, 2] = value

        value =
            (h[2] * h[3]) / (18 * h[1]) - (h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (9 * h[3])
        K_μ[3, 1, 1, 1, 3, 1, 2, 1] = value
        K_μ[3, 1, 1, 2, 3, 1, 2, 2] = value
        K_μ[3, 1, 2, 1, 3, 1, 1, 1] = value
        K_μ[3, 1, 2, 2, 3, 1, 1, 2] = value
        K_μ[3, 2, 1, 1, 3, 2, 2, 1] = value
        K_μ[3, 2, 1, 2, 3, 2, 2, 2] = value
        K_μ[3, 2, 2, 1, 3, 2, 1, 1] = value
        K_μ[3, 2, 2, 2, 3, 2, 1, 2] = value

        value =
            (h[2] * h[3]) / (18 * h[1]) - (h[1] * h[3]) / (18 * h[2]) -
            (h[1] * h[2]) / (18 * h[3])
        K_μ[1, 1, 1, 1, 1, 1, 2, 2] = value
        K_μ[1, 1, 1, 2, 1, 1, 2, 1] = value
        K_μ[1, 1, 2, 1, 1, 1, 1, 2] = value
        K_μ[1, 1, 2, 2, 1, 1, 1, 1] = value
        K_μ[1, 2, 1, 1, 1, 2, 2, 2] = value
        K_μ[1, 2, 1, 2, 1, 2, 2, 1] = value
        K_μ[1, 2, 2, 1, 1, 2, 1, 2] = value
        K_μ[1, 2, 2, 2, 1, 2, 1, 1] = value

        value =
            (h[2] * h[3]) / (18 * h[1]) + (h[1] * h[3]) / (18 * h[2]) -
            (2 * h[1] * h[2]) / (9 * h[3])
        K_μ[3, 1, 1, 1, 3, 1, 1, 2] = value
        K_μ[3, 1, 1, 2, 3, 1, 1, 1] = value
        K_μ[3, 1, 2, 1, 3, 1, 2, 2] = value
        K_μ[3, 1, 2, 2, 3, 1, 2, 1] = value
        K_μ[3, 2, 1, 1, 3, 2, 1, 2] = value
        K_μ[3, 2, 1, 2, 3, 2, 1, 1] = value
        K_μ[3, 2, 2, 1, 3, 2, 2, 2] = value
        K_μ[3, 2, 2, 2, 3, 2, 2, 1] = value

        value =
            (h[2] * h[3]) / (18 * h[1]) + (h[1] * h[3]) / (9 * h[2]) -
            (h[1] * h[2]) / (9 * h[3])
        K_μ[2, 1, 1, 1, 2, 1, 1, 2] = value
        K_μ[2, 1, 1, 2, 2, 1, 1, 1] = value
        K_μ[2, 1, 2, 1, 2, 1, 2, 2] = value
        K_μ[2, 1, 2, 2, 2, 1, 2, 1] = value
        K_μ[2, 2, 1, 1, 2, 2, 1, 2] = value
        K_μ[2, 2, 1, 2, 2, 2, 1, 1] = value
        K_μ[2, 2, 2, 1, 2, 2, 2, 2] = value
        K_μ[2, 2, 2, 2, 2, 2, 2, 1] = value

        value =
            (h[2] * h[3]) / (9 * h[1]) - (h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (18 * h[3])
        K_μ[1, 1, 1, 1, 1, 1, 2, 1] = value
        K_μ[1, 1, 1, 2, 1, 1, 2, 2] = value
        K_μ[1, 1, 2, 1, 1, 1, 1, 1] = value
        K_μ[1, 1, 2, 2, 1, 1, 1, 2] = value
        K_μ[1, 2, 1, 1, 1, 2, 2, 1] = value
        K_μ[1, 2, 1, 2, 1, 2, 2, 2] = value
        K_μ[1, 2, 2, 1, 1, 2, 1, 1] = value
        K_μ[1, 2, 2, 2, 1, 2, 1, 2] = value

        value =
            (h[2] * h[3]) / (9 * h[1]) + (h[1] * h[3]) / (18 * h[2]) -
            (h[1] * h[2]) / (9 * h[3])
        K_μ[1, 1, 1, 1, 1, 1, 1, 2] = value
        K_μ[1, 1, 1, 2, 1, 1, 1, 1] = value
        K_μ[1, 1, 2, 1, 1, 1, 2, 2] = value
        K_μ[1, 1, 2, 2, 1, 1, 2, 1] = value
        K_μ[1, 2, 1, 1, 1, 2, 1, 2] = value
        K_μ[1, 2, 1, 2, 1, 2, 1, 1] = value
        K_μ[1, 2, 2, 1, 1, 2, 2, 2] = value
        K_μ[1, 2, 2, 2, 1, 2, 2, 1] = value

        value =
            (h[2] * h[3]) / (9 * h[1]) +
            (h[1] * h[3]) / (9 * h[2]) +
            (2 * h[1] * h[2]) / (9 * h[3])
        K_μ[3, 1, 1, 1, 3, 1, 1, 1] = value
        K_μ[3, 1, 1, 2, 3, 1, 1, 2] = value
        K_μ[3, 1, 2, 1, 3, 1, 2, 1] = value
        K_μ[3, 1, 2, 2, 3, 1, 2, 2] = value
        K_μ[3, 2, 1, 1, 3, 2, 1, 1] = value
        K_μ[3, 2, 1, 2, 3, 2, 1, 2] = value
        K_μ[3, 2, 2, 1, 3, 2, 2, 1] = value
        K_μ[3, 2, 2, 2, 3, 2, 2, 2] = value

        value =
            (h[2] * h[3]) / (9 * h[1]) +
            (2 * h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (9 * h[3])
        K_μ[2, 1, 1, 1, 2, 1, 1, 1] = value
        K_μ[2, 1, 1, 2, 2, 1, 1, 2] = value
        K_μ[2, 1, 2, 1, 2, 1, 2, 1] = value
        K_μ[2, 1, 2, 2, 2, 1, 2, 2] = value
        K_μ[2, 2, 1, 1, 2, 2, 1, 1] = value
        K_μ[2, 2, 1, 2, 2, 2, 1, 2] = value
        K_μ[2, 2, 2, 1, 2, 2, 2, 1] = value
        K_μ[2, 2, 2, 2, 2, 2, 2, 2] = value

        value =
            (2 * h[2] * h[3]) / (9 * h[1]) +
            (h[1] * h[3]) / (9 * h[2]) +
            (h[1] * h[2]) / (9 * h[3])
        K_μ[1, 1, 1, 1, 1, 1, 1, 1] = value
        K_μ[1, 1, 1, 2, 1, 1, 1, 2] = value
        K_μ[1, 1, 2, 1, 1, 1, 2, 1] = value
        K_μ[1, 1, 2, 2, 1, 1, 2, 2] = value
        K_μ[1, 2, 1, 1, 1, 2, 1, 1] = value
        K_μ[1, 2, 1, 2, 1, 2, 1, 2] = value
        K_μ[1, 2, 2, 1, 1, 2, 2, 1] = value
        K_μ[1, 2, 2, 2, 1, 2, 2, 2] = value
    else
    end
    return K_μ
end

@testset "Brick module" begin

    @testset "Gauss–Legendre integration" begin
        order = 2
        for d = 1:3
            @testset "Gauss-Legendre integration, $(d)d" begin
                for n in map(Tuple, CartesianIndices(Tuple(fill(1:(2*order-1), d))))
                    h = Tuple([1.1, 1.2, 1.3][1:d])
                    actual = integrate(x -> prod(x .^ n), h)
                    n1 = n .+ 1
                    expected = prod(((h ./ 2) .^ n1 .- (h ./ (-2)) .^ n1) ./ n1)
                    @test actual ≈ expected rtol = 1e-15 atol = 1e-15
                end
            end
        end
    end

    @testset "Node numbering" begin
        for d = 1:3
            h = Tuple([1.1, 1.2, 1.3][1:d])
            ℒ = cell_vertices(d)
            for p ∈ ℒ
                x = h .* (-1) .^ Tuple(p) ./ 2
                N = shape(x, h)
                for q ∈ ℒ
                    @test N[q] == Float64(p == q)
                end
            end
        end
    end

    @testset "Brick element operators" begin
        for d = 2:3
            @testset "Brick element operators, $(d)d" begin
                # Note: reference values where computed with maxima
                # (see `ref/nd_brick_elasticity.mac`).
                h = Tuple([1.1, 1.2, 1.3][1:d])
                C = Hooke{d,Float64}(5.6, 0.3)

                @testset "Average gradient_operator, $(d)d" begin
                    D_act = avg_gradient_operator(h)
                    D_exp = expected_avg_gradient_operator(h)
                    @test D_act ≈ D_exp rtol = 1e-15 atol = 1e-15
                end

                @testset "Average strain-displacement matrix, $(d)d" begin
                    B_act = avg_strain_displacement_operator(h)
                    B_exp = expected_avg_strain_displacement_operator(h)
                    @test B_act ≈ B_exp rtol = 1e-15 atol = 1e-15
                end

                @testset "Stiffness operator, $(d)d" begin
                    K_act = stiffness_operator(h, C)
                    K_λ = expected_K_λ(h)
                    K_μ = expected_K_μ(h)
                    K_exp = C.λ * K_λ + C.μ * K_μ
                    @test K_act ≈ K_exp rtol = 1e-15 atol = 1e-15
                end
            end
        end
    end
end


using DualNumbers, FinancialFFT, FinancialMonteCarlo, BenchmarkTools

A = 400.0;
N = 14;

method_cm = CarrMadanMethod(A, N);
method_lewis = LewisMethod(A, 2^N);
method_cm_lewis = CarrMadanLewisMethod(A, N);
S0 = 100.0;
K = 101.0;
r = dual(0.02, 1.0);
#r = 0.02;
T = 1.0;
d = 0.0;
#sigma=dual(0.2,1.0);
sigma = 0.2;
lam = 15.0;
mu1 = 0.03;
sigma1 = 0.02;
zero_rate = ZeroRate(r);

#Model=MertonProcess(sigma,lam,mu1,sigma1,Underlying(S0,d));
Model = BlackScholesProcess(sigma, Underlying(S0, d));

EUData = EuropeanOption(T, K)
Nsim = 10000;
Nstep = 30;
mc = MonteCarloConfiguration(Nsim, Nstep);

@btime pricer(Model, zero_rate, method_cm, EUData);
@btime pricer(Model, zero_rate, method_lewis, EUData);
@btime pricer(Model, zero_rate, method_cm_lewis, EUData);


export iau2000a, nu2000k, fund_args

"""
    iau2000a(jd)

Compute the forced nutation of the non-rigid earth based on the IAU 2000A nutation model.

# Arguments
- `jd_high::Real`: High order part of the TT Julian date
- `jd_low::Real=0.0`: Low order part of the TT Julian date

# Return
`(dpsi,deps)` where
- `dpsi`: Nutation (luni-solar + planetary) in longitude, in radians
- `deps`: Nutation (luni-solar + planetary) in obliquity, in radians
"""
function iau2000a(jd_high::Real, jd_low::Real=0.0)
    # Interval between fundamental epoch J2000.0 and given date
    t = ((jd_high - T0) + jd_low) / 36525.0

    # Compute fundamental arguments in radians
    a = fund_args(t)

    Δϕ_ls = 0.0
    Δε_ls = 0.0

    @inbounds @simd for i in 1:size(nals_a)[1]
        arg = nals_a[i, 1] * a[1] +
              nals_a[i, 2] * a[2] +
              nals_a[i, 3] * a[3] +
              nals_a[i, 4] * a[4] +
              nals_a[i, 5] * a[5]
        sarg, carg = sincos(arg)
        Δϕ_ls += (cls_a[i, 1] + cls_a[i, 2] * t) * sarg + cls_a[i, 3] * carg
        Δε_ls += (cls_a[i, 4] + cls_a[i, 5] * t) * carg + cls_a[i, 6] * sarg
    end

    # Mean anomaly of the Moon.
    al = mod2pi(2.35555598 + 8328.6914269554 * t)
    # Mean anomaly of the Sun.
    alsu = mod2pi(6.24006013 + 628.301955 * t)
    #  Mean argument of the latitude of the Moon.
    af = mod2pi(1.627905234 + 8433.466158131 * t)
    #  Mean elongation of the Moon from the Sun.
    ad = mod2pi(5.198466741 + 7771.3771468121 * t)
    # Mean longitude of the ascending node of the Moon.
    aom = mod2pi(2.18243920 - 33.757045 * t)
    # General accumulated precession in longitude.
    apa = (0.02438175 + 0.00000538691 * t) * t
    # Planetary longitudes, Mercury through Neptune (Souchay et al. 1999).
    alme = mod2pi(4.402608842 + 2608.7903141574 * t)
    alve = mod2pi(3.176146697 + 1021.3285546211 * t)
    alea = mod2pi(1.753470314 + 628.3075849991 * t)
    alma = mod2pi(6.203480913 + 334.0612426700 * t)
    alju = mod2pi(0.599546497 + 52.9690962641 * t)
    alsa = mod2pi(0.874016757 + 21.3299104960 * t)
    alur = mod2pi(5.481293871 + 7.4781598567 * t)
    alne = mod2pi(5.321159000 + 3.8127774000 * t)

    Δϕ_pl = 0.0
    Δε_pl = 0.0

    @inbounds @simd for i in 1:size(napl_a)[1]
        arg = napl_a[i, 1] * al +
              napl_a[i, 2] * alsu +
              napl_a[i, 3] * af +
              napl_a[i, 4] * ad +
              napl_a[i, 5] * aom +
              napl_a[i, 6] * alme +
              napl_a[i, 7] * alve +
              napl_a[i, 8] * alea +
              napl_a[i, 9] * alma +
              napl_a[i, 10] * alju +
              napl_a[i, 11] * alsa +
              napl_a[i, 12] * alur +
              napl_a[i, 13] * alne +
              napl_a[i, 14] * apa
        sarg, carg = sincos(arg)
        Δϕ_pl += cpl_a[i, 1] * sarg + cpl_a[i, 2] * carg
        Δε_pl += cpl_a[i, 3] * sarg + cpl_a[i, 4] * carg
    end

    # Scale Results
    scaling_factor = 1.0e-7 * ASEC2RAD

    # Return result!
    return (Δϕ_ls + Δϕ_pl, Δε_ls + Δε_pl) .* scaling_factor
end

"""
    nu2000k(jd)

Compute the forced nutation of the non-rigid earth based on the NU2000K nutation model.

# Arguments
- `jd_high::Real`: High order part of the TT Julian date
- `jd_low::Real=0.0`: Low order part of the TT Julian date

# Return
`(dpsi,deps)` where
- `dpsi`: Nutation (luni-solar + planetary) in longitude, in radians
- `deps`: Nutation (luni-solar + planetary) in obliquity, in radians
"""
function nu2000k(jd_high::Real, jd_low::Real=0.0)
    # Interval between fundamental epoch J2000.0 and given date
    t = ((jd_high - T0) + jd_low) / 36525.0

    # Compute fundamental arguments in radians
    a = fund_args(t)

    Δϕ_ls = 0.0
    Δε_ls = 0.0

    @inbounds @simd for i in 1:size(nals_k)[1]
        arg = nals_k[i, 1] * a[1] +
              nals_k[i, 2] * a[2] +
              nals_k[i, 3] * a[3] +
              nals_k[i, 4] * a[4] +
              nals_k[i, 5] * a[5]
        sarg, carg = sincos(arg)
        Δϕ_ls += (cls_k[i, 1] + cls_k[i, 2] * t) * sarg + cls_k[i, 3] * carg
        Δε_ls += (cls_k[i, 4] + cls_k[i, 5] * t) * carg + cls_k[i, 6] * sarg
    end

    # Planetary longitudes, Mercury through Neptune, wrt mean dynamical
    # ecliptic and equinox of J2000, with high order terms omitted
    # (Simon et al. 1994, 5.8.1-5.8.8).
    alme = mod2pi(4.402608842461 + 2608.790314157421 * t)
    alve = mod2pi(3.176146696956 + 1021.328554621099 * t)
    alea = mod2pi(1.753470459496 + 628.307584999142 * t)
    alma = mod2pi(6.203476112911 + 334.061242669982 * t)
    alju = mod2pi(0.599547105074 + 52.969096264064 * t)
    alsa = mod2pi(0.874016284019 + 21.329910496032 * t)
    alur = mod2pi(5.481293871537 + 7.478159856729 * t)
    alne = mod2pi(5.311886286677 + 3.813303563778 * t)
    # General precession in longitude (Simon et al. 1994), equivalent to 5028.8200 arcsec/cy at J2000.
    apa = (0.024380407358 + 0.000005391235 * t) * t

    Δϕ_pl = 0.0
    Δε_pl = 0.0

    @inbounds @simd for i in 1:size(napl_k)[1]
        arg = napl_k[i, 1] * a[1] +
              napl_k[i, 2] * a[2] +
              napl_k[i, 3] * a[3] +
              napl_k[i, 4] * a[4] +
              napl_k[i, 5] * a[5] +
              napl_k[i, 6] * alme +
              napl_k[i, 7] * alve +
              napl_k[i, 8] * alea +
              napl_k[i, 9] * alma +
              napl_k[i, 10] * alju +
              napl_k[i, 11] * alsa +
              napl_k[i, 12] * alur +
              napl_k[i, 13] * alne +
              napl_k[i, 14] * apa
        sarg, carg = sincos(arg)
        Δϕ_pl += cpl_k[i, 1] * sarg + cpl_k[i, 2] * carg
        Δε_pl += cpl_k[i, 3] * sarg + cpl_k[i, 4] * carg
    end

    # Scale Results
    scaling_factor = 1.0e-7 * ASEC2RAD

    # Return result!
    return (Δϕ_ls + Δϕ_pl, Δε_ls + Δε_pl) .* scaling_factor
end