From 4e4690e5104af58d48fd8f08d2fdbbb132f26751 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Mon, 15 Mar 2021 08:48:31 +0000 Subject: [PATCH] unsafe buffer casts moved to internal utils --- .../kscience/kmath/structures/FloatBuffer.kt | 8 ----- .../kscience/kmath/structures/IntBuffer.kt | 8 ----- .../kscience/kmath/structures/LongBuffer.kt | 8 ----- .../kscience/kmath/structures/RealBuffer.kt | 8 ----- .../tensors/RealLinearOpsTensorAlgebra.kt | 2 -- .../kmath/tensors/RealTensorAlgebra.kt | 2 -- .../space/kscience/kmath/tensors/utils.kt | 36 +++++++++++++++++-- .../kmath/tensors/TestRealTensorAlgebra.kt | 26 +++++++------- 8 files changed, 47 insertions(+), 51 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt index dbcf35504..9fc7d55f3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt @@ -43,14 +43,6 @@ public fun Buffer.toFloatArray(): FloatArray = when(this) { else -> FloatArray(size, ::get) } -/** - * Returns a reference to [FloatArray] containing all of the elements of this [Buffer]. - */ -public fun Buffer.unsafeToFloatArray(): FloatArray = when(this) { - is FloatBuffer -> array - else -> throw RuntimeException("Failed to cast Buffer to FloatArray") -} - /** * Returns [FloatBuffer] over this array. * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt index d58451bc4..d3d0f79a5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt @@ -42,14 +42,6 @@ public fun Buffer.toIntArray(): IntArray = when(this) { else -> IntArray(size, ::get) } -/** - * Returns a reference to [IntArray] containing all of the elements of this [Buffer]. - */ -public fun Buffer.unsafeToIntArray(): IntArray = when(this) { - is IntBuffer -> array - else -> throw RuntimeException("Failed to cast Buffer to IntArray") -} - /** * Returns [IntBuffer] over this array. * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt index 3fa9bf861..fec358421 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt @@ -42,14 +42,6 @@ public fun Buffer.toLongArray(): LongArray = when(this) { else -> LongArray(size, ::get) } -/** - * Returns a reference to [LongArray] containing all of the elements of this [Buffer]. - */ -public fun Buffer.unsafeToLongArray(): LongArray = when(this) { - is LongBuffer -> array - else -> throw RuntimeException("Failed to cast Buffer to LongArray") -} - /** * Returns [LongBuffer] over this array. * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt index 69e1ae9cd..01b533138 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt @@ -47,14 +47,6 @@ public fun Buffer.toDoubleArray(): DoubleArray = when(this) { else -> DoubleArray(size, ::get) } -/** - * Returns a reference to [DoubleArray] containing all of the elements of this [Buffer]. - */ -public fun Buffer.unsafeToDoubleArray(): DoubleArray = when(this) { - is RealBuffer -> array - else -> throw RuntimeException("Failed to cast Buffer to DoubleArray") -} - /** * Returns [RealBuffer] over this array. * diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt index f73e4c4cd..dcd740356 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt @@ -1,7 +1,5 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.unsafeToIntArray - public class RealLinearOpsTensorAlgebra : LinearOpsTensorAlgebra, RealTensorAlgebra() 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 ee0733577..83a513a7a 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 @@ -1,7 +1,5 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.unsafeToDoubleArray - public open class RealTensorAlgebra : TensorPartialDivisionAlgebra { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/utils.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/utils.kt index 04b4ddcbf..b03fb7dd7 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/utils.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/utils.kt @@ -1,6 +1,6 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.unsafeToDoubleArray +import space.kscience.kmath.structures.* import kotlin.math.max @@ -93,4 +93,36 @@ internal inline fun , internal inline fun , TorchTensorAlgebraType : TensorAlgebra> TorchTensorAlgebraType.checkView(a: TensorType, shape: IntArray): Unit = - check(a.shape.reduce(Int::times) == shape.reduce(Int::times)) \ No newline at end of file + check(a.shape.reduce(Int::times) == shape.reduce(Int::times)) + +/** + * Returns a reference to [IntArray] containing all of the elements of this [Buffer]. + */ +internal fun Buffer.unsafeToIntArray(): IntArray = when(this) { + is IntBuffer -> array + else -> throw RuntimeException("Failed to cast Buffer to IntArray") +} + +/** + * Returns a reference to [LongArray] containing all of the elements of this [Buffer]. + */ +internal fun Buffer.unsafeToLongArray(): LongArray = when(this) { + is LongBuffer -> array + else -> throw RuntimeException("Failed to cast Buffer to LongArray") +} + +/** + * Returns a reference to [FloatArray] containing all of the elements of this [Buffer]. + */ +internal fun Buffer.unsafeToFloatArray(): FloatArray = when(this) { + is FloatBuffer -> array + else -> throw RuntimeException("Failed to cast Buffer to FloatArray") +} + +/** + * Returns a reference to [DoubleArray] containing all of the elements of this [Buffer]. + */ +internal fun Buffer.unsafeToDoubleArray(): DoubleArray = when(this) { + is RealBuffer -> array + else -> throw RuntimeException("Failed to cast Buffer to DoubleArray") +} diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensorAlgebra.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensorAlgebra.kt index 80e3c3eab..72bbf1787 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensorAlgebra.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestRealTensorAlgebra.kt @@ -1,6 +1,6 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.toDoubleArray + import kotlin.test.Test import kotlin.test.assertTrue @@ -10,7 +10,7 @@ class TestRealTensorAlgebra { fun doublePlus() = RealTensorAlgebra { val tensor = RealTensor(intArrayOf(2), doubleArrayOf(1.0, 2.0)) val res = 10.0 + tensor - assertTrue(res.buffer.toDoubleArray() contentEquals doubleArrayOf(11.0,12.0)) + assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(11.0,12.0)) } @Test @@ -18,7 +18,7 @@ class TestRealTensorAlgebra { val tensor = RealTensor(intArrayOf(1), doubleArrayOf(0.0)) val res = tensor.transpose(0, 0) - assertTrue(res.buffer.toDoubleArray() contentEquals doubleArrayOf(0.0)) + assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(0.0)) assertTrue(res.shape contentEquals intArrayOf(1)) } @@ -27,7 +27,7 @@ class TestRealTensorAlgebra { val tensor = RealTensor(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val res = tensor.transpose(1, 0) - assertTrue(res.buffer.toDoubleArray() contentEquals doubleArrayOf(1.0, 3.0, 5.0, 2.0, 4.0, 6.0)) + assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 3.0, 5.0, 2.0, 4.0, 6.0)) assertTrue(res.shape contentEquals intArrayOf(2, 3)) } @@ -42,9 +42,9 @@ class TestRealTensorAlgebra { assertTrue(res02.shape contentEquals intArrayOf(3, 2, 1)) assertTrue(res12.shape contentEquals intArrayOf(1, 3, 2)) - assertTrue(res01.buffer.toDoubleArray() contentEquals doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) - assertTrue(res02.buffer.toDoubleArray() contentEquals doubleArrayOf(1.0, 4.0, 2.0, 5.0, 3.0, 6.0)) - assertTrue(res12.buffer.toDoubleArray() contentEquals doubleArrayOf(1.0, 4.0, 2.0, 5.0, 3.0, 6.0)) + assertTrue(res01.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) + assertTrue(res02.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 4.0, 2.0, 5.0, 3.0, 6.0)) + assertTrue(res12.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 4.0, 2.0, 5.0, 3.0, 6.0)) } @Test @@ -70,9 +70,9 @@ class TestRealTensorAlgebra { assertTrue(res[1].shape contentEquals intArrayOf(1, 2, 3)) assertTrue(res[2].shape contentEquals intArrayOf(1, 2, 3)) - assertTrue(res[0].buffer.toDoubleArray() contentEquals doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) - assertTrue(res[1].buffer.toDoubleArray() contentEquals doubleArrayOf(10.0, 20.0, 30.0, 10.0, 20.0, 30.0)) - assertTrue(res[2].buffer.toDoubleArray() contentEquals doubleArrayOf(500.0, 500.0, 500.0, 500.0, 500.0, 500.0)) + assertTrue(res[0].buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) + assertTrue(res[1].buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(10.0, 20.0, 30.0, 10.0, 20.0, 30.0)) + assertTrue(res[2].buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(500.0, 500.0, 500.0, 500.0, 500.0, 500.0)) } @Test @@ -82,14 +82,14 @@ class TestRealTensorAlgebra { val tensor3 = RealTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) assertTrue((tensor2 - tensor1).shape contentEquals intArrayOf(2, 3)) - assertTrue((tensor2 - tensor1).buffer.toDoubleArray() contentEquals doubleArrayOf(9.0, 18.0, 27.0, 6.0, 15.0, 24.0)) + assertTrue((tensor2 - tensor1).buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(9.0, 18.0, 27.0, 6.0, 15.0, 24.0)) assertTrue((tensor3 - tensor1).shape contentEquals intArrayOf(1, 2, 3)) - assertTrue((tensor3 - tensor1).buffer.toDoubleArray() + assertTrue((tensor3 - tensor1).buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(499.0, 498.0, 497.0, 496.0, 495.0, 494.0)) assertTrue((tensor3 - tensor2).shape contentEquals intArrayOf(1, 1, 3)) - assertTrue((tensor3 - tensor2).buffer.toDoubleArray() contentEquals doubleArrayOf(490.0, 480.0, 470.0)) + assertTrue((tensor3 - tensor2).buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(490.0, 480.0, 470.0)) } } \ No newline at end of file