Add toQuaternion extension for Complex

This commit is contained in:
Iaroslav Postovalov 2020-10-27 19:19:53 +07:00
parent 59a50810cc
commit 828e40c452
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7

View File

@ -68,7 +68,7 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w, 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 val s = b.w * b.w + b.x * b.x + b.y * b.y + b.z * b.z
return Quaternion( return Quaternion(
@ -196,8 +196,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>, FieldElement<Quaternion, Quaternion, QuaternionField>, Comparable<Quaternion> {
Comparable<Quaternion> {
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(),
@ -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. * Creates a quaternion with real part equal to this real.
* *
* @receiver the real part. * @receiver the real part.
* @return the new quaternion. * @return a new quaternion.
*/ */
public fun Number.toQuaternion(): Quaternion = Quaternion(this, 0, 0, 0) 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 * Creates a new buffer of quaternions with the specified [size], where each element is calculated by calling the
* specified [init] function. * specified [init] function.