Updates at index maps, and whole loops, on Int16Array #
a.modifyAt idx lo hi g is a with g i a[idx i] written at the index idx i, for each i
from lo up to hi - 1, in this order. It is the model of a loop that updates the elements at a
map of indices: a stride a.modifyAt s.idx lo hi g, for a ScalarArray.Stride s, the diagonal
of a matrix, a packed triangle or a permutation. When idx is injective on the range, which
Stride.Fits.idx_inj gives for a stride, the element at idx i is g i a[idx i]
(getElem_modifyAt_idx). The elements at no index of the range are unchanged
(getElem_modifyAt_of_forall_ne). copySlice_eq_modifyAt gives a copy within bounds as a
modifyAt at the indices destOff + i, so that a kernel can copy contiguous data with
copySlice and strided data with a loop, and specify both the same way.
The whole-loop lemmas of InPlace.Tail run a loop in continuation-passing style from any step
j₀, where its one-step equation Tail.forInFrom_eq runs one step:
- by
Tail.run_forInFrom, a loop whose stepjmaps its variable and the array byf jruns asiterRange f, and byTail.run_forInFrom_unit, a loop with no variable whose stepjmaps the array byF jruns asiterRange F. A loop of loops is of this kind, with the inner loops proved by the same lemmas; - by
Tail.run_forInFrom_modify, the loopfor j in lo...hi do a[e j] := g a[e j]of theinplacenotation, where the value of the indexe jisidx j, runs asmodifyAt idx, and byTail.run_forInFrom_write, so doesfor j in lo...hi do a[e j] := g; - the
Tail.run_forInRevFromlemmas do the same for the loops down,for j in reverse lo...hi, withiterRangeRev, and withmodifyAtwhenidxis injective on the range.
Two updates at a time have models too. a.modifyAt2 ia ib lo hi f g reads the elements at ia i
and ib i and writes f i u v and g i u v back, for two index maps of one array, such as two
rows of a matrix, and zipModifyAt a b ia ib lo hi f g does the same on two arrays. When the maps
are injective on the range, and for modifyAt2 never meet, modifyAt2_eq and zipModifyAt_eq
give them as modifyAts, whose lemmas then apply. Tail.run_forInFrom_modify2 runs the loop
for j in lo...hi do let u := a[ea j]; let v := a[eb j]; a[ea j] := …; a[eb j] := … as
modifyAt2.
The strided loops modifyStride, modifyStride2 and zipModifyStride run these models on
strides as single loops, which make each array exclusive once and then store without checks. They
are specified by their models with @[csimp].
Updates at index maps #
The array a with g i a[idx i] written at the index idx i, for each i from lo up to
hi - 1, in this order, where g i sees the earlier writes. Indices out of bounds are skipped.
It is the model of a loop that updates the elements at the indices idx i, such as the steps
s.idx i of a stride s.
Instances For
modifyAt depends only on the indices and the updates in its range.
The element at the index idx i of a step i of the range is g i of the element of a, when
idx is injective on the range.
The step hi of modifyAt can come first instead of last, when idx is injective on the range,
since then no other step reads or writes its element.
Strides #
Copies #
copySlice within bounds writes the elements of src at the indices destOff + i.
Two updates at a time #
Two index maps on one array #
The array a where, for each i from lo up to hi - 1, in this order, u := a[ia i]! and
v := a[ib i]! are read, and then f i u v is written at the index ia i and g i u v at the
index ib i, where each step sees the earlier writes. Indices out of bounds read default and are
not written. It is the model of a loop that updates two strides of one array together, such as a
plane rotation of two rows of a matrix. When the two index maps are injective on the range and
never meet, modifyAt2_eq gives it as two modifyAts.
Instances For
modifyAt2 depends only on the indices and the updates in its range.
When ia and ib are injective on the range and never meet, each step of modifyAt2 reads only
elements that no other step writes, so it is the modifyAt at ia followed by the modifyAt at
ib, both reading the other elements from a.
Two arrays #
The arrays a and b where, for each i from lo up to hi - 1, in this order,
u := a[ia i]! and v := b[ib i]! are read, and then f i u v is written at the index ia i of
a and g i u v at the index ib i of b, where each step sees the earlier writes. Indices out
of bounds read default and are not written. It is the model of a loop that updates two arrays
together, such as a plane rotation of two vectors. When the two index maps are injective on the
range, zipModifyAt_eq gives it as a modifyAt of each array.
Instances For
The step lo of zipModifyAt comes first.
zipModifyAt splits at any mid between its bounds.
zipModifyAt depends only on the indices and the updates in its range.
When ia and ib are injective on the range, each step of zipModifyAt reads only elements that
no other step writes, so it is a modifyAt of each array, reading the elements of the other array
from the arrays it started with.
Strides #
The element at the step i of the first stride of modifyAt2, for disjoint strides.
The element at the step i of the second stride of modifyAt2, for disjoint strides.
The element at the step i of the stride of the first array of zipModifyAt.
The element at the step i of the stride of the second array of zipModifyAt.
Whole loops #
Runs the loop Tail.forInFrom lo hi j₀ hlo init body k whose step j maps its variable b and
the array a by f j.toNat (b, a), before it runs the next step: it maps them by
iterRange f j₀.toNat hi.toNat, and then runs k.
Runs the loop Tail.forInFrom lo hi j₀ hlo () body k with no variable whose step j maps the
array a by F j.toNat, before it runs the next step: it maps the array by
iterRange F j₀.toNat hi.toNat, and then runs k. This is the case of a loop of loops, where F j
is the model of the inner loop.
Runs the loop for j in lo...hi do a[e j] := g a[e j] of the inplace notation, from the step
j₀, where the value of the index e j is idx j.toNat and g may use the bounds of j: it
maps the array by modifyAt idx j₀.toNat hi.toNat G, and then runs k.
Runs the loop for j in lo...hi do a[e j] := g of the inplace notation, from the step j₀,
where the value of the index e j is idx j.toNat and g may use the bounds of j: it maps the
array by modifyAt idx j₀.toNat hi.toNat fun i _ => G i, and then runs k.
Runs the loop for j in lo...hi do let u := a[ea j]; let v := a[eb j]; a[ea j] := f u v; a[eb j] := g u v of the inplace notation, from the step j₀, where the values of the indices
ea j and eb j are ia j.toNat and ib j.toNat, and f and g may use the bounds of j: it
maps the array by modifyAt2 ia ib j₀.toNat hi.toNat F G, and then runs k.
Whole loops down #
Runs the loop Tail.forInRevFrom lo hi j₀ hj₀ init body k, from j₀ - 1 down to lo, whose step
j maps its variable b and the array a by f j.toNat (b, a), before it runs the next step: it
maps them by iterRangeRev f lo.toNat j₀.toNat, and then runs k.
Runs the loop Tail.forInRevFrom lo hi j₀ hj₀ () body k with no variable, from j₀ - 1 down to
lo, whose step j maps the array a by F j.toNat, before it runs the next step: it maps the
array by iterRangeRev F lo.toNat j₀.toNat, and then runs k.
Runs the loop for j in reverse lo...hi do a[e j] := g a[e j] of the inplace notation, from
j₀ - 1 down to lo, where the value of the index e j is idx j.toNat, which is injective on
the range, and g may use the bounds of j: it maps the array by
modifyAt idx lo.toNat j₀.toNat G, and then runs k.
Runs the loop for j in reverse lo...hi do a[e j] := g of the inplace notation, from j₀ - 1
down to lo, where the value of the index e j is idx j.toNat, which is injective on the range,
and g may use the bounds of j: it maps the array by
modifyAt idx lo.toNat j₀.toNat fun i _ => G i, and then runs k.
zipForInFrom runs as zipModifyAt, from its step j₀, when the values of the indices ea j
and eb j are ia j.toNat and ib j.toNat.
Strided loops #
The loops modifyStride, modifyStride2 and zipModifyStride update the elements at the first
n steps of strides. Each makes its arrays exclusive once, before it starts, and then updates them
in place without checks. Each is specified by a modifyAt model, by @[csimp], and runs as the
model when an array has USize.size elements or more, which no array in memory has.
a with f applied to the element at each of the first n steps of the stride s, which fits
in a: the model a.modifyAt s.idx 0 n fun _ => f, run as one loop.
Instances For
For internal use only.
Instances For
a with, at each of the first n steps i of the strides sa and sb, which fit in a,
f u v written at sa.idx i and g u v at sb.idx i, where u and v are the elements there:
the model a.modifyAt2 sa.idx sb.idx 0 n (fun _ => f) (fun _ => g), run as one loop, such as a
plane rotation of two rows of a matrix. modifyAt2_eq gives the elements when the two strides
never meet, which Stride.idx_ne_idx_of_same_inc and Stride.idx_ne_idx_of_lt_off give.
Instances For
The arrays a and b with, at each of the first n steps i of the strides sa, which fits in
a, and sb, which fits in b, f u v written at sa.idx i in a and g u v at sb.idx i
in b, where u and v are the elements there: the model
zipModifyAt a b sa.idx sb.idx 0 n (fun _ => f) (fun _ => g), run as one loop, such as a plane
rotation of two vectors. a and b may be the same array: the model treats them as two arrays,
and so does the loop, which then copies one of them.