numpy-ts 0.8.0 → 0.9.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 +12 -12
- package/dist/numpy-ts.browser.js +2 -2
- package/dist/numpy-ts.esm.js +2 -2
- package/dist/numpy-ts.node-io.cjs +3 -3
- package/dist/numpy-ts.node-io.cjs.map +4 -4
- package/dist/numpy-ts.node-io.mjs +3 -3
- package/dist/numpy-ts.node-io.mjs.map +4 -4
- package/dist/numpy-ts.node.cjs +2 -2
- package/dist/numpy-ts.node.cjs.map +4 -4
- package/dist/types/core/ndarray.d.ts +402 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/ops/exponential.d.ts +79 -1
- package/dist/types/ops/gradient.d.ts +57 -0
- package/dist/types/ops/linalg.d.ts +220 -0
- package/dist/types/ops/rounding.d.ts +36 -0
- package/dist/types/ops/sets.d.ts +38 -0
- package/package.json +1 -1
package/dist/numpy-ts.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function zt(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 _t(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 s=e<0?t+e:e;if(s<0||s>=t)throw new Error(`Index ${e} is out of bounds for size ${t}`);return{start:s,stop:s+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 k="float64";function O(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 Et(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 Ut(r){return r==="int64"||r==="int32"||r==="int16"||r==="int8"||r==="uint64"||r==="uint32"||r==="uint16"||r==="uint8"}function Bt(r){return r==="float64"||r==="float32"}function x(r){return r==="int64"||r==="uint64"}function C(r,t){if(r===t)return r;if(r==="bool")return t;if(t==="bool")return r;if(Bt(r)||Bt(t)){if(r==="float64"||t==="float64")return"float64";if(r==="float32"){let c=t;return c==="int32"||c==="int64"||c==="uint32"||c==="uint64"?"float64":"float32"}if(t==="float32"){let c=r;return c==="int32"||c==="int64"||c==="uint32"||c==="uint64"?"float64":"float32"}return"float32"}let e=r.startsWith("int"),n=t.startsWith("int"),o=r.startsWith("uint"),a=t.startsWith("uint"),s=c=>c.includes("64")?64:c.includes("32")?32:c.includes("16")?16:c.includes("8")?8:0,i=s(r),u=s(t);if(r==="int64"&&t==="uint64"||r==="uint64"&&t==="int64")return"float64";if(e&&a&&i===u){if(i===8)return"int16";if(i===16)return"int32";if(i===32)return"int64"}if(o&&n&&i===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(i,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?i>u?r:u===8?"int16":u===16?"int32":u===32?"int64":"float64":o&&n?u>i?t:i===8?"int16":i===16?"int32":i===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,s=this._offset;for(let i=0;i<o;i++){let u=1;for(let l=i+1;l<o;l++)u*=e[l];let c=Math.floor(a/u);a=a%u,s+=c*n[i]}return this._data[s]}iset(t,e){let n=this._shape,o=this._strides,a=n.length;if(a===0){this._data[this._offset]=e;return}let s=t,i=this._offset;for(let u=0;u<a;u++){let c=1;for(let p=u+1;p<a;p++)c*=n[p];let l=Math.floor(s/c);s=s%c,i+=l*o[u]}this._data[i]=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=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(x(e)){let s=this._data,i=a;for(let u=0;u<n;u++)i[u]=s[u]}else a.set(this._data);else if(x(e)){let s=a;for(let i=0;i<n;i++)s[i]=this.iget(i)}else for(let s=0;s<n;s++)a[s]=this.iget(s);return new r(a,t,r._computeStrides(t),0,e)}static fromData(t,e,n,o,a){let s=o??r._computeStrides(e),i=a??0;return new r(t,e,s,i,n)}static zeros(t,e=k){let n=t.reduce((s,i)=>s*i,1),o=O(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=k){let n=t.reduce((s,i)=>s*i,1),o=O(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return x(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 R(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 Tt(r,t){let e=r.length,n=t.length,o=Math.max(e,n),a=new Array(o);for(let s=0;s<o;s++){let i=s<o-e?1:r[s-(o-e)],u=s<o-n?1:t[s-(o-n)];if(i===u)a[s]=i;else if(i===1)a[s]=u;else if(u===1)a[s]=i;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 ro(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let s=0;s<n;s++){let i=o-n+s,u=r[s],c=e[i];if(u===c)a[i]=t[s];else if(u===1)a[i]=0;else throw new Error("Invalid broadcast")}return a}function mr(r,t){let e=ro(r.shape,r.strides,t);return D.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function P(r,t,e,n){let o=Tt(r.shape,t.shape),a=mr(r,o),s=mr(t,o),i=C(r.dtype,t.dtype),u=D.zeros(o,i),c=u.data,l=u.size;if(x(i)){let p=c;for(let f=0;f<l;f++){let y=a.iget(f),m=s.iget(f),d=typeof y=="bigint"?y:BigInt(Math.round(y)),g=typeof m=="bigint"?m:BigInt(Math.round(m));n==="add"?p[f]=d+g:n==="subtract"?p[f]=d-g:n==="multiply"?p[f]=d*g:n==="divide"?p[f]=d/g:p[f]=BigInt(Math.round(e(Number(d),Number(g))))}}else{let p=x(r.dtype)||x(t.dtype);for(let f=0;f<l;f++){let y=a.iget(f),m=s.iget(f),d=Number(y),g=Number(m);c[f]=e(d,g)}}return u}function J(r,t,e){let n=Tt(r.shape,t.shape),o=mr(r,n),a=mr(t,n),s=n.reduce((c,l)=>c*l,1),i=new Uint8Array(s),u=x(r.dtype)||x(t.dtype);for(let c=0;c<s;c++){let l=o.iget(c),p=a.iget(c),f=Number(l),y=Number(p);i[c]=e(f,y)?1:0}return D.fromData(i,n,"bool")}function V(r,t,e=!0){let n=r.dtype,o=Array.from(r.shape),a=r.size,i=e?n:n!=="float32"&&n!=="float64"?"float64":n,u=D.zeros(o,i),c=u.data,l=r.data;if(x(n))if(x(i)){let p=c;for(let f=0;f<a;f++){let y=Number(l[f]);p[f]=BigInt(Math.round(t(y)))}}else for(let p=0;p<a;p++)c[p]=t(Number(l[p]));else for(let p=0;p<a;p++)c[p]=t(Number(l[p]));return u}function Cr(r,t){return r.isCContiguous&&t.isCContiguous&&r.shape.length===t.shape.length&&r.shape.every((e,n)=>e===t.shape[n])}function vt(r,t){return typeof t=="number"?oo(r,t):Cr(r,t)?to(r,t):P(r,t,(e,n)=>e+n,"add")}function to(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;if(!x(r.dtype)||!x(t.dtype))for(let l=0;l<o;l++){let p=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof s[l]=="bigint"?s[l]:BigInt(Math.round(Number(s[l])));u[l]=p+f}else{let l=a,p=s;for(let f=0;f<o;f++)u[f]=l[f]+p[f]}}else if(x(r.dtype)||x(t.dtype))for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?Number(a[c]):a[c],p=typeof s[c]=="bigint"?Number(s[c]):s[c];i[c]=l+p}else for(let c=0;c<o;c++)i[c]=a[c]+s[c];return n}function Ot(r,t){return typeof t=="number"?ao(r,t):Cr(r,t)?eo(r,t):P(r,t,(e,n)=>e-n,"subtract")}function eo(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;if(!x(r.dtype)||!x(t.dtype))for(let l=0;l<o;l++){let p=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof s[l]=="bigint"?s[l]:BigInt(Math.round(Number(s[l])));u[l]=p-f}else{let l=a,p=s;for(let f=0;f<o;f++)u[f]=l[f]-p[f]}}else if(x(r.dtype)||x(t.dtype))for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?Number(a[c]):a[c],p=typeof s[c]=="bigint"?Number(s[c]):s[c];i[c]=l-p}else for(let c=0;c<o;c++)i[c]=a[c]-s[c];return n}function $t(r,t){return typeof t=="number"?so(r,t):Cr(r,t)?no(r,t):P(r,t,(e,n)=>e*n,"multiply")}function no(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;if(!x(r.dtype)||!x(t.dtype))for(let l=0;l<o;l++){let p=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof s[l]=="bigint"?s[l]:BigInt(Math.round(Number(s[l])));u[l]=p*f}else{let l=a,p=s;for(let f=0;f<o;f++)u[f]=l[f]*p[f]}}else if(x(r.dtype)||x(t.dtype))for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?Number(a[c]):a[c],p=typeof s[c]=="bigint"?Number(s[c]):s[c];i[c]=l*p}else for(let c=0;c<o;c++)i[c]=a[c]*s[c];return n}function Mt(r,t){if(typeof t=="number")return io(r,t);let e=r.dtype==="float64",n=t.dtype==="float64",o=r.dtype==="float32",a=t.dtype==="float32";if(e||n){let u=e?r:rr(r,"float64"),c=n?t:rr(t,"float64");return P(u,c,(l,p)=>l/p,"divide")}if(o||a){let u=o?r:rr(r,"float32"),c=a?t:rr(t,"float32");return P(u,c,(l,p)=>l/p,"divide")}let s=rr(r,"float64"),i=rr(t,"float64");return P(s,i,(u,c)=>u/c,"divide")}function rr(r,t){let e=D.zeros(Array.from(r.shape),t),n=r.size,o=r.data,a=e.data;for(let s=0;s<n;s++)a[s]=Number(o[s]);return e}function oo(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]+l}else for(let u=0;u<a;u++)i[u]=Number(o[u])+t;return s}function ao(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]-l}else for(let u=0;u<a;u++)i[u]=Number(o[u])-t;return s}function so(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]*l}else for(let u=0;u<a;u++)i[u]=Number(o[u])*t;return s}function io(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=e!=="float32"&&e!=="float64"?"float64":e,u=D.zeros(n,i),c=u.data;if(x(e))for(let l=0;l<a;l++)c[l]=Number(o[l])/t;else for(let l=0;l<a;l++)c[l]=Number(o[l])/t;return u}function Ct(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),s=a.data;if(x(t)){let i=n,u=s;for(let c=0;c<o;c++){let l=i[c];u[c]=l<0n?-l:l}}else for(let i=0;i<o;i++)s[i]=Math.abs(Number(n[i]));return a}function kt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),s=a.data;if(x(t)){let i=n,u=s;for(let c=0;c<o;c++)u[c]=-i[c]}else for(let i=0;i<o;i++)s[i]=-Number(n[i]);return a}function Vt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),s=a.data;if(x(t)){let i=n,u=s;for(let c=0;c<o;c++){let l=i[c];u[c]=l>0n?1n:l<0n?-1n:0n}}else for(let i=0;i<o;i++){let u=Number(n[i]);s[i]=u>0?1:u<0?-1:0}return a}function dr(r,t){return typeof t=="number"?uo(r,t):P(r,t,(e,n)=>(e%n+n)%n,"mod")}function uo(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++){let f=u[p];c[p]=(f%l+l)%l}}else for(let u=0;u<a;u++){let c=Number(o[u]);i[u]=(c%t+t)%t}return s}function kr(r,t){return typeof t=="number"?co(r,t):P(r,t,(e,n)=>Math.floor(e/n),"floor_divide")}function co(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]/l}else for(let u=0;u<a;u++)i[u]=Math.floor(Number(o[u])/t);return s}function Ft(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),s=a.data;for(let i=0;i<o;i++)s[i]=n[i];return a}function Pt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,s=t!=="float32"&&t!=="float64"?"float64":t,i=D.zeros(e,s),u=i.data;if(x(t))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 i}function Rt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,s=t!=="float32"&&t!=="float64"?"float64":t,i=D.zeros(e,s),u=i.data;for(let c=0;c<o;c++)u[c]=Math.cbrt(Number(n[c]));return i}function Lt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",s=D.zeros(e,a),i=s.data;for(let u=0;u<o;u++)i[u]=Math.abs(Number(n[u]));return s}function qt(r,t){let e=kr(r,t),n=dr(r,t);return[e,n]}function jt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),s=a.data;if(x(t)){let i=n,u=s;for(let c=0;c<o;c++)u[c]=i[c]*i[c]}else for(let i=0;i<o;i++){let u=Number(n[i]);s[i]=u*u}return a}function Zt(r,t){return dr(r,t)}function Wt(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.size,a=e==="float32"?"float32":"float64",s=D.zeros(n,a),i=s.data;if(typeof t=="number")for(let u=0;u<o;u++){let c=Number(r.data[u]);c<0?i[u]=0:c===0?i[u]=t:i[u]=1}else{let u=t.data,c=t.shape;if(n.every((l,p)=>l===c[p]))for(let l=0;l<o;l++){let p=Number(r.data[l]);p<0?i[l]=0:p===0?i[l]=Number(u[l]):i[l]=1}else for(let l=0;l<o;l++){let p=Number(r.data[l]),f=l%t.size;p<0?i[l]=0:p===0?i[l]=Number(u[f]):i[l]=1}}return s}function X(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 s=a.length-t+n,i=s<0?1:a[s];if(i!==1){if(o===1)o=i;else if(o!==i)return null}}e[n]=o}return e}function fo(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let s=0;s<n;s++){let i=o-n+s,u=r[s],c=e[i];if(u===c)a[i]=t[s];else if(u===1)a[i]=0;else throw new Error("Invalid broadcast")}return a}function G(r,t){let e=fo(r.shape,r.strides,t);return D.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function Vr(...r){let t=X(r);if(t===null){let e=r.map(n=>`(${n.join(",")})`).join(" ");throw new Error(`shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between ${e}`)}return t}function Gt(r,t){return typeof t=="number"?po(r,t):J(r,t,(e,n)=>e>n)}function Ht(r,t){return typeof t=="number"?yo(r,t):J(r,t,(e,n)=>e>=n)}function Yt(r,t){return typeof t=="number"?mo(r,t):J(r,t,(e,n)=>e<n)}function Jt(r,t){return typeof t=="number"?go(r,t):J(r,t,(e,n)=>e<=n)}function Xt(r,t){return typeof t=="number"?ho(r,t):J(r,t,(e,n)=>e===n)}function Qt(r,t){return typeof t=="number"?Ao(r,t):J(r,t,(e,n)=>e!==n)}function Fr(r,t,e=1e-5,n=1e-8){return typeof t=="number"?bo(r,t,e,n):J(r,t,(o,a)=>{let s=Math.abs(o-a),i=n+e*Math.abs(a);return s<=i})}function Kt(r,t,e=1e-5,n=1e-8){let o=Fr(r,t,e,n),a=o.data;for(let s=0;s<o.size;s++)if(a[s]===0)return!1;return!0}function re(r,t){let e=[Array.from(r.shape),Array.from(t.shape)],n=X(e);if(n===null)return!1;let o=G(r,n),a=G(t,n),s=n.length,i=n.reduce((l,p)=>l*p,1),u=x(o.dtype),c=x(a.dtype);for(let l=0;l<i;l++){let p=l,f=new Array(s);for(let d=s-1;d>=0;d--)f[d]=p%n[d],p=Math.floor(p/n[d]);let y=o.get(...f),m=a.get(...f);if(u||c){let d=typeof y=="bigint"?y:BigInt(Number(y)),g=typeof m=="bigint"?m:BigInt(Number(m));if(d!==g)return!1}else if(y!==m)return!1}return!0}function po(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 yo(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 mo(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 go(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 ho(r,t){let e=new Uint8Array(r.size),n=r.data,o=r.dtype;if(x(o)){let a=BigInt(Math.round(t)),s=n;for(let i=0;i<r.size;i++)e[i]=s[i]===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 Ao(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 bo(r,t,e,n){let o=new Uint8Array(r.size),a=r.data,s=r.dtype;if(x(s)){let i=a;for(let u=0;u<r.size;u++){let c=Number(i[u]),l=Math.abs(c-t),p=n+e*Math.abs(t);o[u]=l<=p?1:0}}else for(let i=0;i<r.size;i++){let u=Number(a[i]),c=Math.abs(u-t),l=n+e*Math.abs(t);o[i]=c<=l?1:0}return D.fromData(o,Array.from(r.shape),"bool")}function T(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 v(r,t,e,n){let o=n.length,a=new Array(o),s=Array.from(n).filter((u,c)=>c!==t),i=r;for(let u=s.length-1;u>=0;u--)a[u>=t?u+1:u]=i%s[u],i=Math.floor(i/s[u]);return a[t]=e,a}function or(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,s=r.size,i=r.data;if(t===void 0)if(x(n)){let m=i,d=BigInt(0);for(let g=0;g<s;g++)d+=m[g];return Number(d)}else{let m=0;for(let d=0;d<s;d++)m+=Number(i[d]);return m}let u=t;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((m,d)=>d!==u);if(c.length===0)return or(r);let l=D.zeros(c,n),p=l.data,f=o[u],y=c.reduce((m,d)=>m*d,1);if(x(n)){let m=i,d=p;for(let g=0;g<y;g++){let A=BigInt(0);for(let h=0;h<f;h++){let b=v(g,u,h,o),S=T(b,o);A+=m[S]}d[g]=A}}else for(let m=0;m<y;m++){let d=0;for(let g=0;g<f;g++){let A=v(m,u,g,o),h=T(A,o);d+=Number(i[h])}p[m]=d}if(e){let m=[...o];return m[u]=1,D.fromData(p,m,n)}return l}function gr(r,t,e=!1){let n=r.dtype,o=r.shape;if(t===void 0)return or(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 s=or(r,t,e);if(typeof s=="number")return s/o[a];let i=o[a],u=n;(x(n)||n.startsWith("int")||n.startsWith("uint"))&&(u="float64");let c=D.zeros(Array.from(s.shape),u),l=c.data,p=s.data;if(x(n)){let f=p;for(let y=0;y<l.length;y++)l[y]=Number(f[y])/i}else for(let f=0;f<l.length;f++)l[f]=Number(p[f])/i;return c}function hr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,s=r.size,i=r.data;if(t===void 0){if(s===0)throw new Error("max of empty array");let m=i[0];for(let d=1;d<s;d++)i[d]>m&&(m=i[d]);return Number(m)}let u=t;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((m,d)=>d!==u);if(c.length===0)return hr(r);let l=D.zeros(c,n),p=l.data,f=o[u],y=c.reduce((m,d)=>m*d,1);if(x(n)){let m=i,d=p;for(let g=0;g<y;g++){let A=v(g,u,0,o),h=T(A,o),b=m[h];for(let S=1;S<f;S++){let N=v(g,u,S,o),w=T(N,o),I=m[w];I>b&&(b=I)}d[g]=b}}else for(let m=0;m<y;m++){let d=-1/0;for(let g=0;g<f;g++){let A=v(m,u,g,o),h=T(A,o),b=Number(i[h]);b>d&&(d=b)}p[m]=d}if(e){let m=[...o];return m[u]=1,D.fromData(p,m,n)}return l}function Pr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,s=r.size,i=r.data;if(t===void 0)if(x(n)){let m=i,d=BigInt(1);for(let g=0;g<s;g++)d*=m[g];return Number(d)}else{let m=1;for(let d=0;d<s;d++)m*=Number(i[d]);return m}let u=t;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((m,d)=>d!==u);if(c.length===0)return Pr(r);let l=D.zeros(c,n),p=l.data,f=o[u],y=c.reduce((m,d)=>m*d,1);if(x(n)){let m=i,d=p;for(let g=0;g<y;g++){let A=BigInt(1);for(let h=0;h<f;h++){let b=v(g,u,h,o),S=T(b,o);A*=m[S]}d[g]=A}}else for(let m=0;m<y;m++){let d=1;for(let g=0;g<f;g++){let A=v(m,u,g,o),h=T(A,o);d*=Number(i[h])}p[m]=d}if(e){let m=[...o];return m[u]=1,D.fromData(p,m,n)}return l}function Ar(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,s=r.size,i=r.data;if(t===void 0){if(s===0)throw new Error("min of empty array");let m=i[0];for(let d=1;d<s;d++)i[d]<m&&(m=i[d]);return Number(m)}let u=t;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let c=Array.from(o).filter((m,d)=>d!==u);if(c.length===0)return Ar(r);let l=D.zeros(c,n),p=l.data,f=o[u],y=c.reduce((m,d)=>m*d,1);if(x(n)){let m=i,d=p;for(let g=0;g<y;g++){let A=v(g,u,0,o),h=T(A,o),b=m[h];for(let S=1;S<f;S++){let N=v(g,u,S,o),w=T(N,o),I=m[w];I<b&&(b=I)}d[g]=b}}else for(let m=0;m<y;m++){let d=1/0;for(let g=0;g<f;g++){let A=v(m,u,g,o),h=T(A,o),b=Number(i[h]);b<d&&(d=b)}p[m]=d}if(e){let m=[...o];return m[u]=1,D.fromData(p,m,n)}return l}function Rr(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,s=r.data;if(t===void 0){if(a===0)throw new Error("argmin of empty array");let y=s[0],m=0;for(let d=1;d<a;d++)s[d]<y&&(y=s[d],m=d);return m}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((y,m)=>m!==i);if(u.length===0)return Rr(r);let c=D.zeros(u,"int32"),l=c.data,p=n[i],f=u.reduce((y,m)=>y*m,1);if(x(e)){let y=s;for(let m=0;m<f;m++){let d=v(m,i,0,n),g=T(d,n),A=y[g],h=0;for(let b=1;b<p;b++){let S=v(m,i,b,n),N=T(S,n),w=y[N];w<A&&(A=w,h=b)}l[m]=h}}else for(let y=0;y<f;y++){let m=1/0,d=0;for(let g=0;g<p;g++){let A=v(y,i,g,n),h=T(A,n),b=Number(s[h]);b<m&&(m=b,d=g)}l[y]=d}return c}function Lr(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,s=r.data;if(t===void 0){if(a===0)throw new Error("argmax of empty array");let y=s[0],m=0;for(let d=1;d<a;d++)s[d]>y&&(y=s[d],m=d);return m}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((y,m)=>m!==i);if(u.length===0)return Lr(r);let c=D.zeros(u,"int32"),l=c.data,p=n[i],f=u.reduce((y,m)=>y*m,1);if(x(e)){let y=s;for(let m=0;m<f;m++){let d=v(m,i,0,n),g=T(d,n),A=y[g],h=0;for(let b=1;b<p;b++){let S=v(m,i,b,n),N=T(S,n),w=y[N];w>A&&(A=w,h=b)}l[m]=h}}else for(let y=0;y<f;y++){let m=-1/0,d=0;for(let g=0;g<p;g++){let A=v(y,i,g,n),h=T(A,n),b=Number(s[h]);b>m&&(m=b,d=g)}l[y]=d}return c}function qr(r,t,e=0,n=!1){let o=r.shape,a=o.length,s=r.size,i=r.data,u=gr(r,t,n);if(t===void 0){let A=u,h=0;for(let b=0;b<s;b++){let S=Number(i[b])-A;h+=S*S}return h/(s-e)}let c=t;if(c<0&&(c=a+c),c<0||c>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let l=o[c],p=u,f=p.data,y=n?p.shape:Array.from(o).filter((A,h)=>h!==c),m=D.zeros(Array.from(y),"float64"),d=m.data,g=y.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 N=v(A,c,S,o),w=T(N,o),I=Number(i[w])-b;h+=I*I}d[A]=h/(l-e)}return m}function te(r,t,e=0,n=!1){let o=qr(r,t,e,n);if(typeof o=="number")return Math.sqrt(o);let a=D.zeros(Array.from(o.shape),"float64"),s=o.data,i=a.data;for(let u=0;u<s.length;u++)i[u]=Math.sqrt(Number(s[u]));return a}function jr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,s=r.data;if(t===void 0){for(let y=0;y<a;y++)if(!s[y])return!1;return!0}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((y,m)=>m!==i);if(u.length===0)return jr(r);let c=D.zeros(u,"bool"),l=c.data,p=n[i],f=u.reduce((y,m)=>y*m,1);for(let y=0;y<f;y++){let m=!0;for(let d=0;d<p;d++){let g=v(y,i,d,n),A=T(g,n);if(!s[A]){m=!1;break}}l[y]=m?1:0}if(e){let y=[...n];return y[i]=1,D.fromData(l,y,"bool")}return c}function Zr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,s=r.data;if(t===void 0){for(let y=0;y<a;y++)if(s[y])return!0;return!1}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((y,m)=>m!==i);if(u.length===0)return Zr(r);let c=D.zeros(u,"bool"),l=c.data,p=n[i],f=u.reduce((y,m)=>y*m,1);for(let y=0;y<f;y++){let m=!1;for(let d=0;d<p;d++){let g=v(y,i,d,n),A=T(g,n);if(s[A]){m=!0;break}}l[y]=m?1:0}if(e){let y=[...n];return y[i]=1,D.fromData(l,y,"bool")}return c}function Wr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.size,y=new Float64Array(f),m=0;for(let d=0;d<f;d++)m+=Number(o[d]),y[d]=m;return D.fromData(y,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let s=new Float64Array(r.size),i=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=r.size,p=u[a];for(let f=0;f<l;f++)Math.floor(f/p)%i===0?s[f]=Number(o[f]):s[f]=s[f-p]+Number(o[f]);return D.fromData(s,[...e],"float64")}function Gr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.size,y=new Float64Array(f),m=1;for(let d=0;d<f;d++)m*=Number(o[d]),y[d]=m;return D.fromData(y,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let s=new Float64Array(r.size),i=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=r.size,p=u[a];for(let f=0;f<l;f++)Math.floor(f/p)%i===0?s[f]=Number(o[f]):s[f]=s[f-p]*Number(o[f]);return D.fromData(s,[...e],"float64")}function Hr(r,t,e=!1){let n=hr(r,t,e),o=Ar(r,t,e);if(typeof n=="number"&&typeof o=="number")return n-o;let a=n,s=o,i=a.data,u=s.data,c=new Float64Array(a.size);for(let l=0;l<a.size;l++)c[l]=Number(i[l])-Number(u[l]);return D.fromData(c,[...a.shape],"float64")}function Yr(r,t,e=!1){return tr(r,.5,t,e)}function Jr(r,t,e,n=!1){return tr(r,t/100,e,n)}function tr(r,t,e,n=!1){if(t<0||t>1)throw new Error("Quantile must be between 0 and 1");let o=r.shape,a=o.length,s=r.data;if(e===void 0){let y=[];for(let b=0;b<r.size;b++)y.push(Number(s[b]));y.sort((b,S)=>b-S);let m=y.length,d=t*(m-1),g=Math.floor(d),A=Math.ceil(d);if(g===A)return y[g];let h=d-g;return y[g]*(1-h)+y[A]*h}let i=e;if(i<0&&(i=a+i),i<0||i>=a)throw new Error(`axis ${e} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((y,m)=>m!==i);if(u.length===0)return tr(r,t);let c=u.reduce((y,m)=>y*m,1),l=o[i],p=new Float64Array(c);for(let y=0;y<c;y++){let m=[];for(let b=0;b<l;b++){let S=v(y,i,b,o),N=T(S,o);m.push(Number(s[N]))}m.sort((b,S)=>b-S);let d=m.length,g=t*(d-1),A=Math.floor(g),h=Math.ceil(g);if(A===h)p[y]=m[A];else{let b=g-A;p[y]=m[A]*(1-b)+m[h]*b}}let f=D.fromData(p,u,"float64");if(n){let y=[...o];return y[i]=1,D.fromData(p,y,"float64")}return f}function br(r,t,e,n=!1){let o=r.shape,a=o.length,s=r.data;if(e===void 0)return gr(r,t,n);if(t===void 0){let m=0,d=0,g=e.data;for(let A=0;A<r.size;A++){let h=Number(g[A%e.size]);m+=Number(s[A])*h,d+=h}return d===0?NaN:m/d}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 u=Array.from(o).filter((m,d)=>d!==i);if(u.length===0)return br(r,void 0,e);let c=u.reduce((m,d)=>m*d,1),l=o[i],p=e.data,f=new Float64Array(c);for(let m=0;m<c;m++){let d=0,g=0;for(let A=0;A<l;A++){let h=v(m,i,A,o),b=T(h,o),S=Number(p[A%e.size]);d+=Number(s[b])*S,g+=S}f[m]=g===0?NaN:d/g}let y=D.fromData(f,u,"float64");if(n){let m=[...o];return m[i]=1,D.fromData(f,m,"float64")}return y}function Sr(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=0;for(let y=0;y<r.size;y++){let m=Number(a[y]);isNaN(m)||(f+=m)}return f}let s=t;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let i=Array.from(n).filter((f,y)=>y!==s);if(i.length===0)return Sr(r);let u=i.reduce((f,y)=>f*y,1),c=n[s],l=new Float64Array(u);for(let f=0;f<u;f++){let y=0;for(let m=0;m<c;m++){let d=v(f,s,m,n),g=T(d,n),A=Number(a[g]);isNaN(A)||(y+=A)}l[f]=y}let p=D.fromData(l,i,"float64");if(e){let f=[...n];return f[s]=1,D.fromData(l,f,"float64")}return p}function Dr(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=1;for(let y=0;y<r.size;y++){let m=Number(a[y]);isNaN(m)||(f*=m)}return f}let s=t;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let i=Array.from(n).filter((f,y)=>y!==s);if(i.length===0)return Dr(r);let u=i.reduce((f,y)=>f*y,1),c=n[s],l=new Float64Array(u);for(let f=0;f<u;f++){let y=1;for(let m=0;m<c;m++){let d=v(f,s,m,n),g=T(d,n),A=Number(a[g]);isNaN(A)||(y*=A)}l[f]=y}let p=D.fromData(l,i,"float64");if(e){let f=[...n];return f[s]=1,D.fromData(l,f,"float64")}return p}function xr(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=0,y=0;for(let m=0;m<r.size;m++){let d=Number(a[m]);isNaN(d)||(f+=d,y++)}return y===0?NaN:f/y}let s=t;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let i=Array.from(n).filter((f,y)=>y!==s);if(i.length===0)return xr(r);let u=i.reduce((f,y)=>f*y,1),c=n[s],l=new Float64Array(u);for(let f=0;f<u;f++){let y=0,m=0;for(let d=0;d<c;d++){let g=v(f,s,d,n),A=T(g,n),h=Number(a[A]);isNaN(h)||(y+=h,m++)}l[f]=m===0?NaN:y/m}let p=D.fromData(l,i,"float64");if(e){let f=[...n];return f[s]=1,D.fromData(l,f,"float64")}return p}function ar(r,t,e=0,n=!1){let o=r.shape,a=o.length,s=r.data;if(t===void 0){let y=0,m=0;for(let A=0;A<r.size;A++){let h=Number(s[A]);isNaN(h)||(y+=h,m++)}if(m-e<=0)return NaN;let d=y/m,g=0;for(let A=0;A<r.size;A++){let h=Number(s[A]);isNaN(h)||(g+=(h-d)**2)}return g/(m-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 u=Array.from(o).filter((y,m)=>m!==i);if(u.length===0)return ar(r,void 0,e);let c=u.reduce((y,m)=>y*m,1),l=o[i],p=new Float64Array(c);for(let y=0;y<c;y++){let m=0,d=0;for(let h=0;h<l;h++){let b=v(y,i,h,o),S=T(b,o),N=Number(s[S]);isNaN(N)||(m+=N,d++)}if(d-e<=0){p[y]=NaN;continue}let g=m/d,A=0;for(let h=0;h<l;h++){let b=v(y,i,h,o),S=T(b,o),N=Number(s[S]);isNaN(N)||(A+=(N-g)**2)}p[y]=A/(d-e)}let f=D.fromData(p,u,"float64");if(n){let y=[...o];return y[i]=1,D.fromData(p,y,"float64")}return f}function Xr(r,t,e=0,n=!1){let o=ar(r,t,e,n);if(typeof o=="number")return Math.sqrt(o);let a=o,s=new Float64Array(a.size);for(let i=0;i<a.size;i++)s[i]=Math.sqrt(Number(a.data[i]));return D.fromData(s,[...a.shape],"float64")}function Nr(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=1/0;for(let y=0;y<r.size;y++){let m=Number(a[y]);!isNaN(m)&&m<f&&(f=m)}return f===1/0?NaN:f}let s=t;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let i=Array.from(n).filter((f,y)=>y!==s);if(i.length===0)return Nr(r);let u=i.reduce((f,y)=>f*y,1),c=n[s],l=new Float64Array(u);for(let f=0;f<u;f++){let y=1/0;for(let m=0;m<c;m++){let d=v(f,s,m,n),g=T(d,n),A=Number(a[g]);!isNaN(A)&&A<y&&(y=A)}l[f]=y===1/0?NaN:y}let p=D.fromData(l,i,"float64");if(e){let f=[...n];return f[s]=1,D.fromData(l,f,"float64")}return p}function wr(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=-1/0;for(let y=0;y<r.size;y++){let m=Number(a[y]);!isNaN(m)&&m>f&&(f=m)}return f===-1/0?NaN:f}let s=t;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let i=Array.from(n).filter((f,y)=>y!==s);if(i.length===0)return wr(r);let u=i.reduce((f,y)=>f*y,1),c=n[s],l=new Float64Array(u);for(let f=0;f<u;f++){let y=-1/0;for(let m=0;m<c;m++){let d=v(f,s,m,n),g=T(d,n),A=Number(a[g]);!isNaN(A)&&A>y&&(y=A)}l[f]=y===-1/0?NaN:y}let p=D.fromData(l,i,"float64");if(e){let f=[...n];return f[s]=1,D.fromData(l,f,"float64")}return p}function Ir(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let l=1/0,p=-1;for(let f=0;f<r.size;f++){let y=Number(o[f]);!isNaN(y)&&y<l&&(l=y,p=f)}return p}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let s=Array.from(e).filter((l,p)=>p!==a);if(s.length===0)return Ir(r);let i=s.reduce((l,p)=>l*p,1),u=e[a],c=new Int32Array(i);for(let l=0;l<i;l++){let p=1/0,f=0;for(let y=0;y<u;y++){let m=v(l,a,y,e),d=T(m,e),g=Number(o[d]);!isNaN(g)&&g<p&&(p=g,f=y)}c[l]=f}return D.fromData(c,s,"int32")}function zr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let l=-1/0,p=-1;for(let f=0;f<r.size;f++){let y=Number(o[f]);!isNaN(y)&&y>l&&(l=y,p=f)}return p}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let s=Array.from(e).filter((l,p)=>p!==a);if(s.length===0)return zr(r);let i=s.reduce((l,p)=>l*p,1),u=e[a],c=new Int32Array(i);for(let l=0;l<i;l++){let p=-1/0,f=0;for(let y=0;y<u;y++){let m=v(l,a,y,e),d=T(m,e),g=Number(o[d]);!isNaN(g)&&g>p&&(p=g,f=y)}c[l]=f}return D.fromData(c,s,"int32")}function Qr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.size,y=new Float64Array(f),m=0;for(let d=0;d<f;d++){let g=Number(o[d]);isNaN(g)||(m+=g),y[d]=m}return D.fromData(y,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let s=new Float64Array(r.size),i=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=r.size,p=u[a];for(let f=0;f<l;f++){let y=Number(o[f]);Math.floor(f/p)%i===0?s[f]=isNaN(y)?0:y:s[f]=s[f-p]+(isNaN(y)?0:y)}return D.fromData(s,[...e],"float64")}function Kr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.size,y=new Float64Array(f),m=1;for(let d=0;d<f;d++){let g=Number(o[d]);isNaN(g)||(m*=g),y[d]=m}return D.fromData(y,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let s=new Float64Array(r.size),i=e[a],u=[],c=1;for(let f=n-1;f>=0;f--)u.unshift(c),c*=e[f];let l=r.size,p=u[a];for(let f=0;f<l;f++){let y=Number(o[f]);Math.floor(f/p)%i===0?s[f]=isNaN(y)?1:y:s[f]=s[f-p]*(isNaN(y)?1:y)}return D.fromData(s,[...e],"float64")}function _r(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=[];for(let d=0;d<r.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 y=f.length,m=Math.floor(y/2);return y%2===0?(f[m-1]+f[m])/2:f[m]}let s=t;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let i=Array.from(n).filter((f,y)=>y!==s);if(i.length===0)return _r(r);let u=i.reduce((f,y)=>f*y,1),c=n[s],l=new Float64Array(u);for(let f=0;f<u;f++){let y=[];for(let g=0;g<c;g++){let A=v(f,s,g,n),h=T(A,n),b=Number(a[h]);isNaN(b)||y.push(b)}if(y.length===0){l[f]=NaN;continue}y.sort((g,A)=>g-A);let m=y.length,d=Math.floor(m/2);m%2===0?l[f]=(y[d-1]+y[d])/2:l[f]=y[d]}let p=D.fromData(l,i,"float64");if(e){let f=[...n];return f[s]=1,D.fromData(l,f,"float64")}return p}function j(r,t){let e=r.size,n=r.dtype,o=t.indexOf(-1),a;if(o!==-1){let c=t.reduce((p,f,y)=>y===o?p:p*f,1),l=e/c;if(!Number.isInteger(l))throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(t)}`);a=t.map((p,f)=>f===o?l:p)}else a=t;if(a.reduce((c,l)=>c*l,1)!==e)throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(a)}`);if(r.isCContiguous){let c=r.data;return D.fromData(c,a,n,R(a),0)}let u=r.copy().data;return D.fromData(u,a,n,R(a),0)}function Br(r){let t=r.size,e=r.dtype,n=O(e);if(!n)throw new Error(`Cannot flatten array with dtype ${e}`);if(r.isCContiguous){let i=r.data.slice(r.offset,r.offset+t);return D.fromData(i,[t],e,[1],0)}let o=new n(t),a=x(e);for(let s=0;s<t;s++){let i=r.iget(s);o[s]=i}return D.fromData(o,[t],e,[1],0)}function ir(r){let t=r.size,e=r.dtype;if(r.isCContiguous){let n=r.data;return D.fromData(n,[t],e,[1],0)}return Br(r)}function ur(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,s=r.dtype,i;if(t===void 0)i=Array.from({length:n},(p,f)=>n-1-f);else{if(t.length!==n)throw new Error(`axes must have length ${n}, got ${t.length}`);let p=new Set;for(let f of t){let y=f<0?n+f:f;if(y<0||y>=n)throw new Error(`axis ${f} is out of bounds for array of dimension ${n}`);if(p.has(y))throw new Error("repeated axis in transpose");p.add(y)}i=t.map(f=>f<0?n+f:f)}let u=i.map(p=>e[p]),c=Array.from(o),l=i.map(p=>c[p]);return D.fromData(a,u,s,l,r.offset)}function ee(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,s=r.dtype;if(t===void 0){let i=[],u=[];for(let c=0;c<n;c++)e[c]!==1&&(i.push(e[c]),u.push(o[c]));return i.length===0&&(i.push(1),u.push(1)),D.fromData(a,i,s,u,r.offset)}else{let i=t<0?n+t:t;if(i<0||i>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);if(e[i]!==1)throw new Error(`cannot select an axis which has size not equal to one (axis ${t} has size ${e[i]})`);let u=[],c=[];for(let l=0;l<n;l++)l!==i&&(u.push(e[l]),c.push(o[l]));return D.fromData(a,u,s,c,r.offset)}}function sr(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,s=r.dtype,i=t;if(i<0&&(i=n+t+1),i<0||i>n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n+1}`);let u=[...Array.from(e)];u.splice(i,0,1);let c=[...Array.from(o)],l=i<n?o[i]*(e[i]||1):1;return c.splice(i,0,l),D.fromData(a,u,s,c,r.offset)}function ne(r,t,e){let n=r.shape,o=n.length,a=r.strides,s=r.data,i=r.dtype,u=t<0?o+t:t,c=e<0?o+e:e;if(u<0||u>=o)throw new Error(`axis1 ${t} 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(s,Array.from(n),i,Array.from(a),r.offset);let l=Array.from(n),p=Array.from(a);return[l[u],l[c]]=[l[c],l[u]],[p[u],p[c]]=[p[c],p[u]],D.fromData(s,l,i,p,r.offset)}function rt(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 s=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}),i=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(s).size!==s.length)throw new Error("repeated axis in source");if(new Set(i).size!==i.length)throw new Error("repeated axis in destination");let u=[];for(let c=0;c<n;c++)s.includes(c)||u.push(c);for(let c=0;c<s.length;c++){let l=i[c];u.splice(l,0,s[c])}return ur(r,u)}function Q(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 y=1;y<r.length;y++){let m=r[y];if(m.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&&m.shape[d]!==e.shape[d])throw new Error("all the input array dimensions except for the concatenation axis must match exactly")}let s=Array.from(e.shape),i=e.shape[a];for(let y=1;y<r.length;y++)i+=r[y].shape[a];s[a]=i;let u=s.reduce((y,m)=>y*m,1),c=O(o);if(!c)throw new Error(`Cannot concatenate arrays with dtype ${o}`);let l=new c(u),p=R(s),f=0;for(let y of r){let m=y.shape[a];xo(y,l,s,p,a,f,o),f+=m}return D.fromData(l,s,o)}function xo(r,t,e,n,o,a,s){let i=r.shape,u=i.length,c=r.size,l=s==="int64"||s==="uint64";if(o===0&&r.isCContiguous&&u>0){let y=a*n[0],m=r.data,d=r.offset,g=d+c;t.set(m.subarray(d,g),y);return}if(o===1&&u===2&&r.isCContiguous){let y=i[0],m=i[1],d=e[1],g=r.data,A=r.offset;for(let h=0;h<y;h++){let b=A+h*m,S=h*d+a;t.set(g.subarray(b,b+m),S)}return}let p=new Array(u).fill(0),f=a*n[o];for(let y=0;y<c;y++){let m=r.iget(y),d=f;for(let g=0;g<u;g++)d+=p[g]*n[g];t[d]=m;for(let g=u-1;g>=0&&(p[g]++,!(p[g]<i[g]));g--)p[g]=0}}function oe(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 i=1;i<r.length;i++){let u=r[i];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 s=r.map(i=>sr(i,a));return Q(s,a)}function ae(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 Q(t,0)}function tt(r){if(r.length===0)throw new Error("need at least one array to stack");return r.every(e=>e.ndim===1)?Q(r,0):Q(r,1)}function se(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?j(sr(j(e,[1,e.shape[0]]),2),[1,e.shape[0],1]):e.ndim===2?sr(e,2):e);return Q(t,2)}function ie(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 s=n[a],i;if(typeof t=="number"){if(s%t!==0)throw new Error("array split does not result in an equal division");let u=s/t;i=[];for(let c=1;c<t;c++)i.push(c*u)}else i=t;return ue(r,i,a)}function cr(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 s=n[a],i;if(typeof t=="number"){let u=t,c=Math.floor(s/u),l=s%u;i=[];let p=0;for(let f=0;f<u-1;f++)p+=c+(f<l?1:0),i.push(p)}else i=t;return ue(r,i,a)}function ue(r,t,e){let n=r.shape,o=n[e],a=[0,...t,o],s=[];for(let i=0;i<a.length-1;i++){let u=a[i],c=a[i+1];if(u>c)throw new Error("split indices must be in ascending order");let l=Array.from(n);l[e]=c-u;let p=r.offset+u*r.strides[e];s.push(D.fromData(r.data,l,r.dtype,Array.from(r.strides),p))}return s}function ce(r,t){if(r.ndim<2)throw new Error("vsplit only works on arrays of 2 or more dimensions");return cr(r,t,0)}function le(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 cr(r,t,e)}function fe(r,t){let e=r.shape,n=e.length,o=r.dtype,a=Array.isArray(t)?t:[t],s=Math.max(n,a.length),i=new Array(s).fill(1),u=new Array(s).fill(1);for(let h=0;h<n;h++)i[s-n+h]=e[h];for(let h=0;h<a.length;h++)u[s-a.length+h]=a[h];let c=i.map((h,b)=>h*u[b]),l=c.reduce((h,b)=>h*b,1),p=O(o);if(!p)throw new Error(`Cannot tile array with dtype ${o}`);let f=new p(l),y=R(c),m=r;n<s&&(m=j(r,i));let d=o==="int64"||o==="uint64",g=m.strides,A=new Array(s).fill(0);for(let h=0;h<l;h++){let b=m.offset;for(let w=0;w<s;w++){let I=A[w]%i[w];b+=I*g[w]}let S=m.data[b],N=0;for(let w=0;w<s;w++)N+=A[w]*y[w];f[N]=S;for(let w=s-1;w>=0&&(A[w]++,!(A[w]<c[w]));w--)A[w]=0}return D.fromData(f,c,o)}function pe(r,t,e){let n=r.shape,o=n.length,a=r.dtype,s=r.size;if(e===void 0){let h=s,b=Array.isArray(t)?t:new Array(h).fill(t);if(b.length!==h)throw new Error(`operands could not be broadcast together with shape (${h},) (${b.length},)`);let S=b.reduce((z,B)=>z+B,0),N=O(a);if(!N)throw new Error(`Cannot repeat array with dtype ${a}`);let w=new N(S),I=0;for(let z=0;z<h;z++){let B=r.iget(z),E=b[z];for(let U=0;U<E;U++)w[I++]=B}return D.fromData(w,[S],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 u=n[i],c=Array.isArray(t)?t:new Array(u).fill(t);if(c.length!==u)throw new Error(`operands could not be broadcast together with shape (${u},) (${c.length},)`);let l=Array.from(n);l[i]=c.reduce((h,b)=>h+b,0);let p=l.reduce((h,b)=>h*b,1),f=O(a);if(!f)throw new Error(`Cannot repeat array with dtype ${a}`);let y=new f(p),m=R(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<s;h++){let b=r.iget(h),S=d[i],N=c[S],w=0;for(let B=0;B<o;B++)B!==i&&(w+=d[B]*m[B]);let I=m[i],z=A[S];for(let B=0;B<N;B++){let E=w+(z+B)*I;y[E]=b}for(let B=o-1;B>=0&&(d[B]++,!(d[B]<n[B]));B--)d[B]=0}return D.fromData(y,l,a)}function ye(r,t){let e=r.shape,n=e.length,o=r.dtype,a=r.size,s;if(t===void 0)s=new Set(Array.from({length:n},(f,y)=>y));else if(typeof t=="number"){let f=t<0?n+t:t;if(f<0||f>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);s=new Set([f])}else s=new Set(t.map(f=>{let y=f<0?n+f:f;if(y<0||y>=n)throw new Error(`axis ${f} is out of bounds for array of dimension ${n}`);return y}));let i=O(o);if(!i)throw new Error(`Cannot flip array with dtype ${o}`);let u=new i(a),c=x(o);if(n===1&&r.isCContiguous){let f=r.data,y=r.offset;for(let m=0;m<a;m++)u[m]=f[y+a-1-m];return D.fromData(u,[...e],o)}if(n===2&&r.isCContiguous){let f=e[0],y=e[1],m=r.data,d=r.offset;if(s.size===2){for(let g=0;g<a;g++)u[g]=m[d+a-1-g];return D.fromData(u,[...e],o)}if(s.size===1){if(s.has(0)){for(let g=0;g<f;g++){let A=d+(f-1-g)*y,h=g*y;for(let b=0;b<y;b++)u[h+b]=m[A+b]}return D.fromData(u,[...e],o)}else if(s.has(1)){for(let g=0;g<f;g++){let A=d+g*y,h=g*y;for(let b=0;b<y;b++)u[h+b]=m[A+y-1-b]}return D.fromData(u,[...e],o)}}}let l=new Array(n),p=new Array(n).fill(0);for(let f=0;f<a;f++){for(let d=0;d<n;d++)l[d]=s.has(d)?e[d]-1-p[d]:p[d];let y=r.offset;for(let d=0;d<n;d++)y+=l[d]*r.strides[d];let m=r.data[y];u[f]=m;for(let d=n-1;d>=0&&(p[d]++,!(p[d]<e[d]));d--)p[d]=0}return D.fromData(u,[...e],o)}function me(r,t=1,e=[0,1]){let n=r.shape,o=n.length,a=r.dtype;if(o<2)throw new Error("Input must be at least 2-D");let s=e[0]<0?o+e[0]:e[0],i=e[1]<0?o+e[1]:e[1];if(s<0||s>=o||i<0||i>=o)throw new Error(`Axes are out of bounds for array of dimension ${o}`);if(s===i)throw new Error("Axes must be different");if(t=(t%4+4)%4,t===0)return r.copy();let u=O(a);if(!u)throw new Error(`Cannot rotate array with dtype ${a}`);let c=[...n];(t===1||t===3)&&([c[s],c[i]]=[c[i],c[s]]);let l=c.reduce((g,A)=>g*A,1),p=new u(l),f=R(c),y=x(a),m=new Array(o).fill(0),d=new Array(o);for(let g=0;g<r.size;g++){for(let N=0;N<o;N++)d[N]=m[N];let A,h;t===1?(A=n[i]-1-m[i],h=m[s]):t===2?(A=n[s]-1-m[s],h=n[i]-1-m[i],d[s]=A,d[i]=h):(A=m[i],h=n[s]-1-m[s]),t!==2&&(d[s]=A,d[i]=h);let b=0;for(let N=0;N<o;N++)b+=d[N]*f[N];let S=r.iget(g);p[b]=S;for(let N=o-1;N>=0&&(m[N]++,!(m[N]<n[N]));N--)m[N]=0}return D.fromData(p,c,a)}function de(r,t,e){let n=r.shape,o=n.length,a=r.dtype,s=r.size;if(e===void 0){let m=Array.isArray(t)?t.reduce((b,S)=>b+S,0):t,d=Br(r),g=O(a);if(!g)throw new Error(`Cannot roll array with dtype ${a}`);let A=new g(s),h=x(a);for(let b=0;b<s;b++){let S=((b-m)%s+s)%s,N=d.iget(S);A[b]=N}return D.fromData(A,[...n],a)}let i=Array.isArray(t)?t:[t],u=Array.isArray(e)?e:[e];if(i.length!==u.length)throw new Error("shift and axis must have the same length");let c=u.map(m=>{let d=m<0?o+m:m;if(d<0||d>=o)throw new Error(`axis ${m} is out of bounds for array of dimension ${o}`);return d}),l=O(a);if(!l)throw new Error(`Cannot roll array with dtype ${a}`);let p=new l(s),f=x(a),y=new Array(o).fill(0);for(let m=0;m<s;m++){let d=[...y];for(let h=0;h<c.length;h++){let b=c[h],S=n[b],N=i[h];d[b]=((d[b]-N)%S+S)%S}let g=r.offset;for(let h=0;h<o;h++)g+=d[h]*r.strides[h];let A=r.data[g];p[m]=A;for(let h=o-1;h>=0&&(y[h]++,!(y[h]<n[h]));h--)y[h]=0}return D.fromData(p,[...n],a)}function ge(r,t,e=0){let n=r.ndim,o=t<0?n+t:t;if(o<0||o>=n)throw new Error(`axis ${t} 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(r.data,Array.from(r.shape),r.dtype,Array.from(r.strides),r.offset):rt(r,o,a)}function he(r,t){if(r.ndim<3)throw new Error("dsplit only works on arrays of 3 or more dimensions");return cr(r,t,2)}function Ae(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,[e.shape[0],1]):e);return tt(t)}function be(r,t){let e=r.dtype,n=t.reduce((u,c)=>u*c,1),o=r.size,a=O(e);if(!a)throw new Error(`Cannot resize array with dtype ${e}`);let s=new a(n),i=x(e);for(let u=0;u<n;u++){let c=u%o,l=r.iget(c);s[u]=l}return D.fromData(s,t,e)}function Se(r){return r.map(t=>t.ndim===0?j(t,[1]):t)}function De(r){return r.map(t=>t.ndim===0?j(t,[1,1]):t.ndim===1?j(t,[1,t.shape[0]]):t)}function xe(r){return r.map(t=>t.ndim===0?j(t,[1,1,1]):t.ndim===1?j(t,[1,t.shape[0],1]):t.ndim===2?j(t,[t.shape[0],t.shape[1],1]):t)}function No(r,t,e,n,o,a,s,i,u,c,l,p,f,y){if(p===0)for(let A=0;A<n*o;A++)f[A]=0;else if(p!==1)for(let A=0;A<n*o;A++)f[A]=(f[A]??0)*p;let m=r==="row-major",d=t==="transpose",g=e==="transpose";if(m&&!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+=(i[A*u+S]??0)*(c[S*l+h]??0);f[A*y+h]=(f[A*y+h]??0)+s*b}else if(m&&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+=(i[S*u+A]??0)*(c[S*l+h]??0);f[A*y+h]=(f[A*y+h]??0)+s*b}else if(m&&!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+=(i[A*u+S]??0)*(c[h*l+S]??0);f[A*y+h]=(f[A*y+h]??0)+s*b}else if(m&&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+=(i[S*u+A]??0)*(c[h*l+S]??0);f[A*y+h]=(f[A*y+h]??0)+s*b}else if(!m&&!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+=(i[S*u+A]??0)*(c[h*l+S]??0);f[h*y+A]=(f[h*y+A]??0)+s*b}else if(!m&&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+=(i[A*u+S]??0)*(c[h*l+S]??0);f[h*y+A]=(f[h*y+A]??0)+s*b}else if(!m&&!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+=(i[S*u+A]??0)*(c[S*l+h]??0);f[h*y+A]=(f[h*y+A]??0)+s*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+=(i[A*u+S]??0)*(c[S*l+h]??0);f[h*y+A]=(f[h*y+A]??0)+s*b}}function nt(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 s=C(r.dtype,t.dtype),i=D.zeros([...t.shape],s);for(let u=0;u<t.size;u++){let c=t.data[u+t.offset];typeof o=="bigint"&&typeof c=="bigint"?i.data[u]=o*c:i.data[u]=Number(o)*Number(c)}return i}else{let s=C(r.dtype,t.dtype),i=D.zeros([...r.shape],s);for(let u=0;u<r.size;u++){let c=r.data[u+r.offset];typeof c=="bigint"&&typeof a=="bigint"?i.data[u]=c*a:i.data[u]=Number(c)*Number(a)}return i}}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 s=0;s<o;s++){let i=r.get(s),u=t.get(s);typeof i=="bigint"&&typeof u=="bigint"?a=Number(a)+Number(i*u):a+=Number(i)*Number(u)}return a}if(e===2&&n===2)return K(r,t);if(e===2&&n===1){let[o,a]=r.shape,s=t.shape[0];if(a!==s)throw new Error(`dot: incompatible shapes (${o},${a}) and (${s},)`);let i=C(r.dtype,t.dtype),u=D.zeros([o],i);for(let c=0;c<o;c++){let l=0;for(let p=0;p<a;p++){let f=r.get(c,p),y=t.get(p);typeof f=="bigint"&&typeof y=="bigint"?l=Number(l)+Number(f*y):l+=Number(f)*Number(y)}u.set([c],l)}return u}if(e===1&&n===2){let o=r.shape[0],[a,s]=t.shape;if(o!==a)throw new Error(`dot: incompatible shapes (${o},) and (${a},${s})`);let i=C(r.dtype,t.dtype),u=D.zeros([s],i);for(let c=0;c<s;c++){let l=0;for(let p=0;p<o;p++){let f=r.get(p),y=t.get(p,c);typeof f=="bigint"&&typeof y=="bigint"?l=Number(l)+Number(f*y):l+=Number(f)*Number(y)}u.set([c],l)}return u}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 s=[...r.shape.slice(0,-1)],i=C(r.dtype,t.dtype),u=D.zeros(s,i),c=s.reduce((l,p)=>l*p,1);for(let l=0;l<c;l++){let p=0,f=l,y=[];for(let m=s.length-1;m>=0;m--)y[m]=f%s[m],f=Math.floor(f/s[m]);for(let m=0;m<o;m++){let d=[...y,m],g=r.get(...d),A=t.get(m);typeof g=="bigint"&&typeof A=="bigint"?p=Number(p)+Number(g*A):p+=Number(g)*Number(A)}u.set(y,p)}return u}if(e===1&&n>2){let o=r.shape[0],a=1,s=t.shape[a];if(o!==s)throw new Error(`dot: incompatible shapes (${o},) and ${JSON.stringify(t.shape)}`);let i=[...t.shape.slice(0,a),...t.shape.slice(a+1)],u=C(r.dtype,t.dtype),c=D.zeros(i,u),l=i.reduce((p,f)=>p*f,1);for(let p=0;p<l;p++){let f=p,y=[];for(let A=i.length-1;A>=0;A--)y[A]=f%i[A],f=Math.floor(f/i[A]);let m=y.slice(0,a),d=y.slice(a),g=0;for(let A=0;A<o;A++){let h=r.get(A),b=[...m,A,...d],S=t.get(...b);typeof h=="bigint"&&typeof S=="bigint"?g=Number(g)+Number(h*S):g+=Number(h)*Number(S)}c.set(y,g)}return c}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 s=[...r.shape.slice(0,-1),...t.shape.slice(0,-2),t.shape[n-1]],i=C(r.dtype,t.dtype),u=D.zeros(s,i),c=r.shape.slice(0,-1).reduce((y,m)=>y*m,1),l=t.shape.slice(0,-2).reduce((y,m)=>y*m,1),p=t.shape[n-1],f=o;for(let y=0;y<c;y++)for(let m=0;m<l;m++)for(let d=0;d<p;d++){let g=0;for(let h=0;h<f;h++){let b=y*f+h,S=r.data[b+r.offset],N=m*f*p+h*p+d,w=t.data[N+t.offset];typeof S=="bigint"&&typeof w=="bigint"?g=Number(g)+Number(S*w):g+=Number(S)*Number(w)}let A=y*l*p+m*p+d;u.data[A]=g}return u}throw new Error(`dot: unexpected combination of dimensions ${e}D \xB7 ${n}D`)}function K(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 s=C(r.dtype,t.dtype),i=s.startsWith("int")||s.startsWith("uint")||s==="bool"?"float64":s;if(i!=="float64")throw new Error(`matmul currently only supports float64, got ${i}`);let u=r.dtype==="float64"?r.data:Float64Array.from(Array.from(r.data).map(Number)),c=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number));r.offset>0&&(u=u.subarray(r.offset)),t.offset>0&&(c=c.subarray(t.offset));let[l=0,p=0]=r.strides,[f=0,y=0]=t.strides,m=p>l,d=y>f,g=m?"transpose":"no-transpose",A=d?"transpose":"no-transpose",h,b;m?h=p:h=l,d?b=y:b=f;let S=D.zeros([e,a],"float64");return No("row-major",g,A,e,a,n,1,u,h,c,b,0,S.data,a),S}function Ie(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 s=r.get(a,a);typeof s=="bigint"?o=(typeof o=="bigint"?o:BigInt(o))+s:o=(typeof o=="bigint"?Number(o):o)+s}return o}function we(r,t){return ur(r,t)}function ze(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 nt(r,t);let s=[...r.shape.slice(0,-1),...t.shape.slice(0,-1)],i=C(r.dtype,t.dtype),u=D.zeros(s,i),c=e===1?1:r.shape.slice(0,-1).reduce((f,y)=>f*y,1),l=n===1?1:t.shape.slice(0,-1).reduce((f,y)=>f*y,1),p=o;for(let f=0;f<c;f++)for(let y=0;y<l;y++){let m=0;for(let g=0;g<p;g++){let A=e===1?g:f*p+g,h=n===1?g:y*p+g,b=r.data[A+r.offset],S=t.data[h+t.offset];typeof b=="bigint"&&typeof S=="bigint"?m=Number(m)+Number(b*S):m+=Number(b)*Number(S)}if(s.length===0)return m;let d=c===1?y:f*l+y;u.data[d]=m}return u}function ot(r,t){let e=r.ndim===1?r:ir(r),n=t.ndim===1?t:ir(t),o=e.size,a=n.size,s=C(r.dtype,t.dtype),i=D.zeros([o,a],s);for(let u=0;u<o;u++)for(let c=0;c<a;c++){let l=e.get(u),p=n.get(c),f;typeof l=="bigint"&&typeof p=="bigint"?f=l*p:f=Number(l)*Number(p),i.set([u,c],f)}return i}function _e(r,t,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>r.ndim||f>t.ndim)throw new Error("tensordot: axes exceeds array dimensions");n=Array.from({length:f},(y,m)=>r.ndim-f+m),o=Array.from({length:f},(y,m)=>m)}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 y=n[f],m=o[f];if(y<0||y>=r.ndim||m<0||m>=t.ndim)throw new Error("tensordot: axis out of bounds");if(r.shape[y]!==t.shape[m])throw new Error(`tensordot: shape mismatch on axes ${y} and ${m}: ${r.shape[y]} != ${t.shape[m]}`)}let a=[],s=[];for(let f=0;f<r.ndim;f++)n.includes(f)||a.push(f);for(let f=0;f<t.ndim;f++)o.includes(f)||s.push(f);let i=[...a.map(f=>r.shape[f]),...s.map(f=>t.shape[f])];if(i.length===0){let f=0,y=n.map(m=>r.shape[m]).reduce((m,d)=>m*d,1);for(let m=0;m<y;m++){let d=m,g=new Array(n.length);for(let N=n.length-1;N>=0;N--){let w=n[N];g[N]=d%r.shape[w],d=Math.floor(d/r.shape[w])}let A=new Array(r.ndim),h=new Array(t.ndim);for(let N=0;N<n.length;N++)A[n[N]]=g[N];for(let N=0;N<o.length;N++)h[o[N]]=g[N];let b=r.get(...A),S=t.get(...h);typeof b=="bigint"&&typeof S=="bigint"?f=Number(f)+Number(b*S):f+=Number(b)*Number(S)}return f}let u=C(r.dtype,t.dtype),c=D.zeros(i,u),l=i.reduce((f,y)=>f*y,1),p=n.map(f=>r.shape[f]).reduce((f,y)=>f*y,1);for(let f=0;f<l;f++){let y=f,m=[];for(let h=i.length-1;h>=0;h--)m[h]=y%i[h],y=Math.floor(y/i[h]);let d=m.slice(0,a.length),g=m.slice(a.length),A=0;for(let h=0;h<p;h++){y=h;let b=[];for(let z=n.length-1;z>=0;z--){let B=n[z];b[z]=y%r.shape[B],y=Math.floor(y/r.shape[B])}let S=new Array(r.ndim),N=new Array(t.ndim);for(let z=0;z<a.length;z++)S[a[z]]=d[z];for(let z=0;z<s.length;z++)N[s[z]]=g[z];for(let z=0;z<n.length;z++)S[n[z]]=b[z],N[o[z]]=b[z];let w=r.get(...S),I=t.get(...N);typeof w=="bigint"&&typeof I=="bigint"?A=Number(A)+Number(w*I):A+=Number(w)*Number(I)}c.set(m,A)}return c}function Be(r,t=0,e=0,n=1){let o=r.shape,a=o.length;if(a<2)throw new Error("diagonal requires an array of at least two dimensions");let s=e<0?a+e:e,i=n<0?a+n:n;if(s<0||s>=a||i<0||i>=a)throw new Error("axis out of bounds");if(s===i)throw new Error("axis1 and axis2 cannot be the same");let u=o[s],c=o[i],l;t>=0?l=Math.max(0,Math.min(u,c-t)):l=Math.max(0,Math.min(u+t,c));let p=[];for(let d=0;d<a;d++)d!==s&&d!==i&&p.push(o[d]);p.push(l);let f=D.zeros(p,r.dtype),y=o.filter((d,g)=>g!==s&&g!==i),m=y.reduce((d,g)=>d*g,1);for(let d=0;d<m;d++){let g=d,A=[];for(let h=y.length-1;h>=0;h--)A.unshift(g%y[h]),g=Math.floor(g/y[h]);for(let h=0;h<l;h++){let b=new Array(a),S=0;for(let I=0;I<a;I++)I===s?b[I]=t>=0?h:h-t:I===i?b[I]=t>=0?h+t:h:b[I]=A[S++];let N=[...A,h],w=r.get(...b);f.set(N,w)}}return f}function Ee(r,...t){let e=r.indexOf("->"),n,o;e===-1?(n=r,o=wo(n)):(n=r.slice(0,e),o=r.slice(e+2));let a=n.split(",").map(d=>d.trim());if(a.length!==t.length)throw new Error(`einsum: expected ${a.length} operands, got ${t.length}`);let s=new Map;for(let d=0;d<t.length;d++){let g=a[d],A=t[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(s.has(b)){if(s.get(b)!==S)throw new Error(`einsum: size mismatch for index '${b}': ${s.get(b)} vs ${S}`)}else s.set(b,S)}}for(let d of o)if(!s.has(d))throw new Error(`einsum: output subscript contains unknown index '${d}'`);let i=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)i.has(d)||c.push(d);if(t.length===2&&a.length===2){let[d,g]=a,[A,h]=t;if(d.length===2&&g.length===2&&o.length===2&&A.ndim===2&&h.ndim===2){let[b,S]=[d[0],d[1]],[N,w]=[g[0],g[1]],[I,z]=[o[0],o[1]];if(b===I&&w===z&&S===N&&c.length===1&&c[0]===S||b===I&&w===z&&S===N&&c.length===1&&c[0]===S)return K(A,h);if(S===I&&w===z&&b===N&&c.length===1&&c[0]===b){let B=we(A);return K(B,h)}if(b===I&&N===z&&S===w&&c.length===1&&c[0]===S){let B=we(h);return K(A,B)}}if(d.length===1&&g.length===1&&d===g&&o.length===0&&A.ndim===1&&h.ndim===1)return et(t,a,c,s);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 ot(A,h)}if(t.length===1&&a[0].length===2&&o.length===0){let d=a[0];if(d[0]===d[1]&&t[0].ndim===2)return et(t,a,c,s)}let l=Array.from(o).map(d=>s.get(d));if(l.length===0)return et(t,a,c,s);let p=t[0].dtype;for(let d=1;d<t.length;d++)p=C(p,t[d].dtype);let f=D.zeros(l,p),y=l.reduce((d,g)=>d*g,1),m=1;for(let d of c)m*=s.get(d);for(let d=0;d<y;d++){let g=Io(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<m;b++){let S=b;for(let w=c.length-1;w>=0;w--){let I=c[w],z=s.get(I);A.set(I,S%z),S=Math.floor(S/z)}let N=1;for(let w=0;w<t.length;w++){let I=t[w],z=a[w],B=[];for(let U of z)B.push(A.get(U));let E=I.get(...B);N*=Number(E)}h+=N}f.set(g,h)}return f}function wo(r){let t=new Map,e=r.split(",");for(let o of e)for(let a of o.trim())t.set(a,(t.get(a)||0)+1);let n=[];for(let[o,a]of t)a===1&&n.push(o);return n.sort().join("")}function et(r,t,e,n){let o=1;for(let s of e)o*=n.get(s);let a=0;for(let s=0;s<o;s++){let i=new Map,u=s;for(let l=e.length-1;l>=0;l--){let p=e[l],f=n.get(p);i.set(p,u%f),u=Math.floor(u/f)}let c=1;for(let l=0;l<r.length;l++){let p=r[l],f=t[l],y=[];for(let d of f)y.push(i.get(d));let m=p.get(...y);c*=Number(m)}a+=c}return a}function Io(r,t){let e=new Array(t.length),n=r;for(let o=t.length-1;o>=0;o--)e[o]=n%t[o],n=Math.floor(n/t[o]);return e}function Ue(r,t){let e=r.shape,n=t.shape,o=e.length,a=n.length,s=C(r.dtype,t.dtype),i=Math.max(o,a),u=new Array(i),c=new Array(i).fill(1),l=new Array(i).fill(1);for(let m=0;m<o;m++)c[i-o+m]=e[m];for(let m=0;m<a;m++)l[i-a+m]=n[m];for(let m=0;m<i;m++)u[m]=c[m]*l[m];let p=D.zeros(u,s),f=e.reduce((m,d)=>m*d,1),y=n.reduce((m,d)=>m*d,1);for(let m=0;m<f;m++){let d=m,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(i).fill(0);for(let b=0;b<o;b++)A[i-o+b]=g[b];let h=r.get(...g);for(let b=0;b<y;b++){let S=b,N=new Array(a);for(let E=a-1;E>=0;E--)N[E]=S%n[E],S=Math.floor(S/n[E]);let w=new Array(i).fill(0);for(let E=0;E<a;E++)w[i-a+E]=N[E];let I=t.get(...N),z=new Array(i);for(let E=0;E<i;E++)z[E]=A[E]*l[E]+w[E];let B=typeof h=="bigint"||typeof I=="bigint"?BigInt(Number(h))*BigInt(Number(I)):Number(h)*Number(I);p.set(z,B)}}return p}function Te(r){return V(r,Math.sqrt,!1)}function ve(r,t){return typeof t=="number"?_o(r,t):P(r,t,Math.pow,"power")}function _o(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,u=e!=="float32"&&e!=="float64"&&(t<0||!Number.isInteger(t))?"float64":e,c=D.zeros(n,u),l=c.data;if(x(e))if(x(u)&&Number.isInteger(t)&&t>=0){let p=o,f=l;for(let y=0;y<a;y++)f[y]=p[y]**BigInt(t)}else for(let p=0;p<a;p++)l[p]=Math.pow(Number(o[p]),t);else for(let p=0;p<a;p++)l[p]=Math.pow(Number(o[p]),t);return c}function Oe(r){return V(r,Math.sin,!1)}function $e(r){return V(r,Math.cos,!1)}function Me(r){return V(r,Math.tan,!1)}function Ce(r){return V(r,Math.asin,!1)}function ke(r){return V(r,Math.acos,!1)}function Ve(r){return V(r,Math.atan,!1)}function Fe(r,t){return typeof t=="number"?Uo(r,t):Eo(r,t)}function Eo(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,s=o==="float32"&&a==="float32"?"float32":"float64",i=D.zeros(e,s),u=i.data;for(let c=0;c<n;c++){let l=(x(o),Number(r.data[c])),p=(x(a),Number(t.data[c]));u[c]=Math.atan2(l,p)}return i}function Uo(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=e==="float32"?"float32":"float64",i=D.zeros(n,s),u=i.data;if(x(e))for(let c=0;c<a;c++)u[c]=Math.atan2(Number(o[c]),t);else for(let c=0;c<a;c++)u[c]=Math.atan2(Number(o[c]),t);return i}function Pe(r,t){return typeof t=="number"?vo(r,t):To(r,t)}function To(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,s=o==="float32"&&a==="float32"?"float32":"float64",i=D.zeros(e,s),u=i.data;for(let c=0;c<n;c++){let l=(x(o),Number(r.data[c])),p=(x(a),Number(t.data[c]));u[c]=Math.hypot(l,p)}return i}function vo(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=e==="float32"?"float32":"float64",i=D.zeros(n,s),u=i.data;if(x(e))for(let c=0;c<a;c++)u[c]=Math.hypot(Number(o[c]),t);else for(let c=0;c<a;c++)u[c]=Math.hypot(Number(o[c]),t);return i}function Re(r){let t=180/Math.PI;return V(r,e=>e*t,!1)}function Le(r){let t=Math.PI/180;return V(r,e=>e*t,!1)}function qe(r){return V(r,Math.sinh,!1)}function je(r){return V(r,Math.cosh,!1)}function Ze(r){return V(r,Math.tanh,!1)}function We(r){return V(r,Math.asinh,!1)}function Ge(r){return V(r,Math.acosh,!1)}function He(r){return V(r,Math.atanh,!1)}function at(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=X([Array.from(e),t]);if(a===null)throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);for(let s=0;s<o;s++)if(a[s]!==t[s])throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);return G(r,t)}function Ye(r){if(r.length===0)return[];if(r.length===1)return[r[0]];let t=r.map(n=>Array.from(n.shape)),e=X(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 Je(r,t,e){let n=r.shape,o=n.length,a=r.dtype;if(e===void 0){let m=r.size;for(let h of t){let b=h<0?m+h:h;if(b<0||b>=m)throw new Error(`index ${h} is out of bounds for axis 0 with size ${m}`)}let d=t.length,g=O(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=t[h];b<0&&(b=m+b);let S=r.iget(b);x(a),A[h]=S}return D.fromData(A,[d],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 i=n[s];for(let m of t){let d=m<0?i+m:m;if(d<0||d>=i)throw new Error(`index ${m} is out of bounds for axis ${s} with size ${i}`)}let u=Array.from(n);u[s]=t.length;let c=u.reduce((m,d)=>m*d,1),l=O(a);if(!l)throw new Error(`Cannot take from array with dtype ${a}`);let p=new l(c),f=R(u),y=new Array(o).fill(0);for(let m=0;m<c;m++){let d=[...y],g=y[s],A=t[g];A<0&&(A=i+A),d[s]=A;let h=r.get(...d),b=0;for(let S=0;S<o;S++)b+=y[S]*f[S];x(a),p[b]=h;for(let S=o-1;S>=0&&(y[S]++,!(y[S]<u[S]));S--)y[S]=0}return D.fromData(p,u,a)}function Xe(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 s=0;s<e.size;s++)a.push(e.iget(s));if(a.length===1)a=new Array(t.length).fill(a[0]);else if(a.length!==t.length){let s=[...a];a=[];for(let i=0;i<t.length;i++)a.push(s[i%s.length])}}for(let s=0;s<t.length;s++){let i=t[s];if(i<0&&(i=n+i),i<0||i>=n)throw new Error(`index ${t[s]} is out of bounds for axis 0 with size ${n}`);let u=a[s];x(o)?typeof u!="bigint"&&(u=BigInt(Math.round(Number(u)))):typeof u=="bigint"&&(u=Number(u)),r.iset(i,u)}}function Qe(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(f=>Array.from(f.shape));a.unshift(Array.from(e));let s=X(a);if(s===null)throw new Error("operands could not be broadcast together");let i=G(r,s),u=t.map(f=>G(f,s)),c=s.reduce((f,y)=>f*y,1),l=O(o);if(!l)throw new Error(`Cannot choose with dtype ${o}`);let p=new l(c);for(let f=0;f<c;f++){let y=Number(i.iget(f));if(y<0||y>=n)throw new Error(`index ${y} is out of bounds for axis 0 with size ${n}`);let m=u[y].iget(f);x(o),p[f]=m}return D.fromData(p,s,o)}function Ke(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),s=t.iget(o);if(e){let i=typeof a=="number"&&Number.isNaN(a),u=typeof s=="number"&&Number.isNaN(s);if(i&&u)continue}if(a!==s)return!1}return!0}function rn(r,t,e){let n=r.shape,o=n.length,a=r.dtype,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 i=t.shape;if(i.length!==o)throw new Error(`indices and arr must have the same number of dimensions, got ${i.length} vs ${o}`);for(let d=0;d<o;d++)if(d!==s&&i[d]!==n[d]&&i[d]!==1&&n[d]!==1)throw new Error(`index ${i[d]} is out of bounds for size ${n[d]} in dimension ${d}`);let u=Array.from(i),c=u.reduce((d,g)=>d*g,1),l=O(a);if(!l)throw new Error(`Cannot take_along_axis with dtype ${a}`);let p=new l(c),f=R(n),y=R(i),m=n[s];for(let d=0;d<c;d++){let g=new Array(o),A=d;for(let I=o-1;I>=0;I--)g[I]=A%u[I],A=Math.floor(A/u[I]);let h=0;for(let I=0;I<o;I++){let z=i[I]===1?0:g[I];h+=z*y[I]}let b=Number(t.iget(h));if(b<0&&(b=m+b),b<0||b>=m)throw new Error(`index ${b} is out of bounds for axis ${s} with size ${m}`);let S=[...g];S[s]=b;let N=0;for(let I=0;I<o;I++){let z=n[I]===1?0:S[I];N+=z*f[I]}let w=r.iget(N);x(a),p[d]=w}return D.fromData(p,u,a)}function tn(r,t,e,n){let o=r.shape,a=o.length,s=r.dtype,i=n<0?a+n:n;if(i<0||i>=a)throw new Error(`axis ${n} is out of bounds for array of dimension ${a}`);let u=t.shape,c=e.shape;if(u.length!==a||c.length!==a)throw new Error("indices, arr, and values must have same ndim");let l=o[i],p=R(o),f=R(u),y=R(c),m=u.reduce((d,g)=>d*g,1);for(let d=0;d<m;d++){let g=new Array(a),A=d;for(let z=a-1;z>=0;z--)g[z]=A%u[z],A=Math.floor(A/u[z]);let h=0;for(let z=0;z<a;z++)h+=g[z]*f[z];let b=Number(t.iget(h));if(b<0&&(b=l+b),b<0||b>=l)throw new Error(`index ${b} is out of bounds for axis ${i} with size ${l}`);let S=0;for(let z=0;z<a;z++){let B=c[z]===1?0:g[z];S+=B*y[z]}let N=e.iget(S),w=[...g];w[i]=b;let I=0;for(let z=0;z<a;z++)I+=w[z]*p[z];x(s)?typeof N!="bigint"&&(N=BigInt(Math.round(Number(N)))):typeof N=="bigint"&&(N=Number(N)),r.iset(I,N)}}function en(r,t,e){let n=r.size,o=r.dtype,a;if(typeof e=="number"||typeof e=="bigint")a=[e];else{a=[];for(let i=0;i<e.size;i++)a.push(e.iget(i))}let s=0;for(let i=0;i<n;i++)if(t.iget(i)){let c=a[s%a.length];x(o)?typeof c!="bigint"&&(c=BigInt(Math.round(Number(c)))):typeof c=="bigint"&&(c=Number(c)),r.iset(i,c),s++}}function nn(r,t,e){let n=t.shape,o=n.length,a=t.dtype,s=t.data,i=x(a);if(e===void 0){let h=0,b=Math.min(r.size,t.size);for(let I=0;I<b;I++)r.iget(I)&&h++;let S=O(a);if(!S)throw new Error(`Cannot compress with dtype ${a}`);let N=new S(h),w=0;for(let I=0;I<b;I++)r.iget(I)&&(N[w]=s[I],w++);return D.fromData(N,[h],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 c=n[u],l=Math.min(r.size,c),p=[];for(let h=0;h<l;h++)r.iget(h)&&p.push(h);let f=p.length,y=[...n];y[u]=f;let m=y.reduce((h,b)=>h*b,1),d=O(a);if(!d)throw new Error(`Cannot compress with dtype ${a}`);let g=new d(m),A=R(n);if(u===0){let h=A[0],b=n.slice(1).reduce((N,w)=>N*w,1),S=0;for(let N=0;N<f;N++){let I=p[N]*h;if(i){let z=s,B=g;for(let E=0;E<b;E++)B[S++]=z[I+E]}else{let z=s,B=g;for(let E=0;E<b;E++)B[S++]=z[I+E]}}}else{let h=n.slice(0,u).reduce((N,w)=>N*w,1),b=n.slice(u+1).reduce((N,w)=>N*w,1),S=0;for(let N=0;N<h;N++)for(let w=0;w<f;w++){let I=p[w],z=0,B=N;for(let E=u-1;E>=0;E--){let U=B%n[E];B=Math.floor(B/n[E]),z+=U*A[E]}if(z+=I*A[u],i){let E=s,U=g;for(let $=0;$<b;$++)U[S++]=E[z+$]}else{let E=s,U=g;for(let $=0;$<b;$++)U[S++]=E[z+$]}}}return D.fromData(g,y,a)}function on(r,t,e=0){if(r.length!==t.length)throw new Error("condlist and choicelist must have same length");if(r.length===0)throw new Error("condlist and choicelist cannot be empty");let n=[...r.map(f=>Array.from(f.shape)),...t.map(f=>Array.from(f.shape))],o=X(n);if(o===null)throw new Error("condlist and choicelist arrays could not be broadcast together");let a=t[0].dtype,s=o.reduce((f,y)=>f*y,1),i=O(a);if(!i)throw new Error(`Cannot select with dtype ${a}`);let u=e;x(a)?u=typeof e=="bigint"?e:BigInt(e):u=typeof e=="bigint"?Number(e):e;let c=new i(s);for(let f=0;f<s;f++)x(a),c[f]=u;let l=r.map(f=>G(f,o)),p=t.map(f=>G(f,o));for(let f=0;f<s;f++)for(let y=0;y<r.length;y++)if(l[y].iget(f)){let m=p[y].iget(f);x(a),c[f]=m;break}return D.fromData(c,o,a)}function an(r,t,e){let n=r.size,o=r.dtype,a=[];for(let i=0;i<e.size;i++)a.push(e.iget(i));if(a.length===0)return;let s=0;for(let i=0;i<n;i++)if(t.iget(i)){let c=a[s%a.length];x(o)?typeof c!="bigint"&&(c=BigInt(Math.round(Number(c)))):typeof c=="bigint"&&(c=Number(c)),r.iset(i,c),s++}}function st(r,t=2){if(t<1)throw new Error("ndim must be at least 1");let e=new Int32Array(r);for(let o=0;o<r;o++)e[o]=o;let n=[];for(let o=0;o<t;o++)n.push(D.fromData(new Int32Array(e),[r],"int32"));return n}function sn(r){let t=r.shape,e=t.length;if(e<2)throw new Error("array must be at least 2-D");let n=t[0];for(let o=1;o<e;o++)if(t[o]!==n)throw new Error("All dimensions of input must be equal");return st(n,e)}function it(r,t=0,e){let n=e??r,o=[],a=[];for(let s=0;s<r;s++)for(let i=0;i<=Math.min(s+t,n-1);i++)i>=0&&(o.push(s),a.push(i));return[D.fromData(new Int32Array(o),[o.length],"int32"),D.fromData(new Int32Array(a),[a.length],"int32")]}function un(r,t=0){let e=r.shape;if(e.length!==2)throw new Error("array must be 2-D");return it(e[0],t,e[1])}function ut(r,t=0,e){let n=e??r,o=[],a=[];for(let s=0;s<r;s++)for(let i=Math.max(s+t,0);i<n;i++)o.push(s),a.push(i);return[D.fromData(new Int32Array(o),[o.length],"int32"),D.fromData(new Int32Array(a),[a.length],"int32")]}function cn(r,t=0){let e=r.shape;if(e.length!==2)throw new Error("array must be 2-D");return ut(e[0],t,e[1])}function ln(r,t,e=0){let n=t(r,e),o=n.shape;if(o.length!==2||o[0]!==r||o[1]!==r)throw new Error("mask_func must return n x n array");let a=[],s=[];for(let i=0;i<r;i++)for(let u=0;u<r;u++)n.get(i,u)&&(a.push(i),s.push(u));return[D.fromData(new Int32Array(a),[a.length],"int32"),D.fromData(new Int32Array(s),[s.length],"int32")]}function fn(r,t="int32"){let e=r.length,n=[e,...r],o=n.reduce((u,c)=>u*c,1),a=O(t);if(!a)throw new Error(`Cannot create indices with dtype ${t}`);let s=new a(o),i=r.reduce((u,c)=>u*c,1);for(let u=0;u<e;u++){let c=u*i;for(let l=0;l<i;l++){let p=new Array(e),f=l;for(let m=e-1;m>=0;m--)p[m]=f%r[m],f=Math.floor(f/r[m]);let y=p[u];t==="int64"?s[c+l]=BigInt(y):s[c+l]=y}}return D.fromData(s,n,t)}function pn(...r){let t=r.length,e=[];for(let n=0;n<t;n++){let o=r[n],a=o.size,s=o.dtype,i=new Array(t).fill(1);i[n]=a;let u=O(s);if(!u)throw new Error(`Cannot create ix_ with dtype ${s}`);let c=new u(a);for(let l=0;l<a;l++){let p=o.iget(l);x(s),c[l]=p}e.push(D.fromData(c,i,s))}return e}function yn(r,t,e="raise"){if(r.length!==t.length)throw new Error("multi_index length must equal dims length");if(r.length===0)throw new Error("multi_index cannot be empty");let n=r[0].size,o=t.length,a=new Int32Array(n),s=new Array(o),i=1;for(let u=o-1;u>=0;u--)s[u]=i,i*=t[u];for(let u=0;u<n;u++){let c=0;for(let l=0;l<o;l++){let p=Number(r[l].iget(u)),f=t[l];if(e==="wrap")p=(p%f+f)%f;else if(e==="clip")p=Math.max(0,Math.min(p,f-1));else if(p<0||p>=f)throw new Error(`index ${p} is out of bounds for axis ${l} with size ${f}`);c+=p*s[l]}a[u]=c}return D.fromData(a,[n],"int32")}function mn(r,t,e="C"){let n=t.length,o,a;if(typeof r=="number")o=[r],a=[];else{o=[];for(let l=0;l<r.size;l++)o.push(Number(r.iget(l)));a=Array.from(r.shape)}let s=o.length,i=t.reduce((l,p)=>l*p,1),u=new Array(n);if(e==="C"){let l=1;for(let p=n-1;p>=0;p--)u[p]=l,l*=t[p]}else{let l=1;for(let p=0;p<n;p++)u[p]=l,l*=t[p]}let c=[];for(let l=0;l<n;l++){let p=new Int32Array(s);c.push(D.fromData(p,a.length?a:[1],"int32"))}for(let l=0;l<s;l++){let p=o[l];if(p<0||p>=i)throw new Error(`index ${p} is out of bounds for array with size ${i}`);if(e==="C")for(let f=0;f<n;f++){let y=Math.floor(p/u[f]);p=p%u[f],c[f].data[l]=y%t[f]}else for(let f=n-1;f>=0;f--){let y=Math.floor(p/u[f]);p=p%u[f],c[f].data[l]=y%t[f]}}return typeof r=="number"?c.map(l=>{let p=l.iget(0);return D.fromData(new Int32Array([Number(p)]),[],"int32")}):c}function W(r,t){if(!Ut(r)&&r!=="bool")throw new TypeError(`ufunc '${t}' not supported for the input types, and the inputs could not be safely coerced to any supported types`)}function lr(r,t){return r.isCContiguous&&t.isCContiguous&&r.shape.length===t.shape.length&&r.shape.every((e,n)=>e===t.shape[n])}function hn(r,t){return W(r.dtype,"bitwise_and"),typeof t=="number"?ko(r,t):(W(t.dtype,"bitwise_and"),lr(r,t)?Co(r,t):P(r,t,(e,n)=>e&n,"bitwise_and"))}function Co(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;if(!x(r.dtype)||!x(t.dtype))for(let l=0;l<o;l++){let p=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof s[l]=="bigint"?s[l]:BigInt(Math.round(Number(s[l])));u[l]=p&f}else{let l=a,p=s;for(let f=0;f<o;f++)u[f]=l[f]&p[f]}}else for(let u=0;u<o;u++)i[u]=a[u]&s[u];return n}function ko(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]&l}else for(let u=0;u<a;u++)i[u]=o[u]&t;return s}function An(r,t){return W(r.dtype,"bitwise_or"),typeof t=="number"?Fo(r,t):(W(t.dtype,"bitwise_or"),lr(r,t)?Vo(r,t):P(r,t,(e,n)=>e|n,"bitwise_or"))}function Vo(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;if(!x(r.dtype)||!x(t.dtype))for(let l=0;l<o;l++){let p=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof s[l]=="bigint"?s[l]:BigInt(Math.round(Number(s[l])));u[l]=p|f}else{let l=a,p=s;for(let f=0;f<o;f++)u[f]=l[f]|p[f]}}else for(let u=0;u<o;u++)i[u]=a[u]|s[u];return n}function Fo(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]|l}else for(let u=0;u<a;u++)i[u]=o[u]|t;return s}function bn(r,t){return W(r.dtype,"bitwise_xor"),typeof t=="number"?Ro(r,t):(W(t.dtype,"bitwise_xor"),lr(r,t)?Po(r,t):P(r,t,(e,n)=>e^n,"bitwise_xor"))}function Po(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;if(!x(r.dtype)||!x(t.dtype))for(let l=0;l<o;l++){let p=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),f=typeof s[l]=="bigint"?s[l]:BigInt(Math.round(Number(s[l])));u[l]=p^f}else{let l=a,p=s;for(let f=0;f<o;f++)u[f]=l[f]^p[f]}}else for(let u=0;u<o;u++)i[u]=a[u]^s[u];return n}function Ro(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]^l}else for(let u=0;u<a;u++)i[u]=o[u]^t;return s}function ct(r){W(r.dtype,"bitwise_not");let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),s=a.data;if(x(t)){let i=n,u=s;for(let c=0;c<o;c++)u[c]=~i[c]}else for(let i=0;i<o;i++)s[i]=~n[i];return a}function Sn(r){return ct(r)}function Dn(r,t){if(W(r.dtype,"left_shift"),typeof t=="number")return dn(r,t);if(W(t.dtype,"left_shift"),t.size===1||t.ndim===1&&t.shape[0]===1){let e=x(t.dtype)?Number(t.data[0]):t.data[0];return dn(r,e)}return lr(r,t)?Lo(r,t):P(r,t,(e,n)=>e<<n,"left_shift")}function Lo(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),p=typeof s[c]=="bigint"?s[c]:BigInt(Math.round(Number(s[c])));u[c]=l<<p}}else for(let u=0;u<o;u++)i[u]=a[u]<<s[u];return n}function dn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]<<l}else for(let u=0;u<a;u++)i[u]=o[u]<<t;return s}function xn(r,t){if(W(r.dtype,"right_shift"),typeof t=="number")return gn(r,t);if(W(t.dtype,"right_shift"),t.size===1||t.ndim===1&&t.shape[0]===1){let e=x(t.dtype)?Number(t.data[0]):t.data[0];return gn(r,e)}return lr(r,t)?qo(r,t):P(r,t,(e,n)=>e>>n,"right_shift")}function qo(r,t){let e=C(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,s=t.data,i=n.data;if(x(e)){let u=i;for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),p=typeof s[c]=="bigint"?s[c]:BigInt(Math.round(Number(s[c])));u[c]=l>>p}}else for(let u=0;u<o;u++)i[u]=a[u]>>s[u];return n}function gn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=D.zeros(n,e),i=s.data;if(x(e)){let u=o,c=i,l=BigInt(Math.round(t));for(let p=0;p<a;p++)c[p]=u[p]>>l}else for(let u=0;u<a;u++)i[u]=o[u]>>t;return s}function Nn(r,t=-1,e="big"){let n=Array.from(r.shape),o=n.length;if(t<0&&(t=o+t),t<0||t>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let a=n[t],s=Math.ceil(a/8),i=[...n];i[t]=s;let u=D.zeros(i,"uint8"),c=u.data;if(o===1){for(let g=0;g<s;g++){let A=0;for(let h=0;h<8;h++){let b=g*8+h;if(b<a){let S=Number(r.data[b])!==0?1:0;e==="big"?A|=S<<7-h:A|=S<<h}}c[g]=A}return u}let l=n.slice(0,t),p=n.slice(t+1),f=l.reduce((g,A)=>g*A,1),y=p.reduce((g,A)=>g*A,1),m=Er(n),d=Er(i);for(let g=0;g<f;g++)for(let A=0;A<y;A++)for(let h=0;h<s;h++){let b=0;for(let I=0;I<8;I++){let z=h*8+I;if(z<a){let B=0,E=g;for(let M=0;M<t;M++){let F=M<t-1?l.slice(M+1).reduce(($r,Mr)=>$r*Mr,1):1,Z=Math.floor(E/F);E%=F,B+=Z*m[M]}B+=z*m[t];let U=A;for(let M=t+1;M<o;M++){let F=M<o-1?p.slice(M-t).reduce(($r,Mr)=>$r*Mr,1):1,Z=Math.floor(U/F);U%=F,B+=Z*m[M]}let $=Number(r.data[B])!==0?1:0;e==="big"?b|=$<<7-I:b|=$<<I}}let S=0,N=g;for(let I=0;I<t;I++){let z=I<t-1?l.slice(I+1).reduce((E,U)=>E*U,1):1,B=Math.floor(N/z);N%=z,S+=B*d[I]}S+=h*d[t];let w=A;for(let I=t+1;I<o;I++){let z=I<o-1?p.slice(I-t).reduce((E,U)=>E*U,1):1,B=Math.floor(w/z);w%=z,S+=B*d[I]}c[S]=b}return u}function wn(r,t=-1,e=-1,n="big"){if(r.dtype!=="uint8")throw new TypeError("Expected an input array of unsigned byte data type");let o=Array.from(r.shape),a=o.length;if(t<0&&(t=a+t),t<0||t>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let s=o[t],i=s*8;e>=0&&(i=e);let u=[...o];u[t]=i;let c=D.zeros(u,"uint8"),l=c.data;if(a===1){for(let A=0;A<s;A++){let h=Number(r.data[A]);for(let b=0;b<8;b++){let S=A*8+b;if(S>=i)break;n==="big"?l[S]=h>>7-b&1:l[S]=h>>b&1}}return c}let p=o.slice(0,t),f=o.slice(t+1),y=p.reduce((A,h)=>A*h,1),m=f.reduce((A,h)=>A*h,1),d=Er(o),g=Er(u);for(let A=0;A<y;A++)for(let h=0;h<m;h++)for(let b=0;b<s;b++){let S=0,N=A;for(let z=0;z<t;z++){let B=z<t-1?p.slice(z+1).reduce((U,$)=>U*$,1):1,E=Math.floor(N/B);N%=B,S+=E*d[z]}S+=b*d[t];let w=h;for(let z=t+1;z<a;z++){let B=z<a-1?f.slice(z-t).reduce((U,$)=>U*$,1):1,E=Math.floor(w/B);w%=B,S+=E*d[z]}let I=Number(r.data[S]);for(let z=0;z<8;z++){let B=b*8+z;if(B>=i)break;let E=0;N=A;for(let U=0;U<t;U++){let $=U<t-1?p.slice(U+1).reduce((F,Z)=>F*Z,1):1,M=Math.floor(N/$);N%=$,E+=M*g[U]}E+=B*g[t],w=h;for(let U=t+1;U<a;U++){let $=U<a-1?f.slice(U-t).reduce((F,Z)=>F*Z,1):1,M=Math.floor(w/$);w%=$,E+=M*g[U]}n==="big"?l[E]=I>>7-z&1:l[E]=I>>z&1}}return c}function Er(r){let t=r.length,e=new Array(t),n=1;for(let o=t-1;o>=0;o--)e[o]=n,n*=r[o];return e}function lt(r,t=-1){let e=r.shape,n=e.length,o=r.dtype,a=r.data;if(n===0)return r.copy();let s=t;if(s<0&&(s=n+s),s<0||s>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=r.copy(),u=i.data,c=e[s],l=Array.from(e).filter((f,y)=>y!==s),p=l.length===0?1:l.reduce((f,y)=>f*y,1);if(x(o)){let f=a,y=u;for(let m=0;m<p;m++){let d=[];for(let g=0;g<c;g++){let A=v(m,s,g,e),h=T(A,e);d.push({value:f[h],idx:g})}d.sort((g,A)=>g.value<A.value?-1:g.value>A.value?1:0);for(let g=0;g<c;g++){let A=v(m,s,g,e),h=T(A,e);y[h]=d[g].value}}}else for(let f=0;f<p;f++){let y=[];for(let m=0;m<c;m++){let d=v(f,s,m,e),g=T(d,e);y.push(Number(a[g]))}y.sort((m,d)=>isNaN(m)&&isNaN(d)?0:isNaN(m)?1:isNaN(d)?-1:m-d);for(let m=0;m<c;m++){let d=v(f,s,m,e),g=T(d,e);u[g]=y[m]}}return i}function ft(r,t=-1){let e=r.shape,n=e.length,o=r.dtype,a=r.data;if(n===0)return D.zeros([0],"int32");let s=t;if(s<0&&(s=n+s),s<0||s>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=D.zeros(Array.from(e),"int32"),u=i.data,c=e[s],l=Array.from(e).filter((f,y)=>y!==s),p=l.length===0?1:l.reduce((f,y)=>f*y,1);if(x(o)){let f=a;for(let y=0;y<p;y++){let m=[];for(let d=0;d<c;d++){let g=v(y,s,d,e),A=T(g,e);m.push({value:f[A],idx:d})}m.sort((d,g)=>d.value<g.value?-1:d.value>g.value?1:0);for(let d=0;d<c;d++){let g=v(y,s,d,e),A=T(g,e);u[A]=m[d].idx}}}else for(let f=0;f<p;f++){let y=[];for(let m=0;m<c;m++){let d=v(f,s,m,e),g=T(d,e);y.push({value:Number(a[g]),idx:m})}y.sort((m,d)=>isNaN(m.value)&&isNaN(d.value)?0:isNaN(m.value)?1:isNaN(d.value)?-1:m.value-d.value);for(let m=0;m<c;m++){let d=v(f,s,m,e),g=T(d,e);u[g]=y[m].idx}}return i}function In(r){if(r.length===0)return D.zeros([0],"int32");let e=r[0].size;for(let s of r){if(s.ndim!==1)throw new Error("keys must be 1D arrays");if(s.size!==e)throw new Error("all keys must have the same length")}let n=[];for(let s=0;s<e;s++)n.push(s);n.sort((s,i)=>{for(let u=r.length-1;u>=0;u--){let l=r[u].data,p=Number(l[s]),f=Number(l[i]);if(!(isNaN(p)&&isNaN(f))){if(isNaN(p))return 1;if(isNaN(f)||p<f)return-1;if(p>f)return 1}}return 0});let o=D.zeros([e],"int32"),a=o.data;for(let s=0;s<e;s++)a[s]=n[s];return o}function Zo(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e],s=r[o],i=r[n],u;a<=s&&s<=i||i<=s&&s<=a?u=o:s<=a&&a<=i||i<=a&&a<=s?u=e:u=n;let c=r[u];[r[u],r[n]]=[r[n],r[u]];let l=e;for(let p=e;p<n;p++){let f=r[p],y=isNaN(f),m=isNaN(c);!y&&(m||f<=c)&&([r[l],r[p]]=[r[p],r[l]],l++)}if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function Wo(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e],s=r[o],i=r[n],u;a<=s&&s<=i||i<=s&&s<=a?u=o:s<=a&&a<=i||i<=a&&a<=s?u=e:u=n;let c=r[u];[r[u],r[n]]=[r[n],r[u]];let l=e;for(let p=e;p<n;p++)r[p]<=c&&([r[l],r[p]]=[r[p],r[l]],l++);if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function Go(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e].value,s=r[o].value,i=r[n].value,u;a<=s&&s<=i||i<=s&&s<=a?u=o:s<=a&&a<=i||i<=a&&a<=s?u=e:u=n;let c=r[u].value;[r[u],r[n]]=[r[n],r[u]];let l=e;for(let p=e;p<n;p++){let f=r[p].value,y=isNaN(f),m=isNaN(c);!y&&(m||f<=c)&&([r[l],r[p]]=[r[p],r[l]],l++)}if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function Ho(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e].value,s=r[o].value,i=r[n].value,u;a<=s&&s<=i||i<=s&&s<=a?u=o:s<=a&&a<=i||i<=a&&a<=s?u=e:u=n;let c=r[u].value;[r[u],r[n]]=[r[n],r[u]];let l=e;for(let p=e;p<n;p++)r[p].value<=c&&([r[l],r[p]]=[r[p],r[l]],l++);if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function pt(r,t,e=-1){let n=r.shape,o=n.length,a=r.dtype;if(o===0)return r.copy();let s=e;if(s<0&&(s=o+s),s<0||s>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let i=n[s],u=t;if(u<0&&(u=i+u),u<0||u>=i)throw new Error(`kth(=${t}) out of bounds (${i})`);let c=r.copy(),l=c.data,p=Array.from(n).filter((y,m)=>m!==s),f=p.length===0?1:p.reduce((y,m)=>y*m,1);if(x(a)){let y=l;for(let m=0;m<f;m++){let d=[];for(let g=0;g<i;g++){let A=v(m,s,g,n),h=T(A,n);d.push(y[h])}Wo(d,u);for(let g=0;g<i;g++){let A=v(m,s,g,n),h=T(A,n);y[h]=d[g]}}}else for(let y=0;y<f;y++){let m=[];for(let d=0;d<i;d++){let g=v(y,s,d,n),A=T(g,n);m.push(Number(l[A]))}Zo(m,u);for(let d=0;d<i;d++){let g=v(y,s,d,n),A=T(g,n);l[A]=m[d]}}return c}function yt(r,t,e=-1){let n=r.shape,o=n.length,a=r.dtype,s=r.data;if(o===0)return D.zeros([0],"int32");let i=e;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let u=n[i],c=t;if(c<0&&(c=u+c),c<0||c>=u)throw new Error(`kth(=${t}) out of bounds (${u})`);let l=D.zeros(Array.from(n),"int32"),p=l.data,f=Array.from(n).filter((m,d)=>d!==i),y=f.length===0?1:f.reduce((m,d)=>m*d,1);if(x(a)){let m=s;for(let d=0;d<y;d++){let g=[];for(let A=0;A<u;A++){let h=v(d,i,A,n),b=T(h,n);g.push({value:m[b],idx:A})}Ho(g,c);for(let A=0;A<u;A++){let h=v(d,i,A,n),b=T(h,n);p[b]=g[A].idx}}}else for(let m=0;m<y;m++){let d=[];for(let g=0;g<u;g++){let A=v(m,i,g,n),h=T(A,n);d.push({value:Number(s[h]),idx:g})}Go(d,c);for(let g=0;g<u;g++){let A=v(m,i,g,n),h=T(A,n);p[h]=d[g].idx}}return l}function zn(r){let t=r.dtype,e=r.size,n=r.data,o=[];for(let i=0;i<e;i++)o.push(Number(n[i]));o.sort((i,u)=>isNaN(i)&&isNaN(u)?0:isNaN(i)?1:isNaN(u)?-1:i-u);let a=D.zeros([e],t),s=a.data;for(let i=0;i<e;i++)s[i]=o[i];return a}function Ur(r){let t=r.shape,e=t.length,n=r.data,o=r.size,a=[];for(let l=0;l<e;l++)a.push([]);let s=[],i=1;for(let l=e-1;l>=0;l--)s.unshift(i),i*=t[l];for(let l=0;l<o;l++)if(n[l]){let p=l;for(let f=0;f<e;f++){let y=Math.floor(p/s[f]);p=p%s[f],a[f].push(y)}}let u=a[0]?.length??0,c=[];for(let l=0;l<e;l++){let p=D.zeros([u],"int32"),f=p.data;for(let y=0;y<u;y++)f[y]=a[l][y];c.push(p)}return c}function _n(r){let t=r.data,e=r.size,n=[];for(let s=0;s<e;s++)t[s]&&n.push(s);let o=D.zeros([n.length],"int32"),a=o.data;for(let s=0;s<n.length;s++)a[s]=n[s];return o}function Bn(r,t,e){if(t===void 0&&e===void 0)return Ur(r);if(t===void 0||e===void 0)throw new Error("either both or neither of x and y should be given");let n=r.shape,o=t.shape,a=e.shape,s=Math.max(n.length,o.length,a.length),i=B=>{let E=Array(s).fill(1);for(let U=0;U<B.length;U++)E[s-B.length+U]=B[U];return E},u=i(n),c=i(o),l=i(a),p=[];for(let B=0;B<s;B++){let E=[u[B],c[B],l[B]],U=Math.max(...E);for(let $ of E)if($!==1&&$!==U)throw new Error("operands could not be broadcast together");p.push(U)}let f=t.dtype,y=D.zeros(p,f),m=y.data,d=r.data,g=t.data,A=e.data,h=(B,E)=>{let U=[],$=1;for(let M=B.length-1;M>=0;M--)U.unshift($),$*=B[M];for(;U.length<E.length;)U.unshift(0);for(let M=0;M<E.length;M++)E[M]===1&&p[M]!==1&&(U[M]=0);return U},b=h(n,u),S=h(o,c),N=h(a,l),w=[],I=1;for(let B=p.length-1;B>=0;B--)w.unshift(I),I*=p[B];let z=p.reduce((B,E)=>B*E,1);for(let B=0;B<z;B++){let E=B,U=0,$=0,M=0;for(let F=0;F<s;F++){let Z=Math.floor(E/w[F]);E=E%w[F],U+=Z*b[F],$+=Z*S[F],M+=Z*N[F]}d[U]?m[B]=g[$]:m[B]=A[M]}return y}function mt(r,t,e="left"){if(r.ndim!==1)throw new Error("storage must be 1D");let n=r.data,o=r.size,a=t.data,s=t.size,i=D.zeros([s],"int32"),u=i.data;for(let c=0;c<s;c++){let l=Number(a[c]),p=0,f=o;if(e==="left")for(;p<f;){let y=Math.floor((p+f)/2);Number(n[y])<l?p=y+1:f=y}else for(;p<f;){let y=Math.floor((p+f)/2);Number(n[y])<=l?p=y+1:f=y}u[c]=p}return i}function En(r,t){let e=r.data,n=t.data,o=t.dtype,a=Math.min(r.size,t.size),s=0;for(let l=0;l<a;l++)e[l]&&s++;let i=D.zeros([s],o),u=i.data,c=0;if(x(o)){let l=n,p=u;for(let f=0;f<a;f++)e[f]&&(p[c++]=l[f])}else for(let l=0;l<a;l++)e[l]&&(u[c++]=n[l]);return i}function dt(r,t){let e=r.shape,n=e.length,o=r.data,a=r.size;if(t===void 0){let f=0;for(let y=0;y<a;y++)o[y]&&f++;return f}let s=t;if(s<0&&(s=n+s),s<0||s>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=Array.from(e).filter((f,y)=>y!==s);if(i.length===0)return dt(r);let u=D.zeros(i,"int32"),c=u.data,l=e[s],p=i.reduce((f,y)=>f*y,1);for(let f=0;f<p;f++){let y=0;for(let m=0;m<l;m++){let d=v(f,s,m,e),g=T(d,e);o[g]&&y++}c[f]=y}return u}var _=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((s,i)=>{let u=s;if(u<0&&(u=this.shape[i]+u),u<0||u>=this.shape[i])throw new Error(`Index ${s} is out of bounds for axis ${i} with size ${this.shape[i]}`);return u}),o=this.dtype,a;x(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,s=O(t);if(!s)throw new Error(`Cannot convert to dtype ${t}`);let i=new s(a),u=this.data;if(x(n)&&!x(t)){let l=u;if(t==="bool")for(let p=0;p<a;p++)i[p]=l[p]!==BigInt(0)?1:0;else for(let p=0;p<a;p++)i[p]=Number(l[p])}else if(!x(n)&&x(t)){let l=u;for(let p=0;p<a;p++)i[p]=BigInt(Math.round(Number(l[p])))}else if(t==="bool"){let l=u;for(let p=0;p<a;p++)i[p]=l[p]!==0?1:0}else if(n==="bool"&&!x(t)){let l=u;for(let p=0;p<a;p++)i[p]=l[p]}else if(!x(n)&&!x(t)){let l=u;for(let p=0;p<a;p++)i[p]=l[p]}else{let l=u;for(let p=0;p<a;p++)i[p]=l[p]}let c=D.fromData(i,o,t);return new r(c)}add(t){let e=typeof t=="number"?t:t._storage,n=vt(this._storage,e);return r._fromStorage(n)}subtract(t){let e=typeof t=="number"?t:t._storage,n=Ot(this._storage,e);return r._fromStorage(n)}multiply(t){let e=typeof t=="number"?t:t._storage,n=$t(this._storage,e);return r._fromStorage(n)}divide(t){let e=typeof t=="number"?t:t._storage,n=Mt(this._storage,e);return r._fromStorage(n)}mod(t){let e=typeof t=="number"?t:t._storage,n=dr(this._storage,e);return r._fromStorage(n)}floor_divide(t){let e=typeof t=="number"?t:t._storage,n=kr(this._storage,e);return r._fromStorage(n)}positive(){let t=Ft(this._storage);return r._fromStorage(t)}reciprocal(){let t=Pt(this._storage);return r._fromStorage(t)}sqrt(){let t=Te(this._storage);return r._fromStorage(t)}power(t){let e=typeof t=="number"?t:t._storage,n=ve(this._storage,e);return r._fromStorage(n)}absolute(){let t=Ct(this._storage);return r._fromStorage(t)}negative(){let t=kt(this._storage);return r._fromStorage(t)}sign(){let t=Vt(this._storage);return r._fromStorage(t)}sin(){let t=Oe(this._storage);return r._fromStorage(t)}cos(){let t=$e(this._storage);return r._fromStorage(t)}tan(){let t=Me(this._storage);return r._fromStorage(t)}arcsin(){let t=Ce(this._storage);return r._fromStorage(t)}arccos(){let t=ke(this._storage);return r._fromStorage(t)}arctan(){let t=Ve(this._storage);return r._fromStorage(t)}arctan2(t){let e=typeof t=="number"?t:t._storage,n=Fe(this._storage,e);return r._fromStorage(n)}hypot(t){let e=typeof t=="number"?t:t._storage,n=Pe(this._storage,e);return r._fromStorage(n)}degrees(){let t=Re(this._storage);return r._fromStorage(t)}radians(){let t=Le(this._storage);return r._fromStorage(t)}sinh(){let t=qe(this._storage);return r._fromStorage(t)}cosh(){let t=je(this._storage);return r._fromStorage(t)}tanh(){let t=Ze(this._storage);return r._fromStorage(t)}arcsinh(){let t=We(this._storage);return r._fromStorage(t)}arccosh(){let t=Ge(this._storage);return r._fromStorage(t)}arctanh(){let t=He(this._storage);return r._fromStorage(t)}greater(t){let e=typeof t=="number"?t:t._storage,n=Gt(this._storage,e);return r._fromStorage(n)}greater_equal(t){let e=typeof t=="number"?t:t._storage,n=Ht(this._storage,e);return r._fromStorage(n)}less(t){let e=typeof t=="number"?t:t._storage,n=Yt(this._storage,e);return r._fromStorage(n)}less_equal(t){let e=typeof t=="number"?t:t._storage,n=Jt(this._storage,e);return r._fromStorage(n)}equal(t){let e=typeof t=="number"?t:t._storage,n=Xt(this._storage,e);return r._fromStorage(n)}not_equal(t){let e=typeof t=="number"?t:t._storage,n=Qt(this._storage,e);return r._fromStorage(n)}isclose(t,e=1e-5,n=1e-8){let o=typeof t=="number"?t:t._storage,a=Fr(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 Kt(this._storage,o,e,n)}bitwise_and(t){let e=typeof t=="number"?t:t._storage,n=hn(this._storage,e);return r._fromStorage(n)}bitwise_or(t){let e=typeof t=="number"?t:t._storage,n=An(this._storage,e);return r._fromStorage(n)}bitwise_xor(t){let e=typeof t=="number"?t:t._storage,n=bn(this._storage,e);return r._fromStorage(n)}bitwise_not(){let t=ct(this._storage);return r._fromStorage(t)}invert(){let t=Sn(this._storage);return r._fromStorage(t)}left_shift(t){let e=typeof t=="number"?t:t._storage,n=Dn(this._storage,e);return r._fromStorage(n)}right_shift(t){let e=typeof t=="number"?t:t._storage,n=xn(this._storage,e);return r._fromStorage(n)}sum(t,e=!1){let n=or(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}mean(t,e=!1){let n=gr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}max(t,e=!1){let n=hr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}min(t,e=!1){let n=Ar(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}prod(t,e=!1){let n=Pr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}argmin(t){let e=Rr(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}argmax(t){let e=Lr(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}var(t,e=0,n=!1){let o=qr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}std(t,e=0,n=!1){let o=te(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}all(t,e=!1){let n=jr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}any(t,e=!1){let n=Zr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}cumsum(t){return r._fromStorage(Wr(this._storage,t))}cumprod(t){return r._fromStorage(Gr(this._storage,t))}ptp(t,e=!1){let n=Hr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}median(t,e=!1){let n=Yr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}percentile(t,e,n=!1){let o=Jr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}quantile(t,e,n=!1){let o=tr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}average(t,e){let n=br(this._storage,e,t?.storage);return typeof n=="number"?n:r._fromStorage(n)}nansum(t,e=!1){let n=Sr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanprod(t,e=!1){let n=Dr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanmean(t,e=!1){let n=xr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanvar(t,e=0,n=!1){let o=ar(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}nanstd(t,e=0,n=!1){let o=Xr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}nanmin(t,e=!1){let n=Nr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanmax(t,e=!1){let n=wr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanargmin(t){let e=Ir(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}nanargmax(t){let e=zr(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}nancumsum(t){return r._fromStorage(Qr(this._storage,t))}nancumprod(t){return r._fromStorage(Kr(this._storage,t))}nanmedian(t,e=!1){let n=_r(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}sort(t=-1){return r._fromStorage(lt(this._storage,t))}argsort(t=-1){return r._fromStorage(ft(this._storage,t))}partition(t,e=-1){return r._fromStorage(pt(this._storage,t,e))}argpartition(t,e=-1){return r._fromStorage(yt(this._storage,t,e))}nonzero(){return Ur(this._storage).map(e=>r._fromStorage(e))}searchsorted(t,e="left"){return r._fromStorage(mt(this._storage,t._storage,e))}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=Br(this._storage);return r._fromStorage(t)}ravel(){let t=ir(this._storage),n=t.data===this.data?this._base??this:void 0;return r._fromStorage(t,n)}transpose(t){let e=ur(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}squeeze(t){let e=ee(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}expand_dims(t){let e=sr(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}swapaxes(t,e){let n=ne(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}moveaxis(t,e){let n=rt(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}repeat(t,e){let n=pe(this._storage,t,e);return r._fromStorage(n)}take(t,e){let n=Je(this._storage,t,e);return r._fromStorage(n)}put(t,e){let n=e instanceof r?e._storage:e;Xe(this._storage,t,n)}matmul(t){let e=K(this._storage,t._storage);return r._fromStorage(e)}dot(t){let e=nt(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}trace(){return Ie(this._storage)}inner(t){let e=ze(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}outer(t){let e=ot(this._storage,t._storage);return r._fromStorage(e)}tensordot(t,e=2){let n=_e(this._storage,t._storage,e);return typeof n=="number"||typeof n=="bigint"?n:r._fromStorage(n)}cbrt(){let t=Rt(this._storage);return r._fromStorage(t)}fabs(){let t=Lt(this._storage);return r._fromStorage(t)}divmod(t){let e=typeof t=="number"?t:t._storage,[n,o]=qt(this._storage,e);return[r._fromStorage(n),r._fromStorage(o)]}square(){let t=jt(this._storage);return r._fromStorage(t)}remainder(t){let e=typeof t=="number"?t:t._storage,n=Zt(this._storage,e);return r._fromStorage(n)}heaviside(t){let e=typeof t=="number"?t:t._storage,n=Wt(this._storage,e);return 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((u,c)=>{let l=zt(u);return _t(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 p;c.step>0?p=Math.max(0,Math.ceil((c.stop-c.start)/c.step)):p=Math.max(0,Math.ceil((c.start-c.stop)/Math.abs(c.step))),n.push(p),o.push(l*c.step)}}let s=D.fromData(this._storage.data,n,this._storage.dtype,o,a),i=this._base??this;return new r(s,i)}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 s=[];for(let i=0;i<t[a];i++)o[a]=i,s.push(n(o,a+1));return s};return n(new Array(e),0)}};function H(r,t=k){let e=D.zeros(r,t);return new _(e)}function Un(r,t=k){let e=D.ones(r,t);return new _(e)}function Jo(r){let t=[],e=r;for(;Array.isArray(e);)t.push(e.length),e=e[0];return t}function Tn(r){return typeof r=="bigint"?!0:Array.isArray(r)?r.some(t=>Tn(t)):!1}function Xo(r){let t=[];function e(n){Array.isArray(n)?n.forEach(o=>e(o)):t.push(n)}return e(r),t}function L(r,t){if(r instanceof _)return!t||r.dtype===t?r.copy():r.astype(t);let e=Tn(r),n=Jo(r),o=n.reduce((l,p)=>l*p,1),a=t;a||(e?a="int64":a=k);let s=O(a);if(!s)throw new Error(`Cannot create array with dtype ${a}`);let i=new s(o),u=Xo(r);if(x(a)){let l=i;for(let p=0;p<o;p++){let f=u[p];l[p]=typeof f=="bigint"?f:BigInt(Math.round(Number(f)))}}else if(a==="bool"){let l=i;for(let p=0;p<o;p++)l[p]=u[p]?1:0}else{let l=i;for(let p=0;p<o;p++){let f=u[p];l[p]=Number(f)}}let c=D.fromData(i,n,a);return new _(c)}function Qo(r,t,e=1,n=k){let o=r,a=t;if(t===void 0&&(o=0,a=r),a===void 0)throw new Error("stop is required");let s=Math.max(0,Math.ceil((a-o)/e)),i=O(n);if(!i)throw new Error(`Cannot create arange array with dtype ${n}`);let u=new i(s);if(x(n))for(let l=0;l<s;l++)u[l]=BigInt(Math.round(o+l*e));else if(n==="bool")for(let l=0;l<s;l++)u[l]=o+l*e!==0?1:0;else for(let l=0;l<s;l++)u[l]=o+l*e;let c=D.fromData(u,[s],n);return new _(c)}function Ko(r,t,e=50,n=k){if(e<0)throw new Error("num must be non-negative");if(e===0)return L([],n);if(e===1)return L([r],n);let o=O(n);if(!o)throw new Error(`Cannot create linspace array with dtype ${n}`);let a=new o(e),s=(t-r)/(e-1);if(x(n))for(let u=0;u<e;u++)a[u]=BigInt(Math.round(r+u*s));else if(n==="bool")for(let u=0;u<e;u++)a[u]=r+u*s!==0?1:0;else for(let u=0;u<e;u++)a[u]=r+u*s;let i=D.fromData(a,[e],n);return new _(i)}function ra(r,t,e=50,n=10,o=k){if(e<0)throw new Error("num must be non-negative");if(e===0)return L([],o);if(e===1)return L([Math.pow(n,r)],o);let a=O(o);if(!a)throw new Error(`Cannot create logspace array with dtype ${o}`);let s=new a(e),i=(t-r)/(e-1);if(x(o))for(let c=0;c<e;c++){let l=r+c*i;s[c]=BigInt(Math.round(Math.pow(n,l)))}else if(o==="bool")for(let c=0;c<e;c++){let l=r+c*i;s[c]=Math.pow(n,l)!==0?1:0}else for(let c=0;c<e;c++){let l=r+c*i;s[c]=Math.pow(n,l)}let u=D.fromData(s,[e],o);return new _(u)}function ta(r,t,e=50,n=k){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 L([],n);if(e===1)return L([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 s=O(n);if(!s)throw new Error(`Cannot create geomspace array with dtype ${n}`);let i=new s(e),u=Math.log(Math.abs(r)),l=(Math.log(Math.abs(t))-u)/(e-1);if(x(n))for(let f=0;f<e;f++){let y=o*Math.exp(u+f*l);i[f]=BigInt(Math.round(y))}else if(n==="bool")for(let f=0;f<e;f++){let y=o*Math.exp(u+f*l);i[f]=y!==0?1:0}else for(let f=0;f<e;f++){let y=o*Math.exp(u+f*l);i[f]=y}let p=D.fromData(i,[e],n);return new _(p)}function vn(r,t,e=0,n=k){let o=t??r,a=H([r,o],n),s=a.data;if(x(n)){let i=s;for(let u=0;u<r;u++){let c=u+e;c>=0&&c<o&&(i[u*o+c]=BigInt(1))}}else{let i=s;for(let u=0;u<r;u++){let c=u+e;c>=0&&c<o&&(i[u*o+c]=1)}}return a}function On(r,t=k){return H(r,t)}function $n(r,t,e){let n=e;n||(typeof t=="bigint"?n="int64":typeof t=="boolean"?n="bool":Number.isInteger(t)?n="int32":n=k);let o=O(n);if(!o)throw new Error(`Cannot create full array with dtype ${n}`);let a=r.reduce((u,c)=>u*c,1),s=new o(a);if(x(n)){let u=typeof t=="bigint"?t:BigInt(Math.round(Number(t)));s.fill(u)}else n==="bool"?s.fill(t?1:0):s.fill(Number(t));let i=D.fromData(s,r,n);return new _(i)}function ea(r,t=k){return vn(r,r,0,t)}function Tr(r,t){return r instanceof _?!t||r.dtype===t?r:r.astype(t):L(r,t)}function na(r){return r.copy()}function oa(r,t){return H(Array.from(r.shape),t??r.dtype)}function aa(r,t){return Un(Array.from(r.shape),t??r.dtype)}function sa(r,t){return On(Array.from(r.shape),t??r.dtype)}function ia(r,t,e){return $n(Array.from(r.shape),t,e??r.dtype)}function ua(r,t){return Tr(r,t)}function ca(r,t){let e=Tr(r,t);return e.flags.C_CONTIGUOUS?e:e.copy()}function la(r,t){return Tr(r,t).copy()}function Mn(r,t=0){if(r.ndim===1){let e=r.size,n=e+Math.abs(t),o=H([n,n],r.dtype);for(let a=0;a<e;a++){let s=t>=0?a:a-t,i=t>=0?a+t:a;o.set([s,i],r.get([a]))}return o}else if(r.ndim===2){let[e,n]=r.shape,o,a,s;if(t>=0?(o=0,a=t,s=Math.min(e,n-t)):(o=-t,a=0,s=Math.min(e+t,n)),s<=0)return H([0],r.dtype);let i=O(r.dtype),u=new i(s);for(let l=0;l<s;l++){let p=r.get([o+l,a+l]);x(r.dtype)?u[l]=typeof p=="bigint"?p:BigInt(p):u[l]=p}let c=D.fromData(u,[s],r.dtype);return new _(c)}else throw new Error("Input must be 1-D or 2-D")}function fa(r,t=0){let e=r.flatten();return Mn(e,t)}function pa(r,t,e=k){let n=t.reduce((c,l)=>c*l,1),o=O(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n),s=t.length,i=new Array(s).fill(0);for(let c=0;c<n;c++){let l=r(...i);x(e)?a[c]=typeof l=="bigint"?l:BigInt(Number(l)):e==="bool"?a[c]=l?1:0:a[c]=Number(l);for(let p=s-1;p>=0&&(i[p]++,!(i[p]<t[p]));p--)i[p]=0}let u=D.fromData(a,t,e);return new _(u)}function ya(...r){let t=[],e="xy";for(let i of r)i instanceof _?t.push(i):typeof i=="object"&&"indexing"in i&&(e=i.indexing||"xy");if(t.length===0)return[];if(t.length===1)return[t[0].copy()];let n=t.map(i=>i.size);e==="xy"&&t.length>=2&&(t=[t[1],t[0],...t.slice(2)],[n[0],n[1]]=[n[1],n[0]]);let o=n,a=o.length,s=[];for(let i=0;i<t.length;i++){let u=t[i],c=u.size,l=new Array(a).fill(1);l[i]=c;let p=u.reshape(...l),f=at(p.storage,o),y=_._fromStorage(f.copy());s.push(y)}return e==="xy"&&s.length>=2&&([s[0],s[1]]=[s[1],s[0]]),s}function ma(r,t,e=0,n=k){let o=t??r,a=H([r,o],n);for(let s=0;s<r;s++)for(let i=0;i<=s+e&&i<o;i++)i>=0&&a.set([s,i],1);return a}function da(r,t=0){if(r.ndim<2)throw new Error("Input must have at least 2 dimensions");let e=r.copy(),n=e.shape,o=n[n.length-2],a=n[n.length-1],s=n.slice(0,-2).reduce((i,u)=>i*u,1);for(let i=0;i<s;i++)for(let u=0;u<o;u++)for(let c=0;c<a;c++)if(c>u+t){let l=[],p=i;for(let f=n.length-3;f>=0;f--)l.unshift(p%n[f]),p=Math.floor(p/n[f]);l.push(u,c),e.set(l,0)}return e}function ga(r,t=0){if(r.ndim<2)throw new Error("Input must have at least 2 dimensions");let e=r.copy(),n=e.shape,o=n[n.length-2],a=n[n.length-1],s=n.slice(0,-2).reduce((i,u)=>i*u,1);for(let i=0;i<s;i++)for(let u=0;u<o;u++)for(let c=0;c<a;c++)if(c<u+t){let l=[],p=i;for(let f=n.length-3;f>=0;f--)l.unshift(p%n[f]),p=Math.floor(p/n[f]);l.push(u,c),e.set(l,0)}return e}function ha(r,t,e=!1){if(r.ndim!==1)throw new Error("Input must be 1-D");let n=r.size,o=t??n;if(o<0)throw new Error("N must be non-negative");let a=H([n,o],r.dtype);for(let s=0;s<n;s++){let i=r.get([s]);for(let u=0;u<o;u++){let c=e?u:o-1-u;a.set([s,u],Math.pow(i,c))}}return a}function Aa(r,t=k,e=-1,n=0){let o,a=n;r instanceof ArrayBuffer?o=r:(o=r.buffer,a+=r.byteOffset);let s=xa(t),i=o.byteLength-a,u=Math.floor(i/s),c=e<0?u:Math.min(e,u);if(c<=0)return L([],t);let l=O(t);if(!l)throw new Error(`Unsupported dtype: ${t}`);let p=new l(o,a,c),f=D.fromData(p,[c],t);return new _(f)}function ba(r,t=k,e=-1){let n=[],o=0;for(let a of r){if(e>=0&&o>=e)break;n.push(a),o++}return L(n,t)}function Sa(r,t=k,e=-1){let n=[],o=0;for(let a of r){if(e>=0&&o>=e)break;n.push(a),o++}return L(n,t)}function Da(r,t=k,e=-1,n=""){let o;n===""?o=r.trim().split(/\s+/):o=r.split(n);let a=[],s=0;for(let i of o){if(e>=0&&s>=e)break;let u=i.trim();u!==""&&(x(t)?a.push(BigInt(u)):a.push(parseFloat(u)),s++)}return L(a,t)}function xa(r){switch(r){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 Na(r){return r.sqrt()}function wa(r,t){return r.power(t)}function Ia(r){return r.absolute()}function za(r){return r.negative()}function _a(r){return r.sign()}function Ba(r,t){return r.mod(t)}function Ea(r,t){return r.floor_divide(t)}function Ua(r){return r.positive()}function Ta(r){return r.reciprocal()}function va(r,t){return r.dot(t)}function Oa(r){return r.trace()}function $a(r,t=0,e=0,n=1){let o=Be(r.storage,t,e,n);return _._fromStorage(o)}function Ma(r,t){let e=Ue(r.storage,t.storage);return _._fromStorage(e)}function Ca(r,t){return r.transpose(t)}function ka(r,t){return r.inner(t)}function Va(r,t){return r.outer(t)}function Fa(r,t,e=2){return r.tensordot(t,e)}function Pa(r){return r.sin()}function Ra(r){return r.cos()}function La(r){return r.tan()}function qa(r){return r.arcsin()}function ja(r){return r.arccos()}function Za(r){return r.arctan()}function Wa(r,t){return r.arctan2(t)}function Ga(r,t){return r.hypot(t)}function Ha(r){return r.degrees()}function Ya(r){return r.radians()}function Ja(r){return r.radians()}function Xa(r){return r.degrees()}function Qa(r){return r.sinh()}function Ka(r){return r.cosh()}function rs(r){return r.tanh()}function ts(r){return r.arcsinh()}function es(r){return r.arccosh()}function ns(r){return r.arctanh()}function os(r,t,e){return r.swapaxes(t,e)}function as(r,t,e){return r.moveaxis(t,e)}function er(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=Q(e,t);return _._fromStorage(n)}function ss(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=oe(e,t);return _._fromStorage(n)}function Cn(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=ae(t);return _._fromStorage(e)}function is(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=tt(t);return _._fromStorage(e)}function us(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=se(t);return _._fromStorage(e)}function cs(r,t,e=0){return ie(r.storage,t,e).map(o=>_._fromStorage(o,r.base??r))}function ls(r,t,e=0){return cr(r.storage,t,e).map(o=>_._fromStorage(o,r.base??r))}function fs(r,t){return ce(r.storage,t).map(n=>_._fromStorage(n,r.base??r))}function ps(r,t){return le(r.storage,t).map(n=>_._fromStorage(n,r.base??r))}function ys(r,t){let e=fe(r.storage,t);return _._fromStorage(e)}function ms(r,t,e){return r.repeat(t,e)}function ds(r){return r.ravel()}function gs(r,t){return r.reshape(...t)}function hs(r,t){return r.squeeze(t)}function As(r,t){return r.expand_dims(t)}function gt(r,t){let e=ye(r.storage,t);return _._fromStorage(e)}function bs(r){if(r.ndim<2)throw new Error("Input must be at least 2-D");return gt(r,1)}function Ss(r){if(r.ndim<2)throw new Error("Input must be at least 2-D");return gt(r,0)}function Ds(r,t=1,e=[0,1]){let n=me(r.storage,t,e);return _._fromStorage(n)}function xs(r,t,e){let n=de(r.storage,t,e);return _._fromStorage(n)}function Ns(r,t,e=0){let n=ge(r.storage,t,e);return _._fromStorage(n,r.base??r)}function ws(...r){let t=r.map(o=>o.storage),n=Se(t).map((o,a)=>o===t[a]?r[a]:_._fromStorage(o));return n.length===1?n[0]:n}function Is(...r){let t=r.map(o=>o.storage),n=De(t).map((o,a)=>o===t[a]?r[a]:_._fromStorage(o));return n.length===1?n[0]:n}function zs(...r){let t=r.map(o=>o.storage),n=xe(t).map((o,a)=>o===t[a]?r[a]:_._fromStorage(o));return n.length===1?n[0]:n}function _s(r,t){return he(r.storage,t).map(n=>_._fromStorage(n,r.base??r))}function Bs(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=Ae(t);return _._fromStorage(e)}function Es(r){return Cn(r)}function Us(r,t){let e=be(r.storage,t);return _._fromStorage(e)}function Ts(r,t,e){let n=t instanceof _?t:L(t,r.dtype);if(e===void 0){let o=r.flatten(),a=n.flatten();return er([o,a])}return er([r,n],e)}function vs(r,t,e){let n=r.dtype;if(e===void 0){let y=r.flatten(),d=(Array.isArray(t)?t:[t]).map(S=>S<0?y.size+S:S),g=[];for(let S=0;S<y.size;S++)d.includes(S)||g.push(S);let A=O(n),h=new A(g.length);for(let S=0;S<g.length;S++){let N=y.get([g[S]]);x(n)?h[S]=typeof N=="bigint"?N:BigInt(N):h[S]=N}let b=D.fromData(h,[g.length],n);return new _(b)}let o=r.shape,a=o.length,s=e<0?a+e:e;if(s<0||s>=a)throw new Error(`axis ${e} is out of bounds for array of dimension ${a}`);let i=o[s],u=Array.isArray(t)?t:[t],c=new Set(u.map(y=>y<0?i+y:y)),l=[],p=0;for(let y=0;y<=i;y++)(c.has(y)||y===i)&&(y>p&&l.push([p,y]),p=y+1);if(l.length===0){let y=[...o];return y[s]=0,H(y,n)}let f=[];for(let[y,m]of l){let d=o.map(()=>":");d[s]=`${y}:${m}`,f.push(r.slice(...d))}return er(f,s)}function Os(r,t,e,n){let o=e instanceof _?e:L(e,r.dtype);if(n===void 0){let p=r.flatten(),f=o.flatten(),y=t<0?p.size+t:t;if(y<0||y>p.size)throw new Error(`index ${t} is out of bounds for array of size ${p.size}`);let m=y>0?p.slice(`0:${y}`):null,d=y<p.size?p.slice(`${y}:`):null,g=[];return m&&g.push(m),g.push(f),d&&g.push(d),er(g)}let a=r.shape,s=a.length,i=n<0?s+n:n;if(i<0||i>=s)throw new Error(`axis ${n} is out of bounds for array of dimension ${s}`);let u=a[i],c=t<0?u+t:t;if(c<0||c>u)throw new Error(`index ${t} is out of bounds for axis ${n} with size ${u}`);let l=[];if(c>0){let p=a.map(()=>":");p[i]=`0:${c}`,l.push(r.slice(...p))}if(l.push(o),c<u){let p=a.map(()=>":");p[i]=`${c}:`,l.push(r.slice(...p))}return er(l,i)}function $s(r,t,e="constant",n=0){let o=r.shape,a=o.length,s=r.dtype,i;if(typeof t=="number"?i=o.map(()=>[t,t]):Array.isArray(t)&&typeof t[0]=="number"?i=o.map(()=>t):i=t,i.length!==a)throw new Error(`pad_width must have ${a} elements`);let u=o.map((d,g)=>d+i[g][0]+i[g][1]),c=u.reduce((d,g)=>d*g,1),l=O(s),p=new l(c),f=x(s);e==="constant"&&(f?p.fill(BigInt(n)):p.fill(n));let y=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]=i[b],N=y[b]-S;if(N<0||N>=o[b]){g=!1;break}A.push(N)}let h;if(g)h=r.get(A);else if(e==="constant"){for(let b=a-1;b>=0&&(y[b]++,!(y[b]<u[b]));b--)y[b]=0;continue}else{let b=[];for(let S=0;S<a;S++){let[N]=i[S],w=y[S]-N,I=o[S];w<0?e==="edge"?w=0:e==="reflect"?(w=-w,w>=I&&(w=I-1)):e==="symmetric"?(w=-w-1,w>=I&&(w=I-1),w<0&&(w=0)):e==="wrap"&&(w=(w%I+I)%I):w>=I&&(e==="edge"?w=I-1:e==="reflect"?(w=2*I-w-2,w<0&&(w=0)):e==="symmetric"?(w=2*I-w-1,w<0&&(w=0)):e==="wrap"&&(w=w%I)),b.push(Math.max(0,Math.min(I-1,w)))}h=r.get(b)}f?p[d]=typeof h=="bigint"?h:BigInt(Number(h)):p[d]=Number(h);for(let b=a-1;b>=0&&(y[b]++,!(y[b]<u[b]));b--)y[b]=0}let m=D.fromData(p,u,s);return new _(m)}function Ms(r,t){let e=at(r.storage,t);return _._fromStorage(e,r.base??r)}function Cs(...r){let t=r.map(n=>n.storage);return Ye(t).map((n,o)=>_._fromStorage(n,r[o].base??r[o]))}function ks(...r){return Vr(...r)}function Vs(r,t,e){return r.take(t,e)}function Fs(r,t,e){r.put(t,e)}function Ps(r,t){let e=t.map(o=>o.storage),n=Qe(r.storage,e);return _._fromStorage(n)}function Rs(r,t,e=!1){return Ke(r.storage,t.storage,e)}function Ls(r,t){return re(r.storage,t.storage)}function qs(r,t){return _._fromStorage(Wr(r.storage,t))}function js(r,t){return _._fromStorage(Gr(r.storage,t))}function Zs(r,t,e=!1){let n=Hr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function Ws(r,t,e=!1){let n=Yr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function Gs(r,t,e,n=!1){let o=Jr(r.storage,t,e,n);return typeof o=="number"?o:_._fromStorage(o)}function Hs(r,t,e,n=!1){let o=tr(r.storage,t,e,n);return typeof o=="number"?o:_._fromStorage(o)}function Ys(r,t,e,n=!1){let o=e?e.storage:void 0,a=br(r.storage,t,o,n);return typeof a=="number"?a:_._fromStorage(a)}function Js(r,t,e=!1){let n=Sr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function Xs(r,t,e=!1){let n=Dr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function Qs(r,t,e=!1){let n=xr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function Ks(r,t,e=0,n=!1){let o=ar(r.storage,t,e,n);return typeof o=="number"?o:_._fromStorage(o)}function ri(r,t,e=0,n=!1){let o=Xr(r.storage,t,e,n);return typeof o=="number"?o:_._fromStorage(o)}function ti(r,t,e=!1){let n=Nr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function ei(r,t,e=!1){let n=wr(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function ni(r,t){let e=Ir(r.storage,t);return typeof e=="number"?e:_._fromStorage(e)}function oi(r,t){let e=zr(r.storage,t);return typeof e=="number"?e:_._fromStorage(e)}function ai(r,t){return _._fromStorage(Qr(r.storage,t))}function si(r,t){return _._fromStorage(Kr(r.storage,t))}function ii(r,t,e=!1){let n=_r(r.storage,t,e);return typeof n=="number"?n:_._fromStorage(n)}function ui(r){return r.cbrt()}function ci(r){return r.fabs()}function li(r,t){return r.divmod(t)}function fi(r){return r.square()}function pi(r,t){return r.remainder(t)}function yi(r,t){return r.heaviside(t)}function mi(r,t){return r.bitwise_and(t)}function di(r,t){return r.bitwise_or(t)}function gi(r,t){return r.bitwise_xor(t)}function hi(r){return r.bitwise_not()}function Ai(r){return r.invert()}function bi(r,t){return r.left_shift(t)}function Si(r,t){return r.right_shift(t)}function Di(r,t=-1,e="big"){let n=Nn(r.storage,t,e);return _._fromStorage(n)}function xi(r,t=-1,e=-1,n="big"){let o=wn(r.storage,t,e,n);return _._fromStorage(o)}function Ni(r,...t){let e=t.map(o=>o.storage),n=Ee(r,...e);return typeof n=="number"||typeof n=="bigint"?n:_._fromStorage(n)}function wi(r,t,e){return _._fromStorage(rn(r.storage,t.storage,e))}function Ii(r,t,e,n){tn(r.storage,t.storage,e.storage,n)}function zi(r,t,e){let n=e instanceof _?e.storage:e;en(r.storage,t.storage,n)}function _i(r,t,e){return _._fromStorage(nn(r.storage,t.storage,e))}function Bi(r,t,e=0){let n=r.map(a=>a.storage),o=t.map(a=>a.storage);return _._fromStorage(on(n,o,e))}function Ei(r,t,e){an(r.storage,t.storage,e.storage)}function Ui(r,t=2){return st(r,t).map(n=>_._fromStorage(n))}function Ti(r){return sn(r.storage).map(e=>_._fromStorage(e))}function vi(r,t=0,e){return it(r,t,e).map(o=>_._fromStorage(o))}function Oi(r,t=0){return un(r.storage,t).map(n=>_._fromStorage(n))}function $i(r,t=0,e){return ut(r,t,e).map(o=>_._fromStorage(o))}function Mi(r,t=0){return cn(r.storage,t).map(n=>_._fromStorage(n))}function Ci(r,t,e=0){return ln(r,(a,s)=>t(a,s).storage,e).map(a=>_._fromStorage(a))}function ki(r,t="int32"){return _._fromStorage(fn(r,t))}function Vi(...r){return pn(...r.map(e=>e.storage)).map(e=>_._fromStorage(e))}function Fi(r,t,e="raise"){let n=r.map(o=>o.storage);return _._fromStorage(yn(n,t,e))}function Pi(r,t,e="C"){let n=r instanceof _?r.storage:r;return mn(n,t,e).map(a=>_._fromStorage(a))}function Ri(r,t=-1){return _._fromStorage(lt(r.storage,t))}function Li(r,t=-1){return _._fromStorage(ft(r.storage,t))}function qi(r){let t=r.map(e=>e.storage);return _._fromStorage(In(t))}function ji(r,t,e=-1){return _._fromStorage(pt(r.storage,t,e))}function Zi(r,t,e=-1){return _._fromStorage(yt(r.storage,t,e))}function Wi(r){return _._fromStorage(zn(r.storage))}function Gi(r){return Ur(r.storage).map(e=>_._fromStorage(e))}function Hi(r){return _._fromStorage(_n(r.storage))}function Yi(r,t,e){let n=Bn(r.storage,t?.storage,e?.storage);return Array.isArray(n)?n.map(o=>_._fromStorage(o)):_._fromStorage(n)}function Ji(r,t,e="left"){return _._fromStorage(mt(r.storage,t.storage,e))}function Xi(r,t){return _._fromStorage(En(r.storage,t.storage))}function Qi(r,t){let e=dt(r.storage,t);return typeof e=="number"?e:_._fromStorage(e)}var fr=new Uint8Array([147,78,85,77,80,89]),ht=["float64","float32","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"];function At(){let r=new ArrayBuffer(2);return new DataView(r).setInt16(0,256,!0),new Int16Array(r)[0]===256}var Ki={f8:"float64",f4:"float32",i8:"int64",i4:"int32",i2:"int16",i1:"int8",u8:"uint64",u4:"uint32",u2:"uint16",u1:"uint8",b1:"bool"},vr={float64:"<f8",float32:"<f4",int64:"<i8",int32:"<i4",int16:"<i2",int8:"|i1",uint64:"<u8",uint32:"<u4",uint16:"<u2",uint8:"|u1",bool:"|b1"},kn={c:"complex numbers",S:"byte strings",U:"Unicode strings",O:"Python objects",V:"structured arrays (void)",M:"datetime64",m:"timedelta64"};function Vn(r){if(r.startsWith("[")||r.startsWith("("))throw new Y(`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 kn)throw new Y(`Unsupported dtype: ${kn[n]} (${r}). Use the 'force' parameter to skip arrays with unsupported dtypes.`);let o=Ki[e];if(!o)throw new Y(`Unknown or unsupported dtype descriptor: ${r}. Supported types: ${ht.join(", ")}. Use the 'force' parameter to skip arrays with unsupported dtypes.`);let a=At(),s=t==="<"||t==="|"||t==="="&&a,i=t===">"||t==="="&&!a,u=parseInt(e.slice(1),10),c=u>1&&(i&&a||s&&!a);return{dtype:o,needsByteSwap:c,itemsize:u}}var Y=class extends Error{constructor(t){super(t),this.name="UnsupportedDTypeError"}},q=class extends Error{constructor(t){super(t),this.name="InvalidNpyError"}};function Or(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=bt(t);return St(t,e)}function bt(r){if(r.length<10)throw new q("File too small to be a valid NPY file");for(let c=0;c<fr.length;c++)if(r[c]!==fr[c])throw new q("Invalid NPY magic number");let t=r[6],e=r[7];if(t!==1&&t!==2&&t!==3)throw new q(`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 q("File truncated: header extends beyond file");let s=r.slice(o,a),i=new TextDecoder("utf-8").decode(s).trim(),u=ru(i);return{version:{major:t,minor:e},header:u,dataOffset:a}}function St(r,t){let{header:e,dataOffset:n}=t,{dtype:o,needsByteSwap:a,itemsize:s}=Vn(e.descr),i=e.shape.reduce((d,g)=>d*g,1),u=i*s,c=r.length-n;if(c<u)throw new q(`File truncated: expected ${u} bytes of data, got ${c}`);let l=new ArrayBuffer(u);new Uint8Array(l).set(r.subarray(n,n+u));let f=tu(l,o,i,a,s),y=e.shape,m;if(e.fortran_order&&y.length>1){let d=[...y].reverse(),g=D.fromData(f,d,o);m=eu(g,d),y=e.shape}else m=D.fromData(f,[...y],o);return new _(m)}function ru(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 q(`Failed to parse NPY header: ${r}`);let o=t[1],a=e[1]==="True",s=n[1].trim(),i;return s===""?i=[]:i=s.split(",").map(u=>u.trim()).filter(u=>u!=="").map(u=>{let c=parseInt(u,10);if(isNaN(c))throw new q(`Invalid shape value: ${u}`);return c}),{descr:o,fortran_order:a,shape:i}}function tu(r,t,e,n,o){let a=O(t);if(!a)throw new q(`Cannot create array for dtype: ${t}`);if(!n)return new a(r,0,e);let s=new Uint8Array(r),i=new Uint8Array(r.byteLength);for(let u=0;u<e;u++){let c=u*o;for(let l=0;l<o;l++)i[c+l]=s[c+o-1-l]}return new a(i.buffer,0,e)}function eu(r,t){let e=t.length,n=r.size,o=r.dtype,a=O(o);if(!a)throw new q(`Cannot create array for dtype: ${o}`);let s=new a(n),i=[...t].reverse(),u=Fn(t),c=Fn(i),l=new Array(e).fill(0);for(let p=0;p<n;p++){let f=p;for(let m=0;m<e;m++){let d=u[m];l[m]=Math.floor(f/d),f=f%d}let y=0;for(let m=0;m<e;m++)y+=l[e-1-m]*c[m];x(o),s[y]=r.iget(p)}return D.fromData(s,i,o)}function Fn(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 pr(r){let t=r.shape,e=r.dtype,n=vr[e],o=t.length===0?"()":t.length===1?`(${t[0]},)`:`(${t.join(", ")})`,a=`{'descr': '${n}', 'fortran_order': False, 'shape': ${o}, }`,s=12,u=(64-(s+a.length+1)%64)%64;a=a+" ".repeat(u)+`
|
|
2
|
-
`;let c=new TextEncoder().encode(a),l=c.length,p=r.size,f=Et(e),y=p*f,m=s+l+y,d=new Uint8Array(m);d.set(fr,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,s);let g=s+l;return nu(r,d.subarray(g),f),d}function nu(r,t,e){let n=r.dtype,o=r.size,a=At(),s=x(n),i=r._storage;if(i.isCContiguous&&i.offset===0&&a){let c=i.data,l=new Uint8Array(c.buffer,c.byteOffset,o*e);t.set(l)}else{let c=new DataView(t.buffer,t.byteOffset,t.byteLength);for(let l=0;l<o;l++){let p=i.iget(l),f=l*e;s?ou(c,f,p,n==="uint64"):au(c,f,p,n)}}}function ou(r,t,e,n){n?r.setBigUint64(t,e,!0):r.setBigInt64(t,e,!0)}function au(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 su=(()=>{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 Dt(r){let t=4294967295;for(let e=0;e<r.length;e++)t=su[(t^r[e])&255]^t>>>8;return(t^4294967295)>>>0}async function Pn(r){let t=Ln(r),e=new Map;for(let n of t){let o=await iu(n);e.set(n.name,o)}return e}function Rn(r){let t=Ln(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 Ln(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=new DataView(t.buffer,t.byteOffset,t.byteLength),n=[],o=-1;for(let c=t.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),s=e.getUint16(o+10,!0),i=[],u=a;for(let c=0;c<s&&e.getUint32(u,!0)===33639248;c++){let p=e.getUint16(u+10,!0),f=e.getUint32(u+16,!0),y=e.getUint32(u+20,!0),m=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=t.slice(u+46,u+46+d),S=new TextDecoder("utf-8").decode(b);i.push({name:S,compressionMethod:p,crc32:f,compressedSize:y,uncompressedSize:m,localHeaderOffset:h}),u=u+46+d+g+A}for(let c of i){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),y=e.getUint16(l+28,!0),m=l+30+f+y,d=t.slice(m,m+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 iu(r){if(r.compressionMethod===0)return r.compressedData;if(r.compressionMethod===8)return await uu(r.compressedData);throw new Error(`Unsupported compression method: ${r.compressionMethod}`)}async function uu(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:c,value:l}=await o.read();if(c)break;a.push(l)}let s=a.reduce((c,l)=>c+l.length,0),i=new Uint8Array(s),u=0;for(let c of a)i.set(c,u),u+=c.length;return i}async function wt(r,t={}){let e=t.force??!1,n=await Pn(r);return qn(n,e)}function It(r,t={}){let e=t.force??!1,n=Rn(r);return qn(n,e)}function qn(r,t){let e=new Map,n=[],o=new Map;for(let[a,s]of r){if(!a.endsWith(".npy"))continue;let i=a.slice(0,-4);try{let u=Or(s);e.set(i,u)}catch(u){if(u instanceof Y&&t)n.push(i),o.set(i,u.message);else throw u}}return{arrays:e,skipped:n,errors:o}}async function jn(r,t={}){let e=await wt(r,t);return Object.fromEntries(e.arrays)}function Zn(r,t={}){let e=It(r,t);return Object.fromEntries(e.arrays)}async function Wn(r,t={}){let e=t.compress??!1,n=[];for(let[f,y]of r){let m=Dt(y),d,g;e?(d=await lu(y),d.length<y.length?g=8:(d=y,g=0)):(d=y,g=0),n.push({name:f,data:y,compressedData:d,crc:m,compressionMethod:g,offset:0})}let o=0;for(let f of n){let y=new TextEncoder().encode(f.name);o+=30+y.length+f.compressedData.length}let a=0;for(let f of n){let y=new TextEncoder().encode(f.name);a+=46+y.length}let i=o+a+22,u=new Uint8Array(i),c=new DataView(u.buffer),l=0;for(let f of n)f.offset=l,l=Hn(u,c,l,f);let p=l;for(let f of n)l=Yn(u,c,l,f);return Jn(c,l,n.length,a,p),u}function Gn(r){let t=[];for(let[l,p]of r){let f=Dt(p);t.push({name:l,data:p,compressedData:p,crc:f,compressionMethod:0,offset:0})}let e=0;for(let l of t){let p=new TextEncoder().encode(l.name);e+=30+p.length+l.compressedData.length}let n=0;for(let l of t){let p=new TextEncoder().encode(l.name);n+=46+p.length}let a=e+n+22,s=new Uint8Array(a),i=new DataView(s.buffer),u=0;for(let l of t)l.offset=u,u=Hn(s,i,u,l);let c=u;for(let l of t)u=Yn(s,i,u,l);return Jn(i,u,t.length,n,c),s}function Hn(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 Yn(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 Jn(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 lu(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:c,value:l}=await o.read();if(c)break;a.push(l)}let s=a.reduce((c,l)=>c+l.length,0),i=new Uint8Array(s),u=0;for(let c of a)i.set(c,u),u+=c.length;return i}async function Xn(r,t={}){let e=Kn(r);return Wn(e,{compress:t.compress??!1})}function Qn(r){let t=Kn(r);return Gn(t)}function Kn(r){let t=new Map;if(Array.isArray(r)){for(let n=0;n<r.length;n++){let o=r[n],a=pr(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=pr(o),s=n.endsWith(".npy")?n:`${n}.npy`;t.set(s,a)}return t}var Rc="0.8.0";export{vr as DTYPE_TO_DESCR,q as InvalidNpyError,_ as NDArray,ht as SUPPORTED_DTYPES,Y as UnsupportedDTypeError,Rc as __version__,Ia as absolute,Ts as append,Qo as arange,ja as arccos,es as arccosh,qa as arcsin,ts as arcsinh,Za as arctan,Wa as arctan2,ns as arctanh,Zi as argpartition,Li as argsort,L as array,Rs as array_equal,Ls as array_equiv,ls as array_split,ua as asanyarray,Tr as asarray,ca as ascontiguousarray,la as asfortranarray,ws as atleast_1d,Is as atleast_2d,zs as atleast_3d,Ys as average,mi as bitwise_and,hi as bitwise_not,di as bitwise_or,gi as bitwise_xor,Cs as broadcast_arrays,ks as broadcast_shapes,Ms as broadcast_to,ui as cbrt,Ps as choose,Bs as column_stack,_i as compress,er as concatenate,na as copy,Ra as cos,Ka as cosh,Qi as count_nonzero,js as cumprod,qs as cumsum,Ja as deg2rad,Ha as degrees,vs as delete,Mn as diag,Ui as diag_indices,Ti as diag_indices_from,fa as diagflat,$a as diagonal,li as divmod,va as dot,_s as dsplit,us as dstack,Ni as einsum,On as empty,sa as empty_like,As as expand_dims,Xi as extract,vn as eye,ci as fabs,Hi as flatnonzero,gt as flip,bs as fliplr,Ss as flipud,Ea as floor_divide,Aa as frombuffer,ba as fromfile,pa as fromfunction,Sa as fromiter,Da as fromstring,$n as full,ia as full_like,ta as geomspace,yi as heaviside,ps as hsplit,is as hstack,Ga as hypot,ea as identity,ki as indices,ka as inner,Os as insert,Ai as invert,Vi as ix_,Ma as kron,bi as left_shift,qi as lexsort,Ko as linspace,jn as loadNpz,Zn as loadNpzSync,ra as logspace,Ci as mask_indices,Ws as median,ya as meshgrid,Ba as mod,as as moveaxis,oi as nanargmax,ni as nanargmin,si as nancumprod,ai as nancumsum,ei as nanmax,Qs as nanmean,ii as nanmedian,ti as nanmin,Xs as nanprod,ri as nanstd,Js as nansum,Ks as nanvar,za as negative,Gi as nonzero,Un as ones,aa as ones_like,Va as outer,Di as packbits,$s as pad,Or as parseNpy,St as parseNpyData,bt as parseNpyHeader,wt as parseNpz,It as parseNpzSync,ji as partition,Gs as percentile,Ei as place,Ua as positive,wa as power,Zs as ptp,Fs as put,Ii as put_along_axis,zi as putmask,Hs as quantile,Xa as rad2deg,Ya as radians,ds as ravel,Fi as ravel_multi_index,Ta as reciprocal,pi as remainder,ms as repeat,gs as reshape,Us as resize,Si as right_shift,xs as roll,Ns as rollaxis,Ds as rot90,Es as row_stack,Ji as searchsorted,Bi as select,pr as serializeNpy,Xn as serializeNpz,Qn as serializeNpzSync,_a as sign,Pa as sin,Qa as sinh,Ri as sort,Wi as sort_complex,cs as split,Na as sqrt,fi as square,hs as squeeze,ss as stack,os as swapaxes,Vs as take,wi as take_along_axis,La as tan,rs as tanh,Fa as tensordot,ys as tile,Oa as trace,Ca as transpose,ma as tri,da as tril,vi as tril_indices,Oi as tril_indices_from,ga as triu,$i as triu_indices,Mi as triu_indices_from,xi as unpackbits,Pi as unravel_index,ha as vander,fs as vsplit,Cn as vstack,Yi as where,H as zeros,oa as zeros_like};
|
|
1
|
+
function Wt(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 Gt(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 i=e<0?t+e:e;if(i<0||i>=t)throw new Error(`Index ${e} is out of bounds for size ${t}`);return{start:i,stop:i+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 q="float64";function T(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 Yt(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 Jt(r){return r==="int64"||r==="int32"||r==="int16"||r==="int8"||r==="uint64"||r==="uint32"||r==="uint16"||r==="uint8"}function Ht(r){return r==="float64"||r==="float32"}function I(r){return r==="int64"||r==="uint64"}function k(r,t){if(r===t)return r;if(r==="bool")return t;if(t==="bool")return r;if(Ht(r)||Ht(t)){if(r==="float64"||t==="float64")return"float64";if(r==="float32"){let c=t;return c==="int32"||c==="int64"||c==="uint32"||c==="uint64"?"float64":"float32"}if(t==="float32"){let c=r;return c==="int32"||c==="int64"||c==="uint32"||c==="uint64"?"float64":"float32"}return"float32"}let e=r.startsWith("int"),n=t.startsWith("int"),o=r.startsWith("uint"),a=t.startsWith("uint"),i=c=>c.includes("64")?64:c.includes("32")?32:c.includes("16")?16:c.includes("8")?8:0,u=i(r),s=i(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 c=Math.max(u,s);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?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 N=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,i=this._offset;for(let u=0;u<o;u++){let s=1;for(let l=u+1;l<o;l++)s*=e[l];let c=Math.floor(a/s);a=a%s,i+=c*n[u]}return this._data[i]}iset(t,e){let n=this._shape,o=this._strides,a=n.length;if(a===0){this._data[this._offset]=e;return}let i=t,u=this._offset;for(let s=0;s<a;s++){let c=1;for(let m=s+1;m<a;m++)c*=n[m];let l=Math.floor(i/c);i=i%c,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=T(e);if(!o)throw new Error(`Cannot copy array with dtype ${e}`);let a=new o(n);if(this.isCContiguous&&this._offset===0)if(I(e)){let i=this._data,u=a;for(let s=0;s<n;s++)u[s]=i[s]}else a.set(this._data);else if(I(e)){let i=a;for(let u=0;u<n;u++)i[u]=this.iget(u)}else for(let i=0;i<n;i++)a[i]=this.iget(i);return new r(a,t,r._computeStrides(t),0,e)}static fromData(t,e,n,o,a){let i=o??r._computeStrides(e),u=a??0;return new r(t,e,i,u,n)}static zeros(t,e=q){let n=t.reduce((i,u)=>i*u,1),o=T(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=q){let n=t.reduce((i,u)=>i*u,1),o=T(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return I(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 R(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 fr(r,t){let e=r.length,n=t.length,o=Math.max(e,n),a=new Array(o);for(let i=0;i<o;i++){let u=i<o-e?1:r[i-(o-e)],s=i<o-n?1:t[i-(o-n)];if(u===s)a[i]=u;else if(u===1)a[i]=s;else if(s===1)a[i]=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 na(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let i=0;i<n;i++){let u=o-n+i,s=r[i],c=e[u];if(s===c)a[u]=t[i];else if(s===1)a[u]=0;else throw new Error("Invalid broadcast")}return a}function Nr(r,t){let e=na(r.shape,r.strides,t);return N.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function F(r,t,e,n){let o=fr(r.shape,t.shape),a=Nr(r,o),i=Nr(t,o),u=k(r.dtype,t.dtype),s=N.zeros(o,u),c=s.data,l=s.size;if(I(u)){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=I(r.dtype)||I(t.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 s}function K(r,t,e){let n=fr(r.shape,t.shape),o=Nr(r,n),a=Nr(t,n),i=n.reduce((c,l)=>c*l,1),u=new Uint8Array(i),s=I(r.dtype)||I(t.dtype);for(let c=0;c<i;c++){let l=o.iget(c),m=a.iget(c),f=Number(l),p=Number(m);u[c]=e(f,p)?1:0}return N.fromData(u,n,"bool")}function C(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=N.zeros(o,u),c=s.data,l=r.data;if(I(n))if(I(u)){let m=c;for(let f=0;f<a;f++){let p=Number(l[f]);m[f]=BigInt(Math.round(t(p)))}}else for(let m=0;m<a;m++)c[m]=t(Number(l[m]));else for(let m=0;m<a;m++)c[m]=t(Number(l[m]));return s}function Rr(r,t){return r.isCContiguous&&t.isCContiguous&&r.shape.length===t.shape.length&&r.shape.every((e,n)=>e===t.shape[n])}function Qt(r,t){return typeof t=="number"?ia(r,t):Rr(r,t)?oa(r,t):F(r,t,(e,n)=>e+n,"add")}function oa(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;if(!I(r.dtype)||!I(t.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])));s[l]=m+f}else{let l=a,m=i;for(let f=0;f<o;f++)s[f]=l[f]+m[f]}}else if(I(r.dtype)||I(t.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];u[c]=l+m}else for(let c=0;c<o;c++)u[c]=a[c]+i[c];return n}function Xt(r,t){return typeof t=="number"?ua(r,t):Rr(r,t)?aa(r,t):F(r,t,(e,n)=>e-n,"subtract")}function aa(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;if(!I(r.dtype)||!I(t.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])));s[l]=m-f}else{let l=a,m=i;for(let f=0;f<o;f++)s[f]=l[f]-m[f]}}else if(I(r.dtype)||I(t.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];u[c]=l-m}else for(let c=0;c<o;c++)u[c]=a[c]-i[c];return n}function Kt(r,t){return typeof t=="number"?ca(r,t):Rr(r,t)?sa(r,t):F(r,t,(e,n)=>e*n,"multiply")}function sa(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;if(!I(r.dtype)||!I(t.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])));s[l]=m*f}else{let l=a,m=i;for(let f=0;f<o;f++)s[f]=l[f]*m[f]}}else if(I(r.dtype)||I(t.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];u[c]=l*m}else for(let c=0;c<o;c++)u[c]=a[c]*i[c];return n}function re(r,t){if(typeof t=="number")return la(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:or(r,"float64"),c=n?t:or(t,"float64");return F(s,c,(l,m)=>l/m,"divide")}if(o||a){let s=o?r:or(r,"float32"),c=a?t:or(t,"float32");return F(s,c,(l,m)=>l/m,"divide")}let i=or(r,"float64"),u=or(t,"float64");return F(i,u,(s,c)=>s/c,"divide")}function or(r,t){let e=N.zeros(Array.from(r.shape),t),n=r.size,o=r.data,a=e.data;for(let i=0;i<n;i++)a[i]=Number(o[i]);return e}function ia(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]+l}else for(let s=0;s<a;s++)u[s]=Number(o[s])+t;return i}function ua(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]-l}else for(let s=0;s<a;s++)u[s]=Number(o[s])-t;return i}function ca(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]*l}else for(let s=0;s<a;s++)u[s]=Number(o[s])*t;return i}function la(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=N.zeros(n,u),c=s.data;if(I(e))for(let l=0;l<a;l++)c[l]=Number(o[l])/t;else for(let l=0;l<a;l++)c[l]=Number(o[l])/t;return s}function te(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=N.zeros(e,t),i=a.data;if(I(t)){let u=n,s=i;for(let c=0;c<o;c++){let l=u[c];s[c]=l<0n?-l:l}}else for(let u=0;u<o;u++)i[u]=Math.abs(Number(n[u]));return a}function ee(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=N.zeros(e,t),i=a.data;if(I(t)){let u=n,s=i;for(let c=0;c<o;c++)s[c]=-u[c]}else for(let u=0;u<o;u++)i[u]=-Number(n[u]);return a}function ne(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=N.zeros(e,t),i=a.data;if(I(t)){let u=n,s=i;for(let c=0;c<o;c++){let l=u[c];s[c]=l>0n?1n:l<0n?-1n:0n}}else for(let u=0;u<o;u++){let s=Number(n[u]);i[u]=s>0?1:s<0?-1:0}return a}function Dr(r,t){return typeof t=="number"?fa(r,t):F(r,t,(e,n)=>(e%n+n)%n,"mod")}function fa(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++){let f=s[m];c[m]=(f%l+l)%l}}else for(let s=0;s<a;s++){let c=Number(o[s]);u[s]=(c%t+t)%t}return i}function Lr(r,t){return typeof t=="number"?ma(r,t):F(r,t,(e,n)=>Math.floor(e/n),"floor_divide")}function ma(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]/l}else for(let s=0;s<a;s++)u[s]=Math.floor(Number(o[s])/t);return i}function oe(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=N.zeros(e,t),i=a.data;for(let u=0;u<o;u++)i[u]=n[u];return a}function ae(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,i=t!=="float32"&&t!=="float64"?"float64":t,u=N.zeros(e,i),s=u.data;if(I(t))for(let c=0;c<o;c++)s[c]=1/Number(n[c]);else for(let c=0;c<o;c++)s[c]=1/Number(n[c]);return u}function se(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,i=t!=="float32"&&t!=="float64"?"float64":t,u=N.zeros(e,i),s=u.data;for(let c=0;c<o;c++)s[c]=Math.cbrt(Number(n[c]));return u}function ie(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",i=N.zeros(e,a),u=i.data;for(let s=0;s<o;s++)u[s]=Math.abs(Number(n[s]));return i}function ue(r,t){let e=Lr(r,t),n=Dr(r,t);return[e,n]}function ce(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=N.zeros(e,t),i=a.data;if(I(t)){let u=n,s=i;for(let c=0;c<o;c++)s[c]=u[c]*u[c]}else for(let u=0;u<o;u++){let s=Number(n[u]);i[u]=s*s}return a}function le(r,t){return Dr(r,t)}function fe(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.size,a=e==="float32"?"float32":"float64",i=N.zeros(n,a),u=i.data;if(typeof t=="number")for(let s=0;s<o;s++){let c=Number(r.data[s]);c<0?u[s]=0:c===0?u[s]=t:u[s]=1}else{let s=t.data,c=t.shape;if(n.every((l,m)=>l===c[m]))for(let l=0;l<o;l++){let m=Number(r.data[l]);m<0?u[l]=0:m===0?u[l]=Number(s[l]):u[l]=1}else for(let l=0;l<o;l++){let m=Number(r.data[l]),f=l%t.size;m<0?u[l]=0:m===0?u[l]=Number(s[f]):u[l]=1}}return i}function rr(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 i=a.length-t+n,u=i<0?1:a[i];if(u!==1){if(o===1)o=u;else if(o!==u)return null}}e[n]=o}return e}function ya(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let i=0;i<n;i++){let u=o-n+i,s=r[i],c=e[u];if(s===c)a[u]=t[i];else if(s===1)a[u]=0;else throw new Error("Invalid broadcast")}return a}function Y(r,t){let e=ya(r.shape,r.strides,t);return N.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function Zr(...r){let t=rr(r);if(t===null){let e=r.map(n=>`(${n.join(",")})`).join(" ");throw new Error(`shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between ${e}`)}return t}function me(r,t){return typeof t=="number"?da(r,t):K(r,t,(e,n)=>e>n)}function pe(r,t){return typeof t=="number"?ga(r,t):K(r,t,(e,n)=>e>=n)}function ye(r,t){return typeof t=="number"?ha(r,t):K(r,t,(e,n)=>e<n)}function de(r,t){return typeof t=="number"?Aa(r,t):K(r,t,(e,n)=>e<=n)}function ge(r,t){return typeof t=="number"?ba(r,t):K(r,t,(e,n)=>e===n)}function he(r,t){return typeof t=="number"?Sa(r,t):K(r,t,(e,n)=>e!==n)}function Wr(r,t,e=1e-5,n=1e-8){return typeof t=="number"?Na(r,t,e,n):K(r,t,(o,a)=>{let i=Math.abs(o-a),u=n+e*Math.abs(a);return i<=u})}function Ae(r,t,e=1e-5,n=1e-8){let o=Wr(r,t,e,n),a=o.data;for(let i=0;i<o.size;i++)if(a[i]===0)return!1;return!0}function be(r,t){let e=[Array.from(r.shape),Array.from(t.shape)],n=rr(e);if(n===null)return!1;let o=Y(r,n),a=Y(t,n),i=n.length,u=n.reduce((l,m)=>l*m,1),s=I(o.dtype),c=I(a.dtype);for(let l=0;l<u;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(s||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 da(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 N.fromData(e,Array.from(r.shape),"bool")}function ga(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 N.fromData(e,Array.from(r.shape),"bool")}function ha(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 N.fromData(e,Array.from(r.shape),"bool")}function Aa(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 N.fromData(e,Array.from(r.shape),"bool")}function ba(r,t){let e=new Uint8Array(r.size),n=r.data,o=r.dtype;if(I(o)){let a=BigInt(Math.round(t)),i=n;for(let u=0;u<r.size;u++)e[u]=i[u]===a?1:0}else for(let a=0;a<r.size;a++)e[a]=n[a]===t?1:0;return N.fromData(e,Array.from(r.shape),"bool")}function Sa(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 N.fromData(e,Array.from(r.shape),"bool")}function Na(r,t,e,n){let o=new Uint8Array(r.size),a=r.data,i=r.dtype;if(I(i)){let u=a;for(let s=0;s<r.size;s++){let c=Number(u[s]),l=Math.abs(c-t),m=n+e*Math.abs(t);o[s]=l<=m?1:0}}else for(let u=0;u<r.size;u++){let s=Number(a[u]),c=Math.abs(s-t),l=n+e*Math.abs(t);o[u]=c<=l?1:0}return N.fromData(o,Array.from(r.shape),"bool")}function O(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 U(r,t,e,n){let o=n.length,a=new Array(o),i=Array.from(n).filter((s,c)=>c!==t),u=r;for(let s=i.length-1;s>=0;s--)a[s>=t?s+1:s]=u%i[s],u=Math.floor(u/i[s]);return a[t]=e,a}function mr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,i=r.size,u=r.data;if(t===void 0)if(I(n)){let y=u,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(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 c=Array.from(o).filter((y,d)=>d!==s);if(c.length===0)return mr(r);let l=N.zeros(c,n),m=l.data,f=o[s],p=c.reduce((y,d)=>y*d,1);if(I(n)){let y=u,d=m;for(let g=0;g<p;g++){let b=BigInt(0);for(let A=0;A<f;A++){let h=U(g,s,A,o),S=O(h,o);b+=y[S]}d[g]=b}}else for(let y=0;y<p;y++){let d=0;for(let g=0;g<f;g++){let b=U(y,s,g,o),A=O(b,o);d+=Number(u[A])}m[y]=d}if(e){let y=[...o];return y[s]=1,N.fromData(m,y,n)}return l}function xr(r,t,e=!1){let n=r.dtype,o=r.shape;if(t===void 0)return mr(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 i=mr(r,t,e);if(typeof i=="number")return i/o[a];let u=o[a],s=n;(I(n)||n.startsWith("int")||n.startsWith("uint"))&&(s="float64");let c=N.zeros(Array.from(i.shape),s),l=c.data,m=i.data;if(I(n)){let f=m;for(let p=0;p<l.length;p++)l[p]=Number(f[p])/u}else for(let f=0;f<l.length;f++)l[f]=Number(m[f])/u;return c}function wr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,i=r.size,u=r.data;if(t===void 0){if(i===0)throw new Error("max of empty array");let y=u[0];for(let d=1;d<i;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 c=Array.from(o).filter((y,d)=>d!==s);if(c.length===0)return wr(r);let l=N.zeros(c,n),m=l.data,f=o[s],p=c.reduce((y,d)=>y*d,1);if(I(n)){let y=u,d=m;for(let g=0;g<p;g++){let b=U(g,s,0,o),A=O(b,o),h=y[A];for(let S=1;S<f;S++){let D=U(g,s,S,o),x=O(D,o),w=y[x];w>h&&(h=w)}d[g]=h}}else for(let y=0;y<p;y++){let d=-1/0;for(let g=0;g<f;g++){let b=U(y,s,g,o),A=O(b,o),h=Number(u[A]);h>d&&(d=h)}m[y]=d}if(e){let y=[...o];return y[s]=1,N.fromData(m,y,n)}return l}function Gr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,i=r.size,u=r.data;if(t===void 0)if(I(n)){let y=u,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(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 c=Array.from(o).filter((y,d)=>d!==s);if(c.length===0)return Gr(r);let l=N.zeros(c,n),m=l.data,f=o[s],p=c.reduce((y,d)=>y*d,1);if(I(n)){let y=u,d=m;for(let g=0;g<p;g++){let b=BigInt(1);for(let A=0;A<f;A++){let h=U(g,s,A,o),S=O(h,o);b*=y[S]}d[g]=b}}else for(let y=0;y<p;y++){let d=1;for(let g=0;g<f;g++){let b=U(y,s,g,o),A=O(b,o);d*=Number(u[A])}m[y]=d}if(e){let y=[...o];return y[s]=1,N.fromData(m,y,n)}return l}function Ir(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,i=r.size,u=r.data;if(t===void 0){if(i===0)throw new Error("min of empty array");let y=u[0];for(let d=1;d<i;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 c=Array.from(o).filter((y,d)=>d!==s);if(c.length===0)return Ir(r);let l=N.zeros(c,n),m=l.data,f=o[s],p=c.reduce((y,d)=>y*d,1);if(I(n)){let y=u,d=m;for(let g=0;g<p;g++){let b=U(g,s,0,o),A=O(b,o),h=y[A];for(let S=1;S<f;S++){let D=U(g,s,S,o),x=O(D,o),w=y[x];w<h&&(h=w)}d[g]=h}}else for(let y=0;y<p;y++){let d=1/0;for(let g=0;g<f;g++){let b=U(y,s,g,o),A=O(b,o),h=Number(u[A]);h<d&&(d=h)}m[y]=d}if(e){let y=[...o];return y[s]=1,N.fromData(m,y,n)}return l}function Hr(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,i=r.data;if(t===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 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((p,y)=>y!==u);if(s.length===0)return Hr(r);let c=N.zeros(s,"int32"),l=c.data,m=n[u],f=s.reduce((p,y)=>p*y,1);if(I(e)){let p=i;for(let y=0;y<f;y++){let d=U(y,u,0,n),g=O(d,n),b=p[g],A=0;for(let h=1;h<m;h++){let S=U(y,u,h,n),D=O(S,n),x=p[D];x<b&&(b=x,A=h)}l[y]=A}}else for(let p=0;p<f;p++){let y=1/0,d=0;for(let g=0;g<m;g++){let b=U(p,u,g,n),A=O(b,n),h=Number(i[A]);h<y&&(y=h,d=g)}l[p]=d}return c}function Yr(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,i=r.data;if(t===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 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((p,y)=>y!==u);if(s.length===0)return Yr(r);let c=N.zeros(s,"int32"),l=c.data,m=n[u],f=s.reduce((p,y)=>p*y,1);if(I(e)){let p=i;for(let y=0;y<f;y++){let d=U(y,u,0,n),g=O(d,n),b=p[g],A=0;for(let h=1;h<m;h++){let S=U(y,u,h,n),D=O(S,n),x=p[D];x>b&&(b=x,A=h)}l[y]=A}}else for(let p=0;p<f;p++){let y=-1/0,d=0;for(let g=0;g<m;g++){let b=U(p,u,g,n),A=O(b,n),h=Number(i[A]);h>y&&(y=h,d=g)}l[p]=d}return c}function Jr(r,t,e=0,n=!1){let o=r.shape,a=o.length,i=r.size,u=r.data,s=xr(r,t,n);if(t===void 0){let b=s,A=0;for(let h=0;h<i;h++){let S=Number(u[h])-b;A+=S*S}return A/(i-e)}let c=t;if(c<0&&(c=a+c),c<0||c>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let l=o[c],m=s,f=m.data,p=n?m.shape:Array.from(o).filter((b,A)=>A!==c),y=N.zeros(Array.from(p),"float64"),d=y.data,g=p.reduce((b,A)=>b*A,1);for(let b=0;b<g;b++){let A=0,h=Number(f[b]);for(let S=0;S<l;S++){let D=U(b,c,S,o),x=O(D,o),w=Number(u[x])-h;A+=w*w}d[b]=A/(l-e)}return y}function Se(r,t,e=0,n=!1){let o=Jr(r,t,e,n);if(typeof o=="number")return Math.sqrt(o);let a=N.zeros(Array.from(o.shape),"float64"),i=o.data,u=a.data;for(let s=0;s<i.length;s++)u[s]=Math.sqrt(Number(i[s]));return a}function Qr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,i=r.data;if(t===void 0){for(let p=0;p<a;p++)if(!i[p])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((p,y)=>y!==u);if(s.length===0)return Qr(r);let c=N.zeros(s,"bool"),l=c.data,m=n[u],f=s.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=U(p,u,d,n),b=O(g,n);if(!i[b]){y=!1;break}}l[p]=y?1:0}if(e){let p=[...n];return p[u]=1,N.fromData(l,p,"bool")}return c}function Xr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,i=r.data;if(t===void 0){for(let p=0;p<a;p++)if(i[p])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((p,y)=>y!==u);if(s.length===0)return Xr(r);let c=N.zeros(s,"bool"),l=c.data,m=n[u],f=s.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=U(p,u,d,n),b=O(g,n);if(i[b]){y=!0;break}}l[p]=y?1:0}if(e){let p=[...n];return p[u]=1,N.fromData(l,p,"bool")}return c}function Kr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.size,p=new Float64Array(f),y=0;for(let d=0;d<f;d++)y+=Number(o[d]),p[d]=y;return N.fromData(p,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=new Float64Array(r.size),u=e[a],s=[],c=1;for(let f=n-1;f>=0;f--)s.unshift(c),c*=e[f];let l=r.size,m=s[a];for(let f=0;f<l;f++)Math.floor(f/m)%u===0?i[f]=Number(o[f]):i[f]=i[f-m]+Number(o[f]);return N.fromData(i,[...e],"float64")}function rt(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.size,p=new Float64Array(f),y=1;for(let d=0;d<f;d++)y*=Number(o[d]),p[d]=y;return N.fromData(p,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=new Float64Array(r.size),u=e[a],s=[],c=1;for(let f=n-1;f>=0;f--)s.unshift(c),c*=e[f];let l=r.size,m=s[a];for(let f=0;f<l;f++)Math.floor(f/m)%u===0?i[f]=Number(o[f]):i[f]=i[f-m]*Number(o[f]);return N.fromData(i,[...e],"float64")}function tt(r,t,e=!1){let n=wr(r,t,e),o=Ir(r,t,e);if(typeof n=="number"&&typeof o=="number")return n-o;let a=n,i=o,u=a.data,s=i.data,c=new Float64Array(a.size);for(let l=0;l<a.size;l++)c[l]=Number(u[l])-Number(s[l]);return N.fromData(c,[...a.shape],"float64")}function et(r,t,e=!1){return ar(r,.5,t,e)}function nt(r,t,e,n=!1){return ar(r,t/100,e,n)}function ar(r,t,e,n=!1){if(t<0||t>1)throw new Error("Quantile must be between 0 and 1");let o=r.shape,a=o.length,i=r.data;if(e===void 0){let p=[];for(let h=0;h<r.size;h++)p.push(Number(i[h]));p.sort((h,S)=>h-S);let y=p.length,d=t*(y-1),g=Math.floor(d),b=Math.ceil(d);if(g===b)return p[g];let A=d-g;return p[g]*(1-A)+p[b]*A}let u=e;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${e} is out of bounds for array of dimension ${a}`);let s=Array.from(o).filter((p,y)=>y!==u);if(s.length===0)return ar(r,t);let c=s.reduce((p,y)=>p*y,1),l=o[u],m=new Float64Array(c);for(let p=0;p<c;p++){let y=[];for(let h=0;h<l;h++){let S=U(p,u,h,o),D=O(S,o);y.push(Number(i[D]))}y.sort((h,S)=>h-S);let d=y.length,g=t*(d-1),b=Math.floor(g),A=Math.ceil(g);if(b===A)m[p]=y[b];else{let h=g-b;m[p]=y[b]*(1-h)+y[A]*h}}let f=N.fromData(m,s,"float64");if(n){let p=[...o];return p[u]=1,N.fromData(m,p,"float64")}return f}function zr(r,t,e,n=!1){let o=r.shape,a=o.length,i=r.data;if(e===void 0)return xr(r,t,n);if(t===void 0){let y=0,d=0,g=e.data;for(let b=0;b<r.size;b++){let A=Number(g[b%e.size]);y+=Number(i[b])*A,d+=A}return d===0?NaN:y/d}let u=t;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let s=Array.from(o).filter((y,d)=>d!==u);if(s.length===0)return zr(r,void 0,e);let c=s.reduce((y,d)=>y*d,1),l=o[u],m=e.data,f=new Float64Array(c);for(let y=0;y<c;y++){let d=0,g=0;for(let b=0;b<l;b++){let A=U(y,u,b,o),h=O(A,o),S=Number(m[b%e.size]);d+=Number(i[h])*S,g+=S}f[y]=g===0?NaN:d/g}let p=N.fromData(f,s,"float64");if(n){let y=[...o];return y[u]=1,N.fromData(f,y,"float64")}return p}function _r(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=0;for(let p=0;p<r.size;p++){let y=Number(a[p]);isNaN(y)||(f+=y)}return f}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((f,p)=>p!==i);if(u.length===0)return _r(r);let s=u.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(s);for(let f=0;f<s;f++){let p=0;for(let y=0;y<c;y++){let d=U(f,i,y,n),g=O(d,n),b=Number(a[g]);isNaN(b)||(p+=b)}l[f]=p}let m=N.fromData(l,u,"float64");if(e){let f=[...n];return f[i]=1,N.fromData(l,f,"float64")}return m}function Br(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=1;for(let p=0;p<r.size;p++){let y=Number(a[p]);isNaN(y)||(f*=y)}return f}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((f,p)=>p!==i);if(u.length===0)return Br(r);let s=u.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(s);for(let f=0;f<s;f++){let p=1;for(let y=0;y<c;y++){let d=U(f,i,y,n),g=O(d,n),b=Number(a[g]);isNaN(b)||(p*=b)}l[f]=p}let m=N.fromData(l,u,"float64");if(e){let f=[...n];return f[i]=1,N.fromData(l,f,"float64")}return m}function Er(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=0,p=0;for(let y=0;y<r.size;y++){let d=Number(a[y]);isNaN(d)||(f+=d,p++)}return p===0?NaN:f/p}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((f,p)=>p!==i);if(u.length===0)return Er(r);let s=u.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(s);for(let f=0;f<s;f++){let p=0,y=0;for(let d=0;d<c;d++){let g=U(f,i,d,n),b=O(g,n),A=Number(a[b]);isNaN(A)||(p+=A,y++)}l[f]=y===0?NaN:p/y}let m=N.fromData(l,u,"float64");if(e){let f=[...n];return f[i]=1,N.fromData(l,f,"float64")}return m}function pr(r,t,e=0,n=!1){let o=r.shape,a=o.length,i=r.data;if(t===void 0){let p=0,y=0;for(let b=0;b<r.size;b++){let A=Number(i[b]);isNaN(A)||(p+=A,y++)}if(y-e<=0)return NaN;let d=p/y,g=0;for(let b=0;b<r.size;b++){let A=Number(i[b]);isNaN(A)||(g+=(A-d)**2)}return g/(y-e)}let u=t;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let s=Array.from(o).filter((p,y)=>y!==u);if(s.length===0)return pr(r,void 0,e);let c=s.reduce((p,y)=>p*y,1),l=o[u],m=new Float64Array(c);for(let p=0;p<c;p++){let y=0,d=0;for(let A=0;A<l;A++){let h=U(p,u,A,o),S=O(h,o),D=Number(i[S]);isNaN(D)||(y+=D,d++)}if(d-e<=0){m[p]=NaN;continue}let g=y/d,b=0;for(let A=0;A<l;A++){let h=U(p,u,A,o),S=O(h,o),D=Number(i[S]);isNaN(D)||(b+=(D-g)**2)}m[p]=b/(d-e)}let f=N.fromData(m,s,"float64");if(n){let p=[...o];return p[u]=1,N.fromData(m,p,"float64")}return f}function ot(r,t,e=0,n=!1){let o=pr(r,t,e,n);if(typeof o=="number")return Math.sqrt(o);let a=o,i=new Float64Array(a.size);for(let u=0;u<a.size;u++)i[u]=Math.sqrt(Number(a.data[u]));return N.fromData(i,[...a.shape],"float64")}function vr(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=1/0;for(let p=0;p<r.size;p++){let y=Number(a[p]);!isNaN(y)&&y<f&&(f=y)}return f===1/0?NaN:f}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((f,p)=>p!==i);if(u.length===0)return vr(r);let s=u.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(s);for(let f=0;f<s;f++){let p=1/0;for(let y=0;y<c;y++){let d=U(f,i,y,n),g=O(d,n),b=Number(a[g]);!isNaN(b)&&b<p&&(p=b)}l[f]=p===1/0?NaN:p}let m=N.fromData(l,u,"float64");if(e){let f=[...n];return f[i]=1,N.fromData(l,f,"float64")}return m}function Or(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=-1/0;for(let p=0;p<r.size;p++){let y=Number(a[p]);!isNaN(y)&&y>f&&(f=y)}return f===-1/0?NaN:f}let i=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((f,p)=>p!==i);if(u.length===0)return Or(r);let s=u.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(s);for(let f=0;f<s;f++){let p=-1/0;for(let y=0;y<c;y++){let d=U(f,i,y,n),g=O(d,n),b=Number(a[g]);!isNaN(b)&&b>p&&(p=b)}l[f]=p===-1/0?NaN:p}let m=N.fromData(l,u,"float64");if(e){let f=[...n];return f[i]=1,N.fromData(l,f,"float64")}return m}function Ur(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let l=1/0,m=-1;for(let f=0;f<r.size;f++){let p=Number(o[f]);!isNaN(p)&&p<l&&(l=p,m=f)}return m}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=Array.from(e).filter((l,m)=>m!==a);if(i.length===0)return Ur(r);let u=i.reduce((l,m)=>l*m,1),s=e[a],c=new Int32Array(u);for(let l=0;l<u;l++){let m=1/0,f=0;for(let p=0;p<s;p++){let y=U(l,a,p,e),d=O(y,e),g=Number(o[d]);!isNaN(g)&&g<m&&(m=g,f=p)}c[l]=f}return N.fromData(c,i,"int32")}function Tr(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let l=-1/0,m=-1;for(let f=0;f<r.size;f++){let p=Number(o[f]);!isNaN(p)&&p>l&&(l=p,m=f)}return m}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=Array.from(e).filter((l,m)=>m!==a);if(i.length===0)return Tr(r);let u=i.reduce((l,m)=>l*m,1),s=e[a],c=new Int32Array(u);for(let l=0;l<u;l++){let m=-1/0,f=0;for(let p=0;p<s;p++){let y=U(l,a,p,e),d=O(y,e),g=Number(o[d]);!isNaN(g)&&g>m&&(m=g,f=p)}c[l]=f}return N.fromData(c,i,"int32")}function at(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.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 N.fromData(p,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=new Float64Array(r.size),u=e[a],s=[],c=1;for(let f=n-1;f>=0;f--)s.unshift(c),c*=e[f];let l=r.size,m=s[a];for(let f=0;f<l;f++){let p=Number(o[f]);Math.floor(f/m)%u===0?i[f]=isNaN(p)?0:p:i[f]=i[f-m]+(isNaN(p)?0:p)}return N.fromData(i,[...e],"float64")}function st(r,t){let e=r.shape,n=e.length,o=r.data;if(t===void 0){let f=r.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 N.fromData(p,[f],"float64")}let a=t;if(a<0&&(a=n+a),a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let i=new Float64Array(r.size),u=e[a],s=[],c=1;for(let f=n-1;f>=0;f--)s.unshift(c),c*=e[f];let l=r.size,m=s[a];for(let f=0;f<l;f++){let p=Number(o[f]);Math.floor(f/m)%u===0?i[f]=isNaN(p)?1:p:i[f]=i[f-m]*(isNaN(p)?1:p)}return N.fromData(i,[...e],"float64")}function $r(r,t,e=!1){let n=r.shape,o=n.length,a=r.data;if(t===void 0){let f=[];for(let d=0;d<r.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=t;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let u=Array.from(n).filter((f,p)=>p!==i);if(u.length===0)return $r(r);let s=u.reduce((f,p)=>f*p,1),c=n[i],l=new Float64Array(s);for(let f=0;f<s;f++){let p=[];for(let g=0;g<c;g++){let b=U(f,i,g,n),A=O(b,n),h=Number(a[A]);isNaN(h)||p.push(h)}if(p.length===0){l[f]=NaN;continue}p.sort((g,b)=>g-b);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=N.fromData(l,u,"float64");if(e){let f=[...n];return f[i]=1,N.fromData(l,f,"float64")}return m}function P(r,t){let e=r.size,n=r.dtype,o=t.indexOf(-1),a;if(o!==-1){let c=t.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(t)}`);a=t.map((m,f)=>f===o?l:m)}else a=t;if(a.reduce((c,l)=>c*l,1)!==e)throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(a)}`);if(r.isCContiguous){let c=r.data;return N.fromData(c,a,n,R(a),0)}let s=r.copy().data;return N.fromData(s,a,n,R(a),0)}function Mr(r){let t=r.size,e=r.dtype,n=T(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 N.fromData(u,[t],e,[1],0)}let o=new n(t),a=I(e);for(let i=0;i<t;i++){let u=r.iget(i);o[i]=u}return N.fromData(o,[t],e,[1],0)}function sr(r){let t=r.size,e=r.dtype;if(r.isCContiguous){let n=r.data;return N.fromData(n,[t],e,[1],0)}return Mr(r)}function dr(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,i=r.dtype,u;if(t===void 0)u=Array.from({length:n},(m,f)=>n-1-f);else{if(t.length!==n)throw new Error(`axes must have length ${n}, got ${t.length}`);let m=new Set;for(let f of t){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)}u=t.map(f=>f<0?n+f:f)}let s=u.map(m=>e[m]),c=Array.from(o),l=u.map(m=>c[m]);return N.fromData(a,s,i,l,r.offset)}function Ne(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,i=r.dtype;if(t===void 0){let u=[],s=[];for(let c=0;c<n;c++)e[c]!==1&&(u.push(e[c]),s.push(o[c]));return u.length===0&&(u.push(1),s.push(1)),N.fromData(a,u,i,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=[],c=[];for(let l=0;l<n;l++)l!==u&&(s.push(e[l]),c.push(o[l]));return N.fromData(a,s,i,c,r.offset)}}function yr(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,i=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 c=[...Array.from(o)],l=u<n?o[u]*(e[u]||1):1;return c.splice(u,0,l),N.fromData(a,s,i,c,r.offset)}function De(r,t,e){let n=r.shape,o=n.length,a=r.strides,i=r.data,u=r.dtype,s=t<0?o+t:t,c=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(c<0||c>=o)throw new Error(`axis2 ${e} is out of bounds for array of dimension ${o}`);if(s===c)return N.fromData(i,Array.from(n),u,Array.from(a),r.offset);let l=Array.from(n),m=Array.from(a);return[l[s],l[c]]=[l[c],l[s]],[m[s],m[c]]=[m[c],m[s]],N.fromData(i,l,u,m,r.offset)}function it(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 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}),u=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(u).size!==u.length)throw new Error("repeated axis in destination");let s=[];for(let c=0;c<n;c++)i.includes(c)||s.push(c);for(let c=0;c<i.length;c++){let l=u[c];s.splice(l,0,i[c])}return dr(r,s)}function er(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 p=1;p<r.length;p++){let y=r[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),u=e.shape[a];for(let p=1;p<r.length;p++)u+=r[p].shape[a];i[a]=u;let s=i.reduce((p,y)=>p*y,1),c=T(o);if(!c)throw new Error(`Cannot concatenate arrays with dtype ${o}`);let l=new c(s),m=R(i),f=0;for(let p of r){let y=p.shape[a];wa(p,l,i,m,a,f,o),f+=y}return N.fromData(l,i,o)}function wa(r,t,e,n,o,a,i){let u=r.shape,s=u.length,c=r.size,l=i==="int64"||i==="uint64";if(o===0&&r.isCContiguous&&s>0){let p=a*n[0],y=r.data,d=r.offset,g=d+c;t.set(y.subarray(d,g),p);return}if(o===1&&s===2&&r.isCContiguous){let p=u[0],y=u[1],d=e[1],g=r.data,b=r.offset;for(let A=0;A<p;A++){let h=b+A*y,S=A*d+a;t.set(g.subarray(h,h+y),S)}return}let m=new Array(s).fill(0),f=a*n[o];for(let p=0;p<c;p++){let y=r.iget(p),d=f;for(let g=0;g<s;g++)d+=m[g]*n[g];t[d]=y;for(let g=s-1;g>=0&&(m[g]++,!(m[g]<u[g]));g--)m[g]=0}}function xe(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 c=0;c<o;c++)if(s.shape[c]!==n[c])throw new Error("all input arrays must have the same shape")}let i=r.map(u=>yr(u,a));return er(i,a)}function we(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?P(e,[1,e.shape[0]]):e);return er(t,0)}function ut(r){if(r.length===0)throw new Error("need at least one array to stack");return r.every(e=>e.ndim===1)?er(r,0):er(r,1)}function Ie(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?P(yr(P(e,[1,e.shape[0]]),2),[1,e.shape[0],1]):e.ndim===2?yr(e,2):e);return er(t,2)}function ze(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 i=n[a],u;if(typeof t=="number"){if(i%t!==0)throw new Error("array split does not result in an equal division");let s=i/t;u=[];for(let c=1;c<t;c++)u.push(c*s)}else u=t;return _e(r,u,a)}function gr(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 i=n[a],u;if(typeof t=="number"){let s=t,c=Math.floor(i/s),l=i%s;u=[];let m=0;for(let f=0;f<s-1;f++)m+=c+(f<l?1:0),u.push(m)}else u=t;return _e(r,u,a)}function _e(r,t,e){let n=r.shape,o=n[e],a=[0,...t,o],i=[];for(let u=0;u<a.length-1;u++){let s=a[u],c=a[u+1];if(s>c)throw new Error("split indices must be in ascending order");let l=Array.from(n);l[e]=c-s;let m=r.offset+s*r.strides[e];i.push(N.fromData(r.data,l,r.dtype,Array.from(r.strides),m))}return i}function Be(r,t){if(r.ndim<2)throw new Error("vsplit only works on arrays of 2 or more dimensions");return gr(r,t,0)}function Ee(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 gr(r,t,e)}function ve(r,t){let e=r.shape,n=e.length,o=r.dtype,a=Array.isArray(t)?t:[t],i=Math.max(n,a.length),u=new Array(i).fill(1),s=new Array(i).fill(1);for(let A=0;A<n;A++)u[i-n+A]=e[A];for(let A=0;A<a.length;A++)s[i-a.length+A]=a[A];let c=u.map((A,h)=>A*s[h]),l=c.reduce((A,h)=>A*h,1),m=T(o);if(!m)throw new Error(`Cannot tile array with dtype ${o}`);let f=new m(l),p=R(c),y=r;n<i&&(y=P(r,u));let d=o==="int64"||o==="uint64",g=y.strides,b=new Array(i).fill(0);for(let A=0;A<l;A++){let h=y.offset;for(let x=0;x<i;x++){let w=b[x]%u[x];h+=w*g[x]}let S=y.data[h],D=0;for(let x=0;x<i;x++)D+=b[x]*p[x];f[D]=S;for(let x=i-1;x>=0&&(b[x]++,!(b[x]<c[x]));x--)b[x]=0}return N.fromData(f,c,o)}function Oe(r,t,e){let n=r.shape,o=n.length,a=r.dtype,i=r.size;if(e===void 0){let A=i,h=Array.isArray(t)?t:new Array(A).fill(t);if(h.length!==A)throw new Error(`operands could not be broadcast together with shape (${A},) (${h.length},)`);let S=h.reduce((_,B)=>_+B,0),D=T(a);if(!D)throw new Error(`Cannot repeat array with dtype ${a}`);let x=new D(S),w=0;for(let _=0;_<A;_++){let B=r.iget(_),E=h[_];for(let v=0;v<E;v++)x[w++]=B}return N.fromData(x,[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],c=Array.isArray(t)?t:new Array(s).fill(t);if(c.length!==s)throw new Error(`operands could not be broadcast together with shape (${s},) (${c.length},)`);let l=Array.from(n);l[u]=c.reduce((A,h)=>A+h,0);let m=l.reduce((A,h)=>A*h,1),f=T(a);if(!f)throw new Error(`Cannot repeat array with dtype ${a}`);let p=new f(m),y=R(l),d=new Array(o).fill(0),g=a==="int64"||a==="uint64",b=[0];for(let A=0;A<s;A++)b.push(b[A]+c[A]);for(let A=0;A<i;A++){let h=r.iget(A),S=d[u],D=c[S],x=0;for(let B=0;B<o;B++)B!==u&&(x+=d[B]*y[B]);let w=y[u],_=b[S];for(let B=0;B<D;B++){let E=x+(_+B)*w;p[E]=h}for(let B=o-1;B>=0&&(d[B]++,!(d[B]<n[B]));B--)d[B]=0}return N.fromData(p,l,a)}function Ue(r,t){let e=r.shape,n=e.length,o=r.dtype,a=r.size,i;if(t===void 0)i=new Set(Array.from({length:n},(f,p)=>p));else if(typeof t=="number"){let f=t<0?n+t:t;if(f<0||f>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);i=new Set([f])}else i=new Set(t.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 u=T(o);if(!u)throw new Error(`Cannot flip array with dtype ${o}`);let s=new u(a),c=I(o);if(n===1&&r.isCContiguous){let f=r.data,p=r.offset;for(let y=0;y<a;y++)s[y]=f[p+a-1-y];return N.fromData(s,[...e],o)}if(n===2&&r.isCContiguous){let f=e[0],p=e[1],y=r.data,d=r.offset;if(i.size===2){for(let g=0;g<a;g++)s[g]=y[d+a-1-g];return N.fromData(s,[...e],o)}if(i.size===1){if(i.has(0)){for(let g=0;g<f;g++){let b=d+(f-1-g)*p,A=g*p;for(let h=0;h<p;h++)s[A+h]=y[b+h]}return N.fromData(s,[...e],o)}else if(i.has(1)){for(let g=0;g<f;g++){let b=d+g*p,A=g*p;for(let h=0;h<p;h++)s[A+h]=y[b+p-1-h]}return N.fromData(s,[...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=r.offset;for(let d=0;d<n;d++)p+=l[d]*r.strides[d];let y=r.data[p];s[f]=y;for(let d=n-1;d>=0&&(m[d]++,!(m[d]<e[d]));d--)m[d]=0}return N.fromData(s,[...e],o)}function Te(r,t=1,e=[0,1]){let n=r.shape,o=n.length,a=r.dtype;if(o<2)throw new Error("Input must be at least 2-D");let i=e[0]<0?o+e[0]:e[0],u=e[1]<0?o+e[1]:e[1];if(i<0||i>=o||u<0||u>=o)throw new Error(`Axes are out of bounds for array of dimension ${o}`);if(i===u)throw new Error("Axes must be different");if(t=(t%4+4)%4,t===0)return r.copy();let s=T(a);if(!s)throw new Error(`Cannot rotate array with dtype ${a}`);let c=[...n];(t===1||t===3)&&([c[i],c[u]]=[c[u],c[i]]);let l=c.reduce((g,b)=>g*b,1),m=new s(l),f=R(c),p=I(a),y=new Array(o).fill(0),d=new Array(o);for(let g=0;g<r.size;g++){for(let D=0;D<o;D++)d[D]=y[D];let b,A;t===1?(b=n[u]-1-y[u],A=y[i]):t===2?(b=n[i]-1-y[i],A=n[u]-1-y[u],d[i]=b,d[u]=A):(b=y[u],A=n[i]-1-y[i]),t!==2&&(d[i]=b,d[u]=A);let h=0;for(let D=0;D<o;D++)h+=d[D]*f[D];let S=r.iget(g);m[h]=S;for(let D=o-1;D>=0&&(y[D]++,!(y[D]<n[D]));D--)y[D]=0}return N.fromData(m,c,a)}function $e(r,t,e){let n=r.shape,o=n.length,a=r.dtype,i=r.size;if(e===void 0){let y=Array.isArray(t)?t.reduce((h,S)=>h+S,0):t,d=Mr(r),g=T(a);if(!g)throw new Error(`Cannot roll array with dtype ${a}`);let b=new g(i),A=I(a);for(let h=0;h<i;h++){let S=((h-y)%i+i)%i,D=d.iget(S);b[h]=D}return N.fromData(b,[...n],a)}let u=Array.isArray(t)?t:[t],s=Array.isArray(e)?e:[e];if(u.length!==s.length)throw new Error("shift and axis must have the same length");let c=s.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=T(a);if(!l)throw new Error(`Cannot roll array with dtype ${a}`);let m=new l(i),f=I(a),p=new Array(o).fill(0);for(let y=0;y<i;y++){let d=[...p];for(let A=0;A<c.length;A++){let h=c[A],S=n[h],D=u[A];d[h]=((d[h]-D)%S+S)%S}let g=r.offset;for(let A=0;A<o;A++)g+=d[A]*r.strides[A];let b=r.data[g];m[y]=b;for(let A=o-1;A>=0&&(p[A]++,!(p[A]<n[A]));A--)p[A]=0}return N.fromData(m,[...n],a)}function Me(r,t,e=0){let n=r.ndim,o=t<0?n+t:t;if(o<0||o>=n)throw new Error(`axis ${t} 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?N.fromData(r.data,Array.from(r.shape),r.dtype,Array.from(r.strides),r.offset):it(r,o,a)}function ke(r,t){if(r.ndim<3)throw new Error("dsplit only works on arrays of 3 or more dimensions");return gr(r,t,2)}function Ce(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?P(e,[e.shape[0],1]):e);return ut(t)}function Ve(r,t){let e=r.dtype,n=t.reduce((s,c)=>s*c,1),o=r.size,a=T(e);if(!a)throw new Error(`Cannot resize array with dtype ${e}`);let i=new a(n),u=I(e);for(let s=0;s<n;s++){let c=s%o,l=r.iget(c);i[s]=l}return N.fromData(i,t,e)}function qe(r){return r.map(t=>t.ndim===0?P(t,[1]):t)}function Fe(r){return r.map(t=>t.ndim===0?P(t,[1,1]):t.ndim===1?P(t,[1,t.shape[0]]):t)}function Pe(r){return r.map(t=>t.ndim===0?P(t,[1,1,1]):t.ndim===1?P(t,[1,t.shape[0],1]):t.ndim===2?P(t,[t.shape[0],t.shape[1],1]):t)}function Ia(r,t,e,n,o,a,i,u,s,c,l,m,f,p){if(m===0)for(let b=0;b<n*o;b++)f[b]=0;else if(m!==1)for(let b=0;b<n*o;b++)f[b]=(f[b]??0)*m;let y=r==="row-major",d=t==="transpose",g=e==="transpose";if(y&&!d&&!g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[b*s+S]??0)*(c[S*l+A]??0);f[b*p+A]=(f[b*p+A]??0)+i*h}else if(y&&d&&!g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[S*s+b]??0)*(c[S*l+A]??0);f[b*p+A]=(f[b*p+A]??0)+i*h}else if(y&&!d&&g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[b*s+S]??0)*(c[A*l+S]??0);f[b*p+A]=(f[b*p+A]??0)+i*h}else if(y&&d&&g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[S*s+b]??0)*(c[A*l+S]??0);f[b*p+A]=(f[b*p+A]??0)+i*h}else if(!y&&!d&&!g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[S*s+b]??0)*(c[A*l+S]??0);f[A*p+b]=(f[A*p+b]??0)+i*h}else if(!y&&d&&!g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[b*s+S]??0)*(c[A*l+S]??0);f[A*p+b]=(f[A*p+b]??0)+i*h}else if(!y&&!d&&g)for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[S*s+b]??0)*(c[S*l+A]??0);f[A*p+b]=(f[A*p+b]??0)+i*h}else for(let b=0;b<n;b++)for(let A=0;A<o;A++){let h=0;for(let S=0;S<a;S++)h+=(u[b*s+S]??0)*(c[S*l+A]??0);f[A*p+b]=(f[A*p+b]??0)+i*h}}function lt(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 i=k(r.dtype,t.dtype),u=N.zeros([...t.shape],i);for(let s=0;s<t.size;s++){let c=t.data[s+t.offset];typeof o=="bigint"&&typeof c=="bigint"?u.data[s]=o*c:u.data[s]=Number(o)*Number(c)}return u}else{let i=k(r.dtype,t.dtype),u=N.zeros([...r.shape],i);for(let s=0;s<r.size;s++){let c=r.data[s+r.offset];typeof c=="bigint"&&typeof a=="bigint"?u.data[s]=c*a:u.data[s]=Number(c)*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 i=0;i<o;i++){let u=r.get(i),s=t.get(i);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 G(r,t);if(e===2&&n===1){let[o,a]=r.shape,i=t.shape[0];if(a!==i)throw new Error(`dot: incompatible shapes (${o},${a}) and (${i},)`);let u=k(r.dtype,t.dtype),s=N.zeros([o],u);for(let c=0;c<o;c++){let l=0;for(let m=0;m<a;m++){let f=r.get(c,m),p=t.get(m);typeof f=="bigint"&&typeof p=="bigint"?l=Number(l)+Number(f*p):l+=Number(f)*Number(p)}s.set([c],l)}return s}if(e===1&&n===2){let o=r.shape[0],[a,i]=t.shape;if(o!==a)throw new Error(`dot: incompatible shapes (${o},) and (${a},${i})`);let u=k(r.dtype,t.dtype),s=N.zeros([i],u);for(let c=0;c<i;c++){let l=0;for(let m=0;m<o;m++){let f=r.get(m),p=t.get(m,c);typeof f=="bigint"&&typeof p=="bigint"?l=Number(l)+Number(f*p):l+=Number(f)*Number(p)}s.set([c],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 i=[...r.shape.slice(0,-1)],u=k(r.dtype,t.dtype),s=N.zeros(i,u),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=r.get(...d),b=t.get(y);typeof g=="bigint"&&typeof b=="bigint"?m=Number(m)+Number(g*b):m+=Number(g)*Number(b)}s.set(p,m)}return s}if(e===1&&n>2){let o=r.shape[0],a=1,i=t.shape[a];if(o!==i)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=k(r.dtype,t.dtype),c=N.zeros(u,s),l=u.reduce((m,f)=>m*f,1);for(let m=0;m<l;m++){let f=m,p=[];for(let b=u.length-1;b>=0;b--)p[b]=f%u[b],f=Math.floor(f/u[b]);let y=p.slice(0,a),d=p.slice(a),g=0;for(let b=0;b<o;b++){let A=r.get(b),h=[...y,b,...d],S=t.get(...h);typeof A=="bigint"&&typeof S=="bigint"?g=Number(g)+Number(A*S):g+=Number(A)*Number(S)}c.set(p,g)}return c}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 i=[...r.shape.slice(0,-1),...t.shape.slice(0,-2),t.shape[n-1]],u=k(r.dtype,t.dtype),s=N.zeros(i,u),c=r.shape.slice(0,-1).reduce((p,y)=>p*y,1),l=t.shape.slice(0,-2).reduce((p,y)=>p*y,1),m=t.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 A=0;A<f;A++){let h=p*f+A,S=r.data[h+r.offset],D=y*f*m+A*m+d,x=t.data[D+t.offset];typeof S=="bigint"&&typeof x=="bigint"?g=Number(g)+Number(S*x):g+=Number(S)*Number(x)}let b=p*l*m+y*m+d;s.data[b]=g}return s}throw new Error(`dot: unexpected combination of dimensions ${e}D \xB7 ${n}D`)}function G(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 i=k(r.dtype,t.dtype),u=i.startsWith("int")||i.startsWith("uint")||i==="bool"?"float64":i;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)),c=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number));r.offset>0&&(s=s.subarray(r.offset)),t.offset>0&&(c=c.subarray(t.offset));let[l=0,m=0]=r.strides,[f=0,p=0]=t.strides,y=m>l,d=p>f,g=y?"transpose":"no-transpose",b=d?"transpose":"no-transpose",A,h;y?A=m:A=l,d?h=p:h=f;let S=N.zeros([e,a],"float64");return Ia("row-major",g,b,e,a,n,1,s,A,c,h,0,S.data,a),S}function Ze(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 i=r.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 Re(r,t){return dr(r,t)}function We(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 lt(r,t);let i=[...r.shape.slice(0,-1),...t.shape.slice(0,-1)],u=k(r.dtype,t.dtype),s=N.zeros(i,u),c=e===1?1:r.shape.slice(0,-1).reduce((f,p)=>f*p,1),l=n===1?1:t.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 b=e===1?g:f*m+g,A=n===1?g:p*m+g,h=r.data[b+r.offset],S=t.data[A+t.offset];typeof h=="bigint"&&typeof S=="bigint"?y=Number(y)+Number(h*S):y+=Number(h)*Number(S)}if(i.length===0)return y;let d=c===1?p:f*l+p;s.data[d]=y}return s}function ft(r,t){let e=r.ndim===1?r:sr(r),n=t.ndim===1?t:sr(t),o=e.size,a=n.size,i=k(r.dtype,t.dtype),u=N.zeros([o,a],i);for(let s=0;s<o;s++)for(let c=0;c<a;c++){let l=e.get(s),m=n.get(c),f;typeof l=="bigint"&&typeof m=="bigint"?f=l*m:f=Number(l)*Number(m),u.set([s,c],f)}return u}function Ge(r,t,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>r.ndim||f>t.ndim)throw new Error("tensordot: axes exceeds array dimensions");n=Array.from({length:f},(p,y)=>r.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>=r.ndim||y<0||y>=t.ndim)throw new Error("tensordot: axis out of bounds");if(r.shape[p]!==t.shape[y])throw new Error(`tensordot: shape mismatch on axes ${p} and ${y}: ${r.shape[p]} != ${t.shape[y]}`)}let a=[],i=[];for(let f=0;f<r.ndim;f++)n.includes(f)||a.push(f);for(let f=0;f<t.ndim;f++)o.includes(f)||i.push(f);let u=[...a.map(f=>r.shape[f]),...i.map(f=>t.shape[f])];if(u.length===0){let f=0,p=n.map(y=>r.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 D=n.length-1;D>=0;D--){let x=n[D];g[D]=d%r.shape[x],d=Math.floor(d/r.shape[x])}let b=new Array(r.ndim),A=new Array(t.ndim);for(let D=0;D<n.length;D++)b[n[D]]=g[D];for(let D=0;D<o.length;D++)A[o[D]]=g[D];let h=r.get(...b),S=t.get(...A);typeof h=="bigint"&&typeof S=="bigint"?f=Number(f)+Number(h*S):f+=Number(h)*Number(S)}return f}let s=k(r.dtype,t.dtype),c=N.zeros(u,s),l=u.reduce((f,p)=>f*p,1),m=n.map(f=>r.shape[f]).reduce((f,p)=>f*p,1);for(let f=0;f<l;f++){let p=f,y=[];for(let A=u.length-1;A>=0;A--)y[A]=p%u[A],p=Math.floor(p/u[A]);let d=y.slice(0,a.length),g=y.slice(a.length),b=0;for(let A=0;A<m;A++){p=A;let h=[];for(let _=n.length-1;_>=0;_--){let B=n[_];h[_]=p%r.shape[B],p=Math.floor(p/r.shape[B])}let S=new Array(r.ndim),D=new Array(t.ndim);for(let _=0;_<a.length;_++)S[a[_]]=d[_];for(let _=0;_<i.length;_++)D[i[_]]=g[_];for(let _=0;_<n.length;_++)S[n[_]]=h[_],D[o[_]]=h[_];let x=r.get(...S),w=t.get(...D);typeof x=="bigint"&&typeof w=="bigint"?b=Number(b)+Number(x*w):b+=Number(x)*Number(w)}c.set(y,b)}return c}function He(r,t=0,e=0,n=1){let o=r.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,u=n<0?a+n:n;if(i<0||i>=a||u<0||u>=a)throw new Error("axis out of bounds");if(i===u)throw new Error("axis1 and axis2 cannot be the same");let s=o[i],c=o[u],l;t>=0?l=Math.max(0,Math.min(s,c-t)):l=Math.max(0,Math.min(s+t,c));let m=[];for(let d=0;d<a;d++)d!==i&&d!==u&&m.push(o[d]);m.push(l);let f=N.zeros(m,r.dtype),p=o.filter((d,g)=>g!==i&&g!==u),y=p.reduce((d,g)=>d*g,1);for(let d=0;d<y;d++){let g=d,b=[];for(let A=p.length-1;A>=0;A--)b.unshift(g%p[A]),g=Math.floor(g/p[A]);for(let A=0;A<l;A++){let h=new Array(a),S=0;for(let w=0;w<a;w++)w===i?h[w]=t>=0?A:A-t:w===u?h[w]=t>=0?A+t:A:h[w]=b[S++];let D=[...b,A],x=r.get(...h);f.set(D,x)}}return f}function Ye(r,...t){let e=r.indexOf("->"),n,o;e===-1?(n=r,o=za(n)):(n=r.slice(0,e),o=r.slice(e+2));let a=n.split(",").map(d=>d.trim());if(a.length!==t.length)throw new Error(`einsum: expected ${a.length} operands, got ${t.length}`);let i=new Map;for(let d=0;d<t.length;d++){let g=a[d],b=t[d];if(g.length!==b.ndim)throw new Error(`einsum: operand ${d} has ${b.ndim} dimensions but subscript '${g}' has ${g.length} indices`);for(let A=0;A<g.length;A++){let h=g[A],S=b.shape[A];if(i.has(h)){if(i.get(h)!==S)throw new Error(`einsum: size mismatch for index '${h}': ${i.get(h)} vs ${S}`)}else i.set(h,S)}}for(let d of o)if(!i.has(d))throw new Error(`einsum: output subscript contains unknown index '${d}'`);let u=new Set(o),s=new Set;for(let d of a)for(let g of d)s.add(g);let c=[];for(let d of s)u.has(d)||c.push(d);if(t.length===2&&a.length===2){let[d,g]=a,[b,A]=t;if(d.length===2&&g.length===2&&o.length===2&&b.ndim===2&&A.ndim===2){let[h,S]=[d[0],d[1]],[D,x]=[g[0],g[1]],[w,_]=[o[0],o[1]];if(h===w&&x===_&&S===D&&c.length===1&&c[0]===S||h===w&&x===_&&S===D&&c.length===1&&c[0]===S)return G(b,A);if(S===w&&x===_&&h===D&&c.length===1&&c[0]===h){let B=Re(b);return G(B,A)}if(h===w&&D===_&&S===x&&c.length===1&&c[0]===S){let B=Re(A);return G(b,B)}}if(d.length===1&&g.length===1&&d===g&&o.length===0&&b.ndim===1&&A.ndim===1)return ct(t,a,c,i);if(d&&g&&d.length===1&&g.length===1&&o.length===2&&o===d+g&&c.length===0&&b.ndim===1&&A.ndim===1)return ft(b,A)}if(t.length===1&&a[0].length===2&&o.length===0){let d=a[0];if(d[0]===d[1]&&t[0].ndim===2)return ct(t,a,c,i)}let l=Array.from(o).map(d=>i.get(d));if(l.length===0)return ct(t,a,c,i);let m=t[0].dtype;for(let d=1;d<t.length;d++)m=k(m,t[d].dtype);let f=N.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=_a(d,l),b=new Map;for(let h=0;h<o.length;h++)b.set(o[h],g[h]);let A=0;for(let h=0;h<y;h++){let S=h;for(let x=c.length-1;x>=0;x--){let w=c[x],_=i.get(w);b.set(w,S%_),S=Math.floor(S/_)}let D=1;for(let x=0;x<t.length;x++){let w=t[x],_=a[x],B=[];for(let v of _)B.push(b.get(v));let E=w.get(...B);D*=Number(E)}A+=D}f.set(g,A)}return f}function za(r){let t=new Map,e=r.split(",");for(let o of e)for(let a of o.trim())t.set(a,(t.get(a)||0)+1);let n=[];for(let[o,a]of t)a===1&&n.push(o);return n.sort().join("")}function ct(r,t,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 u=new Map,s=i;for(let l=e.length-1;l>=0;l--){let m=e[l],f=n.get(m);u.set(m,s%f),s=Math.floor(s/f)}let c=1;for(let l=0;l<r.length;l++){let m=r[l],f=t[l],p=[];for(let d of f)p.push(u.get(d));let y=m.get(...p);c*=Number(y)}a+=c}return a}function _a(r,t){let e=new Array(t.length),n=r;for(let o=t.length-1;o>=0;o--)e[o]=n%t[o],n=Math.floor(n/t[o]);return e}function Je(r,t){let e=r.shape,n=t.shape,o=e.length,a=n.length,i=k(r.dtype,t.dtype),u=Math.max(o,a),s=new Array(u),c=new Array(u).fill(1),l=new Array(u).fill(1);for(let y=0;y<o;y++)c[u-o+y]=e[y];for(let y=0;y<a;y++)l[u-a+y]=n[y];for(let y=0;y<u;y++)s[y]=c[y]*l[y];let m=N.zeros(s,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 h=o-1;h>=0;h--)g[h]=d%e[h],d=Math.floor(d/e[h]);let b=new Array(u).fill(0);for(let h=0;h<o;h++)b[u-o+h]=g[h];let A=r.get(...g);for(let h=0;h<p;h++){let S=h,D=new Array(a);for(let E=a-1;E>=0;E--)D[E]=S%n[E],S=Math.floor(S/n[E]);let x=new Array(u).fill(0);for(let E=0;E<a;E++)x[u-a+E]=D[E];let w=t.get(...D),_=new Array(u);for(let E=0;E<u;E++)_[E]=b[E]*l[E]+x[E];let B=typeof A=="bigint"||typeof w=="bigint"?BigInt(Number(A))*BigInt(Number(w)):Number(A)*Number(w);m.set(_,B)}}return m}function Qe(r,t,e=-1,n=-1,o=-1,a){a!==void 0&&(e=a,n=a,o=a);let i=(h,S)=>h<0?S+h:h,u=i(e,r.ndim),s=i(n,t.ndim);if(r.ndim===1&&t.ndim===1){let h=r.shape[0],S=t.shape[0];if(h===3&&S===3){let D=Number(r.get(0)),x=Number(r.get(1)),w=Number(r.get(2)),_=Number(t.get(0)),B=Number(t.get(1)),E=Number(t.get(2)),v=N.zeros([3],"float64");return v.set([0],x*E-w*B),v.set([1],w*_-D*E),v.set([2],D*B-x*_),v}else if(h===2&&S===2){let D=Number(r.get(0)),x=Number(r.get(1)),w=Number(t.get(0)),_=Number(t.get(1));return D*_-x*w}else if(h===2&&S===3||h===3&&S===2){let D=Number(r.get(0)),x=Number(r.get(1)),w=h===3?Number(r.get(2)):0,_=Number(t.get(0)),B=Number(t.get(1)),E=S===3?Number(t.get(2)):0,v=N.zeros([3],"float64");return v.set([0],x*E-w*B),v.set([1],w*_-D*E),v.set([2],D*B-x*_),v}else throw new Error(`cross: incompatible dimensions for cross product: ${h} and ${S}`)}let c=r.shape[u],l=t.shape[s];if(c!==2&&c!==3||l!==2&&l!==3)throw new Error(`cross: incompatible dimensions for cross product: ${c} and ${l}`);let m=c===2&&l===2?0:3,f=[...r.shape.slice(0,u),...r.shape.slice(u+1)],p=[...t.shape.slice(0,s),...t.shape.slice(s+1)];if(f.length!==p.length)throw new Error("cross: incompatible shapes for cross product");for(let h=0;h<f.length;h++)if(f[h]!==p[h])throw new Error("cross: incompatible shapes for cross product");let y=f,d=o<0?y.length+1+o:o,g;if(m===0?g=y:g=[...y.slice(0,d),m,...y.slice(d)],g.length===0)throw new Error("cross: unexpected scalar result from higher-dimensional input");let b=N.zeros(g,"float64"),A=y.reduce((h,S)=>h*S,1);for(let h=0;h<A;h++){let S=h,D=[];for(let j=y.length-1;j>=0;j--)D[j]=S%y[j],S=Math.floor(S/y[j]);let x=[...D.slice(0,u),0,...D.slice(u)],w=[...D.slice(0,s),0,...D.slice(s)],_=j=>(x[u]=j,Number(r.get(...x))),B=j=>(w[s]=j,Number(t.get(...w))),E=_(0),v=_(1),$=c===3?_(2):0,M=B(0),V=B(1),Z=l===3?B(2):0;if(m===0)b.set(D,E*V-v*M);else{let j=v*Z-$*V,lr=$*M-E*Z,Ko=E*V-v*M,jr=(ra,ta)=>{let ea=[...D.slice(0,d),ra,...D.slice(d)];b.set(ea,ta)};jr(0,j),jr(1,lr),jr(2,Ko)}}return b}function ir(r,t=2,e,n=!1){if(typeof t!="number")throw new Error("vector_norm: ord must be a number");if(e==null){let c=r.ndim===1?r:sr(r),l=c.size,m;if(t===1/0){m=0;for(let f=0;f<l;f++)m=Math.max(m,Math.abs(Number(c.get(f))))}else if(t===-1/0){m=1/0;for(let f=0;f<l;f++)m=Math.min(m,Math.abs(Number(c.get(f))))}else if(t===0){m=0;for(let f=0;f<l;f++)Number(c.get(f))!==0&&m++}else if(t===1){m=0;for(let f=0;f<l;f++)m+=Math.abs(Number(c.get(f)))}else if(t===2){m=0;for(let f=0;f<l;f++){let p=Number(c.get(f));m+=p*p}m=Math.sqrt(m)}else{m=0;for(let f=0;f<l;f++)m+=Math.pow(Math.abs(Number(c.get(f))),t);m=Math.pow(m,1/t)}if(n){let f=new Array(r.ndim).fill(1),p=N.zeros(f,"float64");return p.set(new Array(r.ndim).fill(0),m),p}return m}let o=e<0?r.ndim+e:e;if(o<0||o>=r.ndim)throw new Error(`vector_norm: axis ${e} out of bounds for array with ${r.ndim} dimensions`);let a=n?[...r.shape.slice(0,o),1,...r.shape.slice(o+1)]:[...r.shape.slice(0,o),...r.shape.slice(o+1)];if(a.length===0)return ir(r,t,null,!1);let i=N.zeros(a,"float64"),u=r.shape[o],s=a.reduce((c,l)=>c*l,1);for(let c=0;c<s;c++){let l=c,m=[];for(let y=a.length-1;y>=0;y--)m[y]=l%a[y],l=Math.floor(l/a[y]);let f=n?[...m.slice(0,o),0,...m.slice(o+1)]:[...m.slice(0,o),0,...m.slice(o)],p;if(t===1/0){p=0;for(let y=0;y<u;y++)f[o]=y,p=Math.max(p,Math.abs(Number(r.get(...f))))}else if(t===-1/0){p=1/0;for(let y=0;y<u;y++)f[o]=y,p=Math.min(p,Math.abs(Number(r.get(...f))))}else if(t===0){p=0;for(let y=0;y<u;y++)f[o]=y,Number(r.get(...f))!==0&&p++}else if(t===1){p=0;for(let y=0;y<u;y++)f[o]=y,p+=Math.abs(Number(r.get(...f)))}else if(t===2){p=0;for(let y=0;y<u;y++){f[o]=y;let d=Number(r.get(...f));p+=d*d}p=Math.sqrt(p)}else{p=0;for(let y=0;y<u;y++)f[o]=y,p+=Math.pow(Math.abs(Number(r.get(...f))),t);p=Math.pow(p,1/t)}i.set(m,p)}return i}function nr(r,t="fro",e=!1){if(r.ndim!==2)throw new Error(`matrix_norm: input must be 2D, got ${r.ndim}D`);let[n,o]=r.shape,a;if(t==="fro"){a=0;for(let i=0;i<n;i++)for(let u=0;u<o;u++){let s=Number(r.get(i,u));a+=s*s}a=Math.sqrt(a)}else if(t==="nuc"){let{s:i}=tr(r);a=0;for(let u=0;u<i.size;u++)a+=Number(i.get(u))}else if(t===1){a=0;for(let i=0;i<o;i++){let u=0;for(let s=0;s<n;s++)u+=Math.abs(Number(r.get(s,i)));a=Math.max(a,u)}}else if(t===-1){a=1/0;for(let i=0;i<o;i++){let u=0;for(let s=0;s<n;s++)u+=Math.abs(Number(r.get(s,i)));a=Math.min(a,u)}}else if(t===1/0){a=0;for(let i=0;i<n;i++){let u=0;for(let s=0;s<o;s++)u+=Math.abs(Number(r.get(i,s)));a=Math.max(a,u)}}else if(t===-1/0){a=1/0;for(let i=0;i<n;i++){let u=0;for(let s=0;s<o;s++)u+=Math.abs(Number(r.get(i,s)));a=Math.min(a,u)}}else if(t===2){let{s:i}=tr(r);a=Number(i.get(0))}else if(t===-2){let{s:i}=tr(r);a=Number(i.get(i.size-1))}else throw new Error(`matrix_norm: invalid ord value: ${t}`);if(e){let i=N.zeros([1,1],"float64");return i.set([0,0],a),i}return a}function Xe(r,t=null,e=null,n=!1){if(t===null)return e===null?ir(r,2,null,n):typeof e=="number"?ir(r,2,e,n):nr(r,"fro",n);if(Array.isArray(e)){if(e.length!==2)throw new Error("norm: axis must be a 2-tuple for matrix norms");let o=e[0]<0?r.ndim+e[0]:e[0],a=e[1]<0?r.ndim+e[1]:e[1];if(r.ndim!==2||o!==0&&o!==1||a!==0&&a!==1||o===a)throw new Error("norm: complex axis specification not yet supported");return nr(r,t,n)}if(r.ndim===2&&e===null&&(t==="fro"||t==="nuc"))return nr(r,t,n);if(typeof t!="number"&&t!==null)throw new Error(`norm: ord '${t}' not valid for vector norm`);return ir(r,t??2,e,n)}function mt(r,t="reduced"){if(r.ndim!==2)throw new Error(`qr: input must be 2D, got ${r.ndim}D`);let[e,n]=r.shape,o=Math.min(e,n),a=N.zeros([e,n],"float64");for(let p=0;p<e;p++)for(let y=0;y<n;y++)a.set([p,y],Number(r.get(p,y)));let i=[],u=[];for(let p=0;p<o;p++){let y=e-p,d=[];for(let D=p;D<e;D++)d.push(Number(a.get(D,p)));let g=0;for(let D=0;D<y;D++)g+=d[D]*d[D];if(g=Math.sqrt(g),g<1e-15){i.push(d),u.push(0);continue}let b=d[0]>=0?1:-1,A=d[0]+b*g,h=[1];for(let D=1;D<y;D++)h.push(d[D]/A);let S=b*A/g;u.push(S),i.push(h);for(let D=p;D<n;D++){let x=0;for(let w=0;w<y;w++)x+=h[w]*Number(a.get(p+w,D));for(let w=0;w<y;w++)a.set([p+w,D],Number(a.get(p+w,D))-S*h[w]*x)}}if(t==="raw"){let p=N.zeros([e,n],"float64");for(let d=0;d<e;d++)for(let g=0;g<n;g++)p.set([d,g],Number(a.get(d,g)));let y=N.zeros([o],"float64");for(let d=0;d<o;d++)y.set([d],u[d]);return{h:p,tau:y}}if(t==="r"){let p=N.zeros([o,n],"float64");for(let y=0;y<o;y++)for(let d=y;d<n;d++)p.set([y,d],Number(a.get(y,d)));return p}let s=t==="complete"?e:o,c=N.zeros([e,s],"float64");for(let p=0;p<Math.min(e,s);p++)c.set([p,p],1);for(let p=o-1;p>=0;p--){let y=i[p],d=u[p],g=e-p;for(let b=p;b<s;b++){let A=0;for(let h=0;h<g;h++)A+=y[h]*Number(c.get(p+h,b));for(let h=0;h<g;h++)c.set([p+h,b],Number(c.get(p+h,b))-d*y[h]*A)}}let l=N.zeros([e,s],"float64");for(let p=0;p<e;p++)for(let y=0;y<s;y++)l.set([p,y],Number(c.get(p,y)));let m=t==="complete"?e:o,f=N.zeros([m,n],"float64");for(let p=0;p<m;p++)for(let y=0;y<n;y++)y>=p&&f.set([p,y],Number(a.get(p,y)));return{q:l,r:f}}function Ke(r,t=!1){if(r.ndim!==2)throw new Error(`cholesky: input must be 2D, got ${r.ndim}D`);let[e,n]=r.shape;if(e!==n)throw new Error(`cholesky: matrix must be square, got ${e}x${n}`);let o=e,a=N.zeros([o,o],"float64");for(let i=0;i<o;i++)for(let u=0;u<=i;u++){let s=0;if(i===u){for(let l=0;l<u;l++)s+=Number(a.get(u,l))**2;let c=Number(r.get(u,u))-s;if(c<0)throw new Error("cholesky: matrix is not positive definite");a.set([u,u],Math.sqrt(c))}else{for(let l=0;l<u;l++)s+=Number(a.get(i,l))*Number(a.get(u,l));let c=Number(a.get(u,u));if(Math.abs(c)<1e-15)throw new Error("cholesky: matrix is not positive definite");a.set([i,u],(Number(r.get(i,u))-s)/c)}}if(t){let i=N.zeros([o,o],"float64");for(let u=0;u<o;u++)for(let s=u;s<o;s++)i.set([u,s],Number(a.get(s,u)));return i}return a}function tr(r){if(r.ndim!==2)throw new Error(`svd: input must be 2D, got ${r.ndim}D`);let[t,e]=r.shape,n=Math.min(t,e),o=N.zeros([e,e],"float64");for(let m=0;m<e;m++)for(let f=0;f<e;f++){let p=0;for(let y=0;y<t;y++)p+=Number(r.get(y,m))*Number(r.get(y,f));o.set([m,f],p)}let{values:a,vectors:i}=pt(o),u=Array.from({length:e},(m,f)=>f);u.sort((m,f)=>a[f]-a[m]);let s=N.zeros([n],"float64");for(let m=0;m<n;m++){let f=a[u[m]];s.set([m],Math.sqrt(Math.max(0,f)))}let c=N.zeros([e,e],"float64");for(let m=0;m<e;m++)for(let f=0;f<e;f++)c.set([m,f],i[f][u[m]]);let l=N.zeros([t,t],"float64");for(let m=0;m<t;m++)for(let f=0;f<n;f++){let p=Number(s.get(f));if(p>1e-10){let y=0;for(let d=0;d<e;d++)y+=Number(r.get(m,d))*Number(c.get(f,d));l.set([m,f],y/p)}}if(t>n)for(let m=n;m<t;m++){let f=new Array(t).fill(0);f[m]=1;for(let y=0;y<m;y++){let d=0;for(let g=0;g<t;g++)d+=f[g]*Number(l.get(g,y));for(let g=0;g<t;g++)f[g]=f[g]-d*Number(l.get(g,y))}let p=0;for(let y=0;y<t;y++)p+=f[y]*f[y];if(p=Math.sqrt(p),p>1e-10)for(let y=0;y<t;y++)l.set([y,m],f[y]/p)}return{u:l,s,vt:c}}function pt(r){let t=r.shape[0],e=100*t*t,n=1e-10,o=[];for(let u=0;u<t;u++){o.push([]);for(let s=0;s<t;s++)o[u].push(Number(r.get(u,s)))}let a=[];for(let u=0;u<t;u++){a.push([]);for(let s=0;s<t;s++)a[u].push(u===s?1:0)}for(let u=0;u<e;u++){let s=0,c=0,l=1;for(let h=0;h<t;h++)for(let S=h+1;S<t;S++)Math.abs(o[h][S])>s&&(s=Math.abs(o[h][S]),c=h,l=S);if(s<n)break;let m=o[c][c],f=o[l][l],p=o[c][l],y;Math.abs(m-f)<1e-15?y=Math.PI/4:y=.5*Math.atan2(2*p,f-m);let d=Math.cos(y),g=Math.sin(y),b=d*d*m+g*g*f-2*g*d*p,A=g*g*m+d*d*f+2*g*d*p;o[c][c]=b,o[l][l]=A,o[c][l]=0,o[l][c]=0;for(let h=0;h<t;h++)if(h!==c&&h!==l){let S=o[h][c],D=o[h][l];o[h][c]=d*S-g*D,o[c][h]=o[h][c],o[h][l]=g*S+d*D,o[l][h]=o[h][l]}for(let h=0;h<t;h++){let S=a[h][c],D=a[h][l];a[h][c]=d*S-g*D,a[h][l]=g*S+d*D}}let i=[];for(let u=0;u<t;u++)i.push(o[u][u]);return{values:i,vectors:a}}function rn(r,t=!0,e=!0){let n=tr(r);if(!e)return n.s;if(!t){let[o,a]=r.shape,i=Math.min(o,a),u=N.zeros([o,i],"float64");for(let c=0;c<o;c++)for(let l=0;l<i;l++)u.set([c,l],Number(n.u.get(c,l)));let s=N.zeros([i,a],"float64");for(let c=0;c<i;c++)for(let l=0;l<a;l++)s.set([c,l],Number(n.vt.get(c,l)));return{u,s:n.s,vt:s}}return n}function tn(r){if(r.ndim!==2)throw new Error(`det: input must be 2D, got ${r.ndim}D`);let[t,e]=r.shape;if(t!==e)throw new Error(`det: matrix must be square, got ${t}x${e}`);let n=t;if(n===0)return 1;let o=r.data;if(n===1)return Number(o[0]);if(n===2)return Number(o[0])*Number(o[3])-Number(o[1])*Number(o[2]);let{lu:a,sign:i}=yt(r),u=a.data,s=i;for(let c=0;c<n;c++)s*=u[c*n+c];return s}function yt(r){let[t,e]=r.shape,n=t,o=e,a=N.zeros([n,o],"float64"),i=a.data,u=r.data;for(let l=0;l<n*o;l++)i[l]=Number(u[l]);let s=Array.from({length:n},(l,m)=>m),c=1;for(let l=0;l<Math.min(n,o);l++){let m=Math.abs(i[l*o+l]),f=l;for(let y=l+1;y<n;y++){let d=Math.abs(i[y*o+l]);d>m&&(m=d,f=y)}if(f!==l){for(let d=0;d<o;d++){let g=i[l*o+d];i[l*o+d]=i[f*o+d],i[f*o+d]=g}let y=s[l];s[l]=s[f],s[f]=y,c=-c}let p=i[l*o+l];if(Math.abs(p)>1e-15)for(let y=l+1;y<n;y++){let d=i[y*o+l]/p;i[y*o+l]=d;for(let g=l+1;g<o;g++)i[y*o+g]=i[y*o+g]-d*i[l*o+g]}}return{lu:a,piv:s,sign:c}}function kr(r){if(r.ndim!==2)throw new Error(`inv: input must be 2D, got ${r.ndim}D`);let[t,e]=r.shape;if(t!==e)throw new Error(`inv: matrix must be square, got ${t}x${e}`);let n=t,{lu:o,piv:a}=yt(r),i=o.data,u=N.zeros([n,n],"float64"),s=u.data;for(let c=0;c<n;c++){let l=new Float64Array(n);for(let m=0;m<n;m++){let f=a[m]===c?1:0;for(let p=0;p<m;p++)f-=i[m*n+p]*l[p];l[m]=f}for(let m=n-1;m>=0;m--){let f=l[m];for(let y=m+1;y<n;y++)f-=i[m*n+y]*s[y*n+c];let p=i[m*n+m];if(Math.abs(p)<1e-15)throw new Error("inv: singular matrix");s[m*n+c]=f/p}}return u}function Le(r,t){let[e]=r.shape,n=e,{lu:o,piv:a}=yt(r),i=o.data,u=t.data,s=new Float64Array(n);for(let f=0;f<n;f++)s[f]=Number(u[a[f]]);let c=new Float64Array(n);for(let f=0;f<n;f++){let p=s[f];for(let y=0;y<f;y++)p-=i[f*n+y]*c[y];c[f]=p}let l=N.zeros([n],"float64"),m=l.data;for(let f=n-1;f>=0;f--){let p=c[f];for(let d=f+1;d<n;d++)p-=i[f*n+d]*m[d];let y=i[f*n+f];if(Math.abs(y)<1e-15)throw new Error("solve: singular matrix");m[f]=p/y}return l}function en(r,t){if(r.ndim!==2)throw new Error(`solve: coefficient matrix must be 2D, got ${r.ndim}D`);let[e,n]=r.shape;if(e!==n)throw new Error(`solve: coefficient matrix must be square, got ${e}x${n}`);let o=e;if(t.ndim===1){if(t.shape[0]!==o)throw new Error(`solve: incompatible shapes (${e},${n}) and (${t.shape[0]},)`);return Le(r,t)}if(t.ndim===2){if(t.shape[0]!==o)throw new Error(`solve: incompatible shapes (${e},${n}) and (${t.shape[0]},${t.shape[1]})`);let a=t.shape[1],i=N.zeros([o,a],"float64");for(let u=0;u<a;u++){let s=N.zeros([o],"float64");for(let l=0;l<o;l++)s.set([l],Number(t.get(l,u)));let c=Le(r,s);for(let l=0;l<o;l++)i.set([l,u],Number(c.get(l)))}return i}throw new Error(`solve: b must be 1D or 2D, got ${t.ndim}D`)}function nn(r,t,e=null){if(r.ndim!==2)throw new Error(`lstsq: coefficient matrix must be 2D, got ${r.ndim}D`);let[n,o]=r.shape,{u:a,s:i,vt:u}=tr(r),s=Math.min(n,o),c=e??Math.max(n,o)*Number.EPSILON,m=Number(i.get(0))*c,f=0;for(let A=0;A<s;A++)Number(i.get(A))>m&&f++;let p=t.ndim===1?P(t,[t.size,1]):t,y=p.shape[1];if(p.shape[0]!==n)throw new Error(`lstsq: incompatible shapes (${n},${o}) and (${t.shape.join(",")})`);let d=N.zeros([o,y],"float64");for(let A=0;A<y;A++){let h=new Array(n).fill(0);for(let S=0;S<n;S++)for(let D=0;D<n;D++)h[S]+=Number(a.get(D,S))*Number(p.get(D,A));for(let S=0;S<o;S++){let D=0;for(let x=0;x<s;x++){let w=Number(i.get(x));w>m&&(D+=Number(u.get(x,S))*h[x]/w)}d.set([S,A],D)}}let g;if(n>o){g=N.zeros([y],"float64");for(let A=0;A<y;A++){let h=0;for(let S=0;S<n;S++){let D=0;for(let w=0;w<o;w++)D+=Number(r.get(S,w))*Number(d.get(w,A));let x=D-Number(p.get(S,A));h+=x*x}g.set([A],h)}}else g=N.zeros([0],"float64");return{x:t.ndim===1?P(d,[o]):d,residuals:g,rank:f,s:i}}function on(r,t=2){if(r.ndim!==2)throw new Error(`cond: input must be 2D, got ${r.ndim}D`);let[e,n]=r.shape;if(t===2||t===-2){let{s:u}=tr(r),s=Math.min(e,n),c=Number(u.get(0)),l=Number(u.get(s-1));return t===2?l>0?c/l:1/0:c>0?l/c:0}if(e!==n)throw new Error(`cond: matrix must be square for p=${t}`);let o=nr(r,t),a=kr(r),i=nr(a,t);return o*i}function an(r,t){if(r.ndim===0)return Number(r.get())!==0?1:0;if(r.ndim===1){for(let i=0;i<r.size;i++)if(Number(r.get(i))!==0)return 1;return 0}if(r.ndim!==2)throw new Error(`matrix_rank: input must be at most 2D, got ${r.ndim}D`);let{s:e}=tr(r),n=Number(e.get(0)),o=t??n*Math.max(r.shape[0],r.shape[1])*Number.EPSILON,a=0;for(let i=0;i<e.size;i++)Number(e.get(i))>o&&a++;return a}function sn(r,t){if(r.ndim!==2)throw new Error(`matrix_power: input must be 2D, got ${r.ndim}D`);let[e,n]=r.shape;if(e!==n)throw new Error(`matrix_power: matrix must be square, got ${e}x${n}`);let o=e;if(!Number.isInteger(t))throw new Error("matrix_power: exponent must be an integer");if(t===0){let c=N.zeros([o,o],"float64");for(let l=0;l<o;l++)c.set([l,l],1);return c}let a=r,i=t;t<0&&(a=kr(r),i=-t);let u=N.zeros([o,o],"float64");for(let c=0;c<o;c++)u.set([c,c],1);let s=N.zeros([o,o],"float64");for(let c=0;c<o;c++)for(let l=0;l<o;l++)s.set([c,l],Number(a.get(c,l)));for(;i>0;)i&1&&(u=G(u,s)),s=G(s,s),i>>=1;return u}function un(r,t=1e-15){if(r.ndim!==2)throw new Error(`pinv: input must be 2D, got ${r.ndim}D`);let[e,n]=r.shape,{u:o,s:a,vt:i}=tr(r),u=Math.min(e,n),c=Number(a.get(0))*t,l=N.zeros([n,e],"float64");for(let m=0;m<n;m++)for(let f=0;f<e;f++){let p=0;for(let y=0;y<u;y++){let d=Number(a.get(y));d>c&&(p+=Number(i.get(y,m))*Number(o.get(f,y))/d)}l.set([m,f],p)}return l}function dt(r){if(r.ndim!==2)throw new Error(`eig: input must be 2D, got ${r.ndim}D`);let[t,e]=r.shape;if(t!==e)throw new Error(`eig: matrix must be square, got ${t}x${e}`);let n=t,o=!0;r:for(let c=0;c<n;c++)for(let l=c+1;l<n;l++)if(Math.abs(Number(r.get(c,l))-Number(r.get(l,c)))>1e-10){o=!1;break r}if(o){let{values:c,vectors:l}=pt(r),m=N.zeros([n],"float64"),f=N.zeros([n,n],"float64");for(let p=0;p<n;p++){m.set([p],c[p]);for(let y=0;y<n;y++)f.set([y,p],l[y][p])}return{w:m,v:f}}console.warn("numpy-ts: eig() called on non-symmetric matrix. Complex eigenvalues are not supported; results may be inaccurate. For symmetric matrices, use eigh() instead.");let{values:a,vectors:i}=Ba(r),u=N.zeros([n],"float64"),s=N.zeros([n,n],"float64");for(let c=0;c<n;c++){u.set([c],a[c]);for(let l=0;l<n;l++)s.set([l,c],i[l][c])}return{w:u,v:s}}function Ba(r){let t=r.shape[0],e=1e3,n=1e-10,o=N.zeros([t,t],"float64");for(let s=0;s<t;s++)for(let c=0;c<t;c++)o.set([s,c],Number(r.get(s,c)));let a=N.zeros([t,t],"float64");for(let s=0;s<t;s++)a.set([s,s],1);for(let s=0;s<e;s++){let c=0;for(let p=0;p<t;p++)for(let y=0;y<t;y++)p!==y&&(c+=Number(o.get(p,y))**2);if(Math.sqrt(c)<n*t)break;let l=mt(o,"reduced"),m=l.q,f=l.r;o=G(f,m),a=G(a,m)}let i=[];for(let s=0;s<t;s++)i.push(Number(o.get(s,s)));let u=[];for(let s=0;s<t;s++){u.push([]);for(let c=0;c<t;c++)u[s].push(Number(a.get(s,c)))}return{values:i,vectors:u}}function gt(r,t="L"){if(r.ndim!==2)throw new Error(`eigh: input must be 2D, got ${r.ndim}D`);let[e,n]=r.shape;if(e!==n)throw new Error(`eigh: matrix must be square, got ${e}x${n}`);let o=e,a=N.zeros([o,o],"float64");for(let m=0;m<o;m++)for(let f=0;f<o;f++)t==="L"?m>=f&&(a.set([m,f],Number(r.get(m,f))),a.set([f,m],Number(r.get(m,f)))):f>=m&&(a.set([m,f],Number(r.get(m,f))),a.set([f,m],Number(r.get(m,f))));let{values:i,vectors:u}=pt(a),s=Array.from({length:o},(m,f)=>f);s.sort((m,f)=>i[m]-i[f]);let c=N.zeros([o],"float64"),l=N.zeros([o,o],"float64");for(let m=0;m<o;m++){c.set([m],i[s[m]]);for(let f=0;f<o;f++)l.set([f,m],u[f][s[m]])}return{w:c,v:l}}function cn(r){let{w:t}=dt(r);return t}function ln(r,t="L"){let{w:e}=gt(r,t);return e}function fn(r){return C(r,Math.sqrt,!1)}function mn(r,t){return typeof t=="number"?va(r,t):F(r,t,Math.pow,"power")}function va(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,c=N.zeros(n,s),l=c.data;if(I(e))if(I(s)&&Number.isInteger(t)&&t>=0){let m=o,f=l;for(let p=0;p<a;p++)f[p]=m[p]**BigInt(t)}else for(let m=0;m<a;m++)l[m]=Math.pow(Number(o[m]),t);else for(let m=0;m<a;m++)l[m]=Math.pow(Number(o[m]),t);return c}function pn(r){return C(r,Math.exp,!1)}function yn(r){return C(r,t=>Math.pow(2,t),!1)}function dn(r){return C(r,Math.expm1,!1)}function gn(r){return C(r,Math.log,!1)}function hn(r){return C(r,Math.log2,!1)}function An(r){return C(r,Math.log10,!1)}function bn(r){return C(r,Math.log1p,!1)}function Sn(r,t){return typeof t=="number"?Ua(r,t):Oa(r,t)}function Oa(r,t){let e=fr(r.shape,t.shape),n=e.reduce((c,l)=>c*l,1),o=r.dtype,a=t.dtype,i=o==="float32"&&a==="float32"?"float32":"float64",u=N.zeros(e,i),s=u.data;for(let c=0;c<n;c++){let l=(I(o),Number(r.iget(c))),m=(I(a),Number(t.iget(c))),f=Math.max(l,m),p=Math.min(l,m);s[c]=f+Math.log1p(Math.exp(p-f))}return u}function Ua(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.size,a=e==="float32"?"float32":"float64",i=N.zeros(n,a),u=i.data;for(let s=0;s<o;s++){let c=(I(e),Number(r.data[s])),l=Math.max(c,t),m=Math.min(c,t);u[s]=l+Math.log1p(Math.exp(m-l))}return i}function Nn(r,t){return typeof t=="number"?$a(r,t):Ta(r,t)}function Ta(r,t){let e=fr(r.shape,t.shape),n=e.reduce((l,m)=>l*m,1),o=r.dtype,a=t.dtype,i=o==="float32"&&a==="float32"?"float32":"float64",u=N.zeros(e,i),s=u.data,c=Math.LOG2E;for(let l=0;l<n;l++){let m=(I(o),Number(r.iget(l))),f=(I(a),Number(t.iget(l))),p=Math.max(m,f),y=Math.min(m,f);s[l]=p+Math.log1p(Math.pow(2,y-p))*c}return u}function $a(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.size,a=e==="float32"?"float32":"float64",i=N.zeros(n,a),u=i.data,s=Math.LOG2E;for(let c=0;c<o;c++){let l=(I(e),Number(r.data[c])),m=Math.max(l,t),f=Math.min(l,t);u[c]=m+Math.log1p(Math.pow(2,f-m))*s}return i}function Dn(r){return C(r,Math.sin,!1)}function xn(r){return C(r,Math.cos,!1)}function wn(r){return C(r,Math.tan,!1)}function In(r){return C(r,Math.asin,!1)}function zn(r){return C(r,Math.acos,!1)}function _n(r){return C(r,Math.atan,!1)}function Bn(r,t){return typeof t=="number"?Ca(r,t):ka(r,t)}function ka(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,i=o==="float32"&&a==="float32"?"float32":"float64",u=N.zeros(e,i),s=u.data;for(let c=0;c<n;c++){let l=(I(o),Number(r.data[c])),m=(I(a),Number(t.data[c]));s[c]=Math.atan2(l,m)}return u}function Ca(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=e==="float32"?"float32":"float64",u=N.zeros(n,i),s=u.data;if(I(e))for(let c=0;c<a;c++)s[c]=Math.atan2(Number(o[c]),t);else for(let c=0;c<a;c++)s[c]=Math.atan2(Number(o[c]),t);return u}function En(r,t){return typeof t=="number"?qa(r,t):Va(r,t)}function Va(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,i=o==="float32"&&a==="float32"?"float32":"float64",u=N.zeros(e,i),s=u.data;for(let c=0;c<n;c++){let l=(I(o),Number(r.data[c])),m=(I(a),Number(t.data[c]));s[c]=Math.hypot(l,m)}return u}function qa(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=e==="float32"?"float32":"float64",u=N.zeros(n,i),s=u.data;if(I(e))for(let c=0;c<a;c++)s[c]=Math.hypot(Number(o[c]),t);else for(let c=0;c<a;c++)s[c]=Math.hypot(Number(o[c]),t);return u}function vn(r){let t=180/Math.PI;return C(r,e=>e*t,!1)}function On(r){let t=Math.PI/180;return C(r,e=>e*t,!1)}function Un(r){return C(r,Math.sinh,!1)}function Tn(r){return C(r,Math.cosh,!1)}function $n(r){return C(r,Math.tanh,!1)}function Mn(r){return C(r,Math.asinh,!1)}function kn(r){return C(r,Math.acosh,!1)}function Cn(r){return C(r,Math.atanh,!1)}function ht(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=rr([Array.from(e),t]);if(a===null)throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);for(let i=0;i<o;i++)if(a[i]!==t[i])throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);return Y(r,t)}function Vn(r){if(r.length===0)return[];if(r.length===1)return[r[0]];let t=r.map(n=>Array.from(n.shape)),e=rr(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=>Y(n,e))}function qn(r,t,e){let n=r.shape,o=n.length,a=r.dtype;if(e===void 0){let y=r.size;for(let A of t){let h=A<0?y+A:A;if(h<0||h>=y)throw new Error(`index ${A} is out of bounds for axis 0 with size ${y}`)}let d=t.length,g=T(a);if(!g)throw new Error(`Cannot take from array with dtype ${a}`);let b=new g(d);for(let A=0;A<d;A++){let h=t[A];h<0&&(h=y+h);let S=r.iget(h);I(a),b[A]=S}return N.fromData(b,[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 u=n[i];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 ${i} with size ${u}`)}let s=Array.from(n);s[i]=t.length;let c=s.reduce((y,d)=>y*d,1),l=T(a);if(!l)throw new Error(`Cannot take from array with dtype ${a}`);let m=new l(c),f=R(s),p=new Array(o).fill(0);for(let y=0;y<c;y++){let d=[...p],g=p[i],b=t[g];b<0&&(b=u+b),d[i]=b;let A=r.get(...d),h=0;for(let S=0;S<o;S++)h+=p[S]*f[S];I(a),m[h]=A;for(let S=o-1;S>=0&&(p[S]++,!(p[S]<s[S]));S--)p[S]=0}return N.fromData(m,s,a)}function Fn(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 i=0;i<e.size;i++)a.push(e.iget(i));if(a.length===1)a=new Array(t.length).fill(a[0]);else if(a.length!==t.length){let i=[...a];a=[];for(let u=0;u<t.length;u++)a.push(i[u%i.length])}}for(let i=0;i<t.length;i++){let u=t[i];if(u<0&&(u=n+u),u<0||u>=n)throw new Error(`index ${t[i]} is out of bounds for axis 0 with size ${n}`);let s=a[i];I(o)?typeof s!="bigint"&&(s=BigInt(Math.round(Number(s)))):typeof s=="bigint"&&(s=Number(s)),r.iset(u,s)}}function Pn(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(f=>Array.from(f.shape));a.unshift(Array.from(e));let i=rr(a);if(i===null)throw new Error("operands could not be broadcast together");let u=Y(r,i),s=t.map(f=>Y(f,i)),c=i.reduce((f,p)=>f*p,1),l=T(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(u.iget(f));if(p<0||p>=n)throw new Error(`index ${p} is out of bounds for axis 0 with size ${n}`);let y=s[p].iget(f);I(o),m[f]=y}return N.fromData(m,i,o)}function jn(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),i=t.iget(o);if(e){let u=typeof a=="number"&&Number.isNaN(a),s=typeof i=="number"&&Number.isNaN(i);if(u&&s)continue}if(a!==i)return!1}return!0}function Rn(r,t,e){let n=r.shape,o=n.length,a=r.dtype,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 u=t.shape;if(u.length!==o)throw new Error(`indices and arr must have the same number of dimensions, got ${u.length} vs ${o}`);for(let d=0;d<o;d++)if(d!==i&&u[d]!==n[d]&&u[d]!==1&&n[d]!==1)throw new Error(`index ${u[d]} is out of bounds for size ${n[d]} in dimension ${d}`);let s=Array.from(u),c=s.reduce((d,g)=>d*g,1),l=T(a);if(!l)throw new Error(`Cannot take_along_axis with dtype ${a}`);let m=new l(c),f=R(n),p=R(u),y=n[i];for(let d=0;d<c;d++){let g=new Array(o),b=d;for(let w=o-1;w>=0;w--)g[w]=b%s[w],b=Math.floor(b/s[w]);let A=0;for(let w=0;w<o;w++){let _=u[w]===1?0:g[w];A+=_*p[w]}let h=Number(t.iget(A));if(h<0&&(h=y+h),h<0||h>=y)throw new Error(`index ${h} is out of bounds for axis ${i} with size ${y}`);let S=[...g];S[i]=h;let D=0;for(let w=0;w<o;w++){let _=n[w]===1?0:S[w];D+=_*f[w]}let x=r.iget(D);I(a),m[d]=x}return N.fromData(m,s,a)}function Ln(r,t,e,n){let o=r.shape,a=o.length,i=r.dtype,u=n<0?a+n:n;if(u<0||u>=a)throw new Error(`axis ${n} is out of bounds for array of dimension ${a}`);let s=t.shape,c=e.shape;if(s.length!==a||c.length!==a)throw new Error("indices, arr, and values must have same ndim");let l=o[u],m=R(o),f=R(s),p=R(c),y=s.reduce((d,g)=>d*g,1);for(let d=0;d<y;d++){let g=new Array(a),b=d;for(let _=a-1;_>=0;_--)g[_]=b%s[_],b=Math.floor(b/s[_]);let A=0;for(let _=0;_<a;_++)A+=g[_]*f[_];let h=Number(t.iget(A));if(h<0&&(h=l+h),h<0||h>=l)throw new Error(`index ${h} is out of bounds for axis ${u} with size ${l}`);let S=0;for(let _=0;_<a;_++){let B=c[_]===1?0:g[_];S+=B*p[_]}let D=e.iget(S),x=[...g];x[u]=h;let w=0;for(let _=0;_<a;_++)w+=x[_]*m[_];I(i)?typeof D!="bigint"&&(D=BigInt(Math.round(Number(D)))):typeof D=="bigint"&&(D=Number(D)),r.iset(w,D)}}function Zn(r,t,e){let n=r.size,o=r.dtype,a;if(typeof e=="number"||typeof e=="bigint")a=[e];else{a=[];for(let u=0;u<e.size;u++)a.push(e.iget(u))}let i=0;for(let u=0;u<n;u++)if(t.iget(u)){let c=a[i%a.length];I(o)?typeof c!="bigint"&&(c=BigInt(Math.round(Number(c)))):typeof c=="bigint"&&(c=Number(c)),r.iset(u,c),i++}}function Wn(r,t,e){let n=t.shape,o=n.length,a=t.dtype,i=t.data,u=I(a);if(e===void 0){let A=0,h=Math.min(r.size,t.size);for(let w=0;w<h;w++)r.iget(w)&&A++;let S=T(a);if(!S)throw new Error(`Cannot compress with dtype ${a}`);let D=new S(A),x=0;for(let w=0;w<h;w++)r.iget(w)&&(D[x]=i[w],x++);return N.fromData(D,[A],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 c=n[s],l=Math.min(r.size,c),m=[];for(let A=0;A<l;A++)r.iget(A)&&m.push(A);let f=m.length,p=[...n];p[s]=f;let y=p.reduce((A,h)=>A*h,1),d=T(a);if(!d)throw new Error(`Cannot compress with dtype ${a}`);let g=new d(y),b=R(n);if(s===0){let A=b[0],h=n.slice(1).reduce((D,x)=>D*x,1),S=0;for(let D=0;D<f;D++){let w=m[D]*A;if(u){let _=i,B=g;for(let E=0;E<h;E++)B[S++]=_[w+E]}else{let _=i,B=g;for(let E=0;E<h;E++)B[S++]=_[w+E]}}}else{let A=n.slice(0,s).reduce((D,x)=>D*x,1),h=n.slice(s+1).reduce((D,x)=>D*x,1),S=0;for(let D=0;D<A;D++)for(let x=0;x<f;x++){let w=m[x],_=0,B=D;for(let E=s-1;E>=0;E--){let v=B%n[E];B=Math.floor(B/n[E]),_+=v*b[E]}if(_+=w*b[s],u){let E=i,v=g;for(let $=0;$<h;$++)v[S++]=E[_+$]}else{let E=i,v=g;for(let $=0;$<h;$++)v[S++]=E[_+$]}}}return N.fromData(g,p,a)}function Gn(r,t,e=0){if(r.length!==t.length)throw new Error("condlist and choicelist must have same length");if(r.length===0)throw new Error("condlist and choicelist cannot be empty");let n=[...r.map(f=>Array.from(f.shape)),...t.map(f=>Array.from(f.shape))],o=rr(n);if(o===null)throw new Error("condlist and choicelist arrays could not be broadcast together");let a=t[0].dtype,i=o.reduce((f,p)=>f*p,1),u=T(a);if(!u)throw new Error(`Cannot select with dtype ${a}`);let s=e;I(a)?s=typeof e=="bigint"?e:BigInt(e):s=typeof e=="bigint"?Number(e):e;let c=new u(i);for(let f=0;f<i;f++)I(a),c[f]=s;let l=r.map(f=>Y(f,o)),m=t.map(f=>Y(f,o));for(let f=0;f<i;f++)for(let p=0;p<r.length;p++)if(l[p].iget(f)){let y=m[p].iget(f);I(a),c[f]=y;break}return N.fromData(c,o,a)}function Hn(r,t,e){let n=r.size,o=r.dtype,a=[];for(let u=0;u<e.size;u++)a.push(e.iget(u));if(a.length===0)return;let i=0;for(let u=0;u<n;u++)if(t.iget(u)){let c=a[i%a.length];I(o)?typeof c!="bigint"&&(c=BigInt(Math.round(Number(c)))):typeof c=="bigint"&&(c=Number(c)),r.iset(u,c),i++}}function At(r,t=2){if(t<1)throw new Error("ndim must be at least 1");let e=new Int32Array(r);for(let o=0;o<r;o++)e[o]=o;let n=[];for(let o=0;o<t;o++)n.push(N.fromData(new Int32Array(e),[r],"int32"));return n}function Yn(r){let t=r.shape,e=t.length;if(e<2)throw new Error("array must be at least 2-D");let n=t[0];for(let o=1;o<e;o++)if(t[o]!==n)throw new Error("All dimensions of input must be equal");return At(n,e)}function bt(r,t=0,e){let n=e??r,o=[],a=[];for(let i=0;i<r;i++)for(let u=0;u<=Math.min(i+t,n-1);u++)u>=0&&(o.push(i),a.push(u));return[N.fromData(new Int32Array(o),[o.length],"int32"),N.fromData(new Int32Array(a),[a.length],"int32")]}function Jn(r,t=0){let e=r.shape;if(e.length!==2)throw new Error("array must be 2-D");return bt(e[0],t,e[1])}function St(r,t=0,e){let n=e??r,o=[],a=[];for(let i=0;i<r;i++)for(let u=Math.max(i+t,0);u<n;u++)o.push(i),a.push(u);return[N.fromData(new Int32Array(o),[o.length],"int32"),N.fromData(new Int32Array(a),[a.length],"int32")]}function Qn(r,t=0){let e=r.shape;if(e.length!==2)throw new Error("array must be 2-D");return St(e[0],t,e[1])}function Xn(r,t,e=0){let n=t(r,e),o=n.shape;if(o.length!==2||o[0]!==r||o[1]!==r)throw new Error("mask_func must return n x n array");let a=[],i=[];for(let u=0;u<r;u++)for(let s=0;s<r;s++)n.get(u,s)&&(a.push(u),i.push(s));return[N.fromData(new Int32Array(a),[a.length],"int32"),N.fromData(new Int32Array(i),[i.length],"int32")]}function Kn(r,t="int32"){let e=r.length,n=[e,...r],o=n.reduce((s,c)=>s*c,1),a=T(t);if(!a)throw new Error(`Cannot create indices with dtype ${t}`);let i=new a(o),u=r.reduce((s,c)=>s*c,1);for(let s=0;s<e;s++){let c=s*u;for(let l=0;l<u;l++){let m=new Array(e),f=l;for(let y=e-1;y>=0;y--)m[y]=f%r[y],f=Math.floor(f/r[y]);let p=m[s];t==="int64"?i[c+l]=BigInt(p):i[c+l]=p}}return N.fromData(i,n,t)}function ro(...r){let t=r.length,e=[];for(let n=0;n<t;n++){let o=r[n],a=o.size,i=o.dtype,u=new Array(t).fill(1);u[n]=a;let s=T(i);if(!s)throw new Error(`Cannot create ix_ with dtype ${i}`);let c=new s(a);for(let l=0;l<a;l++){let m=o.iget(l);I(i),c[l]=m}e.push(N.fromData(c,u,i))}return e}function to(r,t,e="raise"){if(r.length!==t.length)throw new Error("multi_index length must equal dims length");if(r.length===0)throw new Error("multi_index cannot be empty");let n=r[0].size,o=t.length,a=new Int32Array(n),i=new Array(o),u=1;for(let s=o-1;s>=0;s--)i[s]=u,u*=t[s];for(let s=0;s<n;s++){let c=0;for(let l=0;l<o;l++){let m=Number(r[l].iget(s)),f=t[l];if(e==="wrap")m=(m%f+f)%f;else if(e==="clip")m=Math.max(0,Math.min(m,f-1));else if(m<0||m>=f)throw new Error(`index ${m} is out of bounds for axis ${l} with size ${f}`);c+=m*i[l]}a[s]=c}return N.fromData(a,[n],"int32")}function eo(r,t,e="C"){let n=t.length,o,a;if(typeof r=="number")o=[r],a=[];else{o=[];for(let l=0;l<r.size;l++)o.push(Number(r.iget(l)));a=Array.from(r.shape)}let i=o.length,u=t.reduce((l,m)=>l*m,1),s=new Array(n);if(e==="C"){let l=1;for(let m=n-1;m>=0;m--)s[m]=l,l*=t[m]}else{let l=1;for(let m=0;m<n;m++)s[m]=l,l*=t[m]}let c=[];for(let l=0;l<n;l++){let m=new Int32Array(i);c.push(N.fromData(m,a.length?a:[1],"int32"))}for(let l=0;l<i;l++){let m=o[l];if(m<0||m>=u)throw new Error(`index ${m} is out of bounds for array with size ${u}`);if(e==="C")for(let f=0;f<n;f++){let p=Math.floor(m/s[f]);m=m%s[f],c[f].data[l]=p%t[f]}else for(let f=n-1;f>=0;f--){let p=Math.floor(m/s[f]);m=m%s[f],c[f].data[l]=p%t[f]}}return typeof r=="number"?c.map(l=>{let m=l.iget(0);return N.fromData(new Int32Array([Number(m)]),[],"int32")}):c}function H(r,t){if(!Jt(r)&&r!=="bool")throw new TypeError(`ufunc '${t}' not supported for the input types, and the inputs could not be safely coerced to any supported types`)}function hr(r,t){return r.isCContiguous&&t.isCContiguous&&r.shape.length===t.shape.length&&r.shape.every((e,n)=>e===t.shape[n])}function ao(r,t){return H(r.dtype,"bitwise_and"),typeof t=="number"?La(r,t):(H(t.dtype,"bitwise_and"),hr(r,t)?Ra(r,t):F(r,t,(e,n)=>e&n,"bitwise_and"))}function Ra(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;if(!I(r.dtype)||!I(t.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])));s[l]=m&f}else{let l=a,m=i;for(let f=0;f<o;f++)s[f]=l[f]&m[f]}}else for(let s=0;s<o;s++)u[s]=a[s]&i[s];return n}function La(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]&l}else for(let s=0;s<a;s++)u[s]=o[s]&t;return i}function so(r,t){return H(r.dtype,"bitwise_or"),typeof t=="number"?Wa(r,t):(H(t.dtype,"bitwise_or"),hr(r,t)?Za(r,t):F(r,t,(e,n)=>e|n,"bitwise_or"))}function Za(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;if(!I(r.dtype)||!I(t.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])));s[l]=m|f}else{let l=a,m=i;for(let f=0;f<o;f++)s[f]=l[f]|m[f]}}else for(let s=0;s<o;s++)u[s]=a[s]|i[s];return n}function Wa(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]|l}else for(let s=0;s<a;s++)u[s]=o[s]|t;return i}function io(r,t){return H(r.dtype,"bitwise_xor"),typeof t=="number"?Ha(r,t):(H(t.dtype,"bitwise_xor"),hr(r,t)?Ga(r,t):F(r,t,(e,n)=>e^n,"bitwise_xor"))}function Ga(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;if(!I(r.dtype)||!I(t.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])));s[l]=m^f}else{let l=a,m=i;for(let f=0;f<o;f++)s[f]=l[f]^m[f]}}else for(let s=0;s<o;s++)u[s]=a[s]^i[s];return n}function Ha(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]^l}else for(let s=0;s<a;s++)u[s]=o[s]^t;return i}function Nt(r){H(r.dtype,"bitwise_not");let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=N.zeros(e,t),i=a.data;if(I(t)){let u=n,s=i;for(let c=0;c<o;c++)s[c]=~u[c]}else for(let u=0;u<o;u++)i[u]=~n[u];return a}function uo(r){return Nt(r)}function co(r,t){if(H(r.dtype,"left_shift"),typeof t=="number")return no(r,t);if(H(t.dtype,"left_shift"),t.size===1||t.ndim===1&&t.shape[0]===1){let e=I(t.dtype)?Number(t.data[0]):t.data[0];return no(r,e)}return hr(r,t)?Ya(r,t):F(r,t,(e,n)=>e<<n,"left_shift")}function Ya(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),m=typeof i[c]=="bigint"?i[c]:BigInt(Math.round(Number(i[c])));s[c]=l<<m}}else for(let s=0;s<o;s++)u[s]=a[s]<<i[s];return n}function no(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]<<l}else for(let s=0;s<a;s++)u[s]=o[s]<<t;return i}function lo(r,t){if(H(r.dtype,"right_shift"),typeof t=="number")return oo(r,t);if(H(t.dtype,"right_shift"),t.size===1||t.ndim===1&&t.shape[0]===1){let e=I(t.dtype)?Number(t.data[0]):t.data[0];return oo(r,e)}return hr(r,t)?Ja(r,t):F(r,t,(e,n)=>e>>n,"right_shift")}function Ja(r,t){let e=k(r.dtype,t.dtype),n=N.zeros(Array.from(r.shape),e),o=r.size,a=r.data,i=t.data,u=n.data;if(I(e)){let s=u;for(let c=0;c<o;c++){let l=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),m=typeof i[c]=="bigint"?i[c]:BigInt(Math.round(Number(i[c])));s[c]=l>>m}}else for(let s=0;s<o;s++)u[s]=a[s]>>i[s];return n}function oo(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=N.zeros(n,e),u=i.data;if(I(e)){let s=o,c=u,l=BigInt(Math.round(t));for(let m=0;m<a;m++)c[m]=s[m]>>l}else for(let s=0;s<a;s++)u[s]=o[s]>>t;return i}function fo(r,t=-1,e="big"){let n=Array.from(r.shape),o=n.length;if(t<0&&(t=o+t),t<0||t>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let a=n[t],i=Math.ceil(a/8),u=[...n];u[t]=i;let s=N.zeros(u,"uint8"),c=s.data;if(o===1){for(let g=0;g<i;g++){let b=0;for(let A=0;A<8;A++){let h=g*8+A;if(h<a){let S=Number(r.data[h])!==0?1:0;e==="big"?b|=S<<7-A:b|=S<<A}}c[g]=b}return s}let l=n.slice(0,t),m=n.slice(t+1),f=l.reduce((g,b)=>g*b,1),p=m.reduce((g,b)=>g*b,1),y=Cr(n),d=Cr(u);for(let g=0;g<f;g++)for(let b=0;b<p;b++)for(let A=0;A<i;A++){let h=0;for(let w=0;w<8;w++){let _=A*8+w;if(_<a){let B=0,E=g;for(let M=0;M<t;M++){let V=M<t-1?l.slice(M+1).reduce((j,lr)=>j*lr,1):1,Z=Math.floor(E/V);E%=V,B+=Z*y[M]}B+=_*y[t];let v=b;for(let M=t+1;M<o;M++){let V=M<o-1?m.slice(M-t).reduce((j,lr)=>j*lr,1):1,Z=Math.floor(v/V);v%=V,B+=Z*y[M]}let $=Number(r.data[B])!==0?1:0;e==="big"?h|=$<<7-w:h|=$<<w}}let S=0,D=g;for(let w=0;w<t;w++){let _=w<t-1?l.slice(w+1).reduce((E,v)=>E*v,1):1,B=Math.floor(D/_);D%=_,S+=B*d[w]}S+=A*d[t];let x=b;for(let w=t+1;w<o;w++){let _=w<o-1?m.slice(w-t).reduce((E,v)=>E*v,1):1,B=Math.floor(x/_);x%=_,S+=B*d[w]}c[S]=h}return s}function mo(r,t=-1,e=-1,n="big"){if(r.dtype!=="uint8")throw new TypeError("Expected an input array of unsigned byte data type");let o=Array.from(r.shape),a=o.length;if(t<0&&(t=a+t),t<0||t>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=o[t],u=i*8;e>=0&&(u=e);let s=[...o];s[t]=u;let c=N.zeros(s,"uint8"),l=c.data;if(a===1){for(let b=0;b<i;b++){let A=Number(r.data[b]);for(let h=0;h<8;h++){let S=b*8+h;if(S>=u)break;n==="big"?l[S]=A>>7-h&1:l[S]=A>>h&1}}return c}let m=o.slice(0,t),f=o.slice(t+1),p=m.reduce((b,A)=>b*A,1),y=f.reduce((b,A)=>b*A,1),d=Cr(o),g=Cr(s);for(let b=0;b<p;b++)for(let A=0;A<y;A++)for(let h=0;h<i;h++){let S=0,D=b;for(let _=0;_<t;_++){let B=_<t-1?m.slice(_+1).reduce((v,$)=>v*$,1):1,E=Math.floor(D/B);D%=B,S+=E*d[_]}S+=h*d[t];let x=A;for(let _=t+1;_<a;_++){let B=_<a-1?f.slice(_-t).reduce((v,$)=>v*$,1):1,E=Math.floor(x/B);x%=B,S+=E*d[_]}let w=Number(r.data[S]);for(let _=0;_<8;_++){let B=h*8+_;if(B>=u)break;let E=0;D=b;for(let v=0;v<t;v++){let $=v<t-1?m.slice(v+1).reduce((V,Z)=>V*Z,1):1,M=Math.floor(D/$);D%=$,E+=M*g[v]}E+=B*g[t],x=A;for(let v=t+1;v<a;v++){let $=v<a-1?f.slice(v-t).reduce((V,Z)=>V*Z,1):1,M=Math.floor(x/$);x%=$,E+=M*g[v]}n==="big"?l[E]=w>>7-_&1:l[E]=w>>_&1}}return c}function Cr(r){let t=r.length,e=new Array(t),n=1;for(let o=t-1;o>=0;o--)e[o]=n,n*=r[o];return e}function Dt(r,t=-1){let e=r.shape,n=e.length,o=r.dtype,a=r.data;if(n===0)return r.copy();let i=t;if(i<0&&(i=n+i),i<0||i>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let u=r.copy(),s=u.data,c=e[i],l=Array.from(e).filter((f,p)=>p!==i),m=l.length===0?1:l.reduce((f,p)=>f*p,1);if(I(o)){let f=a,p=s;for(let y=0;y<m;y++){let d=[];for(let g=0;g<c;g++){let b=U(y,i,g,e),A=O(b,e);d.push({value:f[A],idx:g})}d.sort((g,b)=>g.value<b.value?-1:g.value>b.value?1:0);for(let g=0;g<c;g++){let b=U(y,i,g,e),A=O(b,e);p[A]=d[g].value}}}else for(let f=0;f<m;f++){let p=[];for(let y=0;y<c;y++){let d=U(f,i,y,e),g=O(d,e);p.push(Number(a[g]))}p.sort((y,d)=>isNaN(y)&&isNaN(d)?0:isNaN(y)?1:isNaN(d)?-1:y-d);for(let y=0;y<c;y++){let d=U(f,i,y,e),g=O(d,e);s[g]=p[y]}}return u}function xt(r,t=-1){let e=r.shape,n=e.length,o=r.dtype,a=r.data;if(n===0)return N.zeros([0],"int32");let i=t;if(i<0&&(i=n+i),i<0||i>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let u=N.zeros(Array.from(e),"int32"),s=u.data,c=e[i],l=Array.from(e).filter((f,p)=>p!==i),m=l.length===0?1:l.reduce((f,p)=>f*p,1);if(I(o)){let f=a;for(let p=0;p<m;p++){let y=[];for(let d=0;d<c;d++){let g=U(p,i,d,e),b=O(g,e);y.push({value:f[b],idx:d})}y.sort((d,g)=>d.value<g.value?-1:d.value>g.value?1:0);for(let d=0;d<c;d++){let g=U(p,i,d,e),b=O(g,e);s[b]=y[d].idx}}}else for(let f=0;f<m;f++){let p=[];for(let y=0;y<c;y++){let d=U(f,i,y,e),g=O(d,e);p.push({value:Number(a[g]),idx:y})}p.sort((y,d)=>isNaN(y.value)&&isNaN(d.value)?0:isNaN(y.value)?1:isNaN(d.value)?-1:y.value-d.value);for(let y=0;y<c;y++){let d=U(f,i,y,e),g=O(d,e);s[g]=p[y].idx}}return u}function po(r){if(r.length===0)return N.zeros([0],"int32");let e=r[0].size;for(let i of r){if(i.ndim!==1)throw new Error("keys must be 1D arrays");if(i.size!==e)throw new Error("all keys must have the same length")}let n=[];for(let i=0;i<e;i++)n.push(i);n.sort((i,u)=>{for(let s=r.length-1;s>=0;s--){let l=r[s].data,m=Number(l[i]),f=Number(l[u]);if(!(isNaN(m)&&isNaN(f))){if(isNaN(m))return 1;if(isNaN(f)||m<f)return-1;if(m>f)return 1}}return 0});let o=N.zeros([e],"int32"),a=o.data;for(let i=0;i<e;i++)a[i]=n[i];return o}function Xa(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e],i=r[o],u=r[n],s;a<=i&&i<=u||u<=i&&i<=a?s=o:i<=a&&a<=u||u<=a&&a<=i?s=e:s=n;let c=r[s];[r[s],r[n]]=[r[n],r[s]];let l=e;for(let m=e;m<n;m++){let f=r[m],p=isNaN(f),y=isNaN(c);!p&&(y||f<=c)&&([r[l],r[m]]=[r[m],r[l]],l++)}if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function Ka(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e],i=r[o],u=r[n],s;a<=i&&i<=u||u<=i&&i<=a?s=o:i<=a&&a<=u||u<=a&&a<=i?s=e:s=n;let c=r[s];[r[s],r[n]]=[r[n],r[s]];let l=e;for(let m=e;m<n;m++)r[m]<=c&&([r[l],r[m]]=[r[m],r[l]],l++);if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function rs(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e].value,i=r[o].value,u=r[n].value,s;a<=i&&i<=u||u<=i&&i<=a?s=o:i<=a&&a<=u||u<=a&&a<=i?s=e:s=n;let c=r[s].value;[r[s],r[n]]=[r[n],r[s]];let l=e;for(let m=e;m<n;m++){let f=r[m].value,p=isNaN(f),y=isNaN(c);!p&&(y||f<=c)&&([r[l],r[m]]=[r[m],r[l]],l++)}if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function ts(r,t){let e=0,n=r.length-1;for(;e<n;){let o=Math.floor((e+n)/2),a=r[e].value,i=r[o].value,u=r[n].value,s;a<=i&&i<=u||u<=i&&i<=a?s=o:i<=a&&a<=u||u<=a&&a<=i?s=e:s=n;let c=r[s].value;[r[s],r[n]]=[r[n],r[s]];let l=e;for(let m=e;m<n;m++)r[m].value<=c&&([r[l],r[m]]=[r[m],r[l]],l++);if([r[l],r[n]]=[r[n],r[l]],l===t)return;l<t?e=l+1:n=l-1}}function wt(r,t,e=-1){let n=r.shape,o=n.length,a=r.dtype;if(o===0)return r.copy();let i=e;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let u=n[i],s=t;if(s<0&&(s=u+s),s<0||s>=u)throw new Error(`kth(=${t}) out of bounds (${u})`);let c=r.copy(),l=c.data,m=Array.from(n).filter((p,y)=>y!==i),f=m.length===0?1:m.reduce((p,y)=>p*y,1);if(I(a)){let p=l;for(let y=0;y<f;y++){let d=[];for(let g=0;g<u;g++){let b=U(y,i,g,n),A=O(b,n);d.push(p[A])}Ka(d,s);for(let g=0;g<u;g++){let b=U(y,i,g,n),A=O(b,n);p[A]=d[g]}}}else for(let p=0;p<f;p++){let y=[];for(let d=0;d<u;d++){let g=U(p,i,d,n),b=O(g,n);y.push(Number(l[b]))}Xa(y,s);for(let d=0;d<u;d++){let g=U(p,i,d,n),b=O(g,n);l[b]=y[d]}}return c}function It(r,t,e=-1){let n=r.shape,o=n.length,a=r.dtype,i=r.data;if(o===0)return N.zeros([0],"int32");let u=e;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let s=n[u],c=t;if(c<0&&(c=s+c),c<0||c>=s)throw new Error(`kth(=${t}) out of bounds (${s})`);let l=N.zeros(Array.from(n),"int32"),m=l.data,f=Array.from(n).filter((y,d)=>d!==u),p=f.length===0?1:f.reduce((y,d)=>y*d,1);if(I(a)){let y=i;for(let d=0;d<p;d++){let g=[];for(let b=0;b<s;b++){let A=U(d,u,b,n),h=O(A,n);g.push({value:y[h],idx:b})}ts(g,c);for(let b=0;b<s;b++){let A=U(d,u,b,n),h=O(A,n);m[h]=g[b].idx}}}else for(let y=0;y<p;y++){let d=[];for(let g=0;g<s;g++){let b=U(y,u,g,n),A=O(b,n);d.push({value:Number(i[A]),idx:g})}rs(d,c);for(let g=0;g<s;g++){let b=U(y,u,g,n),A=O(b,n);m[A]=d[g].idx}}return l}function yo(r){let t=r.dtype,e=r.size,n=r.data,o=[];for(let u=0;u<e;u++)o.push(Number(n[u]));o.sort((u,s)=>isNaN(u)&&isNaN(s)?0:isNaN(u)?1:isNaN(s)?-1:u-s);let a=N.zeros([e],t),i=a.data;for(let u=0;u<e;u++)i[u]=o[u];return a}function Vr(r){let t=r.shape,e=t.length,n=r.data,o=r.size,a=[];for(let l=0;l<e;l++)a.push([]);let i=[],u=1;for(let l=e-1;l>=0;l--)i.unshift(u),u*=t[l];for(let l=0;l<o;l++)if(n[l]){let m=l;for(let f=0;f<e;f++){let p=Math.floor(m/i[f]);m=m%i[f],a[f].push(p)}}let s=a[0]?.length??0,c=[];for(let l=0;l<e;l++){let m=N.zeros([s],"int32"),f=m.data;for(let p=0;p<s;p++)f[p]=a[l][p];c.push(m)}return c}function go(r){let t=r.data,e=r.size,n=[];for(let i=0;i<e;i++)t[i]&&n.push(i);let o=N.zeros([n.length],"int32"),a=o.data;for(let i=0;i<n.length;i++)a[i]=n[i];return o}function ho(r,t,e){if(t===void 0&&e===void 0)return Vr(r);if(t===void 0||e===void 0)throw new Error("either both or neither of x and y should be given");let n=r.shape,o=t.shape,a=e.shape,i=Math.max(n.length,o.length,a.length),u=B=>{let E=Array(i).fill(1);for(let v=0;v<B.length;v++)E[i-B.length+v]=B[v];return E},s=u(n),c=u(o),l=u(a),m=[];for(let B=0;B<i;B++){let E=[s[B],c[B],l[B]],v=Math.max(...E);for(let $ of E)if($!==1&&$!==v)throw new Error("operands could not be broadcast together");m.push(v)}let f=t.dtype,p=N.zeros(m,f),y=p.data,d=r.data,g=t.data,b=e.data,A=(B,E)=>{let v=[],$=1;for(let M=B.length-1;M>=0;M--)v.unshift($),$*=B[M];for(;v.length<E.length;)v.unshift(0);for(let M=0;M<E.length;M++)E[M]===1&&m[M]!==1&&(v[M]=0);return v},h=A(n,s),S=A(o,c),D=A(a,l),x=[],w=1;for(let B=m.length-1;B>=0;B--)x.unshift(w),w*=m[B];let _=m.reduce((B,E)=>B*E,1);for(let B=0;B<_;B++){let E=B,v=0,$=0,M=0;for(let V=0;V<i;V++){let Z=Math.floor(E/x[V]);E=E%x[V],v+=Z*h[V],$+=Z*S[V],M+=Z*D[V]}d[v]?y[B]=g[$]:y[B]=b[M]}return p}function zt(r,t,e="left"){if(r.ndim!==1)throw new Error("storage must be 1D");let n=r.data,o=r.size,a=t.data,i=t.size,u=N.zeros([i],"int32"),s=u.data;for(let c=0;c<i;c++){let l=Number(a[c]),m=0,f=o;if(e==="left")for(;m<f;){let p=Math.floor((m+f)/2);Number(n[p])<l?m=p+1:f=p}else for(;m<f;){let p=Math.floor((m+f)/2);Number(n[p])<=l?m=p+1:f=p}s[c]=m}return u}function Ao(r,t){let e=r.data,n=t.data,o=t.dtype,a=Math.min(r.size,t.size),i=0;for(let l=0;l<a;l++)e[l]&&i++;let u=N.zeros([i],o),s=u.data,c=0;if(I(o)){let l=n,m=s;for(let f=0;f<a;f++)e[f]&&(m[c++]=l[f])}else for(let l=0;l<a;l++)e[l]&&(s[c++]=n[l]);return u}function _t(r,t){let e=r.shape,n=e.length,o=r.data,a=r.size;if(t===void 0){let f=0;for(let p=0;p<a;p++)o[p]&&f++;return f}let i=t;if(i<0&&(i=n+i),i<0||i>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);let u=Array.from(e).filter((f,p)=>p!==i);if(u.length===0)return _t(r);let s=N.zeros(u,"int32"),c=s.data,l=e[i],m=u.reduce((f,p)=>f*p,1);for(let f=0;f<m;f++){let p=0;for(let y=0;y<l;y++){let d=U(f,i,y,e),g=O(d,e);o[g]&&p++}c[f]=p}return s}function bo(r){if(!isFinite(r))return r;let t=Math.floor(r),e=r-t;return Math.abs(e-.5)<1e-10?t%2===0?t:t+1:Math.round(r)}function Bt(r,t=0){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,i=e==="float32"?"float32":"float64",u=N.zeros(n,i),s=u.data,c=Math.pow(10,t);for(let l=0;l<a;l++){let m=Number(o[l]);s[l]=bo(m*c)/c}return u}function Et(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",i=N.zeros(e,a),u=i.data;for(let s=0;s<o;s++)u[s]=Math.ceil(Number(n[s]));return i}function vt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",i=N.zeros(e,a),u=i.data;for(let s=0;s<o;s++)u[s]=Math.trunc(Number(n[s]));return i}function Ot(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",i=N.zeros(e,a),u=i.data;for(let s=0;s<o;s++)u[s]=Math.floor(Number(n[s]));return i}function Ut(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",i=N.zeros(e,a),u=i.data;for(let s=0;s<o;s++)u[s]=bo(Number(n[s]));return i}function Tt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=t==="float32"?"float32":"float64",i=N.zeros(e,a),u=i.data;for(let s=0;s<o;s++)u[s]=Math.trunc(Number(n[s]));return i}function J(r,t=!1,e=!1,n=!1){let o=r.dtype,a=r.size,i=r.data,u=[];for(let h=0;h<a;h++)u.push({value:Number(i[h]),index:h});u.sort((h,S)=>isNaN(h.value)&&isNaN(S.value)?0:isNaN(h.value)?1:isNaN(S.value)?-1:h.value-S.value);let s=[],c=[],l=new Array(a),m=[],f,p=0;for(let h=0;h<u.length;h++){let{value:S,index:D}=u[h];f===void 0||isNaN(S)&&!isNaN(f)||!isNaN(S)&&isNaN(f)||!isNaN(S)&&!isNaN(f)&&S!==f?(f!==void 0&&m.push(p),s.push(S),c.push(D),p=1,f=S):p++}p>0&&m.push(p);let y=new Map,d=-1;for(let h=0;h<s.length;h++){let S=s[h];isNaN(S)?d=h:y.set(S,h)}for(let h=0;h<a;h++){let S=Number(i[h]);isNaN(S)?l[h]=d:l[h]=y.get(S)}let g=N.zeros([s.length],o),b=g.data;for(let h=0;h<s.length;h++)b[h]=s[h];if(!t&&!e&&!n)return g;let A={values:g};if(t){let h=N.zeros([c.length],"int32"),S=h.data;for(let D=0;D<c.length;D++)S[D]=c[D];A.indices=h}if(e){let h=N.zeros([l.length],"int32"),S=h.data;for(let D=0;D<l.length;D++)S[D]=l[D];A.inverse=h}if(n){let h=N.zeros([m.length],"int32"),S=h.data;for(let D=0;D<m.length;D++)S[D]=m[D];A.counts=h}return A}function So(r,t){return $t(r,t)}function No(r,t){let e=r.dtype,n=J(r),o=J(t),a=new Set;for(let c=0;c<o.size;c++)a.add(Number(o.data[c]));let i=[];for(let c=0;c<n.size;c++){let l=Number(n.data[c]);a.has(l)&&i.push(l)}i.sort((c,l)=>c-l);let u=N.zeros([i.length],e),s=u.data;for(let c=0;c<i.length;c++)s[c]=i[c];return u}function $t(r,t){let e=Array.from(r.shape),n=r.size,o=new Set;for(let s=0;s<t.size;s++)o.add(Number(t.data[s]));let a=N.zeros(e,"bool"),i=a.data,u=r.data;for(let s=0;s<n;s++){let c=Number(u[s]);i[s]=o.has(c)?1:0}return a}function Do(r,t){let e=r.dtype,n=J(r),o=new Set;for(let s=0;s<t.size;s++)o.add(Number(t.data[s]));let a=[];for(let s=0;s<n.size;s++){let c=Number(n.data[s]);o.has(c)||a.push(c)}let i=N.zeros([a.length],e),u=i.data;for(let s=0;s<a.length;s++)u[s]=a[s];return i}function xo(r,t){let e=r.dtype,n=J(r),o=J(t),a=new Set,i=new Set;for(let l=0;l<n.size;l++)a.add(Number(n.data[l]));for(let l=0;l<o.size;l++)i.add(Number(o.data[l]));let u=[];for(let l of a)i.has(l)||u.push(l);for(let l of i)a.has(l)||u.push(l);u.sort((l,m)=>l-m);let s=N.zeros([u.length],e),c=s.data;for(let l=0;l<u.length;l++)c[l]=u[l];return s}function wo(r,t){let e=r.dtype,n=J(r),o=J(t),a=new Set;for(let c=0;c<n.size;c++)a.add(Number(n.data[c]));for(let c=0;c<o.size;c++)a.add(Number(o.data[c]));let i=Array.from(a);i.sort((c,l)=>c-l);let u=N.zeros([i.length],e),s=u.data;for(let c=0;c<i.length;c++)s[c]=i[c];return u}function Mt(r,t=1,e=-1){if(t<0)throw new Error(`order must be non-negative but got ${t}`);if(t===0)return r.copy();let n=Array.from(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}`);if(n[a]<t+1)throw new Error(`diff requires at least ${t+1} elements along axis ${e}, but got ${n[a]}`);let i=r;for(let u=0;u<t;u++)i=as(i,a);return i}function as(r,t){let e=Array.from(r.shape),n=e.length,o=e[t],a=[...e];a[t]=o-1;let i=r.dtype,u=I(i)?"float64":i,s=N.zeros(a,u),c=s.data,l=r.strides,m=s.size;for(let f=0;f<m;f++){let p=f,y=new Array(n);for(let D=n-1;D>=0;D--)y[D]=p%a[D],p=Math.floor(p/a[D]);let d=[...y],g=[...y];g[t]=d[t]+1;let b=0,A=0;for(let D=0;D<n;D++)b+=d[D]*l[D],A+=g[D]*l[D];let h=(I(i),Number(r.data[b])),S=(I(i),Number(r.data[A]));c[f]=S-h}return s}function Io(r,t=null,e=null){let n=r.size,o=r.dtype,a=I(o)?"float64":o,i=Math.max(0,n-1),u=e?e.length:0,s=t?t.length:0,c=u+i+s,l=N.zeros([c],a),m=l.data,f=0;if(e)for(let p of e)m[f++]=p;for(let p=0;p<i;p++){let y=(I(o),Number(r.iget(p))),d=(I(o),Number(r.iget(p+1)));m[f++]=d-y}if(t)for(let p of t)m[f++]=p;return l}function zo(r,t=1,e=null){let o=Array.from(r.shape).length,a;if(e===null)a=Array.from({length:o},(s,c)=>c);else if(typeof e=="number"){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}`);a=[s]}else a=e.map(s=>{let c=s<0?o+s:s;if(c<0||c>=o)throw new Error(`axis ${s} is out of bounds for array of dimension ${o}`);return c});let i;if(typeof t=="number")i=a.map(()=>t);else{if(t.length!==a.length)throw new Error("Number of spacings must match number of axes");i=t}let u=[];for(let s=0;s<a.length;s++)u.push(ss(r,a[s],i[s]));return u.length===1?u[0]:u}function ss(r,t,e){let n=Array.from(r.shape),o=n.length,a=n[t];if(a<2)throw new Error(`Shape of array along axis ${t} must be at least 2, but got ${a}`);let i=r.dtype,u=I(i)?"float64":i==="float32"?"float32":"float64",s=N.zeros(n,u),c=s.data,l=r.strides,m=e,f=2*m,p=r.size;for(let y=0;y<p;y++){let d=y,g=new Array(o);for(let h=o-1;h>=0;h--)g[h]=d%n[h],d=Math.floor(d/n[h]);let b=g[t],A;if(b===0){let h=[...g];h[t]=1;let S=0;for(let w=0;w<o;w++)S+=h[w]*l[w];let D=(I(i),Number(r.data[y]));A=((I(i),Number(r.data[S]))-D)/m}else if(b===a-1){let h=[...g];h[t]=a-2;let S=0;for(let w=0;w<o;w++)S+=h[w]*l[w];let D=(I(i),Number(r.data[y])),x=(I(i),Number(r.data[S]));A=(D-x)/m}else{let h=[...g],S=[...g];h[t]=b+1,S[t]=b-1;let D=0,x=0;for(let B=0;B<o;B++)D+=h[B]*l[B],x+=S[B]*l[B];let w=(I(i),Number(r.data[D])),_=(I(i),Number(r.data[x]));A=(w-_)/f}c[y]=A}return s}function _o(r,t,e=-1,n=-1,o=-1){let a=Array.from(r.shape),i=Array.from(t.shape),u=a.length,s=i.length,c=e<0?u+e:e,l=n<0?s+n:n;if(c<0||c>=u)throw new Error(`axisa ${e} is out of bounds for array of dimension ${u}`);if(l<0||l>=s)throw new Error(`axisb ${n} is out of bounds for array of dimension ${s}`);let m=a[c],f=i[l];if(m!==2&&m!==3)throw new Error(`incompatible dimensions for cross product (dimension must be 2 or 3, got ${m})`);if(f!==2&&f!==3)throw new Error(`incompatible dimensions for cross product (dimension must be 2 or 3, got ${f})`);let p=k(r.dtype,t.dtype);if(u===1&&s===1&&m===3&&f===3){let y=N.zeros([3],p),d=y.data,g=Number(r.iget(0)),b=Number(r.iget(1)),A=Number(r.iget(2)),h=Number(t.iget(0)),S=Number(t.iget(1)),D=Number(t.iget(2));return d[0]=b*D-A*S,d[1]=A*h-g*D,d[2]=g*S-b*h,y}if(u===1&&s===1&&m===2&&f===2){let y=N.zeros([],p),d=Number(r.iget(0)),g=Number(r.iget(1)),b=Number(t.iget(0)),A=Number(t.iget(1));return y.data[0]=d*A-g*b,y}if(u===1&&s===1){if(m===2&&f===3){let y=N.zeros([3],p),d=y.data,g=Number(r.iget(0)),b=Number(r.iget(1)),A=Number(t.iget(0)),h=Number(t.iget(1)),S=Number(t.iget(2));return d[0]=b*S,d[1]=-g*S,d[2]=g*h-b*A,y}else if(m===3&&f===2){let y=N.zeros([3],p),d=y.data,g=Number(r.iget(0)),b=Number(r.iget(1)),A=Number(r.iget(2)),h=Number(t.iget(0)),S=Number(t.iget(1));return d[0]=-A*S,d[1]=A*h,d[2]=g*S-b*h,y}}if(u===2&&s===2&&c===1&&l===1){let y=a[0];if(i[0]!==y)throw new Error(`Shape mismatch: a has ${y} vectors, b has ${i[0]} vectors`);if(m===3&&f===3){let d=N.zeros([y,3],p),g=d.data;for(let b=0;b<y;b++){let A=Number(r.iget(b*3)),h=Number(r.iget(b*3+1)),S=Number(r.iget(b*3+2)),D=Number(t.iget(b*3)),x=Number(t.iget(b*3+1)),w=Number(t.iget(b*3+2));g[b*3]=h*w-S*x,g[b*3+1]=S*D-A*w,g[b*3+2]=A*x-h*D}return d}if(m===2&&f===2){let d=N.zeros([y],p),g=d.data;for(let b=0;b<y;b++){let A=Number(r.iget(b*2)),h=Number(r.iget(b*2+1)),S=Number(t.iget(b*2)),D=Number(t.iget(b*2+1));g[b]=A*D-h*S}return d}}throw new Error(`cross product not implemented for arrays with shapes ${JSON.stringify(a)} and ${JSON.stringify(i)}`)}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((i,u)=>{let s=i;if(s<0&&(s=this.shape[u]+s),s<0||s>=this.shape[u])throw new Error(`Index ${i} is out of bounds for axis ${u} with size ${this.shape[u]}`);return s}),o=this.dtype,a;I(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,i=T(t);if(!i)throw new Error(`Cannot convert to dtype ${t}`);let u=new i(a),s=this.data;if(I(n)&&!I(t)){let l=s;if(t==="bool")for(let m=0;m<a;m++)u[m]=l[m]!==BigInt(0)?1:0;else for(let m=0;m<a;m++)u[m]=Number(l[m])}else if(!I(n)&&I(t)){let l=s;for(let m=0;m<a;m++)u[m]=BigInt(Math.round(Number(l[m])))}else if(t==="bool"){let l=s;for(let m=0;m<a;m++)u[m]=l[m]!==0?1:0}else if(n==="bool"&&!I(t)){let l=s;for(let m=0;m<a;m++)u[m]=l[m]}else if(!I(n)&&!I(t)){let l=s;for(let m=0;m<a;m++)u[m]=l[m]}else{let l=s;for(let m=0;m<a;m++)u[m]=l[m]}let c=N.fromData(u,o,t);return new r(c)}add(t){let e=typeof t=="number"?t:t._storage,n=Qt(this._storage,e);return r._fromStorage(n)}subtract(t){let e=typeof t=="number"?t:t._storage,n=Xt(this._storage,e);return r._fromStorage(n)}multiply(t){let e=typeof t=="number"?t:t._storage,n=Kt(this._storage,e);return r._fromStorage(n)}divide(t){let e=typeof t=="number"?t:t._storage,n=re(this._storage,e);return r._fromStorage(n)}mod(t){let e=typeof t=="number"?t:t._storage,n=Dr(this._storage,e);return r._fromStorage(n)}floor_divide(t){let e=typeof t=="number"?t:t._storage,n=Lr(this._storage,e);return r._fromStorage(n)}positive(){let t=oe(this._storage);return r._fromStorage(t)}reciprocal(){let t=ae(this._storage);return r._fromStorage(t)}sqrt(){let t=fn(this._storage);return r._fromStorage(t)}power(t){let e=typeof t=="number"?t:t._storage,n=mn(this._storage,e);return r._fromStorage(n)}exp(){let t=pn(this._storage);return r._fromStorage(t)}exp2(){let t=yn(this._storage);return r._fromStorage(t)}expm1(){let t=dn(this._storage);return r._fromStorage(t)}log(){let t=gn(this._storage);return r._fromStorage(t)}log2(){let t=hn(this._storage);return r._fromStorage(t)}log10(){let t=An(this._storage);return r._fromStorage(t)}log1p(){let t=bn(this._storage);return r._fromStorage(t)}logaddexp(t){let e=typeof t=="number"?t:t._storage,n=Sn(this._storage,e);return r._fromStorage(n)}logaddexp2(t){let e=typeof t=="number"?t:t._storage,n=Nn(this._storage,e);return r._fromStorage(n)}absolute(){let t=te(this._storage);return r._fromStorage(t)}negative(){let t=ee(this._storage);return r._fromStorage(t)}sign(){let t=ne(this._storage);return r._fromStorage(t)}around(t=0){let e=Bt(this._storage,t);return r._fromStorage(e)}round(t=0){return this.around(t)}ceil(){let t=Et(this._storage);return r._fromStorage(t)}fix(){let t=vt(this._storage);return r._fromStorage(t)}floor(){let t=Ot(this._storage);return r._fromStorage(t)}rint(){let t=Ut(this._storage);return r._fromStorage(t)}trunc(){let t=Tt(this._storage);return r._fromStorage(t)}sin(){let t=Dn(this._storage);return r._fromStorage(t)}cos(){let t=xn(this._storage);return r._fromStorage(t)}tan(){let t=wn(this._storage);return r._fromStorage(t)}arcsin(){let t=In(this._storage);return r._fromStorage(t)}arccos(){let t=zn(this._storage);return r._fromStorage(t)}arctan(){let t=_n(this._storage);return r._fromStorage(t)}arctan2(t){let e=typeof t=="number"?t:t._storage,n=Bn(this._storage,e);return r._fromStorage(n)}hypot(t){let e=typeof t=="number"?t:t._storage,n=En(this._storage,e);return r._fromStorage(n)}degrees(){let t=vn(this._storage);return r._fromStorage(t)}radians(){let t=On(this._storage);return r._fromStorage(t)}sinh(){let t=Un(this._storage);return r._fromStorage(t)}cosh(){let t=Tn(this._storage);return r._fromStorage(t)}tanh(){let t=$n(this._storage);return r._fromStorage(t)}arcsinh(){let t=Mn(this._storage);return r._fromStorage(t)}arccosh(){let t=kn(this._storage);return r._fromStorage(t)}arctanh(){let t=Cn(this._storage);return r._fromStorage(t)}greater(t){let e=typeof t=="number"?t:t._storage,n=me(this._storage,e);return r._fromStorage(n)}greater_equal(t){let e=typeof t=="number"?t:t._storage,n=pe(this._storage,e);return r._fromStorage(n)}less(t){let e=typeof t=="number"?t:t._storage,n=ye(this._storage,e);return r._fromStorage(n)}less_equal(t){let e=typeof t=="number"?t:t._storage,n=de(this._storage,e);return r._fromStorage(n)}equal(t){let e=typeof t=="number"?t:t._storage,n=ge(this._storage,e);return r._fromStorage(n)}not_equal(t){let e=typeof t=="number"?t:t._storage,n=he(this._storage,e);return r._fromStorage(n)}isclose(t,e=1e-5,n=1e-8){let o=typeof t=="number"?t:t._storage,a=Wr(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 Ae(this._storage,o,e,n)}bitwise_and(t){let e=typeof t=="number"?t:t._storage,n=ao(this._storage,e);return r._fromStorage(n)}bitwise_or(t){let e=typeof t=="number"?t:t._storage,n=so(this._storage,e);return r._fromStorage(n)}bitwise_xor(t){let e=typeof t=="number"?t:t._storage,n=io(this._storage,e);return r._fromStorage(n)}bitwise_not(){let t=Nt(this._storage);return r._fromStorage(t)}invert(){let t=uo(this._storage);return r._fromStorage(t)}left_shift(t){let e=typeof t=="number"?t:t._storage,n=co(this._storage,e);return r._fromStorage(n)}right_shift(t){let e=typeof t=="number"?t:t._storage,n=lo(this._storage,e);return r._fromStorage(n)}sum(t,e=!1){let n=mr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}mean(t,e=!1){let n=xr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}max(t,e=!1){let n=wr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}min(t,e=!1){let n=Ir(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=Hr(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}argmax(t){let e=Yr(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}var(t,e=0,n=!1){let o=Jr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}std(t,e=0,n=!1){let o=Se(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}all(t,e=!1){let n=Qr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}any(t,e=!1){let n=Xr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}cumsum(t){return r._fromStorage(Kr(this._storage,t))}cumprod(t){return r._fromStorage(rt(this._storage,t))}ptp(t,e=!1){let n=tt(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}median(t,e=!1){let n=et(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}percentile(t,e,n=!1){let o=nt(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}quantile(t,e,n=!1){let o=ar(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}average(t,e){let n=zr(this._storage,e,t?.storage);return typeof n=="number"?n:r._fromStorage(n)}nansum(t,e=!1){let n=_r(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanprod(t,e=!1){let n=Br(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanmean(t,e=!1){let n=Er(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanvar(t,e=0,n=!1){let o=pr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}nanstd(t,e=0,n=!1){let o=ot(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}nanmin(t,e=!1){let n=vr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanmax(t,e=!1){let n=Or(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}nanargmin(t){let e=Ur(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}nanargmax(t){let e=Tr(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}nancumsum(t){return r._fromStorage(at(this._storage,t))}nancumprod(t){return r._fromStorage(st(this._storage,t))}nanmedian(t,e=!1){let n=$r(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}sort(t=-1){return r._fromStorage(Dt(this._storage,t))}argsort(t=-1){return r._fromStorage(xt(this._storage,t))}partition(t,e=-1){return r._fromStorage(wt(this._storage,t,e))}argpartition(t,e=-1){return r._fromStorage(It(this._storage,t,e))}nonzero(){return Vr(this._storage).map(e=>r._fromStorage(e))}searchsorted(t,e="left"){return r._fromStorage(zt(this._storage,t._storage,e))}diff(t=1,e=-1){return r._fromStorage(Mt(this._storage,t,e))}reshape(...t){let e=t.length===1&&Array.isArray(t[0])?t[0]:t,n=P(this._storage,e),a=n.data===this.data?this._base??this:void 0;return r._fromStorage(n,a)}flatten(){let t=Mr(this._storage);return r._fromStorage(t)}ravel(){let t=sr(this._storage),n=t.data===this.data?this._base??this:void 0;return r._fromStorage(t,n)}transpose(t){let e=dr(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}squeeze(t){let e=Ne(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}expand_dims(t){let e=yr(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}swapaxes(t,e){let n=De(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}moveaxis(t,e){let n=it(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}repeat(t,e){let n=Oe(this._storage,t,e);return r._fromStorage(n)}take(t,e){let n=qn(this._storage,t,e);return r._fromStorage(n)}put(t,e){let n=e instanceof r?e._storage:e;Fn(this._storage,t,n)}matmul(t){let e=G(this._storage,t._storage);return r._fromStorage(e)}dot(t){let e=lt(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}trace(){return Ze(this._storage)}inner(t){let e=We(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}outer(t){let e=ft(this._storage,t._storage);return r._fromStorage(e)}tensordot(t,e=2){let n=Ge(this._storage,t._storage,e);return typeof n=="number"||typeof n=="bigint"?n:r._fromStorage(n)}cbrt(){let t=se(this._storage);return r._fromStorage(t)}fabs(){let t=ie(this._storage);return r._fromStorage(t)}divmod(t){let e=typeof t=="number"?t:t._storage,[n,o]=ue(this._storage,e);return[r._fromStorage(n),r._fromStorage(o)]}square(){let t=ce(this._storage);return r._fromStorage(t)}remainder(t){let e=typeof t=="number"?t:t._storage,n=le(this._storage,e);return r._fromStorage(n)}heaviside(t){let e=typeof t=="number"?t:t._storage,n=fe(this._storage,e);return 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,c)=>{let l=Wt(s);return Gt(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 s=0;s<e.length;s++){let c=e[s],l=this._storage.strides[s];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=N.fromData(this._storage.data,n,this._storage.dtype,o,a),u=this._base??this;return new r(i,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 i=[];for(let u=0;u<t[a];u++)o[a]=u,i.push(n(o,a+1));return i};return n(new Array(e),0)}};function Q(r,t=q){let e=N.zeros(r,t);return new z(e)}function Bo(r,t=q){let e=N.ones(r,t);return new z(e)}function us(r){let t=[],e=r;for(;Array.isArray(e);)t.push(e.length),e=e[0];return t}function Eo(r){return typeof r=="bigint"?!0:Array.isArray(r)?r.some(t=>Eo(t)):!1}function cs(r){let t=[];function e(n){Array.isArray(n)?n.forEach(o=>e(o)):t.push(n)}return e(r),t}function L(r,t){if(r instanceof z)return!t||r.dtype===t?r.copy():r.astype(t);let e=Eo(r),n=us(r),o=n.reduce((l,m)=>l*m,1),a=t;a||(e?a="int64":a=q);let i=T(a);if(!i)throw new Error(`Cannot create array with dtype ${a}`);let u=new i(o),s=cs(r);if(I(a)){let l=u;for(let m=0;m<o;m++){let f=s[m];l[m]=typeof f=="bigint"?f:BigInt(Math.round(Number(f)))}}else if(a==="bool"){let l=u;for(let m=0;m<o;m++)l[m]=s[m]?1:0}else{let l=u;for(let m=0;m<o;m++){let f=s[m];l[m]=Number(f)}}let c=N.fromData(u,n,a);return new z(c)}function ls(r,t,e=1,n=q){let o=r,a=t;if(t===void 0&&(o=0,a=r),a===void 0)throw new Error("stop is required");let i=Math.max(0,Math.ceil((a-o)/e)),u=T(n);if(!u)throw new Error(`Cannot create arange array with dtype ${n}`);let s=new u(i);if(I(n))for(let l=0;l<i;l++)s[l]=BigInt(Math.round(o+l*e));else if(n==="bool")for(let l=0;l<i;l++)s[l]=o+l*e!==0?1:0;else for(let l=0;l<i;l++)s[l]=o+l*e;let c=N.fromData(s,[i],n);return new z(c)}function fs(r,t,e=50,n=q){if(e<0)throw new Error("num must be non-negative");if(e===0)return L([],n);if(e===1)return L([r],n);let o=T(n);if(!o)throw new Error(`Cannot create linspace array with dtype ${n}`);let a=new o(e),i=(t-r)/(e-1);if(I(n))for(let s=0;s<e;s++)a[s]=BigInt(Math.round(r+s*i));else if(n==="bool")for(let s=0;s<e;s++)a[s]=r+s*i!==0?1:0;else for(let s=0;s<e;s++)a[s]=r+s*i;let u=N.fromData(a,[e],n);return new z(u)}function ms(r,t,e=50,n=10,o=q){if(e<0)throw new Error("num must be non-negative");if(e===0)return L([],o);if(e===1)return L([Math.pow(n,r)],o);let a=T(o);if(!a)throw new Error(`Cannot create logspace array with dtype ${o}`);let i=new a(e),u=(t-r)/(e-1);if(I(o))for(let c=0;c<e;c++){let l=r+c*u;i[c]=BigInt(Math.round(Math.pow(n,l)))}else if(o==="bool")for(let c=0;c<e;c++){let l=r+c*u;i[c]=Math.pow(n,l)!==0?1:0}else for(let c=0;c<e;c++){let l=r+c*u;i[c]=Math.pow(n,l)}let s=N.fromData(i,[e],o);return new z(s)}function ps(r,t,e=50,n=q){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 L([],n);if(e===1)return L([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 i=T(n);if(!i)throw new Error(`Cannot create geomspace array with dtype ${n}`);let u=new i(e),s=Math.log(Math.abs(r)),l=(Math.log(Math.abs(t))-s)/(e-1);if(I(n))for(let f=0;f<e;f++){let p=o*Math.exp(s+f*l);u[f]=BigInt(Math.round(p))}else if(n==="bool")for(let f=0;f<e;f++){let p=o*Math.exp(s+f*l);u[f]=p!==0?1:0}else for(let f=0;f<e;f++){let p=o*Math.exp(s+f*l);u[f]=p}let m=N.fromData(u,[e],n);return new z(m)}function vo(r,t,e=0,n=q){let o=t??r,a=Q([r,o],n),i=a.data;if(I(n)){let u=i;for(let s=0;s<r;s++){let c=s+e;c>=0&&c<o&&(u[s*o+c]=BigInt(1))}}else{let u=i;for(let s=0;s<r;s++){let c=s+e;c>=0&&c<o&&(u[s*o+c]=1)}}return a}function Oo(r,t=q){return Q(r,t)}function Uo(r,t,e){let n=e;n||(typeof t=="bigint"?n="int64":typeof t=="boolean"?n="bool":Number.isInteger(t)?n="int32":n=q);let o=T(n);if(!o)throw new Error(`Cannot create full array with dtype ${n}`);let a=r.reduce((s,c)=>s*c,1),i=new o(a);if(I(n)){let s=typeof t=="bigint"?t:BigInt(Math.round(Number(t)));i.fill(s)}else n==="bool"?i.fill(t?1:0):i.fill(Number(t));let u=N.fromData(i,r,n);return new z(u)}function ys(r,t=q){return vo(r,r,0,t)}function qr(r,t){return r instanceof z?!t||r.dtype===t?r:r.astype(t):L(r,t)}function ds(r){return r.copy()}function gs(r,t){return Q(Array.from(r.shape),t??r.dtype)}function hs(r,t){return Bo(Array.from(r.shape),t??r.dtype)}function As(r,t){return Oo(Array.from(r.shape),t??r.dtype)}function bs(r,t,e){return Uo(Array.from(r.shape),t,e??r.dtype)}function Ss(r,t){return qr(r,t)}function Ns(r,t){let e=qr(r,t);return e.flags.C_CONTIGUOUS?e:e.copy()}function Ds(r,t){return qr(r,t).copy()}function To(r,t=0){if(r.ndim===1){let e=r.size,n=e+Math.abs(t),o=Q([n,n],r.dtype);for(let a=0;a<e;a++){let i=t>=0?a:a-t,u=t>=0?a+t:a;o.set([i,u],r.get([a]))}return o}else if(r.ndim===2){let[e,n]=r.shape,o,a,i;if(t>=0?(o=0,a=t,i=Math.min(e,n-t)):(o=-t,a=0,i=Math.min(e+t,n)),i<=0)return Q([0],r.dtype);let u=T(r.dtype),s=new u(i);for(let l=0;l<i;l++){let m=r.get([o+l,a+l]);I(r.dtype)?s[l]=typeof m=="bigint"?m:BigInt(m):s[l]=m}let c=N.fromData(s,[i],r.dtype);return new z(c)}else throw new Error("Input must be 1-D or 2-D")}function xs(r,t=0){let e=r.flatten();return To(e,t)}function ws(r,t,e=q){let n=t.reduce((c,l)=>c*l,1),o=T(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n),i=t.length,u=new Array(i).fill(0);for(let c=0;c<n;c++){let l=r(...u);I(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&&(u[m]++,!(u[m]<t[m]));m--)u[m]=0}let s=N.fromData(a,t,e);return new z(s)}function Is(...r){let t=[],e="xy";for(let u of r)u instanceof z?t.push(u):typeof u=="object"&&"indexing"in u&&(e=u.indexing||"xy");if(t.length===0)return[];if(t.length===1)return[t[0].copy()];let n=t.map(u=>u.size);e==="xy"&&t.length>=2&&(t=[t[1],t[0],...t.slice(2)],[n[0],n[1]]=[n[1],n[0]]);let o=n,a=o.length,i=[];for(let u=0;u<t.length;u++){let s=t[u],c=s.size,l=new Array(a).fill(1);l[u]=c;let m=s.reshape(...l),f=ht(m.storage,o),p=z._fromStorage(f.copy());i.push(p)}return e==="xy"&&i.length>=2&&([i[0],i[1]]=[i[1],i[0]]),i}function zs(r,t,e=0,n=q){let o=t??r,a=Q([r,o],n);for(let i=0;i<r;i++)for(let u=0;u<=i+e&&u<o;u++)u>=0&&a.set([i,u],1);return a}function _s(r,t=0){if(r.ndim<2)throw new Error("Input must have at least 2 dimensions");let e=r.copy(),n=e.shape,o=n[n.length-2],a=n[n.length-1],i=n.slice(0,-2).reduce((u,s)=>u*s,1);for(let u=0;u<i;u++)for(let s=0;s<o;s++)for(let c=0;c<a;c++)if(c>s+t){let l=[],m=u;for(let f=n.length-3;f>=0;f--)l.unshift(m%n[f]),m=Math.floor(m/n[f]);l.push(s,c),e.set(l,0)}return e}function Bs(r,t=0){if(r.ndim<2)throw new Error("Input must have at least 2 dimensions");let e=r.copy(),n=e.shape,o=n[n.length-2],a=n[n.length-1],i=n.slice(0,-2).reduce((u,s)=>u*s,1);for(let u=0;u<i;u++)for(let s=0;s<o;s++)for(let c=0;c<a;c++)if(c<s+t){let l=[],m=u;for(let f=n.length-3;f>=0;f--)l.unshift(m%n[f]),m=Math.floor(m/n[f]);l.push(s,c),e.set(l,0)}return e}function Es(r,t,e=!1){if(r.ndim!==1)throw new Error("Input must be 1-D");let n=r.size,o=t??n;if(o<0)throw new Error("N must be non-negative");let a=Q([n,o],r.dtype);for(let i=0;i<n;i++){let u=r.get([i]);for(let s=0;s<o;s++){let c=e?s:o-1-s;a.set([i,s],Math.pow(u,c))}}return a}function vs(r,t=q,e=-1,n=0){let o,a=n;r instanceof ArrayBuffer?o=r:(o=r.buffer,a+=r.byteOffset);let i=$s(t),u=o.byteLength-a,s=Math.floor(u/i),c=e<0?s:Math.min(e,s);if(c<=0)return L([],t);let l=T(t);if(!l)throw new Error(`Unsupported dtype: ${t}`);let m=new l(o,a,c),f=N.fromData(m,[c],t);return new z(f)}function Os(r,t=q,e=-1){let n=[],o=0;for(let a of r){if(e>=0&&o>=e)break;n.push(a),o++}return L(n,t)}function Us(r,t=q,e=-1){let n=[],o=0;for(let a of r){if(e>=0&&o>=e)break;n.push(a),o++}return L(n,t)}function Ts(r,t=q,e=-1,n=""){let o;n===""?o=r.trim().split(/\s+/):o=r.split(n);let a=[],i=0;for(let u of o){if(e>=0&&i>=e)break;let s=u.trim();s!==""&&(I(t)?a.push(BigInt(s)):a.push(parseFloat(s)),i++)}return L(a,t)}function $s(r){switch(r){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 Ms(r){return r.sqrt()}function ks(r,t){return r.power(t)}function Cs(r){return r.exp()}function Vs(r){return r.exp2()}function qs(r){return r.expm1()}function Fs(r){return r.log()}function Ps(r){return r.log2()}function js(r){return r.log10()}function Rs(r){return r.log1p()}function Ls(r,t){return r.logaddexp(t)}function Zs(r,t){return r.logaddexp2(t)}function Ws(r){return r.absolute()}function Gs(r){return r.negative()}function Hs(r){return r.sign()}function Ys(r,t){return r.mod(t)}function Js(r,t){return r.floor_divide(t)}function Qs(r){return r.positive()}function Xs(r){return r.reciprocal()}function Ks(r,t){return r.dot(t)}function ri(r){return r.trace()}function ti(r,t=0,e=0,n=1){let o=He(r.storage,t,e,n);return z._fromStorage(o)}function ei(r,t){let e=Je(r.storage,t.storage);return z._fromStorage(e)}function ni(r,t){return r.transpose(t)}function oi(r,t){return r.inner(t)}function ai(r,t){return r.outer(t)}function si(r,t,e=2){return r.tensordot(t,e)}function ii(r){return r.sin()}function ui(r){return r.cos()}function ci(r){return r.tan()}function li(r){return r.arcsin()}function fi(r){return r.arccos()}function mi(r){return r.arctan()}function pi(r,t){return r.arctan2(t)}function yi(r,t){return r.hypot(t)}function di(r){return r.degrees()}function gi(r){return r.radians()}function hi(r){return r.radians()}function Ai(r){return r.degrees()}function bi(r){return r.sinh()}function Si(r){return r.cosh()}function Ni(r){return r.tanh()}function Di(r){return r.arcsinh()}function xi(r){return r.arccosh()}function wi(r){return r.arctanh()}function Ii(r,t,e){return r.swapaxes(t,e)}function zi(r,t,e){return r.moveaxis(t,e)}function ur(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=er(e,t);return z._fromStorage(n)}function _i(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=xe(e,t);return z._fromStorage(n)}function $o(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=we(t);return z._fromStorage(e)}function Bi(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=ut(t);return z._fromStorage(e)}function Ei(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=Ie(t);return z._fromStorage(e)}function vi(r,t,e=0){return ze(r.storage,t,e).map(o=>z._fromStorage(o,r.base??r))}function Oi(r,t,e=0){return gr(r.storage,t,e).map(o=>z._fromStorage(o,r.base??r))}function Ui(r,t){return Be(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function Ti(r,t){return Ee(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function $i(r,t){let e=ve(r.storage,t);return z._fromStorage(e)}function Mi(r,t,e){return r.repeat(t,e)}function ki(r){return r.ravel()}function Ci(r,t){return r.reshape(...t)}function Vi(r,t){return r.squeeze(t)}function qi(r,t){return r.expand_dims(t)}function kt(r,t){let e=Ue(r.storage,t);return z._fromStorage(e)}function Fi(r){if(r.ndim<2)throw new Error("Input must be at least 2-D");return kt(r,1)}function Pi(r){if(r.ndim<2)throw new Error("Input must be at least 2-D");return kt(r,0)}function ji(r,t=1,e=[0,1]){let n=Te(r.storage,t,e);return z._fromStorage(n)}function Ri(r,t,e){let n=$e(r.storage,t,e);return z._fromStorage(n)}function Li(r,t,e=0){let n=Me(r.storage,t,e);return z._fromStorage(n,r.base??r)}function Zi(...r){let t=r.map(o=>o.storage),n=qe(t).map((o,a)=>o===t[a]?r[a]:z._fromStorage(o));return n.length===1?n[0]:n}function Wi(...r){let t=r.map(o=>o.storage),n=Fe(t).map((o,a)=>o===t[a]?r[a]:z._fromStorage(o));return n.length===1?n[0]:n}function Gi(...r){let t=r.map(o=>o.storage),n=Pe(t).map((o,a)=>o===t[a]?r[a]:z._fromStorage(o));return n.length===1?n[0]:n}function Hi(r,t){return ke(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function Yi(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=Ce(t);return z._fromStorage(e)}function Ji(r){return $o(r)}function Qi(r,t){let e=Ve(r.storage,t);return z._fromStorage(e)}function Xi(r,t,e){let n=t instanceof z?t:L(t,r.dtype);if(e===void 0){let o=r.flatten(),a=n.flatten();return ur([o,a])}return ur([r,n],e)}function Ki(r,t,e){let n=r.dtype;if(e===void 0){let p=r.flatten(),d=(Array.isArray(t)?t:[t]).map(S=>S<0?p.size+S:S),g=[];for(let S=0;S<p.size;S++)d.includes(S)||g.push(S);let b=T(n),A=new b(g.length);for(let S=0;S<g.length;S++){let D=p.get([g[S]]);I(n)?A[S]=typeof D=="bigint"?D:BigInt(D):A[S]=D}let h=N.fromData(A,[g.length],n);return new z(h)}let o=r.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 u=o[i],s=Array.isArray(t)?t:[t],c=new Set(s.map(p=>p<0?u+p:p)),l=[],m=0;for(let p=0;p<=u;p++)(c.has(p)||p===u)&&(p>m&&l.push([m,p]),m=p+1);if(l.length===0){let p=[...o];return p[i]=0,Q(p,n)}let f=[];for(let[p,y]of l){let d=o.map(()=>":");d[i]=`${p}:${y}`,f.push(r.slice(...d))}return ur(f,i)}function ru(r,t,e,n){let o=e instanceof z?e:L(e,r.dtype);if(n===void 0){let m=r.flatten(),f=o.flatten(),p=t<0?m.size+t:t;if(p<0||p>m.size)throw new Error(`index ${t} 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),ur(g)}let a=r.shape,i=a.length,u=n<0?i+n:n;if(u<0||u>=i)throw new Error(`axis ${n} is out of bounds for array of dimension ${i}`);let s=a[u],c=t<0?s+t:t;if(c<0||c>s)throw new Error(`index ${t} is out of bounds for axis ${n} with size ${s}`);let l=[];if(c>0){let m=a.map(()=>":");m[u]=`0:${c}`,l.push(r.slice(...m))}if(l.push(o),c<s){let m=a.map(()=>":");m[u]=`${c}:`,l.push(r.slice(...m))}return ur(l,u)}function tu(r,t,e="constant",n=0){let o=r.shape,a=o.length,i=r.dtype,u;if(typeof t=="number"?u=o.map(()=>[t,t]):Array.isArray(t)&&typeof t[0]=="number"?u=o.map(()=>t):u=t,u.length!==a)throw new Error(`pad_width must have ${a} elements`);let s=o.map((d,g)=>d+u[g][0]+u[g][1]),c=s.reduce((d,g)=>d*g,1),l=T(i),m=new l(c),f=I(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,b=[];for(let h=0;h<a;h++){let[S]=u[h],D=p[h]-S;if(D<0||D>=o[h]){g=!1;break}b.push(D)}let A;if(g)A=r.get(b);else if(e==="constant"){for(let h=a-1;h>=0&&(p[h]++,!(p[h]<s[h]));h--)p[h]=0;continue}else{let h=[];for(let S=0;S<a;S++){let[D]=u[S],x=p[S]-D,w=o[S];x<0?e==="edge"?x=0:e==="reflect"?(x=-x,x>=w&&(x=w-1)):e==="symmetric"?(x=-x-1,x>=w&&(x=w-1),x<0&&(x=0)):e==="wrap"&&(x=(x%w+w)%w):x>=w&&(e==="edge"?x=w-1:e==="reflect"?(x=2*w-x-2,x<0&&(x=0)):e==="symmetric"?(x=2*w-x-1,x<0&&(x=0)):e==="wrap"&&(x=x%w)),h.push(Math.max(0,Math.min(w-1,x)))}A=r.get(h)}f?m[d]=typeof A=="bigint"?A:BigInt(Number(A)):m[d]=Number(A);for(let h=a-1;h>=0&&(p[h]++,!(p[h]<s[h]));h--)p[h]=0}let y=N.fromData(m,s,i);return new z(y)}function eu(r,t){let e=ht(r.storage,t);return z._fromStorage(e,r.base??r)}function nu(...r){let t=r.map(n=>n.storage);return Vn(t).map((n,o)=>z._fromStorage(n,r[o].base??r[o]))}function ou(...r){return Zr(...r)}function au(r,t,e){return r.take(t,e)}function su(r,t,e){r.put(t,e)}function iu(r,t){let e=t.map(o=>o.storage),n=Pn(r.storage,e);return z._fromStorage(n)}function uu(r,t,e=!1){return jn(r.storage,t.storage,e)}function cu(r,t){return be(r.storage,t.storage)}function lu(r,t){return z._fromStorage(Kr(r.storage,t))}function fu(r,t){return z._fromStorage(rt(r.storage,t))}function mu(r,t,e=!1){let n=tt(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function pu(r,t,e=!1){let n=et(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function yu(r,t,e,n=!1){let o=nt(r.storage,t,e,n);return typeof o=="number"?o:z._fromStorage(o)}function du(r,t,e,n=!1){let o=ar(r.storage,t,e,n);return typeof o=="number"?o:z._fromStorage(o)}function gu(r,t,e,n=!1){let o=e?e.storage:void 0,a=zr(r.storage,t,o,n);return typeof a=="number"?a:z._fromStorage(a)}function hu(r,t,e=!1){let n=_r(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function Au(r,t,e=!1){let n=Br(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function bu(r,t,e=!1){let n=Er(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function Su(r,t,e=0,n=!1){let o=pr(r.storage,t,e,n);return typeof o=="number"?o:z._fromStorage(o)}function Nu(r,t,e=0,n=!1){let o=ot(r.storage,t,e,n);return typeof o=="number"?o:z._fromStorage(o)}function Du(r,t,e=!1){let n=vr(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function xu(r,t,e=!1){let n=Or(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function wu(r,t){let e=Ur(r.storage,t);return typeof e=="number"?e:z._fromStorage(e)}function Iu(r,t){let e=Tr(r.storage,t);return typeof e=="number"?e:z._fromStorage(e)}function zu(r,t){return z._fromStorage(at(r.storage,t))}function _u(r,t){return z._fromStorage(st(r.storage,t))}function Bu(r,t,e=!1){let n=$r(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)}function Eu(r){return r.cbrt()}function vu(r){return r.fabs()}function Ou(r,t){return r.divmod(t)}function Uu(r){return r.square()}function Tu(r,t){return r.remainder(t)}function $u(r,t){return r.heaviside(t)}function Mu(r,t){return r.bitwise_and(t)}function ku(r,t){return r.bitwise_or(t)}function Cu(r,t){return r.bitwise_xor(t)}function Vu(r){return r.bitwise_not()}function qu(r){return r.invert()}function Fu(r,t){return r.left_shift(t)}function Pu(r,t){return r.right_shift(t)}function ju(r,t=-1,e="big"){let n=fo(r.storage,t,e);return z._fromStorage(n)}function Ru(r,t=-1,e=-1,n="big"){let o=mo(r.storage,t,e,n);return z._fromStorage(o)}function Lu(r,...t){let e=t.map(o=>o.storage),n=Ye(r,...e);return typeof n=="number"||typeof n=="bigint"?n:z._fromStorage(n)}var Zu={cross:(r,t,e=-1,n=-1,o=-1,a)=>{let i=Qe(r.storage,t.storage,e,n,o,a);return typeof i=="number"?i:z._fromStorage(i)},norm:(r,t=null,e=null,n=!1)=>{let o=Xe(r.storage,t,e,n);return typeof o=="number"?o:z._fromStorage(o)},vector_norm:(r,t=2,e,n=!1)=>{let o=ir(r.storage,t,e,n);return typeof o=="number"?o:z._fromStorage(o)},matrix_norm:(r,t="fro",e=!1)=>{let n=nr(r.storage,t,e);return typeof n=="number"?n:z._fromStorage(n)},qr:(r,t="reduced")=>{let e=mt(r.storage,t);return e instanceof N?z._fromStorage(e):"q"in e&&"r"in e?{q:z._fromStorage(e.q),r:z._fromStorage(e.r)}:{h:z._fromStorage(e.h),tau:z._fromStorage(e.tau)}},cholesky:(r,t=!1)=>z._fromStorage(Ke(r.storage,t)),svd:(r,t=!0,e=!0)=>{let n=rn(r.storage,t,e);return"u"in n?{u:z._fromStorage(n.u),s:z._fromStorage(n.s),vt:z._fromStorage(n.vt)}:z._fromStorage(n)},det:r=>tn(r.storage),inv:r=>z._fromStorage(kr(r.storage)),solve:(r,t)=>z._fromStorage(en(r.storage,t.storage)),lstsq:(r,t,e=null)=>{let n=nn(r.storage,t.storage,e);return{x:z._fromStorage(n.x),residuals:z._fromStorage(n.residuals),rank:n.rank,s:z._fromStorage(n.s)}},cond:(r,t=2)=>on(r.storage,t),matrix_rank:(r,t)=>an(r.storage,t),matrix_power:(r,t)=>z._fromStorage(sn(r.storage,t)),pinv:(r,t=1e-15)=>z._fromStorage(un(r.storage,t)),eig:r=>{let t=dt(r.storage);return{w:z._fromStorage(t.w),v:z._fromStorage(t.v)}},eigh:(r,t="L")=>{let e=gt(r.storage,t);return{w:z._fromStorage(e.w),v:z._fromStorage(e.v)}},eigvals:r=>z._fromStorage(cn(r.storage)),eigvalsh:(r,t="L")=>z._fromStorage(ln(r.storage,t))};function Wu(r,t,e){return z._fromStorage(Rn(r.storage,t.storage,e))}function Gu(r,t,e,n){Ln(r.storage,t.storage,e.storage,n)}function Hu(r,t,e){let n=e instanceof z?e.storage:e;Zn(r.storage,t.storage,n)}function Yu(r,t,e){return z._fromStorage(Wn(r.storage,t.storage,e))}function Ju(r,t,e=0){let n=r.map(a=>a.storage),o=t.map(a=>a.storage);return z._fromStorage(Gn(n,o,e))}function Qu(r,t,e){Hn(r.storage,t.storage,e.storage)}function Xu(r,t=2){return At(r,t).map(n=>z._fromStorage(n))}function Ku(r){return Yn(r.storage).map(e=>z._fromStorage(e))}function rc(r,t=0,e){return bt(r,t,e).map(o=>z._fromStorage(o))}function tc(r,t=0){return Jn(r.storage,t).map(n=>z._fromStorage(n))}function ec(r,t=0,e){return St(r,t,e).map(o=>z._fromStorage(o))}function nc(r,t=0){return Qn(r.storage,t).map(n=>z._fromStorage(n))}function oc(r,t,e=0){return Xn(r,(a,i)=>t(a,i).storage,e).map(a=>z._fromStorage(a))}function ac(r,t="int32"){return z._fromStorage(Kn(r,t))}function sc(...r){return ro(...r.map(e=>e.storage)).map(e=>z._fromStorage(e))}function ic(r,t,e="raise"){let n=r.map(o=>o.storage);return z._fromStorage(to(n,t,e))}function uc(r,t,e="C"){let n=r instanceof z?r.storage:r;return eo(n,t,e).map(a=>z._fromStorage(a))}function cc(r,t=-1){return z._fromStorage(Dt(r.storage,t))}function lc(r,t=-1){return z._fromStorage(xt(r.storage,t))}function fc(r){let t=r.map(e=>e.storage);return z._fromStorage(po(t))}function mc(r,t,e=-1){return z._fromStorage(wt(r.storage,t,e))}function pc(r,t,e=-1){return z._fromStorage(It(r.storage,t,e))}function yc(r){return z._fromStorage(yo(r.storage))}function dc(r){return Vr(r.storage).map(e=>z._fromStorage(e))}function gc(r){return z._fromStorage(go(r.storage))}function hc(r,t,e){let n=ho(r.storage,t?.storage,e?.storage);return Array.isArray(n)?n.map(o=>z._fromStorage(o)):z._fromStorage(n)}function Ac(r,t,e="left"){return z._fromStorage(zt(r.storage,t.storage,e))}function bc(r,t){return z._fromStorage(Ao(r.storage,t.storage))}function Sc(r,t){let e=_t(r.storage,t);return typeof e=="number"?e:z._fromStorage(e)}function Mo(r,t=0){return z._fromStorage(Bt(r.storage,t))}function Nc(r){return z._fromStorage(Et(r.storage))}function Dc(r){return z._fromStorage(vt(r.storage))}function xc(r){return z._fromStorage(Ot(r.storage))}function wc(r){return z._fromStorage(Ut(r.storage))}function Ic(r){return z._fromStorage(Tt(r.storage))}function zc(r,t=!1,e=!1,n=!1){let o=J(r.storage,t,e,n);if(o instanceof N)return z._fromStorage(o);let a={values:z._fromStorage(o.values)};return o.indices&&(a.indices=z._fromStorage(o.indices)),o.inverse&&(a.inverse=z._fromStorage(o.inverse)),o.counts&&(a.counts=z._fromStorage(o.counts)),a}function _c(r,t){return z._fromStorage(So(r.storage,t.storage))}function Bc(r,t){return z._fromStorage(No(r.storage,t.storage))}function Ec(r,t){return z._fromStorage($t(r.storage,t.storage))}function vc(r,t){return z._fromStorage(Do(r.storage,t.storage))}function Oc(r,t){return z._fromStorage(xo(r.storage,t.storage))}function Uc(r,t){return z._fromStorage(wo(r.storage,t.storage))}function Tc(r,t=1,e=-1){return z._fromStorage(Mt(r.storage,t,e))}function $c(r,t=null,e=null){return z._fromStorage(Io(r.storage,t,e))}function Mc(r,t=1,e=null){let n=zo(r.storage,t,e);return Array.isArray(n)?n.map(o=>z._fromStorage(o)):z._fromStorage(n)}function kc(r,t,e=-1,n=-1,o=-1){return z._fromStorage(_o(r.storage,t.storage,e,n,o))}var Ar=new Uint8Array([147,78,85,77,80,89]),Ct=["float64","float32","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"];function Vt(){let r=new ArrayBuffer(2);return new DataView(r).setInt16(0,256,!0),new Int16Array(r)[0]===256}var Cc={f8:"float64",f4:"float32",i8:"int64",i4:"int32",i2:"int16",i1:"int8",u8:"uint64",u4:"uint32",u2:"uint16",u1:"uint8",b1:"bool"},Fr={float64:"<f8",float32:"<f4",int64:"<i8",int32:"<i4",int16:"<i2",int8:"|i1",uint64:"<u8",uint32:"<u4",uint16:"<u2",uint8:"|u1",bool:"|b1"},ko={c:"complex numbers",S:"byte strings",U:"Unicode strings",O:"Python objects",V:"structured arrays (void)",M:"datetime64",m:"timedelta64"};function Co(r){if(r.startsWith("[")||r.startsWith("("))throw new X(`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 ko)throw new X(`Unsupported dtype: ${ko[n]} (${r}). Use the 'force' parameter to skip arrays with unsupported dtypes.`);let o=Cc[e];if(!o)throw new X(`Unknown or unsupported dtype descriptor: ${r}. Supported types: ${Ct.join(", ")}. Use the 'force' parameter to skip arrays with unsupported dtypes.`);let a=Vt(),i=t==="<"||t==="|"||t==="="&&a,u=t===">"||t==="="&&!a,s=parseInt(e.slice(1),10),c=s>1&&(u&&a||i&&!a);return{dtype:o,needsByteSwap:c,itemsize:s}}var X=class extends Error{constructor(t){super(t),this.name="UnsupportedDTypeError"}},W=class extends Error{constructor(t){super(t),this.name="InvalidNpyError"}};function Pr(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=qt(t);return Ft(t,e)}function qt(r){if(r.length<10)throw new W("File too small to be a valid NPY file");for(let c=0;c<Ar.length;c++)if(r[c]!==Ar[c])throw new W("Invalid NPY magic number");let t=r[6],e=r[7];if(t!==1&&t!==2&&t!==3)throw new W(`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 W("File truncated: header extends beyond file");let i=r.slice(o,a),u=new TextDecoder("utf-8").decode(i).trim(),s=Vc(u);return{version:{major:t,minor:e},header:s,dataOffset:a}}function Ft(r,t){let{header:e,dataOffset:n}=t,{dtype:o,needsByteSwap:a,itemsize:i}=Co(e.descr),u=e.shape.reduce((d,g)=>d*g,1),s=u*i,c=r.length-n;if(c<s)throw new W(`File truncated: expected ${s} bytes of data, got ${c}`);let l=new ArrayBuffer(s);new Uint8Array(l).set(r.subarray(n,n+s));let f=qc(l,o,u,a,i),p=e.shape,y;if(e.fortran_order&&p.length>1){let d=[...p].reverse(),g=N.fromData(f,d,o);y=Fc(g,d),p=e.shape}else y=N.fromData(f,[...p],o);return new z(y)}function Vc(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 W(`Failed to parse NPY header: ${r}`);let o=t[1],a=e[1]==="True",i=n[1].trim(),u;return i===""?u=[]:u=i.split(",").map(s=>s.trim()).filter(s=>s!=="").map(s=>{let c=parseInt(s,10);if(isNaN(c))throw new W(`Invalid shape value: ${s}`);return c}),{descr:o,fortran_order:a,shape:u}}function qc(r,t,e,n,o){let a=T(t);if(!a)throw new W(`Cannot create array for dtype: ${t}`);if(!n)return new a(r,0,e);let i=new Uint8Array(r),u=new Uint8Array(r.byteLength);for(let s=0;s<e;s++){let c=s*o;for(let l=0;l<o;l++)u[c+l]=i[c+o-1-l]}return new a(u.buffer,0,e)}function Fc(r,t){let e=t.length,n=r.size,o=r.dtype,a=T(o);if(!a)throw new W(`Cannot create array for dtype: ${o}`);let i=new a(n),u=[...t].reverse(),s=Vo(t),c=Vo(u),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=s[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];I(o),i[p]=r.iget(m)}return N.fromData(i,u,o)}function Vo(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 br(r){let t=r.shape,e=r.dtype,n=Fr[e],o=t.length===0?"()":t.length===1?`(${t[0]},)`:`(${t.join(", ")})`,a=`{'descr': '${n}', 'fortran_order': False, 'shape': ${o}, }`,i=12,s=(64-(i+a.length+1)%64)%64;a=a+" ".repeat(s)+`
|
|
2
|
+
`;let c=new TextEncoder().encode(a),l=c.length,m=r.size,f=Yt(e),p=m*f,y=i+l+p,d=new Uint8Array(y);d.set(Ar,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 Pc(r,d.subarray(g),f),d}function Pc(r,t,e){let n=r.dtype,o=r.size,a=Vt(),i=I(n),u=r._storage;if(u.isCContiguous&&u.offset===0&&a){let c=u.data,l=new Uint8Array(c.buffer,c.byteOffset,o*e);t.set(l)}else{let c=new DataView(t.buffer,t.byteOffset,t.byteLength);for(let l=0;l<o;l++){let m=u.iget(l),f=l*e;i?jc(c,f,m,n==="uint64"):Rc(c,f,m,n)}}}function jc(r,t,e,n){n?r.setBigUint64(t,e,!0):r.setBigInt64(t,e,!0)}function Rc(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 Lc=(()=>{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 Pt(r){let t=4294967295;for(let e=0;e<r.length;e++)t=Lc[(t^r[e])&255]^t>>>8;return(t^4294967295)>>>0}async function qo(r){let t=Po(r),e=new Map;for(let n of t){let o=await Zc(n);e.set(n.name,o)}return e}function Fo(r){let t=Po(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 Po(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=new DataView(t.buffer,t.byteOffset,t.byteLength),n=[],o=-1;for(let c=t.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),u=[],s=a;for(let c=0;c<i&&e.getUint32(s,!0)===33639248;c++){let m=e.getUint16(s+10,!0),f=e.getUint32(s+16,!0),p=e.getUint32(s+20,!0),y=e.getUint32(s+24,!0),d=e.getUint16(s+28,!0),g=e.getUint16(s+30,!0),b=e.getUint16(s+32,!0),A=e.getUint32(s+42,!0),h=t.slice(s+46,s+46+d),S=new TextDecoder("utf-8").decode(h);u.push({name:S,compressionMethod:m,crc32:f,compressedSize:p,uncompressedSize:y,localHeaderOffset:A}),s=s+46+d+g+b}for(let c of u){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=t.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 Zc(r){if(r.compressionMethod===0)return r.compressedData;if(r.compressionMethod===8)return await Wc(r.compressedData);throw new Error(`Unsupported compression method: ${r.compressionMethod}`)}async function Wc(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:c,value:l}=await o.read();if(c)break;a.push(l)}let i=a.reduce((c,l)=>c+l.length,0),u=new Uint8Array(i),s=0;for(let c of a)u.set(c,s),s+=c.length;return u}async function Lt(r,t={}){let e=t.force??!1,n=await qo(r);return jo(n,e)}function Zt(r,t={}){let e=t.force??!1,n=Fo(r);return jo(n,e)}function jo(r,t){let e=new Map,n=[],o=new Map;for(let[a,i]of r){if(!a.endsWith(".npy"))continue;let u=a.slice(0,-4);try{let s=Pr(i);e.set(u,s)}catch(s){if(s instanceof X&&t)n.push(u),o.set(u,s.message);else throw s}}return{arrays:e,skipped:n,errors:o}}async function Ro(r,t={}){let e=await Lt(r,t);return Object.fromEntries(e.arrays)}function Lo(r,t={}){let e=Zt(r,t);return Object.fromEntries(e.arrays)}async function Zo(r,t={}){let e=t.compress??!1,n=[];for(let[f,p]of r){let y=Pt(p),d,g;e?(d=await Hc(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 u=o+a+22,s=new Uint8Array(u),c=new DataView(s.buffer),l=0;for(let f of n)f.offset=l,l=Go(s,c,l,f);let m=l;for(let f of n)l=Ho(s,c,l,f);return Yo(c,l,n.length,a,m),s}function Wo(r){let t=[];for(let[l,m]of r){let f=Pt(m);t.push({name:l,data:m,compressedData:m,crc:f,compressionMethod:0,offset:0})}let e=0;for(let l of t){let m=new TextEncoder().encode(l.name);e+=30+m.length+l.compressedData.length}let n=0;for(let l of t){let m=new TextEncoder().encode(l.name);n+=46+m.length}let a=e+n+22,i=new Uint8Array(a),u=new DataView(i.buffer),s=0;for(let l of t)l.offset=s,s=Go(i,u,s,l);let c=s;for(let l of t)s=Ho(i,u,s,l);return Yo(u,s,t.length,n,c),i}function Go(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 Ho(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 Yo(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 Hc(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:c,value:l}=await o.read();if(c)break;a.push(l)}let i=a.reduce((c,l)=>c+l.length,0),u=new Uint8Array(i),s=0;for(let c of a)u.set(c,s),s+=c.length;return u}async function Jo(r,t={}){let e=Xo(r);return Zo(e,{compress:t.compress??!1})}function Qo(r){let t=Xo(r);return Wo(t)}function Xo(r){let t=new Map;if(Array.isArray(r)){for(let n=0;n<r.length;n++){let o=r[n],a=br(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=br(o),i=n.endsWith(".npy")?n:`${n}.npy`;t.set(i,a)}return t}var vf="0.9.0";export{Fr as DTYPE_TO_DESCR,W as InvalidNpyError,z as NDArray,Ct as SUPPORTED_DTYPES,X as UnsupportedDTypeError,vf as __version__,Ws as absolute,Xi as append,ls as arange,fi as arccos,xi as arccosh,li as arcsin,Di as arcsinh,mi as arctan,pi as arctan2,wi as arctanh,pc as argpartition,lc as argsort,Mo as around,L as array,uu as array_equal,cu as array_equiv,Oi as array_split,Ss as asanyarray,qr as asarray,Ns as ascontiguousarray,Ds as asfortranarray,Zi as atleast_1d,Wi as atleast_2d,Gi as atleast_3d,gu as average,Mu as bitwise_and,Vu as bitwise_not,ku as bitwise_or,Cu as bitwise_xor,nu as broadcast_arrays,ou as broadcast_shapes,eu as broadcast_to,Eu as cbrt,Nc as ceil,iu as choose,Yi as column_stack,Yu as compress,ur as concatenate,ds as copy,ui as cos,Si as cosh,Sc as count_nonzero,kc as cross,fu as cumprod,lu as cumsum,hi as deg2rad,di as degrees,Ki as delete,To as diag,Xu as diag_indices,Ku as diag_indices_from,xs as diagflat,ti as diagonal,Tc as diff,Ou as divmod,Ks as dot,Hi as dsplit,Ei as dstack,$c as ediff1d,Lu as einsum,Oo as empty,As as empty_like,Cs as exp,Vs as exp2,qi as expand_dims,qs as expm1,bc as extract,vo as eye,vu as fabs,Dc as fix,gc as flatnonzero,kt as flip,Fi as fliplr,Pi as flipud,xc as floor,Js as floor_divide,vs as frombuffer,Os as fromfile,ws as fromfunction,Us as fromiter,Ts as fromstring,Uo as full,bs as full_like,ps as geomspace,Mc as gradient,$u as heaviside,Ti as hsplit,Bi as hstack,yi as hypot,ys as identity,_c as in1d,ac as indices,oi as inner,ru as insert,Bc as intersect1d,qu as invert,Ec as isin,sc as ix_,ei as kron,Fu as left_shift,fc as lexsort,Zu as linalg,fs as linspace,Ro as loadNpz,Lo as loadNpzSync,Fs as log,js as log10,Rs as log1p,Ps as log2,Ls as logaddexp,Zs as logaddexp2,ms as logspace,oc as mask_indices,pu as median,Is as meshgrid,Ys as mod,zi as moveaxis,Iu as nanargmax,wu as nanargmin,_u as nancumprod,zu as nancumsum,xu as nanmax,bu as nanmean,Bu as nanmedian,Du as nanmin,Au as nanprod,Nu as nanstd,hu as nansum,Su as nanvar,Gs as negative,dc as nonzero,Bo as ones,hs as ones_like,ai as outer,ju as packbits,tu as pad,Pr as parseNpy,Ft as parseNpyData,qt as parseNpyHeader,Lt as parseNpz,Zt as parseNpzSync,mc as partition,yu as percentile,Qu as place,Qs as positive,ks as power,mu as ptp,su as put,Gu as put_along_axis,Hu as putmask,du as quantile,Ai as rad2deg,gi as radians,ki as ravel,ic as ravel_multi_index,Xs as reciprocal,Tu as remainder,Mi as repeat,Ci as reshape,Qi as resize,Pu as right_shift,wc as rint,Ri as roll,Li as rollaxis,ji as rot90,Mo as round,Ji as row_stack,Ac as searchsorted,Ju as select,br as serializeNpy,Jo as serializeNpz,Qo as serializeNpzSync,vc as setdiff1d,Oc as setxor1d,Hs as sign,ii as sin,bi as sinh,cc as sort,yc as sort_complex,vi as split,Ms as sqrt,Uu as square,Vi as squeeze,_i as stack,Ii as swapaxes,au as take,Wu as take_along_axis,ci as tan,Ni as tanh,si as tensordot,$i as tile,ri as trace,ni as transpose,zs as tri,_s as tril,rc as tril_indices,tc as tril_indices_from,Bs as triu,ec as triu_indices,nc as triu_indices_from,Ic as trunc,Uc as union1d,zc as unique,Ru as unpackbits,uc as unravel_index,Es as vander,Ui as vsplit,$o as vstack,hc as where,Q as zeros,gs as zeros_like};
|