|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Samsara\Fermat; |
|
4
|
|
|
|
|
5
|
|
|
use Samsara\Fermat\Provider\SequenceProvider; |
|
6
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Groups\MatrixInterface; |
|
7
|
|
|
use Samsara\Fermat\Types\NumberCollection; |
|
8
|
|
|
use Samsara\Fermat\Values\ImmutableMatrix; |
|
9
|
|
|
use Samsara\Fermat\Values\MutableMatrix; |
|
10
|
|
|
|
|
11
|
|
|
class Matrices |
|
12
|
|
|
{ |
|
13
|
|
|
const IMMUTABLE_MATRIX = ImmutableMatrix::class; |
|
14
|
|
|
const MUTABLE_MATRIX = MutableMatrix::class; |
|
15
|
|
|
|
|
16
|
1 |
|
public static function zeroMatrix(string $type, int $rows, int $columns): MatrixInterface |
|
17
|
|
|
{ |
|
18
|
1 |
|
$matrixData = []; |
|
19
|
|
|
|
|
20
|
1 |
|
for ($i = 0;$i < $rows;$i++) { |
|
21
|
1 |
|
$matrixData[$i] = new NumberCollection(); |
|
22
|
1 |
|
for ($n = 0;$n < $columns;$n++) { |
|
23
|
1 |
|
$matrixData[$i]->push(Numbers::makeZero()); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
public static function onesMatrix(string $type, int $rows, int $columns): MatrixInterface |
|
31
|
|
|
{ |
|
32
|
7 |
|
$matrixData = []; |
|
33
|
|
|
|
|
34
|
7 |
|
for ($i = 0;$i < $rows;$i++) { |
|
35
|
7 |
|
$matrixData[$i] = new NumberCollection(); |
|
36
|
7 |
|
for ($n = 0;$n < $columns;$n++) { |
|
37
|
7 |
|
$matrixData[$i]->push(Numbers::makeOne()); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
7 |
|
return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
public static function identityMatrix(string $type, int $size): MatrixInterface |
|
45
|
|
|
{ |
|
46
|
3 |
|
$matrixData = []; |
|
47
|
|
|
|
|
48
|
3 |
|
for ($i = 0;$i < $size;$i++) { |
|
49
|
3 |
|
$matrixData[$i] = new NumberCollection(); |
|
50
|
3 |
|
for ($n = 0;$n < $size;$n++) { |
|
51
|
3 |
|
if ($n === $i) { |
|
52
|
3 |
|
$matrixData[$i]->push(Numbers::makeOne()); |
|
53
|
|
|
} else { |
|
54
|
3 |
|
$matrixData[$i]->push(Numbers::makeZero()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public static function cofactorMatrix(string $type, int $size) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
1 |
|
$matrixData = []; |
|
66
|
|
|
|
|
67
|
1 |
|
$p = 0; |
|
68
|
|
|
|
|
69
|
1 |
|
for ($i = 0;$i < $size;$i++) { |
|
70
|
1 |
|
$matrixData[$i] = SequenceProvider::nthPowerNegativeOne($p, true, $size); |
|
71
|
1 |
|
$p = ($p === 0 ? 1 : 0); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |