Skip to content

Commit

Permalink
Use System.Math statically
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Aug 22, 2023
1 parent dff3aa2 commit be01ad9
Show file tree
Hide file tree
Showing 41 changed files with 482 additions and 769 deletions.
6 changes: 3 additions & 3 deletions MechJeb2/MechJebLib/Control/PIDLoop.cs
Expand Up @@ -3,8 +3,8 @@
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/

using System;
using static MechJebLib.Utils.Statics;
using static System.Math;

#nullable enable

Expand Down Expand Up @@ -43,10 +43,10 @@ public double Update(double reference, double measured)

double delta = reference - measured;

if (Math.Abs(delta) < Deadband)
if (Abs(delta) < Deadband)
delta = 0;
else
delta -= Math.Sign(delta) * Deadband;
delta -= Sign(delta) * Deadband;

// clegg filtering on zero-crossing to remove integral windup
if (_delta1.IsFinite() && Clegg && ((delta < 0 && _delta1 > 0) || (delta > 0 && _delta1 < 0)))
Expand Down
17 changes: 9 additions & 8 deletions MechJeb2/MechJebLib/Core/BrentMin.cs
Expand Up @@ -5,6 +5,7 @@

using System;
using static MechJebLib.Utils.Statics;
using static System.Math;

#nullable enable

