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