Milestone 2 of 8
Implement and hand-check squared distance
Calculate one-query and all-reference distances without changing the inputs, then reproduce three supplied cases by hand.
Make the distance calculation small enough to inspect by hand before applying it to every reference record.
Goal
Implement squared distance for one query and one reference row, extend it to a vector of reference rows, preserve inputs, and verify three supplied calculations.
Inputs
Use finite two-coordinate query and reference values from the validated data. For one pair, calculate:
(query_value_1 - reference_value_1) ** 2
+ (query_value_2 - reference_value_2) ** 2
The result is non-negative. Do not take a square root or introduce a scaling factor. Keep the query vector and reference matrix in the documented coordinate order.
Deliverables
Implement src/distance.py with a function for one query/reference pair and a
function that returns one distance for every reference row. Record at least
three hand calculations and the complete distance vector for one supplied
query in the report or a stable evidence section.
The functions must return finite values and must not alter either input.
Checks
Check three hand-computed pairs, a query against all reference rows, zero distance for equal coordinates, and the expected output shape. Reject wrong coordinate width, empty input where the interface requires a row, non-finite values, and non-two-dimensional reference arrays.
Check that the input query and reference arrays retain their values and that the vector result's position still corresponds to the original reference row. Compare the compact NumPy result with an expanded calculation for one row.
Workspace
Put the distance boundary in src/distance.py and use validated arrays from
src/data.py. Do not sort or select neighbors in this module. Keep the hand
calculations available for the later report.
Hints
HintSubtract coordinate by coordinate
HintDistance vector order matters
Review
Compare the hand calculations with the function output. Explain why the square root is unnecessary for ordering non-negative distances and why the supplied coordinate units remain part of the project boundary.
How to check your work
Checks compare the distance functions and edge-case behavior with the supplied fixture. The supplied fixture demonstrates the supplied formula without adding a new representation choice.