a gentle introduction to pythont ex
play

A Gentle Introduction to PythonT EX A Question of Primes - PowerPoint PPT Presentation

page.1 A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Gentle Introduction to PythonT EX A Question of Primes Introduction to PythonT EX Mathematics with Sympy Andrew Mertz William Slough Plots with


  1. page.1 A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Gentle Introduction to PythonT EX A Question of Primes Introduction to PythonT EX Mathematics with Sympy Andrew Mertz William Slough Plots with matplotlib Web Services Mathematics and Computer Science Department Conclusions Eastern Illinois University Extra Examples October 23, 2013

  2. page.2 A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Question of Primes Introduction to PythonT EX Mathematics with Sympy Plots with matplotlib Web Services Conclusions Extra Examples T EX

  3. page.3 Python Overview A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview ◮ General purpose, high-level programming language A Question of Primes Introduction to PythonT EX ◮ Multi-paradigm: object-oriented, imperative, functional Mathematics with Sympy ◮ Comprehensive standard library Plots with matplotlib Web Services ◮ Origins from late 1989 Conclusions Extra Examples ◮ Free and open-source

  4. page.4 Python + Scientific Computing A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Question of Primes Introduction to PythonT EX Mathematics with Sympy Plots with matplotlib Web Services Conclusions Extra Examples

  5. page.5 A Recent Question on T EX Stack Exchange A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Question of Primes “I would like to write a L A T EX script that produces all the prime Introduction to numbers between the numbers n and m , where n < m . How can I do PythonT EX Mathematics with this? I feel it should not be that hard, but I cannot seem to program Sympy it.” Plots with matplotlib Web Services — Kevin † Conclusions Extra Examples † tex.stackexchange.com/questions/134305/how-to-produce-a-list-of-prime- numbers-in-latex/134366#134366

  6. page.6 From The T EXBook, Page 218 (1984) A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview The first 30 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, A Question of Primes 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, Introduction to PythonT EX 109, and 113. You may not find this fact very startling; but you may Mathematics with be surprised to learn that the previous sentence was typeset by saying Sympy Plots with matplotlib The first thirty prime numbers are \primes{30}. Web Services T EX did all the calculation by expanding the primes macro, so the Conclusions author is pretty sure that the list of prime numbers given above is Extra Examples quite free of typographic errors.

  7. page.7 Knuth’s Code, � � -worthy A Gentle Introduction to PythonT EX Andrew Mertz, William Slough \newif\ifprime \newif\ifunknown % boolean variables \newcount\n \newcount\p \newcount\d \newcount\a % integer variables Overview \def\primes#1{2,~3% assume that #1 is at least 3 A Question of Primes \n=#1 \advance\n by-2 % n more to go Introduction to \p=5 % odd primes starting with p PythonT EX \loop\ifnum\n>0 \printifprime\advance\p by2 \repeat} Mathematics with \def\printp{, % we will invoke \printp if p is prime Sympy \ifnum\n=1 and~\fi % and precedes the last value Plots with matplotlib \number\p \advance\n by -1 } Web Services \def\printifprime{\testprimality \ifprime\printp\fi} Conclusions \def\testprimality{{\d=3 \global\primetrue Extra Examples \loop\trialdivision \ifunknown\advance\d by2 \repeat}} \def\trialdivision{\a=\p \divide\a by\d \ifnum\a>\d \unknowntrue\else\unknownfalse\fi \multiply\a by\d \ifnum\a=\p \global\primefalse\unknownfalse\fi}

  8. page.8 David Carlisle’s Response A Gentle Introduction to PythonT EX Andrew Mertz, William Slough \makeatletter Overview \def\primes#1#2{{% A Question of Primes \def\comma{\def\comma{, }}% Introduction to \count@\@ne\@tempcntb#2\relax\@curtab#1\relax PythonT EX \@primes}} Mathematics with \def\@primes{\loop\advance\count@\@ne Sympy \expandafter\ifx\csname p-\the\count@\endcsname\relax Plots with matplotlib \ifnum\@tempcntb<\count@\else Web Services \ifnum\count@<\@curtab\else\comma\the\count@\fi\fi\else\repeat Conclusions \@tempcnta\count@\loop\advance\@tempcnta\count@ \expandafter\let\csname p-\the\@tempcnta\endcsname\@ne Extra Examples \ifnum\@tempcnta<\@tempcntb\repeat \ifnum\@tempcntb>\count@\expandafter\@primes\fi} \makeatother

  9. page.9 Karl Koeller’s Response A Gentle Introduction to PythonT EX A solution using the \pgfmathisprime macro provided by Alain Andrew Mertz, William Slough Matthes’ tkz-euclide package: Overview A Question of Primes Introduction to \usepackage{tkz-euclide} PythonT EX Mathematics with \newif\ifcomma Sympy Plots with matplotlib \newcommand{\primes}[2]{% Web Services \commafalse% Conclusions \foreach\numb in {#1,...,#2}{% Extra Examples \pgfmathisprime{\numb}% \ifnum\pgfmathresult=1 \ifcomma, \numb\else\numb\global\commatrue\fi% \fi% }% }

  10. page.10 Can PythonT EX Make This Simpler? A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Question of Primes Introduction to PythonT EX Yes. . . Mathematics with Sympy Plots with matplotlib Web Services Conclusions Extra Examples

  11. page.11 Evaluating Expressions With \py A Gentle Introduction to PythonT EX Andrew Mertz, William Slough The macro \py{expression} evaluates a Python expression and Overview typesets its value . A Question of Primes Introduction to PythonT EX Mathematics with Sympy Plots with matplotlib Did you know that $2^{65} = \py{2**65}$? Web Services Conclusions Extra Examples Did you know that 2 65 = 36893488147419103232?

  12. page.12 Evaluating Expressions With \pyc A Gentle Introduction to PythonT EX Andrew Mertz, William Slough The macro \pyc{expression} evaluates a Python expression and typesets anything that it prints . Overview A Question of Primes Introduction to PythonT EX Did you know that $2^{65} = \pyc{print(2**65)}$? Mathematics with Sympy Plots with matplotlib Web Services Did you know that 2 65 = 36893488147419103232? Conclusions Extra Examples While “printing” adds little in this case, it is important for more complex examples.

  13. page.13 A More Complex Example Using \pyc A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Question of Primes Introduction to PythonT EX Mathematics with Sympy Plots with matplotlib Web Services Conclusions Extra Examples \pyc{showGoogleMap("Tokyo", 11)}

  14. page.14 Charleston, Illinois USA A Gentle Introduction to PythonT EX Andrew Mertz, William Slough Overview A Question of Primes Introduction to PythonT EX Mathematics with Sympy Plots with matplotlib Web Services Conclusions Extra Examples \pyc{showGoogleMap("600 Lincoln,Charleston,IL", 14)}

  15. page.15 Generating Tables With pycode A Gentle Introduction to PythonT EX \begin{pycode} Andrew Mertz, print(r"\begin{tabular}{c|c}") William Slough print(r"$m$ & $2^m$ \\ \hline") Overview print(r"%d & %d \\" % (1, 2**1)) A Question of Primes print(r"%d & %d \\" % (2, 2**2)) Introduction to PythonT EX print(r"%d & %d \\" % (3, 2**3)) Mathematics with print(r"%d & %d \\" % (4, 2**4)) Sympy print(r"\end{tabular}") Plots with matplotlib Web Services \end{pycode} Conclusions Extra Examples \begin{tabular}{c|c} $m$ & $2^m$ \\ \hline 1 & 2 \\ 2 & 4 \\ 3 & 8 \\ 4 & 16 \\ \end{tabular}

  16. page.16 Generating Tables With pycode A Gentle Introduction to PythonT EX \begin{pycode} Andrew Mertz, print(r"\begin{tabular}{c|c}") William Slough print(r"$m$ & $2^m$ \\ \hline") Overview print(r"%d & %d \\" % (1, 2**1)) A Question of Primes print(r"%d & %d \\" % (2, 2**2)) Introduction to PythonT EX print(r"%d & %d \\" % (3, 2**3)) Mathematics with print(r"%d & %d \\" % (4, 2**4)) Sympy print(r"\end{tabular}") Plots with matplotlib Web Services \end{pycode} Conclusions Extra Examples 2 m m 1 2 2 4 3 8 4 16

  17. page.17 Generating Tables With a Loop A Gentle Introduction to \begin{pycode} PythonT EX Andrew Mertz, lo, hi = 1, 6 William Slough print(r"\begin{tabular}{c|c}") Overview print(r"$m$ & $2^m$ \\ \hline") A Question of Primes for m in range(lo, hi + 1): Introduction to PythonT EX print(r"%d & %d \\" % (m, 2**m)) Mathematics with print(r"\end{tabular}") Sympy \end{pycode} Plots with matplotlib Web Services Conclusions m 2 m Extra Examples 1 2 2 4 3 8 4 16 5 32 6 64

  18. page.18 Defining a Function A Gentle Introduction to PythonT EX Andrew Mertz, William Slough \begin{pycode} Overview def fib(n): # nth Fibonacci value A Question of Primes a, b = 0, 1 Introduction to for i in range(n): PythonT EX a, b = b, a + b Mathematics with Sympy return a Plots with matplotlib \end{pycode} Web Services Conclusions Extra Examples Did you know that $F_{10} = \py{fib(10)}$? Did you know that F 10 = 55?

Recommend


More recommend