多項式の割り算

 投稿者:しばっち  投稿日:2010年10月30日(土)20時05分9秒
  PUBLIC NUMERIC MAXLEVEL,MINLEVEL
LET  MAXLEVEL=5
LET  MINLEVEL=-30
DIM F(MINLEVEL TO MAXLEVEL),G(MINLEVEL TO MAXLEVEL),Y(MINLEVEL TO MAXLEVEL)
LET G(0)=2
LET G(1)=-1
LET G(2)=1
LET F(0)=-1
LET F(1)=0
LET F(2)=1
LET F(3)=-3
LET F(4)=2
PRINT "F(X)=";
CALL DISPLAY(F)
PRINT "G(X)=";
CALL DISPLAY(G)
CALL DIV(Y,F,G) !'Y(X)=F(X)/G(X)
PRINT "F(X)/G(X)=";
CALL DISPLAY(Y)
LET  X=3  !' Y(X)の収束半径はABS(X)>1と思われる
PRINT VALUE(F,X)/VALUE(G,X) !'検証 (X=3を代入)
PRINT VALUE(Y,X)
END

EXTERNAL  SUB DIV(FF(),X(),Y()) !'多項式の割り算
DIM YY(MINLEVEL TO MAXLEVEL),XX(MINLEVEL TO MAXLEVEL)
DIM F(MINLEVEL TO MAXLEVEL)
MAT XX=X
MAT FF=ZER
LET XL=DIMCHECK(X)
LET YL=DIMCHECK(Y)
FOR I=XL-YL TO MINLEVEL STEP -1 !'筆算法
   MAT F=ZER
   LET F(I)=XX(YL+I)/Y(YL)
   MAT FF=FF+F
   CALL MUL(F,Y,YY)
   MAT XX=XX-YY
NEXT I
END SUB

EXTERNAL  SUB DISPLAY(A())
LET  N=DIMCHECK(A)
IF N>1 THEN
   IF A(N)<0 THEN PRINT "-";
   IF ABS(A(N))<>1 THEN
      PRINT STR$(ABS(A(N)));"*X^";STR$(N);
   ELSE
      PRINT "X^";STR$(N);
   END IF
END IF
FOR I=N-1 TO 2 STEP -1
   IF A(I)<>0 THEN
      IF A(I)<0 THEN PRINT "-"; ELSE PRINT "+";
      IF ABS(A(I))<>1 THEN
         PRINT STR$(ABS(A(I)));"*X^";STR$(I);
      ELSEIF ABS(A(I))=1 THEN
         PRINT "X^";STR$(I);
      END IF
   END IF
NEXT I
IF A(1)<>0 THEN
   IF N>1 THEN
      IF A(1)<0 THEN PRINT "-"; ELSE PRINT "+";
   END IF
   IF ABS(A(1))<>1 THEN
      PRINT STR$(ABS(A(1)));"*X";
   ELSEIF ABS(A(1))=1 THEN
      PRINT "X";
   END IF
END IF
IF A(0)<>0 THEN
   IF A(0)<0 THEN PRINT "-"; ELSE PRINT "+";
   PRINT STR$(ABS(A(0)));
END IF
IF MINLEVEL<0 THEN
   FOR I=-1 TO MINLEVEL STEP -1
      IF A(I)<>0 THEN
         IF A(I)<0 THEN PRINT "-"; ELSE PRINT "+";
         IF I=-1 THEN
            PRINT STR$(ABS(A(I)));"/X";
         ELSE
            PRINT STR$(ABS(A(I)));"/X^";STR$(ABS(I));
         END IF
      END IF
   NEXT I
END IF
PRINT
END SUB

EXTERNAL  SUB MUL(X(),Y(),C())
MAT C=ZER
FOR J=MINLEVEL TO MAXLEVEL
   FOR I=MAX(MINLEVEL,MINLEVEL-J) TO MIN(MAXLEVEL-J,MAXLEVEL)
      LET  C(I+J)=C(I+J)+Y(I)*X(J)
   NEXT I
NEXT J
END SUB

EXTERNAL  FUNCTION DIMCHECK(X())
FOR N=MAXLEVEL TO MINLEVEL STEP -1
   IF X(N)<>0 THEN EXIT FOR
NEXT N
LET  DIMCHECK=N
END FUNCTION

EXTERNAL  FUNCTION VALUE(F(),X)
LET S=F(MAXLEVEL)
FOR I=MAXLEVEL-1 TO 0 STEP -1
   LET S=S*X+F(I)
NEXT I
LET SS=F(MINLEVEL)
FOR I=MINLEVEL+1 TO -1
   LET SS=SS/X+F(I)
NEXT I
LET VALUE=S+SS/X
END FUNCTION
 

戻る