[R] polynomial

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Oct 9 22:14:03 CEST 2002


Ben Bolker <ben at zoo.ufl.edu> writes:

>   Any better (more efficient, built-in) ideas for computing 
> 
>  coef[1]+coef[2]*x+coef[3]*x^2+ ...
> 
>  than
> 
> polynom <- function(coef,x) {
>   n <- length(coef)
>   
> sum(coef*apply(matrix(c(rep(x,n),seq(0,n-1)),ncol=2),1,function(z)z[1]^z[2]))
> }

Back when debugging polyroot() I used this one

polyeval<-function(coef,x){v<-0;for (i in rev(coef)) v<-v*x+i; v}

Can't vouch for the efficiency, but  it vectorizes nicely in x.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list