From 828e40c452b76c2ff5150b19e65fd91f810925c0 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Tue, 27 Oct 2020 19:19:53 +0700 Subject: [PATCH] Add toQuaternion extension for Complex --- .../kscience/kmath/operations/Quaternion.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Quaternion.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Quaternion.kt index 96331bd8a..a4574cb60 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Quaternion.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/Quaternion.kt @@ -68,7 +68,7 @@ public object QuaternionField : Field, Norm, a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w, ) - override fun divide(a: Quaternion, b: Quaternion): Quaternion { + public override fun divide(a: Quaternion, b: Quaternion): Quaternion { val s = b.w * b.w + b.x * b.x + b.y * b.y + b.z * b.z return Quaternion( @@ -196,8 +196,7 @@ public object QuaternionField : Field, Norm, * @property z The fourth component. */ public data class Quaternion(val w: Double, val x: Double, val y: Double, val z: Double) : - FieldElement, - Comparable { + FieldElement, Comparable { public constructor(w: Number, x: Number, y: Number, z: Number) : this( w.toDouble(), x.toDouble(), @@ -240,10 +239,19 @@ public data class Quaternion(val w: Double, val x: Double, val y: Double, val z: * Creates a quaternion with real part equal to this real. * * @receiver the real part. - * @return the new quaternion. + * @return a new quaternion. */ public fun Number.toQuaternion(): Quaternion = Quaternion(this, 0, 0, 0) +/** + * Creates a quaternion with `w`-component equal to `re`-component of given complex and `x`-component equal to + * `im`-component of given complex. + * + * @receiver the complex number. + * @return a new quaternion. + */ +public fun Complex.toQuaternion(): Quaternion = Quaternion(re, im, 0, 0) + /** * Creates a new buffer of quaternions with the specified [size], where each element is calculated by calling the * specified [init] function.