Add more corner cases for complex power

This commit is contained in:
Alexander Nozik 2024-02-08 18:06:06 +03:00
parent 83d9e1f0af
commit ca9df8a167
3 changed files with 16 additions and 10 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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