44
55package org.jetbrains.kotlinx.multik.ndarray.complex
66
7- import kotlin.jvm.JvmInline
87import kotlin.math.atan2
98import kotlin.math.sqrt
109
10+ /* *
11+ * Creates a [ComplexDouble] with the given real and imaginary values in floating-point format.
12+ *
13+ * @param re the real value of the complex number in double format.
14+ * @param im the imaginary value of the complex number in double format.
15+ */
16+ public expect fun ComplexDouble (re : Double , im : Double ): ComplexDouble
17+
18+ /* *
19+ * Creates a [ComplexDouble] with the given real and imaginary values in number format.
20+ *
21+ * @param re the real value of the complex number in number format.
22+ * @param im the imaginary value of the complex number in number format.
23+ */
24+ public expect fun ComplexDouble (re : Number , im : Number ): ComplexDouble
25+
26+ /* *
27+ * Creates a [ComplexDouble] with a zero imaginary value.
28+ * @param re the real value of the complex number in number format.
29+ */
30+ public fun ComplexDouble (re : Number ): ComplexDouble = ComplexDouble (re.toDouble(), 0.0 )
31+
1132/* *
1233 * Represents a complex number with double precision.
1334 * The class is implemented as a double-precision 128-bit complex number.
@@ -40,7 +61,7 @@ import kotlin.math.sqrt
4061 * @property im the imaginary part of the complex number.
4162 * @throws ArithmeticException if division by zero or infinity occurs during division.
4263 */
43- public sealed interface ComplexDouble : Complex {
64+ public interface ComplexDouble : Complex {
4465
4566 /* *
4667 * The real part of the complex number.
@@ -53,64 +74,23 @@ public sealed interface ComplexDouble : Complex {
5374 public val im: Double
5475
5576 public companion object {
56- /* *
57- * Determines whether the given double value can be accurately represented as a float value or not.
58- *
59- * @param d is the double value to be checked.
60- * @return this method returns a Boolean value,
61- * true if the double value can be accurately represented as a float value, false otherwise.
62- */
63- private fun fitsInFloat (d : Double ): Boolean = d.toFloat().toDouble() == d
64-
65- /* *
66- * Creates a [ComplexDouble] with the given real and imaginary values in floating-point format.
67- *
68- * @param re the real value of the complex number in double format.
69- * @param im the imaginary value of the complex number in double format.
70- */
71- public operator fun invoke (re : Double , im : Double ): ComplexDouble {
72- return if (fitsInFloat(re) && fitsInFloat(im))
73- ComplexDouble32 (re, im)
74- else
75- ComplexDouble64 (re, im)
76- }
77-
78- /* *
79- * Creates a [ComplexDouble] with the given real and imaginary values in number format.
80- *
81- * @param re the real value of the complex number in number format.
82- * @param im the imaginary value of the complex number in number format.
83- */
84- public operator fun invoke (re : Number , im : Number ): ComplexDouble = invoke(re.toDouble(), im.toDouble())
85-
86- /* *
87- * Creates a [ComplexDouble] with a zero imaginary value.
88- * @param re the real value of the complex number in number format.
89- */
90- public operator fun invoke (re : Number ): ComplexDouble {
91- return if (fitsInFloat(re.toDouble()))
92- ComplexDouble32 (re.toDouble(), 0.0 )
93- else
94- ComplexDouble64 (re.toDouble(), 0.0 )
95- }
96-
9777 /* *
9878 * Represents a [ComplexDouble] number with 1.0 real part and 0f imaginary part.
9979 */
10080 public val one: ComplexDouble
101- get() = ComplexDouble32 (1.0 , 0.0 )
81+ get() = ComplexDouble (1.0 , 0.0 )
10282
10383 /* *
10484 * Represents a [ComplexDouble] number with real and imaginary parts set to 0.0.
10585 */
10686 public val zero: ComplexDouble
107- get() = ComplexDouble32 (0.0 , 0.0 )
87+ get() = ComplexDouble (0.0 , 0.0 )
10888
10989 /* *
11090 * Represents a not-a-number (NaN) value in complex floating point arithmetic.
11191 */
11292 public val NaN : ComplexDouble
113- get() = ComplexDouble64 (Double .NaN , Double .NaN )
93+ get() = ComplexDouble (Double .NaN , Double .NaN )
11494 }
11595
11696 /* *
@@ -452,64 +432,9 @@ public sealed interface ComplexDouble : Complex {
452432 * @return the imaginary part of the complex number as a Double value.
453433 */
454434 public operator fun component2 (): Double = im
455- }
456-
457- /* *
458- * ComplexDouble64 represents a double-precision 128-bit complex number,
459- * which implements the interface `ComplexDouble`.
460- *
461- * @property re the real part of the complex number.
462- * @property im the imaginary part of the complex number.
463- * @constructor creates an instance of ComplexDouble64.
464- */
465- public class ComplexDouble64 internal constructor(
466- public override val re : Double , public override val im : Double
467- ) : ComplexDouble {
468-
469- override fun equals (other : Any? ): Boolean = when {
470- this == = other -> true
471- other is ComplexDouble -> re == other.re && im == other.im
472- else -> false
473- }
474-
475- override fun hashCode (): Int = 31 * re.toBits().hashCode() + im.toBits().hashCode()
476-
477- override fun toString (): String = " $re +($im )i"
478- }
479-
480- /* *
481- * Represents a complex number of single-precision real and imaginary parts.
482- *
483- * @param number The number representing the complex number as a Long value
484- * @property re the real part of the complex number.
485- * @property im the imaginary part of the complex number.
486- */
487- @JvmInline
488- public value class ComplexDouble32 internal constructor(private val number : Long ) : ComplexDouble {
489- override val re: Double
490- get() = Float .fromBits((number shr 32 ).toInt()).toDouble()
491-
492- override val im: Double
493- get() = Float .fromBits(number.toInt()).toDouble()
494-
495- internal constructor (re: Double , im: Double ) : this (Complex .convertComplexFloatToLong(re.toFloat(), im.toFloat()))
496-
497- // TODO
498- // "https://youtrack.jetbrains.com/issue/KT-24874/Support-custom-equals-and-hashCode-for-value-classes"
499- internal fun eq (other : ComplexDouble32 ): Boolean = when {
500- number == other.number -> true
501- else -> re == other.re && im == other.im
502- }
503-
504- internal fun hash (): Int = 31 * number.hashCode()
505435
506- // override fun equals(other: Any?): Boolean = when {
507- // this === other -> true
508- // other is ComplexDouble -> re == other.re && im == other.im
509- // else -> false
510- // }
511- //
512- // override fun hashCode(): Int = 31 * re.toBits().hashCode() + im.toBits().hashCode()
436+ // TODO("https://youtrack.jetbrains.com/issue/KT-24874/Support-custom-equals-and-hashCode-for-value-classes")
437+ public fun eq (other : Any ): Boolean
513438
514- override fun toString (): String = " $re +( $im )i "
439+ public fun hash (): Int
515440}
0 commit comments