Why Python? Python I

Total Page:16

File Type:pdf, Size:1020Kb

Why Python? Python I What is Python? Why Python? Python I ... is an interpreted programming language. I ... was invented around 1991. Joseph Thomas I ... is intended to emphasize code readability. I ... takes ideas from several programming paradigms University of Arizona Department of Mathematics I procedural languages (like C) I object oriented languages (like Java) November 5, 2012 I functional languages (like Lisp, Haskell) I ... is used by many industries (Google, etc.). Why Python? Why Python? For the mathematical researcher, a software tool should: Python is a useful language to know if you want to... I Let you implement an idea fast. I quickly (and correctly) implement an abstract idea. I Avoid reinventing the wheel. I bring together many different libraries (which might not be in I Allow you to naturally convert math into code. Python) to do something new. I Example: Is a (mathematical) function I easily link your code into a computer algebra system (namely I A procedure call? Sage). I An array? I A hash map / dictionary / associative array? I process data (particularly text) and/or crawl the internet. I A set of tuples? Example: Bayesian Spam Filtering Example: Bayesian Spam Filtering We need to do an experiment! I Get a collection of emails, classified into spam and not-spam. I Estimate P(w appears in a spam email) by I Q: Can we detect a statistical difference between the emails written by spammers and regular people? # appearances of word w in the corpus I Fundamental Problem: Given a word w, estimate the # words in the corpus probability w comes from a spam email. I Context: Use Bayes' Theorem to estimate the probability an Programming Problem: email is spam, based upon its words. I Input: A bunch of emails (text). I I made a lot of assumptions. Do I still have a useful Output: A dictionary mapping words to the conditional approximation? I probability that they appear in an email from the spam corpus. I Sub-Problems: I Reading a file (get a big string). I Break the text of the file into pieces. I Building a dictionary data structure. Python Code Example: Bayesian Spam Filtering | Java versus Python Input: A file F of emails. Python Solution: 93 lines of code. Output: A mapping of words to conditional probabilities. Java Solution: 321 lines of code. Why the difference? W = open("emails.txt").read().split() D = {} I Different languages have different goals! for w in W: I Python: Get a small prototype working quickly. if w not in D: I Java: Build huge applications from organized code. D[w] = 1 else : I Matlab/Mathematica/GAP: Make working with math easy D[w] = D[w] + 1 (without necessarily providing a lot of CS primitives). Moral: If you want to know \Is my theory correct?", maybe for w in D: D[w] = D[w]/len(W) code-correctness and ease of implementation matter more than speed and maintainability. Python, Sage, and Visualization Python, Sage, and Visualization 3 Common Geometry Problem: Visualizing examples in R . Sage, a computer algebra system from the University of Example: Consider a hyper-rectangular prism. Washington, is written in Python. I The project takes advantage of a lot of good software engineering ideas, particularly when it comes to documentation. I Unlike Mathematica, GAP, and Matlab, there is basically no distinction between Python code and Sage code. I Theme: Prototype in SAGE, then move polished code out into python files. Up to scaling, a hyper-rectangular prism is specified by 3 positive I Let's see an example! lengths (x; y; z). Research Problem: Investigate geodesics on this space, based on (x; y; z). Hyper-rectangular Prism Drawing Tumblings Calculation Problem: I Easy: How does a geodesic move through one cell? 3 I Hard: Tracking how all eight cells are connected. A tumbling is just a sequence of adjacent rectangular prisms in R . We'll use JMol to visualize it. I Given a sequence of cells, is there a geodesic through them? I want to show different things in different situations: I I want to visualize this with a tumbling! I The labels we put on the vertices/edges. I Only certain labels on the vertices. I Particular colors of the cells. How can I get all of this in one procedure call? def drawCube( eCube, showEdgesTuple=(False,False,False), showVertexLabelPred=(lambda vert: True), colorCube=False ): A \tumbling" of the hyper-rectangular prism. Using Libraries Libraries What if I need to do [Computationally Intensive Process P]? Anecdote: I I once studied a bio-informatics problem in which I needed to I Numpy : Linear algebra, big arrays, Fourier analysis, etc. solve a big, messy linear program at run-time. I cvxopt/PyGLPK : Linear programming solvers. I Linear programming packages are usually written in C. I Cython : Connect your C/C++ code to Python. I Bad Assumption: If I need a C library, my whole program I Call C/C++ from Python must be in C. I Call Python from C/C++ I Pass data between languages (without writing/parsing text I Price of this assumption: ∼ 3000 lines of hard-to-modify C files). code. (Replaced by ∼ 600 lines of easy Python code.) Moral: Sometimes it's better to specify your problem in a descriptive language, then hand it over to a C library for solving. Example: Gluing Code Georg Lichtenberg Johan Frank Christian von Weigel Carl Gauß Georg-August- Abraham Kästner Georg Beer Ruprecht-Karls- Georg-August- Universität Universität Universität Leipzig Universität Wien Universität Universität Helmstedt Göttingen 1739 1786 Heidelberg Göttingen 1799 1765 1766 1771 Heinrich Brandes Friedrich Bessel Karl Rudolphi Philipp von Walther Georg-August- Georg-August- Ernst-Moritz-Arndt- Ludwig-Maximilians- Universität Universität Universität The Mathematics Genealogy Project catalogs people who received Universität Landshut Göttingen Göttingen Greifswald 1803 PhD's in math and data on their advisor(s). 1800 1810 1795 Johannes Müller Christoph Gudermann Heinrich Scherk Rheinische Friedrich- Humboldt-Universität Universität Berlin Wilhelms-Universität All of this data is posted to the web. zu Berlin 1823 Bonn 1832 1822 Ernst Kummer Karl Weierstraß Martin-Luther- Hermann von Helmholtz Universität Universität Halle- Universität Berlin Question: What would the \genealogical tree" of the department Königsberg Wittenberg 1842 1854 look like? 1831 Hermann Schwarz Friedrich Schottky Universität Berlin Universität Berlin 1864 1875 Leon Lichtenstein Problems: Universität Berlin 1909 We can find out, for ≥ $150 + (Shipping and Handling). Aurel Wintner I Universität Leipzig 1928 The MGP website doesn't offer all the data in a convenient I Monroe Martin Shlomo Sternberg The Johns Hopkins The Johns Hopkins University University text file. 1932 1957 Jim Cushing Victor Guillemin University of Maryland We could do all the work by hand (a lot of labor). Harvard University I College Park 1962 1968 Python Solution: ∼ 300 lines of code. Leonid Friedlander Massachusetts Institute of Technology 1989 Wrap Up In Summary: I Mathematical research on the computer is not software engineering. I Python makes many good ideas from software engineering accessible for mathematical research. I It allows you to develop a correct prototype quickly ... I ... then improve your code's speed/usability when you know you've found a good idea. I It allows you to connect disparate tools and libraries... I ...and integrate them into a computer algebra system, Sage. Thanks!.
Recommended publications
  • Mathematicians Fleeing from Nazi Germany
    Mathematicians Fleeing from Nazi Germany Mathematicians Fleeing from Nazi Germany Individual Fates and Global Impact Reinhard Siegmund-Schultze princeton university press princeton and oxford Copyright 2009 © by Princeton University Press Published by Princeton University Press, 41 William Street, Princeton, New Jersey 08540 In the United Kingdom: Princeton University Press, 6 Oxford Street, Woodstock, Oxfordshire OX20 1TW All Rights Reserved Library of Congress Cataloging-in-Publication Data Siegmund-Schultze, R. (Reinhard) Mathematicians fleeing from Nazi Germany: individual fates and global impact / Reinhard Siegmund-Schultze. p. cm. Includes bibliographical references and index. ISBN 978-0-691-12593-0 (cloth) — ISBN 978-0-691-14041-4 (pbk.) 1. Mathematicians—Germany—History—20th century. 2. Mathematicians— United States—History—20th century. 3. Mathematicians—Germany—Biography. 4. Mathematicians—United States—Biography. 5. World War, 1939–1945— Refuges—Germany. 6. Germany—Emigration and immigration—History—1933–1945. 7. Germans—United States—History—20th century. 8. Immigrants—United States—History—20th century. 9. Mathematics—Germany—History—20th century. 10. Mathematics—United States—History—20th century. I. Title. QA27.G4S53 2008 510.09'04—dc22 2008048855 British Library Cataloging-in-Publication Data is available This book has been composed in Sabon Printed on acid-free paper. ∞ press.princeton.edu Printed in the United States of America 10 987654321 Contents List of Figures and Tables xiii Preface xvii Chapter 1 The Terms “German-Speaking Mathematician,” “Forced,” and“Voluntary Emigration” 1 Chapter 2 The Notion of “Mathematician” Plus Quantitative Figures on Persecution 13 Chapter 3 Early Emigration 30 3.1. The Push-Factor 32 3.2. The Pull-Factor 36 3.D.
    [Show full text]
  • Recollections and Notes, Vol. 2 (1945–1968) Translated by Abe
    Vita Mathematica 19 Hugo Steinhaus Mathematician for All Seasons Recollections and Notes, Vol. 2 (1945–1968) Translated by Abe Shenitzer Edited by Robert G. Burns, Irena Szymaniec and Aleksander Weron Vita Mathematica Volume 19 Edited by Martin MattmullerR More information about this series at http://www.springer.com/series/4834 Hugo Steinhaus Mathematician for All Seasons Recollections and Notes, Vol. 2 (1945–1968) Translated by Abe Shenitzer Edited by Robert G. Burns, Irena Szymaniec and Aleksander Weron Author Hugo Steinhaus (1887–1972) Translator Abe Shenitzer Brookline, MA, USA Editors Robert G. Burns Department of Mathematics and Statistics York University Toronto, Ontario, Canada Irena Szymaniec Wrocław, Poland Aleksander Weron The Hugo Steinhaus Center Wrocław University of Technology Wrocław, Poland Vita Mathematica ISBN 978-3-319-23101-3 ISBN 978-3-319-23102-0 (eBook) DOI 10.1007/978-3-319-23102-0 Library of Congress Control Number: 2015954183 Springer Cham Heidelberg New York Dordrecht London © Springer International Publishing Switzerland 2016 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
    [Show full text]
  • Norbert Wiener
    NATIONAL ACADEMY OF SCIENCES N O rb E R T W I E N E R 1894—1964 A Biographical Memoir by I rvIN G E zrA S E G AL Any opinions expressed in this memoir are those of the author(s) and do not necessarily reflect the views of the National Academy of Sciences. Biographical Memoir COPYRIGHT 1992 NATIONAL ACADEMY PRESS WASHINGTOND.C. fU*tf/r^,^ NORBERT WIENER November 26, 1894-March 18, 1964 BY IRVING EZRA SEGAL ORBERT WIENER was one of the most original mathemati- N cians and influential scientsts of the twentieth century. He developed a new, purely mathematical theory, an inte- gral calculus for functions of infinitely many variables known as functional integration. It has been of great importance for probability and theoretical physics. Wiener made huge strides in the harmonic analysis of functions of real and complex variables. In a unified way, this resolved old problems, produced new challenges, and provided a prototype for key aspects of harmonic analysis on topological groups. In part concurrently, he developed applications of his math- ematical ideas in engineering, biology, and other fields. In later life he developed a synthesis of such applications with diverse ideas represented by central parts of the work done in the twenties and thirties by Vannevar Bush, Walter B. Cannon, Alan Turing, and others. This synthesis, which he called "cybernetics," has since been a productive unifying philosophy in science and engi- neering. In the United States, it primarily epitomized his earlier contributions to communication engineering; in Britain, it had a notable impact on neurophysiology, and its de- layed, but eventually enthusiastic, acceptance in the Soviet Union stimulated important mathematical developments 389 390 BIOGRAPHICAL MEMOIRS in control and ergodic theory.
    [Show full text]
  • Siegmund-Schultze R. Mathematicians Fleeing from Nazi Germany
    Mathematicians Fleeing from Nazi Germany Mathematicians Fleeing from Nazi Germany Individual Fates and Global Impact Reinhard Siegmund-Schultze princeton university press princeton and oxford Copyright 2009 © by Princeton University Press Published by Princeton University Press, 41 William Street, Princeton, New Jersey 08540 In the United Kingdom: Princeton University Press, 6 Oxford Street, Woodstock, Oxfordshire OX20 1TW All Rights Reserved Library of Congress Cataloging-in-Publication Data Siegmund-Schultze, R. (Reinhard) Mathematicians fleeing from Nazi Germany: individual fates and global impact / Reinhard Siegmund-Schultze. p. cm. Includes bibliographical references and index. ISBN 978-0-691-12593-0 (cloth) — ISBN 978-0-691-14041-4 (pbk.) 1. Mathematicians—Germany—History—20th century. 2. Mathematicians— United States—History—20th century. 3. Mathematicians—Germany—Biography. 4. Mathematicians—United States—Biography. 5. World War, 1939–1945— Refuges—Germany. 6. Germany—Emigration and immigration—History—1933–1945. 7. Germans—United States—History—20th century. 8. Immigrants—United States—History—20th century. 9. Mathematics—Germany—History—20th century. 10. Mathematics—United States—History—20th century. I. Title. QA27.G4S53 2008 510.09'04—dc22 2008048855 British Library Cataloging-in-Publication Data is available This book has been composed in Sabon Printed on acid-free paper. ∞ press.princeton.edu Printed in the United States of America 10 987654321 Contents List of Figures and Tables xiii Preface xvii Chapter 1 The Terms “German-Speaking Mathematician,” “Forced,” and“Voluntary Emigration” 1 Chapter 2 The Notion of “Mathematician” Plus Quantitative Figures on Persecution 13 Chapter 3 Early Emigration 30 3.1. The Push-Factor 32 3.2. The Pull-Factor 36 3.D.
    [Show full text]