In [3]:
%matplotlib inline

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, etc.

  • Blog, GitHub

Contact

This talk - a proposal, a hypothesis, a statement

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


In [ ]:
As I will attempt to describe in the next slides, Python is an amazing way to lead to a more fun learning and teaching
experience.

It can be a basic calculator, a fancy calculator and 

Math, Science, Geography..

Tools that will help us in that quest are:

(Main) Tools

Python - a scientific calculator

  • Python 3 is my favorite calculator (not Python 2 because 1/2 = 0)
  • fabs(), abs(), sin(), cos(), gcd(), log() (See math)

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

Python - a really fancy calculator

SymPy - a pure Python symbolic math library

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


In [ ]:
When you bring in SymPy to the picture, things really  get awesome. You are suddenly writing computer
programs which are capable of speaking algebra. You are no more limited to numbers.

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}$$

Python - Making other subjects more lively

  • matplotlib

  • basemap

  • Interactive Jupyter Notebooks

Bringing Science to life

Animation of a Projectile motion

Drawing fractals

Interactively drawing a Barnsley Fern

The world is your graph paper

Showing places on a digital map

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

Predict the college admission score based on high school math score

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


In [ ]:
### TODO: digit recognition using Neural networks

In [ ]:
### Scikitlearn, pandas, scipy, statsmodel

Book: Doing Math With Python

Published by No Starch Press, out in 2015.

Early feedback very encouraging

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 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

PyCon Special!

Use PYCONMATH code to get 30% off from No Starch Press

(Valid from May 26th - June 8th)

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

Dialogue

Questions, Thoughts, comments, discussions?

Online: @echorand, amitsaha.in@gmail.com

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