Skip to content

Commit aeb6c11

Browse files
committed
Add support for Deep Java Library NDArray
1 parent 14d1e4a commit aeb6c11

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
}
1313

1414
dependencies {
15-
implementation("com.martmists.ndarray-simd:ndarray-simd:1.5.5")
15+
implementation("com.martmists.ndarray-simd:ndarray-simd:1.6.0")
1616
}
1717
```
1818

build.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ plugins {
1717
}
1818

1919
group = "com.martmists.ndarray-simd"
20-
version = "1.5.5"
20+
version = "1.6.0"
2121
val isProduction = (findProperty("production") ?: System.getProperty("production")) != null
2222

2323
repositories {
@@ -282,6 +282,9 @@ kotlin {
282282

283283
// Compat: kotlinx.dataframe
284284
compileOnly("org.jetbrains.kotlinx:dataframe-core:0.13.1")
285+
286+
// Compat: Deep Java Library
287+
compileOnly("ai.djl:api:0.32.0")
285288
}
286289
}
287290

@@ -293,10 +296,12 @@ kotlin {
293296
implementation("com.sksamuel.scrimage:scrimage-core:4.1.3")
294297
implementation("dev.langchain4j:langchain4j:0.32.0")
295298
implementation("org.jetbrains.kotlinx:dataframe-core:0.13.1")
299+
implementation("ai.djl:api:0.32.0")
296300

297301
implementation("org.jetbrains.exposed:exposed-core:0.60.0")
298302
implementation("org.jetbrains.exposed:exposed-jdbc:0.60.0")
299303
implementation("org.xerial:sqlite-jdbc:3.44.1.0")
304+
300305
}
301306
}
302307

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.martmists.ndarray.simd.compat
2+
3+
import ai.djl.ndarray.NDArray
4+
import ai.djl.ndarray.NDManager
5+
import ai.djl.ndarray.types.Shape
6+
import com.martmists.ndarray.simd.F64Array
7+
8+
private val fallbackManager by lazy {
9+
NDManager.newBaseManager()
10+
}
11+
12+
/**
13+
* Converts an [NDArray] to an [F64Array].
14+
* @since 1.6.0
15+
*/
16+
fun NDArray.toF64Array(): F64Array {
17+
val shape = IntArray(shape.dimension()) { shape.get(it).toInt() }
18+
return F64Array.of(toDoubleArray()).reshape(*shape)
19+
}
20+
21+
/**
22+
* Converts an [F64Array] to a [NDArray].
23+
* @param manager An optional manager to use. Falls back to [NDManager.newBaseManager] if null.
24+
* @since 1.6.0
25+
*/
26+
fun F64Array.toDJL(manager: NDManager? = null): NDArray {
27+
return (manager ?: fallbackManager).create(flatten().toDoubleArray(), Shape(shape.map(Int::toLong)))
28+
}

src/jvmTest/kotlin/DJLTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import com.martmists.ndarray.simd.F64Array
2+
import com.martmists.ndarray.simd.compat.fromImage
3+
import com.martmists.ndarray.simd.compat.image
4+
import com.martmists.ndarray.simd.compat.toDJL
5+
import com.martmists.ndarray.simd.compat.toF64Array
6+
import com.martmists.ndarray.simd.compat.toImage
7+
import com.martmists.ndarray.simd.compat.toOpenCVMat
8+
import org.opencv.core.CvType
9+
import java.io.File
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
13+
class DJLTest {
14+
private val image = F64Array.fromImage(File(this::class.java.getResource("sample.png")!!.toURI()))
15+
16+
@Test
17+
fun `Test DJL conversion`() {
18+
val nd = image.toDJL()
19+
val img = nd.toF64Array()
20+
img.neqInPlace(image)
21+
assertEquals(0.0, img.sum())
22+
}
23+
}

0 commit comments

Comments
 (0)