ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MatrixFunctions.php
Go to the documentation of this file.
1 <?php
2 
4 
7 use Matrix\Matrix;
10 
12 {
18  private static function getMatrix($matrixValues): Matrix
19  {
20  $matrixData = [];
21  if (!is_array($matrixValues)) {
22  $matrixValues = [[$matrixValues]];
23  }
24 
25  $row = 0;
26  foreach ($matrixValues as $matrixRow) {
27  if (!is_array($matrixRow)) {
28  $matrixRow = [$matrixRow];
29  }
30  $column = 0;
31  foreach ($matrixRow as $matrixCell) {
32  if ((is_string($matrixCell)) || ($matrixCell === null)) {
33  throw new Exception(Functions::VALUE());
34  }
35  $matrixData[$row][$column] = $matrixCell;
36  ++$column;
37  }
38  ++$row;
39  }
40 
41  return new Matrix($matrixData);
42  }
43 
56  public static function determinant($matrixValues)
57  {
58  try {
59  $matrix = self::getMatrix($matrixValues);
60 
61  return $matrix->determinant();
62  } catch (MatrixException $ex) {
63  return Functions::VALUE();
64  } catch (Exception $e) {
65  return $e->getMessage();
66  }
67  }
68 
81  public static function inverse($matrixValues)
82  {
83  try {
84  $matrix = self::getMatrix($matrixValues);
85 
86  return $matrix->inverse()->toArray();
87  } catch (MatrixException $e) {
88  return (strpos($e->getMessage(), 'determinant') === false) ? Functions::VALUE() : Functions::NAN();
89  } catch (Exception $e) {
90  return $e->getMessage();
91  }
92  }
93 
102  public static function multiply($matrixData1, $matrixData2)
103  {
104  try {
105  $matrixA = self::getMatrix($matrixData1);
106  $matrixB = self::getMatrix($matrixData2);
107 
108  return $matrixA->multiply($matrixB)->toArray();
109  } catch (MatrixException $ex) {
110  return Functions::VALUE();
111  } catch (Exception $e) {
112  return $e->getMessage();
113  }
114  }
115 
123  public static function identity($dimension)
124  {
125  try {
126  $dimension = (int) Helpers::validateNumericNullBool($dimension);
128  $matrix = Builder::createFilledMatrix(0, $dimension)->toArray();
129  for ($x = 0; $x < $dimension; ++$x) {
130  $matrix[$x][$x] = 1;
131  }
132 
133  return $matrix;
134  } catch (Exception $e) {
135  return $e->getMessage();
136  }
137  }
138 }
static validateNumericNullBool($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:27
static createFilledMatrix($fillValue, $rows, $columns=null)
Create a new matrix of specified dimensions, and filled with a specified value If the column argument...
Definition: Builder.php:30
static multiply($matrixData1, $matrixData2)
MMULT.
$matrix
Definition: test.php:18
static getMatrix($matrixValues)
Convert parameter to matrix.
$row
Class for the creating "special" Matrices.
Definition: Builder.php:11
$x
Definition: complexTest.php:9
static validatePositive($number, ?string $except=null)
Confirm number > 0.
Definition: Helpers.php:83