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