1% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
2% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
3\newcommand{\PolynomialXmpTitle}{Polynomial}
4\newcommand{\PolynomialXmpNumber}{9.67}
5%
6% =====================================================================
7\begin{page}{PolynomialXmpPage}{9.67 Polynomial}
8% =====================================================================
9\beginscroll
10
11%
12%
13%
14%
15%
16
17
18
19The domain constructor \spadtype{Polynomial} (abbreviation: \spadtype{POLY})
20provides polynomials with an arbitrary number of unspecified
21variables.
22
23\xtc{
24It is used to create the default polynomial domains
25in \Language{}.
26Here the coefficients are integers.
27}{
28\spadpaste{x + 1}
29}
30\xtc{
31Here the coefficients have type \spadtype{Float}.
32}{
33\spadpaste{z - 2.3}
34}
35\xtc{
36And here we have a polynomial in two variables with coefficients which
37have type \spadtype{Fraction Integer}.
38}{
39\spadpaste{y^2 - z + 3/4}
40}
41
42The representation of objects of domains created by \spadtype{Polynomial}
43is that of recursive univariate polynomials.\footnote{The term
44\spad{univariate} means ``one variable.'' \spad{multivariate} means
45``possibly more than one variable.''}
46\xtc{
47This recursive structure is sometimes obvious from the display of
48a polynomial.
49}{
50\spadpaste{y ^2 + x*y + y \bound{prev}}
51}
52In this example, you see that the polynomial is stored as a polynomial in
53\spad{y} with coefficients that are polynomials in \spad{x} with integer
54coefficients.
55In fact, you really don't need to worry about the representation unless
56you are working on an advanced application where it is critical.
57The polynomial types created from
58\spadtype{DistributedMultivariatePolynomial} and
59\spadtype{NewDistributedMultivariatePolynomial} (discussed in
60\downlink{`DistributedMultivariatePolynomial'}{DistributedMultivariatePolynomialXmpPage}\ignore{DistributedMultivariatePolynomial}) are stored and displayed in a
61non-recursive manner.
62\xtc{
63You see a ``flat'' display of the above
64polynomial by converting to one of those types.
65}{
66\spadpaste{\% :: DMP([y,x],INT) \free{prev}}
67}
68
69We will demonstrate many of the polynomial facilities by using two
70polynomials with integer coefficients.
71\xtc{
72By default, the interpreter expands polynomial expressions, even if they
73are written in a factored format.
74}{
75\spadpaste{p := (y-1)^2 * x * z \bound{p}}
76}
77\xtc{
78See \downlink{`Factored'}{FactoredXmpPage}\ignore{Factored} to see
79how to create objects in factored form directly.
80}{
81\spadpaste{q := (y-1) * x * (z+5) \bound{q}}
82}
83\xtc{
84The fully factored form can be recovered by using
85\spadfunFrom{factor}{Polynomial}.
86}{
87\spadpaste{factor(q) \free{q}}
88}
89This is the same name used for the operation to factor integers.
90Such reuse of names is called \spadgloss{overloading} and makes it much easier
91to think of solving problems in general ways.
92\Language{} facilities for factoring polynomials created with
93\spadtype{Polynomial} are currently restricted to
94the integer and rational number coefficient cases.
95There are more complete facilities for factoring univariate polynomials:
96see \downlink{``\ugProblemFactorTitle''}{ugProblemFactorPage} in Section \ugProblemFactorNumber\ignore{ugProblemFactor}.
97
98\xtc{
99The standard arithmetic operations are available for polynomials.
100}{
101\spadpaste{p - q^2\free{p q}}
102}
103\xtc{
104The operation \spadfunFrom{gcd}{Polynomial} is used to compute the
105greatest common divisor of two polynomials.
106}{
107\spadpaste{gcd(p,q) \free{p q}\bound{prev4}}
108}
109\xtc{
110In the case of \spad{p} and \spad{q}, the gcd is obvious from their
111definitions.
112We factor the gcd to show this relationship better.
113}{
114\spadpaste{factor \% \free{prev4}}
115}
116\xtc{
117The least common multiple is computed by using \spadfunFrom{lcm}{Polynomial}.
118}{
119\spadpaste{lcm(p,q) \free{p q}}
120}
121\xtc{
122Use \spadfunFrom{content}{Polynomial} to compute the greatest common divisor of the
123coefficients of the polynomial.
124}{
125\spadpaste{content p \free{p}}
126}
127
128Many of the operations on polynomials require you to specify a variable.
129For example, \spadfunFrom{resultant}{Polynomial} requires you to give the
130variable in which the polynomials should be expressed.
131\xtc{
132This computes the resultant of the values of \spad{p} and
133\spad{q}, considering them as polynomials in the variable \spad{z}.
134They do not share a root when thought of as polynomials in \spad{z}.
135}{
136\spadpaste{resultant(p,q,z) \free{p q}}
137}
138\xtc{
139This value is \spad{0} because as polynomials in \spad{x} the polynomials
140have a common root.
141}{
142\spadpaste{resultant(p,q,x) \free{p}\free{q}}
143}
144The data type used for the variables created by \spadtype{Polynomial} is
145\spadtype{Symbol}.
146As mentioned above, the representation used by \spadtype{Polynomial} is
147recursive and so there is a main variable for nonconstant polynomials.
148\xtc{
149The operation \spadfunFrom{mainVariable}{Polynomial} returns this
150variable.
151The return type is actually a union of \spadtype{Symbol} and
152\spad{"failed"}.
153}{
154\spadpaste{mainVariable p \free{p}}
155}
156\xtc{
157The latter branch of the union is be used if the polynomial has no
158variables, that is, is a constant.
159}{
160\spadpaste{mainVariable(1 :: POLY INT)}
161}
162\xtc{
163You can also use the predicate \spadfunFrom{ground?}{Polynomial} to test
164whether a polynomial is in fact a member of its ground ring.
165}{
166\spadpaste{ground? p \free{p}}
167}
168\xtc{
169}{
170\spadpaste{ground?(1 :: POLY INT)}
171}
172\xtc{
173The complete list of variables actually used in a particular polynomial is
174returned by \spadfunFrom{variables}{Polynomial}.
175For constant polynomials, this list is empty.
176}{
177\spadpaste{variables p \free{p}}
178}
179
180\xtc{
181The \spadfunFrom{degree}{Polynomial} operation returns the
182degree of a polynomial in a specific variable.
183}{
184\spadpaste{degree(p,x) \free{p}}
185}
186\xtc{
187}{
188\spadpaste{degree(p,y) \free{p}}
189}
190\xtc{
191}{
192\spadpaste{degree(p,z) \free{p}}
193}
194\xtc{
195If you give a list of variables for the second argument, a list
196of the degrees in those variables is returned.
197}{
198\spadpaste{degree(p,[x,y,z]) \free{p}}
199}
200\xtc{
201The minimum degree of a variable in a polynomial is computed using
202\spadfunFrom{minimumDegree}{Polynomial}.
203}{
204\spadpaste{minimumDegree(p,z) \free{p}}
205}
206\xtc{
207The total degree of a polynomial is returned by
208\spadfunFrom{totalDegree}{Polynomial}.
209}{
210\spadpaste{totalDegree p \free{p}}
211}
212
213\xtc{
214It is often convenient to think of a polynomial as a leading monomial plus
215the remaining terms.
216}{
217\spadpaste{leadingMonomial p \free{p}}
218}
219\xtc{
220The \spadfunFrom{reductum}{Polynomial} operation returns a polynomial
221consisting of the sum of the monomials after the first.
222}{
223\spadpaste{reductum p \free{p}}
224}
225\xtc{
226These have the obvious relationship that the original polynomial
227is equal to the leading monomial plus the reductum.
228}{
229\spadpaste{p - leadingMonomial p - reductum p \free{p}}
230}
231\xtc{
232The value returned by \spadfunFrom{leadingMonomial}{Polynomial} includes
233the coefficient of that term.
234This is extracted by using
235\spadfunFrom{leadingCoefficient}{Polynomial} on the original polynomial.
236}{
237\spadpaste{leadingCoefficient p \free{p}}
238}
239\xtc{
240The operation \spadfunFrom{eval}{Polynomial} is used to substitute a value
241for a variable in a polynomial.
242}{
243\spadpaste{p  \free{p}}
244}
245\xtc{
246This value may be another variable, a constant or a polynomial.
247}{
248\spadpaste{eval(p,x,w) \free{p}}
249}
250\xtc{
251}{
252\spadpaste{eval(p,x,1) \free{p}}
253}
254\xtc{
255Actually, all the things being substituted are just polynomials,
256some more trivial than others.
257}{
258\spadpaste{eval(p,x,y^2 - 1) \free{p}}
259}
260
261\xtc{
262Derivatives are computed using the \spadfunFrom{D}{Polynomial}
263operation.
264}{
265\spadpaste{D(p,x) \free{p}}
266}
267\xtc{
268The first argument is the polynomial and the second is the variable.
269}{
270\spadpaste{D(p,y) \free{p}}
271}
272\xtc{
273Even if the polynomial has only one variable, you must specify it.
274}{
275\spadpaste{D(p,z) \free{p}}
276}
277
278Integration of polynomials is similar and the
279\spadfunFrom{integrate}{Polynomial} operation is used.
280
281\xtc{
282Integration requires that the coefficients support division.
283Consequently,
284\Language{} converts polynomials over the integers to polynomials over
285the rational numbers before integrating them.
286}{
287\spadpaste{integrate(p,y) \free{p}}
288}
289
290It is not possible, in general, to divide two polynomials.
291In our example using polynomials over the integers, the operation
292\spadfunFrom{monicDivide}{Polynomial} divides a polynomial by a monic
293polynomial (that is, a polynomial with leading coefficient equal to 1).
294The result is a record of the quotient and remainder of the
295division.
296\xtc{
297You must specify the variable in which to express the polynomial.
298}{
299\spadpaste{qr := monicDivide(p,x+1,x) \free{p}\bound{qr}}
300}
301\xtc{
302The selectors of the components of the record are
303\spad{quotient} and \spad{remainder}.
304Issue this to extract the remainder.
305}{
306\spadpaste{qr.remainder \free{qr}}
307}
308\xtc{
309Now that we can extract the components, we can demonstrate the
310relationship among them and the arguments to our original expression
311\spad{qr := monicDivide(p,x+1,x)}.
312}{
313\spadpaste{p - ((x+1) * qr.quotient + qr.remainder) \free{p}\free{qr}}
314}
315
316\xtc{
317If the \spadopFrom{/}{Fraction} operator is used with polynomials, a
318fraction object is created.
319In this example, the result is an object of type
320\spadtype{Fraction Polynomial Integer}.
321}{
322\spadpaste{p/q \free{p}\free{q}}
323}
324\xtc{
325If you use rational numbers as polynomial coefficients, the
326resulting object is of type \spadtype{Polynomial Fraction Integer}.
327}{
328\spadpaste{(2/3) * x^2 - y + 4/5 \bound{prev1}}
329}
330\xtc{
331This can be converted to a fraction of polynomials and back again, if
332required.
333}{
334\spadpaste{\% :: FRAC POLY INT \free{prev1}\bound{prev2}}
335}
336\xtc{
337}{
338\spadpaste{\% :: POLY FRAC INT \free{prev2}\bound{prev3}}
339}
340\xtc{
341To convert the coefficients to floating point,
342map the \spadfun{numeric} operation on the coefficients of the polynomial.
343}{
344\spadpaste{map(numeric,\%) \free{prev3}}
345}
346
347For more information on related topics, see
348\downlink{`UnivariatePolynomial'}{UnivariatePolynomialXmpPage}\ignore{UnivariatePolynomial},
349\downlink{`MultivariatePolynomial'}{MultivariatePolynomialXmpPage}\ignore{MultivariatePolynomial}, and
350\downlink{`DistributedMultivariatePolynomial'}{DistributedMultivariatePolynomialXmpPage}\ignore{DistributedMultivariatePolynomial}.
351You can also issue the system command
352\spadsys{)show Polynomial}
353to display the full list of operations defined by
354\spadtype{Polynomial}.
355\endscroll
356\autobuttons
357\end{page}
358%
359