Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlin.math.pow
- import kotlin.math.sqrt
- data class Complex(val re: Double, val im: Double) {
- companion object {
- val zero = Complex(0.0, 0.0)
- }
- fun reciprocal(): Complex {
- val scale = re.pow(2) + re.pow(2)
- return Complex(re / scale, -im / scale)
- }
- fun inCardioid(): Boolean {
- val pq = (re - 0.25).pow(2) + im.pow(2)
- return re < sqrt(pq) - (2*pq) + 0.25
- }
- fun dsq(): Double = re*re + im*im
- fun abs(): Double = sqrt(re.pow(2) + im.pow(2))
- operator fun unaryMinus(): Complex = Complex(-re, -im)
- operator fun plus(other: Double): Complex = Complex(re + other, im)
- operator fun minus(other: Double): Complex = Complex(re - other, im)
- operator fun times(other: Double): Complex = Complex(re * other, im * other)
- operator fun div(other: Double): Complex = Complex(re / other, im / other)
- operator fun plus(other: Complex): Complex =
- Complex(re + other.re, im + other.im)
- operator fun minus(other: Complex): Complex =
- Complex(re - other.re, im - other.im)
- operator fun times(other: Complex): Complex =
- Complex(
- (re * other.re) - (im * other.im),
- (re * other.im) + (im * other.re))
- operator fun div(other: Complex): Complex = this * other.reciprocal()
- //@Override
- //fun equals(other: Complex): Boolean = this.im == other.im && this.re == other.re
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement