| 1 |
\name{polyn.eval} |
| 2 |
\alias{polyn.eval} |
| 3 |
\title{Evaluate Polynomials} |
| 4 |
\description{ |
| 5 |
Evaluate one or several univariate polynomials at several locations, |
| 6 |
i.e. compute \code{coef[1] + coef[2]*x + ... + coef[p+1]* x^p} |
| 7 |
(in the simplest case where \code{x} is scalar and \code{coef} a vector). |
| 8 |
} |
| 9 |
\usage{ |
| 10 |
polyn.eval(coef, x) |
| 11 |
} |
| 12 |
\arguments{ |
| 13 |
\item{coef}{\dQuote{numeric} vector or matrix. If a vector, \code{x} can be an |
| 14 |
array and the result matches \code{x}.\cr |
| 15 |
If \code{coef} is a matrix it specifies several polynomials of the |
| 16 |
same degree as rows, \code{x} must be a vector, \code{coef[,k]} is |
| 17 |
for \eqn{x^{k-1}}{x^(k-1)} and the result |
| 18 |
is a matrix of dimension \code{length(x) * nrow(coef)}. |
| 19 |
|
| 20 |
Note that \code{coef} can also be \code{\link{complex}} or bigrational |
| 21 |
(\code{as.bigq(.)} from \CRANpkg{gmp}, or arbitrary precision |
| 22 |
(\code{"mpfr"}) from \CRANpkg{Rmpfr}, or similar number-like objects |
| 23 |
for which basic arithmetic is defined. |
| 24 |
} |
| 25 |
\item{x}{\dQuote{numeric} vector or array. Either \code{x} or \code{coef} must |
| 26 |
be a vector.} |
| 27 |
|
| 28 |
} |
| 29 |
\details{ |
| 30 |
The stable \dQuote{Horner rule} is used for evaluation in any case. |
| 31 |
|
| 32 |
When \code{length(coef) == 1L}, \code{polyn.eval(coef, x)} now returns a |
| 33 |
vector of \code{length(x)} whereas previously, it just gave the number |
| 34 |
\code{coef} independent of \code{x}. |
| 35 |
} |
| 36 |
\value{ |
| 37 |
numeric vector or array, depending on input dimensionalities, see above. |
| 38 |
} |
| 39 |
\author{Martin Maechler, ages ago.} |
| 40 |
\seealso{For much more sophisticated handling of polynomials, use the |
| 41 |
\CRANpkg{polynom} package, see, e.g., \code{\link[polynom]{predict.polynomial}}. |
| 42 |
For multivariate polynomials (and also for nice interface to the |
| 43 |
\CRANpkg{orthopolynom} package), consider the \CRANpkg{mpoly} package. |
| 44 |
} |
| 45 |
\examples{ |
| 46 |
polyn.eval(c(1,-2,1), x = 0:3)# (x - 1)^2 |
| 47 |
polyn.eval(c(0, 24, -50, 35, -10, 1), x = matrix(0:5, 2,3))# 5 zeros! |
| 48 |
(cf <- rbind(diag(3), c(1,-2,1))) |
| 49 |
polyn.eval(cf, 0:5) |
| 50 |
|
| 51 |
x <- seq(-3,7, by=1/4) |
| 52 |
cf <- 4:1 |
| 53 |
(px <- polyn.eval(cf, x)) # is exact |
| 54 |
if((gmpT <-"package:gmp" \%in\% search()) || require("gmp")) withAutoprint({ |
| 55 |
pxq <- polyn.eval(coef = as.bigq(cf, 1), x=x) |
| 56 |
pxq |
| 57 |
stopifnot(pxq == px) |
| 58 |
if(!gmpT) detach("package:gmp") |
| 59 |
}) |
| 60 |
|
| 61 |
if((RmpfrT <-"package:Rmpfr" \%in\% search()) || require("Rmpfr")) withAutoprint({ |
| 62 |
pxM <- polyn.eval(coef = mpfr(cf, 80), x=x) # 80 bits accuracy |
| 63 |
pxM |
| 64 |
stopifnot(pxM == px) |
| 65 |
if(!RmpfrT) detach("package:Rmpfr") |
| 66 |
}) |
| 67 |
|
| 68 |
stopifnot(identical(polyn.eval(12, x), rep(12, length(x))), |
| 69 |
identical(polyn.eval(7, diag(3)), matrix(7, 3,3))) |
| 70 |
} |
| 71 |
\keyword{arith} |