Failed Conditions
Pull Request — dev (#50)
by Jordan
12:22
created

Matrices::cofactorMatrix()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 2
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 20
rs 10
c 0
b 0
f 0
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
    public static function zeroMatrix(string $type, int $rows, int $columns): MatrixInterface
17
    {
18
        $matrixData = [];
19
20
        for ($i = 0;$i < $rows;$i++) {
21
            $matrixData[$i] = new NumberCollection();
22
            for ($n = 0;$n < $columns;$n++) {
23
                $matrixData[$i]->push(Numbers::makeZero());
24
            }
25
        }
26
27
        return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData);
28
    }
29
30
    public static function onesMatrix(string $type, int $rows, int $columns): MatrixInterface
31
    {
32
        $matrixData = [];
33
34
        for ($i = 0;$i < $rows;$i++) {
35
            $matrixData[$i] = new NumberCollection();
36
            for ($n = 0;$n < $columns;$n++) {
37
                $matrixData[$i]->push(Numbers::makeOne());
38
            }
39
        }
40
41
        return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData);
42
    }
43
44
    public static function identityMatrix(string $type, int $size): MatrixInterface
45
    {
46
        $matrixData = [];
47
48
        for ($i = 0;$i < $size;$i++) {
49
            $matrixData[$i] = new NumberCollection();
50
            for ($n = 0;$n < $size;$n++) {
51
                if ($n === $i) {
52
                    $matrixData[$i]->push(Numbers::makeOne());
53
                } else {
54
                    $matrixData[$i]->push(Numbers::makeZero());
55
                }
56
            }
57
        }
58
59
        return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData);
60
    }
61
62
    public static function cofactorMatrix(string $type, int $size)
63
    {
64
65
        $matrixData = [];
66
67
        $p = 0;
68
69
        for ($i = 0;$i < $size;$i++) {
70
            $matrixData[$i] = SequenceProvider::nthPowerNegativeOne($p, true, $size);
71
            $p = ($p === 0 ? 1 : 0);
72
        }
73
74
        return ($type === Matrices::IMMUTABLE_MATRIX) ? new ImmutableMatrix($matrixData) : new MutableMatrix($matrixData);
75
    }
76
77
}