bhylu.com

Advertisements

NumPy Array Arithmetic

Addition, subtraction, multiplication and division operate elementwise on compatible arrays. Floor division and remainder answer different questions from ordinary division.

How it works

Use multiplication for a per-reading scale adjustment, and division for ratios only after checking the denominator policy. Integer-looking inputs can produce floating results from ordinary division. Matrix multiplication is a separate operation, written with @ or matmul; using * does not perform a matrix product. Keep units in the explanation so an operation is meaningful rather than just syntactically valid.

Run the code

Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.

For local work, use a separate virtual environment and run python arithmetic.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

arithmetic.py

import numpy as np

litres = np.array([12, 18, 24])
print((litres * 2).tolist())
print((litres / 6).tolist())
print(np.floor_divide(litres, 5).tolist())
print(np.remainder(litres, 5).tolist())

Expected result

[24, 36, 48]
[2.0, 3.0, 4.0]
[2, 3, 4]
[2, 3, 4]

The last two results happen to contain the same numbers for this fixture, but one is a quotient and the other a remainder. Change a value to ten and explain why those results differ.

Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.