{
A similar feature is also available on JS.
```kotlin
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.*
import space.kscience.kmath.operations.*
import space.kscience.kmath.estree.*
-MstField { bindSymbol("x") + 2 }.compileToExpression(DoubleField)
+MstField { x + 2 }.compileToExpression(DoubleField)
```
The code above returns expression implemented with such a JS function:
@@ -104,15 +106,16 @@ var executable = function (constants, arguments) {
};
```
-JS also supports very experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation.
+JS also supports experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation.
Currently, only expressions inside `DoubleField` and `IntRing` are supported.
```kotlin
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.*
import space.kscience.kmath.operations.*
import space.kscience.kmath.wasm.*
-MstField { bindSymbol("x") + 2 }.compileToExpression(DoubleField)
+MstField { x + 2 }.compileToExpression(DoubleField)
```
An example of emitted Wasm IR in the form of WAT:
@@ -158,7 +161,10 @@ public fun main() {
Result LaTeX:
+
+
![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3})
+
Result MathML (can be used with MathJax or other renderers):
diff --git a/kmath-ast/build.gradle.kts b/kmath-ast/build.gradle.kts
index 508374d82..9de7e9980 100644
--- a/kmath-ast/build.gradle.kts
+++ b/kmath-ast/build.gradle.kts
@@ -37,17 +37,15 @@ kotlin.sourceSets {
jsMain {
dependencies {
- implementation(npm("astring", "1.7.4"))
- implementation(npm("binaryen", "100.0"))
- implementation(npm("js-base64", "3.6.0"))
- implementation(npm("webassembly", "0.11.0"))
+ implementation(npm("astring", "1.7.5"))
+ implementation(npm("binaryen", "101.0.0"))
+ implementation(npm("js-base64", "3.6.1"))
}
}
jvmMain {
dependencies {
- implementation("org.ow2.asm:asm:9.1")
- implementation("org.ow2.asm:asm-commons:9.1")
+ implementation("org.ow2.asm:asm-commons:9.2")
}
}
}
@@ -63,25 +61,21 @@ readme {
feature(
id = "expression-language",
- description = "Expression language and its parser",
ref = "src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt"
- )
+ ) { "Expression language and its parser" }
feature(
id = "mst-jvm-codegen",
- description = "Dynamic MST to JVM bytecode compiler",
ref = "src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt"
- )
+ ) { "Dynamic MST to JVM bytecode compiler" }
feature(
id = "mst-js-codegen",
- description = "Dynamic MST to JS compiler",
ref = "src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt"
- )
+ ) { "Dynamic MST to JS compiler" }
feature(
id = "rendering",
- description = "Extendable MST rendering",
ref = "src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt"
- )
+ ) { "Extendable MST rendering" }
}
diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md
index 80ea31642..9494af63a 100644
--- a/kmath-ast/docs/README-TEMPLATE.md
+++ b/kmath-ast/docs/README-TEMPLATE.md
@@ -16,11 +16,12 @@ special implementation of `Expression` with implemented `invoke` function.
For example, the following builder:
```kotlin
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.*
import space.kscience.kmath.operations.*
import space.kscience.kmath.asm.*
-MstField { bindSymbol("x") + 2 }.compileToExpression(DoubleField)
+MstField { x + 2 }.compileToExpression(DoubleField)
```
... leads to generation of bytecode, which can be decompiled to the following Java class:
@@ -60,11 +61,12 @@ public final class AsmCompiledExpression_45045_0 implements Expression {
A similar feature is also available on JS.
```kotlin
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.*
import space.kscience.kmath.operations.*
import space.kscience.kmath.estree.*
-MstField { bindSymbol("x") + 2 }.compileToExpression(DoubleField)
+MstField { x + 2 }.compileToExpression(DoubleField)
```
The code above returns expression implemented with such a JS function:
@@ -75,15 +77,16 @@ var executable = function (constants, arguments) {
};
```
-JS also supports very experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation.
+JS also supports experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation.
Currently, only expressions inside `DoubleField` and `IntRing` are supported.
```kotlin
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.*
import space.kscience.kmath.operations.*
import space.kscience.kmath.wasm.*
-MstField { bindSymbol("x") + 2 }.compileToExpression(DoubleField)
+MstField { x + 2 }.compileToExpression(DoubleField)
```
An example of emitted Wasm IR in the form of WAT:
@@ -129,7 +132,10 @@ public fun main() {
Result LaTeX:
+
+
![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3})
+
Result MathML (can be used with MathJax or other renderers):
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt
index 246625d29..ef6d51c7b 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
@@ -17,6 +17,7 @@ import com.github.h0tk3y.betterParse.lexer.regexToken
import com.github.h0tk3y.betterParse.parser.ParseResult
import com.github.h0tk3y.betterParse.parser.Parser
import space.kscience.kmath.expressions.MST
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.operations.FieldOperations
import space.kscience.kmath.operations.GroupOperations
import space.kscience.kmath.operations.PowerOperations
@@ -42,7 +43,7 @@ public object ArithmeticsEvaluator : Grammar() {
private val ws: Token by regexToken("\\s+".toRegex(), ignore = true)
private val number: Parser by num use { MST.Numeric(text.toDouble()) }
- private val singular: Parser by id use { MST.Symbolic(text) }
+ private val singular: Parser by id use { Symbol(text) }
private val unaryFunction: Parser by (id and -lpar and parser(ArithmeticsEvaluator::subSumChain) and -rpar)
.map { (id, term) -> MST.Unary(id.text, term) }
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt
index 01717b0f9..bf5916fa5 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
@@ -27,7 +27,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI
*/
@UnstableKMathAPI
public object LatexSyntaxRenderer : SyntaxRenderer {
- public override fun render(node: MathSyntax, output: Appendable): Unit = output.run {
+ override fun render(node: MathSyntax, output: Appendable): Unit = output.run {
fun render(syntax: MathSyntax) = render(syntax, output)
when (node) {
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt
index cda8e2322..5439c42fa 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
@@ -16,7 +16,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI
*/
@UnstableKMathAPI
public object MathMLSyntaxRenderer : SyntaxRenderer {
- public override fun render(node: MathSyntax, output: Appendable) {
+ override fun render(node: MathSyntax, output: Appendable) {
output.append("")
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt
index c33f95483..24bac425a 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
@@ -29,7 +29,7 @@ public fun interface MathRenderer {
*/
@UnstableKMathAPI
public open class FeaturedMathRenderer(public val features: List) : MathRenderer {
- public override fun render(mst: MST): MathSyntax {
+ override fun render(mst: MST): MathSyntax {
for (feature in features) feature.render(this, mst)?.let { return it }
throw UnsupportedOperationException("Renderer $this has no appropriate feature to render node $mst.")
}
@@ -54,9 +54,9 @@ public open class FeaturedMathRenderer(public val features: List)
@UnstableKMathAPI
public open class FeaturedMathRendererWithPostProcess(
features: List,
- public val stages: List,
+ public val stages: List,
) : FeaturedMathRenderer(features) {
- public override fun render(mst: MST): MathSyntax {
+ override fun render(mst: MST): MathSyntax {
val res = super.render(mst)
for (stage in stages) stage.perform(res)
return res
@@ -65,7 +65,7 @@ public open class FeaturedMathRendererWithPostProcess(
/**
* Logical unit of [MathSyntax] post-processing.
*/
- public fun interface PostProcessStage {
+ public fun interface PostProcessPhase {
/**
* Performs the specified action over [MathSyntax].
*/
@@ -102,7 +102,7 @@ public open class FeaturedMathRendererWithPostProcess(
// Printing terminal nodes as string
PrintNumeric,
- PrintSymbolic,
+ PrintSymbol,
),
listOf(
BetterExponent,
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt
index a71985fbc..81b7d2afb 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
@@ -8,7 +8,7 @@ package space.kscience.kmath.ast.rendering
import space.kscience.kmath.misc.UnstableKMathAPI
/**
- * Mathematical typography syntax node.
+ * Syntax node for mathematical typography.
*
* @author Iaroslav Postovalov
*/
@@ -150,9 +150,9 @@ public data class OperandSyntax(
*/
@UnstableKMathAPI
public data class UnaryOperatorSyntax(
- public override val operation: String,
+ override val operation: String,
public var prefix: MathSyntax,
- public override val operand: OperandSyntax,
+ override val operand: OperandSyntax,
) : UnarySyntax() {
init {
operand.parent = this
@@ -166,8 +166,8 @@ public data class UnaryOperatorSyntax(
*/
@UnstableKMathAPI
public data class UnaryPlusSyntax(
- public override val operation: String,
- public override val operand: OperandSyntax,
+ override val operation: String,
+ override val operand: OperandSyntax,
) : UnarySyntax() {
init {
operand.parent = this
@@ -181,8 +181,8 @@ public data class UnaryPlusSyntax(
*/
@UnstableKMathAPI
public data class UnaryMinusSyntax(
- public override val operation: String,
- public override val operand: OperandSyntax,
+ override val operation: String,
+ override val operand: OperandSyntax,
) : UnarySyntax() {
init {
operand.parent = this
@@ -197,8 +197,8 @@ public data class UnaryMinusSyntax(
*/
@UnstableKMathAPI
public data class RadicalSyntax(
- public override val operation: String,
- public override val operand: MathSyntax,
+ override val operation: String,
+ override val operand: MathSyntax,
) : UnarySyntax() {
init {
operand.parent = this
@@ -215,8 +215,8 @@ public data class RadicalSyntax(
*/
@UnstableKMathAPI
public data class ExponentSyntax(
- public override val operation: String,
- public override val operand: OperandSyntax,
+ override val operation: String,
+ override val operand: OperandSyntax,
public var useOperatorForm: Boolean,
) : UnarySyntax() {
init {
@@ -233,9 +233,9 @@ public data class ExponentSyntax(
*/
@UnstableKMathAPI
public data class SuperscriptSyntax(
- public override val operation: String,
- public override val left: MathSyntax,
- public override val right: MathSyntax,
+ override val operation: String,
+ override val left: MathSyntax,
+ override val right: MathSyntax,
) : BinarySyntax() {
init {
left.parent = this
@@ -252,9 +252,9 @@ public data class SuperscriptSyntax(
*/
@UnstableKMathAPI
public data class SubscriptSyntax(
- public override val operation: String,
- public override val left: MathSyntax,
- public override val right: MathSyntax,
+ override val operation: String,
+ override val left: MathSyntax,
+ override val right: MathSyntax,
) : BinarySyntax() {
init {
left.parent = this
@@ -270,10 +270,10 @@ public data class SubscriptSyntax(
*/
@UnstableKMathAPI
public data class BinaryOperatorSyntax(
- public override val operation: String,
+ override val operation: String,
public var prefix: MathSyntax,
- public override val left: MathSyntax,
- public override val right: MathSyntax,
+ override val left: MathSyntax,
+ override val right: MathSyntax,
) : BinarySyntax() {
init {
left.parent = this
@@ -290,9 +290,9 @@ public data class BinaryOperatorSyntax(
*/
@UnstableKMathAPI
public data class BinaryPlusSyntax(
- public override val operation: String,
- public override val left: OperandSyntax,
- public override val right: OperandSyntax,
+ override val operation: String,
+ override val left: OperandSyntax,
+ override val right: OperandSyntax,
) : BinarySyntax() {
init {
left.parent = this
@@ -301,7 +301,7 @@ public data class BinaryPlusSyntax(
}
/**
- * Represents binary, infix subtraction (*42 - 42*).
+ * Represents binary, infix subtraction (*42 − 42*).
*
* @param left The minuend.
* @param right The subtrahend.
@@ -309,9 +309,9 @@ public data class BinaryPlusSyntax(
*/
@UnstableKMathAPI
public data class BinaryMinusSyntax(
- public override val operation: String,
- public override val left: OperandSyntax,
- public override val right: OperandSyntax,
+ override val operation: String,
+ override val left: OperandSyntax,
+ override val right: OperandSyntax,
) : BinarySyntax() {
init {
left.parent = this
@@ -329,9 +329,9 @@ public data class BinaryMinusSyntax(
*/
@UnstableKMathAPI
public data class FractionSyntax(
- public override val operation: String,
- public override val left: OperandSyntax,
- public override val right: OperandSyntax,
+ override val operation: String,
+ override val left: OperandSyntax,
+ override val right: OperandSyntax,
public var infix: Boolean,
) : BinarySyntax() {
init {
@@ -349,9 +349,9 @@ public data class FractionSyntax(
*/
@UnstableKMathAPI
public data class RadicalWithIndexSyntax(
- public override val operation: String,
- public override val left: MathSyntax,
- public override val right: MathSyntax,
+ override val operation: String,
+ override val left: MathSyntax,
+ override val right: MathSyntax,
) : BinarySyntax() {
init {
left.parent = this
@@ -369,9 +369,9 @@ public data class RadicalWithIndexSyntax(
*/
@UnstableKMathAPI
public data class MultiplicationSyntax(
- public override val operation: String,
- public override val left: OperandSyntax,
- public override val right: OperandSyntax,
+ override val operation: String,
+ override val left: OperandSyntax,
+ override val right: OperandSyntax,
public var times: Boolean,
) : BinarySyntax() {
init {
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt
index fb2b3b66f..2f285c600 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
@@ -9,7 +9,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI
/**
* Abstraction of writing [MathSyntax] as a string of an actual markup language. Typical implementation should
- * involve traversal of MathSyntax with handling each its subtype.
+ * involve traversal of MathSyntax with handling each subtype.
*
* @author Iaroslav Postovalov
*/
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt
index ac716f9ff..a7a28d87f 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt
@@ -1,27 +1,26 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
import space.kscience.kmath.ast.rendering.FeaturedMathRenderer.RenderFeature
import space.kscience.kmath.expressions.MST
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.*
import kotlin.reflect.KClass
/**
- * Prints any [MST.Symbolic] as a [SymbolSyntax] containing the [MST.Symbolic.value] of it.
+ * Prints any [Symbol] as a [SymbolSyntax] containing the [Symbol.identity] of it.
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
-public object PrintSymbolic : RenderFeature {
- public override fun render(renderer: FeaturedMathRenderer, node: MST): SymbolSyntax? =
- if (node !is MST.Symbolic) null
- else
- SymbolSyntax(string = node.value)
+public val PrintSymbol: RenderFeature = RenderFeature { _, node ->
+ if (node !is Symbol) null
+ else SymbolSyntax(string = node.identity)
}
/**
@@ -30,8 +29,8 @@ public object PrintSymbolic : RenderFeature {
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
-public object PrintNumeric : RenderFeature {
- public override fun render(renderer: FeaturedMathRenderer, node: MST): NumberSyntax? = if (node !is MST.Numeric)
+public val PrintNumeric: RenderFeature = RenderFeature { _, node ->
+ if (node !is MST.Numeric)
null
else
NumberSyntax(string = node.value.toString())
@@ -50,7 +49,7 @@ else
NumberSyntax(string = s)
/**
- * Special printing for numeric types which are printed in form of
+ * Special printing for numeric types that are printed in form of
* *('-'? (DIGIT+ ('.' DIGIT+)? ('E' '-'? DIGIT+)? | 'Infinity')) | 'NaN'*.
*
* @property types The suitable types.
@@ -58,7 +57,7 @@ else
*/
@UnstableKMathAPI
public class PrettyPrintFloats(public val types: Set>) : RenderFeature {
- public override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? {
+ override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? {
if (node !is MST.Numeric || node.value::class !in types) return null
val toString = when (val v = node.value) {
@@ -111,14 +110,14 @@ public class PrettyPrintFloats(public val types: Set>) : Rend
}
/**
- * Special printing for numeric types which are printed in form of *'-'? DIGIT+*.
+ * Special printing for numeric types that are printed in form of *'-'? DIGIT+*.
*
* @property types The suitable types.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class PrettyPrintIntegers(public val types: Set>) : RenderFeature {
- public override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
+ override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
if (node !is MST.Numeric || node.value::class !in types)
null
else
@@ -141,8 +140,8 @@ public class PrettyPrintIntegers(public val types: Set>) : Re
*/
@UnstableKMathAPI
public class PrettyPrintPi(public val symbols: Set) : RenderFeature {
- public override fun render(renderer: FeaturedMathRenderer, node: MST): SpecialSymbolSyntax? =
- if (node !is MST.Symbolic || node.value !in symbols)
+ override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
+ if (node !is Symbol || node.identity !in symbols)
null
else
SpecialSymbolSyntax(kind = SpecialSymbolSyntax.Kind.SMALL_PI)
@@ -156,7 +155,7 @@ public class PrettyPrintPi(public val symbols: Set) : RenderFeature {
}
/**
- * Abstract printing of unary operations which discards [MST] if their operation is not in [operations] or its type is
+ * Abstract printing of unary operations that discards [MST] if their operation is not in [operations] or its type is
* not [MST.Unary].
*
* @param operations the allowed operations. If `null`, any operation is accepted.
@@ -177,7 +176,7 @@ public abstract class Unary(public val operations: Collection?) : Render
}
/**
- * Abstract printing of unary operations which discards [MST] if their operation is not in [operations] or its type is
+ * Abstract printing of unary operations that discards [MST] if their operation is not in [operations] or its type is
* not [MST.Binary].
*
* @property operations the allowed operations. If `null`, any operation is accepted.
@@ -203,7 +202,7 @@ public abstract class Binary(public val operations: Collection?) : Rende
*/
@UnstableKMathAPI
public class BinaryPlus(operations: Collection?) : Binary(operations) {
- public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): BinaryPlusSyntax =
+ override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryPlusSyntax(
operation = node.operation,
left = OperandSyntax(parent.render(node.left), true),
@@ -225,7 +224,7 @@ public class BinaryPlus(operations: Collection?) : Binary(operations) {
*/
@UnstableKMathAPI
public class BinaryMinus(operations: Collection?) : Binary(operations) {
- public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): BinaryMinusSyntax =
+ override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryMinusSyntax(
operation = node.operation,
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
@@ -247,7 +246,7 @@ public class BinaryMinus(operations: Collection?) : Binary(operations) {
*/
@UnstableKMathAPI
public class UnaryPlus(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryPlusSyntax = UnaryPlusSyntax(
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax(
operation = node.operation,
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
)
@@ -267,7 +266,7 @@ public class UnaryPlus(operations: Collection?) : Unary(operations) {
*/
@UnstableKMathAPI
public class UnaryMinus(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryMinusSyntax = UnaryMinusSyntax(
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax(
operation = node.operation,
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
)
@@ -287,7 +286,7 @@ public class UnaryMinus(operations: Collection?) : Unary(operations) {
*/
@UnstableKMathAPI
public class Fraction(operations: Collection?) : Binary(operations) {
- public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): FractionSyntax = FractionSyntax(
+ override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax(
operation = node.operation,
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
right = OperandSyntax(operand = parent.render(node.right), parentheses = true),
@@ -309,7 +308,7 @@ public class Fraction(operations: Collection?) : Binary(operations) {
*/
@UnstableKMathAPI
public class BinaryOperator(operations: Collection?) : Binary(operations) {
- public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): BinaryOperatorSyntax =
+ override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryOperatorSyntax(
operation = node.operation,
prefix = OperatorNameSyntax(name = node.operation),
@@ -332,7 +331,7 @@ public class BinaryOperator(operations: Collection?) : Binary(operations
*/
@UnstableKMathAPI
public class UnaryOperator(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryOperatorSyntax =
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax(
operation = node.operation,
prefix = OperatorNameSyntax(node.operation),
@@ -354,7 +353,7 @@ public class UnaryOperator(operations: Collection?) : Unary(operations)
*/
@UnstableKMathAPI
public class Power(operations: Collection?) : Binary(operations) {
- public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): SuperscriptSyntax =
+ override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
SuperscriptSyntax(
operation = node.operation,
left = OperandSyntax(parent.render(node.left), true),
@@ -374,7 +373,7 @@ public class Power(operations: Collection?) : Binary(operations) {
*/
@UnstableKMathAPI
public class SquareRoot(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): RadicalSyntax =
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
RadicalSyntax(operation = node.operation, operand = parent.render(node.value))
public companion object {
@@ -392,7 +391,7 @@ public class SquareRoot(operations: Collection?) : Unary(operations) {
*/
@UnstableKMathAPI
public class Exponent(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): ExponentSyntax = ExponentSyntax(
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax(
operation = node.operation,
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
useOperatorForm = true,
@@ -413,7 +412,7 @@ public class Exponent(operations: Collection?) : Unary(operations) {
*/
@UnstableKMathAPI
public class Multiplication(operations: Collection?) : Binary(operations) {
- public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MultiplicationSyntax =
+ override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
MultiplicationSyntax(
operation = node.operation,
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
@@ -436,7 +435,7 @@ public class Multiplication(operations: Collection?) : Binary(operations
*/
@UnstableKMathAPI
public class InverseTrigonometricOperations(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryOperatorSyntax =
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax(
operation = node.operation,
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "arc")),
@@ -463,7 +462,7 @@ public class InverseTrigonometricOperations(operations: Collection?) : U
*/
@UnstableKMathAPI
public class InverseHyperbolicOperations(operations: Collection?) : Unary(operations) {
- public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryOperatorSyntax =
+ override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax(
operation = node.operation,
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "ar")),
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
index 291399cee..3e33d6415 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/stages.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
similarity index 75%
rename from kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/stages.kt
rename to kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
index 1f31af853..3d05e03d6 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/stages.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
@@ -1,10 +1,11 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
+import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess.PostProcessPhase
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.FieldOperations
import space.kscience.kmath.operations.GroupOperations
@@ -17,8 +18,8 @@ import space.kscience.kmath.operations.RingOperations
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
-public object BetterMultiplication : FeaturedMathRendererWithPostProcess.PostProcessStage {
- public override fun perform(node: MathSyntax): Unit = when (node) {
+public val BetterMultiplication: PostProcessPhase = PostProcessPhase { node ->
+ fun perform(node: MathSyntax): Unit = when (node) {
is NumberSyntax -> Unit
is SymbolSyntax -> Unit
is OperatorNameSyntax -> Unit
@@ -81,6 +82,8 @@ public object BetterMultiplication : FeaturedMathRendererWithPostProcess.PostPro
perform(node.right)
}
}
+
+ perform(node)
}
/**
@@ -89,68 +92,68 @@ public object BetterMultiplication : FeaturedMathRendererWithPostProcess.PostPro
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
-public object BetterFraction : FeaturedMathRendererWithPostProcess.PostProcessStage {
- private fun perform0(node: MathSyntax, infix: Boolean = false): Unit = when (node) {
+public val BetterFraction: PostProcessPhase = PostProcessPhase { node ->
+ fun perform(node: MathSyntax, infix: Boolean = false): Unit = when (node) {
is NumberSyntax -> Unit
is SymbolSyntax -> Unit
is OperatorNameSyntax -> Unit
is SpecialSymbolSyntax -> Unit
- is OperandSyntax -> perform0(node.operand, infix)
+ is OperandSyntax -> perform(node.operand, infix)
is UnaryOperatorSyntax -> {
- perform0(node.prefix, infix)
- perform0(node.operand, infix)
+ perform(node.prefix, infix)
+ perform(node.operand, infix)
}
- is UnaryPlusSyntax -> perform0(node.operand, infix)
- is UnaryMinusSyntax -> perform0(node.operand, infix)
- is RadicalSyntax -> perform0(node.operand, infix)
- is ExponentSyntax -> perform0(node.operand, infix)
+ is UnaryPlusSyntax -> perform(node.operand, infix)
+ is UnaryMinusSyntax -> perform(node.operand, infix)
+ is RadicalSyntax -> perform(node.operand, infix)
+ is ExponentSyntax -> perform(node.operand, infix)
is SuperscriptSyntax -> {
- perform0(node.left, true)
- perform0(node.right, true)
+ perform(node.left, true)
+ perform(node.right, true)
}
is SubscriptSyntax -> {
- perform0(node.left, true)
- perform0(node.right, true)
+ perform(node.left, true)
+ perform(node.right, true)
}
is BinaryOperatorSyntax -> {
- perform0(node.prefix, infix)
- perform0(node.left, infix)
- perform0(node.right, infix)
+ perform(node.prefix, infix)
+ perform(node.left, infix)
+ perform(node.right, infix)
}
is BinaryPlusSyntax -> {
- perform0(node.left, infix)
- perform0(node.right, infix)
+ perform(node.left, infix)
+ perform(node.right, infix)
}
is BinaryMinusSyntax -> {
- perform0(node.left, infix)
- perform0(node.right, infix)
+ perform(node.left, infix)
+ perform(node.right, infix)
}
is FractionSyntax -> {
node.infix = infix
- perform0(node.left, infix)
- perform0(node.right, infix)
+ perform(node.left, infix)
+ perform(node.right, infix)
}
is RadicalWithIndexSyntax -> {
- perform0(node.left, true)
- perform0(node.right, true)
+ perform(node.left, true)
+ perform(node.right, true)
}
is MultiplicationSyntax -> {
- perform0(node.left, infix)
- perform0(node.right, infix)
+ perform(node.left, infix)
+ perform(node.right, infix)
}
}
- public override fun perform(node: MathSyntax): Unit = perform0(node)
+ perform(node)
}
/**
@@ -160,39 +163,37 @@ public object BetterFraction : FeaturedMathRendererWithPostProcess.PostProcessSt
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
-public object BetterExponent : FeaturedMathRendererWithPostProcess.PostProcessStage {
- private fun perform0(node: MathSyntax): Boolean {
+public val BetterExponent: PostProcessPhase = PostProcessPhase { node ->
+ fun perform(node: MathSyntax): Boolean {
return when (node) {
is NumberSyntax -> false
is SymbolSyntax -> false
is OperatorNameSyntax -> false
is SpecialSymbolSyntax -> false
- is OperandSyntax -> perform0(node.operand)
- is UnaryOperatorSyntax -> perform0(node.prefix) || perform0(node.operand)
- is UnaryPlusSyntax -> perform0(node.operand)
- is UnaryMinusSyntax -> perform0(node.operand)
+ is OperandSyntax -> perform(node.operand)
+ is UnaryOperatorSyntax -> perform(node.prefix) || perform(node.operand)
+ is UnaryPlusSyntax -> perform(node.operand)
+ is UnaryMinusSyntax -> perform(node.operand)
is RadicalSyntax -> true
is ExponentSyntax -> {
- val r = perform0(node.operand)
+ val r = perform(node.operand)
node.useOperatorForm = r
r
}
is SuperscriptSyntax -> true
is SubscriptSyntax -> true
- is BinaryOperatorSyntax -> perform0(node.prefix) || perform0(node.left) || perform0(node.right)
- is BinaryPlusSyntax -> perform0(node.left) || perform0(node.right)
- is BinaryMinusSyntax -> perform0(node.left) || perform0(node.right)
+ is BinaryOperatorSyntax -> perform(node.prefix) || perform(node.left) || perform(node.right)
+ is BinaryPlusSyntax -> perform(node.left) || perform(node.right)
+ is BinaryMinusSyntax -> perform(node.left) || perform(node.right)
is FractionSyntax -> true
is RadicalWithIndexSyntax -> true
- is MultiplicationSyntax -> perform0(node.left) || perform0(node.right)
+ is MultiplicationSyntax -> perform(node.left) || perform(node.right)
}
}
- public override fun perform(node: MathSyntax) {
- perform0(node)
- }
+ perform(node)
}
/**
@@ -203,8 +204,8 @@ public object BetterExponent : FeaturedMathRendererWithPostProcess.PostProcessSt
*/
@UnstableKMathAPI
public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) -> Int) :
- FeaturedMathRendererWithPostProcess.PostProcessStage {
- public override fun perform(node: MathSyntax): Unit = when (node) {
+ PostProcessPhase {
+ override fun perform(node: MathSyntax): Unit = when (node) {
is NumberSyntax -> Unit
is SymbolSyntax -> Unit
is OperatorNameSyntax -> Unit
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt
index 0d018070c..802d4c10e 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt
@@ -1,17 +1,16 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.MstField
import space.kscience.kmath.expressions.MstRing
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.interpret
-import space.kscience.kmath.misc.Symbol.Companion.x
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.IntRing
-import space.kscience.kmath.operations.bindSymbol
import space.kscience.kmath.operations.invoke
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -22,7 +21,7 @@ internal class TestCompilerConsistencyWithInterpreter {
val mst = MstRing {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- (bindSymbol(x) - (2.toByte() + (scale(
+ (x - (2.toByte() + (scale(
add(number(1), number(1)),
2.0,
) + 1.toByte()))) * 3.0 - 1.toByte()
@@ -42,7 +41,7 @@ internal class TestCompilerConsistencyWithInterpreter {
fun doubleField() = runCompilerTest {
val mst = MstField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (bindSymbol(x) + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ (3.0 - (x + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one,
) + zero
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt
index 7d2af31c2..f5b1e2842 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt
@@ -1,15 +1,14 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.MstExtendedField
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.misc.Symbol.Companion.x
import space.kscience.kmath.operations.DoubleField
-import space.kscience.kmath.operations.bindSymbol
import space.kscience.kmath.operations.invoke
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -17,49 +16,49 @@ import kotlin.test.assertEquals
internal class TestCompilerOperations {
@Test
fun testUnaryPlus() = runCompilerTest {
- val expr = MstExtendedField { +bindSymbol(x) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { +x }.compileToExpression(DoubleField)
assertEquals(2.0, expr(x to 2.0))
}
@Test
fun testUnaryMinus() = runCompilerTest {
- val expr = MstExtendedField { -bindSymbol(x) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { -x }.compileToExpression(DoubleField)
assertEquals(-2.0, expr(x to 2.0))
}
@Test
fun testAdd() = runCompilerTest {
- val expr = MstExtendedField { bindSymbol(x) + bindSymbol(x) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { x + x }.compileToExpression(DoubleField)
assertEquals(4.0, expr(x to 2.0))
}
@Test
fun testSine() = runCompilerTest {
- val expr = MstExtendedField { sin(bindSymbol(x)) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { sin(x) }.compileToExpression(DoubleField)
assertEquals(0.0, expr(x to 0.0))
}
@Test
fun testCosine() = runCompilerTest {
- val expr = MstExtendedField { cos(bindSymbol(x)) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { cos(x) }.compileToExpression(DoubleField)
assertEquals(1.0, expr(x to 0.0))
}
@Test
fun testSubtract() = runCompilerTest {
- val expr = MstExtendedField { bindSymbol(x) - bindSymbol(x) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { x - x }.compileToExpression(DoubleField)
assertEquals(0.0, expr(x to 2.0))
}
@Test
fun testDivide() = runCompilerTest {
- val expr = MstExtendedField { bindSymbol(x) / bindSymbol(x) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { x / x }.compileToExpression(DoubleField)
assertEquals(1.0, expr(x to 2.0))
}
@Test
fun testPower() = runCompilerTest {
- val expr = MstExtendedField { bindSymbol(x) pow 2 }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { x pow 2 }.compileToExpression(DoubleField)
assertEquals(4.0, expr(x to 2.0))
}
}
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt
index ecf8ed367..8d9a2301f 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt
@@ -1,15 +1,14 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.MstRing
+import space.kscience.kmath.expressions.Symbol.Companion.x
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.misc.Symbol.Companion.x
import space.kscience.kmath.operations.IntRing
-import space.kscience.kmath.operations.bindSymbol
import space.kscience.kmath.operations.invoke
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -18,13 +17,13 @@ import kotlin.test.assertFailsWith
internal class TestCompilerVariables {
@Test
fun testVariable() = runCompilerTest {
- val expr = MstRing { bindSymbol(x) }.compileToExpression(IntRing)
+ val expr = MstRing { x }.compileToExpression(IntRing)
assertEquals(1, expr(x to 1))
}
@Test
fun testUndefinedVariableFails() = runCompilerTest {
- val expr = MstRing { bindSymbol(x) }.compileToExpression(IntRing)
+ val expr = MstRing { x }.compileToExpression(IntRing)
assertFailsWith { expr() }
}
}
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt
index b838245e1..4c834a9ca 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt
index bb6bb3ce1..9776da45c 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt
index a40c785b9..ae429d97e 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt
index 6322df25d..d8e432230 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt
index 2d7bfad19..a7fcbc75b 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt
index 09ec127c7..4485605a6 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt
index bf87b6fd0..6b418821b 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt
index abeaed0f8..ef9f3145a 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt
@@ -1,13 +1,13 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
-import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.IntRing
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
index 521907d2c..2e69a536f 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
index 40468f5ab..316fdeeff 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.estree
@@ -9,22 +9,23 @@ import space.kscience.kmath.estree.internal.ESTreeBuilder
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
import space.kscience.kmath.expressions.MST.*
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.expressions.invoke
import space.kscience.kmath.internal.estree.BaseExpression
-import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.operations.Algebra
import space.kscience.kmath.operations.NumericAlgebra
+import space.kscience.kmath.operations.bindSymbolOrNull
@PublishedApi
internal fun MST.compileWith(algebra: Algebra): Expression {
fun ESTreeBuilder.visit(node: MST): BaseExpression = when (node) {
- is Symbolic -> {
- val symbol = algebra.bindSymbolOrNull(node.value)
+ is Symbol -> {
+ val symbol = algebra.bindSymbolOrNull(node)
if (symbol != null)
constant(symbol)
else
- variable(node.value)
+ variable(node.identity)
}
is Numeric -> constant(node.value)
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
index ac20484a4..850f20be7 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
@@ -1,19 +1,14 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.estree.internal
import space.kscience.kmath.expressions.Expression
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.internal.astring.generate
import space.kscience.kmath.internal.estree.*
-import space.kscience.kmath.internal.estree.BaseExpression
-import space.kscience.kmath.internal.estree.BlockStatement
-import space.kscience.kmath.internal.estree.Program
-import space.kscience.kmath.internal.estree.VariableDeclaration
-import space.kscience.kmath.internal.estree.VariableDeclarator
-import space.kscience.kmath.misc.Symbol
internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExpression) {
private class GeneratedExpression(val executable: dynamic, val constants: Array) : Expression {
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt
index eb5c1e3dd..c7faf73e0 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.estree.internal.astring
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt
index cca2d83af..c36860654 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:JsModule("astring")
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt
index 93b4f6ce6..0a5b059ba 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.internal.astring
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt
index 86e0cede7..26186c453 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:Suppress(
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt
index 42b6ac7d8..13e3a49e2 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:Suppress(
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt
index 523b13b40..8e449627c 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:Suppress("PackageDirectoryMismatch", "NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING", "KDocMissingDocumentation")
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt
index 1f7b09af8..d85857de8 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.internal.emitter
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt
index 3aa31f921..122a3a397 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.internal.estree
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt
index e5254013e..ad079dbd0 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt
@@ -1,8 +1,10 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
+@file:Suppress("ClassName")
+
package space.kscience.kmath.internal.estree
import kotlin.js.RegExp
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt
index 52be5530f..caab91731 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.internal.stream
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt
index 9c012e3a3..5c091e3a1 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.internal.tsstdlib
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt
index 0cd395f2c..bb7fd44ca 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:Suppress("UNUSED_TYPEALIAS_PARAMETER", "DEPRECATION")
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt
index 3754c3eff..52dd64a5e 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:JsQualifier("WebAssembly")
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt
index c5023c384..d59a52701 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:Suppress(
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt
index 8fd3c9fb9..5b6cf65db 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.wasm.internal
@@ -8,9 +8,9 @@ package space.kscience.kmath.wasm.internal
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
import space.kscience.kmath.expressions.MST.*
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.internal.binaryen.*
import space.kscience.kmath.internal.webassembly.Instance
-import space.kscience.kmath.misc.StringSymbol
import space.kscience.kmath.operations.*
import space.kscience.kmath.internal.binaryen.Module as BinaryenModule
import space.kscience.kmath.internal.webassembly.Module as WasmModule
@@ -23,20 +23,16 @@ internal sealed class WasmBuilder(
val algebra: Algebra,
val target: MST,
) where T : Number {
- val keys: MutableList = mutableListOf()
+ val keys: MutableList = mutableListOf()
lateinit var ctx: BinaryenModule
- open fun visitSymbolic(mst: Symbolic): ExpressionRef {
- try {
- algebra.bindSymbol(mst.value)
- } catch (ignored: Throwable) {
- null
- }?.let { return visitNumeric(Numeric(it)) }
+ open fun visitSymbolic(mst: Symbol): ExpressionRef {
+ algebra.bindSymbolOrNull(mst)?.let { return visitNumeric(Numeric(it)) }
- var idx = keys.indexOf(mst.value)
+ var idx = keys.indexOf(mst)
if (idx == -1) {
- keys += mst.value
+ keys += mst
idx = keys.lastIndex
}
@@ -54,7 +50,7 @@ internal sealed class WasmBuilder(
open fun createModule(): BinaryenModule = js("new \$module\$binaryen.Module()")
fun visit(mst: MST): ExpressionRef = when (mst) {
- is Symbolic -> visitSymbolic(mst)
+ is Symbol -> visitSymbolic(mst)
is Numeric -> visitNumeric(mst)
is Unary -> when {
@@ -96,7 +92,7 @@ internal sealed class WasmBuilder(
})
val i = Instance(c, js("{}") as Any)
- val symbols = keys.map(::StringSymbol)
+ val symbols = keys
keys.clear()
Expression { args ->
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt
index 21a88b5d0..fe9c22c18 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.wasm.internal
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt
index 394a0567e..5b28b8782 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.wasm
@@ -8,8 +8,8 @@ package space.kscience.kmath.wasm
import space.kscience.kmath.estree.compileWith
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.IntRing
diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/TestExecutionTime.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/TestExecutionTime.kt
index 01746ddb6..f8c429d5a 100644
--- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/TestExecutionTime.kt
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/TestExecutionTime.kt
@@ -1,40 +1,41 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.*
-import space.kscience.kmath.misc.symbol
import space.kscience.kmath.operations.DoubleField
-import space.kscience.kmath.operations.ExtendedField
import space.kscience.kmath.operations.bindSymbol
import space.kscience.kmath.operations.invoke
import kotlin.math.sin
import kotlin.random.Random
+import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.time.measureTime
import space.kscience.kmath.estree.compileToExpression as estreeCompileToExpression
import space.kscience.kmath.wasm.compileToExpression as wasmCompileToExpression
+// TODO move to benchmarks when https://github.com/Kotlin/kotlinx-benchmark/pull/38 or similar feature is merged
+@Ignore
internal class TestExecutionTime {
private companion object {
private const val times = 1_000_000
private val x by symbol
- private val algebra: ExtendedField = DoubleField
+ private val algebra = DoubleField
- private val functional = DoubleField.expressionInExtendedField {
+ private val functional = algebra.expressionInExtendedField {
bindSymbol(x) * const(2.0) + const(2.0) / bindSymbol(x) - const(16.0) / sin(bindSymbol(x))
}
private val node = MstExtendedField {
- bindSymbol(x) * number(2.0) + number(2.0) / bindSymbol(x) - number(16.0) / sin(bindSymbol(x))
+ x * number(2.0) + number(2.0) / x - number(16.0) / sin(x)
}
- private val mst = node.toExpression(DoubleField)
- private val wasm = node.wasmCompileToExpression(DoubleField)
- private val estree = node.estreeCompileToExpression(DoubleField)
+ private val mst = node.toExpression(algebra)
+ private val wasm = node.wasmCompileToExpression(algebra)
+ private val estree = node.estreeCompileToExpression(algebra)
// In JavaScript, the expression below is implemented like
// _no_name_provided__125.prototype.invoke_178 = function (args) {
@@ -44,7 +45,13 @@ internal class TestExecutionTime {
// };
private val raw = Expression { args ->
- args.getValue(x) * 2.0 + 2.0 / args.getValue(x) - 16.0 / sin(args.getValue(x))
+ val x = args[x]!!
+ algebra { x * 2.0 + 2.0 / x - 16.0 / sin(x) }
+ }
+
+ private val justCalculate = { args: dynamic ->
+ val x = args[x].unsafeCast()
+ x * 2.0 + 2.0 / x - 16.0 / sin(x)
}
}
@@ -52,21 +59,56 @@ internal class TestExecutionTime {
println(name)
val rng = Random(0)
var sum = 0.0
- measureTime { repeat(times) { sum += expr(x to rng.nextDouble()) } }.also(::println)
+ measureTime {
+ repeat(times) { sum += expr(x to rng.nextDouble()) }
+ }.also(::println)
}
+ /**
+ * [Expression] created with [expressionInExtendedField].
+ */
@Test
fun functionalExpression() = invokeAndSum("functional", functional)
+ /**
+ * [Expression] created with [mstExpression].
+ */
@Test
fun mstExpression() = invokeAndSum("mst", mst)
+ /**
+ * [Expression] created with [wasmCompileToExpression].
+ */
@Test
fun wasmExpression() = invokeAndSum("wasm", wasm)
+ /**
+ * [Expression] created with [estreeCompileToExpression].
+ */
@Test
fun estreeExpression() = invokeAndSum("estree", wasm)
+ /**
+ * [Expression] implemented manually with `kotlin.math`.
+ */
@Test
fun rawExpression() = invokeAndSum("raw", raw)
+
+ /**
+ * Direct computation w/o [Expression].
+ */
+ @Test
+ fun justCalculateExpression() {
+ println("justCalculate")
+ val rng = Random(0)
+ var sum = 0.0
+ measureTime {
+ repeat(times) {
+ val arg = rng.nextDouble()
+ val o = js("{}")
+ o["x"] = arg
+ sum += justCalculate(o)
+ }
+ }.also(::println)
+ }
}
diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt
index 6b5b1b83d..3c2a9bd13 100644
--- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt
@@ -1,15 +1,17 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
-import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.IntRing
+import kotlin.contracts.InvocationKind
+import kotlin.contracts.contract
import space.kscience.kmath.estree.compile as estreeCompile
import space.kscience.kmath.estree.compileToExpression as estreeCompileToExpression
import space.kscience.kmath.wasm.compile as wasmCompile
@@ -34,6 +36,7 @@ private object ESTreeCompilerTestContext : CompilerTestContext {
}
internal actual inline fun runCompilerTest(action: CompilerTestContext.() -> Unit) {
+ contract { callsInPlace(action, InvocationKind.AT_LEAST_ONCE) }
action(WasmCompilerTestContext)
action(ESTreeCompilerTestContext)
}
diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt
index dd5452d04..6c91df866 100644
--- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.wasm
@@ -8,10 +8,9 @@ package space.kscience.kmath.wasm
import space.kscience.kmath.expressions.MstExtendedField
import space.kscience.kmath.expressions.MstRing
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.misc.symbol
+import space.kscience.kmath.expressions.symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.IntRing
-import space.kscience.kmath.operations.bindSymbol
import space.kscience.kmath.operations.invoke
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -31,7 +30,7 @@ internal class TestWasmSpecific {
@Test
fun argsPassing() {
- val res = MstExtendedField { bindSymbol(y) + bindSymbol(x).pow(10) }.compile(
+ val res = MstExtendedField { y + x.pow(10) }.compile(
DoubleField,
x to 2.0,
y to 100000000.0,
@@ -42,7 +41,7 @@ internal class TestWasmSpecific {
@Test
fun powFunction() {
- val expr = MstExtendedField { bindSymbol(x).pow(1.0 / 6.0) }.compileToExpression(DoubleField)
+ val expr = MstExtendedField { x.pow(1.0 / 6.0) }.compileToExpression(DoubleField)
assertEquals(0.9730585187140817, expr(x to 0.8488554755054833))
}
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt
index dbce893d1..2426d6ee4 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.asm
@@ -10,10 +10,11 @@ import space.kscience.kmath.asm.internal.buildName
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
import space.kscience.kmath.expressions.MST.*
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.operations.Algebra
import space.kscience.kmath.operations.NumericAlgebra
+import space.kscience.kmath.operations.bindSymbolOrNull
/**
* Compiles given MST to an Expression using AST compiler.
@@ -26,13 +27,13 @@ import space.kscience.kmath.operations.NumericAlgebra
@PublishedApi
internal fun MST.compileWith(type: Class, algebra: Algebra): Expression {
fun AsmBuilder.visit(node: MST): Unit = when (node) {
- is Symbolic -> {
- val symbol = algebra.bindSymbolOrNull(node.value)
+ is Symbol -> {
+ val symbol = algebra.bindSymbolOrNull(node)
if (symbol != null)
loadObjectConstant(symbol as Any)
else
- loadVariable(node.value)
+ loadVariable(node.identity)
}
is Numeric -> loadNumberConstant(node.value)
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt
index 39ebf049d..418d6141b 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.asm.internal
@@ -14,9 +14,11 @@ import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
+import java.nio.file.Paths
import java.util.stream.Collectors.toMap
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
+import kotlin.io.path.writeBytes
/**
* ASM Builder is a structure that abstracts building a class designated to unwrap [MST] to plain Java expression.
@@ -194,15 +196,18 @@ internal class AsmBuilder(
visitEnd()
}
- val cls = classLoader.defineClass(className, classWriter.toByteArray())
- // java.io.File("dump.class").writeBytes(classWriter.toByteArray())
+ val binary = classWriter.toByteArray()
+ val cls = classLoader.defineClass(className, binary)
+
+ if (System.getProperty("space.kscience.communicator.prettyapi.dump.generated.classes") == "1")
+ Paths.get("$className.class").writeBytes(binary)
+
val l = MethodHandles.publicLookup()
- if (hasConstants)
- l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array::class.java))
- .invoke(constants.toTypedArray()) as Expression
+ (if (hasConstants)
+ l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array::class.java))(constants.toTypedArray())
else
- l.findConstructor(cls, MethodType.methodType(Void.TYPE)).invoke() as Expression
+ l.findConstructor(cls, MethodType.methodType(Void.TYPE))()) as Expression
}
/**
@@ -342,8 +347,8 @@ internal class AsmBuilder(
val MAP_INTRINSICS_TYPE: Type by lazy { getObjectType("space/kscience/kmath/asm/internal/MapIntrinsics") }
/**
- * ASM Type for [space.kscience.kmath.misc.Symbol].
+ * ASM Type for [space.kscience.kmath.expressions.Symbol].
*/
- val SYMBOL_TYPE: Type by lazy { getObjectType("space/kscience/kmath/misc/Symbol") }
+ val SYMBOL_TYPE: Type by lazy { getObjectType("space/kscience/kmath/expressions/Symbol") }
}
}
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt
index cfac59847..5e2e7d8c6 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.asm.internal
@@ -57,13 +57,13 @@ internal fun MethodVisitor.label(): Label = Label().also(::visitLabel)
/**
* Creates a class name for [Expression] subclassed to implement [mst] provided.
*
- * This methods helps to avoid collisions of class name to prevent loading several classes with the same name. If there
+ * These methods help to avoid collisions of class name to prevent loading several classes with the same name. If there
* is a colliding class, change [collision] parameter or leave it `0` to check existing classes recursively.
*
* @author Iaroslav Postovalov
*/
internal tailrec fun buildName(mst: MST, collision: Int = 0): String {
- val name = "kscience.kmath.asm.generated.AsmCompiledExpression_${mst.hashCode()}_$collision"
+ val name = "space.kscience.kmath.asm.generated.CompiledExpression_${mst.hashCode()}_$collision"
try {
Class.forName(name)
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
index dc8f19fb6..40d9d8fe6 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
@@ -1,18 +1,17 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
@file:JvmName("MapIntrinsics")
package space.kscience.kmath.asm.internal
-import space.kscience.kmath.misc.StringSymbol
-import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.expressions.Symbol
/**
* Gets value with given [key] or throws [NoSuchElementException] whenever it is not present.
*
* @author Iaroslav Postovalov
*/
-internal fun Map.getOrFail(key: String): V = getValue(StringSymbol(key))
+internal fun Map.getOrFail(key: String): V = getValue(Symbol(key))
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
index 556adbe7d..3e5253084 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast.rendering
diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt
index 607c5fdd6..a0bdd68a0 100644
--- a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt
+++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt
@@ -1,15 +1,17 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST
-import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.expressions.Symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.IntRing
+import kotlin.contracts.InvocationKind
+import kotlin.contracts.contract
import space.kscience.kmath.asm.compile as asmCompile
import space.kscience.kmath.asm.compileToExpression as asmCompileToExpression
@@ -22,4 +24,7 @@ private object AsmCompilerTestContext : CompilerTestContext {
asmCompile(algebra, arguments)
}
-internal actual inline fun runCompilerTest(action: CompilerTestContext.() -> Unit) = action(AsmCompilerTestContext)
+internal actual inline fun runCompilerTest(action: CompilerTestContext.() -> Unit) {
+ contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) }
+ action(AsmCompilerTestContext)
+}
diff --git a/kmath-commons/build.gradle.kts b/kmath-commons/build.gradle.kts
index a208c956c..96c17a215 100644
--- a/kmath-commons/build.gradle.kts
+++ b/kmath-commons/build.gradle.kts
@@ -9,6 +9,7 @@ dependencies {
api(project(":kmath-core"))
api(project(":kmath-complex"))
api(project(":kmath-coroutines"))
+ api(project(":kmath-optimization"))
api(project(":kmath-stat"))
api(project(":kmath-functions"))
api("org.apache.commons:commons-math3:3.6.1")
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt
index 736685789..bc0119ca2 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt
@@ -1,13 +1,12 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.commons.expressions
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
import space.kscience.kmath.expressions.*
-import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.ExtendedField
import space.kscience.kmath.operations.NumbersAddOperations
@@ -16,7 +15,7 @@ import space.kscience.kmath.operations.NumbersAddOperations
* A field over commons-math [DerivativeStructure].
*
* @property order The derivation order.
- * @property bindings The map of bindings values. All bindings are considered free parameters
+ * @param bindings The map of bindings values. All bindings are considered free parameters
*/
@OptIn(UnstableKMathAPI::class)
public class DerivativeStructureField(
@@ -26,13 +25,13 @@ public class DerivativeStructureField(
NumbersAddOperations {
public val numberOfVariables: Int = bindings.size
- public override val zero: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order) }
- public override val one: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order, 1.0) }
+ override val zero: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order) }
+ override val one: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order, 1.0) }
- public override fun number(value: Number): DerivativeStructure = const(value.toDouble())
+ override fun number(value: Number): DerivativeStructure = const(value.toDouble())
/**
- * A class that implements both [DerivativeStructure] and a [Symbol]
+ * A class implementing both [DerivativeStructure] and [Symbol].
*/
public inner class DerivativeStructureSymbol(
size: Int,
@@ -40,10 +39,10 @@ public class DerivativeStructureField(
symbol: Symbol,
value: Double,
) : DerivativeStructure(size, order, index, value), Symbol {
- public override val identity: String = symbol.identity
- public override fun toString(): String = identity
- public override fun equals(other: Any?): Boolean = this.identity == (other as? Symbol)?.identity
- public override fun hashCode(): Int = identity.hashCode()
+ override val identity: String = symbol.identity
+ override fun toString(): String = identity
+ override fun equals(other: Any?): Boolean = this.identity == (other as? Symbol)?.identity
+ override fun hashCode(): Int = identity.hashCode()
}
/**
@@ -53,10 +52,10 @@ public class DerivativeStructureField(
key.identity to DerivativeStructureSymbol(numberOfVariables, index, key, value)
}.toMap()
- public override fun const(value: Double): DerivativeStructure = DerivativeStructure(numberOfVariables, order, value)
+ override fun const(value: Double): DerivativeStructure = DerivativeStructure(numberOfVariables, order, value)
- public override fun bindSymbolOrNull(value: String): DerivativeStructureSymbol? = variables[value]
- public override fun bindSymbol(value: String): DerivativeStructureSymbol = variables.getValue(value)
+ override fun bindSymbolOrNull(value: String): DerivativeStructureSymbol? = variables[value]
+ override fun bindSymbol(value: String): DerivativeStructureSymbol = variables.getValue(value)
public fun bindSymbolOrNull(symbol: Symbol): DerivativeStructureSymbol? = variables[symbol.identity]
public fun bindSymbol(symbol: Symbol): DerivativeStructureSymbol = variables.getValue(symbol.identity)
@@ -69,47 +68,50 @@ public class DerivativeStructureField(
public fun DerivativeStructure.derivative(vararg symbols: Symbol): Double = derivative(symbols.toList())
- public override fun DerivativeStructure.unaryMinus(): DerivativeStructure = negate()
+ override fun DerivativeStructure.unaryMinus(): DerivativeStructure = negate()
- public override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b)
+ override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b)
- public override fun scale(a: DerivativeStructure, value: Double): DerivativeStructure = a.multiply(value)
+ override fun scale(a: DerivativeStructure, value: Double): DerivativeStructure = a.multiply(value)
- public override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b)
- public override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b)
- public override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin()
- public override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos()
- public override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan()
- public override fun asin(arg: DerivativeStructure): DerivativeStructure = arg.asin()
- public override fun acos(arg: DerivativeStructure): DerivativeStructure = arg.acos()
- public override fun atan(arg: DerivativeStructure): DerivativeStructure = arg.atan()
- public override fun sinh(arg: DerivativeStructure): DerivativeStructure = arg.sinh()
- public override fun cosh(arg: DerivativeStructure): DerivativeStructure = arg.cosh()
- public override fun tanh(arg: DerivativeStructure): DerivativeStructure = arg.tanh()
- public override fun asinh(arg: DerivativeStructure): DerivativeStructure = arg.asinh()
- public override fun acosh(arg: DerivativeStructure): DerivativeStructure = arg.acosh()
- public override fun atanh(arg: DerivativeStructure): DerivativeStructure = arg.atanh()
+ override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b)
+ override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b)
+ override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin()
+ override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos()
+ override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan()
+ override fun asin(arg: DerivativeStructure): DerivativeStructure = arg.asin()
+ override fun acos(arg: DerivativeStructure): DerivativeStructure = arg.acos()
+ override fun atan(arg: DerivativeStructure): DerivativeStructure = arg.atan()
+ override fun sinh(arg: DerivativeStructure): DerivativeStructure = arg.sinh()
+ override fun cosh(arg: DerivativeStructure): DerivativeStructure = arg.cosh()
+ override fun tanh(arg: DerivativeStructure): DerivativeStructure = arg.tanh()
+ override fun asinh(arg: DerivativeStructure): DerivativeStructure = arg.asinh()
+ override fun acosh(arg: DerivativeStructure): DerivativeStructure = arg.acosh()
+ override fun atanh(arg: DerivativeStructure): DerivativeStructure = arg.atanh()
- public override fun power(arg: DerivativeStructure, pow: Number): DerivativeStructure = when (pow) {
+ override fun power(arg: DerivativeStructure, pow: Number): DerivativeStructure = when (pow) {
is Double -> arg.pow(pow)
is Int -> arg.pow(pow)
else -> arg.pow(pow.toDouble())
}
public fun power(arg: DerivativeStructure, pow: DerivativeStructure): DerivativeStructure = arg.pow(pow)
- public override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp()
- public override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log()
+ override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp()
+ override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log()
- public override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble())
- public override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble())
- public override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this
- public override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this
+ override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble())
+ override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble())
+ override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this
+ override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this
+}
- public companion object :
- AutoDiffProcessor> {
- public override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression> =
- DerivativeStructureExpression(function)
- }
+/**
+ * Auto-diff processor based on Commons-math [DerivativeStructure]
+ */
+public object DSProcessor : AutoDiffProcessor {
+ override fun differentiate(
+ function: DerivativeStructureField.() -> DerivativeStructure,
+ ): DerivativeStructureExpression = DerivativeStructureExpression(function)
}
/**
@@ -117,14 +119,14 @@ public class DerivativeStructureField(
*/
public class DerivativeStructureExpression(
public val function: DerivativeStructureField.() -> DerivativeStructure,
-) : DifferentiableExpression> {
- public override operator fun invoke(arguments: Map): Double =
+) : DifferentiableExpression {
+ override operator fun invoke(arguments: Map): Double =
DerivativeStructureField(0, arguments).function().value
/**
* Get the derivative expression with given orders
*/
- public override fun derivativeOrNull(symbols: List): Expression = Expression { arguments ->
+ override fun derivativeOrNull(symbols: List): Expression = Expression { arguments ->
with(DerivativeStructureField(symbols.size, arguments)) { function().derivative(symbols) }
}
}
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/GaussRuleIntegrator.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt
similarity index 90%
rename from kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/GaussRuleIntegrator.kt
rename to kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt
index 1c9915563..5152b04f9 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/GaussRuleIntegrator.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.commons.integration
@@ -11,12 +11,12 @@ import space.kscience.kmath.integration.*
/**
* A simple one-pass integrator based on Gauss rule
*/
-public class GaussRuleIntegrator(
+public class CMGaussRuleIntegrator(
private val numpoints: Int,
private var type: GaussRule = GaussRule.LEGANDRE,
) : UnivariateIntegrator {
- override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand {
+ override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand {
val range = integrand.getFeature()?.range
?: error("Integration range is not provided")
val integrator: GaussIntegrator = getIntegrator(range)
@@ -76,8 +76,8 @@ public class GaussRuleIntegrator(
numPoints: Int = 100,
type: GaussRule = GaussRule.LEGANDRE,
function: (Double) -> Double,
- ): Double = GaussRuleIntegrator(numPoints, type).integrate(
+ ): Double = CMGaussRuleIntegrator(numPoints, type).process(
UnivariateIntegrand(function, IntegrationRange(range))
- ).value!!
+ ).value
}
}
\ No newline at end of file
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt
index 92bf86128..76a2f297c 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.commons.integration
@@ -18,13 +18,7 @@ public class CMIntegrator(
public val integratorBuilder: (Integrand) -> org.apache.commons.math3.analysis.integration.UnivariateIntegrator,
) : UnivariateIntegrator {
- public class TargetRelativeAccuracy(public val value: Double) : IntegrandFeature
- public class TargetAbsoluteAccuracy(public val value: Double) : IntegrandFeature
-
- public class MinIterations(public val value: Int) : IntegrandFeature
- public class MaxIterations(public val value: Int) : IntegrandFeature
-
- override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand {
+ override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand {
val integrator = integratorBuilder(integrand)
val maxCalls = integrand.getFeature()?.maxCalls ?: defaultMaxCalls
val remainingCalls = maxCalls - integrand.calls
@@ -45,16 +39,15 @@ public class CMIntegrator(
* Create a Simpson integrator based on [SimpsonIntegrator]
*/
public fun simpson(defaultMaxCalls: Int = 200): CMIntegrator = CMIntegrator(defaultMaxCalls) { integrand ->
- val absoluteAccuracy = integrand.getFeature()?.value
+ val absoluteAccuracy = integrand.getFeature()?.accuracy
?: SimpsonIntegrator.DEFAULT_ABSOLUTE_ACCURACY
- val relativeAccuracy = integrand.getFeature()?.value
+ val relativeAccuracy = integrand.getFeature()?.accuracy
?: SimpsonIntegrator.DEFAULT_ABSOLUTE_ACCURACY
- val minIterations = integrand.getFeature()?.value
- ?: SimpsonIntegrator.DEFAULT_MIN_ITERATIONS_COUNT
- val maxIterations = integrand.getFeature()?.value
- ?: SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT
+ val iterations = integrand.getFeature()?.range
+ ?: SimpsonIntegrator.DEFAULT_MIN_ITERATIONS_COUNT..SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT
- SimpsonIntegrator(relativeAccuracy, absoluteAccuracy, minIterations, maxIterations)
+
+ SimpsonIntegrator(relativeAccuracy, absoluteAccuracy, iterations.first, iterations.last)
}
/**
@@ -62,21 +55,19 @@ public class CMIntegrator(
*/
public fun legandre(numPoints: Int, defaultMaxCalls: Int = numPoints * 5): CMIntegrator =
CMIntegrator(defaultMaxCalls) { integrand ->
- val absoluteAccuracy = integrand.getFeature()?.value
+ val absoluteAccuracy = integrand.getFeature()?.accuracy
?: IterativeLegendreGaussIntegrator.DEFAULT_ABSOLUTE_ACCURACY
- val relativeAccuracy = integrand.getFeature()?.value
+ val relativeAccuracy = integrand.getFeature()?.accuracy
?: IterativeLegendreGaussIntegrator.DEFAULT_ABSOLUTE_ACCURACY
- val minIterations = integrand.getFeature()?.value
- ?: IterativeLegendreGaussIntegrator.DEFAULT_MIN_ITERATIONS_COUNT
- val maxIterations = integrand.getFeature()?.value
- ?: IterativeLegendreGaussIntegrator.DEFAULT_MAX_ITERATIONS_COUNT
+ val iterations = integrand.getFeature()?.range
+ ?: IterativeLegendreGaussIntegrator.DEFAULT_MIN_ITERATIONS_COUNT..IterativeLegendreGaussIntegrator.DEFAULT_MAX_ITERATIONS_COUNT
IterativeLegendreGaussIntegrator(
numPoints,
relativeAccuracy,
absoluteAccuracy,
- minIterations,
- maxIterations
+ iterations.first,
+ iterations.last
)
}
}
@@ -84,14 +75,14 @@ public class CMIntegrator(
@UnstableKMathAPI
public var MutableList.targetAbsoluteAccuracy: Double?
- get() = filterIsInstance().lastOrNull()?.value
+ get() = filterIsInstance().lastOrNull()?.accuracy
set(value) {
- value?.let { add(CMIntegrator.TargetAbsoluteAccuracy(value)) }
+ value?.let { add(IntegrandAbsoluteAccuracy(value)) }
}
@UnstableKMathAPI
public var MutableList.targetRelativeAccuracy: Double?
- get() = filterIsInstance().lastOrNull()?.value
+ get() = filterIsInstance().lastOrNull()?.accuracy
set(value) {
- value?.let { add(CMIntegrator.TargetRelativeAccuracy(value)) }
+ value?.let { add(IntegrandRelativeAccuracy(value)) }
}
\ No newline at end of file
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt
index 11b097831..14e7fc365 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt
@@ -1,6 +1,6 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.commons.linear
@@ -10,23 +10,27 @@ import space.kscience.kmath.linear.*
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.nd.StructureFeature
import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.DoubleBuffer
import kotlin.reflect.KClass
import kotlin.reflect.cast
public class CMMatrix(public val origin: RealMatrix) : Matrix {
- public override val rowNum: Int get() = origin.rowDimension
- public override val colNum: Int get() = origin.columnDimension
+ override val rowNum: Int get() = origin.rowDimension
+ override val colNum: Int get() = origin.columnDimension
- public override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j)
+ override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j)
}
-public class CMVector(public val origin: RealVector) : Point {
- public override val size: Int get() = origin.dimension
+@JvmInline
+public value class CMVector(public val origin: RealVector) : Point {
+ override val size: Int get() = origin.dimension
- public override operator fun get(index: Int): Double = origin.getEntry(index)
+ override operator fun get(index: Int): Double = origin.getEntry(index)
- public override operator fun iterator(): Iterator = origin.toArray().iterator()
+ override operator fun iterator(): Iterator = origin.toArray().iterator()
+
+ override fun toString(): String = Buffer.toString(this)
}
public fun RealVector.toPoint(): CMVector = CMVector(this)
@@ -34,7 +38,7 @@ public fun RealVector.toPoint(): CMVector = CMVector(this)
public object CMLinearSpace : LinearSpace {
override val elementAlgebra: DoubleField get() = DoubleField
- public override fun buildMatrix(
+ override fun buildMatrix(
rows: Int,
columns: Int,
initializer: DoubleField.(i: Int, j: Int) -> Double,
@@ -73,16 +77,16 @@ public object CMLinearSpace : LinearSpace {
override fun Point.minus(other: Point): CMVector =
toCM().origin.subtract(other.toCM().origin).wrap()
- public override fun Matrix.dot(other: Matrix): CMMatrix =
+ override fun Matrix.dot(other: Matrix): CMMatrix =
toCM().origin.multiply(other.toCM().origin).wrap()
- public override fun Matrix.dot(vector: Point): CMVector =
+ override fun Matrix.dot(vector: Point): CMVector =
toCM().origin.preMultiply(vector.toCM().origin).wrap()
- public override operator fun Matrix.minus(other: Matrix): CMMatrix =
+ override operator fun Matrix.minus(other: Matrix): CMMatrix =
toCM().origin.subtract(other.toCM().origin).wrap()
- public override operator fun Matrix.times(value: Double): CMMatrix =
+ override operator fun Matrix.times(value: Double): CMMatrix =
toCM().origin.scalarMultiply(value).wrap()
override fun Double.times(m: Matrix): CMMatrix =
@@ -95,7 +99,7 @@ public object CMLinearSpace : LinearSpace {
v * this
@UnstableKMathAPI
- override fun getFeature(structure: Matrix, type: KClass): F? {
+ override fun computeFeature(structure: Matrix, type: KClass): F? {
//Return the feature if it is intrinsic to the structure
structure.getFeature(type)?.let { return it }
@@ -109,22 +113,22 @@ public object CMLinearSpace : LinearSpace {
LupDecompositionFeature {
private val lup by lazy { LUDecomposition(origin) }
override val determinant: Double by lazy { lup.determinant }
- override val l: Matrix by lazy { CMMatrix(lup.l) + LFeature }
- override val u: Matrix by lazy { CMMatrix(lup.u) + UFeature }
+ override val l: Matrix by lazy> { CMMatrix(lup.l).withFeature(LFeature) }
+ override val u: Matrix by lazy> { CMMatrix(lup.u).withFeature(UFeature) }
override val p: Matrix by lazy { CMMatrix(lup.p) }
}
CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature {
- override val l: Matrix by lazy {
+ override val l: Matrix by lazy> {
val cholesky = CholeskyDecomposition(origin)
- CMMatrix(cholesky.l) + LFeature
+ CMMatrix(cholesky.l).withFeature(LFeature)
}
}
QRDecompositionFeature::class -> object : QRDecompositionFeature {
private val qr by lazy { QRDecomposition(origin) }
- override val q: Matrix by lazy { CMMatrix(qr.q) + OrthogonalFeature }
- override val r: Matrix by lazy { CMMatrix(qr.r) + UFeature }
+ override val q: Matrix by lazy> { CMMatrix(qr.q).withFeature(OrthogonalFeature) }
+ override val r: Matrix by lazy> { CMMatrix(qr.r).withFeature(UFeature) }
}
SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature {
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt
index ee602ca06..d1fb441b0 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt
@@ -1,11 +1,12 @@
/*
* Copyright 2018-2021 KMath contributors.
- * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
+ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package space.kscience.kmath.commons.linear
import org.apache.commons.math3.linear.*
+import space.kscience.kmath.linear.LinearSolver
import space.kscience.kmath.linear.Matrix
import space.kscience.kmath.linear.Point
@@ -17,7 +18,7 @@ public enum class CMDecomposition {
CHOLESKY
}
-public fun CMLinearSpace.solver(
+private fun CMLinearSpace.solver(
a: Matrix,
decomposition: CMDecomposition = CMDecomposition.LUP,
): DecompositionSolver = when (decomposition) {
@@ -44,3 +45,14 @@ public fun CMLinearSpace.inverse(
a: Matrix,
decomposition: CMDecomposition = CMDecomposition.LUP,
): CMMatrix = solver(a, decomposition).inverse.wrap()
+
+
+public fun CMLinearSpace.solver(decomposition: CMDecomposition): LinearSolver = object : LinearSolver {
+ override fun solve(a: Matrix, b: Matrix): Matrix = solver(a, decomposition).solve(b.toCM().origin).wrap()
+
+ override fun solve(a: Matrix