A Univariate Polynomial Class for S
===================================
Introduction and Summary:
The following started as a straightforward programming exercise in
operator overloading, but seems to be more generally useful. The goal
is to write a polynomial class, that is a suite of facilities that allow
operations on polynomials: addition, subtraction, multiplication,
“division”, remaindering, printing, plotting, and so forth, to be
conducted using the same operators and functions, and hence with the
same ease, as ordinary arithmetic, plotting, printing, and so on.
The class is limited to univariate polynomials, and so they may
therefore be uniquely defined by their numeric coefficient vector.
Coercing a polynomial to numeric yields this coefficient vector as a
numeric vector.
For reasons of simplicity it is limited to REAL polynomials; handling
polynomials with complex coefficients would be a simple extension.
Dealing with polynomials with polynomial coefficients, and hence
multivariate polynomials, would be feasible, though a major undertaking
and the result would be very slow and of rather limited usefulness and
efficiency.
General Orientation:
The function polynomial() creates an object of class polynomial' from a numeric coefficient vector. Coefficient vectors are assumed to apply to the powers of the carrier variable in increasing order, that is, in the truncated power series’ form, and in the same form as required by
polyroot(), the system function for computing zeros of polynomials. (As
a matter or terminology, the “zeros” of the polynomial P(x) are the same
as the “roots” of equation P(x) = 0.)
Polynomials may also be created by specifying a set of (x,y) pairs and
constructing the Lagrange interpolation polynomial that passes through
them (poly.calc(x, y)). If y is a matrix, an interpolation polynomial
is calculated for each column and the result is a list of polynomials
(of class `polylist’).
The third way polynomials are commonly generated is via its zeros using
poly.calc(z), which creates the monic polynomial of lowest degree with
the values in z as its zeros.
The core facility provided is the group method function
Ops.polynomial(), which allows arithmetic operations to be performed on
polynomial arguments using ordinary arithmetic operators.
Notes:
+', -‘ and `*’ have their obvious meanings for polynomials.
`^’ is limited to non-negative integer powers.
`/‘ returns the polynomial quotient. If division is not exact the
remainder is discarded, (but see 4.)
`%%’ returns the polynomial remainder, so that if all arguments are
polynomials, p1 * (p2 / p1) + p2 %% p1 is the same polynomial as p2,
provided p1 is not the zero polynomial.
If numeric vectors are used in polynomial arithmetic they are
coerced to polynomial, which could be a source of surprise. In the
case of scalars, though, the result is natural.
Some logical operations are allowed, but not always very
satisfactorily. ==' and !=’ mean exact equality or not,
respectively, however <', <=’, >', >=’, !', |’ and `&’ are not
allowed at all and cause stops in the calculation.
Most Math group functions are disallowed with polynomial arguments.
The only exceptions are ceiling', floor’, round', trunc’, and
`signif’.
Summary group functions are not allowed.
Polynomials may be evaluated at specific x values either directly
using predict(p, x), or indirectly using as.function(p), which
creates a function to evaluate the polynomial, and then using the
result.
The print method for polynomials can be slow and is a bit
pretentious. The plotting methods (plot(p), lines(p), points(p))
are fairly nominal, but may prove useful.
Examples:
Find the Hermite polynomials up to degree 5 and plot them.
Also plot their derivatives and integrals on separate plots.
The polynomials in question satisfy He(0) = 1, He(1) = x,
He(n) = x * He(n) - (n - 1) * He(n-1), n = 2, 3, …
He <- list(polynomial(1), polynomial(0:1))
for (n in 3:6) He[[n]] <- 0:1 * He[[n-1]] - (n-2) * He[[n-2]]
He
[[1]]:
1
[[2]]:
x
[[3]]:
-1 + x^2
[[4]]:
-3*x + x^3
[[5]]:
3 - 6*x^2 + x^4
[[6]]:
15x - 10x^3 + x^5
plot(He[[6]])
for(n in 1:5) lines(He[[n]], lty=n)
plot(deriv(He[[6]]))
for(n in 1:5) lines(deriv(He[[n]]), lty=n)
plot(integral(He[[6]]))
for(n in 1:5) lines(integral(He[[n]]), lty=n)
Find the orthogonal polynomials on x = (0, 1, 2, 4) and
construct S functions to evaluate them at arbitrary x values.
x <- c(0,1,2,4)
op <- poly.orth(x)
op
List of polynomials:
[[1]]
0.5
‘polynom’ is an R collection of functions to implement a class for univariate polynomial manipulations. It is based on the corresponding S package by Bill Venables Bill.Venables@adelaide.edu.au, and was adapted to R by Kurt Hornik Kurt.Hornik@R-project.org and Martin Maechler maechler@stat.math.ethz.ch.
The original ‘NOTES’ is appended below:
Introduction and Summary:
The following started as a straightforward programming exercise in operator overloading, but seems to be more generally useful. The goal is to write a polynomial class, that is a suite of facilities that allow operations on polynomials: addition, subtraction, multiplication, “division”, remaindering, printing, plotting, and so forth, to be conducted using the same operators and functions, and hence with the same ease, as ordinary arithmetic, plotting, printing, and so on.
The class is limited to univariate polynomials, and so they may therefore be uniquely defined by their numeric coefficient vector. Coercing a polynomial to numeric yields this coefficient vector as a numeric vector.
For reasons of simplicity it is limited to REAL polynomials; handling polynomials with complex coefficients would be a simple extension. Dealing with polynomials with polynomial coefficients, and hence multivariate polynomials, would be feasible, though a major undertaking and the result would be very slow and of rather limited usefulness and efficiency.
General Orientation:
The function polynomial() creates an object of class
polynomial' from a numeric coefficient vector. Coefficient vectors are assumed to apply to the powers of the carrier variable in increasing order, that is, in thetruncated power series’ form, and in the same form as required by polyroot(), the system function for computing zeros of polynomials. (As a matter or terminology, the “zeros” of the polynomial P(x) are the same as the “roots” of equation P(x) = 0.)Polynomials may also be created by specifying a set of (x,y) pairs and constructing the Lagrange interpolation polynomial that passes through them (poly.calc(x, y)). If y is a matrix, an interpolation polynomial is calculated for each column and the result is a list of polynomials (of class `polylist’).
The third way polynomials are commonly generated is via its zeros using poly.calc(z), which creates the monic polynomial of lowest degree with the values in z as its zeros.
The core facility provided is the group method function Ops.polynomial(), which allows arithmetic operations to be performed on polynomial arguments using ordinary arithmetic operators.
Notes:
+',-‘ and `*’ have their obvious meanings for polynomials.==' and!=’ mean exact equality or not, respectively, however<',<=’,>',>=’,!',|’ and `&’ are not allowed at all and cause stops in the calculation.ceiling',floor’,round',trunc’, and `signif’.Examples:
Find the Hermite polynomials up to degree 5 and plot them. Also plot their derivatives and integrals on separate plots.
The polynomials in question satisfy He(0) = 1, He(1) = x, He(n) = x * He(n) - (n - 1) * He(n-1), n = 2, 3, …
[[2]]: x
[[3]]: -1 + x^2
[[4]]: -3*x + x^3
[[5]]: 3 - 6*x^2 + x^4
[[6]]: 15x - 10x^3 + x^5
[[2]] -0.591608 + 0.3380617*x
[[3]] 0.5640761 - 1.168443x + 0.282038x^2
[[4]] -0.2860388 + 3.114644x - 2.502839x^2 + 0.4370037*x^3
[excess output deleted for simplicity]