From b2746e5c0ef6c31d89640f6cfe7f41e3c2c74930 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 9 Apr 2023 10:55:58 +0300 Subject: [PATCH] Wasm support --- CHANGELOG.md | 1 + build.gradle.kts | 14 +-- .../space/kscience/kmath/ast/TypedMst.kt | 5 - .../kscience/kmath/ast/evaluateConstants.kt | 2 - .../ast/rendering/LatexSyntaxRenderer.kt | 3 - .../ast/rendering/MathMLSyntaxRenderer.kt | 3 - .../kmath/ast/rendering/MathRenderer.kt | 4 - .../kmath/ast/rendering/MathSyntax.kt | 25 ----- .../kmath/ast/rendering/SyntaxRenderer.kt | 4 - .../kscience/kmath/ast/rendering/features.kt | 22 ---- .../kscience/kmath/ast/rendering/phases.kt | 5 - kmath-complex/build.gradle.kts | 22 ++++ kmath-core/build.gradle.kts | 22 +++- .../space/kscience/kmath/misc/PermSortTest.kt | 2 + .../kmath/structures/NumberNDFieldTest.kt | 1 + kmath-coroutines/build.gradle.kts | 1 + kmath-functions/build.gradle.kts | 30 +++-- kmath-geometry/build.gradle.kts | 5 + .../kscience/kmath/jupyter/KMathJupyter.kt | 2 + kmath-memory/build.gradle.kts | 18 ++- .../space/kscience/kmath/memory/MemoryTest.kt | 37 +++++++ .../kscience/kmath/memory/ByteBufferMemory.kt | 5 +- .../kmath/memory/WasmDataViewMemory.kt | 103 ++++++++++++++++++ .../space/kscience/kmath/memory/WasmMemory.kt | 66 ----------- .../kmath/optimization/OptimizationBuilder.kt | 3 + kmath-stat/build.gradle.kts | 2 +- test-utils/build.gradle.kts | 2 +- 27 files changed, 242 insertions(+), 167 deletions(-) create mode 100644 kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt create mode 100644 kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt delete mode 100644 kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmMemory.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4e6123b..08b72f0d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] ### Added +- Wasm support for `memory`, `core`, `complex` and `functions` modules. - Generic builders for `BufferND` and `MutableBufferND` - `NamedMatrix` - matrix with symbol-based indexing - `Expression` with default arguments diff --git a/build.gradle.kts b/build.gradle.kts index 2eea9c47b..ec67eaa54 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,7 @@ allprojects { } group = "space.kscience" - version = "0.4.0-dev-1" + version = "0.3.1-dev-RC" } subprojects { @@ -55,18 +55,6 @@ subprojects { } } } - - plugins.withId("org.jetbrains.kotlin.multiplatform") { - configure { - sourceSets { - val commonTest by getting { - dependencies { - implementation(projects.testUtils) - } - } - } - } - } } readme.readmeTemplate = file("docs/templates/README-TEMPLATE.md") diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt index e211259af..e82f7a3ab 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/TypedMst.kt @@ -5,7 +5,6 @@ package space.kscience.kmath.ast -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.operations.Algebra @@ -16,7 +15,6 @@ import space.kscience.kmath.operations.NumericAlgebra * * @param T the type. */ -@UnstableKMathAPI public sealed interface TypedMst { /** * A node containing a unary operation. @@ -133,7 +131,6 @@ public sealed interface TypedMst { /** * Interprets the [TypedMst] node with this [Algebra] and [arguments]. */ -@UnstableKMathAPI public fun TypedMst.interpret(algebra: Algebra, arguments: Map): T = when (this) { is TypedMst.Unary -> algebra.unaryOperation(operation, interpret(algebra, arguments)) @@ -158,7 +155,6 @@ public fun TypedMst.interpret(algebra: Algebra, arguments: Map TypedMst.interpret(algebra: Algebra, vararg arguments: Pair): T = interpret( algebra, when (arguments.size) { @@ -171,7 +167,6 @@ public fun TypedMst.interpret(algebra: Algebra, vararg arguments: Pair /** * Interpret this [TypedMst] node as expression. */ -@UnstableKMathAPI public fun TypedMst.toExpression(algebra: Algebra): Expression = Expression { arguments -> interpret(algebra, arguments) } diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt index fb0c9b872..8fc5a6aaf 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/evaluateConstants.kt @@ -5,7 +5,6 @@ package space.kscience.kmath.ast -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.MST import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.operations.Algebra @@ -15,7 +14,6 @@ import space.kscience.kmath.operations.bindSymbolOrNull /** * Evaluates constants in given [MST] for given [algebra] at the same time with converting to [TypedMst]. */ -@UnstableKMathAPI public fun MST.evaluateConstants(algebra: Algebra): TypedMst = when (this) { is MST.Numeric -> TypedMst.Constant( (algebra as? NumericAlgebra)?.number(value) ?: error("Numeric nodes are not supported by $algebra"), diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt index 50162a4f5..5a338afed 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/LatexSyntaxRenderer.kt @@ -5,8 +5,6 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI - /** * [SyntaxRenderer] implementation for LaTeX. * @@ -25,7 +23,6 @@ import space.kscience.kmath.UnstableKMathAPI * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public object LatexSyntaxRenderer : SyntaxRenderer { override fun render(node: MathSyntax, output: Appendable): Unit = output.run { fun render(syntax: MathSyntax) = render(syntax, output) diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt index bb49c5df4..bfd9aeef9 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathMLSyntaxRenderer.kt @@ -5,8 +5,6 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI - /** * [SyntaxRenderer] implementation for MathML. * @@ -14,7 +12,6 @@ import space.kscience.kmath.UnstableKMathAPI * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public object MathMLSyntaxRenderer : SyntaxRenderer { override fun render(node: MathSyntax, output: Appendable) { output.append("") diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt index afa25febe..f88a5b319 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/MathRenderer.kt @@ -5,7 +5,6 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.MST /** @@ -13,7 +12,6 @@ import space.kscience.kmath.expressions.MST * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public fun interface MathRenderer { /** * Renders [MST] to [MathSyntax]. @@ -27,7 +25,6 @@ public fun interface MathRenderer { * @property features The applied features. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public open class FeaturedMathRenderer(public val features: List) : MathRenderer { override fun render(mst: MST): MathSyntax { for (feature in features) feature.render(this, mst)?.let { return it } @@ -51,7 +48,6 @@ public open class FeaturedMathRenderer(public val features: List) * @property stages The applied stages. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public open class FeaturedMathRendererWithPostProcess( features: List, public val stages: List, 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 887469164..0196be3b6 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 @@ -5,14 +5,11 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI - /** * Syntax node for mathematical typography. * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public sealed class MathSyntax { /** * The parent node of this syntax node. @@ -25,7 +22,6 @@ public sealed class MathSyntax { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public sealed class TerminalSyntax : MathSyntax() /** @@ -33,7 +29,6 @@ public sealed class TerminalSyntax : MathSyntax() * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public sealed class OperationSyntax : MathSyntax() { /** * The operation token. @@ -46,7 +41,6 @@ public sealed class OperationSyntax : MathSyntax() { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public sealed class UnarySyntax : OperationSyntax() { /** * The operand of this node. @@ -59,7 +53,6 @@ public sealed class UnarySyntax : OperationSyntax() { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public sealed class BinarySyntax : OperationSyntax() { /** * The left-hand side operand. @@ -78,7 +71,6 @@ public sealed class BinarySyntax : OperationSyntax() { * @property string The digits of number. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class NumberSyntax(public var string: String) : TerminalSyntax() /** @@ -87,7 +79,6 @@ public data class NumberSyntax(public var string: String) : TerminalSyntax() * @property string The symbol. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class SymbolSyntax(public var string: String) : TerminalSyntax() /** @@ -98,7 +89,6 @@ public data class SymbolSyntax(public var string: String) : TerminalSyntax() * @see UnaryOperatorSyntax * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class OperatorNameSyntax(public var name: String) : TerminalSyntax() /** @@ -107,7 +97,6 @@ public data class OperatorNameSyntax(public var name: String) : TerminalSyntax() * @property kind The kind of symbol. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class SpecialSymbolSyntax(public var kind: Kind) : TerminalSyntax() { /** * The kind of symbol. @@ -132,7 +121,6 @@ public data class SpecialSymbolSyntax(public var kind: Kind) : TerminalSyntax() * @property parentheses Whether the operand should be wrapped with parentheses. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class OperandSyntax( public val operand: MathSyntax, public var parentheses: Boolean, @@ -148,7 +136,6 @@ public data class OperandSyntax( * @property prefix The prefix. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class UnaryOperatorSyntax( override val operation: String, public var prefix: MathSyntax, @@ -164,7 +151,6 @@ public data class UnaryOperatorSyntax( * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class UnaryPlusSyntax( override val operation: String, override val operand: OperandSyntax, @@ -179,7 +165,6 @@ public data class UnaryPlusSyntax( * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class UnaryMinusSyntax( override val operation: String, override val operand: OperandSyntax, @@ -195,7 +180,6 @@ public data class UnaryMinusSyntax( * @property operand The radicand. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class RadicalSyntax( override val operation: String, override val operand: MathSyntax, @@ -213,7 +197,6 @@ public data class RadicalSyntax( * (*ex*). * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class ExponentSyntax( override val operation: String, override val operand: OperandSyntax, @@ -231,7 +214,6 @@ public data class ExponentSyntax( * @property right The superscript. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class SuperscriptSyntax( override val operation: String, override val left: MathSyntax, @@ -250,7 +232,6 @@ public data class SuperscriptSyntax( * @property right The subscript. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class SubscriptSyntax( override val operation: String, override val left: MathSyntax, @@ -268,7 +249,6 @@ public data class SubscriptSyntax( * @property prefix The prefix. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class BinaryOperatorSyntax( override val operation: String, public var prefix: MathSyntax, @@ -288,7 +268,6 @@ public data class BinaryOperatorSyntax( * @param right The addend. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class BinaryPlusSyntax( override val operation: String, override val left: OperandSyntax, @@ -307,7 +286,6 @@ public data class BinaryPlusSyntax( * @param right The subtrahend. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class BinaryMinusSyntax( override val operation: String, override val left: OperandSyntax, @@ -327,7 +305,6 @@ public data class BinaryMinusSyntax( * @property infix Whether infix (*1 / 2*) or normal (*½*) fraction should be made. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class FractionSyntax( override val operation: String, override val left: OperandSyntax, @@ -347,7 +324,6 @@ public data class FractionSyntax( * @property right The radicand. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class RadicalWithIndexSyntax( override val operation: String, override val left: MathSyntax, @@ -367,7 +343,6 @@ public data class RadicalWithIndexSyntax( * @property times Whether the times (×) symbol should be used. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public data class MultiplicationSyntax( override val operation: String, override val left: OperandSyntax, 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 7669be664..16957bdd2 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 @@ -5,15 +5,12 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI - /** * Abstraction of writing [MathSyntax] as a string of an actual markup language. Typical implementation should * involve traversal of MathSyntax with handling each subtype. * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public fun interface SyntaxRenderer { /** * Renders the [MathSyntax] to [output]. @@ -26,7 +23,6 @@ public fun interface SyntaxRenderer { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public fun SyntaxRenderer.renderWithStringBuilder(node: MathSyntax): String { val sb = StringBuilder() render(node, sb) 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 8bb7e3585..4deffc83c 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 @@ -5,7 +5,6 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.ast.rendering.FeaturedMathRenderer.RenderFeature import space.kscience.kmath.expressions.MST import space.kscience.kmath.expressions.Symbol @@ -17,7 +16,6 @@ import kotlin.reflect.KClass * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public val PrintSymbol: RenderFeature = RenderFeature { _, node -> if (node !is Symbol) null else SymbolSyntax(string = node.identity) @@ -28,7 +26,6 @@ public val PrintSymbol: RenderFeature = RenderFeature { _, node -> * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public val PrintNumeric: RenderFeature = RenderFeature { _, node -> if (node !is MST.Numeric) null @@ -36,7 +33,6 @@ public val PrintNumeric: RenderFeature = RenderFeature { _, node -> NumberSyntax(string = node.value.toString()) } -@UnstableKMathAPI private fun printSignedNumberString(s: String): MathSyntax = if (s.startsWith('-')) UnaryMinusSyntax( operation = GroupOps.MINUS_OPERATION, @@ -55,7 +51,6 @@ else * @property types The suitable types. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class PrettyPrintFloats(public val types: Set>) : RenderFeature { override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? { if (node !is MST.Numeric || node.value::class !in types) return null @@ -115,7 +110,6 @@ public class PrettyPrintFloats(public val types: Set>) : Rend * @property types The suitable types. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class PrettyPrintIntegers(public val types: Set>) : RenderFeature { override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? = if (node !is MST.Numeric || node.value::class !in types) @@ -138,7 +132,6 @@ public class PrettyPrintIntegers(public val types: Set>) : Re * @property symbols The allowed symbols. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class PrettyPrintPi(public val symbols: Set) : RenderFeature { override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? = if (node !is Symbol || node.identity !in symbols) @@ -161,7 +154,6 @@ public class PrettyPrintPi(public val symbols: Set) : RenderFeature { * @param operations the allowed operations. If `null`, any operation is accepted. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public abstract class Unary(public val operations: Collection?) : RenderFeature { /** * The actual render function specialized for [MST.Unary]. @@ -182,7 +174,6 @@ public abstract class Unary(public val operations: Collection?) : Render * @property operations the allowed operations. If `null`, any operation is accepted. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public abstract class Binary(public val operations: Collection?) : RenderFeature { /** * The actual render function specialized for [MST.Binary]. @@ -200,7 +191,6 @@ public abstract class Binary(public val operations: Collection?) : Rende * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class BinaryPlus(operations: Collection?) : Binary(operations) { override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = BinaryPlusSyntax( @@ -222,7 +212,6 @@ public class BinaryPlus(operations: Collection?) : Binary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class BinaryMinus(operations: Collection?) : Binary(operations) { override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = BinaryMinusSyntax( @@ -244,7 +233,6 @@ public class BinaryMinus(operations: Collection?) : Binary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class UnaryPlus(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax( operation = node.operation, @@ -264,7 +252,6 @@ public class UnaryPlus(operations: Collection?) : Unary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class UnaryMinus(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax( operation = node.operation, @@ -284,7 +271,6 @@ public class UnaryMinus(operations: Collection?) : Unary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class Fraction(operations: Collection?) : Binary(operations) { override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax( operation = node.operation, @@ -306,7 +292,6 @@ public class Fraction(operations: Collection?) : Binary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class BinaryOperator(operations: Collection?) : Binary(operations) { override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = BinaryOperatorSyntax( @@ -329,7 +314,6 @@ public class BinaryOperator(operations: Collection?) : Binary(operations * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class UnaryOperator(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryOperatorSyntax( @@ -351,7 +335,6 @@ public class UnaryOperator(operations: Collection?) : Unary(operations) * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class Power(operations: Collection?) : Binary(operations) { override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = SuperscriptSyntax( @@ -371,7 +354,6 @@ public class Power(operations: Collection?) : Binary(operations) { /** * Handles binary nodes by producing [RadicalSyntax] with no index. */ -@UnstableKMathAPI public class SquareRoot(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = RadicalSyntax(operation = node.operation, operand = parent.render(node.value)) @@ -389,7 +371,6 @@ public class SquareRoot(operations: Collection?) : Unary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class Exponent(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax( operation = node.operation, @@ -410,7 +391,6 @@ public class Exponent(operations: Collection?) : Unary(operations) { * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class Multiplication(operations: Collection?) : Binary(operations) { override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = MultiplicationSyntax( @@ -433,7 +413,6 @@ public class Multiplication(operations: Collection?) : Binary(operations * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class InverseTrigonometricOperations(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryOperatorSyntax( @@ -460,7 +439,6 @@ public class InverseTrigonometricOperations(operations: Collection?) : U * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class InverseHyperbolicOperations(operations: Collection?) : Unary(operations) { override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryOperatorSyntax( diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt index 2399e8f68..0d26621d3 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt @@ -5,7 +5,6 @@ package space.kscience.kmath.ast.rendering -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess.PostProcessPhase import space.kscience.kmath.operations.FieldOps import space.kscience.kmath.operations.GroupOps @@ -17,7 +16,6 @@ import space.kscience.kmath.operations.RingOps * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public val BetterMultiplication: PostProcessPhase = PostProcessPhase { node -> fun perform(node: MathSyntax): Unit = when (node) { is NumberSyntax -> Unit @@ -91,7 +89,6 @@ public val BetterMultiplication: PostProcessPhase = PostProcessPhase { node -> * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public val BetterFraction: PostProcessPhase = PostProcessPhase { node -> fun perform(node: MathSyntax, infix: Boolean = false): Unit = when (node) { is NumberSyntax -> Unit @@ -162,7 +159,6 @@ public val BetterFraction: PostProcessPhase = PostProcessPhase { node -> * * @author Iaroslav Postovalov */ -@UnstableKMathAPI public val BetterExponent: PostProcessPhase = PostProcessPhase { node -> fun perform(node: MathSyntax): Boolean { return when (node) { @@ -202,7 +198,6 @@ public val BetterExponent: PostProcessPhase = PostProcessPhase { node -> * @property precedenceFunction Returns the precedence number for syntax node. Higher number is lower priority. * @author Iaroslav Postovalov */ -@UnstableKMathAPI public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) -> Int) : PostProcessPhase { override fun perform(node: MathSyntax): Unit = when (node) { diff --git a/kmath-complex/build.gradle.kts b/kmath-complex/build.gradle.kts index 76d5a4c9a..0611e9aae 100644 --- a/kmath-complex/build.gradle.kts +++ b/kmath-complex/build.gradle.kts @@ -7,9 +7,31 @@ kscience { js() native() + wasm{ + browser { + testTask { + useKarma { + this.webpackConfig.experiments.add("topLevelAwait") + useChromeHeadless() + useConfigDirectory(project.projectDir.resolve("karma.config.d").resolve("wasm")) + } + } + } + } + + wasmTest{ + dependencies { + implementation(kotlin("test")) + } + } + dependencies { api(projects.kmathCore) } + + testDependencies { + implementation(projects.testUtils) + } } readme { diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index b6a955b12..08411be59 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -6,11 +6,31 @@ kscience{ jvm() js() native() -// wasm() + wasm{ + browser { + testTask { + useKarma { + this.webpackConfig.experiments.add("topLevelAwait") + useChromeHeadless() + useConfigDirectory(project.projectDir.resolve("karma.config.d").resolve("wasm")) + } + } + } + } + + wasmTest{ + dependencies { + implementation(kotlin("test")) + } + } dependencies { api(projects.kmathMemory) } + + testDependencies { + implementation(projects.testUtils) + } } kotlin.sourceSets { diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt index 4f3469ea2..dd97df1e8 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt @@ -5,6 +5,7 @@ package space.kscience.kmath.misc +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.misc.PermSortTest.Platform.* import space.kscience.kmath.structures.IntBuffer import space.kscience.kmath.structures.asBuffer @@ -14,6 +15,7 @@ import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertTrue +@OptIn(UnstableKMathAPI::class) class PermSortTest { private enum class Platform { diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt index 1572db816..993fb089f 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt @@ -22,6 +22,7 @@ import kotlin.math.pow import kotlin.test.Test import kotlin.test.assertEquals +@OptIn(PerformancePitfall::class) @Suppress("UNUSED_VARIABLE") class NumberNDFieldTest { val algebra = DoubleField.ndAlgebra diff --git a/kmath-coroutines/build.gradle.kts b/kmath-coroutines/build.gradle.kts index 9d5cfabe4..1e901ca98 100644 --- a/kmath-coroutines/build.gradle.kts +++ b/kmath-coroutines/build.gradle.kts @@ -6,6 +6,7 @@ kscience { jvm() js() native() + dependencies { api(project(":kmath-core")) api(project(":kmath-complex")) diff --git a/kmath-functions/build.gradle.kts b/kmath-functions/build.gradle.kts index 08e76aef0..acabd1eb9 100644 --- a/kmath-functions/build.gradle.kts +++ b/kmath-functions/build.gradle.kts @@ -6,18 +6,32 @@ kscience{ jvm() js() native() + + wasm{ + browser { + testTask { + useKarma { + this.webpackConfig.experiments.add("topLevelAwait") + useChromeHeadless() + useConfigDirectory(project.projectDir.resolve("karma.config.d").resolve("wasm")) + } + } + } + } + + wasmTest{ + dependencies { + implementation(kotlin("test")) + } + } + + dependencies { + api(projects.kmathCore) + } } description = "Functions, integration and interpolation" -kotlin.sourceSets { - commonMain { - dependencies { - api(project(":kmath-core")) - } - } -} - dependencies { dokkaPlugin("org.jetbrains.dokka:mathjax-plugin:${spclibs.versions.dokka.get()}") } diff --git a/kmath-geometry/build.gradle.kts b/kmath-geometry/build.gradle.kts index f248a1717..32926db7e 100644 --- a/kmath-geometry/build.gradle.kts +++ b/kmath-geometry/build.gradle.kts @@ -12,6 +12,11 @@ kscience{ dependencies{ api(projects.kmath.kmathComplex) } + + testDependencies { + implementation(projects.testUtils) + } + } readme { diff --git a/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt index 2d32dd609..944666c9e 100644 --- a/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt +++ b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt @@ -13,6 +13,7 @@ import org.jetbrains.kotlinx.jupyter.api.DisplayResult import org.jetbrains.kotlinx.jupyter.api.HTML import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration import space.kscience.kmath.PerformancePitfall +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess import space.kscience.kmath.ast.rendering.MathMLSyntaxRenderer import space.kscience.kmath.ast.rendering.renderWithStringBuilder @@ -30,6 +31,7 @@ import space.kscience.kmath.structures.Buffer */ public fun Number.toMst(): MST.Numeric = MST.Numeric(this) +@OptIn(UnstableKMathAPI::class) internal class KMathJupyter : JupyterIntegration() { private val mathRender = FeaturedMathRendererWithPostProcess.Default private val syntaxRender = MathMLSyntaxRenderer diff --git a/kmath-memory/build.gradle.kts b/kmath-memory/build.gradle.kts index d8af1e3a2..4e5370c0f 100644 --- a/kmath-memory/build.gradle.kts +++ b/kmath-memory/build.gradle.kts @@ -6,7 +6,23 @@ kscience { jvm() js() native() -// wasm() + wasm{ + browser { + testTask { + useKarma { + this.webpackConfig.experiments.add("topLevelAwait") + useChromeHeadless() + useConfigDirectory(project.projectDir.resolve("karma.config.d").resolve("wasm")) + } + } + } + } + + wasmTest{ + dependencies { + implementation(kotlin("test")) + } + } } readme { diff --git a/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt b/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt new file mode 100644 index 000000000..3726ddbb7 --- /dev/null +++ b/kmath-memory/src/commonTest/kotlin/space/kscience/kmath/memory/MemoryTest.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2018-2023 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.memory + +import kotlin.test.Test +import kotlin.test.assertEquals + +class MemoryTest { + @Test + fun memoryWriteRead() { + val memorySize = 60 + val data = buildList { + for (i in 0 until (memorySize / 4)) { + add(i) + } + } + val memory = Memory.allocate(memorySize) + memory.write { + for (i in 0 until (memory.size / 4)) { + writeInt(i*4, data[i]) + } + } + + val result = memory.read { + buildList { + for (i in 0 until (memory.size / 4)) { + add(readInt(i*4)) + } + } + } + + assertEquals(data,result) + } +} \ No newline at end of file diff --git a/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt b/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt index 19910a396..d022cab23 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 @@ -20,8 +20,7 @@ internal class ByteBufferMemory( val startOffset: Int = 0, override val size: Int = buffer.limit(), ) : Memory { - @Suppress("NOTHING_TO_INLINE") - private inline fun position(o: Int): Int = startOffset + o + private fun position(offset: Int): Int = startOffset + offset override fun view(offset: Int, length: Int): Memory { require(offset >= 0) { "offset shouldn't be negative: $offset" } @@ -120,7 +119,7 @@ public fun ByteBuffer.asMemory(startOffset: Int = 0, size: Int = limit()): Memor ByteBufferMemory(this, startOffset, size) /** - * Uses direct memory-mapped buffer from file to read something and close it afterwards. + * Uses direct memory-mapped buffer from file to read something and close it afterward. */ @Throws(IOException::class) public inline fun Path.readAsMemory(position: Long = 0, size: Long = Files.size(this), block: Memory.() -> R): R { diff --git a/kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt b/kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt new file mode 100644 index 000000000..0cff551fa --- /dev/null +++ b/kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmDataViewMemory.kt @@ -0,0 +1,103 @@ +/* + * Copyright 2018-2022 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.memory + +import org.khronos.webgl.ArrayBuffer +import org.khronos.webgl.DataView +import org.khronos.webgl.Int8Array + +private class WasmDataViewMemory(val view: DataView) : Memory { + override val size: Int get() = view.byteLength + + override fun view(offset: Int, length: Int): Memory { + require(offset >= 0) { "offset shouldn't be negative: $offset" } + require(length >= 0) { "length shouldn't be negative: $length" } + require(offset + length <= size) { "Can't view memory outside the parent region." } + + if (offset + length > size) + throw IndexOutOfBoundsException("offset + length > size: $offset + $length > $size") + + return WasmDataViewMemory(DataView(view.buffer, view.byteOffset + offset, length)) + } + + override fun copy(): Memory = WasmDataViewMemory(DataView(view.buffer.slice(0))) + + private val reader: MemoryReader = object : MemoryReader { + override val memory: Memory get() = this@WasmDataViewMemory + + override fun readDouble(offset: Int): Double = view.getFloat64(offset, false) + + override fun readFloat(offset: Int): Float = view.getFloat32(offset, false) + + override fun readByte(offset: Int): Byte = view.getInt8(offset) + + override fun readShort(offset: Int): Short = view.getInt16(offset, false) + + override fun readInt(offset: Int): Int = view.getInt32(offset, false) + + override fun readLong(offset: Int): Long = + view.getInt32(offset, false).toLong() shl 32 or view.getInt32(offset + 4, false).toLong() + + override fun close() { + // does nothing on JS + } + } + + override fun reader(): MemoryReader = reader + + private val writer: MemoryWriter = object : MemoryWriter { + override val memory: Memory get() = this@WasmDataViewMemory + + override fun writeDouble(offset: Int, value: Double) { + view.setFloat64(offset, value, false) + } + + override fun writeFloat(offset: Int, value: Float) { + view.setFloat32(offset, value, false) + } + + override fun writeByte(offset: Int, value: Byte) { + view.setInt8(offset, value) + } + + override fun writeShort(offset: Int, value: Short) { + view.setUint16(offset, value, false) + } + + override fun writeInt(offset: Int, value: Int) { + view.setInt32(offset, value, false) + } + + override fun writeLong(offset: Int, value: Long) { + view.setInt32(offset, (value shr 32).toInt(), littleEndian = false) + view.setInt32(offset + 4, (value and 0xffffffffL).toInt(), littleEndian = false) + } + + override fun close() { + // does nothing on JS + } + } + + override fun writer(): MemoryWriter = writer + +} + +/** + * Allocates memory based on a [DataView]. + */ +public actual fun Memory.Companion.allocate(length: Int): Memory { + val buffer = ArrayBuffer(length) + return WasmDataViewMemory(DataView(buffer, 0, length)) +} + +/** + * Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied + * 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 + return WasmDataViewMemory(DataView(int8Array.buffer, int8Array.byteOffset, int8Array.length)) +} diff --git a/kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmMemory.kt b/kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmMemory.kt deleted file mode 100644 index 7c2be19cc..000000000 --- a/kmath-memory/src/wasmMain/kotlin/space/kscience/kmath/memory/WasmMemory.kt +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.kmath.memory - -import kotlin.wasm.unsafe.Pointer -import kotlin.wasm.unsafe.UnsafeWasmMemoryApi - -@OptIn(UnsafeWasmMemoryApi::class) -public class WasmMemory private constructor( - public val pointer: Pointer, - override val size: Int, -) : Memory { - - override fun view(offset: Int, length: Int): Memory { - TODO("Not yet implemented") - } - - override fun copy(): Memory { - TODO("Not yet implemented") - } - - override fun reader(): MemoryReader = object : MemoryReader { - override val memory: Memory - get() = this@WasmMemory - - override fun readDouble(offset: Int): Double { - return Double.fromBits(pointer.plus(offset).loadLong()) - } - - override fun readFloat(offset: Int): Float { - return Float.fromBits(pointer.plus(offset).loadInt()) - } - - override fun readByte(offset: Int): Byte { - return pointer.plus(offset).loadByte() - } - - override fun readShort(offset: Int): Short { - return pointer.plus(offset).loadShort() - } - - override fun readInt(offset: Int): Int { - return pointer.plus(offset).loadInt() - } - - override fun readLong(offset: Int): Long { - return pointer.plus(offset).loadLong() - } - - override fun close() { - TODO() - } - - } - - override fun writer(): MemoryWriter = TODO() -} - -public actual fun Memory.Companion.allocate(length: Int): Memory { - TODO() -} - -public actual fun Memory.Companion.wrap(array: ByteArray): Memory = TODO() \ No newline at end of file diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt index d1ceccf1a..0459d46ee 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/OptimizationBuilder.kt @@ -5,6 +5,7 @@ package space.kscience.kmath.optimization +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.data.XYColumnarData import space.kscience.kmath.expressions.DifferentiableExpression import space.kscience.kmath.expressions.Symbol @@ -69,6 +70,7 @@ public suspend fun DifferentiableExpression.optimizeWith( } +@OptIn(UnstableKMathAPI::class) public class XYOptimizationBuilder( public val data: XYColumnarData, public val model: DifferentiableExpression, @@ -86,6 +88,7 @@ public class XYOptimizationBuilder( ) } +@OptIn(UnstableKMathAPI::class) public fun XYOptimization( data: XYColumnarData, model: DifferentiableExpression, diff --git a/kmath-stat/build.gradle.kts b/kmath-stat/build.gradle.kts index 1426e913a..000280def 100644 --- a/kmath-stat/build.gradle.kts +++ b/kmath-stat/build.gradle.kts @@ -11,7 +11,7 @@ kscience{ kotlin.sourceSets { commonMain { dependencies { - api(project(":kmath-coroutines")) + api(projects.kmathCoroutines) //implementation(spclibs.atomicfu) } } diff --git a/test-utils/build.gradle.kts b/test-utils/build.gradle.kts index 31c57aca7..b03059eaf 100644 --- a/test-utils/build.gradle.kts +++ b/test-utils/build.gradle.kts @@ -6,7 +6,7 @@ kscience{ jvm() js() native() -// wasm() + wasm() } kotlin.sourceSets {