Exemplo n.º 1
0
 public function testTranspose()
 {
     $arr1 = array(array(1, 2), array(3, 4));
     $mat1 = new Matrix($arr1);
     $t = $mat1->Transpose();
     $testarr = array(array(1, 3), array(2, 4));
     $this->assertSame($t->GetInnerArray(), $testarr);
 }
Exemplo n.º 2
0
 /**
  * Compute the inverse of the matrix on which this method is found (A*A(-1)=I)
  * (cofactor(a))T/(det a)
  * @link http://www.mathwords.com/i/inverse_of_a_matrix.htm
  * @throws \Exception
  * @return Matrix
  */
 function Inverse()
 {
     if (!$this->isSquareMatrix()) {
         throw new \Exception("Not a square matrix!");
     }
     $rows = $this->rows;
     $columns = $this->columns;
     $newMatrix = array();
     for ($i = 0; $i < $rows; $i++) {
         for ($j = 0; $j < $columns; $j++) {
             $subMatrix = $this->GetSubMatrix($i, $j);
             if (fmod($i + $j, 2) == 0) {
                 $newMatrix[$i][$j] = $subMatrix->Determinant();
             } else {
                 $newMatrix[$i][$j] = -$subMatrix->Determinant();
             }
         }
     }
     $cofactorMatrix = new Matrix($newMatrix);
     return $cofactorMatrix->Transpose()->ScalarDivide($this->Determinant());
 }