# HG changeset patch # User Christian Urban # Date 1529974172 -3600 # Node ID 4d300409f2fee085a5360366663ce2272694734c # Parent f211d9cb856e5fcc555e2c561c8caf768eab9a01 updated diff -r f211d9cb856e -r 4d300409f2fe LINKS --- a/LINKS Fri Jun 22 13:34:05 2018 +0100 +++ b/LINKS Tue Jun 26 01:49:32 2018 +0100 @@ -116,4 +116,18 @@ --------------------- Scala resources -https://danielwestheide.com/scala/neophytes.html \ No newline at end of file +https://danielwestheide.com/scala/neophytes.html + + + + + + + +functional programming +---------------------- +Functional Data Structures +https://cs.uwaterloo.ca/~plragde/flaneries/FDS/index.html + +Okasaki +http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf \ No newline at end of file diff -r f211d9cb856e -r 4d300409f2fe handouts/pep-ho.pdf Binary file handouts/pep-ho.pdf has changed diff -r f211d9cb856e -r 4d300409f2fe handouts/pep-ho.tex --- a/handouts/pep-ho.tex Fri Jun 22 13:34:05 2018 +0100 +++ b/handouts/pep-ho.tex Tue Jun 26 01:49:32 2018 +0100 @@ -214,21 +214,23 @@ \begin{figure}[p] \begin{boxedminipage}{\textwidth} + +A Scala programm for generating pretty pictures of the Mandelbrot Set +(\url{https://en.wikipedia.org/wiki/Mandelbrot_set}): \begin{center} \begin{tabular}{c} -\includegraphics[scale=0.15]{../pics/mand1.png}\\ +\includegraphics[scale=0.12]{../pics/mand1.png} \end{tabular} - -Wellknown Mandelbrot program for generating pretty pictures due to -Benoit Mandelbrot. (\url{https://en.wikipedia.org/wiki/Mandelbrot_set}) -\bigskip +\end{center} +\begin{center} +\begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}} + \bf sequential version: & \bf parallel version: (on 4 cores)\smallskip\\ -\begin{tabular}{@{}p{6cm}|p{6cm}@{}} - \includegraphics[scale=0.15]{../pics/mand4.png} & - \includegraphics[scale=0.15]{../pics/mand3.png} \\ -\footnotesize -{\begin{lstlisting} + {\hfill\includegraphics[scale=0.12]{../pics/mand4.png}\hfill} & + {\hfill\includegraphics[scale=0.12]{../pics/mand3.png}\hfill} \\ + +{\footnotesize\begin{lstlisting}[xleftmargin=-1mm] for (y <- (0 until H)) { for (x <- (0 until W)) { @@ -243,11 +245,32 @@ } viewer.updateUI() } -\end{lstlisting}} -& \\ +\end{lstlisting}} +& +{\footnotesize\begin{lstlisting}[xleftmargin=0mm] +for (y <- (0 until H).par) { + for (x <- (0 until W).par) { + + val c = start + + (x * d_x + y * d_y * i) + val iters = iterations(c, max) + val col = + if (iters == max) black + else colours(iters % 16) + + pixel(x, y, col) + } + viewer.updateUI() +} +\end{lstlisting}}\\ + +\centering\includegraphics[scale=0.5]{../pics/cpu2.png} & +\centering\includegraphics[scale=0.5]{../pics/cpu1.png}\\ + + \end{tabular} \end{center} -\caption{Test \label{mand}} +\caption{Test \label{mand}} \end{boxedminipage} \end{figure} diff -r f211d9cb856e -r 4d300409f2fe pics/cpu1.png Binary file pics/cpu1.png has changed diff -r f211d9cb856e -r 4d300409f2fe pics/cpu2.png Binary file pics/cpu2.png has changed diff -r f211d9cb856e -r 4d300409f2fe pics/mand3.png Binary file pics/mand3.png has changed diff -r f211d9cb856e -r 4d300409f2fe pics/mand4.png Binary file pics/mand4.png has changed diff -r f211d9cb856e -r 4d300409f2fe progs/mandelbrot.scala --- a/progs/mandelbrot.scala Fri Jun 22 13:34:05 2018 +0100 +++ b/progs/mandelbrot.scala Tue Jun 26 01:49:32 2018 +0100 @@ -1,3 +1,6 @@ +// Mandelbrot pictures +// see https://en.wikipedia.org/wiki/Mandelbrot_set + import java.awt.Color import java.awt.Dimension import java.awt.Graphics @@ -19,9 +22,10 @@ def abs() = Math.sqrt(this.re * this.re + this.im * this.im) } +// to allow the notation n + m * i object i extends Complex(0, 1) +implicit def double2complex(re: Double) = Complex(re, 0) -implicit def double2complex(re: Double): Complex = Complex(re, 0) // some customn colours for the "sliding effect" val colours = List( @@ -84,6 +88,8 @@ } // main function +// start and end are the upper-left and lower right corners +// max is the number of maximum iterations def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = { viewer.clearCanvas(black) @@ -91,8 +97,8 @@ val d_x = (end.re - start.re) / W val d_y = (end.im - start.im) / H - for (y <- (0 until H).par) { - for (x <- (0 until W).par) { + for (y <- (0 until H)) { + for (x <- (0 until W)) { val c = start + (x * d_x + y * d_y * i) @@ -107,6 +113,7 @@ } } + // Examples //==========