I actually think you don't need generated functions for this kind of stuff anymore. Our iterators have gotten to be sufficiently awesome for many tasks, especially with the LLVM optimizations that mean that things that _look_ like additions don't actually need to be performed :-)
That said, I did run into a situation recently that needed some hand tweaking, so it's possible that this would be one. Before going to a generated function, though, I'd split off the inner index for special treatment, much like in https://github.com/JuliaLang/julia/blob/6da5d0662059e8fda1acf012d9fc4ce7b88398a8/base/reducedim.jl#L200-L221 Of course, generated functions are the ultimate solution. See also the devdocs on Base.Cartesian. --Tim On Tuesday, August 11, 2015 04:13:24 PM Stefan Karpinski wrote: > The way to do this efficiently and generally is to use generated functions > <http://docs.julialang.org/en/latest/manual/metaprogramming/#generated-funct > ions>. You can generate specialized versions of this functionality for each > dimensionality of array and shape of slice. > > On Tue, Aug 11, 2015 at 2:28 PM, Tim Holy <[email protected]> wrote: > > If you don't care about performance, the generalization of your method is > > `mapslices`. > > > > If you do care about performance and you're running Julia 0.4, > > check out the Cartesian iterators: > > http://docs.julialang.org/en/latest/manual/arrays/#iteration > > > > Something like: > > for I in CartesianRange(Istart, Iend) > > > > Acurrent = A[I] > > for d = 1:N > > > > counter[d] += Acurrent >= A[I-offset[d]] > > > > end > > > > end > > > > where offset[d] is a vector of CartesianIndexes that look like > > (0,0,1,0,0,...) > > (1 is in the dth slot). > > > > Best, > > --Tim > > > > On Tuesday, August 11, 2015 09:50:07 AM Nils Gudat wrote: > > > This is a re-post of sorts of a question I already asked a year ago in > > > > the > > > > > user group. At the time, Stefan Karpinski proposed that base.issorted > > > > might > > > > > be expanded to accomodate checking in multiple dimensions, but looking > > > at > > > the docs it seems this hasn't happened. This post now has two purposes: > > > 1. To ask if anyone knows of such a functionality, maybe in a package? > > > 2. To see if someone has an idea about how to improve my approach. I'm > > > basically doing this: > > > > > > function checkmonotonicity(v::Array{Float64,2}) > > > > > > counter = zeros(2) > > > > > > for b = 1:size(v,2) > > > > > > counter[1] += ~issorted(v[:, b]) > > > > > > end > > > > > > for a = 1:size(v,1) > > > > > > counter[2] += ~issorted(v[a, :]) > > > > > > end > > > > > > return counter > > > > > > end > > > > > > which for three dimensions requires me to write: > > > > > > function checkmonotonicity(v::Array{Float64,3}) > > > > > > counter = zeros(3) > > > > > > for c = 1:size(v,3), b = 1:size(v,2) > > > > > > counter[1] += ~issorted(v[:, b, c]) > > > > > > end > > > > > > for c = 1:size(v,3), a = 1:size(v,1) > > > > > > counter[2] += ~issorted(v[a, :, c]) > > > > > > end > > > > > > for b = 1:size(v,2), a = 1:size(v,1) > > > > > > counter[3] += ~issorted(v[a, b, :]) > > > > > > end > > > > > > return counter > > > > > > end > > > > > > and so on for higher dimensions. Obviously, this quickly gets cumbersome > > > for higher dimensions, so I wanted to see if anyone could recommend a > > > > smart > > > > > way of doing this (maybe via metaprogramming?). > > > > > > (For context, I'm actually interested in the number of monotonicity > > > violations, which is why I count them rather than just breaking at the > > > first violation in a given dimension. This isn't too important, so if > > > there's an existing function that simply returns a Boolean for each > > > dimension, I'm happy with that.)
