Update typealiases usages; add optimization for exponentiation real complex numbers

This commit is contained in:
Commander Tvis 2020-07-03 17:38:13 +07:00
parent badb7b1365
commit 77625cca2b
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
2 changed files with 23 additions and 18 deletions

View File

@ -104,7 +104,12 @@ object ComplexField : ExtendedField<Complex> {
override fun asinh(arg: Complex): Complex = ln(sqrt((arg pow 2) + 1) + arg)
override fun acosh(arg: Complex): Complex = ln(arg + sqrt((arg - 1) * (arg + 1)))
override fun atanh(arg: Complex): Complex = (ln(arg + 1) - ln(1 - arg)) / 2
override fun power(arg: Complex, pow: Number): Complex = exp(ln(arg) * pow)
override fun power(arg: Complex, pow: Number): Complex = if (arg.im == 0.0)
arg.re.pow(pow.toDouble()).toComplex()
else
exp(pow * ln(arg))
override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im))
override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re)
@ -145,9 +150,9 @@ data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Compl
}
/**
* Creates a [Complex] with its real part of this double.
* Creates a [Complex] with real part equal to this number.
*/
fun Double.toComplex(): Complex = Complex(this, 0.0)
fun Number.toComplex(): Complex = Complex(this, 0.0)
inline fun Buffer.Companion.complex(size: Int, crossinline init: (Int) -> Complex): Buffer<Complex> =
MemoryBuffer.create(Complex, size, init)

View File

@ -70,25 +70,25 @@ class ComplexNDField(override val shape: IntArray) :
override fun NDBuffer<Complex>.toElement(): FieldElement<NDBuffer<Complex>, *, out BufferedNDField<Complex, ComplexField>> =
BufferedNDFieldElement(this@ComplexNDField, buffer)
override fun power(arg: NDBuffer<Complex>, pow: Number): BufferedNDFieldElement<Complex, ComplexField> =
override fun power(arg: NDBuffer<Complex>, pow: Number): ComplexNDElement =
map(arg) { power(it, pow) }
override fun exp(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { exp(it) }
override fun ln(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { ln(it) }
override fun exp(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { exp(it) }
override fun ln(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { ln(it) }
override fun sin(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { sin(it) }
override fun cos(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { cos(it) }
override fun tan(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { tan(it) }
override fun asin(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { asin(it) }
override fun acos(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { acos(it) }
override fun atan(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { atan(it) }
override fun sin(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { sin(it) }
override fun cos(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { cos(it) }
override fun tan(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { tan(it) }
override fun asin(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { asin(it) }
override fun acos(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { acos(it) }
override fun atan(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { atan(it) }
override fun sinh(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { sinh(it) }
override fun cosh(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { cosh(it) }
override fun tanh(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { tanh(it) }
override fun asinh(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { asinh(it) }
override fun acosh(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { acosh(it) }
override fun atanh(arg: NDBuffer<Complex>): BufferedNDFieldElement<Complex, ComplexField> = map(arg) { atanh(it) }
override fun sinh(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { sinh(it) }
override fun cosh(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { cosh(it) }
override fun tanh(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { tanh(it) }
override fun asinh(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { asinh(it) }
override fun acosh(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { acosh(it) }
override fun atanh(arg: NDBuffer<Complex>): ComplexNDElement = map(arg) { atanh(it) }
}