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.对应的多项式方程一般形式为
注意
对于方程 x2−2x+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].
对于方程 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].
对于方程 −3x3+4x2−5x+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].