-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Const polynomial roots #20539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Const polynomial roots #20539
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -147,16 +147,16 @@ IGenFunction * Polynomial::Clone() const { | |||||
| return f; | ||||||
| } | ||||||
|
|
||||||
| const std::vector<std::complex<double>> Polynomial::FindRoots() const | ||||||
| { | ||||||
|
|
||||||
| const std::vector< std::complex <double> > & Polynomial::FindRoots(){ | ||||||
|
|
||||||
|
|
||||||
| // check if order is correct | ||||||
| unsigned int n = fOrder; | ||||||
| while ( Parameters()[n] == 0 ) { | ||||||
| // check if order is correct | ||||||
| unsigned int n = fOrder; | ||||||
| while (Parameters()[n] == 0) { | ||||||
| n--; | ||||||
| } | ||||||
|
|
||||||
| std::vector<std::complex<double>> fRoots; | ||||||
| fRoots.clear(); | ||||||
| fRoots.reserve(n); | ||||||
meiyasan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
|
|
@@ -226,32 +226,34 @@ const std::vector< std::complex <double> > & Polynomial::FindRoots(){ | |||||
| } | ||||||
| else { | ||||||
| // for higher order polynomial use numerical fRoots | ||||||
| FindNumRoots(); | ||||||
| return FindNumRoots(); | ||||||
| } | ||||||
|
|
||||||
| return fRoots; | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| std::vector<double> Polynomial::FindRealRoots() const | ||||||
| { | ||||||
|
|
||||||
| std::vector< double > Polynomial::FindRealRoots(){ | ||||||
| FindRoots(); | ||||||
| std::vector<double> roots; | ||||||
| roots.reserve(fOrder); | ||||||
| for (unsigned int i = 0; i < fOrder; ++i) { | ||||||
| if (fRoots[i].imag() == 0) | ||||||
| roots.push_back( fRoots[i].real() ); | ||||||
| } | ||||||
| return roots; | ||||||
| std::vector<std::complex<double>> fRoots = FindRoots(); | ||||||
| std::vector<double> roots; | ||||||
| roots.reserve(fOrder); | ||||||
| for (unsigned int i = 0; i < fOrder; ++i) { | ||||||
| if (fRoots[i].imag() == 0) | ||||||
| roots.push_back(fRoots[i].real()); | ||||||
| } | ||||||
|
Comment on lines
+238
to
+244
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, consider the following perhaps a bit more idiomatic code auto roots = FindRoots();
std::vector<double> realRoots;
realRoots.reserve(roots.size());
for (const auto &r: roots)
if (r.imag() == 0)
realRoots.push_back(r.real());
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I look at this twice, I see the for loop is stopping after
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well.. I agree the historical implementation sounds risky; what do you think about comparing double and strict 0 integers, shouldn't we implement an epsilon limit ? here would be my preference for simplicity purpose. auto roots = FindRoots();
std::vector<double> realRoots;
for (const auto &r: roots)
if (std::abs(r.imag()) < std::numeric_limits<double>::epsilon())
realRoots.push_back(r.real()); |
||||||
| return roots; | ||||||
| } | ||||||
| const std::vector< std::complex <double> > & Polynomial::FindNumRoots(){ | ||||||
|
|
||||||
| const std::vector<std::complex<double>> Polynomial::FindNumRoots() const | ||||||
| { | ||||||
|
|
||||||
| // check if order is correct | ||||||
| unsigned int n = fOrder; | ||||||
| while ( Parameters()[n] == 0 ) { | ||||||
| // check if order is correct | ||||||
| unsigned int n = fOrder; | ||||||
| while (Parameters()[n] == 0) { | ||||||
| n--; | ||||||
| } | ||||||
|
|
||||||
| std::vector<std::complex<double>> fRoots; | ||||||
| fRoots.clear(); | ||||||
| fRoots.reserve(n); | ||||||
|
Comment on lines
257
to
258
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
These are not needed now |
||||||
|
|
||||||
|
|
@@ -271,6 +273,5 @@ const std::vector< std::complex <double> > & Polynomial::FindNumRoots(){ | |||||
| return fRoots; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| } // namespace Math | ||||||
| } // namespace ROOT | ||||||
Uh oh!
There was an error while loading. Please reload this page.