! -*- mode: f90; coding: utf-8 -*-
! ----------------------------------------------------------------------------
! 定数・関数ユーティリティ/Constant and Function Utilities
! ----------------------------------------------------------------------------
!> @author Shin-ichi Takehiro, Youhei SASAKI
!> @copyright Copyright (C) SPMODEL Development Group, 2026
!>            License is MIT/X11. see [COPYRIGHT](@ref COPYRIGHT) in detail
!> @ja
!> @brief 定数・関数ユーティリティ
!>
!> @details
!> 複数のモジュールで個別に設定していた定数, 関数をまとめた.
!> また, 単精度/倍精度両対応にした固有値計算と並び替え関数も提供している.
!> @endja
!> @en
!> @brief Constant and Function Utilities
!>
!> @details
!> Constants and functions previously configured individually across
!> multiple modules have been consolidated. Furthermore, eigenvalue
!> calculations and sorting functions supporting both single-precision
!> and double-precision are now provided.
!> @enden
module spml_utils
  use dc_message, only : MessageNotify
  use dc_types, only: DP, DP_EPS, SP, SP_EPS
  implicit none
  private
  public pi
  public gammaln
  public factrl
  ! --------------------------------------------------------------------------
  !> PI: 3.14159265358979323846264338327950288
  real(DP), parameter :: pi = 3.141592653589793238d0
  !> -------------------------------------------------------------------------
  !> external file: dlgamma.f90
  interface
    real(DP) function dlgamma(x)
      use dc_types, only: DP
      real(DP), intent(in) :: x
    end function dlgamma
  end interface
contains
  ! --------------------------------------------------------------------------
  !> Returns the vaule \f$ ln[Γ(x)] for x >= 0 \f$
  function gammaln(xx)
    real(DP), intent(in) :: xx
    real(DP)             :: gammaln
    gammaln = dlgamma(xx)
  end function gammaln
  ! --------------------------------------------------------------------------
  !> Returns the value \f$n!\f$ as a floating point number
  function factrl(n)
    integer, intent(in)  :: n
    real(DP)             :: factrl
    factrl = exp(gammaln(n+1.0d0))
  end function factrl
end module spml_utils
