Elementwise Math
Elementwise Product
Two 6-element lists multiplied position by position in an index loop. The
trace shows i stepping through each index and result accumulating one
pairwise product per iteration.
By hand
Walk each index i from 0 to len(a) - 1. Multiply a[i] by b[i] and
append the product to result.
naive.py
Replay: real traced execution (multi-file project)
a = [1, 2, 3, 4, 5, 6]
b = [2, 3, 4, 5, 6, 7]
result = []
for i in range(len(a)):
result.append(a[i] * b[i])
print('RESULT:', result)
a ← [1, 2, 3, 4, 5, 6]
1a = [1, 2, 3, 4, 5, 6]2b = [2, 3, 4, 5, 6, 7]values this step[1, 2, 3, 4, 5, 6]ab ← [2, 3, 4, 5, 6, 7]
1a = [1, 2, 3, 4, 5, 6]2b = [2, 3, 4, 5, 6, 7]3result = []values this step[2, 3, 4, 5, 6, 7]bresult ← []
2b = [2, 3, 4, 5, 6, 7]3result = []4for i in range(len(a)):values this step[]resulti ← 0
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])values this step0iresult ← [2]
4for i in range(len(a)):5 result.append(a[i] * b[i])6print('RESULT:', result)values this step[] → [2]resulti ← 1
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])values this step0 → 1iresult ← [2, 6]
4for i in range(len(a)):5 result.append(a[i] * b[i])6print('RESULT:', result)values this step[2] → [2, 6]resulti ← 2
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])values this step1 → 2iresult ← [2, 6, 12]
4for i in range(len(a)):5 result.append(a[i] * b[i])6print('RESULT:', result)values this step[2, 6] → [2, 6, 12]resulti ← 3
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])values this step2 → 3iresult ← [2, 6, 12, 20]
4for i in range(len(a)):5 result.append(a[i] * b[i])6print('RESULT:', result)values this step[2, 6, 12] → [2, 6, 12, 20]resulti ← 4
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])values this step3 → 4iresult ← [2, 6, 12, 20, 30]
4for i in range(len(a)):5 result.append(a[i] * b[i])6print('RESULT:', result)values this step[2, 6, 12, 20] → [2, 6, 12, 20, 30]resulti ← 5
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])values this step4 → 5iresult ← [2, 6, 12, 20, 30, 42]
4for i in range(len(a)):5 result.append(a[i] * b[i])6print('RESULT:', result)values this step[2, 6, 12, 20, 30] → [2, 6, 12, 20, 30, 42]resultfor i in range(len(a)):
3result = []4for i in range(len(a)):5 result.append(a[i] * b[i])stdout ← RESULT: [2, 6, 12, 20, 30, 42]
5 result.append(a[i] * b[i])6print('RESULT:', result)values this stepRESULT: [2, 6, 12, 20, 30, 42]stdout
With NumPy
arr_a * arr_b multiplies the two arrays elementwise without an explicit loop,
producing the Hadamard (elementwise) product.
library.py
import numpy as np
a = [1, 2, 3, 4, 5, 6]
b = [2, 3, 4, 5, 6, 7]
arr_a = np.array(a)
arr_b = np.array(b)
result = arr_a * arr_b
print('shape:', result.shape)
print('dtype:', result.dtype)
print('values:', result.tolist())
print('RESULT:', result.tolist())
shape: (6,)
dtype: int64
values: [2, 6, 12, 20, 30, 42]
RESULT: [2, 6, 12, 20, 30, 42]
Implementation notes
- This is the Hadamard product: position
iof the output isa[i] * b[i]. It is NOT the dot product (sum of pairwise products). For dot product usenp.dot(a, b)ora @ b— that scalar output lesson is in the roadmap linear-algebra chapter. - Both arrays must have compatible shapes. The resulting array has the same shape as the inputs; no reduction occurs.
- Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.