|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* (c) Ruben Dorado <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace SiteAnalyzer; |
|
10
|
|
|
/** |
|
11
|
|
|
* class Matrix |
|
12
|
|
|
* |
|
13
|
|
|
* @package SiteAnalyzer |
|
14
|
|
|
* @author Ruben Dorado <[email protected]> |
|
15
|
|
|
* @copyright 2018 Ruben Dorado |
|
16
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License |
|
17
|
|
|
*/ |
|
18
|
|
|
class Matrix |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/* |
|
22
|
|
|
* @param |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function submatrix($matrix, $columns) |
|
25
|
|
|
{ |
|
26
|
|
|
$n = count($matrix); |
|
27
|
|
|
if ($n == 0) return []; |
|
28
|
|
|
|
|
29
|
|
|
$m = count($matrix[0]); |
|
30
|
|
|
if ($m == 0) return $matrix; |
|
31
|
|
|
|
|
32
|
|
|
$result = []; |
|
33
|
|
|
for ($i=0; $i<$n; $i++) { |
|
34
|
|
|
$tmp = []; |
|
35
|
|
|
foreach ($columns as $col) { |
|
36
|
|
|
$tmp[] = $matrix[$i][$col]; |
|
37
|
|
|
} |
|
38
|
|
|
$result[] = $tmp; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $result; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/* |
|
45
|
|
|
* @param |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function toSquareMatrix($matrix, $ncol, $mcol, $vcol, $sparse = False) |
|
48
|
|
|
{ |
|
49
|
|
|
$n = count($matrix); |
|
50
|
|
|
if ($n == 0) return []; |
|
51
|
|
|
|
|
52
|
|
|
$labels = []; |
|
53
|
|
|
$id = 0; |
|
54
|
|
|
$data = []; |
|
55
|
|
|
for ($i=0; $i<$n; $i++) { |
|
56
|
|
|
if (!array_key_exists($matrix[$i][$ncol],$labels)) { |
|
57
|
|
|
$labels[$matrix[$i][$ncol]] = $id++; |
|
58
|
|
|
} |
|
59
|
|
|
$nid = $labels[$matrix[$i][$ncol]]; |
|
60
|
|
|
if (!array_key_exists($matrix[$i][$mcol],$labels)) { |
|
61
|
|
|
$labels[$matrix[$i][$mcol]] = $id++; |
|
62
|
|
|
} |
|
63
|
|
|
$mid = $labels[$matrix[$i][$mcol]]; |
|
64
|
|
|
$val = $matrix[$i][$vcol]; |
|
65
|
|
|
$data[] = [$nid, $mid, $val]; |
|
66
|
|
|
} |
|
67
|
|
|
if ($sparse) return ["data" => $data, "labels" => $labels]; |
|
68
|
|
|
$nlab = count($labels); |
|
69
|
|
|
$result = []; |
|
70
|
|
|
for ($i=0; $i<$nlab; $i++) { |
|
71
|
|
|
$tmp = array_fill(0, $nlab, 0); |
|
72
|
|
|
$result[] = $tmp; |
|
73
|
|
|
} |
|
74
|
|
|
foreach ($data as $row) { |
|
75
|
|
|
$result[$row[0]][$row[1]] = $row[2]; |
|
76
|
|
|
} |
|
77
|
|
|
return ["data" => $result, "labels" => $labels]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/* |
|
81
|
|
|
* @param |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function getColumn($matrix, $col) |
|
84
|
|
|
{ |
|
85
|
|
|
$n = count($matrix1); |
|
|
|
|
|
|
86
|
|
|
$resp = []; |
|
87
|
|
|
foreach ($matrix as $row) { |
|
88
|
|
|
$resp[] = $row[$col]; |
|
89
|
|
|
} |
|
90
|
|
|
return $resp; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/* |
|
94
|
|
|
* @param |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function sum($matrix1, $matrix2) |
|
97
|
|
|
{ |
|
98
|
|
|
$result = []; |
|
99
|
|
|
$n = count($matrix1); |
|
100
|
|
|
if ($n != count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
|
101
|
|
|
$m = count($matrix1[0]); |
|
102
|
|
|
for ($i=0;$i<$n;$i++) { |
|
103
|
|
|
$tmp = []; |
|
104
|
|
|
for($j=0;$j<$m;$j++) { |
|
105
|
|
|
$tmp[] = $matrix1[$i][$j] + $matrix2[$i][$j]; |
|
106
|
|
|
} |
|
107
|
|
|
$result[] = $tmp; |
|
108
|
|
|
} |
|
109
|
|
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/* |
|
114
|
|
|
* @param |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function multiply($matrix1, $matrix2) |
|
117
|
|
|
{ |
|
118
|
|
|
$n1 = count($matrix1); |
|
119
|
|
|
$m1 = count($matrix1[0]); |
|
120
|
|
|
$n2 = count($matrix2); |
|
121
|
|
|
$m2 = count($matrix2[0]); |
|
122
|
|
|
|
|
123
|
|
|
if ($n2 !== $m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
124
|
|
|
|
|
125
|
|
|
$result = []; |
|
126
|
|
|
for ($i=0; $i<$n1; $i++) { |
|
127
|
|
|
$result[] = array_fill(0, $m2, 0); |
|
128
|
|
|
} |
|
129
|
|
|
for ($i=0; $i<$n1; $i++) { |
|
130
|
|
|
for ($j=0; $j<$m2; $j++) { |
|
131
|
|
|
for ($k=0; $k<$n2; $k++) { |
|
132
|
|
|
$result[$i][$j] += $matrix1[$i][$k]*$matrix2[$k][$j]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/* |
|
140
|
|
|
* @param |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function toBinary($matrix) { |
|
143
|
|
|
$resp = []; |
|
144
|
|
|
foreach ($matrix as $row) { |
|
145
|
|
|
$tmp = []; |
|
146
|
|
|
foreach ($row as $dat) { |
|
147
|
|
|
$tmp[] = $dat == 0 ? 0 : 1; |
|
148
|
|
|
} |
|
149
|
|
|
$resp[] = $tmp; |
|
150
|
|
|
} |
|
151
|
|
|
return $resp; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/* |
|
155
|
|
|
* @param |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function arrayToMatrix($array) { |
|
158
|
|
|
$resp = []; |
|
159
|
|
|
foreach ($array as $key => $val) { |
|
160
|
|
|
$resp[] = [$key, $val]; |
|
161
|
|
|
} |
|
162
|
|
|
return $resp; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/* |
|
166
|
|
|
* @param |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function power($matrix, $n) |
|
169
|
|
|
{ |
|
170
|
|
|
if($n <= 1) return $matrix; |
|
171
|
|
|
$resp = self::multiply($matrix, $matrix); |
|
172
|
|
|
if($n == 2) return $resp; |
|
173
|
|
|
for($i=2; $i<$n; $i++) { |
|
174
|
|
|
$resp = self::multiply($resp, $matrix); |
|
175
|
|
|
} |
|
176
|
|
|
return $resp; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/* |
|
180
|
|
|
* @param |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function sumArray($array1, $array2) { |
|
183
|
|
|
$resp = []; |
|
184
|
|
|
$n = min([count($array1), count($array2)]); |
|
185
|
|
|
for ($i=0;$i<$n;$i++){ |
|
186
|
|
|
$resp[] = $array1[$i] + $array2[$i]; |
|
187
|
|
|
} |
|
188
|
|
|
return $resp; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|