diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 626062abf..25f2cfd0d 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -3,105 +3,34 @@ name: Gradle build
on: [ push ]
jobs:
- build-ubuntu:
- runs-on: ubuntu-20.04
-
+ build:
+ strategy:
+ matrix:
+ os: [ macOS-latest, windows-latest ]
+ runs-on: ${{matrix.os}}
steps:
- - uses: actions/checkout@v2
+ - name: Checkout the repo
+ uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- - name: Grant execute permission for gradlew
- run: chmod +x gradlew
- - name: Install Chrome
- run: |
- sudo apt install -y libappindicator1 fonts-liberation
- wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome*.deb
- - name: Cache gradle
- uses: actions/cache@v2
- with:
- path: |
- .gradle
- build
- ~/.gradle
- key: gradle
- restore-keys: gradle
-
- - name: Cache konan
- uses: actions/cache@v2
- with:
- path: |
- ~/.konan/dependencies
- ~/.konan/kotlin-native-prebuilt-linux-*
- key: ${{ runner.os }}-konan
- restore-keys: ${{ runner.os }}-konan
- - name: Build with Gradle
- run: ./gradlew -Dorg.gradle.daemon=false --build-cache build
-
- build-osx:
- runs-on: macos-latest
-
- steps:
- - uses: actions/checkout@v2
- - name: Set up JDK 11
- uses: actions/setup-java@v1
- with:
- java-version: 11
- - name: Grant execute permission for gradlew
- run: chmod +x gradlew
- - name: Cache gradle
- uses: actions/cache@v2
- with:
- path: |
- .gradle
- build
- ~/.gradle
- key: gradle
- restore-keys: gradle
-
- - name: Cache konan
- uses: actions/cache@v2
- with:
- path: |
- ~/.konan/dependencies
- ~/.konan/kotlin-native-prebuilt-macos-*
- key: ${{ runner.os }}-konan
- restore-keys: ${{ runner.os }}-konan
- - name: Build with Gradle
- run: sudo ./gradlew -Dorg.gradle.daemon=false --build-cache build
-
- build-windows:
- runs-on: windows-latest
-
- steps:
- - uses: actions/checkout@v2
- - name: Set up JDK 11
- uses: actions/setup-java@v1
- with:
- java-version: 11
- - name: Grant execute permission for gradlew
- run: chmod +x gradlew
- name: Add msys to path
+ if: matrix.os == 'windows-latest'
run: SETX PATH "%PATH%;C:\msys64\mingw64\bin"
- name: Cache gradle
uses: actions/cache@v2
with:
- path: |
- .gradle
- build
- ~/.gradle
- key: ${{ runner.os }}-gradle
- restore-keys: ${{ runner.os }}-gradle
-
+ path: ~/.gradle/caches
+ key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
+ restore-keys: |
+ ${{ runner.os }}-gradle-
- name: Cache konan
uses: actions/cache@v2
with:
- path: |
- ~/.konan/dependencies
- ~/.konan/kotlin-native-prebuilt-mingw-*
- key: ${{ runner.os }}-konan
- restore-keys: ${{ runner.os }}-konan
- - name: Build with Gradle
- run: ./gradlew --build-cache build
+ path: ~/.konan
+ key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
+ restore-keys: |
+ ${{ runner.os }}-gradle-
+ - name: Build
+ run: ./gradlew build --no-daemon --stacktrace
diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml
new file mode 100644
index 000000000..82b0fb303
--- /dev/null
+++ b/.github/workflows/pages.yml
@@ -0,0 +1,40 @@
+name: Dokka publication
+
+on:
+ push:
+ branches:
+ - master
+
+jobs:
+ build:
+ runs-on: ubuntu-20.04
+ steps:
+ - name: Checkout the repo
+ uses: actions/checkout@v2
+ - name: Set up JDK 11
+ uses: actions/setup-java@v1
+ with:
+ java-version: 11
+ - name: Cache gradle
+ uses: actions/cache@v2
+ with:
+ path: ~/.gradle/caches
+ key: ubuntu-20.04-gradle-${{ hashFiles('*.gradle.kts') }}
+ restore-keys: |
+ ubuntu-20.04-gradle-
+ - name: Cache konan
+ uses: actions/cache@v2
+ with:
+ path: ~/.konan
+ key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
+ restore-keys: |
+ ${{ runner.os }}-gradle-
+ - name: Build
+ run: |
+ ./gradlew dokkaHtmlMultiModule --no-daemon --no-parallel --stacktrace
+ mv build/dokka/htmlMultiModule/-modules.html build/dokka/htmlMultiModule/index.html
+ - name: Deploy to GitHub Pages
+ uses: JamesIves/github-pages-deploy-action@4.1.0
+ with:
+ branch: gh-pages
+ folder: build/dokka/htmlMultiModule
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 40b172908..ca374574e 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -1,117 +1,59 @@
-name: Bintray Publish
+name: Gradle publish
on:
+ workflow_dispatch:
release:
types:
- created
jobs:
- build-ubuntu:
- runs-on: ubuntu-20.04
-
+ publish:
+ environment:
+ name: publish
+ strategy:
+ matrix:
+ os: [ macOS-latest, windows-latest ]
+ runs-on: ${{matrix.os}}
steps:
- - uses: actions/checkout@v2
+ - name: Checkout the repo
+ uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- - name: Grant execute permission for gradlew
- run: chmod +x gradlew
- - name: Install Chrome
- run: |
- sudo apt install -y libappindicator1 fonts-liberation
- wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome*.deb
- - name: Cache gradle
- uses: actions/cache@v2
- with:
- path: |
- .gradle
- build
- ~/.gradle
- key: gradle
- restore-keys: gradle
-
- - name: Cache konan
- uses: actions/cache@v2
- with:
- path: |
- ~/.konan/dependencies
- ~/.konan/kotlin-native-prebuilt-linux-*
- key: ${{ runner.os }}-konan
- restore-keys: ${{ runner.os }}-konan
- - name: Build with Gradle
- run: ./gradlew -Dorg.gradle.daemon=false --build-cache build
- - name: Run release task
- run: ./gradlew release -PbintrayUser=${{ secrets.BINTRAY_USER }} -PbintrayApiKey=${{ secrets.BINTRAY_KEY }}
-
- build-osx:
- runs-on: macos-latest
-
- steps:
- - uses: actions/checkout@v2
- - name: Set up JDK 11
- uses: actions/setup-java@v1
- with:
- java-version: 11
- - name: Grant execute permission for gradlew
- run: chmod +x gradlew
- - name: Cache gradle
- uses: actions/cache@v2
- with:
- path: |
- .gradle
- build
- ~/.gradle
- key: gradle
- restore-keys: gradle
-
- - name: Cache konan
- uses: actions/cache@v2
- with:
- path: |
- ~/.konan/dependencies
- ~/.konan/kotlin-native-prebuilt-macos-*
- key: ${{ runner.os }}-konan
- restore-keys: ${{ runner.os }}-konan
- - name: Build with Gradle
- run: sudo ./gradlew -Dorg.gradle.daemon=false --build-cache build
- - name: Run release task
- run: ./gradlew release -PbintrayUser=${{ secrets.BINTRAY_USER }} -PbintrayApiKey=${{ secrets.BINTRAY_KEY }}
-
- build-windows:
- runs-on: windows-latest
-
- steps:
- - uses: actions/checkout@v2
- - name: Set up JDK 11
- uses: actions/setup-java@v1
- with:
- java-version: 11
- - name: Grant execute permission for gradlew
- run: chmod +x gradlew
- name: Add msys to path
+ if: matrix.os == 'windows-latest'
run: SETX PATH "%PATH%;C:\msys64\mingw64\bin"
- name: Cache gradle
uses: actions/cache@v2
with:
- path: |
- .gradle
- build
- ~/.gradle
- key: ${{ runner.os }}-gradle
- restore-keys: ${{ runner.os }}-gradle
-
+ path: ~/.gradle/caches
+ key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
+ restore-keys: |
+ ${{ runner.os }}-gradle-
- name: Cache konan
uses: actions/cache@v2
with:
- path: |
- ~/.konan/dependencies
- ~/.konan/kotlin-native-prebuilt-mingw-*
- key: ${{ runner.os }}-konan
- restore-keys: ${{ runner.os }}-konan
- - name: Build with Gradle
- run: ./gradlew --build-cache build
- - name: Run release task
- run: ./gradlew release -PbintrayUser=${{ secrets.BINTRAY_USER }} -PbintrayApiKey=${{ secrets.BINTRAY_KEY }}
-
+ path: ~/.konan
+ key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
+ restore-keys: |
+ ${{ runner.os }}-gradle-
+ - name: Publish Windows Artifacts
+ if: matrix.os == 'windows-latest'
+ run: >
+ ./gradlew release --no-daemon
+ -Ppublishing.enabled=true
+ -Ppublishing.github.user=${{ secrets.PUBLISHING_GITHUB_USER }}
+ -Ppublishing.github.token=${{ secrets.PUBLISHING_GITHUB_TOKEN }}
+ -Ppublishing.space.user=${{ secrets.PUBLISHING_SPACE_USER }}
+ -Ppublishing.space.token=${{ secrets.PUBLISHING_SPACE_TOKEN }}
+ - name: Publish Mac Artifacts
+ if: matrix.os == 'macOS-latest'
+ run: >
+ ./gradlew release --no-daemon
+ -Ppublishing.enabled=true
+ -Ppublishing.platform=macosX64
+ -Ppublishing.github.user=${{ secrets.PUBLISHING_GITHUB_USER }}
+ -Ppublishing.github.token=${{ secrets.PUBLISHING_GITHUB_TOKEN }}
+ -Ppublishing.space.user=${{ secrets.PUBLISHING_SPACE_USER }}
+ -Ppublishing.space.token=${{ secrets.PUBLISHING_SPACE_TOKEN }}
diff --git a/.gitignore b/.gitignore
index bade7f08c..07589aa00 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,9 +2,14 @@
build/
out/
.idea/
+.vscode/
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
# Cache of project
.gradletasknamecache
+
+# Generated by javac -h and runtime
+*.class
+*.log
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2396c6b27..4ade9cd9c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,32 @@
## [Unreleased]
### Added
+- ScaleOperations interface
+- Field extends ScaleOperations
+- Basic integration API
+
+### Changed
+- Exponential operations merged with hyperbolic functions
+- Space is replaced by Group. Space is reserved for vector spaces.
+- VectorSpace is now a vector space
+- Buffer factories for primitives moved to MutableBuffer.Companion
+- NDStructure and NDAlgebra to StructureND and AlgebraND respectively
+- Real -> Double
+- DataSets are moved from functions to core
+
+### Deprecated
+
+### Removed
+- Nearest in Domain. To be implemented in geometry package.
+- Number multiplication and division in main Algebra chain
+- `contentEquals` from Buffer. It moved to the companion.
+
+### Fixed
+
+### Security
+
+## [0.2.0]
+### Added
- `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)
@@ -21,11 +47,11 @@
- Basic Quaternion vector support in `kmath-complex`.
### Changed
-- Package changed from `scientifik` to `kscience.kmath`
-- Gradle version: 6.6 -> 6.8
+- Package changed from `scientifik` to `space.kscience`
+- Gradle version: 6.6 -> 6.8.2
- Minor exceptions refactor (throwing `IllegalArgumentException` by argument checks instead of `IllegalStateException`)
- `Polynomial` secondary constructor made function
-- Kotlin version: 1.3.72 -> 1.4.21
+- Kotlin version: 1.3.72 -> 1.4.30
- `kmath-ast` doesn't depend on heavy `kotlin-reflect` library
- Full autodiff refactoring based on `Symbol`
- `kmath-prob` renamed to `kmath-stat`
@@ -41,6 +67,8 @@
- Refactor histograms. They are marked as prototype
- `Complex` and related features moved to a separate module `kmath-complex`
- Refactor AlgebraElement
+- `symbol` method in `Algebra` renamed to `bindSymbol` to avoid ambiguity
+- Add `out` projection to `Buffer` generic
### Deprecated
@@ -50,6 +78,7 @@
- `toGrid` method.
- Public visibility of `BufferAccessor2D`
- `Real` class
+- StructureND identity and equals
### Fixed
- `symbol` method in `MstExtendedField` (https://github.com/mipt-npm/kmath/pull/140)
diff --git a/README.md b/README.md
index 586d2185f..7080c757e 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,8 @@
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
[![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382)
-
![Gradle build](https://github.com/mipt-npm/kmath/workflows/Gradle%20build/badge.svg)
-
-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)
+[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22)
+[![Space](https://img.shields.io/maven-metadata/v?label=Space&metadataUrl=https%3A%2F%2Fmaven.pkg.jetbrains.space%2Fmipt-npm%2Fp%2Fsci%2Fmaven%2Fkscience%2Fkmath%2Fkmath-core%2Fmaven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/)
# KMath
@@ -89,12 +86,12 @@ KMath is a modular library. Different modules provide different features with di
> **Maturity**: PROTOTYPE
>
> **Features:**
-> - [expression-language](kmath-ast/src/jvmMain/kotlin/kscience/kmath/ast/parser.kt) : Expression language and its parser
-> - [mst](kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MST.kt) : MST (Mathematical Syntax Tree) as expression language's syntax intermediate representation
-> - [mst-building](kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt) : MST building algebraic structure
-> - [mst-interpreter](kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MST.kt) : MST interpreter
-> - [mst-jvm-codegen](kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt) : Dynamic MST to JVM bytecode compiler
-> - [mst-js-codegen](kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/estree.kt) : Dynamic MST to JS compiler
+> - [expression-language](kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt) : Expression language and its parser
+> - [mst](kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt) : MST (Mathematical Syntax Tree) as expression language's syntax intermediate representation
+> - [mst-building](kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt) : MST building algebraic structure
+> - [mst-interpreter](kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt) : MST interpreter
+> - [mst-jvm-codegen](kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt) : Dynamic MST to JVM bytecode compiler
+> - [mst-js-codegen](kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt) : Dynamic MST to JS compiler
@@ -110,8 +107,8 @@ KMath is a modular library. Different modules provide different features with di
> **Maturity**: PROTOTYPE
>
> **Features:**
-> - [complex](kmath-complex/src/commonMain/kotlin/kscience/kmath/complex/Complex.kt) : Complex Numbers
-> - [quaternion](kmath-complex/src/commonMain/kotlin/kscience/kmath/complex/Quaternion.kt) : Quaternions
+> - [complex](kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt) : Complex Numbers
+> - [quaternion](kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt) : Quaternions
@@ -121,15 +118,15 @@ KMath is a modular library. Different modules provide different features with di
> **Maturity**: DEVELOPMENT
>
> **Features:**
-> - [algebras](kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt) : Algebraic structures like rings, spaces and fields.
-> - [nd](kmath-core/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt) : Many-dimensional structures and operations on them.
-> - [linear](kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt) : Basic linear algebra operations (sums, products, etc.), backed by the `Space` API. Advanced linear algebra operations like matrix inversion and LU decomposition.
-> - [buffers](kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt) : One-dimensional structure
-> - [expressions](kmath-core/src/commonMain/kotlin/kscience/kmath/expressions) : By writing a single mathematical expression once, users will be able to apply different types of
+> - [algebras](kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt) : Algebraic structures like rings, spaces and fields.
+> - [nd](kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/StructureND.kt) : Many-dimensional structures and operations on them.
+> - [linear](kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt) : Basic linear algebra operations (sums, products, etc.), backed by the `Space` API. Advanced linear algebra operations like matrix inversion and LU decomposition.
+> - [buffers](kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffers.kt) : One-dimensional structure
+> - [expressions](kmath-core/src/commonMain/kotlin/space/kscience/kmath/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.
-> - [domains](kmath-core/src/commonMain/kotlin/kscience/kmath/domains) : Domains
-> - [autodif](kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/SimpleAutoDiff.kt) : Automatic differentiation
+> - [domains](kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains) : Domains
+> - [autodif](kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt) : Automatic differentiation
@@ -149,6 +146,12 @@ performance calculations to code generation.
>
>
> **Maturity**: PROTOTYPE
+>
+> **Features:**
+> - [ejml-vector](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt) : The Point implementation using SimpleMatrix.
+> - [ejml-matrix](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt) : The Matrix implementation using SimpleMatrix.
+> - [ejml-linear-space](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt) : The LinearSpace implementation using SimpleMatrix.
+
* ### [kmath-for-real](kmath-for-real)
@@ -159,9 +162,9 @@ One can still use generic algebras though.
> **Maturity**: EXPERIMENTAL
>
> **Features:**
-> - [RealVector](kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt) : Numpy-like operations for Buffers/Points
-> - [RealMatrix](kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealMatrix.kt) : Numpy-like operations for 2d real structures
-> - [grids](kmath-for-real/src/commonMain/kotlin/kscience/kmath/structures/grids.kt) : Uniform grid generators
+> - [DoubleVector](kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt) : Numpy-like operations for Buffers/Points
+> - [DoubleMatrix](kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleMatrix.kt) : Numpy-like operations for 2d real structures
+> - [grids](kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/structures/grids.kt) : Uniform grid generators
@@ -171,10 +174,10 @@ One can still use generic algebras though.
> **Maturity**: PROTOTYPE
>
> **Features:**
-> - [piecewise](kmath-functions/Piecewise functions.) : src/commonMain/kotlin/kscience/kmath/functions/Piecewise.kt
-> - [polynomials](kmath-functions/Polynomial functions.) : src/commonMain/kotlin/kscience/kmath/functions/Polynomial.kt
-> - [linear interpolation](kmath-functions/Linear XY interpolator.) : src/commonMain/kotlin/kscience/kmath/interpolation/LinearInterpolator.kt
-> - [spline interpolation](kmath-functions/Cubic spline XY interpolator.) : src/commonMain/kotlin/kscience/kmath/interpolation/SplineInterpolator.kt
+> - [piecewise](kmath-functions/Piecewise functions.) : src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt
+> - [polynomials](kmath-functions/Polynomial functions.) : src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt
+> - [linear interpolation](kmath-functions/Linear XY interpolator.) : src/commonMain/kotlin/space/kscience/kmath/interpolation/LinearInterpolator.kt
+> - [spline interpolation](kmath-functions/Cubic spline XY interpolator.) : src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt
@@ -208,9 +211,9 @@ One can still use generic algebras though.
> **Maturity**: EXPERIMENTAL
>
> **Features:**
-> - [nd4jarraystructure](kmath-nd4j/src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt) : NDStructure wrapper for INDArray
-> - [nd4jarrayrings](kmath-nd4j/src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt) : Rings over Nd4jArrayStructure of Int and Long
-> - [nd4jarrayfields](kmath-nd4j/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt) : Fields over Nd4jArrayStructure of Float and Double
+> - [nd4jarraystructure](kmath-nd4j/#) : NDStructure wrapper for INDArray
+> - [nd4jarrayrings](kmath-nd4j/#) : Rings over Nd4jArrayStructure of Int and Long
+> - [nd4jarrayfields](kmath-nd4j/#) : Fields over Nd4jArrayStructure of Float and Double
@@ -247,33 +250,22 @@ better than SciPy.
### Repositories
-Release artifacts are accessible from bintray with following configuration (see documentation of
-[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details):
+Release and development artifacts are accessible from mipt-npm [Space](https://www.jetbrains.com/space/) repository `https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven` (see documentation of
+[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details). The repository could be reached through [repo.kotlin.link](https://repo.kotlin.link) proxy:
```kotlin
repositories {
- maven("https://dl.bintray.com/mipt-npm/kscience")
- // maven("https://dl.bintray.com/mipt-npm/dev") for dev versions
+ maven("https://repo.kotlin.link")
}
dependencies {
- api("kscience.kmath:kmath-core:0.2.0-dev-7")
- // api("kscience.kmath:kmath-core-jvm:0.2.0-dev-7") for jvm-specific version
+ api("space.kscience:kmath-core:0.3.0-dev-3")
+ // api("kscience.kmath:kmath-core-jvm:0.3.0-dev-3") for jvm-specific version
}
```
Gradle `6.0+` is required for multiplatform artifacts.
-#### Development
-
-Development builds are uploaded to the separate repository:
-
-```kotlin
-repositories {
- maven("https://dl.bintray.com/mipt-npm/dev")
-}
-```
-
## Contributing
The project requires a lot of additional work. The most important thing we need is a feedback about what features are
diff --git a/build.gradle.kts b/build.gradle.kts
index 7fe132315..cc863a957 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,34 +1,49 @@
-import ru.mipt.npm.gradle.KSciencePublishPlugin
+import org.jetbrains.dokka.gradle.DokkaTask
+import ru.mipt.npm.gradle.KSciencePublishingPlugin
+import java.net.URL
plugins {
- id("ru.mipt.npm.project")
+ id("ru.mipt.npm.gradle.project")
}
-internal val kmathVersion: String by extra("0.2.0-dev-7")
-internal val bintrayRepo: String by extra("kscience")
-internal val githubProject: String by extra("kmath")
-
allprojects {
repositories {
jcenter()
maven("https://clojars.org/repo")
maven("https://dl.bintray.com/egor-bogomolov/astminer/")
maven("https://dl.bintray.com/hotkeytlt/maven")
- maven("https://dl.bintray.com/kotlin/kotlin-eap")
- maven("https://dl.bintray.com/kotlin/kotlinx")
- maven("https://dl.bintray.com/mipt-npm/dev")
- maven("https://dl.bintray.com/mipt-npm/kscience")
maven("https://jitpack.io")
maven("http://logicrunch.research.it.uu.se/maven/")
mavenCentral()
}
- group = "kscience.kmath"
- version = kmathVersion
+ group = "space.kscience"
+ version = "0.3.0-dev-4"
}
subprojects {
- if (name.startsWith("kmath")) apply()
+ if (name.startsWith("kmath")) apply()
+
+ afterEvaluate {
+ tasks.withType {
+ dokkaSourceSets.all {
+ val readmeFile = File(this@subprojects.projectDir, "./README.md")
+ if (readmeFile.exists())
+ includes.setFrom(includes + readmeFile.absolutePath)
+
+ arrayOf(
+ "http://ejml.org/javadoc/",
+ "https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/",
+ "https://deeplearning4j.org/api/latest/"
+ ).map { URL("${it}package-list") to URL(it) }.forEach { (a, b) ->
+ externalDocumentationLink {
+ packageListUrl.set(a)
+ url.set(b)
+ }
+ }
+ }
+ }
+ }
}
readme {
@@ -36,5 +51,11 @@ readme {
}
ksciencePublish {
- spaceRepo = "https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven"
-}
\ No newline at end of file
+ github("kmath")
+ space()
+ sonatype()
+}
+
+apiValidation {
+ nonPublicMarkers.add("space.kscience.kmath.misc.UnstableKMathAPI")
+}
diff --git a/docs/algebra.md b/docs/algebra.md
index c3227517f..84693bb81 100644
--- a/docs/algebra.md
+++ b/docs/algebra.md
@@ -5,7 +5,7 @@ operation, say `+`, one needs two objects of a type `T` and an algebra context,
say `Space`. Next one needs to run the actual operation in the context:
```kotlin
-import kscience.kmath.operations.*
+import space.kscience.kmath.operations.*
val a: T = ...
val b: T = ...
@@ -31,7 +31,7 @@ multiplication;
- [Ring](http://mathworld.wolfram.com/Ring.html) adds multiplication and its neutral element (i.e. 1);
- [Field](http://mathworld.wolfram.com/Field.html) adds division operation.
-A typical implementation of `Field` is the `RealField` which works on doubles, and `VectorSpace` for `Space`.
+A typical implementation of `Field` is the `DoubleField` which works on doubles, and `VectorSpace` for `Space`.
In some cases algebra context can hold additional operations like `exp` or `sin`, and then it inherits appropriate
interface. Also, contexts may have operations, which produce elements outside of the context. For example, `Matrix.dot`
@@ -47,7 +47,7 @@ but it also holds reference to the `ComplexField` singleton, which allows perfor
numbers without explicit involving the context like:
```kotlin
-import kscience.kmath.operations.*
+import space.kscience.kmath.operations.*
// Using elements
val c1 = Complex(1.0, 1.0)
@@ -82,7 +82,7 @@ operations in all performance-critical places. The performance of element operat
KMath submits both contexts and elements for builtin algebraic structures:
```kotlin
-import kscience.kmath.operations.*
+import space.kscience.kmath.operations.*
val c1 = Complex(1.0, 2.0)
val c2 = ComplexField.i
@@ -95,7 +95,7 @@ val c3 = ComplexField { c1 + c2 }
Also, `ComplexField` features special operations to mix complex and real numbers, for example:
```kotlin
-import kscience.kmath.operations.*
+import space.kscience.kmath.operations.*
val c1 = Complex(1.0, 2.0)
val c2 = ComplexField { c1 - 1.0 } // Returns: Complex(re=0.0, im=2.0)
diff --git a/docs/images/KM.svg b/docs/images/KM.svg
index 50126cbc5..83af21f35 100644
--- a/docs/images/KM.svg
+++ b/docs/images/KM.svg
@@ -13,27 +13,30 @@
version="1.1">image/svg+xml
+
+
+ image/svg+xml
+
+ image/svg+xml
+
+
+
+
+ image/svg+xml
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ #### 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
+## Artifact:
+
+The Maven coordinates of this project are `${group}:${name}:${version}`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation '${group}:${name}:${version}'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("${group}:${name}:${version}")
+}
+```
\ No newline at end of file
diff --git a/docs/templates/README-TEMPLATE.md b/docs/templates/README-TEMPLATE.md
index ec65c2db0..3502cdccd 100644
--- a/docs/templates/README-TEMPLATE.md
+++ b/docs/templates/README-TEMPLATE.md
@@ -1,11 +1,8 @@
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
[![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382)
-
![Gradle build](https://github.com/mipt-npm/kmath/workflows/Gradle%20build/badge.svg)
-
-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)
+[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22)
+[![Space](https://img.shields.io/maven-metadata/v?label=Space&metadataUrl=https%3A%2F%2Fmaven.pkg.jetbrains.space%2Fmipt-npm%2Fp%2Fsci%2Fmaven%2Fkscience%2Fkmath%2Fkmath-core%2Fmaven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/)
# KMath
@@ -97,33 +94,22 @@ better than SciPy.
### Repositories
-Release artifacts are accessible from bintray with following configuration (see documentation of
-[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details):
+Release and development artifacts are accessible from mipt-npm [Space](https://www.jetbrains.com/space/) repository `https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven` (see documentation of
+[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details). The repository could be reached through [repo.kotlin.link](https://repo.kotlin.link) proxy:
```kotlin
repositories {
- maven("https://dl.bintray.com/mipt-npm/kscience")
- // maven("https://dl.bintray.com/mipt-npm/dev") for dev versions
+ maven("https://repo.kotlin.link")
}
dependencies {
- api("kscience.kmath:kmath-core:$version")
+ api("${group}: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 uploaded to the separate repository:
-
-```kotlin
-repositories {
- maven("https://dl.bintray.com/mipt-npm/dev")
-}
-```
-
## Contributing
The project requires a lot of additional work. The most important thing we need is a feedback about what features are
diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts
index 054945eb7..5dd40b609 100644
--- a/examples/build.gradle.kts
+++ b/examples/build.gradle.kts
@@ -1,7 +1,6 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
- java
kotlin("jvm")
kotlin("plugin.allopen")
id("kotlinx.benchmark")
@@ -12,6 +11,7 @@ sourceSets.register("benchmarks")
repositories {
jcenter()
+ maven("https://repo.kotlin.link")
maven("https://clojars.org/repo")
maven("https://dl.bintray.com/egor-bogomolov/astminer/")
maven("https://dl.bintray.com/hotkeytlt/maven")
@@ -69,6 +69,14 @@ benchmark {
targets.register("benchmarks")
// This one matches sourceSet name above
+ configurations.register("buffer") {
+ warmups = 1 // number of warmup iterations
+ iterations = 3 // number of iterations
+ iterationTime = 500 // time in seconds per iteration
+ iterationTimeUnit = "ms" // time unity for iterationTime, default is seconds
+ include("BufferBenchmark")
+ }
+
configurations.register("dot") {
warmups = 1 // number of warmup iterations
iterations = 3 // number of iterations
@@ -76,6 +84,22 @@ benchmark {
iterationTimeUnit = "ms" // time unity for iterationTime, default is seconds
include("DotBenchmark")
}
+
+ configurations.register("expressions") {
+ warmups = 1 // number of warmup iterations
+ iterations = 3 // number of iterations
+ iterationTime = 500 // time in seconds per iteration
+ iterationTimeUnit = "ms" // time unity for iterationTime, default is seconds
+ include("ExpressionsInterpretersBenchmark")
+ }
+
+ configurations.register("matrixInverse") {
+ warmups = 1 // number of warmup iterations
+ iterations = 3 // number of iterations
+ iterationTime = 500 // time in seconds per iteration
+ iterationTimeUnit = "ms" // time unity for iterationTime, default is seconds
+ include("MatrixInverseBenchmark")
+ }
}
kotlin.sourceSets.all {
@@ -89,6 +113,6 @@ tasks.withType {
kotlinOptions.jvmTarget = "11"
}
-readme{
+readme {
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ArrayBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ArrayBenchmark.kt
deleted file mode 100644
index ebf31a590..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ArrayBenchmark.kt
+++ /dev/null
@@ -1,34 +0,0 @@
-package kscience.kmath.benchmarks
-
-import org.openjdk.jmh.annotations.Benchmark
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-import java.nio.IntBuffer
-
-@State(Scope.Benchmark)
-internal class ArrayBenchmark {
- @Benchmark
- fun benchmarkArrayRead() {
- var res = 0
- for (i in 1..size) res += array[size - i]
- }
-
- @Benchmark
- fun benchmarkBufferRead() {
- var res = 0
- for (i in 1..size) res += arrayBuffer[size - i]
- }
-
- @Benchmark
- fun nativeBufferRead() {
- var res = 0
- for (i in 1..size) res += nativeBuffer[size - i]
- }
-
- companion object {
- const val size: Int = 1000
- val array: IntArray = IntArray(size) { it }
- val arrayBuffer: IntBuffer = IntBuffer.wrap(array)
- val nativeBuffer: IntBuffer = IntBuffer.allocate(size).also { for (i in 0 until size) it.put(i, i) }
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/BufferBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/BufferBenchmark.kt
deleted file mode 100644
index e14b393e2..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/BufferBenchmark.kt
+++ /dev/null
@@ -1,34 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kscience.kmath.complex.Complex
-import kscience.kmath.complex.complex
-import kscience.kmath.structures.MutableBuffer
-import kscience.kmath.structures.RealBuffer
-import org.openjdk.jmh.annotations.Benchmark
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-
-@State(Scope.Benchmark)
-internal class BufferBenchmark {
- @Benchmark
- fun genericRealBufferReadWrite() {
- val buffer = RealBuffer(size) { it.toDouble() }
-
- (0 until size).forEach {
- buffer[it]
- }
- }
-
- @Benchmark
- fun complexBufferReadWrite() {
- val buffer = MutableBuffer.complex(size / 2) { Complex(it.toDouble(), -it.toDouble()) }
-
- (0 until size / 2).forEach {
- buffer[it]
- }
- }
-
- companion object {
- const val size: Int = 100
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/DotBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/DotBenchmark.kt
deleted file mode 100644
index 48be30cef..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/DotBenchmark.kt
+++ /dev/null
@@ -1,67 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kotlinx.benchmark.Benchmark
-import kscience.kmath.commons.linear.CMMatrixContext
-import kscience.kmath.ejml.EjmlMatrixContext
-import kscience.kmath.linear.BufferMatrixContext
-import kscience.kmath.linear.Matrix
-import kscience.kmath.linear.RealMatrixContext
-import kscience.kmath.operations.RealField
-import kscience.kmath.operations.invoke
-import kscience.kmath.structures.Buffer
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-import kotlin.random.Random
-
-@State(Scope.Benchmark)
-internal class DotBenchmark {
- companion object {
- val random = Random(12224)
- val dim = 1000
-
- //creating invertible matrix
- val matrix1 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
- val matrix2 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
-
- val cmMatrix1 = CMMatrixContext { matrix1.toCM() }
- val cmMatrix2 = CMMatrixContext { matrix2.toCM() }
-
- val ejmlMatrix1 = EjmlMatrixContext { matrix1.toEjml() }
- val ejmlMatrix2 = EjmlMatrixContext { matrix2.toEjml() }
- }
-
- @Benchmark
- fun cmDot() {
- CMMatrixContext {
- cmMatrix1 dot cmMatrix2
- }
- }
-
- @Benchmark
- fun ejmlDot() {
- EjmlMatrixContext {
- ejmlMatrix1 dot ejmlMatrix2
- }
- }
-
- @Benchmark
- fun ejmlDotWithConversion() {
- EjmlMatrixContext {
- matrix1 dot matrix2
- }
- }
-
- @Benchmark
- fun bufferedDot() {
- BufferMatrixContext(RealField, Buffer.Companion::real).invoke {
- matrix1 dot matrix2
- }
- }
-
- @Benchmark
- fun realDot() {
- RealMatrixContext {
- matrix1 dot matrix2
- }
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
deleted file mode 100644
index 9607dd499..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
+++ /dev/null
@@ -1,65 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kscience.kmath.asm.compile
-import kscience.kmath.ast.MstField
-import kscience.kmath.ast.mstInField
-import kscience.kmath.expressions.Expression
-import kscience.kmath.expressions.expressionInField
-import kscience.kmath.expressions.invoke
-import kscience.kmath.expressions.symbol
-import kscience.kmath.operations.Field
-import kscience.kmath.operations.RealField
-import org.openjdk.jmh.annotations.Benchmark
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-import kotlin.random.Random
-
-@State(Scope.Benchmark)
-internal class ExpressionsInterpretersBenchmark {
- private val algebra: Field = RealField
-
- @Benchmark
- fun functionalExpression() {
- val expr = algebra.expressionInField {
- symbol("x") * const(2.0) + const(2.0) / symbol("x") - const(16.0)
- }
-
- invokeAndSum(expr)
- }
-
- @Benchmark
- fun mstExpression() {
- val expr = algebra.mstInField {
- symbol("x") * 2.0 + 2.0 / symbol("x") - 16.0
- }
-
- invokeAndSum(expr)
- }
-
- @Benchmark
- fun asmExpression() {
- val expr = algebra.mstInField {
- MstField.symbol("x") * 2.0 + 2.0 / MstField.symbol("x") - 16.0
- }.compile()
-
- invokeAndSum(expr)
- }
-
- @Benchmark
- fun rawExpression() {
- val x by symbol
- val expr = Expression { args -> args.getValue(x) * 2.0 + 2.0 / args.getValue(x) - 16.0 }
- invokeAndSum(expr)
- }
-
- private fun invokeAndSum(expr: Expression) {
- val random = Random(0)
- var sum = 0.0
-
- repeat(1000000) {
- sum += expr("x" to random.nextDouble())
- }
-
- println(sum)
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt
deleted file mode 100644
index b54cff926..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt
+++ /dev/null
@@ -1,48 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kotlinx.benchmark.Benchmark
-import kscience.kmath.commons.linear.CMMatrixContext
-import kscience.kmath.commons.linear.CMMatrixContext.dot
-import kscience.kmath.commons.linear.inverse
-import kscience.kmath.ejml.EjmlMatrixContext
-import kscience.kmath.ejml.inverse
-import kscience.kmath.linear.Matrix
-import kscience.kmath.linear.MatrixContext
-import kscience.kmath.linear.inverseWithLup
-import kscience.kmath.linear.real
-import kscience.kmath.operations.invoke
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-import kotlin.random.Random
-
-@State(Scope.Benchmark)
-internal class LinearAlgebraBenchmark {
- companion object {
- val random = Random(1224)
- val dim = 100
-
- //creating invertible matrix
- val u = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
- val l = Matrix.real(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 }
- val matrix = l dot u
- }
-
- @Benchmark
- fun kmathLupInversion() {
- MatrixContext.real.inverseWithLup(matrix)
- }
-
- @Benchmark
- fun cmLUPInversion() {
- CMMatrixContext {
- inverse(matrix)
- }
- }
-
- @Benchmark
- fun ejmlInverse() {
- EjmlMatrixContext {
- inverse(matrix)
- }
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/NDFieldBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/NDFieldBenchmark.kt
deleted file mode 100644
index 5f7559d02..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/NDFieldBenchmark.kt
+++ /dev/null
@@ -1,45 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kscience.kmath.nd.*
-import kscience.kmath.operations.RealField
-import kscience.kmath.operations.invoke
-import kscience.kmath.structures.Buffer
-import org.openjdk.jmh.annotations.Benchmark
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-
-@State(Scope.Benchmark)
-internal class NDFieldBenchmark {
- @Benchmark
- fun autoFieldAdd() {
- autoField {
- var res: NDStructure = one
- repeat(n) { res += one }
- }
- }
-
- @Benchmark
- fun specializedFieldAdd() {
- specializedField {
- var res: NDStructure = one
- repeat(n) { res += 1.0 }
- }
- }
-
-
- @Benchmark
- fun boxingFieldAdd() {
- genericField {
- var res: NDStructure = one
- repeat(n) { res += 1.0 }
- }
- }
-
- companion object {
- const val dim: Int = 1000
- const val n: Int = 100
- val autoField = NDAlgebra.auto(RealField, dim, dim)
- val specializedField: RealNDField = NDAlgebra.real(dim, dim)
- val genericField = NDAlgebra.field(RealField, Buffer.Companion::boxing, dim, dim)
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ViktorBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ViktorBenchmark.kt
deleted file mode 100644
index 5cf6c2ab8..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ViktorBenchmark.kt
+++ /dev/null
@@ -1,52 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kscience.kmath.nd.*
-import kscience.kmath.operations.RealField
-import kscience.kmath.operations.invoke
-import kscience.kmath.viktor.ViktorNDField
-import org.jetbrains.bio.viktor.F64Array
-import org.openjdk.jmh.annotations.Benchmark
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-
-@State(Scope.Benchmark)
-internal class ViktorBenchmark {
- final val dim: Int = 1000
- final val n: Int = 100
-
- // automatically build context most suited for given type.
- final val autoField: NDField = NDAlgebra.auto(RealField, dim, dim)
- final val realField: RealNDField = NDAlgebra.real(dim, dim)
- final val viktorField: ViktorNDField = ViktorNDField(dim, dim)
-
- @Benchmark
- fun automaticFieldAddition() {
- autoField {
- var res: NDStructure = one
- repeat(n) { res += 1.0 }
- }
- }
-
- @Benchmark
- fun realFieldAddition() {
- realField {
- var res: NDStructure = one
- repeat(n) { res += 1.0 }
- }
- }
-
- @Benchmark
- fun viktorFieldAddition() {
- viktorField {
- var res = one
- repeat(n) { res += 1.0 }
- }
- }
-
- @Benchmark
- fun rawViktor() {
- val one = F64Array.full(init = 1.0, shape = intArrayOf(dim, dim))
- var res = one
- repeat(n) { res = res + one }
- }
-}
diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ViktorLogBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
deleted file mode 100644
index 914584b8e..000000000
--- a/examples/src/benchmarks/kotlin/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
+++ /dev/null
@@ -1,49 +0,0 @@
-package kscience.kmath.benchmarks
-
-import kscience.kmath.nd.*
-import kscience.kmath.operations.RealField
-import kscience.kmath.operations.invoke
-import kscience.kmath.viktor.ViktorNDField
-import org.jetbrains.bio.viktor.F64Array
-import org.openjdk.jmh.annotations.Benchmark
-import org.openjdk.jmh.annotations.Scope
-import org.openjdk.jmh.annotations.State
-
-@State(Scope.Benchmark)
-internal class ViktorLogBenchmark {
- final val dim: Int = 1000
- final val n: Int = 100
-
- // automatically build context most suited for given type.
- final val autoField: NDField = NDAlgebra.auto(RealField, dim, dim)
- final val realField: RealNDField = NDAlgebra.real(dim, dim)
- final val viktorField: ViktorNDField = ViktorNDField(intArrayOf(dim, dim))
-
-
- @Benchmark
- fun realFieldLog() {
- realField {
- val fortyTwo = produce { 42.0 }
- var res = one
- repeat(n) { res = ln(fortyTwo) }
- }
- }
-
- @Benchmark
- fun viktorFieldLog() {
- viktorField {
- val fortyTwo = produce { 42.0 }
- var res = one
- repeat(n) { res = ln(fortyTwo) }
- }
- }
-
- @Benchmark
- fun rawViktorLog() {
- val fortyTwo = F64Array.full(dim, dim, init = 42.0)
- var res: F64Array
- repeat(n) {
- res = fortyTwo.log()
- }
- }
-}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt
new file mode 100644
index 000000000..43b698c03
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt
@@ -0,0 +1,38 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import java.nio.IntBuffer
+
+@State(Scope.Benchmark)
+internal class ArrayBenchmark {
+ @Benchmark
+ fun benchmarkArrayRead(blackhole: Blackhole) {
+ var res = 0
+ for (i in 1..size) res += array[size - i]
+ blackhole.consume(res)
+ }
+
+ @Benchmark
+ fun benchmarkBufferRead(blackhole: Blackhole) {
+ var res = 0
+ for (i in 1..size) res += arrayBuffer[size - i]
+ blackhole.consume(res)
+ }
+
+ @Benchmark
+ fun nativeBufferRead(blackhole: Blackhole) {
+ var res = 0
+ for (i in 1..size) res += nativeBuffer[size - i]
+ blackhole.consume(res)
+ }
+
+ private companion object {
+ private const val size = 1000
+ private val array = IntArray(size) { it }
+ private val arrayBuffer = IntBuffer.wrap(array)
+ private val nativeBuffer = IntBuffer.allocate(size).also { for (i in 0 until size) it.put(i, i) }
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt
new file mode 100644
index 000000000..1db1d77dc
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt
@@ -0,0 +1,34 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import space.kscience.kmath.complex.Complex
+import space.kscience.kmath.complex.complex
+import space.kscience.kmath.structures.DoubleBuffer
+import space.kscience.kmath.structures.MutableBuffer
+
+@State(Scope.Benchmark)
+internal class BufferBenchmark {
+ @Benchmark
+ fun genericDoubleBufferReadWrite() {
+ val buffer = DoubleBuffer(size) { it.toDouble() }
+
+ (0 until size).forEach {
+ buffer[it]
+ }
+ }
+
+ @Benchmark
+ fun complexBufferReadWrite() {
+ val buffer = MutableBuffer.complex(size / 2) { Complex(it.toDouble(), -it.toDouble()) }
+
+ (0 until size / 2).forEach {
+ buffer[it]
+ }
+ }
+
+ private companion object {
+ private const val size = 100
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt
new file mode 100644
index 000000000..6a2126dc1
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt
@@ -0,0 +1,65 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import space.kscience.kmath.commons.linear.CMLinearSpace
+import space.kscience.kmath.ejml.EjmlLinearSpace
+import space.kscience.kmath.linear.LinearSpace
+import space.kscience.kmath.linear.invoke
+import space.kscience.kmath.operations.DoubleField
+import kotlin.random.Random
+
+@State(Scope.Benchmark)
+internal class DotBenchmark {
+ companion object {
+ val random = Random(12224)
+ const val dim = 1000
+
+ //creating invertible matrix
+ val matrix1 = LinearSpace.real.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
+ val matrix2 = LinearSpace.real.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
+
+ val cmMatrix1 = CMLinearSpace { matrix1.toCM() }
+ val cmMatrix2 = CMLinearSpace { matrix2.toCM() }
+
+ val ejmlMatrix1 = EjmlLinearSpace { matrix1.toEjml() }
+ val ejmlMatrix2 = EjmlLinearSpace { matrix2.toEjml() }
+ }
+
+ @Benchmark
+ fun cmDot(blackhole: Blackhole) {
+ CMLinearSpace.run {
+ blackhole.consume(cmMatrix1 dot cmMatrix2)
+ }
+ }
+
+ @Benchmark
+ fun ejmlDot(blackhole: Blackhole) {
+ EjmlLinearSpace {
+ blackhole.consume(ejmlMatrix1 dot ejmlMatrix2)
+ }
+ }
+
+ @Benchmark
+ fun ejmlDotWithConversion(blackhole: Blackhole) {
+ EjmlLinearSpace {
+ blackhole.consume(matrix1 dot matrix2)
+ }
+ }
+
+ @Benchmark
+ fun bufferedDot(blackhole: Blackhole) {
+ LinearSpace.auto(DoubleField).invoke {
+ blackhole.consume(matrix1 dot matrix2)
+ }
+ }
+
+ @Benchmark
+ fun realDot(blackhole: Blackhole) {
+ LinearSpace.real {
+ blackhole.consume(matrix1 dot matrix2)
+ }
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
new file mode 100644
index 000000000..2438e3979
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt
@@ -0,0 +1,74 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import space.kscience.kmath.asm.compile
+import space.kscience.kmath.ast.mstInField
+import space.kscience.kmath.expressions.Expression
+import space.kscience.kmath.expressions.expressionInField
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.misc.symbol
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.operations.bindSymbol
+import kotlin.random.Random
+
+@State(Scope.Benchmark)
+internal class ExpressionsInterpretersBenchmark {
+ @Benchmark
+ fun functionalExpression(blackhole: Blackhole) {
+ val expr = algebra.expressionInField {
+ val x = bindSymbol(x)
+ x * const(2.0) + const(2.0) / x - const(16.0)
+ }
+
+ invokeAndSum(expr, blackhole)
+ }
+
+ @Benchmark
+ fun mstExpression(blackhole: Blackhole) {
+ val expr = algebra.mstInField {
+ val x = bindSymbol(x)
+ x * 2.0 + number(2.0) / x - 16.0
+ }
+
+ invokeAndSum(expr, blackhole)
+ }
+
+ @Benchmark
+ fun asmExpression(blackhole: Blackhole) {
+ val expr = algebra.mstInField {
+ val x = bindSymbol(x)
+ x * 2.0 + number(2.0) / x - 16.0
+ }.compile()
+
+ invokeAndSum(expr, blackhole)
+ }
+
+ @Benchmark
+ fun rawExpression(blackhole: Blackhole) {
+ val expr = Expression { args ->
+ val x = args.getValue(x)
+ x * 2.0 + 2.0 / x - 16.0
+ }
+
+ invokeAndSum(expr, blackhole)
+ }
+
+ private fun invokeAndSum(expr: Expression, blackhole: Blackhole) {
+ val random = Random(0)
+ var sum = 0.0
+
+ repeat(1000000) {
+ sum += expr(x to random.nextDouble())
+ }
+
+ blackhole.consume(sum)
+ }
+
+ private companion object {
+ private val algebra = DoubleField
+ private val x by symbol
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt
new file mode 100644
index 000000000..7aa8ac975
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt
@@ -0,0 +1,48 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import space.kscience.kmath.commons.linear.CMLinearSpace
+import space.kscience.kmath.commons.linear.inverse
+import space.kscience.kmath.ejml.EjmlLinearSpace
+import space.kscience.kmath.ejml.inverse
+import space.kscience.kmath.linear.LinearSpace
+import space.kscience.kmath.linear.inverseWithLup
+import space.kscience.kmath.linear.invoke
+import kotlin.random.Random
+
+@State(Scope.Benchmark)
+internal class MatrixInverseBenchmark {
+ companion object {
+ val random = Random(1224)
+ const val dim = 100
+
+ private val space = LinearSpace.real
+
+ //creating invertible matrix
+ val u = space.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
+ val l = space.buildMatrix(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 }
+ val matrix = space { l dot u }
+ }
+
+ @Benchmark
+ fun kmathLupInversion(blackhole: Blackhole) {
+ blackhole.consume(LinearSpace.real.inverseWithLup(matrix))
+ }
+
+ @Benchmark
+ fun cmLUPInversion(blackhole: Blackhole) {
+ with(CMLinearSpace) {
+ blackhole.consume(inverse(matrix))
+ }
+ }
+
+ @Benchmark
+ fun ejmlInverse(blackhole: Blackhole) {
+ with(EjmlLinearSpace) {
+ blackhole.consume(inverse(matrix))
+ }
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt
new file mode 100644
index 000000000..09c415d9a
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt
@@ -0,0 +1,48 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import space.kscience.kmath.nd.*
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.structures.Buffer
+
+@State(Scope.Benchmark)
+internal class NDFieldBenchmark {
+ @Benchmark
+ fun autoFieldAdd(blackhole: Blackhole) {
+ with(autoField) {
+ var res: StructureND = one
+ repeat(n) { res += one }
+ blackhole.consume(res)
+ }
+ }
+
+ @Benchmark
+ fun specializedFieldAdd(blackhole: Blackhole) {
+ with(specializedField) {
+ var res: StructureND = one
+ repeat(n) { res += 1.0 }
+ blackhole.consume(res)
+ }
+ }
+
+
+ @Benchmark
+ fun boxingFieldAdd(blackhole: Blackhole) {
+ with(genericField) {
+ var res: StructureND = one
+ repeat(n) { res += 1.0 }
+ blackhole.consume(res)
+ }
+ }
+
+ private companion object {
+ private const val dim = 1000
+ private const val n = 100
+ private val autoField = AlgebraND.auto(DoubleField, dim, dim)
+ private val specializedField = AlgebraND.real(dim, dim)
+ private val genericField = AlgebraND.field(DoubleField, Buffer.Companion::boxing, dim, dim)
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt
new file mode 100644
index 000000000..fd0188bd6
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt
@@ -0,0 +1,61 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import org.jetbrains.bio.viktor.F64Array
+import space.kscience.kmath.nd.AlgebraND
+import space.kscience.kmath.nd.StructureND
+import space.kscience.kmath.nd.auto
+import space.kscience.kmath.nd.real
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.viktor.ViktorNDField
+
+@State(Scope.Benchmark)
+internal class ViktorBenchmark {
+ @Benchmark
+ fun automaticFieldAddition(blackhole: Blackhole) {
+ with(autoField) {
+ var res: StructureND = one
+ repeat(n) { res += 1.0 }
+ blackhole.consume(res)
+ }
+ }
+
+ @Benchmark
+ fun realFieldAddition(blackhole: Blackhole) {
+ with(realField) {
+ var res: StructureND = one
+ repeat(n) { res += 1.0 }
+ blackhole.consume(res)
+ }
+ }
+
+ @Benchmark
+ fun viktorFieldAddition(blackhole: Blackhole) {
+ with(viktorField) {
+ var res = one
+ repeat(n) { res += 1.0 }
+ blackhole.consume(res)
+ }
+ }
+
+ @Benchmark
+ fun rawViktor(blackhole: Blackhole) {
+ val one = F64Array.full(init = 1.0, shape = intArrayOf(dim, dim))
+ var res = one
+ repeat(n) { res = res + one }
+ blackhole.consume(res)
+ }
+
+ private companion object {
+ private const val dim = 1000
+ private const val n = 100
+
+ // automatically build context most suited for given type.
+ private val autoField = AlgebraND.auto(DoubleField, dim, dim)
+ private val realField = AlgebraND.real(dim, dim)
+ private val viktorField = ViktorNDField(dim, dim)
+ }
+}
diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
new file mode 100644
index 000000000..b6bd036ba
--- /dev/null
+++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
@@ -0,0 +1,53 @@
+package space.kscience.kmath.benchmarks
+
+import kotlinx.benchmark.Benchmark
+import kotlinx.benchmark.Blackhole
+import kotlinx.benchmark.Scope
+import kotlinx.benchmark.State
+import org.jetbrains.bio.viktor.F64Array
+import space.kscience.kmath.nd.AlgebraND
+import space.kscience.kmath.nd.auto
+import space.kscience.kmath.nd.real
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.viktor.ViktorFieldND
+
+@State(Scope.Benchmark)
+internal class ViktorLogBenchmark {
+ @Benchmark
+ fun realFieldLog(blackhole: Blackhole) {
+ with(realNdField) {
+ val fortyTwo = produce { 42.0 }
+ var res = one
+ repeat(n) { res = ln(fortyTwo) }
+ blackhole.consume(res)
+ }
+ }
+
+ @Benchmark
+ fun viktorFieldLog(blackhole: Blackhole) {
+ with(viktorField) {
+ val fortyTwo = produce { 42.0 }
+ var res = one
+ repeat(n) { res = ln(fortyTwo) }
+ blackhole.consume(res)
+ }
+ }
+
+ @Benchmark
+ fun rawViktorLog(blackhole: Blackhole) {
+ val fortyTwo = F64Array.full(dim, dim, init = 42.0)
+ lateinit var res: F64Array
+ repeat(n) { res = fortyTwo.log() }
+ blackhole.consume(res)
+ }
+
+ private companion object {
+ private const val dim = 1000
+ private const val n = 100
+
+ // automatically build context most suited for given type.
+ private val autoField = AlgebraND.auto(DoubleField, dim, dim)
+ private val realNdField = AlgebraND.real(dim, dim)
+ private val viktorField = ViktorFieldND(intArrayOf(dim, dim))
+ }
+}
diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt
deleted file mode 100644
index b3c827503..000000000
--- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt
+++ /dev/null
@@ -1,24 +0,0 @@
-package kscience.kmath.ast
-
-import kscience.kmath.asm.compile
-import kscience.kmath.expressions.derivative
-import kscience.kmath.expressions.invoke
-import kscience.kmath.expressions.symbol
-import kscience.kmath.kotlingrad.differentiable
-import kscience.kmath.operations.RealField
-
-/**
- * In this example, x^2-4*x-44 function is differentiated with Kotlin∇, and the autodiff result is compared with
- * valid derivative.
- */
-fun main() {
- val x by symbol
-
- val actualDerivative = MstExpression(RealField, "x^2-4*x-44".parseMath())
- .differentiable()
- .derivative(x)
- .compile()
-
- val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile()
- assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0))
-}
diff --git a/examples/src/main/kotlin/kscience/kmath/structures/ParallelRealNDField.kt b/examples/src/main/kotlin/kscience/kmath/structures/ParallelRealNDField.kt
deleted file mode 100644
index 48286c140..000000000
--- a/examples/src/main/kotlin/kscience/kmath/structures/ParallelRealNDField.kt
+++ /dev/null
@@ -1,103 +0,0 @@
-package kscience.kmath.structures
-
-import kscience.kmath.misc.UnstableKMathAPI
-import kscience.kmath.nd.*
-import kscience.kmath.operations.ExtendedField
-import kscience.kmath.operations.RealField
-import kscience.kmath.operations.RingWithNumbers
-import java.util.*
-import java.util.stream.IntStream
-
-/**
- * A demonstration implementation of NDField over Real using Java [DoubleStream] for parallel execution
- */
-@OptIn(UnstableKMathAPI::class)
-class StreamRealNDField(
- override val shape: IntArray,
-) : NDField,
- RingWithNumbers>,
- ExtendedField> {
-
- private val strides = DefaultStrides(shape)
- override val elementContext: RealField get() = RealField
- override val zero: NDBuffer by lazy { produce { zero } }
- override val one: NDBuffer by lazy { produce { one } }
-
- override fun number(value: Number): NDBuffer {
- val d = value.toDouble() // minimize conversions
- return produce { d }
- }
-
- private val NDStructure.buffer: RealBuffer
- get() = when {
- !shape.contentEquals(this@StreamRealNDField.shape) -> throw ShapeMismatchException(
- this@StreamRealNDField.shape,
- shape
- )
- this is NDBuffer && this.strides == this@StreamRealNDField.strides -> this.buffer as RealBuffer
- else -> RealBuffer(strides.linearSize) { offset -> get(strides.index(offset)) }
- }
-
-
- override fun produce(initializer: RealField.(IntArray) -> Double): NDBuffer {
- val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
- val index = strides.index(offset)
- RealField.initializer(index)
- }.toArray()
-
- return NDBuffer(strides, array.asBuffer())
- }
-
- override fun NDStructure.map(
- transform: RealField.(Double) -> Double,
- ): NDBuffer {
- val array = Arrays.stream(buffer.array).parallel().map { RealField.transform(it) }.toArray()
- return NDBuffer(strides, array.asBuffer())
- }
-
- override fun NDStructure.mapIndexed(
- transform: RealField.(index: IntArray, Double) -> Double,
- ): NDBuffer {
- val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
- RealField.transform(
- strides.index(offset),
- buffer.array[offset]
- )
- }.toArray()
-
- return NDBuffer(strides, array.asBuffer())
- }
-
- override fun combine(
- a: NDStructure,
- b: NDStructure,
- transform: RealField.(Double, Double) -> Double,
- ): NDBuffer {
- val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
- RealField.transform(a.buffer.array[offset], b.buffer.array[offset])
- }.toArray()
- return NDBuffer(strides, array.asBuffer())
- }
-
- override fun power(arg: NDStructure, pow: Number): NDBuffer = arg.map() { power(it, pow) }
-
- override fun exp(arg: NDStructure): NDBuffer = arg.map() { exp(it) }
-
- override fun ln(arg: NDStructure): NDBuffer = arg.map() { ln(it) }
-
- override fun sin(arg: NDStructure): NDBuffer = arg.map() { sin(it) }
- override fun cos(arg: NDStructure): NDBuffer = arg.map() { cos(it) }
- override fun tan(arg: NDStructure): NDBuffer = arg.map() { tan(it) }
- override fun asin(arg: NDStructure): NDBuffer = arg.map() { asin(it) }
- override fun acos(arg: NDStructure): NDBuffer = arg.map() { acos(it) }
- override fun atan(arg: NDStructure): NDBuffer = arg.map() { atan(it) }
-
- override fun sinh(arg: NDStructure): NDBuffer = arg.map() { sinh(it) }
- override fun cosh(arg: NDStructure): NDBuffer = arg.map() { cosh(it) }
- override fun tanh(arg: NDStructure): NDBuffer = arg.map() { tanh(it) }
- override fun asinh(arg: NDStructure): NDBuffer = arg.map() { asinh(it) }
- override fun acosh(arg: NDStructure): NDBuffer = arg.map() { acosh(it) }
- override fun atanh(arg: NDStructure): NDBuffer = arg.map() { atanh(it) }
-}
-
-fun NDAlgebra.Companion.realWithStream(vararg shape: Int): StreamRealNDField = StreamRealNDField(shape)
\ No newline at end of file
diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt
new file mode 100644
index 000000000..17c85eea5
--- /dev/null
+++ b/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt
@@ -0,0 +1,15 @@
+package space.kscience.kmath.ast
+
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.DoubleField
+
+fun main() {
+ val expr = DoubleField.mstInField {
+ val x = bindSymbol("x")
+ x * 2.0 + number(2.0) / x - 16.0
+ }
+
+ repeat(10000000) {
+ expr.invoke("x" to 1.0)
+ }
+}
\ No newline at end of file
diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt
new file mode 100644
index 000000000..138b3e708
--- /dev/null
+++ b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt
@@ -0,0 +1,24 @@
+package space.kscience.kmath.ast
+
+import space.kscience.kmath.asm.compile
+import space.kscience.kmath.expressions.derivative
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.kotlingrad.differentiable
+import space.kscience.kmath.misc.symbol
+import space.kscience.kmath.operations.DoubleField
+
+/**
+ * In this example, x^2-4*x-44 function is differentiated with Kotlin∇, and the autodiff result is compared with
+ * valid derivative.
+ */
+fun main() {
+ val x by symbol
+
+ val actualDerivative = MstExpression(DoubleField, "x^2-4*x-44".parseMath())
+ .differentiable()
+ .derivative(x)
+ .compile()
+
+ val expectedDerivative = MstExpression(DoubleField, "2*x-4".parseMath()).compile()
+ assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0))
+}
diff --git a/examples/src/main/kotlin/kscience/kmath/commons/fit/fitWithAutoDiff.kt b/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt
similarity index 100%
rename from examples/src/main/kotlin/kscience/kmath/commons/fit/fitWithAutoDiff.kt
rename to examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt
diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt
new file mode 100644
index 000000000..8940aeac9
--- /dev/null
+++ b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt
@@ -0,0 +1,28 @@
+package space.kscience.kmath.linear
+
+import space.kscience.kmath.real.*
+import space.kscience.kmath.structures.DoubleBuffer
+
+fun main() {
+ val x0 = DoubleVector(0.0, 0.0, 0.0)
+ val sigma = DoubleVector(1.0, 1.0, 1.0)
+
+ val gaussian: (Point) -> Double = { x ->
+ require(x.size == x0.size)
+ kotlin.math.exp(-((x - x0) / sigma).square().sum())
+ }
+
+ fun ((Point) -> Double).grad(x: Point): Point {
+ require(x.size == x0.size)
+ return DoubleBuffer(x.size) { i ->
+ val h = sigma[i] / 5
+ val dVector = DoubleBuffer(x.size) { if (it == i) h else 0.0 }
+ val f1 = invoke(x + dVector / 2)
+ val f0 = invoke(x - dVector / 2)
+ (f1 - f0) / h
+ }
+ }
+
+ println(gaussian.grad(x0))
+
+}
\ No newline at end of file
diff --git a/examples/src/main/kotlin/kscience/kmath/operations/BigIntDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt
similarity index 67%
rename from examples/src/main/kotlin/kscience/kmath/operations/BigIntDemo.kt
rename to examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt
index 0e9811ff8..6d5903cae 100644
--- a/examples/src/main/kotlin/kscience/kmath/operations/BigIntDemo.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt
@@ -1,4 +1,4 @@
-package kscience.kmath.operations
+package space.kscience.kmath.operations
fun main() {
val res = BigIntField { number(1) * 2 }
diff --git a/examples/src/main/kotlin/kscience/kmath/operations/ComplexDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt
similarity index 54%
rename from examples/src/main/kotlin/kscience/kmath/operations/ComplexDemo.kt
rename to examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt
index 6641aad85..105fb108e 100644
--- a/examples/src/main/kotlin/kscience/kmath/operations/ComplexDemo.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/operations/ComplexDemo.kt
@@ -1,18 +1,18 @@
-package kscience.kmath.operations
+package space.kscience.kmath.operations
-import kscience.kmath.complex.Complex
-import kscience.kmath.complex.complex
-import kscience.kmath.nd.NDAlgebra
+import space.kscience.kmath.complex.Complex
+import space.kscience.kmath.complex.complex
+import space.kscience.kmath.nd.AlgebraND
fun main() {
// 2d element
- val element = NDAlgebra.complex(2, 2).produce { (i, j) ->
+ val element = AlgebraND.complex(2, 2).produce { (i, j) ->
Complex(i.toDouble() - j.toDouble(), i.toDouble() + j.toDouble())
}
println(element)
// 1d element operation
- val result = with(NDAlgebra.complex(8)) {
+ val result = with(AlgebraND.complex(8)) {
val a = produce { (it) -> i * it - it.toDouble() }
val b = 3
val c = Complex(1.0, 1.0)
diff --git a/examples/src/main/kotlin/kscience/kmath/stat/DistributionBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt
similarity index 100%
rename from examples/src/main/kotlin/kscience/kmath/stat/DistributionBenchmark.kt
rename to examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt
diff --git a/examples/src/main/kotlin/kscience/kmath/stat/DistributionDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt
similarity index 100%
rename from examples/src/main/kotlin/kscience/kmath/stat/DistributionDemo.kt
rename to examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt
diff --git a/examples/src/main/kotlin/kscience/kmath/structures/ComplexND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt
similarity index 68%
rename from examples/src/main/kotlin/kscience/kmath/structures/ComplexND.kt
rename to examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt
index 2c0254139..68af2560b 100644
--- a/examples/src/main/kotlin/kscience/kmath/structures/ComplexND.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt
@@ -1,26 +1,26 @@
@file:Suppress("unused")
-package kscience.kmath.structures
+package space.kscience.kmath.structures
-import kscience.kmath.complex.*
-import kscience.kmath.linear.transpose
-import kscience.kmath.nd.NDAlgebra
-import kscience.kmath.nd.NDStructure
-import kscience.kmath.nd.as2D
-import kscience.kmath.nd.real
-import kscience.kmath.operations.invoke
+import space.kscience.kmath.complex.*
+import space.kscience.kmath.linear.transpose
+import space.kscience.kmath.nd.AlgebraND
+import space.kscience.kmath.nd.StructureND
+import space.kscience.kmath.nd.as2D
+import space.kscience.kmath.nd.real
+import space.kscience.kmath.operations.invoke
import kotlin.system.measureTimeMillis
fun main() {
val dim = 1000
val n = 1000
- val realField = NDAlgebra.real(dim, dim)
- val complexField: ComplexNDField = NDAlgebra.complex(dim, dim)
+ val realField = AlgebraND.real(dim, dim)
+ val complexField: ComplexFieldND = AlgebraND.complex(dim, dim)
val realTime = measureTimeMillis {
realField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) {
res += 1.0
}
@@ -31,7 +31,7 @@ fun main() {
val complexTime = measureTimeMillis {
complexField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) {
res += 1.0
}
diff --git a/examples/src/main/kotlin/kscience/kmath/structures/NDField.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt
similarity index 68%
rename from examples/src/main/kotlin/kscience/kmath/structures/NDField.kt
rename to examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt
index 1e7a19cf8..f7dc3280a 100644
--- a/examples/src/main/kotlin/kscience/kmath/structures/NDField.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt
@@ -1,12 +1,12 @@
-package kscience.kmath.structures
+package space.kscience.kmath.structures
import kotlinx.coroutines.GlobalScope
-import kscience.kmath.nd.*
-import kscience.kmath.nd4j.Nd4jArrayField
-import kscience.kmath.operations.RealField
-import kscience.kmath.operations.invoke
-import kscience.kmath.viktor.ViktorNDField
import org.nd4j.linalg.factory.Nd4j
+import space.kscience.kmath.nd.*
+import space.kscience.kmath.nd4j.Nd4jArrayField
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.operations.invoke
+import space.kscience.kmath.viktor.ViktorNDField
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.system.measureTimeMillis
@@ -24,56 +24,56 @@ fun main() {
val n = 1000
// automatically build context most suited for given type.
- val autoField = NDAlgebra.auto(RealField, dim, dim)
+ val autoField = AlgebraND.auto(DoubleField, dim, dim)
// specialized nd-field for Double. It works as generic Double field as well
- val realField = NDAlgebra.real(dim, dim)
+ val realField = AlgebraND.real(dim, dim)
//A generic boxing field. It should be used for objects, not primitives.
- val boxingField = NDAlgebra.field(RealField, Buffer.Companion::boxing, dim, dim)
+ val boxingField = AlgebraND.field(DoubleField, Buffer.Companion::boxing, dim, dim)
// Nd4j specialized field.
val nd4jField = Nd4jArrayField.real(dim, dim)
//viktor field
- val viktorField = ViktorNDField(dim,dim)
+ val viktorField = ViktorNDField(dim, dim)
//parallel processing based on Java Streams
- val parallelField = NDAlgebra.realWithStream(dim,dim)
+ val parallelField = AlgebraND.realWithStream(dim, dim)
measureAndPrint("Boxing addition") {
boxingField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Specialized addition") {
realField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Nd4j specialized addition") {
nd4jField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Viktor addition") {
viktorField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Parallel stream addition") {
parallelField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Automatic field addition") {
autoField {
- var res: NDStructure = one
+ var res: StructureND = one
repeat(n) { res += 1.0 }
}
}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt
new file mode 100644
index 000000000..6741209fc
--- /dev/null
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt
@@ -0,0 +1,107 @@
+package space.kscience.kmath.structures
+
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.nd.*
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.operations.ExtendedField
+import space.kscience.kmath.operations.NumbersAddOperations
+import java.util.*
+import java.util.stream.IntStream
+
+/**
+ * A demonstration implementation of NDField over Real using Java [DoubleStream] for parallel execution
+ */
+@OptIn(UnstableKMathAPI::class)
+class StreamDoubleFieldND(
+ override val shape: IntArray,
+) : FieldND,
+ NumbersAddOperations>,
+ ExtendedField> {
+
+ private val strides = DefaultStrides(shape)
+ override val elementContext: DoubleField get() = DoubleField
+ override val zero: BufferND by lazy { produce { zero } }
+ override val one: BufferND by lazy { produce { one } }
+
+ override fun number(value: Number): BufferND {
+ val d = value.toDouble() // minimize conversions
+ return produce { d }
+ }
+
+ private val StructureND.buffer: DoubleBuffer
+ get() = when {
+ !shape.contentEquals(this@StreamDoubleFieldND.shape) -> throw ShapeMismatchException(
+ this@StreamDoubleFieldND.shape,
+ shape
+ )
+ this is BufferND && this.strides == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer
+ else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) }
+ }
+
+
+ override fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND {
+ val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
+ val index = strides.index(offset)
+ DoubleField.initializer(index)
+ }.toArray()
+
+ return BufferND(strides, array.asBuffer())
+ }
+
+ override fun StructureND.map(
+ transform: DoubleField.(Double) -> Double,
+ ): BufferND {
+ val array = Arrays.stream(buffer.array).parallel().map { DoubleField.transform(it) }.toArray()
+ return BufferND(strides, array.asBuffer())
+ }
+
+ override fun StructureND.mapIndexed(
+ transform: DoubleField.(index: IntArray, Double) -> Double,
+ ): BufferND {
+ val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
+ DoubleField.transform(
+ strides.index(offset),
+ buffer.array[offset]
+ )
+ }.toArray()
+
+ return BufferND(strides, array.asBuffer())
+ }
+
+ override fun combine(
+ a: StructureND,
+ b: StructureND,
+ transform: DoubleField.(Double, Double) -> Double,
+ ): BufferND {
+ val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
+ DoubleField.transform(a.buffer.array[offset], b.buffer.array[offset])
+ }.toArray()
+ return BufferND(strides, array.asBuffer())
+ }
+
+ override fun StructureND.unaryMinus(): StructureND = map { -it }
+
+ override fun scale(a: StructureND, value: Double): StructureND = a.map { it * value }
+
+ override fun power(arg: StructureND, pow: Number): BufferND = arg.map { power(it, pow) }
+
+ override fun exp(arg: StructureND): BufferND = arg.map { exp(it) }
+
+ override fun ln(arg: StructureND): BufferND = arg.map { ln(it) }
+
+ override fun sin(arg: StructureND): BufferND = arg.map { sin(it) }
+ override fun cos(arg: StructureND): BufferND = arg.map { cos(it) }
+ override fun tan(arg: StructureND): BufferND = arg.map { tan(it) }
+ override fun asin(arg: StructureND): BufferND = arg.map { asin(it) }
+ override fun acos(arg: StructureND): BufferND = arg.map { acos(it) }
+ override fun atan(arg: StructureND): BufferND = arg.map { atan(it) }
+
+ override fun sinh(arg: StructureND): BufferND = arg.map { sinh(it) }
+ override fun cosh(arg: StructureND): BufferND = arg.map { cosh(it) }
+ override fun tanh(arg: StructureND): BufferND = arg.map { tanh(it) }
+ override fun asinh(arg: StructureND): BufferND = arg.map { asinh(it) }
+ override fun acosh(arg: StructureND): BufferND = arg.map { acosh(it) }
+ override fun atanh(arg: StructureND): BufferND = arg.map { atanh(it) }
+}
+
+fun AlgebraND.Companion.realWithStream(vararg shape: Int): StreamDoubleFieldND = StreamDoubleFieldND(shape)
\ No newline at end of file
diff --git a/examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt
similarity index 76%
rename from examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt
rename to examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt
index 40db51908..8b0a2ed0e 100644
--- a/examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt
@@ -1,15 +1,16 @@
-package kscience.kmath.structures
+package space.kscience.kmath.structures
-import kscience.kmath.nd.DefaultStrides
-import kscience.kmath.nd.NDBuffer
+import space.kscience.kmath.nd.BufferND
+import space.kscience.kmath.nd.DefaultStrides
import kotlin.system.measureTimeMillis
+@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
fun main() {
val n = 6000
val array = DoubleArray(n * n) { 1.0 }
- val buffer = RealBuffer(array)
+ val buffer = DoubleBuffer(array)
val strides = DefaultStrides(intArrayOf(n, n))
- val structure = NDBuffer(strides, buffer)
+ val structure = BufferND(strides, buffer)
measureTimeMillis {
var res = 0.0
diff --git a/examples/src/main/kotlin/kscience/kmath/structures/StructureWriteBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt
similarity index 67%
rename from examples/src/main/kotlin/kscience/kmath/structures/StructureWriteBenchmark.kt
rename to examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt
index 307b068df..27741be61 100644
--- a/examples/src/main/kotlin/kscience/kmath/structures/StructureWriteBenchmark.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt
@@ -1,12 +1,13 @@
-package kscience.kmath.structures
+package space.kscience.kmath.structures
-import kscience.kmath.nd.NDStructure
-import kscience.kmath.nd.mapToBuffer
+import space.kscience.kmath.nd.StructureND
+import space.kscience.kmath.nd.mapToBuffer
import kotlin.system.measureTimeMillis
+@Suppress("UNUSED_VARIABLE")
fun main() {
val n = 6000
- val structure = NDStructure.build(intArrayOf(n, n), Buffer.Companion::auto) { 1.0 }
+ val structure = StructureND.buffered(intArrayOf(n, n), Buffer.Companion::auto) { 1.0 }
structure.mapToBuffer { it + 1 } // warm-up
val time1 = measureTimeMillis { val res = structure.mapToBuffer { it + 1 } }
println("Structure mapping finished in $time1 millis")
@@ -19,10 +20,10 @@ fun main() {
println("Array mapping finished in $time2 millis")
- val buffer = RealBuffer(DoubleArray(n * n) { 1.0 })
+ val buffer = DoubleBuffer(DoubleArray(n * n) { 1.0 })
val time3 = measureTimeMillis {
- val target = RealBuffer(DoubleArray(n * n))
+ val target = DoubleBuffer(DoubleArray(n * n))
val res = array.forEachIndexed { index, value ->
target[index] = value + 1
}
diff --git a/examples/src/main/kotlin/kscience/kmath/structures/typeSafeDimensions.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt
similarity index 61%
rename from examples/src/main/kotlin/kscience/kmath/structures/typeSafeDimensions.kt
rename to examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt
index 96684f7dc..d2d130ab4 100644
--- a/examples/src/main/kotlin/kscience/kmath/structures/typeSafeDimensions.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt
@@ -1,11 +1,11 @@
-package kscience.kmath.structures
+package space.kscience.kmath.structures
-import kscience.kmath.dimensions.D2
-import kscience.kmath.dimensions.D3
-import kscience.kmath.dimensions.DMatrixContext
-import kscience.kmath.dimensions.Dimension
+import space.kscience.kmath.dimensions.D2
+import space.kscience.kmath.dimensions.D3
+import space.kscience.kmath.dimensions.DMatrixContext
+import space.kscience.kmath.dimensions.Dimension
-private fun DMatrixContext.simple() {
+private fun DMatrixContext.simple() {
val m1 = produce { i, j -> (i + j).toDouble() }
val m2 = produce { i, j -> (i + j).toDouble() }
@@ -17,7 +17,7 @@ private object D5 : Dimension {
override val dim: UInt = 5u
}
-private fun DMatrixContext.custom() {
+private fun DMatrixContext.custom() {
val m1 = produce { i, j -> (i + j).toDouble() }
val m2 = produce { i, j -> (i - j).toDouble() }
val m3 = produce { i, j -> (i - j).toDouble() }
diff --git a/gradle.properties b/gradle.properties
index 88b90f27b..50123b16c 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -3,6 +3,6 @@ kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.mpp.stability.nowarn=true
kotlin.native.enableDependencyPropagation=false
kotlin.parallel.tasks.in.project=true
-org.gradle.jvmargs=-XX:MaxMetaspaceSize=512m
+org.gradle.configureondemand=true
+org.gradle.jvmargs=-XX:MaxMetaspaceSize=9G
org.gradle.parallel=true
-systemProp.org.gradle.internal.publish.checksums.insecure=true
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 28ff446a2..442d9132e 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
diff --git a/kmath-ast/README.md b/kmath-ast/README.md
index c54c4afb1..ff954b914 100644
--- a/kmath-ast/README.md
+++ b/kmath-ast/README.md
@@ -1,52 +1,43 @@
-# Abstract Syntax Tree Expression Representation and Operations (`kmath-ast`)
+# Module kmath-ast
-This subproject implements the following features:
+Abstract syntax tree expression representation and related optimizations.
- - [expression-language](src/jvmMain/kotlin/kscience/kmath/ast/parser.kt) : Expression language and its parser
- - [mst](src/commonMain/kotlin/kscience/kmath/ast/MST.kt) : MST (Mathematical Syntax Tree) as expression language's syntax intermediate representation
- - [mst-building](src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt) : MST building algebraic structure
- - [mst-interpreter](src/commonMain/kotlin/kscience/kmath/ast/MST.kt) : MST interpreter
- - [mst-jvm-codegen](src/jvmMain/kotlin/kscience/kmath/asm/asm.kt) : Dynamic MST to JVM bytecode compiler
- - [mst-js-codegen](src/jsMain/kotlin/kscience/kmath/estree/estree.kt) : Dynamic MST to JS compiler
+ - [expression-language](src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt) : Expression language and its parser
+ - [mst](src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt) : MST (Mathematical Syntax Tree) as expression language's syntax intermediate representation
+ - [mst-building](src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt) : MST building algebraic structure
+ - [mst-interpreter](src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt) : MST interpreter
+ - [mst-jvm-codegen](src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt) : Dynamic MST to JVM bytecode compiler
+ - [mst-js-codegen](src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt) : Dynamic MST to JS compiler
-> #### Artifact:
->
-> This module artifact: `kscience.kmath:kmath-ast:0.2.0-dev-7`.
->
-> Bintray release version: [ ![Download](https://api.bintray.com/packages/mipt-npm/kscience/kmath-ast/images/download.svg) ](https://bintray.com/mipt-npm/kscience/kmath-ast/_latestVersion)
->
-> Bintray development version: [ ![Download](https://api.bintray.com/packages/mipt-npm/dev/kmath-ast/images/download.svg) ](https://bintray.com/mipt-npm/dev/kmath-ast/_latestVersion)
->
-> **Gradle:**
->
-> ```gradle
-> repositories {
-> maven { url "https://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 'kscience.kmath:kmath-ast:0.2.0-dev-7'
-> }
-> ```
-> **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("kscience.kmath:kmath-ast:0.2.0-dev-7")
-> }
-> ```
+## Artifact:
+
+The Maven coordinates of this project are `space.kscience:kmath-ast:0.3.0-dev-3`.
+
+**Gradle:**
+```gradle
+repositories {
+ maven { url 'https://repo.kotlin.link' }
+ maven { url 'https://dl.bintray.com/hotkeytlt/maven' }
+ maven { url "https://dl.bintray.com/kotlin/kotlin-eap" } // include for builds based on kotlin-eap
+}
+
+dependencies {
+ implementation 'space.kscience:kmath-ast:0.3.0-dev-3'
+}
+```
+**Gradle Kotlin DSL:**
+```kotlin
+repositories {
+ maven("https://repo.kotlin.link")
+ maven("https://dl.bintray.com/kotlin/kotlin-eap") // include for builds based on kotlin-eap
+ maven("https://dl.bintray.com/hotkeytlt/maven") // required for a
+}
+
+dependencies {
+ implementation("space.kscience:kmath-ast:0.3.0-dev-3")
+}
+```
## Dynamic expression code generation
@@ -58,19 +49,19 @@ a special implementation of `Expression` with implemented `invoke` function.
For example, the following builder:
```kotlin
-RealField.mstInField { symbol("x") + 2 }.compile()
+DoubleField.mstInField { symbol("x") + 2 }.compile()
```
… leads to generation of bytecode, which can be decompiled to the following Java class:
```java
-package kscience.kmath.asm.generated;
+package space.kscience.kmath.asm.generated;
import java.util.Map;
import kotlin.jvm.functions.Function2;
-import kscience.kmath.asm.internal.MapIntrinsics;
-import kscience.kmath.expressions.Expression;
-import kscience.kmath.expressions.Symbol;
+import space.kscience.kmath.asm.internal.MapIntrinsics;
+import space.kscience.kmath.expressions.Expression;
+import space.kscience.kmath.expressions.Symbol;
public final class AsmCompiledExpression_45045_0 implements Expression {
private final Object[] constants;
@@ -91,8 +82,8 @@ public final class AsmCompiledExpression_45045_0 implements Expression {
This API extends MST and MstExpression, so you may optimize as both of them:
```kotlin
-RealField.mstInField { symbol("x") + 2 }.compile()
-RealField.expression("x+2".parseMath())
+DoubleField.mstInField { symbol("x") + 2 }.compile()
+DoubleField.expression("x+2".parseMath())
```
#### Known issues
@@ -106,7 +97,7 @@ RealField.expression("x+2".parseMath())
A similar feature is also available on JS.
```kotlin
-RealField.mstInField { symbol("x") + 2 }.compile()
+DoubleField.mstInField { symbol("x") + 2 }.compile()
```
The code above returns expression implemented with such a JS function:
diff --git a/kmath-ast/build.gradle.kts b/kmath-ast/build.gradle.kts
index ed06b396b..e3a7faf0a 100644
--- a/kmath-ast/build.gradle.kts
+++ b/kmath-ast/build.gradle.kts
@@ -1,7 +1,7 @@
import ru.mipt.npm.gradle.Maturity
plugins {
- id("ru.mipt.npm.mpp")
+ id("ru.mipt.npm.gradle.mpp")
}
kotlin.js {
@@ -33,19 +33,24 @@ kotlin.sourceSets {
jsMain {
dependencies {
- implementation(npm("astring", "1.4.3"))
+ implementation(npm("astring", "1.7.0"))
}
}
jvmMain {
dependencies {
- api("com.github.h0tk3y.betterParse:better-parse:0.4.0")
- implementation("org.ow2.asm:asm:9.0")
- implementation("org.ow2.asm:asm-commons:9.0")
+ api("com.github.h0tk3y.betterParse:better-parse:0.4.1")
+ implementation("org.ow2.asm:asm:9.1")
+ implementation("org.ow2.asm:asm-commons:9.1")
}
}
}
+//Workaround for https://github.com/Kotlin/dokka/issues/1455
+tasks.dokkaHtml {
+ dependsOn(tasks.build)
+}
+
readme {
maturity = Maturity.PROTOTYPE
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
@@ -53,36 +58,36 @@ readme {
feature(
id = "expression-language",
description = "Expression language and its parser",
- ref = "src/jvmMain/kotlin/kscience/kmath/ast/parser.kt"
+ ref = "src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt"
)
feature(
id = "mst",
description = "MST (Mathematical Syntax Tree) as expression language's syntax intermediate representation",
- ref = "src/commonMain/kotlin/kscience/kmath/ast/MST.kt"
+ ref = "src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt"
)
feature(
id = "mst-building",
description = "MST building algebraic structure",
- ref = "src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt"
+ ref = "src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt"
)
feature(
id = "mst-interpreter",
description = "MST interpreter",
- ref = "src/commonMain/kotlin/kscience/kmath/ast/MST.kt"
+ ref = "src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt"
)
feature(
id = "mst-jvm-codegen",
description = "Dynamic MST to JVM bytecode compiler",
- ref = "src/jvmMain/kotlin/kscience/kmath/asm/asm.kt"
+ ref = "src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt"
)
feature(
id = "mst-js-codegen",
description = "Dynamic MST to JS compiler",
- ref = "src/jsMain/kotlin/kscience/kmath/estree/estree.kt"
+ ref = "src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt"
)
}
diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md
index ebaa6246a..db071adb4 100644
--- a/kmath-ast/docs/README-TEMPLATE.md
+++ b/kmath-ast/docs/README-TEMPLATE.md
@@ -1,6 +1,6 @@
-# Abstract Syntax Tree Expression Representation and Operations (`kmath-ast`)
+# Module kmath-ast
-This subproject implements the following features:
+Abstract syntax tree expression representation and related optimizations.
${features}
@@ -16,19 +16,19 @@ a special implementation of `Expression` with implemented `invoke` function.
For example, the following builder:
```kotlin
-RealField.mstInField { symbol("x") + 2 }.compile()
+DoubleField.mstInField { symbol("x") + 2 }.compile()
```
… leads to generation of bytecode, which can be decompiled to the following Java class:
```java
-package kscience.kmath.asm.generated;
+package space.kscience.kmath.asm.generated;
import java.util.Map;
import kotlin.jvm.functions.Function2;
-import kscience.kmath.asm.internal.MapIntrinsics;
-import kscience.kmath.expressions.Expression;
-import kscience.kmath.expressions.Symbol;
+import space.kscience.kmath.asm.internal.MapIntrinsics;
+import space.kscience.kmath.expressions.Expression;
+import space.kscience.kmath.expressions.Symbol;
public final class AsmCompiledExpression_45045_0 implements Expression {
private final Object[] constants;
@@ -49,8 +49,8 @@ public final class AsmCompiledExpression_45045_0 implements Expression {
This API extends MST and MstExpression, so you may optimize as both of them:
```kotlin
-RealField.mstInField { symbol("x") + 2 }.compile()
-RealField.expression("x+2".parseMath())
+DoubleField.mstInField { symbol("x") + 2 }.compile()
+DoubleField.expression("x+2".parseMath())
```
#### Known issues
@@ -64,7 +64,7 @@ RealField.expression("x+2".parseMath())
A similar feature is also available on JS.
```kotlin
-RealField.mstInField { symbol("x") + 2 }.compile()
+DoubleField.mstInField { symbol("x") + 2 }.compile()
```
The code above returns expression implemented with such a JS function:
diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MST.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt
similarity index 93%
rename from kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MST.kt
rename to kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt
index 212fd0d0b..c459d7ff5 100644
--- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MST.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MST.kt
@@ -1,7 +1,7 @@
-package kscience.kmath.ast
+package space.kscience.kmath.ast
-import kscience.kmath.operations.Algebra
-import kscience.kmath.operations.NumericAlgebra
+import space.kscience.kmath.operations.Algebra
+import space.kscience.kmath.operations.NumericAlgebra
/**
* A Mathematical Syntax Tree (MST) node for mathematical expressions.
@@ -55,7 +55,7 @@ public fun Algebra.evaluate(node: MST): T = when (node) {
is MST.Numeric -> (this as? NumericAlgebra)?.number(node.value)
?: error("Numeric nodes are not supported by $this")
- is MST.Symbolic -> symbol(node.value)
+ is MST.Symbolic -> bindSymbol(node.value)
is MST.Unary -> when {
this is NumericAlgebra && node.value is MST.Numeric -> unaryOperationFunction(node.operation)(number(node.value.value))
diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt
similarity index 67%
rename from kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt
rename to kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt
index eadbc85ee..c1aeae90e 100644
--- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt
@@ -1,14 +1,14 @@
-package kscience.kmath.ast
+package space.kscience.kmath.ast
-import kscience.kmath.misc.UnstableKMathAPI
-import kscience.kmath.operations.*
+import space.kscience.kmath.misc.UnstableKMathAPI
+import space.kscience.kmath.operations.*
/**
* [Algebra] over [MST] nodes.
*/
public object MstAlgebra : NumericAlgebra {
public override fun number(value: Number): MST.Numeric = MST.Numeric(value)
- public override fun symbol(value: String): MST.Symbolic = MST.Symbolic(value)
+ public override fun bindSymbol(value: String): MST.Symbolic = MST.Symbolic(value)
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
{ arg -> MST.Unary(operation, arg) }
@@ -18,25 +18,25 @@ public object MstAlgebra : NumericAlgebra {
}
/**
- * [Space] over [MST] nodes.
+ * [Group] over [MST] nodes.
*/
-public object MstSpace : Space, NumericAlgebra {
- public override val zero: MST.Numeric by lazy { number(0.0) }
+public object MstGroup : Group, NumericAlgebra, ScaleOperations {
+ public override val zero: MST.Numeric = number(0.0)
public override fun number(value: Number): MST.Numeric = MstAlgebra.number(value)
- public override fun symbol(value: String): MST.Symbolic = MstAlgebra.symbol(value)
- public override fun add(a: MST, b: MST): MST.Binary = binaryOperationFunction(SpaceOperations.PLUS_OPERATION)(a, b)
+ public override fun bindSymbol(value: String): MST.Symbolic = MstAlgebra.bindSymbol(value)
+ public override fun add(a: MST, b: MST): MST.Binary = binaryOperationFunction(GroupOperations.PLUS_OPERATION)(a, b)
public override operator fun MST.unaryPlus(): MST.Unary =
- unaryOperationFunction(SpaceOperations.PLUS_OPERATION)(this)
+ unaryOperationFunction(GroupOperations.PLUS_OPERATION)(this)
public override operator fun MST.unaryMinus(): MST.Unary =
- unaryOperationFunction(SpaceOperations.MINUS_OPERATION)(this)
+ unaryOperationFunction(GroupOperations.MINUS_OPERATION)(this)
public override operator fun MST.minus(b: MST): MST.Binary =
- binaryOperationFunction(SpaceOperations.MINUS_OPERATION)(this, b)
+ binaryOperationFunction(GroupOperations.MINUS_OPERATION)(this, b)
- public override fun multiply(a: MST, k: Number): MST.Binary =
- binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, number(k))
+ public override fun scale(a: MST, value: Double): MST.Binary =
+ binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, number(value))
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
MstAlgebra.binaryOperationFunction(operation)
@@ -49,25 +49,26 @@ public object MstSpace : Space, NumericAlgebra {
* [Ring] over [MST] nodes.
*/
@OptIn(UnstableKMathAPI::class)
-public object MstRing : Ring, RingWithNumbers {
- public override val zero: MST.Numeric
- get() = MstSpace.zero
+public object MstRing : Ring, NumbersAddOperations, ScaleOperations {
+ public override val zero: MST.Numeric get() = MstGroup.zero
+ public override val one: MST.Numeric = number(1.0)
- public override val one: MST.Numeric by lazy { number(1.0) }
+ public override fun number(value: Number): MST.Numeric = MstGroup.number(value)
+ public override fun bindSymbol(value: String): MST.Symbolic = MstAlgebra.bindSymbol(value)
+ public override fun add(a: MST, b: MST): MST.Binary = MstGroup.add(a, b)
+
+ public override fun scale(a: MST, value: Double): MST.Binary =
+ MstGroup.binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, MstGroup.number(value))
- public override fun number(value: Number): MST.Numeric = MstSpace.number(value)
- public override fun symbol(value: String): MST.Symbolic = MstSpace.symbol(value)
- public override fun add(a: MST, b: MST): MST.Binary = MstSpace.add(a, b)
- public override fun multiply(a: MST, k: Number): MST.Binary = MstSpace.multiply(a, k)
public override fun multiply(a: MST, b: MST): MST.Binary =
binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, b)
- public override operator fun MST.unaryPlus(): MST.Unary = MstSpace { +this@unaryPlus }
- public override operator fun MST.unaryMinus(): MST.Unary = MstSpace { -this@unaryMinus }
- public override operator fun MST.minus(b: MST): MST.Binary = MstSpace { this@minus - b }
+ public override operator fun MST.unaryPlus(): MST.Unary = MstGroup { +this@unaryPlus }
+ public override operator fun MST.unaryMinus(): MST.Unary = MstGroup { -this@unaryMinus }
+ public override operator fun MST.minus(b: MST): MST.Binary = MstGroup { this@minus - b }
public override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
- MstSpace.binaryOperationFunction(operation)
+ MstGroup.binaryOperationFunction(operation)
public override fun unaryOperationFunction(operation: String): (arg: MST) -> MST.Unary =
MstAlgebra.unaryOperationFunction(operation)
@@ -77,17 +78,18 @@ public object MstRing : Ring, RingWithNumbers {
* [Field] over [MST] nodes.
*/
@OptIn(UnstableKMathAPI::class)
-public object MstField : Field, RingWithNumbers {
- public override val zero: MST.Numeric
- get() = MstRing.zero
+public object MstField : Field, NumbersAddOperations, ScaleOperations {
+ public override val zero: MST.Numeric get() = MstRing.zero
- public override val one: MST.Numeric
- get() = MstRing.one
+ public override val one: MST.Numeric get() = MstRing.one
- public override fun symbol(value: String): MST.Symbolic = MstRing.symbol(value)
+ public override fun bindSymbol(value: String): MST.Symbolic = MstAlgebra.bindSymbol(value)
public override fun number(value: Number): MST.Numeric = MstRing.number(value)
public override fun add(a: MST, b: MST): MST.Binary = MstRing.add(a, b)
- public override fun multiply(a: MST, k: Number): MST.Binary = MstRing.multiply(a, k)
+
+ public override fun scale(a: MST, value: Double): MST.Binary =
+ MstGroup.binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, MstGroup.number(value))
+
public override fun multiply(a: MST, b: MST): MST.Binary = MstRing.multiply(a, b)
public override fun divide(a: MST, b: MST): MST.Binary =
binaryOperationFunction(FieldOperations.DIV_OPERATION)(a, b)
@@ -107,13 +109,10 @@ public object MstField : Field, RingWithNumbers {
* [ExtendedField] over [MST] nodes.
*/
public object MstExtendedField : ExtendedField, NumericAlgebra {
- public override val zero: MST.Numeric
- get() = MstField.zero
+ public override val zero: MST.Numeric get() = MstField.zero
+ public override val one: MST.Numeric get() = MstField.one
- public override val one: MST.Numeric
- get() = MstField.one
-
- public override fun symbol(value: String): MST.Symbolic = MstField.symbol(value)
+ public override fun bindSymbol(value: String): MST.Symbolic = MstAlgebra.bindSymbol(value)
public override fun number(value: Number): MST.Numeric = MstRing.number(value)
public override fun sin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.SIN_OPERATION)(arg)
public override fun cos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.COS_OPERATION)(arg)
@@ -121,14 +120,17 @@ public object MstExtendedField : ExtendedField, NumericAlgebra {
public override fun asin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ASIN_OPERATION)(arg)
public override fun acos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ACOS_OPERATION)(arg)
public override fun atan(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.ATAN_OPERATION)(arg)
- public override fun sinh(arg: MST): MST.Unary = unaryOperationFunction(HyperbolicOperations.SINH_OPERATION)(arg)
- public override fun cosh(arg: MST): MST.Unary = unaryOperationFunction(HyperbolicOperations.COSH_OPERATION)(arg)
- public override fun tanh(arg: MST): MST.Unary = unaryOperationFunction(HyperbolicOperations.TANH_OPERATION)(arg)
- public override fun asinh(arg: MST): MST.Unary = unaryOperationFunction(HyperbolicOperations.ASINH_OPERATION)(arg)
- public override fun acosh(arg: MST): MST.Unary = unaryOperationFunction(HyperbolicOperations.ACOSH_OPERATION)(arg)
- public override fun atanh(arg: MST): MST.Unary = unaryOperationFunction(HyperbolicOperations.ATANH_OPERATION)(arg)
+ public override fun sinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.SINH_OPERATION)(arg)
+ public override fun cosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.COSH_OPERATION)(arg)
+ public override fun tanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.TANH_OPERATION)(arg)
+ public override fun asinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ASINH_OPERATION)(arg)
+ public override fun acosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ACOSH_OPERATION)(arg)
+ public override fun atanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ATANH_OPERATION)(arg)
public override fun add(a: MST, b: MST): MST.Binary = MstField.add(a, b)
- public override fun multiply(a: MST, k: Number): MST.Binary = MstField.multiply(a, k)
+
+ public override fun scale(a: MST, value: Double): MST =
+ binaryOperation(GroupOperations.PLUS_OPERATION, a, number(value))
+
public override fun multiply(a: MST, b: MST): MST.Binary = MstField.multiply(a, b)
public override fun divide(a: MST, b: MST): MST.Binary = MstField.divide(a, b)
public override operator fun MST.unaryPlus(): MST.Unary = MstField { +this@unaryPlus }
diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
similarity index 76%
rename from kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt
rename to kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
index 03d33aa2b..5c43df068 100644
--- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
@@ -1,7 +1,9 @@
-package kscience.kmath.ast
+package space.kscience.kmath.ast
-import kscience.kmath.expressions.*
-import kscience.kmath.operations.*
+import space.kscience.kmath.expressions.*
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
+import space.kscience.kmath.operations.*
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@@ -15,14 +17,23 @@ import kotlin.contracts.contract
*/
public class MstExpression>(public val algebra: A, public val mst: MST) : Expression {
private inner class InnerAlgebra(val arguments: Map) : NumericAlgebra {
- override fun symbol(value: String): T = try {
- algebra.symbol(value)
+ override fun bindSymbol(value: String): T = try {
+ algebra.bindSymbol(value)
} catch (ignored: IllegalStateException) {
null
} ?: arguments.getValue(StringSymbol(value))
- override fun unaryOperationFunction(operation: String): (arg: T) -> T = algebra.unaryOperationFunction(operation)
- override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = algebra.binaryOperationFunction(operation)
+ override fun unaryOperation(operation: String, arg: T): T =
+ algebra.unaryOperation(operation, arg)
+
+ override fun binaryOperation(operation: String, left: T, right: T): T =
+ algebra.binaryOperation(operation, left, right)
+
+ override fun unaryOperationFunction(operation: String): (arg: T) -> T =
+ algebra.unaryOperationFunction(operation)
+
+ override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T =
+ algebra.binaryOperationFunction(operation)
@Suppress("UNCHECKED_CAST")
override fun number(value: Number): T = if (algebra is NumericAlgebra<*>)
@@ -45,13 +56,13 @@ public inline fun , E : Algebra> A.mst(
): MstExpression = MstExpression(this, mstAlgebra.block())
/**
- * Builds [MstExpression] over [Space].
+ * Builds [MstExpression] over [Group].
*
* @author Alexander Nozik
*/
-public inline fun > A.mstInSpace(block: MstSpace.() -> MST): MstExpression {
+public inline fun > A.mstInGroup(block: MstGroup.() -> MST): MstExpression {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
- return MstExpression(this, MstSpace.block())
+ return MstExpression(this, MstGroup.block())
}
/**
@@ -85,13 +96,13 @@ public inline fun > A.mstInExtendedField(b
}
/**
- * Builds [MstExpression] over [FunctionalExpressionSpace].
+ * Builds [MstExpression] over [FunctionalExpressionGroup].
*
* @author Alexander Nozik
*/
-public inline fun > FunctionalExpressionSpace.mstInSpace(block: MstSpace.() -> MST): MstExpression {
+public inline fun > FunctionalExpressionGroup.mstInGroup(block: MstGroup.() -> MST): MstExpression {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
- return algebra.mstInSpace(block)
+ return algebra.mstInGroup(block)
}
/**
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/astring/astring.typealises.kt b/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/astring/astring.typealises.kt
deleted file mode 100644
index 5a7fe4f16..000000000
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/astring/astring.typealises.kt
+++ /dev/null
@@ -1,3 +0,0 @@
-package kscience.kmath.estree.internal.astring
-
-internal typealias Generator = Any
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/stream/stream.kt b/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/stream/stream.kt
deleted file mode 100644
index b3c65a758..000000000
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/stream/stream.kt
+++ /dev/null
@@ -1,7 +0,0 @@
-package kscience.kmath.estree.internal.stream
-
-import kscience.kmath.estree.internal.emitter.Emitter
-
-internal open external class Stream : Emitter {
- open fun pipe(dest: Any, options: Any): Any
-}
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
similarity index 82%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/estree.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
index 5c08ada31..0bd9a386d 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/estree.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
@@ -1,20 +1,20 @@
-package kscience.kmath.estree
+package space.kscience.kmath.estree
-import kscience.kmath.ast.MST
-import kscience.kmath.ast.MST.*
-import kscience.kmath.ast.MstExpression
-import kscience.kmath.estree.internal.ESTreeBuilder
-import kscience.kmath.estree.internal.estree.BaseExpression
-import kscience.kmath.expressions.Expression
-import kscience.kmath.operations.Algebra
-import kscience.kmath.operations.NumericAlgebra
+import space.kscience.kmath.ast.MST
+import space.kscience.kmath.ast.MST.*
+import space.kscience.kmath.ast.MstExpression
+import space.kscience.kmath.estree.internal.ESTreeBuilder
+import space.kscience.kmath.estree.internal.estree.BaseExpression
+import space.kscience.kmath.expressions.Expression
+import space.kscience.kmath.operations.Algebra
+import space.kscience.kmath.operations.NumericAlgebra
@PublishedApi
internal fun MST.compileWith(algebra: Algebra): Expression {
fun ESTreeBuilder.visit(node: MST): BaseExpression = when (node) {
is Symbolic -> {
val symbol = try {
- algebra.symbol(node.value)
+ algebra.bindSymbol(node.value)
} catch (ignored: IllegalStateException) {
null
}
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/ESTreeBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
similarity index 91%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/ESTreeBuilder.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
index e1823813a..1e966d986 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/ESTreeBuilder.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
@@ -1,9 +1,9 @@
-package kscience.kmath.estree.internal
+package space.kscience.kmath.estree.internal
-import kscience.kmath.estree.internal.astring.generate
-import kscience.kmath.estree.internal.estree.*
-import kscience.kmath.expressions.Expression
-import kscience.kmath.expressions.Symbol
+import space.kscience.kmath.estree.internal.astring.generate
+import space.kscience.kmath.estree.internal.estree.*
+import space.kscience.kmath.expressions.Expression
+import space.kscience.kmath.misc.Symbol
internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExpression) {
private class GeneratedExpression(val executable: dynamic, val constants: Array) : Expression {
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/astring/astring.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.kt
similarity index 88%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/astring/astring.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.kt
index cf0a8de25..1b7500ddb 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/astring/astring.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.kt
@@ -1,9 +1,9 @@
@file:JsModule("astring")
@file:JsNonModule
-package kscience.kmath.estree.internal.astring
+package space.kscience.kmath.estree.internal.astring
-import kscience.kmath.estree.internal.estree.BaseNode
+import space.kscience.kmath.estree.internal.estree.BaseNode
internal external interface Options {
var indent: String?
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt
new file mode 100644
index 000000000..9e75a97c7
--- /dev/null
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt
@@ -0,0 +1,3 @@
+package space.kscience.kmath.estree.internal.astring
+
+internal typealias Generator = Any
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/emitter/emitter.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/emitter/emitter.kt
similarity index 87%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/emitter/emitter.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/emitter/emitter.kt
index 1e0a95a16..3028656f5 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/emitter/emitter.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/emitter/emitter.kt
@@ -1,4 +1,4 @@
-package kscience.kmath.estree.internal.emitter
+package space.kscience.kmath.estree.internal.emitter
internal open external class Emitter {
constructor(obj: Any)
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/estree/estree.extensions.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/estree/estree.extensions.kt
similarity index 97%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/estree/estree.extensions.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/estree/estree.extensions.kt
index 5bc197d0c..0e088c717 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/estree/estree.extensions.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/estree/estree.extensions.kt
@@ -1,4 +1,4 @@
-package kscience.kmath.estree.internal.estree
+package space.kscience.kmath.estree.internal.estree
internal fun Program(sourceType: String, vararg body: dynamic) = object : Program {
override var type = "Program"
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/estree/estree.kt
similarity index 99%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/estree/estree.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/estree/estree.kt
index a5385d1ee..4dc1bfc10 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/estree/estree.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/estree/estree.kt
@@ -1,4 +1,4 @@
-package kscience.kmath.estree.internal.estree
+package space.kscience.kmath.estree.internal.estree
import kotlin.js.RegExp
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/stream/stream.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/stream/stream.kt
new file mode 100644
index 000000000..c3477c482
--- /dev/null
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/stream/stream.kt
@@ -0,0 +1,7 @@
+package space.kscience.kmath.estree.internal.stream
+
+import space.kscience.kmath.estree.internal.emitter.Emitter
+
+internal open external class Stream : Emitter {
+ open fun pipe(dest: Any, options: Any): Any
+}
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/tsstdlib/lib.es2015.iterable.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/tsstdlib/lib.es2015.iterable.kt
similarity index 93%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/tsstdlib/lib.es2015.iterable.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/tsstdlib/lib.es2015.iterable.kt
index 22d4dd8e0..7f2975219 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/tsstdlib/lib.es2015.iterable.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/tsstdlib/lib.es2015.iterable.kt
@@ -1,4 +1,4 @@
-package kscience.kmath.estree.internal.tsstdlib
+package space.kscience.kmath.estree.internal.tsstdlib
internal external interface IteratorYieldResult {
var done: Boolean?
diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/tsstdlib/lib.es5.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/tsstdlib/lib.es5.kt
similarity index 97%
rename from kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/tsstdlib/lib.es5.kt
rename to kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/tsstdlib/lib.es5.kt
index 70f6d9702..7286b4a20 100644
--- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/tsstdlib/lib.es5.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/tsstdlib/lib.es5.kt
@@ -1,6 +1,6 @@
@file:Suppress("UNUSED_TYPEALIAS_PARAMETER", "DEPRECATION")
-package kscience.kmath.estree.internal.tsstdlib
+package space.kscience.kmath.estree.internal.tsstdlib
import kotlin.js.RegExp
diff --git a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeOperationsSupport.kt b/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeOperationsSupport.kt
deleted file mode 100644
index 72a4669d9..000000000
--- a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeOperationsSupport.kt
+++ /dev/null
@@ -1,41 +0,0 @@
-package kscience.kmath.estree
-
-import kscience.kmath.ast.mstInExtendedField
-import kscience.kmath.ast.mstInField
-import kscience.kmath.ast.mstInSpace
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.RealField
-import kotlin.random.Random
-import kotlin.test.Test
-import kotlin.test.assertEquals
-
-internal class TestESTreeOperationsSupport {
- @Test
- fun testUnaryOperationInvocation() {
- val expression = RealField.mstInSpace { -symbol("x") }.compile()
- val res = expression("x" to 2.0)
- assertEquals(-2.0, res)
- }
-
- @Test
- fun testBinaryOperationInvocation() {
- val expression = RealField.mstInSpace { -symbol("x") + number(1.0) }.compile()
- val res = expression("x" to 2.0)
- assertEquals(-1.0, res)
- }
-
- @Test
- fun testConstProductInvocation() {
- val res = RealField.mstInField { symbol("x") * 2 }("x" to 2.0)
- assertEquals(4.0, res)
- }
-
- @Test
- fun testMultipleCalls() {
- val e = RealField.mstInExtendedField { sin(symbol("x")).pow(4) - 6 * symbol("x") / tanh(symbol("x")) }.compile()
- val r = Random(0)
- var s = 0.0
- repeat(1000000) { s += e("x" to r.nextDouble()) }
- println(s)
- }
-}
diff --git a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeSpecialization.kt b/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeSpecialization.kt
deleted file mode 100644
index 9d0d17e58..000000000
--- a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeSpecialization.kt
+++ /dev/null
@@ -1,54 +0,0 @@
-package kscience.kmath.estree
-
-import kscience.kmath.ast.mstInField
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.RealField
-import kotlin.test.Test
-import kotlin.test.assertEquals
-
-internal class TestESTreeSpecialization {
- @Test
- fun testUnaryPlus() {
- val expr = RealField.mstInField { unaryOperationFunction("+")(symbol("x")) }.compile()
- assertEquals(2.0, expr("x" to 2.0))
- }
-
- @Test
- fun testUnaryMinus() {
- val expr = RealField.mstInField { unaryOperationFunction("-")(symbol("x")) }.compile()
- assertEquals(-2.0, expr("x" to 2.0))
- }
-
- @Test
- fun testAdd() {
- val expr = RealField.mstInField { binaryOperationFunction("+")(symbol("x"), symbol("x")) }.compile()
- assertEquals(4.0, expr("x" to 2.0))
- }
-
- @Test
- fun testSine() {
- val expr = RealField.mstInField { unaryOperationFunction("sin")(symbol("x")) }.compile()
- assertEquals(0.0, expr("x" to 0.0))
- }
-
- @Test
- fun testMinus() {
- val expr = RealField.mstInField { binaryOperationFunction("-")(symbol("x"), symbol("x")) }.compile()
- assertEquals(0.0, expr("x" to 2.0))
- }
-
- @Test
- fun testDivide() {
- val expr = RealField.mstInField { binaryOperationFunction("/")(symbol("x"), symbol("x")) }.compile()
- assertEquals(1.0, expr("x" to 2.0))
- }
-
- @Test
- fun testPower() {
- val expr = RealField
- .mstInField { binaryOperationFunction("pow")(symbol("x"), number(2)) }
- .compile()
-
- assertEquals(4.0, expr("x" to 2.0))
- }
-}
diff --git a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt
similarity index 69%
rename from kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt
rename to kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt
index ff10c8a43..683c0337c 100644
--- a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt
@@ -1,41 +1,41 @@
-package kscience.kmath.estree
+package space.kscience.kmath.estree
-import kscience.kmath.ast.*
-import kscience.kmath.complex.ComplexField
-import kscience.kmath.complex.toComplex
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.ByteRing
-import kscience.kmath.operations.RealField
+import space.kscience.kmath.ast.*
+import space.kscience.kmath.complex.ComplexField
+import space.kscience.kmath.complex.toComplex
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.ByteRing
+import space.kscience.kmath.operations.DoubleField
import kotlin.test.Test
import kotlin.test.assertEquals
internal class TestESTreeConsistencyWithInterpreter {
@Test
fun mstSpace() {
- val res1 = MstSpace.mstInSpace {
+ val res1 = MstGroup.mstInGroup {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- number(3.toByte()) - (number(2.toByte()) + (multiply(
+ number(3.toByte()) - (number(2.toByte()) + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + number(1.toByte()) * 3.toByte() - number(1.toByte())))
),
number(1)
- ) + symbol("x") + zero
+ ) + bindSymbol("x") + zero
}("x" to MST.Numeric(2))
- val res2 = MstSpace.mstInSpace {
+ val res2 = MstGroup.mstInGroup {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- number(3.toByte()) - (number(2.toByte()) + (multiply(
+ number(3.toByte()) - (number(2.toByte()) + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + number(1.toByte()) * 3.toByte() - number(1.toByte())))
),
number(1)
- ) + symbol("x") + zero
+ ) + bindSymbol("x") + zero
}.compile()("x" to MST.Numeric(2))
assertEquals(res1, res2)
@@ -46,9 +46,9 @@ internal class TestESTreeConsistencyWithInterpreter {
val res1 = ByteRing.mstInRing {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- (symbol("x") - (2.toByte() + (multiply(
+ (bindSymbol("x") - (2.toByte() + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + 1.toByte()))) * 3.0 - 1.toByte()
),
@@ -59,9 +59,9 @@ internal class TestESTreeConsistencyWithInterpreter {
val res2 = ByteRing.mstInRing {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- (symbol("x") - (2.toByte() + (multiply(
+ (bindSymbol("x") - (2.toByte() + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + 1.toByte()))) * 3.0 - 1.toByte()
),
number(1)
@@ -73,17 +73,17 @@ internal class TestESTreeConsistencyWithInterpreter {
@Test
fun realField() {
- val res1 = RealField.mstInField {
+ val res1 = DoubleField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
}("x" to 2.0)
- val res2 = RealField.mstInField {
+ val res2 = DoubleField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
@@ -96,7 +96,7 @@ internal class TestESTreeConsistencyWithInterpreter {
fun complexField() {
val res1 = ComplexField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
@@ -104,7 +104,7 @@ internal class TestESTreeConsistencyWithInterpreter {
val res2 = ComplexField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt
new file mode 100644
index 000000000..d59c048b6
--- /dev/null
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt
@@ -0,0 +1,43 @@
+package space.kscience.kmath.estree
+
+import space.kscience.kmath.ast.mstInExtendedField
+import space.kscience.kmath.ast.mstInField
+import space.kscience.kmath.ast.mstInGroup
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.DoubleField
+import kotlin.random.Random
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+internal class TestESTreeOperationsSupport {
+ @Test
+ fun testUnaryOperationInvocation() {
+ val expression = DoubleField.mstInGroup { -bindSymbol("x") }.compile()
+ val res = expression("x" to 2.0)
+ assertEquals(-2.0, res)
+ }
+
+ @Test
+ fun testBinaryOperationInvocation() {
+ val expression = DoubleField.mstInGroup { -bindSymbol("x") + number(1.0) }.compile()
+ val res = expression("x" to 2.0)
+ assertEquals(-1.0, res)
+ }
+
+ @Test
+ fun testConstProductInvocation() {
+ val res = DoubleField.mstInField { bindSymbol("x") * 2 }("x" to 2.0)
+ assertEquals(4.0, res)
+ }
+
+ @Test
+ fun testMultipleCalls() {
+ val e =
+ DoubleField.mstInExtendedField { sin(bindSymbol("x")).pow(4) - 6 * bindSymbol("x") / tanh(bindSymbol("x")) }
+ .compile()
+ val r = Random(0)
+ var s = 0.0
+ repeat(1000000) { s += e("x" to r.nextDouble()) }
+ println(s)
+ }
+}
diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeSpecialization.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeSpecialization.kt
new file mode 100644
index 000000000..6be963175
--- /dev/null
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeSpecialization.kt
@@ -0,0 +1,54 @@
+package space.kscience.kmath.estree
+
+import space.kscience.kmath.ast.mstInField
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.DoubleField
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+internal class TestESTreeSpecialization {
+ @Test
+ fun testUnaryPlus() {
+ val expr = DoubleField.mstInField { unaryOperationFunction("+")(bindSymbol("x")) }.compile()
+ assertEquals(2.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testUnaryMinus() {
+ val expr = DoubleField.mstInField { unaryOperationFunction("-")(bindSymbol("x")) }.compile()
+ assertEquals(-2.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testAdd() {
+ val expr = DoubleField.mstInField { binaryOperationFunction("+")(bindSymbol("x"), bindSymbol("x")) }.compile()
+ assertEquals(4.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testSine() {
+ val expr = DoubleField.mstInField { unaryOperationFunction("sin")(bindSymbol("x")) }.compile()
+ assertEquals(0.0, expr("x" to 0.0))
+ }
+
+ @Test
+ fun testMinus() {
+ val expr = DoubleField.mstInField { binaryOperationFunction("-")(bindSymbol("x"), bindSymbol("x")) }.compile()
+ assertEquals(0.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testDivide() {
+ val expr = DoubleField.mstInField { binaryOperationFunction("/")(bindSymbol("x"), bindSymbol("x")) }.compile()
+ assertEquals(1.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testPower() {
+ val expr = DoubleField
+ .mstInField { binaryOperationFunction("pow")(bindSymbol("x"), number(2)) }
+ .compile()
+
+ assertEquals(4.0, expr("x" to 2.0))
+ }
+}
diff --git a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeVariables.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeVariables.kt
similarity index 52%
rename from kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeVariables.kt
rename to kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeVariables.kt
index 846120ee2..ee8f4c6f5 100644
--- a/kmath-ast/src/jsTest/kotlin/kscience/kmath/estree/TestESTreeVariables.kt
+++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeVariables.kt
@@ -1,8 +1,8 @@
-package kscience.kmath.estree
+package space.kscience.kmath.estree
-import kscience.kmath.ast.mstInRing
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.ByteRing
+import space.kscience.kmath.ast.mstInRing
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.ByteRing
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -10,13 +10,13 @@ import kotlin.test.assertFailsWith
internal class TestESTreeVariables {
@Test
fun testVariable() {
- val expr = ByteRing.mstInRing { symbol("x") }.compile()
+ val expr = ByteRing.mstInRing { bindSymbol("x") }.compile()
assertEquals(1.toByte(), expr("x" to 1.toByte()))
}
@Test
fun testUndefinedVariableFails() {
- val expr = ByteRing.mstInRing { symbol("x") }.compile()
+ val expr = ByteRing.mstInRing { bindSymbol("x") }.compile()
assertFailsWith { expr() }
}
}
diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt
similarity index 84%
rename from kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt
rename to kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt
index 55cdec243..8875bd715 100644
--- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt
@@ -1,13 +1,13 @@
-package kscience.kmath.asm
+package space.kscience.kmath.asm
-import kscience.kmath.asm.internal.AsmBuilder
-import kscience.kmath.asm.internal.buildName
-import kscience.kmath.ast.MST
-import kscience.kmath.ast.MST.*
-import kscience.kmath.ast.MstExpression
-import kscience.kmath.expressions.Expression
-import kscience.kmath.operations.Algebra
-import kscience.kmath.operations.NumericAlgebra
+import space.kscience.kmath.asm.internal.AsmBuilder
+import space.kscience.kmath.asm.internal.buildName
+import space.kscience.kmath.ast.MST
+import space.kscience.kmath.ast.MST.*
+import space.kscience.kmath.ast.MstExpression
+import space.kscience.kmath.expressions.Expression
+import space.kscience.kmath.operations.Algebra
+import space.kscience.kmath.operations.NumericAlgebra
/**
* Compiles given MST to an Expression using AST compiler.
@@ -22,7 +22,7 @@ internal fun MST.compileWith(type: Class, algebra: Algebra): Exp
fun AsmBuilder.visit(node: MST): Unit = when (node) {
is Symbolic -> {
val symbol = try {
- algebra.symbol(node.value)
+ algebra.bindSymbol(node.value)
} catch (ignored: IllegalStateException) {
null
}
diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt
similarity index 95%
rename from kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt
rename to kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt
index cd1236b70..a03af5bf9 100644
--- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/AsmBuilder.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt
@@ -1,15 +1,14 @@
-package kscience.kmath.asm.internal
+package space.kscience.kmath.asm.internal
-import kscience.kmath.asm.internal.AsmBuilder.ClassLoader
-import kscience.kmath.ast.MST
-import kscience.kmath.expressions.Expression
import org.objectweb.asm.*
import org.objectweb.asm.Opcodes.*
import org.objectweb.asm.Type.*
import org.objectweb.asm.commons.InstructionAdapter
+import space.kscience.kmath.asm.internal.AsmBuilder.ClassLoader
+import space.kscience.kmath.ast.MST
+import space.kscience.kmath.expressions.Expression
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
-import java.lang.reflect.Modifier
import java.util.stream.Collectors.toMap
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@@ -310,7 +309,7 @@ internal class AsmBuilder(
/**
* ASM type for [Expression].
*/
- val EXPRESSION_TYPE: Type by lazy { getObjectType("kscience/kmath/expressions/Expression") }
+ val EXPRESSION_TYPE: Type by lazy { getObjectType("space/kscience/kmath/expressions/Expression") }
/**
* ASM type for [java.util.Map].
@@ -335,11 +334,11 @@ internal class AsmBuilder(
/**
* ASM type for MapIntrinsics.
*/
- val MAP_INTRINSICS_TYPE: Type by lazy { getObjectType("kscience/kmath/asm/internal/MapIntrinsics") }
+ val MAP_INTRINSICS_TYPE: Type by lazy { getObjectType("space/kscience/kmath/asm/internal/MapIntrinsics") }
/**
* ASM Type for [kscience.kmath.expressions.Symbol].
*/
- val SYMBOL_TYPE: Type by lazy { getObjectType("kscience/kmath/expressions/Symbol") }
+ val SYMBOL_TYPE: Type by lazy { getObjectType("space/kscience/kmath/expressions/Symbol") }
}
}
diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/codegenUtils.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt
similarity index 94%
rename from kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/codegenUtils.kt
rename to kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt
index 6d5d19d42..4522c966f 100644
--- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/codegenUtils.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt
@@ -1,9 +1,9 @@
-package kscience.kmath.asm.internal
+package space.kscience.kmath.asm.internal
-import kscience.kmath.ast.MST
-import kscience.kmath.expressions.Expression
import org.objectweb.asm.*
import org.objectweb.asm.commons.InstructionAdapter
+import space.kscience.kmath.ast.MST
+import space.kscience.kmath.expressions.Expression
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@@ -86,7 +86,7 @@ internal inline fun ClassWriter.visitField(
descriptor: String,
signature: String?,
value: Any?,
- block: FieldVisitor.() -> Unit
+ block: FieldVisitor.() -> Unit,
): FieldVisitor {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return visitField(access, name, descriptor, signature, value).apply(block)
diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/mapIntrinsics.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
similarity index 66%
rename from kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/mapIntrinsics.kt
rename to kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
index 588b9611a..0a0c21d8a 100644
--- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/internal/mapIntrinsics.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt
@@ -1,9 +1,9 @@
@file:JvmName("MapIntrinsics")
-package kscience.kmath.asm.internal
+package space.kscience.kmath.asm.internal
-import kscience.kmath.expressions.StringSymbol
-import kscience.kmath.expressions.Symbol
+import space.kscience.kmath.misc.StringSymbol
+import space.kscience.kmath.misc.Symbol
/**
* Gets value with given [key] or throws [NoSuchElementException] whenever it is not present.
diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/ast/parser.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt
similarity index 86%
rename from kmath-ast/src/jvmMain/kotlin/kscience/kmath/ast/parser.kt
rename to kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt
index 0b66e2c31..9a38ce81a 100644
--- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/ast/parser.kt
+++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/parser.kt
@@ -1,6 +1,6 @@
-// TODO move to common when https://github.com/h0tk3y/better-parse/pull/33 is merged
+// TODO move to common when https://github.com/h0tk3y/better-parse/pull/37 is merged
-package kscience.kmath.ast
+package space.kscience.kmath.ast
import com.github.h0tk3y.betterParse.combinators.*
import com.github.h0tk3y.betterParse.grammar.Grammar
@@ -13,10 +13,10 @@ import com.github.h0tk3y.betterParse.lexer.literalToken
import com.github.h0tk3y.betterParse.lexer.regexToken
import com.github.h0tk3y.betterParse.parser.ParseResult
import com.github.h0tk3y.betterParse.parser.Parser
-import kscience.kmath.operations.FieldOperations
-import kscience.kmath.operations.PowerOperations
-import kscience.kmath.operations.RingOperations
-import kscience.kmath.operations.SpaceOperations
+import space.kscience.kmath.operations.FieldOperations
+import space.kscience.kmath.operations.GroupOperations
+import space.kscience.kmath.operations.PowerOperations
+import space.kscience.kmath.operations.RingOperations
/**
* better-parse implementation of grammar defined in the ArithmeticsEvaluator.g4.
@@ -25,8 +25,8 @@ import kscience.kmath.operations.SpaceOperations
*/
public object ArithmeticsEvaluator : Grammar() {
// TODO replace with "...".toRegex() when better-parse 0.4.1 is released
- private val num: Token by regexToken("[\\d.]+(?:[eE][-+]?\\d+)?")
- private val id: Token by regexToken("[a-z_A-Z][\\da-z_A-Z]*")
+ private val num: Token by regexToken("[\\d.]+(?:[eE][-+]?\\d+)?".toRegex())
+ private val id: Token by regexToken("[a-z_A-Z][\\da-z_A-Z]*".toRegex())
private val lpar: Token by literalToken("(")
private val rpar: Token by literalToken(")")
private val comma: Token by literalToken(",")
@@ -35,7 +35,7 @@ public object ArithmeticsEvaluator : Grammar() {
private val div: Token by literalToken("/")
private val minus: Token by literalToken("-")
private val plus: Token by literalToken("+")
- private val ws: Token by regexToken("\\s+", ignore = true)
+ private val ws: Token by regexToken("\\s+".toRegex(), ignore = true)
private val number: Parser by num use { MST.Numeric(text.toDouble()) }
private val singular: Parser by id use { MST.Symbolic(text) }
@@ -55,7 +55,7 @@ public object ArithmeticsEvaluator : Grammar() {
.or(binaryFunction)
.or(unaryFunction)
.or(singular)
- .or(-minus and parser(ArithmeticsEvaluator::term) map { MST.Unary(SpaceOperations.MINUS_OPERATION, it) })
+ .or(-minus and parser(ArithmeticsEvaluator::term) map { MST.Unary(GroupOperations.MINUS_OPERATION, it) })
.or(-lpar and parser(ArithmeticsEvaluator::subSumChain) and -rpar)
private val powChain: Parser by leftAssociative(term = term, operator = pow) { a, _, b ->
@@ -77,9 +77,9 @@ public object ArithmeticsEvaluator : Grammar() {
operator = plus or minus use TokenMatch::type
) { a, op, b ->
if (op == plus)
- MST.Binary(SpaceOperations.PLUS_OPERATION, a, b)
+ MST.Binary(GroupOperations.PLUS_OPERATION, a, b)
else
- MST.Binary(SpaceOperations.MINUS_OPERATION, a, b)
+ MST.Binary(GroupOperations.MINUS_OPERATION, a, b)
}
override val rootParser: Parser by subSumChain
diff --git a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmOperationsSupport.kt b/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmOperationsSupport.kt
deleted file mode 100644
index 2ce52aa87..000000000
--- a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmOperationsSupport.kt
+++ /dev/null
@@ -1,41 +0,0 @@
-package kscience.kmath.asm
-
-import kscience.kmath.ast.mstInExtendedField
-import kscience.kmath.ast.mstInField
-import kscience.kmath.ast.mstInSpace
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.RealField
-import kotlin.random.Random
-import kotlin.test.Test
-import kotlin.test.assertEquals
-
-internal class TestAsmOperationsSupport {
- @Test
- fun testUnaryOperationInvocation() {
- val expression = RealField.mstInSpace { -symbol("x") }.compile()
- val res = expression("x" to 2.0)
- assertEquals(-2.0, res)
- }
-
- @Test
- fun testBinaryOperationInvocation() {
- val expression = RealField.mstInSpace { -symbol("x") + number(1.0) }.compile()
- val res = expression("x" to 2.0)
- assertEquals(-1.0, res)
- }
-
- @Test
- fun testConstProductInvocation() {
- val res = RealField.mstInField { symbol("x") * 2 }("x" to 2.0)
- assertEquals(4.0, res)
- }
-
- @Test
- fun testMultipleCalls() {
- val e = RealField.mstInExtendedField { sin(symbol("x")).pow(4) - 6 * symbol("x") / tanh(symbol("x")) }.compile()
- val r = Random(0)
- var s = 0.0
- repeat(1000000) { s += e("x" to r.nextDouble()) }
- println(s)
- }
-}
diff --git a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmSpecialization.kt b/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmSpecialization.kt
deleted file mode 100644
index 602c54651..000000000
--- a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmSpecialization.kt
+++ /dev/null
@@ -1,54 +0,0 @@
-package kscience.kmath.asm
-
-import kscience.kmath.ast.mstInField
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.RealField
-import kotlin.test.Test
-import kotlin.test.assertEquals
-
-internal class TestAsmSpecialization {
- @Test
- fun testUnaryPlus() {
- val expr = RealField.mstInField { unaryOperationFunction("+")(symbol("x")) }.compile()
- assertEquals(2.0, expr("x" to 2.0))
- }
-
- @Test
- fun testUnaryMinus() {
- val expr = RealField.mstInField { unaryOperationFunction("-")(symbol("x")) }.compile()
- assertEquals(-2.0, expr("x" to 2.0))
- }
-
- @Test
- fun testAdd() {
- val expr = RealField.mstInField { binaryOperationFunction("+")(symbol("x"), symbol("x")) }.compile()
- assertEquals(4.0, expr("x" to 2.0))
- }
-
- @Test
- fun testSine() {
- val expr = RealField.mstInField { unaryOperationFunction("sin")(symbol("x")) }.compile()
- assertEquals(0.0, expr("x" to 0.0))
- }
-
- @Test
- fun testMinus() {
- val expr = RealField.mstInField { binaryOperationFunction("-")(symbol("x"), symbol("x")) }.compile()
- assertEquals(0.0, expr("x" to 2.0))
- }
-
- @Test
- fun testDivide() {
- val expr = RealField.mstInField { binaryOperationFunction("/")(symbol("x"), symbol("x")) }.compile()
- assertEquals(1.0, expr("x" to 2.0))
- }
-
- @Test
- fun testPower() {
- val expr = RealField
- .mstInField { binaryOperationFunction("pow")(symbol("x"), number(2)) }
- .compile()
-
- assertEquals(4.0, expr("x" to 2.0))
- }
-}
diff --git a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt
similarity index 69%
rename from kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt
rename to kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt
index 4091aa8ed..abc320360 100644
--- a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt
+++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt
@@ -1,40 +1,41 @@
-package kscience.kmath.asm
+package space.kscience.kmath.asm
-import kscience.kmath.ast.*
-import kscience.kmath.complex.*
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.ByteRing
-import kscience.kmath.operations.RealField
+import space.kscience.kmath.ast.*
+import space.kscience.kmath.complex.ComplexField
+import space.kscience.kmath.complex.toComplex
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.ByteRing
+import space.kscience.kmath.operations.DoubleField
import kotlin.test.Test
import kotlin.test.assertEquals
internal class TestAsmConsistencyWithInterpreter {
@Test
fun mstSpace() {
- val res1 = MstSpace.mstInSpace {
+ val res1 = MstGroup.mstInGroup {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- number(3.toByte()) - (number(2.toByte()) + (multiply(
+ number(3.toByte()) - (number(2.toByte()) + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + number(1.toByte()) * 3.toByte() - number(1.toByte())))
),
number(1)
- ) + symbol("x") + zero
+ ) + bindSymbol("x") + zero
}("x" to MST.Numeric(2))
- val res2 = MstSpace.mstInSpace {
+ val res2 = MstGroup.mstInGroup {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- number(3.toByte()) - (number(2.toByte()) + (multiply(
+ number(3.toByte()) - (number(2.toByte()) + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + number(1.toByte()) * 3.toByte() - number(1.toByte())))
),
number(1)
- ) + symbol("x") + zero
+ ) + bindSymbol("x") + zero
}.compile()("x" to MST.Numeric(2))
assertEquals(res1, res2)
@@ -45,9 +46,9 @@ internal class TestAsmConsistencyWithInterpreter {
val res1 = ByteRing.mstInRing {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- (symbol("x") - (2.toByte() + (multiply(
+ (bindSymbol("x") - (2.toByte() + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + 1.toByte()))) * 3.0 - 1.toByte()
),
@@ -58,9 +59,9 @@ internal class TestAsmConsistencyWithInterpreter {
val res2 = ByteRing.mstInRing {
binaryOperationFunction("+")(
unaryOperationFunction("+")(
- (symbol("x") - (2.toByte() + (multiply(
+ (bindSymbol("x") - (2.toByte() + (scale(
add(number(1), number(1)),
- 2
+ 2.0
) + 1.toByte()))) * 3.0 - 1.toByte()
),
number(1)
@@ -72,17 +73,17 @@ internal class TestAsmConsistencyWithInterpreter {
@Test
fun realField() {
- val res1 = RealField.mstInField {
+ val res1 = DoubleField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
}("x" to 2.0)
- val res2 = RealField.mstInField {
+ val res2 = DoubleField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
@@ -95,7 +96,7 @@ internal class TestAsmConsistencyWithInterpreter {
fun complexField() {
val res1 = ComplexField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
@@ -103,7 +104,7 @@ internal class TestAsmConsistencyWithInterpreter {
val res2 = ComplexField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
- (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ (3.0 - (bindSymbol("x") + (scale(add(number(1.0), number(1.0)), 2.0) + 1.0))) * 3 - 1.0
+ number(1),
number(1) / 2 + number(2.0) * one
) + zero
diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt
new file mode 100644
index 000000000..5d70cb76b
--- /dev/null
+++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt
@@ -0,0 +1,43 @@
+package space.kscience.kmath.asm
+
+import space.kscience.kmath.ast.mstInExtendedField
+import space.kscience.kmath.ast.mstInField
+import space.kscience.kmath.ast.mstInGroup
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.DoubleField
+import kotlin.random.Random
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+internal class TestAsmOperationsSupport {
+ @Test
+ fun testUnaryOperationInvocation() {
+ val expression = DoubleField.mstInGroup { -bindSymbol("x") }.compile()
+ val res = expression("x" to 2.0)
+ assertEquals(-2.0, res)
+ }
+
+ @Test
+ fun testBinaryOperationInvocation() {
+ val expression = DoubleField.mstInGroup { -bindSymbol("x") + number(1.0) }.compile()
+ val res = expression("x" to 2.0)
+ assertEquals(-1.0, res)
+ }
+
+ @Test
+ fun testConstProductInvocation() {
+ val res = DoubleField.mstInField { bindSymbol("x") * 2 }("x" to 2.0)
+ assertEquals(4.0, res)
+ }
+
+ @Test
+ fun testMultipleCalls() {
+ val e =
+ DoubleField.mstInExtendedField { sin(bindSymbol("x")).pow(4) - 6 * bindSymbol("x") / tanh(bindSymbol("x")) }
+ .compile()
+ val r = Random(0)
+ var s = 0.0
+ repeat(1000000) { s += e("x" to r.nextDouble()) }
+ println(s)
+ }
+}
diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmSpecialization.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmSpecialization.kt
new file mode 100644
index 000000000..f485260c9
--- /dev/null
+++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmSpecialization.kt
@@ -0,0 +1,54 @@
+package space.kscience.kmath.asm
+
+import space.kscience.kmath.ast.mstInField
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.DoubleField
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+internal class TestAsmSpecialization {
+ @Test
+ fun testUnaryPlus() {
+ val expr = DoubleField.mstInField { unaryOperationFunction("+")(bindSymbol("x")) }.compile()
+ assertEquals(2.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testUnaryMinus() {
+ val expr = DoubleField.mstInField { unaryOperationFunction("-")(bindSymbol("x")) }.compile()
+ assertEquals(-2.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testAdd() {
+ val expr = DoubleField.mstInField { binaryOperationFunction("+")(bindSymbol("x"), bindSymbol("x")) }.compile()
+ assertEquals(4.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testSine() {
+ val expr = DoubleField.mstInField { unaryOperationFunction("sin")(bindSymbol("x")) }.compile()
+ assertEquals(0.0, expr("x" to 0.0))
+ }
+
+ @Test
+ fun testMinus() {
+ val expr = DoubleField.mstInField { binaryOperationFunction("-")(bindSymbol("x"), bindSymbol("x")) }.compile()
+ assertEquals(0.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testDivide() {
+ val expr = DoubleField.mstInField { binaryOperationFunction("/")(bindSymbol("x"), bindSymbol("x")) }.compile()
+ assertEquals(1.0, expr("x" to 2.0))
+ }
+
+ @Test
+ fun testPower() {
+ val expr = DoubleField
+ .mstInField { binaryOperationFunction("pow")(bindSymbol("x"), number(2)) }
+ .compile()
+
+ assertEquals(4.0, expr("x" to 2.0))
+ }
+}
diff --git a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmVariables.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmVariables.kt
similarity index 52%
rename from kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmVariables.kt
rename to kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmVariables.kt
index c91568dbf..d1aaefffe 100644
--- a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/asm/TestAsmVariables.kt
+++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmVariables.kt
@@ -1,8 +1,8 @@
-package kscience.kmath.asm
+package space.kscience.kmath.asm
-import kscience.kmath.ast.mstInRing
-import kscience.kmath.expressions.invoke
-import kscience.kmath.operations.ByteRing
+import space.kscience.kmath.ast.mstInRing
+import space.kscience.kmath.expressions.invoke
+import space.kscience.kmath.operations.ByteRing
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -10,13 +10,13 @@ import kotlin.test.assertFailsWith
internal class TestAsmVariables {
@Test
fun testVariable() {
- val expr = ByteRing.mstInRing { symbol("x") }.compile()
+ val expr = ByteRing.mstInRing { bindSymbol("x") }.compile()
assertEquals(1.toByte(), expr("x" to 1.toByte()))
}
@Test
fun testUndefinedVariableFails() {
- val expr = ByteRing.mstInRing { symbol("x") }.compile()
+ val expr = ByteRing.mstInRing { bindSymbol("x") }.compile()
assertFailsWith { expr() }
}
}
diff --git a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/ast/ParserPrecedenceTest.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/ParserPrecedenceTest.kt
similarity index 81%
rename from kmath-ast/src/jvmTest/kotlin/kscience/kmath/ast/ParserPrecedenceTest.kt
rename to kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/ParserPrecedenceTest.kt
index 561fe51bd..1450cab84 100644
--- a/kmath-ast/src/jvmTest/kotlin/kscience/kmath/ast/ParserPrecedenceTest.kt
+++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/ParserPrecedenceTest.kt
@@ -1,12 +1,12 @@
-package kscience.kmath.ast
+package space.kscience.kmath.ast
-import kscience.kmath.operations.Field
-import kscience.kmath.operations.RealField
+import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.operations.Field
import kotlin.test.Test
import kotlin.test.assertEquals
internal class ParserPrecedenceTest {
- private val f: Field