From 09a9df5213b273c6848183355736729f58b39ba3 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 9 Oct 2021 20:19:36 +0300 Subject: [PATCH] Add complex power --- CHANGELOG.md | 1 + .../space/kscience/kmath/complex/Complex.kt | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cb9f57f4..05376d425 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Unified architecture for Integration and Optimization using features. - `BigInt` operation performance improvement and fixes by @zhelenskiy (#328) - Integration between `MST` and Symja `IExpr` +- Complex power ### Changed - Exponential operations merged with hyperbolic functions 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 793587492..7d948cb61 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 @@ -9,10 +9,7 @@ import space.kscience.kmath.memory.MemoryReader import space.kscience.kmath.memory.MemorySpec import space.kscience.kmath.memory.MemoryWriter import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.ExtendedField -import space.kscience.kmath.operations.Norm -import space.kscience.kmath.operations.NumbersAddOperations -import space.kscience.kmath.operations.ScaleOperations +import space.kscience.kmath.operations.* import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.MemoryBuffer import space.kscience.kmath.structures.MutableBuffer @@ -52,11 +49,23 @@ private val PI_DIV_2 = Complex(PI / 2, 0) * A field of [Complex]. */ @OptIn(UnstableKMathAPI::class) -public object ComplexField : ExtendedField, Norm, NumbersAddOperations, +public object ComplexField : + ExtendedField, + Norm, + NumbersAddOperations, ScaleOperations { + override val zero: Complex = 0.0.toComplex() override val one: Complex = 1.0.toComplex() + override fun bindSymbolOrNull(value: String): Complex? = if (value == "i") i else null + + override fun binaryOperationFunction(operation: String): (left: Complex, right: Complex) -> Complex = + when (operation) { + PowerOperations.POW_OPERATION -> ComplexField::power + else -> super.binaryOperationFunction(operation) + } + /** * The imaginary unit. */ @@ -117,10 +126,14 @@ public object ComplexField : ExtendedField, Norm, Num return i * (ln(1 - iArg) - ln(1 + iArg)) / 2 } - override fun power(arg: Complex, pow: Number): Complex = if (arg.im == 0.0) + override fun power(arg: Complex, pow: Number): Complex = if (arg.im == 0.0) { arg.re.pow(pow.toDouble()).toComplex() - else + } else { exp(pow * ln(arg)) + } + + public fun power(arg: Complex, pow: Complex): Complex = exp(pow * ln(arg)) + override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im)) @@ -172,8 +185,6 @@ public object ComplexField : ExtendedField, Norm, Num public operator fun Double.times(c: Complex): Complex = Complex(c.re * this, c.im * this) override fun norm(arg: Complex): Complex = sqrt(arg.conjugate * arg) - - override fun bindSymbolOrNull(value: String): Complex? = if (value == "i") i else null } /**