Arrays
Literals, zeros, indexing, elementwise arithmetic, and passing arrays through functions. Run a cell to see its output beside the expected one.
Elementwise math on an array
Every 1-argument math builtin maps elementwise over an array. No loop.
Broadcasting an array and a scalar
+, -, *, / broadcast between arrays and scalars, in either order.
Resetting an array in place with fill
fill(a, v) sets every element. Rebinding (a = zeros(n)) makes a new array, and a caller that passed a in never sees the change; fill changes the array in place.
Literals and indexing
Comma-separated subscripts, zero-based.
zeros and indexed stores
zeros allocates rank 1 through 4.
Elementwise arithmetic
* between arrays is elementwise (the Hadamard product); array times scalar scales.
len and linspace
len reads the leading dimension.
Functions take and return arrays
head_tail takes an array and returns a new one.
Scalars broadcast
A scalar combines elementwise with an array from either side, for every arithmetic operator.
Matrices combine elementwise too
Rank 3 and 4 exist
Allocate with zeros; index with one subscript per axis. show prints the flat data for rank 3+.
linspace endpoints are inclusive
Both endpoints are in the array, as in NumPy. With n = 1 it is the start alone, and with n = 0 it is empty.
dot is the loop you would write
dot(a, b) is a builtin. The def below is the loop it runs, left to right, and gives the same sum.
Negative indices count from the end
As in Python: a[-1] is the last element, a[:-1] all but it, A[-1, :] the last row.
A comprehension builds an array
Its for takes any loop head, and its if keeps the elements it holds for.
in, and a key for max and sorted
x in a is NumPy's: some element equals x. A key is a def, a builtin or fn(x) -> ....
norm is the square root of dot
norm(v) is a builtin too, the square root of dot(v, v).