SlideShare a Scribd company logo
Pythonic Mathematics Kirby Urner EuroPython 2005 Göteborg, Sweden
Principal Themes Math Through Programming Math Through Storytelling Beyond Flatland Curriculum as Network
Math Through Programming Common goal:  Literacy Levels of fluency Reading (Recognition) Writing (Recall) Planning Designing Testing and Correcting (Debugging)
Math Through Storytelling © USPS
Beyond Flatland
Curriculum as Network: Typical Urner-style graph of inter- connecting topics
OO, Functions, More OO We need a big picture of the OO design at first, to make sense of dot notation around core objects such as lists i.e. “these are types, and this is how we think of them, use them.”  Use lots of analogies, metaphors. Write your own classes a little later, after you’ve defined and saved functions in modules.  Get more specific about syntax at this point.
From a class for home schoolers, taught by me @ Free Geek in PDX See:  http://www.4dsolutions.net/ocn/pygeom.html
OO & Data Structures Aristotle and the Kingdoms (family trees) The Tree (big in classical thinking) Sets (big in mid 20 th  century “new math”) Data Structures:  list, array, vector, matrix, table, n-tuple, record, dictionary, hash, stack, queue, network, tree, linked list, graph… Python:  a good language for show and tell plus plays well with others
New Hybrid: CS + Mathematics Mathematics as extensible type system A focus on algorithms that use objects Mix traditional notation w/ OO’s dot notation Math Objects:  polynomials, integers, integers modulo N, permutations, vectors, matrices, quaternions, polyhedra … Python’s operator overloading:  a gateway to new levels of abstraction
Python plays well with others The prospect of getting to do computer graphics is a motivational incentive to tackle “the hard stuff.”  The promise:  we’ll take you somewhere fun.
Types of Graphics with sample apps & libraries From presentation given at OSCON in 2004. See: http://www.4dsolutions.net/oscon2004/ POV-Ray VPython PyOpenGL POV-Ray Tk wxWidgets PyGame PIL Spatial Flat Still Moving
Python + POV-Ray
Jython + POV-Ray + QuickHull3D
Python + VRML + Qhull
Python + PIL http://www.4dsolutions.net/ocn/fractals.html
Python + Tk Cellular Automata ala Wolfram generated using graphics.py by John Zelle
Python + VPython
Functions and Figurate Numbers def   tri (n):  return  n * (n + 1) // 2 >>>  [tri(x)  for  x  in   range (1, 10)] [1, 3, 6, 10, 15, 21, 28, 36, 45] Front cover: The Book of Numbers by Conway & Guy
Sequence Generators >>>   def  tritet():   term = trinum = tetranum = 1   while   True :   yield  (term, trinum, tetranum)   term += 1   trinum += term   tetranum += trinum   >>>  gen = tritet() >>>  [gen.next()  for  i  in   range (6)] [(1, 1, 1), (2, 3, 4), (3, 6, 10),  (4, 10, 20), (5, 15, 35), (6, 21, 56)] from  Synergetics by RBF w/ EJA
Polyhedral Numbers Animation:  growing cuboctahedron >>>  gen = cubocta() >>>  [gen.next()  for  i  in   range (6)] [(1, 1, 1), (2, 12, 13), (3, 42, 55),  (4, 92, 147), (5, 162, 309), (6, 252, 561)]
Cubocta Shell = Icosa Shell def   cubocta (): freq = shell = cubonum = 1 while   True : yield  (freq, shell, cubonum) shell = 10 * freq**2 + 2 cubonum += shell freq += 1 From Synergetics Java +  POV-Ray
Pascal’s Triangle def   pascal (): row = [1] while  True :   yield  row   this = [0] + row   next = row + [0]   row = [a+b  for  a,b  in  zip(this, next)] >>>  gen = pascal() >>>  gen.next() [1] >>>  gen.next() [1, 1] >>>  gen.next() [1, 2, 1]
Fermat’s Little Theorem def   gcd (a,b): """Euclidean Algorithm""" while  b: a, b = b, a % b return  abs(a) if  isprime(p)  and  gcd(b,p) == 1: try: assert  pow(b, p - 1, p) == 1 except: raise  \ Exception,   'Houston, we’ve got a problem.'
Euler’s Theorem def   tots (n):  return  [i  for  i  in   range (1,n)  if  gcd(i, n)==1] def   phi (n): return len(tots(n)) if  gcd(b,n) == 1: try: assert  pow(b, phi(n), n) == 1 except: raise  \ Exception,  'Houston, we’ve got a problem.'
RSA def   demo (): """ Abbreviated from more complete version at:  http://www.4dsolutions.net/satacad/sa6299/rsa.py """ plaintext =  "hello world" m = txt2n(plaintext) p,q = getf(20), getf(20)  # two big primes N = p*q phiN = (p-1)*(q-1) e = 3 s,t,g = eea(e, phiN)  # Extended Euclidean Algorithm d = s % phiN c = encrypt(m,N)  # pow(m, e, N) w/ booster newm = decrypt(c,d,N)  # pow(c, e*d, N) plaintext = n2txt(newm) return  plaintext
Math Objects: grab from the library  and/or build your own >>>  mypoly = Poly([ (7,0), (2,1), (3,3), (-4,10) ] ) >>>  mypoly (7) + (2*x) + (3*x**3) + (-4*x**10) >>>  int1, int2 = M(3, 12), M(5, 12)  # used in crypto >>>  int1 * int2  # operator overloading 3 >>>  int1 – int2 10 >>>  tetra = rbf.Tetra() >>>  bigtetra = tetra * 3  # volume increases 27-fold >>>  bigtetra.render()
Example:  Build A Rational Number Type From http://www.4dsolutions.net/ocn/python/mathteach.py def   cf2 (terms): """ Recursive approach to continued fractions, returns Rat object >>> cf2([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) (4181/2584) >>> cf2([1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]) (665857/470832) >>> (665857./470832) 1.4142135623746899 """ if  len(terms)==1: return  terms.pop() else: return  Rat(terms.pop(0),1) + Rat(1, cf2(terms))
Conclusions “ Programming to learn” is as valid an activity as “learning to program.”  My proposal is about programming in Python in order to build a stronger understanding of mathematics. Mastering specialized “learning languages” or even “math languages” doesn’t offer the same payoff as mastering a full-featured generic computer language. This approach and/or curriculum is not for everyone. A wide variety of approaches exist even  within  the broad brush strokes vision sketched out here.
Thank You! And now… HyperToon demo + Q&A Presentations repository: http://www.4dsolutions.net/presentations/

