Finding a View in Common
The postmark gives me a date, but it doesn’t tell me which corner the photographer stood on. Before setting out to rephotograph a postcard, I make a small inventory of visible landmarks: church tower, bridge, trees. A promising modern view has some of the same ingredients, even if the framing isn’t right yet.
Cosine similarity gives two inventories a score based on their direction as vectors. For each landmark, I write a 1 if it’s visible and a 0 if it isn’t. The postcard has a tower and a bridge; the candidate view has both, plus trees. Their score is about 0.816. A score of 1 would mean the vectors point in the same direction; 0 would mean they share no present landmarks.
(def postcard [1.0 1.0 0.0])
(def candidate [1.0 1.0 1.0])
(defn dot [a b]
(apply + (map * a b)))
(defn magnitude [v]
(Math/sqrt (dot v v)))
(println (/ (dot postcard candidate)
(* (magnitude postcard) (magnitude candidate))))
Clojure’s map multiplies corresponding entries, and dot adds the results. Dividing that dot product by both magnitudes removes the effect of vector length. Cosine similarity became a useful tool in information retrieval in the 1970s, where document vectors represented terms rather than landmarks.
Ada makes the accumulation explicit:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Elementary_Functions;
procedure Views is
use Ada.Numerics.Elementary_Functions;
type Vector is array (1 .. 3) of Float;
Card : constant Vector := (1.0, 1.0, 0.0);
View : constant Vector := (1.0, 1.0, 1.0);
Dot, Card_Sq, View_Sq : Float := 0.0;
begin
for I in Card'Range loop
Dot := Dot + Card (I) * View (I);
Card_Sq := Card_Sq + Card (I) ** 2;
View_Sq := View_Sq + View (I) ** 2;
end loop;
Put_Line (Float'Image (Dot / Sqrt (Card_Sq * View_Sq)));
end Views;
This is a shortlist, not a camera position. The score ignores where landmarks sit in the frame, and a newly planted tree counts just as much as an old bridge. An empty inventory also has zero magnitude, so a practical tool must handle that case before dividing. Once a candidate looks plausible, the final alignment still takes a walk, a careful look and a little patience.