Skip to content

Commit

Permalink
Get (c)transpose for vectors working with non-1 indices
Browse files Browse the repository at this point in the history
It was already working for matrices

(cherry picked from commit 9114ae6)
ref #17816
  • Loading branch information
timholy authored and tkelman committed Aug 11, 2016
1 parent 730c0a3 commit 1debda4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,13 @@ convert{T,S,N}(::Type{AbstractArray{T }}, A::AbstractArray{S,N}) = convert(Abst

convert{T,N}(::Type{Array}, A::AbstractArray{T,N}) = convert(Array{T,N}, A)

"""
of_indices(x, y)
Represents the array `y` as an array having the same indices type as `x`.
"""
of_indices(x, y) = similar(dims->y, oftype(indices(x), indices(y)))

full(x::AbstractArray) = x

map(::Type{Integer}, a::Array) = map!(Integer, similar(a,typeof(Integer(one(eltype(a))))), a)
Expand Down
4 changes: 2 additions & 2 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ function ctranspose(A::AbstractMatrix)
end
ctranspose{T<:Real}(A::AbstractVecOrMat{T}) = transpose(A)

transpose(x::AbstractVector) = [ transpose(v) for i=1:1, v in x ]
ctranspose{T}(x::AbstractVector{T}) = T[ ctranspose(v) for i=1:1, v in x ]
transpose(x::AbstractVector) = [ transpose(v) for i=of_indices(x, OneTo(1)), v in x ]
ctranspose{T}(x::AbstractVector{T}) = T[ ctranspose(v) for i=of_indices(x, OneTo(1)), v in x ]

_cumsum_type{T<:Number}(v::AbstractArray{T}) = typeof(+zero(T))
_cumsum_type(v) = typeof(v[1]+v[1])
Expand Down
17 changes: 16 additions & 1 deletion test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ using OAs

let
# Basics
v0 = rand(4)
v = OffsetArray(v0, (-3,))
@test indices(v) == (-2:1,)
@test_throws ErrorException size(v)
@test_throws ErrorException size(v, 1)

A0 = [1 3; 2 4]
A = OffsetArray(A0, (-1,2)) # LinearFast
S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow
Expand Down Expand Up @@ -179,6 +185,10 @@ end

# show
io = IOBuffer()
show(io, v)
str = takebuf_string(io)
show(io, v0)
@test str == takebuf_string(io)
show(io, A)
str = takebuf_string(io)
@test str == "[1 3; 2 4]"
Expand Down Expand Up @@ -261,7 +271,7 @@ v = view(A0, 1:1, i1)
# logical indexing
@test A[A .> 2] == [3,4]

# copy!
# copy! and fill!
a = OffsetArray{Int}((-3:-1,))
fill!(a, -1)
copy!(a, (1,2)) # non-array iterables
Expand Down Expand Up @@ -316,6 +326,7 @@ copy!(am, b)
@test am[1,8] == 2
@test am[1,9] == -1

# map
dest = similar(am)
map!(+, dest, am, am)
@test dest[1,7] == 2
Expand All @@ -326,6 +337,10 @@ am = map(identity, a)
@test isa(am, OffsetArray)
@test am == a

# other functions
v = OffsetArray(v0, (-3,))
@test parent(v') == v0'
@test indices(v') === (1:1,-2:1)
A = OffsetArray(rand(4,4), (-3,5))
@test maximum(A) == maximum(parent(A))
@test minimum(A) == minimum(parent(A))
Expand Down

0 comments on commit 1debda4

Please sign in to comment.