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

Fix broadcast with RowVectors of matrices #20980

Merged
merged 3 commits into from
Mar 12, 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
9 changes: 5 additions & 4 deletions base/linalg/rowvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,17 @@ end
@inline check_tail_indices(i1, i2, i3, is...) = i3 == 1 ? check_tail_indices(i1, i2, is...) : false

# helper function for below
@inline to_vec(rowvec::RowVector) = transpose(rowvec)
@inline to_vec(rowvec::RowVector) = map(transpose, transpose(rowvec))
@inline to_vec(x::Number) = x
@inline to_vecs(rowvecs...) = (map(to_vec, rowvecs)...)

# map
@inline map(f, rowvecs::RowVector...) = RowVector(map(f, to_vecs(rowvecs...)...))
# map: Preserve the RowVector by un-wrapping and re-wrapping, but note that `f`
# expects to operate within the transposed domain, so to_vec transposes the elements
@inline map(f, rowvecs::RowVector...) = RowVector(map(transpose∘f, to_vecs(rowvecs...)...))

# broacast (other combinations default to higher-dimensional array)
@inline broadcast(f, rowvecs::Union{Number,RowVector}...) =
RowVector(broadcast(f, to_vecs(rowvecs...)...))
RowVector(broadcast(transpose∘f, to_vecs(rowvecs...)...))

# Horizontal concatenation #

Expand Down
17 changes: 16 additions & 1 deletion test/linalg/rowvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ end
end
end

@testset "issue #20979" begin
f20979(z::Complex) = [z.re -z.im; z.im z.re]
v = [1+2im]'
@test (f20979.(v))[1] == f20979(v[1])
@test f20979.(v) == f20979.(collect(v))

w = rand(Complex128, 3)
@test f20979.(v') == f20979.(collect(v')) == (f20979.(v))'

g20979(x, y) = [x[2,1] x[1,2]; y[1,2] y[2,1]]
v = [rand(2,2), rand(2,2), rand(2,2)]
@test g20979.(v', v') == g20979.(collect(v'), collect(v')) ==
map(g20979, v', v') == map(g20979, collect(v'), collect(v'))
end

@testset "ambiguity between * methods with RowVectors and ConjRowVectors (#20971)" begin
@test RowVector(ConjArray(ones(4))) * ones(4) == 4
end
end