From 3a37b88b5c4808107e2522d5f33eb07440169fb9 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Tue, 9 Mar 2021 21:13:48 +0000 Subject: [PATCH] Fixed getting value test for tensors --- .../space/kscience/kmath/tensors/RealTensorAlgebra.kt | 8 ++------ .../kotlin/space/kscience/kmath/tensors/TensorStrides.kt | 2 +- .../kotlin/space/kscience/kmath/tensors/TestRealTensor.kt | 4 ++-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt index 13c57fc18..366acfb62 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt @@ -3,8 +3,6 @@ package space.kscience.kmath.tensors import space.kscience.kmath.nd.MutableNDBuffer import space.kscience.kmath.structures.RealBuffer import space.kscience.kmath.structures.array -import kotlin.js.JsName -import kotlin.math.abs public class RealTensor( @@ -19,11 +17,9 @@ public class RealTensor( public class RealTensorAlgebra : TensorPartialDivisionAlgebra { - //rename to item? override fun RealTensor.value(): Double { - check(this.dimension == 0) { - // todo change message - "This tensor has shape ${shape.toList()}" + check(this.shape contentEquals intArrayOf(1)) { + "Inconsistent value for tensor of shape ${shape.toList()}" } return this.buffer.array[0] } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorStrides.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorStrides.kt index dbaefe907..0c10203f0 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorStrides.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorStrides.kt @@ -48,5 +48,5 @@ public class TensorStrides(override val shape: IntArray): Strides indexFromOffset(offset, strides, shape.size) override val linearSize: Int - get() = shape.fold(1) { acc, i -> acc * i } + get() = shape.reduce(Int::times) } \ No newline at end of file diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensor.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensor.kt index 7938eb864..d96b00baf 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensor.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensor.kt @@ -9,9 +9,9 @@ import kotlin.test.assertTrue class TestRealTensor { @Test - fun valueTest(){ + fun valueTest() = RealTensorAlgebra { val value = 12.5 - val tensor = RealTensor(IntArray(0), doubleArrayOf(value)) + val tensor = RealTensor(intArrayOf(1), doubleArrayOf(value)) assertEquals(tensor.value(), value) }