More Related Content

PDF
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
PDF
Learning to Reconstruct at Stanford
Jonas Adler
 
PDF
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Paulo Sergio Lemes Queiroz
 
PDF
PyTorch for Deep Learning Practitioners
Bayu Aldi Yansyah
 
PDF
Introduction to NumPy for Machine Learning Programmers
Kimikazu Kato
 
KEY
Numpy Talk at SIAM
Enthought, Inc.
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PDF
Introduction To TensorFlow | Deep Learning with TensorFlow | TensorFlow For B...
Edureka!
 
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
Learning to Reconstruct at Stanford
Jonas Adler
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Paulo Sergio Lemes Queiroz
 
PyTorch for Deep Learning Practitioners
Bayu Aldi Yansyah
 
Introduction to NumPy for Machine Learning Programmers
Kimikazu Kato
 
Numpy Talk at SIAM
Enthought, Inc.
 
Introduction to NumPy (PyData SV 2013)
PyData
 
Introduction To TensorFlow | Deep Learning with TensorFlow | TensorFlow For B...
Edureka!
 

What's hot (19)

KEY
NumPy/SciPy Statistics
Enthought, Inc.
 
PDF
The Elements of Machine Learning
Alexander Jung
 
PPT
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Enthought, Inc.
 
PDF
Ada boosting2
Nassim Asbai
 
PDF
Recommendation System --Theory and Practice
Kimikazu Kato
 
PPTX
Tensorflow in practice by Engineer - donghwi cha
Donghwi Cha
 
PPTX
論文紹介 Fast imagetagging
Takashi Abe
 
PDF
Ensembles of Many Diverse Weak Defenses can be Strong: Defending Deep Neural ...
Pooyan Jamshidi
 
PDF
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
Edge AI and Vision Alliance
 
PDF
2020 1학기 정기스터디 1주차
Moonki Choi
 
PPTX
Introduction to PyTorch
Jun Young Park
 
PDF
TensorFlow In 10 Minutes | Deep Learning & TensorFlow | Edureka
Edureka!
 
PDF
Scientific Computing with Python - NumPy | WeiYuan
Wei-Yuan Chang
 
PDF
Intoduction to numpy
Faraz Ahmed
 
PDF
Tokyo webmining 2017-10-28
Kimikazu Kato
 
