[Fortran] 纯文本查看 复制代码
SUBROUTINE CHHBG(A,N)
DIMENSION A(N,N)
DOUBLE PRECISION A,D,T
DO 100 K=2,N-1
D=0.0
DO 10 J=K,N
IF (ABS(A(J,K-1)).GT.ABS(D)) THEN
D=A(J,K-1)
I=J
END IF
10 CONTINUE
IF (ABS(D)+1.0.NE.1.0) THEN
IF (I.NE.K) THEN
DO 20 J=K-1,N
T=A(I,J)
A(I,J)=A(K,J)
20 A(K,J)=T
DO 30 J=1,N
T=A(J,I)
A(J,I)=A(J,K)
30 A(J,K)=T
END IF
DO 90 I=K+1,N
T=A(I,K-1)/D
A(I,K-1)=0.0
DO 40 J=K,N
40 A(I,J)=A(I,J)-T*A(K,J)
DO 50 J=1,N
50 A(J,K)=A(J,K)+T*A(J,I)
90 CONTINUE
END IF
100 CONTINUE
RETURN
END
SUBROUTINE CHHQR(A,N,U,V,EPS,JT)
DIMENSION A(N,N),U(N),V(N)
DOUBLE PRECISION A,U,V,W,B,C,X,Y,XY,P,Q,R,E,F,Z,G
JT=1
M=N
IT=0
10 IF (M.EQ.0) RETURN
L=M+1
40 L=L-1
IF (ABS(A(L,L-1)).GT.EPS*(ABS(A(L-1,L-1))+ABS(A(L,L))).AND.
* L.GT.1) GOTO 40
IF (L.EQ.M) THEN
U(M)=A(M,M)
V(M)=0.0
M=M-1
IT=0
GOTO 10
END IF
IF (L.EQ.M-1) THEN
B=-(A(M,M)+A(M-1,M-1))
C=A(M,M)*A(M-1,M-1)-A(M,M-1)*A(M-1,M)
W=B*B-4*C
Y=SQRT(ABS(W))
IF (W.GT.0.0) THEN
XY=1.0
IF (B.LT.0.0) XY=-1.0
U(M)=(-B-XY*Y)/2.0
U(M-1)=C/U(M)
V(M)=0.0
V(M-1)=0.0
ELSE
U(M)=-B/2.0
U(M-1)=U(M)
V(M)=Y/2.0
V(M-1)=-V(M)
END IF
M=M-2
IT=0
GOTO 10
END IF
IF (IT.GE.60) THEN
WRITE(*,50)
50 FORMAT(1X, 'FAIL')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