Skip to content

Instantly share code, notes, and snippets.

@mbauman mbauman/slicedim.ack
Created Sep 15, 2017

Embed
What would you like to do?
$ ack -C 5 '\bslicedim\b'
AutoGrad/src/base/abstractarraymath.jl
13-# imag
14-# +
15-# *
16-# /
17-# \
18:# slicedim
19-# flipdim
20-# circshift
21-# cumsum_kbn
22-# ipermutedims
23-if VERSION >= v"0.6.0"; @eval begin # ipermutedims was deprecated in julia6
AutoGrad/src/base/arraymath.jl
21-# .*
22-# .%
23-# .<<
24-# .>>
25-# rem
26:# slicedim
27-# flipdim
28-# rotl90
29-# rotr90
30-# rot180
31-# transpose!: Overwriting
Bootstrap/src/draw.jl
20- return o
21-end
22-
23-pick(x::AbstractVector, i::AbstractVector) = x[i]
24-
25:pick(x::AbstractArray, i::AbstractVector) = slicedim(x, 1, i)
26-
27-pick(x::AbstractDataFrame, i::AbstractVector) = x[i,:]
ControlSystems/src/utilities.jl
84-end
85-
86-function unwrap!(M::Array, dim=1)
87- alldims(i) = [ n==dim ? i : (1:size(M,n)) for n in 1:ndims(M) ]
88- for i = 2:size(M, dim)
89: #This is a copy of slicedim from the JuliaLang but enables us to write to it
90- #The code (with dim=1) is equivalent to
91- # d = M[i,:,:,...,:] - M[i-1,:,...,:]
92- # M[i,:,:,...,:] -= floor((d+π) / (2π)) * 2π
93- d = M[alldims(i)...] - M[alldims(i-1)...]
94- M[alldims(i)...] -= floor.((d+π) / 2π) * 2π
DiscreteFactor/src/dfactor.jl
114- Bcard[Reduce_dims] = 1
115-
116- valuespace = reshape(A.value, A.card...)
117-
118- for i = 1:length(Reduce_dims)
119: valuespace = slicedim(valuespace, Reduce_dims[i], Reduce_idx[i])
120- end
121- DF(Bvar, Bcard, valuespace[:])
122-end
123-
124-function DFNormalize(A::DF)
DSP/src/util.jl
30- thresh = range / 2
31- if size(m, dim) < 2
32- return m
33- end
34- for i = 2:size(m, dim)
35: d = slicedim(m, dim, i:i) - slicedim(m, dim, i-1:i-1)
36- slice_tuple = ntuple(n->(n==dim ? (i:i) : (1:size(m,n))), ndims(m))
37- offset = floor.((d.+thresh) / (range)) * range
38-# println("offset: ", offset)
39-# println("typeof(offset): ", typeof(offset))
40-# println("typeof(m[slice_tuple...]): ", typeof(m[slice_tuple...]))
Flux/src/utils.jl
5-flatten(xs) = reshape(xs, size(xs, 1), :)
6-
7-unsqueeze(xs, dim) = reshape(xs, (size(xs)[1:dim-1]..., 1, size(xs)[dim:end]...))
8-
9-stack(xs, dim) = cat(dim, unsqueeze.(xs, dim)...)
10:unstack(xs, dim) = [slicedim(xs, dim, i) for i = 1:size(xs, dim)]
11-
12-# Other
13-
14-function accuracy(m, data)
15- n = 0
FunctionalData/src/accessors.jl
16-@inline at{T,N}(a::NTuple{T,N},i) = a[i]
17-@inline at(a::AbstractArray, ind::Tuple) = a[ind...]
18-@inline at{T}(a::AbstractArray{T},i::AbstractArray) =
19- len(i) == 1 ? (size(i,1) == 1 ? at(a, i[1]) : a[subtoind(i,a)]) : error("index has len>1")
20-@inline at{T}(a::AbstractArray{T,1},i::Number) = a[i]
21:#at{T,N}(a::AbstractArray{T,N},i) = slicedim(a,N,i)
22-@inline at{T}(a::AbstractArray{T,2},i::Number) = col(a[:,i])
23-@inline at{T}(a::AbstractArray{T,3},i::Number) = a[:,:,i]
24-@inline at{T}(a::AbstractArray{T,4},i::Number) = a[:,:,:,i]
25-@inline at{T}(a::AbstractArray{T,5},i::Number) = a[:,:,:,:,i]
26-@inline at{T}(a::AbstractArray{T,6},i::Number) = a[:,:,:,:,:,i]
--
61-part(a::AbstractArray, i::Real) = part(a,[i])
62-part{T}(a::Vector, i::AbstractArray{T,1}) = a[i]
63-part{T}(a::AbstractString, i::AbstractArray{T,1}) = string(a[i])
64-part(a::UTF8String, i::Array{Bool,1}) = string(a[find(i)])
65-part{T}(a::NTuple{T},i::Int) = a[i]
66:part{T,T2,N}(a::AbstractArray{T2,N}, i::AbstractArray{T,1}) = slicedim(a,max(2,ndims(a)),i)
67-part{T1,T2}(a::AbstractArray{T1,1}, i::AbstractArray{T2,1}) = a[i]
68-dictpart(a, inds) = Dict(map(filter(collect(keys(a)), x->in(x,inds)), x->Pair(x,at(a,x))))
69-part(a::Dict, inds::AbstractVector) = dictpart(a, inds)
70-part(a::Dict, inds...) = dictpart(a, inds)
71-part{T<:Number}(a::AbstractArray,i::DenseArray{T,2}) = map(i, x->at(a,x))
FunctionalData/test/runtests.jl
210- @fact at(a,1) --> "bb"
211- end
212-
213- # shouldtestcontext("at!") do
214- # @fact at!([1,2,3],1) 1
215: # @fact at!([1 2 3],1) slicedim([1 2 3],2,1)
216- # @fact at!([1;2;3],1) 1
217: # @fact at!([1 2 3; 4 5 6],1) slicedim([1 2 3; 4 5 6],2,1)
218- # @fact at!("asdf",1) 'a'
219- # @fact at!(['a','b'],1) 'a'
220- # @fact at!([1,[2 3],'a'],2) [2 3]
221-
222- # D = [1 2 3; 4 5 6]
--
228- shouldtestcontext("part") do
229- @fact part([1,2,3],[1]) --> [1]
230- @fact part([1,2,3],1:2) --> [1,2]
231- @fact part([1,2,3],[1,2]) --> [1,2]
232- @fact part([1 2 3],[1,3]) --> [1 3]
233: @fact part([1 2 3],[1,3]) --> slicedim([1 2 3],2,[1,3])
234- @fact part([1;2;3],[1,3]) --> [1;3]
235- @fact part([1 2 3; 4 5 6],[1,3]) --> [1 3;4 6]
236- @fact part([1 2 3; 4 5 6],[1 2; 3 2]) --> [3,5]
237- end
238-
IntervalLinearEquations/src/F.jl
18-
19-function subDifferential(solver)
20- identityMatrix = eye(solver.system.size)
21-
22- loSubDiff = map(
23: index -> calulateLoSubdifferentialRow(slicedim(solver.system.a, 1, index), solver.previous.sti, slicedim(identityMatrix, 1, index)),
24- range(1, solver.system.size)
25- )
26- hiSubDiff = map(
27: index -> calulateHiSubdifferentialRow(slicedim(solver.system.a, 1, index), solver.previous.sti, slicedim(identityMatrix, 1, index)),
28- range(1, solver.system.size)
29- )
30- vcat(loSubDiff..., hiSubDiff...)
31-end
32-
IntervalLinearEquations/src/G.jl
17-end
18-
19-function subDifferential(solver)
20- identityMatrix = eye(solver.system.size)
21- loSubDiff = map(
22: index -> calulateLoSubdifferentialRow(slicedim(solver.system.a, 1, index), solver.previous.sti, slicedim(identityMatrix, 1, index)),
23- range(1, solver.system.size)
24- )
25- hiSubDiff = map(
26: index -> calulateHiSubdifferentialRow(slicedim(solver.system.a, 1, index), solver.previous.sti, slicedim(identityMatrix, 1, index)),
27- range(1, solver.system.size)
28- )
29- vcat(loSubDiff..., hiSubDiff...)
30-end
31-
LTISystems/src/methods/unwrap.jl
17- settol = max(float(tol), π)
18- setrng = 2settol
19- circ = 2π
20- for idx in 2:size(Θ, dim)
21- changeddims = ntuple(n->(n == dim ? (idx:idx) : 1:size(Θ, n)), dim)
22: jump = slicedim(Θ, dim, idx:idx) - slicedim(Θ, dim, idx-1:idx-1)
23- map!(j->floor((j+settol)/setrng)*circ, jump)
24- Θ[changeddims...] -= jump
25- end
26- return Θ
27-end
Match/CHANGES.md
58-===================
59-
60- * Misc cleanups
61- * Added tests, fixes for @zachallaun's Match.jl examples
62- * Update docs to remove Regex section
63: * Rename viewdim -> slicedim, clean up generated code more
64- * Fix match for v0.4, remove evals
65-
66-v0.0.6 / 2015-01-30
67-===================
68-
Match/src/matchmacro.jl
299- error("elipses (...) are only allowed once in an an Array pattern match.") #in the last position of
300- end
301- seen_dots = true
302- sym = array_type_of(exprs[i].args[1])
303- j = length(exprs) - i
304: s = :(Match.slicedim($val, $dim, $i, $j))
305- unapply(s, sym, syms, guardsyms, valsyms, info, array_checked)
306- elseif seen_dots
307- j = length(exprs) - i
308: s = :(Match.slicedim($val, $dim, $j, true))
309- unapply(s, exprs[i], syms, guardsyms, valsyms, info, array_checked)
310- else
311: s = :(Match.slicedim($val, $dim, $i))
312- unapply(s, exprs[i], syms, guardsyms, valsyms, info, array_checked)
313- end
314- end
315-
316- end
Match/src/matchutils.jl
10-Base.ismatch(c::Char, s::Number) = false
11-Base.ismatch(s::Number, c::Char) = false
12-Base.ismatch(r,s) = (r == s)
13-
14-#
15:# slicedim
16-#
17:# "view" version of slicedim
18-
19-function _slicedim(A::AbstractArray, d::Integer, i::Integer)
20- if (d < 1) | (d > ndims(A))
21- throw(BoundsError())
22- end
--
44- (if (d < 0) | (d > 1); throw(BoundsError()) end; A[i])
45-
46-_slicedim(A::AbstractVector, d::Integer, i) =
47- (if (d < 0) | (d > 1); throw(BoundsError()) end; view(A, i))
48-
49:function slicedim(A::AbstractArray, s::Integer, i::Integer, from_end::Bool = false)
50- d = s + max(ndims(A)-2, 0)
51- from_end && (i = size(A,d)-i)
52- _slicedim(A, d, i)
53-end
54-
55:function slicedim(A::AbstractArray, s::Integer, i::Integer, j::Integer)
56- d = s + max(ndims(A)-2, 0)
57- j = size(A,d)-j # this is the distance from the end of the dim size
58- _slicedim(A, d, i:j)
59-end
60-
MAT/src/MAT_HDF5.jl
130- if T == Float32
131- d = reinterpret(Complex64, dbuf, sz)
132- elseif T == Float64
133- d = reinterpret(Complex128, dbuf, sz)
134- else
135: d = slicedim(dbuf, 1, 1) + im * slicedim(dbuf, 1, 2)
136- end
137- length(d) == 1 ? d[1] : d
138-end
139-
140-function m_read(dset::HDF5Dataset)
RegionTrees/src/hyperrectangle.jl
21- convert(SVector{N, T2}, r.widths))
22-
23-@generated function faces(rect::HyperRectangle{N}) where N
24- quote
25- verts = vertices(rect)
26: SVector($(Expr(:tuple, [:(slicedim(verts, $d, $i)) for i in 1:2 for d in 1:N]...)))
27- end
28-end
29-
30-function body_and_face_centers(rect::HyperRectangle)
31- chain((center(rect),), (f -> reduce(+, f) ./ length(f)).(faces(rect)))
RegionTrees/src/twosarray.jl
15-end
16-
17-getindex(b::TwosArray, i::Int) = b.data[i]
18-
19-"""
20:Highly specialized slicedim implementation for TwosArray (an array of size
21-2 along every dimension). Returns a TwosArray with N-1 dimensions. See
22-`test/twosarray.jl` for exhaustive testing of this function. This is about
23:100 times faster than Julia's base slicedim().
24-"""
25:@generated function Base.slicedim(A::TwosArray{N, T}, d::Integer, i::Integer) where {N, T}
26- quote
27- x = 2^(d - 1)
28- j = 1
29- k = 1
30- TwosArray($(Expr(:tuple, [quote
RegionTrees/test/twosarray.jl
14- @test a2[2,1] == "b"
15- @test a2[3] == "c"
16- @test a2[1,2] == "c"
17- @test a2[4] == "d"
18- @test a2[2,2] == "d"
19: @test slicedim(a2, 1, 1) == a2[1,:]
20: @test slicedim(a2, 1, 2) == a2[2,:]
21: @test slicedim(a2, 2, 1) == a2[:,1]
22: @test slicedim(a2, 2, 2) == a2[:,2]
23-
24- a3 = TwosArray(1:8...)
25- @test size(a3) == (2,2,2)
26- @test a3[1,1,1] == 1
27- @test a3[2,1,1] == 2
28- @test a3[1,2,2] == 7
29: @test slicedim(a3, 1, 1) == a3[1,:,:]
30: @test slicedim(a3, 1, 2) == a3[2,:,:]
31: @test slicedim(a3, 2, 1) == a3[:,1,:]
32: @test slicedim(a3, 2, 2) == a3[:,2,:]
33: @test slicedim(a3, 3, 1) == a3[:,:,1]
34: @test slicedim(a3, 3, 2) == a3[:,:,2]
35-
36- a4 = TwosArray(1:16...)
37- @test size(a4) == (2,2,2,2)
38- @test a4[1,1,1,1] == 1
39- @test a4[2,1,1,1] == 2
40: @test slicedim(a4, 1, 1) == a4[1,:,:,:]
41: @test slicedim(a4, 1, 2) == a4[2,:,:,:]
42: @test slicedim(a4, 2, 1) == a4[:,1,:,:]
43: @test slicedim(a4, 2, 2) == a4[:,2,:,:]
44: @test slicedim(a4, 3, 1) == a4[:,:,1,:]
45: @test slicedim(a4, 3, 2) == a4[:,:,2,:]
46: @test slicedim(a4, 4, 1) == a4[:,:,:,1]
47: @test slicedim(a4, 4, 2) == a4[:,:,:,2]
48-end
RLEVectors/src/RLEDataTable-type.jl
118-Base.setindex!(x::RLEDataTable, value, i::Integer, j) = setindex!(x,value,[i],j)
119-Base.setindex!(x::RLEDataTable, value, i::Integer, j::ColumnIndex) = setindex!(x.columns[j],value,i)
120-
121-### Familiar operations over rows or columns from R
122-
123:# Probably these are all a job for mapslice or slicedim. I need to RTM.
124-rowmap(x::Matrix,f::Function) = [ f( @view x[i,:] ) for i in 1:size(x)[1] ]
125-colmap(x::Matrix,f::Function) = [ f( @view x[:,j] ) for j in 1:size(x)[2] ]
126-rowMeans(x) = rowmap(x,mean)
127-rowMedians(x) = rowmap(x,median)
128-rowSums(x) = rowmap(x,sum)
ShapeModels/src/plotfunctions.jl
34- else
35- gridplot(a)
36- end
37- end
38-
39: at(a,i) = slicedim(a,ndims(a),i)
40-
41- function gridplot(a)
42- N = last(size(a))
43- sm = floor(sqrt(N))
44- sn = ceil(N/sm)
TensorDecompositions/src/tensorcur.jl
22-
23- res = zeros(0)
24- if compute_u
25- output_index = collect(1:3)
26- output_index[slab_axis] = 4
27: S = tensorcontract(slicedim(tnsr, slab_axis, Cindex), [1, 2, 3], U, [4, slab_axis], output_index)
28- W = squeeze(permutedims(mapslices(slab -> slab[Rindex], tnsr, fiber_axes),
29- [slab_axis, fiber_axes[1], fiber_axes[2]]), 3)
30- S = tensorcontract(S, output_index, W, [slab_axis, 4], [1, 2, 3])
31- res = mapslices(vecnorm, S - tnsr, fiber_axes)[:] ./ mapslices(vecnorm, tnsr, fiber_axes)[:]
32- end
--
62-
63- U = compute_u ? Array{Float64}(r, c) : zeros(0, 0)
64- if compute_u
65- P = (Cweight*Rweight') ./ (p[Cindex]*q[Rindex]')
66- W = squeeze(permutedims(mapslices(slab -> slab[Rindex],
67: slicedim(tnsr, slab_axis, Cindex), fiber_axes),
68- [slab_axis, fiber_axes[1], fiber_axes[2]]), 3)
69- U = pinv(W .* P) .* P'
70- end
71-
72- return CUR(tnsr, slab_axis, fiber_axes, fiber_size, Cindex, Cweight, Rindex, Rweight, U, compute_u)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.