Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Working BS3 implementation - Start of event support - Additional DP5 random testing - Fixes major interpolant caching/re-use bug - Adds Bisection method (used by eventing) - Eventing may or may not work at all right now Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
- Loading branch information
1 parent
d277897
commit 51e44f9
Showing
21 changed files
with
1,001 additions
and
500 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,83 @@ | ||
/* | ||
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors | ||
* SPDX-License-Identifier: MIT-0 OR LGPL-2.1+ OR CC0-1.0 | ||
*/ | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using MechJebLib.Utils; | ||
|
||
// ReSharper disable CompareOfFloatsByEqualityOperator | ||
namespace MechJebLib.Core | ||
{ | ||
using RootFunc = Func<double, object?, double>; | ||
|
||
public class Bisection | ||
{ | ||
/// <summary> | ||
/// Bisection search. | ||
/// </summary> | ||
/// <param name="f">1 dimensional function</param> | ||
/// <param name="a">minimum bounds</param> | ||
/// <param name="b">maximum bounds</param> | ||
/// <param name="o">state object to pass to function</param> | ||
/// <param name="rtol">tolerance</param> | ||
/// <param name="preferLeft"></param> | ||
/// <returns>value for which the function evaluates to zero</returns> | ||
/// <exception cref="ArgumentException">guess does not brack the root</exception> | ||
public static (double c, double fc) Solve(RootFunc f, double a, double b, object? o, double tolX = 0, | ||
bool preferLeft = true) | ||
{ | ||
Check.Finite(a); | ||
Check.Finite(b); | ||
Check.NonNegative(tolX); | ||
|
||
if (a > b) | ||
{ | ||
(a, b) = (b, a); | ||
preferLeft = !preferLeft; | ||
} | ||
|
||
double fa = f(a, o); | ||
double fb = f(b, o); | ||
|
||
Check.Finite(fa); | ||
Check.Finite(fb); | ||
|
||
if (fa == 0) | ||
return (a, fa); | ||
|
||
if (fb == 0) | ||
return (b, fb); | ||
|
||
if (fa * fb > 0) | ||
throw new ArgumentException("Bisection rootfinding method: guess does not bracket the root"); | ||
|
||
while (true) | ||
{ | ||
double c = (a + b) / 2; | ||
|
||
if (c == a || c == b || b - c < tolX) | ||
break; | ||
|
||
double fc = f(c, o); | ||
|
||
if (fc * fa < 0) | ||
{ | ||
b = c; | ||
fb = fc; | ||
} | ||
else | ||
{ | ||
a = c; | ||
fa = fc; | ||
} | ||
} | ||
|
||
if (preferLeft) | ||
return (a, fa); | ||
return (b, fb); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.