1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CL\Matrix; |
4
|
|
|
|
5
|
|
|
use CL\Matrix\Vector\Column; |
6
|
|
|
use CL\Matrix\Vector\Row; |
7
|
|
|
|
8
|
|
|
class Matrix |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Row[] |
12
|
|
|
*/ |
13
|
|
|
private $rows = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Column[] |
17
|
|
|
*/ |
18
|
|
|
private $columns = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Row[] $rowVectors |
22
|
|
|
*/ |
23
|
20 |
|
public function __construct(array $rowVectors = []) |
24
|
|
|
{ |
25
|
20 |
|
foreach ($rowVectors as $rowVector) { |
26
|
|
|
$this->addRow($rowVector); |
27
|
20 |
|
} |
28
|
20 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Row $row |
32
|
|
|
*/ |
33
|
6 |
View Code Duplication |
public function addRow(Row $row) |
|
|
|
|
34
|
|
|
{ |
35
|
6 |
|
array_push($this->rows, $row); |
36
|
|
|
|
37
|
6 |
|
$keys = array_keys($this->rows); |
38
|
6 |
|
$offset = end($keys); |
39
|
|
|
|
40
|
6 |
|
$row->setMatrix($this, $offset); |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int $offset |
45
|
|
|
* @param Row $row |
46
|
|
|
*/ |
47
|
6 |
|
public function setRow($offset, Row $row) |
48
|
|
|
{ |
49
|
6 |
|
$row->setMatrix($this, $offset); |
50
|
|
|
|
51
|
6 |
|
$this->rows[$offset] = $row; |
52
|
6 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $offset |
56
|
|
|
* |
57
|
|
|
* @return Row |
58
|
|
|
*/ |
59
|
10 |
View Code Duplication |
public function getRow($offset) |
|
|
|
|
60
|
|
|
{ |
61
|
10 |
|
if (!array_key_exists($offset, $this->rows)) { |
62
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
63
|
2 |
|
'There is no row in this matrix with that offset: %s', |
64
|
|
|
$offset |
65
|
2 |
|
)); |
66
|
|
|
} |
67
|
|
|
|
68
|
8 |
|
return $this->rows[$offset]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Row[] |
73
|
|
|
*/ |
74
|
10 |
|
public function getRows() |
75
|
|
|
{ |
76
|
10 |
|
return $this->rows; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $offset |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
8 |
|
public function hasRow($offset) |
85
|
|
|
{ |
86
|
8 |
|
return array_key_exists($offset, $this->rows); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Column $column |
91
|
|
|
*/ |
92
|
6 |
View Code Duplication |
public function addColumn(Column $column) |
|
|
|
|
93
|
|
|
{ |
94
|
6 |
|
array_push($this->columns, $column); |
95
|
|
|
|
96
|
6 |
|
$keys = array_keys($this->columns); |
97
|
6 |
|
$offset = end($keys); |
98
|
|
|
|
99
|
6 |
|
$column->setMatrix($this, $offset); |
100
|
6 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $offset |
104
|
|
|
* @param Column $column |
105
|
|
|
*/ |
106
|
6 |
|
public function setColumn($offset, Column $column) |
107
|
|
|
{ |
108
|
6 |
|
$column->setMatrix($this, $offset); |
109
|
|
|
|
110
|
6 |
|
$this->columns[$offset] = $column; |
111
|
6 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $offset |
115
|
|
|
* |
116
|
|
|
* @return Column |
117
|
|
|
*/ |
118
|
10 |
View Code Duplication |
public function getColumn($offset) |
|
|
|
|
119
|
|
|
{ |
120
|
10 |
|
if (!array_key_exists($offset, $this->columns)) { |
121
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
122
|
2 |
|
'There is no column in this matrix with that offset: %s', |
123
|
|
|
$offset |
124
|
2 |
|
)); |
125
|
|
|
} |
126
|
|
|
|
127
|
8 |
|
return $this->columns[$offset]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return Column[] |
132
|
|
|
*/ |
133
|
10 |
|
public function getColumns() |
134
|
|
|
{ |
135
|
10 |
|
return $this->columns; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $offset |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
8 |
|
public function hasColumn($offset) |
144
|
|
|
{ |
145
|
8 |
|
return array_key_exists($offset, $this->columns); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $rows |
150
|
|
|
* |
151
|
|
|
* @return Matrix |
152
|
|
|
*/ |
153
|
2 |
|
public static function createFromArray(array $rows) |
154
|
|
|
{ |
155
|
2 |
|
$matrix = new Matrix(); |
156
|
|
|
|
157
|
2 |
|
foreach ($rows as $offset => $row) { |
158
|
2 |
|
$matrix->setRow($offset, Row::createFromArray($row)); |
|
|
|
|
159
|
2 |
|
} |
160
|
|
|
|
161
|
2 |
|
return $matrix; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.