diff --git a/CHANGELOG.md b/CHANGELOG.md index f9e3f963e..644c634b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ - `toGrid` method. - Public visibility of `BufferAccessor2D` - `Real` class +- StructureND identity and equals ### Fixed - `symbol` method in `MstExtendedField` (https://github.com/mipt-npm/kmath/pull/140) diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt index b4653b598..6741209fc 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt @@ -20,10 +20,10 @@ class StreamDoubleFieldND( private val strides = DefaultStrides(shape) override val elementContext: DoubleField get() = DoubleField - override val zero: NDBuffer by lazy { produce { zero } } - override val one: NDBuffer by lazy { produce { one } } + override val zero: BufferND by lazy { produce { zero } } + override val one: BufferND by lazy { produce { one } } - override fun number(value: Number): NDBuffer { + override fun number(value: Number): BufferND { val d = value.toDouble() // minimize conversions return produce { d } } @@ -34,30 +34,30 @@ class StreamDoubleFieldND( this@StreamDoubleFieldND.shape, shape ) - this is NDBuffer && this.strides == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer + this is BufferND && this.strides == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) } } - override fun produce(initializer: DoubleField.(IntArray) -> Double): NDBuffer { + override fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND { val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> val index = strides.index(offset) DoubleField.initializer(index) }.toArray() - return NDBuffer(strides, array.asBuffer()) + return BufferND(strides, array.asBuffer()) } override fun StructureND.map( transform: DoubleField.(Double) -> Double, - ): NDBuffer { + ): BufferND { val array = Arrays.stream(buffer.array).parallel().map { DoubleField.transform(it) }.toArray() - return NDBuffer(strides, array.asBuffer()) + return BufferND(strides, array.asBuffer()) } override fun StructureND.mapIndexed( transform: DoubleField.(index: IntArray, Double) -> Double, - ): NDBuffer { + ): BufferND { val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> DoubleField.transform( strides.index(offset), @@ -65,43 +65,43 @@ class StreamDoubleFieldND( ) }.toArray() - return NDBuffer(strides, array.asBuffer()) + return BufferND(strides, array.asBuffer()) } override fun combine( a: StructureND, b: StructureND, transform: DoubleField.(Double, Double) -> Double, - ): NDBuffer { + ): BufferND { val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> DoubleField.transform(a.buffer.array[offset], b.buffer.array[offset]) }.toArray() - return NDBuffer(strides, array.asBuffer()) + return BufferND(strides, array.asBuffer()) } override fun StructureND.unaryMinus(): StructureND = map { -it } override fun scale(a: StructureND, value: Double): StructureND = a.map { it * value } - override fun power(arg: StructureND, pow: Number): NDBuffer = arg.map { power(it, pow) } + override fun power(arg: StructureND, pow: Number): BufferND = arg.map { power(it, pow) } - override fun exp(arg: StructureND): NDBuffer = arg.map { exp(it) } + override fun exp(arg: StructureND): BufferND = arg.map { exp(it) } - override fun ln(arg: StructureND): NDBuffer = arg.map { ln(it) } + override fun ln(arg: StructureND): BufferND = arg.map { ln(it) } - override fun sin(arg: StructureND): NDBuffer = arg.map { sin(it) } - override fun cos(arg: StructureND): NDBuffer = arg.map { cos(it) } - override fun tan(arg: StructureND): NDBuffer = arg.map { tan(it) } - override fun asin(arg: StructureND): NDBuffer = arg.map { asin(it) } - override fun acos(arg: StructureND): NDBuffer = arg.map { acos(it) } - override fun atan(arg: StructureND): NDBuffer = arg.map { atan(it) } + override fun sin(arg: StructureND): BufferND = arg.map { sin(it) } + override fun cos(arg: StructureND): BufferND = arg.map { cos(it) } + override fun tan(arg: StructureND): BufferND = arg.map { tan(it) } + override fun asin(arg: StructureND): BufferND = arg.map { asin(it) } + override fun acos(arg: StructureND): BufferND = arg.map { acos(it) } + override fun atan(arg: StructureND): BufferND = arg.map { atan(it) } - override fun sinh(arg: StructureND): NDBuffer = arg.map { sinh(it) } - override fun cosh(arg: StructureND): NDBuffer = arg.map { cosh(it) } - override fun tanh(arg: StructureND): NDBuffer = arg.map { tanh(it) } - override fun asinh(arg: StructureND): NDBuffer = arg.map { asinh(it) } - override fun acosh(arg: StructureND): NDBuffer = arg.map { acosh(it) } - override fun atanh(arg: StructureND): NDBuffer = arg.map { atanh(it) } + override fun sinh(arg: StructureND): BufferND = arg.map { sinh(it) } + override fun cosh(arg: StructureND): BufferND = arg.map { cosh(it) } + override fun tanh(arg: StructureND): BufferND = arg.map { tanh(it) } + override fun asinh(arg: StructureND): BufferND = arg.map { asinh(it) } + override fun acosh(arg: StructureND): BufferND = arg.map { acosh(it) } + override fun atanh(arg: StructureND): BufferND = arg.map { atanh(it) } } fun AlgebraND.Companion.realWithStream(vararg shape: Int): StreamDoubleFieldND = StreamDoubleFieldND(shape) \ No newline at end of file diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt index 89f984a5b..1c8a923c7 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt @@ -1,7 +1,7 @@ package space.kscience.kmath.structures +import space.kscience.kmath.nd.BufferND import space.kscience.kmath.nd.DefaultStrides -import space.kscience.kmath.nd.NDBuffer import kotlin.system.measureTimeMillis @Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE") @@ -10,7 +10,7 @@ fun main() { val array = DoubleArray(n * n) { 1.0 } val buffer = DoubleBuffer(array) val strides = DefaultStrides(intArrayOf(n, n)) - val structure = NDBuffer(strides, buffer) + val structure = BufferND(strides, buffer) measureTimeMillis { var res = 0.0 diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt index 4efd8383b..89e9649e9 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt @@ -3,25 +3,16 @@ package space.kscience.kmath.commons.linear import org.apache.commons.math3.linear.* import space.kscience.kmath.linear.* import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.structures.DoubleBuffer import kotlin.reflect.KClass import kotlin.reflect.cast -public class CMMatrix(public val origin: RealMatrix) : Matrix { +public inline class CMMatrix(public val origin: RealMatrix) : Matrix { public override val rowNum: Int get() = origin.rowDimension public override val colNum: Int get() = origin.columnDimension public override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j) - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is StructureND<*>) return false - return StructureND.contentEquals(this, other) - } - - override fun hashCode(): Int = origin.hashCode() } public inline class CMVector(public val origin: RealVector) : Point { diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt index ad6b6d8cd..010ea7e9f 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt @@ -6,9 +6,11 @@ import kotlinx.coroutines.flow.map import org.apache.commons.math3.transform.* import space.kscience.kmath.complex.Complex import space.kscience.kmath.streaming.chunked +import space.kscience.kmath.streaming.spread import space.kscience.kmath.structures.* + /** * Streaming and buffer transformations */ diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt index e16962da7..d11f2b7db 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt @@ -2,8 +2,8 @@ package space.kscience.kmath.complex import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.nd.AlgebraND +import space.kscience.kmath.nd.BufferND import space.kscience.kmath.nd.BufferedFieldND -import space.kscience.kmath.nd.NDBuffer import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.ExtendedField import space.kscience.kmath.operations.NumbersAddOperations @@ -22,10 +22,10 @@ public class ComplexFieldND( NumbersAddOperations>, ExtendedField> { - override val zero: NDBuffer by lazy { produce { zero } } - override val one: NDBuffer by lazy { produce { one } } + override val zero: BufferND by lazy { produce { zero } } + override val one: BufferND by lazy { produce { one } } - override fun number(value: Number): NDBuffer { + override fun number(value: Number): BufferND { val d = value.toComplex() // minimize conversions return produce { d } } @@ -76,35 +76,35 @@ public class ComplexFieldND( // return BufferedNDFieldElement(this, buffer) // } - override fun power(arg: StructureND, pow: Number): NDBuffer = arg.map { power(it, pow) } + override fun power(arg: StructureND, pow: Number): BufferND = arg.map { power(it, pow) } - override fun exp(arg: StructureND): NDBuffer = arg.map { exp(it) } + override fun exp(arg: StructureND): BufferND = arg.map { exp(it) } - override fun ln(arg: StructureND): NDBuffer = arg.map { ln(it) } + override fun ln(arg: StructureND): BufferND = arg.map { ln(it) } - override fun sin(arg: StructureND): NDBuffer = arg.map { sin(it) } - override fun cos(arg: StructureND): NDBuffer = arg.map { cos(it) } - override fun tan(arg: StructureND): NDBuffer = arg.map { tan(it) } - override fun asin(arg: StructureND): NDBuffer = arg.map { asin(it) } - override fun acos(arg: StructureND): NDBuffer = arg.map { acos(it) } - override fun atan(arg: StructureND): NDBuffer = arg.map { atan(it) } + override fun sin(arg: StructureND): BufferND = arg.map { sin(it) } + override fun cos(arg: StructureND): BufferND = arg.map { cos(it) } + override fun tan(arg: StructureND): BufferND = arg.map { tan(it) } + override fun asin(arg: StructureND): BufferND = arg.map { asin(it) } + override fun acos(arg: StructureND): BufferND = arg.map { acos(it) } + override fun atan(arg: StructureND): BufferND = arg.map { atan(it) } - override fun sinh(arg: StructureND): NDBuffer = arg.map { sinh(it) } - override fun cosh(arg: StructureND): NDBuffer = arg.map { cosh(it) } - override fun tanh(arg: StructureND): NDBuffer = arg.map { tanh(it) } - override fun asinh(arg: StructureND): NDBuffer = arg.map { asinh(it) } - override fun acosh(arg: StructureND): NDBuffer = arg.map { acosh(it) } - override fun atanh(arg: StructureND): NDBuffer = arg.map { atanh(it) } + override fun sinh(arg: StructureND): BufferND = arg.map { sinh(it) } + override fun cosh(arg: StructureND): BufferND = arg.map { cosh(it) } + override fun tanh(arg: StructureND): BufferND = arg.map { tanh(it) } + override fun asinh(arg: StructureND): BufferND = arg.map { asinh(it) } + override fun acosh(arg: StructureND): BufferND = arg.map { acosh(it) } + override fun atanh(arg: StructureND): BufferND = arg.map { atanh(it) } } /** * Fast element production using function inlining */ -public inline fun BufferedFieldND.produceInline(initializer: ComplexField.(Int) -> Complex): NDBuffer { +public inline fun BufferedFieldND.produceInline(initializer: ComplexField.(Int) -> Complex): BufferND { contract { callsInPlace(initializer, InvocationKind.EXACTLY_ONCE) } val buffer = Buffer.complex(strides.linearSize) { offset -> ComplexField.initializer(offset) } - return NDBuffer(strides, buffer) + return BufferND(strides, buffer) } diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 8be9ff115..5fabaae1b 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -584,7 +584,6 @@ public final class space/kscience/kmath/linear/MatrixFeaturesKt { public final class space/kscience/kmath/linear/MatrixWrapper : space/kscience/kmath/nd/Structure2D { public fun elements ()Lkotlin/sequences/Sequence; - public fun equals (Ljava/lang/Object;)Z public fun get (II)Ljava/lang/Object; public fun get ([I)Ljava/lang/Object; public fun getColNum ()I @@ -595,7 +594,6 @@ public final class space/kscience/kmath/linear/MatrixWrapper : space/kscience/km public fun getRowNum ()I public fun getRows ()Ljava/util/List; public fun getShape ()[I - public fun hashCode ()I public fun toString ()Ljava/lang/String; } @@ -640,7 +638,6 @@ public final class space/kscience/kmath/linear/UnitFeature : space/kscience/kmat public final class space/kscience/kmath/linear/VirtualMatrix : space/kscience/kmath/nd/Structure2D { public fun (IILkotlin/jvm/functions/Function2;)V public fun elements ()Lkotlin/sequences/Sequence; - public fun equals (Ljava/lang/Object;)Z public fun get (II)Ljava/lang/Object; public fun get ([I)Ljava/lang/Object; public fun getColNum ()I @@ -650,7 +647,6 @@ public final class space/kscience/kmath/linear/VirtualMatrix : space/kscience/km public fun getRowNum ()I public fun getRows ()Ljava/util/List; public fun getShape ()[I - public fun hashCode ()I } public final class space/kscience/kmath/linear/ZeroFeature : space/kscience/kmath/linear/DiagonalFeature { @@ -698,22 +694,22 @@ public final class space/kscience/kmath/nd/AlgebraND$DefaultImpls { } public abstract interface class space/kscience/kmath/nd/BufferAlgebraND : space/kscience/kmath/nd/AlgebraND { - public abstract fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; + public abstract fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public abstract fun getBuffer (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/structures/Buffer; public abstract fun getBufferFactory ()Lkotlin/jvm/functions/Function2; public abstract fun getStrides ()Lspace/kscience/kmath/nd/Strides; - public abstract fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; - public abstract fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; - public abstract fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public abstract fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; + public abstract fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; + public abstract fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; } public final class space/kscience/kmath/nd/BufferAlgebraND$DefaultImpls { - public static fun combine (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; + public static fun combine (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public static fun getBuffer (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/structures/Buffer; public static fun invoke (Lspace/kscience/kmath/nd/BufferAlgebraND;Lkotlin/jvm/functions/Function1;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public static fun map (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; - public static fun mapIndexed (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; - public static fun produce (Lspace/kscience/kmath/nd/BufferAlgebraND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public static fun map (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; + public static fun mapIndexed (Lspace/kscience/kmath/nd/BufferAlgebraND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; + public static fun produce (Lspace/kscience/kmath/nd/BufferAlgebraND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; } public final class space/kscience/kmath/nd/BufferAlgebraNDKt { @@ -725,6 +721,17 @@ public final class space/kscience/kmath/nd/BufferAlgebraNDKt { public static final fun ring (Lspace/kscience/kmath/nd/AlgebraND$Companion;Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;[I)Lspace/kscience/kmath/nd/BufferedRingND; } +public class space/kscience/kmath/nd/BufferND : space/kscience/kmath/nd/StructureND { + public fun (Lspace/kscience/kmath/nd/Strides;Lspace/kscience/kmath/structures/Buffer;)V + public fun elements ()Lkotlin/sequences/Sequence; + public fun get ([I)Ljava/lang/Object; + public fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; + public fun getDimension ()I + public fun getShape ()[I + public final fun getStrides ()Lspace/kscience/kmath/nd/Strides; + public fun toString ()Ljava/lang/String; +} + public class space/kscience/kmath/nd/BufferedFieldND : space/kscience/kmath/nd/BufferedRingND, space/kscience/kmath/nd/FieldND { public fun ([ILspace/kscience/kmath/operations/Field;Lkotlin/jvm/functions/Function2;)V public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; @@ -761,7 +768,7 @@ public class space/kscience/kmath/nd/BufferedGroupND : space/kscience/kmath/nd/B public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public synthetic fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object; public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/nd/StructureND; - public fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; + public fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; public fun getBuffer (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/structures/Buffer; public final fun getBufferFactory ()Lkotlin/jvm/functions/Function2; @@ -770,11 +777,11 @@ public class space/kscience/kmath/nd/BufferedGroupND : space/kscience/kmath/nd/B public final fun getShape ()[I public fun getStrides ()Lspace/kscience/kmath/nd/Strides; public synthetic fun getZero ()Ljava/lang/Object; - public fun getZero ()Lspace/kscience/kmath/nd/NDBuffer; + public fun getZero ()Lspace/kscience/kmath/nd/BufferND; public fun invoke (Lkotlin/jvm/functions/Function1;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; - public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; + public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun minus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; @@ -784,7 +791,7 @@ public class space/kscience/kmath/nd/BufferedGroupND : space/kscience/kmath/nd/B public fun plus (Ljava/lang/Object;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND; public fun plus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; - public fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; public fun unaryMinus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; @@ -799,7 +806,7 @@ public class space/kscience/kmath/nd/BufferedRingND : space/kscience/kmath/nd/Bu public fun ([ILspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public synthetic fun getOne ()Ljava/lang/Object; - public fun getOne ()Lspace/kscience/kmath/nd/NDBuffer; + public fun getOne ()Lspace/kscience/kmath/nd/BufferND; public synthetic fun multiply (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun multiply (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun times (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; @@ -828,43 +835,43 @@ public final class space/kscience/kmath/nd/DefaultStrides$Companion { public final class space/kscience/kmath/nd/DoubleFieldND : space/kscience/kmath/nd/BufferedFieldND, space/kscience/kmath/operations/ExtendedField, space/kscience/kmath/operations/NumbersAddOperations, space/kscience/kmath/operations/ScaleOperations { public fun ([I)V public synthetic fun acos (Ljava/lang/Object;)Ljava/lang/Object; - public fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun acos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun acosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun acosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun asin (Ljava/lang/Object;)Ljava/lang/Object; - public fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun asin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun asinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun asinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun atan (Ljava/lang/Object;)Ljava/lang/Object; - public fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun atan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun atanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; - public fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; + public fun atanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; + public fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun combine (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun cos (Ljava/lang/Object;)Ljava/lang/Object; - public fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun cos (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun cosh (Ljava/lang/Object;)Ljava/lang/Object; - public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun cosh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun exp (Ljava/lang/Object;)Ljava/lang/Object; - public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun exp (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun getBuffer (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/structures/Buffer; public fun getBuffer-Udx-57Q (Lspace/kscience/kmath/nd/StructureND;)[D public synthetic fun getOne ()Ljava/lang/Object; - public fun getOne ()Lspace/kscience/kmath/nd/NDBuffer; + public fun getOne ()Lspace/kscience/kmath/nd/BufferND; public synthetic fun getZero ()Ljava/lang/Object; - public fun getZero ()Lspace/kscience/kmath/nd/NDBuffer; + public fun getZero ()Lspace/kscience/kmath/nd/BufferND; public synthetic fun ln (Ljava/lang/Object;)Ljava/lang/Object; - public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; - public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public fun ln (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; + public fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun map (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; - public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/NDBuffer; + public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun minus (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; public fun minus (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; public fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/NDBuffer; + public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun plus (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; public fun plus (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; @@ -873,22 +880,22 @@ public final class space/kscience/kmath/nd/DoubleFieldND : space/kscience/kmath/ public synthetic fun pow (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; public fun pow (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun power (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/NDBuffer; - public fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public fun power (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/BufferND; + public fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/StructureND; public fun rightSideNumberOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; public fun scale (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/nd/StructureND; public synthetic fun sin (Ljava/lang/Object;)Ljava/lang/Object; - public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun sin (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun sinh (Ljava/lang/Object;)Ljava/lang/Object; - public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun sinh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun sqrt (Ljava/lang/Object;)Ljava/lang/Object; public fun sqrt (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun tan (Ljava/lang/Object;)Ljava/lang/Object; - public fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun tan (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun tanh (Ljava/lang/Object;)Ljava/lang/Object; - public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/NDBuffer; + public fun tanh (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/BufferND; public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; } @@ -965,32 +972,19 @@ public final class space/kscience/kmath/nd/GroupND$DefaultImpls { public static fun unaryPlus (Lspace/kscience/kmath/nd/GroupND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; } -public final class space/kscience/kmath/nd/MutableNDBuffer : space/kscience/kmath/nd/NDBuffer, space/kscience/kmath/nd/MutableNDStructure { +public final class space/kscience/kmath/nd/MutableBufferND : space/kscience/kmath/nd/BufferND, space/kscience/kmath/nd/MutableStructureND { public fun (Lspace/kscience/kmath/nd/Strides;Lspace/kscience/kmath/structures/MutableBuffer;)V public synthetic fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; public fun getBuffer ()Lspace/kscience/kmath/structures/MutableBuffer; public fun set ([ILjava/lang/Object;)V } -public abstract interface class space/kscience/kmath/nd/MutableNDStructure : space/kscience/kmath/nd/StructureND { +public abstract interface class space/kscience/kmath/nd/MutableStructureND : space/kscience/kmath/nd/StructureND { public abstract fun set ([ILjava/lang/Object;)V } -public final class space/kscience/kmath/nd/MutableNDStructure$DefaultImpls { - public static fun getDimension (Lspace/kscience/kmath/nd/MutableNDStructure;)I -} - -public class space/kscience/kmath/nd/NDBuffer : space/kscience/kmath/nd/StructureND { - public fun (Lspace/kscience/kmath/nd/Strides;Lspace/kscience/kmath/structures/Buffer;)V - public fun elements ()Lkotlin/sequences/Sequence; - public fun equals (Ljava/lang/Object;)Z - public fun get ([I)Ljava/lang/Object; - public fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; - public fun getDimension ()I - public fun getShape ()[I - public final fun getStrides ()Lspace/kscience/kmath/nd/Strides; - public fun hashCode ()I - public fun toString ()Ljava/lang/String; +public final class space/kscience/kmath/nd/MutableStructureND$DefaultImpls { + public static fun getDimension (Lspace/kscience/kmath/nd/MutableStructureND;)I } public abstract interface class space/kscience/kmath/nd/RingND : space/kscience/kmath/nd/GroupND, space/kscience/kmath/operations/Ring { @@ -1033,9 +1027,9 @@ public final class space/kscience/kmath/nd/ShapeMismatchException : java/lang/Ru public final class space/kscience/kmath/nd/ShortRingND : space/kscience/kmath/nd/BufferedRingND, space/kscience/kmath/operations/NumbersAddOperations { public fun ([I)V public synthetic fun getOne ()Ljava/lang/Object; - public fun getOne ()Lspace/kscience/kmath/nd/NDBuffer; + public fun getOne ()Lspace/kscience/kmath/nd/BufferND; public synthetic fun getZero ()Ljava/lang/Object; - public fun getZero ()Lspace/kscience/kmath/nd/NDBuffer; + public fun getZero ()Lspace/kscience/kmath/nd/BufferND; public synthetic fun leftSideNumberOperation (Ljava/lang/String;Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; public fun leftSideNumberOperation (Ljava/lang/String;Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public fun leftSideNumberOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; @@ -1044,7 +1038,7 @@ public final class space/kscience/kmath/nd/ShortRingND : space/kscience/kmath/nd public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; public fun minus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Number;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun number (Ljava/lang/Number;)Ljava/lang/Object; - public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/NDBuffer; + public fun number (Ljava/lang/Number;)Lspace/kscience/kmath/nd/BufferND; public synthetic fun plus (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; public fun plus (Ljava/lang/Number;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; @@ -1056,7 +1050,7 @@ public final class space/kscience/kmath/nd/ShortRingND : space/kscience/kmath/nd public final class space/kscience/kmath/nd/ShortRingNDKt { public static final fun nd (Lspace/kscience/kmath/operations/ShortRing;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public static final fun produceInline (Lspace/kscience/kmath/nd/BufferedRingND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/NDBuffer; + public static final fun produceInline (Lspace/kscience/kmath/nd/BufferedRingND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/BufferND; } public abstract interface class space/kscience/kmath/nd/Strides { @@ -1121,20 +1115,18 @@ public final class space/kscience/kmath/nd/Structure2DKt { public abstract interface class space/kscience/kmath/nd/StructureND { public static final field Companion Lspace/kscience/kmath/nd/StructureND$Companion; public abstract fun elements ()Lkotlin/sequences/Sequence; - public abstract fun equals (Ljava/lang/Object;)Z public abstract fun get ([I)Ljava/lang/Object; public abstract fun getDimension ()I public abstract fun getShape ()[I - public abstract fun hashCode ()I } public final class space/kscience/kmath/nd/StructureND$Companion { - public final fun auto (Lkotlin/reflect/KClass;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public final fun auto (Lkotlin/reflect/KClass;[ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public final fun buffered (Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public final fun buffered ([ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/StructureND$Companion;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/NDBuffer; - public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/StructureND$Companion;[ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/NDBuffer; + public final fun auto (Lkotlin/reflect/KClass;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; + public final fun auto (Lkotlin/reflect/KClass;[ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; + public final fun buffered (Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; + public final fun buffered ([ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/BufferND; + public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/StructureND$Companion;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/BufferND; + public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/StructureND$Companion;[ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/BufferND; public final fun contentEquals (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Z } @@ -1144,7 +1136,7 @@ public final class space/kscience/kmath/nd/StructureND$DefaultImpls { public final class space/kscience/kmath/nd/StructureNDKt { public static final fun get (Lspace/kscience/kmath/nd/StructureND;[I)Ljava/lang/Object; - public static final fun mapInPlace (Lspace/kscience/kmath/nd/MutableNDStructure;Lkotlin/jvm/functions/Function2;)V + public static final fun mapInPlace (Lspace/kscience/kmath/nd/MutableStructureND;Lkotlin/jvm/functions/Function2;)V } public abstract interface class space/kscience/kmath/operations/Algebra { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt index 97f0acd61..868f74cc6 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt @@ -23,8 +23,6 @@ public class MatrixWrapper internal constructor( override fun getFeature(type: KClass): T? = features.singleOrNull { type.isInstance(it) } as? T ?: origin.getFeature(type) - override fun equals(other: Any?): Boolean = origin == other - override fun hashCode(): Int = origin.hashCode() override fun toString(): String { return "MatrixWrapper(matrix=$origin, features=$features)" } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt index 56cafbdb5..e80ee5ebd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt @@ -1,7 +1,5 @@ package space.kscience.kmath.linear -import space.kscience.kmath.nd.StructureND - /** * The matrix where each element is evaluated each time when is being accessed. * @@ -16,17 +14,4 @@ public class VirtualMatrix( override val shape: IntArray get() = intArrayOf(rowNum, colNum) override operator fun get(i: Int, j: Int): T = generator(i, j) - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is StructureND<*>) return false - return StructureND.contentEquals(this, other) - } - - override fun hashCode(): Int { - var result = rowNum - result = 31 * result + colNum - result = 31 * result + generator.hashCode() - return result - } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt index b81eb9519..67e94df74 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt @@ -10,7 +10,7 @@ public interface BufferAlgebraND> : AlgebraND { public val strides: Strides public val bufferFactory: BufferFactory - override fun produce(initializer: A.(IntArray) -> T): NDBuffer = NDBuffer( + override fun produce(initializer: A.(IntArray) -> T): BufferND = BufferND( strides, bufferFactory(strides.linearSize) { offset -> elementContext.initializer(strides.index(offset)) @@ -23,32 +23,32 @@ public interface BufferAlgebraND> : AlgebraND { this@BufferAlgebraND.shape, shape ) - this is NDBuffer && this.strides == this@BufferAlgebraND.strides -> this.buffer + this is BufferND && this.strides == this@BufferAlgebraND.strides -> this.buffer else -> bufferFactory(strides.linearSize) { offset -> get(strides.index(offset)) } } - override fun StructureND.map(transform: A.(T) -> T): NDBuffer { + override fun StructureND.map(transform: A.(T) -> T): BufferND { val buffer = bufferFactory(strides.linearSize) { offset -> elementContext.transform(buffer[offset]) } - return NDBuffer(strides, buffer) + return BufferND(strides, buffer) } - override fun StructureND.mapIndexed(transform: A.(index: IntArray, T) -> T): NDBuffer { + override fun StructureND.mapIndexed(transform: A.(index: IntArray, T) -> T): BufferND { val buffer = bufferFactory(strides.linearSize) { offset -> elementContext.transform( strides.index(offset), buffer[offset] ) } - return NDBuffer(strides, buffer) + return BufferND(strides, buffer) } - override fun combine(a: StructureND, b: StructureND, transform: A.(T, T) -> T): NDBuffer { + override fun combine(a: StructureND, b: StructureND, transform: A.(T, T) -> T): BufferND { val buffer = bufferFactory(strides.linearSize) { offset -> elementContext.transform(a.buffer[offset], b.buffer[offset]) } - return NDBuffer(strides, buffer) + return BufferND(strides, buffer) } } @@ -58,7 +58,7 @@ public open class BufferedGroupND>( final override val bufferFactory: BufferFactory, ) : GroupND, BufferAlgebraND { override val strides: Strides = DefaultStrides(shape) - override val zero: NDBuffer by lazy { produce { zero } } + override val zero: BufferND by lazy { produce { zero } } override fun StructureND.unaryMinus(): StructureND = produce { -get(it) } } @@ -67,7 +67,7 @@ public open class BufferedRingND>( elementContext: R, bufferFactory: BufferFactory, ) : BufferedGroupND(shape, elementContext, bufferFactory), RingND { - override val one: NDBuffer by lazy { produce { one } } + override val one: BufferND by lazy { produce { one } } } public open class BufferedFieldND>( diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt index a7a9d8935..d38ed02da 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt @@ -17,10 +17,10 @@ public class DoubleFieldND( ScaleOperations>, ExtendedField> { - override val zero: NDBuffer by lazy { produce { zero } } - override val one: NDBuffer by lazy { produce { one } } + override val zero: BufferND by lazy { produce { zero } } + override val one: BufferND by lazy { produce { one } } - override fun number(value: Number): NDBuffer { + override fun number(value: Number): BufferND { val d = value.toDouble() // minimize conversions return produce { d } } @@ -31,31 +31,31 @@ public class DoubleFieldND( this@DoubleFieldND.shape, shape ) - this is NDBuffer && this.strides == this@DoubleFieldND.strides -> this.buffer as DoubleBuffer + this is BufferND && this.strides == this@DoubleFieldND.strides -> this.buffer as DoubleBuffer else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) } } @Suppress("OVERRIDE_BY_INLINE") override inline fun StructureND.map( transform: DoubleField.(Double) -> Double, - ): NDBuffer { + ): BufferND { val buffer = DoubleBuffer(strides.linearSize) { offset -> DoubleField.transform(buffer.array[offset]) } - return NDBuffer(strides, buffer) + return BufferND(strides, buffer) } @Suppress("OVERRIDE_BY_INLINE") - override inline fun produce(initializer: DoubleField.(IntArray) -> Double): NDBuffer { + override inline fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND { val array = DoubleArray(strides.linearSize) { offset -> val index = strides.index(offset) DoubleField.initializer(index) } - return NDBuffer(strides, DoubleBuffer(array)) + return BufferND(strides, DoubleBuffer(array)) } @Suppress("OVERRIDE_BY_INLINE") override inline fun StructureND.mapIndexed( transform: DoubleField.(index: IntArray, Double) -> Double, - ): NDBuffer = NDBuffer( + ): BufferND = BufferND( strides, buffer = DoubleBuffer(strides.linearSize) { offset -> DoubleField.transform( @@ -69,34 +69,34 @@ public class DoubleFieldND( a: StructureND, b: StructureND, transform: DoubleField.(Double, Double) -> Double, - ): NDBuffer { + ): BufferND { val buffer = DoubleBuffer(strides.linearSize) { offset -> DoubleField.transform(a.buffer.array[offset], b.buffer.array[offset]) } - return NDBuffer(strides, buffer) + return BufferND(strides, buffer) } override fun scale(a: StructureND, value: Double): StructureND = a.map { it * value } - override fun power(arg: StructureND, pow: Number): NDBuffer = arg.map { power(it, pow) } + override fun power(arg: StructureND, pow: Number): BufferND = arg.map { power(it, pow) } - override fun exp(arg: StructureND): NDBuffer = arg.map { exp(it) } + override fun exp(arg: StructureND): BufferND = arg.map { exp(it) } - override fun ln(arg: StructureND): NDBuffer = arg.map { ln(it) } + override fun ln(arg: StructureND): BufferND = arg.map { ln(it) } - override fun sin(arg: StructureND): NDBuffer = arg.map { sin(it) } - override fun cos(arg: StructureND): NDBuffer = arg.map { cos(it) } - override fun tan(arg: StructureND): NDBuffer = arg.map { tan(it) } - override fun asin(arg: StructureND): NDBuffer = arg.map { asin(it) } - override fun acos(arg: StructureND): NDBuffer = arg.map { acos(it) } - override fun atan(arg: StructureND): NDBuffer = arg.map { atan(it) } + override fun sin(arg: StructureND): BufferND = arg.map { sin(it) } + override fun cos(arg: StructureND): BufferND = arg.map { cos(it) } + override fun tan(arg: StructureND): BufferND = arg.map { tan(it) } + override fun asin(arg: StructureND): BufferND = arg.map { asin(it) } + override fun acos(arg: StructureND): BufferND = arg.map { acos(it) } + override fun atan(arg: StructureND): BufferND = arg.map { atan(it) } - override fun sinh(arg: StructureND): NDBuffer = arg.map { sinh(it) } - override fun cosh(arg: StructureND): NDBuffer = arg.map { cosh(it) } - override fun tanh(arg: StructureND): NDBuffer = arg.map { tanh(it) } - override fun asinh(arg: StructureND): NDBuffer = arg.map { asinh(it) } - override fun acosh(arg: StructureND): NDBuffer = arg.map { acosh(it) } - override fun atanh(arg: StructureND): NDBuffer = arg.map { atanh(it) } + override fun sinh(arg: StructureND): BufferND = arg.map { sinh(it) } + override fun cosh(arg: StructureND): BufferND = arg.map { cosh(it) } + override fun tanh(arg: StructureND): BufferND = arg.map { tanh(it) } + override fun asinh(arg: StructureND): BufferND = arg.map { asinh(it) } + override fun acosh(arg: StructureND): BufferND = arg.map { acosh(it) } + override fun atanh(arg: StructureND): BufferND = arg.map { atanh(it) } } public fun AlgebraND.Companion.real(vararg shape: Int): DoubleFieldND = DoubleFieldND(shape) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt index 4e39dd544..f1056d0ff 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt @@ -14,10 +14,10 @@ public class ShortRingND( ) : BufferedRingND(shape, ShortRing, Buffer.Companion::auto), NumbersAddOperations> { - override val zero: NDBuffer by lazy { produce { zero } } - override val one: NDBuffer by lazy { produce { one } } + override val zero: BufferND by lazy { produce { zero } } + override val one: BufferND by lazy { produce { one } } - override fun number(value: Number): NDBuffer { + override fun number(value: Number): BufferND { val d = value.toShort() // minimize conversions return produce { d } } @@ -26,8 +26,8 @@ public class ShortRingND( /** * Fast element production using function inlining. */ -public inline fun BufferedRingND.produceInline(crossinline initializer: ShortRing.(Int) -> Short): NDBuffer { - return NDBuffer(strides, ShortBuffer(ShortArray(strides.linearSize) { offset -> ShortRing.initializer(offset) })) +public inline fun BufferedRingND.produceInline(crossinline initializer: ShortRing.(Int) -> Short): BufferND { + return BufferND(strides, ShortBuffer(ShortArray(strides.linearSize) { offset -> ShortRing.initializer(offset) })) } public inline fun ShortRing.nd(vararg shape: Int, action: ShortRingND.() -> R): R { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt index eba51a980..8cf5d18cb 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt @@ -47,7 +47,7 @@ private inline class Buffer1DWrapper(val buffer: Buffer) : Structure1D */ public fun StructureND.as1D(): Structure1D = this as? Structure1D ?: if (shape.size == 1) { when (this) { - is NDBuffer -> Buffer1DWrapper(this.buffer) + is BufferND -> Buffer1DWrapper(this.buffer) else -> Structure1DWrapper(this) } } else error("Can't create 1d-structure from ${shape.size}d-structure") @@ -62,6 +62,6 @@ public fun Buffer.asND(): Structure1D = Buffer1DWrapper(this) */ internal fun Structure1D.unwrap(): Buffer = when { this is Buffer1DWrapper -> buffer - this is Structure1DWrapper && structure is NDBuffer -> structure.buffer + this is Structure1DWrapper && structure is BufferND -> structure.buffer else -> this } 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 1c3b0fec8..3834cd97c 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 @@ -60,7 +60,7 @@ public interface Structure2D : StructureND { /** * A 2D wrapper for nd-structure */ -private class Structure2DWrapper(val structure: StructureND) : Structure2D { +private inline class Structure2DWrapper(val structure: StructureND) : Structure2D { override val shape: IntArray get() = structure.shape override val rowNum: Int get() = shape[0] @@ -72,10 +72,6 @@ private class Structure2DWrapper(val structure: StructureND) : Structure2D override fun getFeature(type: KClass): F? = structure.getFeature(type) override fun elements(): Sequence> = structure.elements() - - override fun equals(other: Any?): Boolean = structure == other - - override fun hashCode(): Int = structure.hashCode() } /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt index 54253ba9e..bb7814bfd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt @@ -14,6 +14,8 @@ import kotlin.reflect.KClass * of dimensions and items in an array is defined by its shape, which is a sequence of non-negative integers that * specify the sizes of each dimension. * + * StructureND is in general identity-free. [StructureND.contentEquals] should be used in tests to compare contents. + * * @param T the type of items. */ public interface StructureND { @@ -43,10 +45,6 @@ public interface StructureND { */ public fun elements(): Sequence> - //force override equality and hash code - public override fun equals(other: Any?): Boolean - public override fun hashCode(): Int - /** * Feature is some additional strucure information which allows to access it special properties or hints. * If the feature is not present, null is returned. @@ -62,7 +60,7 @@ public interface StructureND { if (st1 === st2) return true // fast comparison of buffers if possible - if (st1 is NDBuffer && st2 is NDBuffer && st1.strides == st2.strides) + if (st1 is BufferND && st2 is BufferND && st1.strides == st2.strides) return st1.buffer.contentEquals(st2.buffer) //element by element comparison if it could not be avoided @@ -78,7 +76,7 @@ public interface StructureND { strides: Strides, bufferFactory: BufferFactory = Buffer.Companion::boxing, initializer: (IntArray) -> T, - ): NDBuffer = NDBuffer(strides, bufferFactory(strides.linearSize) { i -> initializer(strides.index(i)) }) + ): BufferND = BufferND(strides, bufferFactory(strides.linearSize) { i -> initializer(strides.index(i)) }) /** * Inline create NDStructure with non-boxing buffer implementation if it is possible @@ -86,37 +84,37 @@ public interface StructureND { public inline fun auto( strides: Strides, crossinline initializer: (IntArray) -> T, - ): NDBuffer = NDBuffer(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) }) + ): BufferND = BufferND(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) }) public inline fun auto( type: KClass, strides: Strides, crossinline initializer: (IntArray) -> T, - ): NDBuffer = NDBuffer(strides, Buffer.auto(type, strides.linearSize) { i -> initializer(strides.index(i)) }) + ): BufferND = BufferND(strides, Buffer.auto(type, strides.linearSize) { i -> initializer(strides.index(i)) }) public fun buffered( shape: IntArray, bufferFactory: BufferFactory = Buffer.Companion::boxing, initializer: (IntArray) -> T, - ): NDBuffer = buffered(DefaultStrides(shape), bufferFactory, initializer) + ): BufferND = buffered(DefaultStrides(shape), bufferFactory, initializer) public inline fun auto( shape: IntArray, crossinline initializer: (IntArray) -> T, - ): NDBuffer = auto(DefaultStrides(shape), initializer) + ): BufferND = auto(DefaultStrides(shape), initializer) @JvmName("autoVarArg") public inline fun auto( vararg shape: Int, crossinline initializer: (IntArray) -> T, - ): NDBuffer = + ): BufferND = auto(DefaultStrides(shape), initializer) public inline fun auto( type: KClass, vararg shape: Int, crossinline initializer: (IntArray) -> T, - ): NDBuffer = auto(type, DefaultStrides(shape), initializer) + ): BufferND = auto(type, DefaultStrides(shape), initializer) } } @@ -134,7 +132,7 @@ public inline fun StructureND<*>.getFeature(): T? = getFeature /** * Represents mutable [StructureND]. */ -public interface MutableNDStructure : StructureND { +public interface MutableStructureND : StructureND { /** * Inserts an item at the specified indices. * @@ -147,7 +145,7 @@ public interface MutableNDStructure : StructureND { /** * Transform a structure element-by element in place. */ -public inline fun MutableNDStructure.mapInPlace(action: (IntArray, T) -> T): Unit = +public inline fun MutableStructureND.mapInPlace(action: (IntArray, T) -> T): Unit = elements().forEach { (index, oldValue) -> this[index] = action(index, oldValue) } /** @@ -258,7 +256,7 @@ public class DefaultStrides private constructor(override val shape: IntArray) : * @param strides The strides to access elements of [Buffer] by linear indices. * @param buffer The underlying buffer. */ -public open class NDBuffer( +public open class BufferND( public val strides: Strides, buffer: Buffer, ) : StructureND { @@ -279,16 +277,6 @@ public open class NDBuffer( it to this[it] } - override fun equals(other: Any?): Boolean { - return StructureND.contentEquals(this, other as? StructureND<*> ?: return false) - } - - override fun hashCode(): Int { - var result = strides.hashCode() - result = 31 * result + buffer.hashCode() - return result - } - override fun toString(): String { val bufferRepr: String = when (shape.size) { 1 -> buffer.asSequence().joinToString(prefix = "[", postfix = "]", separator = ", ") @@ -305,27 +293,27 @@ public open class NDBuffer( } /** - * Transform structure to a new structure using provided [BufferFactory] and optimizing if argument is [NDBuffer] + * Transform structure to a new structure using provided [BufferFactory] and optimizing if argument is [BufferND] */ public inline fun StructureND.mapToBuffer( factory: BufferFactory = Buffer.Companion::auto, crossinline transform: (T) -> R, -): NDBuffer { - return if (this is NDBuffer) - NDBuffer(this.strides, factory.invoke(strides.linearSize) { transform(buffer[it]) }) +): BufferND { + return if (this is BufferND) + BufferND(this.strides, factory.invoke(strides.linearSize) { transform(buffer[it]) }) else { val strides = DefaultStrides(shape) - NDBuffer(strides, factory.invoke(strides.linearSize) { transform(get(strides.index(it))) }) + BufferND(strides, factory.invoke(strides.linearSize) { transform(get(strides.index(it))) }) } } /** * Mutable ND buffer based on linear [MutableBuffer]. */ -public class MutableNDBuffer( +public class MutableBufferND( strides: Strides, buffer: MutableBuffer, -) : NDBuffer(strides, buffer), MutableNDStructure { +) : BufferND(strides, buffer), MutableStructureND { init { require(strides.linearSize == buffer.size) { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt index 1c64bfcec..d373be35e 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt @@ -17,7 +17,9 @@ public typealias BufferFactory = (Int, (Int) -> T) -> Buffer public typealias MutableBufferFactory = (Int, (Int) -> T) -> MutableBuffer /** - * A generic immutable random-access structure for both primitives and objects. + * A generic read-only random-access structure for both primitives and objects. + * + * [Buffer] is in general identity-free. [contentEquals] should be used for content equality checks * * @param T the type of elements contained in the buffer. */ @@ -40,8 +42,13 @@ public interface Buffer { /** * Checks content equality with another buffer. */ - public fun contentEquals(other: Buffer<*>): Boolean = - asSequence().mapIndexed { index, value -> value == other[index] }.all { it } + public fun contentEquals(other: Buffer<*>): Boolean { + if (this.size != other.size) return false + for (i in indices) { + if (get(i) != other[i]) return false + } + return true + } public companion object { /** diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt index 6761575f6..445aadd97 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/DoubleLUSolverTest.kt @@ -1,8 +1,14 @@ package space.kscience.kmath.linear import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.nd.StructureND import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue + +fun assertMatrixEquals(expected: StructureND, actual: StructureND) { + assertTrue { StructureND.contentEquals(expected, actual) } +} @UnstableKMathAPI class DoubleLUSolverTest { @@ -11,7 +17,7 @@ class DoubleLUSolverTest { fun testInvertOne() { val matrix = LinearSpace.real.one(2, 2) val inverted = LinearSpace.real.inverseWithLup(matrix) - assertEquals(matrix, inverted) + assertMatrixEquals(matrix, inverted) } @Test @@ -27,7 +33,7 @@ class DoubleLUSolverTest { //Check determinant assertEquals(7.0, lup.determinant) - assertEquals(lup.p dot matrix, lup.l dot lup.u) + assertMatrixEquals(lup.p dot matrix, lup.l dot lup.u) } } @@ -45,6 +51,6 @@ class DoubleLUSolverTest { -0.125, 0.375 ) - assertEquals(expected, inverted) + assertMatrixEquals(expected, inverted) } } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt index a8a2f2586..fd6fb320b 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt @@ -5,6 +5,7 @@ import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.as2D import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue @UnstableKMathAPI @Suppress("UNUSED_VARIABLE") @@ -13,7 +14,7 @@ class MatrixTest { fun testTranspose() { val matrix = LinearSpace.real.one(3, 3) val transposed = matrix.transpose() - assertEquals(matrix, transposed) + assertTrue { StructureND.contentEquals(matrix, transposed) } } @Test diff --git a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt index 261f91aee..6b0938c54 100644 --- a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt +++ b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt @@ -24,18 +24,6 @@ public class LazyStructureND( val res = runBlocking { strides.indices().toList().map { index -> index to await(index) } } return res.asSequence() } - - public override fun equals(other: Any?): Boolean { - return StructureND.contentEquals(this, other as? StructureND<*> ?: return false) - } - - public override fun hashCode(): Int { - var result = scope.hashCode() - result = 31 * result + shape.contentHashCode() - result = 31 * result + function.hashCode() - result = 31 * result + cache.hashCode() - return result - } } public fun StructureND.deferred(index: IntArray): Deferred = diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt index c79493411..712c2a42c 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt @@ -2,7 +2,6 @@ package space.kscience.kmath.ejml import org.ejml.simple.SimpleMatrix import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.nd.StructureND /** * Represents featured matrix over EJML [SimpleMatrix]. @@ -10,19 +9,9 @@ import space.kscience.kmath.nd.StructureND * @property origin the underlying [SimpleMatrix]. * @author Iaroslav Postovalov */ -public class EjmlMatrix(public val origin: SimpleMatrix) : Matrix { +public inline class EjmlMatrix(public val origin: SimpleMatrix) : Matrix { public override val rowNum: Int get() = origin.numRows() public override val colNum: Int get() = origin.numCols() public override operator fun get(i: Int, j: Int): Double = origin[i, j] - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is StructureND<*>) return false - return StructureND.contentEquals(this, other) - } - - override fun hashCode(): Int = origin.hashCode() - - } diff --git a/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt b/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt index 70b2ce723..5fb526f40 100644 --- a/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt +++ b/kmath-ejml/src/test/kotlin/space/kscience/kmath/ejml/EjmlMatrixTest.kt @@ -4,11 +4,16 @@ import org.ejml.dense.row.factory.DecompositionFactory_DDRM import org.ejml.simple.SimpleMatrix import space.kscience.kmath.linear.* import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.getFeature import kotlin.random.Random import kotlin.random.asJavaRandom import kotlin.test.* +fun assertMatrixEquals(expected: StructureND, actual: StructureND) { + assertTrue { StructureND.contentEquals(expected, actual) } +} + internal class EjmlMatrixTest { private val random = Random(0) @@ -49,9 +54,9 @@ internal class EjmlMatrixTest { val ludecompositionF64 = DecompositionFactory_DDRM.lu(m.numRows(), m.numCols()) .also { it.decompose(m.ddrm.copy()) } - assertEquals(EjmlMatrix(SimpleMatrix(ludecompositionF64.getLower(null))), lup.l) - assertEquals(EjmlMatrix(SimpleMatrix(ludecompositionF64.getUpper(null))), lup.u) - assertEquals(EjmlMatrix(SimpleMatrix(ludecompositionF64.getRowPivot(null))), lup.p) + assertMatrixEquals(EjmlMatrix(SimpleMatrix(ludecompositionF64.getLower(null))), lup.l) + assertMatrixEquals(EjmlMatrix(SimpleMatrix(ludecompositionF64.getUpper(null))), lup.u) + assertMatrixEquals(EjmlMatrix(SimpleMatrix(ludecompositionF64.getRowPivot(null))), lup.p) } private object SomeFeature : MatrixFeature {} diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt index 7655a4170..95faec4b3 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt @@ -1,31 +1,31 @@ package space.kscience.kmath.real -import space.kscience.kmath.nd.NDBuffer +import space.kscience.kmath.nd.BufferND import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.structures.DoubleBuffer /** - * Map one [NDBuffer] using function without indices. + * Map one [BufferND] using function without indices. */ -public inline fun NDBuffer.mapInline(crossinline transform: DoubleField.(Double) -> Double): NDBuffer { +public inline fun BufferND.mapInline(crossinline transform: DoubleField.(Double) -> Double): BufferND { val array = DoubleArray(strides.linearSize) { offset -> DoubleField.transform(buffer[offset]) } - return NDBuffer(strides, DoubleBuffer(array)) + return BufferND(strides, DoubleBuffer(array)) } /** * Element by element application of any operation on elements to the whole array. Just like in numpy. */ -public operator fun Function1.invoke(ndElement: NDBuffer): NDBuffer = - ndElement.mapInline { this@invoke(it) } +public operator fun Function1.invoke(elementND: BufferND): BufferND = + elementND.mapInline { this@invoke(it) } /* plus and minus */ /** - * Summation operation for [NDBuffer] and single element + * Summation operation for [BufferND] and single element */ -public operator fun NDBuffer.plus(arg: Double): NDBuffer = mapInline { it + arg } +public operator fun BufferND.plus(arg: Double): BufferND = mapInline { it + arg } /** - * Subtraction operation between [NDBuffer] and single element + * Subtraction operation between [BufferND] and single element */ -public operator fun NDBuffer.minus(arg: Double): NDBuffer = mapInline { it - arg } \ No newline at end of file +public operator fun BufferND.minus(arg: Double): BufferND = mapInline { it - arg } \ No newline at end of file diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/DoubleMatrixTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/DoubleMatrixTest.kt index c1d3ccf5f..1c47d803a 100644 --- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/DoubleMatrixTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/DoubleMatrixTest.kt @@ -3,12 +3,17 @@ package kaceince.kmath.real import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.matrix import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.nd.StructureND import space.kscience.kmath.real.* import space.kscience.kmath.structures.contentEquals import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue +fun assertMatrixEquals(expected: StructureND, actual: StructureND) { + assertTrue { StructureND.contentEquals(expected, actual) } +} + @UnstableKMathAPI internal class DoubleMatrixTest { @Test @@ -43,7 +48,7 @@ internal class DoubleMatrixTest { 1.0, 0.0, 0.0, 0.0, 1.0, 2.0 ) - assertEquals(matrix2, matrix1.repeatStackVertical(3)) + assertMatrixEquals(matrix2, matrix1.repeatStackVertical(3)) } @Test @@ -57,7 +62,7 @@ internal class DoubleMatrixTest { 0.75, -0.5, 3.25, 4.5, 7.0, 2.0 ) - assertEquals(matrix2, expectedResult) + assertMatrixEquals(matrix2, expectedResult) } @Test @@ -72,7 +77,7 @@ internal class DoubleMatrixTest { 5.0, 10.0, -5.0, -10.0, -20.0, 0.0 ) - assertEquals(matrix2, expectedResult) + assertMatrixEquals(matrix2, expectedResult) } @Test @@ -89,8 +94,8 @@ internal class DoubleMatrixTest { -1.0, 0.0, 27.0, 64.0, -216.0, -8.0 ) - assertEquals(matrix1.square(), matrix2) - assertEquals(matrix1.pow(3), matrix3) + assertMatrixEquals(matrix1.square(), matrix2) + assertMatrixEquals(matrix1.pow(3), matrix3) } @OptIn(UnstableKMathAPI::class) @@ -109,7 +114,7 @@ internal class DoubleMatrixTest { -3.0, 0.0, 9.0, 16.0, -48.0, -5.0 ) - assertEquals(result, expectedResult) + assertMatrixEquals(result, expectedResult) } @Test @@ -128,9 +133,9 @@ internal class DoubleMatrixTest { -6.0, 7.0 ) - assertEquals(matrix1.appendColumn { it[0] }, matrix2) - assertEquals(matrix1.extractColumn(1), col1) - assertEquals(matrix1.extractColumns(1..2), cols1to2) + assertMatrixEquals(matrix1.appendColumn { it[0] }, matrix2) + assertMatrixEquals(matrix1.extractColumn(1), col1) + assertMatrixEquals(matrix1.extractColumns(1..2), cols1to2) //equals should never be called on buffers assertTrue { matrix1.sumByColumn().contentEquals(3.0, -6.0, 10.0, 4.0) diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt index fa9b67404..384ce6159 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt @@ -75,7 +75,7 @@ public class DoubleHistogramSpace( ndCounter[index].add(value.toDouble()) } hBuilder.apply(builder) - val values: NDBuffer = ndCounter.mapToBuffer { it.value } + val values: BufferND = ndCounter.mapToBuffer { it.value } return IndexedHistogram(this, values) } diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt index ec4cc9c56..6a3ae37a7 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayStructure.kt @@ -1,7 +1,7 @@ package space.kscience.kmath.nd4j import org.nd4j.linalg.api.ndarray.INDArray -import space.kscience.kmath.nd.MutableNDStructure +import space.kscience.kmath.nd.MutableStructureND import space.kscience.kmath.nd.StructureND /** @@ -9,7 +9,7 @@ import space.kscience.kmath.nd.StructureND * * @param T the type of items. */ -public sealed class Nd4jArrayStructure : MutableNDStructure { +public sealed class Nd4jArrayStructure : MutableStructureND { /** * The wrapped [INDArray]. */ diff --git a/kmath-viktor/api/kmath-viktor.api b/kmath-viktor/api/kmath-viktor.api index 6fe6c9db7..d322ddd71 100644 --- a/kmath-viktor/api/kmath-viktor.api +++ b/kmath-viktor/api/kmath-viktor.api @@ -148,7 +148,7 @@ public final class space/kscience/kmath/viktor/ViktorFieldND : space/kscience/km public fun unaryPlus (Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/nd/StructureND; } -public final class space/kscience/kmath/viktor/ViktorStructureND : space/kscience/kmath/nd/MutableNDStructure { +public final class space/kscience/kmath/viktor/ViktorStructureND : space/kscience/kmath/nd/MutableStructureND { public static final synthetic fun box-impl (Lorg/jetbrains/bio/viktor/F64Array;)Lspace/kscience/kmath/viktor/ViktorStructureND; public static fun constructor-impl (Lorg/jetbrains/bio/viktor/F64Array;)Lorg/jetbrains/bio/viktor/F64Array; public fun elements ()Lkotlin/sequences/Sequence; diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt index 2749f5431..420bcac90 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorStructureND.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.operations.NumbersAddOperations import space.kscience.kmath.operations.ScaleOperations @Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -public inline class ViktorStructureND(public val f64Buffer: F64Array) : MutableNDStructure { +public inline class ViktorStructureND(public val f64Buffer: F64Array) : MutableStructureND { public override val shape: IntArray get() = f64Buffer.shape public override inline fun get(index: IntArray): Double = f64Buffer.get(*index)