@@ -197,7 +197,7 @@ |
||
| 197 | 197 | */ |
| 198 | 198 | private static function getStringParameter($config, $section, $name) |
| 199 | 199 | { |
| 200 | - return isset($config[$section][$name]) ? $config[$section][$name] : NULL; |
|
| 200 | + return isset($config[$section][$name]) ? $config[$section][$name] : NULL; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /* |
@@ -244,7 +244,7 @@ |
||
| 244 | 244 | * @param $config Configuration |
| 245 | 245 | * |
| 246 | 246 | */ |
| 247 | - public static function findIdByTimeUser($pdo, $config, $by=[]) { |
|
| 247 | + public static function findIdByTimeUser($pdo, $config, $by = []) { |
|
| 248 | 248 | return OptionsDAO::findIdByTimeUser($pdo, $config, $by); |
| 249 | 249 | } |
| 250 | 250 | |
@@ -91,9 +91,9 @@ |
||
| 91 | 91 | } |
| 92 | 92 | for ($i=0; $i<$n1; $i++) { |
| 93 | 93 | for ($j=0; $j<$m2; $j++) { |
| 94 | - for ($k=0; $k<$n2; $k++) { |
|
| 95 | - $result[$i][$j] += $matrix1[$i][$k]*$matrix2[$k][$j]; |
|
| 96 | - } |
|
| 94 | + for ($k=0; $k<$n2; $k++) { |
|
| 95 | + $result[$i][$j] += $matrix1[$i][$k]*$matrix2[$k][$j]; |
|
| 96 | + } |
|
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | 99 | return $result; |
@@ -24,13 +24,13 @@ discard block |
||
| 24 | 24 | public static function submatrix($matrix, $columns) |
| 25 | 25 | { |
| 26 | 26 | $n = count($matrix); |
| 27 | - if ($n == 0) return []; |
|
| 27 | + if ($n==0) return []; |
|
| 28 | 28 | |
| 29 | 29 | $m = count($matrix[0]); |
| 30 | - if ($m == 0) return $matrix; |
|
| 30 | + if ($m==0) return $matrix; |
|
| 31 | 31 | |
| 32 | 32 | $result = []; |
| 33 | - for ($i=0; $i<$n; $i++) { |
|
| 33 | + for ($i = 0; $i < $n; $i++) { |
|
| 34 | 34 | $tmp = []; |
| 35 | 35 | foreach ($columns as $col) { |
| 36 | 36 | $tmp[] = $matrix[$i][$col]; |
@@ -47,17 +47,17 @@ discard block |
||
| 47 | 47 | public static function toSquareMatrix($matrix, $ncol, $mcol, $vcol, $sparse = False) |
| 48 | 48 | { |
| 49 | 49 | $n = count($matrix); |
| 50 | - if ($n == 0) return []; |
|
| 50 | + if ($n==0) return []; |
|
| 51 | 51 | |
| 52 | 52 | $labels = []; |
| 53 | 53 | $id = 0; |
| 54 | 54 | $data = []; |
| 55 | - for ($i=0; $i<$n; $i++) { |
|
| 56 | - if (!array_key_exists($matrix[$i][$ncol],$labels)) { |
|
| 55 | + for ($i = 0; $i < $n; $i++) { |
|
| 56 | + if (!array_key_exists($matrix[$i][$ncol], $labels)) { |
|
| 57 | 57 | $labels[$matrix[$i][$ncol]] = $id++; |
| 58 | 58 | } |
| 59 | 59 | $nid = $labels[$matrix[$i][$ncol]]; |
| 60 | - if (!array_key_exists($matrix[$i][$mcol],$labels)) { |
|
| 60 | + if (!array_key_exists($matrix[$i][$mcol], $labels)) { |
|
| 61 | 61 | $labels[$matrix[$i][$mcol]] = $id++; |
| 62 | 62 | } |
| 63 | 63 | $mid = $labels[$matrix[$i][$mcol]]; |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | if ($sparse) return ["data" => $data, "labels" => $labels]; |
| 68 | 68 | $nlab = count($labels); |
| 69 | 69 | $result = []; |
| 70 | - for ($i=0; $i<$nlab; $i++) { |
|
| 70 | + for ($i = 0; $i < $nlab; $i++) { |
|
| 71 | 71 | $tmp = array_fill(0, $nlab, 0); |
| 72 | 72 | $result[] = $tmp; |
| 73 | 73 | } |
@@ -84,11 +84,11 @@ discard block |
||
| 84 | 84 | { |
| 85 | 85 | $result = []; |
| 86 | 86 | $n = count($matrix1); |
| 87 | - if ($n != count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
|
| 87 | + if ($n!=count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
|
| 88 | 88 | $m = count($matrix1[0]); |
| 89 | - for ($i=0;$i<$n;$i++) { |
|
| 89 | + for ($i = 0; $i < $n; $i++) { |
|
| 90 | 90 | $tmp = []; |
| 91 | - for($j=0;$j<$m;$j++) { |
|
| 91 | + for ($j = 0; $j < $m; $j++) { |
|
| 92 | 92 | $tmp[] = $matrix1[$i][$j] + $matrix2[$i][$j]; |
| 93 | 93 | } |
| 94 | 94 | $result[] = $tmp; |
@@ -107,16 +107,16 @@ discard block |
||
| 107 | 107 | $n2 = count($matrix2); |
| 108 | 108 | $m2 = count($matrix2[0]); |
| 109 | 109 | |
| 110 | - if ($n2 !== $m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
| 110 | + if ($n2!==$m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
| 111 | 111 | |
| 112 | 112 | $result = []; |
| 113 | - for ($i=0; $i<$n1; $i++) { |
|
| 113 | + for ($i = 0; $i < $n1; $i++) { |
|
| 114 | 114 | $result[] = array_fill(0, $m2, 0); |
| 115 | 115 | } |
| 116 | - for ($i=0; $i<$n1; $i++) { |
|
| 117 | - for ($j=0; $j<$m2; $j++) { |
|
| 118 | - for ($k=0; $k<$n2; $k++) { |
|
| 119 | - $result[$i][$j] += $matrix1[$i][$k]*$matrix2[$k][$j]; |
|
| 116 | + for ($i = 0; $i < $n1; $i++) { |
|
| 117 | + for ($j = 0; $j < $m2; $j++) { |
|
| 118 | + for ($k = 0; $k < $n2; $k++) { |
|
| 119 | + $result[$i][$j] += $matrix1[$i][$k] * $matrix2[$k][$j]; |
|
| 120 | 120 | } |
| 121 | 121 | } |
| 122 | 122 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | foreach ($matrix as $row) { |
| 132 | 132 | $tmp = []; |
| 133 | 133 | foreach ($row as $dat) { |
| 134 | - $tmp[] = $dat == 0 ? 0 : 1; |
|
| 134 | + $tmp[] = $dat==0 ? 0 : 1; |
|
| 135 | 135 | } |
| 136 | 136 | $resp[] = $tmp; |
| 137 | 137 | } |
@@ -154,10 +154,10 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | public static function power($matrix, $n) |
| 156 | 156 | { |
| 157 | - if($n <= 1) return $matrix; |
|
| 157 | + if ($n <= 1) return $matrix; |
|
| 158 | 158 | $resp = self::multiply($matrix, $matrix); |
| 159 | - if($n == 2) return $resp; |
|
| 160 | - for($i=2; $i<$n; $i++) { |
|
| 159 | + if ($n==2) return $resp; |
|
| 160 | + for ($i = 2; $i < $n; $i++) { |
|
| 161 | 161 | $resp = self::multiply($resp, $matrix); |
| 162 | 162 | } |
| 163 | 163 | return $resp; |
@@ -24,10 +24,14 @@ discard block |
||
| 24 | 24 | public static function submatrix($matrix, $columns) |
| 25 | 25 | { |
| 26 | 26 | $n = count($matrix); |
| 27 | - if ($n == 0) return []; |
|
| 27 | + if ($n == 0) { |
|
| 28 | + return []; |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | $m = count($matrix[0]); |
| 30 | - if ($m == 0) return $matrix; |
|
| 32 | + if ($m == 0) { |
|
| 33 | + return $matrix; |
|
| 34 | + } |
|
| 31 | 35 | |
| 32 | 36 | $result = []; |
| 33 | 37 | for ($i=0; $i<$n; $i++) { |
@@ -47,7 +51,9 @@ discard block |
||
| 47 | 51 | public static function toSquareMatrix($matrix, $ncol, $mcol, $vcol, $sparse = False) |
| 48 | 52 | { |
| 49 | 53 | $n = count($matrix); |
| 50 | - if ($n == 0) return []; |
|
| 54 | + if ($n == 0) { |
|
| 55 | + return []; |
|
| 56 | + } |
|
| 51 | 57 | |
| 52 | 58 | $labels = []; |
| 53 | 59 | $id = 0; |
@@ -64,7 +70,9 @@ discard block |
||
| 64 | 70 | $val = $matrix[$i][$vcol]; |
| 65 | 71 | $data[] = [$nid, $mid, $val]; |
| 66 | 72 | } |
| 67 | - if ($sparse) return ["data" => $data, "labels" => $labels]; |
|
| 73 | + if ($sparse) { |
|
| 74 | + return ["data" => $data, "labels" => $labels]; |
|
| 75 | + } |
|
| 68 | 76 | $nlab = count($labels); |
| 69 | 77 | $result = []; |
| 70 | 78 | for ($i=0; $i<$nlab; $i++) { |
@@ -84,7 +92,9 @@ discard block |
||
| 84 | 92 | { |
| 85 | 93 | $result = []; |
| 86 | 94 | $n = count($matrix1); |
| 87 | - if ($n != count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
|
| 95 | + if ($n != count($matrix2)) { |
|
| 96 | + throw new \Exception("Summing non compatible matrices. "); |
|
| 97 | + } |
|
| 88 | 98 | $m = count($matrix1[0]); |
| 89 | 99 | for ($i=0;$i<$n;$i++) { |
| 90 | 100 | $tmp = []; |
@@ -107,7 +117,9 @@ discard block |
||
| 107 | 117 | $n2 = count($matrix2); |
| 108 | 118 | $m2 = count($matrix2[0]); |
| 109 | 119 | |
| 110 | - if ($n2 !== $m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
| 120 | + if ($n2 !== $m1) { |
|
| 121 | + throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
| 122 | + } |
|
| 111 | 123 | |
| 112 | 124 | $result = []; |
| 113 | 125 | for ($i=0; $i<$n1; $i++) { |
@@ -154,9 +166,13 @@ discard block |
||
| 154 | 166 | */ |
| 155 | 167 | public static function power($matrix, $n) |
| 156 | 168 | { |
| 157 | - if($n <= 1) return $matrix; |
|
| 169 | + if($n <= 1) { |
|
| 170 | + return $matrix; |
|
| 171 | + } |
|
| 158 | 172 | $resp = self::multiply($matrix, $matrix); |
| 159 | - if($n == 2) return $resp; |
|
| 173 | + if($n == 2) { |
|
| 174 | + return $resp; |
|
| 175 | + } |
|
| 160 | 176 | for($i=2; $i<$n; $i++) { |
| 161 | 177 | $resp = self::multiply($resp, $matrix); |
| 162 | 178 | } |
@@ -92,12 +92,12 @@ |
||
| 92 | 92 | * @param |
| 93 | 93 | */ |
| 94 | 94 | function eclideanDistance($p1, $p2) { |
| 95 | - $len = count($p1); |
|
| 96 | - $acum = 0; |
|
| 97 | - for($i=0; $i<$len; $i++) { |
|
| 98 | - $acum += ($p1[$i] - $p2[$i])**2; |
|
| 99 | - } |
|
| 100 | - return sqrt($acum); |
|
| 95 | + $len = count($p1); |
|
| 96 | + $acum = 0; |
|
| 97 | + for($i=0; $i<$len; $i++) { |
|
| 98 | + $acum += ($p1[$i] - $p2[$i])**2; |
|
| 99 | + } |
|
| 100 | + return sqrt($acum); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | $npoints = count($data); |
| 32 | 32 | if (count($npoints) <= 0) throw new Exception("Not enough data. "); |
| 33 | 33 | $ndimensions = count($npoints[0]); |
| 34 | - $centroids = initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;}); |
|
| 34 | + $centroids = initCentroids($nclusters, $ndimensions, function() {return rand(0, 100) / 100; }); |
|
| 35 | 35 | while (!$finished) { |
| 36 | 36 | |
| 37 | 37 | // Assign each one of the points to one centroid |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | $bdist = INF; |
| 42 | 42 | for ($i = 0; $i < $nclusters; $i++) { |
| 43 | 43 | $ndist = eclideanDistance($npoints[$j], $nclusters[$i]); |
| 44 | - if($bdist > $ndist) { |
|
| 44 | + if ($bdist > $ndist) { |
|
| 45 | 45 | $bdist = $ndist; |
| 46 | 46 | $best = $i; |
| 47 | 47 | } |
@@ -50,10 +50,10 @@ discard block |
||
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // Check change |
| 53 | - if(count($resp)!=0) { |
|
| 53 | + if (count($resp)!=0) { |
|
| 54 | 54 | $finished = true; |
| 55 | 55 | for ($j = 0; $j < $npoints; $j++) { |
| 56 | - if($resp[$j]!==$nresp[$j]){ |
|
| 56 | + if ($resp[$j]!==$nresp[$j]) { |
|
| 57 | 57 | $finished = false; |
| 58 | 58 | break; |
| 59 | 59 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** Recalculate the centroids*/ |
| 65 | - $centroids = initCentroids($nclusters, $ndimensions, function(){return 0;}); |
|
| 65 | + $centroids = initCentroids($nclusters, $ndimensions, function() {return 0; }); |
|
| 66 | 66 | $counts = array_fill(0, $nclusters, 0); |
| 67 | 67 | for ($j = 0; $j < $npoints; $j++) { |
| 68 | 68 | sumCentroid($centroids[$resp[$j]], $data[$j]); |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | function eclideanDistance($p1, $p2) { |
| 95 | 95 | $len = count($p1); |
| 96 | 96 | $acum = 0; |
| 97 | - for($i=0; $i<$len; $i++) { |
|
| 97 | + for ($i = 0; $i < $len; $i++) { |
|
| 98 | 98 | $acum += ($p1[$i] - $p2[$i])**2; |
| 99 | 99 | } |
| 100 | 100 | return sqrt($acum); |
@@ -29,7 +29,9 @@ |
||
| 29 | 29 | $resp = []; |
| 30 | 30 | $finished = false; |
| 31 | 31 | $npoints = count($data); |
| 32 | - if (count($npoints) <= 0) throw new Exception("Not enough data. "); |
|
| 32 | + if (count($npoints) <= 0) { |
|
| 33 | + throw new Exception("Not enough data. "); |
|
| 34 | + } |
|
| 33 | 35 | $ndimensions = count($npoints[0]); |
| 34 | 36 | $centroids = initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;}); |
| 35 | 37 | while (!$finished) { |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public static function getPDO($config, $options) |
| 84 | 84 | { |
| 85 | - if (array_key_exists("pdo",$options)) { |
|
| 85 | + if (array_key_exists("pdo", $options)) { |
|
| 86 | 86 | return $options["pdo"]; |
| 87 | 87 | } |
| 88 | 88 | return Persistence::getPDO($config); |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | $result = $data; |
| 189 | - for ($i=1;$i<$level;$i++) { |
|
| 189 | + for ($i = 1; $i < $level; $i++) { |
|
| 190 | 190 | $tmp = Matrix::multiply($result, $data); |
| 191 | 191 | $result = Matrix::sum($result, $tmp); |
| 192 | 192 | } |
@@ -180,8 +180,7 @@ |
||
| 180 | 180 | |
| 181 | 181 | if (array_key_exists("level", $options)) { |
| 182 | 182 | $level = $options["level"]; |
| 183 | - } |
|
| 184 | - else { |
|
| 183 | + } else { |
|
| 185 | 184 | $level = count($labels) - 1; |
| 186 | 185 | } |
| 187 | 186 | |