<divclass="platform-hinted UnderCoverText"data-platform-hinted="data-platform-hinted"><divclass="content sourceset-depenent-content"data-active=""data-togglable=":kmath-ast:dokkaHtmlPartial/commonMain"><pclass="paragraph">Performance and visualization extensions to MST API.</p><ul><li><pclass="paragraph">src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt : Expression language and its parser</p></li><li><pclass="paragraph">src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt : Dynamic MST to JVM bytecode compiler</p></li><li><pclass="paragraph">src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt : Dynamic MST to JS compiler</p></li><li><pclass="paragraph">src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt : Extendable MST rendering</p></li></ul><h2class=""> Artifact:</h2><pclass="paragraph">The Maven coordinates of this project are <code>space.kscience:kmath-ast:0.3.0-dev-16</code>.</p><pclass="paragraph">Gradle:</p><divclass="sample-container"><codeclass=""theme="idea"><pre>repositories {<br> maven { url 'https://repo.kotlin.link' }<br> mavenCentral()<br>}<br><br>dependencies {<br> implementation 'space.kscience:kmath-ast:0.3.0-dev-16'<br>}</pre></code></div><pclass="paragraph">Gradle Kotlin DSL:</p><divclass="sample-container"><codeclass=""theme="idea"><pre>repositories {<br> maven("https://repo.kotlin.link")<br> mavenCentral()<br>}<br><br>dependencies {<br> implementation("space.kscience:kmath-ast:0.3.0-dev-16")<br>}</pre></code></div><h2class=""> Dynamic expression code generation</h2><h3class=""> On JVM</h3><pclass="paragraph"><code>kmath-ast</code> JVM module supports runtime code generation to eliminate overhead of tree traversal. Code generator builds a special implementation of <code>Expression<T></code> with implemented <code>invoke</code> function.</p><pclass="paragraph">For example, the following builder:</p><divclass="sample-container"><codeclass=""theme="idea"><pre>import space.kscience.kmath.expressions.Symbol.Companion.x<br>import space.kscience.kmath.expressions.*<br>import space.kscience.kmath.operations.*<br>import space.kscience.kmath.asm.*<br><br>MstField { x + 2 }.compileToExpression(DoubleField)</pre></code></div><pclass="paragraph">... leads to generation of bytecode, which can be decompiled to the following Java class:</p><divclass="sample-container"><codeclass=""theme="idea"><pre>package space.kscience.kmath.asm.generated;<br><br>import java.util.Map;<br><br>import kotlin.jvm.functions.Function2;<br>import space.kscience.kmath.asm.internal.MapIntrinsics;<br>import space.kscience.kmath.expressions.Expression;<br>import space.kscience.kmath.expressions.Symbol;<br><br>public final class AsmCompiledExpression_45045_0 implements Expression<Double> {<br> private final Object[] constants;<br><br> public final Double invoke(Map<Symbol, ? extends Double> arguments) {<br> return (Double) ((Function2) this.constants[0]).invoke((Double) MapIntrinsics.getOrFail(arguments, "x"), 2);<br> }<br><br> public AsmCompiledExpression_45045_0(Object[] constants) {<br> this.constants = constants;<br> }<br>}</pre></code></div><h4class=""> Known issues</h4><ul><li><pclass="paragraph">The same classes may be generated and loaded twice, so it is recommended to cache compiled expressions to avoid class loading overhead.</p></li><li><pclass="paragraph">This API is not supported by non-dynamic JVM implementations (like TeaVM and GraalVM) because of using class loaders.</p></li></ul><h3class=""> On JS</h3><pclass="paragraph">A similar feature is also available on JS.</p><divclass="sample-container"><codeclass=""theme="idea"><pre>import space.kscience.kmath.expressions.Symbol.Companion.x<br>import space.kscience.kmath.expressions.*<br>import space.kscience.kmath.operations.*<br>import space.kscience.kmath.estree.*<br><br>MstField { x + 2 }.compileToExpression(DoubleField)</pre></code></div><pclass="paragraph">The code above returns expression implemented with such a JS function:</p>