Sampling and refinement

Construct a cache to sample a function without opening a figure. A triangle is incomplete when its sampled values fail the cache’s agreement test. Refinement adds samples to those triangles until the budget or area cutoff stops it.

using AdaptiveVisualization

TC = TriangulationCache((x, y) -> x^2 + y^2; resolution=100)
inserted = refine!(TC; budget=200, min_refinement_area=1e-4)

budget limits new samples; min_refinement_area sets the normalized triangle area cutoff. When both are given, refinement stops when the budget is spent or no eligible points remain. The return value is the number of inserted samples.

Sampling cache

AdaptiveVisualization.TriangulationCache — Type
TriangulationCache(function_oracle; kwargs...)

Sample a function over a rectangular window and store its values and Delaunay triangulation. Use refine! to add samples and visualize to plot the cache. Construction does not open a figure.

function_oracle may be either batched, f(points) -> values, or single-point, f(point) / f(x, y). Batched oracles are preferred and are preserved when detected.

Keyword arguments:

  • xlims, ylims: domain limits, default [-1, 1].
  • resolution: target number of initial sample points, default 1000. The rectangular grid may contain fewer points than this target.
  • strategy: one of :random, :sierpinski, :barycenter; default :sierpinski.
  • min_refinement_area: finite, nonnegative minimum incomplete-triangle area, normalized by the current window area; defaults to 1e-5, and refinement skips triangles at or below this threshold.
  • discrete: true requires exact value agreement and selects categorical plotting; false uses continuous numeric behavior. Default nothing uses the evaluator's default, or automatic detection for ordinary functions. HomotopyContinuation solution counters default to true; an explicit false overrides this. The choice is retained during refinement and navigation.
  • is_complete: default nothing selects the built-in rule; otherwise a custom completeness predicate (vertices, values; kwargs...), where vertices is an NTuple{3,NTuple{2,Float64}} and values contains the three corresponding oracle values. A custom predicate takes precedence over the completeness rule selected by discrete.
  • verbose: whether to print progress, default false.
  • batched: nothing detects the call form, false calls the oracle once per point, and true requires a batched oracle.
  • input_dimension: number of coordinates expected by the function. Supply input_dimension=n to sample a random two-dimensional plane through the origin. Default nothing; the dimension can also be inferred from near or plane_points.
  • near: point through which a slice passes; default nothing.
  • plane_points: three points [p, q, r] specifying a slice; default nothing. When supplied, these override near.
  • zoomer: positive slice scale, default 1.0.
  • rng: random generator for choosing slice directions, default Random.default_rng().

The default completeness rule ignores :wildcard values. Discrete values must agree exactly; continuous real values may differ by up to one sixteenth of the initial sampled range. For ordinary functions with discrete=nothing, samples are classified after removing :wildcard: they are discrete if any value is not a real number or if there are fewer than 50 distinct values. An all-wildcard triangle is considered complete. Agreement at sampled vertices does not guarantee that the function is constant throughout a triangle.

The cache retains samples when the plot window changes, so returning to a previously covered window does not require new evaluations.

parameter_slice stores the affine coordinate map for sliced functions and polynomial systems, or nothing for unsliced functions. Call TC.parameter_slice([u, v]) to map plot coordinates to the original input coordinates. retrieve_witnesses applies this map automatically.

source

Refining a cache

AdaptiveVisualization.refine! — Function
refine!(TC::TriangulationCache; min_refinement_area=nothing, budget=nothing, verbose=TC.verbose)

Refine a TriangulationCache in place and return the number of new sample points inserted as an integer.

By default, this performs one pass over the current incomplete triangles, adding new points according to TC.strategy and skipping triangles whose area is at or below TC.min_refinement_area * window_area.

If budget is supplied, refinement adds up to that many new sample points across repeated passes, stopping early without error if no eligible points remain. Without either keyword, one refinement pass uses TC.oracle_budget as its cap.

If min_refinement_area is supplied, TC.min_refinement_area is updated and refinement repeats until every incomplete triangle in the current window is at or below the resulting area cutoff. Iterative minimum-area refinement requires a positive, finite value. When both keywords are supplied, refinement stops when the budget is spent or no eligible refinement points remain, whichever comes first.

source

Witnesses for discrete values

Retrieve one sampled input for each distinct non-wildcard value. For sliced functions, witnesses use the original input coordinates.

AdaptiveVisualization.retrieve_witnesses — Function
retrieve_witnesses(TC::TriangulationCache)

Return a vector of parameter vectors, one sampled witness for each distinct non-:wildcard value in TC, in the order those values first appear among the cached samples. Uses all cached samples, including outside the current window, without evaluating the oracle again.

For a cache built from a sliced evaluator, witnesses are in the original parameter coordinates. Polynomial-system parameters follow the order of HomotopyContinuation.parameters(F). Unsliced caches return two-dimensional plot coordinates. An all-wildcard cache returns an empty vector.

Throws ArgumentError when the cached values are classified as continuous. Set discrete=true when constructing or visualizing the cache to allow any number of categories. Without an evaluator default or explicit override, classification ignores :wildcard: any non-real value makes the cache discrete; otherwise, fewer than 50 distinct values are required for a discrete cache.

source