Dev #194
73
kmath-nd4j/README.md
Normal file
73
kmath-nd4j/README.md
Normal file
@ -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).
|
@ -15,7 +15,7 @@ interface INDArrayStructure<T> : NDStructure<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data class INDArrayIntStructure(override val ndArray: INDArray) : INDArrayStructure<Int>, MutableNDStructure<Int> {
|
data class INDArrayIntStructure(override val ndArray: INDArray) : INDArrayStructure<Int>, MutableNDStructure<Int> {
|
||||||
override fun elementsIterator(): Iterator<Pair<IntArray, Int>> = INDArrayIntIterator(ndArray)
|
override fun elementsIterator(): Iterator<Pair<IntArray, Int>> = ndArray.intIterator()
|
||||||
override fun get(index: IntArray): Int = ndArray.getInt(*index)
|
override fun get(index: IntArray): Int = ndArray.getInt(*index)
|
||||||
override fun set(index: IntArray, value: Int): Unit = run { ndArray.putScalar(index, value) }
|
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)
|
fun INDArray.asIntStructure(): INDArrayIntStructure = INDArrayIntStructure(this)
|
||||||
|
|
||||||
data class INDArrayLongStructure(override val ndArray: INDArray) : INDArrayStructure<Long> {
|
data class INDArrayLongStructure(override val ndArray: INDArray) : INDArrayStructure<Long> {
|
||||||
override fun elementsIterator(): Iterator<Pair<IntArray, Long>> = INDArrayLongIterator(ndArray)
|
override fun elementsIterator(): Iterator<Pair<IntArray, Long>> = ndArray.longIterator()
|
||||||
override fun get(index: IntArray): Long = ndArray.getLong(*index.toLongArray())
|
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<Double>,
|
data class INDArrayRealStructure(override val ndArray: INDArray) : INDArrayStructure<Double>,
|
||||||
MutableNDStructure<Double> {
|
MutableNDStructure<Double> {
|
||||||
override fun elementsIterator(): Iterator<Pair<IntArray, Double>> = INDArrayRealIterator(ndArray)
|
override fun elementsIterator(): Iterator<Pair<IntArray, Double>> = ndArray.realIterator()
|
||||||
override fun get(index: IntArray): Double = ndArray.getDouble(*index)
|
override fun get(index: IntArray): Double = ndArray.getDouble(*index)
|
||||||
override fun set(index: IntArray, value: Double): Unit = run { ndArray.putScalar(index, value) }
|
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<Float>,
|
data class INDArrayFloatStructure(override val ndArray: INDArray) : INDArrayStructure<Float>,
|
||||||
MutableNDStructure<Float> {
|
MutableNDStructure<Float> {
|
||||||
override fun elementsIterator(): Iterator<Pair<IntArray, Float>> = INDArrayFloatIterator(ndArray)
|
override fun elementsIterator(): Iterator<Pair<IntArray, Float>> = ndArray.floatIterator()
|
||||||
override fun get(index: IntArray): Float = ndArray.getFloat(*index)
|
override fun get(index: IntArray): Float = ndArray.getFloat(*index)
|
||||||
override fun set(index: IntArray, value: Float): Unit = run { ndArray.putScalar(index, value) }
|
override fun set(index: IntArray, value: Float): Unit = run { ndArray.putScalar(index, value) }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user