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
|
|
|
if ($mat->dtype == \Np\matrix::FLOAT) { |
41
|
|
|
return self::$ffi_lapack->LAPACKE_sgetrf($matLayout, $mat->row, $mat->col, $mat->data, $mat->row, $ipiv->data); |
42
|
|
|
} |
43
|
|
|
return self::$ffi_lapack->LAPACKE_dgetrf($matLayout, $mat->row, $mat->col, $mat->data, $mat->row, $ipiv->data); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @param \Np\matrix $mat |
49
|
|
|
* @param \Np\vector $ipiv |
50
|
|
|
* @param int $matLayout |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public static function getri(\Np\matrix $mat, \Np\vector $ipiv, int $matLayout = self::ROW_MAJOR) { |
54
|
|
|
self::init(); |
55
|
|
|
if ($mat->dtype == \Np\matrix::FLOAT) { |
56
|
|
|
return self::$ffi_lapack->LAPACKE_sgetri($matLayout, $mat->row, $mat->data, $mat->row, $ipiv->data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return self::$ffi_lapack->LAPACKE_dgetri($matLayout, $mat->row, $mat->data, $mat->row, $ipiv->data); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @param \Np\matrix $mat |
65
|
|
|
* @param \Np\vector $s |
66
|
|
|
* @param \Np\matrix $u |
67
|
|
|
* @param \Np\matrix $vt |
68
|
|
|
* @param int $matLayout |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public static function gesdd(\Np\matrix $mat, \Np\vector $s, \Np\matrix $u, \Np\matrix $vt, int $matLayout = self::ROW_MAJOR) { |
72
|
|
|
self::init(); |
73
|
|
|
if ($mat->dtype == \Np\matrix::FLOAT) { |
74
|
|
|
return self::$ffi_lapack->LAPACKE_sgesdd($matLayout, 'A', $mat->row, $mat->col, $mat->data, $mat->col, $s->data, $u->data, $mat->row, $vt->data, $mat->col); |
75
|
|
|
} |
76
|
|
|
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); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @param \Np\matrix $mat |
82
|
|
|
* @param string $uplo |
83
|
|
|
* @param int $matLayout |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public static function potrf(\Np\matrix $mat, $uplo = self::Lower, int $matLayout = self::ROW_MAJOR) { |
87
|
|
|
self::init(); |
88
|
|
|
if ($mat->dtype == \Np\matrix::FLOAT) { |
89
|
|
|
return self::$ffi_lapack->LAPACKE_spotrf($matLayout, $uplo, $mat->col, $mat->data, $mat->col); |
90
|
|
|
} |
91
|
|
|
return self::$ffi_lapack->LAPACKE_dpotrf($matLayout, $uplo, $mat->col, $mat->data, $mat->col); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param \Np\matrix $mat |
97
|
|
|
* @param \Np\vector $wr |
98
|
|
|
* @param \Np\vector $wi |
99
|
|
|
* @param \Np\matrix $vr |
100
|
|
|
* @param int $matLayout |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public static function geev(\Np\matrix $mat, \Np\vector $wr, \Np\vector $wi, \Np\matrix $vr, int $matLayout = self::ROW_MAJOR) { |
104
|
|
|
self::init(); |
105
|
|
|
if ($mat->dtype == \Np\matrix::FLOAT) { |
106
|
|
|
return self::$ffi_lapack->LAPACKE_sgeev($matLayout, 'N', 'V', $mat->col, $mat->data, $mat->col, $wr->data, $wi->data, null, $mat->col, $vr->data, $mat->col); |
107
|
|
|
} |
108
|
|
|
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); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* @param \Np\matrix $mat |
114
|
|
|
* @param \Np\vector $wr |
115
|
|
|
* @param int $matLayout |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public static function syev(\Np\matrix $mat, \Np\vector $wr, int $matLayout = self::ROW_MAJOR) { |
119
|
|
|
self::init(); |
120
|
|
|
if ($mat->dtype == \Np\matrix::FLOAT) { |
121
|
|
|
return self::$ffi_lapack->LAPACKE_ssyev($matLayout, 'V', 'U', $mat->col, $mat->data, $mat->col, $wr->data); |
122
|
|
|
} |
123
|
|
|
return self::$ffi_lapack->LAPACKE_dsyev($matLayout, 'V', 'U', $mat->col, $mat->data, $mat->col, $wr->data); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
* @param string $norm |
129
|
|
|
* @param \Np\matrix $m |
130
|
|
|
* @param int $matLayout |
131
|
|
|
* @return \FFI\CData |
132
|
|
|
*/ |
133
|
|
|
public static function lange(string $norm, \Np\matrix $m, int $matLayout = self::ROW_MAJOR) { |
134
|
|
|
self::init(); |
135
|
|
|
if ($m->dtype == \Np\matrix::FLOAT) { |
136
|
|
|
return self::$ffi_lapack->LAPACKE_slange($matLayout, $norm, $m->row, $m->col, $m->data, $m->col); |
137
|
|
|
} |
138
|
|
|
return self::$ffi_lapack->LAPACKE_dlange($matLayout, $norm, $m->row, $m->col, $m->data, $m->col); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* @param \Np\vector $v |
144
|
|
|
* @param $id |
145
|
|
|
* @return \FFI\CData |
146
|
|
|
*/ |
147
|
|
|
public static function sort(\Np\vector $v, $id = self::INCR) { |
148
|
|
|
self::init(); |
149
|
|
|
if ($v->dtype == \Np\vector::FLOAT) { |
150
|
|
|
return self::$ffi_lapack->LAPACKE_slasrt($id, $v->col, $v->data); |
151
|
|
|
} |
152
|
|
|
return self::$ffi_lapack->LAPACKE_dlasrt($id, $v->col, $v->data); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* The routine solves overdetermined or underdetermined real linear systems |
158
|
|
|
* involving an m-by-n matrix M, or its transpose, using a QR or LQ |
159
|
|
|
* factorization of M. It is assumed that M has full rank. |
160
|
|
|
* @param \Np\matrix $m |
161
|
|
|
* @param \Np\matrix|\Np\vector $b |
162
|
|
|
* @param int $matLayout |
163
|
|
|
* @param string $trans |
164
|
|
|
* @return type |
165
|
|
|
*/ |
166
|
|
|
public static function gels(\Np\matrix $m, \Np\matrix|\Np\vector $b, int $matLayout = self::ROW_MAJOR,string $trans = 'N') { |
167
|
|
|
self::init(); |
168
|
|
|
if($m->dtype == \Np\matrix::FLOAT){ |
169
|
|
|
return self::$ffi_lapack->LAPACKE_sgels( $matLayout, $trans, $m->row, $m->col, $b->col, $m->data, |
170
|
|
|
$m->col, $b->data, $b->col ); |
171
|
|
|
} |
172
|
|
|
return self::$ffi_lapack->LAPACKE_dgels( $matLayout, $trans, $m->row, $m->col, $b->col, $m->data, |
173
|
|
|
$m->col, $b->data, $b->col ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|