'***************************************************** '* This program calculates R(x) = a*P(x) + b*Q(x), * '* P(x), Q(x) and R(x) being polynomials. * '* ------------------------------------------------- * '* Ref.: "Mathematiques en Turbo-Pascal By M. Ducamp * '* and A. Reverchon (vol 2), Eyrolles, Paris, 1988" * '* [BIBLI 05]. * '* ------------------------------------------------- * '* SAMPLE RUN: * '* * '* LINEAR COMBINATION OF TWO POLYNOMIALS: * '* * '* P(X) = x3 +5/4x2 -8 * '* Q(x) = 2x2 -1/7 * '* * '* a = 1/2 * '* b = -1/3 * '* * '* * '* + 1 / 2 X3 - 1 / 24 X2 - 83 / 21 * '* * '* * '* BASIC version by J-P Moreau. * '* (www.jpmoreau.fr) * '***************************************************** defdbl a-h,o-z defint i-n MAXINT = 32767 'Maximum integer number MAXPOL = 20 'Maximum degree for a polynomial SMALL = 1e-20 'small real number ' number zz (real, integer or fractional) ' isrzz=1: zz is real (double precision) ' isrzz=0: zz is integer or fractional ' zzv: value of zz if real ' ipzz: integer value of zz if integer (or numerator value if fractional) ' iqzz: denominator value if fractional (=1 if integer) 'a polynomial P is defined by: 'integer ipdeg (degree of polynomial) 'number cp(MAXPOL) (coefficients of polynomial, real, integer or fractional) 'predefine 3 polynomials P,Q,R DIM isrcp(MAXPOL),cpv(MAXPOL),ipcp(MAXPOL),iqcp(MAXPOL) DIM isrcq(MAXPOL),cqv(MAXPOL),ipcq(MAXPOL),iqcq(MAXPOL) DIM isrcr(MAXPOL),crv(MAXPOL),ipcr(MAXPOL),iqcr(MAXPOL) cls print print " LINEAR COMBINATION OF TWO POLYNOMIALS:" print tx\$=" P(x) = ": gosub 4000 'Enter P(x) 'put P(x) in R(x) irdeg=ipdeg for i=0 to ipdeg isrcr(i)=isrcp(i) crv(i)=cpv(i) ipcr(i)=ipcp(i) iqcr(i)=iqcp(i) next i tx\$=" Q(x) = ": gosub 4000 'Enter P(x) 'put P(x) in Q(x) iqdeg=ipdeg for i=0 to ipdeg isrcq(i)=isrcp(i) cqv(i)=cpv(i) ipcq(i)=ipcp(i) iqcq(i)=iqcp(i) next i 'restore P(x) ipdeg=irdeg for i=0 to irdeg isrcp(i)=isrcr(i) cpv(i)=crv(i) ipcp(i)=ipcr(i) iqcp(i)=iqcr(i) next i print 'read number a input " a = ", ch\$: gosub 1000 'zz=SetNumber(ch\$) 'put zz in a isra=isrzz:av=zzv:ipa=ipzz:iqa=iqzz 'read number b input " b = ", ch\$: gosub 1000 'zz=SetNumber(ch\$) 'put zz in b isrb=isrzz:bv=zzv:ipb=ipzz:iqb=iqzz gosub 500 'R(x)=CombiPolynom(P,Q,a,b) if IERROR<>0 then print " Error in combination." else 'put R(x) in P(x) for printing ipdeg=irdeg for i=0 to irdeg isrcp(i)=isrcr(i) cpv(i)=crv(i) ipcp(i)=ipcr(i) iqcp(i)=iqcr(i) next i print gosub 5000 'display P(x) end if END ' a P(X) + b Q(X) = R(X) 'CombiPolynom 'u,v: NUMBER 500 IERROR=0 if iqdeg > ipdeg then irdeg=iqdeg else irdeg=ipdeg end if if irdeg > MAXPOL then IERROR=1 return 'degree of R(x) is too big end if for i=0 to irdeg 'set u and v to zero isru=0:uv=0#:ipu=0:iqu=1 isrv=0:vv=0#:ipv=0:iqv=1 if i<=ipdeg then 'MultNumber(a,P.coeff[i], u) isrxx=isra:xxv=av:ipxx=ipa:iqxx=iqa 'xx=a isryy=isrcp(i):yyv=cpv(i):ipyy=ipcp(i):iqyy=iqcp(i) 'yy=P coeff(i) gosub 3100 'zz=xx*yy isru=isrzz:uv=zzv:ipu=ipzz:iqu=iqzz 'u=zz end if if i<=iqdeg then 'MultNumber(b,Q.coeff[i], v) isrxx=isrb:xxv=bv:ipxx=ipb:iqxx=iqb 'xx=b isryy=isrcq(i):yyv=cqv(i):ipyy=ipcq(i):iqyy=iqcq(i) 'yy=Q coeff(i) gosub 3100 'zz=xx*yy isrv=isrzz:vv=zzv:ipv=ipzz:iqv=iqzz 'v=zz end if 'AddNumber(u,v,R.coeff[i]) isrxx=isru:xxv=uv:ipxx=ipu:iqxx=iqu 'xx=u isryy=isrv:yyv=vv:ipyy=ipv:iqyy=iqv 'yy=v gosub 3000 'zz=xx+yy isrcr(i)=isrzz:crv(i)=zzv:ipcr(i)=ipzz:iqcr(i)=iqzz 'R coeff(i)=zz next i 510 if irdeg<=0 or abs(crv(irdeg))>SMALL then goto 520 irdeg=irdeg-1 goto 510 520 return \$INCLUDE "Numeric\Polynoms\polynoms.bas" 'end of file combipol.bas