generated from JetBrains/intellij-platform-plugin-template
-
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
Peva Blanchard
committed
Jul 22, 2023
1 parent
b5e8077
commit a542e9a
Showing
7 changed files
with
95 additions
and
19 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DualNumberRenderer.kt
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,36 @@ | ||
package ch.kleis.lcaplugin.ui.toolwindow | ||
|
||
import ch.kleis.lcaplugin.core.lang.evaluator.ArgumentName | ||
import ch.kleis.lcaplugin.core.math.DualNumber | ||
import ch.kleis.lcaplugin.core.matrix.IndexedCollection | ||
import org.jetbrains.kotlinx.multik.ndarray.operations.toList | ||
import kotlin.math.abs | ||
|
||
class DualNumberRenderer( | ||
private val arguments: IndexedCollection<ArgumentName> | ||
) { | ||
private val absoluteSensitivityThreshold = 1e-10 | ||
private val relativeToMaxThreshold = 2e-2 | ||
private val dot = "\u00B7" | ||
private val delta = "δ" | ||
|
||
fun render(v: DualNumber): String { | ||
val z = "${DisplayedNumber(v.zeroth)}" | ||
val f = v.first | ||
.toList() | ||
.mapIndexed { i, d -> | ||
d to arguments[i].uid | ||
} | ||
val maxSensitivity = f.maxOf { abs(it.first) } | ||
val ff = f | ||
.filter { maxSensitivity > absoluteSensitivityThreshold && abs(it.first)/maxSensitivity > relativeToMaxThreshold } | ||
.fold("") { acc, p -> | ||
val d = p.first | ||
val absD = DisplayedNumber(abs(d)) | ||
val parameterName = p.second | ||
if (d >= 0.0) "$acc + $absD$dot$delta$parameterName" | ||
else "$acc - $absD$dot$delta$parameterName" | ||
} | ||
return z + ff | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/test/kotlin/ch/kleis/lcaplugin/ui/toolwindow/DualNumberRendererTest.kt
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,36 @@ | ||
package ch.kleis.lcaplugin.ui.toolwindow | ||
|
||
import ch.kleis.lcaplugin.core.lang.evaluator.ArgumentName | ||
import ch.kleis.lcaplugin.core.math.DualNumber | ||
import ch.kleis.lcaplugin.core.matrix.IndexedCollection | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
|
||
class DualNumberRendererTest { | ||
private val dx = DualNumber.basis(2, 0) | ||
private val dy = DualNumber.basis(2, 1) | ||
|
||
@Test | ||
fun test_render() { | ||
// given | ||
val arguments = IndexedCollection( | ||
listOf( | ||
"x", "y" | ||
).map { ArgumentName(it) } | ||
) | ||
val renderer = DualNumberRenderer(arguments) | ||
|
||
// when/then | ||
listOf( | ||
DualNumber.constant(123456.123) to "123E3", | ||
DualNumber.constant(123456.123) + dx to "123E3 + 1\u00B7δx", | ||
DualNumber.constant(123456.123) - dy to "123E3 - 1\u00B7δy", | ||
).forEach { | ||
val actual = renderer.render(it.first) | ||
assertEquals(it.second, actual) | ||
} | ||
} | ||
|
||
|
||
} |
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