@@ -32,7 +32,6 @@ $(T2 kronecker, Kronecker product.)
3232$(BOOKTABLE $(H2 Representation Selectors),
3333$(TR $(TH Function Name) $(TH Description))
3434
35- $(T2 apply, Convenience function to apply a function along a dimension of a slice.)
3635$(T2 as, Convenience function that creates a lazy view,
3736where each element of the original slice is converted to a type `T`.)
3837$(T2 bitpack, Bitpack slice over an unsigned integral slice.)
@@ -3345,135 +3344,3 @@ unittest
33453344 y.popFront;
33463345 assert (y.front == iota([3 ], 1 , 4 ));
33473346}
3348-
3349- /+ +
3350- Convenience function to apply a function along a dimension of a slice.
3351-
3352- Combines $(LREF byDim) and $(LREF map) together for ease of use.
3353-
3354- Params:
3355- f = function to feed to map
3356- dim = dimension to perform function on
3357- slice = input slice (may not be 1-dimensional slice)
3358- Returns:
3359- 1-dimensional slice of results of function applied to each dimension
3360- See_also:
3361- $(LREF byDim),
3362- $(LREF map),
3363- $(HTTP https://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html, apply)
3364- +/
3365- template apply (alias f, size_t dim = 0 )
3366- {
3367- import mir.ndslice : Slice, SliceKind;
3368-
3369- auto apply (SliceKind kind, size_t [] packs, Iterator)(Slice! (kind, packs, Iterator) slice)
3370- {
3371- import mir.ndslice : map;
3372- return slice.byDim! dim.map! f;
3373- }
3374- }
3375-
3376- // / Apply functions to matrix by row
3377- @safe @nogc pure nothrow
3378- unittest
3379- {
3380- import mir.math.sum : sum;
3381- import mir.ndslice : sliced, count;
3382-
3383- static immutable x = [0.0 , 1.0 , 1.5 , 2.0 , 3.5 , 4.25 ];
3384- auto y = x.sliced(3 , 2 );
3385-
3386- static immutable sumResult = [1.0 , 3.5 , 7.75 ];
3387- assert (y.apply! sum == sumResult.sliced);
3388-
3389- static immutable countResult = [0 , 0 , 2 ];
3390- assert (y.apply! (count! (a => a > 2.0 )) == countResult.sliced);
3391- }
3392-
3393- // / Apply functions to matrix by row with lambda
3394- @safe @nogc pure nothrow
3395- unittest
3396- {
3397- import mir.math.sum : sum;
3398- import mir.ndslice : sliced, count;
3399-
3400- static immutable x = [0.0 , 1.0 , 1.5 , 2.0 , 3.5 , 4.25 ];
3401- auto y = x.sliced(3 , 2 );
3402-
3403- static immutable sumResult = [1.0 , 3.5 , 7.75 ];
3404- assert (y.apply! (a => a.sum(0.0L )) == sumResult.sliced);
3405-
3406- static immutable countResult = [0 , 0 , 2 ];
3407- assert (y.apply! (a => a.count! (a => a > 2.0 )) == countResult.sliced);
3408- }
3409-
3410- // / Apply functions to matrix by column
3411- @safe @nogc pure nothrow
3412- unittest
3413- {
3414- import mir.math.sum : sum;
3415- import mir.ndslice : sliced, count;
3416-
3417- static immutable x = [0.0 , 1.0 , 1.5 , 2.0 , 3.5 , 4.25 ];
3418- auto y = x.sliced(3 , 2 );
3419-
3420- static immutable sumResult = [5.0 , 7.25 ];
3421- assert (x.sliced(3 , 2 ).apply! (sum, 1 ) == sumResult.sliced);
3422-
3423- static immutable countResult = [1 , 1 ];
3424- assert (y.apply! (count! (a => a > 2.0 ), 1 ) == countResult.sliced);
3425- }
3426-
3427- // / Apply functions to 3-dimensional slice by 1st dimension
3428- @safe @nogc pure nothrow
3429- unittest
3430- {
3431- import mir.math.sum : sum;
3432- import mir.ndslice : sliced, count;
3433-
3434- static immutable x = [0.0 , 1.0 , 1.5 , 2.0 , 3.5 , 4.25 ,
3435- 2.0 , 7.5 , 5.0 , 1.0 , 1.5 , 0.0 ];
3436- auto y = x.sliced(3 , 2 , 2 );
3437-
3438- static immutable sumResult = [4.5 , 17.25 , 7.5 ];
3439- assert (y.apply! (sum) == sumResult.sliced);
3440-
3441- static immutable countResult = [0 , 3 , 1 ];
3442- assert (y.apply! (count! (a => a > 2.0 )) == countResult.sliced);
3443- }
3444-
3445- // / Apply functions to 3-dimensional slice by 2nd dimension
3446- @safe @nogc pure nothrow
3447- unittest
3448- {
3449- import mir.math.sum : sum;
3450- import mir.ndslice : sliced, count;
3451-
3452- static immutable x = [0.0 , 1.0 , 1.5 , 2.0 , 3.5 , 4.25 ,
3453- 2.0 , 7.5 , 5.0 , 1.0 , 1.5 , 0.0 ];
3454- auto y = x.sliced(3 , 2 , 2 );
3455-
3456- static immutable sumResult = [14.75 , 14.5 ];
3457- assert (y.apply! (sum, 1 ) == sumResult.sliced);
3458-
3459- static immutable countResult = [3 , 1 ];
3460- assert (y.apply! (count! (a => a > 2.0 ), 1 ) == countResult.sliced);
3461- }
3462-
3463- // / Apply functions to 3-dimensional slice by 3rd dimension
3464- @safe @nogc pure nothrow
3465- unittest
3466- {
3467- import mir.math.sum : sum;
3468- import mir.ndslice : sliced, count;
3469-
3470- static immutable x = [0.0 , 1.0 , 1.5 , 2.0 , 3.5 , 4.25 ,
3471- 2.0 , 7.5 , 5.0 , 1.0 , 1.5 , 0.0 ];
3472- auto y = x.sliced(3 , 2 , 2 );
3473-
3474- static immutable sumResult = [13.5 , 15.75 ];
3475- assert (y.apply! (sum, 2 ) == sumResult.sliced);
3476-
3477- static immutable countResult = [2 , 2 ];
3478- assert (y.apply! (count! (a => a > 2.0 ), 2 ) == countResult.sliced);
3479- }
0 commit comments