Math for Data Science

Start Calculating in Python - Math Fundamentals

Author

Joanna Bieri
DATA100

Important Information

Todays Goals:

  • Start using Python as a calculator.
  • Deeper thinking - math review:
    • Fractions
    • Exponents and Logarithms (calculations)
    • Algebra

Python is my New Calculator!

Today we are going to start experimenting with Python as a calculator. One goal of the class is to get you to be comforatble using Python as a way to understand math topics, check answers, and solve problems.

import numpy as np
import sympy as sp

Sympy and Numpy

Numpy is for numerical calculations - the result is a number not an equation.

Sympy is for symbolic calculations - the result is usually an equaion, although you can ask it to calculate a number.

BEWARE - Once you define something as a Sympy object it does not play with with Numpy.

Get out a piece of paper and do the calculations by hand as we go through the python code!


Rounding

# Rounding numbers
round(14.9858187509,3)
14.986
# Rounding numbers - decimal places
my_number = 14.9858187509
decimal_place = 3

rounded_number = round(my_number,decimal_place)

print(rounded_number)
14.986
# Rounding numbers - negative decimal places
my_number = 14.9858187509
decimal_place = -1

rounded_number = round(my_number,decimal_place)

print(rounded_number)
10.0
# What will the code do?
'''
my_number = 14.9858187509
decimal_place = 0

rounded_number = round(my_number,decimal_place)

print(rounded_number)
'''

print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
# Your code here.

Scientific Notation

# Scientific (exponential) Notation
my_number = 14.9858187509
my_format = ".2e"
scientific_notation = format(my_number, my_format)

print('The format .2e tells us we want exponential notation with two decimal places!')
print(scientific_notation) 
The format .2e tells us we want exponential notation with two decimal places!
1.50e+01
# Scientific (exponential) Notation
print('What would happen if we changed from .2e to .4e?')
print('How would you do this calculation by hand?')
What would happen if we changed from .2e to .4e?
How would you do this calculation by hand?
# What will the code do?
'''
my_number = 135726384769325
my_format = ".2e"
scientific_notation = format(my_number, my_format)
'''

print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
# Your code here.

Calculator Calculations

# Add, Subtract, Multiply, Divide
a = 10
b = 3
c = 27
d = 2
e = -4

first_answer = (a+b)*e/(c-2)

print(first_answer)
-2.08
# I can keep calculating and Python will remember my variables
second_answer = first_answer**2
print(second_answer)
4.3264000000000005
# What will the code do?
'''
first_answer = a**d/e
print(first_answer)
'''
print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
# Your code here.
# What will the code do?
'''
second_answer = first_answer - c
print(second_answer)
'''
print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
# Your code here.
# Evaluate functions
first_answer = np.exp(a)
print(first_answer)
22026.465794806718
second_answer = np.sqrt(first_answer)
print(second_answer)
148.4131591025766
# What will the code do?
'''
first_answer = np.sin(b)
second_answer = np.sqrt(first_answer)
print(second_answer)
'''
print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
# Your code here.

Fractions

# To keep fractions in rational form
a = sp.Rational(1,3)
a

\(\displaystyle \frac{1}{3}\)

print('How would you write the fraction 5/7 using sp.Rational(a,b)?')
How would you write the fraction 5/7 using sp.Rational(a,b)?
# Here is another fraction
b = sp.Rational(1,4)
b

\(\displaystyle \frac{1}{4}\)

# Fraction Calculations

# Lets do a+b - we are adding 1/3 and 1/4
# What do we need to do?
# Why does it make sense that we need a common denominator?
# Can you draw a picture that represents this?

a+b

\(\displaystyle \frac{7}{12}\)

# Lets do a*b
# What are the rules here?
# Why does it make sense that we just multiply 
# the numerator numbers and denominator numbers?
# Can you draw a picture that represents this?

a*b

\(\displaystyle \frac{1}{12}\)

# Let's do a/b
# What are the rules here?
# Why does it make sense that we just multiply by the reciprocal?
# Remember division by fraction is asking about how many pieces there will be.
# Can you draw a picture that represents this?

a/b

\(\displaystyle \frac{4}{3}\)

YOU TRY!

First calculate by hand:

\[\frac{\frac{2}{3}+\frac{5}{4}}{\frac{3}{2}}\]

Then use Python and Sympy to confirm your answer.

# Your code here

Exponents

# Exponents remember we use ** to raise something to a power!
3**2
9
# What are the exponent rules?
a,b,x,y = sp.symbols('a,b,x,y')

Multiplying with the same base:

my_expr = x**a*x**b
my_expr

\(\displaystyle x^{a} x^{b}\)

