C....*...1.........2.........3.........4.........5.........6.........7.*.......8
C     DS1      9/14/74
C
C     PURPOSE
C     FOR A GIVEN VECTOR W COMPUTE SCALARS C,S, AND R SUCH THAT THE
C     VECTOR V=QW SATISFIES:
C     1) COMPONANT K OF V &S R,
C     2) COMPONANT L OF V IS ZERO,
C     3) THE REMAINING COMPONANTS OF V ARE THE SAME AS W,
C     WHERE Q IS THE GIVENS TRANSFORMATION MATRIX:
C     Q(K,K)=Q(L,L)=C,
C     Q(L,K)=-S,
C     Q(K,L)=S,
C     Q(I,I)=1     IF I NOT EQUAL K OR L,
C     Q(I,J)=0     OTHERWISE.
C
C     USAGE
C     CALL DS1(K,L,M,W,C,S,R)
C
C     ARGUMENTS
C     K - POSITIVE INTEGER LESS THAN L
C         INTEGER*4
C     L - POSITIVE INTEGER LESS THAN OR EQUAL TO M
C         INTEGER*4
C     M - LENGTH OF W.
C         INTEGER*4
C     W - INPUT VECTOR OF LENGTH M.
C         REAL*8
C     C - SCALAR DESCRIBED ABOVE
C         REAL*8
C     S - SCALAR DESCRIBED ABOVE
C         REAL*8
C     R - SCALAR DESCRIBED ABOVE
C         REAL*8
C
C     REMARK
C     THE USAGE
C     CALL DS1(K,L,M,W,C,S,R)
C     S=-S
C     WILL PRODUCE Q AS DEFINED ABOVE SUCH THAT V'=W'Q SATISFIES 1), 2),
C     AND 3) ABOVE.
C
C
      SUBROUTINE DS1(K,L,M,W,C,S,R)
      IMPLICIT REAL*8 (A-H,O-Z)
      save
      REAL*8 W(1)
      DATA TOL/1.D-38/
      V=W(K)**2+W(L)**2
      IF(V.LT.TOL) V=0.D0
      C=1.D0
      S=0.D0
      IF(V.EQ.0.D0) RETURN
      R=DSQRT(V)
      C=W(K)/R
      S=W(L)/R
      RETURN
      END
C....*...1.........2.........3.........4.........5.........6.........7.*
C     DS2      9/14/74
C
C     PURPOSE
C     GIVEN A MATRIX C COMPUTE THE MATRIX V=QA FROM OUTPUT OF DH1.
C
C     USAGE
C     CALL DS1(K,L,M,C,S,R,A,N)
C
C     ARGUMENTS
C     K - AS FOR DS1
C     L - AS FOR DS1
C     M - AS FOR DS1
C     C - AS RETURNED BY DS1
C     S - AS RETURNED BY DS1
C     R - AS RETURNED BY DS1
C     A - INPUT M BY N MATRIX STORED COLUMNWISE (STORAGE MODE 0)
C         CONTAINS V ON RETURN
C         REAL*8
C     N - NUMBER OF COLUMNS IN A
C         INTEGER*4
C
C     REMARK
C     THE USAGE
C     S=-S
C     CALL DS2(K,L,M,C,S,R,A,N)
C     WILL COMPUTE V=Q'A
C
C
      SUBROUTINE DS2(K,L,M,C,S,R,A,N)
      IMPLICIT REAL*8 (A-H,O-Z)
      save
      REAL*8 A(M,N)
      DO 10 IC=1,N
      AK=A(K,IC)
      AL=A(L,IC)
      A(K,IC)=C*AK+S*AL
10    A(L,IC)=-S*AK+C*AL
      RETURN
      END
C....*...1.........2.........3.........4.........5.........6.........7.*
C     DS3      9/14/74
C
C     PURPOSE
C     GIVEN A MATRIX A COMPUTE THE MATRIX V=AQ FROM OUTPUT OF DH1.
C
C     USAGE
C     CALL DS3(K,L,M,C,S,R,A,N)
C
C     ARGUMENTS
C     K - AS FOR DS1
C     L - AS FOR DS1
C     M - AS FOR DS1
C     C - AS RETURNED BY DS1
C     S - AS RETURNED BY DS1
C     R - AS RETURNED BY DS1
C     A - INPUT N BY M MATRIX STORED COLUMNWISE (STORAGE MODE 0)
C         CONTAINS V ON RETURN
C         REAL*8
C     N - NUMBER OF ROWS IN A
C         INTEGER*4
C
C     REMARK
C     THE USAGE
C     S=-S
C     CALL DS3(K.L,M,C,S,R,A,N)
C     WILL COMPUTE V=AQ'
C
C
      SUBROUTINE DS3(K,L,M,C,S,R,A,N)
      IMPLICIT REAL*8 (A-H,O-Z)
      save
      REAL*8 A(N,M)
      DO 10 IR=1,N
      AK=A(IR,K)
      AL=A(IR,L)
      A(IR,K)=AK*C-AL*S
10    A(IR,L)=AK*S+AL*C
      RETURN
      END
