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

eigen   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A eigenVal() 0 2 1
A factory() 0 23 5
A __construct() 0 3 1
A eigenVec() 0 2 1
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
use Np\exceptions\invalidArgumentException;
11
12
/**
13
 * Eigen
14
 *
15
 * The Eigen decompositon or (Spectral decomposition) is a matrix factorization resulting in a matrix of eigenvectors and a
16
 * corresponding array of eigenvalues.
17
 * 
18
 * @package Np
19
 * @category Scientific Library
20
 * @author ghost (Shubham Chaudhary)
21
 * @email [email protected]
22
 * @copyright (c) 2020-2021, Shubham Chaudhary
23
 */
24
class eigen {
25
26
    protected $eignVal;
27
    protected $eignVec;
28
29
    public static function factory(\Np\matrix $m, bool $symmetric = false): self {
30
        if (!$m->isSquare()) {
31
            throw new invalidArgumentException('A Non Square Matrix is given!');
32
        }
33
        $wr = vector::factory($m->col);
34
        $ar = $m->copy();
35
        if ($symmetric) {
36
            $lp = lapack::syev($ar, $wr);
37
            if ($lp != 0) {
38
                return null;
39
            }
40
41
            return new self($wr, $ar);
42
        } else {
43
            $wi = vector::factory($m->col);
44
            $vr = matrix::factory($m->col, $m->col);
45
46
            $lp = lapack::geev($ar, $wr, $wi, $vr);
47
            if ($lp != 0) {
48
                return null;
49
            }
50
51
            return new self($wr, $vr);
52
        }
53
    }
54
55
    /**
56
     * 
57
     * @param vector $eignVal
58
     * @param matrix $eignVec
59
     */
60
    protected function __construct(vector $eignVal, matrix $eignVec) {
61
        $this->eignVal = $eignVal;
62
        $this->eignVec = $eignVec;
63
    }
64
65
    /**
66
     * Return the eigenvalues of the eigen decomposition.
67
     * 
68
     * @return vector
69
     */
70
    public function eigenVal(): vector {
71
        return $this->eignVal;
72
    }
73
74
    /**
75
     * Return the eigen vectors of the eigen decomposition.
76
     * 
77
     * @return matrix
78
     */
79
    public function eigenVec(): matrix {
80
        return $this->eignVec->transpose();
81
    }
82
83
}
84