src/graph.bend checks
raw source on the hub · import bend-visgraph@1.0.1.0/src/graph.bend as Graph
Natural and horizontal visibility graphs, as ts2vg 1.2.4 builds them, in F32. Each def below that has a ts2vg twin does the same F32 operations in the same order as the twin does in float64.
1 import
import Base
Types
type Graph source · line 6 · raw
Data
NaturalGraph
HorizontalGraph
type Direction source · line 10 · raw
Data
UndirectedDirection
LeftToRightDirection
TopToBottomDirection
type Weight source · line 15 · raw
Data
UnweightedWeight
DistanceWeight
SqDistanceWeight
VDistanceWeight
AbsVDistanceWeight
HDistanceWeight
AbsHDistanceWeight
SlopeWeight
AbsSlopeWeight
AngleWeight
AbsAngleWeight
NumPenetrationsWeight
type Xs source · line 31 · raw
Type
The x coordinate of each value: its index, or the value at the same index of an array.
IndexXs
At@xs:Array<F32> -> Xs
type Edges source · line 37 · raw
Data
The edges, as a rope: ECon adds an edge and EJoin joins two ropes, so the two halves of a split join in one step.
ENilEdges
ECon@src:U32 -> @dst:U32 -> @w:F32 -> @next:Edges -> Edges
EJoin@a:Edges -> @b:Edges -> Edges
type Stack source · line 186 · raw
Data
The stack holds the points no later point has beaten yet. A point pops the entries it beats; the entry left on top bounds its interval on the left, and the point that pops an entry bounds that entry's interval on the right.
KNilStack
KCon@i:U32 -> @key:F32 -> @next:Stack -> Stack
type Popped source · line 199 · raw
Type
The work only balances the split, so F32 rounding does no harm.
Popped@first:U32 -> @below:F32 -> @st:Stack -> @rights:Array<U32> -> @visits:F32 -> Popped
type Cols source · line 222 · raw
Type
lefts and rights count the points each point's left and right sweeps visit.
Cols@ys:Array<F32> -> @lefts:Array<U32> -> @rights:Array<U32> -> Cols
type Arrived source · line 225 · raw
Type
OpensWithNaN@rights:Array<U32> -> @visits:F32 -> Arrived
PopsAll@rights:Array<U32> -> @visits:F32 -> Arrived
Bounded@first:U32 -> @below:F32 -> @st:Stack -> @rights:Array<U32> -> @visits:F32 -> Arrived
type Unbounded source · line 253 · raw
Data
Latest first.
UNilUnbounded
UCon@i:U32 -> @y:F32 -> @next:Unbounded -> Unbounded
type Block source · line 259 · raw
Data
A block of points runs the stack on its own points. Its points that pop its whole stack, and the NaNs that open it, need the points before it.
Block@start:U32 -> @opening_nans:U32 -> @unbounded:Unbounded -> @st:Stack -> @visits:F32 -> Block
type Local source · line 262 · raw
Type
Local@b:Block -> @c:Cols -> Local
type Merged source · line 300 · raw
Type
Merged@st:Stack -> @lefts:Array<U32> -> @rights:Array<U32> -> @visits:F32 -> Merged
type Walked source · line 366 · raw
Type
Bend matches only parameters, so a read's result must arrive as one: a
sweep reads y and x at ahead, where the next sweep starts, and hands them
over. The sweeps read x through x_at, so x as the index costs no read.
Walked@yr:Pair(Array<F32>, F32) -> @xr:Pair(Array<F32>, F32) -> @es:Edges -> Walked
type Out source · line 457 · raw
Type
Out@cols:Cols -> @xs:Array<F32> -> @es:Edges -> Out
Definitions
def nan source · line 46 · raw
F32
def inf source · line 49 · raw
F32
def neg_inf source · line 52 · raw
F32
def is_nan source · line 55 · raw
@+a:F32 -> Bool
def pick source · line 58 · raw
@c:Bool -> @a:F32 -> @b:F32 -> F32
def pick_u32 source · line 65 · raw
@c:Bool -> @a:U32 -> @b:U32 -> U32
def sq_distance source · line 75 · raw
@+x1:F32 -> @+x2:F32 -> @+y1:F32 -> @+y2:F32 -> F32
def known_slope source · line 78 · raw
@+s:F32 -> @+x1:F32 -> @+x2:F32 -> @+y1:F32 -> @+y2:F32 -> F32
def weight source · line 81 · raw
@+k:Weight -> @+x1:F32 -> @+x2:F32 -> @+y1:F32 -> @+y2:F32 -> @+s:F32 -> F32
def put source · line 111 · raw
@drop:Bool -> @i1:U32 -> @i2:U32 -> @w:F32 -> @es:Edges -> Edges
def add_edge source · line 118 · raw
@+k:Weight -> @+lo:F32 -> @+hi:F32 -> @i1:U32 -> @i2:U32 -> @+x1:F32 -> @+x2:F32 -> @+y1:F32 -> @+y2:F32 -> @+s:F32 -> @es:Edges -> Edges
def add_right source · line 123 · raw
@seen:Bool -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @ia:U32 -> @ib:U32 -> @xa:F32 -> @xb:F32 -> @ya:F32 -> @yb:F32 -> @s:F32 -> @es:Edges -> Edges
A visible point right of the pivot a: the edge (a, b).
def add_left_dir source · line 132 · raw
@dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @ia:U32 -> @ib:U32 -> @xa:F32 -> @xb:F32 -> @ya:F32 -> @yb:F32 -> @s:F32 -> @es:Edges -> Edges
A visible point left of the pivot a: the edge (a, b) for top_to_bottom, else (b, a).
def add_left source · line 139 · raw
@seen:Bool -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @ia:U32 -> @ib:U32 -> @xa:F32 -> @xb:F32 -> @ya:F32 -> @yb:F32 -> @s:F32 -> @es:Edges -> Edges
def abs_tol source · line 148 · raw
F32
def rel_tol source · line 151 · raw
F32
def scale source · line 155 · raw
@+xa:F32 -> @+ya:F32 -> F32
max(|xa|, |ya|), which a sweep computes once.
def tol source · line 161 · raw
@+scale_a:F32 -> @+xb:F32 -> @+yb:F32 -> F32
ts2vg's tolerance for the points a and b. F32.max only picks one of its words, so grouping the four maxima this way gives ts2vg's word.
def greater source · line 164 · raw
@+a:F32 -> @+b:F32 -> @+t:F32 -> Bool
def clears source · line 170 · raw
@up:Bool -> @+s:F32 -> @+best:F32 -> @+scale_a:F32 -> @+xb:F32 -> @+yb:F32 -> Bool
greater(s, best, tol(scale_a, xb, yb)), with the tolerance computed only when s is above best: the tolerance is positive, so otherwise it fails anyway.
def key source · line 195 · raw
@+y:F32 -> @+below:F32 -> F32
What a later point must beat to pop y. An equal point beats nothing, so an interval's first highest point is its pivot. A NaN beats nothing either, and ts2vg's argmax takes it only as an interval's first point, so a NaN at i is the pivot of the right side of i - 1: it takes the key of the entry below it and leaves the stack with that entry.
def pops source · line 202 · raw
@rest:Stack -> @beat:Bool -> @+y:F32 -> @+i:U32 -> @+j:U32 -> @+kj:F32 -> @rights:Array<U32> -> @+visits:F32 -> Popped
def flush source · line 213 · raw
@st:Stack -> @+n:U32 -> @rights:Array<U32> -> @+visits:F32 -> Pair(Array<U32>, F32)
def lpops source · line 230 · raw
@rest:Stack -> @beat:Bool -> @+y:F32 -> @+i:U32 -> @+j:U32 -> @+kj:F32 -> @rights:Array<U32> -> @+visits:F32 -> Arrived
def arrive source · line 241 · raw
@st:Stack -> @nan:Bool -> @+y:F32 -> @+i:U32 -> @rights:Array<U32> -> @+visits:F32 -> Arrived
def local source · line 265 · raw
@count:Nat -> @+i:U32 -> @+y:F32 -> @a:Arrived -> @yr:Pair(Array<F32>, F32) -> @lefts:Array<U32> -> @+start:U32 -> @+opening_nans:U32 -> @unbounded:Unbounded -> Local
def block_pass source · line 291 · raw
@count:Nat -> @+s:U32 -> @yr:Pair(Array<F32>, F32) -> @lefts:Array<U32> -> @rights:Array<U32> -> Local
def push_with_key source · line 303 · raw
@count:Nat -> @+i:U32 -> @+k:F32 -> @st:Stack -> Stack
def push_opening_nans source · line 310 · raw
@count:Nat -> @+i:U32 -> @st:Stack -> Stack
def pops_from source · line 317 · raw
@st:Stack -> @+y:F32 -> @+i:U32 -> @rights:Array<U32> -> @+visits:F32 -> Popped
def set_left source · line 324 · raw
@+i:U32 -> @lefts:Array<U32> -> @p:Popped -> Merged
def in_order source · line 329 · raw
@u:Unbounded -> @acc:Unbounded -> Unbounded
def bound_unbounded source · line 336 · raw
@u:Unbounded -> @m:Merged -> Merged
def onto source · line 344 · raw
@st:Stack -> @acc:Stack -> Stack
def push_block_stack source · line 351 · raw
@st:Stack -> @m:Merged -> Merged
def merge_block source · line 358 · raw
@b:Block -> @m:Merged -> Merged
The merge replays the stack over every point before the block. The points the block's own stack could not bound pop it in order; the others sit on the block's stack, which goes on top.
def at_index source · line 369 · raw
@xs:Array<F32> -> @+j:U32 -> Pair(Array<F32>, F32)
def right_sweep source · line 427 · raw
@g:Graph -> @index:Bool -> @count:Nat -> @yr:Pair(Array<F32>, F32) -> @xs:Array<F32> -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+ia:U32 -> @+xa:F32 -> @+ya:F32 -> @es:Edges -> Walked
Each sweep, with x as the index (index) or read from xs.
def left_sweep source · line 438 · raw
@g:Graph -> @index:Bool -> @count:Nat -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+ia:U32 -> @+xa:F32 -> @+ya:F32 -> @w:Walked -> Walked
def points source · line 460 · raw
@count:Nat -> @+i:U32 -> @lr:Pair(Array<U32>, U32) -> @rr:Pair(Array<U32>, U32) -> @w:Walked -> @+index:Bool -> @+g:Graph -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> Out
def edges_of source · line 479 · raw
@o:Out -> Edges
def swept source · line 483 · raw
@+index:Bool -> @+g:Graph -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+n:U32 -> @ys:Array<F32> -> @xs:Array<F32> -> @lefts:Array<U32> -> @r:Pair(Array<U32>, F32) -> Edges
def bounded source · line 487 · raw
@+index:Bool -> @+g:Graph -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+n:U32 -> @ys:Array<F32> -> @xs:Array<F32> -> @m:Merged -> Edges
def stacked source · line 491 · raw
@+index:Bool -> @+g:Graph -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+n:U32 -> @xs:Array<F32> -> @l:Local -> Edges
def counts source · line 496 · raw
@+n:U32 -> Array<U32>
def no_xs source · line 500 · raw
Array<F32>
A stand-in for xs when x is the index, which no sweep reads.
def built source · line 503 · raw
@+index:Bool -> @+g:Graph -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @ys:Array<F32> -> @xs:Array<F32> -> @+n:U32 -> Edges
def build source · line 512 · raw
@+g:Graph -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @ys:Array<F32> -> @xs:Xs -> @+n:U32 -> Edges
The graph of the n values ys at xs, on one thread, as one block.
lo and hi bound the weights, as ts2vg's min_weight and max_weight;
pass neg_inf() and inf() for no bound. The x of Index{} is the index as
an F32, exact for up to 2^24 values; the x of At{xs} must increase.
src/parallel.bend builds the same graph on every thread, and relies on
Base's @unsafe Array.fork.
Templates
template natural_right source · line 372 · raw
@-x_at:(@_:Array<F32> -> @_:U32 -> Pair(Array<F32>, F32)) -> @count:Nat -> @+j:U32 -> @yr:Pair(Array<F32>, F32) -> @xr:Pair(Array<F32>, F32) -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+ia:U32 -> @+xa:F32 -> @+ya:F32 -> @+scale_a:F32 -> @+best:F32 -> @es:Edges -> @+ahead:U32 -> Walked
template natural_left source · line 386 · raw
@-x_at:(@_:Array<F32> -> @_:U32 -> Pair(Array<F32>, F32)) -> @count:Nat -> @+j:U32 -> @yr:Pair(Array<F32>, F32) -> @xr:Pair(Array<F32>, F32) -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+ia:U32 -> @+xa:F32 -> @+ya:F32 -> @+scale_a:F32 -> @+best:F32 -> @es:Edges -> @+ahead:U32 -> Walked
template horizontal_right source · line 400 · raw
@-x_at:(@_:Array<F32> -> @_:U32 -> Pair(Array<F32>, F32)) -> @count:Nat -> @+j:U32 -> @yr:Pair(Array<F32>, F32) -> @xr:Pair(Array<F32>, F32) -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+ia:U32 -> @+xa:F32 -> @+ya:F32 -> @+best:F32 -> @es:Edges -> @+ahead:U32 -> Walked
template horizontal_left source · line 413 · raw
@-x_at:(@_:Array<F32> -> @_:U32 -> Pair(Array<F32>, F32)) -> @count:Nat -> @+j:U32 -> @yr:Pair(Array<F32>, F32) -> @xr:Pair(Array<F32>, F32) -> @+dir:Direction -> @+k:Weight -> @+lo:F32 -> @+hi:F32 -> @+ia:U32 -> @+xa:F32 -> @+ya:F32 -> @+best:F32 -> @es:Edges -> @+ahead:U32 -> Walked