R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("polynom-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('polynom') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "Math.polynomial" > > ### * Math.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: Math.polynomial > ### Title: Math Group Methods for Polynomials > ### Aliases: Math.polynomial > ### Keywords: symbolmath > > ### ** Examples > > op <- options(digits=18) > p <- poly.from.values(1:4, (2:5)^2) > ## 1 + 2.00000000000001*x + x^2 > p <- round(p) > ## 1 + 2*x + x^2 > options(op) > > > > cleanEx(); ..nameEx <- "Ops.polynomial" > > ### * Ops.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: Ops.polynomial > ### Title: Arithmetic Ops Group Methods for Polynomials > ### Aliases: Ops.polynomial > ### Keywords: symbolmath > > ### ** Examples > > p <- polynomial(c(1, 2, 1)) > ## 1 + 2*x + x^2 > r <- poly.calc(-1 : 1) > ## -1*x + x^3 > (r - 2 * p)^2 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6 > ## 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6 > > > > cleanEx(); ..nameEx <- "as.function.polynomial" > > ### * as.function.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: as.function.polynomial > ### Title: Coerce a Polynomial to a Function > ### Aliases: as.function.polynomial > ### Keywords: symbolmath > > ### ** Examples > > pr <- (poly.calc(-1:1) - 2 * polynomial(c(1, 2, 1)))^2 > pr 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6 > ## 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6 > prf <- as.function(pr) > prf function (x) 4 + x * (20 + x * (33 + x * (16 + x * (-6 + x * (-4 + x * (1)))))) > ## function (x) > ## 4 + x * (20 + x * (33 + x * (16 + x * (-6 + x * (-4 + x * (1)))))) > ## > prf(-3:3) [1] 1024 64 0 4 64 144 64 > ## 1024 64 0 4 64 144 64 > predict(pr, -3:3) [1] 1024 64 0 4 64 144 64 > ## 1024 64 0 4 64 144 64 > > > > cleanEx(); ..nameEx <- "change.origin" > > ### * change.origin > > flush(stderr()); flush(stdout()) > > ### Name: change.origin > ### Title: Change Origin for a Polynomial > ### Aliases: change.origin > ### Keywords: symbolmath > > ### ** Examples > > pr <- poly.calc(1:5) > pr -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > ## -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > change.origin(pr, 3) 4*x - 5*x^3 + x^5 > ## 4*x - 5*x^3 + x^5 > > > > cleanEx(); ..nameEx <- "deriv.polynomial" > > ### * deriv.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: deriv.polynomial > ### Title: Differentiate a Polynomial > ### Aliases: deriv.polynomial > ### Keywords: symbolmath > > ### ** Examples > > pr <- poly.calc(1:5) > pr -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > ## -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > deriv(pr) 274 - 450*x + 255*x^2 - 60*x^3 + 5*x^4 > ## 274 - 450*x + 255*x^2 - 60*x^3 + 5*x^4 > > > > cleanEx(); ..nameEx <- "integral.polynomial" > > ### * integral.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: integral.polynomial > ### Title: Integrate a Polynomial > ### Aliases: integral integral.polynomial > ### Keywords: symbolmath > > ### ** Examples > > p <- poly.calc(1:5) > p -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > ## -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > deriv(p) 274 - 450*x + 255*x^2 - 60*x^3 + 5*x^4 > ## 274 - 450*x + 255*x^2 - 60*x^3 + 5*x^4 > integral(deriv(p)) - 120 -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > ## -120 + 274*x - 225*x^2 + 85*x^3 - 15*x^4 + x^5 > > > > cleanEx(); ..nameEx <- "lines.polynomial" > > ### * lines.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: lines.polynomial > ### Title: Lines Method for Polynomials > ### Aliases: lines.polynomial > ### Keywords: symbolmath > > ### ** Examples > > plot (poly.calc(-1:5)) > lines (poly.calc( 2:4), lty = 2) > points(poly.calc(-2:6), pch = 4) > > > > cleanEx(); ..nameEx <- "plot.polynomial" > > ### * plot.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: plot.polynomial > ### Title: Plot Method for Polynomials > ### Aliases: plot.polynomial > ### Keywords: symbolmath > > ### ** Examples > > plot(p <- poly.calc(-1:5)) > > > > cleanEx(); ..nameEx <- "points.polynomial" > > ### * points.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: points.polynomial > ### Title: Points Method for Polynomials > ### Aliases: points.polynomial > ### Keywords: symbolmath > > ### ** Examples > > plot(poly.calc(-1:5)) > lines(poly.calc(2:4), lty=2) > points(poly.calc(-2:6), pch=4) > > > > cleanEx(); ..nameEx <- "poly.calc" > > ### * poly.calc > > flush(stderr()); flush(stdout()) > > ### Name: poly.calc > ### Title: Calculate Polynomials from Zeros or Values > ### Aliases: poly.calc poly.from.zeros poly.from.roots poly.from.values > ### Keywords: symbolmath > > ### ** Examples > > poly.calc(rep(1,3)) -1 + 3*x - 3*x^2 + x^3 > ## -1 + 3*x - 3*x^2 + x^3 > poly.calc(0:4, (0:4)^2 + 1) 1 + x^2 > ## 1 + x^2 > poly.calc(0:4, cbind(0:4, (0:4)^2 + 1), lab = letters[1:2]) List of polynomials: $a x $b 1 + x^2 > ## List of polynomials: > ## $a: > ## x > ## > ## $b: > ## 1 + x^2 > > > > cleanEx(); ..nameEx <- "poly.orth" > > ### * poly.orth > > flush(stderr()); flush(stdout()) > > ### Name: poly.orth > ### Title: Construct Orthogonal Polynomials > ### Aliases: poly.orth > ### Keywords: symbolmath > > ### ** Examples > > x <- rep(1:4, 1:4) # x with repetitions for weighting > x [1] 1 2 2 3 3 3 4 4 4 4 > ## [1] 1 2 2 3 3 3 4 4 4 4 > polx <- poly.orth(x, 3) # calculate orthogonal polynomials > polx List of polynomials: [[1]] 0.3162278 [[2]] -0.9486833 + 0.3162278*x [[3]] 2.139203 - 1.863177*x + 0.3450328*x^2 [[4]] -5.831564 + 8.80369*x - 3.803194*x^2 + 0.4930066*x^3 > ## List of polynomials: > ## [[1]] > ## 0.3162278 > ## > ## [[2]] > ## -0.9486833 + 0.3162278*x > ## > ## [[3]] > ## 2.139203 - 1.863177*x + 0.3450328*x^2 > ## > ## [[4]] > ## -5.831564 + 8.80369*x - 3.803194*x^2 + 0.4930066*x^3 > v <- sapply(polx, predict, x) # orthonormal basis > round(crossprod(v), 10) # check orthonormality [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 1 0 0 [3,] 0 0 1 0 [4,] 0 0 0 1 > > > > cleanEx(); ..nameEx <- "polylist" > > ### * polylist > > flush(stderr()); flush(stdout()) > > ### Name: polylist > ### Title: Lists of Polynomials > ### Aliases: polylist deriv.polylist integral.polylist plot.polylist > ### print.polylist > ### Keywords: symbolmath > > ### ** Examples > > ## Calculate orthogonal polynomials > pl <- poly.orth(rep(1:4, 1:4), 3) > pl List of polynomials: [[1]] 0.3162278 [[2]] -0.9486833 + 0.3162278*x [[3]] 2.139203 - 1.863177*x + 0.3450328*x^2 [[4]] -5.831564 + 8.80369*x - 3.803194*x^2 + 0.4930066*x^3 > plot(pl) > deriv(pl) List of polynomials: [[1]] 0 [[2]] 0.3162278 [[3]] -1.863177 + 0.6900656*x [[4]] 8.80369 - 7.606388*x + 1.47902*x^2 > integral(pl) List of polynomials: [[1]] 0.3162278*x [[2]] -0.9486833*x + 0.1581139*x^2 [[3]] 2.139203*x - 0.9315885*x^2 + 0.1150109*x^3 [[4]] -5.831564*x + 4.401845*x^2 - 1.267731*x^3 + 0.1232517*x^4 > > > > cleanEx(); ..nameEx <- "polynomial" > > ### * polynomial > > flush(stderr()); flush(stdout()) > > ### Name: polynomial > ### Title: Polynomials > ### Aliases: polynomial as.polynomial is.polynomial as.character.polynomial > ### print.polynomial coef.polynomial > ### Keywords: symbolmath > > ### ** Examples > > polynomial(1:4) 1 + 2*x + 3*x^2 + 4*x^3 > p <- as.polynomial(c(1,0,3,0)) > p 1 + 3*x^2 > print(p, decreasing = TRUE) 3*x^2 + 1 > stopifnot(coef(p) == c(1,0,3)) > > polynomial(c(2,rep(0,10),1)) 2 + x^11 > > > > cleanEx(); ..nameEx <- "solve.polynomial" > > ### * solve.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: solve.polynomial > ### Title: Zeros of a Polynomial > ### Aliases: solve.polynomial > ### Keywords: symbolmath > > ### ** Examples > > p <- polynomial(6:1) > p 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > ## 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > pz <- solve(p) > pz [1] -1.4917980+0.000000i -0.8057865-1.222905i -0.8057865+1.222905i [4] 0.5516855-1.253349i 0.5516855+1.253349i > ## [1] -1.49180+0.0000i -0.80579-1.2229i -0.80579+1.2229i > ## [4] 0.55169-1.2533i 0.55169+1.2533i > ## To retrieve the original polynomial from the zeros: > poly.calc(pz) Warning: imaginary parts discarded in coercion Warning: out-of-range values treated as 0 in coercion to raw 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > ## Warning: imaginary parts discarded in coercion > ## 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > > > > cleanEx(); ..nameEx <- "summary.polynomial" > > ### * summary.polynomial > > flush(stderr()); flush(stdout()) > > ### Name: summary.polynomial > ### Title: Summary of a Polynomial > ### Aliases: summary.polynomial print.summary.polynomial > ### Keywords: symbolmath > > ### ** Examples > > p <- polynomial(6:1) > p 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > ## 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > pz <- summary(p) > pz Summary information for: 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 Zeros: [1] -1.4917980+0.000000i -0.8057865-1.222905i -0.8057865+1.222905i [4] 0.5516855-1.253349i 0.5516855+1.253349i Stationary points: [1] -0.85825757-0.5132192i -0.85825757+0.5132192i 0.05825757-0.9983016i [4] 0.05825757+0.9983016i Points of inflexion: [1] -0.7211424+0.00000i -0.2394288-0.70523i -0.2394288+0.70523i > ## [1] -1.49180+0.0000i -0.80579-1.2229i -0.80579+1.2229i > ## [4] 0.55169-1.2533i 0.55169+1.2533i > ## To retrieve the original polynomial from the zeros: > poly.calc(pz) Warning in xi * p : longer object length is not a multiple of shorter object length Warning in xi * p : longer object length is not a multiple of shorter object length Warning: imaginary parts discarded in coercion Warning: out-of-range values treated as 0 in coercion to raw 0.9233124 + 2.072266*x + 5.862637*x^2 + 4.444231*x^3 + 2.253262*x^4 - 0.5543656*x^5 + 1.027714*x^6 + x^7 > ## Warning: imaginary parts discarded in coercion > ## 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5 > > > > ### *