mod_MPA.F Source File


Source Code

!
! License-Identifier: GPL
!
! Copyright (C) 2019 The Yambo Team
!
! Authors (see AUTHORS file for details): DALV,AF
!
! headers
!
#include <y_memory.h>
#include <dev_defs.h>
!
!------------------------------------------------------------
! Multipole interpolation:
!                         - analytical solution for 1-3 poles
!                         - Linear solver for n poles*
!                         - Pade-Thiele solver for n poles*
!
! Failure condition for the position of the poles*
!
! *DA. Leon et al, PRB 104, 115157 (2021)
!
! Notes:
!
!   1) X(w) is approximated as a sum of poles
!   2) Form of one pole: -P/(w**2-Q) = 2*E*R/(w**2-E**2)
!   3) The input are two w and X(w) for each pole
!   4) The output are E and R coefficients
!   5) Use real(R), imaginary(I) or complex(C) w
!
! **The module works for scalar polarizabilities, so if 
!   one wants the solution for matrix element X(G,G',q)
!   then RQ_solver should be called for each G, G' and q.  
!------------------------------------------------------------
!
! GS forced Padé in double precision, this strongly stabilizes the MPA
!    change of parallism with Padé in SP leads to QP differences in meV range or larger
!    with Padé in DP, QP differences are within the SP error
!
!> @brief Multipole approximation (MPA) of the polarizability
!!
!! X(w) is fitted, for each matrix element, with a sum of \(n\) poles
!! \[ X(\omega)=\sum_{i=1}^{n} \frac{2 E_i R_i}{\omega^2-E_i^2} \]
!! sampled on two frequency lines (\(n\) frequencies per line). The pole positions \(E_i\) are obtained by one of
!! the solvers selected by `mpa_sol` in `mpa_RE_solver`:
!!
!! - `LA`: linear algebra solution of the system for the denominator polynomial
!! - `PT`: Pade-Thiele continued fraction (default)
!! - `PR`: Pade-Thiele on `MPA_n_refine` poles followed by a least squares refinement of the poles (see `mpa_E_solver_PadeRefine`)
!!
!! The residues \(R_i\) are then obtained by a linear least squares fit (`mpa_R_fit`).
!! Poles that are outside the frequency range or that coincide are discarded (`mpa_cond`).
!! All the fits are done in double precision, also when the code is compiled in single precision.
!!
!! In the notation of Sesti et al. (Eq. 8) the model is
!! \[ \chi_{GG'}(\mathbf{q},\omega)=\sum_{p=1}^{n_p}\frac{2R_{p,GG'}(\mathbf{q})\,\Omega_{p,GG'}(\mathbf{q})}{\omega^2-\left[\Omega_{p,GG'}(\mathbf{q})\right]^2} \]
!! where \(\Omega_p\) and \(R_p\) are the poles and the residues (`E` and `R` in this module), determined for each
!! \(\mathbf{q}\) and \(GG'\) from points sampled in the complex plane \(z=\omega+i\bar\omega\).
!! The MPA reduces to the plasmon-pole approximation for \(n_p=1\) and approaches the full-frequency result for increasing \(n_p\).
!!
!! Refs.:
!!
!! - D.A. Leon et al, PRB 104, 115157 (2021), [arXiv:2109.01532](https://arxiv.org/abs/2109.01532)
!! - G. Sesti et al, npj Comput. Mater. (2026), [doi:10.1038/s41524-026-02268-7](https://doi.org/10.1038/s41524-026-02268-7), [arXiv:2508.06930](https://arxiv.org/abs/2508.06930)
module mpa_m
 !
 use pars,            ONLY:SP,DP,cZERO,cONE,cI
 use functions,       ONLY:NAN
 use matrix,          ONLY:PAR_matrix
 use gpu_m,           ONLY:have_gpu
 use y_memory_alloc
 !
 implicit none

 complex(SP),      allocatable, target :: MPA_freqs(:)
   !! Frequencies at which X is sampled
 type(PAR_matrix), allocatable, target :: MPA_E_par(:)
   !! Parallel matrix of the pole positions \(E_i\)
 type(PAR_matrix), allocatable, target :: MPA_R_par(:)
   !! Parallel matrix of the residues \(R_i\)
 complex(SP), allocatable DEV_ATTR, target :: MPA_E_par_d(:,:,:)
   !! Device copy of the pole positions
 complex(SP), allocatable DEV_ATTR, target :: MPA_R_par_d(:,:,:)
   !! Device copy of the residues
 !
 ! Number of poles used by the "PR" solver (Pade-Thiele on a subset of the
 ! frequencies followed by a least-squares refinement of the pole positions)
 integer :: MPA_n_refine=4
   !! Number of poles of the `PR` solver (the solver is used only if the number of poles is larger)
 integer :: MPA_refine_maxit=30
   !! Maximum number of Levenberg-Marquardt iterations of the `PR` solver
 !
 public :: mpa_RE_solver,err_func_X
 public :: MPA_alloc,MPA_free
 !
contains
 !
 !> @brief Allocate the MPA frequencies
 subroutine MPA_alloc(d)
   implicit none
   integer :: d(4)
     !! Dimensions, `d(4)` is the number of frequencies
   !
   call MPA_free()
   YAMBO_ALLOC(MPA_freqs,(d(4)))
 end subroutine

 !> @brief Deallocate the MPA frequencies
 subroutine MPA_free()
   YAMBO_FREE(MPA_freqs)
 end subroutine
 !
 !> @brief Condition the pole of the one-pole solution
 !!
 !! Converts \(E^2\) into \(E\) enforcing positive real part and negative imaginary part (time ordering).
 !! Unphysical poles are replaced and flagged in `PPcond_rate`.
 subroutine mpa_cond1(z, E, PPcond_rate)
   !
   complex(DP), intent(in)     :: z(2)
     !! Squared frequencies
   complex(DP), intent(inout)  :: E
     !! On input \(E^2\), on output the conditioned pole \(E\)
   real(DP),    intent(out)    :: PPcond_rate
     !! 1 if the pole was replaced, 0 otherwise
   !
   PPcond_rate=0._DP
   !
   if(NAN(real(abs(E),kind=SP)) .or. abs(E)<0.00001_DP) then
     E=abs(z(2))-cI*epsilon(1._DP) 
     PPcond_rate=1._DP  
   elseif(real(E)>0._DP) then     
     E = sqrt(E)
   else
     E = sqrt(-conjg(E)) ! DALV: PPA uses E = 1._SP
     PPcond_rate=1._DP 
   endif
   !
   ! DALV: since MPA uses complex poles 
   !       we need to guarantee the time ordering
   !
   if(real(E)<0._DP) E=-E
   if(aimag(E)>-epsilon(1._DP)) E=real(E,SP)-cI*epsilon(1._DP)
   !
 end subroutine mpa_cond1
 !
 !> @brief Check whether the pole `i` must be discarded
 !!
 !! A pole is discarded if its real part exceeds `wmax` or if it is closer than `thr` to a previous pole `j<i`.
 !! In the second case the previous pole is replaced by the average of the two.
 function pole_is_out(i, wmax, thr, E)
   implicit none
   !
   integer ,    intent(in)     :: i
     !! Index of the pole to check
   real(DP),    intent(in)     :: wmax, thr
     !! `wmax`: Maximum allowed real part; `thr`: Minimum separation between poles
   complex(DP), intent(inout)  :: E(:)
     !! Pole positions (the pole `j<i` can be modified)
   !
   integer     :: j
   logical     :: pole_is_out
   !
   pole_is_out=.false.
   !
   if(real(E(i),SP)>wmax) pole_is_out=.true.
   !
   j=1
   do while(j<i.and..not.pole_is_out)
     !
     if(abs(real(E(i),DP)-real(E(j),DP))<thr) then
       !
       pole_is_out=.true.
       !
       if(abs(real(E(j),DP))>max(abs(aimag(E(j))),abs(aimag(E(i))))) then
         E(j)=(real(E(j),DP)+real(E(i),DP))/2._DP-cI*max(abs(aimag(E(j))),abs(aimag(E(i))))
       else
         E(j)=(real(E(j),DP)+real(E(i),DP))/2._DP-cI*min(abs(aimag(E(j))),abs(aimag(E(i))))
       endif
       !
     endif
     !
     j=j+1
     !
   enddo
   !
 endfunction pole_is_out
 !
 !> @brief Condition the poles obtained from a solver
 !!
 !! Takes the roots \(E_i^2\) of the denominator polynomial, discards the poles outside
 !! `1.5*max(w)` or coincident with other poles (see `pole_is_out`), and moves the discarded ones to the end of the list.
 subroutine mpa_cond(np, npr, z, E, PPcond)
   !
   integer,     intent(in)     :: np
     !! Number of poles
   integer,     intent(out)    :: npr
     !! Number of poles kept
   logical,     intent(out)    :: PPcond(np)
     !! `.true.` if the pole has been discarded/modified
   complex(DP), intent(in)     :: z(2*np)
     !! Squared frequencies
   complex(DP), intent(inout)  :: E(np)
     !! On input \(E^2\), on output the poles; the discarded ones are set to \(1-i\epsilon\)
   !
   integer     :: i, j
   complex(DP) :: Eaux(np)
   real(DP)    :: wmax, thr=0.00001_DP
   !
   npr=np
   !
   !DALV: we were using the square before, 
   !      now we use 1.5* the extreme of the interval
   wmax=maxval(sqrt(real(z,SP)))*1.5_DP 
   !
   i=1
   do while(i<=npr)
     !
     Eaux(i)=sqrt(E(i))
     !
     do while(pole_is_out(i,wmax,thr,Eaux).and.i<=npr)
       Eaux(i)=sqrt(E(npr))
       PPcond(npr)=.true.
       npr=npr-1
     enddo
     !
     if(.not.pole_is_out(i,wmax,thr,Eaux).and.abs(real(Eaux(i),SP))<abs(aimag(Eaux(i)))) PPcond(i)=.false.
     Eaux(i)=max(abs(real(Eaux(i),SP)),abs(aimag(Eaux(i))))-cI*min(abs(real(Eaux(i),SP)),abs(aimag(Eaux(i))))
     !
     i=i+1
     !
   enddo
   !
   E(1:npr)=Eaux(1:npr)
   if(npr<np) then
     E(npr+1:np)=1._DP-cI*epsilon(1._DP)
     PPcond(npr+1:np)=.true.
   endif
   !
 end subroutine mpa_cond
 !
 !> @brief Analytical pole position for the 1-pole fit
 subroutine mpa_E_1p_solver(z, x, E, PPcond_rate) 
   !
   ! DALV: analytical form of the E position of the 1 pole solution
   !
   implicit none
   !
   complex(DP), intent(in)   :: z(2)
     !! Squared frequencies
   complex(DP), intent(in)   :: x(2)
     !! X at the two frequencies
   complex(DP), intent(out)  :: E
     !! Pole position
   real(DP),    intent(out)  :: PPcond_rate
     !! 1 if the pole was replaced by `mpa_cond1`
   !
   E = (x(1)*z(1)**2 -x(2)*z(2)**2 )/(x(1)-x(2))
   call mpa_cond1(z, E, PPcond_rate)
   !
 end subroutine mpa_E_1p_solver
 !
 !> @brief Residues by linear least squares
 !!
 !! Solves (with `zgelsd`) the overdetermined system \(X(\omega_k)=\sum_i 2E_iR_i/(\omega_k^2-E_i^2)\) over all the `2*np` frequencies
 !! for the first `npr` poles. The remaining residues are set to zero.
 subroutine mpa_R_fit(np,npr, w, xq, R, E)
   implicit none
   integer,     intent(in)   :: np, npr
     !! `np`: Number of poles of the model; `npr`: Number of poles kept
   complex(DP), intent(in)   :: w(2*np)
     !! Frequencies
   complex(DP), intent(in)   :: xq(2*np), E(np)
     !! `xq`: X at the frequencies; `E`: Pole positions
   complex(DP), intent(out)  :: R(np)
     !! Residues
   !
   complex(DP)               :: A(2*np,npr), B(2*np)
   integer                   :: i, k, info, rank, lwork, lwmax 
   parameter                 (lwmax=1000)
   real(DP)                  :: rcond
   integer                   :: iwork(3*npr*0+11*npr)
   real(DP)                  :: S(npr), rwork(10*npr+2*npr*25+8*npr*0+3*25+26*26)
   complex(DP)               :: work(lwmax)
   !
   do k = 1, 2*np
     B(k) = xq(k)
     do i = 1, npr
       A(k,i) = 2._DP*E(i)/(w(k)**2 -E(i)**2)
     enddo
   enddo 
   !
   rcond=-1._DP
   !
! #ifdef _DOUBLE
   call zgelsd(2*np,npr,1,A,2*np,B,2*np,S,rcond,rank,work,-1,   rwork,iwork,info)
   lwork=min(lwmax,int(work(1)))
   call zgelsd(2*np,npr,1,A,2*np,B,2*np,S,rcond,rank,work,lwork,rwork,iwork,info)
! #else
!   call cgelsd(2*np,npr,1,A,2*np,B,2*np,S,rcond,rank,work,-1,   rwork,iwork,info)
!   lwork=min(lwmax,int(work(1)))
!   call cgelsd(2*np,npr,1,A,2*np,B,2*np,S,rcond,rank,work,lwork,rwork,iwork,info)
! #endif
   !
   R(1:npr)=B(1:npr)
   if (npr<np) R(npr+1:np)=0.0_DP
   !
 end subroutine mpa_R_fit
 !
 !> @brief Residues by direct solution of the `np x np` system
 !!
 !! Not used by the current solvers (`mpa_R_fit` is used instead).
 subroutine mpa_R_solver(np, w, xq, R, E, rcond)
   implicit none
   integer,     intent(in)   :: np
     !! Number of poles
   complex(DP), intent(in)   :: w(2*np)
     !! Frequencies (only the first `np` are used)
   complex(DP), intent(in)   :: xq(2*np), E(np)
     !! `xq`: X at the frequencies; `E`: Pole positions
   complex(DP), intent(out)  :: R(np)
     !! Residues
   real(DP),    intent(out)  :: rcond
     !! Reciprocal condition number of the system
   !
   complex(DP) :: A(np,np)
   real(DP)    :: anorm
   integer     :: i, k, info, ind(np)    
   complex(DP) :: rwork(2*np), work(2*np)
   !
   anorm=abs(2._DP*E(1)/(w(1)**2 -E(1)**2))
   !
   do k = 1, np
     R(k) = xq(k)
     do i = 1, np
       A(k,i) = 2._DP*E(i)/(w(k)**2 -E(i)**2)
       if(abs(A(k,i))>anorm) anorm=abs(A(k,i))
     enddo
   enddo 
   !
!  #ifdef _DOUBLE
   call zgecon('I', np, A, np, anorm, rcond, work, rwork, info)
   call zgesv(np, 1, A, np, ind, R, np, info) 
!  #else
!    call cgecon('I', np, A, np, anorm, rcond, work, rwork, info)
!    call cgesv(np, 1, A, np, ind, R, np, info) 
!  #endif
   !
 end subroutine mpa_R_solver
 !
 !> @brief Pole positions by the linear algebra (`LA`) solver
 !!
 !! Builds the polynomial that has the poles as roots from the two sets of `np` frequencies, by solving
 !! two linear systems. The poles are the eigenvalues of the companion matrix and are then conditioned with `mpa_cond`.
 subroutine mpa_E_solver(np, npr, z, xq, E, PPcond, econd)
   implicit none
   integer,     intent(in)   :: np
     !! Number of poles
   integer,     intent(out)  :: npr
     !! Number of poles kept
   complex(DP), intent(in)   :: z(2*np)
     !! Squared frequencies
   complex(DP), intent(in)   :: xq(2*np)
     !! X at the frequencies
   complex(DP), intent(out)  :: E(np)
     !! Pole positions
   logical,     intent(out)  :: PPcond(np)
     !! `.true.` if the pole has been discarded/modified
   real(DP),    intent(out)  :: econd(2)
     !! Reciprocal condition numbers of the two linear systems
   !
   complex(DP) :: Z1(np,np), Z2(np,np), M1(np,np), M2(np,np), v1(np)
   complex(DP) :: M(np,np), b(np), Companion(np,np)
   complex(DP) :: rwork(2*np),work(2*np),VR(np),VL(np),S(np),RCE(np,np),RCV(np,np),ABNRM
   integer     :: i, j, info, ind(np), ILO,IHI    
   real(DP)    :: rcond, anorm, Wm
   !
   PPcond(:)=.true.
   Wm = maxval(abs(z(1:np)))
   !
   do j = 1, np
     ind(j) = j
     v1(j)  = xq(j   )*z(j   )**np 
     b (j)  = xq(j+np)*z(j+np)**np 
     Z1(j,1) = 1._DP
     Z2(j,1) = 1._DP  
     M1(j,1) = -xq(j) 
     M2(j,1) = -xq(j+np) 
     M (j,1) = 0._DP
     M (j,j) = 1._DP
     !
     do i = 2, np
       Z1(j,i) =  (z(j)/Wm)**(i-1)
       Z2(j,i) =  (z(j+np)/Wm)**(i-1)
       M1(j,i) = -xq(j   )*Z1(j,i)
       M2(j,i) = -xq(j+np)*Z2(j,i)
       if(i/=j) M(j,i) = 0._DP 
     enddo
     !
   enddo
   !
   anorm=abs(Z1(1,1))
   !
   do j = 1, np
     do i = 1, np
       if(abs(Z1(j,i))>anorm) anorm=abs(Z1(j,i))
     enddo
   enddo
   !
! #ifdef _DOUBLE
   call zgecon('I', np, Z1, np, anorm, econd(1), work, rwork, info)
   call zgesv(np, np, Z1, np, ind, M, np,info) 
! #else
!   call cgecon('I', np, Z1, np, anorm, econd(1), work, rwork, info)
!   call cgesv(np, np, Z1, np, ind, M, np,info) 
! #endif
   !
   M  = matmul(Z2,M)
   M2 = matmul(M,M1) - M2
   b  = matmul(M,v1) - b
   !
   M = 0.0_DP
   !
   do i =1,np
     M(i,i) = 1.0_DP
   enddo
   !
   anorm=abs(M2(1,1))
   !
   do j = 1, np
     do i = 1, np
       if(abs(M2(j,i))>anorm) anorm=abs(M2(j,i))
     enddo
   enddo
   !
! #ifdef _DOUBLE
   call zgecon('I', np, M2, np, anorm, econd(2), work, rwork, info)
   call zgesv(np, 1, M2, np, ind, b, np, info)
! #else
!   call cgecon('I', np, M2, np, anorm, econd(2), work, rwork, info)
!   call cgesv(np, 1, M2, np, ind, b, np, info)
! #endif
   !
   Companion=0._DP
   Companion(1,np) = -b(1)
   !
   do i = 2, np
     Companion(i,np)  = -b(i)/Wm**(i-1)
     Companion(i,i-1) = 1._DP
   enddo
   !
! #ifdef _DOUBLE
   call zgeev( 'N', 'N', np, Companion, np, E, VL, 1, VR, 1, work, 2*np, rwork, info )
! #else
!   call cgeev( 'N', 'N', np, Companion, np, E, VL, 1, VR, 1, work, 2*np, rwork, info )
! #endif
   !
   ! DALV: here we need to force real(E) to be positive.
   ! This is because of the way the residue integral is performed, later.
   !
   call mpa_cond(np, npr, z, E, PPcond)
   !
 end subroutine mpa_E_solver
 !
 !> @brief Pole positions by the Pade-Thiele (`PT`) solver
 !!
 !! Builds the Pade-Thiele continued fraction interpolating all the `2*np` points. The poles are the roots of the
 !! denominator (eigenvalues of the companion matrix) and are then conditioned with `mpa_cond`.
 subroutine mpa_E_solver_Pade(np, npr, z, X, E, PPcond)
   implicit none
   integer,     intent(in)   :: np
     !! Number of poles
   integer,     intent(out)  :: npr
     !! Number of poles kept
   complex(DP), intent(in)   :: z(2*np)
     !! Squared frequencies
   complex(DP), intent(in)   :: X(2*np)
     !! X at the frequencies
   complex(DP), intent(out)  :: E(np)
     !! Pole positions
   logical,     intent(out)  :: PPcond(np)
     !! `.true.` if the pole has been discarded/modified
   !
   complex(DP) :: c(2*np), b(np+1), Companion(np,np)
   complex(DP) :: c_m1(2*np), b_m1(np+1), b_m2(np+1)
   complex(DP) :: rwork(2*np),work(2*np),VR(np),VL(np)
   integer     :: i, j, info   
   real(DP)    :: rcond, anorm, Wm
   !
   PPcond(:)=.true.
   b_m1(1)=1._DP
   b_m2(1)=1._DP
   b_m1(2:)=0._DP
   b_m2(2:)=0._DP
   b=b_m1
   c=X
   c_m1=c
   !
   do i=2, 2*np
     !
     do j=i, 2*np
       c(j)=(c_m1(i-1)-c_m1(j))/((z(j)-z(i-1))*c_m1(j))
     enddo
     !
     c_m1=c
     !
     do j=1, np+1
       b(j)=b_m1(j)-z(i-1)*c(i)*b_m2(j)
     enddo
     !
     do j=1, np
       b_m2(np+2-j)=c(i)*b_m2(np+1-j)
     enddo
     !
     do j=1, np
       b(j+1)=b(j+1)+b_m2(j+1)
     enddo
     !
     b_m2=b_m1
     b_m1=b
     !
   enddo
   !
   Companion=0._SP
   Companion(1,np) = -b(1)/b(np+1)
   !
   do i = 2, np
     Companion(i,np)  = -b(i)/b(np+1)
     Companion(i,i-1) = 1._DP
   enddo
   !
! #ifdef _DOUBLE
   call zgeev( 'N', 'N', np, Companion, np, E, VL, 1, VR, 1, work, 2*np, rwork, info )
! #else
!   call cgeev( 'N', 'N', np, Companion, np, E, VL, 1, VR, 1, work, 2*np, rwork, info )
! #endif
   !
   ! DALV: here we need to force real(E) to be positive.
   ! This is because of the way the residue integral is performed, later.
   !
   call mpa_cond(np, npr, z, E, PPcond)
   !
 end subroutine mpa_E_solver_Pade
 !
 !> @brief Residual of the fit with `n` poles
 !!
 !! The residues are eliminated with a linear least squares fit (`mpa_R_fit`, variable projection).
 !! The residual is normalized to `xmax` and returned as real and imaginary parts.
 subroutine mpa_pole_residual(np, n, w, x, xmax, E, r, cost)
   !
   ! Residual of the fit of x(w) with n poles E. The residues are eliminated
   ! by a linear least squares fit (variable projection).
   !
   implicit none
   integer,     intent(in)   :: np, n
     !! `np`: Half of the number of frequencies; `n`: Number of poles
   complex(DP), intent(in)   :: w(2*np), x(2*np), E(np)
     !! `w`: Frequencies; `E`: Pole positions
   real(DP),    intent(in)   :: xmax
     !! Normalization of the residual
   real(DP),    intent(out)  :: r(4*np), cost
     !! `r`: Residual (real parts followed by imaginary parts); `cost`: Sum of the squared residuals
   !
   integer     :: i, k
   complex(DP) :: R_fit(np), res(2*np)
   !
   call mpa_R_fit(np, n, w, x, R_fit, E)
   !
   do k = 1, 2*np
     res(k) = -x(k)
     do i = 1, n
       res(k) = res(k) + 2._DP*E(i)*R_fit(i)/(w(k)**2 -E(i)**2)
     enddo
   enddo
   res = res/xmax
   !
   r(1:2*np)      = real(res,DP)
   r(2*np+1:4*np) = aimag(res)
   cost = sum(r**2)
   !
 end subroutine mpa_pole_residual
 !
 !> @brief Pole positions by the `PR` solver: Pade-Thiele plus pole refinement
 !!
 !! A Pade-Thiele fit interpolating all the `2*np` frequencies uses `np` poles to describe data that contain fewer of them,
 !! and the spurious poles depend on the numerical noise of X (e.g. on the order of the MPI sums).
 !! Here the Pade-Thiele is built on `nfit<np` poles using a subset of `2*nfit` frequencies (evenly spaced on each line),
 !! and the poles are then refined by a Levenberg-Marquardt minimization of the misfit (`mpa_pole_residual`)
 !! over all the `2*np` frequencies. The Jacobian is computed by forward finite differences. The real part of the poles is
 !! bound to \([10^{-3},1.5\,\omega_{max}]\) and the imaginary part to be negative. The number of iterations is limited by `MPA_refine_maxit`.
 subroutine mpa_E_solver_PadeRefine(np, nfit, npr, w, x, E, PPcond)
   !
   ! A Pade-Thiele fit interpolating all the 2*np frequencies uses np poles
   ! to describe data that contain fewer of them, and the spurious poles
   ! depend on the numerical noise of x (e.g. on the order of the MPI sums).
   ! Here the Pade-Thiele is built on nfit<np poles using a subset of 2*nfit
   ! frequencies (evenly spaced on each of the two lines), and the poles are
   ! then refined by a Levenberg-Marquardt minimization of the misfit over
   ! all the 2*np frequencies.
   !
   implicit none
   integer,     intent(in)   :: np, nfit
     !! `np`: Half of the number of frequencies; `nfit`: Number of poles of the fit
   integer,     intent(out)  :: npr
     !! Number of poles kept (equal to the poles found by the Pade-Thiele)
   complex(DP), intent(in)   :: w(2*np), x(2*np)
     !! `w`: Frequencies; `x`: X at the frequencies
   complex(DP), intent(out)  :: E(np)
     !! Pole positions
   logical,     intent(out)  :: PPcond(np)
     !! Set to `.true.`
   !
   integer     :: j, l, n, it, irej, ip, info, idx(2*nfit), ipiv(2*nfit)
   complex(DP) :: w_sub(2*nfit), x_sub(2*nfit), E_sub(nfit), E_try(np)
   logical     :: PPcond_sub(nfit), accepted
   real(DP)    :: xmax, wmax, lambda, cost, cost_try, step_fd
   real(DP)    :: p(2*nfit), p_try(2*nfit), g(2*nfit), dp_step(2*nfit)
   real(DP)    :: H(2*nfit,2*nfit), Hl(2*nfit,2*nfit)
   real(DP)    :: Jac(4*np,2*nfit), r0(4*np), r1(4*np)
   !
   do l = 0, 1
     do j = 1, nfit
       idx(j+l*nfit) = 1 + l*np + nint(real(j-1,DP)*real(np-1,DP)/real(nfit-1,DP))
     enddo
   enddo
   w_sub = w(idx)
   x_sub = x(idx)
   !
   call mpa_E_solver_Pade(nfit, n, w_sub**2, x_sub, E_sub, PPcond_sub)
   !
   xmax = maxval(abs(x))
   wmax = 1.5_DP*maxval(abs(real(w,DP)))
   !
   E = 1._DP-cI*epsilon(1._DP)
   E(1:n) = E_sub(1:n)
   !
   if (n>0 .and. xmax>0._DP) then
     !
     p(1:n)     = real(E(1:n),DP)
     p(n+1:2*n) = aimag(E(1:n))
     lambda = 1.E-3_DP
     call mpa_pole_residual(np, n, w, x, xmax, E, r0, cost)
     !
     do it = 1, MPA_refine_maxit
       !
       ! Jacobian of the residual by forward differences
       do ip = 1, 2*n
         p_try = p
         step_fd = 1.E-6_DP*max(1._DP,abs(p(ip)))
         p_try(ip) = p(ip)+step_fd
         E_try = E
         E_try(1:n) = cmplx(p_try(1:n),p_try(n+1:2*n),kind=DP)
         call mpa_pole_residual(np, n, w, x, xmax, E_try, r1, cost_try)
         Jac(1:4*np,ip) = (r1-r0)/step_fd
       enddo
       !
       g = 0._DP
       H = 0._DP
       g(1:2*n)       = matmul(transpose(Jac(1:4*np,1:2*n)),r0)
       H(1:2*n,1:2*n) = matmul(transpose(Jac(1:4*np,1:2*n)),Jac(1:4*np,1:2*n))
       !
       accepted = .false.
       do irej = 1, 8
         Hl = H
         do ip = 1, 2*n
           Hl(ip,ip) = H(ip,ip)*(1._DP+lambda)+tiny(1._DP)
         enddo
         dp_step = -g
         call dgesv(2*n, 1, Hl, 2*nfit, ipiv, dp_step, 2*nfit, info)
         if (info==0) then
           p_try = p+dp_step
           p_try(1:n)     = min(max(p_try(1:n),1.E-3_DP),wmax)
           p_try(n+1:2*n) = min(max(p_try(n+1:2*n),-wmax),-epsilon(1._DP))
           E_try = E
           E_try(1:n) = cmplx(p_try(1:n),p_try(n+1:2*n),kind=DP)
           call mpa_pole_residual(np, n, w, x, xmax, E_try, r1, cost_try)
           if (cost_try<cost) accepted = .true.
         endif
         if (accepted) exit
         lambda = lambda*4._DP
       enddo
       !
       if (.not.accepted) exit
       !
       lambda = max(lambda/3._DP,1.E-10_DP)
       p = p_try
       E = E_try
       r0 = r1
       if (abs(cost-cost_try)<1.E-9_DP*cost) then
         cost = cost_try
         exit
       endif
       cost = cost_try
       !
     enddo
     !
   endif
   !
   npr = n
   PPcond(:) = .true.
   !
 end subroutine mpa_E_solver_PadeRefine
 !
 !> @brief Poles and residues of the MPA fit of X
 !!
 !! Entry point of the module. The fit is done in double precision. For `np=1` the analytical solution is used,
 !! otherwise the solver is selected by `mpa_sol`; the residues are then obtained with `mpa_R_fit`.
 subroutine mpa_RE_solver(np, w, x, R, E,mpa_sol,MPred,PPcond_rate,MP_err,cond_num)
   implicit none
   !
   integer,      intent(in)   :: np
     !! Number of poles
   complex(SP),  intent(in)   :: w(2*np), x(2*np)
     !! Frequencies
   complex(SP),  intent(out)  :: R(np), E(np)
     !! Residues
   character(2), intent(in)   :: mpa_sol
     !! Solver: `LA`, `PT` (default) or `PR`
   logical,      intent(out)  :: MPred
     !! Set to `.true.` if the number of poles was reduced
   real(SP),     intent(out)  :: PPcond_rate,MP_err
     !! `PPcond_rate`: Weight of the poles that were modified; `MP_err`: Relative error of the fit (`err_func_X`)
   !
   integer  :: i,npr
   logical  :: PPcond(np)
   real(SP) :: cond_num(2)
     !! log10 of the inverse condition numbers (`LA` solver only)
   complex( DP) :: w_dummy(2*np), x_dummy(2*np),R_dummy(np), E_dummy(np)
   real(DP) :: PPcond_rate_dummy,MP_err_dummy,cond_num_dummy(2) 
   !
   PPcond_rate_dummy=0._DP
   !
   ! GS Pass intent-in in double precision 
#ifdef _DOUBLE
   w_dummy = w
   x_dummy = x
#else
   w_dummy = cmplx(w, kind=DP)
   x_dummy = cmplx(x, kind=DP)
#endif
   !
   select case(np)
   case(1)
     !
     call mpa_E_1p_solver(w_dummy, x_dummy, E_dummy(1), PPcond_rate_dummy)
     call mpa_R_fit(1, 1, w_dummy, x_dummy, R_dummy, E_dummy)
   !
   ! DALV: we can also use the particular solutions in the cases of 2 and 3 poles
   !
   case default
     !
     if(mpa_sol=='LA') then
       call mpa_E_solver(np, npr, w_dummy**2, x_dummy, E_dummy, PPcond, cond_num_dummy)
       cond_num_dummy=log10(1._DP/cond_num_dummy)
     else if(mpa_sol=='PR' .and. np>MPA_n_refine .and. MPA_n_refine>1) then
       ! Pade-Thiele on MPA_n_refine poles + least squares refinement of the poles
       cond_num_dummy=0._DP
       call mpa_E_solver_PadeRefine(np, MPA_n_refine, npr, w_dummy, x_dummy, E_dummy, PPcond)
     else ! DALV: we use Pade-Thiele instead (mpa_sol='PT')
       cond_num_dummy=0._DP
       call mpa_E_solver_Pade(np, npr, w_dummy**2, x_dummy, E_dummy, PPcond)
     endif
     !
     if(npr<np .and. .not.(mpa_sol=='PR' .and. np>MPA_n_refine)) MPred=.true.
     !
     call mpa_R_fit(np, npr, w_dummy, x_dummy, R_dummy, E_dummy)
     !
     do i=1,np
       if(.not.PPcond(i)) PPcond_rate=PPcond_rate+abs(R_dummy(i))
     enddo
     !
     PPcond_rate_dummy=PPcond_rate_dummy/sum(abs(R_dummy(:)))
     !
  end select
   !
   MP_err_dummy=err_func_X(np, R_dummy, E_dummy, w_dummy, x_dummy)
   ! 
   ! GS Get intent-out either in single or double precision 
#ifdef _DOUBLE
   R = R_dummy
   E = E_dummy
   PPcond_rate=PPcond_rate_dummy
   MP_err=MP_err_dummy
   cond_num=cond_num_dummy
#else
   R = cmplx(R_dummy, kind=SP)
   E = cmplx(E_dummy, kind=SP)
   PPcond_rate=real(PPcond_rate_dummy, kind=SP)
   MP_err=real(MP_err_dummy, kind=SP)
   cond_num=real(cond_num_dummy, kind=SP)
#endif
   !
 end subroutine mpa_RE_solver
 !
 !
 !> @brief Error of the MPA fit
 !!
 !! Standard deviation of the fit over the `2*np` frequencies, relative to the maximum of |X| (coefficient of variation).
 function err_func_X(np, R, E, w, x)
   implicit none
   !
   integer ,    intent(in)  :: np
     !! Number of poles
   complex(DP), intent(in)  :: x(2*np), w(2*np)
     !! X at the frequencies
   complex(DP), intent(in)  :: R(np), E(np)
     !! `R`: Residues; `E`: Pole positions
   !
   complex(DP) :: aux
   real(DP)    :: err_func_X
   integer     :: i,j
   !
   err_func_X = 0._DP
   !
   do j=1, 2*np
     !
     aux = cZERO
     !
     do i=1, np
       aux = aux + 2._DP*E(i)*R(i)/(w(j)**2-E(i)**2)     
     enddo
     !
     err_func_X = err_func_X + abs( aux - x(j) )**2
     !
   enddo  
   !
   ! DALV: this is the Standard Deviation relative to the maximum: 
   !       (Coefficient of variation)
   !
   err_func_X = sqrt(err_func_X/(2._DP*np-1._DP))/maxval(abs(X(:)))
   !
 endfunction err_func_X
 !
 !
 ! ----------------------------------------------------------------
 ! ------- DALV: Not in use ---------------------------------------
 ! ----------------------------------------------------------------
 !
 subroutine mpa_RE_1p_solver(z, x, R, E, PPcond_rate) 
   !
   ! DALV: analytical form of the 1 pole solution
   !
   implicit none
   !
   complex(DP), intent(in)   :: z(2)
   complex(DP), intent(in)   :: x(2)
   complex(DP), intent(out)  :: R, E
   real(DP),    intent(out)  :: PPcond_rate
   !
   E = (x(1)*z(1)**2 -x(2)*z(2)**2 )/(x(1)-x(2))
   !
   call mpa_cond1(z, E, PPcond_rate)
   !
   R = x(2)*(z(2)**2-E**2)/(2._SP*E)
   !
 end subroutine mpa_RE_1p_solver
 !
 !
 subroutine mpa_RQ_2p_solver(w, x, r, q) 
   implicit none
   !
   complex(SP), intent(in)   :: w(4)
   complex(SP), intent(in)   :: x(4)
   complex(SP), intent(out)  :: r(2), q(2)
   !
   integer     :: i, j
   complex(SP) :: a, b, c ! DALV: coefficients of the w^2 polynomial
   complex(SP) :: den, det
   !
   !
   a =  -(x(1)-x(2))*(x(3)-x(4))*( w(1)**2*w(2)**2 + w(3)**2*w(4)**2 ) &
        +(x(1)-x(3))*(x(2)-x(4))*( w(1)**2*w(3)**2 + w(2)**2*w(4)**2 ) &
        -(x(1)-x(4))*(x(2)-x(3))*( w(1)**2*w(4)**2 + w(2)**2*w(3)**2 ) 
   !
   b =   (x(1)-x(2))*w(3)**2*w(4)**2*( x(3)*w(3)**2 - x(4)*w(4)**2 ) - &
         (x(1)-x(3))*w(2)**2*w(4)**2*( x(2)*w(2)**2 - x(4)*w(4)**2 ) + &
         (x(1)-x(4))*w(2)**2*w(3)**2*( x(2)*w(2)**2 - x(3)*w(3)**2 ) + &
         (x(2)-x(3))*w(1)**2*w(4)**2*( x(1)*w(1)**2 - x(4)*w(4)**2 ) - &
         (x(2)-x(4))*w(1)**2*w(3)**2*( x(1)*w(1)**2 - x(3)*w(3)**2 ) + &
         (x(3)-x(4))*w(1)**2*w(2)**2*( x(1)*w(1)**2 - x(2)*w(2)**2 )   
   !
   c =   (x(1)-x(2))*w(1)**2*w(2)**2*( x(3)*w(3)**4 - x(4)*w(4)**4 ) - &
         (x(1)-x(3))*w(1)**2*w(3)**2*( x(2)*w(2)**4 - x(4)*w(4)**4 ) + &
         (x(1)-x(4))*w(1)**2*w(4)**2*( x(2)*w(2)**4 - x(3)*w(3)**4 ) + &
         (x(2)-x(3))*w(2)**2*w(3)**2*( x(1)*w(1)**4 - x(4)*w(4)**4 ) - &
         (x(2)-x(4))*w(2)**2*w(4)**2*( x(1)*w(1)**4 - x(3)*w(3)**4 ) + &
         (x(3)-x(4))*w(3)**2*w(4)**2*( x(1)*w(1)**4 - x(2)*w(2)**4 ) 
   !
   det  = b**2 -4._SP*a*c
   q(1) = -(b + sqrt(det))/2._SP
   q(2) = -(b - sqrt(det))/2._SP
   !
   if(abs(a)>epsilon(1._SP)) then
     q(1)=q(1)/a
     q(2)=q(2)/a
   endif
   !
   den  =  (q(1)-q(2))*(w(2)-w(3))*(w(2)+w(3))
   r(1) = -(q(1) - w(2)**2)*(q(1) - w(3)**2)*( x(3)*w(3)**2 - x(2)*w(2)**2  + q(2)*(x(2) - x(3)) )
   r(2) =  (q(2) - w(2)**2)*(q(2) - w(3)**2)*( x(3)*w(3)**2 - x(2)*w(2)**2  + q(1)*(x(2) - x(3)) )
   !
   if(abs(den)>epsilon(1._SP)) then
     r(1)=r(1)/den
     r(2)=r(2)/den
   endif
   !
 end subroutine mpa_RQ_2p_solver
 !
 !
 function f1(t, w, x) 
   implicit none
   complex(SP), intent(in)  :: w(6)
   complex(SP), intent(in)  :: x(6)
   integer,     intent(in)  :: t
   !
   complex(SP) :: f1
   !
   f1 = (x(1)-x(2)) *w(3)**2 *w(t)**2 *(w(3)**2 - w(t)**2) -  &
        (x(1)-x(3)) *w(2)**2 *w(t)**2 *(w(2)**2 - w(t)**2) +  &
        (x(1)-x(t)) *w(2)**2 *w(3)**2 *(w(2)**2 - w(3)**2) +  &
        (x(2)-x(3)) *w(1)**2 *w(t)**2 *(w(1)**2 - w(t)**2) -  &
        (x(2)-x(t)) *w(1)**2 *w(3)**2 *(w(1)**2 - w(3)**2) +  &
        (x(3)-x(t)) *w(1)**2 *w(2)**2 *(w(1)**2 - w(2)**2)
   !
 end function
 !
 !
 function f2(t, w, x) 
   implicit none
   complex(SP), intent(in)  :: w(6)
   complex(SP), intent(in)  :: x(6)
   integer,     intent(in)  :: t
   !
   complex(SP) :: f2
   !
   f2 = (x(1)-x(2)) *w(1)**2 *w(2)**2 *(w(3)**4 - w(t)**4) -  &
        (x(1)-x(3)) *w(1)**2 *w(3)**2 *(w(2)**4 - w(t)**4) +  &
        (x(1)-x(t)) *w(1)**2 *w(t)**2 *(w(2)**4 - w(3)**4) +  &
        (x(2)-x(3)) *w(2)**2 *w(3)**2 *(w(1)**4 - w(t)**4) -  &
        (x(2)-x(t)) *w(2)**2 *w(t)**2 *(w(1)**4 - w(3)**4) +  &
        (x(3)-x(t)) *w(3)**2 *w(t)**2 *(w(1)**4 - w(2)**4)
   !
 end function
 !
 !
 function f3(t, w, x) 
   implicit none
   complex(SP), intent(in)  :: w(6)
   complex(SP), intent(in)  :: x(6)
   integer,     intent(in)  :: t
   !
   complex(SP) :: f3
   !
   f3 = (x(1)-x(2)) *w(1)**4 *w(2)**4 *(w(3)**2 - w(t)**2) -  &
        (x(1)-x(3)) *w(1)**4 *w(3)**4 *(w(2)**2 - w(t)**2) +  &
        (x(1)-x(t)) *w(1)**4 *w(t)**4 *(w(2)**2 - w(3)**2) +  &
        (x(2)-x(3)) *w(2)**4 *w(3)**4 *(w(1)**2 - w(t)**2) -  &
        (x(2)-x(t)) *w(2)**4 *w(t)**4 *(w(1)**2 - w(3)**2) +  &
        (x(3)-x(t)) *w(3)**4 *w(t)**4 *(w(1)**2 - w(2)**2)
   !
 end function
 !
 !
 function f4(t, w, x) 
   implicit none
   complex(SP), intent(in)  :: w(6)
   complex(SP), intent(in)  :: x(6)
   integer,     intent(in)  :: t
   !
   complex(SP) :: f4
   !
   f4 = x(1) *w(1)**6 * (w(2)**2 - w(3)**2) * (w(2)**2 - w(t)**2) * (w(3)**2 - w(t)**2) - &
        x(2) *w(2)**6 * (w(1)**2 - w(3)**2) * (w(1)**2 - w(t)**2) * (w(3)**2 - w(t)**2) + &
        x(3) *w(3)**6 * (w(1)**2 - w(2)**2) * (w(1)**2 - w(t)**2) * (w(2)**2 - w(t)**2) - &
        x(t) *w(t)**6 * (w(1)**2 - w(2)**2) * (w(1)**2 - w(3)**2) * (w(2)**2 - w(3)**2)
   !
 end function
 !
 !
 subroutine mpa_RQ_3p_solver(w, x, r, q) 
   implicit none
   complex(SP), intent(in)   :: w(6)
   complex(SP), intent(in)   :: x(6)
   complex(SP), intent(out)  :: r(3), q(3)
   !
   integer     :: i, j
   complex(SP) :: a, b, c, d ! DALV: coefficients of the w^2 polynomial
   complex(SP) :: D1, D2, den, deR 
   real(SP)    :: t=0.33333333333333334_SP 
   complex(SP) :: s=(1._SP, 1.7320508075688772_SP)
   !
   !
   a =  f1(4, w, x)* ( f2(5, w, x)*f3(6, w, x) - f2(6, w, x)*f3(5, w, x) ) - &
        f1(5, w, x)* ( f2(4, w, x)*f3(6, w, x) - f2(6, w, x)*f3(4, w, x) ) + &
        f1(6, w, x)* ( f2(4, w, x)*f3(5, w, x) - f2(5, w, x)*f3(4, w, x) ) 
   !
   b = -f1(4, w, x)* ( f2(5, w, x)*f4(6, w, x) - f2(6, w, x)*f4(5, w, x) ) + &
        f1(5, w, x)* ( f2(4, w, x)*f4(6, w, x) - f2(6, w, x)*f4(4, w, x) ) - &
        f1(6, w, x)* ( f2(4, w, x)*f4(5, w, x) - f2(5, w, x)*f4(4, w, x) ) 
   !
   c = -f4(4, w, x)* ( f1(5, w, x)*f3(6, w, x) - f1(6, w, x)*f3(5, w, x) ) + &
        f4(5, w, x)* ( f1(4, w, x)*f3(6, w, x) - f1(6, w, x)*f3(4, w, x) ) - &
        f4(6, w, x)* ( f1(4, w, x)*f3(5, w, x) - f1(5, w, x)*f3(4, w, x) ) 
   !
   d = -f4(4, w, x)* ( f2(5, w, x)*f3(6, w, x) - f2(6, w, x)*f3(5, w, x) ) + &
        f4(5, w, x)* ( f2(4, w, x)*f3(6, w, x) - f2(6, w, x)*f3(4, w, x) ) - &
        f4(6, w, x)* ( f2(4, w, x)*f3(5, w, x) - f2(5, w, x)*f3(4, w, x) ) 
   !
   D1   = 3._SP*a*c - b**2
   D2   = 2._SP*b*D1 + 3._SP*a*(b*c - 9._SP*a*d)
   den  = ( D2 + sqrt(4.0_SP*D1**3 + D2**2) )**(t)
   !
   q(1) = -( den*(b- den/2._SP**(t)) + 2.0_SP**(t)*D1)/3.0_SP
   q(2) = -( den*(b+ conjg(s)* den/2._SP**(4._SP*t)) - s* D1/2._SP**(2._SP*t)  )/3._SP
   q(3) = -( den*(b+ s*den/2._SP**(4._SP*t)) - conjg(s)* D1/2._SP**(2._SP*t)  )/3._SP
   !
   if(abs(den*a)>epsilon(1._SP)) then
     q(1)=q(1)/(den*a)
     q(2)=q(2)/(den*a)
     q(3)=q(3)/(den*a)
   endif
   !
   deR  =  (w(1)**2-q(1)) * (w(1)**2-q(2)) * (w(2)**2-q(3)) * (w(3)**2-q(3))*   &
          ((w(2)**2-q(1)) * (w(3)**2-q(2)) - (w(2)**2-q(2)) * (w(3)**2-q(1)))   &
          +(w(1)**2-q(1)) * (w(1)**2-q(3)) * (w(2)**2-q(2)) * (w(3)**2-q(2))*   &
          ((w(2)**2-q(3)) * (w(3)**2-q(1)) - (w(2)**2-q(1)) * (w(3)**2-q(3)))   &
          +(w(1)**2-q(2)) * (w(1)**2-q(3)) * (w(2)**2-q(1)) * (w(3)**2-q(1))*   &
          ((w(2)**2-q(2)) * (w(3)**2-q(3)) - (w(2)**2-q(3)) * (w(3)**2-q(2))) 
   !
   r(1) = (w(1)**2-q(1))*(w(2)**2-q(1))*(w(3)**2-q(1))*(                                                         &
         -(w(1)**2-q(2))*(w(1)**2-q(3))*( (w(2)**2-q(2))*(w(3)**2-q(3)) - (w(2)**2-q(3))*(w(3)**2-q(2)) )*x(1)   &
         +(w(2)**2-q(2))*(w(2)**2-q(3))*( (w(1)**2-q(2))*(w(3)**2-q(3)) - (w(1)**2-q(3))*(w(3)**2-q(2)) )*x(2)   &
         -(w(3)**2-q(2))*(w(3)**2-q(3))*( (w(1)**2-q(2))*(w(2)**2-q(3)) - (w(1)**2-q(3))*(w(2)**2-q(2)) )*x(3) ) 
   !
   r(2) = (w(1)**2-q(2))*(w(2)**2-q(2))*(w(3)**2-q(2))*(                                                         &
         +(w(1)**2-q(1))*(w(1)**2-q(3))*( (w(2)**2-q(1))*(w(3)**2-q(3)) - (w(2)**2-q(3))*(w(3)**2-q(1)) )*x(1)   &
         -(w(2)**2-q(1))*(w(2)**2-q(3))*( (w(1)**2-q(1))*(w(3)**2-q(3)) - (w(1)**2-q(3))*(w(3)**2-q(1)) )*x(2)   &
         +(w(3)**2-q(1))*(w(3)**2-q(3))*( (w(1)**2-q(1))*(w(2)**2-q(3)) - (w(1)**2-q(3))*(w(2)**2-q(1)) )*x(3) ) 
   !
   r(3) = (w(1)**2-q(3))*(w(2)**2-q(3))*(w(3)**2-q(3))*(                                                        &
         -(w(1)**2-q(1))*(w(1)**2-q(2))*( (w(2)**2-q(1))*(w(3)**2-q(2)) - (w(2)**2-q(2))*(w(3)**2-q(1)) )*x(1)  & 
         +(w(2)**2-q(1))*(w(2)**2-q(2))*( (w(1)**2-q(1))*(w(3)**2-q(2)) - (w(1)**2-q(2))*(w(3)**2-q(1)) )*x(2)  & 
         -(w(3)**2-q(1))*(w(3)**2-q(2))*( (w(1)**2-q(1))*(w(2)**2-q(2)) - (w(1)**2-q(2))*(w(2)**2-q(1)) )*x(3) )
   !
   if(abs(deR)>epsilon(1._SP)) then
     r(1)=r(1)/deR
     r(2)=r(2)/deR
     r(3)=r(3)/deR
   endif
   !
 end subroutine mpa_RQ_3p_solver
 !
!
end module mpa_m

📚 Documentation Pages
⏱️ Real-Time
🔗 Code API