      subroutine lsdmakeW(m,nu,parity,matdim
     :                   ,dimmax,dvec,svec,ok)

C *****************************************************************
C
C This subroutine MAKEW generates the inverse of the matrix W which
C appears in the equations of Lee & Saio. The elements of W^-1 can be
C found their paper MNRAS (1987) 221, 513-526. The matrix is symmetric
C and tridiagonal, so that we only need to define the diagonal and
C sub-diagonal elements dvec() and svec(). svec(1) is undefined. The
C matrix to be generated is of logical size n and physical size nd.
C m and nu are parameters which govern the state of the matrix - m can
C be any integer (except zero), and nu can be any number. The logical
C variable parity governs whether the matrix is generated for even
C or odd modes - .true. gives even modes, and .false. gives odd modes.
C Sometimes W^-1 has singular elements; this is checked for, and when
C it occurs, the returned statues flag ok is .false.; otherwise,
C ok is .true.
C
C (c) Richard Townsend 1995
C
C*******************************************************************

      implicit none	! Turn off implicit typing

C Declare the passed variables

      integer m		! The m-value to make the matrix for
      double precision nu  	! The nu (rotation parameter) to use
      logical parity	! Whether the matrix is for even or odd modes
      integer matdim	! The logical dimension of the matrix to make
      integer dimmax	! The physical dimension of the matrix to make
      double precision dvec(dimmax)	! The diagonal matrix elements
      double precision svec(dimmax)	! The sub-diagonal matrix elements
      logical ok	! The returning status flag

C Declare the in-program variables

      integer j		! A counting variable which steps over l-values
      integer lj	! The l-value associated with each value of j 
      double precision lsdFl   	! Lee & Saio's F(l) function - see MNRAS 221
      double precision lsdJml   ! Lee & Saio's Jml(m,l) function  - "

C Initialise the returning status flag to .true. (ie no error)

      ok=.true.

C Set up the values of lj for j=1; we only need to set up lj once by
C discriminating whether parity is .even. or .odd., since after that,
C l(j+1)=l(j)+2 always...

      if(parity) then
        lj=abs(m)
      else
        lj=abs(m)+1
      endif

C Now we loop over all of the matrix elements (diagonal) except the last...

      do j=1,matdim-1

C Work out if the matrix is going to go singular. This will happen when
C m*nu=lj(lj-1) (except for j=1,even modes) or m*nu=(lj+1)(lj+2). If this
C occurs, bum out with a .false. value in ok. We only need to check the
C lj(lj-1) singularity for odd modes, since the (lj+1)(lj+2) check will
C pick up all the even-mode singularities.

C        if(m*nu.eq.dble(lj*(lj-1)).and.parity.eq..false.) then
C          ok=.false.
C          goto 9000
        if(m*nu.eq.dble((lj+1)*(lj+2))) then
          ok=.false.
          goto 9000
        endif

C Set up the matrix elements.

        dvec(j)=(1.D0 - m*nu/(lj*(lj+1))
     :             - nu**2 * lsdFl(m,lj,nu))/(lj*(lj+1))

        svec(j+1)=-nu**2 * lsdJml(m,lj+1) * lsdJml(m,lj+2) /
     :           ((lj+1)*(lj+2)-m*nu)

C Increment lj

        lj=lj+2

C ...and loop around.

      enddo

C Do the last diagonal element, again checking for singularities. In this
C case, the singularities will only be generated by the m*nu=(lj+1)(lj+2)
C term, so we only need to check for that...
 
      if(m*nu.eq.dble((lj+1)*(lj+2))) then
        ok=.false.
        goto 9000
      endif

C Now we set up the last matrix element in the usual way...

      dvec(matdim)=(1.D0 - m*nu/(lj*(lj+1))
     :           - nu**2 * lsdFl(m,lj,nu))/(lj*(lj+1))

C Finshed, so we quit...

 9000 continue

      return

      end
