Skip to content

Fix inference on axes for broadcasting #547

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

Merged
merged 2 commits into from
Nov 19, 2018
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
3 changes: 3 additions & 0 deletions src/SOneTo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ end
Base.@pure function Base.checkindex(::Type{Bool}, ::SOneTo{n1}, ::SOneTo{n2}) where {n1, n2}
return n1::Int >= n2::Int
end

Base.promote_rule(a::Type{Base.OneTo{T}}, ::Type{SOneTo{n}}) where {T,n} =
Base.OneTo{promote_type(T, Int)}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is not strictly necessary for the fix (it's just the first thing I tried). I could get rid of this if you have reservations about it. (One also has to be rid of the tests this adds to core.jl.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good.

2 changes: 2 additions & 0 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Base.axes(s::StaticArray) = _axes(Size(s))
@pure function _axes(::Size{sizes}) where {sizes}
map(SOneTo, sizes)
end
Base.axes(rv::Adjoint{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
Base.axes(rv::Transpose{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)

function Base.summary(io::IO, a, inds::Tuple{SOneTo, Vararg{SOneTo}})
print(io, Base.dims2string(length.(inds)), " ")
Expand Down
6 changes: 6 additions & 0 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import Base.Broadcast:
BroadcastStyle, AbstractArrayStyle, Broadcasted, DefaultArrayStyle, materialize!
import Base.Broadcast: _bcs1 # for SOneTo axis information
using Base.Broadcast: _bcsm
# Add a new BroadcastStyle for StaticArrays, derived from AbstractArrayStyle
# A constructor that changes the style parameter N (array dimension) is also required
struct StaticArrayStyle{N} <: AbstractArrayStyle{N} end
Expand Down Expand Up @@ -37,6 +39,10 @@ end
_broadcast!(f, destsize, dest, argsizes, as...)
end

# Resolving priority between dynamic and static axes
_bcs1(a::SOneTo, b::SOneTo) = _bcsm(b, a) ? b : (_bcsm(a, b) ? a : throw(DimensionMismatch("arrays could not be broadcast to a common size")))
_bcs1(a::SOneTo, b::Base.OneTo) = _bcs1(Base.OneTo(a), b)
_bcs1(a::Base.OneTo, b::SOneTo) = _bcs1(a, Base.OneTo(b))

###################################################
## Internal broadcast machinery for StaticArrays ##
Expand Down
4 changes: 4 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ end
@test @inferred(v .- m) === @SMatrix [0 -1; 1 0]
@test @inferred(m .^ v) === @SMatrix [1 2; 81 256]
@test @inferred(v .^ m) === @SMatrix [1 1; 64 256]
# Issue #546
@test @inferred(m ./ (v .* v')) === @SMatrix [1.0 0.5; 0.75 0.25]
testinf(m, v) = m ./ (v .* v')
@test @inferred(testinf(m, v)) === @SMatrix [1.0 0.5; 0.75 0.25]
end

@testset "2x2 StaticMatrix with 1x2 StaticMatrix" begin
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,11 @@
@test !StaticArrays.sizematch(Size(2, 2), a)
@test !StaticArrays.sizematch(Size(3, StaticArrays.Dynamic()), a)
end

@testset "SOneTo" begin
a = Base.OneTo(3)
b = StaticArrays.SOneTo{2}()
@test @inferred(promote(a, b)) === (a, Base.OneTo(2))
@test @inferred(promote(b, a)) === (Base.OneTo(2), a)
end
end