Solving polynomial equations
In this recipe, you will learn how to solve polynomial equations using OpenCV. Such problems can arise in such areas as machine learning, computational algebra, and signal processing.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.3 (or greater) Python API package.
How to do it...
You need to complete the following steps:
- Import the modules:
import cv2 import numpy as np
- Generate a fourth degree polynomial equation:
N = 4 coeffs = np.random.randn(N+1,1)
- Find all the roots in the complex domain:
retval, roots = cv2.solvePoly(coeffs)
- Check the roots:
for i in range(N): print('Root', roots[i],'residual:', np.abs(np.polyval(coeffs[::-1], roots[i][0][0]+1j*roots[i][0][1])))
How it works...
Polynomial equations of degree n always have n roots in the complex domain (some of them can be repeated, however). All of the solutions can be found using the cv2.solvePoly
function. It takes equation coefficients and returns all of the roots...