progs/mandelbrot.scala
changeset 353 bb6074814a73
parent 329 8a34b2ebc8cc
child 354 4b347b6e134f
equal deleted inserted replaced
352:97bcf8efe4e0 353:bb6074814a73
    88 // calculates the number of iterations using lazy lists (streams)
    88 // calculates the number of iterations using lazy lists (streams)
    89 //   the iteration goes on for a maximum of max steps,
    89 //   the iteration goes on for a maximum of max steps,
    90 //   but might leave early when the pred is satisfied
    90 //   but might leave early when the pred is satisfied
    91 def iterations(c: Complex, max: Int) : Int = {
    91 def iterations(c: Complex, max: Int) : Int = {
    92   def next(z: Complex) = z * z + c    
    92   def next(z: Complex) = z * z + c    
    93   def pred(z: Complex) = z.abs < 2    // exit condition
    93   def pred(z: Complex) = z.abs() < 2    // exit condition
    94   LazyList.iterate(0.0 * i, max)(next).takeWhile(pred).size
    94   LazyList.iterate(0.0 * i, max)(next).takeWhile(pred).size
    95 }
    95 }
    96 
    96 
    97 // main function 
    97 // main function 
    98 //    start and end are the upper-left and lower-right corners, 
    98 //    start and end are the upper-left and lower-right corners, 
   131   val end = System.nanoTime()
   131   val end = System.nanoTime()
   132   (end - start) / 1.0e9
   132   (end - start) / 1.0e9
   133 }
   133 }
   134 
   134 
   135 
   135 
       
   136 
   136 // example 1
   137 // example 1
   137 val exa1 = -2.0 + -1.5 * i
   138 val exa1 = -2.0 + -1.5 * i
   138 val exa2 =  1.0 +  1.5 * i
   139 val exa2 =  1.0 +  1.5 * i
   139 
   140 
   140 println(s"${time_needed(mandelbrot(exa1, exa2, 1000))} secs")
   141 println(s"${time_needed(mandelbrot(exa1, exa2, 1000))} secs")
   155 
   156 
   156 // some more computations with example 3
   157 // some more computations with example 3
   157 
   158 
   158 val delta = (exc2 - exc1) * 0.0333
   159 val delta = (exc2 - exc1) * 0.0333
   159 
   160 
   160 /*
       
   161 println(s"${time_needed(
   161 println(s"${time_needed(
   162   for (n <- (0 to 12)) 
   162   for (n <- (0 to 12)) 
   163      mandelbrot(exc1 + delta * n, 
   163      mandelbrot(exc1 + delta * n, 
   164                 exc2 - delta * n, 100))} secs") 
   164                 exc2 - delta * n, 100))} secs") 
   165 */
       
   166 
   165 
   167 
   166 
   168 
   167 
   169 // Larry Paulson's example
   168 // Larry Paulson's example
   170 val exl1 = -0.74364990 + 0.13188170 * i
   169 val exl1 = -0.74364990 + 0.13188170 * i