sp.simplify(my_expr)

\(\displaystyle x^{a + b}\)

Multipying with different base:

my_expr = x**a*y**b
my_expr

\(\displaystyle x^{a} y^{b}\)

sp.simplify(my_expr)

\(\displaystyle x^{a} y^{b}\)

Adding with same base:

my_expr = x**a+y**b
my_expr

\(\displaystyle x^{a} + y^{b}\)

sp.simplify(my_expr)

\(\displaystyle x^{a} + y^{b}\)

Adding with different base

my_expr = x**a+y**b
my_expr

\(\displaystyle x^{a} + y^{b}\)

sp.simplify(my_expr)

\(\displaystyle x^{a} + y^{b}\)

Adding with same exponent

my_expr = x**a+3*x**a
my_expr

\(\displaystyle 4 x^{a}\)

Exponents of exponents

my_expr = (x**a)**b
my_expr

\(\displaystyle \left(x^{a}\right)^{b}\)

# When we raise an exponent to an exponent we add the exponents
# But this is only true if the exponents are real numbers
# And the symbolic logic works better if we know if parameters are positive or negative
a,b,x = sp.symbols('a,b,x',real=True,positive=True)
my_expr = (x**a)**b
my_expr

\(\displaystyle x^{a b}\)

# How do we deal with negative exponents?
my_expr = (x**a)**(-b)
my_expr

\(\displaystyle x^{- a b}\)

Negative exponents

# Exponents raised to a negative power
# Remember the negative gives the reciprocal
c=2
x**(-c)

\(\displaystyle \frac{1}{x^{2}}\)

# Exponents raised to a negative power
# Number types are important
# Here c is represented as a decimal 0.5
c=0.5
x**(-c)

\(\displaystyle x^{-0.5}\)

# Exponents raised to a negative power
# Here c is represented as a rational number 1/2
c=sp.Rational(1,2)
x**(-c)

\(\displaystyle \frac{1}{\sqrt{x}}\)

# We can also use the square root function
sp.sqrt(1/x)

\(\displaystyle \frac{1}{\sqrt{x}}\)

YOU TRY

First simplify by hand

\[ (2^4+(4^{1/2})^4) \]

Then use Python and Sympy or Numpy to confirm your answer

# Your code here

YOU TRY

First simplify by hand

\[ \frac{(x^5+x^2x^3)^2}{x^2} \]

Then use Python and Sympy to confirm your answer

# Your code here

Logarithms vs Exponents - Frenemies to the end!

a,b,x,y = sp.symbols('a,b,x,y')
my_expr = sp.exp(x)
my_expr

\(\displaystyle e^{x}\)

my_expr = sp.log(x)
my_expr

\(\displaystyle \log{\left(x \right)}\)

my_expr = sp.log(sp.exp(x))
my_expr

\(\displaystyle \log{\left(e^{x} \right)}\)

a,b,x,y = sp.symbols('a,b,x,y',real=True)
my_expr = sp.log(sp.exp(x))
my_expr

\(\displaystyle x\)

Logs and Exponents are inverses of eachother! What does this mean?

If I have an equation like:

\[ 2^x = 3\]

and I want to get to that \(x\) to solve… aka I want to get rid of that base \(2\)… then I can use the log to do it!

\[ log_2(2^x) = x = log_2(3) \]

If I have an equation like:

\[ ln(x) = log_e(x) = 5 \]

and I want to get to that \(x\) to solve… aka I want to get rid of the log… then I can use the exponent to do it!

\[ e^{ln(x)} = e^{log_e(x)} = x = e^5 \]

my_expr = sp.log(x,4)
4**my_expr

\(\displaystyle x\)

# Why does this look so funny?
my_expr

\(\displaystyle \frac{\log{\left(x \right)}}{\log{\left(4 \right)}}\)

This has to do with the change of base formula

\[ log_b(a) = \frac{log_e(a)}{log_e(b)} = \frac{ln(a)}{ln(b)} \]

my_expr = 4**x
sp.log(my_expr,4)

\(\displaystyle \frac{\log{\left(4^{x} \right)}}{\log{\left(4 \right)}}\)

sp.simplify(sp.log(my_expr,4))

\(\displaystyle x\)

YOU TRY

First simplify by hand

\[ \ln((e^{x^2})^2) \]

Then use Python and Sympy to confirm your answer

# Your code here

If you want more practice:

  • No Bullshit Guide to Math & Physics:

      - page 11 E1.1, E1.3
      - page 22 E1.4
      - page 44 E1.11

See if you can do these both by hand and in python.

We will do more work with exponents and logarithms later in class.