From 51eca003af9daeb72a53fc05867d955d1167154c Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Mon, 29 Mar 2021 22:11:34 +0100 Subject: [PATCH] Moved back value to main algebra context --- kmath-core/api/kmath-core.api | 17 +++-------------- .../kmath/tensors/ReduceOpsTensorAlgebra.kt | 7 ------- .../kscience/kmath/tensors/TensorAlgebra.kt | 2 ++ .../core/DoubleReduceOpsTensorAlgebra.kt | 18 ------------------ .../kmath/tensors/core/DoubleTensorAlgebra.kt | 7 +++++++ .../tensors/core/TestDoubleLinearOpsAlgebra.kt | 9 +++------ .../kmath/tensors/core/TestDoubleTensor.kt | 10 +++++++++- 7 files changed, 24 insertions(+), 46 deletions(-) delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/ReduceOpsTensorAlgebra.kt delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleReduceOpsTensorAlgebra.kt diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 786b81d19..373920204 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -2654,10 +2654,6 @@ public final class space/kscience/kmath/tensors/LinearOpsTensorAlgebra$DefaultIm public static synthetic fun symEig$default (Lspace/kscience/kmath/tensors/LinearOpsTensorAlgebra;Lspace/kscience/kmath/nd/MutableStructureND;ZILjava/lang/Object;)Lkotlin/Pair; } -public abstract interface class space/kscience/kmath/tensors/ReduceOpsTensorAlgebra : space/kscience/kmath/tensors/TensorAlgebra { - public abstract fun value (Lspace/kscience/kmath/nd/MutableStructureND;)Ljava/lang/Object; -} - public abstract interface class space/kscience/kmath/tensors/TensorAlgebra { public abstract fun copy (Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND; public abstract fun diagonalEmbedding (Lspace/kscience/kmath/nd/MutableStructureND;III)Lspace/kscience/kmath/nd/MutableStructureND; @@ -2686,6 +2682,7 @@ public abstract interface class space/kscience/kmath/tensors/TensorAlgebra { public abstract fun timesAssign (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/MutableStructureND;)V public abstract fun transpose (Lspace/kscience/kmath/nd/MutableStructureND;II)Lspace/kscience/kmath/nd/MutableStructureND; public abstract fun unaryMinus (Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND; + public abstract fun value (Lspace/kscience/kmath/nd/MutableStructureND;)Ljava/lang/Object; public abstract fun view (Lspace/kscience/kmath/nd/MutableStructureND;[I)Lspace/kscience/kmath/nd/MutableStructureND; public abstract fun viewAs (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND; public abstract fun zeroesLike (Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND; @@ -2809,16 +2806,6 @@ public final class space/kscience/kmath/tensors/core/DoubleLinearOpsTensorAlgebr public static final fun DoubleLinearOpsTensorAlgebra (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } -public final class space/kscience/kmath/tensors/core/DoubleReduceOpsTensorAlgebra : space/kscience/kmath/tensors/core/DoubleTensorAlgebra, space/kscience/kmath/tensors/ReduceOpsTensorAlgebra { - public fun ()V - public synthetic fun value (Lspace/kscience/kmath/nd/MutableStructureND;)Ljava/lang/Object; - public fun value (Lspace/kscience/kmath/tensors/core/DoubleTensor;)Ljava/lang/Double; -} - -public final class space/kscience/kmath/tensors/core/DoubleReduceOpsTensorAlgebraKt { - public static final fun DoubleReduceOpsTensorAlgebra (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; -} - public final class space/kscience/kmath/tensors/core/DoubleTensor : space/kscience/kmath/tensors/core/BufferedTensor { } @@ -2892,6 +2879,8 @@ public class space/kscience/kmath/tensors/core/DoubleTensorAlgebra : space/kscie public fun transpose (Lspace/kscience/kmath/tensors/core/DoubleTensor;II)Lspace/kscience/kmath/tensors/core/DoubleTensor; public synthetic fun unaryMinus (Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND; public fun unaryMinus (Lspace/kscience/kmath/tensors/core/DoubleTensor;)Lspace/kscience/kmath/tensors/core/DoubleTensor; + public synthetic fun value (Lspace/kscience/kmath/nd/MutableStructureND;)Ljava/lang/Object; + public fun value (Lspace/kscience/kmath/tensors/core/DoubleTensor;)Ljava/lang/Double; public synthetic fun view (Lspace/kscience/kmath/nd/MutableStructureND;[I)Lspace/kscience/kmath/nd/MutableStructureND; public fun view (Lspace/kscience/kmath/tensors/core/DoubleTensor;[I)Lspace/kscience/kmath/tensors/core/DoubleTensor; public synthetic fun viewAs (Lspace/kscience/kmath/nd/MutableStructureND;Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND; diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/ReduceOpsTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/ReduceOpsTensorAlgebra.kt deleted file mode 100644 index 820b9b25b..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/ReduceOpsTensorAlgebra.kt +++ /dev/null @@ -1,7 +0,0 @@ -package space.kscience.kmath.tensors - -public interface ReduceOpsTensorAlgebra> : - TensorAlgebra { - public fun TensorType.value(): T - -} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorAlgebra.kt index b05105ed8..5b7515b20 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorAlgebra.kt @@ -5,6 +5,8 @@ import space.kscience.kmath.tensors.core.DoubleTensor // https://proofwiki.org/wiki/Definition:Algebra_over_Ring public interface TensorAlgebra> { + public fun TensorType.value(): T + //https://pytorch.org/docs/stable/generated/torch.full.html public fun full(value: T, shape: IntArray): TensorType diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleReduceOpsTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleReduceOpsTensorAlgebra.kt deleted file mode 100644 index 9a8aa9ebf..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleReduceOpsTensorAlgebra.kt +++ /dev/null @@ -1,18 +0,0 @@ -package space.kscience.kmath.tensors.core - -import space.kscience.kmath.tensors.ReduceOpsTensorAlgebra - -public class DoubleReduceOpsTensorAlgebra: - DoubleTensorAlgebra(), - ReduceOpsTensorAlgebra { - - override fun DoubleTensor.value(): Double { - check(this.shape contentEquals intArrayOf(1)) { - "Inconsistent value for tensor of shape ${shape.toList()}" - } - return this.buffer.array()[this.bufferStart] - } -} - -public inline fun DoubleReduceOpsTensorAlgebra(block: DoubleReduceOpsTensorAlgebra.() -> R): R = - DoubleReduceOpsTensorAlgebra().block() \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index b25f0164b..b418f3647 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -7,6 +7,13 @@ import kotlin.math.abs public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra { + override fun DoubleTensor.value(): Double { + check(this.shape contentEquals intArrayOf(1)) { + "Inconsistent value for tensor of shape ${shape.toList()}" + } + return this.buffer.array()[this.bufferStart] + } + public fun fromArray(shape: IntArray, buffer: DoubleArray): DoubleTensor { checkEmptyShape(shape) checkEmptyDoubleBuffer(buffer) diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt index 3a129b03c..d2d4ffb67 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleLinearOpsAlgebra.kt @@ -72,11 +72,8 @@ class TestDoubleLinearOpsTensorAlgebra { @Test fun testScalarProduct() = DoubleLinearOpsTensorAlgebra { - val a = fromArray(intArrayOf(3), doubleArrayOf(1.8,2.5, 6.8)) - val b = fromArray(intArrayOf(3), doubleArrayOf(5.5,2.6, 6.4)) - DoubleReduceOpsTensorAlgebra { - assertEquals(a.dot(b).value(), 59.92) - } - + val a = fromArray(intArrayOf(3), doubleArrayOf(1.8, 2.5, 6.8)) + val b = fromArray(intArrayOf(3), doubleArrayOf(5.5, 2.6, 6.4)) + assertEquals(a.dot(b).value(), 59.92) } } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt index 5e6fdcdc5..4f4d9bbdf 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt @@ -10,7 +10,7 @@ import kotlin.test.assertTrue class TestDoubleTensor { @Test - fun valueTest() = DoubleReduceOpsTensorAlgebra { + fun valueTest() = DoubleTensorAlgebra { val value = 12.5 val tensor = fromArray(intArrayOf(1), doubleArrayOf(value)) assertEquals(tensor.value(), value) @@ -37,5 +37,13 @@ class TestDoubleTensor { vector[0] = 109.56 assertEquals(tensor[intArrayOf(0,1,0)], 109.56) + + tensor.matrixSequence().forEach { + val a = it.asTensor() + val secondRow = a[1].as1D() + val secondColumn = a.transpose(0,1)[1].as1D() + assertEquals(secondColumn[0], 77.89) + assertEquals(secondRow[1], secondColumn[1]) + } } } \ No newline at end of file