Merge remote-tracking branch 'origin/dev' into mp-samplers

This commit is contained in:
Iaroslav Postovalov 2020-09-28 03:02:48 +07:00
commit b665a3a2fc
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
34 changed files with 937 additions and 212 deletions

2
.gitignore vendored
View File

@ -8,5 +8,3 @@ out/
# Cache of project
.gradletasknamecache
gradle.properties

View File

@ -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)

113
README.md
View File

@ -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
<hr/>
### [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
<hr/>
* ### [kmath-ast](kmath-ast)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-commons](kmath-commons)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [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
<hr/>
* ### [kmath-coroutines](kmath-coroutines)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-dimensions](kmath-dimensions)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-for-real](kmath-for-real)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-functions](kmath-functions)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-geometry](kmath-geometry)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-histograms](kmath-histograms)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-memory](kmath-memory)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-prob](kmath-prob)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
* ### [kmath-viktor](kmath-viktor)
>
>
> **Maturity**: EXPERIMENTAL
<hr/>
## 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

View File

@ -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<ru.mipt.npm.gradle.KSciencePublishPlugin>()
}
/**
* TODO move to base plugin
*/
val generateReadme by tasks.creating {
group = "documentation"
fun List<Map<String, Any?>>.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<String, Map<String, Any?>>()
project.subprojects {
var properties: Map<String, Any?> = 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<String, Any?> =
(reader.parse(projectProperties) as? Map<String, Any?> ?: emptyMap()).withDefault { null }
val features: List<Map<String, Any?>>? 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<Map<String, Any?>>? by properties
if (features != null) {
appendln(features!!.generateFeatureString("$name/"))
}
}
}
val rootReadmeProperties: Map<String, Any> = 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()
)
}
}
}
readme{
readmeTemplate = file("docs/templates/README-TEMPLATE.md")
}

59
docs/images/KM.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 248 KiB

55
docs/images/KM_mono.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 18 KiB

91
docs/images/KMath.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 278 KiB

371
docs/images/KMath_mono.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 117 KiB

37
docs/templates/ARTIFACT-TEMPLATE.md vendored Normal file
View File

@ -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}")
> }
> ```

View File

@ -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

View File

@ -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

9
gradle.properties Normal file
View File

@ -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

View File

@ -94,7 +94,7 @@ public class DiffExpression(public val function: DerivativeStructureField.() ->
* TODO make result [DiffExpression]
*/
public fun derivative(orders: Map<String, Int>): Expression<Double> = 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

View File

@ -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 {

View File

@ -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"
)
}

View File

@ -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")
> }
> ```

View File

@ -0,0 +1,7 @@
# The Core Module (`kmath-core`)
The core features of KMath:
${features}
${artifact}

View File

@ -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"
}
]
}

View File

@ -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.*
/**

View File

@ -1,6 +1,6 @@
package kscience.kmath.structures
import kscience.memory.*
import kscience.kmath.memory.*
/**
* A non-boxing buffer over [Memory] object.

View File

@ -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<T, F : Field<T>, N : NDStructure<T>> : Field<N>, NDRing
public operator fun T.div(arg: N): N = map(arg) { divide(it, this@div) }
@ThreadLocal
public companion object {
private val realNDFieldCache: MutableMap<IntArray, RealNDField> = hashMapOf()

View File

@ -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<IntArray, Strides>()

View File

@ -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

View File

@ -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"

View File

@ -0,0 +1,20 @@
package kscience.kmath.dimensions
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KClass
@ThreadLocal
private val dimensionMap: MutableMap<UInt, Dimension> = hashMapOf(1u to D1, 2u to D2, 3u to D3)
@Suppress("UNCHECKED_CAST")
public actual fun <D : Dimension> Dimension.Companion.resolve(type: KClass<D>): D = dimensionMap
.entries
.map(MutableMap.MutableEntry<UInt, Dimension>::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
}
}

View File

@ -1 +1,4 @@
plugins { id("ru.mipt.npm.mpp") }
plugins {
id("ru.mipt.npm.mpp")
id("ru.mipt.npm.native")
}

View File

@ -1,4 +1,4 @@
package kscience.memory
package kscience.kmath.memory
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

View File

@ -1,4 +1,4 @@
package kscience.memory
package kscience.kmath.memory
/**
* A specification to read or write custom objects with fixed size in bytes.

View File

@ -1,4 +1,4 @@
package kscience.memory
package kscience.kmath.memory
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.DataView

View File

@ -1,4 +1,4 @@
package kscience.memory
package kscience.kmath.memory
import java.io.IOException
import java.nio.ByteBuffer

View File

@ -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)
}

View File

@ -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",