Skip to main content

MATH 345: Linear Algebra and Optimization

Section B.12 Small Python Patterns Used in the Labs

This section helps you read small Python patterns that support repeated mathematical computations in the labs.
def cosim(u, v):
    return (u @ v) / (np.linalg.norm(u) * np.linalg.norm(v))
losses = []

for k in range(num_steps):
    losses.append(float(x @ x))
    x = x - alpha * grad_f(x)
results = {alpha: run_gd(alpha) for alpha in [0.05, 0.2, 1.05]}

Helper function.

def cosim(u, v):
Read as. Define a helper function.
Shape/return. A function.
Used for. Repeated computations.

Function output.

return ...
Read as. Output from a function.
Shape/return. A value.
Used for. Helper functions.

Empty list.

losses = []
Read as. An empty list.
Shape/return. A list.
Used for. Recording values.

Repeated loop.

for k in range(num_steps):
Read as. Repeat the indented commands.
Shape/return. Repeated commands.
Used for. Iterative algorithms.

Append value.

losses.append(...)
Read as. Add one recorded value.
Shape/return. A modified list.
Used for. Training or descent history.

Plain number.

float(...)
Read as. Convert to a plain number.
Shape/return. A scalar.
Used for. Cleaner output.

Dictionary comprehension.

{alpha: run_gd(alpha) for ...}
Read as. Build a dictionary by repeating a computation.
Shape/return. A dictionary.
Used for. Comparing learning rates.

List comprehension.

[f(x) for x in values]
Read as. Build a list by applying the same expression to each item.
Shape/return. A list.
Used for. Compact repeated calculations.
The labs also use list comprehensions, zip, dict, len, round, print, and assert. These are ordinary Python patterns. The course emphasis remains the mathematical meaning of the computation.