public static function inverse(Matrix $matrix)
    {
        if (!$matrix->isSquare()) {
            throw new Exception('Inverse can only be calculated for a square matrix');
        }

        $determinant = self::getDeterminant($matrix);
        if ($determinant == 0.0) {
            throw new Exception('Inverse can only be calculated for a matrix with a non-zero determinant');
        }

        if ($matrix->rows == 1) {
            return new Matrix([[1 / $matrix->getValue(1, 1)]]);
        }

        return self::getAdjoint($matrix)
            ->multiply(1 / $determinant);
    }
public static function inverse(Matrix $matrix)
    {
        if (!$matrix->isSquare()) {
            throw new Exception('Inverse can only be calculated for a square matrix');
        }

        $determinant = self::getDeterminant($matrix);
        if ($determinant == 0.0) {
            throw new Exception('Inverse can only be calculated for a matrix with a non-zero determinant');
        }

        if ($matrix->rows == 1) {
            return new Matrix([[1 / $matrix->getValue(1, 1)]]);
        }

        return self::getAdjoint($matrix)
            ->multiply(1 / $determinant);
    }
public static function inverse ( Matrix $ matrix ) { if ( ! $ matrix -> isSquare ( ) ) { throw new Exception ( 'Inverse can only be calculated for a square matrix' ) ; } $ determinant = self :: getDeterminant ( $ matrix ) ; if ( $ determinant == 0.0 ) { throw new Exception ( 'Inverse can only be calculated for a matrix with a non-zero determinant' ) ; } if ( $ matrix -> rows == 1 ) { return new Matrix ( [ [ 1 / $ matrix -> getValue ( 1 , 1 ) ] ] ) ; } return self :: getAdjoint ( $ matrix ) -> multiply ( 1 / $ determinant ) ; }
Return the inverse of this matrix

@param Matrix $matrix The matrix whose inverse we wish to calculate
@return Matrix
@throws Exception
Return the inverse of this matrix