From 4aa1df0628c5d531b518691eb8ab22b907946eb0 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Tue, 2 Mar 2021 09:32:36 +0000 Subject: [PATCH] Safe item method and view_as added --- .../space/kscience/kmath/tensors/RealTensorAlgebra.kt | 10 +++++++++- .../space/kscience/kmath/tensors/TensorAlgebra.kt | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) 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 6023d2b72..24612a6aa 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 @@ -14,9 +14,13 @@ public class RealTensor( TensorStrides(shape), RealBuffer(buffer) ) { - override fun item(): Double = buffer[0] + override fun item(): Double { + check(buffer.size > 0) { "The tensor is empty" } + return buffer[0] + } } + public class RealTensorAlgebra : TensorPartialDivisionAlgebra { override fun Double.plus(other: RealTensor): RealTensor { @@ -116,6 +120,10 @@ public class RealTensorAlgebra : TensorPartialDivisionAlgebra>{ //https://pytorch.org/docs/stable/tensor_view.html public fun TensorType.view(shape: IntArray): TensorType + public fun TensorType.view_as(other: TensorType): TensorType //https://pytorch.org/docs/stable/generated/torch.abs.html public fun TensorType.abs(): TensorType