Skip to main content

Timeline for Inverting a 4x4 matrix

Current License: CC BY-SA 3.0

21 events
when toggle format what by license comment
Mar 9, 2022 at 3:14 comment added anymous.asker I should add, this code works very well in general, but it will produce quite incorrect results when the determinant of the matrix is low (say, <= 1e-4).
Jan 7, 2020 at 1:23 comment added fluffy @whn Where are the access patterns that can be turned into unrollable loops in a way that the compiler would be likely to be able to unroll this concisely? Also why ping me specifically on a comment I posted on an answer 9 years ago?
Jan 6, 2020 at 20:31 comment added Krupip @fluffy This is just using determinants isn't it? This should be fairly "loopable".
Sep 7, 2018 at 14:56 comment added PhilT @BrynnMahsman From the looks of it, it's based on Minors, cofactors and adjugate - mathsisfun.com/algebra/…. The other method would be column and row operations but I think this would be difficult to code as you'd have to use conditional logic
May 14, 2017 at 23:08 comment added Alundaio I don't understand, this code is moving m12,m13,m14 to m3,m7,m11 where other algorithms will just leave the last column in place.
May 2, 2016 at 12:18 comment added Bim Strainghtforward implementation. @crazyjoe: You could move the calculation of inv[0], inv[4], inv[8], inv[12] to the top and calculate the determinant earlier, so you optimize the non-invertible path too...
Aug 28, 2014 at 19:10 comment added Alexandre C. Comatrix-based inversion like this is going to be very prone to roundoff error (look at all the potential for catastrophic cancellation). Stabler versions include branches (ie. pivoting) however, so this is kind of a trade-off. I suspect that Mesa programmers prefer performance here.
Jul 31, 2014 at 18:22 comment added crazyjoe @dfeuer It would take 72 multiplications to get the determinant first. While doing it first would actually be faster in case of non-invertible matrix, for an invertible matrix it saves 68 multiplications by doing it after. Also there's the bonus of having less code this way.
Jun 4, 2014 at 8:20 comment added user234736 @Imagist you can check my answer if you're looking for a 'looping variant'. Aggressive compiler optimisations won't fix the performance hit...
May 22, 2014 at 12:30 comment added dfeuer Why not calculate the determinant first? Is this attempting realtime consistency or something?
S Feb 15, 2012 at 21:35 history suggested rodrigo-silveira CC BY-SA 3.0
Formatted code so it's easier to read - especially on a mobile device
Feb 15, 2012 at 21:17 review Suggested edits
S Feb 15, 2012 at 21:35
Feb 15, 2012 at 20:52 comment added Timmmm Zoomulator: Awesomely it is for both! This is because inverse(transpose(A)) = transpose(inverse(A)).
Feb 15, 2012 at 20:46 comment added Timmmm Imagist: It wasn't derived from an unrolled loop. That really is the simplest way of writing it.
Jan 25, 2012 at 19:00 comment added Zoomulator Is this for column major or row major ordered matrices?
Aug 22, 2011 at 20:18 comment added fluffy Sadly, that code isn't really that straightforward to make in a loopable way to begin with, much less a way that a compiler can adequately unroll. Also, that code comes from a rather old C library which has a LOT of very fiddly optimizations, and it's code that works already (and has been thoroughly tested and proven by thousands of Linux OpenGL programs at this point) so why rewrite it?
Aug 2, 2011 at 13:59 comment added Maja Piechotka Even if they wouldn't that might be time to invest into metaprogramming (here's place for loop). I workarounded one compiler which did not unroll trivial loops by using m4.
Jul 19, 2009 at 5:34 comment added Imagist Yes I would. Compilers are perfectly capable of unrolling loops, especially when you tell them to.
Jul 19, 2009 at 0:06 comment added shoosh You probably wouldn't want it any other way.
Jul 18, 2009 at 20:46 vote accept clamp
Jul 18, 2009 at 19:44 history answered shoosh CC BY-SA 2.5