z9ln2r.f 1.94 KB
complex(kind(1.d0)) function z9ln2r (z)
      implicit none
c april 1978 edition.  w. fullerton c3, los alamos scientific lab.
c
c evaluate  clog(1+z)  from 2-nd order with relative error accuracy so
c that     clog(1+z) = z - z**2/2 + z**3*c9ln2r(z).
c
c now  clog(1+z) = 0.5*alog(1+2*x+cabs(z)**2) + i*carg(1+z),
c where x = real(z)  and  y = aimag(z).
c we find
c     z**3 * c9ln2r(z) = -x*cabs(z)**2 - 0.25*cabs(z)**4
c        + (2*x+cabs(z)**2)**3 * r9ln2r(2*x+cabs(z)**2)
c        + i * (carg(1+z) + (x-1)*y)
c the imaginary part must be evaluated carefully as
c     (atan(y/(1+x)) - y/(1+x)) + y/(1+x) - (1-x)*y
c       = (y/(1+x))**3 * r9atn1(y/(1+x)) + x**2*y/(1+x)
c
c now we divide through by z**3 carefully.  write
c     1/z**3 = (x-i*y)/cabs(z)**3 * (1/cabs(z)**3)
c then   c9ln2r(z) = ((x-i*y)/cabs(z))**3 * (-x/cabs(z) - cabs(z)/4
c        + 0.5*((2*x+cabs(z)**2)/cabs(z))**3 * r9ln2r(2*x+cabs(z)**2)
c        + i*y/(cabs(z)*(1+x)) * ((x/cabs(z))**2 +
c          + (y/(cabs(z)*(1+x)))**2 * r9atn1(y/(1+x)) ) )
c
c if we let  xz = x/cabs(z)  and  yz = y/cabs(z)  we may write
c     c9ln2r(z) = (xz-i*yz)**3 * (-xz - cabs(z)/4
c        + 0.5*(2*xz+cabs(z))**3 * r9ln2r(2*x+cabs(z)**2)
c        + i*yz/(1+x) * (xz**2 + (yz/(1+x))**2*r9atn1(y/(1+x)) ))
c
      complex(kind(1.d0)) z
      real(kind(1.d0)) x,y,xz,yz,cabsz,d9ln2r,d9atn1,arg,rpart,aipart,
     1 y1x
      
      external d9atn1, d9ln2r
c
      x = real (z)
      y = aimag (z)
c
      cabsz = abs(z)
      if (cabsz.gt.0.8125) go to 20
c
      z9ln2r = cmplx (1.0/3.0, 0.0, kind(1.d0))
      if (cabsz.eq.0.0) return
c
      xz = x/cabsz
      yz = y/cabsz
c
      arg = 2.0*xz + cabsz
      rpart = 0.5*arg**3*d9ln2r(cabsz*arg) - xz - 0.25*cabsz
      y1x = yz/(1.0+x)
      aipart = y1x * (xz**2 + y1x**2*d9atn1(cabsz*y1x) )
c
      z9ln2r = cmplx(xz,-yz,kind(1.d0))**3 *
     1 cmplx(rpart,aipart,kind(1.d0))
      return
c
 20   z9ln2r = (log(1.0+z) - z*(1.0-0.5*z)) / z**3
      return
c
      end