From 0553a28ee89e0d55f498c578882fece47bc620fe Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Tue, 16 Mar 2021 07:47:02 +0000 Subject: [PATCH] ReduceOpsTensorAlgebra --- .../kmath/tensors/AnalyticTensorAlgebra.kt | 15 +++++++++++- .../tensors/DoubleAnalyticTensorAlgebra.kt | 18 ++++++++++++++- .../tensors/DoubleOrderedTensorAlgebra.kt | 2 +- .../tensors/DoubleReduceOpsTensorAlgebra.kt | 16 +++++++++++++ .../kmath/tensors/DoubleTensorAlgebra.kt | 23 ------------------- .../kmath/tensors/ReduceOpsTensorAlgebra.kt | 7 ++++++ .../kscience/kmath/tensors/TensorAlgebra.kt | 1 - .../tensors/TensorPartialDivisionAlgebra.kt | 14 ----------- .../kmath/tensors/TestDoubleTensor.kt | 12 +++++++++- 9 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleReduceOpsTensorAlgebra.kt create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/ReduceOpsTensorAlgebra.kt diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/AnalyticTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/AnalyticTensorAlgebra.kt index 17a25b6b3..41772da44 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/AnalyticTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/AnalyticTensorAlgebra.kt @@ -2,7 +2,17 @@ package space.kscience.kmath.tensors public interface AnalyticTensorAlgebra> : - TensorPartialDivisionAlgebra { + TensorPartialDivisionAlgebra, + OrderedTensorAlgebra{ + + //https://pytorch.org/docs/stable/generated/torch.quantile.html#torch.quantile + public fun TensorType.quantile(q: T, dim: Int, keepDim: Boolean): TensorType + + //https://pytorch.org/docs/stable/generated/torch.std.html#torch.std + public fun TensorType.std(dim: Int, unbiased: Boolean, keepDim: Boolean): TensorType + + //https://pytorch.org/docs/stable/generated/torch.var.html#torch.var + public fun TensorType.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): TensorType //https://pytorch.org/docs/stable/generated/torch.exp.html public fun TensorType.exp(): TensorType @@ -109,4 +119,7 @@ public interface AnalyticTensorAlgebra> : //https://pytorch.org/docs/stable/generated/torch.trapz.html#torch.trapz public fun TensorType.trapz(xValues: TensorType, dim: Int): TensorType + //https://pytorch.org/docs/stable/generated/torch.histc.html#torch.histc + public fun TensorType.histc(bins: Int, min: T, max: T): TensorType + } \ 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 index 00e7a7fbd..cccc02789 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt @@ -2,7 +2,7 @@ package space.kscience.kmath.tensors public class DoubleAnalyticTensorAlgebra: AnalyticTensorAlgebra, - DoubleTensorAlgebra() + DoubleOrderedTensorAlgebra() { override fun DoubleTensor.exp(): DoubleTensor { TODO("Not yet implemented") @@ -144,6 +144,22 @@ public class DoubleAnalyticTensorAlgebra: TODO("Not yet implemented") } + override fun DoubleTensor.quantile(q: Double, dim: Int, keepDim: Boolean): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.std(dim: Int, unbiased: Boolean, keepDim: Boolean): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): DoubleTensor { + TODO("Not yet implemented") + } + + override fun DoubleTensor.histc(bins: Int, min: Double, max: Double): DoubleTensor { + TODO("Not yet implemented") + } + } public inline fun DoubleAnalyticTensorAlgebra(block: DoubleAnalyticTensorAlgebra.() -> R): R = diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleOrderedTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleOrderedTensorAlgebra.kt index e7ebf6c56..bd6bcfe8f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleOrderedTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleOrderedTensorAlgebra.kt @@ -1,6 +1,6 @@ package space.kscience.kmath.tensors -public class DoubleOrderedTensorAlgebra: +public open class DoubleOrderedTensorAlgebra: OrderedTensorAlgebra, DoubleTensorAlgebra() { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleReduceOpsTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleReduceOpsTensorAlgebra.kt new file mode 100644 index 000000000..00d9b3ff8 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleReduceOpsTensorAlgebra.kt @@ -0,0 +1,16 @@ +package space.kscience.kmath.tensors + +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/DoubleTensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt index 3b65e89da..c383387ef 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/DoubleTensorAlgebra.kt @@ -3,13 +3,6 @@ package space.kscience.kmath.tensors 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] - } - override operator fun DoubleTensor.get(i: Int): DoubleTensor { val lastShape = this.shape.drop(1).toIntArray() val newShape = if (lastShape.isNotEmpty()) lastShape else intArrayOf(1) @@ -257,22 +250,6 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra> : + 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 e56abfaaa..60f0b3379 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 @@ -3,7 +3,6 @@ package space.kscience.kmath.tensors // https://proofwiki.org/wiki/Definition:Algebra_over_Ring public interface TensorAlgebra> { - public fun TensorType.value(): T public fun zeros(shape: IntArray): TensorType public fun TensorType.zeroesLike(): TensorType diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorPartialDivisionAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorPartialDivisionAlgebra.kt index 2d448fa8c..ca3876e2c 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorPartialDivisionAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/tensors/TensorPartialDivisionAlgebra.kt @@ -3,7 +3,6 @@ package space.kscience.kmath.tensors // https://proofwiki.org/wiki/Definition:Division_Algebra public interface TensorPartialDivisionAlgebra> : TensorAlgebra { - public operator fun TensorType.div(value: T): TensorType public operator fun TensorType.div(other: TensorType): TensorType public operator fun TensorType.divAssign(value: T) @@ -11,17 +10,4 @@ public interface TensorPartialDivisionAlgebra //https://pytorch.org/docs/stable/generated/torch.mean.html#torch.mean public fun TensorType.mean(dim: Int, keepDim: Boolean): TensorType - - //https://pytorch.org/docs/stable/generated/torch.quantile.html#torch.quantile - public fun TensorType.quantile(q: T, dim: Int, keepDim: Boolean): TensorType - - //https://pytorch.org/docs/stable/generated/torch.std.html#torch.std - public fun TensorType.std(dim: Int, unbiased: Boolean, keepDim: Boolean): TensorType - - //https://pytorch.org/docs/stable/generated/torch.var.html#torch.var - public fun TensorType.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): TensorType - - //https://pytorch.org/docs/stable/generated/torch.histc.html#torch.histc - public fun TensorType.histc(bins: Int, min: T, max: T): TensorType - } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestDoubleTensor.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestDoubleTensor.kt index 3050f064a..006b0273a 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestDoubleTensor.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/tensors/TestDoubleTensor.kt @@ -1,6 +1,7 @@ package space.kscience.kmath.tensors +import space.kscience.kmath.nd.as1D import space.kscience.kmath.nd.as2D import space.kscience.kmath.structures.toDoubleArray import kotlin.test.Test @@ -10,7 +11,7 @@ import kotlin.test.assertTrue class TestDoubleTensor { @Test - fun valueTest() = DoubleTensorAlgebra { + fun valueTest() = DoubleReduceOpsTensorAlgebra { val value = 12.5 val tensor = DoubleTensor(intArrayOf(1), doubleArrayOf(value)) assertEquals(tensor.value(), value) @@ -28,5 +29,14 @@ class TestDoubleTensor { val tensor = DoubleTensor(intArrayOf(1,2,2), doubleArrayOf(3.5,5.8,58.4,2.4)) val matrix = tensor[0].as2D() assertEquals(matrix[0,1], 5.8) + + val vector = tensor[0][1].as1D() + assertEquals(vector[0], 58.4) + + matrix[0,1] = 77.89 + assertEquals(tensor[intArrayOf(0,0,1)], 77.89) + + //vector[0] = 109.56 + //println(tensor[intArrayOf(0,1,0)]) } } \ No newline at end of file