! copyright (c) 2008 Swedish Defence Research Agency (FOI) 
! may not be copied or distributed without written permission from FOI



C	Sven Ivansson 1991



	COMPLEX*16 FUNCTION AZIMILN (Z)
C	to compute    - ln(z) / (1-z*z)   accurately, even for z close to 1
	IMPLICIT NONE
	COMPLEX*16 Z

	INTEGER N, I
	REAL*8 EPS
	COMPLEX*16 ZZ, ZD, CREL(20)

	DATA EPS /1.0D-15/  ! desired relative accuracy (approximately), 
C	strict analysis possible with MacLaurin remainder term

	ZZ = Z
	IF ( CDABS(1-ZZ) .GE. 0.1D0 ) THEN
	  AZIMILN = -CDLOG(ZZ) / (1-ZZ**2)
	ELSE
C	  azimiln := 1 + z/2 + (z**2)/3 + (z**3)/4 + ...
	  ZD = 1-ZZ
	  N = 1
	  CREL(1) = ZD/2
	  DO WHILE ( CDABS(CREL(N)) .GT. EPS )
	    N = N + 1
	    IF (N.GT.20) STOP '%%% azimiln, dim'
	    CREL(N) = CREL(N-1) * (N*ZD)/(N+1)     !  zd**n / (n+1)
	  END DO
	  AZIMILN = (0.0D0,0.0D0)
	  DO I = N, 1, -1
	    AZIMILN = AZIMILN + CREL(I)
	  END DO
	  AZIMILN = ( (1.0D0,0.0D0) + AZIMILN ) / (2-ZD)
	END IF

	RETURN
	END
