Multiply a 2×2 matrix by a length-2 vector by hand. An outer loop over rows feeds an inner loop that accumulates one dot product per row into s. The replay shows s resetting to 0 at each new row, then building up before being appended to result.

By hand

With NumPy

mat @ vec applies the @ (matmul) operator: each element of the output is the dot product of the corresponding row of mat with vec. The snapshot shows the matrix, the vector, and the result with their shapes.

naive.py
A = [[1, 2], [3, 4]]
v = [5, 6]
result = []
for row in A:
    s = 0
    for i in range(len(v)):
        s += row[i] * v[i]
    result.append(s)
print('RESULT:', result)
library.py
import numpy as np

A = [[1, 2], [3, 4]]
v = [5, 6]
mat = np.array(A)
vec = np.array(v)
result = mat @ vec
print('mat: shape:', mat.shape, 'dtype:', mat.dtype, 'values:', mat.tolist())
print('vec: shape:', vec.shape, 'dtype:', vec.dtype, 'values:', vec.tolist())
print('result: shape:', result.shape, 'dtype:', result.dtype, 'values:', result.tolist())
print('RESULT:', result.tolist())
mat: shape: (2, 2) dtype: int64 values: [[1, 2], [3, 4]]
vec: shape: (2,) dtype: int64 values: [5, 6]
result: shape: (2,) dtype: int64 values: [17, 39]
RESULT: [17, 39]

Implementation notes

  • Each element of the output is a dot product: result[0] = row0 · vec, result[1] = row1 · vec. The hand-written loop makes this explicit — see dot-product for the scalar version.
  • The shapes must be compatible: (m, n) @ (n,)(m,). Here (2, 2) @ (2,)(2,). A shape mismatch raises a ValueError.
  • @ dispatches to np.matmul for 2-D arrays. For a matrix times a matrix use the same @ operator: (m, n) @ (n, p)(m, p).
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.