factor/extra/tensors/tensor-slice/tensor-slice.factor

26 lines
761 B
Factor
Raw Permalink Normal View History

tensors: create basic tensors vocabulary. tensors: create tensors vocabulary. tensors: create file heading tensors: define tensor constructor. tensors: add additional constructors. tensors: add reshaping. tensors: implement add and include tests. tensors: add binary operations. tensors: add scalar multiply. tensors: added >array functionality tensors: tests for >array tensors: unit tests fix tensors: use more idiomatic >array. tensors: add multi-methods for scalar multiplication. tensors: cleaned up >array tensors: combine a few constructors tensors: added dims function and unit tests. tensors: add documentation capabilities. tensors: added multi-methods for scalar addition/subtraction/division help.lint.coverage: fix for shadowing "empty" word; prevent the other test-only words from being shadowed too soundex: move to extra as it's unused; fix authors.txt filename modify arange to match numpy; replace with naturals create >float-array for efficient float array construction use combinators tensors: documentation added for public functions. tensors: implement t% and matrix multiplication. tensors: add slice with non-zero step tensors: add documentation. tensors: added transposition funcitonality, with documentation and tests tensors: add error documentation. Add error documentation tensors: fix matmul documentation. extra/tensors: add tests for arange tensors: make transpose style more similar tensors: make some of the PR changes. tensors: separate shape checking. tensors: add documentation for non-positive-shape-error. tensors: add missing comment. tensors: transpose edits for efficiency
2019-10-29 13:09:38 -04:00
USING: accessors kernel locals math math.order sequences ;
IN: tensors.tensor-slice
TUPLE: step-slice < slice { step integer read-only } ;
:: <step-slice> ( from to step seq -- step-slice )
step zero? [ "can't be zero" throw ] when
seq length :> len
step 0 > [
from [ 0 ] unless*
to [ len ] unless*
] [
from [ len ] unless*
to [ 0 ] unless*
] if
[ dup 0 < [ len + ] when 0 len clamp ] bi@
! FIXME: make this work with steps
seq dup slice? [ collapse-slice ] when
step step-slice boa ;
M: step-slice virtual@
[ step>> * ] [ from>> + ] [ seq>> ] tri ;
M: step-slice length
[ to>> ] [ from>> - ] [ step>> ] tri
dup 0 < [ [ neg 0 max ] dip neg ] when /mod
zero? [ 1 + ] unless ;