多項式代入

 投稿者:しばっち  投稿日:2009年 5月10日(日)15時46分3秒
  多項式に多項式を代入 f(g(x))


OPTION BASE 0
PUBLIC NUMERIC MAXLEVEL
LET  MAXLEVEL=20
DIM X(MAXLEVEL),Y(MAXLEVEL)
CALL COSINE(X)
CALL SINE(Y)
PRINT "COS(SIN(X))"
CALL HORNER(X,Y)
CALL DISPLAY(X)
LET XX=.5
PRINT VALUE(X,XX);COS(SIN(XX)) !'検算
PRINT
CALL CLR(X)
LET X(0)=-1
LET X(2)=2 !'2*X^2-1  COS 2倍角式
CALL REPEATFUNC(X,4) !'COS 2^4倍角式
PRINT "F(F(F(F(X))))"
CALL DISPLAY(X)
LET XX=COS(RAD(30)/2^4)
PRINT VALUE(X,XX);F(F(F(F(XX)))) !'検算
PRINT COS(RAD(30))
PRINT
CALL COSINE(X)
CALL RCPFUNC(X) !'逆数
PRINT "1/COS(X)"
CALL DISPLAY(X)
LET XX=.5
PRINT VALUE(X,XX);1/COS(XX) !'検算
PRINT
CALL COSINE(X)
CALL SQRFUNC(X) !'平方根
PRINT "SQR(COS(X))"
CALL DISPLAY(X)
LET XX=.5
PRINT VALUE(X,XX);SQR(COS(XX)) !'検算
PRINT
CALL COSINE(X)
LET N=SQR(2)
CALL ROOTFUNC(X,N) !'非整数乗
PRINT "COS(X)^";N
CALL DISPLAY(X)
LET XX=.5
PRINT VALUE(X,XX);COS(XX)^N !'検算
PRINT
CALL COSINE(X)
CALL SINE(Y)
CALL POWERFUNC(X,Y) !'多項式乗
PRINT "COS(X)^SIN(X)"
CALL DISPLAY(X)
LET XX=.5
PRINT VALUE(X,XX);COS(XX)^SIN(XX) !'検算
END

EXTERNAL  FUNCTION F(X)
LET F=2*X*X-1 !'COS 2倍角
END FUNCTION

EXTERNAL  FUNCTION VALUE(A(),XX) !'多項式 F(X)の値
LET  N=DIMCHECK(A)
LET  Y=A(N)
FOR I=N-1 TO 0 STEP -1
   LET  Y=Y*XX+A(I)
NEXT I
LET  VALUE=Y
END FUNCTION

EXTERNAL  SUB HORNER(F(),G())
!' 多項式 F[X}に多項式 G[X]を代入 F[G[X]]
OPTION BASE 0
DIM Y(MAXLEVEL)
LET  N=DIMCHECK(F)
LET  Y(0)=F(N)
FOR I=N-1 TO 0 STEP -1
   CALL MUL(Y,G)
   LET  Y(0)=Y(0)+F(I)
NEXT I
CALL COPY(F,Y)
END SUB

EXTERNAL  SUB REPEATFUNC(X(),N)
!'F[F[F[...F[X]]]]...]
OPTION BASE 0
DIM C(MAXLEVEL),T(MAXLEVEL)
CALL COPY(C,X)
CALL COPY(T,X)
FOR I=2 TO N
   CALL HORNER(C,X)
   CALL COPY(X,C)
   CALL COPY(C,T)
NEXT I
END SUB

EXTERNAL  SUB RCPFUNC(X())
!'1/(1-F[X])=1+F[X]+F[X]^2+F[X]^3+...収束半径(ABS(F[X]) < 1)
!'1/F[X]=1/(1-(1-F[X])
OPTION BASE 0
DIM C(MAXLEVEL),Y(MAXLEVEL)
LET  Y(0)=1
FOR I=0 TO MAXLEVEL
   LET  C(I)=1 !'1+F[X]+F[X]^2+F[X]^3+...
NEXT I
CALL SUBST(Y,X) !'1-F[X]
CALL HORNER(C,Y)
CALL COPY(X,C)
END SUB

