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 9fc7d55f3..dbcf35504 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,6 +43,14 @@ 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 d3d0f79a5..d58451bc4 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,6 +42,14 @@ 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 fec358421..3fa9bf861 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,6 +42,14 @@ 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 01b533138..69e1ae9cd 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,6 +47,14 @@ 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 98a5e581f..f73e4c4cd 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,6 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.toDoubleArray -import space.kscience.kmath.structures.toIntArray +import space.kscience.kmath.structures.unsafeToIntArray public class RealLinearOpsTensorAlgebra : LinearOpsTensorAlgebra, @@ -81,7 +80,7 @@ public class RealLinearOpsTensorAlgebra : // todo checks val n = lu.shape[0] val p = lu.zeroesLike() - pivots.buffer.toIntArray().forEachIndexed { i, pivot -> + pivots.buffer.unsafeToIntArray().forEachIndexed { i, pivot -> p[i, pivot] = 1.0 } val l = lu.zeroesLike() 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 10cc0edad..ee0733577 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,6 +1,6 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.toDoubleArray +import space.kscience.kmath.structures.unsafeToDoubleArray public open class RealTensorAlgebra : TensorPartialDivisionAlgebra { @@ -9,7 +9,7 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - other.buffer.toDoubleArray()[i] + this + other.buffer.unsafeToDoubleArray()[i] + this } return RealTensor(other.shape, resBuffer) } @@ -50,34 +50,34 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - newThis.buffer.toDoubleArray()[i] + newOther.buffer.toDoubleArray()[i] + newThis.buffer.unsafeToDoubleArray()[i] + newOther.buffer.unsafeToDoubleArray()[i] } return RealTensor(newThis.shape, resBuffer) } override fun RealTensor.plusAssign(value: Double) { - for (i in this.buffer.toDoubleArray().indices) { - this.buffer.toDoubleArray()[i] += value + for (i in this.buffer.unsafeToDoubleArray().indices) { + this.buffer.unsafeToDoubleArray()[i] += value } } override fun RealTensor.plusAssign(other: RealTensor) { //todo should be change with broadcasting - for (i in this.buffer.toDoubleArray().indices) { - this.buffer.toDoubleArray()[i] += other.buffer.toDoubleArray()[i] + for (i in this.buffer.unsafeToDoubleArray().indices) { + this.buffer.unsafeToDoubleArray()[i] += other.buffer.unsafeToDoubleArray()[i] } } override fun Double.minus(other: RealTensor): RealTensor { val resBuffer = DoubleArray(other.buffer.size) { i -> - this - other.buffer.toDoubleArray()[i] + this - other.buffer.unsafeToDoubleArray()[i] } return RealTensor(other.shape, resBuffer) } override fun RealTensor.minus(value: Double): RealTensor { val resBuffer = DoubleArray(this.buffer.size) { i -> - this.buffer.toDoubleArray()[i] - value + this.buffer.unsafeToDoubleArray()[i] - value } return RealTensor(this.shape, resBuffer) } @@ -87,14 +87,14 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - newThis.buffer.toDoubleArray()[i] - newOther.buffer.toDoubleArray()[i] + newThis.buffer.unsafeToDoubleArray()[i] - newOther.buffer.unsafeToDoubleArray()[i] } return RealTensor(newThis.shape, resBuffer) } override fun RealTensor.minusAssign(value: Double) { - for (i in this.buffer.toDoubleArray().indices) { - this.buffer.toDoubleArray()[i] -= value + for (i in this.buffer.unsafeToDoubleArray().indices) { + this.buffer.unsafeToDoubleArray()[i] -= value } } @@ -105,7 +105,7 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - other.buffer.toDoubleArray()[i] * this + other.buffer.unsafeToDoubleArray()[i] * this } return RealTensor(other.shape, resBuffer) } @@ -116,28 +116,28 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - this.buffer.toDoubleArray()[i] * other.buffer.toDoubleArray()[i] + this.buffer.unsafeToDoubleArray()[i] * other.buffer.unsafeToDoubleArray()[i] } return RealTensor(this.shape, resBuffer) } override fun RealTensor.timesAssign(value: Double) { //todo should be change with broadcasting - for (i in this.buffer.toDoubleArray().indices) { - this.buffer.toDoubleArray()[i] *= value + for (i in this.buffer.unsafeToDoubleArray().indices) { + this.buffer.unsafeToDoubleArray()[i] *= value } } override fun RealTensor.timesAssign(other: RealTensor) { //todo should be change with broadcasting - for (i in this.buffer.toDoubleArray().indices) { - this.buffer.toDoubleArray()[i] *= other.buffer.toDoubleArray()[i] + for (i in this.buffer.unsafeToDoubleArray().indices) { + this.buffer.unsafeToDoubleArray()[i] *= other.buffer.unsafeToDoubleArray()[i] } } override fun RealTensor.unaryMinus(): RealTensor { val resBuffer = DoubleArray(this.buffer.size) { i -> - this.buffer.toDoubleArray()[i].unaryMinus() + this.buffer.unsafeToDoubleArray()[i].unaryMinus() } return RealTensor(this.shape, resBuffer) } @@ -158,14 +158,14 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra