Skip to content

Commit

Permalink
Added some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ferris committed Feb 16, 2017
1 parent 5177a27 commit e600e89
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
29 changes: 4 additions & 25 deletions src/linalg.jl
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand All @@ -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)]
Expand Down
6 changes: 2 additions & 4 deletions test/FieldVector.jl
Expand Up @@ -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)

Expand All @@ -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;
Expand All @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions test/arraymath.jl
Expand Up @@ -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
36 changes: 36 additions & 0 deletions test/linalg.jl
Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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

0 comments on commit e600e89

Please sign in to comment.