From 46e7da9ae00ae6470201624583adc47e2187a840 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 28 Oct 2021 11:18:16 +0300 Subject: [PATCH] Tensor algebra generified --- .../space/kscience/kmath/nd/DoubleFieldND.kt | 8 ++--- .../kscience/kmath/operations/Algebra.kt | 4 +-- .../kmath/operations/DoubleBufferOps.kt | 14 ++++---- .../kscience/kmath/operations/numbers.kt | 36 +++++++++---------- .../kscience/kmath/jafama/KMathJafama.kt | 12 +++---- .../kmath/multik/MultikTensorAlgebra.kt | 6 ++-- .../kscience/kmath/nd4j/Nd4jTensorAlgebra.kt | 4 +-- .../api/TensorPartialDivisionAlgebra.kt | 6 ++-- .../core/BroadcastDoubleTensorAlgebra.kt | 8 ++--- .../kmath/tensors/core/DoubleTensorAlgebra.kt | 14 ++++---- 10 files changed, 56 insertions(+), 56 deletions(-) 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 8cc5472a9..7285fdb24 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 @@ -81,8 +81,8 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D override fun StructureND.unaryMinus(): DoubleBufferND = mapInline(toBufferND()) { -it } - override fun StructureND.div(other: StructureND): DoubleBufferND = - zipInline(toBufferND(), other.toBufferND()) { l, r -> l / r } + override fun StructureND.div(arg: StructureND): DoubleBufferND = + zipInline(toBufferND(), arg.toBufferND()) { l, r -> l / r } override fun divide(left: StructureND, right: StructureND): DoubleBufferND = zipInline(left.toBufferND(), right.toBufferND()) { l: Double, r: Double -> l / r } @@ -101,8 +101,8 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D 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 } + override fun StructureND.times(arg: StructureND): DoubleBufferND = + zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l * r } override fun StructureND.times(k: Number): DoubleBufferND = mapInline(toBufferND()) { it * k.toDouble() } 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 c6af75237..0e5c6de1f 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 @@ -270,10 +270,10 @@ public interface FieldOps : RingOps { * Division of two elements. * * @receiver the dividend. - * @param other the divisor. + * @param arg the divisor. * @return the quotient. */ - public operator fun T.div(other: T): T = divide(this, other) + public operator fun T.div(arg: T): T = divide(this, arg) override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = when (operation) { DIV_OPERATION -> ::divide diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt index 29b25aae8..3d51b3d32 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt @@ -34,18 +34,18 @@ public abstract class DoubleBufferOps : ExtendedFieldOps>, Norm.plus(other: Buffer): DoubleBuffer = add(this, other) + override fun Buffer.plus(arg: Buffer): DoubleBuffer = add(this, arg) - override fun Buffer.minus(other: Buffer): DoubleBuffer { - require(other.size == this.size) { - "The size of the first buffer ${this.size} should be the same as for second one: ${other.size} " + override fun Buffer.minus(arg: Buffer): DoubleBuffer { + require(arg.size == this.size) { + "The size of the first buffer ${this.size} should be the same as for second one: ${arg.size} " } - return if (this is DoubleBuffer && other is DoubleBuffer) { + return if (this is DoubleBuffer && arg is DoubleBuffer) { val aArray = this.array - val bArray = other.array + val bArray = arg.array DoubleBuffer(DoubleArray(this.size) { aArray[it] - bArray[it] }) - } else DoubleBuffer(DoubleArray(this.size) { this[it] - other[it] }) + } else DoubleBuffer(DoubleArray(this.size) { this[it] - arg[it] }) } // 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 0b111349c..ceb85f3ab 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 @@ -102,10 +102,10 @@ public object DoubleField : ExtendedField, Norm, ScaleOp override inline fun norm(arg: Double): Double = 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.times(other: Double): Double = this * other - override inline fun Double.div(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(arg: Double): Double = this * arg + override inline fun Double.div(arg: Double): Double = this / arg } public val Double.Companion.algebra: DoubleField get() = DoubleField @@ -155,10 +155,10 @@ public object FloatField : ExtendedField, Norm { override inline fun norm(arg: Float): Float = abs(arg) override inline fun Float.unaryMinus(): Float = -this - override inline fun Float.plus(other: Float): Float = this + other - override inline fun Float.minus(other: Float): Float = this - other - override inline fun Float.times(other: Float): Float = this * other - override inline fun Float.div(other: Float): Float = this / other + override inline fun Float.plus(arg: Float): Float = this + arg + override inline fun Float.minus(arg: Float): Float = this - arg + override inline fun Float.times(arg: Float): Float = this * arg + override inline fun Float.div(arg: Float): Float = this / arg } public val Float.Companion.algebra: FloatField get() = FloatField @@ -180,9 +180,9 @@ public object IntRing : Ring, Norm, NumericAlgebra { override inline fun norm(arg: Int): Int = abs(arg) override inline fun Int.unaryMinus(): Int = -this - override inline fun Int.plus(other: Int): Int = this + other - override inline fun Int.minus(other: Int): Int = this - other - override inline fun Int.times(other: Int): Int = this * other + override inline fun Int.plus(arg: Int): Int = this + arg + override inline fun Int.minus(arg: Int): Int = this - arg + override inline fun Int.times(arg: Int): Int = this * arg } public val Int.Companion.algebra: IntRing get() = IntRing @@ -204,9 +204,9 @@ public object ShortRing : Ring, Norm, NumericAlgebra override fun norm(arg: Short): Short = if (arg > 0) arg else (-arg).toShort() override inline fun Short.unaryMinus(): Short = (-this).toShort() - override inline fun Short.plus(other: Short): Short = (this + other).toShort() - override inline fun Short.minus(other: Short): Short = (this - other).toShort() - override inline fun Short.times(other: Short): Short = (this * other).toShort() + override inline fun Short.plus(arg: Short): Short = (this + arg).toShort() + override inline fun Short.minus(arg: Short): Short = (this - arg).toShort() + override inline fun Short.times(arg: Short): Short = (this * arg).toShort() } public val Short.Companion.algebra: ShortRing get() = ShortRing @@ -230,7 +230,7 @@ public object ByteRing : Ring, Norm, NumericAlgebra { override inline fun Byte.unaryMinus(): Byte = (-this).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() + override inline fun Byte.times(arg: Byte): Byte = (this * arg).toByte() } public val Byte.Companion.algebra: ByteRing get() = ByteRing @@ -252,9 +252,9 @@ public object LongRing : Ring, Norm, NumericAlgebra { override fun norm(arg: Long): Long = abs(arg) override inline fun Long.unaryMinus(): Long = (-this) - override inline fun Long.plus(other: Long): Long = (this + other) - override inline fun Long.minus(other: Long): Long = (this - other) - override inline fun Long.times(other: Long): Long = (this * other) + override inline fun Long.plus(arg: Long): Long = (this + arg) + override inline fun Long.minus(arg: Long): Long = (this - arg) + override inline fun Long.times(arg: Long): Long = (this * arg) } public val Long.Companion.algebra: LongRing get() = LongRing 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 9ff7ffc9c..64a935705 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 @@ -59,8 +59,8 @@ public object JafamaDoubleField : ExtendedField, Norm, S override inline fun Double.unaryMinus(): Double = -this 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 + override inline fun Double.times(arg: Double): Double = this * arg + override inline fun Double.div(arg: Double): Double = this / arg } /** @@ -108,8 +108,8 @@ public object StrictJafamaDoubleField : ExtendedField, Norm> } } - override fun Tensor.divAssign(other: StructureND) { + override fun Tensor.divAssign(arg: StructureND) { if (this is MultikTensor) { - array.divAssign(other.asMultik().array) + array.divAssign(arg.asMultik().array) } else { - mapInPlace { index, t -> elementAlgebra.divide(t, other[index]) } + mapInPlace { index, t -> elementAlgebra.divide(t, arg[index]) } } } } 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 33765b40a..a537c0629 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 @@ -148,8 +148,8 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe ndArray.divi(value) } - override fun Tensor.divAssign(other: StructureND) { - ndArray.divi(other.ndArray) + override fun Tensor.divAssign(arg: StructureND) { + ndArray.divi(arg.ndArray) } override fun StructureND.variance(dim: Int, keepDim: Boolean): Nd4jArrayStructure = 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 304cc66cc..0a1e09081 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 @@ -53,9 +53,9 @@ public interface TensorPartialDivisionAlgebra> : TensorAlgebra.divAssign(value: T) /** - * Each element of this tensor is divided by each element of the [other] tensor. + * Each element of this tensor is divided by each element of the [arg] tensor. * - * @param other tensor to be divided by. + * @param arg tensor to be divided by. */ - public operator fun Tensor.divAssign(other: StructureND) + public operator fun Tensor.divAssign(arg: StructureND) } 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 83d1f7b35..7353ecab1 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 @@ -74,8 +74,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { } } - override fun StructureND.div(other: StructureND): DoubleTensor { - val broadcast = broadcastTensors(tensor, other.tensor) + override fun StructureND.div(arg: StructureND): DoubleTensor { + val broadcast = broadcastTensors(tensor, arg.tensor) val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.indices.linearSize) { i -> @@ -85,8 +85,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { return DoubleTensor(newThis.shape, resBuffer) } - override fun Tensor.divAssign(other: StructureND) { - val newOther = broadcastTo(other.tensor, tensor.shape) + override fun Tensor.divAssign(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/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index 8b6f6403f..d7aa18c3d 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 @@ -314,11 +314,11 @@ public open class DoubleTensorAlgebra : return DoubleTensor(shape, resBuffer) } - override fun StructureND.div(other: StructureND): DoubleTensor { - checkShapesCompatible(tensor, other) + override fun StructureND.div(arg: StructureND): DoubleTensor { + checkShapesCompatible(tensor, arg) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.mutableBuffer.array()[other.tensor.bufferStart + i] / - other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] + tensor.mutableBuffer.array()[arg.tensor.bufferStart + i] / + arg.tensor.mutableBuffer.array()[arg.tensor.bufferStart + i] } return DoubleTensor(tensor.shape, resBuffer) } @@ -329,11 +329,11 @@ public open class DoubleTensorAlgebra : } } - override fun Tensor.divAssign(other: StructureND) { - checkShapesCompatible(tensor, other) + override fun Tensor.divAssign(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] } }