1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Matrix\Exception as MatrixException; |
7
|
|
|
use Matrix\Matrix; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
9
|
|
|
|
10
|
|
|
class MatrixFunctions |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Convert parameter to matrix. |
14
|
|
|
* |
15
|
|
|
* @param mixed $matrixValues A matrix of values |
16
|
|
|
*/ |
17
|
52 |
|
private static function getMatrix($matrixValues): Matrix |
18
|
|
|
{ |
19
|
52 |
|
$matrixData = []; |
20
|
52 |
|
if (!is_array($matrixValues)) { |
21
|
3 |
|
$matrixValues = [[$matrixValues]]; |
22
|
|
|
} |
23
|
|
|
|
24
|
52 |
|
$row = 0; |
25
|
52 |
|
foreach ($matrixValues as $matrixRow) { |
26
|
52 |
|
if (!is_array($matrixRow)) { |
27
|
2 |
|
$matrixRow = [$matrixRow]; |
28
|
|
|
} |
29
|
52 |
|
$column = 0; |
30
|
52 |
|
foreach ($matrixRow as $matrixCell) { |
31
|
52 |
|
if ((is_string($matrixCell)) || ($matrixCell === null)) { |
32
|
10 |
|
throw new Exception(Functions::VALUE()); |
33
|
|
|
} |
34
|
50 |
|
$matrixData[$row][$column] = $matrixCell; |
35
|
50 |
|
++$column; |
36
|
|
|
} |
37
|
48 |
|
++$row; |
38
|
|
|
} |
39
|
|
|
|
40
|
43 |
|
return new Matrix($matrixData); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* MDETERM. |
45
|
|
|
* |
46
|
|
|
* Returns the matrix determinant of an array. |
47
|
|
|
* |
48
|
|
|
* Excel Function: |
49
|
|
|
* MDETERM(array) |
50
|
|
|
* |
51
|
|
|
* @param mixed $matrixValues A matrix of values |
52
|
|
|
* |
53
|
|
|
* @return float|string The result, or a string containing an error |
54
|
|
|
*/ |
55
|
25 |
|
public static function funcMDeterm($matrixValues) |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
25 |
|
$matrix = self::getMatrix($matrixValues); |
59
|
|
|
|
60
|
19 |
|
return $matrix->determinant(); |
61
|
8 |
|
} catch (MatrixException $ex) { |
62
|
2 |
|
return Functions::VALUE(); |
63
|
6 |
|
} catch (Exception $e) { |
64
|
6 |
|
return $e->getMessage(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* MINVERSE. |
70
|
|
|
* |
71
|
|
|
* Returns the inverse matrix for the matrix stored in an array. |
72
|
|
|
* |
73
|
|
|
* Excel Function: |
74
|
|
|
* MINVERSE(array) |
75
|
|
|
* |
76
|
|
|
* @param mixed $matrixValues A matrix of values |
77
|
|
|
* |
78
|
|
|
* @return array|string The result, or a string containing an error |
79
|
|
|
*/ |
80
|
17 |
|
public static function funcMInverse($matrixValues) |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
17 |
|
$matrix = self::getMatrix($matrixValues); |
84
|
|
|
|
85
|
15 |
|
return $matrix->inverse()->toArray(); |
86
|
5 |
|
} catch (MatrixException $e) { |
87
|
3 |
|
return (strpos($e->getMessage(), 'determinant') === false) ? Functions::VALUE() : Functions::NAN(); |
88
|
2 |
|
} catch (Exception $e) { |
89
|
2 |
|
return $e->getMessage(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* MMULT. |
95
|
|
|
* |
96
|
|
|
* @param mixed $matrixData1 A matrix of values |
97
|
|
|
* @param mixed $matrixData2 A matrix of values |
98
|
|
|
* |
99
|
|
|
* @return array|string The result, or a string containing an error |
100
|
|
|
*/ |
101
|
13 |
|
public static function funcMMult($matrixData1, $matrixData2) |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
13 |
|
$matrixA = self::getMatrix($matrixData1); |
105
|
12 |
|
$matrixB = self::getMatrix($matrixData2); |
106
|
|
|
|
107
|
11 |
|
return $matrixA->multiply($matrixB)->toArray(); |
108
|
5 |
|
} catch (MatrixException $ex) { |
109
|
3 |
|
return Functions::VALUE(); |
110
|
2 |
|
} catch (Exception $e) { |
111
|
2 |
|
return $e->getMessage(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|