Even more documentation comments and minor refactorings #144

Merged
CommanderTvis merged 5 commits from even-more-docs into dev 2020-10-01 20:44:30 +03:00
41 changed files with 1101 additions and 98 deletions
Showing only changes of commit c97f81b277 - Show all commits

2
.gitignore vendored
View File

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

View File

@ -5,17 +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 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)

104
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](doc/features.md)
Actual feature list is [here](/docs/features.md)
* **Algebra**
* Algebraic structures like rings, spaces and field (**TODO** add example to wiki)
@ -66,9 +66,101 @@ can be used for a wide variety of purposes from high performance calculations to
* **Fitting** Non-linear curve fitting facilities
## Modules
<hr/>
* ### [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
@ -84,8 +176,8 @@ repositories{
}
dependencies{
api("kscience.kmath:kmath-core:${kmathVersion}")
//api("kscience.kmath:kmath-core-jvm:${kmathVersion}") for jvm-specific version
api("kscience.kmath:kmath-core:0.2.0-dev-1")
//api("kscience.kmath:kmath-core-jvm:0.2.0-dev-1") for jvm-specific version
}
```

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: String by extra("0.2.0-dev-1")
val kmathVersion: String by extra("0.2.0-dev-2")
val bintrayRepo: String by extra("kscience")
val githubProject: String 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")
}
@ -18,4 +18,10 @@ allprojects {
version = kmathVersion
}
subprojects { if (name.startsWith("kmath")) apply(plugin = "ru.mipt.npm.publish") }
subprojects {
if (name.startsWith("kmath")) apply<ru.mipt.npm.gradle.KSciencePublishPlugin>()
}
readme {
readmeTemplate = file("docs/templates/README-TEMPLATE.md")
}

View File

@ -1,14 +0,0 @@
# Features
* [Algebra](./algebra.md) - [Context-based](./contexts.md) operations on different primitives and structures.
* [NDStructures](./nd-structure.md)
* [Linear algebra](./linear.md) - Matrices, operations and linear equations solving. To be moved to separate module. Currently supports basic
api and multiple library back-ends.
* [Histograms](./histograms.md) - Multidimensional histogram calculation and operations.
* [Expressions](./expressions.md)
* Commons math integration

14
docs/features.md Normal file
View File

@ -0,0 +1,14 @@
# Features
* [Algebra](algebra.md) - [Context-based](contexts.md) operations on different primitives and structures.
* [NDStructures](nd-structure.md)
* [Linear algebra](linear.md) - Matrices, operations and linear equations solving. To be moved to separate module. Currently supports basic
api and multiple library back-ends.
* [Histograms](histograms.md) - Multidimensional histogram calculation and operations.
* [Expressions](expressions.md)
* Commons math integration

59
docs/images/KM.svg Normal file

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 248 KiB

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

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

91
docs/images/KMath.svg Normal file

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 278 KiB

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

Before

Width:  |  Height:  |  Size: 117 KiB

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

110
docs/templates/README-TEMPLATE.md vendored Normal file
View File

@ -0,0 +1,110 @@
[![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)
Bintray: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-core/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-core/_latestVersion)
Bintray-dev: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-core/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-core/_latestVersion)
# KMath
Could be pronounced as `key-math`.
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 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)
# Goal
* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM and JS for now and Native in future).
* Provide basic multiplatform implementations for those abstractions (without significant performance optimization).
* Provide bindings and wrappers with those abstractions for popular optimized platform libraries.
## Non-goals
* Be like Numpy. It was the idea at the beginning, but we decided that we can do better in terms of API.
* Provide best performance out of the box. We have specialized libraries for that. Need only API wrappers for them.
* Cover all cases as immediately and in one bundle. We will modularize everything and add new features gradually.
* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like for `Double` in the core. For that we will have specialization modules like `for-real`, which will give better experience for those, who want to work with specific types.
## Features
Actual feature list is [here](/docs/features.md)
* **Algebra**
* Algebraic structures like rings, spaces and field (**TODO** add example to wiki)
* Basic linear algebra operations (sums, products, etc.), backed by the `Space` API.
* Complex numbers backed by the `Field` API (meaning that they will be usable in any structure like vectors and N-dimensional arrays).
* Advanced linear algebra operations like matrix inversion and LU decomposition.
* **Array-like structures** Full support of many-dimensional array-like structures
including mixed arithmetic operations and function operations over arrays and numbers (with the added benefit of static type checking).
* **Expressions** By writing a single mathematical expression
once, users will be able to apply different types of objects to the expression by providing a context. Expressions
can be used for a wide variety of purposes from high performance calculations to code generation.
* **Histograms** Fast multi-dimensional histograms.
* **Streaming** Streaming operations on mathematical objects and objects buffers.
* **Type-safe dimensions** Type-safe dimensions for matrix operations.
* **Commons-math wrapper** It is planned to gradually wrap most parts of [Apache commons-math](http://commons.apache.org/proper/commons-math/)
library in Kotlin code and maybe rewrite some parts to better suit the Kotlin programming paradigm, however there is no fixed roadmap for that. Feel free
to submit a feature request if you want something to be done first.
## Planned features
* **Messaging** A mathematical notation to support multi-language and multi-node communication for mathematical tasks.
* **Array statistics**
* **Integration** Univariate and multivariate integration framework.
* **Probability and distributions**
* **Fitting** Non-linear curve fitting facilities
## Modules
$modules
## Multi-platform support
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
Calculation performance is one of major goals of KMath in the future, but in some cases it is not possible to achieve both performance and flexibility. We expect to focus on creating convenient universal API first and then work on increasing performance for specific cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be better than SciPy.
### Dependency
Release artifacts are accessible from bintray with following configuration (see documentation for [kotlin-multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) form more details):
```kotlin
repositories{
maven("https://dl.bintray.com/mipt-npm/kscience")
}
dependencies{
api("kscience.kmath:kmath-core:$version")
//api("kscience.kmath:kmath-core-jvm:$version") for jvm-specific version
}
```
Gradle `6.0+` is required for multiplatform artifacts.
### Development
Development builds are accessible from the reposirtory
```kotlin
repositories{
maven("https://dl.bintray.com/mipt-npm/dev")
}
```
with the same artifact names.
## Contributing
The project requires a lot of additional work. Please feel free to contribute in any way and propose new features.

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")
implementation("org.slf4j:slf4j-simple:1.7.30")
"benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8")
@ -36,9 +36,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

@ -19,39 +19,37 @@ import kotlin.reflect.KClass
* @author Alexander Nozik
*/
public fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<T> {
fun AsmBuilder<T>.visit(node: MST) {
when (node) {
is MST.Symbolic -> {
val symbol = try {
algebra.symbol(node.value)
} catch (ignored: Throwable) {
null
}
if (symbol != null)
loadTConstant(symbol)
else
loadVariable(node.value)
fun AsmBuilder<T>.visit(node: MST): Unit = when (node) {
is MST.Symbolic -> {
val symbol = try {
algebra.symbol(node.value)
} catch (ignored: Throwable) {
null
}
is MST.Numeric -> loadNumeric(node.value)
if (symbol != null)
loadTConstant(symbol)
else
loadVariable(node.value)
}
is MST.Unary -> buildAlgebraOperationCall(
context = algebra,
name = node.operation,
fallbackMethodName = "unaryOperation",
parameterTypes = arrayOf(MstType.fromMst(node.value))
) { visit(node.value) }
is MST.Numeric -> loadNumeric(node.value)
is MST.Binary -> buildAlgebraOperationCall(
context = algebra,
name = node.operation,
fallbackMethodName = "binaryOperation",
parameterTypes = arrayOf(MstType.fromMst(node.left), MstType.fromMst(node.right))
) {
visit(node.left)
visit(node.right)
}
is MST.Unary -> buildAlgebraOperationCall(
context = algebra,
name = node.operation,
fallbackMethodName = "unaryOperation",
parameterTypes = arrayOf(MstType.fromMst(node.value))
) { visit(node.value) }
is MST.Binary -> buildAlgebraOperationCall(
context = algebra,
name = node.operation,
fallbackMethodName = "binaryOperation",
parameterTypes = arrayOf(MstType.fromMst(node.left), MstType.fromMst(node.right))
) {
visit(node.left)
visit(node.right)
}
}

View File

@ -2,15 +2,21 @@
The core features of KMath:
- Algebraic structures: contexts and elements.
- ND structures.
- Buffers.
- Functional Expressions.
- Domains.
- Automatic differentiation.
- [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
- [expressions](src/commonMain/kotlin/kscience/kmath/expressions) : Functional Expressions
- [domains](src/commonMain/kotlin/kscience/kmath/domains) : Domains
- [autodif](src/commonMain/kotlin/kscience/kmath/misc/AutoDiff.kt) : Automatic differentiation
> #### Artifact:
> This module is distributed in the artifact `kscience.kmath:kmath-core:0.1.4-dev-8`.
>
> 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:**
>
@ -18,11 +24,11 @@ The core features of KMath:
> 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 {
> implementation 'kscience.kmath:kmath-core:0.1.4-dev-8'
> implementation 'kscience.kmath:kmath-core:0.2.0-dev-1'
> }
> ```
> **Gradle Kotlin DSL:**
@ -35,6 +41,6 @@ The core features of KMath:
> }
>
> dependencies {
> implementation("kscience.kmath:kmath-core:0.1.4-dev-8")
> implementation("kscience.kmath:kmath-core:0.2.0-dev-1")
> }
> ```

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

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

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.
@ -226,6 +227,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
/**
@ -224,6 +225,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.
@ -32,8 +32,7 @@ public fun <T : Any> MemoryReader.read(spec: MemorySpec<T>, offset: Int): T = wi
/**
* Writes the object [value] with [spec] starting from [offset].
*/
public fun <T : Any> MemoryWriter.write(spec: MemorySpec<T>, offset: Int, value: T): Unit =
with(spec) { write(offset, value) }
public fun <T : Any> MemoryWriter.write(spec: MemorySpec<T>, offset: Int, value: T): Unit = with(spec) { write(offset, value) }
/**
* Reads array of [size] objects mapped by [spec] at certain [offset].

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
@ -100,8 +100,7 @@ public actual fun Memory.Companion.allocate(length: Int): Memory =
* 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 =
ByteBufferMemory(checkNotNull(ByteBuffer.wrap(array)))
public actual fun Memory.Companion.wrap(array: ByteArray): Memory = ByteBufferMemory(checkNotNull(ByteBuffer.wrap(array)))
/**
* Wraps this [ByteBuffer] to [Memory] object.

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"
@ -32,9 +34,7 @@ include(
":kmath-histograms",
":kmath-commons",
":kmath-viktor",
":kmath-koma",
":kmath-prob",
":kmath-io",
":kmath-dimensions",
":kmath-for-real",
":kmath-geometry",