File tree Expand file tree Collapse file tree 4 files changed +58
-2
lines changed
jvmMain/kotlin/com/martmists/ndarray/simd/compat Expand file tree Collapse file tree 4 files changed +58
-2
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ repositories {
1212}
1313
1414dependencies {
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
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ plugins {
1717}
1818
1919group = " com.martmists.ndarray-simd"
20- version = " 1.5.5 "
20+ version = " 1.6.0 "
2121val isProduction = (findProperty(" production" ) ? : System .getProperty(" production" )) != null
2222
2323repositories {
@@ -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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments