Skip to content

Commit 9114ae6

Browse files
committedAug 4, 2016
Get (c)transpose for vectors working with non-1 indices
It was already working for matrices
1 parent a1e5daf commit 9114ae6

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed
 

‎base/abstractarray.jl

+7
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,13 @@ convert{T,S,N}(::Type{AbstractArray{T }}, A::AbstractArray{S,N}) = convert(Abst
625625

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

628+
"""
629+
of_indices(x, y)
630+
631+
Represents the array `y` as an array having the same indices type as `x`.
632+
"""
633+
of_indices(x, y) = similar(dims->y, oftype(indices(x), indices(y)))
634+
628635
full(x::AbstractArray) = x
629636

630637
map(::Type{Integer}, a::Array) = map!(Integer, similar(a,typeof(Integer(one(eltype(a))))), a)

‎base/arraymath.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ function ctranspose(A::AbstractMatrix)
272272
end
273273
ctranspose{T<:Real}(A::AbstractVecOrMat{T}) = transpose(A)
274274

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

278278
_cumsum_type{T<:Number}(v::AbstractArray{T}) = typeof(+zero(T))
279279
_cumsum_type(v) = typeof(v[1]+v[1])

‎test/offsetarray.jl

+16-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ using OAs
101101

102102
let
103103
# Basics
104+
v0 = rand(4)
105+
v = OffsetArray(v0, (-3,))
106+
@test indices(v) == (-2:1,)
107+
@test_throws ErrorException size(v)
108+
@test_throws ErrorException size(v, 1)
109+
104110
A0 = [1 3; 2 4]
105111
A = OffsetArray(A0, (-1,2)) # LinearFast
106112
S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow
@@ -179,6 +185,10 @@ end
179185

180186
# show
181187
io = IOBuffer()
188+
show(io, v)
189+
str = takebuf_string(io)
190+
show(io, v0)
191+
@test str == takebuf_string(io)
182192
show(io, A)
183193
str = takebuf_string(io)
184194
@test str == "[1 3; 2 4]"
@@ -261,7 +271,7 @@ v = view(A0, 1:1, i1)
261271
# logical indexing
262272
@test A[A .> 2] == [3,4]
263273

264-
# copy!
274+
# copy! and fill!
265275
a = OffsetArray{Int}((-3:-1,))
266276
fill!(a, -1)
267277
copy!(a, (1,2)) # non-array iterables
@@ -316,6 +326,7 @@ copy!(am, b)
316326
@test am[1,8] == 2
317327
@test am[1,9] == -1
318328

329+
# map
319330
dest = similar(am)
320331
map!(+, dest, am, am)
321332
@test dest[1,7] == 2
@@ -326,6 +337,10 @@ am = map(identity, a)
326337
@test isa(am, OffsetArray)
327338
@test am == a
328339

340+
# other functions
341+
v = OffsetArray(v0, (-3,))
342+
@test parent(v') == v0'
343+
@test indices(v') === (1:1,-2:1)
329344
A = OffsetArray(rand(4,4), (-3,5))
330345
@test maximum(A) == maximum(parent(A))
331346
@test minimum(A) == minimum(parent(A))

0 commit comments

Comments
 (0)
Please sign in to comment.