
c/* %W%      latest revision %G% %U% */
      subroutine Tmatrx ( psfac , n1, n2 , nmax, nspin )
********************************************************************************

      implicit real*8 (a-h, o-z)
      complex*16   zf, zi

      dimension   ur(43,43,8) , ui(43,43,8)
c     dimension   zf(194,194) 
      dimension   zf(86,86)

      common /nlspfl/   ur, ui
      common /foptp/    zf
      common /Rcomn/ rr , ri , rrb , rib
      common /spins/    nspina , nspinb , nspinc , nspind , nspine

********************************************************************************

      zi = (0.0,1.0)
      rr = 0.
      ri = 0.0
      rrb = 0.
      rib = 0.

c *** calculation  of  T-matrix   (on  shell)
c ***
c *** do matrix mult   r= finv * u      f always square r not necly so
c ***

      do 270 j3 = 1,nmax

c ***
c *** coupled channels      spin .5 x .5
c ***                       nspin=3/4 j=l+1     5/6 j=l-1
c ***
c *** test j3 to see if need nspina(upper) or nspinb(lower)

  271    if (j3 .le. n1) go to 272

c *** j3 gt n1 use b=lower

         ibc = j3 - n1
         nsp = nspinb
         go to 273

c *** j3 lt n1 use a

 272     ibc = j3
         nsp = nspina

c *** nb, b is not,not, born, rather upper,lower positions=a,b
c *** calc a and b position r magtrices

 273     rr  = rr + zf(n1,j3) * ur(ibc,n1,nsp)
     $            + zi * zf(n1,j3) * ui(ibc,n1,nsp)
         ri  = ri + zf(n1,j3) * ui(ibc,n1,nsp)
     $            - zi * zf(n1,j3) * ur(ibc,n1,nsp)
         rrb = rrb + zf(n2,j3) * ur(ibc,n1,nsp)
     $             + zi * zf(n2,j3) * ui(ibc,n1,nsp)
         rib = rib + zf(n2,j3) * ui(ibc,n1,nsp)
     $             - zi * zf(n2,j3) * ur(ibc,n1,nsp)

 270  continue


      rr = -psfac * rr
      ri = -psfac * ri
      rrb = -psfac * rrb
      rib = -psfac * rib

      RETURN

c *** format statements ***

 242  format(' ********** j3 too big in Rcalc at 242 ************',i8/)
 970  format(1h ,37h  unnormalized r matrix(re,im)       ,13x,4e14.7)
 1400 format('       lid= ',i3)
 1498 format('   ???rmatrx ur(lid,lid,1)= ',e10.4,'  ui(=)= ',e10.4)
 2423 format(1h ,39h normalized re and im ra,rb(ko,ko,ko)=  ,
     $ 11x,4e14.7)

      end
