From 50ae9bff33a8e24236e2c27e6134d52dc3dc6301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Wed, 11 May 2005 14:07:18 +0000 Subject: for non-square matrices, we now calculate automatically the (correct) pseudoinverse svn path=/trunk/externals/iem/iemmatrix/; revision=2949 --- src/mtx_inverse.c | 68 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/mtx_inverse.c b/src/mtx_inverse.c index f994b39..20339e3 100644 --- a/src/mtx_inverse.c +++ b/src/mtx_inverse.c @@ -17,7 +17,7 @@ static t_class *mtx_inverse_class; -t_matrixfloat* mtx_doInvert(t_matrixfloat*input, int rowcol){ +t_matrixfloat* mtx_doInvert(t_matrixfloat*input, int rowcol, int*error){ /* * row==col==rowclo * input=t_matrixfloat[row*col] @@ -30,10 +30,15 @@ t_matrixfloat* mtx_doInvert(t_matrixfloat*input, int rowcol){ int col=rowcol, row=rowcol, row2=row*col; t_matrixfloat *original=input; + t_matrixfloat *inverted = 0; - // 1a reserve space for the inverted matrix - t_matrixfloat *inverted = (t_matrixfloat *)getbytes(sizeof(t_matrixfloat)*row2); - // 1b make an eye-shaped float-buf for B + if(input==0)return 0; + + /* 1a reserve space for the inverted matrix */ + inverted=(t_matrixfloat *)getbytes(sizeof(t_matrixfloat)*row2); + if(inverted==0)return 0; + + /* 1b make an eye-shaped float-buf for B */ i=row2; b1=inverted; while(i--)*b1++=0; @@ -41,9 +46,9 @@ t_matrixfloat* mtx_doInvert(t_matrixfloat*input, int rowcol){ b1=inverted; while(i--)b1[i*(row+1)]=1; - // 2. do the Gauss-Jordan + /* 2. do the Gauss-Jordan */ for (k=0;kcol){ + inverteeCol=col; + invertee =mtx_doMultiply(col, transposed, row, original, col); + inverted =mtx_doMultiply(col, mtx_doInvert(invertee, col, 0), col, transposed, row); + } else { + inverteeCol=row; + invertee =mtx_doMultiply(row, original, col, transposed, row); + inverted =mtx_doMultiply(col, transposed, row, mtx_doInvert(invertee, row, 0), row); + } + freebytes(transposed, sizeof(t_matrixfloat)*col*row); + freebytes(invertee , sizeof(t_matrixfloat)*inverteeCol*inverteeCol); } - // 3. output the matrix - // 3a convert the floatbuf to an atombuf; + /* 3. output the matrix */ + /* 3a convert the floatbuf to an atombuf; */ float2matrix(x->atombuffer, inverted); - // 3b destroy the buffers + /* 3b destroy the buffers */ freebytes(original, sizeof(t_matrixfloat)*row*col); - freebytes(inverted, sizeof(t_matrixfloat)*row*row); - // 3c output the atombuf; + /* 3c output the atombuf; */ matrix_bang(x); } -- cgit v1.2.1