      SUBROUTINE COEF_RENORM(NDIF)
!
!  This subroutine computes the coefficients for the renormalization
!     of the multiple scattering series. These coefficients are 
!     expressed as C_REN(K) where K is the multiple scattering order.
!     REN2 is the value of the mixing (or renormalization) parameter.
!
!     NDIF is the scattering order at which the series is truncated, 
!     so that K varies from 0 to NDIF.
!
!  COMMON /RENORM/:
!
!     I_REN = 1 : renormalization in terms of G_n matrices (n : N_REN)
!           = 2 : renormalization in terms of the Sigma_n matrices
!           = 3 : renormalization in terms of the Z_n matrices
!           = 4 : Löwdin renormalization in terms of the Pi_1 matrices
!           = 5 : Löwdin renormalization in terms of the L_n  matrices
!
!     N_REN = renormalization order n
!
!     REN   = REN_R + i * REN_I : omega
!
!
!   Reference: A. Takatsu, S. Tricot, P. Schieffer, K. Dunseath,
!              M. Terao-Dunseath, K. Hatada and D. Sébilleau,
!                  Phys. Chem. Chem. Phys., 2022, 24, 5658
!
!
!  Authors : D. Sébilleau, A. Takatsu, M. Terao-Dunseath, K. Dunseath
!
!
!                                   Last modified (DS,ST): 26 Feb 2025
!
      USE DIM_MOD
      USE C_RENORM_MOD
      USE RENORM_MOD
!
      INTEGER M_MIN, M_MAX
!
      REAL C(0:NDIF_M,0:NDIF_M)
!
      COMPLEX X(0:NDIF_M,0:NDIF_M),SUM_L,POWER
      COMPLEX REN,REN2,COEF1,COEF2,ZEROC,ONEC,IC
      COMPLEX Y1(0:NDIF_M,0:NDIF_M)
!
!
      ZEROC=(0.,0.)
      ONEC=(1.,0.)
      IC=(0.,1.)
!
      REN=REN_R+IC*REN_I                                         ! omega
!
!  Initialisation of renormalization coefficients
!
      DO J=0,NDIF
        C_REN(J)=ZEROC
      ENDDO
!                                               
!  Computing the binomial coefficients C(N,K) = (N) = N! / K! (N-K)!
!                                               (K)
!CCC  2019.06.09 Aika
      c=0.0
!CCC  2019.06.09 Aika
      C(0,0)=1.
      C(1,0)=1.
      C(1,1)=1.
      DO N=2,NDIF
        C(N,0)=1.
        C(N,N)=1.
        DO K=1,N-1
          C(N,K)=C(N-1,K)+C(N-1,K-1)
        ENDDO
      ENDDO
!
      IF(I_REN.LE.3) THEN
!
!  Computing the modified renormalization parameter REN2 (g_n,s_n,zeta_n)
!
        IF(I_REN.EQ.1) THEN
!
!.....(g_n,G_n) renormalization
!
          REN2=REN**N_REN                                        ! g_n = omega^n
!
        ELSEIF(I_REN.EQ.2) THEN
!
!.....(s_{n},Sigma_n) renormalization
!
          REN2=(ONEC-REN**(N_REN+1))/(FLOAT(N_REN+1)*(ONEC-REN)) ! s_n
!
        ELSEIF(I_REN.EQ.3) THEN
!
!.....(zeta_{n},Z_n) renormalization
!
!     2019.04.29
!         REN2=(REN-ONEC)**(N_REN+1)                             ! zeta_n  
!     2019.06.09
!          REN2=-(REN-ONEC)**(N_REN+1)                             ! zeta_n  
           REN2=-(ONCE-REN)**(N_REN+1)                              ! zeta_n  
!
        ENDIF
!
!     AT & MTD 2019.04.17 - summation over j ?
        DO K=0,NDIF
           C_REN(K)=ZEROC
           DO J=K,NDIF
              C_REN(K)=C_REN(K)+C(J,K)*(ONEC-REN2)**(J-K)
           ENDDO
           C_REN(K)=C_REN(K)*REN2**(K+1)
        ENDDO
!        
!        DO K=0,NDIF
!          COEF1=REN2**(K+1)
!          DO J=K,NDIF
!            COEF2=(ONEC-REN2)**(J-K)
!            C_REN(J)=C_REN(J)+COEF1*COEF2*C(J,K)
!          ENDDO
!        ENDDO
!
      ELSEIF(I_REN.EQ.4) THEN
!
!     Loewdin (Pi_1) renormalization for n = 1
!
!     Notation: Y1(K,M) : [Y_1^k]_m
!
!       with k : scattering order
!            m : summation index
!
        COEF1 = ONEC - REN                                        ! (1 - omega)
!
        Y1(0,0) = ONEC                                            !
!
        DO K = 1, NDIF                                         !
          M_MAX = MIN(K,NDIF)                                   !
          M_MIN = INT(K / 2)                                      !
          DO M = M_MIN, M_MAX                                     !
            COEF2   = (REN**(K-M)) * (COEF1**(2*M-K))             !
            Y1(K,M) = COEF2 * ( C(M,K-M) + COEF1 * C(M,K-M-1) )   !
          END DO                                                  !
        END DO
!                                                                 !
        C_REN(0) = ONEC                                           !
        C_REN(1) = ONEC                                           !
        DO K = 2, NDIF                                          !
          C_REN(K) = ZEROC                                        !
          DO M = M_MIN, M_MAX                                     !
            C_REN(K) = C_REN(K) + Y1(M,K)                         !
          END DO                                                  !
        END DO                                                    !

      ELSE IF(I_REN.EQ.5) THEN
!
!     Loewdin L_n(omega,NDIF) renormalization
!
!     Notation: X(K,N) = X_n(omega,k)
!
!
!     Computing the X(N,K) coefficients, with K <= N
!
      POWER = ONEC / REN                                          !
      DO N = 0, NDIF                                              !
        POWER = POWER * REN                                       ! omega^n
        IF(N == 0) THEN                                           !
          X(N,0) = ONEC                                           !
        ELSE                                                      !
          X(N,0) = ZEROC                                          !
        END IF                                                    !
        DO K = 1, NDIF                                            !
          IF(K > N) THEN                                          !
            X(N,K) = ZEROC                                        !
          ELSE IF(K == N) THEN                                    !
            X(N,K) = POWER * X(N-1,K-1)                           !
          ELSE                                                    !
            X(N,K) = X(N-1,K) * (REN - POWER) + POWER * X(N-1,K-1)!
          END IF                                                  !
        END DO                                                    !
      END DO                                                      !
!
!  Calculation of L_n(omega,NDIF)
!
      DO N = 0, NDIF                                              !
        SUM_L = ZEROC                                             !
        DO K = N, NDIF                                            !
          SUM_L = SUM_L + X(K,N)                                  !
        END DO                                                    !
        C_REN(N) = SUM_L                                          !
      END DO                                                      !
!
      END IF                                                      !
!
      END
