Skip to content

Commit

Permalink
Merge pull request #200 from ebraminio/kotlin-sam
Browse files Browse the repository at this point in the history
kotlin: Move search context to the end of arguments
  • Loading branch information
cosinekitty authored Apr 12, 2022
2 parents 4c4aa3d + 902f25d commit 5052920
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 28 deletions.
16 changes: 8 additions & 8 deletions generate/template/astronomy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ internal fun searchEarthShadow(radiusLimit: Double, direction: Double, t1: Time,
override fun eval(time: Time) = direction * (earthShadow(time).r - radiusLimit)
}
val context = Context(radiusLimit, direction)
return search(context, t1, t2, 1.0) ?: throw InternalError("Failed to find Earth shadow transition.")
return search(t1, t2, 1.0, context) ?: throw InternalError("Failed to find Earth shadow transition.")
}


Expand All @@ -1965,7 +1965,7 @@ internal fun peakEarthShadow(searchCenterTime: Time): ShadowInfo {
val window = 0.03 // initial search window, in days, before/after searchCenterTime
val t1 = searchCenterTime.addDays(-window)
val t2 = searchCenterTime.addDays(+window)
val tx = search(earthShadowSlopeContext, t1, t2, 1.0) ?:
val tx = search(t1, t2, 1.0, earthShadowSlopeContext) ?:
throw InternalError("Failed to find Earth peak shadow event.")
return earthShadow(tx)
}
Expand Down Expand Up @@ -2368,7 +2368,7 @@ class NodeEventInfo(
* A class that implements `SearchContext` can hold state information
* needed to evaluate the scalar function `eval`.
*/
interface SearchContext {
fun interface SearchContext {
/**
* Evaluates a scalar function at a given time.
*
Expand Down Expand Up @@ -4635,10 +4635,10 @@ fun jupiterMoons(time: Time) =
* window `time1`..`time2`, the function returns `null`.
*/
fun search(
func: SearchContext,
time1: Time,
time2: Time,
toleranceSeconds: Double
toleranceSeconds: Double,
func: SearchContext,
): Time? {
var t1 = time1
var t2 = time2
Expand Down Expand Up @@ -4855,7 +4855,7 @@ fun searchSunLongitude(targetLon: Double, startTime: Time, limitDays: Double): T
}
val context = Context(targetLon)
val time2 = startTime.addDays(limitDays)
return search(context, startTime, time2, 0.01)
return search(startTime, time2, 0.01, context)
}

/**
Expand Down Expand Up @@ -5018,7 +5018,7 @@ fun searchMoonPhase(targetLon: Double, startTime: Time, limitDays: Double): Time
val dt2 = min(limitDays, estDt + uncertainty)
val t1 = startTime.addDays(dt1)
val t2 = startTime.addDays(dt2)
return search(moonOffset, t1, t2, 1.0)
return search(t1, t2, 1.0, moonOffset)
}

/**
Expand Down Expand Up @@ -5203,7 +5203,7 @@ private fun internalSearchAltitude(
if (altBefore <= 0.0 && altAfter > 0.0) {
// The body crosses the horizon during the time interval.
// Search between evtBefore and evtAfter for the desired event.
val time = search(context, timeBefore, timeAfter, 1.0)
val time = search(timeBefore, timeAfter, 1.0, context)
if (time != null)
return time
}
Expand Down
4 changes: 2 additions & 2 deletions source/kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ these are used in function and type names.
| [Observer](doc/-observer/index.md) | [jvm]<br>data class [Observer](doc/-observer/index.md)(latitude: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), longitude: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), height: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html))<br>The location of an observer on (or near) the surface of the Earth. |
| [Refraction](doc/-refraction/index.md) | [jvm]<br>enum [Refraction](doc/-refraction/index.md) : [Enum](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/index.html)&lt;[Refraction](doc/-refraction/index.md)&gt; <br>Selects whether to correct for atmospheric refraction, and if so, how. |
| [RotationMatrix](doc/-rotation-matrix/index.md) | [jvm]<br>class [RotationMatrix](doc/-rotation-matrix/index.md)(rot: [Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)&lt;[DoubleArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double-array/index.html)&gt;)<br>A rotation matrix that can be used to transform one coordinate system to another. |
| [SearchContext](doc/-search-context/index.md) | [jvm]<br>interface [SearchContext](doc/-search-context/index.md)<br>Represents a function whose ascending root is to be found. |
| [SearchContext](doc/-search-context/index.md) | [jvm]<br>fun interface [SearchContext](doc/-search-context/index.md)<br>Represents a function whose ascending root is to be found. |
| [SeasonsInfo](doc/-seasons-info/index.md) | [jvm]<br>class [SeasonsInfo](doc/-seasons-info/index.md)(marchEquinox: [Time](doc/-time/index.md), juneSolstice: [Time](doc/-time/index.md), septemberEquinox: [Time](doc/-time/index.md), decemberSolstice: [Time](doc/-time/index.md))<br>The dates and times of changes of season for a given calendar year. |
| [Spherical](doc/-spherical/index.md) | [jvm]<br>data class [Spherical](doc/-spherical/index.md)(lat: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), lon: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), dist: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html))<br>Spherical coordinates: latitude, longitude, distance. |
| [StateVector](doc/-state-vector/index.md) | [jvm]<br>data class [StateVector](doc/-state-vector/index.md)(x: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), y: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), z: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), vx: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), vy: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), vz: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), t: [Time](doc/-time/index.md))<br>Represents a combined position vector and velocity vector at a given moment in time. |
Expand Down Expand Up @@ -142,7 +142,7 @@ these are used in function and type names.
| [rotationHorEcl](doc/rotation-hor-ecl.md) | [jvm]<br>fun [rotationHorEcl](doc/rotation-hor-ecl.md)(time: [Time](doc/-time/index.md), observer: [Observer](doc/-observer/index.md)): [RotationMatrix](doc/-rotation-matrix/index.md)<br>Calculates a rotation matrix from horizontal (HOR) to ecliptic J2000 (ECL). |
| [rotationHorEqd](doc/rotation-hor-eqd.md) | [jvm]<br>fun [rotationHorEqd](doc/rotation-hor-eqd.md)(time: [Time](doc/-time/index.md), observer: [Observer](doc/-observer/index.md)): [RotationMatrix](doc/-rotation-matrix/index.md)<br>Calculates a rotation matrix from horizontal (HOR) to equatorial of-date (EQD). |
| [rotationHorEqj](doc/rotation-hor-eqj.md) | [jvm]<br>fun [rotationHorEqj](doc/rotation-hor-eqj.md)(time: [Time](doc/-time/index.md), observer: [Observer](doc/-observer/index.md)): [RotationMatrix](doc/-rotation-matrix/index.md)<br>Calculates a rotation matrix from horizontal (HOR) to J2000 equatorial (EQJ). This is one of the family of functions that returns a rotation matrix for converting from one orientation to another. Source: HOR = horizontal system (x=North, y=West, z=Zenith). Target: EQJ = equatorial system, using equator at the J2000 epoch. |
| [search](doc/search.md) | [jvm]<br>fun [search](doc/search.md)(func: [SearchContext](doc/-search-context/index.md), time1: [Time](doc/-time/index.md), time2: [Time](doc/-time/index.md), toleranceSeconds: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](doc/-time/index.md)?<br>Searches for a time at which a function's value increases through zero. |
| [search](doc/search.md) | [jvm]<br>fun [search](doc/search.md)(time1: [Time](doc/-time/index.md), time2: [Time](doc/-time/index.md), toleranceSeconds: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), func: [SearchContext](doc/-search-context/index.md)): [Time](doc/-time/index.md)?<br>Searches for a time at which a function's value increases through zero. |
| [searchAltitude](doc/search-altitude.md) | [jvm]<br>fun [searchAltitude](doc/search-altitude.md)(body: [Body](doc/-body/index.md), observer: [Observer](doc/-observer/index.md), direction: [Direction](doc/-direction/index.md), startTime: [Time](doc/-time/index.md), limitDays: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), altitude: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](doc/-time/index.md)?<br>Finds the next time a body reaches a given altitude. |
| [searchGlobalSolarEclipse](doc/search-global-solar-eclipse.md) | [jvm]<br>fun [searchGlobalSolarEclipse](doc/search-global-solar-eclipse.md)(startTime: [Time](doc/-time/index.md)): [GlobalSolarEclipseInfo](doc/-global-solar-eclipse-info/index.md)<br>Searches for a solar eclipse visible anywhere on the Earth's surface. |
| [searchHourAngle](doc/search-hour-angle.md) | [jvm]<br>fun [searchHourAngle](doc/search-hour-angle.md)(body: [Body](doc/-body/index.md), observer: [Observer](doc/-observer/index.md), hourAngle: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), startTime: [Time](doc/-time/index.md)): [HourAngleInfo](doc/-hour-angle-info/index.md)<br>Searches for the time when a celestial body reaches a specified hour angle as seen by an observer on the Earth. |
Expand Down
2 changes: 1 addition & 1 deletion source/kotlin/doc/-search-context/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SearchContext

[jvm]\
interface [SearchContext](index.md)
fun interface [SearchContext](index.md)

Represents a function whose ascending root is to be found.

Expand Down
4 changes: 2 additions & 2 deletions source/kotlin/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
| [Observer](-observer/index.md) | [jvm]<br>data class [Observer](-observer/index.md)(latitude: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), longitude: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), height: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html))<br>The location of an observer on (or near) the surface of the Earth. |
| [Refraction](-refraction/index.md) | [jvm]<br>enum [Refraction](-refraction/index.md) : [Enum](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/index.html)&lt;[Refraction](-refraction/index.md)&gt; <br>Selects whether to correct for atmospheric refraction, and if so, how. |
| [RotationMatrix](-rotation-matrix/index.md) | [jvm]<br>class [RotationMatrix](-rotation-matrix/index.md)(rot: [Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)&lt;[DoubleArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double-array/index.html)&gt;)<br>A rotation matrix that can be used to transform one coordinate system to another. |
| [SearchContext](-search-context/index.md) | [jvm]<br>interface [SearchContext](-search-context/index.md)<br>Represents a function whose ascending root is to be found. |
| [SearchContext](-search-context/index.md) | [jvm]<br>fun interface [SearchContext](-search-context/index.md)<br>Represents a function whose ascending root is to be found. |
| [SeasonsInfo](-seasons-info/index.md) | [jvm]<br>class [SeasonsInfo](-seasons-info/index.md)(marchEquinox: [Time](-time/index.md), juneSolstice: [Time](-time/index.md), septemberEquinox: [Time](-time/index.md), decemberSolstice: [Time](-time/index.md))<br>The dates and times of changes of season for a given calendar year. |
| [Spherical](-spherical/index.md) | [jvm]<br>data class [Spherical](-spherical/index.md)(lat: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), lon: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), dist: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html))<br>Spherical coordinates: latitude, longitude, distance. |
| [StateVector](-state-vector/index.md) | [jvm]<br>data class [StateVector](-state-vector/index.md)(x: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), y: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), z: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), vx: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), vy: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), vz: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), t: [Time](-time/index.md))<br>Represents a combined position vector and velocity vector at a given moment in time. |
Expand Down Expand Up @@ -87,7 +87,7 @@
| [rotationHorEcl](rotation-hor-ecl.md) | [jvm]<br>fun [rotationHorEcl](rotation-hor-ecl.md)(time: [Time](-time/index.md), observer: [Observer](-observer/index.md)): [RotationMatrix](-rotation-matrix/index.md)<br>Calculates a rotation matrix from horizontal (HOR) to ecliptic J2000 (ECL). |
| [rotationHorEqd](rotation-hor-eqd.md) | [jvm]<br>fun [rotationHorEqd](rotation-hor-eqd.md)(time: [Time](-time/index.md), observer: [Observer](-observer/index.md)): [RotationMatrix](-rotation-matrix/index.md)<br>Calculates a rotation matrix from horizontal (HOR) to equatorial of-date (EQD). |
| [rotationHorEqj](rotation-hor-eqj.md) | [jvm]<br>fun [rotationHorEqj](rotation-hor-eqj.md)(time: [Time](-time/index.md), observer: [Observer](-observer/index.md)): [RotationMatrix](-rotation-matrix/index.md)<br>Calculates a rotation matrix from horizontal (HOR) to J2000 equatorial (EQJ). This is one of the family of functions that returns a rotation matrix for converting from one orientation to another. Source: HOR = horizontal system (x=North, y=West, z=Zenith). Target: EQJ = equatorial system, using equator at the J2000 epoch. |
| [search](search.md) | [jvm]<br>fun [search](search.md)(func: [SearchContext](-search-context/index.md), time1: [Time](-time/index.md), time2: [Time](-time/index.md), toleranceSeconds: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](-time/index.md)?<br>Searches for a time at which a function's value increases through zero. |
| [search](search.md) | [jvm]<br>fun [search](search.md)(time1: [Time](-time/index.md), time2: [Time](-time/index.md), toleranceSeconds: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), func: [SearchContext](-search-context/index.md)): [Time](-time/index.md)?<br>Searches for a time at which a function's value increases through zero. |
| [searchAltitude](search-altitude.md) | [jvm]<br>fun [searchAltitude](search-altitude.md)(body: [Body](-body/index.md), observer: [Observer](-observer/index.md), direction: [Direction](-direction/index.md), startTime: [Time](-time/index.md), limitDays: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), altitude: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](-time/index.md)?<br>Finds the next time a body reaches a given altitude. |
| [searchGlobalSolarEclipse](search-global-solar-eclipse.md) | [jvm]<br>fun [searchGlobalSolarEclipse](search-global-solar-eclipse.md)(startTime: [Time](-time/index.md)): [GlobalSolarEclipseInfo](-global-solar-eclipse-info/index.md)<br>Searches for a solar eclipse visible anywhere on the Earth's surface. |
| [searchHourAngle](search-hour-angle.md) | [jvm]<br>fun [searchHourAngle](search-hour-angle.md)(body: [Body](-body/index.md), observer: [Observer](-observer/index.md), hourAngle: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), startTime: [Time](-time/index.md)): [HourAngleInfo](-hour-angle-info/index.md)<br>Searches for the time when a celestial body reaches a specified hour angle as seen by an observer on the Earth. |
Expand Down
2 changes: 1 addition & 1 deletion source/kotlin/doc/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# search

[jvm]\
fun [search](search.md)(func: [SearchContext](-search-context/index.md), time1: [Time](-time/index.md), time2: [Time](-time/index.md), toleranceSeconds: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](-time/index.md)?
fun [search](search.md)(time1: [Time](-time/index.md), time2: [Time](-time/index.md), toleranceSeconds: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html), func: [SearchContext](-search-context/index.md)): [Time](-time/index.md)?

Searches for a time at which a function's value increases through zero.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ internal fun searchEarthShadow(radiusLimit: Double, direction: Double, t1: Time,
override fun eval(time: Time) = direction * (earthShadow(time).r - radiusLimit)
}
val context = Context(radiusLimit, direction)
return search(context, t1, t2, 1.0) ?: throw InternalError("Failed to find Earth shadow transition.")
return search(t1, t2, 1.0, context) ?: throw InternalError("Failed to find Earth shadow transition.")
}


Expand All @@ -1965,7 +1965,7 @@ internal fun peakEarthShadow(searchCenterTime: Time): ShadowInfo {
val window = 0.03 // initial search window, in days, before/after searchCenterTime
val t1 = searchCenterTime.addDays(-window)
val t2 = searchCenterTime.addDays(+window)
val tx = search(earthShadowSlopeContext, t1, t2, 1.0) ?:
val tx = search(t1, t2, 1.0, earthShadowSlopeContext) ?:
throw InternalError("Failed to find Earth peak shadow event.")
return earthShadow(tx)
}
Expand Down Expand Up @@ -2368,7 +2368,7 @@ class NodeEventInfo(
* A class that implements `SearchContext` can hold state information
* needed to evaluate the scalar function `eval`.
*/
interface SearchContext {
fun interface SearchContext {
/**
* Evaluates a scalar function at a given time.
*
Expand Down Expand Up @@ -4635,10 +4635,10 @@ fun jupiterMoons(time: Time) =
* window `time1`..`time2`, the function returns `null`.
*/
fun search(
func: SearchContext,
time1: Time,
time2: Time,
toleranceSeconds: Double
toleranceSeconds: Double,
func: SearchContext,
): Time? {
var t1 = time1
var t2 = time2
Expand Down Expand Up @@ -4855,7 +4855,7 @@ fun searchSunLongitude(targetLon: Double, startTime: Time, limitDays: Double): T
}
val context = Context(targetLon)
val time2 = startTime.addDays(limitDays)
return search(context, startTime, time2, 0.01)
return search(startTime, time2, 0.01, context)
}

/**
Expand Down Expand Up @@ -5018,7 +5018,7 @@ fun searchMoonPhase(targetLon: Double, startTime: Time, limitDays: Double): Time
val dt2 = min(limitDays, estDt + uncertainty)
val t1 = startTime.addDays(dt1)
val t2 = startTime.addDays(dt2)
return search(moonOffset, t1, t2, 1.0)
return search(t1, t2, 1.0, moonOffset)
}

/**
Expand Down Expand Up @@ -5203,7 +5203,7 @@ private fun internalSearchAltitude(
if (altBefore <= 0.0 && altAfter > 0.0) {
// The body crosses the horizon during the time interval.
// Search between evtBefore and evtAfter for the desired event.
val time = search(context, timeBefore, timeAfter, 1.0)
val time = search(timeBefore, timeAfter, 1.0, context)
if (time != null)
return time
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,16 +734,11 @@ class Tests {

@Test
fun `Basic search`() {
class CosineIdentityFunc : SearchContext {
override fun eval(time: Time) = time.ut - cos(time.ut)
}

val toleranceDays = 1.0e-9
val toleranceSeconds = toleranceDays / 86400.0
val func = CosineIdentityFunc()
val time1 = Time(0.0)
val time2 = Time(1.0)
val tsolve = search(func, time1, time2, toleranceSeconds)
val tsolve = search(time1, time2, toleranceSeconds) { time -> time.ut - cos(time.ut) }
if (tsolve == null)
fail("Basic search failed")

Expand Down

0 comments on commit 5052920

Please sign in to comment.