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
|
|
|
$resp = []; |
86
|
|
|
foreach ($matrix as $row) { |
87
|
|
|
$resp[] = $row[$col]; |
88
|
|
|
} |
89
|
|
|
return $resp; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
* @param |
94
|
|
|
*/ |
95
|
|
|
public static function sum($matrix1, $matrix2) |
96
|
|
|
{ |
97
|
|
|
$result = []; |
98
|
|
|
$n = count($matrix1); |
99
|
|
|
if ($n != count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
100
|
|
|
$m = count($matrix1[0]); |
101
|
|
|
for ($i=0;$i<$n;$i++) { |
102
|
|
|
$tmp = []; |
103
|
|
|
for($j=0;$j<$m;$j++) { |
104
|
|
|
$tmp[] = $matrix1[$i][$j] + $matrix2[$i][$j]; |
105
|
|
|
} |
106
|
|
|
$result[] = $tmp; |
107
|
|
|
} |
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/* |
113
|
|
|
* @param |
114
|
|
|
*/ |
115
|
|
|
public static function multiply($matrix1, $matrix2) |
116
|
|
|
{ |
117
|
|
|
$n1 = count($matrix1); |
118
|
|
|
$m1 = count($matrix1[0]); |
119
|
|
|
$n2 = count($matrix2); |
120
|
|
|
$m2 = count($matrix2[0]); |
121
|
|
|
|
122
|
|
|
if ($n2 !== $m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
123
|
|
|
|
124
|
|
|
$result = []; |
125
|
|
|
for ($i=0; $i<$n1; $i++) { |
126
|
|
|
$result[] = array_fill(0, $m2, 0); |
127
|
|
|
} |
128
|
|
|
for ($i=0; $i<$n1; $i++) { |
129
|
|
|
for ($j=0; $j<$m2; $j++) { |
130
|
|
|
for ($k=0; $k<$n2; $k++) { |
131
|
|
|
$result[$i][$j] += $matrix1[$i][$k]*$matrix2[$k][$j]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/* |
139
|
|
|
* @param |
140
|
|
|
*/ |
141
|
|
|
public static function toBinary($matrix) { |
142
|
|
|
$resp = []; |
143
|
|
|
foreach ($matrix as $row) { |
144
|
|
|
$tmp = []; |
145
|
|
|
foreach ($row as $dat) { |
146
|
|
|
$tmp[] = $dat == 0 ? 0 : 1; |
147
|
|
|
} |
148
|
|
|
$resp[] = $tmp; |
149
|
|
|
} |
150
|
|
|
return $resp; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/* |
154
|
|
|
* @param |
155
|
|
|
*/ |
156
|
|
|
public static function arrayToMatrix($array) { |
157
|
|
|
$resp = []; |
158
|
|
|
foreach ($array as $key => $val) { |
159
|
|
|
$resp[] = [$key, $val]; |
160
|
|
|
} |
161
|
|
|
return $resp; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
* @param |
166
|
|
|
*/ |
167
|
|
|
public static function power($matrix, $n) |
168
|
|
|
{ |
169
|
|
|
if($n <= 1) return $matrix; |
170
|
|
|
$resp = self::multiply($matrix, $matrix); |
171
|
|
|
if($n == 2) return $resp; |
172
|
|
|
for($i=2; $i<$n; $i++) { |
173
|
|
|
$resp = self::multiply($resp, $matrix); |
174
|
|
|
} |
175
|
|
|
return $resp; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/* |
179
|
|
|
* @param |
180
|
|
|
*/ |
181
|
|
|
public static function sumArray($array1, $array2) { |
182
|
|
|
$resp = []; |
183
|
|
|
$n = min([count($array1), count($array2)]); |
184
|
|
|
for ($i=0;$i<$n;$i++){ |
185
|
|
|
$resp[] = $array1[$i] + $array2[$i]; |
186
|
|
|
} |
187
|
|
|
return $resp; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
|