@@ 8-76 (lines=69) @@ | ||
5 | use CL\Matrix\Element; |
|
6 | use CL\Matrix\Matrix; |
|
7 | ||
8 | class Column extends AbstractVector |
|
9 | { |
|
10 | /** |
|
11 | * @param Matrix $matrix |
|
12 | * @param int $matrixOffset |
|
13 | */ |
|
14 | public function setMatrix(Matrix $matrix, $matrixOffset) |
|
15 | { |
|
16 | $this->matrix = $matrix; |
|
17 | ||
18 | foreach ($this->getElements() as $offset => $element) { |
|
19 | if (!$this->matrix->hasRow($offset)) { |
|
20 | $this->matrix->setRow($offset, new Row([])); |
|
21 | } |
|
22 | ||
23 | $this->matrix->getRow($offset)->addElement($element); |
|
24 | } |
|
25 | ||
26 | $this->matrixOffset = $matrixOffset; |
|
27 | } |
|
28 | ||
29 | /** |
|
30 | * @param Element $element |
|
31 | */ |
|
32 | public function addElement(Element $element) |
|
33 | { |
|
34 | array_push($this->elements, $element); |
|
35 | ||
36 | $keys = array_keys($this->elements); |
|
37 | $offset = end($keys); |
|
38 | ||
39 | if ($this->matrix) { |
|
40 | if ($this->matrix->hasRow($offset)) { |
|
41 | $this->matrix->getRow($offset)->addElement($element); |
|
42 | } |
|
43 | } |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * @param int $offset |
|
48 | * @param Element $element |
|
49 | */ |
|
50 | public function setElement($offset, Element $element) |
|
51 | { |
|
52 | $this->elements[$offset] = $element; |
|
53 | ||
54 | if ($this->matrix) { |
|
55 | if ($this->matrix->hasRow($offset)) { |
|
56 | $this->matrix->getRow($offset)->setElement($this->matrixOffset, $element); |
|
57 | } |
|
58 | } |
|
59 | } |
|
60 | ||
61 | /** |
|
62 | * @param mixed[] $values |
|
63 | * |
|
64 | * @return Column |
|
65 | */ |
|
66 | public static function createFromArray(array $values) |
|
67 | { |
|
68 | $vector = new self; |
|
69 | ||
70 | foreach ($values as $offset => $value) { |
|
71 | $vector->setElement($offset, new Element($value)); |
|
72 | } |
|
73 | ||
74 | return $vector; |
|
75 | } |
|
76 | } |
|
77 |
@@ 8-76 (lines=69) @@ | ||
5 | use CL\Matrix\Element; |
|
6 | use CL\Matrix\Matrix; |
|
7 | ||
8 | class Row extends AbstractVector |
|
9 | { |
|
10 | /** |
|
11 | * @param Matrix $matrix |
|
12 | * @param int $matrixOffset |
|
13 | */ |
|
14 | public function setMatrix(Matrix $matrix, $matrixOffset) |
|
15 | { |
|
16 | $this->matrix = $matrix; |
|
17 | ||
18 | foreach ($this->getElements() as $offset => $element) { |
|
19 | if (!$this->matrix->hasColumn($offset)) { |
|
20 | $this->matrix->setColumn($offset, new Column([])); |
|
21 | } |
|
22 | ||
23 | $this->matrix->getColumn($offset)->addElement($element); |
|
24 | } |
|
25 | ||
26 | $this->matrixOffset = $matrixOffset; |
|
27 | } |
|
28 | ||
29 | /** |
|
30 | * @param Element $element |
|
31 | */ |
|
32 | public function addElement(Element $element) |
|
33 | { |
|
34 | array_push($this->elements, $element); |
|
35 | ||
36 | $keys = array_keys($this->elements); |
|
37 | $offset = end($keys); |
|
38 | ||
39 | if ($this->matrix) { |
|
40 | if ($this->matrix->hasColumn($offset)) { |
|
41 | $this->matrix->getColumn($offset)->addElement($element); |
|
42 | } |
|
43 | } |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * @param int $offset |
|
48 | * @param Element $element |
|
49 | */ |
|
50 | public function setElement($offset, Element $element) |
|
51 | { |
|
52 | $this->elements[$offset] = $element; |
|
53 | ||
54 | if ($this->matrix) { |
|
55 | if ($this->matrix->hasColumn($offset)) { |
|
56 | $this->matrix->getColumn($offset)->setElement($this->matrixOffset, $element); |
|
57 | } |
|
58 | } |
|
59 | } |
|
60 | ||
61 | /** |
|
62 | * @param mixed[] $values |
|
63 | * |
|
64 | * @return Row |
|
65 | */ |
|
66 | public static function createFromArray(array $values) |
|
67 | { |
|
68 | $vector = new self; |
|
69 | ||
70 | foreach ($values as $offset => $value) { |
|
71 | $vector->setElement($offset, new Element($value)); |
|
72 | } |
|
73 | ||
74 | return $vector; |
|
75 | } |
|
76 | } |
|
77 |