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.63}
6
% =====================================================================
7
\begin{page}{PolynomialXmpPage}{9.63 Polynomial}
8
% =====================================================================
11
The domain constructor \spadtype{Polynomial} (abbreviation: \spadtype{POLY})
12
provides polynomials with an arbitrary number of unspecified
16
It is used to create the default polynomial domains
18
Here the coefficients are integers.
23
Here the coefficients have type \spadtype{Float}.
28
And here we have a polynomial in two variables with coefficients which
29
have type \spadtype{Fraction Integer}.
31
\spadpaste{y**2 - z + 3/4}
34
The representation of objects of domains created by \spadtype{Polynomial}
35
is that of recursive univariate polynomials.\footnote{The term
36
\spad{univariate} means ``one variable.'' \spad{multivariate} means
37
``possibly more than one variable.''}
39
This recursive structure is sometimes obvious from the display of
42
\spadpaste{y **2 + x*y + y \bound{prev}}
44
In this example, you see that the polynomial is stored as a polynomial in
45
\spad{y} with coefficients that are polynomials in \spad{x} with integer
47
In fact, you really don't need to worry about the representation unless
48
you are working on an advanced application where it is critical.
49
The polynomial types created from
50
\spadtype{DistributedMultivariatePolynomial} and
51
\spadtype{NewDistributedMultivariatePolynomial} (discussed in
52
\downlink{`DistributedMultivariatePolynomial'}{DistributedMultivariatePolynomialXmpPage}\ignore{DistributedMultivariatePolynomial}) are stored and displayed in a
55
You see a ``flat'' display of the above
56
polynomial by converting to one of those types.
58
\spadpaste{\% :: DMP([y,x],INT) \free{prev}}
61
We will demonstrate many of the polynomial facilities by using two
62
polynomials with integer coefficients.
64
By default, the interpreter expands polynomial expressions, even if they
65
are written in a factored format.
67
\spadpaste{p := (y-1)**2 * x * z \bound{p}}
70
See \downlink{`Factored'}{FactoredXmpPage}\ignore{Factored} to see
71
how to create objects in factored form directly.
73
\spadpaste{q := (y-1) * x * (z+5) \bound{q}}
76
The fully factored form can be recovered by using
77
\spadfunFrom{factor}{Polynomial}.
79
\spadpaste{factor(q) \free{q}}
81
This is the same name used for the operation to factor integers.
82
Such reuse of names is called \spadglos{overloading} and makes it much easier
83
to think of solving problems in general ways.
84
\Language{} facilities for factoring polynomials created with
85
\spadtype{Polynomial} are currently restricted to
86
the integer and rational number coefficient cases.
87
There are more complete facilities for factoring univariate polynomials:
88
see \downlink{``\ugProblemFactorTitle''}{ugProblemFactorPage} in Section \ugProblemFactorNumber\ignore{ugProblemFactor}.
91
The standard arithmetic operations are available for polynomials.
93
\spadpaste{p - q**2\free{p q}}
96
The operation \spadfunFrom{gcd}{Polynomial} is used to compute the
97
greatest common divisor of two polynomials.
99
\spadpaste{gcd(p,q) \free{p q}\bound{prev4}}
102
In the case of \spad{p} and \spad{q}, the gcd is obvious from their
104
We factor the gcd to show this relationship better.
106
\spadpaste{factor \% \free{prev4}}
109
The least common multiple is computed by using \spadfunFrom{lcm}{Polynomial}.
111
\spadpaste{lcm(p,q) \free{p q}}
114
Use \spadfunFrom{content}{Polynomial} to compute the greatest common divisor of the
115
coefficients of the polynomial.
117
\spadpaste{content p \free{p}}
120
Many of the operations on polynomials require you to specify a variable.
121
For example, \spadfunFrom{resultant}{Polynomial} requires you to give the
122
variable in which the polynomials should be expressed.
124
This computes the resultant of the values of \spad{p} and
125
\spad{q}, considering them as polynomials in the variable \spad{z}.
126
They do not share a root when thought of as polynomials in \spad{z}.
128
\spadpaste{resultant(p,q,z) \free{p q}}
131
This value is \spad{0} because as polynomials in \spad{x} the polynomials
134
\spadpaste{resultant(p,q,x) \free{p}\free{q}}
136
The data type used for the variables created by \spadtype{Polynomial} is
138
As mentioned above, the representation used by \spadtype{Polynomial} is
139
recursive and so there is a main variable for nonconstant polynomials.
141
The operation \spadfunFrom{mainVariable}{Polynomial} returns this
143
The return type is actually a union of \spadtype{Symbol} and
146
\spadpaste{mainVariable p \free{p}}
149
The latter branch of the union is be used if the polynomial has no
150
variables, that is, is a constant.
152
\spadpaste{mainVariable(1 :: POLY INT)}
155
You can also use the predicate \spadfunFrom{ground?}{Polynomial} to test
156
whether a polynomial is in fact a member of its ground ring.
158
\spadpaste{ground? p \free{p}}
162
\spadpaste{ground?(1 :: POLY INT)}
165
The complete list of variables actually used in a particular polynomial is
166
returned by \spadfunFrom{variables}{Polynomial}.
167
For constant polynomials, this list is empty.
169
\spadpaste{variables p \free{p}}
173
The \spadfunFrom{degree}{Polynomial} operation returns the
174
degree of a polynomial in a specific variable.
176
\spadpaste{degree(p,x) \free{p}}
180
\spadpaste{degree(p,y) \free{p}}
184
\spadpaste{degree(p,z) \free{p}}
187
If you give a list of variables for the second argument, a list
188
of the degrees in those variables is returned.
190
\spadpaste{degree(p,[x,y,z]) \free{p}}
193
The minimum degree of a variable in a polynomial is computed using
194
\spadfunFrom{minimumDegree}{Polynomial}.
196
\spadpaste{minimumDegree(p,z) \free{p}}
199
The total degree of a polynomial is returned by
200
\spadfunFrom{totalDegree}{Polynomial}.
202
\spadpaste{totalDegree p \free{p}}
206
It is often convenient to think of a polynomial as a leading monomial plus
209
\spadpaste{leadingMonomial p \free{p}}
212
The \spadfunFrom{reductum}{Polynomial} operation returns a polynomial
213
consisting of the sum of the monomials after the first.
215
\spadpaste{reductum p \free{p}}
218
These have the obvious relationship that the original polynomial
219
is equal to the leading monomial plus the reductum.
221
\spadpaste{p - leadingMonomial p - reductum p \free{p}}
224
The value returned by \spadfunFrom{leadingMonomial}{Polynomial} includes
225
the coefficient of that term.
226
This is extracted by using
227
\spadfunFrom{leadingCoefficient}{Polynomial} on the original polynomial.
229
\spadpaste{leadingCoefficient p \free{p}}
232
The operation \spadfunFrom{eval}{Polynomial} is used to substitute a value
233
for a variable in a polynomial.
235
\spadpaste{p \free{p}}
238
This value may be another variable, a constant or a polynomial.
240
\spadpaste{eval(p,x,w) \free{p}}
244
\spadpaste{eval(p,x,1) \free{p}}
247
Actually, all the things being substituted are just polynomials,
248
some more trivial than others.
250
\spadpaste{eval(p,x,y**2 - 1) \free{p}}
254
Derivatives are computed using the \spadfunFrom{D}{Polynomial}
257
\spadpaste{D(p,x) \free{p}}
260
The first argument is the polynomial and the second is the variable.
262
\spadpaste{D(p,y) \free{p}}
265
Even if the polynomial has only one variable, you must specify it.
267
\spadpaste{D(p,z) \free{p}}
270
Integration of polynomials is similar and the
271
\spadfunFrom{integrate}{Polynomial} operation is used.
274
Integration requires that the coefficients support division.
276
\Language{} converts polynomials over the integers to polynomials over
277
the rational numbers before integrating them.
279
\spadpaste{integrate(p,y) \free{p}}
282
It is not possible, in general, to divide two polynomials.
283
In our example using polynomials over the integers, the operation
284
\spadfunFrom{monicDivide}{Polynomial} divides a polynomial by a monic
285
polynomial (that is, a polynomial with leading coefficient equal to 1).
286
The result is a record of the quotient and remainder of the
289
You must specify the variable in which to express the polynomial.
291
\spadpaste{qr := monicDivide(p,x+1,x) \free{p}\bound{qr}}
294
The selectors of the components of the record are
295
\spad{quotient} and \spad{remainder}.
296
Issue this to extract the remainder.
298
\spadpaste{qr.remainder \free{qr}}
301
Now that we can extract the components, we can demonstrate the
302
relationship among them and the arguments to our original expression
303
\spad{qr := monicDivide(p,x+1,x)}.
305
\spadpaste{p - ((x+1) * qr.quotient + qr.remainder) \free{p}\free{qr}}
309
If the \spadopFrom{/}{Fraction} operator is used with polynomials, a
310
fraction object is created.
311
In this example, the result is an object of type \spadtype{Fraction
314
\spadpaste{p/q \free{p}\free{q}}
317
If you use rational numbers as polynomial coefficients, the
318
resulting object is of type \spadtype{Polynomial Fraction Integer}.
320
\spadpaste{(2/3) * x**2 - y + 4/5 \bound{prev1}}
323
This can be converted to a fraction of polynomials and back again, if
326
\spadpaste{\% :: FRAC POLY INT \free{prev1}\bound{prev2}}
330
\spadpaste{\% :: POLY FRAC INT \free{prev2}\bound{prev3}}
333
To convert the coefficients to floating point,
334
map the \spadfun{numeric} operation on the coefficients of the polynomial.
336
\spadpaste{map(numeric,\%) \free{prev3}}
339
For more information on related topics, see
340
\downlink{`UnivariatePolynomial'}{UnivariatePolynomialXmpPage}\ignore{UnivariatePolynomial},
341
\downlink{`MultivariatePolynomial'}{MultivariatePolynomialXmpPage}\ignore{MultivariatePolynomial}, and
342
\downlink{`DistributedMultivariatePolynomial'}{DistributedMultivariatePolynomialXmpPage}\ignore{DistributedMultivariatePolynomial}.
343
You can also issue the system command
344
\spadcmd{)show Polynomial}
345
to display the full list of operations defined by
346
\spadtype{Polynomial}.