0.3.1 #514

Merged
altavir merged 36 commits from dev into master 2023-05-12 22:19:49 +03:00
27 changed files with 242 additions and 167 deletions
Showing only changes of commit b2746e5c0e - Show all commits

View File

@ -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

View File

@ -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<org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension> {
sourceSets {
val commonTest by getting {
dependencies {
implementation(projects.testUtils)
}
}
}
}
}
}
readme.readmeTemplate = file("docs/templates/README-TEMPLATE.md")

View File

@ -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<T> {
/**
* A node containing a unary operation.
@ -133,7 +131,6 @@ public sealed interface TypedMst<T> {
/**
* Interprets the [TypedMst] node with this [Algebra] and [arguments].
*/
@UnstableKMathAPI
public fun <T> TypedMst<T>.interpret(algebra: Algebra<T>, arguments: Map<Symbol, T>): T = when (this) {
is TypedMst.Unary -> algebra.unaryOperation(operation, interpret(algebra, arguments))
@ -158,7 +155,6 @@ public fun <T> TypedMst<T>.interpret(algebra: Algebra<T>, arguments: Map<Symbol,
/**
* Interprets the [TypedMst] node with this [Algebra] and optional [arguments].
*/
@UnstableKMathAPI
public fun <T> TypedMst<T>.interpret(algebra: Algebra<T>, vararg arguments: Pair<Symbol, T>): T = interpret(
algebra,
when (arguments.size) {
@ -171,7 +167,6 @@ public fun <T> TypedMst<T>.interpret(algebra: Algebra<T>, vararg arguments: Pair
/**
* Interpret this [TypedMst] node as expression.
*/
@UnstableKMathAPI
public fun <T : Any> TypedMst<T>.toExpression(algebra: Algebra<T>): Expression<T> = Expression { arguments ->
interpret(algebra, arguments)
}

View File

@ -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 <T> MST.evaluateConstants(algebra: Algebra<T>): TypedMst<T> = when (this) {
is MST.Numeric -> TypedMst.Constant(
(algebra as? NumericAlgebra<T>)?.number(value) ?: error("Numeric nodes are not supported by $algebra"),

View File

@ -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)

View File

@ -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("<math xmlns=\"https://www.w3.org/1998/Math/MathML\"><mrow>")

View File

@ -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<RenderFeature>) : 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<RenderFeature>)
* @property stages The applied stages.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public open class FeaturedMathRendererWithPostProcess(
features: List<RenderFeature>,
public val stages: List<PostProcessPhase>,

View File

@ -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(
* (*e<sup>x</sup>*).
* @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 (*&frac12;*) 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 (&times;) symbol should be used.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public data class MultiplicationSyntax(
override val operation: String,
override val left: OperandSyntax,

View File

@ -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)

View File

@ -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<KClass<out Number>>) : 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<KClass<out Number>>) : Rend
* @property types The suitable types.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class PrettyPrintIntegers(public val types: Set<KClass<out Number>>) : RenderFeature {
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
if (node !is MST.Numeric || node.value::class !in types)
@ -138,7 +132,6 @@ public class PrettyPrintIntegers(public val types: Set<KClass<out Number>>) : Re
* @property symbols The allowed symbols.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class PrettyPrintPi(public val symbols: Set<String>) : RenderFeature {
override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
if (node !is Symbol || node.identity !in symbols)
@ -161,7 +154,6 @@ public class PrettyPrintPi(public val symbols: Set<String>) : RenderFeature {
* @param operations the allowed operations. If `null`, any operation is accepted.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public abstract class Unary(public val operations: Collection<String>?) : RenderFeature {
/**
* The actual render function specialized for [MST.Unary].
@ -182,7 +174,6 @@ public abstract class Unary(public val operations: Collection<String>?) : Render
* @property operations the allowed operations. If `null`, any operation is accepted.
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public abstract class Binary(public val operations: Collection<String>?) : RenderFeature {
/**
* The actual render function specialized for [MST.Binary].
@ -200,7 +191,6 @@ public abstract class Binary(public val operations: Collection<String>?) : Rende
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryPlusSyntax(
@ -222,7 +212,6 @@ public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryMinusSyntax(
@ -244,7 +233,6 @@ public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax(
operation = node.operation,
@ -264,7 +252,6 @@ public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax(
operation = node.operation,
@ -284,7 +271,6 @@ public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class Fraction(operations: Collection<String>?) : Binary(operations) {
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax(
operation = node.operation,
@ -306,7 +292,6 @@ public class Fraction(operations: Collection<String>?) : Binary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class BinaryOperator(operations: Collection<String>?) : Binary(operations) {
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryOperatorSyntax(
@ -329,7 +314,6 @@ public class BinaryOperator(operations: Collection<String>?) : Binary(operations
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class UnaryOperator(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax(
@ -351,7 +335,6 @@ public class UnaryOperator(operations: Collection<String>?) : Unary(operations)
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class Power(operations: Collection<String>?) : Binary(operations) {
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
SuperscriptSyntax(
@ -371,7 +354,6 @@ public class Power(operations: Collection<String>?) : Binary(operations) {
/**
* Handles binary nodes by producing [RadicalSyntax] with no index.
*/
@UnstableKMathAPI
public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
RadicalSyntax(operation = node.operation, operand = parent.render(node.value))
@ -389,7 +371,6 @@ public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class Exponent(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax(
operation = node.operation,
@ -410,7 +391,6 @@ public class Exponent(operations: Collection<String>?) : Unary(operations) {
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class Multiplication(operations: Collection<String>?) : Binary(operations) {
override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
MultiplicationSyntax(
@ -433,7 +413,6 @@ public class Multiplication(operations: Collection<String>?) : Binary(operations
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class InverseTrigonometricOperations(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax(
@ -460,7 +439,6 @@ public class InverseTrigonometricOperations(operations: Collection<String>?) : U
*
* @author Iaroslav Postovalov
*/
@UnstableKMathAPI
public class InverseHyperbolicOperations(operations: Collection<String>?) : Unary(operations) {
override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax(

View File

@ -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) {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -6,6 +6,7 @@ kscience {
jvm()
js()
native()
dependencies {
api(project(":kmath-core"))
api(project(":kmath-complex"))

View File

@ -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()}")
}

View File

@ -12,6 +12,11 @@ kscience{
dependencies{
api(projects.kmath.kmathComplex)
}
testDependencies {
implementation(projects.testUtils)
}
}
readme {

View File

@ -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

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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 <R> Path.readAsMemory(position: Long = 0, size: Long = Files.size(this), block: Memory.() -> R): R {

View File

@ -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))
}

View File

@ -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()

View File

@ -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 <T> DifferentiableExpression<T>.optimizeWith(
}
@OptIn(UnstableKMathAPI::class)
public class XYOptimizationBuilder(
public val data: XYColumnarData<Double, Double, Double>,
public val model: DifferentiableExpression<Double>,
@ -86,6 +88,7 @@ public class XYOptimizationBuilder(
)
}
@OptIn(UnstableKMathAPI::class)
public fun XYOptimization(
data: XYColumnarData<Double, Double, Double>,
model: DifferentiableExpression<Double>,

View File

@ -11,7 +11,7 @@ kscience{
kotlin.sourceSets {
commonMain {
dependencies {
api(project(":kmath-coroutines"))
api(projects.kmathCoroutines)
//implementation(spclibs.atomicfu)
}
}

View File

@ -6,7 +6,7 @@ kscience{
jvm()
js()
native()
// wasm()
wasm()
}
kotlin.sourceSets {