import numpy as np
import sympy as sp
Math for Data Science
Start Calculating in Python - Math Fundamentals
Important Information
- Email: joanna_bieri@redlands.edu
- Office Hours take place in Duke 209 unless otherwise noted – Office Hours Schedule
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.
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
= 14.9858187509
my_number = 3
decimal_place
= round(my_number,decimal_place)
rounded_number
print(rounded_number)
14.986
# Rounding numbers - negative decimal places
= 14.9858187509
my_number = -1
decimal_place
= round(my_number,decimal_place)
rounded_number
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
= 14.9858187509
my_number = ".2e"
my_format = format(my_number, my_format)
scientific_notation
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
= 10
a = 3
b = 27
c = 2
d = -4
e
= (a+b)*e/(c-2)
first_answer
print(first_answer)
-2.08
# I can keep calculating and Python will remember my variables
= first_answer**2
second_answer 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
= np.exp(a)
first_answer print(first_answer)
22026.465794806718
= np.sqrt(first_answer)
second_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
= sp.Rational(1,3)
a 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
= sp.Rational(1,4)
b 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?
+b a
\(\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?
*b a
\(\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?
/b a
\(\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?
= sp.symbols('a,b,x,y') a,b,x,y
Multiplying with the same base:
= x**a*x**b
my_expr my_expr
\(\displaystyle x^{a} x^{b}\)
sp.simplify(my_expr)
\(\displaystyle x^{a + b}\)
Multipying with different base:
= x**a*y**b
my_expr my_expr
\(\displaystyle x^{a} y^{b}\)
sp.simplify(my_expr)
\(\displaystyle x^{a} y^{b}\)
Adding with same base:
= x**a+y**b
my_expr my_expr
\(\displaystyle x^{a} + y^{b}\)
sp.simplify(my_expr)
\(\displaystyle x^{a} + y^{b}\)
Adding with different base
= x**a+y**b
my_expr my_expr
\(\displaystyle x^{a} + y^{b}\)
sp.simplify(my_expr)
\(\displaystyle x^{a} + y^{b}\)
Adding with same exponent
= x**a+3*x**a
my_expr my_expr
\(\displaystyle 4 x^{a}\)
Exponents of exponents
= (x**a)**b
my_expr 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
= sp.symbols('a,b,x',real=True,positive=True) a,b,x
= (x**a)**b
my_expr my_expr
\(\displaystyle x^{a b}\)
# How do we deal with negative exponents?
= (x**a)**(-b)
my_expr my_expr
\(\displaystyle x^{- a b}\)
Negative exponents
# Exponents raised to a negative power
# Remember the negative gives the reciprocal
=2
c**(-c) x
\(\displaystyle \frac{1}{x^{2}}\)
# Exponents raised to a negative power
# Number types are important
# Here c is represented as a decimal 0.5
=0.5
c**(-c) x
\(\displaystyle x^{-0.5}\)
# Exponents raised to a negative power
# Here c is represented as a rational number 1/2
=sp.Rational(1,2)
c**(-c) x
\(\displaystyle \frac{1}{\sqrt{x}}\)
# We can also use the square root function
1/x) sp.sqrt(
\(\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!
= sp.symbols('a,b,x,y') a,b,x,y
= sp.exp(x)
my_expr my_expr
\(\displaystyle e^{x}\)
= sp.log(x)
my_expr my_expr
\(\displaystyle \log{\left(x \right)}\)
= sp.log(sp.exp(x))
my_expr my_expr
\(\displaystyle \log{\left(e^{x} \right)}\)
= sp.symbols('a,b,x,y',real=True)
a,b,x,y = sp.log(sp.exp(x))
my_expr 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 \]
= sp.log(x,4)
my_expr 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)} \]
= 4**x
my_expr 4) sp.log(my_expr,
\(\displaystyle \frac{\log{\left(4^{x} \right)}}{\log{\left(4 \right)}}\)
4)) sp.simplify(sp.log(my_expr,
\(\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.