Skip to content

Commit e578b9b

Browse files
committed
Avoid variable name changing type, moved promotion to dispatching function
1 parent cc34e5e commit e578b9b

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

src/det.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
@inline function det(A::StaticMatrix)
22
T = eltype(A)
33
S = typeof((one(T)*zero(T) + zero(T))/one(T))
4-
_det(Size(A),A,S)
4+
A_S = similar_type(A,S)(A)
5+
_det(Size(A_S),A_S)
56
end
67

78
@inline logdet(A::StaticMatrix) = log(det(A))
89

9-
@inline _det(::Size{(1,1)}, A::StaticMatrix,S::Type) = @inbounds return convert(S,A[1])
10+
@inline _det(::Size{(1,1)}, A::StaticMatrix) = @inbounds return A[1]
1011

11-
@inline function _det(::Size{(2,2)}, A::StaticMatrix, S::Type)
12-
A = similar_type(A,S)(A)
12+
@inline function _det(::Size{(2,2)}, A::StaticMatrix)
1313
@inbounds return A[1]*A[4] - A[3]*A[2]
1414
end
1515

16-
@inline function _det(::Size{(3,3)}, A::StaticMatrix, S::Type)
17-
A = similar_type(A,S)(A)
16+
@inline function _det(::Size{(3,3)}, A::StaticMatrix)
1817
@inbounds x0 = SVector(A[1], A[2], A[3])
1918
@inbounds x1 = SVector(A[4], A[5], A[6])
2019
@inbounds x2 = SVector(A[7], A[8], A[9])
2120
return vecdot(x0, cross(x1, x2))
2221
end
2322

24-
@inline function _det(::Size{(4,4)}, A::StaticMatrix,S::Type)
25-
A = similar_type(A,S)(A)
23+
@inline function _det(::Size{(4,4)}, A::StaticMatrix)
2624
@inbounds return (
2725
A[13] * A[10] * A[7] * A[4] - A[9] * A[14] * A[7] * A[4] -
2826
A[13] * A[6] * A[11] * A[4] + A[5] * A[14] * A[11] * A[4] +
@@ -38,6 +36,6 @@ end
3836
A[5] * A[2] * A[11] * A[16] + A[1] * A[6] * A[11] * A[16])
3937
end
4038

41-
@inline function _det(::Size, A::StaticMatrix,::Type)
39+
@inline function _det(::Size, A::StaticMatrix)
4240
return det(Matrix(A))
4341
end

src/inv.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
@inline function inv(A::StaticMatrix)
22
T = eltype(A)
33
S = typeof((one(T)*zero(T) + zero(T))/one(T))
4-
_inv(Size(A),A,S)
4+
A_S = similar_type(A,S)(A)
5+
_inv(Size(A_S),A_S)
56
end
67

7-
@inline _inv(::Size{(1,1)}, A, S::Type) = similar_type(A,S)(inv(A[1]))
8+
@inline _inv(::Size{(1,1)}, A) = similar_type(A)(inv(A[1]))
89

9-
@inline function _inv(::Size{(2,2)}, A, S::Type)
10-
newtype = similar_type(A,S)
11-
A = newtype(A)
10+
@inline function _inv(::Size{(2,2)}, A)
11+
newtype = similar_type(A)
1212
idet = 1/det(A)
1313
@inbounds return newtype((A[4]*idet, -(A[2]*idet), -(A[3]*idet), A[1]*idet))
1414
end
1515

16-
@inline function _inv(::Size{(3,3)}, A,S::Type)
17-
newtype = similar_type(A, S)
16+
@inline function _inv(::Size{(3,3)}, A)
17+
newtype = similar_type(A)
1818

19-
@inbounds x0 = SVector{3,S}(A[1], A[2], A[3])
20-
@inbounds x1 = SVector{3,S}(A[4], A[5], A[6])
21-
@inbounds x2 = SVector{3,S}(A[7], A[8], A[9])
19+
@inbounds x0 = SVector{3}(A[1], A[2], A[3])
20+
@inbounds x1 = SVector{3}(A[4], A[5], A[6])
21+
@inbounds x2 = SVector{3}(A[7], A[8], A[9])
2222

2323
y0 = cross(x1,x2)
2424
d = vecdot(x0, y0)
@@ -30,8 +30,7 @@ end
3030
@inbounds return newtype((y0[1], y1[1], y2[1], y0[2], y1[2], y2[2], y0[3], y1[3], y2[3]))
3131
end
3232

33-
@inline function _inv(::Size{(4,4)}, A, S::Type)
34-
A = similar_type(A,S)(A)
33+
@inline function _inv(::Size{(4,4)}, A)
3534
idet = 1/det(A)
3635
B = @SMatrix [
3736
(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
@@ -53,9 +52,9 @@ end
5352
(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
5453
(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
5554
(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]
56-
return A = similar_type(A,S)(B)
55+
return similar_type(A)(B)
5756
end
5857

59-
@inline function _inv(::Size, A , S::Type)
60-
similar_type(A,S)(inv(Matrix(A)))
58+
@inline function _inv(::Size, A)
59+
similar_type(A)(inv(Matrix(A)))
6160
end

0 commit comments

Comments
 (0)