First: some comments
I could probably have shaved more off the problem but this is quite short and general
The animation was made using pyorb - one of my
packages
(code is in the handout)
"Solving the problem" took about 10 minutes to make because
Lets this lecture go trough
Two levels for the solution
An example problem: implement vector dot product
Two levels for the solution
An example problem: implement vector dot product
def dot(vec1, vec2):
result = 0
for ind in range(len(vec1)):
result = result + vec1[ind] * vec2[ind]
return result
def dot(vec1, vec2):
result = 0
for ind in range(len(vec1)):
result = result + vec1[ind] * vec2[ind]
return result
def dot(vec1, vec2):
result = 0
for x1, x2 in zip(vec1, vec2):
result += x1 * x2
return result
Not dependant on [], shortest iter goes, readable
def dot(vec1, vec2):
result = 0
for x1, x2 in zip(vec1, vec2):
result += x1 * x2
return result
def dot(vec1, vec2):
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
Not dependant on first type, less readable, robust?
def dot(vec1, vec2):
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
def dot(vec1, vec2):
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
def dot(vec1, vec2):
assert len(vec1) == len(vec2), "Input vectors must be same length"
if len(vec1) == 0:
return
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
def dot(vec1, vec2):
assert len(vec1) == len(vec2), "Input vectors must be same length"
if len(vec1) == 0:
return
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
def dot(vec1, vec2):
assert len(vec1) == len(vec2), "Input vectors must be same length"
if len(vec1) == 0:
return
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
This is either a really cool or a really bad feature - depending on what you want
def dot(vec1, vec2):
assert len(vec1) == len(vec2), "Input vectors must be same length"
if len(vec1) == 0:
return
common_iter = zip(vec1, vec2)
x1, x2 = next(common_iter)
result = x1 * x2
for x1, x2 in common_iter:
result += x1 * x2
return result
Balance of abstraction and specification, safety and brevity, efficiency and ease
Lets move to numpy arrays
import numpy as np
def dot(vec1, vec2):
if np.can_cast(vec1.dtype, vec2.dtype, casting="safe"):
result = vec2.dtype.type(0)
elif np.can_cast(vec2.dtype, vec1.dtype, casting="safe"):
result = vec1.dtype.type(0)
else:
raise TypeError("Types cannot be cast safely")
assert vec1.size == vec2.size
for x1, x2 in zip(vec1, vec2):
result += x1 * x2
return result
Now we have MORE code that just checks input than actual working lines
"Meh! No one is gonna input something stupid to this"
import numpy as np
def dot(vec1: np.ndarray, vec2: np.ndarray):
result = vec1.dtype.type(0)
for x1, x2 in zip(vec1, vec2):
result += x1 * x2
return result
from numpy import dot
We could also just know the correct tools for the job
+it is written already / tested (hopefully) / robust (hopefully) / fast (hopefully)
(you can never assume with dependencies)
-added a dependency (and its own tree) / no control over behaviour / no option to optimize
from numpy import dot
E.g. coordinate transforms (used everywhere):
religiously
tested and validated and designed
E.g. my CLI interface:
Thrown together and when
it crashes I fix it
E.g. I trust numpy to be fast and scipy to be
correct
I don't trust the 1 star, 0 issues,
last commit 7 years ago, github repo
So that is what we are doing!
First: some common patterns I think are useful to know
All of these are described in the handout!
Time to code!
If you don't want to log in: use This input file - the result should be 1660292
Let's compare solutions
Further study and homework
Let's check out the handout above!