Matrix inversion

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix inversion

lhartmann
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix inversion

Parkinbot
Are you sure this code ever run properly on a matrix with 0 in diagonal?

look at this trace:

mx = m4rx(90)*m4ry(90);
i=0;
echo(m4inv_solve(0,m4inv_mx(mx)));  // first call evaluated in m4inv(mx)
echo(i+2<4 && !m4inv_zero(mx[i][i+2]) ? m4inv_solve2(i, m4inv_rowswap(i,i+2,mx)):"not taken");  // echo(m4inv_rowswap(i,i+2,mx));  // d
echo((i==0) ? mx[i]/mx[i][i]:"not taken");

Answer:
ECHO: [[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]] // mx
ECHO: [[nan, inf, nan, nan, nan, nan, inf, nan], [nan, -inf, nan, nan, nan, nan, -inf, nan], [nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan, nan, nan, nan]]
ECHO: [[nan, inf, nan, nan], [nan, -inf, nan, nan], [nan, nan, nan, nan], [nan, nan, nan, nan]]
ECHO: [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] // row swap
ECHO: [nan, nan, inf, nan]
So, you test for a column
  !m4inv_zero(mx[i][i+2])
then do a row swap
  m4inv_rowswap(i,i+2,mx))
and then divide by 0
 mx[0]/mx[0][0]

You don't know, what you are "buying" by the row swap.
You should test for the row with first element non-zero and swap this one
Reply | Threaded
Open this post in threaded view
|

Re: Matrix inversion

Parkinbot
Ups I pressed the wrong button ...

I'd say this works better:

function m4inv_solve(i,mx) =
                 !m4inv_zero(mx[i][i  ]) ? m4inv_solve2(i,                     mx ) :
        i+1<4 && !m4inv_zero(mx[i+1][i]) ? m4inv_solve2(i, m4inv_rowswap(i,i+1,mx)) :
        i+2<4 && !m4inv_zero(mx[i+2][i]) ? m4inv_solve2(i, m4inv_rowswap(i,i+2,mx)) :
        i+3<4 && !m4inv_zero(mx[i+3][i]) ? m4inv_solve2(i, m4inv_rowswap(i,i+3,mx)) :
        m4identity(); // Singular matrix, reset to identity
Reply | Threaded
Open this post in threaded view
|

Re: Matrix inversion

lhartmann
CONTENTS DELETED
The author has deleted this message.