Skip to content
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

Fixing newton again #66

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scion/math/SeriesBase/src/roots.hpp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test if the f(X) at that value is already zero so you avoid to loop through newton method.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::vector< X > roots( const Y& a = Y( 0. ) ) const {

if ( isCloseToZero( value.imag() ) ) {

roots.emplace_back( derivative( value.real() ) != X( 0. )
roots.emplace_back( !isCloseToZero( derivative( value.real() ) )
? newton( value.real(), functor, derivative )
: value.real() );
}
Expand Down
6 changes: 5 additions & 1 deletion src/scion/math/newton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ X newton( X estimate, Functor&& functor, Derivative&& derivative,
int iterations = 5 ) noexcept {

X root = estimate - functor( estimate ) / derivative( estimate );
while ( ( ! isClose( root, estimate ) ) || ( iterations != 0 ) ) {
while ( ! isClose( root, estimate ) ) {

std::swap( estimate, root );
root = estimate - functor( estimate ) / derivative( estimate );
--iterations;
if ( iterations == 0 ) {

break;
}
}
return root;
}
Expand Down