Skip to content

Commit 4f089af

Browse files
authored
Merge pull request #547 from JuliaArrays/teh/inferbcast
Fix inference on axes for broadcasting
2 parents cf87ee2 + bb47a48 commit 4f089af

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

src/SOneTo.jl

+3
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ end
5858
Base.@pure function Base.checkindex(::Type{Bool}, ::SOneTo{n1}, ::SOneTo{n2}) where {n1, n2}
5959
return n1::Int >= n2::Int
6060
end
61+
62+
Base.promote_rule(a::Type{Base.OneTo{T}}, ::Type{SOneTo{n}}) where {T,n} =
63+
Base.OneTo{promote_type(T, Int)}

src/abstractarray.jl

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Base.axes(s::StaticArray) = _axes(Size(s))
1313
@pure function _axes(::Size{sizes}) where {sizes}
1414
map(SOneTo, sizes)
1515
end
16+
Base.axes(rv::Adjoint{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
17+
Base.axes(rv::Transpose{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1618

1719
function Base.summary(io::IO, a, inds::Tuple{SOneTo, Vararg{SOneTo}})
1820
print(io, Base.dims2string(length.(inds)), " ")

src/broadcast.jl

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

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

4147
###################################################
4248
## Internal broadcast machinery for StaticArrays ##

test/broadcast.jl

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ end
4949
@test @inferred(v .- m) === @SMatrix [0 -1; 1 0]
5050
@test @inferred(m .^ v) === @SMatrix [1 2; 81 256]
5151
@test @inferred(v .^ m) === @SMatrix [1 1; 64 256]
52+
# Issue #546
53+
@test @inferred(m ./ (v .* v')) === @SMatrix [1.0 0.5; 0.75 0.25]
54+
testinf(m, v) = m ./ (v .* v')
55+
@test @inferred(testinf(m, v)) === @SMatrix [1.0 0.5; 0.75 0.25]
5256
end
5357

5458
@testset "2x2 StaticMatrix with 1x2 StaticMatrix" begin

test/core.jl

+7
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@
183183
@test !StaticArrays.sizematch(Size(2, 2), a)
184184
@test !StaticArrays.sizematch(Size(3, StaticArrays.Dynamic()), a)
185185
end
186+
187+
@testset "SOneTo" begin
188+
a = Base.OneTo(3)
189+
b = StaticArrays.SOneTo{2}()
190+
@test @inferred(promote(a, b)) === (a, Base.OneTo(2))
191+
@test @inferred(promote(b, a)) === (Base.OneTo(2), a)
192+
end
186193
end

0 commit comments

Comments
 (0)