Skip to content

Commit 139df3f

Browse files
Rewrite 4x4 det as product of 2x2 minors for improved performance (#1046)
* Rewrite 4x4 det as product of 2x2 minors * Update src/det.jl Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent 29a76ec commit 139df3f

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/det.jl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@ end
1818
return bilinear_vecdot(x0, cross(x1, x2))
1919
end
2020

21+
@inline function _prod2x2minors(A, a,b,c,d)
22+
@inbounds return (A[a]*A[b+4] - A[b]*A[a+4])*(A[c+8]*A[d+12] - A[c+12]*A[d+8])
23+
end
24+
2125
@inline function _det(::Size{(4,4)}, A::StaticMatrix)
22-
@inbounds return (
23-
A[13] * A[10] * A[7] * A[4] - A[9] * A[14] * A[7] * A[4] -
24-
A[13] * A[6] * A[11] * A[4] + A[5] * A[14] * A[11] * A[4] +
25-
A[9] * A[6] * A[15] * A[4] - A[5] * A[10] * A[15] * A[4] -
26-
A[13] * A[10] * A[3] * A[8] + A[9] * A[14] * A[3] * A[8] +
27-
A[13] * A[2] * A[11] * A[8] - A[1] * A[14] * A[11] * A[8] -
28-
A[9] * A[2] * A[15] * A[8] + A[1] * A[10] * A[15] * A[8] +
29-
A[13] * A[6] * A[3] * A[12] - A[5] * A[14] * A[3] * A[12] -
30-
A[13] * A[2] * A[7] * A[12] + A[1] * A[14] * A[7] * A[12] +
31-
A[5] * A[2] * A[15] * A[12] - A[1] * A[6] * A[15] * A[12] -
32-
A[9] * A[6] * A[3] * A[16] + A[5] * A[10] * A[3] * A[16] +
33-
A[9] * A[2] * A[7] * A[16] - A[1] * A[10] * A[7] * A[16] -
34-
A[5] * A[2] * A[11] * A[16] + A[1] * A[6] * A[11] * A[16])
26+
@inbounds return (_prod2x2minors(A,1,2,3,4) - _prod2x2minors(A,1,3,2,4) + _prod2x2minors(A,1,4,2,3)
27+
+_prod2x2minors(A,2,3,1,4) - _prod2x2minors(A,2,4,1,3) + _prod2x2minors(A,3,4,1,2))
3528
end
3629

30+
3731
@inline function _parity(p) # inefficient compared to computing cycle lengths, but non-allocating
3832
s = 0
3933
for i in 1:length(p), j in i+1:length(p)

0 commit comments

Comments
 (0)