From 8a8b314d0a60c027f6abcfcff8d0d924613cd0d1 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 29 Jun 2020 03:48:11 +0700 Subject: [PATCH] Optimize reverse division for FP INDArrayAlgebra --- .../scientifik.kmath.nd4j/INDArrayAlgebra.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayAlgebra.kt b/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayAlgebra.kt index a4ecd09e5..44d8f6611 100644 --- a/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/scientifik.kmath.nd4j/INDArrayAlgebra.kt @@ -7,8 +7,8 @@ import scientifik.kmath.structures.MutableNDStructure import scientifik.kmath.structures.NDField import scientifik.kmath.structures.NDRing -interface INDArrayRing : - NDRing where F : Ring, N : INDArrayStructure, N : MutableNDStructure { +interface INDArrayRing : + NDRing where R : Ring, N : INDArrayStructure, N : MutableNDStructure { fun INDArray.wrap(): N override val zero: N @@ -17,13 +17,13 @@ interface INDArrayRing : override val one: N get() = Nd4j.ones(*shape).wrap() - override fun produce(initializer: F.(IntArray) -> T): N { + override fun produce(initializer: R.(IntArray) -> T): N { val struct = Nd4j.create(*shape).wrap() struct.elements().map(Pair::first).forEach { struct[it] = elementContext.initializer(it) } return struct } - override fun map(arg: N, transform: F.(T) -> T): N { + override fun map(arg: N, transform: R.(T) -> T): N { val new = Nd4j.create(*shape) Nd4j.copy(arg.ndArray, new) val newStruct = new.wrap() @@ -31,13 +31,13 @@ interface INDArrayRing : return newStruct } - override fun mapIndexed(arg: N, transform: F.(index: IntArray, T) -> T): N { + override fun mapIndexed(arg: N, transform: R.(index: IntArray, T) -> T): N { val new = Nd4j.create(*shape).wrap() new.elements().forEach { (idx, _) -> new[idx] = elementContext.transform(idx, arg[idx]) } return new } - override fun combine(a: N, b: N, transform: F.(T, T) -> T): N { + override fun combine(a: N, b: N, transform: R.(T, T) -> T): N { val new = Nd4j.create(*shape).wrap() new.elements().forEach { (idx, _) -> new[idx] = elementContext.transform(a[idx], b[idx]) } return new @@ -66,6 +66,7 @@ class RealINDArrayField(override val shape: IntArray, override val elementContex override fun INDArrayRealStructure.plus(arg: Double): INDArrayRealStructure = ndArray.addi(arg).wrap() override fun INDArrayRealStructure.minus(arg: Double): INDArrayRealStructure = ndArray.subi(arg).wrap() override fun INDArrayRealStructure.times(arg: Double): INDArrayRealStructure = ndArray.muli(arg).wrap() + override fun Double.div(arg: INDArrayRealStructure): INDArrayRealStructure = arg.ndArray.rdivi(this).wrap() } class FloatINDArrayField(override val shape: IntArray, override val elementContext: Field = FloatField) : @@ -75,6 +76,7 @@ class FloatINDArrayField(override val shape: IntArray, override val elementConte override fun INDArrayFloatStructure.plus(arg: Float): INDArrayFloatStructure = ndArray.addi(arg).wrap() override fun INDArrayFloatStructure.minus(arg: Float): INDArrayFloatStructure = ndArray.subi(arg).wrap() override fun INDArrayFloatStructure.times(arg: Float): INDArrayFloatStructure = ndArray.muli(arg).wrap() + override fun Float.div(arg: INDArrayFloatStructure): INDArrayFloatStructure = arg.ndArray.rdivi(this).wrap() } class IntINDArrayRing(override val shape: IntArray, override val elementContext: Ring = IntRing) :