Polynom F
Polynom F
R topics documented:
as.character.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
as.function.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
c.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
change_origin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
coef.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
deriv.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
2 as.character.polynom
GroupGenerics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
LCM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
neville . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Ops.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
plot.polylist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
poly_calc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
poly_orth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
poly_orth_general . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
predict.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
print.polylist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
print.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
rep.polylist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
solve.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
summary.polynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
tangent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
unique.polylist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
zap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
[.polylist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Index 24
Description
Produce a text representation of a polynomial object
Usage
## S3 method for class 'polynom'
as.character(x, variable = "x", decreasing = FALSE, ...)
Arguments
x The polynomial object in question
variable Character string: what variable name should be used?
decreasing Logical: in decreasing powers or increasing powers?
... Additional arguments (ignored as yet)
Value
A character string representation of the polynomial
as.function.polynom 3
Examples
p <- poly_from_zeros(-2:3)
as.character(p, "z", FALSE)
as.character(p, "z", TRUE)
parse(text = as.character(p, "z", TRUE))[[1]]
Description
PolynomF objects ARE functions, but this coercion method creates from a polynomial object a pure
function with the coefficients fully exposed in the code and which evaluates the polynomial more
efficiently.
Usage
Arguments
x A polynomial object
variable Character string: what variable name should be used?
... Additional arguments
Value
Examples
p <- poly_from_zeros(-2:3)
p
as.function(p)
4 change_origin
Description
Concatenation of polynomial objects into lists
Usage
## S3 method for class 'polynom'
c(..., recursive = FALSE)
Arguments
... Polynomial or polylist objects
recursive Logical, should the concatenation flatten all component lists?
Value
A polylist object with all argumets included
Description
Given a polynomial P(x) and a new origin o, find the polynomial Q(x) = P(x + o). I.e. Q(0) = P(o)
Usage
change_origin(p, o, ...)
## Default S3 method:
change_origin(p, o, ...)
Arguments
Value
Description
Usage
Arguments
Value
Examples
p <- polynomial(1:3)*polynomial(5:1)
coef(p)
6 deriv.polynom
Description
Usage
integral(expr, ...)
## Default S3 method:
integral(expr, ...)
Arguments
Value
Examples
p <- poly_from_roots(-2:3)
p
deriv(p)
integral(p)
GCD 7
Description
Find a monic polynomial of maximal degree that divides each of a set of polynomials exactly
Usage
GCD(...)
greatest_common_divisor(...)
Arguments
... A list of polynomials or polylist objects
Value
A polynomial giving the greatest common divisor, as defined above
Examples
p <- poly_calc(0:5)
r <- poly_calc(1:6)
greatest_common_divisor(p, r)
solve(greatest_common_divisor(p, r))
lowest_common_multiple(p, r)
solve(lowest_common_multiple(p, r))
Description
These provide methods for the generic function Summary and Math for polynomial and polylist
objects. For Summary only sum and prod members are implemented
8 LCM
Usage
## S3 method for class 'polynom'
Summary(..., na.rm = FALSE)
Arguments
... Additional arguments
na.rm Logical: should missing values be removed?
x a "polynom" or "polylist" objects.
Value
The result of the group generic operation
Examples
lis <- as_polylist(lapply(-2:3, function(x) polynomial() - x))
prod(lis)
sum(lis)
solve(prod(lis))
solve(sum(lis))
Description
For a list of polynomials, find the lowest degree monic polynomial into which each divides exactly
Usage
LCM(...)
lowest_common_multiple(...)
Arguments
Value
Examples
p <- poly_calc(0:5)
r <- poly_calc(1:6)
greatest_common_divisor(p, r)
solve(greatest_common_divisor(p, r))
lowest_common_multiple(p, r)
solve(lowest_common_multiple(p, r))
Description
Compute the Lagrange Interpolation Polynomial from a given set of x- and y-values, or, alterntively,
compute the interpolated values at a set of given x-values. Two algorithms are provided, namely
Neville’s algorithm, or a more direct version based on the usual Lagrange formula. The latter is
generally faster but the former can be more accurate numerically.
Usage
neville(x, y, x0 = polynomial())
lagrange(x, y, x0 = polynomial())
Arguments
Value
Examples
set.seed(123)
x <- 1:5
y <- rnorm(x)
xout <- 0.5 + 1:4
p1 <- neville(x, y)
plot(p1, xlim = range(x), ylim = extendrange(y, f = 1), panel.first = grid())
points(x, y, col = 4)
points(xout, lagrange(x, y, xout), col = 2)
Description
Usage
Arguments
Value
Examples
x <- polynomial()
(p <- (x-1)^5 - 1)
(p1 <- (p + 1)/(x - 1)^2 - 1)
for(i in 0:10) cat(coef((x+1)^i), "\n")
plot.polylist 11
Description
Plot methods for polynom or polylist objects
Usage
## S3 method for class 'polylist'
plot(
x,
xlim = 0:1,
ylim = range(Px),
type = "l",
xlab = "x",
ylab = "P(x)",
...,
col = seq_along(x),
lty = if (length(col) == 1) seq_along(x) else "solid",
len = 1000,
legend = FALSE
)
len = 1000,
limits = pu[1:2],
col = seq_along(x),
lty = if (length(col) == 1) seq_along(x) else "solid"
)
Arguments
Value
Examples
p <- poly_from_zeros((-3):4)
plot(p)
lines(deriv(p), col = "red")
Description
Usage
polynom(a = c(0, 1), ..., eps = 0)
as_polynom(a)
is_polynom(a)
polylist(...)
is_polylist(x)
as_polylist(x)
Arguments
a A polynom object, or a numeric vector of coefficients (in "power series" order)
or a vector object which can be coerced to one.
... Additional arguments, currently ignored.
eps A small non-negative tolerance to check for zero components.
x An object of class "polylist", at least potentially.
Value
A polynomial object.
Examples
(s <- polynomial())
(p <- polynomial(c(1, 5, 4, 1)/11))
oldPar <- par(mar = c(5,5,2,2)+0.1)
plot(p, xlim = 0:1, ylim = 0:1, type = "n", bty="n",
xlab = "s", ylab = expression({P^(n)}(s)))
lines(s, limits = 0:1)
P <- p
for(j in 1:7) {
lines(P, col = j+1, limits = 0:1)
P <- p(P)
}
lines(P, limits = 0:1, col = 9)
(r <- Re(solve((p-s)/(1-s))))
arrows(r, p(r), r, par("usr")[3], lwd = 0.5,
length = 0.125, angle = 15)
text(r, 0.025, paste("r =", format(r, digits = 3)))
leg <- sapply(0:8, function(x) bquote({P^(.(x))}(s)))
legend("topleft", legend = as.expression(leg),
lty = "solid", col = 1:9, bty = "n", ncol=3)
par(oldPar)
rm(leg, oldPar, p, P, r, s, j)
14 poly_calc
Description
Calculate the Lagrange interpolation polynomial, or list of polynomials, given a set of (x, y) points
to fit
Usage
poly_from_zeros(...)
poly_from_roots(...)
poly_from_roots(...)
Arguments
Value
Examples
(p <- poly_calc(0:5)) ## same as poly_from_zeros(0:5)
(p <- poly_calc(0:5, exp(0:5)))
plot(p)
curve(exp, add = TRUE, col = "red")
poly_orth 15
Description
Generate a list of polynomials up to a specified degree, orthogonal with respect to the natural inner
product on a discrete, finite set of x-values with equal weights.
Usage
Arguments
x A numeric vector
degree The desired maximum degree
norm Logical: should polynomials be normalised to length one?
Value
Examples
x <- c(0:3, 5)
P <- poly_orth(x)
plot(P, lty = "solid")
Pf <- as.function(P)
zap(crossprod(Pf(x)))
Description
Generate sets of polynomials orthogonal with respect to a general inner product. The inner product
is specified by an R function of (at least) two polynomial arguments.
16 poly_orth_general
Usage
poly_orth_general(inner_product, degree, norm = FALSE, ...)
Hermite(p, q = p)
Legendre(p, q = p)
ChebyshevT(p, q = p)
ChebyshevU(p, q = p)
Arguments
inner_product An R function of two "polynom" arguments with the second polynomial having
a default value equal to the first. Additional arguments may be specified. See
examples
degree A non-negative integer specifying the maximum degree
norm Logical: should the polynomials be normalized?
... additional arguments passed on to the inner product function
p, q Polynomials
alpha, beta Family parameters for the Jacobi polynomials
x numeric vector defining discrete orthogonal polynomials
w a weight function for discrete orthogonal polynomials
Details
Discrete orthogonal polynomials, equally or unequally weighted, are included as special cases. See
the Discrete inner product function.
Computations are done using the recurrence relation with computed coefficients. If the algebraic
expressions for these recurrence relation coefficients are known the computation can be made much
more efficient.
Value
A "polylist" object containing the orthogonal set
Examples
(P0 <- poly_orth(0:5, norm = FALSE))
(P1 <- poly_orth_general(Discrete, degree = 5, x = 0:5, norm = FALSE))
sapply(P0-P1, function(x) max(abs(coef(x)))) ## visual check for equality
(P0 <- poly_orth_general(Legendre, 5))
### should be same as P0, up to roundoff
predict.polynom 17
Description
Evaluate a polynomial, or polylist object components.
Usage
## S3 method for class 'polynom'
predict(object, newdata, ...)
Arguments
object A polynomial or polylist object
newdata A target object at which to evaluate.
... Not used
Value
If newdata is a numeric vector, a numeric vector of results. If newdata is a polynomial, then the
composition is returned as a polynomial, or polylist object.
Description
Print method for polynomial objects
Usage
## S3 method for class 'polylist'
print(x, ...)
Arguments
x A polynomial object or list thereof
... Additional arguments passed on to methods
18 rep.polylist
Value
The original object, invisibly.
Description
Standard method for printing polynomial objects
Usage
## S3 method for class 'polynom'
print(x, variable = "x", digits = getOption("digits"), decreasing = FALSE, ...)
Arguments
x A polynomial object
variable Character string: what variable name should be given?
digits Integer: how many decimal degits to use?
decreasing Logical: in descending powers, or ascending?
... Additional arguments
Value
The original object x, invisibly
Description
Repeat components of a polylist object
Usage
## S3 method for class 'polylist'
rep(x, times, ...)
Arguments
Value
Description
Solve polynomial equations, a(x) = b(x), or alternatively find the zeros of the polynomial a(x) - b(x)
Usage
Arguments
Value
Examples
p <- poly_calc(0:5)
solve(p)
solve(p, 1)
20 tangent
Description
Provide a succinct summary of the critical points of a polynomial, or list thereof
Usage
## S3 method for class 'polynom'
summary(object, ...)
Arguments
object, x A polynomial or polylist object
... Currently unused
Value
A list giving the zeros, stationary points and points of inflexion of the polynomial(s)
Examples
p <- poly_calc(0:5)
summary(p)
Description
Find the tangent line to a polynomial at one or more x-points
Usage
tangent(p, x0)
Arguments
p A polynomial object
x0 A numeric vector of values at which the tangent line(s) are required
unique.polylist 21
Value
A linear polynomial giving the tangent line, or a list of such polynomials
Examples
p <- poly_from_zeros(c(0, 0:5, 4))
plot(p, xlab = expression(italic(x)), ylab = expression(italic(P(x))),
main = parse(text = paste("italic(P(x) ==",
as.character(p, decreasing = TRUE),")")))
x0 <- solve(deriv(p)) ## stationary points
lines(tangent(p, x0), col = "dark green", lty = "solid",
limits = cbind(x0-1/4, x0+1/4))
points(x0, p(x0), col = "dark green")
Description
Remove duplicated polynomials in a polylist object
Usage
## S3 method for class 'polylist'
unique(x, incomparables = FALSE, ...)
Arguments
x A polylist object
incomparables Logical: as for the base function unique
... As for the base function unique
Value
A polylist object with no duplicated components
22 zap
Description
A convenience function for setting polynomial coefficients likely to be entirely round-off error to
zero. The decision is relegated to the function base::zapsmall, to which this is a front-end.
Usage
## Default S3 method:
zap(x, digits = getOption("digits"))
Arguments
Value
Examples
Description
Extract components of a list of polynomials
Usage
## S3 method for class 'polylist'
x[i]
Arguments
x A polylist object
i An index vector of any crongruent form
Value
A polylist object of the components
Index
as.character.polynom, 2 neville, 9
as.function.polylist
(as.function.polynom), 3 Ops.polylist (Ops.polynom), 10
as.function.polynom, 3 Ops.polynom, 10
as_polylist (polynom), 12 plot.polylist, 11
as_polynom (polynom), 12 plot.polynom (plot.polylist), 11
points.polylist (plot.polylist), 11
c.polylist (c.polynom), 4
points.polynom (plot.polylist), 11
c.polynom, 4
poly_calc, 14
change_origin, 4
poly_from_roots (poly_calc), 14
ChebyshevT (poly_orth_general), 15
poly_from_values (poly_calc), 14
ChebyshevU (poly_orth_general), 15
poly_from_zeros (poly_calc), 14
coef.polylist (coef.polynom), 5
poly_orth, 15
coef.polynom, 5
poly_orth_general, 15
polylist (polynom), 12
deriv.polylist (deriv.polynom), 6
polynom, 12
deriv.polynom, 6
polynomial (polynom), 12
Discrete (poly_orth_general), 15
predict.polylist (predict.polynom), 17
GCD, 7 predict.polynom, 17
greatest_common_divisor (GCD), 7 print.polylist, 17
GroupGenerics, 7 print.polynom, 18
print.summary.polynom
Hermite (poly_orth_general), 15 (summary.polynom), 20
24