progs/mandelbrot.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 06 Mar 2024 18:20:25 +0000
changeset 484 c4561fc667b7
child 488 5deaf53c2faa
permissions -rw-r--r--
test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
484
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Mandelbrot pictures
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//=====================
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
//   see https://en.wikipedia.org/wiki/Mandelbrot_set
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
// 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// needs to be called with
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
// 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// scala -cp scala-parallel-collections_3-1.0.4.jar
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// !! UPDATE: On my faster Mac-M1 machine the times
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
// !! are ca. 4 secs for the sequential version and
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
// !! around 0.7 secs for the par-version.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
import java.awt.Color
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
import java.awt.Dimension
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
import java.awt.Graphics
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
import java.awt.Graphics2D
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
import java.awt.image.BufferedImage
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
import javax.swing.JFrame
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
import javax.swing.JPanel
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
import javax.swing.WindowConstants
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
import scala.language.implicitConversions    
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
import scala.collection.parallel.CollectionConverters._
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
// complex numbers
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class Complex(val re: Double, val im: Double) { 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  // represents the complex number re + im * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
                                 this.re * that.im + that.re * this.im)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  def *(that: Double) = Complex(this.re * that, this.im * that)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  def abs() = Math.sqrt(this.re * this.re + this.im * this.im)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
// to allow the notation n + m * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
object i extends Complex(0, 1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
// implicit conversion from Doubles to Complex
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
implicit def d2c(d: Double) : Complex = Complex(d, 0)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
// some customn colours for the "sliding effect"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
val colours = List(
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  new Color(66, 30, 15),    new Color(25, 7, 26),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  new Color(9, 1, 47),      new Color(4, 4, 73),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  new Color(0, 7, 100),     new Color(12, 44, 138),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  new Color(24, 82, 177),   new Color(57, 125, 209),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  new Color(134, 181, 229), new Color(211, 236, 248),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
  new Color(241, 233, 191), new Color(248, 201, 95),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  new Color(255, 170, 0),   new Color(204, 128, 0),
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
  new Color(153, 87, 0),    new Color(106, 52, 3))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
// the viewer panel with an image canvas
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
class Viewer(width: Int, height: Int) extends JPanel {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
  val canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  override def paintComponent(g: Graphics) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
    g.asInstanceOf[Graphics2D].drawImage(canvas, null, null)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  override def getPreferredSize() = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    new Dimension(width, height)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  def clearCanvas(color: Color) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    for (x <- 0 to width - 1; y <- 0 to height - 1) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
      canvas.setRGB(x, y, color.getRGB())
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
    repaint()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  }  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
// initialising the viewer panel
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
def openViewer(width: Int, height: Int) : Viewer = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  val frame = new JFrame("XYPlane")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  val viewer = new Viewer(width, height)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
  frame.add(viewer)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  frame.pack()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  frame.setVisible(true)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  frame.setResizable(false)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  viewer
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
// some hardcoded parameters
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
val W = 900   // width
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
val H = 800   // height
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
val black = Color.black
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
val viewer = openViewer(W, H)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// draw a pixel on the canvas
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
def pixel(x: Int, y: Int, color: Color) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  viewer.canvas.setRGB(x, y, color.getRGB())
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
// calculates the number of iterations using lazy lists (streams)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
//   the iteration goes on for a maximum of max steps,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
//   but might leave early when the pred is satisfied
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
def iterations(c: Complex, max: Int) : Int = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  def next(z: Complex) = z * z + c    
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  def pred(z: Complex) = z.abs() < 2    // exit condition
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  LazyList.iterate(0.0 * i, max)(next).takeWhile(pred).size
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
// main function 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
//    start and end are the upper-left and lower-right corners, 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
//    max is the number of maximum iterations
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  viewer.clearCanvas(black)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  // deltas for each grid step 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  val d_x = (end.re - start.re) / W
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  val d_y = (end.im - start.im) / H
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
   
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  for (y <- (0 until H).par) {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
    for (x <- (0 until W).par) {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
    
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
     val c = start + 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
      (x * d_x + y * d_y * i)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
     val iters = iterations(c, max) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
     val colour = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
       if (iters == max) black 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
       else colours(iters % 16)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
     pixel(x, y, colour)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
    viewer.updateUI()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  }   
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
// Examples
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
//==========
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
//for measuring time
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
def time_needed[T](code: => T) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  val start = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  (end - start) / 1.0e9
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
// example 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
val exa1 = -2.0 + -1.5 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
val exa2 =  1.0 +  1.5 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
println(s"${time_needed(mandelbrot(exa1, exa2, 1000))} secs")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
// example 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
val exb1 = -0.37465401 + 0.659227668 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
val exb2 = -0.37332410 + 0.66020767 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
//time_needed(mandelbrot(exb1, exb2, 1000))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
// example 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
val exc1 = 0.435396403 + 0.367981352 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
val exc2 = 0.451687191 + 0.380210061 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
//time_needed(mandelbrot(exc1, exc2, 1000))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
// some more computations with example 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
val delta = (exc2 - exc1) * 0.0333
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
println(s"${time_needed(
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  for (n <- (0 to 25)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
     mandelbrot(exc1 + delta * n, 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
                exc2 - delta * n, 1000))} secs") 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
// Larry Paulson's example
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
val exl1 = -0.74364990 + 0.13188170 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
val exl2 = -0.74291189 + 0.13261971 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
//println(s"${time_needed(mandelbrot(exl1, exl2, 1000))} secs")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
// example by Jorgen Villadsen
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
val exj1 = 0.10284 - 0.63275 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
val exj2 = 0.11084 - 0.64075 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
//time_needed(mandelbrot(exj1, exj2, 1000))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
// another example
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
val exA = 0.3439274 + 0.6516478 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
val exB = 0.3654477 + 0.6301795 * i
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
//time_needed(mandelbrot(exA, exB, 1000))