-
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
Conversation
|
sorry @hageboeck, I failed to put you as reviewer and cannot adjust. |
hageboeck
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, thanks for the PR!
Please see a few comments inline. Could you also update the commit message along the lines of replacing "property" by "member" or similar?
More or less, one could use:
Allow for using several methods of Polynomial.h in const context.
- Remove the fRoots member. It doesn't serve any purpose, because it is cleared before every root computation.
- Return polynomial roots by value
- Mark affected methods const
math/mathmore/src/Polynomial.cxx
Outdated
| n--; | ||
| } | ||
|
|
||
| std::vector< std::complex < double > > fRoots; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the intention, but maybe we should not use the f for "field" in this context, since it's not any more a field of the class.
|
@meiyasan one more thing: |
- Remove unused `fRoots` data member (it was cleared before each root computation and provided no persistent state). - Return polynomial roots by value instead of via internal storage. - Mark the affected query/utility methods as `const` to make the class usable in const contexts. - Apply clang-format to the modified sections.
475b4c2 to
ea56fb6
Compare
edb49cb to
5dfb494
Compare
|
thanks! I just ran the formatter against the previous commit; removed the extra const you recommended & also renamed fRoots into |
Hello, sounds good! There must have been a small error in git, though:
|
vepadulano
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! A few comments with suggestions for minor improvements.
| fRoots.clear(); | ||
| fRoots.reserve(n); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| fRoots.clear(); | |
| fRoots.reserve(n); |
These are not needed now
| 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()); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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());There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 fOrder iterations. Although it's not clear from the code whether the vector returned by FindRoots will have a size greater (or even smaller which would be UB!) than fOrder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 ?
additionally, considering we are removing std::vector::reverse() in other cases, we could also drop realRoots.reserve(roots.size()); here.
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());
This Pull request:
Hi @hageboeck ; This is a followup on my previous PR #20524 aiming to improve polynomial implementation. I just implemented your suggestions and deprecating internal
fRootproperty from polynomial header and defined it in each of theFind*Rootmethods .Changes or fixes:
Removed
fProp, added const property to publicFind*Rootmethods.Feel free to as necessary.
Checklist:
Tests:
I ran the ROOT battery of tests with couple of issues.
Regarding the Polynomial class modifications, my use case output remains unchanged. (I use polynomials to compute digital filtering, biquad, etc..) I also paid attention in particular to those related to polynomials:
gtest-hist-hist-testTProfile2Poly, etc..