diff --git a/.gitignore b/.gitignore index a9294eff9..bade7f08c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,3 @@ out/ # Cache of project .gradletasknamecache - -gradle.properties \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index e017fe565..f3fe37b6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,18 +5,21 @@ - `fun` annotation for SAM interfaces in library - Explicit `public` visibility for all public APIs - Better trigonometric and hyperbolic functions for `AutoDiffField` (https://github.com/mipt-npm/kmath/pull/140). -- Automatic documentation generation for features (#139) +- Automatic README generation for features (#139) +- Native support for `memory`, `core` and `dimensions` ### Changed - Package changed from `scientifik` to `kscience.kmath`. - Gradle version: 6.6 -> 6.6.1 - Minor exceptions refactor (throwing `IllegalArgumentException` by argument checks instead of `IllegalStateException`) - `Polynomial` secondary constructor made function. +- Kotlin version: 1.3.72 -> 1.4.20-M1 ### Deprecated ### Removed - `kmath-koma` module because it doesn't support Kotlin 1.4. +- Support of `legacy` JS backend (we will support only IR) ### Fixed - `symbol` method in `MstExtendedField` (https://github.com/mipt-npm/kmath/pull/140) diff --git a/README.md b/README.md index 44363b27a..8bc85bf2b 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ Bintray-dev: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmat # KMath Could be pronounced as `key-math`. -The Kotlin MATHematics library is intended as a Kotlin-based analog to Python's `numpy` library. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. +The Kotlin MATHematics library was initially intended as a Kotlin-based analog to Python's `numpy` library. Later we found that kotlin is much more flexible language and allows superior architecture designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like experience could be achieved with [kmath-for-real](/kmath-for-real) extension module. -## Publications +## Publications and talks * [A conceptual article about context-oriented design](https://proandroiddev.com/an-introduction-context-oriented-programming-in-kotlin-2e79d316b0a2) * [Another article about context-oriented design](https://proandroiddev.com/diving-deeper-into-context-oriented-programming-in-kotlin-3ecb4ec38814) * [ACAT 2019 conference paper](https://aip.scitation.org/doi/abs/10.1063/1.5130103) @@ -29,7 +29,7 @@ The Kotlin MATHematics library is intended as a Kotlin-based analog to Python's ## Features -Actual feature list is [here](docs/features.md) +Actual feature list is [here](/docs/features.md) * **Algebra** * Algebraic structures like rings, spaces and field (**TODO** add example to wiki) @@ -68,30 +68,99 @@ can be used for a wide variety of purposes from high performance calculations to ## Modules -### [kmath-core](kmath-core) - - [algebras](kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt) : Algebraic structures: contexts and elements - - [nd](kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt) : Many-dimensional structures - - [buffers](kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt) : One-dimensional structure - - [expressions](kmath-core/src/commonMain/kotlin/kscience/kmath/expressions) : Functional Expressions - - [domains](kmath-core/src/commonMain/kotlin/kscience/kmath/domains) : Domains - - [autodif](kmath-core/src/commonMain/kotlin/kscience/kmath/misc/AutoDiff.kt) : Automatic differentiation +
-### [kmath-coroutines](kmath-coroutines) -### [kmath-viktor](kmath-viktor) -### [kmath-prob](kmath-prob) -### [kmath-ast](kmath-ast) -### [kmath-commons](kmath-commons) -### [kmath-memory](kmath-memory) -### [kmath-for-real](kmath-for-real) -### [kmath-functions](kmath-functions) -### [kmath-dimensions](kmath-dimensions) -### [kmath-histograms](kmath-histograms) -### [kmath-geometry](kmath-geometry) +* ### [examples](examples) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-ast](kmath-ast) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-commons](kmath-commons) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-core](kmath-core) +> Core classes, algebra definitions, basic linear algebra +> +> **Maturity**: DEVELOPMENT +> +> **Features:** +> - [algebras](kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt) : Algebraic structures: contexts and elements +> - [nd](kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt) : Many-dimensional structures +> - [buffers](kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt) : One-dimensional structure +> - [expressions](kmath-core/src/commonMain/kotlin/kscience/kmath/expressions) : Functional Expressions +> - [domains](kmath-core/src/commonMain/kotlin/kscience/kmath/domains) : Domains +> - [autodif](kmath-core/src/commonMain/kotlin/kscience/kmath/misc/AutoDiff.kt) : Automatic differentiation + +
+ +* ### [kmath-coroutines](kmath-coroutines) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-dimensions](kmath-dimensions) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-for-real](kmath-for-real) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-functions](kmath-functions) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-geometry](kmath-geometry) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-histograms](kmath-histograms) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-memory](kmath-memory) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-prob](kmath-prob) +> +> +> **Maturity**: EXPERIMENTAL +
+ +* ### [kmath-viktor](kmath-viktor) +> +> +> **Maturity**: EXPERIMENTAL +
## Multi-platform support -KMath is developed as a multi-platform library, which means that most of interfaces are declared in the [common module](kmath-core/src/commonMain). Implementation is also done in the common module wherever possible. In some cases, features are delegated to platform-specific implementations even if they could be done in the common module for performance reasons. Currently, the JVM is the main focus of development, however Kotlin/Native and Kotlin/JS contributions are also welcome. +KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the [common module](/kmath-core/src/commonMain). Implementation is also done in the common module wherever possible. In some cases, features are delegated to platform-specific implementations even if they could be done in the common module for performance reasons. Currently, the JVM is the main focus of development, however Kotlin/Native and Kotlin/JS contributions are also welcome. ## Performance diff --git a/build.gradle.kts b/build.gradle.kts index ed3317bf8..499f49d1d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,15 +1,15 @@ plugins { - id("ru.mipt.npm.base") - id("org.jetbrains.changelog") version "0.4.0" + id("ru.mipt.npm.project") } -val kmathVersion by extra("0.2.0-dev-1") +val kmathVersion by extra("0.2.0-dev-2") val bintrayRepo by extra("kscience") val githubProject by extra("kmath") allprojects { repositories { jcenter() + maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/kotlin/kotlinx") maven("https://dl.bintray.com/hotkeytlt/maven") } @@ -22,79 +22,6 @@ subprojects { if (name.startsWith("kmath")) apply() } -/** - * TODO move to base plugin - */ -val generateReadme by tasks.creating { - group = "documentation" - - fun List>.generateFeatureString(pathPrefix: String): String = buildString { - this@generateFeatureString.forEach { feature -> - val id by feature - val description by feature - val ref by feature - appendln(" - [$id]($pathPrefix$ref) : $description") - } - } - - doLast { - val reader = groovy.json.JsonSlurper() - val projects = HashMap>() - - project.subprojects { - var properties: Map = mapOf( - "name" to this.name, - "group" to this.group, - "version" to this.version - ) - - val projectProperties = this.file("docs/kscience-module.json") - - @Suppress("UNCHECKED_CAST") - if (projectProperties.exists()) { - val customProperties: Map = - (reader.parse(projectProperties) as? Map ?: emptyMap()).withDefault { null } - val features: List>? by customProperties - val featureString = features?.generateFeatureString("") - properties = customProperties + properties + ("featuresString" to featureString) - } - - projects[name] = properties.withDefault { null } - - val readmeStub = this.file("docs/README-STUB.md") - if (readmeStub.exists()) { - val readmeFile = this.file("README.md") - readmeFile.writeText( - groovy.text.SimpleTemplateEngine().createTemplate(readmeStub).make(properties).toString() - ) - } - } - - val rootReadmeStub = project.file("docs/README-STUB.md") - - val modulesString = buildString { - projects.filter { it.key.startsWith("kmath") }.forEach { (name, properties) -> - appendln("### [$name]($name)") - val features: List>? by properties - if (features != null) { - appendln(features!!.generateFeatureString("$name/")) - } - } - } - - val rootReadmeProperties: Map = mapOf( - "name" to project.name, - "group" to project.group, - "version" to project.version, - "modulesString" to modulesString - ) - - if (rootReadmeStub.exists()) { - val readmeFile = project.file("README.md") - readmeFile.writeText( - groovy.text.SimpleTemplateEngine().createTemplate(rootReadmeStub).make(rootReadmeProperties).toString() - ) - } - - } -} \ No newline at end of file +readme{ + readmeTemplate = file("docs/templates/README-TEMPLATE.md") +} diff --git a/docs/images/KM.svg b/docs/images/KM.svg new file mode 100644 index 000000000..50126cbc5 --- /dev/null +++ b/docs/images/KM.svg @@ -0,0 +1,59 @@ + +image/svg+xml \ No newline at end of file diff --git a/docs/images/KM_mono.svg b/docs/images/KM_mono.svg new file mode 100644 index 000000000..3b6890b6b --- /dev/null +++ b/docs/images/KM_mono.svg @@ -0,0 +1,55 @@ + +image/svg+xml \ No newline at end of file diff --git a/docs/images/KMath.svg b/docs/images/KMath.svg new file mode 100644 index 000000000..d88cfe7b0 --- /dev/null +++ b/docs/images/KMath.svg @@ -0,0 +1,91 @@ + +image/svg+xml \ No newline at end of file diff --git a/docs/images/KMath_mono.svg b/docs/images/KMath_mono.svg new file mode 100644 index 000000000..3a62ac383 --- /dev/null +++ b/docs/images/KMath_mono.svg @@ -0,0 +1,371 @@ + +image/svg+xml \ No newline at end of file diff --git a/docs/templates/ARTIFACT-TEMPLATE.md b/docs/templates/ARTIFACT-TEMPLATE.md new file mode 100644 index 000000000..c77948d4b --- /dev/null +++ b/docs/templates/ARTIFACT-TEMPLATE.md @@ -0,0 +1,37 @@ +> #### 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://dl.bintray.com/kotlin/kotlin-eap" } +> maven { url 'https://dl.bintray.com/mipt-npm/kscience' } +> maven { url 'https://dl.bintray.com/mipt-npm/dev' } +> maven { url 'https://dl.bintray.com/hotkeytlt/maven' } + +> } +> +> dependencies { +> implementation '${group}:${name}:${version}' +> } +> ``` +> **Gradle Kotlin DSL:** +> +> ```kotlin +> repositories { +> maven("https://dl.bintray.com/kotlin/kotlin-eap") +> maven("https://dl.bintray.com/mipt-npm/kscience") +> maven("https://dl.bintray.com/mipt-npm/dev") +> maven("https://dl.bintray.com/hotkeytlt/maven") +> } +> +> dependencies { +> implementation("${group}:${name}:${version}") +> } +> ``` \ No newline at end of file diff --git a/docs/README-STUB.md b/docs/templates/README-TEMPLATE.md similarity index 85% rename from docs/README-STUB.md rename to docs/templates/README-TEMPLATE.md index 1c6a1f6e0..f451adb24 100644 --- a/docs/README-STUB.md +++ b/docs/templates/README-TEMPLATE.md @@ -9,9 +9,9 @@ Bintray-dev: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmat # KMath Could be pronounced as `key-math`. -The Kotlin MATHematics library is intended as a Kotlin-based analog to Python's `numpy` library. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. +The Kotlin MATHematics library was initially intended as a Kotlin-based analog to Python's `numpy` library. Later we found that kotlin is much more flexible language and allows superior architecture designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like experience could be achieved with [kmath-for-real](/kmath-for-real) extension module. -## Publications +## Publications and talks * [A conceptual article about context-oriented design](https://proandroiddev.com/an-introduction-context-oriented-programming-in-kotlin-2e79d316b0a2) * [Another article about context-oriented design](https://proandroiddev.com/diving-deeper-into-context-oriented-programming-in-kotlin-3ecb4ec38814) * [ACAT 2019 conference paper](https://aip.scitation.org/doi/abs/10.1063/1.5130103) @@ -29,7 +29,7 @@ The Kotlin MATHematics library is intended as a Kotlin-based analog to Python's ## Features -Actual feature list is [here](docs/features.md) +Actual feature list is [here](/docs/features.md) * **Algebra** * Algebraic structures like rings, spaces and field (**TODO** add example to wiki) @@ -68,11 +68,11 @@ can be used for a wide variety of purposes from high performance calculations to ## Modules -$modulesString +$modules ## Multi-platform support -KMath is developed as a multi-platform library, which means that most of interfaces are declared in the [common module](kmath-core/src/commonMain). Implementation is also done in the common module wherever possible. In some cases, features are delegated to platform-specific implementations even if they could be done in the common module for performance reasons. Currently, the JVM is the main focus of development, however Kotlin/Native and Kotlin/JS contributions are also welcome. +KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the [common module](/kmath-core/src/commonMain). Implementation is also done in the common module wherever possible. In some cases, features are delegated to platform-specific implementations even if they could be done in the common module for performance reasons. Currently, the JVM is the main focus of development, however Kotlin/Native and Kotlin/JS contributions are also welcome. ## Performance diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 3d193efce..3f18d3cf3 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -3,8 +3,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { java kotlin("jvm") - kotlin("plugin.allopen") version "1.4.20-dev-3898-14" - id("kotlinx.benchmark") version "0.2.0-dev-20" + kotlin("plugin.allopen") + id("kotlinx.benchmark") } allOpen.annotation("org.openjdk.jmh.annotations.State") @@ -26,7 +26,7 @@ dependencies { implementation(project(":kmath-prob")) implementation(project(":kmath-viktor")) implementation(project(":kmath-dimensions")) - implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:0.2.0-npm-dev-6") + implementation("org.jetbrains.kotlinx:kotlinx-io:0.2.0-npm-dev-11") implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-20") "benchmarksCompile"(sourceSets.main.get().output + sourceSets.main.get().compileClasspath) //sourceSets.main.output + sourceSets.main.runtimeClasspath } @@ -34,9 +34,8 @@ dependencies { // Configure benchmark benchmark { // Setup configurations - targets - // This one matches sourceSet name above - .register("benchmarks") + targets.register("benchmarks") + // This one matches sourceSet name above configurations.register("fast") { warmups = 5 // number of warmup iterations diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 000000000..930bba550 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,9 @@ +kotlin.code.style=official +kotlin.parallel.tasks.in.project=true +kotlin.mpp.enableGranularSourceSetsMetadata=true +kotlin.native.enableDependencyPropagation=false +kotlin.mpp.stability.nowarn=true + +org.gradle.jvmargs=-XX:MaxMetaspaceSize=512m +org.gradle.parallel=true +systemProp.org.gradle.internal.publish.checksums.insecure=true \ No newline at end of file diff --git a/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DiffExpression.kt b/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DiffExpression.kt index 3ac908536..8a09cc793 100644 --- a/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DiffExpression.kt +++ b/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DiffExpression.kt @@ -94,7 +94,7 @@ public class DiffExpression(public val function: DerivativeStructureField.() -> * TODO make result [DiffExpression] */ public fun derivative(orders: Map): Expression = Expression { arguments -> - (DerivativeStructureField(orders.values.max() ?: 0, arguments)) { function().deriv(orders) } + (DerivativeStructureField(orders.values.maxOrNull() ?: 0, arguments)) { function().deriv(orders) } } //TODO add gradient and maybe other vector operators diff --git a/kmath-core/README.md b/kmath-core/README.md index 1fcd9edc9..2cf7ed5dc 100644 --- a/kmath-core/README.md +++ b/kmath-core/README.md @@ -1,6 +1,7 @@ # The Core Module (`kmath-core`) The core features of KMath: + - [algebras](src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt) : Algebraic structures: contexts and elements - [nd](src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt) : Many-dimensional structures - [buffers](src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt) : One-dimensional structure @@ -10,15 +11,20 @@ The core features of KMath: > #### Artifact: +> > This module artifact: `kscience.kmath:kmath-core:0.2.0-dev-1`. -> +> +> 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://dl.bintray.com/mipt-npm/kscience' } > maven { url 'https://dl.bintray.com/mipt-npm/dev' } -> maven { url https://dl.bintray.com/hotkeytlt/maven' } +> maven { url 'https://dl.bintray.com/hotkeytlt/maven' } > } > > dependencies { diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index e315e1640..b56151abe 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -1,7 +1,46 @@ -plugins { id("ru.mipt.npm.mpp") } +plugins { + id("ru.mipt.npm.mpp") + id("ru.mipt.npm.native") +} kotlin.sourceSets.commonMain { dependencies { api(project(":kmath-memory")) } } + +readme { + description = "Core classes, algebra definitions, basic linear algebra" + maturity = ru.mipt.npm.gradle.Maturity.DEVELOPMENT + propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) + feature( + id = "algebras", + description = "Algebraic structures: contexts and elements", + ref = "src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt" + ) + feature( + id = "nd", + description = "Many-dimensional structures", + ref = "src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt" + ) + feature( + id = "buffers", + description = "One-dimensional structure", + ref = "src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt" + ) + feature( + id = "expressions", + description = "Functional Expressions", + ref = "src/commonMain/kotlin/kscience/kmath/expressions" + ) + feature( + id = "domains", + description = "Domains", + ref = "src/commonMain/kotlin/kscience/kmath/domains" + ) + feature( + id = "autodif", + description = "Automatic differentiation", + ref = "src/commonMain/kotlin/kscience/kmath/misc/AutoDiff.kt" + ) +} \ No newline at end of file diff --git a/kmath-core/docs/README-STUB.md b/kmath-core/docs/README-STUB.md deleted file mode 100644 index c2e8f661f..000000000 --- a/kmath-core/docs/README-STUB.md +++ /dev/null @@ -1,34 +0,0 @@ -# The Core Module (`kmath-core`) - -The core features of KMath: -$featuresString - -> #### Artifact: -> This module artifact: `$group:kmath-core:$version`. -> -> **Gradle:** -> -> ```gradle -> repositories { -> maven { url 'https://dl.bintray.com/mipt-npm/kscience' } -> maven { url 'https://dl.bintray.com/mipt-npm/dev' } -> maven { url https://dl.bintray.com/hotkeytlt/maven' } -> } -> -> dependencies { -> implementation '$group:kmath-core:$version' -> } -> ``` -> **Gradle Kotlin DSL:** -> -> ```kotlin -> repositories { -> maven("https://dl.bintray.com/mipt-npm/kscience") -> maven("https://dl.bintray.com/mipt-npm/dev") -> maven("https://dl.bintray.com/hotkeytlt/maven") -> } -> -> dependencies { -> implementation("$group:kmath-core:$version") -> } -> ``` diff --git a/kmath-core/docs/README-TEMPLATE.md b/kmath-core/docs/README-TEMPLATE.md new file mode 100644 index 000000000..83d1ebdce --- /dev/null +++ b/kmath-core/docs/README-TEMPLATE.md @@ -0,0 +1,7 @@ +# The Core Module (`kmath-core`) + +The core features of KMath: + +${features} + +${artifact} diff --git a/kmath-core/docs/kscience-module.json b/kmath-core/docs/kscience-module.json deleted file mode 100644 index c7ac4f082..000000000 --- a/kmath-core/docs/kscience-module.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "description": "Core classes, algebra definitions, basic linear algebra", - "maturity": "development", - "features": [ - { - "id": "algebras", - "description": "Algebraic structures: contexts and elements", - "ref": "src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt" - }, - { - "id": "nd", - "description": "Many-dimensional structures", - "ref": "src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt" - }, - { - "id": "buffers", - "description": "One-dimensional structure", - "ref": "src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt" - }, - { - "id": "expressions", - "description": "Functional Expressions", - "ref": "src/commonMain/kotlin/kscience/kmath/expressions" - }, - { - "id": "domains", - "description": "Domains", - "ref": "src/commonMain/kotlin/kscience/kmath/domains" - }, - { - "id": "autodif", - "description": "Automatic differentiation", - "ref": "src/commonMain/kotlin/kscience/kmath/misc/AutoDiff.kt" - } - ] -} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Complex.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Complex.kt index c0faf5dc5..24bfec054 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Complex.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Complex.kt @@ -1,11 +1,11 @@ package kscience.kmath.operations +import kscience.kmath.memory.MemoryReader +import kscience.kmath.memory.MemorySpec +import kscience.kmath.memory.MemoryWriter import kscience.kmath.structures.Buffer import kscience.kmath.structures.MemoryBuffer import kscience.kmath.structures.MutableBuffer -import kscience.memory.MemoryReader -import kscience.memory.MemorySpec -import kscience.memory.MemoryWriter import kotlin.math.* /** diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/MemoryBuffer.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/MemoryBuffer.kt index b7e6a8218..c171e7c1d 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/MemoryBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/MemoryBuffer.kt @@ -1,6 +1,6 @@ package kscience.kmath.structures -import kscience.memory.* +import kscience.kmath.memory.* /** * A non-boxing buffer over [Memory] object. diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDAlgebra.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDAlgebra.kt index 28eaef2f1..03c601717 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDAlgebra.kt @@ -4,6 +4,7 @@ import kscience.kmath.operations.Complex import kscience.kmath.operations.Field import kscience.kmath.operations.Ring import kscience.kmath.operations.Space +import kotlin.native.concurrent.ThreadLocal /** * An exception is thrown when the expected ans actual shape of NDArray differs @@ -115,6 +116,7 @@ public interface NDField, N : NDStructure> : Field, NDRing public operator fun T.div(arg: N): N = map(arg) { divide(it, this@div) } + @ThreadLocal public companion object { private val realNDFieldCache: MutableMap = hashMapOf() diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt index 4ab49c9bc..fd679d073 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt @@ -1,6 +1,7 @@ package kscience.kmath.structures import kotlin.jvm.JvmName +import kotlin.native.concurrent.ThreadLocal import kotlin.reflect.KClass /** @@ -230,6 +231,7 @@ public class DefaultStrides private constructor(override val shape: IntArray) : override fun hashCode(): Int = shape.contentHashCode() + @ThreadLocal public companion object { private val defaultStridesCache = HashMap() diff --git a/kmath-core/src/commonTest/kotlin/kscience/kmath/operations/ComplexTest.kt b/kmath-core/src/commonTest/kotlin/kscience/kmath/operations/ComplexTest.kt index f8b9b7262..456e41467 100644 --- a/kmath-core/src/commonTest/kotlin/kscience/kmath/operations/ComplexTest.kt +++ b/kmath-core/src/commonTest/kotlin/kscience/kmath/operations/ComplexTest.kt @@ -2,6 +2,7 @@ package kscience.kmath.operations import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue internal class ComplexTest { @Test @@ -13,7 +14,7 @@ internal class ComplexTest { @Test fun reciprocal() { - assertEquals(Complex(0.5, -0.0), 2.toComplex().reciprocal) + assertTrue { (Complex(0.5, -0.0) - 2.toComplex().reciprocal).r < 1e-10} } @Test diff --git a/kmath-dimensions/build.gradle.kts b/kmath-dimensions/build.gradle.kts index 0a36e4435..412d7162f 100644 --- a/kmath-dimensions/build.gradle.kts +++ b/kmath-dimensions/build.gradle.kts @@ -1,4 +1,7 @@ -plugins { id("ru.mipt.npm.mpp") } +plugins { + id("ru.mipt.npm.mpp") + id("ru.mipt.npm.native") +} description = "A proof of concept module for adding type-safe dimensions to structures" diff --git a/kmath-dimensions/src/jsMain/kotlin/kscience/kmath/dimensions/dim.kt b/kmath-dimensions/src/jsMain/kotlin/kscience/kmath/dimensions/dimJs.kt similarity index 100% rename from kmath-dimensions/src/jsMain/kotlin/kscience/kmath/dimensions/dim.kt rename to kmath-dimensions/src/jsMain/kotlin/kscience/kmath/dimensions/dimJs.kt diff --git a/kmath-dimensions/src/jvmMain/kotlin/kscience/kmath/dimensions/dim.kt b/kmath-dimensions/src/jvmMain/kotlin/kscience/kmath/dimensions/dimJvm.kt similarity index 100% rename from kmath-dimensions/src/jvmMain/kotlin/kscience/kmath/dimensions/dim.kt rename to kmath-dimensions/src/jvmMain/kotlin/kscience/kmath/dimensions/dimJvm.kt diff --git a/kmath-dimensions/src/nativeMain/kotlin/kscience/kmath/dimensions/dimNative.kt b/kmath-dimensions/src/nativeMain/kotlin/kscience/kmath/dimensions/dimNative.kt new file mode 100644 index 000000000..aeaeaf759 --- /dev/null +++ b/kmath-dimensions/src/nativeMain/kotlin/kscience/kmath/dimensions/dimNative.kt @@ -0,0 +1,20 @@ +package kscience.kmath.dimensions + +import kotlin.native.concurrent.ThreadLocal +import kotlin.reflect.KClass + +@ThreadLocal +private val dimensionMap: MutableMap = hashMapOf(1u to D1, 2u to D2, 3u to D3) + +@Suppress("UNCHECKED_CAST") +public actual fun Dimension.Companion.resolve(type: KClass): D = dimensionMap + .entries + .map(MutableMap.MutableEntry::value) + .find { it::class == type } as? D + ?: error("Can't resolve dimension $type") + +public actual fun Dimension.Companion.of(dim: UInt): Dimension = dimensionMap.getOrPut(dim) { + object : Dimension { + override val dim: UInt get() = dim + } +} diff --git a/kmath-memory/build.gradle.kts b/kmath-memory/build.gradle.kts index 94527a6a3..9f92cca92 100644 --- a/kmath-memory/build.gradle.kts +++ b/kmath-memory/build.gradle.kts @@ -1 +1,4 @@ -plugins { id("ru.mipt.npm.mpp") } +plugins { + id("ru.mipt.npm.mpp") + id("ru.mipt.npm.native") +} diff --git a/kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt b/kmath-memory/src/commonMain/kotlin/kscience/kmath/memory/Memory.kt similarity index 99% rename from kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt rename to kmath-memory/src/commonMain/kotlin/kscience/kmath/memory/Memory.kt index 8de9a7a0c..344a1f1d3 100644 --- a/kmath-memory/src/commonMain/kotlin/scientifik/memory/Memory.kt +++ b/kmath-memory/src/commonMain/kotlin/kscience/kmath/memory/Memory.kt @@ -1,4 +1,4 @@ -package kscience.memory +package kscience.kmath.memory import kotlin.contracts.InvocationKind import kotlin.contracts.contract diff --git a/kmath-memory/src/commonMain/kotlin/scientifik/memory/MemorySpec.kt b/kmath-memory/src/commonMain/kotlin/kscience/kmath/memory/MemorySpec.kt similarity index 98% rename from kmath-memory/src/commonMain/kotlin/scientifik/memory/MemorySpec.kt rename to kmath-memory/src/commonMain/kotlin/kscience/kmath/memory/MemorySpec.kt index d2cbb32fd..572dab0fa 100644 --- a/kmath-memory/src/commonMain/kotlin/scientifik/memory/MemorySpec.kt +++ b/kmath-memory/src/commonMain/kotlin/kscience/kmath/memory/MemorySpec.kt @@ -1,4 +1,4 @@ -package kscience.memory +package kscience.kmath.memory /** * A specification to read or write custom objects with fixed size in bytes. diff --git a/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt b/kmath-memory/src/jsMain/kotlin/kscience/kmath/memory/DataViewMemory.kt similarity index 99% rename from kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt rename to kmath-memory/src/jsMain/kotlin/kscience/kmath/memory/DataViewMemory.kt index d6b8841e4..2146cd4e1 100644 --- a/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt +++ b/kmath-memory/src/jsMain/kotlin/kscience/kmath/memory/DataViewMemory.kt @@ -1,4 +1,4 @@ -package kscience.memory +package kscience.kmath.memory import org.khronos.webgl.ArrayBuffer import org.khronos.webgl.DataView diff --git a/kmath-memory/src/jvmMain/kotlin/scientifik/memory/ByteBufferMemory.kt b/kmath-memory/src/jvmMain/kotlin/kscience/kmath/memory/ByteBufferMemory.kt similarity index 99% rename from kmath-memory/src/jvmMain/kotlin/scientifik/memory/ByteBufferMemory.kt rename to kmath-memory/src/jvmMain/kotlin/kscience/kmath/memory/ByteBufferMemory.kt index c912b28ff..7a75b423e 100644 --- a/kmath-memory/src/jvmMain/kotlin/scientifik/memory/ByteBufferMemory.kt +++ b/kmath-memory/src/jvmMain/kotlin/kscience/kmath/memory/ByteBufferMemory.kt @@ -1,4 +1,4 @@ -package kscience.memory +package kscience.kmath.memory import java.io.IOException import java.nio.ByteBuffer diff --git a/kmath-memory/src/nativeMain/kotlin/kscience/kmath/memory/NativeMemory.kt b/kmath-memory/src/nativeMain/kotlin/kscience/kmath/memory/NativeMemory.kt new file mode 100644 index 000000000..0e007a8ab --- /dev/null +++ b/kmath-memory/src/nativeMain/kotlin/kscience/kmath/memory/NativeMemory.kt @@ -0,0 +1,93 @@ +package kscience.kmath.memory + +@PublishedApi +internal class NativeMemory( + val array: ByteArray, + val startOffset: Int = 0, + override val size: Int = array.size +) : Memory { + @Suppress("NOTHING_TO_INLINE") + private inline fun position(o: Int): Int = startOffset + o + + override fun view(offset: Int, length: Int): Memory { + require(offset >= 0) { "offset shouldn't be negative: $offset" } + require(length >= 0) { "length shouldn't be negative: $length" } + require(offset + length <= size) { "Can't view memory outside the parent region." } + return NativeMemory(array, position(offset), length) + } + + override fun copy(): Memory { + val copy = array.copyOfRange(startOffset, startOffset + size) + return NativeMemory(copy) + } + + private val reader: MemoryReader = object : MemoryReader { + override val memory: Memory get() = this@NativeMemory + + override fun readDouble(offset: Int) = array.getDoubleAt(position(offset)) + + override fun readFloat(offset: Int) = array.getFloatAt(position(offset)) + + override fun readByte(offset: Int) = array[position(offset)] + + override fun readShort(offset: Int) = array.getShortAt(position(offset)) + + override fun readInt(offset: Int) = array.getIntAt(position(offset)) + + override fun readLong(offset: Int) = array.getLongAt(position(offset)) + + override fun release() { + // does nothing on JVM + } + } + + override fun reader(): MemoryReader = reader + + private val writer: MemoryWriter = object : MemoryWriter { + override val memory: Memory get() = this@NativeMemory + + override fun writeDouble(offset: Int, value: Double) { + array.setDoubleAt(position(offset), value) + } + + override fun writeFloat(offset: Int, value: Float) { + array.setFloatAt(position(offset), value) + } + + override fun writeByte(offset: Int, value: Byte) { + array.set(position(offset), value) + } + + override fun writeShort(offset: Int, value: Short) { + array.setShortAt(position(offset), value) + } + + override fun writeInt(offset: Int, value: Int) { + array.setIntAt(position(offset), value) + } + + override fun writeLong(offset: Int, value: Long) { + array.setLongAt(position(offset), value) + } + + override fun release() { + // does nothing on JVM + } + } + + override fun writer(): MemoryWriter = writer +} + +/** + * Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied + * and could be mutated independently from the resulting [Memory]. + */ +public actual fun Memory.Companion.wrap(array: ByteArray): Memory = NativeMemory(array) + +/** + * Allocates the most effective platform-specific memory. + */ +public actual fun Memory.Companion.allocate(length: Int): Memory { + val array = ByteArray(length) + return NativeMemory(array) +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index a3732cf9d..78372f1fa 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,15 +1,4 @@ pluginManagement { - val toolsVersion = "0.6.0" - - plugins { - id("kotlinx.benchmark") version "0.2.0-dev-20" - id("ru.mipt.npm.base") version toolsVersion - id("ru.mipt.npm.mpp") version toolsVersion - id("ru.mipt.npm.jvm") version toolsVersion - id("ru.mipt.npm.publish") version toolsVersion - kotlin("plugin.allopen") - } - repositories { mavenLocal() jcenter() @@ -20,6 +9,19 @@ pluginManagement { maven("https://dl.bintray.com/kotlin/kotlinx") maven("https://dl.bintray.com/kotlin/kotlin-dev/") } + + val toolsVersion = "0.6.1-dev-1.4.20-M1" + val kotlinVersion = "1.4.20-M1" + + plugins { + id("kotlinx.benchmark") version "0.2.0-dev-20" + id("ru.mipt.npm.project") version toolsVersion + id("ru.mipt.npm.mpp") version toolsVersion + id("ru.mipt.npm.jvm") version toolsVersion + id("ru.mipt.npm.publish") version toolsVersion + kotlin("jvm") version kotlinVersion + kotlin("plugin.allopen") version kotlinVersion + } } rootProject.name = "kmath" @@ -34,7 +36,6 @@ include( ":kmath-commons", ":kmath-viktor", ":kmath-prob", -// ":kmath-io", ":kmath-dimensions", ":kmath-for-real", ":kmath-geometry",