numbl 0.0.5 → 0.0.7

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.
@@ -4,7 +4,7 @@
4
4
  <meta charset="UTF-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
6
  <title>numbl - Figures</title>
7
- <script type="module" crossorigin src="/assets/index-CYInUJ8O.js"></script>
7
+ <script type="module" crossorigin src="/assets/index-_FVS_eKT.js"></script>
8
8
  </head>
9
9
  <body>
10
10
  <div id="root"></div>
@@ -9,6 +9,7 @@
9
9
  * svd(data, m, n, econ, computeUV) — Singular Value Decomp. (lapack_svd.cpp)
10
10
  * matmul(A, m, k, B, n) — matrix-matrix multiply (lapack_matmul.cpp)
11
11
  * linsolve(A, m, n, B, nrhs) — linear solve / least-sq (lapack_linsolve.cpp)
12
+ * linsolveComplex(ARe, AIm, m, n, BRe, BIm, nrhs) — complex linear solve (lapack_linsolve.cpp)
12
13
  * eig(data, n, computeVL, computeVR, balance) — eigenvalue decomp. (lapack_eig.cpp)
13
14
  */
14
15
 
@@ -29,6 +30,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
29
30
  Napi::Function::New(env, Matmul));
30
31
  exports.Set(Napi::String::New(env, "linsolve"),
31
32
  Napi::Function::New(env, Linsolve));
33
+ exports.Set(Napi::String::New(env, "linsolveComplex"),
34
+ Napi::Function::New(env, LinsolveComplex));
32
35
  exports.Set(Napi::String::New(env, "eig"),
33
36
  Napi::Function::New(env, Eig));
34
37
  return exports;
@@ -70,6 +70,17 @@ extern "C" {
70
70
  double* a, int* lda, double* b, int* ldb,
71
71
  double* work, int* lwork, int* info);
72
72
 
73
+ // ── Complex linear solve (square) ────────────────────────────────────────
74
+ // LU factorisation + solve for complex matrices: A * X = B (A is n×n, B is n×nrhs)
75
+ void zgesv_(int* n, int* nrhs, lapack_complex_double* a, int* lda, int* ipiv,
76
+ lapack_complex_double* b, int* ldb, int* info);
77
+
78
+ // ── Complex linear least-squares / minimum-norm solve (general) ──────────
79
+ // Uses QR (trans='N', m>=n) or LQ (trans='N', m<n) factorisation for complex matrices.
80
+ void zgels_(char* trans, int* m, int* n, int* nrhs,
81
+ lapack_complex_double* a, int* lda, lapack_complex_double* b, int* ldb,
82
+ lapack_complex_double* work, int* lwork, int* info);
83
+
73
84
  // ── Eigenvalue decomposition ───────────────────────────────────────────────
74
85
  // Compute eigenvalues and optionally left/right eigenvectors of a general
75
86
  // real matrix A. A = VR * diag(WR+i*WI) * VR^(-1)
@@ -87,4 +98,5 @@ Napi::Value Qr(const Napi::CallbackInfo& info);
87
98
  Napi::Value Svd(const Napi::CallbackInfo& info);
88
99
  Napi::Value Matmul(const Napi::CallbackInfo& info);
89
100
  Napi::Value Linsolve(const Napi::CallbackInfo& info);
101
+ Napi::Value LinsolveComplex(const Napi::CallbackInfo& info);
90
102
  Napi::Value Eig(const Napi::CallbackInfo& info);
@@ -1,17 +1,21 @@
1
1
  /**
2
- * linsolve() — solve A * X = B via LAPACK.
2
+ * linsolve() — solve A * X = B via LAPACK (real).
3
+ * linsolveComplex() — solve A * X = B via LAPACK (complex).
3
4
  *
4
5
  * linsolve(A: Float64Array, m: number, n: number,
5
6
  * B: Float64Array, nrhs: number): Float64Array
6
7
  *
8
+ * linsolveComplex(ARe: Float64Array, AIm: Float64Array, m: number, n: number,
9
+ * BRe: Float64Array, BIm: Float64Array, nrhs: number): {re, im}
10
+ *
7
11
  * A is m×n in column-major order; B is m×nrhs.
8
12
  * Returns X (n×nrhs) in a new Float64Array in column-major order.
9
13
  *
10
14
  * Square (m == n):
11
- * Uses dgesv (LU with partial pivoting). Throws if A is singular.
15
+ * Uses dgesv / zgesv (LU with partial pivoting). Throws if A is singular.
12
16
  *
13
17
  * Non-square:
14
- * Uses dgels (QR for overdetermined, LQ for underdetermined).
18
+ * Uses dgels / zgels (QR for overdetermined, LQ for underdetermined).
15
19
  * Overdetermined (m > n): least-squares solution minimising ||A*X - B||₂.
16
20
  * Underdetermined (m < n): minimum-norm solution minimising ||X||₂.
17
21
  */
