From 9c353f4a0d3e0d2c09d8551b9efdc492fc43dbab Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 20 Apr 2021 22:48:09 +0300 Subject: [PATCH] Make ND4J float algebra extended --- CHANGELOG.md | 1 + .../kscience/kmath/nd4j/Nd4jArrayAlgebra.kt | 46 +++++++++++++++++-- .../kmath/nd4j/Nd4jArrayAlgebraTest.kt | 14 ++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d33a75a..7464695ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Blocking chains and Statistics - Multiplatform integration - Integration for any Field element +- Extendend operations for ND4J fields ### Changed - Exponential operations merged with hyperbolic functions diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt index f2c69adee..0205f1c87 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt @@ -6,7 +6,10 @@ package space.kscience.kmath.nd4j import org.nd4j.linalg.api.ndarray.INDArray +import org.nd4j.linalg.api.ops.impl.scalar.Pow +import org.nd4j.linalg.api.ops.impl.transforms.strict.* import org.nd4j.linalg.factory.Nd4j +import org.nd4j.linalg.ops.transforms.Transforms import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.nd.* import space.kscience.kmath.operations.* @@ -207,7 +210,8 @@ public interface Nd4jArrayField> : FieldND, Nd4jArrayRing< /** * Represents [FieldND] over [Nd4jArrayDoubleStructure]. */ -public class DoubleNd4jArrayField(public override val shape: IntArray) : Nd4jArrayField { +public class DoubleNd4jArrayField(public override val shape: IntArray) : Nd4jArrayField, + ExtendedField> { public override val elementContext: DoubleField get() = DoubleField public override fun INDArray.wrap(): Nd4jArrayStructure = checkShape(this).asDoubleStructure() @@ -239,14 +243,31 @@ public class DoubleNd4jArrayField(public override val shape: IntArray) : Nd4jArr public override operator fun Double.minus(arg: StructureND): Nd4jArrayStructure { return arg.ndArray.rsub(this).wrap() } + + override fun sin(arg: StructureND): StructureND = Transforms.sin(arg.ndArray).wrap() + + override fun cos(arg: StructureND): StructureND = Transforms.cos(arg.ndArray).wrap() + + override fun asin(arg: StructureND): StructureND = Transforms.asin(arg.ndArray).wrap() + + override fun acos(arg: StructureND): StructureND = Transforms.acos(arg.ndArray).wrap() + + override fun atan(arg: StructureND): StructureND = Transforms.atan(arg.ndArray).wrap() + + override fun power(arg: StructureND, pow: Number): StructureND = + Transforms.pow(arg.ndArray,pow).wrap() + + override fun exp(arg: StructureND): StructureND = Transforms.exp(arg.ndArray).wrap() + + override fun ln(arg: StructureND): StructureND = Transforms.log(arg.ndArray).wrap() } /** * Represents [FieldND] over [Nd4jArrayStructure] of [Float]. */ -public class FloatNd4jArrayField(public override val shape: IntArray) : Nd4jArrayField { - public override val elementContext: FloatField - get() = FloatField +public class FloatNd4jArrayField(public override val shape: IntArray) : Nd4jArrayField, + ExtendedField> { + public override val elementContext: FloatField get() = FloatField public override fun INDArray.wrap(): Nd4jArrayStructure = checkShape(this).asFloatStructure() @@ -270,6 +291,23 @@ public class FloatNd4jArrayField(public override val shape: IntArray) : Nd4jArra public override operator fun Float.minus(arg: StructureND): Nd4jArrayStructure = arg.ndArray.rsub(this).wrap() + + override fun sin(arg: StructureND): StructureND = Sin(arg.ndArray).z().wrap() + + override fun cos(arg: StructureND): StructureND = Cos(arg.ndArray).z().wrap() + + override fun asin(arg: StructureND): StructureND = ASin(arg.ndArray).z().wrap() + + override fun acos(arg: StructureND): StructureND = ACos(arg.ndArray).z().wrap() + + override fun atan(arg: StructureND): StructureND = ATan(arg.ndArray).z().wrap() + + override fun power(arg: StructureND, pow: Number): StructureND = + Pow(arg.ndArray, pow.toDouble()).z().wrap() + + override fun exp(arg: StructureND): StructureND = Exp(arg.ndArray).z().wrap() + + override fun ln(arg: StructureND): StructureND = Log(arg.ndArray).z().wrap() } /** diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt index c3874b249..b6c524cc6 100644 --- a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt @@ -6,8 +6,12 @@ package space.kscience.kmath.nd4j import org.nd4j.linalg.factory.Nd4j +import space.kscience.kmath.nd.StructureND +import space.kscience.kmath.operations.invoke +import kotlin.math.PI import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue import kotlin.test.fail internal class Nd4jArrayAlgebraTest { @@ -43,4 +47,14 @@ internal class Nd4jArrayAlgebraTest { expected[intArrayOf(1, 1)] = 26 assertEquals(expected, res) } + + @Test + fun testSin() = DoubleNd4jArrayField(intArrayOf(2, 2)).invoke { + val initial = produce { (i, j) -> if (i == j) PI/2 else 0.0 } + val transformed = sin(initial) + val expected = produce { (i, j) -> if (i == j) 1.0 else 0.0 } + + println(transformed) + assertTrue { StructureND.contentEquals(transformed, expected) } + } }