forked from mathnet/mathnet-numerics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RootFinding: more robust hybrid newton-raphson
- Loading branch information
Showing
6 changed files
with
166 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace MathNet.Numerics.RootFinding.Algorithms | ||
{ | ||
public static class ZeroCrossingBracketing | ||
{ | ||
public static IEnumerable<Tuple<double, double>> FindIntervalsWithin(Func<double, double> f, double lowerBound, double upperBound, int parts) | ||
{ | ||
// TODO: Consider binary-style search instead of linear scan | ||
|
||
var fmin = f(lowerBound); | ||
var fmax = f(upperBound); | ||
|
||
if (Math.Sign(fmin) != Math.Sign(fmax)) | ||
{ | ||
yield return new Tuple<double, double>(lowerBound, upperBound); | ||
yield break; | ||
} | ||
|
||
double subdiv = (upperBound - lowerBound)/parts; | ||
var smin = lowerBound; | ||
int sign = Math.Sign(fmin); | ||
|
||
for (int k = 0; k < parts; k++) | ||
{ | ||
var smax = smin + subdiv; | ||
var sfmax = f(smax); | ||
if (double.IsInfinity(sfmax)) | ||
{ | ||
// expand interval to include pole | ||
smin = smax; | ||
continue; | ||
} | ||
if (Math.Sign(sfmax) != sign) | ||
{ | ||
yield return new Tuple<double, double>(smin, smax); | ||
sign = Math.Sign(sfmax); | ||
} | ||
smin = smax; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters