|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Samsara\Fermat\LinearAlgebra\Types\Traits\Matrix; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Samsara\Fermat\LinearAlgebra\Types\Base\Interfaces\Groups\MatrixInterface; |
|
8
|
|
|
use Samsara\Fermat\Core\Types\NumberCollection; |
|
9
|
|
|
|
|
10
|
|
|
trait ShapeTrait |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** @var NumberCollection[] */ |
|
14
|
|
|
protected $rows; |
|
15
|
|
|
/** @var NumberCollection[] */ |
|
16
|
|
|
protected $columns; |
|
17
|
|
|
/** @var int */ |
|
18
|
|
|
protected $numRows; |
|
19
|
|
|
/** @var int */ |
|
20
|
|
|
protected $numColumns; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
8 |
|
public function isSquare(): bool |
|
26
|
|
|
{ |
|
27
|
8 |
|
return $this->getRowCount() === $this->getColumnCount(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return int |
|
32
|
|
|
*/ |
|
33
|
30 |
|
public function getRowCount(): int |
|
34
|
|
|
{ |
|
35
|
30 |
|
return $this->numRows; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int $row |
|
40
|
|
|
* @return NumberCollection |
|
41
|
|
|
*/ |
|
42
|
34 |
|
public function getRow(int $row): NumberCollection |
|
43
|
|
|
{ |
|
44
|
34 |
|
return clone $this->rows[$row]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return int |
|
49
|
|
|
*/ |
|
50
|
30 |
|
public function getColumnCount(): int |
|
51
|
|
|
{ |
|
52
|
30 |
|
return $this->numColumns; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param int $column |
|
57
|
|
|
* @return NumberCollection |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function getColumn(int $column): NumberCollection |
|
60
|
|
|
{ |
|
61
|
2 |
|
return clone $this->columns[$column]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return MatrixInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getAdjoint(): MatrixInterface |
|
68
|
|
|
{ |
|
69
|
|
|
$rows = $this->columns; |
|
70
|
|
|
|
|
71
|
|
|
return $this->setValue($rows); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return MatrixInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getAdjugate(): MatrixInterface |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getAdjoint(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return MatrixInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function transpose(): MatrixInterface |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getAdjoint(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param bool $clockwise |
|
92
|
|
|
* @return MatrixInterface |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function rotate(bool $clockwise = true): MatrixInterface |
|
95
|
|
|
{ |
|
96
|
2 |
|
$tempData = $clockwise ? $this->rows : $this->columns; |
|
97
|
2 |
|
$mode = $clockwise ? self::MODE_COLUMNS_INPUT : self::MODE_ROWS_INPUT; |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
2 |
|
$tempData = array_reverse($tempData); |
|
100
|
|
|
|
|
101
|
2 |
|
return $this->setValue($tempData, $mode); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |