POLYNOMIALS

Polynomials are important in several branches of mathematics because many continuous functions are represented by polynomials. The familiar quadratic equation is a polynomial of degree 2, and its general form is:

ax^2 + bx + c

where "^" means "raised to the power".

The present program is a general-purpose polynomial manipulator. It will simplify an equation to a polynomial of integer coefficients, and can add, : subtract, multiply, and divide polynomials. The program first asks for the degree of the polynomial; that is, the highest power of X represented. For example:

DEGREE?4
SIMPLIFY:(X+2)*(3-X)*(X+1) 2

will give:

- X^4 - X^3 + 7X^2 + 13X + 6

and:

DEGREE?3
SIMPLIFY:(X^3+1)/(X+1)

will give:

X^2 - X + 1

Program Description

The program works by substituting different values of X in the equation to be simplified, to obtain a series of values of Y. For example, the first equation above produces:

X: 0	1	2	3	4
Y: 6	24	36	0	-150

A 2-stage procedure is then used to obtain the polynomial coefficients from these values. This procedure involves taking differences between successive members of the series, and these differences are divided by the number of the row:

X:	0		1		2		3		4
Y:	6		24		36		0		-150
		18		12		-36		-150
C=1		18		12		-36		-150
			-6		-48		-114
C=2			-3		-24		-57
				-21		-33
C=3				-7		-11
					-4
C=4					-1

The next stage involves taking the series of numbers on the left-hand side of this triangle of differences, namely:

6, 18, -3, -7, -1.

These numbers are then transformed into the coefficients of the polynomial by the following procedure:

  1. Start with N one less than the degree (3 in this case).
  2. Subtract N times the last number from its predecessor.
  3. Subtract (N-1) times the last two numbers from their predecessors.
  4. Repeat step b until N=0.

In the present example the series of coefficients produced is:

6, 13, 7, -l, -l

BBC Computer Version

The BBC Computer BASIC allows negative nurobers to be raised to integer powers; therefore the "^" operator can be used in the expression to be simplified, as in: (X-1)^6.

10 INPUT "DEGREE?"N
20 INPUT "SIMPLIFY:" X$ 
30 DIM CX(N):@%=1

Evaluate equation for successive values of X.

40 FOR X=0 TO N:PROCE:NEXT

Keep taking differences.

50 FOR C=1 TO N
60 FOR J=N TO C STEP-1
70 CX(J)=(CX(J) MX(J-1)) DIV C
80 NEXT J:NEXT C

Transform differences into coefficients of polynomial.

90 FOR C=N-1 TO 0 STEP-1
100 FOR J=C TO N-1
110 CX(J)=CX(J) MX(J+1)*C
120 NEXT J:NEXT C

Print non-zero terms of polynomial, with a leading "-" if first non-zero term is negative.

125 S=0
130 FOR C=N TO 0 STEP-1: IF CX{C)=0 GOTO 160
135 IF CX(C)<0 PRINT "- ";ELSE IF S PRINT "+ ";
140 IF ABS(CX(C))>1 OR C=0 PRINT ABS(CX(C));
145 IF C>l PRINT "X"";C;
148 IF C=l PRINT "X";
150 S=1
160 NEXT C:PRINT '
170 END

PROCE - Evaluate equation here.

200 DEF PROCE CX(X)=EVAL(X$): ENDPROC

Variables:

C - Coefficient number
CX(0)..CX(N) - Coefficients of X^N in polynomial
J - Counter
N - Maximum degree of polynomial
S - Flag for printing "+" sign
T - Pointer to equation string
X,Y - Unknowns in equation

Atom Version

In the Atom version, powers of numbers and polynomials should be represented by repeated multiplication; thus, instead of '(X+1)"2' write '(X+1)*(X+1)’.

10 INPUT "DEGREE"N

Insert equation into line 200 of program.

15 T-TOP;DO T=T-1; UNTIL ?T=CH"e"
20 T-T+3;INPUT "SIMPLIFY"$T
25 T=T+LENT;$T=";R.";T?4=#FF
30 DIM T(64),XX(N);@=0

Evaluate equation for successive values of X.

40 FOR X=0 TO N; GOSUB e; XX(X)=Y; NEXT

Keep taking differences.

50 FOR C=1 TO N
60 FOR J=N TO C STEP -1
70 XX(J)=(XX(J)-XX(J-1))/C 
80 NEXT J;NEXT C

Transform differences into coefficients of polynomial.

90 FOR C=N-1 TO 0 STEP -1
100 FOR J=C TO N-1
110 XX(J)=XX(J)-XX(J+1)*C
120 NEXT J;NEXT C

Print non-zero terms of polynomial, with a leading "-" if first non-zero term is negative.

125 S=0
130 FOR C=N TO 0 STEP -1;IF XX(C)=0 GOTO z
135 IF XX(C)<0 PRINT " - "; GOTO s
137 IF S PRINT " + "
140sIF ABS XX(C)>1 OR C=0 PRINT ABS XX(C)
145 IF C>1 PRINT "X" C
148 IF C=l PRINT "X"
150 S=1
155zNEXT C;PRINT ''
170 END

Poke equation to be evaluated into program here.

200eY=X;RETURN

Variables:

C - Coefficient number
J - Counter
N - Maximum degree of polynomial
S - Flag for printing "+" sign
T - Pointer to equation string
XX(0) ..XX(N) - Coefficients of X^N in polynomial
X,Y - Unknowns in equation

Further Suggestions

With slight modification the program can be used to generate a polynomial of specified degree from a set of data points. For the BBC version, alter line 200 to:

200 DEF PROCE PRINT "F("X")";:INPUTY:ENDPROC

For the Atom version delete lines 15, 20, and 25, and alter line 200 to:

200ePRINT "F("X")";INPUTY;RETURN

The program will then prompt for the coefficients:

DEGREE?4
F(0)?6
F(1)?24 F(2)?36 F(3)?0 F(4)?-150

This will print:

- X^4 - X^3 + 7X^2 + 13X + 6

The program could also be modified to handle polynomials with real coefficients, and the Fractions program could be used to give the coefficients as fractions where possible.