From afd590878421093e4203aca8238f964900465a3e Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 7 May 2021 19:59:21 +0700 Subject: [PATCH] Revise grammar of KDoc comments, refresh documentation files --- README.md | 68 ++++++++------ docs/algebra.md | 83 +++++------------ docs/buffers.md | 17 ++-- docs/codestyle.md | 19 ++-- docs/contexts.md | 44 ++++----- docs/expressions.md | 23 ++--- docs/features.md | 14 --- docs/linear.md | 32 ++++--- docs/nd-structure.md | 18 ++-- docs/readme.md | 14 +++ docs/templates/README-TEMPLATE.md | 66 ++++++++------ .../kscience/kmath/ast/kotlingradSupport.kt | 4 +- .../space/kscience/kmath/ast/symjaSupport.kt | 4 +- .../kmath/commons/fit/fitWithAutoDiff.kt | 3 +- .../space/kscience/kmath/jafama/JafamaDemo.kt | 15 ++++ .../kscience/kmath/jafama/KMathaJafamaDemo.kt | 17 ---- .../kscience/kmath/stat/DistributionDemo.kt | 3 - .../kscience/kmath/structures/NDField.kt | 2 +- .../kmath/tensors/DataSetNormalization.kt | 4 +- .../tensors/LinearSystemSolvingWithLUP.kt | 2 +- .../kscience/kmath/tensors/NeuralNetwork.kt | 2 +- .../space/kscience/kmath/tensors/PCA.kt | 4 +- kmath-ast/README.md | 5 +- kmath-ast/docs/README-TEMPLATE.md | 5 +- .../kmath/ast/rendering/MathSyntax.kt | 4 +- .../kmath/ast/rendering/SyntaxRenderer.kt | 2 +- .../kscience/kmath/ast/rendering/features.kt | 8 +- .../kmath/asm/internal/codegenUtils.kt | 4 +- .../DerivativeStructureExpression.kt | 2 +- .../DerivativeStructureExpressionTest.kt | 2 +- .../commons/optimization/OptimizeTest.kt | 2 +- .../expressions/DifferentiableExpression.kt | 2 +- .../kscience/kmath/expressions/Expression.kt | 4 +- .../FunctionalExpressionAlgebra.kt | 2 +- .../kscience/kmath/linear/LinearSolver.kt | 4 +- .../kscience/kmath/linear/LinearSpace.kt | 4 +- .../kscience/kmath/linear/LupDecomposition.kt | 4 +- .../kscience/kmath/linear/MatrixFeatures.kt | 2 +- .../kscience/kmath/linear/MatrixWrapper.kt | 9 +- .../space/kscience/kmath/misc/annotations.kt | 6 +- .../space/kscience/kmath/nd/AlgebraND.kt | 26 +++--- .../space/kscience/kmath/nd/Structure1D.kt | 2 +- .../space/kscience/kmath/nd/Structure2D.kt | 5 +- .../space/kscience/kmath/nd/StructureND.kt | 10 +-- .../kscience/kmath/operations/Algebra.kt | 62 +++++++------ .../kmath/operations/AlgebraElements.kt | 2 +- .../space/kscience/kmath/operations/BigInt.kt | 4 +- .../kmath/operations/NumericAlgebra.kt | 32 +++---- .../kmath/operations/OptionalOperations.kt | 4 +- .../kscience/kmath/structures/DoubleBuffer.kt | 4 +- .../kmath/structures/FlaggedBuffer.kt | 2 +- .../kscience/kmath/structures/FloatBuffer.kt | 4 +- .../kscience/kmath/structures/IntBuffer.kt | 4 +- .../kscience/kmath/structures/LongBuffer.kt | 4 +- .../kscience/kmath/structures/ShortBuffer.kt | 4 +- .../space/kscience/kmath/chains/Chain.kt | 14 +-- .../kscience/kmath/streaming/BufferFlow.kt | 2 +- .../kscience/kmath/dimensions/Wrappers.kt | 4 +- kmath-for-real/build.gradle.kts | 2 +- .../space/kscience/kmath/real/RealVector.kt | 2 +- .../kotlin/space/kscience/kmath/real/grids.kt | 4 +- .../kscience/kmath/functions/Piecewise.kt | 10 +-- .../kmath/integration/GaussIntegrator.kt | 20 +++-- .../integration/GaussIntegratorRuleFactory.kt | 4 +- .../kmath/integration/SimpsonIntegrator.kt | 15 ++-- .../kmath/integration/SplineIntegrator.kt | 13 +-- .../kmath/integration/UnivariateIntegrand.kt | 4 +- .../kscience/kmath/histogram/Histogram.kt | 2 +- .../kmath/histogram/UnivariateHistogram.kt | 7 +- kmath-jafama/README.md | 20 +---- kmath-kotlingrad/README.md | 2 +- kmath-kotlingrad/docs/README-TEMPLATE.md | 2 +- .../space/kscience/kmath/memory/Memory.kt | 2 +- .../kscience/kmath/memory/DataViewMemory.kt | 2 +- .../kscience/kmath/memory/ByteBufferMemory.kt | 2 +- .../kscience/kmath/memory/NativeMemory.kt | 4 +- .../kscience/kmath/nd4j/Nd4jArrayAlgebra.kt | 2 +- .../kscience/kmath/nd4j/Nd4jArrayStructure.kt | 2 +- .../kscience/kmath/nd4j/Nd4jTensorAlgebra.kt | 2 +- .../kmath/distributions/Distribution.kt | 2 +- .../distributions/FactorizedDistribution.kt | 4 +- .../kscience/kmath/internal/InternalGamma.kt | 2 +- .../optimization/FunctionOptimization.kt | 15 ++-- .../NoDerivFunctionOptimization.kt | 2 +- .../kmath/optimization/Optimization.kt | 2 +- .../AhrensDieterMarsagliaTsangGammaSampler.kt | 4 +- .../samplers/AliasMethodDiscreteSampler.kt | 12 +-- .../samplers/KempSmallMeanPoissonSampler.kt | 5 +- .../kscience/kmath/samplers/PoissonSampler.kt | 8 +- .../kscience/kmath/stat/RandomGenerator.kt | 6 +- .../kscience/kmath/stat/SamplerAlgebra.kt | 2 +- .../space/kscience/kmath/stat/Statistic.kt | 7 +- .../kmath/stat/RandomSourceGenerator.kt | 2 +- .../tensors/api/LinearOpsTensorAlgebra.kt | 38 ++++---- .../kmath/tensors/api/TensorAlgebra.kt | 13 ++- .../api/TensorPartialDivisionAlgebra.kt | 2 +- .../kmath/tensors/core/DoubleTensorAlgebra.kt | 90 ++++++++++--------- .../kmath/tensors/core/internal/utils.kt | 4 +- 98 files changed, 523 insertions(+), 538 deletions(-) delete mode 100644 docs/features.md create mode 100644 docs/readme.md create mode 100644 examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt delete mode 100644 examples/src/main/kotlin/space/kscience/kmath/jafama/KMathaJafamaDemo.kt diff --git a/README.md b/README.md index a15c4384f..db069d4e0 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,10 @@ # KMath -Could be pronounced as `key-math`. The **K**otlin **Math**ematics library was initially intended as a Kotlin-based analog to -Python's NumPy library. Later we found that kotlin is much more flexible language and allows superior architecture -designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like experience could -be achieved with [kmath-for-real](/kmath-for-real) extension module. +Could be pronounced as `key-math`. The **K**otlin **Math**ematics library was initially intended as a Kotlin-based +analog to Python's NumPy library. Later we found that kotlin is much more flexible language and allows superior +architecture designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like +experience could be achieved with [kmath-for-real](/kmath-for-real) extension module. [Documentation site (**WIP**)](https://mipt-npm.github.io/kmath/) @@ -21,26 +21,33 @@ be achieved with [kmath-for-real](/kmath-for-real) extension module. # 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 and Native) + . * Provide basic multiplatform implementations for those abstractions (without significant performance optimization). * Provide bindings and wrappers with those abstractions for popular optimized platform libraries. ## Non-goals -* Be like NumPy. It was the idea at the beginning, but we decided that we can do better in terms of API. +* Be like NumPy. It was the idea at the beginning, but we decided that we can do better in API. * Provide the best performance out of the box. We have specialized libraries for that. Need only API wrappers for them. * Cover all cases as immediately and in one bundle. We will modularize everything and add new features gradually. -* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like -for `Double` in the core. For that we will have specialization modules like `kmath-for-real`, which will give better -experience for those, who want to work with specific types. +* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like + for `Double` in the core. For that we will have specialization modules like `kmath-for-real`, which will give better + experience for those, who want to work with specific types. ## Features and stability -KMath is a modular library. Different modules provide different features with different API stability guarantees. All core modules are released with the same version, but with different API change policy. The features are described in module definitions below. The module stability could have following levels: +KMath is a modular library. Different modules provide different features with different API stability guarantees. All +core modules are released with the same version, but with different API change policy. The features are described in +module definitions below. The module stability could have the following levels: -* **PROTOTYPE**. On this level there are no compatibility guarantees. All methods and classes form those modules could break any moment. You can still use it, but be sure to fix the specific version. -* **EXPERIMENTAL**. The general API is decided, but some changes could be made. Volatile API is marked with `@UnstableKmathAPI` or other stability warning annotations. -* **DEVELOPMENT**. API breaking generally follows semantic versioning ideology. There could be changes in minor versions, but not in patch versions. API is protected with [binary-compatibility-validator](https://github.com/Kotlin/binary-compatibility-validator) tool. +* **PROTOTYPE**. On this level there are no compatibility guarantees. All methods and classes form those modules could + break any moment. You can still use it, but be sure to fix the specific version. +* **EXPERIMENTAL**. The general API is decided, but some changes could be made. Volatile API is marked + with `@UnstableKmathAPI` or other stability warning annotations. +* **DEVELOPMENT**. API breaking generally follows semantic versioning ideology. There could be changes in minor + versions, but not in patch versions. API is protected + with [binary-compatibility-validator](https://github.com/Kotlin/binary-compatibility-validator) tool. * **STABLE**. The API stabilized. Breaking changes are allowed only in major releases. @@ -161,7 +168,7 @@ performance calculations to code generation.
* ### [kmath-for-real](kmath-for-real) -> Extension module that should be used to achieve numpy-like behavior. +> Extension module that should be used to achieve numpy-like behavior. All operations are specialized to work with `Double` numbers without declaring algebraic contexts. One can still use generic algebras though. > @@ -278,30 +285,33 @@ One can still use generic algebras though. ## Multi-platform support -KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the -[common source sets](/kmath-core/src/commonMain) and implemented there wherever it is possible. In some cases, features -are delegated to platform-specific implementations even if they could be provided in the common module for performance -reasons. Currently, the Kotlin/JVM is the primary platform, however Kotlin/Native and Kotlin/JS contributions and +KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the +[common source sets](/kmath-core/src/commonMain) and implemented there wherever it is possible. In some cases, features +are delegated to platform-specific implementations even if they could be provided in the common module for performance +reasons. Currently, the Kotlin/JVM is the primary platform, however Kotlin/Native and Kotlin/JS contributions and feedback are also welcome. ## Performance -Calculation performance is one of major goals of KMath in the future, but in some cases it is impossible to achieve -both performance and flexibility. +Calculation performance is one of major goals of KMath in the future, but in some cases it is impossible to achieve both +performance and flexibility. -We expect to focus on creating convenient universal API first and then work on increasing performance for specific -cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized -native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be +We expect to focus on creating convenient universal API first and then work on increasing performance for specific +cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized +native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be better than SciPy. ## Requirements -KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend to use GraalVM-CE 11 for execution in order to get better performance. +KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend to use GraalVM-CE 11 for +execution to get better performance. ### Repositories -Release and development artifacts are accessible from mipt-npm [Space](https://www.jetbrains.com/space/) repository `https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven` (see documentation of -[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details). The repository could be reached through [repo.kotlin.link](https://repo.kotlin.link) proxy: +Release and development artifacts are accessible from mipt-npm [Space](https://www.jetbrains.com/space/) +repository `https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven` (see documentation of +[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details). The repository could +be reached through [repo.kotlin.link](https://repo.kotlin.link) proxy: ```kotlin repositories { @@ -318,7 +328,7 @@ 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 +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. diff --git a/docs/algebra.md b/docs/algebra.md index 84693bb81..20158a125 100644 --- a/docs/algebra.md +++ b/docs/algebra.md @@ -1,85 +1,45 @@ # Algebraic Structures and Algebraic Elements -The mathematical operations in KMath are generally separated from mathematical objects. This means that to perform an -operation, say `+`, one needs two objects of a type `T` and an algebra context, which draws appropriate operation up, -say `Space`. Next one needs to run the actual operation in the context: +The mathematical operations in KMath are generally separated from mathematical objects. This means that to perform an +operation, say `+`, one needs two objects of a type `T` and an algebra context, which draws appropriate operation up, +say `Group`. Next one needs to run the actual operation in the context: ```kotlin import space.kscience.kmath.operations.* val a: T = ... val b: T = ... -val space: Space = ... +val group: Group = ... -val c = space { a + b } +val c = group { a + b } ``` -At first glance, this distinction seems to be a needless complication, but in fact one needs to remember that in -mathematics, one could draw up different operations on same objects. For example, one could use different types of +At first glance, this distinction seems to be a needless complication, but in fact one needs to remember that in +mathematics, one could draw up different operations on same objects. For example, one could use different types of geometry for vectors. ## Algebraic Structures -Mathematical contexts have the following hierarchy: +Primary mathematical contexts have the following hierarchy: -**Algebra** ← **Space** ← **Ring** ← **Field** +`Field <: Ring <: Group <: Algebra` These interfaces follow real algebraic structures: -- [Space](https://mathworld.wolfram.com/VectorSpace.html) defines addition, its neutral element (i.e. 0) and scalar -multiplication; -- [Ring](http://mathworld.wolfram.com/Ring.html) adds multiplication and its neutral element (i.e. 1); +- [Group](https://mathworld.wolfram.com/Group.html) defines addition, its identity element (i.e., 0) and additive + inverse (-x); +- [Ring](http://mathworld.wolfram.com/Ring.html) adds multiplication and its identity element (i.e., 1); - [Field](http://mathworld.wolfram.com/Field.html) adds division operation. A typical implementation of `Field` is the `DoubleField` which works on doubles, and `VectorSpace` for `Space`. In some cases algebra context can hold additional operations like `exp` or `sin`, and then it inherits appropriate -interface. Also, contexts may have operations, which produce elements outside of the context. For example, `Matrix.dot` -operation produces a matrix with new dimensions, which can be incompatible with initial matrix in terms of linear -operations. - -## Algebraic Element - -To achieve more familiar behavior (where you apply operations directly to mathematical objects), without involving -contexts KMath submits special type objects called `MathElement`. A `MathElement` is basically some object coupled to -a mathematical context. For example `Complex` is the pair of real numbers representing real and imaginary parts, -but it also holds reference to the `ComplexField` singleton, which allows performing direct operations on `Complex` -numbers without explicit involving the context like: - -```kotlin -import space.kscience.kmath.operations.* - -// Using elements -val c1 = Complex(1.0, 1.0) -val c2 = Complex(1.0, -1.0) -val c3 = c1 + c2 + 3.0.toComplex() - -// Using context -val c4 = ComplexField { c1 + i - 2.0 } -``` - -Both notations have their pros and cons. - -The hierarchy for algebraic elements follows the hierarchy for the corresponding algebraic structures. - -**MathElement** ← **SpaceElement** ← **RingElement** ← **FieldElement** - -`MathElement` is the generic common ancestor of the class with context. - -One major distinction between algebraic elements and algebraic contexts is that elements have three type -parameters: - -1. The type of elements, the field operates on. -2. The self-type of the element returned from operation (which has to be an algebraic element). -3. The type of the algebra over first type-parameter. - -The middle type is needed for of algebra members do not store context. For example, it is impossible to add a context -to regular `Double`. The element performs automatic conversions from context types and back. One should use context -operations in all performance-critical places. The performance of element operations is not guaranteed. +interface. Also, contexts may have operations, which produce elements outside the context. For example, `Matrix.dot` +operation produces a matrix with new dimensions, which can be incompatible with initial matrix in linear operations. ## Spaces and Fields -KMath submits both contexts and elements for builtin algebraic structures: +KMath introduces contexts for builtin algebraic structures: ```kotlin import space.kscience.kmath.operations.* @@ -102,13 +62,13 @@ val c2 = ComplexField { c1 - 1.0 } // Returns: Complex(re=0.0, im=2.0) val c3 = ComplexField { c1 - i * 2.0 } ``` -**Note**: In theory it is possible to add behaviors directly to the context, but as for now Kotlin does not support -that. Watch [KT-10468](https://youtrack.jetbrains.com/issue/KT-10468) and +**Note**: In theory it is possible to add behaviors directly to the context, but as for now Kotlin does not support +that. Watch [KT-10468](https://youtrack.jetbrains.com/issue/KT-10468) and [KEEP-176](https://github.com/Kotlin/KEEP/pull/176) for updates. ## Nested fields -Contexts allow one to build more complex structures. For example, it is possible to create a `Matrix` from complex +Contexts allow one to build more complex structures. For example, it is possible to create a `Matrix` from complex elements like so: ```kotlin @@ -118,8 +78,9 @@ val element = NDElement.complex(shape = intArrayOf(2, 2)) { index: IntArray -> ``` The `element` in this example is a member of the `Field` of 2D structures, each element of which is a member of its own -`ComplexField`. It is important one does not need to create a special n-d class to hold complex -numbers and implement operations on it, one just needs to provide a field for its elements. +`ComplexField`. It is important one does not need to create a special n-d class to hold complex numbers and implement +operations on it, one just needs to provide a field for its elements. -**Note**: Fields themselves do not solve the problem of JVM boxing, but it is possible to solve with special contexts like +**Note**: Fields themselves do not solve the problem of JVM boxing, but it is possible to solve with special contexts +like `MemorySpec`. diff --git a/docs/buffers.md b/docs/buffers.md index 679bd4e78..e7573497e 100644 --- a/docs/buffers.md +++ b/docs/buffers.md @@ -1,17 +1,20 @@ # Buffers -Buffer is one of main building blocks of kmath. It is a basic interface allowing random-access read and write (with `MutableBuffer`). -There are different types of buffers: +Buffer is one of main building blocks of kmath. It is a basic interface allowing random-access read and write ( +with `MutableBuffer`). There are different types of buffers: -* Primitive buffers wrapping like `RealBuffer` which are wrapping primitive arrays. +* Primitive buffers wrapping like `DoubleBuffer` which are wrapping primitive arrays. * Boxing `ListBuffer` wrapping a list * Functionally defined `VirtualBuffer` which does not hold a state itself, but provides a function to calculate value * `MemoryBuffer` allows direct allocation of objects in continuous memory block. -Some kmath features require a `BufferFactory` class to operate properly. A general convention is to use functions defined in -`Buffer` and `MutableBuffer` companion classes. For example factory `Buffer.Companion::auto` in most cases creates the most suitable -buffer for given reified type (for types with custom memory buffer it still better to use their own `MemoryBuffer.create()` factory). +Some kmath features require a `BufferFactory` class to operate properly. A general convention is to use functions +defined in +`Buffer` and `MutableBuffer` companion classes. For example factory `Buffer.Companion::auto` in most cases creates the +most suitable buffer for given reified type (for types with custom memory buffer it still better to use their +own `MemoryBuffer.create()` factory). ## Buffer performance -One should avoid using default boxing buffer wherever it is possible. Try to use primitive buffers or memory buffers instead +One should avoid using default boxing buffer wherever it is possible. Try to use primitive buffers or memory buffers +instead . diff --git a/docs/codestyle.md b/docs/codestyle.md index 541dc4973..73ba5f754 100644 --- a/docs/codestyle.md +++ b/docs/codestyle.md @@ -1,26 +1,20 @@ # Coding Conventions -KMath code follows general [Kotlin conventions](https://kotlinlang.org/docs/reference/coding-conventions.html), but -with a number of small changes and clarifications. +Generally, KMath code follows general [Kotlin coding conventions](https://kotlinlang.org/docs/reference/coding-conventions.html), but with a number of small changes and clarifications. ## Utility Class Naming -Filename should coincide with a name of one of the classes contained in the file or start with small letter and -describe its contents. +Filename should coincide with a name of one of the classes contained in the file or start with small letter and describe its contents. -The code convention [here](https://kotlinlang.org/docs/reference/coding-conventions.html#source-file-names) says that -file names should start with a capital letter even if file does not contain classes. Yet starting utility classes and -aggregators with a small letter seems to be a good way to visually separate those files. +The code convention [here](https://kotlinlang.org/docs/reference/coding-conventions.html#source-file-names) says that file names should start with a capital letter even if file does not contain classes. Yet starting utility classes and aggregators with a small letter seems to be a good way to visually separate those files. This convention could be changed in future in a non-breaking way. ## Private Variable Naming -Private variables' names may start with underscore `_` for of the private mutable variable is shadowed by the public -read-only value with the same meaning. +Private variables' names may start with underscore `_` for of the private mutable variable is shadowed by the public read-only value with the same meaning. -This rule does not permit underscores in names, but it is sometimes useful to "underscore" the fact that public and -private versions draw up the same entity. It is allowed only for private variables. +This rule does not permit underscores in names, but it is sometimes useful to "underscore" the fact that public and private versions draw up the same entity. It is allowed only for private variables. This convention could be changed in future in a non-breaking way. @@ -30,5 +24,4 @@ Use one-liners when they occupy single code window line both for functions and p `val b: String get() = "fff"`. The same should be performed with multiline expressions when they could be cleanly separated. -There is no universal consensus whenever use `fun a() = ...` or `fun a() { return ... }`. Yet from reader outlook -one-lines seem to better show that the property or function is easily calculated. +There is no universal consensus whenever use `fun a() = ...` or `fun a() { return ... }`. Yet from reader outlook one-lines seem to better show that the property or function is easily calculated. diff --git a/docs/contexts.md b/docs/contexts.md index 58b198046..c26333860 100644 --- a/docs/contexts.md +++ b/docs/contexts.md @@ -2,18 +2,17 @@ ## The problem -A known problem for implementing mathematics in statically-typed languages (but not only in them) is that different -sets of mathematical operators can be defined on the same mathematical objects. Sometimes there is no single way to -treat some operations, including basic arithmetic operations, on a Java/Kotlin `Number`. Sometimes there are different ways to -define the same structure, such as Euclidean and elliptic geometry vector spaces over real vectors. Another problem arises when -one wants to add some kind of behavior to an existing entity. In dynamic languages those problems are usually solved -by adding dynamic context-specific behaviors at runtime, but this solution has a lot of drawbacks. +A known problem for implementing mathematics in statically-typed languages (but not only in them) is that different sets +of mathematical operators can be defined on the same mathematical objects. Sometimes there is no single way to treat +some operations, including basic arithmetic operations, on a Java/Kotlin `Number`. Sometimes there are different ways to +define the same structure, such as Euclidean and elliptic geometry vector spaces over real vectors. Another problem +arises when one wants to add some kind of behavior to an existing entity. In dynamic languages those problems are +usually solved by adding dynamic context-specific behaviors at runtime, but this solution has a lot of drawbacks. ## Context-oriented approach -One possible solution to these problems is to divorce numerical representations from behaviors. -For example in Kotlin one can define a separate class which represents some entity without any operations, -ex. a complex number: +One possible solution to these problems is to divorce numerical representations from behaviors. For example in Kotlin +one can define a separate class representing some entity without any operations, ex. a complex number: ```kotlin data class Complex(val re: Double, val im: Double) @@ -28,9 +27,10 @@ object ComplexOperations { } ``` -In Java, applying such external operations could be very cumbersome, but Kotlin has a unique feature which allows us -implement this naturally: [extensions with receivers](https://kotlinlang.org/docs/reference/extensions.html#extension-functions). -In Kotlin, an operation on complex number could be implemented as: +In Java, applying such external operations could be cumbersome, but Kotlin has a unique feature that allows us +implement this +naturally: [extensions with receivers](https://kotlinlang.org/docs/reference/extensions.html#extension-functions). In +Kotlin, an operation on complex number could be implemented as: ```kotlin with(ComplexOperations) { c1 + c2 - c3 } @@ -52,20 +52,20 @@ In KMath, contexts are not only responsible for operations, but also for raw obj ### Type classes -An obvious candidate to get more or less the same functionality is the type class, which allows one to bind a behavior to -a specific type without modifying the type itself. On the plus side, type classes do not require explicit context +An obvious candidate to get more or less the same functionality is the type class, which allows one to bind a behavior +to a specific type without modifying the type itself. On the plus side, type classes do not require explicit context declaration, so the code looks cleaner. On the minus side, if there are different sets of behaviors for the same types, -it is impossible to combine them into one module. Also, unlike type classes, context can have parameters or even -state. For example in KMath, sizes and strides for `NDElement` or `Matrix` could be moved to context to optimize -performance in case of a large amount of structures. +it is impossible to combine them into one module. Also, unlike type classes, context can have parameters or even state. +For example in KMath, sizes and strides for `NDElement` or `Matrix` could be moved to context to optimize performance in +case of a large amount of structures. ### Wildcard imports and importing-on-demand -Sometimes, one may wish to use a single context throughout a file. In this case, is possible to import all members -from a package or file, via `import context.complex.*`. Effectively, this is the same as enclosing an entire file -with a single context. However when using multiple contexts, this technique can introduce operator ambiguity, due to -namespace pollution. If there are multiple scoped contexts which define the same operation, it is still possible to -to import specific operations as needed, without using an explicit context with extension functions, for example: +Sometimes, one may wish to use a single context throughout a file. In this case, is possible to import all members from +a package or file, via `import context.complex.*`. Effectively, this is the same as enclosing an entire file with a +single context. However, when using multiple contexts, this technique can introduce operator ambiguity, due to namespace +pollution. If there are multiple scoped contexts that define the same operation, it is still possible to import +specific operations as needed, without using an explicit context with extension functions, for example: ``` import context.complex.op1 diff --git a/docs/expressions.md b/docs/expressions.md index 1e05e5340..e6250110c 100644 --- a/docs/expressions.md +++ b/docs/expressions.md @@ -1,26 +1,21 @@ # Expressions -**Experimental: this API is in early stage and could change any time** - -Expressions is an experimental feature which allows to construct lazily or immediately calculated parametric mathematical -expressions. +Expressions is a feature, which allows constructing lazily or immediately calculated parametric mathematical expressions. The potential use-cases for it (so far) are following: -* Lazy evaluation (in general simple lambda is better, but there are some border cases) +* lazy evaluation (in general simple lambda is better, but there are some border cases); +* automatic differentiation in single-dimension and in multiple dimensions; +* generation of mathematical syntax trees with subsequent code generation for other languages; +* symbolic computations, especially differentiation (and some other actions with `kmath-symja` integration with Symja's `IExpr`—integration, simplification, and more); +* visualization with `kmath-jupyter`. -* Automatic differentiation in single-dimension and in multiple dimensions - -* Generation of mathematical syntax trees with subsequent code generation for other languages - -* Maybe symbolic computations (needs additional research) - -The workhorse of this API is `Expression` interface which exposes single `operator fun invoke(arguments: Map): T` -method. `ExpressionContext` is used to generate expressions and introduce variables. +The workhorse of this API is `Expression` interface, which exposes single `operator fun invoke(arguments: Map): T` +method. `ExpressionAlgebra` is used to generate expressions and introduce variables. Currently there are two implementations: * Generic `ExpressionField` in `kmath-core` which allows construction of custom lazy expressions -* Auto-differentiation expression in `kmath-commons` module allows to use full power of `DerivativeStructure` +* Auto-differentiation expression in `kmath-commons` module allows using full power of `DerivativeStructure` from commons-math. **TODO: add example** diff --git a/docs/features.md b/docs/features.md deleted file mode 100644 index 1068a4417..000000000 --- a/docs/features.md +++ /dev/null @@ -1,14 +0,0 @@ -# Features - -* [Algebra](algebra.md) - [Context-based](contexts.md) operations on different primitives and structures. - -* [NDStructures](nd-structure.md) - -* [Linear algebra](linear.md) - Matrices, operations and linear equations solving. To be moved to separate module. Currently supports basic -api and multiple library back-ends. - -* [Histograms](histograms.md) - Multidimensional histogram calculation and operations. - -* [Expressions](expressions.md) - -* Commons math integration diff --git a/docs/linear.md b/docs/linear.md index 6ccc6caac..2a05499ef 100644 --- a/docs/linear.md +++ b/docs/linear.md @@ -1,19 +1,31 @@ ## Basic linear algebra layout -KMath support for linear algebra organized in a context-oriented way. Meaning that operations are in most cases declared -in context classes, and are not the members of classes that store data. This allows more flexible approach to maintain multiple -back-ends. The new operations added as extensions to contexts instead of being member functions of data structures. +KMath support for linear algebra organized in a context-oriented way, which means that operations are in most cases declared in context classes, and are not the members of classes that store data. This allows more flexible approach to maintain multiple back-ends. The new operations added as extensions to contexts instead of being member functions of data structures. -Two major contexts used for linear algebra and hyper-geometry: +The main context for linear algebra over matrices and vectors is `LinearSpace`, which defines addition and dot products of matrices and vectors: -* `VectorSpace` forms a mathematical space on top of array-like structure (`Buffer` and its type alias `Point` used for geometry). +```kotlin +import space.kscience.kmath.linear.* -* `MatrixContext` forms a space-like context for 2d-structures. It does not store matrix size and therefore does not implement -`Space` interface (it is impossible to create zero element without knowing the matrix size). +LinearSpace.Companion.real { + val vec = buildVector(10) { i -> i.toDouble() } + val mat = buildMatrix(10, 10) { i, j -> i.toDouble() + j } -## Vector spaces + // Addition + vec + vec + mat + mat + // Multiplication by scalar + vec * 2.0 + mat * 2.0 -## Matrix operations + // Dot product + mat dot vec + mat dot mat +} +``` -## Back-end overview \ No newline at end of file +## Backends overview + +### EJML +### Commons Math diff --git a/docs/nd-structure.md b/docs/nd-structure.md index ab214094e..3e9203ec0 100644 --- a/docs/nd-structure.md +++ b/docs/nd-structure.md @@ -11,16 +11,16 @@ Let us consider following contexts: ```kotlin // automatically build context most suited for given type. val autoField = NDField.auto(DoubleField, dim, dim) - // specialized nd-field for Double. It works as generic Double field as well + // specialized nd-field for Double. It works as generic Double field as well. val specializedField = NDField.real(dim, dim) //A generic boxing field. It should be used for objects, not primitives. val genericField = NDField.buffered(DoubleField, dim, dim) ``` -Now let us perform several tests and see which implementation is best suited for each case: +Now let us perform several tests and see, which implementation is best suited for each case: ## Test case -In order to test performance we will take 2d-structures with `dim = 1000` and add a structure filled with `1.0` +To test performance we will take 2d-structures with `dim = 1000` and add a structure filled with `1.0` to it `n = 1000` times. ## Specialized @@ -35,8 +35,8 @@ The code to run this looks like: ``` The performance of this code is the best of all tests since it inlines all operations and is specialized for operation with doubles. We will measure everything else relative to this one, so time for this test will be `1x` (real time -on my computer is about 4.5 seconds). The only problem with this approach is that it requires to specify type -from the beginning. Everyone do so anyway, so it is the recommended approach. +on my computer is about 4.5 seconds). The only problem with this approach is that it requires specifying type +from the beginning. Everyone does so anyway, so it is the recommended approach. ## Automatic Let's do the same with automatic field inference: @@ -49,7 +49,7 @@ Let's do the same with automatic field inference: } ``` Ths speed of this operation is approximately the same as for specialized case since `NDField.auto` just -returns the same `RealNDField` in this case. Of course it is usually better to use specialized method to be sure. +returns the same `RealNDField` in this case. Of course, it is usually better to use specialized method to be sure. ## Lazy Lazy field does not produce a structure when asked, instead it generates an empty structure and fills it on-demand @@ -63,7 +63,7 @@ When one calls } } ``` -The result will be calculated almost immediately but the result will be empty. In order to get the full result +The result will be calculated almost immediately but the result will be empty. To get the full result structure one needs to call all its elements. In this case computation overhead will be huge. So this field never should be used if one expects to use the full result structure. Though if one wants only small fraction, it could save a lot of time. @@ -94,7 +94,7 @@ The boxing field produced by } } ``` -obviously is the slowest one, because it requires to box and unbox the `double` on each operation. It takes about +is the slowest one, because it requires boxing and unboxing the `double` on each operation. It takes about `15x` time (**TODO: there seems to be a problem here, it should be slow, but not that slow**). This field should never be used for primitives. @@ -123,6 +123,6 @@ for i in range(1000): ``` gives the completion time of about `1.1x`, which means that specialized kotlin code in fact is working faster (I think it is because better memory management). Of course if one writes `res += 1.0`, the performance will be different, -but it would be differenc case, because numpy overrides `+=` with in-place operations. In-place operations are +but it would be different case, because numpy overrides `+=` with in-place operations. In-place operations are available in `kmath` with `MutableNDStructure` but there is no field for it (one can still work with mapping functions). \ No newline at end of file diff --git a/docs/readme.md b/docs/readme.md new file mode 100644 index 000000000..2953b7113 --- /dev/null +++ b/docs/readme.md @@ -0,0 +1,14 @@ +# Documentation + +* [Algebra](algebra.md): [context-based](contexts.md) operations on different primitives and structures. + +* [NDStructures](nd-structure.md) + +* [Linear algebra](linear.md): matrices, operations and linear equations solving. To be moved to separate module. + Currently, supports basic API and multiple library back-ends. + +* [Histograms](histograms.md): multidimensional histogram calculation and operations. + +* [Expressions](expressions.md) + +* Commons math integration diff --git a/docs/templates/README-TEMPLATE.md b/docs/templates/README-TEMPLATE.md index bad11a31a..e75d4c5ed 100644 --- a/docs/templates/README-TEMPLATE.md +++ b/docs/templates/README-TEMPLATE.md @@ -6,10 +6,10 @@ # KMath -Could be pronounced as `key-math`. The **K**otlin **Math**ematics library was initially intended as a Kotlin-based analog to -Python's NumPy library. Later we found that kotlin is much more flexible language and allows superior architecture -designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like experience could -be achieved with [kmath-for-real](/kmath-for-real) extension module. +Could be pronounced as `key-math`. The **K**otlin **Math**ematics library was initially intended as a Kotlin-based +analog to Python's NumPy library. Later we found that kotlin is much more flexible language and allows superior +architecture designs. In contrast to `numpy` and `scipy` it is modular and has a lightweight core. The `numpy`-like +experience could be achieved with [kmath-for-real](/kmath-for-real) extension module. [Documentation site (**WIP**)](https://mipt-npm.github.io/kmath/) @@ -21,26 +21,33 @@ be achieved with [kmath-for-real](/kmath-for-real) extension module. # 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 and Native) + . * Provide basic multiplatform implementations for those abstractions (without significant performance optimization). * Provide bindings and wrappers with those abstractions for popular optimized platform libraries. ## Non-goals -* Be like NumPy. It was the idea at the beginning, but we decided that we can do better in terms of API. +* Be like NumPy. It was the idea at the beginning, but we decided that we can do better in API. * Provide the best performance out of the box. We have specialized libraries for that. Need only API wrappers for them. * Cover all cases as immediately and in one bundle. We will modularize everything and add new features gradually. -* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like -for `Double` in the core. For that we will have specialization modules like `kmath-for-real`, which will give better -experience for those, who want to work with specific types. +* Provide specialized behavior in the core. API is made generic on purpose, so one needs to specialize for types, like + for `Double` in the core. For that we will have specialization modules like `kmath-for-real`, which will give better + experience for those, who want to work with specific types. ## Features and stability -KMath is a modular library. Different modules provide different features with different API stability guarantees. All core modules are released with the same version, but with different API change policy. The features are described in module definitions below. The module stability could have following levels: +KMath is a modular library. Different modules provide different features with different API stability guarantees. All +core modules are released with the same version, but with different API change policy. The features are described in +module definitions below. The module stability could have the following levels: -* **PROTOTYPE**. On this level there are no compatibility guarantees. All methods and classes form those modules could break any moment. You can still use it, but be sure to fix the specific version. -* **EXPERIMENTAL**. The general API is decided, but some changes could be made. Volatile API is marked with `@UnstableKmathAPI` or other stability warning annotations. -* **DEVELOPMENT**. API breaking generally follows semantic versioning ideology. There could be changes in minor versions, but not in patch versions. API is protected with [binary-compatibility-validator](https://github.com/Kotlin/binary-compatibility-validator) tool. +* **PROTOTYPE**. On this level there are no compatibility guarantees. All methods and classes form those modules could + break any moment. You can still use it, but be sure to fix the specific version. +* **EXPERIMENTAL**. The general API is decided, but some changes could be made. Volatile API is marked + with `@UnstableKmathAPI` or other stability warning annotations. +* **DEVELOPMENT**. API breaking generally follows semantic versioning ideology. There could be changes in minor + versions, but not in patch versions. API is protected + with [binary-compatibility-validator](https://github.com/Kotlin/binary-compatibility-validator) tool. * **STABLE**. The API stabilized. Breaking changes are allowed only in major releases. @@ -78,30 +85,33 @@ $modules ## Multi-platform support -KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the -[common source sets](/kmath-core/src/commonMain) and implemented there wherever it is possible. In some cases, features -are delegated to platform-specific implementations even if they could be provided in the common module for performance -reasons. Currently, the Kotlin/JVM is the primary platform, however Kotlin/Native and Kotlin/JS contributions and +KMath is developed as a multi-platform library, which means that most of the interfaces are declared in the +[common source sets](/kmath-core/src/commonMain) and implemented there wherever it is possible. In some cases, features +are delegated to platform-specific implementations even if they could be provided in the common module for performance +reasons. Currently, the Kotlin/JVM is the primary platform, however Kotlin/Native and Kotlin/JS contributions and feedback are also welcome. ## Performance -Calculation performance is one of major goals of KMath in the future, but in some cases it is impossible to achieve -both performance and flexibility. +Calculation performance is one of major goals of KMath in the future, but in some cases it is impossible to achieve both +performance and flexibility. -We expect to focus on creating convenient universal API first and then work on increasing performance for specific -cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized -native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be +We expect to focus on creating convenient universal API first and then work on increasing performance for specific +cases. We expect the worst KMath benchmarks will perform better than native Python, but worse than optimized +native/SciPy (mostly due to boxing operations on primitive numbers). The best performance of optimized parts could be better than SciPy. ## Requirements -KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend to use GraalVM-CE 11 for execution in order to get better performance. +KMath currently relies on JDK 11 for compilation and execution of Kotlin-JVM part. We recommend to use GraalVM-CE 11 for +execution to get better performance. ### Repositories -Release and development artifacts are accessible from mipt-npm [Space](https://www.jetbrains.com/space/) repository `https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven` (see documentation of -[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details). The repository could be reached through [repo.kotlin.link](https://repo.kotlin.link) proxy: +Release and development artifacts are accessible from mipt-npm [Space](https://www.jetbrains.com/space/) +repository `https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven` (see documentation of +[Kotlin Multiplatform](https://kotlinlang.org/docs/reference/multiplatform.html) for more details). The repository could +be reached through [repo.kotlin.link](https://repo.kotlin.link) proxy: ```kotlin repositories { @@ -118,7 +128,7 @@ 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 +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. 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 6ceaa962a..dec3bfb81 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt @@ -13,8 +13,8 @@ import space.kscience.kmath.kotlingrad.toKotlingradExpression import space.kscience.kmath.operations.DoubleField /** - * In this example, x^2-4*x-44 function is differentiated with Kotlin∇, and the autodiff result is compared with - * valid derivative in a certain point. + * In this example, *x2 − 4 x − 44* function is differentiated with Kotlin∇, and the + * derivation result is compared with valid derivative in a certain point. */ fun main() { val actualDerivative = "x^2-4*x-44" 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 a9eca0500..7e09faeff 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt @@ -13,8 +13,8 @@ import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.symja.toSymjaExpression /** - * In this example, x^2-4*x-44 function is differentiated with Symja, and the autodiff result is compared with - * valid derivative in a certain point. + * In this example, *x2 − 4 x − 44* function is differentiated with Symja, and the + * derivation result is compared with valid derivative in a certain point. */ fun main() { val actualDerivative = "x^2-4*x-44" diff --git a/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt b/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt index 5e64235e3..9118d09ae 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/commons/fit/fitWithAutoDiff.kt @@ -25,8 +25,7 @@ import space.kscience.plotly.models.TraceValues import kotlin.math.pow import kotlin.math.sqrt -//Forward declaration of symbols that will be used in expressions. -// This declaration is required for +// Forward declaration of symbols that will be used in expressions. private val a by symbol private val b by symbol private val c by symbol diff --git a/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt new file mode 100644 index 000000000..9c3d0fdbe --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/jafama/JafamaDemo.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2018-2021 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.jafama + +import space.kscience.kmath.operations.invoke + +fun main() { + val a = 2.0 + val b = StrictJafamaDoubleField { exp(a) } + println(JafamaDoubleField { b + a }) + println(StrictJafamaDoubleField { ln(b) }) +} diff --git a/examples/src/main/kotlin/space/kscience/kmath/jafama/KMathaJafamaDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/jafama/KMathaJafamaDemo.kt deleted file mode 100644 index 879aab08f..000000000 --- a/examples/src/main/kotlin/space/kscience/kmath/jafama/KMathaJafamaDemo.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2018-2021 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.kmath.jafama - -import net.jafama.FastMath - - -fun main(){ - val a = JafamaDoubleField.number(2.0) - val b = StrictJafamaDoubleField.power(FastMath.E,a) - - println(JafamaDoubleField.add(b,a)) - println(StrictJafamaDoubleField.ln(b)) -} 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 b319766e3..bde83cea9 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/stat/DistributionDemo.kt @@ -10,9 +10,6 @@ import space.kscience.kmath.chains.Chain import space.kscience.kmath.chains.collectWithState import space.kscience.kmath.distributions.NormalDistribution -/** - * The state of distribution averager. - */ private data class AveragingChainState(var num: Int = 0, var value: Double = 0.0) /** 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 501bf98db..e1621d477 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt @@ -32,7 +32,7 @@ fun main() { // automatically build context most suited for given type. val autoField = AlgebraND.auto(DoubleField, dim, dim) - // specialized nd-field for Double. It works as generic Double field as well + // specialized nd-field for Double. It works as generic Double field as well. val realField = AlgebraND.real(dim, dim) //A generic boxing field. It should be used for objects, not primitives. val boxingField = AlgebraND.field(DoubleField, Buffer.Companion::boxing, dim, dim) 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 74795cc68..ad6890b15 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/DataSetNormalization.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/DataSetNormalization.kt @@ -17,7 +17,7 @@ fun main() = BroadcastDoubleTensorAlgebra { // work in context with broadcast m dataset += fromArray( intArrayOf(5), - doubleArrayOf(0.0, 1.0, 1.5, 3.0, 5.0) // rows means + doubleArrayOf(0.0, 1.0, 1.5, 3.0, 5.0) // row means ) @@ -28,7 +28,7 @@ fun main() = BroadcastDoubleTensorAlgebra { // work in context with broadcast m println("Mean:\n$mean") println("Standard deviation:\n$std") - // also we can calculate other statistic as minimum and maximum of rows + // also, we can calculate other statistic as minimum and maximum of rows println("Minimum:\n${dataset.min(0, false)}") println("Maximum:\n${dataset.max(0, false)}") 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 6453ca44e..3e6a60c31 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/LinearSystemSolvingWithLUP.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/LinearSystemSolvingWithLUP.kt @@ -42,7 +42,7 @@ fun main() = BroadcastDoubleTensorAlgebra {// work in context with linear operat // get P, L, U such that PA = LU val (p, l, u) = a.lu() - // check that P is permutation matrix + // check P is permutation matrix println("P:\n$p") // L is lower triangular matrix and U is upper triangular matrix println("L:\n$l") 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 b262bee02..55a9aa67a 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/NeuralNetwork.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/NeuralNetwork.kt @@ -186,7 +186,7 @@ fun main() = BroadcastDoubleTensorAlgebra { x += fromArray( intArrayOf(5), - doubleArrayOf(0.0, -1.0, -2.5, -3.0, 5.5) // rows means + doubleArrayOf(0.0, -1.0, -2.5, -3.0, 5.5) // row means ) 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 411e048d7..b973abdef 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/PCA.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/PCA.kt @@ -65,8 +65,8 @@ fun main(): Unit = BroadcastDoubleTensorAlgebra { // work in context with broad val datasetReduced = v dot stack(listOf(xScaled, yScaled)) println("Reduced data:\n$datasetReduced") - // we can restore original data from reduced data. - // for example, find 7th element of dataset + // we can restore original data from reduced data; + // for example, find 7th element of dataset. val n = 7 val restored = (datasetReduced[n] dot v.view(intArrayOf(1, 2))) * std + mean println("Original value:\n${dataset[n]}") diff --git a/kmath-ast/README.md b/kmath-ast/README.md index c3c4c38a1..686506f6f 100644 --- a/kmath-ast/README.md +++ b/kmath-ast/README.md @@ -106,7 +106,7 @@ var executable = function (constants, arguments) { }; ``` -JS also supports very experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation. +JS also supports experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation. Currently, only expressions inside `DoubleField` and `IntRing` are supported. ```kotlin @@ -161,7 +161,10 @@ public fun main() { Result LaTeX: +
+ ![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3}) +
Result MathML (can be used with MathJax or other renderers): diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md index b90f8ff08..9494af63a 100644 --- a/kmath-ast/docs/README-TEMPLATE.md +++ b/kmath-ast/docs/README-TEMPLATE.md @@ -77,7 +77,7 @@ var executable = function (constants, arguments) { }; ``` -JS also supports very experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation. +JS also supports experimental expression optimization with [WebAssembly](https://webassembly.org/) IR generation. Currently, only expressions inside `DoubleField` and `IntRing` are supported. ```kotlin @@ -132,7 +132,10 @@ public fun main() { Result LaTeX: +
+ ![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3}) +
Result MathML (can be used with MathJax or other renderers): 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 38450403d..ee23ab408 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 @@ -8,7 +8,7 @@ package space.kscience.kmath.ast.rendering import space.kscience.kmath.misc.UnstableKMathAPI /** - * Mathematical typography syntax node. + * Syntax node for mathematical typography. * * @author Iaroslav Postovalov */ @@ -301,7 +301,7 @@ public data class BinaryPlusSyntax( } /** - * Represents binary, infix subtraction (*42 - 42*). + * Represents binary, infix subtraction (*42 − 42*). * * @param left The minuend. * @param right The subtrahend. 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 fb2b3b66f..362c07d72 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 @@ -9,7 +9,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI /** * Abstraction of writing [MathSyntax] as a string of an actual markup language. Typical implementation should - * involve traversal of MathSyntax with handling each its subtype. + * involve traversal of MathSyntax with handling each subtype. * * @author Iaroslav Postovalov */ 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 37e9a8c19..8a4804916 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 @@ -49,7 +49,7 @@ else NumberSyntax(string = s) /** - * Special printing for numeric types which are printed in form of + * Special printing for numeric types that are printed in form of * *('-'? (DIGIT+ ('.' DIGIT+)? ('E' '-'? DIGIT+)? | 'Infinity')) | 'NaN'*. * * @property types The suitable types. @@ -110,7 +110,7 @@ public class PrettyPrintFloats(public val types: Set>) : Rend } /** - * Special printing for numeric types which are printed in form of *'-'? DIGIT+*. + * Special printing for numeric types that are printed in form of *'-'? DIGIT+*. * * @property types The suitable types. * @author Iaroslav Postovalov @@ -155,7 +155,7 @@ public class PrettyPrintPi(public val symbols: Set) : RenderFeature { } /** - * Abstract printing of unary operations which discards [MST] if their operation is not in [operations] or its type is + * Abstract printing of unary operations that discards [MST] if their operation is not in [operations] or its type is * not [MST.Unary]. * * @param operations the allowed operations. If `null`, any operation is accepted. @@ -176,7 +176,7 @@ public abstract class Unary(public val operations: Collection?) : Render } /** - * Abstract printing of unary operations which discards [MST] if their operation is not in [operations] or its type is + * Abstract printing of unary operations that discards [MST] if their operation is not in [operations] or its type is * not [MST.Binary]. * * @property operations the allowed operations. If `null`, any operation is accepted. 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 a84248f63..06e040e93 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 @@ -57,13 +57,13 @@ internal fun MethodVisitor.label(): Label = Label().also(::visitLabel) /** * Creates a class name for [Expression] subclassed to implement [mst] provided. * - * This methods helps to avoid collisions of class name to prevent loading several classes with the same name. If there + * These methods help to avoid collisions of class name to prevent loading several classes with the same name. If there * is a colliding class, change [collision] parameter or leave it `0` to check existing classes recursively. * * @author Iaroslav Postovalov */ internal tailrec fun buildName(mst: MST, collision: Int = 0): String { - val name = "space.kscience.kmath.asm.generated.AsmCompiledExpression_${mst.hashCode()}_$collision" + val name = "space.kscience.kmath.asm.generated.CompiledExpression_${mst.hashCode()}_$collision" try { Class.forName(name) diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt index 0925fee7e..d5473063a 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt @@ -31,7 +31,7 @@ public class DerivativeStructureField( override fun number(value: Number): DerivativeStructure = const(value.toDouble()) /** - * A class that implements both [DerivativeStructure] and a [Symbol] + * A class implementing both [DerivativeStructure] and [Symbol]. */ public inner class DerivativeStructureSymbol( size: Int, diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt index 0bf1d53f0..3c57f5467 100644 --- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt +++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpressionTest.kt @@ -34,7 +34,7 @@ internal class AutoDiffTest { println(z.derivative(x)) println(z.derivative(y, x)) assertEquals(z.derivative(x, y), z.derivative(y, x)) - //check that improper order cause failure + // check improper order cause failure assertFails { z.derivative(x, x, y) } } } diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt index 15c9120ec..b0e5d7904 100644 --- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt +++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt @@ -27,7 +27,7 @@ internal class OptimizeTest { fun testGradientOptimization() { val result = normal.optimize(x, y) { initialGuess(x to 1.0, y to 1.0) - //no need to select optimizer. Gradient optimizer is used by default because gradients are provided by function + // no need to select optimizer. Gradient optimizer is used by default because gradients are provided by function. } println(result.point) println(result.value) 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 ce1265b2f..19befe6e6 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 @@ -6,7 +6,7 @@ package space.kscience.kmath.expressions /** - * Represents expression which structure can be differentiated. + * Represents expression, which structure can be differentiated. * * @param T the type this expression takes as argument and returns. */ 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 84e66918f..5105c2bec 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 @@ -43,7 +43,7 @@ public operator fun Expression.invoke(vararg pairs: Pair): T = /** * Calls this expression from arguments. * - * @param pairs the pairs of arguments' names to values. + * @param pairs the pairs of arguments' names to value. * @return a value. */ @JvmName("callByString") @@ -60,7 +60,7 @@ public operator fun Expression.invoke(vararg pairs: Pair): T = public interface ExpressionAlgebra : Algebra { /** - * A constant expression which does not depend on arguments + * A constant expression that does not depend on arguments. */ public fun const(value: T): E } 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 5935424b6..838118339 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 @@ -18,7 +18,7 @@ public abstract class FunctionalExpressionAlgebra>( public val algebra: A, ) : ExpressionAlgebra> { /** - * Builds an Expression of constant expression which does not depend on arguments. + * Builds an Expression of constant expression that does not depend on arguments. */ override fun const(value: T): Expression = Expression { value } 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 9c3ffd819..d63f141ba 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 @@ -8,8 +8,8 @@ package space.kscience.kmath.linear import space.kscience.kmath.nd.as1D /** - * A group of methods to solve for *X* in equation *X = A -1 · B*, where *A* and *B* are matrices or - * vectors. + * A group of methods to solve for *X* in equation *X = A−1 · B*, where *A* and *B* are + * matrices or vectors. * * @param T the type of items. */ 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 badf2794e..63b881105 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 @@ -164,7 +164,7 @@ public interface LinearSpace> { public operator fun T.times(v: Point): Point = v * this /** - * Get a feature of the structure in this scope. Structure features take precedence other context features + * Get a feature of the structure in this scope. Structure features take precedence other context features. * * @param F the type of feature. * @param structure the structure. @@ -195,7 +195,7 @@ public interface LinearSpace> { } /** - * Get a feature of the structure in this scope. Structure features take precedence other context features + * 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. 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 f3653d394..25f53f65d 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 @@ -114,7 +114,7 @@ public fun > LinearSpace>.lup( for (i in 0 until col) sum -= luRow[i] * lu[i, col] luRow[col] = sum - // maintain best permutation choice + // maintain the best permutation choice if (abs(sum) > largest) { largest = abs(sum) max = row @@ -241,7 +241,7 @@ public fun LinearSpace.solveWithLup(a: Matrix, b: M } /** - * Inverses a square matrix using LUP decomposition. Non square matrix will throw a error. + * Inverses a square matrix using LUP decomposition. Non square matrix will throw an error. */ public fun LinearSpace.inverseWithLup(matrix: Matrix): Matrix = solveWithLup(matrix, one(matrix.rowNum, matrix.colNum)) \ 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 index e0d733a66..b70e9d8a9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixFeatures.kt @@ -31,7 +31,7 @@ public object ZeroFeature : DiagonalFeature public object UnitFeature : DiagonalFeature /** - * Matrices with this feature can be inverted: [inverse] = `a`-1 where `a` is the owning matrix. + * Matrices with this feature can be inverted: *[inverse] = a−1* where *a* is the owning matrix. * * @param T the type of matrices' items. */ 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 4b624b6a0..df3852d72 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 @@ -22,7 +22,8 @@ public class MatrixWrapper internal constructor( ) : Matrix by origin { /** - * Get the first feature matching given class. Does not guarantee that matrix has only one feature matching the criteria + * Get the first feature matching given class. Does not guarantee that matrix has only one feature matching the + * criteria. */ @UnstableKMathAPI @Suppress("UNCHECKED_CAST") @@ -61,7 +62,7 @@ public operator fun Matrix.plus(newFeatures: Collection LinearSpace>.one( rows: Int, @@ -87,7 +88,7 @@ public class TransposedFeature(public val original: Matrix) : Ma * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A` */ @OptIn(UnstableKMathAPI::class) -public fun Matrix.transpose(): Matrix = getFeature>()?.original ?: VirtualMatrix( +public fun Matrix.transpose(): Matrix = getFeature>()?.original ?: (VirtualMatrix( colNum, rowNum, -) { i, j -> get(j, i) } + TransposedFeature(this) \ No newline at end of file +) { i, j -> get(j, i) } + TransposedFeature(this)) \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/annotations.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/annotations.kt index e521e6237..99f959c86 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/annotations.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/annotations.kt @@ -7,8 +7,8 @@ package space.kscience.kmath.misc /** * Marks declarations that are still experimental in the KMath APIs, which means that the design of the corresponding - * declarations has open issues which may (or may not) lead to their changes in the future. Roughly speaking, there is - * a chance that those declarations will be deprecated in the near future or the semantics of their behavior may change + * 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 near future or the semantics of their behavior may change * in some way that may break some code. */ @MustBeDocumented @@ -17,7 +17,7 @@ package space.kscience.kmath.misc public annotation class UnstableKMathAPI /** - * Marks API which could cause performance problems. The code, marked by this API is not necessary slow, but could cause + * Marks API that could cause performance problems. The code marked by this API is unnecessary slow but could cause * slow-down in some cases. Refer to the documentation and benchmark it to be sure. */ @MustBeDocumented 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 7b0b06a58..454a33e86 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 @@ -11,7 +11,7 @@ import space.kscience.kmath.structures.* import kotlin.reflect.KClass /** - * An exception is thrown when the expected ans actual shape of NDArray differs. + * An exception is thrown when the expected and actual shape of NDArray differ. * * @property expected the expected shape. * @property actual the actual shape. @@ -63,7 +63,7 @@ public interface AlgebraND> { structure.map { value -> this@invoke(value) } /** - * Get a feature of the structure in this scope. Structure features take precedence other context features + * Get a feature of the structure in this scope. Structure features take precedence other context features. * * @param F the type of feature. * @param structure the structure. @@ -79,7 +79,7 @@ public interface AlgebraND> { /** - * Get a feature of the structure in this scope. Structure features take precedence other context features + * 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. @@ -130,15 +130,6 @@ public interface GroupND> : Group>, AlgebraND override fun add(a: StructureND, b: StructureND): StructureND = combine(a, b) { aValue, bValue -> add(aValue, bValue) } -// /** -// * Element-wise multiplication by scalar. -// * -// * @param a the multiplicand. -// * @param k the multiplier. -// * @return the product. -// */ -// override fun multiply(a: NDStructure, k: Number): NDStructure = a.map { multiply(it, k) } - // TODO move to extensions after KEEP-176 /** @@ -226,7 +217,7 @@ public interface RingND> : Ring>, GroupND> : Field>, RingND, ScaleOperations> { +public interface FieldND> : Field>, RingND { /** * Element-wise division. * @@ -256,6 +247,15 @@ public interface FieldND> : Field>, RingND): StructureND = arg.map { divide(it, this@div) } + /** + * Element-wise scaling. + * + * @param a the multiplicand. + * @param value the multiplier. + * @return the product. + */ + override fun scale(a: StructureND, value: Double): StructureND = a.map { scale(it, value) } + // @ThreadLocal // public companion object { // private val realNDFieldCache: MutableMap = hashMapOf() 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 d916bc0d6..1af80149c 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 @@ -110,7 +110,7 @@ internal class MutableBuffer1DWrapper(val buffer: MutableBuffer) : Mutable } /** - * Represent a [StructureND] as [Structure1D]. Throw error in case of dimension mismatch + * Represent a [StructureND] as [Structure1D]. Throw error in case of dimension mismatch. */ public fun StructureND.as1D(): Structure1D = this as? Structure1D ?: if (shape.size == 1) { when (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 cb69bdc00..293faaeda 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 @@ -144,13 +144,16 @@ private class MutableStructure2DWrapper(val structure: MutableStructureND) } /** - * Represent a [StructureND] as [Structure1D]. Throw error in case of dimension mismatch + * Represents a [StructureND] as [Structure2D]. Throws runtime error in case of dimension mismatch. */ public fun StructureND.as2D(): Structure2D = this as? Structure2D ?: when (shape.size) { 2 -> Structure2DWrapper(this) else -> error("Can't create 2d-structure from ${shape.size}d-structure") } +/** + * Represents a [StructureND] as [Structure2D]. Throws runtime error in case of dimension mismatch. + */ public fun MutableStructureND.as2D(): MutableStructure2D = this as? MutableStructure2D ?: when (shape.size) { 2 -> MutableStructure2DWrapper(this) else -> error("Can't create 2d-structure from ${shape.size}d-structure") 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 1ad8289bc..716bd15f3 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 @@ -16,7 +16,7 @@ import kotlin.reflect.KClass public interface StructureFeature /** - * Represents n-dimensional structure, i.e. multidimensional container of items of the same type and size. The number + * Represents n-dimensional structure i.e., multidimensional container of items of the same type and size. The number * of dimensions and items in an array is defined by its shape, which is a sequence of non-negative integers that * specify the sizes of each dimension. * @@ -26,7 +26,7 @@ public interface StructureFeature */ public interface StructureND { /** - * 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 of * this structure. */ public val shape: IntArray @@ -53,8 +53,8 @@ public interface StructureND { public fun elements(): Sequence> /** - * Feature is some additional structure information which allows to access it special properties or hints. - * If the feature is not present, null is returned. + * Feature is some additional structure information that allows to access it special properties or hints. + * If the feature is not present, `null` is returned. */ @UnstableKMathAPI public fun getFeature(type: KClass): F? = null @@ -177,7 +177,7 @@ public inline fun MutableStructureND.mapInPlace(action: (IntArray, T) -> elements().forEach { (index, oldValue) -> this[index] = action(index, oldValue) } /** - * A way to convert ND index to linear one and back. + * A way to convert ND indices to linear one and back. */ public interface Strides { /** 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 cef34dce2..1009742e1 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 @@ -23,15 +23,13 @@ public interface Algebra { * Wraps a raw string to [T] object. This method is designed for three purposes: * * 1. Mathematical constants (`e`, `pi`). - * 2. Variables for expression-like contexts (`a`, `b`, `c`...). - * 3. Literals (`{1, 2}`, (`(3; 4)`)). + * 1. Variables for expression-like contexts (`a`, `b`, `c`…). + * 1. Literals (`{1, 2}`, (`(3; 4)`)). * - * In case if algebra can't parse the string, this method must throw [kotlin.IllegalStateException]. - * - * Returns `null` if symbol could not be bound to the context + * If algebra can't parse the string, then this method must throw [kotlin.IllegalStateException]. * * @param value the raw string. - * @return an object. + * @return an object or `null` if symbol could not be bound to the context. */ public fun bindSymbolOrNull(value: String): T? = null @@ -42,13 +40,12 @@ public interface Algebra { bindSymbolOrNull(value) ?: error("Symbol '$value' is not supported in $this") /** - * Dynamically dispatches an unary operation with the certain name. + * Dynamically dispatches a unary operation with the certain name. * - * This function must has two features: + * Implementations must fulfil the following requirements: * - * 1. In case operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with second `unaryOperation` overload: - * i.e. `unaryOperationFunction(a)(b) == unaryOperation(a, b)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [unaryOperation]: for any `a` and `b`, `unaryOperationFunction(a)(b) == unaryOperation(a, b)`. * * @param operation the name of operation. * @return an operation. @@ -57,13 +54,13 @@ public interface Algebra { error("Unary operation $operation not defined in $this") /** - * Dynamically invokes an unary operation with the certain name. + * Dynamically invokes a unary operation with the certain name. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with second [unaryOperationFunction] overload: - * i.e. `unaryOperationFunction(a)(b) == unaryOperation(a, b)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [unaryOperationFunction]: i.e., for any `a` and `b`, + * `unaryOperationFunction(a)(b) == unaryOperation(a, b)`. * * @param operation the name of operation. * @param arg the argument of operation. @@ -74,11 +71,11 @@ public interface Algebra { /** * Dynamically dispatches a binary operation with the certain name. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with second [binaryOperationFunction] overload: - * i.e. `binaryOperationFunction(a)(b, c) == binaryOperation(a, b, c)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [binaryOperation]: for any `a`, `b`, and `c`, + * `binaryOperationFunction(a)(b, c) == binaryOperation(a, b, c)`. * * @param operation the name of operation. * @return an operation. @@ -89,11 +86,11 @@ public interface Algebra { /** * Dynamically invokes a binary operation with the certain name. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with second [binaryOperationFunction] overload: - * i.e. `binaryOperationFunction(a)(b, c) == binaryOperation(a, b, c)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [binaryOperationFunction]: for any `a`, `b`, and `c`, + * `binaryOperationFunction(a)(b, c) == binaryOperation(a, b, c)`. * * @param operation the name of operation. * @param left the first argument of operation. @@ -115,7 +112,7 @@ public fun Algebra.bindSymbol(symbol: Symbol): T = bindSymbol(symbol.iden public inline operator fun , R> A.invoke(block: A.() -> R): R = run(block) /** - * Represents group without neutral element (also known as inverse semigroup), i.e. algebraic structure with + * Represents group without neutral element (also known as inverse semigroup) i.e., algebraic structure with * associative, binary operation [add]. * * @param T the type of element of this semispace. @@ -130,7 +127,7 @@ public interface GroupOperations : Algebra { */ public fun add(a: T, b: T): T - // Operations to be performed in this context. Could be moved to extensions in case of KEEP-176 + // Operations to be performed in this context. Could be moved to extensions in case of KEEP-176. /** * The negation of this element. @@ -192,7 +189,7 @@ public interface GroupOperations : Algebra { } /** - * Represents group, i.e. algebraic structure with associative, binary operation [add]. + * Represents group i.e., algebraic structure with associative, binary operation [add]. * * @param T the type of element of this semispace. */ @@ -204,7 +201,7 @@ public interface Group : GroupOperations { } /** - * Represents ring without multiplicative and additive identities, i.e. algebraic structure with + * Represents ring without multiplicative and additive identities i.e., algebraic structure with * associative, binary, commutative operation [add] and associative, operation [multiply] distributive over [add]. * * @param T the type of element of this semiring. @@ -240,7 +237,7 @@ public interface RingOperations : GroupOperations { } /** - * Represents ring, i.e. algebraic structure with two associative binary operations called "addition" and + * Represents ring i.e., algebraic structure with two associative binary operations called "addition" and * "multiplication" and their neutral elements. * * @param T the type of element of this ring. @@ -253,8 +250,9 @@ public interface Ring : Group, RingOperations { } /** - * Represents field without without multiplicative and additive identities, i.e. algebraic structure with associative, binary, commutative operations - * [add] and [multiply]; binary operation [divide] as multiplication of left operand by reciprocal of right one. + * Represents field without multiplicative and additive identities i.e., algebraic structure with associative, binary, + * commutative operations [add] and [multiply]; binary operation [divide] as multiplication of left operand by + * reciprocal of right one. * * @param T the type of element of this semifield. */ @@ -291,7 +289,7 @@ public interface FieldOperations : RingOperations { } /** - * Represents field, i.e. algebraic structure with three operations: associative, commutative addition and + * Represents field i.e., algebraic structure with three operations: associative, commutative addition and * multiplication, and division. **This interface differs from the eponymous mathematical definition: fields in KMath * also support associative multiplication by scalar.** * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/AlgebraElements.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/AlgebraElements.kt index 403a9c9b1..6b0e2cabd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/AlgebraElements.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/AlgebraElements.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.operations import space.kscience.kmath.misc.UnstableKMathAPI /** - * The generic mathematics elements which is able to store its context + * The generic mathematics elements that is able to store its context * * @param C the type of mathematical context for this element. * @param T the type wrapped by this wrapper. 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 4ccbfc531..8111691aa 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 @@ -121,7 +121,7 @@ public class BigInt internal constructor( var r = ZERO val bitSize = - (BASE_SIZE * (this.magnitude.size - 1) + log2(this.magnitude.lastOrNull()?.toFloat() ?: 0f + 1)).toInt() + (BASE_SIZE * (this.magnitude.size - 1) + log2(this.magnitude.lastOrNull()?.toFloat() ?: (0f + 1))).toInt() for (i in bitSize downTo 0) { r = r shl 1 @@ -442,7 +442,7 @@ public fun UIntArray.toBigInt(sign: Byte): BigInt { } /** - * Returns null if a valid number can not be read from a string + * Returns `null` if a valid number cannot be read from a string */ public fun String.parseBigInteger(): BigInt? { if (isEmpty()) return null 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 7a8fa5668..92070b33d 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 @@ -26,11 +26,11 @@ public interface NumericAlgebra : Algebra { /** * Dynamically dispatches a binary operation with the certain name with numeric first argument. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with the other [leftSideNumberOperation] overload: - * i.e. `leftSideNumberOperationFunction(a)(b, c) == leftSideNumberOperation(a, b)`. + * 1. If operation is not defined in the structure, then function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [leftSideNumberOperation]: for any `a`, `b`, and `c`, + * `leftSideNumberOperationFunction(a)(b, c) == leftSideNumberOperation(a, b)`. * * @param operation the name of operation. * @return an operation. @@ -41,11 +41,11 @@ public interface NumericAlgebra : Algebra { /** * Dynamically invokes a binary operation with the certain name with numeric first argument. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with second [leftSideNumberOperation] overload: - * i.e. `leftSideNumberOperationFunction(a)(b, c) == leftSideNumberOperation(a, b, c)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [leftSideNumberOperation]: for any `a`, `b`, and `c`, + * `leftSideNumberOperationFunction(a)(b, c) == leftSideNumberOperation(a, b, c)`. * * @param operation the name of operation. * @param left the first argument of operation. @@ -58,11 +58,11 @@ public interface NumericAlgebra : Algebra { /** * Dynamically dispatches a binary operation with the certain name with numeric first argument. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with the other [rightSideNumberOperationFunction] overload: - * i.e. `rightSideNumberOperationFunction(a)(b, c) == leftSideNumberOperation(a, b, c)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [rightSideNumberOperation]: for any `a`, `b`, and `c`, + * `rightSideNumberOperationFunction(a)(b, c) == leftSideNumberOperation(a, b, c)`. * * @param operation the name of operation. * @return an operation. @@ -73,11 +73,11 @@ public interface NumericAlgebra : Algebra { /** * Dynamically invokes a binary operation with the certain name with numeric second argument. * - * This function must follow two properties: + * Implementations must fulfil the following requirements: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. - * 2. This function is symmetric with the other [rightSideNumberOperationFunction] overload: - * i.e. `rightSideNumberOperationFunction(a)(b, c) == rightSideNumberOperation(a, b, c)`. + * 1. If operation is not defined in the structure, then the function throws [kotlin.IllegalStateException]. + * 1. Equivalence to [rightSideNumberOperationFunction]: for any `a`, `b`, and `c`, + * `rightSideNumberOperationFunction(a)(b, c) == rightSideNumberOperation(a, b, c)`. * * @param operation the name of operation. * @param left the first argument of operation. 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 86365394f..31bd47bc3 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 @@ -330,13 +330,13 @@ public fun >> atanh(arg: T): */ public interface Norm { /** - * Computes the norm of [arg] (i.e. absolute value or vector length). + * Computes the norm of [arg] (i.e., absolute value or vector length). */ public fun norm(arg: T): R } /** - * Computes the norm of [arg] (i.e. absolute value or vector length). + * Computes the norm of [arg] (i.e., absolute value or vector length). */ @UnstableKMathAPI public fun >, R> norm(arg: T): R = arg.context.norm(arg) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt index b4ef37598..7a79663c4 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt @@ -32,7 +32,7 @@ public value class DoubleBuffer(public val array: DoubleArray) : MutableBuffer Double): DoubleBuffer = DoubleBuffer(DoubleArray(size) { init(it) }) @@ -47,7 +47,7 @@ public fun DoubleBuffer(vararg doubles: Double): DoubleBuffer = DoubleBuffer(dou public fun DoubleBuffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(doubles) /** - * Returns a new [DoubleArray] containing all of the elements of this [Buffer]. + * Returns a new [DoubleArray] containing all the elements of this [Buffer]. */ public fun Buffer.toDoubleArray(): DoubleArray = when (this) { is DoubleBuffer -> array.copyOf() 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 0b16a3afc..efb8504c2 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 @@ -51,7 +51,7 @@ public fun FlaggedBuffer<*>.hasFlag(index: Int, flag: ValueFlag): Boolean = (get public fun FlaggedBuffer<*>.isMissing(index: Int): Boolean = hasFlag(index, ValueFlag.MISSING) /** - * A real buffer which supports flags for each value like NaN or Missing + * A [Double] buffer that supports flags for each value like `NaN` or Missing. */ public class FlaggedDoubleBuffer(public val values: DoubleArray, public val flags: ByteArray) : FlaggedBuffer, Buffer { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt index 58b7c6aea..e7e98fc71 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt @@ -34,7 +34,7 @@ public value class FloatBuffer(public val array: FloatArray) : MutableBuffer Float): FloatBuffer = FloatBuffer(FloatArray(size) { init(it) }) @@ -44,7 +44,7 @@ public inline fun FloatBuffer(size: Int, init: (Int) -> Float): FloatBuffer = Fl public fun FloatBuffer(vararg floats: Float): FloatBuffer = FloatBuffer(floats) /** - * Returns a new [FloatArray] containing all of the elements of this [Buffer]. + * Returns a new [FloatArray] containing all the elements of this [Buffer]. */ public fun Buffer.toFloatArray(): FloatArray = when (this) { is FloatBuffer -> array.copyOf() diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt index 57b6cfde3..35b722e2b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt @@ -33,7 +33,7 @@ public value class IntBuffer(public val array: IntArray) : MutableBuffer { * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. - * It should return the value for an buffer element given its index. + * It should return the value for a buffer element given its index. */ public inline fun IntBuffer(size: Int, init: (Int) -> Int): IntBuffer = IntBuffer(IntArray(size) { init(it) }) @@ -43,7 +43,7 @@ public inline fun IntBuffer(size: Int, init: (Int) -> Int): IntBuffer = IntBuffe public fun IntBuffer(vararg ints: Int): IntBuffer = IntBuffer(ints) /** - * Returns a new [IntArray] containing all of the elements of this [Buffer]. + * Returns a new [IntArray] containing all the elements of this [Buffer]. */ public fun Buffer.toIntArray(): IntArray = when (this) { is IntBuffer -> array.copyOf() diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt index 57affa1c5..c69f4646d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt @@ -33,7 +33,7 @@ public value class LongBuffer(public val array: LongArray) : MutableBuffer * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. - * It should return the value for an buffer element given its index. + * It should return the value for a buffer element given its index. */ public inline fun LongBuffer(size: Int, init: (Int) -> Long): LongBuffer = LongBuffer(LongArray(size) { init(it) }) @@ -43,7 +43,7 @@ public inline fun LongBuffer(size: Int, init: (Int) -> Long): LongBuffer = LongB public fun LongBuffer(vararg longs: Long): LongBuffer = LongBuffer(longs) /** - * Returns a new [LongArray] containing all of the elements of this [Buffer]. + * Returns a new [LongArray] containing all the elements of this [Buffer]. */ public fun Buffer.toLongArray(): LongArray = when (this) { is LongBuffer -> array.copyOf() diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt index 8dadecff7..20691511b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt @@ -31,7 +31,7 @@ public value class ShortBuffer(public val array: ShortArray) : MutableBuffer Short): ShortBuffer = ShortBuffer(ShortArray(size) { init(it) }) @@ -41,7 +41,7 @@ public inline fun ShortBuffer(size: Int, init: (Int) -> Short): ShortBuffer = Sh public fun ShortBuffer(vararg shorts: Short): ShortBuffer = ShortBuffer(shorts) /** - * Returns a new [ShortArray] containing all of the elements of this [Buffer]. + * Returns a new [ShortArray] containing all the elements of this [Buffer]. */ public fun Buffer.toShortArray(): ShortArray = when (this) { is ShortBuffer -> array.copyOf() 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 8523ac864..f8d2549e5 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 @@ -13,7 +13,7 @@ import kotlinx.coroutines.sync.withLock /** * A not-necessary-Markov chain of some type - * @param T - the chain element type + * @param T the chain element type */ public interface Chain : Flow { /** @@ -22,7 +22,7 @@ public interface Chain : Flow { public suspend fun next(): T /** - * Create a copy of current chain state. Consuming resulting chain does not affect initial chain + * Create a copy of current chain state. Consuming resulting chain does not affect initial chain. */ public suspend fun fork(): Chain @@ -62,9 +62,11 @@ public class MarkovChain(private val seed: suspend () -> R, private } /** - * A chain with possibly mutable state. The state must not be changed outside the chain. Two chins should never share the state - * @param S - the state of the chain - * @param forkState - the function to copy current state without modifying it + * A chain with possibly mutable state. The state must not be changed outside the chain. Two chins should never share + * the state. + * + * @param S the state of the chain. + * @param forkState the function to copy current state without modifying it. */ public class StatefulChain( private val state: S, @@ -96,7 +98,7 @@ public class ConstantChain(public val value: T) : Chain { /** * Map the chain result using suspended transformation. Initial chain result can no longer be safely consumed - * since mapped chain consumes tokens. Accepts regular transformation function + * since mapped chain consumes tokens. Accepts regular transformation function. */ public fun Chain.map(func: suspend (T) -> R): Chain = object : Chain { override suspend fun next(): R = func(this@map.next()) 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 0d6a1178a..4d4493aa4 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 @@ -76,7 +76,7 @@ public fun Flow.chunked(bufferSize: Int): Flow = flow { /** * Map a flow to a moving window buffer. The window step is one. - * In order to get different steps, one could use skip operation. + * To get different steps, one could use skip operation. */ public fun Flow.windowed(window: Int): Flow> = flow { require(window > 1) { "Window size must be more than one" } 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 deb297913..eb099bd79 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 @@ -20,7 +20,7 @@ import kotlin.jvm.JvmInline public interface DMatrix : Structure2D { public companion object { /** - * Coerces a regular matrix to a matrix with type-safe dimensions and throws a error if coercion failed + * Coerces a regular matrix to a matrix with type-safe dimensions and throws an error if coercion failed */ public inline fun coerce(structure: Structure2D): DMatrix { require(structure.rowNum == Dimension.dim()) { @@ -35,7 +35,7 @@ public interface DMatrix : Structure2D { } /** - * The same as [DMatrix.coerce] but without dimension checks. Use with caution + * The same as [DMatrix.coerce] but without dimension checks. Use with caution. */ public fun coerceUnsafe(structure: Structure2D): DMatrix = DMatrixWrapper(structure) diff --git a/kmath-for-real/build.gradle.kts b/kmath-for-real/build.gradle.kts index 25fdefaba..4cccaef5c 100644 --- a/kmath-for-real/build.gradle.kts +++ b/kmath-for-real/build.gradle.kts @@ -12,7 +12,7 @@ kotlin.sourceSets.commonMain { readme { description = """ - Extension module that should be used to achieve numpy-like behavior. + Extension module that should be used to achieve numpy-like behavior. All operations are specialized to work with `Double` numbers without declaring algebraic contexts. One can still use generic algebras though. """.trimIndent() diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt index d3867ea89..0508e9ca3 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt @@ -22,7 +22,7 @@ public typealias DoubleVector = Point public fun DoubleVector(vararg doubles: Double): DoubleVector = doubles.asBuffer() /** - * Fill the vector of given [size] with given [value] + * Fill the vector with given [size] with given [value] */ @UnstableKMathAPI public fun Buffer.Companion.same(size: Int, value: Number): DoubleVector = double(size) { value.toDouble() } 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 c3556216d..1926ef02c 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 @@ -45,8 +45,8 @@ public fun Buffer.Companion.withFixedStep(range: ClosedFloatingPointRange> PiecewisePolynomial( } /** - * An optimized piecewise which uses not separate pieces, but a range separated by delimiters. - * The pieces search is logarithmic + * An optimized piecewise that uses not separate pieces, but a range separated by delimiters. + * The pieces search is logarithmic. */ private class OrderedPiecewisePolynomial>( override val pieces: List, Polynomial>>, @@ -79,7 +79,7 @@ public class PiecewiseBuilder>(delimiter: T) { /** * Dynamically adds a piece to the right side (beyond maximum argument value of previous piece) * - * @param right new rightmost position. If is less then current rightmost position, an error is thrown. + * @param right new rightmost position. If is less than current rightmost position, an error is thrown. * @param piece the sub-function. */ public fun putRight(right: T, piece: Polynomial) { @@ -91,7 +91,7 @@ public class PiecewiseBuilder>(delimiter: T) { /** * Dynamically adds a piece to the left side (beyond maximum argument value of previous piece) * - * @param left the new leftmost position. If is less then current rightmost position, an error is thrown. + * @param left the new leftmost position. If is less than current rightmost position, an error is thrown. * @param piece the sub-function. */ public fun putLeft(left: T, piece: Polynomial) { @@ -114,7 +114,7 @@ public fun > PiecewisePolynomial( ): PiecewisePolynomial = PiecewiseBuilder(startingPoint).apply(builder).build() /** - * Return a value of polynomial function with given [ring] an given [arg] or null if argument is outside of piecewise + * Return a value of polynomial function with given [ring] a given [arg] or null if argument is outside piecewise * definition. */ public fun , C : Ring> PiecewisePolynomial.value(ring: C, arg: T): T? = 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 283f97557..278ef6abe 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 @@ -10,15 +10,17 @@ import space.kscience.kmath.structures.Buffer 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: - * [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. - * [UnivariateIntegrandRanges] - Set of ranges and number of points per range. Defaults to given [IntegrationRange] and [IntegrandMaxCalls] + * + * * [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. + * * [UnivariateIntegrandRanges]—set of ranges and number of points per range. Defaults to given + * [IntegrationRange] and [IntegrandMaxCalls]. */ public class GaussIntegrator( public val algebra: Field, @@ -71,14 +73,14 @@ public class GaussIntegrator( } /** - * Create a Gauss-Legendre integrator for this field + * Create a Gauss-Legendre integrator for this field. * @see [GaussIntegrator] */ -public val Field.gaussIntegrator: GaussIntegrator get() = GaussIntegrator(this) +public val Field.gaussIntegrator: GaussIntegrator get() = GaussIntegrator(this) /** - * Integrate using [intervals] segments with Gauss-Legendre rule of [order] order + * Integrate using [intervals] segments with Gauss-Legendre rule of [order] order. */ @UnstableKMathAPI public fun GaussIntegrator.integrate( 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 594ca9940..2eceec135 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 @@ -72,7 +72,7 @@ public object GaussLegendreRuleFactory : GaussIntegratorRuleFactory { } // Get previous rule. - // If it has not been computed yet it will trigger a recursive call + // If it has not been computed, yet it will trigger a recursive call // to this method. val previousPoints: Buffer = getOrBuildRule(numPoints - 1).first @@ -146,7 +146,7 @@ public object GaussLegendreRuleFactory : GaussIntegratorRuleFactory { } // If "numPoints" is odd, 0 is a root. // Note: as written, the test for oddness will work for negative - // integers too (although it is not necessary here), preventing + // integers too (although it is unnecessary here), preventing // a FindBugs warning. if (numPoints % 2 != 0) { var pmc = 1.0 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 baa9d4af8..c01c30ec2 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 @@ -13,9 +13,10 @@ import space.kscience.kmath.operations.sum /** * Use double pass Simpson rule integration with a fixed number of points. - * Requires [UnivariateIntegrandRanges] or [IntegrationRange] and [IntegrandMaxCalls] - * [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. + * Requires [UnivariateIntegrandRanges] or [IntegrationRange] and [IntegrandMaxCalls]. + * * [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. */ @UnstableKMathAPI public class SimpsonIntegrator( @@ -63,12 +64,12 @@ public val Field.simpsonIntegrator: SimpsonIntegrator get() = Si /** * Use double pass Simpson rule integration with a fixed number of points. - * Requires [UnivariateIntegrandRanges] or [IntegrationRange] and [IntegrandMaxCalls] - * [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. + * Requires [UnivariateIntegrandRanges] or [IntegrationRange] and [IntegrandMaxCalls]. + * * [IntegrationRange]—the univariate range of integration. By default, uses `0.0..1.0` 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. */ public object DoubleSimpsonIntegrator : UnivariateIntegrator { - private fun integrateRange( integrand: UnivariateIntegrand, range: ClosedRange, numPoints: Int, ): Double { 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 23d7bdd8d..07b37bd6a 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 @@ -45,8 +45,9 @@ public fun > PiecewisePolynomial.integrate( /** * A generic spline-interpolation-based analytic integration - * [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. + * * [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. */ @UnstableKMathAPI public class SplineIntegrator>( @@ -57,6 +58,7 @@ public class SplineIntegrator>( val range = integrand.getFeature()?.range ?: 0.0..1.0 val interpolator: PolynomialInterpolator = SplineInterpolator(algebra, bufferFactory) + val nodes: Buffer = integrand.getFeature()?.nodes ?: run { val numPoints = integrand.getFeature()?.maxCalls ?: 100 val step = (range.endInclusive - range.start) / (numPoints - 1) @@ -75,15 +77,16 @@ public class SplineIntegrator>( /** * A simplified double-based spline-interpolation-based analytic integration - * [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. + * * [IntegrationRange]—the univariate range of integration. By default, uses `0.0..1.0` 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. */ @UnstableKMathAPI public object DoubleSplineIntegrator : UnivariateIntegrator { override fun integrate(integrand: UnivariateIntegrand): UnivariateIntegrand { val range = integrand.getFeature()?.range ?: 0.0..1.0 - val interpolator: PolynomialInterpolator = SplineInterpolator(DoubleField, ::DoubleBuffer) + val nodes: Buffer = integrand.getFeature()?.nodes ?: run { val numPoints = integrand.getFeature()?.maxCalls ?: 100 val step = (range.endInclusive - range.start) / (numPoints - 1) 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 7f68284a7..9c8aeef1b 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 @@ -40,8 +40,8 @@ public class IntegrationRange(public val range: ClosedRange) : Integrand } /** - * Set of univariate integration ranges. First components correspond to ranges themselves, second components to number of - * integration nodes per range + * Set of univariate integration ranges. First components correspond to the ranges themselves, second components to + * number of integration nodes per range. */ public class UnivariateIntegrandRanges(public val ranges: List, Int>>) : IntegrandFeature { public constructor(vararg pairs: Pair, Int>) : this(pairs.toList()) 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 31da4f392..4e803fc63 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 @@ -11,7 +11,7 @@ import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.asBuffer /** - * The binned data element. Could be a histogram bin with a number of counts or an artificial construct + * The binned data element. Could be a histogram bin with a number of counts or an artificial construct. */ public interface Bin : Domain { /** diff --git a/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/UnivariateHistogram.kt b/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/UnivariateHistogram.kt index f461ee4fa..ca37279c3 100644 --- a/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/UnivariateHistogram.kt +++ b/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/UnivariateHistogram.kt @@ -16,9 +16,10 @@ public val UnivariateDomain.center: Double get() = (range.endInclusive + range.start) / 2 /** - * A univariate bin based an a range - * @param value The value of histogram including weighting - * @param standardDeviation Standard deviation of the bin value. Zero or negative if not applicable + * A univariate bin based on a range + * + * @property value The value of histogram including weighting + * @property standardDeviation Standard deviation of the bin value. Zero or negative if not applicable */ @UnstableKMathAPI public class UnivariateBin( diff --git a/kmath-jafama/README.md b/kmath-jafama/README.md index 71097771d..3c5d4e19d 100644 --- a/kmath-jafama/README.md +++ b/kmath-jafama/README.md @@ -52,22 +52,4 @@ fun main() { According to KMath benchmarks on GraalVM, Jafama functions are slower than JDK math; however, there are indications that on Hotspot Jafama is a bit faster. -
- -Report for benchmark configuration jafamaDouble - - -* Run on OpenJDK 64-Bit Server VM (build 11.0.11+8-jvmci-21.1-b05) with Java process: - -``` -/home/commandertvis/graalvm-ce-java11/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCIProduct -XX:-UnlockExperimentalVMOptions -XX:ThreadPriorityPolicy=1 -javaagent:/home/commandertvis/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.5.0/d8cebccdcddd029022aa8646a5a953ff88b13ac8/kotlinx-coroutines-core-jvm-1.5.0.jar -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -ea -``` -* JMH 1.21 was used in `thrpt` mode with 1 warmup iteration by 1000 ms and 5 measurement iterations by 1000 ms. - -| Benchmark | Score | -|:---------:|:-----:| -|`space.kscience.kmath.benchmarks.JafamaBenchmark.core`|14.35014650168397 ± 0.9200669832937576 ops/s| -|`space.kscience.kmath.benchmarks.JafamaBenchmark.jafama`|12.048429204455887 ± 1.2882929181842269 ops/s| -|`space.kscience.kmath.benchmarks.JafamaBenchmark.strictJafama`|12.977653357239152 ± 1.4122819627470866 ops/s| -
- +> **Can't find appropriate benchmark data. Try generating readme files after running benchmarks**. diff --git a/kmath-kotlingrad/README.md b/kmath-kotlingrad/README.md index 3b337460f..aeb44ea13 100644 --- a/kmath-kotlingrad/README.md +++ b/kmath-kotlingrad/README.md @@ -1,6 +1,6 @@ # Module kmath-kotlingrad -[Kotlin∇](https://www.htmlsymbols.xyz/unicode/U+2207) integration module. +[Kotlin∇](https://github.com/breandan/kotlingrad) integration module. - [differentiable-mst-expression](src/main/kotlin/space/kscience/kmath/kotlingrad/KotlingradExpression.kt) : MST based DifferentiableExpression. - [scalars-adapters](src/main/kotlin/space/kscience/kmath/kotlingrad/scalarsAdapters.kt) : Conversions between Kotlin∇'s SFun and MST diff --git a/kmath-kotlingrad/docs/README-TEMPLATE.md b/kmath-kotlingrad/docs/README-TEMPLATE.md index ac38c849b..bc99bdf5f 100644 --- a/kmath-kotlingrad/docs/README-TEMPLATE.md +++ b/kmath-kotlingrad/docs/README-TEMPLATE.md @@ -1,6 +1,6 @@ # Module kmath-kotlingrad -[Kotlin∇](https://www.htmlsymbols.xyz/unicode/U+2207) integration module. +[Kotlin∇](https://github.com/breandan/kotlingrad) integration module. ${features} 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 930b21095..e8e51e9e2 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 @@ -156,6 +156,6 @@ public expect fun Memory.Companion.allocate(length: Int): Memory /** * Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied - * and could be mutated independently from the resulting [Memory]. + * and could be mutated independently of the resulting [Memory]. */ public expect fun Memory.Companion.wrap(array: ByteArray): Memory 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 9a622ea36..6153743fc 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 @@ -95,7 +95,7 @@ public actual fun Memory.Companion.allocate(length: Int): Memory { /** * Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied - * and could be mutated independently from the resulting [Memory]. + * and could be mutated independently of the resulting [Memory]. */ public actual fun Memory.Companion.wrap(array: ByteArray): Memory { @Suppress("CAST_NEVER_SUCCEEDS") val int8Array = array as Int8Array 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 944e8455b..aef68fd80 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 @@ -103,7 +103,7 @@ public actual fun Memory.Companion.allocate(length: Int): Memory = /** * Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied - * and could be mutated independently from the resulting [Memory]. + * and could be mutated independently of the resulting [Memory]. */ public actual fun Memory.Companion.wrap(array: ByteArray): Memory = ByteBufferMemory(checkNotNull(ByteBuffer.wrap(array))) 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 d31c9e8f4..5146d9689 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 @@ -60,7 +60,7 @@ internal class NativeMemory( } override fun writeByte(offset: Int, value: Byte) { - array.set(position(offset), value) + array[position(offset)] = value } override fun writeShort(offset: Int, value: Short) { @@ -85,7 +85,7 @@ internal class NativeMemory( /** * Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied - * and could be mutated independently from the resulting [Memory]. + * and could be mutated independently of the resulting [Memory]. */ public actual fun Memory.Companion.wrap(array: ByteArray): Memory = NativeMemory(array) 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 7a650df3c..f69f831e8 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 @@ -35,7 +35,7 @@ public sealed interface Nd4jArrayAlgebra> : AlgebraND /** - * Unwraps to or acquires [INDArray] from [StructureND]. + * Unwraps to or get [INDArray] from [StructureND]. */ public val StructureND.ndArray: INDArray 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 97427d5c6..2a0fdc86c 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 @@ -17,7 +17,7 @@ import space.kscience.kmath.nd.StructureND */ public sealed class Nd4jArrayStructure : MutableStructureND { /** - * The wrapped [INDArray]. Since KMath uses [Int] indexes, assuming that the size of [INDArray] is less or equal to + * The wrapped [INDArray]. Since KMath uses [Int] indexes, assuming the size of [INDArray] is less or equal to * [Int.MAX_VALUE]. */ public abstract val ndArray: INDArray 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 0674c565e..1fdf845a6 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 @@ -29,7 +29,7 @@ public sealed interface Nd4jTensorAlgebra : AnalyticTensorAlgebra public fun INDArray.wrap(): Nd4jArrayStructure /** - * Unwraps to or acquires [INDArray] from [StructureND]. + * Unwraps to or gets [INDArray] from [StructureND]. */ public val StructureND.ndArray: INDArray 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 5a74a9a18..3d3f95f8f 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 @@ -22,7 +22,7 @@ public interface Distribution : Sampler { override fun sample(generator: RandomGenerator): Chain /** - * An empty companion. Distribution factories should be written as its extensions + * An empty companion. Distribution factories should be written as its extensions. */ public companion object } 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 dde429244..1218f13c5 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 @@ -10,12 +10,12 @@ import space.kscience.kmath.chains.SimpleChain import space.kscience.kmath.stat.RandomGenerator /** - * A multivariate distribution which takes a map of parameters + * A multivariate distribution that takes a map of parameters. */ public interface NamedDistribution : Distribution> /** - * A multivariate distribution that has independent distributions for separate axis + * A multivariate distribution that has independent distributions for separate axis. */ public class FactorizedDistribution(public val distributions: Collection>) : NamedDistribution { diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/internal/InternalGamma.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/internal/InternalGamma.kt index a584af4f9..6e7eb039d 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/internal/InternalGamma.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/internal/InternalGamma.kt @@ -110,7 +110,7 @@ internal object InternalGamma { x <= 8.0 -> { val n = floor(x - 1.5).toInt() - val prod = (1..n).fold(1.0, { prod, i -> prod * (x - i) }) + val prod = (1..n).fold(1.0) { prod, i -> prod * (x - i) } logGamma1p(x - (n + 1)) + ln(prod) } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt index f54ba5723..997f1bbe3 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/FunctionOptimization.kt @@ -11,27 +11,28 @@ import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.indices /** - * A likelihood function optimization problem with provided derivatives + * A likelihood function optimization problem with provided derivatives. */ public interface FunctionOptimization : Optimization { /** - * The optimization direction. If true search for function maximum, if false, search for the minimum + * The optimization direction. If true search for function maximum, if false, search for the minimum. */ public var maximize: Boolean /** - * Define the initial guess for the optimization problem + * Defines the initial guess for the optimization problem. */ public fun initialGuess(map: Map) /** - * Set a differentiable expression as objective function as function and gradient provider + * Set a differentiable expression as objective function as function and gradient provider. */ public fun diffFunction(expression: DifferentiableExpression) public companion object { /** - * Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation + * Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic + * differentiation. */ public fun chiSquared( autoDiff: AutoDiffProcessor>, @@ -61,7 +62,7 @@ public interface FunctionOptimization : Optimization { } /** - * Define a chi-squared-based objective function + * Defines a chi-squared-based objective function. */ public fun FunctionOptimization.chiSquared( autoDiff: AutoDiffProcessor>, @@ -76,7 +77,7 @@ public fun FunctionOptimization.chiSquared( } /** - * Optimize differentiable expression using specific [OptimizationProblemFactory] + * Optimizes differentiable expression using specific [OptimizationProblemFactory]. */ public fun > DifferentiableExpression.optimizeWith( factory: OptimizationProblemFactory, diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/NoDerivFunctionOptimization.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/NoDerivFunctionOptimization.kt index 0f2167549..44b66169f 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/NoDerivFunctionOptimization.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/NoDerivFunctionOptimization.kt @@ -16,7 +16,7 @@ import kotlin.math.pow */ public interface NoDerivFunctionOptimization : Optimization { /** - * The optimization direction. If true search for function maximum, if false, search for the minimum + * The optimization direction. If `true` search for function maximum, search for the minimum otherwise. */ public var maximize: Boolean diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimization.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimization.kt index 15ae494f6..f85658d87 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimization.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/optimization/Optimization.kt @@ -24,7 +24,7 @@ public operator fun OptimizationResult.plus( ): OptimizationResult = OptimizationResult(point, value, features + feature) /** - * An optimization problem builder over [T] variables + * A builder of optimization problems over [T] variables */ public interface Optimization { 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 93605c7d8..993215d41 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 @@ -14,9 +14,9 @@ import kotlin.math.* /** * Sampling from the [gamma distribution](http://mathworld.wolfram.com/GammaDistribution.html). - * - For 0 < alpha < 1: + * * For 0 < alpha < 1: * Ahrens, J. H. and Dieter, U., Computer methods for sampling from gamma, beta, Poisson and binomial distributions, Computing, 12, 223-246, 1974. - * - For alpha >= 1: + * * For alpha >= 1: * Marsaglia and Tsang, A Simple Method for Generating Gamma Variables. ACM Transactions on Mathematical Software, Volume 26 Issue 3, September, 2000. * * Based on Commons RNG implementation. 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 36ddd3c8e..5390a2e09 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 @@ -20,7 +20,7 @@ import kotlin.math.min * implements Vose's algorithm. * * Vose, M.D., A linear algorithm for generating random numbers with a given distribution, IEEE Transactions on - * Software Engineering, 17, 972-975, 1991. he algorithm will sample values in O(1) time after a pre-processing step + * Software Engineering, 17, 972-975, 1991. The algorithm will sample values in O(1) time after a pre-processing step * of O(n) time. * * The alias tables are constructed using fraction probabilities with an assumed denominator of 253. In the generic @@ -77,7 +77,7 @@ public open class AliasMethodDiscreteSampler private constructor( } override fun sample(generator: RandomGenerator): Chain = generator.chain { - // This implements the algorithm as per Vose (1991): + // This implements the algorithm in accordance with Vose (1991): // v = uniform() in [0, 1) // j = uniform(n) in [0, n) // if v < prob[j] then @@ -95,7 +95,7 @@ public open class AliasMethodDiscreteSampler private constructor( // p(j) == 1 => j // However it is assumed these edge cases are rare: // - // The probability table will be 1 for approximately 1/n samples, i.e. only the + // The probability table will be 1 for approximately 1/n samples i.e., only the // last unpaired probability. This is only worth checking for when the table size (n) // is small. But in that case the user should zero-pad the table for performance. // @@ -211,7 +211,7 @@ public open class AliasMethodDiscreteSampler private constructor( // c: 2=2/3; 6=1/3 (6 is the alias) // d: 1=1/3; 6=2/3 (6 is the alias) // - // The sample is obtained by randomly selecting a section, then choosing which category + // The sample is obtained by randomly selecting a section, then choosing, which category // from the pair based on a uniform random deviate. val sumProb = InternalUtils.validateProbabilities(probabilities) // Allow zero-padding @@ -241,9 +241,9 @@ public open class AliasMethodDiscreteSampler private constructor( val alias = IntArray(n) // This loop uses each large in turn to fill the alias table for small probabilities that - // do not reach the requirement to fill an entire section alone (i.e. p < mean). + // do not reach the requirement to fill an entire section alone (i.e., p < mean). // Since the sum of the small should be less than the sum of the large it should use up - // all the small first. However floating point round-off can result in + // all the small first. However, floating point round-off can result in // misclassification of items as small or large. The Vose algorithm handles this using // a while loop conditioned on the size of both sets and a subsequent loop to use // unpaired items. 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 14737decb..0105731c4 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 @@ -13,7 +13,7 @@ import kotlin.math.exp /** * Sampler for the Poisson distribution. - * - Kemp, A, W, (1981) Efficient Generation of Logarithmically Distributed Pseudo-Random Variables. Journal of the Royal Statistical Society. Vol. 30, No. 3, pp. 249-253. + * * Kemp, A, W, (1981) Efficient Generation of Logarithmically Distributed Pseudo-Random Variables. Journal of the Royal Statistical Society. Vol. 30, No. 3, pp. 249-253. * This sampler is suitable for mean < 40. For large means, LargeMeanPoissonSampler should be used instead. * * Note: The algorithm uses a recurrence relation to compute the Poisson probability and a rolling summation for the cumulative probability. When the mean is large the initial probability (Math.exp(-mean)) is zero and an exception is raised by the constructor. @@ -66,8 +66,7 @@ public class KempSmallMeanPoissonSampler internal constructor( public fun KempSmallMeanPoissonSampler(mean: Double): KempSmallMeanPoissonSampler { require(mean > 0) { "Mean is not strictly positive: $mean" } val p0 = exp(-mean) - // Probability must be positive. As mean increases then p(0) decreases. + // Probability must be positive. As mean increases, p(0) decreases. require(p0 > 0) { "No probability for mean: $mean" } return KempSmallMeanPoissonSampler(p0, mean) } - 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 96131aa4b..f0f94900e 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 @@ -18,11 +18,11 @@ private const val PIVOT = 40.0 /** * Sampler for the Poisson distribution. - * - For small means, a Poisson process is simulated using uniform deviates, as described in + * * For small means, a Poisson process is simulated using uniform deviates, as described in * Knuth (1969). Seminumerical Algorithms. The Art of Computer Programming, Volume 2. Chapter 3.4.1.F.3 * Important integer-valued distributions: The Poisson distribution. Addison Wesley. * The Poisson process (and hence, the returned value) is bounded by 1000 * mean. - * - For large means, we use the rejection algorithm described in + * * For large means, we use the rejection algorithm described in * Devroye, Luc. (1981). The Computer Generation of Poisson Random Variables Computing vol. 26 pp. 197-207. * * Based on Commons RNG implementation. @@ -35,10 +35,10 @@ public fun PoissonSampler(mean: Double): Sampler { /** * Sampler for the Poisson distribution. - * - For small means, a Poisson process is simulated using uniform deviates, as described in + * * For small means, a Poisson process is simulated using uniform deviates, as described in * Knuth (1969). Seminumerical Algorithms. The Art of Computer Programming, Volume 2. Chapter 3.4.1.F.3 Important * integer-valued distributions: The Poisson distribution. Addison Wesley. - * - The Poisson process (and hence, the returned value) is bounded by 1000 * mean. + * * The Poisson process (and hence, the returned value) is bounded by 1000 * mean. * This sampler is suitable for mean < 40. For large means, [LargeMeanPoissonSampler] should be used instead. * * Based on Commons RNG implementation. diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt index 5698f1a79..f280a78aa 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt @@ -23,7 +23,7 @@ public interface RandomGenerator { public fun nextDouble(): Double /** - * A chunk of doubles of given [size] + * A chunk of doubles of given [size]. */ public fun nextDoubleBuffer(size: Int): DoubleBuffer = DoubleBuffer(size) { nextDouble() } @@ -57,7 +57,7 @@ public interface RandomGenerator { public fun nextLong(until: Long): Long /** - * Fills a subrange of the specified byte [array] starting from [fromIndex] inclusive and ending [toIndex] exclusive + * Fills a subrange with the specified byte [array] starting from [fromIndex] inclusive and ending [toIndex] exclusive * with random bytes. * * @return [array] with the subrange filled with random bytes. @@ -70,7 +70,7 @@ public interface RandomGenerator { public fun nextBytes(size: Int): ByteArray = ByteArray(size).also { fillBytes(it) } /** - * Create a new generator which is independent from current generator (operations on new generator do not affect this one + * Create a new generator that is independent of current generator (operations on new generator do not affect this one * and vise versa). The statistical properties of new generator should be the same as for this one. * For pseudo-random generator, the fork is keeping the same sequence of numbers for given call order for each run. * diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/SamplerAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/SamplerAlgebra.kt index 849cf152a..044e489b2 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/SamplerAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/SamplerAlgebra.kt @@ -32,7 +32,7 @@ public class BasicSampler(public val chainBuilder: (RandomGenerator } /** - * A space of samplers. Allows to perform simple operations on distributions. + * A space of samplers. Allows performing simple operations on distributions. * * @property algebra the space to provide addition and scalar multiplication for [T]. */ 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 499dcac6a..0af901fc9 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 @@ -30,9 +30,10 @@ public interface BlockingStatistic : Statistic { /** * A statistic tha could be computed separately on different blocks of data and then composed - * @param T - source type - * @param I - intermediate block type - * @param R - result type + * + * @param T the source type. + * @param I the intermediate block type. + * @param R the result type. */ public interface ComposableStatistic : Statistic { //compute statistic on a single block 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 7aaac7d32..202a1c8dd 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 @@ -55,7 +55,7 @@ public class RandomGeneratorProvider(public val generator: RandomGenerator) : Un /** * Generates [Byte] values and places them into a user-supplied array. * - * The number of random bytes produced is equal to the length of the the byte array. + * The number of random bytes produced is equal to the length of the byte array. * * @param bytes byte array in which to put the random bytes. */ 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 6bdecfa85..af4ae8239 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 @@ -23,7 +23,7 @@ public interface LinearOpsTensorAlgebra : TensorPartialDivisionAlgebra { /** * Computes the multiplicative inverse matrix of a square matrix input, or of each square matrix in a batched input. * Given a square matrix `A`, return the matrix `AInv` satisfying - * `A dot AInv = AInv dot A = eye(a.shape[0])`. + * `A dot AInv == AInv dot A == eye(a.shape[0])`. * For more information: https://pytorch.org/docs/stable/linalg.html#torch.linalg.inv * * @return the multiplicative inverse of a matrix. @@ -36,25 +36,27 @@ public interface LinearOpsTensorAlgebra : TensorPartialDivisionAlgebra { * Computes the Cholesky decomposition of a Hermitian (or symmetric for real-valued matrices) * positive-definite matrix or the Cholesky decompositions for a batch of such matrices. * Each decomposition has the form: - * Given a tensor `input`, return the tensor `L` satisfying `input = L dot L.H`, - * where L is a lower-triangular matrix and L.H is the conjugate transpose of L, + * Given a tensor `input`, return the tensor `L` satisfying `input = L dot LH`, + * where `L` is a lower-triangular matrix and `LH` is the conjugate transpose of `L`, * which is just a transpose for the case of real-valued input matrices. * For more information: https://pytorch.org/docs/stable/linalg.html#torch.linalg.cholesky * - * @return the batch of L matrices. + * @receiver the `input`. + * @return the batch of `L` matrices. */ public fun Tensor.cholesky(): Tensor /** * QR decomposition. * - * Computes the QR decomposition of a matrix or a batch of matrices, and returns a pair `(Q, R)` of tensors. - * Given a tensor `input`, return tensors (Q, R) satisfying ``input = Q dot R``, + * Computes the QR decomposition of a matrix or a batch of matrices, and returns a pair `Q to R` of tensors. + * Given a tensor `input`, return tensors `Q to R` satisfying `input == Q dot R`, * with `Q` being an orthogonal matrix or batch of orthogonal matrices * and `R` being an upper triangular matrix or batch of upper triangular matrices. * For more information: https://pytorch.org/docs/stable/linalg.html#torch.linalg.qr * - * @return pair of Q and R tensors. + * @receiver the `input`. + * @return pair of `Q` and `R` tensors. */ public fun Tensor.qr(): Pair, Tensor> @@ -67,7 +69,8 @@ public interface LinearOpsTensorAlgebra : TensorPartialDivisionAlgebra { * `L` being a lower triangular matrix or batch of matrices, * `U` being an upper triangular matrix or batch of matrices. * - * * @return triple of P, L and U tensors + * @receiver the `input`. + * @return triple of P, L and U tensors */ public fun Tensor.lu(): Triple, Tensor, Tensor> @@ -75,22 +78,25 @@ public interface LinearOpsTensorAlgebra : TensorPartialDivisionAlgebra { * Singular Value Decomposition. * * Computes the singular value decomposition of either a matrix or batch of matrices `input`. - * The singular value decomposition is represented as a triple `(U, S, V)`, - * such that `input = U dot diagonalEmbedding(S) dot V.H`, - * where V.H is the conjugate transpose of V. - * If input is a batch of tensors, then U, S, and Vh are also batched with the same batch dimensions as input. + * The singular value decomposition is represented as a triple `Triple(U, S, V)`, + * such that `input = U dot diagonalEmbedding(S) dot VH`, + * where `VH` is the conjugate transpose of V. + * If `input` is a batch of tensors, then `U`, `S`, and `VH` are also batched with the same batch dimensions as + * `input`. * For more information: https://pytorch.org/docs/stable/linalg.html#torch.linalg.svd * - * @return triple `(U, S, V)`. + * @receiver the `input`. + * @return triple `Triple(U, S, V)`. */ public fun Tensor.svd(): Triple, Tensor, Tensor> /** - * Returns eigenvalues and eigenvectors of a real symmetric matrix input or a batch of real symmetric matrices, - * represented by a pair (eigenvalues, eigenvectors). + * Returns eigenvalues and eigenvectors of a real symmetric matrix `input` or a batch of real symmetric matrices, + * represented by a pair `eigenvalues to eigenvectors`. * For more information: https://pytorch.org/docs/stable/generated/torch.symeig.html * - * @return a pair (eigenvalues, eigenvectors) + * @receiver the `input`. + * @return a pair `eigenvalues to eigenvectors` */ public fun Tensor.symEig(): Pair, Tensor> 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 62b8ef046..d48e7b1c9 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 @@ -14,7 +14,6 @@ import space.kscience.kmath.operations.Algebra * @param T the type of items in the tensors. */ public interface TensorAlgebra : Algebra> { - /** * Returns a single tensor value of unit dimension if tensor shape equals to [1]. * @@ -189,7 +188,7 @@ public interface TensorAlgebra : Algebra> { /** * View this tensor as the same size as [other]. - * ``this.viewAs(other) is equivalent to this.view(other.shape)``. + * `this.viewAs(other)` is equivalent to `this.view(other.shape)`. * For more information: https://pytorch.org/cppdocs/notes/tensor_indexing.html * * @param other the result tensor has the same size as other. @@ -217,14 +216,14 @@ public interface TensorAlgebra : Algebra> { * a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. * If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix * multiple and removed after. - * The non-matrix (i.e. batch) dimensions are broadcast (and thus must be broadcastable). + * The non-matrix (i.e., batch) dimensions are broadcast (and thus must be broadcastable). * For example, if `input` is a (j × 1 × n × n) tensor and `other` is a * (k × n × n) tensor, out will be a (j × k × n × n) tensor. * * For more information: https://pytorch.org/docs/stable/generated/torch.matmul.html * - * @param other tensor to be multiplied - * @return mathematical product of two tensors + * @param other tensor to be multiplied. + * @return a mathematical product of two tensors. */ public infix fun Tensor.dot(other: Tensor): Tensor @@ -234,7 +233,7 @@ public interface TensorAlgebra : Algebra> { * To facilitate creating batched diagonal matrices, * the 2D planes formed by the last two dimensions of the returned tensor are chosen by default. * - * The argument [offset] controls which diagonal to consider: + * The argument [offset] controls, which diagonal to consider: * 1. If [offset] = 0, it is the main diagonal. * 1. If [offset] > 0, it is above the main diagonal. * 1. If [offset] < 0, it is below the main diagonal. @@ -321,7 +320,7 @@ public interface TensorAlgebra : Algebra> { * * @param dim the dimension to reduce. * @param keepDim whether the output tensor has [dim] retained or not. - * @return the the index of maximum value of each row of the input tensor in the given dimension [dim]. + * @return the index of maximum value of each row of the input tensor in the given dimension [dim]. */ public fun Tensor.argMax(dim: Int, keepDim: Boolean): Tensor } 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 02bf5415d..867d4b26e 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 @@ -49,7 +49,7 @@ public interface TensorPartialDivisionAlgebra : TensorAlgebra { /** * Each element of this tensor is divided by each element of the [other] tensor. * - * @param other tensor to be divide by. + * @param other tensor to be divided by. */ public operator fun Tensor.divAssign(other: Tensor) } 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 d182558c5..d6ad8e8e2 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 @@ -90,37 +90,37 @@ public open class DoubleTensorAlgebra : } /** - * Returns a tensor filled with the scalar value 0.0, with the shape defined by the variable argument [shape]. + * Returns a tensor filled with the scalar value `0.0`, with the shape defined by the variable argument [shape]. * * @param shape array of integers defining the shape of the output tensor. - * @return tensor filled with the scalar value 0.0, with the [shape] shape. + * @return tensor filled with the scalar value `0.0`, with the [shape] shape. */ public fun zeros(shape: IntArray): DoubleTensor = full(0.0, shape) /** - * Returns a tensor filled with the scalar value 0.0, with the same shape as a given array. + * Returns a tensor filled with the scalar value `0.0`, with the same shape as a given array. * - * @return tensor filled with the scalar value 0.0, with the same shape as `input` tensor. + * @return tensor filled with the scalar value `0.0`, with the same shape as `input` tensor. */ public fun Tensor.zeroesLike(): DoubleTensor = tensor.fullLike(0.0) /** - * Returns a tensor filled with the scalar value 1.0, with the shape defined by the variable argument [shape]. + * Returns a tensor filled with the scalar value `1.0`, with the shape defined by the variable argument [shape]. * * @param shape array of integers defining the shape of the output tensor. - * @return tensor filled with the scalar value 1.0, with the [shape] shape. + * @return tensor filled with the scalar value `1.0`, with the [shape] shape. */ public fun ones(shape: IntArray): DoubleTensor = full(1.0, shape) /** - * Returns a tensor filled with the scalar value 1.0, with the same shape as a given array. + * Returns a tensor filled with the scalar value `1.0`, with the same shape as a given array. * - * @return tensor filled with the scalar value 1.0, with the same shape as `input` tensor. + * @return tensor filled with the scalar value `1.0`, with the same shape as `input` tensor. */ public fun Tensor.onesLike(): DoubleTensor = tensor.fullLike(1.0) /** - * Returns a 2-D tensor with shape ([n], [n]), with ones on the diagonal and zeros elsewhere. + * Returns a 2D tensor with shape ([n], [n]), with ones on the diagonal and zeros elsewhere. * * @param n the number of rows and columns * @return a 2-D tensor with ones on the diagonal and zeros elsewhere. @@ -311,7 +311,6 @@ public open class DoubleTensorAlgebra : return resTensor } - override fun Tensor.view(shape: IntArray): DoubleTensor { checkView(tensor, shape) return DoubleTensor(shape, tensor.mutableBuffer.array(), tensor.bufferStart) @@ -444,7 +443,7 @@ public open class DoubleTensorAlgebra : /** * Compares element-wise two tensors. - * Comparison of two Double values occurs with 1e-5 precision. + * Comparison of two Double values occurs with `1e-5` precision. * * @param other the tensor to compare with `input` tensor. * @return true if two tensors have the same shape and elements, false otherwise. @@ -473,23 +472,24 @@ public open class DoubleTensorAlgebra : } /** - * Returns a tensor of random numbers drawn from normal distributions with 0.0 mean and 1.0 standard deviation. + * Returns a tensor of random numbers drawn from normal distributions with `0.0` mean and `1.0` standard deviation. * * @param shape the desired shape for the output tensor. * @param seed the random seed of the pseudo-random number generator. * @return tensor of a given shape filled with numbers from the normal distribution - * with 0.0 mean and 1.0 standard deviation. + * with `0.0` mean and `1.0` standard deviation. */ public fun randomNormal(shape: IntArray, seed: Long = 0): DoubleTensor = DoubleTensor(shape, getRandomNormals(shape.reduce(Int::times), seed)) /** * Returns a tensor with the same shape as `input` of random numbers drawn from normal distributions - * with 0.0 mean and 1.0 standard deviation. + * with `0.0` mean and `1.0` standard deviation. * + * @receiver the `input`. * @param seed the random seed of the pseudo-random number generator. - * @return tensor with the same shape as `input` filled with numbers from the normal distribution - * with 0.0 mean and 1.0 standard deviation. + * @return a tensor with the same shape as `input` filled with numbers from the normal distribution + * with `0.0` mean and `1.0` standard deviation. */ public fun Tensor.randomNormalLike(seed: Long = 0): DoubleTensor = DoubleTensor(tensor.shape, getRandomNormals(tensor.shape.reduce(Int::times), seed)) @@ -512,10 +512,10 @@ public open class DoubleTensorAlgebra : } /** - * Builds tensor from rows of input tensor + * Builds tensor from rows of the input tensor. * * @param indices the [IntArray] of 1-dimensional indices - * @return tensor with rows corresponding to rows by [indices] + * @return tensor with rows corresponding to row by [indices] */ public fun Tensor.rowsByIndices(indices: IntArray): DoubleTensor = stack(indices.map { this[it] }) @@ -615,12 +615,12 @@ public open class DoubleTensorAlgebra : } /** - * Returns the covariance matrix M of given vectors. + * Returns the covariance matrix `M` of given vectors. * - * M[i, j] contains covariance of i-th and j-th given vectors + * `M[i, j]` contains covariance of `i`-th and `j`-th given vectors * * @param tensors the [List] of 1-dimensional tensors with same shape - * @return the covariance matrix + * @return `M`. */ public fun cov(tensors: List>): DoubleTensor { check(tensors.isNotEmpty()) { "List must have at least 1 element" } @@ -703,14 +703,14 @@ public open class DoubleTensorAlgebra : /** * Unpacks the data and pivots from a LU factorization of a tensor. - * Given a tensor [luTensor], return tensors (P, L, U) satisfying ``P * luTensor = L * U``, + * Given a tensor [luTensor], return tensors `Triple(P, L, U)` satisfying `P dot luTensor = L dot U`, * with `P` being a permutation matrix or batch of matrices, * `L` being a lower triangular matrix or batch of matrices, * `U` being an upper triangular matrix or batch of matrices. * * @param luTensor the packed LU factorization data * @param pivotsTensor the packed LU factorization pivots - * @return triple of P, L and U tensors + * @return triple of `P`, `L` and `U` tensors */ public fun luPivot( luTensor: Tensor, @@ -746,14 +746,15 @@ public open class DoubleTensorAlgebra : /** * QR decomposition. * - * Computes the QR decomposition of a matrix or a batch of matrices, and returns a pair `(Q, R)` of tensors. - * Given a tensor `input`, return tensors (Q, R) satisfying ``input = Q * R``, + * Computes the QR decomposition of a matrix or a batch of matrices, and returns a pair `Q to R` of tensors. + * Given a tensor `input`, return tensors `Q to R` satisfying `input == Q dot R`, * with `Q` being an orthogonal matrix or batch of orthogonal matrices * and `R` being an upper triangular matrix or batch of upper triangular matrices. * - * @param epsilon permissible error when comparing tensors for equality. + * @receiver the `input`. + * @param epsilon the permissible error when comparing tensors for equality. * Used when checking the positive definiteness of the input matrix or matrices. - * @return pair of Q and R tensors. + * @return a pair of `Q` and `R` tensors. */ public fun Tensor.cholesky(epsilon: Double): DoubleTensor { checkSquareMatrix(shape) @@ -793,13 +794,14 @@ public open class DoubleTensorAlgebra : * Singular Value Decomposition. * * Computes the singular value decomposition of either a matrix or batch of matrices `input`. - * The singular value decomposition is represented as a triple `(U, S, V)`, - * such that ``input = U.dot(diagonalEmbedding(S).dot(V.T))``. - * If input is a batch of tensors, then U, S, and Vh are also batched with the same batch dimensions as input. + * The singular value decomposition is represented as a triple `Triple(U, S, V)`, + * such that `input == U dot diagonalEmbedding(S) dot V.transpose()`. + * If `input` is a batch of tensors, then U, S, and Vh are also batched with the same batch dimensions as `input. * - * @param epsilon permissible error when calculating the dot product of vectors, - * i.e. the precision with which the cosine approaches 1 in an iterative algorithm. - * @return triple `(U, S, V)`. + * @receiver the `input`. + * @param epsilon permissible error when calculating the dot product of vectors + * i.e., the precision with which the cosine approaches 1 in an iterative algorithm. + * @return a triple `Triple(U, S, V)`. */ public fun Tensor.svd(epsilon: Double): Triple { val size = tensor.dimension @@ -834,11 +836,11 @@ public open class DoubleTensorAlgebra : /** * Returns eigenvalues and eigenvectors of a real symmetric matrix input or a batch of real symmetric matrices, - * represented by a pair (eigenvalues, eigenvectors). + * represented by a pair `eigenvalues to eigenvectors`. * - * @param epsilon permissible error when comparing tensors for equality + * @param epsilon the permissible error when comparing tensors for equality * and when the cosine approaches 1 in the SVD algorithm. - * @return a pair (eigenvalues, eigenvectors) + * @return a pair `eigenvalues to eigenvectors`. */ public fun Tensor.symEig(epsilon: Double): Pair { checkSymmetric(tensor, epsilon) @@ -857,11 +859,11 @@ public open class DoubleTensorAlgebra : * Computes the determinant of a square matrix input, or of each square matrix in a batched input * using LU factorization algorithm. * - * @param epsilon error in the LU algorithm - permissible error when comparing the determinant of a matrix with zero + * @param epsilon the error in the LU algorithm—permissible error when comparing the determinant of a matrix + * with zero. * @return the determinant. */ public fun Tensor.detLU(epsilon: Double = 1e-9): DoubleTensor { - checkSquareMatrix(tensor.shape) val luTensor = tensor.copy() val pivotsTensor = tensor.setUpPivots() @@ -889,9 +891,9 @@ public open class DoubleTensorAlgebra : * Computes the multiplicative inverse matrix of a square matrix input, or of each square matrix in a batched input * using LU factorization algorithm. * Given a square matrix `a`, return the matrix `aInv` satisfying - * ``a.dot(aInv) = aInv.dot(a) = eye(a.shape[0])``. + * `a dot aInv == aInv dot a == eye(a.shape[0])`. * - * @param epsilon error in the LU algorithm - permissible error when comparing the determinant of a matrix with zero + * @param epsilon error in the LU algorithm—permissible error when comparing the determinant of a matrix with zero * @return the multiplicative inverse of a matrix. */ public fun Tensor.invLU(epsilon: Double = 1e-9): DoubleTensor { @@ -908,16 +910,16 @@ public open class DoubleTensorAlgebra : } /** - * LUP decomposition + * LUP decomposition. * * Computes the LUP decomposition of a matrix or a batch of matrices. - * Given a tensor `input`, return tensors (P, L, U) satisfying ``P * input = L * U``, + * Given a tensor `input`, return tensors `Triple(P, L, U)` satisfying `P dot input == L dot U`, * with `P` being a permutation matrix or batch of matrices, * `L` being a lower triangular matrix or batch of matrices, * `U` being an upper triangular matrix or batch of matrices. * - * @param epsilon permissible error when comparing the determinant of a matrix with zero - * @return triple of P, L and U tensors + * @param epsilon permissible error when comparing the determinant of a matrix with zero. + * @return triple of `P`, `L` and `U` tensors. */ public fun Tensor.lu(epsilon: Double = 1e-9): Triple { val (lu, pivots) = tensor.luFactor(epsilon) 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 e337eeef9..ff89568ea 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 @@ -14,7 +14,7 @@ import space.kscience.kmath.tensors.core.DoubleTensor import kotlin.math.* /** - * Returns a reference to [IntArray] containing all of the elements of this [Buffer] or copy the data. + * Returns a reference to [IntArray] containing all the elements of this [Buffer] or copy the data. */ internal fun Buffer.array(): IntArray = when (this) { is IntBuffer -> array @@ -22,7 +22,7 @@ internal fun Buffer.array(): IntArray = when (this) { } /** - * Returns a reference to [DoubleArray] containing all of the elements of this [Buffer] or copy the data. + * Returns a reference to [DoubleArray] containing all the elements of this [Buffer] or copy the data. */ @PublishedApi internal fun Buffer.array(): DoubleArray = when (this) {