diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b4dddf81..4ade9cd9c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@
- Buffer factories for primitives moved to MutableBuffer.Companion
- NDStructure and NDAlgebra to StructureND and AlgebraND respectively
- Real -> Double
+- DataSets are moved from functions to core
### Deprecated
diff --git a/README.md b/README.md
index cc9439d27..7080c757e 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,8 @@
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
[![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382)
-
![Gradle build](https://github.com/mipt-npm/kmath/workflows/Gradle%20build/badge.svg)
-
-[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22%20AND%20a:%22kmath-core%22)
+[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22)
+[![Space](https://img.shields.io/maven-metadata/v?label=Space&metadataUrl=https%3A%2F%2Fmaven.pkg.jetbrains.space%2Fmipt-npm%2Fp%2Fsci%2Fmaven%2Fkscience%2Fkmath%2Fkmath-core%2Fmaven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/)
# KMath
@@ -147,6 +146,12 @@ performance calculations to code generation.
>
>
> **Maturity**: PROTOTYPE
+>
+> **Features:**
+> - [ejml-vector](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt) : The Point implementation using SimpleMatrix.
+> - [ejml-matrix](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt) : The Matrix implementation using SimpleMatrix.
+> - [ejml-linear-space](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt) : The LinearSpace implementation using SimpleMatrix.
+
* ### [kmath-for-real](kmath-for-real)
diff --git a/build.gradle.kts b/build.gradle.kts
index 5f1a8b88a..cc863a957 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,4 +1,6 @@
+import org.jetbrains.dokka.gradle.DokkaTask
import ru.mipt.npm.gradle.KSciencePublishingPlugin
+import java.net.URL
plugins {
id("ru.mipt.npm.gradle.project")
@@ -10,21 +12,38 @@ allprojects {
maven("https://clojars.org/repo")
maven("https://dl.bintray.com/egor-bogomolov/astminer/")
maven("https://dl.bintray.com/hotkeytlt/maven")
- maven("https://dl.bintray.com/kotlin/kotlin-eap")
- maven("https://dl.bintray.com/kotlin/kotlinx")
- maven("https://dl.bintray.com/mipt-npm/dev")
- maven("https://dl.bintray.com/mipt-npm/kscience")
maven("https://jitpack.io")
maven("http://logicrunch.research.it.uu.se/maven/")
mavenCentral()
}
group = "space.kscience"
- version = "0.3.0-dev-3"
+ version = "0.3.0-dev-4"
}
subprojects {
if (name.startsWith("kmath")) apply()
+
+ afterEvaluate {
+ tasks.withType {
+ dokkaSourceSets.all {
+ val readmeFile = File(this@subprojects.projectDir, "./README.md")
+ if (readmeFile.exists())
+ includes.setFrom(includes + readmeFile.absolutePath)
+
+ arrayOf(
+ "http://ejml.org/javadoc/",
+ "https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/",
+ "https://deeplearning4j.org/api/latest/"
+ ).map { URL("${it}package-list") to URL(it) }.forEach { (a, b) ->
+ externalDocumentationLink {
+ packageListUrl.set(a)
+ url.set(b)
+ }
+ }
+ }
+ }
+ }
}
readme {
diff --git a/docs/templates/ARTIFACT-TEMPLATE.md b/docs/templates/ARTIFACT-TEMPLATE.md
index cb741bc6f..01d9c51da 100644
--- a/docs/templates/ARTIFACT-TEMPLATE.md
+++ b/docs/templates/ARTIFACT-TEMPLATE.md
@@ -1,34 +1,28 @@
-> #### Artifact:
->
-> This module artifact: `${group}:${name}:${version}`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/${name}/images/download.svg) ](https://bintray.com/mipt-npm/kscience/${name}/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/${name}/images/download.svg) ](https://bintray.com/mipt-npm/dev/${name}/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation '${group}:${name}:${version}'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("${group}:${name}:${version}")
-> }
-> ```
\ No newline at end of file
+## Artifact:
+
+The Maven coordinates of this project are `${group}:${name}:${version}`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation '${group}:${name}:${version}'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("${group}:${name}:${version}")
+}
+```
\ No newline at end of file
diff --git a/docs/templates/README-TEMPLATE.md b/docs/templates/README-TEMPLATE.md
index 4366c8fcd..3502cdccd 100644
--- a/docs/templates/README-TEMPLATE.md
+++ b/docs/templates/README-TEMPLATE.md
@@ -1,9 +1,8 @@
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
[![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382)
-
![Gradle build](https://github.com/mipt-npm/kmath/workflows/Gradle%20build/badge.svg)
-
-[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22%20AND%20a:%22kmath-core%22)
+[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22)
+[![Space](https://img.shields.io/maven-metadata/v?label=Space&metadataUrl=https%3A%2F%2Fmaven.pkg.jetbrains.space%2Fmipt-npm%2Fp%2Fsci%2Fmaven%2Fkscience%2Fkmath%2Fkmath-core%2Fmaven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/)
# KMath
diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts
index 5dd40b609..a48b4d0d9 100644
--- a/examples/build.gradle.kts
+++ b/examples/build.gradle.kts
@@ -106,6 +106,7 @@ kotlin.sourceSets.all {
with(languageSettings) {
useExperimentalAnnotation("kotlin.contracts.ExperimentalContracts")
useExperimentalAnnotation("kotlin.ExperimentalUnsignedTypes")
+ useExperimentalAnnotation("space.kscience.kmath.misc.UnstableKMathAPI")
}
}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
index 0899241f9..2438e3979 100644
--- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
@@ -9,7 +9,7 @@ import space.kscience.kmath.ast.mstInField
import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.expressionInField
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.expressions.symbol
+import space.kscience.kmath.misc.symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.bindSymbol
import kotlin.random.Random
diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt
index 23c9d5b41..138b3e708 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt
@@ -3,8 +3,8 @@ package space.kscience.kmath.ast
import space.kscience.kmath.asm.compile
import space.kscience.kmath.expressions.derivative
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.expressions.symbol
import space.kscience.kmath.kotlingrad.differentiable
+import space.kscience.kmath.misc.symbol
import space.kscience.kmath.operations.DoubleField
/**
diff --git a/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt b/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt
index ef0c29a2d..04c55b34c 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt
@@ -7,11 +7,14 @@ import kscience.plotly.models.ScatterMode
import kscience.plotly.models.TraceValues
import space.kscience.kmath.commons.optimization.chiSquared
import space.kscience.kmath.commons.optimization.minimize
-import space.kscience.kmath.expressions.symbol
+import space.kscience.kmath.misc.symbol
+import space.kscience.kmath.optimization.FunctionOptimization
+import space.kscience.kmath.optimization.OptimizationResult
import space.kscience.kmath.real.DoubleVector
import space.kscience.kmath.real.map
import space.kscience.kmath.real.step
-import space.kscience.kmath.stat.*
+import space.kscience.kmath.stat.RandomGenerator
+import space.kscience.kmath.stat.distributions.NormalDistribution
import space.kscience.kmath.structures.asIterable
import space.kscience.kmath.structures.toList
import kotlin.math.pow
@@ -33,10 +36,9 @@ operator fun TraceValues.invoke(vector: DoubleVector) {
/**
* Least squares fie with auto-differentiation. Uses `kmath-commons` and `kmath-for-real` modules.
*/
-fun main() {
-
+suspend fun main() {
//A generator for a normally distributed values
- val generator = Distribution.normal()
+ val generator = NormalDistribution(2.0, 7.0)
//A chain/flow of random values with the given seed
val chain = generator.sample(RandomGenerator.default(112667))
@@ -49,7 +51,7 @@ fun main() {
//Perform an operation on each x value (much more effective, than numpy)
val y = x.map {
val value = it.pow(2) + it + 1
- value + chain.nextDouble() * sqrt(value)
+ value + chain.next() * sqrt(value)
}
// this will also work, but less effective:
// val y = x.pow(2)+ x + 1 + chain.nextDouble()
@@ -58,7 +60,7 @@ fun main() {
val yErr = y.map { sqrt(it) }//RealVector.same(x.size, sigma)
// compute differentiable chi^2 sum for given model ax^2 + bx + c
- val chi2 = Fitting.chiSquared(x, y, yErr) { x1 ->
+ val chi2 = FunctionOptimization.chiSquared(x, y, yErr) { x1 ->
//bind variables to autodiff context
val a = bind(a)
val b = bind(b)
@@ -99,4 +101,4 @@ fun main() {
}
page.makeFile()
-}
\ No newline at end of file
+}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt
index 1761ed1b5..bfd138502 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt
@@ -1,23 +1,24 @@
-package kscience.kmath.commons.prob
+package space.kscience.kmath.stat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
-import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler
+import space.kscience.kmath.stat.samplers.GaussianSampler
import org.apache.commons.rng.simple.RandomSource
-import space.kscience.kmath.stat.*
import java.time.Duration
import java.time.Instant
+import org.apache.commons.rng.sampling.distribution.GaussianSampler as CMGaussianSampler
+import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler as CMZigguratNormalizedGaussianSampler
-private fun runChain(): Duration {
+private suspend fun runKMathChained(): Duration {
val generator = RandomGenerator.fromSource(RandomSource.MT, 123L)
- val normal = Distribution.normal(NormalSamplerMethod.Ziggurat)
- val chain = normal.sample(generator)
+ val normal = GaussianSampler.of(7.0, 2.0)
+ val chain = normal.sample(generator).blocking()
val startTime = Instant.now()
var sum = 0.0
repeat(10000001) { counter ->
- sum += chain.nextDouble()
+ sum += chain.next()
if (counter % 100000 == 0) {
val duration = Duration.between(startTime, Instant.now())
@@ -29,9 +30,15 @@ private fun runChain(): Duration {
return Duration.between(startTime, Instant.now())
}
-private fun runDirect(): Duration {
- val provider = RandomSource.create(RandomSource.MT, 123L)
- val sampler = ZigguratNormalizedGaussianSampler(provider)
+private fun runApacheDirect(): Duration {
+ val rng = RandomSource.create(RandomSource.MT, 123L)
+
+ val sampler = CMGaussianSampler.of(
+ CMZigguratNormalizedGaussianSampler.of(rng),
+ 7.0,
+ 2.0
+ )
+
val startTime = Instant.now()
var sum = 0.0
@@ -51,11 +58,9 @@ private fun runDirect(): Duration {
/**
* Comparing chain sampling performance with direct sampling performance
*/
-fun main() {
- runBlocking(Dispatchers.Default) {
- val chainJob = async { runChain() }
- val directJob = async { runDirect() }
- println("Chain: ${chainJob.await()}")
- println("Direct: ${directJob.await()}")
- }
+fun main(): Unit = runBlocking(Dispatchers.Default) {
+ val chainJob = async { runKMathChained() }
+ val directJob = async { runApacheDirect() }
+ println("KMath Chained: ${chainJob.await()}")
+ println("Apache Direct: ${directJob.await()}")
}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt
index 47b8d8111..aac7d51d4 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt
@@ -3,14 +3,15 @@ package space.kscience.kmath.stat
import kotlinx.coroutines.runBlocking
import space.kscience.kmath.chains.Chain
import space.kscience.kmath.chains.collectWithState
+import space.kscience.kmath.stat.distributions.NormalDistribution
/**
- * The state of distribution averager
+ * The state of distribution averager.
*/
private data class AveragingChainState(var num: Int = 0, var value: Double = 0.0)
/**
- * Averaging
+ * Averaging.
*/
private fun Chain.mean(): Chain = collectWithState(AveragingChainState(), { it.copy() }) { chain ->
val next = chain.next()
@@ -21,7 +22,7 @@ private fun Chain.mean(): Chain = collectWithState(AveragingChai
fun main() {
- val normal = Distribution.normal()
+ val normal = NormalDistribution(0.0, 2.0)
val chain = normal.sample(RandomGenerator.default).mean()
runBlocking {
@@ -32,4 +33,4 @@ fun main() {
}
}
}
-}
\ No newline at end of file
+}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt
index 1c8a923c7..8b0a2ed0e 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt
@@ -34,4 +34,4 @@ fun main() {
strides.indices().forEach { res = array[strides.offset(it)] }
}
println("Array reading finished in $time3 millis")
-}
\ No newline at end of file
+}
diff --git a/gradle.properties b/gradle.properties
index 7ff50a435..50123b16c 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -4,5 +4,5 @@ kotlin.mpp.stability.nowarn=true
kotlin.native.enableDependencyPropagation=false
kotlin.parallel.tasks.in.project=true
org.gradle.configureondemand=true
-org.gradle.jvmargs=-XX:MaxMetaspaceSize=2G
+org.gradle.jvmargs=-XX:MaxMetaspaceSize=9G
org.gradle.parallel=true
diff --git a/kmath-ast/README.md b/kmath-ast/README.md
index ee14604d2..ff954b914 100644
--- a/kmath-ast/README.md
+++ b/kmath-ast/README.md
@@ -1,6 +1,6 @@
-# Abstract Syntax Tree Expression Representation and Operations (`kmath-ast`)
+# Module kmath-ast
-This subproject implements the following features:
+Abstract syntax tree expression representation and related optimizations.
- [expression-language](src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt) : Expression language and its parser
- [mst](src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt) : MST (Mathematical Syntax Tree) as expression language's syntax intermediate representation
@@ -10,40 +10,34 @@ This subproject implements the following features:
- [mst-js-codegen](src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt) : Dynamic MST to JS compiler
-> #### Artifact:
->
-> This module artifact: `space.kscience:kmath-ast:0.3.0-dev-3`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-ast/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-ast/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-ast/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-ast/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation 'space.kscience:kmath-ast:0.3.0-dev-3'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("space.kscience:kmath-ast:0.3.0-dev-3")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-ast:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-ast:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-ast:0.3.0-dev-3")
+}
+```
## Dynamic expression code generation
diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md
index 80e48008b..db071adb4 100644
--- a/kmath-ast/docs/README-TEMPLATE.md
+++ b/kmath-ast/docs/README-TEMPLATE.md
@@ -1,6 +1,6 @@
-# Abstract Syntax Tree Expression Representation and Operations (`kmath-ast`)
+# Module kmath-ast
-This subproject implements the following features:
+Abstract syntax tree expression representation and related optimizations.
${features}
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
index 63dfb38f7..5c43df068 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
@@ -1,6 +1,8 @@
package space.kscience.kmath.ast
import space.kscience.kmath.expressions.*
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.operations.*
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
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 b4de9968d..1e966d986 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
@@ -3,7 +3,7 @@ package space.kscience.kmath.estree.internal
import space.kscience.kmath.estree.internal.astring.generate
import space.kscience.kmath.estree.internal.estree.*
import space.kscience.kmath.expressions.Expression
-import space.kscience.kmath.expressions.Symbol
+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/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
index f54bc070c..0a0c21d8a 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
@@ -2,8 +2,8 @@
package space.kscience.kmath.asm.internal
-import space.kscience.kmath.expressions.StringSymbol
-import space.kscience.kmath.expressions.Symbol
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
/**
* Gets value with given [key] or throws [NoSuchElementException] whenever it is not present.
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 b74167c3f..58e9687e5 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
@@ -2,6 +2,8 @@ package space.kscience.kmath.commons.expressions
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
import space.kscience.kmath.expressions.*
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.ExtendedField
import space.kscience.kmath.operations.NumbersAddOperations
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 89e9649e9..80929e6b9 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
@@ -3,6 +3,7 @@ package space.kscience.kmath.commons.linear
import org.apache.commons.math3.linear.*
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.DoubleBuffer
import kotlin.reflect.KClass
@@ -89,7 +90,7 @@ public object CMLinearSpace : LinearSpace {
v * this
@UnstableKMathAPI
- override fun getFeature(structure: Matrix, type: KClass): F? {
+ override fun getFeature(structure: Matrix, type: KClass): F? {
//Return the feature if it is intrinsic to the structure
structure.getFeature(type)?.let { return it }
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizationProblem.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt
similarity index 64%
rename from kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizationProblem.kt
rename to kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt
index 13a10475f..444c505c9 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimizationProblem.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/CMOptimization.kt
@@ -9,22 +9,36 @@ import org.apache.commons.math3.optim.nonlinear.scalar.gradient.NonLinearConjuga
import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.AbstractSimplex
import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex
import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer
-import space.kscience.kmath.expressions.*
-import space.kscience.kmath.stat.OptimizationFeature
-import space.kscience.kmath.stat.OptimizationProblem
-import space.kscience.kmath.stat.OptimizationProblemFactory
-import space.kscience.kmath.stat.OptimizationResult
+import space.kscience.kmath.expressions.DifferentiableExpression
+import space.kscience.kmath.expressions.Expression
+import space.kscience.kmath.expressions.SymbolIndexer
+import space.kscience.kmath.expressions.derivative
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.optimization.*
import kotlin.reflect.KClass
public operator fun PointValuePair.component1(): DoubleArray = point
public operator fun PointValuePair.component2(): Double = value
-public class CMOptimizationProblem(override val symbols: List) :
- OptimizationProblem, SymbolIndexer, OptimizationFeature {
+@OptIn(UnstableKMathAPI::class)
+public class CMOptimization(
+ override val symbols: List,
+) : FunctionOptimization, NoDerivFunctionOptimization, SymbolIndexer, OptimizationFeature {
+
private val optimizationData: HashMap, OptimizationData> = HashMap()
- private var optimizatorBuilder: (() -> MultivariateOptimizer)? = null
- public var convergenceChecker: ConvergenceChecker = SimpleValueChecker(DEFAULT_RELATIVE_TOLERANCE,
- DEFAULT_ABSOLUTE_TOLERANCE, DEFAULT_MAX_ITER)
+ private var optimizerBuilder: (() -> MultivariateOptimizer)? = null
+ public var convergenceChecker: ConvergenceChecker = SimpleValueChecker(
+ DEFAULT_RELATIVE_TOLERANCE,
+ DEFAULT_ABSOLUTE_TOLERANCE,
+ DEFAULT_MAX_ITER
+ )
+
+ override var maximize: Boolean
+ get() = optimizationData[GoalType::class] == GoalType.MAXIMIZE
+ set(value) {
+ optimizationData[GoalType::class] = if (value) GoalType.MAXIMIZE else GoalType.MINIMIZE
+ }
public fun addOptimizationData(data: OptimizationData) {
optimizationData[data::class] = data
@@ -40,7 +54,7 @@ public class CMOptimizationProblem(override val symbols: List) :
addOptimizationData(InitialGuess(map.toDoubleArray()))
}
- public override fun expression(expression: Expression): Unit {
+ public override fun function(expression: Expression): Unit {
val objectiveFunction = ObjectiveFunction {
val args = it.toMap()
expression(args)
@@ -48,8 +62,8 @@ public class CMOptimizationProblem(override val symbols: List) :
addOptimizationData(objectiveFunction)
}
- public override fun diffExpression(expression: DifferentiableExpression>) {
- expression(expression)
+ public override fun diffFunction(expression: DifferentiableExpression>) {
+ function(expression)
val gradientFunction = ObjectiveFunctionGradient {
val args = it.toMap()
DoubleArray(symbols.size) { index ->
@@ -57,8 +71,8 @@ public class CMOptimizationProblem(override val symbols: List) :
}
}
addOptimizationData(gradientFunction)
- if (optimizatorBuilder == null) {
- optimizatorBuilder = {
+ if (optimizerBuilder == null) {
+ optimizerBuilder = {
NonLinearConjugateGradientOptimizer(
NonLinearConjugateGradientOptimizer.Formula.FLETCHER_REEVES,
convergenceChecker
@@ -70,8 +84,8 @@ public class CMOptimizationProblem(override val symbols: List) :
public fun simplex(simplex: AbstractSimplex) {
addOptimizationData(simplex)
//Set optimization builder to simplex if it is not present
- if (optimizatorBuilder == null) {
- optimizatorBuilder = { SimplexOptimizer(convergenceChecker) }
+ if (optimizerBuilder == null) {
+ optimizerBuilder = { SimplexOptimizer(convergenceChecker) }
}
}
@@ -84,7 +98,7 @@ public class CMOptimizationProblem(override val symbols: List) :
}
public fun optimizer(block: () -> MultivariateOptimizer) {
- optimizatorBuilder = block
+ optimizerBuilder = block
}
override fun update(result: OptimizationResult) {
@@ -92,19 +106,19 @@ public class CMOptimizationProblem(override val symbols: List) :
}
override fun optimize(): OptimizationResult {
- val optimizer = optimizatorBuilder?.invoke() ?: error("Optimizer not defined")
+ val optimizer = optimizerBuilder?.invoke() ?: error("Optimizer not defined")
val (point, value) = optimizer.optimize(*optimizationData.values.toTypedArray())
return OptimizationResult(point.toMap(), value, setOf(this))
}
- public companion object : OptimizationProblemFactory {
+ public companion object : OptimizationProblemFactory {
public const val DEFAULT_RELATIVE_TOLERANCE: Double = 1e-4
public const val DEFAULT_ABSOLUTE_TOLERANCE: Double = 1e-4
public const val DEFAULT_MAX_ITER: Int = 1000
- override fun build(symbols: List): CMOptimizationProblem = CMOptimizationProblem(symbols)
+ override fun build(symbols: List): CMOptimization = CMOptimization(symbols)
}
}
-public fun CMOptimizationProblem.initialGuess(vararg pairs: Pair): Unit = initialGuess(pairs.toMap())
-public fun CMOptimizationProblem.simplexSteps(vararg pairs: Pair): Unit = simplexSteps(pairs.toMap())
+public fun CMOptimization.initialGuess(vararg pairs: Pair): Unit = initialGuess(pairs.toMap())
+public fun CMOptimization.simplexSteps(vararg pairs: Pair): Unit = simplexSteps(pairs.toMap())
diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/cmFit.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/cmFit.kt
index 5ecd5b756..f84dae693 100644
--- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/cmFit.kt
+++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/optimization/cmFit.kt
@@ -1,21 +1,21 @@
package space.kscience.kmath.commons.optimization
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
-import org.apache.commons.math3.optim.nonlinear.scalar.GoalType
import space.kscience.kmath.commons.expressions.DerivativeStructureField
import space.kscience.kmath.expressions.DifferentiableExpression
import space.kscience.kmath.expressions.Expression
-import space.kscience.kmath.expressions.Symbol
-import space.kscience.kmath.stat.Fitting
-import space.kscience.kmath.stat.OptimizationResult
-import space.kscience.kmath.stat.optimizeWith
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.optimization.FunctionOptimization
+import space.kscience.kmath.optimization.OptimizationResult
+import space.kscience.kmath.optimization.noDerivOptimizeWith
+import space.kscience.kmath.optimization.optimizeWith
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asBuffer
/**
* Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation
*/
-public fun Fitting.chiSquared(
+public fun FunctionOptimization.Companion.chiSquared(
x: Buffer,
y: Buffer,
yErr: Buffer,
@@ -25,7 +25,7 @@ public fun Fitting.chiSquared(
/**
* Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation
*/
-public fun Fitting.chiSquared(
+public fun FunctionOptimization.Companion.chiSquared(
x: Iterable,
y: Iterable,
yErr: Iterable,
@@ -43,25 +43,26 @@ public fun Fitting.chiSquared(
*/
public fun Expression.optimize(
vararg symbols: Symbol,
- configuration: CMOptimizationProblem.() -> Unit,
-): OptimizationResult = optimizeWith(CMOptimizationProblem, symbols = symbols, configuration)
+ configuration: CMOptimization.() -> Unit,
+): OptimizationResult = noDerivOptimizeWith(CMOptimization, symbols = symbols, configuration)
/**
* Optimize differentiable expression
*/
public fun DifferentiableExpression>.optimize(
vararg symbols: Symbol,
- configuration: CMOptimizationProblem.() -> Unit,
-): OptimizationResult = optimizeWith(CMOptimizationProblem, symbols = symbols, configuration)
+ configuration: CMOptimization.() -> Unit,
+): OptimizationResult = optimizeWith(CMOptimization, symbols = symbols, configuration)
public fun DifferentiableExpression>.minimize(
vararg startPoint: Pair,
- configuration: CMOptimizationProblem.() -> Unit = {},
+ configuration: CMOptimization.() -> Unit = {},
): OptimizationResult {
- require(startPoint.isNotEmpty()) { "Must provide a list of symbols for optimization" }
- val problem = CMOptimizationProblem(startPoint.map { it.first }).apply(configuration)
- problem.diffExpression(this)
- problem.initialGuess(startPoint.toMap())
- problem.goal(GoalType.MINIMIZE)
- return problem.optimize()
+ val symbols = startPoint.map { it.first }.toTypedArray()
+ return optimize(*symbols){
+ maximize = false
+ initialGuess(startPoint.toMap())
+ diffFunction(this@minimize)
+ configuration()
+ }
}
\ No newline at end of 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 8d9bab652..b19eb5950 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,6 +1,10 @@
package space.kscience.kmath.commons.expressions
-import space.kscience.kmath.expressions.*
+import space.kscience.kmath.expressions.binding
+import space.kscience.kmath.expressions.derivative
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.symbol
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.test.Test
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 d29934a4d..36f2639f4 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,13 +1,13 @@
package space.kscience.kmath.commons.optimization
-import org.junit.jupiter.api.Test
+import kotlinx.coroutines.runBlocking
import space.kscience.kmath.commons.expressions.DerivativeStructureExpression
-import space.kscience.kmath.expressions.symbol
-import space.kscience.kmath.stat.Distribution
-import space.kscience.kmath.stat.Fitting
+import space.kscience.kmath.misc.symbol
+import space.kscience.kmath.optimization.FunctionOptimization
import space.kscience.kmath.stat.RandomGenerator
-import space.kscience.kmath.stat.normal
+import space.kscience.kmath.stat.distributions.NormalDistribution
import kotlin.math.pow
+import kotlin.test.Test
internal class OptimizeTest {
val x by symbol
@@ -34,28 +34,29 @@ internal class OptimizeTest {
simplexSteps(x to 2.0, y to 0.5)
//this sets simplex optimizer
}
+
println(result.point)
println(result.value)
}
@Test
- fun testCmFit() {
+ fun testCmFit() = runBlocking {
val a by symbol
val b by symbol
val c by symbol
val sigma = 1.0
- val generator = Distribution.normal(0.0, sigma)
+ val generator = NormalDistribution(0.0, sigma)
val chain = generator.sample(RandomGenerator.default(112667))
val x = (1..100).map(Int::toDouble)
val y = x.map {
- it.pow(2) + it + 1 + chain.nextDouble()
+ it.pow(2) + it + 1 + chain.next()
}
val yErr = List(x.size) { sigma }
- val chi2 = Fitting.chiSquared(x, y, yErr) { x1 ->
+ val chi2 = FunctionOptimization.chiSquared(x, y, yErr) { x1 ->
val cWithDefault = bindSymbolOrNull(c) ?: one
bind(a) * x1.pow(2) + bind(b) * x1 + cWithDefault
}
@@ -64,5 +65,4 @@ internal class OptimizeTest {
println(result)
println("Chi2/dof = ${result.value / (x.size - 3)}")
}
-
-}
\ No newline at end of file
+}
diff --git a/kmath-complex/README.md b/kmath-complex/README.md
index 9e9cd5b6f..d7b2937fd 100644
--- a/kmath-complex/README.md
+++ b/kmath-complex/README.md
@@ -1,42 +1,36 @@
-# The Core Module (`kmath-core`)
+# Module kmath-complex
-Complex and hypercomplex number systems in KMath:
+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
-> #### Artifact:
->
-> This module artifact: `space.kscience:kmath-complex:0.3.0-dev-3`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-complex/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-complex/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-complex/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-complex/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation 'space.kscience:kmath-complex:0.3.0-dev-3'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("space.kscience:kmath-complex:0.3.0-dev-3")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-complex:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-complex:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-complex:0.3.0-dev-3")
+}
+```
diff --git a/kmath-complex/docs/README-TEMPLATE.md b/kmath-complex/docs/README-TEMPLATE.md
index 462fd617e..106d4aff1 100644
--- a/kmath-complex/docs/README-TEMPLATE.md
+++ b/kmath-complex/docs/README-TEMPLATE.md
@@ -1,6 +1,6 @@
-# The Core Module (`kmath-core`)
+# Module kmath-complex
-Complex and hypercomplex number systems in KMath:
+Complex and hypercomplex number systems in KMath.
${features}
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 81a131318..3837b0d40 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
@@ -3,7 +3,7 @@ package space.kscience.kmath.complex
import space.kscience.kmath.expressions.FunctionalExpressionField
import space.kscience.kmath.expressions.bindSymbol
import space.kscience.kmath.expressions.invoke
-import space.kscience.kmath.expressions.symbol
+import space.kscience.kmath.misc.symbol
import kotlin.test.Test
import kotlin.test.assertEquals
diff --git a/kmath-core/README.md b/kmath-core/README.md
index 4e4b5273d..096c7d833 100644
--- a/kmath-core/README.md
+++ b/kmath-core/README.md
@@ -1,6 +1,6 @@
-# The Core Module (`kmath-core`)
+# Module kmath-core
-The core features of KMath:
+The core interfaces of KMath.
- [algebras](src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt) : Algebraic structures like rings, spaces and fields.
- [nd](src/commonMain/kotlin/space/kscience/kmath/structures/StructureND.kt) : Many-dimensional structures and operations on them.
@@ -13,37 +13,31 @@ performance calculations to code generation.
- [autodif](src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt) : Automatic differentiation
-> #### Artifact:
->
-> This module artifact: `space.kscience:kmath-core:0.3.0-dev-3`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-core/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-core/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-core/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-core/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation 'space.kscience:kmath-core:0.3.0-dev-3'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("space.kscience:kmath-core:0.3.0-dev-3")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-core:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-core:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-core:0.3.0-dev-3")
+}
+```
diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api
index f0e749d2b..cc6c93c20 100644
--- a/kmath-core/api/kmath-core.api
+++ b/kmath-core/api/kmath-core.api
@@ -1,3 +1,18 @@
+public final class space/kscience/kmath/data/ColumnarDataKt {
+}
+
+public final class space/kscience/kmath/data/XYColumnarData$DefaultImpls {
+ public static fun get (Lspace/kscience/kmath/data/XYColumnarData;Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/structures/Buffer;
+}
+
+public final class space/kscience/kmath/data/XYColumnarDataKt {
+ public static synthetic fun asXYData$default (Lspace/kscience/kmath/nd/Structure2D;IIILjava/lang/Object;)Lspace/kscience/kmath/data/XYColumnarData;
+}
+
+public final class space/kscience/kmath/data/XYZColumnarData$DefaultImpls {
+ public static fun get (Lspace/kscience/kmath/data/XYZColumnarData;Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/structures/Buffer;
+}
+
public abstract interface class space/kscience/kmath/domains/Domain {
public abstract fun contains (Lspace/kscience/kmath/structures/Buffer;)Z
public abstract fun getDimension ()I
@@ -14,7 +29,7 @@ public class space/kscience/kmath/expressions/AutoDiffValue {
public final class space/kscience/kmath/expressions/DerivationResult {
public fun (Ljava/lang/Object;Ljava/util/Map;Lspace/kscience/kmath/operations/Field;)V
- public final fun derivative (Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
+ public final fun derivative (Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
public final fun div ()Ljava/lang/Object;
public final fun getContext ()Lspace/kscience/kmath/operations/Field;
public final fun getValue ()Ljava/lang/Object;
@@ -27,7 +42,7 @@ public abstract interface class space/kscience/kmath/expressions/DifferentiableE
public final class space/kscience/kmath/expressions/DifferentiableExpressionKt {
public static final fun derivative (Lspace/kscience/kmath/expressions/DifferentiableExpression;Ljava/lang/String;)Lspace/kscience/kmath/expressions/Expression;
public static final fun derivative (Lspace/kscience/kmath/expressions/DifferentiableExpression;Ljava/util/List;)Lspace/kscience/kmath/expressions/Expression;
- public static final fun derivative (Lspace/kscience/kmath/expressions/DifferentiableExpression;[Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/expressions/Expression;
+ public static final fun derivative (Lspace/kscience/kmath/expressions/DifferentiableExpression;[Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/expressions/Expression;
}
public abstract interface class space/kscience/kmath/expressions/Expression {
@@ -36,7 +51,7 @@ public abstract interface class space/kscience/kmath/expressions/Expression {
public abstract interface class space/kscience/kmath/expressions/ExpressionAlgebra : space/kscience/kmath/operations/Algebra {
public abstract fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object;
- public abstract fun bindSymbolOrNull (Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
+ public abstract fun bindSymbolOrNull (Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
public abstract fun const (Ljava/lang/Object;)Ljava/lang/Object;
}
@@ -56,18 +71,17 @@ public final class space/kscience/kmath/expressions/ExpressionBuildersKt {
}
public final class space/kscience/kmath/expressions/ExpressionKt {
- public static final fun bindSymbol (Lspace/kscience/kmath/expressions/ExpressionAlgebra;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
+ public static final fun bindSymbol (Lspace/kscience/kmath/expressions/ExpressionAlgebra;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
public static final fun binding (Lspace/kscience/kmath/expressions/ExpressionAlgebra;)Lkotlin/properties/ReadOnlyProperty;
public static final fun callByString (Lspace/kscience/kmath/expressions/Expression;[Lkotlin/Pair;)Ljava/lang/Object;
public static final fun callBySymbol (Lspace/kscience/kmath/expressions/Expression;[Lkotlin/Pair;)Ljava/lang/Object;
- public static final fun getSymbol ()Lkotlin/properties/ReadOnlyProperty;
public static final fun invoke (Lspace/kscience/kmath/expressions/Expression;)Ljava/lang/Object;
}
public abstract class space/kscience/kmath/expressions/FirstDerivativeExpression : space/kscience/kmath/expressions/DifferentiableExpression {
public fun ()V
public final fun derivativeOrNull (Ljava/util/List;)Lspace/kscience/kmath/expressions/Expression;
- public abstract fun derivativeOrNull (Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/expressions/Expression;
+ public abstract fun derivativeOrNull (Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/expressions/Expression;
}
public abstract class space/kscience/kmath/expressions/FunctionalExpressionAlgebra : space/kscience/kmath/expressions/ExpressionAlgebra {
@@ -77,8 +91,8 @@ public abstract class space/kscience/kmath/expressions/FunctionalExpressionAlgeb
public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2;
public synthetic fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object;
public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/expressions/Expression;
- public synthetic fun bindSymbolOrNull (Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public fun bindSymbolOrNull (Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/expressions/Expression;
+ public synthetic fun bindSymbolOrNull (Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
+ public fun bindSymbolOrNull (Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/expressions/Expression;
public synthetic fun const (Ljava/lang/Object;)Ljava/lang/Object;
public fun const (Ljava/lang/Object;)Lspace/kscience/kmath/expressions/Expression;
public final fun getAlgebra ()Lspace/kscience/kmath/operations/Algebra;
@@ -203,7 +217,7 @@ public class space/kscience/kmath/expressions/FunctionalExpressionRing : space/k
public final class space/kscience/kmath/expressions/SimpleAutoDiffExpression : space/kscience/kmath/expressions/FirstDerivativeExpression {
public fun (Lspace/kscience/kmath/operations/Field;Lkotlin/jvm/functions/Function1;)V
- public fun derivativeOrNull (Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/expressions/Expression;
+ public fun derivativeOrNull (Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/expressions/Expression;
public final fun getField ()Lspace/kscience/kmath/operations/Field;
public final fun getFunction ()Lkotlin/jvm/functions/Function1;
public fun invoke (Ljava/util/Map;)Ljava/lang/Object;
@@ -264,8 +278,8 @@ public class space/kscience/kmath/expressions/SimpleAutoDiffField : space/kscien
public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2;
public synthetic fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object;
public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/expressions/AutoDiffValue;
- public synthetic fun bindSymbolOrNull (Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public fun bindSymbolOrNull (Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/expressions/AutoDiffValue;
+ public synthetic fun bindSymbolOrNull (Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
+ public fun bindSymbolOrNull (Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/expressions/AutoDiffValue;
public synthetic fun const (Ljava/lang/Object;)Ljava/lang/Object;
public fun const (Ljava/lang/Object;)Lspace/kscience/kmath/expressions/AutoDiffValue;
public final fun const (Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/AutoDiffValue;
@@ -332,7 +346,7 @@ public final class space/kscience/kmath/expressions/SimpleAutoDiffKt {
public static final fun cos (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;)Lspace/kscience/kmath/expressions/AutoDiffValue;
public static final fun cosh (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;)Lspace/kscience/kmath/expressions/AutoDiffValue;
public static final fun exp (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;)Lspace/kscience/kmath/expressions/AutoDiffValue;
- public static final fun grad (Lspace/kscience/kmath/expressions/DerivationResult;[Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/structures/Buffer;
+ public static final fun grad (Lspace/kscience/kmath/expressions/DerivationResult;[Lspace/kscience/kmath/misc/Symbol;)Lspace/kscience/kmath/structures/Buffer;
public static final fun ln (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;)Lspace/kscience/kmath/expressions/AutoDiffValue;
public static final fun pow (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;D)Lspace/kscience/kmath/expressions/AutoDiffValue;
public static final fun pow (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;I)Lspace/kscience/kmath/expressions/AutoDiffValue;
@@ -348,79 +362,13 @@ public final class space/kscience/kmath/expressions/SimpleAutoDiffKt {
public static final fun tanh (Lspace/kscience/kmath/expressions/SimpleAutoDiffField;Lspace/kscience/kmath/expressions/AutoDiffValue;)Lspace/kscience/kmath/expressions/AutoDiffValue;
}
-public final class space/kscience/kmath/expressions/SimpleSymbolIndexer : space/kscience/kmath/expressions/SymbolIndexer {
- public static final synthetic fun box-impl (Ljava/util/List;)Lspace/kscience/kmath/expressions/SimpleSymbolIndexer;
- public static fun constructor-impl (Ljava/util/List;)Ljava/util/List;
- public fun equals (Ljava/lang/Object;)Z
- public static fun equals-impl (Ljava/util/List;Ljava/lang/Object;)Z
- public static final fun equals-impl0 (Ljava/util/List;Ljava/util/List;)Z
- public fun get (Ljava/util/List;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public fun get (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/expressions/Symbol;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public fun get (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public fun get ([DLspace/kscience/kmath/expressions/Symbol;)D
- public fun get ([Ljava/lang/Object;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get-impl (Ljava/util/List;Ljava/util/List;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get-impl (Ljava/util/List;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/expressions/Symbol;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get-impl (Ljava/util/List;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get-impl (Ljava/util/List;[DLspace/kscience/kmath/expressions/Symbol;)D
- public static fun get-impl (Ljava/util/List;[Ljava/lang/Object;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public fun getSymbols ()Ljava/util/List;
- public fun hashCode ()I
- public static fun hashCode-impl (Ljava/util/List;)I
- public fun indexOf (Lspace/kscience/kmath/expressions/Symbol;)I
- public static fun indexOf-impl (Ljava/util/List;Lspace/kscience/kmath/expressions/Symbol;)I
- public fun toDoubleArray (Ljava/util/Map;)[D
- public static fun toDoubleArray-impl (Ljava/util/List;Ljava/util/Map;)[D
- public fun toList (Ljava/util/Map;)Ljava/util/List;
- public static fun toList-impl (Ljava/util/List;Ljava/util/Map;)Ljava/util/List;
- public fun toMap ([D)Ljava/util/Map;
- public static fun toMap-impl (Ljava/util/List;[D)Ljava/util/Map;
- public fun toPoint (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer;
- public static fun toPoint-impl (Ljava/util/List;Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer;
- public fun toString ()Ljava/lang/String;
- public static fun toString-impl (Ljava/util/List;)Ljava/lang/String;
- public final synthetic fun unbox-impl ()Ljava/util/List;
-}
-
-public final class space/kscience/kmath/expressions/StringSymbol : space/kscience/kmath/expressions/Symbol {
- public static final synthetic fun box-impl (Ljava/lang/String;)Lspace/kscience/kmath/expressions/StringSymbol;
- public static fun constructor-impl (Ljava/lang/String;)Ljava/lang/String;
- public fun equals (Ljava/lang/Object;)Z
- public static fun equals-impl (Ljava/lang/String;Ljava/lang/Object;)Z
- public static final fun equals-impl0 (Ljava/lang/String;Ljava/lang/String;)Z
- public fun getIdentity ()Ljava/lang/String;
- public fun hashCode ()I
- public static fun hashCode-impl (Ljava/lang/String;)I
- public fun toString ()Ljava/lang/String;
- public static fun toString-impl (Ljava/lang/String;)Ljava/lang/String;
- public final synthetic fun unbox-impl ()Ljava/lang/String;
-}
-
-public abstract interface class space/kscience/kmath/expressions/Symbol {
- public abstract fun getIdentity ()Ljava/lang/String;
-}
-
-public abstract interface class space/kscience/kmath/expressions/SymbolIndexer {
- public abstract fun get (Ljava/util/List;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public abstract fun get (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/expressions/Symbol;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public abstract fun get (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public abstract fun get ([DLspace/kscience/kmath/expressions/Symbol;)D
- public abstract fun get ([Ljava/lang/Object;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public abstract fun getSymbols ()Ljava/util/List;
- public abstract fun indexOf (Lspace/kscience/kmath/expressions/Symbol;)I
- public abstract fun toDoubleArray (Ljava/util/Map;)[D
- public abstract fun toList (Ljava/util/Map;)Ljava/util/List;
- public abstract fun toMap ([D)Ljava/util/Map;
- public abstract fun toPoint (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer;
-}
-
public final class space/kscience/kmath/expressions/SymbolIndexer$DefaultImpls {
- public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;Ljava/util/List;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/expressions/Symbol;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;[DLspace/kscience/kmath/expressions/Symbol;)D
- public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;[Ljava/lang/Object;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
- public static fun indexOf (Lspace/kscience/kmath/expressions/SymbolIndexer;Lspace/kscience/kmath/expressions/Symbol;)I
+ public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;Ljava/util/List;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
+ public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/misc/Symbol;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
+ public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
+ public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;[DLspace/kscience/kmath/misc/Symbol;)D
+ public static fun get (Lspace/kscience/kmath/expressions/SymbolIndexer;[Ljava/lang/Object;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
+ public static fun indexOf (Lspace/kscience/kmath/expressions/SymbolIndexer;Lspace/kscience/kmath/misc/Symbol;)I
public static fun toDoubleArray (Lspace/kscience/kmath/expressions/SymbolIndexer;Ljava/util/Map;)[D
public static fun toList (Lspace/kscience/kmath/expressions/SymbolIndexer;Ljava/util/Map;)Ljava/util/List;
public static fun toMap (Lspace/kscience/kmath/expressions/SymbolIndexer;[D)Ljava/util/Map;
@@ -428,8 +376,6 @@ public final class space/kscience/kmath/expressions/SymbolIndexer$DefaultImpls {
}
public final class space/kscience/kmath/expressions/SymbolIndexerKt {
- public static final fun withSymbols (Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
- public static final fun withSymbols ([Lspace/kscience/kmath/expressions/Symbol;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
}
public final class space/kscience/kmath/linear/BufferedLinearSpace : space/kscience/kmath/linear/LinearSpace {
@@ -575,7 +521,7 @@ public final class space/kscience/kmath/linear/MatrixBuilderKt {
public static final fun row (Lspace/kscience/kmath/linear/LinearSpace;[Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D;
}
-public abstract interface class space/kscience/kmath/linear/MatrixFeature {
+public abstract interface class space/kscience/kmath/linear/MatrixFeature : space/kscience/kmath/nd/StructureFeature {
}
public final class space/kscience/kmath/linear/MatrixFeaturesKt {
@@ -672,6 +618,35 @@ public final class space/kscience/kmath/misc/CumulativeKt {
public static final fun cumulativeSumOfLong (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
}
+public final class space/kscience/kmath/misc/StringSymbol : space/kscience/kmath/misc/Symbol {
+ public static final synthetic fun box-impl (Ljava/lang/String;)Lspace/kscience/kmath/misc/StringSymbol;
+ public static fun constructor-impl (Ljava/lang/String;)Ljava/lang/String;
+ public fun equals (Ljava/lang/Object;)Z
+ public static fun equals-impl (Ljava/lang/String;Ljava/lang/Object;)Z
+ public static final fun equals-impl0 (Ljava/lang/String;Ljava/lang/String;)Z
+ public fun getIdentity ()Ljava/lang/String;
+ public fun hashCode ()I
+ public static fun hashCode-impl (Ljava/lang/String;)I
+ public fun toString ()Ljava/lang/String;
+ public static fun toString-impl (Ljava/lang/String;)Ljava/lang/String;
+ public final synthetic fun unbox-impl ()Ljava/lang/String;
+}
+
+public abstract interface class space/kscience/kmath/misc/Symbol {
+ public static final field Companion Lspace/kscience/kmath/misc/Symbol$Companion;
+ public abstract fun getIdentity ()Ljava/lang/String;
+}
+
+public final class space/kscience/kmath/misc/Symbol$Companion {
+ public final fun getX-tWtZOCg ()Ljava/lang/String;
+ public final fun getY-tWtZOCg ()Ljava/lang/String;
+ public final fun getZ-tWtZOCg ()Ljava/lang/String;
+}
+
+public final class space/kscience/kmath/misc/SymbolKt {
+ public static final fun getSymbol ()Lkotlin/properties/ReadOnlyProperty;
+}
+
public abstract interface annotation class space/kscience/kmath/misc/UnstableKMathAPI : java/lang/annotation/Annotation {
}
@@ -1086,11 +1061,15 @@ public final class space/kscience/kmath/nd/Strides$DefaultImpls {
}
public abstract interface class space/kscience/kmath/nd/Structure1D : space/kscience/kmath/nd/StructureND, space/kscience/kmath/structures/Buffer {
+ public static final field Companion Lspace/kscience/kmath/nd/Structure1D$Companion;
public abstract fun get ([I)Ljava/lang/Object;
public abstract fun getDimension ()I
public abstract fun iterator ()Ljava/util/Iterator;
}
+public final class space/kscience/kmath/nd/Structure1D$Companion {
+}
+
public final class space/kscience/kmath/nd/Structure1D$DefaultImpls {
public static fun get (Lspace/kscience/kmath/nd/Structure1D;[I)Ljava/lang/Object;
public static fun getDimension (Lspace/kscience/kmath/nd/Structure1D;)I
@@ -1132,6 +1111,9 @@ public final class space/kscience/kmath/nd/Structure2DKt {
public static final fun as2D (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Structure2D;
}
+public abstract interface class space/kscience/kmath/nd/StructureFeature {
+}
+
public abstract interface class space/kscience/kmath/nd/StructureND {
public static final field Companion Lspace/kscience/kmath/nd/StructureND$Companion;
public abstract fun elements ()Lkotlin/sequences/Sequence;
@@ -1201,7 +1183,7 @@ public final class space/kscience/kmath/operations/AlgebraExtensionsKt {
}
public final class space/kscience/kmath/operations/AlgebraKt {
- public static final fun bindSymbol (Lspace/kscience/kmath/operations/Algebra;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object;
+ public static final fun bindSymbol (Lspace/kscience/kmath/operations/Algebra;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object;
public static final fun invoke (Lspace/kscience/kmath/operations/Algebra;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
}
@@ -2140,6 +2122,7 @@ public final class space/kscience/kmath/structures/BufferKt {
public final class space/kscience/kmath/structures/BufferOperationKt {
public static final fun asIterable (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Iterable;
public static final fun asSequence (Lspace/kscience/kmath/structures/Buffer;)Lkotlin/sequences/Sequence;
+ public static final fun fold (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
public static final fun toList (Lspace/kscience/kmath/structures/Buffer;)Ljava/util/List;
}
diff --git a/kmath-core/docs/README-TEMPLATE.md b/kmath-core/docs/README-TEMPLATE.md
index 83d1ebdce..41cfe1ccb 100644
--- a/kmath-core/docs/README-TEMPLATE.md
+++ b/kmath-core/docs/README-TEMPLATE.md
@@ -1,6 +1,6 @@
-# The Core Module (`kmath-core`)
+# Module kmath-core
-The core features of KMath:
+The core interfaces of KMath.
${features}
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
new file mode 100644
index 000000000..761255158
--- /dev/null
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt
@@ -0,0 +1,34 @@
+package space.kscience.kmath.data
+
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.nd.Structure2D
+import space.kscience.kmath.structures.Buffer
+
+/**
+ * A column-based data set with all columns of the same size (not necessary fixed in time).
+ * The column could be retrieved by a [get] operation.
+ */
+@UnstableKMathAPI
+public interface ColumnarData {
+ public val size: Int
+
+ public operator fun get(symbol: Symbol): Buffer
+}
+
+/**
+ * A zero-copy method to represent a [Structure2D] as a two-column x-y data.
+ * There could more than two columns in the structure.
+ */
+@UnstableKMathAPI
+public fun Structure2D.asColumnarData(mapping: Map): ColumnarData {
+ require(shape[1] >= mapping.maxOf { it.value }) { "Column index out of bounds" }
+ return object : ColumnarData {
+ override val size: Int get() = shape[0]
+ override fun get(symbol: Symbol): Buffer {
+ val index = mapping[symbol] ?: error("No column mapping for symbol $symbol")
+ return columns[index]
+ }
+ }
+}
+
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
new file mode 100644
index 000000000..15239bca1
--- /dev/null
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt
@@ -0,0 +1,55 @@
+package space.kscience.kmath.data
+
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.nd.Structure2D
+import space.kscience.kmath.structures.Buffer
+import kotlin.math.max
+
+/**
+ * The buffer of X values.
+ */
+@UnstableKMathAPI
+public interface XYColumnarData : ColumnarData {
+ /**
+ * The buffer of X values
+ */
+ public val x: Buffer
+
+ /**
+ * The buffer of Y values.
+ */
+ public val y: Buffer
+
+ override fun get(symbol: Symbol): Buffer = when (symbol) {
+ Symbol.x -> x
+ Symbol.y -> y
+ else -> error("A column for symbol $symbol not found")
+ }
+}
+
+@Suppress("FunctionName")
+@UnstableKMathAPI
+public fun XYColumnarData(x: Buffer, y: Buffer): XYColumnarData {
+ require(x.size == y.size) { "Buffer size mismatch. x buffer size is ${x.size}, y buffer size is ${y.size}" }
+ return object : XYColumnarData {
+ override val size: Int = x.size
+ override val x: Buffer = x
+ override val y: Buffer = y
+ }
+}
+
+
+/**
+ * A zero-copy method to represent a [Structure2D] as a two-column x-y data.
+ * There could more than two columns in the structure.
+ */
+@UnstableKMathAPI
+public fun Structure2D.asXYData(xIndex: Int = 0, yIndex: Int = 1): XYColumnarData {
+ require(shape[1] >= max(xIndex, yIndex)) { "Column index out of bounds" }
+ return object : XYColumnarData {
+ override val size: Int get() = this@asXYData.shape[0]
+ override val x: Buffer get() = columns[xIndex]
+ override val y: Buffer get() = columns[yIndex]
+ }
+}
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
new file mode 100644
index 000000000..f74c6e2d6
--- /dev/null
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt
@@ -0,0 +1,21 @@
+package space.kscience.kmath.data
+
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.structures.Buffer
+
+/**
+ * A [XYColumnarData] with guaranteed [x], [y] and [z] columns designated by corresponding symbols.
+ * Inherits [XYColumnarData].
+ */
+@UnstableKMathAPI
+public interface XYZColumnarData : XYColumnarData {
+ public val z: Buffer
+
+ override fun get(symbol: Symbol): Buffer = when (symbol) {
+ Symbol.x -> x
+ Symbol.y -> y
+ Symbol.z -> z
+ else -> error("A column for symbol $symbol not found")
+ }
+}
\ No newline at end of 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 057a4a344..154763159 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
@@ -24,7 +24,6 @@ import space.kscience.kmath.misc.UnstableKMathAPI
*/
@UnstableKMathAPI
public interface DoubleDomain : Domain {
-
/**
* Global lower edge
* @param num axis number
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt
index 5cbc4dbf4..1f0ceaec3 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt
@@ -1,5 +1,8 @@
package space.kscience.kmath.expressions
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
+
/**
* Represents expression which structure can be differentiated.
*
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt
index 5ba24aa62..7918f199e 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt
@@ -1,26 +1,11 @@
package space.kscience.kmath.expressions
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.operations.Algebra
import kotlin.jvm.JvmName
import kotlin.properties.ReadOnlyProperty
-/**
- * A marker interface for a symbol. A symbol mus have an identity
- */
-public interface Symbol {
- /**
- * Identity object for the symbol. Two symbols with the same identity are considered to be the same symbol.
- */
- public val identity: String
-}
-
-/**
- * A [Symbol] with a [String] identity
- */
-public inline class StringSymbol(override val identity: String) : Symbol {
- override fun toString(): String = identity
-}
-
/**
* An elementary function that could be invoked on a map of arguments.
*
@@ -92,13 +77,6 @@ public interface ExpressionAlgebra : Algebra {
public fun ExpressionAlgebra.bindSymbol(symbol: Symbol): E =
bindSymbolOrNull(symbol) ?: error("Symbol $symbol could not be bound to $this")
-/**
- * A delegate to create a symbol with a string identity in this scope
- */
-public val symbol: ReadOnlyProperty = ReadOnlyProperty { _, property ->
- StringSymbol(property.name)
-}
-
/**
* Bind a symbol by name inside the [ExpressionAlgebra]
*/
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt
index cca75754f..ebd9e7f22 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt
@@ -1,5 +1,6 @@
package space.kscience.kmath.expressions
+import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.operations.*
/**
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt
index 4b0d402ed..d9be4a92e 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt
@@ -1,6 +1,7 @@
package space.kscience.kmath.expressions
import space.kscience.kmath.linear.Point
+import space.kscience.kmath.misc.Symbol
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.*
import space.kscience.kmath.structures.asBuffer
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt
index 580acaafb..4db4b5828 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt
@@ -1,6 +1,8 @@
package space.kscience.kmath.expressions
import space.kscience.kmath.linear.Point
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.nd.Structure2D
import space.kscience.kmath.structures.BufferFactory
@@ -8,6 +10,7 @@ import space.kscience.kmath.structures.BufferFactory
* An environment to easy transform indexed variables to symbols and back.
* TODO requires multi-receivers to be beautiful
*/
+@UnstableKMathAPI
public interface SymbolIndexer {
public val symbols: List
public fun indexOf(symbol: Symbol): Int = symbols.indexOf(symbol)
@@ -49,13 +52,16 @@ public interface SymbolIndexer {
public fun Map.toDoubleArray(): DoubleArray = DoubleArray(symbols.size) { getValue(symbols[it]) }
}
+@UnstableKMathAPI
public inline class SimpleSymbolIndexer(override val symbols: List) : SymbolIndexer
/**
* Execute the block with symbol indexer based on given symbol order
*/
+@UnstableKMathAPI
public inline fun withSymbols(vararg symbols: Symbol, block: SymbolIndexer.() -> R): R =
with(SimpleSymbolIndexer(symbols.toList()), block)
+@UnstableKMathAPI
public inline fun withSymbols(symbols: Collection, block: SymbolIndexer.() -> R): R =
with(SimpleSymbolIndexer(symbols.toList()), block)
\ No newline at end of file
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt
index af136c552..3e2dbee3f 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt
@@ -3,7 +3,10 @@ package space.kscience.kmath.linear
import space.kscience.kmath.nd.as1D
/**
- * A group of methods to resolve equation A dot X = B, where A and B are matrices or vectors
+ * A group of methods to solve for *X* in equation *X = A -1 · B*, where *A* and *B* are matrices or
+ * vectors.
+ *
+ * @param T the type of items.
*/
public interface LinearSolver {
/**
@@ -23,7 +26,7 @@ public interface LinearSolver {
}
/**
- * Convert matrix to vector if it is possible
+ * Convert matrix to vector if it is possible.
*/
public fun Matrix.asVector(): Point =
if (this.colNum == 1)
@@ -31,4 +34,11 @@ public fun Matrix.asVector(): Point =
else
error("Can't convert matrix with more than one column to vector")
+/**
+ * Creates an n × 1 [VirtualMatrix], where n is the size of the given buffer.
+ *
+ * @param T the type of elements contained in the buffer.
+ * @receiver a buffer.
+ * @return the new matrix.
+ */
public fun Point.asMatrix(): VirtualMatrix = VirtualMatrix(size, 1) { i, _ -> get(i) }
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt
index 4f2afc6fa..21094db73 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt
@@ -18,6 +18,8 @@ public typealias MutableMatrix = MutableStructure2D
/**
* Alias or using [Buffer] as a point/vector in a many-dimensional space.
+ *
+ * @param T the type of elements contained in the buffer.
*/
public typealias Point = Buffer
@@ -165,7 +167,7 @@ public interface LinearSpace> {
* @return a feature object or `null` if it isn't present.
*/
@UnstableKMathAPI
- public fun getFeature(structure: Matrix, type: KClass): F? = structure.getFeature(type)
+ public fun getFeature(structure: Matrix, type: KClass): F? = structure.getFeature(type)
public companion object {
@@ -195,7 +197,7 @@ public interface LinearSpace> {
* @return a feature object or `null` if it isn't present.
*/
@UnstableKMathAPI
-public inline fun LinearSpace.getFeature(structure: Matrix): F? =
+public inline fun LinearSpace.getFeature(structure: Matrix): F? =
getFeature(structure, F::class)
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt
index 6b97e89ef..30e3daa7a 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt
@@ -1,10 +1,12 @@
package space.kscience.kmath.linear
+import space.kscience.kmath.nd.StructureFeature
+
/**
* A marker interface representing some properties of matrices or additional transformations of them. Features are used
* to optimize matrix operations performance in some cases or retrieve the APIs.
*/
-public interface MatrixFeature
+public interface MatrixFeature: StructureFeature
/**
* Matrices with this feature are considered to have only diagonal non-null elements.
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt
index 868f74cc6..def3b87f7 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt
@@ -1,6 +1,7 @@
package space.kscience.kmath.linear
import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.nd.StructureFeature
import space.kscience.kmath.nd.getFeature
import space.kscience.kmath.operations.Ring
import kotlin.reflect.KClass
@@ -20,7 +21,7 @@ public class MatrixWrapper internal constructor(
*/
@UnstableKMathAPI
@Suppress("UNCHECKED_CAST")
- override fun getFeature(type: KClass): T? = features.singleOrNull { type.isInstance(it) } as? T
+ override fun getFeature(type: KClass): F? = features.singleOrNull { type.isInstance(it) } as? F
?: origin.getFeature(type)
override fun toString(): String {
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt
new file mode 100644
index 000000000..2c9774b6a
--- /dev/null
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt
@@ -0,0 +1,34 @@
+package space.kscience.kmath.misc
+
+import kotlin.properties.ReadOnlyProperty
+
+/**
+ * A marker interface for a symbol. A symbol mus have an identity
+ */
+public interface Symbol {
+ /**
+ * Identity object for the symbol. Two symbols with the same identity are considered to be the same symbol.
+ */
+ public val identity: String
+
+ public companion object{
+ public val x: StringSymbol = StringSymbol("x")
+ public val y: StringSymbol = StringSymbol("y")
+ public val z: StringSymbol = StringSymbol("z")
+ }
+}
+
+/**
+ * A [Symbol] with a [String] identity
+ */
+public inline class StringSymbol(override val identity: String) : Symbol {
+ override fun toString(): String = identity
+}
+
+
+/**
+ * A delegate to create a symbol with a string identity in this scope
+ */
+public val symbol: ReadOnlyProperty = ReadOnlyProperty { _, property ->
+ StringSymbol(property.name)
+}
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt
index b23ce947d..2821a6648 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt
@@ -67,7 +67,8 @@ public interface AlgebraND> {
* @return a feature object or `null` if it isn't present.
*/
@UnstableKMathAPI
- public fun getFeature(structure: StructureND, type: KClass): F? = structure.getFeature(type)
+ public fun getFeature(structure: StructureND, type: KClass): F? =
+ structure.getFeature(type)
public companion object
}
@@ -81,7 +82,7 @@ public interface AlgebraND> {
* @return a feature object or `null` if it isn't present.
*/
@UnstableKMathAPI
-public inline fun AlgebraND.getFeature(structure: StructureND): F? =
+public inline fun AlgebraND.getFeature(structure: StructureND): F? =
getFeature(structure, F::class)
/**
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt
index 354f3d802..313e8fb31 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt
@@ -17,6 +17,8 @@ public interface Structure1D : StructureND, Buffer {
}
public override operator fun iterator(): Iterator = (0 until size).asSequence().map(::get).iterator()
+
+ public companion object
}
/**
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt
index e1a1d37de..5d83ce3dd 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt
@@ -97,7 +97,7 @@ private inline class Structure2DWrapper(val structure: StructureND) : Stru
override operator fun get(i: Int, j: Int): T = structure[i, j]
@UnstableKMathAPI
- override fun getFeature(type: KClass): F? = structure.getFeature(type)
+ override fun getFeature(type: KClass): F? = structure.getFeature(type)
override fun elements(): Sequence> = structure.elements()
}
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt
index 78eac1809..a1aa5e554 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt
@@ -7,6 +7,8 @@ import kotlin.jvm.JvmName
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KClass
+public interface StructureFeature
+
/**
* Represents n-dimensional structure, i.e. multidimensional container of items of the same type and size. The number
* of dimensions and items in an array is defined by its shape, which is a sequence of non-negative integers that
@@ -48,7 +50,7 @@ public interface StructureND {
* If the feature is not present, null is returned.
*/
@UnstableKMathAPI
- public fun getFeature(type: KClass): F? = null
+ public fun getFeature(type: KClass): F? = null
public companion object {
/**
@@ -144,7 +146,7 @@ public interface StructureND {
public operator fun StructureND.get(vararg index: Int): T = get(index)
@UnstableKMathAPI
-public inline fun StructureND<*>.getFeature(): T? = getFeature(T::class)
+public inline fun StructureND<*>.getFeature(): T? = getFeature(T::class)
/**
* Represents mutable [StructureND].
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt
index 9f57bc4c1..492ec8e88 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt
@@ -1,6 +1,6 @@
package space.kscience.kmath.operations
-import space.kscience.kmath.expressions.Symbol
+import space.kscience.kmath.misc.Symbol
/**
* Stub for DSL the [Algebra] is.
@@ -100,8 +100,8 @@ public fun Algebra.bindSymbol(symbol: Symbol): T = bindSymbol(symbo
public inline operator fun , R> A.invoke(block: A.() -> R): R = run(block)
/**
- * Represents linear space without neutral element, i.e. algebraic structure with associative, binary operation [add]
- * and scalar multiplication [multiply].
+ * Represents group without neutral element (also known as inverse semigroup), i.e. algebraic structure with
+ * associative, binary operation [add].
*
* @param T the type of element of this semispace.
*/
@@ -177,7 +177,7 @@ public interface GroupOperations : Algebra {
}
/**
- * Represents linear space with neutral element, i.e. algebraic structure with associative, binary operation [add].
+ * Represents group, i.e. algebraic structure with associative, binary operation [add].
*
* @param T the type of element of this semispace.
*/
@@ -189,8 +189,8 @@ public interface Group : GroupOperations {
}
/**
- * Represents rng, i.e. algebraic structure with associative, binary, commutative operation [add] and associative,
- * operation [multiply] distributive over [add].
+ * Represents ring without multiplicative and additive identities, i.e. algebraic structure with
+ * associative, binary, commutative operation [add] and associative, operation [multiply] distributive over [add].
*
* @param T the type of element of this semiring.
*/
@@ -238,7 +238,7 @@ public interface Ring : Group, RingOperations {
}
/**
- * Represents field without identity elements, i.e. algebraic structure with associative, binary, commutative operations
+ * Represents field without without multiplicative and additive identities, i.e. algebraic structure with associative, binary, commutative operations
* [add] and [multiply]; binary operation [divide] as multiplication of left operand by reciprocal of right one.
*
* @param T the type of element of this semifield.
@@ -276,10 +276,11 @@ public interface FieldOperations : RingOperations {
}
/**
- * Represents field, i.e. algebraic structure with three operations: associative "addition" and "multiplication",
- * and "division" and their neutral elements.
+ * Represents field, i.e. algebraic structure with three operations: associative, commutative addition and
+ * multiplication, and division. **This interface differs from the eponymous mathematical definition: fields in KMath
+ * also support associative multiplication by scalar.**
*
- * @param T the type of element of this semifield.
+ * @param T the type of element of this field.
*/
public interface Field : Ring, FieldOperations, ScaleOperations, NumericAlgebra {
override fun number(value: Number): T = scale(one, value.toDouble())
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt
index 55bb68850..18fbf0fdd 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt
@@ -12,8 +12,8 @@ import kotlin.math.max
import kotlin.math.min
import kotlin.math.sign
-public typealias Magnitude = UIntArray
-public typealias TBase = ULong
+private typealias Magnitude = UIntArray
+private typealias TBase = ULong
/**
* Kotlin Multiplatform implementation of Big Integer numbers (KBigInteger).
@@ -241,18 +241,18 @@ public class BigInt internal constructor(
)
private fun compareMagnitudes(mag1: Magnitude, mag2: Magnitude): Int {
- when {
- mag1.size > mag2.size -> return 1
- mag1.size < mag2.size -> return -1
+ return when {
+ mag1.size > mag2.size -> 1
+ mag1.size < mag2.size -> -1
+
else -> {
- for (i in mag1.size - 1 downTo 0) {
- if (mag1[i] > mag2[i]) {
- return 1
- } else if (mag1[i] < mag2[i]) {
- return -1
- }
+ for (i in mag1.size - 1 downTo 0) return when {
+ mag1[i] > mag2[i] -> 1
+ mag1[i] < mag2[i] -> -1
+ else -> continue
}
- return 0
+
+ 0
}
}
}
@@ -302,10 +302,11 @@ public class BigInt internal constructor(
var carry = 0uL
for (i in mag.indices) {
- val cur: ULong = carry + mag[i].toULong() * x.toULong()
+ val cur = carry + mag[i].toULong() * x.toULong()
result[i] = (cur and BASE).toUInt()
carry = cur shr BASE_SIZE
}
+
result[resultLength - 1] = (carry and BASE).toUInt()
return stripLeadingZeros(result)
@@ -358,6 +359,9 @@ private fun stripLeadingZeros(mag: Magnitude): Magnitude {
return mag.sliceArray(IntRange(0, resSize))
}
+/**
+ * Returns the absolute value of the given value [x].
+ */
public fun abs(x: BigInt): BigInt = x.abs()
/**
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt
index 72a6e3cae..1b4ccb069 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt
@@ -40,7 +40,6 @@ public interface Buffer {
public operator fun iterator(): Iterator
public companion object {
-
/**
* Check the element-by-element match of content of two buffers.
*/
@@ -110,7 +109,6 @@ public interface MutableBuffer : Buffer {
public fun copy(): MutableBuffer
public companion object {
-
/**
* Creates a [DoubleBuffer] with the specified [size], where each element is calculated by calling the specified
* [initializer] function.
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt
index 4355ba17f..5f8bbe21f 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt
@@ -70,6 +70,15 @@ public inline fun Buffer.mapIndexed(
crossinline block: (index: Int, value: T) -> R,
): Buffer = bufferFactory(size) { block(it, get(it)) }
+/**
+ * Fold given buffer according to [operation]
+ */
+public inline fun Buffer.fold(initial: R, operation: (acc: R, T) -> R): R {
+ var accumulator = initial
+ for (index in this.indices) accumulator = operation(accumulator, get(index))
+ return accumulator
+}
+
/**
* Zip two buffers using given [transform].
*/
diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt
index b1be0c392..61b1d03f0 100644
--- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt
+++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt
@@ -1,5 +1,6 @@
package space.kscience.kmath.expressions
+import space.kscience.kmath.misc.symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.invoke
import kotlin.test.Test
diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt
index ca7aca905..666db13d8 100644
--- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt
+++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt
@@ -1,5 +1,7 @@
package space.kscience.kmath.expressions
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.misc.symbol
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asBuffer
diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt
index fb67f0308..376415a56 100644
--- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt
+++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt
@@ -38,12 +38,11 @@ class NumberNDFieldTest {
(i * 10 + j).toDouble()
}
- for (i in 0..2) {
+ for (i in 0..2)
for (j in 0..2) {
val expected = (i * 10 + j).toDouble()
assertEquals(expected, array[i, j], "Error at index [$i, $j]")
}
- }
}
@Test
diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt
index bc4508994..d024147b4 100644
--- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt
+++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt
@@ -1,12 +1,13 @@
package space.kscience.kmath.chains
/**
- * Performance optimized chain for real values
+ * Chunked, specialized chain for real values.
*/
-public abstract class BlockingDoubleChain : Chain {
- public abstract fun nextDouble(): Double
+public interface BlockingDoubleChain : Chain {
+ public override suspend fun next(): Double
- override suspend fun next(): Double = nextDouble()
-
- public fun nextBlock(size: Int): DoubleArray = DoubleArray(size) { nextDouble() }
+ /**
+ * Returns an [DoubleArray] chunk of [size] values of [next].
+ */
+ public suspend fun nextBlock(size: Int): DoubleArray = DoubleArray(size) { next() }
}
diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt
index 11da7e503..fb2e453ad 100644
--- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt
+++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt
@@ -3,10 +3,7 @@ package space.kscience.kmath.chains
/**
* Performance optimized chain for integer values
*/
-public abstract class BlockingIntChain : Chain {
- public abstract fun nextInt(): Int
-
- override suspend fun next(): Int = nextInt()
-
- public fun nextBlock(size: Int): IntArray = IntArray(size) { nextInt() }
+public interface BlockingIntChain : Chain {
+ public override suspend fun next(): Int
+ public suspend fun nextBlock(size: Int): IntArray = IntArray(size) { next() }
}
diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt
index 26d078fcb..a961f2e09 100644
--- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt
+++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt
@@ -63,12 +63,10 @@ public class MarkovChain(private val seed: suspend () -> R, private
public fun value(): R? = value
- public override suspend fun next(): R {
- mutex.withLock {
- val newValue = gen(value ?: seed())
- value = newValue
- return newValue
- }
+ public override suspend fun next(): R = mutex.withLock {
+ val newValue = gen(value ?: seed())
+ value = newValue
+ newValue
}
public override fun fork(): Chain = MarkovChain(seed = { value ?: seed() }, gen = gen)
@@ -90,12 +88,10 @@ public class StatefulChain(
public fun value(): R? = value
- public override suspend fun next(): R {
- mutex.withLock {
- val newValue = state.gen(value ?: state.seed())
- value = newValue
- return newValue
- }
+ public override suspend fun next(): R = mutex.withLock {
+ val newValue = state.gen(value ?: state.seed())
+ value = newValue
+ newValue
}
public override fun fork(): Chain = StatefulChain(forkState(state), seed, forkState, gen)
diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt
index c271f8889..dc1dd4757 100644
--- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt
+++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt
@@ -28,7 +28,7 @@ public fun Flow.chunked(bufferSize: Int, bufferFactory: BufferFactory)
var counter = 0
this@chunked.collect { element ->
- list.add(element)
+ list += element
counter++
if (counter == bufferSize) {
diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt
index f81ad2f0d..e844b765d 100644
--- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt
+++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt
@@ -48,11 +48,9 @@ public class RingBuffer(
/**
* A safe snapshot operation
*/
- public suspend fun snapshot(): Buffer {
- mutex.withLock {
- val copy = buffer.copy()
- return VirtualBuffer(size) { i -> copy[startIndex.forward(i)] as T }
- }
+ public suspend fun snapshot(): Buffer = mutex.withLock {
+ val copy = buffer.copy()
+ VirtualBuffer(size) { i -> copy[startIndex.forward(i)] as T }
}
public suspend fun push(element: T) {
diff --git a/kmath-dimensions/src/commonTest/kotlin/kscience/dimensions/DMatrixContextTest.kt b/kmath-dimensions/src/commonTest/kotlin/kscience/dimensions/DMatrixContextTest.kt
index e2a9628ac..58ed82723 100644
--- a/kmath-dimensions/src/commonTest/kotlin/kscience/dimensions/DMatrixContextTest.kt
+++ b/kmath-dimensions/src/commonTest/kotlin/kscience/dimensions/DMatrixContextTest.kt
@@ -1,4 +1,4 @@
-package kscience.dimensions
+package space.kscience.dimensions
import space.kscience.kmath.dimensions.D2
import space.kscience.kmath.dimensions.D3
diff --git a/kmath-ejml/README.md b/kmath-ejml/README.md
index 1081b2b7f..2551703a4 100644
--- a/kmath-ejml/README.md
+++ b/kmath-ejml/README.md
@@ -1,43 +1,37 @@
-# ejml-simple support (`kmath-ejml`)
+# Module kmath-ejml
-This subproject implements the following features:
+EJML based linear algebra implementation.
- [ejml-vector](src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt) : The Point implementation using SimpleMatrix.
- [ejml-matrix](src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt) : The Matrix implementation using SimpleMatrix.
- [ejml-linear-space](src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt) : The LinearSpace implementation using SimpleMatrix.
-> #### Artifact:
->
-> This module artifact: `space.kscience:kmath-ejml:0.3.0-dev-3`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-ejml/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-ejml/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-ejml/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-ejml/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation 'space.kscience:kmath-ejml:0.3.0-dev-3'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("space.kscience:kmath-ejml:0.3.0-dev-3")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-ejml:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-ejml:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-ejml:0.3.0-dev-3")
+}
+```
diff --git a/kmath-ejml/docs/README-TEMPLATE.md b/kmath-ejml/docs/README-TEMPLATE.md
index c53f4a81c..27fcedd65 100644
--- a/kmath-ejml/docs/README-TEMPLATE.md
+++ b/kmath-ejml/docs/README-TEMPLATE.md
@@ -1,6 +1,6 @@
-# ejml-simple support (`kmath-ejml`)
+# Module kmath-ejml
-This subproject implements the following features:
+EJML based linear algebra implementation.
${features}
diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt
index 6fc0a049c..94429e7b3 100644
--- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt
+++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt
@@ -4,6 +4,7 @@ import org.ejml.dense.row.factory.DecompositionFactory_DDRM
import org.ejml.simple.SimpleMatrix
import space.kscience.kmath.linear.*
import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.nd.StructureFeature
import space.kscience.kmath.nd.getFeature
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.structures.DoubleBuffer
@@ -95,7 +96,7 @@ public object EjmlLinearSpace : LinearSpace {
v.toEjml().origin.scale(this).wrapVector()
@UnstableKMathAPI
- public override fun getFeature(structure: Matrix, type: KClass): F? {
+ public override fun getFeature(structure: Matrix, type: KClass): F? {
//Return the feature if it is intrinsic to the structure
structure.getFeature(type)?.let { return it }
diff --git a/kmath-for-real/README.md b/kmath-for-real/README.md
index 139cd3cef..ad3d33062 100644
--- a/kmath-for-real/README.md
+++ b/kmath-for-real/README.md
@@ -1,41 +1,37 @@
-# Real number specialization module (`kmath-for-real`)
+# Module kmath-for-real
+
+Specialization of KMath APIs for Double numbers.
- [DoubleVector](src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt) : Numpy-like operations for Buffers/Points
- [DoubleMatrix](src/commonMain/kotlin/space/kscience/kmath/real/DoubleMatrix.kt) : Numpy-like operations for 2d real structures
- [grids](src/commonMain/kotlin/space/kscience/kmath/structures/grids.kt) : Uniform grid generators
-> #### Artifact:
->
-> This module artifact: `space.kscience:kmath-for-real:0.3.0-dev-3`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-for-real/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-for-real/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-for-real/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-for-real/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation 'space.kscience:kmath-for-real:0.3.0-dev-3'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("space.kscience:kmath-for-real:0.3.0-dev-3")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-for-real:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-for-real:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-for-real:0.3.0-dev-3")
+}
+```
diff --git a/kmath-for-real/docs/README-TEMPLATE.md b/kmath-for-real/docs/README-TEMPLATE.md
index 670844bd0..c2ef25aa7 100644
--- a/kmath-for-real/docs/README-TEMPLATE.md
+++ b/kmath-for-real/docs/README-TEMPLATE.md
@@ -1,4 +1,6 @@
-# Real number specialization module (`kmath-for-real`)
+# Module kmath-for-real
+
+Specialization of KMath APIs for Double numbers.
${features}
diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt
index 433e3c756..6b059ef56 100644
--- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt
+++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt
@@ -6,17 +6,13 @@ import space.kscience.kmath.operations.Norm
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.MutableBuffer.Companion.double
import space.kscience.kmath.structures.asBuffer
-import space.kscience.kmath.structures.asIterable
+import space.kscience.kmath.structures.fold
import space.kscience.kmath.structures.indices
import kotlin.math.pow
import kotlin.math.sqrt
public typealias DoubleVector = Point
-public object VectorL2Norm : Norm, Double> {
- override fun norm(arg: Point): Double = sqrt(arg.asIterable().sumByDouble(Number::toDouble))
-}
-
@Suppress("FunctionName")
public fun DoubleVector(vararg doubles: Double): DoubleVector = doubles.asBuffer()
@@ -102,4 +98,10 @@ public fun DoubleVector.sum(): Double {
res += get(i)
}
return res
-}
\ No newline at end of file
+}
+
+public object VectorL2Norm : Norm {
+ override fun norm(arg: DoubleVector): Double = sqrt(arg.fold(0.0) { acc: Double, d: Double -> acc + d.pow(2) })
+}
+
+public val DoubleVector.norm: Double get() = VectorL2Norm.norm(this)
\ No newline at end of file
diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt
index 35297a3ac..7f02c7db3 100644
--- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt
+++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt
@@ -1,7 +1,41 @@
package space.kscience.kmath.real
-import space.kscience.kmath.structures.asBuffer
-import kotlin.math.abs
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.structures.Buffer
+import space.kscience.kmath.structures.DoubleBuffer
+import kotlin.math.floor
+
+public val ClosedFloatingPointRange.length: Double get() = endInclusive - start
+
+/**
+ * Create a Buffer-based grid with equally distributed [numberOfPoints] points. The range could be increasing or decreasing.
+ * If range has a zero size, then the buffer consisting of [numberOfPoints] equal values is returned.
+ */
+public fun Buffer.Companion.fromRange(range: ClosedFloatingPointRange, numberOfPoints: Int): DoubleBuffer {
+ require(numberOfPoints >= 2) { "Number of points in grid must be more than 1" }
+ val normalizedRange = when {
+ range.endInclusive > range.start -> range
+ range.endInclusive < range.start -> range.endInclusive..range.start
+ else -> return DoubleBuffer(numberOfPoints) { range.start }
+ }
+ val step = normalizedRange.length / (numberOfPoints - 1)
+ return DoubleBuffer(numberOfPoints) { normalizedRange.start + step * it }
+}
+
+/**
+ * Create a Buffer-based grid with equally distributed points with a fixed [step]. The range could be increasing or decreasing.
+ * If the step is larger than the range size, single point is returned.
+ */
+public fun Buffer.Companion.withFixedStep(range: ClosedFloatingPointRange, step: Double): DoubleBuffer {
+ require(step > 0) { "The grid step must be positive" }
+ val normalizedRange = when {
+ range.endInclusive > range.start -> range
+ range.endInclusive < range.start -> range.endInclusive..range.start
+ else -> return DoubleBuffer(range.start)
+ }
+ val numberOfPoints = floor(normalizedRange.length / step).toInt() + 1
+ return DoubleBuffer(numberOfPoints) { normalizedRange.start + step * it }
+}
/**
* Convert double range to sequence.
@@ -11,35 +45,5 @@ import kotlin.math.abs
*
* If step is negative, the same goes from upper boundary downwards
*/
-public fun ClosedFloatingPointRange.toSequenceWithStep(step: Double): Sequence = when {
- step == 0.0 -> error("Zero step in double progression")
-
- step > 0 -> sequence {
- var current = start
-
- while (current <= endInclusive) {
- yield(current)
- current += step
- }
- }
-
- else -> sequence {
- var current = endInclusive
-
- while (current >= start) {
- yield(current)
- current += step
- }
- }
-}
-
-public infix fun ClosedFloatingPointRange.step(step: Double): DoubleVector =
- toSequenceWithStep(step).toList().asBuffer()
-
-/**
- * Convert double range to sequence with the fixed number of points
- */
-public fun ClosedFloatingPointRange.toSequenceWithPoints(numPoints: Int): Sequence {
- require(numPoints > 1) { "The number of points should be more than 2" }
- return toSequenceWithStep(abs(endInclusive - start) / (numPoints - 1))
-}
+@UnstableKMathAPI
+public infix fun ClosedFloatingPointRange.step(step: Double): DoubleBuffer = Buffer.withFixedStep(this, step)
\ No newline at end of file
diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/GridTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/GridTest.kt
index 5a644c8f9..91ee517ab 100644
--- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/GridTest.kt
+++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/GridTest.kt
@@ -1,13 +1,18 @@
package kaceince.kmath.real
+import space.kscience.kmath.real.DoubleVector
+import space.kscience.kmath.real.minus
+import space.kscience.kmath.real.norm
import space.kscience.kmath.real.step
import kotlin.test.Test
import kotlin.test.assertEquals
+import kotlin.test.assertTrue
class GridTest {
@Test
fun testStepGrid() {
val grid = 0.0..1.0 step 0.2
assertEquals(6, grid.size)
+ assertTrue { (grid - DoubleVector(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)).norm < 1e-4 }
}
}
\ No newline at end of file
diff --git a/kmath-functions/README.md b/kmath-functions/README.md
index d13c4c107..531e97a44 100644
--- a/kmath-functions/README.md
+++ b/kmath-functions/README.md
@@ -1,6 +1,6 @@
-# Functions (`kmath-functions`)
+# Module kmath-functions
-Functions and interpolations:
+Functions and interpolations.
- [piecewise](Piecewise functions.) : src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt
- [polynomials](Polynomial functions.) : src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt
@@ -8,37 +8,31 @@ Functions and interpolations:
- [spline interpolation](Cubic spline XY interpolator.) : src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt
-> #### Artifact:
->
-> This module artifact: `space.kscience:kmath-functions:0.3.0-dev-3`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-functions/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-functions/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-functions/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-functions/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url 'https://repo.kotlin.link' }
-> maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
-> maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
-> }
->
-> dependencies {
-> implementation 'space.kscience:kmath-functions:0.3.0-dev-3'
-> }
-> ```
-> **Gradle Kotlin DSL:**
->
-> ```kotlin
-> repositories {
-> maven("https://repo.kotlin.link")
-> maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
-> maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
-> }
->
-> dependencies {
-> implementation("space.kscience:kmath-functions:0.3.0-dev-3")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-functions:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-functions:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-functions:0.3.0-dev-3")
+}
+```
diff --git a/kmath-functions/docs/README-TEMPLATE.md b/kmath-functions/docs/README-TEMPLATE.md
index 8a34a7cc4..2e163eee5 100644
--- a/kmath-functions/docs/README-TEMPLATE.md
+++ b/kmath-functions/docs/README-TEMPLATE.md
@@ -1,6 +1,6 @@
-# Functions (`kmath-functions`)
+# Module kmath-functions
-Functions and interpolations:
+Functions and interpolations.
${features}
diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt
index d2470a4b4..2477af618 100644
--- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt
+++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt
@@ -2,14 +2,28 @@ package space.kscience.kmath.functions
import space.kscience.kmath.operations.Ring
+/**
+ * Represents piecewise-defined function.
+ *
+ * @param T the piece key type.
+ * @param R the sub-function type.
+ */
public fun interface Piecewise {
+ /**
+ * Returns the appropriate sub-function for given piece key.
+ */
public fun findPiece(arg: T): R?
}
+/**
+ * Represents piecewise-defined function where all the sub-functions are polynomials.
+ */
public fun interface PiecewisePolynomial : Piecewise>
/**
- * Ordered list of pieces in piecewise function
+ * Basic [Piecewise] implementation where all the pieces are ordered by the [Comparable] type instances.
+ *
+ * @param T the comparable piece key type.
*/
public class OrderedPiecewisePolynomial>(delimiter: T) :
PiecewisePolynomial {
@@ -17,22 +31,30 @@ public class OrderedPiecewisePolynomial>(delimiter: T) :
private val pieces: MutableList> = arrayListOf()
/**
- * Dynamically add a piece to the "right" side (beyond maximum argument value of previous piece)
- * @param right new rightmost position. If is less then current rightmost position, a error is thrown.
+ * Dynamically adds a piece to the right side (beyond maximum argument value of previous piece)
+ *
+ * @param right new rightmost position. If is less then current rightmost position, an error is thrown.
+ * @param piece the sub-function.
*/
public fun putRight(right: T, piece: Polynomial) {
require(right > delimiters.last()) { "New delimiter should be to the right of old one" }
- delimiters.add(right)
- pieces.add(piece)
+ delimiters += right
+ pieces += piece
}
+ /**
+ * Dynamically adds a piece to the left side (beyond maximum argument value of previous piece)
+ *
+ * @param left the new leftmost position. If is less then current rightmost position, an error is thrown.
+ * @param piece the sub-function.
+ */
public fun putLeft(left: T, piece: Polynomial) {
require(left < delimiters.first()) { "New delimiter should be to the left of old one" }
delimiters.add(0, left)
pieces.add(0, piece)
}
- override fun findPiece(arg: T): Polynomial? {
+ public override fun findPiece(arg: T): Polynomial? {
if (arg < delimiters.first() || arg >= delimiters.last())
return null
else {
@@ -46,9 +68,10 @@ public class OrderedPiecewisePolynomial>(delimiter: T) :
}
/**
- * Return a value of polynomial function with given [ring] an given [arg] or null if argument is outside of piecewise definition.
+ * Return a value of polynomial function with given [ring] an given [arg] or null if argument is outside of piecewise
+ * definition.
*/
public fun , C : Ring> PiecewisePolynomial.value(ring: C, arg: T): T? =
findPiece(arg)?.value(ring, arg)
-public fun , C : Ring