Coerce a Polynomial to a Function
Description
Takes a polynomial argument and constructs an R function to evaluate
it at arbitrary points.
Usage
as.function(x, ...)
Arguments
x |
An object of class "polynomial" .
|
... |
further arguments to be passed to or from methods.
|
Details
This is a method for the generic function as.function
.
The polynomial is evaluated within the function using the Horner
scheme.
Note that you can use the model-oriented predict
method for
polynomials for purpose of evaluation (without explicit coercion to
a function), see the example below.
Value
A function to evaluate the polynomial p
.
See Also
as.function
,
predict.polynomial
Examples
pr <- (poly.calc(-1:1) - 2 * polynomial(c(1, 2, 1)))^2
pr
prf <- as.function(pr)
prf
prf(-3:3)
predict(pr, -3:3)
Change Origin for a Polynomial
Description
Calculate the coefficients of a polynomial relative to a new origin
on the x axis.
Usage
change.origin(p, o)
Arguments
p |
an object of class "polynomial" .
|
o |
a numeric scalar representing the new origin on the original
scale.
|
Details
Let P(x)=∑ipixi
be a given polynomial and consider
writing P(x)=∑jqj(x−o)j
. This function calculates
the coefficients qj
and returns the result as a polynomial.
Value
A polynomial with coefficients relative to the re-located x axis.
Examples
pr <- poly.calc(1:5)
pr
change.origin(pr, 3)
Differentiate a Polynomial
Description
Calculates the derivative of a univariate polynomial.
Usage
deriv(expr, ...)
Arguments
expr |
an object of class "polynomial" .
|
... |
further arguments to be passed to or from methods.
|
Details
This is a method for the generic function deriv
.
Value
Derivative of the polynomial.
See Also
integral.polynomial
,
deriv
.
Examples
pr <- poly.calc(1:5)
pr
deriv(pr)
GCD and LCM for Polynomials
Description
Compute the greatest common divisor (GCD) and least common multiple
(LCM) of a collection of polynomials and polylists.
Usage
GCD(...)
GCD(...)
LCM(...)
LCM(...)
Arguments
Examples
pl <- polylist(poly.from.roots(-1),
poly.from.roots(c(-1, -1)),
poly.from.roots(1))
GCD(pl)
GCD(pl[-3])
LCM(pl)
LCM(pl, pl, pl[[2]])
Integrate a Polynomial
Description
Find the integral of a univariate polynomial.
Usage
integral(expr, limits = NULL, ...)
Arguments
expr |
an object of class "polynomial" .
|
limits |
numeric vector of length 2 giving the integration
limits.
|
... |
further arguments to be passed to or from methods.
|
Value
If limits
is not given, the integral of p
from 0 to
‘x’'. Otherwise, the integral with the given integration
limits.
See Also
deriv.polynomial
Examples
p <- poly.calc(1:5)
p
deriv(p)
integral(deriv(p)) - 120
Lines Method for Polynomials
Description
Add a polynomial to an existing plot usually as a line plot.
Usage
lines(x, len = 1000, xlim = NULL, ylim = NULL, ...)
Arguments
x |
an object of class "polynomial" .
|
len |
size of vector at which evaluations are to be made.
|
xlim , ylim
|
the range of x and y values with sensible defaults.
|
... |
additional arguments as for the lines
generic.
|
Details
This is a method for the generic function lines
.
Lines representing the given polynomial are added to an existing
plot. Values outside the current plot region are not shown.
See Also
lines
,
points
,
points.polynomial
,
plot
,
plot.polynomial
.
Examples
plot (poly.calc(-1:5))
lines (poly.calc( 2:4), lty = 2)
points(poly.calc(-2:6), pch = 4)
Math Group Methods for Polynomials
Description
Group method for functions in the Math group.
Usage
Math(x, ...)
Arguments
x |
an object of class "polynomial" .
|
... |
further arguments to be passed to or from methods, such
as digits .
|
Details
Most math group functions are disallowed with polynomial arguments.
The only exceptions are ceiling
, floor
,
round
, trunc
, and signif
which may be used to transform the coefficients accordingly.
Value
A polynomial with transformed coefficients.
See Also
Ops.polynomial
,
Summary.polynomial
.
Examples
op <- options(digits=18)
p <- poly.from.values(1:4, (2:5)^2)
p <- round(p)
options(op)
Monic Polynomials
Description
Convert a polynomial to monic form by dividing by the leading
coefficient.
Usage
monic(p)
Arguments
p |
A polynomial. A warning is issued if the polynomial is
identically zero.
|
Details
Similar in effect to p/as.numeric(p[length(p)])
but with some
safeguards against leading zero coefficients.
Value
A polynomial proportional to p
with leading coefficient 1.
See Also
change.origin
Arithmetic Ops Group Methods for Polynomials
Description
Allows arithmetic operators to be used for polynomial calculations,
such as addition, multiplication, division, etc.
Usage
Ops(e1, e2)
Arguments
e1 |
an object of class "polynomial" .
|
e2 |
an object of class "polynomial" .
|
Value
A polynomial got by performing the operation on the two arguments.
See Also
Math.polynomial
,
Summary.polynomial
.
Examples
p <- polynomial(c(1, 2, 1))
r <- poly.calc(-1 : 1)
(r - 2 * p)^2
Plot Method for Polynomials
Description
Plots polynomials, optionally allowing the “interesting” region to
be automatically determined.
Usage
plot(x, xlim = 0:1, ylim = range(Px), type = "l",
len = 1000, ..., xlab = "x", ylab = "P(x)")
Arguments
x |
an object of class "polynomial" .
|
xlim |
the range to be encompassed by the x axis.
|
ylim |
the range to be encompassed by the y axis.
|
type |
as for plot() .
|
len |
number of x points drawn.
|
... |
additional arguments as for plot .
|
xlab , ylab
|
graphical parameters.
|
Details
This is a method for the generic function plot
.
A plot of the polynomial is produced on the currently active device.
Unless otherwise specified, the domain is chosen to enclose the real
parts of all zeros, stationary points and zero itself.
See Also
plot
,
lines
,
points
,
lines.polynomial
,
points.polynomial
.
Examples
plot(p <- poly.calc(-1:5))
Points Method for Polynomials
Description
Add a polynomial to an existing plot usually as a point plot.
Usage
points(x, length = 100, ...)
Arguments
x |
an object of class "polynomial" .
|
length |
size of x vector at which evaluations are to be made.
|
... |
additional arguments as for the points generic.
|
Details
This is a method for the generic function points
.
Points representing the given polynomial are added to an existing
plot. Values outside the current plot region are not shown.
See Also
plot
,
lines
,
points
,
plot.polynomial
,
lines.polynomial
.
Examples
plot(poly.calc(-1:5))
lines(poly.calc(2:4), lty=2)
points(poly.calc(-2:6), pch=4)
Calculate Polynomials from Zeros or Values
Description
Calculate either the monic polynomial with specified zeros, or the
Lagrange interpolation polynomial through the (x,y) points.
Usage
poly.calc(x, y, tol=sqrt(.Machine$double.eps), lab=dimnames(y)[[2]])
Arguments
x |
numeric vector specifying either the zeros of the desired
polynomial if this is the only non-missing argument, or the x-values
for Lagrange interpolation.
|
y |
numeric vector or matrix specifying the y-values for the
Lagrange interpolation polynomial. If y is a matrix,
nrow(y) must equal length(x) , and each column of
y is used separately with x .
|
tol |
An absolute value tolerance, below which coefficients are
treated as zero.
|
lab |
If y is a matrix, lab is used as the names vector
for the list result.
|
Details
If y
is a matrix, the result is a list of polynomials using
each column separately.
If x
only is given, repeated zeros are allowed. If x
and y
are given, repeated values in the x
vector must
have identical y
values associated with them (up to
tol
), otherwise the first y-value only is used and a warning
is issued.
Value
Either a polynomial object, or a list of polynomials, as appropriate.
In the latter case the object is of class "polylist"
.
See Also
polynomial
Examples
poly.calc(rep(1,3))
poly.calc(0:4, (0:4)^2 + 1)
poly.calc(0:4, cbind(0:4, (0:4)^2 + 1), lab = letters[1:2])
Construct Orthogonal Polynomials
Description
Construct the orthogonal polynomials on a given vector, up to a
specified degree.
Usage
poly.orth(x, degree = length(unique(x)) - 1, norm = TRUE)
Arguments
x |
a numeric vector of abscissae. When evaluated at x
the polynomials will generate an orthonormal set.
|
degree |
maximum degree required. The default is one fewer than
the number of distinct values in x , which is maximum
possible.
|
norm |
a logical indicating whether the polynomials should be
normalized.
|
Value
A list of class "polylist"
of objects of class
"polynomial"
of degree 1, 2, ..., degree
.
Examples
x <- rep(1:4, 1:4)
x
polx <- poly.orth(x, 3)
polx
v <- sapply(polx, predict, x)
round(crossprod(v), 10)
Lists of Polynomials
Description
Create and manipulate lists of polynomials.
Usage
polylist(...)
as.polylist(x)
is.polylist(x)
Arguments
... |
R objects, polynomials or capable of coercion to polynomials.
|
x |
an R object.
|
Details
polylist
takes a list of arguments, tries to convert each into
a polynomial (see polynomial
), and sets the class of the
list to "polylist"
.
as.polylist
tries to coerce its arguments to a polylist, and
will do so for arguments which are polynomials or lists thereof.
is.polylist
tests whether its argument is a polylist.
This class has several useful methods, such as taking derivatives
(deriv
) and antiderivatives (integral
),
printing and plotting, subscripting, computing sums and products of
the elements, and methods for c
, rep
, and
unique
.
Examples
pl <- poly.orth(rep(1:4, 1:4), 3)
pl
plot(pl)
deriv(pl)
integral(pl)
sum(pl)
prod(pl)
unique(rep(pl, 3)[c(8, 12)])
Polynomials
Description
Construct, coerce to, test for, and print polynomial objects.
Usage
polynomial(coef = c(0, 1))
as.polynomial(p)
is.polynomial(p)
as.character(x, decreasing = FALSE, ...)
print(x, digits = getOption("digits"), decreasing = FALSE, ...)
Arguments
coef |
numeric vector, giving the polynomial coefficients in
increasing order.
|
p |
an arbitrary R object.
|
x |
a polynomial object.
|
decreasing |
a logical specifying the order of the terms;
in increasing (default) or decreasing powers.
|
digits |
the number of significant digits to use for printing.
|
... |
potentially further arguments passed to and from other methods.
|
Details
polynomial
constructs a polynomial from its coefficients,
i.e., p[1:k]
specifies the polynomial
p1+p2x+p3x2+⋯+pkxk−1.
Internally, polynomials are simply numeric coefficient vectors of
class "polynomial"
. Several useful methods are available for
this class, such as coercion to character (as.character()
) and
function (as.function.polynomial
), extraction of
the coefficients (coef()
), printing (using as.character
),
plotting (plot.polynomial
), and computing sums and
products of arbitrarily many polynomials.
as.polynomial
tries to coerce its arguments to a polynomial.
is.polynomial
tests whether its argument is a polynomial (in
the sense that it has class "polynomial"
.
Examples
polynomial(1:4)
p <- as.polynomial(c(1,0,3,0))
p
print(p, decreasing = TRUE)
stopifnot(coef(p) == c(1,0,3))
polynomial(c(2,rep(0,10),1))
Evaluate a Polynomial
Description
Evaluate a polynomial at a given numeric or polynomial argument.
Usage
predict(object, newdata, ...)
Arguments
object |
A polynomial object to be evaluated.
|
newdata |
Argument at which evaluation is requested. May be
numeric or itself a polynomial
|
... |
Not used by this method.
|
Details
This is a method for the generic function predict
.
The polynomial is evaluated according to the Horner scheme for speed
and numerical accuracy.
Value
Evaluated object of the same class as newdata
.
See Also
as.function.polynomial
Zeros of a Polynomial
Description
Find the zeros, if any, of a given polynomial.
Usage
solve(a, b, ...)
Arguments
a |
A polynomial object for which the zeros are required.
|
b |
a numeric value specifying an additional intercept. If
given, the zeros of a - b are found.
|
... |
Not used by this method.
|
Details
This is a method for the generic function solve
.
The zeros are found as the eigenvalues of the companion matrix,
sorted according to their real parts.
Value
A numeric vector, generally complex, of zeros.
See Also
polyroot
,
poly.calc
,
summary.polynomial
Examples
p <- polynomial(6:1)
p
pz <- solve(p)
pz
poly.calc(pz)
Summary of a Polynomial
Description
Summarize a polynomial by describing its “key” points.
Usage
summary(object, ...)
Arguments
object |
an object of class "polynomial" .
|
... |
Not used by this method.
|
Details
This is a method for the generic function summary
.
Value
A list of class "summary.polynomial"
(which has its own
print
method) containing information on zeros, stationary and
inflexion points.
Examples
p <- polynomial(6:1)
p
pz <- summary(p)
pz
poly.calc(pz)
Summary Group Methods for Polynomials
Description
Allows summary group generics to be used on polynomial arguments.
Usage
Summary(..., na.rm = FALSE)
Arguments
... |
R objects, the first supplied of class
"polynomial" .
|
na.rm |
logical: should missing values be removed?
|
Details
For the sum
and prod
functions, the sum and product of
the given polynomials, respectively. For the other members of the
Summary group, an error is returned.
Note that one could order polynomials by divisibility, and
define min
and max
as the corresponding lattice meet and
join, i.e., the greatest common divisor and the least common multiple,
respectively. This is currently not provided: instead, functions
GCD
and LCM
should be called directly.
See Also
Math.polynomial
,
Ops.polynomial