1

Hi and thank you for your time,

I realize my question is in fact very similar to the question in this thread: How to get coefficients of polynomial expression

However, I would like to elaborate on this question.

I have written a program which when given a vector filled with the coefficients of a polynomial in ascending degree with respect to the monomials within that polynomial will output a vector filled with the coefficients of the primitive of this polynomial in a similar manner. For example, when I want to know the primitive of the expression y = 54s^2 - 36s + 3, I would input the vector (3, -36, 54) into my program and it would return the vector (0, 3, -18, 18) since the primitive of this y is 18s^3 - 18s^2 + 3s.

The code for this program is as follows:

# Define function degree
degree <- function() {

  # First asks for degree
  m1 <- readline("Please input the degree of your polynomial.")

  # Checks if degree is integer and non-negative
  m2 <- as.integer(m1)
  if ((m2 == m1)&(m1>0)) {
    print(paste("The degree of your polynomial is", m1))
  }
  if(m2 != m1) {
    print("The degree of a polynomial must be an integer.")
    degree()
  }
  if(m1 < 0) {
    print("The degree of a polynomial must be non-negative.")
    degree()
  }

  # Last output of degree is defining numeric m
  m <- as.numeric(m1)
}

# Define function polynomial
polynomial <- function() {

  # First calls on the degree function
  m <- degree()

  # Then asks for coefficients
  a <- readline("Please input the coefficients of your polynomial in order of their 
ascending degree separated by a space, such as: a[0] a[1] a[2] ... a[m]")

  # Creates vector a filled with these coefficients
  a <- strsplit(a, " ")
  a <- a[[1]]
  a <- as.numeric(a)

  # Checks if any non-numeric values were entered for the coefficients
  if (is.na(sum(a)) == TRUE) {
    print("The program does not allow for non-numeric coefficients.")
    polynomial()
  }

  # Checks if length of vector is equal to degree + 1
  if ((length(a) == m+1)) {
      print("Your polynomial can be represented by the vector:")
      print(a)
    }
  if ((length(a) != m+1)) {
    print("Please input a coefficient for the degree of each separate monomial within 
your polynomial.")
    polynomial()
  }

  # Last output of polynomial is defining numeric a
  a <- as.numeric(a)
}

# Define function primitive
primitive <- function() {

  # Call on function polynomial
  a <- polynomial()

  # Calculate the primitive
  p <- c()
  p[1] <- 0
  for (i in 1:length(a)){
    p[i + 1] <- a[i]/i 
  }
  print(paste("The degree of your primitive is", (length(p) - 1)))
  print("The primitive of your polynomial can be represented by the vector")
  print(p)
}

Now this is probably not flawless in itself, but it works fine. The issue I have now however is that I want to be able to calculate Picard iterations of differential equations in the form of a polynomial. Now, I'm quite confident that I can write this code myself, so I will only ask what is relevant for me to continue my work.

I essentially want to be able to be able to simplify any expression to a polynomial form (if the expression allows for this, but we assume it does). For example, if my expression is 6(3s - 1)^2, then I want R to simplify this to 54s^2 - 36s + 6 and put it into the vector form (6, -36, 54), so I can run it with my written program primitive. I have tried using the package rSympy to get the following:

sympy("expand(6*(3*s - 1)**2)")

Which gives me the output

[1] "6 - 36*s + 54*s**2"

However I have no idea how to obtain the (numerical) vector (6, -36, 54) from this output.

In the thread which I linked I saw that they used the function 'gregexpr'. I however have no idea what this function does and how it works, I couldn't for the life of me figure out what exactly I'd have to input into this function in order to obtain the vector I need. I dislike writing code which I do not understand. Please help and explain!

Anon
  • 25
  • 1
  • 6
  • What does `dput(sympy("expand(6*(3*s - 1)**2)"))` return? – MrFlick Dec 18 '18 at 17:42
  • It removes the [1] from the output, which I suppose means that the output is no longer a vector? What are the implications of this? I am relatively new to R and programming in general. – Anon Dec 18 '18 at 17:58
  • I thought maybe it was returning something other than a string that was just being printed to the screen as a string. I don't want to bother installing Java so I wasn't able to run the code myself. I'm just surprised that `sympy` doesn't return a more structured object. If i recall correctly, libraries like `ryacas` returned more useful structures. – MrFlick Dec 18 '18 at 18:15

0 Answers0