From ca9df8a1671abda0307fc5ef98257d091a95410f Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 8 Feb 2024 18:06:06 +0300 Subject: [PATCH] Add more corner cases for complex power --- .../kotlin/space/kscience/kmath/complex/Complex.kt | 11 +++++++++-- .../space/kscience/kmath/complex/ComplexFieldTest.kt | 5 +++++ .../kotlin/space/kscience/kmath/ejml/_generated.kt | 10 ++-------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt index 009662176..2608f9f65 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt @@ -139,12 +139,19 @@ public object ComplexField : zero } } - } else { exp(pow * ln(arg)) } - public fun power(arg: Complex, pow: Complex): Complex = exp(pow * ln(arg)) + public fun power(arg: Complex, pow: Complex): Complex = if(arg == zero || arg == (-0.0).toComplex()){ + if(pow == zero){ + one + } else { + zero + } + } else { + exp(pow * ln(arg)) + } public fun Complex.pow(power: Complex): Complex = power(this, power) diff --git a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt index cc1f3ad0a..06f7f4580 100644 --- a/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt +++ b/kmath-complex/src/commonTest/kotlin/space/kscience/kmath/complex/ComplexFieldTest.kt @@ -71,6 +71,11 @@ internal class ComplexFieldTest { (i * 8).let { it.im.toInt() to it.re.toInt() }, (Complex(2, 2) pow 2).let { it.im.toInt() to it.re.toInt() }) + assertEquals(1.0, Complex(0.0).pow(Complex(0.0)).re, 0.01) + assertEquals(1.0, Complex(-0.0).pow(Complex(0.0)).re, 0.01) + assertEquals(0.0, Complex(0.0).pow(Complex(2.0)).re, 0.01) + assertEquals(0.0, Complex(-0.0).pow(Complex(2.0)).re, 0.01) + assertEquals(1.0, Complex(-1.0).pow(Complex(2.0)).re, 0.01) assertEquals(2.0, power(Complex(-4.0, 0.0), 0.5 + 0 * i).im, 0.01) assertEquals(2.0, power(Complex(-4.0, 0.0), 0.5).im, 0.01) } diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt index 7a267b77b..a2622eee2 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt @@ -19,19 +19,13 @@ import org.ejml.sparse.csc.factory.DecompositionFactory_DSCC import org.ejml.sparse.csc.factory.DecompositionFactory_FSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureFeature -import space.kscience.kmath.structures.Float64 -import space.kscience.kmath.structures.Float32 -import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.Float32Field -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.FloatField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.Float64Buffer -import space.kscience.kmath.structures.Float32Buffer import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.FloatBuffer import kotlin.reflect.KClass