Remove StructureND identity #248

This commit is contained in:
Alexander Nozik 2021-03-16 21:43:29 +03:00
parent 206e4cbcf6
commit a3ca06a241
27 changed files with 239 additions and 285 deletions

View File

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

View File

@ -20,10 +20,10 @@ class StreamDoubleFieldND(
private val strides = DefaultStrides(shape)
override val elementContext: DoubleField get() = DoubleField
override val zero: NDBuffer<Double> by lazy { produce { zero } }
override val one: NDBuffer<Double> by lazy { produce { one } }
override val zero: BufferND<Double> by lazy { produce { zero } }
override val one: BufferND<Double> by lazy { produce { one } }
override fun number(value: Number): NDBuffer<Double> {
override fun number(value: Number): BufferND<Double> {
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<Double> {
override fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND<Double> {
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<Double>.map(
transform: DoubleField.(Double) -> Double,
): NDBuffer<Double> {
): BufferND<Double> {
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<Double>.mapIndexed(
transform: DoubleField.(index: IntArray, Double) -> Double,
): NDBuffer<Double> {
): BufferND<Double> {
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<Double>,
b: StructureND<Double>,
transform: DoubleField.(Double, Double) -> Double,
): NDBuffer<Double> {
): BufferND<Double> {
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<Double>.unaryMinus(): StructureND<Double> = map { -it }
override fun scale(a: StructureND<Double>, value: Double): StructureND<Double> = a.map { it * value }
override fun power(arg: StructureND<Double>, pow: Number): NDBuffer<Double> = arg.map { power(it, pow) }
override fun power(arg: StructureND<Double>, pow: Number): BufferND<Double> = arg.map { power(it, pow) }
override fun exp(arg: StructureND<Double>): NDBuffer<Double> = arg.map { exp(it) }
override fun exp(arg: StructureND<Double>): BufferND<Double> = arg.map { exp(it) }
override fun ln(arg: StructureND<Double>): NDBuffer<Double> = arg.map { ln(it) }
override fun ln(arg: StructureND<Double>): BufferND<Double> = arg.map { ln(it) }
override fun sin(arg: StructureND<Double>): NDBuffer<Double> = arg.map { sin(it) }
override fun cos(arg: StructureND<Double>): NDBuffer<Double> = arg.map { cos(it) }
override fun tan(arg: StructureND<Double>): NDBuffer<Double> = arg.map { tan(it) }
override fun asin(arg: StructureND<Double>): NDBuffer<Double> = arg.map { asin(it) }
override fun acos(arg: StructureND<Double>): NDBuffer<Double> = arg.map { acos(it) }
override fun atan(arg: StructureND<Double>): NDBuffer<Double> = arg.map { atan(it) }
override fun sin(arg: StructureND<Double>): BufferND<Double> = arg.map { sin(it) }
override fun cos(arg: StructureND<Double>): BufferND<Double> = arg.map { cos(it) }
override fun tan(arg: StructureND<Double>): BufferND<Double> = arg.map { tan(it) }
override fun asin(arg: StructureND<Double>): BufferND<Double> = arg.map { asin(it) }
override fun acos(arg: StructureND<Double>): BufferND<Double> = arg.map { acos(it) }
override fun atan(arg: StructureND<Double>): BufferND<Double> = arg.map { atan(it) }
override fun sinh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { sinh(it) }
override fun cosh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { cosh(it) }
override fun tanh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { tanh(it) }
override fun asinh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { asinh(it) }
override fun acosh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { acosh(it) }
override fun atanh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { atanh(it) }
override fun sinh(arg: StructureND<Double>): BufferND<Double> = arg.map { sinh(it) }
override fun cosh(arg: StructureND<Double>): BufferND<Double> = arg.map { cosh(it) }
override fun tanh(arg: StructureND<Double>): BufferND<Double> = arg.map { tanh(it) }
override fun asinh(arg: StructureND<Double>): BufferND<Double> = arg.map { asinh(it) }
override fun acosh(arg: StructureND<Double>): BufferND<Double> = arg.map { acosh(it) }
override fun atanh(arg: StructureND<Double>): BufferND<Double> = arg.map { atanh(it) }
}
fun AlgebraND.Companion.realWithStream(vararg shape: Int): StreamDoubleFieldND = StreamDoubleFieldND(shape)

View File

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

View File

@ -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<Double> {
public inline class CMMatrix(public val origin: RealMatrix) : Matrix<Double> {
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<Double> {

View File

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

View File

@ -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<StructureND<Complex>>,
ExtendedField<StructureND<Complex>> {
override val zero: NDBuffer<Complex> by lazy { produce { zero } }
override val one: NDBuffer<Complex> by lazy { produce { one } }
override val zero: BufferND<Complex> by lazy { produce { zero } }
override val one: BufferND<Complex> by lazy { produce { one } }
override fun number(value: Number): NDBuffer<Complex> {
override fun number(value: Number): BufferND<Complex> {
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<Complex>, pow: Number): NDBuffer<Complex> = arg.map { power(it, pow) }
override fun power(arg: StructureND<Complex>, pow: Number): BufferND<Complex> = arg.map { power(it, pow) }
override fun exp(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { exp(it) }
override fun exp(arg: StructureND<Complex>): BufferND<Complex> = arg.map { exp(it) }
override fun ln(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { ln(it) }
override fun ln(arg: StructureND<Complex>): BufferND<Complex> = arg.map { ln(it) }
override fun sin(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { sin(it) }
override fun cos(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { cos(it) }
override fun tan(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { tan(it) }
override fun asin(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { asin(it) }
override fun acos(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { acos(it) }
override fun atan(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { atan(it) }
override fun sin(arg: StructureND<Complex>): BufferND<Complex> = arg.map { sin(it) }
override fun cos(arg: StructureND<Complex>): BufferND<Complex> = arg.map { cos(it) }
override fun tan(arg: StructureND<Complex>): BufferND<Complex> = arg.map { tan(it) }
override fun asin(arg: StructureND<Complex>): BufferND<Complex> = arg.map { asin(it) }
override fun acos(arg: StructureND<Complex>): BufferND<Complex> = arg.map { acos(it) }
override fun atan(arg: StructureND<Complex>): BufferND<Complex> = arg.map { atan(it) }
override fun sinh(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { sinh(it) }
override fun cosh(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { cosh(it) }
override fun tanh(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { tanh(it) }
override fun asinh(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { asinh(it) }
override fun acosh(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { acosh(it) }
override fun atanh(arg: StructureND<Complex>): NDBuffer<Complex> = arg.map { atanh(it) }
override fun sinh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { sinh(it) }
override fun cosh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { cosh(it) }
override fun tanh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { tanh(it) }
override fun asinh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { asinh(it) }
override fun acosh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { acosh(it) }
override fun atanh(arg: StructureND<Complex>): BufferND<Complex> = arg.map { atanh(it) }
}
/**
* Fast element production using function inlining
*/
public inline fun BufferedFieldND<Complex, ComplexField>.produceInline(initializer: ComplexField.(Int) -> Complex): NDBuffer<Complex> {
public inline fun BufferedFieldND<Complex, ComplexField>.produceInline(initializer: ComplexField.(Int) -> Complex): BufferND<Complex> {
contract { callsInPlace(initializer, InvocationKind.EXACTLY_ONCE) }
val buffer = Buffer.complex(strides.linearSize) { offset -> ComplexField.initializer(offset) }
return NDBuffer(strides, buffer)
return BufferND(strides, buffer)
}

View File

@ -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 <init> (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 <init> (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 <init> ([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 <init> ([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 <init> ([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 <init> (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 <init> (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 <init> ([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 {

View File

@ -23,8 +23,6 @@ public class MatrixWrapper<T : Any> internal constructor(
override fun <T : Any> getFeature(type: KClass<T>): 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)"
}

View File

@ -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<T : Any>(
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
}
}

View File

@ -10,7 +10,7 @@ public interface BufferAlgebraND<T, A : Algebra<T>> : AlgebraND<T, A> {
public val strides: Strides
public val bufferFactory: BufferFactory<T>
override fun produce(initializer: A.(IntArray) -> T): NDBuffer<T> = NDBuffer(
override fun produce(initializer: A.(IntArray) -> T): BufferND<T> = BufferND(
strides,
bufferFactory(strides.linearSize) { offset ->
elementContext.initializer(strides.index(offset))
@ -23,32 +23,32 @@ public interface BufferAlgebraND<T, A : Algebra<T>> : AlgebraND<T, A> {
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<T>.map(transform: A.(T) -> T): NDBuffer<T> {
override fun StructureND<T>.map(transform: A.(T) -> T): BufferND<T> {
val buffer = bufferFactory(strides.linearSize) { offset ->
elementContext.transform(buffer[offset])
}
return NDBuffer(strides, buffer)
return BufferND(strides, buffer)
}
override fun StructureND<T>.mapIndexed(transform: A.(index: IntArray, T) -> T): NDBuffer<T> {
override fun StructureND<T>.mapIndexed(transform: A.(index: IntArray, T) -> T): BufferND<T> {
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<T>, b: StructureND<T>, transform: A.(T, T) -> T): NDBuffer<T> {
override fun combine(a: StructureND<T>, b: StructureND<T>, transform: A.(T, T) -> T): BufferND<T> {
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<T, A : Group<T>>(
final override val bufferFactory: BufferFactory<T>,
) : GroupND<T, A>, BufferAlgebraND<T, A> {
override val strides: Strides = DefaultStrides(shape)
override val zero: NDBuffer<T> by lazy { produce { zero } }
override val zero: BufferND<T> by lazy { produce { zero } }
override fun StructureND<T>.unaryMinus(): StructureND<T> = produce { -get(it) }
}
@ -67,7 +67,7 @@ public open class BufferedRingND<T, R : Ring<T>>(
elementContext: R,
bufferFactory: BufferFactory<T>,
) : BufferedGroupND<T, R>(shape, elementContext, bufferFactory), RingND<T, R> {
override val one: NDBuffer<T> by lazy { produce { one } }
override val one: BufferND<T> by lazy { produce { one } }
}
public open class BufferedFieldND<T, R : Field<T>>(

View File

@ -17,10 +17,10 @@ public class DoubleFieldND(
ScaleOperations<StructureND<Double>>,
ExtendedField<StructureND<Double>> {
override val zero: NDBuffer<Double> by lazy { produce { zero } }
override val one: NDBuffer<Double> by lazy { produce { one } }
override val zero: BufferND<Double> by lazy { produce { zero } }
override val one: BufferND<Double> by lazy { produce { one } }
override fun number(value: Number): NDBuffer<Double> {
override fun number(value: Number): BufferND<Double> {
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<Double>.map(
transform: DoubleField.(Double) -> Double,
): NDBuffer<Double> {
): BufferND<Double> {
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<Double> {
override inline fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND<Double> {
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<Double>.mapIndexed(
transform: DoubleField.(index: IntArray, Double) -> Double,
): NDBuffer<Double> = NDBuffer(
): BufferND<Double> = BufferND(
strides,
buffer = DoubleBuffer(strides.linearSize) { offset ->
DoubleField.transform(
@ -69,34 +69,34 @@ public class DoubleFieldND(
a: StructureND<Double>,
b: StructureND<Double>,
transform: DoubleField.(Double, Double) -> Double,
): NDBuffer<Double> {
): BufferND<Double> {
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<Double>, value: Double): StructureND<Double> = a.map { it * value }
override fun power(arg: StructureND<Double>, pow: Number): NDBuffer<Double> = arg.map { power(it, pow) }
override fun power(arg: StructureND<Double>, pow: Number): BufferND<Double> = arg.map { power(it, pow) }
override fun exp(arg: StructureND<Double>): NDBuffer<Double> = arg.map { exp(it) }
override fun exp(arg: StructureND<Double>): BufferND<Double> = arg.map { exp(it) }
override fun ln(arg: StructureND<Double>): NDBuffer<Double> = arg.map { ln(it) }
override fun ln(arg: StructureND<Double>): BufferND<Double> = arg.map { ln(it) }
override fun sin(arg: StructureND<Double>): NDBuffer<Double> = arg.map { sin(it) }
override fun cos(arg: StructureND<Double>): NDBuffer<Double> = arg.map { cos(it) }
override fun tan(arg: StructureND<Double>): NDBuffer<Double> = arg.map { tan(it) }
override fun asin(arg: StructureND<Double>): NDBuffer<Double> = arg.map { asin(it) }
override fun acos(arg: StructureND<Double>): NDBuffer<Double> = arg.map { acos(it) }
override fun atan(arg: StructureND<Double>): NDBuffer<Double> = arg.map { atan(it) }
override fun sin(arg: StructureND<Double>): BufferND<Double> = arg.map { sin(it) }
override fun cos(arg: StructureND<Double>): BufferND<Double> = arg.map { cos(it) }
override fun tan(arg: StructureND<Double>): BufferND<Double> = arg.map { tan(it) }
override fun asin(arg: StructureND<Double>): BufferND<Double> = arg.map { asin(it) }
override fun acos(arg: StructureND<Double>): BufferND<Double> = arg.map { acos(it) }
override fun atan(arg: StructureND<Double>): BufferND<Double> = arg.map { atan(it) }
override fun sinh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { sinh(it) }
override fun cosh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { cosh(it) }
override fun tanh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { tanh(it) }
override fun asinh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { asinh(it) }
override fun acosh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { acosh(it) }
override fun atanh(arg: StructureND<Double>): NDBuffer<Double> = arg.map { atanh(it) }
override fun sinh(arg: StructureND<Double>): BufferND<Double> = arg.map { sinh(it) }
override fun cosh(arg: StructureND<Double>): BufferND<Double> = arg.map { cosh(it) }
override fun tanh(arg: StructureND<Double>): BufferND<Double> = arg.map { tanh(it) }
override fun asinh(arg: StructureND<Double>): BufferND<Double> = arg.map { asinh(it) }
override fun acosh(arg: StructureND<Double>): BufferND<Double> = arg.map { acosh(it) }
override fun atanh(arg: StructureND<Double>): BufferND<Double> = arg.map { atanh(it) }
}
public fun AlgebraND.Companion.real(vararg shape: Int): DoubleFieldND = DoubleFieldND(shape)

View File

@ -14,10 +14,10 @@ public class ShortRingND(
) : BufferedRingND<Short, ShortRing>(shape, ShortRing, Buffer.Companion::auto),
NumbersAddOperations<StructureND<Short>> {
override val zero: NDBuffer<Short> by lazy { produce { zero } }
override val one: NDBuffer<Short> by lazy { produce { one } }
override val zero: BufferND<Short> by lazy { produce { zero } }
override val one: BufferND<Short> by lazy { produce { one } }
override fun number(value: Number): NDBuffer<Short> {
override fun number(value: Number): BufferND<Short> {
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<Short, ShortRing>.produceInline(crossinline initializer: ShortRing.(Int) -> Short): NDBuffer<Short> {
return NDBuffer(strides, ShortBuffer(ShortArray(strides.linearSize) { offset -> ShortRing.initializer(offset) }))
public inline fun BufferedRingND<Short, ShortRing>.produceInline(crossinline initializer: ShortRing.(Int) -> Short): BufferND<Short> {
return BufferND(strides, ShortBuffer(ShortArray(strides.linearSize) { offset -> ShortRing.initializer(offset) }))
}
public inline fun <R> ShortRing.nd(vararg shape: Int, action: ShortRingND.() -> R): R {

View File

@ -47,7 +47,7 @@ private inline class Buffer1DWrapper<T>(val buffer: Buffer<T>) : Structure1D<T>
*/
public fun <T> StructureND<T>.as1D(): Structure1D<T> = this as? Structure1D<T> ?: 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 <T> Buffer<T>.asND(): Structure1D<T> = Buffer1DWrapper(this)
*/
internal fun <T : Any> Structure1D<T>.unwrap(): Buffer<T> = when {
this is Buffer1DWrapper<T> -> buffer
this is Structure1DWrapper && structure is NDBuffer<T> -> structure.buffer
this is Structure1DWrapper && structure is BufferND<T> -> structure.buffer
else -> this
}

View File

@ -60,7 +60,7 @@ public interface Structure2D<T> : StructureND<T> {
/**
* A 2D wrapper for nd-structure
*/
private class Structure2DWrapper<T>(val structure: StructureND<T>) : Structure2D<T> {
private inline class Structure2DWrapper<T>(val structure: StructureND<T>) : Structure2D<T> {
override val shape: IntArray get() = structure.shape
override val rowNum: Int get() = shape[0]
@ -72,10 +72,6 @@ private class Structure2DWrapper<T>(val structure: StructureND<T>) : Structure2D
override fun <F : Any> getFeature(type: KClass<F>): F? = structure.getFeature(type)
override fun elements(): Sequence<Pair<IntArray, T>> = structure.elements()
override fun equals(other: Any?): Boolean = structure == other
override fun hashCode(): Int = structure.hashCode()
}
/**

View File

@ -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<T> {
@ -43,10 +45,6 @@ public interface StructureND<T> {
*/
public fun elements(): Sequence<Pair<IntArray, T>>
//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<T> {
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<T> {
strides: Strides,
bufferFactory: BufferFactory<T> = Buffer.Companion::boxing,
initializer: (IntArray) -> T,
): NDBuffer<T> = NDBuffer(strides, bufferFactory(strides.linearSize) { i -> initializer(strides.index(i)) })
): BufferND<T> = 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<T> {
public inline fun <reified T : Any> auto(
strides: Strides,
crossinline initializer: (IntArray) -> T,
): NDBuffer<T> = NDBuffer(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) })
): BufferND<T> = BufferND(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) })
public inline fun <T : Any> auto(
type: KClass<T>,
strides: Strides,
crossinline initializer: (IntArray) -> T,
): NDBuffer<T> = NDBuffer(strides, Buffer.auto(type, strides.linearSize) { i -> initializer(strides.index(i)) })
): BufferND<T> = BufferND(strides, Buffer.auto(type, strides.linearSize) { i -> initializer(strides.index(i)) })
public fun <T> buffered(
shape: IntArray,
bufferFactory: BufferFactory<T> = Buffer.Companion::boxing,
initializer: (IntArray) -> T,
): NDBuffer<T> = buffered(DefaultStrides(shape), bufferFactory, initializer)
): BufferND<T> = buffered(DefaultStrides(shape), bufferFactory, initializer)
public inline fun <reified T : Any> auto(
shape: IntArray,
crossinline initializer: (IntArray) -> T,
): NDBuffer<T> = auto(DefaultStrides(shape), initializer)
): BufferND<T> = auto(DefaultStrides(shape), initializer)
@JvmName("autoVarArg")
public inline fun <reified T : Any> auto(
vararg shape: Int,
crossinline initializer: (IntArray) -> T,
): NDBuffer<T> =
): BufferND<T> =
auto(DefaultStrides(shape), initializer)
public inline fun <T : Any> auto(
type: KClass<T>,
vararg shape: Int,
crossinline initializer: (IntArray) -> T,
): NDBuffer<T> = auto(type, DefaultStrides(shape), initializer)
): BufferND<T> = auto(type, DefaultStrides(shape), initializer)
}
}
@ -134,7 +132,7 @@ public inline fun <reified T : Any> StructureND<*>.getFeature(): T? = getFeature
/**
* Represents mutable [StructureND].
*/
public interface MutableNDStructure<T> : StructureND<T> {
public interface MutableStructureND<T> : StructureND<T> {
/**
* Inserts an item at the specified indices.
*
@ -147,7 +145,7 @@ public interface MutableNDStructure<T> : StructureND<T> {
/**
* Transform a structure element-by element in place.
*/
public inline fun <T> MutableNDStructure<T>.mapInPlace(action: (IntArray, T) -> T): Unit =
public inline fun <T> MutableStructureND<T>.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<T>(
public open class BufferND<T>(
public val strides: Strides,
buffer: Buffer<T>,
) : StructureND<T> {
@ -279,16 +277,6 @@ public open class NDBuffer<T>(
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<T>(
}
/**
* 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 <T, reified R : Any> StructureND<T>.mapToBuffer(
factory: BufferFactory<R> = Buffer.Companion::auto,
crossinline transform: (T) -> R,
): NDBuffer<R> {
return if (this is NDBuffer<T>)
NDBuffer(this.strides, factory.invoke(strides.linearSize) { transform(buffer[it]) })
): BufferND<R> {
return if (this is BufferND<T>)
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<T>(
public class MutableBufferND<T>(
strides: Strides,
buffer: MutableBuffer<T>,
) : NDBuffer<T>(strides, buffer), MutableNDStructure<T> {
) : BufferND<T>(strides, buffer), MutableStructureND<T> {
init {
require(strides.linearSize == buffer.size) {

View File

@ -17,7 +17,9 @@ public typealias BufferFactory<T> = (Int, (Int) -> T) -> Buffer<T>
public typealias MutableBufferFactory<T> = (Int, (Int) -> T) -> MutableBuffer<T>
/**
* 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<out T> {
/**
* 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 {
/**

View File

@ -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 <T : Any> assertMatrixEquals(expected: StructureND<T>, actual: StructureND<T>) {
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)
}
}

View File

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

View File

@ -24,18 +24,6 @@ public class LazyStructureND<T>(
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 <T> StructureND<T>.deferred(index: IntArray): Deferred<T> =

View File

@ -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<Double> {
public inline class EjmlMatrix(public val origin: SimpleMatrix) : Matrix<Double> {
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()
}

View File

@ -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 <T : Any> assertMatrixEquals(expected: StructureND<T>, actual: StructureND<T>) {
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 {}

View File

@ -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<Double>.mapInline(crossinline transform: DoubleField.(Double) -> Double): NDBuffer<Double> {
public inline fun BufferND<Double>.mapInline(crossinline transform: DoubleField.(Double) -> Double): BufferND<Double> {
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<Double, Double>.invoke(ndElement: NDBuffer<Double>): NDBuffer<Double> =
ndElement.mapInline { this@invoke(it) }
public operator fun Function1<Double, Double>.invoke(elementND: BufferND<Double>): BufferND<Double> =
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<Double>.plus(arg: Double): NDBuffer<Double> = mapInline { it + arg }
public operator fun BufferND<Double>.plus(arg: Double): BufferND<Double> = mapInline { it + arg }
/**
* Subtraction operation between [NDBuffer] and single element
* Subtraction operation between [BufferND] and single element
*/
public operator fun NDBuffer<Double>.minus(arg: Double): NDBuffer<Double> = mapInline { it - arg }
public operator fun BufferND<Double>.minus(arg: Double): BufferND<Double> = mapInline { it - arg }

View File

@ -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 <T : Any> assertMatrixEquals(expected: StructureND<T>, actual: StructureND<T>) {
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)

View File

@ -75,7 +75,7 @@ public class DoubleHistogramSpace(
ndCounter[index].add(value.toDouble())
}
hBuilder.apply(builder)
val values: NDBuffer<Double> = ndCounter.mapToBuffer { it.value }
val values: BufferND<Double> = ndCounter.mapToBuffer { it.value }
return IndexedHistogram(this, values)
}

View File

@ -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<T> : MutableNDStructure<T> {
public sealed class Nd4jArrayStructure<T> : MutableStructureND<T> {
/**
* The wrapped [INDArray].
*/

View File

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

View File

@ -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<Double> {
public inline class ViktorStructureND(public val f64Buffer: F64Array) : MutableStructureND<Double> {
public override val shape: IntArray get() = f64Buffer.shape
public override inline fun get(index: IntArray): Double = f64Buffer.get(*index)