【OpenCV】多项式方程求解(PolySolver)

OpenCV 提供了一个求解多项式的接口,solvePoly

//Finds the real or complex roots of a polynomial equation.
double solvePoly(InputArray coeffs, OutputArray roots, int maxIters=300 )

各参数含义,

  • coeffs – array of polynomial coefficients.
  • roots – output (complex) array of roots. 是一个 n×2 n × 2 的矩阵,每一行代表一个root,第一列代表实部,第二列代表虚部
  • maxIters – maximum number of iterations the algorithm does.

对应的多项式方程一般形式为

coeffs[n]xn+coeffs[n1]xn1+...+coeffs[1]x+coeffs[0]=0 coeffs [ n ] x n + coeffs [ n − 1 ] x n − 1 + . . . + coeffs [ 1 ] x + coeffs [ 0 ] = 0

注意

  • OpenCV 版本中使用 的 Coeffs 是从常数项到最高阶的,与我们一般使用的顺序相反

栗子 1

对于方程 x22x+1=0 x 2 − 2 x + 1 = 0 求解

Mat coef = (Mat_<float>(3,1) << 1,-2,1);
Mat roots;
solvePoly(coef, roots);
cout << "Roots: channels = " << roots.channels() << " , values = " << roots << ".";

Output:

Root: channels = 2 , values = [1, 0; 1, 0].

栗子 2

对于方程 x2+2=0 x 2 + 2 = 0 求解

Mat coef = (Mat_<float>(3,1) << 2,0,1);
Mat roots;
solvePoly(coef, roots);
cout << "Roots: channels = " << roots.channels() << " , values = " << roots << ".";

Output:

Roots: channels = 2 , values = [0, -1.4142135; 0, 1.4142135].

栗子 3

对于方程 3x3+4x25x+6=0 − 3 x 3 + 4 x 2 − 5 x + 6 = 0 求解

Mat coef = (Mat_<float>(4,1) << 6,-5,4,-3);
Mat roots;
solvePoly(coef, roots);
cout << "Roots: channels = " << roots.channels() << " , values = " << roots << ".";

Output:

Roots: channels = 2 , values = [0.034002621, -1.2567663; 1.265328, 0; 0.034002621, 1.2567663].

你可能感兴趣的:(OpenCV)