From f66ac653e0264881d1197a395be84bdb37902b23 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Mon, 15 Mar 2021 16:59:50 +0000 Subject: [PATCH] RealTensor to DoubleTensor rename --- .../kscience/kmath/tensors/BufferedTensor.kt | 2 +- .../tensors/DoubleAnalyticTensorAlgebra.kt | 150 ++++++++++++++++++ ...bra.kt => DoubleLinearOpsTensorAlgebra.kt} | 26 +-- ...ensorAlgebra.kt => DoubleTensorAlgebra.kt} | 128 +++++++-------- .../tensors/RealAnalyticTensorAlgebra.kt | 150 ------------------ .../space/kscience/kmath/tensors/utils.kt | 6 +- .../kscience/kmath/tensors/TestRealTensor.kt | 4 +- .../kmath/tensors/TestRealTensorAlgebra.kt | 20 +-- 8 files changed, 243 insertions(+), 243 deletions(-) create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt rename kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/{RealLinearOpsTensorAlgebra.kt => DoubleLinearOpsTensorAlgebra.kt} (73%) rename kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/{RealTensorAlgebra.kt => DoubleTensorAlgebra.kt} (54%) delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealAnalyticTensorAlgebra.kt diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt index 29605024d..066fb8708 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/BufferedTensor.kt @@ -41,7 +41,7 @@ public class FloatTensor( buffer: FloatArray ) : BufferedTensor(shape, FloatBuffer(buffer)) -public class RealTensor( +public class DoubleTensor( shape: IntArray, buffer: DoubleArray ) : BufferedTensor(shape, RealBuffer(buffer)) \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt new file mode 100644 index 000000000..702049a20 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt @@ -0,0 +1,150 @@ +package space.kscience.kmath.tensors + +public class RealAnalyticTensorAlgebra: + AnalyticTensorAlgebra, + RealTensorAlgebra() +{ + override fun DoubleTensor.exp(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.log(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.sqrt(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.square(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.cos(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.acos(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.cosh(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.acosh(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.sin(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.asin(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.sinh(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.asinh(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.tan(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.atan(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.tanh(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.atanh(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.ceil(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.floor(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.clamp(min: Double, max: Double): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.erf(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.erfinv(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.erfc(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.lerp(end: DoubleTensor, weight: DoubleTensor): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.lgamma(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.logit(eps: Double): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.igamma(other: DoubleTensor): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.igammac(other: DoubleTensor): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.mvlgamma(dimensions: Int): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.polygamma(order: Int): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.pow(exponent: Double): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.round(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.sigmoid(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.sinc(): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.heaviside(values: DoubleTensor): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.trapz(xValues: DoubleTensor, dim: Int): DoubleTensor { + TODO("Not yet implemented") + } + +} + +public inline fun RealAnalyticTensorAlgebra(block: RealAnalyticTensorAlgebra.() -> R): R = + RealAnalyticTensorAlgebra().block() \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleLinearOpsTensorAlgebra.kt similarity index 73% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleLinearOpsTensorAlgebra.kt index 6b1957b8c..5fd8c4151 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealLinearOpsTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleLinearOpsTensorAlgebra.kt @@ -1,13 +1,13 @@ package space.kscience.kmath.tensors public class RealLinearOpsTensorAlgebra : - LinearOpsTensorAlgebra, + LinearOpsTensorAlgebra, RealTensorAlgebra() { - override fun eye(n: Int): RealTensor { + override fun eye(n: Int): DoubleTensor { val shape = intArrayOf(n, n) val buffer = DoubleArray(n * n) { 0.0 } - val res = RealTensor(shape, buffer) + val res = DoubleTensor(shape, buffer) for (i in 0 until n) { res[intArrayOf(i, i)] = 1.0 } @@ -15,16 +15,16 @@ public class RealLinearOpsTensorAlgebra : } - override fun RealTensor.dot(other: RealTensor): RealTensor { + override fun DoubleTensor.dot(other: DoubleTensor): DoubleTensor { TODO("Alya") } - override fun diagonalEmbedding(diagonalEntries: RealTensor, offset: Int, dim1: Int, dim2: Int): RealTensor { + override fun diagonalEmbedding(diagonalEntries: DoubleTensor, offset: Int, dim1: Int, dim2: Int): DoubleTensor { TODO("Alya") } - override fun RealTensor.lu(): Pair { + override fun DoubleTensor.lu(): Pair { // todo checks val lu = this.copy() val m = this.shape[0] @@ -74,7 +74,7 @@ public class RealLinearOpsTensorAlgebra : return Pair(lu, IntTensor(intArrayOf(m), pivot)) } - override fun luPivot(lu: RealTensor, pivots: IntTensor): Triple { + override fun luPivot(lu: DoubleTensor, pivots: IntTensor): Triple { // todo checks val n = lu.shape[0] val p = lu.zeroesLike() @@ -100,28 +100,28 @@ public class RealLinearOpsTensorAlgebra : return Triple(p, l, u) } - override fun RealTensor.det(): RealTensor { + override fun DoubleTensor.det(): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.inv(): RealTensor { + override fun DoubleTensor.inv(): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.cholesky(): RealTensor { + override fun DoubleTensor.cholesky(): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.qr(): RealTensor { + override fun DoubleTensor.qr(): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.svd(): Triple { + override fun DoubleTensor.svd(): Triple { TODO("Not yet implemented") } - override fun RealTensor.symEig(eigenvectors: Boolean): Pair { + override fun DoubleTensor.symEig(eigenvectors: Boolean): Pair { TODO("Not yet implemented") } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt similarity index 54% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt index 83a513a7a..dd15a257c 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt @@ -1,146 +1,146 @@ package space.kscience.kmath.tensors -public open class RealTensorAlgebra : TensorPartialDivisionAlgebra { +public open class RealTensorAlgebra : TensorPartialDivisionAlgebra { - override fun RealTensor.value(): Double { + override fun DoubleTensor.value(): Double { check(this.shape contentEquals intArrayOf(1)) { "Inconsistent value for tensor of shape ${shape.toList()}" } return this.buffer.unsafeToDoubleArray()[0] } - override fun zeros(shape: IntArray): RealTensor { + override fun zeros(shape: IntArray): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.zeroesLike(): RealTensor { + override fun DoubleTensor.zeroesLike(): DoubleTensor { val shape = this.shape val buffer = DoubleArray(this.buffer.size) { 0.0 } - return RealTensor(shape, buffer) + return DoubleTensor(shape, buffer) } - override fun ones(shape: IntArray): RealTensor { + override fun ones(shape: IntArray): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.onesLike(): RealTensor { + override fun DoubleTensor.onesLike(): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.copy(): RealTensor { + override fun DoubleTensor.copy(): DoubleTensor { // should be rework as soon as copy() method for NDBuffer will be available - return RealTensor(this.shape, this.buffer.unsafeToDoubleArray().copyOf()) + return DoubleTensor(this.shape, this.buffer.unsafeToDoubleArray().copyOf()) } - override fun Double.plus(other: RealTensor): RealTensor { + override fun Double.plus(other: DoubleTensor): DoubleTensor { val resBuffer = DoubleArray(other.buffer.size) { i -> other.buffer.unsafeToDoubleArray()[i] + this } - return RealTensor(other.shape, resBuffer) + return DoubleTensor(other.shape, resBuffer) } - override fun RealTensor.plus(value: Double): RealTensor = value + this + override fun DoubleTensor.plus(value: Double): DoubleTensor = value + this - override fun RealTensor.plus(other: RealTensor): RealTensor { + override fun DoubleTensor.plus(other: DoubleTensor): DoubleTensor { val broadcast = broadcastTensors(this, other) val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.buffer.size) { i -> newThis.buffer.unsafeToDoubleArray()[i] + newOther.buffer.unsafeToDoubleArray()[i] } - return RealTensor(newThis.shape, resBuffer) + return DoubleTensor(newThis.shape, resBuffer) } - override fun RealTensor.plusAssign(value: Double) { + override fun DoubleTensor.plusAssign(value: Double) { for (i in this.buffer.unsafeToDoubleArray().indices) { this.buffer.unsafeToDoubleArray()[i] += value } } - override fun RealTensor.plusAssign(other: RealTensor) { + override fun DoubleTensor.plusAssign(other: DoubleTensor) { //todo should be change with broadcasting for (i in this.buffer.unsafeToDoubleArray().indices) { this.buffer.unsafeToDoubleArray()[i] += other.buffer.unsafeToDoubleArray()[i] } } - override fun Double.minus(other: RealTensor): RealTensor { + override fun Double.minus(other: DoubleTensor): DoubleTensor { val resBuffer = DoubleArray(other.buffer.size) { i -> this - other.buffer.unsafeToDoubleArray()[i] } - return RealTensor(other.shape, resBuffer) + return DoubleTensor(other.shape, resBuffer) } - override fun RealTensor.minus(value: Double): RealTensor { + override fun DoubleTensor.minus(value: Double): DoubleTensor { val resBuffer = DoubleArray(this.buffer.size) { i -> this.buffer.unsafeToDoubleArray()[i] - value } - return RealTensor(this.shape, resBuffer) + return DoubleTensor(this.shape, resBuffer) } - override fun RealTensor.minus(other: RealTensor): RealTensor { + override fun DoubleTensor.minus(other: DoubleTensor): DoubleTensor { val broadcast = broadcastTensors(this, other) val newThis = broadcast[0] val newOther = broadcast[1] val resBuffer = DoubleArray(newThis.buffer.size) { i -> newThis.buffer.unsafeToDoubleArray()[i] - newOther.buffer.unsafeToDoubleArray()[i] } - return RealTensor(newThis.shape, resBuffer) + return DoubleTensor(newThis.shape, resBuffer) } - override fun RealTensor.minusAssign(value: Double) { + override fun DoubleTensor.minusAssign(value: Double) { for (i in this.buffer.unsafeToDoubleArray().indices) { this.buffer.unsafeToDoubleArray()[i] -= value } } - override fun RealTensor.minusAssign(other: RealTensor) { + override fun DoubleTensor.minusAssign(other: DoubleTensor) { TODO("Alya") } - override fun Double.times(other: RealTensor): RealTensor { + override fun Double.times(other: DoubleTensor): DoubleTensor { //todo should be change with broadcasting val resBuffer = DoubleArray(other.buffer.size) { i -> other.buffer.unsafeToDoubleArray()[i] * this } - return RealTensor(other.shape, resBuffer) + return DoubleTensor(other.shape, resBuffer) } //todo should be change with broadcasting - override fun RealTensor.times(value: Double): RealTensor = value * this + override fun DoubleTensor.times(value: Double): DoubleTensor = value * this - override fun RealTensor.times(other: RealTensor): RealTensor { + override fun DoubleTensor.times(other: DoubleTensor): DoubleTensor { //todo should be change with broadcasting val resBuffer = DoubleArray(this.buffer.size) { i -> this.buffer.unsafeToDoubleArray()[i] * other.buffer.unsafeToDoubleArray()[i] } - return RealTensor(this.shape, resBuffer) + return DoubleTensor(this.shape, resBuffer) } - override fun RealTensor.timesAssign(value: Double) { + override fun DoubleTensor.timesAssign(value: Double) { //todo should be change with broadcasting for (i in this.buffer.unsafeToDoubleArray().indices) { this.buffer.unsafeToDoubleArray()[i] *= value } } - override fun RealTensor.timesAssign(other: RealTensor) { + override fun DoubleTensor.timesAssign(other: DoubleTensor) { //todo should be change with broadcasting for (i in this.buffer.unsafeToDoubleArray().indices) { this.buffer.unsafeToDoubleArray()[i] *= other.buffer.unsafeToDoubleArray()[i] } } - override fun RealTensor.unaryMinus(): RealTensor { + override fun DoubleTensor.unaryMinus(): DoubleTensor { val resBuffer = DoubleArray(this.buffer.size) { i -> this.buffer.unsafeToDoubleArray()[i].unaryMinus() } - return RealTensor(this.shape, resBuffer) + return DoubleTensor(this.shape, resBuffer) } - override fun RealTensor.transpose(i: Int, j: Int): RealTensor { + override fun DoubleTensor.transpose(i: Int, j: Int): DoubleTensor { checkTranspose(this.dimension, i, j) val n = this.buffer.size val resBuffer = DoubleArray(n) @@ -148,7 +148,7 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra, dim: Int): RealTensor { + override fun cat(tensors: List, dim: Int): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.div(value: Double): RealTensor { + override fun DoubleTensor.div(value: Double): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.div(other: RealTensor): RealTensor { + override fun DoubleTensor.div(other: DoubleTensor): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.flatten(startDim: Int, endDim: Int): RealTensor { + override fun DoubleTensor.flatten(startDim: Int, endDim: Int): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.divAssign(value: Double) { + override fun DoubleTensor.divAssign(value: Double) { TODO("Not yet implemented") } - override fun RealTensor.divAssign(other: RealTensor) { + override fun DoubleTensor.divAssign(other: DoubleTensor) { TODO("Not yet implemented") } - override fun RealTensor.mean(dim: Int, keepDim: Boolean): RealTensor { + override fun DoubleTensor.mean(dim: Int, keepDim: Boolean): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.quantile(q: Double, dim: Int, keepDim: Boolean): RealTensor { + override fun DoubleTensor.quantile(q: Double, dim: Int, keepDim: Boolean): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.std(dim: Int, unbiased: Boolean, keepDim: Boolean): RealTensor { + override fun DoubleTensor.std(dim: Int, unbiased: Boolean, keepDim: Boolean): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): RealTensor { + override fun DoubleTensor.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): DoubleTensor { TODO("Not yet implemented") } - override fun RealTensor.histc(bins: Int, min: Double, max: Double): RealTensor { + override fun DoubleTensor.histc(bins: Int, min: Double, max: Double): DoubleTensor { TODO("Not yet implemented") } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealAnalyticTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealAnalyticTensorAlgebra.kt deleted file mode 100644 index f610361fc..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealAnalyticTensorAlgebra.kt +++ /dev/null @@ -1,150 +0,0 @@ -package space.kscience.kmath.tensors - -public class RealAnalyticTensorAlgebra: - AnalyticTensorAlgebra, - RealTensorAlgebra() -{ - override fun RealTensor.exp(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.log(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.sqrt(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.square(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.cos(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.acos(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.cosh(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.acosh(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.sin(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.asin(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.sinh(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.asinh(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.tan(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.atan(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.tanh(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.atanh(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.ceil(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.floor(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.clamp(min: Double, max: Double): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.erf(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.erfinv(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.erfc(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.lerp(end: RealTensor, weight: RealTensor): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.lgamma(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.logit(eps: Double): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.igamma(other: RealTensor): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.igammac(other: RealTensor): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.mvlgamma(dimensions: Int): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.polygamma(order: Int): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.pow(exponent: Double): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.round(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.sigmoid(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.sinc(): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.heaviside(values: RealTensor): RealTensor { - TODO("Not yet implemented") - } - - override fun RealTensor.trapz(xValues: RealTensor, dim: Int): RealTensor { - TODO("Not yet implemented") - } - -} - -public inline fun RealAnalyticTensorAlgebra(block: RealAnalyticTensorAlgebra.() -> R): R = - RealAnalyticTensorAlgebra().block() \ No newline at end of file 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 b03fb7dd7..9f7e811db 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 @@ -32,13 +32,13 @@ internal inline fun broadcastShapes(vararg shapes: IntArray): IntArray { return totalShape } -internal inline fun broadcastTensors(vararg tensors: RealTensor): List { +internal inline fun broadcastTensors(vararg tensors: DoubleTensor): List { val totalShape = broadcastShapes(*(tensors.map { it.shape }).toTypedArray()) val n = totalShape.reduce { acc, i -> acc * i } - val res = ArrayList(0) + val res = ArrayList(0) for (tensor in tensors) { - val resTensor = RealTensor(totalShape, DoubleArray(n)) + val resTensor = DoubleTensor(totalShape, DoubleArray(n)) for (linearIndex in 0 until n) { val totalMultiIndex = resTensor.strides.index(linearIndex) 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 473a3da8b..5d42e6d0f 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 @@ -11,13 +11,13 @@ class TestRealTensor { @Test fun valueTest() = RealTensorAlgebra { val value = 12.5 - val tensor = RealTensor(intArrayOf(1), doubleArrayOf(value)) + val tensor = DoubleTensor(intArrayOf(1), doubleArrayOf(value)) assertEquals(tensor.value(), value) } @Test fun stridesTest(){ - val tensor = RealTensor(intArrayOf(2,2), doubleArrayOf(3.5,5.8,58.4,2.4)) + val tensor = DoubleTensor(intArrayOf(2,2), doubleArrayOf(3.5,5.8,58.4,2.4)) assertEquals(tensor[intArrayOf(0,1)], 5.8) assertTrue(tensor.elements().map{ it.second }.toList().toDoubleArray() contentEquals tensor.buffer.toDoubleArray()) } 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 72bbf1787..3e27a1cc2 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 @@ -8,14 +8,14 @@ class TestRealTensorAlgebra { @Test fun doublePlus() = RealTensorAlgebra { - val tensor = RealTensor(intArrayOf(2), doubleArrayOf(1.0, 2.0)) + val tensor = DoubleTensor(intArrayOf(2), doubleArrayOf(1.0, 2.0)) val res = 10.0 + tensor assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(11.0,12.0)) } @Test fun transpose1x1() = RealTensorAlgebra { - val tensor = RealTensor(intArrayOf(1), doubleArrayOf(0.0)) + val tensor = DoubleTensor(intArrayOf(1), doubleArrayOf(0.0)) val res = tensor.transpose(0, 0) assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(0.0)) @@ -24,7 +24,7 @@ class TestRealTensorAlgebra { @Test fun transpose3x2() = RealTensorAlgebra { - val tensor = RealTensor(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) + val tensor = DoubleTensor(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.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 3.0, 5.0, 2.0, 4.0, 6.0)) @@ -33,7 +33,7 @@ class TestRealTensorAlgebra { @Test fun transpose1x2x3() = RealTensorAlgebra { - val tensor = RealTensor(intArrayOf(1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) + val tensor = DoubleTensor(intArrayOf(1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) val res01 = tensor.transpose(0, 1) val res02 = tensor.transpose(0, 2) val res12 = tensor.transpose(1, 2) @@ -60,9 +60,9 @@ class TestRealTensorAlgebra { @Test fun broadcastTensors() = RealTensorAlgebra { - val tensor1 = RealTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) - val tensor2 = RealTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) - val tensor3 = RealTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) + val tensor1 = DoubleTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) + val tensor2 = DoubleTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) + val tensor3 = DoubleTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) val res = broadcastTensors(tensor1, tensor2, tensor3) @@ -77,9 +77,9 @@ class TestRealTensorAlgebra { @Test fun minusTensor() = RealTensorAlgebra { - val tensor1 = RealTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) - val tensor2 = RealTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) - val tensor3 = RealTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) + val tensor1 = DoubleTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)) + val tensor2 = DoubleTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0)) + val tensor3 = DoubleTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0)) assertTrue((tensor2 - tensor1).shape contentEquals intArrayOf(2, 3)) assertTrue((tensor2 - tensor1).buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(9.0, 18.0, 27.0, 6.0, 15.0, 24.0))