From 39a088912346078f3c3647ac262b2cf8c490aa4a Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Mon, 15 Mar 2021 07:50:20 +0000 Subject: [PATCH] Dev merge assemble fixed, tests still broken --- .../kmath/tensors/LinearOpsTensorAlgebra.kt | 2 +- .../tensors/RealAnalyticTensorAlgebra.kt | 4 +- .../tensors/RealLinearOpsTensorAlgebra.kt | 7 +-- .../kmath/tensors/RealTensorAlgebra.kt | 46 +++++++++---------- .../space/kscience/kmath/tensors/utils.kt | 4 +- .../kscience/kmath/tensors/TestRealTensor.kt | 4 +- .../kmath/tensors/TestRealTensorAlgebra.kt | 26 +++++------ 7 files changed, 48 insertions(+), 45 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/LinearOpsTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/LinearOpsTensorAlgebra.kt index bd9cbfd45..db1135a33 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/LinearOpsTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/LinearOpsTensorAlgebra.kt @@ -32,7 +32,7 @@ public interface LinearOpsTensorAlgebra> : public fun TensorType.lu(): Pair //https://pytorch.org/docs/stable/generated/torch.lu_unpack.html - public fun luPivot(aLU: TensorType, pivots: IntTensor): Triple + public fun luPivot(lu: TensorType, pivots: IntTensor): Triple //https://pytorch.org/docs/stable/linalg.html#torch.linalg.svd public fun TensorType.svd(): Triple 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 index cfecac0f4..a93ebcb89 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealAnalyticTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/RealAnalyticTensorAlgebra.kt @@ -144,5 +144,7 @@ public class RealAnalyticTensorAlgebra: TODO("Not yet implemented") } +} -} \ No newline at end of file +public inline fun RealAnalyticTensorAlgebra(block: RealTensorAlgebra.() -> 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/RealLinearOpsTensorAlgebra.kt index 18c2050c0..98a5e581f 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,6 +1,7 @@ package space.kscience.kmath.tensors -import space.kscience.kmath.structures.array +import space.kscience.kmath.structures.toDoubleArray +import space.kscience.kmath.structures.toIntArray public class RealLinearOpsTensorAlgebra : LinearOpsTensorAlgebra, @@ -80,7 +81,7 @@ public class RealLinearOpsTensorAlgebra : // todo checks val n = lu.shape[0] val p = lu.zeroesLike() - pivots.buffer.array.forEachIndexed { i, pivot -> + pivots.buffer.toIntArray().forEachIndexed { i, pivot -> p[i, pivot] = 1.0 } val l = lu.zeroesLike() @@ -130,4 +131,4 @@ public class RealLinearOpsTensorAlgebra : } public inline fun RealLinearOpsTensorAlgebra(block: RealTensorAlgebra.() -> R): R = - RealTensorAlgebra().block() \ No newline at end of file + RealLinearOpsTensorAlgebra().block() \ No newline at end of file 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 05e2b57d2..10cc0edad 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.array +import space.kscience.kmath.structures.toDoubleArray public open class RealTensorAlgebra : TensorPartialDivisionAlgebra { @@ -9,7 +9,7 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - other.buffer.array[i] + this + other.buffer.toDoubleArray()[i] + this } return RealTensor(other.shape, resBuffer) } @@ -50,34 +50,34 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - newThis.buffer.array[i] + newOther.buffer.array[i] + newThis.buffer.toDoubleArray()[i] + newOther.buffer.toDoubleArray()[i] } return RealTensor(newThis.shape, resBuffer) } override fun RealTensor.plusAssign(value: Double) { - for (i in this.buffer.array.indices) { - this.buffer.array[i] += value + for (i in this.buffer.toDoubleArray().indices) { + this.buffer.toDoubleArray()[i] += value } } override fun RealTensor.plusAssign(other: RealTensor) { //todo should be change with broadcasting - for (i in this.buffer.array.indices) { - this.buffer.array[i] += other.buffer.array[i] + for (i in this.buffer.toDoubleArray().indices) { + this.buffer.toDoubleArray()[i] += other.buffer.toDoubleArray()[i] } } override fun Double.minus(other: RealTensor): RealTensor { val resBuffer = DoubleArray(other.buffer.size) { i -> - this - other.buffer.array[i] + this - other.buffer.toDoubleArray()[i] } return RealTensor(other.shape, resBuffer) } override fun RealTensor.minus(value: Double): RealTensor { val resBuffer = DoubleArray(this.buffer.size) { i -> - this.buffer.array[i] - value + this.buffer.toDoubleArray()[i] - value } return RealTensor(this.shape, resBuffer) } @@ -87,14 +87,14 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - newThis.buffer.array[i] - newOther.buffer.array[i] + newThis.buffer.toDoubleArray()[i] - newOther.buffer.toDoubleArray()[i] } return RealTensor(newThis.shape, resBuffer) } override fun RealTensor.minusAssign(value: Double) { - for (i in this.buffer.array.indices) { - this.buffer.array[i] -= value + for (i in this.buffer.toDoubleArray().indices) { + this.buffer.toDoubleArray()[i] -= value } } @@ -105,7 +105,7 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - other.buffer.array[i] * this + other.buffer.toDoubleArray()[i] * this } return RealTensor(other.shape, resBuffer) } @@ -116,28 +116,28 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra - this.buffer.array[i] * other.buffer.array[i] + this.buffer.toDoubleArray()[i] * other.buffer.toDoubleArray()[i] } return RealTensor(this.shape, resBuffer) } override fun RealTensor.timesAssign(value: Double) { //todo should be change with broadcasting - for (i in this.buffer.array.indices) { - this.buffer.array[i] *= value + for (i in this.buffer.toDoubleArray().indices) { + this.buffer.toDoubleArray()[i] *= value } } override fun RealTensor.timesAssign(other: RealTensor) { //todo should be change with broadcasting - for (i in this.buffer.array.indices) { - this.buffer.array[i] *= other.buffer.array[i] + for (i in this.buffer.toDoubleArray().indices) { + this.buffer.toDoubleArray()[i] *= other.buffer.toDoubleArray()[i] } } override fun RealTensor.unaryMinus(): RealTensor { val resBuffer = DoubleArray(this.buffer.size) { i -> - this.buffer.array[i].unaryMinus() + this.buffer.toDoubleArray()[i].unaryMinus() } return RealTensor(this.shape, resBuffer) } @@ -158,14 +158,14 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra