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

cholesky   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A factory() 0 14 5
1
<?php
2
3
declare (strict_types=1);
4
5
namespace Np\linAlgb\decompositions;
6
7
use Np\matrix;
8
use Np\core\lapack;
9
10
/**
11
 * Cholesky
12
 *
13
 * An efficient decomposition of a square positive definite matrix into a
14
 * lower triangular matrix and its conjugate transpose.
15
 * 
16
 * @package Np
17
 * @category Scientific Library
18
 * @author ghost (Shubham Chaudhary)
19
 * @email [email protected]
20
 * @copyright (c) 2020-2021, Shubham Chaudhary
21
 */
22
class cholesky {
23
24
    /**
25
     * 
26
     * @param matrix $m
27
     * @return matrix|null
28
     * 
29
     */
30
    public static function factory(matrix $m): matrix|null {
31
        if ($m->isSquare()) {
32
            $ar = $m->copy();
33
            $lp = lapack::potrf($ar);
34
            if ($lp != 0) {
35
                return null;
36
            }
37
            for ($i = 0; $i < $m->col; ++$i) {
38
                for ($j = $i + 1; $j < $m->col; ++$j) {
39
                    $ar->data[$i * $m->col + $j] = 0.0;
40
                }
41
            }
42
            unset($lp);
43
            return $ar;
44
        }
45
    }
46
47
}
48