Skip to content

Commit b31a48e

Browse files
authored
Merge branch 'master' into matrix-exponential-master
2 parents a9ee5c4 + 6a292f2 commit b31a48e

20 files changed

+238
-12
lines changed

src/FieldVector.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ abstract type FieldVector{N, T} <: StaticVector{N, T} end
2323
@propagate_inbounds setindex!(v::FieldVector, x, i::Int) = setfield!(v, i, x)
2424

2525
# See #53
26-
Base.cconvert{T}(::Type{Ptr{T}}, v::FieldVector) = Base.RefValue(v)
27-
Base.unsafe_convert{T, FV <: FieldVector}(::Type{Ptr{T}}, m::Base.RefValue{FV}) =
28-
_unsafe_convert(Ptr{T}, eltype(FV), m)
29-
_unsafe_convert{T, FV <: FieldVector}(::Type{Ptr{T}}, ::Type{T}, m::Base.RefValue{FV}) =
30-
Ptr{T}(Base.unsafe_convert(Ptr{FV}, m))
26+
Base.cconvert(::Type{<:Ptr}, v::FieldVector) = Base.RefValue(v)
27+
Base.unsafe_convert(::Type{Ptr{T}}, m::Base.RefValue{FV}) where {N,T,FV<:FieldVector{N,T}} =
28+
Ptr{T}(Base.unsafe_convert(Ptr{FV}, m))

src/SUnitRange.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ end
1313
end
1414

1515
function check_sunitrange_params(L)
16-
throw(TypeError(:SUnitRange, "type parameters must be `Int`s", Tuple{Int, Int, Int}, Tuple{typeof(a), typeof(b), typeof(c)}))
16+
throw(TypeError(:SUnitRange, "type parameters must be `Int`", Tuple{Int,}, Tuple{typeof(L),}))
1717
end
1818

1919
@pure SUnitRange(a::Int, b::Int) = SUnitRange{a, max(0, b - a + 1)}()

src/linalg.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ end
103103

104104
return quote
105105
@_inline_meta
106-
@inbounds return similar_type(a, Size($Snew))(tuple($(exprs...)))
106+
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
107107
end
108108
end
109109
# TODO make these more efficient
@@ -119,7 +119,7 @@ end
119119

120120
@generated function _hcat(::Size{Sa}, ::Size{Sb}, a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}) where {Sa, Sb}
121121
if Sa[1] != Sb[1]
122-
error("Dimension mismatch")
122+
throw(DimensionMismatch("Tried to hcat arrays of size $Sa and $Sb"))
123123
end
124124

125125
exprs = vcat([:(a[$i]) for i = 1:prod(Sa)],
@@ -129,7 +129,7 @@ end
129129

130130
return quote
131131
@_inline_meta
132-
@inbounds return similar_type(a, Size($Snew))(tuple($(exprs...)))
132+
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
133133
end
134134
end
135135
# TODO make these more efficient
@@ -297,8 +297,8 @@ end
297297
end
298298
end
299299

300-
# TODO same for `RowVector`?
301300
@inline Size(::Union{RowVector{T, SA}, Type{RowVector{T, SA}}}) where {T, SA <: StaticArray} = Size(1, Size(SA)[1])
301+
@inline Size(::Union{RowVector{T, CA}, Type{RowVector{T, CA}}} where CA <: ConjVector{<:Any, SA}) where {T, SA <: StaticArray} = Size(1, Size(SA)[1])
302302
@inline Size(::Union{Symmetric{T,SA}, Type{Symmetric{T,SA}}}) where {T,SA<:StaticArray} = Size(SA)
303303
@inline Size(::Union{Hermitian{T,SA}, Type{Hermitian{T,SA}}}) where {T,SA<:StaticArray} = Size(SA)
304304

src/mapreduce.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,15 @@ end
126126
end
127127

128128
@generated function _mapreducedim(f, op, ::Size{S}, a::StaticArray, ::Type{Val{D}}, v0) where {S, D}
129-
N = ndims(a)
129+
N = length(S)
130130
Snew = ([n==D ? 1 : S[n] for n = 1:N]...)
131131

132132
exprs = Array{Expr}(Snew)
133133
itr = [1:n for n = Snew]
134134
for i Base.product(itr...)
135135
expr = :v0
136136
for k = 1:S[D]
137+
ik = collect(i)
137138
ik[D] = k
138139
expr = :(op($expr, f(a[$(ik...)])))
139140
end

src/matrix_multiply.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ promote_matprod{T1,T2}(::Type{T1}, ::Type{T2}) = typeof(zero(T1)*zero(T2) + zero
3636
@inline *(A::StaticMatrix, B::StaticMatrix) = _A_mul_B(Size(A), Size(B), A, B)
3737
@inline *(A::StaticVector, B::StaticMatrix) = *(reshape(A, Size(Size(A)[1], 1)), B)
3838
@inline *(A::StaticVector, B::RowVector{<:Any, <:StaticVector}) = _A_mul_B(Size(A), Size(B), A, B)
39+
@inline *(A::StaticVector, B::RowVector{<:Any, <:ConjVector{<:Any, <:StaticVector}}) = _A_mul_B(Size(A), Size(B), A, B)
3940

4041
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticMatrix, B::StaticVector) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
4142
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticMatrix, B::StaticMatrix) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
4243
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticVector, B::StaticMatrix) = A_mul_B!(dest, reshape(A, Size(Size(A)[1], 1)), B)
4344
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticVector, B::RowVector{<:Any, <:StaticVector}) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
45+
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticVector, B::RowVector{<:Any, <:ConjVector{<:Any, <:StaticVector}}) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
4446

