Skip to content

Commit e72b219

Browse files
authored
Merge pull request #20 from andyferris/arrays-of-sarrays
Make Array-of-SArray `op` Number infer the right output Array type
2 parents 017cf71 + 05be4fb commit e72b219

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/arraymath.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import Base: .+, .-, .*, ./
22

3+
# Support for elementwise ops on AbstractArray{S<:StaticArray} with Number
4+
Base.promote_op{Op,A<:StaticArray,T<:Number}(op::Op, ::Type{A}, ::Type{T}) = similar_type(A, promote_op(op, eltype(A), T))
5+
Base.promote_op{Op,T<:Number,A<:StaticArray}(op::Op, ::Type{T}, ::Type{A}) = similar_type(A, promote_op(op, T, eltype(A)))
6+
7+
38
# TODO lots more operators
49

510
@inline .-(a1::StaticArray) = broadcast(-, a1)

test/arraymath.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
@testset "Array math" begin
2+
@testset "AbstractArray-of-StaticArray with scalar math" begin
3+
v = SVector{2,Float64}[SVector{2,Float64}(1,1)]
4+
@test v .* 1.0 == v
5+
@test typeof(v .* 1.0) == typeof(v)
6+
@test 1 .- v == v .- v
7+
@test typeof(1 .- v) == typeof(v)
8+
v2 = SVector{2,Int}[SVector{2,Int}(1,1)]
9+
@test v2 .* 1.0 == v
10+
@test typeof(v2 .* 1.0) == typeof(v)
11+
end
12+
213
@testset "Array-scalar math" begin
314
m = @SMatrix [1 2; 3 4]
415

0 commit comments

Comments
 (0)