'Converting polynomial

I would like to write a function that converts a polynomial given as a character into a named vector. The entries of the vector should correspond to the coefficients of the polynomial and be named as x^n, where n is the corresponding exponent.

The output should look like this:

> function("2x^5 + x + 3x^5 - 1 + 4x")

x^0 x^1 x^2 x^3 x^4 x^5
 -1   5   0   0   0   5

Cheers!



Solution 1:[1]

Since your polynomial may contain the same power twice (e.g. x^5 in your example), you need to do some operations to simplify it. The easiest way to do this is to evaluate it and its derivatives at zero. Here's some code to do that:

polycoefs <- function(poly) {
  expr <- parse(text = poly)
  result <- numeric()
  env <- list2env(list(x = 0), parent = baseenv())
  i <- 0
  while (!identical(expr, 0)) {
    coef <- eval(expr, envir = env)/factorial(i)
    names(coef) <- paste0("x^", i)
    result <- c(result, coef)
    expr <- D(expr, "x")
    i <- i + 1
  }
  result
}

polycoefs("2*x^5 + x + 3*x^5 - 1 + 4*x")
#> x^0 x^1 x^2 x^3 x^4 x^5 
#>  -1   5   0   0   0   5

Created on 2022-05-23 by the reprex package (v2.0.1)

Note that the polynomial needs to be a legal R expression, and it needs to be a polynomial, not some other kind of expression, or the function won't ever terminate. I'll leave it to you to add some edits and tests to make sure these conditions are met.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2554330