Passed
Push — master ( a0d90f...a5d8ab )
by Ruben
01:49
created

Matrix::sumArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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