Sfoglia il codice sorgente

Added visualization of the EEA

Martin Thoma 10 anni fa
parent
commit
42bc49a23d

+ 35 - 0
tikz/extended-euclidean-algorithm-runtime/Makefile

@@ -0,0 +1,35 @@
+SOURCE = extended-euclidean-algorithm-runtime
+DELAY = 80
+DENSITY = 300
+WIDTH = 512
+
+make:
+	pdflatex $(SOURCE).tex -output-format=pdf
+	make clean
+
+clean:
+	rm -rf  $(TARGET) *.class *.html *.log *.aux *.data *.gnuplot
+
+gif:
+	pdfcrop $(SOURCE).pdf
+	convert -verbose -delay $(DELAY) -loop 0 -density $(DENSITY) $(SOURCE)-crop.pdf $(SOURCE).gif
+	make clean
+
+png:
+	make
+	make svg
+	inkscape $(SOURCE).svg -w $(WIDTH) --export-png=$(SOURCE).png
+
+transparentGif:
+	convert $(SOURCE).pdf -transparent white result.gif
+	make clean
+
+svg:
+	make
+	#inkscape $(SOURCE).pdf --export-plain-svg=$(SOURCE).svg
+	pdf2svg $(SOURCE).pdf $(SOURCE).svg
+	# Necessary, as pdf2svg does not always create valid svgs:
+	inkscape $(SOURCE).svg --export-plain-svg=$(SOURCE).svg
+	rsvg-convert -a -w $(WIDTH) -f svg $(SOURCE).svg -o $(SOURCE)2.svg
+	inkscape $(SOURCE)2.svg --export-plain-svg=$(SOURCE).svg
+	rm $(SOURCE)2.svg

+ 8 - 0
tikz/extended-euclidean-algorithm-runtime/README.md

@@ -0,0 +1,8 @@
+Compiled example
+----------------
+![Example](csv-2d-gaussian-multivarate-distributions.png)
+
+
+TeX main memory
+---------------
+You might need to give tex more main memory: http://tex.stackexchange.com/questions/75399/increasing-texs-main-memory

File diff suppressed because it is too large
+ 12101 - 0
tikz/extended-euclidean-algorithm-runtime/data.csv


+ 42 - 0
tikz/extended-euclidean-algorithm-runtime/eea.py

@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+import csv
+
+
+def eea(a, b):
+    """
+    Calculates u*a + v*b = ggT
+    returns (ggT, u, v, steps)
+    """
+    u, v, s, t, steps = 1, 0, 0, 1, 0
+    while b > 0:
+        q = a//b
+        a, b = b, a-q*b
+        u, s = s, u-q*s
+        v, t = t, v-q*t
+        steps += 1
+    return a, u, v, steps
+
+
+def create_csv(n, filename):
+    max_steps = 0
+    with open(filename, 'wb') as csvfile:
+        csvwriter = csv.writer(csvfile, delimiter=',',
+                               quotechar='"', quoting=csv.QUOTE_MINIMAL)
+        csvwriter.writerow(["x", "y", "steps"])
+        for x in range(n):
+            for y in range(n):
+                steps = eea(x, y)[-1]
+                max_steps = max(steps, max_steps)
+                csvwriter.writerow([x, y, steps])
+    print("Maximum step number: %i" % max_steps)
+
+if __name__ == "__main__":
+    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
+    parser = ArgumentParser(description=__doc__,
+                            formatter_class=ArgumentDefaultsHelpFormatter)
+    parser.add_argument("-n",
+                        dest="n", default=10, type=int,
+                        help="how many lines get printed")
+    args = parser.parse_args()
+    create_csv(args.n, "data.csv")

+ 77 - 0
tikz/extended-euclidean-algorithm-runtime/extended-euclidean-algorithm-runtime.tex

@@ -0,0 +1,77 @@
+\documentclass[varwidth=true, border=5pt]{article}
+\usepackage[active,tightpage]{preview}
+\usepackage[latin1]{inputenc}
+\usepackage{amsmath}
+\usepackage{pgfplots}
+\pgfplotsset{compat=1.10}
+\usepackage{tikz}
+\usetikzlibrary{arrows, positioning} 
+\usepackage{helvet}
+\usepackage[eulergreek]{sansmath}
+
+\begin{document}
+\begin{preview}
+\pgfplotsset{
+    colormap={whitered}{
+        color(0cm)=(white);
+        color(1cm)=(orange!75!red)
+    }
+}
+\begin{tikzpicture}
+    \begin{axis}[
+        colormap name=whitered,
+        clip mode=individual,
+        width=10.0cm,
+        height=10.0cm,
+        % Grid
+        grid = major,
+        % size
+        %xmin= 40,     % start the diagram at this x-coordinate
+        %xmax= 90,   % end   the diagram at this x-coordinate
+        %ymin= 0,     % start the diagram at this y-coordinate
+        %ymax= 60, % end   the diagram at this y-coordinate
+        % Legende
+        legend style={
+            font=\large\sansmath\sffamily,
+            at={(0.5,-0.18)},
+            anchor=north,
+            legend cell align=left,
+            legend columns=-1,
+            column sep=0.5cm
+        },
+        % Ticks
+        tick align=inside,
+        every axis/.append style={font=\large\sansmath\sffamily},
+        minor tick style={thick},
+        scaled y ticks = false,
+        % Axis
+        axis line style = {very thick,shorten <=-0.5\pgflinewidth},
+        axis lines = middle,
+        axis line style = very thick,
+        xlabel=$m$,
+        x label style={at={(axis description cs:0.5,-0.05)},
+                       anchor=north,
+                       font=\boldmath\sansmath\sffamily\Large},
+        ylabel=$n$,
+        y label style={at={(axis description cs:-0.1,0.5)},
+                       anchor=south,
+                       rotate=90,
+                       font=\boldmath\sansmath\sffamily\Large},
+        colorbar,
+        colorbar style={
+            at={(-0.2,0)},
+            anchor=south west,
+            height=0.25*\pgfkeysvalueof{/pgfplots/parent axis height},
+            title={Schritte}  % ADJUST THIS TO YOUR LANGUAGE
+        }
+        ]
+\addplot[
+scatter,
+only marks,
+mark=square*
+]
+table[col sep=comma,point meta=\thisrow{steps}] {data.csv};
+\end{axis}
+\end{tikzpicture}
+\end{preview}
+\end{document}