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

svd   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A v() 0 2 1
A __construct() 0 1 1
A u() 0 2 1
A vt() 0 2 1
A s() 0 2 1
A factory() 0 14 2
1
<?php
2
3
declare (strict_types=1);
4
5
namespace Np\linAlgb\decompositions;
6
7
use Np\matrix;
8
use Np\vector;
9
use Np\core\lapack;
10
11
/**
12
 * SVD
13
 * Compute the singular value decomposition of a matrix and 
14
 * return an object of the singular values and unitary matrices
15
 * 
16
 * @package Np
17
 * @category Scientific Library for Php
18
 * @author ghost (Shubham Chaudhary)
19
 * @email [email protected]
20
 * @copyright (c) 2020-2021, Shubham Chaudhary
21
 */
22
class svd {
23
24
    /**
25
     * 
26
     * @param matrix $m
27
     * @return self
28
     */
29
    public static function factory(\Np\matrix $m): self {
30
        $k = min($m->row, $m->col);
31
        $ar = $m->copy();
32
        $s = vector::factory($k);
33
        $u = matrix::factory($m->row, $m->row);
34
        $v = matrix::factory($m->col, $m->col);
35
        $lp = lapack::gesdd($ar, $s, $u, $v);
36
        if ($lp != 0) {
37
            return null;
38
        }
39
        unset($ar);
40
        unset($k);
41
        unset($lp);
42
        return new self($u, $v, $s);
43
    }
44
45
    /**
46
     * 
47
     * @param \Np\matrix $u
48
     * @param \Np\matrix $v
49
     * @param \Np\vector $s
50
     */
51
    protected function __construct(protected \Np\matrix $u, protected \Np\matrix $v, protected \Np\vector $s) {
52
        
53
    }
54
55
    /**
56
     * Return the U matrix.
57
     * 
58
     * @return matrix
59
     */
60
    public function u(): matrix {
61
        return $this->u;
62
    }
63
64
    /**
65
     * Return the singular values of matrix.
66
     * 
67
     * @return vector
68
     */
69
    public function s(): vector {
70
        return $this->s;
71
    }
72
73
    /**
74
     * Return the V matrix.
75
     * 
76
     * @return matrix
77
     */
78
    public function v(): matrix {
79
        return $this->v;
80
    }
81
82
    /**
83
     * Return the V transposed matrix.
84
     *
85
     * @return matrix
86
     */
87
    public function vt(): matrix {
88
        return $this->v->transpose();
89
    }
90
91
}
92