diff --git a/src/linalg.jl b/src/linalg.jl index cd9ff9c81..07f9f8698 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -54,7 +54,7 @@ end error("Dimension mismatch") end - exprs = [i == j ? :(a.λ + b[$(sub2ind(size(a), i, j))]) : :(b[$(sub2ind(size(a), i, j))]) for i = 1:n, j = 1:n] + exprs = [i == j ? :(a.λ + b[$(sub2ind(size(b), i, j))]) : :(b[$(sub2ind(size(b), i, j))]) for i = 1:n, j = 1:n] return quote $(Expr(:meta, :inline)) @@ -86,7 +86,7 @@ end error("Dimension mismatch") end - exprs = [i == j ? :(a.λ - b[$(sub2ind(size(a), i, j))]) : :(-b[$(sub2ind(size(a), i, j))]) for i = 1:n, j = 1:n] + exprs = [i == j ? :(a.λ - b[$(sub2ind(size(b), i, j))]) : :(-b[$(sub2ind(size(b), i, j))]) for i = 1:n, j = 1:n] return quote $(Expr(:meta, :inline)) @@ -220,6 +220,7 @@ end @inline Base.zero{SA <: StaticArray}(a::SA) = zeros(SA) @inline Base.zero{SA <: StaticArray}(a::Type{SA}) = zeros(SA) +@inline one{SM <: StaticMatrix}(::SM) = one(SM) @generated function one{SM <: StaticArray}(::Type{SM}) s = size(SM) if (length(s) != 2) || (s[1] != s[2]) @@ -236,19 +237,7 @@ end end end -@generated function one{SM <: StaticMatrix}(::SM) - s = size(SM) - if s[1] != s[2] - error("multiplicative identity defined only for square matrices") - end - T = eltype(SM) - e = eye(T, s...) - return quote - $(Expr(:meta, :inline)) - $(Expr(:call, SM, Expr(:tuple, e...))) - end -end - +@inline eye{SM <: StaticMatrix}(::SM) = eye(SM) @generated function eye{SM <: StaticArray}(::Type{SM}) s = size(SM) if length(s) != 2 @@ -265,16 +254,6 @@ end end end -@generated function eye{SM <: StaticMatrix}(::SM) - s = size(SM) - T = eltype(SM) - e = eye(T, s...) - return quote - $(Expr(:meta, :inline)) - $(Expr(:call, SM, Expr(:tuple, e...))) - end -end - @generated function diagm(v::StaticVector) T = eltype(v) exprs = [i == j ? :(v[$i]) : zero(T) for i = 1:length(v), j = 1:length(v)] diff --git a/test/FieldVector.jl b/test/FieldVector.jl index 8dc8d533e..fce38f2f1 100644 --- a/test/FieldVector.jl +++ b/test/FieldVector.jl @@ -7,8 +7,6 @@ z::Float64 end - @inline Point3D(xyz::NTuple{3,Float64}) = Point3D(xyz[1], xyz[2], xyz[3]) - StaticArrays.similar_type(::Type{Point3D}, ::Type{Float64}, s::Size{(3,)}) = Point3D end) @@ -20,6 +18,8 @@ @test length(p) === 3 @test eltype(p) === Float64 + @test (p + p)::Point3D ≈ Point3D(2.0, 4.0, 6.0) + @test (p[1], p[2], p[3]) === (p.x, p.y, p.z) m = @SMatrix [2.0 0.0 0.0; @@ -42,8 +42,6 @@ y::T end - @inline Point2D(xy::NTuple{2,Any}) = Point2D(xy[1], xy[2]) - StaticArrays.similar_type{P2D<:Point2D,T}(::Type{P2D}, ::Type{T}, s::Size{(2,)}) = Point2D{T} end) diff --git a/test/arraymath.jl b/test/arraymath.jl index c6750b365..a3d054fee 100644 --- a/test/arraymath.jl +++ b/test/arraymath.jl @@ -63,4 +63,10 @@ @test all(@inferred(fill(3., SMatrix{4, 16, Float64})) .== 3.) @test @allocated(fill(0., SMatrix{1, 16, Float64})) == 0 # #81 end + + @testset "fill!()" begin + m = MMatrix{4,16,Float64}() + fill!(m, 3) + @test all(m .== 3.) + end end diff --git a/test/linalg.jl b/test/linalg.jl index 8fced674d..34cd86507 100644 --- a/test/linalg.jl +++ b/test/linalg.jl @@ -9,9 +9,30 @@ @test @inferred(v1 - c) === @SVector [0,2,4,6] @test @inferred(v1 * c) === @SVector [4,8,12,16] @test @inferred(v1 / c) === @SVector [1.0,2.0,3.0,4.0] + @test @inferred(c \ v1)::SVector ≈ @SVector [1.0,2.0,3.0,4.0] @test @inferred(v1 + v2) === @SVector [6, 7, 8, 9] @test @inferred(v1 - v2) === @SVector [-2, 1, 4, 7] + + v3 = [2,4,6,8] + v4 = [4,3,2,1] + + @test @inferred(v1 - v4) === @SVector [-2, 1, 4, 7] + @test @inferred(v3 - v2) === @SVector [-2, 1, 4, 7] + @test @inferred(v1 - v4) === @SVector [-2, 1, 4, 7] + @test @inferred(v3 - v2) === @SVector [-2, 1, 4, 7] + end + + @testset "Interaction with `UniformScaling`" begin + @test @inferred(@SMatrix([0 1; 2 3]) + I) === @SMatrix [1 1; 2 4] + @test @inferred(I + @SMatrix([0 1; 2 3])) === @SMatrix [1 1; 2 4] + @test @inferred(@SMatrix([0 1; 2 3]) - I) === @SMatrix [-1 1; 2 2] + @test @inferred(I - @SMatrix([0 1; 2 3])) === @SMatrix [1 -1; -2 -2] + + @test @inferred(@SMatrix([0 1; 2 3]) * I) === @SMatrix [0 1; 2 3] + @test @inferred(I * @SMatrix([0 1; 2 3])) === @SMatrix [0 1; 2 3] + @test @inferred(@SMatrix([0 1; 2 3]) / I) === @SMatrix [0.0 1.0; 2.0 3.0] + @test @inferred(I \ @SMatrix([0 1; 2 3])) === @SMatrix [0.0 1.0; 2.0 3.0] end @testset "diagm()" begin @@ -22,6 +43,7 @@ @test @inferred(one(SMatrix{2,2,Int})) === @SMatrix [1 0; 0 1] @test @inferred(one(SMatrix{2,2})) === @SMatrix [1.0 0.0; 0.0 1.0] @test @inferred(one(SMatrix{2})) === @SMatrix [1.0 0.0; 0.0 1.0] + @test @inferred(one(one(SMatrix{2,2,Int}))) === @SMatrix [1 0; 0 1] @test @inferred(one(MMatrix{2,2,Int}))::MMatrix == @MMatrix [1 0; 0 1] @test @inferred(one(MMatrix{2,2}))::MMatrix == @MMatrix [1.0 0.0; 0.0 1.0] @@ -32,6 +54,7 @@ @test @inferred(eye(SMatrix{2,2,Int})) === @SMatrix [1 0; 0 1] @test @inferred(eye(SMatrix{2,2})) === @SMatrix [1.0 0.0; 0.0 1.0] @test @inferred(eye(SMatrix{2})) === @SMatrix [1.0 0.0; 0.0 1.0] + @test @inferred(eye(eye(SMatrix{2,2,Int}))) === @SMatrix [1 0; 0 1] @test @inferred(eye(SMatrix{2,3,Int})) === @SMatrix [1 0 0; 0 1 0] @test @inferred(eye(SMatrix{2,3})) === @SMatrix [1.0 0.0 0.0; 0.0 1.0 0.0] @@ -43,6 +66,8 @@ @testset "cross()" begin @test @inferred(cross(SVector(1,2,3), SVector(4,5,6))) === SVector(-3, 6, -3) + @test @inferred(cross(SVector(1,2), SVector(4,5))) === -3 + end @testset "transpose() and conj()" begin @@ -58,6 +83,10 @@ end @testset "vcat() and hcat()" begin + @test @inferred(vcat(SVector(1,2,3))) === SVector(1,2,3) + @test @inferred(hcat(SVector(1,2,3))) === SMatrix{3,1}(1,2,3) + @test @inferred(hcat(SMatrix{3,1}(1,2,3))) === SMatrix{3,1}(1,2,3) + @test @inferred(vcat(SVector(1,2,3), SVector(4,5,6))) === SVector(1,2,3,4,5,6) @test @inferred(hcat(SVector(1,2,3), SVector(4,5,6))) === @SMatrix [1 4; 2 5; 3 6] @@ -68,6 +97,9 @@ @test @inferred(vcat(@SMatrix([1;2;3]), @SMatrix([4;5;6]))) === @SMatrix([1;2;3;4;5;6]) @test @inferred(hcat(@SMatrix([1;2;3]), @SMatrix([4;5;6]))) === @SMatrix [1 4; 2 5; 3 6] + + @test @inferred(vcat(SVector(1),SVector(2),SVector(3),SVector(4))) === SVector(1,2,3,4) + @test @inferred(hcat(SVector(1),SVector(2),SVector(3),SVector(4))) === SMatrix{1,4}(1,2,3,4) end @testset "normalization" begin @@ -82,4 +114,8 @@ @test normalize(SVector(1,2,3)) ≈ normalize([1,2,3]) end + + @testset "trace" begin + @test trace(@SMatrix [1.0 2.0; 3.0 4.0]) ≈ 5.0 + end end