) {
for ((xBatch, yBatch) in iterBatch(xTrain, yTrain)) {
train(xBatch, yBatch)
}
- println("Accuracy:${accuracy(yTrain, predict(xTrain).argMax(1, true).asDouble())}")
+ println("Accuracy:${accuracy(yTrain, predict(xTrain).argMax(1, true).toDoubleTensor())}")
}
}
@@ -194,7 +194,7 @@ fun main() = BroadcastDoubleTensorAlgebra {
val y = fromArray(
intArrayOf(sampleSize, 1),
DoubleArray(sampleSize) { i ->
- if (x[i].sum() > 0.0) {
+ if (x.getTensor(i).sum() > 0.0) {
1.0
} else {
0.0
@@ -230,7 +230,7 @@ fun main() = BroadcastDoubleTensorAlgebra {
val prediction = model.predict(xTest)
// process raw prediction via argMax
- val predictionLabels = prediction.argMax(1, true).asDouble()
+ val predictionLabels = prediction.argMax(1, true).toDoubleTensor()
// find out accuracy
val acc = accuracy(yTest, predictionLabels)
diff --git a/gradle.properties b/gradle.properties
index 80108737e..0d1506980 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -6,8 +6,10 @@ kotlin.code.style=official
kotlin.jupyter.add.scanner=false
kotlin.mpp.stability.nowarn=true
kotlin.native.ignoreDisabledTargets=true
-#kotlin.incremental.js.ir=true
+kotlin.incremental.js.ir=true
org.gradle.configureondemand=true
-org.gradle.jvmargs=-XX:MaxMetaspaceSize=1G
org.gradle.parallel=true
+org.gradle.jvmargs=-Xmx4096m
+
+toolsVersion=0.13.0-kotlin-1.7.20-Beta
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index aa991fcea..8049c684f 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
diff --git a/kmath-ast/README.md b/kmath-ast/README.md
index 5d5630f46..c6da64982 100644
--- a/kmath-ast/README.md
+++ b/kmath-ast/README.md
@@ -10,7 +10,7 @@ Extensions to MST API: transformations, dynamic compilation and visualization.
## Artifact:
-The Maven coordinates of this project are `space.kscience:kmath-ast:0.3.0`.
+The Maven coordinates of this project are `space.kscience:kmath-ast:0.3.1-dev-1`.
**Gradle Groovy:**
```groovy
@@ -20,7 +20,7 @@ repositories {
}
dependencies {
- implementation 'space.kscience:kmath-ast:0.3.0'
+ implementation 'space.kscience:kmath-ast:0.3.1-dev-1'
}
```
**Gradle Kotlin DSL:**
@@ -31,7 +31,7 @@ repositories {
}
dependencies {
- implementation("space.kscience:kmath-ast:0.3.0")
+ implementation("space.kscience:kmath-ast:0.3.1-dev-1")
}
```
@@ -199,10 +199,7 @@ 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})
-
+$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\\,x\right)}{2\times10^{10}+x^{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 15b1d0900..7fd0f43f7 100644
--- a/kmath-ast/build.gradle.kts
+++ b/kmath-ast/build.gradle.kts
@@ -1,6 +1,9 @@
plugins {
- kotlin("multiplatform")
- id("ru.mipt.npm.gradle.common")
+ id("space.kscience.gradle.mpp")
+}
+
+kscience{
+ native()
}
kotlin.js {
@@ -24,7 +27,7 @@ kotlin.sourceSets {
commonMain {
dependencies {
- api("com.github.h0tk3y.betterParse:better-parse:0.4.2")
+ api("com.github.h0tk3y.betterParse:better-parse:0.4.4")
api(project(":kmath-core"))
}
}
@@ -57,11 +60,11 @@ tasks.dokkaHtml {
if (System.getProperty("space.kscience.kmath.ast.dump.generated.classes") == "1")
tasks.jvmTest {
- jvmArgs = (jvmArgs ?: emptyList()) + listOf("-Dspace.kscience.kmath.ast.dump.generated.classes=1")
+ jvmArgs("-Dspace.kscience.kmath.ast.dump.generated.classes=1")
}
readme {
- maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
+ maturity = space.kscience.gradle.Maturity.EXPERIMENTAL
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
feature(
diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md
index e9e22f4d4..96bbfbf5a 100644
--- a/kmath-ast/docs/README-TEMPLATE.md
+++ b/kmath-ast/docs/README-TEMPLATE.md
@@ -170,10 +170,7 @@ 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})
-
+$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\\,x\right)}{2\times10^{10}+x^{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/TypedMst.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt
index 8a8b8797d..6458dc123 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt
index 71fb154c9..e411cd251 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 012a6e65f..2c9a2a9ad 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 2df3d3cc7..13806703c 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 8b5819b84..bd941745b 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 fdef35ebd..dd8ed3457 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 ee23ab408..14df5ad8d 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 362c07d72..e19a9722e 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 90f78a152..c1a895015 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..dce99cc5a 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
index c0271fbb5..f42237ba6 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 1edb5923e..3400db0f8 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 be8a92f3e..bf56b80a6 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 93ef97b0f..f23d36240 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt
index 954a0f330..95b804455 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 d0c3a789e..d3c203903 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 42cf5ce58..4b3631663 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..7b5ec5765 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 43f31baba..66c0ae1ae 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 145055494..8bc014c54 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..306538d5d 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..79f178eef 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 ec7436188..fe035c69f 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..f6411334c 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 a8b1aa2e1..853f5f983 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -36,7 +36,7 @@ public fun MST.compileToExpression(algebra: Algebra): Expression
)
}
- return ESTreeBuilder { visit(typed) }.instance
+ return ESTreeBuilder { visit(typed) }.instance
}
/**
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 10a6c4a16..1517cdef2 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -22,28 +22,20 @@ internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExp
}
}
+ @Suppress("UNUSED_VARIABLE")
val instance: Expression by lazy {
val node = Program(
sourceType = "script",
- VariableDeclaration(
- kind = "var",
- VariableDeclarator(
- id = Identifier("executable"),
- init = FunctionExpression(
- params = arrayOf(Identifier("constants"), Identifier("arguments")),
- body = BlockStatement(ReturnStatement(bodyCallback())),
- ),
- ),
- ),
+ ReturnStatement(bodyCallback())
)
- eval(generate(node))
- GeneratedExpression(js("executable"), constants.toTypedArray())
+ val code = generate(node)
+ GeneratedExpression(js("new Function('constants', 'arguments_0', code)"), constants.toTypedArray())
}
private val constants = mutableListOf()
- fun constant(value: Any?) = when {
+ fun constant(value: Any?): BaseExpression = when {
value == null || jsTypeOf(value) == "number" || jsTypeOf(value) == "string" || jsTypeOf(value) == "boolean" ->
SimpleLiteral(value)
@@ -61,7 +53,8 @@ internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExp
}
}
- fun variable(name: Symbol): BaseExpression = call(getOrFail, Identifier("arguments"), SimpleLiteral(name.identity))
+ fun variable(name: Symbol): BaseExpression =
+ call(getOrFail, Identifier("arguments_0"), SimpleLiteral(name.identity))
fun call(function: Function, vararg args: BaseExpression): BaseExpression = SimpleCallExpression(
optional = false,
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..2434788ec 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..cc4360f8d 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..a17726c9d 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..8ea699837 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..d907a12c9 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..eebfeb6ef 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..a2a04da79 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..2dd2c08cd 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,8 +1,10 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
+@file:Suppress("unused")
+
package space.kscience.kmath.internal.estree
internal fun Program(sourceType: String, vararg body: dynamic) = object : Program {
@@ -28,9 +30,10 @@ internal fun Identifier(name: String) = object : Identifier {
override var name = name
}
-internal fun FunctionExpression(params: Array, body: BlockStatement) = object : FunctionExpression {
+internal fun FunctionExpression(id: Identifier?, params: Array, body: BlockStatement) = object : FunctionExpression {
override var params = params
override var type = "FunctionExpression"
+ override var id: Identifier? = id
override var body = body
}
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 b62b8c06c..bf4a25367 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..ced165a3a 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..2c0dc9de1 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..f20e2d865 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 90690abed..873a5e1d2 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..ba86e977b 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 aacb62f36..d29fbde55 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -71,7 +71,7 @@ internal sealed class WasmBuilder>(
protected open fun visitBinary(mst: TypedMst.Binary): ExpressionRef =
error("Binary operation ${mst.operation} not defined in $this")
- protected open fun createModule(): BinaryenModule = js("new \$module\$binaryen.Module()")
+ protected open fun createModule(): BinaryenModule = space.kscience.kmath.internal.binaryen.Module()
protected fun visit(node: TypedMst): ExpressionRef = when (node) {
is TypedMst.Constant -> visitNumber(
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..d60f24247 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 f9540f9db..6a20da799 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 0d896c6f6..c6b241e5c 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 8ae5fcb36..0a85d5f24 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 73b9c97a7..59a26af7d 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 a85079fc8..e1fd455fd 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -49,5 +49,7 @@ internal abstract class AsmBuilder {
* ASM Type for [space.kscience.kmath.expressions.Symbol].
*/
val SYMBOL_TYPE: Type = getObjectType("space/kscience/kmath/expressions/Symbol")
+
+ const val ARGUMENTS_NAME = "args"
}
}
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt
index 6cf3d8721..acae45d87 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt
index 01bad83e5..d50318cd1 100644
--- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 9e880f4fc..b3ccfdc0c 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 3a5ef74f7..6459f4dcf 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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..ec66be830 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 47f1cc476..9f0fb33ab 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
new file mode 100644
index 000000000..ec66be830
--- /dev/null
+++ b/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt
@@ -0,0 +1,9 @@
+/*
+ * Copyright 2018-2022 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.
+ */
+
+package space.kscience.kmath.ast.rendering
+
+internal actual fun Double.multiplatformToString(): String = toString()
+internal actual fun Float.multiplatformToString(): String = toString()
diff --git a/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt b/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt
new file mode 100644
index 000000000..0674b0492
--- /dev/null
+++ b/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt
@@ -0,0 +1,10 @@
+/*
+ * Copyright 2018-2022 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.
+ */
+
+package space.kscience.kmath.ast
+
+internal actual inline fun runCompilerTest(action: CompilerTestContext.() -> Unit) {
+ //doNothing
+}
\ No newline at end of file
diff --git a/kmath-commons/README.md b/kmath-commons/README.md
index 7195e6fb1..89f1f6c9f 100644
--- a/kmath-commons/README.md
+++ b/kmath-commons/README.md
@@ -6,7 +6,7 @@ Commons math binding for kmath
## Artifact:
-The Maven coordinates of this project are `space.kscience:kmath-commons:0.3.0`.
+The Maven coordinates of this project are `space.kscience:kmath-commons:0.3.1-dev-1`.
**Gradle Groovy:**
```groovy
@@ -16,7 +16,7 @@ repositories {
}
dependencies {
- implementation 'space.kscience:kmath-commons:0.3.0'
+ implementation 'space.kscience:kmath-commons:0.3.1-dev-1'
}
```
**Gradle Kotlin DSL:**
@@ -27,6 +27,6 @@ repositories {
}
dependencies {
- implementation("space.kscience:kmath-commons:0.3.0")
+ implementation("space.kscience:kmath-commons:0.3.1-dev-1")
}
```
diff --git a/kmath-commons/build.gradle.kts b/kmath-commons/build.gradle.kts
index 96c17a215..50fef7ac8 100644
--- a/kmath-commons/build.gradle.kts
+++ b/kmath-commons/build.gradle.kts
@@ -1,6 +1,5 @@
plugins {
- kotlin("jvm")
- id("ru.mipt.npm.gradle.common")
+ id("space.kscience.gradle.jvm")
}
description = "Commons math binding for kmath"
@@ -16,5 +15,5 @@ dependencies {
}
readme {
- maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
+ maturity = space.kscience.gradle.Maturity.EXPERIMENTAL
}
\ No newline at end of file
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 82694d95a..6d756b15f 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt
index e0a2f4931..263463d37 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
package space.kscience.kmath.commons.integration
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 257429fa7..4839518e6 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 aa7e0a638..e95c9115d 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
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 9bb5deffd..19799aab3 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,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt
index 11eb6fba8..46baa7d50 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@file:OptIn(UnstableKMathAPI::class)
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt
index 194be6002..c7a23ebc4 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt
index 40168971e..2243dc5d9 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt
index 56252ab34..40ba9fc7d 100644
--- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt
+++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt
index c5573fef1..308f504af 100644
--- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt
+++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt
index 0977dc247..b1b7b3ed7 100644
--- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt
+++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-complex/README.md b/kmath-complex/README.md
index 4646c6a80..f00952065 100644
--- a/kmath-complex/README.md
+++ b/kmath-complex/README.md
@@ -2,13 +2,13 @@
Complex and hypercomplex number systems in KMath.
- - [complex](src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt) : Complex Numbers
- - [quaternion](src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt) : Quaternions
+ - [complex](src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt) : Complex numbers operations
+ - [quaternion](src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt) : Quaternions and their composition
## Artifact:
-The Maven coordinates of this project are `space.kscience:kmath-complex:0.3.0`.
+The Maven coordinates of this project are `space.kscience:kmath-complex:0.3.1-dev-1`.
**Gradle Groovy:**
```groovy
@@ -18,7 +18,7 @@ repositories {
}
dependencies {
- implementation 'space.kscience:kmath-complex:0.3.0'
+ implementation 'space.kscience:kmath-complex:0.3.1-dev-1'
}
```
**Gradle Kotlin DSL:**
@@ -29,6 +29,6 @@ repositories {
}
dependencies {
- implementation("space.kscience:kmath-complex:0.3.0")
+ implementation("space.kscience:kmath-complex:0.3.1-dev-1")
}
```
diff --git a/kmath-complex/build.gradle.kts b/kmath-complex/build.gradle.kts
index ea74df646..b63def0a7 100644
--- a/kmath-complex/build.gradle.kts
+++ b/kmath-complex/build.gradle.kts
@@ -1,7 +1,9 @@
plugins {
- kotlin("multiplatform")
- id("ru.mipt.npm.gradle.common")
- id("ru.mipt.npm.gradle.native")
+ id("space.kscience.gradle.mpp")
+}
+
+kscience {
+ native()
}
kotlin.sourceSets {
@@ -14,18 +16,20 @@ kotlin.sourceSets {
readme {
description = "Complex numbers and quaternions."
- maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE
+ maturity = space.kscience.gradle.Maturity.PROTOTYPE
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
feature(
id = "complex",
- description = "Complex Numbers",
ref = "src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt"
- )
+ ) {
+ "Complex numbers operations"
+ }
feature(
id = "quaternion",
- description = "Quaternions",
ref = "src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt"
- )
+ ) {
+ "Quaternions and their composition"
+ }
}
diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt
index 77fe782a9..5804e47c1 100644
--- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt
+++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -10,10 +10,7 @@ import space.kscience.kmath.memory.MemorySpec
import space.kscience.kmath.memory.MemoryWriter
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.*
-import space.kscience.kmath.structures.Buffer
-import space.kscience.kmath.structures.MemoryBuffer
-import space.kscience.kmath.structures.MutableBuffer
-import space.kscience.kmath.structures.MutableMemoryBuffer
+import space.kscience.kmath.structures.*
import kotlin.math.*
/**
@@ -54,6 +51,9 @@ public object ComplexField :
Norm,
NumbersAddOps,
ScaleOperations {
+ override val bufferFactory: MutableBufferFactory = MutableBufferFactory { size, init ->
+ MutableMemoryBuffer.create(Complex, size, init)
+ }
override val zero: Complex = 0.0.toComplex()
override val one: Complex = 1.0.toComplex()
diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt
index 46d4b7c5c..42914ed5b 100644
--- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt
+++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -56,11 +56,6 @@ public sealed class ComplexFieldOpsND : BufferedFieldOpsND
- get() = bufferAlgebra(Buffer.Companion::complex)
-
-
@OptIn(UnstableKMathAPI::class)
public class ComplexFieldND(override val shape: Shape) :
ComplexFieldOpsND(), FieldND,
diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt
index 3ef3428c6..e9b42118f 100644
--- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt
+++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
@@ -16,52 +16,137 @@ import space.kscience.kmath.structures.MutableBuffer
import space.kscience.kmath.structures.MutableMemoryBuffer
import kotlin.math.*
+/**
+ * Represents `double`-based quaternion.
+ *
+ * @property w The first component.
+ * @property x The second component.
+ * @property y The third component.
+ * @property z The fourth component.
+ */
+public class Quaternion(
+ public val w: Double,
+ public val x: Double,
+ public val y: Double,
+ public val z: Double,
+) : Buffer {
+ init {
+ require(!w.isNaN()) { "w-component of quaternion is not-a-number" }
+ require(!x.isNaN()) { "x-component of quaternion is not-a-number" }
+ require(!y.isNaN()) { "y-component of quaternion is not-a-number" }
+ require(!z.isNaN()) { "z-component of quaternion is not-a-number" }
+ }
+
+ /**
+ * Returns a string representation of this quaternion.
+ */
+ override fun toString(): String = "($w + $x * i + $y * j + $z * k)"
+
+ override val size: Int get() = 4
+
+ override fun get(index: Int): Double = when (index) {
+ 0 -> w
+ 1 -> x
+ 2 -> y
+ 3 -> z
+ else -> error("Index $index out of bounds [0,3]")
+ }
+
+ override fun iterator(): Iterator = listOf(w, x, y, z).iterator()
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (other == null || this::class != other::class) return false
+
+ other as Quaternion
+
+ if (w != other.w) return false
+ if (x != other.x) return false
+ if (y != other.y) return false
+ if (z != other.z) return false
+
+ return true
+ }
+
+ override fun hashCode(): Int {
+ var result = w.hashCode()
+ result = 31 * result + x.hashCode()
+ result = 31 * result + y.hashCode()
+ result = 31 * result + z.hashCode()
+ return result
+ }
+
+ public companion object : MemorySpec {
+ override val objectSize: Int get() = 32
+
+ override fun MemoryReader.read(offset: Int): Quaternion = Quaternion(
+ readDouble(offset),
+ readDouble(offset + 8),
+ readDouble(offset + 16),
+ readDouble(offset + 24)
+ )
+
+ override fun MemoryWriter.write(offset: Int, value: Quaternion) {
+ writeDouble(offset, value.w)
+ writeDouble(offset + 8, value.x)
+ writeDouble(offset + 16, value.y)
+ writeDouble(offset + 24, value.z)
+ }
+ }
+}
+
+public fun Quaternion(w: Number, x: Number = 0.0, y: Number = 0.0, z: Number = 0.0): Quaternion = Quaternion(
+ w.toDouble(),
+ x.toDouble(),
+ y.toDouble(),
+ z.toDouble(),
+)
+
/**
* This quaternion's conjugate.
*/
public val Quaternion.conjugate: Quaternion
- get() = QuaternionField { z - x * i - y * j - z * k }
+ get() = Quaternion(w, -x, -y, -z)
/**
* This quaternion's reciprocal.
*/
public val Quaternion.reciprocal: Quaternion
get() {
- QuaternionField {
- val n = norm(this@reciprocal)
- return conjugate / (n * n)
- }
+ val norm2 = (w * w + x * x + y * y + z * z)
+ return Quaternion(w / norm2, -x / norm2, -y / norm2, -z / norm2)
}
+
+//TODO consider adding a-priory normalized quaternions
/**
- * Absolute value of the quaternion.
+ * Produce a normalized version of this quaternion
*/
-public val Quaternion.r: Double
- get() = sqrt(w * w + x * x + y * y + z * z)
+public fun Quaternion.normalized(): Quaternion = with(QuaternionField){ this@normalized / norm(this@normalized) }
/**
* A field of [Quaternion].
*/
@OptIn(UnstableKMathAPI::class)
-public object QuaternionField : Field, Norm, PowerOperations,
+public object QuaternionField : Field, Norm, PowerOperations,
ExponentialOperations, NumbersAddOps, ScaleOperations {
- override val zero: Quaternion = 0.toQuaternion()
- override val one: Quaternion = 1.toQuaternion()
+ override val zero: Quaternion = Quaternion(0.0)
+ override val one: Quaternion = Quaternion(1.0)
/**
* The `i` quaternion unit.
*/
- public val i: Quaternion = Quaternion(0, 1)
+ public val i: Quaternion = Quaternion(0.0, 1.0, 0.0, 0.0)
/**
* The `j` quaternion unit.
*/
- public val j: Quaternion = Quaternion(0, 0, 1)
+ public val j: Quaternion = Quaternion(0.0, 0.0, 1.0, 0.0)
/**
* The `k` quaternion unit.
*/
- public val k: Quaternion = Quaternion(0, 0, 0, 1)
+ public val k: Quaternion = Quaternion(0.0, 0.0, 0.0, 1.0)
override fun add(left: Quaternion, right: Quaternion): Quaternion =
Quaternion(left.w + right.w, left.x + right.x, left.y + right.y, left.z + right.z)
@@ -133,7 +218,7 @@ public object QuaternionField : Field, Norm,
override fun exp(arg: Quaternion): Quaternion {
val un = arg.x * arg.x + arg.y * arg.y + arg.z * arg.z
- if (un == 0.0) return exp(arg.w).toQuaternion()
+ if (un == 0.0) return Quaternion(exp(arg.w))
val n1 = sqrt(un)
val ea = exp(arg.w)
val n2 = ea * sin(n1) / n1
@@ -158,7 +243,8 @@ public object QuaternionField : Field, Norm,
return Quaternion(ln(n), th * arg.x, th * arg.y, th * arg.z)
}
- override operator fun Number.plus(other: Quaternion): Quaternion = Quaternion(toDouble() + other.w, other.x, other.y, other.z)
+ override operator fun Number.plus(other: Quaternion): Quaternion =
+ Quaternion(toDouble() + other.w, other.x, other.y, other.z)
override operator fun Number.minus(other: Quaternion): Quaternion =
Quaternion(toDouble() - other.w, -other.x, -other.y, -other.z)
@@ -170,7 +256,12 @@ public object QuaternionField : Field, Norm,
Quaternion(toDouble() * arg.w, toDouble() * arg.x, toDouble() * arg.y, toDouble() * arg.z)
override fun Quaternion.unaryMinus(): Quaternion = Quaternion(-w, -x, -y, -z)
- override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg)
+ override fun norm(arg: Quaternion): Double = sqrt(
+ arg.w.pow(2) +
+ arg.x.pow(2) +
+ arg.y.pow(2) +
+ arg.z.pow(2)
+ )
override fun bindSymbolOrNull(value: String): Quaternion? = when (value) {
"i" -> i
@@ -179,7 +270,7 @@ public object QuaternionField : Field, Norm,
else -> null
}
- override fun number(value: Number): Quaternion = value.toQuaternion()
+ override fun number(value: Number): Quaternion = Quaternion(value)
override fun sinh(arg: Quaternion): Quaternion = (exp(arg) - exp(-arg)) / 2.0
override fun cosh(arg: Quaternion): Quaternion = (exp(arg) + exp(-arg)) / 2.0
@@ -189,76 +280,6 @@ public object QuaternionField : Field, Norm,
override fun atanh(arg: Quaternion): Quaternion = (ln(arg + one) - ln(one - arg)) / 2.0
}
-/**
- * Represents `double`-based quaternion.
- *
- * @property w The first component.
- * @property x The second component.
- * @property y The third component.
- * @property z The fourth component.
- */
-@OptIn(UnstableKMathAPI::class)
-public data class Quaternion(
- val w: Double, val x: Double, val y: Double, val z: Double,
-) {
- public constructor(w: Number, x: Number, y: Number, z: Number) : this(
- w.toDouble(),
- x.toDouble(),
- y.toDouble(),
- z.toDouble(),
- )
-
- public constructor(w: Number, x: Number, y: Number) : this(w.toDouble(), x.toDouble(), y.toDouble(), 0.0)
- public constructor(w: Number, x: Number) : this(w.toDouble(), x.toDouble(), 0.0, 0.0)
- public constructor(w: Number) : this(w.toDouble(), 0.0, 0.0, 0.0)
- public constructor(wx: Complex, yz: Complex) : this(wx.re, wx.im, yz.re, yz.im)
- public constructor(wx: Complex) : this(wx.re, wx.im, 0, 0)
-
- init {
- require(!w.isNaN()) { "w-component of quaternion is not-a-number" }
- require(!x.isNaN()) { "x-component of quaternion is not-a-number" }
- require(!y.isNaN()) { "x-component of quaternion is not-a-number" }
- require(!z.isNaN()) { "x-component of quaternion is not-a-number" }
- }
-
- /**
- * Returns a string representation of this quaternion.
- */
- override fun toString(): String = "($w + $x * i + $y * j + $z * k)"
-
- public companion object : MemorySpec {
- override val objectSize: Int
- get() = 32
-
- override fun MemoryReader.read(offset: Int): Quaternion =
- Quaternion(readDouble(offset), readDouble(offset + 8), readDouble(offset + 16), readDouble(offset + 24))
-
- override fun MemoryWriter.write(offset: Int, value: Quaternion) {
- writeDouble(offset, value.w)
- writeDouble(offset + 8, value.x)
- writeDouble(offset + 16, value.y)
- writeDouble(offset + 24, value.z)
- }
- }
-}
-
-/**
- * Creates a quaternion with real part equal to this real.
- *
- * @receiver the real part.
- * @return a new quaternion.
- */
-public fun Number.toQuaternion(): Quaternion = Quaternion(this)
-
-/**
- * Creates a quaternion with `w`-component equal to `re`-component of given complex and `x`-component equal to
- * `im`-component of given complex.
- *
- * @receiver the complex number.
- * @return a new quaternion.
- */
-public fun Complex.toQuaternion(): Quaternion = Quaternion(this)
-
/**
* Creates a new buffer of quaternions with the specified [size], where each element is calculated by calling the
* specified [init] function.
diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt
index 17a077ea7..ca3f8f43f 100644
--- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt
+++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt
index cbaaa815b..e11f1c1ea 100644
--- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt
+++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt
index 7ad7f883d..1a35d1dd8 100644
--- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt
+++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt
index 4279471d4..767406e0b 100644
--- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt
+++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionFieldTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt
similarity index 82%
rename from kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionFieldTest.kt
rename to kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt
index 6784f3516..fd0fd46a7 100644
--- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionFieldTest.kt
+++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt
@@ -1,15 +1,28 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
package space.kscience.kmath.complex
import space.kscience.kmath.operations.invoke
+import space.kscience.kmath.testutils.assertBufferEquals
import kotlin.test.Test
import kotlin.test.assertEquals
-internal class QuaternionFieldTest {
+internal class QuaternionTest {
+
+ @Test
+ fun testNorm() {
+ assertEquals(2.0, QuaternionField.norm(Quaternion(1.0, 1.0, 1.0, 1.0)))
+ }
+
+ @Test
+ fun testInverse() = QuaternionField {
+ val q = Quaternion(1.0, 2.0, -3.0, 4.0)
+ assertBufferEquals(one, q * q.reciprocal, 1e-4)
+ }
+
@Test
fun testAddition() {
assertEquals(Quaternion(42, 42), QuaternionField { Quaternion(16, 16) + Quaternion(26, 26) })
diff --git a/kmath-core/README.md b/kmath-core/README.md
index 4fddd327c..e84ca38d7 100644
--- a/kmath-core/README.md
+++ b/kmath-core/README.md
@@ -15,7 +15,7 @@ performance calculations to code generation.
## Artifact:
-The Maven coordinates of this project are `space.kscience:kmath-core:0.3.0`.
+The Maven coordinates of this project are `space.kscience:kmath-core:0.3.1-dev-1`.
**Gradle Groovy:**
```groovy
@@ -25,7 +25,7 @@ repositories {
}
dependencies {
- implementation 'space.kscience:kmath-core:0.3.0'
+ implementation 'space.kscience:kmath-core:0.3.1-dev-1'
}
```
**Gradle Kotlin DSL:**
@@ -36,6 +36,6 @@ repositories {
}
dependencies {
- implementation("space.kscience:kmath-core:0.3.0")
+ implementation("space.kscience:kmath-core:0.3.1-dev-1")
}
```
diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts
index 4a35a54fb..f33d33324 100644
--- a/kmath-core/build.gradle.kts
+++ b/kmath-core/build.gradle.kts
@@ -1,8 +1,13 @@
plugins {
- kotlin("multiplatform")
- id("ru.mipt.npm.gradle.common")
- id("ru.mipt.npm.gradle.native")
-// id("com.xcporter.metaview") version "0.0.5"
+ id("space.kscience.gradle.mpp")
+}
+
+kscience{
+ native()
+
+ dependencies {
+ api(project(":kmath-memory"))
+ }
}
kotlin.sourceSets {
@@ -12,23 +17,11 @@ kotlin.sourceSets {
it.optIn("space.kscience.kmath.misc.PerformancePitfall")
it.optIn("space.kscience.kmath.misc.UnstableKMathAPI")
}
-
- commonMain {
- dependencies {
- api(project(":kmath-memory"))
- }
- }
}
-//generateUml {
-// classTree {
-//
-// }
-//}
-
readme {
description = "Core classes, algebra definitions, basic linear algebra"
- maturity = ru.mipt.npm.gradle.Maturity.DEVELOPMENT
+ maturity = space.kscience.gradle.Maturity.DEVELOPMENT
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
feature(
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt
index e06b774fd..d09228e96 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt
index 2fce772cc..a3c3a2eda 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt
index 8ddd6406f..dd0e35bc8 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt
index e99ae0698..3dc1bb99b 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt
index b5a84cf6c..eecdcebad 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt
index 3d531349c..10755e633 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt
index ee1bebde0..b0803f3e1 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
package space.kscience.kmath.domains
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt
index 485416a69..7d5843a97 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
package space.kscience.kmath.domains
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt
index 32a5fc56c..e8b867fac 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2018-2021 KMath contributors.
+ * Copyright 2018-2022 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.
*/
package space.kscience.kmath.domains
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt
new file mode 100644
index 000000000..fa8bf5e58
--- /dev/null
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt
@@ -0,0 +1,456 @@
+/*
+ * Copyright 2018-2022 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.
+ */
+
+package space.kscience.kmath.expressions
+
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.operations.*
+import space.kscience.kmath.structures.Buffer
+import space.kscience.kmath.structures.MutableBuffer
+import space.kscience.kmath.structures.MutableBufferFactory
+import space.kscience.kmath.structures.asBuffer
+import kotlin.math.max
+import kotlin.math.min
+
+/**
+ * Class representing both the value and the differentials of a function.
+ *
+ * This class is the workhorse of the differentiation package.
+ *
+ * This class is an implementation of the extension to Rall's numbers described in Dan Kalman's paper
+ * [Doubly Recursive Multivariate Automatic Differentiation](http://www1.american.edu/cas/mathstat/People/kalman/pdffiles/mmgautodiff.pdf),
+ * Mathematics Magazine, vol. 75, no. 3, June 2002. Rall's numbers are an extension to the real numbers used
+ * throughout mathematical expressions; they hold the derivative together with the value of a function. Dan Kalman's
+ * derivative structures hold all partial derivatives up to any specified order, with respect to any number of free
+ * parameters. Rall's numbers therefore can be seen as derivative structures for order one derivative and one free
+ * parameter, and real numbers can be seen as derivative structures with zero order derivative and no free parameters.
+ *
+ * Derived from
+ * [Commons Math's `DerivativeStructure`](https://github.com/apache/commons-math/blob/924f6c357465b39beb50e3c916d5eb6662194175/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/differentiation/DerivativeStructure.java).
+ */
+@UnstableKMathAPI
+public interface DS> {
+ public val derivativeAlgebra: DSAlgebra
+ public val data: Buffer
+}
+
+/**
+ * Get a partial derivative.
+ *
+ * @param orders derivation orders with respect to each variable (if all orders are 0, the value is returned).
+ * @return partial derivative.
+ * @see value
+ */
+@UnstableKMathAPI
+private fun > DS.getPartialDerivative(vararg orders: Int): T =
+ data[derivativeAlgebra.compiler.getPartialDerivativeIndex(*orders)]
+
+/**
+ * Provide a partial derivative with given symbols. On symbol could me mentioned multiple times
+ */
+@UnstableKMathAPI
+public fun > DS.derivative(symbols: List): T {
+ require(symbols.size <= derivativeAlgebra.order) { "The order of derivative ${symbols.size} exceeds computed order ${derivativeAlgebra.order}" }
+ val ordersCount: Map = symbols.map { it.identity }.groupBy { it }.mapValues { it.value.size }
+ return getPartialDerivative(*symbols.map { ordersCount[it] ?: 0 }.toIntArray())
+}
+
+/**
+ * Provide a partial derivative with given symbols. On symbol could me mentioned multiple times
+ */
+@UnstableKMathAPI
+public fun > DS.derivative(vararg symbols: Symbol): T {
+ require(symbols.size <= derivativeAlgebra.order) { "The order of derivative ${symbols.size} exceeds computed order ${derivativeAlgebra.order}" }
+ val ordersCount: Map = symbols.map { it.identity }.groupBy { it }.mapValues { it.value.size }
+ return getPartialDerivative(*symbols.map { ordersCount[it] ?: 0 }.toIntArray())
+}
+
+/**
+ * The value part of the derivative structure.
+ *
+ * @see getPartialDerivative
+ */
+@UnstableKMathAPI
+public val > DS.value: T get() = data[0]
+
+@UnstableKMathAPI
+public abstract class DSAlgebra>(
+ public val algebra: A,
+ public val order: Int,
+ bindings: Map,
+ public val valueBufferFactory: MutableBufferFactory = algebra.bufferFactory,
+) : ExpressionAlgebra>, SymbolIndexer {
+
+ /**
+ * Get the compiler for number of free parameters and order.
+ *
+ * @return cached rules set.
+ */
+ @PublishedApi
+ internal val compiler: DSCompiler by lazy {
+ val numberOfVariables = bindings.size
+ // get the cached compilers
+ val cache: Array?>>? = null
+
+ // we need to create more compilers
+ val maxParameters: Int = max(numberOfVariables, cache?.size ?: 0)
+ val maxOrder: Int = max(order, if (cache == null) 0 else cache[0].size)
+ val newCache: Array?>> = Array(maxParameters + 1) { arrayOfNulls(maxOrder + 1) }
+
+ if (cache != null) {
+ // preserve the already created compilers
+ for (i in cache.indices) {
+ cache[i].copyInto(newCache[i], endIndex = cache[i].size)
+ }
+ }
+
+ // create the array in increasing diagonal order
+ for (diag in 0..numberOfVariables + order) {
+ for (o in max(0, diag - numberOfVariables)..min(order, diag)) {
+ val p: Int = diag - o
+ if (newCache[p][o] == null) {
+ val valueCompiler: DSCompiler? = if (p == 0) null else newCache[p - 1][o]!!
+ val derivativeCompiler: DSCompiler? = if (o == 0) null else newCache[p][o - 1]!!
+
+ newCache[p][o] = DSCompiler(
+ algebra,
+ valueBufferFactory,
+ p,
+ o,
+ valueCompiler,
+ derivativeCompiler,
+ )
+ }
+ }
+ }
+
+ return@lazy newCache[numberOfVariables][order]!!
+ }
+
+ private val variables: Map by lazy {
+ bindings.entries.mapIndexed { index, (key, value) ->
+ key to DSSymbol(
+ index,
+ key,
+ value,
+ )
+ }.toMap()
+ }
+ override val symbols: List = bindings.map { it.key }
+
+ private fun bufferForVariable(index: Int, value: T): Buffer {
+ val buffer = valueBufferFactory(compiler.size) { algebra.zero }
+ buffer[0] = value
+ if (compiler.order > 0) {
+ // the derivative of the variable with respect to itself is 1.
+
+ val indexOfDerivative = compiler.getPartialDerivativeIndex(*IntArray(symbols.size).apply {
+ set(index, 1)
+ })
+
+ buffer[indexOfDerivative] = algebra.one
+ }
+ return buffer
+ }
+
+ @UnstableKMathAPI
+ private inner class DSImpl(
+ override val data: Buffer,
+ ) : DS {
+ override val derivativeAlgebra: DSAlgebra get() = this@DSAlgebra
+ }
+
+ protected fun DS(data: Buffer): DS = DSImpl(data)
+
+
+ /**
+ * Build an instance representing a variable.
+ *
+ * Instances built using this constructor are considered to be the free variables with respect to which
+ * differentials are computed. As such, their differential with respect to themselves is +1.
+ */
+ public fun variable(
+ index: Int,
+ value: T,
+ ): DS {
+ require(index < compiler.freeParameters) { "number is too large: $index >= ${compiler.freeParameters}" }
+ return DS(bufferForVariable(index, value))
+ }
+
+ /**
+ * Build an instance from all its derivatives.
+ *
+ * @param derivatives derivatives sorted according to [DSCompiler.getPartialDerivativeIndex].
+ */
+ public fun ofDerivatives(
+ vararg derivatives: T,
+ ): DS {
+ require(derivatives.size == compiler.size) { "dimension mismatch: ${derivatives.size} and ${compiler.size}" }
+ val data = derivatives.asBuffer()
+
+ return DS(data)
+ }
+
+ /**
+ * A class implementing both [DS] and [Symbol].
+ */
+ @UnstableKMathAPI
+ public inner class DSSymbol internal constructor(
+ index: Int,
+ symbol: Symbol,
+ value: T,
+ ) : Symbol by symbol, DS {
+ override val derivativeAlgebra: DSAlgebra get() = this@DSAlgebra
+ override val data: Buffer = bufferForVariable(index, value)
+ }
+
+ public override fun const(value: T): DS {
+ val buffer = valueBufferFactory(compiler.size) { algebra.zero }
+ buffer[0] = value
+
+ return DS(buffer)
+ }
+
+ override fun bindSymbolOrNull(value: String): DSSymbol? = variables[StringSymbol(value)]
+
+ override fun bindSymbol(value: String): DSSymbol =
+ bindSymbolOrNull(value) ?: error("Symbol '$value' is not supported in $this")
+
+ public fun bindSymbolOrNull(symbol: Symbol): DSSymbol? = variables[symbol.identity]
+
+ public fun bindSymbol(symbol: Symbol): DSSymbol =
+ bindSymbolOrNull(symbol.identity) ?: error("Symbol '${symbol}' is not supported in $this")
+
+ public fun DS.derivative(symbols: List): T {
+ require(symbols.size <= order) { "The order of derivative ${symbols.size} exceeds computed order $order" }
+ val ordersCount = symbols.groupBy { it }.mapValues { it.value.size }
+ return getPartialDerivative(*variables.keys.map { ordersCount[it] ?: 0 }.toIntArray())
+ }
+
+ public fun DS.derivative(vararg symbols: Symbol): T = derivative(symbols.toList())
+
+}
+
+
+/**
+ * A ring over [DS].
+ *
+ * @property order The derivation order.
+ * @param bindings The map of bindings values. All bindings are considered free parameters.
+ */
+@UnstableKMathAPI
+public open class DSRing(
+ algebra: A,
+ order: Int,
+ bindings: Map,
+ valueBufferFactory: MutableBufferFactory,
+) : DSAlgebra(algebra, order, bindings, valueBufferFactory),
+ Ring>, ScaleOperations>,
+ NumericAlgebra>,
+ NumbersAddOps> where A : Ring, A : NumericAlgebra, A : ScaleOperations {
+
+ override fun bindSymbolOrNull(value: String): DSSymbol? =
+ super.bindSymbolOrNull(value)
+
+ override fun DS.unaryMinus(): DS = mapData { -it }
+
+ /**
+ * Create a copy of given [Buffer] and modify it according to [block]
+ */
+ protected inline fun DS.transformDataBuffer(block: A.(MutableBuffer) -> Unit): DS {
+ require(derivativeAlgebra == this@DSRing) { "All derivative operations should be done in the same algebra" }
+ val newData = valueBufferFactory(compiler.size) { data[it] }
+ algebra.block(newData)
+ return DS(newData)
+ }
+
+ protected fun DS.mapData(block: A.(T) -> T): DS {
+ require(derivativeAlgebra == this@DSRing) { "All derivative operations should be done in the same algebra" }
+ val newData: Buffer = data.map(valueBufferFactory) {
+ algebra.block(it)
+ }
+ return DS(newData)
+ }
+
+ protected fun DS.mapDataIndexed(block: (Int, T) -> T): DS {
+ require(derivativeAlgebra == this@DSRing) { "All derivative operations should be done in the same algebra" }
+ val newData: Buffer = data.mapIndexed(valueBufferFactory, block)
+ return DS(newData)
+ }
+
+ override val zero: DS by lazy {
+ const(algebra.zero)
+ }
+
+ override val one: DS by lazy {
+ const(algebra.one)
+ }
+
+ override fun number(value: Number): DS = const(algebra.number(value))
+
+ override fun add(left: DS, right: DS): DS = left.transformDataBuffer { result ->
+ require(right.derivativeAlgebra == this@DSRing) { "All derivative operations should be done in the same algebra" }
+ compiler.add(left.data, 0, right.data, 0, result, 0)
+ }
+
+ override fun scale(a: DS, value: Double): DS = a.mapData {
+ it.times(value)
+ }
+
+ override fun multiply(
+ left: DS,
+ right: DS,
+ ): DS = left.transformDataBuffer { result ->
+ compiler.multiply(left.data, 0, right.data, 0, result, 0)
+ }
+//
+// override fun DS.minus(arg: DS): DS = transformDataBuffer { result ->
+// subtract(data, 0, arg.data, 0, result, 0)
+// }
+
+ override operator fun DS.plus(other: Number): DS = transformDataBuffer {
+ it[0] += number(other)
+ }
+
+//
+// override operator fun DS.minus(other: Number): DS =
+// this + (-other.toDouble())
+
+ override operator fun Number.plus(other: DS): DS = other + this
+ override operator fun Number.minus(other: DS): DS = other - this
+}
+
+@UnstableKMathAPI
+public class DerivativeStructureRingExpression(
+ public val algebra: A,
+ public val elementBufferFactory: MutableBufferFactory = algebra.bufferFactory,
+ public val function: DSRing.() -> DS,
+) : DifferentiableExpression where A : Ring, A : ScaleOperations, A : NumericAlgebra {
+ override operator fun invoke(arguments: Map): T =
+ DSRing(algebra, 0, arguments, elementBufferFactory).function().value
+
+ override fun derivativeOrNull(symbols: List): Expression = Expression { arguments ->
+ with(
+ DSRing(
+ algebra,
+ symbols.size,
+ arguments,
+ elementBufferFactory
+ )
+ ) { function().derivative(symbols) }
+ }
+}
+
+/**
+ * A field over commons-math [DerivativeStructure].
+ *
+ * @property order The derivation order.
+ * @param bindings The map of bindings values. All bindings are considered free parameters.
+ */
+@UnstableKMathAPI
+public class DSField>(
+ algebra: A,
+ order: Int,
+ bindings: Map,
+ valueBufferFactory: MutableBufferFactory,
+) : DSRing(algebra, order, bindings, valueBufferFactory), ExtendedField> {
+ override fun number(value: Number): DS = const(algebra.number(value))
+
+ override fun divide(left: DS