debug stuff cleanup and simplify
This commit is contained in:
parent
cd007050a8
commit
a72991d1b7
@ -27,45 +27,22 @@ class Complex(val real: Real, val imag: Real = 0.0) {
|
|||||||
val re get() = real
|
val re get() = real
|
||||||
val im get() = imag
|
val im get() = imag
|
||||||
|
|
||||||
fun plus(other: Complex): Complex {
|
fun plus(other: Complex): Complex = Complex(real + other.real, imag + other.imag)
|
||||||
val ar = real
|
|
||||||
val ai = imag
|
|
||||||
val br = other.real
|
|
||||||
val bi = other.imag
|
|
||||||
val realPart = ar + br
|
|
||||||
val imagPart = ai + bi
|
|
||||||
Complex(realPart, imagPart)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun minus(other: Complex): Complex {
|
fun minus(other: Complex): Complex = Complex(real - other.real, imag - other.imag)
|
||||||
val ar = real
|
|
||||||
val ai = imag
|
|
||||||
val br = other.real
|
|
||||||
val bi = other.imag
|
|
||||||
val realPart = ar - br
|
|
||||||
val imagPart = ai - bi
|
|
||||||
Complex(realPart, imagPart)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun mul(other: Complex): Complex {
|
fun mul(other: Complex): Complex =
|
||||||
val ar = real
|
Complex(
|
||||||
val ai = imag
|
real * other.real - imag * other.imag,
|
||||||
val br = other.real
|
real * other.imag + imag * other.real
|
||||||
val bi = other.imag
|
)
|
||||||
val realPart = ar * br - ai * bi
|
|
||||||
val imagPart = ar * bi + ai * br
|
|
||||||
Complex(realPart, imagPart)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun div(other: Complex): Complex {
|
fun div(other: Complex): Complex {
|
||||||
val ar = real
|
val denominator = other.real * other.real + other.imag * other.imag
|
||||||
val ai = imag
|
Complex(
|
||||||
val br = other.real
|
(real * other.real + imag * other.imag) / denominator,
|
||||||
val bi = other.imag
|
(imag * other.real - real * other.imag) / denominator
|
||||||
val denominator = br * br + bi * bi
|
)
|
||||||
val realPart = (ar * br + ai * bi) / denominator
|
|
||||||
val imagPart = (ai * br - ar * bi) / denominator
|
|
||||||
Complex(realPart, imagPart)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun negate(): Complex = Complex(-real, -imag)
|
fun negate(): Complex = Complex(-real, -imag)
|
||||||
@ -114,9 +91,8 @@ class Complex(val real: Real, val imag: Real = 0.0) {
|
|||||||
Complex(Math.sqrt(real), 0.0)
|
Complex(Math.sqrt(real), 0.0)
|
||||||
} else {
|
} else {
|
||||||
val radius = magnitude
|
val radius = magnitude
|
||||||
val realPart = Math.sqrt((radius + real) / 2.0)
|
|
||||||
val imagPart = Math.sqrt((radius - real) / 2.0)
|
val imagPart = Math.sqrt((radius - real) / 2.0)
|
||||||
Complex(realPart, if (imag < 0.0) -imagPart else imagPart)
|
Complex(Math.sqrt((radius + real) / 2.0), if (imag < 0.0) -imagPart else imagPart)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString() =
|
override fun toString() =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user