In [3]:
%matplotlib inline

Hello everyone! My name is Amit Saha and today I am here to talk to about "Doing Math with Python".

Thank you for coming to my talk - i know you could have chosen the other talk, so it's good to know that my talk's topic interests you.

A bit about me - I am a software engineer at Freelancer.com in Sydney Australia. I am a fairly regular writer for Linux Voice and other Linux magazines. And last, but not the least, I am the author of the book "Doing Math with Python" (not coincidentally titled the same as this talk - haha) published by No Starch Press in 2015.

There is a link to my blog, GitHub, twitter, etc. so, if you want to learn more about my work or get in touch, those are the means to do so!

Okay, so all that aside - let's start with the talk!

Doing Math with Python

Amit Saha

May 29, PyCon US 2016 Education Summit

Portland, Oregon

About me

  • Software Engineer at Freelancer.com HQ in Sydney, Australia

  • Author of "Doing Math with Python" (No Starch Press, 2015)

  • Writes for Linux Voice, Linux Journal, and others.

  • Blog, GitHub

Contact

Slides: https://doingmathwithpython.github.io/pycon-us-2016/

All presentation materials: https://github.com/doingmathwithpython

So, what am I selling to you today? Not my book (I am, but in a subtle way). I am presenting an idea, a hypothesis or even making a statement - Python can lead to a more enriching learning and teaching experience in the classroom.

Let me explain where I am coming from. When I think back about when I was learning to program and learning all other subjects in standards 7-10. I think it's true today as well. Programming and other subjects such as Math, Science are taught in a disconnected fashion. Programming seems to be all about finding the sum of a series or generating fibonacci numbers. Make no mistake, these exercises are what builds up the programming logic. Some students get really excited about being able to do these, but a lot of them don't. It's a lot like not everyone gets interested in solving puzzles - i don't, i never took to them.

I think I know of a way we could excite more students! Show them how you can write programs to do your homework, or experiment without having to go the science lab or setup elaborate experimental setups. This is my goal for today - in the following slides and notebooks, I will hypothesise on a way of connecting Python programming and other subjects. That will show that programming is a way to get real work done, not something to learn for the sake of it.

We need some tools to help us on our quest. The Python community has some giant shoulders we can stand upon - Python 3, SymPy and matplotlib.

This talk - a proposal, a hypothesis, a statement

What? Python can lead to a more enriching learning and teaching experience in the classroom

How? Next slides

Tools (or, Giant shoulders we will stand on)

Python 3, SymPy, matplotlib

*Individual logos are copyright of the respective projects. Source of the "giant shoulders" image.

Whose calculator looks like this?

Who uses Python as a calculator? Raise of hands please!

I do! Specifically, I use Python 3 because of 1/2=0 messes up my monthly expenditure calculation.

Besides the usual addition and subtraction, we have of course the math module and more recently the statistics module which makes Python a worthy scientific calculator.

But then, there's more! You are not limited to the functions from those libraries, you can write your own custom functions and make them available whenever you start your Python interpreter. How?

Use PYTHONSTARTUP!

Python - a scientific calculator

Whose calculator looks like this?

>>> (131 + 21.5 + 100.2 + 88.7 + 99.5 + 100.5 + 200.5)/4
185.475

Python 3 is my favorite calculator (not Python 2 because 1/2 = 0)

Beyond basic operations:

  • fabs(), abs(), sin(), cos(), gcd(), log() and more (See math)

  • Descriptive statistics (See statistics)

Python - a scientific calculator

  • Develop your own functions: unit conversion, finding correlation, .., anything really

  • Use PYTHONSTARTUP to extend the battery of readily available mathematical functions

$ PYTHONSTARTUP=~/work/dmwp/pycon-us-2016/startup_math.py idle3 -s

Unit conversion functions

>>> unit_conversion()
1. Kilometers to Miles
2. Miles to Kilometers
3. Kilograms to Pounds
4. Pounds to Kilograms
5. Celsius to Fahrenheit
6. Fahrenheit to Celsius
Which conversion would you like to do? 6
Enter temperature in fahrenheit: 98
Temperature in celsius: 36.66666666666667
>>>

Finding linear correlation

>>> 
>>> x = [1, 2, 3, 4]
>>> y = [2, 4, 6.1, 7.9]
>>> find_corr_x_y(x, y)
0.9995411791453812

So, that was Python and it's standard libraries. When you bring in third party libraries to the mix, Python becomes a seriously fancy calculator.

Who has heard about SymPy?

You can give it algebraic expressions to a function and a graph will be created for you.

You can give an equation and out comes the solutions for that equation.

We can even solve calculus problems.

Python - a really fancy calculator

SymPy - a pure Python symbolic math library

from sympy import awesomeness - don't try that :)


In [5]:
# Create graphs from algebraic expressions

from sympy import Symbol, plot
x = Symbol('x')
p = plot(2*x**2 + 2*x + 2)



In [13]:
# Solve equations

from sympy import solve, Symbol
x = Symbol('x')
solve(2*x + 1)


Out[13]:
[-1/2]

In [24]:
# Limits

from sympy import Symbol, Limit, sin
x = Symbol('x')
Limit(sin(x)/x, x, 0).doit()


Out[24]:
1

In [2]:
# Derivative

