From 70bebbe8488840a38e84855ca3d3eea1ddb4eebb Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Tue, 16 Mar 2021 12:12:28 +0000 Subject: [PATCH] 1D mutable structure setter fixed --- .../kotlin/space/kscience/kmath/nd/Structure1D.kt | 2 +- .../space/kscience/kmath/tensors/AnalyticTensorAlgebra.kt | 6 ------ .../kscience/kmath/tensors/DoubleAnalyticTensorAlgebra.kt | 8 -------- .../space/kscience/kmath/tensors/DoubleTensorAlgebra.kt | 8 ++++++++ .../kotlin/space/kscience/kmath/tensors/TensorAlgebra.kt | 3 +++ .../kmath/tensors/TensorPartialDivisionAlgebra.kt | 3 +++ .../space/kscience/kmath/tensors/TestDoubleTensor.kt | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt index 2926b3d1b..fd965a668 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt @@ -52,7 +52,7 @@ private inline class MutableStructure1DWrapper(val structure: MutableNDStruct override fun get(index: Int): T = structure[index] override fun set(index: Int, value: T) { - set(index, value) + structure[intArrayOf(index)] = value } override fun copy(): MutableBuffer = 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 41772da44..c3a558298 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 @@ -11,9 +11,6 @@ public interface AnalyticTensorAlgebra> : //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 @@ -23,9 +20,6 @@ public interface AnalyticTensorAlgebra> : //https://pytorch.org/docs/stable/generated/torch.sqrt.html public fun TensorType.sqrt(): TensorType - //https://pytorch.org/docs/stable/generated/torch.square.html - public fun TensorType.square(): TensorType - //https://pytorch.org/docs/stable/generated/torch.acos.html#torch.cos public fun TensorType.cos(): TensorType 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 cccc02789..5349a9923 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 @@ -16,10 +16,6 @@ public class DoubleAnalyticTensorAlgebra: TODO("Not yet implemented") } - override fun DoubleTensor.square(): DoubleTensor { - TODO("Not yet implemented") - } - override fun DoubleTensor.cos(): DoubleTensor { TODO("Not yet implemented") } @@ -152,10 +148,6 @@ public class DoubleAnalyticTensorAlgebra: 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") } 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 c383387ef..bafbd9a96 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 @@ -254,6 +254,14 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra DoubleTensorAlgebra(block: DoubleTensorAlgebra.() -> R): R = 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 60f0b3379..0af757d1a 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 @@ -40,6 +40,9 @@ public interface TensorAlgebra> { public operator fun TensorType.timesAssign(other: TensorType): Unit public operator fun TensorType.unaryMinus(): TensorType + //https://pytorch.org/docs/stable/generated/torch.square.html + public fun TensorType.square(): TensorType + //https://pytorch.org/cppdocs/notes/tensor_indexing.html public operator fun TensorType.get(i: Int): 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 ca3876e2c..9f70f9621 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 @@ -10,4 +10,7 @@ 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.var.html#torch.var + public fun TensorType.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): 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 006b0273a..6b20027c7 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 @@ -36,7 +36,7 @@ class TestDoubleTensor { matrix[0,1] = 77.89 assertEquals(tensor[intArrayOf(0,0,1)], 77.89) - //vector[0] = 109.56 - //println(tensor[intArrayOf(0,1,0)]) + vector[0] = 109.56 + assertEquals(tensor[intArrayOf(0,1,0)], 109.56) } } \ No newline at end of file