|
1 |
| -@inline inv(A::StaticMatrix) = _inv(Size(A), A) |
2 |
| - |
3 |
| -@inline _inv(::Size{(1,1)}, A) = similar_type(typeof(A), typeof(inv(one(eltype(A)))))(inv(A[1])) |
4 |
| - |
5 |
| -@inline function _inv(::Size{(2,2)}, A) |
| 1 | +@inline function inv(A::StaticMatrix) |
6 | 2 | T = eltype(A)
|
7 | 3 | S = typeof((one(T)*zero(T) + zero(T))/one(T))
|
8 |
| - newtype = similar_type(A, S) |
| 4 | + _inv(Size(A),A,S) |
| 5 | +end |
| 6 | + |
| 7 | +@inline _inv(::Size{(1,1)}, A, S::Type) = inv(A[1]) |
9 | 8 |
|
| 9 | +@inline function _inv(::Size{(2,2)}, A, S::Type) |
| 10 | + newtype = similar_type(A, S) |
10 | 11 | d = det(A)
|
11 | 12 | @inbounds return newtype((A[4]/d, -(A[2]/d), -(A[3]/d), A[1]/d))
|
12 | 13 | end
|
13 | 14 |
|
14 |
| -@inline function _inv(::Size{(3,3)}, A) |
15 |
| - T = eltype(A) |
16 |
| - S = typeof((one(T)*zero(T) + zero(T))/one(T)) |
| 15 | +@inline function _inv(::Size{(3,3)}, A,S::Type) |
17 | 16 | newtype = similar_type(A, S)
|
18 | 17 |
|
19 | 18 | @inbounds x0 = SVector{3,S}(A[1], A[2], A[3])
|
|
30 | 29 | @inbounds return newtype((y0[1], y1[1], y2[1], y0[2], y1[2], y2[2], y0[3], y1[3], y2[3]))
|
31 | 30 | end
|
32 | 31 |
|
33 |
| -@inline function _inv(::Size{(4,4)}, A) |
| 32 | +@inline function _inv(::Size{(4,4)}, A, S::Type) |
| 33 | + A = similar_type(A,S)(A) |
34 | 34 | idet = 1/det(A)
|
35 | 35 | B = @SMatrix [
|
36 | 36 | (A[2,3]*A[3,4]*A[4,2] - A[2,4]*A[3,3]*A[4,2] + A[2,4]*A[3,2]*A[4,3] - A[2,2]*A[3,4]*A[4,3] - A[2,3]*A[3,2]*A[4,4] + A[2,2]*A[3,3]*A[4,4]) * idet
|
|
52 | 52 | (A[1,3]*A[2,4]*A[3,1] - A[1,4]*A[2,3]*A[3,1] + A[1,4]*A[2,1]*A[3,3] - A[1,1]*A[2,4]*A[3,3] - A[1,3]*A[2,1]*A[3,4] + A[1,1]*A[2,3]*A[3,4]) * idet
|
53 | 53 | (A[1,4]*A[2,2]*A[3,1] - A[1,2]*A[2,4]*A[3,1] - A[1,4]*A[2,1]*A[3,2] + A[1,1]*A[2,4]*A[3,2] + A[1,2]*A[2,1]*A[3,4] - A[1,1]*A[2,2]*A[3,4]) * idet
|
54 | 54 | (A[1,2]*A[2,3]*A[3,1] - A[1,3]*A[2,2]*A[3,1] + A[1,3]*A[2,1]*A[3,2] - A[1,1]*A[2,3]*A[3,2] - A[1,2]*A[2,1]*A[3,3] + A[1,1]*A[2,2]*A[3,3]) * idet]
|
55 |
| - return SMatrix{4,4,eltype(B),16}(B) |
| 55 | + return A = similar_type(A,S)(B) |
56 | 56 | end
|
57 | 57 |
|
58 |
| -@inline function _inv(::Size, A) |
59 |
| - T = eltype(A) |
60 |
| - S = typeof((one(T)*zero(T) + zero(T))/one(T)) |
61 |
| - AA = convert(Array{S}, A) # lufact() doesn't work with StaticArrays at the moment... and the branches below must be type-stable |
62 |
| - if istriu(A) |
63 |
| - Ai_ut = inv(UpperTriangular(AA)) |
64 |
| - # TODO double check these routines leave the parent in a clean (upper triangular) state |
65 |
| - return Size(A)(parent(Ai_ut)) # Return a `SizedArray` |
66 |
| - elseif istril(A) |
67 |
| - Ai_lt = inv(LowerTriangular(AA)) |
68 |
| - # TODO double check these routines leave the parent in a clean (lower triangular) state |
69 |
| - return Size(A)(parent(Ai_lt)) # Return a `SizedArray` |
70 |
| - else |
71 |
| - Ai_lu = inv(lufact(AA)) |
72 |
| - return Size(A)(Ai_lu) # Return a `SizedArray` |
73 |
| - end |
| 58 | +@inline function _inv(::Size, A ,::Type) |
| 59 | + inv(Matrix(A)) |
74 | 60 | end
|
0 commit comments