From 4db7398a288cec1939b4b3bbcd02908bd89c501a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 28 Oct 2021 10:58:22 +0300 Subject: [PATCH] Tensor algebra generified --- .../structures/StructureReadBenchmark.kt | 8 +-- .../kscience/kmath/complex/Quaternion.kt | 4 +- .../kscience/kmath/expressions/MstAlgebra.kt | 10 +-- .../kmath/linear/BufferedLinearSpace.kt | 2 + .../space/kscience/kmath/nd/AlgebraND.kt | 45 +++++++++++--- .../kscience/kmath/nd/BufferAlgebraND.kt | 6 ++ .../space/kscience/kmath/nd/BufferND.kt | 4 +- .../space/kscience/kmath/nd/DoubleFieldND.kt | 13 ++-- .../space/kscience/kmath/nd/ShapeIndexer.kt | 8 ++- .../space/kscience/kmath/nd/StructureND.kt | 6 +- .../kscience/kmath/operations/Algebra.kt | 12 ++-- .../kmath/operations/NumericAlgebra.kt | 4 +- .../kscience/kmath/operations/numbers.kt | 4 +- .../kscience/kmath/operations/BigNumbers.kt | 4 +- .../kmath/structures/LazyStructureND.kt | 2 +- .../kmath/histogram/IndexedHistogramSpace.kt | 2 +- .../histogram/MultivariateHistogramTest.kt | 2 +- .../kscience/kmath/jafama/KMathJafama.kt | 4 +- .../kmath/multik/MultikTensorAlgebra.kt | 46 +++++++------- .../kscience/kmath/nd4j/Nd4jArrayAlgebra.kt | 4 +- .../kscience/kmath/nd4j/Nd4jTensorAlgebra.kt | 30 ++++----- .../kmath/tensors/api/TensorAlgebra.kt | 62 +++++++++---------- .../api/TensorPartialDivisionAlgebra.kt | 8 +-- .../core/BroadcastDoubleTensorAlgebra.kt | 24 +++---- .../kmath/tensors/core/BufferedTensor.kt | 5 +- .../kmath/tensors/core/DoubleTensorAlgebra.kt | 60 +++++++++--------- .../tensors/core/internal/tensorCastsUtils.kt | 2 +- .../kscience/kmath/viktor/ViktorFieldOpsND.kt | 16 ++--- .../kmath/viktor/ViktorStructureND.kt | 2 +- 29 files changed, 221 insertions(+), 178 deletions(-) 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 84dd6538c..db77129a2 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt @@ -19,24 +19,24 @@ fun main() { measureTimeMillis { var res = 0.0 - strides.indices().forEach { res = structure[it] } + strides.asSequence().forEach { res = structure[it] } } // warmup val time1 = measureTimeMillis { var res = 0.0 - strides.indices().forEach { res = structure[it] } + strides.asSequence().forEach { res = structure[it] } } println("Structure reading finished in $time1 millis") val time2 = measureTimeMillis { var res = 0.0 - strides.indices().forEach { res = buffer[strides.offset(it)] } + strides.asSequence().forEach { res = buffer[strides.offset(it)] } } println("Buffer reading finished in $time2 millis") val time3 = measureTimeMillis { var res = 0.0 - strides.indices().forEach { res = array[strides.offset(it)] } + strides.asSequence().forEach { res = array[strides.offset(it)] } } println("Array reading finished in $time3 millis") } diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt index 47cc61313..ff9a8302a 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt @@ -166,8 +166,8 @@ public object QuaternionField : Field, Norm, override operator fun Quaternion.plus(other: Number): Quaternion = Quaternion(w + other.toDouble(), x, y, z) override operator fun Quaternion.minus(other: Number): Quaternion = Quaternion(w - other.toDouble(), x, y, z) - override operator fun Number.times(other: Quaternion): Quaternion = - Quaternion(toDouble() * other.w, toDouble() * other.x, toDouble() * other.y, toDouble() * other.z) + override operator fun Number.times(arg: Quaternion): Quaternion = + Quaternion(toDouble() * arg.w, toDouble() * arg.x, toDouble() * arg.y, toDouble() * arg.z) override fun Quaternion.unaryMinus(): Quaternion = Quaternion(-w, -x, -y, -z) override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt index dd3c46207..ca0671ccb 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt @@ -38,8 +38,8 @@ public object MstGroup : Group, NumericAlgebra, ScaleOperations { override operator fun MST.unaryMinus(): MST.Unary = unaryOperationFunction(GroupOps.MINUS_OPERATION)(this) - override operator fun MST.minus(other: MST): MST.Binary = - binaryOperationFunction(GroupOps.MINUS_OPERATION)(this, other) + override operator fun MST.minus(arg: MST): MST.Binary = + binaryOperationFunction(GroupOps.MINUS_OPERATION)(this, arg) override fun scale(a: MST, value: Double): MST.Binary = binaryOperationFunction(RingOps.TIMES_OPERATION)(a, number(value)) @@ -72,7 +72,7 @@ public object MstRing : Ring, NumbersAddOps, ScaleOperations { override operator fun MST.unaryPlus(): MST.Unary = MstGroup { +this@unaryPlus } override operator fun MST.unaryMinus(): MST.Unary = MstGroup { -this@unaryMinus } - override operator fun MST.minus(other: MST): MST.Binary = MstGroup { this@minus - other } + override operator fun MST.minus(arg: MST): MST.Binary = MstGroup { this@minus - arg } override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary = MstGroup.binaryOperationFunction(operation) @@ -103,7 +103,7 @@ public object MstField : Field, NumbersAddOps, ScaleOperations { override operator fun MST.unaryPlus(): MST.Unary = MstRing { +this@unaryPlus } override operator fun MST.unaryMinus(): MST.Unary = MstRing { -this@unaryMinus } - override operator fun MST.minus(other: MST): MST.Binary = MstRing { this@minus - other } + override operator fun MST.minus(arg: MST): MST.Binary = MstRing { this@minus - arg } override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary = MstRing.binaryOperationFunction(operation) @@ -144,7 +144,7 @@ public object MstExtendedField : ExtendedField, NumericAlgebra { override fun divide(left: MST, right: MST): MST.Binary = MstField.divide(left, right) override operator fun MST.unaryPlus(): MST.Unary = MstField { +this@unaryPlus } override operator fun MST.unaryMinus(): MST.Unary = MstField { -this@unaryMinus } - override operator fun MST.minus(other: MST): MST.Binary = MstField { this@minus - other } + override operator fun MST.minus(arg: MST): MST.Binary = MstField { this@minus - arg } override fun power(arg: MST, pow: Number): MST.Binary = binaryOperationFunction(PowerOperations.POW_OPERATION)(arg, number(pow)) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt index f72b9bd81..410fb8505 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferedLinearSpace.kt @@ -29,6 +29,7 @@ public class BufferedLinearSpace>( override fun buildVector(size: Int, initializer: A.(Int) -> T): Point = bufferAlgebra.buffer(size) { elementAlgebra.initializer(it) } + @OptIn(PerformancePitfall::class) override fun Matrix.unaryMinus(): Matrix = ndAlgebra { asND().map { -it }.as2D() } @@ -83,6 +84,7 @@ public class BufferedLinearSpace>( } } + @OptIn(PerformancePitfall::class) override fun Matrix.times(value: T): Matrix = ndAlgebra { asND().map { it * value }.as2D() } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt index 3d2d08fac..4e52c8ba9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt @@ -5,6 +5,7 @@ package space.kscience.kmath.nd +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.* import kotlin.reflect.KClass @@ -24,6 +25,8 @@ public fun Shape(shapeFirst: Int, vararg shapeRest: Int): Shape = intArrayOf(sha public interface WithShape { public val shape: Shape + + public val indices: ShapeIndexer get() = DefaultStrides(shape) } /** @@ -46,21 +49,37 @@ public interface AlgebraND> { /** * Maps elements from one structure to another one by applying [transform] to them. */ - public fun StructureND.map(transform: C.(T) -> T): StructureND + @PerformancePitfall("Very slow on remote execution algebras") + public fun StructureND.map(transform: C.(T) -> T): StructureND = structureND(shape) { index -> + elementAlgebra.transform(get(index)) + } /** * Maps elements from one structure to another one by applying [transform] to them alongside with their indices. */ - public fun StructureND.mapIndexed(transform: C.(index: IntArray, T) -> T): StructureND + @PerformancePitfall("Very slow on remote execution algebras") + public fun StructureND.mapIndexed(transform: C.(index: IntArray, T) -> T): StructureND = + structureND(shape) { index -> + elementAlgebra.transform(index, get(index)) + } /** * Combines two structures into one. */ - public fun zip(left: StructureND, right: StructureND, transform: C.(T, T) -> T): StructureND + @PerformancePitfall("Very slow on remote execution algebras") + public fun zip(left: StructureND, right: StructureND, transform: C.(T, T) -> T): StructureND { + require(left.shape.contentEquals(right.shape)) { + "Expected left and right of the same shape, but left - ${left.shape} and right - ${right.shape}" + } + return structureND(left.shape) { index -> + elementAlgebra.transform(left[index], right[index]) + } + } /** * Element-wise invocation of function working on [T] on a [StructureND]. */ + @PerformancePitfall public operator fun Function1.invoke(structure: StructureND): StructureND = structure.map { value -> this@invoke(value) } @@ -104,6 +123,7 @@ public interface GroupOpsND> : GroupOps>, * @param right the addend. * @return the sum. */ + @OptIn(PerformancePitfall::class) override fun add(left: StructureND, right: StructureND): StructureND = zip(left, right) { aValue, bValue -> add(aValue, bValue) } @@ -116,6 +136,7 @@ public interface GroupOpsND> : GroupOps>, * @param arg the addend. * @return the sum. */ + @OptIn(PerformancePitfall::class) public operator fun StructureND.plus(arg: T): StructureND = this.map { value -> add(arg, value) } /** @@ -125,25 +146,28 @@ public interface GroupOpsND> : GroupOps>, * @param arg the divisor. * @return the quotient. */ + @OptIn(PerformancePitfall::class) public operator fun StructureND.minus(arg: T): StructureND = this.map { value -> add(arg, -value) } /** * Adds an element to ND structure of it. * * @receiver the augend. - * @param other the addend. + * @param arg the addend. * @return the sum. */ - public operator fun T.plus(other: StructureND): StructureND = other.map { value -> add(this@plus, value) } + @OptIn(PerformancePitfall::class) + public operator fun T.plus(arg: StructureND): StructureND = arg.map { value -> add(this@plus, value) } /** * Subtracts an ND structure from an element of it. * * @receiver the dividend. - * @param other the divisor. + * @param arg the divisor. * @return the quotient. */ - public operator fun T.minus(other: StructureND): StructureND = other.map { value -> add(-this@minus, value) } + @OptIn(PerformancePitfall::class) + public operator fun T.minus(arg: StructureND): StructureND = arg.map { value -> add(-this@minus, value) } public companion object } @@ -166,6 +190,7 @@ public interface RingOpsND> : RingOps>, Gro * @param right the multiplier. * @return the product. */ + @OptIn(PerformancePitfall::class) override fun multiply(left: StructureND, right: StructureND): StructureND = zip(left, right) { aValue, bValue -> multiply(aValue, bValue) } @@ -178,6 +203,7 @@ public interface RingOpsND> : RingOps>, Gro * @param arg the multiplier. * @return the product. */ + @OptIn(PerformancePitfall::class) public operator fun StructureND.times(arg: T): StructureND = this.map { value -> multiply(arg, value) } /** @@ -187,6 +213,7 @@ public interface RingOpsND> : RingOps>, Gro * @param arg the multiplier. * @return the product. */ + @OptIn(PerformancePitfall::class) public operator fun T.times(arg: StructureND): StructureND = arg.map { value -> multiply(this@times, value) } public companion object @@ -214,6 +241,7 @@ public interface FieldOpsND> : * @param right the divisor. * @return the quotient. */ + @OptIn(PerformancePitfall::class) override fun divide(left: StructureND, right: StructureND): StructureND = zip(left, right) { aValue, bValue -> divide(aValue, bValue) } @@ -225,6 +253,7 @@ public interface FieldOpsND> : * @param arg the divisor. * @return the quotient. */ + @OptIn(PerformancePitfall::class) public operator fun StructureND.div(arg: T): StructureND = this.map { value -> divide(arg, value) } /** @@ -234,8 +263,10 @@ public interface FieldOpsND> : * @param arg the divisor. * @return the quotient. */ + @OptIn(PerformancePitfall::class) public operator fun T.div(arg: StructureND): StructureND = arg.map { divide(it, this@div) } + @OptIn(PerformancePitfall::class) override fun scale(a: StructureND, value: Double): StructureND = a.map { scale(it, value) } } 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 cf007e7c9..0e094a8c7 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 @@ -7,6 +7,7 @@ package space.kscience.kmath.nd +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.* import space.kscience.kmath.structures.BufferFactory @@ -34,11 +35,14 @@ public interface BufferAlgebraND> : AlgebraND { } } + @PerformancePitfall override fun StructureND.map(transform: A.(T) -> T): BufferND = mapInline(toBufferND(), transform) + @PerformancePitfall override fun StructureND.mapIndexed(transform: A.(index: IntArray, T) -> T): BufferND = mapIndexedInline(toBufferND(), transform) + @PerformancePitfall override fun zip(left: StructureND, right: StructureND, transform: A.(T, T) -> T): BufferND = zipInline(left.toBufferND(), right.toBufferND(), transform) @@ -78,6 +82,7 @@ internal inline fun > BufferAlgebraND.zipInline( return BufferND(indexes, bufferAlgebra.zipInline(l.buffer, r.buffer, block)) } +@OptIn(PerformancePitfall::class) public open class BufferedGroupNDOps>( override val bufferAlgebra: BufferAlgebra, override val indexerBuilder: (IntArray) -> ShapeIndexer = BufferAlgebraND.defaultIndexerBuilder @@ -101,6 +106,7 @@ public open class BufferedFieldOpsND>( indexerBuilder: (IntArray) -> ShapeIndexer = BufferAlgebraND.defaultIndexerBuilder ) : this(BufferFieldOps(elementAlgebra, bufferFactory), indexerBuilder) + @OptIn(PerformancePitfall::class) override fun scale(a: StructureND, value: Double): StructureND = a.map { it * value } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt index 2b6fd3693..19924616d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferND.kt @@ -19,7 +19,7 @@ import space.kscience.kmath.structures.MutableBufferFactory * @param buffer The underlying buffer. */ public open class BufferND( - public val indices: ShapeIndexer, + override val indices: ShapeIndexer, public open val buffer: Buffer, ) : StructureND { @@ -28,7 +28,7 @@ public open class BufferND( override val shape: IntArray get() = indices.shape @PerformancePitfall - override fun elements(): Sequence> = indices.indices().map { + override fun elements(): Sequence> = indices.asSequence().map { it to this[it] } 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 abb8e46ea..8cc5472a9 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 @@ -5,6 +5,7 @@ package space.kscience.kmath.nd +import space.kscience.kmath.misc.PerformancePitfall import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.* import space.kscience.kmath.structures.DoubleBuffer @@ -50,10 +51,12 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D return DoubleBufferND(indexes, DoubleBuffer(indexes.linearSize) { block(lArray[it], rArray[it]) }) } + @OptIn(PerformancePitfall::class) override fun StructureND.map(transform: DoubleField.(Double) -> Double): BufferND = mapInline(toBufferND()) { DoubleField.transform(it) } + @OptIn(PerformancePitfall::class) override fun zip( left: StructureND, right: StructureND, @@ -92,11 +95,11 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D override fun StructureND.unaryPlus(): DoubleBufferND = toBufferND() - override fun StructureND.plus(other: StructureND): DoubleBufferND = - zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l + r } + override fun StructureND.plus(arg: StructureND): DoubleBufferND = + zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l + r } - override fun StructureND.minus(other: StructureND): DoubleBufferND = - zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l - r } + override fun StructureND.minus(arg: StructureND): DoubleBufferND = + zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l - r } override fun StructureND.times(other: StructureND): DoubleBufferND = zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l * r } @@ -107,7 +110,7 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D override fun StructureND.div(k: Number): DoubleBufferND = mapInline(toBufferND()) { it / k.toDouble() } - override fun Number.times(other: StructureND): DoubleBufferND = other * this + override fun Number.times(arg: StructureND): DoubleBufferND = arg * this override fun StructureND.plus(arg: Double): DoubleBufferND = mapInline(toBufferND()) { it + arg } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndexer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndexer.kt index 1ce6b7519..20e180dd1 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndexer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShapeIndexer.kt @@ -10,7 +10,7 @@ import kotlin.native.concurrent.ThreadLocal /** * A converter from linear index to multivariate index */ -public interface ShapeIndexer{ +public interface ShapeIndexer: Iterable{ public val shape: Shape /** @@ -33,7 +33,9 @@ public interface ShapeIndexer{ /** * Iterate over ND indices in a natural order */ - public fun indices(): Sequence + public fun asSequence(): Sequence + + override fun iterator(): Iterator = asSequence().iterator() override fun equals(other: Any?): Boolean override fun hashCode(): Int @@ -58,7 +60,7 @@ public abstract class Strides: ShapeIndexer { /** * Iterate over ND indices in a natural order */ - public override fun indices(): Sequence = (0 until linearSize).asSequence().map(::index) + public override fun asSequence(): Sequence = (0 until linearSize).asSequence().map(::index) } /** 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 496abf60f..614d97950 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 @@ -28,12 +28,12 @@ public interface StructureFeature : Feature * * @param T the type of items. */ -public interface StructureND : Featured { +public interface StructureND : Featured, WithShape { /** * The shape of structure i.e., non-empty sequence of non-negative integers that specify sizes of dimensions of * this structure. */ - public val shape: Shape + override val shape: Shape /** * The count of dimensions in this structure. It should be equal to size of [shape]. @@ -54,7 +54,7 @@ public interface StructureND : Featured { * @return the lazy sequence of pairs of indices to values. */ @PerformancePitfall - public fun elements(): Sequence> + public fun elements(): Sequence> = indices.asSequence().map { it to get(it) } /** * Feature is some additional structure information that allows to access it special properties or hints. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt index d0b0c0b73..c6af75237 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt @@ -149,19 +149,19 @@ public interface GroupOps : Algebra { * Addition of two elements. * * @receiver the augend. - * @param other the addend. + * @param arg the addend. * @return the sum. */ - public operator fun T.plus(other: T): T = add(this, other) + public operator fun T.plus(arg: T): T = add(this, arg) /** * Subtraction of two elements. * * @receiver the minuend. - * @param other the subtrahend. + * @param arg the subtrahend. * @return the difference. */ - public operator fun T.minus(other: T): T = add(this, -other) + public operator fun T.minus(arg: T): T = add(this, -arg) // Dynamic dispatch of operations override fun unaryOperationFunction(operation: String): (arg: T) -> T = when (operation) { PLUS_OPERATION -> { arg -> +arg } @@ -219,9 +219,9 @@ public interface RingOps : GroupOps { * Multiplies this element by scalar. * * @receiver the multiplier. - * @param other the multiplicand. + * @param arg the multiplicand. */ - public operator fun T.times(other: T): T = multiply(this, other) + public operator fun T.times(arg: T): T = multiply(this, arg) override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) { TIMES_OPERATION -> ::multiply diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt index 6be3449f9..9d9fc0885 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt @@ -139,10 +139,10 @@ public interface ScaleOperations : Algebra { * Multiplication of this number by element. * * @receiver the multiplier. - * @param other the multiplicand. + * @param arg the multiplicand. * @return the product. */ - public operator fun Number.times(other: T): T = other * this + public operator fun Number.times(arg: T): T = arg * this } /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt index 1168dc6ba..0b111349c 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt @@ -228,8 +228,8 @@ public object ByteRing : Ring, Norm, NumericAlgebra { override fun norm(arg: Byte): Byte = if (arg > 0) arg else (-arg).toByte() override inline fun Byte.unaryMinus(): Byte = (-this).toByte() - override inline fun Byte.plus(other: Byte): Byte = (this + other).toByte() - override inline fun Byte.minus(other: Byte): Byte = (this - other).toByte() + override inline fun Byte.plus(arg: Byte): Byte = (this + arg).toByte() + override inline fun Byte.minus(arg: Byte): Byte = (this - arg).toByte() override inline fun Byte.times(other: Byte): Byte = (this * other).toByte() } diff --git a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt index 3a9c242fc..f63efbef2 100644 --- a/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/space/kscience/kmath/operations/BigNumbers.kt @@ -19,7 +19,7 @@ public object JBigIntegerField : Ring, NumericAlgebra { override fun number(value: Number): BigInteger = BigInteger.valueOf(value.toLong()) override fun add(left: BigInteger, right: BigInteger): BigInteger = left.add(right) - override operator fun BigInteger.minus(other: BigInteger): BigInteger = subtract(other) + override operator fun BigInteger.minus(arg: BigInteger): BigInteger = subtract(arg) override fun multiply(left: BigInteger, right: BigInteger): BigInteger = left.multiply(right) override operator fun BigInteger.unaryMinus(): BigInteger = negate() @@ -40,7 +40,7 @@ public abstract class JBigDecimalFieldBase internal constructor( get() = BigDecimal.ONE override fun add(left: BigDecimal, right: BigDecimal): BigDecimal = left.add(right) - override operator fun BigDecimal.minus(other: BigDecimal): BigDecimal = subtract(other) + override operator fun BigDecimal.minus(arg: BigDecimal): BigDecimal = subtract(arg) override fun number(value: Number): BigDecimal = BigDecimal.valueOf(value.toDouble()) override fun scale(a: BigDecimal, value: Double): BigDecimal = 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 3eb6f3aa6..1feb43f33 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 @@ -28,7 +28,7 @@ public class LazyStructureND( @OptIn(PerformancePitfall::class) override fun elements(): Sequence> { val strides = DefaultStrides(shape) - val res = runBlocking { strides.indices().toList().map { index -> index to await(index) } } + val res = runBlocking { strides.asSequence().toList().map { index -> index to await(index) } } return res.asSequence() } } diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt index a495577c3..f36f45389 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt @@ -38,7 +38,7 @@ public class IndexedHistogram, V : Any>( override val dimension: Int get() = context.shape.size override val bins: Iterable> - get() = DefaultStrides(context.shape).indices().map { + get() = DefaultStrides(context.shape).asSequence().map { context.produceBin(it, values[it]) }.asIterable() diff --git a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt index 23dd076e1..e07488741 100644 --- a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt +++ b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/MultivariateHistogramTest.kt @@ -70,7 +70,7 @@ internal class MultivariateHistogramTest { } val res = histogram1 - histogram2 assertTrue { - DefaultStrides(shape).indices().all { index -> + DefaultStrides(shape).asSequence().all { index -> res.values[index] <= histogram1.values[index] } } diff --git a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt index 645a14e30..9ff7ffc9c 100644 --- a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt +++ b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt @@ -57,8 +57,8 @@ public object JafamaDoubleField : ExtendedField, Norm, S override inline fun norm(arg: Double): Double = FastMath.abs(arg) override inline fun Double.unaryMinus(): Double = -this - override inline fun Double.plus(other: Double): Double = this + other - override inline fun Double.minus(other: Double): Double = this - other + override inline fun Double.plus(arg: Double): Double = this + arg + override inline fun Double.minus(arg: Double): Double = this - arg override inline fun Double.times(other: Double): Double = this * other override inline fun Double.div(other: Double): Double = this / other } diff --git a/kmath-multik/src/main/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt b/kmath-multik/src/main/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt index ec0d4c6d9..02959d4fb 100644 --- a/kmath-multik/src/main/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt +++ b/kmath-multik/src/main/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt @@ -59,7 +59,7 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra override fun structureND(shape: Shape, initializer: A.(IntArray) -> T): MultikTensor { val strides = DefaultStrides(shape) val memoryView = initMemoryView(strides.linearSize, type) - strides.indices().forEachIndexed { linearIndex, tensorIndex -> + strides.asSequence().forEachIndexed { linearIndex, tensorIndex -> memoryView[linearIndex] = elementAlgebra.initializer(tensorIndex) } return MultikTensor(NDArray(memoryView, shape = shape, dim = DN(shape.size))) @@ -131,14 +131,14 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra get(intArrayOf(0)) } else null - override fun T.plus(other: StructureND): MultikTensor = - other.plus(this) + override fun T.plus(arg: StructureND): MultikTensor = + arg.plus(this) override fun StructureND.plus(arg: T): MultikTensor = asMultik().array.deepCopy().apply { plusAssign(arg) }.wrap() - override fun StructureND.plus(other: StructureND): MultikTensor = - asMultik().array.plus(other.asMultik().array).wrap() + override fun StructureND.plus(arg: StructureND): MultikTensor = + asMultik().array.plus(arg.asMultik().array).wrap() override fun Tensor.plusAssign(value: T) { if (this is MultikTensor) { @@ -148,21 +148,21 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra } } - override fun Tensor.plusAssign(other: StructureND) { + override fun Tensor.plusAssign(arg: StructureND) { if (this is MultikTensor) { - array.plusAssign(other.asMultik().array) + array.plusAssign(arg.asMultik().array) } else { - mapInPlace { index, t -> elementAlgebra.add(t, other[index]) } + mapInPlace { index, t -> elementAlgebra.add(t, arg[index]) } } } - override fun T.minus(other: StructureND): MultikTensor = (-(other.asMultik().array - this)).wrap() + override fun T.minus(arg: StructureND): MultikTensor = (-(arg.asMultik().array - this)).wrap() override fun StructureND.minus(arg: T): MultikTensor = asMultik().array.deepCopy().apply { minusAssign(arg) }.wrap() - override fun StructureND.minus(other: StructureND): MultikTensor = - asMultik().array.minus(other.asMultik().array).wrap() + override fun StructureND.minus(arg: StructureND): MultikTensor = + asMultik().array.minus(arg.asMultik().array).wrap() override fun Tensor.minusAssign(value: T) { if (this is MultikTensor) { @@ -172,11 +172,11 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra } } - override fun Tensor.minusAssign(other: StructureND) { + override fun Tensor.minusAssign(arg: StructureND) { if (this is MultikTensor) { - array.minusAssign(other.asMultik().array) + array.minusAssign(arg.asMultik().array) } else { - mapInPlace { index, t -> elementAlgebra.run { t - other[index] } } + mapInPlace { index, t -> elementAlgebra.run { t - arg[index] } } } } @@ -186,8 +186,8 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra override fun StructureND.times(arg: T): Tensor = asMultik().array.deepCopy().apply { timesAssign(arg) }.wrap() - override fun StructureND.times(other: StructureND): MultikTensor = - asMultik().array.times(other.asMultik().array).wrap() + override fun StructureND.times(arg: StructureND): MultikTensor = + asMultik().array.times(arg.asMultik().array).wrap() override fun Tensor.timesAssign(value: T) { if (this is MultikTensor) { @@ -197,11 +197,11 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra } } - override fun Tensor.timesAssign(other: StructureND) { + override fun Tensor.timesAssign(arg: StructureND) { if (this is MultikTensor) { - array.timesAssign(other.asMultik().array) + array.timesAssign(arg.asMultik().array) } else { - mapInPlace { index, t -> elementAlgebra.multiply(t, other[index]) } + mapInPlace { index, t -> elementAlgebra.multiply(t, arg[index]) } } } @@ -212,7 +212,7 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra override fun StructureND.transpose(i: Int, j: Int): MultikTensor = asMultik().array.transpose(i, j).wrap() - override fun StructureND.view(shape: IntArray): MultikTensor { + override fun Tensor.view(shape: IntArray): MultikTensor { require(shape.all { it > 0 }) require(shape.fold(1, Int::times) == this.shape.size) { "Cannot reshape array of size ${this.shape.size} into a new shape ${ @@ -231,7 +231,7 @@ public abstract class MultikTensorAlgebra> : TensorAlgebra }.wrap() } - override fun StructureND.viewAs(other: StructureND): MultikTensor = view(other.shape) + override fun Tensor.viewAs(other: StructureND): MultikTensor = view(other.shape) override fun StructureND.dot(other: StructureND): MultikTensor = if (this.shape.size == 1 && other.shape.size == 1) { @@ -283,8 +283,8 @@ public abstract class MultikDivisionTensorAlgebra> override fun StructureND.div(arg: T): MultikTensor = asMultik().array.deepCopy().apply { divAssign(arg) }.wrap() - override fun StructureND.div(other: StructureND): MultikTensor = - asMultik().array.div(other.asMultik().array).wrap() + override fun StructureND.div(arg: StructureND): MultikTensor = + asMultik().array.div(arg.asMultik().array).wrap() override fun Tensor.divAssign(value: T) { if (this is MultikTensor) { diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt index 1f312849a..b1cc1f834 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt @@ -76,8 +76,8 @@ public sealed interface Nd4jArrayGroupOps> : GroupOpsND override fun add(left: StructureND, right: StructureND): Nd4jArrayStructure = left.ndArray.add(right.ndArray).wrap() - override operator fun StructureND.minus(other: StructureND): Nd4jArrayStructure = - ndArray.sub(other.ndArray).wrap() + override operator fun StructureND.minus(arg: StructureND): Nd4jArrayStructure = + ndArray.sub(arg.ndArray).wrap() override operator fun StructureND.unaryMinus(): Nd4jArrayStructure = ndArray.neg().wrap() diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt index 3fb03c964..33765b40a 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt @@ -51,29 +51,29 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe return structureND(left.shape) { index -> elementAlgebra.transform(left[index], right[index]) } } - override fun T.plus(other: StructureND): Nd4jArrayStructure = other.ndArray.add(this).wrap() + override fun T.plus(arg: StructureND): Nd4jArrayStructure = arg.ndArray.add(this).wrap() override fun StructureND.plus(arg: T): Nd4jArrayStructure = ndArray.add(arg).wrap() - override fun StructureND.plus(other: StructureND): Nd4jArrayStructure = ndArray.add(other.ndArray).wrap() + override fun StructureND.plus(arg: StructureND): Nd4jArrayStructure = ndArray.add(arg.ndArray).wrap() override fun Tensor.plusAssign(value: T) { ndArray.addi(value) } - override fun Tensor.plusAssign(other: StructureND) { - ndArray.addi(other.ndArray) + override fun Tensor.plusAssign(arg: StructureND) { + ndArray.addi(arg.ndArray) } - override fun T.minus(other: StructureND): Nd4jArrayStructure = other.ndArray.rsub(this).wrap() + override fun T.minus(arg: StructureND): Nd4jArrayStructure = arg.ndArray.rsub(this).wrap() override fun StructureND.minus(arg: T): Nd4jArrayStructure = ndArray.sub(arg).wrap() - override fun StructureND.minus(other: StructureND): Nd4jArrayStructure = ndArray.sub(other.ndArray).wrap() + override fun StructureND.minus(arg: StructureND): Nd4jArrayStructure = ndArray.sub(arg.ndArray).wrap() override fun Tensor.minusAssign(value: T) { ndArray.rsubi(value) } - override fun Tensor.minusAssign(other: StructureND) { - ndArray.subi(other.ndArray) + override fun Tensor.minusAssign(arg: StructureND) { + ndArray.subi(arg.ndArray) } override fun T.times(arg: StructureND): Nd4jArrayStructure = arg.ndArray.mul(this).wrap() @@ -81,14 +81,14 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe override fun StructureND.times(arg: T): Nd4jArrayStructure = ndArray.mul(arg).wrap() - override fun StructureND.times(other: StructureND): Nd4jArrayStructure = ndArray.mul(other.ndArray).wrap() + override fun StructureND.times(arg: StructureND): Nd4jArrayStructure = ndArray.mul(arg.ndArray).wrap() override fun Tensor.timesAssign(value: T) { ndArray.muli(value) } - override fun Tensor.timesAssign(other: StructureND) { - ndArray.mmuli(other.ndArray) + override fun Tensor.timesAssign(arg: StructureND) { + ndArray.mmuli(arg.ndArray) } override fun StructureND.unaryMinus(): Nd4jArrayStructure = ndArray.neg().wrap() @@ -105,8 +105,8 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe override fun StructureND.max(dim: Int, keepDim: Boolean): Nd4jArrayStructure = ndArray.max(keepDim, dim).wrap() - override fun StructureND.view(shape: IntArray): Nd4jArrayStructure = ndArray.reshape(shape).wrap() - override fun StructureND.viewAs(other: StructureND): Nd4jArrayStructure = view(other.shape) + override fun Tensor.view(shape: IntArray): Nd4jArrayStructure = ndArray.reshape(shape).wrap() + override fun Tensor.viewAs(other: StructureND): Nd4jArrayStructure = view(other.shape) override fun StructureND.argMax(dim: Int, keepDim: Boolean): Nd4jArrayStructure = ndBase.get().argmax(ndArray, keepDim, dim).wrap() @@ -142,7 +142,7 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe override fun T.div(arg: StructureND): Nd4jArrayStructure = arg.ndArray.rdiv(this).wrap() override fun StructureND.div(arg: T): Nd4jArrayStructure = ndArray.div(arg).wrap() - override fun StructureND.div(other: StructureND): Nd4jArrayStructure = ndArray.div(other.ndArray).wrap() + override fun StructureND.div(arg: StructureND): Nd4jArrayStructure = ndArray.div(arg.ndArray).wrap() override fun Tensor.divAssign(value: T) { ndArray.divi(value) @@ -172,7 +172,7 @@ public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra { override fun structureND(shape: Shape, initializer: DoubleField.(IntArray) -> Double): Nd4jArrayStructure { val array: INDArray = Nd4j.zeros(*shape) val indices = DefaultStrides(shape) - indices.indices().forEach { index -> + indices.asSequence().forEach { index -> array.putScalar(index, elementAlgebra.initializer(index)) } return array.wrap() diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt index 28d7c4677..8c445cf2d 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorAlgebra.kt @@ -32,13 +32,13 @@ public interface TensorAlgebra> : RingOpsND { valueOrNull() ?: throw IllegalArgumentException("Inconsistent value for tensor of with $shape shape") /** - * Each element of the tensor [other] is added to this value. + * Each element of the tensor [arg] is added to this value. * The resulting tensor is returned. * - * @param other tensor to be added. - * @return the sum of this value and tensor [other]. + * @param arg tensor to be added. + * @return the sum of this value and tensor [arg]. */ - override operator fun T.plus(other: StructureND): Tensor + override operator fun T.plus(arg: StructureND): Tensor /** * Adds the scalar [arg] to each element of this tensor and returns a new resulting tensor. @@ -49,13 +49,13 @@ public interface TensorAlgebra> : RingOpsND { override operator fun StructureND.plus(arg: T): Tensor /** - * Each element of the tensor [other] is added to each element of this tensor. + * Each element of the tensor [arg] is added to each element of this tensor. * The resulting tensor is returned. * - * @param other tensor to be added. - * @return the sum of this tensor and [other]. + * @param arg tensor to be added. + * @return the sum of this tensor and [arg]. */ - override operator fun StructureND.plus(other: StructureND): Tensor + override operator fun StructureND.plus(arg: StructureND): Tensor /** * Adds the scalar [value] to each element of this tensor. @@ -65,20 +65,20 @@ public interface TensorAlgebra> : RingOpsND { public operator fun Tensor.plusAssign(value: T) /** - * Each element of the tensor [other] is added to each element of this tensor. + * Each element of the tensor [arg] is added to each element of this tensor. * - * @param other tensor to be added. + * @param arg tensor to be added. */ - public operator fun Tensor.plusAssign(other: StructureND) + public operator fun Tensor.plusAssign(arg: StructureND) /** - * Each element of the tensor [other] is subtracted from this value. + * Each element of the tensor [arg] is subtracted from this value. * The resulting tensor is returned. * - * @param other tensor to be subtracted. - * @return the difference between this value and tensor [other]. + * @param arg tensor to be subtracted. + * @return the difference between this value and tensor [arg]. */ - override operator fun T.minus(other: StructureND): Tensor + override operator fun T.minus(arg: StructureND): Tensor /** * Subtracts the scalar [arg] from each element of this tensor and returns a new resulting tensor. @@ -89,13 +89,13 @@ public interface TensorAlgebra> : RingOpsND { override operator fun StructureND.minus(arg: T): Tensor /** - * Each element of the tensor [other] is subtracted from each element of this tensor. + * Each element of the tensor [arg] is subtracted from each element of this tensor. * The resulting tensor is returned. * - * @param other tensor to be subtracted. - * @return the difference between this tensor and [other]. + * @param arg tensor to be subtracted. + * @return the difference between this tensor and [arg]. */ - override operator fun StructureND.minus(other: StructureND): Tensor + override operator fun StructureND.minus(arg: StructureND): Tensor /** * Subtracts the scalar [value] from each element of this tensor. @@ -105,11 +105,11 @@ public interface TensorAlgebra> : RingOpsND { public operator fun Tensor.minusAssign(value: T) /** - * Each element of the tensor [other] is subtracted from each element of this tensor. + * Each element of the tensor [arg] is subtracted from each element of this tensor. * - * @param other tensor to be subtracted. + * @param arg tensor to be subtracted. */ - public operator fun Tensor.minusAssign(other: StructureND) + public operator fun Tensor.minusAssign(arg: StructureND) /** @@ -130,13 +130,13 @@ public interface TensorAlgebra> : RingOpsND { override operator fun StructureND.times(arg: T): Tensor /** - * Each element of the tensor [other] is multiplied by each element of this tensor. + * Each element of the tensor [arg] is multiplied by each element of this tensor. * The resulting tensor is returned. * - * @param other tensor to be multiplied. - * @return the product of this tensor and [other]. + * @param arg tensor to be multiplied. + * @return the product of this tensor and [arg]. */ - override operator fun StructureND.times(other: StructureND): Tensor + override operator fun StructureND.times(arg: StructureND): Tensor /** * Multiplies the scalar [value] by each element of this tensor. @@ -146,11 +146,11 @@ public interface TensorAlgebra> : RingOpsND { public operator fun Tensor.timesAssign(value: T) /** - * Each element of the tensor [other] is multiplied by each element of this tensor. + * Each element of the tensor [arg] is multiplied by each element of this tensor. * - * @param other tensor to be multiplied. + * @param arg tensor to be multiplied. */ - public operator fun Tensor.timesAssign(other: StructureND) + public operator fun Tensor.timesAssign(arg: StructureND) /** * Numerical negative, element-wise. @@ -186,7 +186,7 @@ public interface TensorAlgebra> : RingOpsND { * @param shape the desired size * @return tensor with new shape */ - public fun StructureND.view(shape: IntArray): Tensor + public fun Tensor.view(shape: IntArray): Tensor /** * View this tensor as the same size as [other]. @@ -196,7 +196,7 @@ public interface TensorAlgebra> : RingOpsND { * @param other the result tensor has the same size as other. * @return the result tensor with the same size as other. */ - public fun StructureND.viewAs(other: StructureND): Tensor + public fun Tensor.viewAs(other: StructureND): Tensor /** * Matrix product of two tensors. diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt index dece54834..304cc66cc 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/api/TensorPartialDivisionAlgebra.kt @@ -35,13 +35,13 @@ public interface TensorPartialDivisionAlgebra> : TensorAlgebra.div(arg: T): Tensor /** - * Each element of the tensor [other] is divided by each element of this tensor. + * Each element of the tensor [arg] is divided by each element of this tensor. * The resulting tensor is returned. * - * @param other tensor to be divided by. - * @return the division of this tensor by [other]. + * @param arg tensor to be divided by. + * @return the division of this tensor by [arg]. */ - override operator fun StructureND.div(other: StructureND): Tensor + override operator fun StructureND.div(arg: StructureND): Tensor override fun divide(left: StructureND, right: StructureND): StructureND = left.div(right) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt index f53a0ca55..83d1f7b35 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt @@ -19,8 +19,8 @@ import space.kscience.kmath.tensors.core.internal.tensor */ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { - override fun StructureND.plus(other: StructureND): DoubleTensor { - val broadcast = broadcastTensors(tensor, other.tensor) + override fun StructureND.plus(arg: StructureND): DoubleTensor { + val broadcast = broadcastTensors(tensor, arg.tensor) val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.indices.linearSize) { i -> @@ -29,16 +29,16 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { return DoubleTensor(newThis.shape, resBuffer) } - override fun Tensor.plusAssign(other: StructureND) { - val newOther = broadcastTo(other.tensor, tensor.shape) + override fun Tensor.plusAssign(arg: StructureND) { + val newOther = broadcastTo(arg.tensor, tensor.shape) for (i in 0 until tensor.indices.linearSize) { tensor.mutableBuffer.array()[tensor.bufferStart + i] += newOther.mutableBuffer.array()[tensor.bufferStart + i] } } - override fun StructureND.minus(other: StructureND): DoubleTensor { - val broadcast = broadcastTensors(tensor, other.tensor) + override fun StructureND.minus(arg: StructureND): DoubleTensor { + val broadcast = broadcastTensors(tensor, arg.tensor) val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.indices.linearSize) { i -> @@ -47,16 +47,16 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { return DoubleTensor(newThis.shape, resBuffer) } - override fun Tensor.minusAssign(other: StructureND) { - val newOther = broadcastTo(other.tensor, tensor.shape) + override fun Tensor.minusAssign(arg: StructureND) { + val newOther = broadcastTo(arg.tensor, tensor.shape) for (i in 0 until tensor.indices.linearSize) { tensor.mutableBuffer.array()[tensor.bufferStart + i] -= newOther.mutableBuffer.array()[tensor.bufferStart + i] } } - override fun StructureND.times(other: StructureND): DoubleTensor { - val broadcast = broadcastTensors(tensor, other.tensor) + override fun StructureND.times(arg: StructureND): DoubleTensor { + val broadcast = broadcastTensors(tensor, arg.tensor) val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.indices.linearSize) { i -> @@ -66,8 +66,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { return DoubleTensor(newThis.shape, resBuffer) } - override fun Tensor.timesAssign(other: StructureND) { - val newOther = broadcastTo(other.tensor, tensor.shape) + override fun Tensor.timesAssign(arg: StructureND) { + val newOther = broadcastTo(arg.tensor, tensor.shape) for (i in 0 until tensor.indices.linearSize) { tensor.mutableBuffer.array()[tensor.bufferStart + i] *= newOther.mutableBuffer.array()[tensor.bufferStart + i] diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt index ba3331067..eab4d3d5b 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt @@ -23,8 +23,7 @@ public open class BufferedTensor internal constructor( /** * Buffer strides based on [TensorLinearStructure] implementation */ - public val indices: Strides - get() = TensorLinearStructure(shape) + override val indices: Strides get() = TensorLinearStructure(shape) /** * Number of elements in tensor @@ -39,7 +38,7 @@ public open class BufferedTensor internal constructor( } @PerformancePitfall - override fun elements(): Sequence> = indices.indices().map { + override fun elements(): Sequence> = indices.asSequence().map { it to get(it) } } diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index be87f8019..8b6f6403f 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -112,7 +112,7 @@ public open class DoubleTensorAlgebra : */ override fun structureND(shape: IntArray, initializer: DoubleField.(IntArray) -> Double): DoubleTensor = fromArray( shape, - TensorLinearStructure(shape).indices().map { DoubleField.initializer(it) }.toMutableList().toDoubleArray() + TensorLinearStructure(shape).asSequence().map { DoubleField.initializer(it) }.toMutableList().toDoubleArray() ) override operator fun StructureND.get(i: Int): DoubleTensor { @@ -201,19 +201,19 @@ public open class DoubleTensorAlgebra : public fun StructureND.copy(): DoubleTensor = DoubleTensor(tensor.shape, tensor.mutableBuffer.array().copyOf(), tensor.bufferStart) - override fun Double.plus(other: StructureND): DoubleTensor { - val resBuffer = DoubleArray(other.tensor.numElements) { i -> - other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] + this + override fun Double.plus(arg: StructureND): DoubleTensor { + val resBuffer = DoubleArray(arg.tensor.numElements) { i -> + arg.tensor.mutableBuffer.array()[arg.tensor.bufferStart + i] + this } - return DoubleTensor(other.shape, resBuffer) + return DoubleTensor(arg.shape, resBuffer) } override fun StructureND.plus(arg: Double): DoubleTensor = arg + tensor - override fun StructureND.plus(other: StructureND): DoubleTensor { - checkShapesCompatible(tensor, other.tensor) + override fun StructureND.plus(arg: StructureND): DoubleTensor { + checkShapesCompatible(tensor, arg.tensor) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.mutableBuffer.array()[i] + other.tensor.mutableBuffer.array()[i] + tensor.mutableBuffer.array()[i] + arg.tensor.mutableBuffer.array()[i] } return DoubleTensor(tensor.shape, resBuffer) } @@ -224,19 +224,19 @@ public open class DoubleTensorAlgebra : } } - override fun Tensor.plusAssign(other: StructureND) { - checkShapesCompatible(tensor, other.tensor) + override fun Tensor.plusAssign(arg: StructureND) { + checkShapesCompatible(tensor, arg.tensor) for (i in 0 until tensor.numElements) { tensor.mutableBuffer.array()[tensor.bufferStart + i] += - other.tensor.mutableBuffer.array()[tensor.bufferStart + i] + arg.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } - override fun Double.minus(other: StructureND): DoubleTensor { - val resBuffer = DoubleArray(other.tensor.numElements) { i -> - this - other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] + override fun Double.minus(arg: StructureND): DoubleTensor { + val resBuffer = DoubleArray(arg.tensor.numElements) { i -> + this - arg.tensor.mutableBuffer.array()[arg.tensor.bufferStart + i] } - return DoubleTensor(other.shape, resBuffer) + return DoubleTensor(arg.shape, resBuffer) } override fun StructureND.minus(arg: Double): DoubleTensor { @@ -246,10 +246,10 @@ public open class DoubleTensorAlgebra : return DoubleTensor(tensor.shape, resBuffer) } - override fun StructureND.minus(other: StructureND): DoubleTensor { - checkShapesCompatible(tensor, other) + override fun StructureND.minus(arg: StructureND): DoubleTensor { + checkShapesCompatible(tensor, arg) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.mutableBuffer.array()[i] - other.tensor.mutableBuffer.array()[i] + tensor.mutableBuffer.array()[i] - arg.tensor.mutableBuffer.array()[i] } return DoubleTensor(tensor.shape, resBuffer) } @@ -260,11 +260,11 @@ public open class DoubleTensorAlgebra : } } - override fun Tensor.minusAssign(other: StructureND) { - checkShapesCompatible(tensor, other) + override fun Tensor.minusAssign(arg: StructureND) { + checkShapesCompatible(tensor, arg) for (i in 0 until tensor.numElements) { tensor.mutableBuffer.array()[tensor.bufferStart + i] -= - other.tensor.mutableBuffer.array()[tensor.bufferStart + i] + arg.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } @@ -277,11 +277,11 @@ public open class DoubleTensorAlgebra : override fun StructureND.times(arg: Double): DoubleTensor = arg * tensor - override fun StructureND.times(other: StructureND): DoubleTensor { - checkShapesCompatible(tensor, other) + override fun StructureND.times(arg: StructureND): DoubleTensor { + checkShapesCompatible(tensor, arg) val resBuffer = DoubleArray(tensor.numElements) { i -> tensor.mutableBuffer.array()[tensor.bufferStart + i] * - other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] + arg.tensor.mutableBuffer.array()[arg.tensor.bufferStart + i] } return DoubleTensor(tensor.shape, resBuffer) } @@ -292,11 +292,11 @@ public open class DoubleTensorAlgebra : } } - override fun Tensor.timesAssign(other: StructureND) { - checkShapesCompatible(tensor, other) + override fun Tensor.timesAssign(arg: StructureND) { + checkShapesCompatible(tensor, arg) for (i in 0 until tensor.numElements) { tensor.mutableBuffer.array()[tensor.bufferStart + i] *= - other.tensor.mutableBuffer.array()[tensor.bufferStart + i] + arg.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } @@ -368,12 +368,12 @@ public open class DoubleTensorAlgebra : return resTensor } - override fun StructureND.view(shape: IntArray): DoubleTensor { + override fun Tensor.view(shape: IntArray): DoubleTensor { checkView(tensor, shape) return DoubleTensor(shape, tensor.mutableBuffer.array(), tensor.bufferStart) } - override fun StructureND.viewAs(other: StructureND): DoubleTensor = + override fun Tensor.viewAs(other: StructureND): DoubleTensor = tensor.view(other.shape) override infix fun StructureND.dot(other: StructureND): DoubleTensor { @@ -584,7 +584,7 @@ public open class DoubleTensorAlgebra : } val resNumElements = resShape.reduce(Int::times) val resTensor = DoubleTensor(resShape, DoubleArray(resNumElements) { 0.0 }, 0) - for (index in resTensor.indices.indices()) { + for (index in resTensor.indices.asSequence()) { val prefix = index.take(dim).toIntArray() val suffix = index.takeLast(dimension - dim - 1).toIntArray() resTensor[index] = foldFunction(DoubleArray(shape[dim]) { i -> diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/tensorCastsUtils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/tensorCastsUtils.kt index 4123ff923..d3d327f66 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/tensorCastsUtils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/tensorCastsUtils.kt @@ -22,7 +22,7 @@ internal fun BufferedTensor.asTensor(): DoubleTensor = internal fun StructureND.copyToBufferedTensor(): BufferedTensor = BufferedTensor( this.shape, - TensorLinearStructure(this.shape).indices().map(this::get).toMutableList().asMutableBuffer(), 0 + TensorLinearStructure(this.shape).asSequence().map(this::get).toMutableList().asMutableBuffer(), 0 ) internal fun StructureND.toBufferedTensor(): BufferedTensor = when (this) { diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt index aaa113e56..c50404c9c 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt @@ -28,7 +28,7 @@ public open class ViktorFieldOpsND : override fun structureND(shape: IntArray, initializer: DoubleField.(IntArray) -> Double): ViktorStructureND = F64Array(*shape).apply { - DefaultStrides(shape).indices().forEach { index -> + DefaultStrides(shape).asSequence().forEach { index -> set(value = DoubleField.initializer(index), indices = index) } }.asStructure() @@ -37,7 +37,7 @@ public open class ViktorFieldOpsND : override fun StructureND.map(transform: DoubleField.(Double) -> Double): ViktorStructureND = F64Array(*shape).apply { - DefaultStrides(shape).indices().forEach { index -> + DefaultStrides(shape).asSequence().forEach { index -> set(value = DoubleField.transform(this@map[index]), indices = index) } }.asStructure() @@ -45,7 +45,7 @@ public open class ViktorFieldOpsND : override fun StructureND.mapIndexed( transform: DoubleField.(index: IntArray, Double) -> Double, ): ViktorStructureND = F64Array(*shape).apply { - DefaultStrides(shape).indices().forEach { index -> + DefaultStrides(shape).asSequence().forEach { index -> set(value = DoubleField.transform(index, this@mapIndexed[index]), indices = index) } }.asStructure() @@ -57,7 +57,7 @@ public open class ViktorFieldOpsND : ): ViktorStructureND { require(left.shape.contentEquals(right.shape)) return F64Array(*left.shape).apply { - DefaultStrides(left.shape).indices().forEach { index -> + DefaultStrides(left.shape).asSequence().forEach { index -> set(value = DoubleField.transform(left[index], right[index]), indices = index) } }.asStructure() @@ -69,11 +69,11 @@ public open class ViktorFieldOpsND : override fun scale(a: StructureND, value: Double): ViktorStructureND = (a.f64Buffer * value).asStructure() - override fun StructureND.plus(other: StructureND): ViktorStructureND = - (f64Buffer + other.f64Buffer).asStructure() + override fun StructureND.plus(arg: StructureND): ViktorStructureND = + (f64Buffer + arg.f64Buffer).asStructure() - override fun StructureND.minus(other: StructureND): ViktorStructureND = - (f64Buffer - other.f64Buffer).asStructure() + override fun StructureND.minus(arg: StructureND): ViktorStructureND = + (f64Buffer - arg.f64Buffer).asStructure() override fun StructureND.times(k: Number): ViktorStructureND = (f64Buffer * k.toDouble()).asStructure() 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 0d29983f9..4926652ed 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 @@ -22,7 +22,7 @@ public class ViktorStructureND(public val f64Buffer: F64Array) : MutableStructur @PerformancePitfall override fun elements(): Sequence> = - DefaultStrides(shape).indices().map { it to get(it) } + DefaultStrides(shape).asSequence().map { it to get(it) } } public fun F64Array.asStructure(): ViktorStructureND = ViktorStructureND(this)