from sympy import Symbol, Derivative, sin, init_printing
x = Symbol('x')
init_printing()
Derivative(sin(x)**(2*x+1), x).doit()


Out[2]:
$$\left(\frac{\left(2 x + 1\right) \cos{\left (x \right )}}{\sin{\left (x \right )}} + 2 \log{\left (\sin{\left (x \right )} \right )}\right) \sin^{2 x + 1}{\left (x \right )}$$

In [16]:
# Indefinite integral

from sympy import Symbol, Integral, sqrt, sin, init_printing
x = Symbol('x')
init_printing()
Integral(sqrt(x)).doit()


Out[16]:
$$\frac{2 x^{\frac{3}{2}}}{3}$$

In [19]:
# Definite integral

from sympy import Symbol, Integral, sqrt
x = Symbol('x')
Integral(sqrt(x), (x, 0, 2)).doit()


Out[19]:
$$\frac{4 \sqrt{2}}{3}$$

I will pause for a moment now. In the first two slides, we have seen how Python can be a super awesome calculator. What does that buy us? We have now been able to show that you can make computer programs literally do your homework. Write a program to do your work once and you will never have to make those lengthy calculations yourselves. Can we use Python to do more?

Let's continue.

Can we do more than write smart calculators?

Python can be more than a super powerful calculator. We can use it to enhance the learning experience of other subjects. Next, I have three examples including a demo. First up, a video of a projectile motion. This program uses matplotlib's animation API to create a basic animation of a projectile motion - a fairly common subject introduced in introductory Physics. The program which is linked asks for the angle of projection and speed and then draws the trajectory of the projectile. Just by running the program multiple times, we can see how the trajectory changes. We don't have to go outside and start throwing balls..

Next, we will put Jupyter Notebook's interactive widgets to good effect by drawing a Barnsley Fern. Let's see how the demo goes.

Next, with the help of basemap, we can draw places on a world map like we would draw points on a graph paper.

I know I would be excited if someone was showing me all these cool things when I was learning these things!

Python - Making other subjects more lively

  • matplotlib

  • basemap

  • Interactive Jupyter Notebooks

Bringing Science to life

Animation of a Projectile motion (Python Source)


In [3]:
from IPython.display import YouTubeVideo
YouTubeVideo("8uWRVh58KdQ")


Out[3]:

Exploring Fractals in Nature

Interactively drawing a Barnsley Fern (Notebook)

The world is your graph paper

Showing places on a digital map (Notebook)

Next, I would like to talk about my book "Doing Math with Python". My idea was attractive enough to get it published by No Starch Press which makes me hope that I am probably onto something.

Has anybody read my book? What do you think of it? You have read it and came to my talk? I am feeling better :)

I discuss all of the topics I discuss today in my talk. In addition, I discuss sets, probability and random numbers and descriptive statistics.

It's being translated into several non-English languages.

The reviews/feedback so far has been really positive. I don't have any first hand involvement in teaching, so it's very appreciative of people to share their viewpoints with me.

Book: Doing Math With Python

Overview

  • All of what I have discussed so far

  • In addition: Descriptive statistics, Sets and Probability, Random numbers

Published by No Starch Press in August, 2015.

Upcoming/In-progress translations: Simplified Chinese, Japanese, French and Korean.

Comments

Saha does an excellent job providing a clear link between Python and upper-level math concepts, and demonstrates how Python can be transformed into a mathematical stage. This book deserves a spot on every geometry teacher’s bookshelf.

School Library Journal

Outstanding guide to using Python to do maths. Working back through my undergrad maths using Python.

Saha does an excellent job providing a clear link between Python and upper-level math concepts, and demonstrates how Python can be transformed into a mathematical stage.

This book is highly recommended for the high school or college student and anyone who is looking for a more natural way of programming math and scientific functions

As a teacher I highly recommend this book as a way to work with someone in learning both math and programming

Okay, so that's great. We have successfully used Python to make the learning experience of young learners more fun and immediately applicable. Can we derive more benefit from doing that? Like something for the future? We all love doing things for the future, don't we?

I think yes, i think if we teach young learners the things we have discussed today, it is a great base for someone wanting to go into data science or machine learning.

Statistics and visualising data are two very key factors of data science.

Differential calculus and specifically the gradient descent method is a simple but useful optimization method used in Machine Learning. Let's see a demo of using gradient descent to find the minimum value of a function.

Now, let's apply gradient descent as an optimizer in a Linear Regression problem.

Great base for the future

Statistics and Graphing data -> Data Science

Differential Calculus -> Machine learning

Application of differentiation

Use gradient descent to find a function's minimum value (Notebook)

Predict the college admission score based on high school math score

Use gradient descent as the optimizer for single variable linear regression model (Notebook)

Dialogue

Questions, Thoughts, comments, discussions?

Online

  • Twitter: @echorand

  • Email: amitsaha.in@gmail.com

PyCon Special!

Use PYCONMATH code to get 30% off "Doing Math with Python" from No Starch Press

(Valid from May 26th - June 8th)

Book Signing - May 31st - 2.00 PM - No Starch Press booth

Acknowledgements

PyCon US Education Summit team for inviting me

Thanks to PyCon US for reduced registration rates

Massive thanks to my employer, Freelancer.com for sponsoring my travel and stay