From a1cbd7a457a93619ad3033c66248b522137e4c46 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Thu, 6 May 2021 09:48:43 +0100 Subject: [PATCH 1/3] TensorLinearStructure doc --- .../kmath/tensors/core/algebras/TensorLinearStructure.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt index 5fbc7390f..d69d342ad 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt @@ -56,7 +56,12 @@ internal fun stepIndex(index: IntArray, shape: IntArray, nDim: Int): IntArray { return res } - +/** + * This [Strides] implemetation follow the last dimension first convention + * For more information: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.strides.html + * + * @param shape the shape of the tensor. + */ public class TensorLinearStructure(override val shape: IntArray) : Strides { override val strides: IntArray @@ -65,6 +70,7 @@ public class TensorLinearStructure(override val shape: IntArray) : Strides override fun index(offset: Int): IntArray = indexFromOffset(offset, strides, shape.size) + // TODO: documentation (Alya) public fun stepIndex(index: IntArray): IntArray = stepIndex(index, shape, shape.size) From 477e64e4d396ebff3a66f487b2b0cab7f507e6bd Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Thu, 6 May 2021 09:51:59 +0100 Subject: [PATCH 2/3] Typos corrected --- .../kmath/tensors/core/algebras/TensorLinearStructure.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt index d69d342ad..a5c01af55 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt @@ -57,7 +57,7 @@ internal fun stepIndex(index: IntArray, shape: IntArray, nDim: Int): IntArray { } /** - * This [Strides] implemetation follow the last dimension first convention + * This [Strides] implementation follows the last dimension first convention * For more information: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.strides.html * * @param shape the shape of the tensor. From 16bed539977c516645d0a980f4f22578a6fbec11 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Thu, 6 May 2021 09:59:58 +0100 Subject: [PATCH 3/3] Drop unused functionality in TensorLinearStructure --- .../algebras/DoubleLinearOpsTensorAlgebra.kt | 2 +- .../core/algebras/TensorLinearStructure.kt | 27 +------------------ 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt index d17dc70fe..430482613 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt @@ -155,7 +155,7 @@ public object DoubleLinearOpsTensorAlgebra : * @return triple `(U, S, V)`. */ public fun Tensor.svd(epsilon: Double): Triple { - val size = tensor.linearStructure.dim + val size = tensor.dimension val commonShape = tensor.shape.sliceArray(0 until size - 2) val (n, m) = tensor.shape.sliceArray(size - 2 until size) val uTensor = zeros(commonShape + intArrayOf(min(n, m), n)) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt index a5c01af55..68aa03311 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/TensorLinearStructure.kt @@ -39,45 +39,20 @@ internal fun indexFromOffset(offset: Int, strides: IntArray, nDim: Int): IntArra return res } -internal fun stepIndex(index: IntArray, shape: IntArray, nDim: Int): IntArray { - val res = index.copyOf() - var current = nDim - 1 - var carry = 0 - - do { - res[current]++ - if (res[current] >= shape[current]) { - carry = 1 - res[current] = 0 - } - current-- - } while (carry != 0 && current >= 0) - - return res -} - /** * This [Strides] implementation follows the last dimension first convention * For more information: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.strides.html * * @param shape the shape of the tensor. */ -public class TensorLinearStructure(override val shape: IntArray) : Strides -{ +public class TensorLinearStructure(override val shape: IntArray) : Strides { override val strides: IntArray get() = stridesFromShape(shape) override fun index(offset: Int): IntArray = indexFromOffset(offset, strides, shape.size) - // TODO: documentation (Alya) - public fun stepIndex(index: IntArray): IntArray = - stepIndex(index, shape, shape.size) - override val linearSize: Int get() = shape.reduce(Int::times) - public val dim: Int - get() = shape.size - } \ No newline at end of file