From 6be5caa93f4086e222677a55d15edaca707b16f0 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Fri, 30 Apr 2021 14:44:42 +0100 Subject: [PATCH] No secondary constructors --- .../kmath/tensors/core/BufferedTensor.kt | 25 +++---- .../algebras/BroadcastDoubleTensorAlgebra.kt | 28 ++++---- .../algebras/DoubleLinearOpsTensorAlgebra.kt | 2 +- .../core/algebras/DoubleTensorAlgebra.kt | 68 +++++++++---------- .../kmath/tensors/core/broadcastUtils.kt | 10 +-- .../kscience/kmath/tensors/core/linUtils.kt | 14 ++-- .../kmath/tensors/core/TestBroadcasting.kt | 20 +++--- .../core/TestDoubleAnalyticTensorAlgebra.kt | 2 +- .../core/TestDoubleLinearOpsAlgebra.kt | 4 +- .../kmath/tensors/core/TestDoubleTensor.kt | 5 +- .../tensors/core/TestDoubleTensorAlgebra.kt | 34 +++++----- 11 files changed, 103 insertions(+), 109 deletions(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt index 6e1cab11a..fae59804d 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt @@ -8,7 +8,7 @@ import space.kscience.kmath.tensors.core.algebras.TensorLinearStructure public open class BufferedTensor( override val shape: IntArray, - internal val buffer: MutableBuffer, + internal val mutableBuffer: MutableBuffer, internal val bufferStart: Int ) : TensorStructure { public val linearStructure: TensorLinearStructure @@ -17,10 +17,10 @@ public open class BufferedTensor( public val numElements: Int get() = linearStructure.size - override fun get(index: IntArray): T = buffer[bufferStart + linearStructure.offset(index)] + override fun get(index: IntArray): T = mutableBuffer[bufferStart + linearStructure.offset(index)] override fun set(index: IntArray, value: T) { - buffer[bufferStart + linearStructure.offset(index)] = value + mutableBuffer[bufferStart + linearStructure.offset(index)] = value } override fun elements(): Sequence> = linearStructure.indices().map { @@ -37,33 +37,28 @@ public class IntTensor internal constructor( shape: IntArray, buffer: IntArray, offset: Int = 0 -) : BufferedTensor(shape, IntBuffer(buffer), offset) { - internal constructor(bufferedTensor: BufferedTensor) : - this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart) -} +) : BufferedTensor(shape, IntBuffer(buffer), offset) public class DoubleTensor internal constructor( shape: IntArray, buffer: DoubleArray, offset: Int = 0 ) : BufferedTensor(shape, DoubleBuffer(buffer), offset) { - internal constructor(bufferedTensor: BufferedTensor) : - this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart) - override fun toString(): String = toPrettyString() - } -internal inline fun BufferedTensor.asTensor(): IntTensor = IntTensor(this) -internal inline fun BufferedTensor.asTensor(): DoubleTensor = DoubleTensor(this) +internal fun BufferedTensor.asTensor(): IntTensor = + IntTensor(this.shape, this.mutableBuffer.array(), this.bufferStart) +internal fun BufferedTensor.asTensor(): DoubleTensor = + DoubleTensor(this.shape, this.mutableBuffer.array(), this.bufferStart) -internal inline fun TensorStructure.copyToBufferedTensor(): BufferedTensor = +internal fun TensorStructure.copyToBufferedTensor(): BufferedTensor = BufferedTensor( this.shape, TensorLinearStructure(this.shape).indices().map(this::get).toMutableList().asMutableBuffer(), 0 ) -internal inline fun TensorStructure.toBufferedTensor(): BufferedTensor = when (this) { +internal fun TensorStructure.toBufferedTensor(): BufferedTensor = when (this) { is BufferedTensor -> this is MutableBufferND -> if (this.strides.strides.toIntArray() contentEquals TensorLinearStructure(this.shape).strides) BufferedTensor(this.shape, this.mutableBuffer, 0) else this.copyToBufferedTensor() diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/BroadcastDoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/BroadcastDoubleTensorAlgebra.kt index fa64092d6..350d939af 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/BroadcastDoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/BroadcastDoubleTensorAlgebra.kt @@ -21,7 +21,7 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.linearStructure.size) { i -> - newThis.buffer.array()[i] + newOther.buffer.array()[i] + newThis.mutableBuffer.array()[i] + newOther.mutableBuffer.array()[i] } return DoubleTensor(newThis.shape, resBuffer) } @@ -29,8 +29,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { override fun TensorStructure.plusAssign(other: TensorStructure) { val newOther = broadcastTo(other.tensor, tensor.shape) for (i in 0 until tensor.linearStructure.size) { - tensor.buffer.array()[tensor.bufferStart + i] += - newOther.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] += + newOther.mutableBuffer.array()[tensor.bufferStart + i] } } @@ -39,7 +39,7 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.linearStructure.size) { i -> - newThis.buffer.array()[i] - newOther.buffer.array()[i] + newThis.mutableBuffer.array()[i] - newOther.mutableBuffer.array()[i] } return DoubleTensor(newThis.shape, resBuffer) } @@ -47,8 +47,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { override fun TensorStructure.minusAssign(other: TensorStructure) { val newOther = broadcastTo(other.tensor, tensor.shape) for (i in 0 until tensor.linearStructure.size) { - tensor.buffer.array()[tensor.bufferStart + i] -= - newOther.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] -= + newOther.mutableBuffer.array()[tensor.bufferStart + i] } } @@ -57,8 +57,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.linearStructure.size) { i -> - newThis.buffer.array()[newThis.bufferStart + i] * - newOther.buffer.array()[newOther.bufferStart + i] + newThis.mutableBuffer.array()[newThis.bufferStart + i] * + newOther.mutableBuffer.array()[newOther.bufferStart + i] } return DoubleTensor(newThis.shape, resBuffer) } @@ -66,8 +66,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { override fun TensorStructure.timesAssign(other: TensorStructure) { val newOther = broadcastTo(other.tensor, tensor.shape) for (i in 0 until tensor.linearStructure.size) { - tensor.buffer.array()[tensor.bufferStart + i] *= - newOther.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] *= + newOther.mutableBuffer.array()[tensor.bufferStart + i] } } @@ -76,8 +76,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.linearStructure.size) { i -> - newThis.buffer.array()[newOther.bufferStart + i] / - newOther.buffer.array()[newOther.bufferStart + i] + newThis.mutableBuffer.array()[newOther.bufferStart + i] / + newOther.mutableBuffer.array()[newOther.bufferStart + i] } return DoubleTensor(newThis.shape, resBuffer) } @@ -85,8 +85,8 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { override fun TensorStructure.divAssign(other: TensorStructure) { val newOther = broadcastTo(other.tensor, tensor.shape) for (i in 0 until tensor.linearStructure.size) { - tensor.buffer.array()[tensor.bufferStart + i] /= - newOther.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] /= + newOther.mutableBuffer.array()[tensor.bufferStart + i] } } } \ No newline at end of file diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt index 975755656..ab73b25b1 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleLinearOpsTensorAlgebra.kt @@ -106,7 +106,7 @@ public object DoubleLinearOpsTensorAlgebra : val matrixSize = matrix.shape.reduce { acc, i -> acc * i } val curMatrix = DoubleTensor( matrix.shape, - matrix.buffer.array().slice(matrix.bufferStart until matrix.bufferStart + matrixSize).toDoubleArray() + matrix.mutableBuffer.array().slice(matrix.bufferStart until matrix.bufferStart + matrixSize).toDoubleArray() ) svdHelper(curMatrix, USV, m, n, epsilon) } diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt index 8729358cc..f428b9d2e 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/algebras/DoubleTensorAlgebra.kt @@ -29,7 +29,7 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { check(tensor.shape contentEquals intArrayOf(1)) { "Inconsistent value for tensor of shape ${shape.toList()}" } - return tensor.buffer.array()[tensor.bufferStart] + return tensor.mutableBuffer.array()[tensor.bufferStart] } public fun fromArray(shape: IntArray, buffer: DoubleArray): DoubleTensor { @@ -43,7 +43,7 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { val lastShape = tensor.shape.drop(1).toIntArray() val newShape = if (lastShape.isNotEmpty()) lastShape else intArrayOf(1) val newStart = newShape.reduce(Int::times) * i + tensor.bufferStart - return DoubleTensor(newShape, tensor.buffer.array(), newStart) + return DoubleTensor(newShape, tensor.mutableBuffer.array(), newStart) } public fun full(value: Double, shape: IntArray): DoubleTensor { @@ -77,12 +77,12 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { } public fun TensorStructure.copy(): DoubleTensor { - return DoubleTensor(tensor.shape, tensor.buffer.array().copyOf(), tensor.bufferStart) + return DoubleTensor(tensor.shape, tensor.mutableBuffer.array().copyOf(), tensor.bufferStart) } override fun Double.plus(other: TensorStructure): DoubleTensor { val resBuffer = DoubleArray(other.tensor.numElements) { i -> - other.tensor.buffer.array()[other.tensor.bufferStart + i] + this + other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] + this } return DoubleTensor(other.shape, resBuffer) } @@ -92,35 +92,35 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { override fun TensorStructure.plus(other: TensorStructure): DoubleTensor { checkShapesCompatible(tensor, other.tensor) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[i] + other.tensor.buffer.array()[i] + tensor.mutableBuffer.array()[i] + other.tensor.mutableBuffer.array()[i] } return DoubleTensor(tensor.shape, resBuffer) } override fun TensorStructure.plusAssign(value: Double) { for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] += value + tensor.mutableBuffer.array()[tensor.bufferStart + i] += value } } override fun TensorStructure.plusAssign(other: TensorStructure) { checkShapesCompatible(tensor, other.tensor) for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] += - other.tensor.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] += + other.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } override fun Double.minus(other: TensorStructure): DoubleTensor { val resBuffer = DoubleArray(other.tensor.numElements) { i -> - this - other.tensor.buffer.array()[other.tensor.bufferStart + i] + this - other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] } return DoubleTensor(other.shape, resBuffer) } override fun TensorStructure.minus(value: Double): DoubleTensor { val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[tensor.bufferStart + i] - value + tensor.mutableBuffer.array()[tensor.bufferStart + i] - value } return DoubleTensor(tensor.shape, resBuffer) } @@ -128,28 +128,28 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { override fun TensorStructure.minus(other: TensorStructure): DoubleTensor { checkShapesCompatible(tensor, other) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[i] - other.tensor.buffer.array()[i] + tensor.mutableBuffer.array()[i] - other.tensor.mutableBuffer.array()[i] } return DoubleTensor(tensor.shape, resBuffer) } override fun TensorStructure.minusAssign(value: Double) { for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] -= value + tensor.mutableBuffer.array()[tensor.bufferStart + i] -= value } } override fun TensorStructure.minusAssign(other: TensorStructure) { checkShapesCompatible(tensor, other) for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] -= - other.tensor.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] -= + other.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } override fun Double.times(other: TensorStructure): DoubleTensor { val resBuffer = DoubleArray(other.tensor.numElements) { i -> - other.tensor.buffer.array()[other.tensor.bufferStart + i] * this + other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] * this } return DoubleTensor(other.shape, resBuffer) } @@ -159,36 +159,36 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { override fun TensorStructure.times(other: TensorStructure): DoubleTensor { checkShapesCompatible(tensor, other) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[tensor.bufferStart + i] * - other.tensor.buffer.array()[other.tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] * + other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] } return DoubleTensor(tensor.shape, resBuffer) } override fun TensorStructure.timesAssign(value: Double) { for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] *= value + tensor.mutableBuffer.array()[tensor.bufferStart + i] *= value } } override fun TensorStructure.timesAssign(other: TensorStructure) { checkShapesCompatible(tensor, other) for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] *= - other.tensor.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] *= + other.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } override fun Double.div(other: TensorStructure): DoubleTensor { val resBuffer = DoubleArray(other.tensor.numElements) { i -> - this / other.tensor.buffer.array()[other.tensor.bufferStart + i] + this / other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] } return DoubleTensor(other.shape, resBuffer) } override fun TensorStructure.div(value: Double): DoubleTensor { val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[tensor.bufferStart + i] / value + tensor.mutableBuffer.array()[tensor.bufferStart + i] / value } return DoubleTensor(shape, resBuffer) } @@ -196,29 +196,29 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { override fun TensorStructure.div(other: TensorStructure): DoubleTensor { checkShapesCompatible(tensor, other) val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[other.tensor.bufferStart + i] / - other.tensor.buffer.array()[other.tensor.bufferStart + i] + tensor.mutableBuffer.array()[other.tensor.bufferStart + i] / + other.tensor.mutableBuffer.array()[other.tensor.bufferStart + i] } return DoubleTensor(tensor.shape, resBuffer) } override fun TensorStructure.divAssign(value: Double) { for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] /= value + tensor.mutableBuffer.array()[tensor.bufferStart + i] /= value } } override fun TensorStructure.divAssign(other: TensorStructure) { checkShapesCompatible(tensor, other) for (i in 0 until tensor.numElements) { - tensor.buffer.array()[tensor.bufferStart + i] /= - other.tensor.buffer.array()[tensor.bufferStart + i] + tensor.mutableBuffer.array()[tensor.bufferStart + i] /= + other.tensor.mutableBuffer.array()[tensor.bufferStart + i] } } override fun TensorStructure.unaryMinus(): DoubleTensor { val resBuffer = DoubleArray(tensor.numElements) { i -> - tensor.buffer.array()[tensor.bufferStart + i].unaryMinus() + tensor.mutableBuffer.array()[tensor.bufferStart + i].unaryMinus() } return DoubleTensor(tensor.shape, resBuffer) } @@ -241,8 +241,8 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { newMultiIndex[ii] = newMultiIndex[jj].also { newMultiIndex[jj] = newMultiIndex[ii] } val linearIndex = resTensor.linearStructure.offset(newMultiIndex) - resTensor.buffer.array()[linearIndex] = - tensor.buffer.array()[tensor.bufferStart + offset] + resTensor.mutableBuffer.array()[linearIndex] = + tensor.mutableBuffer.array()[tensor.bufferStart + offset] } return resTensor } @@ -250,7 +250,7 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { override fun TensorStructure.view(shape: IntArray): DoubleTensor { checkView(tensor, shape) - return DoubleTensor(shape, tensor.buffer.array(), tensor.bufferStart) + return DoubleTensor(shape, tensor.mutableBuffer.array(), tensor.bufferStart) } override fun TensorStructure.viewAs(other: TensorStructure): DoubleTensor { @@ -259,7 +259,7 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { override infix fun TensorStructure.dot(other: TensorStructure): DoubleTensor { if (tensor.shape.size == 1 && other.shape.size == 1) { - return DoubleTensor(intArrayOf(1), doubleArrayOf(tensor.times(other).tensor.buffer.array().sum())) + return DoubleTensor(intArrayOf(1), doubleArrayOf(tensor.times(other).tensor.mutableBuffer.array().sum())) } var newThis = tensor.copy() @@ -361,7 +361,7 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { public fun TensorStructure.map(transform: (Double) -> Double): DoubleTensor { return DoubleTensor( tensor.shape, - tensor.buffer.array().map { transform(it) }.toDoubleArray(), + tensor.mutableBuffer.array().map { transform(it) }.toDoubleArray(), tensor.bufferStart ) } @@ -382,7 +382,7 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { return false } for (i in 0 until n) { - if (!eqFunction(tensor.buffer[tensor.bufferStart + i], other.tensor.buffer[other.tensor.bufferStart + i])) { + if (!eqFunction(tensor.mutableBuffer[tensor.bufferStart + i], other.tensor.mutableBuffer[other.tensor.bufferStart + i])) { return false } } diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/broadcastUtils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/broadcastUtils.kt index 71ccbf97b..2d4d6f0f7 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/broadcastUtils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/broadcastUtils.kt @@ -18,8 +18,8 @@ internal inline fun multiIndexBroadCasting(tensor: DoubleTensor, resTensor: Doub } val curLinearIndex = tensor.linearStructure.offset(curMultiIndex) - resTensor.buffer.array()[linearIndex] = - tensor.buffer.array()[tensor.bufferStart + curLinearIndex] + resTensor.mutableBuffer.array()[linearIndex] = + tensor.mutableBuffer.array()[tensor.bufferStart + curLinearIndex] } } @@ -113,7 +113,7 @@ internal inline fun broadcastOuterTensors(vararg tensors: DoubleTensor): List BufferedTensor.vectorSequence(): Sequence BufferedTensor.matrixSequence(): Sequence