4547
#@inline *{TA<:Base.LinAlg.BlasFloat,Tb}(A::StaticMatrix{TA}, b::StaticVector{Tb})
4648

@@ -93,6 +95,18 @@ end
9395
end
9496
end
9597

98+
# complex outer product
99+
@generated function _A_mul_B(::Size{sa}, ::Size{sb}, a::StaticVector{<: Any, Ta}, b::RowVector{Tb, <:ConjVector{<:Any, <:StaticVector}}) where {sa, sb, Ta, Tb}
100+
newsize = (sa[1], sb[2])
101+
exprs = [:(a[$i]*b[$j]) for i = 1:sa[1], j = 1:sb[2]]
102+
103+
return quote
104+
@_inline_meta
105+
T = promote_op(*, Ta, Tb)
106+
@inbounds return similar_type(b, T, Size($newsize))(tuple($(exprs...)))
107+
end
108+
end
109+
96110
@generated function _A_mul_B(Sa::Size{sa}, Sb::Size{sb}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticMatrix{<:Any, <:Any, Tb}) where {sa, sb, Ta, Tb}
97111
# Heuristic choice for amount of codegen
98112
if sa[1]*sa[2]*sb[2] <= 8*8*8

test/MMatrix.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
@test ((@MMatrix eye(Float32, 2, 2))::MMatrix{2, 2, Float32}).data === (1.0f0, 0.0f0, 0.0f0, 1.0f0)
5555
@test isa(@MMatrix(rand(Float32, 2, 2)), MMatrix{2, 2, Float32})
5656
@test isa(@MMatrix(randn(Float32, 2, 2)), MMatrix{2, 2, Float32})
57+
58+
@test MMatrix(SMatrix{1,1,Int,1}((1,))).data == (1,)
5759
end
5860

5961
@testset "Methods" begin
@@ -87,5 +89,12 @@
8789
m[3] = 13
8890
m[4] = 14
8991
@test m.data === (11, 12, 13, 14)
92+
93+
m = @MMatrix [0 0; 0 0]
94+
m[1] = Int8(11)
95+
m[2] = Int8(12)
96+
m[3] = Int8(13)
97+
m[4] = Int8(14)
98+
@test m.data === (11, 12, 13, 14)
9099
end
91100
end

test/MVector.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,12 @@
6262
v[2] = 12
6363
v[3] = 13
6464
@test v.data === (11, 12, 13)
65+
66+
v = @MVector [1.,2.,3.]
67+
v[1] = Float16(11)
68+
@test v.data === (11., 2., 3.)
69+
70+
@test_throws BoundsError setindex!(v, 4., -1)
71+
@test_throws BoundsError setindex!(v, 4., 4)
6572
end
6673
end

test/SUnitRange.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
@test length(StaticArrays.SUnitRange(1,-10)) === 0
44

55
@test StaticArrays.SUnitRange(2,4)[2] === 3
6+
7+
@test_throws BoundsError StaticArrays.SUnitRange(1,3)[4]
8+
@test_throws Exception StaticArrays.SUnitRange{1, -1}()
9+
@test_throws TypeError StaticArrays.SUnitRange{1, 1.5}()
610
end

test/Scalar.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
@test Scalar(2) .* [1, 2, 3] == [2, 4, 6]
33
@test Scalar([1 2; 3 4]) .+ [[1 1; 1 1], [2 2; 2 2]] == [[2 3; 4 5], [3 4; 5 6]]
44
@test (Scalar(1) + Scalar(1.0))::Scalar{Float64} Scalar(2.0)
5+
@test_throws ErrorException Scalar(2)[2]
6+
@test Scalar(2)[] == 2
7+
@test Tuple(Scalar(2)) == (2,)
58
end

test/SizedArray.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@testset "SizedArray" begin
22
@testset "Inner Constructors" begin
3+
@test SizedArray{Tuple{2}, Int, 1}((3, 4)).data == [3, 4]
34
@test SizedArray{Tuple{2}, Int, 1}([3, 4]).data == [3, 4]
45
@test SizedArray{Tuple{2, 2}, Int, 2}(collect(3:6)).data == collect(3:6)
56
@test size(SizedArray{Tuple{4, 5}, Int, 2}().data) == (4, 5)
@@ -11,5 +12,19 @@
1112
# Bad parameters
1213
@test_throws Exception SizedArray{Tuple{1},Int,2}()
1314
@test_throws Exception SArray{Tuple{3, 4},Int,1}()
15+
16+
# Parameter/input size mismatch
17+
@test_throws Exception SizedArray{Tuple{1},Int,2}([2; 3])
18+
@test_throws Exception SizedArray{Tuple{1},Int,2}((2, 3))
19+
end
20+
21+
# setindex
22+
sa = SizedArray{Tuple{2}, Int, 1}([3, 4])
23+
sa[1] = 2
24+
@test sa.data == [2, 4]
25+
26+
@testset "back to Array" begin
27+
@test Array(SizedArray{Tuple{2}, Int, 1}([3, 4])) == [3, 4]
28+
@test Vector(SizedArray{Tuple{4}, Int, 1}(collect(3:6))) == collect(3:6)
1429
end
1530
end

0 commit comments

Comments
 (0)