Fix after-merge problems

This commit is contained in:
Iaroslav Postovalov 2021-01-20 14:56:09 +07:00
parent 9748e0bfbe
commit 94a6d5a114
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
5 changed files with 89 additions and 50 deletions

View File

@ -5,15 +5,21 @@ plugins {
id("ru.mipt.npm.native") id("ru.mipt.npm.native")
} }
kotlin.sourceSets.commonMain { kotlin.sourceSets {
all {
languageSettings.useExperimentalAnnotation("kscience.kmath.misc.UnstableKMathAPI")
}
commonMain {
dependencies { dependencies {
api(project(":kmath-core")) api(project(":kmath-core"))
} }
}
} }
readme { readme {
description = "Complex numbers and quaternions." description = "Complex numbers and quaternions."
maturity = Maturity.DEVELOPMENT maturity = Maturity.PROTOTYPE
propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md"))
feature( feature(

View File

@ -6,6 +6,7 @@ import kscience.kmath.memory.MemoryWriter
import kscience.kmath.operations.ExtendedField import kscience.kmath.operations.ExtendedField
import kscience.kmath.operations.FieldElement import kscience.kmath.operations.FieldElement
import kscience.kmath.operations.Norm import kscience.kmath.operations.Norm
import kscience.kmath.operations.RingWithNumbers
import kscience.kmath.structures.Buffer import kscience.kmath.structures.Buffer
import kscience.kmath.structures.MemoryBuffer import kscience.kmath.structures.MemoryBuffer
import kscience.kmath.structures.MutableBuffer import kscience.kmath.structures.MutableBuffer
@ -44,14 +45,14 @@ private val PI_DIV_2 = Complex(PI / 2, 0)
/** /**
* A field of [Complex]. * A field of [Complex].
*/ */
public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex> { public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex>, RingWithNumbers<Complex> {
override val zero: Complex by lazy { 0.toComplex() } public override val zero: Complex by lazy { 0.0.toComplex() }
override val one: Complex by lazy { 1.toComplex() } public override val one: Complex by lazy { 1.0.toComplex() }
/** /**
* The imaginary unit. * The imaginary unit.
*/ */
public val i: Complex by lazy { Complex(0, 1) } public val i: Complex by lazy { Complex(0.0, 1.0) }
public override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im) public override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im)
public override fun multiply(a: Complex, k: Number): Complex = Complex(a.re * k.toDouble(), a.im * k.toDouble()) public override fun multiply(a: Complex, k: Number): Complex = Complex(a.re * k.toDouble(), a.im * k.toDouble())
@ -92,29 +93,71 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex> {
return i * (e1 - e2) / (e1 + e2) return i * (e1 - e2) / (e1 + e2)
} }
public override fun asin(arg: Complex): Complex = -i * ln(sqrt(one - (arg * arg)) + i * arg) public override fun asin(arg: Complex): Complex = -i * ln(sqrt(1 - (arg * arg)) + i * arg)
public override fun acos(arg: Complex): Complex = PI_DIV_2 + i * ln(sqrt(one - (arg * arg)) + i * arg) override fun acos(arg: Complex): Complex = PI_DIV_2 + i * ln(sqrt(1 - (arg * arg)) + i * arg)
public override fun atan(arg: Complex): Complex { override fun atan(arg: Complex): Complex {
val iArg = i * arg val iArg = i * arg
return i * (ln(1 - iArg) - ln(1 + iArg)) / 2 return i * (ln(1 - iArg) - ln(1 + iArg)) / 2
} }
public 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() arg.re.pow(pow.toDouble()).toComplex()
else else
exp(pow * ln(arg)) exp(pow * ln(arg))
public override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im)) override fun exp(arg: Complex): Complex = exp(arg.re) * (cos(arg.im) + i * sin(arg.im))
public override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re)
public override operator fun Number.plus(b: Complex): Complex = add(toComplex(), b) override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re)
public override operator fun Number.minus(b: Complex): Complex = add(toComplex(), -b)
public override operator fun Complex.plus(b: Number): Complex = b + this /**
public override operator fun Complex.minus(b: Number): Complex = add(this, -b.toComplex()) * Adds complex number to real one.
public override operator fun Number.times(b: Complex): Complex = Complex(b.re * toDouble(), b.im * toDouble()) *
public override operator fun Complex.unaryMinus(): Complex = Complex(-re, -im) * @receiver the addend.
public override fun norm(arg: Complex): Complex = sqrt(arg.conjugate * arg) * @param c the augend.
public override fun symbol(value: String): Complex = if (value == "i") i else super.symbol(value) * @return the sum.
*/
public operator fun Double.plus(c: Complex): Complex = add(this.toComplex(), c)
/**
* Subtracts complex number from real one.
*
* @receiver the minuend.
* @param c the subtrahend.
* @return the difference.
*/
public operator fun Double.minus(c: Complex): Complex = add(this.toComplex(), -c)
/**
* Adds real number to complex one.
*
* @receiver the addend.
* @param d the augend.
* @return the sum.
*/
public operator fun Complex.plus(d: Double): Complex = d + this
/**
* Subtracts real number from complex one.
*
* @receiver the minuend.
* @param d the subtrahend.
* @return the difference.
*/
public operator fun Complex.minus(d: Double): Complex = add(this, -d.toComplex())
/**
* Multiplies real number by complex one.
*
* @receiver the multiplier.
* @param c the multiplicand.
* @receiver the product.
*/
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 symbol(value: String): Complex = if (value == "i") i else super<ExtendedField>.symbol(value)
} }
/** /**
@ -123,33 +166,23 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex> {
* @property re The real part. * @property re The real part.
* @property im The imaginary part. * @property im The imaginary part.
*/ */
public data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Complex, ComplexField>, public data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Complex, ComplexField> {
Comparable<Complex> {
public constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble()) public constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble())
public constructor(re: Number) : this(re.toDouble(), 0.0) public constructor(re: Number) : this(re.toDouble(), 0.0)
public override val context: ComplexField public override val context: ComplexField get() = ComplexField
get() = ComplexField
init {
require(!re.isNaN()) { "Real component of complex is not-a-number" }
require(!im.isNaN()) { "Imaginary component of complex is not-a-number" }
}
public override fun unwrap(): Complex = this public override fun unwrap(): Complex = this
public override fun Complex.wrap(): Complex = this public override fun Complex.wrap(): Complex = this
public override fun compareTo(other: Complex): Int = r.compareTo(other.r) public override fun toString(): String = "($re + i*$im)"
public override fun toString(): String = "($re + $im * i)"
public override fun minus(b: Complex): Complex = Complex(re - b.re, im - b.im)
public companion object : MemorySpec<Complex> { public companion object : MemorySpec<Complex> {
public override val objectSize: Int override val objectSize: Int
get() = 16 get() = 16
public override fun MemoryReader.read(offset: Int): Complex = override fun MemoryReader.read(offset: Int): Complex = Complex(readDouble(offset), readDouble(offset + 8))
Complex(readDouble(offset), readDouble(offset + 8))
public override fun MemoryWriter.write(offset: Int, value: Complex) { override fun MemoryWriter.write(offset: Int, value: Complex) {
writeDouble(offset, value.re) writeDouble(offset, value.re)
writeDouble(offset + 8, value.im) writeDouble(offset + 8, value.im)
} }

View File

@ -1,7 +1,8 @@
package kscience.kmath.structures package kscience.kmath.complex
import kscience.kmath.misc.UnstableKMathAPI import kscience.kmath.misc.UnstableKMathAPI
import kscience.kmath.operations.* import kscience.kmath.operations.*
import kscience.kmath.structures.*
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
@ -16,12 +17,12 @@ public class ComplexNDField(override val shape: IntArray) :
ExtendedNDField<Complex, ComplexField, NDBuffer<Complex>>, ExtendedNDField<Complex, ComplexField, NDBuffer<Complex>>,
RingWithNumbers<NDBuffer<Complex>>{ RingWithNumbers<NDBuffer<Complex>>{
override val strides: Strides = DefaultStrides(shape) public override val strides: Strides = DefaultStrides(shape)
override val elementContext: ComplexField get() = ComplexField public override val elementContext: ComplexField get() = ComplexField
override val zero: ComplexNDElement by lazy { produce { zero } } public override val zero: ComplexNDElement by lazy { produce { zero } }
override val one: ComplexNDElement by lazy { produce { one } } public override val one: ComplexNDElement by lazy { produce { one } }
override fun number(value: Number): NDBuffer<Complex> { public override fun number(value: Number): NDBuffer<Complex> {
val c = value.toComplex() val c = value.toComplex()
return produce { c } return produce { c }
} }

View File

@ -14,14 +14,14 @@ import kotlin.math.*
* This quaternion's conjugate. * This quaternion's conjugate.
*/ */
public val Quaternion.conjugate: Quaternion public val Quaternion.conjugate: Quaternion
get() = QuaternionField { z - x * QuaternionField.i - y * QuaternionField.j - z * QuaternionField.k } get() = QuaternionField { z - x * i - y * j - z * k }
/** /**
* This quaternion's reciprocal. * This quaternion's reciprocal.
*/ */
public val Quaternion.reciprocal: Quaternion public val Quaternion.reciprocal: Quaternion
get() { get() {
val n = QuaternionField { QuaternionField.norm(this@reciprocal) } val n = QuaternionField { norm(this@reciprocal) }
return conjugate / (n * n) return conjugate / (n * n)
} }
@ -35,7 +35,7 @@ public val Quaternion.r: Double
* A field of [Quaternion]. * A field of [Quaternion].
*/ */
public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>, PowerOperations<Quaternion>, public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>, PowerOperations<Quaternion>,
ExponentialOperations<Quaternion> { ExponentialOperations<Quaternion>, RingWithNumbers<Quaternion> {
override val zero: Quaternion by lazy { 0.toQuaternion() } override val zero: Quaternion by lazy { 0.toQuaternion() }
override val one: Quaternion by lazy { 1.toQuaternion() } override val one: Quaternion by lazy { 1.toQuaternion() }
@ -182,7 +182,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
* @property z The fourth component. * @property z The fourth component.
*/ */
public data class Quaternion(val w: Double, val x: Double, val y: Double, val z: Double) : public data class Quaternion(val w: Double, val x: Double, val y: Double, val z: Double) :
FieldElement<Quaternion, Quaternion, QuaternionField>, Comparable<Quaternion> { FieldElement<Quaternion, Quaternion, QuaternionField> {
public constructor(w: Number, x: Number, y: Number, z: Number) : this( public constructor(w: Number, x: Number, y: Number, z: Number) : this(
w.toDouble(), w.toDouble(),
x.toDouble(), x.toDouble(),
@ -213,7 +213,6 @@ public data class Quaternion(val w: Double, val x: Double, val y: Double, val z:
public override fun unwrap(): Quaternion = this public override fun unwrap(): Quaternion = this
public override fun Quaternion.wrap(): Quaternion = this public override fun Quaternion.wrap(): Quaternion = this
public override fun compareTo(other: Quaternion): Int = r.compareTo(other.r)
/** /**
* Returns a string representation of this quaternion. * Returns a string representation of this quaternion.