-
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.
- Loading branch information
1 parent
b4fd71f
commit dbba47f
Showing
9 changed files
with
203 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package flatsphere | ||
|
||
import "math" | ||
|
||
// Try to find a root of the given target function. Returns NaN if the process fails. | ||
func newtonsMethod( | ||
initialGuess float64, | ||
targetFunc func(float64) float64, | ||
derivative func(float64) float64, | ||
tolerance float64, | ||
epsilon float64, | ||
maxIterations int, | ||
) float64 { | ||
currentGuess := initialGuess | ||
for i := 0; i < maxIterations; i++ { | ||
// get function/derivative values for current guess | ||
y := targetFunc(currentGuess) | ||
yPrime := derivative(currentGuess) | ||
|
||
// if denominator is too small error out | ||
if math.Abs(yPrime) < epsilon { | ||
break | ||
} | ||
|
||
nextGuess := currentGuess - y/yPrime | ||
if math.Abs(nextGuess-currentGuess) < tolerance { | ||
return nextGuess | ||
} | ||
currentGuess = nextGuess | ||
} | ||
return math.NaN() | ||
} |
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,26 @@ | ||
package flatsphere | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func FuzzNewtonsMethodSqrt(f *testing.F) { | ||
f.Add(612.0) | ||
f.Add(4.0) | ||
f.Add(100.0) | ||
f.Add(2.0) | ||
f.Add(123.0) | ||
f.Skip(0.0) | ||
f.Fuzz(func(t *testing.T, sqrd float64) { | ||
f := func(x float64) float64 { return x*x - sqrd } | ||
d := func(x float64) float64 { return 2 * x } | ||
approx := newtonsMethod(sqrd/2, f, d, 1e-6, 1e-12, 100) | ||
native := math.Sqrt(sqrd) | ||
if math.IsNaN(approx) && !math.IsNaN(native) { | ||
t.Errorf("expected %e, got %e", native, approx) | ||
} else if !math.IsNaN(approx) && !withinTolerance(approx, native, 0.00001) { | ||
t.Errorf("expected %e, got %e", native, approx) | ||
} | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package flatsphere | ||
|
||
// A package of functionality for converting from spherical locations to planar coordinates, or from | ||
// planar coordinates into spherical locations. Contains information specifying some characteristics | ||
// of the planar space mapped to by the projection functions, which can differ between projections. | ||
type Projection interface { | ||
// Convert a location on the sphere (in radians) to a coordinate on the plane. | ||
Project(lat float64, lon float64) (x float64, y float64) | ||
// Convert a coordinate on the plane to a location in radians on the sphere. | ||
Inverse(x float64, y float64) (lat float64, lon float64) | ||
// Retrieve the planar bounds of the projection. | ||
Bounds() Bounds | ||
PlanarBounds() Bounds | ||
} |
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