Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import kotlin.math.pow
  2. import kotlin.math.sqrt
  3.  
  4.  
  5. data class Complex(val re: Double, val im: Double) {
  6.  
  7. companion object {
  8. val zero = Complex(0.0, 0.0)
  9. }
  10.  
  11. fun reciprocal(): Complex {
  12. val scale = re.pow(2) + re.pow(2)
  13. return Complex(re / scale, -im / scale)
  14. }
  15.  
  16. fun inCardioid(): Boolean {
  17. val pq = (re - 0.25).pow(2) + im.pow(2)
  18. return re < sqrt(pq) - (2*pq) + 0.25
  19. }
  20.  
  21. fun dsq(): Double = re*re + im*im
  22.  
  23. fun abs(): Double = sqrt(re.pow(2) + im.pow(2))
  24.  
  25. operator fun unaryMinus(): Complex = Complex(-re, -im)
  26. operator fun plus(other: Double): Complex = Complex(re + other, im)
  27. operator fun minus(other: Double): Complex = Complex(re - other, im)
  28. operator fun times(other: Double): Complex = Complex(re * other, im * other)
  29. operator fun div(other: Double): Complex = Complex(re / other, im / other)
  30.  
  31. operator fun plus(other: Complex): Complex =
  32. Complex(re + other.re, im + other.im)
  33.  
  34. operator fun minus(other: Complex): Complex =
  35. Complex(re - other.re, im - other.im)
  36.  
  37. operator fun times(other: Complex): Complex =
  38. Complex(
  39. (re * other.re) - (im * other.im),
  40. (re * other.im) + (im * other.re))
  41.  
  42. operator fun div(other: Complex): Complex = this * other.reciprocal()
  43.  
  44. //@Override
  45. //fun equals(other: Complex): Boolean = this.im == other.im && this.re == other.re
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement