Fermat distances and their supporting graphs on surfaces

Fermat distances allow paths through every observation. A symmetric nearest-neighbor graph is an optional restriction, not part of the complete-graph estimator. This example asks how that restriction changes distances on an area-uniform paraboloid, an area-uniform saddle, and an arc-length-uniform helix. The companion grip vignette focuses on embedding the same distances into three dimensions.

This vignette renders the bundled October 1, 2026 results for sample seed 4101: 150 observations per shape and 1,000 evaluated pairs per shape. All tables and the figures use these saved results. Building this document does not run sampling, distance construction, reference-path searches or MDS optimization. The teaching code below is shown without execution. The original fits used SGD and stopped at 300 epochs; that budget is not a convergence certificate.

Generate the observations

The quadratic surfaces are \((u,v,u^2+v^2)\) (index zero) and \((u,v,u^2-v^2)\) (index one), over the unit disk. Index counts negative eigenvalues of the quadratic form. We reuse the existing dgraphs area sampler:

library(dgraphs)
geometry <- synthetic.quadform(2, 3, forms=list(diag(c(1,-1))))
sampling <- synthetic.sampling.quadform.lab('disk',extent=1,mode='area')
sample <- sample.synthetic.geometry(geometry,sampling,n=150,seed=4101)
X <- sample$predictors

The sampler weights parameter-space proposals by \(\sqrt{1+\|2Au\|^2}\), the area element for \((u,u^TAu)\), where \(u\) is a two-dimensional parameter and \(A\) is its quadratic-form matrix. This is uniform on surface area, not on the parameter disk. The helix is \((\cos t,\sin t,0.2t)\) for \(0\leq t\leq4\pi\); uniform \(t\) is uniform arc length because its speed is the constant \(\sqrt{1+0.2^2}\).

Distances and the path graph

For observation indices \(i,j\) and exponent \(p\geq1\), we compute \[ D_p(i,j)=\min_{\pi:i\leadsto j}\sum_{(a,b)\in\pi}\|X_a-X_b\|^p. \] The minimum ranges over paths through the observed points, including direct edges. The distance is unrooted, so its units are length to power \(p\).

result <- fermat.distances(points=X,p=2,backend='implicit',rooted=FALSE,
                          return.graph=TRUE)
D <- result$distances
graph <- result$graph
head(graph.edges(graph))
result$metadata

The returned graph is the union of one selected shortest-path tree per source. Its edges carry the powered ambient step lengths. With every source requested, its shortest-path distances preserve the full matrix. With only landmark sources, only landmark-to-sample preservation is guaranteed. Ties may admit other valid unions, and the union need not be sparse: at \(p=1\), direct Euclidean edges usually remain necessary.

A Fermat symmetric kNN graph has different edges: they summarize Fermat paths to the \(k\) nearest neighbors, then take the union of directed neighbor selections.

g <- create.fermat.sknn.graph(X,k=5,p=2,rooted=FALSE)

Compare with surface geodesics

The shared example uses powers 1–4 and k values 3–10. Numerical references on 1,000 reproducibly selected pairs use best_of_six in the existing quadform_geodesics() interface on both surfaces. The paraboloid also uses paraboloid_clairaut on the same 1,000 pairs. Best-of-six selects the shortest usable result from six refinement searches, each with 64 rounds. These are numerical references, not certified globally shortest paths. Helix references are exact arc lengths \(\sqrt{1.04}|t_i-t_j|\).

We compare estimates \(d_r\) with reference distances \(g_r\) on valid pairs \(r\): \[ a=\frac{\sum_r d_rg_r}{\sum_r d_r^2},\qquad E=\sqrt{\frac{\sum_r(ad_r-g_r)^2}{\sum_r g_r^2}}. \] The fitted global scale \(a\) makes powers comparable; \(E=0\) means proportional agreement. It does not establish equality in physical units. Spearman correlation measures rank agreement. For graph restriction error we compare graph distances with \(D_p\) using the same formula with \(a=1\), on all pairs in the largest component. Disconnected graphs are not repaired, and retained sample counts are reported. If a retained component contains no reference pair, its geodesic error is undefined and omitted from the corresponding curve.

Example p Samples Scale Scaled error Rank correlation
paraboloid 1 150 1.160 0.087 0.997
paraboloid 2 150 4.341 0.121 0.966
paraboloid 3 150 21.393 0.199 0.909
paraboloid 4 150 101.281 0.257 0.856
saddle 1 150 1.084 0.092 0.981
saddle 2 150 4.123 0.129 0.954
saddle 3 150 20.101 0.169 0.926
saddle 4 150 91.002 0.225 0.877
helix 1 150 2.597 0.429 0.604
helix 2 150 6.164 0.092 0.986
helix 3 150 24.787 0.212 0.949
helix 4 150 74.758 0.316 0.927

Table 1. Complete-graph Fermat distances compared with the surface or curve reference. Each row uses the same reference pairs within its example; these are finite-sample comparisons, not estimates of an asymptotic convergence rate.

Figure 1. Distance change caused by retaining only symmetric Fermat kNN edges. Zero means that the restricted graph preserves the dense Fermat distances on the retained component. The corresponding component counts and retained sample counts are available below; a small error on a small component would not demonstrate whole-sample preservation.

Example p Retained samples: min–max Components: min–max
helix 1 31–95 2–10
saddle 1 139–139 2–2
helix 2 28–83 3–16
paraboloid 2 66–66 8–8
saddle 2 100–100 4–4
helix 3 19–51 4–18
paraboloid 3 27–127 2–13
saddle 3 46–136 2–10
helix 4 14–51 4–21
paraboloid 4 27–127 2–15
saddle 4 33–136 2–12

The table summarizes ranges among disconnected k values within each example and power. Complete coverage is 150 samples; restricting a disconnected graph can leave far fewer samples even when its within-component distance error is small.

Inspect supporting edges in three dimensions

The following view uses dense-target SGD metric MDS from grip. The gray patch is the underlying surface; colored points are the embedding after one global least-squares similarity Procrustes alignment for display, and thin blue segments are supporting path edges. Gray points identify the original observations. Plot titles report the root mean squared Euclidean alignment residual per retained sample, in target-coordinate units; zero means exact overlap. Similarity alignment uses translation, rotation/reflection and uniform scale, not a deformation of the fit.

Saved saddle observations, aligned MDS coordinates and Fermat path edges shown in an orthographic view with equal coordinate units.

Figure 2. Saddle embedding and complete-graph Fermat supporting paths. The installed guide shows a static projection; interactive builds can be rotated. The legend states how many edges are shown; a deterministic display subset is used above 1,800 edges. The saved graph contains every selected edge.

Intrinsic distances measure travel along a surface. Euclidean distances in a three-dimensional embedding measure straight chords, so geometric recovery of the original surface is not implied by a good distance fit. In particular, exact helix arc lengths embed on a straight line. See the grip companion for the full embedding comparison and additional overlays.

Chord lengths before and after scaling

At p=1, complete-graph Fermat distances equal ambient Euclidean chord lengths. A 3D MDS fit can therefore reproduce the sampled shape without reproducing its intrinsic distances. The earlier distance score fits a global scale; it is not the discrepancy of the original chord lengths.

For each of the same evaluated pairs, write \(c_r\) for the chord and \(g_r\) for the intrinsic reference. The following table reports \(E(c,g;1)\) and \(E(c,g;a)\) with \(a=\sum c_rg_r/\sum c_r^2\), together with the median and maximum ratio \(g_r/c_r\). Errors are dimensionless and zero means agreement. The ratio is one for equal lengths; 1.3 means the geodesic is 30% longer than its chord. These are the 1,000 evaluated pairs for seed 4101, not all sample pairs.

Example Pairs Scale Raw error Scaled error Median ratio Maximum ratio
paraboloid 1000 1.1599 0.1627 0.0873 1.0618 1.4006
saddle 1000 1.0845 0.1206 0.0924 1.0158 1.4526
helix 1000 2.5970 0.7019 0.4292 1.6880 5.0989

Scaling reduces chord underestimation on these patches, but does not make chords intrinsic distances. Larger discrepancies remain for individual endpoint pairs. On the helix, accurately preserving arc lengths unfolds the curve into a line; matching the original coil is a different objective.

Independent solver checks are attempts, not guaranteed successes. Candidate lengths and close agreement where both solvers succeed do not certify global minimality. The table below makes the successful comparison count explicit. The saddle check covers the original 80 pairs, not all 1,000 evaluation pairs.

Example Attempted Finite Failed Check
paraboloid 1000 1000 0 Clairaut (same 1,000 pairs)
saddle 80 80 0 Original shooting check (80 pairs)
helix 1000 1000 0 Analytic identity

Paraboloid reference-method comparison

Best-of-six and Clairaut evaluate the same 1,000 pairs. Positive signed relative difference \((b_r-h_r)/h_r\) means the best-of-six candidate \(b_r\) is longer than the Clairaut candidate \(h_r\). Neither is assumed globally certified.

Pairs Median.signed Maximum.absolute
1000 0.001121 0.012262
p Best of six Clairaut
1 0.08734 0.08533
2 0.12111 0.12060
3 0.19894 0.19838
4 0.25699 0.25640

The second table compares scaled Fermat-distance errors under each reference. It uses identical targets and endpoint pairs, isolating reference-method effects.

Follow-up: sampling density, intermediate powers and stronger curvature

The dedicated package article Fermat distances: sampling density, curvature, and geodesic recovery develops this comparison with the full sample-size curves, coverage, and reference-solver checks.

A separate distance-only experiment varied total sample size from 150 to 2,400, with five repetitions, on paraboloids and saddles with coefficients 1, 2 and 4, and an area-uniform Swiss roll. It reused the existing quadform area sampler. For each surface, 64 fixed sampled endpoints supplied 500 calibration pairs and 1,000 different evaluation pairs. Additional path-support points varied across repetitions and were nested across sample sizes. All observations could be used by the complete-graph paths; this was not a landmark approximation.

Errors below are percentages: the square root of the ratio of summed squared distance residuals to summed squared reference distances. Each method’s single multiplicative scale was fitted on calibration pairs only. Values are means over five repetitions conditional on the fixed endpoints; they are new measurements, not replacements for the original 150-point fits above. No MDS error enters them.

Surface Power p Error at n=150 (%) Error at n=2,400 (%) Ambient error (%)
Paraboloid, coefficient 1 1.25 3.73 1.82 8.51
Paraboloid, coefficient 1 2 11.72 5.67 8.51
Saddle, coefficient 1 1.25 4.55 1.75 8.74
Saddle, coefficient 1 2 14.27 5.58 8.74
Saddle, coefficient 4 1.5 14.82 2.55 28.32
Swiss roll 3 42.33 7.70 55.84

Ambient error is constant because the endpoints are fixed. These results expose both a sample-size effect and a limitation of the original integer-only power grid: p=1.25 already beats ambient chords on the new mild-surface samples at n=150. Larger powers are needed to suppress shortcuts on the rolled surface. The displayed powers illustrate these effects; they are not universal tuning recommendations.

Ordinary Euclidean kNN graph distances were more accurate on these uniform examples at the larger sample size. For k=20 and n=2,400, errors were 0.62%, 0.49% and 0.35% on the mild paraboloid, mild saddle and Swiss roll. This separates recovering ordinary geodesics from deliberately imposing a density-weighted geometry.

References used best-of-six on quadforms and analytic distances on the roll, with Clairaut sensitivity checks on every paraboloid pair. For the steepest paraboloid, p=1.25 at n=2,400 had 4.82% error against best-of-six but 2.05% against Clairaut. Reference-method disagreement therefore matters for this harder surface. Increasing the quadform coefficient changes its curvature profile and area; it does not multiply curvature uniformly over the patch. The complete experiment, pair definitions, coverage and uncertainty are recorded in the research experiment fermat_density_curvature_01_oct_2026.

Saved evidence and rebuilding

The installed extdata/fermat-surfaces/snapshot.rds contains the 108 seed-4101 conditions, chord summaries, independent-reference counts, 1,000 paraboloid solver comparisons and the saved saddle Plotly traces and layout. Its provenance records the source-file checksums, evaluation date and original solver budget. The separate density-and-curvature follow-up above retains its original results.

snapshot$provenance[c('experiment','results_date','sample_seed',
                      'samples_per_shape','reference_pairs_per_shape',
                      'mds_backend','mds_epochs')]
#> $experiment
#> [1] "fermat_surfaces_best_of_six_01_oct_2026"
#> 
#> $results_date
#> [1] "2026-10-01"
#> 
#> $sample_seed
#> [1] 4101
#> 
#> $samples_per_shape
#> [1] 150
#> 
#> $reference_pairs_per_shape
#> [1] 1000
#> 
#> $mds_backend
#> [1] "sgd"
#> 
#> $mds_epochs
#> [1] 300

The rendering helper only reads and validates this snapshot, formats its tables, plots saved error values and displays saved widget data. It does not call the experiment helper in inst/examples/fermat-surfaces.R. Missing or invalid saved results stop the build rather than launching experiments or silently omitting calculated sections. The historical FERMAT_SURFACE_RESULTS and FERMAT_SURFACE_HELPER environment variables are not used by vignette builds. The sampling and graph-construction snippets remain teaching examples; their original graph-object console output is not reconstructed from missing caches.

For maintainers, dev/import-fermat-surfaces-results.R imports a completed best-of-six experiment into the bundled snapshot; it never runs the experiment. make preview-fermat-surfaces validates the saved evidence and renders this vignette. Normal package and pkgdown builds use the same maintained source and bundled snapshot. Set DGRAPHS_STATIC_VIGNETTES=false to include the saved interactive figure with Plotly; the default static projection uses the same coordinates and paths. Grip and numerical experiment caches are not needed.