identity() public static method

Returns a identity matrix (NumArray) filled with 0 and 1 on the main diagonal
Since: 1.0.0
public static identity ( integer $mAxis ) : NumArray
$mAxis integer size of the m axis and n axis
return NumArray
 /**
  * Tests LinAlg::lud with singular 4x4 matrix
  */
 public function testLUDecompositionSingularMatrix()
 {
     $matrix = NumPHP::identity(4);
     $matrix->set(2, 2, 0);
     $expectedP = NumPHP::identity(4);
     $expectedL = NumPHP::identity(4);
     $expectedU = NumPHP::identity(4);
     $expectedU->set(2, 2, 0);
     list($pMatrix, $lMatrix, $uMatrix) = LinAlg::lud($matrix);
     $this->assertNumArrayEquals($expectedP, $pMatrix, 'Matrix P is not equal');
     $this->assertNumArrayEquals($expectedL, $lMatrix, 'Matrix L is not equal');
     $this->assertNumArrayEquals($expectedU, $uMatrix, 'Matrix U is not equal');
 }
示例#2
0
 /**
  * Tests if InvalidArgumentException will be thrown, when using LinAlg::solve with not align matrix and vector
  *
  * @expectedException        \NumPHP\LinAlg\Exception\InvalidArgumentException
  * @expectedExceptionMessage Can not solve a linear system with matrix (4, 4) and matrix (3)
  */
 public function testSolveNotAlign()
 {
     $matrix = NumPHP::identity(4);
     $vector = NumPHP::ones(3);
     LinAlg::solve($matrix, $vector);
 }
示例#3
0
 /**
  * Tests if Helper::isNotSingularMatrix works with invalid not singular matrix
  */
 public function testCheckNotSingularMatrixInvalid()
 {
     $numArray = NumPHP::identity(4);
     $numArray->set(2, 2, 0);
     $this->assertFalse(Helper::isNotSingularMatrix($numArray));
 }
示例#4
0
文件: EyeTest.php 项目: raslin/NumPHP
 /**
  * Tests NumPHP::identity with argument 3
  */
 public function testIdentity()
 {
     $numArray = NumPHP::identity(3);
     $expectedNumArray = NumPHP::eye(3, 3);
     $this->assertNumArrayEquals($expectedNumArray, $numArray);
 }
示例#5
0
 /**
  * Calculates the inverse of a not singular square matrix
  *
  * @param mixed $squareMatrix not singular matrix
  *
  * @throws SingularMatrixException will be thrown, when `$squareMatrix` is singular
  *
  * @api
  * @since 1.0.0
  *
  * @return NumArray
  */
 public static function inv($squareMatrix)
 {
     if (!$squareMatrix instanceof NumArray) {
         $squareMatrix = new NumArray($squareMatrix);
     } elseif ($squareMatrix->inCache(self::CACHE_KEY_INVERSE)) {
         return $squareMatrix->getCache(self::CACHE_KEY_INVERSE);
     }
     if (!Helper::isNotSingularMatrix($squareMatrix)) {
         throw new SingularMatrixException("Matrix is singular");
     }
     $shape = $squareMatrix->getShape();
     $inv = self::solve($squareMatrix, NumPHP::identity($shape[0]));
     $squareMatrix->setCache(self::CACHE_KEY_INVERSE, $inv);
     return self::inv($squareMatrix);
 }