Matrix Operations

Page Contents

To use you must include MathymaWrite.js and MathymaMatrix.js as scripts in the head section of the page. All matrix prints should be in a mathyma class < div > . See previous page for details.

Retrieving a Matrix Element

< script > 
   var wMatA = new mathyma.Matrix ("1,2/4,5,6/7,8,9");
   document.write("#A _ _ _ = _ _ _ " + wMatA.McPrint());
   document.write("< p > the element in row 2, column 3  is " + wMatA.Element(2,3) + "< /p > "); 
< /script > 

Product of Matrices

Lets define a second matrix

< script > 
   var wMatB = new mathyma.Matrix ("11,12/13,14/15,20,30");
   document.write("#B _ _ _ = _ _ _ " + wMatB.McPrint());
< /script > 

OK, now that we've defined two matrices, let's see what their product is. They are both 3x3 matrices, so both AB and BA exist. First let's see what AB is:

< script > 
   document.write("#A#B _ _ _ = _ _ _ " + wMatA.Mult(wMatB).McPrint());
< /script > 

Note the use of the shorthand method to do the Mult() and McPrint() methods in one. If this seems confusing it may help to use brackets to clarify what is happening:

document.write("#A#B _ _ _ = _ _ _ " + (wMatA.Mult(wMatB)).McPrint());

or even more explicit:

var wProd = wMatA.Mult(wMatB);
document.write("#A#B _ _ _ = _ _ _ " + wProd.McPrint());

Let's have a quick look to see what BA is:

< script > 
   document.write("#B#A _ _ _ = _ _ _ " + wMatB.Mult(wMatA).McPrint());
< /script > 

"Multiply()", "Times()", "Prod()", and "Product()" are synonyms for "Mult()", i.e. the same result would be obtained from any of the following statements:

document.write("#A#B _ _ _ = _ _ _ " + (wMatA.Mult(wMatB)).McPrint());
document.write("#A#B _ _ _ = _ _ _ " + (wMatA.Multiply(wMatB)).McPrint());
document.write("#A#B _ _ _ = _ _ _ " + (wMatA.Prod(wMatB)).McPrint());
document.write("#A#B _ _ _ = _ _ _ " + (wMatA.Product(wMatB)).McPrint());
document.write("#A#B _ _ _ = _ _ _ " + (wMatA.Times(wMatB)).McPrint());

Sum of Matrices

< script > 
   document.write("#A+#B _ _ _ = _ _ _ " + wMatA.Plus(wMatB).McPrint());
< /script > 

"Sum()" and "Add()" are synonyms for "Plus()", i.e. the same result would be obtained from any of the following statements:

document.write("#A+#B _ _ _ = _ _ _ " + wMatA.Plus(wMatB).McPrint());
document.write("#A+#B _ _ _ = _ _ _ " + wMatA.Sum(wMatB).McPrint());
document.write("#A+#B _ _ _ = _ _ _ " + wMatA.Add(wMatB).McPrint());

Difference of Matrices

< script > 
   document.write("#A-#B _ _ _ = _ _ _ " + wMatA.Minus(wMatB).McPrint());
< /script > 

"Difference()" and "Subtract()" are synonyms for "Minus()", i.e. the same result would be obtained from any of the following statements:

document.write("#A-#B _ _ _ = _ _ _ " + wMatA.Minus(wMatB).McPrint());
document.write("#A-#B _ _ _ = _ _ _ " + wMatA.Difference(wMatB).McPrint());
document.write("#A-#B _ _ _ = _ _ _ " + wMatA.Subtract(wMatB).McPrint());

Inverse of a Matrix

< script > 
   mathyma.Matrix_Precision = 4;
   document.write("#A^{-1} _ _ _ = _ _ _ " + wMatA.Inv().McPrint());
< /script > 

General Inverse of a Matrix

