Skip to content

Commit 3c5750b

Browse files
authored
Very Minor Simplification to Matrix Functions (PHPOffice#2222)
The external Matrix library has introduced some changes which permit the matrix functions to be slightly simplified.
1 parent 11bf051 commit 3c5750b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/PhpSpreadsheet/Calculation/MathTrig/MatrixFunctions.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

55
use Matrix\Builder;
6+
use Matrix\Div0Exception as MatrixDiv0Exception;
67
use Matrix\Exception as MatrixException;
78
use Matrix\Matrix;
89
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
@@ -84,8 +85,10 @@ public static function inverse($matrixValues)
8485
$matrix = self::getMatrix($matrixValues);
8586

8687
return $matrix->inverse()->toArray();
88+
} catch (MatrixDiv0Exception $e) {
89+
return Functions::NAN();
8790
} catch (MatrixException $e) {
88-
return (strpos($e->getMessage(), 'determinant') === false) ? Functions::VALUE() : Functions::NAN();
91+
return Functions::VALUE();
8992
} catch (Exception $e) {
9093
return $e->getMessage();
9194
}
@@ -125,10 +128,7 @@ public static function identity($dimension)
125128
try {
126129
$dimension = (int) Helpers::validateNumericNullBool($dimension);
127130
Helpers::validatePositive($dimension, Functions::VALUE());
128-
$matrix = Builder::createFilledMatrix(0, $dimension)->toArray();
129-
for ($x = 0; $x < $dimension; ++$x) {
130-
$matrix[$x][$x] = 1;
131-
}
131+
$matrix = Builder::createIdentityMatrix($dimension, 0)->toArray();
132132

133133
return $matrix;
134134
} catch (Exception $e) {

0 commit comments

Comments
 (0)