using LinearAlgebra, SparseArrays, Plots include("../p_space.jl") include("../ho_basis.jl") include("../berggren.jl") println("No of threads = ", Threads.nthreads()) atol = 10^-5 maxevals = 10^5 R_cutoff = 16 Λ = 0 m = 1.0 μ = m/2 # due to simple relative coordinates vertices = [0, 2 - 0.2im, 3, 4] subdivisions = [15, 10, 10] ks, ws = get_mesh(vertices, subdivisions) jmax = 4 tri((j1, j2)) = triangle_ineq(j1, j2, Λ) js = collect(Iterators.filter(tri, iter_prod(0:jmax, 0:jmax))) basis = iter_prod(js, zip(ks, ws), zip(ks, ws)) # basis = ((j1, j2), (k1, w1), (k2, w2)) basis_size = length(js) * length(ks)^2 @assert length(basis) == basis_size "Something wrong with the basis" println("Basis size = $basis_size") V_of_r(r) = 2 * exp(-(r-3)^2 / (1.5)^2) V_l(j, k, kp) = Vl_mat_elem(V_of_r, j, k, kp; atol=atol, maxevals=maxevals, R_cutoff=R_cutoff) # generate p-space bases (actually identity matrices) @time "p-space bases" ps_bases = [Matrix(spdiagm(1 ./ sqrt.(ws))) for _ in 0:jmax] # generate Berggren bases @time "Berggren bases" begin berg_bases = Vector{Matrix{ComplexF64}}(undef, jmax + 1) for j in 0:jmax _, berg_basis = eigen(get_H_matrix((k, kp) -> V_l(j, k, kp), ks, ws, μ); permute=false, scale=false) N_berg = sum(berg_basis.^2 .* ws, dims=1) berg_bases[1 + j] = berg_basis ./ transpose(sqrt.(N_berg)) end end @time "BB tranform matrix" begin U_blocks = [kron(berg_bases[1 + j1] .* ws, berg_bases[1 + j2] .* ws) for (j1, j2) in js] U = blockdiag(sparse.(U_blocks)...) end @time "In p-space" T_cross_PS = get_2p_p1p2_matrix(length(ks), js, Λ, ps_bases, ps_bases, ws) @time "In BB" T_cross_BB = get_2p_p1p2_matrix(length(ks), js, Λ, berg_bases, berg_bases, ws) @time "Basis transform" T_cross_transformed = transpose(U) * T_cross_PS * U diff = abs.(T_cross_transformed .- T_cross_BB) println("Max error = $(maximum(diff))") #@time "In in HO" T_cross_HO = get_2p_p1p2_matrix(n1s, l1s, n2s, l2s, Λ, μω, μω; dtype=ComplexF64)