[enh] change trap/reartrap interpolation to bilinear (for speed)
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/// generated by https://gemini.google.com/app/7627cdc6341c5973 (вроде правильно)
|
||||
package ru.inr.mass.scripts
|
||||
|
||||
class BilinearInterpolator(
|
||||
private val x: DoubleArray,
|
||||
private val y: DoubleArray,
|
||||
private val value: Array<DoubleArray>
|
||||
) {
|
||||
|
||||
private val xSize: Int = x.size
|
||||
private val ySize: Int = y.size
|
||||
|
||||
init {
|
||||
if (value.size != xSize) {
|
||||
throw IllegalArgumentException("Размерность value по x должна быть равна размерности x")
|
||||
}
|
||||
if (value[0].size != ySize) {
|
||||
throw IllegalArgumentException("Размерность value по y должна быть равна размерности y")
|
||||
}
|
||||
if (xSize < 2 || ySize < 2) {
|
||||
throw IllegalArgumentException("Размерность x и y должна быть как минимум 2 для билинейной интерполяции")
|
||||
}
|
||||
}
|
||||
|
||||
fun value(xQuery: Double, yQuery: Double): Double {
|
||||
if (xQuery < x.first() || xQuery > x.last() || yQuery < y.first() || yQuery > y.last()) {
|
||||
throw IllegalArgumentException("Точка запроса находится вне границ сетки")
|
||||
}
|
||||
|
||||
var xIndex = -1
|
||||
for (i in 0 until xSize - 1) {
|
||||
if (xQuery >= x[i] && xQuery <= x[i + 1]) {
|
||||
xIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if (xIndex == -1) xIndex = xSize - 2 // На случай, если xQuery == x.last()
|
||||
|
||||
var yIndex = -1
|
||||
for (i in 0 until ySize - 1) {
|
||||
if (yQuery >= y[i] && yQuery <= y[i + 1]) {
|
||||
yIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if (yIndex == -1) yIndex = ySize - 2 // На случай, если yQuery == y.last()
|
||||
|
||||
|
||||
val xWeight = (xQuery - x[xIndex]) / (x[xIndex + 1] - x[xIndex])
|
||||
val yWeight = (yQuery - y[yIndex]) / (y[yIndex + 1] - y[yIndex])
|
||||
|
||||
val v00 = value[xIndex][yIndex]
|
||||
val v10 = value[xIndex + 1][yIndex]
|
||||
val v01 = value[xIndex][yIndex + 1]
|
||||
val v11 = value[xIndex + 1][yIndex + 1]
|
||||
|
||||
return (1 - xWeight) * (1 - yWeight) * v00 +
|
||||
xWeight * (1 - yWeight) * v10 +
|
||||
(1 - xWeight) * yWeight * v01 +
|
||||
xWeight * yWeight * v11
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,8 @@ class RearTrapInterpolator {
|
||||
0.0005479427438980562, 0.0005479427438980562, 0.0004632291784324396, 0.00041283744236910466, 0.0003757945105609578, 0.00035376898353989755, 0.0003343105342602195, 0.0003201916066826168, 0.00030807499974329225, 0.0002923131424112048, 0.0002849456292934375, 0.00027234127758325033, 0.00027123743415445595, 0.0002654358384589319, 0.00025855607011202725, 0.00025011038434287947, 0.0002467475125016686, 0.00024341031143787156, 0.0002383274975099346, 0.00023488761333648227, 0.00023740334952303697, 0.00022924004230544118, 0.00022456796081612537, 0.00022931705463768263, 0.00022189819996508772, 0.0002218468584102601, 0.000220409294875086, 0.00021219464610266258, 0.00021902307289473953, 0.00021571154260835635, 0.00021450501606990667, 0.0002095762268064526, 0.0002132214771992155, 0.00020975592224834936, 0.00020654707507162146, 0.00020824134638093382, 0.00020341524022713504, 0.00020521219464610265, 0.00020113054103730477, 0.00020292749545627238, 0.00019941059895057863, 0.0001968435212091963, 0.0002035949356690318, 0.00019512357912247015, 0.00020338956944972124, 0.00019876882951523305, 0.00019992401449885508, 0.0001958423608900572, 0.00019496955445798722, 0.00019566266544816043, 0.000195868031667471, 0.00019535461611919455, 0.00019550864078367747, 0.0001990512080667851, 0.0001990512080667851, 0.00019170936572643167, 0.0001937373571421237, 0.00019450748046453837, 0.00019740827831230042, 0.00019702321665109306, 0.00019014344830418843, 0.00019476418823867662, 0.00019740827831230042, 0.0001972029120929898, 0.00019753663219936954, 0.00019571400700298808, 0.00019481552979350427, 0.00019876882951523305, 0.00019522626223212542, 0.00019933358661833716, 0.0002011562118147186, 0.00019712589976074836, 0.0002039286557754115, 0.00019812706007988747, 0.0002015926150307536, 0.00019869181718299158, 0.0001990768788441989, 0.00020231139679834065, 0.00020174663969523655, 0.0002046987790978262, 0.00020264511690472033, 0.0002039029849979977, 0.00020346658178196269, 0.00020297883701110003, 0.00020462176676558472, 0.00020503249920420592, 0.00020187499358230565, 0.00020939653136455583, 0.0002041340219947221, 0.00020811299249386467,
|
||||
).toDoubleArray(),
|
||||
)
|
||||
private val coarseInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yCoarse, gridCoarse)
|
||||
// private val coarseInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yCoarse, gridCoarse)
|
||||
private val coarseInterpolator = BilinearInterpolator(x, yCoarse, gridCoarse)
|
||||
|
||||
private val yFine = arrayOf(
|
||||
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0,
|
||||
@@ -55,7 +56,8 @@ class RearTrapInterpolator {
|
||||
).toDoubleArray(),
|
||||
)
|
||||
|
||||
private val fineInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yFine, gridFine)
|
||||
// private val fineInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yFine, gridFine)
|
||||
private val fineInterpolator = BilinearInterpolator(x, yFine, gridFine)
|
||||
|
||||
fun value(ei: Double, delta: Double): Double {
|
||||
if (ei < x.first() || ei > x.last())
|
||||
|
||||
@@ -29,7 +29,8 @@ class TrapInterpolator {
|
||||
2.922372662620021e-5, 2.922372662620021e-5, 2.6591518625116932e-5, 2.485743933831403e-5, 2.3613876503156577e-5, 2.228740947898862e-5, 2.2052514276792218e-5, 2.139618944712578e-5, 2.110602478558904e-5, 2.12994678932802e-5, 2.0048996375705204e-5, 2.0048996375705204e-5, 2.007663110537537e-5, 1.9779557761421086e-5, 1.9579205971312387e-5, 1.962756674823518e-5, 1.9959183504277166e-5, 1.964138411307026e-5, 1.963447543065272e-5, 1.989009668010175e-5, 1.9765740396586005e-5, 1.9482484417466807e-5, 2.0069722422957827e-5, 1.989009668010175e-5, 2.0691503840536555e-5, 2.0988577184490835e-5, 2.1803801709760723e-5, 2.1175111609764453e-5, 2.1271833163610033e-5, 2.1720897520750227e-5, 2.147909363613628e-5, 2.1900523263606304e-5, 2.2114692418550087e-5, 2.2812469342721776e-5, 2.2674295694370943e-5, 2.2584482822942904e-5, 2.3261533699861966e-5, 2.3869497752605607e-5, 2.339279866579525e-5, 2.3890223799858233e-5, 2.384186302293544e-5, 2.3959310624033648e-5, 2.4829804608643864e-5, 2.4159662414142348e-5, 2.5078517175675355e-5, 2.4284018697658093e-5, 2.4974886939412236e-5, 2.5458494708640138e-5, 2.6183906362481985e-5, 2.5403225249299807e-5, 2.614936295039428e-5, 2.7206391360278118e-5, 2.6888591969071212e-5, 2.609409349105395e-5, 2.769690781192356e-5, 2.6591518625116932e-5, 2.7731451224011263e-5, 2.735838237346403e-5, 2.814597216906375e-5, 2.860885389103902e-5, 2.819433294598654e-5, 2.7987072473460296e-5, 2.8981922741586264e-5, 2.8961196694333638e-5, 2.8788479633895104e-5, 2.9741877807515815e-5, 2.956916074707728e-5, 3.017712479982093e-5, 3.056401101520325e-5, 3.0384385272347167e-5, 3.1607222060252e-5, 3.0633097839378665e-5, 3.136541817563805e-5, 3.138614422289068e-5, 3.1475957094318715e-5, 3.0474198143775208e-5, 3.1876660674536114e-5, 3.228427293717106e-5, 3.307186273277078e-5, 3.41427085074897e-5, 3.3465657630570645e-5, 3.4308516885510695e-5, 3.359001391408639e-5, 3.370746151518459e-5, 3.376273097452492e-5, 3.4280882155840527e-5, 3.522046296462616e-5, 3.535863661297699e-5, 3.5683344686601436e-5, 3.555207972066815e-5,
|
||||
).toDoubleArray(),
|
||||
)
|
||||
private val coarseInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yCoarse, gridCoarse)
|
||||
// private val coarseInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yCoarse, gridCoarse)
|
||||
private val coarseInterpolator = BilinearInterpolator(x, yCoarse, gridCoarse)
|
||||
|
||||
private val yFine = arrayOf(
|
||||
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0,
|
||||
@@ -55,7 +56,8 @@ class TrapInterpolator {
|
||||
).toDoubleArray(),
|
||||
)
|
||||
|
||||
private val fineInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yFine, gridFine)
|
||||
// private val fineInterpolator = PiecewiseBicubicSplineInterpolator().interpolate(x, yFine, gridFine)
|
||||
private val fineInterpolator = BilinearInterpolator(x, yFine, gridFine)
|
||||
|
||||
fun value(ei: Double, delta: Double): Double {
|
||||
if (ei < x.first() || ei > x.last())
|
||||
|
||||
Reference in New Issue
Block a user