Passed
Push — main ( b63ebb...d6f51d )
by Shubham
02:04
created

rref::factory()   B

Complexity

Conditions 10
Paths 3

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 3
nop 1
dl 0
loc 35
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
namespace Np\linAlgb\reductions;
4
5
use Np\matrix;
6
7
/**
8
 * RREF
9
 *
10
 * The reduced row echelon form (RREF) of a matrix.
11
 * 
12
 * @package Np
13
 * @category Scientific Computing
14
 * @author ghost (Shubham Chaudhary)
15
 * @email [email protected]
16
 * @copyright (c) 2020-2021, Shubham Chaudhary
17
 */
18
19
class rref { 
20
     
21
    /**
22
     * 
23
     * @param \Np\matrix $m
24
     * @return matrix
25
     */
26
    public static function factory(\Np\matrix $m): matrix {
27
        $lead = 0;
28
        $ar = $m->copy();
29
        for ($r = 0; $r < $ar->row; ++$r) {
30
            if ($lead >= $ar->col)
31
                break; {
32
                $i = $r;
33
                while ($ar->data[$i * $ar->col + $lead] == 0) {
34
                    $i++;
35
                    if ($i == $ar->row) {
36
                        $i = $r;
37
                        $lead++;
38
                        if ($lead == $ar->col) {
39
                            return $ar;
40
                        }
41
                    }
42
                }
43
                $ar->swapRows($r, $i);
44
            } {
45
                $lv = $ar->data[$r * $ar->col + $lead];
46
                for ($j = 0; $j < $ar->col; ++$j) {
47
                    $ar->data[$r * $ar->col + $j] = $ar->data[$r * $ar->col + $j] / $lv;
48
                }
49
            }
50
            for ($i = 0; $i < $ar->row; ++$i) {
51
                if ($i != $r) {
52
                    $lv = $ar->data[$i * $ar->col + $lead];
53
                    for ($j = 0; $j < $ar->col; ++$j) {
54
                        $ar->data[$i * $ar->col + $j] -= $lv * $ar->data[$r * $ar->col + $j];
55
                    }
56
                }
57
            }
58
            $lead++;
59
        }
60
        return $ar;
61
    }
62
}
63