Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Invert any size matrix for Least Square varible math issue

Status
Not open for further replies.

ccccrnr

Junior Member level 3
Junior Member level 3
Joined
Dec 7, 2009
Messages
29
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Location
Tennessee USA
Activity points
1,468
I am trying to write some code in JavaScript to use on my website that can take data in the from of a square matrix and invert the matrix to do Least Squares to generate an equation for the data.

I'm having a problem of reverse assignments.
Meaning the value i'm sending is getting changed by the operations I do later on the new varible in the function invertM
It is like a have done my varibles with pointers
but Javascript doesn't have Pointers or Constants(unfortuanately)

Please help me with the variables and this code. THANKS

I'm trying to test this with input like
Code:
script src="LinAlgerbraTool.js"   /script
script
var trym = new Array([1, 2, 3], [2, 3, 4], [3, 2, 1] );
var inv_try = invertM(trym);
document.write( inv_try );
/script

the LinAlgerbraTool.js file looks like this
Code:
function matxProd( a, b) {
   var al = size(a);
   var bl = size(b);
   var mout = new Array();
   if (al[0] != bl[1]) { alert("matrix deminsions can't be multiplyed" + al[0] + al[1]); return NaN; }
   for(var j=0; j< bl[0]; j++)  {
      mout[j] = new Array();
      for(var i=0; i< al[1]; i++) {             // line 52 char 12 expect ';'  
         var c = 0;
         for(var k=0; k< al[0] ; k++) {
            c = a[k][i]*b[j][k] + c;
         }
         mout[j][i] = c;
      }
   }
   return mout;
}


function size(a) {         // size takes a vector or matrix => returns [length, height] h = 0 for vector
   var b = new Array(2);
   b[0] = a.length;
   for(var i=0; a[0][i] != undefined ; i++) {  }
   b[1] = i;
   return b;
}

function det(a) {    //  this is to take the determinant of the matrix a
   var al = size(a);
   if(al[0] != al[1]) {alert("matrix must be square"); return NaN; }
   var d = 0;
   var dp = 1;
   var dd = 1;
   for(var i=0; i<al[0]; i++) {
     for(var j=0; j<al[0]; j++) {
        if( (i+j) >= al[0] ) { dp = dp * a[i+j-al[0]][j]; dd = dd * a[2*al[0]-j-i-1][j]; 
        }else {         dp = dp * a[j+i][j];  dd = dd * a[al[0]-j-i-1][j]; }
     }
     d = d + dp - dd;
     dp = 1;
     dd = 1;
   }
   return d;
}

function invertM(ati) {    //   take the inverse of a matrix ati
   var al = size(ati);
   if(al[0] != al[1]) {alert("inverse matrix must be square"); return NaN; }
   var detm = det(ati);
   if(!detm) { alert("determinant of inverse Matrix can not = 0 "); return NaN; } //   JS error line 105 by ie
   var ain = new Array();
document.write("_ ati " + ati + " 
 ");
   for(var i=0; i<al[0] ; i++) {
      ain[i] = new Array();
      for(var j=0; j<al[0]; j++) {
         var eventst = (i+j)/2;                         //    test for even positive 
         if(Math.round(eventst) == eventst){ ain[i][j] = minormat(ati, i, j, al[0]) ;  // build matrix of minors
         }else{ ain[i][j] = -1 * minormat(ati, i, j, al[0]) ; }
      } 
   } 
   ain = transp(ain);
   ain = matscalmut( (1/detm), ain);
   return ain;
}

function matscalmut( s, m) {          //  multiple the scalar value by each cell of matrix
   var al = size(m);
   for(var i=0; i<al[0] ; i++) {
      for(var j=0; j<al[1]; j++) {
          m[i][j] = s * m[i][j];
     //     if( (m[i][j]> -.0000001) && (m[i][j] < .0000001) ) { m[i][j] = 0; }
      }
   }
   return m;
}

function minormat( amm, i, j, len) {
document.write(" in mat min " + amm);   //  line 123
  for (var k=0; k<len; k++) {
     amm[i][k] = 1; 
     amm[k][j] = 1;
  }
document.write(" mat minor amm " + amm );
  amm = det(amm);
document.write(" determinant amm " + amm + " 
 ");
  return amm;
}
matrProd is to multply 2 matrix (testing this function works)
size is the dimention of the matrix [length, height] (this function works)
det is to calculate the determinant of the matrix sent to it (this function works)
invertM is to return the inverse matrix of the matrix sent to it ( Not work)
matscalmut is to multiply a scalar value into each cell in the sent matrix (this function works)
minormat is to help generate the matrix of minors for the inversion process for any size matrix (it is also to help seperate code and debug my invertM problem)
 

Re: Invert any size matrix for Least Square varible math iss

Any ideas welcome

Test File
Code:
<html>
<head>
<script src="LinAlgerbra.js">   </script>
<script >
var trym = new Array([1, 2, 3], [2, 3, 4], [2, 2, 1] ); 
var inv_try = invertM(trym); 
document.write("

" + inv_try ); 
var test_iv = matxProd( trym, inv_try);
document.write("

 is this Identy Matrix -> " + test_iv);
</script> 
</html>

OUTPUT
_ ati 1,2,3,2,3,4,2,2,1
in mat min 1,2,3,2,3,4,2,2,1 mat minor amm 1,1,1,1,3,4,1,2,1 determinant amm -3
in mat min 1,1,1,1,3,4,1,2,1 mat minor amm 1,1,1,1,1,4,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,4,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,1,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,1,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,1,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,1,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,1,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0
in mat min 1,1,1,1,1,1,1,1,1 mat minor amm 1,1,1,1,1,1,1,1,1 determinant amm 0

-3,0,0,0,0,0,0,0,0

is this Identy Matrix -> -3,-3,-3,0,0,0,0,0,0

I've been working on this since Thanksgiving in November. I can't find an error and the javascript books I have (while bit dated) aren't helping out on this one.

If there is a math error please point me to it.
if there is a code error please help with that too? moderaters if this is a code error and/or you feel a different forum is more approproate please move
Thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top