shaped-slice-view ( array selectors -- view )
NumPy-style shaped-array operations

Prev:reshape ( array shape -- result )
Next:shaped-permute ( array axes -- view )


Vocabulary
arrays.shaped

Inputs
arraya shaped-array, a sequence, or a number
selectorsa sequence


Outputs
viewa shaped-array


Word description
Returns a writable view using one integer, shaped-slice, or f selector per selected axis. Integers remove an axis; f and omitted trailing selectors retain the whole axis. Negative indices and slice steps are supported. Writes through the view affect the source storage and vice versa. Nested ordinary sequences are first converted to owned flat storage. Advanced indexing, ellipsis, and new-axis selectors are not implemented.

Examples
USING: arrays arrays.shaped prettyprint ; { 0 1 2 3 4 } f f -1 <shaped-slice> 1array shaped-slice-view shaped-array>array .
{ 4 3 2 1 0 }


Definition


:: shaped-slice-view ( array selectors -- view )
array >shaped-array :> shaped shaped shape>> :> dimensions
selectors length dimensions length >
[ selectors invalid-shaped-slice ] when
selectors dimensions length f pad-tail
:> selection dimensions row-major-strides :> strides
V{ } clone :> result-shape
V{ } clone :> result-strides 0 :> offset! selection [|
selector axis |
axis dimensions nth :> size axis strides nth
:> stride selector integer? [
selector size normalize-axis stride * offset +
offset!
] [
selector [ ] [ f f 1 <shaped-slice> ] if*
:> range range shaped-slice?
[ selector invalid-shaped-slice ] unless
range size slice-parameters :> ( start count step )
offset start stride * +
offset! count result-shape push stride step *
result-strides push
] if
] each-index
shaped underlying>> result-shape >array result-strides
>array offset <shaped-view> ;