forked from kscience/kmath
Compare commits
2 Commits
dev
...
commandert
Author | SHA1 | Date | |
---|---|---|---|
|
4fdd534e5b | ||
|
57dabba0a3 |
32
.github/workflows/build.yml
vendored
32
.github/workflows/build.yml
vendored
@ -1,32 +0,0 @@
|
|||||||
name: Gradle build
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ dev, master ]
|
|
||||||
pull_request:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ macOS-latest, windows-latest ]
|
|
||||||
runs-on: ${{matrix.os}}
|
|
||||||
timeout-minutes: 40
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3.0.0
|
|
||||||
- uses: actions/setup-java@v3.0.0
|
|
||||||
with:
|
|
||||||
java-version: 11
|
|
||||||
distribution: liberica
|
|
||||||
- name: Cache konan
|
|
||||||
uses: actions/cache@v3.0.1
|
|
||||||
with:
|
|
||||||
path: ~/.konan
|
|
||||||
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-gradle-
|
|
||||||
- name: Gradle Wrapper Validation
|
|
||||||
uses: gradle/wrapper-validation-action@v1.0.4
|
|
||||||
- uses: gradle/gradle-build-action@v2.1.5
|
|
||||||
with:
|
|
||||||
arguments: build
|
|
30
.github/workflows/pages.yml
vendored
30
.github/workflows/pages.yml
vendored
@ -1,30 +0,0 @@
|
|||||||
name: Dokka publication
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-20.04
|
|
||||||
timeout-minutes: 40
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3.0.0
|
|
||||||
- uses: actions/setup-java@v3.0.0
|
|
||||||
with:
|
|
||||||
java-version: 11
|
|
||||||
distribution: liberica
|
|
||||||
- name: Cache konan
|
|
||||||
uses: actions/cache@v3.0.1
|
|
||||||
with:
|
|
||||||
path: ~/.konan
|
|
||||||
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-gradle-
|
|
||||||
- uses: gradle/gradle-build-action@v2.1.5
|
|
||||||
with:
|
|
||||||
arguments: dokkaHtmlMultiModule --no-parallel
|
|
||||||
- uses: JamesIves/github-pages-deploy-action@4.2.5
|
|
||||||
with:
|
|
||||||
branch: gh-pages
|
|
||||||
folder: build/dokka/htmlMultiModule
|
|
49
.github/workflows/publish.yml
vendored
49
.github/workflows/publish.yml
vendored
@ -1,49 +0,0 @@
|
|||||||
name: Gradle publish
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
release:
|
|
||||||
types: [ created ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish:
|
|
||||||
environment:
|
|
||||||
name: publish
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ macOS-latest, windows-latest ]
|
|
||||||
runs-on: ${{matrix.os}}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3.0.0
|
|
||||||
- uses: actions/setup-java@v3.0.0
|
|
||||||
with:
|
|
||||||
java-version: 11
|
|
||||||
distribution: liberica
|
|
||||||
- name: Cache konan
|
|
||||||
uses: actions/cache@v3.0.1
|
|
||||||
with:
|
|
||||||
path: ~/.konan
|
|
||||||
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-gradle-
|
|
||||||
- uses: gradle/wrapper-validation-action@v1.0.4
|
|
||||||
- name: Publish Windows Artifacts
|
|
||||||
if: matrix.os == 'windows-latest'
|
|
||||||
uses: gradle/gradle-build-action@v2.1.5
|
|
||||||
with:
|
|
||||||
arguments: |
|
|
||||||
releaseAll
|
|
||||||
-Ppublishing.enabled=true
|
|
||||||
-Ppublishing.space.user=${{ secrets.SPACE_APP_ID }}
|
|
||||||
-Ppublishing.space.token=${{ secrets.SPACE_APP_SECRET }}
|
|
||||||
- name: Publish Mac Artifacts
|
|
||||||
if: matrix.os == 'macOS-latest'
|
|
||||||
uses: gradle/gradle-build-action@v2.1.5
|
|
||||||
with:
|
|
||||||
arguments: |
|
|
||||||
releaseMacosX64
|
|
||||||
releaseIosArm64
|
|
||||||
releaseIosX64
|
|
||||||
-Ppublishing.enabled=true
|
|
||||||
-Ppublishing.space.user=${{ secrets.SPACE_APP_ID }}
|
|
||||||
-Ppublishing.space.token=${{ secrets.SPACE_APP_SECRET }}
|
|
@ -1,3 +0,0 @@
|
|||||||
job("Build") {
|
|
||||||
gradlew("openjdk:11", "build")
|
|
||||||
}
|
|
165
CHANGELOG.md
165
CHANGELOG.md
@ -1,165 +0,0 @@
|
|||||||
# KMath
|
|
||||||
|
|
||||||
## [Unreleased]
|
|
||||||
### Added
|
|
||||||
- `ScaleOperations` interface
|
|
||||||
- `Field` extends `ScaleOperations`
|
|
||||||
- Basic integration API
|
|
||||||
- Basic MPP distributions and samplers
|
|
||||||
- `bindSymbolOrNull`
|
|
||||||
- Blocking chains and Statistics
|
|
||||||
- Multiplatform integration
|
|
||||||
- Integration for any Field element
|
|
||||||
- Extended operations for ND4J fields
|
|
||||||
- Jupyter Notebook integration module (kmath-jupyter)
|
|
||||||
- `@PerformancePitfall` annotation to mark possibly slow API
|
|
||||||
- Unified architecture for Integration and Optimization using features.
|
|
||||||
- `BigInt` operation performance improvement and fixes by @zhelenskiy (#328)
|
|
||||||
- Integration between `MST` and Symja `IExpr`
|
|
||||||
- Complex power
|
|
||||||
- Separate methods for UInt, Int and Number powers. NaN safety.
|
|
||||||
- Tensorflow prototype
|
|
||||||
|
|
||||||
### 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
|
|
||||||
- Rename `NDStructure` and `NDAlgebra` to `StructureND` and `AlgebraND` respectively
|
|
||||||
- `Real` -> `Double`
|
|
||||||
- DataSets are moved from functions to core
|
|
||||||
- Redesign advanced Chain API
|
|
||||||
- Redesign `MST`. Remove `MstExpression`.
|
|
||||||
- Move `MST` to core
|
|
||||||
- Separated benchmarks and examples
|
|
||||||
- Rewrite `kmath-ejml` without `ejml-simple` artifact, support sparse matrices
|
|
||||||
- Promote stability of kmath-ast and kmath-kotlingrad to EXPERIMENTAL.
|
|
||||||
- ColumnarData returns nullable column
|
|
||||||
- `MST` is made sealed interface
|
|
||||||
- Replace `MST.Symbolic` by `Symbol`, `Symbol` now implements MST
|
|
||||||
- Remove Any restriction on polynomials
|
|
||||||
- Add `out` variance to type parameters of `StructureND` and its implementations where possible
|
|
||||||
- Rename `DifferentiableMstExpression` to `KotlingradExpression`
|
|
||||||
- `FeatureSet` now accepts only `Feature`. It is possible to override keys and use interfaces.
|
|
||||||
- Use `Symbol` factory function instead of `StringSymbol`
|
|
||||||
- New discoverability pattern: `<Type>.algebra.<nd/etc>`
|
|
||||||
- Adjusted commons-math API for linear solvers to match conventions.
|
|
||||||
- Buffer algebra does not require size anymore
|
|
||||||
- Operations -> Ops
|
|
||||||
- Default Buffer and ND algebras are now Ops and lack neutral elements (0, 1) as well as algebra-level shapes.
|
|
||||||
- Tensor algebra takes read-only structures as input and inherits AlgebraND
|
|
||||||
|
|
||||||
### Deprecated
|
|
||||||
- Specialized `DoubleBufferAlgebra`
|
|
||||||
|
|
||||||
### 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.
|
|
||||||
- MSTExpression
|
|
||||||
- Expression algebra builders
|
|
||||||
- Complex and Quaternion no longer are elements.
|
|
||||||
- Second generic from DifferentiableExpression
|
|
||||||
- Algebra elements are completely removed. Use algebra contexts instead.
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Ring inherits RingOperations, not GroupOperations
|
|
||||||
- Univariate histogram filling
|
|
||||||
|
|
||||||
### 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)
|
|
||||||
- Automatic README generation for features (#139)
|
|
||||||
- Native support for `memory`, `core` and `dimensions`
|
|
||||||
- `kmath-ejml` to supply EJML SimpleMatrix wrapper (https://github.com/mipt-npm/kmath/pull/136)
|
|
||||||
- A separate `Symbol` entity, which is used for global unbound symbol.
|
|
||||||
- A `Symbol` indexing scope.
|
|
||||||
- Basic optimization API for Commons-math.
|
|
||||||
- Chi squared optimization for array-like data in CM
|
|
||||||
- `Fitting` utility object in prob/stat
|
|
||||||
- ND4J support module submitting `NDStructure` and `NDAlgebra` over `INDArray`
|
|
||||||
- Coroutine-deterministic Monte-Carlo scope with a random number generator
|
|
||||||
- Some minor utilities to `kmath-for-real`
|
|
||||||
- Generic operation result parameter to `MatrixContext`
|
|
||||||
- New `MatrixFeature` interfaces for matrix decompositions
|
|
||||||
- Basic Quaternion vector support in `kmath-complex`.
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- 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.30
|
|
||||||
- `kmath-ast` doesn't depend on heavy `kotlin-reflect` library
|
|
||||||
- Full autodiff refactoring based on `Symbol`
|
|
||||||
- `kmath-prob` renamed to `kmath-stat`
|
|
||||||
- Grid generators moved to `kmath-for-real`
|
|
||||||
- Use `Point<Double>` instead of specialized type in `kmath-for-real`
|
|
||||||
- Optimized dot product for buffer matrices moved to `kmath-for-real`
|
|
||||||
- EjmlMatrix context is an object
|
|
||||||
- Matrix LUP `inverse` renamed to `inverseWithLup`
|
|
||||||
- `NumericAlgebra` moved outside of regular algebra chain (`Ring` no longer implements it).
|
|
||||||
- Features moved to NDStructure and became transparent.
|
|
||||||
- Capitalization of LUP in many names changed to Lup.
|
|
||||||
- Refactored `NDStructure` algebra to be more simple, preferring under-the-hood conversion to explicit NDStructure types
|
|
||||||
- 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
|
|
||||||
|
|
||||||
### Removed
|
|
||||||
- `kmath-koma` module because it doesn't support Kotlin 1.4.
|
|
||||||
- Support of `legacy` JS backend (we will support only IR)
|
|
||||||
- `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)
|
|
||||||
|
|
||||||
### Security
|
|
||||||
|
|
||||||
## [0.1.4]
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- Functional Expressions API
|
|
||||||
- Mathematical Syntax Tree, its interpreter and API
|
|
||||||
- String to MST parser (https://github.com/mipt-npm/kmath/pull/120)
|
|
||||||
- MST to JVM bytecode translator (https://github.com/mipt-npm/kmath/pull/94)
|
|
||||||
- FloatBuffer (specialized MutableBuffer over FloatArray)
|
|
||||||
- FlaggedBuffer to associate primitive numbers buffer with flags (to mark values infinite or missing, etc.)
|
|
||||||
- Specialized builder functions for all primitive buffers like `IntBuffer(25) { it + 1 }` (https://github.com/mipt-npm/kmath/pull/125)
|
|
||||||
- Interface `NumericAlgebra` where `number` operation is available to convert numbers to algebraic elements
|
|
||||||
- Inverse trigonometric functions support in ExtendedField (`asin`, `acos`, `atan`) (https://github.com/mipt-npm/kmath/pull/114)
|
|
||||||
- New space extensions: `average` and `averageWith`
|
|
||||||
- Local coding conventions
|
|
||||||
- Geometric Domains API in `kmath-core`
|
|
||||||
- Blocking chains in `kmath-coroutines`
|
|
||||||
- Full hyperbolic functions support and default implementations within `ExtendedField`
|
|
||||||
- Norm support for `Complex`
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- `readAsMemory` now has `throws IOException` in JVM signature.
|
|
||||||
- Several functions taking functional types were made `inline`.
|
|
||||||
- Several functions taking functional types now have `callsInPlace` contracts.
|
|
||||||
- BigInteger and BigDecimal algebra: JBigDecimalField has companion object with default math context; minor optimizations
|
|
||||||
- `power(T, Int)` extension function has preconditions and supports `Field<T>`
|
|
||||||
- Memory objects have more preconditions (overflow checking)
|
|
||||||
- `tg` function is renamed to `tan` (https://github.com/mipt-npm/kmath/pull/114)
|
|
||||||
- Gradle version: 6.3 -> 6.6
|
|
||||||
- Moved probability distributions to commons-rng and to `kmath-prob`
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Missing copy method in Memory implementation on JS (https://github.com/mipt-npm/kmath/pull/106)
|
|
||||||
- D3.dim value in `kmath-dimensions`
|
|
||||||
- Multiplication in integer rings in `kmath-core` (https://github.com/mipt-npm/kmath/pull/101)
|
|
||||||
- Commons RNG compatibility (https://github.com/mipt-npm/kmath/issues/93)
|
|
||||||
- Multiplication of BigInt by scalar
|
|
297
README.md
297
README.md
@ -1,297 +0,0 @@
|
|||||||
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
|
|
||||||
[![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382)
|
|
||||||
![Gradle build](https://github.com/mipt-npm/kmath/workflows/Gradle%20build/badge.svg)
|
|
||||||
[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22)
|
|
||||||
[![Space](https://img.shields.io/badge/dynamic/xml?color=orange&label=Space&query=//metadata/versioning/latest&url=https%3A%2F%2Fmaven.pkg.jetbrains.space%2Fmipt-npm%2Fp%2Fsci%2Fmaven%2Fspace%2Fkscience%2Fkmath-core%2Fmaven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/)
|
|
||||||
|
|
||||||
# KMath
|
|
||||||
|
|
||||||
Could be pronounced as `key-math`. The **K**otlin **Math**ematics library was initially intended as a Kotlin-based
|
|
||||||
analog to Python's NumPy library. Later we found that kotlin is much more flexible language and allows superior
|
|
||||||
architecture designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like
|
|
||||||
experience could be achieved with [kmath-for-real](/kmath-for-real) extension module.
|
|
||||||
|
|
||||||
[Documentation site (**WIP**)](https://mipt-npm.github.io/kmath/)
|
|
||||||
|
|
||||||
## Publications and talks
|
|
||||||
|
|
||||||
* [A conceptual article about context-oriented design](https://proandroiddev.com/an-introduction-context-oriented-programming-in-kotlin-2e79d316b0a2)
|
|
||||||
* [Another article about context-oriented design](https://proandroiddev.com/diving-deeper-into-context-oriented-programming-in-kotlin-3ecb4ec38814)
|
|
||||||
* [ACAT 2019 conference paper](https://aip.scitation.org/doi/abs/10.1063/1.5130103)
|
|
||||||
|
|
||||||
# Goal
|
|
||||||
|
|
||||||
* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM, JS and Native)
|
|
||||||
.
|
|
||||||
* Provide basic multiplatform implementations for those abstractions (without significant performance optimization).
|
|
||||||
* Provide bindings and wrappers with those abstractions for popular optimized platform libraries.
|
|
||||||
|
|
||||||
## Non-goals
|
|
||||||
|
|
||||||
* Be like NumPy. It was the idea at the beginning, but we decided that we can do better in API.
|
|
||||||
* Provide the best performance out of the box. We have specialized libraries for that. Need only API wrappers for them.
|
|
||||||
* Cover all cases as immediately and in one bundle. We will modularize everything and add new features gradually.
|
|
||||||
* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like
|
|
||||||
for `Double` in the core. For that we will have specialization modules like `kmath-for-real`, which will give better
|
|
||||||
experience for those, who want to work with specific types.
|
|
||||||
|
|
||||||
## Features and stability
|
|
||||||
|
|
||||||
KMath is a modular library. Different modules provide different features with different API stability guarantees. All
|
|
||||||
core modules are released with the same version, but with different API change policy. The features are described in
|
|
||||||
module definitions below. The module stability could have the following levels:
|
|
||||||
|
|
||||||
* **PROTOTYPE**. On this level there are no compatibility guarantees. All methods and classes form those modules could
|
|
||||||
break any moment. You can still use it, but be sure to fix the specific version.
|
|
||||||
* **EXPERIMENTAL**. The general API is decided, but some changes could be made. Volatile API is marked
|
|
||||||
with `@UnstableKmathAPI` or other stability warning annotations.
|
|
||||||
* **DEVELOPMENT**. API breaking generally follows semantic versioning ideology. There could be changes in minor
|
|
||||||
versions, but not in patch versions. API is protected
|
|
||||||
with [binary-compatibility-validator](https://github.com/Kotlin/binary-compatibility-validator) tool.
|
|
||||||
* **STABLE**. The API stabilized. Breaking changes are allowed only in major releases.
|
|
||||||
|
|
||||||
## Modules
|
|
||||||
|
|
||||||
|
|
||||||
### [benchmarks](benchmarks)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
|
|
||||||
### [examples](examples)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
|
|
||||||
### [kmath-ast](kmath-ast)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [expression-language](kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt) : Expression language and its parser
|
|
||||||
> - [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
|
|
||||||
> - [rendering](kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt) : Extendable MST rendering
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-commons](kmath-commons)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
|
|
||||||
### [kmath-complex](kmath-complex)
|
|
||||||
> Complex numbers and quaternions.
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [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
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-core](kmath-core)
|
|
||||||
> Core classes, algebra definitions, basic linear algebra
|
|
||||||
>
|
|
||||||
> **Maturity**: DEVELOPMENT
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [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/space/kscience/kmath/domains) : Domains
|
|
||||||
> - [autodiff](kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt) : Automatic differentiation
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-coroutines](kmath-coroutines)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
|
|
||||||
### [kmath-dimensions](kmath-dimensions)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-ejml](kmath-ejml)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [ejml-vector](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt) : Point implementations.
|
|
||||||
> - [ejml-matrix](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt) : Matrix implementation.
|
|
||||||
> - [ejml-linear-space](kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt) : LinearSpace implementations.
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-for-real](kmath-for-real)
|
|
||||||
> Extension module that should be used to achieve numpy-like behavior.
|
|
||||||
All operations are specialized to work with `Double` numbers without declaring algebraic contexts.
|
|
||||||
One can still use generic algebras though.
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [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
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-functions](kmath-functions)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [piecewise](kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt) : Piecewise functions.
|
|
||||||
> - [polynomials](kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt) : Polynomial functions.
|
|
||||||
> - [linear interpolation](kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/LinearInterpolator.kt) : Linear XY interpolator.
|
|
||||||
> - [spline interpolation](kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt) : Cubic spline XY interpolator.
|
|
||||||
> - [integration](kmath-functions/#) : Univariate and multivariate quadratures
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-geometry](kmath-geometry)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-histograms](kmath-histograms)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-jafama](kmath-jafama)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [jafama-double](kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/) : Double ExtendedField implementations based on Jafama
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-jupyter](kmath-jupyter)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-kotlingrad](kmath-kotlingrad)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [differentiable-mst-expression](kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KotlingradExpression.kt) : MST based DifferentiableExpression.
|
|
||||||
> - [scalars-adapters](kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/scalarsAdapters.kt) : Conversions between Kotlin∇'s SFun and MST
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-memory](kmath-memory)
|
|
||||||
> An API and basic implementation for arranging objects in a continuous memory block.
|
|
||||||
>
|
|
||||||
> **Maturity**: DEVELOPMENT
|
|
||||||
|
|
||||||
### [kmath-multik](kmath-multik)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-nd4j](kmath-nd4j)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [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
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-optimization](kmath-optimization)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
|
|
||||||
### [kmath-stat](kmath-stat)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: EXPERIMENTAL
|
|
||||||
|
|
||||||
### [kmath-symja](kmath-symja)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-tensorflow](kmath-tensorflow)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
|
|
||||||
### [kmath-tensors](kmath-tensors)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: PROTOTYPE
|
|
||||||
>
|
|
||||||
> **Features:**
|
|
||||||
> - [tensor algebra](kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt) : Basic linear algebra operations on tensors (plus, dot, etc.)
|
|
||||||
> - [tensor algebra with broadcasting](kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt) : Basic linear algebra operations implemented with broadcasting.
|
|
||||||
> - [linear algebra operations](kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/LinearOpsTensorAlgebra.kt) : Advanced linear algebra operations like LU decomposition, SVD, etc.
|
|
||||||
|
|
||||||
|
|
||||||
### [kmath-viktor](kmath-viktor)
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> **Maturity**: DEVELOPMENT
|
|
||||||
|
|
||||||
|
|
||||||
## Multi-platform support
|
|
||||||
|
|
||||||
KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the
|
|
||||||
[common source sets](/kmath-core/src/commonMain) and implemented there wherever it is possible. In some cases, features
|
|
||||||
are delegated to platform-specific implementations even if they could be provided in the common module for performance
|
|
||||||
reasons. Currently, the Kotlin/JVM is the primary platform, however Kotlin/Native and Kotlin/JS contributions and
|
|
||||||
feedback are also welcome.
|
|
||||||
|
|
||||||
## Performance
|
|
||||||
|
|
||||||
Calculation performance is one of major goals of KMath in the future, but in some cases it is impossible to achieve both
|
|
||||||
performance and flexibility.
|
|
||||||
|
|
||||||
We expect to focus on creating convenient universal API first and then work on increasing performance for specific
|
|
||||||
cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized
|
|
||||||
native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be
|
|
||||||
better than SciPy.
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend to use GraalVM-CE 11 for
|
|
||||||
execution to get better performance.
|
|
||||||
|
|
||||||
### Repositories
|
|
||||||
|
|
||||||
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://repo.kotlin.link")
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
api("space.kscience:kmath-core:$version")
|
|
||||||
// api("space.kscience:kmath-core-jvm:$version") for jvm-specific version
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Gradle `6.0+` is required for multiplatform artifacts.
|
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
The project requires a lot of additional work. The most important thing we need is a feedback about what features are
|
|
||||||
required the most. Feel free to create feature requests. We are also welcome to code contributions, especially in issues
|
|
||||||
marked with
|
|
||||||
[waiting for a hero](https://github.com/mipt-npm/kmath/labels/waiting%20for%20a%20hero) label.
|
|
@ -1,4 +0,0 @@
|
|||||||
# Module benchmarks
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,169 +0,0 @@
|
|||||||
@file:Suppress("UNUSED_VARIABLE")
|
|
||||||
|
|
||||||
import space.kscience.kmath.benchmarks.addBenchmarkProperties
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
kotlin("multiplatform")
|
|
||||||
kotlin("plugin.allopen")
|
|
||||||
id("org.jetbrains.kotlinx.benchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
allOpen.annotation("org.openjdk.jmh.annotations.State")
|
|
||||||
sourceSets.register("benchmarks")
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
kotlin {
|
|
||||||
jvm()
|
|
||||||
|
|
||||||
js(IR) {
|
|
||||||
nodejs()
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceSets {
|
|
||||||
all {
|
|
||||||
languageSettings {
|
|
||||||
progressiveMode = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val commonMain by getting {
|
|
||||||
dependencies {
|
|
||||||
implementation(project(":kmath-ast"))
|
|
||||||
implementation(project(":kmath-core"))
|
|
||||||
implementation(project(":kmath-coroutines"))
|
|
||||||
implementation(project(":kmath-complex"))
|
|
||||||
implementation(project(":kmath-stat"))
|
|
||||||
implementation(project(":kmath-dimensions"))
|
|
||||||
implementation(project(":kmath-for-real"))
|
|
||||||
implementation(project(":kmath-tensors"))
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.2")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val jvmMain by getting {
|
|
||||||
dependencies {
|
|
||||||
implementation(project(":kmath-commons"))
|
|
||||||
implementation(project(":kmath-ejml"))
|
|
||||||
implementation(project(":kmath-nd4j"))
|
|
||||||
implementation(project(":kmath-kotlingrad"))
|
|
||||||
implementation(project(":kmath-viktor"))
|
|
||||||
implementation(project(":kmath-jafama"))
|
|
||||||
implementation(project(":kmath-multik"))
|
|
||||||
implementation(projects.kmath.kmathTensorflow)
|
|
||||||
implementation("org.tensorflow:tensorflow-core-platform:0.4.0")
|
|
||||||
implementation("org.nd4j:nd4j-native:1.0.0-M1")
|
|
||||||
// uncomment if your system supports AVX2
|
|
||||||
// val os = System.getProperty("os.name")
|
|
||||||
//
|
|
||||||
// if (System.getProperty("os.arch") in arrayOf("x86_64", "amd64")) when {
|
|
||||||
// os.startsWith("Windows") -> implementation("org.nd4j:nd4j-native:1.0.0-beta7:windows-x86_64-avx2")
|
|
||||||
// os == "Linux" -> implementation("org.nd4j:nd4j-native:1.0.0-beta7:linux-x86_64-avx2")
|
|
||||||
// os == "Mac OS X" -> implementation("org.nd4j:nd4j-native:1.0.0-beta7:macosx-x86_64-avx2")
|
|
||||||
// } else
|
|
||||||
// implementation("org.nd4j:nd4j-native-platform:1.0.0-beta7")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure benchmark
|
|
||||||
benchmark {
|
|
||||||
// Setup configurations
|
|
||||||
targets {
|
|
||||||
register("jvm")
|
|
||||||
register("js")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun kotlinx.benchmark.gradle.BenchmarkConfiguration.commonConfiguration() {
|
|
||||||
warmups = 2
|
|
||||||
iterations = 5
|
|
||||||
iterationTime = 2000
|
|
||||||
iterationTimeUnit = "ms"
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("buffer") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("BufferBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("nd") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("NDFieldBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("dot") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("DotBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("expressions") {
|
|
||||||
// Some extra precision
|
|
||||||
warmups = 2
|
|
||||||
iterations = 10
|
|
||||||
iterationTime = 10
|
|
||||||
iterationTimeUnit = "s"
|
|
||||||
outputTimeUnit = "s"
|
|
||||||
include("ExpressionsInterpretersBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("matrixInverse") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("MatrixInverseBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("bigInt") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("BigIntBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("jafamaDouble") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("JafamaBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("tensorAlgebra") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("TensorAlgebraBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("viktor") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("ViktorBenchmark")
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.register("viktorLog") {
|
|
||||||
commonConfiguration()
|
|
||||||
include("ViktorLogBenchmark")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix kotlinx-benchmarks bug
|
|
||||||
afterEvaluate {
|
|
||||||
val jvmBenchmarkJar by tasks.getting(org.gradle.jvm.tasks.Jar::class) {
|
|
||||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
kotlin.sourceSets.all {
|
|
||||||
with(languageSettings) {
|
|
||||||
optIn("kotlin.contracts.ExperimentalContracts")
|
|
||||||
optIn("kotlin.ExperimentalUnsignedTypes")
|
|
||||||
optIn("space.kscience.kmath.misc.UnstableKMathAPI")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile> {
|
|
||||||
kotlinOptions {
|
|
||||||
jvmTarget = "11"
|
|
||||||
freeCompilerArgs = freeCompilerArgs + "-Xjvm-default=all" + "-Xlambdas=indy"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
readme {
|
|
||||||
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
|
|
||||||
}
|
|
||||||
|
|
||||||
addBenchmarkProperties()
|
|
@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.expressions.*
|
|
||||||
import space.kscience.kmath.operations.Algebra
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.bindSymbol
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.math.sin
|
|
||||||
import kotlin.random.Random
|
|
||||||
import space.kscience.kmath.estree.compileToExpression as estreeCompileToExpression
|
|
||||||
import space.kscience.kmath.wasm.compileToExpression as wasmCompileToExpression
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
class ExpressionsInterpretersBenchmark {
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [expressionInExtendedField].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun functionalExpression(blackhole: Blackhole) = invokeAndSum(functional, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [toExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun mstExpression(blackhole: Blackhole) = invokeAndSum(mst, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [compileToExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun wasmExpression(blackhole: Blackhole) = invokeAndSum(wasm, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [compileToExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun estreeExpression(blackhole: Blackhole) = invokeAndSum(estree, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] implemented manually with `kotlin.math` functions.
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun rawExpression(blackhole: Blackhole) = invokeAndSum(raw, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for direct computation w/o [Expression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun justCalculate(blackhole: Blackhole) {
|
|
||||||
val random = Random(0)
|
|
||||||
var sum = 0.0
|
|
||||||
|
|
||||||
repeat(times) {
|
|
||||||
val x = random.nextDouble()
|
|
||||||
sum += x * 2.0 + 2.0 / x - 16.0 / sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
blackhole.consume(sum)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun invokeAndSum(expr: Expression<Double>, blackhole: Blackhole) {
|
|
||||||
val random = Random(0)
|
|
||||||
var sum = 0.0
|
|
||||||
val m = HashMap<Symbol, Double>()
|
|
||||||
|
|
||||||
repeat(times) {
|
|
||||||
m[x] = random.nextDouble()
|
|
||||||
sum += expr(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
blackhole.consume(sum)
|
|
||||||
}
|
|
||||||
|
|
||||||
private companion object {
|
|
||||||
private val x by symbol
|
|
||||||
private const val times = 1_000_000
|
|
||||||
|
|
||||||
private val functional = DoubleField.expression {
|
|
||||||
val x = bindSymbol(Symbol.x)
|
|
||||||
x * number(2.0) + 2.0 / x - 16.0 / sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val node = MstExtendedField {
|
|
||||||
x * 2.0 + number(2.0) / x - number(16.0) / sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val mst = node.toExpression(DoubleField)
|
|
||||||
private val wasm = node.wasmCompileToExpression(DoubleField)
|
|
||||||
private val estree = node.estreeCompileToExpression(DoubleField)
|
|
||||||
|
|
||||||
private val raw = Expression<Double> { args ->
|
|
||||||
val x = args[x]!!
|
|
||||||
x * 2.0 + 2.0 / x - 16.0 / sin(x)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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) }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,112 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.benchmarks
|
|
||||||
|
|
||||||
|
|
||||||
import kotlinx.benchmark.Blackhole
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark
|
|
||||||
import org.openjdk.jmh.annotations.Scope
|
|
||||||
import org.openjdk.jmh.annotations.State
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.operations.BigIntField
|
|
||||||
import space.kscience.kmath.operations.JBigIntegerField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import space.kscience.kmath.operations.parseBigInteger
|
|
||||||
import java.math.BigInteger
|
|
||||||
|
|
||||||
|
|
||||||
@UnstableKMathAPI
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class BigIntBenchmark {
|
|
||||||
|
|
||||||
val kmSmallNumber = BigIntField.number(100)
|
|
||||||
val jvmSmallNumber = JBigIntegerField.number(100)
|
|
||||||
val kmNumber = BigIntField.number(Int.MAX_VALUE)
|
|
||||||
val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE)
|
|
||||||
val kmLargeNumber = BigIntField { number(11).pow(100_000U) }
|
|
||||||
val jvmLargeNumber: BigInteger = JBigIntegerField { number(11).pow(100_000) }
|
|
||||||
val bigExponent = 50_000
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmSmallAdd(blackhole: Blackhole) = BigIntField {
|
|
||||||
blackhole.consume(kmSmallNumber + kmSmallNumber + kmSmallNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmSmallAdd(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume(jvmSmallNumber + jvmSmallNumber + jvmSmallNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmAdd(blackhole: Blackhole) = BigIntField {
|
|
||||||
blackhole.consume(kmNumber + kmNumber + kmNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmAdd(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume(jvmNumber + jvmNumber + jvmNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmAddLarge(blackhole: Blackhole) = BigIntField {
|
|
||||||
blackhole.consume(kmLargeNumber + kmLargeNumber + kmLargeNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmAddLarge(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume(jvmLargeNumber + jvmLargeNumber + jvmLargeNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmMultiply(blackhole: Blackhole) = BigIntField {
|
|
||||||
blackhole.consume(kmNumber * kmNumber * kmNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmMultiplyLarge(blackhole: Blackhole) = BigIntField {
|
|
||||||
blackhole.consume(kmLargeNumber*kmLargeNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmMultiply(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume(jvmNumber * jvmNumber * jvmNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmMultiplyLarge(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume(jvmLargeNumber*jvmLargeNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmPower(blackhole: Blackhole) = BigIntField {
|
|
||||||
blackhole.consume(kmNumber.pow(bigExponent.toUInt()))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmPower(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume(jvmNumber.pow(bigExponent))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmParsing16(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume("0x7f57ed8b89c29a3b9a85c7a5b84ca3929c7b7488593".parseBigInteger())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmParsing10(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume("236656783929183747565738292847574838922010".parseBigInteger())
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmParsing10(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume("236656783929183747565738292847574838922010".toBigInteger(10))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun jvmParsing16(blackhole: Blackhole) = JBigIntegerField {
|
|
||||||
blackhole.consume("7f57ed8b89c29a3b9a85c7a5b84ca3929c7b7488593".toBigInteger(16))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,100 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.EjmlLinearSpaceDDRM
|
|
||||||
import space.kscience.kmath.linear.invoke
|
|
||||||
import space.kscience.kmath.linear.linearSpace
|
|
||||||
import space.kscience.kmath.multik.multikAlgebra
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import space.kscience.kmath.structures.Buffer
|
|
||||||
import space.kscience.kmath.tensorflow.produceWithTF
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensorAlgebra
|
|
||||||
import space.kscience.kmath.tensors.core.tensorAlgebra
|
|
||||||
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 = DoubleField.linearSpace.buildMatrix(dim, dim) { _, _ ->
|
|
||||||
random.nextDouble()
|
|
||||||
}
|
|
||||||
val matrix2 = DoubleField.linearSpace.buildMatrix(dim, dim) { _, _ ->
|
|
||||||
random.nextDouble()
|
|
||||||
}
|
|
||||||
|
|
||||||
val cmMatrix1 = CMLinearSpace { matrix1.toCM() }
|
|
||||||
val cmMatrix2 = CMLinearSpace { matrix2.toCM() }
|
|
||||||
|
|
||||||
val ejmlMatrix1 = EjmlLinearSpaceDDRM { matrix1.toEjml() }
|
|
||||||
val ejmlMatrix2 = EjmlLinearSpaceDDRM { matrix2.toEjml() }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun tfDot(blackhole: Blackhole) {
|
|
||||||
blackhole.consume(
|
|
||||||
DoubleField.produceWithTF {
|
|
||||||
matrix1 dot matrix1
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun cmDotWithConversion(blackhole: Blackhole) = CMLinearSpace {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun cmDot(blackhole: Blackhole) = CMLinearSpace {
|
|
||||||
blackhole.consume(cmMatrix1 dot cmMatrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun ejmlDot(blackhole: Blackhole) = EjmlLinearSpaceDDRM {
|
|
||||||
blackhole.consume(ejmlMatrix1 dot ejmlMatrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun ejmlDotWithConversion(blackhole: Blackhole) = EjmlLinearSpaceDDRM {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun tensorDot(blackhole: Blackhole) = with(DoubleField.tensorAlgebra) {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun multikDot(blackhole: Blackhole) = with(DoubleField.multikAlgebra) {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun bufferedDot(blackhole: Blackhole) = with(DoubleField.linearSpace(Buffer.Companion::auto)) {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun doubleDot(blackhole: Blackhole) = with(DoubleField.linearSpace) {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun doubleTensorDot(blackhole: Blackhole) = DoubleTensorAlgebra.invoke {
|
|
||||||
blackhole.consume(matrix1 dot matrix2)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,124 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.compileToExpression
|
|
||||||
import space.kscience.kmath.expressions.*
|
|
||||||
import space.kscience.kmath.operations.Algebra
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.bindSymbol
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.math.sin
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class ExpressionsInterpretersBenchmark {
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [expressionInExtendedField].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun functionalExpression(blackhole: Blackhole) = invokeAndSum(functional, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [toExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun mstExpression(blackhole: Blackhole) = invokeAndSum(mst, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [compileToExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun asmGenericExpression(blackhole: Blackhole) = invokeAndSum(asmGeneric, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [compileToExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun asmPrimitiveExpressionArray(blackhole: Blackhole) {
|
|
||||||
val random = Random(0)
|
|
||||||
var sum = 0.0
|
|
||||||
val m = DoubleArray(1)
|
|
||||||
|
|
||||||
repeat(times) {
|
|
||||||
m[xIdx] = random.nextDouble()
|
|
||||||
sum += asmPrimitive(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
blackhole.consume(sum)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] created with [compileToExpression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun asmPrimitiveExpression(blackhole: Blackhole) = invokeAndSum(asmPrimitive, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for [Expression] implemented manually with `kotlin.math` functions.
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun rawExpression(blackhole: Blackhole) = invokeAndSum(raw, blackhole)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Benchmark case for direct computation w/o [Expression].
|
|
||||||
*/
|
|
||||||
@Benchmark
|
|
||||||
fun justCalculate(blackhole: Blackhole) {
|
|
||||||
val random = Random(0)
|
|
||||||
var sum = 0.0
|
|
||||||
|
|
||||||
repeat(times) {
|
|
||||||
val x = random.nextDouble()
|
|
||||||
sum += x * 2.0 + 2.0 / x - 16.0 / sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
blackhole.consume(sum)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun invokeAndSum(expr: Expression<Double>, blackhole: Blackhole) {
|
|
||||||
val random = Random(0)
|
|
||||||
var sum = 0.0
|
|
||||||
val m = HashMap<Symbol, Double>()
|
|
||||||
|
|
||||||
repeat(times) {
|
|
||||||
m[x] = random.nextDouble()
|
|
||||||
sum += expr(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
blackhole.consume(sum)
|
|
||||||
}
|
|
||||||
|
|
||||||
private companion object {
|
|
||||||
private val x by symbol
|
|
||||||
private const val times = 1_000_000
|
|
||||||
|
|
||||||
private val functional = DoubleField.expression {
|
|
||||||
val x = bindSymbol(Symbol.x)
|
|
||||||
x * number(2.0) + 2.0 / x - 16.0 / sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val node = MstExtendedField {
|
|
||||||
x * 2.0 + number(2.0) / x - number(16.0) / sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val mst = node.toExpression(DoubleField)
|
|
||||||
|
|
||||||
private val asmPrimitive = node.compileToExpression(DoubleField)
|
|
||||||
private val xIdx = asmPrimitive.indexer.indexOf(x)
|
|
||||||
|
|
||||||
private val asmGeneric = node.compileToExpression(DoubleField as Algebra<Double>)
|
|
||||||
|
|
||||||
private val raw = Expression<Double> { args ->
|
|
||||||
val x = args[x]!!
|
|
||||||
x * 2.0 + 2.0 / x - 16.0 / sin(x)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.benchmarks
|
|
||||||
|
|
||||||
import kotlinx.benchmark.Blackhole
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark
|
|
||||||
import org.openjdk.jmh.annotations.Scope
|
|
||||||
import org.openjdk.jmh.annotations.State
|
|
||||||
import space.kscience.kmath.jafama.JafamaDoubleField
|
|
||||||
import space.kscience.kmath.jafama.StrictJafamaDoubleField
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.contracts.InvocationKind
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class JafamaBenchmark {
|
|
||||||
@Benchmark
|
|
||||||
fun jafama(blackhole: Blackhole) = invokeBenchmarks(blackhole) { x ->
|
|
||||||
JafamaDoubleField { x * power(x, 4) * exp(x) / cos(x) + sin(x) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun core(blackhole: Blackhole) = invokeBenchmarks(blackhole) { x ->
|
|
||||||
DoubleField { x * power(x, 4) * exp(x) / cos(x) + sin(x) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun strictJafama(blackhole: Blackhole) = invokeBenchmarks(blackhole) { x ->
|
|
||||||
StrictJafamaDoubleField { x * power(x, 4) * exp(x) / cos(x) + sin(x) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private inline fun invokeBenchmarks(blackhole: Blackhole, expr: (Double) -> Double) {
|
|
||||||
contract { callsInPlace(expr, InvocationKind.AT_LEAST_ONCE) }
|
|
||||||
val rng = Random(0)
|
|
||||||
repeat(1000000) { blackhole.consume(expr(rng.nextDouble())) }
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.lupSolver
|
|
||||||
import space.kscience.kmath.ejml.EjmlLinearSpaceDDRM
|
|
||||||
import space.kscience.kmath.linear.invoke
|
|
||||||
import space.kscience.kmath.linear.linearSpace
|
|
||||||
import space.kscience.kmath.linear.lupSolver
|
|
||||||
import space.kscience.kmath.operations.algebra
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class MatrixInverseBenchmark {
|
|
||||||
private companion object {
|
|
||||||
private val random = Random(1224)
|
|
||||||
private const val dim = 100
|
|
||||||
|
|
||||||
private val space = Double.algebra.linearSpace
|
|
||||||
|
|
||||||
//creating invertible matrix
|
|
||||||
private val u = space.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 }
|
|
||||||
private val l = space.buildMatrix(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 }
|
|
||||||
private val matrix = space { l dot u }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun kmathLupInversion(blackhole: Blackhole) {
|
|
||||||
blackhole.consume(Double.algebra.linearSpace.lupSolver().inverse(matrix))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun cmLUPInversion(blackhole: Blackhole) {
|
|
||||||
CMLinearSpace {
|
|
||||||
blackhole.consume(lupSolver().inverse(matrix))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun ejmlInverse(blackhole: Blackhole) {
|
|
||||||
EjmlLinearSpaceDDRM {
|
|
||||||
blackhole.consume(matrix.toEjml().inverse())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.benchmarks
|
|
||||||
|
|
||||||
import kotlinx.benchmark.Benchmark
|
|
||||||
import kotlinx.benchmark.Blackhole
|
|
||||||
import kotlinx.benchmark.Scope
|
|
||||||
import kotlinx.benchmark.State
|
|
||||||
import org.jetbrains.kotlinx.multik.api.Multik
|
|
||||||
import org.jetbrains.kotlinx.multik.api.ones
|
|
||||||
import org.jetbrains.kotlinx.multik.ndarray.data.DN
|
|
||||||
import org.jetbrains.kotlinx.multik.ndarray.data.DataType
|
|
||||||
import space.kscience.kmath.multik.multikAlgebra
|
|
||||||
import space.kscience.kmath.nd.BufferedFieldOpsND
|
|
||||||
import space.kscience.kmath.nd.StructureND
|
|
||||||
import space.kscience.kmath.nd.ndAlgebra
|
|
||||||
import space.kscience.kmath.nd.one
|
|
||||||
import space.kscience.kmath.nd4j.nd4j
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.structures.Buffer
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensor
|
|
||||||
import space.kscience.kmath.tensors.core.one
|
|
||||||
import space.kscience.kmath.tensors.core.tensorAlgebra
|
|
||||||
import space.kscience.kmath.viktor.viktorAlgebra
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class NDFieldBenchmark {
|
|
||||||
@Benchmark
|
|
||||||
fun autoFieldAdd(blackhole: Blackhole) = with(autoField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun specializedFieldAdd(blackhole: Blackhole) = with(specializedField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun boxingFieldAdd(blackhole: Blackhole) = with(genericField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun multikAdd(blackhole: Blackhole) = with(multikField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun viktorAdd(blackhole: Blackhole) = with(viktorField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun tensorAdd(blackhole: Blackhole) = with(Double.tensorAlgebra) {
|
|
||||||
var res: DoubleTensor = one(shape)
|
|
||||||
repeat(n) { res = res + 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun tensorInPlaceAdd(blackhole: Blackhole) = with(Double.tensorAlgebra) {
|
|
||||||
val res: DoubleTensor = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun multikInPlaceAdd(blackhole: Blackhole) = with(DoubleField.multikAlgebra) {
|
|
||||||
val res = Multik.ones<Double, DN>(shape, DataType.DoubleDataType).wrap()
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Benchmark
|
|
||||||
// fun nd4jAdd(blackhole: Blackhole) = with(nd4jField) {
|
|
||||||
// var res: StructureND<Double> = one(dim, dim)
|
|
||||||
// repeat(n) { res += 1.0 }
|
|
||||||
// blackhole.consume(res)
|
|
||||||
// }
|
|
||||||
|
|
||||||
private companion object {
|
|
||||||
private const val dim = 1000
|
|
||||||
private const val n = 100
|
|
||||||
private val shape = intArrayOf(dim, dim)
|
|
||||||
private val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
|
|
||||||
private val specializedField = DoubleField.ndAlgebra
|
|
||||||
private val genericField = BufferedFieldOpsND(DoubleField, Buffer.Companion::boxing)
|
|
||||||
private val nd4jField = DoubleField.nd4j
|
|
||||||
private val multikField = DoubleField.multikAlgebra
|
|
||||||
private val viktorField = DoubleField.viktorAlgebra
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.linear.linearSpace
|
|
||||||
import space.kscience.kmath.linear.matrix
|
|
||||||
import space.kscience.kmath.linear.symmetric
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.tensors.core.tensorAlgebra
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class TensorAlgebraBenchmark {
|
|
||||||
companion object {
|
|
||||||
private val random = Random(12224)
|
|
||||||
private const val dim = 30
|
|
||||||
|
|
||||||
private val matrix = DoubleField.linearSpace.matrix(dim, dim).symmetric { _, _ -> random.nextDouble() }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun tensorSymEigSvd(blackhole: Blackhole) = with(Double.tensorAlgebra) {
|
|
||||||
blackhole.consume(matrix.symEigSvd(1e-10))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun tensorSymEigJacobi(blackhole: Blackhole) = with(Double.tensorAlgebra) {
|
|
||||||
blackhole.consume(matrix.symEigJacobi(50, 1e-10))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,65 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.*
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.structures.Buffer
|
|
||||||
import space.kscience.kmath.viktor.ViktorFieldND
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class ViktorBenchmark {
|
|
||||||
@Benchmark
|
|
||||||
fun automaticFieldAddition(blackhole: Blackhole) {
|
|
||||||
with(autoField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun realFieldAddition(blackhole: Blackhole) {
|
|
||||||
with(realField) {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun viktorFieldAddition(blackhole: Blackhole) {
|
|
||||||
with(viktorField) {
|
|
||||||
var res = one(shape)
|
|
||||||
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
|
|
||||||
private val shape = Shape(dim, dim)
|
|
||||||
|
|
||||||
// automatically build context most suited for given type.
|
|
||||||
private val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
|
|
||||||
private val realField = DoubleField.ndAlgebra
|
|
||||||
private val viktorField = ViktorFieldND(dim, dim)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.BufferedFieldOpsND
|
|
||||||
import space.kscience.kmath.nd.Shape
|
|
||||||
import space.kscience.kmath.nd.ndAlgebra
|
|
||||||
import space.kscience.kmath.nd.one
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.structures.Buffer
|
|
||||||
import space.kscience.kmath.viktor.ViktorFieldND
|
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
internal class ViktorLogBenchmark {
|
|
||||||
@Benchmark
|
|
||||||
fun realFieldLog(blackhole: Blackhole) {
|
|
||||||
with(realField) {
|
|
||||||
val fortyTwo = structureND(shape) { 42.0 }
|
|
||||||
var res = one(shape)
|
|
||||||
repeat(n) { res = ln(fortyTwo) }
|
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
fun viktorFieldLog(blackhole: Blackhole) {
|
|
||||||
with(viktorField) {
|
|
||||||
val fortyTwo = structureND(shape) { 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
|
|
||||||
private val shape = Shape(dim, dim)
|
|
||||||
|
|
||||||
// automatically build context most suited for given type.
|
|
||||||
private val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
|
|
||||||
private val realField = DoubleField.ndAlgebra
|
|
||||||
private val viktorField = ViktorFieldND(dim, dim)
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,4 +4,4 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
toolsVersion=0.11.2-kotlin-1.6.10
|
toolsVersion=0.11.2-kotlin-1.6.20
|
||||||
|
@ -1,86 +0,0 @@
|
|||||||
# Algebraic Structures and Algebraic Elements
|
|
||||||
|
|
||||||
The mathematical operations in KMath are generally separated from mathematical objects. This means that to perform an
|
|
||||||
operation, say `+`, one needs two objects of a type `T` and an algebra context, which draws appropriate operation up,
|
|
||||||
say `Group<T>`. Next one needs to run the actual operation in the context:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
|
|
||||||
val a: T = ...
|
|
||||||
val b: T = ...
|
|
||||||
val group: Group<T> = ...
|
|
||||||
|
|
||||||
val c = group { a + b }
|
|
||||||
```
|
|
||||||
|
|
||||||
At first glance, this distinction seems to be a needless complication, but in fact one needs to remember that in
|
|
||||||
mathematics, one could draw up different operations on same objects. For example, one could use different types of
|
|
||||||
geometry for vectors.
|
|
||||||
|
|
||||||
## Algebraic Structures
|
|
||||||
|
|
||||||
Primary mathematical contexts have the following hierarchy:
|
|
||||||
|
|
||||||
`Field <: Ring <: Group <: Algebra`
|
|
||||||
|
|
||||||
These interfaces follow real algebraic structures:
|
|
||||||
|
|
||||||
- [Group](https://mathworld.wolfram.com/Group.html) defines addition, its identity element (i.e., 0) and additive
|
|
||||||
inverse (-x);
|
|
||||||
- [Ring](http://mathworld.wolfram.com/Ring.html) adds multiplication and its identity element (i.e., 1);
|
|
||||||
- [Field](http://mathworld.wolfram.com/Field.html) adds division operation.
|
|
||||||
|
|
||||||
A typical implementation of `Field<T>` is the `DoubleField` which works on doubles, and `VectorSpace` for `Space<T>`.
|
|
||||||
|
|
||||||
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 the context. For example, `Matrix.dot`
|
|
||||||
operation produces a matrix with new dimensions, which can be incompatible with initial matrix in linear operations.
|
|
||||||
|
|
||||||
## Spaces and Fields
|
|
||||||
|
|
||||||
KMath introduces contexts for builtin algebraic structures:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
|
|
||||||
val c1 = Complex(1.0, 2.0)
|
|
||||||
val c2 = ComplexField.i
|
|
||||||
|
|
||||||
val c3 = c1 + c2
|
|
||||||
// or
|
|
||||||
val c3 = ComplexField { c1 + c2 }
|
|
||||||
```
|
|
||||||
|
|
||||||
Also, `ComplexField` features special operations to mix complex and real numbers, for example:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
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)
|
|
||||||
val c3 = ComplexField { c1 - i * 2.0 }
|
|
||||||
```
|
|
||||||
|
|
||||||
**Note**: In theory it is possible to add behaviors directly to the context, but as for now Kotlin does not support
|
|
||||||
that. Watch [KT-10468](https://youtrack.jetbrains.com/issue/KT-10468) and
|
|
||||||
[KEEP-176](https://github.com/Kotlin/KEEP/pull/176) for updates.
|
|
||||||
|
|
||||||
## Nested fields
|
|
||||||
|
|
||||||
Contexts allow one to build more complex structures. For example, it is possible to create a `Matrix` from complex
|
|
||||||
elements like so:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
val element = NDElement.complex(shape = intArrayOf(2, 2)) { index: IntArray ->
|
|
||||||
Complex(index[0].toDouble() - index[1].toDouble(), index[0].toDouble() + index[1].toDouble())
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The `element` in this example is a member of the `Field` of 2D structures, each element of which is a member of its own
|
|
||||||
`ComplexField`. It is important one does not need to create a special n-d class to hold complex numbers and implement
|
|
||||||
operations on it, one just needs to provide a field for its elements.
|
|
||||||
|
|
||||||
**Note**: Fields themselves do not solve the problem of JVM boxing, but it is possible to solve with special contexts
|
|
||||||
like
|
|
||||||
`MemorySpec`.
|
|
@ -1,20 +0,0 @@
|
|||||||
# Buffers
|
|
||||||
|
|
||||||
Buffer is one of main building blocks of kmath. It is a basic interface allowing random-access read and write (
|
|
||||||
with `MutableBuffer`). There are different types of buffers:
|
|
||||||
|
|
||||||
* Primitive buffers wrapping like `DoubleBuffer` which are wrapping primitive arrays.
|
|
||||||
* Boxing `ListBuffer` wrapping a list
|
|
||||||
* Functionally defined `VirtualBuffer` which does not hold a state itself, but provides a function to calculate value
|
|
||||||
* `MemoryBuffer` allows direct allocation of objects in continuous memory block.
|
|
||||||
|
|
||||||
Some kmath features require a `BufferFactory` class to operate properly. A general convention is to use functions
|
|
||||||
defined in
|
|
||||||
`Buffer` and `MutableBuffer` companion classes. For example factory `Buffer.Companion::auto` in most cases creates the
|
|
||||||
most suitable buffer for given reified type (for types with custom memory buffer it still better to use their
|
|
||||||
own `MemoryBuffer.create()` factory).
|
|
||||||
|
|
||||||
## Buffer performance
|
|
||||||
|
|
||||||
One should avoid using default boxing buffer wherever it is possible. Try to use primitive buffers or memory buffers
|
|
||||||
instead .
|
|
@ -1,27 +0,0 @@
|
|||||||
# Coding Conventions
|
|
||||||
|
|
||||||
Generally, KMath code follows general [Kotlin coding conventions](https://kotlinlang.org/docs/reference/coding-conventions.html), but with a number of small changes and clarifications.
|
|
||||||
|
|
||||||
## Utility Class Naming
|
|
||||||
|
|
||||||
Filename should coincide with a name of one of the classes contained in the file or start with small letter and describe its contents.
|
|
||||||
|
|
||||||
The code convention [here](https://kotlinlang.org/docs/reference/coding-conventions.html#source-file-names) says that file names should start with a capital letter even if file does not contain classes. Yet starting utility classes and aggregators with a small letter seems to be a good way to visually separate those files.
|
|
||||||
|
|
||||||
This convention could be changed in future in a non-breaking way.
|
|
||||||
|
|
||||||
## Private Variable Naming
|
|
||||||
|
|
||||||
Private variables' names may start with underscore `_` for of the private mutable variable is shadowed by the public read-only value with the same meaning.
|
|
||||||
|
|
||||||
This rule does not permit underscores in names, but it is sometimes useful to "underscore" the fact that public and private versions draw up the same entity. It is allowed only for private variables.
|
|
||||||
|
|
||||||
This convention could be changed in future in a non-breaking way.
|
|
||||||
|
|
||||||
## Functions and Properties One-liners
|
|
||||||
|
|
||||||
Use one-liners when they occupy single code window line both for functions and properties with getters like
|
|
||||||
`val b: String get() = "fff"`. The same should be performed with multiline expressions when they could be
|
|
||||||
cleanly separated.
|
|
||||||
|
|
||||||
There is no universal consensus whenever use `fun a() = ...` or `fun a() { return ... }`. Yet from reader outlook one-lines seem to better show that the property or function is easily calculated.
|
|
@ -1,73 +0,0 @@
|
|||||||
# Context-oriented mathematics
|
|
||||||
|
|
||||||
## The problem
|
|
||||||
|
|
||||||
A known problem for implementing mathematics in statically-typed languages (but not only in them) is that different sets
|
|
||||||
of mathematical operators can be defined on the same mathematical objects. Sometimes there is no single way to treat
|
|
||||||
some operations, including basic arithmetic operations, on a Java/Kotlin `Number`. Sometimes there are different ways to
|
|
||||||
define the same structure, such as Euclidean and elliptic geometry vector spaces over real vectors. Another problem
|
|
||||||
arises when one wants to add some kind of behavior to an existing entity. In dynamic languages those problems are
|
|
||||||
usually solved by adding dynamic context-specific behaviors at runtime, but this solution has a lot of drawbacks.
|
|
||||||
|
|
||||||
## Context-oriented approach
|
|
||||||
|
|
||||||
One possible solution to these problems is to divorce numerical representations from behaviors. For example in Kotlin
|
|
||||||
one can define a separate class representing some entity without any operations, ex. a complex number:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
data class Complex(val re: Double, val im: Double)
|
|
||||||
```
|
|
||||||
|
|
||||||
And then to define a separate class or singleton, representing an operation on those complex numbers:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
object ComplexOperations {
|
|
||||||
operator fun Complex.plus(other: Complex) = Complex(re + other.re, im + other.im)
|
|
||||||
operator fun Complex.minus(other: Complex) = Complex(re - other.re, im - other.im)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
In Java, applying such external operations could be cumbersome, but Kotlin has a unique feature that allows us
|
|
||||||
implement this
|
|
||||||
naturally: [extensions with receivers](https://kotlinlang.org/docs/reference/extensions.html#extension-functions). In
|
|
||||||
Kotlin, an operation on complex number could be implemented as:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
with(ComplexOperations) { c1 + c2 - c3 }
|
|
||||||
```
|
|
||||||
|
|
||||||
Kotlin also allows the creation of functions with receivers:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
fun ComplexOperations.doSomethingWithComplex(c1: Complex, c2: Complex, c3: Complex) = c1 + c2 - c3
|
|
||||||
|
|
||||||
ComplexOperations.doComethingWithComplex(c1, c2, c3)
|
|
||||||
```
|
|
||||||
|
|
||||||
In fact, whole parts of a program may be run within a mathematical context or even multiple nested contexts.
|
|
||||||
|
|
||||||
In KMath, contexts are not only responsible for operations, but also for raw object creation and advanced features.
|
|
||||||
|
|
||||||
## Other possibilities
|
|
||||||
|
|
||||||
### Type classes
|
|
||||||
|
|
||||||
An obvious candidate to get more or less the same functionality is the type class, which allows one to bind a behavior
|
|
||||||
to a specific type without modifying the type itself. On the plus side, type classes do not require explicit context
|
|
||||||
declaration, so the code looks cleaner. On the minus side, if there are different sets of behaviors for the same types,
|
|
||||||
it is impossible to combine them into one module. Also, unlike type classes, context can have parameters or even state.
|
|
||||||
For example in KMath, sizes and strides for `NDElement` or `Matrix` could be moved to context to optimize performance in
|
|
||||||
case of a large amount of structures.
|
|
||||||
|
|
||||||
### Wildcard imports and importing-on-demand
|
|
||||||
|
|
||||||
Sometimes, one may wish to use a single context throughout a file. In this case, is possible to import all members from
|
|
||||||
a package or file, via `import context.complex.*`. Effectively, this is the same as enclosing an entire file with a
|
|
||||||
single context. However, when using multiple contexts, this technique can introduce operator ambiguity, due to namespace
|
|
||||||
pollution. If there are multiple scoped contexts that define the same operation, it is still possible to import
|
|
||||||
specific operations as needed, without using an explicit context with extension functions, for example:
|
|
||||||
|
|
||||||
```
|
|
||||||
import context.complex.op1
|
|
||||||
import context.quaternion.op2
|
|
||||||
```
|
|
File diff suppressed because it is too large
Load Diff
@ -1,21 +0,0 @@
|
|||||||
# Expressions
|
|
||||||
|
|
||||||
Expressions is a feature, which allows constructing lazily or immediately calculated parametric mathematical expressions.
|
|
||||||
|
|
||||||
The potential use-cases for it (so far) are following:
|
|
||||||
|
|
||||||
* lazy evaluation (in general simple lambda is better, but there are some border cases);
|
|
||||||
* automatic differentiation in single-dimension and in multiple dimensions;
|
|
||||||
* generation of mathematical syntax trees with subsequent code generation for other languages;
|
|
||||||
* symbolic computations, especially differentiation (and some other actions with `kmath-symja` integration with Symja's `IExpr`—integration, simplification, and more);
|
|
||||||
* visualization with `kmath-jupyter`.
|
|
||||||
|
|
||||||
The workhorse of this API is `Expression` interface, which exposes single `operator fun invoke(arguments: Map<Symbol, T>): T`
|
|
||||||
method. `ExpressionAlgebra` is used to generate expressions and introduce variables.
|
|
||||||
|
|
||||||
Currently there are two implementations:
|
|
||||||
|
|
||||||
* Generic `ExpressionField` in `kmath-core` which allows construction of custom lazy expressions
|
|
||||||
|
|
||||||
* Auto-differentiation expression in `kmath-commons` module allows using full power of `DerivativeStructure`
|
|
||||||
from commons-math. **TODO: add example**
|
|
@ -1 +0,0 @@
|
|||||||
**TODO**
|
|
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 249 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 19 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 278 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 118 KiB |
@ -1,31 +0,0 @@
|
|||||||
## Basic linear algebra layout
|
|
||||||
|
|
||||||
KMath support for linear algebra organized in a context-oriented way, which means that operations are in most cases declared in context classes, and are not the members of classes that store data. This allows more flexible approach to maintain multiple back-ends. The new operations added as extensions to contexts instead of being member functions of data structures.
|
|
||||||
|
|
||||||
The main context for linear algebra over matrices and vectors is `LinearSpace`, which defines addition and dot products of matrices and vectors:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.linear.*
|
|
||||||
|
|
||||||
LinearSpace.Companion.real {
|
|
||||||
val vec = buildVector(10) { i -> i.toDouble() }
|
|
||||||
val mat = buildMatrix(10, 10) { i, j -> i.toDouble() + j }
|
|
||||||
|
|
||||||
// Addition
|
|
||||||
vec + vec
|
|
||||||
mat + mat
|
|
||||||
|
|
||||||
// Multiplication by scalar
|
|
||||||
vec * 2.0
|
|
||||||
mat * 2.0
|
|
||||||
|
|
||||||
// Dot product
|
|
||||||
mat dot vec
|
|
||||||
mat dot mat
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Backends overview
|
|
||||||
|
|
||||||
### EJML
|
|
||||||
### Commons Math
|
|
@ -1,128 +0,0 @@
|
|||||||
# ND-structure generation and operations
|
|
||||||
|
|
||||||
**TODO**
|
|
||||||
|
|
||||||
# Performance for n-dimensional structures operations
|
|
||||||
|
|
||||||
One of the most sought after features of mathematical libraries is the high-performance operations on n-dimensional
|
|
||||||
structures. In `kmath` performance depends on which particular context was used for operation.
|
|
||||||
|
|
||||||
Let us consider following contexts:
|
|
||||||
```kotlin
|
|
||||||
// automatically build context most suited for given type.
|
|
||||||
val autoField = NDField.auto(DoubleField, dim, dim)
|
|
||||||
// specialized nd-field for Double. It works as generic Double field as well.
|
|
||||||
val specializedField = NDField.real(dim, dim)
|
|
||||||
//A generic boxing field. It should be used for objects, not primitives.
|
|
||||||
val genericField = NDField.buffered(DoubleField, dim, dim)
|
|
||||||
```
|
|
||||||
Now let us perform several tests and see, which implementation is best suited for each case:
|
|
||||||
|
|
||||||
## Test case
|
|
||||||
|
|
||||||
To test performance we will take 2d-structures with `dim = 1000` and add a structure filled with `1.0`
|
|
||||||
to it `n = 1000` times.
|
|
||||||
|
|
||||||
## Specialized
|
|
||||||
The code to run this looks like:
|
|
||||||
```kotlin
|
|
||||||
specializedField.run {
|
|
||||||
var res: NDBuffer<Double> = one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
The performance of this code is the best of all tests since it inlines all operations and is specialized for operation
|
|
||||||
with doubles. We will measure everything else relative to this one, so time for this test will be `1x` (real time
|
|
||||||
on my computer is about 4.5 seconds). The only problem with this approach is that it requires specifying type
|
|
||||||
from the beginning. Everyone does so anyway, so it is the recommended approach.
|
|
||||||
|
|
||||||
## Automatic
|
|
||||||
Let's do the same with automatic field inference:
|
|
||||||
```kotlin
|
|
||||||
autoField.run {
|
|
||||||
var res = one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
Ths speed of this operation is approximately the same as for specialized case since `NDField.auto` just
|
|
||||||
returns the same `RealNDField` in this case. Of course, it is usually better to use specialized method to be sure.
|
|
||||||
|
|
||||||
## Lazy
|
|
||||||
Lazy field does not produce a structure when asked, instead it generates an empty structure and fills it on-demand
|
|
||||||
using coroutines to parallelize computations.
|
|
||||||
When one calls
|
|
||||||
```kotlin
|
|
||||||
lazyField.run {
|
|
||||||
var res = one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
The result will be calculated almost immediately but the result will be empty. To get the full result
|
|
||||||
structure one needs to call all its elements. In this case computation overhead will be huge. So this field never
|
|
||||||
should be used if one expects to use the full result structure. Though if one wants only small fraction, it could
|
|
||||||
save a lot of time.
|
|
||||||
|
|
||||||
This field still could be used with reasonable performance if call code is changed:
|
|
||||||
```kotlin
|
|
||||||
lazyField.run {
|
|
||||||
val res = one.map {
|
|
||||||
var c = 0.0
|
|
||||||
repeat(n) {
|
|
||||||
c += 1.0
|
|
||||||
}
|
|
||||||
c
|
|
||||||
}
|
|
||||||
|
|
||||||
res.elements().forEach { it.second }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
In this case it completes in about `4x-5x` time due to boxing.
|
|
||||||
|
|
||||||
## Boxing
|
|
||||||
The boxing field produced by
|
|
||||||
```kotlin
|
|
||||||
genericField.run {
|
|
||||||
var res: NDBuffer<Double> = one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
is the slowest one, because it requires boxing and unboxing the `double` on each operation. It takes about
|
|
||||||
`15x` time (**TODO: there seems to be a problem here, it should be slow, but not that slow**). This field should
|
|
||||||
never be used for primitives.
|
|
||||||
|
|
||||||
## Element operation
|
|
||||||
Let us also check the speed for direct operations on elements:
|
|
||||||
```kotlin
|
|
||||||
var res = genericField.one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
```
|
|
||||||
One would expect to be at least as slow as field operation, but in fact, this one takes only `2x` time to complete.
|
|
||||||
It happens, because in this particular case it does not use actual `NDField` but instead calculated directly
|
|
||||||
via extension function.
|
|
||||||
|
|
||||||
## What about python?
|
|
||||||
|
|
||||||
Usually it is bad idea to compare the direct numerical operation performance in different languages, but it hard to
|
|
||||||
work completely without frame of reference. In this case, simple numpy code:
|
|
||||||
```python
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
res = np.ones((1000,1000))
|
|
||||||
for i in range(1000):
|
|
||||||
res = res + 1.0
|
|
||||||
```
|
|
||||||
gives the completion time of about `1.1x`, which means that specialized kotlin code in fact is working faster (I think it is
|
|
||||||
because better memory management). Of course if one writes `res += 1.0`, the performance will be different,
|
|
||||||
but it would be different case, because numpy overrides `+=` with in-place operations. In-place operations are
|
|
||||||
available in `kmath` with `MutableNDStructure` but there is no field for it (one can still work with mapping
|
|
||||||
functions).
|
|
@ -1,14 +0,0 @@
|
|||||||
# Documentation
|
|
||||||
|
|
||||||
* [Algebra](algebra.md): [context-based](contexts.md) operations on different primitives and structures.
|
|
||||||
|
|
||||||
* [NDStructures](nd-structure.md)
|
|
||||||
|
|
||||||
* [Linear algebra](linear.md): matrices, operations and linear equations solving. To be moved to separate module.
|
|
||||||
Currently, supports basic API and multiple library back-ends.
|
|
||||||
|
|
||||||
* [Histograms](histograms.md): multidimensional histogram calculation and operations.
|
|
||||||
|
|
||||||
* [Expressions](expressions.md)
|
|
||||||
|
|
||||||
* Commons math integration
|
|
26
docs/templates/ARTIFACT-TEMPLATE.md
vendored
26
docs/templates/ARTIFACT-TEMPLATE.md
vendored
@ -1,26 +0,0 @@
|
|||||||
## Artifact:
|
|
||||||
|
|
||||||
The Maven coordinates of this project are `${group}:${name}:${version}`.
|
|
||||||
|
|
||||||
**Gradle:**
|
|
||||||
```gradle
|
|
||||||
repositories {
|
|
||||||
maven { url 'https://repo.kotlin.link' }
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation '${group}:${name}:${version}'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
**Gradle Kotlin DSL:**
|
|
||||||
```kotlin
|
|
||||||
repositories {
|
|
||||||
maven("https://repo.kotlin.link")
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation("${group}:${name}:${version}")
|
|
||||||
}
|
|
||||||
```
|
|
105
docs/templates/README-TEMPLATE.md
vendored
105
docs/templates/README-TEMPLATE.md
vendored
@ -1,105 +0,0 @@
|
|||||||
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
|
|
||||||
[![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382)
|
|
||||||
![Gradle build](https://github.com/mipt-npm/kmath/workflows/Gradle%20build/badge.svg)
|
|
||||||
[![Maven Central](https://img.shields.io/maven-central/v/space.kscience/kmath-core.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22space.kscience%22)
|
|
||||||
[![Space](https://img.shields.io/badge/dynamic/xml?color=orange&label=Space&query=//metadata/versioning/latest&url=https%3A%2F%2Fmaven.pkg.jetbrains.space%2Fmipt-npm%2Fp%2Fsci%2Fmaven%2Fspace%2Fkscience%2Fkmath-core%2Fmaven-metadata.xml)](https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven/space/kscience/)
|
|
||||||
|
|
||||||
# KMath
|
|
||||||
|
|
||||||
Could be pronounced as `key-math`. The **K**otlin **Math**ematics library was initially intended as a Kotlin-based
|
|
||||||
analog to Python's NumPy library. Later we found that kotlin is much more flexible language and allows superior
|
|
||||||
architecture designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like
|
|
||||||
experience could be achieved with [kmath-for-real](/kmath-for-real) extension module.
|
|
||||||
|
|
||||||
[Documentation site (**WIP**)](https://mipt-npm.github.io/kmath/)
|
|
||||||
|
|
||||||
## Publications and talks
|
|
||||||
|
|
||||||
* [A conceptual article about context-oriented design](https://proandroiddev.com/an-introduction-context-oriented-programming-in-kotlin-2e79d316b0a2)
|
|
||||||
* [Another article about context-oriented design](https://proandroiddev.com/diving-deeper-into-context-oriented-programming-in-kotlin-3ecb4ec38814)
|
|
||||||
* [ACAT 2019 conference paper](https://aip.scitation.org/doi/abs/10.1063/1.5130103)
|
|
||||||
|
|
||||||
# Goal
|
|
||||||
|
|
||||||
* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM, JS and Native)
|
|
||||||
.
|
|
||||||
* Provide basic multiplatform implementations for those abstractions (without significant performance optimization).
|
|
||||||
* Provide bindings and wrappers with those abstractions for popular optimized platform libraries.
|
|
||||||
|
|
||||||
## Non-goals
|
|
||||||
|
|
||||||
* Be like NumPy. It was the idea at the beginning, but we decided that we can do better in API.
|
|
||||||
* Provide the best performance out of the box. We have specialized libraries for that. Need only API wrappers for them.
|
|
||||||
* Cover all cases as immediately and in one bundle. We will modularize everything and add new features gradually.
|
|
||||||
* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like
|
|
||||||
for `Double` in the core. For that we will have specialization modules like `kmath-for-real`, which will give better
|
|
||||||
experience for those, who want to work with specific types.
|
|
||||||
|
|
||||||
## Features and stability
|
|
||||||
|
|
||||||
KMath is a modular library. Different modules provide different features with different API stability guarantees. All
|
|
||||||
core modules are released with the same version, but with different API change policy. The features are described in
|
|
||||||
module definitions below. The module stability could have the following levels:
|
|
||||||
|
|
||||||
* **PROTOTYPE**. On this level there are no compatibility guarantees. All methods and classes form those modules could
|
|
||||||
break any moment. You can still use it, but be sure to fix the specific version.
|
|
||||||
* **EXPERIMENTAL**. The general API is decided, but some changes could be made. Volatile API is marked
|
|
||||||
with `@UnstableKmathAPI` or other stability warning annotations.
|
|
||||||
* **DEVELOPMENT**. API breaking generally follows semantic versioning ideology. There could be changes in minor
|
|
||||||
versions, but not in patch versions. API is protected
|
|
||||||
with [binary-compatibility-validator](https://github.com/Kotlin/binary-compatibility-validator) tool.
|
|
||||||
* **STABLE**. The API stabilized. Breaking changes are allowed only in major releases.
|
|
||||||
|
|
||||||
## Modules
|
|
||||||
|
|
||||||
${modules}
|
|
||||||
|
|
||||||
## Multi-platform support
|
|
||||||
|
|
||||||
KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the
|
|
||||||
[common source sets](/kmath-core/src/commonMain) and implemented there wherever it is possible. In some cases, features
|
|
||||||
are delegated to platform-specific implementations even if they could be provided in the common module for performance
|
|
||||||
reasons. Currently, the Kotlin/JVM is the primary platform, however Kotlin/Native and Kotlin/JS contributions and
|
|
||||||
feedback are also welcome.
|
|
||||||
|
|
||||||
## Performance
|
|
||||||
|
|
||||||
Calculation performance is one of major goals of KMath in the future, but in some cases it is impossible to achieve both
|
|
||||||
performance and flexibility.
|
|
||||||
|
|
||||||
We expect to focus on creating convenient universal API first and then work on increasing performance for specific
|
|
||||||
cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized
|
|
||||||
native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be
|
|
||||||
better than SciPy.
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend to use GraalVM-CE 11 for
|
|
||||||
execution to get better performance.
|
|
||||||
|
|
||||||
### Repositories
|
|
||||||
|
|
||||||
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://repo.kotlin.link")
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
api("${group}:kmath-core:$version")
|
|
||||||
// api("${group}:kmath-core-jvm:$version") for jvm-specific version
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Gradle `6.0+` is required for multiplatform artifacts.
|
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
The project requires a lot of additional work. The most important thing we need is a feedback about what features are
|
|
||||||
required the most. Feel free to create feature requests. We are also welcome to code contributions, especially in issues
|
|
||||||
marked with
|
|
||||||
[waiting for a hero](https://github.com/mipt-npm/kmath/labels/waiting%20for%20a%20hero) label.
|
|
@ -1,4 +0,0 @@
|
|||||||
# Module examples
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
|||||||
plugins {
|
|
||||||
kotlin("jvm")
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
maven("https://repo.kotlin.link")
|
|
||||||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-js-wrappers")
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation(project(":kmath-ast"))
|
|
||||||
implementation(project(":kmath-kotlingrad"))
|
|
||||||
implementation(project(":kmath-core"))
|
|
||||||
implementation(project(":kmath-coroutines"))
|
|
||||||
implementation(project(":kmath-commons"))
|
|
||||||
implementation(project(":kmath-complex"))
|
|
||||||
implementation(project(":kmath-optimization"))
|
|
||||||
implementation(project(":kmath-stat"))
|
|
||||||
implementation(project(":kmath-viktor"))
|
|
||||||
implementation(project(":kmath-dimensions"))
|
|
||||||
implementation(project(":kmath-ejml"))
|
|
||||||
implementation(project(":kmath-nd4j"))
|
|
||||||
implementation(project(":kmath-tensors"))
|
|
||||||
implementation(project(":kmath-symja"))
|
|
||||||
implementation(project(":kmath-for-real"))
|
|
||||||
//jafama
|
|
||||||
implementation(project(":kmath-jafama"))
|
|
||||||
//multik
|
|
||||||
implementation(project(":kmath-multik"))
|
|
||||||
|
|
||||||
|
|
||||||
implementation("org.nd4j:nd4j-native:1.0.0-beta7")
|
|
||||||
|
|
||||||
// uncomment if your system supports AVX2
|
|
||||||
// val os = System.getProperty("os.name")
|
|
||||||
//
|
|
||||||
// if (System.getProperty("os.arch") in arrayOf("x86_64", "amd64")) when {
|
|
||||||
// os.startsWith("Windows") -> implementation("org.nd4j:nd4j-native:1.0.0-beta7:windows-x86_64-avx2")
|
|
||||||
// os == "Linux" -> implementation("org.nd4j:nd4j-native:1.0.0-beta7:linux-x86_64-avx2")
|
|
||||||
// os == "Mac OS X" -> implementation("org.nd4j:nd4j-native:1.0.0-beta7:macosx-x86_64-avx2")
|
|
||||||
// } else
|
|
||||||
implementation("org.nd4j:nd4j-native-platform:1.0.0-beta7")
|
|
||||||
|
|
||||||
// multik implementation
|
|
||||||
implementation("org.jetbrains.kotlinx:multik-default:0.1.0")
|
|
||||||
|
|
||||||
implementation("org.slf4j:slf4j-simple:1.7.32")
|
|
||||||
// plotting
|
|
||||||
implementation("space.kscience:plotlykt-server:0.5.0")
|
|
||||||
}
|
|
||||||
|
|
||||||
kotlin.sourceSets.all {
|
|
||||||
with(languageSettings) {
|
|
||||||
optIn("kotlin.contracts.ExperimentalContracts")
|
|
||||||
optIn("kotlin.ExperimentalUnsignedTypes")
|
|
||||||
optIn("space.kscience.kmath.misc.UnstableKMathAPI")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile> {
|
|
||||||
kotlinOptions {
|
|
||||||
jvmTarget = "11"
|
|
||||||
freeCompilerArgs = freeCompilerArgs + "-Xjvm-default=all" + "-Xopt-in=kotlin.RequiresOptIn" + "-Xlambdas=indy"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
readme {
|
|
||||||
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess
|
|
||||||
import space.kscience.kmath.ast.rendering.LatexSyntaxRenderer
|
|
||||||
import space.kscience.kmath.ast.rendering.MathMLSyntaxRenderer
|
|
||||||
import space.kscience.kmath.ast.rendering.renderWithStringBuilder
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val mst = "exp(sqrt(x))-asin(2*x)/(2e10+x^3)/(-12)".parseMath()
|
|
||||||
val syntax = FeaturedMathRendererWithPostProcess.Default.render(mst)
|
|
||||||
println("MathSyntax:")
|
|
||||||
println(syntax)
|
|
||||||
println()
|
|
||||||
val latex = LatexSyntaxRenderer.renderWithStringBuilder(syntax)
|
|
||||||
println("LaTeX:")
|
|
||||||
println(latex)
|
|
||||||
println()
|
|
||||||
val mathML = MathMLSyntaxRenderer.renderWithStringBuilder(syntax)
|
|
||||||
println("MathML:")
|
|
||||||
println(mathML)
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.asm.compileToExpression
|
|
||||||
import space.kscience.kmath.expressions.MstExtendedField
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val expr = MstExtendedField {
|
|
||||||
x * 2.0 + number(2.0) / x - number(16.0) + asinh(x) / sin(x)
|
|
||||||
}.compileToExpression(DoubleField)
|
|
||||||
|
|
||||||
val m = DoubleArray(expr.indexer.symbols.size)
|
|
||||||
val xIdx = expr.indexer.indexOf(x)
|
|
||||||
|
|
||||||
repeat(10000000) {
|
|
||||||
m[xIdx] = 1.0
|
|
||||||
expr(m)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.derivative
|
|
||||||
import space.kscience.kmath.expressions.invoke
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.toExpression
|
|
||||||
import space.kscience.kmath.kotlingrad.toKotlingradExpression
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
|
|
||||||
/**
|
|
||||||
* In this example, *x<sup>2</sup> − 4 x − 44* function is differentiated with Kotlin∇, and the
|
|
||||||
* derivation result is compared with valid derivative in a certain point.
|
|
||||||
*/
|
|
||||||
fun main() {
|
|
||||||
val actualDerivative = "x^2-4*x-44"
|
|
||||||
.parseMath()
|
|
||||||
.toKotlingradExpression(DoubleField)
|
|
||||||
.derivative(x)
|
|
||||||
|
|
||||||
val expectedDerivative = "2*x-4".parseMath().toExpression(DoubleField)
|
|
||||||
check(actualDerivative(x to 123.0) == expectedDerivative(x to 123.0))
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.derivative
|
|
||||||
import space.kscience.kmath.expressions.invoke
|
|
||||||
import space.kscience.kmath.expressions.toExpression
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.symja.toSymjaExpression
|
|
||||||
|
|
||||||
/**
|
|
||||||
* In this example, *x<sup>2</sup> − 4 x − 44* function is differentiated with Symja, and the
|
|
||||||
* derivation result is compared with valid derivative in a certain point.
|
|
||||||
*/
|
|
||||||
fun main() {
|
|
||||||
val actualDerivative = "x^2-4*x-44"
|
|
||||||
.parseMath()
|
|
||||||
.toSymjaExpression(DoubleField)
|
|
||||||
.derivative(x)
|
|
||||||
|
|
||||||
val expectedDerivative = "2*x-4".parseMath().toExpression(DoubleField)
|
|
||||||
check(actualDerivative(x to 123.0) == expectedDerivative(x to 123.0))
|
|
||||||
}
|
|
@ -1,115 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.fit
|
|
||||||
|
|
||||||
import kotlinx.html.br
|
|
||||||
import kotlinx.html.h3
|
|
||||||
import space.kscience.kmath.commons.expressions.DSProcessor
|
|
||||||
import space.kscience.kmath.commons.optimization.CMOptimizer
|
|
||||||
import space.kscience.kmath.distributions.NormalDistribution
|
|
||||||
import space.kscience.kmath.expressions.chiSquaredExpression
|
|
||||||
import space.kscience.kmath.expressions.symbol
|
|
||||||
import space.kscience.kmath.operations.asIterable
|
|
||||||
import space.kscience.kmath.operations.toList
|
|
||||||
import space.kscience.kmath.optimization.FunctionOptimizationTarget
|
|
||||||
import space.kscience.kmath.optimization.optimizeWith
|
|
||||||
import space.kscience.kmath.optimization.resultPoint
|
|
||||||
import space.kscience.kmath.optimization.resultValue
|
|
||||||
import space.kscience.kmath.real.DoubleVector
|
|
||||||
import space.kscience.kmath.real.map
|
|
||||||
import space.kscience.kmath.real.step
|
|
||||||
import space.kscience.kmath.stat.RandomGenerator
|
|
||||||
import space.kscience.plotly.*
|
|
||||||
import space.kscience.plotly.models.ScatterMode
|
|
||||||
import space.kscience.plotly.models.TraceValues
|
|
||||||
import kotlin.math.pow
|
|
||||||
import kotlin.math.sqrt
|
|
||||||
|
|
||||||
// Forward declaration of symbols that will be used in expressions.
|
|
||||||
private val a by symbol
|
|
||||||
private val b by symbol
|
|
||||||
private val c by symbol
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shortcut to use buffers in plotly
|
|
||||||
*/
|
|
||||||
operator fun TraceValues.invoke(vector: DoubleVector) {
|
|
||||||
numbers = vector.asIterable()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Least squares fie with auto-differentiation. Uses `kmath-commons` and `kmath-for-real` modules.
|
|
||||||
*/
|
|
||||||
suspend fun main() {
|
|
||||||
//A generator for a normally distributed values
|
|
||||||
val generator = NormalDistribution(0.0, 1.0)
|
|
||||||
|
|
||||||
//A chain/flow of random values with the given seed
|
|
||||||
val chain = generator.sample(RandomGenerator.default(112667))
|
|
||||||
|
|
||||||
|
|
||||||
//Create a uniformly distributed x values like numpy.arrange
|
|
||||||
val x = 1.0..100.0 step 1.0
|
|
||||||
|
|
||||||
|
|
||||||
//Perform an operation on each x value (much more effective, than numpy)
|
|
||||||
val y = x.map { it ->
|
|
||||||
val value = it.pow(2) + it + 1
|
|
||||||
value + chain.next() * sqrt(value)
|
|
||||||
}
|
|
||||||
// this will also work, but less effective:
|
|
||||||
// val y = x.pow(2)+ x + 1 + chain.nextDouble()
|
|
||||||
|
|
||||||
// create same errors for all xs
|
|
||||||
val yErr = y.map { sqrt(it) }//RealVector.same(x.size, sigma)
|
|
||||||
|
|
||||||
// compute differentiable chi^2 sum for given model ax^2 + bx + c
|
|
||||||
val chi2 = DSProcessor.chiSquaredExpression(x, y, yErr) { arg ->
|
|
||||||
//bind variables to autodiff context
|
|
||||||
val a = bindSymbol(a)
|
|
||||||
val b = bindSymbol(b)
|
|
||||||
//Include default value for c if it is not provided as a parameter
|
|
||||||
val c = bindSymbolOrNull(c) ?: one
|
|
||||||
a * arg.pow(2) + b * arg + c
|
|
||||||
}
|
|
||||||
|
|
||||||
//minimize the chi^2 in given starting point. Derivatives are not required, they are already included.
|
|
||||||
val result = chi2.optimizeWith(
|
|
||||||
CMOptimizer,
|
|
||||||
mapOf(a to 1.5, b to 0.9, c to 1.0),
|
|
||||||
FunctionOptimizationTarget.MINIMIZE
|
|
||||||
)
|
|
||||||
|
|
||||||
//display a page with plot and numerical results
|
|
||||||
val page = Plotly.page {
|
|
||||||
plot {
|
|
||||||
scatter {
|
|
||||||
mode = ScatterMode.markers
|
|
||||||
x(x)
|
|
||||||
y(y)
|
|
||||||
error_y {
|
|
||||||
array = yErr.toList()
|
|
||||||
}
|
|
||||||
name = "data"
|
|
||||||
}
|
|
||||||
scatter {
|
|
||||||
mode = ScatterMode.lines
|
|
||||||
x(x)
|
|
||||||
y(x.map { result.resultPoint[a]!! * it.pow(2) + result.resultPoint[b]!! * it + 1 })
|
|
||||||
name = "fit"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
br()
|
|
||||||
h3 {
|
|
||||||
+"Fit result: $result"
|
|
||||||
}
|
|
||||||
h3 {
|
|
||||||
+"Chi2/dof = ${result.resultValue / (x.size - 3)}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
page.makeFile()
|
|
||||||
}
|
|
@ -1,106 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.fit
|
|
||||||
|
|
||||||
import kotlinx.html.br
|
|
||||||
import kotlinx.html.h3
|
|
||||||
import space.kscience.kmath.commons.expressions.DSProcessor
|
|
||||||
import space.kscience.kmath.data.XYErrorColumnarData
|
|
||||||
import space.kscience.kmath.distributions.NormalDistribution
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.expressions.binding
|
|
||||||
import space.kscience.kmath.expressions.symbol
|
|
||||||
import space.kscience.kmath.operations.asIterable
|
|
||||||
import space.kscience.kmath.operations.toList
|
|
||||||
import space.kscience.kmath.optimization.QowOptimizer
|
|
||||||
import space.kscience.kmath.optimization.chiSquaredOrNull
|
|
||||||
import space.kscience.kmath.optimization.fitWith
|
|
||||||
import space.kscience.kmath.optimization.resultPoint
|
|
||||||
import space.kscience.kmath.real.map
|
|
||||||
import space.kscience.kmath.real.step
|
|
||||||
import space.kscience.kmath.stat.RandomGenerator
|
|
||||||
import space.kscience.plotly.*
|
|
||||||
import space.kscience.plotly.models.ScatterMode
|
|
||||||
import kotlin.math.abs
|
|
||||||
import kotlin.math.pow
|
|
||||||
import kotlin.math.sqrt
|
|
||||||
|
|
||||||
// Forward declaration of symbols that will be used in expressions.
|
|
||||||
private val a by symbol
|
|
||||||
private val b by symbol
|
|
||||||
private val c by symbol
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Least squares fie with auto-differentiation. Uses `kmath-commons` and `kmath-for-real` modules.
|
|
||||||
*/
|
|
||||||
suspend fun main() {
|
|
||||||
//A generator for a normally distributed values
|
|
||||||
val generator = NormalDistribution(0.0, 1.0)
|
|
||||||
|
|
||||||
//A chain/flow of random values with the given seed
|
|
||||||
val chain = generator.sample(RandomGenerator.default(112667))
|
|
||||||
|
|
||||||
|
|
||||||
//Create a uniformly distributed x values like numpy.arrange
|
|
||||||
val x = 1.0..100.0 step 1.0
|
|
||||||
|
|
||||||
|
|
||||||
//Perform an operation on each x value (much more effective, than numpy)
|
|
||||||
val y = x.map { it ->
|
|
||||||
val value = it.pow(2) + it + 1
|
|
||||||
value + chain.next() * sqrt(value)
|
|
||||||
}
|
|
||||||
// this will also work, but less effective:
|
|
||||||
// val y = x.pow(2)+ x + 1 + chain.nextDouble()
|
|
||||||
|
|
||||||
// create same errors for all xs
|
|
||||||
val yErr = y.map { sqrt(abs(it)) }
|
|
||||||
require(yErr.asIterable().all { it > 0 }) { "All errors must be strictly positive" }
|
|
||||||
|
|
||||||
val result = XYErrorColumnarData.of(x, y, yErr).fitWith(
|
|
||||||
QowOptimizer,
|
|
||||||
DSProcessor,
|
|
||||||
mapOf(a to 0.9, b to 1.2, c to 2.0)
|
|
||||||
) { arg ->
|
|
||||||
//bind variables to autodiff context
|
|
||||||
val a by binding
|
|
||||||
val b by binding
|
|
||||||
//Include default value for c if it is not provided as a parameter
|
|
||||||
val c = bindSymbolOrNull(c) ?: one
|
|
||||||
a * arg.pow(2) + b * arg + c
|
|
||||||
}
|
|
||||||
|
|
||||||
//display a page with plot and numerical results
|
|
||||||
val page = Plotly.page {
|
|
||||||
plot {
|
|
||||||
scatter {
|
|
||||||
mode = ScatterMode.markers
|
|
||||||
x(x)
|
|
||||||
y(y)
|
|
||||||
error_y {
|
|
||||||
array = yErr.toList()
|
|
||||||
}
|
|
||||||
name = "data"
|
|
||||||
}
|
|
||||||
scatter {
|
|
||||||
mode = ScatterMode.lines
|
|
||||||
x(x)
|
|
||||||
y(x.map { result.model(result.resultPoint + (Symbol.x to it)) })
|
|
||||||
name = "fit"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
br()
|
|
||||||
h3 {
|
|
||||||
+"Fit result: ${result.resultPoint}"
|
|
||||||
}
|
|
||||||
h3 {
|
|
||||||
+"Chi2/dof = ${result.chiSquaredOrNull!! / (x.size - 3)}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
page.makeFile()
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.functions
|
|
||||||
|
|
||||||
import space.kscience.kmath.integration.gaussIntegrator
|
|
||||||
import space.kscience.kmath.integration.integrate
|
|
||||||
import space.kscience.kmath.integration.value
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import kotlin.math.pow
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
//Define a function
|
|
||||||
val function: UnivariateFunction<Double> = { x -> 3 * x.pow(2) + 2 * x + 1 }
|
|
||||||
|
|
||||||
//get the result of the integration
|
|
||||||
val result = DoubleField.gaussIntegrator.integrate(0.0..10.0, function = function)
|
|
||||||
|
|
||||||
//the value is nullable because in some cases the integration could not succeed
|
|
||||||
println(result.value)
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.functions
|
|
||||||
|
|
||||||
import space.kscience.kmath.interpolation.SplineInterpolator
|
|
||||||
import space.kscience.kmath.interpolation.interpolatePolynomials
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.structures.DoubleBuffer
|
|
||||||
import space.kscience.plotly.Plotly
|
|
||||||
import space.kscience.plotly.UnstablePlotlyAPI
|
|
||||||
import space.kscience.plotly.makeFile
|
|
||||||
import space.kscience.plotly.models.functionXY
|
|
||||||
import space.kscience.plotly.scatter
|
|
||||||
import kotlin.math.PI
|
|
||||||
import kotlin.math.sin
|
|
||||||
|
|
||||||
@OptIn(UnstablePlotlyAPI::class)
|
|
||||||
fun main() {
|
|
||||||
val data = (0..10).map {
|
|
||||||
val x = it.toDouble() / 5 * PI
|
|
||||||
x to sin(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
val polynomial: PiecewisePolynomial<Double> = SplineInterpolator(
|
|
||||||
DoubleField, ::DoubleBuffer
|
|
||||||
).interpolatePolynomials(data)
|
|
||||||
|
|
||||||
val function = polynomial.asFunction(DoubleField, 0.0)
|
|
||||||
|
|
||||||
val cmInterpolate = org.apache.commons.math3.analysis.interpolation.SplineInterpolator().interpolate(
|
|
||||||
data.map { it.first }.toDoubleArray(),
|
|
||||||
data.map { it.second }.toDoubleArray()
|
|
||||||
)
|
|
||||||
|
|
||||||
Plotly.plot {
|
|
||||||
scatter {
|
|
||||||
name = "interpolated"
|
|
||||||
x.numbers = data.map { it.first }
|
|
||||||
y.numbers = x.doubles.map { function(it) }
|
|
||||||
}
|
|
||||||
scatter {
|
|
||||||
name = "original"
|
|
||||||
functionXY(0.0..(2 * PI), 0.1) { sin(it) }
|
|
||||||
}
|
|
||||||
scatter {
|
|
||||||
name = "cm"
|
|
||||||
x.numbers = data.map { it.first }
|
|
||||||
y.numbers = x.doubles.map { cmInterpolate.value(it) }
|
|
||||||
}
|
|
||||||
}.makeFile()
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.functions
|
|
||||||
|
|
||||||
import space.kscience.kmath.interpolation.interpolatePolynomials
|
|
||||||
import space.kscience.kmath.interpolation.splineInterpolator
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.real.map
|
|
||||||
import space.kscience.kmath.real.step
|
|
||||||
import space.kscience.plotly.Plotly
|
|
||||||
import space.kscience.plotly.UnstablePlotlyAPI
|
|
||||||
import space.kscience.plotly.makeFile
|
|
||||||
import space.kscience.plotly.models.functionXY
|
|
||||||
import space.kscience.plotly.scatter
|
|
||||||
|
|
||||||
@OptIn(UnstablePlotlyAPI::class)
|
|
||||||
fun main() {
|
|
||||||
val function: UnivariateFunction<Double> = { x ->
|
|
||||||
if (x in 30.0..50.0) {
|
|
||||||
1.0
|
|
||||||
} else {
|
|
||||||
0.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val xs = 0.0..100.0 step 0.5
|
|
||||||
val ys = xs.map(function)
|
|
||||||
|
|
||||||
val polynomial: PiecewisePolynomial<Double> = DoubleField.splineInterpolator.interpolatePolynomials(xs, ys)
|
|
||||||
|
|
||||||
val polyFunction = polynomial.asFunction(DoubleField, 0.0)
|
|
||||||
|
|
||||||
Plotly.plot {
|
|
||||||
scatter {
|
|
||||||
name = "interpolated"
|
|
||||||
functionXY(25.0..55.0, 0.1) { polyFunction(it) }
|
|
||||||
}
|
|
||||||
scatter {
|
|
||||||
name = "original"
|
|
||||||
functionXY(25.0..55.0, 0.1) { function(it) }
|
|
||||||
}
|
|
||||||
}.makeFile()
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.functions
|
|
||||||
|
|
||||||
import space.kscience.kmath.integration.gaussIntegrator
|
|
||||||
import space.kscience.kmath.integration.integrate
|
|
||||||
import space.kscience.kmath.integration.value
|
|
||||||
import space.kscience.kmath.nd.StructureND
|
|
||||||
import space.kscience.kmath.nd.structureND
|
|
||||||
import space.kscience.kmath.nd.withNdAlgebra
|
|
||||||
import space.kscience.kmath.operations.algebra
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
|
|
||||||
fun main(): Unit = Double.algebra {
|
|
||||||
withNdAlgebra(2, 2) {
|
|
||||||
|
|
||||||
//Produce a diagonal StructureND
|
|
||||||
fun diagonal(v: Double) = structureND { (i, j) ->
|
|
||||||
if (i == j) v else 0.0
|
|
||||||
}
|
|
||||||
|
|
||||||
//Define a function in a nd space
|
|
||||||
val function: (Double) -> StructureND<Double> = { x: Double -> 3 * x.pow(2) + 2 * diagonal(x) + 1 }
|
|
||||||
|
|
||||||
//get the result of the integration
|
|
||||||
val result = gaussIntegrator.integrate(0.0..10.0, function = function)
|
|
||||||
|
|
||||||
//the value is nullable because in some cases the integration could not succeed
|
|
||||||
println(result.value)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.jafama
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val a = 2.0
|
|
||||||
val b = StrictJafamaDoubleField { exp(a) }
|
|
||||||
println(JafamaDoubleField { b + a })
|
|
||||||
println(StrictJafamaDoubleField { ln(b) })
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.linear
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.algebra
|
|
||||||
import kotlin.random.Random
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val random = Random(12224)
|
|
||||||
val dim = 1000
|
|
||||||
|
|
||||||
//creating invertible matrix
|
|
||||||
val matrix1 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j ->
|
|
||||||
if (i <= j) random.nextDouble() else 0.0
|
|
||||||
}
|
|
||||||
val matrix2 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j ->
|
|
||||||
if (i <= j) random.nextDouble() else 0.0
|
|
||||||
}
|
|
||||||
|
|
||||||
val time = measureTimeMillis {
|
|
||||||
with(Double.algebra.linearSpace) {
|
|
||||||
repeat(10) {
|
|
||||||
matrix1 dot matrix2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println(time)
|
|
||||||
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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>) -> Double = { x ->
|
|
||||||
require(x.size == x0.size)
|
|
||||||
kotlin.math.exp(-((x - x0) / sigma).square().sum())
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ((Point<Double>) -> Double).grad(x: Point<Double>): Point<Double> {
|
|
||||||
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 = this(x + dVector / 2)
|
|
||||||
val f0 = this(x - dVector / 2)
|
|
||||||
(f1 - f0) / h
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println(gaussian.grad(x0))
|
|
||||||
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.operations
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val res = BigIntField { number(1) * 2 }
|
|
||||||
println("bigint:$res")
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.operations
|
|
||||||
|
|
||||||
import space.kscience.kmath.complex.Complex
|
|
||||||
import space.kscience.kmath.complex.algebra
|
|
||||||
import space.kscience.kmath.complex.bufferAlgebra
|
|
||||||
import space.kscience.kmath.complex.ndAlgebra
|
|
||||||
import space.kscience.kmath.nd.BufferND
|
|
||||||
import space.kscience.kmath.nd.StructureND
|
|
||||||
import space.kscience.kmath.nd.structureND
|
|
||||||
|
|
||||||
fun main() = Complex.algebra {
|
|
||||||
val complex = 2 + 2 * i
|
|
||||||
println(complex * 8 - 5 * i)
|
|
||||||
|
|
||||||
//flat buffer
|
|
||||||
val buffer = with(bufferAlgebra){
|
|
||||||
buffer(8) { Complex(it, -it) }.map { Complex(it.im, it.re) }
|
|
||||||
}
|
|
||||||
println(buffer)
|
|
||||||
|
|
||||||
// 2d element
|
|
||||||
val element: BufferND<Complex> = ndAlgebra.structureND(2, 2) { (i, j) ->
|
|
||||||
Complex(i - j, i + j)
|
|
||||||
}
|
|
||||||
println(element)
|
|
||||||
|
|
||||||
// 1d element operation
|
|
||||||
val result: StructureND<Complex> = ndAlgebra{
|
|
||||||
val a = structureND(8) { (it) -> i * it - it.toDouble() }
|
|
||||||
val b = 3
|
|
||||||
val c = Complex(1.0, 1.0)
|
|
||||||
|
|
||||||
(a pow b) + c
|
|
||||||
}
|
|
||||||
println(result)
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.operations
|
|
||||||
|
|
||||||
import space.kscience.kmath.commons.linear.CMLinearSpace
|
|
||||||
import space.kscience.kmath.linear.matrix
|
|
||||||
import space.kscience.kmath.nd.DoubleBufferND
|
|
||||||
import space.kscience.kmath.nd.Shape
|
|
||||||
import space.kscience.kmath.nd.Structure2D
|
|
||||||
import space.kscience.kmath.nd.ndAlgebra
|
|
||||||
import space.kscience.kmath.viktor.ViktorStructureND
|
|
||||||
import space.kscience.kmath.viktor.viktorAlgebra
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val viktorStructure: ViktorStructureND = DoubleField.viktorAlgebra.structureND(Shape(2, 2)) { (i, j) ->
|
|
||||||
if (i == j) 2.0 else 0.0
|
|
||||||
}
|
|
||||||
|
|
||||||
val cmMatrix: Structure2D<Double> = CMLinearSpace.matrix(2, 2)(0.0, 1.0, 0.0, 3.0)
|
|
||||||
|
|
||||||
val res: DoubleBufferND = DoubleField.ndAlgebra {
|
|
||||||
exp(viktorStructure) + 2.0 * cmMatrix
|
|
||||||
}
|
|
||||||
|
|
||||||
println(res)
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.stat
|
|
||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler
|
|
||||||
import org.apache.commons.rng.simple.RandomSource
|
|
||||||
import space.kscience.kmath.samplers.GaussianSampler
|
|
||||||
import java.time.Duration
|
|
||||||
import java.time.Instant
|
|
||||||
import org.apache.commons.rng.sampling.distribution.GaussianSampler as CMGaussianSampler
|
|
||||||
|
|
||||||
private suspend fun runKMathChained(): Duration {
|
|
||||||
val generator = RandomGenerator.fromSource(RandomSource.MT, 123L)
|
|
||||||
val normal = GaussianSampler(7.0, 2.0)
|
|
||||||
val chain = normal.sample(generator)
|
|
||||||
val startTime = Instant.now()
|
|
||||||
var sum = 0.0
|
|
||||||
|
|
||||||
repeat(10000001) { counter ->
|
|
||||||
sum += chain.next()
|
|
||||||
|
|
||||||
if (counter % 100000 == 0) {
|
|
||||||
val duration = Duration.between(startTime, Instant.now())
|
|
||||||
val meanValue = sum / counter
|
|
||||||
println("Chain sampler completed $counter elements in $duration: $meanValue")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Duration.between(startTime, Instant.now())
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun runApacheDirect(): Duration {
|
|
||||||
val rng = RandomSource.create(RandomSource.MT, 123L)
|
|
||||||
|
|
||||||
val sampler = CMGaussianSampler.of(
|
|
||||||
BoxMullerNormalizedGaussianSampler.of(rng),
|
|
||||||
7.0,
|
|
||||||
2.0
|
|
||||||
)
|
|
||||||
|
|
||||||
val startTime = Instant.now()
|
|
||||||
var sum = 0.0
|
|
||||||
|
|
||||||
repeat(10000001) { counter ->
|
|
||||||
sum += sampler.sample()
|
|
||||||
|
|
||||||
if (counter % 100000 == 0) {
|
|
||||||
val duration = Duration.between(startTime, Instant.now())
|
|
||||||
val meanValue = sum / counter
|
|
||||||
println("Direct sampler completed $counter elements in $duration: $meanValue")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Duration.between(startTime, Instant.now())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Comparing chain sampling performance with direct sampling performance
|
|
||||||
*/
|
|
||||||
fun main(): Unit = runBlocking(Dispatchers.Default) {
|
|
||||||
val directJob = async { runApacheDirect() }
|
|
||||||
val chainJob = async { runKMathChained() }
|
|
||||||
println("KMath Chained: ${chainJob.await()}")
|
|
||||||
println("Apache Direct: ${directJob.await()}")
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.stat
|
|
||||||
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import space.kscience.kmath.chains.Chain
|
|
||||||
import space.kscience.kmath.chains.combineWithState
|
|
||||||
import space.kscience.kmath.distributions.NormalDistribution
|
|
||||||
|
|
||||||
private data class AveragingChainState(var num: Int = 0, var value: Double = 0.0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Averaging.
|
|
||||||
*/
|
|
||||||
private fun Chain<Double>.mean(): Chain<Double> = combineWithState(AveragingChainState(), { it.copy() }) { chain ->
|
|
||||||
val next = chain.next()
|
|
||||||
num++
|
|
||||||
value += next
|
|
||||||
return@combineWithState value / num
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val normal = NormalDistribution(0.0, 2.0)
|
|
||||||
val chain = normal.sample(RandomGenerator.default).mean()
|
|
||||||
|
|
||||||
runBlocking {
|
|
||||||
repeat(10001) { counter ->
|
|
||||||
val mean = chain.next()
|
|
||||||
if (counter % 1000 == 0) {
|
|
||||||
println("[$counter] Average value is $mean")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@file:Suppress("unused")
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
import space.kscience.kmath.complex.*
|
|
||||||
import space.kscience.kmath.linear.transpose
|
|
||||||
import space.kscience.kmath.nd.StructureND
|
|
||||||
import space.kscience.kmath.nd.as2D
|
|
||||||
import space.kscience.kmath.nd.ndAlgebra
|
|
||||||
import space.kscience.kmath.nd.structureND
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val dim = 1000
|
|
||||||
val n = 1000
|
|
||||||
|
|
||||||
val realField = DoubleField.ndAlgebra(dim, dim)
|
|
||||||
val complexField: ComplexFieldND = ComplexField.ndAlgebra(dim, dim)
|
|
||||||
|
|
||||||
val realTime = measureTimeMillis {
|
|
||||||
realField {
|
|
||||||
var res: StructureND<Double> = one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Real addition completed in $realTime millis")
|
|
||||||
|
|
||||||
val complexTime = measureTimeMillis {
|
|
||||||
complexField {
|
|
||||||
var res: StructureND<Complex> = one
|
|
||||||
repeat(n) {
|
|
||||||
res += 1.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Complex addition completed in $complexTime millis")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun complexExample() {
|
|
||||||
//Create a context for 2-d structure with complex values
|
|
||||||
ComplexField {
|
|
||||||
withNdAlgebra(4, 8) {
|
|
||||||
//a constant real-valued structure
|
|
||||||
val x = one * 2.5
|
|
||||||
operator fun Number.plus(other: Complex) = Complex(this.toDouble() + other.re, other.im)
|
|
||||||
//a structure generator specific to this context
|
|
||||||
val matrix = structureND { (k, l) -> k + l * i }
|
|
||||||
//Perform sum
|
|
||||||
val sum = matrix + x + 1.0
|
|
||||||
|
|
||||||
//Represent the sum as 2d-structure and transpose
|
|
||||||
sum.as2D().transpose()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,101 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import org.nd4j.linalg.factory.Nd4j
|
|
||||||
import space.kscience.kmath.nd.*
|
|
||||||
import space.kscience.kmath.nd4j.nd4j
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import space.kscience.kmath.viktor.ViktorFieldND
|
|
||||||
import kotlin.contracts.InvocationKind
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
internal inline fun measureAndPrint(title: String, block: () -> Unit) {
|
|
||||||
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
|
|
||||||
val time = measureTimeMillis(block)
|
|
||||||
println("$title completed in $time millis")
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(DelicateCoroutinesApi::class)
|
|
||||||
fun main() {
|
|
||||||
// initializing Nd4j
|
|
||||||
Nd4j.zeros(0)
|
|
||||||
val dim = 1000
|
|
||||||
val n = 1000
|
|
||||||
val shape = Shape(dim, dim)
|
|
||||||
|
|
||||||
|
|
||||||
// automatically build context most suited for given type.
|
|
||||||
val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
|
|
||||||
// specialized nd-field for Double. It works as generic Double field as well.
|
|
||||||
val realField = DoubleField.ndAlgebra
|
|
||||||
//A generic boxing field. It should be used for objects, not primitives.
|
|
||||||
val boxingField = BufferedFieldOpsND(DoubleField, Buffer.Companion::boxing)
|
|
||||||
// Nd4j specialized field.
|
|
||||||
val nd4jField = DoubleField.nd4j
|
|
||||||
//viktor field
|
|
||||||
val viktorField = ViktorFieldND(dim, dim)
|
|
||||||
//parallel processing based on Java Streams
|
|
||||||
val parallelField = DoubleField.ndStreaming(dim, dim)
|
|
||||||
|
|
||||||
measureAndPrint("Boxing addition") {
|
|
||||||
boxingField {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
measureAndPrint("Specialized addition") {
|
|
||||||
realField {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
measureAndPrint("Nd4j specialized addition") {
|
|
||||||
nd4jField {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
measureAndPrint("Viktor addition") {
|
|
||||||
viktorField {
|
|
||||||
var res: StructureND<Double> = one
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
measureAndPrint("Parallel stream addition") {
|
|
||||||
parallelField {
|
|
||||||
var res: StructureND<Double> = one
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
measureAndPrint("Automatic field addition") {
|
|
||||||
autoField {
|
|
||||||
var res: StructureND<Double> = one(shape)
|
|
||||||
repeat(n) { res += 1.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
measureAndPrint("Lazy addition") {
|
|
||||||
val res = realField.one(shape).mapAsync(GlobalScope) {
|
|
||||||
var c = 0.0
|
|
||||||
repeat(n) {
|
|
||||||
c += 1.0
|
|
||||||
}
|
|
||||||
c
|
|
||||||
}
|
|
||||||
|
|
||||||
res.elements().forEach { it.second }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,108 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
import space.kscience.kmath.nd.*
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.ExtendedField
|
|
||||||
import space.kscience.kmath.operations.NumbersAddOps
|
|
||||||
import java.util.*
|
|
||||||
import java.util.stream.IntStream
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A demonstration implementation of NDField over Real using Java [java.util.stream.DoubleStream] for parallel
|
|
||||||
* execution.
|
|
||||||
*/
|
|
||||||
class StreamDoubleFieldND(override val shape: IntArray) : FieldND<Double, DoubleField>,
|
|
||||||
NumbersAddOps<StructureND<Double>>,
|
|
||||||
ExtendedField<StructureND<Double>> {
|
|
||||||
|
|
||||||
private val strides = DefaultStrides(shape)
|
|
||||||
override val elementAlgebra: DoubleField get() = DoubleField
|
|
||||||
override val zero: BufferND<Double> by lazy { structureND(shape) { zero } }
|
|
||||||
override val one: BufferND<Double> by lazy { structureND(shape) { one } }
|
|
||||||
|
|
||||||
override fun number(value: Number): BufferND<Double> {
|
|
||||||
val d = value.toDouble() // minimize conversions
|
|
||||||
return structureND(shape) { d }
|
|
||||||
}
|
|
||||||
|
|
||||||
private val StructureND<Double>.buffer: DoubleBuffer
|
|
||||||
get() = when {
|
|
||||||
!shape.contentEquals(this@StreamDoubleFieldND.shape) -> throw ShapeMismatchException(
|
|
||||||
this@StreamDoubleFieldND.shape,
|
|
||||||
shape
|
|
||||||
)
|
|
||||||
this is BufferND && this.indices == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer
|
|
||||||
else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun structureND(shape: Shape, initializer: DoubleField.(IntArray) -> Double): BufferND<Double> {
|
|
||||||
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<Double>.map(
|
|
||||||
transform: DoubleField.(Double) -> Double,
|
|
||||||
): BufferND<Double> {
|
|
||||||
val array = Arrays.stream(buffer.array).parallel().map { DoubleField.transform(it) }.toArray()
|
|
||||||
return BufferND(strides, array.asBuffer())
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun StructureND<Double>.mapIndexed(
|
|
||||||
transform: DoubleField.(index: IntArray, Double) -> Double,
|
|
||||||
): BufferND<Double> {
|
|
||||||
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 zip(
|
|
||||||
left: StructureND<Double>,
|
|
||||||
right: StructureND<Double>,
|
|
||||||
transform: DoubleField.(Double, Double) -> Double,
|
|
||||||
): BufferND<Double> {
|
|
||||||
val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
|
|
||||||
DoubleField.transform(left.buffer.array[offset], right.buffer.array[offset])
|
|
||||||
}.toArray()
|
|
||||||
return BufferND(strides, array.asBuffer())
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun StructureND<Double>.unaryMinus(): StructureND<Double> = map { -it }
|
|
||||||
|
|
||||||
override fun scale(a: StructureND<Double>, value: Double): StructureND<Double> = a.map { it * value }
|
|
||||||
|
|
||||||
override fun power(arg: StructureND<Double>, pow: Number): BufferND<Double> = arg.map { power(it, pow) }
|
|
||||||
|
|
||||||
override fun exp(arg: StructureND<Double>): BufferND<Double> = arg.map { exp(it) }
|
|
||||||
|
|
||||||
override fun ln(arg: StructureND<Double>): BufferND<Double> = arg.map { ln(it) }
|
|
||||||
|
|
||||||
override fun sin(arg: StructureND<Double>): BufferND<Double> = arg.map { sin(it) }
|
|
||||||
override fun cos(arg: StructureND<Double>): BufferND<Double> = arg.map { cos(it) }
|
|
||||||
override fun tan(arg: StructureND<Double>): BufferND<Double> = arg.map { tan(it) }
|
|
||||||
override fun asin(arg: StructureND<Double>): BufferND<Double> = arg.map { asin(it) }
|
|
||||||
override fun acos(arg: StructureND<Double>): BufferND<Double> = arg.map { acos(it) }
|
|
||||||
override fun atan(arg: StructureND<Double>): BufferND<Double> = arg.map { atan(it) }
|
|
||||||
|
|
||||||
override fun sinh(arg: StructureND<Double>): BufferND<Double> = arg.map { sinh(it) }
|
|
||||||
override fun cosh(arg: StructureND<Double>): BufferND<Double> = arg.map { cosh(it) }
|
|
||||||
override fun tanh(arg: StructureND<Double>): BufferND<Double> = arg.map { tanh(it) }
|
|
||||||
override fun asinh(arg: StructureND<Double>): BufferND<Double> = arg.map { asinh(it) }
|
|
||||||
override fun acosh(arg: StructureND<Double>): BufferND<Double> = arg.map { acosh(it) }
|
|
||||||
override fun atanh(arg: StructureND<Double>): BufferND<Double> = arg.map { atanh(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun DoubleField.ndStreaming(vararg shape: Int): StreamDoubleFieldND = StreamDoubleFieldND(shape)
|
|
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
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 = DoubleBuffer(array)
|
|
||||||
val strides = DefaultStrides(intArrayOf(n, n))
|
|
||||||
val structure = BufferND(strides, buffer)
|
|
||||||
|
|
||||||
measureTimeMillis {
|
|
||||||
var res = 0.0
|
|
||||||
strides.asSequence().forEach { res = structure[it] }
|
|
||||||
} // warmup
|
|
||||||
|
|
||||||
val time1 = measureTimeMillis {
|
|
||||||
var res = 0.0
|
|
||||||
strides.asSequence().forEach { res = structure[it] }
|
|
||||||
}
|
|
||||||
println("Structure reading finished in $time1 millis")
|
|
||||||
|
|
||||||
val time2 = measureTimeMillis {
|
|
||||||
var res = 0.0
|
|
||||||
strides.asSequence().forEach { res = buffer[strides.offset(it)] }
|
|
||||||
}
|
|
||||||
println("Buffer reading finished in $time2 millis")
|
|
||||||
|
|
||||||
val time3 = measureTimeMillis {
|
|
||||||
var res = 0.0
|
|
||||||
strides.asSequence().forEach { res = array[strides.offset(it)] }
|
|
||||||
}
|
|
||||||
println("Array reading finished in $time3 millis")
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
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 = 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")
|
|
||||||
val array = DoubleArray(n * n) { 1.0 }
|
|
||||||
|
|
||||||
val time2 = measureTimeMillis {
|
|
||||||
val target = DoubleArray(n * n)
|
|
||||||
val res = array.forEachIndexed { index, value -> target[index] = value + 1 }
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Array mapping finished in $time2 millis")
|
|
||||||
|
|
||||||
val buffer = DoubleBuffer(DoubleArray(n * n) { 1.0 })
|
|
||||||
|
|
||||||
val time3 = measureTimeMillis {
|
|
||||||
val target = DoubleBuffer(DoubleArray(n * n))
|
|
||||||
val res = array.forEachIndexed { index, value ->
|
|
||||||
target[index] = value + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println("Buffer mapping finished in $time3 millis")
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.buffer
|
|
||||||
import space.kscience.kmath.operations.bufferAlgebra
|
|
||||||
import space.kscience.kmath.operations.withSize
|
|
||||||
|
|
||||||
inline fun <reified R : Any> MutableBuffer.Companion.same(
|
|
||||||
n: Int,
|
|
||||||
value: R
|
|
||||||
): MutableBuffer<R> = auto(n) { value }
|
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
with(DoubleField.bufferAlgebra.withSize(5)) {
|
|
||||||
println(number(2.0) + buffer(1, 2, 3, 4, 5))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.structures
|
|
||||||
|
|
||||||
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<Double, *>.simple() {
|
|
||||||
val m1 = produce<D2, D3> { i, j -> (i + j).toDouble() }
|
|
||||||
val m2 = produce<D3, D2> { i, j -> (i + j).toDouble() }
|
|
||||||
|
|
||||||
//Dimension-safe addition
|
|
||||||
m1.transpose() + m2
|
|
||||||
}
|
|
||||||
|
|
||||||
private object D5 : Dimension {
|
|
||||||
override val dim: Int = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun DMatrixContext<Double, *>.custom() {
|
|
||||||
val m1 = produce<D2, D5> { i, j -> (i + j).toDouble() }
|
|
||||||
val m2 = produce<D5, D2> { i, j -> (i - j).toDouble() }
|
|
||||||
val m3 = produce<D2, D2> { i, j -> (i - j).toDouble() }
|
|
||||||
(m1 dot m2) + m3
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main(): Unit = with(DMatrixContext.real) {
|
|
||||||
simple()
|
|
||||||
custom()
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.tensors
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensor
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensorAlgebra
|
|
||||||
|
|
||||||
import kotlin.math.abs
|
|
||||||
|
|
||||||
// OLS estimator using SVD
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
//seed for random
|
|
||||||
val randSeed = 100500L
|
|
||||||
|
|
||||||
// work in context with linear operations
|
|
||||||
DoubleTensorAlgebra {
|
|
||||||
// take coefficient vector from normal distribution
|
|
||||||
val alpha = randomNormal(
|
|
||||||
intArrayOf(5),
|
|
||||||
randSeed
|
|
||||||
) + fromArray(
|
|
||||||
intArrayOf(5),
|
|
||||||
doubleArrayOf(1.0, 2.5, 3.4, 5.0, 10.1)
|
|
||||||
)
|
|
||||||
|
|
||||||
println("Real alpha:\n$alpha")
|
|
||||||
|
|
||||||
// also take sample of size 20 from normal distribution for x
|
|
||||||
val x = randomNormal(
|
|
||||||
intArrayOf(20, 5),
|
|
||||||
randSeed
|
|
||||||
)
|
|
||||||
|
|
||||||
// calculate y and add gaussian noise (N(0, 0.05))
|
|
||||||
val y = x dot alpha
|
|
||||||
y += y.randomNormalLike(randSeed) * 0.05
|
|
||||||
|
|
||||||
// now restore the coefficient vector with OSL estimator with SVD
|
|
||||||
val (u, singValues, v) = x.svd()
|
|
||||||
|
|
||||||
// we have to make sure the singular values of the matrix are not close to zero
|
|
||||||
println("Singular values:\n$singValues")
|
|
||||||
|
|
||||||
|
|
||||||
// inverse Sigma matrix can be restored from singular values with diagonalEmbedding function
|
|
||||||
val sigma = diagonalEmbedding(singValues.map{ if (abs(it) < 1e-3) 0.0 else 1.0/it })
|
|
||||||
|
|
||||||
val alphaOLS = v dot sigma dot u.transpose() dot y
|
|
||||||
println("Estimated alpha:\n" +
|
|
||||||
"$alphaOLS")
|
|
||||||
|
|
||||||
// figure out MSE of approximation
|
|
||||||
fun mse(yTrue: DoubleTensor, yPred: DoubleTensor): Double {
|
|
||||||
require(yTrue.shape.size == 1)
|
|
||||||
require(yTrue.shape contentEquals yPred.shape)
|
|
||||||
|
|
||||||
val diff = yTrue - yPred
|
|
||||||
return diff.dot(diff).sqrt().value()
|
|
||||||
}
|
|
||||||
|
|
||||||
println("MSE: ${mse(alpha, alphaOLS)}")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.tensors
|
|
||||||
|
|
||||||
import space.kscience.kmath.tensors.core.tensorAlgebra
|
|
||||||
import space.kscience.kmath.tensors.core.withBroadcast
|
|
||||||
|
|
||||||
|
|
||||||
// simple PCA
|
|
||||||
|
|
||||||
fun main(): Unit = Double.tensorAlgebra.withBroadcast { // work in context with broadcast methods
|
|
||||||
val seed = 100500L
|
|
||||||
|
|
||||||
// assume x is range from 0 until 10
|
|
||||||
val x = fromArray(
|
|
||||||
intArrayOf(10),
|
|
||||||
DoubleArray(10) { it.toDouble() }
|
|
||||||
)
|
|
||||||
|
|
||||||
// take y dependent on x with noise
|
|
||||||
val y = 2.0 * x + (3.0 + x.randomNormalLike(seed) * 1.5)
|
|
||||||
|
|
||||||
println("x:\n$x")
|
|
||||||
println("y:\n$y")
|
|
||||||
|
|
||||||
// stack them into single dataset
|
|
||||||
val dataset = stack(listOf(x, y)).transpose()
|
|
||||||
|
|
||||||
// normalize both x and y
|
|
||||||
val xMean = x.mean()
|
|
||||||
val yMean = y.mean()
|
|
||||||
|
|
||||||
val xStd = x.std()
|
|
||||||
val yStd = y.std()
|
|
||||||
|
|
||||||
val xScaled = (x - xMean) / xStd
|
|
||||||
val yScaled = (y - yMean) / yStd
|
|
||||||
|
|
||||||
// save means ans standard deviations for further recovery
|
|
||||||
val mean = fromArray(
|
|
||||||
intArrayOf(2),
|
|
||||||
doubleArrayOf(xMean, yMean)
|
|
||||||
)
|
|
||||||
println("Means:\n$mean")
|
|
||||||
|
|
||||||
val std = fromArray(
|
|
||||||
intArrayOf(2),
|
|
||||||
doubleArrayOf(xStd, yStd)
|
|
||||||
)
|
|
||||||
println("Standard deviations:\n$std")
|
|
||||||
|
|
||||||
// calculate the covariance matrix of scaled x and y
|
|
||||||
val covMatrix = cov(listOf(xScaled, yScaled))
|
|
||||||
println("Covariance matrix:\n$covMatrix")
|
|
||||||
|
|
||||||
// and find out eigenvector of it
|
|
||||||
val (_, evecs) = covMatrix.symEig()
|
|
||||||
val v = evecs[0]
|
|
||||||
println("Eigenvector:\n$v")
|
|
||||||
|
|
||||||
// reduce dimension of dataset
|
|
||||||
val datasetReduced = v dot stack(listOf(xScaled, yScaled))
|
|
||||||
println("Reduced data:\n$datasetReduced")
|
|
||||||
|
|
||||||
// we can restore original data from reduced data;
|
|
||||||
// for example, find 7th element of dataset.
|
|
||||||
val n = 7
|
|
||||||
val restored = (datasetReduced[n] dot v.view(intArrayOf(1, 2))) * std + mean
|
|
||||||
println("Original value:\n${dataset[n]}")
|
|
||||||
println("Restored value:\n$restored")
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.tensors
|
|
||||||
|
|
||||||
import space.kscience.kmath.tensors.core.tensorAlgebra
|
|
||||||
import space.kscience.kmath.tensors.core.withBroadcast
|
|
||||||
|
|
||||||
|
|
||||||
// Dataset normalization
|
|
||||||
|
|
||||||
fun main() = Double.tensorAlgebra.withBroadcast { // work in context with broadcast methods
|
|
||||||
// take dataset of 5-element vectors from normal distribution
|
|
||||||
val dataset = randomNormal(intArrayOf(100, 5)) * 1.5 // all elements from N(0, 1.5)
|
|
||||||
|
|
||||||
dataset += fromArray(
|
|
||||||
intArrayOf(5),
|
|
||||||
doubleArrayOf(0.0, 1.0, 1.5, 3.0, 5.0) // row means
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
// find out mean and standard deviation of each column
|
|
||||||
val mean = dataset.mean(0, false)
|
|
||||||
val std = dataset.std(0, false)
|
|
||||||
|
|
||||||
println("Mean:\n$mean")
|
|
||||||
println("Standard deviation:\n$std")
|
|
||||||
|
|
||||||
// also, we can calculate other statistic as minimum and maximum of rows
|
|
||||||
println("Minimum:\n${dataset.min(0, false)}")
|
|
||||||
println("Maximum:\n${dataset.max(0, false)}")
|
|
||||||
|
|
||||||
// now we can scale dataset with mean normalization
|
|
||||||
val datasetScaled = (dataset - mean) / std
|
|
||||||
|
|
||||||
// find out mean and std of scaled dataset
|
|
||||||
|
|
||||||
println("Mean of scaled:\n${datasetScaled.mean(0, false)}")
|
|
||||||
println("Mean of scaled:\n${datasetScaled.std(0, false)}")
|
|
||||||
}
|
|
@ -1,93 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.tensors
|
|
||||||
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensor
|
|
||||||
import space.kscience.kmath.tensors.core.tensorAlgebra
|
|
||||||
import space.kscience.kmath.tensors.core.withBroadcast
|
|
||||||
|
|
||||||
// solving linear system with LUP decomposition
|
|
||||||
|
|
||||||
fun main() = Double.tensorAlgebra.withBroadcast {// work in context with linear operations
|
|
||||||
|
|
||||||
// set true value of x
|
|
||||||
val trueX = fromArray(
|
|
||||||
intArrayOf(4),
|
|
||||||
doubleArrayOf(-2.0, 1.5, 6.8, -2.4)
|
|
||||||
)
|
|
||||||
|
|
||||||
// and A matrix
|
|
||||||
val a = fromArray(
|
|
||||||
intArrayOf(4, 4),
|
|
||||||
doubleArrayOf(
|
|
||||||
0.5, 10.5, 4.5, 1.0,
|
|
||||||
8.5, 0.9, 12.8, 0.1,
|
|
||||||
5.56, 9.19, 7.62, 5.45,
|
|
||||||
1.0, 2.0, -3.0, -2.5
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
// calculate y value
|
|
||||||
val b = a dot trueX
|
|
||||||
|
|
||||||
// check out A and b
|
|
||||||
println("A:\n$a")
|
|
||||||
println("b:\n$b")
|
|
||||||
|
|
||||||
// solve `Ax = b` system using LUP decomposition
|
|
||||||
|
|
||||||
// get P, L, U such that PA = LU
|
|
||||||
val (p, l, u) = a.lu()
|
|
||||||
|
|
||||||
// check P is permutation matrix
|
|
||||||
println("P:\n$p")
|
|
||||||
// L is lower triangular matrix and U is upper triangular matrix
|
|
||||||
println("L:\n$l")
|
|
||||||
println("U:\n$u")
|
|
||||||
// and PA = LU
|
|
||||||
println("PA:\n${p dot a}")
|
|
||||||
println("LU:\n${l dot u}")
|
|
||||||
|
|
||||||
/* Ax = b;
|
|
||||||
PAx = Pb;
|
|
||||||
LUx = Pb;
|
|
||||||
let y = Ux, then
|
|
||||||
Ly = Pb -- this system can be easily solved, since the matrix L is lower triangular;
|
|
||||||
Ux = y can be solved the same way, since the matrix L is upper triangular
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// this function returns solution x of a system lx = b, l should be lower triangular
|
|
||||||
fun solveLT(l: DoubleTensor, b: DoubleTensor): DoubleTensor {
|
|
||||||
val n = l.shape[0]
|
|
||||||
val x = zeros(intArrayOf(n))
|
|
||||||
for (i in 0 until n) {
|
|
||||||
x[intArrayOf(i)] = (b[intArrayOf(i)] - l[i].dot(x).value()) / l[intArrayOf(i, i)]
|
|
||||||
}
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
|
|
||||||
val y = solveLT(l, p dot b)
|
|
||||||
|
|
||||||
// solveLT(l, b) function can be easily adapted for upper triangular matrix by the permutation matrix revMat
|
|
||||||
// create it by placing ones on side diagonal
|
|
||||||
val revMat = u.zeroesLike()
|
|
||||||
val n = revMat.shape[0]
|
|
||||||
for (i in 0 until n) {
|
|
||||||
revMat[intArrayOf(i, n - 1 - i)] = 1.0
|
|
||||||
}
|
|
||||||
|
|
||||||
// solution of system ux = b, u should be upper triangular
|
|
||||||
fun solveUT(u: DoubleTensor, b: DoubleTensor): DoubleTensor = revMat dot solveLT(
|
|
||||||
revMat dot u dot revMat, revMat dot b
|
|
||||||
)
|
|
||||||
|
|
||||||
val x = solveUT(u, y)
|
|
||||||
|
|
||||||
println("True x:\n$trueX")
|
|
||||||
println("x founded with LU method:\n$x")
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.tensors
|
|
||||||
|
|
||||||
import org.jetbrains.kotlinx.multik.api.Multik
|
|
||||||
import org.jetbrains.kotlinx.multik.api.ndarray
|
|
||||||
import space.kscience.kmath.multik.multikAlgebra
|
|
||||||
import space.kscience.kmath.nd.one
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
|
|
||||||
fun main(): Unit = with(DoubleField.multikAlgebra) {
|
|
||||||
val a = Multik.ndarray(intArrayOf(1, 2, 3)).asType<Double>().wrap()
|
|
||||||
val b = Multik.ndarray(doubleArrayOf(1.0, 2.0, 3.0)).wrap()
|
|
||||||
one(a.shape) - a + b * 3.0
|
|
||||||
}
|
|
@ -1,239 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.tensors
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import space.kscience.kmath.tensors.core.BroadcastDoubleTensorAlgebra
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensor
|
|
||||||
import space.kscience.kmath.tensors.core.DoubleTensorAlgebra
|
|
||||||
import space.kscience.kmath.tensors.core.copyArray
|
|
||||||
import kotlin.math.sqrt
|
|
||||||
|
|
||||||
const val seed = 100500L
|
|
||||||
|
|
||||||
// Simple feedforward neural network with backpropagation training
|
|
||||||
|
|
||||||
// interface of network layer
|
|
||||||
interface Layer {
|
|
||||||
fun forward(input: DoubleTensor): DoubleTensor
|
|
||||||
fun backward(input: DoubleTensor, outputError: DoubleTensor): DoubleTensor
|
|
||||||
}
|
|
||||||
|
|
||||||
// activation layer
|
|
||||||
open class Activation(
|
|
||||||
val activation: (DoubleTensor) -> DoubleTensor,
|
|
||||||
val activationDer: (DoubleTensor) -> DoubleTensor,
|
|
||||||
) : Layer {
|
|
||||||
override fun forward(input: DoubleTensor): DoubleTensor {
|
|
||||||
return activation(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun backward(input: DoubleTensor, outputError: DoubleTensor): DoubleTensor {
|
|
||||||
return DoubleTensorAlgebra { outputError * activationDer(input) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun relu(x: DoubleTensor): DoubleTensor = DoubleTensorAlgebra {
|
|
||||||
x.map { if (it > 0) it else 0.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun reluDer(x: DoubleTensor): DoubleTensor = DoubleTensorAlgebra {
|
|
||||||
x.map { if (it > 0) 1.0 else 0.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
// activation layer with relu activator
|
|
||||||
class ReLU : Activation(::relu, ::reluDer)
|
|
||||||
|
|
||||||
fun sigmoid(x: DoubleTensor): DoubleTensor = DoubleTensorAlgebra {
|
|
||||||
1.0 / (1.0 + (-x).exp())
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sigmoidDer(x: DoubleTensor): DoubleTensor = DoubleTensorAlgebra {
|
|
||||||
sigmoid(x) * (1.0 - sigmoid(x))
|
|
||||||
}
|
|
||||||
|
|
||||||
// activation layer with sigmoid activator
|
|
||||||
class Sigmoid : Activation(::sigmoid, ::sigmoidDer)
|
|
||||||
|
|
||||||
// dense layer
|
|
||||||
class Dense(
|
|
||||||
private val inputUnits: Int,
|
|
||||||
private val outputUnits: Int,
|
|
||||||
private val learningRate: Double = 0.1,
|
|
||||||
) : Layer {
|
|
||||||
|
|
||||||
private val weights: DoubleTensor = DoubleTensorAlgebra {
|
|
||||||
randomNormal(
|
|
||||||
intArrayOf(inputUnits, outputUnits),
|
|
||||||
seed
|
|
||||||
) * sqrt(2.0 / (inputUnits + outputUnits))
|
|
||||||
}
|
|
||||||
|
|
||||||
private val bias: DoubleTensor = DoubleTensorAlgebra { zeros(intArrayOf(outputUnits)) }
|
|
||||||
|
|
||||||
override fun forward(input: DoubleTensor): DoubleTensor = BroadcastDoubleTensorAlgebra {
|
|
||||||
(input dot weights) + bias
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun backward(input: DoubleTensor, outputError: DoubleTensor): DoubleTensor = DoubleTensorAlgebra {
|
|
||||||
val gradInput = outputError dot weights.transpose()
|
|
||||||
|
|
||||||
val gradW = input.transpose() dot outputError
|
|
||||||
val gradBias = outputError.mean(dim = 0, keepDim = false) * input.shape[0].toDouble()
|
|
||||||
|
|
||||||
weights -= learningRate * gradW
|
|
||||||
bias -= learningRate * gradBias
|
|
||||||
|
|
||||||
gradInput
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// simple accuracy equal to the proportion of correct answers
|
|
||||||
fun accuracy(yPred: DoubleTensor, yTrue: DoubleTensor): Double {
|
|
||||||
check(yPred.shape contentEquals yTrue.shape)
|
|
||||||
val n = yPred.shape[0]
|
|
||||||
var correctCnt = 0
|
|
||||||
for (i in 0 until n) {
|
|
||||||
if (yPred[intArrayOf(i, 0)] == yTrue[intArrayOf(i, 0)]) {
|
|
||||||
correctCnt += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return correctCnt.toDouble() / n.toDouble()
|
|
||||||
}
|
|
||||||
|
|
||||||
// neural network class
|
|
||||||
@OptIn(ExperimentalStdlibApi::class)
|
|
||||||
class NeuralNetwork(private val layers: List<Layer>) {
|
|
||||||
private fun softMaxLoss(yPred: DoubleTensor, yTrue: DoubleTensor): DoubleTensor = BroadcastDoubleTensorAlgebra {
|
|
||||||
|
|
||||||
val onesForAnswers = yPred.zeroesLike()
|
|
||||||
yTrue.copyArray().forEachIndexed { index, labelDouble ->
|
|
||||||
val label = labelDouble.toInt()
|
|
||||||
onesForAnswers[intArrayOf(index, label)] = 1.0
|
|
||||||
}
|
|
||||||
|
|
||||||
val softmaxValue = yPred.exp() / yPred.exp().sum(dim = 1, keepDim = true)
|
|
||||||
|
|
||||||
(-onesForAnswers + softmaxValue) / (yPred.shape[0].toDouble())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun forward(x: DoubleTensor): List<DoubleTensor> {
|
|
||||||
var input = x
|
|
||||||
|
|
||||||
return buildList {
|
|
||||||
layers.forEach { layer ->
|
|
||||||
val output = layer.forward(input)
|
|
||||||
add(output)
|
|
||||||
input = output
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun train(xTrain: DoubleTensor, yTrain: DoubleTensor) {
|
|
||||||
val layerInputs = buildList {
|
|
||||||
add(xTrain)
|
|
||||||
addAll(forward(xTrain))
|
|
||||||
}
|
|
||||||
|
|
||||||
var lossGrad = softMaxLoss(layerInputs.last(), yTrain)
|
|
||||||
|
|
||||||
layers.zip(layerInputs).reversed().forEach { (layer, input) ->
|
|
||||||
lossGrad = layer.backward(input, lossGrad)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun fit(xTrain: DoubleTensor, yTrain: DoubleTensor, batchSize: Int, epochs: Int) = DoubleTensorAlgebra {
|
|
||||||
fun iterBatch(x: DoubleTensor, y: DoubleTensor): Sequence<Pair<DoubleTensor, DoubleTensor>> = sequence {
|
|
||||||
val n = x.shape[0]
|
|
||||||
val shuffledIndices = (0 until n).shuffled()
|
|
||||||
for (i in 0 until n step batchSize) {
|
|
||||||
val excerptIndices = shuffledIndices.drop(i).take(batchSize).toIntArray()
|
|
||||||
val batch = x.rowsByIndices(excerptIndices) to y.rowsByIndices(excerptIndices)
|
|
||||||
yield(batch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (epoch in 0 until epochs) {
|
|
||||||
println("Epoch ${epoch + 1}/$epochs")
|
|
||||||
for ((xBatch, yBatch) in iterBatch(xTrain, yTrain)) {
|
|
||||||
train(xBatch, yBatch)
|
|
||||||
}
|
|
||||||
println("Accuracy:${accuracy(yTrain, predict(xTrain).argMax(1, true).asDouble())}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun predict(x: DoubleTensor): DoubleTensor {
|
|
||||||
return forward(x).last()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalStdlibApi::class)
|
|
||||||
fun main() = BroadcastDoubleTensorAlgebra {
|
|
||||||
val features = 5
|
|
||||||
val sampleSize = 250
|
|
||||||
val trainSize = 180
|
|
||||||
//val testSize = sampleSize - trainSize
|
|
||||||
|
|
||||||
// take sample of features from normal distribution
|
|
||||||
val x = randomNormal(intArrayOf(sampleSize, features), seed) * 2.5
|
|
||||||
|
|
||||||
x += fromArray(
|
|
||||||
intArrayOf(5),
|
|
||||||
doubleArrayOf(0.0, -1.0, -2.5, -3.0, 5.5) // row means
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
// define class like '1' if the sum of features > 0 and '0' otherwise
|
|
||||||
val y = fromArray(
|
|
||||||
intArrayOf(sampleSize, 1),
|
|
||||||
DoubleArray(sampleSize) { i ->
|
|
||||||
if (x[i].sum() > 0.0) {
|
|
||||||
1.0
|
|
||||||
} else {
|
|
||||||
0.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// split train ans test
|
|
||||||
val trainIndices = (0 until trainSize).toList().toIntArray()
|
|
||||||
val testIndices = (trainSize until sampleSize).toList().toIntArray()
|
|
||||||
|
|
||||||
val xTrain = x.rowsByIndices(trainIndices)
|
|
||||||
val yTrain = y.rowsByIndices(trainIndices)
|
|
||||||
|
|
||||||
val xTest = x.rowsByIndices(testIndices)
|
|
||||||
val yTest = y.rowsByIndices(testIndices)
|
|
||||||
|
|
||||||
// build model
|
|
||||||
val layers = buildList {
|
|
||||||
add(Dense(features, 64))
|
|
||||||
add(ReLU())
|
|
||||||
add(Dense(64, 16))
|
|
||||||
add(ReLU())
|
|
||||||
add(Dense(16, 2))
|
|
||||||
add(Sigmoid())
|
|
||||||
}
|
|
||||||
val model = NeuralNetwork(layers)
|
|
||||||
|
|
||||||
// fit it with train data
|
|
||||||
model.fit(xTrain, yTrain, batchSize = 20, epochs = 10)
|
|
||||||
|
|
||||||
// make prediction
|
|
||||||
val prediction = model.predict(xTest)
|
|
||||||
|
|
||||||
// process raw prediction via argMax
|
|
||||||
val predictionLabels = prediction.argMax(1, true).asDouble()
|
|
||||||
|
|
||||||
// find out accuracy
|
|
||||||
val acc = accuracy(yTest, predictionLabels)
|
|
||||||
println("Test accuracy:$acc")
|
|
||||||
|
|
||||||
}
|
|
@ -1,279 +0,0 @@
|
|||||||
# Module kmath-ast
|
|
||||||
|
|
||||||
Extensions to MST API: transformations, dynamic compilation and visualization.
|
|
||||||
|
|
||||||
- [expression-language](src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt) : Expression language and its parser
|
|
||||||
- [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
|
|
||||||
- [rendering](src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt) : Extendable MST rendering
|
|
||||||
|
|
||||||
|
|
||||||
## Artifact:
|
|
||||||
|
|
||||||
The Maven coordinates of this project are `space.kscience:kmath-ast:0.3.0-dev-20`.
|
|
||||||
|
|
||||||
**Gradle Groovy:**
|
|
||||||
```groovy
|
|
||||||
repositories {
|
|
||||||
maven { url 'https://repo.kotlin.link' }
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation 'space.kscience:kmath-ast:0.3.0-dev-20'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
**Gradle Kotlin DSL:**
|
|
||||||
```kotlin
|
|
||||||
repositories {
|
|
||||||
maven("https://repo.kotlin.link")
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation("space.kscience:kmath-ast:0.3.0-dev-20")
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Parsing expressions
|
|
||||||
|
|
||||||
In this module there is a parser from human-readable strings like `"x^3-x+3"` (in the more specific [grammar](reference/ArithmeticsEvaluator.g4)) to MST instances.
|
|
||||||
|
|
||||||
Supported literals:
|
|
||||||
1. Constants and variables (consist of latin letters, digits and underscores, can't start with digit): `x`, `_Abc2`.
|
|
||||||
2. Numbers: `123`, `1.02`, `1e10`, `1e-10`, `1.0e+3`—all parsed either as `kotlin.Long` or `kotlin.Double`.
|
|
||||||
|
|
||||||
Supported binary operators (from the highest precedence to the lowest one):
|
|
||||||
1. `^`
|
|
||||||
2. `*`, `/`
|
|
||||||
3. `+`, `-`
|
|
||||||
|
|
||||||
Supported unary operator:
|
|
||||||
1. `-`, e. g. `-x`
|
|
||||||
|
|
||||||
Arbitrary unary and binary functions are also supported: names consist of latin letters, digits and underscores, can't start with digit. Examples:
|
|
||||||
1. `sin(x)`
|
|
||||||
2. `add(x, y)`
|
|
||||||
|
|
||||||
## Dynamic expression code generation
|
|
||||||
|
|
||||||
### On JVM
|
|
||||||
|
|
||||||
`kmath-ast` JVM module supports runtime code generation to eliminate overhead of tree traversal. Code generator builds a
|
|
||||||
special implementation of `Expression<T>` with implemented `invoke` function.
|
|
||||||
|
|
||||||
For example, the following code:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.asm.compileToExpression
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
|
|
||||||
"x^3-x+3".parseMath().compileToExpression(DoubleField)
|
|
||||||
```
|
|
||||||
|
|
||||||
… leads to generation of bytecode, which can be decompiled to the following Java class:
|
|
||||||
|
|
||||||
```java
|
|
||||||
import java.util.*;
|
|
||||||
import kotlin.jvm.functions.*;
|
|
||||||
import space.kscience.kmath.asm.internal.*;
|
|
||||||
import space.kscience.kmath.complex.*;
|
|
||||||
import space.kscience.kmath.expressions.*;
|
|
||||||
|
|
||||||
public final class CompiledExpression_45045_0 implements Expression<Complex> {
|
|
||||||
private final Object[] constants;
|
|
||||||
|
|
||||||
public Complex invoke(Map<Symbol, ? extends Complex> arguments) {
|
|
||||||
Complex var2 = (Complex)MapIntrinsics.getOrFail(arguments, "x");
|
|
||||||
return (Complex)((Function2)this.constants[0]).invoke(var2, (Complex)this.constants[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
For `LongRing`, `IntRing`, and `DoubleField` specialization is supported for better performance:
|
|
||||||
|
|
||||||
```java
|
|
||||||
import java.util.*;
|
|
||||||
import space.kscience.kmath.asm.internal.*;
|
|
||||||
import space.kscience.kmath.expressions.*;
|
|
||||||
|
|
||||||
public final class CompiledExpression_-386104628_0 implements DoubleExpression {
|
|
||||||
private final SymbolIndexer indexer;
|
|
||||||
|
|
||||||
public SymbolIndexer getIndexer() {
|
|
||||||
return this.indexer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double invoke(double[] arguments) {
|
|
||||||
double var2 = arguments[0];
|
|
||||||
return Math.pow(var2, 3.0D) - var2 + 3.0D;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final Double invoke(Map<Symbol, ? extends Double> arguments) {
|
|
||||||
double var2 = ((Double)MapIntrinsics.getOrFail(arguments, "x")).doubleValue();
|
|
||||||
return Math.pow(var2, 3.0D) - var2 + 3.0D;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Setting JVM system property `space.kscience.kmath.ast.dump.generated.classes` to `1` makes the translator dump class files to program's working directory, so they can be reviewed manually.
|
|
||||||
|
|
||||||
#### Limitations
|
|
||||||
|
|
||||||
- The same classes may be generated and loaded twice, so it is recommended to cache compiled expressions to avoid class loading overhead.
|
|
||||||
- This API is not supported by non-dynamic JVM implementations like TeaVM or GraalVM Native Image because they may not support class loaders.
|
|
||||||
|
|
||||||
### On JS
|
|
||||||
|
|
||||||
A similar feature is also available on JS.
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.*
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
import space.kscience.kmath.estree.*
|
|
||||||
|
|
||||||
MstField { x + 2 }.compileToExpression(DoubleField)
|
|
||||||
```
|
|
||||||
|
|
||||||
The code above returns expression implemented with such a JS function:
|
|
||||||
|
|
||||||
```js
|
|
||||||
var executable = function (constants, arguments) {
|
|
||||||
return constants[1](constants[0](arguments, "x"), 2);
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
JS also supports experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation.
|
|
||||||
Currently, only expressions inside `DoubleField` and `IntRing` are supported.
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.*
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
import space.kscience.kmath.wasm.*
|
|
||||||
|
|
||||||
MstField { x + 2 }.compileToExpression(DoubleField)
|
|
||||||
```
|
|
||||||
|
|
||||||
An example of emitted Wasm IR in the form of WAT:
|
|
||||||
|
|
||||||
```lisp
|
|
||||||
(func \$executable (param \$0 f64) (result f64)
|
|
||||||
(f64.add
|
|
||||||
(local.get \$0)
|
|
||||||
(f64.const 2)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Limitations
|
|
||||||
|
|
||||||
- ESTree expression compilation uses `eval` which can be unavailable in several environments.
|
|
||||||
- WebAssembly isn't supported by old versions of browsers (see https://webassembly.org/roadmap/).
|
|
||||||
|
|
||||||
## Rendering expressions
|
|
||||||
|
|
||||||
kmath-ast also includes an extensible engine to display expressions in LaTeX or MathML syntax.
|
|
||||||
|
|
||||||
Example usage:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.ast.*
|
|
||||||
import space.kscience.kmath.ast.rendering.*
|
|
||||||
import space.kscience.kmath.misc.*
|
|
||||||
|
|
||||||
@OptIn(UnstableKMathAPI::class)
|
|
||||||
public fun main() {
|
|
||||||
val mst = "exp(sqrt(x))-asin(2*x)/(2e10+x^3)/(12)+x^(2/3)".parseMath()
|
|
||||||
val syntax = FeaturedMathRendererWithPostProcess.Default.render(mst)
|
|
||||||
val latex = LatexSyntaxRenderer.renderWithStringBuilder(syntax)
|
|
||||||
println("LaTeX:")
|
|
||||||
println(latex)
|
|
||||||
println()
|
|
||||||
val mathML = MathMLSyntaxRenderer.renderWithStringBuilder(syntax)
|
|
||||||
println("MathML:")
|
|
||||||
println(mathML)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Result LaTeX:
|
|
||||||
|
|
||||||
<div style="background-color:white;">
|
|
||||||
|
|
||||||
![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3})
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Result MathML (can be used with MathJax or other renderers):
|
|
||||||
|
|
||||||
<details>
|
|
||||||
|
|
||||||
```html
|
|
||||||
<math xmlns="https://www.w3.org/1998/Math/MathML">
|
|
||||||
<mrow>
|
|
||||||
<mo>exp</mo>
|
|
||||||
<mspace width="0.167em"></mspace>
|
|
||||||
<mfenced open="(" close=")" separators="">
|
|
||||||
<msqrt>
|
|
||||||
<mi>x</mi>
|
|
||||||
</msqrt>
|
|
||||||
</mfenced>
|
|
||||||
<mo>-</mo>
|
|
||||||
<mfrac>
|
|
||||||
<mrow>
|
|
||||||
<mfrac>
|
|
||||||
<mrow>
|
|
||||||
<mo>arcsin</mo>
|
|
||||||
<mspace width="0.167em"></mspace>
|
|
||||||
<mfenced open="(" close=")" separators="">
|
|
||||||
<mn>2</mn>
|
|
||||||
<mspace width="0.167em"></mspace>
|
|
||||||
<mi>x</mi>
|
|
||||||
</mfenced>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>2</mn>
|
|
||||||
<mo>×</mo>
|
|
||||||
<msup>
|
|
||||||
<mrow>
|
|
||||||
<mn>10</mn>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>10</mn>
|
|
||||||
</mrow>
|
|
||||||
</msup>
|
|
||||||
<mo>+</mo>
|
|
||||||
<msup>
|
|
||||||
<mrow>
|
|
||||||
<mi>x</mi>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>3</mn>
|
|
||||||
</mrow>
|
|
||||||
</msup>
|
|
||||||
</mrow>
|
|
||||||
</mfrac>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>12</mn>
|
|
||||||
</mrow>
|
|
||||||
</mfrac>
|
|
||||||
<mo>+</mo>
|
|
||||||
<msup>
|
|
||||||
<mrow>
|
|
||||||
<mi>x</mi>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>2</mn>
|
|
||||||
<mo>/</mo>
|
|
||||||
<mn>3</mn>
|
|
||||||
</mrow>
|
|
||||||
</msup>
|
|
||||||
</mrow>
|
|
||||||
</math>
|
|
||||||
```
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
It is also possible to create custom algorithms of render, and even add support of other markup languages
|
|
||||||
(see API reference).
|
|
@ -1,86 +0,0 @@
|
|||||||
plugins {
|
|
||||||
kotlin("multiplatform")
|
|
||||||
id("ru.mipt.npm.gradle.common")
|
|
||||||
}
|
|
||||||
|
|
||||||
kotlin.js {
|
|
||||||
nodejs {
|
|
||||||
testTask {
|
|
||||||
useMocha().timeout = "0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
browser {
|
|
||||||
testTask {
|
|
||||||
useMocha().timeout = "0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
kotlin.sourceSets {
|
|
||||||
filter { it.name.contains("test", true) }
|
|
||||||
.map(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet::languageSettings)
|
|
||||||
.forEach { it.optIn("space.kscience.kmath.misc.UnstableKMathAPI") }
|
|
||||||
|
|
||||||
commonMain {
|
|
||||||
dependencies {
|
|
||||||
api("com.github.h0tk3y.betterParse:better-parse:0.4.2")
|
|
||||||
api(project(":kmath-core"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
commonTest {
|
|
||||||
dependencies {
|
|
||||||
implementation(project(":kmath-complex"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jsMain {
|
|
||||||
dependencies {
|
|
||||||
implementation(npm("astring", "1.7.5"))
|
|
||||||
implementation(npm("binaryen", "101.0.0"))
|
|
||||||
implementation(npm("js-base64", "3.6.1"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jvmMain {
|
|
||||||
dependencies {
|
|
||||||
implementation("org.ow2.asm:asm-commons:9.2")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Workaround for https://github.com/Kotlin/dokka/issues/1455
|
|
||||||
tasks.dokkaHtml {
|
|
||||||
dependsOn(tasks.build)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (System.getProperty("space.kscience.kmath.ast.dump.generated.classes") == "1")
|
|
||||||
tasks.jvmTest {
|
|
||||||
jvmArgs = (jvmArgs ?: emptyList()) + listOf("-Dspace.kscience.kmath.ast.dump.generated.classes=1")
|
|
||||||
}
|
|
||||||
|
|
||||||
readme {
|
|
||||||
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
|
|
||||||
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
|
|
||||||
|
|
||||||
feature(
|
|
||||||
id = "expression-language",
|
|
||||||
ref = "src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt"
|
|
||||||
) { "Expression language and its parser" }
|
|
||||||
|
|
||||||
feature(
|
|
||||||
id = "mst-jvm-codegen",
|
|
||||||
ref = "src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt"
|
|
||||||
) { "Dynamic MST to JVM bytecode compiler" }
|
|
||||||
|
|
||||||
feature(
|
|
||||||
id = "mst-js-codegen",
|
|
||||||
ref = "src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt"
|
|
||||||
) { "Dynamic MST to JS compiler" }
|
|
||||||
|
|
||||||
feature(
|
|
||||||
id = "rendering",
|
|
||||||
ref = "src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt"
|
|
||||||
) { "Extendable MST rendering" }
|
|
||||||
}
|
|
@ -1,250 +0,0 @@
|
|||||||
# Module kmath-ast
|
|
||||||
|
|
||||||
Extensions to MST API: transformations, dynamic compilation and visualization.
|
|
||||||
|
|
||||||
${features}
|
|
||||||
|
|
||||||
${artifact}
|
|
||||||
|
|
||||||
## Parsing expressions
|
|
||||||
|
|
||||||
In this module there is a parser from human-readable strings like `"x^3-x+3"` (in the more specific [grammar](reference/ArithmeticsEvaluator.g4)) to MST instances.
|
|
||||||
|
|
||||||
Supported literals:
|
|
||||||
1. Constants and variables (consist of latin letters, digits and underscores, can't start with digit): `x`, `_Abc2`.
|
|
||||||
2. Numbers: `123`, `1.02`, `1e10`, `1e-10`, `1.0e+3`—all parsed either as `kotlin.Long` or `kotlin.Double`.
|
|
||||||
|
|
||||||
Supported binary operators (from the highest precedence to the lowest one):
|
|
||||||
1. `^`
|
|
||||||
2. `*`, `/`
|
|
||||||
3. `+`, `-`
|
|
||||||
|
|
||||||
Supported unary operator:
|
|
||||||
1. `-`, e. g. `-x`
|
|
||||||
|
|
||||||
Arbitrary unary and binary functions are also supported: names consist of latin letters, digits and underscores, can't start with digit. Examples:
|
|
||||||
1. `sin(x)`
|
|
||||||
2. `add(x, y)`
|
|
||||||
|
|
||||||
## Dynamic expression code generation
|
|
||||||
|
|
||||||
### On JVM
|
|
||||||
|
|
||||||
`kmath-ast` JVM module supports runtime code generation to eliminate overhead of tree traversal. Code generator builds a
|
|
||||||
special implementation of `Expression<T>` with implemented `invoke` function.
|
|
||||||
|
|
||||||
For example, the following code:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.asm.compileToExpression
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
|
|
||||||
"x^3-x+3".parseMath().compileToExpression(DoubleField)
|
|
||||||
```
|
|
||||||
|
|
||||||
… leads to generation of bytecode, which can be decompiled to the following Java class:
|
|
||||||
|
|
||||||
```java
|
|
||||||
import java.util.*;
|
|
||||||
import kotlin.jvm.functions.*;
|
|
||||||
import space.kscience.kmath.asm.internal.*;
|
|
||||||
import space.kscience.kmath.complex.*;
|
|
||||||
import space.kscience.kmath.expressions.*;
|
|
||||||
|
|
||||||
public final class CompiledExpression_45045_0 implements Expression<Complex> {
|
|
||||||
private final Object[] constants;
|
|
||||||
|
|
||||||
public Complex invoke(Map<Symbol, ? extends Complex> arguments) {
|
|
||||||
Complex var2 = (Complex)MapIntrinsics.getOrFail(arguments, "x");
|
|
||||||
return (Complex)((Function2)this.constants[0]).invoke(var2, (Complex)this.constants[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
For `LongRing`, `IntRing`, and `DoubleField` specialization is supported for better performance:
|
|
||||||
|
|
||||||
```java
|
|
||||||
import java.util.*;
|
|
||||||
import space.kscience.kmath.asm.internal.*;
|
|
||||||
import space.kscience.kmath.expressions.*;
|
|
||||||
|
|
||||||
public final class CompiledExpression_-386104628_0 implements DoubleExpression {
|
|
||||||
private final SymbolIndexer indexer;
|
|
||||||
|
|
||||||
public SymbolIndexer getIndexer() {
|
|
||||||
return this.indexer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double invoke(double[] arguments) {
|
|
||||||
double var2 = arguments[0];
|
|
||||||
return Math.pow(var2, 3.0D) - var2 + 3.0D;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final Double invoke(Map<Symbol, ? extends Double> arguments) {
|
|
||||||
double var2 = ((Double)MapIntrinsics.getOrFail(arguments, "x")).doubleValue();
|
|
||||||
return Math.pow(var2, 3.0D) - var2 + 3.0D;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Setting JVM system property `space.kscience.kmath.ast.dump.generated.classes` to `1` makes the translator dump class files to program's working directory, so they can be reviewed manually.
|
|
||||||
|
|
||||||
#### Limitations
|
|
||||||
|
|
||||||
- The same classes may be generated and loaded twice, so it is recommended to cache compiled expressions to avoid class loading overhead.
|
|
||||||
- This API is not supported by non-dynamic JVM implementations like TeaVM or GraalVM Native Image because they may not support class loaders.
|
|
||||||
|
|
||||||
### On JS
|
|
||||||
|
|
||||||
A similar feature is also available on JS.
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.*
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
import space.kscience.kmath.estree.*
|
|
||||||
|
|
||||||
MstField { x + 2 }.compileToExpression(DoubleField)
|
|
||||||
```
|
|
||||||
|
|
||||||
The code above returns expression implemented with such a JS function:
|
|
||||||
|
|
||||||
```js
|
|
||||||
var executable = function (constants, arguments) {
|
|
||||||
return constants[1](constants[0](arguments, "x"), 2);
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
JS also supports experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation.
|
|
||||||
Currently, only expressions inside `DoubleField` and `IntRing` are supported.
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.*
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
import space.kscience.kmath.wasm.*
|
|
||||||
|
|
||||||
MstField { x + 2 }.compileToExpression(DoubleField)
|
|
||||||
```
|
|
||||||
|
|
||||||
An example of emitted Wasm IR in the form of WAT:
|
|
||||||
|
|
||||||
```lisp
|
|
||||||
(func \$executable (param \$0 f64) (result f64)
|
|
||||||
(f64.add
|
|
||||||
(local.get \$0)
|
|
||||||
(f64.const 2)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Limitations
|
|
||||||
|
|
||||||
- ESTree expression compilation uses `eval` which can be unavailable in several environments.
|
|
||||||
- WebAssembly isn't supported by old versions of browsers (see https://webassembly.org/roadmap/).
|
|
||||||
|
|
||||||
## Rendering expressions
|
|
||||||
|
|
||||||
kmath-ast also includes an extensible engine to display expressions in LaTeX or MathML syntax.
|
|
||||||
|
|
||||||
Example usage:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
import space.kscience.kmath.ast.*
|
|
||||||
import space.kscience.kmath.ast.rendering.*
|
|
||||||
import space.kscience.kmath.misc.*
|
|
||||||
|
|
||||||
@OptIn(UnstableKMathAPI::class)
|
|
||||||
public fun main() {
|
|
||||||
val mst = "exp(sqrt(x))-asin(2*x)/(2e10+x^3)/(12)+x^(2/3)".parseMath()
|
|
||||||
val syntax = FeaturedMathRendererWithPostProcess.Default.render(mst)
|
|
||||||
val latex = LatexSyntaxRenderer.renderWithStringBuilder(syntax)
|
|
||||||
println("LaTeX:")
|
|
||||||
println(latex)
|
|
||||||
println()
|
|
||||||
val mathML = MathMLSyntaxRenderer.renderWithStringBuilder(syntax)
|
|
||||||
println("MathML:")
|
|
||||||
println(mathML)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Result LaTeX:
|
|
||||||
|
|
||||||
<div style="background-color:white;">
|
|
||||||
|
|
||||||
![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3})
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Result MathML (can be used with MathJax or other renderers):
|
|
||||||
|
|
||||||
<details>
|
|
||||||
|
|
||||||
```html
|
|
||||||
<math xmlns="https://www.w3.org/1998/Math/MathML">
|
|
||||||
<mrow>
|
|
||||||
<mo>exp</mo>
|
|
||||||
<mspace width="0.167em"></mspace>
|
|
||||||
<mfenced open="(" close=")" separators="">
|
|
||||||
<msqrt>
|
|
||||||
<mi>x</mi>
|
|
||||||
</msqrt>
|
|
||||||
</mfenced>
|
|
||||||
<mo>-</mo>
|
|
||||||
<mfrac>
|
|
||||||
<mrow>
|
|
||||||
<mfrac>
|
|
||||||
<mrow>
|
|
||||||
<mo>arcsin</mo>
|
|
||||||
<mspace width="0.167em"></mspace>
|
|
||||||
<mfenced open="(" close=")" separators="">
|
|
||||||
<mn>2</mn>
|
|
||||||
<mspace width="0.167em"></mspace>
|
|
||||||
<mi>x</mi>
|
|
||||||
</mfenced>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>2</mn>
|
|
||||||
<mo>×</mo>
|
|
||||||
<msup>
|
|
||||||
<mrow>
|
|
||||||
<mn>10</mn>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>10</mn>
|
|
||||||
</mrow>
|
|
||||||
</msup>
|
|
||||||
<mo>+</mo>
|
|
||||||
<msup>
|
|
||||||
<mrow>
|
|
||||||
<mi>x</mi>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>3</mn>
|
|
||||||
</mrow>
|
|
||||||
</msup>
|
|
||||||
</mrow>
|
|
||||||
</mfrac>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>12</mn>
|
|
||||||
</mrow>
|
|
||||||
</mfrac>
|
|
||||||
<mo>+</mo>
|
|
||||||
<msup>
|
|
||||||
<mrow>
|
|
||||||
<mi>x</mi>
|
|
||||||
</mrow>
|
|
||||||
<mrow>
|
|
||||||
<mn>2</mn>
|
|
||||||
<mo>/</mo>
|
|
||||||
<mn>3</mn>
|
|
||||||
</mrow>
|
|
||||||
</msup>
|
|
||||||
</mrow>
|
|
||||||
</math>
|
|
||||||
```
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
It is also possible to create custom algorithms of render, and even add support of other markup languages
|
|
||||||
(see API reference).
|
|
@ -1,59 +0,0 @@
|
|||||||
grammar ArithmeticsEvaluator;
|
|
||||||
|
|
||||||
fragment DIGIT: '0'..'9';
|
|
||||||
fragment LETTER: 'a'..'z';
|
|
||||||
fragment CAPITAL_LETTER: 'A'..'Z';
|
|
||||||
fragment UNDERSCORE: '_';
|
|
||||||
|
|
||||||
ID: (LETTER | UNDERSCORE | CAPITAL_LETTER) (LETTER | UNDERSCORE | DIGIT | CAPITAL_LETTER)*;
|
|
||||||
NUM: (DIGIT | '.')+ ([eE] [-+]? DIGIT+)?;
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
PLUS: '+';
|
|
||||||
MINUS: '-';
|
|
||||||
POW: '^';
|
|
||||||
COMMA: ',';
|
|
||||||
LPAR: '(';
|
|
||||||
RPAR: ')';
|
|
||||||
WS: [ \n\t\r]+ -> skip;
|
|
||||||
|
|
||||||
num
|
|
||||||
: NUM
|
|
||||||
;
|
|
||||||
|
|
||||||
singular
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryFunction
|
|
||||||
: ID LPAR subSumChain RPAR
|
|
||||||
;
|
|
||||||
|
|
||||||
binaryFunction
|
|
||||||
: ID LPAR subSumChain COMMA subSumChain RPAR
|
|
||||||
;
|
|
||||||
|
|
||||||
term
|
|
||||||
: num
|
|
||||||
| singular
|
|
||||||
| unaryFunction
|
|
||||||
| binaryFunction
|
|
||||||
| MINUS term
|
|
||||||
| LPAR subSumChain RPAR
|
|
||||||
;
|
|
||||||
|
|
||||||
powChain
|
|
||||||
: term (POW term)*
|
|
||||||
;
|
|
||||||
|
|
||||||
divMulChain
|
|
||||||
: powChain ((DIV | MUL) powChain)*
|
|
||||||
;
|
|
||||||
|
|
||||||
subSumChain
|
|
||||||
: divMulChain ((PLUS | MINUS) divMulChain)*
|
|
||||||
;
|
|
||||||
|
|
||||||
rootParser
|
|
||||||
: subSumChain EOF
|
|
||||||
;
|
|
@ -1,177 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.Expression
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.operations.Algebra
|
|
||||||
import space.kscience.kmath.operations.NumericAlgebra
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MST form where all values belong to the type [T]. It is optimal for constant folding, dynamic compilation, etc.
|
|
||||||
*
|
|
||||||
* @param T the type.
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public sealed interface TypedMst<T> {
|
|
||||||
/**
|
|
||||||
* A node containing a unary operation.
|
|
||||||
*
|
|
||||||
* @param T the type.
|
|
||||||
* @property operation The identifier of operation.
|
|
||||||
* @property function The function implementing this operation.
|
|
||||||
* @property value The argument of this operation.
|
|
||||||
*/
|
|
||||||
public class Unary<T>(public val operation: String, public val function: (T) -> T, public val value: TypedMst<T>) :
|
|
||||||
TypedMst<T> {
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (other == null || this::class != other::class) return false
|
|
||||||
other as Unary<*>
|
|
||||||
if (operation != other.operation) return false
|
|
||||||
if (value != other.value) return false
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = operation.hashCode()
|
|
||||||
result = 31 * result + value.hashCode()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String = "Unary(operation=$operation, value=$value)"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A node containing binary operation.
|
|
||||||
*
|
|
||||||
* @param T the type.
|
|
||||||
* @property operation The identifier of operation.
|
|
||||||
* @property function The binary function implementing this operation.
|
|
||||||
* @property left The left operand.
|
|
||||||
* @property right The right operand.
|
|
||||||
*/
|
|
||||||
public class Binary<T>(
|
|
||||||
public val operation: String,
|
|
||||||
public val function: Function<T>,
|
|
||||||
public val left: TypedMst<T>,
|
|
||||||
public val right: TypedMst<T>,
|
|
||||||
) : TypedMst<T> {
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (other == null || this::class != other::class) return false
|
|
||||||
|
|
||||||
other as Binary<*>
|
|
||||||
|
|
||||||
if (operation != other.operation) return false
|
|
||||||
if (left != other.left) return false
|
|
||||||
if (right != other.right) return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = operation.hashCode()
|
|
||||||
result = 31 * result + left.hashCode()
|
|
||||||
result = 31 * result + right.hashCode()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String = "Binary(operation=$operation, left=$left, right=$right)"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The non-numeric constant value.
|
|
||||||
*
|
|
||||||
* @param T the type.
|
|
||||||
* @property value The held value.
|
|
||||||
* @property number The number this value corresponds.
|
|
||||||
*/
|
|
||||||
public class Constant<T>(public val value: T, public val number: Number?) : TypedMst<T> {
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (other == null || this::class != other::class) return false
|
|
||||||
other as Constant<*>
|
|
||||||
if (value != other.value) return false
|
|
||||||
if (number != other.number) return false
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
var result = value?.hashCode() ?: 0
|
|
||||||
result = 31 * result + (number?.hashCode() ?: 0)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String = "Constant(value=$value, number=$number)"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The node containing a variable
|
|
||||||
*
|
|
||||||
* @param T the type.
|
|
||||||
* @property symbol The symbol of the variable.
|
|
||||||
*/
|
|
||||||
public class Variable<T>(public val symbol: Symbol) : TypedMst<T> {
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (other == null || this::class != other::class) return false
|
|
||||||
other as Variable<*>
|
|
||||||
if (symbol != other.symbol) return false
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int = symbol.hashCode()
|
|
||||||
override fun toString(): String = "Variable(symbol=$symbol)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interprets the [TypedMst] node with this [Algebra] and [arguments].
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun <T> TypedMst<T>.interpret(algebra: Algebra<T>, arguments: Map<Symbol, T>): T = when (this) {
|
|
||||||
is TypedMst.Unary -> algebra.unaryOperation(operation, interpret(algebra, arguments))
|
|
||||||
|
|
||||||
is TypedMst.Binary -> when {
|
|
||||||
algebra is NumericAlgebra && left is TypedMst.Constant && left.number != null ->
|
|
||||||
algebra.leftSideNumberOperation(operation, left.number, right.interpret(algebra, arguments))
|
|
||||||
|
|
||||||
algebra is NumericAlgebra && right is TypedMst.Constant && right.number != null ->
|
|
||||||
algebra.rightSideNumberOperation(operation, left.interpret(algebra, arguments), right.number)
|
|
||||||
|
|
||||||
else -> algebra.binaryOperation(
|
|
||||||
operation,
|
|
||||||
left.interpret(algebra, arguments),
|
|
||||||
right.interpret(algebra, arguments),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
is TypedMst.Constant -> value
|
|
||||||
is TypedMst.Variable -> arguments.getValue(symbol)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interprets the [TypedMst] node with this [Algebra] and optional [arguments].
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun <T> TypedMst<T>.interpret(algebra: Algebra<T>, vararg arguments: Pair<Symbol, T>): T = interpret(
|
|
||||||
algebra,
|
|
||||||
when (arguments.size) {
|
|
||||||
0 -> emptyMap()
|
|
||||||
1 -> mapOf(arguments[0])
|
|
||||||
else -> hashMapOf(*arguments)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interpret this [TypedMst] node as expression.
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun <T : Any> TypedMst<T>.toExpression(algebra: Algebra<T>): Expression<T> = Expression { arguments ->
|
|
||||||
interpret(algebra, arguments)
|
|
||||||
}
|
|
@ -1,93 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.operations.Algebra
|
|
||||||
import space.kscience.kmath.operations.NumericAlgebra
|
|
||||||
import space.kscience.kmath.operations.bindSymbolOrNull
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Evaluates constants in given [MST] for given [algebra] at the same time with converting to [TypedMst].
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun <T> MST.evaluateConstants(algebra: Algebra<T>): TypedMst<T> = when (this) {
|
|
||||||
is MST.Numeric -> TypedMst.Constant(
|
|
||||||
(algebra as? NumericAlgebra<T>)?.number(value) ?: error("Numeric nodes are not supported by $algebra"),
|
|
||||||
value,
|
|
||||||
)
|
|
||||||
|
|
||||||
is MST.Unary -> when (val arg = value.evaluateConstants(algebra)) {
|
|
||||||
is TypedMst.Constant<T> -> {
|
|
||||||
val value = algebra.unaryOperation(
|
|
||||||
operation,
|
|
||||||
arg.value,
|
|
||||||
)
|
|
||||||
|
|
||||||
TypedMst.Constant(value, if (value is Number) value else null)
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> TypedMst.Unary(operation, algebra.unaryOperationFunction(operation), arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
is MST.Binary -> {
|
|
||||||
val left = left.evaluateConstants(algebra)
|
|
||||||
val right = right.evaluateConstants(algebra)
|
|
||||||
|
|
||||||
when {
|
|
||||||
left is TypedMst.Constant<T> && right is TypedMst.Constant<T> -> {
|
|
||||||
val value = when {
|
|
||||||
algebra is NumericAlgebra && left.number != null -> algebra.leftSideNumberOperation(
|
|
||||||
operation,
|
|
||||||
left.number,
|
|
||||||
right.value,
|
|
||||||
)
|
|
||||||
|
|
||||||
algebra is NumericAlgebra && right.number != null -> algebra.rightSideNumberOperation(
|
|
||||||
operation,
|
|
||||||
left.value,
|
|
||||||
right.number,
|
|
||||||
)
|
|
||||||
|
|
||||||
else -> algebra.binaryOperation(
|
|
||||||
operation,
|
|
||||||
left.value,
|
|
||||||
right.value,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
TypedMst.Constant(value, if (value is Number) value else null)
|
|
||||||
}
|
|
||||||
|
|
||||||
algebra is NumericAlgebra && left is TypedMst.Constant && left.number != null -> TypedMst.Binary(
|
|
||||||
operation,
|
|
||||||
algebra.leftSideNumberOperationFunction(operation),
|
|
||||||
left,
|
|
||||||
right,
|
|
||||||
)
|
|
||||||
|
|
||||||
algebra is NumericAlgebra && right is TypedMst.Constant && right.number != null -> TypedMst.Binary(
|
|
||||||
operation,
|
|
||||||
algebra.rightSideNumberOperationFunction(operation),
|
|
||||||
left,
|
|
||||||
right,
|
|
||||||
)
|
|
||||||
|
|
||||||
else -> TypedMst.Binary(operation, algebra.binaryOperationFunction(operation), left, right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is Symbol -> {
|
|
||||||
val boundSymbol = algebra.bindSymbolOrNull(this)
|
|
||||||
|
|
||||||
if (boundSymbol != null)
|
|
||||||
TypedMst.Constant(boundSymbol, if (boundSymbol is Number) boundSymbol else null)
|
|
||||||
else
|
|
||||||
TypedMst.Variable(this)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,122 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import com.github.h0tk3y.betterParse.combinators.*
|
|
||||||
import com.github.h0tk3y.betterParse.grammar.Grammar
|
|
||||||
import com.github.h0tk3y.betterParse.grammar.parseToEnd
|
|
||||||
import com.github.h0tk3y.betterParse.grammar.parser
|
|
||||||
import com.github.h0tk3y.betterParse.grammar.tryParseToEnd
|
|
||||||
import com.github.h0tk3y.betterParse.lexer.Token
|
|
||||||
import com.github.h0tk3y.betterParse.lexer.TokenMatch
|
|
||||||
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 space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.operations.FieldOps
|
|
||||||
import space.kscience.kmath.operations.GroupOps
|
|
||||||
import space.kscience.kmath.operations.PowerOperations
|
|
||||||
import space.kscience.kmath.operations.RingOps
|
|
||||||
import kotlin.math.floor
|
|
||||||
|
|
||||||
/**
|
|
||||||
* better-parse implementation of grammar defined in the ArithmeticsEvaluator.g4.
|
|
||||||
*
|
|
||||||
* @author Alexander Nozik
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
public object ArithmeticsEvaluator : Grammar<MST>() {
|
|
||||||
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(",")
|
|
||||||
private val mul: Token by literalToken("*")
|
|
||||||
private val pow: Token by literalToken("^")
|
|
||||||
private val div: Token by literalToken("/")
|
|
||||||
private val minus: Token by literalToken("-")
|
|
||||||
private val plus: Token by literalToken("+")
|
|
||||||
|
|
||||||
@Suppress("unused")
|
|
||||||
private val ws: Token by regexToken("\\s+".toRegex(), ignore = true)
|
|
||||||
|
|
||||||
// TODO Rewrite as custom parser to handle numbers with better precision. Currently, numbers like 1e10 are handled while they could be stored as longs without precision loss.
|
|
||||||
private val number: Parser<MST> by num use {
|
|
||||||
val d = text.toDoubleOrNull()
|
|
||||||
|
|
||||||
MST.Numeric(
|
|
||||||
if (d == null || d == floor(d) && !d.isInfinite()) {
|
|
||||||
text.toLongOrNull() ?: text.toDouble()
|
|
||||||
} else
|
|
||||||
d
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val singular: Parser<MST> by id use { Symbol(text) }
|
|
||||||
|
|
||||||
private val unaryFunction: Parser<MST> by (id and -lpar and parser(ArithmeticsEvaluator::subSumChain) and -rpar)
|
|
||||||
.map { (id, term) -> MST.Unary(id.text, term) }
|
|
||||||
|
|
||||||
private val binaryFunction: Parser<MST> by id
|
|
||||||
.and(-lpar)
|
|
||||||
.and(parser(ArithmeticsEvaluator::subSumChain))
|
|
||||||
.and(-comma)
|
|
||||||
.and(parser(ArithmeticsEvaluator::subSumChain))
|
|
||||||
.and(-rpar)
|
|
||||||
.map { (id, left, right) -> MST.Binary(id.text, left, right) }
|
|
||||||
|
|
||||||
private val term: Parser<MST> by number
|
|
||||||
.or(binaryFunction)
|
|
||||||
.or(unaryFunction)
|
|
||||||
.or(singular)
|
|
||||||
.or(-minus and parser(ArithmeticsEvaluator::term) map { MST.Unary(GroupOps.MINUS_OPERATION, it) })
|
|
||||||
.or(-lpar and parser(ArithmeticsEvaluator::subSumChain) and -rpar)
|
|
||||||
|
|
||||||
private val powChain: Parser<MST> by leftAssociative(term = term, operator = pow) { a, _, b ->
|
|
||||||
MST.Binary(PowerOperations.POW_OPERATION, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val divMulChain: Parser<MST> by leftAssociative(
|
|
||||||
term = powChain,
|
|
||||||
operator = div or mul use TokenMatch::type
|
|
||||||
) { a, op, b ->
|
|
||||||
if (op == div)
|
|
||||||
MST.Binary(FieldOps.DIV_OPERATION, a, b)
|
|
||||||
else
|
|
||||||
MST.Binary(RingOps.TIMES_OPERATION, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val subSumChain: Parser<MST> by leftAssociative(
|
|
||||||
term = divMulChain,
|
|
||||||
operator = plus or minus use TokenMatch::type
|
|
||||||
) { a, op, b ->
|
|
||||||
if (op == plus)
|
|
||||||
MST.Binary(GroupOps.PLUS_OPERATION, a, b)
|
|
||||||
else
|
|
||||||
MST.Binary(GroupOps.MINUS_OPERATION, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
override val rootParser: Parser<MST> by subSumChain
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tries to parse the string into [MST] using [ArithmeticsEvaluator]. Returns [ParseResult] representing expression or
|
|
||||||
* error.
|
|
||||||
*
|
|
||||||
* @receiver the string to parse.
|
|
||||||
* @return the [MST] node.
|
|
||||||
*/
|
|
||||||
public fun String.tryParseMath(): ParseResult<MST> = ArithmeticsEvaluator.tryParseToEnd(this)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parses the string into [MST] using [ArithmeticsEvaluator].
|
|
||||||
*
|
|
||||||
* @receiver the string to parse.
|
|
||||||
* @return the [MST] node.
|
|
||||||
*/
|
|
||||||
public fun String.parseMath(): MST = ArithmeticsEvaluator.parseToEnd(this)
|
|
@ -1,150 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [SyntaxRenderer] implementation for LaTeX.
|
|
||||||
*
|
|
||||||
* The generated string is a valid LaTeX fragment to be used in the Math Mode.
|
|
||||||
*
|
|
||||||
* Example usage:
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* \documentclass{article}
|
|
||||||
* \begin{document}
|
|
||||||
* \begin{equation}
|
|
||||||
* %code generated by the syntax renderer
|
|
||||||
* \end{equation}
|
|
||||||
* \end{document}
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public object LatexSyntaxRenderer : SyntaxRenderer {
|
|
||||||
override fun render(node: MathSyntax, output: Appendable): Unit = output.run {
|
|
||||||
fun render(syntax: MathSyntax) = render(syntax, output)
|
|
||||||
|
|
||||||
when (node) {
|
|
||||||
is NumberSyntax -> append(node.string)
|
|
||||||
is SymbolSyntax -> append(node.string)
|
|
||||||
|
|
||||||
is OperatorNameSyntax -> {
|
|
||||||
append("\\operatorname{")
|
|
||||||
append(node.name)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is SpecialSymbolSyntax -> when (node.kind) {
|
|
||||||
SpecialSymbolSyntax.Kind.INFINITY -> append("\\infty")
|
|
||||||
SpecialSymbolSyntax.Kind.SMALL_PI -> append("\\pi")
|
|
||||||
}
|
|
||||||
|
|
||||||
is OperandSyntax -> {
|
|
||||||
if (node.parentheses) append("\\left(")
|
|
||||||
render(node.operand)
|
|
||||||
if (node.parentheses) append("\\right)")
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryOperatorSyntax -> {
|
|
||||||
render(node.prefix)
|
|
||||||
append("\\,")
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryPlusSyntax -> {
|
|
||||||
append('+')
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryMinusSyntax -> {
|
|
||||||
append('-')
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalSyntax -> {
|
|
||||||
append("\\sqrt")
|
|
||||||
append('{')
|
|
||||||
render(node.operand)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is ExponentSyntax -> if (node.useOperatorForm) {
|
|
||||||
append("\\operatorname{exp}\\,")
|
|
||||||
render(node.operand)
|
|
||||||
} else {
|
|
||||||
append("e^{")
|
|
||||||
render(node.operand)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is SuperscriptSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
append("^{")
|
|
||||||
render(node.right)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is SubscriptSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
append("_{")
|
|
||||||
render(node.right)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryOperatorSyntax -> {
|
|
||||||
render(node.prefix)
|
|
||||||
append("\\left(")
|
|
||||||
render(node.left)
|
|
||||||
append(',')
|
|
||||||
render(node.right)
|
|
||||||
append("\\right)")
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryPlusSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
append('+')
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryMinusSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
append('-')
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is FractionSyntax -> if (node.infix) {
|
|
||||||
render(node.left)
|
|
||||||
append('/')
|
|
||||||
render(node.right)
|
|
||||||
} else {
|
|
||||||
append("\\frac{")
|
|
||||||
render(node.left)
|
|
||||||
append("}{")
|
|
||||||
render(node.right)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalWithIndexSyntax -> {
|
|
||||||
append("\\sqrt")
|
|
||||||
append('[')
|
|
||||||
render(node.left)
|
|
||||||
append(']')
|
|
||||||
append('{')
|
|
||||||
render(node.right)
|
|
||||||
append('}')
|
|
||||||
}
|
|
||||||
|
|
||||||
is MultiplicationSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
append(if (node.times) "\\times" else "\\,")
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,157 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [SyntaxRenderer] implementation for MathML.
|
|
||||||
*
|
|
||||||
* The generated XML string is a valid MathML instance.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public object MathMLSyntaxRenderer : SyntaxRenderer {
|
|
||||||
override fun render(node: MathSyntax, output: Appendable) {
|
|
||||||
output.append("<math xmlns=\"https://www.w3.org/1998/Math/MathML\"><mrow>")
|
|
||||||
renderPart(node, output)
|
|
||||||
output.append("</mrow></math>")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders a part of syntax returning a correct MathML tag not the whole MathML instance.
|
|
||||||
*/
|
|
||||||
public fun renderPart(node: MathSyntax, output: Appendable): Unit = output.run {
|
|
||||||
fun tag(tagName: String, vararg attr: Pair<String, String>, block: () -> Unit = {}) {
|
|
||||||
append('<')
|
|
||||||
append(tagName)
|
|
||||||
|
|
||||||
if (attr.isNotEmpty()) {
|
|
||||||
append(' ')
|
|
||||||
var count = 0
|
|
||||||
|
|
||||||
for ((name, value) in attr) {
|
|
||||||
if (++count > 1) append(' ')
|
|
||||||
append(name)
|
|
||||||
append("=\"")
|
|
||||||
append(value)
|
|
||||||
append('"')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
append('>')
|
|
||||||
block()
|
|
||||||
append("</")
|
|
||||||
append(tagName)
|
|
||||||
append('>')
|
|
||||||
}
|
|
||||||
|
|
||||||
fun render(syntax: MathSyntax) = renderPart(syntax, output)
|
|
||||||
|
|
||||||
when (node) {
|
|
||||||
is NumberSyntax -> tag("mn") { append(node.string) }
|
|
||||||
is SymbolSyntax -> tag("mi") { append(node.string) }
|
|
||||||
is OperatorNameSyntax -> tag("mo") { append(node.name) }
|
|
||||||
|
|
||||||
is SpecialSymbolSyntax -> when (node.kind) {
|
|
||||||
SpecialSymbolSyntax.Kind.INFINITY -> tag("mo") { append("∞") }
|
|
||||||
SpecialSymbolSyntax.Kind.SMALL_PI -> tag("mo") { append("π") }
|
|
||||||
}
|
|
||||||
|
|
||||||
is OperandSyntax -> if (node.parentheses) {
|
|
||||||
tag("mfenced", "open" to "(", "close" to ")", "separators" to "") {
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryOperatorSyntax -> {
|
|
||||||
render(node.prefix)
|
|
||||||
tag("mspace", "width" to "0.167em")
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryPlusSyntax -> {
|
|
||||||
tag("mo") { append('+') }
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryMinusSyntax -> {
|
|
||||||
tag("mo") { append("-") }
|
|
||||||
render(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalSyntax -> tag("msqrt") { render(node.operand) }
|
|
||||||
|
|
||||||
is ExponentSyntax -> if (node.useOperatorForm) {
|
|
||||||
tag("mo") { append("exp") }
|
|
||||||
tag("mspace", "width" to "0.167em")
|
|
||||||
render(node.operand)
|
|
||||||
} else {
|
|
||||||
tag("msup") {
|
|
||||||
tag("mrow") {
|
|
||||||
tag("mi") { append("e") }
|
|
||||||
}
|
|
||||||
tag("mrow") { render(node.operand) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is SuperscriptSyntax -> tag("msup") {
|
|
||||||
tag("mrow") { render(node.left) }
|
|
||||||
tag("mrow") { render(node.right) }
|
|
||||||
}
|
|
||||||
|
|
||||||
is SubscriptSyntax -> tag("msub") {
|
|
||||||
tag("mrow") { render(node.left) }
|
|
||||||
tag("mrow") { render(node.right) }
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryOperatorSyntax -> {
|
|
||||||
render(node.prefix)
|
|
||||||
|
|
||||||
tag("mfenced", "open" to "(", "close" to ")", "separators" to "") {
|
|
||||||
render(node.left)
|
|
||||||
tag("mo") { append(',') }
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryPlusSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
tag("mo") { append('+') }
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryMinusSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
tag("mo") { append('-') }
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is FractionSyntax -> if (node.infix) {
|
|
||||||
render(node.left)
|
|
||||||
tag("mo") { append('/') }
|
|
||||||
render(node.right)
|
|
||||||
} else tag("mfrac") {
|
|
||||||
tag("mrow") { render(node.left) }
|
|
||||||
tag("mrow") { render(node.right) }
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalWithIndexSyntax -> tag("mroot") {
|
|
||||||
tag("mrow") { render(node.right) }
|
|
||||||
tag("mrow") { render(node.left) }
|
|
||||||
}
|
|
||||||
|
|
||||||
is MultiplicationSyntax -> {
|
|
||||||
render(node.left)
|
|
||||||
if (node.times) tag("mo") { append("×") } else tag("mspace", "width" to "0.167em")
|
|
||||||
render(node.right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,115 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders [MST] to [MathSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun interface MathRenderer {
|
|
||||||
/**
|
|
||||||
* Renders [MST] to [MathSyntax].
|
|
||||||
*/
|
|
||||||
public fun render(mst: MST): MathSyntax
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements [MST] render process with sequence of features.
|
|
||||||
*
|
|
||||||
* @property features The applied features.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public open class FeaturedMathRenderer(public val features: List<RenderFeature>) : MathRenderer {
|
|
||||||
override fun render(mst: MST): MathSyntax {
|
|
||||||
for (feature in features) feature.render(this, mst)?.let { return it }
|
|
||||||
throw UnsupportedOperationException("Renderer $this has no appropriate feature to render node $mst.")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logical unit of [MST] rendering.
|
|
||||||
*/
|
|
||||||
public fun interface RenderFeature {
|
|
||||||
/**
|
|
||||||
* Renders [MST] to [MathSyntax] in the context of owning renderer.
|
|
||||||
*/
|
|
||||||
public fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extends [FeaturedMathRenderer] by adding post-processing stages.
|
|
||||||
*
|
|
||||||
* @property stages The applied stages.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public open class FeaturedMathRendererWithPostProcess(
|
|
||||||
features: List<RenderFeature>,
|
|
||||||
public val stages: List<PostProcessPhase>,
|
|
||||||
) : FeaturedMathRenderer(features) {
|
|
||||||
override fun render(mst: MST): MathSyntax {
|
|
||||||
val res = super.render(mst)
|
|
||||||
for (stage in stages) stage.perform(res)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logical unit of [MathSyntax] post-processing.
|
|
||||||
*/
|
|
||||||
public fun interface PostProcessPhase {
|
|
||||||
/**
|
|
||||||
* Performs the specified action over [MathSyntax].
|
|
||||||
*/
|
|
||||||
public fun perform(node: MathSyntax)
|
|
||||||
}
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default setup of [FeaturedMathRendererWithPostProcess].
|
|
||||||
*/
|
|
||||||
public val Default: FeaturedMathRendererWithPostProcess = FeaturedMathRendererWithPostProcess(
|
|
||||||
listOf(
|
|
||||||
// Printing known operations
|
|
||||||
BinaryPlus.Default,
|
|
||||||
BinaryMinus.Default,
|
|
||||||
UnaryPlus.Default,
|
|
||||||
UnaryMinus.Default,
|
|
||||||
Multiplication.Default,
|
|
||||||
Fraction.Default,
|
|
||||||
Power.Default,
|
|
||||||
SquareRoot.Default,
|
|
||||||
Exponent.Default,
|
|
||||||
InverseTrigonometricOperations.Default,
|
|
||||||
InverseHyperbolicOperations.Default,
|
|
||||||
|
|
||||||
// Fallback option for unknown operations - printing them as operator
|
|
||||||
BinaryOperator.Default,
|
|
||||||
UnaryOperator.Default,
|
|
||||||
|
|
||||||
// Pretty printing for some objects
|
|
||||||
PrettyPrintFloats.Default,
|
|
||||||
PrettyPrintIntegers.Default,
|
|
||||||
PrettyPrintPi.Default,
|
|
||||||
|
|
||||||
// Printing terminal nodes as string
|
|
||||||
PrintNumeric,
|
|
||||||
PrintSymbol,
|
|
||||||
),
|
|
||||||
listOf(
|
|
||||||
BetterExponent,
|
|
||||||
BetterFraction,
|
|
||||||
SimplifyParentheses.Default,
|
|
||||||
BetterMultiplication,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,381 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Syntax node for mathematical typography.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public sealed class MathSyntax {
|
|
||||||
/**
|
|
||||||
* The parent node of this syntax node.
|
|
||||||
*/
|
|
||||||
public var parent: MathSyntax? = null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Terminal node, which should not have any children nodes.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public sealed class TerminalSyntax : MathSyntax()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Node containing a certain operation.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public sealed class OperationSyntax : MathSyntax() {
|
|
||||||
/**
|
|
||||||
* The operation token.
|
|
||||||
*/
|
|
||||||
public abstract val operation: String
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unary node, which has only one child.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public sealed class UnarySyntax : OperationSyntax() {
|
|
||||||
/**
|
|
||||||
* The operand of this node.
|
|
||||||
*/
|
|
||||||
public abstract val operand: MathSyntax
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Binary node, which has only two children.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public sealed class BinarySyntax : OperationSyntax() {
|
|
||||||
/**
|
|
||||||
* The left-hand side operand.
|
|
||||||
*/
|
|
||||||
public abstract val left: MathSyntax
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The right-hand side operand.
|
|
||||||
*/
|
|
||||||
public abstract val right: MathSyntax
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a number.
|
|
||||||
*
|
|
||||||
* @property string The digits of number.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class NumberSyntax(public var string: String) : TerminalSyntax()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a symbol.
|
|
||||||
*
|
|
||||||
* @property string The symbol.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class SymbolSyntax(public var string: String) : TerminalSyntax()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents special typing for operator name.
|
|
||||||
*
|
|
||||||
* @property name The operator name.
|
|
||||||
* @see BinaryOperatorSyntax
|
|
||||||
* @see UnaryOperatorSyntax
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class OperatorNameSyntax(public var name: String) : TerminalSyntax()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a usage of special symbols (e.g., *∞*).
|
|
||||||
*
|
|
||||||
* @property kind The kind of symbol.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class SpecialSymbolSyntax(public var kind: Kind) : TerminalSyntax() {
|
|
||||||
/**
|
|
||||||
* The kind of symbol.
|
|
||||||
*/
|
|
||||||
public enum class Kind {
|
|
||||||
/**
|
|
||||||
* The infinity (∞) symbol.
|
|
||||||
*/
|
|
||||||
INFINITY,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Pi (π) symbol.
|
|
||||||
*/
|
|
||||||
SMALL_PI;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents operand of a certain operator wrapped with parentheses or not.
|
|
||||||
*
|
|
||||||
* @property operand The operand.
|
|
||||||
* @property parentheses Whether the operand should be wrapped with parentheses.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class OperandSyntax(
|
|
||||||
public val operand: MathSyntax,
|
|
||||||
public var parentheses: Boolean,
|
|
||||||
) : MathSyntax() {
|
|
||||||
init {
|
|
||||||
operand.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents unary, prefix operator syntax (like *f(x)*).
|
|
||||||
*
|
|
||||||
* @property prefix The prefix.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class UnaryOperatorSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
public var prefix: MathSyntax,
|
|
||||||
override val operand: OperandSyntax,
|
|
||||||
) : UnarySyntax() {
|
|
||||||
init {
|
|
||||||
operand.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents prefix, unary plus operator (*+x*).
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class UnaryPlusSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val operand: OperandSyntax,
|
|
||||||
) : UnarySyntax() {
|
|
||||||
init {
|
|
||||||
operand.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents prefix, unary minus operator (*-x*).
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class UnaryMinusSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val operand: OperandSyntax,
|
|
||||||
) : UnarySyntax() {
|
|
||||||
init {
|
|
||||||
operand.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents radical with a node inside it (*√x*).
|
|
||||||
*
|
|
||||||
* @property operand The radicand.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class RadicalSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val operand: MathSyntax,
|
|
||||||
) : UnarySyntax() {
|
|
||||||
init {
|
|
||||||
operand.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents exponential function.
|
|
||||||
*
|
|
||||||
* @property operand The argument of function.
|
|
||||||
* @property useOperatorForm `true` if operator form is used (*exp (x)*), `false` if exponentiation form is used
|
|
||||||
* (*e<sup>x</sup>*).
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class ExponentSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val operand: OperandSyntax,
|
|
||||||
public var useOperatorForm: Boolean,
|
|
||||||
) : UnarySyntax() {
|
|
||||||
init {
|
|
||||||
operand.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a syntax node with superscript (*x<sup>2</sup>*).
|
|
||||||
*
|
|
||||||
* @property left The node.
|
|
||||||
* @property right The superscript.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class SuperscriptSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: MathSyntax,
|
|
||||||
override val right: MathSyntax,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a syntax node with subscript (*x<sub>i</sup>*).
|
|
||||||
*
|
|
||||||
* @property left The node.
|
|
||||||
* @property right The subscript.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class SubscriptSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: MathSyntax,
|
|
||||||
override val right: MathSyntax,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents binary, prefix operator syntax (like *f(a, b)*).
|
|
||||||
*
|
|
||||||
* @property prefix The prefix.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class BinaryOperatorSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
public var prefix: MathSyntax,
|
|
||||||
override val left: MathSyntax,
|
|
||||||
override val right: MathSyntax,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents binary, infix addition (*42 + 42*).
|
|
||||||
*
|
|
||||||
* @param left The augend.
|
|
||||||
* @param right The addend.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class BinaryPlusSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: OperandSyntax,
|
|
||||||
override val right: OperandSyntax,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents binary, infix subtraction (*42 − 42*).
|
|
||||||
*
|
|
||||||
* @param left The minuend.
|
|
||||||
* @param right The subtrahend.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class BinaryMinusSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: OperandSyntax,
|
|
||||||
override val right: OperandSyntax,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents fraction with numerator and denominator.
|
|
||||||
*
|
|
||||||
* @property left The numerator.
|
|
||||||
* @property right The denominator.
|
|
||||||
* @property infix Whether infix (*1 / 2*) or normal (*½*) fraction should be made.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class FractionSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: OperandSyntax,
|
|
||||||
override val right: OperandSyntax,
|
|
||||||
public var infix: Boolean,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents radical syntax with index (*<sup>3</sup>√x*).
|
|
||||||
*
|
|
||||||
* @property left The index.
|
|
||||||
* @property right The radicand.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class RadicalWithIndexSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: MathSyntax,
|
|
||||||
override val right: MathSyntax,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents binary, infix multiplication in the form of coefficient (*2 x*) or with operator (*x × 2*).
|
|
||||||
*
|
|
||||||
* @property left The multiplicand.
|
|
||||||
* @property right The multiplier.
|
|
||||||
* @property times Whether the times (×) symbol should be used.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public data class MultiplicationSyntax(
|
|
||||||
override val operation: String,
|
|
||||||
override val left: OperandSyntax,
|
|
||||||
override val right: OperandSyntax,
|
|
||||||
public var times: Boolean,
|
|
||||||
) : BinarySyntax() {
|
|
||||||
init {
|
|
||||||
left.parent = this
|
|
||||||
right.parent = this
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstraction of writing [MathSyntax] as a string of an actual markup language. Typical implementation should
|
|
||||||
* involve traversal of MathSyntax with handling each subtype.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun interface SyntaxRenderer {
|
|
||||||
/**
|
|
||||||
* Renders the [MathSyntax] to [output].
|
|
||||||
*/
|
|
||||||
public fun render(node: MathSyntax, output: Appendable)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls [SyntaxRenderer.render] with given [node] and a new [StringBuilder] instance, and returns its content.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public fun SyntaxRenderer.renderWithStringBuilder(node: MathSyntax): String {
|
|
||||||
val sb = StringBuilder()
|
|
||||||
render(node, sb)
|
|
||||||
return sb.toString()
|
|
||||||
}
|
|
@ -1,483 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.FeaturedMathRenderer.RenderFeature
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.operations.*
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints any [Symbol] as a [SymbolSyntax] containing the [Symbol.identity] of it.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public val PrintSymbol: RenderFeature = RenderFeature { _, node ->
|
|
||||||
if (node !is Symbol) null
|
|
||||||
else SymbolSyntax(string = node.identity)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints any [MST.Numeric] as a [NumberSyntax] containing the [Any.toString] result of it.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public val PrintNumeric: RenderFeature = RenderFeature { _, node ->
|
|
||||||
if (node !is MST.Numeric)
|
|
||||||
null
|
|
||||||
else
|
|
||||||
NumberSyntax(string = node.value.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
@UnstableKMathAPI
|
|
||||||
private fun printSignedNumberString(s: String): MathSyntax = if (s.startsWith('-'))
|
|
||||||
UnaryMinusSyntax(
|
|
||||||
operation = GroupOps.MINUS_OPERATION,
|
|
||||||
operand = OperandSyntax(
|
|
||||||
operand = NumberSyntax(string = s.removePrefix("-")),
|
|
||||||
parentheses = true,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
else
|
|
||||||
NumberSyntax(string = s)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Special printing for numeric types that are printed in form of
|
|
||||||
* *('-'? (DIGIT+ ('.' DIGIT+)? ('E' '-'? DIGIT+)? | 'Infinity')) | 'NaN'*.
|
|
||||||
*
|
|
||||||
* @property types The suitable types.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class PrettyPrintFloats(public val types: Set<KClass<out Number>>) : RenderFeature {
|
|
||||||
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? {
|
|
||||||
if (node !is MST.Numeric || node.value::class !in types) return null
|
|
||||||
|
|
||||||
val toString = when (val v = node.value) {
|
|
||||||
is Float -> v.multiplatformToString()
|
|
||||||
is Double -> v.multiplatformToString()
|
|
||||||
else -> v.toString()
|
|
||||||
}.removeSuffix(".0")
|
|
||||||
|
|
||||||
if (toString.contains('E', ignoreCase = true)) {
|
|
||||||
val (beforeE, afterE) = toString.split('E', ignoreCase = true)
|
|
||||||
val significand = beforeE.toDouble().toString().removeSuffix(".0")
|
|
||||||
val exponent = afterE.toDouble().toString().removeSuffix(".0")
|
|
||||||
|
|
||||||
return MultiplicationSyntax(
|
|
||||||
operation = RingOps.TIMES_OPERATION,
|
|
||||||
left = OperandSyntax(operand = NumberSyntax(significand), parentheses = true),
|
|
||||||
right = OperandSyntax(
|
|
||||||
operand = SuperscriptSyntax(
|
|
||||||
operation = PowerOperations.POW_OPERATION,
|
|
||||||
left = NumberSyntax(string = "10"),
|
|
||||||
right = printSignedNumberString(exponent),
|
|
||||||
),
|
|
||||||
parentheses = true,
|
|
||||||
),
|
|
||||||
times = true,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toString.endsWith("Infinity")) {
|
|
||||||
val infty = SpecialSymbolSyntax(SpecialSymbolSyntax.Kind.INFINITY)
|
|
||||||
|
|
||||||
if (toString.startsWith('-'))
|
|
||||||
return UnaryMinusSyntax(
|
|
||||||
operation = GroupOps.MINUS_OPERATION,
|
|
||||||
operand = OperandSyntax(operand = infty, parentheses = true),
|
|
||||||
)
|
|
||||||
|
|
||||||
return infty
|
|
||||||
}
|
|
||||||
|
|
||||||
return printSignedNumberString(toString)
|
|
||||||
}
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance containing [Float], and [Double].
|
|
||||||
*/
|
|
||||||
public val Default: PrettyPrintFloats = PrettyPrintFloats(setOf(Float::class, Double::class))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Special printing for numeric types that are printed in form of *'-'? DIGIT+*.
|
|
||||||
*
|
|
||||||
* @property types The suitable types.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class PrettyPrintIntegers(public val types: Set<KClass<out Number>>) : RenderFeature {
|
|
||||||
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
|
||||||
if (node !is MST.Numeric || node.value::class !in types)
|
|
||||||
null
|
|
||||||
else
|
|
||||||
printSignedNumberString(node.value.toString())
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance containing [Byte], [Short], [Int], and [Long].
|
|
||||||
*/
|
|
||||||
public val Default: PrettyPrintIntegers =
|
|
||||||
PrettyPrintIntegers(setOf(Byte::class, Short::class, Int::class, Long::class))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Special printing for symbols meaning Pi.
|
|
||||||
*
|
|
||||||
* @property symbols The allowed symbols.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class PrettyPrintPi(public val symbols: Set<String>) : RenderFeature {
|
|
||||||
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
|
||||||
if (node !is Symbol || node.identity !in symbols)
|
|
||||||
null
|
|
||||||
else
|
|
||||||
SpecialSymbolSyntax(kind = SpecialSymbolSyntax.Kind.SMALL_PI)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance containing `pi`.
|
|
||||||
*/
|
|
||||||
public val Default: PrettyPrintPi = PrettyPrintPi(setOf("pi"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract printing of unary operations that discards [MST] if their operation is not in [operations] or its type is
|
|
||||||
* not [MST.Unary].
|
|
||||||
*
|
|
||||||
* @param operations the allowed operations. If `null`, any operation is accepted.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public abstract class Unary(public val operations: Collection<String>?) : RenderFeature {
|
|
||||||
/**
|
|
||||||
* The actual render function specialized for [MST.Unary].
|
|
||||||
*/
|
|
||||||
protected abstract fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax?
|
|
||||||
|
|
||||||
public final override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
|
|
||||||
if (node !is MST.Unary || operations != null && node.operation !in operations)
|
|
||||||
null
|
|
||||||
else
|
|
||||||
renderUnary(renderer, node)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract printing of unary operations that discards [MST] if their operation is not in [operations] or its type is
|
|
||||||
* not [MST.Binary].
|
|
||||||
*
|
|
||||||
* @property operations the allowed operations. If `null`, any operation is accepted.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public abstract class Binary(public val operations: Collection<String>?) : RenderFeature {
|
|
||||||
/**
|
|
||||||
* The actual render function specialized for [MST.Binary].
|
|
||||||
*/
|
|
||||||
protected abstract fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax?
|
|
||||||
|
|
||||||
public final override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? {
|
|
||||||
if (node !is MST.Binary || operations != null && node.operation !in operations) return null
|
|
||||||
return renderBinary(renderer, node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [BinaryPlusSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
|
|
||||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
|
||||||
BinaryPlusSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
left = OperandSyntax(parent.render(node.left), true),
|
|
||||||
right = OperandSyntax(parent.render(node.right), true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [GroupOps.PLUS_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: BinaryPlus = BinaryPlus(setOf(GroupOps.PLUS_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [BinaryMinusSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
|
|
||||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
|
||||||
BinaryMinusSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
|
|
||||||
right = OperandSyntax(operand = parent.render(node.right), parentheses = true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [GroupOps.MINUS_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: BinaryMinus = BinaryMinus(setOf(GroupOps.MINUS_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles unary nodes by producing [UnaryPlusSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [GroupOps.PLUS_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: UnaryPlus = UnaryPlus(setOf(GroupOps.PLUS_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [UnaryMinusSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [GroupOps.MINUS_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: UnaryMinus = UnaryMinus(setOf(GroupOps.MINUS_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [FractionSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class Fraction(operations: Collection<String>?) : Binary(operations) {
|
|
||||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
|
|
||||||
right = OperandSyntax(operand = parent.render(node.right), parentheses = true),
|
|
||||||
infix = true,
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [FieldOps.DIV_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: Fraction = Fraction(setOf(FieldOps.DIV_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [BinaryOperatorSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class BinaryOperator(operations: Collection<String>?) : Binary(operations) {
|
|
||||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
|
||||||
BinaryOperatorSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
prefix = OperatorNameSyntax(name = node.operation),
|
|
||||||
left = parent.render(node.left),
|
|
||||||
right = parent.render(node.right),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with `null`.
|
|
||||||
*/
|
|
||||||
public val Default: BinaryOperator = BinaryOperator(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles unary nodes by producing [UnaryOperatorSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class UnaryOperator(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
|
||||||
UnaryOperatorSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
prefix = OperatorNameSyntax(node.operation),
|
|
||||||
operand = OperandSyntax(parent.render(node.value), true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with `null`.
|
|
||||||
*/
|
|
||||||
public val Default: UnaryOperator = UnaryOperator(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [SuperscriptSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class Power(operations: Collection<String>?) : Binary(operations) {
|
|
||||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
|
||||||
SuperscriptSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
left = OperandSyntax(parent.render(node.left), true),
|
|
||||||
right = OperandSyntax(parent.render(node.right), true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [PowerOperations.POW_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: Power = Power(setOf(PowerOperations.POW_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [RadicalSyntax] with no index.
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
|
||||||
RadicalSyntax(operation = node.operation, operand = parent.render(node.value))
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [PowerOperations.SQRT_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: SquareRoot = SquareRoot(setOf(PowerOperations.SQRT_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles unary nodes by producing [ExponentSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class Exponent(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
|
||||||
useOperatorForm = true,
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [ExponentialOperations.EXP_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: Exponent = Exponent(setOf(ExponentialOperations.EXP_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing [MultiplicationSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class Multiplication(operations: Collection<String>?) : Binary(operations) {
|
|
||||||
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
|
|
||||||
MultiplicationSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
|
|
||||||
right = OperandSyntax(operand = parent.render(node.right), parentheses = true),
|
|
||||||
times = true,
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [RingOps.TIMES_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: Multiplication = Multiplication(setOf(RingOps.TIMES_OPERATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing inverse [UnaryOperatorSyntax] with *arc* prefix instead of *a*.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class InverseTrigonometricOperations(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
|
||||||
UnaryOperatorSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "arc")),
|
|
||||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [TrigonometricOperations.ACOS_OPERATION],
|
|
||||||
* [TrigonometricOperations.ASIN_OPERATION], [TrigonometricOperations.ATAN_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: InverseTrigonometricOperations = InverseTrigonometricOperations(setOf(
|
|
||||||
TrigonometricOperations.ACOS_OPERATION,
|
|
||||||
TrigonometricOperations.ASIN_OPERATION,
|
|
||||||
TrigonometricOperations.ATAN_OPERATION,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles binary nodes by producing inverse [UnaryOperatorSyntax] with *ar* prefix instead of *a*.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class InverseHyperbolicOperations(operations: Collection<String>?) : Unary(operations) {
|
|
||||||
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
|
|
||||||
UnaryOperatorSyntax(
|
|
||||||
operation = node.operation,
|
|
||||||
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "ar")),
|
|
||||||
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
|
|
||||||
)
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default instance configured with [ExponentialOperations.ACOSH_OPERATION],
|
|
||||||
* [ExponentialOperations.ASINH_OPERATION], and [ExponentialOperations.ATANH_OPERATION].
|
|
||||||
*/
|
|
||||||
public val Default: InverseHyperbolicOperations = InverseHyperbolicOperations(setOf(
|
|
||||||
ExponentialOperations.ACOSH_OPERATION,
|
|
||||||
ExponentialOperations.ASINH_OPERATION,
|
|
||||||
ExponentialOperations.ATANH_OPERATION,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
internal expect fun Double.multiplatformToString(): String
|
|
||||||
internal expect fun Float.multiplatformToString(): String
|
|
@ -1,320 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess.PostProcessPhase
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.operations.FieldOps
|
|
||||||
import space.kscience.kmath.operations.GroupOps
|
|
||||||
import space.kscience.kmath.operations.PowerOperations
|
|
||||||
import space.kscience.kmath.operations.RingOps
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes unnecessary times (×) symbols from [MultiplicationSyntax].
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public val BetterMultiplication: PostProcessPhase = PostProcessPhase { node ->
|
|
||||||
fun perform(node: MathSyntax): Unit = when (node) {
|
|
||||||
is NumberSyntax -> Unit
|
|
||||||
is SymbolSyntax -> Unit
|
|
||||||
is OperatorNameSyntax -> Unit
|
|
||||||
is SpecialSymbolSyntax -> Unit
|
|
||||||
is OperandSyntax -> perform(node.operand)
|
|
||||||
|
|
||||||
is UnaryOperatorSyntax -> {
|
|
||||||
perform(node.prefix)
|
|
||||||
perform(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryPlusSyntax -> perform(node.operand)
|
|
||||||
is UnaryMinusSyntax -> perform(node.operand)
|
|
||||||
is RadicalSyntax -> perform(node.operand)
|
|
||||||
is ExponentSyntax -> perform(node.operand)
|
|
||||||
|
|
||||||
is SuperscriptSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is SubscriptSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryOperatorSyntax -> {
|
|
||||||
perform(node.prefix)
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryPlusSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryMinusSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is FractionSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalWithIndexSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is MultiplicationSyntax -> {
|
|
||||||
node.times = node.right.operand is NumberSyntax && !node.right.parentheses
|
|
||||||
|| node.left.operand is NumberSyntax && node.right.operand is FractionSyntax
|
|
||||||
|| node.left.operand is NumberSyntax && node.right.operand is NumberSyntax
|
|
||||||
|| node.left.operand is NumberSyntax && node.right.operand is SuperscriptSyntax && node.right.operand.left is NumberSyntax
|
|
||||||
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
perform(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Chooses [FractionSyntax.infix] depending on the context.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public val BetterFraction: PostProcessPhase = PostProcessPhase { node ->
|
|
||||||
fun perform(node: MathSyntax, infix: Boolean = false): Unit = when (node) {
|
|
||||||
is NumberSyntax -> Unit
|
|
||||||
is SymbolSyntax -> Unit
|
|
||||||
is OperatorNameSyntax -> Unit
|
|
||||||
is SpecialSymbolSyntax -> Unit
|
|
||||||
is OperandSyntax -> perform(node.operand, infix)
|
|
||||||
|
|
||||||
is UnaryOperatorSyntax -> {
|
|
||||||
perform(node.prefix, infix)
|
|
||||||
perform(node.operand, infix)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryPlusSyntax -> perform(node.operand, infix)
|
|
||||||
is UnaryMinusSyntax -> perform(node.operand, infix)
|
|
||||||
is RadicalSyntax -> perform(node.operand, infix)
|
|
||||||
is ExponentSyntax -> perform(node.operand, infix)
|
|
||||||
|
|
||||||
is SuperscriptSyntax -> {
|
|
||||||
perform(node.left, true)
|
|
||||||
perform(node.right, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is SubscriptSyntax -> {
|
|
||||||
perform(node.left, true)
|
|
||||||
perform(node.right, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryOperatorSyntax -> {
|
|
||||||
perform(node.prefix, infix)
|
|
||||||
perform(node.left, infix)
|
|
||||||
perform(node.right, infix)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryPlusSyntax -> {
|
|
||||||
perform(node.left, infix)
|
|
||||||
perform(node.right, infix)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryMinusSyntax -> {
|
|
||||||
perform(node.left, infix)
|
|
||||||
perform(node.right, infix)
|
|
||||||
}
|
|
||||||
|
|
||||||
is FractionSyntax -> {
|
|
||||||
node.infix = infix
|
|
||||||
perform(node.left, infix)
|
|
||||||
perform(node.right, infix)
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalWithIndexSyntax -> {
|
|
||||||
perform(node.left, true)
|
|
||||||
perform(node.right, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is MultiplicationSyntax -> {
|
|
||||||
perform(node.left, infix)
|
|
||||||
perform(node.right, infix)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
perform(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Applies [ExponentSyntax.useOperatorForm] to [ExponentSyntax] when the operand contains a fraction, a
|
|
||||||
* superscript or a subscript to improve readability.
|
|
||||||
*
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public val BetterExponent: PostProcessPhase = PostProcessPhase { node ->
|
|
||||||
fun perform(node: MathSyntax): Boolean {
|
|
||||||
return when (node) {
|
|
||||||
is NumberSyntax -> false
|
|
||||||
is SymbolSyntax -> false
|
|
||||||
is OperatorNameSyntax -> false
|
|
||||||
is SpecialSymbolSyntax -> false
|
|
||||||
is OperandSyntax -> perform(node.operand)
|
|
||||||
is UnaryOperatorSyntax -> perform(node.prefix) || perform(node.operand)
|
|
||||||
is UnaryPlusSyntax -> perform(node.operand)
|
|
||||||
is UnaryMinusSyntax -> perform(node.operand)
|
|
||||||
is RadicalSyntax -> true
|
|
||||||
|
|
||||||
is ExponentSyntax -> {
|
|
||||||
val r = perform(node.operand)
|
|
||||||
node.useOperatorForm = r
|
|
||||||
r
|
|
||||||
}
|
|
||||||
|
|
||||||
is SuperscriptSyntax -> true
|
|
||||||
is SubscriptSyntax -> true
|
|
||||||
is BinaryOperatorSyntax -> perform(node.prefix) || perform(node.left) || perform(node.right)
|
|
||||||
is BinaryPlusSyntax -> perform(node.left) || perform(node.right)
|
|
||||||
is BinaryMinusSyntax -> perform(node.left) || perform(node.right)
|
|
||||||
is FractionSyntax -> true
|
|
||||||
is RadicalWithIndexSyntax -> true
|
|
||||||
is MultiplicationSyntax -> perform(node.left) || perform(node.right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
perform(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes unnecessary parentheses from [OperandSyntax].
|
|
||||||
*
|
|
||||||
* @property precedenceFunction Returns the precedence number for syntax node. Higher number is lower priority.
|
|
||||||
* @author Iaroslav Postovalov
|
|
||||||
*/
|
|
||||||
@UnstableKMathAPI
|
|
||||||
public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) -> Int) :
|
|
||||||
PostProcessPhase {
|
|
||||||
override fun perform(node: MathSyntax): Unit = when (node) {
|
|
||||||
is NumberSyntax -> Unit
|
|
||||||
is SymbolSyntax -> Unit
|
|
||||||
is OperatorNameSyntax -> Unit
|
|
||||||
is SpecialSymbolSyntax -> Unit
|
|
||||||
|
|
||||||
is OperandSyntax -> {
|
|
||||||
val isRightOfSuperscript =
|
|
||||||
(node.parent is SuperscriptSyntax) && (node.parent as SuperscriptSyntax).right === node
|
|
||||||
|
|
||||||
val precedence = precedenceFunction(node.operand)
|
|
||||||
|
|
||||||
val needParenthesesByPrecedence = when (val parent = node.parent) {
|
|
||||||
null -> false
|
|
||||||
|
|
||||||
is BinarySyntax -> {
|
|
||||||
val parentPrecedence = precedenceFunction(parent)
|
|
||||||
|
|
||||||
parentPrecedence < precedence ||
|
|
||||||
parentPrecedence == precedence && parentPrecedence != 0 && node === parent.right
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> precedence > precedenceFunction(parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
val isInsideExpOperator =
|
|
||||||
node.parent is ExponentSyntax && (node.parent as ExponentSyntax).useOperatorForm
|
|
||||||
|
|
||||||
val isOnOrUnderNormalFraction = node.parent is FractionSyntax && !((node.parent as FractionSyntax).infix)
|
|
||||||
|
|
||||||
node.parentheses = !isRightOfSuperscript
|
|
||||||
&& (needParenthesesByPrecedence || node.parent is UnaryOperatorSyntax || isInsideExpOperator)
|
|
||||||
&& !isOnOrUnderNormalFraction
|
|
||||||
|
|
||||||
perform(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryOperatorSyntax -> {
|
|
||||||
perform(node.prefix)
|
|
||||||
perform(node.operand)
|
|
||||||
}
|
|
||||||
|
|
||||||
is UnaryPlusSyntax -> perform(node.operand)
|
|
||||||
is UnaryMinusSyntax -> perform(node.operand)
|
|
||||||
is RadicalSyntax -> perform(node.operand)
|
|
||||||
is ExponentSyntax -> perform(node.operand)
|
|
||||||
|
|
||||||
is SuperscriptSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is SubscriptSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryOperatorSyntax -> {
|
|
||||||
perform(node.prefix)
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryPlusSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BinaryMinusSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is FractionSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is MultiplicationSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
|
|
||||||
is RadicalWithIndexSyntax -> {
|
|
||||||
perform(node.left)
|
|
||||||
perform(node.right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public companion object {
|
|
||||||
/**
|
|
||||||
* The default configuration of [SimplifyParentheses] where power is 1, multiplicative operations are 2,
|
|
||||||
* additive operations are 3.
|
|
||||||
*/
|
|
||||||
public val Default: SimplifyParentheses = SimplifyParentheses {
|
|
||||||
when (it) {
|
|
||||||
is TerminalSyntax -> 0
|
|
||||||
is UnarySyntax -> 2
|
|
||||||
|
|
||||||
is BinarySyntax -> when (it.operation) {
|
|
||||||
PowerOperations.POW_OPERATION -> 1
|
|
||||||
RingOps.TIMES_OPERATION -> 3
|
|
||||||
FieldOps.DIV_OPERATION -> 3
|
|
||||||
GroupOps.MINUS_OPERATION -> 4
|
|
||||||
GroupOps.PLUS_OPERATION -> 4
|
|
||||||
else -> 0
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.MstField
|
|
||||||
import space.kscience.kmath.expressions.MstRing
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.interpret
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.IntRing
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
internal class TestCompilerConsistencyWithInterpreter {
|
|
||||||
@Test
|
|
||||||
fun intRing() = runCompilerTest {
|
|
||||||
val mst = MstRing {
|
|
||||||
binaryOperationFunction("+")(
|
|
||||||
unaryOperationFunction("+")(
|
|
||||||
(x - (2.toByte() + (scale(
|
|
||||||
add(number(1), number(1)),
|
|
||||||
2.0,
|
|
||||||
) + 1.toByte()))) * 3.0 - 1.toByte()
|
|
||||||
),
|
|
||||||
|
|
||||||
number(1),
|
|
||||||
) * number(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
mst.interpret(IntRing, x to 3),
|
|
||||||
mst.compile(IntRing, x to 3),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun doubleField() = runCompilerTest {
|
|
||||||
val mst = MstField {
|
|
||||||
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperationFunction("+")(
|
|
||||||
(3.0 - (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
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
mst.interpret(DoubleField, x to 2.0),
|
|
||||||
mst.compile(DoubleField, x to 2.0),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,88 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.MstExtendedField
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.invoke
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
internal class TestCompilerOperations {
|
|
||||||
@Test
|
|
||||||
fun testUnaryPlus() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { +x }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(2.0, expr(x to 2.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testUnaryMinus() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { -x }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(-2.0, expr(x to 2.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAdd() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { x + x }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(4.0, expr(x to 2.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testSine() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { sin(x) }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(x to 0.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testCosine() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { cos(x) }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(1.0, expr(x to 0.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testTangent() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { tan(x) }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(x to 0.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testArcSine() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { asin(x) }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(x to 0.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testArcCosine() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { acos(x) }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(x to 1.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testAreaHyperbolicSine() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { asinh(x) }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(x to 0.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testSubtract() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { x - x }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(x to 2.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testDivide() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { x / x }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(1.0, expr(x to 2.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testPower() = runCompilerTest {
|
|
||||||
val expr = MstExtendedField { x pow 2 }.compileToExpression(DoubleField)
|
|
||||||
assertEquals(4.0, expr(x to 2.0))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.MstRing
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.x
|
|
||||||
import space.kscience.kmath.expressions.Symbol.Companion.y
|
|
||||||
import space.kscience.kmath.expressions.invoke
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.IntRing
|
|
||||||
import space.kscience.kmath.operations.invoke
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertFailsWith
|
|
||||||
|
|
||||||
internal class TestCompilerVariables {
|
|
||||||
@Test
|
|
||||||
fun testNoVariables() = runCompilerTest {
|
|
||||||
val expr = "0".parseMath().compileToExpression(DoubleField)
|
|
||||||
assertEquals(0.0, expr(), 0.0001)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testOneVariable() = runCompilerTest {
|
|
||||||
val expr = MstRing { x }.compileToExpression(IntRing)
|
|
||||||
assertEquals(1, expr(x to 1))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testTwoVariables() = runCompilerTest {
|
|
||||||
val expr = "y+x/y+x".parseMath().compileToExpression(DoubleField)
|
|
||||||
assertEquals(8.0, expr(x to 4.0, y to 2.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testUndefinedVariableFails() = runCompilerTest {
|
|
||||||
val expr = MstRing { x }.compileToExpression(IntRing)
|
|
||||||
assertFailsWith<NoSuchElementException> { expr() }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.ByteRing
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.IntRing
|
|
||||||
import space.kscience.kmath.operations.pi
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.fail
|
|
||||||
|
|
||||||
internal class TestFolding {
|
|
||||||
@Test
|
|
||||||
fun foldUnary() = assertEquals(
|
|
||||||
-1,
|
|
||||||
("-(1)".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant<Int> ?: fail()).value,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun foldDeepUnary() = assertEquals(
|
|
||||||
1,
|
|
||||||
("-(-(1))".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant<Int> ?: fail()).value,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun foldBinary() = assertEquals(
|
|
||||||
2,
|
|
||||||
("1*2".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant<Int> ?: fail()).value,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun foldDeepBinary() = assertEquals(
|
|
||||||
10,
|
|
||||||
("1*2*5".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant<Int> ?: fail()).value,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun foldSymbol() = assertEquals(
|
|
||||||
DoubleField.pi,
|
|
||||||
("pi".parseMath().evaluateConstants(DoubleField) as? TypedMst.Constant<Double> ?: fail()).value,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun foldNumeric() = assertEquals(
|
|
||||||
42.toByte(),
|
|
||||||
("42".parseMath().evaluateConstants(ByteRing) as? TypedMst.Constant<Byte> ?: fail()).value,
|
|
||||||
)
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.complex.Complex
|
|
||||||
import space.kscience.kmath.complex.ComplexField
|
|
||||||
import space.kscience.kmath.expressions.interpret
|
|
||||||
import space.kscience.kmath.operations.Algebra
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
internal class TestParser {
|
|
||||||
@Test
|
|
||||||
fun evaluateParsedMst() {
|
|
||||||
val mst = "2+2*(2+2)".parseMath()
|
|
||||||
val res = mst.interpret(ComplexField)
|
|
||||||
assertEquals(Complex(10.0, 0.0), res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun evaluateMstSymbol() {
|
|
||||||
val mst = "i".parseMath()
|
|
||||||
val res = mst.interpret(ComplexField)
|
|
||||||
assertEquals(ComplexField.i, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun evaluateMstUnary() {
|
|
||||||
val mst = "sin(0)".parseMath()
|
|
||||||
val res = mst.interpret(DoubleField)
|
|
||||||
assertEquals(0.0, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun evaluateMstBinary() {
|
|
||||||
val magicalAlgebra = object : Algebra<String> {
|
|
||||||
override fun bindSymbolOrNull(value: String): String = value
|
|
||||||
|
|
||||||
override fun unaryOperationFunction(operation: String): (arg: String) -> String {
|
|
||||||
throw NotImplementedError()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun binaryOperationFunction(operation: String): (left: String, right: String) -> String =
|
|
||||||
when (operation) {
|
|
||||||
"magic" -> { left, right -> "$left ★ $right" }
|
|
||||||
else -> throw NotImplementedError()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val mst = "magic(a, b)".parseMath()
|
|
||||||
val res = mst.interpret(magicalAlgebra)
|
|
||||||
assertEquals("a ★ b", res)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.interpret
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
internal class TestParserPrecedence {
|
|
||||||
@Test
|
|
||||||
fun test1(): Unit = assertEquals(6.0, "2*2+2".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test2(): Unit = assertEquals(6.0, "2+2*2".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test3(): Unit = assertEquals(10.0, "2^3+2".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test4(): Unit = assertEquals(10.0, "2+2^3".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test5(): Unit = assertEquals(16.0, "2^3*2".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test6(): Unit = assertEquals(16.0, "2*2^3".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test7(): Unit = assertEquals(18.0, "2+2^3*2".parseMath().interpret(f))
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test8(): Unit = assertEquals(18.0, "2*2^3+2".parseMath().interpret(f))
|
|
||||||
|
|
||||||
private companion object {
|
|
||||||
private val f = DoubleField
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,120 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
|
|
||||||
import space.kscience.kmath.expressions.MST.Numeric
|
|
||||||
import kotlin.test.Test
|
|
||||||
|
|
||||||
internal class TestFeatures {
|
|
||||||
@Test
|
|
||||||
fun printSymbolic() = testLatex("x", "x")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun printNumeric() {
|
|
||||||
val num = object : Number() {
|
|
||||||
override fun toByte(): Byte = throw UnsupportedOperationException()
|
|
||||||
override fun toChar(): Char = throw UnsupportedOperationException()
|
|
||||||
override fun toDouble(): Double = throw UnsupportedOperationException()
|
|
||||||
override fun toFloat(): Float = throw UnsupportedOperationException()
|
|
||||||
override fun toInt(): Int = throw UnsupportedOperationException()
|
|
||||||
override fun toLong(): Long = throw UnsupportedOperationException()
|
|
||||||
override fun toShort(): Short = throw UnsupportedOperationException()
|
|
||||||
override fun toString(): String = "foo"
|
|
||||||
}
|
|
||||||
|
|
||||||
testLatex(Numeric(num), "foo")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun prettyPrintFloats() {
|
|
||||||
testLatex(Numeric(Double.NaN), "NaN")
|
|
||||||
testLatex(Numeric(Double.POSITIVE_INFINITY), "\\infty")
|
|
||||||
testLatex(Numeric(Double.NEGATIVE_INFINITY), "-\\infty")
|
|
||||||
testLatex(Numeric(1.0), "1")
|
|
||||||
testLatex(Numeric(-1.0), "-1")
|
|
||||||
testLatex(Numeric(1.42), "1.42")
|
|
||||||
testLatex(Numeric(-1.42), "-1.42")
|
|
||||||
testLatex(Numeric(1.1e10), "1.1\\times10^{10}")
|
|
||||||
testLatex(Numeric(1.1e-10), "1.1\\times10^{-10}")
|
|
||||||
testLatex(Numeric(-1.1e-10), "-1.1\\times10^{-10}")
|
|
||||||
testLatex(Numeric(-1.1e10), "-1.1\\times10^{10}")
|
|
||||||
testLatex(Numeric(0.001), "0.001")
|
|
||||||
testLatex(Numeric(0.0000001), "1\\times10^{-7}")
|
|
||||||
|
|
||||||
testLatex(Numeric(Float.NaN), "NaN")
|
|
||||||
testLatex(Numeric(Float.POSITIVE_INFINITY), "\\infty")
|
|
||||||
testLatex(Numeric(Float.NEGATIVE_INFINITY), "-\\infty")
|
|
||||||
testLatex(Numeric(1.0f), "1")
|
|
||||||
testLatex(Numeric(-1.0f), "-1")
|
|
||||||
testLatex(Numeric(1.42f), "1.42")
|
|
||||||
testLatex(Numeric(-1.42f), "-1.42")
|
|
||||||
testLatex(Numeric(1e10f), "1\\times10^{10}")
|
|
||||||
testLatex(Numeric(1e-10f), "1\\times10^{-10}")
|
|
||||||
testLatex(Numeric(-1e-10f), "-1\\times10^{-10}")
|
|
||||||
testLatex(Numeric(-1e10f), "-1\\times10^{10}")
|
|
||||||
testLatex(Numeric(0.001f), "0.001")
|
|
||||||
testLatex(Numeric(0.0000001f), "1\\times10^{-7}")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun prettyPrintIntegers() {
|
|
||||||
testLatex(Numeric(42), "42")
|
|
||||||
testLatex(Numeric(-42), "-42")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun prettyPrintPi() {
|
|
||||||
testLatex("pi", "\\pi")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryPlus() = testLatex("2+2", "2+2")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryMinus() = testLatex("2-2", "2-2")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun fraction() = testLatex("2/2", "\\frac{2}{2}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryOperator() = testLatex("f(x, y)", "\\operatorname{f}\\left(x,y\\right)")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryOperator() = testLatex("f(x)", "\\operatorname{f}\\,\\left(x\\right)")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun power() = testLatex("x^y", "x^{y}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun squareRoot() = testLatex("sqrt(x)", "\\sqrt{x}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun exponential() = testLatex("exp(x)", "e^{x}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun multiplication() = testLatex("x*1", "x\\times1")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun inverseTrigonometric() {
|
|
||||||
testLatex("asin(x)", "\\operatorname{arcsin}\\,\\left(x\\right)")
|
|
||||||
testLatex("acos(x)", "\\operatorname{arccos}\\,\\left(x\\right)")
|
|
||||||
testLatex("atan(x)", "\\operatorname{arctan}\\,\\left(x\\right)")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun inverseHyperbolic() {
|
|
||||||
testLatex("asinh(x)", "\\operatorname{arsinh}\\,\\left(x\\right)")
|
|
||||||
testLatex("acosh(x)", "\\operatorname{arcosh}\\,\\left(x\\right)")
|
|
||||||
testLatex("atanh(x)", "\\operatorname{artanh}\\,\\left(x\\right)")
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// fun unaryPlus() {
|
|
||||||
// testLatex("+1", "+1")
|
|
||||||
// testLatex("+1", "++1")
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.operations.GroupOps
|
|
||||||
import kotlin.test.Test
|
|
||||||
|
|
||||||
internal class TestLatex {
|
|
||||||
@Test
|
|
||||||
fun number() = testLatex("42", "42")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun symbol() = testLatex("x", "x")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun operatorName() = testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun specialSymbol() {
|
|
||||||
testLatex(MST.Numeric(Double.POSITIVE_INFINITY), "\\infty")
|
|
||||||
testLatex("pi", "\\pi")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun operand() {
|
|
||||||
testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
|
|
||||||
testLatex("1+1", "1+1")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryOperator() = testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryPlus() = testLatex(MST.Unary(GroupOps.PLUS_OPERATION, MST.Numeric(1)), "+1")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryMinus() = testLatex("-x", "-x")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun radical() = testLatex("sqrt(x)", "\\sqrt{x}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun superscript() = testLatex("x^y", "x^{y}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun subscript() = testLatex(SubscriptSyntax("", SymbolSyntax("x"), NumberSyntax("123")), "x_{123}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryOperator() = testLatex("f(x, y)", "\\operatorname{f}\\left(x,y\\right)")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryPlus() = testLatex("x+x", "x+x")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryMinus() = testLatex("x-x", "x-x")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun fraction() = testLatex("x/x", "\\frac{x}{x}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun radicalWithIndex() = testLatex(RadicalWithIndexSyntax("", SymbolSyntax("x"), SymbolSyntax("y")), "\\sqrt[x]{y}")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun multiplication() {
|
|
||||||
testLatex("x*1", "x\\times1")
|
|
||||||
testLatex("1*x", "1\\,x")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,92 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.TestUtils.testMathML
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.operations.GroupOps
|
|
||||||
import kotlin.test.Test
|
|
||||||
|
|
||||||
internal class TestMathML {
|
|
||||||
@Test
|
|
||||||
fun number() = testMathML("42", "<mn>42</mn>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun symbol() = testMathML("x", "<mi>x</mi>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun operatorName() = testMathML(
|
|
||||||
"sin(1)",
|
|
||||||
"<mo>sin</mo><mspace width=\"0.167em\"></mspace><mfenced open=\"(\" close=\")\" separators=\"\"><mn>1</mn></mfenced>",
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun specialSymbol() {
|
|
||||||
testMathML(MST.Numeric(Double.POSITIVE_INFINITY), "<mo>∞</mo>")
|
|
||||||
testMathML("pi", "<mo>π</mo>")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun operand() {
|
|
||||||
testMathML(
|
|
||||||
"sin(1)",
|
|
||||||
"<mo>sin</mo><mspace width=\"0.167em\"></mspace><mfenced open=\"(\" close=\")\" separators=\"\"><mn>1</mn></mfenced>",
|
|
||||||
)
|
|
||||||
|
|
||||||
testMathML("1+1", "<mn>1</mn><mo>+</mo><mn>1</mn>")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryOperator() = testMathML(
|
|
||||||
"sin(1)",
|
|
||||||
"<mo>sin</mo><mspace width=\"0.167em\"></mspace><mfenced open=\"(\" close=\")\" separators=\"\"><mn>1</mn></mfenced>",
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryPlus() =
|
|
||||||
testMathML(MST.Unary(GroupOps.PLUS_OPERATION, MST.Numeric(1)), "<mo>+</mo><mn>1</mn>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun unaryMinus() = testMathML("-x", "<mo>-</mo><mi>x</mi>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun radical() = testMathML("sqrt(x)", "<msqrt><mi>x</mi></msqrt>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun superscript() = testMathML("x^y", "<msup><mrow><mi>x</mi></mrow><mrow><mi>y</mi></mrow></msup>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun subscript() = testMathML(
|
|
||||||
SubscriptSyntax("", SymbolSyntax("x"), NumberSyntax("123")),
|
|
||||||
"<msub><mrow><mi>x</mi></mrow><mrow><mn>123</mn></mrow></msub>",
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryOperator() = testMathML(
|
|
||||||
"f(x, y)",
|
|
||||||
"<mo>f</mo><mfenced open=\"(\" close=\")\" separators=\"\"><mi>x</mi><mo>,</mo><mi>y</mi></mfenced>",
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryPlus() = testMathML("x+x", "<mi>x</mi><mo>+</mo><mi>x</mi>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun binaryMinus() = testMathML("x-x", "<mi>x</mi><mo>-</mo><mi>x</mi>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun fraction() = testMathML("x/x", "<mfrac><mrow><mi>x</mi></mrow><mrow><mi>x</mi></mrow></mfrac>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun radicalWithIndex() =
|
|
||||||
testMathML(RadicalWithIndexSyntax("", SymbolSyntax("x"), SymbolSyntax("y")),
|
|
||||||
"<mroot><mrow><mi>y</mi></mrow><mrow><mi>x</mi></mrow></mroot>")
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun multiplication() {
|
|
||||||
testMathML("x*1", "<mi>x</mi><mo>×</mo><mn>1</mn>")
|
|
||||||
testMathML("1*x", "<mn>1</mn><mspace width=\"0.167em\"></mspace><mi>x</mi>")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
|
|
||||||
import kotlin.test.Test
|
|
||||||
|
|
||||||
internal class TestStages {
|
|
||||||
@Test
|
|
||||||
fun betterMultiplication() {
|
|
||||||
testLatex("a*1", "a\\times1")
|
|
||||||
testLatex("1*(2/3)", "1\\times\\left(\\frac{2}{3}\\right)")
|
|
||||||
testLatex("1*1", "1\\times1")
|
|
||||||
testLatex("2e10", "2\\times10^{10}")
|
|
||||||
testLatex("2*x", "2\\,x")
|
|
||||||
testLatex("2*(x+1)", "2\\,\\left(x+1\\right)")
|
|
||||||
testLatex("x*y", "x\\,y")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun parentheses() {
|
|
||||||
testLatex("(x+1)", "x+1")
|
|
||||||
testLatex("x*x*x", "x\\,x\\,x")
|
|
||||||
testLatex("(x+x)*x", "\\left(x+x\\right)\\,x")
|
|
||||||
testLatex("x+x*x", "x+x\\,x")
|
|
||||||
testLatex("x+x^x*x+x", "x+x^{x}\\,x+x")
|
|
||||||
testLatex("(x+x)^x+x*x", "\\left(x+x\\right)^{x}+x\\,x")
|
|
||||||
testLatex("x^(x+x)", "x^{x+x}")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun exponent() {
|
|
||||||
testLatex("exp(x)", "e^{x}")
|
|
||||||
testLatex("exp(x/2)", "\\operatorname{exp}\\,\\left(\\frac{x}{2}\\right)")
|
|
||||||
testLatex("exp(x^2)", "\\operatorname{exp}\\,\\left(x^{2}\\right)")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun fraction() {
|
|
||||||
testLatex("x/y", "\\frac{x}{y}")
|
|
||||||
testLatex("x^(x/y)", "x^{x/y}")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.parseMath
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
internal object TestUtils {
|
|
||||||
private fun mathSyntax(mst: MST) = FeaturedMathRendererWithPostProcess.Default.render(mst)
|
|
||||||
private fun latex(mst: MST) = LatexSyntaxRenderer.renderWithStringBuilder(mathSyntax(mst))
|
|
||||||
private fun mathML(mst: MST) = MathMLSyntaxRenderer.renderWithStringBuilder(mathSyntax(mst))
|
|
||||||
|
|
||||||
internal fun testLatex(mst: MST, expectedLatex: String) = assertEquals(
|
|
||||||
expected = expectedLatex,
|
|
||||||
actual = latex(mst),
|
|
||||||
)
|
|
||||||
|
|
||||||
internal fun testLatex(expression: String, expectedLatex: String) = assertEquals(
|
|
||||||
expected = expectedLatex,
|
|
||||||
actual = latex(expression.parseMath()),
|
|
||||||
)
|
|
||||||
|
|
||||||
internal fun testLatex(expression: MathSyntax, expectedLatex: String) = assertEquals(
|
|
||||||
expected = expectedLatex,
|
|
||||||
actual = LatexSyntaxRenderer.renderWithStringBuilder(expression),
|
|
||||||
)
|
|
||||||
|
|
||||||
internal fun testMathML(mst: MST, expectedMathML: String) = assertEquals(
|
|
||||||
expected = "<math xmlns=\"https://www.w3.org/1998/Math/MathML\"><mrow>$expectedMathML</mrow></math>",
|
|
||||||
actual = mathML(mst),
|
|
||||||
)
|
|
||||||
|
|
||||||
internal fun testMathML(expression: String, expectedMathML: String) = assertEquals(
|
|
||||||
expected = "<math xmlns=\"https://www.w3.org/1998/Math/MathML\"><mrow>$expectedMathML</mrow></math>",
|
|
||||||
actual = mathML(expression.parseMath()),
|
|
||||||
)
|
|
||||||
|
|
||||||
internal fun testMathML(expression: MathSyntax, expectedMathML: String) = assertEquals(
|
|
||||||
expected = "<math xmlns=\"https://www.w3.org/1998/Math/MathML\"><mrow>$expectedMathML</mrow></math>",
|
|
||||||
actual = MathMLSyntaxRenderer.renderWithStringBuilder(expression),
|
|
||||||
)
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.Expression
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.operations.DoubleField
|
|
||||||
import space.kscience.kmath.operations.IntRing
|
|
||||||
|
|
||||||
internal interface CompilerTestContext {
|
|
||||||
fun MST.compileToExpression(algebra: IntRing): Expression<Int>
|
|
||||||
fun MST.compile(algebra: IntRing, arguments: Map<Symbol, Int>): Int
|
|
||||||
fun MST.compile(algebra: IntRing, vararg arguments: Pair<Symbol, Int>): Int = compile(algebra, mapOf(*arguments))
|
|
||||||
fun MST.compileToExpression(algebra: DoubleField): Expression<Double>
|
|
||||||
fun MST.compile(algebra: DoubleField, arguments: Map<Symbol, Double>): Double
|
|
||||||
|
|
||||||
fun MST.compile(algebra: DoubleField, vararg arguments: Pair<Symbol, Double>): Double =
|
|
||||||
compile(algebra, mapOf(*arguments))
|
|
||||||
}
|
|
||||||
|
|
||||||
internal expect inline fun runCompilerTest(action: CompilerTestContext.() -> Unit)
|
|
@ -1,18 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.ast.rendering
|
|
||||||
|
|
||||||
internal actual fun Double.multiplatformToString(): String {
|
|
||||||
val d = this
|
|
||||||
if (d >= 1e7 || d <= -1e7) return js("d.toExponential()") as String
|
|
||||||
return toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal actual fun Float.multiplatformToString(): String {
|
|
||||||
val d = this
|
|
||||||
if (d >= 1e7f || d <= -1e7f) return js("d.toExponential()") as String
|
|
||||||
return toString()
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.estree
|
|
||||||
|
|
||||||
import space.kscience.kmath.ast.TypedMst
|
|
||||||
import space.kscience.kmath.ast.evaluateConstants
|
|
||||||
import space.kscience.kmath.estree.internal.ESTreeBuilder
|
|
||||||
import space.kscience.kmath.expressions.Expression
|
|
||||||
import space.kscience.kmath.expressions.MST
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.expressions.invoke
|
|
||||||
import space.kscience.kmath.internal.estree.BaseExpression
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.operations.Algebra
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a compiled expression with given [MST] and given [algebra].
|
|
||||||
*/
|
|
||||||
@OptIn(UnstableKMathAPI::class)
|
|
||||||
public fun <T : Any> MST.compileToExpression(algebra: Algebra<T>): Expression<T> {
|
|
||||||
val typed = evaluateConstants(algebra)
|
|
||||||
if (typed is TypedMst.Constant<T>) return Expression { typed.value }
|
|
||||||
|
|
||||||
fun ESTreeBuilder<T>.visit(node: TypedMst<T>): BaseExpression = when (node) {
|
|
||||||
is TypedMst.Constant -> constant(node.value)
|
|
||||||
is TypedMst.Variable -> variable(node.symbol)
|
|
||||||
is TypedMst.Unary -> call(node.function, visit(node.value))
|
|
||||||
|
|
||||||
is TypedMst.Binary -> call(
|
|
||||||
node.function,
|
|
||||||
visit(node.left),
|
|
||||||
visit(node.right),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ESTreeBuilder<T> { visit(typed) }.instance
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compile given MST to expression and evaluate it against [arguments]
|
|
||||||
*/
|
|
||||||
public fun <T : Any> MST.compile(algebra: Algebra<T>, arguments: Map<Symbol, T>): T =
|
|
||||||
compileToExpression(algebra)(arguments)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compile given MST to expression and evaluate it against [arguments]
|
|
||||||
*/
|
|
||||||
public fun <T : Any> MST.compile(algebra: Algebra<T>, vararg arguments: Pair<Symbol, T>): T =
|
|
||||||
compileToExpression(algebra)(*arguments)
|
|
@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2018-2021 KMath contributors.
|
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package space.kscience.kmath.estree.internal
|
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.Expression
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.internal.astring.generate
|
|
||||||
import space.kscience.kmath.internal.estree.*
|
|
||||||
|
|
||||||
internal class ESTreeBuilder<T>(val bodyCallback: ESTreeBuilder<T>.() -> BaseExpression) {
|
|
||||||
private class GeneratedExpression<T>(val executable: dynamic, val constants: Array<dynamic>) : Expression<T> {
|
|
||||||
@Suppress("UNUSED_VARIABLE")
|
|
||||||
override fun invoke(arguments: Map<Symbol, T>): T {
|
|
||||||
val e = executable
|
|
||||||
val c = constants
|
|
||||||
val a = js("{}")
|
|
||||||
arguments.forEach { (key, value) -> a[key.identity] = value }
|
|
||||||
return js("e(c, a)").unsafeCast<T>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val instance: Expression<T> by lazy {
|
|
||||||
val node = Program(
|
|
||||||
sourceType = "script",
|
|
||||||
VariableDeclaration(
|
|
||||||
kind = "var",
|
|
||||||
VariableDeclarator(
|
|
||||||
id = Identifier("executable"),
|
|
||||||
init = FunctionExpression(
|
|
||||||
params = arrayOf(Identifier("constants"), Identifier("arguments")),
|
|
||||||
body = BlockStatement(ReturnStatement(bodyCallback())),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
eval(generate(node))
|
|
||||||
GeneratedExpression(js("executable"), constants.toTypedArray())
|
|
||||||
}
|
|
||||||
|
|
||||||
private val constants = mutableListOf<Any>()
|
|
||||||
|
|
||||||
fun constant(value: Any?) = when {
|
|
||||||
value == null || jsTypeOf(value) == "number" || jsTypeOf(value) == "string" || jsTypeOf(value) == "boolean" ->
|
|
||||||
SimpleLiteral(value)
|
|
||||||
|
|
||||||
jsTypeOf(value) == "undefined" -> Identifier("undefined")
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
val idx = if (value in constants) constants.indexOf(value) else constants.also { it += value }.lastIndex
|
|
||||||
|
|
||||||
MemberExpression(
|
|
||||||
computed = true,
|
|
||||||
optional = false,
|
|
||||||
`object` = Identifier("constants"),
|
|
||||||
property = SimpleLiteral(idx),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun variable(name: Symbol): BaseExpression = call(getOrFail, Identifier("arguments"), SimpleLiteral(name.identity))
|
|
||||||
|
|
||||||
fun call(function: Function<T>, vararg args: BaseExpression): BaseExpression = SimpleCallExpression(
|
|
||||||
optional = false,
|
|
||||||
callee = constant(function),
|
|
||||||
*args,
|
|
||||||
)
|
|
||||||
|
|
||||||
private companion object {
|
|
||||||
@Suppress("UNUSED_VARIABLE")
|
|
||||||
val getOrFail: (`object`: dynamic, key: String) -> dynamic = { `object`, key ->
|
|
||||||
val k = key
|
|
||||||
val o = `object`
|
|
||||||
|
|
||||||
if (!(js("k in o") as Boolean))
|
|
||||||
throw NoSuchElementException("Key $key is missing in the map.")
|
|
||||||
|
|
||||||
js("o[k]")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user