From 730678b2fc5aaba5ad3b3fba32fc77ae4b72f160 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 31 Jan 2021 20:02:11 +0300 Subject: [PATCH 1/2] Add safety checks for kmath-for-real buffer operations. --- .../kotlin/kscience/kmath/real/RealVector.kt | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt index 596692782..ee2a960a1 100644 --- a/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/kscience/kmath/real/RealVector.kt @@ -29,8 +29,10 @@ public inline fun RealVector.map(transform: (Double) -> Double): RealVector = public inline fun RealVector.mapIndexed(transform: (index: Int, value: Double) -> Double): RealVector = Buffer.real(size) { transform(it, get(it)) } -public operator fun RealVector.plus(other: RealVector): RealVector = - mapIndexed { index, value -> value + other[index] } +public operator fun RealVector.plus(other: RealVector): RealVector { + require(size == other.size){"Vector size $size expected but ${other.size} found"} + return mapIndexed { index, value -> value + other[index] } +} public operator fun RealVector.plus(number: Number): RealVector = map { it + number.toDouble() } @@ -38,22 +40,28 @@ public operator fun Number.plus(vector: RealVector): RealVector = vector + this public operator fun RealVector.unaryMinus(): Buffer = map { -it } -public operator fun RealVector.minus(other: RealVector): RealVector = - mapIndexed { index, value -> value - other[index] } +public operator fun RealVector.minus(other: RealVector): RealVector { + require(size == other.size){"Vector size $size expected but ${other.size} found"} + return mapIndexed { index, value -> value - other[index] } +} public operator fun RealVector.minus(number: Number): RealVector = map { it - number.toDouble() } public operator fun Number.minus(vector: RealVector): RealVector = vector.map { toDouble() - it } -public operator fun RealVector.times(other: RealVector): RealVector = - mapIndexed { index, value -> value * other[index] } +public operator fun RealVector.times(other: RealVector): RealVector { + require(size == other.size){"Vector size $size expected but ${other.size} found"} + return mapIndexed { index, value -> value * other[index] } +} public operator fun RealVector.times(number: Number): RealVector = map { it * number.toDouble() } public operator fun Number.times(vector: RealVector): RealVector = vector * this -public operator fun RealVector.div(other: RealVector): RealVector = - mapIndexed { index, value -> value / other[index] } +public operator fun RealVector.div(other: RealVector): RealVector { + require(size == other.size){"Vector size $size expected but ${other.size} found"} + return mapIndexed { index, value -> value / other[index] } +} public operator fun RealVector.div(number: Number): RealVector = map { it / number.toDouble() } From 8f101c5cd0b60af6f9df527f5b9c545bc304de04 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 1 Feb 2021 12:34:30 +0300 Subject: [PATCH 2/2] Fix symbol delegate after build work-around for https://youtrack.jetbrains.com/issue/KT-4012 --- kmath-core/api/kmath-core.api | 6 ------ .../kscience/kmath/expressions/Expression.kt | 14 +++----------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index ab0555932..92165f795 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -488,15 +488,9 @@ public final class kscience/kmath/expressions/StringSymbol : kscience/kmath/expr } public abstract interface class kscience/kmath/expressions/Symbol { - public static final field Companion Lkscience/kmath/expressions/Symbol$Companion; public abstract fun getIdentity ()Ljava/lang/String; } -public final class kscience/kmath/expressions/Symbol$Companion : kotlin/properties/ReadOnlyProperty { - public synthetic fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object; - public fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Lkscience/kmath/expressions/Symbol; -} - public abstract interface class kscience/kmath/expressions/SymbolIndexer { public abstract fun get (Ljava/util/List;Lkscience/kmath/expressions/Symbol;)Ljava/lang/Object; public abstract fun get (Lkscience/kmath/nd/Structure2D;Lkscience/kmath/expressions/Symbol;Lkscience/kmath/expressions/Symbol;)Ljava/lang/Object; diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt index 63bbc9312..c53d64871 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt @@ -3,7 +3,6 @@ package kscience.kmath.expressions import kscience.kmath.operations.Algebra import kotlin.jvm.JvmName import kotlin.properties.ReadOnlyProperty -import kotlin.reflect.KProperty /** * A marker interface for a symbol. A symbol mus have an identity @@ -13,13 +12,6 @@ public interface Symbol { * Identity object for the symbol. Two symbols with the same identity are considered to be the same symbol. */ public val identity: String - - public companion object : ReadOnlyProperty { - //TODO deprecate and replace by top level function after fix of https://youtrack.jetbrains.com/issue/KT-40121 - override fun getValue(thisRef: Any?, property: KProperty<*>): Symbol { - return StringSymbol(property.name) - } - } } /** @@ -103,9 +95,9 @@ public fun ExpressionAlgebra.bind(symbol: Symbol): E = /** * A delegate to create a symbol with a string identity in this scope */ -public val symbol: ReadOnlyProperty get() = Symbol -//TODO does not work directly on native due to https://youtrack.jetbrains.com/issue/KT-40121 - +public val symbol: ReadOnlyProperty = ReadOnlyProperty { thisRef, property -> + StringSymbol(property.name) +} /** * Bind a symbol by name inside the [ExpressionAlgebra]