PPTX
Machine Learning - Introduction to Tensorflow
Andrew Ferlitsch
 
PDF
ICML2013読み会 Large-Scale Learning with Less RAM via Randomization
Hidekazu Oiwa
 
PDF
Numpy tutorial(final) 20160303
Namgee Lee
 
PDF
NumPy Refresher
Lukasz Dobrzanski
 
NumPy/SciPy Statistics
Enthought, Inc.
 
The Elements of Machine Learning
Alexander Jung
 
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Enthought, Inc.
 
Ada boosting2
Nassim Asbai
 
Recommendation System --Theory and Practice
Kimikazu Kato
 
Tensorflow in practice by Engineer - donghwi cha
Donghwi Cha
 
論文紹介 Fast imagetagging
Takashi Abe
 
Ensembles of Many Diverse Weak Defenses can be Strong: Defending Deep Neural ...
Pooyan Jamshidi
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
Edge AI and Vision Alliance
 
2020 1학기 정기스터디 1주차
Moonki Choi
 
Introduction to PyTorch
Jun Young Park
 
TensorFlow In 10 Minutes | Deep Learning & TensorFlow | Edureka
Edureka!
 
Scientific Computing with Python - NumPy | WeiYuan
Wei-Yuan Chang
 
Intoduction to numpy
Faraz Ahmed
 
Tokyo webmining 2017-10-28
Kimikazu Kato
 
Machine Learning - Introduction to Tensorflow
Andrew Ferlitsch
 
ICML2013読み会 Large-Scale Learning with Less RAM via Randomization
Hidekazu Oiwa
 
Numpy tutorial(final) 20160303
Namgee Lee
 
NumPy Refresher
Lukasz Dobrzanski
 
Ad

Viewers also liked (18)

PDF
(Sin anotaciones) - En busca de la Física
Fernando Salamero
 
PDF
Programación de Videojuegos con Python y Pilas (III)
Fernando Salamero
 
PDF
Programación de Videojuegos con Python y Pilas (I)
Fernando Salamero
 
PDF
Programación de Videojuegos con Python y Pilas (II)
Fernando Salamero
 
PDF
Python básico II
Fernando Salamero
 
PDF
Intro Pygame Capitulo 6
Ricardo Daniel Quiroga
 
PDF
Programación con Pygame VIII
Fernando Salamero
 
PDF
Programación con Pygame III
Fernando Salamero
 
PDF
Intro PyGame Capitulo 0
Ricardo Daniel Quiroga
 
PDF
Intro PyGame Capitulo 1
Ricardo Daniel Quiroga
 
PDF
Programación con Pygame IV
Fernando Salamero
 
PDF
Programación con Pygame IX
Fernando Salamero
 
PDF
Taller de Pilas Engine, un motor de juegos en Python - PyConES 2014
Fernando Salamero
 
PDF
Curso Programacion de Juego Introducion IA
Ricardo Daniel Quiroga
 
PDF
Intro PyGame Capitulo 5
Ricardo Daniel Quiroga
 
PDF
Iniciación a python
Fernando Salamero
 
PDF
Programación de Videojuegos con Python y Pilas (VI)
Fernando Salamero
 
PDF
Programación con Pygame I
Fernando Salamero
 
(Sin anotaciones) - En busca de la Física
Fernando Salamero
 
Programación de Videojuegos con Python y Pilas (III)
Fernando Salamero
 
Programación de Videojuegos con Python y Pilas (I)
Fernando Salamero
 
Programación de Videojuegos con Python y Pilas (II)
Fernando Salamero
 
Python básico II
Fernando Salamero
 
Intro Pygame Capitulo 6
Ricardo Daniel Quiroga
 
Programación con Pygame VIII
Fernando Salamero
 
Programación con Pygame III
Fernando Salamero
 
Intro PyGame Capitulo 0
Ricardo Daniel Quiroga
 
Intro PyGame Capitulo 1
Ricardo Daniel Quiroga
 
Programación con Pygame IV
Fernando Salamero
 
Programación con Pygame IX
Fernando Salamero
 
Taller de Pilas Engine, un motor de juegos en Python - PyConES 2014
Fernando Salamero
 
Curso Programacion de Juego Introducion IA
Ricardo Daniel Quiroga
 
Intro PyGame Capitulo 5
Ricardo Daniel Quiroga
 
Iniciación a python
Fernando Salamero
 
Programación de Videojuegos con Python y Pilas (VI)
Fernando Salamero
 
