2. Short Lesson: Math Operations#
This notebook just highlights a couple of useful constants and functions we will use all the time. It assumes you know how to perform simiple math operations at this point.
Reminder \(\text{**}\) means power not “^”.
5**2
25
5^2 #this does not mean power as you can see from output
7
First thing we do is import the numpy extension and the scipy.constants extension which has a lot of useful mathematical functions. A link to the numpy documentation can be found under the jupyter Help menu.
import numpy as np
from scipy import constants
Some available constants:
np.e
np.pi
constants.e
constants.pi
constants.R
constants.N_A
constants.k
np.e # this "e" is the base of the natural logarithm
2.718281828459045
constants.e # notice that this "e" is the charge on an electron
1.6021766208e-19
Some available functions:
np.cos(), np.sin(), np.arcsin(), etc.
np.log(), np.log10(), np.sqrt(), etc.
np.mean(), np.sum(), np.median(), etc
General Math: https://numpy.org/devdocs/reference/routines.math.html
Statistics: https://numpy.org/devdocs/reference/routines.statistics.html
np.cos(np.pi)
-1.0
np.log10(100)
2.0
We can use sympy if we want to perform symbolic manipulation such as derivatives, integration, solving equations, etc. We will look at that later in the course.
np.sqrt(9)
3.0
np.random.randn(3)
array([-1.27598594, -0.9301613 , 0.39859151])
np.random.random((3))
array([0.94765948, 0.27808895, 0.89048267])
np.random.randint(3,10, size=100) #notice that 3,10 gives numbers from 3,9
array([9, 5, 9, 8, 6, 6, 9, 7, 6, 9, 3, 9, 6, 8, 4, 3, 3, 6, 4, 9, 3, 5,
6, 8, 7, 3, 6, 8, 3, 5, 5, 3, 7, 5, 8, 7, 4, 4, 4, 3, 6, 6, 7, 6,
4, 5, 5, 5, 4, 9, 6, 8, 5, 6, 3, 5, 9, 7, 3, 6, 7, 3, 4, 8, 8, 5,
8, 3, 5, 6, 4, 7, 6, 3, 5, 6, 9, 3, 6, 7, 3, 4, 5, 3, 3, 9, 6, 4,
4, 7, 9, 8, 9, 3, 3, 4, 8, 9, 3, 6])