notes

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit ec5877c504e1fb06063fee43d5c5c8e0729c0e89
parent 5438a7ce3f94ff6b052f7f2b6263beb7100faa9c
Author: ling0x <ling0x@users.noreply.github.com>
Date:   Fri, 14 Aug 2026 19:09:44 +0100

coordinates and distances

Diffstat:
Aalgorithm/cartesian_polar_spehrical.txt | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aalgorithm/euclidean_manhantan_chebyshev.txt | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amathematic/matrix_ode.txt | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 291 insertions(+), 0 deletions(-)

diff --git a/algorithm/cartesian_polar_spehrical.txt b/algorithm/cartesian_polar_spehrical.txt @@ -0,0 +1,90 @@ +Cartesian (x, y), polar (r, θ), spherical (r, θ, φ) + +Coordinate systems: different ways of naming the same point in space. +The point never moves; only the labels change. Pick the system whose +symmetry matches the problem's symmetry. + + +CARTESIAN (x, y) in 2D, (x, y, z) in 3D +---------------------------------------- +Signed distances along fixed perpendicular axes. + + - Every coordinate is a length, all axes are interchangeable. + - Unique: one point <-> exactly one tuple. + - Translation is addition; rotation needs a matrix. + - Straight lines and boxes are trivial; circles and spheres are not + (x^2 + y^2 = r^2 has a square root in it). + +Use when: grids, pixels, arrays, linear algebra, anything axis-aligned. + + +POLAR (r, θ) -- 2D +-------------------- +Distance from origin + angle from the +x axis. + + r >= 0 radius + θ angle, CCW from +x axis, typically (-π, π] or [0, 2π) + + x = r cos θ r = sqrt(x^2 + y^2) + y = r sin θ θ = atan2(y, x) <- atan2, never atan(y/x) + + - NOT unique: θ is mod 2π, and r = 0 leaves θ undefined (the origin is + a singularity). (r, θ) and (r, θ + 2π) are the same point. + - Circles become r = const: one coordinate instead of an equation. + - Rotation is addition on θ; scaling is multiplication on r. + - Area element is r dr dθ, not dr dθ -- the Jacobian matters. + +Use when: rotation, orbits, radar/lidar returns, wave propagation, +anything radially symmetric about a point. + +Cylindrical (r, θ, z) is polar with an untouched z bolted on -- use for +things symmetric about an axis (pipes, wheels, extrusions). + + +SPHERICAL (r, θ, φ) -- 3D +--------------------------- +Distance from origin + two angles. + +WARNING: conventions collide. Two common ones: + + ISO / physics: θ = polar angle from +z axis [0, π] + φ = azimuth in xy-plane [0, 2π) + x = r sin θ cos φ + y = r sin θ sin φ + z = r cos θ + r = sqrt(x^2+y^2+z^2), θ = acos(z/r), φ = atan2(y, x) + + Math / US calc: θ and φ are swapped. + + Geography uses latitude (measured from the equator, not the pole) and + longitude, so lat = 90° - θ_ISO. Always check which one a library means. + + - Singular at r = 0 (both angles undefined) and at the poles + (φ undefined when θ = 0 or π). This is gimbal lock's cousin -- it's + why orientation is stored as quaternions, not Euler angles. + - Volume element is r^2 sin θ dr dθ dφ. + +Use when: point sources, gravity/EM fields, globes, ray directions, +camera look-at angles, spherical harmonics. + + +COMPARISON +---------- + Cartesian Polar / Spherical + coordinates all lengths one length + angles + uniqueness unique not unique (mod 2π; poles degenerate) + origin nothing special singular + translation cheap (add) expensive (round-trip to Cartesian) + rotation matrix multiply cheap (add to angle) + distance Pythagoras law of cosines / haversine + natural shape boxes, lines circles, spheres, cones + interpolation straight lines arcs (and θ must wrap correctly!) + +Practical notes: + - Convert to Cartesian to add vectors; convert back to read off angles. + - Interpolating angles naively goes the wrong way around at the ±π + seam. Use the shortest signed difference: atan2(sin d, cos d). + - Comparing radii? Compare r^2 and skip the sqrt. + - Great-circle distance on a sphere is r * central angle; use the + haversine form, since acos of a dot product loses precision for + nearby points. diff --git a/algorithm/euclidean_manhantan_chebyshev.txt b/algorithm/euclidean_manhantan_chebyshev.txt @@ -0,0 +1,135 @@ +=============================================================================== +Manhattan (L1), Euclidean (L2), Chebyshev (L-inf) +=============================================================================== + +Distance metrics: different answers to "how far apart are two points?" +All three are special cases of the Minkowski distance + + D_p(a, b) = ( sum_i |a_i - b_i|^p )^(1/p) + +with p = 2, p = 1, p = infinity. They all satisfy the metric axioms +(non-negative, zero iff identical, symmetric, triangle inequality), so +any of them is a valid metric for k-NN, clustering, or A* -- they just +disagree about what "close" means. + + +MANHATTAN (L1, taxicab, city block) +------------------------------------ + d = sum_i |a_i - b_i| + 2D: |dx| + |dy| + +Distance walking a street grid: you may only move along axes. + + - Unit ball is a diamond (rotated square). + - NOT rotation-invariant -- rotating the frame changes distances. + - Grows linearly with each difference, so outliers are penalized less + than under L2; more robust. + - No sqrt, no multiplication -> cheap, exact in integers. + - The natural L1 "center" is the coordinate-wise median, not the mean. + - In high dimensions it discriminates better than L2, which suffers + more from distance concentration (all points look equidistant). + +Use when: 4-way grid movement (up/down/left/right, no diagonals), +circuit routing, warehouse/robot paths, lasso-style sparsity, and as the +admissible A* heuristic on a 4-connected grid. + + +EUCLIDEAN (L2, p = 2) +---------------------- + d = sqrt( sum_i (a_i - b_i)^2 ) + 2D: sqrt(dx^2 + dy^2) + +Straight-line "as the crow flies" distance. The one everyone means by +default. + + - Rotation-invariant: the only one of the three that is. Rotating the + coordinate frame doesn't change distances. + - Unit ball is a circle / sphere. + - Smooth and differentiable away from zero -> gradient descent likes it. + - Squares the differences, so large deviations dominate; sensitive to + outliers. + - Costs a sqrt. For ranking/comparison, use squared distance instead -- + monotonic, so it gives the same ordering, and it's exact in integers. + +Use when: real physical space, free movement in any direction, least +squares, k-means (whose mean-as-centroid step assumes L2). + + +CHEBYSHEV (L-inf, chessboard) +------------------------------ + d = max_i |a_i - b_i| + 2D: max(|dx|, |dy|) + +Only the single largest coordinate difference counts -- the others come +along free. + + - Unit ball is an axis-aligned square / cube. + - Number of king moves on a chessboard: a king covers one step in x + and one in y simultaneously, so the diagonal is free. + - Also the right model for a machine whose axes move in parallel at + equal speed (a CNC/plotter's travel time is the slowest axis). + - Cheapest of the three to compute. + +Use when: 8-way grid movement with uniform cost, tolerance checks +("every component within eps"), max-error bounds, A* heuristic on an +8-connected grid. + + +COMPARISON +---------- +Take dx = 3, dy = 4: + + Euclidean sqrt(9 + 16) = 5 + Manhattan 3 + 4 = 7 + Chebyshev max(3, 4) = 4 + +Ordering always holds (in n dimensions): + + L-inf <= L2 <= L1 <= n * L-inf + +so Chebyshev is the loosest bound and Manhattan the tightest -- which is +exactly why the choice of A* heuristic matters: all three are admissible +on a free-movement grid, but the largest admissible one expands the +fewest nodes. + + Euclidean Manhattan Chebyshev + unit ball circle diamond square + rotation-inv. yes no no + cost sqrt adds max + outliers sensitive robust only max matters + center statistic mean median midrange + grid movement any angle 4-way 8-way + diagonal step sqrt(2) 2 1 + +Relations worth knowing: + - In 2D, rotating 45° and scaling maps L1 <-> L-inf: + (x, y) -> (x + y, x - y) turns Manhattan distance into Chebyshev. + Handy for turning diagonal-movement problems into axis-aligned ones. + - Diagonal / octile distance is the honest 8-way grid metric when a + diagonal step costs sqrt(2) instead of 1: + d = (dx + dy) + (sqrt(2) - 2) * min(dx, dy) + It sits between Chebyshev and Manhattan; use it, not Chebyshev, when + diagonals aren't free. + - As p -> infinity the Minkowski ball inflates from a diamond (p=1) to + a circle (p=2) to a square (p=inf). p < 1 is not a metric -- it + violates the triangle inequality. + + +PICKING ONE +----------- + Movement is unconstrained and physical -> Euclidean + Movement is axis-locked / grid, no diagonal -> Manhattan + Diagonals cost the same as straight steps -> Chebyshev + Diagonals cost sqrt(2) -> octile + High-dimensional feature vectors -> Manhattan often better + Outliers in the data -> Manhattan + You only need an ordering, not a value -> squared Euclidean + + +HOW THIS RELATES TO COORDINATE SYSTEMS +-------------------------------------- +See cartesian_polar_spehrical.txt. Coordinate systems change how a point +is *named*; metrics change how far apart two points *are*. The two +interact: L2 is the only one of these metrics invariant to the choice of +(rotated) Cartesian frame, which is why polar/spherical conversions +preserve Euclidean distance but scramble Manhattan and Chebyshev. diff --git a/mathematic/matrix_ode.txt b/mathematic/matrix_ode.txt @@ -0,0 +1,65 @@ +========================================================================== +Systems of ODEs written as a matrix product +========================================================================== + +It's shorthand. Nothing more. + +Say you're tracking two things that affect each other — rabbits and foxes, + or two temperatures, whatever. How fast each one changes depends on both + current values: + +rate of change of x = 2x + 1y +rate of change of y = -1x + 3y + +That's it. That's the actual system. Now look at just the numbers, pulled + out of those two lines: + + 2 1 +-1 3 + +Writing x' = Ax means exactly the same thing as those two lines above. +A is that grid of numbers, and the "multiplication" is the rule that puts + them back together: take a row, pair it up with the variables, multiply + and add. + +So the "multiplication" is just the recipe for turning that grid back into + the equations. Slide across a row, multiply each number by its matching + variable, add them up. That's the whole operation. + +-------------------------------------------------------------------------- + +Why anyone bothers + +Two reasons. + +It's compact. With 2 variables, writing it out is fine. With 50, it's + unbearable. x' = Ax stays the same length no matter how many variables + you have. + +It unlocks tools. Once it's a matrix, you can ask questions of A itself — + questions you can't easily ask of a pile of equations. The big one: will + this system settle down or blow up? There's a standard calculation on +A (finding its eigenvalues) that answers this without ever solving for +x and y. That's the real payoff. + +Concrete numbers, to make sure it's landed + +Say right now x = 5 and y = 2. Feed them in: + +x' = 2(5) + 1(2) = 12 — so x is currently climbing at 12 units per second +y' = -1(5) + 3(2) = 1 — y is barely moving + +Notice y' came out small because the −1 and the +3 nearly cancelled. +That's the coupling doing its thing: x is dragging y down while y +pushes itself up. + +-------------------------------------------------------------------------- + +The one-sentence version + +A matrix is a table of "how much does each variable affect each rate", +and matrix multiplication is the lookup procedure that reads the table. + +Does that land better? If so, I can go one step further into the + eigenvalue part — which is where it actually starts being useful rather + than just tidy. +\ No newline at end of file