Skip to content

Commit 4da8330

Browse files
author
Andy Ferris
committed
Made det tests and safe for Unsigned
Half of #29
1 parent 7562c3f commit 4da8330

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/det.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66
@inbounds return A[1]*A[4] - A[3]*A[2]
77
end
88

9+
@inline function _det{T<:Unsigned}(::Size{(2,2)}, A::AbstractMatrix{T})
10+
@inbounds return Signed(A[1]*A[4]) - Signed(A[3]*A[2])
11+
end
12+
913
@inline function _det(::Size{(3,3)}, A::AbstractMatrix)
1014
@inbounds x0 = SVector(A[1], A[2], A[3])
1115
@inbounds x1 = SVector(A[4], A[5], A[6])
1216
@inbounds x2 = SVector(A[7], A[8], A[9])
1317
return vecdot(x0, cross(x1, x2))
1418
end
1519

20+
@inline function _det{T<:Unsigned}(::Size{(3,3)}, A::AbstractMatrix{T})
21+
@inbounds x0 = SVector(Signed(A[1]), Signed(A[2]), Signed(A[3]))
22+
@inbounds x1 = SVector(Signed(A[4]), Signed(A[5]), Signed(A[6]))
23+
@inbounds x2 = SVector(Signed(A[7]), Signed(A[8]), Signed(A[9]))
24+
return vecdot(x0, cross(x1, x2))
25+
end
26+
1627
@generated function _det{S,T}(::Size{S}, A::AbstractMatrix{T})
1728
if S[1] != S[2]
1829
throw(DimensionMismatch("matrix is not square"))

test/det.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@testset "Determinant" begin
2+
@test det(@SMatrix [1]) === 1
3+
@test det(@SMatrix [0 1; 1 0]) === -1
4+
@test det(@SMatrix [0 1 0; 1 0 0; 0 0 1]) === -1
5+
m = randn(Float64, 4,4)
6+
@test det(SMatrix{4,4}(m)) det(m)
7+
8+
# Unsigned specializations
9+
@test det(@SMatrix [0x00 0x01; 0x01 0x00])::Int8 == -1
10+
@test det(@SMatrix [0x00 0x01 0x00; 0x01 0x00 0x00; 0x00 0x00 0x01])::Int8 == -1
11+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using Base.Test
1818
include("arraymath.jl")
1919
include("linalg.jl")
2020
include("matrix_multiply.jl")
21+
include("det.jl")
2122
include("solve.jl")
2223
include("eigen.jl")
2324
include("deque.jl")

0 commit comments

Comments
 (0)