Failed Conditions
Pull Request — dev (#50)
by Jordan
09:06 queued 04:26
created

Matrices::cofactorMatrix()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
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 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 5
    public static function onesMatrix(string $type, int $rows, int $columns): MatrixInterface
31
    {
32 5
        $matrixData = [];
33
34 5
        for ($i = 0;$i < $rows;$i++) {
35 5
            $matrixData[$i] = new NumberCollection();
36 5
            for ($n = 0;$n < $columns;$n++) {
37 5
                $matrixData[$i]->push(Numbers::makeOne());
38
            }
39
        }
40
41 5
        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
}