From 5e94610e2862fc2ecfe5e4f9e709ecc2ab3f428a Mon Sep 17 00:00:00 2001 From: Andrei Kislitsyn Date: Fri, 19 Mar 2021 17:51:30 +0300 Subject: [PATCH] imp full and remove copypaste in geners --- .../kscience/kmath/tensors/BufferedTensor.kt | 34 +++++++------------ .../kmath/tensors/DoubleTensorAlgebra.kt | 34 +++++++------------ .../kscience/kmath/tensors/TensorAlgebra.kt | 12 +++---- 3 files changed, 31 insertions(+), 49 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt index c48e47f4c..b9ebf578a 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt @@ -31,28 +31,6 @@ public open class BufferedTensor( override fun hashCode(): Int = 0 - // todo rename to vector - public inline fun forEachVector(vectorAction : (MutableStructure1D) -> Unit): Unit { - check(shape.size >= 1) {"todo"} - val vectorOffset = strides.strides[0] - val vectorShape = intArrayOf(shape.last()) - for (offset in 0 until numel step vectorOffset) { - val vector = BufferedTensor(vectorShape, buffer, offset).as1D() - vectorAction(vector) - } - } - - public inline fun forEachMatrix(matrixAction : (MutableStructure2D) -> Unit): Unit { - check(shape.size >= 2) {"todo"} - val matrixOffset = strides.strides[1] - val matrixShape = intArrayOf(shape[shape.size - 2], shape.last()) //todo better way? - for (offset in 0 until numel step matrixOffset) { - val matrix = BufferedTensor(matrixShape, buffer, offset).as2D() - matrixAction(matrix) - } - } - // todo remove code copy-pasting - public fun vectorSequence(): Sequence> = sequence { check(shape.size >= 1) {"todo"} val vectorOffset = strides.strides[0] @@ -73,6 +51,18 @@ public open class BufferedTensor( } } + public inline fun forEachVector(vectorAction : (MutableStructure1D) -> Unit): Unit { + for (vector in vectorSequence()){ + vectorAction(vector) + } + } + + public inline fun forEachMatrix(matrixAction : (MutableStructure2D) -> Unit): Unit { + for (matrix in matrixSequence()){ + matrixAction(matrix) + } + } + } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt index 675be2f33..6459f4510 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt @@ -10,23 +10,24 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra> { - public fun zeros(shape: IntArray): TensorType - public fun TensorType.zeroesLike(): TensorType // mb it shouldn't be tensor but algebra method (like in numpy/torch) ? - public fun ones(shape: IntArray): TensorType - public fun TensorType.onesLike(): TensorType - - //https://pytorch.org/docs/stable/generated/torch.full.html public fun full(shape: IntArray, value: T): TensorType + public fun ones(shape: IntArray): TensorType + public fun zeros(shape: IntArray): TensorType + //https://pytorch.org/docs/stable/generated/torch.full_like.html#torch.full_like public fun TensorType.fullLike(value: T): TensorType + public fun TensorType.zeroesLike(): TensorType + public fun TensorType.onesLike(): TensorType + //https://pytorch.org/docs/stable/generated/torch.eye.html public fun eye(n: Int): TensorType -- 2.34.1