Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map and map! #71

Merged
merged 1 commit into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/combine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function equalvalued(X::NTuple)
return allequal
end #equalvalued

sizes{T<:AxisArray}(As::T...) = tuple(zip(map(size, As)...)...)
sizes{T<:AxisArray}(As::T...) = tuple(zip(map(a -> map(length, indices(a)), As)...)...)
matchingdims{N,T<:AxisArray}(As::NTuple{N,T}) = all(equalvalued, sizes(As...))
matchingdimsexcept{N,T<:AxisArray}(As::NTuple{N,T}, n::Int) = all(equalvalued, sizes(As[[1:n-1; n+1:end]]...))

Expand Down
17 changes: 17 additions & 0 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,23 @@ Base.ctranspose{T}(A::AxisArray{T,2}) = AxisArray(ctranspose(A.data), A.axes[2],
Base.transpose{T}(A::AxisArray{T,1}) = AxisArray(transpose(A.data), Axis{:transpose}(Base.OneTo(1)), A.axes[1])
Base.ctranspose{T}(A::AxisArray{T,1}) = AxisArray(ctranspose(A.data), Axis{:transpose}(Base.OneTo(1)), A.axes[1])

Base.map!{F}(f::F, A::AxisArray) = (map!(f, A.data); A)
Base.map(f, A::AxisArray) = AxisArray(map(f, A.data), A.axes...)

function Base.map!{F,T,N,D,Ax<:Tuple{Vararg{Axis}}}(f::F, dest::AxisArray{T,N,D,Ax},
As::AxisArray{T,N,D,Ax}...)
matchingdims((dest, As...)) || error("All axes must be identically-valued")
data = map(a -> a.data, As)
map!(f, dest.data, data...)
return dest
end

function Base.map{T,N,D,Ax<:Tuple{Vararg{Axis}}}(f, As::AxisArray{T,N,D,Ax}...)
matchingdims(As) || error("All axes must be identically-valued")
data = map(a -> a.data, As)
return AxisArray(map(f, data...), As[1].axes...)
end

permutation(to::Union{AbstractVector{Int},Tuple{Int,Vararg{Int}}}, from::Symbols) = to

"""
Expand Down
23 changes: 23 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,26 @@ A[0] = 12
A = AxisArray(OffsetArrays.OffsetArray(rand(4,5), -1:2, 5:9), :x, :y)
@test indices(A) == (-1:2, 5:9)
@test linearindices(A) == 1:20

@test AxisArrays.matchingdims((A, A))

f1(x) = x < 0
A2 = map(f1, A)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == map(f1, A.data)

map!(~, A2)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == ~map(f1, A).data

A2 = map(+, A, A)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == A.data .+ A.data

map!(*, A2, A, A)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == A.data .* A.data