numbl 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-cli/cli.js +17781 -27499
- package/native/lapack_addon.cpp +2 -0
- package/native/lapack_common.h +8 -0
- package/native/lapack_svd.cpp +158 -0
- package/package.json +1 -1
package/native/lapack_addon.cpp
CHANGED
|
@@ -26,6 +26,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
26
26
|
Napi::Function::New(env, Qr));
|
|
27
27
|
exports.Set(Napi::String::New(env, "svd"),
|
|
28
28
|
Napi::Function::New(env, Svd));
|
|
29
|
+
exports.Set(Napi::String::New(env, "svdComplex"),
|
|
30
|
+
Napi::Function::New(env, SvdComplex));
|
|
29
31
|
exports.Set(Napi::String::New(env, "matmul"),
|
|
30
32
|
Napi::Function::New(env, Matmul));
|
|
31
33
|
exports.Set(Napi::String::New(env, "linsolve"),
|
package/native/lapack_common.h
CHANGED
|
@@ -49,6 +49,13 @@ extern "C" {
|
|
|
49
49
|
double* s, double* u, int* ldu, double* vt, int* ldvt,
|
|
50
50
|
double* work, int* lwork, int* iwork, int* info);
|
|
51
51
|
|
|
52
|
+
// Complex SVD using divide-and-conquer: A = U * Sigma * V^H
|
|
53
|
+
void zgesdd_(char* jobz, int* m, int* n, lapack_complex_double* a, int* lda,
|
|
54
|
+
double* s, lapack_complex_double* u, int* ldu,
|
|
55
|
+
lapack_complex_double* vt, int* ldvt,
|
|
56
|
+
lapack_complex_double* work, int* lwork, double* rwork,
|
|
57
|
+
int* iwork, int* info);
|
|
58
|
+
|
|
52
59
|
// ── Matrix-matrix multiplication (BLAS) ──────────────────────────────────
|
|
53
60
|
// C = alpha * op(A) * op(B) + beta * C
|
|
54
61
|
void dgemm_(char* transa, char* transb,
|
|
@@ -96,6 +103,7 @@ Napi::Value Inv(const Napi::CallbackInfo& info);
|
|
|
96
103
|
Napi::Value InvComplex(const Napi::CallbackInfo& info);
|
|
97
104
|
Napi::Value Qr(const Napi::CallbackInfo& info);
|
|
98
105
|
Napi::Value Svd(const Napi::CallbackInfo& info);
|
|
106
|
+
Napi::Value SvdComplex(const Napi::CallbackInfo& info);
|
|
99
107
|
Napi::Value Matmul(const Napi::CallbackInfo& info);
|
|
100
108
|
Napi::Value Linsolve(const Napi::CallbackInfo& info);
|
|
101
109
|
Napi::Value LinsolveComplex(const Napi::CallbackInfo& info);
|
package/native/lapack_svd.cpp
CHANGED
|
@@ -147,3 +147,161 @@ Napi::Value Svd(const Napi::CallbackInfo& info) {
|
|
|
147
147
|
|
|
148
148
|
return result;
|
|
149
149
|
}
|
|
150
|
+
|
|
151
|
+
// ── svdComplex() ─────────────────────────────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
Napi::Value SvdComplex(const Napi::CallbackInfo& info) {
|
|
154
|
+
Napi::Env env = info.Env();
|
|
155
|
+
|
|
156
|
+
if (info.Length() < 6
|
|
157
|
+
|| !info[0].IsTypedArray()
|
|
158
|
+
|| !info[1].IsTypedArray()
|
|
159
|
+
|| !info[2].IsNumber()
|
|
160
|
+
|| !info[3].IsNumber()
|
|
161
|
+
|| !info[4].IsBoolean()
|
|
162
|
+
|| !info[5].IsBoolean()) {
|
|
163
|
+
Napi::TypeError::New(env,
|
|
164
|
+
"svdComplex: expected (Float64Array dataRe, Float64Array dataIm, number m, number n, boolean econ, boolean computeUV)")
|
|
165
|
+
.ThrowAsJavaScriptException();
|
|
166
|
+
return env.Null();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
auto arrRe = info[0].As<Napi::TypedArray>();
|
|
170
|
+
auto arrIm = info[1].As<Napi::TypedArray>();
|
|
171
|
+
if (arrRe.TypedArrayType() != napi_float64_array || arrIm.TypedArrayType() != napi_float64_array) {
|
|
172
|
+
Napi::TypeError::New(env, "svdComplex: data must be Float64Arrays")
|
|
173
|
+
.ThrowAsJavaScriptException();
|
|
174
|
+
return env.Null();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
int m = info[2].As<Napi::Number>().Int32Value();
|
|
178
|
+
int n = info[3].As<Napi::Number>().Int32Value();
|
|
179
|
+
bool econ = info[4].As<Napi::Boolean>().Value();
|
|
180
|
+
bool computeUV = info[5].As<Napi::Boolean>().Value();
|
|
181
|
+
|
|
182
|
+
if (m <= 0 || n <= 0
|
|
183
|
+
|| static_cast<int>(arrRe.ElementLength()) != m * n
|
|
184
|
+
|| static_cast<int>(arrIm.ElementLength()) != m * n) {
|
|
185
|
+
Napi::RangeError::New(env, "svdComplex: data.length must equal m*n")
|
|
186
|
+
.ThrowAsJavaScriptException();
|
|
187
|
+
return env.Null();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
auto float64Re = info[0].As<Napi::Float64Array>();
|
|
191
|
+
auto float64Im = info[1].As<Napi::Float64Array>();
|
|
192
|
+
int k = m < n ? m : n;
|
|
193
|
+
|
|
194
|
+
// Convert split real/imag to interleaved complex format
|
|
195
|
+
std::vector<lapack_complex_double> a(m * n);
|
|
196
|
+
for (int i = 0; i < m * n; ++i) {
|
|
197
|
+
a[i].real = float64Re[i];
|
|
198
|
+
a[i].imag = float64Im[i];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
std::vector<double> s(k);
|
|
202
|
+
int info_val = 0;
|
|
203
|
+
|
|
204
|
+
char jobz;
|
|
205
|
+
if (!computeUV) {
|
|
206
|
+
jobz = 'N';
|
|
207
|
+
} else if (econ) {
|
|
208
|
+
jobz = 'S';
|
|
209
|
+
} else {
|
|
210
|
+
jobz = 'A';
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
int ldu, ldvt;
|
|
214
|
+
std::vector<lapack_complex_double> u_vec, vt_vec;
|
|
215
|
+
|
|
216
|
+
if (jobz == 'N') {
|
|
217
|
+
ldu = m;
|
|
218
|
+
ldvt = n;
|
|
219
|
+
} else if (jobz == 'S') {
|
|
220
|
+
ldu = m;
|
|
221
|
+
ldvt = k;
|
|
222
|
+
u_vec.resize(m * k);
|
|
223
|
+
vt_vec.resize(k * n);
|
|
224
|
+
} else { // 'A'
|
|
225
|
+
ldu = m;
|
|
226
|
+
ldvt = n;
|
|
227
|
+
u_vec.resize(m * m);
|
|
228
|
+
vt_vec.resize(n * n);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
lapack_complex_double* u_ptr = (jobz == 'N') ? nullptr : u_vec.data();
|
|
232
|
+
lapack_complex_double* vt_ptr = (jobz == 'N') ? nullptr : vt_vec.data();
|
|
233
|
+
|
|
234
|
+
// Compute rwork size for zgesdd
|
|
235
|
+
// For jobz='N': 5*min(m,n)
|
|
236
|
+
// For jobz='S' or 'A': max(5*min(m,n)^2 + 5*min(m,n), 2*max(m,n)*min(m,n) + 2*min(m,n)^2 + min(m,n))
|
|
237
|
+
int rwork_size;
|
|
238
|
+
if (jobz == 'N') {
|
|
239
|
+
rwork_size = 5 * k;
|
|
240
|
+
} else {
|
|
241
|
+
int t1 = 5 * k * k + 5 * k;
|
|
242
|
+
int t2 = 2 * std::max(m, n) * k + 2 * k * k + k;
|
|
243
|
+
rwork_size = std::max(t1, t2);
|
|
244
|
+
}
|
|
245
|
+
std::vector<double> rwork(rwork_size);
|
|
246
|
+
std::vector<int> iwork(8 * k);
|
|
247
|
+
|
|
248
|
+
// Workspace query
|
|
249
|
+
int lwork = -1;
|
|
250
|
+
lapack_complex_double work_query;
|
|
251
|
+
zgesdd_(&jobz, &m, &n, a.data(), &m, s.data(), u_ptr, &ldu, vt_ptr, &ldvt,
|
|
252
|
+
&work_query, &lwork, rwork.data(), iwork.data(), &info_val);
|
|
253
|
+
|
|
254
|
+
lwork = static_cast<int>(work_query.real);
|
|
255
|
+
if (lwork < 1) lwork = 3 * k + std::max(m, n);
|
|
256
|
+
|
|
257
|
+
std::vector<lapack_complex_double> work(lwork);
|
|
258
|
+
zgesdd_(&jobz, &m, &n, a.data(), &m, s.data(), u_ptr, &ldu, vt_ptr, &ldvt,
|
|
259
|
+
work.data(), &lwork, rwork.data(), iwork.data(), &info_val);
|
|
260
|
+
|
|
261
|
+
if (info_val != 0) {
|
|
262
|
+
Napi::Error::New(env, "svdComplex: zgesdd failed")
|
|
263
|
+
.ThrowAsJavaScriptException();
|
|
264
|
+
return env.Null();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Build result
|
|
268
|
+
auto result = Napi::Object::New(env);
|
|
269
|
+
|
|
270
|
+
// S is always real
|
|
271
|
+
auto S_arr = Napi::Float64Array::New(env, static_cast<size_t>(k));
|
|
272
|
+
std::memcpy(S_arr.Data(), s.data(), k * sizeof(double));
|
|
273
|
+
result.Set("S", S_arr);
|
|
274
|
+
|
|
275
|
+
if (computeUV) {
|
|
276
|
+
// U: convert from interleaved to split real/imag
|
|
277
|
+
int u_size = (jobz == 'S') ? m * k : m * m;
|
|
278
|
+
auto URe = Napi::Float64Array::New(env, static_cast<size_t>(u_size));
|
|
279
|
+
auto UIm = Napi::Float64Array::New(env, static_cast<size_t>(u_size));
|
|
280
|
+
for (int i = 0; i < u_size; i++) {
|
|
281
|
+
URe[i] = u_vec[i].real;
|
|
282
|
+
UIm[i] = u_vec[i].imag;
|
|
283
|
+
}
|
|
284
|
+
result.Set("URe", URe);
|
|
285
|
+
result.Set("UIm", UIm);
|
|
286
|
+
|
|
287
|
+
// V = conj(VT^T): conjugate transpose of VT
|
|
288
|
+
int vt_rows = (jobz == 'S') ? k : n;
|
|
289
|
+
int vt_cols = n;
|
|
290
|
+
int v_size = vt_rows * vt_cols;
|
|
291
|
+
auto VRe = Napi::Float64Array::New(env, static_cast<size_t>(v_size));
|
|
292
|
+
auto VIm = Napi::Float64Array::New(env, static_cast<size_t>(v_size));
|
|
293
|
+
for (int i = 0; i < vt_rows; i++) {
|
|
294
|
+
for (int j = 0; j < vt_cols; j++) {
|
|
295
|
+
// V(j, i) = conj(VT(i, j)) — column-major conjugate transpose
|
|
296
|
+
int v_idx = j + i * vt_cols;
|
|
297
|
+
int vt_idx = i + j * vt_rows;
|
|
298
|
+
VRe[v_idx] = vt_vec[vt_idx].real;
|
|
299
|
+
VIm[v_idx] = -vt_vec[vt_idx].imag; // conjugate
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
result.Set("VRe", VRe);
|
|
303
|
+
result.Set("VIm", VIm);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return result;
|
|
307
|
+
}
|