@@ -137,3 +141,150 @@ Napi::Value Linsolve(const Napi::CallbackInfo& info) {
137
141
  return result;
138
142
  }
139
143
  }
144
+
145
+ // ── linsolveComplex() ─────────────────────────────────────────────────────────
146
+
147
+ Napi::Value LinsolveComplex(const Napi::CallbackInfo& info) {
148
+ Napi::Env env = info.Env();
149
+
150
+ if (info.Length() < 7
151
+ || !info[0].IsTypedArray()
152
+ || !info[1].IsTypedArray()
153
+ || !info[2].IsNumber()
154
+ || !info[3].IsNumber()
155
+ || !info[4].IsTypedArray()
156
+ || !info[5].IsTypedArray()
157
+ || !info[6].IsNumber()) {
158
+ Napi::TypeError::New(env,
159
+ "linsolveComplex: expected (Float64Array ARe, Float64Array AIm,"
160
+ " number m, number n, Float64Array BRe, Float64Array BIm, number nrhs)")
161
+ .ThrowAsJavaScriptException();
162
+ return env.Null();
163
+ }
164
+
165
+ auto arrARe = info[0].As<Napi::TypedArray>();
166
+ auto arrAIm = info[1].As<Napi::TypedArray>();
167
+ auto arrBRe = info[4].As<Napi::TypedArray>();
168
+ auto arrBIm = info[5].As<Napi::TypedArray>();
169
+
170
+ if (arrARe.TypedArrayType() != napi_float64_array ||
171
+ arrAIm.TypedArrayType() != napi_float64_array ||
172
+ arrBRe.TypedArrayType() != napi_float64_array ||
173
+ arrBIm.TypedArrayType() != napi_float64_array) {
174
+ Napi::TypeError::New(env,
175
+ "linsolveComplex: ARe, AIm, BRe, BIm must be Float64Arrays")
176
+ .ThrowAsJavaScriptException();
177
+ return env.Null();
178
+ }
179
+
180
+ int m = info[2].As<Napi::Number>().Int32Value();
181
+ int n = info[3].As<Napi::Number>().Int32Value();
182
+ int nrhs = info[6].As<Napi::Number>().Int32Value();
183
+
184
+ if (m <= 0 || n <= 0 || nrhs <= 0
185
+ || static_cast<int>(arrARe.ElementLength()) != m * n
186
+ || static_cast<int>(arrAIm.ElementLength()) != m * n
187
+ || static_cast<int>(arrBRe.ElementLength()) != m * nrhs
188
+ || static_cast<int>(arrBIm.ElementLength()) != m * nrhs) {
189
+ Napi::RangeError::New(env,
190
+ "linsolveComplex: array lengths must match m*n (A) and m*nrhs (B)")
191
+ .ThrowAsJavaScriptException();
192
+ return env.Null();
193
+ }
194
+
195
+ auto fARe = info[0].As<Napi::Float64Array>();
196
+ auto fAIm = info[1].As<Napi::Float64Array>();
197
+ auto fBRe = info[4].As<Napi::Float64Array>();
198
+ auto fBIm = info[5].As<Napi::Float64Array>();
199
+
200
+ int info_val = 0;
201
+
202
+ if (m == n) {
203
+ // ── Square: zgesv (LU + solve) ────────────────────────────────────────────
204
+ std::vector<lapack_complex_double> a(n * n), b(n * nrhs);
205
+ for (int i = 0; i < n * n; ++i) { a[i].real = fARe[i]; a[i].imag = fAIm[i]; }
206
+ for (int i = 0; i < n * nrhs; ++i) { b[i].real = fBRe[i]; b[i].imag = fBIm[i]; }
207
+
208
+ std::vector<int> ipiv(n);
209
+ zgesv_(&n, &nrhs, a.data(), &n, ipiv.data(), b.data(), &n, &info_val);
210
+
211
+ if (info_val > 0) {
212
+ Napi::Error::New(env, "linsolveComplex: matrix is singular (zgesv)")
213
+ .ThrowAsJavaScriptException();
214
+ return env.Null();
215
+ }
216
+ if (info_val < 0) {
217
+ Napi::Error::New(env, "linsolveComplex: illegal argument passed to zgesv")
218
+ .ThrowAsJavaScriptException();
219
+ return env.Null();
220
+ }
221
+
222
+ auto resultRe = Napi::Float64Array::New(env, static_cast<size_t>(n * nrhs));
223
+ auto resultIm = Napi::Float64Array::New(env, static_cast<size_t>(n * nrhs));
224
+ for (int i = 0; i < n * nrhs; ++i) { resultRe[i] = b[i].real; resultIm[i] = b[i].imag; }
225
+
226
+ auto result = Napi::Object::New(env);
227
+ result.Set("re", resultRe);
228
+ result.Set("im", resultIm);
229
+ return result;
230
+
231
+ } else {
232
+ // ── Non-square: zgels (QR / LQ least-squares / min-norm solve) ───────────
233
+ // zgels needs B with ldb = max(m, n) rows so the solution fits in place.
234
+ int ldb = m > n ? m : n;
235
+ std::vector<lapack_complex_double> a(m * n), b(ldb * nrhs);
236
+ for (int i = 0; i < m * n; ++i) { a[i].real = fARe[i]; a[i].imag = fAIm[i]; }
237
+ for (int i = 0; i < ldb * nrhs; ++i) { b[i].real = 0.0; b[i].imag = 0.0; }
238
+
239
+ // Copy each column of B into the top m rows of the extended buffer
240
+ for (int c = 0; c < nrhs; ++c) {
241
+ for (int r = 0; r < m; ++r) {
242
+ b[r + c * ldb].real = fBRe[r + c * m];
243
+ b[r + c * ldb].imag = fBIm[r + c * m];
244
+ }
245
+ }
246
+
247
+ char trans = 'N';
248
+
249
+ // Workspace query
250
+ int lwork = -1;
251
+ lapack_complex_double work_query;
252
+ zgels_(&trans, &m, &n, &nrhs,
253
+ a.data(), &m, b.data(), &ldb,
254
+ &work_query, &lwork, &info_val);
255
+ lwork = static_cast<int>(work_query.real);
256
+ if (lwork < 1) lwork = std::max(1, std::max(m, n));
257
+
258
+ std::vector<lapack_complex_double> work(lwork);
259
+ zgels_(&trans, &m, &n, &nrhs,
260
+ a.data(), &m, b.data(), &ldb,
261
+ work.data(), &lwork, &info_val);
262
+
263
+ if (info_val < 0) {
264
+ Napi::Error::New(env, "linsolveComplex: illegal argument passed to zgels")
265
+ .ThrowAsJavaScriptException();
266
+ return env.Null();
267
+ }
268
+ if (info_val > 0) {
269
+ Napi::Error::New(env,
270
+ "linsolveComplex: A does not have full rank (zgels)")
271
+ .ThrowAsJavaScriptException();
272
+ return env.Null();
273
+ }
274
+
275
+ // Solution is in the first n rows of b (column-major, ldb rows per column)
276
+ auto resultRe = Napi::Float64Array::New(env, static_cast<size_t>(n * nrhs));
277
+ auto resultIm = Napi::Float64Array::New(env, static_cast<size_t>(n * nrhs));
278
+ for (int c = 0; c < nrhs; ++c) {
279
+ for (int r = 0; r < n; ++r) {
280
+ resultRe[r + c * n] = b[r + c * ldb].real;
281
+ resultIm[r + c * n] = b[r + c * ldb].imag;
282
+ }
283
+ }
284
+
285
+ auto result = Napi::Object::New(env);
286
+ result.Set("re", resultRe);
287
+ result.Set("im", resultIm);
288
+ return result;
289
+ }
290
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "numbl",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Run .m source files in the browser and on the command line by compiling to JavaScript",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",