/** * 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'); }
/** * 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); }
/** * 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)); }
/** * Tests NumPHP::identity with argument 3 */ public function testIdentity() { $numArray = NumPHP::identity(3); $expectedNumArray = NumPHP::eye(3, 3); $this->assertNumArrayEquals($expectedNumArray, $numArray); }
/** * 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); }