numpy-ts 0.4.0 → 0.5.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.
@@ -1,2 +1,3 @@
1
- "use strict";var j=Object.defineProperty;var gt=Object.getOwnPropertyDescriptor;var ht=Object.getOwnPropertyNames;var At=Object.prototype.hasOwnProperty;var bt=(t,r)=>{for(var n in r)j(t,n,{get:r[n],enumerable:!0})},St=(t,r,n,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of ht(r))!At.call(t,o)&&o!==n&&j(t,o,{get:()=>r[o],enumerable:!(e=gt(r,o))||e.enumerable});return t};var Dt=t=>St(j({},"__esModule",{value:!0}),t);var Yt={};bt(Yt,{NDArray:()=>E,__version__:()=>Ht,absolute:()=>nt,arange:()=>Wr,array:()=>v,asarray:()=>Yr,copy:()=>Qr,dot:()=>ft,empty:()=>ar,empty_like:()=>Kr,eye:()=>sr,floor_divide:()=>it,full:()=>ir,full_like:()=>rt,geomspace:()=>Gr,identity:()=>Hr,inner:()=>pt,linspace:()=>Jr,logspace:()=>Pr,mod:()=>at,negative:()=>ot,ones:()=>or,ones_like:()=>Zr,outer:()=>mt,positive:()=>ut,power:()=>et,reciprocal:()=>lt,sign:()=>st,sqrt:()=>tt,tensordot:()=>dt,trace:()=>ct,transpose:()=>yt,zeros:()=>k,zeros_like:()=>Xr});module.exports=Dt(Yt);function lr(t){if(!t.includes(":")){if(t.includes("."))throw new Error(`Invalid slice index: "${t}" (must be integer)`);let a=parseInt(t,10);if(isNaN(a))throw new Error(`Invalid slice index: "${t}"`);return{start:a,stop:null,step:1,isIndex:!0}}let r=t.split(":");if(r.length>3)throw new Error(`Invalid slice notation: "${t}" (too many colons)`);let n=r[0]===""?null:parseInt(r[0],10),e=r[1]===""||r[1]===void 0?null:parseInt(r[1],10),o=r[2]===""||r[2]===void 0?1:parseInt(r[2],10);if(n!==null&&isNaN(n))throw new Error(`Invalid start index in slice: "${t}"`);if(e!==null&&isNaN(e))throw new Error(`Invalid stop index in slice: "${t}"`);if(isNaN(o))throw new Error(`Invalid step in slice: "${t}"`);if(o===0)throw new Error("Slice step cannot be zero");return{start:n,stop:e,step:o,isIndex:!1}}function fr(t,r){let{start:n,stop:e}=t,{step:o,isIndex:a}=t;if(a){if(n===null)throw new Error("Index cannot be null");let l=n<0?r+n:n;if(l<0||l>=r)throw new Error(`Index ${n} is out of bounds for size ${r}`);return{start:l,stop:l+1,step:1,isIndex:!0}}return o>0?(n===null&&(n=0),e===null&&(e=r)):(n===null&&(n=r-1),e===null&&(e=-r-1)),n<0&&(n=r+n),e<0&&(e=r+e),n=Math.max(0,Math.min(n,r)),e=Math.max(-1,Math.min(e,r)),{start:n,stop:e,step:o,isIndex:!1}}var T="float64";function U(t){switch(t){case"float64":return Float64Array;case"float32":return Float32Array;case"int64":return BigInt64Array;case"int32":return Int32Array;case"int16":return Int16Array;case"int8":return Int8Array;case"uint64":return BigUint64Array;case"uint32":return Uint32Array;case"uint16":return Uint16Array;case"uint8":return Uint8Array;case"bool":return Uint8Array;default:throw new Error(`Unknown dtype: ${t}`)}}function cr(t){return t==="float64"||t==="float32"}function x(t){return t==="int64"||t==="uint64"}function _(t,r){if(t===r)return t;if(t==="bool")return r;if(r==="bool")return t;if(cr(t)||cr(r)){if(t==="float64"||r==="float64")return"float64";if(t==="float32"){let u=r;return u==="int32"||u==="int64"||u==="uint32"||u==="uint64"?"float64":"float32"}if(r==="float32"){let u=t;return u==="int32"||u==="int64"||u==="uint32"||u==="uint64"?"float64":"float32"}return"float32"}let n=t.startsWith("int"),e=r.startsWith("int"),o=t.startsWith("uint"),a=r.startsWith("uint"),l=u=>u.includes("64")?64:u.includes("32")?32:u.includes("16")?16:u.includes("8")?8:0,i=l(t),s=l(r);if(t==="int64"&&r==="uint64"||t==="uint64"&&r==="int64")return"float64";if(n&&a&&i===s){if(i===8)return"int16";if(i===16)return"int32";if(i===32)return"int64"}if(o&&e&&i===s){if(s===8)return"int16";if(s===16)return"int32";if(s===32)return"int64"}if(n&&e||o&&a){let u=Math.max(i,s);return n?u===64?"int64":u===32?"int32":u===16?"int16":"int8":u===64?"uint64":u===32?"uint32":u===16?"uint16":"uint8"}return n&&a?i>s?t:s===8?"int16":s===16?"int32":s===32?"int64":"float64":o&&e?s>i?r:i===8?"int16":i===16?"int32":i===32?"int64":"float64":"float64"}var D=class t{constructor(r,n,e,o,a){this._data=r,this._shape=n,this._strides=e,this._offset=o,this._dtype=a}get shape(){return this._shape}get ndim(){return this._shape.length}get size(){return this._shape.reduce((r,n)=>r*n,1)}get dtype(){return this._dtype}get data(){return this._data}get strides(){return this._strides}get offset(){return this._offset}get isCContiguous(){let r=this._shape,n=this._strides,e=r.length;if(e===0)return!0;if(e===1)return n[0]===1;let o=1;for(let a=e-1;a>=0;a--){if(n[a]!==o)return!1;o*=r[a]}return!0}get isFContiguous(){let r=this._shape,n=this._strides,e=r.length;if(e===0)return!0;if(e===1)return n[0]===1;let o=1;for(let a=0;a<e;a++){if(n[a]!==o)return!1;o*=r[a]}return!0}iget(r){let n=this._shape,e=this._strides,o=n.length;if(o===0)return this._data[this._offset];let a=r,l=this._offset;for(let i=0;i<o;i++){let s=1;for(let c=i+1;c<o;c++)s*=n[c];let u=Math.floor(a/s);a=a%s,l+=u*e[i]}return this._data[l]}iset(r,n){let e=this._shape,o=this._strides,a=e.length;if(a===0){this._data[this._offset]=n;return}let l=r,i=this._offset;for(let s=0;s<a;s++){let u=1;for(let f=s+1;f<a;f++)u*=e[f];let c=Math.floor(l/u);l=l%u,i+=c*o[s]}this._data[i]=n}get(...r){let n=this._strides,e=this._offset;for(let o=0;o<r.length;o++)e+=r[o]*n[o];return this._data[e]}set(r,n){let e=this._strides,o=this._offset;for(let a=0;a<r.length;a++)o+=r[a]*e[a];this._data[o]=n}copy(){let r=Array.from(this._shape),n=this._dtype,e=this.size,o=U(n);if(!o)throw new Error(`Cannot copy array with dtype ${n}`);let a=new o(e);if(this.isCContiguous&&this._offset===0)if(x(n)){let l=this._data,i=a;for(let s=0;s<e;s++)i[s]=l[s]}else a.set(this._data);else if(x(n)){let l=a;for(let i=0;i<e;i++)l[i]=this.iget(i)}else for(let l=0;l<e;l++)a[l]=this.iget(l);return new t(a,r,t._computeStrides(r),0,n)}static fromData(r,n,e,o,a){let l=o??t._computeStrides(n),i=a??0;return new t(r,n,l,i,e)}static zeros(r,n=T){let e=r.reduce((l,i)=>l*i,1),o=U(n);if(!o)throw new Error(`Cannot create array with dtype ${n}`);let a=new o(e);return new t(a,r,t._computeStrides(r),0,n)}static ones(r,n=T){let e=r.reduce((l,i)=>l*i,1),o=U(n);if(!o)throw new Error(`Cannot create array with dtype ${n}`);let a=new o(e);return x(n)?a.fill(BigInt(1)):a.fill(1),new t(a,r,t._computeStrides(r),0,n)}static _computeStrides(r){let n=new Array(r.length),e=1;for(let o=r.length-1;o>=0;o--)n[o]=e,e*=r[o];return n}};function L(t){let r=new Array(t.length),n=1;for(let e=t.length-1;e>=0;e--)r[e]=n,n*=t[e];return r}function yr(t,r){let n=t.length,e=r.length,o=Math.max(n,e),a=new Array(o);for(let l=0;l<o;l++){let i=l<o-n?1:t[l-(o-n)],s=l<o-e?1:r[l-(o-e)];if(i===s)a[l]=i;else if(i===1)a[l]=s;else if(s===1)a[l]=i;else throw new Error(`operands could not be broadcast together with shapes ${JSON.stringify(Array.from(t))} ${JSON.stringify(Array.from(r))}`)}return a}function xt(t,r,n){let e=t.length,o=n.length,a=new Array(o).fill(0);for(let l=0;l<e;l++){let i=o-e+l,s=t[l],u=n[i];if(s===u)a[i]=r[l];else if(s===1)a[i]=0;else throw new Error("Invalid broadcast")}return a}function q(t,r){let n=xt(t.shape,t.strides,r);return D.fromData(t.data,Array.from(r),t.dtype,n,t.offset)}function O(t,r,n,e){let o=yr(t.shape,r.shape),a=q(t,o),l=q(r,o),i=_(t.dtype,r.dtype),s=D.zeros(o,i),u=s.data,c=s.size;if(x(i)){let f=u;for(let y=0;y<c;y++){let m=a.iget(y),p=l.iget(y),d=typeof m=="bigint"?m:BigInt(Math.round(m)),A=typeof p=="bigint"?p:BigInt(Math.round(p));e==="add"?f[y]=d+A:e==="subtract"?f[y]=d-A:e==="multiply"?f[y]=d*A:e==="divide"?f[y]=d/A:f[y]=BigInt(Math.round(n(Number(d),Number(A))))}}else{let f=x(t.dtype)||x(r.dtype);for(let y=0;y<c;y++){let m=a.iget(y),p=l.iget(y),d=Number(m),A=Number(p);u[y]=n(d,A)}}return s}function M(t,r,n){let e=yr(t.shape,r.shape),o=q(t,e),a=q(r,e),l=e.reduce((u,c)=>u*c,1),i=new Uint8Array(l),s=x(t.dtype)||x(r.dtype);for(let u=0;u<l;u++){let c=o.iget(u),f=a.iget(u),y=Number(c),m=Number(f);i[u]=n(y,m)?1:0}return D.fromData(i,e,"bool")}function pr(t,r,n=!0){let e=t.dtype,o=Array.from(t.shape),a=t.size,i=n?e:e!=="float32"&&e!=="float64"?"float64":e,s=D.zeros(o,i),u=s.data,c=t.data;if(x(e))if(x(i)){let f=u;for(let y=0;y<a;y++){let m=Number(c[y]);f[y]=BigInt(Math.round(r(m)))}}else for(let f=0;f<a;f++)u[f]=r(Number(c[f]));else for(let f=0;f<a;f++)u[f]=r(Number(c[f]));return s}function R(t,r){return t.isCContiguous&&r.isCContiguous&&t.shape.length===r.shape.length&&t.shape.every((n,e)=>n===r.shape[e])}function mr(t,r){return typeof r=="number"?Bt(t,r):R(t,r)?It(t,r):O(t,r,(n,e)=>n+e,"add")}function It(t,r){let n=_(t.dtype,r.dtype),e=D.zeros(Array.from(t.shape),n),o=t.size,a=t.data,l=r.data,i=e.data;if(x(n)){let s=i;if(!x(t.dtype)||!x(r.dtype))for(let c=0;c<o;c++){let f=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),y=typeof l[c]=="bigint"?l[c]:BigInt(Math.round(Number(l[c])));s[c]=f+y}else{let c=a,f=l;for(let y=0;y<o;y++)s[y]=c[y]+f[y]}}else if(x(t.dtype)||x(r.dtype))for(let u=0;u<o;u++){let c=typeof a[u]=="bigint"?Number(a[u]):a[u],f=typeof l[u]=="bigint"?Number(l[u]):l[u];i[u]=c+f}else for(let u=0;u<o;u++)i[u]=a[u]+l[u];return e}function dr(t,r){return typeof r=="number"?zt(t,r):R(t,r)?wt(t,r):O(t,r,(n,e)=>n-e,"subtract")}function wt(t,r){let n=_(t.dtype,r.dtype),e=D.zeros(Array.from(t.shape),n),o=t.size,a=t.data,l=r.data,i=e.data;if(x(n)){let s=i;if(!x(t.dtype)||!x(r.dtype))for(let c=0;c<o;c++){let f=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),y=typeof l[c]=="bigint"?l[c]:BigInt(Math.round(Number(l[c])));s[c]=f-y}else{let c=a,f=l;for(let y=0;y<o;y++)s[y]=c[y]-f[y]}}else if(x(t.dtype)||x(r.dtype))for(let u=0;u<o;u++){let c=typeof a[u]=="bigint"?Number(a[u]):a[u],f=typeof l[u]=="bigint"?Number(l[u]):l[u];i[u]=c-f}else for(let u=0;u<o;u++)i[u]=a[u]-l[u];return e}function gr(t,r){return typeof r=="number"?_t(t,r):R(t,r)?Nt(t,r):O(t,r,(n,e)=>n*e,"multiply")}function Nt(t,r){let n=_(t.dtype,r.dtype),e=D.zeros(Array.from(t.shape),n),o=t.size,a=t.data,l=r.data,i=e.data;if(x(n)){let s=i;if(!x(t.dtype)||!x(r.dtype))for(let c=0;c<o;c++){let f=typeof a[c]=="bigint"?a[c]:BigInt(Math.round(Number(a[c]))),y=typeof l[c]=="bigint"?l[c]:BigInt(Math.round(Number(l[c])));s[c]=f*y}else{let c=a,f=l;for(let y=0;y<o;y++)s[y]=c[y]*f[y]}}else if(x(t.dtype)||x(r.dtype))for(let u=0;u<o;u++){let c=typeof a[u]=="bigint"?Number(a[u]):a[u],f=typeof l[u]=="bigint"?Number(l[u]):l[u];i[u]=c*f}else for(let u=0;u<o;u++)i[u]=a[u]*l[u];return e}function hr(t,r){if(typeof r=="number")return Tt(t,r);let n=t.dtype==="float64",e=r.dtype==="float64",o=t.dtype==="float32",a=r.dtype==="float32";if(n||e){let s=n?t:V(t,"float64"),u=e?r:V(r,"float64");return O(s,u,(c,f)=>c/f,"divide")}if(o||a){let s=o?t:V(t,"float32"),u=a?r:V(r,"float32");return O(s,u,(c,f)=>c/f,"divide")}let l=V(t,"float64"),i=V(r,"float64");return O(l,i,(s,u)=>s/u,"divide")}function V(t,r){let n=D.zeros(Array.from(t.shape),r),e=t.size,o=t.data,a=n.data;for(let l=0;l<e;l++)a[l]=Number(o[l]);return n}function Bt(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,l=D.zeros(e,n),i=l.data;if(x(n)){let s=o,u=i,c=BigInt(Math.round(r));for(let f=0;f<a;f++)u[f]=s[f]+c}else for(let s=0;s<a;s++)i[s]=Number(o[s])+r;return l}function zt(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,l=D.zeros(e,n),i=l.data;if(x(n)){let s=o,u=i,c=BigInt(Math.round(r));for(let f=0;f<a;f++)u[f]=s[f]-c}else for(let s=0;s<a;s++)i[s]=Number(o[s])-r;return l}function _t(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,l=D.zeros(e,n),i=l.data;if(x(n)){let s=o,u=i,c=BigInt(Math.round(r));for(let f=0;f<a;f++)u[f]=s[f]*c}else for(let s=0;s<a;s++)i[s]=Number(o[s])*r;return l}function Tt(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,i=n!=="float32"&&n!=="float64"?"float64":n,s=D.zeros(e,i),u=s.data;if(x(n))for(let c=0;c<a;c++)u[c]=Number(o[c])/r;else for(let c=0;c<a;c++)u[c]=Number(o[c])/r;return s}function Ar(t){let r=t.dtype,n=Array.from(t.shape),e=t.data,o=t.size,a=D.zeros(n,r),l=a.data;if(x(r)){let i=e,s=l;for(let u=0;u<o;u++){let c=i[u];s[u]=c<0n?-c:c}}else for(let i=0;i<o;i++)l[i]=Math.abs(Number(e[i]));return a}function br(t){let r=t.dtype,n=Array.from(t.shape),e=t.data,o=t.size,a=D.zeros(n,r),l=a.data;if(x(r)){let i=e,s=l;for(let u=0;u<o;u++)s[u]=-i[u]}else for(let i=0;i<o;i++)l[i]=-Number(e[i]);return a}function Sr(t){let r=t.dtype,n=Array.from(t.shape),e=t.data,o=t.size,a=D.zeros(n,r),l=a.data;if(x(r)){let i=e,s=l;for(let u=0;u<o;u++){let c=i[u];s[u]=c>0n?1n:c<0n?-1n:0n}}else for(let i=0;i<o;i++){let s=Number(e[i]);l[i]=s>0?1:s<0?-1:0}return a}function Dr(t,r){return typeof r=="number"?Ut(t,r):O(t,r,(n,e)=>(n%e+e)%e,"mod")}function Ut(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,l=D.zeros(e,n),i=l.data;if(x(n)){let s=o,u=i,c=BigInt(Math.round(r));for(let f=0;f<a;f++){let y=s[f];u[f]=(y%c+c)%c}}else for(let s=0;s<a;s++){let u=Number(o[s]);i[s]=(u%r+r)%r}return l}function xr(t,r){return typeof r=="number"?Et(t,r):O(t,r,(n,e)=>Math.floor(n/e),"floor_divide")}function Et(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,l=D.zeros(e,n),i=l.data;if(x(n)){let s=o,u=i,c=BigInt(Math.round(r));for(let f=0;f<a;f++)u[f]=s[f]/c}else for(let s=0;s<a;s++)i[s]=Math.floor(Number(o[s])/r);return l}function Ir(t){let r=t.dtype,n=Array.from(t.shape),e=t.data,o=t.size,a=D.zeros(n,r),l=a.data;for(let i=0;i<o;i++)l[i]=e[i];return a}function wr(t){let r=t.dtype,n=Array.from(t.shape),e=t.data,o=t.size,l=r!=="float32"&&r!=="float64"?"float64":r,i=D.zeros(n,l),s=i.data;if(x(r))for(let u=0;u<o;u++)s[u]=1/Number(e[u]);else for(let u=0;u<o;u++)s[u]=1/Number(e[u]);return i}function Nr(t,r){return typeof r=="number"?Ot(t,r):M(t,r,(n,e)=>n>e)}function Br(t,r){return typeof r=="number"?vt(t,r):M(t,r,(n,e)=>n>=e)}function zr(t,r){return typeof r=="number"?Mt(t,r):M(t,r,(n,e)=>n<e)}function _r(t,r){return typeof r=="number"?Vt(t,r):M(t,r,(n,e)=>n<=e)}function Tr(t,r){return typeof r=="number"?Ct(t,r):M(t,r,(n,e)=>n===e)}function Ur(t,r){return typeof r=="number"?Ft(t,r):M(t,r,(n,e)=>n!==e)}function W(t,r,n=1e-5,e=1e-8){return typeof r=="number"?kt(t,r,n,e):M(t,r,(o,a)=>{let l=Math.abs(o-a),i=e+n*Math.abs(a);return l<=i})}function Er(t,r,n=1e-5,e=1e-8){let o=W(t,r,n,e),a=o.data;for(let l=0;l<o.size;l++)if(a[l]===0)return!1;return!0}function Ot(t,r){let n=new Uint8Array(t.size),e=t.data;for(let o=0;o<t.size;o++)n[o]=e[o]>r?1:0;return D.fromData(n,Array.from(t.shape),"bool")}function vt(t,r){let n=new Uint8Array(t.size),e=t.data;for(let o=0;o<t.size;o++)n[o]=e[o]>=r?1:0;return D.fromData(n,Array.from(t.shape),"bool")}function Mt(t,r){let n=new Uint8Array(t.size),e=t.data;for(let o=0;o<t.size;o++)n[o]=e[o]<r?1:0;return D.fromData(n,Array.from(t.shape),"bool")}function Vt(t,r){let n=new Uint8Array(t.size),e=t.data;for(let o=0;o<t.size;o++)n[o]=e[o]<=r?1:0;return D.fromData(n,Array.from(t.shape),"bool")}function Ct(t,r){let n=new Uint8Array(t.size),e=t.data,o=t.dtype;if(x(o)){let a=BigInt(Math.round(r)),l=e;for(let i=0;i<t.size;i++)n[i]=l[i]===a?1:0}else for(let a=0;a<t.size;a++)n[a]=e[a]===r?1:0;return D.fromData(n,Array.from(t.shape),"bool")}function Ft(t,r){let n=new Uint8Array(t.size),e=t.data;for(let o=0;o<t.size;o++)n[o]=e[o]!==r?1:0;return D.fromData(n,Array.from(t.shape),"bool")}function kt(t,r,n,e){let o=new Uint8Array(t.size),a=t.data,l=t.dtype;if(x(l)){let i=a;for(let s=0;s<t.size;s++){let u=Number(i[s]),c=Math.abs(u-r),f=e+n*Math.abs(r);o[s]=c<=f?1:0}}else for(let i=0;i<t.size;i++){let s=Number(a[i]),u=Math.abs(s-r),c=e+n*Math.abs(r);o[i]=u<=c?1:0}return D.fromData(o,Array.from(t.shape),"bool")}function B(t,r){let n=0,e=1;for(let o=t.length-1;o>=0;o--)n+=t[o]*e,e*=r[o];return n}function z(t,r,n,e){let o=e.length,a=new Array(o),l=Array.from(e).filter((s,u)=>u!==r),i=t;for(let s=l.length-1;s>=0;s--)a[s>=r?s+1:s]=i%l[s],i=Math.floor(i/l[s]);return a[r]=n,a}function C(t,r,n=!1){let e=t.dtype,o=t.shape,a=o.length,l=t.size,i=t.data;if(r===void 0)if(x(e)){let p=i,d=BigInt(0);for(let A=0;A<l;A++)d+=p[A];return Number(d)}else{let p=0;for(let d=0;d<l;d++)p+=Number(i[d]);return p}let s=r;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((p,d)=>d!==s);if(u.length===0)return C(t);let c=D.zeros(u,e),f=c.data,y=o[s],m=u.reduce((p,d)=>p*d,1);if(x(e)){let p=i,d=f;for(let A=0;A<m;A++){let g=BigInt(0);for(let h=0;h<y;h++){let S=z(A,s,h,o),b=B(S,o);g+=p[b]}d[A]=g}}else for(let p=0;p<m;p++){let d=0;for(let A=0;A<y;A++){let g=z(p,s,A,o),h=B(g,o);d+=Number(i[h])}f[p]=d}if(n){let p=[...o];return p[s]=1,D.fromData(f,p,e)}return c}function J(t,r,n=!1){let e=t.dtype,o=t.shape;if(r===void 0)return C(t)/t.size;let a=r;if(a<0&&(a=o.length+a),a<0||a>=o.length)throw new Error(`axis ${r} is out of bounds for array of dimension ${o.length}`);let l=C(t,r,n);if(typeof l=="number")return l/o[a];let i=o[a],s=e;(x(e)||e.startsWith("int")||e.startsWith("uint"))&&(s="float64");let u=D.zeros(Array.from(l.shape),s),c=u.data,f=l.data;if(x(e)){let y=f;for(let m=0;m<c.length;m++)c[m]=Number(y[m])/i}else for(let y=0;y<c.length;y++)c[y]=Number(f[y])/i;return u}function P(t,r,n=!1){let e=t.dtype,o=t.shape,a=o.length,l=t.size,i=t.data;if(r===void 0){if(l===0)throw new Error("max of empty array");let p=i[0];for(let d=1;d<l;d++)i[d]>p&&(p=i[d]);return Number(p)}let s=r;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((p,d)=>d!==s);if(u.length===0)return P(t);let c=D.zeros(u,e),f=c.data,y=o[s],m=u.reduce((p,d)=>p*d,1);if(x(e)){let p=i,d=f;for(let A=0;A<m;A++){let g=z(A,s,0,o),h=B(g,o),S=p[h];for(let b=1;b<y;b++){let I=z(A,s,b,o),w=B(I,o),$=p[w];$>S&&(S=$)}d[A]=S}}else for(let p=0;p<m;p++){let d=-1/0;for(let A=0;A<y;A++){let g=z(p,s,A,o),h=B(g,o),S=Number(i[h]);S>d&&(d=S)}f[p]=d}if(n){let p=[...o];return p[s]=1,D.fromData(f,p,e)}return c}function G(t,r,n=!1){let e=t.dtype,o=t.shape,a=o.length,l=t.size,i=t.data;if(r===void 0)if(x(e)){let p=i,d=BigInt(1);for(let A=0;A<l;A++)d*=p[A];return Number(d)}else{let p=1;for(let d=0;d<l;d++)p*=Number(i[d]);return p}let s=r;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((p,d)=>d!==s);if(u.length===0)return G(t);let c=D.zeros(u,e),f=c.data,y=o[s],m=u.reduce((p,d)=>p*d,1);if(x(e)){let p=i,d=f;for(let A=0;A<m;A++){let g=BigInt(1);for(let h=0;h<y;h++){let S=z(A,s,h,o),b=B(S,o);g*=p[b]}d[A]=g}}else for(let p=0;p<m;p++){let d=1;for(let A=0;A<y;A++){let g=z(p,s,A,o),h=B(g,o);d*=Number(i[h])}f[p]=d}if(n){let p=[...o];return p[s]=1,D.fromData(f,p,e)}return c}function H(t,r,n=!1){let e=t.dtype,o=t.shape,a=o.length,l=t.size,i=t.data;if(r===void 0){if(l===0)throw new Error("min of empty array");let p=i[0];for(let d=1;d<l;d++)i[d]<p&&(p=i[d]);return Number(p)}let s=r;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let u=Array.from(o).filter((p,d)=>d!==s);if(u.length===0)return H(t);let c=D.zeros(u,e),f=c.data,y=o[s],m=u.reduce((p,d)=>p*d,1);if(x(e)){let p=i,d=f;for(let A=0;A<m;A++){let g=z(A,s,0,o),h=B(g,o),S=p[h];for(let b=1;b<y;b++){let I=z(A,s,b,o),w=B(I,o),$=p[w];$<S&&(S=$)}d[A]=S}}else for(let p=0;p<m;p++){let d=1/0;for(let A=0;A<y;A++){let g=z(p,s,A,o),h=B(g,o),S=Number(i[h]);S<d&&(d=S)}f[p]=d}if(n){let p=[...o];return p[s]=1,D.fromData(f,p,e)}return c}function Y(t,r){let n=t.dtype,e=t.shape,o=e.length,a=t.size,l=t.data;if(r===void 0){if(a===0)throw new Error("argmin of empty array");let m=l[0],p=0;for(let d=1;d<a;d++)l[d]<m&&(m=l[d],p=d);return p}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(e).filter((m,p)=>p!==i);if(s.length===0)return Y(t);let u=D.zeros(s,"int32"),c=u.data,f=e[i],y=s.reduce((m,p)=>m*p,1);if(x(n)){let m=l;for(let p=0;p<y;p++){let d=z(p,i,0,e),A=B(d,e),g=m[A],h=0;for(let S=1;S<f;S++){let b=z(p,i,S,e),I=B(b,e),w=m[I];w<g&&(g=w,h=S)}c[p]=h}}else for(let m=0;m<y;m++){let p=1/0,d=0;for(let A=0;A<f;A++){let g=z(m,i,A,e),h=B(g,e),S=Number(l[h]);S<p&&(p=S,d=A)}c[m]=d}return u}function Q(t,r){let n=t.dtype,e=t.shape,o=e.length,a=t.size,l=t.data;if(r===void 0){if(a===0)throw new Error("argmax of empty array");let m=l[0],p=0;for(let d=1;d<a;d++)l[d]>m&&(m=l[d],p=d);return p}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(e).filter((m,p)=>p!==i);if(s.length===0)return Q(t);let u=D.zeros(s,"int32"),c=u.data,f=e[i],y=s.reduce((m,p)=>m*p,1);if(x(n)){let m=l;for(let p=0;p<y;p++){let d=z(p,i,0,e),A=B(d,e),g=m[A],h=0;for(let S=1;S<f;S++){let b=z(p,i,S,e),I=B(b,e),w=m[I];w>g&&(g=w,h=S)}c[p]=h}}else for(let m=0;m<y;m++){let p=-1/0,d=0;for(let A=0;A<f;A++){let g=z(m,i,A,e),h=B(g,e),S=Number(l[h]);S>p&&(p=S,d=A)}c[m]=d}return u}function X(t,r,n=0,e=!1){let o=t.shape,a=o.length,l=t.size,i=t.data,s=J(t,r,e);if(r===void 0){let g=s,h=0;for(let S=0;S<l;S++){let b=Number(i[S])-g;h+=b*b}return h/(l-n)}let u=r;if(u<0&&(u=a+u),u<0||u>=a)throw new Error(`axis ${r} is out of bounds for array of dimension ${a}`);let c=o[u],f=s,y=f.data,m=e?f.shape:Array.from(o).filter((g,h)=>h!==u),p=D.zeros(Array.from(m),"float64"),d=p.data,A=m.reduce((g,h)=>g*h,1);for(let g=0;g<A;g++){let h=0,S=Number(y[g]);for(let b=0;b<c;b++){let I=z(g,u,b,o),w=B(I,o),$=Number(i[w])-S;h+=$*$}d[g]=h/(c-n)}return p}function $r(t,r,n=0,e=!1){let o=X(t,r,n,e);if(typeof o=="number")return Math.sqrt(o);let a=D.zeros(Array.from(o.shape),"float64"),l=o.data,i=a.data;for(let s=0;s<l.length;s++)i[s]=Math.sqrt(Number(l[s]));return a}function Z(t,r,n=!1){let e=t.shape,o=e.length,a=t.size,l=t.data;if(r===void 0){for(let m=0;m<a;m++)if(!l[m])return!1;return!0}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(e).filter((m,p)=>p!==i);if(s.length===0)return Z(t);let u=D.zeros(s,"bool"),c=u.data,f=e[i],y=s.reduce((m,p)=>m*p,1);for(let m=0;m<y;m++){let p=!0;for(let d=0;d<f;d++){let A=z(m,i,d,e),g=B(A,e);if(!l[g]){p=!1;break}}c[m]=p?1:0}if(n){let m=[...e];return m[i]=1,D.fromData(c,m,"bool")}return u}function K(t,r,n=!1){let e=t.shape,o=e.length,a=t.size,l=t.data;if(r===void 0){for(let m=0;m<a;m++)if(l[m])return!0;return!1}let i=r;if(i<0&&(i=o+i),i<0||i>=o)throw new Error(`axis ${r} is out of bounds for array of dimension ${o}`);let s=Array.from(e).filter((m,p)=>p!==i);if(s.length===0)return K(t);let u=D.zeros(s,"bool"),c=u.data,f=e[i],y=s.reduce((m,p)=>m*p,1);for(let m=0;m<y;m++){let p=!1;for(let d=0;d<f;d++){let A=z(m,i,d,e),g=B(A,e);if(l[g]){p=!0;break}}c[m]=p?1:0}if(n){let m=[...e];return m[i]=1,D.fromData(c,m,"bool")}return u}function Or(t,r){let n=t.size,e=t.dtype,o=r.indexOf(-1),a;if(o!==-1){let u=r.reduce((f,y,m)=>m===o?f:f*y,1),c=n/u;if(!Number.isInteger(c))throw new Error(`cannot reshape array of size ${n} into shape ${JSON.stringify(r)}`);a=r.map((f,y)=>y===o?c:f)}else a=r;if(a.reduce((u,c)=>u*c,1)!==n)throw new Error(`cannot reshape array of size ${n} into shape ${JSON.stringify(a)}`);if(t.isCContiguous){let u=t.data;return D.fromData(u,a,e,L(a),0)}let s=t.copy().data;return D.fromData(s,a,e,L(a),0)}function rr(t){let r=t.shape,n=r.length,e=t.size,o=t.dtype,a=U(o);if(!a)throw new Error(`Cannot flatten array with dtype ${o}`);let l=new a(e),i=0,s=(u,c)=>{if(c===n){let f=t.get(...u);l[i++]=f;return}for(let f=0;f<r[c];f++)u[c]=f,s(u,c+1)};return s(new Array(n),0),D.fromData(l,[e],o,[1],0)}function F(t){let r=t.size,n=t.dtype;if(t.isCContiguous){let e=t.data;return D.fromData(e,[r],n,[1],0)}return rr(t)}function tr(t,r){let n=t.shape,e=n.length,o=t.strides,a=t.data,l=t.dtype,i;if(r===void 0)i=Array.from({length:e},(f,y)=>e-1-y);else{if(r.length!==e)throw new Error(`axes must have length ${e}, got ${r.length}`);let f=new Set;for(let y of r){let m=y<0?e+y:y;if(m<0||m>=e)throw new Error(`axis ${y} is out of bounds for array of dimension ${e}`);if(f.has(m))throw new Error("repeated axis in transpose");f.add(m)}i=r.map(y=>y<0?e+y:y)}let s=i.map(f=>n[f]),u=Array.from(o),c=i.map(f=>u[f]);return D.fromData(a,s,l,c,t.offset)}function vr(t,r){let n=t.shape,e=n.length,o=t.strides,a=t.data,l=t.dtype;if(r===void 0){let i=[],s=[];for(let u=0;u<e;u++)n[u]!==1&&(i.push(n[u]),s.push(o[u]));return i.length===0&&(i.push(1),s.push(1)),D.fromData(a,i,l,s,t.offset)}else{let i=r<0?e+r:r;if(i<0||i>=e)throw new Error(`axis ${r} is out of bounds for array of dimension ${e}`);if(n[i]!==1)throw new Error(`cannot select an axis which has size not equal to one (axis ${r} has size ${n[i]})`);let s=[],u=[];for(let c=0;c<e;c++)c!==i&&(s.push(n[c]),u.push(o[c]));return D.fromData(a,s,l,u,t.offset)}}function Mr(t,r){let n=t.shape,e=n.length,o=t.strides,a=t.data,l=t.dtype,i=r;if(i<0&&(i=e+r+1),i<0||i>e)throw new Error(`axis ${r} is out of bounds for array of dimension ${e+1}`);let s=[...Array.from(n)];s.splice(i,0,1);let u=[...Array.from(o)],c=i<e?o[i]*(n[i]||1):1;return u.splice(i,0,c),D.fromData(a,s,l,u,t.offset)}function Lt(t,r,n,e,o,a,l,i,s,u,c,f,y,m){if(f===0)for(let g=0;g<e*o;g++)y[g]=0;else if(f!==1)for(let g=0;g<e*o;g++)y[g]=(y[g]??0)*f;let p=t==="row-major",d=r==="transpose",A=n==="transpose";if(p&&!d&&!A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[g*s+b]??0)*(u[b*c+h]??0);y[g*m+h]=(y[g*m+h]??0)+l*S}else if(p&&d&&!A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[b*s+g]??0)*(u[b*c+h]??0);y[g*m+h]=(y[g*m+h]??0)+l*S}else if(p&&!d&&A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[g*s+b]??0)*(u[h*c+b]??0);y[g*m+h]=(y[g*m+h]??0)+l*S}else if(p&&d&&A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[b*s+g]??0)*(u[h*c+b]??0);y[g*m+h]=(y[g*m+h]??0)+l*S}else if(!p&&!d&&!A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[b*s+g]??0)*(u[h*c+b]??0);y[h*m+g]=(y[h*m+g]??0)+l*S}else if(!p&&d&&!A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[g*s+b]??0)*(u[h*c+b]??0);y[h*m+g]=(y[h*m+g]??0)+l*S}else if(!p&&!d&&A)for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[b*s+g]??0)*(u[b*c+h]??0);y[h*m+g]=(y[h*m+g]??0)+l*S}else for(let g=0;g<e;g++)for(let h=0;h<o;h++){let S=0;for(let b=0;b<a;b++)S+=(i[g*s+b]??0)*(u[b*c+h]??0);y[h*m+g]=(y[h*m+g]??0)+l*S}}function er(t,r){let n=t.ndim,e=r.ndim;if(n===0||e===0){let o=n===0?t.get():null,a=e===0?r.get():null;if(n===0&&e===0)return typeof o=="bigint"&&typeof a=="bigint"?o*a:Number(o)*Number(a);if(n===0){let l=_(t.dtype,r.dtype),i=D.zeros([...r.shape],l);for(let s=0;s<r.size;s++){let u=r.data[s+r.offset];typeof o=="bigint"&&typeof u=="bigint"?i.data[s]=o*u:i.data[s]=Number(o)*Number(u)}return i}else{let l=_(t.dtype,r.dtype),i=D.zeros([...t.shape],l);for(let s=0;s<t.size;s++){let u=t.data[s+t.offset];typeof u=="bigint"&&typeof a=="bigint"?i.data[s]=u*a:i.data[s]=Number(u)*Number(a)}return i}}if(n===1&&e===1){if(t.shape[0]!==r.shape[0])throw new Error(`dot: incompatible shapes (${t.shape[0]},) and (${r.shape[0]},)`);let o=t.shape[0],a=0;for(let l=0;l<o;l++){let i=t.get(l),s=r.get(l);typeof i=="bigint"&&typeof s=="bigint"?a=Number(a)+Number(i*s):a+=Number(i)*Number(s)}return a}if(n===2&&e===2)return nr(t,r);if(n===2&&e===1){let[o,a]=t.shape,l=r.shape[0];if(a!==l)throw new Error(`dot: incompatible shapes (${o},${a}) and (${l},)`);let i=_(t.dtype,r.dtype),s=D.zeros([o],i);for(let u=0;u<o;u++){let c=0;for(let f=0;f<a;f++){let y=t.get(u,f),m=r.get(f);typeof y=="bigint"&&typeof m=="bigint"?c=Number(c)+Number(y*m):c+=Number(y)*Number(m)}s.set([u],c)}return s}if(n===1&&e===2){let o=t.shape[0],[a,l]=r.shape;if(o!==a)throw new Error(`dot: incompatible shapes (${o},) and (${a},${l})`);let i=_(t.dtype,r.dtype),s=D.zeros([l],i);for(let u=0;u<l;u++){let c=0;for(let f=0;f<o;f++){let y=t.get(f),m=r.get(f,u);typeof y=="bigint"&&typeof m=="bigint"?c=Number(c)+Number(y*m):c+=Number(y)*Number(m)}s.set([u],c)}return s}if(n>2&&e===1){let o=t.shape[n-1],a=r.shape[0];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and (${a},)`);let l=[...t.shape.slice(0,-1)],i=_(t.dtype,r.dtype),s=D.zeros(l,i),u=l.reduce((c,f)=>c*f,1);for(let c=0;c<u;c++){let f=0,y=c,m=[];for(let p=l.length-1;p>=0;p--)m[p]=y%l[p],y=Math.floor(y/l[p]);for(let p=0;p<o;p++){let d=[...m,p],A=t.get(...d),g=r.get(p);typeof A=="bigint"&&typeof g=="bigint"?f=Number(f)+Number(A*g):f+=Number(A)*Number(g)}s.set(m,f)}return s}if(n===1&&e>2){let o=t.shape[0],a=1,l=r.shape[a];if(o!==l)throw new Error(`dot: incompatible shapes (${o},) and ${JSON.stringify(r.shape)}`);let i=[...r.shape.slice(0,a),...r.shape.slice(a+1)],s=_(t.dtype,r.dtype),u=D.zeros(i,s),c=i.reduce((f,y)=>f*y,1);for(let f=0;f<c;f++){let y=f,m=[];for(let g=i.length-1;g>=0;g--)m[g]=y%i[g],y=Math.floor(y/i[g]);let p=m.slice(0,a),d=m.slice(a),A=0;for(let g=0;g<o;g++){let h=t.get(g),S=[...p,g,...d],b=r.get(...S);typeof h=="bigint"&&typeof b=="bigint"?A=Number(A)+Number(h*b):A+=Number(h)*Number(b)}u.set(m,A)}return u}if(n>=2&&e>=2&&!(n===2&&e===2)){let o=t.shape[n-1],a=r.shape[e-2];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and ${JSON.stringify(r.shape)}`);let l=[...t.shape.slice(0,-1),...r.shape.slice(0,-2),r.shape[e-1]],i=_(t.dtype,r.dtype),s=D.zeros(l,i),u=t.shape.slice(0,-1).reduce((m,p)=>m*p,1),c=r.shape.slice(0,-2).reduce((m,p)=>m*p,1),f=r.shape[e-1],y=o;for(let m=0;m<u;m++)for(let p=0;p<c;p++)for(let d=0;d<f;d++){let A=0;for(let h=0;h<y;h++){let S=m*y+h,b=t.data[S+t.offset],I=p*y*f+h*f+d,w=r.data[I+r.offset];typeof b=="bigint"&&typeof w=="bigint"?A=Number(A)+Number(b*w):A+=Number(b)*Number(w)}let g=m*c*f+p*f+d;s.data[g]=A}return s}throw new Error(`dot: unexpected combination of dimensions ${n}D \xB7 ${e}D`)}function nr(t,r){if(t.ndim!==2||r.ndim!==2)throw new Error("matmul requires 2D arrays");let[n=0,e=0]=t.shape,[o=0,a=0]=r.shape;if(e!==o)throw new Error(`matmul shape mismatch: (${n},${e}) @ (${o},${a})`);let l=_(t.dtype,r.dtype),i=l.startsWith("int")||l.startsWith("uint")||l==="bool"?"float64":l;if(i!=="float64")throw new Error(`matmul currently only supports float64, got ${i}`);let s=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number)),u=r.dtype==="float64"?r.data:Float64Array.from(Array.from(r.data).map(Number));t.offset>0&&(s=s.subarray(t.offset)),r.offset>0&&(u=u.subarray(r.offset));let[c=0,f=0]=t.strides,[y=0,m=0]=r.strides,p=f>c,d=m>y,A=p?"transpose":"no-transpose",g=d?"transpose":"no-transpose",h,S;p?h=f:h=c,d?S=m:S=y;let b=D.zeros([n,a],"float64");return Lt("row-major",A,g,n,a,e,1,s,h,u,S,0,b.data,a),b}function Cr(t){if(t.ndim!==2)throw new Error(`trace requires 2D array, got ${t.ndim}D`);let[r=0,n=0]=t.shape,e=Math.min(r,n),o=0;for(let a=0;a<e;a++){let l=t.get(a,a);typeof l=="bigint"?o=(typeof o=="bigint"?o:BigInt(o))+l:o=(typeof o=="bigint"?Number(o):o)+l}return o}function Fr(t,r){let n=t.ndim,e=r.ndim,o=t.shape[n-1],a=r.shape[e-1];if(o!==a)throw new Error(`inner: incompatible shapes - last dimensions ${o} and ${a} don't match`);if(n===1&&e===1)return er(t,r);let l=[...t.shape.slice(0,-1),...r.shape.slice(0,-1)],i=_(t.dtype,r.dtype),s=D.zeros(l,i),u=n===1?1:t.shape.slice(0,-1).reduce((y,m)=>y*m,1),c=e===1?1:r.shape.slice(0,-1).reduce((y,m)=>y*m,1),f=o;for(let y=0;y<u;y++)for(let m=0;m<c;m++){let p=0;for(let A=0;A<f;A++){let g=n===1?A:y*f+A,h=e===1?A:m*f+A,S=t.data[g+t.offset],b=r.data[h+r.offset];typeof S=="bigint"&&typeof b=="bigint"?p=Number(p)+Number(S*b):p+=Number(S)*Number(b)}if(l.length===0)return p;let d=u===1?m:y*c+m;s.data[d]=p}return s}function kr(t,r){let n=t.ndim===1?t:F(t),e=r.ndim===1?r:F(r),o=n.size,a=e.size,l=_(t.dtype,r.dtype),i=D.zeros([o,a],l);for(let s=0;s<o;s++)for(let u=0;u<a;u++){let c=n.get(s),f=e.get(u),y;typeof c=="bigint"&&typeof f=="bigint"?y=c*f:y=Number(c)*Number(f),i.set([s,u],y)}return i}function qr(t,r,n){let e,o;if(typeof n=="number"){let y=n;if(y<0)throw new Error("tensordot: axes must be non-negative");if(y>t.ndim||y>r.ndim)throw new Error("tensordot: axes exceeds array dimensions");e=Array.from({length:y},(m,p)=>t.ndim-y+p),o=Array.from({length:y},(m,p)=>p)}else if([e,o]=n,e.length!==o.length)throw new Error("tensordot: axes lists must have same length");for(let y=0;y<e.length;y++){let m=e[y],p=o[y];if(m<0||m>=t.ndim||p<0||p>=r.ndim)throw new Error("tensordot: axis out of bounds");if(t.shape[m]!==r.shape[p])throw new Error(`tensordot: shape mismatch on axes ${m} and ${p}: ${t.shape[m]} != ${r.shape[p]}`)}let a=[],l=[];for(let y=0;y<t.ndim;y++)e.includes(y)||a.push(y);for(let y=0;y<r.ndim;y++)o.includes(y)||l.push(y);let i=[...a.map(y=>t.shape[y]),...l.map(y=>r.shape[y])];if(i.length===0){let y=0,m=e.map(p=>t.shape[p]).reduce((p,d)=>p*d,1);for(let p=0;p<m;p++){let d=p,A=new Array(e.length);for(let I=e.length-1;I>=0;I--){let w=e[I];A[I]=d%t.shape[w],d=Math.floor(d/t.shape[w])}let g=new Array(t.ndim),h=new Array(r.ndim);for(let I=0;I<e.length;I++)g[e[I]]=A[I];for(let I=0;I<o.length;I++)h[o[I]]=A[I];let S=t.get(...g),b=r.get(...h);typeof S=="bigint"&&typeof b=="bigint"?y=Number(y)+Number(S*b):y+=Number(S)*Number(b)}return y}let s=_(t.dtype,r.dtype),u=D.zeros(i,s),c=i.reduce((y,m)=>y*m,1),f=e.map(y=>t.shape[y]).reduce((y,m)=>y*m,1);for(let y=0;y<c;y++){let m=y,p=[];for(let h=i.length-1;h>=0;h--)p[h]=m%i[h],m=Math.floor(m/i[h]);let d=p.slice(0,a.length),A=p.slice(a.length),g=0;for(let h=0;h<f;h++){m=h;let S=[];for(let N=e.length-1;N>=0;N--){let ur=e[N];S[N]=m%t.shape[ur],m=Math.floor(m/t.shape[ur])}let b=new Array(t.ndim),I=new Array(r.ndim);for(let N=0;N<a.length;N++)b[a[N]]=d[N];for(let N=0;N<l.length;N++)I[l[N]]=A[N];for(let N=0;N<e.length;N++)b[e[N]]=S[N],I[o[N]]=S[N];let w=t.get(...b),$=r.get(...I);typeof w=="bigint"&&typeof $=="bigint"?g=Number(g)+Number(w*$):g+=Number(w)*Number($)}u.set(p,g)}return u}function jr(t){return pr(t,Math.sqrt,!1)}function Lr(t,r){return typeof r=="number"?Wt(t,r):O(t,r,Math.pow,"power")}function Wt(t,r){let n=t.dtype,e=Array.from(t.shape),o=t.data,a=t.size,s=n!=="float32"&&n!=="float64"&&(r<0||!Number.isInteger(r))?"float64":n,u=D.zeros(e,s),c=u.data;if(x(n))if(x(s)&&Number.isInteger(r)&&r>=0){let f=o,y=c;for(let m=0;m<a;m++)y[m]=f[m]**BigInt(r)}else for(let f=0;f<a;f++)c[f]=Math.pow(Number(o[f]),r);else for(let f=0;f<a;f++)c[f]=Math.pow(Number(o[f]),r);return u}var E=class t{constructor(r,n){this._storage=r,this._base=n}get storage(){return this._storage}static _fromStorage(r,n){return new t(r,n)}get shape(){return this._storage.shape}get ndim(){return this._storage.ndim}get size(){return this._storage.size}get dtype(){return this._storage.dtype}get data(){return this._storage.data}get strides(){return this._storage.strides}get flags(){return{C_CONTIGUOUS:this._storage.isCContiguous,F_CONTIGUOUS:this._storage.isFContiguous,OWNDATA:this._base===void 0}}get base(){return this._base??null}get(r){if(r.length!==this.ndim)throw new Error(`Index has ${r.length} dimensions, but array has ${this.ndim} dimensions`);let n=r.map((e,o)=>{let a=e;if(a<0&&(a=this.shape[o]+a),a<0||a>=this.shape[o])throw new Error(`Index ${e} is out of bounds for axis ${o} with size ${this.shape[o]}`);return a});return this._storage.get(...n)}set(r,n){if(r.length!==this.ndim)throw new Error(`Index has ${r.length} dimensions, but array has ${this.ndim} dimensions`);let e=r.map((l,i)=>{let s=l;if(s<0&&(s=this.shape[i]+s),s<0||s>=this.shape[i])throw new Error(`Index ${l} is out of bounds for axis ${i} with size ${this.shape[i]}`);return s}),o=this.dtype,a;x(o)?a=typeof n=="bigint"?n:BigInt(Math.round(n)):o==="bool"?a=n?1:0:a=Number(n),this._storage.set(e,a)}copy(){return new t(this._storage.copy())}astype(r,n=!0){let e=this.dtype;if(e===r&&!n)return this;if(e===r&&n)return this.copy();let o=Array.from(this.shape),a=this.size,l=U(r);if(!l)throw new Error(`Cannot convert to dtype ${r}`);let i=new l(a),s=this.data;if(x(e)&&!x(r)){let c=s;if(r==="bool")for(let f=0;f<a;f++)i[f]=c[f]!==BigInt(0)?1:0;else for(let f=0;f<a;f++)i[f]=Number(c[f])}else if(!x(e)&&x(r)){let c=s;for(let f=0;f<a;f++)i[f]=BigInt(Math.round(Number(c[f])))}else if(r==="bool"){let c=s;for(let f=0;f<a;f++)i[f]=c[f]!==0?1:0}else if(e==="bool"&&!x(r)){let c=s;for(let f=0;f<a;f++)i[f]=c[f]}else if(!x(e)&&!x(r)){let c=s;for(let f=0;f<a;f++)i[f]=c[f]}else{let c=s;for(let f=0;f<a;f++)i[f]=c[f]}let u=D.fromData(i,o,r);return new t(u)}add(r){let n=typeof r=="number"?r:r._storage,e=mr(this._storage,n);return t._fromStorage(e)}subtract(r){let n=typeof r=="number"?r:r._storage,e=dr(this._storage,n);return t._fromStorage(e)}multiply(r){let n=typeof r=="number"?r:r._storage,e=gr(this._storage,n);return t._fromStorage(e)}divide(r){let n=typeof r=="number"?r:r._storage,e=hr(this._storage,n);return t._fromStorage(e)}mod(r){let n=typeof r=="number"?r:r._storage,e=Dr(this._storage,n);return t._fromStorage(e)}floor_divide(r){let n=typeof r=="number"?r:r._storage,e=xr(this._storage,n);return t._fromStorage(e)}positive(){let r=Ir(this._storage);return t._fromStorage(r)}reciprocal(){let r=wr(this._storage);return t._fromStorage(r)}sqrt(){let r=jr(this._storage);return t._fromStorage(r)}power(r){let n=typeof r=="number"?r:r._storage,e=Lr(this._storage,n);return t._fromStorage(e)}absolute(){let r=Ar(this._storage);return t._fromStorage(r)}negative(){let r=br(this._storage);return t._fromStorage(r)}sign(){let r=Sr(this._storage);return t._fromStorage(r)}greater(r){let n=typeof r=="number"?r:r._storage,e=Nr(this._storage,n);return t._fromStorage(e)}greater_equal(r){let n=typeof r=="number"?r:r._storage,e=Br(this._storage,n);return t._fromStorage(e)}less(r){let n=typeof r=="number"?r:r._storage,e=zr(this._storage,n);return t._fromStorage(e)}less_equal(r){let n=typeof r=="number"?r:r._storage,e=_r(this._storage,n);return t._fromStorage(e)}equal(r){let n=typeof r=="number"?r:r._storage,e=Tr(this._storage,n);return t._fromStorage(e)}not_equal(r){let n=typeof r=="number"?r:r._storage,e=Ur(this._storage,n);return t._fromStorage(e)}isclose(r,n=1e-5,e=1e-8){let o=typeof r=="number"?r:r._storage,a=W(this._storage,o,n,e);return t._fromStorage(a)}allclose(r,n=1e-5,e=1e-8){let o=typeof r=="number"?r:r._storage;return Er(this._storage,o,n,e)}sum(r,n=!1){let e=C(this._storage,r,n);return typeof e=="number"?e:t._fromStorage(e)}mean(r,n=!1){let e=J(this._storage,r,n);return typeof e=="number"?e:t._fromStorage(e)}max(r,n=!1){let e=P(this._storage,r,n);return typeof e=="number"?e:t._fromStorage(e)}min(r,n=!1){let e=H(this._storage,r,n);return typeof e=="number"?e:t._fromStorage(e)}prod(r,n=!1){let e=G(this._storage,r,n);return typeof e=="number"?e:t._fromStorage(e)}argmin(r){let n=Y(this._storage,r);return typeof n=="number"?n:t._fromStorage(n)}argmax(r){let n=Q(this._storage,r);return typeof n=="number"?n:t._fromStorage(n)}var(r,n=0,e=!1){let o=X(this._storage,r,n,e);return typeof o=="number"?o:t._fromStorage(o)}std(r,n=0,e=!1){let o=$r(this._storage,r,n,e);return typeof o=="number"?o:t._fromStorage(o)}all(r,n=!1){let e=Z(this._storage,r,n);return typeof e=="boolean"?e:t._fromStorage(e)}any(r,n=!1){let e=K(this._storage,r,n);return typeof e=="boolean"?e:t._fromStorage(e)}reshape(...r){let n=r.length===1&&Array.isArray(r[0])?r[0]:r,e=Or(this._storage,n),a=e.data===this.data?this._base??this:void 0;return t._fromStorage(e,a)}flatten(){let r=rr(this._storage);return t._fromStorage(r)}ravel(){let r=F(this._storage),e=r.data===this.data?this._base??this:void 0;return t._fromStorage(r,e)}transpose(r){let n=tr(this._storage,r),e=this._base??this;return t._fromStorage(n,e)}squeeze(r){let n=vr(this._storage,r),e=this._base??this;return t._fromStorage(n,e)}expand_dims(r){let n=Mr(this._storage,r),e=this._base??this;return t._fromStorage(n,e)}matmul(r){let n=nr(this._storage,r._storage);return t._fromStorage(n)}dot(r){let n=er(this._storage,r._storage);return typeof n=="number"||typeof n=="bigint"?n:t._fromStorage(n)}trace(){return Cr(this._storage)}inner(r){let n=Fr(this._storage,r._storage);return typeof n=="number"||typeof n=="bigint"?n:t._fromStorage(n)}outer(r){let n=kr(this._storage,r._storage);return t._fromStorage(n)}tensordot(r,n=2){let e=qr(this._storage,r._storage,n);return typeof e=="number"||typeof e=="bigint"?e:t._fromStorage(e)}slice(...r){if(r.length===0)return this;if(r.length>this.ndim)throw new Error(`Too many indices for array: array is ${this.ndim}-dimensional, but ${r.length} were indexed`);let n=r.map((s,u)=>{let c=lr(s);return fr(c,this.shape[u])});for(;n.length<this.ndim;)n.push({start:0,stop:this.shape[n.length],step:1,isIndex:!1});let e=[],o=[],a=this._storage.offset;for(let s=0;s<n.length;s++){let u=n[s],c=this._storage.strides[s];if(a+=u.start*c,!u.isIndex){let f;u.step>0?f=Math.max(0,Math.ceil((u.stop-u.start)/u.step)):f=Math.max(0,Math.ceil((u.start-u.stop)/Math.abs(u.step))),e.push(f),o.push(c*u.step)}}let l=D.fromData(this._storage.data,e,this._storage.dtype,o,a),i=this._base??this;return new t(l,i)}row(r){if(this.ndim<2)throw new Error("row() requires at least 2 dimensions");return this.slice(String(r),":")}col(r){if(this.ndim<2)throw new Error("col() requires at least 2 dimensions");return this.slice(":",String(r))}rows(r,n){if(this.ndim<2)throw new Error("rows() requires at least 2 dimensions");return this.slice(`${r}:${n}`,":")}cols(r,n){if(this.ndim<2)throw new Error("cols() requires at least 2 dimensions");return this.slice(":",`${r}:${n}`)}toString(){return`NDArray(shape=${JSON.stringify(this.shape)}, dtype=${this.dtype})`}toArray(){if(this.ndim===0)return this._storage.iget(0);let r=this.shape,n=r.length,e=(o,a)=>{if(a===n)return this._storage.get(...o);let l=[];for(let i=0;i<r[a];i++)o[a]=i,l.push(e(o,a+1));return l};return e(new Array(n),0)}};function k(t,r=T){let n=D.zeros(t,r);return new E(n)}function or(t,r=T){let n=D.ones(t,r);return new E(n)}function Pt(t){let r=[],n=t;for(;Array.isArray(n);)r.push(n.length),n=n[0];return r}function Rr(t){return typeof t=="bigint"?!0:Array.isArray(t)?t.some(r=>Rr(r)):!1}function Gt(t){let r=[];function n(e){Array.isArray(e)?e.forEach(o=>n(o)):r.push(e)}return n(t),r}function v(t,r){if(t instanceof E)return!r||t.dtype===r?t.copy():t.astype(r);let n=Rr(t),e=Pt(t),o=e.reduce((c,f)=>c*f,1),a=r;a||(n?a="int64":a=T);let l=U(a);if(!l)throw new Error(`Cannot create array with dtype ${a}`);let i=new l(o),s=Gt(t);if(x(a)){let c=i;for(let f=0;f<o;f++){let y=s[f];c[f]=typeof y=="bigint"?y:BigInt(Math.round(Number(y)))}}else if(a==="bool"){let c=i;for(let f=0;f<o;f++)c[f]=s[f]?1:0}else{let c=i;for(let f=0;f<o;f++){let y=s[f];c[f]=Number(y)}}let u=D.fromData(i,e,a);return new E(u)}function Wr(t,r,n=1,e=T){let o=t,a=r;if(r===void 0&&(o=0,a=t),a===void 0)throw new Error("stop is required");let l=Math.max(0,Math.ceil((a-o)/n)),i=U(e);if(!i)throw new Error(`Cannot create arange array with dtype ${e}`);let s=new i(l);if(x(e))for(let c=0;c<l;c++)s[c]=BigInt(Math.round(o+c*n));else if(e==="bool")for(let c=0;c<l;c++)s[c]=o+c*n!==0?1:0;else for(let c=0;c<l;c++)s[c]=o+c*n;let u=D.fromData(s,[l],e);return new E(u)}function Jr(t,r,n=50,e=T){if(n<0)throw new Error("num must be non-negative");if(n===0)return v([],e);if(n===1)return v([t],e);let o=U(e);if(!o)throw new Error(`Cannot create linspace array with dtype ${e}`);let a=new o(n),l=(r-t)/(n-1);if(x(e))for(let s=0;s<n;s++)a[s]=BigInt(Math.round(t+s*l));else if(e==="bool")for(let s=0;s<n;s++)a[s]=t+s*l!==0?1:0;else for(let s=0;s<n;s++)a[s]=t+s*l;let i=D.fromData(a,[n],e);return new E(i)}function Pr(t,r,n=50,e=10,o=T){if(n<0)throw new Error("num must be non-negative");if(n===0)return v([],o);if(n===1)return v([Math.pow(e,t)],o);let a=U(o);if(!a)throw new Error(`Cannot create logspace array with dtype ${o}`);let l=new a(n),i=(r-t)/(n-1);if(x(o))for(let u=0;u<n;u++){let c=t+u*i;l[u]=BigInt(Math.round(Math.pow(e,c)))}else if(o==="bool")for(let u=0;u<n;u++){let c=t+u*i;l[u]=Math.pow(e,c)!==0?1:0}else for(let u=0;u<n;u++){let c=t+u*i;l[u]=Math.pow(e,c)}let s=D.fromData(l,[n],o);return new E(s)}function Gr(t,r,n=50,e=T){if(n<0)throw new Error("num must be non-negative");if(t===0||r===0)throw new Error("Geometric sequence cannot include zero");if(n===0)return v([],e);if(n===1)return v([t],e);let o=Math.sign(t),a=Math.sign(r);if(o!==a)throw new Error("Geometric sequence cannot contain both positive and negative values");let l=U(e);if(!l)throw new Error(`Cannot create geomspace array with dtype ${e}`);let i=new l(n),s=Math.log(Math.abs(t)),c=(Math.log(Math.abs(r))-s)/(n-1);if(x(e))for(let y=0;y<n;y++){let m=o*Math.exp(s+y*c);i[y]=BigInt(Math.round(m))}else if(e==="bool")for(let y=0;y<n;y++){let m=o*Math.exp(s+y*c);i[y]=m!==0?1:0}else for(let y=0;y<n;y++){let m=o*Math.exp(s+y*c);i[y]=m}let f=D.fromData(i,[n],e);return new E(f)}function sr(t,r,n=0,e=T){let o=r??t,a=k([t,o],e),l=a.data;if(x(e)){let i=l;for(let s=0;s<t;s++){let u=s+n;u>=0&&u<o&&(i[s*o+u]=BigInt(1))}}else{let i=l;for(let s=0;s<t;s++){let u=s+n;u>=0&&u<o&&(i[s*o+u]=1)}}return a}function ar(t,r=T){return k(t,r)}function ir(t,r,n){let e=n;e||(typeof r=="bigint"?e="int64":typeof r=="boolean"?e="bool":Number.isInteger(r)?e="int32":e=T);let o=U(e);if(!o)throw new Error(`Cannot create full array with dtype ${e}`);let a=t.reduce((s,u)=>s*u,1),l=new o(a);if(x(e)){let s=typeof r=="bigint"?r:BigInt(Math.round(Number(r)));l.fill(s)}else e==="bool"?l.fill(r?1:0):l.fill(Number(r));let i=D.fromData(l,t,e);return new E(i)}function Hr(t,r=T){return sr(t,t,0,r)}function Yr(t,r){return t instanceof E?!r||t.dtype===r?t:t.astype(r):v(t,r)}function Qr(t){return t.copy()}function Xr(t,r){return k(Array.from(t.shape),r??t.dtype)}function Zr(t,r){return or(Array.from(t.shape),r??t.dtype)}function Kr(t,r){return ar(Array.from(t.shape),r??t.dtype)}function rt(t,r,n){return ir(Array.from(t.shape),r,n??t.dtype)}function tt(t){return t.sqrt()}function et(t,r){return t.power(r)}function nt(t){return t.absolute()}function ot(t){return t.negative()}function st(t){return t.sign()}function at(t,r){return t.mod(r)}function it(t,r){return t.floor_divide(r)}function ut(t){return t.positive()}function lt(t){return t.reciprocal()}function ft(t,r){return t.dot(r)}function ct(t){return t.trace()}function yt(t,r){return t.transpose(r)}function pt(t,r){return t.inner(r)}function mt(t,r){return t.outer(r)}function dt(t,r,n=2){return t.tensordot(r,n)}var Ht="0.4.0";0&&(module.exports={NDArray,__version__,absolute,arange,array,asarray,copy,dot,empty,empty_like,eye,floor_divide,full,full_like,geomspace,identity,inner,linspace,logspace,mod,negative,ones,ones_like,outer,positive,power,reciprocal,sign,sqrt,tensordot,trace,transpose,zeros,zeros_like});
1
+ "use strict";var fr=Object.defineProperty;var bn=Object.getOwnPropertyDescriptor;var Sn=Object.getOwnPropertyNames;var Dn=Object.prototype.hasOwnProperty;var wn=(r,t)=>{for(var e in t)fr(r,e,{get:t[e],enumerable:!0})},Nn=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Sn(t))!Dn.call(r,o)&&o!==e&&fr(r,o,{get:()=>t[o],enumerable:!(n=bn(t,o))||n.enumerable});return r};var xn=r=>Nn(fr({},"__esModule",{value:!0}),r);var So={};wn(So,{DTYPE_TO_DESCR:()=>Q,InvalidNpyError:()=>C,NDArray:()=>z,SUPPORTED_DTYPES:()=>sr,UnsupportedDTypeError:()=>v,__version__:()=>bo,absolute:()=>fe,arange:()=>Qt,arccos:()=>Ue,arccosh:()=>Ve,arcsin:()=>Be,arcsinh:()=>ve,arctan:()=>_e,arctan2:()=>Ee,arctanh:()=>Pe,array:()=>V,array_equal:()=>on,array_split:()=>We,asarray:()=>ne,broadcast_arrays:()=>rn,broadcast_to:()=>Ke,choose:()=>nn,concatenate:()=>Le,copy:()=>oe,cos:()=>Ie,cosh:()=>Ce,degrees:()=>Oe,dot:()=>Ae,dstack:()=>He,empty:()=>Ur,empty_like:()=>ie,eye:()=>Br,floor_divide:()=>de,full:()=>_r,full_like:()=>ue,geomspace:()=>te,hsplit:()=>Je,hstack:()=>Ze,hypot:()=>Te,identity:()=>ee,inner:()=>De,linspace:()=>Kt,loadNpz:()=>Mr,loadNpzSync:()=>Cr,logspace:()=>re,mod:()=>me,moveaxis:()=>Re,negative:()=>pe,ones:()=>zr,ones_like:()=>se,outer:()=>we,parseNpy:()=>K,parseNpyData:()=>ur,parseNpyHeader:()=>ir,parseNpz:()=>cr,parseNpzSync:()=>lr,positive:()=>ge,power:()=>le,put:()=>en,radians:()=>$e,reciprocal:()=>he,repeat:()=>Qe,serializeNpy:()=>q,serializeNpz:()=>kr,serializeNpzSync:()=>vr,sign:()=>ye,sin:()=>xe,sinh:()=>Me,split:()=>Ye,sqrt:()=>ce,stack:()=>je,swapaxes:()=>Fe,take:()=>tn,tan:()=>ze,tanh:()=>ke,tensordot:()=>Ne,tile:()=>Xe,trace:()=>be,transpose:()=>Se,vsplit:()=>Ge,vstack:()=>qe,zeros:()=>J,zeros_like:()=>ae});module.exports=xn(So);function Pr(r){if(!r.includes(":")){if(r.includes("."))throw new Error(`Invalid slice index: "${r}" (must be integer)`);let a=parseInt(r,10);if(isNaN(a))throw new Error(`Invalid slice index: "${r}"`);return{start:a,stop:null,step:1,isIndex:!0}}let t=r.split(":");if(t.length>3)throw new Error(`Invalid slice notation: "${r}" (too many colons)`);let e=t[0]===""?null:parseInt(t[0],10),n=t[1]===""||t[1]===void 0?null:parseInt(t[1],10),o=t[2]===""||t[2]===void 0?1:parseInt(t[2],10);if(e!==null&&isNaN(e))throw new Error(`Invalid start index in slice: "${r}"`);if(n!==null&&isNaN(n))throw new Error(`Invalid stop index in slice: "${r}"`);if(isNaN(o))throw new Error(`Invalid step in slice: "${r}"`);if(o===0)throw new Error("Slice step cannot be zero");return{start:e,stop:n,step:o,isIndex:!1}}function Fr(r,t){let{start:e,stop:n}=r,{step:o,isIndex:a}=r;if(a){if(e===null)throw new Error("Index cannot be null");let c=e<0?t+e:e;if(c<0||c>=t)throw new Error(`Index ${e} is out of bounds for size ${t}`);return{start:c,stop:c+1,step:1,isIndex:!0}}return o>0?(e===null&&(e=0),n===null&&(n=t)):(e===null&&(e=t-1),n===null&&(n=-t-1)),e<0&&(e=t+e),n<0&&(n=t+n),e=Math.max(0,Math.min(e,t)),n=Math.max(-1,Math.min(n,t)),{start:e,stop:n,step:o,isIndex:!1}}var M="float64";function B(r){switch(r){case"float64":return Float64Array;case"float32":return Float32Array;case"int64":return BigInt64Array;case"int32":return Int32Array;case"int16":return Int16Array;case"int8":return Int8Array;case"uint64":return BigUint64Array;case"uint32":return Uint32Array;case"uint16":return Uint16Array;case"uint8":return Uint8Array;case"bool":return Uint8Array;default:throw new Error(`Unknown dtype: ${r}`)}}function Lr(r){switch(r){case"float64":case"int64":case"uint64":return 8;case"float32":case"int32":case"uint32":return 4;case"int16":case"uint16":return 2;case"int8":case"uint8":case"bool":return 1;default:throw new Error(`Unknown dtype: ${r}`)}}function Rr(r){return r==="float64"||r==="float32"}function w(r){return r==="int64"||r==="uint64"}function O(r,t){if(r===t)return r;if(r==="bool")return t;if(t==="bool")return r;if(Rr(r)||Rr(t)){if(r==="float64"||t==="float64")return"float64";if(r==="float32"){let i=t;return i==="int32"||i==="int64"||i==="uint32"||i==="uint64"?"float64":"float32"}if(t==="float32"){let i=r;return i==="int32"||i==="int64"||i==="uint32"||i==="uint64"?"float64":"float32"}return"float32"}let e=r.startsWith("int"),n=t.startsWith("int"),o=r.startsWith("uint"),a=t.startsWith("uint"),c=i=>i.includes("64")?64:i.includes("32")?32:i.includes("16")?16:i.includes("8")?8:0,u=c(r),s=c(t);if(r==="int64"&&t==="uint64"||r==="uint64"&&t==="int64")return"float64";if(e&&a&&u===s){if(u===8)return"int16";if(u===16)return"int32";if(u===32)return"int64"}if(o&&n&&u===s){if(s===8)return"int16";if(s===16)return"int32";if(s===32)return"int64"}if(e&&n||o&&a){let i=Math.max(u,s);return e?i===64?"int64":i===32?"int32":i===16?"int16":"int8":i===64?"uint64":i===32?"uint32":i===16?"uint16":"uint8"}return e&&a?u>s?r:s===8?"int16":s===16?"int32":s===32?"int64":"float64":o&&n?s>u?t:u===8?"int16":u===16?"int32":u===32?"int64":"float64":"float64"}var D=class r{constructor(t,e,n,o,a){this._data=t,this._shape=e,this._strides=n,this._offset=o,this._dtype=a}get shape(){return this._shape}get ndim(){return this._shape.length}get size(){return this._shape.reduce((t,e)=>t*e,1)}get dtype(){return this._dtype}get data(){return this._data}get strides(){return this._strides}get offset(){return this._offset}get isCContiguous(){let t=this._shape,e=this._strides,n=t.length;if(n===0)return!0;if(n===1)return e[0]===1;let o=1;for(let a=n-1;a>=0;a--){if(e[a]!==o)return!1;o*=t[a]}return!0}get isFContiguous(){let t=this._shape,e=this._strides,n=t.length;if(n===0)return!0;if(n===1)return e[0]===1;let o=1;for(let a=0;a<n;a++){if(e[a]!==o)return!1;o*=t[a]}return!0}iget(t){let e=this._shape,n=this._strides,o=e.length;if(o===0)return this._data[this._offset];let a=t,c=this._offset;for(let u=0;u<o;u++){let s=1;for(let l=u+1;l<o;l++)s*=e[l];let i=Math.floor(a/s);a=a%s,c+=i*n[u]}return this._data[c]}iset(t,e){let n=this._shape,o=this._strides,a=n.length;if(a===0){this._data[this._offset]=e;return}let c=t,u=this._offset;for(let s=0;s<a;s++){let i=1;for(let f=s+1;f<a;f++)i*=n[f];let l=Math.floor(c/i);c=c%i,u+=l*o[s]}this._data[u]=e}get(...t){let e=this._strides,n=this._offset;for(let o=0;o<t.length;o++)n+=t[o]*e[o];return this._data[n]}set(t,e){let n=this._strides,o=this._offset;for(let a=0;a<t.length;a++)o+=t[a]*n[a];this._data[o]=e}copy(){let t=Array.from(this._shape),e=this._dtype,n=this.size,o=B(e);if(!o)throw new Error(`Cannot copy array with dtype ${e}`);let a=new o(n);if(this.isCContiguous&&this._offset===0)if(w(e)){let c=this._data,u=a;for(let s=0;s<n;s++)u[s]=c[s]}else a.set(this._data);else if(w(e)){let c=a;for(let u=0;u<n;u++)c[u]=this.iget(u)}else for(let c=0;c<n;c++)a[c]=this.iget(c);return new r(a,t,r._computeStrides(t),0,e)}static fromData(t,e,n,o,a){let c=o??r._computeStrides(e),u=a??0;return new r(t,e,c,u,n)}static zeros(t,e=M){let n=t.reduce((c,u)=>c*u,1),o=B(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return new r(a,t,r._computeStrides(t),0,e)}static ones(t,e=M){let n=t.reduce((c,u)=>c*u,1),o=B(e);if(!o)throw new Error(`Cannot create array with dtype ${e}`);let a=new o(n);return w(e)?a.fill(BigInt(1)):a.fill(1),new r(a,t,r._computeStrides(t),0,e)}static _computeStrides(t){let e=new Array(t.length),n=1;for(let o=t.length-1;o>=0;o--)e[o]=n,n*=t[o];return e}};function P(r){let t=new Array(r.length),e=1;for(let n=r.length-1;n>=0;n--)t[n]=e,e*=r[n];return t}function jr(r,t){let e=r.length,n=t.length,o=Math.max(e,n),a=new Array(o);for(let c=0;c<o;c++){let u=c<o-e?1:r[c-(o-e)],s=c<o-n?1:t[c-(o-n)];if(u===s)a[c]=u;else if(u===1)a[c]=s;else if(s===1)a[c]=u;else throw new Error(`operands could not be broadcast together with shapes ${JSON.stringify(Array.from(r))} ${JSON.stringify(Array.from(t))}`)}return a}function In(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let c=0;c<n;c++){let u=o-n+c,s=r[c],i=e[u];if(s===i)a[u]=t[c];else if(s===1)a[u]=0;else throw new Error("Invalid broadcast")}return a}function er(r,t){let e=In(r.shape,r.strides,t);return D.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function k(r,t,e,n){let o=jr(r.shape,t.shape),a=er(r,o),c=er(t,o),u=O(r.dtype,t.dtype),s=D.zeros(o,u),i=s.data,l=s.size;if(w(u)){let f=i;for(let p=0;p<l;p++){let m=a.iget(p),y=c.iget(p),d=typeof m=="bigint"?m:BigInt(Math.round(m)),A=typeof y=="bigint"?y:BigInt(Math.round(y));n==="add"?f[p]=d+A:n==="subtract"?f[p]=d-A:n==="multiply"?f[p]=d*A:n==="divide"?f[p]=d/A:f[p]=BigInt(Math.round(e(Number(d),Number(A))))}}else{let f=w(r.dtype)||w(t.dtype);for(let p=0;p<l;p++){let m=a.iget(p),y=c.iget(p),d=Number(m),A=Number(y);i[p]=e(d,A)}}return s}function F(r,t,e){let n=jr(r.shape,t.shape),o=er(r,n),a=er(t,n),c=n.reduce((i,l)=>i*l,1),u=new Uint8Array(c),s=w(r.dtype)||w(t.dtype);for(let i=0;i<c;i++){let l=o.iget(i),f=a.iget(i),p=Number(l),m=Number(f);u[i]=e(p,m)?1:0}return D.fromData(u,n,"bool")}function $(r,t,e=!0){let n=r.dtype,o=Array.from(r.shape),a=r.size,u=e?n:n!=="float32"&&n!=="float64"?"float64":n,s=D.zeros(o,u),i=s.data,l=r.data;if(w(n))if(w(u)){let f=i;for(let p=0;p<a;p++){let m=Number(l[p]);f[p]=BigInt(Math.round(t(m)))}}else for(let f=0;f<a;f++)i[f]=t(Number(l[f]));else for(let f=0;f<a;f++)i[f]=t(Number(l[f]));return s}function pr(r,t){return r.isCContiguous&&t.isCContiguous&&r.shape.length===t.shape.length&&r.shape.every((e,n)=>e===t.shape[n])}function qr(r,t){return typeof t=="number"?_n(r,t):pr(r,t)?zn(r,t):k(r,t,(e,n)=>e+n,"add")}function zn(r,t){let e=O(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,c=t.data,u=n.data;if(w(e)){let s=u;if(!w(r.dtype)||!w(t.dtype))for(let l=0;l<o;l++){let f=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),p=typeof c[l]=="bigint"?c[l]:BigInt(Math.round(Number(c[l])));s[l]=f+p}else{let l=a,f=c;for(let p=0;p<o;p++)s[p]=l[p]+f[p]}}else if(w(r.dtype)||w(t.dtype))for(let i=0;i<o;i++){let l=typeof a[i]=="bigint"?Number(a[i]):a[i],f=typeof c[i]=="bigint"?Number(c[i]):c[i];u[i]=l+f}else for(let i=0;i<o;i++)u[i]=a[i]+c[i];return n}function Zr(r,t){return typeof t=="number"?En(r,t):pr(r,t)?Bn(r,t):k(r,t,(e,n)=>e-n,"subtract")}function Bn(r,t){let e=O(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,c=t.data,u=n.data;if(w(e)){let s=u;if(!w(r.dtype)||!w(t.dtype))for(let l=0;l<o;l++){let f=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),p=typeof c[l]=="bigint"?c[l]:BigInt(Math.round(Number(c[l])));s[l]=f-p}else{let l=a,f=c;for(let p=0;p<o;p++)s[p]=l[p]-f[p]}}else if(w(r.dtype)||w(t.dtype))for(let i=0;i<o;i++){let l=typeof a[i]=="bigint"?Number(a[i]):a[i],f=typeof c[i]=="bigint"?Number(c[i]):c[i];u[i]=l-f}else for(let i=0;i<o;i++)u[i]=a[i]-c[i];return n}function Hr(r,t){return typeof t=="number"?Tn(r,t):pr(r,t)?Un(r,t):k(r,t,(e,n)=>e*n,"multiply")}function Un(r,t){let e=O(r.dtype,t.dtype),n=D.zeros(Array.from(r.shape),e),o=r.size,a=r.data,c=t.data,u=n.data;if(w(e)){let s=u;if(!w(r.dtype)||!w(t.dtype))for(let l=0;l<o;l++){let f=typeof a[l]=="bigint"?a[l]:BigInt(Math.round(Number(a[l]))),p=typeof c[l]=="bigint"?c[l]:BigInt(Math.round(Number(c[l])));s[l]=f*p}else{let l=a,f=c;for(let p=0;p<o;p++)s[p]=l[p]*f[p]}}else if(w(r.dtype)||w(t.dtype))for(let i=0;i<o;i++){let l=typeof a[i]=="bigint"?Number(a[i]):a[i],f=typeof c[i]=="bigint"?Number(c[i]):c[i];u[i]=l*f}else for(let i=0;i<o;i++)u[i]=a[i]*c[i];return n}function Yr(r,t){if(typeof t=="number")return On(r,t);let e=r.dtype==="float64",n=t.dtype==="float64",o=r.dtype==="float32",a=t.dtype==="float32";if(e||n){let s=e?r:L(r,"float64"),i=n?t:L(t,"float64");return k(s,i,(l,f)=>l/f,"divide")}if(o||a){let s=o?r:L(r,"float32"),i=a?t:L(t,"float32");return k(s,i,(l,f)=>l/f,"divide")}let c=L(r,"float64"),u=L(t,"float64");return k(c,u,(s,i)=>s/i,"divide")}function L(r,t){let e=D.zeros(Array.from(r.shape),t),n=r.size,o=r.data,a=e.data;for(let c=0;c<n;c++)a[c]=Number(o[c]);return e}function _n(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]+l}else for(let s=0;s<a;s++)u[s]=Number(o[s])+t;return c}function En(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]-l}else for(let s=0;s<a;s++)u[s]=Number(o[s])-t;return c}function Tn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]*l}else for(let s=0;s<a;s++)u[s]=Number(o[s])*t;return c}function On(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,u=e!=="float32"&&e!=="float64"?"float64":e,s=D.zeros(n,u),i=s.data;if(w(e))for(let l=0;l<a;l++)i[l]=Number(o[l])/t;else for(let l=0;l<a;l++)i[l]=Number(o[l])/t;return s}function Wr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;if(w(t)){let u=n,s=c;for(let i=0;i<o;i++){let l=u[i];s[i]=l<0n?-l:l}}else for(let u=0;u<o;u++)c[u]=Math.abs(Number(n[u]));return a}function Gr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;if(w(t)){let u=n,s=c;for(let i=0;i<o;i++)s[i]=-u[i]}else for(let u=0;u<o;u++)c[u]=-Number(n[u]);return a}function Jr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;if(w(t)){let u=n,s=c;for(let i=0;i<o;i++){let l=u[i];s[i]=l>0n?1n:l<0n?-1n:0n}}else for(let u=0;u<o;u++){let s=Number(n[u]);c[u]=s>0?1:s<0?-1:0}return a}function Xr(r,t){return typeof t=="number"?$n(r,t):k(r,t,(e,n)=>(e%n+n)%n,"mod")}function $n(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++){let p=s[f];i[f]=(p%l+l)%l}}else for(let s=0;s<a;s++){let i=Number(o[s]);u[s]=(i%t+t)%t}return c}function Qr(r,t){return typeof t=="number"?Mn(r,t):k(r,t,(e,n)=>Math.floor(e/n),"floor_divide")}function Mn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=D.zeros(n,e),u=c.data;if(w(e)){let s=o,i=u,l=BigInt(Math.round(t));for(let f=0;f<a;f++)i[f]=s[f]/l}else for(let s=0;s<a;s++)u[s]=Math.floor(Number(o[s])/t);return c}function Kr(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,a=D.zeros(e,t),c=a.data;for(let u=0;u<o;u++)c[u]=n[u];return a}function rt(r){let t=r.dtype,e=Array.from(r.shape),n=r.data,o=r.size,c=t!=="float32"&&t!=="float64"?"float64":t,u=D.zeros(e,c),s=u.data;if(w(t))for(let i=0;i<o;i++)s[i]=1/Number(n[i]);else for(let i=0;i<o;i++)s[i]=1/Number(n[i]);return u}function tt(r,t){return typeof t=="number"?kn(r,t):F(r,t,(e,n)=>e>n)}function et(r,t){return typeof t=="number"?vn(r,t):F(r,t,(e,n)=>e>=n)}function nt(r,t){return typeof t=="number"?Vn(r,t):F(r,t,(e,n)=>e<n)}function ot(r,t){return typeof t=="number"?Pn(r,t):F(r,t,(e,n)=>e<=n)}function at(r,t){return typeof t=="number"?Fn(r,t):F(r,t,(e,n)=>e===n)}function st(r,t){return typeof t=="number"?Rn(r,t):F(r,t,(e,n)=>e!==n)}function yr(r,t,e=1e-5,n=1e-8){return typeof t=="number"?Ln(r,t,e,n):F(r,t,(o,a)=>{let c=Math.abs(o-a),u=n+e*Math.abs(a);return c<=u})}function it(r,t,e=1e-5,n=1e-8){let o=yr(r,t,e,n),a=o.data;for(let c=0;c<o.size;c++)if(a[c]===0)return!1;return!0}function kn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]>t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function vn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]>=t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Vn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]<t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Pn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]<=t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Fn(r,t){let e=new Uint8Array(r.size),n=r.data,o=r.dtype;if(w(o)){let a=BigInt(Math.round(t)),c=n;for(let u=0;u<r.size;u++)e[u]=c[u]===a?1:0}else for(let a=0;a<r.size;a++)e[a]=n[a]===t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Rn(r,t){let e=new Uint8Array(r.size),n=r.data;for(let o=0;o<r.size;o++)e[o]=n[o]!==t?1:0;return D.fromData(e,Array.from(r.shape),"bool")}function Ln(r,t,e,n){let o=new Uint8Array(r.size),a=r.data,c=r.dtype;if(w(c)){let u=a;for(let s=0;s<r.size;s++){let i=Number(u[s]),l=Math.abs(i-t),f=n+e*Math.abs(t);o[s]=l<=f?1:0}}else for(let u=0;u<r.size;u++){let s=Number(a[u]),i=Math.abs(s-t),l=n+e*Math.abs(t);o[u]=i<=l?1:0}return D.fromData(o,Array.from(r.shape),"bool")}function _(r,t){let e=0,n=1;for(let o=r.length-1;o>=0;o--)e+=r[o]*n,n*=t[o];return e}function E(r,t,e,n){let o=n.length,a=new Array(o),c=Array.from(n).filter((s,i)=>i!==t),u=r;for(let s=c.length-1;s>=0;s--)a[s>=t?s+1:s]=u%c[s],u=Math.floor(u/c[s]);return a[t]=e,a}function H(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0)if(w(n)){let y=u,d=BigInt(0);for(let A=0;A<c;A++)d+=y[A];return Number(d)}else{let y=0;for(let d=0;d<c;d++)y+=Number(u[d]);return y}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return H(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=BigInt(0);for(let g=0;g<p;g++){let b=E(A,s,g,o),S=_(b,o);h+=y[S]}d[A]=h}}else for(let y=0;y<m;y++){let d=0;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o);d+=Number(u[g])}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function mr(r,t,e=!1){let n=r.dtype,o=r.shape;if(t===void 0)return H(r)/r.size;let a=t;if(a<0&&(a=o.length+a),a<0||a>=o.length)throw new Error(`axis ${t} is out of bounds for array of dimension ${o.length}`);let c=H(r,t,e);if(typeof c=="number")return c/o[a];let u=o[a],s=n;(w(n)||n.startsWith("int")||n.startsWith("uint"))&&(s="float64");let i=D.zeros(Array.from(c.shape),s),l=i.data,f=c.data;if(w(n)){let p=f;for(let m=0;m<l.length;m++)l[m]=Number(p[m])/u}else for(let p=0;p<l.length;p++)l[p]=Number(f[p])/u;return i}function dr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0){if(c===0)throw new Error("max of empty array");let y=u[0];for(let d=1;d<c;d++)u[d]>y&&(y=u[d]);return Number(y)}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return dr(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=E(A,s,0,o),g=_(h,o),b=y[g];for(let S=1;S<p;S++){let x=E(A,s,S,o),N=_(x,o),T=y[N];T>b&&(b=T)}d[A]=b}}else for(let y=0;y<m;y++){let d=-1/0;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o),b=Number(u[g]);b>d&&(d=b)}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function gr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0)if(w(n)){let y=u,d=BigInt(1);for(let A=0;A<c;A++)d*=y[A];return Number(d)}else{let y=1;for(let d=0;d<c;d++)y*=Number(u[d]);return y}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return gr(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=BigInt(1);for(let g=0;g<p;g++){let b=E(A,s,g,o),S=_(b,o);h*=y[S]}d[A]=h}}else for(let y=0;y<m;y++){let d=1;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o);d*=Number(u[g])}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function hr(r,t,e=!1){let n=r.dtype,o=r.shape,a=o.length,c=r.size,u=r.data;if(t===void 0){if(c===0)throw new Error("min of empty array");let y=u[0];for(let d=1;d<c;d++)u[d]<y&&(y=u[d]);return Number(y)}let s=t;if(s<0&&(s=a+s),s<0||s>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let i=Array.from(o).filter((y,d)=>d!==s);if(i.length===0)return hr(r);let l=D.zeros(i,n),f=l.data,p=o[s],m=i.reduce((y,d)=>y*d,1);if(w(n)){let y=u,d=f;for(let A=0;A<m;A++){let h=E(A,s,0,o),g=_(h,o),b=y[g];for(let S=1;S<p;S++){let x=E(A,s,S,o),N=_(x,o),T=y[N];T<b&&(b=T)}d[A]=b}}else for(let y=0;y<m;y++){let d=1/0;for(let A=0;A<p;A++){let h=E(y,s,A,o),g=_(h,o),b=Number(u[g]);b<d&&(d=b)}f[y]=d}if(e){let y=[...o];return y[s]=1,D.fromData(f,y,n)}return l}function Ar(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){if(a===0)throw new Error("argmin of empty array");let m=c[0],y=0;for(let d=1;d<a;d++)c[d]<m&&(m=c[d],y=d);return y}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return Ar(r);let i=D.zeros(s,"int32"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);if(w(e)){let m=c;for(let y=0;y<p;y++){let d=E(y,u,0,n),A=_(d,n),h=m[A],g=0;for(let b=1;b<f;b++){let S=E(y,u,b,n),x=_(S,n),N=m[x];N<h&&(h=N,g=b)}l[y]=g}}else for(let m=0;m<p;m++){let y=1/0,d=0;for(let A=0;A<f;A++){let h=E(m,u,A,n),g=_(h,n),b=Number(c[g]);b<y&&(y=b,d=A)}l[m]=d}return i}function br(r,t){let e=r.dtype,n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){if(a===0)throw new Error("argmax of empty array");let m=c[0],y=0;for(let d=1;d<a;d++)c[d]>m&&(m=c[d],y=d);return y}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return br(r);let i=D.zeros(s,"int32"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);if(w(e)){let m=c;for(let y=0;y<p;y++){let d=E(y,u,0,n),A=_(d,n),h=m[A],g=0;for(let b=1;b<f;b++){let S=E(y,u,b,n),x=_(S,n),N=m[x];N>h&&(h=N,g=b)}l[y]=g}}else for(let m=0;m<p;m++){let y=-1/0,d=0;for(let A=0;A<f;A++){let h=E(m,u,A,n),g=_(h,n),b=Number(c[g]);b>y&&(y=b,d=A)}l[m]=d}return i}function Sr(r,t,e=0,n=!1){let o=r.shape,a=o.length,c=r.size,u=r.data,s=mr(r,t,n);if(t===void 0){let h=s,g=0;for(let b=0;b<c;b++){let S=Number(u[b])-h;g+=S*S}return g/(c-e)}let i=t;if(i<0&&(i=a+i),i<0||i>=a)throw new Error(`axis ${t} is out of bounds for array of dimension ${a}`);let l=o[i],f=s,p=f.data,m=n?f.shape:Array.from(o).filter((h,g)=>g!==i),y=D.zeros(Array.from(m),"float64"),d=y.data,A=m.reduce((h,g)=>h*g,1);for(let h=0;h<A;h++){let g=0,b=Number(p[h]);for(let S=0;S<l;S++){let x=E(h,i,S,o),N=_(x,o),T=Number(u[N])-b;g+=T*T}d[h]=g/(l-e)}return y}function ut(r,t,e=0,n=!1){let o=Sr(r,t,e,n);if(typeof o=="number")return Math.sqrt(o);let a=D.zeros(Array.from(o.shape),"float64"),c=o.data,u=a.data;for(let s=0;s<c.length;s++)u[s]=Math.sqrt(Number(c[s]));return a}function Dr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){for(let m=0;m<a;m++)if(!c[m])return!1;return!0}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return Dr(r);let i=D.zeros(s,"bool"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);for(let m=0;m<p;m++){let y=!0;for(let d=0;d<f;d++){let A=E(m,u,d,n),h=_(A,n);if(!c[h]){y=!1;break}}l[m]=y?1:0}if(e){let m=[...n];return m[u]=1,D.fromData(l,m,"bool")}return i}function wr(r,t,e=!1){let n=r.shape,o=n.length,a=r.size,c=r.data;if(t===void 0){for(let m=0;m<a;m++)if(c[m])return!0;return!1}let u=t;if(u<0&&(u=o+u),u<0||u>=o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o}`);let s=Array.from(n).filter((m,y)=>y!==u);if(s.length===0)return wr(r);let i=D.zeros(s,"bool"),l=i.data,f=n[u],p=s.reduce((m,y)=>m*y,1);for(let m=0;m<p;m++){let y=!1;for(let d=0;d<f;d++){let A=E(m,u,d,n),h=_(A,n);if(c[h]){y=!0;break}}l[m]=y?1:0}if(e){let m=[...n];return m[u]=1,D.fromData(l,m,"bool")}return i}function j(r,t){let e=r.size,n=r.dtype,o=t.indexOf(-1),a;if(o!==-1){let i=t.reduce((f,p,m)=>m===o?f:f*p,1),l=e/i;if(!Number.isInteger(l))throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(t)}`);a=t.map((f,p)=>p===o?l:f)}else a=t;if(a.reduce((i,l)=>i*l,1)!==e)throw new Error(`cannot reshape array of size ${e} into shape ${JSON.stringify(a)}`);if(r.isCContiguous){let i=r.data;return D.fromData(i,a,n,P(a),0)}let s=r.copy().data;return D.fromData(s,a,n,P(a),0)}function Nr(r){let t=r.size,e=r.dtype,n=B(e);if(!n)throw new Error(`Cannot flatten array with dtype ${e}`);if(r.isCContiguous){let u=r.data.slice(r.offset,r.offset+t);return D.fromData(u,[t],e,[1],0)}let o=new n(t),a=w(e);for(let c=0;c<t;c++){let u=r.iget(c);o[c]=u}return D.fromData(o,[t],e,[1],0)}function W(r){let t=r.size,e=r.dtype;if(r.isCContiguous){let n=r.data;return D.fromData(n,[t],e,[1],0)}return Nr(r)}function nr(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,c=r.dtype,u;if(t===void 0)u=Array.from({length:n},(f,p)=>n-1-p);else{if(t.length!==n)throw new Error(`axes must have length ${n}, got ${t.length}`);let f=new Set;for(let p of t){let m=p<0?n+p:p;if(m<0||m>=n)throw new Error(`axis ${p} is out of bounds for array of dimension ${n}`);if(f.has(m))throw new Error("repeated axis in transpose");f.add(m)}u=t.map(p=>p<0?n+p:p)}let s=u.map(f=>e[f]),i=Array.from(o),l=u.map(f=>i[f]);return D.fromData(a,s,c,l,r.offset)}function ct(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,c=r.dtype;if(t===void 0){let u=[],s=[];for(let i=0;i<n;i++)e[i]!==1&&(u.push(e[i]),s.push(o[i]));return u.length===0&&(u.push(1),s.push(1)),D.fromData(a,u,c,s,r.offset)}else{let u=t<0?n+t:t;if(u<0||u>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);if(e[u]!==1)throw new Error(`cannot select an axis which has size not equal to one (axis ${t} has size ${e[u]})`);let s=[],i=[];for(let l=0;l<n;l++)l!==u&&(s.push(e[l]),i.push(o[l]));return D.fromData(a,s,c,i,r.offset)}}function Y(r,t){let e=r.shape,n=e.length,o=r.strides,a=r.data,c=r.dtype,u=t;if(u<0&&(u=n+t+1),u<0||u>n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n+1}`);let s=[...Array.from(e)];s.splice(u,0,1);let i=[...Array.from(o)],l=u<n?o[u]*(e[u]||1):1;return i.splice(u,0,l),D.fromData(a,s,c,i,r.offset)}function lt(r,t,e){let n=r.shape,o=n.length,a=r.strides,c=r.data,u=r.dtype,s=t<0?o+t:t,i=e<0?o+e:e;if(s<0||s>=o)throw new Error(`axis1 ${t} is out of bounds for array of dimension ${o}`);if(i<0||i>=o)throw new Error(`axis2 ${e} is out of bounds for array of dimension ${o}`);if(s===i)return D.fromData(c,Array.from(n),u,Array.from(a),r.offset);let l=Array.from(n),f=Array.from(a);return[l[s],l[i]]=[l[i],l[s]],[f[s],f[i]]=[f[i],f[s]],D.fromData(c,l,u,f,r.offset)}function ft(r,t,e){let n=r.ndim,o=Array.isArray(t)?t:[t],a=Array.isArray(e)?e:[e];if(o.length!==a.length)throw new Error("source and destination must have the same number of elements");let c=o.map(i=>{let l=i<0?n+i:i;if(l<0||l>=n)throw new Error(`source axis ${i} is out of bounds for array of dimension ${n}`);return l}),u=a.map(i=>{let l=i<0?n+i:i;if(l<0||l>=n)throw new Error(`destination axis ${i} is out of bounds for array of dimension ${n}`);return l});if(new Set(c).size!==c.length)throw new Error("repeated axis in source");if(new Set(u).size!==u.length)throw new Error("repeated axis in destination");let s=[];for(let i=0;i<n;i++)c.includes(i)||s.push(i);for(let i=0;i<c.length;i++){let l=u[i];s.splice(l,0,c[i])}return nr(r,s)}function R(r,t=0){if(r.length===0)throw new Error("need at least one array to concatenate");if(r.length===1)return r[0].copy();let e=r[0],n=e.ndim,o=e.dtype,a=t<0?n+t:t;if(a<0||a>=n)throw new Error(`axis ${t} is out of bounds for array of dimension ${n}`);for(let m=1;m<r.length;m++){let y=r[m];if(y.ndim!==n)throw new Error("all the input arrays must have same number of dimensions");for(let d=0;d<n;d++)if(d!==a&&y.shape[d]!==e.shape[d])throw new Error("all the input array dimensions except for the concatenation axis must match exactly")}let c=Array.from(e.shape),u=e.shape[a];for(let m=1;m<r.length;m++)u+=r[m].shape[a];c[a]=u;let s=c.reduce((m,y)=>m*y,1),i=B(o);if(!i)throw new Error(`Cannot concatenate arrays with dtype ${o}`);let l=new i(s),f=P(c),p=0;for(let m of r){let y=m.shape[a];Zn(m,l,c,f,a,p,o),p+=y}return D.fromData(l,c,o)}function Zn(r,t,e,n,o,a,c){let u=r.shape,s=u.length,i=r.size,l=c==="int64"||c==="uint64";if(o===0&&r.isCContiguous&&s>0){let p=a*n[0],m=r.data,y=r.offset,d=y+i;t.set(m.subarray(y,d),p);return}let f=new Array(s).fill(0);for(let p=0;p<i;p++){let m=r.iget(p),y=[...f];y[o]+=a;let d=0;for(let A=0;A<s;A++)d+=y[A]*n[A];t[d]=m;for(let A=s-1;A>=0&&(f[A]++,!(f[A]<u[A]));A--)f[A]=0}}function pt(r,t=0){if(r.length===0)throw new Error("need at least one array to stack");let e=r[0],n=e.shape,o=e.ndim,a=t<0?o+1+t:t;if(a<0||a>o)throw new Error(`axis ${t} is out of bounds for array of dimension ${o+1}`);for(let u=1;u<r.length;u++){let s=r[u];if(s.ndim!==o)throw new Error("all input arrays must have the same shape");for(let i=0;i<o;i++)if(s.shape[i]!==n[i])throw new Error("all input arrays must have the same shape")}let c=r.map(u=>Y(u,a));return R(c,a)}function yt(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?j(e,[1,e.shape[0]]):e);return R(t,0)}function mt(r){if(r.length===0)throw new Error("need at least one array to stack");return r.every(e=>e.ndim===1)?R(r,0):R(r,1)}function dt(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(e=>e.ndim===1?j(Y(j(e,[1,e.shape[0]]),2),[1,e.shape[0],1]):e.ndim===2?Y(e,2):e);return R(t,2)}function gt(r,t,e=0){let n=r.shape,o=n.length,a=e<0?o+e:e;if(a<0||a>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let c=n[a],u;if(typeof t=="number"){if(c%t!==0)throw new Error("array split does not result in an equal division");let s=c/t;u=[];for(let i=1;i<t;i++)u.push(i*s)}else u=t;return ht(r,u,a)}function or(r,t,e=0){let n=r.shape,o=n.length,a=e<0?o+e:e;if(a<0||a>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let c=n[a],u;if(typeof t=="number"){let s=t,i=Math.floor(c/s),l=c%s;u=[];let f=0;for(let p=0;p<s-1;p++)f+=i+(p<l?1:0),u.push(f)}else u=t;return ht(r,u,a)}function ht(r,t,e){let n=r.shape,o=n[e],a=[0,...t,o],c=[];for(let u=0;u<a.length-1;u++){let s=a[u],i=a[u+1];if(s>i)throw new Error("split indices must be in ascending order");let l=Array.from(n);l[e]=i-s;let f=r.offset+s*r.strides[e];c.push(D.fromData(r.data,l,r.dtype,Array.from(r.strides),f))}return c}function At(r,t){if(r.ndim<2)throw new Error("vsplit only works on arrays of 2 or more dimensions");return or(r,t,0)}function bt(r,t){if(r.ndim<1)throw new Error("hsplit only works on arrays of 1 or more dimensions");let e=r.ndim===1?0:1;return or(r,t,e)}function St(r,t){let e=r.shape,n=e.length,o=r.dtype,a=Array.isArray(t)?t:[t],c=Math.max(n,a.length),u=new Array(c).fill(1),s=new Array(c).fill(1);for(let g=0;g<n;g++)u[c-n+g]=e[g];for(let g=0;g<a.length;g++)s[c-a.length+g]=a[g];let i=u.map((g,b)=>g*s[b]),l=i.reduce((g,b)=>g*b,1),f=B(o);if(!f)throw new Error(`Cannot tile array with dtype ${o}`);let p=new f(l),m=P(i),y=r;n<c&&(y=j(r,u));let d=o==="int64"||o==="uint64",A=y.strides,h=new Array(c).fill(0);for(let g=0;g<l;g++){let b=y.offset;for(let N=0;N<c;N++){let T=h[N]%u[N];b+=T*A[N]}let S=y.data[b],x=0;for(let N=0;N<c;N++)x+=h[N]*m[N];p[x]=S;for(let N=c-1;N>=0&&(h[N]++,!(h[N]<i[N]));N--)h[N]=0}return D.fromData(p,i,o)}function Dt(r,t,e){let n=r.shape,o=n.length,a=r.dtype,c=r.size;if(e===void 0){let g=c,b=Array.isArray(t)?t:new Array(g).fill(t);if(b.length!==g)throw new Error(`operands could not be broadcast together with shape (${g},) (${b.length},)`);let S=b.reduce((I,U)=>I+U,0),x=B(a);if(!x)throw new Error(`Cannot repeat array with dtype ${a}`);let N=new x(S),T=0;for(let I=0;I<g;I++){let U=r.iget(I),tr=b[I];for(let Vr=0;Vr<tr;Vr++)N[T++]=U}return D.fromData(N,[S],a)}let u=e<0?o+e:e;if(u<0||u>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let s=n[u],i=Array.isArray(t)?t:new Array(s).fill(t);if(i.length!==s)throw new Error(`operands could not be broadcast together with shape (${s},) (${i.length},)`);let l=Array.from(n);l[u]=i.reduce((g,b)=>g+b,0);let f=l.reduce((g,b)=>g*b,1),p=B(a);if(!p)throw new Error(`Cannot repeat array with dtype ${a}`);let m=new p(f),y=P(l),d=new Array(o).fill(0),A=a==="int64"||a==="uint64",h=[0];for(let g=0;g<s;g++)h.push(h[g]+i[g]);for(let g=0;g<c;g++){let b=r.iget(g),S=d[u],x=i[S],N=0;for(let U=0;U<o;U++)U!==u&&(N+=d[U]*y[U]);let T=y[u],I=h[S];for(let U=0;U<x;U++){let tr=N+(I+U)*T;m[tr]=b}for(let U=o-1;U>=0&&(d[U]++,!(d[U]<n[U]));U--)d[U]=0}return D.fromData(m,l,a)}function Hn(r,t,e,n,o,a,c,u,s,i,l,f,p,m){if(f===0)for(let h=0;h<n*o;h++)p[h]=0;else if(f!==1)for(let h=0;h<n*o;h++)p[h]=(p[h]??0)*f;let y=r==="row-major",d=t==="transpose",A=e==="transpose";if(y&&!d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[S*l+g]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(y&&d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[S*l+g]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(y&&!d&&A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[g*l+S]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(y&&d&&A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[g*l+S]??0);p[h*m+g]=(p[h*m+g]??0)+c*b}else if(!y&&!d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[g*l+S]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}else if(!y&&d&&!A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[g*l+S]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}else if(!y&&!d&&A)for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[S*s+h]??0)*(i[S*l+g]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}else for(let h=0;h<n;h++)for(let g=0;g<o;g++){let b=0;for(let S=0;S<a;S++)b+=(u[h*s+S]??0)*(i[S*l+g]??0);p[g*m+h]=(p[g*m+h]??0)+c*b}}function xr(r,t){let e=r.ndim,n=t.ndim;if(e===0||n===0){let o=e===0?r.get():null,a=n===0?t.get():null;if(e===0&&n===0)return typeof o=="bigint"&&typeof a=="bigint"?o*a:Number(o)*Number(a);if(e===0){let c=O(r.dtype,t.dtype),u=D.zeros([...t.shape],c);for(let s=0;s<t.size;s++){let i=t.data[s+t.offset];typeof o=="bigint"&&typeof i=="bigint"?u.data[s]=o*i:u.data[s]=Number(o)*Number(i)}return u}else{let c=O(r.dtype,t.dtype),u=D.zeros([...r.shape],c);for(let s=0;s<r.size;s++){let i=r.data[s+r.offset];typeof i=="bigint"&&typeof a=="bigint"?u.data[s]=i*a:u.data[s]=Number(i)*Number(a)}return u}}if(e===1&&n===1){if(r.shape[0]!==t.shape[0])throw new Error(`dot: incompatible shapes (${r.shape[0]},) and (${t.shape[0]},)`);let o=r.shape[0],a=0;for(let c=0;c<o;c++){let u=r.get(c),s=t.get(c);typeof u=="bigint"&&typeof s=="bigint"?a=Number(a)+Number(u*s):a+=Number(u)*Number(s)}return a}if(e===2&&n===2)return Ir(r,t);if(e===2&&n===1){let[o,a]=r.shape,c=t.shape[0];if(a!==c)throw new Error(`dot: incompatible shapes (${o},${a}) and (${c},)`);let u=O(r.dtype,t.dtype),s=D.zeros([o],u);for(let i=0;i<o;i++){let l=0;for(let f=0;f<a;f++){let p=r.get(i,f),m=t.get(f);typeof p=="bigint"&&typeof m=="bigint"?l=Number(l)+Number(p*m):l+=Number(p)*Number(m)}s.set([i],l)}return s}if(e===1&&n===2){let o=r.shape[0],[a,c]=t.shape;if(o!==a)throw new Error(`dot: incompatible shapes (${o},) and (${a},${c})`);let u=O(r.dtype,t.dtype),s=D.zeros([c],u);for(let i=0;i<c;i++){let l=0;for(let f=0;f<o;f++){let p=r.get(f),m=t.get(f,i);typeof p=="bigint"&&typeof m=="bigint"?l=Number(l)+Number(p*m):l+=Number(p)*Number(m)}s.set([i],l)}return s}if(e>2&&n===1){let o=r.shape[e-1],a=t.shape[0];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(r.shape)} and (${a},)`);let c=[...r.shape.slice(0,-1)],u=O(r.dtype,t.dtype),s=D.zeros(c,u),i=c.reduce((l,f)=>l*f,1);for(let l=0;l<i;l++){let f=0,p=l,m=[];for(let y=c.length-1;y>=0;y--)m[y]=p%c[y],p=Math.floor(p/c[y]);for(let y=0;y<o;y++){let d=[...m,y],A=r.get(...d),h=t.get(y);typeof A=="bigint"&&typeof h=="bigint"?f=Number(f)+Number(A*h):f+=Number(A)*Number(h)}s.set(m,f)}return s}if(e===1&&n>2){let o=r.shape[0],a=1,c=t.shape[a];if(o!==c)throw new Error(`dot: incompatible shapes (${o},) and ${JSON.stringify(t.shape)}`);let u=[...t.shape.slice(0,a),...t.shape.slice(a+1)],s=O(r.dtype,t.dtype),i=D.zeros(u,s),l=u.reduce((f,p)=>f*p,1);for(let f=0;f<l;f++){let p=f,m=[];for(let h=u.length-1;h>=0;h--)m[h]=p%u[h],p=Math.floor(p/u[h]);let y=m.slice(0,a),d=m.slice(a),A=0;for(let h=0;h<o;h++){let g=r.get(h),b=[...y,h,...d],S=t.get(...b);typeof g=="bigint"&&typeof S=="bigint"?A=Number(A)+Number(g*S):A+=Number(g)*Number(S)}i.set(m,A)}return i}if(e>=2&&n>=2&&!(e===2&&n===2)){let o=r.shape[e-1],a=t.shape[n-2];if(o!==a)throw new Error(`dot: incompatible shapes ${JSON.stringify(r.shape)} and ${JSON.stringify(t.shape)}`);let c=[...r.shape.slice(0,-1),...t.shape.slice(0,-2),t.shape[n-1]],u=O(r.dtype,t.dtype),s=D.zeros(c,u),i=r.shape.slice(0,-1).reduce((m,y)=>m*y,1),l=t.shape.slice(0,-2).reduce((m,y)=>m*y,1),f=t.shape[n-1],p=o;for(let m=0;m<i;m++)for(let y=0;y<l;y++)for(let d=0;d<f;d++){let A=0;for(let g=0;g<p;g++){let b=m*p+g,S=r.data[b+r.offset],x=y*p*f+g*f+d,N=t.data[x+t.offset];typeof S=="bigint"&&typeof N=="bigint"?A=Number(A)+Number(S*N):A+=Number(S)*Number(N)}let h=m*l*f+y*f+d;s.data[h]=A}return s}throw new Error(`dot: unexpected combination of dimensions ${e}D \xB7 ${n}D`)}function Ir(r,t){if(r.ndim!==2||t.ndim!==2)throw new Error("matmul requires 2D arrays");let[e=0,n=0]=r.shape,[o=0,a=0]=t.shape;if(n!==o)throw new Error(`matmul shape mismatch: (${e},${n}) @ (${o},${a})`);let c=O(r.dtype,t.dtype),u=c.startsWith("int")||c.startsWith("uint")||c==="bool"?"float64":c;if(u!=="float64")throw new Error(`matmul currently only supports float64, got ${u}`);let s=r.dtype==="float64"?r.data:Float64Array.from(Array.from(r.data).map(Number)),i=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number));r.offset>0&&(s=s.subarray(r.offset)),t.offset>0&&(i=i.subarray(t.offset));let[l=0,f=0]=r.strides,[p=0,m=0]=t.strides,y=f>l,d=m>p,A=y?"transpose":"no-transpose",h=d?"transpose":"no-transpose",g,b;y?g=f:g=l,d?b=m:b=p;let S=D.zeros([e,a],"float64");return Hn("row-major",A,h,e,a,n,1,s,g,i,b,0,S.data,a),S}function Nt(r){if(r.ndim!==2)throw new Error(`trace requires 2D array, got ${r.ndim}D`);let[t=0,e=0]=r.shape,n=Math.min(t,e),o=0;for(let a=0;a<n;a++){let c=r.get(a,a);typeof c=="bigint"?o=(typeof o=="bigint"?o:BigInt(o))+c:o=(typeof o=="bigint"?Number(o):o)+c}return o}function xt(r,t){let e=r.ndim,n=t.ndim,o=r.shape[e-1],a=t.shape[n-1];if(o!==a)throw new Error(`inner: incompatible shapes - last dimensions ${o} and ${a} don't match`);if(e===1&&n===1)return xr(r,t);let c=[...r.shape.slice(0,-1),...t.shape.slice(0,-1)],u=O(r.dtype,t.dtype),s=D.zeros(c,u),i=e===1?1:r.shape.slice(0,-1).reduce((p,m)=>p*m,1),l=n===1?1:t.shape.slice(0,-1).reduce((p,m)=>p*m,1),f=o;for(let p=0;p<i;p++)for(let m=0;m<l;m++){let y=0;for(let A=0;A<f;A++){let h=e===1?A:p*f+A,g=n===1?A:m*f+A,b=r.data[h+r.offset],S=t.data[g+t.offset];typeof b=="bigint"&&typeof S=="bigint"?y=Number(y)+Number(b*S):y+=Number(b)*Number(S)}if(c.length===0)return y;let d=i===1?m:p*l+m;s.data[d]=y}return s}function It(r,t){let e=r.ndim===1?r:W(r),n=t.ndim===1?t:W(t),o=e.size,a=n.size,c=O(r.dtype,t.dtype),u=D.zeros([o,a],c);for(let s=0;s<o;s++)for(let i=0;i<a;i++){let l=e.get(s),f=n.get(i),p;typeof l=="bigint"&&typeof f=="bigint"?p=l*f:p=Number(l)*Number(f),u.set([s,i],p)}return u}function zt(r,t,e){let n,o;if(typeof e=="number"){let p=e;if(p<0)throw new Error("tensordot: axes must be non-negative");if(p>r.ndim||p>t.ndim)throw new Error("tensordot: axes exceeds array dimensions");n=Array.from({length:p},(m,y)=>r.ndim-p+y),o=Array.from({length:p},(m,y)=>y)}else if([n,o]=e,n.length!==o.length)throw new Error("tensordot: axes lists must have same length");for(let p=0;p<n.length;p++){let m=n[p],y=o[p];if(m<0||m>=r.ndim||y<0||y>=t.ndim)throw new Error("tensordot: axis out of bounds");if(r.shape[m]!==t.shape[y])throw new Error(`tensordot: shape mismatch on axes ${m} and ${y}: ${r.shape[m]} != ${t.shape[y]}`)}let a=[],c=[];for(let p=0;p<r.ndim;p++)n.includes(p)||a.push(p);for(let p=0;p<t.ndim;p++)o.includes(p)||c.push(p);let u=[...a.map(p=>r.shape[p]),...c.map(p=>t.shape[p])];if(u.length===0){let p=0,m=n.map(y=>r.shape[y]).reduce((y,d)=>y*d,1);for(let y=0;y<m;y++){let d=y,A=new Array(n.length);for(let x=n.length-1;x>=0;x--){let N=n[x];A[x]=d%r.shape[N],d=Math.floor(d/r.shape[N])}let h=new Array(r.ndim),g=new Array(t.ndim);for(let x=0;x<n.length;x++)h[n[x]]=A[x];for(let x=0;x<o.length;x++)g[o[x]]=A[x];let b=r.get(...h),S=t.get(...g);typeof b=="bigint"&&typeof S=="bigint"?p=Number(p)+Number(b*S):p+=Number(b)*Number(S)}return p}let s=O(r.dtype,t.dtype),i=D.zeros(u,s),l=u.reduce((p,m)=>p*m,1),f=n.map(p=>r.shape[p]).reduce((p,m)=>p*m,1);for(let p=0;p<l;p++){let m=p,y=[];for(let g=u.length-1;g>=0;g--)y[g]=m%u[g],m=Math.floor(m/u[g]);let d=y.slice(0,a.length),A=y.slice(a.length),h=0;for(let g=0;g<f;g++){m=g;let b=[];for(let I=n.length-1;I>=0;I--){let U=n[I];b[I]=m%r.shape[U],m=Math.floor(m/r.shape[U])}let S=new Array(r.ndim),x=new Array(t.ndim);for(let I=0;I<a.length;I++)S[a[I]]=d[I];for(let I=0;I<c.length;I++)x[c[I]]=A[I];for(let I=0;I<n.length;I++)S[n[I]]=b[I],x[o[I]]=b[I];let N=r.get(...S),T=t.get(...x);typeof N=="bigint"&&typeof T=="bigint"?h=Number(h)+Number(N*T):h+=Number(N)*Number(T)}i.set(y,h)}return i}function Bt(r){return $(r,Math.sqrt,!1)}function Ut(r,t){return typeof t=="number"?Wn(r,t):k(r,t,Math.pow,"power")}function Wn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,s=e!=="float32"&&e!=="float64"&&(t<0||!Number.isInteger(t))?"float64":e,i=D.zeros(n,s),l=i.data;if(w(e))if(w(s)&&Number.isInteger(t)&&t>=0){let f=o,p=l;for(let m=0;m<a;m++)p[m]=f[m]**BigInt(t)}else for(let f=0;f<a;f++)l[f]=Math.pow(Number(o[f]),t);else for(let f=0;f<a;f++)l[f]=Math.pow(Number(o[f]),t);return i}function _t(r){return $(r,Math.sin,!1)}function Et(r){return $(r,Math.cos,!1)}function Tt(r){return $(r,Math.tan,!1)}function Ot(r){return $(r,Math.asin,!1)}function $t(r){return $(r,Math.acos,!1)}function Mt(r){return $(r,Math.atan,!1)}function Ct(r,t){return typeof t=="number"?Xn(r,t):Jn(r,t)}function Jn(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,c=o==="float32"&&a==="float32"?"float32":"float64",u=D.zeros(e,c),s=u.data;for(let i=0;i<n;i++){let l=(w(o),Number(r.data[i])),f=(w(a),Number(t.data[i]));s[i]=Math.atan2(l,f)}return u}function Xn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=e==="float32"?"float32":"float64",u=D.zeros(n,c),s=u.data;if(w(e))for(let i=0;i<a;i++)s[i]=Math.atan2(Number(o[i]),t);else for(let i=0;i<a;i++)s[i]=Math.atan2(Number(o[i]),t);return u}function kt(r,t){return typeof t=="number"?Kn(r,t):Qn(r,t)}function Qn(r,t){let e=Array.from(r.shape),n=r.size,o=r.dtype,a=t.dtype,c=o==="float32"&&a==="float32"?"float32":"float64",u=D.zeros(e,c),s=u.data;for(let i=0;i<n;i++){let l=(w(o),Number(r.data[i])),f=(w(a),Number(t.data[i]));s[i]=Math.hypot(l,f)}return u}function Kn(r,t){let e=r.dtype,n=Array.from(r.shape),o=r.data,a=r.size,c=e==="float32"?"float32":"float64",u=D.zeros(n,c),s=u.data;if(w(e))for(let i=0;i<a;i++)s[i]=Math.hypot(Number(o[i]),t);else for(let i=0;i<a;i++)s[i]=Math.hypot(Number(o[i]),t);return u}function vt(r){let t=180/Math.PI;return $(r,e=>e*t,!1)}function Vt(r){let t=Math.PI/180;return $(r,e=>e*t,!1)}function Pt(r){return $(r,Math.sinh,!1)}function Ft(r){return $(r,Math.cosh,!1)}function Rt(r){return $(r,Math.tanh,!1)}function Lt(r){return $(r,Math.asinh,!1)}function jt(r){return $(r,Math.acosh,!1)}function qt(r){return $(r,Math.atanh,!1)}function ar(r){if(r.length===0)return[];if(r.length===1)return Array.from(r[0]);let t=Math.max(...r.map(n=>n.length)),e=new Array(t);for(let n=0;n<t;n++){let o=1;for(let a of r){let c=a.length-t+n,u=c<0?1:a[c];if(u!==1){if(o===1)o=u;else if(o!==u)return null}}e[n]=o}return e}function eo(r,t,e){let n=r.length,o=e.length,a=new Array(o).fill(0);for(let c=0;c<n;c++){let u=o-n+c,s=r[c],i=e[u];if(s===i)a[u]=t[c];else if(s===1)a[u]=0;else throw new Error("Invalid broadcast")}return a}function G(r,t){let e=eo(r.shape,r.strides,t);return D.fromData(r.data,Array.from(t),r.dtype,e,r.offset)}function Zt(r,t){let e=r.shape,n=e.length,o=t.length;if(o<n)throw new Error("input operand has more dimensions than allowed by the axis remapping");let a=ar([Array.from(e),t]);if(a===null)throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);for(let c=0;c<o;c++)if(a[c]!==t[c])throw new Error(`operands could not be broadcast together with shape (${e.join(",")}) (${t.join(",")})`);return G(r,t)}function Ht(r){if(r.length===0)return[];if(r.length===1)return[r[0]];let t=r.map(n=>Array.from(n.shape)),e=ar(t);if(e===null)throw new Error(`operands could not be broadcast together with shapes ${t.map(n=>`(${n.join(",")})`).join(" ")}`);return r.map(n=>G(n,e))}function Yt(r,t,e){let n=r.shape,o=n.length,a=r.dtype;if(e===void 0){let y=r.size;for(let g of t){let b=g<0?y+g:g;if(b<0||b>=y)throw new Error(`index ${g} is out of bounds for axis 0 with size ${y}`)}let d=t.length,A=B(a);if(!A)throw new Error(`Cannot take from array with dtype ${a}`);let h=new A(d);for(let g=0;g<d;g++){let b=t[g];b<0&&(b=y+b);let S=r.iget(b);w(a),h[g]=S}return D.fromData(h,[d],a)}let c=e<0?o+e:e;if(c<0||c>=o)throw new Error(`axis ${e} is out of bounds for array of dimension ${o}`);let u=n[c];for(let y of t){let d=y<0?u+y:y;if(d<0||d>=u)throw new Error(`index ${y} is out of bounds for axis ${c} with size ${u}`)}let s=Array.from(n);s[c]=t.length;let i=s.reduce((y,d)=>y*d,1),l=B(a);if(!l)throw new Error(`Cannot take from array with dtype ${a}`);let f=new l(i),p=P(s),m=new Array(o).fill(0);for(let y=0;y<i;y++){let d=[...m],A=m[c],h=t[A];h<0&&(h=u+h),d[c]=h;let g=r.get(...d),b=0;for(let S=0;S<o;S++)b+=m[S]*p[S];w(a),f[b]=g;for(let S=o-1;S>=0&&(m[S]++,!(m[S]<s[S]));S--)m[S]=0}return D.fromData(f,s,a)}function Wt(r,t,e){let n=r.size,o=r.dtype,a;if(typeof e=="number"||typeof e=="bigint")a=new Array(t.length).fill(e);else{a=[];for(let c=0;c<e.size;c++)a.push(e.iget(c));if(a.length===1)a=new Array(t.length).fill(a[0]);else if(a.length!==t.length){let c=[...a];a=[];for(let u=0;u<t.length;u++)a.push(c[u%c.length])}}for(let c=0;c<t.length;c++){let u=t[c];if(u<0&&(u=n+u),u<0||u>=n)throw new Error(`index ${t[c]} is out of bounds for axis 0 with size ${n}`);let s=a[c];w(o)?typeof s!="bigint"&&(s=BigInt(Math.round(Number(s)))):typeof s=="bigint"&&(s=Number(s)),r.iset(u,s)}}function Gt(r,t){if(t.length===0)throw new Error("choices cannot be empty");let e=r.shape,n=t.length,o=t[0].dtype,a=t.map(p=>Array.from(p.shape));a.unshift(Array.from(e));let c=ar(a);if(c===null)throw new Error("operands could not be broadcast together");let u=G(r,c),s=t.map(p=>G(p,c)),i=c.reduce((p,m)=>p*m,1),l=B(o);if(!l)throw new Error(`Cannot choose with dtype ${o}`);let f=new l(i);for(let p=0;p<i;p++){let m=Number(u.iget(p));if(m<0||m>=n)throw new Error(`index ${m} is out of bounds for axis 0 with size ${n}`);let y=s[m].iget(p);w(o),f[p]=y}return D.fromData(f,c,o)}function Jt(r,t,e=!1){if(r.ndim!==t.ndim)return!1;for(let o=0;o<r.ndim;o++)if(r.shape[o]!==t.shape[o])return!1;let n=r.size;for(let o=0;o<n;o++){let a=r.iget(o),c=t.iget(o);if(e){let u=typeof a=="number"&&Number.isNaN(a),s=typeof c=="number"&&Number.isNaN(c);if(u&&s)continue}if(a!==c)return!1}return!0}var z=class r{constructor(t,e){this._storage=t,this._base=e}get storage(){return this._storage}static _fromStorage(t,e){return new r(t,e)}get shape(){return this._storage.shape}get ndim(){return this._storage.ndim}get size(){return this._storage.size}get dtype(){return this._storage.dtype}get data(){return this._storage.data}get strides(){return this._storage.strides}get flags(){return{C_CONTIGUOUS:this._storage.isCContiguous,F_CONTIGUOUS:this._storage.isFContiguous,OWNDATA:this._base===void 0}}get base(){return this._base??null}get(t){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);let e=t.map((n,o)=>{let a=n;if(a<0&&(a=this.shape[o]+a),a<0||a>=this.shape[o])throw new Error(`Index ${n} is out of bounds for axis ${o} with size ${this.shape[o]}`);return a});return this._storage.get(...e)}set(t,e){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);let n=t.map((c,u)=>{let s=c;if(s<0&&(s=this.shape[u]+s),s<0||s>=this.shape[u])throw new Error(`Index ${c} is out of bounds for axis ${u} with size ${this.shape[u]}`);return s}),o=this.dtype,a;w(o)?a=typeof e=="bigint"?e:BigInt(Math.round(e)):o==="bool"?a=e?1:0:a=Number(e),this._storage.set(n,a)}copy(){return new r(this._storage.copy())}astype(t,e=!0){let n=this.dtype;if(n===t&&!e)return this;if(n===t&&e)return this.copy();let o=Array.from(this.shape),a=this.size,c=B(t);if(!c)throw new Error(`Cannot convert to dtype ${t}`);let u=new c(a),s=this.data;if(w(n)&&!w(t)){let l=s;if(t==="bool")for(let f=0;f<a;f++)u[f]=l[f]!==BigInt(0)?1:0;else for(let f=0;f<a;f++)u[f]=Number(l[f])}else if(!w(n)&&w(t)){let l=s;for(let f=0;f<a;f++)u[f]=BigInt(Math.round(Number(l[f])))}else if(t==="bool"){let l=s;for(let f=0;f<a;f++)u[f]=l[f]!==0?1:0}else if(n==="bool"&&!w(t)){let l=s;for(let f=0;f<a;f++)u[f]=l[f]}else if(!w(n)&&!w(t)){let l=s;for(let f=0;f<a;f++)u[f]=l[f]}else{let l=s;for(let f=0;f<a;f++)u[f]=l[f]}let i=D.fromData(u,o,t);return new r(i)}add(t){let e=typeof t=="number"?t:t._storage,n=qr(this._storage,e);return r._fromStorage(n)}subtract(t){let e=typeof t=="number"?t:t._storage,n=Zr(this._storage,e);return r._fromStorage(n)}multiply(t){let e=typeof t=="number"?t:t._storage,n=Hr(this._storage,e);return r._fromStorage(n)}divide(t){let e=typeof t=="number"?t:t._storage,n=Yr(this._storage,e);return r._fromStorage(n)}mod(t){let e=typeof t=="number"?t:t._storage,n=Xr(this._storage,e);return r._fromStorage(n)}floor_divide(t){let e=typeof t=="number"?t:t._storage,n=Qr(this._storage,e);return r._fromStorage(n)}positive(){let t=Kr(this._storage);return r._fromStorage(t)}reciprocal(){let t=rt(this._storage);return r._fromStorage(t)}sqrt(){let t=Bt(this._storage);return r._fromStorage(t)}power(t){let e=typeof t=="number"?t:t._storage,n=Ut(this._storage,e);return r._fromStorage(n)}absolute(){let t=Wr(this._storage);return r._fromStorage(t)}negative(){let t=Gr(this._storage);return r._fromStorage(t)}sign(){let t=Jr(this._storage);return r._fromStorage(t)}sin(){let t=_t(this._storage);return r._fromStorage(t)}cos(){let t=Et(this._storage);return r._fromStorage(t)}tan(){let t=Tt(this._storage);return r._fromStorage(t)}arcsin(){let t=Ot(this._storage);return r._fromStorage(t)}arccos(){let t=$t(this._storage);return r._fromStorage(t)}arctan(){let t=Mt(this._storage);return r._fromStorage(t)}arctan2(t){let e=typeof t=="number"?t:t._storage,n=Ct(this._storage,e);return r._fromStorage(n)}hypot(t){let e=typeof t=="number"?t:t._storage,n=kt(this._storage,e);return r._fromStorage(n)}degrees(){let t=vt(this._storage);return r._fromStorage(t)}radians(){let t=Vt(this._storage);return r._fromStorage(t)}sinh(){let t=Pt(this._storage);return r._fromStorage(t)}cosh(){let t=Ft(this._storage);return r._fromStorage(t)}tanh(){let t=Rt(this._storage);return r._fromStorage(t)}arcsinh(){let t=Lt(this._storage);return r._fromStorage(t)}arccosh(){let t=jt(this._storage);return r._fromStorage(t)}arctanh(){let t=qt(this._storage);return r._fromStorage(t)}greater(t){let e=typeof t=="number"?t:t._storage,n=tt(this._storage,e);return r._fromStorage(n)}greater_equal(t){let e=typeof t=="number"?t:t._storage,n=et(this._storage,e);return r._fromStorage(n)}less(t){let e=typeof t=="number"?t:t._storage,n=nt(this._storage,e);return r._fromStorage(n)}less_equal(t){let e=typeof t=="number"?t:t._storage,n=ot(this._storage,e);return r._fromStorage(n)}equal(t){let e=typeof t=="number"?t:t._storage,n=at(this._storage,e);return r._fromStorage(n)}not_equal(t){let e=typeof t=="number"?t:t._storage,n=st(this._storage,e);return r._fromStorage(n)}isclose(t,e=1e-5,n=1e-8){let o=typeof t=="number"?t:t._storage,a=yr(this._storage,o,e,n);return r._fromStorage(a)}allclose(t,e=1e-5,n=1e-8){let o=typeof t=="number"?t:t._storage;return it(this._storage,o,e,n)}sum(t,e=!1){let n=H(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}mean(t,e=!1){let n=mr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}max(t,e=!1){let n=dr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}min(t,e=!1){let n=hr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}prod(t,e=!1){let n=gr(this._storage,t,e);return typeof n=="number"?n:r._fromStorage(n)}argmin(t){let e=Ar(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}argmax(t){let e=br(this._storage,t);return typeof e=="number"?e:r._fromStorage(e)}var(t,e=0,n=!1){let o=Sr(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}std(t,e=0,n=!1){let o=ut(this._storage,t,e,n);return typeof o=="number"?o:r._fromStorage(o)}all(t,e=!1){let n=Dr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}any(t,e=!1){let n=wr(this._storage,t,e);return typeof n=="boolean"?n:r._fromStorage(n)}reshape(...t){let e=t.length===1&&Array.isArray(t[0])?t[0]:t,n=j(this._storage,e),a=n.data===this.data?this._base??this:void 0;return r._fromStorage(n,a)}flatten(){let t=Nr(this._storage);return r._fromStorage(t)}ravel(){let t=W(this._storage),n=t.data===this.data?this._base??this:void 0;return r._fromStorage(t,n)}transpose(t){let e=nr(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}squeeze(t){let e=ct(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}expand_dims(t){let e=Y(this._storage,t),n=this._base??this;return r._fromStorage(e,n)}swapaxes(t,e){let n=lt(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}moveaxis(t,e){let n=ft(this._storage,t,e),o=this._base??this;return r._fromStorage(n,o)}repeat(t,e){let n=Dt(this._storage,t,e);return r._fromStorage(n)}take(t,e){let n=Yt(this._storage,t,e);return r._fromStorage(n)}put(t,e){let n=e instanceof r?e._storage:e;Wt(this._storage,t,n)}matmul(t){let e=Ir(this._storage,t._storage);return r._fromStorage(e)}dot(t){let e=xr(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}trace(){return Nt(this._storage)}inner(t){let e=xt(this._storage,t._storage);return typeof e=="number"||typeof e=="bigint"?e:r._fromStorage(e)}outer(t){let e=It(this._storage,t._storage);return r._fromStorage(e)}tensordot(t,e=2){let n=zt(this._storage,t._storage,e);return typeof n=="number"||typeof n=="bigint"?n:r._fromStorage(n)}slice(...t){if(t.length===0)return this;if(t.length>this.ndim)throw new Error(`Too many indices for array: array is ${this.ndim}-dimensional, but ${t.length} were indexed`);let e=t.map((s,i)=>{let l=Pr(s);return Fr(l,this.shape[i])});for(;e.length<this.ndim;)e.push({start:0,stop:this.shape[e.length],step:1,isIndex:!1});let n=[],o=[],a=this._storage.offset;for(let s=0;s<e.length;s++){let i=e[s],l=this._storage.strides[s];if(a+=i.start*l,!i.isIndex){let f;i.step>0?f=Math.max(0,Math.ceil((i.stop-i.start)/i.step)):f=Math.max(0,Math.ceil((i.start-i.stop)/Math.abs(i.step))),n.push(f),o.push(l*i.step)}}let c=D.fromData(this._storage.data,n,this._storage.dtype,o,a),u=this._base??this;return new r(c,u)}row(t){if(this.ndim<2)throw new Error("row() requires at least 2 dimensions");return this.slice(String(t),":")}col(t){if(this.ndim<2)throw new Error("col() requires at least 2 dimensions");return this.slice(":",String(t))}rows(t,e){if(this.ndim<2)throw new Error("rows() requires at least 2 dimensions");return this.slice(`${t}:${e}`,":")}cols(t,e){if(this.ndim<2)throw new Error("cols() requires at least 2 dimensions");return this.slice(":",`${t}:${e}`)}toString(){return`NDArray(shape=${JSON.stringify(this.shape)}, dtype=${this.dtype})`}toArray(){if(this.ndim===0)return this._storage.iget(0);let t=this.shape,e=t.length,n=(o,a)=>{if(a===e)return this._storage.get(...o);let c=[];for(let u=0;u<t[a];u++)o[a]=u,c.push(n(o,a+1));return c};return n(new Array(e),0)}};function J(r,t=M){let e=D.zeros(r,t);return new z(e)}function zr(r,t=M){let e=D.ones(r,t);return new z(e)}function oo(r){let t=[],e=r;for(;Array.isArray(e);)t.push(e.length),e=e[0];return t}function Xt(r){return typeof r=="bigint"?!0:Array.isArray(r)?r.some(t=>Xt(t)):!1}function ao(r){let t=[];function e(n){Array.isArray(n)?n.forEach(o=>e(o)):t.push(n)}return e(r),t}function V(r,t){if(r instanceof z)return!t||r.dtype===t?r.copy():r.astype(t);let e=Xt(r),n=oo(r),o=n.reduce((l,f)=>l*f,1),a=t;a||(e?a="int64":a=M);let c=B(a);if(!c)throw new Error(`Cannot create array with dtype ${a}`);let u=new c(o),s=ao(r);if(w(a)){let l=u;for(let f=0;f<o;f++){let p=s[f];l[f]=typeof p=="bigint"?p:BigInt(Math.round(Number(p)))}}else if(a==="bool"){let l=u;for(let f=0;f<o;f++)l[f]=s[f]?1:0}else{let l=u;for(let f=0;f<o;f++){let p=s[f];l[f]=Number(p)}}let i=D.fromData(u,n,a);return new z(i)}function Qt(r,t,e=1,n=M){let o=r,a=t;if(t===void 0&&(o=0,a=r),a===void 0)throw new Error("stop is required");let c=Math.max(0,Math.ceil((a-o)/e)),u=B(n);if(!u)throw new Error(`Cannot create arange array with dtype ${n}`);let s=new u(c);if(w(n))for(let l=0;l<c;l++)s[l]=BigInt(Math.round(o+l*e));else if(n==="bool")for(let l=0;l<c;l++)s[l]=o+l*e!==0?1:0;else for(let l=0;l<c;l++)s[l]=o+l*e;let i=D.fromData(s,[c],n);return new z(i)}function Kt(r,t,e=50,n=M){if(e<0)throw new Error("num must be non-negative");if(e===0)return V([],n);if(e===1)return V([r],n);let o=B(n);if(!o)throw new Error(`Cannot create linspace array with dtype ${n}`);let a=new o(e),c=(t-r)/(e-1);if(w(n))for(let s=0;s<e;s++)a[s]=BigInt(Math.round(r+s*c));else if(n==="bool")for(let s=0;s<e;s++)a[s]=r+s*c!==0?1:0;else for(let s=0;s<e;s++)a[s]=r+s*c;let u=D.fromData(a,[e],n);return new z(u)}function re(r,t,e=50,n=10,o=M){if(e<0)throw new Error("num must be non-negative");if(e===0)return V([],o);if(e===1)return V([Math.pow(n,r)],o);let a=B(o);if(!a)throw new Error(`Cannot create logspace array with dtype ${o}`);let c=new a(e),u=(t-r)/(e-1);if(w(o))for(let i=0;i<e;i++){let l=r+i*u;c[i]=BigInt(Math.round(Math.pow(n,l)))}else if(o==="bool")for(let i=0;i<e;i++){let l=r+i*u;c[i]=Math.pow(n,l)!==0?1:0}else for(let i=0;i<e;i++){let l=r+i*u;c[i]=Math.pow(n,l)}let s=D.fromData(c,[e],o);return new z(s)}function te(r,t,e=50,n=M){if(e<0)throw new Error("num must be non-negative");if(r===0||t===0)throw new Error("Geometric sequence cannot include zero");if(e===0)return V([],n);if(e===1)return V([r],n);let o=Math.sign(r),a=Math.sign(t);if(o!==a)throw new Error("Geometric sequence cannot contain both positive and negative values");let c=B(n);if(!c)throw new Error(`Cannot create geomspace array with dtype ${n}`);let u=new c(e),s=Math.log(Math.abs(r)),l=(Math.log(Math.abs(t))-s)/(e-1);if(w(n))for(let p=0;p<e;p++){let m=o*Math.exp(s+p*l);u[p]=BigInt(Math.round(m))}else if(n==="bool")for(let p=0;p<e;p++){let m=o*Math.exp(s+p*l);u[p]=m!==0?1:0}else for(let p=0;p<e;p++){let m=o*Math.exp(s+p*l);u[p]=m}let f=D.fromData(u,[e],n);return new z(f)}function Br(r,t,e=0,n=M){let o=t??r,a=J([r,o],n),c=a.data;if(w(n)){let u=c;for(let s=0;s<r;s++){let i=s+e;i>=0&&i<o&&(u[s*o+i]=BigInt(1))}}else{let u=c;for(let s=0;s<r;s++){let i=s+e;i>=0&&i<o&&(u[s*o+i]=1)}}return a}function Ur(r,t=M){return J(r,t)}function _r(r,t,e){let n=e;n||(typeof t=="bigint"?n="int64":typeof t=="boolean"?n="bool":Number.isInteger(t)?n="int32":n=M);let o=B(n);if(!o)throw new Error(`Cannot create full array with dtype ${n}`);let a=r.reduce((s,i)=>s*i,1),c=new o(a);if(w(n)){let s=typeof t=="bigint"?t:BigInt(Math.round(Number(t)));c.fill(s)}else n==="bool"?c.fill(t?1:0):c.fill(Number(t));let u=D.fromData(c,r,n);return new z(u)}function ee(r,t=M){return Br(r,r,0,t)}function ne(r,t){return r instanceof z?!t||r.dtype===t?r:r.astype(t):V(r,t)}function oe(r){return r.copy()}function ae(r,t){return J(Array.from(r.shape),t??r.dtype)}function se(r,t){return zr(Array.from(r.shape),t??r.dtype)}function ie(r,t){return Ur(Array.from(r.shape),t??r.dtype)}function ue(r,t,e){return _r(Array.from(r.shape),t,e??r.dtype)}function ce(r){return r.sqrt()}function le(r,t){return r.power(t)}function fe(r){return r.absolute()}function pe(r){return r.negative()}function ye(r){return r.sign()}function me(r,t){return r.mod(t)}function de(r,t){return r.floor_divide(t)}function ge(r){return r.positive()}function he(r){return r.reciprocal()}function Ae(r,t){return r.dot(t)}function be(r){return r.trace()}function Se(r,t){return r.transpose(t)}function De(r,t){return r.inner(t)}function we(r,t){return r.outer(t)}function Ne(r,t,e=2){return r.tensordot(t,e)}function xe(r){return r.sin()}function Ie(r){return r.cos()}function ze(r){return r.tan()}function Be(r){return r.arcsin()}function Ue(r){return r.arccos()}function _e(r){return r.arctan()}function Ee(r,t){return r.arctan2(t)}function Te(r,t){return r.hypot(t)}function Oe(r){return r.degrees()}function $e(r){return r.radians()}function Me(r){return r.sinh()}function Ce(r){return r.cosh()}function ke(r){return r.tanh()}function ve(r){return r.arcsinh()}function Ve(r){return r.arccosh()}function Pe(r){return r.arctanh()}function Fe(r,t,e){return r.swapaxes(t,e)}function Re(r,t,e){return r.moveaxis(t,e)}function Le(r,t=0){if(r.length===0)throw new Error("need at least one array to concatenate");let e=r.map(o=>o.storage),n=R(e,t);return z._fromStorage(n)}function je(r,t=0){if(r.length===0)throw new Error("need at least one array to stack");let e=r.map(o=>o.storage),n=pt(e,t);return z._fromStorage(n)}function qe(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=yt(t);return z._fromStorage(e)}function Ze(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=mt(t);return z._fromStorage(e)}function He(r){if(r.length===0)throw new Error("need at least one array to stack");let t=r.map(n=>n.storage),e=dt(t);return z._fromStorage(e)}function Ye(r,t,e=0){return gt(r.storage,t,e).map(o=>z._fromStorage(o,r.base??r))}function We(r,t,e=0){return or(r.storage,t,e).map(o=>z._fromStorage(o,r.base??r))}function Ge(r,t){return At(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function Je(r,t){return bt(r.storage,t).map(n=>z._fromStorage(n,r.base??r))}function Xe(r,t){let e=St(r.storage,t);return z._fromStorage(e)}function Qe(r,t,e){return r.repeat(t,e)}function Ke(r,t){let e=Zt(r.storage,t);return z._fromStorage(e,r.base??r)}function rn(...r){let t=r.map(n=>n.storage);return Ht(t).map((n,o)=>z._fromStorage(n,r[o].base??r[o]))}function tn(r,t,e){return r.take(t,e)}function en(r,t,e){r.put(t,e)}function nn(r,t){let e=t.map(o=>o.storage),n=Gt(r.storage,e);return z._fromStorage(n)}function on(r,t,e=!1){return Jt(r.storage,t.storage,e)}var X=new Uint8Array([147,78,85,77,80,89]),sr=["float64","float32","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"];function Er(){let r=new ArrayBuffer(2);return new DataView(r).setInt16(0,256,!0),new Int16Array(r)[0]===256}var so={f8:"float64",f4:"float32",i8:"int64",i4:"int32",i2:"int16",i1:"int8",u8:"uint64",u4:"uint32",u2:"uint16",u1:"uint8",b1:"bool"},Q={float64:"<f8",float32:"<f4",int64:"<i8",int32:"<i4",int16:"<i2",int8:"|i1",uint64:"<u8",uint32:"<u4",uint16:"<u2",uint8:"|u1",bool:"|b1"},an={c:"complex numbers",S:"byte strings",U:"Unicode strings",O:"Python objects",V:"structured arrays (void)",M:"datetime64",m:"timedelta64"};function sn(r){if(r.startsWith("[")||r.startsWith("("))throw new v(`Structured/compound dtypes are not supported: ${r}`);let t="",e=r;(r[0]==="<"||r[0]===">"||r[0]==="="||r[0]==="|")&&(t=r[0],e=r.slice(1));let n=e[0];if(n&&n in an)throw new v(`Unsupported dtype: ${an[n]} (${r}). Use the 'force' parameter to skip arrays with unsupported dtypes.`);let o=so[e];if(!o)throw new v(`Unknown or unsupported dtype descriptor: ${r}. Supported types: ${sr.join(", ")}. Use the 'force' parameter to skip arrays with unsupported dtypes.`);let a=Er(),c=t==="<"||t==="|"||t==="="&&a,u=t===">"||t==="="&&!a,s=parseInt(e.slice(1),10),i=s>1&&(u&&a||c&&!a);return{dtype:o,needsByteSwap:i,itemsize:s}}var v=class extends Error{constructor(t){super(t),this.name="UnsupportedDTypeError"}},C=class extends Error{constructor(t){super(t),this.name="InvalidNpyError"}};function K(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=ir(t);return ur(t,e)}function ir(r){if(r.length<10)throw new C("File too small to be a valid NPY file");for(let i=0;i<X.length;i++)if(r[i]!==X[i])throw new C("Invalid NPY magic number");let t=r[6],e=r[7];if(t!==1&&t!==2&&t!==3)throw new C(`Unsupported NPY version: ${t}.${e}`);let n,o;t===1?(n=r[8]|r[9]<<8,o=10):(n=r[8]|r[9]<<8|r[10]<<16|r[11]<<24,o=12);let a=o+n;if(r.length<a)throw new C("File truncated: header extends beyond file");let c=r.slice(o,a),u=new TextDecoder("utf-8").decode(c).trim(),s=io(u);return{version:{major:t,minor:e},header:s,dataOffset:a}}function ur(r,t){let{header:e,dataOffset:n}=t,{dtype:o,needsByteSwap:a,itemsize:c}=sn(e.descr),u=e.shape.reduce((d,A)=>d*A,1),s=u*c,i=r.length-n;if(i<s)throw new C(`File truncated: expected ${s} bytes of data, got ${i}`);let l=new ArrayBuffer(s);new Uint8Array(l).set(r.subarray(n,n+s));let p=uo(l,o,u,a,c),m=e.shape,y;if(e.fortran_order&&m.length>1){let d=[...m].reverse(),A=D.fromData(p,d,o);y=co(A,d),m=e.shape}else y=D.fromData(p,[...m],o);return new z(y)}function io(r){let t=r.match(/'descr'\s*:\s*'([^']+)'/),e=r.match(/'fortran_order'\s*:\s*(True|False)/),n=r.match(/'shape'\s*:\s*\(([^)]*)\)/);if(!t||!e||!n)throw new C(`Failed to parse NPY header: ${r}`);let o=t[1],a=e[1]==="True",c=n[1].trim(),u;return c===""?u=[]:u=c.split(",").map(s=>s.trim()).filter(s=>s!=="").map(s=>{let i=parseInt(s,10);if(isNaN(i))throw new C(`Invalid shape value: ${s}`);return i}),{descr:o,fortran_order:a,shape:u}}function uo(r,t,e,n,o){let a=B(t);if(!a)throw new C(`Cannot create array for dtype: ${t}`);if(!n)return new a(r,0,e);let c=new Uint8Array(r),u=new Uint8Array(r.byteLength);for(let s=0;s<e;s++){let i=s*o;for(let l=0;l<o;l++)u[i+l]=c[i+o-1-l]}return new a(u.buffer,0,e)}function co(r,t){let e=t.length,n=r.size,o=r.dtype,a=B(o);if(!a)throw new C(`Cannot create array for dtype: ${o}`);let c=new a(n),u=[...t].reverse(),s=un(t),i=un(u),l=new Array(e).fill(0);for(let f=0;f<n;f++){let p=f;for(let y=0;y<e;y++){let d=s[y];l[y]=Math.floor(p/d),p=p%d}let m=0;for(let y=0;y<e;y++)m+=l[e-1-y]*i[y];w(o),c[m]=r.iget(f)}return D.fromData(c,u,o)}function un(r){let t=new Array(r.length),e=1;for(let n=r.length-1;n>=0;n--)t[n]=e,e*=r[n];return t}function q(r){let t=r.shape,e=r.dtype,n=Q[e],o=t.length===0?"()":t.length===1?`(${t[0]},)`:`(${t.join(", ")})`,a=`{'descr': '${n}', 'fortran_order': False, 'shape': ${o}, }`,c=12,s=(64-(c+a.length+1)%64)%64;a=a+" ".repeat(s)+`
2
+ `;let i=new TextEncoder().encode(a),l=i.length,f=r.size,p=Lr(e),m=f*p,y=c+l+m,d=new Uint8Array(y);d.set(X,0),d[6]=3,d[7]=0,d[8]=l&255,d[9]=l>>8&255,d[10]=l>>16&255,d[11]=l>>24&255,d.set(i,c);let A=c+l;return lo(r,d.subarray(A),p),d}function lo(r,t,e){let n=r.dtype,o=r.size,a=Er(),c=w(n),u=r._storage;if(u.isCContiguous&&u.offset===0&&a){let i=u.data,l=new Uint8Array(i.buffer,i.byteOffset,o*e);t.set(l)}else{let i=new DataView(t.buffer,t.byteOffset,t.byteLength);for(let l=0;l<o;l++){let f=u.iget(l),p=l*e;c?fo(i,p,f,n==="uint64"):po(i,p,f,n)}}}function fo(r,t,e,n){n?r.setBigUint64(t,e,!0):r.setBigInt64(t,e,!0)}function po(r,t,e,n){switch(n){case"float64":r.setFloat64(t,e,!0);break;case"float32":r.setFloat32(t,e,!0);break;case"int32":r.setInt32(t,e,!0);break;case"int16":r.setInt16(t,e,!0);break;case"int8":r.setInt8(t,e);break;case"uint32":r.setUint32(t,e,!0);break;case"uint16":r.setUint16(t,e,!0);break;case"uint8":case"bool":r.setUint8(t,e);break;default:throw new Error(`Unsupported dtype for serialization: ${n}`)}}var yo=(()=>{let r=new Uint32Array(256);for(let t=0;t<256;t++){let e=t;for(let n=0;n<8;n++)e=e&1?3988292384^e>>>1:e>>>1;r[t]=e}return r})();function Tr(r){let t=4294967295;for(let e=0;e<r.length;e++)t=yo[(t^r[e])&255]^t>>>8;return(t^4294967295)>>>0}async function cn(r){let t=fn(r),e=new Map;for(let n of t){let o=await mo(n);e.set(n.name,o)}return e}function ln(r){let t=fn(r),e=new Map;for(let n of t){if(n.compressionMethod!==0)throw new Error(`Cannot read compressed entry synchronously: ${n.name}. Use readZip() (async) for DEFLATE-compressed files.`);e.set(n.name,n.compressedData)}return e}function fn(r){let t=r instanceof ArrayBuffer?new Uint8Array(r):r,e=new DataView(t.buffer,t.byteOffset,t.byteLength),n=[],o=-1;for(let i=t.length-22;i>=0;i--)if(e.getUint32(i,!0)===101010256){o=i;break}if(o===-1)throw new Error("Invalid ZIP file: end of central directory not found");let a=e.getUint32(o+16,!0),c=e.getUint16(o+10,!0),u=[],s=a;for(let i=0;i<c&&e.getUint32(s,!0)===33639248;i++){let f=e.getUint16(s+10,!0),p=e.getUint32(s+16,!0),m=e.getUint32(s+20,!0),y=e.getUint32(s+24,!0),d=e.getUint16(s+28,!0),A=e.getUint16(s+30,!0),h=e.getUint16(s+32,!0),g=e.getUint32(s+42,!0),b=t.slice(s+46,s+46+d),S=new TextDecoder("utf-8").decode(b);u.push({name:S,compressionMethod:f,crc32:p,compressedSize:m,uncompressedSize:y,localHeaderOffset:g}),s=s+46+d+A+h}for(let i of u){let l=i.localHeaderOffset;if(e.getUint32(l,!0)!==67324752)throw new Error(`Invalid local file header at offset ${l}`);let p=e.getUint16(l+26,!0),m=e.getUint16(l+28,!0),y=l+30+p+m,d=t.slice(y,y+i.compressedSize);n.push({name:i.name,compressedData:d,compressionMethod:i.compressionMethod,crc32:i.crc32,compressedSize:i.compressedSize,uncompressedSize:i.uncompressedSize})}return n}async function mo(r){if(r.compressionMethod===0)return r.compressedData;if(r.compressionMethod===8)return await go(r.compressedData);throw new Error(`Unsupported compression method: ${r.compressionMethod}`)}async function go(r){if(typeof DecompressionStream>"u")throw new Error("DecompressionStream is not available. This environment does not support the Compression Streams API. Please use a modern browser or Node.js 18+.");let t=new DecompressionStream("deflate-raw"),e=new Uint8Array(r.length);e.set(r);let n=t.writable.getWriter();n.write(e),n.close();let o=t.readable.getReader(),a=[];for(;;){let{done:i,value:l}=await o.read();if(i)break;a.push(l)}let c=a.reduce((i,l)=>i+l.length,0),u=new Uint8Array(c),s=0;for(let i of a)u.set(i,s),s+=i.length;return u}async function cr(r,t={}){let e=t.force??!1,n=await cn(r);return pn(n,e)}function lr(r,t={}){let e=t.force??!1,n=ln(r);return pn(n,e)}function pn(r,t){let e=new Map,n=[],o=new Map;for(let[a,c]of r){if(!a.endsWith(".npy"))continue;let u=a.slice(0,-4);try{let s=K(c);e.set(u,s)}catch(s){if(s instanceof v&&t)n.push(u),o.set(u,s.message);else throw s}}return{arrays:e,skipped:n,errors:o}}async function Mr(r,t={}){let e=await cr(r,t);return Object.fromEntries(e.arrays)}function Cr(r,t={}){let e=lr(r,t);return Object.fromEntries(e.arrays)}async function yn(r,t={}){let e=t.compress??!1,n=[];for(let[p,m]of r){let y=Tr(m),d,A;e?(d=await Ao(m),d.length<m.length?A=8:(d=m,A=0)):(d=m,A=0),n.push({name:p,data:m,compressedData:d,crc:y,compressionMethod:A,offset:0})}let o=0;for(let p of n){let m=new TextEncoder().encode(p.name);o+=30+m.length+p.compressedData.length}let a=0;for(let p of n){let m=new TextEncoder().encode(p.name);a+=46+m.length}let u=o+a+22,s=new Uint8Array(u),i=new DataView(s.buffer),l=0;for(let p of n)p.offset=l,l=dn(s,i,l,p);let f=l;for(let p of n)l=gn(s,i,l,p);return hn(i,l,n.length,a,f),s}function mn(r){let t=[];for(let[l,f]of r){let p=Tr(f);t.push({name:l,data:f,compressedData:f,crc:p,compressionMethod:0,offset:0})}let e=0;for(let l of t){let f=new TextEncoder().encode(l.name);e+=30+f.length+l.compressedData.length}let n=0;for(let l of t){let f=new TextEncoder().encode(l.name);n+=46+f.length}let a=e+n+22,c=new Uint8Array(a),u=new DataView(c.buffer),s=0;for(let l of t)l.offset=s,s=dn(c,u,s,l);let i=s;for(let l of t)s=gn(c,u,s,l);return hn(u,s,t.length,n,i),c}function dn(r,t,e,n){let o=new TextEncoder().encode(n.name);return t.setUint32(e,67324752,!0),e+=4,t.setUint16(e,n.compressionMethod===8?20:10,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,n.compressionMethod,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,33,!0),e+=2,t.setUint32(e,n.crc,!0),e+=4,t.setUint32(e,n.compressedData.length,!0),e+=4,t.setUint32(e,n.data.length,!0),e+=4,t.setUint16(e,o.length,!0),e+=2,t.setUint16(e,0,!0),e+=2,r.set(o,e),e+=o.length,r.set(n.compressedData,e),e+=n.compressedData.length,e}function gn(r,t,e,n){let o=new TextEncoder().encode(n.name);return t.setUint32(e,33639248,!0),e+=4,t.setUint16(e,20,!0),e+=2,t.setUint16(e,n.compressionMethod===8?20:10,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,n.compressionMethod,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,33,!0),e+=2,t.setUint32(e,n.crc,!0),e+=4,t.setUint32(e,n.compressedData.length,!0),e+=4,t.setUint32(e,n.data.length,!0),e+=4,t.setUint16(e,o.length,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint16(e,0,!0),e+=2,t.setUint32(e,0,!0),e+=4,t.setUint32(e,n.offset,!0),e+=4,r.set(o,e),e+=o.length,e}function hn(r,t,e,n,o){r.setUint32(t,101010256,!0),t+=4,r.setUint16(t,0,!0),t+=2,r.setUint16(t,0,!0),t+=2,r.setUint16(t,e,!0),t+=2,r.setUint16(t,e,!0),t+=2,r.setUint32(t,n,!0),t+=4,r.setUint32(t,o,!0),t+=4,r.setUint16(t,0,!0)}async function Ao(r){if(typeof CompressionStream>"u")throw new Error("CompressionStream is not available. This environment does not support the Compression Streams API. Please use a modern browser or Node.js 18+.");let t=new CompressionStream("deflate-raw"),e=new Uint8Array(r.length);e.set(r);let n=t.writable.getWriter();n.write(e),n.close();let o=t.readable.getReader(),a=[];for(;;){let{done:i,value:l}=await o.read();if(i)break;a.push(l)}let c=a.reduce((i,l)=>i+l.length,0),u=new Uint8Array(c),s=0;for(let i of a)u.set(i,s),s+=i.length;return u}async function kr(r,t={}){let e=An(r);return yn(e,{compress:t.compress??!1})}function vr(r){let t=An(r);return mn(t)}function An(r){let t=new Map;if(Array.isArray(r)){for(let n=0;n<r.length;n++){let o=r[n],a=q(o);t.set(`arr_${n}.npy`,a)}return t}let e=r instanceof Map?r.entries():Object.entries(r);for(let[n,o]of e){if(typeof n!="string"||n.length===0)throw new Error("Array names must be non-empty strings");let a=q(o),c=n.endsWith(".npy")?n:`${n}.npy`;t.set(c,a)}return t}var bo="0.5.0";0&&(module.exports={DTYPE_TO_DESCR,InvalidNpyError,NDArray,SUPPORTED_DTYPES,UnsupportedDTypeError,__version__,absolute,arange,arccos,arccosh,arcsin,arcsinh,arctan,arctan2,arctanh,array,array_equal,array_split,asarray,broadcast_arrays,broadcast_to,choose,concatenate,copy,cos,cosh,degrees,dot,dstack,empty,empty_like,eye,floor_divide,full,full_like,geomspace,hsplit,hstack,hypot,identity,inner,linspace,loadNpz,loadNpzSync,logspace,mod,moveaxis,negative,ones,ones_like,outer,parseNpy,parseNpyData,parseNpyHeader,parseNpz,parseNpzSync,positive,power,put,radians,reciprocal,repeat,serializeNpy,serializeNpz,serializeNpzSync,sign,sin,sinh,split,sqrt,stack,swapaxes,take,tan,tanh,tensordot,tile,trace,transpose,vsplit,vstack,zeros,zeros_like});
2
3
  //# sourceMappingURL=numpy-ts.node.cjs.map