From 6aa5b547b57092f12a6a2defbfd94b65c5d4f3e9 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 21 Apr 2021 12:48:11 +0300 Subject: [PATCH] cleanup --- CHANGELOG.md | 1 + kmath-core/api/kmath-core.api | 12 ++--- .../kmath/expressions/SymbolIndexer.kt | 4 +- .../kmath/expressions/expressionBuilders.kt | 45 ------------------- .../space/kscience/kmath/misc/Symbol.kt | 25 ++++++++++- .../kscience/kmath/operations/Algebra.kt | 4 +- .../kmath/operations/NumericAlgebra.kt | 6 +-- .../kscience/kmath/operations/numbers.kt | 6 --- 8 files changed, 35 insertions(+), 68 deletions(-) delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/expressionBuilders.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 7464695ec..c41eda374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ - Number multiplication and division in main Algebra chain - `contentEquals` from Buffer. It moved to the companion. - MSTExpression +- Expression algebra builders ### Fixed - Ring inherits RingOperations, not GroupOperations diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 6b300123c..efb626c12 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -45,13 +45,6 @@ public abstract interface class space/kscience/kmath/expressions/ExpressionAlgeb public abstract fun const (Ljava/lang/Object;)Ljava/lang/Object; } -public final class space/kscience/kmath/expressions/ExpressionBuildersKt { - public static final fun extendedFieldExpression (Lspace/kscience/kmath/operations/ExtendedField;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; - public static final fun fieldExpression (Lspace/kscience/kmath/operations/Field;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; - public static final fun ringExpression (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; - public static final fun spaceExpression (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/expressions/Expression; -} - public final class space/kscience/kmath/expressions/ExpressionKt { public static final fun binding (Lspace/kscience/kmath/expressions/ExpressionAlgebra;)Lkotlin/properties/ReadOnlyProperty; public static final fun callByString (Lspace/kscience/kmath/expressions/Expression;[Lkotlin/Pair;)Ljava/lang/Object; @@ -705,7 +698,11 @@ public final class space/kscience/kmath/misc/Symbol$Companion { } public final class space/kscience/kmath/misc/SymbolKt { + public static final fun get (Ljava/util/Map;Ljava/lang/String;)Ljava/lang/Object; + public static final fun get (Ljava/util/Map;Lspace/kscience/kmath/misc/Symbol;)Ljava/lang/Object; public static final fun getSymbol ()Lkotlin/properties/ReadOnlyProperty; + public static final fun set (Ljava/util/Map;Ljava/lang/String;Ljava/lang/Object;)V + public static final fun set (Ljava/util/Map;Lspace/kscience/kmath/misc/Symbol;Ljava/lang/Object;)V } public abstract interface annotation class space/kscience/kmath/misc/UnstableKMathAPI : java/lang/annotation/Annotation { @@ -1209,7 +1206,6 @@ public abstract interface class space/kscience/kmath/operations/ExtendedField : public fun acosh (Ljava/lang/Object;)Ljava/lang/Object; public fun asinh (Ljava/lang/Object;)Ljava/lang/Object; public fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object; public fun cosh (Ljava/lang/Object;)Ljava/lang/Object; public fun rightSideNumberOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public fun sinh (Ljava/lang/Object;)Ljava/lang/Object; diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt index 886008983..738156975 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt @@ -33,12 +33,12 @@ public interface SymbolIndexer { public operator fun DoubleArray.get(symbol: Symbol): Double { require(size == symbols.size) { "The input array size for indexer should be ${symbols.size} but $size found" } - return get(this@SymbolIndexer.indexOf(symbol)) + return get(indexOf(symbol)) } public operator fun Point.get(symbol: Symbol): T { require(size == symbols.size) { "The input buffer size for indexer should be ${symbols.size} but $size found" } - return get(this@SymbolIndexer.indexOf(symbol)) + return get(indexOf(symbol)) } public fun DoubleArray.toMap(): Map { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/expressionBuilders.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/expressionBuilders.kt deleted file mode 100644 index 142194070..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/expressionBuilders.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2018-2021 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.kmath.expressions - -import space.kscience.kmath.operations.ExtendedField -import space.kscience.kmath.operations.Field -import space.kscience.kmath.operations.Ring -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract - - -/** - * Creates a functional expression with this [Ring]. - */ -public inline fun Ring.spaceExpression(block: FunctionalExpressionGroup>.() -> Expression): Expression { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return FunctionalExpressionGroup(this).block() -} - -/** - * Creates a functional expression with this [Ring]. - */ -public inline fun Ring.ringExpression(block: FunctionalExpressionRing>.() -> Expression): Expression { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return FunctionalExpressionRing(this).block() -} - -/** - * Creates a functional expression with this [Field]. - */ -public inline fun Field.fieldExpression(block: FunctionalExpressionField>.() -> Expression): Expression { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return FunctionalExpressionField(this).block() -} - -/** - * Creates a functional expression with this [ExtendedField]. - */ -public inline fun ExtendedField.extendedFieldExpression(block: FunctionalExpressionExtendedField>.() -> Expression): Expression { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return FunctionalExpressionExtendedField(this).block() -} diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt index 737acc025..b9fb6879a 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/Symbol.kt @@ -32,10 +32,33 @@ public value class StringSymbol(override val identity: String) : Symbol { override fun toString(): String = identity } - /** * A delegate to create a symbol with a string identity in this scope */ public val symbol: ReadOnlyProperty = ReadOnlyProperty { _, property -> StringSymbol(property.name) } + +/** + * Ger a value from a [String]-keyed map by a [Symbol] + */ +public operator fun Map.get(symbol: Symbol): T? = get(symbol.identity) + +/** + * Set a value of [String]-keyed map by a [Symbol] + */ +public operator fun MutableMap.set(symbol: Symbol, value: T){ + set(symbol.identity, value) +} + +/** + * Get a value from a [Symbol]-keyed map by a [String] + */ +public operator fun Map.get(string: String): T? = get(StringSymbol(string)) + +/** + * Set a value of [String]-keyed map by a [Symbol] + */ +public operator fun MutableMap.set(string: String, value: T){ + set(StringSymbol(string), value) +} \ No newline at end of file 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 0334e0466..c994580bd 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 @@ -44,9 +44,9 @@ public interface Algebra { /** * Dynamically dispatches an unary operation with the certain name. * - * This function must follow two properties: + * This function must has two features: * - * 1. In case if operation is not defined in the structure, the function throws [kotlin.IllegalStateException]. + * 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)`. * 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 6a35828ee..deeb07e0e 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 @@ -97,14 +97,12 @@ public interface NumericAlgebra : Algebra { /** * The π mathematical constant. */ -public val NumericAlgebra.pi: T - get() = bindSymbolOrNull("pi") ?: number(PI) +public val NumericAlgebra.pi: T get() = bindSymbolOrNull("pi") ?: number(PI) /** * The *e* mathematical constant. */ -public val NumericAlgebra.e: T - get() = number(E) +public val NumericAlgebra.e: T get() = number(E) /** * Scale by scalar operations diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt index 3b0abf657..0d6d9e98d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt @@ -49,12 +49,6 @@ public interface ExtendedField : ExtendedFieldOperations, Field, Numeri public override fun acosh(arg: T): T = ln(arg + sqrt((arg - one) * (arg + one))) public override fun atanh(arg: T): T = (ln(arg + one) - ln(one - arg)) / 2.0 - public override fun bindSymbol(value: String): T = when (value) { - "pi" -> pi - "e" -> e - else -> super.bindSymbol(value) - } - public override fun rightSideNumberOperationFunction(operation: String): (left: T, right: Number) -> T = when (operation) { PowerOperations.POW_OPERATION -> ::power