torpedo
torpedo

Reputation: 23

Python: Find roots of 2d polynomial

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.

Upvotes: 1

Views: 1805

Answers (2)

user9413641
user9413641

Reputation:

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)

Upvotes: 1

Jussi Nurminen
Jussi Nurminen

Reputation: 2408

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.

Upvotes: 2

Related Questions