numpy-ts 0.13.0 → 1.0.0
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/README.md +29 -251
- package/dist/esm/common/ndarray-core.js +1 -1
- package/dist/esm/common/ops/formatting.js +4 -2
- package/dist/esm/common/ops/linalg.js +2 -2
- package/dist/esm/core/formatting.js +1 -1
- package/dist/esm/full/ndarray.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/numpy-ts.browser.js +7 -5
- package/dist/numpy-ts.node-io.cjs +7 -5
- package/dist/numpy-ts.node-io.cjs.map +4 -4
- package/dist/numpy-ts.node-io.mjs +7 -5
- package/dist/numpy-ts.node-io.mjs.map +4 -4
- package/dist/numpy-ts.node.cjs +7 -5
- package/dist/numpy-ts.node.cjs.map +4 -4
- package/dist/types/common/ops/formatting.d.ts +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -2,37 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
[](https://www.npmjs.com/package/numpy-ts)
|
|
5
|
-

|
|
6
6
|

|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
<picture>
|
|
9
|
+
<source
|
|
10
|
+
media="(prefers-color-scheme: dark)"
|
|
11
|
+
srcset="https://raw.githubusercontent.com/dupontcyborg/numpy-ts/main/docs/images/hero-dark.svg"
|
|
12
|
+
/>
|
|
13
|
+
<img
|
|
14
|
+
src="https://raw.githubusercontent.com/dupontcyborg/numpy-ts/main/docs/images/hero-light.svg"
|
|
15
|
+
alt="numpy-ts"
|
|
16
|
+
/>
|
|
17
|
+
</picture>
|
|
18
|
+
|
|
19
|
+
Complete NumPy implementation for TypeScript and JavaScript.
|
|
20
|
+
|
|
21
|
+
- **📊 Extensive API** — **476 of 507 NumPy functions (93.9% coverage)**
|
|
22
|
+
- **✅ NumPy-validated** — 6,000+ tests compared against Python NumPy
|
|
23
|
+
- **🔒 Type-safe** — Full TypeScript type definitions
|
|
24
|
+
- **🌳 Tree-shakeable** — Import only what you use
|
|
25
|
+
- **🌐 Universal** — Works in Node.js and browsers
|
|
16
26
|
|
|
17
|
-
|
|
27
|
+
[Docs](https://numpyts.dev) • [Examples](https://numpyts.dev/examples) • [Benchmarks](https://numpyts.dev/performance)
|
|
18
28
|
|
|
19
|
-
|
|
29
|
+
## Install
|
|
20
30
|
|
|
21
31
|
```bash
|
|
22
32
|
npm install numpy-ts
|
|
23
33
|
```
|
|
24
34
|
|
|
25
|
-
## Why numpy-ts?
|
|
26
|
-
|
|
27
|
-
- **📊 Extensive API** — **476 of 507 NumPy functions (93.9% coverage)**
|
|
28
|
-
- **✅ NumPy-validated** — 6,000+ test cases cross-validated against Python NumPy
|
|
29
|
-
- **🔒 Type-safe** — Full TypeScript support with shape and dtype inference
|
|
30
|
-
- **🌐 Universal** — Works in Node.js and browsers with .npy/.npz file support
|
|
31
|
-
- **🎯 Zero dependencies** — Pure TypeScript, no heavy external libraries
|
|
32
|
-
- **🌳 Tree-shakeable** — Import only what you need (~11KB for `zeros`, not 180KB)
|
|
33
|
-
|
|
34
|
-
> **💡 Bundle size tip:** Standalone functions like `sin(arr)` tree-shake well. Method chaining like `arr.sin()` is convenient but pulls in the full library. See [modular imports guide](docs/modular-imports.mdx).
|
|
35
|
-
|
|
36
35
|
## Quick Start
|
|
37
36
|
|
|
38
37
|
```typescript
|
|
@@ -57,239 +56,18 @@ const row = A.slice('0', ':'); // A[0, :]
|
|
|
57
56
|
const submatrix = A.slice('0:2', '1:'); // A[0:2, 1:]
|
|
58
57
|
```
|
|
59
58
|
|
|
60
|
-
##
|
|
61
|
-
|
|
62
|
-
### API Coverage
|
|
63
|
-
|
|
64
|
-
Progress toward complete NumPy API compatibility:
|
|
65
|
-
|
|
66
|
-
| Category | Complete | Total | Status |
|
|
67
|
-
|----------|----------|-------|--------|
|
|
68
|
-
| **Arithmetic** | 29/29 | 100% | ✅ |
|
|
69
|
-
| **Array Creation** | 35/35 | 100% | ✅ |
|
|
70
|
-
| **Array Manipulation** | 46/46 | 100% | ✅ |
|
|
71
|
-
| **Bit Operations** | 13/13 | 100% | ✅ |
|
|
72
|
-
| **Broadcasting** | 3/3 | 100% | ✅ |
|
|
73
|
-
| **Comparison** | 10/10 | 100% | ✅ |
|
|
74
|
-
| **Exponential** | 9/9 | 100% | ✅ |
|
|
75
|
-
| **FFT** | 18/18 | 100% | ✅ |
|
|
76
|
-
| **Gradient** | 4/4 | 100% | ✅ |
|
|
77
|
-
| **Hyperbolic** | 9/9 | 100% | ✅ |
|
|
78
|
-
| **I/O** | 8/8 | 100% | ✅ |
|
|
79
|
-
| **Indexing** | 21/21 | 100% | ✅ |
|
|
80
|
-
| **Linear Algebra** | 16/16 | 100% | ✅ |
|
|
81
|
-
| **Linear Algebra (linalg)** | 31/31 | 100% | ✅ |
|
|
82
|
-
| **Logic** | 24/24 | 100% | ✅ |
|
|
83
|
-
| **NDArray Methods** | 47/47 | 100% | ✅ |
|
|
84
|
-
| **Other Math** | 15/15 | 100% | ✅ |
|
|
85
|
-
| **Polynomials** | 10/10 | 100% | ✅ |
|
|
86
|
-
| **Printing/Formatting** | 10/10 | 100% | ✅ |
|
|
87
|
-
| **Random** | 53/53 | 100% | ✅ |
|
|
88
|
-
| **Reductions** | 36/36 | 100% | ✅ |
|
|
89
|
-
| **Rounding** | 7/7 | 100% | ✅ |
|
|
90
|
-
| **Searching** | 7/7 | 100% | ✅ |
|
|
91
|
-
| **Set Operations** | 12/12 | 100% | ✅ |
|
|
92
|
-
| **Sorting** | 6/6 | 100% | ✅ |
|
|
93
|
-
| **Statistics** | 11/11 | 100% | ✅ |
|
|
94
|
-
| **Trigonometric** | 16/16 | 100% | ✅ |
|
|
95
|
-
| **Type Checking** | 7/7 | 100% | ✅ |
|
|
96
|
-
| **Utilities** | 10/10 | 100% | ✅ |
|
|
97
|
-
| **Unplanned** | 0/31 | 0% | 🔴 |
|
|
98
|
-
|
|
99
|
-
**Overall: 476/507 functions (93.9% coverage)**
|
|
100
|
-
|
|
101
|
-
See the complete [API Reference](docs/API-REFERENCE.md) for detailed function list.
|
|
102
|
-
|
|
103
|
-
### Data Types (dtypes)
|
|
104
|
-
|
|
105
|
-
NumPy-compatible type system with automatic promotion:
|
|
106
|
-
|
|
107
|
-
| DType | NumPy | numpy-ts | Notes |
|
|
108
|
-
|-------|-------|----------|-------|
|
|
109
|
-
| **Floating Point** ||||
|
|
110
|
-
| `float64` | ✅ | ✅ | Default dtype |
|
|
111
|
-
| `float32` | ✅ | ✅ | |
|
|
112
|
-
| `float16` | ✅ | ⚠️ | Planned (waiting for [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array)) |
|
|
113
|
-
| **Signed Integers** ||||
|
|
114
|
-
| `int64` | ✅ | ✅ | Uses BigInt |
|
|
115
|
-
| `int32` | ✅ | ✅ | |
|
|
116
|
-
| `int16` | ✅ | ✅ | |
|
|
117
|
-
| `int8` | ✅ | ✅ | |
|
|
118
|
-
| **Unsigned Integers** ||||
|
|
119
|
-
| `uint64` | ✅ | ✅ | Uses BigInt |
|
|
120
|
-
| `uint32` | ✅ | ✅ | |
|
|
121
|
-
| `uint16` | ✅ | ✅ | |
|
|
122
|
-
| `uint8` | ✅ | ✅ | |
|
|
123
|
-
| **Other Numeric** ||||
|
|
124
|
-
| `bool` | ✅ | ✅ | Stored as uint8 |
|
|
125
|
-
| `complex64` | ✅ | ✅ | |
|
|
126
|
-
| `complex128` | ✅ | ✅ | |
|
|
127
|
-
| **Non-Numeric** ||||
|
|
128
|
-
| `str_` | ✅ | ❌ | Not planned |
|
|
129
|
-
| `bytes_` | ✅ | ❌ | Not planned |
|
|
130
|
-
| `object_` | ✅ | ❌ | Not planned |
|
|
131
|
-
| `datetime64` | ✅ | ❌ | Not planned |
|
|
132
|
-
| `timedelta64` | ✅ | ❌ | Not planned |
|
|
133
|
-
|
|
134
|
-
**Supported: 13/20 numeric dtypes**
|
|
135
|
-
|
|
136
|
-
### Intentional Divergences from NumPy
|
|
137
|
-
|
|
138
|
-
numpy-ts focuses on numeric array computing. The following NumPy features are **not planned**:
|
|
139
|
-
|
|
140
|
-
| Feature | Why Not Included |
|
|
141
|
-
|---------|------------------|
|
|
142
|
-
| **Datetime/Timedelta** (`datetime64`, `timedelta64`) | JS has native `Date`; libraries like [date-fns](https://date-fns.org/) handle time math better |
|
|
143
|
-
| **F-order memory layout** | Exists in NumPy for Fortran/BLAS interop, which doesn't exist in JS |
|
|
144
|
-
| **Object dtype** (`object_`) | Defeats the purpose of typed arrays; use regular JS arrays instead |
|
|
145
|
-
| **String/Bytes dtypes** (`str_`, `bytes_`, `U`, `S`) | JS strings are first-class; no need for fixed-width string arrays |
|
|
146
|
-
| **Exact random sequence reproduction** | numpy-ts random functions produce statistically correct distributions but use different transformation algorithms (Box-Muller vs Ziggurat), so seeded sequences won't match NumPy exactly |
|
|
147
|
-
|
|
148
|
-
These omissions keep the library focused and the bundle small. For string manipulation, datetime math, or heterogeneous data, use native JS/TS constructs alongside numpy-ts.
|
|
149
|
-
|
|
150
|
-
### NumPy Memory Model
|
|
151
|
-
|
|
152
|
-
- **View tracking** — `base` attribute and `OWNDATA` flag
|
|
153
|
-
- **Strided arrays** — C/F contiguous flags for memory layout
|
|
154
|
-
- **Zero-copy ops** — Views for slicing, transpose, reshape (when possible)
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
const arr = np.ones([4, 4]);
|
|
158
|
-
const view = arr.slice('0:2', '0:2');
|
|
159
|
-
|
|
160
|
-
console.log(view.base === arr); // true - view tracks base
|
|
161
|
-
console.log(view.flags.OWNDATA); // false - doesn't own data
|
|
162
|
-
console.log(arr.flags.C_CONTIGUOUS); // true - row-major layout
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
## Architecture
|
|
166
|
-
|
|
167
|
-
```
|
|
168
|
-
┌─────────────────────────────────┐
|
|
169
|
-
│ NumPy-Compatible API │
|
|
170
|
-
│ Broadcasting, DType Promotion │
|
|
171
|
-
└───────────────┬─────────────────┘
|
|
172
|
-
│
|
|
173
|
-
┌───────────────┴─────────────────┐
|
|
174
|
-
│ NDArray (Views & Memory Mgmt) │
|
|
175
|
-
│ Strided Arrays, Base Tracking │
|
|
176
|
-
└───────────────┬─────────────────┘
|
|
177
|
-
│- - - - - - - - - - - - - - - - - - ┐
|
|
178
|
-
┌───────────────┴─────────────────┐ ┌ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─┐
|
|
179
|
-
│ TypeScript / JavaScript Core │ │ WASM Compute Engine (Future) │
|
|
180
|
-
│ Computational Engine │ │ Optimized BLAS / arithmetic │
|
|
181
|
-
└─────────────────────────────────┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
Pure TypeScript implementation built from scratch for correctness and NumPy compatibility.
|
|
185
|
-
|
|
186
|
-
## Performance
|
|
187
|
-
|
|
188
|
-

|
|
189
|
-
|
|
190
|
-
See [benchmarks/README.md](benchmarks/README.md) for detailed performance analysis.
|
|
191
|
-
|
|
192
|
-
## File I/O
|
|
193
|
-
|
|
194
|
-
Read and write `.npy` and `.npz` files with Node.js or browsers.
|
|
195
|
-
|
|
196
|
-
### Node.js
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
import { load, save, savez, savez_compressed } from 'numpy-ts/node';
|
|
200
|
-
|
|
201
|
-
save('array.npy', arr);
|
|
202
|
-
const arr = load('array.npy');
|
|
203
|
-
|
|
204
|
-
savez('arrays.npz', { a: arr1, b: arr2 });
|
|
205
|
-
const { a, b } = load('arrays.npz');
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
### Browser
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
import * as np from 'numpy-ts';
|
|
212
|
-
|
|
213
|
-
// Parse fetched .npy file
|
|
214
|
-
const response = await fetch('array.npy');
|
|
215
|
-
const arr = np.parseNpy(await response.arrayBuffer());
|
|
216
|
-
|
|
217
|
-
// Serialize for download
|
|
218
|
-
const bytes = np.serializeNpy(arr);
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
*Why separate imports?* The `/node` entry includes Node.js `fs` usage. Keeping it separate ensures browser bundles stay clean.
|
|
222
|
-
|
|
223
|
-
## Examples
|
|
224
|
-
|
|
225
|
-
### Broadcasting
|
|
226
|
-
|
|
227
|
-
```typescript
|
|
228
|
-
const matrix = np.ones([3, 4]); // (3, 4)
|
|
229
|
-
const row = np.arange(4); // (4,)
|
|
230
|
-
const result = matrix.add(row); // (3, 4) - row broadcast to each row
|
|
231
|
-
|
|
232
|
-
const col = np.array([[1], [2], [3]]); // (3, 1)
|
|
233
|
-
const grid = col.multiply(row); // (3, 4) - outer product via broadcasting
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
### Slicing
|
|
237
|
-
|
|
238
|
-
TypeScript doesn't support Python's `arr[0:5, :]`, so we use strings:
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
arr.slice('0:5', '1:3'); // arr[0:5, 1:3]
|
|
242
|
-
arr.slice(':', '-1'); // arr[:, -1]
|
|
243
|
-
arr.slice('::2'); // arr[::2]
|
|
244
|
-
|
|
245
|
-
// Convenience helpers
|
|
246
|
-
arr.row(0); // arr[0, :]
|
|
247
|
-
arr.col(2); // arr[:, 2]
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
### Type Safety
|
|
59
|
+
## Resources
|
|
251
60
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
## Comparison with Alternatives
|
|
260
|
-
|
|
261
|
-
| Feature | numpy-ts | numjs | ndarray | TensorFlow.js |
|
|
262
|
-
|---------|----------|-------|---------|---------------|
|
|
263
|
-
| NumPy API Coverage | 476/507 (94%) | ~20% | Different | ML-focused |
|
|
264
|
-
| TypeScript Native | ✅ Full | Partial | ❌ No | ✅ Yes |
|
|
265
|
-
| NumPy Validated | ✅ 6,000+ tests | Mostly | ❌ No | ❌ No |
|
|
266
|
-
| .npy/.npz Files | ✅ v1/v2/v3 | ❌ No | ❌ No | ❌ No |
|
|
267
|
-
| Broadcasting | ✅ Full | Limited | Limited | ✅ Full |
|
|
268
|
-
| Bundle Size | <100kb | ~60kb | ~5kb | >100kb |
|
|
61
|
+
- Docs: https://numpyts.dev
|
|
62
|
+
- Playground: https://numpyts.dev/playground
|
|
63
|
+
- Usage Examples: https://numpyts.dev/examples
|
|
64
|
+
- API Coverage Report: https://numpyts.dev/coverage
|
|
65
|
+
- Performance Benchmarks: https://numpyts.dev/performance
|
|
269
66
|
|
|
270
67
|
## Contributing
|
|
271
68
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
- Setting up the development environment
|
|
275
|
-
- Adding new functions with tests
|
|
276
|
-
- Running benchmarks
|
|
277
|
-
- Submitting pull requests
|
|
278
|
-
|
|
279
|
-
## Documentation
|
|
280
|
-
|
|
281
|
-
- **[API Reference](docs/API-REFERENCE.md)** — Complete function checklist (120+ functions)
|
|
282
|
-
- **[Feature Details](docs/FEATURES.md)** — Broadcasting, dtypes, views, slicing
|
|
283
|
-
- **[Contributing Guide](CONTRIBUTING.md)** — How to contribute
|
|
284
|
-
- **[Testing Guide](docs/TESTING-GUIDE.md)** — Testing strategy and examples
|
|
285
|
-
- **[Architecture](docs/ARCHITECTURE.md)** — System design and internals
|
|
286
|
-
|
|
69
|
+
Issues and PRs are welcome: https://github.com/dupontcyborg/numpy-ts
|
|
287
70
|
|
|
288
71
|
## License
|
|
289
72
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
---
|
|
293
|
-
|
|
294
|
-
**Bring NumPy to TypeScript!** ⭐
|
|
295
|
-
[GitHub](https://github.com/dupontcyborg/numpy-ts) • [Issues](https://github.com/dupontcyborg/numpy-ts/issues) • [NumPy Docs](https://numpy.org/)
|
|
73
|
+
MIT © Nicolas Dupont
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseSlice as
|
|
1
|
+
import{parseSlice as c,normalizeSlice as d}from"./slicing";import{getTypedArrayConstructor as m,getDTypeSize as b,isBigIntDType as y,isComplexDType as A}from"./dtype";import{Complex as f}from"./complex";import{ArrayStorage as p}from"./storage";import{array_str as _}from"./ops/formatting";class l{constructor(t,r){this._storage=t,this._base=r}get storage(){return this._storage}static fromStorage(t,r){return new l(t,r)}get shape(){return this._storage.shape}get ndim(){return this._storage.ndim}get size(){return this._storage.size}get dtype(){return this._storage.dtype}get data(){return this._storage.data}get strides(){return this._storage.strides}get flags(){return{C_CONTIGUOUS:this._storage.isCContiguous,F_CONTIGUOUS:this._storage.isFContiguous,OWNDATA:this._base===void 0}}get base(){return this._base??null}get itemsize(){return b(this._storage.dtype)}get nbytes(){return this.size*this.itemsize}fill(t){const r=this._storage.dtype,i=this.size;if(y(r)){const n=typeof t=="bigint"?t:BigInt(Math.round(Number(t)));for(let e=0;e<i;e++)this._storage.iset(e,n)}else if(r==="bool"){const n=t?1:0;for(let e=0;e<i;e++)this._storage.iset(e,n)}else{const n=Number(t);for(let e=0;e<i;e++)this._storage.iset(e,n)}}*[Symbol.iterator](){if(this.ndim===0)yield this._storage.iget(0);else if(this.ndim===1)for(let t=0;t<this.shape[0];t++)yield this._storage.iget(t);else for(let t=0;t<this.shape[0];t++)yield this.slice(String(t))}get(t){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);const r=t.map((i,n)=>{let e=i;if(e<0&&(e=this.shape[n]+e),e<0||e>=this.shape[n])throw new Error(`Index ${i} is out of bounds for axis ${n} with size ${this.shape[n]}`);return e});return this._storage.get(...r)}set(t,r){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);const i=t.map((e,a)=>{let o=e;if(o<0&&(o=this.shape[a]+o),o<0||o>=this.shape[a])throw new Error(`Index ${e} is out of bounds for axis ${a} with size ${this.shape[a]}`);return o}),n=this.dtype;if(A(n))this._storage.set(i,r);else if(y(n)){const e=r instanceof f?r.re:Number(r),a=typeof r=="bigint"?r:BigInt(Math.round(e));this._storage.set(i,a)}else if(n==="bool"){const a=(r instanceof f?r.re:Number(r))?1:0;this._storage.set(i,a)}else{const e=r instanceof f?r.re:Number(r);this._storage.set(i,e)}}iget(t){return this._storage.iget(t)}iset(t,r){this._storage.iset(t,r)}copy(){return new l(this._storage.copy())}astype(t,r=!0){const i=this.dtype;if(i===t&&!r)return this;if(i===t&&r)return this.copy();const n=Array.from(this.shape),e=this.size,a=m(t);if(!a)throw new Error(`Cannot convert to dtype ${t}`);const o=new a(e),g=this.data;if(y(i)&&!y(t)){const h=g;if(t==="bool")for(let s=0;s<e;s++)o[s]=h[s]!==BigInt(0)?1:0;else for(let s=0;s<e;s++)o[s]=Number(h[s])}else if(!y(i)&&y(t)){const h=g;for(let s=0;s<e;s++)o[s]=BigInt(Math.round(Number(h[s])))}else if(t==="bool"){const h=g;for(let s=0;s<e;s++)o[s]=h[s]!==0?1:0}else if(i==="bool"&&!y(t)){const h=g;for(let s=0;s<e;s++)o[s]=h[s]}else if(!y(i)&&!y(t)){const h=g;for(let s=0;s<e;s++)o[s]=h[s]}else{const h=g;for(let s=0;s<e;s++)o[s]=h[s]}const u=p.fromData(o,n,t);return new l(u)}slice(...t){if(t.length===0)return this;if(t.length>this.ndim)throw new Error(`Too many indices for array: array is ${this.ndim}-dimensional, but ${t.length} were indexed`);const r=t.map((g,u)=>{const h=c(g);return d(h,this.shape[u])});for(;r.length<this.ndim;)r.push({start:0,stop:this.shape[r.length],step:1,isIndex:!1});const i=[],n=[];let e=this._storage.offset;for(let g=0;g<r.length;g++){const u=r[g];if(e+=u.start*this._storage.strides[g],u.step===0)continue;const h=Math.max(0,Math.ceil((u.stop-u.start)/u.step));i.push(h),n.push(this._storage.strides[g]*u.step)}const a=p.fromData(this._storage.data,i,this._storage.dtype,n,e),o=this._base??this;return new l(a,o)}toString(){return _(this._storage)}toArray(){if(this.ndim===0)return this._storage.iget(0);const t=this.shape,r=t.length,i=(n,e)=>{if(e===r)return this._storage.get(...n);const a=[];for(let o=0;o<t[e];o++)n[e]=o,a.push(i(n,e+1));return a};return i(new Array(r),0)}tolist(){return this.toArray()}tobytes(){if(this._storage.isCContiguous){const i=this._storage.data,n=i.BYTES_PER_ELEMENT,e=this._storage.offset*n,a=this.size*n;return i.buffer.slice(e,e+a)}const r=this.copy()._storage.data;return r.buffer.slice(0,this.size*r.BYTES_PER_ELEMENT)}item(...t){if(t.length===0){if(this.size!==1)throw new Error("can only convert an array of size 1 to a Python scalar");return this._storage.iget(0)}if(t.length===1){const r=t[0];if(r<0||r>=this.size)throw new Error(`index ${r} is out of bounds for size ${this.size}`);return this._storage.iget(r)}return this.get(t)}}export{l as NDArrayCore};
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import{Complex as
|
|
2
|
-
`+
|
|
1
|
+
import{Complex as z}from"../complex";const D={threshold:1e3,edgeitems:3,precision:8,linewidth:75,floatmode:"maxprec",suppress:!0,separator:" ",prefix:"",suffix:"",nanstr:"nan",infstr:"inf",sign:"-",legacy:!1};let x={...D};function q(n){x={...x,...n}}function E(){return{...x}}function R(n){let t=null;const e={_savedOptions:null,enter(){t={...x},e._savedOptions=t,x={...x,...n}},exit(){t&&(x=t,t=null,e._savedOptions=null)},apply(r){e.enter();try{return r()}finally{e.exit()}}};return e}function C(n,t=null,e=!0,r=!0,a="k",o="-",g=null,f=null,h=null){const u=t??x.precision;if(!Number.isFinite(n))return Number.isNaN(n)?x.nanstr:(n>0?"":"-")+x.infstr;let s;if(e&&t===null?(s=n.toString(),r&&!s.includes(".")&&!s.includes("e")&&(s+=".0")):s=n.toFixed(u),h!==null){const p=s.indexOf(".");if(p!==-1){const i=s.length-p-1;i<h&&(s+="0".repeat(h-i))}else r&&(s+="."+"0".repeat(h))}if(a!=="k"&&s.includes(".")){if(a==="."||a==="0"||a==="-"){let p=s.length;for(;p>0&&s[p-1]==="0";)p--;s=s.slice(0,p)}a==="0"&&s.endsWith(".")&&(s+="0"),a==="-"&&s.endsWith(".")&&(s=s.slice(0,-1))}if(n>=0&&!Object.is(n,-0)&&(o==="+"?s="+"+s:o===" "&&(s=" "+s)),g!==null&&s.length<g&&(s=" ".repeat(g-s.length)+s),f!==null){const p=s.indexOf(".");if(p!==-1){const i=s.length-p-1;i<f&&(s+="0".repeat(f-i))}}return s}function w(n,t=null,e=!0,r="k",a="-",o=null,g=2,f=null){const h=t??x.precision;if(!Number.isFinite(n))return Number.isNaN(n)?x.nanstr:(n>0?"":"-")+x.infstr;let u=n.toExponential(h);if(f!==null){const l=u.indexOf("e"),d=u.slice(0,l),S=u.slice(l),N=d.indexOf(".");if(N!==-1){const c=d.length-N-1;c<f&&(u=d+"0".repeat(f-c)+S)}}if(r!=="k"){const l=u.indexOf("e");let d=u.slice(0,l);const S=u.slice(l);d.includes(".")&&((r==="."||r==="0"||r==="-")&&(d=d.replace(/0+$/,"")),r==="0"&&d.endsWith(".")&&(d+="0"),r==="-"&&(d=d.replace(/\.$/,""))),u=d+S}const s=u.indexOf("e"),p=u.slice(0,s);let i=u.slice(s+1);const m=i[0]==="-"?"-":"+";let b=i.replace(/^[+-]/,"");for(;b.length<g;)b="0"+b;return u=p+"e"+m+b,n>=0&&!Object.is(n,-0)&&(a==="+"?u="+"+u:a===" "&&(u=" "+u)),o!==null&&u.length<o&&(u=" ".repeat(o-u.length)+u),u}function U(n,t=2,e=0){if(t<2||t>36)throw new Error("base must be between 2 and 36");n=Math.trunc(n);let r;if(n<0?r="-"+Math.abs(n).toString(t).toUpperCase():r=n.toString(t).toUpperCase(),e>0){const a="0".repeat(e);r.startsWith("-")?r="-"+a+r.slice(1):r=a+r}return r}function B(n,t=null){if(n=Math.trunc(n),t!==null&&n<0){if(n=Math.pow(2,t)+n,n<0)throw new Error("width too small for negative number");let a=n.toString(2);return a.length<t&&(a="0".repeat(t-a.length)+a),a}let e;return n<0?e="-"+Math.abs(n).toString(2):e=n.toString(2),t!==null&&e.length<t&&(e="0".repeat(t-e.length)+e),e}function P(n,t,e){if(n instanceof z){const r=P(n.re,"float64",e),a=P(Math.abs(n.im),"float64",e),o=n.im>=0?"+":"-";return`${r}${o}${a}j`}return typeof n=="boolean"?n?" True":"False":typeof n=="bigint"?n.toString():Number.isFinite(n)?t==="float32"||t==="float64"?e.suppress&&Math.abs(n)<1e-10&&n!==0?"0.":C(n,e.precision,!1,!0,"k",e.sign):n.toString():Number.isNaN(n)?e.nanstr:(n>0?"":"-")+e.infstr}function $(n,t){const e=[],r=n.shape,a=r.length,o=n.strides;function g(f,h){if(h===a){let i=0;for(let m=0;m<a;m++)i+=f[m]*o[m];e.push(n.iget(i));return}const u=r[h];if(r.reduce((i,m)=>i*m,1)>t.threshold&&u>2*t.edgeitems){for(let i=0;i<t.edgeitems;i++)f[h]=i,g(f,h+1);for(let i=u-t.edgeitems;i<u;i++)f[h]=i,g(f,h+1)}else for(let i=0;i<u;i++)f[h]=i,g(f,h+1)}return a>0?g(new Array(a).fill(0),0):e.push(n.iget(0)),e}function T(n,t){return n<0||Object.is(n,-0)?"-"+Math.abs(n).toString()+".":t==="+"?"+"+n.toString()+".":t===" "?" "+n.toString()+".":n.toString()+"."}function M(n,t,e){const r=n.indexOf(".");if(r===-1)return n.padStart(t+(e>0?1+e:0));const a=n.slice(0,r),o=n.slice(r+1),g=a.padStart(t),f=e>0?o.padEnd(e):o;return g+"."+f}function W(n){let t=0,e=0;for(const r of n){const a=r.indexOf(".");a===-1?t=Math.max(t,r.length):(t=Math.max(t,a),e=Math.max(e,r.length-a-1))}return{maxLeft:t,maxRight:e}}function A(n,t){const e=n.filter(i=>Number.isFinite(i));if(e.length===0){const i=n.map(b=>Number.isNaN(b)?t.nanstr:(b>0?"":"-")+t.infstr),m=i.length>0?Math.max(...i.map(b=>b.length)):1;return b=>Number.isNaN(b)?t.nanstr.padStart(m):Number.isFinite(b)?b.toString().padStart(m):((b>0?"":"-")+t.infstr).padStart(m)}const r=t.suppress?e.map(i=>Math.abs(i)<1e-10&&i!==0?0:i):e,a=r.map(Math.abs).filter(i=>i>0),o=a.length>0?Math.max(...a):0,g=a.length>0?Math.min(...a):0;if(o>=1e16||g>0&&g<1e-4||g>0&&o/g>1e3){const i=r.map(c=>w(c,t.precision,!1,".",t.sign));let m=0;for(const c of i){const y=c.indexOf("e"),O=y!==-1?c.slice(0,y):c,F=O.indexOf(".");F!==-1&&(m=Math.max(m,O.length-F-1))}const b=Math.max(m,0),l=Math.max(b,1),d=b===0?".":"k",S=r.map(c=>w(c,l,!1,d,t.sign));for(const c of n)Number.isFinite(c)||S.push(Number.isNaN(c)?t.nanstr:(c>0?"":"-")+t.infstr);const N=Math.max(...S.map(c=>c.length));return c=>(t.suppress&&Math.abs(c)<1e-10&&c!==0&&Number.isFinite(c)&&(c=0),Number.isNaN(c)?t.nanstr.padStart(N):Number.isFinite(c)?w(c,l,!1,d,t.sign).padStart(N):((c>0?"":"-")+t.infstr).padStart(N))}if(r.every(i=>Number.isInteger(i))){const i=r.map(l=>T(l,t.sign));for(const l of n)Number.isFinite(l)||i.push(Number.isNaN(l)?t.nanstr:(l>0?"":"-")+t.infstr);const{maxLeft:m,maxRight:b}=W(i);return l=>(t.suppress&&Math.abs(l)<1e-10&&l!==0&&Number.isFinite(l)&&(l=0),Number.isNaN(l)?M(t.nanstr,m,b):Number.isFinite(l)?M(T(l,t.sign),m,b):M((l>0?"":"-")+t.infstr,m,b))}const u=r.map(i=>C(i,t.precision,!1,!0,".",t.sign));for(const i of n)Number.isFinite(i)||u.push(Number.isNaN(i)?t.nanstr:(i>0?"":"-")+t.infstr);const{maxLeft:s,maxRight:p}=W(u);return i=>(t.suppress&&Math.abs(i)<1e-10&&i!==0&&Number.isFinite(i)&&(i=0),Number.isNaN(i)?M(t.nanstr,s,p):Number.isFinite(i)?M(C(i,t.precision,!1,!0,".",t.sign),s,p):M((i>0?"":"-")+t.infstr,s,p))}function j(n,t){const e=n.dtype,r=$(n,t);if(r.length===0)return o=>P(o,e,t);if(e==="bool")return o=>o?" True":"False";if(e==="int64"||e==="uint64"){const o=r.map(f=>f.toString()),g=Math.max(...o.map(f=>f.length));return f=>f.toString().padStart(g)}if(e.startsWith("int")||e.startsWith("uint")){const o=r.map(f=>f.toString()),g=Math.max(...o.map(f=>f.length));return f=>f.toString().padStart(g)}if(e==="complex64"||e==="complex128"){const o=r.map(u=>u.re),g=r.map(u=>Math.abs(u.im)),f=A(o,t),h=A(g,t);return u=>{const s=u,p=f(s.re),i=h(Math.abs(s.im)),m=s.im>=0?"+":"-";return`${p}${m}${i}j`}}const a=A(r,t);return o=>a(o)}function I(n,t,e,r,a,o){const g=n.shape,f=g.length;if(e===f){let l=0;const d=n.strides;for(let N=0;N<f;N++)l+=t[N]*d[N];const S=n.iget(l);return a(S)}const h=g[e],u=r.threshold,s=r.edgeitems,i=g.reduce((l,d)=>l*d,1)>u&&h>2*s,m=[],b=[...t];if(i){for(let l=0;l<s;l++)b[e]=l,m.push(I(n,b,e+1,r,a,o+1));m.push("...");for(let l=h-s;l<h;l++)b[e]=l,m.push(I(n,b,e+1,r,a,o+1))}else for(let l=0;l<h;l++)b[e]=l,m.push(I(n,b,e+1,r,a,o+1));if(e===f-1){const l="["+m.join(r.separator)+"]";if(o+l.length<r.linewidth)return l;const d=" ".repeat(o+1),S=r.linewidth-o-1,N=[];let c="";for(let y=0;y<m.length;y++){const O=m[y];if(c==="")c=O;else{const F=c+r.separator+O;F.length<S?c=F:(N.push(c),c=O)}}return c&&N.push(c),"["+N.join(`
|
|
2
|
+
`+d)+"]"}else{const l=" ".repeat(o+1),d=f-e-2,S=`
|
|
3
|
+
`+`
|
|
4
|
+
`.repeat(Math.max(0,d))+l;return"["+m.join(S)+"]"}}function k(n,t=null,e=null,r=null,a=" ",o="",g="",f=null,h=null,u=null,s=null){const p={...x,linewidth:t??x.linewidth,precision:e??x.precision,suppress:r??x.suppress,separator:a,prefix:o,suffix:g,threshold:f??x.threshold,edgeitems:h??x.edgeitems,floatmode:u??x.floatmode,sign:s??x.sign};if(n.ndim===0){const l=n.iget(0);return P(l,n.dtype,p)}const i=j(n,p),m=p.prefix.length,b=I(n,new Array(n.ndim).fill(0),0,p,i,m);return p.prefix+b+p.suffix}function G(n,t=null,e=null,r=null){let o="array("+k(n,t,e,r,", ");return n.dtype!=="float64"&&(o+=`, dtype='${n.dtype}'`),o+=")",o}function H(n,t=null,e=null,r=null){return k(n,t,e,r)}export{k as array2string,G as array_repr,H as array_str,U as base_repr,B as binary_repr,C as format_float_positional,w as format_float_scientific,E as get_printoptions,R as printoptions,q as set_printoptions};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{ArrayStorage as z}from"../storage";import{promoteDTypes as V,isComplexDType as k,isBigIntDType as J}from"../dtype";import{Complex as I}from"../complex";import*as R from"./shape";function T(t,e){if(t instanceof I||e instanceof I){const g=t instanceof I?t:new I(Number(t),0),p=e instanceof I?e:new I(Number(e),0);return g.mul(p)}return typeof t=="bigint"&&typeof e=="bigint"?Number(t*e):Number(t)*Number(e)}function ce(t,e,g,p,o,c,f,a,h){for(let s=0;s<g;s++)for(let i=0;i<c;i++)for(let l=0;l<f;l++){let n=0;for(let m=0;m<a;m++)n+=t[e+s*a+m]*p[o+i*a*f+m*f+l];h[s*c*f+i*f+l]=n}}function me(t,e,g,p,o,c,f,a,h,s){for(let i=0;i<g;i++)for(let l=0;l<f;l++){let n=0;for(let m=0;m<h;m++){const r=p===1?m:i*h+m,u=a===1?m:l*h+m;n+=t[e+r]*o[c+u]}s[g===1?l:i*f+l]=n}}function fe(t,e,g,p,o,c,f,a,h,s,i,l){for(let r=0;r<g*p;r++)i[r]=0;const n=t==="transpose",m=e==="transpose";if(!n&&!m)for(let r=0;r<g;r++)for(let u=0;u<p;u++){let b=0;for(let y=0;y<o;y++)b+=(f[r*a+y]??0)*(h[y*s+u]??0);i[r*l+u]=c*b}else if(n&&!m)for(let r=0;r<g;r++)for(let u=0;u<p;u++){let b=0;for(let y=0;y<o;y++)b+=(f[y*a+r]??0)*(h[y*s+u]??0);i[r*l+u]=c*b}else if(!n&&m)for(let r=0;r<g;r++)for(let u=0;u<p;u++){let b=0;for(let y=0;y<o;y++)b+=(f[r*a+y]??0)*(h[u*s+y]??0);i[r*l+u]=c*b}else for(let r=0;r<g;r++)for(let u=0;u<p;u++){let b=0;for(let y=0;y<o;y++)b+=(f[y*a+r]??0)*(h[u*s+y]??0);i[r*l+u]=c*b}}function X(t,e){const g=t.ndim,p=e.ndim,o=k(t.dtype)||k(e.dtype);if(g===0||p===0){const c=g===0?t.get():null,f=p===0?e.get():null;if(g===0&&p===0)return T(c,f);if(g===0){const a=V(t.dtype,e.dtype),h=z.zeros([...e.shape],a),s=(i,l)=>{const n=new Array(l.length);let m=i;for(let r=l.length-1;r>=0;r--)n[r]=m%l[r],m=Math.floor(m/l[r]);return n};for(let i=0;i<e.size;i++){const l=s(i,e.shape),n=e.get(...l);h.set(l,T(c,n))}return h}else{const a=V(t.dtype,e.dtype),h=z.zeros([...t.shape],a),s=(i,l)=>{const n=new Array(l.length);let m=i;for(let r=l.length-1;r>=0;r--)n[r]=m%l[r],m=Math.floor(m/l[r]);return n};for(let i=0;i<t.size;i++){const l=s(i,t.shape),n=t.get(...l);h.set(l,T(n,f))}return h}}if(g===1&&p===1){if(t.shape[0]!==e.shape[0])throw new Error(`dot: incompatible shapes (${t.shape[0]},) and (${e.shape[0]},)`);const c=t.shape[0];if(o){let a=0,h=0;for(let s=0;s<c;s++){const i=t.get(s),l=e.get(s),n=T(i,l);n instanceof I?(a+=n.re,h+=n.im):a+=n}return new I(a,h)}let f=0;for(let a=0;a<c;a++){const h=t.get(a),s=e.get(a);typeof h=="bigint"&&typeof s=="bigint"?f=Number(f)+Number(h*s):f+=Number(h)*Number(s)}return f}if(g===2&&p===2)return B(t,e);if(g===2&&p===1){const[c,f]=t.shape,a=e.shape[0];if(f!==a)throw new Error(`dot: incompatible shapes (${c},${f}) and (${a},)`);const h=V(t.dtype,e.dtype),s=z.zeros([c],h);if(o)for(let i=0;i<c;i++){let l=0,n=0;for(let m=0;m<f;m++){const r=t.get(i,m),u=e.get(m),b=r instanceof I?r:new I(Number(r),0),y=u instanceof I?u:new I(Number(u),0);l+=b.re*y.re-b.im*y.im,n+=b.re*y.im+b.im*y.re}s.set([i],new I(l,n))}else for(let i=0;i<c;i++){let l=0;for(let n=0;n<f;n++){const m=t.get(i,n),r=e.get(n);typeof m=="bigint"&&typeof r=="bigint"?l=Number(l)+Number(m*r):l+=Number(m)*Number(r)}s.set([i],l)}return s}if(g===1&&p===2){const c=t.shape[0],[f,a]=e.shape;if(c!==f)throw new Error(`dot: incompatible shapes (${c},) and (${f},${a})`);const h=V(t.dtype,e.dtype),s=z.zeros([a],h);if(o)for(let i=0;i<a;i++){let l=0,n=0;for(let m=0;m<c;m++){const r=t.get(m),u=e.get(m,i),b=r instanceof I?r:new I(Number(r),0),y=u instanceof I?u:new I(Number(u),0);l+=b.re*y.re-b.im*y.im,n+=b.re*y.im+b.im*y.re}s.set([i],new I(l,n))}else for(let i=0;i<a;i++){let l=0;for(let n=0;n<c;n++){const m=t.get(n),r=e.get(n,i);typeof m=="bigint"&&typeof r=="bigint"?l=Number(l)+Number(m*r):l+=Number(m)*Number(r)}s.set([i],l)}return s}if(g>2&&p===1){const c=t.shape[g-1],f=e.shape[0];if(c!==f)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and (${f},)`);const a=[...t.shape.slice(0,-1)],h=V(t.dtype,e.dtype),s=z.zeros(a,h),i=a.reduce((l,n)=>l*n,1);if(o)for(let l=0;l<i;l++){let n=0,m=0,r=l;const u=[];for(let b=a.length-1;b>=0;b--)u[b]=r%a[b],r=Math.floor(r/a[b]);for(let b=0;b<c;b++){const y=[...u,b],w=t.get(...y),d=e.get(b),x=w instanceof I?w:new I(Number(w),0),A=d instanceof I?d:new I(Number(d),0);n+=x.re*A.re-x.im*A.im,m+=x.re*A.im+x.im*A.re}s.set(u,new I(n,m))}else for(let l=0;l<i;l++){let n=0,m=l;const r=[];for(let u=a.length-1;u>=0;u--)r[u]=m%a[u],m=Math.floor(m/a[u]);for(let u=0;u<c;u++){const b=[...r,u],y=t.get(...b),w=e.get(u);typeof y=="bigint"&&typeof w=="bigint"?n=Number(n)+Number(y*w):n+=Number(y)*Number(w)}s.set(r,n)}return s}if(g===1&&p>2){const c=t.shape[0],f=1,a=e.shape[f];if(c!==a)throw new Error(`dot: incompatible shapes (${c},) and ${JSON.stringify(e.shape)}`);const h=[...e.shape.slice(0,f),...e.shape.slice(f+1)],s=V(t.dtype,e.dtype),i=z.zeros(h,s),l=h.reduce((n,m)=>n*m,1);if(o)for(let n=0;n<l;n++){let m=n;const r=[];for(let d=h.length-1;d>=0;d--)r[d]=m%h[d],m=Math.floor(m/h[d]);const u=r.slice(0,f),b=r.slice(f);let y=0,w=0;for(let d=0;d<c;d++){const x=t.get(d),A=[...u,d,...b],S=e.get(...A),v=x instanceof I?x:new I(Number(x),0),N=S instanceof I?S:new I(Number(S),0);y+=v.re*N.re-v.im*N.im,w+=v.re*N.im+v.im*N.re}i.set(r,new I(y,w))}else for(let n=0;n<l;n++){let m=n;const r=[];for(let w=h.length-1;w>=0;w--)r[w]=m%h[w],m=Math.floor(m/h[w]);const u=r.slice(0,f),b=r.slice(f);let y=0;for(let w=0;w<c;w++){const d=t.get(w),x=[...u,w,...b],A=e.get(...x);typeof d=="bigint"&&typeof A=="bigint"?y=Number(y)+Number(d*A):y+=Number(d)*Number(A)}i.set(r,y)}return i}if(g>=2&&p>=2&&!(g===2&&p===2)){const c=t.shape[g-1],f=e.shape[p-2];if(c!==f)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and ${JSON.stringify(e.shape)}`);const a=[...t.shape.slice(0,-1),...e.shape.slice(0,-2),e.shape[p-1]],h=V(t.dtype,e.dtype),s=z.zeros(a,h),i=t.shape.slice(0,-1).reduce((r,u)=>r*u,1),l=e.shape.slice(0,-2).reduce((r,u)=>r*u,1),n=e.shape[p-1],m=c;if(o)for(let r=0;r<i;r++)for(let u=0;u<l;u++)for(let b=0;b<n;b++){let y=0,w=0;for(let A=0;A<m;A++){const S=[];let v=r;for(let j=t.shape.length-2;j>=0;j--)S.unshift(v%t.shape[j]),v=Math.floor(v/t.shape[j]);S.push(A);const N=t.get(...S),M=[];let $=u;for(let j=e.shape.length-3;j>=0;j--)M.unshift($%e.shape[j]),$=Math.floor($/e.shape[j]);M.push(A,b);const D=e.get(...M),E=N instanceof I?N:new I(Number(N),0),C=D instanceof I?D:new I(Number(D),0);y+=E.re*C.re-E.im*C.im,w+=E.re*C.im+E.im*C.re}const d=r*l*n+u*n+b,x=s.data;x[d*2]=y,x[d*2+1]=w}else if(t.isCContiguous&&e.isCContiguous&&!J(t.dtype)&&!J(e.dtype))ce(t.data,t.offset,i,e.data,e.offset,l,n,m,s.data);else for(let r=0;r<i;r++)for(let u=0;u<l;u++)for(let b=0;b<n;b++){let y=0;for(let d=0;d<m;d++){const x=r*m+d,A=u*m*n+d*n+b,S=t.iget(x),v=e.iget(A);typeof S=="bigint"&&typeof v=="bigint"?y=Number(y)+Number(S*v):y+=Number(S)*Number(v)}const w=r*l*n+u*n+b;s.data[w]=y}return s}throw new Error(`dot: unexpected combination of dimensions ${g}D \xB7 ${p}D`)}function B(t,e){if(t.ndim!==2||e.ndim!==2)throw new Error("matmul requires 2D arrays");const[g=0,p=0]=t.shape,[o=0,c=0]=e.shape;if(p!==o)throw new Error(`matmul shape mismatch: (${g},${p}) @ (${o},${c})`);const f=V(t.dtype,e.dtype);if(k(f)){const A=z.zeros([g,c],f),S=A.data;for(let v=0;v<g;v++)for(let N=0;N<c;N++){let M=0,$=0;for(let E=0;E<p;E++){const C=t.iget(v*p+E),j=e.iget(E*c+N);M+=C.re*j.re-C.im*j.im,$+=C.re*j.im+C.im*j.re}const D=v*c+N;S[D*2]=M,S[D*2+1]=$}return A}const a=f.startsWith("int")||f.startsWith("uint")||f==="bool"?"float64":f;if(a!=="float64")throw new Error(`matmul currently only supports float64, got ${a}`);let h=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number)),s=e.dtype==="float64"?e.data:Float64Array.from(Array.from(e.data).map(Number));t.offset>0&&(h=h.subarray(t.offset)),e.offset>0&&(s=s.subarray(e.offset));const[i=0,l=0]=t.strides,[n=0,m=0]=e.strides,r=l>i,u=m>n,b=r?"transpose":"no-transpose",y=u?"transpose":"no-transpose";let w,d;r?w=l:w=i,u?d=m:d=n;const x=z.zeros([g,c],"float64");return fe(b,y,g,c,p,1,h,w,s,d,x.data,c),x}function $e(t){if(t.ndim!==2)throw new Error(`trace requires 2D array, got ${t.ndim}D`);const[e=0,g=0]=t.shape,p=Math.min(e,g);if(k(t.dtype)){let c=0,f=0;for(let a=0;a<p;a++){const h=t.get(a,a);c+=h.re,f+=h.im}return new I(c,f)}let o=0;for(let c=0;c<p;c++){const f=t.get(c,c);typeof f=="bigint"?o=(typeof o=="bigint"?o:BigInt(o))+f:o=(typeof o=="bigint"?Number(o):o)+f}return o}function L(t,e){return R.transpose(t,e)}function Me(t,e){const g=t.ndim,p=e.ndim,o=k(t.dtype)||k(e.dtype),c=t.shape[g-1],f=e.shape[p-1];if(c!==f)throw new Error(`inner: incompatible shapes - last dimensions ${c} and ${f} don't match`);if(g===1&&p===1)return X(t,e);const a=[...t.shape.slice(0,-1),...e.shape.slice(0,-1)],h=V(t.dtype,e.dtype),s=z.zeros(a,h),i=g===1?1:t.shape.slice(0,-1).reduce((m,r)=>m*r,1),l=p===1?1:e.shape.slice(0,-1).reduce((m,r)=>m*r,1),n=c;if(o)for(let m=0;m<i;m++)for(let r=0;r<l;r++){let u=0,b=0;for(let d=0;d<n;d++){let x,A;if(g===1)x=t.get(d);else{const N=[];let M=m;const $=t.shape.slice(0,-1);for(let D=$.length-1;D>=0;D--)N.unshift(M%$[D]),M=Math.floor(M/$[D]);N.push(d),x=t.get(...N)}if(p===1)A=e.get(d);else{const N=[];let M=r;const $=e.shape.slice(0,-1);for(let D=$.length-1;D>=0;D--)N.unshift(M%$[D]),M=Math.floor(M/$[D]);N.push(d),A=e.get(...N)}const S=x instanceof I?x:new I(Number(x),0),v=A instanceof I?A:new I(Number(A),0);u+=S.re*v.re-S.im*v.im,b+=S.re*v.im+S.im*v.re}if(a.length===0)return new I(u,b);const y=i===1?r:m*l+r,w=s.data;w[y*2]=u,w[y*2+1]=b}else if(t.isCContiguous&&e.isCContiguous&&!J(t.dtype)&&!J(e.dtype)){if(a.length===0){const m=t.data,r=e.data,u=t.offset,b=e.offset;let y=0;for(let w=0;w<n;w++)y+=m[u+w]*r[b+w];return y}me(t.data,t.offset,i,g,e.data,e.offset,l,p,n,s.data)}else for(let m=0;m<i;m++)for(let r=0;r<l;r++){let u=0;for(let y=0;y<n;y++){const w=g===1?y:m*n+y,d=p===1?y:r*n+y,x=t.iget(w),A=e.iget(d);typeof x=="bigint"&&typeof A=="bigint"?u=Number(u)+Number(x*A):u+=Number(x)*Number(A)}if(a.length===0)return u;const b=i===1?r:m*l+r;s.data[b]=u}return s}function pe(t,e){const g=t.ndim===1?t:R.ravel(t),p=e.ndim===1?e:R.ravel(e),o=g.size,c=p.size,f=V(t.dtype,e.dtype),a=z.zeros([o,c],f);for(let h=0;h<o;h++)for(let s=0;s<c;s++){const i=g.get(h),l=p.get(s),n=T(i,l);a.set([h,s],n)}return a}function De(t,e,g){let p,o;if(typeof g=="number"){const r=g;if(r<0)throw new Error("tensordot: axes must be non-negative");if(r>t.ndim||r>e.ndim)throw new Error("tensordot: axes exceeds array dimensions");p=Array.from({length:r},(u,b)=>t.ndim-r+b),o=Array.from({length:r},(u,b)=>b)}else if([p,o]=g,p.length!==o.length)throw new Error("tensordot: axes lists must have same length");for(let r=0;r<p.length;r++){const u=p[r],b=o[r];if(u<0||u>=t.ndim||b<0||b>=e.ndim)throw new Error("tensordot: axis out of bounds");if(t.shape[u]!==e.shape[b])throw new Error(`tensordot: shape mismatch on axes ${u} and ${b}: ${t.shape[u]} != ${e.shape[b]}`)}const c=[],f=[];for(let r=0;r<t.ndim;r++)p.includes(r)||c.push(r);for(let r=0;r<e.ndim;r++)o.includes(r)||f.push(r);const a=[...c.map(r=>t.shape[r]),...f.map(r=>e.shape[r])],h=V(t.dtype,e.dtype),s=k(h),i=r=>r instanceof I?{re:r.re,im:r.im}:{re:Number(r),im:0};if(a.length===0){let r=0,u=0;const b=p.map(y=>t.shape[y]).reduce((y,w)=>y*w,1);for(let y=0;y<b;y++){let w=y;const d=new Array(p.length);for(let N=p.length-1;N>=0;N--){const M=p[N];d[N]=w%t.shape[M],w=Math.floor(w/t.shape[M])}const x=new Array(t.ndim),A=new Array(e.ndim);for(let N=0;N<p.length;N++)x[p[N]]=d[N];for(let N=0;N<o.length;N++)A[o[N]]=d[N];const S=t.get(...x),v=e.get(...A);if(s){const N=i(S),M=i(v);r+=N.re*M.re-N.im*M.im,u+=N.re*M.im+N.im*M.re}else typeof S=="bigint"&&typeof v=="bigint"?r+=Number(S*v):r+=Number(S)*Number(v)}return s?new I(r,u):r}const l=z.zeros(a,h),n=a.reduce((r,u)=>r*u,1),m=p.map(r=>t.shape[r]).reduce((r,u)=>r*u,1);for(let r=0;r<n;r++){let u=r;const b=[];for(let A=a.length-1;A>=0;A--)b[A]=u%a[A],u=Math.floor(u/a[A]);const y=b.slice(0,c.length),w=b.slice(c.length);let d=0,x=0;for(let A=0;A<m;A++){u=A;const S=[];for(let D=p.length-1;D>=0;D--){const E=p[D];S[D]=u%t.shape[E],u=Math.floor(u/t.shape[E])}const v=new Array(t.ndim),N=new Array(e.ndim);for(let D=0;D<c.length;D++)v[c[D]]=y[D];for(let D=0;D<f.length;D++)N[f[D]]=w[D];for(let D=0;D<p.length;D++)v[p[D]]=S[D],N[o[D]]=S[D];const M=t.get(...v),$=e.get(...N);if(s){const D=i(M),E=i($);d+=D.re*E.re-D.im*E.im,x+=D.re*E.im+D.im*E.re}else typeof M=="bigint"&&typeof $=="bigint"?d+=Number(M*$):d+=Number(M)*Number($)}s?l.set(b,new I(d,x)):l.set(b,d)}return l}function ze(t,e=0,g=0,p=1){const o=t.shape,c=o.length;if(c<2)throw new Error("diagonal requires an array of at least two dimensions");const f=g<0?c+g:g,a=p<0?c+p:p;if(f<0||f>=c||a<0||a>=c)throw new Error("axis out of bounds");if(f===a)throw new Error("axis1 and axis2 cannot be the same");const h=o[f],s=o[a];let i;e>=0?i=Math.max(0,Math.min(h,s-e)):i=Math.max(0,Math.min(h+e,s));const l=[];for(let u=0;u<c;u++)u!==f&&u!==a&&l.push(o[u]);l.push(i);const n=z.zeros(l,t.dtype),m=o.filter((u,b)=>b!==f&&b!==a),r=m.reduce((u,b)=>u*b,1);for(let u=0;u<r;u++){let b=u;const y=[];for(let w=m.length-1;w>=0;w--)y.unshift(b%m[w]),b=Math.floor(b/m[w]);for(let w=0;w<i;w++){const d=new Array(c);let x=0;for(let v=0;v<c;v++)v===f?d[v]=e>=0?w:w-e:v===a?d[v]=e>=0?w+e:w:d[v]=y[x++];const A=[...y,w],S=t.get(...d);n.set(A,S)}}return n}function Ce(t,...e){const g=t.indexOf("->");let p,o;g===-1?(p=t,o=ne(p)):(p=t.slice(0,g),o=t.slice(g+2));const c=p.split(",").map(u=>u.trim());if(c.length!==e.length)throw new Error(`einsum: expected ${c.length} operands, got ${e.length}`);const f=new Map;for(let u=0;u<e.length;u++){const b=c[u],y=e[u];if(b.length!==y.ndim)throw new Error(`einsum: operand ${u} has ${y.ndim} dimensions but subscript '${b}' has ${b.length} indices`);for(let w=0;w<b.length;w++){const d=b[w],x=y.shape[w];if(f.has(d)){if(f.get(d)!==x)throw new Error(`einsum: size mismatch for index '${d}': ${f.get(d)} vs ${x}`)}else f.set(d,x)}}for(const u of o)if(!f.has(u))throw new Error(`einsum: output subscript contains unknown index '${u}'`);const a=new Set(o),h=new Set;for(const u of c)for(const b of u)h.add(b);const s=[];for(const u of h)a.has(u)||s.push(u);if(e.length===2&&c.length===2){const[u,b]=c,[y,w]=e;if(u.length===2&&b.length===2&&o.length===2&&y.ndim===2&&w.ndim===2){const[d,x]=[u[0],u[1]],[A,S]=[b[0],b[1]],[v,N]=[o[0],o[1]];if(d===v&&S===N&&x===A&&s.length===1&&s[0]===x||d===v&&S===N&&x===A&&s.length===1&&s[0]===x)return B(y,w);if(x===v&&S===N&&d===A&&s.length===1&&s[0]===d){const M=L(y);return B(M,w)}if(d===v&&A===N&&x===S&&s.length===1&&s[0]===x){const M=L(w);return B(y,M)}}if(u.length===1&&b.length===1&&u===b&&o.length===0&&y.ndim===1&&w.ndim===1)return Y(e,c,s,f);if(u&&b&&u.length===1&&b.length===1&&o.length===2&&o===u+b&&s.length===0&&y.ndim===1&&w.ndim===1)return pe(y,w)}if(e.length===1&&c[0].length===2&&o.length===0){const u=c[0];if(u[0]===u[1]&&e[0].ndim===2)return Y(e,c,s,f)}const i=Array.from(o).map(u=>f.get(u));if(i.length===0)return Y(e,c,s,f);let l=e[0].dtype;for(let u=1;u<e.length;u++)l=V(l,e[u].dtype);const n=z.zeros(i,l),m=i.reduce((u,b)=>u*b,1);let r=1;for(const u of s)r*=f.get(u);for(let u=0;u<m;u++){const b=he(u,i),y=new Map;for(let d=0;d<o.length;d++)y.set(o[d],b[d]);let w=0;for(let d=0;d<r;d++){let x=d;for(let S=s.length-1;S>=0;S--){const v=s[S],N=f.get(v);y.set(v,x%N),x=Math.floor(x/N)}let A=1;for(let S=0;S<e.length;S++){const v=e[S],N=c[S],M=[];for(const D of N)M.push(y.get(D));const $=v.get(...M);A*=Number($)}w+=A}n.set(b,w)}return n}function ne(t){const e=new Map,g=t.split(",");for(const o of g)for(const c of o.trim())e.set(c,(e.get(c)||0)+1);const p=[];for(const[o,c]of e)c===1&&p.push(o);return p.sort().join("")}function Y(t,e,g,p){let o=!1;for(const s of t)if(k(s.dtype)){o=!0;break}const c=s=>s instanceof I?{re:s.re,im:s.im}:{re:Number(s),im:0};let f=1;for(const s of g)f*=p.get(s);let a=0,h=0;for(let s=0;s<f;s++){const i=new Map;let l=s;for(let r=g.length-1;r>=0;r--){const u=g[r],b=p.get(u);i.set(u,l%b),l=Math.floor(l/b)}let n=1,m=0;for(let r=0;r<t.length;r++){const u=t[r],b=e[r],y=[];for(const d of b)y.push(i.get(d));const w=u.get(...y);if(o){const d=c(w),x=n*d.re-m*d.im,A=n*d.im+m*d.re;n=x,m=A}else n*=Number(w)}a+=n,h+=m}return o?new I(a,h):a}function he(t,e){const g=new Array(e.length);let p=t;for(let o=e.length-1;o>=0;o--)g[o]=p%e[o],p=Math.floor(p/e[o]);return g}function Ee(t,e){const g=t.shape,p=e.shape,o=g.length,c=p.length,f=V(t.dtype,e.dtype),a=Math.max(o,c),h=new Array(a),s=new Array(a).fill(1),i=new Array(a).fill(1);for(let r=0;r<o;r++)s[a-o+r]=g[r];for(let r=0;r<c;r++)i[a-c+r]=p[r];for(let r=0;r<a;r++)h[r]=s[r]*i[r];const l=z.zeros(h,f),n=g.reduce((r,u)=>r*u,1),m=p.reduce((r,u)=>r*u,1);for(let r=0;r<n;r++){let u=r;const b=new Array(o);for(let d=o-1;d>=0;d--)b[d]=u%g[d],u=Math.floor(u/g[d]);const y=new Array(a).fill(0);for(let d=0;d<o;d++)y[a-o+d]=b[d];const w=t.get(...b);for(let d=0;d<m;d++){let x=d;const A=new Array(c);for(let $=c-1;$>=0;$--)A[$]=x%p[$],x=Math.floor(x/p[$]);const S=new Array(a).fill(0);for(let $=0;$<c;$++)S[a-c+$]=A[$];const v=e.get(...A),N=new Array(a);for(let $=0;$<a;$++)N[$]=y[$]*i[$]+S[$];const M=T(w,v);l.set(N,M)}}return l}function je(t,e,g=-1,p=-1,o=-1,c){c!==void 0&&(g=c,p=c,o=c);const f=(N,M)=>N<0?M+N:N,a=f(g,t.ndim),h=f(p,e.ndim),s=V(t.dtype,e.dtype),i=k(s),l=(N,...M)=>{const $=N.get(...M);return $ instanceof I?$:Number($)},n=(N,M)=>{if(N instanceof I||M instanceof I){const $=N instanceof I?N:new I(N,0),D=M instanceof I?M:new I(M,0);return $.mul(D)}return N*M},m=(N,M)=>{if(N instanceof I||M instanceof I){const $=N instanceof I?N:new I(N,0),D=M instanceof I?M:new I(M,0);return $.sub(D)}return N-M};if(t.ndim===1&&e.ndim===1){const N=t.shape[0],M=e.shape[0];if(N===3&&M===3){const $=l(t,0),D=l(t,1),E=l(t,2),C=l(e,0),j=l(e,1),F=l(e,2),q=z.zeros([3],s);return q.set([0],m(n(D,F),n(E,j))),q.set([1],m(n(E,C),n($,F))),q.set([2],m(n($,j),n(D,C))),q}else if(N===2&&M===2){const $=l(t,0),D=l(t,1),E=l(e,0),C=l(e,1);return m(n($,C),n(D,E))}else if(N===2&&M===3||N===3&&M===2){const $=l(t,0),D=l(t,1),E=N===3?l(t,2):i?new I(0,0):0,C=l(e,0),j=l(e,1),F=M===3?l(e,2):i?new I(0,0):0,q=z.zeros([3],s);return q.set([0],m(n(D,F),n(E,j))),q.set([1],m(n(E,C),n($,F))),q.set([2],m(n($,j),n(D,C))),q}else throw new Error(`cross: incompatible dimensions for cross product: ${N} and ${M}`)}const r=t.shape[a],u=e.shape[h];if(r!==2&&r!==3||u!==2&&u!==3)throw new Error(`cross: incompatible dimensions for cross product: ${r} and ${u}`);const b=r===2&&u===2?0:3,y=[...t.shape.slice(0,a),...t.shape.slice(a+1)],w=[...e.shape.slice(0,h),...e.shape.slice(h+1)];if(y.length!==w.length)throw new Error("cross: incompatible shapes for cross product");for(let N=0;N<y.length;N++)if(y[N]!==w[N])throw new Error("cross: incompatible shapes for cross product");const d=y,x=o<0?d.length+1+o:o;let A;if(b===0?A=d:A=[...d.slice(0,x),b,...d.slice(x)],A.length===0)throw new Error("cross: unexpected scalar result from higher-dimensional input");const S=z.zeros(A,s),v=d.reduce((N,M)=>N*M,1);for(let N=0;N<v;N++){let M=N;const $=[];for(let _=d.length-1;_>=0;_--)$[_]=M%d[_],M=Math.floor(M/d[_]);const D=[...$.slice(0,a),0,...$.slice(a)],E=[...$.slice(0,h),0,...$.slice(h)],C=_=>(D[a]=_,l(t,...D)),j=_=>(E[h]=_,l(e,...E)),F=C(0),q=C(1),te=r===3?C(2):i?new I(0,0):0,U=j(0),G=j(1),re=u===3?j(2):i?new I(0,0):0;if(b===0)S.set($,m(n(F,G),n(q,U)));else{const _=m(n(q,re),n(te,G)),se=m(n(te,U),n(F,re)),ie=m(n(F,G),n(q,U)),H=(le,ae)=>{const ue=[...$.slice(0,x),le,...$.slice(x)];S.set(ue,ae)};H(0,_),H(1,se),H(2,ie)}}return S}function W(t,e=2,g,p=!1){if(typeof e!="number")throw new Error("vector_norm: ord must be a number");if(g==null){const s=t.ndim===1?t:R.ravel(t),i=s.size;let l;if(e===1/0){l=0;for(let n=0;n<i;n++)l=Math.max(l,Math.abs(Number(s.get(n))))}else if(e===-1/0){l=1/0;for(let n=0;n<i;n++)l=Math.min(l,Math.abs(Number(s.get(n))))}else if(e===0){l=0;for(let n=0;n<i;n++)Number(s.get(n))!==0&&l++}else if(e===1){l=0;for(let n=0;n<i;n++)l+=Math.abs(Number(s.get(n)))}else if(e===2){l=0;for(let n=0;n<i;n++){const m=Number(s.get(n));l+=m*m}l=Math.sqrt(l)}else{l=0;for(let n=0;n<i;n++)l+=Math.pow(Math.abs(Number(s.get(n))),e);l=Math.pow(l,1/e)}if(p){const n=new Array(t.ndim).fill(1),m=z.zeros(n,"float64");return m.set(new Array(t.ndim).fill(0),l),m}return l}const o=g<0?t.ndim+g:g;if(o<0||o>=t.ndim)throw new Error(`vector_norm: axis ${g} out of bounds for array with ${t.ndim} dimensions`);const c=p?[...t.shape.slice(0,o),1,...t.shape.slice(o+1)]:[...t.shape.slice(0,o),...t.shape.slice(o+1)];if(c.length===0)return W(t,e,null,!1);const f=z.zeros(c,"float64"),a=t.shape[o],h=c.reduce((s,i)=>s*i,1);for(let s=0;s<h;s++){let i=s;const l=[];for(let r=c.length-1;r>=0;r--)l[r]=i%c[r],i=Math.floor(i/c[r]);const n=p?[...l.slice(0,o),0,...l.slice(o+1)]:[...l.slice(0,o),0,...l.slice(o)];let m;if(e===1/0){m=0;for(let r=0;r<a;r++)n[o]=r,m=Math.max(m,Math.abs(Number(t.get(...n))))}else if(e===-1/0){m=1/0;for(let r=0;r<a;r++)n[o]=r,m=Math.min(m,Math.abs(Number(t.get(...n))))}else if(e===0){m=0;for(let r=0;r<a;r++)n[o]=r,Number(t.get(...n))!==0&&m++}else if(e===1){m=0;for(let r=0;r<a;r++)n[o]=r,m+=Math.abs(Number(t.get(...n)))}else if(e===2){m=0;for(let r=0;r<a;r++){n[o]=r;const u=Number(t.get(...n));m+=u*u}m=Math.sqrt(m)}else{m=0;for(let r=0;r<a;r++)n[o]=r,m+=Math.pow(Math.abs(Number(t.get(...n))),e);m=Math.pow(m,1/e)}f.set(l,m)}return f}function P(t,e="fro",g=!1){if(t.ndim!==2)throw new Error(`matrix_norm: input must be 2D, got ${t.ndim}D`);const[p,o]=t.shape;let c;if(e==="fro"){c=0;for(let f=0;f<p;f++)for(let a=0;a<o;a++){const h=Number(t.get(f,a));c+=h*h}c=Math.sqrt(c)}else if(e==="nuc"){const{s:f}=O(t);c=0;for(let a=0;a<f.size;a++)c+=Number(f.get(a))}else if(e===1){c=0;for(let f=0;f<o;f++){let a=0;for(let h=0;h<p;h++)a+=Math.abs(Number(t.get(h,f)));c=Math.max(c,a)}}else if(e===-1){c=1/0;for(let f=0;f<o;f++){let a=0;for(let h=0;h<p;h++)a+=Math.abs(Number(t.get(h,f)));c=Math.min(c,a)}}else if(e===1/0){c=0;for(let f=0;f<p;f++){let a=0;for(let h=0;h<o;h++)a+=Math.abs(Number(t.get(f,h)));c=Math.max(c,a)}}else if(e===-1/0){c=1/0;for(let f=0;f<p;f++){let a=0;for(let h=0;h<o;h++)a+=Math.abs(Number(t.get(f,h)));c=Math.min(c,a)}}else if(e===2){const{s:f}=O(t);c=Number(f.get(0))}else if(e===-2){const{s:f}=O(t);c=Number(f.get(f.size-1))}else throw new Error(`matrix_norm: invalid ord value: ${e}`);if(g){const f=z.zeros([1,1],"float64");return f.set([0,0],c),f}return c}function Ve(t,e=null,g=null,p=!1){if(e===null)return g===null?W(t,2,null,p):typeof g=="number"?W(t,2,g,p):P(t,"fro",p);if(Array.isArray(g)){if(g.length!==2)throw new Error("norm: axis must be a 2-tuple for matrix norms");const o=g[0]<0?t.ndim+g[0]:g[0],c=g[1]<0?t.ndim+g[1]:g[1];if(t.ndim!==2||o!==0&&o!==1||c!==0&&c!==1||o===c)throw new Error("norm: complex axis specification not yet supported");return P(t,e,p)}if(t.ndim===2&&g===null&&(e==="fro"||e==="nuc"))return P(t,e,p);if(typeof e!="number"&&e!==null)throw new Error(`norm: ord '${e}' not valid for vector norm`);return W(t,e??2,g,p)}function ge(t,e="reduced"){if(t.ndim!==2)throw new Error(`qr: input must be 2D, got ${t.ndim}D`);const[g,p]=t.shape,o=Math.min(g,p),c=z.zeros([g,p],"float64");for(let m=0;m<g;m++)for(let r=0;r<p;r++)c.set([m,r],Number(t.get(m,r)));const f=[],a=[];for(let m=0;m<o;m++){const r=g-m,u=[];for(let A=m;A<g;A++)u.push(Number(c.get(A,m)));let b=0;for(let A=0;A<r;A++)b+=u[A]*u[A];if(b=Math.sqrt(b),b<1e-15){f.push(u),a.push(0);continue}const y=u[0]>=0?1:-1,w=u[0]+y*b,d=[1];for(let A=1;A<r;A++)d.push(u[A]/w);const x=y*w/b;a.push(x),f.push(d);for(let A=m;A<p;A++){let S=0;for(let v=0;v<r;v++)S+=d[v]*Number(c.get(m+v,A));for(let v=0;v<r;v++)c.set([m+v,A],Number(c.get(m+v,A))-x*d[v]*S)}}if(e==="raw"){const m=z.zeros([g,p],"float64");for(let u=0;u<g;u++)for(let b=0;b<p;b++)m.set([u,b],Number(c.get(u,b)));const r=z.zeros([o],"float64");for(let u=0;u<o;u++)r.set([u],a[u]);return{h:m,tau:r}}if(e==="r"){const m=z.zeros([o,p],"float64");for(let r=0;r<o;r++)for(let u=r;u<p;u++)m.set([r,u],Number(c.get(r,u)));return m}const h=e==="complete"?g:o,s=z.zeros([g,h],"float64");for(let m=0;m<Math.min(g,h);m++)s.set([m,m],1);for(let m=o-1;m>=0;m--){const r=f[m],u=a[m],b=g-m;for(let y=m;y<h;y++){let w=0;for(let d=0;d<b;d++)w+=r[d]*Number(s.get(m+d,y));for(let d=0;d<b;d++)s.set([m+d,y],Number(s.get(m+d,y))-u*r[d]*w)}}const i=z.zeros([g,h],"float64");for(let m=0;m<g;m++)for(let r=0;r<h;r++)i.set([m,r],Number(s.get(m,r)));const l=e==="complete"?g:o,n=z.zeros([l,p],"float64");for(let m=0;m<l;m++)for(let r=0;r<p;r++)r>=m&&n.set([m,r],Number(c.get(m,r)));return{q:i,r:n}}function ke(t,e=!1){if(t.ndim!==2)throw new Error(`cholesky: input must be 2D, got ${t.ndim}D`);const[g,p]=t.shape;if(g!==p)throw new Error(`cholesky: matrix must be square, got ${g}x${p}`);const o=g,c=z.zeros([o,o],"float64");for(let f=0;f<o;f++)for(let a=0;a<=f;a++){let h=0;if(f===a){for(let i=0;i<a;i++)h+=Number(c.get(a,i))**2;const s=Number(t.get(a,a))-h;if(s<0)throw new Error("cholesky: matrix is not positive definite");c.set([a,a],Math.sqrt(s))}else{for(let i=0;i<a;i++)h+=Number(c.get(f,i))*Number(c.get(a,i));const s=Number(c.get(a,a));if(Math.abs(s)<1e-15)throw new Error("cholesky: matrix is not positive definite");c.set([f,a],(Number(t.get(f,a))-h)/s)}}if(e){const f=z.zeros([o,o],"float64");for(let a=0;a<o;a++)for(let h=a;h<o;h++)f.set([a,h],Number(c.get(h,a)));return f}return c}function O(t){if(t.ndim!==2)throw new Error(`svd: input must be 2D, got ${t.ndim}D`);const[e,g]=t.shape,p=Math.min(e,g),o=z.zeros([g,g],"float64");for(let l=0;l<g;l++)for(let n=0;n<g;n++){let m=0;for(let r=0;r<e;r++)m+=Number(t.get(r,l))*Number(t.get(r,n));o.set([l,n],m)}const{values:c,vectors:f}=Z(o),a=Array.from({length:g},(l,n)=>n);a.sort((l,n)=>c[n]-c[l]);const h=z.zeros([p],"float64");for(let l=0;l<p;l++){const n=c[a[l]];h.set([l],Math.sqrt(Math.max(0,n)))}const s=z.zeros([g,g],"float64");for(let l=0;l<g;l++)for(let n=0;n<g;n++)s.set([l,n],f[n][a[l]]);const i=z.zeros([e,e],"float64");for(let l=0;l<e;l++)for(let n=0;n<p;n++){const m=Number(h.get(n));if(m>1e-10){let r=0;for(let u=0;u<g;u++)r+=Number(t.get(l,u))*Number(s.get(n,u));i.set([l,n],r/m)}}if(e>p)for(let l=p;l<e;l++){const n=new Array(e).fill(0);n[l]=1;for(let r=0;r<l;r++){let u=0;for(let b=0;b<e;b++)u+=n[b]*Number(i.get(b,r));for(let b=0;b<e;b++)n[b]=n[b]-u*Number(i.get(b,r))}let m=0;for(let r=0;r<e;r++)m+=n[r]*n[r];if(m=Math.sqrt(m),m>1e-10)for(let r=0;r<e;r++)i.set([r,l],n[r]/m)}return{u:i,s:h,vt:s}}function Z(t){const e=t.shape[0],g=100*e*e,p=1e-10,o=[];for(let a=0;a<e;a++){o.push([]);for(let h=0;h<e;h++)o[a].push(Number(t.get(a,h)))}const c=[];for(let a=0;a<e;a++){c.push([]);for(let h=0;h<e;h++)c[a].push(a===h?1:0)}for(let a=0;a<g;a++){let h=0,s=0,i=1;for(let d=0;d<e;d++)for(let x=d+1;x<e;x++)Math.abs(o[d][x])>h&&(h=Math.abs(o[d][x]),s=d,i=x);if(h<p)break;const l=o[s][s],n=o[i][i],m=o[s][i];let r;Math.abs(l-n)<1e-15?r=Math.PI/4:r=.5*Math.atan2(2*m,n-l);const u=Math.cos(r),b=Math.sin(r),y=u*u*l+b*b*n-2*b*u*m,w=b*b*l+u*u*n+2*b*u*m;o[s][s]=y,o[i][i]=w,o[s][i]=0,o[i][s]=0;for(let d=0;d<e;d++)if(d!==s&&d!==i){const x=o[d][s],A=o[d][i];o[d][s]=u*x-b*A,o[s][d]=o[d][s],o[d][i]=b*x+u*A,o[i][d]=o[d][i]}for(let d=0;d<e;d++){const x=c[d][s],A=c[d][i];c[d][s]=u*x-b*A,c[d][i]=b*x+u*A}}const f=[];for(let a=0;a<e;a++)f.push(o[a][a]);return{values:f,vectors:c}}function be(t,e=!0,g=!0){const p=O(t);if(!g)return p.s;if(!e){const[o,c]=t.shape,f=Math.min(o,c),a=z.zeros([o,f],"float64");for(let s=0;s<o;s++)for(let i=0;i<f;i++)a.set([s,i],Number(p.u.get(s,i)));const h=z.zeros([f,c],"float64");for(let s=0;s<f;s++)for(let i=0;i<c;i++)h.set([s,i],Number(p.vt.get(s,i)));return{u:a,s:p.s,vt:h}}return p}function Fe(t){if(t.ndim!==2)throw new Error(`det: input must be 2D, got ${t.ndim}D`);const[e,g]=t.shape;if(e!==g)throw new Error(`det: matrix must be square, got ${e}x${g}`);const p=e;if(p===0)return 1;const o=t.data;if(p===1)return Number(o[0]);if(p===2)return Number(o[0])*Number(o[3])-Number(o[1])*Number(o[2]);const{lu:c,sign:f}=Q(t),a=c.data;let h=f;for(let s=0;s<p;s++)h*=a[s*p+s];return h}function Q(t){const[e,g]=t.shape,p=e,o=g,c=z.zeros([p,o],"float64"),f=c.data,a=t.data;for(let i=0;i<p*o;i++)f[i]=Number(a[i]);const h=Array.from({length:p},(i,l)=>l);let s=1;for(let i=0;i<Math.min(p,o);i++){let l=Math.abs(f[i*o+i]),n=i;for(let r=i+1;r<p;r++){const u=Math.abs(f[r*o+i]);u>l&&(l=u,n=r)}if(n!==i){for(let u=0;u<o;u++){const b=f[i*o+u];f[i*o+u]=f[n*o+u],f[n*o+u]=b}const r=h[i];h[i]=h[n],h[n]=r,s=-s}const m=f[i*o+i];if(Math.abs(m)>1e-15)for(let r=i+1;r<p;r++){const u=f[r*o+i]/m;f[r*o+i]=u;for(let b=i+1;b<o;b++)f[r*o+b]=f[r*o+b]-u*f[i*o+b]}}return{lu:c,piv:h,sign:s}}function K(t){if(t.ndim!==2)throw new Error(`inv: input must be 2D, got ${t.ndim}D`);const[e,g]=t.shape;if(e!==g)throw new Error(`inv: matrix must be square, got ${e}x${g}`);const p=e,{lu:o,piv:c}=Q(t),f=o.data,a=z.zeros([p,p],"float64"),h=a.data;for(let s=0;s<p;s++){const i=new Float64Array(p);for(let l=0;l<p;l++){let n=c[l]===s?1:0;for(let m=0;m<l;m++)n-=f[l*p+m]*i[m];i[l]=n}for(let l=p-1;l>=0;l--){let n=i[l];for(let r=l+1;r<p;r++)n-=f[l*p+r]*h[r*p+s];const m=f[l*p+l];if(Math.abs(m)<1e-15)throw new Error("inv: singular matrix");h[l*p+s]=n/m}}return a}function oe(t,e){const[g]=t.shape,p=g,{lu:o,piv:c}=Q(t),f=o.data,a=e.data,h=new Float64Array(p);for(let n=0;n<p;n++)h[n]=Number(a[c[n]]);const s=new Float64Array(p);for(let n=0;n<p;n++){let m=h[n];for(let r=0;r<n;r++)m-=f[n*p+r]*s[r];s[n]=m}const i=z.zeros([p],"float64"),l=i.data;for(let n=p-1;n>=0;n--){let m=s[n];for(let u=n+1;u<p;u++)m-=f[n*p+u]*l[u];const r=f[n*p+n];if(Math.abs(r)<1e-15)throw new Error("solve: singular matrix");l[n]=m/r}return i}function de(t,e){if(t.ndim!==2)throw new Error(`solve: coefficient matrix must be 2D, got ${t.ndim}D`);const[g,p]=t.shape;if(g!==p)throw new Error(`solve: coefficient matrix must be square, got ${g}x${p}`);const o=g;if(e.ndim===1){if(e.shape[0]!==o)throw new Error(`solve: incompatible shapes (${g},${p}) and (${e.shape[0]},)`);return oe(t,e)}if(e.ndim===2){if(e.shape[0]!==o)throw new Error(`solve: incompatible shapes (${g},${p}) and (${e.shape[0]},${e.shape[1]})`);const c=e.shape[1],f=z.zeros([o,c],"float64");for(let a=0;a<c;a++){const h=z.zeros([o],"float64");for(let i=0;i<o;i++)h.set([i],Number(e.get(i,a)));const s=oe(t,h);for(let i=0;i<o;i++)f.set([i,a],Number(s.get(i)))}return f}throw new Error(`solve: b must be 1D or 2D, got ${e.ndim}D`)}function qe(t,e,g=null){if(t.ndim!==2)throw new Error(`lstsq: coefficient matrix must be 2D, got ${t.ndim}D`);const[p,o]=t.shape,{u:c,s:f,vt:a}=O(t),h=Math.min(p,o),s=g??Math.max(p,o)*Number.EPSILON,l=Number(f.get(0))*s;let n=0;for(let w=0;w<h;w++)Number(f.get(w))>l&&n++;const m=e.ndim===1?R.reshape(e,[e.size,1]):e,r=m.shape[1];if(m.shape[0]!==p)throw new Error(`lstsq: incompatible shapes (${p},${o}) and (${e.shape.join(",")})`);const u=z.zeros([o,r],"float64");for(let w=0;w<r;w++){const d=new Array(p).fill(0);for(let x=0;x<p;x++)for(let A=0;A<p;A++)d[x]+=Number(c.get(A,x))*Number(m.get(A,w));for(let x=0;x<o;x++){let A=0;for(let S=0;S<h;S++){const v=Number(f.get(S));v>l&&(A+=Number(a.get(S,x))*d[S]/v)}u.set([x,w],A)}}let b;if(p>o){b=z.zeros([r],"float64");for(let w=0;w<r;w++){let d=0;for(let x=0;x<p;x++){let A=0;for(let v=0;v<o;v++)A+=Number(t.get(x,v))*Number(u.get(v,w));const S=A-Number(m.get(x,w));d+=S*S}b.set([w],d)}}else b=z.zeros([0],"float64");return{x:e.ndim===1?R.reshape(u,[o]):u,residuals:b,rank:n,s:f}}function Re(t,e=2){if(t.ndim!==2)throw new Error(`cond: input must be 2D, got ${t.ndim}D`);const[g,p]=t.shape;if(e===2||e===-2){const{s:a}=O(t),h=Math.min(g,p),s=Number(a.get(0)),i=Number(a.get(h-1));return e===2?i>0?s/i:1/0:s>0?i/s:0}if(g!==p)throw new Error(`cond: matrix must be square for p=${e}`);const o=P(t,e),c=K(t),f=P(c,e);return o*f}function _e(t,e){if(t.ndim===0)return Number(t.get())!==0?1:0;if(t.ndim===1){for(let f=0;f<t.size;f++)if(Number(t.get(f))!==0)return 1;return 0}if(t.ndim!==2)throw new Error(`matrix_rank: input must be at most 2D, got ${t.ndim}D`);const{s:g}=O(t),p=Number(g.get(0)),o=e??p*Math.max(t.shape[0],t.shape[1])*Number.EPSILON;let c=0;for(let f=0;f<g.size;f++)Number(g.get(f))>o&&c++;return c}function Be(t,e){if(t.ndim!==2)throw new Error(`matrix_power: input must be 2D, got ${t.ndim}D`);const[g,p]=t.shape;if(g!==p)throw new Error(`matrix_power: matrix must be square, got ${g}x${p}`);const o=g;if(!Number.isInteger(e))throw new Error("matrix_power: exponent must be an integer");if(e===0){const s=z.zeros([o,o],"float64");for(let i=0;i<o;i++)s.set([i,i],1);return s}let c=t,f=e;e<0&&(c=K(t),f=-e);let a=z.zeros([o,o],"float64");for(let s=0;s<o;s++)a.set([s,s],1);let h=z.zeros([o,o],"float64");for(let s=0;s<o;s++)for(let i=0;i<o;i++)h.set([s,i],Number(c.get(s,i)));for(;f>0;)f&1&&(a=B(a,h)),h=B(h,h),f>>=1;return a}function Te(t,e=1e-15){if(t.ndim!==2)throw new Error(`pinv: input must be 2D, got ${t.ndim}D`);const[g,p]=t.shape,{u:o,s:c,vt:f}=O(t),a=Math.min(g,p),s=Number(c.get(0))*e,i=z.zeros([p,g],"float64");for(let l=0;l<p;l++)for(let n=0;n<g;n++){let m=0;for(let r=0;r<a;r++){const u=Number(c.get(r));u>s&&(m+=Number(f.get(r,l))*Number(o.get(n,r))/u)}i.set([l,n],m)}return i}function ye(t){if(t.ndim!==2)throw new Error(`eig: input must be 2D, got ${t.ndim}D`);const[e,g]=t.shape;if(e!==g)throw new Error(`eig: matrix must be square, got ${e}x${g}`);const p=e;let o=!0;e:for(let s=0;s<p;s++)for(let i=s+1;i<p;i++)if(Math.abs(Number(t.get(s,i))-Number(t.get(i,s)))>1e-10){o=!1;break e}if(o){const{values:s,vectors:i}=Z(t),l=z.zeros([p],"float64"),n=z.zeros([p,p],"float64");for(let m=0;m<p;m++){l.set([m],s[m]);for(let r=0;r<p;r++)n.set([r,m],i[r][m])}return{w:l,v:n}}console.warn("numpy-ts: eig() called on non-symmetric matrix. Complex eigenvalues are not supported; results may be inaccurate. For symmetric matrices, use eigh() instead.");const{values:c,vectors:f}=we(t),a=z.zeros([p],"float64"),h=z.zeros([p,p],"float64");for(let s=0;s<p;s++){a.set([s],c[s]);for(let i=0;i<p;i++)h.set([i,s],f[i][s])}return{w:a,v:h}}function we(t){const e=t.shape[0],g=1e3,p=1e-10;let o=z.zeros([e,e],"float64");for(let h=0;h<e;h++)for(let s=0;s<e;s++)o.set([h,s],Number(t.get(h,s)));let c=z.zeros([e,e],"float64");for(let h=0;h<e;h++)c.set([h,h],1);for(let h=0;h<g;h++){let s=0;for(let m=0;m<e;m++)for(let r=0;r<e;r++)m!==r&&(s+=Number(o.get(m,r))**2);if(Math.sqrt(s)<p*e)break;const i=ge(o,"reduced"),l=i.q,n=i.r;o=B(n,l),c=B(c,l)}const f=[];for(let h=0;h<e;h++)f.push(Number(o.get(h,h)));const a=[];for(let h=0;h<e;h++){a.push([]);for(let s=0;s<e;s++)a[h].push(Number(c.get(h,s)))}return{values:f,vectors:a}}function Ae(t,e="L"){if(t.ndim!==2)throw new Error(`eigh: input must be 2D, got ${t.ndim}D`);const[g,p]=t.shape;if(g!==p)throw new Error(`eigh: matrix must be square, got ${g}x${p}`);const o=g,c=z.zeros([o,o],"float64");for(let l=0;l<o;l++)for(let n=0;n<o;n++)e==="L"?l>=n&&(c.set([l,n],Number(t.get(l,n))),c.set([n,l],Number(t.get(l,n)))):n>=l&&(c.set([l,n],Number(t.get(l,n))),c.set([n,l],Number(t.get(l,n))));const{values:f,vectors:a}=Z(c),h=Array.from({length:o},(l,n)=>n);h.sort((l,n)=>f[l]-f[n]);const s=z.zeros([o],"float64"),i=z.zeros([o,o],"float64");for(let l=0;l<o;l++){s.set([l],f[h[l]]);for(let n=0;n<o;n++)i.set([n,l],a[n][h[l]])}return{w:s,v:i}}function Oe(t){const{w:e}=ye(t);return e}function Le(t,e="L"){const{w:g}=Ae(t,e);return g}function Pe(t,e){const g=R.flatten(t),p=R.flatten(e),o=g.shape[0],c=p.shape[0];if(o!==c)throw new Error(`vdot: arrays must have same number of elements, got ${o} and ${c}`);if(k(t.dtype)||k(e.dtype)){let h=0,s=0;for(let i=0;i<o;i++){const l=g.get(i),n=p.get(i),m=l instanceof I?l.re:Number(l),r=l instanceof I?-l.im:0,u=n instanceof I?n.re:Number(n),b=n instanceof I?n.im:0;h+=m*u+r*b,s+=m*b-r*u}return Math.abs(s)<1e-15?h:new I(h,s)}let a=0;for(let h=0;h<o;h++){const s=g.get(h),i=p.get(h);typeof s=="bigint"&&typeof i=="bigint"?a=(typeof a=="bigint"?a:BigInt(a))+s*i:a=(typeof a=="bigint"?Number(a):a)+Number(s)*Number(i)}return a}function Je(t,e,g=-1){const p=t.ndim,o=e.ndim,c=g<0?p+g:g,f=g<0?o+g:g;if(c<0||c>=p)throw new Error(`vecdot: axis ${g} out of bounds for array with ${p} dimensions`);if(f<0||f>=o)throw new Error(`vecdot: axis ${g} out of bounds for array with ${o} dimensions`);const a=t.shape[c],h=e.shape[f];if(a!==h)throw new Error(`vecdot: axis dimensions must match, got ${a} and ${h}`);if(p===1&&o===1)return X(t,e);const s=[...t.shape.slice(0,c),...t.shape.slice(c+1)],i=[...e.shape.slice(0,f),...e.shape.slice(f+1)],l=a,n=k(t.dtype)||k(e.dtype),m=V(t.dtype,e.dtype),r=s.length>i.length?s:i;if(r.length===0){let y=n?new I(0,0):0;for(let w=0;w<l;w++){const d=t.get(w),x=e.get(w),A=T(d,x);if(y instanceof I||A instanceof I){const S=y instanceof I?y:new I(Number(y),0),v=A instanceof I?A:new I(Number(A),0);y=S.add(v)}else typeof y=="bigint"||typeof A=="bigint"?y=BigInt(y)+BigInt(A):y=y+A}return y}const u=z.zeros(r,m),b=r.reduce((y,w)=>y*w,1);for(let y=0;y<b;y++){const w=[];let d=y;for(let v=r.length-1;v>=0;v--)w.unshift(d%r[v]),d=Math.floor(d/r[v]);const x=[...w.slice(0,c),0,...w.slice(c)],A=[...w.slice(0,f),0,...w.slice(f)];let S=n?new I(0,0):0;for(let v=0;v<l;v++){x[c]=v,A[f]=v;const N=t.get(...x),M=e.get(...A),$=T(N,M);if(S instanceof I||$ instanceof I){const D=S instanceof I?S:new I(Number(S),0),E=$ instanceof I?$:new I(Number($),0);S=D.add(E)}else typeof S=="bigint"||typeof $=="bigint"?S=BigInt(S)+BigInt($):S=S+$}u.set(w,S)}return u}function Xe(t){if(t.ndim<2)throw new Error(`matrix_transpose: input must have at least 2 dimensions, got ${t.ndim}D`);const e=Array.from({length:t.ndim},(p,o)=>o),g=e.length-1;return e[g]=g-1,e[g-1]=g,L(t,e)}function We(t,e){return L(t,e)}function Qe(t,e){if(t.ndim<2)throw new Error(`matvec: x1 must have at least 2 dimensions, got ${t.ndim}D`);if(e.ndim<1)throw new Error(`matvec: x2 must have at least 1 dimension, got ${e.ndim}D`);const g=t.shape[t.ndim-2],p=t.shape[t.ndim-1],o=e.shape[e.ndim-1];if(p!==o)throw new Error(`matvec: last axis of x1 (${p}) must match last axis of x2 (${o})`);if(t.ndim===2&&e.ndim===1)return X(t,e);const c=t.shape.slice(0,-2),f=e.shape.slice(0,-1),a=Math.max(c.length,f.length),h=[...Array(a-c.length).fill(1),...c],s=[...Array(a-f.length).fill(1),...f],i=[];for(let b=0;b<a;b++){const y=h[b],w=s[b];if(y!==1&&w!==1&&y!==w)throw new Error(`matvec: batch dimensions not broadcastable: ${c} vs ${f}`);i.push(Math.max(y,w))}const l=[...i,g],n=V(t.dtype,e.dtype),m=z.zeros(l,n),r=k(n),u=i.reduce((b,y)=>b*y,1);for(let b=0;b<u;b++){const y=[];let w=b;for(let A=i.length-1;A>=0;A--)y.unshift(w%i[A]),w=Math.floor(w/i[A]);const d=y.slice(-(c.length||1)).map((A,S)=>(c[S]??1)===1?0:A),x=y.slice(-(f.length||1)).map((A,S)=>(f[S]??1)===1?0:A);for(let A=0;A<g;A++){let S=r?new I(0,0):0;for(let N=0;N<p;N++){const M=[...d,A,N],$=[...x,N],D=t.get(...M),E=e.get(...$),C=T(D,E);if(S instanceof I||C instanceof I){const j=S instanceof I?S:new I(Number(S),0),F=C instanceof I?C:new I(Number(C),0);S=j.add(F)}else typeof S=="bigint"||typeof C=="bigint"?S=BigInt(S)+BigInt(C):S=S+C}const v=[...y,A];m.set(v,S)}}return m}function Ue(t,e){if(t.ndim<1)throw new Error(`vecmat: x1 must have at least 1 dimension, got ${t.ndim}D`);if(e.ndim<2)throw new Error(`vecmat: x2 must have at least 2 dimensions, got ${e.ndim}D`);const g=t.shape[t.ndim-1],p=e.shape[e.ndim-2],o=e.shape[e.ndim-1];if(g!==p)throw new Error(`vecmat: last axis of x1 (${g}) must match second-to-last axis of x2 (${p})`);if(t.ndim===1&&e.ndim===2)return X(t,e);const c=t.shape.slice(0,-1),f=e.shape.slice(0,-2),a=Math.max(c.length,f.length),h=[...Array(a-c.length).fill(1),...c],s=[...Array(a-f.length).fill(1),...f],i=[];for(let b=0;b<a;b++){const y=h[b],w=s[b];if(y!==1&&w!==1&&y!==w)throw new Error(`vecmat: batch dimensions not broadcastable: ${c} vs ${f}`);i.push(Math.max(y,w))}const l=[...i,o],n=V(t.dtype,e.dtype),m=z.zeros(l,n),r=k(n),u=i.reduce((b,y)=>b*y,1);for(let b=0;b<u;b++){const y=[];let w=b;for(let A=i.length-1;A>=0;A--)y.unshift(w%i[A]),w=Math.floor(w/i[A]);const d=y.slice(-(c.length||1)).map((A,S)=>(c[S]??1)===1?0:A),x=y.slice(-(f.length||1)).map((A,S)=>(f[S]??1)===1?0:A);for(let A=0;A<o;A++){let S=r?new I(0,0):0;for(let N=0;N<g;N++){const M=[...d,N],$=[...x,N,A],D=t.get(...M),E=e.get(...$),C=T(D,E);if(S instanceof I||C instanceof I){const j=S instanceof I?S:new I(Number(S),0),F=C instanceof I?C:new I(Number(C),0);S=j.add(F)}else typeof S=="bigint"||typeof C=="bigint"?S=BigInt(S)+BigInt(C):S=S+C}const v=[...y,A];m.set(v,S)}}return m}function Ge(t){if(t.ndim!==2)throw new Error(`slogdet: input must be 2D, got ${t.ndim}D`);const[e,g]=t.shape;if(e!==g)throw new Error(`slogdet: matrix must be square, got ${e}x${g}`);const p=e;if(p===0)return{sign:1,logabsdet:0};const{lu:o,sign:c}=Q(t),f=o.data;let a=0,h=c;for(let s=0;s<p;s++){const i=f[s*p+s];if(i===0)return{sign:0,logabsdet:-1/0};i<0&&(h=-h),a+=Math.log(Math.abs(i))}return{sign:h,logabsdet:a}}function He(t){return be(t,!0,!1)}function Ye(t){if(t.length<2)throw new Error("multi_dot: need at least 2 arrays");if(t.length===2)return B(t[0],t[1]);let e=t[0];for(let g=1;g<t.length;g++)e=B(e,t[g]);return e}function Ze(t,e=2){if(e<=0)throw new Error(`tensorinv: ind must be positive, got ${e}`);const g=t.shape,p=t.ndim;if(p<e)throw new Error(`tensorinv: array has ${p} dimensions, ind=${e} is too large`);let o=1;for(let s=0;s<e;s++)o*=g[s];let c=1;for(let s=e;s<p;s++)c*=g[s];if(o!==c)throw new Error(`tensorinv: product of first ${e} dimensions (${o}) must equal product of remaining dimensions (${c})`);const f=R.reshape(t,[o,c]),a=K(f),h=[...g.slice(e),...g.slice(0,e)];return R.reshape(a,h)}function Ke(t,e,g){const p=t.shape,o=e.shape,c=t.ndim,f=e.ndim;let a;g==null?a=Array.from({length:f},(d,x)=>c-f+x):a=g.map(d=>d<0?c+d:d);const h=[];for(let d=0;d<c;d++)a.includes(d)||h.push(d);const s=[...h,...a],i=L(t,s),n=a.map(d=>p[d]).reduce((d,x)=>d*x,1),r=h.map(d=>p[d]).reduce((d,x)=>d*x,1),u=o.reduce((d,x)=>d*x,1);if(n!==u)throw new Error(`tensorsolve: dimensions don't match - sum dimensions product (${n}) != b total elements (${u})`);if(r!==n)throw new Error(`tensorsolve: non-square problem - other dimensions product (${r}) != sum dimensions product (${n})`);const b=R.reshape(i,[r,n]),y=R.reshape(e,[n]),w=de(b,y);return R.reshape(w,[...o])}function et(t,...e){const g=t.indexOf("->");let p,o;g===-1?(p=t,o=ne(p)):(p=t.slice(0,g),o=t.slice(g+2));const c=p.split(",").map(n=>n.trim());if(c.length!==e.length)throw new Error(`einsum_path: expected ${c.length} operands, got ${e.length}`);const f=e.map(n=>Array.isArray(n)?n:Array.from(n.shape)),a=new Map;for(let n=0;n<e.length;n++){const m=c[n],r=f[n];if(m.length!==r.length)throw new Error(`einsum_path: operand ${n} has ${r.length} dimensions but subscript '${m}' has ${m.length} indices`);for(let u=0;u<m.length;u++){const b=m[u],y=r[u];if(a.has(b)&&a.get(b)!==y)throw new Error(`einsum_path: size mismatch for index '${b}': ${a.get(b)} vs ${y}`);a.set(b,y)}}if(e.length===1){const n=[[0]];return[n,ee(t,f,n,a)]}if(e.length===2){const n=[[0,1]];return[n,ee(t,f,n,a)]}const h=[],s=[...c],i=[...f],l=e.map((n,m)=>m);for(;s.length>1;){let n=0,m=1,r=1/0;for(let y=0;y<s.length;y++)for(let w=y+1;w<s.length;w++){const d=xe(s[y],s[w],i[y],i[w],o,a);d<r&&(r=d,n=y,m=w)}h.push([l[n],l[m]]);const[u,b]=Se(s[n],s[m],i[n],i[m],o,a);s.splice(m,1),s.splice(n,1),i.splice(m,1),i.splice(n,1),l.splice(m,1),l.splice(n,1),s.push(u),i.push(b),l.push(-1)}return[h,ee(t,f,h,a)]}function xe(t,e,g,p,o,c){const f=new Set(t),a=new Set(e);let h=1;for(const s of f)h*=c.get(s)||1;for(const s of a)f.has(s)||(h*=c.get(s)||1);return h}function Se(t,e,g,p,o,c){const f=new Set([...t,...e]),a=new Map;for(const l of t)a.set(l,(a.get(l)||0)+1);for(const l of e)a.set(l,(a.get(l)||0)+1);const h=new Set(o),s=[];for(const l of f)(h.has(l)||a.get(l)===1)&&s.push(l);s.sort();const i=s.map(l=>c.get(l));return[s.join(""),i]}function ee(t,e,g,p){const o=[];o.push(" Complete contraction: "+t),o.push(" Operand shapes: "+e.map(f=>`(${f.join(", ")})`).join(", ")),o.push(" Contraction path: "+JSON.stringify(g));let c=0;for(const f of e)c+=f.reduce((a,h)=>a*h,1);return o.push(" Estimated FLOPS: ~"+c.toExponential(2)),o.join(`
|
|
2
|
-
`)}export{ke as cholesky,Re as cond,je as cross,Fe as det,ze as diagonal,X as dot,ye as eig,Ae as eigh,Oe as eigvals,Le as eigvalsh,
|
|
1
|
+
import{ArrayStorage as z}from"../storage";import{promoteDTypes as V,isComplexDType as k,isBigIntDType as J}from"../dtype";import{Complex as I}from"../complex";import*as R from"./shape";function T(t,e){if(t instanceof I||e instanceof I){const h=t instanceof I?t:new I(Number(t),0),p=e instanceof I?e:new I(Number(e),0);return h.mul(p)}return typeof t=="bigint"&&typeof e=="bigint"?Number(t*e):Number(t)*Number(e)}function ce(t,e,h,p,i,u,f,c,g){for(let o=0;o<h;o++)for(let s=0;s<u;s++)for(let l=0;l<f;l++){let n=0;for(let m=0;m<c;m++)n+=t[e+o*c+m]*p[i+s*c*f+m*f+l];g[o*u*f+s*f+l]=n}}function me(t,e,h,p,i,u,f,c,g,o){for(let s=0;s<h;s++)for(let l=0;l<f;l++){let n=0;for(let m=0;m<g;m++){const r=p===1?m:s*g+m,a=c===1?m:l*g+m;n+=t[e+r]*i[u+a]}o[h===1?l:s*f+l]=n}}function fe(t,e,h,p,i,u,f,c,g,o,s,l){for(let r=0;r<h*p;r++)s[r]=0;const n=t==="transpose",m=e==="transpose";if(!n&&!m)for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[r*c+y]??0)*(g[y*o+a]??0);s[r*l+a]=u*b}else if(n&&!m)for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[y*c+r]??0)*(g[y*o+a]??0);s[r*l+a]=u*b}else if(!n&&m)for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[r*c+y]??0)*(g[a*o+y]??0);s[r*l+a]=u*b}else for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[y*c+r]??0)*(g[a*o+y]??0);s[r*l+a]=u*b}}function X(t,e){const h=t.ndim,p=e.ndim,i=k(t.dtype)||k(e.dtype);if(h===0||p===0){const u=h===0?t.get():null,f=p===0?e.get():null;if(h===0&&p===0)return T(u,f);if(h===0){const c=V(t.dtype,e.dtype),g=z.zeros([...e.shape],c),o=(s,l)=>{const n=new Array(l.length);let m=s;for(let r=l.length-1;r>=0;r--)n[r]=m%l[r],m=Math.floor(m/l[r]);return n};for(let s=0;s<e.size;s++){const l=o(s,e.shape),n=e.get(...l);g.set(l,T(u,n))}return g}else{const c=V(t.dtype,e.dtype),g=z.zeros([...t.shape],c),o=(s,l)=>{const n=new Array(l.length);let m=s;for(let r=l.length-1;r>=0;r--)n[r]=m%l[r],m=Math.floor(m/l[r]);return n};for(let s=0;s<t.size;s++){const l=o(s,t.shape),n=t.get(...l);g.set(l,T(n,f))}return g}}if(h===1&&p===1){if(t.shape[0]!==e.shape[0])throw new Error(`dot: incompatible shapes (${t.shape[0]},) and (${e.shape[0]},)`);const u=t.shape[0];if(i){let c=0,g=0;for(let o=0;o<u;o++){const s=t.get(o),l=e.get(o),n=T(s,l);n instanceof I?(c+=n.re,g+=n.im):c+=n}return new I(c,g)}let f=0;for(let c=0;c<u;c++){const g=t.get(c),o=e.get(c);typeof g=="bigint"&&typeof o=="bigint"?f=Number(f)+Number(g*o):f+=Number(g)*Number(o)}return f}if(h===2&&p===2)return B(t,e);if(h===2&&p===1){const[u,f]=t.shape,c=e.shape[0];if(f!==c)throw new Error(`dot: incompatible shapes (${u},${f}) and (${c},)`);const g=V(t.dtype,e.dtype),o=z.zeros([u],g);if(i)for(let s=0;s<u;s++){let l=0,n=0;for(let m=0;m<f;m++){const r=t.get(s,m),a=e.get(m),b=r instanceof I?r:new I(Number(r),0),y=a instanceof I?a:new I(Number(a),0);l+=b.re*y.re-b.im*y.im,n+=b.re*y.im+b.im*y.re}o.set([s],new I(l,n))}else for(let s=0;s<u;s++){let l=0;for(let n=0;n<f;n++){const m=t.get(s,n),r=e.get(n);typeof m=="bigint"&&typeof r=="bigint"?l=Number(l)+Number(m*r):l+=Number(m)*Number(r)}o.set([s],l)}return o}if(h===1&&p===2){const u=t.shape[0],[f,c]=e.shape;if(u!==f)throw new Error(`dot: incompatible shapes (${u},) and (${f},${c})`);const g=V(t.dtype,e.dtype),o=z.zeros([c],g);if(i)for(let s=0;s<c;s++){let l=0,n=0;for(let m=0;m<u;m++){const r=t.get(m),a=e.get(m,s),b=r instanceof I?r:new I(Number(r),0),y=a instanceof I?a:new I(Number(a),0);l+=b.re*y.re-b.im*y.im,n+=b.re*y.im+b.im*y.re}o.set([s],new I(l,n))}else for(let s=0;s<c;s++){let l=0;for(let n=0;n<u;n++){const m=t.get(n),r=e.get(n,s);typeof m=="bigint"&&typeof r=="bigint"?l=Number(l)+Number(m*r):l+=Number(m)*Number(r)}o.set([s],l)}return o}if(h>2&&p===1){const u=t.shape[h-1],f=e.shape[0];if(u!==f)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and (${f},)`);const c=[...t.shape.slice(0,-1)],g=V(t.dtype,e.dtype),o=z.zeros(c,g),s=c.reduce((l,n)=>l*n,1);if(i)for(let l=0;l<s;l++){let n=0,m=0,r=l;const a=[];for(let b=c.length-1;b>=0;b--)a[b]=r%c[b],r=Math.floor(r/c[b]);for(let b=0;b<u;b++){const y=[...a,b],w=t.get(...y),d=e.get(b),x=w instanceof I?w:new I(Number(w),0),A=d instanceof I?d:new I(Number(d),0);n+=x.re*A.re-x.im*A.im,m+=x.re*A.im+x.im*A.re}o.set(a,new I(n,m))}else for(let l=0;l<s;l++){let n=0,m=l;const r=[];for(let a=c.length-1;a>=0;a--)r[a]=m%c[a],m=Math.floor(m/c[a]);for(let a=0;a<u;a++){const b=[...r,a],y=t.get(...b),w=e.get(a);typeof y=="bigint"&&typeof w=="bigint"?n=Number(n)+Number(y*w):n+=Number(y)*Number(w)}o.set(r,n)}return o}if(h===1&&p>2){const u=t.shape[0],f=1,c=e.shape[f];if(u!==c)throw new Error(`dot: incompatible shapes (${u},) and ${JSON.stringify(e.shape)}`);const g=[...e.shape.slice(0,f),...e.shape.slice(f+1)],o=V(t.dtype,e.dtype),s=z.zeros(g,o),l=g.reduce((n,m)=>n*m,1);if(i)for(let n=0;n<l;n++){let m=n;const r=[];for(let d=g.length-1;d>=0;d--)r[d]=m%g[d],m=Math.floor(m/g[d]);const a=r.slice(0,f),b=r.slice(f);let y=0,w=0;for(let d=0;d<u;d++){const x=t.get(d),A=[...a,d,...b],S=e.get(...A),v=x instanceof I?x:new I(Number(x),0),N=S instanceof I?S:new I(Number(S),0);y+=v.re*N.re-v.im*N.im,w+=v.re*N.im+v.im*N.re}s.set(r,new I(y,w))}else for(let n=0;n<l;n++){let m=n;const r=[];for(let w=g.length-1;w>=0;w--)r[w]=m%g[w],m=Math.floor(m/g[w]);const a=r.slice(0,f),b=r.slice(f);let y=0;for(let w=0;w<u;w++){const d=t.get(w),x=[...a,w,...b],A=e.get(...x);typeof d=="bigint"&&typeof A=="bigint"?y=Number(y)+Number(d*A):y+=Number(d)*Number(A)}s.set(r,y)}return s}if(h>=2&&p>=2&&!(h===2&&p===2)){const u=t.shape[h-1],f=e.shape[p-2];if(u!==f)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and ${JSON.stringify(e.shape)}`);const c=[...t.shape.slice(0,-1),...e.shape.slice(0,-2),e.shape[p-1]],g=V(t.dtype,e.dtype),o=z.zeros(c,g),s=t.shape.slice(0,-1).reduce((r,a)=>r*a,1),l=e.shape.slice(0,-2).reduce((r,a)=>r*a,1),n=e.shape[p-1],m=u;if(i)for(let r=0;r<s;r++)for(let a=0;a<l;a++)for(let b=0;b<n;b++){let y=0,w=0;for(let A=0;A<m;A++){const S=[];let v=r;for(let j=t.shape.length-2;j>=0;j--)S.unshift(v%t.shape[j]),v=Math.floor(v/t.shape[j]);S.push(A);const N=t.get(...S),M=[];let $=a;for(let j=e.shape.length-3;j>=0;j--)M.unshift($%e.shape[j]),$=Math.floor($/e.shape[j]);M.push(A,b);const D=e.get(...M),C=N instanceof I?N:new I(Number(N),0),E=D instanceof I?D:new I(Number(D),0);y+=C.re*E.re-C.im*E.im,w+=C.re*E.im+C.im*E.re}const d=r*l*n+a*n+b,x=o.data;x[d*2]=y,x[d*2+1]=w}else if(t.isCContiguous&&e.isCContiguous&&!J(t.dtype)&&!J(e.dtype))ce(t.data,t.offset,s,e.data,e.offset,l,n,m,o.data);else for(let r=0;r<s;r++)for(let a=0;a<l;a++)for(let b=0;b<n;b++){let y=0;for(let d=0;d<m;d++){const x=r*m+d,A=a*m*n+d*n+b,S=t.iget(x),v=e.iget(A);typeof S=="bigint"&&typeof v=="bigint"?y=Number(y)+Number(S*v):y+=Number(S)*Number(v)}const w=r*l*n+a*n+b;o.data[w]=y}return o}throw new Error(`dot: unexpected combination of dimensions ${h}D \xB7 ${p}D`)}function B(t,e){if(t.ndim!==2||e.ndim!==2)throw new Error("matmul requires 2D arrays");const[h=0,p=0]=t.shape,[i=0,u=0]=e.shape;if(p!==i)throw new Error(`matmul shape mismatch: (${h},${p}) @ (${i},${u})`);const f=V(t.dtype,e.dtype);if(k(f)){const A=z.zeros([h,u],f),S=A.data;for(let v=0;v<h;v++)for(let N=0;N<u;N++){let M=0,$=0;for(let C=0;C<p;C++){const E=t.iget(v*p+C),j=e.iget(C*u+N);M+=E.re*j.re-E.im*j.im,$+=E.re*j.im+E.im*j.re}const D=v*u+N;S[D*2]=M,S[D*2+1]=$}return A}const c=f.startsWith("int")||f.startsWith("uint")||f==="bool"?"float64":f;if(c!=="float64")throw new Error(`matmul currently only supports float64, got ${c}`);let g=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number)),o=e.dtype==="float64"?e.data:Float64Array.from(Array.from(e.data).map(Number));t.offset>0&&(g=g.subarray(t.offset)),e.offset>0&&(o=o.subarray(e.offset));const[s=0,l=0]=t.strides,[n=0,m=0]=e.strides,r=l>s,a=m>n,b=r?"transpose":"no-transpose",y=a?"transpose":"no-transpose";let w,d;r?w=l:w=s,a?d=m:d=n;const x=z.zeros([h,u],"float64");return fe(b,y,h,u,p,1,g,w,o,d,x.data,u),x}function $e(t){if(t.ndim!==2)throw new Error(`trace requires 2D array, got ${t.ndim}D`);const[e=0,h=0]=t.shape,p=Math.min(e,h);if(k(t.dtype)){let u=0,f=0;for(let c=0;c<p;c++){const g=t.get(c,c);u+=g.re,f+=g.im}return new I(u,f)}let i=0;for(let u=0;u<p;u++){const f=t.get(u,u);typeof f=="bigint"?i=(typeof i=="bigint"?i:BigInt(i))+f:i=(typeof i=="bigint"?Number(i):i)+f}return i}function L(t,e){return R.transpose(t,e)}function Me(t,e){const h=t.ndim,p=e.ndim,i=k(t.dtype)||k(e.dtype),u=t.shape[h-1],f=e.shape[p-1];if(u!==f)throw new Error(`inner: incompatible shapes - last dimensions ${u} and ${f} don't match`);if(h===1&&p===1)return X(t,e);const c=[...t.shape.slice(0,-1),...e.shape.slice(0,-1)],g=V(t.dtype,e.dtype),o=z.zeros(c,g),s=h===1?1:t.shape.slice(0,-1).reduce((m,r)=>m*r,1),l=p===1?1:e.shape.slice(0,-1).reduce((m,r)=>m*r,1),n=u;if(i)for(let m=0;m<s;m++)for(let r=0;r<l;r++){let a=0,b=0;for(let d=0;d<n;d++){let x,A;if(h===1)x=t.get(d);else{const N=[];let M=m;const $=t.shape.slice(0,-1);for(let D=$.length-1;D>=0;D--)N.unshift(M%$[D]),M=Math.floor(M/$[D]);N.push(d),x=t.get(...N)}if(p===1)A=e.get(d);else{const N=[];let M=r;const $=e.shape.slice(0,-1);for(let D=$.length-1;D>=0;D--)N.unshift(M%$[D]),M=Math.floor(M/$[D]);N.push(d),A=e.get(...N)}const S=x instanceof I?x:new I(Number(x),0),v=A instanceof I?A:new I(Number(A),0);a+=S.re*v.re-S.im*v.im,b+=S.re*v.im+S.im*v.re}if(c.length===0)return new I(a,b);const y=s===1?r:m*l+r,w=o.data;w[y*2]=a,w[y*2+1]=b}else if(t.isCContiguous&&e.isCContiguous&&!J(t.dtype)&&!J(e.dtype)){if(c.length===0){const m=t.data,r=e.data,a=t.offset,b=e.offset;let y=0;for(let w=0;w<n;w++)y+=m[a+w]*r[b+w];return y}me(t.data,t.offset,s,h,e.data,e.offset,l,p,n,o.data)}else for(let m=0;m<s;m++)for(let r=0;r<l;r++){let a=0;for(let y=0;y<n;y++){const w=h===1?y:m*n+y,d=p===1?y:r*n+y,x=t.iget(w),A=e.iget(d);typeof x=="bigint"&&typeof A=="bigint"?a=Number(a)+Number(x*A):a+=Number(x)*Number(A)}if(c.length===0)return a;const b=s===1?r:m*l+r;o.data[b]=a}return o}function pe(t,e){const h=t.ndim===1?t:R.ravel(t),p=e.ndim===1?e:R.ravel(e),i=h.size,u=p.size,f=V(t.dtype,e.dtype),c=z.zeros([i,u],f);for(let g=0;g<i;g++)for(let o=0;o<u;o++){const s=h.get(g),l=p.get(o),n=T(s,l);c.set([g,o],n)}return c}function De(t,e,h){let p,i;if(typeof h=="number"){const r=h;if(r<0)throw new Error("tensordot: axes must be non-negative");if(r>t.ndim||r>e.ndim)throw new Error("tensordot: axes exceeds array dimensions");p=Array.from({length:r},(a,b)=>t.ndim-r+b),i=Array.from({length:r},(a,b)=>b)}else if([p,i]=h,p.length!==i.length)throw new Error("tensordot: axes lists must have same length");for(let r=0;r<p.length;r++){const a=p[r],b=i[r];if(a<0||a>=t.ndim||b<0||b>=e.ndim)throw new Error("tensordot: axis out of bounds");if(t.shape[a]!==e.shape[b])throw new Error(`tensordot: shape mismatch on axes ${a} and ${b}: ${t.shape[a]} != ${e.shape[b]}`)}const u=[],f=[];for(let r=0;r<t.ndim;r++)p.includes(r)||u.push(r);for(let r=0;r<e.ndim;r++)i.includes(r)||f.push(r);const c=[...u.map(r=>t.shape[r]),...f.map(r=>e.shape[r])],g=V(t.dtype,e.dtype),o=k(g),s=r=>r instanceof I?{re:r.re,im:r.im}:{re:Number(r),im:0};if(c.length===0){let r=0,a=0;const b=p.map(y=>t.shape[y]).reduce((y,w)=>y*w,1);for(let y=0;y<b;y++){let w=y;const d=new Array(p.length);for(let N=p.length-1;N>=0;N--){const M=p[N];d[N]=w%t.shape[M],w=Math.floor(w/t.shape[M])}const x=new Array(t.ndim),A=new Array(e.ndim);for(let N=0;N<p.length;N++)x[p[N]]=d[N];for(let N=0;N<i.length;N++)A[i[N]]=d[N];const S=t.get(...x),v=e.get(...A);if(o){const N=s(S),M=s(v);r+=N.re*M.re-N.im*M.im,a+=N.re*M.im+N.im*M.re}else typeof S=="bigint"&&typeof v=="bigint"?r+=Number(S*v):r+=Number(S)*Number(v)}return o?new I(r,a):r}const l=z.zeros(c,g),n=c.reduce((r,a)=>r*a,1),m=p.map(r=>t.shape[r]).reduce((r,a)=>r*a,1);for(let r=0;r<n;r++){let a=r;const b=[];for(let A=c.length-1;A>=0;A--)b[A]=a%c[A],a=Math.floor(a/c[A]);const y=b.slice(0,u.length),w=b.slice(u.length);let d=0,x=0;for(let A=0;A<m;A++){a=A;const S=[];for(let D=p.length-1;D>=0;D--){const C=p[D];S[D]=a%t.shape[C],a=Math.floor(a/t.shape[C])}const v=new Array(t.ndim),N=new Array(e.ndim);for(let D=0;D<u.length;D++)v[u[D]]=y[D];for(let D=0;D<f.length;D++)N[f[D]]=w[D];for(let D=0;D<p.length;D++)v[p[D]]=S[D],N[i[D]]=S[D];const M=t.get(...v),$=e.get(...N);if(o){const D=s(M),C=s($);d+=D.re*C.re-D.im*C.im,x+=D.re*C.im+D.im*C.re}else typeof M=="bigint"&&typeof $=="bigint"?d+=Number(M*$):d+=Number(M)*Number($)}o?l.set(b,new I(d,x)):l.set(b,d)}return l}function ze(t,e=0,h=0,p=1){const i=t.shape,u=i.length;if(u<2)throw new Error("diagonal requires an array of at least two dimensions");const f=h<0?u+h:h,c=p<0?u+p:p;if(f<0||f>=u||c<0||c>=u)throw new Error("axis out of bounds");if(f===c)throw new Error("axis1 and axis2 cannot be the same");const g=i[f],o=i[c];let s;e>=0?s=Math.max(0,Math.min(g,o-e)):s=Math.max(0,Math.min(g+e,o));const l=[];for(let a=0;a<u;a++)a!==f&&a!==c&&l.push(i[a]);l.push(s);const n=z.zeros(l,t.dtype),m=i.filter((a,b)=>b!==f&&b!==c),r=m.reduce((a,b)=>a*b,1);for(let a=0;a<r;a++){let b=a;const y=[];for(let w=m.length-1;w>=0;w--)y.unshift(b%m[w]),b=Math.floor(b/m[w]);for(let w=0;w<s;w++){const d=new Array(u);let x=0;for(let v=0;v<u;v++)v===f?d[v]=e>=0?w:w-e:v===c?d[v]=e>=0?w+e:w:d[v]=y[x++];const A=[...y,w],S=t.get(...d);n.set(A,S)}}return n}function Ee(t,...e){const h=t.indexOf("->");let p,i;h===-1?(p=t,i=ne(p)):(p=t.slice(0,h),i=t.slice(h+2));const u=p.split(",").map(a=>a.trim());if(u.length!==e.length)throw new Error(`einsum: expected ${u.length} operands, got ${e.length}`);const f=new Map;for(let a=0;a<e.length;a++){const b=u[a],y=e[a];if(b.length!==y.ndim)throw new Error(`einsum: operand ${a} has ${y.ndim} dimensions but subscript '${b}' has ${b.length} indices`);for(let w=0;w<b.length;w++){const d=b[w],x=y.shape[w];if(f.has(d)){if(f.get(d)!==x)throw new Error(`einsum: size mismatch for index '${d}': ${f.get(d)} vs ${x}`)}else f.set(d,x)}}for(const a of i)if(!f.has(a))throw new Error(`einsum: output subscript contains unknown index '${a}'`);const c=new Set(i),g=new Set;for(const a of u)for(const b of a)g.add(b);const o=[];for(const a of g)c.has(a)||o.push(a);if(e.length===2&&u.length===2){const[a,b]=u,[y,w]=e;if(a.length===2&&b.length===2&&i.length===2&&y.ndim===2&&w.ndim===2){const[d,x]=[a[0],a[1]],[A,S]=[b[0],b[1]],[v,N]=[i[0],i[1]];if(d===v&&S===N&&x===A&&o.length===1&&o[0]===x||d===v&&S===N&&x===A&&o.length===1&&o[0]===x)return B(y,w);if(x===v&&S===N&&d===A&&o.length===1&&o[0]===d){const M=L(y);return B(M,w)}if(d===v&&A===N&&x===S&&o.length===1&&o[0]===x){const M=L(w);return B(y,M)}}if(a.length===1&&b.length===1&&a===b&&i.length===0&&y.ndim===1&&w.ndim===1)return Y(e,u,o,f);if(a&&b&&a.length===1&&b.length===1&&i.length===2&&i===a+b&&o.length===0&&y.ndim===1&&w.ndim===1)return pe(y,w)}if(e.length===1&&u[0].length===2&&i.length===0){const a=u[0];if(a[0]===a[1]&&e[0].ndim===2)return Y(e,u,o,f)}const s=Array.from(i).map(a=>f.get(a));if(s.length===0)return Y(e,u,o,f);let l=e[0].dtype;for(let a=1;a<e.length;a++)l=V(l,e[a].dtype);const n=z.zeros(s,l),m=s.reduce((a,b)=>a*b,1);let r=1;for(const a of o)r*=f.get(a);for(let a=0;a<m;a++){const b=he(a,s),y=new Map;for(let d=0;d<i.length;d++)y.set(i[d],b[d]);let w=0;for(let d=0;d<r;d++){let x=d;for(let S=o.length-1;S>=0;S--){const v=o[S],N=f.get(v);y.set(v,x%N),x=Math.floor(x/N)}let A=1;for(let S=0;S<e.length;S++){const v=e[S],N=u[S],M=[];for(const D of N)M.push(y.get(D));const $=v.get(...M);A*=Number($)}w+=A}n.set(b,w)}return n}function ne(t){const e=new Map,h=t.split(",");for(const i of h)for(const u of i.trim())e.set(u,(e.get(u)||0)+1);const p=[];for(const[i,u]of e)u===1&&p.push(i);return p.sort().join("")}function Y(t,e,h,p){let i=!1;for(const o of t)if(k(o.dtype)){i=!0;break}const u=o=>o instanceof I?{re:o.re,im:o.im}:{re:Number(o),im:0};let f=1;for(const o of h)f*=p.get(o);let c=0,g=0;for(let o=0;o<f;o++){const s=new Map;let l=o;for(let r=h.length-1;r>=0;r--){const a=h[r],b=p.get(a);s.set(a,l%b),l=Math.floor(l/b)}let n=1,m=0;for(let r=0;r<t.length;r++){const a=t[r],b=e[r],y=[];for(const d of b)y.push(s.get(d));const w=a.get(...y);if(i){const d=u(w),x=n*d.re-m*d.im,A=n*d.im+m*d.re;n=x,m=A}else n*=Number(w)}c+=n,g+=m}return i?new I(c,g):c}function he(t,e){const h=new Array(e.length);let p=t;for(let i=e.length-1;i>=0;i--)h[i]=p%e[i],p=Math.floor(p/e[i]);return h}function Ce(t,e){const h=t.shape,p=e.shape,i=h.length,u=p.length,f=V(t.dtype,e.dtype),c=Math.max(i,u),g=new Array(c),o=new Array(c).fill(1),s=new Array(c).fill(1);for(let r=0;r<i;r++)o[c-i+r]=h[r];for(let r=0;r<u;r++)s[c-u+r]=p[r];for(let r=0;r<c;r++)g[r]=o[r]*s[r];const l=z.zeros(g,f),n=h.reduce((r,a)=>r*a,1),m=p.reduce((r,a)=>r*a,1);for(let r=0;r<n;r++){let a=r;const b=new Array(i);for(let d=i-1;d>=0;d--)b[d]=a%h[d],a=Math.floor(a/h[d]);const y=new Array(c).fill(0);for(let d=0;d<i;d++)y[c-i+d]=b[d];const w=t.get(...b);for(let d=0;d<m;d++){let x=d;const A=new Array(u);for(let $=u-1;$>=0;$--)A[$]=x%p[$],x=Math.floor(x/p[$]);const S=new Array(c).fill(0);for(let $=0;$<u;$++)S[c-u+$]=A[$];const v=e.get(...A),N=new Array(c);for(let $=0;$<c;$++)N[$]=y[$]*s[$]+S[$];const M=T(w,v);l.set(N,M)}}return l}function je(t,e,h=-1,p=-1,i=-1,u){u!==void 0&&(h=u,p=u,i=u);const f=(N,M)=>N<0?M+N:N,c=f(h,t.ndim),g=f(p,e.ndim),o=V(t.dtype,e.dtype),s=k(o),l=(N,...M)=>{const $=N.get(...M);return $ instanceof I?$:Number($)},n=(N,M)=>{if(N instanceof I||M instanceof I){const $=N instanceof I?N:new I(N,0),D=M instanceof I?M:new I(M,0);return $.mul(D)}return N*M},m=(N,M)=>{if(N instanceof I||M instanceof I){const $=N instanceof I?N:new I(N,0),D=M instanceof I?M:new I(M,0);return $.sub(D)}return N-M};if(t.ndim===1&&e.ndim===1){const N=t.shape[0],M=e.shape[0];if(N===3&&M===3){const $=l(t,0),D=l(t,1),C=l(t,2),E=l(e,0),j=l(e,1),F=l(e,2),q=z.zeros([3],o);return q.set([0],m(n(D,F),n(C,j))),q.set([1],m(n(C,E),n($,F))),q.set([2],m(n($,j),n(D,E))),q}else if(N===2&&M===2){const $=l(t,0),D=l(t,1),C=l(e,0),E=l(e,1);return m(n($,E),n(D,C))}else if(N===2&&M===3||N===3&&M===2){const $=l(t,0),D=l(t,1),C=N===3?l(t,2):s?new I(0,0):0,E=l(e,0),j=l(e,1),F=M===3?l(e,2):s?new I(0,0):0,q=z.zeros([3],o);return q.set([0],m(n(D,F),n(C,j))),q.set([1],m(n(C,E),n($,F))),q.set([2],m(n($,j),n(D,E))),q}else throw new Error(`cross: incompatible dimensions for cross product: ${N} and ${M}`)}const r=t.shape[c],a=e.shape[g];if(r!==2&&r!==3||a!==2&&a!==3)throw new Error(`cross: incompatible dimensions for cross product: ${r} and ${a}`);const b=r===2&&a===2?0:3,y=[...t.shape.slice(0,c),...t.shape.slice(c+1)],w=[...e.shape.slice(0,g),...e.shape.slice(g+1)];if(y.length!==w.length)throw new Error("cross: incompatible shapes for cross product");for(let N=0;N<y.length;N++)if(y[N]!==w[N])throw new Error("cross: incompatible shapes for cross product");const d=y,x=i<0?d.length+1+i:i;let A;if(b===0?A=d:A=[...d.slice(0,x),b,...d.slice(x)],A.length===0)throw new Error("cross: unexpected scalar result from higher-dimensional input");const S=z.zeros(A,o),v=d.reduce((N,M)=>N*M,1);for(let N=0;N<v;N++){let M=N;const $=[];for(let _=d.length-1;_>=0;_--)$[_]=M%d[_],M=Math.floor(M/d[_]);const D=[...$.slice(0,c),0,...$.slice(c)],C=[...$.slice(0,g),0,...$.slice(g)],E=_=>(D[c]=_,l(t,...D)),j=_=>(C[g]=_,l(e,...C)),F=E(0),q=E(1),te=r===3?E(2):s?new I(0,0):0,U=j(0),G=j(1),re=a===3?j(2):s?new I(0,0):0;if(b===0)S.set($,m(n(F,G),n(q,U)));else{const _=m(n(q,re),n(te,G)),se=m(n(te,U),n(F,re)),ie=m(n(F,G),n(q,U)),H=(le,ae)=>{const ue=[...$.slice(0,x),le,...$.slice(x)];S.set(ue,ae)};H(0,_),H(1,se),H(2,ie)}}return S}function W(t,e=2,h,p=!1){if(typeof e!="number")throw new Error("vector_norm: ord must be a number");if(h==null){const o=t.ndim===1?t:R.ravel(t),s=o.size;let l;if(e===1/0){l=0;for(let n=0;n<s;n++)l=Math.max(l,Math.abs(Number(o.get(n))))}else if(e===-1/0){l=1/0;for(let n=0;n<s;n++)l=Math.min(l,Math.abs(Number(o.get(n))))}else if(e===0){l=0;for(let n=0;n<s;n++)Number(o.get(n))!==0&&l++}else if(e===1){l=0;for(let n=0;n<s;n++)l+=Math.abs(Number(o.get(n)))}else if(e===2){l=0;for(let n=0;n<s;n++){const m=Number(o.get(n));l+=m*m}l=Math.sqrt(l)}else{l=0;for(let n=0;n<s;n++)l+=Math.pow(Math.abs(Number(o.get(n))),e);l=Math.pow(l,1/e)}if(p){const n=new Array(t.ndim).fill(1),m=z.zeros(n,"float64");return m.set(new Array(t.ndim).fill(0),l),m}return l}const i=h<0?t.ndim+h:h;if(i<0||i>=t.ndim)throw new Error(`vector_norm: axis ${h} out of bounds for array with ${t.ndim} dimensions`);const u=p?[...t.shape.slice(0,i),1,...t.shape.slice(i+1)]:[...t.shape.slice(0,i),...t.shape.slice(i+1)];if(u.length===0)return W(t,e,null,!1);const f=z.zeros(u,"float64"),c=t.shape[i],g=u.reduce((o,s)=>o*s,1);for(let o=0;o<g;o++){let s=o;const l=[];for(let r=u.length-1;r>=0;r--)l[r]=s%u[r],s=Math.floor(s/u[r]);const n=p?[...l.slice(0,i),0,...l.slice(i+1)]:[...l.slice(0,i),0,...l.slice(i)];let m;if(e===1/0){m=0;for(let r=0;r<c;r++)n[i]=r,m=Math.max(m,Math.abs(Number(t.get(...n))))}else if(e===-1/0){m=1/0;for(let r=0;r<c;r++)n[i]=r,m=Math.min(m,Math.abs(Number(t.get(...n))))}else if(e===0){m=0;for(let r=0;r<c;r++)n[i]=r,Number(t.get(...n))!==0&&m++}else if(e===1){m=0;for(let r=0;r<c;r++)n[i]=r,m+=Math.abs(Number(t.get(...n)))}else if(e===2){m=0;for(let r=0;r<c;r++){n[i]=r;const a=Number(t.get(...n));m+=a*a}m=Math.sqrt(m)}else{m=0;for(let r=0;r<c;r++)n[i]=r,m+=Math.pow(Math.abs(Number(t.get(...n))),e);m=Math.pow(m,1/e)}f.set(l,m)}return f}function P(t,e="fro",h=!1){if(t.ndim!==2)throw new Error(`matrix_norm: input must be 2D, got ${t.ndim}D`);const[p,i]=t.shape;let u;if(e==="fro"){u=0;for(let f=0;f<p;f++)for(let c=0;c<i;c++){const g=Number(t.get(f,c));u+=g*g}u=Math.sqrt(u)}else if(e==="nuc"){const{s:f}=O(t);u=0;for(let c=0;c<f.size;c++)u+=Number(f.get(c))}else if(e===1){u=0;for(let f=0;f<i;f++){let c=0;for(let g=0;g<p;g++)c+=Math.abs(Number(t.get(g,f)));u=Math.max(u,c)}}else if(e===-1){u=1/0;for(let f=0;f<i;f++){let c=0;for(let g=0;g<p;g++)c+=Math.abs(Number(t.get(g,f)));u=Math.min(u,c)}}else if(e===1/0){u=0;for(let f=0;f<p;f++){let c=0;for(let g=0;g<i;g++)c+=Math.abs(Number(t.get(f,g)));u=Math.max(u,c)}}else if(e===-1/0){u=1/0;for(let f=0;f<p;f++){let c=0;for(let g=0;g<i;g++)c+=Math.abs(Number(t.get(f,g)));u=Math.min(u,c)}}else if(e===2){const{s:f}=O(t);u=Number(f.get(0))}else if(e===-2){const{s:f}=O(t);u=Number(f.get(f.size-1))}else throw new Error(`matrix_norm: invalid ord value: ${e}`);if(h){const f=z.zeros([1,1],"float64");return f.set([0,0],u),f}return u}function Ve(t,e=null,h=null,p=!1){if(e===null)return h===null?W(t,2,null,p):typeof h=="number"?W(t,2,h,p):P(t,"fro",p);if(Array.isArray(h)){if(h.length!==2)throw new Error("norm: axis must be a 2-tuple for matrix norms");const i=h[0]<0?t.ndim+h[0]:h[0],u=h[1]<0?t.ndim+h[1]:h[1];if(t.ndim!==2||i!==0&&i!==1||u!==0&&u!==1||i===u)throw new Error("norm: complex axis specification not yet supported");return P(t,e,p)}if(t.ndim===2&&h===null&&(e==="fro"||e==="nuc"))return P(t,e,p);if(typeof e!="number"&&e!==null)throw new Error(`norm: ord '${e}' not valid for vector norm`);return W(t,e??2,h,p)}function ge(t,e="reduced"){if(t.ndim!==2)throw new Error(`qr: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape,i=Math.min(h,p),u=z.zeros([h,p],"float64");for(let m=0;m<h;m++)for(let r=0;r<p;r++)u.set([m,r],Number(t.get(m,r)));const f=[],c=[];for(let m=0;m<i;m++){const r=h-m,a=[];for(let A=m;A<h;A++)a.push(Number(u.get(A,m)));let b=0;for(let A=0;A<r;A++)b+=a[A]*a[A];if(b=Math.sqrt(b),b<1e-15){f.push(a),c.push(0);continue}const y=a[0]>=0?1:-1,w=a[0]+y*b,d=[1];for(let A=1;A<r;A++)d.push(a[A]/w);const x=y*w/b;c.push(x),f.push(d);for(let A=m;A<p;A++){let S=0;for(let v=0;v<r;v++)S+=d[v]*Number(u.get(m+v,A));for(let v=0;v<r;v++)u.set([m+v,A],Number(u.get(m+v,A))-x*d[v]*S)}}if(e==="raw"){const m=z.zeros([h,p],"float64");for(let a=0;a<h;a++)for(let b=0;b<p;b++)m.set([a,b],Number(u.get(a,b)));const r=z.zeros([i],"float64");for(let a=0;a<i;a++)r.set([a],c[a]);return{h:m,tau:r}}if(e==="r"){const m=z.zeros([i,p],"float64");for(let r=0;r<i;r++)for(let a=r;a<p;a++)m.set([r,a],Number(u.get(r,a)));return m}const g=e==="complete"?h:i,o=z.zeros([h,g],"float64");for(let m=0;m<Math.min(h,g);m++)o.set([m,m],1);for(let m=i-1;m>=0;m--){const r=f[m],a=c[m],b=h-m;for(let y=m;y<g;y++){let w=0;for(let d=0;d<b;d++)w+=r[d]*Number(o.get(m+d,y));for(let d=0;d<b;d++)o.set([m+d,y],Number(o.get(m+d,y))-a*r[d]*w)}}const s=z.zeros([h,g],"float64");for(let m=0;m<h;m++)for(let r=0;r<g;r++)s.set([m,r],Number(o.get(m,r)));const l=e==="complete"?h:i,n=z.zeros([l,p],"float64");for(let m=0;m<l;m++)for(let r=0;r<p;r++)r>=m&&n.set([m,r],Number(u.get(m,r)));return{q:s,r:n}}function ke(t,e=!1){if(t.ndim!==2)throw new Error(`cholesky: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`cholesky: matrix must be square, got ${h}x${p}`);const i=h,u=z.zeros([i,i],"float64");for(let f=0;f<i;f++)for(let c=0;c<=f;c++){let g=0;if(f===c){for(let s=0;s<c;s++)g+=Number(u.get(c,s))**2;const o=Number(t.get(c,c))-g;if(o<0)throw new Error("cholesky: matrix is not positive definite");u.set([c,c],Math.sqrt(o))}else{for(let s=0;s<c;s++)g+=Number(u.get(f,s))*Number(u.get(c,s));const o=Number(u.get(c,c));if(Math.abs(o)<1e-15)throw new Error("cholesky: matrix is not positive definite");u.set([f,c],(Number(t.get(f,c))-g)/o)}}if(e){const f=z.zeros([i,i],"float64");for(let c=0;c<i;c++)for(let g=c;g<i;g++)f.set([c,g],Number(u.get(g,c)));return f}return u}function O(t){if(t.ndim!==2)throw new Error(`svd: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape,p=Math.min(e,h),i=z.zeros([h,h],"float64");for(let l=0;l<h;l++)for(let n=0;n<h;n++){let m=0;for(let r=0;r<e;r++)m+=Number(t.get(r,l))*Number(t.get(r,n));i.set([l,n],m)}const{values:u,vectors:f}=Z(i),c=Array.from({length:h},(l,n)=>n);c.sort((l,n)=>u[n]-u[l]);const g=z.zeros([p],"float64");for(let l=0;l<p;l++){const n=u[c[l]];g.set([l],Math.sqrt(Math.max(0,n)))}const o=z.zeros([h,h],"float64");for(let l=0;l<h;l++)for(let n=0;n<h;n++)o.set([l,n],f[n][c[l]]);const s=z.zeros([e,e],"float64");for(let l=0;l<e;l++)for(let n=0;n<p;n++){const m=Number(g.get(n));if(m>1e-10){let r=0;for(let a=0;a<h;a++)r+=Number(t.get(l,a))*Number(o.get(n,a));s.set([l,n],r/m)}}if(e>p)for(let l=p;l<e;l++){const n=new Array(e).fill(0);n[l]=1;for(let r=0;r<l;r++){let a=0;for(let b=0;b<e;b++)a+=n[b]*Number(s.get(b,r));for(let b=0;b<e;b++)n[b]=n[b]-a*Number(s.get(b,r))}let m=0;for(let r=0;r<e;r++)m+=n[r]*n[r];if(m=Math.sqrt(m),m>1e-10)for(let r=0;r<e;r++)s.set([r,l],n[r]/m)}return{u:s,s:g,vt:o}}function Z(t){const e=t.shape[0],h=100*e*e,p=1e-10,i=[];for(let c=0;c<e;c++){i.push([]);for(let g=0;g<e;g++)i[c].push(Number(t.get(c,g)))}const u=[];for(let c=0;c<e;c++){u.push([]);for(let g=0;g<e;g++)u[c].push(c===g?1:0)}for(let c=0;c<h;c++){let g=0,o=0,s=1;for(let d=0;d<e;d++)for(let x=d+1;x<e;x++)Math.abs(i[d][x])>g&&(g=Math.abs(i[d][x]),o=d,s=x);if(g<p)break;const l=i[o][o],n=i[s][s],m=i[o][s];let r;Math.abs(l-n)<1e-15?r=Math.PI/4:r=.5*Math.atan2(2*m,n-l);const a=Math.cos(r),b=Math.sin(r),y=a*a*l+b*b*n-2*b*a*m,w=b*b*l+a*a*n+2*b*a*m;i[o][o]=y,i[s][s]=w,i[o][s]=0,i[s][o]=0;for(let d=0;d<e;d++)if(d!==o&&d!==s){const x=i[d][o],A=i[d][s];i[d][o]=a*x-b*A,i[o][d]=i[d][o],i[d][s]=b*x+a*A,i[s][d]=i[d][s]}for(let d=0;d<e;d++){const x=u[d][o],A=u[d][s];u[d][o]=a*x-b*A,u[d][s]=b*x+a*A}}const f=[];for(let c=0;c<e;c++)f.push(i[c][c]);return{values:f,vectors:u}}function be(t,e=!0,h=!0){const p=O(t);if(!h)return p.s;if(!e){const[i,u]=t.shape,f=Math.min(i,u),c=z.zeros([i,f],"float64");for(let o=0;o<i;o++)for(let s=0;s<f;s++)c.set([o,s],Number(p.u.get(o,s)));const g=z.zeros([f,u],"float64");for(let o=0;o<f;o++)for(let s=0;s<u;s++)g.set([o,s],Number(p.vt.get(o,s)));return{u:c,s:p.s,vt:g}}return p}function Fe(t){if(t.ndim!==2)throw new Error(`det: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`det: matrix must be square, got ${e}x${h}`);const p=e;if(p===0)return 1;const i=t.data;if(p===1)return Number(i[0]);if(p===2)return Number(i[0])*Number(i[3])-Number(i[1])*Number(i[2]);const{lu:u,sign:f}=Q(t),c=u.data;let g=f;for(let o=0;o<p;o++)g*=c[o*p+o];return g}function Q(t){const[e,h]=t.shape,p=e,i=h,u=z.zeros([p,i],"float64"),f=u.data,c=t.data;for(let s=0;s<p*i;s++)f[s]=Number(c[s]);const g=Array.from({length:p},(s,l)=>l);let o=1;for(let s=0;s<Math.min(p,i);s++){let l=Math.abs(f[s*i+s]),n=s;for(let r=s+1;r<p;r++){const a=Math.abs(f[r*i+s]);a>l&&(l=a,n=r)}if(n!==s){for(let a=0;a<i;a++){const b=f[s*i+a];f[s*i+a]=f[n*i+a],f[n*i+a]=b}const r=g[s];g[s]=g[n],g[n]=r,o=-o}const m=f[s*i+s];if(Math.abs(m)>1e-15)for(let r=s+1;r<p;r++){const a=f[r*i+s]/m;f[r*i+s]=a;for(let b=s+1;b<i;b++)f[r*i+b]=f[r*i+b]-a*f[s*i+b]}}return{lu:u,piv:g,sign:o}}function K(t){if(t.ndim!==2)throw new Error(`inv: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`inv: matrix must be square, got ${e}x${h}`);const p=e,{lu:i,piv:u}=Q(t),f=i.data,c=z.zeros([p,p],"float64"),g=c.data;for(let o=0;o<p;o++){const s=new Float64Array(p);for(let l=0;l<p;l++){let n=u[l]===o?1:0;for(let m=0;m<l;m++)n-=f[l*p+m]*s[m];s[l]=n}for(let l=p-1;l>=0;l--){let n=s[l];for(let r=l+1;r<p;r++)n-=f[l*p+r]*g[r*p+o];const m=f[l*p+l];if(Math.abs(m)<1e-15)throw new Error("inv: singular matrix");g[l*p+o]=n/m}}return c}function oe(t,e){const[h]=t.shape,p=h,{lu:i,piv:u}=Q(t),f=i.data,c=e.data,g=new Float64Array(p);for(let n=0;n<p;n++)g[n]=Number(c[u[n]]);const o=new Float64Array(p);for(let n=0;n<p;n++){let m=g[n];for(let r=0;r<n;r++)m-=f[n*p+r]*o[r];o[n]=m}const s=z.zeros([p],"float64"),l=s.data;for(let n=p-1;n>=0;n--){let m=o[n];for(let a=n+1;a<p;a++)m-=f[n*p+a]*l[a];const r=f[n*p+n];if(Math.abs(r)<1e-15)throw new Error("solve: singular matrix");l[n]=m/r}return s}function de(t,e){if(t.ndim!==2)throw new Error(`solve: coefficient matrix must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`solve: coefficient matrix must be square, got ${h}x${p}`);const i=h;if(e.ndim===1){if(e.shape[0]!==i)throw new Error(`solve: incompatible shapes (${h},${p}) and (${e.shape[0]},)`);return oe(t,e)}if(e.ndim===2){if(e.shape[0]!==i)throw new Error(`solve: incompatible shapes (${h},${p}) and (${e.shape[0]},${e.shape[1]})`);const u=e.shape[1],f=z.zeros([i,u],"float64");for(let c=0;c<u;c++){const g=z.zeros([i],"float64");for(let s=0;s<i;s++)g.set([s],Number(e.get(s,c)));const o=oe(t,g);for(let s=0;s<i;s++)f.set([s,c],Number(o.get(s)))}return f}throw new Error(`solve: b must be 1D or 2D, got ${e.ndim}D`)}function qe(t,e,h=null){if(t.ndim!==2)throw new Error(`lstsq: coefficient matrix must be 2D, got ${t.ndim}D`);const[p,i]=t.shape,{u,s:f,vt:c}=O(t),g=Math.min(p,i),o=h??Math.max(p,i)*Number.EPSILON,l=Number(f.get(0))*o;let n=0;for(let w=0;w<g;w++)Number(f.get(w))>l&&n++;const m=e.ndim===1?R.reshape(e,[e.size,1]):e,r=m.shape[1];if(m.shape[0]!==p)throw new Error(`lstsq: incompatible shapes (${p},${i}) and (${e.shape.join(",")})`);const a=z.zeros([i,r],"float64");for(let w=0;w<r;w++){const d=new Array(p).fill(0);for(let x=0;x<p;x++)for(let A=0;A<p;A++)d[x]+=Number(u.get(A,x))*Number(m.get(A,w));for(let x=0;x<i;x++){let A=0;for(let S=0;S<g;S++){const v=Number(f.get(S));v>l&&(A+=Number(c.get(S,x))*d[S]/v)}a.set([x,w],A)}}let b;if(p>i){b=z.zeros([r],"float64");for(let w=0;w<r;w++){let d=0;for(let x=0;x<p;x++){let A=0;for(let v=0;v<i;v++)A+=Number(t.get(x,v))*Number(a.get(v,w));const S=A-Number(m.get(x,w));d+=S*S}b.set([w],d)}}else b=z.zeros([0],"float64");return{x:e.ndim===1?R.reshape(a,[i]):a,residuals:b,rank:n,s:f}}function Re(t,e=2){if(t.ndim!==2)throw new Error(`cond: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(e===2||e===-2){const{s:c}=O(t),g=Math.min(h,p),o=Number(c.get(0)),s=Number(c.get(g-1));return e===2?s>0?o/s:1/0:o>0?s/o:0}if(h!==p)throw new Error(`cond: matrix must be square for p=${e}`);const i=P(t,e),u=K(t),f=P(u,e);return i*f}function _e(t,e){if(t.ndim===0)return Number(t.get())!==0?1:0;if(t.ndim===1){for(let f=0;f<t.size;f++)if(Number(t.get(f))!==0)return 1;return 0}if(t.ndim!==2)throw new Error(`matrix_rank: input must be at most 2D, got ${t.ndim}D`);const{s:h}=O(t),p=Number(h.get(0)),i=e??p*Math.max(t.shape[0],t.shape[1])*Number.EPSILON;let u=0;for(let f=0;f<h.size;f++)Number(h.get(f))>i&&u++;return u}function Be(t,e){if(t.ndim!==2)throw new Error(`matrix_power: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`matrix_power: matrix must be square, got ${h}x${p}`);const i=h;if(!Number.isInteger(e))throw new Error("matrix_power: exponent must be an integer");if(e===0){const o=z.zeros([i,i],"float64");for(let s=0;s<i;s++)o.set([s,s],1);return o}let u=t,f=e;e<0&&(u=K(t),f=-e);let c=z.zeros([i,i],"float64");for(let o=0;o<i;o++)c.set([o,o],1);let g=z.zeros([i,i],"float64");for(let o=0;o<i;o++)for(let s=0;s<i;s++)g.set([o,s],Number(u.get(o,s)));for(;f>0;)f&1&&(c=B(c,g)),g=B(g,g),f>>=1;return c}function Te(t,e=1e-15){if(t.ndim!==2)throw new Error(`pinv: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape,{u:i,s:u,vt:f}=O(t),c=Math.min(h,p),o=Number(u.get(0))*e,s=z.zeros([p,h],"float64");for(let l=0;l<p;l++)for(let n=0;n<h;n++){let m=0;for(let r=0;r<c;r++){const a=Number(u.get(r));a>o&&(m+=Number(f.get(r,l))*Number(i.get(n,r))/a)}s.set([l,n],m)}return s}function ye(t){if(t.ndim!==2)throw new Error(`eig: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`eig: matrix must be square, got ${e}x${h}`);const p=e;let i=!0;e:for(let s=0;s<p;s++)for(let l=s+1;l<p;l++)if(Math.abs(Number(t.get(s,l))-Number(t.get(l,s)))>1e-10){i=!1;break e}if(i){const{values:s,vectors:l}=Z(t),n=z.zeros([p],"float64"),m=z.zeros([p,p],"float64");for(let r=0;r<p;r++){n.set([r],s[r]);for(let a=0;a<p;a++)m.set([a,r],l[a][r])}return{w:n,v:m}}const{values:u,vectors:f,hasComplexEigenvalues:c}=we(t);c&&console.warn("numpy-ts: eig() detected complex eigenvalues which cannot be represented. Results are real approximations and may be inaccurate. For symmetric matrices, use eigh() instead.");const g=z.zeros([p],"float64"),o=z.zeros([p,p],"float64");for(let s=0;s<p;s++){g.set([s],u[s]);for(let l=0;l<p;l++)o.set([l,s],f[l][s])}return{w:g,v:o}}function we(t){const e=t.shape[0],h=1e3,p=1e-10;let i=z.zeros([e,e],"float64");for(let o=0;o<e;o++)for(let s=0;s<e;s++)i.set([o,s],Number(t.get(o,s)));let u=z.zeros([e,e],"float64");for(let o=0;o<e;o++)u.set([o,o],1);for(let o=0;o<h;o++){let s=0;for(let r=0;r<e;r++)for(let a=0;a<e;a++)r!==a&&(s+=Number(i.get(r,a))**2);if(Math.sqrt(s)<p*e)break;const l=ge(i,"reduced"),n=l.q,m=l.r;i=B(m,n),u=B(u,n)}let f=!1;for(let o=0;o<e-1;o++){const s=Math.abs(Number(i.get(o+1,o))),l=Math.abs(Number(i.get(o,o))),n=Math.abs(Number(i.get(o+1,o+1))),m=Math.max(l,n,1e-10);if(s/m>1e-6){f=!0;break}}const c=[];for(let o=0;o<e;o++)c.push(Number(i.get(o,o)));const g=[];for(let o=0;o<e;o++){g.push([]);for(let s=0;s<e;s++)g[o].push(Number(u.get(o,s)))}return{values:c,vectors:g,hasComplexEigenvalues:f}}function Ae(t,e="L"){if(t.ndim!==2)throw new Error(`eigh: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`eigh: matrix must be square, got ${h}x${p}`);const i=h,u=z.zeros([i,i],"float64");for(let l=0;l<i;l++)for(let n=0;n<i;n++)e==="L"?l>=n&&(u.set([l,n],Number(t.get(l,n))),u.set([n,l],Number(t.get(l,n)))):n>=l&&(u.set([l,n],Number(t.get(l,n))),u.set([n,l],Number(t.get(l,n))));const{values:f,vectors:c}=Z(u),g=Array.from({length:i},(l,n)=>n);g.sort((l,n)=>f[l]-f[n]);const o=z.zeros([i],"float64"),s=z.zeros([i,i],"float64");for(let l=0;l<i;l++){o.set([l],f[g[l]]);for(let n=0;n<i;n++)s.set([n,l],c[n][g[l]])}return{w:o,v:s}}function Oe(t){const{w:e}=ye(t);return e}function Le(t,e="L"){const{w:h}=Ae(t,e);return h}function Pe(t,e){const h=R.flatten(t),p=R.flatten(e),i=h.shape[0],u=p.shape[0];if(i!==u)throw new Error(`vdot: arrays must have same number of elements, got ${i} and ${u}`);if(k(t.dtype)||k(e.dtype)){let g=0,o=0;for(let s=0;s<i;s++){const l=h.get(s),n=p.get(s),m=l instanceof I?l.re:Number(l),r=l instanceof I?-l.im:0,a=n instanceof I?n.re:Number(n),b=n instanceof I?n.im:0;g+=m*a+r*b,o+=m*b-r*a}return Math.abs(o)<1e-15?g:new I(g,o)}let c=0;for(let g=0;g<i;g++){const o=h.get(g),s=p.get(g);typeof o=="bigint"&&typeof s=="bigint"?c=(typeof c=="bigint"?c:BigInt(c))+o*s:c=(typeof c=="bigint"?Number(c):c)+Number(o)*Number(s)}return c}function Je(t,e,h=-1){const p=t.ndim,i=e.ndim,u=h<0?p+h:h,f=h<0?i+h:h;if(u<0||u>=p)throw new Error(`vecdot: axis ${h} out of bounds for array with ${p} dimensions`);if(f<0||f>=i)throw new Error(`vecdot: axis ${h} out of bounds for array with ${i} dimensions`);const c=t.shape[u],g=e.shape[f];if(c!==g)throw new Error(`vecdot: axis dimensions must match, got ${c} and ${g}`);if(p===1&&i===1)return X(t,e);const o=[...t.shape.slice(0,u),...t.shape.slice(u+1)],s=[...e.shape.slice(0,f),...e.shape.slice(f+1)],l=c,n=k(t.dtype)||k(e.dtype),m=V(t.dtype,e.dtype),r=o.length>s.length?o:s;if(r.length===0){let y=n?new I(0,0):0;for(let w=0;w<l;w++){const d=t.get(w),x=e.get(w),A=T(d,x);if(y instanceof I||A instanceof I){const S=y instanceof I?y:new I(Number(y),0),v=A instanceof I?A:new I(Number(A),0);y=S.add(v)}else typeof y=="bigint"||typeof A=="bigint"?y=BigInt(y)+BigInt(A):y=y+A}return y}const a=z.zeros(r,m),b=r.reduce((y,w)=>y*w,1);for(let y=0;y<b;y++){const w=[];let d=y;for(let v=r.length-1;v>=0;v--)w.unshift(d%r[v]),d=Math.floor(d/r[v]);const x=[...w.slice(0,u),0,...w.slice(u)],A=[...w.slice(0,f),0,...w.slice(f)];let S=n?new I(0,0):0;for(let v=0;v<l;v++){x[u]=v,A[f]=v;const N=t.get(...x),M=e.get(...A),$=T(N,M);if(S instanceof I||$ instanceof I){const D=S instanceof I?S:new I(Number(S),0),C=$ instanceof I?$:new I(Number($),0);S=D.add(C)}else typeof S=="bigint"||typeof $=="bigint"?S=BigInt(S)+BigInt($):S=S+$}a.set(w,S)}return a}function Xe(t){if(t.ndim<2)throw new Error(`matrix_transpose: input must have at least 2 dimensions, got ${t.ndim}D`);const e=Array.from({length:t.ndim},(p,i)=>i),h=e.length-1;return e[h]=h-1,e[h-1]=h,L(t,e)}function We(t,e){return L(t,e)}function Qe(t,e){if(t.ndim<2)throw new Error(`matvec: x1 must have at least 2 dimensions, got ${t.ndim}D`);if(e.ndim<1)throw new Error(`matvec: x2 must have at least 1 dimension, got ${e.ndim}D`);const h=t.shape[t.ndim-2],p=t.shape[t.ndim-1],i=e.shape[e.ndim-1];if(p!==i)throw new Error(`matvec: last axis of x1 (${p}) must match last axis of x2 (${i})`);if(t.ndim===2&&e.ndim===1)return X(t,e);const u=t.shape.slice(0,-2),f=e.shape.slice(0,-1),c=Math.max(u.length,f.length),g=[...Array(c-u.length).fill(1),...u],o=[...Array(c-f.length).fill(1),...f],s=[];for(let b=0;b<c;b++){const y=g[b],w=o[b];if(y!==1&&w!==1&&y!==w)throw new Error(`matvec: batch dimensions not broadcastable: ${u} vs ${f}`);s.push(Math.max(y,w))}const l=[...s,h],n=V(t.dtype,e.dtype),m=z.zeros(l,n),r=k(n),a=s.reduce((b,y)=>b*y,1);for(let b=0;b<a;b++){const y=[];let w=b;for(let A=s.length-1;A>=0;A--)y.unshift(w%s[A]),w=Math.floor(w/s[A]);const d=y.slice(-(u.length||1)).map((A,S)=>(u[S]??1)===1?0:A),x=y.slice(-(f.length||1)).map((A,S)=>(f[S]??1)===1?0:A);for(let A=0;A<h;A++){let S=r?new I(0,0):0;for(let N=0;N<p;N++){const M=[...d,A,N],$=[...x,N],D=t.get(...M),C=e.get(...$),E=T(D,C);if(S instanceof I||E instanceof I){const j=S instanceof I?S:new I(Number(S),0),F=E instanceof I?E:new I(Number(E),0);S=j.add(F)}else typeof S=="bigint"||typeof E=="bigint"?S=BigInt(S)+BigInt(E):S=S+E}const v=[...y,A];m.set(v,S)}}return m}function Ue(t,e){if(t.ndim<1)throw new Error(`vecmat: x1 must have at least 1 dimension, got ${t.ndim}D`);if(e.ndim<2)throw new Error(`vecmat: x2 must have at least 2 dimensions, got ${e.ndim}D`);const h=t.shape[t.ndim-1],p=e.shape[e.ndim-2],i=e.shape[e.ndim-1];if(h!==p)throw new Error(`vecmat: last axis of x1 (${h}) must match second-to-last axis of x2 (${p})`);if(t.ndim===1&&e.ndim===2)return X(t,e);const u=t.shape.slice(0,-1),f=e.shape.slice(0,-2),c=Math.max(u.length,f.length),g=[...Array(c-u.length).fill(1),...u],o=[...Array(c-f.length).fill(1),...f],s=[];for(let b=0;b<c;b++){const y=g[b],w=o[b];if(y!==1&&w!==1&&y!==w)throw new Error(`vecmat: batch dimensions not broadcastable: ${u} vs ${f}`);s.push(Math.max(y,w))}const l=[...s,i],n=V(t.dtype,e.dtype),m=z.zeros(l,n),r=k(n),a=s.reduce((b,y)=>b*y,1);for(let b=0;b<a;b++){const y=[];let w=b;for(let A=s.length-1;A>=0;A--)y.unshift(w%s[A]),w=Math.floor(w/s[A]);const d=y.slice(-(u.length||1)).map((A,S)=>(u[S]??1)===1?0:A),x=y.slice(-(f.length||1)).map((A,S)=>(f[S]??1)===1?0:A);for(let A=0;A<i;A++){let S=r?new I(0,0):0;for(let N=0;N<h;N++){const M=[...d,N],$=[...x,N,A],D=t.get(...M),C=e.get(...$),E=T(D,C);if(S instanceof I||E instanceof I){const j=S instanceof I?S:new I(Number(S),0),F=E instanceof I?E:new I(Number(E),0);S=j.add(F)}else typeof S=="bigint"||typeof E=="bigint"?S=BigInt(S)+BigInt(E):S=S+E}const v=[...y,A];m.set(v,S)}}return m}function Ge(t){if(t.ndim!==2)throw new Error(`slogdet: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`slogdet: matrix must be square, got ${e}x${h}`);const p=e;if(p===0)return{sign:1,logabsdet:0};const{lu:i,sign:u}=Q(t),f=i.data;let c=0,g=u;for(let o=0;o<p;o++){const s=f[o*p+o];if(s===0)return{sign:0,logabsdet:-1/0};s<0&&(g=-g),c+=Math.log(Math.abs(s))}return{sign:g,logabsdet:c}}function He(t){return be(t,!0,!1)}function Ye(t){if(t.length<2)throw new Error("multi_dot: need at least 2 arrays");if(t.length===2)return B(t[0],t[1]);let e=t[0];for(let h=1;h<t.length;h++)e=B(e,t[h]);return e}function Ze(t,e=2){if(e<=0)throw new Error(`tensorinv: ind must be positive, got ${e}`);const h=t.shape,p=t.ndim;if(p<e)throw new Error(`tensorinv: array has ${p} dimensions, ind=${e} is too large`);let i=1;for(let o=0;o<e;o++)i*=h[o];let u=1;for(let o=e;o<p;o++)u*=h[o];if(i!==u)throw new Error(`tensorinv: product of first ${e} dimensions (${i}) must equal product of remaining dimensions (${u})`);const f=R.reshape(t,[i,u]),c=K(f),g=[...h.slice(e),...h.slice(0,e)];return R.reshape(c,g)}function Ke(t,e,h){const p=t.shape,i=e.shape,u=t.ndim,f=e.ndim;let c;h==null?c=Array.from({length:f},(d,x)=>u-f+x):c=h.map(d=>d<0?u+d:d);const g=[];for(let d=0;d<u;d++)c.includes(d)||g.push(d);const o=[...g,...c],s=L(t,o),n=c.map(d=>p[d]).reduce((d,x)=>d*x,1),r=g.map(d=>p[d]).reduce((d,x)=>d*x,1),a=i.reduce((d,x)=>d*x,1);if(n!==a)throw new Error(`tensorsolve: dimensions don't match - sum dimensions product (${n}) != b total elements (${a})`);if(r!==n)throw new Error(`tensorsolve: non-square problem - other dimensions product (${r}) != sum dimensions product (${n})`);const b=R.reshape(s,[r,n]),y=R.reshape(e,[n]),w=de(b,y);return R.reshape(w,[...i])}function et(t,...e){const h=t.indexOf("->");let p,i;h===-1?(p=t,i=ne(p)):(p=t.slice(0,h),i=t.slice(h+2));const u=p.split(",").map(n=>n.trim());if(u.length!==e.length)throw new Error(`einsum_path: expected ${u.length} operands, got ${e.length}`);const f=e.map(n=>Array.isArray(n)?n:Array.from(n.shape)),c=new Map;for(let n=0;n<e.length;n++){const m=u[n],r=f[n];if(m.length!==r.length)throw new Error(`einsum_path: operand ${n} has ${r.length} dimensions but subscript '${m}' has ${m.length} indices`);for(let a=0;a<m.length;a++){const b=m[a],y=r[a];if(c.has(b)&&c.get(b)!==y)throw new Error(`einsum_path: size mismatch for index '${b}': ${c.get(b)} vs ${y}`);c.set(b,y)}}if(e.length===1){const n=[[0]];return[n,ee(t,f,n,c)]}if(e.length===2){const n=[[0,1]];return[n,ee(t,f,n,c)]}const g=[],o=[...u],s=[...f],l=e.map((n,m)=>m);for(;o.length>1;){let n=0,m=1,r=1/0;for(let y=0;y<o.length;y++)for(let w=y+1;w<o.length;w++){const d=xe(o[y],o[w],s[y],s[w],i,c);d<r&&(r=d,n=y,m=w)}g.push([l[n],l[m]]);const[a,b]=Se(o[n],o[m],s[n],s[m],i,c);o.splice(m,1),o.splice(n,1),s.splice(m,1),s.splice(n,1),l.splice(m,1),l.splice(n,1),o.push(a),s.push(b),l.push(-1)}return[g,ee(t,f,g,c)]}function xe(t,e,h,p,i,u){const f=new Set(t),c=new Set(e);let g=1;for(const o of f)g*=u.get(o)||1;for(const o of c)f.has(o)||(g*=u.get(o)||1);return g}function Se(t,e,h,p,i,u){const f=new Set([...t,...e]),c=new Map;for(const l of t)c.set(l,(c.get(l)||0)+1);for(const l of e)c.set(l,(c.get(l)||0)+1);const g=new Set(i),o=[];for(const l of f)(g.has(l)||c.get(l)===1)&&o.push(l);o.sort();const s=o.map(l=>u.get(l));return[o.join(""),s]}function ee(t,e,h,p){const i=[];i.push(" Complete contraction: "+t),i.push(" Operand shapes: "+e.map(f=>`(${f.join(", ")})`).join(", ")),i.push(" Contraction path: "+JSON.stringify(h));let u=0;for(const f of e)u+=f.reduce((c,g)=>c*g,1);return i.push(" Estimated FLOPS: ~"+u.toExponential(2)),i.join(`
|
|
2
|
+
`)}export{ke as cholesky,Re as cond,je as cross,Fe as det,ze as diagonal,X as dot,ye as eig,Ae as eigh,Oe as eigvals,Le as eigvalsh,Ee as einsum,et as einsum_path,Me as inner,K as inv,Ce as kron,qe as lstsq,B as matmul,P as matrix_norm,Be as matrix_power,_e as matrix_rank,Xe as matrix_transpose,Qe as matvec,Ye as multi_dot,Ve as norm,pe as outer,We as permute_dims,Te as pinv,ge as qr,Ge as slogdet,de as solve,be as svd,He as svdvals,De as tensordot,Ze as tensorinv,Ke as tensorsolve,$e as trace,L as transpose,Pe as vdot,Je as vecdot,Ue as vecmat,W as vector_norm};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as t from"../common/ops/formatting";import{toStorage as o}from"./types";const l=t.set_printoptions,m=t.get_printoptions,
|
|
1
|
+
import*as t from"../common/ops/formatting";import{toStorage as o}from"./types";const l=t.set_printoptions,m=t.get_printoptions,u=t.printoptions,_=t.format_float_positional,f=t.format_float_scientific,x=t.base_repr,c=t.binary_repr;function g(e,r){return t.array2string(o(e),r?.max_line_width??null,r?.precision??null,r?.suppress_small??null,r?.separator??" ",r?.prefix??"",r?.suffix??"",r?.threshold??null,r?.edgeitems??null,r?.floatmode??null,r?.sign??null)}function b(e,r,n,a){return t.array_repr(o(e),r,n,a)}function y(e,r,n){return t.array_str(o(e),r,n)}export{g as array2string,b as array_repr,y as array_str,x as base_repr,c as binary_repr,_ as format_float_positional,f as format_float_scientific,m as get_printoptions,u as printoptions,l as set_printoptions};
|