1

I have a 2D numpy array C which contains the coefficients of a 2d polynomial, such that the polynomial is given by the sum over all coefficients:

c[i,j]*x^i*y^j

How can I find the roots of this 2d polynomial? It seems that numpy.roots only works for 1d polynomials.

torpedo
  • 23
  • 3
  • I believe solving this is likely much harder than you imagine as the roots of a multivariate polynomial will not even form a discrete set (a polynomial of degree 3 does not have 3 roots, but infintely many)... – Nils Werner Feb 11 '20 at 13:31
  • Let's say I have two of these polynomials and know they only have two common roots at most. Anyway I can get those two common roots? – torpedo Feb 11 '20 at 14:20
  • You still have the same problem. Subtract the polynomial tensors from each other and you get another polynomial tensor you need to find a root for. Also two surfaces generally don't intersect at only two points. Perhaps there's something in the underlying problem which casues that which you can use. – Daniel F Feb 11 '20 at 14:29
  • Polynomial 1 describes the intersection between surface A and B, and polynomial 2 the intersection between surface A and C.. so both polynomials describe 3 surface and should only give me 2 points – torpedo Feb 11 '20 at 14:44

2 Answers2

2

This is a polynomial in two variables. In general there will be infinitely many roots (think about all the values of x and y that will yield xy=0), so an algorithm that gives you all the roots cannot exist.

Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
  • Let's say I have two of these polynomials and know they only have two common roots at most. Anyway I can get those two common roots? – torpedo Feb 11 '20 at 14:20
  • In that case, maybe you can start by reducing the problem to a polynomial in single variable, or simplify it in some other way? Trying to solve a general two-variable case by inserting additional assumptions sounds pretty complicated to me. – Jussi Nurminen Feb 12 '20 at 12:26
1

It is correct as Jussi Nurminen pointed out that a multivariate polynomial does not have a finite number of roots in the general case, however it is still possible to find functions that yield all the (infinitely many) roots of a multivariate polynomial.

One solution would be to use sympy:

import numpy as np
import sympy as sp


np.random.seed(1234)

deg = (2, 3)    # degrees of polynomial

x = sp.symbols(f'x:{len(deg)}')         # free variables

c = np.random.uniform(-1, 1, deg)    # coefficients of polynomial

eq = (np.power(np.array(x), np.indices(deg).transpose((*range(1, len(deg) + 1), 0))).prod(-1) * c).sum()

sol = sp.solve(eq, x, dict=True)

for i, s in enumerate(sol):
    print(f'solution {i}:')
    for k, v in s.items():
        print(f'    {k} = {sp.simplify(v).evalf(3)}')

# solution 0:
#     x0 = (1.25e+14*x1**2 - 2.44e+14*x1 + 6.17e+14)/(-4.55e+14*x1**2 + 5.6e+14*x1 + 5.71e+14)
Vinzent
  • 1,070
  • 1
  • 9
  • 14