operation
Sweep vector
import mir.algorithm.iteration: all; import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; static double f(T)(T x) { return 3.5; } auto x = [1.0, 2, 3, 4, 5, 6].sliced; assert(x.sweep!(f, "-").all!approxEqual([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])); assert(x.sweep!"-"(3.5).all!approxEqual([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])); assert(x.sweep!(f, "+").all!approxEqual([4.5, 5.5, 6.5, 7.5, 8.5, 9.5]));
Sweep dynamic array
import mir.algorithm.iteration: all; import mir.math.common: approxEqual; static double f(T)(T x) { return 3.5; } auto x = [1.0, 2, 3, 4, 5, 6]; assert(x.sweep!(f, "-").all!approxEqual([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])); assert(x.sweep!"-"(3.5).all!approxEqual([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])); assert(x.sweep!(f, "+").all!approxEqual([4.5, 5.5, 6.5, 7.5, 8.5, 9.5]));
Sweep matrix
import mir.algorithm.iteration: all; import mir.math.common: approxEqual; import mir.ndslice.fuse: fuse; static double f(T)(T x) { return 3.5; } auto x = [ [1.0, 2, 3], [4.0, 5, 6] ].fuse; auto y0 = [ [-2.5, -1.5, -0.5], [ 0.5, 1.5, 2.5] ]; auto y1 = [ [4.5, 5.5, 6.5], [7.5, 8.5, 9.5] ]; assert(x.sweep!(f, "-").all!approxEqual(y0)); assert(x.sweep!"-"(3.5).all!approxEqual(y0)); assert(x.sweep!(f, "+").all!approxEqual(y1));
Column sweep matrix
import mir.algorithm.iteration: all, equal; import mir.math.common: approxEqual; import mir.ndslice.fuse: fuse; import mir.ndslice.topology: alongDim, byDim, map; static double f(T)(T x) { return 0.5 * (x[0] +x[1]); } auto x = [ [20.0, 100.0, 2000.0], [10.0, 5.0, 2.0] ].fuse; auto result = [ [ 5.0, 47.5, 999], [-5.0, -47.5, -999] ].fuse; // Use byDim with map to sweep mean of row/column. auto xSweepByDim = x.byDim!1.map!(sweep!(f, "-")); auto resultByDim = result.byDim!1; assert(xSweepByDim.equal!(equal!approxEqual)(resultByDim)); auto xSweepAlongDim = x.alongDim!0.map!(sweep!(f, "-")); auto resultAlongDim = result.alongDim!0; assert(xSweepAlongDim.equal!(equal!approxEqual)(resultAlongDim));
Can also pass arguments to sweep function
import mir.algorithm.iteration: all; import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; static double f(T)(T x, double a) { return a; } static double g(double a, T)(T x) { return a; } auto x = [1.0, 2, 3, 4, 5, 6].sliced; assert(x.sweep!(a => f(a, 3.5), "-").all!approxEqual([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])); assert(x.sweep!(a => f(a, 3.5), "+").all!approxEqual([4.5, 5.5, 6.5, 7.5, 8.5, 9.5])); assert(x.sweep!(a => g!3.5(a), "-").all!approxEqual([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])); assert(x.sweep!(a => g!3.5(a), "+").all!approxEqual([4.5, 5.5, 6.5, 7.5, 8.5, 9.5]));
Sweep withAsSlice
import mir.algorithm.iteration: all; import mir.math.common: approxEqual; import mir.rc.array: RCArray; static double f(T)(T x) { return 3.5; } auto x = RCArray!double(6); foreach(i, ref e; x) e = i + 1; static immutable result1 = [-2.5, -1.5, -0.5, 0.5, 1.5, 2.5]; assert(x.sweep!(f, "-").all!approxEqual(result1)); assert(x.sweep!"-"(3.5).all!approxEqual(result1)); static immutable result2 = [4.5, 5.5, 6.5, 7.5, 8.5, 9.5]; assert(x.sweep!(f, "+").all!approxEqual(result2));