From dde1db7b0afa27162988fcb1b3543afe6187a4d6 Mon Sep 17 00:00:00 2001 From: Andrei Kislitsyn Date: Fri, 7 May 2021 23:05:37 +0300 Subject: [PATCH 1/3] fixes --- .../kmath/tensors/core/tensorCasts.kt | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt index 921bcf97e..4e43ed1e1 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt @@ -9,17 +9,17 @@ import space.kscience.kmath.tensors.api.Tensor import space.kscience.kmath.tensors.core.internal.tensor /** - * Casts [Tensor] to [DoubleTensor] + * Casts [Tensor] iof [Double] to [DoubleTensor] */ public fun Tensor.toDoubleTensor(): DoubleTensor = this.tensor /** - * Casts [Tensor] to [IntTensor] + * Casts [Tensor] iof [Int] to [IntTensor] */ public fun Tensor.toIntTensor(): IntTensor = this.tensor /** - * @return [DoubleArray] of tensor elements + * Returns [DoubleArray] of tensor elements */ public fun DoubleTensor.toDoubleArray(): DoubleArray { return DoubleArray(numElements) { i -> @@ -28,42 +28,10 @@ public fun DoubleTensor.toDoubleArray(): DoubleArray { } /** - * @return [IntArray] of tensor elements + * Returns [IntArray] of tensor elements */ public fun IntTensor.toIntArray(): IntArray { return IntArray(numElements) { i -> mutableBuffer[bufferStart + i] } } - -/** - * Casts [Array] to [DoubleTensor] - */ -public fun Array.toDoubleTensor(): DoubleTensor { - val n = size - check(n > 0) { "An empty array cannot be casted to tensor" } - val m = first().size - check(m > 0) { "Inner arrays must have at least 1 argument" } - check(all { size == m }) { "Inner arrays must be the same size" } - - val shape = intArrayOf(n, m) - val buffer = this.flatMap { arr -> arr.map { it } }.toDoubleArray() - - return DoubleTensor(shape, buffer, 0) -} - -/** - * Casts [Array] to [IntTensor] - */ -public fun Array.toIntTensor(): IntTensor { - val n = size - check(n > 0) { "An empty array cannot be casted to tensor" } - val m = first().size - check(m > 0) { "Inner arrays must have at least 1 argument" } - check(all { size == m }) { "Inner arrays must be the same size" } - - val shape = intArrayOf(n, m) - val buffer = this.flatMap { arr -> arr.map { it } }.toIntArray() - - return IntTensor(shape, buffer, 0) -} From 11a1624e64d2b779e84d25bf120698b49b400cc1 Mon Sep 17 00:00:00 2001 From: Andrei Kislitsyn Date: Fri, 7 May 2021 23:06:41 +0300 Subject: [PATCH 2/3] fixes --- .../kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt index 4e43ed1e1..142cb2156 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorCasts.kt @@ -9,12 +9,12 @@ import space.kscience.kmath.tensors.api.Tensor import space.kscience.kmath.tensors.core.internal.tensor /** - * Casts [Tensor] iof [Double] to [DoubleTensor] + * Casts [Tensor] of [Double] to [DoubleTensor] */ public fun Tensor.toDoubleTensor(): DoubleTensor = this.tensor /** - * Casts [Tensor] iof [Int] to [IntTensor] + * Casts [Tensor] of [Int] to [IntTensor] */ public fun Tensor.toIntTensor(): IntTensor = this.tensor From 33e27cd99262d64533ccde72bbdb10b47c75d9fe Mon Sep 17 00:00:00 2001 From: Andrei Kislitsyn Date: Fri, 7 May 2021 23:07:02 +0300 Subject: [PATCH 3/3] fixed --- .../space/kscience/kmath/tensors/core/TestBroadcasting.kt | 2 +- .../kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt | 2 +- .../kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt | 2 -- .../space/kscience/kmath/tensors/core/TestDoubleTensor.kt | 2 +- .../kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt | 2 +- 5 files changed, 4 insertions(+), 6 deletions(-) diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt index 672089a3c..35e605fd9 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestBroadcasting.kt @@ -102,4 +102,4 @@ internal class TestBroadcasting { assertTrue(tensor32.mutableBuffer.array() contentEquals doubleArrayOf(490.0, 480.0, 470.0)) } -} \ No newline at end of file +} diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt index 05fedaa09..3b4c615b4 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleAnalyticTensorAlgebra.kt @@ -155,4 +155,4 @@ internal class TestDoubleAnalyticTensorAlgebra { )} } -} \ No newline at end of file +} diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt index fddb37251..347bb683f 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt @@ -194,5 +194,3 @@ private fun DoubleTensorAlgebra.testSVDFor(tensor: DoubleTensor, epsilon: Double assertTrue(tensor.eq(tensorSVD, epsilon)) } - - diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt index d39b5c365..a6c6a7618 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt @@ -86,4 +86,4 @@ internal class TestDoubleTensor { assertEquals(ndArray[intArrayOf(0)], 1.0) } -} \ No newline at end of file +} diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt index 354840719..e7e898008 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensorAlgebra.kt @@ -164,4 +164,4 @@ internal class TestDoubleTensorAlgebra { assertFalse(tensor1.eq(tensor3)) } -} \ No newline at end of file +}