1 2% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. 3% All rights reserved. 4% 5% Redistribution and use in source and binary forms, with or without 6% modification, are permitted provided that the following conditions are 7% met: 8% 9% - Redistributions of source code must retain the above copyright 10% notice, this list of conditions and the following disclaimer. 11% 12% - Redistributions in binary form must reproduce the above copyright 13% notice, this list of conditions and the following disclaimer in 14% the documentation and/or other materials provided with the 15% distribution. 16% 17% - Neither the name of The Numerical ALgorithms Group Ltd. nor the 18% names of its contributors may be used to endorse or promote products 19% derived from this software without specific prior written permission. 20% 21% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 25% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR 28% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33 34 35The domain constructor \spadtype{UnivariatePolynomial} 36\index{polynomial!one variable} 37(abbreviated \spadtype{UP}) 38creates domains of univariate polynomials in a specified variable. 39For example, the domain 40\spadtype{UP(a1,POLY FRAC INT)} provides polynomials in the single variable 41\spad{a1} whose coefficients are general polynomials with rational 42number coefficients. 43 44\beginImportant 45\noindent {\bf Restriction:} 46\texht{\begin{quotation}\noindent}{\newline\indent{5}} 47\Language{} does not allow you to create types where 48\spadtype{UnivariatePolynomial} is contained in the coefficient type of 49\spadtype{Polynomial}. Therefore, 50\spadtype{UP(x,POLY INT)} is legal but \spadtype{POLY UP(x,INT)} is not. 51\texht{\end{quotation}}{\indent{0}} 52\endImportant 53 54\xtc{ 55\spadtype{UP(x,INT)} is the domain of polynomials in the single 56variable \spad{x} with integer coefficients. 57}{ 58\spadcommand{(p,q) : UP(x,INT) \bound{pdec}\bound{qdec}} 59} 60\xtc{ 61}{ 62\spadcommand{p := (3*x-1)^2 * (2*x + 8) \free{pdec}\bound{p}} 63} 64\xtc{ 65}{ 66\spadcommand{q := (1 - 6*x + 9*x^2)^2 \free{qdec}\bound{q}} 67} 68\xtc{ 69The usual arithmetic operations are available for univariate 70polynomials. 71}{ 72\spadcommand{p^2 + p*q \free{p q}} 73} 74\xtc{ 75The operation \spadfunFrom{leadingCoefficient}{UnivariatePolynomial} 76extracts the coefficient of the term of highest degree. 77}{ 78\spadcommand{leadingCoefficient p \free{p}} 79} 80\xtc{ 81The operation \spadfunFrom{degree}{UnivariatePolynomial} returns 82the degree of the polynomial. 83Since the polynomial has only one variable, the variable is not supplied 84to operations like \spadfunFrom{degree}{UnivariatePolynomial}. 85}{ 86\spadcommand{degree p \free{p}} 87} 88\xtc{ 89The reductum of the polynomial, the polynomial obtained by 90subtracting the term of highest order, is returned by 91\spadfunFrom{reductum}{UnivariatePolynomial}. 92}{ 93\spadcommand{reductum p \free{p}} 94} 95\xtc{ 96The operation \spadfunFrom{gcd}{UnivariatePolynomial} computes the 97greatest common divisor of two polynomials. 98}{ 99\spadcommand{gcd(p,q) \free{p q}} 100} 101\xtc{ 102The operation \spadfunFrom{lcm}{UnivariatePolynomial} computes the 103least common multiple. 104}{ 105\spadcommand{lcm(p,q) \free{p q}} 106} 107\xtc{ 108The operation \spadfunFrom{resultant}{UnivariatePolynomial} 109computes the resultant of two univariate polynomials. 110In the case of \spad{p} and \spad{q}, the resultant is \spad{0} because they 111share a common root. 112}{ 113\spadcommand{resultant(p,q) \free{p q}} 114} 115\xtc{ 116To compute the derivative of a univariate polynomial with respect to its 117variable, use \spadfunFrom{D}{UnivariatePolynomial}. 118}{ 119\spadcommand{D p \free{p}} 120} 121\xtc{ 122Univariate polynomials can also be used as if they were functions. 123To evaluate a univariate polynomial at some point, apply 124the polynomial to the point. 125}{ 126\spadcommand{p(2) \free{p}} 127} 128\xtc{ 129The same syntax is used for composing two univariate polynomials, i.e. 130substituting one polynomial for the variable in another. 131This substitutes \spad{q} for the variable in \spad{p}. 132}{ 133\spadcommand{p(q) \free{p q}} 134} 135\xtc{ 136This substitutes \spad{p} for the variable in \spad{q}. 137}{ 138\spadcommand{q(p) \free{p q}} 139} 140\xtc{ 141To obtain a list of coefficients of the polynomial, use 142\spadfunFrom{coefficients}{UnivariatePolynomial}. 143}{ 144\spadcommand{l := coefficients p \free{p}\bound{l}} 145} 146\xtc{ 147From this you can use \spadfunFrom{gcd}{UnivariatePolynomial} 148and \spadfunFrom{reduce}{List} 149to compute the content of the polynomial. 150}{ 151\spadcommand{reduce(gcd,l) \free{l}} 152} 153\xtc{ 154Alternatively (and more easily), 155you can just call \spadfunFrom{content}{UnivariatePolynomial}. 156}{ 157\spadcommand{content p \free{p}} 158} 159 160Note that the operation \spadfunFrom{coefficients}{UnivariatePolynomial} 161omits the zero coefficients from the list. 162Sometimes it is useful to convert a univariate polynomial 163to a vector whose \eth{i} position contains the degree \spad{i-1} 164coefficient of the polynomial. 165\xtc{ 166}{ 167\spadcommand{ux := (x^4+2*x+3)::UP(x,INT) \bound{ux}} 168} 169\xtc{ 170To get a complete vector of coefficients, use the operation 171\spadfunFrom{vectorise}{UnivariatePolynomial}, which takes a 172univariate polynomial and an integer denoting the length of the 173desired vector. 174}{ 175\spadcommand{vectorise(ux,5) \free{ux}} 176} 177 178It is common to want to do something to every term of a polynomial, 179creating a new polynomial in the process. 180\xtc{ 181This is a function for iterating across the terms of a polynomial, 182squaring each term. 183}{ 184\begin{spadsrc}[\bound{squareTerms}] 185squareTerms(p) == 186 reduce(+,[t^2 for t in monomials p]) 187\end{spadsrc} 188} 189\xtc{ 190Recall what \spad{p} looked like. 191}{ 192\spadcommand{p \free{p}} 193} 194\xtc{ 195We can demonstrate \userfun{squareTerms} on \spad{p}. 196}{ 197\spadcommand{squareTerms p \free{p}\free{squareTerms}} 198} 199 200When the coefficients of the univariate polynomial belong to a 201field,\footnote{For example, when the coefficients are rational 202numbers, as opposed to integers. The important property of 203a field is that non-zero elements can be divided and produce 204another element. The quotient of the integers 2 and 3 is not 205another integer.} 206it is possible to compute quotients and remainders. 207\xtc{ 208}{ 209\spadcommand{(r,s) : UP(a1,FRAC INT) \bound{rdec}\bound{sdec}} 210} 211\xtc{ 212}{ 213\spadcommand{r := a1^2 - 2/3 \free{rdec}\bound{r}} 214} 215\xtc{ 216}{ 217\spadcommand{s := a1 + 4 \free{sdec}\bound{s}} 218} 219\xtc{ 220When the coefficients are rational numbers or rational expressions, the 221operation \spadfunFrom{quo}{UnivariatePolynomial} computes the quotient 222of two polynomials. 223}{ 224\spadcommand{r quo s \free{r s}} 225} 226\xtc{ 227The operation 228\spadfunFrom{rem}{UnivariatePolynomial} computes the remainder. 229}{ 230\spadcommand{r rem s \free{r s}} 231} 232\xtc{ 233The operation \spadfunFrom{divide}{UnivariatePolynomial} can be used to 234return a record of both components. 235}{ 236\spadcommand{d := divide(r, s) \free{r s}\bound{d}} 237} 238\xtc{ 239Now we check the arithmetic! 240}{ 241\spadcommand{r - (d.quotient * s + d.remainder) \free{r s d}} 242} 243\xtc{ 244It is also possible to integrate univariate polynomials when the 245coefficients belong to a field. 246}{ 247\spadcommand{integrate r \free{r}} 248} 249\xtc{ 250}{ 251\spadcommand{integrate s \free{s}} 252} 253 254One application of univariate polynomials is to see expressions in terms 255of a specific variable. 256% 257\xtc{ 258We start with a polynomial in \spad{a1} whose coefficients 259are quotients of polynomials in \spad{b1} and \spad{b2}. 260}{ 261\spadcommand{t : UP(a1,FRAC POLY INT) \bound{tdec}} 262} 263\xtc{ 264Since in this case we are not talking about using multivariate 265polynomials in only two variables, we use \spadtype{Polynomial}. 266We also use \spadtype{Fraction} because we want fractions. 267}{ 268\spadcommand{t := a1^2 - a1/b2 + (b1^2-b1)/(b2+3) \free{tdec}\bound{t}} 269} 270\xtc{ 271We push all the variables into a single quotient of polynomials. 272}{ 273\spadcommand{u : FRAC POLY INT := t \bound{u}\free{t}} 274} 275\xtc{ 276Alternatively, we can view this as a polynomial in the variable 277This is a {\it mode-directed} conversion: you indicate 278as much of the structure as you care about and let \Language{} 279decide on the full type and how to do the transformation. 280}{ 281\spadcommand{u :: UP(b1,?) \free{u}} 282} 283 284See \spadref{ugProblemFactor} 285for a discussion of the factorization facilities 286in \Language{} for univariate polynomials. 287For more information on related topics, see 288\spadref{ugIntroVariables}, 289\spadref{ugTypesConvert}, 290\xmpref{Polynomial}, 291\xmpref{MultivariatePolynomial}, and 292\xmpref{DistributedMultivariatePolynomial}. 293% 294\showBlurb{UnivariatePolynomial} 295