Calculates the Moore-Penrose General Inverse of a Matrix. If #X is a ~m # ~n matrix then the general-inverse, #X^+, satisfies the following

  1. #X#X^+#X _ = _ #X _ _ (general inverse)
  2. #X^+#X#X^+ _ = _ #X^+ _ _ (reflexive)
  3. (#X#X^+)^T _ = _ #X#X^+ _ _ (least square)
  4. (#X^+#X)^T _ = _ #X^+#X _ _ (minimum norm)
< script > 
   mathyma.Matrix_Precision = 2;
   var wMatX = new mathyma.Matrix ("2,-1,4,6/2,3,-4,-5/2,-1,4,6");
   document.write("< p > #X _ _ _ = _ _ _  " + wMatX.McPrint() + "< /p > ");

   var wGInvMatX = wMatX.GInv(); 
   document.write("< p > #X^+ _ _ _ = _ _ _  " + wGInvMatX.McPrint() + "< /p > ");
   document.write("< p > #X#X^+#X _ _ _ = _ _ _  " + wMatX.Mult(wGInvMatX).Mult(wMatX).McPrint() + "< /p > ");
   document.write("< p > #X^+#X#X^+ _ _ _ = _ _ _  " + wGInvMatX.Mult(wMatX).Mult(wGInvMatX).McPrint() + "< /p > ");

   document.write("< p > #X#X^+ _ _ _ = _ _ _  " + wMatX.Mult(wGInvMatX).McPrint() + "< /p > ");
   document.write("< p > #X^+#X _ _ _ = _ _ _  " + wGInvMatX.Mult(wMatX).McPrint() + "< /p > ");
< /script > 

Rank of a Matrix

< script > 
   document.write(" rank ( #A )  _ _ _ = _ _ _ " + wMatA.Rank() );
< /script > 

Determinant of a Matrix

< script > 
   var wDetA = wMatA.Determinant();
   document.write(" | #A | _ _ _ = _ _ _ " + wDetA);
< /script > 

Adjoint of a Matrix

This is the matrix formed by replacing all the elements of A by their cofactor, and then transposing. The following result is well-known:

#A^{-1} _ _ _ = _ _ _ Adj #A / | #A |

< script > 
   mathyma.Matrix_Precision = 0;
   document.write("Adj #A _ _ _ = _ _ _ " + wMatA.Adjoint().McPrint());
< /script > 

Transpose of a Matrix

< script > 
   document.write("#A^T _ _ _ = _ _ _ " + wMatA.Trans().McPrint());
< /script > 

Scalar Multiplication

< script > 
   document.write("5 × #A _ _ _ = _ _ _ " + wMatA.ScalarMult(5).McPrint());
< /script > 

Rank Equivalence

According to matrix theory (see notes on Linear Algebra), for any ~m # ~n matrix #H of rank ρ , one can find regular ~m # ~m matrix #M and regular ~n # ~n matrix #N, such that

#M#H#N _ = _ #C

where #C is a matrix of the form:

#C _ = _ matrix{#I_{ρ},#0/#0,#0}

where #I_{ρ}, the ρ # ρ identity matrix, is embeded in a matrix of zeros.

calls #M and #N the #~{PreMultip}lier and #~{PostMultip}lier respectively (this is not standard nomeclature). #M and #N are not uniquely defined.

< script > 
   mathyma.Matrix_Precision = 1;
   var wMatH = new mathyma.Matrix("1,3,4,5/2,1,3,5/-1,9,8,7");
   document.write("#M#H#N _ _ _ = _ _ _ " + wMatH.PreMultip().McPrint() + wMatH.McPrint() + 
      wMatH.PostMultip().McPrint());
   document.write("< p > ");
   document.write("_ _ _ = _ _ _ " + wMatH.PreMultip().Mult(wMatH).Mult(wMatH.PostMultip()).McPrint());
   document.write("< /p > ");
< /script > 

Multiplication and Division of Elements

Sometimes it is quite usefull to be able to multiply or divide all the elements of an ~m # ~n matrix by the corresponding elements of another ~m # ~n matrix.

These are not "proper" matrix operations in the linear algebraic sense, but provides them in any case, through the methods ElementMult and ElementDiv. [Matrix addition and subtraction are element by element operations anyway].

< script > 
   var wMatC = new mathyma.Matrix ("9,6/-1,8");
   var wMatD = new mathyma.Matrix ("3,-2/-1,4");
   document.write(wMatC.McPrint() + " [&times] " + wMatD.McPrint() + "_ _ _ = _ _ _ " + 
      wMatC.ElementMult(wMatD).McPrint());
   document.write("< p > ");
   document.write(wMatC.McPrint() + " [&div.] " + wMatD.McPrint() + "_ _ _ = _ _ _ " + 
      wMatC.ElementDiv(wMatD).McPrint());
   document.write("< /p > ");
< /script >