1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Np\core; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Lapack |
7
|
|
|
* |
8
|
|
|
* php interface for LAPACK |
9
|
|
|
* |
10
|
|
|
* @package Np |
11
|
|
|
* @category Scientific Computing |
12
|
|
|
* @author ghost (Shubham Chaudhary) |
13
|
|
|
* @email [email protected] |
14
|
|
|
* @copyright (c) 2020-2021, Shubham Chaudhary |
15
|
|
|
*/ |
16
|
|
|
class lapack { |
17
|
|
|
|
18
|
|
|
const ROW_MAJOR = 101, COL_MAJOR = 102; |
19
|
|
|
const Upper = 'U', Lower = 'L'; |
20
|
|
|
const INCR = 'I', DECR = 'D'; |
21
|
|
|
|
22
|
|
|
public static $ffi_lapack; |
23
|
|
|
|
24
|
|
|
public static function init() { |
25
|
|
|
if (is_null(self::$ffi_lapack)) { |
26
|
|
|
self::$ffi_lapack = \FFI::scope('lapack'); |
27
|
|
|
} |
28
|
|
|
return self::$ffi_lapack; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param \Np\matrix $mat |
34
|
|
|
* @param \Np\vector $ipiv |
35
|
|
|
* @param int $matLayout |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
public static function getrf(\Np\matrix $mat, \Np\vector $ipiv, int $matLayout = self::ROW_MAJOR) { |
39
|
|
|
self::init(); |
40
|
|
|
return self::$ffi_lapack->LAPACKE_dgetrf($matLayout, $mat->row, $mat->col, $mat->data, $mat->row, $ipiv->data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @param \Np\matrix $mat |
46
|
|
|
* @param \Np\vector $ipiv |
47
|
|
|
* @param int $matLayout |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
public static function getri(\Np\matrix $mat, \Np\vector $ipiv, int $matLayout = self::ROW_MAJOR) { |
51
|
|
|
self::init(); |
52
|
|
|
return self::$ffi_lapack->LAPACKE_dgetri($matLayout, $mat->row, $mat->data, $mat->row, $ipiv->data); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* @param \Np\matrix $mat |
58
|
|
|
* @param \Np\vector $s |
59
|
|
|
* @param \Np\matrix $u |
60
|
|
|
* @param \Np\matrix $vt |
61
|
|
|
* @param int $matLayout |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
public static function gesdd(\Np\matrix $mat, \Np\vector $s, \Np\matrix $u, \Np\matrix $vt, int $matLayout = self::ROW_MAJOR) { |
65
|
|
|
self::init(); |
66
|
|
|
return self::$ffi_lapack->LAPACKE_dgesdd($matLayout, 'A', $mat->row, $mat->col, $mat->data, $mat->col, $s->data, $u->data, $mat->row, $vt->data, $mat->col); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* @param \Np\matrix $mat |
72
|
|
|
* @param string $uplo |
73
|
|
|
* @param int $matLayout |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public static function potrf(\Np\matrix $mat, $uplo = self::Lower, int $matLayout = self::ROW_MAJOR) { |
77
|
|
|
self::init(); |
78
|
|
|
return self::$ffi_lapack->LAPACKE_dpotrf($matLayout, $uplo, $mat->col, $mat->data, $mat->col); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @param \Np\matrix $mat |
84
|
|
|
* @param \Np\vector $wr |
85
|
|
|
* @param \Np\vector $wi |
86
|
|
|
* @param \Np\matrix $vr |
87
|
|
|
* @param int $matLayout |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public static function geev(\Np\matrix $mat, \Np\vector $wr, \Np\vector $wi, \Np\matrix $vr, int $matLayout = self::ROW_MAJOR) { |
91
|
|
|
self::init(); |
92
|
|
|
return self::$ffi_lapack->LAPACKE_dgeev($matLayout, 'N', 'V', $mat->col, $mat->data, $mat->col, $wr->data, $wi->data, null, $mat->col, $vr->data, $mat->col); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* @param \Np\matrix $mat |
98
|
|
|
* @param \Np\vector $wr |
99
|
|
|
* @param int $matLayout |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public static function syev(\Np\matrix $mat, \Np\vector $wr, int $matLayout = self::ROW_MAJOR) { |
103
|
|
|
self::init(); |
104
|
|
|
return self::$ffi_lapack->LAPACKE_dsyev($matLayout, 'V', 'U', $mat->col, $mat->data, $mat->col, $wr->data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @param string $norm |
110
|
|
|
* @param \Np\matrix $m |
111
|
|
|
* @param int $matLayout |
112
|
|
|
* @return \FFI\CData |
113
|
|
|
*/ |
114
|
|
|
public static function lange(string $norm, \Np\matrix $m, int $matLayout = self::ROW_MAJOR) { |
115
|
|
|
self::init(); |
116
|
|
|
return self::$ffi_lapack->LAPACKE_dlange($matLayout, $norm, $m->row, $m->col, $m->data, $m->col); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @param \Np\vector $v |
122
|
|
|
* @param $id |
123
|
|
|
* @return \FFI\CData |
124
|
|
|
*/ |
125
|
|
|
public static function sort(\Np\vector $v, $id = self::INCR) { |
126
|
|
|
self::init(); |
127
|
|
|
return self::$ffi_lapack->LAPACKE_dlasrt($id, $v->col, $v->data); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* The routine solves overdetermined or underdetermined real linear systems |
133
|
|
|
* involving an m-by-n matrix M, or its transpose, using a QR or LQ |
134
|
|
|
* factorization of M. It is assumed that M has full rank. |
135
|
|
|
* @param \Np\matrix $m |
136
|
|
|
* @param \Np\matrix|\Np\vector $b |
137
|
|
|
* @param int $matLayout |
138
|
|
|
* @param string $trans |
139
|
|
|
* @return type |
140
|
|
|
*/ |
141
|
|
|
public static function gels(\Np\matrix $m, \Np\matrix|\Np\vector $b, int $matLayout = self::ROW_MAJOR, string $trans = 'N') { |
142
|
|
|
self::init(); |
143
|
|
|
return self::$ffi_lapack->LAPACKE_dgels($matLayout, $trans, $m->row, $m->col, $b->col, $m->data, |
144
|
|
|
$m->col, $b->data, $b->col); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|