Recent

Author Topic: Fix matrix inverse algorithm  (Read 2587 times)

array81

  • Newbie
  • Posts: 3
Fix matrix inverse algorithm
« on: August 12, 2018, 02:02:53 pm »
I have found this unit: http://www.efg2.com/Lab/Library/Delphi/Graphics/GraphicsMathLibrary.PAS.TXT

I think InvertMatrix have a bug: the inverse matrix items are in wrong position. All the inverse matrix items value are right but they position are wrong.

Any idea how can I fix it?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Fix matrix inverse algorithm
« Reply #1 on: August 12, 2018, 02:13:28 pm »
doubt it, it would be a bit more helpful if you were to give us a couple of examples that it fails what you expect, what you get etc. There is also a nice little matrix library at https://paginas.fe.up.pt/~paco/pmwiki/index.php?n=DynMatrix.DynMatrix that you can use for validation.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Fix matrix inverse algorithm
« Reply #2 on: August 12, 2018, 02:14:18 pm »
Why would you think this 20+ year old code still contains a bug? That is highly unlikely.
What is the orientation you expect? i.e. what processing order for X and Y. Natural order is row first, column second. Which is what the code does.
The code also contains rotation, you can use that.
« Last Edit: August 12, 2018, 02:22:54 pm by Thaddy »
Specialize a type, not a var.

array81

  • Newbie
  • Posts: 3
Re: Fix matrix inverse algorithm
« Reply #3 on: August 12, 2018, 02:24:17 pm »
doubt it, it would be a bit more helpful if you were to give us a couple of examples that it fails what you expect, what you get etc. There is also a nice little matrix library at https://paginas.fe.up.pt/~paco/pmwiki/index.php?n=DynMatrix.DynMatrix that you can use for validation.

This is a test matrix:
   (3,00, 4,00, 1,00, 3,00)
   (2,00, 7,00, 6,00, 4,00)
   (3,00, 5,00, 4,00, 8,00)
   (6,00, 7,00, 4,00, 9,00)

This is the result of InvertMatrix function:
   (-0,14, -0,21, 0,13, 0,38)
   (0,22, 0,53, -0,80, -0,41)
   (0,03, 0,77, -0,56, -0,68)
   (0,03, -0,56, 0,77, 0,32)

This is the result get from many online tools (I have try 3 tools an I get always the same result):
   (-0.56, 0.03, -0.68, 0.77)
   (0.77, 0.03, 0.32, -0.56)
   (-0.80, 0.22, -0.41, 0.53)
   (0.13, -0.14, 0.38, -0.21)

As you can see all items value are right but position is wrong:

Right position -> Wrong position
11 -> 42
12 -> 44
13 -> 41
14 -> 43
21 -> 32
22 -> 34
23 -> 31
24 -> 33
31 -> 12
32 -> 14
33 -> 11
34 -> 13
41 -> 22
42 -> 24
43 -> 21
44 -> 23

Note: the determinant is right.
« Last Edit: August 12, 2018, 02:52:08 pm by array81 »

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Fix matrix inverse algorithm
« Reply #4 on: August 12, 2018, 04:22:28 pm »
I can confirm your values. But I doubt if this inversion routine can be used for general 4x4 matrices. It seems that the whole unit is designed for 2d and 3d homogenous operations (e.g.  a 3D "graphics" matrix is a  4x4 matrix in  this unit).

And the InvertMatrix function looks uncomplete (the matrix b is never used) and special ( inverses normally do not use operations like  2x2 minor / modulus).

So the function may not have a bug, but is not applicable to general 4x4 matrices.

 

TinyPortal © 2005-2018