diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 7dd459104..14bc70a2a 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -674,6 +674,9 @@ public final class space/kscience/kmath/misc/CumulativeKt { public static final fun cumulativeSumOfLong (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; } +public abstract interface annotation class space/kscience/kmath/misc/PerformancePitfall : java/lang/annotation/Annotation { +} + public final class space/kscience/kmath/misc/StringSymbol : space/kscience/kmath/misc/Symbol { public static final synthetic fun box-impl (Ljava/lang/String;)Lspace/kscience/kmath/misc/StringSymbol; public static fun constructor-impl (Ljava/lang/String;)Ljava/lang/String; 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 8dce4ad6b..5b8183699 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 @@ -59,6 +59,7 @@ public interface Structure2D : StructureND { return get(index[0], index[1]) } + @PerformancePitfall override fun elements(): Sequence> = sequence { for (i in 0 until rowNum) for (j in 0 until colNum) yield(intArrayOf(i, j) to get(i, j)) @@ -134,6 +135,7 @@ private class MutableStructure2DWrapper(val structure: MutableStructureND) structure[intArrayOf(i, j)] = value } + @PerformancePitfall override fun elements(): Sequence> = structure.elements() override fun equals(other: Any?): Boolean = false diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt index 3604fda31..2a4837ee0 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt @@ -7,16 +7,14 @@ package space.kscience.kmath.geometry import space.kscience.kmath.linear.Point import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.GroupElement import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.invoke import kotlin.math.sqrt @OptIn(UnstableKMathAPI::class) -public interface Vector2D : Point, Vector, GroupElement { +public interface Vector2D : Point, Vector{ public val x: Double public val y: Double - public override val context: Euclidean2DSpace get() = Euclidean2DSpace public override val size: Int get() = 2 public override operator fun get(index: Int): Double = when (index) { diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean3DSpace.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean3DSpace.kt index 8643b2dbb..37e7d2cb2 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean3DSpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean3DSpace.kt @@ -7,17 +7,15 @@ package space.kscience.kmath.geometry import space.kscience.kmath.linear.Point import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.GroupElement import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.invoke import kotlin.math.sqrt @OptIn(UnstableKMathAPI::class) -public interface Vector3D : Point, Vector, GroupElement { +public interface Vector3D : Point, Vector { public val x: Double public val y: Double public val z: Double - public override val context: Euclidean3DSpace get() = Euclidean3DSpace public override val size: Int get() = 3 public override operator fun get(index: Int): Double = when (index) { diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt index 818d81ab0..e5f6830c5 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt @@ -12,7 +12,6 @@ import space.kscience.kmath.nd.FieldND import space.kscience.kmath.nd.Strides import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.Group -import space.kscience.kmath.operations.GroupElement import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.invoke @@ -26,9 +25,9 @@ public data class DomainBin>( @OptIn(UnstableKMathAPI::class) public class IndexedHistogram, V : Any>( - override val context: IndexedHistogramSpace, + public val context: IndexedHistogramSpace, public val values: StructureND, -) : Histogram>, GroupElement, IndexedHistogramSpace> { +) : Histogram> { override fun get(point: Point): Bin? { val index = context.getIndex(point) ?: return null 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 0205f1c87..3cac44c47 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 @@ -10,6 +10,7 @@ import org.nd4j.linalg.api.ops.impl.scalar.Pow import org.nd4j.linalg.api.ops.impl.transforms.strict.* import org.nd4j.linalg.factory.Nd4j import org.nd4j.linalg.ops.transforms.Transforms +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.nd.* import space.kscience.kmath.operations.* @@ -52,6 +53,7 @@ public interface Nd4jArrayAlgebra> : AlgebraND { return struct } + @PerformancePitfall public override fun StructureND.map(transform: C.(T) -> T): Nd4jArrayStructure { val newStruct = ndArray.dup().wrap() newStruct.elements().forEach { (idx, value) -> newStruct[idx] = elementContext.transform(value) } diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt index b6c524cc6..c80a1d771 100644 --- a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt @@ -6,6 +6,7 @@ package space.kscience.kmath.nd4j import org.nd4j.linalg.factory.Nd4j +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.invoke import kotlin.math.PI @@ -14,6 +15,7 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue import kotlin.test.fail +@OptIn(PerformancePitfall::class) internal class Nd4jArrayAlgebraTest { @Test fun testProduce() { @@ -28,7 +30,7 @@ internal class Nd4jArrayAlgebraTest { @Test fun testMap() { - val res = with(IntNd4jArrayRing(intArrayOf(2, 2))) { one.map() { it + it * 2 } } + val res = with(IntNd4jArrayRing(intArrayOf(2, 2))) { one.map { it + it * 2 } } val expected = (Nd4j.create(2, 2) ?: fail()).asIntStructure() expected[intArrayOf(0, 0)] = 3 expected[intArrayOf(0, 1)] = 3 diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt index d59e04194..bdfec2ba6 100644 --- a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructureTest.kt @@ -6,6 +6,7 @@ package space.kscience.kmath.nd4j import org.nd4j.linalg.factory.Nd4j +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.nd.get import kotlin.test.Test import kotlin.test.assertEquals @@ -13,6 +14,7 @@ import kotlin.test.assertNotEquals import kotlin.test.fail internal class Nd4jArrayStructureTest { + @OptIn(PerformancePitfall::class) @Test fun testElements() { val nd = Nd4j.create(doubleArrayOf(1.0, 2.0, 3.0))!! diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt index a6c6a7618..a176abdd4 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt @@ -1,5 +1,6 @@ package space.kscience.kmath.tensors.core +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.nd.DefaultStrides import space.kscience.kmath.nd.MutableBufferND import space.kscience.kmath.nd.as1D @@ -24,6 +25,7 @@ internal class TestDoubleTensor { assertEquals(tensor.value(), value) } + @OptIn(PerformancePitfall::class) @Test fun testStrides() = DoubleTensorAlgebra { val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))