From d31726a0d90de41ced32d211d3d929dd46f5091c Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Fri, 7 May 2021 13:12:18 +0100 Subject: [PATCH] produce routine --- .../kmath/tensors/core/DoubleTensorAlgebra.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index d4a0f1433..052d8d97e 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -56,6 +56,17 @@ public open class DoubleTensorAlgebra : return DoubleTensor(shape, buffer, 0) } + /** + * Constructs a tensor with the specified shape and initializer. + * + * @param shape the desired shape for the tensor. + * @param initializer mapping tensor indices to values. + * @return tensor with the [shape] shape and data generated by initializer. + */ + public fun produce(shape: IntArray, initializer: (IntArray) -> Double): DoubleTensor = + fromArray(shape, + TensorLinearStructure(shape).indices().map(initializer).toMutableList().toDoubleArray()) + override operator fun Tensor.get(i: Int): DoubleTensor { val lastShape = tensor.shape.drop(1).toIntArray() val newShape = if (lastShape.isNotEmpty()) lastShape else intArrayOf(1)