def f_a_v3(m, n):
return numpy.arange(m).reshape((m, 1)) * numpy.float64(numpy.arange(n))
numpy.arange(m).reshape((m, 1))
numpy.arange(m)
produces an array of integers from 0
up to m - 1
.
For example, if m = 3
, numpy.arange(m)
is [0, 1, 2]
.
.reshape((m, 1))
turns this 1D array into a 2D array with shape (m, 1)
.
Continuing our example, this becomes:
$$ \begin{bmatrix} 0\\ 1\\ 2 \end{bmatrix} $$
This gives you a “column” of indices, one for each row.
numpy.float64(numpy.arange(n))
numpy.arange(n)
produces an array of integers from 0
to n - 1
.
For example, if n = 4
, that is [0, 1, 2, 3]
.
numpy.float(…)
casts the resulting array to type float64. Technically, you could also rely on numpy’s automatic type promotion, but explicitly converting it ensures the final result will be floating point.
Because this is not further reshaped, it remains a 1D array (shape(n,)
). You can think of it as a row of column indices.
... * ...
When you multiply a (m, 1)
array by a (n,)
array, numpy broadcasts them together.
(m, 1)
array effectively replicates the single column across all columns of length n
.(n,)
array replicates its single row across all rows of length m
.As a result, each element of the final 2D array at position (i, j)
becomes:
$$ i \times j $$
in floating point format.
The function returns this broadcasted product immediately. If you pass m = 3
and n = 4
, the returned 2D array (of shape (3, 4)
) is:
[[0. 0. 0. 0.]
[0. 1. 2. 3.]
[0. 2. 4. 6.]]