diff --git a/kmath-nd4j/README.md b/kmath-nd4j/README.md new file mode 100644 index 000000000..63687a880 --- /dev/null +++ b/kmath-nd4j/README.md @@ -0,0 +1,73 @@ +# ND4J NDStructure implementation (`kmath-nd4j`) + +This subproject implements the following features: + +- NDStructure wrapper for INDArray. +- Optimized NDRing implementation for INDArray storing Ints. +- Optimized NDField implementation for INDArray storing Floats and Doubles. + +> #### Artifact: +> This module is distributed in the artifact `scientifik:kmath-nd4j:0.1.4-dev-8`. +> +> **Gradle:** +> +> ```gradle +> repositories { +> maven { url 'https://dl.bintray.com/mipt-npm/scientifik' } +> maven { url 'https://dl.bintray.com/mipt-npm/dev' } +> } +> +> dependencies { +> implementation 'scientifik:kmath-nd4j:0.1.4-dev-8' +> } +> ``` +> **Gradle Kotlin DSL:** +> +> ```kotlin +> repositories { +> maven("https://dl.bintray.com/mipt-npm/scientifik") +> maven("https://dl.bintray.com/mipt-npm/dev") +> } +> +> dependencies { +> implementation("scientifik:kmath-nd4j:0.1.4-dev-8") +> } +> ``` +> + +## Examples + +NDStructure wrapper for INDArray: + +```kotlin +import org.nd4j.linalg.factory.* +import scientifik.kmath.nd4j.* +import scientifik.kmath.structures.* + +val array = Nd4j.ones(2, 2)!!.asRealStructure() +println(array[0, 0]) // 1.0 +array[intArrayOf(0, 0)] = 24.0 +println(array[0, 0]) // 24.0 +``` + +Fast element-wise arithmetics for INDArray: + +```kotlin +import org.nd4j.linalg.factory.* +import scientifik.kmath.nd4j.* +import scientifik.kmath.operations.* + +val field = RealINDArrayField(intArrayOf(2, 2)) +val array = Nd4j.rand(2, 2)!!.asRealStructure() + +val res = field { + (25.0 / array + 20) * 4 +} + +println(res.ndArray) +// [[ 250.6449, 428.5840], +// [ 269.7913, 202.2077]] +``` + + +Contributed by [Iaroslav Postovalov](https://github.com/CommanderTvis). diff --git a/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayStructures.kt b/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayStructures.kt index ef7436285..06b0354d8 100644 --- a/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayStructures.kt +++ b/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayStructures.kt @@ -15,7 +15,7 @@ interface INDArrayStructure : NDStructure { } data class INDArrayIntStructure(override val ndArray: INDArray) : INDArrayStructure, MutableNDStructure { - override fun elementsIterator(): Iterator> = INDArrayIntIterator(ndArray) + override fun elementsIterator(): Iterator> = ndArray.intIterator() override fun get(index: IntArray): Int = ndArray.getInt(*index) override fun set(index: IntArray, value: Int): Unit = run { ndArray.putScalar(index, value) } } @@ -23,7 +23,7 @@ data class INDArrayIntStructure(override val ndArray: INDArray) : INDArrayStruct fun INDArray.asIntStructure(): INDArrayIntStructure = INDArrayIntStructure(this) data class INDArrayLongStructure(override val ndArray: INDArray) : INDArrayStructure { - override fun elementsIterator(): Iterator> = INDArrayLongIterator(ndArray) + override fun elementsIterator(): Iterator> = ndArray.longIterator() override fun get(index: IntArray): Long = ndArray.getLong(*index.toLongArray()) } @@ -31,7 +31,7 @@ fun INDArray.asLongStructure(): INDArrayLongStructure = INDArrayLongStructure(th data class INDArrayRealStructure(override val ndArray: INDArray) : INDArrayStructure, MutableNDStructure { - override fun elementsIterator(): Iterator> = INDArrayRealIterator(ndArray) + override fun elementsIterator(): Iterator> = ndArray.realIterator() override fun get(index: IntArray): Double = ndArray.getDouble(*index) override fun set(index: IntArray, value: Double): Unit = run { ndArray.putScalar(index, value) } } @@ -40,7 +40,7 @@ fun INDArray.asRealStructure(): INDArrayRealStructure = INDArrayRealStructure(th data class INDArrayFloatStructure(override val ndArray: INDArray) : INDArrayStructure, MutableNDStructure { - override fun elementsIterator(): Iterator> = INDArrayFloatIterator(ndArray) + override fun elementsIterator(): Iterator> = ndArray.floatIterator() override fun get(index: IntArray): Float = ndArray.getFloat(*index) override fun set(index: IntArray, value: Float): Unit = run { ndArray.putScalar(index, value) } }