]> gitweb.factorcode.org Git - factor.git/blob - extra/tensors/tensor-slice/tensor-slice.factor
tensors: create basic tensors vocabulary.
[factor.git] / extra / tensors / tensor-slice / tensor-slice.factor
1 USING: accessors kernel locals math math.order sequences ;
2 IN: tensors.tensor-slice
3
4 TUPLE: step-slice < slice { step integer read-only } ;
5 :: <step-slice> ( from to step seq -- step-slice )
6     step zero? [ "can't be zero" throw ] when
7     seq length :> len
8     step 0 > [
9         from [ 0 ] unless*
10         to [ len ] unless*
11     ] [
12         from [ len ] unless*
13         to [ 0 ] unless*
14     ] if
15     [ dup 0 < [ len + ] when 0 len clamp ] bi@
16     ! FIXME: make this work with steps
17     seq dup slice? [ collapse-slice ] when
18     step step-slice boa ;
19
20 M: step-slice virtual@
21     [ step>> * ] [ from>> + ] [ seq>> ] tri ;
22
23 M: step-slice length
24     [ to>> ] [ from>> - ] [ step>> ] tri
25     dup 0 < [ [ neg 0 max ] dip neg ] when /mod
26     zero? [ 1 + ] unless ;