-3

I want to find out coefficients of the n degree polynomial with roots 0,1,2...n-1. Can anybody suggest a good algorithm? I tried using FFT but didn't work fast enough

  • 2
    There are an infinite number of polynomials with those roots. Are you asking for the coefficients of `(x-1)(x-2)(x-3)...(x-n)`? – btilly Dec 10 '19 at 18:14
  • Also, what does "evaluate all the coefficients" mean? Do you mean to *generate* the coefficients from the roots? If so, then @btilly has the right question phrasing. – RBarryYoung Dec 10 '19 at 18:39
  • Or do you mean: "*Evaluate the value of the implied polynomial for specific values of (x), givien only the roots of the polynomial*". If this, then just evaluate the formulae that @btilly has given. But these are two different problems. – RBarryYoung Dec 10 '19 at 18:43

2 Answers2

1

The simple solution that I would use is to write a function like this:

def poly_with_root_sequence (start, end, gap):
    if end < start + gap:
        return Polynomial([1, -start])
    else:
        p1 = poly_with_root_sequence(start, end, gap*2)
        p2 = poly_with_root_sequence(start+gap, end, gap*2)
        return p1 * p2

answer = poly_with_root_sequence(1, n, 1)

With a naive algorithm this will take O(n^2) arithmetic operations. However some of the operations will involve very large numbers. (Note that n! has more than n digits for large n.) But we have arranged that very few of the operations will involve very large numbers.

There is still no chance of producing answers as quickly as you want unless you are using a polynomial implementation with a very fast multiplication algorithm.

https://gist.github.com/ksenobojca/dc492206f8a8c7e9c75b155b5bd7a099 advertises itself as an implementation of the FFT algorithm for multiplying polynomials in Python. I can't verify that. But it gives you a shot at going fairly fast.

btilly
  • 43,296
  • 3
  • 59
  • 88
0

As replied on Evaluating Polynomial coefficients, you can do this as simple way:

def poly(lst, x): 
  n, tmp = 0, 0
  for a in lst:
    tmp = tmp + (a * (x**n))
    n += 1

  return tmp

print poly([1,2,3], 2)
alvaropaco
  • 1,573
  • 18
  • 29
  • I don't have the coefficients, I have the roots – Dragonaire Dec 10 '19 at 18:07
  • Also to get all the coefficients of a n polynomial expression you can check this reference: https://math.stackexchange.com/questions/3099594/finding-coefficients-of-a-polynomial-with-minimal-queries – alvaropaco Dec 10 '19 at 18:14
  • There is a world of difference between a theoretically correct solution and an acceptably fast solution. The question wasn't how to do the calculation, it is how to do it quickly. – btilly Dec 10 '19 at 18:41