Compare commits
No commits in common. "446c8d9a6ecf81486b6f22aec13d86780a8145ee" and "311cf6ee44b55ae1b2174ee9e59d354c1da67e14" have entirely different histories.
446c8d9a6e
...
311cf6ee44
@ -1,13 +1,28 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
Рассчитывает глубину провала по времени падения камня и прихода звука.
|
||||||
|
|
||||||
|
@param T измеренное полное время (с)
|
||||||
|
@param m масса камня (кг)
|
||||||
|
@param d диаметр камня (м) (предполагается сферическая форма)
|
||||||
|
@param rho плотность воздуха (кг/м³), по умолчанию 1.2
|
||||||
|
@param c скорость звука (м/с), по умолчанию 340.0
|
||||||
|
@param g ускорение свободного падения (м/с²), по умолчанию 9.81
|
||||||
|
@param Cd коэффициент лобового сопротивления, по умолчанию 0.5
|
||||||
|
@param epsilon относительная точность (м), по умолчанию 1e-3
|
||||||
|
@param maxIter максимальное число итераций, по умолчанию 20
|
||||||
|
@return глубина h (м), или null если расчёт не сошёлся
|
||||||
|
*/
|
||||||
fun calculateDepth(
|
fun calculateDepth(
|
||||||
T: Real,
|
T: Real,
|
||||||
m: Real,
|
m: Real,
|
||||||
d: Real,
|
d: Real,
|
||||||
rho: Real = 1.2,
|
rho: Real = 1.2,
|
||||||
c: Real = 340.0,
|
c: Real = 340.0,
|
||||||
g: Real = 9.81,
|
g: Real = 9.81,
|
||||||
Cd: Real = 0.5,
|
Cd: Real = 0.5,
|
||||||
eps: Real = 1e-3,
|
epsilon: Real = 1e-3,
|
||||||
maxIter: Int = 100
|
maxIter: Int = 20
|
||||||
): Real? {
|
): Real? {
|
||||||
// Площадь миделя
|
// Площадь миделя
|
||||||
val r = d / 2.0
|
val r = d / 2.0
|
||||||
@ -21,56 +36,69 @@ fun calculateDepth(
|
|||||||
|
|
||||||
// Функция времени падения с высоты h
|
// Функция времени падения с высоты h
|
||||||
fun tFall(h: Real): Real {
|
fun tFall(h: Real): Real {
|
||||||
// Для численной стабильности при больших h используем логарифмическую форму
|
|
||||||
val arg = exp(g * h / (vTerm * vTerm))
|
val arg = exp(g * h / (vTerm * vTerm))
|
||||||
// arcosh(x) = ln(x + sqrt(x^2 - 1))
|
// arcosh(x) = ln(x + sqrt(x^2 - 1))
|
||||||
val arcosh = ln(arg + sqrt(arg * arg - 1.0))
|
val arcosh = ln(arg + sqrt(arg * arg - 1.0))
|
||||||
return vTerm / g * arcosh
|
return vTerm / g * arcosh
|
||||||
}
|
}
|
||||||
|
|
||||||
// Полное расчётное время
|
// Производная времени падения по h
|
||||||
|
fun dtFall_dh(h: Real): Real {
|
||||||
|
val expArg = exp(2.0 * g * h / (vTerm * vTerm))
|
||||||
|
return 1.0 / (vTerm * sqrt(expArg - 1.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Полное расчётное время T_calc(h) = tFall(h) + h/c
|
||||||
fun Tcalc(h: Real): Real = tFall(h) + h / c
|
fun Tcalc(h: Real): Real = tFall(h) + h / c
|
||||||
|
|
||||||
// Находим интервал, содержащий корень
|
// Производная T_calc по h
|
||||||
// Нижняя граница: глубина не может быть отрицательной
|
fun dTcalc_dh(h: Real): Real = dtFall_dh(h) + 1.0 / c
|
||||||
var lo = 0.0
|
|
||||||
// Верхняя граница: сначала попробуем оценку по свободному падению (без звука)
|
|
||||||
var hi = 0.5 * g * T * T // максимальная глубина, если бы не было сопротивления и звука
|
|
||||||
// Уточним hi, чтобы Tcalc(hi) было заведомо больше T
|
|
||||||
while (Tcalc(hi) < T && hi < 1e4) {
|
|
||||||
hi *= 2.0
|
|
||||||
}
|
|
||||||
// Проверка, что hi достаточно велико
|
|
||||||
if (Tcalc(hi) < T) return null // слишком большая глубина, не укладываемся в разумное
|
|
||||||
|
|
||||||
// Бисекция
|
// Начальное приближение (без сопротивления)
|
||||||
|
val term = 1.0 + g * T / c
|
||||||
|
val sqrtTerm = sqrt(1.0 + 2.0 * g * T / c)
|
||||||
|
var h = (c * c / g) * (term - sqrtTerm)
|
||||||
|
|
||||||
|
// Проверка на валидность начального приближения
|
||||||
|
if (h.isNaN() || h <= 0.0) {
|
||||||
|
// Если формула дала некорректный результат, используем оценку по свободному падению
|
||||||
|
h = 0.5 * g * T * T // грубая оценка, всё равно будет уточняться
|
||||||
|
if (h.isNaN() || h <= 0.0) return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Итерации Ньютона
|
||||||
var iter = 0
|
var iter = 0
|
||||||
var h = (lo + hi) / 2.0
|
while (iter < maxIter) {
|
||||||
while (iter < maxIter && (hi - lo) > eps) {
|
|
||||||
val f = Tcalc(h) - T
|
val f = Tcalc(h) - T
|
||||||
if (abs(f) < eps) break
|
val df = dTcalc_dh(h)
|
||||||
if (f > 0) {
|
|
||||||
hi = h
|
// Если производная близка к нулю, выходим
|
||||||
} else {
|
if (abs(df) < 1e-12) return null
|
||||||
lo = h
|
|
||||||
|
val hNew = h - f / df
|
||||||
|
|
||||||
|
// Проверка сходимости
|
||||||
|
if (abs(hNew - h) < epsilon) {
|
||||||
|
return hNew
|
||||||
}
|
}
|
||||||
h = (lo + hi) / 2.0
|
|
||||||
|
h = hNew
|
||||||
iter++
|
iter++
|
||||||
|
println("iter: $iter: $h")
|
||||||
}
|
}
|
||||||
|
|
||||||
return h
|
// Не сошлось за maxIter
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Пример: T=12 секунд
|
// Пример использования
|
||||||
val T = 26.0
|
val T = 6.0 // секунды
|
||||||
val m = 1.0 // кг
|
val m = 1.0 // кг
|
||||||
val d = 0.1 // м
|
val d = 0.1 // м (10 см)
|
||||||
|
|
||||||
val depth = calculateDepth(T, m, d)
|
val depth = calculateDepth(T, m, d)
|
||||||
if (depth != null) {
|
if (depth != null) {
|
||||||
println("Глубина: %.2f м"(depth))
|
println("Глубина: %.2f м"(depth))
|
||||||
// Для проверки выведем теоретическое время при найденной глубине
|
} else {
|
||||||
// (можно добавить функцию для самопроверки)
|
println("Расчёт не сошёлся")
|
||||||
} else {
|
}
|
||||||
println("Расчёт не сошёлся")
|
|
||||||
}
|
|
||||||
|
|||||||
@ -436,12 +436,6 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun rememberModuleReferencePos(name: String, pos: Pos) {
|
|
||||||
if (!moduleReferencePosByName.containsKey(name)) {
|
|
||||||
moduleReferencePosByName[name] = pos
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun predeclareClassMembers(target: MutableSet<String>, overrides: MutableMap<String, Boolean>) {
|
private fun predeclareClassMembers(target: MutableSet<String>, overrides: MutableMap<String, Boolean>) {
|
||||||
val saved = cc.savePos()
|
val saved = cc.savePos()
|
||||||
var depth = 0
|
var depth = 0
|
||||||
@ -954,7 +948,7 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
captureLocalRef(name, slotLoc, pos)?.let { ref ->
|
captureLocalRef(name, slotLoc, pos)?.let { ref ->
|
||||||
rememberModuleReferencePos(name, pos)
|
moduleReferencePosByName.putIfAbsent(name, pos)
|
||||||
resolutionSink?.reference(name, pos)
|
resolutionSink?.reference(name, pos)
|
||||||
return ref
|
return ref
|
||||||
}
|
}
|
||||||
@ -1048,7 +1042,7 @@ class Compiler(
|
|||||||
moduleEntry.isDelegated
|
moduleEntry.isDelegated
|
||||||
)
|
)
|
||||||
captureLocalRef(name, moduleLoc, pos)?.let { ref ->
|
captureLocalRef(name, moduleLoc, pos)?.let { ref ->
|
||||||
rememberModuleReferencePos(name, pos)
|
moduleReferencePosByName.putIfAbsent(name, pos)
|
||||||
resolutionSink?.reference(name, pos)
|
resolutionSink?.reference(name, pos)
|
||||||
return ref
|
return ref
|
||||||
}
|
}
|
||||||
@ -1075,7 +1069,7 @@ class Compiler(
|
|||||||
strictSlotRefs
|
strictSlotRefs
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
rememberModuleReferencePos(name, pos)
|
moduleReferencePosByName.putIfAbsent(name, pos)
|
||||||
resolutionSink?.reference(name, pos)
|
resolutionSink?.reference(name, pos)
|
||||||
return ref
|
return ref
|
||||||
}
|
}
|
||||||
@ -1106,14 +1100,14 @@ class Compiler(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
registerImportBinding(name, resolved.binding, pos)
|
registerImportBinding(name, resolved.binding, pos)
|
||||||
val slot = lookupSlotLocation(name)
|
val slot = lookupSlotLocation(name)
|
||||||
if (slot != null) {
|
if (slot != null) {
|
||||||
captureLocalRef(name, slot, pos)?.let { ref ->
|
captureLocalRef(name, slot, pos)?.let { ref ->
|
||||||
rememberModuleReferencePos(name, pos)
|
moduleReferencePosByName.putIfAbsent(name, pos)
|
||||||
resolutionSink?.reference(name, pos)
|
resolutionSink?.reference(name, pos)
|
||||||
return ref
|
return ref
|
||||||
}
|
}
|
||||||
val ref = if (!useScopeSlots && capturePlanStack.isEmpty() && slot.depth > 0) {
|
val ref = if (!useScopeSlots && capturePlanStack.isEmpty() && slot.depth > 0) {
|
||||||
LocalSlotRef(
|
LocalSlotRef(
|
||||||
name,
|
name,
|
||||||
@ -1137,7 +1131,7 @@ class Compiler(
|
|||||||
strictSlotRefs
|
strictSlotRefs
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
rememberModuleReferencePos(name, pos)
|
moduleReferencePosByName.putIfAbsent(name, pos)
|
||||||
resolutionSink?.reference(name, pos)
|
resolutionSink?.reference(name, pos)
|
||||||
return ref
|
return ref
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7664,9 +7664,7 @@ class BytecodeCompiler(
|
|||||||
private fun noteScopeSlotRef(slot: Int, pos: Pos) {
|
private fun noteScopeSlotRef(slot: Int, pos: Pos) {
|
||||||
if (slot >= scopeSlotCount) return
|
if (slot >= scopeSlotCount) return
|
||||||
val key = scopeKeyByIndex.getOrNull(slot) ?: return
|
val key = scopeKeyByIndex.getOrNull(slot) ?: return
|
||||||
if (!scopeSlotRefPosByKey.containsKey(key)) {
|
scopeSlotRefPosByKey.putIfAbsent(key, pos)
|
||||||
scopeSlotRefPosByKey[key] = pos
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resolveSlot(ref: LocalSlotRef): Int? {
|
private fun resolveSlot(ref: LocalSlotRef): Int? {
|
||||||
@ -7677,15 +7675,11 @@ class BytecodeCompiler(
|
|||||||
val localIndex = localSlotIndexByKey[key]
|
val localIndex = localSlotIndexByKey[key]
|
||||||
if (localIndex != null) return scopeSlotCount + localIndex
|
if (localIndex != null) return scopeSlotCount + localIndex
|
||||||
scopeSlotMap[key]?.let {
|
scopeSlotMap[key]?.let {
|
||||||
if (!scopeSlotRefPosByKey.containsKey(key)) {
|
scopeSlotRefPosByKey.putIfAbsent(key, ref.pos())
|
||||||
scopeSlotRefPosByKey[key] = ref.pos()
|
|
||||||
}
|
|
||||||
return it
|
return it
|
||||||
}
|
}
|
||||||
scopeSlotIndexByName[ref.name]?.let {
|
scopeSlotIndexByName[ref.name]?.let {
|
||||||
if (!scopeSlotRefPosByKey.containsKey(key)) {
|
scopeSlotRefPosByKey.putIfAbsent(key, ref.pos())
|
||||||
scopeSlotRefPosByKey[key] = ref.pos()
|
|
||||||
}
|
|
||||||
return it
|
return it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -7699,9 +7693,7 @@ class BytecodeCompiler(
|
|||||||
}
|
}
|
||||||
val resolved = scopeSlotMap[scopeKey]
|
val resolved = scopeSlotMap[scopeKey]
|
||||||
if (resolved != null) {
|
if (resolved != null) {
|
||||||
if (!scopeSlotRefPosByKey.containsKey(scopeKey)) {
|
scopeSlotRefPosByKey.putIfAbsent(scopeKey, ref.pos())
|
||||||
scopeSlotRefPosByKey[scopeKey] = ref.pos()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
@ -7716,9 +7708,7 @@ class BytecodeCompiler(
|
|||||||
val scopeKey = ScopeSlotKey(refScopeId(ref), refSlot(ref))
|
val scopeKey = ScopeSlotKey(refScopeId(ref), refSlot(ref))
|
||||||
val resolved = scopeSlotMap[scopeKey]
|
val resolved = scopeSlotMap[scopeKey]
|
||||||
if (resolved != null) {
|
if (resolved != null) {
|
||||||
if (!scopeSlotRefPosByKey.containsKey(scopeKey)) {
|
scopeSlotRefPosByKey.putIfAbsent(scopeKey, ref.pos())
|
||||||
scopeSlotRefPosByKey[scopeKey] = ref.pos()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
|
|||||||
@ -284,31 +284,6 @@ class DecimalModuleTest {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testDecimalTruncateToTwoFractionDigitsViaGlobalRound() = runTest {
|
|
||||||
val scope = Script.newScope()
|
|
||||||
scope.eval(
|
|
||||||
"""
|
|
||||||
import lyng.decimal
|
|
||||||
|
|
||||||
fun trunc2(x: Decimal): Decimal {
|
|
||||||
val scaled = x * 100.d
|
|
||||||
val whole = if (scaled >= 0.d) {
|
|
||||||
floor(scaled) as Decimal
|
|
||||||
} else {
|
|
||||||
ceil(scaled) as Decimal
|
|
||||||
}
|
|
||||||
whole / 100.d
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("12.34", trunc2("12.349".d).toStringExpanded())
|
|
||||||
assertEquals("12.34", trunc2("12.340".d).toStringExpanded())
|
|
||||||
assertEquals("-12.34", trunc2("-12.349".d).toStringExpanded())
|
|
||||||
assertEquals("-12.34", trunc2("-12.340".d).toStringExpanded())
|
|
||||||
""".trimIndent()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testDecimalMathHelpersFallbackThroughRealTemporarily() = runTest {
|
fun testDecimalMathHelpersFallbackThroughRealTemporarily() = runTest {
|
||||||
val scope = Script.newScope()
|
val scope = Script.newScope()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user