Programación con Pygame I
Fernando Salamero
 
Ad

Similar to Pythonic Math (20)

PPT
C#, What Is Next?
Pieter Joost van de Sande
 
PDF
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
PPT
Object Orientation vs. Functional Programming in Python
Python Ireland
 
PPT
Introduction to Algorithms
Venkatesh Iyer
 
ODP
Scala as a Declarative Language
vsssuresh
 
PDF
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
ODP
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
PPT
Function Approx2009
Imthias Ahamed
 
PDF
Machine Learning With R
David Chiu
 
PDF
phan-tich-va-thiet-ke-thuat-toan_pham-quang-dung-and-do-phan-thuan_chapter01-...
MaiTrungTung
 
PPT
Matlab Nn Intro
Imthias Ahamed
 
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PPTX
PyCon 2011 talk - ngram assembly with Bloom filters
c.titus.brown
 
PDF
Python Puzzlers
Tendayi Mawushe
 
PPTX
Pycon 2011 talk (may not be final, note)
c.titus.brown
 
PDF
Writing Faster Python 3
Sebastian Witowski
 
PDF
Introduction to Erlang
Gabriele Lana
 
PPTX
Intelligent Ruby + Machine Learning
Ilya Grigorik
 
PDF
Python Lab manual program for BE First semester (all department
Nazeer Wahab
 
C#, What Is Next?
Pieter Joost van de Sande
 
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Object Orientation vs. Functional Programming in Python
Python Ireland
 
Introduction to Algorithms
Venkatesh Iyer
 
Scala as a Declarative Language
vsssuresh
 
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
Function Approx2009
Imthias Ahamed
 
Machine Learning With R
David Chiu
 
phan-tich-va-thiet-ke-thuat-toan_pham-quang-dung-and-do-phan-thuan_chapter01-...
MaiTrungTung
 
Matlab Nn Intro
Imthias Ahamed
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PyCon 2011 talk - ngram assembly with Bloom filters
c.titus.brown
 
Python Puzzlers
Tendayi Mawushe
 
Pycon 2011 talk (may not be final, note)
c.titus.brown
 
Writing Faster Python 3
Sebastian Witowski
 
Introduction to Erlang
Gabriele Lana
 
Intelligent Ruby + Machine Learning
Ilya Grigorik
 
Python Lab manual program for BE First semester (all department
Nazeer Wahab
 

Pythonic Math

  • 1. Pythonic Mathematics Kirby Urner EuroPython 2005 Göteborg, Sweden
  • 2. Principal Themes Math Through Programming Math Through Storytelling Beyond Flatland Curriculum as Network
  • 3. Math Through Programming Common goal: Literacy Levels of fluency Reading (Recognition) Writing (Recall) Planning Designing Testing and Correcting (Debugging)
  • 6. Curriculum as Network: Typical Urner-style graph of inter- connecting topics
  • 7. OO, Functions, More OO We need a big picture of the OO design at first, to make sense of dot notation around core objects such as lists i.e. “these are types, and this is how we think of them, use them.” Use lots of analogies, metaphors. Write your own classes a little later, after you’ve defined and saved functions in modules. Get more specific about syntax at this point.
  • 8. From a class for home schoolers, taught by me @ Free Geek in PDX See: http://www.4dsolutions.net/ocn/pygeom.html
  • 9. OO & Data Structures Aristotle and the Kingdoms (family trees) The Tree (big in classical thinking) Sets (big in mid 20 th century “new math”) Data Structures: list, array, vector, matrix, table, n-tuple, record, dictionary, hash, stack, queue, network, tree, linked list, graph… Python: a good language for show and tell plus plays well with others
  • 10. New Hybrid: CS + Mathematics Mathematics as extensible type system A focus on algorithms that use objects Mix traditional notation w/ OO’s dot notation Math Objects: polynomials, integers, integers modulo N, permutations, vectors, matrices, quaternions, polyhedra … Python’s operator overloading: a gateway to new levels of abstraction
  • 11. Python plays well with others The prospect of getting to do computer graphics is a motivational incentive to tackle “the hard stuff.” The promise: we’ll take you somewhere fun.
  • 12. Types of Graphics with sample apps & libraries From presentation given at OSCON in 2004. See: http://www.4dsolutions.net/oscon2004/ POV-Ray VPython PyOpenGL POV-Ray Tk wxWidgets PyGame PIL Spatial Flat Still Moving
  • 14. Jython + POV-Ray + QuickHull3D
  • 15. Python + VRML + Qhull
  • 16. Python + PIL http://www.4dsolutions.net/ocn/fractals.html
  • 17. Python + Tk Cellular Automata ala Wolfram generated using graphics.py by John Zelle
  • 19. Functions and Figurate Numbers def tri (n): return n * (n + 1) // 2 >>> [tri(x) for x in range (1, 10)] [1, 3, 6, 10, 15, 21, 28, 36, 45] Front cover: The Book of Numbers by Conway & Guy
  • 20. Sequence Generators >>> def tritet(): term = trinum = tetranum = 1 while True : yield (term, trinum, tetranum) term += 1 trinum += term tetranum += trinum >>> gen = tritet() >>> [gen.next() for i in range (6)] [(1, 1, 1), (2, 3, 4), (3, 6, 10), (4, 10, 20), (5, 15, 35), (6, 21, 56)] from Synergetics by RBF w/ EJA
  • 21. Polyhedral Numbers Animation: growing cuboctahedron >>> gen = cubocta() >>> [gen.next() for i in range (6)] [(1, 1, 1), (2, 12, 13), (3, 42, 55), (4, 92, 147), (5, 162, 309), (6, 252, 561)]
  • 22. Cubocta Shell = Icosa Shell def cubocta (): freq = shell = cubonum = 1 while True : yield (freq, shell, cubonum) shell = 10 * freq**2 + 2 cubonum += shell freq += 1 From Synergetics Java + POV-Ray
  • 23. Pascal’s Triangle def pascal (): row = [1] while True : yield row this = [0] + row next = row + [0] row = [a+b for a,b in zip(this, next)] >>> gen = pascal() >>> gen.next() [1] >>> gen.next() [1, 1] >>> gen.next() [1, 2, 1]
  • 24. Fermat’s Little Theorem def gcd (a,b): """Euclidean Algorithm""" while b: a, b = b, a % b return abs(a) if isprime(p) and gcd(b,p) == 1: try: assert pow(b, p - 1, p) == 1 except: raise \ Exception, 'Houston, we’ve got a problem.'
  • 25. Euler’s Theorem def tots (n): return [i for i in range (1,n) if gcd(i, n)==1] def phi (n): return len(tots(n)) if gcd(b,n) == 1: try: assert pow(b, phi(n), n) == 1 except: raise \ Exception, 'Houston, we’ve got a problem.'
  • 26. RSA def demo (): """ Abbreviated from more complete version at: http://www.4dsolutions.net/satacad/sa6299/rsa.py """ plaintext = "hello world" m = txt2n(plaintext) p,q = getf(20), getf(20) # two big primes N = p*q phiN = (p-1)*(q-1) e = 3 s,t,g = eea(e, phiN) # Extended Euclidean Algorithm d = s % phiN c = encrypt(m,N) # pow(m, e, N) w/ booster newm = decrypt(c,d,N) # pow(c, e*d, N) plaintext = n2txt(newm) return plaintext
  • 27. Math Objects: grab from the library and/or build your own >>> mypoly = Poly([ (7,0), (2,1), (3,3), (-4,10) ] ) >>> mypoly (7) + (2*x) + (3*x**3) + (-4*x**10) >>> int1, int2 = M(3, 12), M(5, 12) # used in crypto >>> int1 * int2 # operator overloading 3 >>> int1 – int2 10 >>> tetra = rbf.Tetra() >>> bigtetra = tetra * 3 # volume increases 27-fold >>> bigtetra.render()
  • 28. Example: Build A Rational Number Type From http://www.4dsolutions.net/ocn/python/mathteach.py def cf2 (terms): """ Recursive approach to continued fractions, returns Rat object >>> cf2([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) (4181/2584) >>> cf2([1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]) (665857/470832) >>> (665857./470832) 1.4142135623746899 """ if len(terms)==1: return terms.pop() else: return Rat(terms.pop(0),1) + Rat(1, cf2(terms))
  • 29. Conclusions “ Programming to learn” is as valid an activity as “learning to program.” My proposal is about programming in Python in order to build a stronger understanding of mathematics. Mastering specialized “learning languages” or even “math languages” doesn’t offer the same payoff as mastering a full-featured generic computer language. This approach and/or curriculum is not for everyone. A wide variety of approaches exist even within the broad brush strokes vision sketched out here.
  • 30. Thank You! And now… HyperToon demo + Q&A Presentations repository: http://www.4dsolutions.net/presentations/