EXTERNAL  SUB SQRFUNC(X())
!' SQR(1-(1-F[X]))  収束半径(ABS(F[X]) < 1)
OPTION BASE 0
DIM C(MAXLEVEL),Y(MAXLEVEL)
LET  Y(0)=1
CALL SUBST(Y,X) !'1-F[X]
CALL COMB(C,1,-1,1,.5) !'(1-X)^.5
CALL HORNER(C,Y)
CALL COPY(X,C)
END SUB

EXTERNAL  SUB ROOTFUNC(X(),N)
!' (1-(1-F[X]))^N  収束半径(ABS(F[X]) < 1)
OPTION BASE 0
DIM C(MAXLEVEL),Y(MAXLEVEL)
LET  Y(0)=1
CALL SUBST(Y,X) !'1-F[X]
CALL COMB(C,1,-1,1,N) !'(1-X)^N
CALL HORNER(C,Y)
CALL COPY(X,C)
END SUB

EXTERNAL  SUB LOGFUNC(X())
!'LOG(F[X])
OPTION BASE 0
DIM C(MAXLEVEL)
CALL LN(C)
LET  X(0)=X(0)-1
CALL HORNER(C,X)
CALL COPY(X,C)
END SUB

EXTERNAL  SUB EXPFUNC(X())
!'EXP(F[X])
OPTION BASE 0
DIM C(MAXLEVEL)
CALL EXPON(C)
CALL HORNER(C,X)
CALL COPY(X,C)
END SUB

EXTERNAL  SUB POWERFUNC(F(),G())
!' F[X] ^ G[X] = EXP(G[X]*LOG(F[X]))
CALL LOGFUNC(F)
CALL MUL(F,G)
CALL EXPFUNC(F)
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
PRINT
END SUB

EXTERNAL  SUB COMB(X(),A,B,M,N) !'二項定理  収束半径(ABS(F[X]) < 1)
!' (A+B*X^M)^N = A^N+N*A^(N-1)*B*X^M+N*(N-1)/2!*A^(N-2)*B^2*X^(2*M)+...B^N*X^(M*N)+B^N
CALL CLR(X)
LET  NN=1
LET  X(0)=A^N
FOR I=1 TO INT(MAXLEVEL/M)
   LET  NN=NN*(N-I+1)/I
   LET  X(M*I)=NN*A^(N-I)*B^I
NEXT I
END SUB

EXTERNAL  SUB SINE(X())
!' SIN(X)
CALL CLR(X)
LET  X(1)=1
LET  T=1
FOR I=3 TO MAXLEVEL STEP 2
   LET T=-T/(I-1)/I
   LET  X(I)=T
NEXT I
END SUB

EXTERNAL  SUB COSINE(X())
!' COS(X)
CALL CLR(X)
LET  X(0)=1
LET  T=1
FOR I=2 TO MAXLEVEL STEP 2
   LET T=-T/(I-1)/I
   LET  X(I)=T
NEXT I
END SUB

EXTERNAL  SUB EXPON(X())
!' EXP(X)
LET  X(0)=1
LET  T=1
FOR I=1 TO MAXLEVEL
   LET  T=T/I
   LET  X(I)=T
NEXT I
END SUB

EXTERNAL  SUB LN(X())
!'LOG(1+X)
LET X(0)=0
FOR I=1 TO MAXLEVEL
   IF MOD(I,2)=1 THEN LET  X(I)=1/I ELSE LET  X(I)=-1/I
NEXT I
END SUB

EXTERNAL  SUB SUBST(Y(),X())
FOR I=0 TO MAXLEVEL
   LET  Y(I)=Y(I)-X(I)
NEXT I
END SUB
 

Re: 多項式代入

 投稿者:しばっち  投稿日:2009年 5月10日(日)15時47分11秒
  > No.363[元記事へ]

続き


EXTERNAL  SUB COPY(X(),Y())
FOR I=0 TO MAXLEVEL
   LET  X(I)=Y(I)
NEXT I
END SUB

EXTERNAL  SUB MUL(Y(),X())
OPTION BASE 0
DIM C(MAXLEVEL)
FOR J=0 TO MAXLEVEL
   FOR I=0 TO MAXLEVEL-J
      LET  C(I+J)=C(I+J)+Y(I)*X(J)
   NEXT I
NEXT J
CALL COPY(Y,C)
END SUB

EXTERNAL  SUB CLR(X())
FOR I=0 TO MAXLEVEL
   LET  X(I)=0
NEXT I
END SUB

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

戻る