我写了一个代码,将方阵转换为简化的行梯形形式。但是当需要行交换时,我遇到了一个问题。我认为我交换的方式给了我错误的输出。谁能帮我解决这个问题。
here是我的代码,您可以在此处运行它。为了简化起见,我也把它放在这里。
!there is a more direct way of entering the value of a matrix by row instead !as by column: reshape has an optional
!argument ORDER which can be used to modify the order of filling the element !of the multidimensional array with the entries of the array constructor.
PROGRAM RREF
IMPLICIT NONE
REAL,DIMENSION(3,3)::A=reshape((/0,0,1,1,2,3,0,1,-2/), shape(A), order=(/2,1/)),TEMP_FOR_SW(1,3)
INTEGER::ROW=3,COL=3,I,J,K,SW_PRINT
WRITE(*,100)TRANSPOSE(A)
100 FORMAT(3F6.2)
DO I=1,ROW
IF(ABS(A(I,I))<1E-2)THEN
WRITE(*,101)I
101 FORMAT('SWAP NEEDED ROW=',I1)
SW_PRINT=0
DO K=1,col,1
IF(A(K,I)>1E-2)THEN
TEMP_FOR_SW=A(K:K,1:3)
A(K:K,1:3)=A(I:I,1:3)
A(I:I,1:3)=TEMP_FOR_SW
IF(SW_PRINT<1)THEN
WRITE(*,102)K
102 FORMAT('SWAPPED WITH ROW=',I1)
SW_PRINT=SW_PRINT+1
END IF
END IF
END DO
END IF
A(I:I,1:3)=A(I:I,1:3)/(A(I,I))
PRINT*,'--------------------------------------------------'
WRITE(*,100)TRANSPOSE(A)
DO J=1,COL
IF(J /= I)THEN
A(J:J,1:3)=A(J:J,1:3)-(A(I:I,1:3)*A(J,I))
PRINT*,'--------------------------------------------------'
WRITE(*,100)TRANSPOSE(A)
END IF
END DO
END DO
END PROGRAM RREF
我希望输出是
RowReduce[{{0, 0, 1}, {1, 2, 3}, {0, 1, -2}}] // MatrixForm
1.00 0.00 0.00
0.00 1.00 0.00
0.00 0.00 1.00
根据Mathematica。但是得到这个:
-0.14 0.00 0.00
0.29 1.00 0.00
0.14 0.00 1.00
编辑:我注意到该问题是在交换行之后立即开始的。