From d2485392c342694bc44affedffa96b65f1933aba Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 28 Apr 2022 01:19:36 +0430 Subject: [PATCH 1/6] kotlin: Use getOrPut to facilitate use of cache --- generate/template/astronomy.kt | 9 +++------ .../kotlin/io/github/cosinekitty/astronomy/astronomy.kt | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/generate/template/astronomy.kt b/generate/template/astronomy.kt index 645a8b36..fe07e33c 100644 --- a/generate/template/astronomy.kt +++ b/generate/template/astronomy.kt @@ -3247,9 +3247,8 @@ private fun getPlutoSegment(tt: Double): List? { return null // Don't bother calculating a segment. Let the caller crawl backward/forward to this time val segIndex = clampIndex((tt - plutoStateTable[0].tt) / PLUTO_TIME_STEP, PLUTO_NUM_STATES-1) - synchronized (plutoCache) { - var list = plutoCache.get(segIndex) - if (list == null) { + return synchronized(plutoCache) { + plutoCache.getOrPut(segIndex) { val seg = ArrayList() // The first endpoint is exact. @@ -3290,10 +3289,8 @@ private fun getPlutoSegment(tt: Double): List? { seg[i].a.mix(ramp, reverse[i].a) } - list = seg.toList() - plutoCache.set(segIndex, list) + seg } - return list } } diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt index 5be82858..11d16b92 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt @@ -3247,9 +3247,8 @@ private fun getPlutoSegment(tt: Double): List? { return null // Don't bother calculating a segment. Let the caller crawl backward/forward to this time val segIndex = clampIndex((tt - plutoStateTable[0].tt) / PLUTO_TIME_STEP, PLUTO_NUM_STATES-1) - synchronized (plutoCache) { - var list = plutoCache.get(segIndex) - if (list == null) { + return synchronized(plutoCache) { + plutoCache.getOrPut(segIndex) { val seg = ArrayList() // The first endpoint is exact. @@ -3290,10 +3289,8 @@ private fun getPlutoSegment(tt: Double): List? { seg[i].a.mix(ramp, reverse[i].a) } - list = seg.toList() - plutoCache.set(segIndex, list) + seg } - return list } } From 2fe4c8abf4d62f17cd89c2dca2a103826c6aaee5 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 28 Apr 2022 01:37:35 +0430 Subject: [PATCH 2/6] kotlin: Use single format instead multiple calls --- generate/template/astronomy.kt | 23 ++++--------------- .../github/cosinekitty/astronomy/astronomy.kt | 23 ++++--------------- 2 files changed, 8 insertions(+), 38 deletions(-) diff --git a/generate/template/astronomy.kt b/generate/template/astronomy.kt index fe07e33c..916c552c 100644 --- a/generate/template/astronomy.kt +++ b/generate/template/astronomy.kt @@ -451,22 +451,8 @@ class DateTime( val millis: Double = 1000.0 * (second - wholeSeconds) val wholeMillis: Int = millis.toInt() - return ( - "%04d".format(year) + - "-" + - "%02d".format(month) + - "-" + - "%02d".format(day) + - "T" + - "%02d".format(hour) + - ":" + - "%02d".format(minute) + - ":" + - "%02d".format(wholeSeconds) + - "." + - "%03d".format(wholeMillis) + - "Z" - ) + return "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ" + .format(year, month, day, hour, minute, wholeSeconds, wholeMillis) } } @@ -6477,8 +6463,7 @@ fun searchPeakMagnitude(body: Body, startTime: Time): IlluminationInfo { var iter = 0 var searchTime = startTime - while (++iter <= 2) - { + while (++iter <= 2) { // Find current heliocentric relative longitude between the // inferior planet and the Earth. val plon = eclipticLongitude(body, searchTime) @@ -7253,7 +7238,7 @@ internal fun planetExtreme(body: Body, kind: ApsisKind, startTime: Time, initDay for (i in 0 until npoints) { val time = searchTime.addDays(i * interval) val distance = direction * helioDistance(body, time) - if (i==0 || distance > bestDistance) { + if (i == 0 || distance > bestDistance) { bestI = i bestDistance = distance } diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt index 11d16b92..b4b26962 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt @@ -451,22 +451,8 @@ class DateTime( val millis: Double = 1000.0 * (second - wholeSeconds) val wholeMillis: Int = millis.toInt() - return ( - "%04d".format(year) + - "-" + - "%02d".format(month) + - "-" + - "%02d".format(day) + - "T" + - "%02d".format(hour) + - ":" + - "%02d".format(minute) + - ":" + - "%02d".format(wholeSeconds) + - "." + - "%03d".format(wholeMillis) + - "Z" - ) + return "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ" + .format(year, month, day, hour, minute, wholeSeconds, wholeMillis) } } @@ -6477,8 +6463,7 @@ fun searchPeakMagnitude(body: Body, startTime: Time): IlluminationInfo { var iter = 0 var searchTime = startTime - while (++iter <= 2) - { + while (++iter <= 2) { // Find current heliocentric relative longitude between the // inferior planet and the Earth. val plon = eclipticLongitude(body, searchTime) @@ -7253,7 +7238,7 @@ internal fun planetExtreme(body: Body, kind: ApsisKind, startTime: Time, initDay for (i in 0 until npoints) { val time = searchTime.addDays(i * interval) val distance = direction * helioDistance(body, time) - if (i==0 || distance > bestDistance) { + if (i == 0 || distance > bestDistance) { bestI = i bestDistance = distance } From 6789c64343d009bfbc00ba59745b60b0a0c18469 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 28 Apr 2022 01:55:24 +0430 Subject: [PATCH 3/6] kotlin: Use idiomatic way to create mutable objects --- generate/template/astronomy.kt | 24 +++++++++---------- .../github/cosinekitty/astronomy/astronomy.kt | 24 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/generate/template/astronomy.kt b/generate/template/astronomy.kt index 916c552c..9471a3bc 100644 --- a/generate/template/astronomy.kt +++ b/generate/template/astronomy.kt @@ -3235,7 +3235,7 @@ private fun getPlutoSegment(tt: Double): List? { val segIndex = clampIndex((tt - plutoStateTable[0].tt) / PLUTO_TIME_STEP, PLUTO_NUM_STATES-1) return synchronized(plutoCache) { plutoCache.getOrPut(segIndex) { - val seg = ArrayList() + val seg = mutableListOf() // The first endpoint is exact. var sim = gravFromState(plutoStateTable[segIndex]) @@ -3254,18 +3254,18 @@ private fun getPlutoSegment(tt: Double): List? { seg.add(sim.grav) // Simulate backwards from the upper time bound. - val backward = ArrayList() - backward.add(sim.grav) + val reverse = buildList { + add(sim.grav) - steptt = sim.grav.tt - for (i in (PLUTO_NSTEPS-2) downTo 1) { - steptt -= PLUTO_DT - sim = simulateGravity(steptt, sim.grav) - backward.add(sim.grav) - } + steptt = sim.grav.tt + for (i in (PLUTO_NSTEPS-2) downTo 1) { + steptt -= PLUTO_DT + sim = simulateGravity(steptt, sim.grav) + add(sim.grav) + } - backward.add(seg[0]) - val reverse = backward.reversed() + add(seg[0]) + }.reversed() // Fade-mix the two series so that there are no discontinuities. for (i in (PLUTO_NSTEPS-2) downTo 1) { @@ -7635,7 +7635,7 @@ private fun addSolarTerms(context: MoonContext) {$ASTRO_ADDSOL()} // Pluto state table $ASTRO_PLUTO_TABLE() -private val plutoCache = HashMap>() +private val plutoCache = mutableMapOf>() //--------------------------------------------------------------------------------------- // Models for Jupiter's four largest moons. diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt index b4b26962..30f53f85 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt @@ -3235,7 +3235,7 @@ private fun getPlutoSegment(tt: Double): List? { val segIndex = clampIndex((tt - plutoStateTable[0].tt) / PLUTO_TIME_STEP, PLUTO_NUM_STATES-1) return synchronized(plutoCache) { plutoCache.getOrPut(segIndex) { - val seg = ArrayList() + val seg = mutableListOf() // The first endpoint is exact. var sim = gravFromState(plutoStateTable[segIndex]) @@ -3254,18 +3254,18 @@ private fun getPlutoSegment(tt: Double): List? { seg.add(sim.grav) // Simulate backwards from the upper time bound. - val backward = ArrayList() - backward.add(sim.grav) + val reverse = buildList { + add(sim.grav) - steptt = sim.grav.tt - for (i in (PLUTO_NSTEPS-2) downTo 1) { - steptt -= PLUTO_DT - sim = simulateGravity(steptt, sim.grav) - backward.add(sim.grav) - } + steptt = sim.grav.tt + for (i in (PLUTO_NSTEPS-2) downTo 1) { + steptt -= PLUTO_DT + sim = simulateGravity(steptt, sim.grav) + add(sim.grav) + } - backward.add(seg[0]) - val reverse = backward.reversed() + add(seg[0]) + }.reversed() // Fade-mix the two series so that there are no discontinuities. for (i in (PLUTO_NSTEPS-2) downTo 1) { @@ -8625,7 +8625,7 @@ private val plutoStateTable: Array = arrayOf( , BodyState( 700800.0, TerseVector(-15.576200701394, 34.399412961275, 15.466033737854), TerseVector(-2.0098814612884e-03, -1.7191109825989e-03, 7.0414782780416e-05)) , BodyState( 730000.0, TerseVector( 4.243252837090, -30.118201690825, -10.707441231349), TerseVector( 3.1725847067411e-03, 1.6098461202270e-04, -9.0672150593868e-04)) ) -private val plutoCache = HashMap>() +private val plutoCache = mutableMapOf>() //--------------------------------------------------------------------------------------- // Models for Jupiter's four largest moons. From 474cffccac988aef324377f026b107b8a7d80b7e Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 28 Apr 2022 02:42:22 +0430 Subject: [PATCH 4/6] kotlin: Use sumOf where possible As sumOf is inline in Kotlin it supposed to have the same performance. --- generate/template/astronomy.kt | 13 ++++--------- .../io/github/cosinekitty/astronomy/astronomy.kt | 13 ++++--------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/generate/template/astronomy.kt b/generate/template/astronomy.kt index 9471a3bc..0e7a92a2 100644 --- a/generate/template/astronomy.kt +++ b/generate/template/astronomy.kt @@ -2747,9 +2747,7 @@ private fun vsopFormulaCalc(formula: VsopFormula, t: Double, clampAngle: Boolean var coord = 0.0 var tpower = 1.0 for (series in formula.series) { - var sum = 0.0 - for (term in series.term) - sum += term.amplitude * cos(term.phase + (t * term.frequency)) + val sum = series.term.sumOf { it.amplitude * cos(it.phase + (t * it.frequency)) } coord += if (clampAngle) (tpower * sum) % PI2 // improve precision: longitude angles can be hundreds of radians @@ -2799,7 +2797,7 @@ private fun vsopDerivCalc(formula: VsopFormula, t: Double): Double { var tpower = 1.0 // t^s var dpower = 0.0 // t^(s-1) var deriv = 0.0 - for ((s, series) in formula.series.withIndex()) { + formula.series.forEachIndexed { s, series -> var sinSum = 0.0 var cosSum = 0.0 for (term in series.term) { @@ -3427,13 +3425,10 @@ private fun calcJupiterMoon(time: Time, m: JupiterMoon): StateVector { val t = time.tt + 18262.5 // number of days since 1950-01-01T00:00:00Z // Calculate 6 orbital elements at the given time t. - var elem0 = 0.0 - for (term in m.a) - elem0 += term.amplitude * cos(term.phase + (t * term.frequency)) + val elem0 = m.a.sumOf { term -> term.amplitude * cos(term.phase + (t * term.frequency)) } var elem1 = m.al0 + (t * m.al1) - for (term in m.l) - elem1 += term.amplitude * sin(term.phase + (t * term.frequency)) + elem1 += m.l.sumOf { term -> term.amplitude * sin(term.phase + (t * term.frequency)) } elem1 %= PI2 if (elem1 < 0) diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt index 30f53f85..55849f13 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt @@ -2747,9 +2747,7 @@ private fun vsopFormulaCalc(formula: VsopFormula, t: Double, clampAngle: Boolean var coord = 0.0 var tpower = 1.0 for (series in formula.series) { - var sum = 0.0 - for (term in series.term) - sum += term.amplitude * cos(term.phase + (t * term.frequency)) + val sum = series.term.sumOf { it.amplitude * cos(it.phase + (t * it.frequency)) } coord += if (clampAngle) (tpower * sum) % PI2 // improve precision: longitude angles can be hundreds of radians @@ -2799,7 +2797,7 @@ private fun vsopDerivCalc(formula: VsopFormula, t: Double): Double { var tpower = 1.0 // t^s var dpower = 0.0 // t^(s-1) var deriv = 0.0 - for ((s, series) in formula.series.withIndex()) { + formula.series.forEachIndexed { s, series -> var sinSum = 0.0 var cosSum = 0.0 for (term in series.term) { @@ -3427,13 +3425,10 @@ private fun calcJupiterMoon(time: Time, m: JupiterMoon): StateVector { val t = time.tt + 18262.5 // number of days since 1950-01-01T00:00:00Z // Calculate 6 orbital elements at the given time t. - var elem0 = 0.0 - for (term in m.a) - elem0 += term.amplitude * cos(term.phase + (t * term.frequency)) + val elem0 = m.a.sumOf { term -> term.amplitude * cos(term.phase + (t * term.frequency)) } var elem1 = m.al0 + (t * m.al1) - for (term in m.l) - elem1 += term.amplitude * sin(term.phase + (t * term.frequency)) + elem1 += m.l.sumOf { term -> term.amplitude * sin(term.phase + (t * term.frequency)) } elem1 %= PI2 if (elem1 < 0) From 7ab343e7d4245a05aff20264b237be7e6519f988 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 28 Apr 2022 03:41:15 +0430 Subject: [PATCH 5/6] kotlin: Avoide code duplication in jupiterMoons by using map --- generate/template/astronomy.kt | 14 ++++---------- .../io/github/cosinekitty/astronomy/astronomy.kt | 14 ++++---------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/generate/template/astronomy.kt b/generate/template/astronomy.kt index 0e7a92a2..6ca31fb6 100644 --- a/generate/template/astronomy.kt +++ b/generate/template/astronomy.kt @@ -3880,7 +3880,7 @@ private fun terra(observer: Observer, time: Time): StateVector { val phi = observer.latitude.degreesToRadians() val sinphi = sin(phi) val cosphi = cos(phi) - val c = 1.0 / hypot(cosphi, EARTH_FLATTENING * sinphi) + val c = 1.0 / hypot(cosphi, EARTH_FLATTENING * sinphi) val s = c * EARTH_FLATTENING_SQUARED val heightKm = observer.height / 1000.0 val ach = (EARTH_EQUATORIAL_RADIUS_KM * c) + heightKm @@ -4747,11 +4747,10 @@ fun equator( val gcObserver = geoPos(time, observer) val gc = geoVector(body, time, aberration) val j2000 = gc - gcObserver - val vector = when (equdate) { + return when (equdate) { EquatorEpoch.OfDate -> gyration(j2000, PrecessDirection.From2000) EquatorEpoch.J2000 -> j2000 - } - return vector.toEquatorial() + }.toEquatorial() } /** @@ -5140,12 +5139,7 @@ fun nextTransit(body: Body, prevTransitTime: Time) = * of how Jupiter and its moons look from Earth. */ fun jupiterMoons(time: Time) = - JupiterMoonsInfo(arrayOf( - calcJupiterMoon(time, jupiterMoonModel[0]), - calcJupiterMoon(time, jupiterMoonModel[1]), - calcJupiterMoon(time, jupiterMoonModel[2]), - calcJupiterMoon(time, jupiterMoonModel[3]) - )) + JupiterMoonsInfo(jupiterMoonModel.map { calcJupiterMoon(time, it) }.toTypedArray()) /** * Searches for a time at which a function's value increases through zero. diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt index 55849f13..6001ad40 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt @@ -3880,7 +3880,7 @@ private fun terra(observer: Observer, time: Time): StateVector { val phi = observer.latitude.degreesToRadians() val sinphi = sin(phi) val cosphi = cos(phi) - val c = 1.0 / hypot(cosphi, EARTH_FLATTENING * sinphi) + val c = 1.0 / hypot(cosphi, EARTH_FLATTENING * sinphi) val s = c * EARTH_FLATTENING_SQUARED val heightKm = observer.height / 1000.0 val ach = (EARTH_EQUATORIAL_RADIUS_KM * c) + heightKm @@ -4747,11 +4747,10 @@ fun equator( val gcObserver = geoPos(time, observer) val gc = geoVector(body, time, aberration) val j2000 = gc - gcObserver - val vector = when (equdate) { + return when (equdate) { EquatorEpoch.OfDate -> gyration(j2000, PrecessDirection.From2000) EquatorEpoch.J2000 -> j2000 - } - return vector.toEquatorial() + }.toEquatorial() } /** @@ -5140,12 +5139,7 @@ fun nextTransit(body: Body, prevTransitTime: Time) = * of how Jupiter and its moons look from Earth. */ fun jupiterMoons(time: Time) = - JupiterMoonsInfo(arrayOf( - calcJupiterMoon(time, jupiterMoonModel[0]), - calcJupiterMoon(time, jupiterMoonModel[1]), - calcJupiterMoon(time, jupiterMoonModel[2]), - calcJupiterMoon(time, jupiterMoonModel[3]) - )) + JupiterMoonsInfo(jupiterMoonModel.map { calcJupiterMoon(time, it) }.toTypedArray()) /** * Searches for a time at which a function's value increases through zero. From 462f172e6d4fd2ec179c00b60d7f41c3dbd663e9 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Wed, 27 Apr 2022 21:18:29 -0400 Subject: [PATCH 6/6] Kotlin: restored original series calculations. Changing series calculations to use `sumOf` caused test failures for Jupiter's moons, compared to the other languages. I don't understand why, but it is losing about 5 decimal places of accuracy: First file: temp/c_check.txt Second file: temp/k_check.txt Tolerance = 6.700e-15 lnum a_value b_value factor diff name OK 59558 -3.7992709399390907e-04 -3.7992709399390658e-04 411.52263 1.026e-15 helio_x OK 70088 -1.2672603312645981e-03 -1.2672603312646005e-03 411.52263 9.816e-16 helio_y OK 122819 1.0937502621531685e-04 1.0937502621531790e-04 411.52263 4.294e-16 helio_z OK 2427 2.0269124415104024e+01 2.0269124415104031e+01 0.01730 1.229e-16 sky_j2000_ra OK 96711 -1.3269907232295576e+01 -1.3269907232295552e+01 0.00556 1.382e-16 sky_j2000_dec OK 321094 1.9426425943521761e+00 1.9426425943521755e+00 2.61780 1.744e-15 sky_j2000_dist OK 302748 3.5388201408771312e+02 3.5388201408771533e+02 0.00275 6.086e-15 sky_hor_az OK 121119 -2.2454039277279009e+01 -2.2454039277279151e+01 0.00556 7.895e-16 sky_hor_alt FAIL 462886 3.2838700693197847e-05 3.2838701021405780e-05 354.60993 1.164e-10 jm_x FAIL 18898 4.1298198258838197e-05 4.1298197961707834e-05 354.60993 1.054e-10 jm_y FAIL 18898 6.5401658638156675e-05 6.5401658496812707e-05 354.60993 5.012e-11 jm_z FAIL 18898 -1.9369714715980919e-04 -1.9369714599049137e-04 100.00000 1.169e-10 jm_vx FAIL 462886 2.0741129523130840e-04 2.0741129628185288e-04 100.00000 1.051e-10 jm_vy FAIL 462886 -7.0200557604537683e-05 -7.0200557103467577e-05 100.00000 5.011e-11 jm_vz Score = 1.169e-10 ctest(Diff): EXCEEDED ERROR TOLERANCE. Restored the original loop logic and now it is back within tolerance: First file: temp/c_check.txt Second file: temp/k_check.txt Tolerance = 6.700e-15 lnum a_value b_value factor diff name OK 59558 -3.7992709399390907e-04 -3.7992709399390658e-04 411.52263 1.026e-15 helio_x OK 70088 -1.2672603312645981e-03 -1.2672603312646005e-03 411.52263 9.816e-16 helio_y OK 122819 1.0937502621531685e-04 1.0937502621531790e-04 411.52263 4.294e-16 helio_z OK 2427 2.0269124415104024e+01 2.0269124415104031e+01 0.01730 1.229e-16 sky_j2000_ra OK 96711 -1.3269907232295576e+01 -1.3269907232295552e+01 0.00556 1.382e-16 sky_j2000_dec OK 321094 1.9426425943521761e+00 1.9426425943521755e+00 2.61780 1.744e-15 sky_j2000_dist OK 302748 3.5388201408771312e+02 3.5388201408771533e+02 0.00275 6.086e-15 sky_hor_az OK 121119 -2.2454039277279009e+01 -2.2454039277279151e+01 0.00556 7.895e-16 sky_hor_alt OK 92717 4.1268347083494783e-03 4.1268347083494774e-03 223.21429 1.936e-16 jm_x OK 45091 -8.0149190392649894e-03 -8.0149190392649929e-03 79.42812 2.756e-16 jm_y OK 135377 1.5470777280065808e-03 1.5470777280065804e-03 223.21429 9.680e-17 jm_z OK 216836 4.5725777238332412e-03 4.5725777238332394e-03 126.58228 2.196e-16 jm_vx OK 351647 5.1351566793199944e-03 5.1351566793199962e-03 126.58228 2.196e-16 jm_vy OK 351647 2.5217607180929289e-03 2.5217607180929298e-03 126.58228 1.098e-16 jm_vz Score = 6.086e-15 --- generate/template/astronomy.kt | 11 ++++++++--- .../io/github/cosinekitty/astronomy/astronomy.kt | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/generate/template/astronomy.kt b/generate/template/astronomy.kt index 6ca31fb6..dbe55dbf 100644 --- a/generate/template/astronomy.kt +++ b/generate/template/astronomy.kt @@ -2747,7 +2747,9 @@ private fun vsopFormulaCalc(formula: VsopFormula, t: Double, clampAngle: Boolean var coord = 0.0 var tpower = 1.0 for (series in formula.series) { - val sum = series.term.sumOf { it.amplitude * cos(it.phase + (t * it.frequency)) } + var sum = 0.0 + for (term in series.term) + sum += term.amplitude * cos(term.phase + (t * term.frequency)) coord += if (clampAngle) (tpower * sum) % PI2 // improve precision: longitude angles can be hundreds of radians @@ -3425,10 +3427,13 @@ private fun calcJupiterMoon(time: Time, m: JupiterMoon): StateVector { val t = time.tt + 18262.5 // number of days since 1950-01-01T00:00:00Z // Calculate 6 orbital elements at the given time t. - val elem0 = m.a.sumOf { term -> term.amplitude * cos(term.phase + (t * term.frequency)) } + var elem0 = 0.0 + for (term in m.a) + elem0 += term.amplitude * cos(term.phase + (t * term.frequency)) var elem1 = m.al0 + (t * m.al1) - elem1 += m.l.sumOf { term -> term.amplitude * sin(term.phase + (t * term.frequency)) } + for (term in m.l) + elem1 += term.amplitude * sin(term.phase + (t * term.frequency)) elem1 %= PI2 if (elem1 < 0) diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt index 6001ad40..b52dbb32 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/astronomy.kt @@ -2747,7 +2747,9 @@ private fun vsopFormulaCalc(formula: VsopFormula, t: Double, clampAngle: Boolean var coord = 0.0 var tpower = 1.0 for (series in formula.series) { - val sum = series.term.sumOf { it.amplitude * cos(it.phase + (t * it.frequency)) } + var sum = 0.0 + for (term in series.term) + sum += term.amplitude * cos(term.phase + (t * term.frequency)) coord += if (clampAngle) (tpower * sum) % PI2 // improve precision: longitude angles can be hundreds of radians @@ -3425,10 +3427,13 @@ private fun calcJupiterMoon(time: Time, m: JupiterMoon): StateVector { val t = time.tt + 18262.5 // number of days since 1950-01-01T00:00:00Z // Calculate 6 orbital elements at the given time t. - val elem0 = m.a.sumOf { term -> term.amplitude * cos(term.phase + (t * term.frequency)) } + var elem0 = 0.0 + for (term in m.a) + elem0 += term.amplitude * cos(term.phase + (t * term.frequency)) var elem1 = m.al0 + (t * m.al1) - elem1 += m.l.sumOf { term -> term.amplitude * sin(term.phase + (t * term.frequency)) } + for (term in m.l) + elem1 += term.amplitude * sin(term.phase + (t * term.frequency)) elem1 %= PI2 if (elem1 < 0)