Expand All @@ -29,10 +30,10 @@ public static class BrentMin
public static (double x, double fx) Solve(Func<double, object?, double> f, double xmin, double xmax, object? o = null, double rtol = 1e-9,
int maxiter = 50)
{
double c = 0.5 * (3.0 - Math.Sqrt(5.0)); // C is the square of the inverse of the golden ratio.
double c = 0.5 * (3.0 - Sqrt(5.0)); // C is the square of the inverse of the golden ratio.
double d = 0;
double e = 0.0;
double eps = Math.Sqrt(EPS);
double eps = Sqrt(EPS);
double sa = xmin;
double sb = xmax;

Expand All @@ -49,34 +50,34 @@ public static class BrentMin
while (true)
{
double m = 0.5 * (sa + sb);
double t = eps * Math.Abs(x) + rtol;
double t = eps * Abs(x) + rtol;
double t2 = 2.0 * t;
//
// Check the stopping criterion.
//
if (Math.Abs(x - m) <= t2 - 0.5 * (sb - sa)) break;
if (Abs(x - m) <= t2 - 0.5 * (sb - sa)) break;
//
// Fit a parabola.
//
double r = 0.0;
double q = r;
double p = q;

if (t < Math.Abs(e))
if (t < Abs(e))
{
r = (x - w) * (fx - fv);
q = (x - v) * (fx - fw);
p = (x - v) * q - (x - w) * r;
q = 2.0 * (q - r);
if (0.0 < q) p = -p;
q = Math.Abs(q);
q = Abs(q);
r = e;
e = d;
}

double u;

if (Math.Abs(p) < Math.Abs(0.5 * q * r) &&
if (Abs(p) < Abs(0.5 * q * r) &&
q * (sa - x) < p &&
p < q * (sb - x))
{
Expand Down Expand Up @@ -111,7 +112,7 @@ public static class BrentMin
//
// F must not be evaluated too close to X.
//
if (t <= Math.Abs(d))
if (t <= Abs(d))
u = x + d;
else if (0.0 < d)
u = x + t;
Expand Down
25 changes: 13 additions & 12 deletions MechJeb2/MechJebLib/Core/BrentRoot.cs
Expand Up @@ -8,6 +8,7 @@
using System;
using MechJebLib.Utils;
using static MechJebLib.Utils.Statics;
using static System.Math;

// ReSharper disable CompareOfFloatsByEqualityOperator
namespace MechJebLib.Core
Expand Down Expand Up @@ -49,7 +50,7 @@ public static class BrentRoot
if (fa * fb > 0)
throw new ArgumentException("Brent's rootfinding method: guess does not bracket the root");

if (Math.Abs(fa) < Math.Abs(fb))
if (Abs(fa) < Abs(fb))
{
(a, b) = (b, a);
(fa, fb) = (fb, fa);
Expand Down Expand Up @@ -82,14 +83,14 @@ public static double Solve(Func<double, object?, double> f, double x, object? o,
return a;

// initial guess to expand search for the zero
double dx = Math.Abs(x / 50);
double dx = Abs(x / 50);

// if x is zero use a larger expansion
if (dx <= 2.24e-15)
dx = 1 / 50d;

// expand by sqrt(2) each iteration
double sqrt2 = Math.Sqrt(2);
double sqrt2 = Sqrt(2);

// iterate until we find a range that brackets the root
while (fa > 0 == fb > 0)
Expand Down Expand Up @@ -145,7 +146,7 @@ public static double Solve(Func<double, object?, double> f, double x, object? o,
e = d;
}

if (Math.Abs(fc) < Math.Abs(fb))
if (Abs(fc) < Abs(fb))
{
a = b;
b = c;
Expand All @@ -156,17 +157,17 @@ public static double Solve(Func<double, object?, double> f, double x, object? o,
}

double m = 0.5 * (c - b);
double toler = 2.0 * rtol * Math.Max(Math.Abs(b), 1.0);
if (Math.Abs(m) <= toler || fb == 0.0)
double toler = 2.0 * rtol * Max(Abs(b), 1.0);
if (Abs(m) <= toler || fb == 0.0)
{
// try one more round to improve fc if fb does not match the sign
if (maybeOneMore && Math.Sign(fb) != sign)
if (maybeOneMore && Sign(fb) != sign)
maybeOneMore = false;
else
break;
}

if (Math.Abs(e) < toler || Math.Abs(fa) <= Math.Abs(fb))
if (Abs(e) < toler || Abs(fa) <= Abs(fb))
{
// Bisection
d = m;
Expand Down Expand Up @@ -200,7 +201,7 @@ public static double Solve(Func<double, object?, double> f, double x, object? o,
p = -p;

// Acceptibility check
if (2.0 * p < 3.0 * m * q - Math.Abs(toler * q) && p < Math.Abs(0.5 * e * q))
if (2.0 * p < 3.0 * m * q - Abs(toler * q) && p < Abs(0.5 * e * q))
{
e = d;
d = p / q;
Expand All @@ -215,7 +216,7 @@ public static double Solve(Func<double, object?, double> f, double x, object? o,
a = b;
fa = fb;

if (Math.Abs(d) > toler)
if (Abs(d) > toler)
b += d;
else if (b > c)
b -= toler;
Expand All @@ -228,10 +229,10 @@ public static double Solve(Func<double, object?, double> f, double x, object? o,
Check.Finite(fa);

if (i++ >= maxiter && maxiter > 0)
throw new TimeoutException("Brent's rootfinding method: maximum iterations exceeded: " + Math.Abs(a - c));
throw new TimeoutException("Brent's rootfinding method: maximum iterations exceeded: " + Abs(a - c));
}

if (sign != 0 && Math.Sign(fb) != sign)
if (sign != 0 && Sign(fb) != sign)
return c;

return b;
Expand Down
Expand Up @@ -59,7 +59,7 @@ public static ( V3 vNeg, V3 vPos, V3 r, double dt) Run2(double mu, V3 r0, V3 v0,
{
if (debug)
{
Log("[MechJeb] singleImpulseHyperbolicBurn mu = " + mu + " r0 = " + r0 + " v0 = " + v0 + " vInf = " + vInf + " rot = " + rot);
Print("[MechJeb] singleImpulseHyperbolicBurn mu = " + mu + " r0 = " + r0 + " v0 = " + v0 + " vInf = " + vInf + " rot = " + rot);
}

// angular momentum of the parking orbit
Expand Down Expand Up @@ -95,7 +95,7 @@ public static ( V3 vNeg, V3 vPos, V3 r, double dt) Run2(double mu, V3 r0, V3 v0,

if (debug)
{
Log("rp0hat: " + rp0Hat + " e0: " + Math.Abs(ecc0));
Print("rp0hat: " + rp0Hat + " e0: " + Math.Abs(ecc0));
}

// parking orbit periapsis velocity unit vector
Expand Down Expand Up @@ -147,7 +147,7 @@ public static ( V3 vNeg, V3 vPos, V3 r, double dt) Run2(double mu, V3 r0, V3 v0,

if (debug)
{
Log("position of burn: " + r);
Print("position of burn: " + r);
}

// constant
Expand Down Expand Up @@ -206,10 +206,10 @@ public static ( V3 vNeg, V3 vPos, V3 r, double dt) Run2(double mu, V3 r0, V3 v0,

if (debug)
{
Log("mean motion: " + n);
Log("true anomaly of ref position: " + nu0 + " true anomaly of burn: " + nu10);
Log("eccentric anomaly of ref position: " + e0 + " eccentric anomaly of burn: " + e1);
Log("mean anomaly of ref posotion: " + m0 + " mean anomaly of burn: " + m1);
Print("mean motion: " + n);
Print("true anomaly of ref position: " + nu0 + " true anomaly of burn: " + nu10);
Print("eccentric anomaly of ref position: " + e0 + " eccentric anomaly of burn: " + e1);
Print("mean anomaly of ref posotion: " + m0 + " mean anomaly of burn: " + m1);
}

// coast time on the parking orbit
Expand All @@ -221,7 +221,7 @@ public static ( V3 vNeg, V3 vPos, V3 r, double dt) Run2(double mu, V3 r0, V3 v0,

if (debug)
{
Log("[MechJeb] singleImpulseHyperbolicBurn vNeg = " + vNeg + " vPos = " + vPos + " r = " + r + " dt = " + dt);
Print("[MechJeb] singleImpulseHyperbolicBurn vNeg = " + vNeg + " vPos = " + vPos + " r = " + r + " dt = " + dt);
}

return (vNeg, vPos, r, dt);
Expand Down
41 changes: 21 additions & 20 deletions MechJeb2/MechJebLib/Core/Functions/Angles.cs
Expand Up @@ -8,6 +8,7 @@
using JetBrains.Annotations;
using MechJebLib.Utils;
using static MechJebLib.Utils.Statics;
using static System.Math;

#nullable enable

Expand Down Expand Up @@ -35,7 +36,7 @@ public static double KeplerEquationPrime(double E, double M, double ecc)
Check.Finite(M);
Check.NonNegativeFinite(ecc);

return 1 - ecc * Math.Cos(E);
return 1 - ecc * Cos(E);
}

[UsedImplicitly]
Expand All @@ -51,10 +52,10 @@ public static double NewtonElliptic(double E0, double M, double ecc)
for (int i = 0; i < 50; i++)
{
double delta = KeplerEquation(E, M, ecc) / KeplerEquationPrime(E, M, ecc);
if (Math.Abs(delta) > PI)
delta = PI * Math.Sign(delta);
if (Abs(delta) > PI)
delta = PI * Sign(delta);
E -= delta;
if (Math.Abs(delta) < tol)
if (Abs(delta) < tol)
return E;
}

Expand All @@ -80,7 +81,7 @@ public static double KeplerEquationPrimeHyper(double F, double M, double ecc)
Check.Finite(M);
Check.NonNegativeFinite(ecc);

return ecc * Math.Cosh(F) - 1;
return ecc * Cosh(F) - 1;
}

[UsedImplicitly]
Expand All @@ -96,10 +97,10 @@ public static double NewtonHyperbolic(double F0, double M, double ecc)
for (int i = 0; i < 50; i++)
{
double delta = KeplerEquationHyper(F, M, ecc) / KeplerEquationPrimeHyper(F, M, ecc);
if (Math.Abs(delta) > PI)
delta = PI * Math.Sign(delta);
if (Abs(delta) > PI)
delta = PI * Sign(delta);
F -= delta;
if (Math.Abs(delta) < tol)
if (Abs(delta) < tol)
return F;
}

Expand All @@ -111,15 +112,15 @@ public static double NuFromD(double D)
{
Check.Finite(D);

return 2.0 * Math.Atan(D);
return 2.0 * Atan(D);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double DFromNu(double nu)
{
Check.Finite(nu);

return Math.Tan(nu / 2.0);
return Tan(nu / 2.0);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -128,7 +129,7 @@ public static double EFromNu(double nu, double ecc)
Check.Finite(nu);
Check.NonNegativeFinite(ecc);

return 2 * Math.Atan(Math.Sqrt((1 - ecc) / (1 + ecc)) * Math.Tan(nu / 2));
return 2 * Atan(Sqrt((1 - ecc) / (1 + ecc)) * Tan(nu / 2));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -137,7 +138,7 @@ public static double FFromNu(double nu, double ecc)
Check.Finite(nu);
Check.NonNegativeFinite(ecc);

return 2 * Atanh(Math.Sqrt((ecc - 1) / (ecc + 1)) * Math.Tan(nu / 2));
return 2 * Atanh(Sqrt((ecc - 1) / (ecc + 1)) * Tan(nu / 2));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -146,7 +147,7 @@ public static double NuFromE(double E, double ecc)
Check.Finite(E);
Check.NonNegativeFinite(ecc);

return 2 * Math.Atan(Math.Sqrt((1 + ecc) / (1 - ecc)) * Math.Tan(E / 2));
return 2 * Atan(Sqrt((1 + ecc) / (1 - ecc)) * Tan(E / 2));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -155,7 +156,7 @@ public static double NuFromF(double F, double ecc)
Check.Finite(F);
Check.NonNegativeFinite(ecc);

return 2 * Math.Atan(Math.Sqrt((ecc + 1) / (ecc - 1)) * Math.Tanh(F / 2));
return 2 * Atan(Sqrt((ecc + 1) / (ecc - 1)) * Tanh(F / 2));
}

public static double EFromM(double M, double ecc)
Expand All @@ -164,7 +165,7 @@ public static double EFromM(double M, double ecc)
Check.NonNegativeFinite(ecc);

double E0;
if ((-Math.PI < M && M < 0) || Math.PI < M)
if ((-PI < M && M < 0) || PI < M)
{
E0 = M - ecc;
}
Expand All @@ -190,8 +191,8 @@ public static double DFromM(double M)
Check.Finite(M);

double B = 3.0 * M / 2.0;
double A = Math.Pow(B + Math.Sqrt(1.0 + Math.Pow(B, 2)), 2.0 / 3.0);
return 2.0 * A * B / (1.0 + A + Math.Pow(A, 2));
double A = Pow(B + Sqrt(1.0 + Pow(B, 2)), 2.0 / 3.0);
return 2.0 * A * B / (1.0 + A + Pow(A, 2));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -200,7 +201,7 @@ public static double MFromE(double E, double ecc)
Check.Finite(E);
Check.NonNegativeFinite(ecc);

return E - ecc * Math.Sin(E);
return E - ecc * Sin(E);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -209,15 +210,15 @@ public static double MFromF(double F, double ecc)
Check.Finite(F);
Check.NonNegativeFinite(ecc);

return ecc * Math.Sinh(F) - F;
return ecc * Sinh(F) - F;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double MFromD(double D)
{
Check.Finite(D);

return D + Math.Pow(D, 3) / 3.0;
return D + Pow(D, 3) / 3.0;
}

public static double MFromNu(double nu, double ecc)
Expand Down

0 comments on commit be01ad9

Please sign in to comment.