From 287e2aeba2ba274508f42f1ab0a839ed69861bc1 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Fri, 23 Apr 2021 14:22:52 +0100 Subject: [PATCH] div operator --- .../kmath/tensors/api/TensorPartialDivisionAlgebra.kt | 1 + .../kmath/tensors/core/algebras/DoubleTensorAlgebra.kt | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) 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 075e86e61..4dab3dd07 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 @@ -8,6 +8,7 @@ package space.kscience.kmath.tensors.api // https://proofwiki.org/wiki/Definition:Division_Algebra public interface TensorPartialDivisionAlgebra : TensorAlgebra { + public operator fun T.div(other: TensorStructure): TensorStructure public operator fun TensorStructure.div(value: T): TensorStructure public operator fun TensorStructure.div(other: TensorStructure): TensorStructure public operator fun TensorStructure.divAssign(value: T) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt index 651db2c21..b61a484d0 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt @@ -178,13 +178,15 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { } } - override fun TensorStructure.div(value: Double): DoubleTensor { - val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[tensor.bufferStart + i] / value + override fun Double.div(other: TensorStructure): DoubleTensor { + val resBuffer = DoubleArray(other.tensor.numElements) { i -> + other.tensor.buffer.array()[other.tensor.bufferStart + i] / this } - return DoubleTensor(tensor.shape, resBuffer) + return DoubleTensor(other.shape, resBuffer) } + override fun TensorStructure.div(value: Double): DoubleTensor = value / tensor + override fun TensorStructure.div(other: TensorStructure): DoubleTensor { checkShapesCompatible(tensor, other) val resBuffer = DoubleArray(tensor.numElements) { i ->