diff --git a/CHANGELOG.md b/CHANGELOG.md index 073eaaba2..3ce57d5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### Added +- Explicit `mutableStructureND` builders for mutable stucures ### Changed diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index afa36ed1e..734f60091 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -3,8 +3,6 @@ plugins { `version-catalog` } -java.targetCompatibility = JavaVersion.VERSION_11 - repositories { mavenLocal() maven("https://repo.kotlin.link") @@ -26,6 +24,11 @@ dependencies { implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.+") } -kotlin.sourceSets.all { - languageSettings.optIn("kotlin.OptIn") +kotlin{ + jvmToolchain{ + languageVersion.set(JavaLanguageVersion.of(11)) + } + sourceSets.all { + languageSettings.optIn("kotlin.OptIn") + } } diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 50708eaa9..4047dfc5e 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -52,7 +52,7 @@ dependencies { implementation("org.slf4j:slf4j-simple:1.7.32") // plotting - implementation("space.kscience:plotlykt-server:0.5.0") + implementation("space.kscience:plotlykt-server:0.5.3-dev-1") } kotlin { diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt index 4a5d783e1..6cbcd9943 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt @@ -8,14 +8,15 @@ package space.kscience.kmath.operations import space.kscience.kmath.commons.linear.CMLinearSpace import space.kscience.kmath.linear.matrix import space.kscience.kmath.nd.DoubleBufferND -import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.Structure2D +import space.kscience.kmath.nd.mutableStructureND import space.kscience.kmath.nd.ndAlgebra -import space.kscience.kmath.viktor.ViktorStructureND import space.kscience.kmath.viktor.viktorAlgebra +import kotlin.collections.component1 +import kotlin.collections.component2 fun main() { - val viktorStructure: ViktorStructureND = DoubleField.viktorAlgebra.structureND(ShapeND(2, 2)) { (i, j) -> + val viktorStructure = DoubleField.viktorAlgebra.mutableStructureND(2, 2) { (i, j) -> if (i == j) 2.0 else 0.0 } diff --git a/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt b/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt index 0e10f1a9a..e49adc82e 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/series/analyzeDif.kt @@ -13,7 +13,10 @@ import space.kscience.kmath.structures.slice import space.kscience.plotly.* import kotlin.math.PI -fun main() = with(Double.algebra.bufferAlgebra.seriesAlgebra()) { +fun Double.Companion.seriesAlgebra() = Double.algebra.bufferAlgebra.seriesAlgebra() + + +fun main() = with(Double.seriesAlgebra()) { fun Plot.plotSeries(name: String, buffer: Buffer) { diff --git a/examples/src/main/kotlin/space/kscience/kmath/series/seriesBuilder.kt b/examples/src/main/kotlin/space/kscience/kmath/series/seriesBuilder.kt new file mode 100644 index 000000000..f8a8f1d0b --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/series/seriesBuilder.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2018-2023 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.series + + +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.asBuffer +import space.kscience.kmath.structures.toDoubleArray +import space.kscience.plotly.* +import space.kscience.plotly.models.Scatter +import space.kscience.plotly.models.ScatterMode +import kotlin.random.Random + +fun main(): Unit = with(Double.seriesAlgebra()) { + + val random = Random(1234) + + val arrayOfRandoms = DoubleArray(20) { random.nextDouble() } + + val series1: DoubleBuffer = arrayOfRandoms.asBuffer() + val series2: Series = series1.moveBy(3) + + val res = series2 - series1 + + println(res.size) + + println(res) + + fun Plot.series(name: String, buffer: Buffer, block: Scatter.() -> Unit = {}) { + scatter { + this.name = name + x.numbers = buffer.offsetIndices + y.doubles = buffer.toDoubleArray() + block() + } + } + + Plotly.plot { + series("series1", series1) + series("series2", series2) + series("dif", res){ + mode = ScatterMode.lines + line.color("magenta") + } + }.makeFile(resourceLocation = ResourceLocation.REMOTE) +} \ No newline at end of file diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt index 2ce2c21a6..9877c41ad 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt @@ -52,6 +52,15 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND Double): MutableBufferND { + val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> + val index = strides.index(offset) + DoubleField.initializer(index) + }.toArray() + + return MutableBufferND(strides, array.asBuffer()) + } + @OptIn(PerformancePitfall::class) override fun StructureND.map( transform: DoubleField.(Double) -> Double, diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/mutableNd.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/mutableNd.kt new file mode 100644 index 000000000..0b0a4cac1 --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/mutableNd.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2018-2023 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.structures + +import space.kscience.kmath.PerformancePitfall +import space.kscience.kmath.nd.* +import space.kscience.kmath.operations.algebra + +@OptIn(PerformancePitfall::class) +fun main(): Unit = with(Double.algebra.ndAlgebra) { + val structure: MutableStructure2D = mutableStructureND(ShapeND(2, 2)) { (i, j) -> + i.toDouble() + j.toDouble() + }.as2D() + + structure[0, 1] = -2.0 + + val structure2 = mutableStructureND(2, 2) { (i, j) -> i.toDouble() + j.toDouble() }.as2D() + + structure2[0, 1] = 2.0 + + + println(structure + structure2) +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 070cb702f..59bc51a20 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index 08411be59..600b0b8ce 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -37,8 +37,8 @@ kotlin.sourceSets { filter { it.name.contains("test", true) } .map(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet::languageSettings) .forEach { - it.optIn("space.kscience.kmath.misc.PerformancePitfall") - it.optIn("space.kscience.kmath.misc.UnstableKMathAPI") + it.optIn("space.kscience.kmath.PerformancePitfall") + it.optIn("space.kscience.kmath.UnstableKMathAPI") } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt index 91e26cc1b..f6626432d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/AlgebraND.kt @@ -16,16 +16,22 @@ import kotlin.reflect.KClass * @param T the type of ND-structure element. * @param C the type of the element context. */ -public interface AlgebraND>: Algebra> { +public interface AlgebraND> : Algebra> { /** * The algebra over elements of ND structure. */ public val elementAlgebra: C + /** + * Produces a new [MutableStructureND] using given initializer function. + */ + public fun mutableStructureND(shape: ShapeND, initializer: C.(IntArray) -> T): MutableStructureND + /** * Produces a new [StructureND] using given initializer function. */ - public fun structureND(shape: ShapeND, initializer: C.(IntArray) -> T): StructureND + public fun structureND(shape: ShapeND, initializer: C.(IntArray) -> T): StructureND = + mutableStructureND(shape, initializer) /** * Maps elements from one structure to another one by applying [transform] to them. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt index 74c63e6e2..78bc83826 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferAlgebraND.kt @@ -16,9 +16,10 @@ public interface BufferAlgebraND> : AlgebraND { public val bufferAlgebra: BufferAlgebra override val elementAlgebra: A get() = bufferAlgebra.elementAlgebra - override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): BufferND { + //TODO change AlgebraND contract to include this + override fun mutableStructureND(shape: ShapeND, initializer: A.(IntArray) -> T): MutableBufferND { val indexer = indexerBuilder(shape) - return BufferND( + return MutableBufferND( indexer, bufferAlgebra.buffer(indexer.linearSize) { offset -> elementAlgebra.initializer(indexer.index(offset)) @@ -26,6 +27,9 @@ public interface BufferAlgebraND> : AlgebraND { ) } + override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): BufferND = + mutableStructureND(shape, initializer) + @OptIn(PerformancePitfall::class) public fun StructureND.toBufferND(): BufferND = when (this) { is BufferND -> this @@ -133,6 +137,11 @@ public fun > BufferAlgebraND.structureND( initializer: A.(IntArray) -> T, ): BufferND = structureND(ShapeND(shape), initializer) +public fun > BufferAlgebraND.mutableStructureND( + vararg shape: Int, + initializer: A.(IntArray) -> T, +): MutableBufferND = mutableStructureND(ShapeND(shape), initializer) + public fun , A> A.structureND( initializer: EA.(IntArray) -> T, ): BufferND where A : BufferAlgebraND, A : WithShape = structureND(shape, initializer) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt index 265d1eec8..15048efa2 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt @@ -74,7 +74,7 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D transform: DoubleField.(Double, Double) -> Double, ): BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> DoubleField.transform(l, r) } - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): DoubleBufferND { + override fun mutableStructureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): DoubleBufferND { val indexer = indexerBuilder(shape) return DoubleBufferND( indexer, @@ -225,7 +225,7 @@ public class DoubleFieldND(override val shape: ShapeND) : override fun number(value: Number): DoubleBufferND { val d = value.toDouble() // minimize conversions - return structureND(shape) { d } + return mutableStructureND(shape) { d } } } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt index e643186ba..f0a258389 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/StructureND.kt @@ -244,6 +244,7 @@ public interface MutableStructureND : StructureND { * Set value at specified indices */ @PerformancePitfall +@Deprecated("") public operator fun MutableStructureND.set(vararg index: Int, value: T) { set(index, value) } \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt index f0d4bd7f5..506f82da4 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/algebraNDExtentions.kt @@ -17,6 +17,12 @@ public fun > AlgebraND.structureND( initializer: A.(IntArray) -> T ): StructureND = structureND(ShapeND(shapeFirst, *shapeRest), initializer) +public fun > AlgebraND.mutableStructureND( + shapeFirst: Int, + vararg shapeRest: Int, + initializer: A.(IntArray) -> T +): MutableStructureND = mutableStructureND(ShapeND(shapeFirst, *shapeRest), initializer) + public fun > AlgebraND.zero(shape: ShapeND): StructureND = structureND(shape) { zero } @JvmName("zeroVarArg") diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt index af0bc4d9b..26a4b0a53 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt @@ -6,7 +6,8 @@ package space.kscience.kmath.operations import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.BufferFactory +import space.kscience.kmath.structures.MutableBuffer +import space.kscience.kmath.structures.MutableBufferFactory public interface WithSize { public val size: Int @@ -17,7 +18,7 @@ public interface WithSize { */ public interface BufferAlgebra> : Algebra> { public val elementAlgebra: A - public val elementBufferFactory: BufferFactory get() = elementAlgebra.bufferFactory + public val elementBufferFactory: MutableBufferFactory get() = elementAlgebra.bufferFactory public fun buffer(size: Int, vararg elements: T): Buffer { require(elements.size == size) { "Expected $size elements but found ${elements.size}" } @@ -73,11 +74,11 @@ private inline fun > BufferAlgebra.zipInline( return elementBufferFactory(l.size) { elementAlgebra.block(l[it], r[it]) } } -public fun BufferAlgebra.buffer(size: Int, initializer: (Int) -> T): Buffer { +public fun BufferAlgebra.buffer(size: Int, initializer: (Int) -> T): MutableBuffer { return elementBufferFactory(size, initializer) } -public fun A.buffer(initializer: (Int) -> T): Buffer where A : BufferAlgebra, A : WithSize { +public fun A.buffer(initializer: (Int) -> T): MutableBuffer where A : BufferAlgebra, A : WithSize { return elementBufferFactory(size, initializer) } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt index c3a82b167..c5bbebfd8 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikTensorAlgebra.kt @@ -33,7 +33,7 @@ public abstract class MultikTensorAlgebra>( protected val multikStat: Statistics = multikEngine.getStatistics() @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): MultikTensor { + override fun mutableStructureND(shape: ShapeND, initializer: A.(IntArray) -> T): MultikTensor { val strides = ColumnStrides(shape) val memoryView = initMemoryView(strides.linearSize, type) strides.asSequence().forEachIndexed { linearIndex, tensorIndex -> @@ -49,7 +49,7 @@ public abstract class MultikTensorAlgebra>( for (el in array) data[count++] = elementAlgebra.transform(el) NDArray(data, shape = shape.asArray(), dim = array.dim).wrap() } else { - structureND(shape) { index -> + mutableStructureND(shape) { index -> transform(get(index)) } } @@ -70,7 +70,7 @@ public abstract class MultikTensorAlgebra>( } NDArray(data, shape = array.shape, dim = array.dim).wrap() } else { - structureND(shape) { index -> + mutableStructureND(shape) { index -> transform(index, get(index)) } } diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt index 0eb147b6f..eab6672a1 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt @@ -34,7 +34,7 @@ public sealed interface Nd4jArrayAlgebra> : AlgebraND.ndArray: INDArray @OptIn(PerformancePitfall::class) - override fun structureND(shape: ShapeND, initializer: C.(IntArray) -> T): Nd4jArrayStructure { + override fun mutableStructureND(shape: ShapeND, initializer: C.(IntArray) -> T): Nd4jArrayStructure { @OptIn(UnsafeKMathAPI::class) val struct: Nd4jArrayStructure = Nd4j.create(*shape.asArray())!!.wrap() struct.indicesIterator().forEach { struct[it] = elementAlgebra.initializer(it) } diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt index 5905739f8..44401ec53 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt @@ -37,20 +37,20 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe */ public val StructureND.ndArray: INDArray - override fun structureND(shape: ShapeND, initializer: A.(IntArray) -> T): Nd4jArrayStructure + override fun mutableStructureND(shape: ShapeND, initializer: A.(IntArray) -> T): Nd4jArrayStructure @OptIn(PerformancePitfall::class) override fun StructureND.map(transform: A.(T) -> T): Nd4jArrayStructure = - structureND(shape) { index -> elementAlgebra.transform(get(index)) } + mutableStructureND(shape) { index -> elementAlgebra.transform(get(index)) } @OptIn(PerformancePitfall::class) override fun StructureND.mapIndexed(transform: A.(index: IntArray, T) -> T): Nd4jArrayStructure = - structureND(shape) { index -> elementAlgebra.transform(index, get(index)) } + mutableStructureND(shape) { index -> elementAlgebra.transform(index, get(index)) } @OptIn(PerformancePitfall::class) override fun zip(left: StructureND, right: StructureND, transform: A.(T, T) -> T): Nd4jArrayStructure { require(left.shape.contentEquals(right.shape)) - return structureND(left.shape) { index -> elementAlgebra.transform(left[index], right[index]) } + return mutableStructureND(left.shape) { index -> elementAlgebra.transform(left[index], right[index]) } } override fun T.plus(arg: StructureND): Nd4jArrayStructure = arg.ndArray.add(this).wrap() @@ -178,7 +178,7 @@ public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra { override fun INDArray.wrap(): Nd4jArrayStructure = asDoubleStructure() @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): Nd4jArrayStructure { + override fun mutableStructureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): Nd4jArrayStructure { val array: INDArray = Nd4j.zeros(*shape.asArray()) val indices = ColumnStrides(shape) indices.asSequence().forEach { index -> diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt index 41c7c0b68..dceb0cb53 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowAlgebra.kt @@ -14,6 +14,7 @@ import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.nd.ColumnStrides +import space.kscience.kmath.nd.MutableStructureND import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.StructureND import space.kscience.kmath.operations.DoubleField @@ -36,10 +37,10 @@ public class DoubleTensorFlowAlgebra internal constructor( override val elementAlgebra: DoubleField get() = DoubleField - override fun structureND( + override fun mutableStructureND( shape: ShapeND, initializer: DoubleField.(IntArray) -> Double, - ): StructureND { + ): MutableStructureND { val res = TFloat64.tensorOf(org.tensorflow.ndarray.Shape.of(*shape.toLongArray())) { array -> ColumnStrides(shape).forEach { index -> array.setDouble(elementAlgebra.initializer(index), *index.toLongArray()) diff --git a/kmath-tensors/build.gradle.kts b/kmath-tensors/build.gradle.kts index d27faeeef..7a556ddb6 100644 --- a/kmath-tensors/build.gradle.kts +++ b/kmath-tensors/build.gradle.kts @@ -15,12 +15,12 @@ kscience{ kotlin.sourceSets { all { - languageSettings.optIn("space.kscience.kmath.misc.UnstableKMathAPI") + languageSettings.optIn("space.kscience.kmath.UnstableKMathAPI") } filter { it.name.contains("test", true) } .map(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet::languageSettings) - .forEach { it.optIn("space.kscience.kmath.misc.PerformancePitfall") } + .forEach { it.optIn("space.kscience.kmath.PerformancePitfall") } commonMain { dependencies { 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 70a3ef7e2..7a076b8b2 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 @@ -127,7 +127,7 @@ public open class DoubleTensorAlgebra : * @param initializer mapping tensor indices to values. * @return tensor with the [shape] shape and data generated by the [initializer]. */ - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): DoubleTensor = fromArray( + override fun mutableStructureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): DoubleTensor = fromArray( shape, RowStrides(shape).asSequence().map { DoubleField.initializer(it) }.toMutableList().toDoubleArray() ) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt index d1cdc68d4..c20b1cd7d 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt @@ -118,7 +118,7 @@ public open class IntTensorAlgebra : TensorAlgebra { * @param initializer mapping tensor indices to values. * @return tensor with the [shape] shape and data generated by the [initializer]. */ - override fun structureND(shape: ShapeND, initializer: IntRing.(IntArray) -> Int): IntTensor = fromArray( + override fun mutableStructureND(shape: ShapeND, initializer: IntRing.(IntArray) -> Int): IntTensor = fromArray( shape, RowStrides(shape).asSequence().map { IntRing.initializer(it) }.toMutableList().toIntArray() ) diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt index 811fc1117..1f3079bd0 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt @@ -92,7 +92,7 @@ internal class TestDoubleTensor { @Test fun test2D() = with(DoubleTensorAlgebra) { - val tensor: DoubleTensor = structureND(ShapeND(3, 3)) { (i, j) -> (i - j).toDouble() } + val tensor: DoubleTensor = mutableStructureND(ShapeND(3, 3)) { (i, j) -> (i - j).toDouble() } //println(tensor.toPrettyString()) val tensor2d = tensor.asDoubleTensor2D() assertBufferEquals(DoubleBuffer(1.0, 0.0, -1.0), tensor2d.rows[1]) @@ -101,7 +101,7 @@ internal class TestDoubleTensor { @Test fun testMatrixIteration() = with(DoubleTensorAlgebra) { - val tensor = structureND(ShapeND(3, 3, 3, 3)) { index -> index.sum().toDouble() } + val tensor = mutableStructureND(ShapeND(3, 3, 3, 3)) { index -> index.sum().toDouble() } tensor.forEachMatrix { index, matrix -> println(index.joinToString { it.toString() }) println(matrix) diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt index 8c7d6d199..ed705d2cd 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt @@ -17,8 +17,7 @@ import space.kscience.kmath.operations.ExtendedFieldOps import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.PowerOperations -@OptIn(UnstableKMathAPI::class, PerformancePitfall::class) -@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") +@OptIn(PerformancePitfall::class) public open class ViktorFieldOpsND : FieldOpsND, ExtendedFieldOps>, @@ -27,13 +26,13 @@ public open class ViktorFieldOpsND : public val StructureND.f64Buffer: F64Array get() = when (this) { is ViktorStructureND -> this.f64Buffer - else -> structureND(shape) { this@f64Buffer[it] }.f64Buffer + else -> mutableStructureND(shape) { this@f64Buffer[it] }.f64Buffer } override val elementAlgebra: DoubleField get() = DoubleField @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): ViktorStructureND = + override fun mutableStructureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): ViktorStructureND = F64Array(*shape.asArray()).apply { ColumnStrides(shape).asSequence().forEach { index -> set(value = DoubleField.initializer(index), indices = index)