      subroutine vfrcos(x,n,iflag)
**************************************************
*** 1-D Chebyshev transform !! MUST call VINIFFT(N) to initialize !!
***  iflag=1: Spectral  to Physical
***  iflag=0: Physical  to Spectral  
************  n  <=  2048  *************************************
      implicit double precision (a-h,o-z)
      dimension x(0:n)
      common /wsave/wsave(6165)
      if (iflag.eq.1) then
         val=.5d0
         do i=1,n-1
            x(i)=val*x(i)
         enddo
         call cost(n+1,x,wsave)
      else
         call cost(n+1,x,wsave)
         val=1.d0/n
         do i=1,n-1
            x(i)=x(i)*val
         enddo
         x(0)=x(0)*val*.5d0
         x(n)=x(n)*val*.5d0
      endif
      return
      end
