Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChangeOrbitalElements now uses Forward AutoDiff via Dual numbers/vectors
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
- Loading branch information
1 parent
be01ad9
commit 89196a3
Showing
6 changed files
with
486 additions
and
265 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors | ||
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+ | ||
*/ | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Globalization; | ||
using MechJebLib.Utils; | ||
|
||
namespace MechJebLib.Primitives | ||
{ | ||
public class Dual : IEquatable<Dual>, IComparable<Dual>, IFormattable | ||
{ | ||
public readonly double M; | ||
public readonly double D; | ||
|
||
public Dual(double m, double d = 0) | ||
{ | ||
M = m; | ||
D = d; | ||
} | ||
|
||
public static Dual Sin(Dual d) => new Dual(Math.Sin(d.M), Math.Cos(d.M) * d.D); | ||
public static Dual Cos(Dual d) => new Dual(Math.Cos(d.M), -Math.Sin(d.M) * d.D); | ||
|
||
public static Dual Tan(Dual d) | ||
{ | ||
double t = Math.Tan(d.M); | ||
return new Dual(t, (1.0 + t * t) * d.D); | ||
} | ||
|
||
public static Dual Log(Dual d) => new Dual(Math.Log(d.M), 1 / d.M * d.D); | ||
public static Dual Powi(Dual d, int k) => new Dual(Statics.Powi(d.M, k), k * Statics.Powi(d.M, k - 1) * d.D); | ||
public static Dual Abs(Dual d) => new Dual(Math.Abs(d.M), (d.M < 0 ? -1 : 1) * d.D); | ||
public static Dual Exp(Dual d) => new Dual(Math.Exp(d.M), Math.Exp(d.M) * d.D); | ||
public static Dual Sqrt(Dual d) => new Dual(Math.Sqrt(d.M), 1.0 / (2 * Math.Sqrt(d.M)) * d.D); | ||
public static Dual Max(Dual d, int i) => new Dual(Math.Max(d.M, i), d.M > i ? d.D : 0); | ||
|
||
public static Dual operator -(Dual x) => new Dual(-x.M, -x.D); | ||
public static Dual operator +(Dual lhs, Dual rhs) => new Dual(lhs.M + rhs.M, lhs.D + rhs.D); | ||
public static Dual operator -(Dual lhs, Dual rhs) => new Dual(lhs.M - rhs.M, lhs.D - rhs.D); | ||
public static Dual operator *(Dual lhs, Dual rhs) => new Dual(lhs.M * rhs.M, lhs.D * rhs.M + rhs.D * lhs.M); | ||
public static Dual operator /(Dual lhs, Dual rhs) => new Dual(lhs.M / rhs.M, (lhs.D * rhs.M - lhs.M * rhs.D) / (rhs.M * rhs.M)); | ||
|
||
public static explicit operator double(Dual d) => d.M; | ||
public static implicit operator Dual(double d) => new Dual(d); | ||
|
||
// ReSharper disable once CompareOfFloatsByEqualityOperator | ||
public bool Equals(Dual other) => M == other.M && D.Equals(other.D); | ||
|
||
public int CompareTo(Dual other) => M.CompareTo(other.M); | ||
|
||
public override string ToString() => ToString(null, CultureInfo.InvariantCulture.NumberFormat); | ||
|
||
public string ToString(string? format) => ToString(format, CultureInfo.InvariantCulture.NumberFormat); | ||
|
||
public string ToString(string? format, IFormatProvider formatProvider) | ||
{ | ||
if (string.IsNullOrEmpty(format)) | ||
format = "G"; | ||
return | ||
$"{M.ToString(format, formatProvider)} + {D.ToString(format, formatProvider)}ϵ"; | ||
} | ||
} | ||
} |
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,62 @@ | ||
/* | ||
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors | ||
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+ | ||
*/ | ||
|
||
#nullable enable | ||
|
||
using System; | ||
|
||
namespace MechJebLib.Primitives | ||
{ | ||
public class DualV3 : IEquatable<DualV3>, IComparable<DualV3>, IFormattable | ||
{ | ||
public readonly V3 M; | ||
public readonly V3 D; | ||
|
||
public Dual x => new Dual(M.x, D.x); | ||
public Dual y => new Dual(M.y, D.y); | ||
public Dual z => new Dual(M.z, D.z); | ||
|
||
private DualV3(V3 m, V3? dx = null) | ||
{ | ||
M = m; | ||
D = dx ?? V3.zero; | ||
} | ||
|
||
public DualV3(Dual x, Dual y, Dual z) : this(x.M, y.M, z.M, x.D, y.D, z.D) { } | ||
public DualV3(double mx, double my, double mz, double dx, double dy, double dz) : this(new V3(mx, my, mz), new V3(dx, dy, dz)) { } | ||
|
||
public Dual sqrMagnitude => x * x + y * y + z * z; | ||
public Dual magnitude => Dual.Sqrt(x * x + y * y + z * z); | ||
public DualV3 normalized => this / magnitude; | ||
|
||
public static DualV3 operator +(DualV3 a, DualV3 b) => new DualV3(a.x + b.x, a.y + b.y, a.z + b.z); | ||
|
||
public static DualV3 operator -(DualV3 a, DualV3 b) => new DualV3(a.x - b.x, a.y - b.y, a.z - b.z); | ||
|
||
public static DualV3 operator *(DualV3 a, DualV3 b) => new DualV3(a.x * b.x, a.y * b.y, a.z * b.z); | ||
|
||
public static DualV3 operator /(DualV3 a, DualV3 b) => new DualV3(a.x / b.x, a.y / b.y, a.z / b.z); | ||
|
||
public static DualV3 operator -(DualV3 a) => new DualV3(-a.x, -a.y, -a.z); | ||
|
||
public static DualV3 operator *(DualV3 a, Dual d) => new DualV3(a.x * d, a.y * d, a.z * d); | ||
|
||
public static DualV3 operator *(Dual d, DualV3 a) => new DualV3(a.x * d, a.y * d, a.z * d); | ||
|
||
public static DualV3 operator /(DualV3 a, Dual d) => new DualV3(a.x / d, a.y / d, a.z / d); | ||
|
||
public static explicit operator V3(DualV3 d) => d.M; | ||
public static implicit operator DualV3(V3 d) => new DualV3(d); | ||
|
||
public bool Equals(DualV3 other) => throw new NotImplementedException(); | ||
public int CompareTo(DualV3 other) => throw new NotImplementedException(); | ||
public string ToString(string format, IFormatProvider formatProvider) => throw new NotImplementedException(); | ||
|
||
public static DualV3 Cross(DualV3 v1, DualV3 v2) => | ||
new DualV3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x); | ||
|
||
public static Dual Dot(DualV3 v1, DualV3 v2) => v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; | ||
} | ||
} |
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