From ed0065266ec7f00d7c7ac5309fa5ce869b7d4c0b Mon Sep 17 00:00:00 2001 From: Viet Dinh Date: Mon, 2 Nov 2015 08:38:05 -0500 Subject: [PATCH] update fixing bug #5599 --- modules/core/src/mathfuncs.cpp | 13 +++------ samples/cpp/polynominal_equations.cpp | 38 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 samples/cpp/polynominal_equations.cpp diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index e97905bcfa..8465a3d545 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -2530,21 +2530,14 @@ double cv::solvePoly( InputArray _coeffs0, OutputArray _roots0, int maxIters ) double old_num_im = num.im; num.re = old_num_re*old_num_re + old_num_im*old_num_im; - Mat num_re_arr = (Mat_(1,1) << num.re); - cv::sqrt(num_re_arr, num_re_arr); - num.re = num_re_arr.at(0, 0); + num.re = std::sqrt(num.re); num.re += old_num_re; num.im = num.re - old_num_re; num.re /= 2; - num_re_arr.at(0, 0) = num.re; - cv::sqrt(num_re_arr, num_re_arr); - num.re = num_re_arr.at(0, 0); + num.re = std::sqrt(num.re); - Mat num_im_arr = (Mat_(1,1) << num.im); num.im /= 2; - num_im_arr.at(0, 0) = num.re; - cv::sqrt(num_im_arr, num_im_arr); - num.im = num_im_arr.at(0, 0); + num.im = std::sqrt(num.im); if( old_num_re < 0 ) num.im = -num.im; } } diff --git a/samples/cpp/polynominal_equations.cpp b/samples/cpp/polynominal_equations.cpp new file mode 100644 index 0000000000..0dd5dc706a --- /dev/null +++ b/samples/cpp/polynominal_equations.cpp @@ -0,0 +1,38 @@ + /* + * solves equations x^4 - x^2 = 0, and x^2 - 2x + 1 = 0 + */ + +#include "opencv2/core/core.hpp" +#include + + +int main(void) +{ + cv::Mat coefs = (cv::Mat_(1,5) << 0, 0, -1, 0, 1 ); + std::cout << "x^4 - x^2 = 0\n\n" << "Coefficients: " << coefs << "\n" << std::endl; + cv::Mat r; + + double prec; + prec = cv::solvePoly(coefs, r); + std::cout << "Preciseness = " << prec << std::endl; + std::cout << "roots after 1000 iterations:\n" << r << "\n" << std::endl; + + prec = cv::solvePoly(coefs, r, 9999); + std::cout << "Preciseness = " << prec << std::endl; + std::cout << "roots after 9999 iterations:\n" << r << "\n" << std::endl; + + std::cout << "\n---------------------------------------\n" << std::endl; + + coefs = (cv::Mat_(1,3) << 1, -2, 1 ); + std::cout << "x^2 - 2x + 1 = 0\n\n" << "Coefficients: " << coefs << "\n" << std::endl; + + prec = cv::solvePoly(coefs, r); + std::cout << "Preciseness = " << prec << std::endl; + std::cout << "roots after 1000 iterations:\n" << r << "\n" << std::endl; + + prec = cv::solvePoly(coefs, r, 9999); + std::cout << "Preciseness = " << prec << std::endl; + std::cout << "roots after 9999 iterations:\n" << r << "\n" << std::endl; + + return 0; +} \ No newline at end of file