From: Peter Schaefer Date: Tue, 5 Jun 2012 17:34:07 +0000 (+0200) Subject: python erweitert X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=1fa2bc854b12cb96f6514a8b3f6c53f0adb42e22;p=zahlenTA.git python erweitert --- diff --git a/UE/lib.py b/UE/lib.py new file mode 100644 index 0000000..a98984a --- /dev/null +++ b/UE/lib.py @@ -0,0 +1,157 @@ +import math + +# Berechnet die Summe aller vielfachen von base bis max +def summandial(m,base=1): + m = int(m/base)*base + return m*(m+base)/(2*base) + +# Gibt die naechst kleine Fib zu max zurueck +def fib(m): + a,b = 1,2 + while b < m: + a,b = b,a+b + return a + +# Gibt die Liste aller Fib zur naechst kleineren Fib von max zurueck +def fibV(m): + a = [1, 2] + i = 1; + while a[i] < m: + a.append(a[i] + a[i-1]) + i = i+1 + return a + +# Gibt die Summe aller d-fachen Fib zu max zurueck (d=2) +def fibS(m,d=2): + a,b = 1,2 + s = 0 + while b < m: + a,b = b,a+b + if a%d == 0: + s = s +a + return s + +# Gibt alle Primzahlen bis n zurueck +def primes(n): + if n < 2: + return 0 + p = range(1,n+1,2) + q = len(p); + p[0] = 2; + for k in range(3,int(n**.5+1),2): + if p[(k-1)/2]: + for i in range(((k*k-1)/2),q,k): + p[i] = 0 + return filter(lambda x:x>0,p) + +# Gibt alle Primfaktoren von n zurueck +def factor(n): + if n<4: + return [n] + p = primes(int(n**.5+1)) + f = [] + while n>1: + p = filter(lambda x:n%x==0,p) + if(p==[]): + f.append(n) + break + f[len(f):] = p + n = n/prod(p) + f.sort() + return f + +# Gibt alle Primfaktoren von n als vektor mit Vielfachheit zurueck +def factorD(n): + if n<4: + return {n:1} + p = primes(int(n**.5+1)) + f = {} + while n>1: + p = filter(lambda x:n%x==0,p) + if(p==[]): + f[n] = 1 + break + for i in range(len(p)): + if(p[i] in f): + f[p[i]]=f[p[i]]+1 + else: + f[p[i]]=1 + n = n/prod(p) +# f.sort() + return f + +# Multipliziert alle Werte aus Liste seq auf +def prod(seq): + return reduce(lambda x,y:x*y, seq, 1) + +# generiert Pyth tripel +def pythtrip(u,v): + return [u*u-v*v, 2*u*v, u*u+v*v] + +#Binomial Koeffizient +def binomial(n,k): + return math.factorial(n)/(math.factorial(k)*math.factorial(n-k)) + +def ebinomial(seq): + vek = [1, 0] + for i in seq: + for j in range(len(vek)-1,0,-1): + vek[j] = vek[j]+vek[j-1]*i + vek[len(vek):]=[0] + + return vek[:len(vek)-1] + +#Anzahl der Teiler +def divisors(num): + test = factorD(num) + return sum(ebinomial(map(lambda x:test[x],test.keys()))[1:])+1 + + +#Euklid +def euklid(a,b): + if a < b: + tmp = a + a = b + b = tmp + x = [1, 0] + y = [0, 1] + q = int(a / b) + while a%b: + [a ,b] = [b,a-q*b] + x = [x[1],x[0]-q*x[1]] + y = [y[1],y[0]-q*y[1]] + q = int(a/b) + return [b,x[1],y[1]] + +def euklid_tex(a,b): + if a < b: + tmp = a + a = b + b = tmp + x = [1, 0] + y = [0, 1] + q = int(a / b) + print "\\begin{array}{ccccccc}" + print " r_{i-2} & r_{i-1} & q_i & x_{i-2} & x_{i-1} & y_{i-2} & y_{i-1}\\\\\\hline" + print " ", a, "&" , b ,"&" , q,"&" ,x[0],"&",x[1] ,"&",y[0] ,"&" ,y[1],"\\\\" + while a%b: + [a ,b] = [b,a-q*b] + x = [x[1],x[0]-q*x[1]] + y = [y[1],y[0]-q*y[1]] + q = int(a/b) + if a%b: + print " ", a, "&" , b ,"&" , q,"&" ,x[0],"&",x[1] ,"&",y[0] ,"&" ,y[1],"\\\\" + else: + print " \\cline{2-2}\\cline{5-5}\\cline{7-7}" + print " \multicolumn{1}{l|}{", a, "}& \multicolumn{1}{l|}{" , b , + print "}&&\multicolumn{1}{l|}{" ,x[0],"}&\multicolumn{1}{l|}{",x[1] , + print "}&\multicolumn{1}{l|}{",y[0] ,"}&\multicolumn{1}{l|}{" ,y[1], "}\\\\" + print " \\cline{2-2}\\cline{5-5}\\cline{7-7}" + print "\\end{array}", + return [b,x[1],y[1]] + +def gcd(a,b): + return euklid(a,b)[0] + +#euklid_tex(2124,1764) + \ No newline at end of file diff --git a/UE/python.sty b/UE/python.sty new file mode 100644 index 0000000..1183d0f --- /dev/null +++ b/UE/python.sty @@ -0,0 +1,105 @@ +%% This program is free software; you can redistribute it and/or +%% modify it under the terms of the GNU General Public License +%% as published by the Free Software Foundation; either version 2 +%% of the License, or (at your option) any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program; if not, write to the Free Software +%% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +%% +%% Author: Martin R. Ehmsen, ehmsen@imada.sdu.dk. +%% Department of Mathematics and Computer Science, +%% University of Southern Denmark, DK +%% +%% You can find an online copy of the GPL at +%% http://www.gnu.org/copyleft/gpl.html . +%% +%% Note: shell-escape needs to be activated for this to work. +%% This can either be done by passing -shell-escape as an option to +%% latex or by adding/changing "shell_escape = t" in your texmf.cnf . + +% 0.2 -> 0.21: Moved \newwrite\@module from \@writemodule and out, since +% no more than 15 \newwrites are allowed (and the previous version created a new +% every time \@writemodule was called. + +\NeedsTeXFormat{LaTeX2e}[1994/12/01] +\ProvidesPackage{python}[2007/06/07 v0.21 Python in LaTeX] + +\newwrite\@out +\newwrite\@module + +\begingroup \catcode `|=0 \catcode `[=1 +\catcode`]=2 \catcode `\{=12 \catcode `\}=12 +\catcode`\\=12 |gdef|@xpython#1\end{python}[|immediate|write|@out[#1]|end[python]] +|endgroup + +\def\python{\kernel@ifnextchar [{\@python}{\@python[]}} + + +\newcounter{py@codenum} +\def\@python[#1]{% +\stepcounter{py@codenum} +\gdef\@pythoninclude{#1} +\immediate\openout\@out=\jobname.py +\newlinechar='15 +\begingroup \catcode`\^^M=12 % +\let\do\@makeother\dospecials\obeyspaces% +\@xpython} + +\def\endpython{% +\endgroup +\immediate\closeout\@out +\@writemodule +\immediate\write18{python\space\jobname.py > \jobname.py.\thepy@codenum.out 2> \jobname.py.\thepy@codenum.err} +\immediate\input\jobname.py.\thepy@codenum.out} + + + +% \def\@python[#1]{% +% \gdef\@pythoninclude{#1} +% \immediate\openout\@out=\jobname.py +% \newlinechar='15 +% \begingroup \catcode`\^^M=12 % +% \let\do\@makeother\dospecials\obeyspaces% +% \@xpython} +% +% \def\endpython{% +% \endgroup +% \immediate\closeout\@out +% \@writemodule +% \immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2> \jobname.py.err} +% \immediate\input\jobname.py.out} +% %\immediate\write{\begin{verbatim}} +% %\immediate\input\jobname.py.err +% %\immediate\write{\end{verbatim}}} + +\def\@writemodule{% +\immediate\openout\@module=latex.py +\immediate\write\@module{jobname="\jobname"} +\immediate\closeout\@module} + +% BUGS: +% 1. If anything gets send to stderr then it should be included +% in \begin{verbatim}...\end{verbatim} to be properly displayed +% +% \immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2>\jobname.py.err} +% +% 2. Watch out for indentation done by aucTeX in Emacs +% +% 3. Let the package accept a "final version" option, such +% that the output of each python run is saved such that it can be +% inserted into the document by hand +% (conference, journals are not likely to compile with +% shell_escape or have python). +% +% \gdef\@prepython{} +% \def\prepython#1{% +% \gdef\@prepython{#1} +% } +% sed -e 's/^ //g' cluster.py +% \immediate\write18{\@prepython\space\jobname.py > \ \ No newline at end of file diff --git a/UE/python/09.py b/UE/python/09.py index 9be1ccd..f36a6de 100644 --- a/UE/python/09.py +++ b/UE/python/09.py @@ -1,4 +1,4 @@ - +import lib def f(x,m=120): return (2*x**3-3*x**2+5*x+6)%m diff --git a/UE/python/lib.py b/UE/python/lib.py deleted file mode 100644 index 894e6e5..0000000 --- a/UE/python/lib.py +++ /dev/null @@ -1,107 +0,0 @@ -import math - -# Berechnet die Summe aller vielfachen von base bis max -def summandial(m,base=1): - m = int(m/base)*base - return m*(m+base)/(2*base) - -# Gibt die naechst kleine Fib zu max zurueck -def fib(m): - a,b = 1,2 - while b < m: - a,b = b,a+b - return a - -# Gibt die Liste aller Fib zur naechst kleineren Fib von max zurueck -def fibV(m): - a = [1, 2] - i = 1; - while a[i] < m: - a.append(a[i] + a[i-1]) - i = i+1 - return a - -# Gibt die Summe aller d-fachen Fib zu max zurueck (d=2) -def fibS(m,d=2): - a,b = 1,2 - s = 0 - while b < m: - a,b = b,a+b - if a%d == 0: - s = s +a - return s - -# Gibt alle Primzahlen bis n zurueck -def primes(n): - if n < 2: - return 0 - p = range(1,n+1,2) - q = len(p); - p[0] = 2; - for k in range(3,int(n**.5+1),2): - if p[(k-1)/2]: - for i in range(((k*k-1)/2),q,k): - p[i] = 0 - return filter(lambda x:x>0,p) - -# Gibt alle Primfaktoren von n zurueck -def factor(n): - if n<4: - return [n] - p = primes(int(n**.5+1)) - f = [] - while n>1: - p = filter(lambda x:n%x==0,p) - if(p==[]): - f.append(n) - break - f[len(f):] = p - n = n/prod(p) - f.sort() - return f - -# Gibt alle Primfaktoren von n als vektor mit Vielfachheit zurueck -def factorD(n): - if n<4: - return {n:1} - p = primes(int(n**.5+1)) - f = {} - while n>1: - p = filter(lambda x:n%x==0,p) - if(p==[]): - f[n] = 1 - break - for i in range(len(p)): - if(p[i] in f): - f[p[i]]=f[p[i]]+1 - else: - f[p[i]]=1 - n = n/prod(p) -# f.sort() - return f - -# Multipliziert alle Werte aus Liste seq auf -def prod(seq): - return reduce(lambda x,y:x*y, seq, 1) - -# generiert Pyth tripel -def pythtrip(u,v): - return [u*u-v*v, 2*u*v, u*u+v*v] - -#Binomial Koeffizient -def binomial(n,k): - return math.factorial(n)/(math.factorial(k)*math.factorial(n-k)) - -def ebinomial(seq): - vek = [1, 0] - for i in seq: - for j in range(len(vek)-1,0,-1): - vek[j] = vek[j]+vek[j-1]*i - vek[len(vek):]=[0] - - return vek[:len(vek)-1] - -#Anzahl der Teiler -def divisors(num): - test = factorD(num) - return sum(ebinomial(map(lambda x:test[x],test.keys()))[1:])+1 \ No newline at end of file diff --git a/UE/python/python.sty b/UE/python/python.sty deleted file mode 100644 index 1183d0f..0000000 --- a/UE/python/python.sty +++ /dev/null @@ -1,105 +0,0 @@ -%% This program is free software; you can redistribute it and/or -%% modify it under the terms of the GNU General Public License -%% as published by the Free Software Foundation; either version 2 -%% of the License, or (at your option) any later version. -%% -%% This program is distributed in the hope that it will be useful, -%% but WITHOUT ANY WARRANTY; without even the implied warranty of -%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -%% GNU General Public License for more details. -%% -%% You should have received a copy of the GNU General Public License -%% along with this program; if not, write to the Free Software -%% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -%% -%% Author: Martin R. Ehmsen, ehmsen@imada.sdu.dk. -%% Department of Mathematics and Computer Science, -%% University of Southern Denmark, DK -%% -%% You can find an online copy of the GPL at -%% http://www.gnu.org/copyleft/gpl.html . -%% -%% Note: shell-escape needs to be activated for this to work. -%% This can either be done by passing -shell-escape as an option to -%% latex or by adding/changing "shell_escape = t" in your texmf.cnf . - -% 0.2 -> 0.21: Moved \newwrite\@module from \@writemodule and out, since -% no more than 15 \newwrites are allowed (and the previous version created a new -% every time \@writemodule was called. - -\NeedsTeXFormat{LaTeX2e}[1994/12/01] -\ProvidesPackage{python}[2007/06/07 v0.21 Python in LaTeX] - -\newwrite\@out -\newwrite\@module - -\begingroup \catcode `|=0 \catcode `[=1 -\catcode`]=2 \catcode `\{=12 \catcode `\}=12 -\catcode`\\=12 |gdef|@xpython#1\end{python}[|immediate|write|@out[#1]|end[python]] -|endgroup - -\def\python{\kernel@ifnextchar [{\@python}{\@python[]}} - - -\newcounter{py@codenum} -\def\@python[#1]{% -\stepcounter{py@codenum} -\gdef\@pythoninclude{#1} -\immediate\openout\@out=\jobname.py -\newlinechar='15 -\begingroup \catcode`\^^M=12 % -\let\do\@makeother\dospecials\obeyspaces% -\@xpython} - -\def\endpython{% -\endgroup -\immediate\closeout\@out -\@writemodule -\immediate\write18{python\space\jobname.py > \jobname.py.\thepy@codenum.out 2> \jobname.py.\thepy@codenum.err} -\immediate\input\jobname.py.\thepy@codenum.out} - - - -% \def\@python[#1]{% -% \gdef\@pythoninclude{#1} -% \immediate\openout\@out=\jobname.py -% \newlinechar='15 -% \begingroup \catcode`\^^M=12 % -% \let\do\@makeother\dospecials\obeyspaces% -% \@xpython} -% -% \def\endpython{% -% \endgroup -% \immediate\closeout\@out -% \@writemodule -% \immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2> \jobname.py.err} -% \immediate\input\jobname.py.out} -% %\immediate\write{\begin{verbatim}} -% %\immediate\input\jobname.py.err -% %\immediate\write{\end{verbatim}}} - -\def\@writemodule{% -\immediate\openout\@module=latex.py -\immediate\write\@module{jobname="\jobname"} -\immediate\closeout\@module} - -% BUGS: -% 1. If anything gets send to stderr then it should be included -% in \begin{verbatim}...\end{verbatim} to be properly displayed -% -% \immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2>\jobname.py.err} -% -% 2. Watch out for indentation done by aucTeX in Emacs -% -% 3. Let the package accept a "final version" option, such -% that the output of each python run is saved such that it can be -% inserted into the document by hand -% (conference, journals are not likely to compile with -% shell_escape or have python). -% -% \gdef\@prepython{} -% \def\prepython#1{% -% \gdef\@prepython{#1} -% } -% sed -e 's/^ //g' cluster.py -% \immediate\write18{\@prepython\space\jobname.py > \ \ No newline at end of file diff --git a/UE/python/test.pdf b/UE/python/test.pdf index f85be3c..c896ca2 100644 Binary files a/UE/python/test.pdf and b/UE/python/test.pdf differ diff --git a/UE/python/test.tex b/UE/python/test.tex index e5af741..d909ab9 100644 --- a/UE/python/test.tex +++ b/UE/python/test.tex @@ -1,16 +1,17 @@ %& -shell-escape \documentclass[a4paper,10pt,fleqn]{article} -\usepackage{template} +\usepackage{../template} \usepackage{python} \begin{document} - \begin{python} - print "hello" - \end{python} - - \begin{python} - print "hello2" - \end{python} + + + +\begin{python} +import lib +euklid_tex( +\end{python} + \end{document} diff --git a/UE/template.sty b/UE/template.sty index 8b37bc3..71f59a6 100644 --- a/UE/template.sty +++ b/UE/template.sty @@ -12,6 +12,8 @@ \usepackage{fancyhdr} % \usepackage{emaxima} +\usepackage{python} + \def\P{\mathbb{P}} \def\N{\mathbb{N}} \def\R{\mathbb{R}} diff --git a/UE/ue1.pdf b/UE/ue1.pdf index 865d779..6595a5e 100644 Binary files a/UE/ue1.pdf and b/UE/ue1.pdf differ diff --git a/UE/ue1.tex b/UE/ue1.tex index e10ac67..0adbfbf 100644 --- a/UE/ue1.tex +++ b/UE/ue1.tex @@ -20,15 +20,14 @@ S_n &= \frac{\frac a 1 + \frac a 2+ \cdots \frac a n} a\\ &= \end{align} \aufgabe{3}{} +\begin{center}$ +\begin{python} +import lib +lib.euklid_tex(312,269) +\end{python}$ +\end{center} + \begin{align} -&\begin{array}{ccccccc} - r_{i-2} & r_{i-1}& q_i & x_{i-2} & x_{i-1} & y_{i-2} & y_{i-1}\\ - 312 & 269 & 1 & 1 & 0 & 0 & 1\\ - 269 & 43 & 6 & 0 & 1 & 1 & -1\\ - 43 & 11 & 3 & 1 & -6 & -1 & 7\\ - 11 & 10 & 1 & -6 & 19 & 7 & - 22\\ - 10 & 1 & & 19 & -25 & -22 & 29\\ -\end{array}\\ \ggT(312,269) & = 1\\ 1 & = -25*312+29*269\\ 5 & = -125*312+145*269\\