行列式、逆行列、連立方程式

 投稿者:しばっち  投稿日:2014年11月26日(水)18時10分49秒
  LET N=6
DIM A$(N,N),B$(N),Y$(N),X$(N,N)
FOR I=1 TO N
   LET B$(I)="Y("&STR$(I)&")"
   FOR J=1 TO N
      LET A$(I,J)="X("&STR$(I)&","&STR$(J)&")"
   NEXT J
NEXT I
PRINT "行列式"
FOR I=2 TO N
   PRINT "DET"&STR$(I)&"=";
   PRINT DET$(I,A$)
   PRINT
NEXT I
PRINT
FOR I=2 TO N
   PRINT "逆行列";I;"*";I
   CALL INV(I,A$,X$)
   FOR J=1 TO I
      FOR K=1 TO I
         PRINT "X("&STR$(J)&","&STR$(K)&")=";X$(J,K)
      NEXT K
   NEXT J
   PRINT
NEXT I
PRINT
FOR I=2 TO N
   MAT Y$=NUL$
   CALL CRAMER(I,A$,B$,Y$)
   PRINT STR$(I);"元連立一次方程式"
   FOR J=1 TO I
      PRINT "X(";STR$(J);")=";Y$(J)
   NEXT J
   PRINT
NEXT I
END

EXTERNAL FUNCTION DET$(N,X$(,))
IF N=1 THEN
   LET DET$=X$(1,1)
   EXIT FUNCTION
ELSEIF N=2 THEN
   LET DET$=X$(1,1)&"*"&X$(2,2)&"-"&X$(2,1)&"*"&X$(1,2)
   EXIT FUNCTION
END IF
DIM D$(N),A$(N-1,N-1)
FOR K=1 TO N
   FOR I=1 TO N-1
      FOR J=1 TO N-1
         IF I>=K THEN
            LET A$(I,J)=X$(I+1,J+1)
         ELSE
            LET A$(I,J)=X$(I,J+1)
         END IF
      NEXT J
   NEXT I
   LET D$(K)=DET$(N-1,A$)
NEXT K
FOR I=1 TO N
   IF D$(I)(1:1)="+" THEN LET D$(I)(1:1)=""
   IF MOD(I,2)=1 THEN
      LET S$=S$&"+"&X$(I,1)&"*("&D$(I)&")"
   ELSE
      LET S$=S$&"-"&X$(I,1)&"*("&D$(I)&")"
   END IF
NEXT I
IF S$(1:1)="+" THEN LET S$(1:1)=""
LET DET$=S$
END FUNCTION

EXTERNAL  SUB INV(N,A$(,),B$(,))
DIM X$(N-1,N-1)
LET D$=DET$(N,A$)
FOR I=1 TO N
   FOR J=1 TO N
      LET P=0
      LET Q=0
      FOR K=1 TO N
         IF I<>K THEN
            LET P=P+1
            LET Q=0
            FOR L=1 TO N
               IF J<>L THEN
                  LET Q=Q+1
                  LET X$(P,Q)=A$(K,L)
               END IF
            NEXT L
         END IF
      NEXT K
      IF (-1)^(I+J)=-1 THEN LET SIGN$="-" ELSE LET SIGN$=""
      LET B$(I,J)=SIGN$&"("&DET$(N-1,X$)&")/("&D$&")"
   NEXT J
NEXT I
FOR I=1 TO N-1
   FOR J=I+1 TO N
      SWAP B$(I,J),B$(J,I)
   NEXT J
NEXT I
END SUB

EXTERNAL SUB CRAMER(N,X$(,),Y$(),D$())
DIM A$(N,N)
FOR I=1 TO N
   FOR J=1 TO N
      LET A$(I,J)=X$(I,J)
   NEXT J
NEXT I
LET DD$=DET$(N,A$)
FOR K=1 TO N
   FOR I=1 TO N
      FOR J=1 TO N
         IF J=K THEN LET A$(I,J)=Y$(I) ELSE LET A$(I,J)=X$(I,J)
      NEXT J
   NEXT I
   IF DD$(1:1)="+" THEN LET DD$(1:1)=""
   LET D$(K)="("&DET$(N,A$)&")/("&DD$&")"
NEXT K
END SUB
 
 

戻る