polynom package - DP
Purpose
DP 1 Hr - polynom package
> library(polynom) |
List all the possible functions
> lsf.str("package:polynom") as.polylist : function (x) as.polynomial : function (p) change.origin : function (p, o) GCD : function (...) integral : function (expr, ...) is.polylist : function (x) is.polynomial : function (p) LCM : function (...) monic : function (p) poly.calc : function (x, y, tol = sqrt(.Machine$double.eps), lab = dimnames(y)[[2]]) poly.from.roots : function (...) poly.from.values : function (x, y, tol = sqrt(.Machine$double.eps), lab = dimnames(y)[[2]]) poly.from.zeros : function (...) poly.orth : function (x, degree = length(unique(x)) - 1, norm = TRUE) polylist : function (...) polynomial : function (coef = c(0, 1)) |
List all the objects
> ls("package:polynom") [1] "as.polylist" "as.polynomial" "change.origin" "GCD" [5] "integral" "is.polylist" "is.polynomial" "LCM" [9] "monic" "poly.calc" "poly.from.roots" "poly.from.values" [13] "poly.from.zeros" "poly.orth" "polylist" "polynomial" |
Polynomial
Construct, coerce to, test for, and print polynomial objects
> print(polynomial(coef = c(0, 1, 1))) x + x^2 > print(is.polynomial(p)) [1] TRUE > print(as.polynomial(c(1.5, 1, 2))) 1.5 + x + 2*x^2 > print(polynomial(c(2, rep(0, 10), 1))) 2 + x^11 |
Change Origin
> p <- (polynomial(coef = c(1, 2, 3))) > change.origin(p, 2) 17 + 14*x + 3*x^2 |
PolyCalc
> poly.calc(rep(1, 2)) 1 - 2*x + x^2 |
PolyOrth
> x <- rep(1:4, 1:4) > poly.orth(x, 3) List of polynomials: [[1]] 0.316227766016838 [[2]] -0.948683298050514 + 0.316227766016838*x [[3]] 2.1392032339613 - 1.86317701022436*x + 0.345032779671177*x^2 [[4]] -5.83156435762676 + 8.80369015342205*x - 3.80319414627833*x^2 + 0.493006648591635*x^3 |
Solve
> p <- polynomial(c(1, -2, 1)) > solve(p) [1] 0.999999990762754 1.000000009237246 > summary(p) Summary information for: 1 - 2*x + x^2 Zeros: [1] 0.999999990762754 1.000000009237246 Stationary points: [1] 1 Points of inflexion: numeric(0) |
Summary
> p <- polynomial(c(1, -2, 1)) > summary(p) Summary information for: 1 - 2*x + x^2 Zeros: [1] 0.999999990762754 1.000000009237246 Stationary points: [1] 1 Points of inflexion: numeric(0) |
Derivative
> p <- polynomial(c(1, -2, 1)) > deriv(p) -2 + 2*x > q <- as.function(deriv(p)) > q(4) [1] 6 |
Integral
> p <- polynomial(c(1, -2, 1)) > integral(p, c(-1, 1)) [1] 2.666666666666666 |
Polynomial Product
> p <- polynomial(c(1, 2, 1)) > r <- poly.calc(-1:1) > (r - 2 * p)^2 4 + 20*x + 33*x^2 + 16*x^3 - 6*x^4 - 4*x^5 + x^6 |
Plot Polynomial
> p <- polynomial(c(1, 2, 1)) > r <- poly.calc(-1:1) > q <- (r - 2 * p)^2 > plot(q) |
Plot Polynomial
> p <- polynomial(c(1, 2, 1)) > r <- poly.calc(-1:1) > q <- (r - 2 * p)^2 > plot(q) > points(q) |
Wilkinson Polynomial
> p <- polynomial(1) > for (i in 1:20) { + p <- p * polynomial(c(-i, 1)) + } |
Summary of Wilkinson Polynomial
> p <- polynomial(1) > for (i in 1:20) { + p <- p * polynomial(c(-i, 1)) + } > summary(p) Summary information for: 2432902008176640000 - 8752948036761600000*x + 13803759753640699904*x^2 - 1.2870931245151e+19*x^3 + 8037811822645052416*x^4 - 3599979517947607040*x^5 + 1206647803780372992*x^6 - 311333643161390720*x^7 + 63030812099294896*x^8 - 10142299865511450*x^9 + 1307535010540395*x^10 - 135585182899530*x^11 + 11310276995381*x^12 - 756111184500*x^13 + 40171771630*x^14 - 1672280820*x^15 + 53327946*x^16 - 1256850*x^17 + 20615*x^18 - 210*x^19 + x^20 Zeros: [1] 0.999999999999987 2.000000000005535 2.999999999606676 4.000000008574716 [5] 4.999999911873786 6.000000539646076 6.999997339135784 8.000015569938538 [9] 8.999900445816888 10.000515221434165 10.998054322771356 12.005420875422889 [13] 12.989040850306202 14.016826758649714 14.981135267518638 16.015413140609159 [17] 16.990899249881661 18.003527072668859 18.999164114293951 20.000089311845350 Stationary points: [1] 1.247666465029009 2.298691625660685 3.334687623347544 4.364313270139911 [5] 5.390407382915106 6.414303028386359 7.436791488429580 8.458244768604928 [9] 9.479505067670450 10.499524555020098 11.521423739485462 12.541100753096529 [13] 13.563090555616593 14.586779485887487 15.608057317428516 16.636904921746151 [17] 17.664724112457709 18.701469607486430 19.752314231591491 Points of inflexion: [1] 1.508386410379220 2.610038102786591 3.682544025758086 4.742567106749005 [5] 5.795668489744330 6.844510051283738 7.890555111707171 8.935170954112486 [9] 9.977936801124695 11.022506284777965 12.063799652301372 13.110369399826602 [13] 14.155216259781531 15.203996573864432 16.257912704788911 17.317170459491777 [17] 18.390049101074904 19.491602510447258 |
coefficients
> rev(coef(p)) [1] 1 -210 20615 [4] -1256850 53327946 -1672280820 [7] 40171771630 -756111184500 11310276995381 [10] -135585182899530 1307535010540395 -10142299865511450 [13] 63030812099294896 -311333643161390656 1206647803780373248 [16] -3599979517947607040 8037811822645052416 -12870931245150988288 [19] 13803759753640704000 -8752948036761600000 2432902008176640000 |