diff --git a/.gitignore b/.gitignore index 96a556ae1..030625b50 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ out/ .idea/ .vscode/ .fleet/ +.kotlin/ # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c6523a0d..5c383fe95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,28 +3,62 @@ ## Unreleased ### Added -- Integer division algebras -- Float32 geometries ### Changed -- Default naming for algebra and buffers now uses IntXX/FloatXX notation instead of Java types. -- Remove unnecessary inlines in basic algebras. -- QuaternionField -> QuaternionAlgebra and does not implement `Field` anymore since it is non-commutative -- kmath-geometry is split into `euclidean2d` and `euclidean3d` ### Deprecated ### Removed -- `asPolynomial` function due to scope pollution ### Fixed -- Median statistics ### Security +## 0.4.0-dev-3 - 2024-02-18 + +### Added + +- Reification. Explicit `SafeType` for algebras. +- Integer division algebras. +- Float32 geometries. +- New Attributes-kt module that could be used as stand-alone. It declares. type-safe attributes containers. +- Explicit `mutableStructureND` builders for mutable structures. +- `Buffer.asList()` zero-copy transformation. +- Wasm support. +- Parallel implementation of `LinearSpace` for Float64 +- Parallel buffer factories + +### Changed + +- Buffer copy removed from API (added as an extension). +- Default naming for algebra and buffers now uses IntXX/FloatXX notation instead of Java types. +- Remove unnecessary inlines in basic algebras. +- QuaternionField -> QuaternionAlgebra and does not implement `Field` anymore since it is non-commutative +- kmath-geometry is split into `euclidean2d` and `euclidean3d` +- Features replaced with Attributes. +- Transposed refactored. +- Kmath-memory is moved on top of core. + +### Deprecated + +- ND4J engine + +### Removed + +- `asPolynomial` function due to scope pollution +- Codegend for ejml (450 lines of codegen for 1000 lines of code is too much) + +### Fixed + +- Median statistics +- Complex power of negative real numbers +- Add proper mutability for MutableBufferND rows and columns +- Generic Float32 and Float64 vectors are used in geometry algebras. + ## 0.3.1 - 2023-04-09 ### Added + - Wasm support for `memory`, `core`, `complex` and `functions` modules. - Generic builders for `BufferND` and `MutableBufferND` - `NamedMatrix` - matrix with symbol-based indexing @@ -34,6 +68,8 @@ - Algebra now has an obligatory `bufferFactory` (#477). ### Changed + +- Removed marker `Vector` type for geometry - Geometry uses type-safe angles - Tensor operations switched to prefix notation - Row-wise and column-wise ND shapes in the core @@ -45,12 +81,14 @@ - Multik went MPP ### Removed + - Trajectory moved to https://github.com/SciProgCentre/maps-kt - Polynomials moved to https://github.com/SciProgCentre/kmath-polynomial ## 0.3.0 ### Added + - `ScaleOperations` interface - `Field` extends `ScaleOperations` - Basic integration API @@ -75,6 +113,7 @@ - Compilation to TeX for MST: #254 ### Changed + - Annotations moved to `space.kscience.kmath` - Exponential operations merged with hyperbolic functions - Space is replaced by Group. Space is reserved for vector spaces. @@ -108,9 +147,11 @@ - `UnivariateFunction` -> `Function1D`, `MultivariateFunction` -> `FunctionND` ### 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. @@ -121,12 +162,14 @@ - Algebra elements are completely removed. Use algebra contexts instead. ### Fixed + - Ring inherits RingOperations, not GroupOperations - Univariate histogram filling ## 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) @@ -146,6 +189,7 @@ - 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`) @@ -170,6 +214,7 @@ - Add `out` projection to `Buffer` generic ### Removed + - `kmath-koma` module because it doesn't support Kotlin 1.4. - Support of `legacy` JS backend (we will support only IR) - `toGrid` method. @@ -178,11 +223,13 @@ - StructureND identity and equals ### Fixed + - `symbol` method in `MstExtendedField` (https://github.com/mipt-npm/kmath/pull/140) ## 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) @@ -200,6 +247,7 @@ - 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. @@ -211,6 +259,7 @@ - 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) diff --git a/README.md b/README.md index 7c1f759c1..00ba00ede 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![DOI](https://zenodo.org/badge/129486382.svg)](https://zenodo.org/badge/latestdoi/129486382) ![Gradle build](https://github.com/SciProgCentre/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/spc/p/sci/maven/space/kscience/) +[![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 @@ -11,18 +11,21 @@ analog to Python's NumPy library. Later we found that kotlin is much more flexib 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://SciProgCentre.github.io/kmath/) +[Documentation site](https://SciProgCentre.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) +* [A talk at KotlinConf 2019 about using kotlin for science](https://youtu.be/LI_5TZ7tnOE?si=4LknX41gl_YeUbIe) +* [A talk on architecture at Joker-2021 (in Russian)](https://youtu.be/1bZ2doHiRRM?si=9w953ro9yu98X_KJ) +* [The same talk in English](https://youtu.be/yP5DIc2fVwQ?si=louZzQ1dcXV6gP10) +* [A seminar on tensor API](https://youtu.be/0H99wUs0xTM?si=6c__04jrByFQtVpo) # Goal -* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM, JS and Native) - . +* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM, JS, Native and Wasm). * Provide basic multiplatform implementations for those abstractions (without significant performance optimization). * Provide bindings and wrappers with those abstractions for popular optimized platform libraries. @@ -53,18 +56,20 @@ module definitions below. The module stability could have the following levels: ## Modules +### [attributes-kt](attributes-kt) +> An API and basic implementation for arranging objects in a continuous memory block. +> +> **Maturity**: DEVELOPMENT + ### [benchmarks](benchmarks) -> > > **Maturity**: EXPERIMENTAL ### [examples](examples) -> > > **Maturity**: EXPERIMENTAL ### [kmath-ast](kmath-ast) -> > > **Maturity**: EXPERIMENTAL > @@ -76,7 +81,7 @@ module definitions below. The module stability could have the following levels: ### [kmath-commons](kmath-commons) -> +> Commons math binding for kmath > > **Maturity**: EXPERIMENTAL @@ -108,17 +113,15 @@ performance calculations to code generation. ### [kmath-coroutines](kmath-coroutines) -> > > **Maturity**: EXPERIMENTAL ### [kmath-dimensions](kmath-dimensions) -> +> A proof of concept module for adding type-safe dimensions to structures > > **Maturity**: PROTOTYPE ### [kmath-ejml](kmath-ejml) -> > > **Maturity**: PROTOTYPE > @@ -142,7 +145,7 @@ One can still use generic algebras though. ### [kmath-functions](kmath-functions) -> +> Functions, integration and interpolation > > **Maturity**: EXPERIMENTAL > @@ -155,31 +158,28 @@ One can still use generic algebras though. ### [kmath-geometry](kmath-geometry) -> > > **Maturity**: PROTOTYPE ### [kmath-histograms](kmath-histograms) -> > > **Maturity**: PROTOTYPE ### [kmath-jafama](kmath-jafama) -> +> Jafama integration module > -> **Maturity**: PROTOTYPE +> **Maturity**: DEPRECATED > > **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) -> +> Kotlin∇ integration module > > **Maturity**: EXPERIMENTAL > @@ -194,14 +194,14 @@ One can still use generic algebras though. > **Maturity**: DEVELOPMENT ### [kmath-multik](kmath-multik) -> +> JetBrains Multik connector > > **Maturity**: PROTOTYPE ### [kmath-nd4j](kmath-nd4j) -> +> ND4J NDStructure implementation and according NDAlgebra classes > -> **Maturity**: EXPERIMENTAL +> **Maturity**: DEPRECATED > > **Features:** > - [nd4jarraystructure](kmath-nd4j/#) : NDStructure wrapper for INDArray @@ -210,27 +210,24 @@ One can still use generic algebras though. ### [kmath-optimization](kmath-optimization) -> > > **Maturity**: EXPERIMENTAL ### [kmath-stat](kmath-stat) -> > > **Maturity**: EXPERIMENTAL ### [kmath-symja](kmath-symja) -> +> Symja integration module > > **Maturity**: PROTOTYPE ### [kmath-tensorflow](kmath-tensorflow) -> +> Google tensorflow connector > > **Maturity**: PROTOTYPE ### [kmath-tensors](kmath-tensors) -> > > **Maturity**: PROTOTYPE > @@ -241,12 +238,11 @@ One can still use generic algebras though. ### [kmath-viktor](kmath-viktor) -> +> Binding for https://github.com/JetBrains-Research/viktor > > **Maturity**: DEVELOPMENT ### [test-utils](test-utils) -> > > **Maturity**: EXPERIMENTAL @@ -256,22 +252,21 @@ One can still use generic algebras though. 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 +reasons. Currently, 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 +Calculation of performance is one of the 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 +We expect to focus on creating a 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. +KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend using GraalVM-CE or Oracle GraalVM for execution to get better performance. ### Repositories @@ -291,10 +286,7 @@ dependencies { } ``` -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/SciProgCentre/kmath/labels/waiting%20for%20a%20hero) label. \ No newline at end of file +The project requires a lot of additional work. The most important thing we need is 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 [good first issue](hhttps://github.com/SciProgCentre/kmath/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label. \ No newline at end of file diff --git a/attributes-kt/README.md b/attributes-kt/README.md new file mode 100644 index 000000000..53f80fcef --- /dev/null +++ b/attributes-kt/README.md @@ -0,0 +1,4 @@ +# Module attributes-kt + + + diff --git a/attributes-kt/api/attributes-kt.api b/attributes-kt/api/attributes-kt.api new file mode 100644 index 000000000..403e12f5c --- /dev/null +++ b/attributes-kt/api/attributes-kt.api @@ -0,0 +1,104 @@ +public abstract interface class space/kscience/attributes/Attribute { +} + +public abstract interface class space/kscience/attributes/AttributeContainer { + public abstract fun getAttributes ()Lspace/kscience/attributes/Attributes; +} + +public abstract interface class space/kscience/attributes/AttributeScope { +} + +public abstract interface class space/kscience/attributes/AttributeWithDefault : space/kscience/attributes/Attribute { + public abstract fun getDefault ()Ljava/lang/Object; +} + +public abstract interface class space/kscience/attributes/Attributes { + public static final field Companion Lspace/kscience/attributes/Attributes$Companion; + public abstract fun equals (Ljava/lang/Object;)Z + public fun get (Lspace/kscience/attributes/Attribute;)Ljava/lang/Object; + public abstract fun getContent ()Ljava/util/Map; + public fun getKeys ()Ljava/util/Set; + public abstract fun hashCode ()I + public abstract fun toString ()Ljava/lang/String; +} + +public final class space/kscience/attributes/Attributes$Companion { + public final fun equals (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attributes;)Z + public final fun getEMPTY ()Lspace/kscience/attributes/Attributes; +} + +public final class space/kscience/attributes/AttributesBuilder : space/kscience/attributes/Attributes { + public fun ()V + public final fun add (Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)V + public final fun build ()Lspace/kscience/attributes/Attributes; + public fun equals (Ljava/lang/Object;)Z + public final fun from (Lspace/kscience/attributes/Attributes;)V + public fun getContent ()Ljava/util/Map; + public fun hashCode ()I + public final fun invoke (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)V + public final fun remove (Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)V + public final fun set (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + +public final class space/kscience/attributes/AttributesBuilderKt { + public static final fun Attributes (Lkotlin/jvm/functions/Function1;)Lspace/kscience/attributes/Attributes; +} + +public final class space/kscience/attributes/AttributesKt { + public static final fun Attributes (Lspace/kscience/attributes/Attribute;)Lspace/kscience/attributes/Attributes; + public static final fun Attributes (Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; + public static final fun getOrDefault (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/AttributeWithDefault;)Ljava/lang/Object; + public static final fun isEmpty (Lspace/kscience/attributes/Attributes;)Z + public static final fun modify (Lspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function1;)Lspace/kscience/attributes/Attributes; + public static final fun plus (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attributes;)Lspace/kscience/attributes/Attributes; + public static final fun withAttribute (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attribute;)Lspace/kscience/attributes/Attributes; + public static final fun withAttribute (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; + public static final fun withAttributeElement (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; + public static final fun withoutAttribute (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/Attribute;)Lspace/kscience/attributes/Attributes; + public static final fun withoutAttributeElement (Lspace/kscience/attributes/Attributes;Lspace/kscience/attributes/SetAttribute;Ljava/lang/Object;)Lspace/kscience/attributes/Attributes; +} + +public abstract interface class space/kscience/attributes/FlagAttribute : space/kscience/attributes/Attribute { +} + +public abstract class space/kscience/attributes/PolymorphicAttribute : space/kscience/attributes/Attribute { + public synthetic fun (Lkotlin/reflect/KType;Lkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun equals (Ljava/lang/Object;)Z + public final fun getType-V0oMfBY ()Lkotlin/reflect/KType; + public fun hashCode ()I +} + +public final class space/kscience/attributes/PolymorphicAttributeKt { + public static final fun get (Lspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; + public static final fun set (Lspace/kscience/attributes/AttributesBuilder;Lkotlin/jvm/functions/Function0;Ljava/lang/Object;)V +} + +public final class space/kscience/attributes/SafeType { + public static final synthetic fun box-impl (Lkotlin/reflect/KType;)Lspace/kscience/attributes/SafeType; + public static fun constructor-impl (Lkotlin/reflect/KType;)Lkotlin/reflect/KType; + public fun equals (Ljava/lang/Object;)Z + public static fun equals-impl (Lkotlin/reflect/KType;Ljava/lang/Object;)Z + public static final fun equals-impl0 (Lkotlin/reflect/KType;Lkotlin/reflect/KType;)Z + public final fun getKType ()Lkotlin/reflect/KType; + public fun hashCode ()I + public static fun hashCode-impl (Lkotlin/reflect/KType;)I + public fun toString ()Ljava/lang/String; + public static fun toString-impl (Lkotlin/reflect/KType;)Ljava/lang/String; + public final synthetic fun unbox-impl ()Lkotlin/reflect/KType; +} + +public final class space/kscience/attributes/SafeTypeKt { + public static final fun getKClass-X0YbwmU (Lkotlin/reflect/KType;)Lkotlin/reflect/KClass; +} + +public abstract interface class space/kscience/attributes/SetAttribute : space/kscience/attributes/Attribute { +} + +public abstract interface annotation class space/kscience/attributes/UnstableAttributesAPI : java/lang/annotation/Annotation { +} + +public abstract interface class space/kscience/attributes/WithType { + public abstract fun getType-V0oMfBY ()Lkotlin/reflect/KType; +} + diff --git a/attributes-kt/build.gradle.kts b/attributes-kt/build.gradle.kts new file mode 100644 index 000000000..5956f1f16 --- /dev/null +++ b/attributes-kt/build.gradle.kts @@ -0,0 +1,20 @@ +plugins { + id("space.kscience.gradle.mpp") + `maven-publish` +} + +version = "0.1.0" + +kscience { + jvm() + js() + native() + wasm() +} + +readme { + maturity = space.kscience.gradle.Maturity.DEVELOPMENT + description = """ + An API and basic implementation for arranging objects in a continuous memory block. + """.trimIndent() +} diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt new file mode 100644 index 000000000..9142cafdb --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attribute.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2018-2023 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.attributes + +/** + * A marker interface for an attribute. Attributes are used as keys to access contents of type [T] in the container. + */ +public interface Attribute + +/** + * An attribute that could be either present or absent + */ +public interface FlagAttribute : Attribute + +/** + * An attribute with a default value + */ +public interface AttributeWithDefault : Attribute { + public val default: T +} + +/** + * Attribute containing a set of values + */ +public interface SetAttribute : Attribute> + diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt new file mode 100644 index 000000000..734887bdb --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributeContainer.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2018-2023 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.attributes + +/** + * A container for [Attributes] + */ +public interface AttributeContainer { + public val attributes: Attributes +} + +/** + * A scope, where attribute keys could be resolved. + * [O] is used only to resolve types in compile-time. + */ +public interface AttributeScope + diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt new file mode 100644 index 000000000..9d186acf1 --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt @@ -0,0 +1,135 @@ +/* + * Copyright 2018-2023 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.attributes + +/** + * A set of attributes. The implementation must guarantee that [content] keys correspond to their value types. + */ +public interface Attributes { + /** + * Raw content for this [Attributes] + */ + public val content: Map, Any?> + + /** + * Attribute keys contained in this [Attributes] + */ + public val keys: Set> get() = content.keys + + /** + * Provide an attribute value. Return null if attribute is not present or if its value is null. + */ + @Suppress("UNCHECKED_CAST") + public operator fun get(attribute: Attribute): T? = content[attribute] as? T + + override fun toString(): String + override fun equals(other: Any?): Boolean + override fun hashCode(): Int + + public companion object { + public val EMPTY: Attributes = AttributesImpl(emptyMap()) + + public fun equals(a1: Attributes, a2: Attributes): Boolean = + a1.keys == a2.keys && a1.keys.all { a1[it] == a2[it] } + } +} + +internal class AttributesImpl(override val content: Map, Any?>) : Attributes { + override fun toString(): String = "Attributes(value=${content.entries})" + override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other) + override fun hashCode(): Int = content.hashCode() +} + +public fun Attributes.isEmpty(): Boolean = content.isEmpty() + +/** + * Get attribute value or default + */ +public fun Attributes.getOrDefault(attribute: AttributeWithDefault): T = get(attribute) ?: attribute.default + +/** + * Check if there is an attribute that matches given key by type and adheres to [predicate]. + */ +@Suppress("UNCHECKED_CAST") +public inline fun > Attributes.hasAny(predicate: (value: T) -> Boolean): Boolean = + content.any { (mapKey, mapValue) -> mapKey is A && predicate(mapValue as T) } + +/** + * Check if there is an attribute of given type (subtypes included) + */ +public inline fun > Attributes.hasAny(): Boolean = + content.any { (mapKey, _) -> mapKey is A } + +/** + * Check if [Attributes] contains a flag. Multiple keys that are instances of a flag could be present + */ +public inline fun Attributes.hasFlag(): Boolean = + content.keys.any { it is A } + +/** + * Create [Attributes] with an added or replaced attribute key. + */ +public fun > Attributes.withAttribute( + attribute: A, + attrValue: T, +): Attributes = AttributesImpl(content + (attribute to attrValue)) + +public fun > Attributes.withAttribute(attribute: A): Attributes = + withAttribute(attribute, Unit) + +/** + * Create a new [Attributes] by modifying the current one + */ +public fun Attributes.modify(block: AttributesBuilder.() -> Unit): Attributes = Attributes { + from(this@modify) + block() +} + +/** + * Create new [Attributes] by removing [attribute] key + */ +public fun Attributes.withoutAttribute(attribute: Attribute<*>): Attributes = AttributesImpl(content.minus(attribute)) + +/** + * Add an element to a [SetAttribute] + */ +public fun > Attributes.withAttributeElement( + attribute: A, + attrValue: T, +): Attributes { + val currentSet: Set = get(attribute) ?: emptySet() + return AttributesImpl( + content + (attribute to (currentSet + attrValue)) + ) +} + +/** + * Remove an element from [SetAttribute] + */ +public fun > Attributes.withoutAttributeElement( + attribute: A, + attrValue: T, +): Attributes { + val currentSet: Set = get(attribute) ?: emptySet() + return AttributesImpl(content + (attribute to (currentSet - attrValue))) +} + +/** + * Create [Attributes] with a single key + */ +public fun > Attributes( + attribute: A, + attrValue: T, +): Attributes = AttributesImpl(mapOf(attribute to attrValue)) + +/** + * Create Attributes with a single [Unit] valued attribute + */ +public fun > Attributes( + attribute: A, +): Attributes = AttributesImpl(mapOf(attribute to Unit)) + +public operator fun Attributes.plus(other: Attributes): Attributes = AttributesImpl(content + other.content) \ No newline at end of file diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt new file mode 100644 index 000000000..0082ba143 --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2018-2023 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.attributes + +/** + * A safe builder for [Attributes] + * + * @param O type marker of an owner object, for which these attributes are made + */ +public class AttributesBuilder internal constructor( + private val map: MutableMap, Any?>, +) : Attributes { + + public constructor() : this(mutableMapOf()) + + override fun toString(): String = "Attributes(value=${content.entries})" + override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other) + override fun hashCode(): Int = content.hashCode() + + override val content: Map, Any?> get() = map + + public operator fun set(attribute: Attribute, value: T?) { + if (value == null) { + map.remove(attribute) + } else { + map[attribute] = value + } + } + + public operator fun Attribute.invoke(value: V?) { + set(this, value) + } + + public fun from(attributes: Attributes) { + map.putAll(attributes.content) + } + + public fun SetAttribute.add( + attrValue: V, + ) { + val currentSet: Set = get(this) ?: emptySet() + map[this] = currentSet + attrValue + } + + /** + * Remove an element from [SetAttribute] + */ + public fun SetAttribute.remove( + attrValue: V, + ) { + val currentSet: Set = get(this) ?: emptySet() + map[this] = currentSet - attrValue + } + + public fun build(): Attributes = AttributesImpl(map) +} + +public inline fun Attributes(builder: AttributesBuilder.() -> Unit): Attributes = + AttributesBuilder().apply(builder).build() \ No newline at end of file diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/PolymorphicAttribute.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/PolymorphicAttribute.kt new file mode 100644 index 000000000..b61d4c477 --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/PolymorphicAttribute.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2018-2023 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.attributes + +/** + * An attribute that has a type parameter for value + * @param type parameter-type + */ +public abstract class PolymorphicAttribute(public val type: SafeType) : Attribute { + override fun equals(other: Any?): Boolean = other != null && + (this::class == other::class) && + (other as? PolymorphicAttribute<*>)?.type == this.type + + override fun hashCode(): Int = this::class.hashCode() + type.hashCode() +} + + +/** + * Get a polymorphic attribute using attribute factory + */ +public operator fun Attributes.get(attributeKeyBuilder: () -> PolymorphicAttribute): T? = get(attributeKeyBuilder()) + +/** + * Set a polymorphic attribute using its factory + */ +public operator fun AttributesBuilder.set(attributeKeyBuilder: () -> PolymorphicAttribute, value: T) { + set(attributeKeyBuilder(), value) +} diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/SafeType.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/SafeType.kt new file mode 100644 index 000000000..b76589164 --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/SafeType.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2018-2023 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.attributes + +import kotlin.jvm.JvmInline +import kotlin.reflect.KClass +import kotlin.reflect.KType +import kotlin.reflect.typeOf + +/** + * Safe variant ok Kotlin [KType] that ensures that the type parameter is of the same type as [kType] + * + * @param kType raw [KType] + */ +@JvmInline +public value class SafeType @PublishedApi internal constructor(public val kType: KType) + +public inline fun safeTypeOf(): SafeType = SafeType(typeOf()) + +/** + * Derive Kotlin [KClass] from this type and fail if the type is not a class (should not happen) + */ +@Suppress("UNCHECKED_CAST") +@UnstableAttributesAPI +public val SafeType.kClass: KClass get() = kType.classifier as KClass + +/** + * An interface containing [type] for dynamic type checking. + */ +public interface WithType { + public val type: SafeType +} \ No newline at end of file diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt new file mode 100644 index 000000000..80d93daaf --- /dev/null +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/annotations.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2018-2023 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.attributes + +/** + * Marks declarations that are still experimental in the Attributes-kt APIs, which means that the design of the corresponding + * declarations has open issues that may (or may not) lead to their changes in the future. Roughly speaking, there is + * a chance of those declarations will be deprecated in the future or the semantics of their behavior may change + * in some way that may break some code. + */ +@MustBeDocumented +@Retention(value = AnnotationRetention.BINARY) +@RequiresOptIn("This API is unstable and could change in future", RequiresOptIn.Level.WARNING) +public annotation class UnstableAttributesAPI \ No newline at end of file diff --git a/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt index a3cfce52f..4fa5be9f1 100644 --- a/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt +++ b/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -99,7 +99,7 @@ class ExpressionsInterpretersBenchmark { private val estree = node.estreeCompileToExpression(Float64Field) private val raw = Expression { args -> - val x = args[x]!! + val x = args.getValue(x) x * 2.0 + 2.0 / x - 16.0 / sin(x) } } diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt index abfc8cbf2..f04f0a46d 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ArrayBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt index d07b7b4df..a67f52b67 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt index 1675216eb..9f665709c 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt index 5784c32b1..a157a67b2 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -11,12 +11,11 @@ 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.Float64ParallelLinearSpace import space.kscience.kmath.linear.invoke import space.kscience.kmath.linear.linearSpace import space.kscience.kmath.operations.Float64Field -import space.kscience.kmath.operations.invoke import space.kscience.kmath.tensorflow.produceWithTF -import space.kscience.kmath.tensors.core.DoubleTensorAlgebra import space.kscience.kmath.tensors.core.tensorAlgebra import kotlin.random.Random @@ -72,12 +71,12 @@ internal class DotBenchmark { } @Benchmark - fun tensorDot(blackhole: Blackhole) = with(Float64Field.tensorAlgebra) { + fun multikDot(blackhole: Blackhole) = with(multikAlgebra) { blackhole.consume(matrix1 dot matrix2) } @Benchmark - fun multikDot(blackhole: Blackhole) = with(multikAlgebra) { + fun tensorDot(blackhole: Blackhole) = with(Float64Field.tensorAlgebra) { blackhole.consume(matrix1 dot matrix2) } @@ -87,12 +86,8 @@ internal class DotBenchmark { } @Benchmark - fun doubleDot(blackhole: Blackhole) = with(Float64Field.linearSpace) { + fun parallelDot(blackhole: Blackhole) = with(Float64ParallelLinearSpace) { blackhole.consume(matrix1 dot matrix2) } - @Benchmark - fun doubleTensorDot(blackhole: Blackhole) = DoubleTensorAlgebra.invoke { - blackhole.consume(matrix1 dot matrix2) - } } diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt index def90ba22..45b3916dc 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/IntegrationBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/IntegrationBenchmark.kt index 6cc649fe9..021c654a1 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/IntegrationBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/IntegrationBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt index 355f54c65..69479200d 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt index f7aac8199..1a9e09013 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,6 +15,7 @@ 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.linear.parallel import space.kscience.kmath.operations.algebra import kotlin.random.Random @@ -38,16 +39,19 @@ internal class MatrixInverseBenchmark { } @Benchmark - fun cmLUPInversion(blackhole: Blackhole) { - CMLinearSpace { - blackhole.consume(lupSolver().inverse(matrix)) - } + fun kmathParallelLupInversion(blackhole: Blackhole) { + blackhole.consume(Double.algebra.linearSpace.parallel.lupSolver().inverse(matrix)) } @Benchmark - fun ejmlInverse(blackhole: Blackhole) { - EjmlLinearSpaceDDRM { - blackhole.consume(matrix.toEjml().inverse()) - } + fun cmLUPInversion(blackhole: Blackhole) = CMLinearSpace { + blackhole.consume(lupSolver().inverse(matrix)) } + + + @Benchmark + fun ejmlInverse(blackhole: Blackhole) = EjmlLinearSpaceDDRM { + blackhole.consume(matrix.toEjml().inverted()) + } + } diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt index ec7f940a0..77ca03099 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -25,6 +25,16 @@ import space.kscience.kmath.viktor.viktorAlgebra @State(Scope.Benchmark) internal class NDFieldBenchmark { + private companion object { + private const val dim = 1000 + private const val n = 100 + private val shape = ShapeND(dim, dim) + private val specializedField = Float64Field.ndAlgebra + private val genericField = BufferedFieldOpsND(Float64Field) + private val nd4jField = Float64Field.nd4j + private val viktorField = Float64Field.viktorAlgebra + } + @Benchmark fun specializedFieldAdd(blackhole: Blackhole) = with(specializedField) { var res: StructureND = one(shape) @@ -82,13 +92,5 @@ internal class NDFieldBenchmark { // blackhole.consume(res) // } - private companion object { - private const val dim = 1000 - private const val n = 100 - private val shape = ShapeND(dim, dim) - private val specializedField = Float64Field.ndAlgebra - private val genericField = BufferedFieldOpsND(Float64Field) - private val nd4jField = Float64Field.nd4j - private val viktorField = Float64Field.viktorAlgebra - } + } diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt index 8bdb101f6..64efaf640 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt index b424491f6..bd5c2006a 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt index b6a485821..65d159e54 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/globals.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/globals.kt index f6d278d83..ff5157f81 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/globals.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/globals.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/build.gradle.kts b/build.gradle.kts index f40cb88ee..ec82e8eeb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,7 +3,7 @@ import space.kscience.gradle.useSPCTeam plugins { id("space.kscience.gradle.project") - id("org.jetbrains.kotlinx.kover") version "0.6.0" + id("org.jetbrains.kotlinx.kover") version "0.7.6" } allprojects { @@ -14,7 +14,7 @@ allprojects { } group = "space.kscience" - version = "0.4.0-dev-2" + version = "0.4.0" } subprojects { @@ -34,7 +34,7 @@ subprojects { localDirectory.set(kotlinDir) remoteUrl.set( - java.net.URL("https://github.com/SciProgCentre/kmath/tree/master/${this@subprojects.name}/$kotlinDirPath") + uri("https://github.com/SciProgCentre/kmath/tree/master/${this@subprojects.name}/$kotlinDirPath").toURL() ) } @@ -69,4 +69,4 @@ ksciencePublish { apiValidation.nonPublicMarkers.add("space.kscience.kmath.UnstableKMathAPI") -val multikVersion by extra("0.2.2") +val multikVersion by extra("0.2.3") diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 08db49244..89bb32b15 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -17,7 +17,7 @@ val benchmarksVersion = spclibs.versions.kotlinx.benchmark.get() dependencies { api("space.kscience:gradle-tools:$toolsVersion") //plugins form benchmarks - api("org.jetbrains.kotlinx:kotlinx-benchmark-plugin:0.4.9") + api("org.jetbrains.kotlinx:kotlinx-benchmark-plugin:$benchmarksVersion") //api("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion") //to be used inside build-script only //implementation(spclibs.kotlinx.serialization.json) diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts index c455f5ae9..09633711e 100644 --- a/buildSrc/settings.gradle.kts +++ b/buildSrc/settings.gradle.kts @@ -6,7 +6,7 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") plugins { - id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0" + id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" } dependencyResolutionManagement { @@ -22,6 +22,7 @@ dependencyResolutionManagement { val toolsVersion: String = projectProperties["toolsVersion"].toString() + @Suppress("UnstableApiUsage") repositories { mavenLocal() maven("https://repo.kotlin.link") diff --git a/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/JmhReport.kt b/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/JmhReport.kt index 3a4fcdc79..98778e009 100644 --- a/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/JmhReport.kt +++ b/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/JmhReport.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt b/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt index 83cb786e3..fc04c82cf 100644 --- a/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt +++ b/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt b/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt deleted file mode 100644 index 41b88f093..000000000 --- a/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt +++ /dev/null @@ -1,433 +0,0 @@ -/* - * Copyright 2018-2022 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("KDocUnresolvedReference") - -package space.kscience.kmath.ejml.codegen - -import org.intellij.lang.annotations.Language -import java.io.File - -private fun Appendable.appendEjmlVector(type: String, ejmlMatrixType: String) { - @Language("kotlin") val text = """/** - * [EjmlVector] specialization for [$type]. - */ -public class Ejml${type}Vector(override val origin: M) : EjmlVector<$type, M>(origin) { - init { - require(origin.numRows == 1) { "The origin matrix must have only one row to form a vector" } - } - - override operator fun get(index: Int): $type = origin[0, index] -}""" - appendLine(text) - appendLine() -} - -private fun Appendable.appendEjmlMatrix(type: String, ejmlMatrixType: String) { - val text = """/** - * [EjmlMatrix] specialization for [$type]. - */ -public class Ejml${type}Matrix(override val origin: M) : EjmlMatrix<$type, M>(origin) { - override operator fun get(i: Int, j: Int): $type = origin[i, j] -}""" - appendLine(text) - appendLine() -} - -private fun Appendable.appendEjmlLinearSpace( - type: String, - kmathAlgebra: String, - ejmlMatrixParentTypeMatrix: String, - ejmlMatrixType: String, - ejmlMatrixDenseType: String, - ops: String, - denseOps: String, - isDense: Boolean, -) { - @Language("kotlin") val text = """/** - * [EjmlLinearSpace] implementation based on [CommonOps_$ops], [DecompositionFactory_${ops}] operations and - * [${ejmlMatrixType}] matrices. - */ -public object EjmlLinearSpace${ops} : EjmlLinearSpace<${type}, ${kmathAlgebra}, $ejmlMatrixType>() { - /** - * The [${kmathAlgebra}] reference. - */ - override val elementAlgebra: $kmathAlgebra get() = $kmathAlgebra - - @Suppress("UNCHECKED_CAST") - override fun Matrix<${type}>.toEjml(): Ejml${type}Matrix<${ejmlMatrixType}> = when { - this is Ejml${type}Matrix<*> && origin is $ejmlMatrixType -> this as Ejml${type}Matrix<${ejmlMatrixType}> - else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) } - } - - @Suppress("UNCHECKED_CAST") - override fun Point<${type}>.toEjml(): Ejml${type}Vector<${ejmlMatrixType}> = when { - this is Ejml${type}Vector<*> && origin is $ejmlMatrixType -> this as Ejml${type}Vector<${ejmlMatrixType}> - else -> Ejml${type}Vector(${ejmlMatrixType}(size, 1).also { - (0 until it.numRows).forEach { row -> it[row, 0] = get(row) } - }) - } - - override fun buildMatrix( - rows: Int, - columns: Int, - initializer: ${kmathAlgebra}.(i: Int, j: Int) -> ${type}, - ): Ejml${type}Matrix<${ejmlMatrixType}> = ${ejmlMatrixType}(rows, columns).also { - (0 until rows).forEach { row -> - (0 until columns).forEach { col -> it[row, col] = elementAlgebra.initializer(row, col) } - } - }.wrapMatrix() - - override fun buildVector( - size: Int, - initializer: ${kmathAlgebra}.(Int) -> ${type}, - ): Ejml${type}Vector<${ejmlMatrixType}> = Ejml${type}Vector(${ejmlMatrixType}(size, 1).also { - (0 until it.numRows).forEach { row -> it[row, 0] = elementAlgebra.initializer(row) } - }) - - private fun T.wrapMatrix() = Ejml${type}Matrix(this) - private fun T.wrapVector() = Ejml${type}Vector(this) - - override fun Matrix<${type}>.unaryMinus(): Matrix<${type}> = this * elementAlgebra { -one } - - override fun Matrix<${type}>.dot(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> { - val out = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.mult(toEjml().origin, other.toEjml().origin, out) - return out.wrapMatrix() - } - - override fun Matrix<${type}>.dot(vector: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> { - val out = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.mult(toEjml().origin, vector.toEjml().origin, out) - return out.wrapVector() - } - - override operator fun Matrix<${type}>.minus(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> { - val out = ${ejmlMatrixType}(1, 1) - - CommonOps_${ops}.add( - elementAlgebra.one, - toEjml().origin, - elementAlgebra { -one }, - other.toEjml().origin, - out,${ - if (isDense) "" else - """ - null, - null,""" - } - ) - - return out.wrapMatrix() - } - - override operator fun Matrix<${type}>.times(value: ${type}): Ejml${type}Matrix<${ejmlMatrixType}> { - val res = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.scale(value, toEjml().origin, res) - return res.wrapMatrix() - } - - override fun Point<${type}>.unaryMinus(): Ejml${type}Vector<${ejmlMatrixType}> { - val res = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.changeSign(toEjml().origin, res) - return res.wrapVector() - } - - override fun Matrix<${type}>.plus(other: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> { - val out = ${ejmlMatrixType}(1, 1) - - CommonOps_${ops}.add( - elementAlgebra.one, - toEjml().origin, - elementAlgebra.one, - other.toEjml().origin, - out,${ - if (isDense) "" else - """ - null, - null,""" - } - ) - - return out.wrapMatrix() - } - - override fun Point<${type}>.plus(other: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> { - val out = ${ejmlMatrixType}(1, 1) - - CommonOps_${ops}.add( - elementAlgebra.one, - toEjml().origin, - elementAlgebra.one, - other.toEjml().origin, - out,${ - if (isDense) "" else - """ - null, - null,""" - } - ) - - return out.wrapVector() - } - - override fun Point<${type}>.minus(other: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> { - val out = ${ejmlMatrixType}(1, 1) - - CommonOps_${ops}.add( - elementAlgebra.one, - toEjml().origin, - elementAlgebra { -one }, - other.toEjml().origin, - out,${ - if (isDense) "" else - """ - null, - null,""" - } - ) - - return out.wrapVector() - } - - override fun ${type}.times(m: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> = m * this - - override fun Point<${type}>.times(value: ${type}): Ejml${type}Vector<${ejmlMatrixType}> { - val res = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.scale(value, toEjml().origin, res) - return res.wrapVector() - } - - override fun ${type}.times(v: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> = v * this - - @UnstableKMathAPI - override fun computeFeature(structure: Matrix<${type}>, type: KClass): F? { - structure.getFeature(type)?.let { return it } - val origin = structure.toEjml().origin - - return when (type) { - ${ - if (isDense) - """ InverseMatrixFeature::class -> object : InverseMatrixFeature<${type}> { - override val inverse: Matrix<${type}> by lazy { - val res = origin.copy() - CommonOps_${ops}.invert(res) - res.wrapMatrix() - } - } - - DeterminantFeature::class -> object : DeterminantFeature<${type}> { - override val determinant: $type by lazy { CommonOps_${ops}.det(origin) } - } - - SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature<${type}> { - private val svd by lazy { - DecompositionFactory_${ops}.svd(origin.numRows, origin.numCols, true, true, false) - .apply { decompose(origin.copy()) } - } - - override val u: Matrix<${type}> by lazy { svd.getU(null, false).wrapMatrix() } - override val s: Matrix<${type}> by lazy { svd.getW(null).wrapMatrix() } - override val v: Matrix<${type}> by lazy { svd.getV(null, false).wrapMatrix() } - override val singularValues: Point<${type}> by lazy { ${type}Buffer(svd.singularValues) } - } - - QRDecompositionFeature::class -> object : QRDecompositionFeature<${type}> { - private val qr by lazy { - DecompositionFactory_${ops}.qr().apply { decompose(origin.copy()) } - } - - override val q: Matrix<${type}> by lazy { - qr.getQ(null, false).wrapMatrix().withFeature(OrthogonalFeature) - } - - override val r: Matrix<${type}> by lazy { qr.getR(null, false).wrapMatrix().withFeature(UFeature) } - } - - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature<${type}> { - override val l: Matrix<${type}> by lazy { - val cholesky = - DecompositionFactory_${ops}.chol(structure.rowNum, true).apply { decompose(origin.copy()) } - - cholesky.getT(null).wrapMatrix().withFeature(LFeature) - } - } - - LupDecompositionFeature::class -> object : LupDecompositionFeature<${type}> { - private val lup by lazy { - DecompositionFactory_${ops}.lu(origin.numRows, origin.numCols).apply { decompose(origin.copy()) } - } - - override val l: Matrix<${type}> by lazy { - lup.getLower(null).wrapMatrix().withFeature(LFeature) - } - - override val u: Matrix<${type}> by lazy { - lup.getUpper(null).wrapMatrix().withFeature(UFeature) - } - - override val p: Matrix<${type}> by lazy { lup.getRowPivot(null).wrapMatrix() } - }""" else """ QRDecompositionFeature::class -> object : QRDecompositionFeature<$type> { - private val qr by lazy { - DecompositionFactory_${ops}.qr(FillReducing.NONE).apply { decompose(origin.copy()) } - } - - override val q: Matrix<${type}> by lazy { - qr.getQ(null, false).wrapMatrix().withFeature(OrthogonalFeature) - } - - override val r: Matrix<${type}> by lazy { qr.getR(null, false).wrapMatrix().withFeature(UFeature) } - } - - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature<${type}> { - override val l: Matrix<${type}> by lazy { - val cholesky = - DecompositionFactory_${ops}.cholesky().apply { decompose(origin.copy()) } - - (cholesky.getT(null) as ${ejmlMatrixParentTypeMatrix}).wrapMatrix().withFeature(LFeature) - } - } - - LUDecompositionFeature::class, DeterminantFeature::class, InverseMatrixFeature::class -> object : - LUDecompositionFeature<${type}>, DeterminantFeature<${type}>, InverseMatrixFeature<${type}> { - private val lu by lazy { - DecompositionFactory_${ops}.lu(FillReducing.NONE).apply { decompose(origin.copy()) } - } - - override val l: Matrix<${type}> by lazy { - lu.getLower(null).wrapMatrix().withFeature(LFeature) - } - - override val u: Matrix<${type}> by lazy { - lu.getUpper(null).wrapMatrix().withFeature(UFeature) - } - - override val inverse: Matrix<${type}> by lazy { - var a = origin - val inverse = ${ejmlMatrixDenseType}(1, 1) - val solver = LinearSolverFactory_${ops}.lu(FillReducing.NONE) - if (solver.modifiesA()) a = a.copy() - val i = CommonOps_${denseOps}.identity(a.numRows) - solver.solve(i, inverse) - inverse.wrapMatrix() - } - - override val determinant: $type by lazy { elementAlgebra.number(lu.computeDeterminant().real) } - }""" - } - - else -> null - }?.let{ - type.cast(it) - } - } - - /** - * Solves for *x* in the following equation: *x = [a] -1 · [b]*. - * - * @param a the base matrix. - * @param b n by p matrix. - * @return the solution for *x* that is n by p. - */ - public fun solve(a: Matrix<${type}>, b: Matrix<${type}>): Ejml${type}Matrix<${ejmlMatrixType}> { - val res = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.solve(${ejmlMatrixType}(a.toEjml().origin), ${ejmlMatrixType}(b.toEjml().origin), res) - return res.wrapMatrix() - } - - /** - * Solves for *x* in the following equation: *x = [a] -1 · [b]*. - * - * @param a the base matrix. - * @param b n by p vector. - * @return the solution for *x* that is n by p. - */ - public fun solve(a: Matrix<${type}>, b: Point<${type}>): Ejml${type}Vector<${ejmlMatrixType}> { - val res = ${ejmlMatrixType}(1, 1) - CommonOps_${ops}.solve(${ejmlMatrixType}(a.toEjml().origin), ${ejmlMatrixType}(b.toEjml().origin), res) - return Ejml${type}Vector(res) - } -}""" - appendLine(text) - appendLine() -} - - -/** - * Generates routine EJML classes. - */ -fun ejmlCodegen(outputFile: String): Unit = File(outputFile).run { - parentFile.mkdirs() - - writer().use { - it.appendLine("/*") - it.appendLine(" * Copyright 2018-2021 KMath contributors.") - it.appendLine(" * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.") - it.appendLine(" */") - it.appendLine() - it.appendLine("/* This file is generated with buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt */") - it.appendLine() - it.appendLine("package space.kscience.kmath.ejml") - it.appendLine() - it.appendLine("""import org.ejml.data.* -import org.ejml.dense.row.CommonOps_DDRM -import org.ejml.dense.row.CommonOps_FDRM -import org.ejml.dense.row.factory.DecompositionFactory_DDRM -import org.ejml.dense.row.factory.DecompositionFactory_FDRM -import org.ejml.sparse.FillReducing -import org.ejml.sparse.csc.CommonOps_DSCC -import org.ejml.sparse.csc.CommonOps_FSCC -import org.ejml.sparse.csc.factory.DecompositionFactory_DSCC -import org.ejml.sparse.csc.factory.DecompositionFactory_FSCC -import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC -import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC -import space.kscience.kmath.linear.* -import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.nd.StructureFeature -import space.kscience.kmath.structures.Float64 -import space.kscience.kmath.structures.Float32 -import space.kscience.kmath.operations.Float64Field -import space.kscience.kmath.operations.Float32Field -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.FloatField -import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.Float64Buffer -import space.kscience.kmath.structures.Float32Buffer -import space.kscience.kmath.structures.DoubleBuffer -import space.kscience.kmath.structures.FloatBuffer -import kotlin.reflect.KClass -import kotlin.reflect.cast""") - it.appendLine() - it.appendEjmlVector("Double", "DMatrix") - it.appendEjmlVector("Float", "FMatrix") - it.appendEjmlMatrix("Double", "DMatrix") - it.appendEjmlMatrix("Float", "FMatrix") - it.appendEjmlLinearSpace("Double", "Float64Field", "DMatrix", "DMatrixRMaj", "DMatrixRMaj", "DDRM", "DDRM", true) - it.appendEjmlLinearSpace("Float", "Float32Field", "FMatrix", "FMatrixRMaj", "FMatrixRMaj", "FDRM", "FDRM", true) - - it.appendEjmlLinearSpace( - type = "Double", - kmathAlgebra = "Float64Field", - ejmlMatrixParentTypeMatrix = "DMatrix", - ejmlMatrixType = "DMatrixSparseCSC", - ejmlMatrixDenseType = "DMatrixRMaj", - ops = "DSCC", - denseOps = "DDRM", - isDense = false, - ) - - it.appendEjmlLinearSpace( - type = "Float", - kmathAlgebra = "Float32Field", - ejmlMatrixParentTypeMatrix = "FMatrix", - ejmlMatrixType = "FMatrixSparseCSC", - ejmlMatrixDenseType = "FMatrixRMaj", - ops = "FSCC", - denseOps = "FDRM", - isDense = false, - ) - } -} diff --git a/docs/images/KM.svg b/docs/images/KM.svg index 55a4339b1..f9003c5bb 100644 --- a/docs/images/KM.svg +++ b/docs/images/KM.svg @@ -1,6 +1,6 @@ diff --git a/docs/images/KM_mono.svg b/docs/images/KM_mono.svg index f1194f887..057968d0e 100644 --- a/docs/images/KM_mono.svg +++ b/docs/images/KM_mono.svg @@ -1,6 +1,6 @@ diff --git a/docs/images/KMath.svg b/docs/images/KMath.svg index 509a184bc..4e1e8a783 100644 --- a/docs/images/KMath.svg +++ b/docs/images/KMath.svg @@ -1,6 +1,6 @@ diff --git a/docs/images/KMath_mono.svg b/docs/images/KMath_mono.svg index e781979e2..5b93d45b7 100644 --- a/docs/images/KMath_mono.svg +++ b/docs/images/KMath_mono.svg @@ -1,6 +1,6 @@ diff --git a/docs/templates/ARTIFACT-TEMPLATE.md b/docs/templates/ARTIFACT-TEMPLATE.md index a3e47e693..121c673fb 100644 --- a/docs/templates/ARTIFACT-TEMPLATE.md +++ b/docs/templates/ARTIFACT-TEMPLATE.md @@ -3,25 +3,10 @@ The Maven coordinates of this project are `${group}:${name}:${version}`. **Gradle:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() - // development and snapshot versions - maven { url 'https://maven.pkg.jetbrains.space/spc/p/sci/dev' } -} - -dependencies { - implementation '${group}:${name}:${version}' -} -``` -**Gradle Kotlin DSL:** ```kotlin repositories { maven("https://repo.kotlin.link") mavenCentral() - // development and snapshot versions - maven("https://maven.pkg.jetbrains.space/spc/p/sci/dev") } dependencies { diff --git a/docs/templates/README-TEMPLATE.md b/docs/templates/README-TEMPLATE.md index d7d5a806d..850f63413 100644 --- a/docs/templates/README-TEMPLATE.md +++ b/docs/templates/README-TEMPLATE.md @@ -11,18 +11,21 @@ analog to Python's NumPy library. Later we found that kotlin is much more flexib 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://SciProgCentre.github.io/kmath/) +[Documentation site](https://SciProgCentre.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) +* [A talk at KotlinConf 2019 about using kotlin for science](https://youtu.be/LI_5TZ7tnOE?si=4LknX41gl_YeUbIe) +* [A talk on architecture at Joker-2021 (in Russian)](https://youtu.be/1bZ2doHiRRM?si=9w953ro9yu98X_KJ) +* [The same talk in English](https://youtu.be/yP5DIc2fVwQ?si=louZzQ1dcXV6gP10) +* [A seminar on tensor API](https://youtu.be/0H99wUs0xTM?si=6c__04jrByFQtVpo) # Goal -* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM, JS and Native) - . +* Provide a flexible and powerful API to work with mathematics abstractions in Kotlin-multiplatform (JVM, JS, Native and Wasm). * Provide basic multiplatform implementations for those abstractions (without significant performance optimization). * Provide bindings and wrappers with those abstractions for popular optimized platform libraries. @@ -59,22 +62,21 @@ ${modules} 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 +reasons. Currently, 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 +Calculation of performance is one of the 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 +We expect to focus on creating a 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. +KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend using GraalVM-CE or Oracle GraalVM for execution to get better performance. ### Repositories @@ -94,11 +96,7 @@ dependencies { } ``` -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. \ No newline at end of file +The project requires a lot of additional work. The most important thing we need is 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 [good first issue](hhttps://github.com/SciProgCentre/kmath/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label. \ No newline at end of file diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 7f2abc852..97a3cb8c2 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -52,7 +52,7 @@ dependencies { implementation("org.slf4j:slf4j-simple:1.7.32") // plotting - implementation("space.kscience:plotlykt-server:0.5.0") + implementation("space.kscience:plotlykt-server:0.7.0") } kotlin { diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/astRendering.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/astRendering.kt index e85bab8d8..d792a4ec6 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/astRendering.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/astRendering.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt index 784fed63e..a8150eb83 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt index f7b169e0b..fc6cbb752 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt index 9a4300f82..549b96cf1 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/expressions/autodiff.kt b/examples/src/main/kotlin/space/kscience/kmath/expressions/autodiff.kt index 863d6be7a..7bcb80da0 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/expressions/autodiff.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/expressions/autodiff.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/fit/chiSquared.kt b/examples/src/main/kotlin/space/kscience/kmath/fit/chiSquared.kt index 258ed0c84..14cd5bc76 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/fit/chiSquared.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/fit/chiSquared.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -13,10 +13,7 @@ import space.kscience.kmath.expressions.autodiff 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.optimization.* import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.real.DoubleVector import space.kscience.kmath.real.map @@ -80,8 +77,9 @@ suspend fun main() { val result = chi2.optimizeWith( CMOptimizer, mapOf(a to 1.5, b to 0.9, c to 1.0), - FunctionOptimizationTarget.MINIMIZE - ) + ){ + FunctionOptimizationTarget(OptimizationDirection.MINIMIZE) + } //display a page with plot and numerical results val page = Plotly.page { @@ -98,7 +96,7 @@ suspend fun main() { scatter { mode = ScatterMode.lines x(x) - y(x.map { result.resultPoint[a]!! * it.pow(2) + result.resultPoint[b]!! * it + 1 }) + y(x.map { result.result[a]!! * it.pow(2) + result.result[b]!! * it + 1 }) name = "fit" } } diff --git a/examples/src/main/kotlin/space/kscience/kmath/fit/qowFit.kt b/examples/src/main/kotlin/space/kscience/kmath/fit/qowFit.kt index fe7f48b72..4f3ce5443 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/fit/qowFit.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/fit/qowFit.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,7 @@ package space.kscience.kmath.fit import kotlinx.html.br import kotlinx.html.h3 +import space.kscience.attributes.Attributes import space.kscience.kmath.data.XYErrorColumnarData import space.kscience.kmath.distributions.NormalDistribution import space.kscience.kmath.expressions.Symbol @@ -64,7 +65,7 @@ suspend fun main() { QowOptimizer, Double.autodiff, mapOf(a to 0.9, b to 1.2, c to 2.0, e to 1.0, d to 1.0, e to 0.0), - OptimizationParameters(a, b, c, d) + attributes = Attributes(OptimizationParameters, listOf(a, b, c, d)) ) { arg -> //bind variables to autodiff context val a by binding @@ -94,13 +95,13 @@ suspend fun main() { scatter { mode = ScatterMode.lines x(x) - y(x.map { result.model(result.startPoint + result.resultPoint + (Symbol.x to it)) }) + y(x.map { result.model(result.startPoint + result.result + (Symbol.x to it)) }) name = "fit" } } br() h3 { - +"Fit result: ${result.resultPoint}" + +"Fit result: ${result.result}" } h3 { +"Chi2/dof = ${result.chiSquaredOrNull!! / result.dof}" diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt index 493a89387..07bacc45d 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt index 6908eebdd..c2e33afd4 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,6 @@ package space.kscience.kmath.functions import space.kscience.kmath.interpolation.SplineInterpolator import space.kscience.kmath.interpolation.interpolatePolynomials import space.kscience.kmath.operations.Float64Field -import space.kscience.kmath.structures.Float64Buffer import space.kscience.plotly.Plotly import space.kscience.plotly.UnstablePlotlyAPI import space.kscience.plotly.makeFile @@ -24,9 +23,7 @@ fun main() { x to sin(x) } - val polynomial: PiecewisePolynomial = SplineInterpolator( - Float64Field, ::Float64Buffer - ).interpolatePolynomials(data) + val polynomial: PiecewisePolynomial = SplineInterpolator(Float64Field).interpolatePolynomials(data) val function = polynomial.asFunction(Float64Field, 0.0) diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt index e621cda08..29a40498b 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt index baba2eb28..0680f42b0 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt index 52451e9f3..b4afa37e4 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt index 79eddc6c3..c59ffeae2 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt @@ -1,33 +1,28 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.time.ExperimentalTime import kotlin.time.measureTime -@OptIn(ExperimentalTime::class) -fun main() { +fun main() = with(Float64ParallelLinearSpace) { val random = Random(12224) val dim = 1000 //creating invertible matrix - val matrix1 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j -> + val matrix1 = buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val matrix2 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j -> + val matrix2 = buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } val time = measureTime { - with(Double.algebra.linearSpace) { - repeat(10) { - matrix1 dot matrix2 - } + repeat(30) { + matrix1 dot matrix2 } } diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt index eb170e7fa..6ebd1d221 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/lupPerformance.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/lupPerformance.kt new file mode 100644 index 000000000..dcd380ea6 --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/lupPerformance.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2018-2024 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 kotlin.random.Random +import kotlin.time.measureTime + +fun main(): Unit = with(Float64LinearSpace) { + val random = Random(1224) + val dim = 500 + + //creating invertible matrix + val u = buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } + val l = buildMatrix(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 } + val matrix = l dot u + + val time = measureTime { + repeat(20) { + lupSolver().inverse(matrix) + } + } + + println(time) +} \ No newline at end of file diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt index 2447d06ed..903388c2f 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/BigIntDemo.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt index 77cfca4ae..a9c363dd6 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt index bb69c6aff..940a20324 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -10,12 +10,14 @@ import space.kscience.kmath.linear.matrix import space.kscience.kmath.nd.Float64BufferND import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.Structure2D +import space.kscience.kmath.nd.mutableStructureND import space.kscience.kmath.nd.ndAlgebra -import space.kscience.kmath.viktor.ViktorStructureND import space.kscience.kmath.viktor.viktorAlgebra +import kotlin.collections.component1 +import kotlin.collections.component2 fun main() { - val viktorStructure: ViktorStructureND = Float64Field.viktorAlgebra.structureND(ShapeND(2, 2)) { (i, j) -> + val viktorStructure = Float64Field.viktorAlgebra.mutableStructureND(2, 2) { (i, j) -> if (i == j) 2.0 else 0.0 } diff --git a/examples/src/main/kotlin/space/kscience/kmath/series/DateTimeSeries.kt b/examples/src/main/kotlin/space/kscience/kmath/series/DateTimeSeries.kt index ca10fc290..35435b64e 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/series/DateTimeSeries.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/series/DateTimeSeries.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt b/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt index 0e10f1a9a..4742b1f62 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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.series @@ -13,7 +18,10 @@ import space.kscience.kmath.structures.slice import space.kscience.plotly.* import kotlin.math.PI -fun main() = with(Double.algebra.bufferAlgebra.seriesAlgebra()) { +fun Double.Companion.seriesAlgebra() = Double.algebra.bufferAlgebra.seriesAlgebra() + + +fun main() = with(Double.seriesAlgebra()) { fun Plot.plotSeries(name: String, buffer: Buffer) { diff --git a/examples/src/main/kotlin/space/kscience/kmath/series/seriesBuilder.kt b/examples/src/main/kotlin/space/kscience/kmath/series/seriesBuilder.kt new file mode 100644 index 000000000..9dfb0fdc9 --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/series/seriesBuilder.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2018-2023 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.series + + +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.Float64Buffer +import space.kscience.kmath.structures.asBuffer +import space.kscience.kmath.structures.toDoubleArray +import space.kscience.plotly.* +import space.kscience.plotly.models.Scatter +import space.kscience.plotly.models.ScatterMode +import kotlin.random.Random + +fun main(): Unit = with(Double.seriesAlgebra()) { + + val random = Random(1234) + + val arrayOfRandoms = DoubleArray(20) { random.nextDouble() } + + val series1: Float64Buffer = arrayOfRandoms.asBuffer() + val series2: Series = series1.moveBy(3) + + val res = series2 - series1 + + println(res.size) + + println(res) + + fun Plot.series(name: String, buffer: Buffer, block: Scatter.() -> Unit = {}) { + scatter { + this.name = name + x.numbers = buffer.offsetIndices + y.doubles = buffer.toDoubleArray() + block() + } + } + + Plotly.plot { + series("series1", series1) + series("series2", series2) + series("dif", res) { + mode = ScatterMode.lines + line.color("magenta") + } + }.makeFile(resourceLocation = ResourceLocation.REMOTE) +} \ No newline at end of file diff --git a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt index 031955e15..19b763113 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt index 8e6d096ed..2f8e9d959 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt index f71f248d9..9b48e44a0 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,7 @@ package space.kscience.kmath.structures import space.kscience.kmath.complex.* -import space.kscience.kmath.linear.transpose +import space.kscience.kmath.linear.transposed import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.as2D import space.kscience.kmath.nd.ndAlgebra @@ -60,7 +60,7 @@ fun complexExample() { val sum = matrix + x + 1.0 //Represent the sum as 2d-structure and transpose - sum.as2D().transpose() + sum.as2D().transposed() } } } diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt index 375d4e182..9ec8719ad 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt index a0e25b81b..8c34ad4f1 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,7 @@ package space.kscience.kmath.structures import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* +import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.ExtendedField import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.NumbersAddOps @@ -52,6 +53,15 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND Double): MutableBufferND { + val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> + val index = strides.index(offset) + DoubleField.initializer(index) + }.toArray() + + return MutableBufferND(strides, array.asBuffer()) + } + @OptIn(PerformancePitfall::class) override fun StructureND.map( transform: Float64Field.(Double) -> Double, diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt index 1e50ef7da..4d52c4258 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt index 4aa7b74dd..8d642d892 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt @@ -1,25 +1,23 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.ShapeND -import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.mapToBuffer import kotlin.system.measureTimeMillis private inline fun BufferND.mapToBufferND( - bufferFactory: BufferFactory = BufferFactory.auto(), + bufferFactory: BufferFactory = BufferFactory(), crossinline block: (T) -> R, ): BufferND = BufferND(indices, buffer.mapToBuffer(bufferFactory, block)) @Suppress("UNUSED_VARIABLE") fun main() { val n = 6000 - val structure = StructureND.buffered(ShapeND(n, n), Buffer.Companion::auto) { 1.0 } + val structure = BufferND(n, n) { 1.0 } structure.mapToBufferND { it + 1 } // warm-up val time1 = measureTimeMillis { val res = structure.mapToBufferND { it + 1 } } println("Structure mapping finished in $time1 millis") diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt index 8785fa6f2..d4dfa60cb 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -13,7 +13,7 @@ import space.kscience.kmath.operations.withSize inline fun MutableBuffer.Companion.same( n: Int, value: R -): MutableBuffer = auto(n) { value } +): MutableBuffer = MutableBuffer(n) { value } fun main() { diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/mutableNd.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/mutableNd.kt new file mode 100644 index 000000000..0b0a4cac1 --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/mutableNd.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2018-2023 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.PerformancePitfall +import space.kscience.kmath.nd.* +import space.kscience.kmath.operations.algebra + +@OptIn(PerformancePitfall::class) +fun main(): Unit = with(Double.algebra.ndAlgebra) { + val structure: MutableStructure2D = mutableStructureND(ShapeND(2, 2)) { (i, j) -> + i.toDouble() + j.toDouble() + }.as2D() + + structure[0, 1] = -2.0 + + val structure2 = mutableStructureND(2, 2) { (i, j) -> i.toDouble() + j.toDouble() }.as2D() + + structure2[0, 1] = 2.0 + + + println(structure + structure2) +} \ No newline at end of file diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt index 1ba0e3503..a4dacb881 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,7 +15,7 @@ private fun DMatrixContext.simple() { val m2 = produce { i, j -> (i + j).toDouble() } //Dimension-safe addition - m1.transpose() + m2 + m1.transposed() + m2 } private object D5 : Dimension { diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticDifficultTest.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticDifficultTest.kt index e6f575262..a6903baca 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticDifficultTest.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticDifficultTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticEasyTest.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticEasyTest.kt index 507943031..ab50d17e1 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticEasyTest.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticEasyTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticMiddleTest.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticMiddleTest.kt index 0659db103..aa8aa73f8 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticMiddleTest.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StaticLm/staticMiddleTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamLm.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamLm.kt index b2818ef2a..f77adab7e 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamLm.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamLm.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamingLmTest.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamingLmTest.kt index c9dd5029e..2c53146b7 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamingLmTest.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/StreamingLm/streamingLmTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/functionsToOptimize.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/functionsToOptimize.kt index 7ccb37ed0..4aefa1e0a 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/functionsToOptimize.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LevenbergMarquardt/functionsToOptimize.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt index 2c570ea34..8de4ab527 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/PCA.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/PCA.kt index fb774a39d..10edcd03a 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/PCA.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/PCA.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/dataSetNormalization.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/dataSetNormalization.kt index 45c2ff120..e804e5818 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/dataSetNormalization.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/dataSetNormalization.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/linearSystemSolvingWithLUP.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/linearSystemSolvingWithLUP.kt index 238696cf9..d5b04e153 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/linearSystemSolvingWithLUP.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/linearSystemSolvingWithLUP.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/multik.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/multik.kt index 67e0631e7..f8bf65ad7 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/multik.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/multik.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/neuralNetwork.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/neuralNetwork.kt index 8fd5ae5ad..78f27c304 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/neuralNetwork.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/neuralNetwork.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e411586a5..17655d0ef 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/kmath-ast/README.md b/kmath-ast/README.md index d85a18e1c..dd9978bee 100644 --- a/kmath-ast/README.md +++ b/kmath-ast/README.md @@ -10,19 +10,8 @@ Extensions to MST API: transformations, dynamic compilation and visualization. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-ast:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-ast:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-ast:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -31,7 +20,7 @@ repositories { } dependencies { - implementation("space.kscience:kmath-ast:0.4.0-dev-1") + implementation("space.kscience:kmath-ast:0.4.0-dev-3") } ``` diff --git a/kmath-ast/build.gradle.kts b/kmath-ast/build.gradle.kts index 7cdb745f0..fe10716af 100644 --- a/kmath-ast/build.gradle.kts +++ b/kmath-ast/build.gradle.kts @@ -31,7 +31,7 @@ kscience{ kotlin { js { nodejs { - testTask { + testTask{ useMocha().timeout = "0" } } diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt index e82f7a3ab..f50a70821 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt @@ -1,10 +1,12 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.SafeType +import space.kscience.attributes.WithType import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.operations.Algebra @@ -15,7 +17,7 @@ import space.kscience.kmath.operations.NumericAlgebra * * @param T the type. */ -public sealed interface TypedMst { +public sealed interface TypedMst : WithType { /** * A node containing a unary operation. * @@ -24,8 +26,13 @@ public sealed interface TypedMst { * @property function The function implementing this operation. * @property value The argument of this operation. */ - public class Unary(public val operation: String, public val function: (T) -> T, public val value: TypedMst) : - TypedMst { + public class Unary( + public val operation: String, + public val function: (T) -> T, + public val value: TypedMst, + ) : TypedMst { + override val type: SafeType get() = value.type + override fun equals(other: Any?): Boolean { if (this === other) return true if (other == null || this::class != other::class) return false @@ -59,6 +66,13 @@ public sealed interface TypedMst { public val left: TypedMst, public val right: TypedMst, ) : TypedMst { + + init { + require(left.type==right.type){"Left and right expressions must be of the same type"} + } + + override val type: SafeType get() = left.type + override fun equals(other: Any?): Boolean { if (this === other) return true if (other == null || this::class != other::class) return false @@ -89,7 +103,12 @@ public sealed interface TypedMst { * @property value The held value. * @property number The number this value corresponds. */ - public class Constant(public val value: T, public val number: Number?) : TypedMst { + public class Constant( + override val type: SafeType, + public val value: T, + public val number: Number?, + ) : TypedMst { + override fun equals(other: Any?): Boolean { if (this === other) return true if (other == null || this::class != other::class) return false @@ -114,7 +133,7 @@ public sealed interface TypedMst { * @param T the type. * @property symbol The symbol of the variable. */ - public class Variable(public val symbol: Symbol) : TypedMst { + public class Variable(override val type: SafeType, public val symbol: Symbol) : TypedMst { override fun equals(other: Any?): Boolean { if (this === other) return true if (other == null || this::class != other::class) return false @@ -167,6 +186,7 @@ public fun TypedMst.interpret(algebra: Algebra, vararg arguments: Pair /** * Interpret this [TypedMst] node as expression. */ -public fun TypedMst.toExpression(algebra: Algebra): Expression = Expression { arguments -> - interpret(algebra, arguments) -} +public fun TypedMst.toExpression(algebra: Algebra): Expression = + Expression(algebra.type) { arguments -> + interpret(algebra, arguments) + } diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt index 8fc5a6aaf..18c7fc2b1 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -16,6 +16,7 @@ import space.kscience.kmath.operations.bindSymbolOrNull */ public fun MST.evaluateConstants(algebra: Algebra): TypedMst = when (this) { is MST.Numeric -> TypedMst.Constant( + algebra.type, (algebra as? NumericAlgebra)?.number(value) ?: error("Numeric nodes are not supported by $algebra"), value, ) @@ -27,7 +28,7 @@ public fun MST.evaluateConstants(algebra: Algebra): TypedMst = when (t arg.value, ) - TypedMst.Constant(value, if (value is Number) value else null) + TypedMst.Constant(algebra.type, value, if (value is Number) value else null) } else -> TypedMst.Unary(operation, algebra.unaryOperationFunction(operation), arg) @@ -59,7 +60,7 @@ public fun MST.evaluateConstants(algebra: Algebra): TypedMst = when (t ) } - TypedMst.Constant(value, if (value is Number) value else null) + TypedMst.Constant(algebra.type, value, if (value is Number) value else null) } algebra is NumericAlgebra && left is TypedMst.Constant && left.number != null -> TypedMst.Binary( @@ -84,8 +85,8 @@ public fun MST.evaluateConstants(algebra: Algebra): TypedMst = when (t val boundSymbol = algebra.bindSymbolOrNull(this) if (boundSymbol != null) - TypedMst.Constant(boundSymbol, if (boundSymbol is Number) boundSymbol else null) + TypedMst.Constant(algebra.type, boundSymbol, if (boundSymbol is Number) boundSymbol else null) else - TypedMst.Variable(this) + TypedMst.Variable(algebra.type, this) } } diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt index 2c9a2a9ad..40bc60f4d 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt index 5a338afed..89464af32 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt index bfd9aeef9..8e6cada95 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt index f88a5b319..4590cebea 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt index 0196be3b6..8cc29b993 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathSyntax.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt index 16957bdd2..8e0b00d10 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/SyntaxRenderer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt index 4deffc83c..9fcabefbc 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt index dce99cc5a..b3844728b 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt index 0d26621d3..dae330916 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt index 77f6a191d..b2fe94589 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt index 30bb7291d..0fb3002f1 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt index 3dbdf063a..6fc3341b6 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt index 61a37944a..74fd4b56c 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt index 784dcece9..9ed6fa3f4 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -10,6 +10,7 @@ import space.kscience.kmath.complex.ComplexField import space.kscience.kmath.expressions.interpret import space.kscience.kmath.operations.Algebra import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -39,6 +40,8 @@ internal class TestParser { @Test fun evaluateMstBinary() { val magicalAlgebra = object : Algebra { + override val bufferFactory: MutableBufferFactory get() = MutableBufferFactory() + override fun bindSymbolOrNull(value: String): String = value override fun unaryOperationFunction(operation: String): (arg: String) -> String { diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt index 504ec9bd0..227c0844a 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt index bb6d39204..158abbdeb 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestFeatures.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt index 66c0ae1ae..12c11f89c 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt index 8bc014c54..abf761d23 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt index 306538d5d..be3d587be 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestStages.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt index 79f178eef..378e6647b 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt index 27b2f2598..ee2346b13 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt index f6411334c..c1842b039 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt index 87c2df2d2..26e963af0 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -22,7 +22,7 @@ import space.kscience.kmath.operations.Algebra @OptIn(UnstableKMathAPI::class) public fun MST.compileToExpression(algebra: Algebra): Expression { val typed = evaluateConstants(algebra) - if (typed is TypedMst.Constant) return Expression { typed.value } + if (typed is TypedMst.Constant) return Expression(algebra.type) { typed.value } fun ESTreeBuilder.visit(node: TypedMst): BaseExpression = when (node) { is TypedMst.Constant -> constant(node.value) @@ -36,7 +36,7 @@ public fun MST.compileToExpression(algebra: Algebra): Expression ) } - return ESTreeBuilder { visit(typed) }.instance + return ESTreeBuilder(algebra.type) { visit(typed) }.instance } /** diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt index 1517cdef2..f4a879d1b 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt @@ -1,17 +1,26 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.SafeType +import space.kscience.attributes.WithType 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(val bodyCallback: ESTreeBuilder.() -> BaseExpression) { - private class GeneratedExpression(val executable: dynamic, val constants: Array) : Expression { +internal class ESTreeBuilder( + override val type: SafeType, + val bodyCallback: ESTreeBuilder.() -> BaseExpression, +) : WithType { + private class GeneratedExpression( + override val type: SafeType, + val executable: dynamic, + val constants: Array, + ) : Expression { @Suppress("UNUSED_VARIABLE") override fun invoke(arguments: Map): T { val e = executable @@ -30,7 +39,7 @@ internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExp ) val code = generate(node) - GeneratedExpression(js("new Function('constants', 'arguments_0', code)"), constants.toTypedArray()) + GeneratedExpression(type, js("new Function('constants', 'arguments_0', code)"), constants.toTypedArray()) } private val constants = mutableListOf() diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt index 2434788ec..ccbebff81 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/astring/astring.typealises.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt index cc4360f8d..25721fee6 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt index a17726c9d..bf0d06660 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/astring/astring.typealises.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt index 03cd76611..bf0f9cd22 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/base64/base64.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt index 8c50de9bb..c34961dee 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt index eebfeb6ef..f9b3654c2 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/binaryen/index.binaryen.typealiases.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt index a2a04da79..e87a5f380 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/emitter/emitter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt index 2dd2c08cd..7549f6514 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt index bf4a25367..a05f57f7f 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt index ced165a3a..496df7e67 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/stream/stream.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt index 2c0dc9de1..9320243d8 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es2015.iterable.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt index f20e2d865..07fd53fec 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/tsstdlib/lib.es5.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt index 628df09b0..2103fcdc9 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/lib.dom.WebAssembly.module_dukat.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt index 4cef2d85a..21e32aeab 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/webassembly/nonDeclarations.WebAssembly.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt index 4917f6f1b..18ef35c4b 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt index d60f24247..63cda9756 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/f64StandardFunctions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt index f3249adf7..ceb1ad69d 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt index 6d826541c..c747adc0c 100644 --- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt +++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -20,6 +20,7 @@ import space.kscience.kmath.estree.compileToExpression as estreeCompileToExpress import space.kscience.kmath.wasm.compile as wasmCompile import space.kscience.kmath.wasm.compileToExpression as wasmCompileToExpression +@OptIn(UnstableKMathAPI::class) private object WasmCompilerTestContext : CompilerTestContext { override fun MST.compileToExpression(algebra: Int32Ring): Expression = wasmCompileToExpression(algebra) override fun MST.compile(algebra: Int32Ring, arguments: Map): Int = wasmCompile(algebra, arguments) diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt index c7e898713..45b5aef5a 100644 --- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt +++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt index 97fe91ee4..cd115cd4f 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -29,7 +29,7 @@ import space.kscience.kmath.operations.Int64Ring @PublishedApi internal fun MST.compileWith(type: Class, algebra: Algebra): Expression { val typed = evaluateConstants(algebra) - if (typed is TypedMst.Constant) return Expression { typed.value } + if (typed is TypedMst.Constant) return Expression(algebra.type) { typed.value } fun GenericAsmBuilder.variablesVisitor(node: TypedMst): Unit = when (node) { is TypedMst.Unary -> variablesVisitor(node.value) diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt index e1fd455fd..aa7e093a5 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/AsmBuilder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt index acae45d87..5314c4cc3 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/GenericAsmBuilder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt index bb8143206..9c27a25b5 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt index b3ccfdc0c..e3565df23 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/codegenUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt index 6459f4dcf..392854541 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/mapIntrinsics.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt index ec66be830..745dced45 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt index e04e2e5bd..f06137c4a 100644 --- a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt +++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt b/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt index ec66be830..745dced45 100644 --- a/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt +++ b/kmath-ast/src/nativeMain/kotlin/space/kscience/kmath/ast/rendering/multiplatformToString.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt b/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt index 0674b0492..28dd12e1f 100644 --- a/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt +++ b/kmath-ast/src/nativeTest/kotlin/space/kscience/kmath/ast/runCompilerTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-commons/README.md b/kmath-commons/README.md index 47b61c409..2b5f3146d 100644 --- a/kmath-commons/README.md +++ b/kmath-commons/README.md @@ -6,19 +6,8 @@ Commons math binding for kmath ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-commons:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-commons:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-commons:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-commons:0.4.0-dev-1") + implementation("space.kscience:kmath-commons:0.4.0-dev-3") } ``` diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/expressions/CmDsExpression.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/expressions/CmDsExpression.kt index 38eaf8868..855de14a0 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/expressions/CmDsExpression.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/expressions/CmDsExpression.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,10 +8,13 @@ package space.kscience.kmath.commons.expressions import org.apache.commons.math3.analysis.differentiation.DerivativeStructure +import space.kscience.attributes.SafeType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.* +import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.ExtendedField import space.kscience.kmath.operations.NumbersAddOps +import space.kscience.kmath.structures.MutableBufferFactory /** * A field over commons-math [DerivativeStructure]. @@ -26,6 +29,9 @@ public class CmDsField( bindings: Map, ) : ExtendedField, ExpressionAlgebra, NumbersAddOps { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + public val numberOfVariables: Int = bindings.size override val zero: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order) } @@ -77,7 +83,9 @@ public class CmDsField( override fun scale(a: DerivativeStructure, value: Double): DerivativeStructure = a.multiply(value) - override fun multiply(left: DerivativeStructure, right: DerivativeStructure): DerivativeStructure = left.multiply(right) + override fun multiply(left: DerivativeStructure, right: DerivativeStructure): DerivativeStructure = + left.multiply(right) + override fun divide(left: DerivativeStructure, right: DerivativeStructure): DerivativeStructure = left.divide(right) override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin() override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos() @@ -113,8 +121,8 @@ public class CmDsField( */ @Deprecated("Use generic DSAlgebra from the core") public object CmDsProcessor : AutoDiffProcessor { - override fun differentiate( - function: CmDsField.() -> DerivativeStructure, + override fun differentiate( + function: CmDsField.() -> DerivativeStructure, ): CmDsExpression = CmDsExpression(function) } @@ -125,13 +133,16 @@ public object CmDsProcessor : AutoDiffProcessor DerivativeStructure, ) : DifferentiableExpression { + + override val type: SafeType get() = DoubleField.type + override operator fun invoke(arguments: Map): Double = CmDsField(0, arguments).function().value /** * Get the derivative expression with given orders */ - override fun derivativeOrNull(symbols: List): Expression = Expression { arguments -> + override fun derivativeOrNull(symbols: List): Expression = Expression(type) { arguments -> with(CmDsField(symbols.size, arguments)) { function().derivative(symbols) } } } diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt index 263463d37..c016c0f62 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMGaussRuleIntegrator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.commons.integration @@ -13,26 +13,29 @@ import space.kscience.kmath.integration.* */ public class CMGaussRuleIntegrator( private val numpoints: Int, - private var type: GaussRule = GaussRule.LEGANDRE, + private var type: GaussRule = GaussRule.LEGENDRE, ) : UnivariateIntegrator { - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand { - val range = integrand.getFeature()?.range + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand { + val range = integrand[IntegrationRange] ?: error("Integration range is not provided") val integrator: GaussIntegrator = getIntegrator(range) //TODO check performance val res: Double = integrator.integrate(integrand.function) - return integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + numpoints) + return integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + numpoints) + } } private fun getIntegrator(range: ClosedRange): GaussIntegrator { return when (type) { - GaussRule.LEGANDRE -> factory.legendre( + GaussRule.LEGENDRE -> factory.legendre( numpoints, range.start, range.endInclusive ) - GaussRule.LEGANDREHP -> factory.legendreHighPrecision( + GaussRule.LEGENDREHP -> factory.legendreHighPrecision( numpoints, range.start, range.endInclusive @@ -65,7 +68,7 @@ public class CMGaussRuleIntegrator( } public enum class GaussRule { - UNIFORM, LEGANDRE, LEGANDREHP + UNIFORM, LEGENDRE, LEGENDREHP } public companion object { @@ -74,10 +77,10 @@ public class CMGaussRuleIntegrator( public fun integrate( range: ClosedRange, numPoints: Int = 100, - type: GaussRule = GaussRule.LEGANDRE, + type: GaussRule = GaussRule.LEGENDRE, function: (Double) -> Double, - ): Double = CMGaussRuleIntegrator(numPoints, type).process( - UnivariateIntegrand(function, IntegrationRange(range)) + ): Double = CMGaussRuleIntegrator(numPoints, type).integrate( + UnivariateIntegrand({IntegrationRange(range)},function) ).value } } \ No newline at end of file diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt index c3e581d31..83880287e 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/integration/CMIntegrator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,30 +7,32 @@ package space.kscience.kmath.commons.integration import org.apache.commons.math3.analysis.integration.IterativeLegendreGaussIntegrator import org.apache.commons.math3.analysis.integration.SimpsonIntegrator +import space.kscience.attributes.Attributes import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.integration.* +import org.apache.commons.math3.analysis.integration.UnivariateIntegrator as CMUnivariateIntegrator /** * Integration wrapper for Common-maths UnivariateIntegrator */ public class CMIntegrator( private val defaultMaxCalls: Int = 200, - public val integratorBuilder: (Integrand) -> org.apache.commons.math3.analysis.integration.UnivariateIntegrator, + public val integratorBuilder: (Integrand) -> CMUnivariateIntegrator, ) : UnivariateIntegrator { - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand { + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand { val integrator = integratorBuilder(integrand) - val maxCalls = integrand.getFeature()?.maxCalls ?: defaultMaxCalls + val maxCalls = integrand[IntegrandMaxCalls] ?: defaultMaxCalls val remainingCalls = maxCalls - integrand.calls - val range = integrand.getFeature()?.range - ?: error("Integration range is not provided") + val range = integrand[IntegrationRange] ?: error("Integration range is not provided") val res = integrator.integrate(remainingCalls, integrand.function, range.start, range.endInclusive) - return integrand + - IntegrandValue(res) + - IntegrandAbsoluteAccuracy(integrator.absoluteAccuracy) + - IntegrandRelativeAccuracy(integrator.relativeAccuracy) + - IntegrandCallsPerformed(integrator.evaluations + integrand.calls) + return integrand.withAttributes { + value(res) + IntegrandAbsoluteAccuracy(integrator.absoluteAccuracy) + IntegrandRelativeAccuracy(integrator.relativeAccuracy) + IntegrandCallsPerformed(integrator.evaluations + integrand.calls) + } } @@ -39,11 +41,9 @@ public class CMIntegrator( * Create a Simpson integrator based on [SimpsonIntegrator] */ public fun simpson(defaultMaxCalls: Int = 200): CMIntegrator = CMIntegrator(defaultMaxCalls) { integrand -> - val absoluteAccuracy = integrand.getFeature()?.accuracy - ?: SimpsonIntegrator.DEFAULT_ABSOLUTE_ACCURACY - val relativeAccuracy = integrand.getFeature()?.accuracy - ?: SimpsonIntegrator.DEFAULT_ABSOLUTE_ACCURACY - val iterations = integrand.getFeature()?.range + val absoluteAccuracy = integrand[IntegrandAbsoluteAccuracy] ?: SimpsonIntegrator.DEFAULT_ABSOLUTE_ACCURACY + val relativeAccuracy = integrand[IntegrandRelativeAccuracy] ?: SimpsonIntegrator.DEFAULT_ABSOLUTE_ACCURACY + val iterations = integrand[IntegrandIterationsRange] ?: SimpsonIntegrator.DEFAULT_MIN_ITERATIONS_COUNT..SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT @@ -55,11 +55,11 @@ public class CMIntegrator( */ public fun legandre(numPoints: Int, defaultMaxCalls: Int = numPoints * 5): CMIntegrator = CMIntegrator(defaultMaxCalls) { integrand -> - val absoluteAccuracy = integrand.getFeature()?.accuracy + val absoluteAccuracy = integrand[IntegrandAbsoluteAccuracy] ?: IterativeLegendreGaussIntegrator.DEFAULT_ABSOLUTE_ACCURACY - val relativeAccuracy = integrand.getFeature()?.accuracy + val relativeAccuracy = integrand[IntegrandRelativeAccuracy] ?: IterativeLegendreGaussIntegrator.DEFAULT_ABSOLUTE_ACCURACY - val iterations = integrand.getFeature()?.range + val iterations = integrand[IntegrandIterationsRange] ?: IterativeLegendreGaussIntegrator.DEFAULT_MIN_ITERATIONS_COUNT..IterativeLegendreGaussIntegrator.DEFAULT_MAX_ITERATIONS_COUNT IterativeLegendreGaussIntegrator( @@ -74,15 +74,9 @@ public class CMIntegrator( } @UnstableKMathAPI -public var MutableList.targetAbsoluteAccuracy: Double? - get() = filterIsInstance().lastOrNull()?.accuracy - set(value) { - value?.let { add(IntegrandAbsoluteAccuracy(value)) } - } +public val Attributes.targetAbsoluteAccuracy: Double? + get() = get(IntegrandAbsoluteAccuracy) @UnstableKMathAPI -public var MutableList.targetRelativeAccuracy: Double? - get() = filterIsInstance().lastOrNull()?.accuracy - set(value) { - value?.let { add(IntegrandRelativeAccuracy(value)) } - } \ No newline at end of file +public val Attributes.targetRelativeAccuracy: Double? + get() = get(IntegrandRelativeAccuracy) diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt index 4d2e2aa90..1fe923cbb 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt @@ -1,21 +1,29 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.commons.linear import org.apache.commons.math3.linear.* +import org.apache.commons.math3.linear.LUDecomposition +import org.apache.commons.math3.linear.SingularValueDecomposition +import space.kscience.attributes.SafeType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* -import space.kscience.kmath.nd.StructureFeature +import space.kscience.kmath.linear.CholeskyDecomposition +import space.kscience.kmath.linear.QRDecomposition +import space.kscience.kmath.nd.Structure2D +import space.kscience.kmath.nd.StructureAttribute +import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.Float64Buffer -import kotlin.reflect.KClass -import kotlin.reflect.cast +import space.kscience.kmath.structures.Float64 +import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.asBuffer public class CMMatrix(public val origin: RealMatrix) : Matrix { + override val rowNum: Int get() = origin.rowDimension override val colNum: Int get() = origin.columnDimension @@ -38,6 +46,8 @@ public fun RealVector.toPoint(): CMVector = CMVector(this) public object CMLinearSpace : LinearSpace { override val elementAlgebra: Float64Field get() = Float64Field + override val type: SafeType get() = DoubleField.type + override fun buildMatrix( rows: Int, columns: Int, @@ -98,49 +108,48 @@ public object CMLinearSpace : LinearSpace { override fun Double.times(v: Point): CMVector = v * this - @UnstableKMathAPI - override fun computeFeature(structure: Matrix, type: KClass): F? { - //Return the feature if it is intrinsic to the structure - structure.getFeature(type)?.let { return it } + override fun > computeAttribute(structure: Structure2D, attribute: A): V? { val origin = structure.toCM().origin - return when (type) { - DiagonalFeature::class -> if (origin is DiagonalMatrix) DiagonalFeature else null + val raw: Any? = when (attribute) { + IsDiagonal -> if (origin is DiagonalMatrix) Unit else null + Determinant -> LUDecomposition(origin).determinant - DeterminantFeature::class, LupDecompositionFeature::class -> object : - DeterminantFeature, - LupDecompositionFeature { - private val lup by lazy { LUDecomposition(origin) } - override val determinant: Double by lazy { lup.determinant } - override val l: Matrix by lazy> { CMMatrix(lup.l).withFeature(LFeature) } - override val u: Matrix by lazy> { CMMatrix(lup.u).withFeature(UFeature) } - override val p: Matrix by lazy { CMMatrix(lup.p) } + LUP -> object : LupDecomposition { + val lup by lazy { LUDecomposition(origin) } + override val pivot: IntBuffer get() = lup.pivot.asBuffer() + override val l: Matrix get() = lup.l.wrap() + override val u: Matrix get() = lup.u.wrap() } - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { - override val l: Matrix by lazy> { - val cholesky = CholeskyDecomposition(origin) - CMMatrix(cholesky.l).withFeature(LFeature) - } + Cholesky -> object : CholeskyDecomposition { + val cmCholesky by lazy { org.apache.commons.math3.linear.CholeskyDecomposition(origin) } + override val l: Matrix get() = cmCholesky.l.wrap() } - QRDecompositionFeature::class -> object : QRDecompositionFeature { - private val qr by lazy { QRDecomposition(origin) } - override val q: Matrix by lazy> { CMMatrix(qr.q).withFeature(OrthogonalFeature) } - override val r: Matrix by lazy> { CMMatrix(qr.r).withFeature(UFeature) } + QR -> object : QRDecomposition { + val cmQr by lazy { org.apache.commons.math3.linear.QRDecomposition(origin) } + override val q: Matrix get() = cmQr.q.wrap().withAttribute(OrthogonalAttribute) + override val r: Matrix get() = cmQr.r.wrap().withAttribute(UpperTriangular) } - SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature { - private val sv by lazy { SingularValueDecomposition(origin) } - override val u: Matrix by lazy { CMMatrix(sv.u) } - override val s: Matrix by lazy { CMMatrix(sv.s) } - override val v: Matrix by lazy { CMMatrix(sv.v) } - override val singularValues: Point by lazy { Float64Buffer(sv.singularValues) } + SVD -> object : space.kscience.kmath.linear.SingularValueDecomposition { + val cmSvd by lazy { SingularValueDecomposition(origin) } + + override val u: Matrix get() = cmSvd.u.wrap() + override val s: Matrix get() = cmSvd.s.wrap() + override val v: Matrix get() = cmSvd.v.wrap() + override val singularValues: Point get() = cmSvd.singularValues.asBuffer() + } + else -> null - }?.let(type::cast) + } + @Suppress("UNCHECKED_CAST") + return raw as V? } + } public operator fun CMMatrix.plus(other: CMMatrix): CMMatrix = CMMatrix(origin.add(other.origin)) diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt index 19799aab3..6b31fcd88 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt index c4dafb6a6..3c5f8f5e2 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/optimization/CMOptimizer.kt @@ -1,8 +1,9 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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:OptIn(UnstableKMathAPI::class) + package space.kscience.kmath.commons.optimization import org.apache.commons.math3.optim.* @@ -13,12 +14,13 @@ import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient import org.apache.commons.math3.optim.nonlinear.scalar.gradient.NonLinearConjugateGradientOptimizer import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer +import space.kscience.attributes.AttributesBuilder +import space.kscience.attributes.SetAttribute import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.expressions.SymbolIndexer import space.kscience.kmath.expressions.derivative import space.kscience.kmath.expressions.withSymbols -import space.kscience.kmath.misc.log import space.kscience.kmath.optimization.* import kotlin.collections.set import kotlin.reflect.KClass @@ -26,34 +28,25 @@ import kotlin.reflect.KClass public operator fun PointValuePair.component1(): DoubleArray = point public operator fun PointValuePair.component2(): Double = value -public class CMOptimizerEngine(public val optimizerBuilder: () -> MultivariateOptimizer) : OptimizationFeature { - override fun toString(): String = "CMOptimizer($optimizerBuilder)" -} +public object CMOptimizerEngine : OptimizationAttribute<() -> MultivariateOptimizer> /** * Specify a Commons-maths optimization engine */ -public fun FunctionOptimizationBuilder.cmEngine(optimizerBuilder: () -> MultivariateOptimizer) { - addFeature(CMOptimizerEngine(optimizerBuilder)) +public fun AttributesBuilder>.cmEngine(optimizerBuilder: () -> MultivariateOptimizer) { + set(CMOptimizerEngine, optimizerBuilder) } -public class CMOptimizerData(public val data: List OptimizationData>) : OptimizationFeature { - public constructor(vararg data: (SymbolIndexer.() -> OptimizationData)) : this(data.toList()) - - override fun toString(): String = "CMOptimizerData($data)" -} +public object CMOptimizerData : SetAttribute OptimizationData> /** * Specify Commons-maths optimization data. */ -public fun FunctionOptimizationBuilder.cmOptimizationData(data: SymbolIndexer.() -> OptimizationData) { - updateFeature { - val newData = (it?.data ?: emptyList()) + data - CMOptimizerData(newData) - } +public fun AttributesBuilder>.cmOptimizationData(data: SymbolIndexer.() -> OptimizationData) { + CMOptimizerData.add(data) } -public fun FunctionOptimizationBuilder.simplexSteps(vararg steps: Pair) { +public fun AttributesBuilder>.simplexSteps(vararg steps: Pair) { //TODO use convergence checker from features cmEngine { SimplexOptimizer(CMOptimizer.defaultConvergenceChecker) } cmOptimizationData { NelderMeadSimplex(mapOf(*steps).toDoubleArray()) } @@ -78,8 +71,8 @@ public object CMOptimizer : Optimizer> { ): FunctionOptimization { val startPoint = problem.startPoint - val parameters = problem.getFeature()?.symbols - ?: problem.getFeature>()?.point?.keys + val parameters = problem.attributes[OptimizationParameters] + ?: problem.attributes[OptimizationStartPoint()]?.keys ?: startPoint.keys @@ -90,7 +83,7 @@ public object CMOptimizer : Optimizer> { DEFAULT_MAX_ITER ) - val cmOptimizer: MultivariateOptimizer = problem.getFeature()?.optimizerBuilder?.invoke() + val cmOptimizer: MultivariateOptimizer = problem.attributes[CMOptimizerEngine]?.invoke() ?: NonLinearConjugateGradientOptimizer( NonLinearConjugateGradientOptimizer.Formula.FLETCHER_REEVES, convergenceChecker @@ -123,23 +116,26 @@ public object CMOptimizer : Optimizer> { } addOptimizationData(gradientFunction) - val logger = problem.getFeature() +// val logger = problem.attributes[OptimizationLog] - for (feature in problem.features) { - when (feature) { - is CMOptimizerData -> feature.data.forEach { dataBuilder -> - addOptimizationData(dataBuilder()) - } - is FunctionOptimizationTarget -> when (feature) { - FunctionOptimizationTarget.MAXIMIZE -> addOptimizationData(GoalType.MAXIMIZE) - FunctionOptimizationTarget.MINIMIZE -> addOptimizationData(GoalType.MINIMIZE) - } - else -> logger?.log { "The feature $feature is unused in optimization" } + problem.attributes[CMOptimizerData]?.let { builders: Set OptimizationData> -> + builders.forEach { dataBuilder -> + addOptimizationData(dataBuilder()) + } + } + + problem.attributes[FunctionOptimizationTarget]?.let { direction: OptimizationDirection -> + when (direction) { + OptimizationDirection.MAXIMIZE -> addOptimizationData(GoalType.MAXIMIZE) + OptimizationDirection.MINIMIZE -> addOptimizationData(GoalType.MINIMIZE) } } val (point, value) = cmOptimizer.optimize(*optimizationData.values.toTypedArray()) - return problem.withFeatures(OptimizationResult(point.toMap()), OptimizationValue(value)) + return problem.withAttributes { + result(point.toMap()) + value(value) + } } } } diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt index 32962659f..62d31021c 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/random/CMRandomGeneratorWrapper.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/transform/Transformations.kt b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/transform/Transformations.kt index de271aedc..838ab95d5 100644 --- a/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/transform/Transformations.kt +++ b/kmath-commons/src/jvmMain/kotlin/space/kscience/kmath/commons/transform/Transformations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt b/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt index 7c3c086ed..34905fa7b 100644 --- a/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt +++ b/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt b/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt index 9a249b29b..43857c539 100644 --- a/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt +++ b/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,8 @@ package space.kscience.kmath.commons.integration import org.junit.jupiter.api.Test import space.kscience.kmath.UnstableKMathAPI +import space.kscience.kmath.integration.IntegrandAbsoluteAccuracy +import space.kscience.kmath.integration.IntegrandRelativeAccuracy import space.kscience.kmath.integration.integrate import space.kscience.kmath.integration.value import space.kscience.kmath.operations.Float64Field.sin @@ -27,8 +29,8 @@ internal class IntegrationTest { @Test fun customSimpson() { val res = CMIntegrator.simpson().integrate(0.0..PI, { - targetRelativeAccuracy = 1e-4 - targetAbsoluteAccuracy = 1e-4 + IntegrandRelativeAccuracy(1e-4) + IntegrandAbsoluteAccuracy(1e-4) }, function).value assertTrue { abs(res - 2) < 1e-3 } assertTrue { abs(res - 2) > 1e-12 } diff --git a/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt b/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt index 5933d0d36..177a01b43 100644 --- a/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt +++ b/kmath-commons/src/jvmTest/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -31,7 +31,7 @@ internal class OptimizeTest { @Test fun testGradientOptimization() = runBlocking { val result = normal.optimizeWith(CMOptimizer, x to 1.0, y to 1.0) - println(result.resultPoint) + println(result.result) println(result.resultValue) } @@ -42,7 +42,7 @@ internal class OptimizeTest { //this sets simplex optimizer } - println(result.resultPoint) + println(result.result) println(result.resultValue) } @@ -73,8 +73,9 @@ internal class OptimizeTest { val result: FunctionOptimization = chi2.optimizeWith( CMOptimizer, mapOf(a to 1.5, b to 0.9, c to 1.0), - FunctionOptimizationTarget.MINIMIZE - ) + ){ + FunctionOptimizationTarget(OptimizationDirection.MINIMIZE) + } println(result) println("Chi2/dof = ${result.resultValue / (x.size - 3)}") } diff --git a/kmath-complex/README.md b/kmath-complex/README.md index 4e800b7ac..eec989eb3 100644 --- a/kmath-complex/README.md +++ b/kmath-complex/README.md @@ -8,19 +8,8 @@ Complex and hypercomplex number systems in KMath. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-complex:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-complex:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-complex:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -29,6 +18,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-complex:0.4.0-dev-1") + implementation("space.kscience:kmath-complex:0.4.0-dev-3") } ``` diff --git a/kmath-complex/build.gradle.kts b/kmath-complex/build.gradle.kts index 3e8b3b75e..9ac2d5ab4 100644 --- a/kmath-complex/build.gradle.kts +++ b/kmath-complex/build.gradle.kts @@ -10,6 +10,7 @@ kscience { dependencies { api(projects.kmathCore) + api(projects.kmathMemory) } testDependencies { diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt index b5f1aabe7..87e750387 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt @@ -1,16 +1,18 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.complex +import space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.memory.MemoryReader -import space.kscience.kmath.memory.MemorySpec -import space.kscience.kmath.memory.MemoryWriter +import space.kscience.kmath.memory.* import space.kscience.kmath.operations.* -import space.kscience.kmath.structures.* +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.MutableBuffer +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.* /** @@ -51,8 +53,11 @@ public object ComplexField : Norm, NumbersAddOps, ScaleOperations { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory { size, init -> - MutableMemoryBuffer.create(Complex, size, init) + override val bufferFactory: MutableBufferFactory = object : MutableBufferFactory { + override fun invoke(size: Int, builder: (Int) -> Complex): MutableBuffer = + MutableMemoryBuffer.create(Complex, size, builder) + + override val type: SafeType = safeTypeOf() } override val zero: Complex = 0.0.toComplex() @@ -129,12 +134,31 @@ public object ComplexField : } override fun power(arg: Complex, pow: Number): Complex = if (arg.im == 0.0) { - arg.re.pow(pow.toDouble()).toComplex() + val powDouble = pow.toDouble() + when { + arg.re > 0 -> arg.re.pow(powDouble).toComplex() + arg.re < 0 -> i * (-arg.re).pow(powDouble) + else -> if (powDouble == 0.0) { + one + } else { + zero + } + } } else { exp(pow * ln(arg)) } - public fun power(arg: Complex, pow: Complex): Complex = exp(pow * ln(arg)) + public fun power(arg: Complex, pow: Complex): Complex = if(arg == zero || arg == (-0.0).toComplex()){ + if(pow == zero){ + one + } else { + zero + } + } else { + exp(pow * ln(arg)) + } + + public fun Complex.pow(power: Complex): Complex = power(this, power) override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im)) @@ -189,6 +213,7 @@ public object ComplexField : override fun norm(arg: Complex): Complex = sqrt(arg.conjugate * arg) } + /** * Represents `double`-based complex number. * @@ -202,8 +227,10 @@ public data class Complex(val re: Double, val im: Double) { override fun toString(): String = "($re + i * $im)" public companion object : MemorySpec { - override val objectSize: Int - get() = 16 + + override val type: SafeType get() = safeTypeOf() + + override val objectSize: Int get() = 16 override fun MemoryReader.read(offset: Int): Complex = Complex(readDouble(offset), readDouble(offset + 8)) diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt index 90a6b3253..8fbf41732 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt index 8f3d15a26..6a6171975 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt @@ -1,19 +1,18 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.complex +import space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.memory.MemoryReader -import space.kscience.kmath.memory.MemorySpec -import space.kscience.kmath.memory.MemoryWriter +import space.kscience.kmath.memory.* import space.kscience.kmath.operations.* import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.MemoryBuffer import space.kscience.kmath.structures.MutableBuffer -import space.kscience.kmath.structures.MutableMemoryBuffer +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.* /** @@ -78,6 +77,7 @@ public class Quaternion( public companion object : MemorySpec { override val objectSize: Int get() = 32 + override val type: SafeType get() = safeTypeOf() override fun MemoryReader.read(offset: Int): Quaternion = Quaternion( readDouble(offset), @@ -122,7 +122,7 @@ public val Quaternion.reciprocal: Quaternion /** * Produce a normalized version of this quaternion */ -public fun Quaternion.normalized(): Quaternion = with(QuaternionAlgebra){ this@normalized / norm(this@normalized) } +public fun Quaternion.normalized(): Quaternion = with(QuaternionAlgebra) { this@normalized / norm(this@normalized) } /** * A field of [Quaternion]. @@ -131,6 +131,8 @@ public fun Quaternion.normalized(): Quaternion = with(QuaternionAlgebra){ this@n public object QuaternionAlgebra : Group, Norm, PowerOperations, ExponentialOperations, NumbersAddOps, ScaleOperations { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override val zero: Quaternion = Quaternion(0.0) public val one: Quaternion = Quaternion(1.0) diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt index ca3f8f43f..124703cbe 100644 --- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt +++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexBufferSpecTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt index e11f1c1ea..06f7f4580 100644 --- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt +++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -58,24 +58,30 @@ internal class ComplexFieldTest { } @Test - fun testInverseHyperbolicSine() { - assertEquals( - ComplexField { i * PI.toComplex() / 2 }, - ComplexField { asinh(i) }) + fun testInverseHyperbolicSine() = ComplexField { + assertEquals(i * PI.toComplex() / 2, asinh(i)) } @Test - fun testPower() { - assertEquals(ComplexField.zero, ComplexField { zero pow 2 }) - assertEquals(ComplexField.zero, ComplexField { zero pow 2 }) + fun testPower() = ComplexField { + assertEquals(zero, zero pow 2) + assertEquals(zero, zero pow 2) assertEquals( - ComplexField { i * 8 }.let { it.im.toInt() to it.re.toInt() }, - ComplexField { Complex(2, 2) pow 2 }.let { it.im.toInt() to it.re.toInt() }) + (i * 8).let { it.im.toInt() to it.re.toInt() }, + (Complex(2, 2) pow 2).let { it.im.toInt() to it.re.toInt() }) + + assertEquals(1.0, Complex(0.0).pow(Complex(0.0)).re, 0.01) + assertEquals(1.0, Complex(-0.0).pow(Complex(0.0)).re, 0.01) + assertEquals(0.0, Complex(0.0).pow(Complex(2.0)).re, 0.01) + assertEquals(0.0, Complex(-0.0).pow(Complex(2.0)).re, 0.01) + assertEquals(1.0, Complex(-1.0).pow(Complex(2.0)).re, 0.01) + assertEquals(2.0, power(Complex(-4.0, 0.0), 0.5 + 0 * i).im, 0.01) + assertEquals(2.0, power(Complex(-4.0, 0.0), 0.5).im, 0.01) } @Test - fun testNorm() { - assertEquals(2.toComplex(), ComplexField { norm(2 * i) }) + fun testNorm() = ComplexField { + assertEquals(2.toComplex(), norm(2 * i)) } } diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt index 1a35d1dd8..a736d5ac5 100644 --- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt +++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt index 767406e0b..9b7bdfa95 100644 --- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt +++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ExpressionFieldForComplexTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt index 1b371adca..b60b853de 100644 --- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt +++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/QuaternionTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/README.md b/kmath-core/README.md index b58105d2f..f1f42210b 100644 --- a/kmath-core/README.md +++ b/kmath-core/README.md @@ -11,23 +11,13 @@ objects to the expression by providing a context. Expressions can be used for a performance calculations to code generation. - [domains](src/commonMain/kotlin/space/kscience/kmath/domains) : Domains - [autodiff](src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt) : Automatic differentiation + - [linear.parallel](#) : Parallel implementation for `LinearAlgebra` ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-core:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-core:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-core:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -36,6 +26,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-core:0.4.0-dev-1") + implementation("space.kscience:kmath-core:0.4.0-dev-3") } ``` diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 42d8277bd..460c2e3c4 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -1,3 +1,14 @@ +public abstract interface annotation class space/kscience/kmath/PerformancePitfall : java/lang/annotation/Annotation { + public abstract fun message ()Ljava/lang/String; +} + +public abstract interface annotation class space/kscience/kmath/UnsafeKMathAPI : java/lang/annotation/Annotation { + public abstract fun message ()Ljava/lang/String; +} + +public abstract interface annotation class space/kscience/kmath/UnstableKMathAPI : java/lang/annotation/Annotation { +} + public final class space/kscience/kmath/data/ColumnarDataKt { } @@ -41,17 +52,19 @@ public final class space/kscience/kmath/expressions/DSCompiler { public final fun getSizes ()[[I } -public final class space/kscience/kmath/expressions/DerivationResult { - public fun (Ljava/lang/Object;Ljava/util/Map;Lspace/kscience/kmath/operations/Field;)V +public final class space/kscience/kmath/expressions/DerivationResult : space/kscience/attributes/WithType { + public synthetic fun (Ljava/lang/Object;Lkotlin/reflect/KType;Ljava/util/Map;Lspace/kscience/kmath/operations/Field;Lkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun derivative (Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object; public final fun div ()Ljava/lang/Object; public final fun getContext ()Lspace/kscience/kmath/operations/Field; + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public final fun getValue ()Ljava/lang/Object; } public final class space/kscience/kmath/expressions/DiffExpressionWithDefault : space/kscience/kmath/expressions/DifferentiableExpression { public fun (Lspace/kscience/kmath/expressions/DifferentiableExpression;Ljava/util/Map;)V public fun derivativeOrNull (Ljava/util/List;)Lspace/kscience/kmath/expressions/Expression; + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public fun invoke (Ljava/util/Map;)Ljava/lang/Object; } @@ -71,7 +84,7 @@ public final class space/kscience/kmath/expressions/DifferentiableExpressionKt { public final class space/kscience/kmath/expressions/DoubleExpression$Companion { } -public abstract interface class space/kscience/kmath/expressions/Expression { +public abstract interface class space/kscience/kmath/expressions/Expression : space/kscience/attributes/WithType { public abstract fun invoke (Ljava/util/Map;)Ljava/lang/Object; } @@ -80,6 +93,7 @@ public abstract interface class space/kscience/kmath/expressions/ExpressionAlgeb } public final class space/kscience/kmath/expressions/ExpressionKt { + public static final fun Expression-AckJDF4 (Lkotlin/reflect/KType;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; public static final fun callByString (Lspace/kscience/kmath/expressions/Expression;[Lkotlin/Pair;)Ljava/lang/Object; public static final fun callBySymbol (Lspace/kscience/kmath/expressions/Expression;[Lkotlin/Pair;)Ljava/lang/Object; public static final fun getBinding (Lspace/kscience/kmath/expressions/ExpressionAlgebra;)Lkotlin/properties/ReadOnlyProperty; @@ -88,6 +102,7 @@ public final class space/kscience/kmath/expressions/ExpressionKt { public final class space/kscience/kmath/expressions/ExpressionWithDefault : space/kscience/kmath/expressions/Expression { public fun (Lspace/kscience/kmath/expressions/Expression;Ljava/util/Map;)V + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public fun invoke (Ljava/util/Map;)Ljava/lang/Object; } @@ -110,11 +125,12 @@ public abstract class space/kscience/kmath/expressions/FunctionalExpressionAlgeb public synthetic fun const (Ljava/lang/Object;)Ljava/lang/Object; public fun const (Ljava/lang/Object;)Lspace/kscience/kmath/expressions/Expression; public final fun getAlgebra ()Lspace/kscience/kmath/operations/Algebra; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; } public final class space/kscience/kmath/expressions/FunctionalExpressionAlgebraKt { - public static final fun expression (Lspace/kscience/kmath/operations/DoubleField;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; + public static final fun expression (Lspace/kscience/kmath/operations/Float64Field;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; public static final fun expressionInExtendedField (Lspace/kscience/kmath/operations/ExtendedField;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; public static final fun expressionInField (Lspace/kscience/kmath/operations/Field;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; public static final fun expressionInGroup (Lspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; @@ -270,6 +286,7 @@ public final class space/kscience/kmath/expressions/MstExtendedField : space/ksc public fun divide (Lspace/kscience/kmath/expressions/MST;Lspace/kscience/kmath/expressions/MST;)Lspace/kscience/kmath/expressions/MST$Binary; public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; public fun exp (Lspace/kscience/kmath/expressions/MST;)Lspace/kscience/kmath/expressions/MST$Unary; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Lspace/kscience/kmath/expressions/MST$Numeric; public synthetic fun getZero ()Ljava/lang/Object; @@ -312,6 +329,7 @@ public final class space/kscience/kmath/expressions/MstField : space/kscience/km public fun bindSymbolOrNull (Ljava/lang/String;)Lspace/kscience/kmath/expressions/Symbol; public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun divide (Lspace/kscience/kmath/expressions/MST;Lspace/kscience/kmath/expressions/MST;)Lspace/kscience/kmath/expressions/MST$Binary; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Lspace/kscience/kmath/expressions/MST$Numeric; public synthetic fun getZero ()Ljava/lang/Object; @@ -338,6 +356,7 @@ public final class space/kscience/kmath/expressions/MstGroup : space/kscience/km public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public synthetic fun bindSymbolOrNull (Ljava/lang/String;)Ljava/lang/Object; public fun bindSymbolOrNull (Ljava/lang/String;)Lspace/kscience/kmath/expressions/Symbol; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getZero ()Ljava/lang/Object; public fun getZero ()Lspace/kscience/kmath/expressions/MST$Numeric; public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; @@ -360,6 +379,7 @@ public final class space/kscience/kmath/expressions/MstNumericAlgebra : space/ks public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/expressions/Symbol; public synthetic fun bindSymbolOrNull (Ljava/lang/String;)Ljava/lang/Object; public fun bindSymbolOrNull (Ljava/lang/String;)Lspace/kscience/kmath/expressions/Symbol; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/expressions/MST$Numeric; public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; @@ -372,6 +392,7 @@ public final class space/kscience/kmath/expressions/MstRing : space/kscience/kma public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public synthetic fun bindSymbolOrNull (Ljava/lang/String;)Ljava/lang/Object; public fun bindSymbolOrNull (Ljava/lang/String;)Lspace/kscience/kmath/expressions/Symbol; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Lspace/kscience/kmath/expressions/MST$Numeric; public synthetic fun getZero ()Ljava/lang/Object; @@ -398,11 +419,10 @@ public final class space/kscience/kmath/expressions/NamedMatrix : space/kscience public fun get (II)Ljava/lang/Object; public final fun get (Lspace/kscience/kmath/expressions/Symbol;Lspace/kscience/kmath/expressions/Symbol;)Ljava/lang/Object; public fun get ([I)Ljava/lang/Object; + public fun getAttributes ()Lspace/kscience/attributes/Attributes; public fun getColNum ()I public fun getColumns ()Ljava/util/List; public fun getDimension ()I - public synthetic fun getFeature (Lkotlin/reflect/KClass;)Ljava/lang/Object; - public fun getFeature (Lkotlin/reflect/KClass;)Lspace/kscience/kmath/nd/StructureFeature; public final fun getIndexer ()Lspace/kscience/kmath/expressions/SymbolIndexer; public fun getIndices ()Lspace/kscience/kmath/nd/ShapeIndexer; public fun getRowNum ()I @@ -425,6 +445,7 @@ public final class space/kscience/kmath/expressions/SimpleAutoDiffExpression : s public fun derivativeOrNull (Lspace/kscience/kmath/expressions/Symbol;)Lspace/kscience/kmath/expressions/Expression; public final fun getField ()Lspace/kscience/kmath/operations/Field; public final fun getFunction ()Lkotlin/jvm/functions/Function1; + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public fun invoke (Ljava/util/Map;)Ljava/lang/Object; } @@ -481,7 +502,8 @@ public class space/kscience/kmath/expressions/SimpleAutoDiffField : space/kscien public final fun derive (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun divide (Lspace/kscience/kmath/expressions/AutoDiffValue;Lspace/kscience/kmath/expressions/AutoDiffValue;)Lspace/kscience/kmath/expressions/AutoDiffValue; - public final fun getContext ()Lspace/kscience/kmath/operations/Field; + public final fun getAlgebra ()Lspace/kscience/kmath/operations/Field; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public final fun getD (Lspace/kscience/kmath/expressions/AutoDiffValue;)Ljava/lang/Object; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Lspace/kscience/kmath/expressions/AutoDiffValue; @@ -572,62 +594,117 @@ public final class space/kscience/kmath/linear/BufferedLinearSpaceKt { public static final fun getLinearSpace (Lspace/kscience/kmath/operations/Ring;)Lspace/kscience/kmath/linear/BufferedLinearSpace; } -public abstract interface class space/kscience/kmath/linear/CholeskyDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { +public abstract interface class space/kscience/kmath/linear/CholeskyDecomposition { public abstract fun getL ()Lspace/kscience/kmath/nd/Structure2D; } -public abstract interface class space/kscience/kmath/linear/DeterminantFeature : space/kscience/kmath/linear/MatrixFeature { - public abstract fun getDeterminant ()Ljava/lang/Object; +public final class space/kscience/kmath/linear/CholeskyDecompositionAttribute : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public fun ()V } -public abstract interface class space/kscience/kmath/linear/DiagonalFeature : space/kscience/kmath/linear/MatrixFeature { - public static final field Companion Lspace/kscience/kmath/linear/DiagonalFeature$Companion; +public final class space/kscience/kmath/linear/Determinant : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public synthetic fun (Lkotlin/reflect/KType;Lkotlin/jvm/internal/DefaultConstructorMarker;)V } -public final class space/kscience/kmath/linear/DiagonalFeature$Companion : space/kscience/kmath/linear/DiagonalFeature { -} - -public final class space/kscience/kmath/linear/DoubleLinearSpace : space/kscience/kmath/linear/LinearSpace { - public static final field INSTANCE Lspace/kscience/kmath/linear/DoubleLinearSpace; +public final class space/kscience/kmath/linear/Float64LinearSpace : space/kscience/kmath/linear/LinearSpace { + public static final field INSTANCE Lspace/kscience/kmath/linear/Float64LinearSpace; public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; public synthetic fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; - public fun buildVector-CZ9oacQ (ILkotlin/jvm/functions/Function2;)[D - public final fun div-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;D)[D + public fun buildVector-Vq5tpWc (ILkotlin/jvm/functions/Function2;)[D + public final fun div-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;D)[D public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public synthetic fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun dot-CZ9oacQ (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)[D - public fun getElementAlgebra ()Lspace/kscience/kmath/operations/DoubleField; + public fun dot-Vq5tpWc (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)[D + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float64Field; public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public synthetic fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun minus-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public fun minus-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public synthetic fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun plus-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public fun plus-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D public synthetic fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public fun times (Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/nd/Structure2D; public synthetic fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; public synthetic fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; - public fun times-CZ9oacQ (DLspace/kscience/kmath/structures/Buffer;)[D - public fun times-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;D)[D + public fun times-Vq5tpWc (DLspace/kscience/kmath/structures/Buffer;)[D + public fun times-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;D)[D public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; } -public final class space/kscience/kmath/linear/DoubleLinearSpaceKt { - public static final fun getLinearSpace (Lspace/kscience/kmath/operations/DoubleField;)Lspace/kscience/kmath/linear/DoubleLinearSpace; +public final class space/kscience/kmath/linear/Float64LinearSpaceKt { + public static final fun getLinearSpace (Lspace/kscience/kmath/operations/Float64Field;)Lspace/kscience/kmath/linear/Float64LinearSpace; } -public abstract interface class space/kscience/kmath/linear/InverseMatrixFeature : space/kscience/kmath/linear/MatrixFeature { - public abstract fun getInverse ()Lspace/kscience/kmath/nd/Structure2D; +public final class space/kscience/kmath/linear/Float64ParallelLinearSpace : space/kscience/kmath/linear/LinearSpace { + public static final field INSTANCE Lspace/kscience/kmath/linear/Float64ParallelLinearSpace; + public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; + public synthetic fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public fun buildVector-Vq5tpWc (ILkotlin/jvm/functions/Function2;)[D + public final fun div-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;D)[D + public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public synthetic fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun dot-Vq5tpWc (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)[D + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float64Field; + public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; + public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public synthetic fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun minus-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public synthetic fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun plus-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun times (Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/nd/Structure2D; + public synthetic fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; + public synthetic fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; + public fun times-Vq5tpWc (DLspace/kscience/kmath/structures/Buffer;)[D + public fun times-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;D)[D + public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; } -public final class space/kscience/kmath/linear/LFeature : space/kscience/kmath/linear/MatrixFeature { - public static final field INSTANCE Lspace/kscience/kmath/linear/LFeature; +public final class space/kscience/kmath/linear/Float64ParallelLinearSpaceKt { + public static final fun getParallel (Lspace/kscience/kmath/linear/Float64LinearSpace;)Lspace/kscience/kmath/linear/Float64ParallelLinearSpace; } -public abstract interface class space/kscience/kmath/linear/LUDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { - public abstract fun getL ()Lspace/kscience/kmath/nd/Structure2D; - public abstract fun getU ()Lspace/kscience/kmath/nd/Structure2D; +public final class space/kscience/kmath/linear/GenericLupDecomposition : space/kscience/kmath/linear/LupDecomposition { + public synthetic fun (Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZLkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getDeterminant ()Ljava/lang/Object; + public final fun getElementAlgebra ()Lspace/kscience/kmath/operations/Field; + public fun getL ()Lspace/kscience/kmath/nd/Structure2D; + public fun getPivot-M_oXE9g ()[I + public fun getU ()Lspace/kscience/kmath/nd/Structure2D; +} + +public final class space/kscience/kmath/linear/Inverted : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public fun ()V +} + +public abstract interface class space/kscience/kmath/linear/IsDiagonal : space/kscience/attributes/FlagAttribute, space/kscience/kmath/linear/MatrixAttribute { + public static final field Companion Lspace/kscience/kmath/linear/IsDiagonal$Companion; +} + +public final class space/kscience/kmath/linear/IsDiagonal$Companion : space/kscience/kmath/linear/IsDiagonal { +} + +public final class space/kscience/kmath/linear/IsUnit : space/kscience/kmath/linear/IsDiagonal { + public static final field INSTANCE Lspace/kscience/kmath/linear/IsUnit; +} + +public final class space/kscience/kmath/linear/IsZero : space/kscience/kmath/linear/IsDiagonal { + public static final field INSTANCE Lspace/kscience/kmath/linear/IsZero; +} + +public final class space/kscience/kmath/linear/LUDecomposition { + public fun (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)V + public final fun component1 ()Lspace/kscience/kmath/nd/Structure2D; + public final fun component2 ()Lspace/kscience/kmath/nd/Structure2D; + public final fun copy (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/LUDecomposition; + public static synthetic fun copy$default (Lspace/kscience/kmath/linear/LUDecomposition;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;ILjava/lang/Object;)Lspace/kscience/kmath/linear/LUDecomposition; + public fun equals (Ljava/lang/Object;)Z + public final fun getL ()Lspace/kscience/kmath/nd/Structure2D; + public final fun getU ()Lspace/kscience/kmath/nd/Structure2D; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; } public abstract interface class space/kscience/kmath/linear/LinearSolver { @@ -636,13 +713,15 @@ public abstract interface class space/kscience/kmath/linear/LinearSolver { public fun solve (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; } -public abstract interface class space/kscience/kmath/linear/LinearSpace { +public abstract interface class space/kscience/kmath/linear/LinearSpace : space/kscience/kmath/linear/MatrixScope { public static final field Companion Lspace/kscience/kmath/linear/LinearSpace$Companion; public abstract fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; public abstract fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public fun computeAttribute (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/StructureAttribute;)Ljava/lang/Object; public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public abstract fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; @@ -665,39 +744,54 @@ public final class space/kscience/kmath/linear/LinearSpaceKt { public static final fun invoke (Lspace/kscience/kmath/linear/LinearSpace;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } -public final class space/kscience/kmath/linear/LupDecomposition : space/kscience/kmath/linear/DeterminantFeature, space/kscience/kmath/linear/LupDecompositionFeature { - public fun (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZ)V - public final fun getContext ()Lspace/kscience/kmath/linear/LinearSpace; - public fun getDeterminant ()Ljava/lang/Object; - public final fun getElementContext ()Lspace/kscience/kmath/operations/Field; - public fun getL ()Lspace/kscience/kmath/nd/Structure2D; - public final fun getLu ()Lspace/kscience/kmath/nd/Structure2D; - public fun getP ()Lspace/kscience/kmath/nd/Structure2D; - public final fun getPivot ()[I - public fun getU ()Lspace/kscience/kmath/nd/Structure2D; +public final class space/kscience/kmath/linear/LowerTriangular : space/kscience/attributes/FlagAttribute, space/kscience/kmath/linear/MatrixAttribute { + public static final field INSTANCE Lspace/kscience/kmath/linear/LowerTriangular; } -public abstract interface class space/kscience/kmath/linear/LupDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { +public final class space/kscience/kmath/linear/LuDecompositionAttribute : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public fun ()V +} + +public abstract interface class space/kscience/kmath/linear/LupDecomposition { public abstract fun getL ()Lspace/kscience/kmath/nd/Structure2D; - public abstract fun getP ()Lspace/kscience/kmath/nd/Structure2D; + public abstract fun getPivot-M_oXE9g ()[I public abstract fun getU ()Lspace/kscience/kmath/nd/Structure2D; } +public final class space/kscience/kmath/linear/LupDecompositionAttribute : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public fun ()V +} + public final class space/kscience/kmath/linear/LupDecompositionKt { public static final fun abs (Lspace/kscience/kmath/linear/LinearSpace;Ljava/lang/Comparable;)Ljava/lang/Comparable; - public static final fun lup (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/linear/LupDecomposition; - public static final fun lup (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/MutableBufferFactory;Lspace/kscience/kmath/nd/Structure2D;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/LupDecomposition; - public static synthetic fun lup$default (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;DILjava/lang/Object;)Lspace/kscience/kmath/linear/LupDecomposition; + public static final fun getLUP (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/LupDecompositionAttribute; + public static final fun lup (Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/linear/GenericLupDecomposition; + public static final fun lup (Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/GenericLupDecomposition; + public static synthetic fun lup$default (Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;DILjava/lang/Object;)Lspace/kscience/kmath/linear/GenericLupDecomposition; public static final fun lupSolver (Lspace/kscience/kmath/linear/LinearSpace;D)Lspace/kscience/kmath/linear/LinearSolver; - public static final fun lupSolver (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/MutableBufferFactory;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/LinearSolver; + public static final fun lupSolver (Lspace/kscience/kmath/linear/LinearSpace;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/LinearSolver; public static synthetic fun lupSolver$default (Lspace/kscience/kmath/linear/LinearSpace;DILjava/lang/Object;)Lspace/kscience/kmath/linear/LinearSolver; + public static final fun pivotMatrix (Lspace/kscience/kmath/linear/LupDecomposition;Lspace/kscience/kmath/linear/LinearSpace;)Lspace/kscience/kmath/nd/Structure2D; } -public final class space/kscience/kmath/linear/MatrixBuilder { +public abstract interface class space/kscience/kmath/linear/MatrixAttribute : space/kscience/kmath/nd/StructureAttribute { +} + +public final class space/kscience/kmath/linear/MatrixAttributesKt { + public static final fun getCholesky (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/CholeskyDecompositionAttribute; + public static final fun getDeterminant (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/Determinant; + public static final fun getInverted (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/Inverted; + public static final fun getLU (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/LuDecompositionAttribute; + public static final fun getQR (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/QRDecompositionAttribute; + public static final fun getSVD (Lspace/kscience/kmath/linear/MatrixScope;)Lspace/kscience/kmath/linear/SVDAttribute; +} + +public final class space/kscience/kmath/linear/MatrixBuilder : space/kscience/attributes/WithType { public fun (Lspace/kscience/kmath/linear/LinearSpace;II)V public final fun getColumns ()I public final fun getLinearSpace ()Lspace/kscience/kmath/linear/LinearSpace; public final fun getRows ()I + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public final fun invoke ([Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; } @@ -709,23 +803,17 @@ public final class space/kscience/kmath/linear/MatrixBuilderKt { public static final fun symmetric (Lspace/kscience/kmath/linear/MatrixBuilder;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/Structure2D; } -public abstract interface class space/kscience/kmath/linear/MatrixFeature : space/kscience/kmath/nd/StructureFeature { -} - -public final class space/kscience/kmath/linear/MatrixFeaturesKt { - public static final fun DeterminantFeature (Ljava/lang/Object;)Lspace/kscience/kmath/linear/DeterminantFeature; +public abstract interface class space/kscience/kmath/linear/MatrixScope : space/kscience/attributes/AttributeScope, space/kscience/attributes/WithType { } public final class space/kscience/kmath/linear/MatrixWrapper : space/kscience/kmath/nd/Structure2D { public fun elements ()Lkotlin/sequences/Sequence; public fun get (II)Ljava/lang/Object; public fun get ([I)Ljava/lang/Object; + public fun getAttributes ()Lspace/kscience/attributes/Attributes; public fun getColNum ()I public fun getColumns ()Ljava/util/List; public fun getDimension ()I - public synthetic fun getFeature (Lkotlin/reflect/KClass;)Ljava/lang/Object; - public fun getFeature (Lkotlin/reflect/KClass;)Lspace/kscience/kmath/nd/StructureFeature; - public final fun getFeatures-En6fw3w ()Ljava/util/Map; public fun getIndices ()Lspace/kscience/kmath/nd/ShapeIndexer; public final fun getOrigin ()Lspace/kscience/kmath/nd/Structure2D; public fun getRowNum ()I @@ -735,50 +823,63 @@ public final class space/kscience/kmath/linear/MatrixWrapper : space/kscience/km } public final class space/kscience/kmath/linear/MatrixWrapperKt { - public static final fun one (Lspace/kscience/kmath/linear/LinearSpace;II)Lspace/kscience/kmath/nd/Structure2D; - public static final fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/linear/MatrixFeature;)Lspace/kscience/kmath/linear/MatrixWrapper; - public static final fun transpose (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static final fun withFeature (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/linear/MatrixFeature;)Lspace/kscience/kmath/linear/MatrixWrapper; - public static final fun withFeatures (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Iterable;)Lspace/kscience/kmath/linear/MatrixWrapper; - public static final fun zero (Lspace/kscience/kmath/linear/LinearSpace;II)Lspace/kscience/kmath/nd/Structure2D; + public static final fun modifyAttributes (Lspace/kscience/kmath/nd/Structure2D;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/MatrixWrapper; + public static final fun one (Lspace/kscience/kmath/linear/LinearSpace;II)Lspace/kscience/kmath/linear/MatrixWrapper; + public static final fun withAttribute (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/attributes/Attribute;)Lspace/kscience/kmath/linear/MatrixWrapper; + public static final fun withAttribute (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/attributes/Attribute;Ljava/lang/Object;)Lspace/kscience/kmath/linear/MatrixWrapper; + public static final fun zero (Lspace/kscience/kmath/linear/LinearSpace;II)Lspace/kscience/kmath/linear/MatrixWrapper; } -public final class space/kscience/kmath/linear/OrthogonalFeature : space/kscience/kmath/linear/MatrixFeature { - public static final field INSTANCE Lspace/kscience/kmath/linear/OrthogonalFeature; +public final class space/kscience/kmath/linear/OrthogonalAttribute : space/kscience/attributes/FlagAttribute, space/kscience/kmath/linear/MatrixAttribute { + public static final field INSTANCE Lspace/kscience/kmath/linear/OrthogonalAttribute; } -public abstract interface class space/kscience/kmath/linear/QRDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { +public abstract interface class space/kscience/kmath/linear/QRDecomposition { public abstract fun getQ ()Lspace/kscience/kmath/nd/Structure2D; public abstract fun getR ()Lspace/kscience/kmath/nd/Structure2D; } -public abstract interface class space/kscience/kmath/linear/SingularValueDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { +public final class space/kscience/kmath/linear/QRDecompositionAttribute : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public fun ()V +} + +public final class space/kscience/kmath/linear/SVDAttribute : space/kscience/attributes/PolymorphicAttribute, space/kscience/kmath/linear/MatrixAttribute { + public fun ()V +} + +public abstract interface class space/kscience/kmath/linear/SingularValueDecomposition { public abstract fun getS ()Lspace/kscience/kmath/nd/Structure2D; public abstract fun getSingularValues ()Lspace/kscience/kmath/structures/Buffer; public abstract fun getU ()Lspace/kscience/kmath/nd/Structure2D; public abstract fun getV ()Lspace/kscience/kmath/nd/Structure2D; } -public final class space/kscience/kmath/linear/SymmetricMatrixFeature : space/kscience/kmath/linear/MatrixFeature { - public static final field INSTANCE Lspace/kscience/kmath/linear/SymmetricMatrixFeature; +public final class space/kscience/kmath/linear/Symmetric : space/kscience/attributes/FlagAttribute, space/kscience/kmath/linear/MatrixAttribute { + public static final field INSTANCE Lspace/kscience/kmath/linear/Symmetric; } -public final class space/kscience/kmath/linear/TransposedFeature : space/kscience/kmath/linear/MatrixFeature { +public final class space/kscience/kmath/linear/TransposedKt { + public static final fun transposed (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; +} + +public final class space/kscience/kmath/linear/TransposedMatrix : space/kscience/kmath/nd/Structure2D { public fun (Lspace/kscience/kmath/nd/Structure2D;)V - public final fun getOriginal ()Lspace/kscience/kmath/nd/Structure2D; + public fun get (II)Ljava/lang/Object; + public fun getAttributes ()Lspace/kscience/attributes/Attributes; + public fun getColNum ()I + public final fun getOrigin ()Lspace/kscience/kmath/nd/Structure2D; + public fun getRowNum ()I } -public final class space/kscience/kmath/linear/UFeature : space/kscience/kmath/linear/MatrixFeature { - public static final field INSTANCE Lspace/kscience/kmath/linear/UFeature; -} - -public final class space/kscience/kmath/linear/UnitFeature : space/kscience/kmath/linear/DiagonalFeature { - public static final field INSTANCE Lspace/kscience/kmath/linear/UnitFeature; +public final class space/kscience/kmath/linear/UpperTriangular : space/kscience/attributes/FlagAttribute, space/kscience/kmath/linear/MatrixAttribute { + public static final field INSTANCE Lspace/kscience/kmath/linear/UpperTriangular; } public final class space/kscience/kmath/linear/VirtualMatrix : space/kscience/kmath/nd/Structure2D { - public fun (IILkotlin/jvm/functions/Function2;)V + public fun (IILspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function2;)V + public synthetic fun (IILspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun get (II)Ljava/lang/Object; + public fun getAttributes ()Lspace/kscience/attributes/Attributes; public fun getColNum ()I public final fun getGenerator ()Lkotlin/jvm/functions/Function2; public fun getRowNum ()I @@ -786,11 +887,8 @@ public final class space/kscience/kmath/linear/VirtualMatrix : space/kscience/km } public final class space/kscience/kmath/linear/VirtualMatrixKt { - public static final fun virtual (Lspace/kscience/kmath/linear/MatrixBuilder;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/VirtualMatrix; -} - -public final class space/kscience/kmath/linear/ZeroFeature : space/kscience/kmath/linear/DiagonalFeature { - public static final field INSTANCE Lspace/kscience/kmath/linear/ZeroFeature; + public static final fun virtual (Lspace/kscience/kmath/linear/MatrixBuilder;Lspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/VirtualMatrix; + public static synthetic fun virtual$default (Lspace/kscience/kmath/linear/MatrixBuilder;Lspace/kscience/attributes/Attributes;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lspace/kscience/kmath/linear/VirtualMatrix; } public final class space/kscience/kmath/misc/CollectionsKt { @@ -818,42 +916,6 @@ public final class space/kscience/kmath/misc/CumulativeKt { public static final fun cumulativeSumOfLong (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; } -public abstract interface class space/kscience/kmath/misc/Feature { - public fun getKey ()Lkotlin/reflect/KClass; -} - -public final class space/kscience/kmath/misc/FeatureSet : space/kscience/kmath/misc/Featured { - public static final field Companion Lspace/kscience/kmath/misc/FeatureSet$Companion; - public static final synthetic fun box-impl (Ljava/util/Map;)Lspace/kscience/kmath/misc/FeatureSet; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Ljava/util/Map;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Ljava/util/Map;Ljava/util/Map;)Z - public synthetic fun getFeature (Lkotlin/reflect/KClass;)Ljava/lang/Object; - public fun getFeature (Lkotlin/reflect/KClass;)Lspace/kscience/kmath/misc/Feature; - public static fun getFeature-impl (Ljava/util/Map;Lkotlin/reflect/KClass;)Lspace/kscience/kmath/misc/Feature; - public final fun getFeatures ()Ljava/util/Map; - public fun hashCode ()I - public static fun hashCode-impl (Ljava/util/Map;)I - public static final fun iterator-impl (Ljava/util/Map;)Ljava/util/Iterator; - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Ljava/util/Map;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Ljava/util/Map; - public static final fun with-JVE9-Rk (Ljava/util/Map;Ljava/lang/Iterable;)Ljava/util/Map; - public static final fun with-JVE9-Rk (Ljava/util/Map;[Lspace/kscience/kmath/misc/Feature;)Ljava/util/Map; - public static final fun with-YU1a0hQ (Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map; - public static final fun with-h58Sd8I (Ljava/util/Map;Lspace/kscience/kmath/misc/Feature;Lkotlin/reflect/KClass;)Ljava/util/Map; - public static synthetic fun with-h58Sd8I$default (Ljava/util/Map;Lspace/kscience/kmath/misc/Feature;Lkotlin/reflect/KClass;ILjava/lang/Object;)Ljava/util/Map; -} - -public final class space/kscience/kmath/misc/FeatureSet$Companion { - public final fun of-JVE9-Rk (Ljava/lang/Iterable;)Ljava/util/Map; - public final fun of-JVE9-Rk ([Lspace/kscience/kmath/misc/Feature;)Ljava/util/Map; -} - -public abstract interface class space/kscience/kmath/misc/Featured { - public abstract fun getFeature (Lkotlin/reflect/KClass;)Ljava/lang/Object; -} - public abstract interface class space/kscience/kmath/misc/Loggable { public static final field Companion Lspace/kscience/kmath/misc/Loggable$Companion; public static final field INFO Ljava/lang/String; @@ -879,6 +941,7 @@ public final class space/kscience/kmath/misc/SortingKt { public static final fun sortedBy (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; public static final fun sortedByDescending (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; public static final fun sortedDescending (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public static final fun sortedWith (Lspace/kscience/kmath/structures/Buffer;Ljava/util/Comparator;)Lspace/kscience/kmath/structures/Buffer; } public abstract interface class space/kscience/kmath/nd/AlgebraND : space/kscience/kmath/operations/Algebra { @@ -887,7 +950,8 @@ public abstract interface class space/kscience/kmath/nd/AlgebraND : space/kscien public fun invoke (Lkotlin/jvm/functions/Function1;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; - public abstract fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; + public abstract fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; public fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; } @@ -895,6 +959,7 @@ public final class space/kscience/kmath/nd/AlgebraND$Companion { } public final class space/kscience/kmath/nd/AlgebraNDExtentionsKt { + public static final fun mutableStructureND (Lspace/kscience/kmath/nd/AlgebraND;I[ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; public static final fun one-waz_sdI (Lspace/kscience/kmath/nd/AlgebraND;[I)Lspace/kscience/kmath/nd/StructureND; public static final fun oneVarArg (Lspace/kscience/kmath/nd/AlgebraND;I[I)Lspace/kscience/kmath/nd/StructureND; public static final fun structureND (Lspace/kscience/kmath/nd/AlgebraND;I[ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; @@ -911,6 +976,8 @@ public abstract interface class space/kscience/kmath/nd/BufferAlgebraND : space/ public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; + public fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableBufferND; + public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; public fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; public fun toBufferND (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; @@ -927,6 +994,7 @@ public final class space/kscience/kmath/nd/BufferAlgebraNDKt { public static final fun getNd (Lspace/kscience/kmath/operations/BufferAlgebra;)Lspace/kscience/kmath/nd/BufferedGroupNDOps; public static final fun getNd (Lspace/kscience/kmath/operations/BufferAlgebra;)Lspace/kscience/kmath/nd/BufferedRingOpsND; public static final fun mapInline (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/BufferND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; + public static final fun mutableStructureND (Lspace/kscience/kmath/nd/BufferAlgebraND;[ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableBufferND; public static final fun structureND (Lspace/kscience/kmath/nd/BufferAlgebraND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; public static final fun structureND (Lspace/kscience/kmath/nd/BufferAlgebraND;[ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; } @@ -940,13 +1008,6 @@ public class space/kscience/kmath/nd/BufferND : space/kscience/kmath/nd/Structur public fun toString ()Ljava/lang/String; } -public final class space/kscience/kmath/nd/BufferNDKt { - public static final fun BufferND-bYNkpeI ([ILspace/kscience/kmath/structures/BufferFactory;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; - public static synthetic fun BufferND-bYNkpeI$default ([ILspace/kscience/kmath/structures/BufferFactory;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/BufferND; - public static final fun MutableBufferND-bYNkpeI ([ILspace/kscience/kmath/structures/MutableBufferFactory;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/MutableBufferND; - public static synthetic fun MutableBufferND-bYNkpeI$default ([ILspace/kscience/kmath/structures/MutableBufferFactory;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/MutableBufferND; -} - public class space/kscience/kmath/nd/BufferedFieldOpsND : space/kscience/kmath/nd/BufferedRingOpsND, space/kscience/kmath/nd/FieldOpsND { public fun (Lspace/kscience/kmath/operations/BufferAlgebra;Lkotlin/jvm/functions/Function1;)V public synthetic fun (Lspace/kscience/kmath/operations/BufferAlgebra;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V @@ -961,6 +1022,7 @@ public class space/kscience/kmath/nd/BufferedGroupNDOps : space/kscience/kmath/n public synthetic fun (Lspace/kscience/kmath/operations/BufferAlgebra;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun getBufferAlgebra ()Lspace/kscience/kmath/operations/BufferAlgebra; public fun getIndexerBuilder ()Lkotlin/jvm/functions/Function1; + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; public fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; } @@ -983,137 +1045,6 @@ public final class space/kscience/kmath/nd/ColumnStrides : space/kscience/kmath/ public final class space/kscience/kmath/nd/ColumnStrides$Companion { } -public final class space/kscience/kmath/nd/DoubleBufferND : space/kscience/kmath/nd/MutableBufferND, space/kscience/kmath/nd/MutableStructureNDOfDouble { - public synthetic fun (Lspace/kscience/kmath/nd/ShapeIndexer;[DLkotlin/jvm/internal/DefaultConstructorMarker;)V - public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; - public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun getBuffer-Dv3HvWU ()[D - public fun getDouble ([I)D - public fun setDouble ([ID)V -} - -public final class space/kscience/kmath/nd/DoubleFieldND : space/kscience/kmath/nd/DoubleFieldOpsND, space/kscience/kmath/nd/FieldND, space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/NumbersAddOps { - public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun getShape-IIYLAfE ()[I - public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun power (Ljava/lang/Object;I)Ljava/lang/Object; - public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun power (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun power-Qn1smSk (Ljava/lang/Object;I)Ljava/lang/Object; - public fun power-Qn1smSk (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; -} - -public final class space/kscience/kmath/nd/DoubleFieldNDKt { - public static final fun getNdAlgebra (Lspace/kscience/kmath/operations/DoubleField;)Lspace/kscience/kmath/nd/DoubleFieldOpsND; - public static final fun ndAlgebra (Lspace/kscience/kmath/operations/DoubleField;[I)Lspace/kscience/kmath/nd/DoubleFieldND; - public static final fun ndAlgebra-waz_sdI (Lspace/kscience/kmath/operations/DoubleField;[I)Lspace/kscience/kmath/nd/DoubleFieldND; -} - -public abstract class space/kscience/kmath/nd/DoubleFieldOpsND : space/kscience/kmath/nd/BufferedFieldOpsND, space/kscience/kmath/operations/ExtendedFieldOps, space/kscience/kmath/operations/ScaleOperations { - public static final field Companion Lspace/kscience/kmath/nd/DoubleFieldOpsND$Companion; - public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; - public fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; - public fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; - public fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; - public fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun div (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun div (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public synthetic fun div (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public fun div (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; - public fun div (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun divide (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun divide (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; - public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; - public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; - public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; - protected final fun mapInline (Lspace/kscience/kmath/nd/DoubleBufferND;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun minus (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public fun minus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; - public fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public fun plus (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public synthetic fun plus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public fun plus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; - public fun plus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; - public fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; - public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; - public fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; - public fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun times (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun times (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun toBufferND (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; - public fun toBufferND (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public synthetic fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public synthetic fun unaryPlus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryPlus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/DoubleBufferND; - public fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; - public synthetic fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; -} - -public final class space/kscience/kmath/nd/DoubleFieldOpsND$Companion : space/kscience/kmath/nd/DoubleFieldOpsND { -} - public abstract interface class space/kscience/kmath/nd/FieldND : space/kscience/kmath/nd/FieldOpsND, space/kscience/kmath/nd/RingND, space/kscience/kmath/nd/WithShape, space/kscience/kmath/operations/Field { public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Lspace/kscience/kmath/nd/StructureND; @@ -1128,6 +1059,137 @@ public abstract interface class space/kscience/kmath/nd/FieldOpsND : space/kscie public fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; } +public abstract class space/kscience/kmath/nd/Floa64FieldOpsND : space/kscience/kmath/nd/BufferedFieldOpsND, space/kscience/kmath/operations/ExtendedFieldOps, space/kscience/kmath/operations/ScaleOperations { + public static final field Companion Lspace/kscience/kmath/nd/Floa64FieldOpsND$Companion; + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public fun div (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun div (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun div (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun div (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/Float64BufferND; + public fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun div (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun divide (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; + public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; + protected final fun mapInline (Lspace/kscience/kmath/nd/Float64BufferND;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/Float64BufferND; + public fun minus (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableBufferND; + public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun plus (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun plus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; + public fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun times (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; + public fun times (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Float64BufferND; + public fun times (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun toBufferND (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; + public fun toBufferND (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun unaryPlus (Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryPlus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; + public synthetic fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; +} + +public final class space/kscience/kmath/nd/Floa64FieldOpsND$Companion : space/kscience/kmath/nd/Floa64FieldOpsND { +} + +public final class space/kscience/kmath/nd/Float64BufferND : space/kscience/kmath/nd/MutableBufferND, space/kscience/kmath/nd/MutableStructureNDOfDouble { + public synthetic fun (Lspace/kscience/kmath/nd/ShapeIndexer;[DLkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; + public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/MutableBuffer; + public fun getBuffer-E20IKn8 ()[D + public fun getDouble ([I)D + public fun setDouble ([ID)V +} + +public final class space/kscience/kmath/nd/Float64FieldND : space/kscience/kmath/nd/Floa64FieldOpsND, space/kscience/kmath/nd/FieldND, space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/NumbersAddOps { + public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public fun getShape-IIYLAfE ()[I + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun power (Ljava/lang/Object;I)Ljava/lang/Object; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun power (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd/Float64BufferND; + public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun power-Qn1smSk (Ljava/lang/Object;I)Ljava/lang/Object; + public fun power-Qn1smSk (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Float64BufferND; +} + +public final class space/kscience/kmath/nd/Float64FieldNDKt { + public static final fun getNdAlgebra (Lspace/kscience/kmath/operations/Float64Field;)Lspace/kscience/kmath/nd/Floa64FieldOpsND; + public static final fun ndAlgebra (Lspace/kscience/kmath/operations/Float64Field;[I)Lspace/kscience/kmath/nd/Float64FieldND; + public static final fun ndAlgebra-waz_sdI (Lspace/kscience/kmath/operations/Float64Field;[I)Lspace/kscience/kmath/nd/Float64FieldND; +} + public abstract interface class space/kscience/kmath/nd/GroupND : space/kscience/kmath/nd/GroupOpsND, space/kscience/kmath/nd/WithShape, space/kscience/kmath/operations/Group { public synthetic fun getZero ()Ljava/lang/Object; public fun getZero ()Lspace/kscience/kmath/nd/StructureND; @@ -1137,6 +1199,7 @@ public abstract interface class space/kscience/kmath/nd/GroupOpsND : space/kscie public static final field Companion Lspace/kscience/kmath/nd/GroupOpsND$Companion; public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; public fun plus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; @@ -1152,11 +1215,29 @@ public final class space/kscience/kmath/nd/IndexOutOfShapeException : java/lang/ public final fun getShape-IIYLAfE ()[I } +public final class space/kscience/kmath/nd/Int16RingND : space/kscience/kmath/nd/Int16RingOpsND, space/kscience/kmath/nd/RingND, space/kscience/kmath/operations/NumbersAddOps { + public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun getShape-IIYLAfE ()[I + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/BufferND; +} + +public final class space/kscience/kmath/nd/Int16RingNDKt { + public static final fun withNdAlgebra (Lspace/kscience/kmath/operations/Int16Ring;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; +} + +public abstract class space/kscience/kmath/nd/Int16RingOpsND : space/kscience/kmath/nd/BufferedRingOpsND { + public static final field Companion Lspace/kscience/kmath/nd/Int16RingOpsND$Companion; +} + +public final class space/kscience/kmath/nd/Int16RingOpsND$Companion : space/kscience/kmath/nd/Int16RingOpsND { +} + public final class space/kscience/kmath/nd/IntBufferND : space/kscience/kmath/nd/MutableBufferND { public synthetic fun (Lspace/kscience/kmath/nd/ShapeIndexer;[ILkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun getBuffer-ir4F4A8 ()[I + public fun getBuffer-M_oXE9g ()[I } public final class space/kscience/kmath/nd/IntRingND : space/kscience/kmath/nd/IntRingOpsND, space/kscience/kmath/nd/RingND, space/kscience/kmath/operations/NumbersAddOps { @@ -1167,7 +1248,7 @@ public final class space/kscience/kmath/nd/IntRingND : space/kscience/kmath/nd/I } public final class space/kscience/kmath/nd/IntRingNDKt { - public static final fun withNdAlgebra (Lspace/kscience/kmath/operations/IntRing;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun withNdAlgebra (Lspace/kscience/kmath/operations/Int32Ring;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; } public abstract class space/kscience/kmath/nd/IntRingOpsND : space/kscience/kmath/nd/BufferedRingOpsND { @@ -1201,6 +1282,15 @@ public abstract interface class space/kscience/kmath/nd/MutableStructureND : spa public abstract fun set ([ILjava/lang/Object;)V } +public final class space/kscience/kmath/nd/MutableStructureNDAccessorBuffer : space/kscience/kmath/structures/MutableBuffer { + public fun (Lspace/kscience/kmath/nd/MutableStructureND;ILkotlin/jvm/functions/Function1;)V + public fun get (I)Ljava/lang/Object; + public fun getSize ()I + public final fun getStructure ()Lspace/kscience/kmath/nd/MutableStructureND; + public fun set (ILjava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + public abstract interface class space/kscience/kmath/nd/MutableStructureNDOfDouble : space/kscience/kmath/nd/MutableStructureND, space/kscience/kmath/nd/StructureNDOfDouble { public abstract fun setDouble ([ID)V } @@ -1332,24 +1422,6 @@ public final class space/kscience/kmath/nd/ShapeNDKt { public static final fun transposed-bYNkpeI ([III)[I } -public final class space/kscience/kmath/nd/ShortRingND : space/kscience/kmath/nd/ShortRingOpsND, space/kscience/kmath/nd/RingND, space/kscience/kmath/operations/NumbersAddOps { - public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun getShape-IIYLAfE ()[I - public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/BufferND; -} - -public final class space/kscience/kmath/nd/ShortRingNDKt { - public static final fun withNdAlgebra (Lspace/kscience/kmath/operations/ShortRing;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; -} - -public abstract class space/kscience/kmath/nd/ShortRingOpsND : space/kscience/kmath/nd/BufferedRingOpsND { - public static final field Companion Lspace/kscience/kmath/nd/ShortRingOpsND$Companion; -} - -public final class space/kscience/kmath/nd/ShortRingOpsND$Companion : space/kscience/kmath/nd/ShortRingOpsND { -} - public abstract class space/kscience/kmath/nd/Strides : space/kscience/kmath/nd/ShapeIndexer { public fun ()V public fun asSequence ()Lkotlin/sequences/Sequence; @@ -1392,26 +1464,19 @@ public final class space/kscience/kmath/nd/Structure2DKt { public static final fun as2D (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/Structure2D; } -public abstract interface class space/kscience/kmath/nd/StructureFeature : space/kscience/kmath/misc/Feature { +public abstract interface class space/kscience/kmath/nd/StructureAttribute : space/kscience/attributes/Attribute { } -public abstract interface class space/kscience/kmath/nd/StructureND : space/kscience/kmath/misc/Featured, space/kscience/kmath/nd/WithShape { +public abstract interface class space/kscience/kmath/nd/StructureND : space/kscience/attributes/AttributeContainer, space/kscience/kmath/nd/WithShape { public static final field Companion Lspace/kscience/kmath/nd/StructureND$Companion; public fun elements ()Lkotlin/sequences/Sequence; public abstract fun get ([I)Ljava/lang/Object; + public fun getAttributes ()Lspace/kscience/attributes/Attributes; public fun getDimension ()I - public synthetic fun getFeature (Lkotlin/reflect/KClass;)Ljava/lang/Object; - public fun getFeature (Lkotlin/reflect/KClass;)Lspace/kscience/kmath/nd/StructureFeature; public abstract fun getShape-IIYLAfE ()[I } public final class space/kscience/kmath/nd/StructureND$Companion { - public final fun auto (Lkotlin/reflect/KClass;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; - public final fun auto (Lkotlin/reflect/KClass;[ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; - public final fun buffered (Lspace/kscience/kmath/nd/Strides;Lspace/kscience/kmath/structures/BufferFactory;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; - public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/StructureND$Companion;Lspace/kscience/kmath/nd/Strides;Lspace/kscience/kmath/structures/BufferFactory;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/BufferND; - public final fun buffered-bYNkpeI ([ILspace/kscience/kmath/structures/BufferFactory;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; - public static synthetic fun buffered-bYNkpeI$default (Lspace/kscience/kmath/nd/StructureND$Companion;[ILspace/kscience/kmath/structures/BufferFactory;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/BufferND; public final fun contentEquals (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Z public final fun contentEquals (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;D)Z public static synthetic fun contentEquals$default (Lspace/kscience/kmath/nd/StructureND$Companion;Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;DILjava/lang/Object;)Z @@ -1419,6 +1484,9 @@ public final class space/kscience/kmath/nd/StructureND$Companion { } public final class space/kscience/kmath/nd/StructureNDKt { + public static final fun BufferND--rwW0uw (Lkotlin/reflect/KType;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; + public static final fun BufferND--rwW0uw (Lkotlin/reflect/KType;[ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; + public static final fun BufferND-vHyE91E (Lkotlin/reflect/KType;[ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; public static final fun contentEquals (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Z public static final fun contentEquals (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Comparable;)Z public static final fun contentEquals (Lspace/kscience/kmath/nd/AlgebraND;Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Z @@ -1449,12 +1517,13 @@ public abstract interface class space/kscience/kmath/nd/WithShape { public abstract fun getShape-IIYLAfE ()[I } -public abstract interface class space/kscience/kmath/operations/Algebra { +public abstract interface class space/kscience/kmath/operations/Algebra : space/kscience/attributes/WithType { public fun binaryOperation (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object; public fun bindSymbolOrNull (Ljava/lang/String;)Ljava/lang/Object; - public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public abstract fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; public fun unaryOperation (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; } @@ -1520,6 +1589,7 @@ public final class space/kscience/kmath/operations/BigIntField : space/kscience/ public fun add (Lspace/kscience/kmath/operations/BigInt;Lspace/kscience/kmath/operations/BigInt;)Lspace/kscience/kmath/operations/BigInt; public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun divide (Lspace/kscience/kmath/operations/BigInt;Lspace/kscience/kmath/operations/BigInt;)Lspace/kscience/kmath/operations/BigInt; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Lspace/kscience/kmath/operations/BigInt; public synthetic fun getZero ()Ljava/lang/Object; @@ -1553,8 +1623,9 @@ public final class space/kscience/kmath/operations/BigIntKt { public abstract interface class space/kscience/kmath/operations/BufferAlgebra : space/kscience/kmath/operations/Algebra { public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public fun buffer (I[Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public abstract fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; - public fun getElementBufferFactory ()Lspace/kscience/kmath/structures/BufferFactory; + public fun getElementBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public fun map (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; public fun mapIndexed (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/structures/Buffer; public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; @@ -1568,15 +1639,15 @@ public final class space/kscience/kmath/operations/BufferAlgebraKt { public static final fun asinh (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static final fun atan (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static final fun atanh (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static final fun buffer (Lspace/kscience/kmath/operations/BufferAlgebra;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public static final fun buffer (Lspace/kscience/kmath/operations/BufferAlgebra;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; + public static final fun buffer (Lspace/kscience/kmath/operations/BufferAlgebra;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; + public static final fun buffer (Lspace/kscience/kmath/operations/BufferAlgebra;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; public static final fun buffer (Lspace/kscience/kmath/operations/BufferField;[Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; public static final fun cos (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static final fun cosh (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static final fun exp (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static final fun getBufferAlgebra (Lspace/kscience/kmath/operations/Field;)Lspace/kscience/kmath/operations/BufferFieldOps; - public static final fun getBufferAlgebra (Lspace/kscience/kmath/operations/IntRing;)Lspace/kscience/kmath/operations/BufferRingOps; - public static final fun getBufferAlgebra (Lspace/kscience/kmath/operations/ShortRing;)Lspace/kscience/kmath/operations/BufferRingOps; + public static final fun getBufferAlgebra (Lspace/kscience/kmath/operations/Int16Ring;)Lspace/kscience/kmath/operations/BufferRingOps; + public static final fun getBufferAlgebra (Lspace/kscience/kmath/operations/Int32Ring;)Lspace/kscience/kmath/operations/BufferRingOps; public static final fun ln (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static final fun pow (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; public static final fun sin (Lspace/kscience/kmath/operations/BufferAlgebra;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; @@ -1635,201 +1706,6 @@ public abstract interface class space/kscience/kmath/operations/BufferTransform public abstract fun transform (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; } -public final class space/kscience/kmath/operations/ByteRing : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { - public static final field INSTANCE Lspace/kscience/kmath/operations/ByteRing; - public fun add (BB)Ljava/lang/Byte; - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; - public fun getOne ()Ljava/lang/Byte; - public synthetic fun getOne ()Ljava/lang/Object; - public fun getZero ()Ljava/lang/Byte; - public synthetic fun getZero ()Ljava/lang/Object; - public fun minus (BB)Ljava/lang/Byte; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun multiply (BB)Ljava/lang/Byte; - public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun norm (B)Ljava/lang/Byte; - public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Ljava/lang/Byte; - public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun plus (BB)Ljava/lang/Byte; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (BB)Ljava/lang/Byte; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (B)Ljava/lang/Byte; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; -} - -public final class space/kscience/kmath/operations/DoubleBufferField : space/kscience/kmath/operations/DoubleBufferOps, space/kscience/kmath/operations/ExtendedField { - public fun (I)V - public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun acosh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun asinh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun atanh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun cosh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun getOne ()Ljava/lang/Object; - public fun getOne ()Lspace/kscience/kmath/structures/Buffer; - public final fun getSize ()I - public synthetic fun getZero ()Ljava/lang/Object; - public fun getZero ()Lspace/kscience/kmath/structures/Buffer; - public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public synthetic fun power (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; - public fun power-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)[D - public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun sinh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun tanh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; -} - -public abstract class space/kscience/kmath/operations/DoubleBufferOps : space/kscience/kmath/operations/BufferAlgebra, space/kscience/kmath/operations/ExtendedFieldOps, space/kscience/kmath/operations/Norm { - public static final field Companion Lspace/kscience/kmath/operations/DoubleBufferOps$Companion; - public fun ()V - public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; - public fun acos-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun acosh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun add-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; - public fun asin-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun asinh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; - public fun atan-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun atanh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; - public fun cos-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun cosh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun divide-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; - public fun exp-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; - public fun getElementAlgebra ()Lspace/kscience/kmath/operations/DoubleField; - public synthetic fun getElementBufferFactory ()Lspace/kscience/kmath/structures/BufferFactory; - public fun getElementBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; - public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; - public fun ln-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun map (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; - public final fun map-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function2;)[D - public synthetic fun mapIndexed (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/structures/Buffer; - public final fun mapIndexed-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)[D - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun minus-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; - public fun norm (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Double; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun plus-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun power (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; - public fun scale-CZ9oacQ (Lspace/kscience/kmath/structures/Buffer;D)[D - public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; - public fun sin-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun sinh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; - public fun tan-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun tanh-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D - public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public synthetic fun zip (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/structures/Buffer; - public final fun zip-XquIszc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)[D -} - -public final class space/kscience/kmath/operations/DoubleBufferOps$Companion : space/kscience/kmath/operations/DoubleBufferOps { -} - -public final class space/kscience/kmath/operations/DoubleBufferOpsKt { - public static final fun average (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;)D - public static final fun averageOf (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function1;)D - public static final fun covariance (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)D - public static final fun dispersion (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;)D - public static final fun std (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;)D - public static final fun sum (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;)D - public static final fun sumOf (Lspace/kscience/kmath/operations/DoubleBufferOps;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function1;)D -} - -public final class space/kscience/kmath/operations/DoubleField : space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/ScaleOperations { - public static final field INSTANCE Lspace/kscience/kmath/operations/DoubleField; - public fun acos (D)Ljava/lang/Double; - public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; - public fun acosh (D)Ljava/lang/Double; - public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun add (DD)Ljava/lang/Double; - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun asin (D)Ljava/lang/Double; - public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; - public fun asinh (D)Ljava/lang/Double; - public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun atan (D)Ljava/lang/Double; - public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; - public fun atanh (D)Ljava/lang/Double; - public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public fun cos (D)Ljava/lang/Double; - public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; - public fun cosh (D)Ljava/lang/Double; - public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun div (DD)Ljava/lang/Double; - public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun divide (DD)Ljava/lang/Double; - public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun exp (D)Ljava/lang/Double; - public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; - public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; - public fun getOne ()Ljava/lang/Double; - public synthetic fun getOne ()Ljava/lang/Object; - public fun getZero ()Ljava/lang/Double; - public synthetic fun getZero ()Ljava/lang/Object; - public fun ln (D)Ljava/lang/Double; - public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; - public fun minus (DD)Ljava/lang/Double; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun multiply (DD)Ljava/lang/Double; - public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun norm (D)Ljava/lang/Double; - public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Ljava/lang/Double; - public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun plus (DD)Ljava/lang/Double; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun power (DLjava/lang/Number;)Ljava/lang/Double; - public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun scale (DD)Ljava/lang/Double; - public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; - public fun sin (D)Ljava/lang/Double; - public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; - public fun sinh (D)Ljava/lang/Double; - public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun sqrt (D)Ljava/lang/Double; - public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; - public fun tan (D)Ljava/lang/Double; - public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; - public fun tanh (D)Ljava/lang/Double; - public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun times (DD)Ljava/lang/Double; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (D)Ljava/lang/Double; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; -} - -public final class space/kscience/kmath/operations/DoubleL2Norm : space/kscience/kmath/operations/Norm { - public static final field INSTANCE Lspace/kscience/kmath/operations/DoubleL2Norm; - public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; - public fun norm (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Double; -} - public abstract interface class space/kscience/kmath/operations/ExponentialOperations : space/kscience/kmath/operations/Algebra { public static final field ACOSH_OPERATION Ljava/lang/String; public static final field ASINH_OPERATION Ljava/lang/String; @@ -1899,8 +1775,8 @@ public final class space/kscience/kmath/operations/FieldOps$Companion { public static final field DIV_OPERATION Ljava/lang/String; } -public final class space/kscience/kmath/operations/FloatField : space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/Norm { - public static final field INSTANCE Lspace/kscience/kmath/operations/FloatField; +public final class space/kscience/kmath/operations/Float32Field : space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/Norm { + public static final field INSTANCE Lspace/kscience/kmath/operations/Float32Field; public fun acos (F)Ljava/lang/Float; public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; public fun acosh (F)Ljava/lang/Float; @@ -1963,6 +1839,175 @@ public final class space/kscience/kmath/operations/FloatField : space/kscience/k public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; } +public final class space/kscience/kmath/operations/Float64BufferField : space/kscience/kmath/operations/Float64BufferOps, space/kscience/kmath/operations/ExtendedField { + public fun (I)V + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun getOne ()Ljava/lang/Object; + public fun getOne ()Lspace/kscience/kmath/structures/Buffer; + public final fun getSize ()I + public synthetic fun getZero ()Ljava/lang/Object; + public fun getZero ()Lspace/kscience/kmath/structures/Buffer; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public synthetic fun power (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; + public fun power-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)[D + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; +} + +public abstract class space/kscience/kmath/operations/Float64BufferOps : space/kscience/kmath/operations/BufferAlgebra, space/kscience/kmath/operations/ExtendedFieldOps, space/kscience/kmath/operations/Norm { + public static final field Companion Lspace/kscience/kmath/operations/Float64BufferOps$Companion; + public fun ()V + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public fun acos-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun add-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public fun asin-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public fun atan-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public fun cos-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public fun exp-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float64Field; + public fun getElementBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public fun ln-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun map (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public final fun map-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function2;)[D + public synthetic fun mapIndexed (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/structures/Buffer; + public final fun mapIndexed-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)[D + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun minus-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Double; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun plus-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun power (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun scale-Vq5tpWc (Lspace/kscience/kmath/structures/Buffer;D)[D + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public fun sin-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; + public fun tan-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D + public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; + public synthetic fun zip (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/structures/Buffer; + public final fun zip-Qj2idzA (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function3;)[D +} + +public final class space/kscience/kmath/operations/Float64BufferOps$Companion : space/kscience/kmath/operations/Float64BufferOps { +} + +public final class space/kscience/kmath/operations/Float64BufferOpsKt { + public static final fun average (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;)D + public static final fun averageOf (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function1;)D + public static final fun covariance (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)D + public static final fun dispersion (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;)D + public static final fun std (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;)D + public static final fun sum (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;)D + public static final fun sumOf (Lspace/kscience/kmath/operations/Float64BufferOps;Lspace/kscience/kmath/structures/Buffer;Lkotlin/jvm/functions/Function1;)D +} + +public final class space/kscience/kmath/operations/Float64Field : space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/ScaleOperations { + public static final field INSTANCE Lspace/kscience/kmath/operations/Float64Field; + public fun acos (D)Ljava/lang/Double; + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh (D)Ljava/lang/Double; + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun add (DD)Ljava/lang/Double; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun asin (D)Ljava/lang/Double; + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh (D)Ljava/lang/Double; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atan (D)Ljava/lang/Double; + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh (D)Ljava/lang/Double; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; + public fun cos (D)Ljava/lang/Double; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh (D)Ljava/lang/Double; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun div (DD)Ljava/lang/Double; + public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (DD)Ljava/lang/Double; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun exp (D)Ljava/lang/Double; + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Double; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Double; + public synthetic fun getZero ()Ljava/lang/Object; + public fun ln (D)Ljava/lang/Double; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public fun minus (DD)Ljava/lang/Double; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (DD)Ljava/lang/Double; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (D)Ljava/lang/Double; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Double; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun plus (DD)Ljava/lang/Double; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun power (DLjava/lang/Number;)Ljava/lang/Double; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun scale (DD)Ljava/lang/Double; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun sin (D)Ljava/lang/Double; + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (D)Ljava/lang/Double; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sqrt (D)Ljava/lang/Double; + public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; + public fun tan (D)Ljava/lang/Double; + public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh (D)Ljava/lang/Double; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun times (DD)Ljava/lang/Double; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (D)Ljava/lang/Double; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class space/kscience/kmath/operations/Float64L2Norm : space/kscience/kmath/operations/Norm { + public static final field INSTANCE Lspace/kscience/kmath/operations/Float64L2Norm; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Double; +} + public abstract interface class space/kscience/kmath/operations/Group : space/kscience/kmath/operations/GroupOps { public abstract fun getZero ()Ljava/lang/Object; } @@ -1985,8 +2030,79 @@ public final class space/kscience/kmath/operations/GroupOps$Companion { public static final field PLUS_OPERATION Ljava/lang/String; } -public final class space/kscience/kmath/operations/IntRing : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { - public static final field INSTANCE Lspace/kscience/kmath/operations/IntRing; +public final class space/kscience/kmath/operations/Int16Field : space/kscience/kmath/operations/Field, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int16Field; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun add (SS)Ljava/lang/Short; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (SS)Ljava/lang/Short; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getOne ()Ljava/lang/Short; + public synthetic fun getZero ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Short; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (SS)Ljava/lang/Short; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (S)Ljava/lang/Short; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Short; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun scale (SD)Ljava/lang/Short; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (S)Ljava/lang/Short; +} + +public final class space/kscience/kmath/operations/Int16Ring : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int16Ring; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun add (SS)Ljava/lang/Short; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getOne ()Ljava/lang/Short; + public synthetic fun getZero ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Short; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun minus (SS)Ljava/lang/Short; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (SS)Ljava/lang/Short; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (S)Ljava/lang/Short; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Short; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun plus (SS)Ljava/lang/Short; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun times (SS)Ljava/lang/Short; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (S)Ljava/lang/Short; +} + +public final class space/kscience/kmath/operations/Int32Field : space/kscience/kmath/operations/Field, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int32Field; + public fun add (II)Ljava/lang/Integer; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (II)Ljava/lang/Integer; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Integer; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Integer; + public synthetic fun getZero ()Ljava/lang/Object; + public fun multiply (II)Ljava/lang/Integer; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (I)Ljava/lang/Integer; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Integer; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun scale (ID)Ljava/lang/Integer; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun unaryMinus (I)Ljava/lang/Integer; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class space/kscience/kmath/operations/Int32Ring : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int32Ring; public fun add (II)Ljava/lang/Integer; public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; @@ -2010,6 +2126,79 @@ public final class space/kscience/kmath/operations/IntRing : space/kscience/kmat public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; } +public final class space/kscience/kmath/operations/Int64Field : space/kscience/kmath/operations/Field, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int64Field; + public fun add (JJ)Ljava/lang/Long; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (JJ)Ljava/lang/Long; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Long; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Long; + public synthetic fun getZero ()Ljava/lang/Object; + public fun multiply (JJ)Ljava/lang/Long; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (J)Ljava/lang/Long; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Long; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun scale (JD)Ljava/lang/Long; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun unaryMinus (J)Ljava/lang/Long; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class space/kscience/kmath/operations/Int64Ring : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int64Ring; + public fun add (JJ)Ljava/lang/Long; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Long; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Long; + public synthetic fun getZero ()Ljava/lang/Object; + public fun minus (JJ)Ljava/lang/Long; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (JJ)Ljava/lang/Long; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (J)Ljava/lang/Long; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Long; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun plus (JJ)Ljava/lang/Long; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun times (JJ)Ljava/lang/Long; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (J)Ljava/lang/Long; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class space/kscience/kmath/operations/Int8Ring : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { + public static final field INSTANCE Lspace/kscience/kmath/operations/Int8Ring; + public fun add (BB)Ljava/lang/Byte; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Byte; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Byte; + public synthetic fun getZero ()Ljava/lang/Object; + public fun minus (BB)Ljava/lang/Byte; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (BB)Ljava/lang/Byte; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (B)Ljava/lang/Byte; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Byte; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun plus (BB)Ljava/lang/Byte; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun times (BB)Ljava/lang/Byte; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (B)Ljava/lang/Byte; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + public final class space/kscience/kmath/operations/IsIntegerKt { public static final fun isInteger (Ljava/lang/Number;)Z } @@ -2030,6 +2219,7 @@ public abstract class space/kscience/kmath/operations/JBigDecimalFieldBase : spa public fun add (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/math/BigDecimal; public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun divide (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/math/BigDecimal; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Ljava/math/BigDecimal; public synthetic fun getZero ()Ljava/lang/Object; @@ -2054,6 +2244,7 @@ public final class space/kscience/kmath/operations/JBigIntegerField : space/ksci public static final field INSTANCE Lspace/kscience/kmath/operations/JBigIntegerField; public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun add (Ljava/math/BigInteger;Ljava/math/BigInteger;)Ljava/math/BigInteger; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; public synthetic fun getOne ()Ljava/lang/Object; public fun getOne ()Ljava/math/BigInteger; public synthetic fun getZero ()Ljava/lang/Object; @@ -2073,42 +2264,17 @@ public final class space/kscience/kmath/operations/LogicAlgebra$Companion { public final fun getTRUE ()Lspace/kscience/kmath/expressions/Symbol; } -public final class space/kscience/kmath/operations/LongRing : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { - public static final field INSTANCE Lspace/kscience/kmath/operations/LongRing; - public fun add (JJ)Ljava/lang/Long; - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; - public fun getOne ()Ljava/lang/Long; - public synthetic fun getOne ()Ljava/lang/Object; - public fun getZero ()Ljava/lang/Long; - public synthetic fun getZero ()Ljava/lang/Object; - public fun minus (JJ)Ljava/lang/Long; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun multiply (JJ)Ljava/lang/Long; - public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun norm (J)Ljava/lang/Long; - public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Ljava/lang/Long; - public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun plus (JJ)Ljava/lang/Long; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (JJ)Ljava/lang/Long; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (J)Ljava/lang/Long; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; -} - public abstract interface class space/kscience/kmath/operations/Norm { public abstract fun norm (Ljava/lang/Object;)Ljava/lang/Object; } public final class space/kscience/kmath/operations/NumbersKt { - public static final fun getAlgebra (Lkotlin/jvm/internal/ByteCompanionObject;)Lspace/kscience/kmath/operations/ByteRing; - public static final fun getAlgebra (Lkotlin/jvm/internal/DoubleCompanionObject;)Lspace/kscience/kmath/operations/DoubleField; - public static final fun getAlgebra (Lkotlin/jvm/internal/FloatCompanionObject;)Lspace/kscience/kmath/operations/FloatField; - public static final fun getAlgebra (Lkotlin/jvm/internal/IntCompanionObject;)Lspace/kscience/kmath/operations/IntRing; - public static final fun getAlgebra (Lkotlin/jvm/internal/LongCompanionObject;)Lspace/kscience/kmath/operations/LongRing; - public static final fun getAlgebra (Lkotlin/jvm/internal/ShortCompanionObject;)Lspace/kscience/kmath/operations/ShortRing; + public static final fun getAlgebra (Lkotlin/jvm/internal/ByteCompanionObject;)Lspace/kscience/kmath/operations/Int8Ring; + public static final fun getAlgebra (Lkotlin/jvm/internal/DoubleCompanionObject;)Lspace/kscience/kmath/operations/Float64Field; + public static final fun getAlgebra (Lkotlin/jvm/internal/FloatCompanionObject;)Lspace/kscience/kmath/operations/Float32Field; + public static final fun getAlgebra (Lkotlin/jvm/internal/IntCompanionObject;)Lspace/kscience/kmath/operations/Int32Ring; + public static final fun getAlgebra (Lkotlin/jvm/internal/LongCompanionObject;)Lspace/kscience/kmath/operations/Int64Ring; + public static final fun getAlgebra (Lkotlin/jvm/internal/ShortCompanionObject;)Lspace/kscience/kmath/operations/Int16Ring; } public abstract interface class space/kscience/kmath/operations/NumericAlgebra : space/kscience/kmath/operations/Algebra { @@ -2125,7 +2291,7 @@ public final class space/kscience/kmath/operations/NumericAlgebraKt { public static final fun getPi (Lspace/kscience/kmath/operations/NumericAlgebra;)Ljava/lang/Object; } -public abstract interface class space/kscience/kmath/operations/PowerOperations : space/kscience/kmath/operations/FieldOps { +public abstract interface class space/kscience/kmath/operations/PowerOperations : space/kscience/kmath/operations/Algebra { public static final field Companion Lspace/kscience/kmath/operations/PowerOperations$Companion; public static final field POW_OPERATION Ljava/lang/String; public static final field SQRT_OPERATION Ljava/lang/String; @@ -2167,31 +2333,6 @@ public abstract interface class space/kscience/kmath/operations/ScaleOperations public fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; } -public final class space/kscience/kmath/operations/ShortRing : space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/NumericAlgebra, space/kscience/kmath/operations/Ring { - public static final field INSTANCE Lspace/kscience/kmath/operations/ShortRing; - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun add (SS)Ljava/lang/Short; - public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; - public synthetic fun getOne ()Ljava/lang/Object; - public fun getOne ()Ljava/lang/Short; - public synthetic fun getZero ()Ljava/lang/Object; - public fun getZero ()Ljava/lang/Short; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun minus (SS)Ljava/lang/Short; - public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun multiply (SS)Ljava/lang/Short; - public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; - public fun norm (S)Ljava/lang/Short; - public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Ljava/lang/Short; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun plus (SS)Ljava/lang/Short; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (SS)Ljava/lang/Short; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (S)Ljava/lang/Short; -} - public abstract interface class space/kscience/kmath/operations/TrigonometricOperations : space/kscience/kmath/operations/Algebra { public static final field ACOS_OPERATION Ljava/lang/String; public static final field ASIN_OPERATION Ljava/lang/String; @@ -2222,17 +2363,28 @@ public abstract interface class space/kscience/kmath/operations/WithSize { } public final class space/kscience/kmath/structures/ArrayBuffer : space/kscience/kmath/structures/MutableBuffer { - public fun ([Ljava/lang/Object;)V - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; + public static final synthetic fun box-impl ([Ljava/lang/Object;)Lspace/kscience/kmath/structures/ArrayBuffer; + public static fun constructor-impl ([Ljava/lang/Object;)[Ljava/lang/Object; + public fun equals (Ljava/lang/Object;)Z + public static fun equals-impl ([Ljava/lang/Object;Ljava/lang/Object;)Z + public static final fun equals-impl0 ([Ljava/lang/Object;[Ljava/lang/Object;)Z public fun get (I)Ljava/lang/Object; + public static fun get-impl ([Ljava/lang/Object;I)Ljava/lang/Object; public fun getSize ()I + public static fun getSize-impl ([Ljava/lang/Object;)I + public fun hashCode ()I + public static fun hashCode-impl ([Ljava/lang/Object;)I public fun iterator ()Ljava/util/Iterator; + public static fun iterator-impl ([Ljava/lang/Object;)Ljava/util/Iterator; public fun set (ILjava/lang/Object;)V + public static fun set-impl ([Ljava/lang/Object;ILjava/lang/Object;)V public fun toString ()Ljava/lang/String; + public static fun toString-impl ([Ljava/lang/Object;)Ljava/lang/String; + public final synthetic fun unbox-impl ()[Ljava/lang/Object; } public final class space/kscience/kmath/structures/ArrayBufferKt { - public static final fun asBuffer ([Ljava/lang/Object;)Lspace/kscience/kmath/structures/ArrayBuffer; + public static final fun asBuffer ([Ljava/lang/Object;)[Ljava/lang/Object; } public abstract interface class space/kscience/kmath/structures/Buffer : space/kscience/kmath/operations/WithSize { @@ -2244,8 +2396,6 @@ public abstract interface class space/kscience/kmath/structures/Buffer : space/k } public final class space/kscience/kmath/structures/Buffer$Companion { - public final fun auto (Lkotlin/reflect/KClass;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public final fun boxing (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; public final fun contentEquals (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Z public final fun toString (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/String; } @@ -2260,17 +2410,14 @@ public final class space/kscience/kmath/structures/BufferExpanded : space/kscien public fun toString ()Ljava/lang/String; } -public abstract interface class space/kscience/kmath/structures/BufferFactory { - public static final field Companion Lspace/kscience/kmath/structures/BufferFactory$Companion; +public abstract interface class space/kscience/kmath/structures/BufferFactory : space/kscience/attributes/WithType { public abstract fun invoke (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; } -public final class space/kscience/kmath/structures/BufferFactory$Companion { - public final fun boxing ()Lspace/kscience/kmath/structures/BufferFactory; -} - public final class space/kscience/kmath/structures/BufferKt { - public static final fun asReadOnly (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public static final fun Buffer--rwW0uw (Lkotlin/reflect/KType;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; + public static final fun BufferFactory-X0YbwmU (Lkotlin/reflect/KType;)Lspace/kscience/kmath/structures/BufferFactory; + public static final fun MutableBufferFactory-X0YbwmU (Lkotlin/reflect/KType;)Lspace/kscience/kmath/structures/MutableBufferFactory; public static final fun first (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Object; public static final fun get-Qn1smSk (Lspace/kscience/kmath/structures/Buffer;I)Ljava/lang/Object; public static final fun getIndices (Lspace/kscience/kmath/structures/Buffer;)Lkotlin/ranges/IntRange; @@ -2278,6 +2425,10 @@ public final class space/kscience/kmath/structures/BufferKt { public static final fun last (Lspace/kscience/kmath/structures/Buffer;)Ljava/lang/Object; } +public final class space/kscience/kmath/structures/BufferListKt { + public static final fun asList (Lspace/kscience/kmath/structures/Buffer;)Ljava/util/List; +} + public final class space/kscience/kmath/structures/BufferPrimitiveAccessKt { } @@ -2304,87 +2455,6 @@ public final class space/kscience/kmath/structures/BufferViewKt { public static final fun slice (Lspace/kscience/kmath/structures/Buffer;Lkotlin/ranges/IntRange;)Lspace/kscience/kmath/structures/BufferView; } -public final class space/kscience/kmath/structures/ByteBuffer : space/kscience/kmath/structures/MutableBuffer { - public static final synthetic fun box-impl ([B)Lspace/kscience/kmath/structures/ByteBuffer; - public static fun constructor-impl ([B)[B - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public static fun copy-impl ([B)Lspace/kscience/kmath/structures/MutableBuffer; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl ([BLjava/lang/Object;)Z - public static final fun equals-impl0 ([B[B)Z - public fun get (I)Ljava/lang/Byte; - public synthetic fun get (I)Ljava/lang/Object; - public static fun get-impl ([BI)Ljava/lang/Byte; - public final fun getArray ()[B - public fun getSize ()I - public static fun getSize-impl ([B)I - public fun hashCode ()I - public static fun hashCode-impl ([B)I - public synthetic fun iterator ()Ljava/util/Iterator; - public fun iterator ()Lkotlin/collections/ByteIterator; - public static fun iterator-impl ([B)Lkotlin/collections/ByteIterator; - public fun set (IB)V - public synthetic fun set (ILjava/lang/Object;)V - public static fun set-impl ([BIB)V - public fun toString ()Ljava/lang/String; - public static fun toString-impl ([B)Ljava/lang/String; - public final synthetic fun unbox-impl ()[B -} - -public final class space/kscience/kmath/structures/ByteBufferKt { - public static final fun ByteBuffer (ILkotlin/jvm/functions/Function1;)[B - public static final fun ByteBuffer ([B)[B - public static final fun asBuffer ([B)[B - public static final fun toByteArray (Lspace/kscience/kmath/structures/Buffer;)[B -} - -public final class space/kscience/kmath/structures/DoubleBuffer : space/kscience/kmath/structures/PrimitiveBuffer { - public static final field Companion Lspace/kscience/kmath/structures/DoubleBuffer$Companion; - public static final synthetic fun box-impl ([D)Lspace/kscience/kmath/structures/DoubleBuffer; - public static fun constructor-impl ([D)[D - public synthetic fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun copy-Dv3HvWU ()[D - public static fun copy-Dv3HvWU ([D)[D - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl ([DLjava/lang/Object;)Z - public static final fun equals-impl0 ([D[D)Z - public fun get (I)Ljava/lang/Double; - public synthetic fun get (I)Ljava/lang/Object; - public static fun get-impl ([DI)Ljava/lang/Double; - public final fun getArray ()[D - public fun getSize ()I - public static fun getSize-impl ([D)I - public fun hashCode ()I - public static fun hashCode-impl ([D)I - public synthetic fun iterator ()Ljava/util/Iterator; - public fun iterator ()Lkotlin/collections/DoubleIterator; - public static fun iterator-impl ([D)Lkotlin/collections/DoubleIterator; - public fun set (ID)V - public synthetic fun set (ILjava/lang/Object;)V - public static fun set-impl ([DID)V - public fun toString ()Ljava/lang/String; - public static fun toString-impl ([D)Ljava/lang/String; - public final synthetic fun unbox-impl ()[D -} - -public final class space/kscience/kmath/structures/DoubleBuffer$Companion { - public final fun zero-Udx-57Q (I)[D -} - -public final class space/kscience/kmath/structures/DoubleBufferKt { - public static final fun DoubleBuffer (ILkotlin/jvm/functions/Function1;)[D - public static final fun DoubleBuffer ([D)[D - public static final fun asBuffer ([D)[D - public static final fun toDoubleArray (Lspace/kscience/kmath/structures/Buffer;)[D - public static final fun toDoubleBuffer (Lspace/kscience/kmath/structures/Buffer;)[D -} - -public abstract interface class space/kscience/kmath/structures/DoubleBufferTransform : space/kscience/kmath/operations/BufferTransform { - public synthetic fun transform (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public abstract fun transform-7Zdoou4 ([D)[D - public fun transform-Udx-57Q (Lspace/kscience/kmath/structures/Buffer;)[D -} - public abstract interface class space/kscience/kmath/structures/FlaggedBuffer : space/kscience/kmath/structures/Buffer { public abstract fun getFlag (I)B } @@ -2408,11 +2478,9 @@ public final class space/kscience/kmath/structures/FlaggedDoubleBuffer : space/k public fun toString ()Ljava/lang/String; } -public final class space/kscience/kmath/structures/FloatBuffer : space/kscience/kmath/structures/PrimitiveBuffer { - public static final synthetic fun box-impl ([F)Lspace/kscience/kmath/structures/FloatBuffer; +public final class space/kscience/kmath/structures/Float32Buffer : space/kscience/kmath/structures/PrimitiveBuffer { + public static final synthetic fun box-impl ([F)Lspace/kscience/kmath/structures/Float32Buffer; public static fun constructor-impl ([F)[F - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public static fun copy-impl ([F)Lspace/kscience/kmath/structures/MutableBuffer; public fun equals (Ljava/lang/Object;)Z public static fun equals-impl ([FLjava/lang/Object;)Z public static final fun equals-impl0 ([F[F)Z @@ -2435,224 +2503,60 @@ public final class space/kscience/kmath/structures/FloatBuffer : space/kscience/ public final synthetic fun unbox-impl ()[F } -public final class space/kscience/kmath/structures/FloatBufferKt { - public static final fun FloatBuffer (ILkotlin/jvm/functions/Function1;)[F - public static final fun FloatBuffer ([F)[F +public final class space/kscience/kmath/structures/Float32BufferKt { + public static final fun Float32Buffer (ILkotlin/jvm/functions/Function1;)[F + public static final fun Float32Buffer ([F)[F public static final fun asBuffer ([F)[F public static final fun toFloatArray (Lspace/kscience/kmath/structures/Buffer;)[F } -public final class space/kscience/kmath/structures/IntBuffer : space/kscience/kmath/structures/PrimitiveBuffer { - public static final synthetic fun box-impl ([I)Lspace/kscience/kmath/structures/IntBuffer; - public static fun constructor-impl ([I)[I - public synthetic fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun copy-ir4F4A8 ()[I - public static fun copy-ir4F4A8 ([I)[I +public final class space/kscience/kmath/structures/Float64Buffer : space/kscience/kmath/structures/PrimitiveBuffer { + public static final field Companion Lspace/kscience/kmath/structures/Float64Buffer$Companion; + public static final synthetic fun box-impl ([D)Lspace/kscience/kmath/structures/Float64Buffer; + public static fun constructor-impl ([D)[D public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl ([ILjava/lang/Object;)Z - public static final fun equals-impl0 ([I[I)Z - public fun get (I)Ljava/lang/Integer; + public static fun equals-impl ([DLjava/lang/Object;)Z + public static final fun equals-impl0 ([D[D)Z + public fun get (I)Ljava/lang/Double; public synthetic fun get (I)Ljava/lang/Object; - public static fun get-impl ([II)Ljava/lang/Integer; - public final fun getArray ()[I + public static fun get-impl ([DI)Ljava/lang/Double; + public final fun getArray ()[D public fun getSize ()I - public static fun getSize-impl ([I)I + public static fun getSize-impl ([D)I public fun hashCode ()I - public static fun hashCode-impl ([I)I + public static fun hashCode-impl ([D)I public synthetic fun iterator ()Ljava/util/Iterator; - public fun iterator ()Lkotlin/collections/IntIterator; - public static fun iterator-impl ([I)Lkotlin/collections/IntIterator; - public fun set (II)V + public fun iterator ()Lkotlin/collections/DoubleIterator; + public static fun iterator-impl ([D)Lkotlin/collections/DoubleIterator; + public fun set (ID)V public synthetic fun set (ILjava/lang/Object;)V - public static fun set-impl ([III)V + public static fun set-impl ([DID)V public fun toString ()Ljava/lang/String; - public static fun toString-impl ([I)Ljava/lang/String; - public final synthetic fun unbox-impl ()[I + public static fun toString-impl ([D)Ljava/lang/String; + public final synthetic fun unbox-impl ()[D } -public final class space/kscience/kmath/structures/IntBufferKt { - public static final fun IntBuffer (ILkotlin/jvm/functions/Function1;)[I - public static final fun IntBuffer ([I)[I - public static final fun asBuffer ([I)[I - public static final fun toIntArray (Lspace/kscience/kmath/structures/Buffer;)[I +public final class space/kscience/kmath/structures/Float64Buffer$Companion { + public final fun zero-qFCK38E (I)[D } -public final class space/kscience/kmath/structures/ListBuffer : space/kscience/kmath/structures/Buffer { - public fun (ILkotlin/jvm/functions/Function1;)V - public fun (Ljava/util/List;)V - public fun get (I)Ljava/lang/Object; - public final fun getList ()Ljava/util/List; - public fun getSize ()I - public fun iterator ()Ljava/util/Iterator; - public fun toString ()Ljava/lang/String; +public final class space/kscience/kmath/structures/Float64BufferKt { + public static final fun Float64Buffer (ILkotlin/jvm/functions/Function1;)[D + public static final fun Float64Buffer ([D)[D + public static final fun asBuffer ([D)[D + public static final fun toDoubleArray (Lspace/kscience/kmath/structures/Buffer;)[D + public static final fun toFloat64Buffer (Lspace/kscience/kmath/structures/Buffer;)[D } -public final class space/kscience/kmath/structures/ListBufferKt { - public static final fun asBuffer (Ljava/util/List;)Lspace/kscience/kmath/structures/ListBuffer; - public static final fun asMutableBuffer (Ljava/util/List;)Ljava/util/List; +public abstract interface class space/kscience/kmath/structures/Float64BufferTransform : space/kscience/kmath/operations/BufferTransform { + public synthetic fun transform (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun transform-kPrB3XE ([D)[D + public fun transform-qFCK38E (Lspace/kscience/kmath/structures/Buffer;)[D } -public final class space/kscience/kmath/structures/LongBuffer : space/kscience/kmath/structures/PrimitiveBuffer { - public static final synthetic fun box-impl ([J)Lspace/kscience/kmath/structures/LongBuffer; - public static fun constructor-impl ([J)[J - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public static fun copy-impl ([J)Lspace/kscience/kmath/structures/MutableBuffer; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl ([JLjava/lang/Object;)Z - public static final fun equals-impl0 ([J[J)Z - public fun get (I)Ljava/lang/Long; - public synthetic fun get (I)Ljava/lang/Object; - public static fun get-impl ([JI)Ljava/lang/Long; - public final fun getArray ()[J - public fun getSize ()I - public static fun getSize-impl ([J)I - public fun hashCode ()I - public static fun hashCode-impl ([J)I - public synthetic fun iterator ()Ljava/util/Iterator; - public fun iterator ()Lkotlin/collections/LongIterator; - public static fun iterator-impl ([J)Lkotlin/collections/LongIterator; - public fun set (IJ)V - public synthetic fun set (ILjava/lang/Object;)V - public static fun set-impl ([JIJ)V - public fun toString ()Ljava/lang/String; - public static fun toString-impl ([J)Ljava/lang/String; - public final synthetic fun unbox-impl ()[J -} - -public final class space/kscience/kmath/structures/LongBufferKt { - public static final fun LongBuffer (ILkotlin/jvm/functions/Function1;)[J - public static final fun LongBuffer ([J)[J - public static final fun asBuffer ([J)[J - public static final fun toLongArray (Lspace/kscience/kmath/structures/Buffer;)[J -} - -public class space/kscience/kmath/structures/MemoryBuffer : space/kscience/kmath/structures/Buffer { - public static final field Companion Lspace/kscience/kmath/structures/MemoryBuffer$Companion; - public fun (Lspace/kscience/kmath/memory/Memory;Lspace/kscience/kmath/memory/MemorySpec;)V - public fun get (I)Ljava/lang/Object; - protected final fun getMemory ()Lspace/kscience/kmath/memory/Memory; - public fun getSize ()I - protected final fun getSpec ()Lspace/kscience/kmath/memory/MemorySpec; - public fun iterator ()Ljava/util/Iterator; - public fun toString ()Ljava/lang/String; -} - -public final class space/kscience/kmath/structures/MemoryBuffer$Companion { - public final fun create (Lspace/kscience/kmath/memory/MemorySpec;I)Lspace/kscience/kmath/structures/MemoryBuffer; - public final fun create (Lspace/kscience/kmath/memory/MemorySpec;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MemoryBuffer; -} - -public abstract interface class space/kscience/kmath/structures/MutableBuffer : space/kscience/kmath/structures/Buffer { - public static final field Companion Lspace/kscience/kmath/structures/MutableBuffer$Companion; - public abstract fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public abstract fun set (ILjava/lang/Object;)V -} - -public final class space/kscience/kmath/structures/MutableBuffer$Companion { - public final fun auto (Lkotlin/reflect/KClass;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; - public final fun boxing (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; - public final fun double-CZ9oacQ (ILkotlin/jvm/functions/Function1;)[D - public final fun float-YxruXGw (ILkotlin/jvm/functions/Function1;)[F - public final fun int-Ye6GY2U (ILkotlin/jvm/functions/Function1;)[I - public final fun long-BuQOeTY (ILkotlin/jvm/functions/Function1;)[J - public final fun short-1yRgbGw (ILkotlin/jvm/functions/Function1;)[S -} - -public abstract interface class space/kscience/kmath/structures/MutableBufferFactory : space/kscience/kmath/structures/BufferFactory { - public static final field Companion Lspace/kscience/kmath/structures/MutableBufferFactory$Companion; - public abstract fun invoke (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; -} - -public final class space/kscience/kmath/structures/MutableBufferFactory$Companion { - public final fun boxing ()Lspace/kscience/kmath/structures/MutableBufferFactory; -} - -public final class space/kscience/kmath/structures/MutableListBuffer : space/kscience/kmath/structures/MutableBuffer { - public static final synthetic fun box-impl (Ljava/util/List;)Lspace/kscience/kmath/structures/MutableListBuffer; - public static fun constructor-impl (ILkotlin/jvm/functions/Function1;)Ljava/util/List; - public static fun constructor-impl (Ljava/util/List;)Ljava/util/List; - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public static fun copy-impl (Ljava/util/List;)Lspace/kscience/kmath/structures/MutableBuffer; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Ljava/util/List;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Ljava/util/List;Ljava/util/List;)Z - public fun get (I)Ljava/lang/Object; - public static fun get-impl (Ljava/util/List;I)Ljava/lang/Object; - public final fun getList ()Ljava/util/List; - public fun getSize ()I - public static fun getSize-impl (Ljava/util/List;)I - public fun hashCode ()I - public static fun hashCode-impl (Ljava/util/List;)I - public fun iterator ()Ljava/util/Iterator; - public static fun iterator-impl (Ljava/util/List;)Ljava/util/Iterator; - public fun set (ILjava/lang/Object;)V - public static fun set-impl (Ljava/util/List;ILjava/lang/Object;)V - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Ljava/util/List;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Ljava/util/List; -} - -public final class space/kscience/kmath/structures/MutableMemoryBuffer : space/kscience/kmath/structures/MemoryBuffer, space/kscience/kmath/structures/MutableBuffer { - public static final field Companion Lspace/kscience/kmath/structures/MutableMemoryBuffer$Companion; - public fun (Lspace/kscience/kmath/memory/Memory;Lspace/kscience/kmath/memory/MemorySpec;)V - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun set (ILjava/lang/Object;)V -} - -public final class space/kscience/kmath/structures/MutableMemoryBuffer$Companion { - public final fun create (Lspace/kscience/kmath/memory/MemorySpec;I)Lspace/kscience/kmath/structures/MutableMemoryBuffer; - public final fun create (Lspace/kscience/kmath/memory/MemorySpec;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableMemoryBuffer; -} - -public final class space/kscience/kmath/structures/PermutedBuffer : space/kscience/kmath/structures/BufferView { - public fun (Lspace/kscience/kmath/structures/Buffer;[I)V - public fun get (I)Ljava/lang/Object; - public fun getOrigin ()Lspace/kscience/kmath/structures/Buffer; - public fun getSize ()I - public fun iterator ()Ljava/util/Iterator; - public fun toString ()Ljava/lang/String; -} - -public final class space/kscience/kmath/structures/PermutedMutableBuffer : space/kscience/kmath/structures/BufferView, space/kscience/kmath/structures/MutableBuffer { - public fun (Lspace/kscience/kmath/structures/MutableBuffer;[I)V - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun get (I)Ljava/lang/Object; - public synthetic fun getOrigin ()Lspace/kscience/kmath/structures/Buffer; - public fun getOrigin ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun getSize ()I - public fun iterator ()Ljava/util/Iterator; - public fun set (ILjava/lang/Object;)V - public fun toString ()Ljava/lang/String; -} - -public abstract interface class space/kscience/kmath/structures/PrimitiveBuffer : space/kscience/kmath/structures/MutableBuffer { -} - -public final class space/kscience/kmath/structures/ReadOnlyBuffer : space/kscience/kmath/structures/Buffer { - public static final synthetic fun box-impl (Lspace/kscience/kmath/structures/MutableBuffer;)Lspace/kscience/kmath/structures/ReadOnlyBuffer; - public static fun constructor-impl (Lspace/kscience/kmath/structures/MutableBuffer;)Lspace/kscience/kmath/structures/MutableBuffer; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Lspace/kscience/kmath/structures/MutableBuffer;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Lspace/kscience/kmath/structures/MutableBuffer;Lspace/kscience/kmath/structures/MutableBuffer;)Z - public fun get (I)Ljava/lang/Object; - public static fun get-impl (Lspace/kscience/kmath/structures/MutableBuffer;I)Ljava/lang/Object; - public final fun getBuffer ()Lspace/kscience/kmath/structures/MutableBuffer; - public fun getSize ()I - public static fun getSize-impl (Lspace/kscience/kmath/structures/MutableBuffer;)I - public fun hashCode ()I - public static fun hashCode-impl (Lspace/kscience/kmath/structures/MutableBuffer;)I - public fun iterator ()Ljava/util/Iterator; - public static fun iterator-impl (Lspace/kscience/kmath/structures/MutableBuffer;)Ljava/util/Iterator; - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Lspace/kscience/kmath/structures/MutableBuffer;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Lspace/kscience/kmath/structures/MutableBuffer; -} - -public final class space/kscience/kmath/structures/ShortBuffer : space/kscience/kmath/structures/MutableBuffer { - public static final synthetic fun box-impl ([S)Lspace/kscience/kmath/structures/ShortBuffer; +public final class space/kscience/kmath/structures/Int16Buffer : space/kscience/kmath/structures/MutableBuffer { + public static final synthetic fun box-impl ([S)Lspace/kscience/kmath/structures/Int16Buffer; public static fun constructor-impl ([S)[S - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public static fun copy-impl ([S)Lspace/kscience/kmath/structures/MutableBuffer; public fun equals (Ljava/lang/Object;)Z public static fun equals-impl ([SLjava/lang/Object;)Z public static final fun equals-impl0 ([S[S)Z @@ -2675,18 +2579,201 @@ public final class space/kscience/kmath/structures/ShortBuffer : space/kscience/ public final synthetic fun unbox-impl ()[S } -public final class space/kscience/kmath/structures/ShortBufferKt { - public static final fun ShortBuffer (ILkotlin/jvm/functions/Function1;)[S - public static final fun ShortBuffer ([S)[S +public final class space/kscience/kmath/structures/Int16BufferKt { + public static final fun Int16Buffer (ILkotlin/jvm/functions/Function1;)[S + public static final fun Int16Buffer ([S)[S public static final fun asBuffer ([S)[S public static final fun toShortArray (Lspace/kscience/kmath/structures/Buffer;)[S } +public final class space/kscience/kmath/structures/Int32Buffer : space/kscience/kmath/structures/PrimitiveBuffer { + public static final synthetic fun box-impl ([I)Lspace/kscience/kmath/structures/Int32Buffer; + public static fun constructor-impl ([I)[I + public fun equals (Ljava/lang/Object;)Z + public static fun equals-impl ([ILjava/lang/Object;)Z + public static final fun equals-impl0 ([I[I)Z + public fun get (I)Ljava/lang/Integer; + public synthetic fun get (I)Ljava/lang/Object; + public static fun get-impl ([II)Ljava/lang/Integer; + public final fun getArray ()[I + public fun getSize ()I + public static fun getSize-impl ([I)I + public fun hashCode ()I + public static fun hashCode-impl ([I)I + public synthetic fun iterator ()Ljava/util/Iterator; + public fun iterator ()Lkotlin/collections/IntIterator; + public static fun iterator-impl ([I)Lkotlin/collections/IntIterator; + public fun set (II)V + public synthetic fun set (ILjava/lang/Object;)V + public static fun set-impl ([III)V + public fun toString ()Ljava/lang/String; + public static fun toString-impl ([I)Ljava/lang/String; + public final synthetic fun unbox-impl ()[I +} + +public final class space/kscience/kmath/structures/Int32BufferKt { + public static final fun Int32Buffer (ILkotlin/jvm/functions/Function1;)[I + public static final fun Int32Buffer ([I)[I + public static final fun asBuffer ([I)[I + public static final fun toIntArray (Lspace/kscience/kmath/structures/Buffer;)[I +} + +public final class space/kscience/kmath/structures/Int64Buffer : space/kscience/kmath/structures/PrimitiveBuffer { + public static final synthetic fun box-impl ([J)Lspace/kscience/kmath/structures/Int64Buffer; + public static fun constructor-impl ([J)[J + public fun equals (Ljava/lang/Object;)Z + public static fun equals-impl ([JLjava/lang/Object;)Z + public static final fun equals-impl0 ([J[J)Z + public fun get (I)Ljava/lang/Long; + public synthetic fun get (I)Ljava/lang/Object; + public static fun get-impl ([JI)Ljava/lang/Long; + public final fun getArray ()[J + public fun getSize ()I + public static fun getSize-impl ([J)I + public fun hashCode ()I + public static fun hashCode-impl ([J)I + public synthetic fun iterator ()Ljava/util/Iterator; + public fun iterator ()Lkotlin/collections/LongIterator; + public static fun iterator-impl ([J)Lkotlin/collections/LongIterator; + public fun set (IJ)V + public synthetic fun set (ILjava/lang/Object;)V + public static fun set-impl ([JIJ)V + public fun toString ()Ljava/lang/String; + public static fun toString-impl ([J)Ljava/lang/String; + public final synthetic fun unbox-impl ()[J +} + +public final class space/kscience/kmath/structures/Int64BufferKt { + public static final fun Int64Buffer (ILkotlin/jvm/functions/Function1;)[J + public static final fun Int64Buffer ([J)[J + public static final fun asBuffer ([J)[J + public static final fun toLongArray (Lspace/kscience/kmath/structures/Buffer;)[J +} + +public final class space/kscience/kmath/structures/Int8Buffer : space/kscience/kmath/structures/MutableBuffer { + public static final synthetic fun box-impl ([B)Lspace/kscience/kmath/structures/Int8Buffer; + public static fun constructor-impl ([B)[B + public fun equals (Ljava/lang/Object;)Z + public static fun equals-impl ([BLjava/lang/Object;)Z + public static final fun equals-impl0 ([B[B)Z + public fun get (I)Ljava/lang/Byte; + public synthetic fun get (I)Ljava/lang/Object; + public static fun get-impl ([BI)Ljava/lang/Byte; + public final fun getArray ()[B + public fun getSize ()I + public static fun getSize-impl ([B)I + public fun hashCode ()I + public static fun hashCode-impl ([B)I + public synthetic fun iterator ()Ljava/util/Iterator; + public fun iterator ()Lkotlin/collections/ByteIterator; + public static fun iterator-impl ([B)Lkotlin/collections/ByteIterator; + public fun set (IB)V + public synthetic fun set (ILjava/lang/Object;)V + public static fun set-impl ([BIB)V + public fun toString ()Ljava/lang/String; + public static fun toString-impl ([B)Ljava/lang/String; + public final synthetic fun unbox-impl ()[B +} + +public final class space/kscience/kmath/structures/Int8BufferKt { + public static final fun Int8Buffer (ILkotlin/jvm/functions/Function1;)[B + public static final fun Int8Buffer ([B)[B + public static final fun asBuffer ([B)[B + public static final fun toByteArray (Lspace/kscience/kmath/structures/Buffer;)[B +} + +public final class space/kscience/kmath/structures/ListBuffer : space/kscience/kmath/structures/Buffer { + public fun (Ljava/util/List;)V + public fun get (I)Ljava/lang/Object; + public final fun getList ()Ljava/util/List; + public fun getSize ()I + public fun iterator ()Ljava/util/Iterator; + public fun toString ()Ljava/lang/String; +} + +public final class space/kscience/kmath/structures/ListBufferKt { + public static final fun asBuffer (Ljava/util/List;)Lspace/kscience/kmath/structures/ListBuffer; + public static final fun asMutableBuffer (Ljava/util/List;)Lspace/kscience/kmath/structures/MutableListBuffer; +} + +public abstract interface class space/kscience/kmath/structures/MutableBuffer : space/kscience/kmath/structures/Buffer { + public static final field Companion Lspace/kscience/kmath/structures/MutableBuffer$Companion; + public abstract fun set (ILjava/lang/Object;)V +} + +public final class space/kscience/kmath/structures/MutableBuffer$Companion { + public final fun double-Vq5tpWc (ILkotlin/jvm/functions/Function1;)[D + public final fun float-pmys_WE (ILkotlin/jvm/functions/Function1;)[F + public final fun int-Tednjrs (ILkotlin/jvm/functions/Function1;)[I + public final fun long-1n4IcUs (ILkotlin/jvm/functions/Function1;)[J + public final fun short-CjmdtSE (ILkotlin/jvm/functions/Function1;)[S +} + +public abstract interface class space/kscience/kmath/structures/MutableBufferFactory : space/kscience/kmath/structures/BufferFactory { + public static final field Companion Lspace/kscience/kmath/structures/MutableBufferFactory$Companion; + public abstract fun invoke (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; +} + +public final class space/kscience/kmath/structures/MutableBufferFactory$Companion { +} + +public final class space/kscience/kmath/structures/MutableBufferKt { + public static final fun MutableBuffer--rwW0uw (Lkotlin/reflect/KType;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; + public static final fun copy (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/BufferFactory;)Lspace/kscience/kmath/structures/Buffer; + public static final fun mutableCopy (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/MutableBufferFactory;)Lspace/kscience/kmath/structures/MutableBuffer; +} + +public final class space/kscience/kmath/structures/MutableListBuffer : space/kscience/kmath/structures/MutableBuffer { + public fun (Ljava/util/List;)V + public fun get (I)Ljava/lang/Object; + public final fun getList ()Ljava/util/List; + public fun getSize ()I + public fun iterator ()Ljava/util/Iterator; + public fun set (ILjava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + +public final class space/kscience/kmath/structures/ParallelBufferFactory : space/kscience/kmath/structures/MutableBufferFactory { + public synthetic fun (Lkotlin/reflect/KType;Lkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun getType-V0oMfBY ()Lkotlin/reflect/KType; + public synthetic fun invoke (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; + public fun invoke (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; +} + +public final class space/kscience/kmath/structures/ParallelMutableBufferKt { + public static final fun parallel-Fnn_obI (Lspace/kscience/kmath/structures/MutableBufferFactory$Companion;Lkotlin/reflect/KType;)Lspace/kscience/kmath/structures/MutableBufferFactory; + public static final fun parallel-wcOc40k (Lspace/kscience/kmath/structures/MutableBuffer$Companion;Lkotlin/reflect/KType;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/MutableBuffer; +} + +public final class space/kscience/kmath/structures/PermutedBuffer : space/kscience/kmath/structures/BufferView { + public fun (Lspace/kscience/kmath/structures/Buffer;[I)V + public fun get (I)Ljava/lang/Object; + public fun getOrigin ()Lspace/kscience/kmath/structures/Buffer; + public fun getSize ()I + public fun iterator ()Ljava/util/Iterator; + public fun toString ()Ljava/lang/String; +} + +public final class space/kscience/kmath/structures/PermutedMutableBuffer : space/kscience/kmath/structures/BufferView, space/kscience/kmath/structures/MutableBuffer { + public fun (Lspace/kscience/kmath/structures/MutableBuffer;[I)V + public fun get (I)Ljava/lang/Object; + public synthetic fun getOrigin ()Lspace/kscience/kmath/structures/Buffer; + public fun getOrigin ()Lspace/kscience/kmath/structures/MutableBuffer; + public fun getSize ()I + public fun iterator ()Ljava/util/Iterator; + public fun set (ILjava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + +public abstract interface class space/kscience/kmath/structures/PrimitiveBuffer : space/kscience/kmath/structures/MutableBuffer { +} + public final class space/kscience/kmath/structures/ValueFlag : java/lang/Enum { public static final field MISSING Lspace/kscience/kmath/structures/ValueFlag; public static final field NAN Lspace/kscience/kmath/structures/ValueFlag; public static final field NEGATIVE_INFINITY Lspace/kscience/kmath/structures/ValueFlag; public static final field POSITIVE_INFINITY Lspace/kscience/kmath/structures/ValueFlag; + public static fun getEntries ()Lkotlin/enums/EnumEntries; public final fun getMask ()B public static fun valueOf (Ljava/lang/String;)Lspace/kscience/kmath/structures/ValueFlag; public static fun values ()[Lspace/kscience/kmath/structures/ValueFlag; diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index 2088e0159..0e061c6dd 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -9,7 +9,7 @@ kscience{ wasm() dependencies { - api(projects.kmathMemory) + api(projects.attributesKt) } testDependencies { @@ -17,6 +17,15 @@ kscience{ } } +kotlin.sourceSets { + filter { it.name.contains("test", true) } + .map(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet::languageSettings) + .forEach { + it.optIn("space.kscience.kmath.PerformancePitfall") + it.optIn("space.kscience.kmath.UnstableKMathAPI") + } +} + readme { description = "Core classes, algebra definitions, basic linear algebra" maturity = space.kscience.gradle.Maturity.DEVELOPMENT @@ -62,4 +71,12 @@ readme { id = "autodiff", ref = "src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt" ) { "Automatic differentiation" } + + feature( + id="Parallel linear algebra" + ){ + """ + Parallel implementation for `LinearAlgebra` + """.trimIndent() + } } diff --git a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/annotations.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/annotations.kt similarity index 97% rename from kmath-memory/src/commonMain/kotlin/space/kscience/kmath/annotations.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/annotations.kt index a95b166cf..13db00968 100644 --- a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/annotations.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/annotations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt index 49e2ee8d0..1dd92980e 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/ColumnarData.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt index 23cbcd0c7..6fe232579 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYColumnarData.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -52,10 +52,10 @@ public interface XYColumnarData : ColumnarData { * Create two-column data from a list of row-objects (zero-copy) */ @UnstableKMathAPI - public fun ofList( + public inline fun ofList( list: List, - xConverter: (I) -> X, - yConverter: (I) -> Y, + noinline xConverter: (I) -> X, + noinline yConverter: (I) -> Y, ): XYColumnarData = object : XYColumnarData { override val size: Int get() = list.size diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt index 797a25443..312894612 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYErrorColumnarData.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt index 846bbad62..2af3c1c19 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/data/XYZColumnarData.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt index eecdcebad..5bfbeb6ac 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt index b03e60e95..79f446f6d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt index 7878f2551..a522381e1 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.domains diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt index 03a080a70..16dae5699 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.domains diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt index b78190c9b..f651dcde3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.domains diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt index 192509993..a6e721d13 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt @@ -1,10 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.expressions +import space.kscience.attributes.SafeType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.* import space.kscience.kmath.structures.Buffer @@ -82,6 +83,8 @@ public abstract class DSAlgebra>( bindings: Map, ) : ExpressionAlgebra>, SymbolIndexer { + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() + /** * Get the compiler for number of free parameters and order. * @@ -186,7 +189,7 @@ public abstract class DSAlgebra>( vararg derivatives: T, ): DS { require(derivatives.size == compiler.size) { "dimension mismatch: ${derivatives.size} and ${compiler.size}" } - val data = derivatives.asBuffer() + val data = derivatives.asList().asBuffer() return DS(data) } @@ -329,10 +332,13 @@ public class DerivativeStructureRingExpression( public val elementBufferFactory: MutableBufferFactory = algebra.bufferFactory, public val function: DSRing.() -> DS, ) : DifferentiableExpression where A : Ring, A : ScaleOperations, A : NumericAlgebra { + + override val type: SafeType get() = elementBufferFactory.type + override operator fun invoke(arguments: Map): T = DSRing(algebra, 0, arguments).function().value - override fun derivativeOrNull(symbols: List): Expression = Expression { arguments -> + override fun derivativeOrNull(symbols: List): Expression = Expression(type) { arguments -> with( DSRing( algebra, @@ -441,10 +447,13 @@ public class DSFieldExpression>( public val algebra: A, public val function: DSField.() -> DS, ) : DifferentiableExpression { + + override val type: SafeType get() = algebra.type + override operator fun invoke(arguments: Map): T = DSField(algebra, 0, arguments).function().value - override fun derivativeOrNull(symbols: List): Expression = Expression { arguments -> + override fun derivativeOrNull(symbols: List): Expression = Expression(type) { arguments -> DSField( algebra, symbols.size, diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSCompiler.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSCompiler.kt index 1da151d46..0699ca1df 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSCompiler.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSCompiler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt index 7c8a957c7..e76374b50 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DifferentiableExpression.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt index f350303bc..f2e134963 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Expression.kt @@ -1,12 +1,18 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.expressions +import space.kscience.attributes.SafeType +import space.kscience.attributes.WithType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.Algebra +import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.LongRing import kotlin.jvm.JvmName import kotlin.properties.ReadOnlyProperty @@ -15,7 +21,7 @@ import kotlin.properties.ReadOnlyProperty * * @param T the type this expression takes as argument and returns. */ -public fun interface Expression { +public interface Expression : WithType { /** * Calls this expression from arguments. * @@ -25,11 +31,26 @@ public fun interface Expression { public operator fun invoke(arguments: Map): T } +/** + * Create an expression from a functional block. + */ +public fun Expression(type: SafeType, block: (Map) -> T): Expression = object : Expression { + override fun invoke(arguments: Map): T = block(arguments) + + override val type: SafeType = type +} + +public inline fun Expression(noinline block: (Map) -> T): Expression = + Expression(safeTypeOf(), block) + /** * Specialization of [Expression] for [Double] allowing better performance because of using array. */ @UnstableKMathAPI public interface DoubleExpression : Expression { + + override val type: SafeType get() = DoubleField.type + /** * The indexer of this expression's arguments that should be used to build array for [invoke]. * @@ -49,7 +70,7 @@ public interface DoubleExpression : Expression { */ public operator fun invoke(arguments: DoubleArray): Double - public companion object{ + public companion object { internal val EMPTY_DOUBLE_ARRAY = DoubleArray(0) } } @@ -59,6 +80,9 @@ public interface DoubleExpression : Expression { */ @UnstableKMathAPI public interface IntExpression : Expression { + + override val type: SafeType get() = IntRing.type + /** * The indexer of this expression's arguments that should be used to build array for [invoke]. * @@ -78,7 +102,7 @@ public interface IntExpression : Expression { */ public operator fun invoke(arguments: IntArray): Int - public companion object{ + public companion object { internal val EMPTY_INT_ARRAY = IntArray(0) } } @@ -88,6 +112,9 @@ public interface IntExpression : Expression { */ @UnstableKMathAPI public interface LongExpression : Expression { + + override val type: SafeType get() = LongRing.type + /** * The indexer of this expression's arguments that should be used to build array for [invoke]. * @@ -107,7 +134,7 @@ public interface LongExpression : Expression { */ public operator fun invoke(arguments: LongArray): Long - public companion object{ + public companion object { internal val EMPTY_LONG_ARRAY = LongArray(0) } } @@ -158,7 +185,6 @@ public operator fun Expression.invoke(vararg pairs: Pair): T = ) - /** * Calls this expression without providing any arguments. * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/ExpressionWithDefault.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/ExpressionWithDefault.kt index c802fe04c..5c5fe707e 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/ExpressionWithDefault.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/ExpressionWithDefault.kt @@ -1,14 +1,19 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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.expressions +import space.kscience.attributes.SafeType + public class ExpressionWithDefault( private val origin: Expression, private val defaultArgs: Map, ) : Expression { + override val type: SafeType + get() = origin.type + override fun invoke(arguments: Map): T = origin.invoke(defaultArgs + arguments) } @@ -21,6 +26,9 @@ public class DiffExpressionWithDefault( private val defaultArgs: Map, ) : DifferentiableExpression { + override val type: SafeType + get() = origin.type + override fun invoke(arguments: Map): T = origin.invoke(defaultArgs + arguments) override fun derivativeOrNull(symbols: List): Expression? = diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt index 8e37f3d32..8ec69c6de 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt @@ -1,11 +1,12 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.expressions import space.kscience.kmath.operations.* +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -17,29 +18,32 @@ import kotlin.contracts.contract public abstract class FunctionalExpressionAlgebra>( public val algebra: A, ) : ExpressionAlgebra> { + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory>() + /** * Builds an Expression of constant expression that does not depend on arguments. */ - override fun const(value: T): Expression = Expression { value } + override fun const(value: T): Expression = Expression(algebra.type) { value } /** * Builds an Expression to access a variable. */ - override fun bindSymbolOrNull(value: String): Expression? = Expression { arguments -> + override fun bindSymbolOrNull(value: String): Expression? = Expression(algebra.type) { arguments -> algebra.bindSymbolOrNull(value) ?: arguments[StringSymbol(value)] ?: error("Symbol '$value' is not supported in $this") } - override fun binaryOperationFunction(operation: String): (left: Expression, right: Expression) -> Expression = - { left, right -> - Expression { arguments -> - algebra.binaryOperationFunction(operation)(left(arguments), right(arguments)) - } + override fun binaryOperationFunction( + operation: String, + ): (left: Expression, right: Expression) -> Expression = { left, right -> + Expression(algebra.type) { arguments -> + algebra.binaryOperationFunction(operation)(left(arguments), right(arguments)) } + } override fun unaryOperationFunction(operation: String): (arg: Expression) -> Expression = { arg -> - Expression { arguments -> algebra.unaryOperation(operation, arg(arguments)) } + Expression(algebra.type) { arguments -> algebra.unaryOperation(operation, arg(arguments)) } } } @@ -49,6 +53,7 @@ public abstract class FunctionalExpressionAlgebra>( public open class FunctionalExpressionGroup>( algebra: A, ) : FunctionalExpressionAlgebra(algebra), Group> { + override val zero: Expression get() = const(algebra.zero) override fun Expression.unaryMinus(): Expression = @@ -120,7 +125,7 @@ public open class FunctionalExpressionField>( super.binaryOperationFunction(operation) override fun scale(a: Expression, value: Double): Expression = algebra { - Expression { args -> a(args) * value } + Expression(algebra.type) { args -> a(args) * value } } override fun bindSymbolOrNull(value: String): Expression? = diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt index 9705a3f03..55c3049b7 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -108,4 +108,4 @@ public fun MST.interpret(algebra: Algebra, vararg arguments: Pair MST.toExpression(algebra: Algebra): Expression = - Expression { arguments -> interpret(algebra, arguments) } + Expression(algebra.type) { arguments -> interpret(algebra, arguments) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt index c894cf00a..04c2b46da 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,11 +7,14 @@ package space.kscience.kmath.expressions import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.* +import space.kscience.kmath.structures.MutableBufferFactory /** * [Algebra] over [MST] nodes. */ public object MstNumericAlgebra : NumericAlgebra { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override fun number(value: Number): MST.Numeric = MST.Numeric(value) override fun bindSymbolOrNull(value: String): Symbol = StringSymbol(value) override fun bindSymbol(value: String): Symbol = bindSymbolOrNull(value) @@ -27,6 +30,9 @@ public object MstNumericAlgebra : NumericAlgebra { * [Group] over [MST] nodes. */ public object MstGroup : Group, NumericAlgebra, ScaleOperations { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override val zero: MST.Numeric = number(0.0) override fun number(value: Number): MST.Numeric = MstNumericAlgebra.number(value) @@ -57,7 +63,11 @@ public object MstGroup : Group, NumericAlgebra, ScaleOperations { @Suppress("OVERRIDE_BY_INLINE") @OptIn(UnstableKMathAPI::class) public object MstRing : Ring, NumbersAddOps, ScaleOperations { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override inline val zero: MST.Numeric get() = MstGroup.zero + override val one: MST.Numeric = number(1.0) override fun number(value: Number): MST.Numeric = MstGroup.number(value) @@ -87,7 +97,11 @@ public object MstRing : Ring, NumbersAddOps, ScaleOperations { @Suppress("OVERRIDE_BY_INLINE") @OptIn(UnstableKMathAPI::class) public object MstField : Field, NumbersAddOps, ScaleOperations { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override inline val zero: MST.Numeric get() = MstRing.zero + override inline val one: MST.Numeric get() = MstRing.one override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value) @@ -117,7 +131,11 @@ public object MstField : Field, NumbersAddOps, ScaleOperations { */ @Suppress("OVERRIDE_BY_INLINE") public object MstExtendedField : ExtendedField, NumericAlgebra { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override inline val zero: MST.Numeric get() = MstField.zero + override inline val one: MST.Numeric get() = MstField.one override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value) @@ -164,6 +182,9 @@ public object MstExtendedField : ExtendedField, NumericAlgebra { */ @UnstableKMathAPI public object MstLogicAlgebra : LogicAlgebra { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value) override fun const(boolean: Boolean): Symbol = if (boolean) { @@ -176,7 +197,7 @@ public object MstLogicAlgebra : LogicAlgebra { override fun MST.and(other: MST): MST = MST.Binary(Boolean::and.name, this, other) - override fun MST.or(other: MST): MST = MST.Binary(Boolean::or.name, this, other) + override fun MST.or(other: MST): MST = MST.Binary(Boolean::or.name, this, other) - override fun MST.xor(other: MST): MST = MST.Binary(Boolean::xor.name, this, other) + override fun MST.xor(other: MST): MST = MST.Binary(Boolean::xor.name, this, other) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/NamedMatrix.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/NamedMatrix.kt index 5d047d5b7..14ccafb0b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/NamedMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/NamedMatrix.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt index 2bb5043b7..1209372b4 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt @@ -1,13 +1,16 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.expressions +import space.kscience.attributes.SafeType +import space.kscience.attributes.WithType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.Point import space.kscience.kmath.operations.* +import space.kscience.kmath.structures.MutableBufferFactory import space.kscience.kmath.structures.asBuffer import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -30,9 +33,10 @@ public open class AutoDiffValue(public val value: T) */ public class DerivationResult( public val value: T, + override val type: SafeType, private val derivativeValues: Map, public val context: Field, -) { +) : WithType { /** * Returns derivative of [variable] or returns [Ring.zero] in [context]. */ @@ -53,15 +57,19 @@ public fun DerivationResult.grad(vararg variables: Symbol): Point>( - public val context: F, + public val algebra: F, bindings: Map, ) : Field>, ExpressionAlgebra>, NumbersAddOps> { - override val zero: AutoDiffValue get() = const(context.zero) - override val one: AutoDiffValue get() = const(context.one) + + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory>() + + override val zero: AutoDiffValue get() = const(algebra.zero) + + override val one: AutoDiffValue get() = const(algebra.one) // this stack contains pairs of blocks and values to apply them to private var stack: Array = arrayOfNulls(8) @@ -69,7 +77,7 @@ public open class SimpleAutoDiffField>( private val derivatives: MutableMap, T> = hashMapOf() private val bindings: Map> = bindings.entries.associate { - it.key.identity to AutoDiffVariableWithDerivative(it.key.identity, it.value, context.zero) + it.key.identity to AutoDiffVariableWithDerivative(it.key.identity, it.value, algebra.zero) } /** @@ -92,7 +100,7 @@ public open class SimpleAutoDiffField>( override fun bindSymbolOrNull(value: String): AutoDiffValue? = bindings[value] private fun getDerivative(variable: AutoDiffValue): T = - (variable as? AutoDiffVariableWithDerivative)?.d ?: derivatives[variable] ?: context.zero + (variable as? AutoDiffVariableWithDerivative)?.d ?: derivatives[variable] ?: algebra.zero private fun setDerivative(variable: AutoDiffValue, value: T) { if (variable is AutoDiffVariableWithDerivative) variable.d = value else derivatives[variable] = value @@ -103,7 +111,7 @@ public open class SimpleAutoDiffField>( while (sp > 0) { val value = stack[--sp] val block = stack[--sp] as F.(Any?) -> Unit - context.block(value) + algebra.block(value) } } @@ -130,7 +138,6 @@ public open class SimpleAutoDiffField>( * } * ``` */ - @Suppress("UNCHECKED_CAST") public fun derive(value: R, block: F.(R) -> Unit): R { // save block to stack for backward pass if (sp >= stack.size) stack = stack.copyOf(stack.size * 2) @@ -142,9 +149,9 @@ public open class SimpleAutoDiffField>( internal fun differentiate(function: SimpleAutoDiffField.() -> AutoDiffValue): DerivationResult { val result = function() - result.d = context.one // computing derivative w.r.t result + result.d = algebra.one // computing derivative w.r.t result runBackwardPass() - return DerivationResult(result.value, bindings.mapValues { it.value.d }, context) + return DerivationResult(result.value, algebra.type, bindings.mapValues { it.value.d }, algebra) } // // Overloads for Double constants @@ -194,7 +201,7 @@ public open class SimpleAutoDiffField>( public inline fun > SimpleAutoDiffField.const(block: F.() -> T): AutoDiffValue { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return const(context.block()) + return const(algebra.block()) } @@ -236,12 +243,15 @@ public class SimpleAutoDiffExpression>( public val field: F, public val function: SimpleAutoDiffField.() -> AutoDiffValue, ) : FirstDerivativeExpression() { + + override val type: SafeType get() = this.field.type + override operator fun invoke(arguments: Map): T { //val bindings = arguments.entries.map { it.key.bind(it.value) } return SimpleAutoDiffField(field, arguments).function().value } - override fun derivativeOrNull(symbol: Symbol): Expression = Expression { arguments -> + override fun derivativeOrNull(symbol: Symbol): Expression = Expression(type) { arguments -> //val bindings = arguments.entries.map { it.key.bind(it.value) } val derivationResult = SimpleAutoDiffField(field, arguments).differentiate(function) derivationResult.derivative(symbol) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Symbol.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Symbol.kt index c57ce69ab..5e2ab64a6 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Symbol.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/Symbol.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt index f0e39d489..d47202df3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt index 4bba47a91..fca29a9dd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt index eb4434c00..bafdbbc3b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -20,7 +20,7 @@ public object Float64LinearSpace : LinearSpace { override fun buildMatrix( rows: Int, columns: Int, - initializer: Float64Field.(i: Int, j: Int) -> Double + initializer: Float64Field.(i: Int, j: Int) -> Double, ): Matrix = Floa64FieldOpsND.structureND(ShapeND(rows, columns)) { (i, j) -> Float64Field.initializer(i, j) }.as2D() @@ -54,11 +54,12 @@ public object Float64LinearSpace : LinearSpace { require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } val rows = this@dot.rows.map { it.linearize() } val columns = other.columns.map { it.linearize() } + val indices = 0 until this.colNum return buildMatrix(rowNum, other.colNum) { i, j -> val r = rows[i] val c = columns[j] var res = 0.0 - for (l in r.indices) { + for (l in indices) { res += r[l] * c[l] } res @@ -69,10 +70,11 @@ public object Float64LinearSpace : LinearSpace { override fun Matrix.dot(vector: Point): Float64Buffer { require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } val rows = this@dot.rows.map { it.linearize() } + val indices = 0 until this.colNum return Float64Buffer(rowNum) { i -> val r = rows[i] var res = 0.0 - for (j in r.indices) { + for (j in indices) { res += r[j] * vector[j] } res diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt index af9ebb463..4082b6f4f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -11,7 +11,7 @@ package space.kscience.kmath.linear * * @param T the type of items. */ -public interface LinearSolver { +public interface LinearSolver { /** * Solve a dot x = b matrix equation and return x */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt index a82bafe57..fb4661b96 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt @@ -1,20 +1,17 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.* import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.nd.MutableStructure2D -import space.kscience.kmath.nd.Structure2D -import space.kscience.kmath.nd.StructureFeature -import space.kscience.kmath.nd.as1D +import space.kscience.kmath.nd.* import space.kscience.kmath.operations.BufferRingOps import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer -import kotlin.reflect.KClass /** * Alias for [Structure2D] with more familiar name. @@ -37,9 +34,11 @@ public typealias Point = Buffer * @param T the type of items in the matrices. * @param A the type of ring over [T]. */ -public interface LinearSpace> { +public interface LinearSpace> : MatrixScope { public val elementAlgebra: A + override val type: SafeType get() = elementAlgebra.type + /** * Produces a matrix with this context and given dimensions. */ @@ -167,16 +166,36 @@ public interface LinearSpace> { public operator fun T.times(v: Point): Point = v * this /** - * Compute a feature of the structure in this scope. Structure features take precedence other context features. + * Compute an [attribute] value for given [structure]. Return null if the attribute could not be computed. + */ + public fun > computeAttribute(structure: Structure2D, attribute: A): V? = null + + @UnstableKMathAPI + public fun > Structure2D.getOrComputeAttribute(attribute: A): V? { + return attributes[attribute] ?: computeAttribute(this, attribute) + } + + /** + * If the structure holds given [attribute] return itself. Otherwise, return a new [Matrix] that contains a computed attribute. * - * @param F the type of feature. - * @param structure the structure. - * @param type the [KClass] instance of [F]. - * @return a feature object or `null` if it isn't present. + * This method is used to compute and cache attribute inside the structure. If one needs an attribute only once, + * better use [StructureND.getOrComputeAttribute]. */ @UnstableKMathAPI - public fun computeFeature(structure: Matrix, type: KClass): F? = - structure.getFeature(type) + public fun > Matrix.compute( + attribute: A, + ): Matrix? { + return if (attributes[attribute] != null) { + this + } else { + val value = computeAttribute(this, attribute) ?: return null + if (this is MatrixWrapper) { + MatrixWrapper(this, attributes.withAttribute(attribute, value)) + } else { + MatrixWrapper(this, Attributes(attribute, value)) + } + } + } public companion object { @@ -184,23 +203,12 @@ public interface LinearSpace> { * A structured matrix with custom buffer */ public fun > buffered( - algebra: A + algebra: A, ): LinearSpace = BufferedLinearSpace(BufferRingOps(algebra)) } } -/** - * Get a feature of the structure in this scope. Structure features take precedence other context features. - * - * @param T the type of items in the matrices. - * @param F the type of feature. - * @return a feature object or `null` if it isn't present. - */ -@UnstableKMathAPI -public inline fun LinearSpace.computeFeature(structure: Matrix): F? = - computeFeature(structure, F::class) - public inline operator fun , R> LS.invoke(block: LS.() -> R): R = run(block) @@ -208,7 +216,7 @@ public inline operator fun , R> LS.invoke(block: LS.() -> /** * Convert matrix to vector if it is possible. */ -public fun Matrix.asVector(): Point = +public fun Matrix.asVector(): Point = if (this.colNum == 1) as1D() else error("Can't convert matrix with more than one column to vector") @@ -219,4 +227,4 @@ public fun Matrix.asVector(): Point = * @receiver a buffer. * @return the new matrix. */ -public fun Point.asMatrix(): VirtualMatrix = VirtualMatrix(size, 1) { i, _ -> get(i) } \ No newline at end of file +public fun Point.asMatrix(): VirtualMatrix = VirtualMatrix(size, 1) { i, _ -> get(i) } \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt index ce55623de..58831f38d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt @@ -1,70 +1,78 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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("UnusedReceiverParameter") +@file:OptIn(PerformancePitfall::class) + package space.kscience.kmath.linear +import space.kscience.attributes.Attributes +import space.kscience.attributes.PolymorphicAttribute +import space.kscience.attributes.safeTypeOf +import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI +import space.kscience.kmath.nd.* import space.kscience.kmath.operations.* -import space.kscience.kmath.structures.BufferAccessor2D -import space.kscience.kmath.structures.Float64Buffer -import space.kscience.kmath.structures.MutableBuffer -import space.kscience.kmath.structures.MutableBufferFactory +import space.kscience.kmath.structures.* + +public interface LupDecomposition { + public val pivot: IntBuffer + public val l: Matrix + public val u: Matrix +} /** - * Common implementation of [LupDecompositionFeature]. + * Create a pivot matrix from pivot vector using provided [LinearSpace] */ -public class LupDecomposition( - public val context: LinearSpace, - public val elementContext: Field, - public val lu: Matrix, - public val pivot: IntArray, - private val even: Boolean, -) : LupDecompositionFeature, DeterminantFeature { - /** - * Returns the matrix L of the decomposition. - * - * L is a lower-triangular matrix with [Ring.one] in diagonal - */ - override val l: Matrix = VirtualMatrix(lu.shape[0], lu.shape[1]) { i, j -> - when { - j < i -> lu[i, j] - j == i -> elementContext.one - else -> elementContext.zero - } - }.withFeature(LFeature) - - - /** - * Returns the matrix U of the decomposition. - * - * U is an upper-triangular matrix including the diagonal - */ - override val u: Matrix = VirtualMatrix(lu.shape[0], lu.shape[1]) { i, j -> - if (j >= i) lu[i, j] else elementContext.zero - }.withFeature(UFeature) - - /** - * Returns the P rows permutation matrix. - * - * P is a sparse matrix with exactly one element set to [Ring.one] in - * each row and each column, all other elements being set to [Ring.zero]. - */ - override val p: Matrix = VirtualMatrix(lu.shape[0], lu.shape[1]) { i, j -> - if (j == pivot[i]) elementContext.one else elementContext.zero +public fun LupDecomposition.pivotMatrix(linearSpace: LinearSpace>): Matrix = + VirtualMatrix(l.rowNum, l.colNum) { row, column -> + if (column == pivot[row]) linearSpace.elementAlgebra.one else linearSpace.elementAlgebra.zero } - /** - * Return the determinant of the matrix - * @return determinant of the matrix - */ - override val determinant: T by lazy { - elementContext { (0 until lu.shape[0]).fold(if (even) one else -one) { value, i -> value * lu[i, i] } } +/** + * Matrices with this feature support LU factorization with partial pivoting: *[p] · a = [l] · [u]* where + * *a* is the owning matrix. + * + * @param T the type of matrices' items. + * @param lu combined L and U matrix + */ +public class GenericLupDecomposition( + public val elementAlgebra: Field, + private val lu: Matrix, + override val pivot: IntBuffer, + private val even: Boolean, +) : LupDecomposition { + + + override val l: Matrix + get() = VirtualMatrix(lu.rowNum, lu.colNum, attributes = Attributes(LowerTriangular)) { i, j -> + when { + j < i -> lu[i, j] + j == i -> elementAlgebra.one + else -> elementAlgebra.zero + } + } + + override val u: Matrix + get() = VirtualMatrix(lu.rowNum, lu.colNum, attributes = Attributes(UpperTriangular)) { i, j -> + if (j >= i) lu[i, j] else elementAlgebra.zero + } + + public val determinant: T by lazy { + elementAlgebra { (0 until l.shape[0]).fold(if (even) one else -one) { value, i -> value * lu[i, i] } } } } +public class LupDecompositionAttribute : + PolymorphicAttribute>(safeTypeOf()), + MatrixAttribute> + +public val MatrixScope.LUP: LupDecompositionAttribute + get() = LupDecompositionAttribute() + @PublishedApi internal fun > LinearSpace>.abs(value: T): T = if (value > elementAlgebra.zero) value else elementAlgebra { -value } @@ -72,159 +80,160 @@ internal fun > LinearSpace>.abs(value: T): T = /** * Create a lup decomposition of generic matrix. */ -public fun > LinearSpace>.lup( - factory: MutableBufferFactory, +public fun > Field.lup( matrix: Matrix, checkSingular: (T) -> Boolean, -): LupDecomposition { +): GenericLupDecomposition { require(matrix.rowNum == matrix.colNum) { "LU decomposition supports only square matrices" } val m = matrix.colNum val pivot = IntArray(matrix.rowNum) - //TODO just waits for multi-receivers - BufferAccessor2D(matrix.rowNum, matrix.colNum, factory).run { - elementAlgebra { - val lu = create(matrix) + val strides = RowStrides(ShapeND(matrix.rowNum, matrix.colNum)) - // Initialize permutation array and parity - for (row in 0 until m) pivot[row] = row - var even = true + val lu = MutableBufferND( + strides, + bufferAlgebra.buffer(strides.linearSize) { offset -> + matrix[strides.index(offset)] + } + ) - // Initialize permutation array and parity - for (row in 0 until m) pivot[row] = row - // Loop over columns - for (col in 0 until m) { - // upper - for (row in 0 until col) { - val luRow = lu.row(row) - var sum = luRow[col] - for (i in 0 until row) sum -= luRow[i] * lu[i, col] - luRow[col] = sum - } + // Initialize the permutation array and parity + for (row in 0 until m) pivot[row] = row + var even = true - // lower - var max = col // permutation row - var largest = -one + // Initialize the permutation array and parity + for (row in 0 until m) pivot[row] = row - for (row in col until m) { - val luRow = lu.row(row) - var sum = luRow[col] - for (i in 0 until col) sum -= luRow[i] * lu[i, col] - luRow[col] = sum + // Loop over columns + for (col in 0 until m) { + // upper + for (row in 0 until col) { + var sum = lu[row, col] + for (i in 0 until row){ + sum -= lu[row, i] * lu[i, col] + } + lu[row, col] = sum + } - // maintain the best permutation choice - if (abs(sum) > largest) { - largest = abs(sum) - max = row - } - } + // lower + var max = col // permutation row + var largest = -one - // Singularity check - check(!checkSingular(abs(lu[max, col]))) { "The matrix is singular" } + for (row in col until m) { + var sum = lu[row, col] + for (i in 0 until col){ + sum -= lu[row, i] * lu[i, col] + } + lu[row, col] = sum - // Pivot if necessary - if (max != col) { - val luMax = lu.row(max) - val luCol = lu.row(col) + // maintain the best permutation choice + if (abs(sum) > largest) { + largest = abs(sum) + max = row + } + } - for (i in 0 until m) { - val tmp = luMax[i] - luMax[i] = luCol[i] - luCol[i] = tmp - } + // Singularity check + check(!checkSingular(abs(lu[max, col]))) { "The matrix is singular" } - val temp = pivot[max] - pivot[max] = pivot[col] - pivot[col] = temp - even = !even - } - - // Divide the lower elements by the "winning" diagonal elt. - val luDiag = lu[col, col] - for (row in col + 1 until m) lu[row, col] /= luDiag + // Pivot if necessary + if (max != col) { + for (i in 0 until m) { + val tmp = lu[max, i] + lu[max, i] = lu[col, i] + lu[col, i] = tmp } - return LupDecomposition(this@lup, elementAlgebra, lu.collect(), pivot, even) + val temp = pivot[max] + pivot[max] = pivot[col] + pivot[col] = temp + even = !even } + + // Divide the lower elements by the "winning" diagonal elt. + val luDiag = lu[col, col] + for (row in col + 1 until m) lu[row, col] /= luDiag } + + + return GenericLupDecomposition(this, lu.as2D(), pivot.asBuffer(), even) + } -public inline fun > LinearSpace>.lup( - matrix: Matrix, - noinline checkSingular: (T) -> Boolean, -): LupDecomposition = lup(MutableBuffer.Companion::auto, matrix, checkSingular) -public fun LinearSpace.lup( +public fun Field.lup( matrix: Matrix, singularityThreshold: Double = 1e-11, -): LupDecomposition = - lup(::Float64Buffer, matrix) { it < singularityThreshold } +): GenericLupDecomposition = lup(matrix) { it < singularityThreshold } -internal fun LupDecomposition.solve( - factory: MutableBufferFactory, +private fun Field.solve( + lup: LupDecomposition, matrix: Matrix, ): Matrix { - require(matrix.rowNum == pivot.size) { "Matrix dimension mismatch. Expected ${pivot.size}, but got ${matrix.colNum}" } + require(matrix.rowNum == lup.l.rowNum) { "Matrix dimension mismatch. Expected ${lup.l.rowNum}, but got ${matrix.colNum}" } - BufferAccessor2D(matrix.rowNum, matrix.colNum, factory).run { - elementContext { - // Apply permutations to b - val bp = create { _, _ -> zero } + val strides = RowStrides(ShapeND(matrix.rowNum, matrix.colNum)) - for (row in pivot.indices) { - val bpRow = bp.row(row) - val pRow = pivot[row] - for (col in 0 until matrix.colNum) bpRow[col] = matrix[pRow, col] - } + // Apply permutations to b + val bp = MutableBufferND( + strides, + bufferAlgebra.buffer(strides.linearSize) { _ -> zero } + ) - // Solve LY = b - for (col in pivot.indices) { - val bpCol = bp.row(col) - for (i in col + 1 until pivot.size) { - val bpI = bp.row(i) - val luICol = lu[i, col] - for (j in 0 until matrix.colNum) { - bpI[j] -= bpCol[j] * luICol - } - } - } - - // Solve UX = Y - for (col in pivot.size - 1 downTo 0) { - val bpCol = bp.row(col) - val luDiag = lu[col, col] - for (j in 0 until matrix.colNum) bpCol[j] /= luDiag - - for (i in 0 until col) { - val bpI = bp.row(i) - val luICol = lu[i, col] - for (j in 0 until matrix.colNum) bpI[j] -= bpCol[j] * luICol - } - } - - return context.buildMatrix(pivot.size, matrix.colNum) { i, j -> bp[i, j] } + for (row in 0 until matrix.rowNum) { + val pRow = lup.pivot[row] + for (col in 0 until matrix.colNum) { + bp[row, col] = matrix[pRow, col] } } + + // Solve LY = b + for (col in 0 until matrix.colNum) { + + for (i in col + 1 until matrix.colNum) { + val luICol = lup.l[i, col] + for (j in 0 until matrix.colNum) { + bp[i, j] -= bp[col, j] * luICol + } + } + } + + // Solve UX = Y + for (col in matrix.colNum - 1 downTo 0) { + val luDiag = lup.u[col, col] + for (j in 0 until matrix.colNum) { + bp[col, j] /= luDiag + } + + for (i in 0 until col) { + val luICol = lup.u[i, col] + for (j in 0 until matrix.colNum) { + bp[i, j] -= bp[col, j] * luICol + } + } + } + + return bp.as2D() } + /** * Produce a generic solver based on LUP decomposition */ @OptIn(UnstableKMathAPI::class) -public fun , F : Field> LinearSpace.lupSolver( - bufferFactory: MutableBufferFactory, +public fun > LinearSpace>.lupSolver( singularityCheck: (T) -> Boolean, ): LinearSolver = object : LinearSolver { - override fun solve(a: Matrix, b: Matrix): Matrix { - // Use existing decomposition if it is provided by matrix - val decomposition = computeFeature(a) ?: lup(bufferFactory, a, singularityCheck) - return decomposition.solve(bufferFactory, b) + override fun solve(a: Matrix, b: Matrix): Matrix = elementAlgebra{ + // Use existing decomposition if it is provided by matrix or linear space itself + val decomposition = a.getOrComputeAttribute(LUP) ?: lup(a, singularityCheck) + return solve(decomposition, b) } override fun inverse(matrix: Matrix): Matrix = solve(matrix, one(matrix.rowNum, matrix.colNum)) } public fun LinearSpace.lupSolver(singularityThreshold: Double = 1e-11): LinearSolver = - lupSolver(::Float64Buffer) { it < singularityThreshold } + lupSolver { it < singularityThreshold } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt index 4d2f01e68..18e1edb56 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt @@ -1,20 +1,26 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.FlagAttribute +import space.kscience.attributes.SafeType +import space.kscience.attributes.WithType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.Ring import space.kscience.kmath.structures.BufferAccessor2D -import space.kscience.kmath.structures.MutableBuffer +import space.kscience.kmath.structures.MutableBufferFactory public class MatrixBuilder>( public val linearSpace: LinearSpace, public val rows: Int, public val columns: Int, -) { +) : WithType { + + override val type: SafeType get() = linearSpace.type + public operator fun invoke(vararg elements: T): Matrix { require(rows * columns == elements.size) { "The number of elements ${elements.size} is not equal $rows * $columns" } return linearSpace.buildMatrix(rows, columns) { i, j -> elements[i * columns + j] } @@ -49,10 +55,10 @@ public inline fun LinearSpace>.column( public fun LinearSpace>.column(vararg values: T): Matrix = column(values.size, values::get) -public object SymmetricMatrixFeature : MatrixFeature +public object Symmetric : MatrixAttribute, FlagAttribute /** - * Naive implementation of a symmetric matrix builder, that adds a [SymmetricMatrixFeature] tag. The resulting matrix contains + * Naive implementation of a symmetric matrix builder, that adds a [Symmetric] tag. The resulting matrix contains * full `size^2` number of elements, but caches elements during calls to save [builder] calls. [builder] is always called in the * upper triangle region meaning that `i <= j` */ @@ -60,7 +66,7 @@ public fun > MatrixBuilder.symmetric( builder: (i: Int, j: Int) -> T, ): Matrix { require(columns == rows) { "In order to build symmetric matrix, number of rows $rows should be equal to number of columns $columns" } - return with(BufferAccessor2D(rows, rows, MutableBuffer.Companion::boxing)) { + return with(BufferAccessor2D(rows, rows, MutableBufferFactory(type))) { val cache = factory(rows * rows) { null } linearSpace.buildMatrix(rows, rows) { i, j -> val cached = cache[i, j] @@ -72,6 +78,6 @@ public fun > MatrixBuilder.symmetric( } else { cached } - }.withFeature(SymmetricMatrixFeature) + }.withAttribute(Symmetric) } } \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt deleted file mode 100644 index ce7acdcba..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright 2018-2022 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.nd.StructureFeature - -/** - * A marker interface representing some properties of matrices or additional transformations of them. Features are used - * to optimize matrix operations performance in some cases or retrieve the APIs. - */ -public interface MatrixFeature: StructureFeature - -/** - * Matrices with this feature are considered to have only diagonal non-null elements. - */ -public interface DiagonalFeature : MatrixFeature { - public companion object : DiagonalFeature -} - -/** - * Matrices with this feature have all zero elements. - */ -public object ZeroFeature : DiagonalFeature - -/** - * Matrices with this feature have unit elements on diagonal and zero elements in all other places. - */ -public object UnitFeature : DiagonalFeature - -/** - * Matrices with this feature can be inverted: *[inverse] = a−1* where *a* is the owning matrix. - * - * @param T the type of matrices' items. - */ -public interface InverseMatrixFeature : MatrixFeature { - /** - * The inverse matrix of the matrix that owns this feature. - */ - public val inverse: Matrix -} - -/** - * Matrices with this feature can compute their determinant. - * - * @param T the type of matrices' items. - */ -public interface DeterminantFeature : MatrixFeature { - /** - * The determinant of the matrix that owns this feature. - */ - public val determinant: T -} - -/** - * Produces a [DeterminantFeature] where the [DeterminantFeature.determinant] is [determinant]. - * - * @param determinant the value of determinant. - * @return a new [DeterminantFeature]. - */ -@Suppress("FunctionName") -public fun DeterminantFeature(determinant: T): DeterminantFeature = object : DeterminantFeature { - override val determinant: T = determinant -} - -/** - * Matrices with this feature are lower triangular ones. - */ -public object LFeature : MatrixFeature - -/** - * Matrices with this feature are upper triangular ones. - */ -public object UFeature : MatrixFeature - -/** - * Matrices with this feature support LU factorization: *a = [l] · [u]* where *a* is the owning matrix. - * - * @param T the type of matrices' items. - */ -public interface LUDecompositionFeature : MatrixFeature { - /** - * The lower triangular matrix in this decomposition. It may have [LFeature]. - */ - public val l: Matrix - - /** - * The upper triangular matrix in this decomposition. It may have [UFeature]. - */ - public val u: Matrix -} - -/** - * Matrices with this feature support LU factorization with partial pivoting: *[p] · a = [l] · [u]* where - * *a* is the owning matrix. - * - * @param T the type of matrices' items. - */ -public interface LupDecompositionFeature : MatrixFeature { - /** - * The lower triangular matrix in this decomposition. It may have [LFeature]. - */ - public val l: Matrix - - /** - * The upper triangular matrix in this decomposition. It may have [UFeature]. - */ - public val u: Matrix - - /** - * The permutation matrix in this decomposition. - */ - public val p: Matrix -} - -/** - * Matrices with this feature are orthogonal ones: *a · aT = u* where *a* is the owning matrix, *u* - * is the unit matrix ([UnitFeature]). - */ -public object OrthogonalFeature : MatrixFeature - -/** - * Matrices with this feature support QR factorization: *a = [q] · [r]* where *a* is the owning matrix. - * - * @param T the type of matrices' items. - */ -public interface QRDecompositionFeature : MatrixFeature { - /** - * The orthogonal matrix in this decomposition. It may have [OrthogonalFeature]. - */ - public val q: Matrix - - /** - * The upper triangular matrix in this decomposition. It may have [UFeature]. - */ - public val r: Matrix -} - -/** - * Matrices with this feature support Cholesky factorization: *a = [l] · [l]H* where *a* is the - * owning matrix. - * - * @param T the type of matrices' items. - */ -public interface CholeskyDecompositionFeature : MatrixFeature { - /** - * The triangular matrix in this decomposition. It may have either [UFeature] or [LFeature]. - */ - public val l: Matrix -} - -/** - * Matrices with this feature support SVD: *a = [u] · [s] · [v]H* where *a* is the owning - * matrix. - * - * @param T the type of matrices' items. - */ -public interface SingularValueDecompositionFeature : MatrixFeature { - /** - * The matrix in this decomposition. It is unitary, and it consists from left singular vectors. - */ - public val u: Matrix - - /** - * The matrix in this decomposition. Its main diagonal elements are singular values. - */ - public val s: Matrix - - /** - * The matrix in this decomposition. It is unitary, and it consists from right singular vectors. - */ - public val v: Matrix - - /** - * The buffer of singular values of this SVD. - */ - public val singularValues: Point -} - -//TODO add sparse matrix feature diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt index 46454a584..a5987bb18 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt @@ -1,35 +1,27 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.Attribute +import space.kscience.attributes.Attributes +import space.kscience.attributes.withAttribute import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.misc.FeatureSet -import space.kscience.kmath.nd.StructureFeature import space.kscience.kmath.operations.Ring -import kotlin.reflect.KClass /** - * A [Matrix] that holds [MatrixFeature] objects. + * A [Matrix] that holds [MatrixAttribute] objects. * * @param T the type of items. */ -public class MatrixWrapper internal constructor( +public class MatrixWrapper internal constructor( public val origin: Matrix, - public val features: FeatureSet, + override val attributes: Attributes, ) : Matrix by origin { - /** - * Get the first feature matching given class. Does not guarantee that matrix has only one feature matching the - * criteria. - */ - @Suppress("UNCHECKED_CAST") - override fun getFeature(type: KClass): F? = - features.getFeature(type) ?: origin.getFeature(type) - - override fun toString(): String = "MatrixWrapper(matrix=$origin, features=$features)" + override fun toString(): String = "MatrixWrapper(matrix=$origin, features=$attributes)" } /** @@ -43,52 +35,50 @@ public val Matrix.origin: Matrix /** * Add a single feature to a [Matrix] */ -public fun Matrix.withFeature(newFeature: MatrixFeature): MatrixWrapper = if (this is MatrixWrapper) { - MatrixWrapper(origin, features.with(newFeature)) +public fun > Matrix.withAttribute( + attribute: A, + attrValue: T, +): MatrixWrapper = if (this is MatrixWrapper) { + MatrixWrapper(origin, attributes.withAttribute(attribute, attrValue)) } else { - MatrixWrapper(this, FeatureSet.of(newFeature)) + MatrixWrapper(this, Attributes(attribute, attrValue)) } -@Deprecated("To be replaced by withFeature") -public operator fun Matrix.plus(newFeature: MatrixFeature): MatrixWrapper = withFeature(newFeature) +public fun > Matrix.withAttribute( + attribute: A, +): MatrixWrapper = if (this is MatrixWrapper) { + MatrixWrapper(origin, attributes.withAttribute(attribute)) +} else { + MatrixWrapper(this, Attributes(attribute, Unit)) +} /** - * Add a collection of features to a [Matrix] + * Modify matrix attributes */ -public fun Matrix.withFeatures(newFeatures: Iterable): MatrixWrapper = +public fun Matrix.modifyAttributes(modifier: (Attributes) -> Attributes): MatrixWrapper = if (this is MatrixWrapper) { - MatrixWrapper(origin, features.with(newFeatures)) + MatrixWrapper(origin, modifier(attributes)) } else { - MatrixWrapper(this, FeatureSet.of(newFeatures)) + MatrixWrapper(this, modifier(Attributes.EMPTY)) } /** - * Diagonal matrix of ones. The matrix is virtual no actual matrix is created. + * Diagonal matrix of ones. The matrix is virtual, no actual matrix is created. */ -public fun LinearSpace>.one( +public fun LinearSpace>.one( rows: Int, columns: Int, -): Matrix = VirtualMatrix(rows, columns) { i, j -> +): MatrixWrapper = VirtualMatrix(rows, columns) { i, j -> if (i == j) elementAlgebra.one else elementAlgebra.zero -}.withFeature(UnitFeature) +}.withAttribute(IsUnit) /** * A virtual matrix of zeroes */ -public fun LinearSpace>.zero( +public fun LinearSpace>.zero( rows: Int, columns: Int, -): Matrix = VirtualMatrix(rows, columns) { _, _ -> +): MatrixWrapper = VirtualMatrix(rows, columns) { _, _ -> elementAlgebra.zero -}.withFeature(ZeroFeature) - -public class TransposedFeature(public val original: Matrix) : MatrixFeature - -/** - * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A` - */ -@Suppress("UNCHECKED_CAST") -@OptIn(UnstableKMathAPI::class) -public fun Matrix.transpose(): Matrix = getFeature(TransposedFeature::class)?.original as? Matrix - ?: VirtualMatrix(colNum, rowNum) { i, j -> get(j, i) }.withFeature(TransposedFeature(this)) +}.withAttribute(IsZero) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Transposed.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Transposed.kt new file mode 100644 index 000000000..36be711e7 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Transposed.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2018-2023 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.attributes.Attributes + + +public class TransposedMatrix(public val origin: Matrix) : Matrix { + + override val rowNum: Int get() = origin.colNum + + override val colNum: Int get() = origin.rowNum + + override fun get(i: Int, j: Int): T = origin[j, i] + + override val attributes: Attributes get() = Attributes.EMPTY +} + + +/** + * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A` + */ +public fun Matrix.transposed(): Matrix = (this as? TransposedMatrix)?.origin ?: TransposedMatrix(this) \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt index 55b970f4a..2b031de06 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt @@ -1,10 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.Attributes import space.kscience.kmath.nd.ShapeND @@ -13,9 +14,10 @@ import space.kscience.kmath.nd.ShapeND * * @property generator the function that provides elements. */ -public class VirtualMatrix( +public class VirtualMatrix( override val rowNum: Int, override val colNum: Int, + override val attributes: Attributes = Attributes.EMPTY, public val generator: (i: Int, j: Int) -> T, ) : Matrix { @@ -24,5 +26,7 @@ public class VirtualMatrix( override operator fun get(i: Int, j: Int): T = generator(i, j) } -public fun MatrixBuilder.virtual(generator: (i: Int, j: Int) -> T): VirtualMatrix = - VirtualMatrix(rows, columns, generator) +public fun MatrixBuilder.virtual( + attributes: Attributes = Attributes.EMPTY, + generator: (i: Int, j: Int) -> T, +): VirtualMatrix = VirtualMatrix(rows, columns, attributes, generator) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/matrixAttributes.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/matrixAttributes.kt new file mode 100644 index 000000000..dadb2f3d7 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/matrixAttributes.kt @@ -0,0 +1,180 @@ +/* + * Copyright 2018-2022 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("UnusedReceiverParameter") + +package space.kscience.kmath.linear + +import space.kscience.attributes.* +import space.kscience.kmath.nd.StructureAttribute + + +/** + * A marker interface for algebras that operate on matrices + * @param T type of matrix element + */ +public interface MatrixScope : AttributeScope>, WithType + +/** + * A marker interface representing some properties of matrices or additional transformations of them. Features are used + * to optimize matrix operations performance in some cases or retrieve the APIs. + */ +public interface MatrixAttribute : StructureAttribute + +/** + * Matrices with this feature are considered to have only diagonal non-zero elements. + */ +public interface IsDiagonal : MatrixAttribute, FlagAttribute { + public companion object : IsDiagonal +} + +/** + * Matrices with this feature have all zero elements. + */ +public object IsZero : IsDiagonal + +/** + * Matrices with this feature have unit elements on diagonal and zero elements in all other places. + */ +public object IsUnit : IsDiagonal + +/** + * Matrices with this feature can be inverted. + * + * @param T the type of matrices' items. + */ +public class Inverted() : + PolymorphicAttribute>(safeTypeOf()), + MatrixAttribute> + +public val MatrixScope.Inverted: Inverted get() = Inverted() + +/** + * Matrices with this feature can compute their determinant. + * + * @param T the type of matrices' items. + */ +public class Determinant(type: SafeType) : + PolymorphicAttribute(type), + MatrixAttribute + +public val MatrixScope.Determinant: Determinant get() = Determinant(type) + +/** + * Matrices with this feature are lower triangular ones. + */ +public object LowerTriangular : MatrixAttribute, FlagAttribute + +/** + * Matrices with this feature are upper triangular ones. + */ +public object UpperTriangular : MatrixAttribute, FlagAttribute + +/** + * Matrices with this feature support LU factorization: *a = [l] · [u]* where *a* is the owning matrix. + * @param l The lower triangular matrix in this decomposition. It may have [LowerTriangular]. + * @param u The upper triangular matrix in this decomposition. It may have [UpperTriangular]. + */ +public data class LUDecomposition(val l: Matrix, val u: Matrix) + +/** + * Matrices with this feature support LU factorization: *a = [l] · [u]* where *a* is the owning matrix. + * + * @param T the type of matrices' items. + */ +public class LuDecompositionAttribute : + PolymorphicAttribute>(safeTypeOf()), + MatrixAttribute> + +public val MatrixScope.LU: LuDecompositionAttribute get() = LuDecompositionAttribute() + + +/** + * Matrices with this feature are orthogonal ones: *a · aT = u* where *a* is the owning matrix, *u* + * is the unit matrix ([IsUnit]). + */ +public object OrthogonalAttribute : MatrixAttribute, FlagAttribute + + +public interface QRDecomposition { + /** + * The orthogonal matrix in this decomposition. It may have [OrthogonalAttribute]. + */ + public val q: Matrix + + /** + * The upper triangular matrix in this decomposition. It may have [UpperTriangular]. + */ + public val r: Matrix +} + +/** + * Matrices with this feature support QR factorization: *a = [QR.q] · [QR.r]* where *a* is the owning matrix. + * + * @param T the type of matrices' items. + */ +public class QRDecompositionAttribute() : + PolymorphicAttribute>(safeTypeOf()), + MatrixAttribute> + +public val MatrixScope.QR: QRDecompositionAttribute + get() = QRDecompositionAttribute() + +public interface CholeskyDecomposition { + /** + * The triangular matrix in this decomposition. It may have either [UpperTriangular] or [LowerTriangular]. + */ + public val l: Matrix +} + +/** + * Matrices with this feature support Cholesky factorization: *a = [l] · [l]H* where *a* is the + * owning matrix. + * + * @param T the type of matrices' items. + */ +public class CholeskyDecompositionAttribute : + PolymorphicAttribute>(safeTypeOf()), + MatrixAttribute> + +public val MatrixScope.Cholesky: CholeskyDecompositionAttribute + get() = CholeskyDecompositionAttribute() + +public interface SingularValueDecomposition { + /** + * The matrix in this decomposition. It is unitary, and it consists of left singular vectors. + */ + public val u: Matrix + + /** + * The matrix in this decomposition. Its main diagonal elements are singular values. + */ + public val s: Matrix + + /** + * The matrix in this decomposition. It is unitary, and it consists of right singular vectors. + */ + public val v: Matrix + + /** + * The buffer of singular values for this SVD. + */ + public val singularValues: Point +} + +/** + * Matrices with this feature support SVD: *a = [u] · [s] · [v]H* where *a* is the owning + * matrix. + * + * @param T the type of matrices' items. + */ +public class SVDAttribute() : + PolymorphicAttribute>(safeTypeOf()), + MatrixAttribute> + +public val MatrixScope.SVD: SVDAttribute get() = SVDAttribute() + + +//TODO add sparse matrix feature diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Featured.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Featured.kt deleted file mode 100644 index bdda674dc..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Featured.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2018-2022 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.misc - -import kotlin.jvm.JvmInline -import kotlin.reflect.KClass - -/** - * An entity that contains a set of features defined by their types - */ -public interface Featured { - public fun getFeature(type: FeatureKey): T? -} - -public typealias FeatureKey = KClass - -public interface Feature> { - - /** - * A key used for extraction - */ - @Suppress("UNCHECKED_CAST") - public val key: FeatureKey - get() = this::class as FeatureKey -} - -/** - * A container for a set of features - */ -@JvmInline -public value class FeatureSet> private constructor(public val features: Map, F>) : Featured { - @Suppress("UNCHECKED_CAST") - override fun getFeature(type: FeatureKey): T? = features[type]?.let { it as T } - - public inline fun getFeature(): T? = getFeature(T::class) - - public fun with(feature: T, type: FeatureKey = feature.key): FeatureSet = - FeatureSet(features + (type to feature)) - - public fun with(other: FeatureSet): FeatureSet = FeatureSet(features + other.features) - - public fun with(vararg otherFeatures: F): FeatureSet = - FeatureSet(features + otherFeatures.associateBy { it.key }) - - public fun with(otherFeatures: Iterable): FeatureSet = - FeatureSet(features + otherFeatures.associateBy { it.key }) - - public operator fun iterator(): Iterator = features.values.iterator() - - override fun toString(): String = features.values.joinToString(prefix = "[ ", postfix = " ]") - - - public companion object { - public fun > of(vararg features: F): FeatureSet = FeatureSet(features.associateBy { it.key }) - public fun > of(features: Iterable): FeatureSet = - FeatureSet(features.associateBy { it.key }) - } -} diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt index f630055fa..6227e2f45 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt index c05f1a6a9..60f71b943 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/logging.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/logging.kt index c7886469f..9655e1139 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/logging.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/logging.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/numbers.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/numbers.kt index 77ef07ea8..cc43c84bc 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/numbers.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/numbers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt index f369f0614..e1b24dd68 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt @@ -1,9 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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:OptIn(UnstableKMathAPI::class) + package space.kscience.kmath.misc import space.kscience.kmath.UnstableKMathAPI @@ -22,7 +24,6 @@ public fun > Buffer.indicesSorted(): IntArray = permSortInd /** * Create a zero-copy virtual buffer that contains the same elements but in ascending order */ -@OptIn(UnstableKMathAPI::class) public fun > Buffer.sorted(): Buffer { val permutations = indicesSorted() return VirtualBuffer(size) { this[permutations[it]] } @@ -35,7 +36,6 @@ public fun > Buffer.indicesSortedDescending(): IntArray = /** * Create a zero-copy virtual buffer that contains the same elements but in descending order */ -@OptIn(UnstableKMathAPI::class) public fun > Buffer.sortedDescending(): Buffer { val permutations = indicesSortedDescending() return VirtualBuffer(size) { this[permutations[it]] } @@ -45,7 +45,6 @@ public fun > Buffer.sortedDescending(): Buffer { public fun > Buffer.indicesSortedBy(selector: (V) -> C): IntArray = permSortIndicesWith(compareBy { selector(get(it)) }) -@OptIn(UnstableKMathAPI::class) public fun > Buffer.sortedBy(selector: (V) -> C): Buffer { val permutations = indicesSortedBy(selector) return VirtualBuffer(size) { this[permutations[it]] } @@ -55,7 +54,6 @@ public fun > Buffer.sortedBy(selector: (V) -> C): Buffer public fun > Buffer.indicesSortedByDescending(selector: (V) -> C): IntArray = permSortIndicesWith(compareByDescending { selector(get(it)) }) -@OptIn(UnstableKMathAPI::class) public fun > Buffer.sortedByDescending(selector: (V) -> C): Buffer { val permutations = indicesSortedByDescending(selector) return VirtualBuffer(size) { this[permutations[it]] } @@ -68,7 +66,6 @@ public fun Buffer.indicesSortedWith(comparator: Comparator): IntArray /** * Create virtual zero-copy buffer with elements sorted by [comparator] */ -@OptIn(UnstableKMathAPI::class) public fun Buffer.sortedWith(comparator: Comparator): Buffer { val permutations = indicesSortedWith(comparator) return VirtualBuffer(size) { this[permutations[it]] } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt index 91e26cc1b..744f760f3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,7 @@ package space.kscience.kmath.nd import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.* -import kotlin.reflect.KClass +import space.kscience.kmath.structures.MutableBufferFactory /** * The base interface for all ND-algebra implementations. @@ -16,16 +16,22 @@ import kotlin.reflect.KClass * @param T the type of ND-structure element. * @param C the type of the element context. */ -public interface AlgebraND>: Algebra> { +public interface AlgebraND> : Algebra> { /** * The algebra over elements of ND structure. */ public val elementAlgebra: C + /** + * Produces a new [MutableStructureND] using given initializer function. + */ + public fun mutableStructureND(shape: ShapeND, initializer: C.(IntArray) -> T): MutableStructureND + /** * Produces a new [StructureND] using given initializer function. */ - public fun structureND(shape: ShapeND, initializer: C.(IntArray) -> T): StructureND + public fun structureND(shape: ShapeND, initializer: C.(IntArray) -> T): StructureND = + mutableStructureND(shape, initializer) /** * Maps elements from one structure to another one by applying [transform] to them. @@ -65,31 +71,19 @@ public interface AlgebraND>: Algebra> { structure.map { value -> this@invoke(value) } /** - * Get a feature of the structure in this scope. Structure features take precedence other context features. + * Get an attribute value for the structure in this scope. Structure features take precedence other context features. * - * @param F the type of feature. * @param structure the structure. - * @param type the [KClass] instance of [F]. + * @param attribute to be computed. * @return a feature object or `null` if it isn't present. */ @UnstableKMathAPI - public fun getFeature(structure: StructureND, type: KClass): F? = - structure.getFeature(type) + public fun > attributeFor(structure: StructureND<*>, attribute: A): T? = + structure.attributes[attribute] public companion object } -/** - * Get a feature of the structure in this scope. Structure features take precedence other context features. - * - * @param T the type of items in the matrices. - * @param F the type of feature. - * @return a feature object or `null` if it isn't present. - */ -@UnstableKMathAPI -public inline fun AlgebraND.getFeature(structure: StructureND): F? = - getFeature(structure, F::class) - /** * Space of [StructureND]. * @@ -97,6 +91,8 @@ public inline fun AlgebraND.getFea * @param A the type of group over structure elements. */ public interface GroupOpsND> : GroupOps>, AlgebraND { + override val bufferFactory: MutableBufferFactory> get() = MutableBufferFactory>() + /** * Element-wise addition. * @@ -108,7 +104,7 @@ public interface GroupOpsND> : GroupOps>, override fun add(left: StructureND, right: StructureND): StructureND = zip(left, right) { aValue, bValue -> add(aValue, bValue) } - // TODO move to extensions after KEEP-176 + // TODO implement using context receivers /** * Adds an ND structure to an element of it. @@ -175,8 +171,6 @@ public interface RingOpsND> : RingOps>, Gro override fun multiply(left: StructureND, right: StructureND): StructureND = zip(left, right) { aValue, bValue -> multiply(aValue, bValue) } - //TODO move to extensions with context receivers - /** * Multiplies an ND structure by an element of it. * @@ -226,7 +220,6 @@ public interface FieldOpsND> : override fun divide(left: StructureND, right: StructureND): StructureND = zip(left, right) { aValue, bValue -> divide(aValue, bValue) } - //TODO move to extensions after https://github.com/Kotlin/KEEP/blob/master/proposals/context-receivers.md /** * Divides an ND structure by an element of it. * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt index 74c63e6e2..a99df4c1f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,8 @@ package space.kscience.kmath.nd +import space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.* @@ -16,9 +18,10 @@ public interface BufferAlgebraND> : AlgebraND { public val bufferAlgebra: BufferAlgebra override val elementAlgebra: A get() = bufferAlgebra.elementAlgebra - override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): BufferND { + //TODO change AlgebraND contract to include this + override fun mutableStructureND(shape: ShapeND, initializer: A.(IntArray) -> T): MutableBufferND { val indexer = indexerBuilder(shape) - return BufferND( + return MutableBufferND( indexer, bufferAlgebra.buffer(indexer.linearSize) { offset -> elementAlgebra.initializer(indexer.index(offset)) @@ -26,6 +29,9 @@ public interface BufferAlgebraND> : AlgebraND { ) } + override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): BufferND = + mutableStructureND(shape, initializer) + @OptIn(PerformancePitfall::class) public fun StructureND.toBufferND(): BufferND = when (this) { is BufferND -> this @@ -101,6 +107,9 @@ public open class BufferedGroupNDOps>( override val bufferAlgebra: BufferAlgebra, override val indexerBuilder: (ShapeND) -> ShapeIndexer = BufferAlgebraND.defaultIndexerBuilder, ) : GroupOpsND, BufferAlgebraND { + + override val type: SafeType> get() = safeTypeOf>() + override fun StructureND.unaryMinus(): StructureND = map { -it } } @@ -133,6 +142,11 @@ public fun > BufferAlgebraND.structureND( initializer: A.(IntArray) -> T, ): BufferND = structureND(ShapeND(shape), initializer) +public fun > BufferAlgebraND.mutableStructureND( + vararg shape: Int, + initializer: A.(IntArray) -> T, +): MutableBufferND = mutableStructureND(ShapeND(shape), initializer) + public fun , A> A.structureND( initializer: EA.(IntArray) -> T, ): BufferND where A : BufferAlgebraND, A : WithShape = structureND(shape, initializer) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt index 9217f6fdc..5746ff881 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -34,10 +34,10 @@ public open class BufferND( /** * Create a generic [BufferND] using provided [initializer] */ -public fun BufferND( +public inline fun BufferND( shape: ShapeND, - bufferFactory: BufferFactory = BufferFactory.boxing(), - initializer: (IntArray) -> T, + bufferFactory: BufferFactory = BufferFactory(), + crossinline initializer: (IntArray) -> T, ): BufferND { val strides = Strides(shape) return BufferND(strides, bufferFactory(strides.linearSize) { initializer(strides.index(it)) }) @@ -84,10 +84,10 @@ public open class MutableBufferND( /** * Create a generic [BufferND] using provided [initializer] */ -public fun MutableBufferND( +public inline fun MutableBufferND( shape: ShapeND, - bufferFactory: MutableBufferFactory = MutableBufferFactory.boxing(), - initializer: (IntArray) -> T, + bufferFactory: MutableBufferFactory = MutableBufferFactory(), + crossinline initializer: (IntArray) -> T, ): MutableBufferND { val strides = Strides(shape) return MutableBufferND(strides, bufferFactory(strides.linearSize) { initializer(strides.index(it)) }) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt index 464ee16b9..4bb853af8 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -20,7 +20,8 @@ import kotlin.math.pow as kpow public class Float64BufferND( indexes: ShapeIndexer, override val buffer: Float64Buffer, -) : MutableBufferND(indexes, buffer), MutableStructureNDOfDouble{ +) : MutableBufferND(indexes, buffer), MutableStructureNDOfDouble { + override fun getDouble(index: IntArray): Double = buffer[indices.offset(index)] override fun setDouble(index: IntArray, value: Double) { @@ -74,7 +75,7 @@ public sealed class Floa64FieldOpsND : BufferedFieldOpsND( transform: Float64Field.(Double, Double) -> Double, ): BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> Float64Field.transform(l, r) } - override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): Float64BufferND { + override fun mutableStructureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): Float64BufferND { val indexer = indexerBuilder(shape) return Float64BufferND( indexer, @@ -225,7 +226,7 @@ public class Float64FieldND(override val shape: ShapeND) : override fun number(value: Number): Float64BufferND { val d = value.toDouble() // minimize conversions - return structureND(shape) { d } + return mutableStructureND(shape) { d } } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Int16RingND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Int16RingND.kt index 758a486f5..2f91a3326 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Int16RingND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Int16RingND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt index 039432bc7..128badda4 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/PermutedStructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/PermutedStructureND.kt index 6c35e2f44..28130c037 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/PermutedStructureND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/PermutedStructureND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -32,7 +32,6 @@ public class PermutedMutableStructureND( public val permutation: (IntArray) -> IntArray, ) : MutableStructureND { - @OptIn(PerformancePitfall::class) override fun set(index: IntArray, value: T) { origin[permutation(index)] = value diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndices.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndices.kt index 3a27614c5..e8545125f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndices.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndices.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeND.kt index d43ebaf1c..dee22e49f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt index 984b5ad0f..14f2aa7c2 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -9,7 +9,6 @@ import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.operations.asSequence import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.MutableBuffer -import space.kscience.kmath.structures.asMutableBuffer import kotlin.jvm.JvmInline /** @@ -46,6 +45,7 @@ public interface MutableStructure1D : Structure1D, MutableStructureND, */ @JvmInline private value class Structure1DWrapper(val structure: StructureND) : Structure1D { + override val shape: ShapeND get() = structure.shape override val size: Int get() = structure.shape[0] @@ -60,6 +60,7 @@ private value class Structure1DWrapper(val structure: StructureND) : S * A 1D wrapper for a mutable nd-structure */ private class MutableStructure1DWrapper(val structure: MutableStructureND) : MutableStructure1D { + override val shape: ShapeND get() = structure.shape override val size: Int get() = structure.shape[0] @@ -74,13 +75,6 @@ private class MutableStructure1DWrapper(val structure: MutableStructureND) structure[intArrayOf(index)] = value } - @OptIn(PerformancePitfall::class) - override fun copy(): MutableBuffer = structure - .elements() - .map(Pair::second) - .toMutableList() - .asMutableBuffer() - override fun toString(): String = Buffer.toString(this) } @@ -90,6 +84,7 @@ private class MutableStructure1DWrapper(val structure: MutableStructureND) */ @JvmInline private value class Buffer1DWrapper(val buffer: Buffer) : Structure1D { + override val shape: ShapeND get() = ShapeND(buffer.size) override val size: Int get() = buffer.size @@ -102,6 +97,7 @@ private value class Buffer1DWrapper(val buffer: Buffer) : Structure1D< } internal class MutableBuffer1DWrapper(val buffer: MutableBuffer) : MutableStructure1D { + override val shape: ShapeND get() = ShapeND(buffer.size) override val size: Int get() = buffer.size @@ -115,8 +111,6 @@ internal class MutableBuffer1DWrapper(val buffer: MutableBuffer) : Mutable buffer[index] = value } - override fun copy(): MutableBuffer = buffer.copy() - override fun toString(): String = Buffer.toString(this) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt index e006d09eb..0a3209970 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt @@ -1,17 +1,16 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.nd +import space.kscience.attributes.Attributes import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.MutableBuffer -import space.kscience.kmath.structures.MutableListBuffer import space.kscience.kmath.structures.VirtualBuffer import kotlin.jvm.JvmInline -import kotlin.reflect.KClass /** * A structure that is guaranteed to be two-dimensional. @@ -32,14 +31,14 @@ public interface Structure2D : StructureND { override val shape: ShapeND get() = ShapeND(rowNum, colNum) /** - * The buffer of rows of this structure. It gets elements from the structure dynamically. + * The buffer of rows for this structure. It gets elements from the structure dynamically. */ @PerformancePitfall public val rows: List> get() = List(rowNum) { i -> VirtualBuffer(colNum) { j -> get(i, j) } } /** - * The buffer of columns of this structure. It gets elements from the structure dynamically. + * The buffer of columns for this structure. It gets elements from the structure dynamically. */ @PerformancePitfall public val columns: List> @@ -69,6 +68,25 @@ public interface Structure2D : StructureND { public companion object } +/** + * A linear accessor for a [MutableStructureND] + */ +@OptIn(PerformancePitfall::class) +public class MutableStructureNDAccessorBuffer( + public val structure: MutableStructureND, + override val size: Int, + private val indexer: (Int) -> IntArray, +) : MutableBuffer { + + override fun set(index: Int, value: T) { + structure[indexer(index)] = value + } + + override fun get(index: Int): T = structure[indexer(index)] + + override fun toString(): String = "AccessorBuffer(structure=$structure, size=$size)" +} + /** * Represents mutable [Structure2D]. */ @@ -87,14 +105,18 @@ public interface MutableStructure2D : Structure2D, MutableStructureND { */ @PerformancePitfall override val rows: List> - get() = List(rowNum) { i -> MutableListBuffer(colNum) { j -> get(i, j) } } + get() = List(rowNum) { i -> + MutableStructureNDAccessorBuffer(this, colNum) { j -> intArrayOf(i, j) } + } /** - * The buffer of columns of this structure. It gets elements from the structure dynamically. + * The buffer of columns for this structure. It gets elements from the structure dynamically. */ @PerformancePitfall override val columns: List> - get() = List(colNum) { j -> MutableListBuffer(rowNum) { i -> get(i, j) } } + get() = List(colNum) { j -> + MutableStructureNDAccessorBuffer(this, rowNum) { i -> intArrayOf(i, j) } + } } /** @@ -102,6 +124,7 @@ public interface MutableStructure2D : Structure2D, MutableStructureND { */ @JvmInline private value class Structure2DWrapper(val structure: StructureND) : Structure2D { + override val shape: ShapeND get() = structure.shape override val rowNum: Int get() = shape[0] @@ -110,7 +133,8 @@ private value class Structure2DWrapper(val structure: StructureND) : S @PerformancePitfall override operator fun get(i: Int, j: Int): T = structure[i, j] - override fun getFeature(type: KClass): F? = structure.getFeature(type) + override val attributes: Attributes + get() = structure.attributes @PerformancePitfall override fun elements(): Sequence> = structure.elements() @@ -120,6 +144,7 @@ private value class Structure2DWrapper(val structure: StructureND) : S * A 2D wrapper for a mutable nd-structure */ private class MutableStructure2DWrapper(val structure: MutableStructureND) : MutableStructure2D { + override val shape: ShapeND get() = structure.shape override val rowNum: Int get() = shape[0] diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt index e643186ba..5c1021147 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt @@ -1,23 +1,22 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.nd +import space.kscience.attributes.Attribute +import space.kscience.attributes.AttributeContainer +import space.kscience.attributes.Attributes +import space.kscience.attributes.SafeType import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.linear.LinearSpace -import space.kscience.kmath.misc.Feature -import space.kscience.kmath.misc.Featured import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.BufferFactory -import kotlin.jvm.JvmName import kotlin.math.abs -import kotlin.reflect.KClass -public interface StructureFeature : Feature +public interface StructureAttribute : Attribute /** * Represents n-dimensional structure i.e., multidimensional container of items of the same type and size. The number @@ -28,9 +27,9 @@ public interface StructureFeature : Feature * * @param T the type of items. */ -public interface StructureND : Featured, WithShape { +public interface StructureND : AttributeContainer, WithShape { /** - * The shape of structure i.e., non-empty sequence of non-negative integers that specify sizes of dimensions of + * The shape of structure i.e., non-empty sequence of non-negative integers that specify sizes of dimensions for * this structure. */ override val shape: ShapeND @@ -57,11 +56,7 @@ public interface StructureND : Featured, WithShape { @PerformancePitfall public fun elements(): Sequence> = indices.asSequence().map { it to get(it) } - /** - * Feature is some additional structure information that allows to access it special properties or hints. - * If the feature is not present, `null` is returned. - */ - override fun getFeature(type: KClass): F? = null + override val attributes: Attributes get() = Attributes.EMPTY public companion object { /** @@ -121,57 +116,53 @@ public interface StructureND : Featured, WithShape { return "$className(shape=${structure.shape}, buffer=$bufferRepr)" } - /** - * Creates a NDStructure with explicit buffer factory. - * - * Strides should be reused if possible. - */ - public fun buffered( - strides: Strides, - bufferFactory: BufferFactory = BufferFactory.boxing(), - initializer: (IntArray) -> T, - ): BufferND = BufferND(strides, bufferFactory(strides.linearSize) { i -> initializer(strides.index(i)) }) - - /** - * Inline create NDStructure with non-boxing buffer implementation if it is possible - */ - public inline fun auto( - strides: Strides, - crossinline initializer: (IntArray) -> T, - ): BufferND = BufferND(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) }) - - public inline fun auto( - type: KClass, - strides: Strides, - crossinline initializer: (IntArray) -> T, - ): BufferND = BufferND(strides, Buffer.auto(type, strides.linearSize) { i -> initializer(strides.index(i)) }) - - public fun buffered( - shape: ShapeND, - bufferFactory: BufferFactory = BufferFactory.boxing(), - initializer: (IntArray) -> T, - ): BufferND = buffered(ColumnStrides(shape), bufferFactory, initializer) - - public inline fun auto( - shape: ShapeND, - crossinline initializer: (IntArray) -> T, - ): BufferND = auto(ColumnStrides(shape), initializer) - - @JvmName("autoVarArg") - public inline fun auto( - vararg shape: Int, - crossinline initializer: (IntArray) -> T, - ): BufferND = - auto(ColumnStrides(ShapeND(shape)), initializer) - - public inline fun auto( - type: KClass, - vararg shape: Int, - crossinline initializer: (IntArray) -> T, - ): BufferND = auto(type, ColumnStrides(ShapeND(shape)), initializer) } } + +/** + * Creates a NDStructure with explicit buffer factory. + * + * Strides should be reused if possible. + */ +public fun BufferND( + type: SafeType, + strides: Strides, + initializer: (IntArray) -> T, +): BufferND = BufferND(strides, Buffer(type, strides.linearSize) { i -> initializer(strides.index(i)) }) + + +public fun BufferND( + type: SafeType, + shape: ShapeND, + initializer: (IntArray) -> T, +): BufferND = BufferND(type, ColumnStrides(shape), initializer) + +/** + * Inline create NDStructure with non-boxing buffer implementation if it is possible + */ +public inline fun BufferND( + strides: Strides, + crossinline initializer: (IntArray) -> T, +): BufferND = BufferND(strides, Buffer(strides.linearSize) { i -> initializer(strides.index(i)) }) + +public inline fun BufferND( + shape: ShapeND, + crossinline initializer: (IntArray) -> T, +): BufferND = BufferND(ColumnStrides(shape), initializer) + +public inline fun BufferND( + vararg shape: Int, + crossinline initializer: (IntArray) -> T, +): BufferND = BufferND(ColumnStrides(ShapeND(shape)), initializer) + +public fun BufferND( + type: SafeType, + vararg shape: Int, + initializer: (IntArray) -> T, +): BufferND = BufferND(type, ColumnStrides(ShapeND(shape)), initializer) + + /** * Indicates whether some [StructureND] is equal to another one. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt index 606b9a631..a7e0b0053 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt index f0d4bd7f5..2cada103d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -17,6 +17,12 @@ public fun > AlgebraND.structureND( initializer: A.(IntArray) -> T ): StructureND = structureND(ShapeND(shapeFirst, *shapeRest), initializer) +public fun > AlgebraND.mutableStructureND( + shapeFirst: Int, + vararg shapeRest: Int, + initializer: A.(IntArray) -> T +): MutableStructureND = mutableStructureND(ShapeND(shapeFirst, *shapeRest), initializer) + public fun > AlgebraND.zero(shape: ShapeND): StructureND = structureND(shape) { zero } @JvmName("zeroVarArg") diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/operationsND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/operationsND.kt index 40db5187f..b91f2bde5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/operationsND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/operationsND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/primitiveStructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/primitiveStructureND.kt index 28e32363f..4831b4138 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/primitiveStructureND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/primitiveStructureND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,6 +8,7 @@ package space.kscience.kmath.nd import space.kscience.kmath.PerformancePitfall public interface StructureNDOfDouble : StructureND { + /** * Guaranteed non-blocking access to content */ @@ -22,6 +23,7 @@ public fun StructureND.getDouble(index: IntArray): Double = if (this is StructureNDOfDouble) getDouble(index) else get(index) public interface MutableStructureNDOfDouble : StructureNDOfDouble, MutableStructureND { + /** * Guaranteed non-blocking access to content */ @@ -34,6 +36,7 @@ public fun MutableStructureND.getDouble(index: IntArray): Double = public interface StructureNDOfInt : StructureND { + /** * Guaranteed non-blocking access to content */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt index 67f37ed62..2f21e47d6 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt @@ -1,10 +1,12 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.SafeType +import space.kscience.attributes.WithType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.operations.Ring.Companion.optimizedPower @@ -13,14 +15,15 @@ import space.kscience.kmath.structures.MutableBufferFactory /** * Represents an algebraic structure. * - * @param T the type of element of this structure. + * @param T the type of element which Algebra operates on. */ -public interface Algebra { - +public interface Algebra : WithType { /** * Provide a factory for buffers, associated with this [Algebra] */ - public val bufferFactory: MutableBufferFactory get() = MutableBufferFactory.boxing() + public val bufferFactory: MutableBufferFactory + + override val type: SafeType get() = bufferFactory.type /** * Wraps a raw string to [T] object. This method is designed for three purposes: @@ -67,12 +70,12 @@ public interface Algebra { * * @param operation the name of operation. * @param arg the argument of operation. - * @return a result of operation. + * @return the result of the operation. */ public fun unaryOperation(operation: String, arg: T): T = unaryOperationFunction(operation)(arg) /** - * Dynamically dispatches a binary operation with the certain name. + * Dynamically dispatches a binary operation with a certain name. * * Implementations must fulfil the following requirements: * @@ -87,7 +90,7 @@ public interface Algebra { error("Binary operation '$operation' not defined in $this") /** - * Dynamically invokes a binary operation with the certain name. + * Dynamically invokes a binary operation with a certain name. * * Implementations must fulfil the following requirements: * @@ -265,7 +268,7 @@ public interface Ring : Group, RingOps { */ public fun power(arg: T, pow: UInt): T = optimizedPower(arg, pow) - public companion object{ + public companion object { /** * Raises [arg] to the non-negative integer power [exponent]. * @@ -348,7 +351,7 @@ public interface Field : Ring, FieldOps, ScaleOperations, NumericAlg public fun power(arg: T, pow: Int): T = optimizedPower(arg, pow) - public companion object{ + public companion object { /** * Raises [arg] to the integer power [exponent]. * @@ -361,7 +364,11 @@ public interface Field : Ring, FieldOps, ScaleOperations, NumericAlg * @author Iaroslav Postovalov, Evgeniy Zhelenskiy */ private fun Field.optimizedPower(arg: T, exponent: Int): T = when { - exponent < 0 -> one / (this as Ring).optimizedPower(arg, if (exponent == Int.MIN_VALUE) Int.MAX_VALUE.toUInt().inc() else (-exponent).toUInt()) + exponent < 0 -> one / (this as Ring).optimizedPower( + arg, + if (exponent == Int.MIN_VALUE) Int.MAX_VALUE.toUInt().inc() else (-exponent).toUInt() + ) + else -> (this as Ring).optimizedPower(arg, exponent.toUInt()) } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt index 34a6d4a80..de26b0cf0 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BigInt.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -10,13 +10,13 @@ import space.kscience.kmath.nd.BufferedRingOpsND import space.kscience.kmath.operations.BigInt.Companion.BASE import space.kscience.kmath.operations.BigInt.Companion.BASE_SIZE import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.log2 import kotlin.math.max import kotlin.math.min import kotlin.math.sign private typealias Magnitude = UIntArray -private typealias TBase = ULong /** * Kotlin Multiplatform implementation of Big Integer numbers (KBigInteger). @@ -26,6 +26,9 @@ private typealias TBase = ULong */ @OptIn(UnstableKMathAPI::class) public object BigIntField : Field, NumbersAddOps, ScaleOperations { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override val zero: BigInt = BigInt.ZERO override val one: BigInt = BigInt.ONE @@ -528,10 +531,10 @@ public fun String.parseBigInteger(): BigInt? { public val BigInt.algebra: BigIntField get() = BigIntField public inline fun BigInt.Companion.buffer(size: Int, initializer: (Int) -> BigInt): Buffer = - Buffer.boxing(size, initializer) + Buffer(size, initializer) public inline fun BigInt.Companion.mutableBuffer(size: Int, initializer: (Int) -> BigInt): Buffer = - Buffer.boxing(size, initializer) + Buffer(size, initializer) public val BigIntField.nd: BufferedRingOpsND get() = BufferedRingOpsND(BufferRingOps(BigIntField)) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt index 0363f39d3..6c260db9f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt @@ -1,12 +1,13 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.structures.Buffer -import space.kscience.kmath.structures.BufferFactory +import space.kscience.kmath.structures.MutableBuffer +import space.kscience.kmath.structures.MutableBufferFactory public interface WithSize { public val size: Int @@ -17,7 +18,10 @@ public interface WithSize { */ public interface BufferAlgebra> : Algebra> { public val elementAlgebra: A - public val elementBufferFactory: BufferFactory get() = elementAlgebra.bufferFactory + + public val elementBufferFactory: MutableBufferFactory get() = elementAlgebra.bufferFactory + + override val bufferFactory: MutableBufferFactory> get() = MutableBufferFactory() public fun buffer(size: Int, vararg elements: T): Buffer { require(elements.size == size) { "Expected $size elements but found ${elements.size}" } @@ -73,13 +77,11 @@ private inline fun > BufferAlgebra.zipInline( return elementBufferFactory(l.size) { elementAlgebra.block(l[it], r[it]) } } -public fun BufferAlgebra.buffer(size: Int, initializer: (Int) -> T): Buffer { - return elementBufferFactory(size, initializer) -} +public fun BufferAlgebra.buffer(size: Int, initializer: (Int) -> T): MutableBuffer = + elementBufferFactory(size, initializer) -public fun A.buffer(initializer: (Int) -> T): Buffer where A : BufferAlgebra, A : WithSize { - return elementBufferFactory(size, initializer) -} +public fun A.buffer(initializer: (Int) -> T): MutableBuffer where A : BufferAlgebra, A : WithSize = + elementBufferFactory(size, initializer) public fun > BufferAlgebra.sin(arg: Buffer): Buffer = mapInline(arg) { sin(it) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt index e33cb2c6e..5defbd7c5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt index 923534e1c..ca4eacb8a 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt index d8bf0fb57..c4a26e242 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,7 @@ package space.kscience.kmath.operations import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Symbol +import space.kscience.kmath.structures.MutableBufferFactory /** * An algebra for generic boolean logic @@ -72,6 +73,8 @@ public interface LogicAlgebra : Algebra { @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object BooleanAlgebra : LogicAlgebra { + override val bufferFactory: MutableBufferFactory get() = MutableBufferFactory() + override fun const(boolean: Boolean): Boolean = boolean override fun Boolean.not(): Boolean = !this diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt index 06ad68298..4f15418b3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/OptionalOperations.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/OptionalOperations.kt index 48cac2870..422134bcd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/OptionalOperations.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/OptionalOperations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt index ddf599240..872d72857 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt index 72e33c523..17f6f2889 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,10 @@ package space.kscience.kmath.operations import space.kscience.kmath.operations.Int16Field.div import space.kscience.kmath.operations.Int32Field.div import space.kscience.kmath.operations.Int64Field.div -import space.kscience.kmath.structures.* +import space.kscience.kmath.structures.Int16 +import space.kscience.kmath.structures.Int32 +import space.kscience.kmath.structures.Int64 +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.roundToInt import kotlin.math.roundToLong @@ -22,7 +25,7 @@ import kotlin.math.roundToLong */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int16Field : Field, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int16Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Int16 get() = 0 override val one: Int16 get() = 1 @@ -45,7 +48,8 @@ public object Int16Field : Field, Norm, NumericAlgebra, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int32Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override val zero: Int get() = 0 override val one: Int get() = 1 @@ -68,7 +72,7 @@ public object Int32Field : Field, Norm, NumericAlgebra, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int64Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Int64 get() = 0L override val one: Int64 get() = 1L diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt index 0b7bef852..96a080afd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt @@ -1,10 +1,10 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.structures.* +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.pow as kpow @@ -67,7 +67,7 @@ public interface ExtendedField : ExtendedFieldOps, Field, NumericAlgebr */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Float64Field : ExtendedField, Norm, ScaleOperations { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Float64Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Double get() = 0.0 override val one: Double get() = 1.0 @@ -129,7 +129,7 @@ public val Double.Companion.algebra: Float64Field get() = Float64Field */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Float32Field : ExtendedField, Norm { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Float32Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Float get() = 0.0f override val one: Float get() = 1.0f @@ -187,7 +187,7 @@ public val Float.Companion.algebra: Float32Field get() = Float32Field */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int32Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int32Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Int get() = 0 override val one: Int get() = 1 @@ -212,7 +212,7 @@ public val Int.Companion.algebra: Int32Ring get() = Int32Ring */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int16Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int16Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Short get() = 0 override val one: Short get() = 1 @@ -237,7 +237,7 @@ public val Short.Companion.algebra: Int16Ring get() = Int16Ring */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int8Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int8Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Byte get() = 0 override val one: Byte get() = 1 @@ -262,7 +262,7 @@ public val Byte.Companion.algebra: Int8Ring get() = Int8Ring */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int64Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int64Buffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: Long get() = 0L override val one: Long get() = 1L diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ArrayBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ArrayBuffer.kt index 8e81dd941..892f6a7c5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ArrayBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ArrayBuffer.kt @@ -1,17 +1,20 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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 kotlin.jvm.JvmInline + /** * [MutableBuffer] implementation over [Array]. * * @param T the type of elements contained in the buffer. * @property array The underlying array. */ -public class ArrayBuffer(internal val array: Array) : MutableBuffer { +@JvmInline +public value class ArrayBuffer(internal val array: Array) : MutableBuffer { // Can't inline because array is invariant override val size: Int get() = array.size @@ -22,13 +25,11 @@ public class ArrayBuffer(internal val array: Array) : MutableBuffer { } override operator fun iterator(): Iterator = array.iterator() - override fun copy(): MutableBuffer = ArrayBuffer(array.copyOf()) override fun toString(): String = Buffer.toString(this) } - /** * Returns an [ArrayBuffer] that wraps the original array. */ -public fun Array.asBuffer(): ArrayBuffer = ArrayBuffer(this) \ No newline at end of file +public fun Array.asBuffer(): ArrayBuffer = ArrayBuffer( this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt index 4c8ba0652..7fc06c43e 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt @@ -1,49 +1,64 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.attributes.SafeType +import space.kscience.attributes.WithType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.operations.WithSize import space.kscience.kmath.operations.asSequence -import kotlin.jvm.JvmInline -import kotlin.reflect.KClass +import kotlin.reflect.typeOf /** * Function that produces [Buffer] from its size and function that supplies values. * * @param T the type of buffer. */ -public fun interface BufferFactory { +public interface BufferFactory : WithType { + public operator fun invoke(size: Int, builder: (Int) -> T): Buffer - - public companion object{ - public inline fun auto(): BufferFactory = - BufferFactory(Buffer.Companion::auto) - - public fun boxing(): BufferFactory = - BufferFactory(Buffer.Companion::boxing) - } } +/** + * Create a [BufferFactory] for given [type], using primitive storage if possible + */ +public fun BufferFactory(type: SafeType): BufferFactory = object : BufferFactory { + override val type: SafeType = type + override fun invoke(size: Int, builder: (Int) -> T): Buffer = Buffer(type, size, builder) +} + +/** + * Create [BufferFactory] using the reified type + */ +public inline fun BufferFactory(): BufferFactory = BufferFactory(safeTypeOf()) + /** * Function that produces [MutableBuffer] from its size and function that supplies values. * * @param T the type of buffer. */ -public fun interface MutableBufferFactory : BufferFactory { +public interface MutableBufferFactory : BufferFactory { override fun invoke(size: Int, builder: (Int) -> T): MutableBuffer - public companion object { - public inline fun auto(): MutableBufferFactory = - MutableBufferFactory(MutableBuffer.Companion::auto) - - public fun boxing(): MutableBufferFactory = - MutableBufferFactory(MutableBuffer.Companion::boxing) - } + public companion object } +/** + * Create a [MutableBufferFactory] for given [type], using primitive storage if possible + */ +public fun MutableBufferFactory(type: SafeType): MutableBufferFactory = object : MutableBufferFactory { + override val type: SafeType = type + override fun invoke(size: Int, builder: (Int) -> T): MutableBuffer = MutableBuffer(type, size, builder) +} + +/** + * Create [BufferFactory] using the reified type + */ +public inline fun MutableBufferFactory(): MutableBufferFactory = MutableBufferFactory(safeTypeOf()) + /** * A generic read-only random-access structure for both primitives and objects. * @@ -58,7 +73,7 @@ public interface Buffer : WithSize { override val size: Int /** - * Gets element at given index. + * Gets an element at given index. */ public operator fun get(index: Int): T @@ -85,38 +100,46 @@ public interface Buffer : WithSize { return true } - /** - * Creates a [ListBuffer] of given type [T] with given [size]. Each element is calculated by calling the - * specified [initializer] function. - */ - public inline fun boxing(size: Int, initializer: (Int) -> T): Buffer = - List(size, initializer).asBuffer() + } +} - /** - * Creates a [Buffer] of given [type]. If the type is primitive, specialized buffers are used ([Int32Buffer], - * [Float64Buffer], etc.), [ListBuffer] is returned otherwise. - * - * The [size] is specified, and each element is calculated by calling the specified [initializer] function. - */ - @Suppress("UNCHECKED_CAST") - public inline fun auto(type: KClass, size: Int, initializer: (Int) -> T): Buffer = - when (type) { - Double::class -> MutableBuffer.double(size) { initializer(it) as Double } as Buffer - Short::class -> MutableBuffer.short(size) { initializer(it) as Short } as Buffer - Int::class -> MutableBuffer.int(size) { initializer(it) as Int } as Buffer - Long::class -> MutableBuffer.long(size) { initializer(it) as Long } as Buffer - Float::class -> MutableBuffer.float(size) { initializer(it) as Float } as Buffer - else -> boxing(size, initializer) - } +/** + * Creates a [Buffer] of given [type]. If the type is primitive, specialized buffers are used ([Int32Buffer], + * [Float64Buffer], etc.), [ListBuffer] is returned otherwise. + * + * The [size] is specified, and each element is calculated by calling the specified [initializer] function. + */ +@Suppress("UNCHECKED_CAST", "DuplicatedCode") +public fun Buffer( + type: SafeType, + size: Int, + initializer: (Int) -> T, +): Buffer = when (type.kType) { + typeOf() -> MutableBuffer.double(size) { initializer(it) as Double } as Buffer + typeOf() -> MutableBuffer.short(size) { initializer(it) as Short } as Buffer + typeOf() -> MutableBuffer.int(size) { initializer(it) as Int } as Buffer + typeOf() -> MutableBuffer.long(size) { initializer(it) as Long } as Buffer + typeOf() -> MutableBuffer.float(size) { initializer(it) as Float } as Buffer + else -> List(size, initializer).asBuffer() +} - /** - * Creates a [Buffer] of given type [T]. If the type is primitive, specialized buffers are used ([Int32Buffer], - * [Float64Buffer], etc.), [ListBuffer] is returned otherwise. - * - * The [size] is specified, and each element is calculated by calling the specified [initializer] function. - */ - public inline fun auto(size: Int, initializer: (Int) -> T): Buffer = - auto(T::class, size, initializer) +/** + * Creates a [Buffer] of given type [T]. If the type is primitive, specialized buffers are used ([Int32Buffer], + * [Float64Buffer], etc.), [ListBuffer] is returned otherwise. + * + * The [size] is specified, and each element is calculated by calling the specified [initializer] function. + */ +@Suppress("UNCHECKED_CAST", "DuplicatedCode") +public inline fun Buffer(size: Int, initializer: (Int) -> T): Buffer { + //code duplication here because we want to inline initializers + val type = safeTypeOf() + return when (type.kType) { + typeOf() -> MutableBuffer.double(size) { initializer(it) as Double } as Buffer + typeOf() -> MutableBuffer.short(size) { initializer(it) as Short } as Buffer + typeOf() -> MutableBuffer.int(size) { initializer(it) as Int } as Buffer + typeOf() -> MutableBuffer.long(size) { initializer(it) as Long } as Buffer + typeOf() -> MutableBuffer.float(size) { initializer(it) as Float } as Buffer + else -> List(size, initializer).asBuffer() } } @@ -142,28 +165,16 @@ public fun Buffer.last(): T { return get(size - 1) } -/** - * Immutable wrapper for [MutableBuffer]. - * - * @param T the type of elements contained in the buffer. - * @property buffer The underlying buffer. - */ -@JvmInline -public value class ReadOnlyBuffer(public val buffer: MutableBuffer) : Buffer { - override val size: Int get() = buffer.size - - override operator fun get(index: Int): T = buffer[index] - - override operator fun iterator(): Iterator = buffer.iterator() -} - /** * A buffer with content calculated on-demand. The calculated content is not stored, so it is recalculated on each call. - * Useful when one needs single element from the buffer. + * Useful when one needs a single element from the buffer. * * @param T the type of elements provided by the buffer. */ -public class VirtualBuffer(override val size: Int, private val generator: (Int) -> T) : Buffer { +public class VirtualBuffer( + override val size: Int, + private val generator: (Int) -> T, +) : Buffer { override operator fun get(index: Int): T { if (index < 0 || index >= size) throw IndexOutOfBoundsException("Expected index from 0 to ${size - 1}, but found $index") return generator(index) @@ -173,8 +184,3 @@ public class VirtualBuffer(override val size: Int, private val generator: override fun toString(): String = Buffer.toString(this) } - -/** - * Convert this buffer to read-only buffer. - */ -public fun Buffer.asReadOnly(): Buffer = if (this is MutableBuffer) ReadOnlyBuffer(this) else this \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt index 48f3e919b..32990c707 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt @@ -1,11 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.nd.Structure2D /** * A context that allows to operate on a [MutableBuffer] as on 2d array @@ -26,15 +26,13 @@ internal class BufferAccessor2D( fun create(mat: Structure2D): MutableBuffer = create { i, j -> mat[i, j] } - //TODO optimize wrapper - fun MutableBuffer.collect(): Structure2D = StructureND.buffered( - ColumnStrides(ShapeND(rowNum, colNum)), - factory - ) { (i, j) -> - get(i, j) - }.as2D() +// //TODO optimize wrapper +// fun MutableBuffer.toStructure2D(): Structure2D = BufferND( +// type, ShapeND(rowNum, colNum) +// ) { (i, j) -> get(i, j) }.as2D() inner class Row(val buffer: MutableBuffer, val rowIndex: Int) : MutableBuffer { + override val size: Int get() = colNum override operator fun get(index: Int): T = buffer[rowIndex, index] @@ -43,7 +41,6 @@ internal class BufferAccessor2D( buffer[rowIndex, index] = value } - override fun copy(): MutableBuffer = factory(colNum) { get(it) } override operator fun iterator(): Iterator = (0 until colNum).map(::get).iterator() override fun toString(): String = Buffer.toString(this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferList.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferList.kt new file mode 100644 index 000000000..ff668589e --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferList.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2018-2023 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 kotlin.jvm.JvmInline + +@JvmInline +private value class BufferList(val buffer: Buffer) : List { + override val size: Int get() = buffer.size + + override fun get(index: Int): T = buffer[index] + + override fun isEmpty(): Boolean = buffer.size == 0 + + override fun iterator(): Iterator = buffer.iterator() + + override fun listIterator(index: Int): ListIterator = object : ListIterator { + var currentIndex = index + + override fun hasNext(): Boolean = currentIndex < buffer.size - 1 + + override fun hasPrevious(): Boolean = currentIndex > 0 + + override fun next(): T { + if (!hasNext()) throw NoSuchElementException() + return get(currentIndex++) + } + + override fun nextIndex(): Int = currentIndex + + override fun previous(): T { + if (!hasPrevious()) throw NoSuchElementException() + return get(--currentIndex) + } + + override fun previousIndex(): Int = currentIndex - 1 + + } + + override fun listIterator(): ListIterator = listIterator(0) + + override fun subList(fromIndex: Int, toIndex: Int): List = + buffer.slice(fromIndex..toIndex).asList() + + override fun lastIndexOf(element: T): Int { + for (i in buffer.indices.reversed()) { + if (buffer[i] == element) return i + } + return -1 + } + + override fun indexOf(element: T): Int { + for (i in buffer.indices) { + if (buffer[i] == element) return i + } + return -1 + } + + override fun containsAll(elements: Collection): Boolean { + val remainingElements = HashSet(elements) + for (e in buffer) { + if (e in remainingElements) { + remainingElements.remove(e) + } + if (remainingElements.isEmpty()) { + return true + } + } + return false + } + + override fun contains(element: T): Boolean = indexOf(element) >= 0 +} + +/** + * Returns a zero-copy list that reflects the content of the buffer. + */ +public fun Buffer.asList(): List = BufferList(this) \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt index f538fdd01..180098f51 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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.UnstableKMathAPI @@ -36,6 +41,7 @@ public class BufferSlice( } } + override fun get(index: Int): T = if (index >= size) { throw IndexOutOfBoundsException("$index is out of ${0 until size} rage") } else { @@ -100,7 +106,8 @@ public fun Buffer.slice(range: IntRange): BufferView = if (this is Buf * Dynamically create a range from the initial range */ @UnstableKMathAPI -public inline fun Buffer.slice(rangeBuilder: IntRange.() -> IntRange): BufferView = slice(rangeBuilder(indices)) +public inline fun Buffer.slice(rangeBuilder: IntRange.() -> IntRange): BufferView = + slice(rangeBuilder(indices)) /** * Resize original buffer to a given range using given [range], filling additional segments with [defaultValue]. @@ -180,9 +187,7 @@ public class PermutedMutableBuffer( origin[permutations[index]] = value } - override fun copy(): MutableBuffer = PermutedMutableBuffer(origin.copy(), permutations) //TODO Probably could be optimized - override fun iterator(): Iterator = permutations.asSequence().map { origin[it] }.iterator() @UnstableKMathAPI diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FlaggedBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FlaggedBuffer.kt index d99e02996..ce31163a9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FlaggedBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FlaggedBuffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -57,6 +57,7 @@ public class FlaggedDoubleBuffer( public val values: DoubleArray, public val flags: ByteArray ) : FlaggedBuffer, Buffer { + init { require(values.size == flags.size) { "Values and flags must have the same dimensions" } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt index 44ef4dcf0..7ad6c2a0d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,6 +15,7 @@ import kotlin.jvm.JvmInline */ @JvmInline public value class Float32Buffer(public val array: FloatArray) : PrimitiveBuffer { + override val size: Int get() = array.size override operator fun get(index: Int): Float = array[index] @@ -25,8 +26,6 @@ public value class Float32Buffer(public val array: FloatArray) : PrimitiveBuffer override operator fun iterator(): FloatIterator = array.iterator() - override fun copy(): MutableBuffer = - Float32Buffer(array.copyOf()) } public typealias FloatBuffer = Float32Buffer diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt index 0542c1bf4..38b737fd5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,6 +15,7 @@ import kotlin.jvm.JvmInline */ @JvmInline public value class Float64Buffer(public val array: DoubleArray) : PrimitiveBuffer { + override val size: Int get() = array.size override operator fun get(index: Int): Double = array[index] @@ -25,8 +26,6 @@ public value class Float64Buffer(public val array: DoubleArray) : PrimitiveBuffe override operator fun iterator(): DoubleIterator = array.iterator() - override fun copy(): Float64Buffer = Float64Buffer(array.copyOf()) - override fun toString(): String = Buffer.toString(this) public companion object { @@ -34,6 +33,7 @@ public value class Float64Buffer(public val array: DoubleArray) : PrimitiveBuffe } } +@Deprecated("Use Float64Buffer instead", ReplaceWith("Float64Buffer")) public typealias DoubleBuffer = Float64Buffer /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt index 1ba40c934..9ceeeec05 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,6 +14,8 @@ import kotlin.jvm.JvmInline */ @JvmInline public value class Int16Buffer(public val array: ShortArray) : MutableBuffer { + + override val size: Int get() = array.size override operator fun get(index: Int): Short = array[index] @@ -23,7 +25,6 @@ public value class Int16Buffer(public val array: ShortArray) : MutableBuffer = Int16Buffer(array.copyOf()) } public typealias ShortBuffer = Int16Buffer diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt index afd94e9cd..bb41c2b9b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,6 +14,8 @@ import kotlin.jvm.JvmInline */ @JvmInline public value class Int32Buffer(public val array: IntArray) : PrimitiveBuffer { + + override val size: Int get() = array.size override operator fun get(index: Int): Int = array[index] @@ -24,7 +26,6 @@ public value class Int32Buffer(public val array: IntArray) : PrimitiveBuffer { + override val size: Int get() = array.size override operator fun get(index: Int): Long = array[index] @@ -24,8 +25,6 @@ public value class Int64Buffer(public val array: LongArray) : PrimitiveBuffer = - Int64Buffer(array.copyOf()) } public typealias LongBuffer = Int64Buffer diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt index 923fbec06..c7b1d43c5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,6 +14,7 @@ import kotlin.jvm.JvmInline */ @JvmInline public value class Int8Buffer(public val array: ByteArray) : MutableBuffer { + override val size: Int get() = array.size override operator fun get(index: Int): Byte = array[index] @@ -23,7 +24,6 @@ public value class Int8Buffer(public val array: ByteArray) : MutableBuffer } override operator fun iterator(): ByteIterator = array.iterator() - override fun copy(): MutableBuffer = Int8Buffer(array.copyOf()) } /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ListBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ListBuffer.kt index fbc9a489b..501bd1c64 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ListBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ListBuffer.kt @@ -1,12 +1,10 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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 kotlin.jvm.JvmInline - /** * [Buffer] implementation over [List]. * @@ -15,8 +13,6 @@ import kotlin.jvm.JvmInline */ public class ListBuffer(public val list: List) : Buffer { - public constructor(size: Int, initializer: (Int) -> T) : this(List(size, initializer)) - override val size: Int get() = list.size override operator fun get(index: Int): T = list[index] @@ -37,10 +33,7 @@ public fun List.asBuffer(): ListBuffer = ListBuffer(this) * @param T the type of elements contained in the buffer. * @property list The underlying list. */ -@JvmInline -public value class MutableListBuffer(public val list: MutableList) : MutableBuffer { - - public constructor(size: Int, initializer: (Int) -> T) : this(MutableList(size, initializer)) +public class MutableListBuffer(public val list: MutableList) : MutableBuffer { override val size: Int get() = list.size @@ -51,10 +44,12 @@ public value class MutableListBuffer(public val list: MutableList) : Mutab } override operator fun iterator(): Iterator = list.iterator() - override fun copy(): MutableBuffer = MutableListBuffer(ArrayList(list)) + + override fun toString(): String = Buffer.toString(this) } + /** * Returns an [MutableListBuffer] that wraps the original list. */ -public fun MutableList.asMutableBuffer(): MutableListBuffer = MutableListBuffer(this) +public fun MutableList.asMutableBuffer(): MutableListBuffer = MutableListBuffer(this) \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt index 772c670ea..be0c76618 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt @@ -1,27 +1,26 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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 kotlin.reflect.KClass +import space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf +import kotlin.reflect.typeOf /** * A generic mutable random-access structure for both primitives and objects. * * @param T the type of elements contained in the buffer. */ -public interface MutableBuffer : Buffer { +public interface MutableBuffer : Buffer{ + /** * Sets the array element at the specified [index] to the specified [value]. */ public operator fun set(index: Int, value: T) - /** - * Returns a shallow copy of the buffer. - */ - public fun copy(): MutableBuffer public companion object { /** @@ -59,42 +58,59 @@ public interface MutableBuffer : Buffer { */ public inline fun float(size: Int, initializer: (Int) -> Float): Float32Buffer = Float32Buffer(size, initializer) - - - /** - * Create a boxing mutable buffer of given type - */ - public inline fun boxing(size: Int, initializer: (Int) -> T): MutableBuffer = - MutableListBuffer(MutableList(size, initializer)) - - /** - * Creates a [MutableBuffer] of given [type]. If the type is primitive, specialized buffers are used - * ([Int32Buffer], [Float64Buffer], etc.), [ListBuffer] is returned otherwise. - * - * The [size] is specified, and each element is calculated by calling the specified [initializer] function. - */ - @Suppress("UNCHECKED_CAST") - public inline fun auto(type: KClass, size: Int, initializer: (Int) -> T): MutableBuffer = - when (type) { - Double::class -> double(size) { initializer(it) as Double } as MutableBuffer - Short::class -> short(size) { initializer(it) as Short } as MutableBuffer - Int::class -> int(size) { initializer(it) as Int } as MutableBuffer - Float::class -> float(size) { initializer(it) as Float } as MutableBuffer - Long::class -> long(size) { initializer(it) as Long } as MutableBuffer - else -> boxing(size, initializer) - } - - /** - * Creates a [MutableBuffer] of given type [T]. If the type is primitive, specialized buffers are used - * ([Int32Buffer], [Float64Buffer], etc.), [ListBuffer] is returned otherwise. - * - * The [size] is specified, and each element is calculated by calling the specified [initializer] function. - */ - @Suppress("UNCHECKED_CAST") - public inline fun auto(size: Int, initializer: (Int) -> T): MutableBuffer = - auto(T::class, size, initializer) } } -public sealed interface PrimitiveBuffer: MutableBuffer \ No newline at end of file +/** + * Returns a shallow copy of the buffer. + */ +public fun Buffer.copy(bufferFactory: BufferFactory): Buffer =if(this is ArrayBuffer){ + ArrayBuffer(array.copyOf()) +}else{ + bufferFactory(size,::get) +} + +/** + * Returns a mutable shallow copy of the buffer. + */ +public fun Buffer.mutableCopy(bufferFactory: MutableBufferFactory): MutableBuffer =if(this is ArrayBuffer){ + ArrayBuffer(array.copyOf()) +}else{ + bufferFactory(size,::get) +} + + +/** + * Creates a [MutableBuffer] of given [type]. If the type is primitive, specialized buffers are used + * ([Int32Buffer], [Float64Buffer], etc.), [ListBuffer] is returned otherwise. + * + * The [size] is specified, and each element is calculated by calling the specified [initializer] function. + */ +@Suppress("UNCHECKED_CAST") +public inline fun MutableBuffer( + type: SafeType, + size: Int, + initializer: (Int) -> T, +): MutableBuffer = when (type.kType) { + typeOf() -> Int8Buffer(size) { initializer(it) as Int8 } as MutableBuffer + typeOf() -> MutableBuffer.short(size) { initializer(it) as Int16 } as MutableBuffer + typeOf() -> MutableBuffer.int(size) { initializer(it) as Int32 } as MutableBuffer + typeOf() -> MutableBuffer.long(size) { initializer(it) as Int64 } as MutableBuffer + typeOf() -> MutableBuffer.float(size) { initializer(it) as Float } as MutableBuffer + typeOf() -> MutableBuffer.double(size) { initializer(it) as Double } as MutableBuffer + //TODO add unsigned types + else -> MutableListBuffer(MutableList(size, initializer)) +} + +/** + * Creates a [MutableBuffer] of given type [T]. If the type is primitive, specialized buffers are used + * ([Int32Buffer], [Float64Buffer], etc.), [ListBuffer] is returned otherwise. + * + * The [size] is specified, and each element is calculated by calling the specified [initializer] function. + */ +public inline fun MutableBuffer(size: Int, initializer: (Int) -> T): MutableBuffer = + MutableBuffer(safeTypeOf(), size, initializer) + + +public sealed interface PrimitiveBuffer : MutableBuffer \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferExtensions.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferExtensions.kt index 6a7b6d836..7c69669c8 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferExtensions.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt index 9033dea98..6035811b9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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.UnstableKMathAPI diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/types.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/types.kt index 4ace17538..3d5f87abc 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/types.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/types.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt index 71bbc325c..eb679cec6 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt index 153090d35..ca2e31d7d 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt index 6533af344..b9ab5e62c 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt index c0c5fc321..dcfb16042 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt index 4d05f9043..901b1157c 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -10,7 +10,6 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.algebra import kotlin.test.Test -import kotlin.test.assertEquals import kotlin.test.assertTrue @OptIn(PerformancePitfall::class) @@ -29,18 +28,18 @@ class DoubleLUSolverTest { } @Test - fun testDecomposition() = Double.algebra.linearSpace.run { + fun testDecomposition() = with(Double.algebra.linearSpace){ val matrix = matrix(2, 2)( 3.0, 1.0, 2.0, 3.0 ) - val lup = lup(matrix) + val lup = elementAlgebra.lup(matrix) //Check determinant - assertEquals(7.0, lup.determinant) +// assertEquals(7.0, lup.determinant) - assertMatrixEquals(lup.p dot matrix, lup.l dot lup.u) + assertMatrixEquals(lup.pivotMatrix(this) dot matrix, lup.l dot lup.u) } @Test diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt index 531aee259..eb76d59dc 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,7 @@ package space.kscience.kmath.linear import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureND -import space.kscience.kmath.nd.as2D +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.algebra import kotlin.test.Test import kotlin.test.assertEquals @@ -22,7 +22,7 @@ class MatrixTest { @Test fun testTranspose() = Double.algebra.linearSpace.run { val matrix = one(3, 3) - val transposed = matrix.transpose() + val transposed = matrix.transposed() assertTrue { StructureND.contentEquals(matrix, transposed) } } @@ -59,9 +59,9 @@ class MatrixTest { } @Test - fun test2DDot() = Double.algebra.linearSpace.run { - val firstMatrix = StructureND.auto(2, 3) { (i, j) -> (i + j).toDouble() }.as2D() - val secondMatrix = StructureND.auto(3, 2) { (i, j) -> (i + j).toDouble() }.as2D() + fun test2DDot() = Float64Field.linearSpace { + val firstMatrix = buildMatrix(2, 3) { i, j -> (i + j).toDouble() } + val secondMatrix = buildMatrix(3, 2) { i, j -> (i + j).toDouble() } // val firstMatrix = produce(2, 3) { i, j -> (i + j).toDouble() } // val secondMatrix = produce(3, 2) { i, j -> (i + j).toDouble() } @@ -71,6 +71,5 @@ class MatrixTest { assertEquals(8.0, result[0, 1]) assertEquals(8.0, result[1, 0]) assertEquals(14.0, result[1, 1]) - } } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/CumulativeKtTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/CumulativeKtTest.kt index 811f2e87f..9cedb8bb3 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/CumulativeKtTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/CumulativeKtTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt index fa9ac19c0..a8fc1c765 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt index ada7f4244..d0f79bb15 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/StridesTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/StridesTest.kt index e6335f652..1c26c215c 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/StridesTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/StridesTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntAlgebraTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntAlgebraTest.kt index 06a9ab439..0bbb92151 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntAlgebraTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntAlgebraTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConstructorTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConstructorTest.kt index 786c68c70..69bccacdd 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConstructorTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConstructorTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConversionsTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConversionsTest.kt index 36f00dc75..f9b5dca2a 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConversionsTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntConversionsTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntOperationsTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntOperationsTest.kt index 8a6116605..1f47dc2c1 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntOperationsTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/BigIntOperationsTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt index 2f0ee28e7..51783df13 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/BufferExpandedTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/BufferExpandedTest.kt index 04671e040..0a9b1d897 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/BufferExpandedTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/BufferExpandedTest.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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 kotlin.test.Test diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt index d6c6adedd..0fc9cb9e3 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt index 4ee6d50cb..2f10af972 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/jsMain/kotlin/space/kscience/kmath/misc/numbers.kt b/kmath-core/src/jsMain/kotlin/space/kscience/kmath/misc/numbers.kt index e52ea9298..e0875993f 100644 --- a/kmath-core/src/jsMain/kotlin/space/kscience/kmath/misc/numbers.kt +++ b/kmath-core/src/jsMain/kotlin/space/kscience/kmath/misc/numbers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/jsMain/kotlin/space/kscience/kmath/operations/isInteger.kt b/kmath-core/src/jsMain/kotlin/space/kscience/kmath/operations/isInteger.kt index 3103f5168..da697bd77 100644 --- a/kmath-core/src/jsMain/kotlin/space/kscience/kmath/operations/isInteger.kt +++ b/kmath-core/src/jsMain/kotlin/space/kscience/kmath/operations/isInteger.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/linear/Float64ParallelLinearSpace.kt b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/linear/Float64ParallelLinearSpace.kt new file mode 100644 index 000000000..fd200d9f8 --- /dev/null +++ b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/linear/Float64ParallelLinearSpace.kt @@ -0,0 +1,125 @@ +/* + * Copyright 2018-2024 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.PerformancePitfall +import space.kscience.kmath.nd.* +import space.kscience.kmath.operations.Float64BufferOps +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.invoke +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.Float64Buffer +import space.kscience.kmath.structures.asBuffer +import java.util.stream.IntStream + + +public object Float64ParallelLinearSpace : LinearSpace { + + override val elementAlgebra: Float64Field = Float64Field + + + override fun buildMatrix( + rows: Int, + columns: Int, + initializer: Float64Field.(i: Int, j: Int) -> Double, + ): Matrix { + val shape = ShapeND(rows, columns) + val indexer = BufferAlgebraND.defaultIndexerBuilder(shape) + + val buffer = IntStream.range(0, indexer.linearSize).parallel().mapToDouble { offset -> + val (i, j) = indexer.index(offset) + elementAlgebra.initializer(i, j) + }.toArray().asBuffer() + + return MutableBufferND( + indexer, + buffer + ).as2D() + } + + override fun buildVector(size: Int, initializer: Float64Field.(Int) -> Double): Float64Buffer = + IntStream.range(0, size).parallel().mapToDouble{ Float64Field.initializer(it) }.toArray().asBuffer() + + override fun Matrix.unaryMinus(): Matrix = Floa64FieldOpsND { + asND().map { -it }.as2D() + } + + override fun Matrix.plus(other: Matrix): Matrix = Floa64FieldOpsND { + require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::plus. Expected $shape but found ${other.shape}" } + asND().plus(other.asND()).as2D() + } + + override fun Matrix.minus(other: Matrix): Matrix = Floa64FieldOpsND { + require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::minus. Expected $shape but found ${other.shape}" } + asND().minus(other.asND()).as2D() + } + + // Create a continuous in-memory representation of this vector for better memory layout handling + private fun Buffer.linearize() = if (this is Float64Buffer) { + this.array + } else { + DoubleArray(size) { get(it) } + } + + @OptIn(PerformancePitfall::class) + override fun Matrix.dot(other: Matrix): Matrix { + require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } + val rows = this@dot.rows.map { it.linearize() } + val columns = other.columns.map { it.linearize() } + val indices = 0 until this.colNum + return buildMatrix(rowNum, other.colNum) { i, j -> + val r = rows[i] + val c = columns[j] + var res = 0.0 + for (l in indices) { + res += r[l] * c[l] + } + res + } + } + + @OptIn(PerformancePitfall::class) + override fun Matrix.dot(vector: Point): Float64Buffer { + require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } + val rows = this@dot.rows.map { it.linearize() } + val indices = 0 until this.colNum + return Float64Buffer(rowNum) { i -> + val r = rows[i] + var res = 0.0 + for (j in indices) { + res += r[j] * vector[j] + } + res + } + + } + + override fun Matrix.times(value: Double): Matrix = Floa64FieldOpsND { + asND().map { it * value }.as2D() + } + + public override fun Point.plus(other: Point): Float64Buffer = Float64BufferOps.run { + this@plus + other + } + + public override fun Point.minus(other: Point): Float64Buffer = Float64BufferOps.run { + this@minus - other + } + + public override fun Point.times(value: Double): Float64Buffer = Float64BufferOps.run { + scale(this@times, value) + } + + public operator fun Point.div(value: Double): Float64Buffer = Float64BufferOps.run { + scale(this@div, 1.0 / value) + } + + public override fun Double.times(v: Point): Float64Buffer = v * this + + +} + +public val Float64LinearSpace.parallel: Float64ParallelLinearSpace get() = Float64ParallelLinearSpace diff --git a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/misc/numbersJVM.kt b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/misc/numbersJVM.kt index 3780ea1ae..222e1bca1 100644 --- a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/misc/numbersJVM.kt +++ b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/misc/numbersJVM.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt index 584748bd7..d1dbca00f 100644 --- a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt @@ -1,10 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.structures.MutableBufferFactory import java.math.BigDecimal import java.math.BigInteger import java.math.MathContext @@ -13,6 +14,8 @@ import java.math.MathContext * A field over [BigInteger]. */ public object JBigIntegerField : Ring, NumericAlgebra { + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: BigInteger get() = BigInteger.ZERO override val one: BigInteger get() = BigInteger.ONE @@ -33,6 +36,7 @@ public object JBigIntegerField : Ring, NumericAlgebra { public abstract class JBigDecimalFieldBase internal constructor( private val mathContext: MathContext = MathContext.DECIMAL64, ) : Field, PowerOperations, NumericAlgebra, ScaleOperations { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override val zero: BigDecimal get() = BigDecimal.ZERO diff --git a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/isInteger.kt b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/isInteger.kt index 9868daddf..fa75860d8 100644 --- a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/isInteger.kt +++ b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/isInteger.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/structures/parallelMutableBuffer.kt b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/structures/parallelMutableBuffer.kt new file mode 100644 index 000000000..7eeb0dbbd --- /dev/null +++ b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/structures/parallelMutableBuffer.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2018-2024 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.attributes.SafeType +import java.util.stream.Collectors +import java.util.stream.IntStream +import kotlin.reflect.typeOf + +/** + * A Java stream-based parallel version of [MutableBuffer]. + * There is no parallelization for [Int8], [Int16] and [Float32] types. + * They are processed sequentially. + */ +@Suppress("UNCHECKED_CAST") +public fun MutableBuffer.Companion.parallel( + type: SafeType, + size: Int, + initializer: (Int) -> T, +): MutableBuffer = when (type.kType) { + typeOf() -> Int8Buffer(size) { initializer(it) as Int8 } as MutableBuffer + typeOf() -> Int16Buffer(size) { initializer(it) as Int16 } as MutableBuffer + typeOf() -> IntStream.range(0, size).parallel().map { initializer(it) as Int32 }.toArray() + .asBuffer() as MutableBuffer + + typeOf() -> IntStream.range(0, size).parallel().mapToLong { initializer(it) as Int64 }.toArray() + .asBuffer() as MutableBuffer + + typeOf() -> Float32Buffer(size) { initializer(it) as Float } as MutableBuffer + typeOf() -> IntStream.range(0, size).parallel().mapToDouble { initializer(it) as Float64 }.toArray() + .asBuffer() as MutableBuffer + //TODO add unsigned types + else -> IntStream.range(0, size).parallel().mapToObj { initializer(it) }.collect(Collectors.toList()).asMutableBuffer() +} + +public class ParallelBufferFactory(override val type: SafeType) : MutableBufferFactory { + override fun invoke(size: Int, builder: (Int) -> T): MutableBuffer { + TODO("Not yet implemented") + } + +} + +/** + * A Java stream-based parallel alternative to [MutableBufferFactory]. + * See [MutableBuffer.Companion.parallel] for details. + */ +public fun MutableBufferFactory.Companion.parallel( + type: SafeType, +): MutableBufferFactory = object : MutableBufferFactory { + + override val type: SafeType = type + + override fun invoke(size: Int, builder: (Int) -> T): MutableBuffer = MutableBuffer.parallel(type, size, builder) + +} \ No newline at end of file diff --git a/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/linear/ParallelMatrixTest.kt b/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/linear/ParallelMatrixTest.kt new file mode 100644 index 000000000..915ec3c5e --- /dev/null +++ b/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/linear/ParallelMatrixTest.kt @@ -0,0 +1,74 @@ +/* + * Copyright 2018-2024 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.PerformancePitfall +import space.kscience.kmath.UnstableKMathAPI +import space.kscience.kmath.nd.StructureND +import space.kscience.kmath.operations.Float64Field +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +@UnstableKMathAPI +@OptIn(PerformancePitfall::class) +@Suppress("UNUSED_VARIABLE") +class ParallelMatrixTest { + + @Test + fun testTranspose() = Float64Field.linearSpace.parallel{ + val matrix = one(3, 3) + val transposed = matrix.transposed() + assertTrue { StructureND.contentEquals(matrix, transposed) } + } + + @Test + fun testBuilder() = Float64Field.linearSpace.parallel{ + val matrix = matrix(2, 3)( + 1.0, 0.0, 0.0, + 0.0, 1.0, 2.0 + ) + + assertEquals(2.0, matrix[1, 2]) + } + + @Test + fun testMatrixExtension() = Float64Field.linearSpace.parallel{ + val transitionMatrix: Matrix = VirtualMatrix(6, 6) { row, col -> + when { + col == 0 -> .50 + row + 1 == col -> .50 + row == 5 && col == 5 -> 1.0 + else -> 0.0 + } + } + + infix fun Matrix.pow(power: Int): Matrix { + var res = this + repeat(power - 1) { + res = res dot this@pow + } + return res + } + + val toTenthPower = transitionMatrix pow 10 + } + + @Test + fun test2DDot() = Float64Field.linearSpace.parallel { + val firstMatrix = buildMatrix(2, 3) { i, j -> (i + j).toDouble() } + val secondMatrix = buildMatrix(3, 2) { i, j -> (i + j).toDouble() } + +// val firstMatrix = produce(2, 3) { i, j -> (i + j).toDouble() } +// val secondMatrix = produce(3, 2) { i, j -> (i + j).toDouble() } + val result = firstMatrix dot secondMatrix + assertEquals(2, result.rowNum) + assertEquals(2, result.colNum) + assertEquals(8.0, result[0, 1]) + assertEquals(8.0, result[1, 0]) + assertEquals(14.0, result[1, 1]) + } +} diff --git a/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/misc/JBigTest.kt b/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/misc/JBigTest.kt index f7f8027e6..f27257a4c 100644 --- a/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/misc/JBigTest.kt +++ b/kmath-core/src/jvmTest/kotlin/space/kscience/kmath/misc/JBigTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/misc/numbers.kt b/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/misc/numbers.kt index e52ea9298..e0875993f 100644 --- a/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/misc/numbers.kt +++ b/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/misc/numbers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/operations/isInteger.kt b/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/operations/isInteger.kt index 9868daddf..fa75860d8 100644 --- a/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/operations/isInteger.kt +++ b/kmath-core/src/nativeMain/kotlin/space/kscience/kmath/operations/isInteger.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/misc/numbers.kt b/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/misc/numbers.kt index e320f350e..e0875993f 100644 --- a/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/misc/numbers.kt +++ b/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/misc/numbers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/operations/isInteger.kt b/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/operations/isInteger.kt index 11c82bf9e..9f68527f7 100644 --- a/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/operations/isInteger.kt +++ b/kmath-core/src/wasmJsMain/kotlin/space/kscience/kmath/operations/isInteger.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/README.md b/kmath-coroutines/README.md index 21831e514..4b7081bb1 100644 --- a/kmath-coroutines/README.md +++ b/kmath-coroutines/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-coroutines:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-coroutines:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-coroutines:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-coroutines:0.4.0-dev-1") + implementation("space.kscience:kmath-coroutines:0.4.0-dev-3") } ``` diff --git a/kmath-coroutines/build.gradle.kts b/kmath-coroutines/build.gradle.kts index 1e901ca98..0cb36e10a 100644 --- a/kmath-coroutines/build.gradle.kts +++ b/kmath-coroutines/build.gradle.kts @@ -6,10 +6,10 @@ kscience { jvm() js() native() + wasm() dependencies { - api(project(":kmath-core")) - api(project(":kmath-complex")) + api(projects.kmathCore) api(spclibs.kotlinx.coroutines.core) } } diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingChain.kt index 2e9a15eed..f843e8af8 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingChain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingChain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -43,7 +43,7 @@ public interface BlockingBufferChain : BlockingChain, BufferChain { public suspend inline fun Chain.nextBuffer(size: Int): Buffer = if (this is BufferChain) { nextBuffer(size) } else { - Buffer.auto(size) { next() } + Buffer(size) { next() } } public inline fun BlockingChain.nextBufferBlocking( @@ -51,5 +51,5 @@ public inline fun BlockingChain.nextBufferBlocking( ): Buffer = if (this is BlockingBufferChain) { nextBufferBlocking(size) } else { - Buffer.auto(size) { nextBlocking() } + Buffer(size) { nextBlocking() } } \ No newline at end of file diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt index e67cab72d..b3e1b59b7 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt index 172aa95cd..82a6d131e 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt index 977346e68..0158c6752 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/Chain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt index 77d4203c5..52114752c 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/coroutines/coroutinesExtra.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/coroutines/coroutinesExtra.kt index 48be93b87..8183d2d15 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/coroutines/coroutinesExtra.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/coroutines/coroutinesExtra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt index c52b6c3d5..12665c8a5 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.flatMapConcat import kotlinx.coroutines.flow.flow import space.kscience.kmath.chains.BlockingDoubleChain +import space.kscience.kmath.operations.Group import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.BufferFactory import space.kscience.kmath.structures.Float64Buffer @@ -84,9 +85,9 @@ public fun Flow.chunked(bufferSize: Int): Flow = flow { * Map a flow to a moving window buffer. The window step is one. * To get different steps, one could use skip operation. */ -public fun Flow.windowed(window: Int): Flow> = flow { +public fun Flow.windowed(window: Int, algebra: Group): Flow> = flow { require(window > 1) { "Window size must be more than one" } - val ringBuffer = RingBuffer.boxing(window) + val ringBuffer = RingBuffer(window, algebra) this@windowed.collect { element -> ringBuffer.push(element) diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt index bb07fede1..ce7f07860 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/RingBuffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,31 +7,34 @@ package space.kscience.kmath.streaming import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.MutableBuffer -import space.kscience.kmath.structures.VirtualBuffer +import space.kscience.kmath.operations.Group +import space.kscience.kmath.structures.* /** * Thread-safe ring buffer */ -@Suppress("UNCHECKED_CAST") public class RingBuffer( - private val buffer: MutableBuffer, + private val buffer: MutableBuffer, + private val bufferFactory: BufferFactory, private var startIndex: Int = 0, size: Int = 0, ) : Buffer { + + private val mutex: Mutex = Mutex() + public val capacity: Int get() = buffer.size + override var size: Int = size private set override operator fun get(index: Int): T { require(index >= 0) { "Index must be positive" } require(index < size) { "Index $index is out of circular buffer size $size" } - return buffer[startIndex.forward(index)] as T + return buffer[startIndex.forward(index)] } - public fun isFull(): Boolean = size == buffer.size + public fun isFull(): Boolean = size == capacity /** * Iterator could provide wrong results if buffer is changed in initialization (iteration is safe) @@ -39,11 +42,11 @@ public class RingBuffer( override operator fun iterator(): Iterator = object : AbstractIterator() { private var count = size private var index = startIndex - val copy = buffer.copy() + val copy = buffer.copy(bufferFactory) override fun computeNext() { if (count == 0) done() else { - setNext(copy[index] as T) + setNext(copy[index]) index = index.forward(1) count-- } @@ -54,10 +57,13 @@ public class RingBuffer( * A safe snapshot operation */ public suspend fun snapshot(): Buffer = mutex.withLock { - val copy = buffer.copy() - VirtualBuffer(size) { i -> copy[startIndex.forward(i)] as T } + val copy = buffer.copy(bufferFactory) + VirtualBuffer(size) { i -> copy[startIndex.forward(i)] } } + /** + * Add an element to the end of the [RingBuffer]. If buffer capacity is reached, the first element is automatically removed. + */ public suspend fun push(element: T) { mutex.withLock { buffer[startIndex.forward(size)] = element @@ -65,22 +71,28 @@ public class RingBuffer( } } - private fun Int.forward(n: Int): Int = (this + n) % (buffer.size) + /** + * Reset buffer to its empty state + */ + public suspend fun reset(): Unit = mutex.withLock { + startIndex = 0 + size = 0 + } + + private fun Int.forward(n: Int): Int = (this + n) % capacity override fun toString(): String = Buffer.toString(this) - - public companion object { - public inline fun build(size: Int, empty: T): RingBuffer { - val buffer = MutableBuffer.auto(size) { empty } as MutableBuffer - return RingBuffer(buffer) - } - - /** - * Slow yet universal buffer - */ - public fun boxing(size: Int): RingBuffer { - val buffer: MutableBuffer = MutableBuffer.boxing(size) { null } - return RingBuffer(buffer) - } - } +} + +public inline fun RingBuffer(size: Int, empty: T): RingBuffer { + val buffer = MutableBuffer(size) { empty } + return RingBuffer(buffer, BufferFactory()) +} + +/** + * Slow yet universal buffer + */ +public fun RingBuffer(size: Int, algebra: Group): RingBuffer { + val buffer: MutableBuffer = MutableBuffer(algebra.type, size) { algebra.zero } + return RingBuffer(buffer, algebra.bufferFactory) } diff --git a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/chains/ChainExt.kt b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/chains/ChainExt.kt index a62bcc6b8..7dc423552 100644 --- a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/chains/ChainExt.kt +++ b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/chains/ChainExt.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt index 22c2ac3ff..1d23162f5 100644 --- a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt +++ b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -17,6 +17,7 @@ public class LazyStructureND( override val shape: ShapeND, public val function: suspend (IntArray) -> T, ) : StructureND { + private val cache: MutableMap> = HashMap() public fun async(index: IntArray): Deferred = cache.getOrPut(index) { @@ -47,13 +48,13 @@ public suspend fun StructureND.await(index: IntArray): T = * PENDING would benefit from KEEP-176 */ @OptIn(PerformancePitfall::class) -public inline fun StructureND.mapAsyncIndexed( +public inline fun StructureND.mapAsyncIndexed( scope: CoroutineScope, crossinline function: suspend (T, index: IntArray) -> R, ): LazyStructureND = LazyStructureND(scope, shape) { index -> function(get(index), index) } @OptIn(PerformancePitfall::class) -public inline fun StructureND.mapAsync( +public inline fun StructureND.mapAsync( scope: CoroutineScope, crossinline function: suspend (T) -> R, -): LazyStructureND = LazyStructureND(scope, shape) { index -> function(get(index)) } +): LazyStructureND = LazyStructureND(scope, shape) { index -> function(get(index)) } diff --git a/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/BufferFlowTest.kt b/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/BufferFlowTest.kt index c448168e3..22893389d 100644 --- a/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/BufferFlowTest.kt +++ b/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/BufferFlowTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/RingBufferTest.kt b/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/RingBufferTest.kt index a6d7f006c..701715123 100644 --- a/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/RingBufferTest.kt +++ b/kmath-coroutines/src/jvmTest/kotlin/space/kscience/kmath/streaming/RingBufferTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,7 @@ package space.kscience.kmath.streaming import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.asSequence import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +15,7 @@ import kotlin.test.assertEquals internal class RingBufferTest { @Test fun push() { - val buffer = RingBuffer.build(20, Double.NaN) + val buffer = RingBuffer(20, Double.NaN) runBlocking { for (i in 1..30) { buffer.push(i.toDouble()) @@ -30,7 +31,7 @@ internal class RingBufferTest { while (true) emit(i++) } - val windowed = flow.windowed(10) + val windowed = flow.windowed(10, Int32Ring) runBlocking { @Suppress("UNUSED_VARIABLE") val first = windowed.take(1).single() diff --git a/kmath-dimensions/README.md b/kmath-dimensions/README.md index 2e7250b51..c1c5e0dc2 100644 --- a/kmath-dimensions/README.md +++ b/kmath-dimensions/README.md @@ -6,19 +6,8 @@ A proof of concept module for adding type-safe dimensions to structures ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-dimensions:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-dimensions:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-dimensions:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-dimensions:0.4.0-dev-1") + implementation("space.kscience:kmath-dimensions:0.4.0-dev-3") } ``` diff --git a/kmath-dimensions/build.gradle.kts b/kmath-dimensions/build.gradle.kts index be1fc65a0..0ed6a8949 100644 --- a/kmath-dimensions/build.gradle.kts +++ b/kmath-dimensions/build.gradle.kts @@ -6,6 +6,7 @@ kscience{ jvm() js() native() + wasm() dependencies{ api(projects.kmathCore) diff --git a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt index 14677319c..afdb8eec1 100644 --- a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt +++ b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt index 6cade1d5d..7ddc143ad 100644 --- a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt +++ b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -48,6 +48,7 @@ public interface DMatrix : Structure2D { public value class DMatrixWrapper( private val structure: Structure2D, ) : DMatrix { + override val shape: ShapeND get() = structure.shape override val rowNum: Int get() = shape[0] override val colNum: Int get() = shape[1] @@ -75,8 +76,8 @@ public interface DPoint : Point { * Dimension-safe point wrapper */ @JvmInline -public value class DPointWrapper(public val point: Point) : - DPoint { +public value class DPointWrapper(public val point: Point) : DPoint { + override val size: Int get() = point.size override operator fun get(index: Int): T = point[index] @@ -146,8 +147,8 @@ public value class DMatrixContext>(public val context: public inline operator fun DMatrix.unaryMinus(): DMatrix = context.run { this@unaryMinus.unaryMinus() }.coerce() - public inline fun DMatrix.transpose(): DMatrix = - context.run { (this@transpose as Matrix).transpose() }.coerce() + public inline fun DMatrix.transposed(): DMatrix = + context.run { (this@transposed as Matrix).transposed() }.coerce() public companion object { public val real: DMatrixContext = DMatrixContext(Double.algebra.linearSpace) diff --git a/kmath-dimensions/src/commonTest/kotlin/space/kscience/dimensions/DMatrixContextTest.kt b/kmath-dimensions/src/commonTest/kotlin/space/kscience/dimensions/DMatrixContextTest.kt index e2793855b..b236f6bf9 100644 --- a/kmath-dimensions/src/commonTest/kotlin/space/kscience/dimensions/DMatrixContextTest.kt +++ b/kmath-dimensions/src/commonTest/kotlin/space/kscience/dimensions/DMatrixContextTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -30,7 +30,7 @@ internal class DMatrixContextTest { val m2 = produce { i, j -> (i + j).toDouble() } //Dimension-safe addition - m1.transpose() + m2 + m1.transposed() + m2 } } } diff --git a/kmath-dimensions/src/jsMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt b/kmath-dimensions/src/jsMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt index 1ae484228..cbf404b7f 100644 --- a/kmath-dimensions/src/jsMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt +++ b/kmath-dimensions/src/jsMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-dimensions/src/jvmMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt b/kmath-dimensions/src/jvmMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt index 24cfb14e8..131a6db42 100644 --- a/kmath-dimensions/src/jvmMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt +++ b/kmath-dimensions/src/jvmMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-dimensions/src/nativeMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt b/kmath-dimensions/src/nativeMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt index f5f749c8a..ddfee4523 100644 --- a/kmath-dimensions/src/nativeMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt +++ b/kmath-dimensions/src/nativeMain/kotlin/space/kscience/kmath/dimensions/Dimension.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-dimensions/src/wasmJsMain/kotlin/space/kscience/kmath/dimensions/Dimension.wasmJs.kt b/kmath-dimensions/src/wasmJsMain/kotlin/space/kscience/kmath/dimensions/Dimension.wasmJs.kt new file mode 100644 index 000000000..cbf404b7f --- /dev/null +++ b/kmath-dimensions/src/wasmJsMain/kotlin/space/kscience/kmath/dimensions/Dimension.wasmJs.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2018-2024 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.dimensions + +import kotlin.reflect.KClass + +private val dimensionMap: MutableMap = hashMapOf(1 to D1, 2 to D2, 3 to D3) + +@Suppress("UNCHECKED_CAST") +public actual fun Dimension.Companion.resolve(type: KClass): D = dimensionMap + .entries + .map(MutableMap.MutableEntry::value) + .find { it::class == type } as? D + ?: error("Can't resolve dimension $type") + +public actual fun Dimension.Companion.of(dim: Int): Dimension = dimensionMap.getOrPut(dim) { + object : Dimension { + override val dim: Int get() = dim + } +} diff --git a/kmath-ejml/README.md b/kmath-ejml/README.md index ad80ba183..dcde8efd3 100644 --- a/kmath-ejml/README.md +++ b/kmath-ejml/README.md @@ -9,19 +9,8 @@ EJML based linear algebra implementation. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-ejml:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-ejml:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-ejml:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -30,6 +19,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-ejml:0.4.0-dev-1") + implementation("space.kscience:kmath-ejml:0.4.0-dev-3") } ``` diff --git a/kmath-ejml/build.gradle.kts b/kmath-ejml/build.gradle.kts index d7f780d79..54b8f7038 100644 --- a/kmath-ejml/build.gradle.kts +++ b/kmath-ejml/build.gradle.kts @@ -1,15 +1,15 @@ -import space.kscience.kmath.ejml.codegen.ejmlCodegen - plugins { id("space.kscience.gradle.jvm") } +val ejmlVerision = "0.43.1" + dependencies { - api("org.ejml:ejml-ddense:0.41") - api("org.ejml:ejml-fdense:0.41") - api("org.ejml:ejml-dsparse:0.41") - api("org.ejml:ejml-fsparse:0.41") - api(project(":kmath-core")) + api("org.ejml:ejml-ddense:$ejmlVerision") + api("org.ejml:ejml-fdense:$ejmlVerision") + api("org.ejml:ejml-dsparse:$ejmlVerision") + api("org.ejml:ejml-fsparse:$ejmlVerision") + api(projects.kmathCore) } readme { @@ -32,10 +32,10 @@ readme { ) { "LinearSpace implementations." } } -kotlin.sourceSets.main { - val codegen by tasks.creating { - ejmlCodegen(kotlin.srcDirs.first().absolutePath + "/space/kscience/kmath/ejml/_generated.kt") - } - - kotlin.srcDirs(files().builtBy(codegen)) -} +//kotlin.sourceSets.main { +// val codegen by tasks.creating { +// ejmlCodegen(kotlin.srcDirs.first().absolutePath + "/space/kscience/kmath/ejml/_generated.kt") +// } +// +// kotlin.srcDirs(files().builtBy(codegen)) +//} diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt index 8925fb045..d5bd8ccb7 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt @@ -1,16 +1,13 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.ejml import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.linear.InverseMatrixFeature -import space.kscience.kmath.linear.LinearSpace -import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.linear.Point -import space.kscience.kmath.nd.Structure2D +import space.kscience.kmath.linear.* +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.Ring /** @@ -40,8 +37,7 @@ public abstract class EjmlLinearSpace, out M : org.ejml public abstract override fun buildVector(size: Int, initializer: A.(Int) -> T): EjmlVector - @Suppress("UNCHECKED_CAST") @UnstableKMathAPI - public fun EjmlMatrix.inverse(): Structure2D = - computeFeature(this, InverseMatrixFeature::class)?.inverse as Structure2D + public fun EjmlMatrix.inverted(): Matrix = + computeAttribute(this, Float64Field.linearSpace.Inverted)!! } diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt index 1d70c0e7d..d24f56042 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt index c4fae9951..82401ba05 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlVector.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/implementations.kt similarity index 72% rename from kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt rename to kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/implementations.kt index f48ab4c19..d196f5f1a 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/implementations.kt @@ -1,6 +1,6 @@ /* - * 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 file. + * Copyright 2018-2024 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. */ /* This file is generated with buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt */ @@ -17,19 +17,18 @@ import org.ejml.sparse.csc.CommonOps_DSCC import org.ejml.sparse.csc.CommonOps_FSCC import org.ejml.sparse.csc.factory.DecompositionFactory_DSCC import org.ejml.sparse.csc.factory.DecompositionFactory_FSCC -import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC -import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC -import space.kscience.kmath.UnstableKMathAPI +import space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.linear.* import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.nd.StructureFeature +import space.kscience.kmath.nd.Structure2D +import space.kscience.kmath.nd.StructureAttribute import space.kscience.kmath.operations.Float32Field import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.DoubleBuffer -import space.kscience.kmath.structures.FloatBuffer -import kotlin.reflect.KClass -import kotlin.reflect.cast +import space.kscience.kmath.structures.Float32 +import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.asBuffer /** * [EjmlVector] specialization for [Double]. @@ -39,6 +38,7 @@ public class EjmlDoubleVector(override val origin: M) : EjmlVec require(origin.numRows == 1) { "The origin matrix must have only one row to form a vector" } } + override operator fun get(index: Int): Double = origin[0, index] } @@ -64,9 +64,11 @@ public class EjmlDoubleMatrix(override val origin: M) : EjmlMat * [EjmlMatrix] specialization for [Float]. */ public class EjmlFloatMatrix(override val origin: M) : EjmlMatrix(origin) { + override operator fun get(i: Int, j: Int): Float = origin[i, j] } + /** * [EjmlLinearSpace] implementation based on [CommonOps_DDRM], [DecompositionFactory_DDRM] operations and * [DMatrixRMaj] matrices. @@ -77,6 +79,8 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace get() = safeTypeOf() + @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlDoubleMatrix = when { this is EjmlDoubleMatrix<*> && origin is DMatrixRMaj -> this as EjmlDoubleMatrix @@ -129,10 +133,10 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace.plus(other: Matrix): EjmlDoubleMatrix { val out = DMatrixRMaj(1, 1) - + CommonOps_DDRM.add( elementAlgebra.one, toEjml().origin, elementAlgebra.one, - other.toEjml().origin, + other.toEjml().origin, out, ) @@ -203,77 +207,65 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace): EjmlDoubleVector = v * this - @UnstableKMathAPI - override fun computeFeature(structure: Matrix, type: KClass): F? { - structure.getFeature(type)?.let { return it } + override fun > computeAttribute(structure: Structure2D, attribute: A): V? { val origin = structure.toEjml().origin - return when (type) { - InverseMatrixFeature::class -> object : InverseMatrixFeature { - override val inverse: Matrix by lazy { - val res = origin.copy() - CommonOps_DDRM.invert(res) - res.wrapMatrix() - } + val raw: Any? = when (attribute) { + Inverted -> { + val res = origin.copy() + CommonOps_DDRM.invert(res) + res.wrapMatrix() } - DeterminantFeature::class -> object : DeterminantFeature { - override val determinant: Double by lazy { CommonOps_DDRM.det(origin) } - } - - SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature { - private val svd by lazy { - DecompositionFactory_DDRM.svd(origin.numRows, origin.numCols, true, true, false) + Determinant -> CommonOps_DDRM.det(origin) + SVD -> object : SingularValueDecomposition { + val ejmlSvd by lazy { + DecompositionFactory_DDRM + .svd(origin.numRows, origin.numCols, true, true, false) .apply { decompose(origin.copy()) } } + override val u: Matrix get() = ejmlSvd.getU(null, false).wrapMatrix() + + override val s: Matrix get() = ejmlSvd.getW(null).wrapMatrix() + override val v: Matrix get() = ejmlSvd.getV(null, false).wrapMatrix() + override val singularValues: Point get() = ejmlSvd.singularValues.asBuffer() - override val u: Matrix by lazy { svd.getU(null, false).wrapMatrix() } - override val s: Matrix by lazy { svd.getW(null).wrapMatrix() } - override val v: Matrix by lazy { svd.getV(null, false).wrapMatrix() } - override val singularValues: Point by lazy { DoubleBuffer(svd.singularValues) } } - QRDecompositionFeature::class -> object : QRDecompositionFeature { - private val qr by lazy { - DecompositionFactory_DDRM.qr().apply { decompose(origin.copy()) } - } - - override val q: Matrix by lazy { - qr.getQ(null, false).wrapMatrix().withFeature(OrthogonalFeature) - } - - override val r: Matrix by lazy { qr.getR(null, false).wrapMatrix().withFeature(UFeature) } + QR -> object : QRDecomposition { + val ejmlQr by lazy { DecompositionFactory_DDRM.qr().apply { decompose(origin.copy()) } } + override val q: Matrix get() = ejmlQr.getQ(null, false).wrapMatrix() + override val r: Matrix get() = ejmlQr.getR(null, false).wrapMatrix() } - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { + Cholesky -> object : CholeskyDecomposition { override val l: Matrix by lazy { val cholesky = DecompositionFactory_DDRM.chol(structure.rowNum, true).apply { decompose(origin.copy()) } - cholesky.getT(null).wrapMatrix().withFeature(LFeature) + cholesky.getT(null).wrapMatrix().withAttribute(LowerTriangular) } } - LupDecompositionFeature::class -> object : LupDecompositionFeature { + LUP -> object : LupDecomposition { private val lup by lazy { DecompositionFactory_DDRM.lu(origin.numRows, origin.numCols).apply { decompose(origin.copy()) } } - override val l: Matrix by lazy { - lup.getLower(null).wrapMatrix().withFeature(LFeature) - } + override val l: Matrix + get() = lup.getLower(null).wrapMatrix().withAttribute(LowerTriangular) - override val u: Matrix by lazy { - lup.getUpper(null).wrapMatrix().withFeature(UFeature) - } - override val p: Matrix by lazy { lup.getRowPivot(null).wrapMatrix() } + override val u: Matrix + get() = lup.getUpper(null).wrapMatrix().withAttribute(UpperTriangular) + override val pivot: IntBuffer get() = lup.getRowPivotV(null).asBuffer() } else -> null - }?.let{ - type.cast(it) } + + @Suppress("UNCHECKED_CAST") + return raw as V? } /** @@ -303,6 +295,7 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace get() = safeTypeOf() + @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlFloatMatrix = when { this is EjmlFloatMatrix<*> && origin is FMatrixRMaj -> this as EjmlFloatMatrix @@ -365,10 +360,10 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace.plus(other: Matrix): EjmlFloatMatrix { val out = FMatrixRMaj(1, 1) - + CommonOps_FDRM.add( elementAlgebra.one, toEjml().origin, elementAlgebra.one, - other.toEjml().origin, + other.toEjml().origin, out, ) @@ -439,77 +434,65 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace): EjmlFloatVector = v * this - @UnstableKMathAPI - override fun computeFeature(structure: Matrix, type: KClass): F? { - structure.getFeature(type)?.let { return it } + override fun > computeAttribute(structure: Structure2D, attribute: A): V? { val origin = structure.toEjml().origin - return when (type) { - InverseMatrixFeature::class -> object : InverseMatrixFeature { - override val inverse: Matrix by lazy { - val res = origin.copy() - CommonOps_FDRM.invert(res) - res.wrapMatrix() - } + val raw: Any? = when (attribute) { + Inverted -> { + val res = origin.copy() + CommonOps_FDRM.invert(res) + res.wrapMatrix() } - DeterminantFeature::class -> object : DeterminantFeature { - override val determinant: Float by lazy { CommonOps_FDRM.det(origin) } - } - - SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature { - private val svd by lazy { - DecompositionFactory_FDRM.svd(origin.numRows, origin.numCols, true, true, false) + Determinant -> CommonOps_FDRM.det(origin) + SVD -> object : SingularValueDecomposition { + val ejmlSvd by lazy { + DecompositionFactory_FDRM + .svd(origin.numRows, origin.numCols, true, true, false) .apply { decompose(origin.copy()) } } + override val u: Matrix get() = ejmlSvd.getU(null, false).wrapMatrix() + + override val s: Matrix get() = ejmlSvd.getW(null).wrapMatrix() + override val v: Matrix get() = ejmlSvd.getV(null, false).wrapMatrix() + override val singularValues: Point get() = ejmlSvd.singularValues.asBuffer() - override val u: Matrix by lazy { svd.getU(null, false).wrapMatrix() } - override val s: Matrix by lazy { svd.getW(null).wrapMatrix() } - override val v: Matrix by lazy { svd.getV(null, false).wrapMatrix() } - override val singularValues: Point by lazy { FloatBuffer(svd.singularValues) } } - QRDecompositionFeature::class -> object : QRDecompositionFeature { - private val qr by lazy { - DecompositionFactory_FDRM.qr().apply { decompose(origin.copy()) } - } - - override val q: Matrix by lazy { - qr.getQ(null, false).wrapMatrix().withFeature(OrthogonalFeature) - } - - override val r: Matrix by lazy { qr.getR(null, false).wrapMatrix().withFeature(UFeature) } + QR -> object : QRDecomposition { + val ejmlQr by lazy { DecompositionFactory_FDRM.qr().apply { decompose(origin.copy()) } } + override val q: Matrix get() = ejmlQr.getQ(null, false).wrapMatrix() + override val r: Matrix get() = ejmlQr.getR(null, false).wrapMatrix() } - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { - override val l: Matrix by lazy { + Cholesky -> object : CholeskyDecomposition { + override val l: Matrix by lazy { val cholesky = DecompositionFactory_FDRM.chol(structure.rowNum, true).apply { decompose(origin.copy()) } - cholesky.getT(null).wrapMatrix().withFeature(LFeature) + cholesky.getT(null).wrapMatrix().withAttribute(LowerTriangular) } } - LupDecompositionFeature::class -> object : LupDecompositionFeature { + LUP -> object : LupDecomposition { private val lup by lazy { DecompositionFactory_FDRM.lu(origin.numRows, origin.numCols).apply { decompose(origin.copy()) } } - override val l: Matrix by lazy { - lup.getLower(null).wrapMatrix().withFeature(LFeature) - } + override val l: Matrix + get() = lup.getLower(null).wrapMatrix().withAttribute(LowerTriangular) - override val u: Matrix by lazy { - lup.getUpper(null).wrapMatrix().withFeature(UFeature) - } - override val p: Matrix by lazy { lup.getRowPivot(null).wrapMatrix() } + override val u: Matrix + get() = lup.getUpper(null).wrapMatrix().withAttribute(UpperTriangular) + override val pivot: IntBuffer get() = lup.getRowPivotV(null).asBuffer() } else -> null - }?.let{ - type.cast(it) } + + @Suppress("UNCHECKED_CAST") + return raw as V? } /** @@ -539,6 +522,7 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace get() = safeTypeOf() + @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlDoubleMatrix = when { this is EjmlDoubleMatrix<*> && origin is DMatrixSparseCSC -> this as EjmlDoubleMatrix @@ -601,12 +587,12 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace.plus(other: Matrix): EjmlDoubleMatrix { val out = DMatrixSparseCSC(1, 1) - + CommonOps_DSCC.add( elementAlgebra.one, toEjml().origin, elementAlgebra.one, - other.toEjml().origin, + other.toEjml().origin, out, - null, + null, null, ) @@ -650,7 +636,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace): EjmlDoubleVector = v * this - @UnstableKMathAPI - override fun computeFeature(structure: Matrix, type: KClass): F? { - structure.getFeature(type)?.let { return it } + override fun > computeAttribute(structure: Structure2D, attribute: A): V? { val origin = structure.toEjml().origin - return when (type) { - QRDecompositionFeature::class -> object : QRDecompositionFeature { - private val qr by lazy { - DecompositionFactory_DSCC.qr(FillReducing.NONE).apply { decompose(origin.copy()) } - } - - override val q: Matrix by lazy { - qr.getQ(null, false).wrapMatrix().withFeature(OrthogonalFeature) - } - - override val r: Matrix by lazy { qr.getR(null, false).wrapMatrix().withFeature(UFeature) } + val raw: Any? = when (attribute) { + Inverted -> { + val res = DMatrixRMaj(origin.numRows,origin.numCols) + CommonOps_DSCC.invert(origin,res) + res.wrapMatrix() } - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { + Determinant -> CommonOps_DSCC.det(origin) + + QR -> object : QRDecomposition { + val ejmlQr by lazy { DecompositionFactory_DSCC.qr(FillReducing.NONE).apply { decompose(origin.copy()) } } + override val q: Matrix get() = ejmlQr.getQ(null, false).wrapMatrix() + override val r: Matrix get() = ejmlQr.getR(null, false).wrapMatrix() + } + + Cholesky -> object : CholeskyDecomposition { override val l: Matrix by lazy { val cholesky = DecompositionFactory_DSCC.cholesky().apply { decompose(origin.copy()) } - (cholesky.getT(null) as DMatrix).wrapMatrix().withFeature(LFeature) + (cholesky.getT(null) as DMatrix).wrapMatrix().withAttribute(LowerTriangular) } } - LUDecompositionFeature::class, DeterminantFeature::class, InverseMatrixFeature::class -> object : - LUDecompositionFeature, DeterminantFeature, InverseMatrixFeature { - private val lu by lazy { + LUP -> object : LupDecomposition { + private val lup by lazy { DecompositionFactory_DSCC.lu(FillReducing.NONE).apply { decompose(origin.copy()) } } - override val l: Matrix by lazy { - lu.getLower(null).wrapMatrix().withFeature(LFeature) - } + override val l: Matrix + get() = lup.getLower(null).wrapMatrix().withAttribute(LowerTriangular) - override val u: Matrix by lazy { - lu.getUpper(null).wrapMatrix().withFeature(UFeature) - } - override val inverse: Matrix by lazy { - var a = origin - val inverse = DMatrixRMaj(1, 1) - val solver = LinearSolverFactory_DSCC.lu(FillReducing.NONE) - if (solver.modifiesA()) a = a.copy() - val i = CommonOps_DDRM.identity(a.numRows) - solver.solve(i, inverse) - inverse.wrapMatrix() - } - - override val determinant: Double by lazy { elementAlgebra.number(lu.computeDeterminant().real) } + override val u: Matrix + get() = lup.getUpper(null).wrapMatrix().withAttribute(UpperTriangular) + override val pivot: IntBuffer get() = lup.getRowPivotV(null).asBuffer() } else -> null - }?.let{ - type.cast(it) } + + @Suppress("UNCHECKED_CAST") + return raw as V? } /** @@ -770,6 +744,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace get() = safeTypeOf() + @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlFloatMatrix = when { this is EjmlFloatMatrix<*> && origin is FMatrixSparseCSC -> this as EjmlFloatMatrix @@ -832,12 +809,12 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace.plus(other: Matrix): EjmlFloatMatrix { val out = FMatrixSparseCSC(1, 1) - + CommonOps_FSCC.add( elementAlgebra.one, toEjml().origin, elementAlgebra.one, - other.toEjml().origin, + other.toEjml().origin, out, - null, + null, null, ) @@ -881,7 +858,7 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace): EjmlFloatVector = v * this - - @UnstableKMathAPI - override fun computeFeature(structure: Matrix, type: KClass): F? { - structure.getFeature(type)?.let { return it } + override fun > computeAttribute(structure: Structure2D, attribute: A): V? { val origin = structure.toEjml().origin - return when (type) { - QRDecompositionFeature::class -> object : QRDecompositionFeature { - private val qr by lazy { - DecompositionFactory_FSCC.qr(FillReducing.NONE).apply { decompose(origin.copy()) } - } - - override val q: Matrix by lazy { - qr.getQ(null, false).wrapMatrix().withFeature(OrthogonalFeature) - } - - override val r: Matrix by lazy { qr.getR(null, false).wrapMatrix().withFeature(UFeature) } + val raw: Any? = when (attribute) { + Inverted -> { + val res = FMatrixRMaj(origin.numRows,origin.numCols) + CommonOps_FSCC.invert(origin,res) + res.wrapMatrix() } - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { - override val l: Matrix by lazy { + Determinant -> CommonOps_FSCC.det(origin) + + QR -> object : QRDecomposition { + val ejmlQr by lazy { DecompositionFactory_FSCC.qr(FillReducing.NONE).apply { decompose(origin.copy()) } } + override val q: Matrix get() = ejmlQr.getQ(null, false).wrapMatrix() + override val r: Matrix get() = ejmlQr.getR(null, false).wrapMatrix() + } + + Cholesky -> object : CholeskyDecomposition { + override val l: Matrix by lazy { val cholesky = DecompositionFactory_FSCC.cholesky().apply { decompose(origin.copy()) } - (cholesky.getT(null) as FMatrix).wrapMatrix().withFeature(LFeature) + (cholesky.getT(null) as FMatrix).wrapMatrix().withAttribute(LowerTriangular) } } - LUDecompositionFeature::class, DeterminantFeature::class, InverseMatrixFeature::class -> object : - LUDecompositionFeature, DeterminantFeature, InverseMatrixFeature { - private val lu by lazy { + LUP -> object : LupDecomposition { + private val lup by lazy { DecompositionFactory_FSCC.lu(FillReducing.NONE).apply { decompose(origin.copy()) } } - override val l: Matrix by lazy { - lu.getLower(null).wrapMatrix().withFeature(LFeature) - } + override val l: Matrix + get() = lup.getLower(null).wrapMatrix().withAttribute(LowerTriangular) - override val u: Matrix by lazy { - lu.getUpper(null).wrapMatrix().withFeature(UFeature) - } - override val inverse: Matrix by lazy { - var a = origin - val inverse = FMatrixRMaj(1, 1) - val solver = LinearSolverFactory_FSCC.lu(FillReducing.NONE) - if (solver.modifiesA()) a = a.copy() - val i = CommonOps_FDRM.identity(a.numRows) - solver.solve(i, inverse) - inverse.wrapMatrix() - } - - override val determinant: Float by lazy { elementAlgebra.number(lu.computeDeterminant().real) } + override val u: Matrix + get() = lup.getUpper(null).wrapMatrix().withAttribute(UpperTriangular) + override val pivot: IntBuffer get() = lup.getRowPivotV(null).asBuffer() } else -> null - }?.let{ - type.cast(it) } + + @Suppress("UNCHECKED_CAST") + return raw as V? } /** diff --git a/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt b/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt index e89810e0d..8b1b28e7d 100644 --- a/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt +++ b/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -58,19 +58,19 @@ internal class EjmlMatrixTest { @OptIn(UnstableKMathAPI::class) @Test - fun features() { + fun features() = EjmlLinearSpaceDDRM { val m = randomMatrix val w = EjmlDoubleMatrix(m) - val det: DeterminantFeature = EjmlLinearSpaceDDRM.computeFeature(w) ?: fail() - assertEquals(CommonOps_DDRM.det(m), det.determinant) - val lup: LupDecompositionFeature = EjmlLinearSpaceDDRM.computeFeature(w) ?: fail() + val det: Double = w.getOrComputeAttribute(Determinant) ?: fail() + assertEquals(CommonOps_DDRM.det(m), det) + val lup: LupDecomposition = w.getOrComputeAttribute(LUP) ?: fail() val ludecompositionF64 = DecompositionFactory_DDRM.lu(m.numRows, m.numCols) .also { it.decompose(m.copy()) } assertMatrixEquals(EjmlDoubleMatrix(ludecompositionF64.getLower(null)), lup.l) assertMatrixEquals(EjmlDoubleMatrix(ludecompositionF64.getUpper(null)), lup.u) - assertMatrixEquals(EjmlDoubleMatrix(ludecompositionF64.getRowPivot(null)), lup.p) + assertMatrixEquals(EjmlDoubleMatrix(ludecompositionF64.getRowPivot(null)), lup.pivotMatrix(this)) } @Test @@ -96,7 +96,7 @@ internal class EjmlMatrixTest { val u = space.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } val l = space.buildMatrix(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 } val matrix = space { l dot u } - val inverted = matrix.toEjml().inverse() + val inverted = matrix.toEjml().inverted() val res = matrix dot inverted diff --git a/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlVectorTest.kt b/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlVectorTest.kt index 7d3ea314b..cbc7f7c41 100644 --- a/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlVectorTest.kt +++ b/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlVectorTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-for-real/README.md b/kmath-for-real/README.md index 638b15bfa..2c880f4f9 100644 --- a/kmath-for-real/README.md +++ b/kmath-for-real/README.md @@ -9,19 +9,8 @@ Specialization of KMath APIs for Double numbers. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-for-real:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-for-real:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-for-real:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -30,6 +19,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-for-real:0.4.0-dev-1") + implementation("space.kscience:kmath-for-real:0.4.0-dev-3") } ``` diff --git a/kmath-for-real/build.gradle.kts b/kmath-for-real/build.gradle.kts index 99ce5903f..a0426b516 100644 --- a/kmath-for-real/build.gradle.kts +++ b/kmath-for-real/build.gradle.kts @@ -6,6 +6,7 @@ kscience { jvm() js() native() + wasm() dependencies { api(projects.kmathCore) diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt index d13528636..493a299c0 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt index fe5593eaa..d36537d71 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -48,7 +48,7 @@ public fun Sequence.toMatrix(): RealMatrix = toList().let { } public fun RealMatrix.repeatStackVertical(n: Int): RealMatrix = - VirtualMatrix(rowNum * n, colNum) { row, col -> + VirtualMatrix( rowNum * n, colNum) { row, col -> get(if (row == 0) 0 else row % rowNum, col) } diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt index 0c18602f1..e9fdfb506 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt index 79af446e2..f1b099cde 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt index 1b9f0f4e2..223cdd25d 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleMatrixTest.kt b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleMatrixTest.kt index c00cd84d1..f54ec6290 100644 --- a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleMatrixTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleMatrixTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt index 16eccd79b..d4051c8d9 100644 --- a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,7 +7,7 @@ package space.kscience.kmath.real import space.kscience.kmath.linear.asMatrix import space.kscience.kmath.linear.linearSpace -import space.kscience.kmath.linear.transpose +import space.kscience.kmath.linear.transposed import space.kscience.kmath.operations.algebra import space.kscience.kmath.structures.Float64Buffer import kotlin.test.Test @@ -34,7 +34,7 @@ internal class DoubleVectorTest { val vector1 = Float64Buffer(5) { it.toDouble() } val vector2 = Float64Buffer(5) { 5 - it.toDouble() } val matrix1 = vector1.asMatrix() - val matrix2 = vector2.asMatrix().transpose() + val matrix2 = vector2.asMatrix().transposed() val product = matrix1 dot matrix2 assertEquals(5.0, product[1, 0]) assertEquals(6.0, product[2, 2]) diff --git a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/GridTest.kt b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/GridTest.kt index 35c53f9d6..861995a19 100644 --- a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/GridTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/GridTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/README.md b/kmath-functions/README.md index 929fd9172..1ee4149bd 100644 --- a/kmath-functions/README.md +++ b/kmath-functions/README.md @@ -11,19 +11,8 @@ Functions and interpolations. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-functions:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-functions:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-functions:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -32,6 +21,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-functions:0.4.0-dev-1") + implementation("space.kscience:kmath-functions:0.4.0-dev-3") } ``` diff --git a/kmath-functions/build.gradle.kts b/kmath-functions/build.gradle.kts index 4ec52f5ee..9296aaaf0 100644 --- a/kmath-functions/build.gradle.kts +++ b/kmath-functions/build.gradle.kts @@ -6,11 +6,8 @@ kscience{ jvm() js() native() - wasm() - - dependencies { api(projects.kmathCore) } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt index a9e75e456..8c02ee044 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Piecewise.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt index af84f47f2..35a947700 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/Polynomial.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -10,6 +10,7 @@ package space.kscience.kmath.functions import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.invoke +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.max import kotlin.math.min @@ -48,7 +49,7 @@ public data class Polynomial( * * @usesMathJax */ - public val coefficients: List + public val coefficients: List, ) { override fun toString(): String = "Polynomial$coefficients" } @@ -62,16 +63,15 @@ public data class Polynomial( * @param ring underlying ring of constants of type [A]. */ public open class PolynomialSpace( - /** - * Underlying ring of constants. Its operations on constants are used by local operations on constants and polynomials. - */ public val ring: A, ) : Ring>, ScaleOperations> where A : Ring, A : ScaleOperations { + override val bufferFactory: MutableBufferFactory> get() = MutableBufferFactory() /** * Instance of zero constant (zero of the underlying ring). */ public val constantZero: C get() = ring.zero + /** * Instance of unit constant (unit of the underlying ring). */ @@ -95,6 +95,7 @@ public open class PolynomialSpace( ) } } + /** * Returns difference between the constant represented as a polynomial and the polynomial. */ @@ -115,6 +116,7 @@ public open class PolynomialSpace( ) } } + /** * Returns product of the constant represented as a polynomial and the polynomial. */ @@ -147,6 +149,7 @@ public open class PolynomialSpace( ) } } + /** * Returns difference between the constant represented as a polynomial and the polynomial. */ @@ -165,6 +168,7 @@ public open class PolynomialSpace( ) } } + /** * Returns product of the constant represented as a polynomial and the polynomial. */ @@ -183,6 +187,7 @@ public open class PolynomialSpace( * Converts the constant [value] to polynomial. */ public fun number(value: C): Polynomial = Polynomial(listOf(value)) + /** * Converts the constant to polynomial. */ @@ -194,6 +199,7 @@ public open class PolynomialSpace( public override operator fun Polynomial.unaryMinus(): Polynomial = ring { Polynomial(coefficients.map { -it }) } + /** * Returns sum of the polynomials. */ @@ -210,6 +216,7 @@ public open class PolynomialSpace( } ) } + /** * Returns difference of the polynomials. */ @@ -226,6 +233,7 @@ public open class PolynomialSpace( } ) } + /** * Returns product of the polynomials. */ @@ -245,6 +253,7 @@ public open class PolynomialSpace( * Instance of zero polynomial (zero of the polynomial ring). */ override val zero: Polynomial = Polynomial(emptyList()) + /** * Instance of unit polynomial (unit of the polynomial ring). */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/functionTypes.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/functionTypes.kt index c2f95f040..6f2ba6c75 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/functionTypes.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/functionTypes.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialConstructors.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialConstructors.kt index e07ff764c..dceef2b75 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialConstructors.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialConstructors.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialUtil.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialUtil.kt index 0d4b93f03..31e2406fa 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialUtil.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/polynomialUtil.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt index f2ac0a296..e2f5c77ba 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegrator.kt @@ -1,9 +1,10 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.integration +import space.kscience.attributes.AttributesBuilder import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.Field import space.kscience.kmath.structures.Buffer @@ -11,14 +12,14 @@ import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.indices /** - * A simple one-pass integrator based on Gauss rule - * Following integrand features are accepted: + * A simple one-pass integrator based on Gauss rule. + * The following integrand features are accepted: * * * [GaussIntegratorRuleFactory]—a factory for computing the Gauss integration rule. By default, uses * [GaussLegendreRuleFactory]. * * [IntegrationRange]—the univariate range of integration. By default, uses `0..1` interval. * * [IntegrandMaxCalls]—the maximum number of function calls during integration. For non-iterative rules, always - * uses the maximum number of points. By default, uses 10 points. + * use the maximum number of points. By default, uses 10 points. * * [UnivariateIntegrandRanges]—set of ranges and number of points per range. Defaults to given * [IntegrationRange] and [IntegrandMaxCalls]. */ @@ -27,11 +28,11 @@ public class GaussIntegrator( ) : UnivariateIntegrator { private fun buildRule(integrand: UnivariateIntegrand): Pair, Buffer> { - val factory = integrand.getFeature() ?: GaussLegendreRuleFactory - val predefinedRanges = integrand.getFeature() + val factory = integrand[GaussIntegratorRuleFactory] ?: GaussLegendreRuleFactory + val predefinedRanges = integrand[UnivariateIntegrandRanges] if (predefinedRanges == null || predefinedRanges.ranges.isEmpty()) { - val numPoints = integrand.getFeature()?.maxCalls ?: 100 - val range = integrand.getFeature()?.range ?: 0.0..1.0 + val numPoints = integrand[IntegrandMaxCalls] ?: 100 + val range = integrand[IntegrationRange] ?: 0.0..1.0 return factory.build(numPoints, range) } else { val ranges = predefinedRanges.ranges @@ -53,10 +54,10 @@ public class GaussIntegrator( } } - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand = with(algebra) { + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand = with(algebra) { val f = integrand.function val (points, weights) = buildRule(integrand) - var res = zero + var res: T = zero var c = zero for (i in points.indices) { val x = points[i] @@ -66,7 +67,10 @@ public class GaussIntegrator( c = t - res - y res = t } - return integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + points.size) + return integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + points.size) + } } public companion object @@ -84,12 +88,12 @@ public val Field.gaussIntegrator: GaussIntegrator get() = GaussI * Integrate using [intervals] segments with Gauss-Legendre rule of [order] order. */ @UnstableKMathAPI -public fun GaussIntegrator.integrate( +public inline fun GaussIntegrator.integrate( range: ClosedRange, order: Int = 10, intervals: Int = 10, - vararg features: IntegrandFeature, - function: (Double) -> T, + attributesBuilder: AttributesBuilder>.() -> Unit = {}, + noinline function: (Double) -> T, ): UnivariateIntegrand { require(range.endInclusive > range.start) { "The range upper bound should be higher than lower bound" } require(order > 1) { "The order of polynomial must be more than 1" } @@ -98,13 +102,15 @@ public fun GaussIntegrator.integrate( val ranges = UnivariateIntegrandRanges( (0 until intervals).map { i -> (range.start + rangeSize * i)..(range.start + rangeSize * (i + 1)) to order } ) - return process( - UnivariateIntegrand( - function, - IntegrationRange(range), - GaussLegendreRuleFactory, - ranges, - *features + return integrate( + UnivariateIntegrand( + attributeBuilder = { + IntegrationRange(range) + GaussIntegratorRuleFactory(GaussLegendreRuleFactory) + UnivariateIntegrandRanges(ranges) + attributesBuilder() + }, + function = function, ) ) } \ No newline at end of file diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt index 975fcd3a8..42ba2e3de 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt @@ -1,10 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.integration +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.mapToBuffer import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.Float64Buffer @@ -12,10 +13,10 @@ import space.kscience.kmath.structures.asBuffer import kotlin.math.ulp import kotlin.native.concurrent.ThreadLocal -public interface GaussIntegratorRuleFactory : IntegrandFeature { +public interface GaussIntegratorRuleFactory { public fun build(numPoints: Int): Pair, Buffer> - public companion object { + public companion object: IntegrandAttribute{ public fun double(numPoints: Int, range: ClosedRange): Pair, Buffer> = GaussLegendreRuleFactory.build(numPoints, range) } @@ -32,11 +33,11 @@ public fun GaussIntegratorRuleFactory.build( val normalized: Pair, Buffer> = build(numPoints) val length = range.endInclusive - range.start - val points = normalized.first.mapToBuffer(::Float64Buffer) { + val points = normalized.first.mapToBuffer(Float64Field.bufferFactory) { range.start + length / 2 + length / 2 * it } - val weights = normalized.second.mapToBuffer(::Float64Buffer) { + val weights = normalized.second.mapToBuffer(Float64Field.bufferFactory) { it * length / 2 } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrand.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrand.kt index 40fe78898..f5c98e9d9 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrand.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrand.kt @@ -1,48 +1,57 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.integration -import space.kscience.kmath.misc.Feature -import space.kscience.kmath.misc.FeatureSet -import space.kscience.kmath.misc.Featured -import kotlin.reflect.KClass +import space.kscience.attributes.* -public interface IntegrandFeature : Feature { - override fun toString(): String +public interface IntegrandAttribute : Attribute + +public interface Integrand : AttributeContainer { + + public val type: SafeType + + /** + * Create a copy of this integrand with a new set of attributes + */ + public fun withAttributes(attributes: Attributes): Integrand + + public companion object } -public interface Integrand : Featured { - public val features: FeatureSet - override fun getFeature(type: KClass): T? = features.getFeature(type) +public operator fun Integrand<*>.get(attribute: Attribute): T? = attributes[attribute] + +public sealed class IntegrandValue private constructor(): IntegrandAttribute{ + public companion object: IntegrandValue(){ + @Suppress("UNCHECKED_CAST") + public fun forType(): IntegrandValue = this as IntegrandValue + } } -public inline fun Integrand.getFeature(): T? = getFeature(T::class) - -public class IntegrandValue(public val value: T) : IntegrandFeature { - override fun toString(): String = "Value($value)" +public fun AttributesBuilder>.value(value: T) { + IntegrandValue.forType().invoke(value) } -public class IntegrandRelativeAccuracy(public val accuracy: Double) : IntegrandFeature { - override fun toString(): String = "TargetRelativeAccuracy($accuracy)" -} +/** + * Value of the integrand if it is present or null + */ +public inline val Integrand.valueOrNull: T? get() = attributes[IntegrandValue.forType()] -public class IntegrandAbsoluteAccuracy(public val accuracy: Double) : IntegrandFeature { - override fun toString(): String = "TargetAbsoluteAccuracy($accuracy)" -} +/** + * Value of the integrand or error + */ +public inline val Integrand.value: T get() = valueOrNull ?: error("No value in the integrand") -public class IntegrandCallsPerformed(public val calls: Int) : IntegrandFeature { - override fun toString(): String = "Calls($calls)" -} +public object IntegrandRelativeAccuracy : IntegrandAttribute -public val Integrand.calls: Int get() = getFeature()?.calls ?: 0 +public object IntegrandAbsoluteAccuracy : IntegrandAttribute -public class IntegrandMaxCalls(public val maxCalls: Int) : IntegrandFeature { - override fun toString(): String = "MaxCalls($maxCalls)" -} +public object IntegrandCallsPerformed : IntegrandAttribute -public class IntegrandIterationsRange(public val range: IntRange) : IntegrandFeature { - override fun toString(): String = "Iterations(${range.first}..${range.last})" -} +public val Integrand<*>.calls: Int get() = attributes[IntegrandCallsPerformed] ?: 0 + +public object IntegrandMaxCalls : IntegrandAttribute + +public object IntegrandIterationsRange : IntegrandAttribute diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrator.kt index 18c46b83b..cc8b833bd 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/Integrator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,9 +8,9 @@ package space.kscience.kmath.integration /** * A general interface for all integrators. */ -public interface Integrator { +public interface Integrator> { /** * Runs one integration pass and return a new [Integrand] with a new set of features. */ - public fun process(integrand: I): I + public fun integrate(integrand: I): I } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/MultivariateIntegrand.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/MultivariateIntegrand.kt index 53a563086..960e07b5b 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/MultivariateIntegrand.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/MultivariateIntegrand.kt @@ -1,26 +1,34 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.integration +import space.kscience.attributes.* import space.kscience.kmath.linear.Point -import space.kscience.kmath.misc.FeatureSet -public class MultivariateIntegrand internal constructor( - override val features: FeatureSet, +public class MultivariateIntegrand( + override val type: SafeType, + override val attributes: Attributes, public val function: (Point) -> T, -) : Integrand { +) : Integrand { + + override fun withAttributes(attributes: Attributes): MultivariateIntegrand = + MultivariateIntegrand(type, attributes, function) - public operator fun plus(feature: F): MultivariateIntegrand = - MultivariateIntegrand(features.with(feature), function) } -@Suppress("FunctionName") -public fun MultivariateIntegrand( - vararg features: IntegrandFeature, - function: (Point) -> T, -): MultivariateIntegrand = MultivariateIntegrand(FeatureSet.of(*features), function) +public fun MultivariateIntegrand.withAttribute( + attribute: Attribute, + value: A, +): MultivariateIntegrand = withAttributes(attributes.withAttribute(attribute, value)) -public val MultivariateIntegrand.value: T? get() = getFeature>()?.value +public fun MultivariateIntegrand.withAttributes( + block: AttributesBuilder>.() -> Unit, +): MultivariateIntegrand = withAttributes(attributes.modify(block)) + +public inline fun MultivariateIntegrand( + attributeBuilder: AttributesBuilder>.() -> Unit, + noinline function: (Point) -> T, +): MultivariateIntegrand = MultivariateIntegrand(safeTypeOf(), Attributes(attributeBuilder), function) diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt index 35ce82351..60e53c54a 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -44,17 +44,23 @@ public class SimpsonIntegrator( return res } - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand { - val ranges = integrand.getFeature() + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand { + val ranges = integrand[UnivariateIntegrandRanges] return if (ranges != null) { val res = algebra.sum(ranges.ranges.map { integrateRange(integrand, it.first, it.second) }) - integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + ranges.ranges.sumOf { it.second }) + integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + ranges.ranges.sumOf { it.second }) + } } else { - val numPoints = integrand.getFeature()?.maxCalls ?: 100 + val numPoints = integrand[IntegrandMaxCalls] ?: 100 require(numPoints >= 4) { "Simpson integrator requires at least 4 nodes" } - val range = integrand.getFeature()?.range ?: 0.0..1.0 + val range = integrand[IntegrationRange] ?: 0.0..1.0 val res = integrateRange(integrand, range, numPoints) - integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + numPoints) + integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + numPoints) + } } } } @@ -90,17 +96,23 @@ public object DoubleSimpsonIntegrator : UnivariateIntegrator { return res } - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand { - val ranges = integrand.getFeature() + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand { + val ranges = integrand[UnivariateIntegrandRanges] return if (ranges != null) { val res = ranges.ranges.sumOf { integrateRange(integrand, it.first, it.second) } - integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + ranges.ranges.sumOf { it.second }) + integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + ranges.ranges.sumOf { it.second }) + } } else { - val numPoints = integrand.getFeature()?.maxCalls ?: 100 + val numPoints = integrand[IntegrandMaxCalls] ?: 100 require(numPoints >= 4) { "Simpson integrator requires at least 4 nodes" } - val range = integrand.getFeature()?.range ?: 0.0..1.0 + val range = integrand[IntegrationRange] ?: 0.0..1.0 val res = integrateRange(integrand, range, numPoints) - integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + numPoints) + integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + numPoints) + } } } } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt index 1306e501f..24149959b 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -54,13 +54,13 @@ public class SplineIntegrator>( public val algebra: Field, public val bufferFactory: MutableBufferFactory, ) : UnivariateIntegrator { - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand = algebra { - val range = integrand.getFeature()?.range ?: 0.0..1.0 + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand = algebra { + val range = integrand[IntegrationRange] ?: 0.0..1.0 val interpolator: PolynomialInterpolator = SplineInterpolator(algebra, bufferFactory) - val nodes: Buffer = integrand.getFeature()?.nodes ?: run { - val numPoints = integrand.getFeature()?.maxCalls ?: 100 + val nodes: Buffer = integrand[UnivariateIntegrationNodes] ?: run { + val numPoints = integrand[IntegrandMaxCalls] ?: 100 val step = (range.endInclusive - range.start) / (numPoints - 1) Float64Buffer(numPoints) { i -> range.start + i * step } } @@ -71,7 +71,10 @@ public class SplineIntegrator>( values ) val res = polynomials.integrate(algebra, number(range.start)..number(range.endInclusive)) - integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + nodes.size) + integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + nodes.size) + } } } @@ -83,20 +86,23 @@ public class SplineIntegrator>( */ @UnstableKMathAPI public object DoubleSplineIntegrator : UnivariateIntegrator { - override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand { - val range = integrand.getFeature()?.range ?: 0.0..1.0 - val interpolator: PolynomialInterpolator = SplineInterpolator(Float64Field, ::Float64Buffer) + override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand { + val range = integrand[IntegrationRange] ?: 0.0..1.0 + val interpolator: PolynomialInterpolator = SplineInterpolator(Float64Field, Float64Field.bufferFactory) - val nodes: Buffer = integrand.getFeature()?.nodes ?: run { - val numPoints = integrand.getFeature()?.maxCalls ?: 100 + val nodes: Buffer = integrand[UnivariateIntegrationNodes] ?: run { + val numPoints = integrand[IntegrandMaxCalls] ?: 100 val step = (range.endInclusive - range.start) / (numPoints - 1) Float64Buffer(numPoints) { i -> range.start + i * step } } - val values = nodes.mapToBuffer(::Float64Buffer) { integrand.function(it) } + val values = nodes.mapToBuffer(Float64Field.bufferFactory) { integrand.function(it) } val polynomials = interpolator.interpolatePolynomials(nodes, values) val res = polynomials.integrate(Float64Field, range) - return integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + nodes.size) + return integrand.withAttributes { + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + nodes.size) + } } } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt index e29a6c881..9d718e633 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt @@ -1,40 +1,49 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.integration +import space.kscience.attributes.* import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.misc.FeatureSet import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.Float64Buffer -public class UnivariateIntegrand internal constructor( - override val features: FeatureSet, +public class UnivariateIntegrand( + override val type: SafeType, + override val attributes: Attributes, public val function: (Double) -> T, -) : Integrand { - public operator fun plus(feature: F): UnivariateIntegrand = - UnivariateIntegrand(features.with(feature), function) +) : Integrand { + + override fun withAttributes(attributes: Attributes): UnivariateIntegrand = + UnivariateIntegrand(type, attributes, function) } -@Suppress("FunctionName") -public fun UnivariateIntegrand( - function: (Double) -> T, - vararg features: IntegrandFeature, -): UnivariateIntegrand = UnivariateIntegrand(FeatureSet.of(*features), function) +public fun UnivariateIntegrand.withAttribute( + attribute: Attribute, + value: A, +): UnivariateIntegrand = withAttributes(attributes.withAttribute(attribute, value)) -public typealias UnivariateIntegrator = Integrator> +public fun UnivariateIntegrand.withAttributes( + block: AttributesBuilder>.() -> Unit, +): UnivariateIntegrand = withAttributes(attributes.modify(block)) + +public inline fun UnivariateIntegrand( + attributeBuilder: AttributesBuilder>.() -> Unit, + noinline function: (Double) -> T, +): UnivariateIntegrand = UnivariateIntegrand(safeTypeOf(), Attributes(attributeBuilder), function) + +public typealias UnivariateIntegrator = Integrator> + +public object IntegrationRange : IntegrandAttribute> -public class IntegrationRange(public val range: ClosedRange) : IntegrandFeature { - override fun toString(): String = "Range(${range.start}..${range.endInclusive})" -} /** * Set of univariate integration ranges. First components correspond to the ranges themselves, second components to - * number of integration nodes per range. + * the number of integration nodes per range. */ -public class UnivariateIntegrandRanges(public val ranges: List, Int>>) : IntegrandFeature { +public class UnivariateIntegrandRanges(public val ranges: List, Int>>) { public constructor(vararg pairs: Pair, Int>) : this(pairs.toList()) override fun toString(): String { @@ -43,60 +52,44 @@ public class UnivariateIntegrandRanges(public val ranges: List } -public class UnivariateIntegrationNodes(public val nodes: Buffer) : IntegrandFeature { - public constructor(vararg nodes: Double) : this(Float64Buffer(nodes)) +public object UnivariateIntegrationNodes : IntegrandAttribute> - override fun toString(): String = "UnivariateNodes($nodes)" +public fun AttributesBuilder>.integrationNodes(vararg nodes: Double) { + UnivariateIntegrationNodes(Float64Buffer(nodes)) } - -/** - * Value of the integrand if it is present or null - */ -public val UnivariateIntegrand.valueOrNull: T? get() = getFeature>()?.value - -/** - * Value of the integrand or error - */ -public val UnivariateIntegrand.value: T get() = valueOrNull ?: error("No value in the integrand") - /** * A shortcut method to integrate a [function] with additional [features]. Range must be provided in features. * The [function] is placed in the end position to allow passing a lambda. */ @UnstableKMathAPI -public fun UnivariateIntegrator.integrate( - vararg features: IntegrandFeature, - function: (Double) -> T, -): UnivariateIntegrand = process(UnivariateIntegrand(function, *features)) - -/** - * A shortcut method to integrate a [function] in [range] with additional [features]. - * The [function] is placed in the end position to allow passing a lambda. - */ -@UnstableKMathAPI -public fun UnivariateIntegrator.integrate( - range: ClosedRange, - vararg features: IntegrandFeature, - function: (Double) -> T, -): UnivariateIntegrand = process(UnivariateIntegrand(function, IntegrationRange(range), *features)) +public inline fun UnivariateIntegrator.integrate( + attributesBuilder: AttributesBuilder>.() -> Unit, + noinline function: (Double) -> T, +): UnivariateIntegrand = integrate(UnivariateIntegrand(attributesBuilder, function)) /** * A shortcut method to integrate a [function] in [range] with additional features. * The [function] is placed in the end position to allow passing a lambda. */ @UnstableKMathAPI -public fun UnivariateIntegrator.integrate( +public inline fun UnivariateIntegrator.integrate( range: ClosedRange, - featureBuilder: MutableList.() -> Unit = {}, - function: (Double) -> T, + attributeBuilder: AttributesBuilder>.() -> Unit = {}, + noinline function: (Double) -> T, ): UnivariateIntegrand { - //TODO use dedicated feature builder class instead or add extensions to MutableList - val features = buildList { - featureBuilder() - add(IntegrationRange(range)) - } - return process(UnivariateIntegrand(function, *features.toTypedArray())) + + return integrate( + UnivariateIntegrand( + attributeBuilder = { + IntegrationRange(range) + attributeBuilder() + }, + function = function + ) + ) } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/Interpolator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/Interpolator.kt index 191e7dfd9..3b9808028 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/Interpolator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/Interpolator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,8 @@ package space.kscience.kmath.interpolation +import space.kscience.attributes.SafeType +import space.kscience.attributes.WithType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.data.XYColumnarData import space.kscience.kmath.functions.PiecewisePolynomial @@ -26,9 +28,11 @@ public fun interface Interpolator { /** * And interpolator returning [PiecewisePolynomial] function */ -public interface PolynomialInterpolator> : Interpolator { +public interface PolynomialInterpolator> : Interpolator, WithType { public val algebra: Ring + override val type: SafeType get() = algebra.type + public fun getDefaultValue(): T = error("Out of bounds") public fun interpolatePolynomials(points: XYColumnarData): PiecewisePolynomial diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/LinearInterpolator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/LinearInterpolator.kt index 5c56e406a..e894799c9 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/LinearInterpolator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/LinearInterpolator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt index ed4657d53..2b58f0b05 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -12,7 +12,6 @@ import space.kscience.kmath.functions.Polynomial import space.kscience.kmath.operations.Field import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.MutableBufferFactory /** @@ -24,7 +23,7 @@ import space.kscience.kmath.structures.MutableBufferFactory */ public class SplineInterpolator>( override val algebra: Field, - public val bufferFactory: MutableBufferFactory, + public val bufferFactory: MutableBufferFactory = algebra.bufferFactory, ) : PolynomialInterpolator { //TODO possibly optimize zeroed buffers @@ -80,4 +79,4 @@ public fun > Field.splineInterpolator( ): SplineInterpolator = SplineInterpolator(this, bufferFactory) public val Float64Field.splineInterpolator: SplineInterpolator - get() = SplineInterpolator(this, ::Float64Buffer) \ No newline at end of file + get() = SplineInterpolator(this, bufferFactory) \ No newline at end of file diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt index 3051cdd8d..c320212f3 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialUtilTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialUtilTest.kt index 48e641335..e91535506 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialUtilTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/PolynomialUtilTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModulo.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModulo.kt index 3b7409e82..d53a1ac2b 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModulo.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModulo.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -9,6 +9,7 @@ package space.kscience.kmath.functions.testUtils import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.structures.MutableBufferFactory class IntModulo { @@ -109,15 +110,17 @@ class IntModulo { } @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -class IntModuloRing : Ring, ScaleOperations { +class IntModuloRing(modulus: Int) : Ring, ScaleOperations { val modulus: Int - constructor(modulus: Int) { + init { require(modulus != 0) { "modulus can not be zero" } this.modulus = if (modulus < 0) -modulus else modulus } + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() + override inline val zero: IntModulo get() = IntModulo(0, modulus, toCheckInput = false) override inline val one: IntModulo get() = IntModulo(1, modulus, toCheckInput = false) diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModuloUtils.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModuloUtils.kt index ffab2157c..ce9a0270c 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModuloUtils.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/IntModuloUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/Rational.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/Rational.kt index 2d21c2224..a1ed90c4d 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/Rational.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/Rational.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -10,6 +10,7 @@ package space.kscience.kmath.functions.testUtils import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.Field import space.kscience.kmath.operations.NumbersAddOps +import space.kscience.kmath.structures.MutableBufferFactory @Suppress("NAME_SHADOWING") class Rational { @@ -159,6 +160,7 @@ class Rational { @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") @OptIn(UnstableKMathAPI::class) object RationalField : Field, NumbersAddOps { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() override inline val zero: Rational get() = Rational.ZERO override inline val one: Rational get() = Rational.ONE diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/misc.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/misc.kt index 61b50f128..267fae83b 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/misc.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/functions/testUtils/misc.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt index dd8069335..0e8f9ddfb 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt index f5a79cdea..a4af7dc82 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -16,7 +16,10 @@ import kotlin.test.assertEquals class SimpsonIntegralTest { @Test fun gaussSin() { - val res = Float64Field.simpsonIntegrator.integrate(0.0..2 * PI, IntegrandMaxCalls(5)) { x -> + val res = Float64Field.simpsonIntegrator.integrate( + 0.0..2 * PI, + { IntegrandMaxCalls(5) } + ) { x -> sin(x) } assertEquals(0.0, res.value, 1e-2) @@ -24,7 +27,10 @@ class SimpsonIntegralTest { @Test fun gaussUniform() { - val res = Float64Field.simpsonIntegrator.integrate(35.0..100.0, IntegrandMaxCalls(20)) { x -> + val res = Float64Field.simpsonIntegrator.integrate( + 35.0..100.0, + { IntegrandMaxCalls(20) } + ) { x -> if (x in 30.0..50.0) { 1.0 } else { diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt index 6b62afbd3..946f8811c 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -18,15 +18,15 @@ import kotlin.test.assertEquals class SplineIntegralTest { @Test - fun integratePolynomial(){ + fun integratePolynomial() { val polynomial = Polynomial(1.0, 2.0, 3.0) - val integral = polynomial.integrate(Float64Field,1.0..2.0) + val integral = polynomial.integrate(Float64Field, 1.0..2.0) assertEquals(11.0, integral, 0.001) } @Test fun gaussSin() { - val res = Float64Field.splineIntegrator.integrate(0.0..2 * PI, IntegrandMaxCalls(5)) { x -> + val res = Float64Field.splineIntegrator.integrate(0.0..2 * PI, { IntegrandMaxCalls(5) }) { x -> sin(x) } assertEquals(0.0, res.value, 1e-2) @@ -34,8 +34,8 @@ class SplineIntegralTest { @Test fun gaussUniform() { - val res = Float64Field.splineIntegrator.integrate(35.0..100.0, IntegrandMaxCalls(20)) { x -> - if(x in 30.0..50.0){ + val res = Float64Field.splineIntegrator.integrate(35.0..100.0, { IntegrandMaxCalls(20) }) { x -> + if (x in 30.0..50.0) { 1.0 } else { 0.0 diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt index 24a664c26..c62d2c8bc 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt index 774b6196e..8869a1f45 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/README.md b/kmath-geometry/README.md index 480945c4f..601610d3d 100644 --- a/kmath-geometry/README.md +++ b/kmath-geometry/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-geometry:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-geometry:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-geometry:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-geometry:0.4.0-dev-1") + implementation("space.kscience:kmath-geometry:0.4.0-dev-3") } ``` diff --git a/kmath-geometry/build.gradle.kts b/kmath-geometry/build.gradle.kts index 32926db7e..4bf768f4b 100644 --- a/kmath-geometry/build.gradle.kts +++ b/kmath-geometry/build.gradle.kts @@ -6,6 +6,7 @@ kscience{ jvm() js() native() + wasm() useContextReceivers() useSerialization() diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt index d6d7e5725..8b9fcd396 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -9,23 +9,26 @@ import space.kscience.kmath.operations.Group import space.kscience.kmath.operations.Norm import space.kscience.kmath.operations.ScaleOperations -public interface Vector - -public interface GeometrySpace : Group, ScaleOperations, Norm { +/** + * A geometry vector space + * @param V the type of vector object + * @param D the type of distance + */ +public interface GeometrySpace> : Group, ScaleOperations, Norm { /** * L2 distance */ - public fun V.distanceTo(other: V): Double + public fun V.distanceTo(other: V): D /** * Scalar product */ public infix fun V.dot(other: V): Double - public companion object{ - /** - * Default precision for geometry objects comparison - */ - internal const val DEFAULT_PRECISION = 1e-6 - } + /** + * Default precision for geometry objects comparison + */ + public val defaultPrecision: D + + public companion object } \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt index 92d31a7cb..c65ccd43d 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,31 +7,30 @@ package space.kscience.kmath.geometry import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.kmath.geometry.euclidean2d.DoubleVector2D -import space.kscience.kmath.geometry.euclidean3d.DoubleVector3D +import space.kscience.kmath.structures.Float64 /** * A line formed by [start] vector of start and a [direction] vector. Direction vector is not necessarily normalized, * but its length does not affect line properties */ -public interface Line { +public interface Line { public val start: V public val direction: V } @Serializable @SerialName("Line") -private data class LineImpl(override val start: V, override val direction: V): Line +private data class LineImpl(override val start: V, override val direction: V) : Line -public fun Line(base: V, direction: V): Line = LineImpl(base, direction) +public fun Line(base: V, direction: V): Line = LineImpl(base, direction) -public typealias Line2D = Line -public typealias Line3D = Line +public typealias Line2D = Line> +public typealias Line3D = Line> /** * A directed line segment between [begin] and [end] */ -public interface LineSegment { +public interface LineSegment { public val begin: V public val end: V } @@ -41,13 +40,13 @@ public interface LineSegment { */ @Serializable @SerialName("LineSegment") -private data class LineSegmentImpl(override val begin: V, override val end: V) : LineSegment +private data class LineSegmentImpl(override val begin: V, override val end: V) : LineSegment -public fun LineSegment(begin: V, end: V): LineSegment = LineSegmentImpl(begin, end) +public fun LineSegment(begin: V, end: V): LineSegment = LineSegmentImpl(begin, end) -public fun LineSegment.line(algebra: GeometrySpace): Line = with(algebra) { +public fun LineSegment.line(algebra: GeometrySpace): Line = with(algebra) { Line(begin, end - begin) } -public typealias LineSegment2D = LineSegment -public typealias LineSegment3D = LineSegment +public typealias LineSegment2D = LineSegment> +public typealias LineSegment3D = LineSegment> diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Polygon.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Polygon.kt new file mode 100644 index 000000000..d7b450ab0 --- /dev/null +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Polygon.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2018-2024 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.geometry + + +/** + * A closed polygon in 2D space + */ +public interface Polygon { + public val points: List +} \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/ReferenceFrame.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/ReferenceFrame.kt index 21045e94e..f28bc6539 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/ReferenceFrame.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/ReferenceFrame.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector2D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector2D.kt index 9eced7ba9..f01b4c576 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector2D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector2D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,7 +7,7 @@ package space.kscience.kmath.geometry import space.kscience.kmath.linear.Point -public interface Vector2D : Point, Vector { +public interface Vector2D : Point { public val x: T public val y: T override val size: Int get() = 2 diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector3D.kt index 7e7c6c4ed..71442cf63 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Vector3D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,7 @@ package space.kscience.kmath.geometry import space.kscience.kmath.linear.Point import space.kscience.kmath.structures.Buffer -public interface Vector3D : Point, Vector { +public interface Vector3D : Point { public val x: T public val y: T public val z: T diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/angles.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/angles.kt index 3855514fb..ff3efc126 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/angles.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/angles.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2021 KMath contributors. + * Copyright 2018-2024 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. */ @@ -11,6 +11,8 @@ import kotlinx.serialization.builtins.serializer import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import space.kscience.kmath.operations.Group +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.jvm.JvmInline import kotlin.math.PI import kotlin.math.floor @@ -28,11 +30,19 @@ public sealed interface Angle : Comparable { public operator fun div(other: Angle): Double public operator fun unaryMinus(): Angle - public companion object { - public val zero: Radians = Radians(0.0) + public companion object: Group { + override val zero: Radians = Radians(0.0) public val pi: Radians = Radians(PI) public val piTimes2: Radians = Radians(PI * 2) public val piDiv2: Radians = Radians(PI / 2) + + + override fun add(left: Angle, right: Angle): Angle = left + right + + @Suppress("EXTENSION_SHADOWED_BY_MEMBER") + override fun Angle.unaryMinus(): Angle = -this + + override val bufferFactory: MutableBufferFactory = MutableBufferFactory() } } @@ -43,7 +53,7 @@ public object AngleSerializer : KSerializer { override fun deserialize(decoder: Decoder): Angle = decoder.decodeDouble().degrees override fun serialize(encoder: Encoder, value: Angle) { - encoder.encodeDouble(value.degrees) + encoder.encodeDouble(value.toDegrees().value) } } @@ -56,16 +66,16 @@ public value class Radians(public val value: Double) : Angle { override fun toRadians(): Radians = this override fun toDegrees(): Degrees = Degrees(value * 180 / PI) - public override fun plus(other: Angle): Radians = Radians(value + other.radians) - public override fun minus(other: Angle): Radians = Radians(value - other.radians) + public override fun plus(other: Angle): Radians = Radians(value + other.toRadians().value) + public override fun minus(other: Angle): Radians = Radians(value - other.toRadians().value) public override fun times(other: Number): Radians = Radians(value * other.toDouble()) public override fun div(other: Number): Radians = Radians(value / other.toDouble()) - override fun div(other: Angle): Double = value / other.radians + override fun div(other: Angle): Double = value / other.toRadians().value public override fun unaryMinus(): Radians = Radians(-value) - override fun compareTo(other: Angle): Int = value.compareTo(other.radians) + override fun compareTo(other: Angle): Int = value.compareTo(other.toRadians().value) } public fun sin(angle: Angle): Double = kotlin.math.sin(angle.toRadians().value) @@ -74,6 +84,7 @@ public fun tan(angle: Angle): Double = kotlin.math.tan(angle.toRadians().value) public val Number.radians: Radians get() = Radians(toDouble()) +@Deprecated("Convert to radians", ReplaceWith("toRadians().value")) public val Angle.radians: Double get() = toRadians().value /** @@ -84,26 +95,27 @@ public value class Degrees(public val value: Double) : Angle { override fun toRadians(): Radians = Radians(value * PI / 180) override fun toDegrees(): Degrees = this - public override fun plus(other: Angle): Degrees = Degrees(value + other.degrees) - public override fun minus(other: Angle): Degrees = Degrees(value - other.degrees) + public override fun plus(other: Angle): Degrees = Degrees(value + other.toDegrees().value) + public override fun minus(other: Angle): Degrees = Degrees(value - other.toDegrees().value) public override fun times(other: Number): Degrees = Degrees(value * other.toDouble()) public override fun div(other: Number): Degrees = Degrees(value / other.toDouble()) - override fun div(other: Angle): Double = value / other.degrees + override fun div(other: Angle): Double = value / other.toDegrees().value public override fun unaryMinus(): Degrees = Degrees(-value) - override fun compareTo(other: Angle): Int = value.compareTo(other.degrees) + override fun compareTo(other: Angle): Int = value.compareTo(other.toDegrees().value) } public val Number.degrees: Degrees get() = Degrees(toDouble()) +@Deprecated("Convert to degrees", ReplaceWith("toDegrees().value")) public val Angle.degrees: Double get() = toDegrees().value /** * Normalized angle 2 PI range symmetric around [center]. By default, uses (0, 2PI) range. */ public fun Angle.normalized(center: Angle = Angle.pi): Angle = - this - Angle.piTimes2 * floor((radians + PI - center.radians) / PI / 2) + this - Angle.piTimes2 * floor((toRadians().value + PI - center.toRadians().value) / PI / 2) public fun abs(angle: Angle): Angle = if (angle < Angle.zero) -angle else angle \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt index 1cf8d5dc2..fcffc7714 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Circle2D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,10 +7,11 @@ package space.kscience.kmath.geometry.euclidean2d import kotlinx.serialization.Serializable import space.kscience.kmath.geometry.Vector2D +import space.kscience.kmath.structures.Float64 import kotlin.math.PI -public interface Circle2D{ +public interface Circle2D { public val center: Vector2D public val radius: Double } @@ -23,9 +24,12 @@ public val Circle2D<*>.circumference: Double get() = radius * 2 * PI @Serializable public data class Float64Circle2D( @Serializable(Float64Space2D.VectorSerializer::class) override val center: Float64Vector2D, - override val radius: Double -): Circle2D + override val radius: Float64, +) : Circle2D -public fun Circle2D(center: Float64Vector2D, radius: Double): Circle2D = Float64Circle2D(center, radius) +public fun Circle2D(center: Vector2D, radius: Double): Circle2D = Float64Circle2D( + center as? Float64Vector2D ?: Float64Vector2D(center.x, center.y), + radius +) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float32Space2D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float32Space2D.kt index 4dd188d49..ae140ff06 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float32Space2D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float32Space2D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,16 +14,16 @@ import kotlinx.serialization.encoding.Encoder import space.kscience.kmath.geometry.GeometrySpace import space.kscience.kmath.geometry.Vector2D import space.kscience.kmath.operations.Float32Field -import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.structures.Float32 +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.pow import kotlin.math.sqrt -@Serializable(Float32Space2D.VectorSerializer::class) -public interface Float32Vector2D : Vector2D + +public typealias Float32Vector2D = Vector2D -public object Float32Space2D : GeometrySpace, ScaleOperations { - +public object Float32Space2D : GeometrySpace, Float32> { @Serializable @SerialName("Float32Vector2D") private data class Vector2DImpl( @@ -51,25 +51,29 @@ public object Float32Space2D : GeometrySpace, ScaleOperations): Float32 = sqrt(arg.x.pow(2) + arg.y.pow(2)) - public fun Float32Vector2D.norm(): Double = norm(this) + public fun Vector2D.norm(): Float32 = norm(this) - override fun Float32Vector2D.unaryMinus(): Float32Vector2D = vector(-x, -y) + override fun Vector2D.unaryMinus(): Float32Vector2D = vector(-x, -y) - override fun Float32Vector2D.distanceTo(other: Float32Vector2D): Double = (this - other).norm() + override fun Vector2D.distanceTo(other: Vector2D): Float32 = (this - other).norm() - override fun add(left: Float32Vector2D, right: Float32Vector2D): Float32Vector2D = + override fun add(left: Vector2D, right: Vector2D): Float32Vector2D = vector(left.x + right.x, left.y + right.y) - override fun scale(a: Float32Vector2D, value: Double): Float32Vector2D = + override fun scale(a: Vector2D, value: Double): Float32Vector2D = vector(a.x * value, a.y * value) - override fun Float32Vector2D.dot(other: Float32Vector2D): Double = + override fun Vector2D.dot(other: Vector2D): Double = (x * other.x + y * other.y).toDouble() public val xAxis: Float32Vector2D = vector(1.0, 0.0) public val yAxis: Float32Vector2D = vector(0.0, 1.0) + + override val defaultPrecision: Float32 = 1e-3f + + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() } public fun Float32Vector2D(x: Number, y: Number): Float32Vector2D = Float32Space2D.vector(x, y) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float64Space2D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float64Space2D.kt index ba8ea1396..37e11ba87 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float64Space2D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Float64Space2D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,59 +15,69 @@ import space.kscience.kmath.geometry.GeometrySpace import space.kscience.kmath.geometry.Vector2D import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.structures.Float64 +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.pow import kotlin.math.sqrt -public typealias DoubleVector2D = Vector2D -public typealias Float64Vector2D = Vector2D +public typealias Float64Vector2D = Vector2D + +@Deprecated("Use Float64Vector2D", ReplaceWith("Float64Vector2D")) +public typealias DoubleVector2D = Float64Vector2D -public val Vector2D.r: Double get() = Float64Space2D.norm(this) /** * 2D Euclidean space */ -public object Float64Space2D : GeometrySpace, ScaleOperations { +public object Float64Space2D : GeometrySpace, Float64>, ScaleOperations> { + @Serializable @SerialName("Float64Vector2D") private data class Vector2DImpl( override val x: Double, override val y: Double, - ) : DoubleVector2D + ) : Float64Vector2D public object VectorSerializer : KSerializer { private val proxySerializer = Vector2DImpl.serializer() override val descriptor: SerialDescriptor get() = proxySerializer.descriptor - override fun deserialize(decoder: Decoder): DoubleVector2D = decoder.decodeSerializableValue(proxySerializer) + override fun deserialize(decoder: Decoder): Float64Vector2D = decoder.decodeSerializableValue(proxySerializer) - override fun serialize(encoder: Encoder, value: DoubleVector2D) { + override fun serialize(encoder: Encoder, value: Float64Vector2D) { val vector = value as? Vector2DImpl ?: Vector2DImpl(value.x, value.y) encoder.encodeSerializableValue(proxySerializer, vector) } } - public fun vector(x: Number, y: Number): DoubleVector2D = Vector2DImpl(x.toDouble(), y.toDouble()) + public fun vector(x: Number, y: Number): Float64Vector2D = Vector2DImpl(x.toDouble(), y.toDouble()) - override val zero: DoubleVector2D by lazy { vector(0.0, 0.0) } + override val zero: Float64Vector2D by lazy { vector(0.0, 0.0) } - override fun norm(arg: DoubleVector2D): Double = sqrt(arg.x.pow(2) + arg.y.pow(2)) + override fun norm(arg: Vector2D): Double = sqrt(arg.x.pow(2) + arg.y.pow(2)) - override fun DoubleVector2D.unaryMinus(): DoubleVector2D = vector(-x, -y) + override fun Vector2D.unaryMinus(): Float64Vector2D = vector(-x, -y) - override fun DoubleVector2D.distanceTo(other: DoubleVector2D): Double = norm(this - other) - override fun add(left: DoubleVector2D, right: DoubleVector2D): DoubleVector2D = + override fun Vector2D.distanceTo(other: Vector2D): Double = norm(this - other) + override fun add(left: Vector2D, right: Vector2D): Float64Vector2D = vector(left.x + right.x, left.y + right.y) - override fun scale(a: DoubleVector2D, value: Double): DoubleVector2D = vector(a.x * value, a.y * value) - override fun DoubleVector2D.dot(other: DoubleVector2D): Double = x * other.x + y * other.y + override fun scale(a: Vector2D, value: Double): Float64Vector2D = vector(a.x * value, a.y * value) + override fun Vector2D.dot(other: Vector2D): Double = x * other.x + y * other.y - public val xAxis: DoubleVector2D = vector(1.0, 0.0) - public val yAxis: DoubleVector2D = vector(0.0, 1.0) + public val xAxis: Float64Vector2D = vector(1.0, 0.0) + public val yAxis: Float64Vector2D = vector(0.0, 1.0) + + override val defaultPrecision: Double = 1e-6 + + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() } public fun Float64Vector2D(x: Number, y: Number): Float64Vector2D = Float64Space2D.vector(x, y) +public val Float64Vector2D.r: Float64 get() = Float64Space2D.norm(this) + public val Float64Field.euclidean2D: Float64Space2D get() = Float64Space2D \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Polygon.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Polygon.kt deleted file mode 100644 index 85b377a56..000000000 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean2d/Polygon.kt +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2018-2023 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.geometry.euclidean2d - -import space.kscience.kmath.geometry.Vector2D - - -/** - * A closed polygon in 2D space - */ -public interface Polygon { - public val points: List> -} \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float32Space3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float32Space3D.kt index 1413a885b..ad61a7b77 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float32Space3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float32Space3D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,17 +14,16 @@ import kotlinx.serialization.encoding.Encoder import space.kscience.kmath.geometry.GeometrySpace import space.kscience.kmath.geometry.Vector3D import space.kscience.kmath.operations.Float32Field -import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.structures.Float32 +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.pow import kotlin.math.sqrt -@Serializable(Float32Space3D.VectorSerializer::class) -public interface Float32Vector3D: Vector3D + +public typealias Float32Vector3D = Vector3D -public object Float32Space3D : - GeometrySpace, - ScaleOperations{ +public object Float32Space3D : GeometrySpace, Float32> { @Serializable @SerialName("Float32Vector3D") @@ -54,21 +53,21 @@ public object Float32Space3D : override val zero: Float32Vector3D by lazy { vector(0.0, 0.0, 0.0) } - override fun norm(arg: Float32Vector3D): Double = sqrt(arg.x.pow(2) + arg.y.pow(2) + arg.z.pow(2)).toDouble() + override fun norm(arg: Vector3D): Float32 = sqrt(arg.x.pow(2) + arg.y.pow(2) + arg.z.pow(2)) - public fun Float32Vector3D.norm(): Double = norm(this) + public fun Vector3D.norm(): Float32 = norm(this) - override fun Float32Vector3D.unaryMinus(): Float32Vector3D = vector(-x, -y, -z) + override fun Vector3D.unaryMinus(): Float32Vector3D = vector(-x, -y, -z) - override fun Float32Vector3D.distanceTo(other: Float32Vector3D): Double = (this - other).norm() + override fun Vector3D.distanceTo(other: Vector3D): Float32 = (this - other).norm() - override fun add(left: Float32Vector3D, right: Float32Vector3D): Float32Vector3D = + override fun add(left: Vector3D, right: Vector3D): Float32Vector3D = vector(left.x + right.x, left.y + right.y, left.z + right.z) - override fun scale(a: Float32Vector3D, value: Double): Float32Vector3D = + override fun scale(a: Vector3D, value: Double): Float32Vector3D = vector(a.x * value, a.y * value, a.z * value) - override fun Float32Vector3D.dot(other: Float32Vector3D): Double = + override fun Vector3D.dot(other: Vector3D): Double = (x * other.x + y * other.y + z * other.z).toDouble() /** @@ -101,6 +100,10 @@ public object Float32Space3D : public val xAxis: Float32Vector3D = vector(1.0, 0.0, 0.0) public val yAxis: Float32Vector3D = vector(0.0, 1.0, 0.0) public val zAxis: Float32Vector3D = vector(0.0, 0.0, 1.0) + + override val defaultPrecision: Float32 = 1e-3f + + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() } public fun Float32Vector3D(x: Number, y: Number, z: Number): Float32Vector3D = Float32Space3D.vector(x, y, z) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt index a8c7e1d17..fa07190bb 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/Float64Space3D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -13,9 +13,10 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import space.kscience.kmath.geometry.GeometrySpace import space.kscience.kmath.geometry.Vector3D +import space.kscience.kmath.linear.Float64LinearSpace import space.kscience.kmath.operations.Float64Field -import space.kscience.kmath.operations.Norm -import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.structures.Float64 +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.pow import kotlin.math.sqrt @@ -32,13 +33,15 @@ internal fun leviCivita(i: Int, j: Int, k: Int): Int = when { else -> 0 } -public typealias DoubleVector3D = Vector3D -public typealias Float64Vector3D = Vector3D +public typealias Float64Vector3D = Vector3D -public val DoubleVector3D.r: Double get() = Float64Space3D.norm(this) +@Deprecated("Use Float64Vector3D", ReplaceWith("Float64Vector3D")) +public typealias DoubleVector3D = Float64Vector3D -public object Float64Space3D : GeometrySpace, ScaleOperations, - Norm { + +public object Float64Space3D : GeometrySpace, Double> { + + public val linearSpace: Float64LinearSpace = Float64LinearSpace @Serializable @SerialName("Float64Vector3D") @@ -46,52 +49,52 @@ public object Float64Space3D : GeometrySpace, ScaleOperations { + public object VectorSerializer : KSerializer { private val proxySerializer = Vector3DImpl.serializer() override val descriptor: SerialDescriptor get() = proxySerializer.descriptor - override fun deserialize(decoder: Decoder): DoubleVector3D = decoder.decodeSerializableValue(proxySerializer) + override fun deserialize(decoder: Decoder): Float64Vector3D = decoder.decodeSerializableValue(proxySerializer) - override fun serialize(encoder: Encoder, value: DoubleVector3D) { + override fun serialize(encoder: Encoder, value: Float64Vector3D) { val vector = value as? Vector3DImpl ?: Vector3DImpl(value.x, value.y, value.z) encoder.encodeSerializableValue(proxySerializer, vector) } } - public fun vector(x: Double, y: Double, z: Double): DoubleVector3D = + public fun vector(x: Double, y: Double, z: Double): Float64Vector3D = Vector3DImpl(x, y, z) - public fun vector(x: Number, y: Number, z: Number): DoubleVector3D = + public fun vector(x: Number, y: Number, z: Number): Float64Vector3D = vector(x.toDouble(), y.toDouble(), z.toDouble()) - override val zero: DoubleVector3D by lazy { vector(0.0, 0.0, 0.0) } + override val zero: Float64Vector3D by lazy { vector(0.0, 0.0, 0.0) } - override fun norm(arg: DoubleVector3D): Double = sqrt(arg.x.pow(2) + arg.y.pow(2) + arg.z.pow(2)) + override fun norm(arg: Vector3D): Double = sqrt(arg.x.pow(2) + arg.y.pow(2) + arg.z.pow(2)) - public fun DoubleVector3D.norm(): Double = norm(this) + public fun Vector3D.norm(): Double = norm(this) - override fun DoubleVector3D.unaryMinus(): DoubleVector3D = vector(-x, -y, -z) + override fun Vector3D.unaryMinus(): Float64Vector3D = vector(-x, -y, -z) - override fun DoubleVector3D.distanceTo(other: DoubleVector3D): Double = (this - other).norm() + override fun Vector3D.distanceTo(other: Vector3D): Double = (this - other).norm() - override fun add(left: DoubleVector3D, right: DoubleVector3D): DoubleVector3D = + override fun add(left: Vector3D, right: Vector3D): Float64Vector3D = vector(left.x + right.x, left.y + right.y, left.z + right.z) - override fun scale(a: DoubleVector3D, value: Double): DoubleVector3D = + override fun scale(a: Vector3D, value: Double): Float64Vector3D = vector(a.x * value, a.y * value, a.z * value) - override fun DoubleVector3D.dot(other: DoubleVector3D): Double = + override fun Vector3D.dot(other: Vector3D): Double = x * other.x + y * other.y + z * other.z /** * Compute vector product of [first] and [second]. The basis is assumed to be right-handed. */ public fun vectorProduct( - first: DoubleVector3D, - second: DoubleVector3D, - ): DoubleVector3D { + first: Vector3D, + second: Vector3D, + ): Float64Vector3D { var x = 0.0 var y = 0.0 var z = 0.0 @@ -110,11 +113,19 @@ public object Float64Space3D : GeometrySpace, ScaleOperations = vectorProduct(this, other) + public infix fun Vector3D.cross(other: Vector3D): Vector3D = vectorProduct(this, other) - public val xAxis: DoubleVector3D = vector(1.0, 0.0, 0.0) - public val yAxis: DoubleVector3D = vector(0.0, 1.0, 0.0) - public val zAxis: DoubleVector3D = vector(0.0, 0.0, 1.0) + public val xAxis: Float64Vector3D = vector(1.0, 0.0, 0.0) + public val yAxis: Float64Vector3D = vector(0.0, 1.0, 0.0) + public val zAxis: Float64Vector3D = vector(0.0, 0.0, 1.0) + + override val defaultPrecision: Double = 1e-6 + + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() } +@Suppress("UnusedReceiverParameter") public val Float64Field.euclidean3D: Float64Space3D get() = Float64Space3D + + +public val Vector3D.r: Double get() = Float64Space3D.norm(this) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt index deb8b0a93..1275ad630 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/euclidean3d/rotations3D.kt @@ -1,25 +1,43 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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.geometry.euclidean3d import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.complex.Quaternion -import space.kscience.kmath.complex.QuaternionAlgebra -import space.kscience.kmath.complex.normalized -import space.kscience.kmath.complex.reciprocal +import space.kscience.kmath.complex.* import space.kscience.kmath.geometry.* import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.Matrix import space.kscience.kmath.linear.linearSpace import space.kscience.kmath.linear.matrix import space.kscience.kmath.operations.Float64Field -import kotlin.math.pow -import kotlin.math.sqrt +import space.kscience.kmath.structures.Float64 +import kotlin.math.* + +public operator fun Quaternion.times(other: Quaternion): Quaternion = QuaternionAlgebra.multiply(this, other) + +public operator fun Quaternion.div(other: Quaternion): Quaternion = QuaternionAlgebra.divide(this, other) + +public fun Quaternion.power(number: Number): Quaternion = QuaternionAlgebra.power(this, number) + +/** + * Linear interpolation between [from] and [to] in spherical space + */ +public fun QuaternionAlgebra.slerp(from: Quaternion, to: Quaternion, fraction: Double): Quaternion = + (to / from).pow(fraction) * from + +public fun QuaternionAlgebra.angleBetween(q1: Quaternion, q2: Quaternion): Angle = (q1.conjugate * q2).theta + +public infix fun Quaternion.dot(other: Quaternion): Double = w * other.w + x * other.x + y * other.y + z * other.z + + +/** + * Represent a vector as quaternion with zero a rotation angle. + */ +internal fun Vector3D.asQuaternion(): Quaternion = Quaternion(0.0, x, y, z) -internal fun DoubleVector3D.toQuaternion(): Quaternion = Quaternion(0.0, x, y, z) /** * Angle in radians denoted by this quaternion rotation @@ -29,7 +47,7 @@ public val Quaternion.theta: Radians get() = (kotlin.math.acos(normalized().w) * /** * Create a normalized Quaternion from rotation angle and rotation vector */ -public fun Quaternion.Companion.fromRotation(theta: Angle, vector: DoubleVector3D): Quaternion { +public fun Quaternion.Companion.fromRotation(theta: Angle, vector: Float64Vector3D): Quaternion { val s = sin(theta / 2) val c = cos(theta / 2) val norm = with(Float64Space3D) { vector.norm() } @@ -39,9 +57,9 @@ public fun Quaternion.Companion.fromRotation(theta: Angle, vector: DoubleVector3 /** * An axis of quaternion rotation */ -public val Quaternion.vector: DoubleVector3D +public val Quaternion.vector: Float64Vector3D get() { - return object : DoubleVector3D { + return object : Float64Vector3D { private val sint2 = sqrt(1 - w * w) override val x: Double get() = this@vector.x / sint2 override val y: Double get() = this@vector.y / sint2 @@ -51,23 +69,30 @@ public val Quaternion.vector: DoubleVector3D } /** - * Rotate a vector in a [Float64Space3D] + * Rotate a vector in a [Float64Space3D] with [quaternion] */ -public fun Float64Space3D.rotate(vector: DoubleVector3D, q: Quaternion): DoubleVector3D = with(QuaternionAlgebra) { - val p = vector.toQuaternion() - (q * p * q.reciprocal).vector -} +public fun Float64Space3D.rotate(vector: Vector3D, quaternion: Quaternion): Float64Vector3D = + with(QuaternionAlgebra) { + val p = vector.asQuaternion() + (quaternion * p * quaternion.reciprocal).vector + } /** * Use a composition of quaternions to create a rotation */ @UnstableKMathAPI -public fun Float64Space3D.rotate(vector: DoubleVector3D, composition: QuaternionAlgebra.() -> Quaternion): DoubleVector3D = +public fun Float64Space3D.rotate( + vector: Float64Vector3D, + composition: QuaternionAlgebra.() -> Quaternion, +): Float64Vector3D = rotate(vector, QuaternionAlgebra.composition()) -public fun Float64Space3D.rotate(vector: DoubleVector3D, matrix: Matrix): DoubleVector3D { +/** + * Rotate a [Float64] vector in 3D space with a rotation matrix + */ +public fun Float64Space3D.rotate(vector: Float64Vector3D, matrix: Matrix): Vector3D { require(matrix.colNum == 3 && matrix.rowNum == 3) { "Square 3x3 rotation matrix is required" } - return with(Float64Field.linearSpace) { matrix.dot(vector).asVector3D() } + return with(linearSpace) { (matrix dot vector).asVector3D() } } /** @@ -86,6 +111,8 @@ public fun Quaternion.toRotationMatrix( } /** + * Convert a quaternion to a rotation matrix + * * taken from https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ */ public fun Quaternion.Companion.fromRotationMatrix(matrix: Matrix): Quaternion { @@ -146,6 +173,8 @@ public enum class RotationOrder { } /** + * Create a quaternion from Euler angles + * * Based on https://github.com/mrdoob/three.js/blob/master/src/math/Quaternion.js */ public fun Quaternion.Companion.fromEuler( @@ -154,13 +183,13 @@ public fun Quaternion.Companion.fromEuler( c: Angle, rotationOrder: RotationOrder, ): Quaternion { - val c1 = cos (a / 2) - val c2 = cos (b / 2) - val c3 = cos (c / 2) + val c1 = cos(a / 2) + val c2 = cos(b / 2) + val c3 = cos(c / 2) - val s1 = sin (a / 2) - val s2 = sin (b / 2) - val s3 = sin (c / 2) + val s1 = sin(a / 2) + val s2 = sin(b / 2) + val s3 = sin(c / 2) return when (rotationOrder) { @@ -206,6 +235,130 @@ public fun Quaternion.Companion.fromEuler( c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3 ) - else -> TODO("Proper Euler rotation orders are not supported yet") + + else -> TODO("Proper Euler rotation orders are not supported yet") } +} + +/** + * A vector consisting of angles + */ +public data class AngleVector(override val x: Angle, override val y: Angle, override val z: Angle) : Vector3D { + public companion object +} + +public fun Quaternion.Companion.fromEuler( + angles: AngleVector, + rotationOrder: RotationOrder, +): Quaternion = fromEuler(angles.x, angles.y, angles.z, rotationOrder) + +/** + * Based on https://github.com/mrdoob/three.js/blob/master/src/math/Euler.js + */ +public fun AngleVector.Companion.fromRotationMatrix( + matrix: Matrix, + rotationOrder: RotationOrder, + gimbaldLockThreshold: Double = 0.9999999, +): AngleVector = when (rotationOrder) { + + RotationOrder.XYZ -> { + if (abs(matrix[0, 2]) < gimbaldLockThreshold) { + AngleVector( + atan2(-matrix[1, 2], matrix[2, 2]).radians, + asin(matrix[0, 2].coerceIn(-1.0, 1.0)).radians, + atan2(-matrix[0, 1], matrix[0, 0]).radians + ) + + } else { + AngleVector( + atan2(matrix[2, 1], matrix[1, 1]).radians, + asin(matrix[0, 2].coerceIn(-1.0, 1.0)).radians, + Angle.zero + ) + } + } + + RotationOrder.YXZ -> { + if (abs(matrix[1, 2]) < gimbaldLockThreshold) { + AngleVector( + x = asin(-matrix[1, 2].coerceIn(-1.0, 1.0)).radians, + y = atan2(matrix[0, 2], matrix[2, 2]).radians, + z = atan2(matrix[1, 0], matrix[1, 1]).radians, + ) + } else { + AngleVector( + x = asin(-matrix[1, 2].coerceIn(-1.0, 1.0)).radians, + y = atan2(-matrix[2, 0], matrix[0, 0]).radians, + z = Angle.zero, + ) + + } + } + + RotationOrder.ZXY -> { + if (abs(matrix[2, 1]) < gimbaldLockThreshold) { + AngleVector( + x = asin(matrix[2, 1].coerceIn(-1.0, 1.0)).radians, + y = atan2(-matrix[2, 0], matrix[2, 2]).radians, + z = atan2(-matrix[0, 1], matrix[1, 1]).radians, + ) + + } else { + AngleVector( + x = asin(matrix[2, 1].coerceIn(-1.0, 1.0)).radians, + y = Angle.zero, + z = atan2(matrix[1, 0], matrix[0, 0]).radians, + ) + } + } + + RotationOrder.ZYX -> { + if (abs(matrix[2, 0]) < gimbaldLockThreshold) { + AngleVector( + x = atan2(matrix[2, 1], matrix[2, 2]).radians, + y = asin(-matrix[2, 0].coerceIn(-1.0, 1.0)).radians, + z = atan2(matrix[1, 0], matrix[0, 0]).radians, + ) + } else { + AngleVector( + x = Angle.zero, + y = asin(-matrix[2, 0].coerceIn(-1.0, 1.0)).radians, + z = atan2(-matrix[0, 1], matrix[1, 1]).radians, + ) + } + } + + RotationOrder.YZX -> { + if (abs(matrix[1, 0]) < gimbaldLockThreshold) { + AngleVector( + x = atan2(-matrix[1, 2], matrix[1, 1]).radians, + y = atan2(-matrix[2, 0], matrix[0, 0]).radians, + z = asin(matrix[1, 0].coerceIn(-1.0, 1.0)).radians, + ) + } else { + AngleVector( + x = Angle.zero, + y = atan2(matrix[0, 2], matrix[2, 2]).radians, + z = asin(matrix[1, 0].coerceIn(-1.0, 1.0)).radians, + ) + } + } + + RotationOrder.XZY -> { + if (abs(matrix[0, 1]) < gimbaldLockThreshold) { + AngleVector( + x = atan2(matrix[2, 1], matrix[1, 1]).radians, + y = atan2(matrix[0, 2], matrix[0, 0]).radians, + z = asin(-matrix[0, 1].coerceIn(-1.0, 1.0)).radians, + ) + } else { + AngleVector( + x = atan2(-matrix[1, 2], matrix[2, 2]).radians, + y = Angle.zero, + z = asin(-matrix[0, 1].coerceIn(-1.0, 1.0)).radians, + ) + } + } + + else -> TODO("Proper Euler rotation orders are not supported yet") } \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt index c5c3487a1..e3f6bb803 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/projections.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -12,7 +12,7 @@ package space.kscience.kmath.geometry * @param vector to project * @param line line to which vector should be projected */ -public fun GeometrySpace.projectToLine(vector: V, line: Line): V = with(line) { +public fun GeometrySpace.projectToLine(vector: V, line: Line): V = with(line) { start + (direction dot (vector - start)) / (direction dot direction) * direction } @@ -23,5 +23,5 @@ public fun GeometrySpace.projectToLine(vector: V, line: Line) * @param normal normal (perpendicular) vector to a hyper-plane to which vector should be projected * @param base point belonging to a hyper-plane to which vector should be projected */ -public fun GeometrySpace.projectAlong(vector: V, normal: V, base: V): V = +public fun GeometrySpace.projectAlong(vector: V, normal: V, base: V): V = vector + normal * ((base - vector) dot normal) / (normal dot normal) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt index 859255004..d45f11a6e 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ @@ -30,25 +30,4 @@ public fun QuaternionAlgebra.angleBetween(q1: Quaternion, q2: Quaternion): Angle /** * Euclidean product of two quaternions */ -public infix fun Quaternion.dot(other: Quaternion): Double = w * other.w + x * other.x + y * other.y + z * other.z - -// -///** -// * Convert a quaternion to XYZ Cardan angles assuming it is normalized. -// */ -//private fun Quaternion.normalizedToEuler(): Float32Vector3D { -// val roll = atan2(2 * y * w + 2 * x * z, 1 - 2 * y * y - 2 * z * z) -// val pitch = atan2(2 * x * w - 2 * y * z, 1 - 2 * x * x - 2 * z * z) -// val yaw = asin(2 * x * y + 2 * z * w) -// -// return Float32Vector3D(roll, pitch, yaw) -//} - -///** -// * Quaternion to XYZ Cardan angles -// */ -//public fun Quaternion.toEuler(): Float32Vector3D = if (QuaternionAlgebra.norm(this) == 0.0) { -// Float32Space3D.zero -//} else { -// normalized().normalizedToEuler() -//} \ No newline at end of file +public infix fun Quaternion.dot(other: Quaternion): Double = w * other.w + x * other.x + y * other.y + z * other.z \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/floatPrecision.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt similarity index 51% rename from kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/floatPrecision.kt rename to kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt index 2ed02182d..e8a14d036 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/floatPrecision.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/vectorPrecision.kt @@ -1,35 +1,24 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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.geometry -import space.kscience.kmath.geometry.GeometrySpace.Companion.DEFAULT_PRECISION import space.kscience.kmath.geometry.euclidean2d.Float64Space2D import space.kscience.kmath.geometry.euclidean2d.Float64Vector2D import space.kscience.kmath.geometry.euclidean3d.Float64Space3D import space.kscience.kmath.geometry.euclidean3d.Float64Vector3D +import space.kscience.kmath.structures.Float64 -/** - * Float equality within given [precision] - */ -public fun Double.equalsFloat(other: Double, precision: Double = DEFAULT_PRECISION): Boolean = - kotlin.math.abs(this - other) < precision - -/** - * Float equality within given [precision] - */ -public fun Double.equalsFloat(other: Float, precision: Double = DEFAULT_PRECISION): Boolean = - kotlin.math.abs(this - other) < precision /** * Vector equality within given [precision] (using [GeometrySpace.norm] provided by the space */ -public fun V.equalsVector( - space: GeometrySpace, +public fun > V.equalsVector( + space: GeometrySpace, other: V, - precision: Double = DEFAULT_PRECISION, + precision: D = space.defaultPrecision, ): Boolean = with(space) { norm(this@equalsVector - other) < precision } @@ -37,24 +26,24 @@ public fun V.equalsVector( /** * Vector equality using Euclidian L2 norm and given [precision] */ -public fun Float64Vector2D.equalsVector( +public fun Vector2D.equalsVector( other: Float64Vector2D, - precision: Double = DEFAULT_PRECISION, + precision: Double = Float64Space2D.defaultPrecision, ): Boolean = equalsVector(Float64Space2D, other, precision) /** - * Vector equality using Euclidian L2 norm and given [precision] + * Vector equality using Euclidean L2 norm and given [precision] */ -public fun Float64Vector3D.equalsVector( +public fun Vector3D.equalsVector( other: Float64Vector3D, - precision: Double = DEFAULT_PRECISION, + precision: Double = Float64Space3D.defaultPrecision, ): Boolean = equalsVector(Float64Space3D, other, precision) /** * Line equality using [GeometrySpace.norm] provided by the [space] and given [precision] */ -public fun LineSegment.equalsLine( - space: GeometrySpace, +public fun > LineSegment.equalsLine( + space: GeometrySpace, other: LineSegment, - precision: Double = DEFAULT_PRECISION, + precision: D = space.defaultPrecision, ): Boolean = begin.equalsVector(space, other.begin, precision) && end.equalsVector(space, other.end, precision) \ No newline at end of file diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/AngleTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/AngleTest.kt index b8086eb75..d5209a1bd 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/AngleTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/AngleTest.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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.geometry import kotlin.test.Test diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean2DSpaceTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean2DSpaceTest.kt index 7c5ee3485..2ad7c940f 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean2DSpaceTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean2DSpaceTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean3DSpaceTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean3DSpaceTest.kt index f881deb1b..64bdfcc61 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean3DSpaceTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Euclidean3DSpaceTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionAlongTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionAlongTest.kt index 9b3f40c4c..0c2267e8b 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionAlongTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionAlongTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionOntoLineTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionOntoLineTest.kt index d3a9d1171..32bd710c6 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionOntoLineTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/ProjectionOntoLineTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt index 1d354bf72..9e62a086c 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt @@ -1,11 +1,12 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.geometry import space.kscience.kmath.complex.Quaternion +import space.kscience.kmath.complex.QuaternionAlgebra import space.kscience.kmath.complex.normalized import space.kscience.kmath.geometry.euclidean3d.* import space.kscience.kmath.structures.Float64Buffer @@ -26,12 +27,31 @@ class RotationTest { } @Test - fun matrixConversion() { + fun matrixConversion() = with(QuaternionAlgebra){ val q = Quaternion(1.0, 2.0, -3.0, 4.0).normalized() val matrix = q.toRotationMatrix() + for (ro in listOf( + RotationOrder.XYZ, + RotationOrder.YXZ, + RotationOrder.ZXY, + RotationOrder.ZYX, + RotationOrder.YZX, + RotationOrder.XZY + )) { + val angles = AngleVector.fromRotationMatrix(matrix, ro) + + val reconstructed = Quaternion.fromEuler(angles, ro) + + if( reconstructed.w>0) { + assertBufferEquals(q, reconstructed) + } else{ + assertBufferEquals(q, -reconstructed) + } + } + assertBufferEquals(q, Quaternion.fromRotationMatrix(matrix)) } @@ -50,4 +70,5 @@ class RotationTest { val q1 = Quaternion.fromEuler(0.1.radians, 0.2.radians, 0.3.radians, RotationOrder.XYZ) assertBufferEquals(Float64Buffer(0.9818562, 0.0640713, 0.0911575, 0.1534393), q1) } + } \ No newline at end of file diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector2DTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector2DTest.kt index af27b5e00..af886ec29 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector2DTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector2DTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector3DTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector3DTest.kt index b10e650a6..9157f95d8 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector3DTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/Vector3DTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/testUtils.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/testUtils.kt index 521ce9e75..3f9c86add 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/testUtils.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/testUtils.kt @@ -1,12 +1,11 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.geometry -import space.kscience.kmath.geometry.euclidean2d.DoubleVector2D -import space.kscience.kmath.geometry.euclidean3d.DoubleVector3D +import space.kscience.kmath.structures.Float64 import kotlin.math.abs import kotlin.test.assertEquals @@ -27,24 +26,24 @@ fun grid( return xs.flatMap { x -> ys.map { y -> x to y } } } -fun assertVectorEquals(expected: DoubleVector2D, actual: DoubleVector2D, absoluteTolerance: Double = 1e-6) { +fun assertVectorEquals(expected: Vector2D, actual: Vector2D, absoluteTolerance: Double = 1e-3) { assertEquals(expected.x, actual.x, absoluteTolerance) assertEquals(expected.y, actual.y, absoluteTolerance) } -fun assertVectorEquals(expected: DoubleVector3D, actual: DoubleVector3D, absoluteTolerance: Double = 1e-6) { +fun assertVectorEquals(expected: Vector3D, actual: Vector3D, absoluteTolerance: Double = 1e-6) { assertEquals(expected.x, actual.x, absoluteTolerance) assertEquals(expected.y, actual.y, absoluteTolerance) assertEquals(expected.z, actual.z, absoluteTolerance) } -fun GeometrySpace.isCollinear(a: V, b: V, absoluteTolerance: Double = 1e-6): Boolean { +fun GeometrySpace.isCollinear(a: V, b: V, absoluteTolerance: Double = defaultPrecision): Boolean { val aDist = a.distanceTo(zero) val bDist = b.distanceTo(zero) - return abs(aDist) < absoluteTolerance || abs(bDist) < absoluteTolerance || abs(abs((a dot b) / (aDist * bDist)) - 1) < absoluteTolerance + return aDist < absoluteTolerance || bDist < absoluteTolerance || abs(abs((a dot b) / (aDist * bDist)) - 1) < absoluteTolerance } -fun GeometrySpace.isOrthogonal(a: V, b: V, absoluteTolerance: Double = 1e-6): Boolean = +fun GeometrySpace.isOrthogonal(a: V, b: V, absoluteTolerance: Double = 1e-6): Boolean = abs(a dot b) < absoluteTolerance fun Double.equalFloat(other: Double, maxFloatDelta: Double = 0.000001): diff --git a/kmath-geometry/src/jvmMain/kotlin/space/kscience/kmath/geometry/lineExtensions.kt b/kmath-geometry/src/jvmMain/kotlin/space/kscience/kmath/geometry/lineExtensions.kt index 5fcd2b23e..c263398ea 100644 --- a/kmath-geometry/src/jvmMain/kotlin/space/kscience/kmath/geometry/lineExtensions.kt +++ b/kmath-geometry/src/jvmMain/kotlin/space/kscience/kmath/geometry/lineExtensions.kt @@ -1,20 +1,23 @@ /* - * Copyright 2018-2021 KMath contributors. + * Copyright 2018-2024 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. */ import space.kscience.kmath.geometry.GeometrySpace import space.kscience.kmath.geometry.Line import space.kscience.kmath.geometry.LineSegment -import space.kscience.kmath.geometry.Vector import space.kscience.kmath.operations.Group /** * Get a line, containing this [LineSegment] */ -context(Group) public val LineSegment.line: Line get() = Line(begin, end - begin) +context(Group) +public val LineSegment.line: Line + get() = Line(begin, end - begin) /** * Get a length of a line segment */ -context(GeometrySpace) public val LineSegment.length: Double get() = norm(end - begin) \ No newline at end of file +context(GeometrySpace) +public val > LineSegment.length: D + get() = norm(end - begin) \ No newline at end of file diff --git a/kmath-histograms/README.md b/kmath-histograms/README.md index bc7f0fa5b..e9aaad3a8 100644 --- a/kmath-histograms/README.md +++ b/kmath-histograms/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-histograms:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-histograms:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-histograms:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-histograms:0.4.0-dev-1") + implementation("space.kscience:kmath-histograms:0.4.0-dev-3") } ``` diff --git a/kmath-histograms/build.gradle.kts b/kmath-histograms/build.gradle.kts index cf68fbb47..63791d61c 100644 --- a/kmath-histograms/build.gradle.kts +++ b/kmath-histograms/build.gradle.kts @@ -6,6 +6,7 @@ kscience{ jvm() js() native() + wasm() useCoroutines() } diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt index 3d948e1a7..acd889bf0 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt index 2a0523e46..9ba20df1a 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -47,7 +47,7 @@ public interface Histogram> { } } -public interface HistogramBuilder { +public interface HistogramBuilder { /** * The default value increment for a bin @@ -61,9 +61,9 @@ public interface HistogramBuilder { } -public fun HistogramBuilder.put(point: Point): Unit = putValue(point) +public fun HistogramBuilder.put(point: Point): Unit = putValue(point) -public fun HistogramBuilder.put(vararg point: T): Unit = put(point.asBuffer()) +public inline fun HistogramBuilder.put(vararg point: T): Unit = put(point.asList().asBuffer()) public fun HistogramBuilder.put(vararg point: Number): Unit = put(Float64Buffer(point.map { it.toDouble() }.toDoubleArray())) diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt index 7b1496fce..75f18a5a4 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/HistogramND.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/HistogramND.kt index 5fdc2ffb0..36acd8db5 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/HistogramND.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/HistogramND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt index 154d35350..770fd70a0 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -12,6 +12,7 @@ import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.MutableBufferFactory import kotlin.math.floor @OptIn(UnstableKMathAPI::class) @@ -46,6 +47,8 @@ public class UniformHistogram1DGroup( public val startPoint: Double = 0.0, ) : Group>, ScaleOperations> where A : Ring, A : ScaleOperations { + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() + override val zero: UniformHistogram1D = UniformHistogram1D(this, emptyMap()) /** diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt index 2ca3540c2..4a53ea1a0 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -37,6 +37,8 @@ public class UniformHistogramGroupND>( require(!lower.indices.any { upper[it] - lower[it] < 0 }) { "Range for one of axis is not strictly positive" } } + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() + public val dimension: Int get() = lower.size override val shape: ShapeND = ShapeND(IntArray(binNums.size) { binNums[it] + 2 }) @@ -87,7 +89,7 @@ public class UniformHistogramGroupND>( builder: HistogramBuilder.() -> Unit, ): HistogramND { val ndCounter: BufferND> = - StructureND.buffered(shape) { Counter.of(valueAlgebraND.elementAlgebra) } + BufferND(shape) { Counter.of(valueAlgebraND.elementAlgebra) } val hBuilder = object : HistogramBuilder { override val defaultValue: V get() = valueAlgebraND.elementAlgebra.one @@ -97,7 +99,8 @@ public class UniformHistogramGroupND>( } } hBuilder.apply(builder) - val values: BufferND = BufferND(ndCounter.indices, ndCounter.buffer.mapToBuffer(valueBufferFactory) { it.value }) + val values: BufferND = + BufferND(ndCounter.indices, ndCounter.buffer.mapToBuffer(valueBufferFactory) { it.value }) return HistogramND(this, values) } @@ -128,8 +131,7 @@ public fun > Histogram.Companion.uniformNDFromRanges( public fun Histogram.Companion.uniformDoubleNDFromRanges( vararg ranges: ClosedFloatingPointRange, -): UniformHistogramGroupND = - uniformNDFromRanges(Floa64FieldOpsND, *ranges, bufferFactory = ::Float64Buffer) +): UniformHistogramGroupND = uniformNDFromRanges(Floa64FieldOpsND, *ranges) /** @@ -147,21 +149,18 @@ public fun > Histogram.Companion.uniformNDFromRanges( bufferFactory: BufferFactory = valueAlgebraND.elementAlgebra.bufferFactory, ): UniformHistogramGroupND = UniformHistogramGroupND( valueAlgebraND, - ListBuffer( - ranges - .map(Pair, Int>::first) - .map(ClosedFloatingPointRange::start) - ), - ListBuffer( - ranges - .map(Pair, Int>::first) - .map(ClosedFloatingPointRange::endInclusive) - ), + ranges + .map(Pair, Int>::first) + .map(ClosedFloatingPointRange::start) + .asBuffer(), + ranges + .map(Pair, Int>::first) + .map(ClosedFloatingPointRange::endInclusive) + .asBuffer(), ranges.map(Pair, Int>::second).toIntArray(), valueBufferFactory = bufferFactory ) public fun Histogram.Companion.uniformDoubleNDFromRanges( vararg ranges: Pair, Int>, -): UniformHistogramGroupND = - uniformNDFromRanges(Floa64FieldOpsND, *ranges, bufferFactory = ::Float64Buffer) \ No newline at end of file +): UniformHistogramGroupND = uniformNDFromRanges(Floa64FieldOpsND, *ranges) \ No newline at end of file diff --git a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt index 54806c9fa..f6c80a903 100644 --- a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt +++ b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt index bda9d2bea..e90dae0fc 100644 --- a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt +++ b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramGroup.kt b/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramGroup.kt index 772db7df3..c8be4f846 100644 --- a/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramGroup.kt +++ b/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramGroup.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,10 +14,7 @@ import space.kscience.kmath.misc.sorted import space.kscience.kmath.operations.Group import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.ScaleOperations -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.first -import space.kscience.kmath.structures.indices -import space.kscience.kmath.structures.last +import space.kscience.kmath.structures.* import java.util.* private fun > TreeMap.getBin(value: Double): B? { @@ -53,6 +50,8 @@ public class TreeHistogramGroup( @PublishedApi internal val binFactory: (Double) -> DoubleDomain1D, ) : Group>, ScaleOperations> where A : Ring, A : ScaleOperations { + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() + internal inner class DomainCounter(val domain: DoubleDomain1D, val counter: Counter = Counter.of(valueAlgebra)) : ClosedRange by domain.range diff --git a/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt b/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt index 00b94c11f..25ffbbca5 100644 --- a/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt +++ b/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-jafama/README.md b/kmath-jafama/README.md index 47142f174..dc183ca37 100644 --- a/kmath-jafama/README.md +++ b/kmath-jafama/README.md @@ -7,19 +7,8 @@ Integration with [Jafama](https://github.com/jeffhain/jafama). ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-jafama:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-jafama:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-jafama:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -28,7 +17,7 @@ repositories { } dependencies { - implementation("space.kscience:kmath-jafama:0.4.0-dev-1") + implementation("space.kscience:kmath-jafama:0.4.0-dev-3") } ``` diff --git a/kmath-jafama/api/kmath-jafama.api b/kmath-jafama/api/kmath-jafama.api new file mode 100644 index 000000000..989634e9b --- /dev/null +++ b/kmath-jafama/api/kmath-jafama.api @@ -0,0 +1,128 @@ +public final class space/kscience/kmath/jafama/JafamaDoubleField : space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/ScaleOperations { + public static final field INSTANCE Lspace/kscience/kmath/jafama/JafamaDoubleField; + public fun acos (D)Ljava/lang/Double; + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh (D)Ljava/lang/Double; + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun add (DD)Ljava/lang/Double; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun asin (D)Ljava/lang/Double; + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh (D)Ljava/lang/Double; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atan (D)Ljava/lang/Double; + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh (D)Ljava/lang/Double; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; + public fun cos (D)Ljava/lang/Double; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh (D)Ljava/lang/Double; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun div (DD)Ljava/lang/Double; + public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (DD)Ljava/lang/Double; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun exp (D)Ljava/lang/Double; + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Double; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Double; + public synthetic fun getZero ()Ljava/lang/Object; + public fun ln (D)Ljava/lang/Double; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public fun minus (DD)Ljava/lang/Double; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (DD)Ljava/lang/Double; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (D)Ljava/lang/Double; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Double; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun plus (DD)Ljava/lang/Double; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun power (DLjava/lang/Number;)Ljava/lang/Double; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun scale (DD)Ljava/lang/Double; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun sin (D)Ljava/lang/Double; + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (D)Ljava/lang/Double; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sqrt (D)Ljava/lang/Double; + public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; + public fun tan (D)Ljava/lang/Double; + public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh (D)Ljava/lang/Double; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun times (DD)Ljava/lang/Double; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (D)Ljava/lang/Double; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class space/kscience/kmath/jafama/StrictJafamaDoubleField : space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/Norm, space/kscience/kmath/operations/ScaleOperations { + public static final field INSTANCE Lspace/kscience/kmath/jafama/StrictJafamaDoubleField; + public fun acos (D)Ljava/lang/Double; + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh (D)Ljava/lang/Double; + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun add (DD)Ljava/lang/Double; + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun asin (D)Ljava/lang/Double; + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh (D)Ljava/lang/Double; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atan (D)Ljava/lang/Double; + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh (D)Ljava/lang/Double; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; + public fun cos (D)Ljava/lang/Double; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh (D)Ljava/lang/Double; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun div (DD)Ljava/lang/Double; + public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun divide (DD)Ljava/lang/Double; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun exp (D)Ljava/lang/Double; + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public fun getBufferFactory ()Lspace/kscience/kmath/structures/MutableBufferFactory; + public fun getOne ()Ljava/lang/Double; + public synthetic fun getOne ()Ljava/lang/Object; + public fun getZero ()Ljava/lang/Double; + public synthetic fun getZero ()Ljava/lang/Object; + public fun ln (D)Ljava/lang/Double; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public fun minus (DD)Ljava/lang/Double; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun multiply (DD)Ljava/lang/Double; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun norm (D)Ljava/lang/Double; + public synthetic fun norm (Ljava/lang/Object;)Ljava/lang/Object; + public fun number (Ljava/lang/Number;)Ljava/lang/Double; + public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; + public fun plus (DD)Ljava/lang/Double; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun power (DLjava/lang/Number;)Ljava/lang/Double; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun scale (DD)Ljava/lang/Double; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun sin (D)Ljava/lang/Double; + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (D)Ljava/lang/Double; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sqrt (D)Ljava/lang/Double; + public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; + public fun tan (D)Ljava/lang/Double; + public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh (D)Ljava/lang/Double; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun times (DD)Ljava/lang/Double; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (D)Ljava/lang/Double; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; +} + diff --git a/kmath-jafama/build.gradle.kts b/kmath-jafama/build.gradle.kts index 5a77a97ed..0390224ba 100644 --- a/kmath-jafama/build.gradle.kts +++ b/kmath-jafama/build.gradle.kts @@ -14,7 +14,7 @@ repositories { } readme { - maturity = space.kscience.gradle.Maturity.PROTOTYPE + maturity = space.kscience.gradle.Maturity.DEPRECATED propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) feature("jafama-double", "src/main/kotlin/space/kscience/kmath/jafama/") { diff --git a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt index f9b8287b4..1b7bbf3ed 100644 --- a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt +++ b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,16 +7,17 @@ package space.kscience.kmath.jafama import net.jafama.FastMath import net.jafama.StrictFastMath -import space.kscience.kmath.operations.ExtendedField -import space.kscience.kmath.operations.Norm -import space.kscience.kmath.operations.PowerOperations -import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.operations.* +import space.kscience.kmath.structures.MutableBufferFactory /** * A field for [Double] (using FastMath) without boxing. Does not produce appropriate field element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") public object JafamaDoubleField : ExtendedField, Norm, ScaleOperations { + + override val bufferFactory: MutableBufferFactory get() = DoubleField.bufferFactory + override inline val zero: Double get() = 0.0 override inline val one: Double get() = 1.0 @@ -68,6 +69,9 @@ public object JafamaDoubleField : ExtendedField, Norm, S */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") public object StrictJafamaDoubleField : ExtendedField, Norm, ScaleOperations { + + override val bufferFactory: MutableBufferFactory get() = DoubleField.bufferFactory + override inline val zero: Double get() = 0.0 override inline val one: Double get() = 1.0 diff --git a/kmath-jupyter/README.md b/kmath-jupyter/README.md index 2b26878dc..525ae673b 100644 --- a/kmath-jupyter/README.md +++ b/kmath-jupyter/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-jupyter:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-jupyter:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-jupyter:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-jupyter:0.4.0-dev-1") + implementation("space.kscience:kmath-jupyter:0.4.0-dev-3") } ``` diff --git a/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt index 944666c9e..36ce25c03 100644 --- a/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt +++ b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-kotlingrad/README.md b/kmath-kotlingrad/README.md index f1a918b4b..a66c31686 100644 --- a/kmath-kotlingrad/README.md +++ b/kmath-kotlingrad/README.md @@ -8,19 +8,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-kotlingrad:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-kotlingrad:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-kotlingrad:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -29,6 +18,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-kotlingrad:0.4.0-dev-1") + implementation("space.kscience:kmath-kotlingrad:0.4.0-dev-3") } ``` diff --git a/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KMathNumber.kt b/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KMathNumber.kt index cd35e0c42..c261e6f91 100644 --- a/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KMathNumber.kt +++ b/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KMathNumber.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KotlingradExpression.kt b/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KotlingradExpression.kt index 110572140..07b77683d 100644 --- a/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KotlingradExpression.kt +++ b/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/KotlingradExpression.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,6 +7,7 @@ package space.kscience.kmath.kotlingrad import ai.hypergraph.kotlingrad.api.SFun import ai.hypergraph.kotlingrad.api.SVar +import space.kscience.attributes.SafeType import space.kscience.kmath.expressions.* import space.kscience.kmath.operations.NumericAlgebra @@ -25,6 +26,8 @@ public class KotlingradExpression>( public val algebra: A, public val mst: MST, ) : SpecialDifferentiableExpression> { + override val type: SafeType = algebra.type + override fun invoke(arguments: Map): T = mst.interpret(algebra, arguments) override fun derivativeOrNull( diff --git a/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/scalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/scalarsAdapters.kt index dd75a704c..363a698c6 100644 --- a/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/scalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/space/kscience/kmath/kotlingrad/scalarsAdapters.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt b/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt index 8d282a58a..f536e103d 100644 --- a/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt +++ b/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-memory/README.md b/kmath-memory/README.md index 594588ecf..d6824add0 100644 --- a/kmath-memory/README.md +++ b/kmath-memory/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-memory:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-memory:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-memory:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-memory:0.4.0-dev-1") + implementation("space.kscience:kmath-memory:0.4.0-dev-3") } ``` diff --git a/kmath-memory/api/kmath-memory.api b/kmath-memory/api/kmath-memory.api index cebb04af2..d8bfe6625 100644 --- a/kmath-memory/api/kmath-memory.api +++ b/kmath-memory/api/kmath-memory.api @@ -1,14 +1,3 @@ -public abstract interface annotation class space/kscience/kmath/PerformancePitfall : java/lang/annotation/Annotation { - public abstract fun message ()Ljava/lang/String; -} - -public abstract interface annotation class space/kscience/kmath/UnsafeKMathAPI : java/lang/annotation/Annotation { - public abstract fun message ()Ljava/lang/String; -} - -public abstract interface annotation class space/kscience/kmath/UnstableKMathAPI : java/lang/annotation/Annotation { -} - public final class space/kscience/kmath/memory/ByteBufferMemory : space/kscience/kmath/memory/Memory { public fun (Ljava/nio/ByteBuffer;II)V public synthetic fun (Ljava/nio/ByteBuffer;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V @@ -42,6 +31,22 @@ public abstract interface class space/kscience/kmath/memory/Memory { public final class space/kscience/kmath/memory/Memory$Companion { } +public class space/kscience/kmath/memory/MemoryBuffer : space/kscience/kmath/structures/Buffer { + public static final field Companion Lspace/kscience/kmath/memory/MemoryBuffer$Companion; + public fun (Lspace/kscience/kmath/memory/Memory;Lspace/kscience/kmath/memory/MemorySpec;)V + public fun get (I)Ljava/lang/Object; + protected final fun getMemory ()Lspace/kscience/kmath/memory/Memory; + public fun getSize ()I + protected final fun getSpec ()Lspace/kscience/kmath/memory/MemorySpec; + public fun iterator ()Ljava/util/Iterator; + public fun toString ()Ljava/lang/String; +} + +public final class space/kscience/kmath/memory/MemoryBuffer$Companion { + public final fun create (Lspace/kscience/kmath/memory/MemorySpec;I)Lspace/kscience/kmath/memory/MemoryBuffer; + public final fun create (Lspace/kscience/kmath/memory/MemorySpec;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/memory/MemoryBuffer; +} + public final class space/kscience/kmath/memory/MemoryKt { public static final fun read (Lspace/kscience/kmath/memory/Memory;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun write (Lspace/kscience/kmath/memory/Memory;Lkotlin/jvm/functions/Function1;)V @@ -58,7 +63,7 @@ public abstract interface class space/kscience/kmath/memory/MemoryReader : java/ public abstract fun readShort (I)S } -public abstract interface class space/kscience/kmath/memory/MemorySpec { +public abstract interface class space/kscience/kmath/memory/MemorySpec : space/kscience/attributes/WithType { public abstract fun getObjectSize ()I public abstract fun read (Lspace/kscience/kmath/memory/MemoryReader;I)Ljava/lang/Object; public abstract fun write (Lspace/kscience/kmath/memory/MemoryWriter;ILjava/lang/Object;)V @@ -81,3 +86,14 @@ public abstract interface class space/kscience/kmath/memory/MemoryWriter : java/ public abstract fun writeShort (IS)V } +public final class space/kscience/kmath/memory/MutableMemoryBuffer : space/kscience/kmath/memory/MemoryBuffer, space/kscience/kmath/structures/MutableBuffer { + public static final field Companion Lspace/kscience/kmath/memory/MutableMemoryBuffer$Companion; + public fun (Lspace/kscience/kmath/memory/Memory;Lspace/kscience/kmath/memory/MemorySpec;)V + public fun set (ILjava/lang/Object;)V +} + +public final class space/kscience/kmath/memory/MutableMemoryBuffer$Companion { + public final fun create (Lspace/kscience/kmath/memory/MemorySpec;I)Lspace/kscience/kmath/memory/MutableMemoryBuffer; + public final fun create (Lspace/kscience/kmath/memory/MemorySpec;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/memory/MutableMemoryBuffer; +} + diff --git a/kmath-memory/build.gradle.kts b/kmath-memory/build.gradle.kts index f1dff3b75..d2e50b10b 100644 --- a/kmath-memory/build.gradle.kts +++ b/kmath-memory/build.gradle.kts @@ -7,6 +7,10 @@ kscience { js() native() wasm() + + dependencies { + api(projects.kmathCore) + } } readme { diff --git a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt index a63753015..c80eb1072 100644 --- a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt +++ b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MemoryBuffer.kt b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemoryBuffer.kt similarity index 86% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MemoryBuffer.kt rename to kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemoryBuffer.kt index cbfd6b9cd..27deea126 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MemoryBuffer.kt +++ b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemoryBuffer.kt @@ -1,11 +1,12 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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 +package space.kscience.kmath.memory -import space.kscience.kmath.memory.* +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.MutableBuffer /** * A non-boxing buffer over [Memory] object. @@ -15,6 +16,7 @@ import space.kscience.kmath.memory.* * @property spec the spec of [T] type. */ public open class MemoryBuffer(protected val memory: Memory, protected val spec: MemorySpec) : Buffer { + override val size: Int get() = memory.size / spec.objectSize override operator fun get(index: Int): T = memory.read { read(spec, spec.objectSize * index) } @@ -43,13 +45,14 @@ public open class MemoryBuffer(protected val memory: Memory, protected * @property memory the underlying memory segment. * @property spec the spec of [T] type. */ -public class MutableMemoryBuffer(memory: Memory, spec: MemorySpec) : MemoryBuffer(memory, spec), - MutableBuffer { +public class MutableMemoryBuffer( + memory: Memory, + spec: MemorySpec, +) : MemoryBuffer(memory, spec), MutableBuffer { private val writer: MemoryWriter = memory.writer() override operator fun set(index: Int, value: T): Unit = writer.write(spec, spec.objectSize * index, value) - override fun copy(): MutableBuffer = MutableMemoryBuffer(memory.copy(), spec) public companion object { public fun create(spec: MemorySpec, size: Int): MutableMemoryBuffer = diff --git a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemorySpec.kt b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemorySpec.kt index 19bc3bae4..ac5dc7b92 100644 --- a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemorySpec.kt +++ b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/MemorySpec.kt @@ -1,16 +1,19 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.memory +import space.kscience.attributes.WithType + /** * A specification to read or write custom objects with fixed size in bytes. * * @param T the type of object this spec manages. */ -public interface MemorySpec { +public interface MemorySpec: WithType { + /** * Size of [T] in bytes after serialization. */ diff --git a/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt b/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt index 3726ddbb7..6ca792839 100644 --- a/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt +++ b/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt b/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt index f8bcef010..f7badde37 100644 --- a/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt +++ b/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt b/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt index d022cab23..c240e512d 100644 --- a/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt +++ b/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt b/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt index 0ae3c7ebc..5edad57c4 100644 --- a/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt +++ b/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-memory/src/wasmJsMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt b/kmath-memory/src/wasmJsMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt index 0cff551fa..30172b8b1 100644 --- a/kmath-memory/src/wasmJsMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt +++ b/kmath-memory/src/wasmJsMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-multik/README.md b/kmath-multik/README.md index 127b12a49..f0bf10208 100644 --- a/kmath-multik/README.md +++ b/kmath-multik/README.md @@ -6,19 +6,8 @@ JetBrains Multik connector ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-multik:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-multik:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-multik:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-multik:0.4.0-dev-1") + implementation("space.kscience:kmath-multik:0.4.0-dev-3") } ``` diff --git a/kmath-multik/build.gradle.kts b/kmath-multik/build.gradle.kts index fc51d2c21..85c4d52cb 100644 --- a/kmath-multik/build.gradle.kts +++ b/kmath-multik/build.gradle.kts @@ -15,7 +15,7 @@ kotlin{ sourceSets{ commonMain{ dependencies{ - api(project(":kmath-tensors")) + api(projects.kmathTensors) api("org.jetbrains.kotlinx:multik-core:$multikVersion") } } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt index 8b463a230..295aee876 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -20,7 +20,7 @@ public class MultikDoubleAlgebra( ) : MultikDivisionTensorAlgebra(multikEngine), TrigonometricOperations>, ExponentialOperations> { override val elementAlgebra: Float64Field get() = Float64Field - override val type: DataType get() = DataType.DoubleDataType + override val dataType: DataType get() = DataType.DoubleDataType override fun sin(arg: StructureND): MultikTensor = multikMath.mathEx.sin(arg.asMultik().array).wrap() diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt index 7a3dda94b..8ce6a791c 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,7 +15,7 @@ public class MultikFloatAlgebra( multikEngine: Engine ) : MultikDivisionTensorAlgebra(multikEngine) { override val elementAlgebra: Float32Field get() = Float32Field - override val type: DataType get() = DataType.FloatDataType + override val dataType: DataType get() = DataType.FloatDataType override fun scalar(value: Float): MultikTensor = Multik.ndarrayOf(value).wrap() } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt index 5bd1b3388..06b44ece7 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,7 +15,7 @@ public class MultikIntAlgebra( multikEngine: Engine ) : MultikTensorAlgebra(multikEngine) { override val elementAlgebra: Int32Ring get() = Int32Ring - override val type: DataType get() = DataType.IntDataType + override val dataType: DataType get() = DataType.IntDataType override fun scalar(value: Int): MultikTensor = Multik.ndarrayOf(value).wrap() } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt index 69a8ec042..d977326af 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,7 +15,7 @@ public class MultikLongAlgebra( multikEngine: Engine ) : MultikTensorAlgebra(multikEngine) { override val elementAlgebra: Int64Ring get() = Int64Ring - override val type: DataType get() = DataType.LongDataType + override val dataType: DataType get() = DataType.LongDataType override fun scalar(value: Long): MultikTensor = Multik.ndarrayOf(value).wrap() } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt index 7c8740665..caf07fef9 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,7 +15,7 @@ public class MultikShortAlgebra( multikEngine: Engine ) : MultikTensorAlgebra(multikEngine) { override val elementAlgebra: Int16Ring get() = Int16Ring - override val type: DataType get() = DataType.ShortDataType + override val dataType: DataType get() = DataType.ShortDataType override fun scalar(value: Short): MultikTensor = Multik.ndarrayOf(value).wrap() } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensor.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensor.kt index 59a9a1bf3..20916d934 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensor.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensor.kt @@ -1,18 +1,35 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.multik import org.jetbrains.kotlinx.multik.ndarray.data.* +import space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.ShapeND +import space.kscience.kmath.operations.* import space.kscience.kmath.tensors.api.Tensor import kotlin.jvm.JvmInline +public val DataType.type: SafeType<*> + get() = when (this) { + DataType.ByteDataType -> ByteRing.type + DataType.ShortDataType -> ShortRing.type + DataType.IntDataType -> IntRing.type + DataType.LongDataType -> LongRing.type + DataType.FloatDataType -> Float32Field.type + DataType.DoubleDataType -> Float64Field.type + DataType.ComplexFloatDataType -> safeTypeOf>() + DataType.ComplexDoubleDataType -> safeTypeOf>() + } + + @JvmInline public value class MultikTensor(public val array: MutableMultiArray) : Tensor { + override val shape: ShapeND get() = ShapeND(array.shape) @PerformancePitfall diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt index c3a82b167..f0212b467 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -26,16 +26,16 @@ public abstract class MultikTensorAlgebra>( private val multikEngine: Engine, ) : TensorAlgebra where T : Number, T : Comparable { - public abstract val type: DataType + public abstract val dataType: DataType protected val multikMath: Math = multikEngine.getMath() protected val multikLinAl: LinAlg = multikEngine.getLinAlg() protected val multikStat: Statistics = multikEngine.getStatistics() @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): MultikTensor { + override fun mutableStructureND(shape: ShapeND, initializer: A.(IntArray) -> T): MultikTensor { val strides = ColumnStrides(shape) - val memoryView = initMemoryView(strides.linearSize, type) + val memoryView = initMemoryView(strides.linearSize, dataType) strides.asSequence().forEachIndexed { linearIndex, tensorIndex -> memoryView[linearIndex] = elementAlgebra.initializer(tensorIndex) } @@ -44,12 +44,12 @@ public abstract class MultikTensorAlgebra>( @OptIn(PerformancePitfall::class, UnsafeKMathAPI::class) override fun StructureND.map(transform: A.(T) -> T): MultikTensor = if (this is MultikTensor) { - val data = initMemoryView(array.size, type) + val data = initMemoryView(array.size, dataType) var count = 0 for (el in array) data[count++] = elementAlgebra.transform(el) NDArray(data, shape = shape.asArray(), dim = array.dim).wrap() } else { - structureND(shape) { index -> + mutableStructureND(shape) { index -> transform(get(index)) } } @@ -58,7 +58,7 @@ public abstract class MultikTensorAlgebra>( override fun StructureND.mapIndexed(transform: A.(index: IntArray, T) -> T): MultikTensor = if (this is MultikTensor) { val array = asMultik().array - val data = initMemoryView(array.size, type) + val data = initMemoryView(array.size, dataType) val indexIter = array.multiIndices.iterator() var index = 0 for (item in array) { @@ -70,7 +70,7 @@ public abstract class MultikTensorAlgebra>( } NDArray(data, shape = array.shape, dim = array.dim).wrap() } else { - structureND(shape) { index -> + mutableStructureND(shape) { index -> transform(index, get(index)) } } @@ -95,7 +95,7 @@ public abstract class MultikTensorAlgebra>( require(left.shape.contentEquals(right.shape)) { "ND array shape mismatch" } //TODO replace by ShapeMismatchException val leftArray = left.asMultik().array val rightArray = right.asMultik().array - val data = initMemoryView(leftArray.size, type) + val data = initMemoryView(leftArray.size, dataType) var counter = 0 val leftIterator = leftArray.iterator() val rightIterator = rightArray.iterator() @@ -114,7 +114,7 @@ public abstract class MultikTensorAlgebra>( public fun StructureND.asMultik(): MultikTensor = if (this is MultikTensor) { this } else { - val res = mk.zeros(shape.asArray(), type).asDNArray() + val res = mk.zeros(shape.asArray(), dataType).asDNArray() for (index in res.multiIndices) { res[index] = this[index] } @@ -296,7 +296,7 @@ public abstract class MultikDivisionTensorAlgebra>( @OptIn(UnsafeKMathAPI::class) override fun T.div(arg: StructureND): MultikTensor = - Multik.ones(arg.shape.asArray(), type).apply { divAssign(arg.asMultik().array) }.wrap() + Multik.ones(arg.shape.asArray(), dataType).apply { divAssign(arg.asMultik().array) }.wrap() override fun StructureND.div(arg: T): MultikTensor = asMultik().array.div(arg).wrap() diff --git a/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt b/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt index 1abcf512d..fff88c70b 100644 --- a/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt +++ b/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-nd4j/README.md b/kmath-nd4j/README.md index b299c1b37..55d6844bc 100644 --- a/kmath-nd4j/README.md +++ b/kmath-nd4j/README.md @@ -9,19 +9,8 @@ ND4J based implementations of KMath abstractions. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-nd4j:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-nd4j:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-nd4j:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -30,7 +19,7 @@ repositories { } dependencies { - implementation("space.kscience:kmath-nd4j:0.4.0-dev-1") + implementation("space.kscience:kmath-nd4j:0.4.0-dev-3") } ``` diff --git a/kmath-nd4j/api/kmath-nd4j.api b/kmath-nd4j/api/kmath-nd4j.api new file mode 100644 index 000000000..5a9a7985d --- /dev/null +++ b/kmath-nd4j/api/kmath-nd4j.api @@ -0,0 +1,394 @@ +public final class space/kscience/kmath/nd4j/DoubleNd4jArrayField : space/kscience/kmath/nd4j/DoubleNd4jArrayFieldOps, space/kscience/kmath/nd/FieldND { + public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun getShape-IIYLAfE ()[I +} + +public class space/kscience/kmath/nd4j/DoubleNd4jArrayFieldOps : space/kscience/kmath/nd4j/Nd4jArrayExtendedFieldOps { + public static final field Companion Lspace/kscience/kmath/nd4j/DoubleNd4jArrayFieldOps$Companion; + public fun ()V + public fun div (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun div (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun div (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float64Field; + public fun getNdArray (Lspace/kscience/kmath/nd/StructureND;)Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun minus (DLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public synthetic fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; + public fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun times (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun wrap (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/DoubleNd4jArrayFieldOps$Companion : space/kscience/kmath/nd4j/DoubleNd4jArrayFieldOps { +} + +public final class space/kscience/kmath/nd4j/DoubleNd4jTensorAlgebra : space/kscience/kmath/nd4j/Nd4jTensorAlgebra { + public static final field INSTANCE Lspace/kscience/kmath/nd4j/DoubleNd4jTensorAlgebra; + public fun diagonalEmbedding (Lspace/kscience/kmath/nd/StructureND;III)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float64Field; + public fun getNdArray (Lspace/kscience/kmath/nd/StructureND;)Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun max (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun max (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public fun mean (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun mean (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public fun min (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun min (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun std (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun std (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public fun sum (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun sum (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public fun valueOrNull (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun valueOrNull (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public fun variance (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Double; + public synthetic fun variance (Lspace/kscience/kmath/nd/StructureND;)Ljava/lang/Object; + public fun wrap (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/FloatNd4jArrayField : space/kscience/kmath/nd4j/FloatNd4jArrayFieldOps, space/kscience/kmath/nd/RingND { + public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun getShape-IIYLAfE ()[I +} + +public class space/kscience/kmath/nd4j/FloatNd4jArrayFieldOps : space/kscience/kmath/nd4j/Nd4jArrayExtendedFieldOps { + public static final field Companion Lspace/kscience/kmath/nd4j/FloatNd4jArrayFieldOps$Companion; + public fun ()V + public fun div (FLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun div (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun div (Lspace/kscience/kmath/nd/StructureND;F)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float32Field; + public fun getNdArray (Lspace/kscience/kmath/nd/StructureND;)Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun minus (FLspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;F)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;F)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; + public fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; + public fun times (Lspace/kscience/kmath/nd/StructureND;F)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun wrap (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/FloatNd4jArrayFieldOps$Companion : space/kscience/kmath/nd4j/FloatNd4jArrayFieldOps { +} + +public final class space/kscience/kmath/nd4j/IntNd4jArrayRing : space/kscience/kmath/nd4j/IntNd4jArrayRingOps, space/kscience/kmath/nd/RingND { + public synthetic fun ([ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun getShape-IIYLAfE ()[I +} + +public class space/kscience/kmath/nd4j/IntNd4jArrayRingOps : space/kscience/kmath/nd4j/Nd4jArrayRingOps { + public static final field Companion Lspace/kscience/kmath/nd4j/IntNd4jArrayRingOps$Companion; + public fun ()V + public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Int32Ring; + public fun getNdArray (Lspace/kscience/kmath/nd/StructureND;)Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun minus (ILspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun times (Lspace/kscience/kmath/nd/StructureND;I)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public fun wrap (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/IntNd4jArrayRingOps$Companion : space/kscience/kmath/nd4j/IntNd4jArrayRingOps { +} + +public abstract interface class space/kscience/kmath/nd4j/Nd4jArrayAlgebra : space/kscience/kmath/nd/AlgebraND { + public abstract fun getNdArray (Lspace/kscience/kmath/nd/StructureND;)Lorg/nd4j/linalg/api/ndarray/INDArray; + public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; + public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; + public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public abstract fun wrap (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; + public fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayAlgebraKt { + public static final fun getNd4j (Lspace/kscience/kmath/operations/Float32Field;)Lspace/kscience/kmath/nd4j/FloatNd4jArrayFieldOps; + public static final fun getNd4j (Lspace/kscience/kmath/operations/Float64Field;)Lspace/kscience/kmath/nd4j/DoubleNd4jArrayFieldOps; + public static final fun getNd4j (Lspace/kscience/kmath/operations/Int32Ring;)Lspace/kscience/kmath/nd4j/IntNd4jArrayRingOps; + public static final fun nd4j (Lspace/kscience/kmath/operations/Float32Field;I[I)Lspace/kscience/kmath/nd4j/FloatNd4jArrayField; + public static final fun nd4j (Lspace/kscience/kmath/operations/Float64Field;I[I)Lspace/kscience/kmath/nd4j/DoubleNd4jArrayField; + public static final fun nd4j (Lspace/kscience/kmath/operations/Int32Ring;I[I)Lspace/kscience/kmath/nd4j/IntNd4jArrayRing; +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayDoubleStructure : space/kscience/kmath/nd4j/Nd4jArrayStructure, space/kscience/kmath/nd/StructureNDOfDouble { + public fun (Lorg/nd4j/linalg/api/ndarray/INDArray;)V + public final fun component1 ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public final fun copy (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayDoubleStructure; + public static synthetic fun copy$default (Lspace/kscience/kmath/nd4j/Nd4jArrayDoubleStructure;Lorg/nd4j/linalg/api/ndarray/INDArray;ILjava/lang/Object;)Lspace/kscience/kmath/nd4j/Nd4jArrayDoubleStructure; + public fun equals (Ljava/lang/Object;)Z + public fun get ([I)Ljava/lang/Double; + public synthetic fun get ([I)Ljava/lang/Object; + public fun getDouble ([I)D + public fun getNdArray ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun hashCode ()I + public fun set ([ID)V + public synthetic fun set ([ILjava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + +public abstract interface class space/kscience/kmath/nd4j/Nd4jArrayExtendedFieldOps : space/kscience/kmath/nd4j/Nd4jArrayField, space/kscience/kmath/operations/ExtendedFieldOps, space/kscience/kmath/operations/PowerOperations { + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; + public fun sqrt (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; +} + +public abstract interface class space/kscience/kmath/nd4j/Nd4jArrayField : space/kscience/kmath/nd/FieldOpsND, space/kscience/kmath/nd4j/Nd4jArrayRingOps { + public static final field Companion Lspace/kscience/kmath/nd4j/Nd4jArrayField$Companion; + public fun div (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun divide (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun divide (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun divide (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayField$Companion { +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayFloatStructure : space/kscience/kmath/nd4j/Nd4jArrayStructure { + public fun (Lorg/nd4j/linalg/api/ndarray/INDArray;)V + public final fun component1 ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public final fun copy (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayFloatStructure; + public static synthetic fun copy$default (Lspace/kscience/kmath/nd4j/Nd4jArrayFloatStructure;Lorg/nd4j/linalg/api/ndarray/INDArray;ILjava/lang/Object;)Lspace/kscience/kmath/nd4j/Nd4jArrayFloatStructure; + public fun equals (Ljava/lang/Object;)Z + public fun get ([I)Ljava/lang/Float; + public synthetic fun get ([I)Ljava/lang/Object; + public fun getNdArray ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun hashCode ()I + public fun set ([IF)V + public synthetic fun set ([ILjava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + +public abstract interface class space/kscience/kmath/nd4j/Nd4jArrayGroupOps : space/kscience/kmath/nd/GroupOpsND, space/kscience/kmath/nd4j/Nd4jArrayAlgebra { + public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun add (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun multiply (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; + public fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayIntStructure : space/kscience/kmath/nd4j/Nd4jArrayStructure, space/kscience/kmath/nd/StructureNDOfInt { + public fun (Lorg/nd4j/linalg/api/ndarray/INDArray;)V + public final fun component1 ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public final fun copy (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayIntStructure; + public static synthetic fun copy$default (Lspace/kscience/kmath/nd4j/Nd4jArrayIntStructure;Lorg/nd4j/linalg/api/ndarray/INDArray;ILjava/lang/Object;)Lspace/kscience/kmath/nd4j/Nd4jArrayIntStructure; + public fun equals (Ljava/lang/Object;)Z + public fun get ([I)Ljava/lang/Integer; + public synthetic fun get ([I)Ljava/lang/Object; + public fun getInt ([I)I + public fun getNdArray ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun hashCode ()I + public fun set ([II)V + public synthetic fun set ([ILjava/lang/Object;)V + public fun toString ()Ljava/lang/String; +} + +public abstract interface class space/kscience/kmath/nd4j/Nd4jArrayRingOps : space/kscience/kmath/nd/RingOpsND, space/kscience/kmath/nd4j/Nd4jArrayGroupOps { + public static final field Companion Lspace/kscience/kmath/nd4j/Nd4jArrayRingOps$Companion; + public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayRingOps$Companion { +} + +public abstract class space/kscience/kmath/nd4j/Nd4jArrayStructure : space/kscience/kmath/nd/MutableStructureND { + public fun elements ()Lkotlin/sequences/Sequence; + public abstract fun getNdArray ()Lorg/nd4j/linalg/api/ndarray/INDArray; + public fun getShape-IIYLAfE ()[I +} + +public final class space/kscience/kmath/nd4j/Nd4jArrayStructureKt { + public static final fun asDoubleStructure (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public static final fun asFloatStructure (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public static final fun asIntStructure (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayIntStructure; +} + +public abstract interface class space/kscience/kmath/nd4j/Nd4jTensorAlgebra : space/kscience/kmath/tensors/api/AnalyticTensorAlgebra { + public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun argMax (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public fun argMin (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun ceil (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun ceil (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun div (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun div (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun div (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun div (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun div (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun div (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun divAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Number;)V + public synthetic fun divAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Object;)V + public fun divAssign (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/StructureND;)V + public synthetic fun dot (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun dot (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun floor (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun floor (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public abstract fun getNdArray (Lspace/kscience/kmath/nd/StructureND;)Lorg/nd4j/linalg/api/ndarray/INDArray; + public synthetic fun getTensor (Lspace/kscience/kmath/nd/MutableStructureND;I)Lspace/kscience/kmath/nd/MutableStructureND; + public fun getTensor (Lspace/kscience/kmath/nd/MutableStructureND;I)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; + public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; + public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun max (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public fun max (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun mean (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun min (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public fun min (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun minus (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun minusAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Number;)V + public synthetic fun minusAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Object;)V + public fun minusAssign (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/StructureND;)V + public abstract fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun plus (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun plus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun plus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun plus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun plusAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Number;)V + public synthetic fun plusAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Object;)V + public fun plusAssign (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/StructureND;)V + public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; + public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; + public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun sqrt (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun sqrt (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun std (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun sum (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public fun sum (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun times (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; + public fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun times (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; + public synthetic fun times (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun times (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun timesAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Number;)V + public synthetic fun timesAssign (Lspace/kscience/kmath/nd/MutableStructureND;Ljava/lang/Object;)V + public fun timesAssign (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/StructureND;)V + public synthetic fun transposed (Lspace/kscience/kmath/nd/StructureND;II)Lspace/kscience/kmath/nd/MutableStructureND; + public fun transposed (Lspace/kscience/kmath/nd/StructureND;II)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; + public synthetic fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public fun variance (Lspace/kscience/kmath/nd/StructureND;IZ)Lspace/kscience/kmath/nd/MutableStructureND; + public synthetic fun view-waz_sdI (Lspace/kscience/kmath/nd/MutableStructureND;[I)Lspace/kscience/kmath/nd/MutableStructureND; + public fun view-waz_sdI (Lspace/kscience/kmath/nd/MutableStructureND;[I)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun viewAs (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun viewAs (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public abstract fun wrap (Lorg/nd4j/linalg/api/ndarray/INDArray;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; + public synthetic fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; + public fun zip (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd4j/Nd4jArrayStructure; +} + diff --git a/kmath-nd4j/build.gradle.kts b/kmath-nd4j/build.gradle.kts index e5c4af891..bfa09e771 100644 --- a/kmath-nd4j/build.gradle.kts +++ b/kmath-nd4j/build.gradle.kts @@ -12,7 +12,7 @@ dependencies { } readme { - maturity = space.kscience.gradle.Maturity.EXPERIMENTAL + maturity = space.kscience.gradle.Maturity.DEPRECATED propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) feature(id = "nd4jarraystructure") { "NDStructure wrapper for INDArray" } feature(id = "nd4jarrayrings") { "Rings over Nd4jArrayStructure of Int and Long" } diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt index 31523b340..cf0ec11c3 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -34,7 +34,7 @@ public sealed interface Nd4jArrayAlgebra> : AlgebraND.ndArray: INDArray @OptIn(PerformancePitfall::class) - override fun structureND(shape: ShapeND, initializer: C.(IntArray) -> T): Nd4jArrayStructure { + override fun mutableStructureND(shape: ShapeND, initializer: C.(IntArray) -> T): Nd4jArrayStructure { @OptIn(UnsafeKMathAPI::class) val struct: Nd4jArrayStructure = Nd4j.create(*shape.asArray())!!.wrap() struct.indicesIterator().forEach { struct[it] = elementAlgebra.initializer(it) } diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayIterator.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayIterator.kt index fedad26e0..0a2305e71 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayIterator.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayIterator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt index 93fbc8f85..ac4ef68d2 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -31,6 +31,7 @@ public sealed class Nd4jArrayStructure : MutableStructureND { } public data class Nd4jArrayIntStructure(override val ndArray: INDArray) : Nd4jArrayStructure(), StructureNDOfInt { + override fun elementsIterator(): Iterator> = ndArray.intIterator() @OptIn(PerformancePitfall::class) @@ -47,8 +48,10 @@ public data class Nd4jArrayIntStructure(override val ndArray: INDArray) : Nd4jAr */ public fun INDArray.asIntStructure(): Nd4jArrayIntStructure = Nd4jArrayIntStructure(this) -public data class Nd4jArrayDoubleStructure(override val ndArray: INDArray) : Nd4jArrayStructure(), StructureNDOfDouble { +public data class Nd4jArrayDoubleStructure(override val ndArray: INDArray) : Nd4jArrayStructure(), + StructureNDOfDouble { override fun elementsIterator(): Iterator> = ndArray.realIterator() + @OptIn(PerformancePitfall::class) override fun get(index: IntArray): Double = ndArray.getDouble(*index) @@ -64,7 +67,9 @@ public data class Nd4jArrayDoubleStructure(override val ndArray: INDArray) : Nd4 public fun INDArray.asDoubleStructure(): Nd4jArrayStructure = Nd4jArrayDoubleStructure(this) public data class Nd4jArrayFloatStructure(override val ndArray: INDArray) : Nd4jArrayStructure() { + override fun elementsIterator(): Iterator> = ndArray.floatIterator() + @PerformancePitfall override fun get(index: IntArray): Float = ndArray.getFloat(*index) diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt index c308e64b9..c69e46292 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -37,20 +37,20 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe */ public val StructureND.ndArray: INDArray - override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): Nd4jArrayStructure + override fun mutableStructureND(shape: ShapeND, initializer: A.(IntArray) -> T): Nd4jArrayStructure @OptIn(PerformancePitfall::class) override fun StructureND.map(transform: A.(T) -> T): Nd4jArrayStructure = - structureND(shape) { index -> elementAlgebra.transform(get(index)) } + mutableStructureND(shape) { index -> elementAlgebra.transform(get(index)) } @OptIn(PerformancePitfall::class) override fun StructureND.mapIndexed(transform: A.(index: IntArray, T) -> T): Nd4jArrayStructure = - structureND(shape) { index -> elementAlgebra.transform(index, get(index)) } + mutableStructureND(shape) { index -> elementAlgebra.transform(index, get(index)) } @OptIn(PerformancePitfall::class) override fun zip(left: StructureND, right: StructureND, transform: A.(T, T) -> T): Nd4jArrayStructure { require(left.shape.contentEquals(right.shape)) - return structureND(left.shape) { index -> elementAlgebra.transform(left[index], right[index]) } + return mutableStructureND(left.shape) { index -> elementAlgebra.transform(left[index], right[index]) } } override fun T.plus(arg: StructureND): Nd4jArrayStructure = arg.ndArray.add(this).wrap() @@ -178,7 +178,7 @@ public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra override fun INDArray.wrap(): Nd4jArrayStructure = asDoubleStructure() @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): Nd4jArrayStructure { + override fun mutableStructureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): Nd4jArrayStructure { val array: INDArray = Nd4j.zeros(*shape.asArray()) val indices = ColumnStrides(shape) indices.asSequence().forEach { index -> diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt index 401c57a7b..8f02f3c55 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt index 484618de2..704b28cd9 100644 --- a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt index d57eb2e2d..5c3664cb3 100644 --- a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-optimization/README.md b/kmath-optimization/README.md index 79a4f5d24..52a018a4e 100644 --- a/kmath-optimization/README.md +++ b/kmath-optimization/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-optimization:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-optimization:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-optimization:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-optimization:0.4.0-dev-1") + implementation("space.kscience:kmath-optimization:0.4.0-dev-3") } ``` diff --git a/kmath-optimization/build.gradle.kts b/kmath-optimization/build.gradle.kts index 7250d1f72..f908b127d 100644 --- a/kmath-optimization/build.gradle.kts +++ b/kmath-optimization/build.gradle.kts @@ -6,6 +6,7 @@ kscience{ jvm() js() native() + wasm() } kotlin.sourceSets { diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt index 07146625c..a38e9e6e8 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt @@ -1,28 +1,33 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.optimization +import space.kscience.attributes.* import space.kscience.kmath.expressions.DifferentiableExpression import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.misc.FeatureSet -public class OptimizationValue(public val value: T) : OptimizationFeature { - override fun toString(): String = "Value($value)" +public class OptimizationValue(type: SafeType) : PolymorphicAttribute(type) + +public inline fun AttributesBuilder>.value(value: T) { + set(OptimizationValue(safeTypeOf()), value) } -public enum class FunctionOptimizationTarget : OptimizationFeature { +public enum class OptimizationDirection { MAXIMIZE, MINIMIZE } +public object FunctionOptimizationTarget: OptimizationAttribute + public class FunctionOptimization( - override val features: FeatureSet, public val expression: DifferentiableExpression, + override val attributes: Attributes, ) : OptimizationProblem { + override val type: SafeType get() = expression.type override fun equals(other: Any?): Boolean { if (this === other) return true @@ -30,42 +35,69 @@ public class FunctionOptimization( other as FunctionOptimization<*> - if (features != other.features) return false + if (attributes != other.attributes) return false if (expression != other.expression) return false return true } override fun hashCode(): Int { - var result = features.hashCode() + var result = attributes.hashCode() result = 31 * result + expression.hashCode() return result } - override fun toString(): String = "FunctionOptimization(features=$features)" + override fun toString(): String = "FunctionOptimization(features=$attributes)" + + public companion object } -public fun FunctionOptimization.withFeatures( - vararg newFeature: OptimizationFeature, +public fun FunctionOptimization( + expression: DifferentiableExpression, + attributeBuilder: AttributesBuilder>.() -> Unit, +): FunctionOptimization = FunctionOptimization(expression, Attributes(attributeBuilder)) + +public class OptimizationPrior : + PolymorphicAttribute>(safeTypeOf()), + Attribute> + +public fun FunctionOptimization.withAttributes( + modifier: AttributesBuilder>.() -> Unit, ): FunctionOptimization = FunctionOptimization( - features.with(*newFeature), expression, + attributes.modify(modifier), ) /** * Optimizes differentiable expression using specific [optimizer] form given [startingPoint]. */ -public suspend fun DifferentiableExpression.optimizeWith( +public suspend fun DifferentiableExpression.optimizeWith( optimizer: Optimizer>, startingPoint: Map, - vararg features: OptimizationFeature, + modifier: AttributesBuilder>.() -> Unit = {}, ): FunctionOptimization { - val problem = FunctionOptimization(FeatureSet.of(OptimizationStartPoint(startingPoint), *features), this) + val problem = FunctionOptimization(this){ + startAt(startingPoint) + modifier() + } return optimizer.optimize(problem) } public val FunctionOptimization.resultValueOrNull: T? - get() = getFeature>()?.point?.let { expression(it) } + get() = attributes[OptimizationResult()]?.let { expression(it) } public val FunctionOptimization.resultValue: T - get() = resultValueOrNull ?: error("Result is not present in $this") \ No newline at end of file + get() = resultValueOrNull ?: error("Result is not present in $this") + + +public suspend fun DifferentiableExpression.optimizeWith( + optimizer: Optimizer>, + vararg startingPoint: Pair, + builder: AttributesBuilder>.() -> Unit = {}, +): FunctionOptimization { + val problem = FunctionOptimization(this) { + startAt(mapOf(*startingPoint)) + builder() + } + return optimizer.optimize(problem) +} diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt deleted file mode 100644 index 0459d46ee..000000000 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2018-2022 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.optimization - -import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.data.XYColumnarData -import space.kscience.kmath.expressions.DifferentiableExpression -import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.misc.FeatureSet - -public abstract class OptimizationBuilder> { - public val features: MutableList = ArrayList() - - public fun addFeature(feature: OptimizationFeature) { - features.add(feature) - } - - public inline fun updateFeature(update: (T?) -> T) { - val existing = features.find { it.key == T::class } as? T - val new = update(existing) - if (existing != null) { - features.remove(existing) - } - addFeature(new) - } - - public abstract fun build(): R -} - -public fun OptimizationBuilder.startAt(startingPoint: Map) { - addFeature(OptimizationStartPoint(startingPoint)) -} - -public class FunctionOptimizationBuilder( - private val expression: DifferentiableExpression, -) : OptimizationBuilder>() { - override fun build(): FunctionOptimization = FunctionOptimization(FeatureSet.of(features), expression) -} - -public fun FunctionOptimization( - expression: DifferentiableExpression, - builder: FunctionOptimizationBuilder.() -> Unit, -): FunctionOptimization = FunctionOptimizationBuilder(expression).apply(builder).build() - -public suspend fun DifferentiableExpression.optimizeWith( - optimizer: Optimizer>, - startingPoint: Map, - builder: FunctionOptimizationBuilder.() -> Unit = {}, -): FunctionOptimization { - val problem = FunctionOptimization(this) { - startAt(startingPoint) - builder() - } - return optimizer.optimize(problem) -} - -public suspend fun DifferentiableExpression.optimizeWith( - optimizer: Optimizer>, - vararg startingPoint: Pair, - builder: FunctionOptimizationBuilder.() -> Unit = {}, -): FunctionOptimization { - val problem = FunctionOptimization(this) { - startAt(mapOf(*startingPoint)) - builder() - } - return optimizer.optimize(problem) -} - - -@OptIn(UnstableKMathAPI::class) -public class XYOptimizationBuilder( - public val data: XYColumnarData, - public val model: DifferentiableExpression, -) : OptimizationBuilder() { - - public var pointToCurveDistance: PointToCurveDistance = PointToCurveDistance.byY - public var pointWeight: PointWeight = PointWeight.byYSigma - - override fun build(): XYFit = XYFit( - data, - model, - FeatureSet.of(features), - pointToCurveDistance, - pointWeight - ) -} - -@OptIn(UnstableKMathAPI::class) -public fun XYOptimization( - data: XYColumnarData, - model: DifferentiableExpression, - builder: XYOptimizationBuilder.() -> Unit, -): XYFit = XYOptimizationBuilder(data, model).apply(builder).build() \ No newline at end of file diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt index 9fdcfc53d..18f552bda 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationProblem.kt @@ -1,79 +1,66 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.optimization -import space.kscience.kmath.expressions.DifferentiableExpression +import space.kscience.attributes.* import space.kscience.kmath.expressions.NamedMatrix import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.misc.* -import kotlin.reflect.KClass +import space.kscience.kmath.misc.Loggable -public interface OptimizationFeature : Feature { - // enforce toString override - override fun toString(): String -} +public interface OptimizationAttribute : Attribute -public interface OptimizationProblem : Featured { - public val features: FeatureSet - override fun getFeature(type: KClass): F? = features.getFeature(type) -} +public interface OptimizationProblem : AttributeContainer, WithType -public inline fun OptimizationProblem<*>.getFeature(): F? = getFeature(F::class) - -public open class OptimizationStartPoint(public val point: Map) : OptimizationFeature { - override fun toString(): String = "StartPoint($point)" -} - - -public interface OptimizationPrior : OptimizationFeature, DifferentiableExpression { - override val key: FeatureKey get() = OptimizationPrior::class -} - -/** - * Covariance matrix for - */ -public class OptimizationCovariance(public val covariance: NamedMatrix) : OptimizationFeature { - override fun toString(): String = "Covariance($covariance)" -} +public class OptimizationStartPoint : OptimizationAttribute>, + PolymorphicAttribute>(safeTypeOf()) /** * Get the starting point for optimization. Throws error if not defined. */ public val OptimizationProblem.startPoint: Map - get() = getFeature>()?.point - ?: error("Starting point not defined in $this") + get() = attributes[OptimizationStartPoint()] ?: error("Starting point not defined in $this") -public open class OptimizationResult(public val point: Map) : OptimizationFeature { - override fun toString(): String = "Result($point)" +public fun AttributesBuilder>.startAt(startingPoint: Map) { + set(::OptimizationStartPoint, startingPoint) } -public val OptimizationProblem.resultPointOrNull: Map? - get() = getFeature>()?.point -public val OptimizationProblem.resultPoint: Map - get() = resultPointOrNull ?: error("Result is not present in $this") +/** + * Covariance matrix for optimization + */ +public class OptimizationCovariance : OptimizationAttribute>, + PolymorphicAttribute>(safeTypeOf()) -public class OptimizationLog(private val loggable: Loggable) : Loggable by loggable, OptimizationFeature { - override fun toString(): String = "Log($loggable)" +public fun AttributesBuilder>.covariance(covariance: NamedMatrix) { + set(OptimizationCovariance(),covariance) } + +public class OptimizationResult() : OptimizationAttribute>, + PolymorphicAttribute>(safeTypeOf()) + +public fun AttributesBuilder>.result(result: Map) { + set(OptimizationResult(), result) +} + +public val OptimizationProblem.resultOrNull: Map? get() = attributes[OptimizationResult()] + +public val OptimizationProblem.result: Map + get() = resultOrNull ?: error("Result is not present in $this") + +public object OptimizationLog : OptimizationAttribute + /** * Free parameters of the optimization */ -public class OptimizationParameters(public val symbols: List) : OptimizationFeature { - public constructor(vararg symbols: Symbol) : this(listOf(*symbols)) - - override fun toString(): String = "Parameters($symbols)" -} +public object OptimizationParameters : OptimizationAttribute> /** * Maximum allowed number of iterations */ -public class OptimizationIterations(public val maxIterations: Int) : OptimizationFeature { - override fun toString(): String = "Iterations($maxIterations)" -} +public object OptimizationIterations : OptimizationAttribute diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt index 41dcbf770..ed8242929 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimizer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt index e922fd423..eba637a83 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -16,13 +16,7 @@ import space.kscience.kmath.structures.Float64Buffer import kotlin.math.abs -public class QowRuns(public val runs: Int) : OptimizationFeature { - init { - require(runs >= 1) { "Number of runs must be more than zero" } - } - - override fun toString(): String = "QowRuns(runs=$runs)" -} +public object QowRuns: OptimizationAttribute /** @@ -69,7 +63,7 @@ public object QowOptimizer : Optimizer { } val prior: DifferentiableExpression? - get() = problem.getFeature>()?.withDefaultArgs(allParameters) + get() = problem.attributes[OptimizationPrior()]?.withDefaultArgs(allParameters) override fun toString(): String = freeParameters.toString() } @@ -176,7 +170,7 @@ public object QowOptimizer : Optimizer { fast: Boolean = false, ): QoWeight { - val logger = problem.getFeature() + val logger = problem.attributes[OptimizationLog] var dis: Double //discrepancy value @@ -231,7 +225,7 @@ public object QowOptimizer : Optimizer { } private fun QoWeight.covariance(): NamedMatrix { - val logger = problem.getFeature() + val logger = problem.attributes[OptimizationLog] logger?.log { """ @@ -257,11 +251,11 @@ public object QowOptimizer : Optimizer { } override suspend fun optimize(problem: XYFit): XYFit { - val qowRuns = problem.getFeature()?.runs ?: 2 - val iterations = problem.getFeature()?.maxIterations ?: 50 + val qowRuns = problem.attributes[QowRuns] ?: 2 + val iterations = problem.attributes[OptimizationIterations] ?: 50 - val freeParameters: Map = problem.getFeature()?.let { op -> - problem.startPoint.filterKeys { it in op.symbols } + val freeParameters: Map = problem.attributes[OptimizationParameters]?.let { symbols -> + problem.startPoint.filterKeys { it in symbols } } ?: problem.startPoint var qow = QoWeight(problem, freeParameters) @@ -270,7 +264,10 @@ public object QowOptimizer : Optimizer { qow = QoWeight(problem, res.freeParameters) res = qow.newtonianRun(maxSteps = iterations) } - val covariance = res.covariance() - return res.problem.withFeature(OptimizationResult(res.freeParameters), OptimizationCovariance(covariance)) + + return res.problem.withAttributes { + result(res.freeParameters) + covariance(res.covariance()) + } } } \ No newline at end of file diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/XYFit.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/XYFit.kt index 9e5396491..3861a796d 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/XYFit.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/XYFit.kt @@ -1,38 +1,42 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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:OptIn(UnstableKMathAPI::class) +@file:OptIn(UnstableKMathAPI::class, UnstableKMathAPI::class) package space.kscience.kmath.optimization +import space.kscience.attributes.* import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.data.XYColumnarData import space.kscience.kmath.data.indices import space.kscience.kmath.expressions.* -import space.kscience.kmath.misc.FeatureSet import space.kscience.kmath.misc.Loggable +import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.ExtendedField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.bindSymbol import kotlin.math.pow /** * Specify the way to compute distance from point to the curve as DifferentiableExpression */ -public interface PointToCurveDistance : OptimizationFeature { +public interface PointToCurveDistance { public fun distance(problem: XYFit, index: Int): DifferentiableExpression - public companion object { + public companion object : OptimizationAttribute { public val byY: PointToCurveDistance = object : PointToCurveDistance { override fun distance(problem: XYFit, index: Int): DifferentiableExpression { val x = problem.data.x[index] val y = problem.data.y[index] return object : DifferentiableExpression { + override val type: SafeType get() = DoubleField.type + override fun derivativeOrNull( symbols: List, ): Expression? = problem.model.derivativeOrNull(symbols)?.let { derivExpression -> - Expression { arguments -> + Expression(DoubleField.type) { arguments -> derivExpression.invoke(arguments + (Symbol.x to x)) } } @@ -51,18 +55,21 @@ public interface PointToCurveDistance : OptimizationFeature { * Compute a wight of the point. The more the weight, the more impact this point will have on the fit. * By default, uses Dispersion^-1 */ -public interface PointWeight : OptimizationFeature { +public interface PointWeight { public fun weight(problem: XYFit, index: Int): DifferentiableExpression - public companion object { + public companion object : OptimizationAttribute { public fun bySigma(sigmaSymbol: Symbol): PointWeight = object : PointWeight { override fun weight(problem: XYFit, index: Int): DifferentiableExpression = object : DifferentiableExpression { + override val type: SafeType get() = DoubleField.type + override fun invoke(arguments: Map): Double { return problem.data[sigmaSymbol]?.get(index)?.pow(-2) ?: 1.0 } - override fun derivativeOrNull(symbols: List): Expression = Expression { 0.0 } + override fun derivativeOrNull(symbols: List): Expression = + Expression(DoubleField.type) { 0.0 } } override fun toString(): String = "PointWeightBySigma($sigmaSymbol)" @@ -79,41 +86,52 @@ public interface PointWeight : OptimizationFeature { public class XYFit( public val data: XYColumnarData, public val model: DifferentiableExpression, - override val features: FeatureSet, + override val attributes: Attributes, internal val pointToCurveDistance: PointToCurveDistance = PointToCurveDistance.byY, internal val pointWeight: PointWeight = PointWeight.byYSigma, public val xSymbol: Symbol = Symbol.x, ) : OptimizationProblem { + + override val type: SafeType get() = Float64Field.type + public fun distance(index: Int): DifferentiableExpression = pointToCurveDistance.distance(this, index) public fun weight(index: Int): DifferentiableExpression = pointWeight.weight(this, index) } -public fun XYFit.withFeature(vararg features: OptimizationFeature): XYFit { - return XYFit(data, model, this.features.with(*features), pointToCurveDistance, pointWeight) -} + +public fun XYOptimization( + data: XYColumnarData, + model: DifferentiableExpression, + builder: AttributesBuilder.() -> Unit, +): XYFit = XYFit(data, model, Attributes(builder)) + +public fun XYFit.withAttributes( + modifier: AttributesBuilder.() -> Unit, +): XYFit = XYFit(data, model, attributes.modify(modifier), pointToCurveDistance, pointWeight, xSymbol) public suspend fun XYColumnarData.fitWith( optimizer: Optimizer, modelExpression: DifferentiableExpression, startingPoint: Map, - vararg features: OptimizationFeature = emptyArray(), + attributes: Attributes = Attributes.EMPTY, xSymbol: Symbol = Symbol.x, pointToCurveDistance: PointToCurveDistance = PointToCurveDistance.byY, pointWeight: PointWeight = PointWeight.byYSigma, ): XYFit { - var actualFeatures = FeatureSet.of(*features, OptimizationStartPoint(startingPoint)) - if (actualFeatures.getFeature() == null) { - actualFeatures = actualFeatures.with(OptimizationLog(Loggable.console)) - } val problem = XYFit( this, modelExpression, - actualFeatures, + attributes.modify { + set(::OptimizationStartPoint, startingPoint) + if (!hasAny()) { + set(OptimizationLog, Loggable.console) + } + }, pointToCurveDistance, pointWeight, - xSymbol + xSymbol, ) return optimizer.optimize(problem) } @@ -125,7 +143,7 @@ public suspend fun XYColumnarData.fitWith( optimizer: Optimizer, processor: AutoDiffProcessor, startingPoint: Map, - vararg features: OptimizationFeature = emptyArray(), + attributes: Attributes = Attributes.EMPTY, xSymbol: Symbol = Symbol.x, pointToCurveDistance: PointToCurveDistance = PointToCurveDistance.byY, pointWeight: PointWeight = PointWeight.byYSigma, @@ -140,7 +158,7 @@ public suspend fun XYColumnarData.fitWith( optimizer = optimizer, modelExpression = modelExpression, startingPoint = startingPoint, - features = features, + attributes = attributes, xSymbol = xSymbol, pointToCurveDistance = pointToCurveDistance, pointWeight = pointWeight @@ -152,7 +170,7 @@ public suspend fun XYColumnarData.fitWith( */ public val XYFit.chiSquaredOrNull: Double? get() { - val result = startPoint + (resultPointOrNull ?: return null) + val result = startPoint + (resultOrNull ?: return null) return data.indices.sumOf { index -> @@ -167,4 +185,4 @@ public val XYFit.chiSquaredOrNull: Double? } public val XYFit.dof: Int - get() = data.size - (getFeature()?.symbols?.size ?: startPoint.size) \ No newline at end of file + get() = data.size - (attributes[OptimizationParameters]?.size ?: startPoint.size) \ No newline at end of file diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/logLikelihood.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/logLikelihood.kt index 8ab9de48d..98ec198da 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/logLikelihood.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/logLikelihood.kt @@ -1,10 +1,12 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.optimization +import space.kscience.attributes.AttributesBuilder +import space.kscience.attributes.SafeType import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.data.XYColumnarData import space.kscience.kmath.data.indices @@ -12,6 +14,7 @@ import space.kscience.kmath.expressions.DifferentiableExpression import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.expressions.derivative +import space.kscience.kmath.operations.Float64Field import kotlin.math.PI import kotlin.math.ln import kotlin.math.pow @@ -22,7 +25,9 @@ private val oneOver2Pi = 1.0 / sqrt(2 * PI) @UnstableKMathAPI internal fun XYFit.logLikelihood(): DifferentiableExpression = object : DifferentiableExpression { - override fun derivativeOrNull(symbols: List): Expression = Expression { arguments -> + override val type: SafeType get() = Float64Field.type + + override fun derivativeOrNull(symbols: List): Expression = Expression(type) { arguments -> data.indices.sumOf { index -> val d = distance(index)(arguments) val weight = weight(index)(arguments) @@ -53,14 +58,18 @@ internal fun XYFit.logLikelihood(): DifferentiableExpression = object : */ @UnstableKMathAPI public suspend fun Optimizer>.maximumLogLikelihood(problem: XYFit): XYFit { - val functionOptimization = FunctionOptimization(problem.features, problem.logLikelihood()) - val result = optimize(functionOptimization.withFeatures(FunctionOptimizationTarget.MAXIMIZE)) - return XYFit(problem.data, problem.model, result.features) + val functionOptimization = FunctionOptimization(problem.logLikelihood(), problem.attributes) + val result = optimize( + functionOptimization.withAttributes { + FunctionOptimizationTarget(OptimizationDirection.MAXIMIZE) + } + ) + return XYFit(problem.data,problem.model, result.attributes) } @UnstableKMathAPI public suspend fun Optimizer>.maximumLogLikelihood( data: XYColumnarData, model: DifferentiableExpression, - builder: XYOptimizationBuilder.() -> Unit, + builder: AttributesBuilder.() -> Unit, ): XYFit = maximumLogLikelihood(XYOptimization(data, model, builder)) diff --git a/kmath-stat/README.md b/kmath-stat/README.md index e7e0a4d92..55ef0f693 100644 --- a/kmath-stat/README.md +++ b/kmath-stat/README.md @@ -6,19 +6,8 @@ ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-stat:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-stat:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-stat:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-stat:0.4.0-dev-1") + implementation("space.kscience:kmath-stat:0.4.0-dev-3") } ``` diff --git a/kmath-stat/build.gradle.kts b/kmath-stat/build.gradle.kts index 000280def..7d396b460 100644 --- a/kmath-stat/build.gradle.kts +++ b/kmath-stat/build.gradle.kts @@ -6,6 +6,7 @@ kscience{ jvm() js() native() + wasm() } kotlin.sourceSets { diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/Distribution.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/Distribution.kt index 806da5560..979c7a002 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/Distribution.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/Distribution.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/FactorizedDistribution.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/FactorizedDistribution.kt index 999fbffbc..8f5a378bb 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/FactorizedDistribution.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/FactorizedDistribution.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt index 10947b732..f2a1bbfe8 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt @@ -1,17 +1,16 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.distributions +import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.Chain import space.kscience.kmath.operations.Float64Field.pow import space.kscience.kmath.random.RandomGenerator -import space.kscience.kmath.samplers.GaussianSampler +import space.kscience.kmath.samplers.* import space.kscience.kmath.samplers.InternalErf -import space.kscience.kmath.samplers.NormalizedGaussianSampler -import space.kscience.kmath.samplers.ZigguratNormalizedGaussianSampler import kotlin.math.* /** @@ -24,7 +23,7 @@ public class NormalDistribution(public val sampler: GaussianSampler) : Distribut return exp(-0.5 * x1 * x1 - (ln(sampler.standardDeviation) + 0.5 * ln(2 * PI))) } - override fun sample(generator: RandomGenerator): Chain = sampler.sample(generator) + override fun sample(generator: RandomGenerator): BlockingDoubleChain = sampler.sample(generator) override fun cumulative(arg: Double): Double { val dev = arg - sampler.mean diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/UniformDistribution.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/UniformDistribution.kt index 953be06fd..d0f13398c 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/UniformDistribution.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/UniformDistribution.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt index 2049a84fc..149b5d85d 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt index 1cc409393..5d175c5ae 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt index ece6bfcc1..8d8fd8612 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt index 04e1221ca..0276cd654 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterMarsagliaTsangGammaSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterMarsagliaTsangGammaSampler.kt index d301ff637..05163387d 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterMarsagliaTsangGammaSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterMarsagliaTsangGammaSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AliasMethodDiscreteSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AliasMethodDiscreteSampler.kt index 2ec40c347..f65e8585b 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AliasMethodDiscreteSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AliasMethodDiscreteSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt index c8ee745dd..9f90d1626 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/GaussianSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/GaussianSampler.kt index 28d588165..c86c6aa35 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/GaussianSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/GaussianSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalErf.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalErf.kt index 0c1a5b36f..c28b4be50 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalErf.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalErf.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalGamma.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalGamma.kt index 43c5a0d3c..ef4a97066 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalGamma.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalGamma.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalUtils.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalUtils.kt index 9f633e3db..cbc361b56 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalUtils.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/InternalUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt index 44834b5bf..7a076d849 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt index 961883af5..b1343a4ca 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/NormalizedGaussianSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/NormalizedGaussianSampler.kt index 291d0bffe..81d1edb47 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/NormalizedGaussianSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/NormalizedGaussianSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt index 964a57178..8faaf33dc 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt index 34355dca7..146a3703f 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -12,8 +12,6 @@ import space.kscience.kmath.chains.combine import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.Float64Buffer -import space.kscience.kmath.structures.Int32Buffer import kotlin.jvm.JvmName /** @@ -36,7 +34,7 @@ public fun interface Sampler { public fun Sampler.sampleBuffer( generator: RandomGenerator, size: Int, - bufferFactory: BufferFactory = BufferFactory.boxing(), + bufferFactory: BufferFactory ): Chain> { require(size > 1) //creating temporary storage once @@ -58,18 +56,11 @@ public fun Sampler.sampleBuffer( public suspend fun Sampler.next(generator: RandomGenerator): T = sample(generator).first() /** - * Generates [size] real samples and chunks them into some buffers. + * Generates [size] samples and chunks them into some buffers. */ @JvmName("sampleRealBuffer") -public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = - sampleBuffer(generator, size, ::Float64Buffer) - -/** - * Generates [size] integer samples and chunks them into some buffers. - */ -@JvmName("sampleIntBuffer") -public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = - sampleBuffer(generator, size, ::Int32Buffer) +public inline fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = + sampleBuffer(generator, size, BufferFactory()) /** diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/SamplerAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/SamplerAlgebra.kt index 44b87a431..26d1b89a8 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/SamplerAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/SamplerAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,6 +14,7 @@ import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.invoke import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.Sampler +import space.kscience.kmath.structures.MutableBufferFactory /** * Implements [Sampler] by sampling only certain [value]. @@ -41,6 +42,8 @@ public class BasicSampler(public val chainBuilder: (RandomGenerator public class SamplerSpace(public val algebra: S) : Group>, ScaleOperations> where S : Group, S : ScaleOperations { + override val bufferFactory: MutableBufferFactory> = MutableBufferFactory() + override val zero: Sampler = ConstantSampler(algebra.zero) override fun add(left: Sampler, right: Sampler): Sampler = BasicSampler { generator -> diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt index 062894c90..db04d94bd 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/MonotonicSeriesAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/MonotonicSeriesAlgebra.kt index ed6f4e07b..bd77974d1 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/MonotonicSeriesAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/MonotonicSeriesAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt index cabff25e6..6790f44e0 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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.series import space.kscience.kmath.operations.BufferAlgebra diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt index 6cd64df9c..337b13020 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/resampling.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/resampling.kt index dc21fe6d9..9931429e7 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/resampling.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/resampling.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/seriesExtensions.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/seriesExtensions.kt index fa5e0addd..92e3cf534 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/seriesExtensions.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/seriesExtensions.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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.series import space.kscience.kmath.operations.BufferAlgebra diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt index 74ef65fa1..843b06f2c 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt index dc504f0b7..f0328839f 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt index 5a873b466..cb309663e 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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 space.kscience.kmath.operations.asIterable diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt index 7cb40a91a..81ed258f7 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/StatisticalAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/StatisticalAlgebra.kt index cce61519b..9f0491b4b 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/StatisticalAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/StatisticalAlgebra.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2018-2024 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 space.kscience.kmath.UnstableKMathAPI diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/ValueAndErrorField.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/ValueAndErrorField.kt index 38cd5f900..8f0aa1729 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/ValueAndErrorField.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/ValueAndErrorField.kt @@ -1,14 +1,18 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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 space.kscience.attributes.SafeType +import space.kscience.attributes.safeTypeOf import space.kscience.kmath.operations.Field +import space.kscience.kmath.structures.* import kotlin.math.pow import kotlin.math.sqrt + /** * A combination of a random [value] and its [dispersion]. * @@ -53,4 +57,46 @@ public object ValueAndErrorField : Field { override fun scale(a: ValueAndError, value: Double): ValueAndError = ValueAndError(a.value * value, a.dispersion * value.pow(2)) + + + private class ValueAndErrorBuffer(val values: Float64Buffer, val ds: Float64Buffer) : MutableBuffer { + init { + require(values.size == ds.size) + } + + override val size: Int + get() = values.size + + override fun get(index: Int): ValueAndError = ValueAndError(values[index], ds[index]) + + override fun toString(): String = Buffer.toString(this) + + override fun set(index: Int, value: ValueAndError) { + values[index] = value.value + values[index] = value.dispersion + } + + } + + override val bufferFactory: MutableBufferFactory = object : MutableBufferFactory { + override fun invoke( + size: Int, + builder: (Int) -> ValueAndError, + ): MutableBuffer { + val values: DoubleArray = DoubleArray(size) + val ds = DoubleArray(size) + repeat(size){ + val (v, d) = builder(it) + values[it] = v + ds[it] = d + } + return ValueAndErrorBuffer( + values.asBuffer(), + ds.asBuffer() + ) + } + + override val type: SafeType get() = safeTypeOf() + + } } \ No newline at end of file diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/chiSquaredExpression.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/chiSquaredExpression.kt index ca9755ad5..c42f149f2 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/chiSquaredExpression.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/chiSquaredExpression.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestSeries.kt b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestSeries.kt index d83abb3f4..68c5af6fb 100644 --- a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestSeries.kt +++ b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestSeries.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt index afc0d541d..b5ac44bc6 100644 --- a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt +++ b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt index 306574a76..0efc3c269 100644 --- a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt +++ b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/jvmMain/kotlin/space/kscience/kmath/stat/RandomSourceGenerator.kt b/kmath-stat/src/jvmMain/kotlin/space/kscience/kmath/stat/RandomSourceGenerator.kt index 2c6391612..8badbce64 100644 --- a/kmath-stat/src/jvmMain/kotlin/space/kscience/kmath/stat/RandomSourceGenerator.kt +++ b/kmath-stat/src/jvmMain/kotlin/space/kscience/kmath/stat/RandomSourceGenerator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt index b9b9dadba..227214d63 100644 --- a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt +++ b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/MCScopeTest.kt b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/MCScopeTest.kt index 5eba0fed8..a95829eff 100644 --- a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/MCScopeTest.kt +++ b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/MCScopeTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/SamplerTest.kt b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/SamplerTest.kt index 0076006e6..fd2db98ae 100644 --- a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/SamplerTest.kt +++ b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/SamplerTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/StatisticTest.kt b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/StatisticTest.kt index d64dcea4d..fb9a4351a 100644 --- a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/StatisticTest.kt +++ b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/StatisticTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-symja/README.md b/kmath-symja/README.md index 8672c6a71..250c0ae35 100644 --- a/kmath-symja/README.md +++ b/kmath-symja/README.md @@ -6,19 +6,8 @@ Symja integration module ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-symja:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-symja:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-symja:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-symja:0.4.0-dev-1") + implementation("space.kscience:kmath-symja:0.4.0-dev-3") } ``` diff --git a/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/SymjaExpression.kt b/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/SymjaExpression.kt index 0f8014913..b23da37f5 100644 --- a/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/SymjaExpression.kt +++ b/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/SymjaExpression.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -7,8 +7,9 @@ package space.kscience.kmath.symja import org.matheclipse.core.eval.ExprEvaluator import org.matheclipse.core.expression.F -import space.kscience.kmath.expressions.SpecialDifferentiableExpression +import space.kscience.attributes.SafeType import space.kscience.kmath.expressions.MST +import space.kscience.kmath.expressions.SpecialDifferentiableExpression import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.expressions.interpret import space.kscience.kmath.operations.NumericAlgebra @@ -30,6 +31,8 @@ public class SymjaExpression>( public val mst: MST, public val evaluator: ExprEvaluator = DEFAULT_EVALUATOR, ) : SpecialDifferentiableExpression> { + override val type: SafeType get() = algebra.type + override fun invoke(arguments: Map): T = mst.interpret(algebra, arguments) override fun derivativeOrNull(symbols: List): SymjaExpression = SymjaExpression( diff --git a/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/adapters.kt b/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/adapters.kt index 92f2474b8..9590f2246 100644 --- a/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/adapters.kt +++ b/kmath-symja/src/main/kotlin/space/kscience/kmath/symja/adapters.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensorflow/README.md b/kmath-tensorflow/README.md index a5b48de4d..03666129d 100644 --- a/kmath-tensorflow/README.md +++ b/kmath-tensorflow/README.md @@ -6,19 +6,8 @@ Google tensorflow connector ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-tensorflow:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-tensorflow:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-tensorflow:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-tensorflow:0.4.0-dev-1") + implementation("space.kscience:kmath-tensorflow:0.4.0-dev-3") } ``` diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt index 027318f99..516a3a2ce 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -14,6 +14,7 @@ import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.nd.ColumnStrides +import space.kscience.kmath.nd.MutableStructureND import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.FieldOps @@ -26,7 +27,6 @@ public class DoubleTensorFlowOutput( ) : TensorFlowOutput(graph, output) { override fun org.tensorflow.Tensor.actualizeTensor(): NdArray = this as TFloat64 - } internal fun ShapeND.toLongArray(): LongArray = LongArray(size) { get(it).toLong() } @@ -38,10 +38,10 @@ public class DoubleTensorFlowAlgebra internal constructor( override val elementAlgebra: Float64Field get() = Float64Field - override fun structureND( + override fun mutableStructureND( shape: ShapeND, initializer: Float64Field.(IntArray) -> Double, - ): StructureND { + ): MutableStructureND { val res = TFloat64.tensorOf(org.tensorflow.ndarray.Shape.of(*shape.toLongArray())) { array -> ColumnStrides(shape).forEach { index -> array.setDouble(elementAlgebra.initializer(index), *index.toLongArray()) diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/IntTensorFlowAlgebra.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/IntTensorFlowAlgebra.kt index 01c8054b3..95ed52638 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/IntTensorFlowAlgebra.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/IntTensorFlowAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -15,6 +15,7 @@ public class IntTensorFlowOutput( graph: Graph, output: Output, ) : TensorFlowOutput(graph, output) { + override fun org.tensorflow.Tensor.actualizeTensor(): NdArray = this as TInt32 } @@ -22,5 +23,6 @@ public class LongTensorFlowOutput( graph: Graph, output: Output, ) : TensorFlowOutput(graph, output) { + override fun org.tensorflow.Tensor.actualizeTensor(): NdArray = this as TInt64 } \ No newline at end of file diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt index 73b36cd67..2a8720275 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -41,6 +41,7 @@ public sealed interface TensorFlowTensor : Tensor */ @JvmInline public value class TensorFlowArray(public val tensor: NdArray) : Tensor { + override val shape: ShapeND get() = ShapeND(tensor.shape().asArray().toIntArray()) @PerformancePitfall diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt index a0a2ddc80..885b9fca6 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt b/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt index fe950f334..dd4f620c4 100644 --- a/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt +++ b/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/README.md b/kmath-tensors/README.md index 80f751ffe..34cf33cb3 100644 --- a/kmath-tensors/README.md +++ b/kmath-tensors/README.md @@ -9,19 +9,8 @@ Common linear algebra operations on tensors. ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-tensors:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-tensors:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-tensors:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -30,6 +19,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-tensors:0.4.0-dev-1") + implementation("space.kscience:kmath-tensors:0.4.0-dev-3") } ``` diff --git a/kmath-tensors/build.gradle.kts b/kmath-tensors/build.gradle.kts index 2497314f0..82213f408 100644 --- a/kmath-tensors/build.gradle.kts +++ b/kmath-tensors/build.gradle.kts @@ -6,12 +6,13 @@ kscience{ jvm() js { browser { - testTask { + testTask{ useMocha().timeout = "0" } } } native() + wasm() dependencies { api(projects.kmathCore) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/AnalyticTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/AnalyticTensorAlgebra.kt index 1a324b200..a986edfa5 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/AnalyticTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/AnalyticTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/LinearOpsTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/LinearOpsTensorAlgebra.kt index f2c7f1821..f9f0ed832 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/LinearOpsTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/LinearOpsTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/Tensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/Tensor.kt deleted file mode 100644 index b328fbeec..000000000 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/Tensor.kt +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright 2018-2022 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.api - -import space.kscience.kmath.nd.MutableStructureND - -public typealias Tensor = MutableStructureND diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt index f923400c5..c960090d2 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt @@ -1,15 +1,18 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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.api +import space.kscience.kmath.nd.MutableStructureND import space.kscience.kmath.nd.RingOpsND import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.Ring +public typealias Tensor = MutableStructureND + /** * Algebra over a ring on [Tensor]. * For more information: https://proofwiki.org/wiki/Definition:Algebra_over_Ring diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt index 33effb2d2..df8c32007 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt index 662469bf3..d02a9f99e 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt index eaec43e2c..a595c94f5 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt index 8a97114c3..b3c2eb1b4 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -34,7 +34,7 @@ public class OffsetDoubleBuffer( /** * Copy only a part of buffer that belongs to this [OffsetDoubleBuffer] */ - override fun copy(): Float64Buffer = origin.array.copyOfRange(offset, offset + size).asBuffer() + public fun copy(): Float64Buffer = origin.array.copyOfRange(offset, offset + size).asBuffer() override fun iterator(): Iterator = iterator { for (i in indices) { diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor1D.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor1D.kt index d2066e404..05f1b2f20 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor1D.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor1D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -8,7 +8,6 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.MutableStructure1D import space.kscience.kmath.nd.ShapeND -import space.kscience.kmath.structures.MutableBuffer public class DoubleTensor1D( source: OffsetDoubleBuffer, @@ -30,8 +29,6 @@ public class DoubleTensor1D( source[index] = value } - override fun copy(): MutableBuffer = source.copy() - @PerformancePitfall override fun elements(): Sequence> = super.elements() } diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor2D.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor2D.kt index fa142afa0..013dfe791 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor2D.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor2D.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index 334a27c5b..cea77f4ab 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -127,7 +127,7 @@ public open class DoubleTensorAlgebra : * @param initializer mapping tensor indices to values. * @return tensor with the [shape] shape and data generated by the [initializer]. */ - override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): DoubleTensor = fromArray( + override fun mutableStructureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): DoubleTensor = fromArray( shape, RowStrides(shape).asSequence().map { Float64Field.initializer(it) }.toMutableList().toDoubleArray() ) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt index 1066793a7..eb95d5e70 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -34,7 +34,7 @@ public class OffsetIntBuffer( /** * Copy only a part of buffer that belongs to this tensor */ - override fun copy(): Int32Buffer = source.array.copyOfRange(offset, offset + size).asBuffer() + public fun copy(): Int32Buffer = source.array.copyOfRange(offset, offset + size).asBuffer() override fun iterator(): Iterator = iterator { for (i in indices) { @@ -83,6 +83,7 @@ public class IntTensor( require(linearSize == source.size) { "Source buffer size must be equal tensor size" } } + public constructor(shape: ShapeND, buffer: Int32Buffer) : this(shape, OffsetIntBuffer(buffer, 0, buffer.size)) @OptIn(PerformancePitfall::class) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt index a09351b85..eaeb43fdc 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -118,7 +118,7 @@ public open class IntTensorAlgebra : TensorAlgebra { * @param initializer mapping tensor indices to values. * @return tensor with the [shape] shape and data generated by the [initializer]. */ - override fun structureND(shape: ShapeND, initializer: Int32Ring.(IntArray) -> Int): IntTensor = fromArray( + override fun mutableStructureND(shape: ShapeND, initializer: Int32Ring.(IntArray) -> Int): IntTensor = fromArray( shape, RowStrides(shape).asSequence().map { Int32Ring.initializer(it) }.toMutableList().toIntArray() ) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/LevenbergMarquardtAlgorithm.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/LevenbergMarquardtAlgorithm.kt index fc87ad1f3..b2106200e 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/LevenbergMarquardtAlgorithm.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/LevenbergMarquardtAlgorithm.kt @@ -1,12 +1,11 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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.core import space.kscience.kmath.PerformancePitfall -import space.kscience.kmath.linear.transpose import space.kscience.kmath.nd.* import kotlin.math.abs import kotlin.math.max @@ -139,7 +138,7 @@ public fun DoubleTensorAlgebra.levenbergMarquardt(inputData: LMInput): LMResultI if (inputData.nargin < 5) { weight = fromArray( ShapeND(intArrayOf(1, 1)), - doubleArrayOf((inputData.realValues.transpose().dot(inputData.realValues)).as1D()[0]) + doubleArrayOf((inputData.realValues.transposed() dot inputData.realValues).as1D()[0]) ).as2D() } @@ -266,12 +265,12 @@ public fun DoubleTensorAlgebra.levenbergMarquardt(inputData: LMInput): LMResultI settings.funcCalls += 1 // val tmp = deltaY.times(weight) - var X2Try = deltaY.as2D().transpose().dot(deltaY.times(weight)) // Chi-squared error criteria + var X2Try = deltaY.as2D().transposed() dot deltaY.times(weight) // Chi-squared error criteria val alpha = 1.0 if (updateType == 2) { // Quadratic // One step of quadratic line update in the h direction for minimum X2 - val alphaTensor = (jtWdy.transpose() dot h) / ((X2Try - x2) / 2.0 + 2 * (jtWdy.transpose() dot h)) + val alphaTensor = (jtWdy.transposed() dot h) / ((X2Try - x2) / 2.0 + 2 * (jtWdy.transposed() dot h)) h = h dot alphaTensor pTry = (p + h).as2D() // update only [idx] elements pTry = smallestElementComparison( @@ -289,7 +288,7 @@ public fun DoubleTensorAlgebra.levenbergMarquardt(inputData: LMInput): LMResultI ) // residual error using p_try settings.funcCalls += 1 - X2Try = deltaY.as2D().transpose() dot deltaY * weight // Chi-squared error criteria + X2Try = deltaY.as2D().transposed() dot deltaY * weight // Chi-squared error criteria } val rho = when (updateType) { // Nielsen diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/broadcastUtils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/broadcastUtils.kt index 1e87e6620..3596cbaf8 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/broadcastUtils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/broadcastUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/checks.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/checks.kt index f384ed462..1a0aa29d1 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/checks.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/checks.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt index 0c1ffdbf3..4750dbea2 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt index c47d94a92..14ab35883 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt index 272af41d5..2a3ed87c9 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -310,32 +310,26 @@ internal fun DoubleTensorAlgebra.svdHelper( } } -private fun pythag(a: Double, b: Double): Double { - val at: Double = abs(a) - val bt: Double = abs(b) - val ct: Double - val result: Double - if (at > bt) { - ct = bt / at - result = at * sqrt(1.0 + ct * ct) - } else if (bt > 0.0) { - ct = at / bt - result = bt * sqrt(1.0 + ct * ct) - } else result = 0.0 - return result -} - -private fun SIGN(a: Double, b: Double): Double { - if (b >= 0.0) - return abs(a) - else - return -abs(a) -} - internal fun MutableStructure2D.svdGolubKahanHelper( u: MutableStructure2D, w: BufferedTensor, v: MutableStructure2D, iterations: Int, epsilon: Double, ) { + fun pythag(a: Double, b: Double): Double { + val at: Double = abs(a) + val bt: Double = abs(b) + val ct: Double + val result: Double + if (at > bt) { + ct = bt / at + result = at * sqrt(1.0 + ct * ct) + } else if (bt > 0.0) { + ct = at / bt + result = bt * sqrt(1.0 + ct * ct) + } else result = 0.0 + return result + } + + val shape = this.shape val m = shape.component1() val n = shape.component2() @@ -553,7 +547,7 @@ internal fun MutableStructure2D.svdGolubKahanHelper( h = rv1[k] f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y) g = pythag(f, 1.0) - f = ((x - z) * (x + z) + h * ((y / (f + SIGN(g, f))) - h)) / x + f = ((x - z) * (x + z) + h * ((y / (f + if (f >= 0.0) abs(g) else -abs(g) )) - h)) / x c = 1.0 s = 1.0 @@ -563,7 +557,7 @@ internal fun MutableStructure2D.svdGolubKahanHelper( g = rv1[i] y = wBuffer[wStart + i] h = s * g - g = c * g + g *= c z = pythag(f, h) rv1[j] = z c = f / z diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt index d48086d57..4dc6b8517 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorAlgebraExtensions.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorAlgebraExtensions.kt index e2b7c23e6..841c51244 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorAlgebraExtensions.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorAlgebraExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt index 1b630a0ef..2c35f8196 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt index 52a530652..6490ecdb9 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -17,7 +17,7 @@ import space.kscience.kmath.tensors.api.Tensor public fun StructureND.copyToTensor(): DoubleTensor = if (this is DoubleTensor) { DoubleTensor(shape, source.copy()) } else if (this is Float64BufferND && indices is RowStrides) { - DoubleTensor(shape, buffer.copy()) + DoubleTensor(shape, buffer.array.copyOf().asBuffer()) } else { DoubleTensor( shape, diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt index 73aed8a7b..f930cf94c 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt index e4c2c40ea..7fd011986 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt index cbd7b9887..1c500af8b 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt index 3081639af..92d997468 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -92,7 +92,7 @@ internal class TestDoubleTensor { @Test fun test2D() = with(DoubleTensorAlgebra) { - val tensor: DoubleTensor = structureND(ShapeND(3, 3)) { (i, j) -> (i - j).toDouble() } + val tensor: DoubleTensor = mutableStructureND(ShapeND(3, 3)) { (i, j) -> (i - j).toDouble() } //println(tensor.toPrettyString()) val tensor2d = tensor.asDoubleTensor2D() assertBufferEquals(Float64Buffer(1.0, 0.0, -1.0), tensor2d.rows[1]) @@ -101,7 +101,7 @@ internal class TestDoubleTensor { @Test fun testMatrixIteration() = with(DoubleTensorAlgebra) { - val tensor = structureND(ShapeND(3, 3, 3, 3)) { index -> index.sum().toDouble() } + val tensor = mutableStructureND(ShapeND(3, 3, 3, 3)) { index -> index.sum().toDouble() } tensor.forEachMatrix { index, matrix -> println(index.joinToString { it.toString() }) println(matrix) diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt index 7222fc7a6..4f58846aa 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt index c3127ed97..38c51d66e 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt b/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt index 0d0332395..6c072d52b 100644 --- a/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt +++ b/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/kmath-viktor/README.md b/kmath-viktor/README.md index 8a781af4c..3b7b8152a 100644 --- a/kmath-viktor/README.md +++ b/kmath-viktor/README.md @@ -6,19 +6,8 @@ Binding for https://github.com/JetBrains-Research/viktor ## Artifact: -The Maven coordinates of this project are `space.kscience:kmath-viktor:0.4.0-dev-1`. +The Maven coordinates of this project are `space.kscience:kmath-viktor:0.4.0-dev-3`. -**Gradle Groovy:** -```groovy -repositories { - maven { url 'https://repo.kotlin.link' } - mavenCentral() -} - -dependencies { - implementation 'space.kscience:kmath-viktor:0.4.0-dev-1' -} -``` **Gradle Kotlin DSL:** ```kotlin repositories { @@ -27,6 +16,6 @@ repositories { } dependencies { - implementation("space.kscience:kmath-viktor:0.4.0-dev-1") + implementation("space.kscience:kmath-viktor:0.4.0-dev-3") } ``` diff --git a/kmath-viktor/api/kmath-viktor.api b/kmath-viktor/api/kmath-viktor.api index 39ae1f84c..0ad7368a9 100644 --- a/kmath-viktor/api/kmath-viktor.api +++ b/kmath-viktor/api/kmath-viktor.api @@ -1,8 +1,6 @@ public final class space/kscience/kmath/viktor/ViktorBuffer : space/kscience/kmath/structures/MutableBuffer { public static final synthetic fun box-impl (Lorg/jetbrains/bio/viktor/F64FlatArray;)Lspace/kscience/kmath/viktor/ViktorBuffer; public static fun constructor-impl (Lorg/jetbrains/bio/viktor/F64FlatArray;)Lorg/jetbrains/bio/viktor/F64FlatArray; - public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer; - public static fun copy-impl (Lorg/jetbrains/bio/viktor/F64FlatArray;)Lspace/kscience/kmath/structures/MutableBuffer; public fun equals (Ljava/lang/Object;)Z public static fun equals-impl (Lorg/jetbrains/bio/viktor/F64FlatArray;Ljava/lang/Object;)Z public static final fun equals-impl0 (Lorg/jetbrains/bio/viktor/F64FlatArray;Lorg/jetbrains/bio/viktor/F64FlatArray;)Z @@ -62,7 +60,7 @@ public class space/kscience/kmath/viktor/ViktorFieldOpsND : space/kscience/kmath public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun getElementAlgebra ()Lspace/kscience/kmath/operations/Algebra; - public fun getElementAlgebra ()Lspace/kscience/kmath/operations/DoubleField; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Float64Field; public final fun getF64Buffer (Lspace/kscience/kmath/nd/StructureND;)Lorg/jetbrains/bio/viktor/F64Array; public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND; @@ -72,6 +70,8 @@ public class space/kscience/kmath/viktor/ViktorFieldOpsND : space/kscience/kmath public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND; + public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND; + public fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun plus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; @@ -85,8 +85,6 @@ public class space/kscience/kmath/viktor/ViktorFieldOpsND : space/kscience/kmath public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND; - public synthetic fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; - public fun structureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; public fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND; public synthetic fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; @@ -102,8 +100,8 @@ public final class space/kscience/kmath/viktor/ViktorFieldOpsND$Companion : spac public final class space/kscience/kmath/viktor/ViktorFieldOpsNDKt { public static final fun ViktorFieldND ([I)Lspace/kscience/kmath/viktor/ViktorFieldND; - public static final fun getViktorAlgebra (Lspace/kscience/kmath/operations/DoubleField;)Lspace/kscience/kmath/viktor/ViktorFieldOpsND; - public static final fun viktorAlgebra (Lspace/kscience/kmath/operations/DoubleField;[I)Lspace/kscience/kmath/viktor/ViktorFieldND; + public static final fun getViktorAlgebra (Lspace/kscience/kmath/operations/Float64Field;)Lspace/kscience/kmath/viktor/ViktorFieldOpsND; + public static final fun viktorAlgebra (Lspace/kscience/kmath/operations/Float64Field;[I)Lspace/kscience/kmath/viktor/ViktorFieldND; } public final class space/kscience/kmath/viktor/ViktorStructureND : space/kscience/kmath/nd/MutableStructureND { diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorBuffer.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorBuffer.kt index 52dc1e192..89b51f269 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorBuffer.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorBuffer.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -12,6 +12,7 @@ import space.kscience.kmath.structures.MutableBuffer @Suppress("NOTHING_TO_INLINE", "OVERRIDE_BY_INLINE") @JvmInline public value class ViktorBuffer(public val flatArray: F64FlatArray) : MutableBuffer { + override val size: Int get() = flatArray.length @@ -21,7 +22,6 @@ public value class ViktorBuffer(public val flatArray: F64FlatArray) : MutableBuf flatArray[index] = value } - override fun copy(): MutableBuffer = ViktorBuffer(flatArray.copy().flatten()) override operator fun iterator(): Iterator = flatArray.data.iterator() override fun toString(): String = Buffer.toString(this) diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt index f93066423..1d95546ac 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ @@ -17,8 +17,7 @@ import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.PowerOperations -@OptIn(UnstableKMathAPI::class, PerformancePitfall::class) -@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") +@OptIn(PerformancePitfall::class) public open class ViktorFieldOpsND : FieldOpsND, ExtendedFieldOps>, @@ -27,13 +26,13 @@ public open class ViktorFieldOpsND : public val StructureND.f64Buffer: F64Array get() = when (this) { is ViktorStructureND -> this.f64Buffer - else -> structureND(shape) { this@f64Buffer[it] }.f64Buffer + else -> mutableStructureND(shape) { this@f64Buffer[it] }.f64Buffer } override val elementAlgebra: Float64Field get() = Float64Field @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): ViktorStructureND = + override fun mutableStructureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): ViktorStructureND = F64Array(*shape.asArray()).apply { ColumnStrides(shape).asSequence().forEach { index -> set(value = Float64Field.initializer(index), indices = index) diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt index 7c0c02086..328a0ab01 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/settings.gradle.kts b/settings.gradle.kts index f158f3444..e660bef85 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -21,6 +21,7 @@ dependencyResolutionManagement { include( ":test-utils", + ":attributes-kt", ":kmath-memory", ":kmath-complex", ":kmath-core", diff --git a/test-utils/api/test-utils.api b/test-utils/api/test-utils.api index fc812a9a6..2915fa039 100644 --- a/test-utils/api/test-utils.api +++ b/test-utils/api/test-utils.api @@ -4,7 +4,7 @@ public final class space/kscience/kmath/testutils/AssertsKt { } public final class space/kscience/kmath/testutils/BufferEqualityKt { - public static final fun contentEquals-2c9zdjM ([D[D)Z + public static final fun contentEquals-YsAwp1c ([D[D)Z public static final fun contentEqualsArray ([D[D)Z public static final fun contentEqualsBuffer ([D[D)Z } diff --git a/test-utils/src/commonMain/kotlin/AlgebraicVerifier.kt b/test-utils/src/commonMain/kotlin/AlgebraicVerifier.kt index 261e74f5a..33e7b5972 100644 --- a/test-utils/src/commonMain/kotlin/AlgebraicVerifier.kt +++ b/test-utils/src/commonMain/kotlin/AlgebraicVerifier.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/test-utils/src/commonMain/kotlin/FieldVerifier.kt b/test-utils/src/commonMain/kotlin/FieldVerifier.kt index a03ca0a27..3b3acd32c 100644 --- a/test-utils/src/commonMain/kotlin/FieldVerifier.kt +++ b/test-utils/src/commonMain/kotlin/FieldVerifier.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/test-utils/src/commonMain/kotlin/RingVerifier.kt b/test-utils/src/commonMain/kotlin/RingVerifier.kt index c40075d93..0a1237b60 100644 --- a/test-utils/src/commonMain/kotlin/RingVerifier.kt +++ b/test-utils/src/commonMain/kotlin/RingVerifier.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/test-utils/src/commonMain/kotlin/SpaceVerifier.kt b/test-utils/src/commonMain/kotlin/SpaceVerifier.kt index 01c02997b..ae805db5a 100644 --- a/test-utils/src/commonMain/kotlin/SpaceVerifier.kt +++ b/test-utils/src/commonMain/kotlin/SpaceVerifier.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/test-utils/src/commonMain/kotlin/asserts.kt b/test-utils/src/commonMain/kotlin/asserts.kt index 8ddce517c..54c6301b6 100644 --- a/test-utils/src/commonMain/kotlin/asserts.kt +++ b/test-utils/src/commonMain/kotlin/asserts.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */ diff --git a/test-utils/src/commonMain/kotlin/bufferEquality.kt b/test-utils/src/commonMain/kotlin/bufferEquality.kt index c4485abbd..5b2ef6e76 100644 --- a/test-utils/src/commonMain/kotlin/bufferEquality.kt +++ b/test-utils/src/commonMain/kotlin/bufferEquality.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 KMath contributors. + * Copyright 2018-2024 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. */