|
1 |
| -@inline det(A::StaticMatrix) = _det(Size(A), A) |
2 |
| -@inline logdet(A::StaticMatrix) = _logdet(Size(A), A) |
| 1 | +@inline function det(A::StaticMatrix) |
| 2 | + T = eltype(A) |
| 3 | + S = typeof((one(T)*zero(T) + zero(T))/one(T)) |
| 4 | + _det(Size(A),A,S) |
| 5 | +end |
3 | 6 |
|
4 |
| -@inline _det(::Size{(1,1)}, A::StaticMatrix) = @inbounds return A[1] |
| 7 | +@inline logdet(A::StaticMatrix) = log(det(A)) |
5 | 8 |
|
6 |
| -@inline function _det(::Size{(2,2)}, A::StaticMatrix) |
7 |
| - @inbounds return A[1]*A[4] - A[3]*A[2] |
8 |
| -end |
| 9 | +@inline _det(::Size{(1,1)}, A::StaticMatrix,S::Type) = @inbounds return convert(S,A[1]) |
9 | 10 |
|
10 |
| -@inline function _det(::Size{(2,2)}, A::StaticMatrix{<:Any, <:Any, <:Unsigned}) |
11 |
| - @inbounds return Signed(A[1]*A[4]) - Signed(A[3]*A[2]) |
| 11 | +@inline function _det(::Size{(2,2)}, A::StaticMatrix, S::Type) |
| 12 | + A = similar_type(A,S)(A) |
| 13 | + @inbounds return A[1]*A[4] - A[3]*A[2] |
12 | 14 | end
|
13 | 15 |
|
14 |
| -@inline function _det(::Size{(3,3)}, A::StaticMatrix) |
| 16 | +@inline function _det(::Size{(3,3)}, A::StaticMatrix, S::Type) |
| 17 | + A = similar_type(A,S)(A) |
15 | 18 | @inbounds x0 = SVector(A[1], A[2], A[3])
|
16 | 19 | @inbounds x1 = SVector(A[4], A[5], A[6])
|
17 | 20 | @inbounds x2 = SVector(A[7], A[8], A[9])
|
18 | 21 | return vecdot(x0, cross(x1, x2))
|
19 | 22 | end
|
20 | 23 |
|
21 |
| -@inline function _det(::Size{(3,3)}, A::StaticMatrix{<:Any, <:Any, <:Unsigned}) |
22 |
| - @inbounds x0 = SVector(Signed(A[1]), Signed(A[2]), Signed(A[3])) |
23 |
| - @inbounds x1 = SVector(Signed(A[4]), Signed(A[5]), Signed(A[6])) |
24 |
| - @inbounds x2 = SVector(Signed(A[7]), Signed(A[8]), Signed(A[9])) |
25 |
| - return vecdot(x0, cross(x1, x2)) |
| 24 | +@inline function _det(::Size, A::StaticMatrix,::Type) |
| 25 | + return det(Matrix(A)) |
26 | 26 | end
|
27 |
| - |
28 |
| -@inline _logdet(S::Union{Size{(1,1)},Size{(2,2)},Size{(3,3)}}, A::StaticMatrix) = log(_det(S, A)) |
29 |
| - |
30 |
| -for (symb, f) in [(:_det, :det), (:_logdet, :logdet)] |
31 |
| - eval(quote |
32 |
| - @generated function $symb{S}(::Size{S}, A::StaticMatrix) |
33 |
| - if S[1] != S[2] |
34 |
| - throw(DimensionMismatch("matrix is not square")) |
35 |
| - end |
36 |
| - return quote # Implementation from Base |
37 |
| - @_inline_meta |
38 |
| - T = eltype(A) |
39 |
| - T2 = typeof((one(T)*zero(T) + zero(T))/one(T)) |
40 |
| - if istriu(A) || istril(A) |
41 |
| - return convert(T2, $($f)(UpperTriangular(A))) # Is this a Julia bug that a convert is not type stable?? |
42 |
| - end |
43 |
| - AA = convert(Array{T2}, A) |
44 |
| - return $($f)(lufact(AA)) |
45 |
| - end |
46 |
| - end |
47 |
| - end) |
48 |
| -end |
49 |
| - |
0 commit comments