numpy-ts 0.5.0 → 0.6.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 CHANGED
@@ -3,22 +3,22 @@
3
3
  ![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
4
4
  [![npm version](https://img.shields.io/npm/v/numpy-ts)](https://www.npmjs.com/package/numpy-ts)
5
5
  ![bundle size](https://img.shields.io/bundlephobia/minzip/numpy-ts)
6
- ![Under Construction](https://img.shields.io/badge/Under%20Construction-red)
6
+ ![numpy api coverage](https://img.shields.io/badge/numpy_api_coverage-58%20%25-yellow)
7
7
 
8
8
  ```
9
- _
10
- _ __ _ _ _ __ ___ _ __ _ _ ____| |_ ___
11
- | '_ \| | | | '_ ` _ \| '_ \| | | |____| __/ __)
12
- | | | | |_| | | | | | | |_) | |_| | | |_\__ \
13
- |_| |_|\__,_|_| |_| |_| .__/ \__, | \__(___/
14
- |_| (___/
9
+ ███╗ ██╗██╗ ██╗███╗ ███╗██████╗ ██╗ ██╗ ████████╗███████╗
10
+ ████╗ ██║██║ ██║████╗ ████║██╔══██╗╚██╗ ██╔╝ ╚══██╔══╝██╔════╝
11
+ ██╔██╗ ██║██║ ██║██╔████╔██║██████╔╝ ╚████╔╝█████╗██║ ███████╗
12
+ ██║╚██╗██║██║ ██║██║╚██╔╝██║██╔═══╝ ╚██╔╝ ╚════╝██║ ╚════██║
13
+ ██║ ╚████║╚██████╔╝██║ ╚═╝ ██║██║ ██║ ██║ ███████║
14
+ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚══════╝
15
15
  ```
16
16
 
17
17
  Complete NumPy implementation for TypeScript and JavaScript
18
18
 
19
- > **⚠️ Under active development** — API may change before v1.0
19
+ **⚠️ Under active development** — API may change before v1.0
20
20
 
21
- A faithful NumPy 2.0+ implementation for TypeScript/JavaScript, validated against Python NumPy. **128 of 333 NumPy functions (38.4% complete)** covering array creation, manipulation, linear algebra, reductions, and more.
21
+ A faithful NumPy 2.0+ implementation for TypeScript/JavaScript, validated against Python NumPy. **192 of 333 NumPy functions (57.7% complete)** covering array creation, manipulation, linear algebra, reductions, and more.
22
22
 
23
23
  ```bash
24
24
  npm install numpy-ts
@@ -64,15 +64,15 @@ Progress toward complete NumPy API compatibility:
64
64
 
65
65
  | Category | Complete | Total | Status |
66
66
  |----------|----------|-------|--------|
67
+ | **Arithmetic** | 19/19 | 100% | ✅ |
68
+ | **Array Creation** | 32/32 | 100% | ✅ |
69
+ | **Array Manipulation** | 35/35 | 100% | ✅ |
70
+ | **Broadcasting** | 3/3 | 100% | ✅ |
71
+ | **Comparison** | 10/10 | 100% | ✅ |
67
72
  | **Hyperbolic** | 6/6 | 100% | ✅ |
68
- | **Comparison** | 9/10 | 90% | 🟡 |
69
- | **Trigonometric** | 10/12 | 83% | 🟡 |
70
- | **Arithmetic** | 13/19 | 68% | 🟡 |
71
- | **Broadcasting** | 2/3 | 67% | 🟡 |
72
- | **Linear Algebra** | 6/9 | 67% | 🟡 |
73
- | **Array Creation** | 17/32 | 53% | 🟡 |
74
- | **Array Manipulation** | 18/35 | 51% | 🟡 |
75
- | **Reductions** | 11/30 | 37% | 🔴 |
73
+ | **Linear Algebra** | 9/9 | 100% | |
74
+ | **Reductions** | 30/30 | 100% | |
75
+ | **Trigonometric** | 12/12 | 100% | |
76
76
  | **Indexing** | 3/20 | 15% | 🔴 |
77
77
  | **Bit Operations** | 0/9 | 0% | 🔴 |
78
78
  | **Exponential** | 0/9 | 0% | 🔴 |
@@ -89,7 +89,7 @@ Progress toward complete NumPy API compatibility:
89
89
  | **Sorting** | 0/6 | 0% | 🔴 |
90
90
  | **Statistics** | 0/9 | 0% | 🔴 |
91
91
 
92
- **Overall: 128/333 functions (38.4% complete)**
92
+ **Overall: 192/333 functions (57.7% complete)**
93
93
 
94
94
  See the complete [API Reference](docs/API-REFERENCE.md) for detailed function list.
95
95
 
@@ -144,20 +144,20 @@ console.log(arr.flags.C_CONTIGUOUS); // true - row-major layout
144
144
  ## Architecture
145
145
 
146
146
  ```
147
- ┌───────────────────────────────────┐
148
- │ NumPy-Compatible API
149
- │ Broadcasting, DType Promotion
150
- └─────────────────┬─────────────────┘
151
-
152
- ┌─────────────────┴─────────────────┐
153
- │ NDArray (Views & Memory Mgmt)
154
- │ Strided Arrays, Base Tracking
155
- └─────────────────┬─────────────────┘
156
- │- - - - - - - - - - - - - - - - - - -
157
- ┌─────────────────┴─────────────────┐ ┌ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┐
158
- │ TypeScript / JavaScript Core │ │ WASM Compute Engine (Future)
159
- │ Computational Engine │ │ Optimized BLAS / arithmetic
160
- └───────────────────────────────────┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
147
+ ┌─────────────────────────────────┐
148
+ │ NumPy-Compatible API
149
+ │ Broadcasting, DType Promotion
150
+ └───────────────┬─────────────────┘
151
+
152
+ ┌───────────────┴─────────────────┐
153
+ │ NDArray (Views & Memory Mgmt)
154
+ │ Strided Arrays, Base Tracking
155
+ └───────────────┬─────────────────┘
156
+ │- - - - - - - - - - - - - - - - - - ┐
157
+ ┌───────────────┴─────────────────┐ ┌ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─┐
158
+ │ TypeScript / JavaScript Core │ │ WASM Compute Engine (Future)
159
+ │ Computational Engine │ │ Optimized BLAS / arithmetic
160
+ └─────────────────────────────────┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘
161
161
  ```
162
162
 
163
163
  Pure TypeScript implementation built from scratch for correctness and NumPy compatibility.
@@ -239,7 +239,7 @@ arr.sum(); // Type: number
239
239
 
240
240
  | Feature | numpy-ts | numjs | ndarray | TensorFlow.js |
241
241
  |---------|----------|-------|---------|---------------|
242
- | NumPy API Coverage | 128/333 (38%) | ~20% | Different | ML-focused |
242
+ | NumPy API Coverage | 192/333 (58%) | ~20% | Different | ML-focused |
243
243
  | TypeScript Native | ✅ Full | Partial | ❌ No | ✅ Yes |
244
244
  | NumPy Validated | ✅ 1365+ tests | Mostly | ❌ No | ❌ No |
245
245
  | .npy/.npz Files | ✅ v1/v2/v3 | ❌ No | ❌ No | ❌ No |
@@ -1,2 +1,2 @@
1
- "use strict";var np=(()=>{var fr=Object.defineProperty;var bn=Object.getOwnPropertyDescriptor;var Sn=Object.getOwnPropertyNames;var Dn=Object.prototype.hasOwnProperty;var wn=(r,t)=>{for(var e in t)fr(r,e,{get:t[e],enumerable:!0})},Nn=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Sn(t))!Dn.call(r,o)&&o!==e&&fr(r,o,{get:()=>t[o],enumerable:!(n=bn(t,o))||n.enumerable});return r};var xn=r=>Nn(fr({},"__esModule",{value:!0}),r);var So={};wn(So,{DTYPE_TO_DESCR:()=>Q,InvalidNpyError:()=>C,NDArray:()=>z,SUPPORTED_DTYPES:()=>sr,UnsupportedDTypeError:()=>v,__version__:()=>bo,absolute:()=>fe,arange:()=>Qt,arccos:()=>Ue,arccosh:()=>Ve,arcsin:()=>Be,arcsinh:()=>ve,arctan:()=>_e,arctan2:()=>Ee,arctanh:()=>Pe,array:()=>V,array_equal:()=>on,array_split:()=>We,asarray:()=>ne,broadcast_arrays:()=>rn,broadcast_to:()=>Ke,choose:()=>nn,concatenate:()=>Le,copy:()=>oe,cos:()=>Ie,cosh:()=>Ce,degrees:()=>Oe,dot:()=>Ae,dstack:()=>He,empty:()=>Ur,empty_like:()=>ie,eye:()=>Br,floor_divide:()=>de,full:()=>_r,full_like:()=>ue,geomspace:()=>te,hsplit:()=>Je,hstack:()=>Ze,hypot:()=>Te,identity:()=>ee,inner:()=>De,linspace:()=>Kt,loadNpz:()=>Mr,loadNpzSync:()=>Cr,logspace:()=>re,mod:()=>me,moveaxis:()=>Re,negative:()=>pe,ones:()=>zr,ones_like:()=>se,outer:()=>we,parseNpy:()=>K,parseNpyData:()=>ur,parseNpyHeader:()=>ir,parseNpz:()=>cr,parseNpzSync:()=>lr,positive:()=>ge,power:()=>le,put:()=>en,radians:()=>$e,reciprocal:()=>he,repeat:()=>Qe,serializeNpy:()=>q,serializeNpz:()=>kr,serializeNpzSync:()=>vr,sign:()=>ye,sin:()=>xe,sinh:()=>Me,split:()=>Ye,sqrt:()=>ce,stack:()=>je,swapaxes:()=>Fe,take:()=>tn,tan:()=>ze,tanh:()=>ke,tensordot:()=>Ne,tile:()=>Xe,trace:()=>be,transpose:()=>Se,vsplit:()=>Ge,vstack:()=>qe,zeros:()=>J,zeros_like:()=>ae});function Pr(r){if(!r.includes(":")){if(r.includes("."))throw new Error(`Invalid slice index: "${r}" (must be integer)`);let a=parseInt(r,10);if(isNaN(a))throw new Error(`Invalid slice index: "${r}"`);return{start:a,stop:null,step:1,isIndex:!0}}let t=r.split(":");if(t.length>3)throw new Error(`Invalid slice notation: "${r}" (too many colons)`);let e=t[0]===""?null:parseInt(t[0],10),n=t[1]===""||t[1]===void 0?null:parseInt(t[1],10),o=t[2]===""||t[2]===void 0?1:parseInt(t[2],10);if(e!==null&&isNaN(e))throw new Error(`Invalid start index in slice: "${r}"`);if(n!==null&&isNaN(n))throw new Error(`Invalid stop index in slice: "${r}"`);if(isNaN(o))throw new Error(`Invalid step in slice: "${r}"`);if(o===0)throw new Error("Slice step cannot be zero");return{start:e,stop:n,step:o,isIndex:!1}}function Fr(r,t){let{start:e,stop:n}=r,{step:o,isIndex:a}=r;if(a){if(e===null)throw new Error("Index cannot be null");let c=e<0?t+e:e;if(c<0||c>=t)throw new Error(`Index ${e} is out of bounds for size ${t}`);return{start:c,stop:c+1,step:1,isIndex:!0}}return o>0?(e===null&&(e=0),n===null&&(n=t)):(e===null&&(e=t-1),n===null&&(n=-t-1)),e<0&&(e=t+e),n<0&&(n=t+n),e=Math.max(0,Math.min(e,t)),n=Math.max(-1,Math.min(n,t)),{start:e,stop:n,step:o,isIndex:!1}}var M="float64";function B(r){switch(r){case"float64":return Float64Array;case"float32":return Float32Array;case"int64":return BigInt64Array;case"int32":return Int32Array;case"int16":return Int16Array;case"int8":return Int8Array;case"uint64":return BigUint64Array;case"uint32":return Uint32Array;case"uint16":return Uint16Array;case"uint8":return Uint8Array;case"bool":return Uint8Array;default:throw new Error(`Unknown dtype: ${r}`)}}function Lr(r){switch(r){case"float64":case"int64":case"uint64":return 8;case"float32":case"int32":case"uint32":return 4;case"int16":case"uint16":return 2;case"int8":case"uint8":case"bool":return 1;default:throw new Error(`Unknown dtype: ${r}`)}}function Rr(r){return r==="float64"||r==="float32"}function w(r){return r==="int64"||r==="uint64"}function O(r,t){if(r===t)return r;if(r==="bool")return t;if(t==="bool")return r;if(Rr(r)||Rr(t)){if(r==="float64"||t==="float64")return"float64";if(r==="float32"){let i=t;return i==="int32"||i==="int64"||i==="uint32"||i==="uint64"?"float64":"float32"}if(t==="float32"){let i=r;return i==="int32"||i==="int64"||i==="uint32"||i==="uint64"?"float64":"float32"}return"float32"}let e=r.startsWith("int"),n=t.startsWith("int"),o=r.startsWith("uint"),a=t.startsWith("uint"),c=i=>i.includes("64")?64:i.includes("32")?32:i.includes("16")?16:i.includes("8")?8:0,u=c(r),s=c(t);if(r==="int64"&&t==="uint64"||r==="uint64"&&t==="int64")return"float64";if(e&&a&&u===s){if(u===8)return"int16";if(u===16)return"int32";if(u===32)return"int64"}if(o&&n&&u===s){if(s===8)return"int16";if(s===16)return"int32";if(s===32)return"int64"}if(e&&n||o&&a){let i=Math.max(u,s);return e?i===64?"int64":i===32?"int32":i===16?"int16":"int8":i===64?"uint64":i===32?"uint32":i===16?"uint16":"uint8"}return e&&a?u>s?r:s===8?"int16":s===16?"int32":s===32?"int64":"float64":o&&n?s>u?t:u===8?"int16":u===16?"int32":u===32?"int64":"float64":"float64"}var D=class r{constructor(t,e,n,o,a){this._data=t,this._shape=e,this._strides=n,this._offset=o,this._dtype=a}get shape(){return this._shape}get ndim(){return this._shape.length}get size(){return this._shape.reduce((t,e)=>t*e,1)}get dtype(){return this._dtype}get data(){return this._data}get strides(){return this._strides}get offset(){return this._offset}get isCContiguous(){let t=this._shape,e=this._strides,n=t.length;if(n===0)return!0;if(n===1)return e[0]===1;let o=1;for(let a=n-1;a>=0;a--){if(e[a]!==o)return!1;o*=t[a]}return!0}get isFContiguous(){let t=this._shape,e=this._strides,n=t.length;if(n===0)return!0;if(n===1)return e[0]===1;let o=1;for(let a=0;a<n;a++){if(e[a]!==o)return!1;o*=t[a]}return!0}iget(t){let e=this._shape,n=this._strides,o=e.length;if(o===0)return this._data[this._offset];let a=t,c=this._offset;for(let u=0;u<o;u++){let s=1;for(let l=u+1;l<o;l++)s*=e[l];let i=Math.floor(a/s);a=a%s,c+=i*n[u]}return this._data[c]}iset(t,e){let n=this._shape,o=this._strides,a=n.length;if(a===0){this._data[this._offset]=e;return}let c=t,u=this._offset;for(let s=0;s<a;s++){let i=1;for(let f=s+1;f<a;f++)i*=n[f];let l=Math.floor(c/i);c=c%i,u+=l*o[s]}this._data[u]=e}get(...t){let e=this._strides,n=this._offset;for(let o=0;o<t.length;o++)n+=t[o]*e[o];return this._data[n]}set(t,e){let n=this._strides,o=this._offset;for(let a=0;a<t.length;a++)o+=t[a]*n[a];this._data[o]=e}copy(){let t=Array.from(this._shape),e=this._dtype,n=this.size,o=B(e);if(!o)throw new Error(`Cannot copy array with dtype ${e}`);let a=new o(n);if(this.isCContiguous&&this._offset===0)if(w(e)){let c=this._data,u=a;for(let s=0;s<n;s++)u[s]=c[s]}else a.set(this._data);else if(w(e)){let c=a;for(let u=0;u<n;u++)c[u]=this.iget(u)}else for(let c=0;c<n;c++)a[c]=this.iget(c);return new r(a,t,r._computeStrides(t),0,e)}static fromData(t,e,n,o,a){let c=o??r._computeStrides(e),u=a??0;return new r(t,e,c,u,n)}static zeros(t,e=M){let n=t.reduce((c,u)=>c*u,1),o=B(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return new r(a,t,r._computeStrides(t),0,e)}static ones(t,e=M){let n=t.reduce((c,u)=>c*u,1),o=B(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return w(e)?a.fill(BigInt(1)):a.fill(1),new r(a,t,r._computeStrides(t),0,e)}static _computeStrides(t){let e=new Array(t.length),n=1;for(let o=t.length-1;o>=0;o--)e[o]=n,n*=t[o];return e}};function P(r){let t=new Array(r.length),e=1;for(let n=r.length-1;n>=0;n--)t[n]=e,e*=r[n];return t}function jr(r,t){let e=r.length,n=t.length,o=Math.max(e,n),a=new Array(o);for(let c=0;c<o;c++){let u=c<o-e?1:r[c-(o-e)],s=c<o-n?1:t[c-(o-n)];if(u===s)a[c]=u;else if(u===1)a[c]=s;else if(s===1)a[c]=u;else throw new Error(`operands could not be broadcast together with shapes ${JSON.stringify(Array.from(r))} ${JSON.stringify(Array.from(t))}`)}return a}function In(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let c=0;c<n;c++){let u=o-n+c,s=r[c],i=e[u];if(s===i)a[u]=t[c];else if(s===1)a[u]=0;else throw new Error("Invalid broadcast")}return a}function er(r,t){let e=In(r.shape,r.strides,t);return D.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function k(r,t,e,n){let o=jr(r.shape,t.shape),a=er(r,o),c=er(t,o),u=O(r.dtype,t.dtype),s=D.zeros(o,u),i=s.data,l=s.size;if(w(u)){let f=i;for(let p=0;p<l;p++){let m=a.iget(p),y=c.iget(p),d=typeof m=="bigint"?m:BigInt(Math.round(m)),A=typeof y=="bigint"?y:BigInt(Math.round(y));n==="add"?f[p]=d+A:n==="subtract"?f[p]=d-A:n==="multiply"?f[p]=d*A:n==="divide"?f[p]=d/A:f[p]=BigInt(Math.round(e(Number(d),Number(A))))}}else{let f=w(r.dtype)||w(t.dtype);for(let p=0;p<l;p++){let m=a.iget(p),y=c.iget(p),d=Number(m),A=Number(y);i[p]=e(d,A)}}return s}function F(r,t,e){let n=jr(r.shape,t.shape),o=er(r,n),a=er(t,n),c=n.reduce((i,l)=>i*l,1),u=new Uint8Array(c),s=w(r.dtype)||w(t.dtype);for(let i=0;i<c;i++){let l=o.iget(i),f=a.iget(i),p=Number(l),m=Number(f);u[i]=e(p,m)?1:0}return D.fromData(u,n,"bool")}function $(r,t,e=!0){let n=r.dtype,o=Array.from(r.shape),a=r.size,u=e?n:n!=="float32"&&n!=="float64"?"float64":n,s=D.zeros(o,u),i=s.data,l=r.data;if(w(n))if(w(u)){let f=i;for(let p=0;p<a;p++){let m=Number(l[p]);f[p]=BigInt(Math.round(t(m)))}}else for(let f=0;f<a;f++)i[f]=t(Number(l[f]));else for(let f=0;f<a;f++)i[f]=t(Number(l[f]));return s}function pr(r,t){return r.isCContiguous&&t.isCContiguous&&r.shape.length===t.shape.length&&r.shape.every((e,n)=>e===t.shape[n])}function qr(r,t){return typeof t=="number"?_n(r,t):pr(r,t)?zn(r,t):k(r,t,(e,n)=>e+n,"add")}function zn(r,t){let e=O(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,c=t.data,u=n.data;if(w(e)){let s=u;if(!w(r.dtype)||!w(t.dtype))for(let l=0;l<o;l++){let f=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),p=typeof c[l]=="bigint"?c[l]:BigInt(Math.round(Number(c[l])));s[l]=f+p}else{let l=a,f=c;for(let p=0;p<o;p++)s[p]=l[p]+f[p]}}else if(w(r.dtype)||w(t.dtype))for(let i=0;i<o;i++){let l=typeof a[i]=="bigint"?Number(a[i]):a[i],f=typeof c[i]=="bigint"?Number(c[i]):c[i];u[i]=l+f}else for(let i=0;i<o;i++)u[i]=a[i]+c[i];return n}function Zr(r,t){return typeof t=="number"?En(r,t):pr(r,t)?Bn(r,t):k(r,t,(e,n)=>e-n,"subtract")}function Bn(r,t){let e=O(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,c=t.data,u=n.data;if(w(e)){let s=u;if(!w(r.dtype)||!w(t.dtype))for(let l=0;l<o;l++){let f=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),p=typeof c[l]=="bigint"?c[l]:BigInt(Math.round(Number(c[l])));s[l]=f-p}else{let l=a,f=c;for(let p=0;p<o;p++)s[p]=l[p]-f[p]}}else if(w(r.dtype)||w(t.dtype))for(let i=0;i<o;i++){let l=typeof a[i]=="bigint"?Number(a[i]):a[i],f=typeof c[i]=="bigint"?Number(c[i]):c[i];u[i]=l-f}else for(let i=0;i<o;i++)u[i]=a[i]-c[i];return n}function Hr(r,t){return typeof t=="number"?Tn(r,t):pr(r,t)?Un(r,t):k(r,t,(e,n)=>e*n,"multiply")}function Un(r,t){let e=O(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,c=t.data,u=n.data;if(w(e)){let s=u;if(!w(r.dtype)||!w(t.dtype))for(let l=0;l<o;l++){let f=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),p=typeof c[l]=="bigint"?c[l]:BigInt(Math.round(Number(c[l])));s[l]=f*p}else{let l=a,f=c;for(let p=0;p<o;p++)s[p]=l[p]*f[p]}}else if(w(r.dtype)||w(t.dtype))for(let i=0;i<o;i++){let l=typeof a[i]=="bigint"?Number(a[i]):a[i],f=typeof c[i]=="bigint"?Number(c[i]):c[i];u[i]=l*f}else for(let i=0;i<o;i++)u[i]=a[i]*c[i];return n}function Yr(r,t){if(typeof t=="number")return On(r,t);let e=r.dtype==="float64",n=t.dtype==="float64",o=r.dtype==="float32",a=t.dtype==="float32";if(e||n){let s=e?r:L(r,"float64"),i=n?t:L(t,"float64");return k(s,i,(l,f)=>l/f,"divide")}if(o||a){let s=o?r:L(r,"float32"),i=a?t:L(t,"float32");return k(s,i,(l,f)=>l/f,"divide")}let c=L(r,"float64"),u=L(t,"float64");return k(c,u,(s,i)=>s/i,"divide")}function L(r,t){let e=D.zeros(Array.from(r.shape),t),n=r.size,o=r.data,a=e.data;for(let c=0;c<n;c++)a[c]=Number(o[c]);return e}function _n(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]+l}else for(let s=0;s<a;s++)u[s]=Number(o[s])+t;return c}function En(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]-l}else for(let s=0;s<a;s++)u[s]=Number(o[s])-t;return c}function Tn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]*l}else for(let s=0;s<a;s++)u[s]=Number(o[s])*t;return c}function On(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,u=e!=="float32"&&e!=="float64"?"float64":e,s=D.zeros(n,u),i=s.data;if(w(e))for(let l=0;l<a;l++)i[l]=Number(o[l])/t;else for(let l=0;l<a;l++)i[l]=Number(o[l])/t;return s}function Wr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;if(w(t)){let u=n,s=c;for(let i=0;i<o;i++){let l=u[i];s[i]=l<0n?-l:l}}else for(let u=0;u<o;u++)c[u]=Math.abs(Number(n[u]));return a}function Gr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;if(w(t)){let u=n,s=c;for(let i=0;i<o;i++)s[i]=-u[i]}else for(let u=0;u<o;u++)c[u]=-Number(n[u]);return a}function Jr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;if(w(t)){let u=n,s=c;for(let i=0;i<o;i++){let l=u[i];s[i]=l>0n?1n:l<0n?-1n:0n}}else for(let u=0;u<o;u++){let s=Number(n[u]);c[u]=s>0?1:s<0?-1:0}return a}function Xr(r,t){return typeof t=="number"?$n(r,t):k(r,t,(e,n)=>(e%n+n)%n,"mod")}function $n(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++){let p=s[f];i[f]=(p%l+l)%l}}else for(let s=0;s<a;s++){let i=Number(o[s]);u[s]=(i%t+t)%t}return c}function Qr(r,t){return typeof t=="number"?Mn(r,t):k(r,t,(e,n)=>Math.floor(e/n),"floor_divide")}function Mn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]/l}else for(let s=0;s<a;s++)u[s]=Math.floor(Number(o[s])/t);return c}function Kr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;for(let u=0;u<o;u++)c[u]=n[u];return a}function rt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,c=t!=="float32"&&t!=="float64"?"float64":t,u=D.zeros(e,c),s=u.data;if(w(t))for(let i=0;i<o;i++)s[i]=1/Number(n[i]);else for(let i=0;i<o;i++)s[i]=1/Number(n[i]);return u}function tt(r,t){return typeof t=="number"?kn(r,t):F(r,t,(e,n)=>e>n)}function et(r,t){return typeof t=="number"?vn(r,t):F(r,t,(e,n)=>e>=n)}function nt(r,t){return typeof t=="number"?Vn(r,t):F(r,t,(e,n)=>e<n)}function ot(r,t){return typeof t=="number"?Pn(r,t):F(r,t,(e,n)=>e<=n)}function at(r,t){return typeof t=="number"?Fn(r,t):F(r,t,(e,n)=>e===n)}function st(r,t){return typeof t=="number"?Rn(r,t):F(r,t,(e,n)=>e!==n)}function yr(r,t,e=1e-5,n=1e-8){return typeof t=="number"?Ln(r,t,e,n):F(r,t,(o,a)=>{let c=Math.abs(o-a),u=n+e*Math.abs(a);return c<=u})}function it(r,t,e=1e-5,n=1e-8){let o=yr(r,t,e,n),a=o.data;for(let c=0;c<o.size;c++)if(a[c]===0)return!1;return!0}function kn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]>t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function vn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]>=t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Vn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]<t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Pn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]<=t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Fn(r,t){let e=new Uint8Array(r.size),n=r.data,o=r.dtype;if(w(o)){let a=BigInt(Math.round(t)),c=n;for(let u=0;u<r.size;u++)e[u]=c[u]===a?1:0}else for(let a=0;a<r.size;a++)e[a]=n[a]===t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Rn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]!==t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Ln(r,t,e,n){let o=new Uint8Array(r.size),a=r.data,c=r.dtype;if(w(c)){let u=a;for(let s=0;s<r.size;s++){let i=Number(u[s]),l=Math.abs(i-t),f=n+e*Math.abs(t);o[s]=l<=f?1:0}}else for(let u=0;u<r.size;u++){let s=Number(a[u]),i=Math.abs(s-t),l=n+e*Math.abs(t);o[u]=i<=l?1:0}return D.fromData(o,Array.from(r.shape),"bool")}function _(r,t){let e=0,n=1;for(let o=r.length-1;o>=0;o--)e+=r[o]*n,n*=t[o];return e}function E(r,t,e,n){let o=n.length,a=new Array(o),c=Array.from(n).filter((s,i)=>i!==t),u=r;for(let s=c.length-1;s>=0;s--)a[s>=t?s+1:s]=u%c[s],u=Math.floor(u/c[s]);return a[t]=e,a}function H(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0)if(w(n)){let y=u,d=BigInt(0);for(let A=0;A<c;A++)d+=y[A];return Number(d)}else{let y=0;for(let d=0;d<c;d++)y+=Number(u[d]);return y}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return H(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=BigInt(0);for(let g=0;g<p;g++){let b=E(A,s,g,o),S=_(b,o);h+=y[S]}d[A]=h}}else for(let y=0;y<m;y++){let d=0;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o);d+=Number(u[g])}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function mr(r,t,e=!1){let n=r.dtype,o=r.shape;if(t===void 0)return H(r)/r.size;let a=t;if(a<0&&(a=o.length+a),a<0||a>=o.length)throw new Error(`axis ${t} is out of bounds for array of dimension ${o.length}`);let c=H(r,t,e);if(typeof c=="number")return c/o[a];let u=o[a],s=n;(w(n)||n.startsWith("int")||n.startsWith("uint"))&&(s="float64");let i=D.zeros(Array.from(c.shape),s),l=i.data,f=c.data;if(w(n)){let p=f;for(let m=0;m<l.length;m++)l[m]=Number(p[m])/u}else for(let p=0;p<l.length;p++)l[p]=Number(f[p])/u;return i}function dr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0){if(c===0)throw new Error("max of empty array");let y=u[0];for(let d=1;d<c;d++)u[d]>y&&(y=u[d]);return Number(y)}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return dr(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=E(A,s,0,o),g=_(h,o),b=y[g];for(let S=1;S<p;S++){let x=E(A,s,S,o),N=_(x,o),T=y[N];T>b&&(b=T)}d[A]=b}}else for(let y=0;y<m;y++){let d=-1/0;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o),b=Number(u[g]);b>d&&(d=b)}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function gr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0)if(w(n)){let y=u,d=BigInt(1);for(let A=0;A<c;A++)d*=y[A];return Number(d)}else{let y=1;for(let d=0;d<c;d++)y*=Number(u[d]);return y}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return gr(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=BigInt(1);for(let g=0;g<p;g++){let b=E(A,s,g,o),S=_(b,o);h*=y[S]}d[A]=h}}else for(let y=0;y<m;y++){let d=1;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o);d*=Number(u[g])}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function hr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0){if(c===0)throw new Error("min of empty array");let y=u[0];for(let d=1;d<c;d++)u[d]<y&&(y=u[d]);return Number(y)}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return hr(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=E(A,s,0,o),g=_(h,o),b=y[g];for(let S=1;S<p;S++){let x=E(A,s,S,o),N=_(x,o),T=y[N];T<b&&(b=T)}d[A]=b}}else for(let y=0;y<m;y++){let d=1/0;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o),b=Number(u[g]);b<d&&(d=b)}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function Ar(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){if(a===0)throw new Error("argmin of empty array");let m=c[0],y=0;for(let d=1;d<a;d++)c[d]<m&&(m=c[d],y=d);return y}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return Ar(r);let i=D.zeros(s,"int32"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);if(w(e)){let m=c;for(let y=0;y<p;y++){let d=E(y,u,0,n),A=_(d,n),h=m[A],g=0;for(let b=1;b<f;b++){let S=E(y,u,b,n),x=_(S,n),N=m[x];N<h&&(h=N,g=b)}l[y]=g}}else for(let m=0;m<p;m++){let y=1/0,d=0;for(let A=0;A<f;A++){let h=E(m,u,A,n),g=_(h,n),b=Number(c[g]);b<y&&(y=b,d=A)}l[m]=d}return i}function br(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){if(a===0)throw new Error("argmax of empty array");let m=c[0],y=0;for(let d=1;d<a;d++)c[d]>m&&(m=c[d],y=d);return y}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return br(r);let i=D.zeros(s,"int32"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);if(w(e)){let m=c;for(let y=0;y<p;y++){let d=E(y,u,0,n),A=_(d,n),h=m[A],g=0;for(let b=1;b<f;b++){let S=E(y,u,b,n),x=_(S,n),N=m[x];N>h&&(h=N,g=b)}l[y]=g}}else for(let m=0;m<p;m++){let y=-1/0,d=0;for(let A=0;A<f;A++){let h=E(m,u,A,n),g=_(h,n),b=Number(c[g]);b>y&&(y=b,d=A)}l[m]=d}return i}function Sr(r,t,e=0,n=!1){let o=r.shape,a=o.length,c=r.size,u=r.data,s=mr(r,t,n);if(t===void 0){let h=s,g=0;for(let b=0;b<c;b++){let S=Number(u[b])-h;g+=S*S}return g/(c-e)}let i=t;if(i<0&&(i=a+i),i<0||i>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let l=o[i],f=s,p=f.data,m=n?f.shape:Array.from(o).filter((h,g)=>g!==i),y=D.zeros(Array.from(m),"float64"),d=y.data,A=m.reduce((h,g)=>h*g,1);for(let h=0;h<A;h++){let g=0,b=Number(p[h]);for(let S=0;S<l;S++){let x=E(h,i,S,o),N=_(x,o),T=Number(u[N])-b;g+=T*T}d[h]=g/(l-e)}return y}function ut(r,t,e=0,n=!1){let o=Sr(r,t,e,n);if(typeof o=="number")return Math.sqrt(o);let a=D.zeros(Array.from(o.shape),"float64"),c=o.data,u=a.data;for(let s=0;s<c.length;s++)u[s]=Math.sqrt(Number(c[s]));return a}function Dr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){for(let m=0;m<a;m++)if(!c[m])return!1;return!0}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return Dr(r);let i=D.zeros(s,"bool"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);for(let m=0;m<p;m++){let y=!0;for(let d=0;d<f;d++){let A=E(m,u,d,n),h=_(A,n);if(!c[h]){y=!1;break}}l[m]=y?1:0}if(e){let m=[...n];return m[u]=1,D.fromData(l,m,"bool")}return i}function wr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){for(let m=0;m<a;m++)if(c[m])return!0;return!1}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return wr(r);let i=D.zeros(s,"bool"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);for(let m=0;m<p;m++){let y=!1;for(let d=0;d<f;d++){let A=E(m,u,d,n),h=_(A,n);if(c[h]){y=!0;break}}l[m]=y?1:0}if(e){let m=[...n];return m[u]=1,D.fromData(l,m,"bool")}return i}function j(r,t){let e=r.size,n=r.dtype,o=t.indexOf(-1),a;if(o!==-1){let i=t.reduce((f,p,m)=>m===o?f:f*p,1),l=e/i;if(!Number.isInteger(l))throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(t)}`);a=t.map((f,p)=>p===o?l:f)}else a=t;if(a.reduce((i,l)=>i*l,1)!==e)throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(a)}`);if(r.isCContiguous){let i=r.data;return D.fromData(i,a,n,P(a),0)}let s=r.copy().data;return D.fromData(s,a,n,P(a),0)}function Nr(r){let t=r.size,e=r.dtype,n=B(e);if(!n)throw new Error(`Cannot flatten array with dtype ${e}`);if(r.isCContiguous){let u=r.data.slice(r.offset,r.offset+t);return D.fromData(u,[t],e,[1],0)}let o=new n(t),a=w(e);for(let c=0;c<t;c++){let u=r.iget(c);o[c]=u}return D.fromData(o,[t],e,[1],0)}function W(r){let t=r.size,e=r.dtype;if(r.isCContiguous){let n=r.data;return D.fromData(n,[t],e,[1],0)}return Nr(r)}function nr(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,c=r.dtype,u;if(t===void 0)u=Array.from({length:n},(f,p)=>n-1-p);else{if(t.length!==n)throw new Error(`axes must have length ${n}, got ${t.length}`);let f=new Set;for(let p of t){let m=p<0?n+p:p;if(m<0||m>=n)throw new Error(`axis ${p} is out of bounds for array of dimension ${n}`);if(f.has(m))throw new Error("repeated axis in transpose");f.add(m)}u=t.map(p=>p<0?n+p:p)}let s=u.map(f=>e[f]),i=Array.from(o),l=u.map(f=>i[f]);return D.fromData(a,s,c,l,r.offset)}function ct(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,c=r.dtype;if(t===void 0){let u=[],s=[];for(let i=0;i<n;i++)e[i]!==1&&(u.push(e[i]),s.push(o[i]));return u.length===0&&(u.push(1),s.push(1)),D.fromData(a,u,c,s,r.offset)}else{let u=t<0?n+t:t;if(u<0||u>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);if(e[u]!==1)throw new Error(`cannot select an axis which has size not equal to one (axis ${t} has size ${e[u]})`);let s=[],i=[];for(let l=0;l<n;l++)l!==u&&(s.push(e[l]),i.push(o[l]));return D.fromData(a,s,c,i,r.offset)}}function Y(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,c=r.dtype,u=t;if(u<0&&(u=n+t+1),u<0||u>n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n+1}`);let s=[...Array.from(e)];s.splice(u,0,1);let i=[...Array.from(o)],l=u<n?o[u]*(e[u]||1):1;return i.splice(u,0,l),D.fromData(a,s,c,i,r.offset)}function lt(r,t,e){let n=r.shape,o=n.length,a=r.strides,c=r.data,u=r.dtype,s=t<0?o+t:t,i=e<0?o+e:e;if(s<0||s>=o)throw new Error(`axis1 ${t} is out of bounds for array of dimension ${o}`);if(i<0||i>=o)throw new Error(`axis2 ${e} is out of bounds for array of dimension ${o}`);if(s===i)return D.fromData(c,Array.from(n),u,Array.from(a),r.offset);let l=Array.from(n),f=Array.from(a);return[l[s],l[i]]=[l[i],l[s]],[f[s],f[i]]=[f[i],f[s]],D.fromData(c,l,u,f,r.offset)}function ft(r,t,e){let n=r.ndim,o=Array.isArray(t)?t:[t],a=Array.isArray(e)?e:[e];if(o.length!==a.length)throw new Error("source and destination must have the same number of elements");let c=o.map(i=>{let l=i<0?n+i:i;if(l<0||l>=n)throw new Error(`source axis ${i} is out of bounds for array of dimension ${n}`);return l}),u=a.map(i=>{let l=i<0?n+i:i;if(l<0||l>=n)throw new Error(`destination axis ${i} is out of bounds for array of dimension ${n}`);return l});if(new Set(c).size!==c.length)throw new Error("repeated axis in source");if(new Set(u).size!==u.length)throw new Error("repeated axis in destination");let s=[];for(let i=0;i<n;i++)c.includes(i)||s.push(i);for(let i=0;i<c.length;i++){let l=u[i];s.splice(l,0,c[i])}return nr(r,s)}function R(r,t=0){if(r.length===0)throw new Error("need at least one array to concatenate");if(r.length===1)return r[0].copy();let e=r[0],n=e.ndim,o=e.dtype,a=t<0?n+t:t;if(a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);for(let m=1;m<r.length;m++){let y=r[m];if(y.ndim!==n)throw new Error("all the input arrays must have same number of dimensions");for(let d=0;d<n;d++)if(d!==a&&y.shape[d]!==e.shape[d])throw new Error("all the input array dimensions except for the concatenation axis must match exactly")}let c=Array.from(e.shape),u=e.shape[a];for(let m=1;m<r.length;m++)u+=r[m].shape[a];c[a]=u;let s=c.reduce((m,y)=>m*y,1),i=B(o);if(!i)throw new Error(`Cannot concatenate arrays with dtype ${o}`);let l=new i(s),f=P(c),p=0;for(let m of r){let y=m.shape[a];Zn(m,l,c,f,a,p,o),p+=y}return D.fromData(l,c,o)}function Zn(r,t,e,n,o,a,c){let u=r.shape,s=u.length,i=r.size,l=c==="int64"||c==="uint64";if(o===0&&r.isCContiguous&&s>0){let p=a*n[0],m=r.data,y=r.offset,d=y+i;t.set(m.subarray(y,d),p);return}let f=new Array(s).fill(0);for(let p=0;p<i;p++){let m=r.iget(p),y=[...f];y[o]+=a;let d=0;for(let A=0;A<s;A++)d+=y[A]*n[A];t[d]=m;for(let A=s-1;A>=0&&(f[A]++,!(f[A]<u[A]));A--)f[A]=0}}function pt(r,t=0){if(r.length===0)throw new Error("need at least one array to stack");let e=r[0],n=e.shape,o=e.ndim,a=t<0?o+1+t:t;if(a<0||a>o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o+1}`);for(let u=1;u<r.length;u++){let s=r[u];if(s.ndim!==o)throw new Error("all input arrays must have the same shape");for(let i=0;i<o;i++)if(s.shape[i]!==n[i])throw new Error("all input arrays must have the same shape")}let c=r.map(u=>Y(u,a));return R(c,a)}function yt(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?j(e,[1,e.shape[0]]):e);return R(t,0)}function mt(r){if(r.length===0)throw new Error("need at least one array to stack");return r.every(e=>e.ndim===1)?R(r,0):R(r,1)}function dt(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?j(Y(j(e,[1,e.shape[0]]),2),[1,e.shape[0],1]):e.ndim===2?Y(e,2):e);return R(t,2)}function gt(r,t,e=0){let n=r.shape,o=n.length,a=e<0?o+e:e;if(a<0||a>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let c=n[a],u;if(typeof t=="number"){if(c%t!==0)throw new Error("array split does not result in an equal division");let s=c/t;u=[];for(let i=1;i<t;i++)u.push(i*s)}else u=t;return ht(r,u,a)}function or(r,t,e=0){let n=r.shape,o=n.length,a=e<0?o+e:e;if(a<0||a>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let c=n[a],u;if(typeof t=="number"){let s=t,i=Math.floor(c/s),l=c%s;u=[];let f=0;for(let p=0;p<s-1;p++)f+=i+(p<l?1:0),u.push(f)}else u=t;return ht(r,u,a)}function ht(r,t,e){let n=r.shape,o=n[e],a=[0,...t,o],c=[];for(let u=0;u<a.length-1;u++){let s=a[u],i=a[u+1];if(s>i)throw new Error("split indices must be in ascending order");let l=Array.from(n);l[e]=i-s;let f=r.offset+s*r.strides[e];c.push(D.fromData(r.data,l,r.dtype,Array.from(r.strides),f))}return c}function At(r,t){if(r.ndim<2)throw new Error("vsplit only works on arrays of 2 or more dimensions");return or(r,t,0)}function bt(r,t){if(r.ndim<1)throw new Error("hsplit only works on arrays of 1 or more dimensions");let e=r.ndim===1?0:1;return or(r,t,e)}function St(r,t){let e=r.shape,n=e.length,o=r.dtype,a=Array.isArray(t)?t:[t],c=Math.max(n,a.length),u=new Array(c).fill(1),s=new Array(c).fill(1);for(let g=0;g<n;g++)u[c-n+g]=e[g];for(let g=0;g<a.length;g++)s[c-a.length+g]=a[g];let i=u.map((g,b)=>g*s[b]),l=i.reduce((g,b)=>g*b,1),f=B(o);if(!f)throw new Error(`Cannot tile array with dtype ${o}`);let p=new f(l),m=P(i),y=r;n<c&&(y=j(r,u));let d=o==="int64"||o==="uint64",A=y.strides,h=new Array(c).fill(0);for(let g=0;g<l;g++){let b=y.offset;for(let N=0;N<c;N++){let T=h[N]%u[N];b+=T*A[N]}let S=y.data[b],x=0;for(let N=0;N<c;N++)x+=h[N]*m[N];p[x]=S;for(let N=c-1;N>=0&&(h[N]++,!(h[N]<i[N]));N--)h[N]=0}return D.fromData(p,i,o)}function Dt(r,t,e){let n=r.shape,o=n.length,a=r.dtype,c=r.size;if(e===void 0){let g=c,b=Array.isArray(t)?t:new Array(g).fill(t);if(b.length!==g)throw new Error(`operands could not be broadcast together with shape (${g},) (${b.length},)`);let S=b.reduce((I,U)=>I+U,0),x=B(a);if(!x)throw new Error(`Cannot repeat array with dtype ${a}`);let N=new x(S),T=0;for(let I=0;I<g;I++){let U=r.iget(I),tr=b[I];for(let Vr=0;Vr<tr;Vr++)N[T++]=U}return D.fromData(N,[S],a)}let u=e<0?o+e:e;if(u<0||u>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let s=n[u],i=Array.isArray(t)?t:new Array(s).fill(t);if(i.length!==s)throw new Error(`operands could not be broadcast together with shape (${s},) (${i.length},)`);let l=Array.from(n);l[u]=i.reduce((g,b)=>g+b,0);let f=l.reduce((g,b)=>g*b,1),p=B(a);if(!p)throw new Error(`Cannot repeat array with dtype ${a}`);let m=new p(f),y=P(l),d=new Array(o).fill(0),A=a==="int64"||a==="uint64",h=[0];for(let g=0;g<s;g++)h.push(h[g]+i[g]);for(let g=0;g<c;g++){let b=r.iget(g),S=d[u],x=i[S],N=0;for(let U=0;U<o;U++)U!==u&&(N+=d[U]*y[U]);let T=y[u],I=h[S];for(let U=0;U<x;U++){let tr=N+(I+U)*T;m[tr]=b}for(let U=o-1;U>=0&&(d[U]++,!(d[U]<n[U]));U--)d[U]=0}return D.fromData(m,l,a)}function Hn(r,t,e,n,o,a,c,u,s,i,l,f,p,m){if(f===0)for(let h=0;h<n*o;h++)p[h]=0;else if(f!==1)for(let h=0;h<n*o;h++)p[h]=(p[h]??0)*f;let y=r==="row-major",d=t==="transpose",A=e==="transpose";if(y&&!d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[S*l+g]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(y&&d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[S*l+g]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(y&&!d&&A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[g*l+S]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(y&&d&&A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[g*l+S]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(!y&&!d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[g*l+S]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}else if(!y&&d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[g*l+S]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}else if(!y&&!d&&A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[S*l+g]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}else for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[S*l+g]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}}function xr(r,t){let e=r.ndim,n=t.ndim;if(e===0||n===0){let o=e===0?r.get():null,a=n===0?t.get():null;if(e===0&&n===0)return typeof o=="bigint"&&typeof a=="bigint"?o*a:Number(o)*Number(a);if(e===0){let c=O(r.dtype,t.dtype),u=D.zeros([...t.shape],c);for(let s=0;s<t.size;s++){let i=t.data[s+t.offset];typeof o=="bigint"&&typeof i=="bigint"?u.data[s]=o*i:u.data[s]=Number(o)*Number(i)}return u}else{let c=O(r.dtype,t.dtype),u=D.zeros([...r.shape],c);for(let s=0;s<r.size;s++){let i=r.data[s+r.offset];typeof i=="bigint"&&typeof a=="bigint"?u.data[s]=i*a:u.data[s]=Number(i)*Number(a)}return u}}if(e===1&&n===1){if(r.shape[0]!==t.shape[0])throw new Error(`dot: incompatible shapes (${r.shape[0]},) and (${t.shape[0]},)`);let o=r.shape[0],a=0;for(let c=0;c<o;c++){let u=r.get(c),s=t.get(c);typeof u=="bigint"&&typeof s=="bigint"?a=Number(a)+Number(u*s):a+=Number(u)*Number(s)}return a}if(e===2&&n===2)return Ir(r,t);if(e===2&&n===1){let[o,a]=r.shape,c=t.shape[0];if(a!==c)throw new Error(`dot: incompatible shapes (${o},${a}) and (${c},)`);let u=O(r.dtype,t.dtype),s=D.zeros([o],u);for(let i=0;i<o;i++){let l=0;for(let f=0;f<a;f++){let p=r.get(i,f),m=t.get(f);typeof p=="bigint"&&typeof m=="bigint"?l=Number(l)+Number(p*m):l+=Number(p)*Number(m)}s.set([i],l)}return s}if(e===1&&n===2){let o=r.shape[0],[a,c]=t.shape;if(o!==a)throw new Error(`dot: incompatible shapes (${o},) and (${a},${c})`);let u=O(r.dtype,t.dtype),s=D.zeros([c],u);for(let i=0;i<c;i++){let l=0;for(let f=0;f<o;f++){let p=r.get(f),m=t.get(f,i);typeof p=="bigint"&&typeof m=="bigint"?l=Number(l)+Number(p*m):l+=Number(p)*Number(m)}s.set([i],l)}return s}if(e>2&&n===1){let o=r.shape[e-1],a=t.shape[0];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(r.shape)} and (${a},)`);let c=[...r.shape.slice(0,-1)],u=O(r.dtype,t.dtype),s=D.zeros(c,u),i=c.reduce((l,f)=>l*f,1);for(let l=0;l<i;l++){let f=0,p=l,m=[];for(let y=c.length-1;y>=0;y--)m[y]=p%c[y],p=Math.floor(p/c[y]);for(let y=0;y<o;y++){let d=[...m,y],A=r.get(...d),h=t.get(y);typeof A=="bigint"&&typeof h=="bigint"?f=Number(f)+Number(A*h):f+=Number(A)*Number(h)}s.set(m,f)}return s}if(e===1&&n>2){let o=r.shape[0],a=1,c=t.shape[a];if(o!==c)throw new Error(`dot: incompatible shapes (${o},) and ${JSON.stringify(t.shape)}`);let u=[...t.shape.slice(0,a),...t.shape.slice(a+1)],s=O(r.dtype,t.dtype),i=D.zeros(u,s),l=u.reduce((f,p)=>f*p,1);for(let f=0;f<l;f++){let p=f,m=[];for(let h=u.length-1;h>=0;h--)m[h]=p%u[h],p=Math.floor(p/u[h]);let y=m.slice(0,a),d=m.slice(a),A=0;for(let h=0;h<o;h++){let g=r.get(h),b=[...y,h,...d],S=t.get(...b);typeof g=="bigint"&&typeof S=="bigint"?A=Number(A)+Number(g*S):A+=Number(g)*Number(S)}i.set(m,A)}return i}if(e>=2&&n>=2&&!(e===2&&n===2)){let o=r.shape[e-1],a=t.shape[n-2];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(r.shape)} and ${JSON.stringify(t.shape)}`);let c=[...r.shape.slice(0,-1),...t.shape.slice(0,-2),t.shape[n-1]],u=O(r.dtype,t.dtype),s=D.zeros(c,u),i=r.shape.slice(0,-1).reduce((m,y)=>m*y,1),l=t.shape.slice(0,-2).reduce((m,y)=>m*y,1),f=t.shape[n-1],p=o;for(let m=0;m<i;m++)for(let y=0;y<l;y++)for(let d=0;d<f;d++){let A=0;for(let g=0;g<p;g++){let b=m*p+g,S=r.data[b+r.offset],x=y*p*f+g*f+d,N=t.data[x+t.offset];typeof S=="bigint"&&typeof N=="bigint"?A=Number(A)+Number(S*N):A+=Number(S)*Number(N)}let h=m*l*f+y*f+d;s.data[h]=A}return s}throw new Error(`dot: unexpected combination of dimensions ${e}D \xB7 ${n}D`)}function Ir(r,t){if(r.ndim!==2||t.ndim!==2)throw new Error("matmul requires 2D arrays");let[e=0,n=0]=r.shape,[o=0,a=0]=t.shape;if(n!==o)throw new Error(`matmul shape mismatch: (${e},${n}) @ (${o},${a})`);let c=O(r.dtype,t.dtype),u=c.startsWith("int")||c.startsWith("uint")||c==="bool"?"float64":c;if(u!=="float64")throw new Error(`matmul currently only supports float64, got ${u}`);let s=r.dtype==="float64"?r.data:Float64Array.from(Array.from(r.data).map(Number)),i=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number));r.offset>0&&(s=s.subarray(r.offset)),t.offset>0&&(i=i.subarray(t.offset));let[l=0,f=0]=r.strides,[p=0,m=0]=t.strides,y=f>l,d=m>p,A=y?"transpose":"no-transpose",h=d?"transpose":"no-transpose",g,b;y?g=f:g=l,d?b=m:b=p;let S=D.zeros([e,a],"float64");return Hn("row-major",A,h,e,a,n,1,s,g,i,b,0,S.data,a),S}function Nt(r){if(r.ndim!==2)throw new Error(`trace requires 2D array, got ${r.ndim}D`);let[t=0,e=0]=r.shape,n=Math.min(t,e),o=0;for(let a=0;a<n;a++){let c=r.get(a,a);typeof c=="bigint"?o=(typeof o=="bigint"?o:BigInt(o))+c:o=(typeof o=="bigint"?Number(o):o)+c}return o}function xt(r,t){let e=r.ndim,n=t.ndim,o=r.shape[e-1],a=t.shape[n-1];if(o!==a)throw new Error(`inner: incompatible shapes - last dimensions ${o} and ${a} don't match`);if(e===1&&n===1)return xr(r,t);let c=[...r.shape.slice(0,-1),...t.shape.slice(0,-1)],u=O(r.dtype,t.dtype),s=D.zeros(c,u),i=e===1?1:r.shape.slice(0,-1).reduce((p,m)=>p*m,1),l=n===1?1:t.shape.slice(0,-1).reduce((p,m)=>p*m,1),f=o;for(let p=0;p<i;p++)for(let m=0;m<l;m++){let y=0;for(let A=0;A<f;A++){let h=e===1?A:p*f+A,g=n===1?A:m*f+A,b=r.data[h+r.offset],S=t.data[g+t.offset];typeof b=="bigint"&&typeof S=="bigint"?y=Number(y)+Number(b*S):y+=Number(b)*Number(S)}if(c.length===0)return y;let d=i===1?m:p*l+m;s.data[d]=y}return s}function It(r,t){let e=r.ndim===1?r:W(r),n=t.ndim===1?t:W(t),o=e.size,a=n.size,c=O(r.dtype,t.dtype),u=D.zeros([o,a],c);for(let s=0;s<o;s++)for(let i=0;i<a;i++){let l=e.get(s),f=n.get(i),p;typeof l=="bigint"&&typeof f=="bigint"?p=l*f:p=Number(l)*Number(f),u.set([s,i],p)}return u}function zt(r,t,e){let n,o;if(typeof e=="number"){let p=e;if(p<0)throw new Error("tensordot: axes must be non-negative");if(p>r.ndim||p>t.ndim)throw new Error("tensordot: axes exceeds array dimensions");n=Array.from({length:p},(m,y)=>r.ndim-p+y),o=Array.from({length:p},(m,y)=>y)}else if([n,o]=e,n.length!==o.length)throw new Error("tensordot: axes lists must have same length");for(let p=0;p<n.length;p++){let m=n[p],y=o[p];if(m<0||m>=r.ndim||y<0||y>=t.ndim)throw new Error("tensordot: axis out of bounds");if(r.shape[m]!==t.shape[y])throw new Error(`tensordot: shape mismatch on axes ${m} and ${y}: ${r.shape[m]} != ${t.shape[y]}`)}let a=[],c=[];for(let p=0;p<r.ndim;p++)n.includes(p)||a.push(p);for(let p=0;p<t.ndim;p++)o.includes(p)||c.push(p);let u=[...a.map(p=>r.shape[p]),...c.map(p=>t.shape[p])];if(u.length===0){let p=0,m=n.map(y=>r.shape[y]).reduce((y,d)=>y*d,1);for(let y=0;y<m;y++){let d=y,A=new Array(n.length);for(let x=n.length-1;x>=0;x--){let N=n[x];A[x]=d%r.shape[N],d=Math.floor(d/r.shape[N])}let h=new Array(r.ndim),g=new Array(t.ndim);for(let x=0;x<n.length;x++)h[n[x]]=A[x];for(let x=0;x<o.length;x++)g[o[x]]=A[x];let b=r.get(...h),S=t.get(...g);typeof b=="bigint"&&typeof S=="bigint"?p=Number(p)+Number(b*S):p+=Number(b)*Number(S)}return p}let s=O(r.dtype,t.dtype),i=D.zeros(u,s),l=u.reduce((p,m)=>p*m,1),f=n.map(p=>r.shape[p]).reduce((p,m)=>p*m,1);for(let p=0;p<l;p++){let m=p,y=[];for(let g=u.length-1;g>=0;g--)y[g]=m%u[g],m=Math.floor(m/u[g]);let d=y.slice(0,a.length),A=y.slice(a.length),h=0;for(let g=0;g<f;g++){m=g;let b=[];for(let I=n.length-1;I>=0;I--){let U=n[I];b[I]=m%r.shape[U],m=Math.floor(m/r.shape[U])}let S=new Array(r.ndim),x=new Array(t.ndim);for(let I=0;I<a.length;I++)S[a[I]]=d[I];for(let I=0;I<c.length;I++)x[c[I]]=A[I];for(let I=0;I<n.length;I++)S[n[I]]=b[I],x[o[I]]=b[I];let N=r.get(...S),T=t.get(...x);typeof N=="bigint"&&typeof T=="bigint"?h=Number(h)+Number(N*T):h+=Number(N)*Number(T)}i.set(y,h)}return i}function Bt(r){return $(r,Math.sqrt,!1)}function Ut(r,t){return typeof t=="number"?Wn(r,t):k(r,t,Math.pow,"power")}function Wn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=e!=="float32"&&e!=="float64"&&(t<0||!Number.isInteger(t))?"float64":e,i=D.zeros(n,s),l=i.data;if(w(e))if(w(s)&&Number.isInteger(t)&&t>=0){let f=o,p=l;for(let m=0;m<a;m++)p[m]=f[m]**BigInt(t)}else for(let f=0;f<a;f++)l[f]=Math.pow(Number(o[f]),t);else for(let f=0;f<a;f++)l[f]=Math.pow(Number(o[f]),t);return i}function _t(r){return $(r,Math.sin,!1)}function Et(r){return $(r,Math.cos,!1)}function Tt(r){return $(r,Math.tan,!1)}function Ot(r){return $(r,Math.asin,!1)}function $t(r){return $(r,Math.acos,!1)}function Mt(r){return $(r,Math.atan,!1)}function Ct(r,t){return typeof t=="number"?Xn(r,t):Jn(r,t)}function Jn(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,c=o==="float32"&&a==="float32"?"float32":"float64",u=D.zeros(e,c),s=u.data;for(let i=0;i<n;i++){let l=(w(o),Number(r.data[i])),f=(w(a),Number(t.data[i]));s[i]=Math.atan2(l,f)}return u}function Xn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=e==="float32"?"float32":"float64",u=D.zeros(n,c),s=u.data;if(w(e))for(let i=0;i<a;i++)s[i]=Math.atan2(Number(o[i]),t);else for(let i=0;i<a;i++)s[i]=Math.atan2(Number(o[i]),t);return u}function kt(r,t){return typeof t=="number"?Kn(r,t):Qn(r,t)}function Qn(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,c=o==="float32"&&a==="float32"?"float32":"float64",u=D.zeros(e,c),s=u.data;for(let i=0;i<n;i++){let l=(w(o),Number(r.data[i])),f=(w(a),Number(t.data[i]));s[i]=Math.hypot(l,f)}return u}function Kn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=e==="float32"?"float32":"float64",u=D.zeros(n,c),s=u.data;if(w(e))for(let i=0;i<a;i++)s[i]=Math.hypot(Number(o[i]),t);else for(let i=0;i<a;i++)s[i]=Math.hypot(Number(o[i]),t);return u}function vt(r){let t=180/Math.PI;return $(r,e=>e*t,!1)}function Vt(r){let t=Math.PI/180;return $(r,e=>e*t,!1)}function Pt(r){return $(r,Math.sinh,!1)}function Ft(r){return $(r,Math.cosh,!1)}function Rt(r){return $(r,Math.tanh,!1)}function Lt(r){return $(r,Math.asinh,!1)}function jt(r){return $(r,Math.acosh,!1)}function qt(r){return $(r,Math.atanh,!1)}function ar(r){if(r.length===0)return[];if(r.length===1)return Array.from(r[0]);let t=Math.max(...r.map(n=>n.length)),e=new Array(t);for(let n=0;n<t;n++){let o=1;for(let a of r){let c=a.length-t+n,u=c<0?1:a[c];if(u!==1){if(o===1)o=u;else if(o!==u)return null}}e[n]=o}return e}function eo(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let c=0;c<n;c++){let u=o-n+c,s=r[c],i=e[u];if(s===i)a[u]=t[c];else if(s===1)a[u]=0;else throw new Error("Invalid broadcast")}return a}function G(r,t){let e=eo(r.shape,r.strides,t);return D.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function Zt(r,t){let e=r.shape,n=e.length,o=t.length;if(o<n)throw new Error("input operand has more dimensions than allowed by the axis remapping");let a=ar([Array.from(e),t]);if(a===null)throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);for(let c=0;c<o;c++)if(a[c]!==t[c])throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);return G(r,t)}function Ht(r){if(r.length===0)return[];if(r.length===1)return[r[0]];let t=r.map(n=>Array.from(n.shape)),e=ar(t);if(e===null)throw new Error(`operands could not be broadcast together with shapes ${t.map(n=>`(${n.join(",")})`).join(" ")}`);return r.map(n=>G(n,e))}function Yt(r,t,e){let n=r.shape,o=n.length,a=r.dtype;if(e===void 0){let y=r.size;for(let g of t){let b=g<0?y+g:g;if(b<0||b>=y)throw new Error(`index ${g} is out of bounds for axis 0 with size ${y}`)}let d=t.length,A=B(a);if(!A)throw new Error(`Cannot take from array with dtype ${a}`);let h=new A(d);for(let g=0;g<d;g++){let b=t[g];b<0&&(b=y+b);let S=r.iget(b);w(a),h[g]=S}return D.fromData(h,[d],a)}let c=e<0?o+e:e;if(c<0||c>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let u=n[c];for(let y of t){let d=y<0?u+y:y;if(d<0||d>=u)throw new Error(`index ${y} is out of bounds for axis ${c} with size ${u}`)}let s=Array.from(n);s[c]=t.length;let i=s.reduce((y,d)=>y*d,1),l=B(a);if(!l)throw new Error(`Cannot take from array with dtype ${a}`);let f=new l(i),p=P(s),m=new Array(o).fill(0);for(let y=0;y<i;y++){let d=[...m],A=m[c],h=t[A];h<0&&(h=u+h),d[c]=h;let g=r.get(...d),b=0;for(let S=0;S<o;S++)b+=m[S]*p[S];w(a),f[b]=g;for(let S=o-1;S>=0&&(m[S]++,!(m[S]<s[S]));S--)m[S]=0}return D.fromData(f,s,a)}function Wt(r,t,e){let n=r.size,o=r.dtype,a;if(typeof e=="number"||typeof e=="bigint")a=new Array(t.length).fill(e);else{a=[];for(let c=0;c<e.size;c++)a.push(e.iget(c));if(a.length===1)a=new Array(t.length).fill(a[0]);else if(a.length!==t.length){let c=[...a];a=[];for(let u=0;u<t.length;u++)a.push(c[u%c.length])}}for(let c=0;c<t.length;c++){let u=t[c];if(u<0&&(u=n+u),u<0||u>=n)throw new Error(`index ${t[c]} is out of bounds for axis 0 with size ${n}`);let s=a[c];w(o)?typeof s!="bigint"&&(s=BigInt(Math.round(Number(s)))):typeof s=="bigint"&&(s=Number(s)),r.iset(u,s)}}function Gt(r,t){if(t.length===0)throw new Error("choices cannot be empty");let e=r.shape,n=t.length,o=t[0].dtype,a=t.map(p=>Array.from(p.shape));a.unshift(Array.from(e));let c=ar(a);if(c===null)throw new Error("operands could not be broadcast together");let u=G(r,c),s=t.map(p=>G(p,c)),i=c.reduce((p,m)=>p*m,1),l=B(o);if(!l)throw new Error(`Cannot choose with dtype ${o}`);let f=new l(i);for(let p=0;p<i;p++){let m=Number(u.iget(p));if(m<0||m>=n)throw new Error(`index ${m} is out of bounds for axis 0 with size ${n}`);let y=s[m].iget(p);w(o),f[p]=y}return D.fromData(f,c,o)}function Jt(r,t,e=!1){if(r.ndim!==t.ndim)return!1;for(let o=0;o<r.ndim;o++)if(r.shape[o]!==t.shape[o])return!1;let n=r.size;for(let o=0;o<n;o++){let a=r.iget(o),c=t.iget(o);if(e){let u=typeof a=="number"&&Number.isNaN(a),s=typeof c=="number"&&Number.isNaN(c);if(u&&s)continue}if(a!==c)return!1}return!0}var z=class r{constructor(t,e){this._storage=t,this._base=e}get storage(){return this._storage}static _fromStorage(t,e){return new r(t,e)}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(t){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);let e=t.map((n,o)=>{let a=n;if(a<0&&(a=this.shape[o]+a),a<0||a>=this.shape[o])throw new Error(`Index ${n} is out of bounds for axis ${o} with size ${this.shape[o]}`);return a});return this._storage.get(...e)}set(t,e){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);let n=t.map((c,u)=>{let s=c;if(s<0&&(s=this.shape[u]+s),s<0||s>=this.shape[u])throw new Error(`Index ${c} is out of bounds for axis ${u} with size ${this.shape[u]}`);return s}),o=this.dtype,a;w(o)?a=typeof e=="bigint"?e:BigInt(Math.round(e)):o==="bool"?a=e?1:0:a=Number(e),this._storage.set(n,a)}copy(){return new r(this._storage.copy())}astype(t,e=!0){let n=this.dtype;if(n===t&&!e)return this;if(n===t&&e)return this.copy();let o=Array.from(this.shape),a=this.size,c=B(t);if(!c)throw new Error(`Cannot convert to dtype ${t}`);let u=new c(a),s=this.data;if(w(n)&&!w(t)){let l=s;if(t==="bool")for(let f=0;f<a;f++)u[f]=l[f]!==BigInt(0)?1:0;else for(let f=0;f<a;f++)u[f]=Number(l[f])}else if(!w(n)&&w(t)){let l=s;for(let f=0;f<a;f++)u[f]=BigInt(Math.round(Number(l[f])))}else if(t==="bool"){let l=s;for(let f=0;f<a;f++)u[f]=l[f]!==0?1:0}else if(n==="bool"&&!w(t)){let l=s;for(let f=0;f<a;f++)u[f]=l[f]}else if(!w(n)&&!w(t)){let l=s;for(let f=0;f<a;f++)u[f]=l[f]}else{let l=s;for(let f=0;f<a;f++)u[f]=l[f]}let i=D.fromData(u,o,t);return new r(i)}add(t){let e=typeof t=="number"?t:t._storage,n=qr(this._storage,e);return r._fromStorage(n)}subtract(t){let e=typeof t=="number"?t:t._storage,n=Zr(this._storage,e);return r._fromStorage(n)}multiply(t){let e=typeof t=="number"?t:t._storage,n=Hr(this._storage,e);return r._fromStorage(n)}divide(t){let e=typeof t=="number"?t:t._storage,n=Yr(this._storage,e);return r._fromStorage(n)}mod(t){let e=typeof t=="number"?t:t._storage,n=Xr(this._storage,e);return r._fromStorage(n)}floor_divide(t){let e=typeof t=="number"?t:t._storage,n=Qr(this._storage,e);return r._fromStorage(n)}positive(){let t=Kr(this._storage);return r._fromStorage(t)}reciprocal(){let t=rt(this._storage);return r._fromStorage(t)}sqrt(){let t=Bt(this._storage);return r._fromStorage(t)}power(t){let e=typeof t=="number"?t:t._storage,n=Ut(this._storage,e);return r._fromStorage(n)}absolute(){let t=Wr(this._storage);return r._fromStorage(t)}negative(){let t=Gr(this._storage);return r._fromStorage(t)}sign(){let t=Jr(this._storage);return r._fromStorage(t)}sin(){let t=_t(this._storage);return r._fromStorage(t)}cos(){let t=Et(this._storage);return r._fromStorage(t)}tan(){let t=Tt(this._storage);return r._fromStorage(t)}arcsin(){let t=Ot(this._storage);return r._fromStorage(t)}arccos(){let t=$t(this._storage);return r._fromStorage(t)}arctan(){let t=Mt(this._storage);return r._fromStorage(t)}arctan2(t){let e=typeof t=="number"?t:t._storage,n=Ct(this._storage,e);return r._fromStorage(n)}hypot(t){let e=typeof t=="number"?t:t._storage,n=kt(this._storage,e);return r._fromStorage(n)}degrees(){let t=vt(this._storage);return r._fromStorage(t)}radians(){let t=Vt(this._storage);return r._fromStorage(t)}sinh(){let t=Pt(this._storage);return r._fromStorage(t)}cosh(){let t=Ft(this._storage);return r._fromStorage(t)}tanh(){let t=Rt(this._storage);return r._fromStorage(t)}arcsinh(){let t=Lt(this._storage);return r._fromStorage(t)}arccosh(){let t=jt(this._storage);return r._fromStorage(t)}arctanh(){let t=qt(this._storage);return r._fromStorage(t)}greater(t){let e=typeof t=="number"?t:t._storage,n=tt(this._storage,e);return r._fromStorage(n)}greater_equal(t){let e=typeof t=="number"?t:t._storage,n=et(this._storage,e);return r._fromStorage(n)}less(t){let e=typeof t=="number"?t:t._storage,n=nt(this._storage,e);return r._fromStorage(n)}less_equal(t){let e=typeof t=="number"?t:t._storage,n=ot(this._storage,e);return r._fromStorage(n)}equal(t){let e=typeof t=="number"?t:t._storage,n=at(this._storage,e);return r._fromStorage(n)}not_equal(t){let e=typeof t=="number"?t:t._storage,n=st(this._storage,e);return r._fromStorage(n)}isclose(t,e=1e-5,n=1e-8){let o=typeof t=="number"?t:t._storage,a=yr(this._storage,o,e,n);return r._fromStorage(a)}allclose(t,e=1e-5,n=1e-8){let o=typeof t=="number"?t:t._storage;return it(this._storage,o,e,n)}sum(t,e=!1){let n=H(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}mean(t,e=!1){let n=mr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}max(t,e=!1){let n=dr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}min(t,e=!1){let n=hr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}prod(t,e=!1){let n=gr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}argmin(t){let e=Ar(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}argmax(t){let e=br(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}var(t,e=0,n=!1){let o=Sr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}std(t,e=0,n=!1){let o=ut(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}all(t,e=!1){let n=Dr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}any(t,e=!1){let n=wr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}reshape(...t){let e=t.length===1&&Array.isArray(t[0])?t[0]:t,n=j(this._storage,e),a=n.data===this.data?this._base??this:void 0;return r._fromStorage(n,a)}flatten(){let t=Nr(this._storage);return r._fromStorage(t)}ravel(){let t=W(this._storage),n=t.data===this.data?this._base??this:void 0;return r._fromStorage(t,n)}transpose(t){let e=nr(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}squeeze(t){let e=ct(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}expand_dims(t){let e=Y(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}swapaxes(t,e){let n=lt(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}moveaxis(t,e){let n=ft(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}repeat(t,e){let n=Dt(this._storage,t,e);return r._fromStorage(n)}take(t,e){let n=Yt(this._storage,t,e);return r._fromStorage(n)}put(t,e){let n=e instanceof r?e._storage:e;Wt(this._storage,t,n)}matmul(t){let e=Ir(this._storage,t._storage);return r._fromStorage(e)}dot(t){let e=xr(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}trace(){return Nt(this._storage)}inner(t){let e=xt(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}outer(t){let e=It(this._storage,t._storage);return r._fromStorage(e)}tensordot(t,e=2){let n=zt(this._storage,t._storage,e);return typeof n=="number"||typeof n=="bigint"?n:r._fromStorage(n)}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`);let e=t.map((s,i)=>{let l=Pr(s);return Fr(l,this.shape[i])});for(;e.length<this.ndim;)e.push({start:0,stop:this.shape[e.length],step:1,isIndex:!1});let n=[],o=[],a=this._storage.offset;for(let s=0;s<e.length;s++){let i=e[s],l=this._storage.strides[s];if(a+=i.start*l,!i.isIndex){let f;i.step>0?f=Math.max(0,Math.ceil((i.stop-i.start)/i.step)):f=Math.max(0,Math.ceil((i.start-i.stop)/Math.abs(i.step))),n.push(f),o.push(l*i.step)}}let c=D.fromData(this._storage.data,n,this._storage.dtype,o,a),u=this._base??this;return new r(c,u)}row(t){if(this.ndim<2)throw new Error("row() requires at least 2 dimensions");return this.slice(String(t),":")}col(t){if(this.ndim<2)throw new Error("col() requires at least 2 dimensions");return this.slice(":",String(t))}rows(t,e){if(this.ndim<2)throw new Error("rows() requires at least 2 dimensions");return this.slice(`${t}:${e}`,":")}cols(t,e){if(this.ndim<2)throw new Error("cols() requires at least 2 dimensions");return this.slice(":",`${t}:${e}`)}toString(){return`NDArray(shape=${JSON.stringify(this.shape)}, dtype=${this.dtype})`}toArray(){if(this.ndim===0)return this._storage.iget(0);let t=this.shape,e=t.length,n=(o,a)=>{if(a===e)return this._storage.get(...o);let c=[];for(let u=0;u<t[a];u++)o[a]=u,c.push(n(o,a+1));return c};return n(new Array(e),0)}};function J(r,t=M){let e=D.zeros(r,t);return new z(e)}function zr(r,t=M){let e=D.ones(r,t);return new z(e)}function oo(r){let t=[],e=r;for(;Array.isArray(e);)t.push(e.length),e=e[0];return t}function Xt(r){return typeof r=="bigint"?!0:Array.isArray(r)?r.some(t=>Xt(t)):!1}function ao(r){let t=[];function e(n){Array.isArray(n)?n.forEach(o=>e(o)):t.push(n)}return e(r),t}function V(r,t){if(r instanceof z)return!t||r.dtype===t?r.copy():r.astype(t);let e=Xt(r),n=oo(r),o=n.reduce((l,f)=>l*f,1),a=t;a||(e?a="int64":a=M);let c=B(a);if(!c)throw new Error(`Cannot create array with dtype ${a}`);let u=new c(o),s=ao(r);if(w(a)){let l=u;for(let f=0;f<o;f++){let p=s[f];l[f]=typeof p=="bigint"?p:BigInt(Math.round(Number(p)))}}else if(a==="bool"){let l=u;for(let f=0;f<o;f++)l[f]=s[f]?1:0}else{let l=u;for(let f=0;f<o;f++){let p=s[f];l[f]=Number(p)}}let i=D.fromData(u,n,a);return new z(i)}function Qt(r,t,e=1,n=M){let o=r,a=t;if(t===void 0&&(o=0,a=r),a===void 0)throw new Error("stop is required");let c=Math.max(0,Math.ceil((a-o)/e)),u=B(n);if(!u)throw new Error(`Cannot create arange array with dtype ${n}`);let s=new u(c);if(w(n))for(let l=0;l<c;l++)s[l]=BigInt(Math.round(o+l*e));else if(n==="bool")for(let l=0;l<c;l++)s[l]=o+l*e!==0?1:0;else for(let l=0;l<c;l++)s[l]=o+l*e;let i=D.fromData(s,[c],n);return new z(i)}function Kt(r,t,e=50,n=M){if(e<0)throw new Error("num must be non-negative");if(e===0)return V([],n);if(e===1)return V([r],n);let o=B(n);if(!o)throw new Error(`Cannot create linspace array with dtype ${n}`);let a=new o(e),c=(t-r)/(e-1);if(w(n))for(let s=0;s<e;s++)a[s]=BigInt(Math.round(r+s*c));else if(n==="bool")for(let s=0;s<e;s++)a[s]=r+s*c!==0?1:0;else for(let s=0;s<e;s++)a[s]=r+s*c;let u=D.fromData(a,[e],n);return new z(u)}function re(r,t,e=50,n=10,o=M){if(e<0)throw new Error("num must be non-negative");if(e===0)return V([],o);if(e===1)return V([Math.pow(n,r)],o);let a=B(o);if(!a)throw new Error(`Cannot create logspace array with dtype ${o}`);let c=new a(e),u=(t-r)/(e-1);if(w(o))for(let i=0;i<e;i++){let l=r+i*u;c[i]=BigInt(Math.round(Math.pow(n,l)))}else if(o==="bool")for(let i=0;i<e;i++){let l=r+i*u;c[i]=Math.pow(n,l)!==0?1:0}else for(let i=0;i<e;i++){let l=r+i*u;c[i]=Math.pow(n,l)}let s=D.fromData(c,[e],o);return new z(s)}function te(r,t,e=50,n=M){if(e<0)throw new Error("num must be non-negative");if(r===0||t===0)throw new Error("Geometric sequence cannot include zero");if(e===0)return V([],n);if(e===1)return V([r],n);let o=Math.sign(r),a=Math.sign(t);if(o!==a)throw new Error("Geometric sequence cannot contain both positive and negative values");let c=B(n);if(!c)throw new Error(`Cannot create geomspace array with dtype ${n}`);let u=new c(e),s=Math.log(Math.abs(r)),l=(Math.log(Math.abs(t))-s)/(e-1);if(w(n))for(let p=0;p<e;p++){let m=o*Math.exp(s+p*l);u[p]=BigInt(Math.round(m))}else if(n==="bool")for(let p=0;p<e;p++){let m=o*Math.exp(s+p*l);u[p]=m!==0?1:0}else for(let p=0;p<e;p++){let m=o*Math.exp(s+p*l);u[p]=m}let f=D.fromData(u,[e],n);return new z(f)}function Br(r,t,e=0,n=M){let o=t??r,a=J([r,o],n),c=a.data;if(w(n)){let u=c;for(let s=0;s<r;s++){let i=s+e;i>=0&&i<o&&(u[s*o+i]=BigInt(1))}}else{let u=c;for(let s=0;s<r;s++){let i=s+e;i>=0&&i<o&&(u[s*o+i]=1)}}return a}function Ur(r,t=M){return J(r,t)}function _r(r,t,e){let n=e;n||(typeof t=="bigint"?n="int64":typeof t=="boolean"?n="bool":Number.isInteger(t)?n="int32":n=M);let o=B(n);if(!o)throw new Error(`Cannot create full array with dtype ${n}`);let a=r.reduce((s,i)=>s*i,1),c=new o(a);if(w(n)){let s=typeof t=="bigint"?t:BigInt(Math.round(Number(t)));c.fill(s)}else n==="bool"?c.fill(t?1:0):c.fill(Number(t));let u=D.fromData(c,r,n);return new z(u)}function ee(r,t=M){return Br(r,r,0,t)}function ne(r,t){return r instanceof z?!t||r.dtype===t?r:r.astype(t):V(r,t)}function oe(r){return r.copy()}function ae(r,t){return J(Array.from(r.shape),t??r.dtype)}function se(r,t){return zr(Array.from(r.shape),t??r.dtype)}function ie(r,t){return Ur(Array.from(r.shape),t??r.dtype)}function ue(r,t,e){return _r(Array.from(r.shape),t,e??r.dtype)}function ce(r){return r.sqrt()}function le(r,t){return r.power(t)}function fe(r){return r.absolute()}function pe(r){return r.negative()}function ye(r){return r.sign()}function me(r,t){return r.mod(t)}function de(r,t){return r.floor_divide(t)}function ge(r){return r.positive()}function he(r){return r.reciprocal()}function Ae(r,t){return r.dot(t)}function be(r){return r.trace()}function Se(r,t){return r.transpose(t)}function De(r,t){return r.inner(t)}function we(r,t){return r.outer(t)}function Ne(r,t,e=2){return r.tensordot(t,e)}function xe(r){return r.sin()}function Ie(r){return r.cos()}function ze(r){return r.tan()}function Be(r){return r.arcsin()}function Ue(r){return r.arccos()}function _e(r){return r.arctan()}function Ee(r,t){return r.arctan2(t)}function Te(r,t){return r.hypot(t)}function Oe(r){return r.degrees()}function $e(r){return r.radians()}function Me(r){return r.sinh()}function Ce(r){return r.cosh()}function ke(r){return r.tanh()}function ve(r){return r.arcsinh()}function Ve(r){return r.arccosh()}function Pe(r){return r.arctanh()}function Fe(r,t,e){return r.swapaxes(t,e)}function Re(r,t,e){return r.moveaxis(t,e)}function Le(r,t=0){if(r.length===0)throw new Error("need at least one array to concatenate");let e=r.map(o=>o.storage),n=R(e,t);return z._fromStorage(n)}function je(r,t=0){if(r.length===0)throw new Error("need at least one array to stack");let e=r.map(o=>o.storage),n=pt(e,t);return z._fromStorage(n)}function qe(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=yt(t);return z._fromStorage(e)}function Ze(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=mt(t);return z._fromStorage(e)}function He(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=dt(t);return z._fromStorage(e)}function Ye(r,t,e=0){return gt(r.storage,t,e).map(o=>z._fromStorage(o,r.base??r))}function We(r,t,e=0){return or(r.storage,t,e).map(o=>z._fromStorage(o,r.base??r))}function Ge(r,t){return At(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function Je(r,t){return bt(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function Xe(r,t){let e=St(r.storage,t);return z._fromStorage(e)}function Qe(r,t,e){return r.repeat(t,e)}function Ke(r,t){let e=Zt(r.storage,t);return z._fromStorage(e,r.base??r)}function rn(...r){let t=r.map(n=>n.storage);return Ht(t).map((n,o)=>z._fromStorage(n,r[o].base??r[o]))}function tn(r,t,e){return r.take(t,e)}function en(r,t,e){r.put(t,e)}function nn(r,t){let e=t.map(o=>o.storage),n=Gt(r.storage,e);return z._fromStorage(n)}function on(r,t,e=!1){return Jt(r.storage,t.storage,e)}var X=new Uint8Array([147,78,85,77,80,89]),sr=["float64","float32","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"];function Er(){let r=new ArrayBuffer(2);return new DataView(r).setInt16(0,256,!0),new Int16Array(r)[0]===256}var so={f8:"float64",f4:"float32",i8:"int64",i4:"int32",i2:"int16",i1:"int8",u8:"uint64",u4:"uint32",u2:"uint16",u1:"uint8",b1:"bool"},Q={float64:"<f8",float32:"<f4",int64:"<i8",int32:"<i4",int16:"<i2",int8:"|i1",uint64:"<u8",uint32:"<u4",uint16:"<u2",uint8:"|u1",bool:"|b1"},an={c:"complex numbers",S:"byte strings",U:"Unicode strings",O:"Python objects",V:"structured arrays (void)",M:"datetime64",m:"timedelta64"};function sn(r){if(r.startsWith("[")||r.startsWith("("))throw new v(`Structured/compound dtypes are not supported: ${r}`);let t="",e=r;(r[0]==="<"||r[0]===">"||r[0]==="="||r[0]==="|")&&(t=r[0],e=r.slice(1));let n=e[0];if(n&&n in an)throw new v(`Unsupported dtype: ${an[n]} (${r}). Use the 'force' parameter to skip arrays with unsupported dtypes.`);let o=so[e];if(!o)throw new v(`Unknown or unsupported dtype descriptor: ${r}. Supported types: ${sr.join(", ")}. Use the 'force' parameter to skip arrays with unsupported dtypes.`);let a=Er(),c=t==="<"||t==="|"||t==="="&&a,u=t===">"||t==="="&&!a,s=parseInt(e.slice(1),10),i=s>1&&(u&&a||c&&!a);return{dtype:o,needsByteSwap:i,itemsize:s}}var v=class extends Error{constructor(t){super(t),this.name="UnsupportedDTypeError"}},C=class extends Error{constructor(t){super(t),this.name="InvalidNpyError"}};function K(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=ir(t);return ur(t,e)}function ir(r){if(r.length<10)throw new C("File too small to be a valid NPY file");for(let i=0;i<X.length;i++)if(r[i]!==X[i])throw new C("Invalid NPY magic number");let t=r[6],e=r[7];if(t!==1&&t!==2&&t!==3)throw new C(`Unsupported NPY version: ${t}.${e}`);let n,o;t===1?(n=r[8]|r[9]<<8,o=10):(n=r[8]|r[9]<<8|r[10]<<16|r[11]<<24,o=12);let a=o+n;if(r.length<a)throw new C("File truncated: header extends beyond file");let c=r.slice(o,a),u=new TextDecoder("utf-8").decode(c).trim(),s=io(u);return{version:{major:t,minor:e},header:s,dataOffset:a}}function ur(r,t){let{header:e,dataOffset:n}=t,{dtype:o,needsByteSwap:a,itemsize:c}=sn(e.descr),u=e.shape.reduce((d,A)=>d*A,1),s=u*c,i=r.length-n;if(i<s)throw new C(`File truncated: expected ${s} bytes of data, got ${i}`);let l=new ArrayBuffer(s);new Uint8Array(l).set(r.subarray(n,n+s));let p=uo(l,o,u,a,c),m=e.shape,y;if(e.fortran_order&&m.length>1){let d=[...m].reverse(),A=D.fromData(p,d,o);y=co(A,d),m=e.shape}else y=D.fromData(p,[...m],o);return new z(y)}function io(r){let t=r.match(/'descr'\s*:\s*'([^']+)'/),e=r.match(/'fortran_order'\s*:\s*(True|False)/),n=r.match(/'shape'\s*:\s*\(([^)]*)\)/);if(!t||!e||!n)throw new C(`Failed to parse NPY header: ${r}`);let o=t[1],a=e[1]==="True",c=n[1].trim(),u;return c===""?u=[]:u=c.split(",").map(s=>s.trim()).filter(s=>s!=="").map(s=>{let i=parseInt(s,10);if(isNaN(i))throw new C(`Invalid shape value: ${s}`);return i}),{descr:o,fortran_order:a,shape:u}}function uo(r,t,e,n,o){let a=B(t);if(!a)throw new C(`Cannot create array for dtype: ${t}`);if(!n)return new a(r,0,e);let c=new Uint8Array(r),u=new Uint8Array(r.byteLength);for(let s=0;s<e;s++){let i=s*o;for(let l=0;l<o;l++)u[i+l]=c[i+o-1-l]}return new a(u.buffer,0,e)}function co(r,t){let e=t.length,n=r.size,o=r.dtype,a=B(o);if(!a)throw new C(`Cannot create array for dtype: ${o}`);let c=new a(n),u=[...t].reverse(),s=un(t),i=un(u),l=new Array(e).fill(0);for(let f=0;f<n;f++){let p=f;for(let y=0;y<e;y++){let d=s[y];l[y]=Math.floor(p/d),p=p%d}let m=0;for(let y=0;y<e;y++)m+=l[e-1-y]*i[y];w(o),c[m]=r.iget(f)}return D.fromData(c,u,o)}function un(r){let t=new Array(r.length),e=1;for(let n=r.length-1;n>=0;n--)t[n]=e,e*=r[n];return t}function q(r){let t=r.shape,e=r.dtype,n=Q[e],o=t.length===0?"()":t.length===1?`(${t[0]},)`:`(${t.join(", ")})`,a=`{'descr': '${n}', 'fortran_order': False, 'shape': ${o}, }`,c=12,s=(64-(c+a.length+1)%64)%64;a=a+" ".repeat(s)+`
2
- `;let i=new TextEncoder().encode(a),l=i.length,f=r.size,p=Lr(e),m=f*p,y=c+l+m,d=new Uint8Array(y);d.set(X,0),d[6]=3,d[7]=0,d[8]=l&255,d[9]=l>>8&255,d[10]=l>>16&255,d[11]=l>>24&255,d.set(i,c);let A=c+l;return lo(r,d.subarray(A),p),d}function lo(r,t,e){let n=r.dtype,o=r.size,a=Er(),c=w(n),u=r._storage;if(u.isCContiguous&&u.offset===0&&a){let i=u.data,l=new Uint8Array(i.buffer,i.byteOffset,o*e);t.set(l)}else{let i=new DataView(t.buffer,t.byteOffset,t.byteLength);for(let l=0;l<o;l++){let f=u.iget(l),p=l*e;c?fo(i,p,f,n==="uint64"):po(i,p,f,n)}}}function fo(r,t,e,n){n?r.setBigUint64(t,e,!0):r.setBigInt64(t,e,!0)}function po(r,t,e,n){switch(n){case"float64":r.setFloat64(t,e,!0);break;case"float32":r.setFloat32(t,e,!0);break;case"int32":r.setInt32(t,e,!0);break;case"int16":r.setInt16(t,e,!0);break;case"int8":r.setInt8(t,e);break;case"uint32":r.setUint32(t,e,!0);break;case"uint16":r.setUint16(t,e,!0);break;case"uint8":case"bool":r.setUint8(t,e);break;default:throw new Error(`Unsupported dtype for serialization: ${n}`)}}var yo=(()=>{let r=new Uint32Array(256);for(let t=0;t<256;t++){let e=t;for(let n=0;n<8;n++)e=e&1?3988292384^e>>>1:e>>>1;r[t]=e}return r})();function Tr(r){let t=4294967295;for(let e=0;e<r.length;e++)t=yo[(t^r[e])&255]^t>>>8;return(t^4294967295)>>>0}async function cn(r){let t=fn(r),e=new Map;for(let n of t){let o=await mo(n);e.set(n.name,o)}return e}function ln(r){let t=fn(r),e=new Map;for(let n of t){if(n.compressionMethod!==0)throw new Error(`Cannot read compressed entry synchronously: ${n.name}. Use readZip() (async) for DEFLATE-compressed files.`);e.set(n.name,n.compressedData)}return e}function fn(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=new DataView(t.buffer,t.byteOffset,t.byteLength),n=[],o=-1;for(let i=t.length-22;i>=0;i--)if(e.getUint32(i,!0)===101010256){o=i;break}if(o===-1)throw new Error("Invalid ZIP file: end of central directory not found");let a=e.getUint32(o+16,!0),c=e.getUint16(o+10,!0),u=[],s=a;for(let i=0;i<c&&e.getUint32(s,!0)===33639248;i++){let f=e.getUint16(s+10,!0),p=e.getUint32(s+16,!0),m=e.getUint32(s+20,!0),y=e.getUint32(s+24,!0),d=e.getUint16(s+28,!0),A=e.getUint16(s+30,!0),h=e.getUint16(s+32,!0),g=e.getUint32(s+42,!0),b=t.slice(s+46,s+46+d),S=new TextDecoder("utf-8").decode(b);u.push({name:S,compressionMethod:f,crc32:p,compressedSize:m,uncompressedSize:y,localHeaderOffset:g}),s=s+46+d+A+h}for(let i of u){let l=i.localHeaderOffset;if(e.getUint32(l,!0)!==67324752)throw new Error(`Invalid local file header at offset ${l}`);let p=e.getUint16(l+26,!0),m=e.getUint16(l+28,!0),y=l+30+p+m,d=t.slice(y,y+i.compressedSize);n.push({name:i.name,compressedData:d,compressionMethod:i.compressionMethod,crc32:i.crc32,compressedSize:i.compressedSize,uncompressedSize:i.uncompressedSize})}return n}async function mo(r){if(r.compressionMethod===0)return r.compressedData;if(r.compressionMethod===8)return await go(r.compressedData);throw new Error(`Unsupported compression method: ${r.compressionMethod}`)}async function go(r){if(typeof DecompressionStream>"u")throw new Error("DecompressionStream is not available. This environment does not support the Compression Streams API. Please use a modern browser or Node.js 18+.");let t=new DecompressionStream("deflate-raw"),e=new Uint8Array(r.length);e.set(r);let n=t.writable.getWriter();n.write(e),n.close();let o=t.readable.getReader(),a=[];for(;;){let{done:i,value:l}=await o.read();if(i)break;a.push(l)}let c=a.reduce((i,l)=>i+l.length,0),u=new Uint8Array(c),s=0;for(let i of a)u.set(i,s),s+=i.length;return u}async function cr(r,t={}){let e=t.force??!1,n=await cn(r);return pn(n,e)}function lr(r,t={}){let e=t.force??!1,n=ln(r);return pn(n,e)}function pn(r,t){let e=new Map,n=[],o=new Map;for(let[a,c]of r){if(!a.endsWith(".npy"))continue;let u=a.slice(0,-4);try{let s=K(c);e.set(u,s)}catch(s){if(s instanceof v&&t)n.push(u),o.set(u,s.message);else throw s}}return{arrays:e,skipped:n,errors:o}}async function Mr(r,t={}){let e=await cr(r,t);return Object.fromEntries(e.arrays)}function Cr(r,t={}){let e=lr(r,t);return Object.fromEntries(e.arrays)}async function yn(r,t={}){let e=t.compress??!1,n=[];for(let[p,m]of r){let y=Tr(m),d,A;e?(d=await Ao(m),d.length<m.length?A=8:(d=m,A=0)):(d=m,A=0),n.push({name:p,data:m,compressedData:d,crc:y,compressionMethod:A,offset:0})}let o=0;for(let p of n){let m=new TextEncoder().encode(p.name);o+=30+m.length+p.compressedData.length}let a=0;for(let p of n){let m=new TextEncoder().encode(p.name);a+=46+m.length}let u=o+a+22,s=new Uint8Array(u),i=new DataView(s.buffer),l=0;for(let p of n)p.offset=l,l=dn(s,i,l,p);let f=l;for(let p of n)l=gn(s,i,l,p);return hn(i,l,n.length,a,f),s}function mn(r){let t=[];for(let[l,f]of r){let p=Tr(f);t.push({name:l,data:f,compressedData:f,crc:p,compressionMethod:0,offset:0})}let e=0;for(let l of t){let f=new TextEncoder().encode(l.name);e+=30+f.length+l.compressedData.length}let n=0;for(let l of t){let f=new TextEncoder().encode(l.name);n+=46+f.length}let a=e+n+22,c=new Uint8Array(a),u=new DataView(c.buffer),s=0;for(let l of t)l.offset=s,s=dn(c,u,s,l);let i=s;for(let l of t)s=gn(c,u,s,l);return hn(u,s,t.length,n,i),c}function dn(r,t,e,n){let o=new TextEncoder().encode(n.name);return t.setUint32(e,67324752,!0),e+=4,t.setUint16(e,n.compressionMethod===8?20:10,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,n.compressionMethod,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,33,!0),e+=2,t.setUint32(e,n.crc,!0),e+=4,t.setUint32(e,n.compressedData.length,!0),e+=4,t.setUint32(e,n.data.length,!0),e+=4,t.setUint16(e,o.length,!0),e+=2,t.setUint16(e,0,!0),e+=2,r.set(o,e),e+=o.length,r.set(n.compressedData,e),e+=n.compressedData.length,e}function gn(r,t,e,n){let o=new TextEncoder().encode(n.name);return t.setUint32(e,33639248,!0),e+=4,t.setUint16(e,20,!0),e+=2,t.setUint16(e,n.compressionMethod===8?20:10,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,n.compressionMethod,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,33,!0),e+=2,t.setUint32(e,n.crc,!0),e+=4,t.setUint32(e,n.compressedData.length,!0),e+=4,t.setUint32(e,n.data.length,!0),e+=4,t.setUint16(e,o.length,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint32(e,0,!0),e+=4,t.setUint32(e,n.offset,!0),e+=4,r.set(o,e),e+=o.length,e}function hn(r,t,e,n,o){r.setUint32(t,101010256,!0),t+=4,r.setUint16(t,0,!0),t+=2,r.setUint16(t,0,!0),t+=2,r.setUint16(t,e,!0),t+=2,r.setUint16(t,e,!0),t+=2,r.setUint32(t,n,!0),t+=4,r.setUint32(t,o,!0),t+=4,r.setUint16(t,0,!0)}async function Ao(r){if(typeof CompressionStream>"u")throw new Error("CompressionStream is not available. This environment does not support the Compression Streams API. Please use a modern browser or Node.js 18+.");let t=new CompressionStream("deflate-raw"),e=new Uint8Array(r.length);e.set(r);let n=t.writable.getWriter();n.write(e),n.close();let o=t.readable.getReader(),a=[];for(;;){let{done:i,value:l}=await o.read();if(i)break;a.push(l)}let c=a.reduce((i,l)=>i+l.length,0),u=new Uint8Array(c),s=0;for(let i of a)u.set(i,s),s+=i.length;return u}async function kr(r,t={}){let e=An(r);return yn(e,{compress:t.compress??!1})}function vr(r){let t=An(r);return mn(t)}function An(r){let t=new Map;if(Array.isArray(r)){for(let n=0;n<r.length;n++){let o=r[n],a=q(o);t.set(`arr_${n}.npy`,a)}return t}let e=r instanceof Map?r.entries():Object.entries(r);for(let[n,o]of e){if(typeof n!="string"||n.length===0)throw new Error("Array names must be non-empty strings");let a=q(o),c=n.endsWith(".npy")?n:`${n}.npy`;t.set(c,a)}return t}var bo="0.5.0";return xn(So);})();
1
+ "use strict";var np=(()=>{var Or=Object.defineProperty;var Na=Object.getOwnPropertyDescriptor;var wa=Object.getOwnPropertyNames;var xa=Object.prototype.hasOwnProperty;var Ia=(t,r)=>{for(var e in r)Or(t,e,{get:r[e],enumerable:!0})},za=(t,r,e,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of wa(r))!xa.call(t,o)&&o!==e&&Or(t,o,{get:()=>r[o],enumerable:!(n=Na(r,o))||n.enumerable});return t};var Ba=t=>za(Or({},"__esModule",{value:!0}),t);var Is={};Ia(Is,{DTYPE_TO_DESCR:()=>ir,InvalidNpyError:()=>k,NDArray:()=>I,SUPPORTED_DTYPES:()=>Br,UnsupportedDTypeError:()=>R,__version__:()=>xs,absolute:()=>An,append:()=>Io,arange:()=>Ze,arccos:()=>kn,arccosh:()=>Gn,arcsin:()=>Cn,arcsinh:()=>Yn,arctan:()=>Vn,arctan2:()=>Pn,arctanh:()=>Jn,array:()=>C,array_equal:()=>Mo,array_equiv:()=>Co,array_split:()=>no,asanyarray:()=>tn,asarray:()=>ar,ascontiguousarray:()=>en,asfortranarray:()=>nn,atleast_1d:()=>Ao,atleast_2d:()=>bo,atleast_3d:()=>So,average:()=>Lo,broadcast_arrays:()=>Uo,broadcast_shapes:()=>To,broadcast_to:()=>Eo,cbrt:()=>ea,choose:()=>vo,column_stack:()=>No,concatenate:()=>Y,copy:()=>Je,cos:()=>vn,cosh:()=>Wn,cumprod:()=>Vo,cumsum:()=>ko,deg2rad:()=>Ln,degrees:()=>Rn,delete:()=>zo,diag:()=>it,diagflat:()=>on,diagonal:()=>Bn,divmod:()=>oa,dot:()=>In,dsplit:()=>Do,dstack:()=>to,einsum:()=>ua,empty:()=>at,empty_like:()=>Ke,expand_dims:()=>lo,eye:()=>ot,fabs:()=>na,flip:()=>zr,fliplr:()=>po,flipud:()=>mo,floor_divide:()=>Nn,frombuffer:()=>pn,fromfile:()=>mn,fromfunction:()=>an,fromiter:()=>yn,fromstring:()=>dn,full:()=>st,full_like:()=>rn,geomspace:()=>Ye,heaviside:()=>ia,hsplit:()=>ao,hstack:()=>ro,hypot:()=>Fn,identity:()=>Ge,inner:()=>Un,insert:()=>Bo,kron:()=>_n,linspace:()=>We,loadNpz:()=>mt,loadNpzSync:()=>yt,logspace:()=>He,median:()=>Fo,meshgrid:()=>sn,mod:()=>Dn,moveaxis:()=>Xn,nanargmax:()=>Xo,nanargmin:()=>Qo,nancumprod:()=>ra,nancumsum:()=>Ko,nanmax:()=>Jo,nanmean:()=>Wo,nanmedian:()=>ta,nanmin:()=>Go,nanprod:()=>Zo,nanstd:()=>Yo,nansum:()=>jo,nanvar:()=>Ho,negative:()=>bn,ones:()=>nt,ones_like:()=>Xe,outer:()=>Tn,pad:()=>_o,parseNpy:()=>ur,parseNpyData:()=>Er,parseNpyHeader:()=>_r,parseNpz:()=>Ur,parseNpzSync:()=>Tr,percentile:()=>Ro,positive:()=>wn,power:()=>hn,ptp:()=>Po,put:()=>$o,quantile:()=>qo,rad2deg:()=>jn,radians:()=>qn,ravel:()=>uo,reciprocal:()=>xn,remainder:()=>sa,repeat:()=>io,reshape:()=>co,resize:()=>xo,roll:()=>go,rollaxis:()=>ho,rot90:()=>yo,row_stack:()=>wo,serializeNpy:()=>Q,serializeNpz:()=>dt,serializeNpzSync:()=>gt,sign:()=>Sn,sin:()=>$n,sinh:()=>Zn,split:()=>eo,sqrt:()=>gn,square:()=>aa,squeeze:()=>fo,stack:()=>Kn,swapaxes:()=>Qn,take:()=>Oo,tan:()=>Mn,tanh:()=>Hn,tensordot:()=>On,tile:()=>so,trace:()=>zn,transpose:()=>En,tri:()=>un,tril:()=>cn,triu:()=>fn,vander:()=>ln,vsplit:()=>oo,vstack:()=>ut,zeros:()=>F,zeros_like:()=>Qe});function ht(t){if(!t.includes(":")){if(t.includes("."))throw new Error(`Invalid slice index: "${t}" (must be integer)`);let a=parseInt(t,10);if(isNaN(a))throw new Error(`Invalid slice index: "${t}"`);return{start:a,stop:null,step:1,isIndex:!0}}let r=t.split(":");if(r.length>3)throw new Error(`Invalid slice notation: "${t}" (too many colons)`);let e=r[0]===""?null:parseInt(r[0],10),n=r[1]===""||r[1]===void 0?null:parseInt(r[1],10),o=r[2]===""||r[2]===void 0?1:parseInt(r[2],10);if(e!==null&&isNaN(e))throw new Error(`Invalid start index in slice: "${t}"`);if(n!==null&&isNaN(n))throw new Error(`Invalid stop index in slice: "${t}"`);if(isNaN(o))throw new Error(`Invalid step in slice: "${t}"`);if(o===0)throw new Error("Slice step cannot be zero");return{start:e,stop:n,step:o,isIndex:!1}}function At(t,r){let{start:e,stop:n}=t,{step:o,isIndex:a}=t;if(a){if(e===null)throw new Error("Index cannot be null");let i=e<0?r+e:e;if(i<0||i>=r)throw new Error(`Index ${e} is out of bounds for size ${r}`);return{start:i,stop:i+1,step:1,isIndex:!0}}return o>0?(e===null&&(e=0),n===null&&(n=r)):(e===null&&(e=r-1),n===null&&(n=-r-1)),e<0&&(e=r+e),n<0&&(n=r+n),e=Math.max(0,Math.min(e,r)),n=Math.max(-1,Math.min(n,r)),{start:e,stop:n,step:o,isIndex:!1}}var $="float64";function _(t){switch(t){case"float64":return Float64Array;case"float32":return Float32Array;case"int64":return BigInt64Array;case"int32":return Int32Array;case"int16":return Int16Array;case"int8":return Int8Array;case"uint64":return BigUint64Array;case"uint32":return Uint32Array;case"uint16":return Uint16Array;case"uint8":return Uint8Array;case"bool":return Uint8Array;default:throw new Error(`Unknown dtype: ${t}`)}}function St(t){switch(t){case"float64":case"int64":case"uint64":return 8;case"float32":case"int32":case"uint32":return 4;case"int16":case"uint16":return 2;case"int8":case"uint8":case"bool":return 1;default:throw new Error(`Unknown dtype: ${t}`)}}function bt(t){return t==="float64"||t==="float32"}function N(t){return t==="int64"||t==="uint64"}function v(t,r){if(t===r)return t;if(t==="bool")return r;if(r==="bool")return t;if(bt(t)||bt(r)){if(t==="float64"||r==="float64")return"float64";if(t==="float32"){let c=r;return c==="int32"||c==="int64"||c==="uint32"||c==="uint64"?"float64":"float32"}if(r==="float32"){let c=t;return c==="int32"||c==="int64"||c==="uint32"||c==="uint64"?"float64":"float32"}return"float32"}let e=t.startsWith("int"),n=r.startsWith("int"),o=t.startsWith("uint"),a=r.startsWith("uint"),i=c=>c.includes("64")?64:c.includes("32")?32:c.includes("16")?16:c.includes("8")?8:0,s=i(t),u=i(r);if(t==="int64"&&r==="uint64"||t==="uint64"&&r==="int64")return"float64";if(e&&a&&s===u){if(s===8)return"int16";if(s===16)return"int32";if(s===32)return"int64"}if(o&&n&&s===u){if(u===8)return"int16";if(u===16)return"int32";if(u===32)return"int64"}if(e&&n||o&&a){let c=Math.max(s,u);return e?c===64?"int64":c===32?"int32":c===16?"int16":"int8":c===64?"uint64":c===32?"uint32":c===16?"uint16":"uint8"}return e&&a?s>u?t:u===8?"int16":u===16?"int32":u===32?"int64":"float64":o&&n?u>s?r:s===8?"int16":s===16?"int32":s===32?"int64":"float64":"float64"}var D=class t{constructor(r,e,n,o,a){this._data=r,this._shape=e,this._strides=n,this._offset=o,this._dtype=a}get shape(){return this._shape}get ndim(){return this._shape.length}get size(){return this._shape.reduce((r,e)=>r*e,1)}get dtype(){return this._dtype}get data(){return this._data}get strides(){return this._strides}get offset(){return this._offset}get isCContiguous(){let r=this._shape,e=this._strides,n=r.length;if(n===0)return!0;if(n===1)return e[0]===1;let o=1;for(let a=n-1;a>=0;a--){if(e[a]!==o)return!1;o*=r[a]}return!0}get isFContiguous(){let r=this._shape,e=this._strides,n=r.length;if(n===0)return!0;if(n===1)return e[0]===1;let o=1;for(let a=0;a<n;a++){if(e[a]!==o)return!1;o*=r[a]}return!0}iget(r){let e=this._shape,n=this._strides,o=e.length;if(o===0)return this._data[this._offset];let a=r,i=this._offset;for(let s=0;s<o;s++){let u=1;for(let l=s+1;l<o;l++)u*=e[l];let c=Math.floor(a/u);a=a%u,i+=c*n[s]}return this._data[i]}iset(r,e){let n=this._shape,o=this._strides,a=n.length;if(a===0){this._data[this._offset]=e;return}let i=r,s=this._offset;for(let u=0;u<a;u++){let c=1;for(let m=u+1;m<a;m++)c*=n[m];let l=Math.floor(i/c);i=i%c,s+=l*o[u]}this._data[s]=e}get(...r){let e=this._strides,n=this._offset;for(let o=0;o<r.length;o++)n+=r[o]*e[o];return this._data[n]}set(r,e){let n=this._strides,o=this._offset;for(let a=0;a<r.length;a++)o+=r[a]*n[a];this._data[o]=e}copy(){let r=Array.from(this._shape),e=this._dtype,n=this.size,o=_(e);if(!o)throw new Error(`Cannot copy array with dtype ${e}`);let a=new o(n);if(this.isCContiguous&&this._offset===0)if(N(e)){let i=this._data,s=a;for(let u=0;u<n;u++)s[u]=i[u]}else a.set(this._data);else if(N(e)){let i=a;for(let s=0;s<n;s++)i[s]=this.iget(s)}else for(let i=0;i<n;i++)a[i]=this.iget(i);return new t(a,r,t._computeStrides(r),0,e)}static fromData(r,e,n,o,a){let i=o??t._computeStrides(e),s=a??0;return new t(r,e,i,s,n)}static zeros(r,e=$){let n=r.reduce((i,s)=>i*s,1),o=_(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return new t(a,r,t._computeStrides(r),0,e)}static ones(r,e=$){let n=r.reduce((i,s)=>i*s,1),o=_(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return N(e)?a.fill(BigInt(1)):a.fill(1),new t(a,r,t._computeStrides(r),0,e)}static _computeStrides(r){let e=new Array(r.length),n=1;for(let o=r.length-1;o>=0;o--)e[o]=n,n*=r[o];return e}};function q(t){let r=new Array(t.length),e=1;for(let n=t.length-1;n>=0;n--)r[n]=e,e*=t[n];return r}function Dt(t,r){let e=t.length,n=r.length,o=Math.max(e,n),a=new Array(o);for(let i=0;i<o;i++){let s=i<o-e?1:t[i-(o-e)],u=i<o-n?1:r[i-(o-n)];if(s===u)a[i]=s;else if(s===1)a[i]=u;else if(u===1)a[i]=s;else throw new Error(`operands could not be broadcast together with shapes ${JSON.stringify(Array.from(t))} ${JSON.stringify(Array.from(r))}`)}return a}function _a(t,r,e){let n=t.length,o=e.length,a=new Array(o).fill(0);for(let i=0;i<n;i++){let s=o-n+i,u=t[i],c=e[s];if(u===c)a[s]=r[i];else if(u===1)a[s]=0;else throw new Error("Invalid broadcast")}return a}function lr(t,r){let e=_a(t.shape,t.strides,r);return D.fromData(t.data,Array.from(r),t.dtype,e,t.offset)}function P(t,r,e,n){let o=Dt(t.shape,r.shape),a=lr(t,o),i=lr(r,o),s=v(t.dtype,r.dtype),u=D.zeros(o,s),c=u.data,l=u.size;if(N(s)){let m=c;for(let f=0;f<l;f++){let p=a.iget(f),y=i.iget(f),d=typeof p=="bigint"?p:BigInt(Math.round(p)),g=typeof y=="bigint"?y:BigInt(Math.round(y));n==="add"?m[f]=d+g:n==="subtract"?m[f]=d-g:n==="multiply"?m[f]=d*g:n==="divide"?m[f]=d/g:m[f]=BigInt(Math.round(e(Number(d),Number(g))))}}else{let m=N(t.dtype)||N(r.dtype);for(let f=0;f<l;f++){let p=a.iget(f),y=i.iget(f),d=Number(p),g=Number(y);c[f]=e(d,g)}}return u}function L(t,r,e){let n=Dt(t.shape,r.shape),o=lr(t,n),a=lr(r,n),i=n.reduce((c,l)=>c*l,1),s=new Uint8Array(i),u=N(t.dtype)||N(r.dtype);for(let c=0;c<i;c++){let l=o.iget(c),m=a.iget(c),f=Number(l),p=Number(m);s[c]=e(f,p)?1:0}return D.fromData(s,n,"bool")}function M(t,r,e=!0){let n=t.dtype,o=Array.from(t.shape),a=t.size,s=e?n:n!=="float32"&&n!=="float64"?"float64":n,u=D.zeros(o,s),c=u.data,l=t.data;if(N(n))if(N(s)){let m=c;for(let f=0;f<a;f++){let p=Number(l[f]);m[f]=BigInt(Math.round(r(p)))}}else for(let m=0;m<a;m++)c[m]=r(Number(l[m]));else for(let m=0;m<a;m++)c[m]=r(Number(l[m]));return u}function $r(t,r){return t.isCContiguous&&r.isCContiguous&&t.shape.length===r.shape.length&&t.shape.every((e,n)=>e===r.shape[n])}function Nt(t,r){return typeof r=="number"?Oa(t,r):$r(t,r)?Ea(t,r):P(t,r,(e,n)=>e+n,"add")}function Ea(t,r){let e=v(t.dtype,r.dtype),n=D.zeros(Array.from(t.shape),e),o=t.size,a=t.data,i=r.data,s=n.data;if(N(e)){let u=s;if(!N(t.dtype)||!N(r.dtype))for(let l=0;l<o;l++){let m=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof i[l]=="bigint"?i[l]:BigInt(Math.round(Number(i[l])));u[l]=m+f}else{let l=a,m=i;for(let f=0;f<o;f++)u[f]=l[f]+m[f]}}else if(N(t.dtype)||N(r.dtype))for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?Number(a[c]):a[c],m=typeof i[c]=="bigint"?Number(i[c]):i[c];s[c]=l+m}else for(let c=0;c<o;c++)s[c]=a[c]+i[c];return n}function wt(t,r){return typeof r=="number"?$a(t,r):$r(t,r)?Ua(t,r):P(t,r,(e,n)=>e-n,"subtract")}function Ua(t,r){let e=v(t.dtype,r.dtype),n=D.zeros(Array.from(t.shape),e),o=t.size,a=t.data,i=r.data,s=n.data;if(N(e)){let u=s;if(!N(t.dtype)||!N(r.dtype))for(let l=0;l<o;l++){let m=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof i[l]=="bigint"?i[l]:BigInt(Math.round(Number(i[l])));u[l]=m-f}else{let l=a,m=i;for(let f=0;f<o;f++)u[f]=l[f]-m[f]}}else if(N(t.dtype)||N(r.dtype))for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?Number(a[c]):a[c],m=typeof i[c]=="bigint"?Number(i[c]):i[c];s[c]=l-m}else for(let c=0;c<o;c++)s[c]=a[c]-i[c];return n}function xt(t,r){return typeof r=="number"?va(t,r):$r(t,r)?Ta(t,r):P(t,r,(e,n)=>e*n,"multiply")}function Ta(t,r){let e=v(t.dtype,r.dtype),n=D.zeros(Array.from(t.shape),e),o=t.size,a=t.data,i=r.data,s=n.data;if(N(e)){let u=s;if(!N(t.dtype)||!N(r.dtype))for(let l=0;l<o;l++){let m=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof i[l]=="bigint"?i[l]:BigInt(Math.round(Number(i[l])));u[l]=m*f}else{let l=a,m=i;for(let f=0;f<o;f++)u[f]=l[f]*m[f]}}else if(N(t.dtype)||N(r.dtype))for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?Number(a[c]):a[c],m=typeof i[c]=="bigint"?Number(i[c]):i[c];s[c]=l*m}else for(let c=0;c<o;c++)s[c]=a[c]*i[c];return n}function It(t,r){if(typeof r=="number")return Ma(t,r);let e=t.dtype==="float64",n=r.dtype==="float64",o=t.dtype==="float32",a=r.dtype==="float32";if(e||n){let u=e?t:G(t,"float64"),c=n?r:G(r,"float64");return P(u,c,(l,m)=>l/m,"divide")}if(o||a){let u=o?t:G(t,"float32"),c=a?r:G(r,"float32");return P(u,c,(l,m)=>l/m,"divide")}let i=G(t,"float64"),s=G(r,"float64");return P(i,s,(u,c)=>u/c,"divide")}function G(t,r){let e=D.zeros(Array.from(t.shape),r),n=t.size,o=t.data,a=e.data;for(let i=0;i<n;i++)a[i]=Number(o[i]);return e}function Oa(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=D.zeros(n,e),s=i.data;if(N(e)){let u=o,c=s,l=BigInt(Math.round(r));for(let m=0;m<a;m++)c[m]=u[m]+l}else for(let u=0;u<a;u++)s[u]=Number(o[u])+r;return i}function $a(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=D.zeros(n,e),s=i.data;if(N(e)){let u=o,c=s,l=BigInt(Math.round(r));for(let m=0;m<a;m++)c[m]=u[m]-l}else for(let u=0;u<a;u++)s[u]=Number(o[u])-r;return i}function va(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=D.zeros(n,e),s=i.data;if(N(e)){let u=o,c=s,l=BigInt(Math.round(r));for(let m=0;m<a;m++)c[m]=u[m]*l}else for(let u=0;u<a;u++)s[u]=Number(o[u])*r;return i}function Ma(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,s=e!=="float32"&&e!=="float64"?"float64":e,u=D.zeros(n,s),c=u.data;if(N(e))for(let l=0;l<a;l++)c[l]=Number(o[l])/r;else for(let l=0;l<a;l++)c[l]=Number(o[l])/r;return u}function zt(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,a=D.zeros(e,r),i=a.data;if(N(r)){let s=n,u=i;for(let c=0;c<o;c++){let l=s[c];u[c]=l<0n?-l:l}}else for(let s=0;s<o;s++)i[s]=Math.abs(Number(n[s]));return a}function Bt(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,a=D.zeros(e,r),i=a.data;if(N(r)){let s=n,u=i;for(let c=0;c<o;c++)u[c]=-s[c]}else for(let s=0;s<o;s++)i[s]=-Number(n[s]);return a}function _t(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,a=D.zeros(e,r),i=a.data;if(N(r)){let s=n,u=i;for(let c=0;c<o;c++){let l=s[c];u[c]=l>0n?1n:l<0n?-1n:0n}}else for(let s=0;s<o;s++){let u=Number(n[s]);i[s]=u>0?1:u<0?-1:0}return a}function pr(t,r){return typeof r=="number"?Ca(t,r):P(t,r,(e,n)=>(e%n+n)%n,"mod")}function Ca(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=D.zeros(n,e),s=i.data;if(N(e)){let u=o,c=s,l=BigInt(Math.round(r));for(let m=0;m<a;m++){let f=u[m];c[m]=(f%l+l)%l}}else for(let u=0;u<a;u++){let c=Number(o[u]);s[u]=(c%r+r)%r}return i}function vr(t,r){return typeof r=="number"?ka(t,r):P(t,r,(e,n)=>Math.floor(e/n),"floor_divide")}function ka(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=D.zeros(n,e),s=i.data;if(N(e)){let u=o,c=s,l=BigInt(Math.round(r));for(let m=0;m<a;m++)c[m]=u[m]/l}else for(let u=0;u<a;u++)s[u]=Math.floor(Number(o[u])/r);return i}function Et(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,a=D.zeros(e,r),i=a.data;for(let s=0;s<o;s++)i[s]=n[s];return a}function Ut(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,i=r!=="float32"&&r!=="float64"?"float64":r,s=D.zeros(e,i),u=s.data;if(N(r))for(let c=0;c<o;c++)u[c]=1/Number(n[c]);else for(let c=0;c<o;c++)u[c]=1/Number(n[c]);return s}function Tt(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,i=r!=="float32"&&r!=="float64"?"float64":r,s=D.zeros(e,i),u=s.data;for(let c=0;c<o;c++)u[c]=Math.cbrt(Number(n[c]));return s}function Ot(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,a=r==="float32"?"float32":"float64",i=D.zeros(e,a),s=i.data;for(let u=0;u<o;u++)s[u]=Math.abs(Number(n[u]));return i}function $t(t,r){let e=vr(t,r),n=pr(t,r);return[e,n]}function vt(t){let r=t.dtype,e=Array.from(t.shape),n=t.data,o=t.size,a=D.zeros(e,r),i=a.data;if(N(r)){let s=n,u=i;for(let c=0;c<o;c++)u[c]=s[c]*s[c]}else for(let s=0;s<o;s++){let u=Number(n[s]);i[s]=u*u}return a}function Mt(t,r){return pr(t,r)}function Ct(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.size,a=e==="float32"?"float32":"float64",i=D.zeros(n,a),s=i.data;if(typeof r=="number")for(let u=0;u<o;u++){let c=Number(t.data[u]);c<0?s[u]=0:c===0?s[u]=r:s[u]=1}else{let u=r.data,c=r.shape;if(n.every((l,m)=>l===c[m]))for(let l=0;l<o;l++){let m=Number(t.data[l]);m<0?s[l]=0:m===0?s[l]=Number(u[l]):s[l]=1}else for(let l=0;l<o;l++){let m=Number(t.data[l]),f=l%r.size;m<0?s[l]=0:m===0?s[l]=Number(u[f]):s[l]=1}}return i}function Z(t){if(t.length===0)return[];if(t.length===1)return Array.from(t[0]);let r=Math.max(...t.map(n=>n.length)),e=new Array(r);for(let n=0;n<r;n++){let o=1;for(let a of t){let i=a.length-r+n,s=i<0?1:a[i];if(s!==1){if(o===1)o=s;else if(o!==s)return null}}e[n]=o}return e}function Pa(t,r,e){let n=t.length,o=e.length,a=new Array(o).fill(0);for(let i=0;i<n;i++){let s=o-n+i,u=t[i],c=e[s];if(u===c)a[s]=r[i];else if(u===1)a[s]=0;else throw new Error("Invalid broadcast")}return a}function j(t,r){let e=Pa(t.shape,t.strides,r);return D.fromData(t.data,Array.from(r),t.dtype,e,t.offset)}function Mr(...t){let r=Z(t);if(r===null){let e=t.map(n=>`(${n.join(",")})`).join(" ");throw new Error(`shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between ${e}`)}return r}function kt(t,r){return typeof r=="number"?Fa(t,r):L(t,r,(e,n)=>e>n)}function Vt(t,r){return typeof r=="number"?Ra(t,r):L(t,r,(e,n)=>e>=n)}function Pt(t,r){return typeof r=="number"?qa(t,r):L(t,r,(e,n)=>e<n)}function Ft(t,r){return typeof r=="number"?La(t,r):L(t,r,(e,n)=>e<=n)}function Rt(t,r){return typeof r=="number"?ja(t,r):L(t,r,(e,n)=>e===n)}function qt(t,r){return typeof r=="number"?Za(t,r):L(t,r,(e,n)=>e!==n)}function Cr(t,r,e=1e-5,n=1e-8){return typeof r=="number"?Wa(t,r,e,n):L(t,r,(o,a)=>{let i=Math.abs(o-a),s=n+e*Math.abs(a);return i<=s})}function Lt(t,r,e=1e-5,n=1e-8){let o=Cr(t,r,e,n),a=o.data;for(let i=0;i<o.size;i++)if(a[i]===0)return!1;return!0}function jt(t,r){let e=[Array.from(t.shape),Array.from(r.shape)],n=Z(e);if(n===null)return!1;let o=j(t,n),a=j(r,n),i=n.length,s=n.reduce((l,m)=>l*m,1),u=N(o.dtype),c=N(a.dtype);for(let l=0;l<s;l++){let m=l,f=new Array(i);for(let d=i-1;d>=0;d--)f[d]=m%n[d],m=Math.floor(m/n[d]);let p=o.get(...f),y=a.get(...f);if(u||c){let d=typeof p=="bigint"?p:BigInt(Number(p)),g=typeof y=="bigint"?y:BigInt(Number(y));if(d!==g)return!1}else if(p!==y)return!1}return!0}function Fa(t,r){let e=new Uint8Array(t.size),n=t.data;for(let o=0;o<t.size;o++)e[o]=n[o]>r?1:0;return D.fromData(e,Array.from(t.shape),"bool")}function Ra(t,r){let e=new Uint8Array(t.size),n=t.data;for(let o=0;o<t.size;o++)e[o]=n[o]>=r?1:0;return D.fromData(e,Array.from(t.shape),"bool")}function qa(t,r){let e=new Uint8Array(t.size),n=t.data;for(let o=0;o<t.size;o++)e[o]=n[o]<r?1:0;return D.fromData(e,Array.from(t.shape),"bool")}function La(t,r){let e=new Uint8Array(t.size),n=t.data;for(let o=0;o<t.size;o++)e[o]=n[o]<=r?1:0;return D.fromData(e,Array.from(t.shape),"bool")}function ja(t,r){let e=new Uint8Array(t.size),n=t.data,o=t.dtype;if(N(o)){let a=BigInt(Math.round(r)),i=n;for(let s=0;s<t.size;s++)e[s]=i[s]===a?1:0}else for(let a=0;a<t.size;a++)e[a]=n[a]===r?1:0;return D.fromData(e,Array.from(t.shape),"bool")}function Za(t,r){let e=new Uint8Array(t.size),n=t.data;for(let o=0;o<t.size;o++)e[o]=n[o]!==r?1:0;return D.fromData(e,Array.from(t.shape),"bool")}function Wa(t,r,e,n){let o=new Uint8Array(t.size),a=t.data,i=t.dtype;if(N(i)){let s=a;for(let u=0;u<t.size;u++){let c=Number(s[u]),l=Math.abs(c-r),m=n+e*Math.abs(r);o[u]=l<=m?1:0}}else for(let s=0;s<t.size;s++){let u=Number(a[s]),c=Math.abs(u-r),l=n+e*Math.abs(r);o[s]=c<=l?1:0}return D.fromData(o,Array.from(t.shape),"bool")}function U(t,r){let e=0,n=1;for(let o=t.length-1;o>=0;o--)e+=t[o]*n,n*=r[o];return e}function T(t,r,e,n){let o=n.length,a=new Array(o),i=Array.from(n).filter((u,c)=>c!==r),s=t;for(let u=i.length-1;u>=0;u--)a[u>=r?u+1:u]=s%i[u],s=Math.floor(s/i[u]);return a[r]=e,a}function K(t,r,e=!1){let n=t.dtype,o=t.shape,a=o.length,i=t.size,s=t.data;if(r===void 0)if(N(n)){let y=s,d=BigInt(0);for(let g=0;g<i;g++)d+=y[g];return Number(d)}else{let y=0;for(let d=0;d<i;d++)y+=Number(s[d]);return y}let u=r;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((y,d)=>d!==u);if(c.length===0)return K(t);let l=D.zeros(c,n),m=l.data,f=o[u],p=c.reduce((y,d)=>y*d,1);if(N(n)){let y=s,d=m;for(let g=0;g<p;g++){let A=BigInt(0);for(let h=0;h<f;h++){let b=T(g,u,h,o),S=U(b,o);A+=y[S]}d[g]=A}}else for(let y=0;y<p;y++){let d=0;for(let g=0;g<f;g++){let A=T(y,u,g,o),h=U(A,o);d+=Number(s[h])}m[y]=d}if(e){let y=[...o];return y[u]=1,D.fromData(m,y,n)}return l}function mr(t,r,e=!1){let n=t.dtype,o=t.shape;if(r===void 0)return K(t)/t.size;let a=r;if(a<0&&(a=o.length+a),a<0||a>=o.length)throw new Error(`axis ${r} is out of bounds for array of dimension ${o.length}`);let i=K(t,r,e);if(typeof i=="number")return i/o[a];let s=o[a],u=n;(N(n)||n.startsWith("int")||n.startsWith("uint"))&&(u="float64");let c=D.zeros(Array.from(i.shape),u),l=c.data,m=i.data;if(N(n)){let f=m;for(let p=0;p<l.length;p++)l[p]=Number(f[p])/s}else for(let f=0;f<l.length;f++)l[f]=Number(m[f])/s;return c}function yr(t,r,e=!1){let n=t.dtype,o=t.shape,a=o.length,i=t.size,s=t.data;if(r===void 0){if(i===0)throw new Error("max of empty array");let y=s[0];for(let d=1;d<i;d++)s[d]>y&&(y=s[d]);return Number(y)}let u=r;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((y,d)=>d!==u);if(c.length===0)return yr(t);let l=D.zeros(c,n),m=l.data,f=o[u],p=c.reduce((y,d)=>y*d,1);if(N(n)){let y=s,d=m;for(let g=0;g<p;g++){let A=T(g,u,0,o),h=U(A,o),b=y[h];for(let S=1;S<f;S++){let w=T(g,u,S,o),x=U(w,o),z=y[x];z>b&&(b=z)}d[g]=b}}else for(let y=0;y<p;y++){let d=-1/0;for(let g=0;g<f;g++){let A=T(y,u,g,o),h=U(A,o),b=Number(s[h]);b>d&&(d=b)}m[y]=d}if(e){let y=[...o];return y[u]=1,D.fromData(m,y,n)}return l}function kr(t,r,e=!1){let n=t.dtype,o=t.shape,a=o.length,i=t.size,s=t.data;if(r===void 0)if(N(n)){let y=s,d=BigInt(1);for(let g=0;g<i;g++)d*=y[g];return Number(d)}else{let y=1;for(let d=0;d<i;d++)y*=Number(s[d]);return y}let u=r;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((y,d)=>d!==u);if(c.length===0)return kr(t);let l=D.zeros(c,n),m=l.data,f=o[u],p=c.reduce((y,d)=>y*d,1);if(N(n)){let y=s,d=m;for(let g=0;g<p;g++){let A=BigInt(1);for(let h=0;h<f;h++){let b=T(g,u,h,o),S=U(b,o);A*=y[S]}d[g]=A}}else for(let y=0;y<p;y++){let d=1;for(let g=0;g<f;g++){let A=T(y,u,g,o),h=U(A,o);d*=Number(s[h])}m[y]=d}if(e){let y=[...o];return y[u]=1,D.fromData(m,y,n)}return l}function dr(t,r,e=!1){let n=t.dtype,o=t.shape,a=o.length,i=t.size,s=t.data;if(r===void 0){if(i===0)throw new Error("min of empty array");let y=s[0];for(let d=1;d<i;d++)s[d]<y&&(y=s[d]);return Number(y)}let u=r;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((y,d)=>d!==u);if(c.length===0)return dr(t);let l=D.zeros(c,n),m=l.data,f=o[u],p=c.reduce((y,d)=>y*d,1);if(N(n)){let y=s,d=m;for(let g=0;g<p;g++){let A=T(g,u,0,o),h=U(A,o),b=y[h];for(let S=1;S<f;S++){let w=T(g,u,S,o),x=U(w,o),z=y[x];z<b&&(b=z)}d[g]=b}}else for(let y=0;y<p;y++){let d=1/0;for(let g=0;g<f;g++){let A=T(y,u,g,o),h=U(A,o),b=Number(s[h]);b<d&&(d=b)}m[y]=d}if(e){let y=[...o];return y[u]=1,D.fromData(m,y,n)}return l}function Vr(t,r){let e=t.dtype,n=t.shape,o=n.length,a=t.size,i=t.data;if(r===void 0){if(a===0)throw new Error("argmin of empty array");let p=i[0],y=0;for(let d=1;d<a;d++)i[d]<p&&(p=i[d],y=d);return y}let s=r;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((p,y)=>y!==s);if(u.length===0)return Vr(t);let c=D.zeros(u,"int32"),l=c.data,m=n[s],f=u.reduce((p,y)=>p*y,1);if(N(e)){let p=i;for(let y=0;y<f;y++){let d=T(y,s,0,n),g=U(d,n),A=p[g],h=0;for(let b=1;b<m;b++){let S=T(y,s,b,n),w=U(S,n),x=p[w];x<A&&(A=x,h=b)}l[y]=h}}else for(let p=0;p<f;p++){let y=1/0,d=0;for(let g=0;g<m;g++){let A=T(p,s,g,n),h=U(A,n),b=Number(i[h]);b<y&&(y=b,d=g)}l[p]=d}return c}function Pr(t,r){let e=t.dtype,n=t.shape,o=n.length,a=t.size,i=t.data;if(r===void 0){if(a===0)throw new Error("argmax of empty array");let p=i[0],y=0;for(let d=1;d<a;d++)i[d]>p&&(p=i[d],y=d);return y}let s=r;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((p,y)=>y!==s);if(u.length===0)return Pr(t);let c=D.zeros(u,"int32"),l=c.data,m=n[s],f=u.reduce((p,y)=>p*y,1);if(N(e)){let p=i;for(let y=0;y<f;y++){let d=T(y,s,0,n),g=U(d,n),A=p[g],h=0;for(let b=1;b<m;b++){let S=T(y,s,b,n),w=U(S,n),x=p[w];x>A&&(A=x,h=b)}l[y]=h}}else for(let p=0;p<f;p++){let y=-1/0,d=0;for(let g=0;g<m;g++){let A=T(p,s,g,n),h=U(A,n),b=Number(i[h]);b>y&&(y=b,d=g)}l[p]=d}return c}function Fr(t,r,e=0,n=!1){let o=t.shape,a=o.length,i=t.size,s=t.data,u=mr(t,r,n);if(r===void 0){let A=u,h=0;for(let b=0;b<i;b++){let S=Number(s[b])-A;h+=S*S}return h/(i-e)}let c=r;if(c<0&&(c=a+c),c<0||c>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let l=o[c],m=u,f=m.data,p=n?m.shape:Array.from(o).filter((A,h)=>h!==c),y=D.zeros(Array.from(p),"float64"),d=y.data,g=p.reduce((A,h)=>A*h,1);for(let A=0;A<g;A++){let h=0,b=Number(f[A]);for(let S=0;S<l;S++){let w=T(A,c,S,o),x=U(w,o),z=Number(s[x])-b;h+=z*z}d[A]=h/(l-e)}return y}function Zt(t,r,e=0,n=!1){let o=Fr(t,r,e,n);if(typeof o=="number")return Math.sqrt(o);let a=D.zeros(Array.from(o.shape),"float64"),i=o.data,s=a.data;for(let u=0;u<i.length;u++)s[u]=Math.sqrt(Number(i[u]));return a}function Rr(t,r,e=!1){let n=t.shape,o=n.length,a=t.size,i=t.data;if(r===void 0){for(let p=0;p<a;p++)if(!i[p])return!1;return!0}let s=r;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((p,y)=>y!==s);if(u.length===0)return Rr(t);let c=D.zeros(u,"bool"),l=c.data,m=n[s],f=u.reduce((p,y)=>p*y,1);for(let p=0;p<f;p++){let y=!0;for(let d=0;d<m;d++){let g=T(p,s,d,n),A=U(g,n);if(!i[A]){y=!1;break}}l[p]=y?1:0}if(e){let p=[...n];return p[s]=1,D.fromData(l,p,"bool")}return c}function qr(t,r,e=!1){let n=t.shape,o=n.length,a=t.size,i=t.data;if(r===void 0){for(let p=0;p<a;p++)if(i[p])return!0;return!1}let s=r;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((p,y)=>y!==s);if(u.length===0)return qr(t);let c=D.zeros(u,"bool"),l=c.data,m=n[s],f=u.reduce((p,y)=>p*y,1);for(let p=0;p<f;p++){let y=!1;for(let d=0;d<m;d++){let g=T(p,s,d,n),A=U(g,n);if(i[A]){y=!0;break}}l[p]=y?1:0}if(e){let p=[...n];return p[s]=1,D.fromData(l,p,"bool")}return c}function Lr(t,r){let e=t.shape,n=e.length,o=t.data;if(r===void 0){let f=t.size,p=new Float64Array(f),y=0;for(let d=0;d<f;d++)y+=Number(o[d]),p[d]=y;return D.fromData(p,[f],"float64")}let a=r;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let i=new Float64Array(t.size),s=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=t.size,m=u[a];for(let f=0;f<l;f++)Math.floor(f/m)%s===0?i[f]=Number(o[f]):i[f]=i[f-m]+Number(o[f]);return D.fromData(i,[...e],"float64")}function jr(t,r){let e=t.shape,n=e.length,o=t.data;if(r===void 0){let f=t.size,p=new Float64Array(f),y=1;for(let d=0;d<f;d++)y*=Number(o[d]),p[d]=y;return D.fromData(p,[f],"float64")}let a=r;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let i=new Float64Array(t.size),s=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=t.size,m=u[a];for(let f=0;f<l;f++)Math.floor(f/m)%s===0?i[f]=Number(o[f]):i[f]=i[f-m]*Number(o[f]);return D.fromData(i,[...e],"float64")}function Zr(t,r,e=!1){let n=yr(t,r,e),o=dr(t,r,e);if(typeof n=="number"&&typeof o=="number")return n-o;let a=n,i=o,s=a.data,u=i.data,c=new Float64Array(a.size);for(let l=0;l<a.size;l++)c[l]=Number(s[l])-Number(u[l]);return D.fromData(c,[...a.shape],"float64")}function Wr(t,r,e=!1){return J(t,.5,r,e)}function Hr(t,r,e,n=!1){return J(t,r/100,e,n)}function J(t,r,e,n=!1){if(r<0||r>1)throw new Error("Quantile must be between 0 and 1");let o=t.shape,a=o.length,i=t.data;if(e===void 0){let p=[];for(let b=0;b<t.size;b++)p.push(Number(i[b]));p.sort((b,S)=>b-S);let y=p.length,d=r*(y-1),g=Math.floor(d),A=Math.ceil(d);if(g===A)return p[g];let h=d-g;return p[g]*(1-h)+p[A]*h}let s=e;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${e} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((p,y)=>y!==s);if(u.length===0)return J(t,r);let c=u.reduce((p,y)=>p*y,1),l=o[s],m=new Float64Array(c);for(let p=0;p<c;p++){let y=[];for(let b=0;b<l;b++){let S=T(p,s,b,o),w=U(S,o);y.push(Number(i[w]))}y.sort((b,S)=>b-S);let d=y.length,g=r*(d-1),A=Math.floor(g),h=Math.ceil(g);if(A===h)m[p]=y[A];else{let b=g-A;m[p]=y[A]*(1-b)+y[h]*b}}let f=D.fromData(m,u,"float64");if(n){let p=[...o];return p[s]=1,D.fromData(m,p,"float64")}return f}function gr(t,r,e,n=!1){let o=t.shape,a=o.length,i=t.data;if(e===void 0)return mr(t,r,n);if(r===void 0){let y=0,d=0,g=e.data;for(let A=0;A<t.size;A++){let h=Number(g[A%e.size]);y+=Number(i[A])*h,d+=h}return d===0?NaN:y/d}let s=r;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((y,d)=>d!==s);if(u.length===0)return gr(t,void 0,e);let c=u.reduce((y,d)=>y*d,1),l=o[s],m=e.data,f=new Float64Array(c);for(let y=0;y<c;y++){let d=0,g=0;for(let A=0;A<l;A++){let h=T(y,s,A,o),b=U(h,o),S=Number(m[A%e.size]);d+=Number(i[b])*S,g+=S}f[y]=g===0?NaN:d/g}let p=D.fromData(f,u,"float64");if(n){let y=[...o];return y[s]=1,D.fromData(f,y,"float64")}return p}function hr(t,r,e=!1){let n=t.shape,o=n.length,a=t.data;if(r===void 0){let f=0;for(let p=0;p<t.size;p++){let y=Number(a[p]);isNaN(y)||(f+=y)}return f}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((f,p)=>p!==i);if(s.length===0)return hr(t);let u=s.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(u);for(let f=0;f<u;f++){let p=0;for(let y=0;y<c;y++){let d=T(f,i,y,n),g=U(d,n),A=Number(a[g]);isNaN(A)||(p+=A)}l[f]=p}let m=D.fromData(l,s,"float64");if(e){let f=[...n];return f[i]=1,D.fromData(l,f,"float64")}return m}function Ar(t,r,e=!1){let n=t.shape,o=n.length,a=t.data;if(r===void 0){let f=1;for(let p=0;p<t.size;p++){let y=Number(a[p]);isNaN(y)||(f*=y)}return f}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((f,p)=>p!==i);if(s.length===0)return Ar(t);let u=s.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(u);for(let f=0;f<u;f++){let p=1;for(let y=0;y<c;y++){let d=T(f,i,y,n),g=U(d,n),A=Number(a[g]);isNaN(A)||(p*=A)}l[f]=p}let m=D.fromData(l,s,"float64");if(e){let f=[...n];return f[i]=1,D.fromData(l,f,"float64")}return m}function br(t,r,e=!1){let n=t.shape,o=n.length,a=t.data;if(r===void 0){let f=0,p=0;for(let y=0;y<t.size;y++){let d=Number(a[y]);isNaN(d)||(f+=d,p++)}return p===0?NaN:f/p}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((f,p)=>p!==i);if(s.length===0)return br(t);let u=s.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(u);for(let f=0;f<u;f++){let p=0,y=0;for(let d=0;d<c;d++){let g=T(f,i,d,n),A=U(g,n),h=Number(a[A]);isNaN(h)||(p+=h,y++)}l[f]=y===0?NaN:p/y}let m=D.fromData(l,s,"float64");if(e){let f=[...n];return f[i]=1,D.fromData(l,f,"float64")}return m}function rr(t,r,e=0,n=!1){let o=t.shape,a=o.length,i=t.data;if(r===void 0){let p=0,y=0;for(let A=0;A<t.size;A++){let h=Number(i[A]);isNaN(h)||(p+=h,y++)}if(y-e<=0)return NaN;let d=p/y,g=0;for(let A=0;A<t.size;A++){let h=Number(i[A]);isNaN(h)||(g+=(h-d)**2)}return g/(y-e)}let s=r;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((p,y)=>y!==s);if(u.length===0)return rr(t,void 0,e);let c=u.reduce((p,y)=>p*y,1),l=o[s],m=new Float64Array(c);for(let p=0;p<c;p++){let y=0,d=0;for(let h=0;h<l;h++){let b=T(p,s,h,o),S=U(b,o),w=Number(i[S]);isNaN(w)||(y+=w,d++)}if(d-e<=0){m[p]=NaN;continue}let g=y/d,A=0;for(let h=0;h<l;h++){let b=T(p,s,h,o),S=U(b,o),w=Number(i[S]);isNaN(w)||(A+=(w-g)**2)}m[p]=A/(d-e)}let f=D.fromData(m,u,"float64");if(n){let p=[...o];return p[s]=1,D.fromData(m,p,"float64")}return f}function Yr(t,r,e=0,n=!1){let o=rr(t,r,e,n);if(typeof o=="number")return Math.sqrt(o);let a=o,i=new Float64Array(a.size);for(let s=0;s<a.size;s++)i[s]=Math.sqrt(Number(a.data[s]));return D.fromData(i,[...a.shape],"float64")}function Sr(t,r,e=!1){let n=t.shape,o=n.length,a=t.data;if(r===void 0){let f=1/0;for(let p=0;p<t.size;p++){let y=Number(a[p]);!isNaN(y)&&y<f&&(f=y)}return f===1/0?NaN:f}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((f,p)=>p!==i);if(s.length===0)return Sr(t);let u=s.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(u);for(let f=0;f<u;f++){let p=1/0;for(let y=0;y<c;y++){let d=T(f,i,y,n),g=U(d,n),A=Number(a[g]);!isNaN(A)&&A<p&&(p=A)}l[f]=p===1/0?NaN:p}let m=D.fromData(l,s,"float64");if(e){let f=[...n];return f[i]=1,D.fromData(l,f,"float64")}return m}function Dr(t,r,e=!1){let n=t.shape,o=n.length,a=t.data;if(r===void 0){let f=-1/0;for(let p=0;p<t.size;p++){let y=Number(a[p]);!isNaN(y)&&y>f&&(f=y)}return f===-1/0?NaN:f}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((f,p)=>p!==i);if(s.length===0)return Dr(t);let u=s.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(u);for(let f=0;f<u;f++){let p=-1/0;for(let y=0;y<c;y++){let d=T(f,i,y,n),g=U(d,n),A=Number(a[g]);!isNaN(A)&&A>p&&(p=A)}l[f]=p===-1/0?NaN:p}let m=D.fromData(l,s,"float64");if(e){let f=[...n];return f[i]=1,D.fromData(l,f,"float64")}return m}function Nr(t,r){let e=t.shape,n=e.length,o=t.data;if(r===void 0){let l=1/0,m=-1;for(let f=0;f<t.size;f++){let p=Number(o[f]);!isNaN(p)&&p<l&&(l=p,m=f)}return m}let a=r;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let i=Array.from(e).filter((l,m)=>m!==a);if(i.length===0)return Nr(t);let s=i.reduce((l,m)=>l*m,1),u=e[a],c=new Int32Array(s);for(let l=0;l<s;l++){let m=1/0,f=0;for(let p=0;p<u;p++){let y=T(l,a,p,e),d=U(y,e),g=Number(o[d]);!isNaN(g)&&g<m&&(m=g,f=p)}c[l]=f}return D.fromData(c,i,"int32")}function wr(t,r){let e=t.shape,n=e.length,o=t.data;if(r===void 0){let l=-1/0,m=-1;for(let f=0;f<t.size;f++){let p=Number(o[f]);!isNaN(p)&&p>l&&(l=p,m=f)}return m}let a=r;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let i=Array.from(e).filter((l,m)=>m!==a);if(i.length===0)return wr(t);let s=i.reduce((l,m)=>l*m,1),u=e[a],c=new Int32Array(s);for(let l=0;l<s;l++){let m=-1/0,f=0;for(let p=0;p<u;p++){let y=T(l,a,p,e),d=U(y,e),g=Number(o[d]);!isNaN(g)&&g>m&&(m=g,f=p)}c[l]=f}return D.fromData(c,i,"int32")}function Gr(t,r){let e=t.shape,n=e.length,o=t.data;if(r===void 0){let f=t.size,p=new Float64Array(f),y=0;for(let d=0;d<f;d++){let g=Number(o[d]);isNaN(g)||(y+=g),p[d]=y}return D.fromData(p,[f],"float64")}let a=r;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let i=new Float64Array(t.size),s=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=t.size,m=u[a];for(let f=0;f<l;f++){let p=Number(o[f]);Math.floor(f/m)%s===0?i[f]=isNaN(p)?0:p:i[f]=i[f-m]+(isNaN(p)?0:p)}return D.fromData(i,[...e],"float64")}function Jr(t,r){let e=t.shape,n=e.length,o=t.data;if(r===void 0){let f=t.size,p=new Float64Array(f),y=1;for(let d=0;d<f;d++){let g=Number(o[d]);isNaN(g)||(y*=g),p[d]=y}return D.fromData(p,[f],"float64")}let a=r;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let i=new Float64Array(t.size),s=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=t.size,m=u[a];for(let f=0;f<l;f++){let p=Number(o[f]);Math.floor(f/m)%s===0?i[f]=isNaN(p)?1:p:i[f]=i[f-m]*(isNaN(p)?1:p)}return D.fromData(i,[...e],"float64")}function xr(t,r,e=!1){let n=t.shape,o=n.length,a=t.data;if(r===void 0){let f=[];for(let d=0;d<t.size;d++){let g=Number(a[d]);isNaN(g)||f.push(g)}if(f.length===0)return NaN;f.sort((d,g)=>d-g);let p=f.length,y=Math.floor(p/2);return p%2===0?(f[y-1]+f[y])/2:f[y]}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((f,p)=>p!==i);if(s.length===0)return xr(t);let u=s.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(u);for(let f=0;f<u;f++){let p=[];for(let g=0;g<c;g++){let A=T(f,i,g,n),h=U(A,n),b=Number(a[h]);isNaN(b)||p.push(b)}if(p.length===0){l[f]=NaN;continue}p.sort((g,A)=>g-A);let y=p.length,d=Math.floor(y/2);y%2===0?l[f]=(p[d-1]+p[d])/2:l[f]=p[d]}let m=D.fromData(l,s,"float64");if(e){let f=[...n];return f[i]=1,D.fromData(l,f,"float64")}return m}function V(t,r){let e=t.size,n=t.dtype,o=r.indexOf(-1),a;if(o!==-1){let c=r.reduce((m,f,p)=>p===o?m:m*f,1),l=e/c;if(!Number.isInteger(l))throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(r)}`);a=r.map((m,f)=>f===o?l:m)}else a=r;if(a.reduce((c,l)=>c*l,1)!==e)throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(a)}`);if(t.isCContiguous){let c=t.data;return D.fromData(c,a,n,q(a),0)}let u=t.copy().data;return D.fromData(u,a,n,q(a),0)}function Ir(t){let r=t.size,e=t.dtype,n=_(e);if(!n)throw new Error(`Cannot flatten array with dtype ${e}`);if(t.isCContiguous){let s=t.data.slice(t.offset,t.offset+r);return D.fromData(s,[r],e,[1],0)}let o=new n(r),a=N(e);for(let i=0;i<r;i++){let s=t.iget(i);o[i]=s}return D.fromData(o,[r],e,[1],0)}function er(t){let r=t.size,e=t.dtype;if(t.isCContiguous){let n=t.data;return D.fromData(n,[r],e,[1],0)}return Ir(t)}function nr(t,r){let e=t.shape,n=e.length,o=t.strides,a=t.data,i=t.dtype,s;if(r===void 0)s=Array.from({length:n},(m,f)=>n-1-f);else{if(r.length!==n)throw new Error(`axes must have length ${n}, got ${r.length}`);let m=new Set;for(let f of r){let p=f<0?n+f:f;if(p<0||p>=n)throw new Error(`axis ${f} is out of bounds for array of dimension ${n}`);if(m.has(p))throw new Error("repeated axis in transpose");m.add(p)}s=r.map(f=>f<0?n+f:f)}let u=s.map(m=>e[m]),c=Array.from(o),l=s.map(m=>c[m]);return D.fromData(a,u,i,l,t.offset)}function Wt(t,r){let e=t.shape,n=e.length,o=t.strides,a=t.data,i=t.dtype;if(r===void 0){let s=[],u=[];for(let c=0;c<n;c++)e[c]!==1&&(s.push(e[c]),u.push(o[c]));return s.length===0&&(s.push(1),u.push(1)),D.fromData(a,s,i,u,t.offset)}else{let s=r<0?n+r:r;if(s<0||s>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);if(e[s]!==1)throw new Error(`cannot select an axis which has size not equal to one (axis ${r} has size ${e[s]})`);let u=[],c=[];for(let l=0;l<n;l++)l!==s&&(u.push(e[l]),c.push(o[l]));return D.fromData(a,u,i,c,t.offset)}}function tr(t,r){let e=t.shape,n=e.length,o=t.strides,a=t.data,i=t.dtype,s=r;if(s<0&&(s=n+r+1),s<0||s>n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n+1}`);let u=[...Array.from(e)];u.splice(s,0,1);let c=[...Array.from(o)],l=s<n?o[s]*(e[s]||1):1;return c.splice(s,0,l),D.fromData(a,u,i,c,t.offset)}function Ht(t,r,e){let n=t.shape,o=n.length,a=t.strides,i=t.data,s=t.dtype,u=r<0?o+r:r,c=e<0?o+e:e;if(u<0||u>=o)throw new Error(`axis1 ${r} is out of bounds for array of dimension ${o}`);if(c<0||c>=o)throw new Error(`axis2 ${e} is out of bounds for array of dimension ${o}`);if(u===c)return D.fromData(i,Array.from(n),s,Array.from(a),t.offset);let l=Array.from(n),m=Array.from(a);return[l[u],l[c]]=[l[c],l[u]],[m[u],m[c]]=[m[c],m[u]],D.fromData(i,l,s,m,t.offset)}function Qr(t,r,e){let n=t.ndim,o=Array.isArray(r)?r:[r],a=Array.isArray(e)?e:[e];if(o.length!==a.length)throw new Error("source and destination must have the same number of elements");let i=o.map(c=>{let l=c<0?n+c:c;if(l<0||l>=n)throw new Error(`source axis ${c} is out of bounds for array of dimension ${n}`);return l}),s=a.map(c=>{let l=c<0?n+c:c;if(l<0||l>=n)throw new Error(`destination axis ${c} is out of bounds for array of dimension ${n}`);return l});if(new Set(i).size!==i.length)throw new Error("repeated axis in source");if(new Set(s).size!==s.length)throw new Error("repeated axis in destination");let u=[];for(let c=0;c<n;c++)i.includes(c)||u.push(c);for(let c=0;c<i.length;c++){let l=s[c];u.splice(l,0,i[c])}return nr(t,u)}function W(t,r=0){if(t.length===0)throw new Error("need at least one array to concatenate");if(t.length===1)return t[0].copy();let e=t[0],n=e.ndim,o=e.dtype,a=r<0?n+r:r;if(a<0||a>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);for(let p=1;p<t.length;p++){let y=t[p];if(y.ndim!==n)throw new Error("all the input arrays must have same number of dimensions");for(let d=0;d<n;d++)if(d!==a&&y.shape[d]!==e.shape[d])throw new Error("all the input array dimensions except for the concatenation axis must match exactly")}let i=Array.from(e.shape),s=e.shape[a];for(let p=1;p<t.length;p++)s+=t[p].shape[a];i[a]=s;let u=i.reduce((p,y)=>p*y,1),c=_(o);if(!c)throw new Error(`Cannot concatenate arrays with dtype ${o}`);let l=new c(u),m=q(i),f=0;for(let p of t){let y=p.shape[a];Ga(p,l,i,m,a,f,o),f+=y}return D.fromData(l,i,o)}function Ga(t,r,e,n,o,a,i){let s=t.shape,u=s.length,c=t.size,l=i==="int64"||i==="uint64";if(o===0&&t.isCContiguous&&u>0){let p=a*n[0],y=t.data,d=t.offset,g=d+c;r.set(y.subarray(d,g),p);return}if(o===1&&u===2&&t.isCContiguous){let p=s[0],y=s[1],d=e[1],g=t.data,A=t.offset;for(let h=0;h<p;h++){let b=A+h*y,S=h*d+a;r.set(g.subarray(b,b+y),S)}return}let m=new Array(u).fill(0),f=a*n[o];for(let p=0;p<c;p++){let y=t.iget(p),d=f;for(let g=0;g<u;g++)d+=m[g]*n[g];r[d]=y;for(let g=u-1;g>=0&&(m[g]++,!(m[g]<s[g]));g--)m[g]=0}}function Yt(t,r=0){if(t.length===0)throw new Error("need at least one array to stack");let e=t[0],n=e.shape,o=e.ndim,a=r<0?o+1+r:r;if(a<0||a>o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o+1}`);for(let s=1;s<t.length;s++){let u=t[s];if(u.ndim!==o)throw new Error("all input arrays must have the same shape");for(let c=0;c<o;c++)if(u.shape[c]!==n[c])throw new Error("all input arrays must have the same shape")}let i=t.map(s=>tr(s,a));return W(i,a)}function Gt(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(e=>e.ndim===1?V(e,[1,e.shape[0]]):e);return W(r,0)}function Xr(t){if(t.length===0)throw new Error("need at least one array to stack");return t.every(e=>e.ndim===1)?W(t,0):W(t,1)}function Jt(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(e=>e.ndim===1?V(tr(V(e,[1,e.shape[0]]),2),[1,e.shape[0],1]):e.ndim===2?tr(e,2):e);return W(r,2)}function Qt(t,r,e=0){let n=t.shape,o=n.length,a=e<0?o+e:e;if(a<0||a>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let i=n[a],s;if(typeof r=="number"){if(i%r!==0)throw new Error("array split does not result in an equal division");let u=i/r;s=[];for(let c=1;c<r;c++)s.push(c*u)}else s=r;return Xt(t,s,a)}function or(t,r,e=0){let n=t.shape,o=n.length,a=e<0?o+e:e;if(a<0||a>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let i=n[a],s;if(typeof r=="number"){let u=r,c=Math.floor(i/u),l=i%u;s=[];let m=0;for(let f=0;f<u-1;f++)m+=c+(f<l?1:0),s.push(m)}else s=r;return Xt(t,s,a)}function Xt(t,r,e){let n=t.shape,o=n[e],a=[0,...r,o],i=[];for(let s=0;s<a.length-1;s++){let u=a[s],c=a[s+1];if(u>c)throw new Error("split indices must be in ascending order");let l=Array.from(n);l[e]=c-u;let m=t.offset+u*t.strides[e];i.push(D.fromData(t.data,l,t.dtype,Array.from(t.strides),m))}return i}function Kt(t,r){if(t.ndim<2)throw new Error("vsplit only works on arrays of 2 or more dimensions");return or(t,r,0)}function re(t,r){if(t.ndim<1)throw new Error("hsplit only works on arrays of 1 or more dimensions");let e=t.ndim===1?0:1;return or(t,r,e)}function te(t,r){let e=t.shape,n=e.length,o=t.dtype,a=Array.isArray(r)?r:[r],i=Math.max(n,a.length),s=new Array(i).fill(1),u=new Array(i).fill(1);for(let h=0;h<n;h++)s[i-n+h]=e[h];for(let h=0;h<a.length;h++)u[i-a.length+h]=a[h];let c=s.map((h,b)=>h*u[b]),l=c.reduce((h,b)=>h*b,1),m=_(o);if(!m)throw new Error(`Cannot tile array with dtype ${o}`);let f=new m(l),p=q(c),y=t;n<i&&(y=V(t,s));let d=o==="int64"||o==="uint64",g=y.strides,A=new Array(i).fill(0);for(let h=0;h<l;h++){let b=y.offset;for(let x=0;x<i;x++){let z=A[x]%s[x];b+=z*g[x]}let S=y.data[b],w=0;for(let x=0;x<i;x++)w+=A[x]*p[x];f[w]=S;for(let x=i-1;x>=0&&(A[x]++,!(A[x]<c[x]));x--)A[x]=0}return D.fromData(f,c,o)}function ee(t,r,e){let n=t.shape,o=n.length,a=t.dtype,i=t.size;if(e===void 0){let h=i,b=Array.isArray(r)?r:new Array(h).fill(r);if(b.length!==h)throw new Error(`operands could not be broadcast together with shape (${h},) (${b.length},)`);let S=b.reduce((B,E)=>B+E,0),w=_(a);if(!w)throw new Error(`Cannot repeat array with dtype ${a}`);let x=new w(S),z=0;for(let B=0;B<h;B++){let E=t.iget(B),O=b[B];for(let fr=0;fr<O;fr++)x[z++]=E}return D.fromData(x,[S],a)}let s=e<0?o+e:e;if(s<0||s>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let u=n[s],c=Array.isArray(r)?r:new Array(u).fill(r);if(c.length!==u)throw new Error(`operands could not be broadcast together with shape (${u},) (${c.length},)`);let l=Array.from(n);l[s]=c.reduce((h,b)=>h+b,0);let m=l.reduce((h,b)=>h*b,1),f=_(a);if(!f)throw new Error(`Cannot repeat array with dtype ${a}`);let p=new f(m),y=q(l),d=new Array(o).fill(0),g=a==="int64"||a==="uint64",A=[0];for(let h=0;h<u;h++)A.push(A[h]+c[h]);for(let h=0;h<i;h++){let b=t.iget(h),S=d[s],w=c[S],x=0;for(let E=0;E<o;E++)E!==s&&(x+=d[E]*y[E]);let z=y[s],B=A[S];for(let E=0;E<w;E++){let O=x+(B+E)*z;p[O]=b}for(let E=o-1;E>=0&&(d[E]++,!(d[E]<n[E]));E--)d[E]=0}return D.fromData(p,l,a)}function ne(t,r){let e=t.shape,n=e.length,o=t.dtype,a=t.size,i;if(r===void 0)i=new Set(Array.from({length:n},(f,p)=>p));else if(typeof r=="number"){let f=r<0?n+r:r;if(f<0||f>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);i=new Set([f])}else i=new Set(r.map(f=>{let p=f<0?n+f:f;if(p<0||p>=n)throw new Error(`axis ${f} is out of bounds for array of dimension ${n}`);return p}));let s=_(o);if(!s)throw new Error(`Cannot flip array with dtype ${o}`);let u=new s(a),c=N(o);if(n===1&&t.isCContiguous){let f=t.data,p=t.offset;for(let y=0;y<a;y++)u[y]=f[p+a-1-y];return D.fromData(u,[...e],o)}if(n===2&&t.isCContiguous){let f=e[0],p=e[1],y=t.data,d=t.offset;if(i.size===2){for(let g=0;g<a;g++)u[g]=y[d+a-1-g];return D.fromData(u,[...e],o)}if(i.size===1){if(i.has(0)){for(let g=0;g<f;g++){let A=d+(f-1-g)*p,h=g*p;for(let b=0;b<p;b++)u[h+b]=y[A+b]}return D.fromData(u,[...e],o)}else if(i.has(1)){for(let g=0;g<f;g++){let A=d+g*p,h=g*p;for(let b=0;b<p;b++)u[h+b]=y[A+p-1-b]}return D.fromData(u,[...e],o)}}}let l=new Array(n),m=new Array(n).fill(0);for(let f=0;f<a;f++){for(let d=0;d<n;d++)l[d]=i.has(d)?e[d]-1-m[d]:m[d];let p=t.offset;for(let d=0;d<n;d++)p+=l[d]*t.strides[d];let y=t.data[p];u[f]=y;for(let d=n-1;d>=0&&(m[d]++,!(m[d]<e[d]));d--)m[d]=0}return D.fromData(u,[...e],o)}function oe(t,r=1,e=[0,1]){let n=t.shape,o=n.length,a=t.dtype;if(o<2)throw new Error("Input must be at least 2-D");let i=e[0]<0?o+e[0]:e[0],s=e[1]<0?o+e[1]:e[1];if(i<0||i>=o||s<0||s>=o)throw new Error(`Axes are out of bounds for array of dimension ${o}`);if(i===s)throw new Error("Axes must be different");if(r=(r%4+4)%4,r===0)return t.copy();let u=_(a);if(!u)throw new Error(`Cannot rotate array with dtype ${a}`);let c=[...n];(r===1||r===3)&&([c[i],c[s]]=[c[s],c[i]]);let l=c.reduce((g,A)=>g*A,1),m=new u(l),f=q(c),p=N(a),y=new Array(o).fill(0),d=new Array(o);for(let g=0;g<t.size;g++){for(let w=0;w<o;w++)d[w]=y[w];let A,h;r===1?(A=n[s]-1-y[s],h=y[i]):r===2?(A=n[i]-1-y[i],h=n[s]-1-y[s],d[i]=A,d[s]=h):(A=y[s],h=n[i]-1-y[i]),r!==2&&(d[i]=A,d[s]=h);let b=0;for(let w=0;w<o;w++)b+=d[w]*f[w];let S=t.iget(g);m[b]=S;for(let w=o-1;w>=0&&(y[w]++,!(y[w]<n[w]));w--)y[w]=0}return D.fromData(m,c,a)}function ae(t,r,e){let n=t.shape,o=n.length,a=t.dtype,i=t.size;if(e===void 0){let y=Array.isArray(r)?r.reduce((b,S)=>b+S,0):r,d=Ir(t),g=_(a);if(!g)throw new Error(`Cannot roll array with dtype ${a}`);let A=new g(i),h=N(a);for(let b=0;b<i;b++){let S=((b-y)%i+i)%i,w=d.iget(S);A[b]=w}return D.fromData(A,[...n],a)}let s=Array.isArray(r)?r:[r],u=Array.isArray(e)?e:[e];if(s.length!==u.length)throw new Error("shift and axis must have the same length");let c=u.map(y=>{let d=y<0?o+y:y;if(d<0||d>=o)throw new Error(`axis ${y} is out of bounds for array of dimension ${o}`);return d}),l=_(a);if(!l)throw new Error(`Cannot roll array with dtype ${a}`);let m=new l(i),f=N(a),p=new Array(o).fill(0);for(let y=0;y<i;y++){let d=[...p];for(let h=0;h<c.length;h++){let b=c[h],S=n[b],w=s[h];d[b]=((d[b]-w)%S+S)%S}let g=t.offset;for(let h=0;h<o;h++)g+=d[h]*t.strides[h];let A=t.data[g];m[y]=A;for(let h=o-1;h>=0&&(p[h]++,!(p[h]<n[h]));h--)p[h]=0}return D.fromData(m,[...n],a)}function se(t,r,e=0){let n=t.ndim,o=r<0?n+r:r;if(o<0||o>=n)throw new Error(`axis ${r} is out of bounds for array of dimension ${n}`);let a=e<0?n+e:e;if(a<0||a>n)throw new Error(`start ${e} is out of bounds`);return o<a&&a--,o===a?D.fromData(t.data,Array.from(t.shape),t.dtype,Array.from(t.strides),t.offset):Qr(t,o,a)}function ie(t,r){if(t.ndim<3)throw new Error("dsplit only works on arrays of 3 or more dimensions");return or(t,r,2)}function ue(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(e=>e.ndim===1?V(e,[e.shape[0],1]):e);return Xr(r)}function ce(t,r){let e=t.dtype,n=r.reduce((u,c)=>u*c,1),o=t.size,a=_(e);if(!a)throw new Error(`Cannot resize array with dtype ${e}`);let i=new a(n),s=N(e);for(let u=0;u<n;u++){let c=u%o,l=t.iget(c);i[u]=l}return D.fromData(i,r,e)}function fe(t){return t.map(r=>r.ndim===0?V(r,[1]):r)}function le(t){return t.map(r=>r.ndim===0?V(r,[1,1]):r.ndim===1?V(r,[1,r.shape[0]]):r)}function pe(t){return t.map(r=>r.ndim===0?V(r,[1,1,1]):r.ndim===1?V(r,[1,r.shape[0],1]):r.ndim===2?V(r,[r.shape[0],r.shape[1],1]):r)}function Ja(t,r,e,n,o,a,i,s,u,c,l,m,f,p){if(m===0)for(let A=0;A<n*o;A++)f[A]=0;else if(m!==1)for(let A=0;A<n*o;A++)f[A]=(f[A]??0)*m;let y=t==="row-major",d=r==="transpose",g=e==="transpose";if(y&&!d&&!g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[A*u+S]??0)*(c[S*l+h]??0);f[A*p+h]=(f[A*p+h]??0)+i*b}else if(y&&d&&!g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[S*u+A]??0)*(c[S*l+h]??0);f[A*p+h]=(f[A*p+h]??0)+i*b}else if(y&&!d&&g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[A*u+S]??0)*(c[h*l+S]??0);f[A*p+h]=(f[A*p+h]??0)+i*b}else if(y&&d&&g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[S*u+A]??0)*(c[h*l+S]??0);f[A*p+h]=(f[A*p+h]??0)+i*b}else if(!y&&!d&&!g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[S*u+A]??0)*(c[h*l+S]??0);f[h*p+A]=(f[h*p+A]??0)+i*b}else if(!y&&d&&!g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[A*u+S]??0)*(c[h*l+S]??0);f[h*p+A]=(f[h*p+A]??0)+i*b}else if(!y&&!d&&g)for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[S*u+A]??0)*(c[S*l+h]??0);f[h*p+A]=(f[h*p+A]??0)+i*b}else for(let A=0;A<n;A++)for(let h=0;h<o;h++){let b=0;for(let S=0;S<a;S++)b+=(s[A*u+S]??0)*(c[S*l+h]??0);f[h*p+A]=(f[h*p+A]??0)+i*b}}function rt(t,r){let e=t.ndim,n=r.ndim;if(e===0||n===0){let o=e===0?t.get():null,a=n===0?r.get():null;if(e===0&&n===0)return typeof o=="bigint"&&typeof a=="bigint"?o*a:Number(o)*Number(a);if(e===0){let i=v(t.dtype,r.dtype),s=D.zeros([...r.shape],i);for(let u=0;u<r.size;u++){let c=r.data[u+r.offset];typeof o=="bigint"&&typeof c=="bigint"?s.data[u]=o*c:s.data[u]=Number(o)*Number(c)}return s}else{let i=v(t.dtype,r.dtype),s=D.zeros([...t.shape],i);for(let u=0;u<t.size;u++){let c=t.data[u+t.offset];typeof c=="bigint"&&typeof a=="bigint"?s.data[u]=c*a:s.data[u]=Number(c)*Number(a)}return s}}if(e===1&&n===1){if(t.shape[0]!==r.shape[0])throw new Error(`dot: incompatible shapes (${t.shape[0]},) and (${r.shape[0]},)`);let o=t.shape[0],a=0;for(let i=0;i<o;i++){let s=t.get(i),u=r.get(i);typeof s=="bigint"&&typeof u=="bigint"?a=Number(a)+Number(s*u):a+=Number(s)*Number(u)}return a}if(e===2&&n===2)return H(t,r);if(e===2&&n===1){let[o,a]=t.shape,i=r.shape[0];if(a!==i)throw new Error(`dot: incompatible shapes (${o},${a}) and (${i},)`);let s=v(t.dtype,r.dtype),u=D.zeros([o],s);for(let c=0;c<o;c++){let l=0;for(let m=0;m<a;m++){let f=t.get(c,m),p=r.get(m);typeof f=="bigint"&&typeof p=="bigint"?l=Number(l)+Number(f*p):l+=Number(f)*Number(p)}u.set([c],l)}return u}if(e===1&&n===2){let o=t.shape[0],[a,i]=r.shape;if(o!==a)throw new Error(`dot: incompatible shapes (${o},) and (${a},${i})`);let s=v(t.dtype,r.dtype),u=D.zeros([i],s);for(let c=0;c<i;c++){let l=0;for(let m=0;m<o;m++){let f=t.get(m),p=r.get(m,c);typeof f=="bigint"&&typeof p=="bigint"?l=Number(l)+Number(f*p):l+=Number(f)*Number(p)}u.set([c],l)}return u}if(e>2&&n===1){let o=t.shape[e-1],a=r.shape[0];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and (${a},)`);let i=[...t.shape.slice(0,-1)],s=v(t.dtype,r.dtype),u=D.zeros(i,s),c=i.reduce((l,m)=>l*m,1);for(let l=0;l<c;l++){let m=0,f=l,p=[];for(let y=i.length-1;y>=0;y--)p[y]=f%i[y],f=Math.floor(f/i[y]);for(let y=0;y<o;y++){let d=[...p,y],g=t.get(...d),A=r.get(y);typeof g=="bigint"&&typeof A=="bigint"?m=Number(m)+Number(g*A):m+=Number(g)*Number(A)}u.set(p,m)}return u}if(e===1&&n>2){let o=t.shape[0],a=1,i=r.shape[a];if(o!==i)throw new Error(`dot: incompatible shapes (${o},) and ${JSON.stringify(r.shape)}`);let s=[...r.shape.slice(0,a),...r.shape.slice(a+1)],u=v(t.dtype,r.dtype),c=D.zeros(s,u),l=s.reduce((m,f)=>m*f,1);for(let m=0;m<l;m++){let f=m,p=[];for(let A=s.length-1;A>=0;A--)p[A]=f%s[A],f=Math.floor(f/s[A]);let y=p.slice(0,a),d=p.slice(a),g=0;for(let A=0;A<o;A++){let h=t.get(A),b=[...y,A,...d],S=r.get(...b);typeof h=="bigint"&&typeof S=="bigint"?g=Number(g)+Number(h*S):g+=Number(h)*Number(S)}c.set(p,g)}return c}if(e>=2&&n>=2&&!(e===2&&n===2)){let o=t.shape[e-1],a=r.shape[n-2];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and ${JSON.stringify(r.shape)}`);let i=[...t.shape.slice(0,-1),...r.shape.slice(0,-2),r.shape[n-1]],s=v(t.dtype,r.dtype),u=D.zeros(i,s),c=t.shape.slice(0,-1).reduce((p,y)=>p*y,1),l=r.shape.slice(0,-2).reduce((p,y)=>p*y,1),m=r.shape[n-1],f=o;for(let p=0;p<c;p++)for(let y=0;y<l;y++)for(let d=0;d<m;d++){let g=0;for(let h=0;h<f;h++){let b=p*f+h,S=t.data[b+t.offset],w=y*f*m+h*m+d,x=r.data[w+r.offset];typeof S=="bigint"&&typeof x=="bigint"?g=Number(g)+Number(S*x):g+=Number(S)*Number(x)}let A=p*l*m+y*m+d;u.data[A]=g}return u}throw new Error(`dot: unexpected combination of dimensions ${e}D \xB7 ${n}D`)}function H(t,r){if(t.ndim!==2||r.ndim!==2)throw new Error("matmul requires 2D arrays");let[e=0,n=0]=t.shape,[o=0,a=0]=r.shape;if(n!==o)throw new Error(`matmul shape mismatch: (${e},${n}) @ (${o},${a})`);let i=v(t.dtype,r.dtype),s=i.startsWith("int")||i.startsWith("uint")||i==="bool"?"float64":i;if(s!=="float64")throw new Error(`matmul currently only supports float64, got ${s}`);let u=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number)),c=r.dtype==="float64"?r.data:Float64Array.from(Array.from(r.data).map(Number));t.offset>0&&(u=u.subarray(t.offset)),r.offset>0&&(c=c.subarray(r.offset));let[l=0,m=0]=t.strides,[f=0,p=0]=r.strides,y=m>l,d=p>f,g=y?"transpose":"no-transpose",A=d?"transpose":"no-transpose",h,b;y?h=m:h=l,d?b=p:b=f;let S=D.zeros([e,a],"float64");return Ja("row-major",g,A,e,a,n,1,u,h,c,b,0,S.data,a),S}function de(t){if(t.ndim!==2)throw new Error(`trace requires 2D array, got ${t.ndim}D`);let[r=0,e=0]=t.shape,n=Math.min(r,e),o=0;for(let a=0;a<n;a++){let i=t.get(a,a);typeof i=="bigint"?o=(typeof o=="bigint"?o:BigInt(o))+i:o=(typeof o=="bigint"?Number(o):o)+i}return o}function ye(t,r){return nr(t,r)}function ge(t,r){let e=t.ndim,n=r.ndim,o=t.shape[e-1],a=r.shape[n-1];if(o!==a)throw new Error(`inner: incompatible shapes - last dimensions ${o} and ${a} don't match`);if(e===1&&n===1)return rt(t,r);let i=[...t.shape.slice(0,-1),...r.shape.slice(0,-1)],s=v(t.dtype,r.dtype),u=D.zeros(i,s),c=e===1?1:t.shape.slice(0,-1).reduce((f,p)=>f*p,1),l=n===1?1:r.shape.slice(0,-1).reduce((f,p)=>f*p,1),m=o;for(let f=0;f<c;f++)for(let p=0;p<l;p++){let y=0;for(let g=0;g<m;g++){let A=e===1?g:f*m+g,h=n===1?g:p*m+g,b=t.data[A+t.offset],S=r.data[h+r.offset];typeof b=="bigint"&&typeof S=="bigint"?y=Number(y)+Number(b*S):y+=Number(b)*Number(S)}if(i.length===0)return y;let d=c===1?p:f*l+p;u.data[d]=y}return u}function tt(t,r){let e=t.ndim===1?t:er(t),n=r.ndim===1?r:er(r),o=e.size,a=n.size,i=v(t.dtype,r.dtype),s=D.zeros([o,a],i);for(let u=0;u<o;u++)for(let c=0;c<a;c++){let l=e.get(u),m=n.get(c),f;typeof l=="bigint"&&typeof m=="bigint"?f=l*m:f=Number(l)*Number(m),s.set([u,c],f)}return s}function he(t,r,e){let n,o;if(typeof e=="number"){let f=e;if(f<0)throw new Error("tensordot: axes must be non-negative");if(f>t.ndim||f>r.ndim)throw new Error("tensordot: axes exceeds array dimensions");n=Array.from({length:f},(p,y)=>t.ndim-f+y),o=Array.from({length:f},(p,y)=>y)}else if([n,o]=e,n.length!==o.length)throw new Error("tensordot: axes lists must have same length");for(let f=0;f<n.length;f++){let p=n[f],y=o[f];if(p<0||p>=t.ndim||y<0||y>=r.ndim)throw new Error("tensordot: axis out of bounds");if(t.shape[p]!==r.shape[y])throw new Error(`tensordot: shape mismatch on axes ${p} and ${y}: ${t.shape[p]} != ${r.shape[y]}`)}let a=[],i=[];for(let f=0;f<t.ndim;f++)n.includes(f)||a.push(f);for(let f=0;f<r.ndim;f++)o.includes(f)||i.push(f);let s=[...a.map(f=>t.shape[f]),...i.map(f=>r.shape[f])];if(s.length===0){let f=0,p=n.map(y=>t.shape[y]).reduce((y,d)=>y*d,1);for(let y=0;y<p;y++){let d=y,g=new Array(n.length);for(let w=n.length-1;w>=0;w--){let x=n[w];g[w]=d%t.shape[x],d=Math.floor(d/t.shape[x])}let A=new Array(t.ndim),h=new Array(r.ndim);for(let w=0;w<n.length;w++)A[n[w]]=g[w];for(let w=0;w<o.length;w++)h[o[w]]=g[w];let b=t.get(...A),S=r.get(...h);typeof b=="bigint"&&typeof S=="bigint"?f=Number(f)+Number(b*S):f+=Number(b)*Number(S)}return f}let u=v(t.dtype,r.dtype),c=D.zeros(s,u),l=s.reduce((f,p)=>f*p,1),m=n.map(f=>t.shape[f]).reduce((f,p)=>f*p,1);for(let f=0;f<l;f++){let p=f,y=[];for(let h=s.length-1;h>=0;h--)y[h]=p%s[h],p=Math.floor(p/s[h]);let d=y.slice(0,a.length),g=y.slice(a.length),A=0;for(let h=0;h<m;h++){p=h;let b=[];for(let B=n.length-1;B>=0;B--){let E=n[B];b[B]=p%t.shape[E],p=Math.floor(p/t.shape[E])}let S=new Array(t.ndim),w=new Array(r.ndim);for(let B=0;B<a.length;B++)S[a[B]]=d[B];for(let B=0;B<i.length;B++)w[i[B]]=g[B];for(let B=0;B<n.length;B++)S[n[B]]=b[B],w[o[B]]=b[B];let x=t.get(...S),z=r.get(...w);typeof x=="bigint"&&typeof z=="bigint"?A=Number(A)+Number(x*z):A+=Number(x)*Number(z)}c.set(y,A)}return c}function Ae(t,r=0,e=0,n=1){let o=t.shape,a=o.length;if(a<2)throw new Error("diagonal requires an array of at least two dimensions");let i=e<0?a+e:e,s=n<0?a+n:n;if(i<0||i>=a||s<0||s>=a)throw new Error("axis out of bounds");if(i===s)throw new Error("axis1 and axis2 cannot be the same");let u=o[i],c=o[s],l;r>=0?l=Math.max(0,Math.min(u,c-r)):l=Math.max(0,Math.min(u+r,c));let m=[];for(let d=0;d<a;d++)d!==i&&d!==s&&m.push(o[d]);m.push(l);let f=D.zeros(m,t.dtype),p=o.filter((d,g)=>g!==i&&g!==s),y=p.reduce((d,g)=>d*g,1);for(let d=0;d<y;d++){let g=d,A=[];for(let h=p.length-1;h>=0;h--)A.unshift(g%p[h]),g=Math.floor(g/p[h]);for(let h=0;h<l;h++){let b=new Array(a),S=0;for(let z=0;z<a;z++)z===i?b[z]=r>=0?h:h-r:z===s?b[z]=r>=0?h+r:h:b[z]=A[S++];let w=[...A,h],x=t.get(...b);f.set(w,x)}}return f}function be(t,...r){let e=t.indexOf("->"),n,o;e===-1?(n=t,o=Qa(n)):(n=t.slice(0,e),o=t.slice(e+2));let a=n.split(",").map(d=>d.trim());if(a.length!==r.length)throw new Error(`einsum: expected ${a.length} operands, got ${r.length}`);let i=new Map;for(let d=0;d<r.length;d++){let g=a[d],A=r[d];if(g.length!==A.ndim)throw new Error(`einsum: operand ${d} has ${A.ndim} dimensions but subscript '${g}' has ${g.length} indices`);for(let h=0;h<g.length;h++){let b=g[h],S=A.shape[h];if(i.has(b)){if(i.get(b)!==S)throw new Error(`einsum: size mismatch for index '${b}': ${i.get(b)} vs ${S}`)}else i.set(b,S)}}for(let d of o)if(!i.has(d))throw new Error(`einsum: output subscript contains unknown index '${d}'`);let s=new Set(o),u=new Set;for(let d of a)for(let g of d)u.add(g);let c=[];for(let d of u)s.has(d)||c.push(d);if(r.length===2&&a.length===2){let[d,g]=a,[A,h]=r;if(d.length===2&&g.length===2&&o.length===2&&A.ndim===2&&h.ndim===2){let[b,S]=[d[0],d[1]],[w,x]=[g[0],g[1]],[z,B]=[o[0],o[1]];if(b===z&&x===B&&S===w&&c.length===1&&c[0]===S||b===z&&x===B&&S===w&&c.length===1&&c[0]===S)return H(A,h);if(S===z&&x===B&&b===w&&c.length===1&&c[0]===b){let E=ye(A);return H(E,h)}if(b===z&&w===B&&S===x&&c.length===1&&c[0]===S){let E=ye(h);return H(A,E)}}if(d.length===1&&g.length===1&&d===g&&o.length===0&&A.ndim===1&&h.ndim===1)return Kr(r,a,c,i);if(d&&g&&d.length===1&&g.length===1&&o.length===2&&o===d+g&&c.length===0&&A.ndim===1&&h.ndim===1)return tt(A,h)}if(r.length===1&&a[0].length===2&&o.length===0){let d=a[0];if(d[0]===d[1]&&r[0].ndim===2)return Kr(r,a,c,i)}let l=Array.from(o).map(d=>i.get(d));if(l.length===0)return Kr(r,a,c,i);let m=r[0].dtype;for(let d=1;d<r.length;d++)m=v(m,r[d].dtype);let f=D.zeros(l,m),p=l.reduce((d,g)=>d*g,1),y=1;for(let d of c)y*=i.get(d);for(let d=0;d<p;d++){let g=Xa(d,l),A=new Map;for(let b=0;b<o.length;b++)A.set(o[b],g[b]);let h=0;for(let b=0;b<y;b++){let S=b;for(let x=c.length-1;x>=0;x--){let z=c[x],B=i.get(z);A.set(z,S%B),S=Math.floor(S/B)}let w=1;for(let x=0;x<r.length;x++){let z=r[x],B=a[x],E=[];for(let fr of B)E.push(A.get(fr));let O=z.get(...E);w*=Number(O)}h+=w}f.set(g,h)}return f}function Qa(t){let r=new Map,e=t.split(",");for(let o of e)for(let a of o.trim())r.set(a,(r.get(a)||0)+1);let n=[];for(let[o,a]of r)a===1&&n.push(o);return n.sort().join("")}function Kr(t,r,e,n){let o=1;for(let i of e)o*=n.get(i);let a=0;for(let i=0;i<o;i++){let s=new Map,u=i;for(let l=e.length-1;l>=0;l--){let m=e[l],f=n.get(m);s.set(m,u%f),u=Math.floor(u/f)}let c=1;for(let l=0;l<t.length;l++){let m=t[l],f=r[l],p=[];for(let d of f)p.push(s.get(d));let y=m.get(...p);c*=Number(y)}a+=c}return a}function Xa(t,r){let e=new Array(r.length),n=t;for(let o=r.length-1;o>=0;o--)e[o]=n%r[o],n=Math.floor(n/r[o]);return e}function Se(t,r){let e=t.shape,n=r.shape,o=e.length,a=n.length,i=v(t.dtype,r.dtype),s=Math.max(o,a),u=new Array(s),c=new Array(s).fill(1),l=new Array(s).fill(1);for(let y=0;y<o;y++)c[s-o+y]=e[y];for(let y=0;y<a;y++)l[s-a+y]=n[y];for(let y=0;y<s;y++)u[y]=c[y]*l[y];let m=D.zeros(u,i),f=e.reduce((y,d)=>y*d,1),p=n.reduce((y,d)=>y*d,1);for(let y=0;y<f;y++){let d=y,g=new Array(o);for(let b=o-1;b>=0;b--)g[b]=d%e[b],d=Math.floor(d/e[b]);let A=new Array(s).fill(0);for(let b=0;b<o;b++)A[s-o+b]=g[b];let h=t.get(...g);for(let b=0;b<p;b++){let S=b,w=new Array(a);for(let O=a-1;O>=0;O--)w[O]=S%n[O],S=Math.floor(S/n[O]);let x=new Array(s).fill(0);for(let O=0;O<a;O++)x[s-a+O]=w[O];let z=r.get(...w),B=new Array(s);for(let O=0;O<s;O++)B[O]=A[O]*l[O]+x[O];let E=typeof h=="bigint"||typeof z=="bigint"?BigInt(Number(h))*BigInt(Number(z)):Number(h)*Number(z);m.set(B,E)}}return m}function De(t){return M(t,Math.sqrt,!1)}function Ne(t,r){return typeof r=="number"?rs(t,r):P(t,r,Math.pow,"power")}function rs(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,u=e!=="float32"&&e!=="float64"&&(r<0||!Number.isInteger(r))?"float64":e,c=D.zeros(n,u),l=c.data;if(N(e))if(N(u)&&Number.isInteger(r)&&r>=0){let m=o,f=l;for(let p=0;p<a;p++)f[p]=m[p]**BigInt(r)}else for(let m=0;m<a;m++)l[m]=Math.pow(Number(o[m]),r);else for(let m=0;m<a;m++)l[m]=Math.pow(Number(o[m]),r);return c}function we(t){return M(t,Math.sin,!1)}function xe(t){return M(t,Math.cos,!1)}function Ie(t){return M(t,Math.tan,!1)}function ze(t){return M(t,Math.asin,!1)}function Be(t){return M(t,Math.acos,!1)}function _e(t){return M(t,Math.atan,!1)}function Ee(t,r){return typeof r=="number"?ns(t,r):es(t,r)}function es(t,r){let e=Array.from(t.shape),n=t.size,o=t.dtype,a=r.dtype,i=o==="float32"&&a==="float32"?"float32":"float64",s=D.zeros(e,i),u=s.data;for(let c=0;c<n;c++){let l=(N(o),Number(t.data[c])),m=(N(a),Number(r.data[c]));u[c]=Math.atan2(l,m)}return s}function ns(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=e==="float32"?"float32":"float64",s=D.zeros(n,i),u=s.data;if(N(e))for(let c=0;c<a;c++)u[c]=Math.atan2(Number(o[c]),r);else for(let c=0;c<a;c++)u[c]=Math.atan2(Number(o[c]),r);return s}function Ue(t,r){return typeof r=="number"?as(t,r):os(t,r)}function os(t,r){let e=Array.from(t.shape),n=t.size,o=t.dtype,a=r.dtype,i=o==="float32"&&a==="float32"?"float32":"float64",s=D.zeros(e,i),u=s.data;for(let c=0;c<n;c++){let l=(N(o),Number(t.data[c])),m=(N(a),Number(r.data[c]));u[c]=Math.hypot(l,m)}return s}function as(t,r){let e=t.dtype,n=Array.from(t.shape),o=t.data,a=t.size,i=e==="float32"?"float32":"float64",s=D.zeros(n,i),u=s.data;if(N(e))for(let c=0;c<a;c++)u[c]=Math.hypot(Number(o[c]),r);else for(let c=0;c<a;c++)u[c]=Math.hypot(Number(o[c]),r);return s}function Te(t){let r=180/Math.PI;return M(t,e=>e*r,!1)}function Oe(t){let r=Math.PI/180;return M(t,e=>e*r,!1)}function $e(t){return M(t,Math.sinh,!1)}function ve(t){return M(t,Math.cosh,!1)}function Me(t){return M(t,Math.tanh,!1)}function Ce(t){return M(t,Math.asinh,!1)}function ke(t){return M(t,Math.acosh,!1)}function Ve(t){return M(t,Math.atanh,!1)}function et(t,r){let e=t.shape,n=e.length,o=r.length;if(o<n)throw new Error("input operand has more dimensions than allowed by the axis remapping");let a=Z([Array.from(e),r]);if(a===null)throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${r.join(",")})`);for(let i=0;i<o;i++)if(a[i]!==r[i])throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${r.join(",")})`);return j(t,r)}function Pe(t){if(t.length===0)return[];if(t.length===1)return[t[0]];let r=t.map(n=>Array.from(n.shape)),e=Z(r);if(e===null)throw new Error(`operands could not be broadcast together with shapes ${r.map(n=>`(${n.join(",")})`).join(" ")}`);return t.map(n=>j(n,e))}function Fe(t,r,e){let n=t.shape,o=n.length,a=t.dtype;if(e===void 0){let y=t.size;for(let h of r){let b=h<0?y+h:h;if(b<0||b>=y)throw new Error(`index ${h} is out of bounds for axis 0 with size ${y}`)}let d=r.length,g=_(a);if(!g)throw new Error(`Cannot take from array with dtype ${a}`);let A=new g(d);for(let h=0;h<d;h++){let b=r[h];b<0&&(b=y+b);let S=t.iget(b);N(a),A[h]=S}return D.fromData(A,[d],a)}let i=e<0?o+e:e;if(i<0||i>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let s=n[i];for(let y of r){let d=y<0?s+y:y;if(d<0||d>=s)throw new Error(`index ${y} is out of bounds for axis ${i} with size ${s}`)}let u=Array.from(n);u[i]=r.length;let c=u.reduce((y,d)=>y*d,1),l=_(a);if(!l)throw new Error(`Cannot take from array with dtype ${a}`);let m=new l(c),f=q(u),p=new Array(o).fill(0);for(let y=0;y<c;y++){let d=[...p],g=p[i],A=r[g];A<0&&(A=s+A),d[i]=A;let h=t.get(...d),b=0;for(let S=0;S<o;S++)b+=p[S]*f[S];N(a),m[b]=h;for(let S=o-1;S>=0&&(p[S]++,!(p[S]<u[S]));S--)p[S]=0}return D.fromData(m,u,a)}function Re(t,r,e){let n=t.size,o=t.dtype,a;if(typeof e=="number"||typeof e=="bigint")a=new Array(r.length).fill(e);else{a=[];for(let i=0;i<e.size;i++)a.push(e.iget(i));if(a.length===1)a=new Array(r.length).fill(a[0]);else if(a.length!==r.length){let i=[...a];a=[];for(let s=0;s<r.length;s++)a.push(i[s%i.length])}}for(let i=0;i<r.length;i++){let s=r[i];if(s<0&&(s=n+s),s<0||s>=n)throw new Error(`index ${r[i]} is out of bounds for axis 0 with size ${n}`);let u=a[i];N(o)?typeof u!="bigint"&&(u=BigInt(Math.round(Number(u)))):typeof u=="bigint"&&(u=Number(u)),t.iset(s,u)}}function qe(t,r){if(r.length===0)throw new Error("choices cannot be empty");let e=t.shape,n=r.length,o=r[0].dtype,a=r.map(f=>Array.from(f.shape));a.unshift(Array.from(e));let i=Z(a);if(i===null)throw new Error("operands could not be broadcast together");let s=j(t,i),u=r.map(f=>j(f,i)),c=i.reduce((f,p)=>f*p,1),l=_(o);if(!l)throw new Error(`Cannot choose with dtype ${o}`);let m=new l(c);for(let f=0;f<c;f++){let p=Number(s.iget(f));if(p<0||p>=n)throw new Error(`index ${p} is out of bounds for axis 0 with size ${n}`);let y=u[p].iget(f);N(o),m[f]=y}return D.fromData(m,i,o)}function Le(t,r,e=!1){if(t.ndim!==r.ndim)return!1;for(let o=0;o<t.ndim;o++)if(t.shape[o]!==r.shape[o])return!1;let n=t.size;for(let o=0;o<n;o++){let a=t.iget(o),i=r.iget(o);if(e){let s=typeof a=="number"&&Number.isNaN(a),u=typeof i=="number"&&Number.isNaN(i);if(s&&u)continue}if(a!==i)return!1}return!0}var I=class t{constructor(r,e){this._storage=r,this._base=e}get storage(){return this._storage}static _fromStorage(r,e){return new t(r,e)}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(r){if(r.length!==this.ndim)throw new Error(`Index has ${r.length} dimensions, but array has ${this.ndim} dimensions`);let e=r.map((n,o)=>{let a=n;if(a<0&&(a=this.shape[o]+a),a<0||a>=this.shape[o])throw new Error(`Index ${n} is out of bounds for axis ${o} with size ${this.shape[o]}`);return a});return this._storage.get(...e)}set(r,e){if(r.length!==this.ndim)throw new Error(`Index has ${r.length} dimensions, but array has ${this.ndim} dimensions`);let n=r.map((i,s)=>{let u=i;if(u<0&&(u=this.shape[s]+u),u<0||u>=this.shape[s])throw new Error(`Index ${i} is out of bounds for axis ${s} with size ${this.shape[s]}`);return u}),o=this.dtype,a;N(o)?a=typeof e=="bigint"?e:BigInt(Math.round(e)):o==="bool"?a=e?1:0:a=Number(e),this._storage.set(n,a)}copy(){return new t(this._storage.copy())}astype(r,e=!0){let n=this.dtype;if(n===r&&!e)return this;if(n===r&&e)return this.copy();let o=Array.from(this.shape),a=this.size,i=_(r);if(!i)throw new Error(`Cannot convert to dtype ${r}`);let s=new i(a),u=this.data;if(N(n)&&!N(r)){let l=u;if(r==="bool")for(let m=0;m<a;m++)s[m]=l[m]!==BigInt(0)?1:0;else for(let m=0;m<a;m++)s[m]=Number(l[m])}else if(!N(n)&&N(r)){let l=u;for(let m=0;m<a;m++)s[m]=BigInt(Math.round(Number(l[m])))}else if(r==="bool"){let l=u;for(let m=0;m<a;m++)s[m]=l[m]!==0?1:0}else if(n==="bool"&&!N(r)){let l=u;for(let m=0;m<a;m++)s[m]=l[m]}else if(!N(n)&&!N(r)){let l=u;for(let m=0;m<a;m++)s[m]=l[m]}else{let l=u;for(let m=0;m<a;m++)s[m]=l[m]}let c=D.fromData(s,o,r);return new t(c)}add(r){let e=typeof r=="number"?r:r._storage,n=Nt(this._storage,e);return t._fromStorage(n)}subtract(r){let e=typeof r=="number"?r:r._storage,n=wt(this._storage,e);return t._fromStorage(n)}multiply(r){let e=typeof r=="number"?r:r._storage,n=xt(this._storage,e);return t._fromStorage(n)}divide(r){let e=typeof r=="number"?r:r._storage,n=It(this._storage,e);return t._fromStorage(n)}mod(r){let e=typeof r=="number"?r:r._storage,n=pr(this._storage,e);return t._fromStorage(n)}floor_divide(r){let e=typeof r=="number"?r:r._storage,n=vr(this._storage,e);return t._fromStorage(n)}positive(){let r=Et(this._storage);return t._fromStorage(r)}reciprocal(){let r=Ut(this._storage);return t._fromStorage(r)}sqrt(){let r=De(this._storage);return t._fromStorage(r)}power(r){let e=typeof r=="number"?r:r._storage,n=Ne(this._storage,e);return t._fromStorage(n)}absolute(){let r=zt(this._storage);return t._fromStorage(r)}negative(){let r=Bt(this._storage);return t._fromStorage(r)}sign(){let r=_t(this._storage);return t._fromStorage(r)}sin(){let r=we(this._storage);return t._fromStorage(r)}cos(){let r=xe(this._storage);return t._fromStorage(r)}tan(){let r=Ie(this._storage);return t._fromStorage(r)}arcsin(){let r=ze(this._storage);return t._fromStorage(r)}arccos(){let r=Be(this._storage);return t._fromStorage(r)}arctan(){let r=_e(this._storage);return t._fromStorage(r)}arctan2(r){let e=typeof r=="number"?r:r._storage,n=Ee(this._storage,e);return t._fromStorage(n)}hypot(r){let e=typeof r=="number"?r:r._storage,n=Ue(this._storage,e);return t._fromStorage(n)}degrees(){let r=Te(this._storage);return t._fromStorage(r)}radians(){let r=Oe(this._storage);return t._fromStorage(r)}sinh(){let r=$e(this._storage);return t._fromStorage(r)}cosh(){let r=ve(this._storage);return t._fromStorage(r)}tanh(){let r=Me(this._storage);return t._fromStorage(r)}arcsinh(){let r=Ce(this._storage);return t._fromStorage(r)}arccosh(){let r=ke(this._storage);return t._fromStorage(r)}arctanh(){let r=Ve(this._storage);return t._fromStorage(r)}greater(r){let e=typeof r=="number"?r:r._storage,n=kt(this._storage,e);return t._fromStorage(n)}greater_equal(r){let e=typeof r=="number"?r:r._storage,n=Vt(this._storage,e);return t._fromStorage(n)}less(r){let e=typeof r=="number"?r:r._storage,n=Pt(this._storage,e);return t._fromStorage(n)}less_equal(r){let e=typeof r=="number"?r:r._storage,n=Ft(this._storage,e);return t._fromStorage(n)}equal(r){let e=typeof r=="number"?r:r._storage,n=Rt(this._storage,e);return t._fromStorage(n)}not_equal(r){let e=typeof r=="number"?r:r._storage,n=qt(this._storage,e);return t._fromStorage(n)}isclose(r,e=1e-5,n=1e-8){let o=typeof r=="number"?r:r._storage,a=Cr(this._storage,o,e,n);return t._fromStorage(a)}allclose(r,e=1e-5,n=1e-8){let o=typeof r=="number"?r:r._storage;return Lt(this._storage,o,e,n)}sum(r,e=!1){let n=K(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}mean(r,e=!1){let n=mr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}max(r,e=!1){let n=yr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}min(r,e=!1){let n=dr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}prod(r,e=!1){let n=kr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}argmin(r){let e=Vr(this._storage,r);return typeof e=="number"?e:t._fromStorage(e)}argmax(r){let e=Pr(this._storage,r);return typeof e=="number"?e:t._fromStorage(e)}var(r,e=0,n=!1){let o=Fr(this._storage,r,e,n);return typeof o=="number"?o:t._fromStorage(o)}std(r,e=0,n=!1){let o=Zt(this._storage,r,e,n);return typeof o=="number"?o:t._fromStorage(o)}all(r,e=!1){let n=Rr(this._storage,r,e);return typeof n=="boolean"?n:t._fromStorage(n)}any(r,e=!1){let n=qr(this._storage,r,e);return typeof n=="boolean"?n:t._fromStorage(n)}cumsum(r){return t._fromStorage(Lr(this._storage,r))}cumprod(r){return t._fromStorage(jr(this._storage,r))}ptp(r,e=!1){let n=Zr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}median(r,e=!1){let n=Wr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}percentile(r,e,n=!1){let o=Hr(this._storage,r,e,n);return typeof o=="number"?o:t._fromStorage(o)}quantile(r,e,n=!1){let o=J(this._storage,r,e,n);return typeof o=="number"?o:t._fromStorage(o)}average(r,e){let n=gr(this._storage,e,r?.storage);return typeof n=="number"?n:t._fromStorage(n)}nansum(r,e=!1){let n=hr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}nanprod(r,e=!1){let n=Ar(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}nanmean(r,e=!1){let n=br(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}nanvar(r,e=0,n=!1){let o=rr(this._storage,r,e,n);return typeof o=="number"?o:t._fromStorage(o)}nanstd(r,e=0,n=!1){let o=Yr(this._storage,r,e,n);return typeof o=="number"?o:t._fromStorage(o)}nanmin(r,e=!1){let n=Sr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}nanmax(r,e=!1){let n=Dr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}nanargmin(r){let e=Nr(this._storage,r);return typeof e=="number"?e:t._fromStorage(e)}nanargmax(r){let e=wr(this._storage,r);return typeof e=="number"?e:t._fromStorage(e)}nancumsum(r){return t._fromStorage(Gr(this._storage,r))}nancumprod(r){return t._fromStorage(Jr(this._storage,r))}nanmedian(r,e=!1){let n=xr(this._storage,r,e);return typeof n=="number"?n:t._fromStorage(n)}reshape(...r){let e=r.length===1&&Array.isArray(r[0])?r[0]:r,n=V(this._storage,e),a=n.data===this.data?this._base??this:void 0;return t._fromStorage(n,a)}flatten(){let r=Ir(this._storage);return t._fromStorage(r)}ravel(){let r=er(this._storage),n=r.data===this.data?this._base??this:void 0;return t._fromStorage(r,n)}transpose(r){let e=nr(this._storage,r),n=this._base??this;return t._fromStorage(e,n)}squeeze(r){let e=Wt(this._storage,r),n=this._base??this;return t._fromStorage(e,n)}expand_dims(r){let e=tr(this._storage,r),n=this._base??this;return t._fromStorage(e,n)}swapaxes(r,e){let n=Ht(this._storage,r,e),o=this._base??this;return t._fromStorage(n,o)}moveaxis(r,e){let n=Qr(this._storage,r,e),o=this._base??this;return t._fromStorage(n,o)}repeat(r,e){let n=ee(this._storage,r,e);return t._fromStorage(n)}take(r,e){let n=Fe(this._storage,r,e);return t._fromStorage(n)}put(r,e){let n=e instanceof t?e._storage:e;Re(this._storage,r,n)}matmul(r){let e=H(this._storage,r._storage);return t._fromStorage(e)}dot(r){let e=rt(this._storage,r._storage);return typeof e=="number"||typeof e=="bigint"?e:t._fromStorage(e)}trace(){return de(this._storage)}inner(r){let e=ge(this._storage,r._storage);return typeof e=="number"||typeof e=="bigint"?e:t._fromStorage(e)}outer(r){let e=tt(this._storage,r._storage);return t._fromStorage(e)}tensordot(r,e=2){let n=he(this._storage,r._storage,e);return typeof n=="number"||typeof n=="bigint"?n:t._fromStorage(n)}cbrt(){let r=Tt(this._storage);return t._fromStorage(r)}fabs(){let r=Ot(this._storage);return t._fromStorage(r)}divmod(r){let e=typeof r=="number"?r:r._storage,[n,o]=$t(this._storage,e);return[t._fromStorage(n),t._fromStorage(o)]}square(){let r=vt(this._storage);return t._fromStorage(r)}remainder(r){let e=typeof r=="number"?r:r._storage,n=Mt(this._storage,e);return t._fromStorage(n)}heaviside(r){let e=typeof r=="number"?r:r._storage,n=Ct(this._storage,e);return t._fromStorage(n)}slice(...r){if(r.length===0)return this;if(r.length>this.ndim)throw new Error(`Too many indices for array: array is ${this.ndim}-dimensional, but ${r.length} were indexed`);let e=r.map((u,c)=>{let l=ht(u);return At(l,this.shape[c])});for(;e.length<this.ndim;)e.push({start:0,stop:this.shape[e.length],step:1,isIndex:!1});let n=[],o=[],a=this._storage.offset;for(let u=0;u<e.length;u++){let c=e[u],l=this._storage.strides[u];if(a+=c.start*l,!c.isIndex){let m;c.step>0?m=Math.max(0,Math.ceil((c.stop-c.start)/c.step)):m=Math.max(0,Math.ceil((c.start-c.stop)/Math.abs(c.step))),n.push(m),o.push(l*c.step)}}let i=D.fromData(this._storage.data,n,this._storage.dtype,o,a),s=this._base??this;return new t(i,s)}row(r){if(this.ndim<2)throw new Error("row() requires at least 2 dimensions");return this.slice(String(r),":")}col(r){if(this.ndim<2)throw new Error("col() requires at least 2 dimensions");return this.slice(":",String(r))}rows(r,e){if(this.ndim<2)throw new Error("rows() requires at least 2 dimensions");return this.slice(`${r}:${e}`,":")}cols(r,e){if(this.ndim<2)throw new Error("cols() requires at least 2 dimensions");return this.slice(":",`${r}:${e}`)}toString(){return`NDArray(shape=${JSON.stringify(this.shape)}, dtype=${this.dtype})`}toArray(){if(this.ndim===0)return this._storage.iget(0);let r=this.shape,e=r.length,n=(o,a)=>{if(a===e)return this._storage.get(...o);let i=[];for(let s=0;s<r[a];s++)o[a]=s,i.push(n(o,a+1));return i};return n(new Array(e),0)}};function F(t,r=$){let e=D.zeros(t,r);return new I(e)}function nt(t,r=$){let e=D.ones(t,r);return new I(e)}function cs(t){let r=[],e=t;for(;Array.isArray(e);)r.push(e.length),e=e[0];return r}function je(t){return typeof t=="bigint"?!0:Array.isArray(t)?t.some(r=>je(r)):!1}function fs(t){let r=[];function e(n){Array.isArray(n)?n.forEach(o=>e(o)):r.push(n)}return e(t),r}function C(t,r){if(t instanceof I)return!r||t.dtype===r?t.copy():t.astype(r);let e=je(t),n=cs(t),o=n.reduce((l,m)=>l*m,1),a=r;a||(e?a="int64":a=$);let i=_(a);if(!i)throw new Error(`Cannot create array with dtype ${a}`);let s=new i(o),u=fs(t);if(N(a)){let l=s;for(let m=0;m<o;m++){let f=u[m];l[m]=typeof f=="bigint"?f:BigInt(Math.round(Number(f)))}}else if(a==="bool"){let l=s;for(let m=0;m<o;m++)l[m]=u[m]?1:0}else{let l=s;for(let m=0;m<o;m++){let f=u[m];l[m]=Number(f)}}let c=D.fromData(s,n,a);return new I(c)}function Ze(t,r,e=1,n=$){let o=t,a=r;if(r===void 0&&(o=0,a=t),a===void 0)throw new Error("stop is required");let i=Math.max(0,Math.ceil((a-o)/e)),s=_(n);if(!s)throw new Error(`Cannot create arange array with dtype ${n}`);let u=new s(i);if(N(n))for(let l=0;l<i;l++)u[l]=BigInt(Math.round(o+l*e));else if(n==="bool")for(let l=0;l<i;l++)u[l]=o+l*e!==0?1:0;else for(let l=0;l<i;l++)u[l]=o+l*e;let c=D.fromData(u,[i],n);return new I(c)}function We(t,r,e=50,n=$){if(e<0)throw new Error("num must be non-negative");if(e===0)return C([],n);if(e===1)return C([t],n);let o=_(n);if(!o)throw new Error(`Cannot create linspace array with dtype ${n}`);let a=new o(e),i=(r-t)/(e-1);if(N(n))for(let u=0;u<e;u++)a[u]=BigInt(Math.round(t+u*i));else if(n==="bool")for(let u=0;u<e;u++)a[u]=t+u*i!==0?1:0;else for(let u=0;u<e;u++)a[u]=t+u*i;let s=D.fromData(a,[e],n);return new I(s)}function He(t,r,e=50,n=10,o=$){if(e<0)throw new Error("num must be non-negative");if(e===0)return C([],o);if(e===1)return C([Math.pow(n,t)],o);let a=_(o);if(!a)throw new Error(`Cannot create logspace array with dtype ${o}`);let i=new a(e),s=(r-t)/(e-1);if(N(o))for(let c=0;c<e;c++){let l=t+c*s;i[c]=BigInt(Math.round(Math.pow(n,l)))}else if(o==="bool")for(let c=0;c<e;c++){let l=t+c*s;i[c]=Math.pow(n,l)!==0?1:0}else for(let c=0;c<e;c++){let l=t+c*s;i[c]=Math.pow(n,l)}let u=D.fromData(i,[e],o);return new I(u)}function Ye(t,r,e=50,n=$){if(e<0)throw new Error("num must be non-negative");if(t===0||r===0)throw new Error("Geometric sequence cannot include zero");if(e===0)return C([],n);if(e===1)return C([t],n);let o=Math.sign(t),a=Math.sign(r);if(o!==a)throw new Error("Geometric sequence cannot contain both positive and negative values");let i=_(n);if(!i)throw new Error(`Cannot create geomspace array with dtype ${n}`);let s=new i(e),u=Math.log(Math.abs(t)),l=(Math.log(Math.abs(r))-u)/(e-1);if(N(n))for(let f=0;f<e;f++){let p=o*Math.exp(u+f*l);s[f]=BigInt(Math.round(p))}else if(n==="bool")for(let f=0;f<e;f++){let p=o*Math.exp(u+f*l);s[f]=p!==0?1:0}else for(let f=0;f<e;f++){let p=o*Math.exp(u+f*l);s[f]=p}let m=D.fromData(s,[e],n);return new I(m)}function ot(t,r,e=0,n=$){let o=r??t,a=F([t,o],n),i=a.data;if(N(n)){let s=i;for(let u=0;u<t;u++){let c=u+e;c>=0&&c<o&&(s[u*o+c]=BigInt(1))}}else{let s=i;for(let u=0;u<t;u++){let c=u+e;c>=0&&c<o&&(s[u*o+c]=1)}}return a}function at(t,r=$){return F(t,r)}function st(t,r,e){let n=e;n||(typeof r=="bigint"?n="int64":typeof r=="boolean"?n="bool":Number.isInteger(r)?n="int32":n=$);let o=_(n);if(!o)throw new Error(`Cannot create full array with dtype ${n}`);let a=t.reduce((u,c)=>u*c,1),i=new o(a);if(N(n)){let u=typeof r=="bigint"?r:BigInt(Math.round(Number(r)));i.fill(u)}else n==="bool"?i.fill(r?1:0):i.fill(Number(r));let s=D.fromData(i,t,n);return new I(s)}function Ge(t,r=$){return ot(t,t,0,r)}function ar(t,r){return t instanceof I?!r||t.dtype===r?t:t.astype(r):C(t,r)}function Je(t){return t.copy()}function Qe(t,r){return F(Array.from(t.shape),r??t.dtype)}function Xe(t,r){return nt(Array.from(t.shape),r??t.dtype)}function Ke(t,r){return at(Array.from(t.shape),r??t.dtype)}function rn(t,r,e){return st(Array.from(t.shape),r,e??t.dtype)}function tn(t,r){return ar(t,r)}function en(t,r){let e=ar(t,r);return e.flags.C_CONTIGUOUS?e:e.copy()}function nn(t,r){return ar(t,r).copy()}function it(t,r=0){if(t.ndim===1){let e=t.size,n=e+Math.abs(r),o=F([n,n],t.dtype);for(let a=0;a<e;a++){let i=r>=0?a:a-r,s=r>=0?a+r:a;o.set([i,s],t.get([a]))}return o}else if(t.ndim===2){let[e,n]=t.shape,o,a,i;if(r>=0?(o=0,a=r,i=Math.min(e,n-r)):(o=-r,a=0,i=Math.min(e+r,n)),i<=0)return F([0],t.dtype);let s=_(t.dtype),u=new s(i);for(let l=0;l<i;l++){let m=t.get([o+l,a+l]);N(t.dtype)?u[l]=typeof m=="bigint"?m:BigInt(m):u[l]=m}let c=D.fromData(u,[i],t.dtype);return new I(c)}else throw new Error("Input must be 1-D or 2-D")}function on(t,r=0){let e=t.flatten();return it(e,r)}function an(t,r,e=$){let n=r.reduce((c,l)=>c*l,1),o=_(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n),i=r.length,s=new Array(i).fill(0);for(let c=0;c<n;c++){let l=t(...s);N(e)?a[c]=typeof l=="bigint"?l:BigInt(Number(l)):e==="bool"?a[c]=l?1:0:a[c]=Number(l);for(let m=i-1;m>=0&&(s[m]++,!(s[m]<r[m]));m--)s[m]=0}let u=D.fromData(a,r,e);return new I(u)}function sn(...t){let r=[],e="xy";for(let s of t)s instanceof I?r.push(s):typeof s=="object"&&"indexing"in s&&(e=s.indexing||"xy");if(r.length===0)return[];if(r.length===1)return[r[0].copy()];let n=r.map(s=>s.size);e==="xy"&&r.length>=2&&(r=[r[1],r[0],...r.slice(2)],[n[0],n[1]]=[n[1],n[0]]);let o=n,a=o.length,i=[];for(let s=0;s<r.length;s++){let u=r[s],c=u.size,l=new Array(a).fill(1);l[s]=c;let m=u.reshape(...l),f=et(m.storage,o),p=I._fromStorage(f.copy());i.push(p)}return e==="xy"&&i.length>=2&&([i[0],i[1]]=[i[1],i[0]]),i}function un(t,r,e=0,n=$){let o=r??t,a=F([t,o],n);for(let i=0;i<t;i++)for(let s=0;s<=i+e&&s<o;s++)s>=0&&a.set([i,s],1);return a}function cn(t,r=0){if(t.ndim<2)throw new Error("Input must have at least 2 dimensions");let e=t.copy(),n=e.shape,o=n[n.length-2],a=n[n.length-1],i=n.slice(0,-2).reduce((s,u)=>s*u,1);for(let s=0;s<i;s++)for(let u=0;u<o;u++)for(let c=0;c<a;c++)if(c>u+r){let l=[],m=s;for(let f=n.length-3;f>=0;f--)l.unshift(m%n[f]),m=Math.floor(m/n[f]);l.push(u,c),e.set(l,0)}return e}function fn(t,r=0){if(t.ndim<2)throw new Error("Input must have at least 2 dimensions");let e=t.copy(),n=e.shape,o=n[n.length-2],a=n[n.length-1],i=n.slice(0,-2).reduce((s,u)=>s*u,1);for(let s=0;s<i;s++)for(let u=0;u<o;u++)for(let c=0;c<a;c++)if(c<u+r){let l=[],m=s;for(let f=n.length-3;f>=0;f--)l.unshift(m%n[f]),m=Math.floor(m/n[f]);l.push(u,c),e.set(l,0)}return e}function ln(t,r,e=!1){if(t.ndim!==1)throw new Error("Input must be 1-D");let n=t.size,o=r??n;if(o<0)throw new Error("N must be non-negative");let a=F([n,o],t.dtype);for(let i=0;i<n;i++){let s=t.get([i]);for(let u=0;u<o;u++){let c=e?u:o-1-u;a.set([i,u],Math.pow(s,c))}}return a}function pn(t,r=$,e=-1,n=0){let o,a=n;t instanceof ArrayBuffer?o=t:(o=t.buffer,a+=t.byteOffset);let i=ls(r),s=o.byteLength-a,u=Math.floor(s/i),c=e<0?u:Math.min(e,u);if(c<=0)return C([],r);let l=_(r);if(!l)throw new Error(`Unsupported dtype: ${r}`);let m=new l(o,a,c),f=D.fromData(m,[c],r);return new I(f)}function mn(t,r=$,e=-1){let n=[],o=0;for(let a of t){if(e>=0&&o>=e)break;n.push(a),o++}return C(n,r)}function yn(t,r=$,e=-1){let n=[],o=0;for(let a of t){if(e>=0&&o>=e)break;n.push(a),o++}return C(n,r)}function dn(t,r=$,e=-1,n=""){let o;n===""?o=t.trim().split(/\s+/):o=t.split(n);let a=[],i=0;for(let s of o){if(e>=0&&i>=e)break;let u=s.trim();u!==""&&(N(r)?a.push(BigInt(u)):a.push(parseFloat(u)),i++)}return C(a,r)}function ls(t){switch(t){case"int8":case"uint8":case"bool":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;case"int64":case"uint64":case"float64":return 8;default:return 8}}function gn(t){return t.sqrt()}function hn(t,r){return t.power(r)}function An(t){return t.absolute()}function bn(t){return t.negative()}function Sn(t){return t.sign()}function Dn(t,r){return t.mod(r)}function Nn(t,r){return t.floor_divide(r)}function wn(t){return t.positive()}function xn(t){return t.reciprocal()}function In(t,r){return t.dot(r)}function zn(t){return t.trace()}function Bn(t,r=0,e=0,n=1){let o=Ae(t.storage,r,e,n);return I._fromStorage(o)}function _n(t,r){let e=Se(t.storage,r.storage);return I._fromStorage(e)}function En(t,r){return t.transpose(r)}function Un(t,r){return t.inner(r)}function Tn(t,r){return t.outer(r)}function On(t,r,e=2){return t.tensordot(r,e)}function $n(t){return t.sin()}function vn(t){return t.cos()}function Mn(t){return t.tan()}function Cn(t){return t.arcsin()}function kn(t){return t.arccos()}function Vn(t){return t.arctan()}function Pn(t,r){return t.arctan2(r)}function Fn(t,r){return t.hypot(r)}function Rn(t){return t.degrees()}function qn(t){return t.radians()}function Ln(t){return t.radians()}function jn(t){return t.degrees()}function Zn(t){return t.sinh()}function Wn(t){return t.cosh()}function Hn(t){return t.tanh()}function Yn(t){return t.arcsinh()}function Gn(t){return t.arccosh()}function Jn(t){return t.arctanh()}function Qn(t,r,e){return t.swapaxes(r,e)}function Xn(t,r,e){return t.moveaxis(r,e)}function Y(t,r=0){if(t.length===0)throw new Error("need at least one array to concatenate");let e=t.map(o=>o.storage),n=W(e,r);return I._fromStorage(n)}function Kn(t,r=0){if(t.length===0)throw new Error("need at least one array to stack");let e=t.map(o=>o.storage),n=Yt(e,r);return I._fromStorage(n)}function ut(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(n=>n.storage),e=Gt(r);return I._fromStorage(e)}function ro(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(n=>n.storage),e=Xr(r);return I._fromStorage(e)}function to(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(n=>n.storage),e=Jt(r);return I._fromStorage(e)}function eo(t,r,e=0){return Qt(t.storage,r,e).map(o=>I._fromStorage(o,t.base??t))}function no(t,r,e=0){return or(t.storage,r,e).map(o=>I._fromStorage(o,t.base??t))}function oo(t,r){return Kt(t.storage,r).map(n=>I._fromStorage(n,t.base??t))}function ao(t,r){return re(t.storage,r).map(n=>I._fromStorage(n,t.base??t))}function so(t,r){let e=te(t.storage,r);return I._fromStorage(e)}function io(t,r,e){return t.repeat(r,e)}function uo(t){return t.ravel()}function co(t,r){return t.reshape(...r)}function fo(t,r){return t.squeeze(r)}function lo(t,r){return t.expand_dims(r)}function zr(t,r){let e=ne(t.storage,r);return I._fromStorage(e)}function po(t){if(t.ndim<2)throw new Error("Input must be at least 2-D");return zr(t,1)}function mo(t){if(t.ndim<2)throw new Error("Input must be at least 2-D");return zr(t,0)}function yo(t,r=1,e=[0,1]){let n=oe(t.storage,r,e);return I._fromStorage(n)}function go(t,r,e){let n=ae(t.storage,r,e);return I._fromStorage(n)}function ho(t,r,e=0){let n=se(t.storage,r,e);return I._fromStorage(n,t.base??t)}function Ao(...t){let r=t.map(o=>o.storage),n=fe(r).map((o,a)=>o===r[a]?t[a]:I._fromStorage(o));return n.length===1?n[0]:n}function bo(...t){let r=t.map(o=>o.storage),n=le(r).map((o,a)=>o===r[a]?t[a]:I._fromStorage(o));return n.length===1?n[0]:n}function So(...t){let r=t.map(o=>o.storage),n=pe(r).map((o,a)=>o===r[a]?t[a]:I._fromStorage(o));return n.length===1?n[0]:n}function Do(t,r){return ie(t.storage,r).map(n=>I._fromStorage(n,t.base??t))}function No(t){if(t.length===0)throw new Error("need at least one array to stack");let r=t.map(n=>n.storage),e=ue(r);return I._fromStorage(e)}function wo(t){return ut(t)}function xo(t,r){let e=ce(t.storage,r);return I._fromStorage(e)}function Io(t,r,e){let n=r instanceof I?r:C(r,t.dtype);if(e===void 0){let o=t.flatten(),a=n.flatten();return Y([o,a])}return Y([t,n],e)}function zo(t,r,e){let n=t.dtype;if(e===void 0){let p=t.flatten(),d=(Array.isArray(r)?r:[r]).map(S=>S<0?p.size+S:S),g=[];for(let S=0;S<p.size;S++)d.includes(S)||g.push(S);let A=_(n),h=new A(g.length);for(let S=0;S<g.length;S++){let w=p.get([g[S]]);N(n)?h[S]=typeof w=="bigint"?w:BigInt(w):h[S]=w}let b=D.fromData(h,[g.length],n);return new I(b)}let o=t.shape,a=o.length,i=e<0?a+e:e;if(i<0||i>=a)throw new Error(`axis ${e} is out of bounds for array of dimension ${a}`);let s=o[i],u=Array.isArray(r)?r:[r],c=new Set(u.map(p=>p<0?s+p:p)),l=[],m=0;for(let p=0;p<=s;p++)(c.has(p)||p===s)&&(p>m&&l.push([m,p]),m=p+1);if(l.length===0){let p=[...o];return p[i]=0,F(p,n)}let f=[];for(let[p,y]of l){let d=o.map(()=>":");d[i]=`${p}:${y}`,f.push(t.slice(...d))}return Y(f,i)}function Bo(t,r,e,n){let o=e instanceof I?e:C(e,t.dtype);if(n===void 0){let m=t.flatten(),f=o.flatten(),p=r<0?m.size+r:r;if(p<0||p>m.size)throw new Error(`index ${r} is out of bounds for array of size ${m.size}`);let y=p>0?m.slice(`0:${p}`):null,d=p<m.size?m.slice(`${p}:`):null,g=[];return y&&g.push(y),g.push(f),d&&g.push(d),Y(g)}let a=t.shape,i=a.length,s=n<0?i+n:n;if(s<0||s>=i)throw new Error(`axis ${n} is out of bounds for array of dimension ${i}`);let u=a[s],c=r<0?u+r:r;if(c<0||c>u)throw new Error(`index ${r} is out of bounds for axis ${n} with size ${u}`);let l=[];if(c>0){let m=a.map(()=>":");m[s]=`0:${c}`,l.push(t.slice(...m))}if(l.push(o),c<u){let m=a.map(()=>":");m[s]=`${c}:`,l.push(t.slice(...m))}return Y(l,s)}function _o(t,r,e="constant",n=0){let o=t.shape,a=o.length,i=t.dtype,s;if(typeof r=="number"?s=o.map(()=>[r,r]):Array.isArray(r)&&typeof r[0]=="number"?s=o.map(()=>r):s=r,s.length!==a)throw new Error(`pad_width must have ${a} elements`);let u=o.map((d,g)=>d+s[g][0]+s[g][1]),c=u.reduce((d,g)=>d*g,1),l=_(i),m=new l(c),f=N(i);e==="constant"&&(f?m.fill(BigInt(n)):m.fill(n));let p=new Array(a).fill(0);for(let d=0;d<c;d++){let g=!0,A=[];for(let b=0;b<a;b++){let[S]=s[b],w=p[b]-S;if(w<0||w>=o[b]){g=!1;break}A.push(w)}let h;if(g)h=t.get(A);else if(e==="constant"){for(let b=a-1;b>=0&&(p[b]++,!(p[b]<u[b]));b--)p[b]=0;continue}else{let b=[];for(let S=0;S<a;S++){let[w]=s[S],x=p[S]-w,z=o[S];x<0?e==="edge"?x=0:e==="reflect"?(x=-x,x>=z&&(x=z-1)):e==="symmetric"?(x=-x-1,x>=z&&(x=z-1),x<0&&(x=0)):e==="wrap"&&(x=(x%z+z)%z):x>=z&&(e==="edge"?x=z-1:e==="reflect"?(x=2*z-x-2,x<0&&(x=0)):e==="symmetric"?(x=2*z-x-1,x<0&&(x=0)):e==="wrap"&&(x=x%z)),b.push(Math.max(0,Math.min(z-1,x)))}h=t.get(b)}f?m[d]=typeof h=="bigint"?h:BigInt(Number(h)):m[d]=Number(h);for(let b=a-1;b>=0&&(p[b]++,!(p[b]<u[b]));b--)p[b]=0}let y=D.fromData(m,u,i);return new I(y)}function Eo(t,r){let e=et(t.storage,r);return I._fromStorage(e,t.base??t)}function Uo(...t){let r=t.map(n=>n.storage);return Pe(r).map((n,o)=>I._fromStorage(n,t[o].base??t[o]))}function To(...t){return Mr(...t)}function Oo(t,r,e){return t.take(r,e)}function $o(t,r,e){t.put(r,e)}function vo(t,r){let e=r.map(o=>o.storage),n=qe(t.storage,e);return I._fromStorage(n)}function Mo(t,r,e=!1){return Le(t.storage,r.storage,e)}function Co(t,r){return jt(t.storage,r.storage)}function ko(t,r){return I._fromStorage(Lr(t.storage,r))}function Vo(t,r){return I._fromStorage(jr(t.storage,r))}function Po(t,r,e=!1){let n=Zr(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Fo(t,r,e=!1){let n=Wr(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Ro(t,r,e,n=!1){let o=Hr(t.storage,r,e,n);return typeof o=="number"?o:I._fromStorage(o)}function qo(t,r,e,n=!1){let o=J(t.storage,r,e,n);return typeof o=="number"?o:I._fromStorage(o)}function Lo(t,r,e,n=!1){let o=e?e.storage:void 0,a=gr(t.storage,r,o,n);return typeof a=="number"?a:I._fromStorage(a)}function jo(t,r,e=!1){let n=hr(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Zo(t,r,e=!1){let n=Ar(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Wo(t,r,e=!1){let n=br(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Ho(t,r,e=0,n=!1){let o=rr(t.storage,r,e,n);return typeof o=="number"?o:I._fromStorage(o)}function Yo(t,r,e=0,n=!1){let o=Yr(t.storage,r,e,n);return typeof o=="number"?o:I._fromStorage(o)}function Go(t,r,e=!1){let n=Sr(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Jo(t,r,e=!1){let n=Dr(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function Qo(t,r){let e=Nr(t.storage,r);return typeof e=="number"?e:I._fromStorage(e)}function Xo(t,r){let e=wr(t.storage,r);return typeof e=="number"?e:I._fromStorage(e)}function Ko(t,r){return I._fromStorage(Gr(t.storage,r))}function ra(t,r){return I._fromStorage(Jr(t.storage,r))}function ta(t,r,e=!1){let n=xr(t.storage,r,e);return typeof n=="number"?n:I._fromStorage(n)}function ea(t){return t.cbrt()}function na(t){return t.fabs()}function oa(t,r){return t.divmod(r)}function aa(t){return t.square()}function sa(t,r){return t.remainder(r)}function ia(t,r){return t.heaviside(r)}function ua(t,...r){let e=r.map(o=>o.storage),n=be(t,...e);return typeof n=="number"||typeof n=="bigint"?n:I._fromStorage(n)}var sr=new Uint8Array([147,78,85,77,80,89]),Br=["float64","float32","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"];function ct(){let t=new ArrayBuffer(2);return new DataView(t).setInt16(0,256,!0),new Int16Array(t)[0]===256}var ps={f8:"float64",f4:"float32",i8:"int64",i4:"int32",i2:"int16",i1:"int8",u8:"uint64",u4:"uint32",u2:"uint16",u1:"uint8",b1:"bool"},ir={float64:"<f8",float32:"<f4",int64:"<i8",int32:"<i4",int16:"<i2",int8:"|i1",uint64:"<u8",uint32:"<u4",uint16:"<u2",uint8:"|u1",bool:"|b1"},ca={c:"complex numbers",S:"byte strings",U:"Unicode strings",O:"Python objects",V:"structured arrays (void)",M:"datetime64",m:"timedelta64"};function fa(t){if(t.startsWith("[")||t.startsWith("("))throw new R(`Structured/compound dtypes are not supported: ${t}`);let r="",e=t;(t[0]==="<"||t[0]===">"||t[0]==="="||t[0]==="|")&&(r=t[0],e=t.slice(1));let n=e[0];if(n&&n in ca)throw new R(`Unsupported dtype: ${ca[n]} (${t}). Use the 'force' parameter to skip arrays with unsupported dtypes.`);let o=ps[e];if(!o)throw new R(`Unknown or unsupported dtype descriptor: ${t}. Supported types: ${Br.join(", ")}. Use the 'force' parameter to skip arrays with unsupported dtypes.`);let a=ct(),i=r==="<"||r==="|"||r==="="&&a,s=r===">"||r==="="&&!a,u=parseInt(e.slice(1),10),c=u>1&&(s&&a||i&&!a);return{dtype:o,needsByteSwap:c,itemsize:u}}var R=class extends Error{constructor(r){super(r),this.name="UnsupportedDTypeError"}},k=class extends Error{constructor(r){super(r),this.name="InvalidNpyError"}};function ur(t){let r=t instanceof ArrayBuffer?new Uint8Array(t):t,e=_r(r);return Er(r,e)}function _r(t){if(t.length<10)throw new k("File too small to be a valid NPY file");for(let c=0;c<sr.length;c++)if(t[c]!==sr[c])throw new k("Invalid NPY magic number");let r=t[6],e=t[7];if(r!==1&&r!==2&&r!==3)throw new k(`Unsupported NPY version: ${r}.${e}`);let n,o;r===1?(n=t[8]|t[9]<<8,o=10):(n=t[8]|t[9]<<8|t[10]<<16|t[11]<<24,o=12);let a=o+n;if(t.length<a)throw new k("File truncated: header extends beyond file");let i=t.slice(o,a),s=new TextDecoder("utf-8").decode(i).trim(),u=ms(s);return{version:{major:r,minor:e},header:u,dataOffset:a}}function Er(t,r){let{header:e,dataOffset:n}=r,{dtype:o,needsByteSwap:a,itemsize:i}=fa(e.descr),s=e.shape.reduce((d,g)=>d*g,1),u=s*i,c=t.length-n;if(c<u)throw new k(`File truncated: expected ${u} bytes of data, got ${c}`);let l=new ArrayBuffer(u);new Uint8Array(l).set(t.subarray(n,n+u));let f=ys(l,o,s,a,i),p=e.shape,y;if(e.fortran_order&&p.length>1){let d=[...p].reverse(),g=D.fromData(f,d,o);y=ds(g,d),p=e.shape}else y=D.fromData(f,[...p],o);return new I(y)}function ms(t){let r=t.match(/'descr'\s*:\s*'([^']+)'/),e=t.match(/'fortran_order'\s*:\s*(True|False)/),n=t.match(/'shape'\s*:\s*\(([^)]*)\)/);if(!r||!e||!n)throw new k(`Failed to parse NPY header: ${t}`);let o=r[1],a=e[1]==="True",i=n[1].trim(),s;return i===""?s=[]:s=i.split(",").map(u=>u.trim()).filter(u=>u!=="").map(u=>{let c=parseInt(u,10);if(isNaN(c))throw new k(`Invalid shape value: ${u}`);return c}),{descr:o,fortran_order:a,shape:s}}function ys(t,r,e,n,o){let a=_(r);if(!a)throw new k(`Cannot create array for dtype: ${r}`);if(!n)return new a(t,0,e);let i=new Uint8Array(t),s=new Uint8Array(t.byteLength);for(let u=0;u<e;u++){let c=u*o;for(let l=0;l<o;l++)s[c+l]=i[c+o-1-l]}return new a(s.buffer,0,e)}function ds(t,r){let e=r.length,n=t.size,o=t.dtype,a=_(o);if(!a)throw new k(`Cannot create array for dtype: ${o}`);let i=new a(n),s=[...r].reverse(),u=la(r),c=la(s),l=new Array(e).fill(0);for(let m=0;m<n;m++){let f=m;for(let y=0;y<e;y++){let d=u[y];l[y]=Math.floor(f/d),f=f%d}let p=0;for(let y=0;y<e;y++)p+=l[e-1-y]*c[y];N(o),i[p]=t.iget(m)}return D.fromData(i,s,o)}function la(t){let r=new Array(t.length),e=1;for(let n=t.length-1;n>=0;n--)r[n]=e,e*=t[n];return r}function Q(t){let r=t.shape,e=t.dtype,n=ir[e],o=r.length===0?"()":r.length===1?`(${r[0]},)`:`(${r.join(", ")})`,a=`{'descr': '${n}', 'fortran_order': False, 'shape': ${o}, }`,i=12,u=(64-(i+a.length+1)%64)%64;a=a+" ".repeat(u)+`
2
+ `;let c=new TextEncoder().encode(a),l=c.length,m=t.size,f=St(e),p=m*f,y=i+l+p,d=new Uint8Array(y);d.set(sr,0),d[6]=3,d[7]=0,d[8]=l&255,d[9]=l>>8&255,d[10]=l>>16&255,d[11]=l>>24&255,d.set(c,i);let g=i+l;return gs(t,d.subarray(g),f),d}function gs(t,r,e){let n=t.dtype,o=t.size,a=ct(),i=N(n),s=t._storage;if(s.isCContiguous&&s.offset===0&&a){let c=s.data,l=new Uint8Array(c.buffer,c.byteOffset,o*e);r.set(l)}else{let c=new DataView(r.buffer,r.byteOffset,r.byteLength);for(let l=0;l<o;l++){let m=s.iget(l),f=l*e;i?hs(c,f,m,n==="uint64"):As(c,f,m,n)}}}function hs(t,r,e,n){n?t.setBigUint64(r,e,!0):t.setBigInt64(r,e,!0)}function As(t,r,e,n){switch(n){case"float64":t.setFloat64(r,e,!0);break;case"float32":t.setFloat32(r,e,!0);break;case"int32":t.setInt32(r,e,!0);break;case"int16":t.setInt16(r,e,!0);break;case"int8":t.setInt8(r,e);break;case"uint32":t.setUint32(r,e,!0);break;case"uint16":t.setUint16(r,e,!0);break;case"uint8":case"bool":t.setUint8(r,e);break;default:throw new Error(`Unsupported dtype for serialization: ${n}`)}}var bs=(()=>{let t=new Uint32Array(256);for(let r=0;r<256;r++){let e=r;for(let n=0;n<8;n++)e=e&1?3988292384^e>>>1:e>>>1;t[r]=e}return t})();function ft(t){let r=4294967295;for(let e=0;e<t.length;e++)r=bs[(r^t[e])&255]^r>>>8;return(r^4294967295)>>>0}async function pa(t){let r=ya(t),e=new Map;for(let n of r){let o=await Ss(n);e.set(n.name,o)}return e}function ma(t){let r=ya(t),e=new Map;for(let n of r){if(n.compressionMethod!==0)throw new Error(`Cannot read compressed entry synchronously: ${n.name}. Use readZip() (async) for DEFLATE-compressed files.`);e.set(n.name,n.compressedData)}return e}function ya(t){let r=t instanceof ArrayBuffer?new Uint8Array(t):t,e=new DataView(r.buffer,r.byteOffset,r.byteLength),n=[],o=-1;for(let c=r.length-22;c>=0;c--)if(e.getUint32(c,!0)===101010256){o=c;break}if(o===-1)throw new Error("Invalid ZIP file: end of central directory not found");let a=e.getUint32(o+16,!0),i=e.getUint16(o+10,!0),s=[],u=a;for(let c=0;c<i&&e.getUint32(u,!0)===33639248;c++){let m=e.getUint16(u+10,!0),f=e.getUint32(u+16,!0),p=e.getUint32(u+20,!0),y=e.getUint32(u+24,!0),d=e.getUint16(u+28,!0),g=e.getUint16(u+30,!0),A=e.getUint16(u+32,!0),h=e.getUint32(u+42,!0),b=r.slice(u+46,u+46+d),S=new TextDecoder("utf-8").decode(b);s.push({name:S,compressionMethod:m,crc32:f,compressedSize:p,uncompressedSize:y,localHeaderOffset:h}),u=u+46+d+g+A}for(let c of s){let l=c.localHeaderOffset;if(e.getUint32(l,!0)!==67324752)throw new Error(`Invalid local file header at offset ${l}`);let f=e.getUint16(l+26,!0),p=e.getUint16(l+28,!0),y=l+30+f+p,d=r.slice(y,y+c.compressedSize);n.push({name:c.name,compressedData:d,compressionMethod:c.compressionMethod,crc32:c.crc32,compressedSize:c.compressedSize,uncompressedSize:c.uncompressedSize})}return n}async function Ss(t){if(t.compressionMethod===0)return t.compressedData;if(t.compressionMethod===8)return await Ds(t.compressedData);throw new Error(`Unsupported compression method: ${t.compressionMethod}`)}async function Ds(t){if(typeof DecompressionStream>"u")throw new Error("DecompressionStream is not available. This environment does not support the Compression Streams API. Please use a modern browser or Node.js 18+.");let r=new DecompressionStream("deflate-raw"),e=new Uint8Array(t.length);e.set(t);let n=r.writable.getWriter();n.write(e),n.close();let o=r.readable.getReader(),a=[];for(;;){let{done:c,value:l}=await o.read();if(c)break;a.push(l)}let i=a.reduce((c,l)=>c+l.length,0),s=new Uint8Array(i),u=0;for(let c of a)s.set(c,u),u+=c.length;return s}async function Ur(t,r={}){let e=r.force??!1,n=await pa(t);return da(n,e)}function Tr(t,r={}){let e=r.force??!1,n=ma(t);return da(n,e)}function da(t,r){let e=new Map,n=[],o=new Map;for(let[a,i]of t){if(!a.endsWith(".npy"))continue;let s=a.slice(0,-4);try{let u=ur(i);e.set(s,u)}catch(u){if(u instanceof R&&r)n.push(s),o.set(s,u.message);else throw u}}return{arrays:e,skipped:n,errors:o}}async function mt(t,r={}){let e=await Ur(t,r);return Object.fromEntries(e.arrays)}function yt(t,r={}){let e=Tr(t,r);return Object.fromEntries(e.arrays)}async function ga(t,r={}){let e=r.compress??!1,n=[];for(let[f,p]of t){let y=ft(p),d,g;e?(d=await ws(p),d.length<p.length?g=8:(d=p,g=0)):(d=p,g=0),n.push({name:f,data:p,compressedData:d,crc:y,compressionMethod:g,offset:0})}let o=0;for(let f of n){let p=new TextEncoder().encode(f.name);o+=30+p.length+f.compressedData.length}let a=0;for(let f of n){let p=new TextEncoder().encode(f.name);a+=46+p.length}let s=o+a+22,u=new Uint8Array(s),c=new DataView(u.buffer),l=0;for(let f of n)f.offset=l,l=Aa(u,c,l,f);let m=l;for(let f of n)l=ba(u,c,l,f);return Sa(c,l,n.length,a,m),u}function ha(t){let r=[];for(let[l,m]of t){let f=ft(m);r.push({name:l,data:m,compressedData:m,crc:f,compressionMethod:0,offset:0})}let e=0;for(let l of r){let m=new TextEncoder().encode(l.name);e+=30+m.length+l.compressedData.length}let n=0;for(let l of r){let m=new TextEncoder().encode(l.name);n+=46+m.length}let a=e+n+22,i=new Uint8Array(a),s=new DataView(i.buffer),u=0;for(let l of r)l.offset=u,u=Aa(i,s,u,l);let c=u;for(let l of r)u=ba(i,s,u,l);return Sa(s,u,r.length,n,c),i}function Aa(t,r,e,n){let o=new TextEncoder().encode(n.name);return r.setUint32(e,67324752,!0),e+=4,r.setUint16(e,n.compressionMethod===8?20:10,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,n.compressionMethod,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,33,!0),e+=2,r.setUint32(e,n.crc,!0),e+=4,r.setUint32(e,n.compressedData.length,!0),e+=4,r.setUint32(e,n.data.length,!0),e+=4,r.setUint16(e,o.length,!0),e+=2,r.setUint16(e,0,!0),e+=2,t.set(o,e),e+=o.length,t.set(n.compressedData,e),e+=n.compressedData.length,e}function ba(t,r,e,n){let o=new TextEncoder().encode(n.name);return r.setUint32(e,33639248,!0),e+=4,r.setUint16(e,20,!0),e+=2,r.setUint16(e,n.compressionMethod===8?20:10,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,n.compressionMethod,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,33,!0),e+=2,r.setUint32(e,n.crc,!0),e+=4,r.setUint32(e,n.compressedData.length,!0),e+=4,r.setUint32(e,n.data.length,!0),e+=4,r.setUint16(e,o.length,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint16(e,0,!0),e+=2,r.setUint32(e,0,!0),e+=4,r.setUint32(e,n.offset,!0),e+=4,t.set(o,e),e+=o.length,e}function Sa(t,r,e,n,o){t.setUint32(r,101010256,!0),r+=4,t.setUint16(r,0,!0),r+=2,t.setUint16(r,0,!0),r+=2,t.setUint16(r,e,!0),r+=2,t.setUint16(r,e,!0),r+=2,t.setUint32(r,n,!0),r+=4,t.setUint32(r,o,!0),r+=4,t.setUint16(r,0,!0)}async function ws(t){if(typeof CompressionStream>"u")throw new Error("CompressionStream is not available. This environment does not support the Compression Streams API. Please use a modern browser or Node.js 18+.");let r=new CompressionStream("deflate-raw"),e=new Uint8Array(t.length);e.set(t);let n=r.writable.getWriter();n.write(e),n.close();let o=r.readable.getReader(),a=[];for(;;){let{done:c,value:l}=await o.read();if(c)break;a.push(l)}let i=a.reduce((c,l)=>c+l.length,0),s=new Uint8Array(i),u=0;for(let c of a)s.set(c,u),u+=c.length;return s}async function dt(t,r={}){let e=Da(t);return ga(e,{compress:r.compress??!1})}function gt(t){let r=Da(t);return ha(r)}function Da(t){let r=new Map;if(Array.isArray(t)){for(let n=0;n<t.length;n++){let o=t[n],a=Q(o);r.set(`arr_${n}.npy`,a)}return r}let e=t instanceof Map?t.entries():Object.entries(t);for(let[n,o]of e){if(typeof n!="string"||n.length===0)throw new Error("Array names must be non-empty strings");let a=Q(o),i=n.endsWith(".npy")?n:`${n}.npy`;r.set(i,a)}return r}var xs="0.6.0";return Ba(Is);})();