dielines
Version:
Create dielines with pantograph APIs
8 lines • 167 kB
JavaScript
(function(it,mt){typeof exports=="object"&&typeof module<"u"?mt(exports):typeof define=="function"&&define.amd?define(["exports"],mt):(it=typeof globalThis<"u"?globalThis:it||self,mt(it.Dielines={}))})(this,function(it){"use strict";var No=Object.defineProperty;var Do=(it,mt,Rt)=>mt in it?No(it,mt,{enumerable:!0,configurable:!0,writable:!0,value:Rt}):it[mt]=Rt;var Ct=(it,mt,Rt)=>Do(it,typeof mt!="symbol"?mt+"":mt,Rt);var mt=Object.defineProperty,Rt=i=>{throw TypeError(i)},rs=(i,t,e)=>t in i?mt(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,_=(i,t,e)=>rs(i,typeof t!="symbol"?t+"":t,e),qe=(i,t,e)=>t.has(i)||Rt("Cannot "+e),yt=(i,t,e)=>(qe(i,t,"read from private field"),e?e.call(i):t.get(i)),xn=(i,t,e)=>t.has(i)?Rt("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(i):t.set(i,e),ze=(i,t,e,n)=>(qe(i,t,"write to private field"),t.set(i,e),e),vn=(i,t,e)=>(qe(i,t,"access private method"),e);const An=Symbol.for("pantograph:BoundingBox");function ue(i,t,e,n){return i<=n&&t>=e}let Jt=class ae{constructor(t=1/0,e=1/0,n=-1/0,s=-1/0){_(this,"xMin"),_(this,"yMin"),_(this,"xMax"),_(this,"yMax"),Object.defineProperty(this,An,{value:!0}),this.xMin=t,this.yMin=e,this.xMax=n,this.yMax=s}static isInstance(t){return!!t&&t[An]===!0}get width(){return this.xMax-this.xMin}get height(){return this.yMax-this.yMin}get center(){return[(this.xMin+this.xMax)/2,(this.yMin+this.yMax)/2]}grow(t){return new ae(this.xMin-t,this.yMin-t,this.xMax+t,this.yMax+t)}contains(t){const[e,n]=t;return ue(this.xMin,this.xMax,e,e)&&ue(this.yMin,this.yMax,n,n)}overlaps(t){return ue(this.xMin,this.xMax,t.xMin,t.xMax)&&ue(this.yMin,this.yMax,t.yMin,t.yMax)}addPoint(t){const[e,n]=t;return new ae(Math.min(this.xMin,e),Math.min(this.yMin,n),Math.max(this.xMax,e),Math.max(this.yMax,n))}merge(t){return new ae(Math.min(this.xMin,t.xMin),Math.min(this.yMin,t.yMin),Math.max(this.xMax,t.xMax),Math.max(this.yMax,t.yMax))}intersection(t){return new ae(Math.max(this.xMin,t.xMin),Math.max(this.yMin,t.yMin),Math.min(this.xMax,t.xMax),Math.min(this.yMax,t.yMax))}};function Ne(i){return i.reduce((t,e)=>t.addPoint(e),new Jt)}const En=(i,t=1e-9)=>{let e=i;return Math.abs(i)<t&&(e=0),e.toFixed(-Math.log10(t))};function vt(i,t=1e-9){return Array.from(new Map(i.map(([e,n])=>[`[${En(e,t)},${En(n,t)}]`,[e,n]])).values())}const Tt=Math.PI/180,De=180/Math.PI,st=i=>`[${i[0]}, ${i[1]}]`,S=([i,t],[e,n],s=1e-9)=>Math.abs(i-e)<=s&&Math.abs(t-n)<=s,W=([i,t],[e,n])=>[i+e,t+n],q=([i,t],[e,n])=>[i-e,t-n],bt=([i,t])=>i*i+t*t,os=([i,t])=>Math.sqrt(bt([i,t])),H=([i,t],e)=>[i*e,t*e],$t=([i,t],[e,n]=[0,0])=>(i-e)**2+(t-n)**2,Z=(i,t=[0,0])=>Math.sqrt($t(i,t));function xt([i,t],[e,n]){return i*n-t*e}function ce([i,t],[e,n]){return i*e+t*n}function rt([i,t]){const e=Z([i,t]);return[i/e,t/e]}function At(i,t){const e=Math.cos(t)*i,n=Math.sin(t)*i;return[e,n]}function as([i,t]){return Math.atan2(t,i)}function In(i){const t=Z(i),e=as(i);return[t,e]}function Ue(i,t,e=1e-9){const n=xt(i,t),s=bt(i),r=bt(t);return n*n<s*r*e*e}function tt(i){return[-i[1],i[0]]}function Zt(i){return[i[1],-i[0]]}function hs(i,t,e){return S(e,i)||Ue(q(e,i),t)}const Sn=Symbol.for("pantograph:TransformationMatrix"),jt=(i,t)=>{const[e,n,s,r,o,a,h,l,c]=i,[u,m,w,g,p,M,f,P,b]=t;return[e*u+n*g+s*f,e*m+n*p+s*P,e*w+n*M+s*b,r*u+o*g+a*f,r*m+o*p+a*P,r*w+o*M+a*b,h*u+l*g+c*f,h*m+l*p+c*P,h*w+l*M+c*b]},ls=i=>{const[t,e,n,s,r,o,a,h,l]=i,c=t*(r*l-o*h)-e*(s*l-o*a)+n*(s*h-r*a);return[(r*l-o*h)/c,(n*h-e*l)/c,(e*o-n*r)/c,(o*a-s*l)/c,(t*l-n*a)/c,(n*s-t*o)/c,(s*h-r*a)/c,(e*a-t*h)/c,(t*r-e*s)/c]},us=i=>{const[t,e,n,s,r,o,a,h,l]=i;return[t,s,a,e,r,h,n,o,l]};let ct=class is{constructor(t){_(this,"_matrix",[1,0,0,0,1,0,0,0,1]),Object.defineProperty(this,Sn,{value:!0}),t&&(this._matrix=[...t])}static isInstance(t){return!!t&&t[Sn]===!0}clone(){return new is(this._matrix)}transpose(){return this._matrix=us(this._matrix),this}inverse(){return this._matrix=ls(this._matrix),this}translate(t,e){return this._matrix=jt([1,0,t,0,1,e,0,0,1],this._matrix),this}rotate(t,e){const n=Math.cos(t),s=Math.sin(t),r=[n,-s,0,s,n,0,0,0,1];return e&&this.translate(-e[0],-e[1]),this._matrix=jt(r,this._matrix),e&&this.translate(e[0],e[1]),this}mirrorX(){return this._matrix=jt([1,0,0,0,-1,0,0,0,1],this._matrix),this}mirrorY(){return this._matrix=jt([-1,0,0,0,1,0,0,0,1],this._matrix),this}mirrorLine(t,e){const[n,s]=t,r=Math.atan2(s,n);return e&&this.translate(-e[0],-e[1]),this.rotate(-r),this.mirrorX(),this.rotate(r),e&&this.translate(e[0],e[1]),this}mirrorCenter(t){return t&&this.translate(-t[0],-t[1]),this._matrix=jt([-1,0,0,0,-1,0,0,0,1],this._matrix),t&&this.translate(t[0],t[1]),this}scale(t,e){return e&&this.translate(-e[0],-e[1]),this._matrix=jt([t,0,0,0,t,0,0,0,1],this._matrix),e&&this.translate(e[0],e[1]),this}transform(t){const[e,n]=t,[s,r,o,a,h,l]=this._matrix;return[s*e+r*n+o,a*e+h*n+l]}transformAngle(t){const[e,n]=this.transform([Math.cos(t),Math.sin(t)]),[s,r]=this.transform([0,0]);return Math.atan2(n-r,e-s)}keepsOrientation(){const[t,,,,e]=this._matrix;return t*e>0}scaleFactor(){const[t,,,e]=this._matrix;return Math.sqrt(t*t+e*e)}};class Ft{translateX(t){const e=new ct().translate(t,0);return this.transform(e)}translateY(t){const e=new ct().translate(0,t);return this.transform(e)}translate(t,e){const n=new ct().translate(t,e);return this.transform(n)}translateTo([t,e]){const n=new ct().translate(t,e);return this.transform(n)}rotate(t,e){const n=new ct().rotate(t*Tt,e);return this.transform(n)}scale(t,e){const n=new ct().scale(t,e);return this.transform(n)}mirrorCenter(t){const e=new ct().mirrorCenter(t);return this.transform(e)}mirror(t="x",e){const n=new ct;return t==="x"?n.mirrorX():t==="y"?n.mirrorY():n.mirrorLine(t,e),this.transform(n)}}let Kt=class extends Ft{constructor(t,e){super(),_(this,"precision",1e-9),this.firstPoint=t,this.lastPoint=e,this.firstPoint=t,this.lastPoint=e}get repr(){return`${this.segmentType} ${st(this.firstPoint)} - ${st(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}};const kn=Symbol.for("pantograph:Line");let U=class Xt extends Kt{constructor(t,e){super(t,e),_(this,"segmentType","LINE"),_(this,"_V",null),_(this,"_slope",null),_(this,"_yIntercept",null),_(this,"_boundingBox",null),Object.defineProperty(this,kn,{value:!0})}static isInstance(t){return!!t&&t[kn]===!0}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return W(this.firstPoint,H(this.V,t))}get length(){return Z(this.firstPoint,this.lastPoint)}get squareLength(){return $t(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=q(this.lastPoint,this.firstPoint)),this._V}get slope(){if(this._slope===null){const[t,e]=this.V;this._slope=e/t}return this._slope}get yIntercept(){return this._yIntercept===null&&(this._yIntercept=this.firstPoint[1]-this.slope*this.firstPoint[0]),this._yIntercept}get midPoint(){return W(this.firstPoint,H(this.V,.5))}isSame(t){return Xt.isInstance(t)?S(this.firstPoint,t.firstPoint)&&S(this.lastPoint,t.lastPoint)||S(this.lastPoint,t.firstPoint)&&S(this.firstPoint,t.lastPoint):!1}clone(){return new Xt(this.firstPoint,this.lastPoint)}reverse(){return new Xt(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new Jt(Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)),this._boundingBox}distanceFrom(t){const e=q(t,this.firstPoint),n=ce(e,this.V)/this.squareLength;if(n<0)return Z(t,this.firstPoint);if(n>1)return Z(t,this.lastPoint);const s=this.paramPoint(n);return Z(t,s)}isOnSegment(t){if(S(t,this.firstPoint,this.precision))return!0;const e=q(t,this.firstPoint);if(!Ue(this.V,e))return!1;const n=ce(e,this.V)/this.squareLength;return this.isValidParameter(n)}gradientAt(t){return this.V}tangentAt(t){if(!this.isOnSegment(t))throw new Error("Point is not on segment");return rt(this.V)}get normalVector(){return tt(rt(this.V))}get tangentAtFirstPoint(){return rt(this.V)}get tangentAtLastPoint(){return rt(this.V)}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t],e.forEach(h=>{if(!this.isOnSegment(h))throw new Error(`Point ${st(h)} is not on segment ${this.repr}`)});const n=[this.firstPoint,...e,this.lastPoint],s=vt(n),r=this.lastPoint[0]-this.firstPoint[0];let o=Math.sign(r),a=0;return Math.abs(r)<this.precision&&(o=Math.sign(this.lastPoint[1]-this.firstPoint[1]),a=1),s.sort((h,l)=>o*(h[a]-l[a])),s.flatMap((h,l)=>l===s.length-1?[]:new Xt(h,s[l+1]))}transform(t){return new Xt(t.transform(this.firstPoint),t.transform(this.lastPoint))}};function cs(i){return Array.from(Array(i).keys())}function Mt(i){const t=Math.min(...i.map(e=>e.length));return cs(t).map(e=>i.map(n=>n[e]))}function _t(i,t=1e-9){return i<0?i+2*Math.PI:i>=2*Math.PI?i%(2*Math.PI):i>2*Math.PI-t?0:i}function fe(i,t,e,n=1e-9){let s=t-i;return e&&(s=-s),s<0&&(s+=2*Math.PI),s>2*Math.PI-n?0:s}const me=(i,t,e)=>{const n=xt(i.V,t.V),s=bt(i.V),r=bt(t.V),o=e?e*e:i.precision*t.precision;if(n*n<s*r*o)return"parallel";const a=q(t.firstPoint,i.firstPoint),h=xt(a,t.V)/n,l=xt(a,i.V)/n;return{intersectionParam1:h,intersectionParam2:l}};function Cn(i,t,e=!1,n){const s=me(i,t,n);if(s==="parallel"){if(!e)return null;if(i.isSame(t))return i;const a=vt([t.isOnSegment(i.firstPoint)?i.firstPoint:null,t.isOnSegment(i.lastPoint)?i.lastPoint:null,i.isOnSegment(t.firstPoint)?t.firstPoint:null,i.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(h=>h!==null)).sort((h,l)=>h[0]-l[0]);if(a.length===0||a.length===1)return null;if(a.length===2)return new U(a[0],a[1]);throw console.error(a),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:r,intersectionParam2:o}=s;return!i.isValidParameter(r)||!t.isValidParameter(o)?null:i.paramPoint(r)}const ge=(i,t)=>{const e=q(i,t);return In(e)},Rn=Symbol.for("pantograph:Arc");let D=class Wt extends Kt{constructor(t,e,n,s=!1,{ignoreChecks:r=!1}={}){if(super(t,e),_(this,"segmentType","ARC"),_(this,"center"),_(this,"clockwise"),_(this,"_coefficients",null),_(this,"_angularLength",null),_(this,"_radius",null),_(this,"_firstAngle",null),_(this,"_lastAngle",null),_(this,"_boundingBox",null),Object.defineProperty(this,Rn,{value:!0}),this.center=n,this.clockwise=s,!r){if(S(t,e))throw new Error("Invalid arc, cannot be a full circle");if(Math.abs(this.radius-Z(this.lastPoint,this.center))>this.precision)throw new Error(`Invalid arc, radius does not match between ${st(t)} and ${st(e)}} (center ${st(n)})`)}}static isInstance(t){return!!t&&t[Rn]===!0}get info(){return`ARC(${st(this.firstPoint)}, ${st(this.lastPoint)}, ${st(this.center)}, ${this.clockwise?"CW":"CCW"})`}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,n=this.radius*this.radius;this._coefficients={x2:1/n,xy:0,y2:1/n,x:-(2*t)/n,y:-(2*e)/n,c:(t*t+e*e-n)/n}}return this._coefficients}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}angleToParam(t){return fe(this.firstAngle,_t(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=fe(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return W(this.center,At(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,n]=ge(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error(`Point ${st(t)} is not on segment ${this.repr}`);const s=this.angleToParam(n);if(!this.isValidParameter(s))throw new Error(`Point ${st(t)} is not on segment ${this.repr}`);return s}get radius(){return this._radius===null&&(this._radius=Z(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=q(this.firstPoint,this.center);this._firstAngle=_t(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=q(this.lastPoint,this.center);this._lastAngle=_t(Math.atan2(e,t))}return this._lastAngle}get length(){return this.radius*this.angularLength}get squareLength(){return this.length*this.length}get midPoint(){return this.paramPoint(.5)}isSame(t){return!Wt.isInstance(t)||!S(this.center,t.center)?!1:S(this.firstPoint,t.firstPoint)&&S(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||S(this.lastPoint,t.firstPoint)&&S(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new Wt(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new Wt(this.lastPoint,this.firstPoint,this.center,!this.clockwise)}get boundingBox(){if(this._boundingBox===null){const t=this.radius+this.precision,e=n=>this.isValidParameter(this.angleToParam(n));this._boundingBox=new Jt(e(Math.PI)?this.center[0]-t:Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,e(Math.PI*1.5)?this.center[1]-t:Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,e(0)?this.center[0]+t:Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,e(Math.PI/2)?this.center[1]+t:Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)}return this._boundingBox}distanceFrom(t){const[e,n]=ge(t,this.center);return this.isValidParameter(this.angleToParam(n))?Math.abs(e-this.radius):Math.sqrt(Math.min($t(t,this.firstPoint),$t(t,this.lastPoint)))}isOnSegment(t){if(S(t,this.firstPoint)||S(t,this.lastPoint))return!0;const[e,n]=ge(t,this.center);if(Math.abs(e-this.radius)>this.precision)return!1;const s=this.angleToParam(n);return this.isValidParameter(s)}gradientAt(t){const e=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),n=this.radius*this.angularLength,s=-n*Math.sin(e),r=n*Math.cos(e);return this.clockwise?[-s,-r]:[s,r]}tangentAt(t){const[e,n]=ge(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error("Point is not on the arc");const s=this.angleToParam(n);if(!this.isValidParameter(s))throw new Error("Point is not on the arc");const r=At(1,n);return(this.clockwise?Zt:tt)(rt(r))}get tangentAtFirstPoint(){const t=At(1,this.firstAngle);return(this.clockwise?Zt:tt)(rt(t))}get tangentAtLastPoint(){const t=At(1,this.lastAngle);return(this.clockwise?Zt:tt)(rt(t))}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=[0,1,...e.map(o=>this.pointToParam(o))],s=new Map(Mt([n,[this.firstPoint,this.lastPoint,...e]]));n.sort((o,a)=>o-a);let r=null;return n.flatMap((o,a)=>{if(a===n.length-1)return[];const h=n[a+1];if(h-o<this.precision)return r===null&&(r=o),[];const l=r===null?o:r,c=new Wt(s.get(l)||this.paramPoint(l),s.get(h)||this.paramPoint(h),this.center,this.clockwise);return r=null,c})}transform(t){return new Wt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}};function fs(i,t,e){const n=new U(t,i),s=new U(t,e),r=tt(n.tangentAtFirstPoint),o=tt(s.tangentAtLastPoint),a=me({firstPoint:n.midPoint,V:r,precision:1e-9},{firstPoint:s.midPoint,V:o,precision:1e-9});if(a==="parallel")throw new Error("Cannot create an arc from three colinear points");const h=xt(q(i,t),q(e,t))>0;return new D(i,e,W(n.midPoint,H(r,a.intersectionParam1)),h,{ignoreChecks:!0})}function Tn(i,t,e){const n=new U(i,t),s=tt(n.tangentAtFirstPoint),r=me({firstPoint:n.midPoint,V:s,precision:1e-9},{firstPoint:i,V:tt(e),precision:1e-9});if(r==="parallel")throw new Error("Cannot create an arc from three colinear points");const o=W(n.midPoint,H(s,r.intersectionParam1)),a=xt(q(o,i),q(o,W(i,e)))<0;return new D(i,t,o,a,{ignoreChecks:!0})}const $n=1e-21,pe=1.618034;function Fn(i,t=0,e=1,n=110,s=1e3){let r,o,a,h,l,c,u,m,w,g,p,M,f;for(o=i(t),a=i(e),o<a&&([t,e]=[e,t],[o,a]=[a,o]),f=e+pe*(e-t),h=i(f),l=3,u=0;h<a;){if(m=(e-t)*(a-h),w=(e-f)*(a-o),g=w-m,Math.abs(g)<$n?r=2*$n:r=2*g,p=e-((e-f)*w-(e-t)*m)/r,M=e+n*(f-e),u>s)throw new Error("Too many iterations.");if(u+=1,(p-f)*(e-p)>0){if(c=i(p),l+=1,c<h)return t=e,e=p,o=a,a=c,[t,e,f,o,a,h,l];if(c>a)return f=p,h=c,[t,e,f,o,a,h,l];p=f+pe*(f-e),c=i(p),l+=1}else(p-M)*(M-f)>=0?(p=M,c=i(p),l+=1):(p-M)*(f-p)>0?(c=i(p),l+=1,c<h&&(e=f,f=p,p=f+pe*(f-e),a=h,h=c,c=i(p),l+=1)):(p=f+pe*(f-e),c=i(p),l+=1);t=e,e=f,f=p,o=a,a=h,h=c}return[t,e,f,o,a,h,l]}const _n=.381966,ms=1e-11;class gs{constructor(t,e=148e-10,n=500){_(this,"xmin"),_(this,"fval"),_(this,"iter"),_(this,"funcalls"),_(this,"brack"),this.func=t,this.tol=e,this.maxiter=n,this.func=t,this.tol=e,this.maxiter=n,this.xmin=1/0,this.fval=1/0,this.iter=0,this.funcalls=0,this.brack=null}setBracket(t=null){this.brack=t}getBracketInfo(){let t,e,n,s,r,o,a;const h=this.func,l=this.brack;if(l===null)[r,o,a,t,e,n,s]=Fn(h);else if(l.length===2)[r,o,a,t,e,n,s]=Fn(h,l[0],l[1]);else if(l.length===3){if([r,o,a]=l,r>a&&([a,r]=[r,a]),!(r<o&&o<a))throw new Error("Not a bracketing interval.");if(t=h(r),e=h(o),n=h(a),!(e<t&&e<n))throw new Error("Not a bracketing interval.");s=3}else throw new Error("Bracketing interval must be length 2 or 3 sequence.");return[r,o,a,t,e,n,s]}optimize(){const t=this.func;let[e,n,s,,,,r]=this.getBracketInfo(),o=n,a=n,h=n,l=t(o),c=l,u=l,m,w;e<s?(m=e,w=s):(m=s,w=e);let g=0;r=1;let p=0,M=0,f;for(;p<this.maxiter;){const P=this.tol*Math.abs(o)+ms,b=2*P,I=.5*(m+w);if(Math.abs(o-I)<b-.5*(w-m))break;if(Math.abs(g)<=P)o>=I?g=m-o:g=w-o,M=_n*g;else{const V=(o-a)*(u-c);let v=(o-h)*(u-l),k=(o-h)*v-(o-a)*V;v=2*(v-V),v>0&&(k=-k),v=Math.abs(v);const O=g;g=M,k>v*(m-o)&&k<v*(w-o)&&Math.abs(k)<Math.abs(.5*v*O)?(M=k*1/v,f=o+M,(f-m<b||w-f<b)&&(I-o>=0?M=P:M=-P)):(o>=I?g=m-o:g=w-o,M=_n*g)}Math.abs(M)<P?M>=0?f=o+P:f=o-P:f=o+M;const C=t(f);r+=1,C>u?(f<o?m=f:w=f,C<=l||a===o?(h=a,a=f,c=l,l=C):(C<=c||h===o||h===a)&&(h=f,c=C)):(f>=o?m=o:w=o,h=a,a=o,o=f,c=l,l=u,u=C),p+=1}this.xmin=o,this.fval=u,this.iter=p,this.funcalls=r}results(){return{argMin:this.xmin,fMin:this.fval,iterations:this.iter,funcCalls:this.funcalls}}run(){return this.optimize(),this.results()}}function Ln(i,t=1e-8,e=1e3){const n=new gs(i,t,e);return n.setBracket([0,1]),n.run()}function Bn(i){if(i.__esModule)return i;var t=i.default;if(typeof t=="function"){var e=function n(){return this instanceof n?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};e.prototype=t.prototype}else e={};return Object.defineProperty(e,"__esModule",{value:!0}),Object.keys(i).forEach(function(n){var s=Object.getOwnPropertyDescriptor(i,n);Object.defineProperty(e,n,s.get?s:{enumerable:!0,get:function(){return i[n]}})}),e}var z={};const ps=Object.prototype.toString;function te(i){const t=ps.call(i);return t.endsWith("Array]")&&!t.includes("Big")}const ws=Object.freeze(Object.defineProperty({__proto__:null,isAnyArray:te},Symbol.toStringTag,{value:"Module"})),ds=Bn(ws);function Ps(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!te(i))throw new TypeError("input must be an array");if(i.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,n=e===void 0?0:e,s=t.toIndex,r=s===void 0?i.length:s;if(n<0||n>=i.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(r<=n||r>i.length||!Number.isInteger(r))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=i[n],a=n+1;a<r;a++)i[a]>o&&(o=i[a]);return o}function Ms(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!te(i))throw new TypeError("input must be an array");if(i.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,n=e===void 0?0:e,s=t.toIndex,r=s===void 0?i.length:s;if(n<0||n>=i.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(r<=n||r>i.length||!Number.isInteger(r))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=i[n],a=n+1;a<r;a++)i[a]<o&&(o=i[a]);return o}function ys(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(te(i)){if(i.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var e;if(t.output!==void 0){if(!te(t.output))throw new TypeError("output option must be an array if specified");e=t.output}else e=new Array(i.length);var n=Ms(i),s=Ps(i);if(n===s)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var r=t.min,o=r===void 0?t.autoMinMax?n:0:r,a=t.max,h=a===void 0?t.autoMinMax?s:1:a;if(o>=h)throw new RangeError("min option must be smaller than max option");for(var l=(h-o)/(s-n),c=0;c<i.length;c++)e[c]=(i[c]-n)*l+o;return e}const bs=Object.freeze(Object.defineProperty({__proto__:null,default:ys},Symbol.toStringTag,{value:"Module"})),xs=Bn(bs);Object.defineProperty(z,"__esModule",{value:!0});var ft=ds,Vn=xs;const we=" ".repeat(2),jn=" ".repeat(4);function vs(){return On(this)}function On(i,t={}){const{maxRows:e=15,maxColumns:n=10,maxNumSize:s=8,padMinus:r="auto"}=t;return`${i.constructor.name} {
${we}[
${jn}${As(i,e,n,s,r)}
${we}]
${we}rows: ${i.rows}
${we}columns: ${i.columns}
}`}function As(i,t,e,n,s){const{rows:r,columns:o}=i,a=Math.min(r,t),h=Math.min(o,e),l=[];if(s==="auto"){s=!1;t:for(let c=0;c<a;c++)for(let u=0;u<h;u++)if(i.get(c,u)<0){s=!0;break t}}for(let c=0;c<a;c++){let u=[];for(let m=0;m<h;m++)u.push(Es(i.get(c,m),n,s));l.push(`${u.join(" ")}`)}return h!==o&&(l[l.length-1]+=` ... ${o-e} more columns`),a!==r&&l.push(`... ${r-t} more rows`),l.join(`
${jn}`)}function Es(i,t,e){return(i>=0&&e?` ${qn(i,t-1)}`:qn(i,t)).padEnd(t)}function qn(i,t){let e=i.toString();if(e.length<=t)return e;let n=i.toFixed(t);if(n.length>t&&(n=i.toFixed(Math.max(0,t-(n.length-t)))),n.length<=t&&!n.startsWith("0.000")&&!n.startsWith("-0.000"))return n;let s=i.toExponential(t);return s.length>t&&(s=i.toExponential(Math.max(0,t-(s.length-t)))),s.slice(0)}function Is(i,t){i.prototype.add=function(e){return typeof e=="number"?this.addS(e):this.addM(e)},i.prototype.addS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)+e);return this},i.prototype.addM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)+e.get(n,s));return this},i.add=function(e,n){return new t(e).add(n)},i.prototype.sub=function(e){return typeof e=="number"?this.subS(e):this.subM(e)},i.prototype.subS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)-e);return this},i.prototype.subM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)-e.get(n,s));return this},i.sub=function(e,n){return new t(e).sub(n)},i.prototype.subtract=i.prototype.sub,i.prototype.subtractS=i.prototype.subS,i.prototype.subtractM=i.prototype.subM,i.subtract=i.sub,i.prototype.mul=function(e){return typeof e=="number"?this.mulS(e):this.mulM(e)},i.prototype.mulS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)*e);return this},i.prototype.mulM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)*e.get(n,s));return this},i.mul=function(e,n){return new t(e).mul(n)},i.prototype.multiply=i.prototype.mul,i.prototype.multiplyS=i.prototype.mulS,i.prototype.multiplyM=i.prototype.mulM,i.multiply=i.mul,i.prototype.div=function(e){return typeof e=="number"?this.divS(e):this.divM(e)},i.prototype.divS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)/e);return this},i.prototype.divM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)/e.get(n,s));return this},i.div=function(e,n){return new t(e).div(n)},i.prototype.divide=i.prototype.div,i.prototype.divideS=i.prototype.divS,i.prototype.divideM=i.prototype.divM,i.divide=i.div,i.prototype.mod=function(e){return typeof e=="number"?this.modS(e):this.modM(e)},i.prototype.modS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)%e);return this},i.prototype.modM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)%e.get(n,s));return this},i.mod=function(e,n){return new t(e).mod(n)},i.prototype.modulus=i.prototype.mod,i.prototype.modulusS=i.prototype.modS,i.prototype.modulusM=i.prototype.modM,i.modulus=i.mod,i.prototype.and=function(e){return typeof e=="number"?this.andS(e):this.andM(e)},i.prototype.andS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)&e);return this},i.prototype.andM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)&e.get(n,s));return this},i.and=function(e,n){return new t(e).and(n)},i.prototype.or=function(e){return typeof e=="number"?this.orS(e):this.orM(e)},i.prototype.orS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)|e);return this},i.prototype.orM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)|e.get(n,s));return this},i.or=function(e,n){return new t(e).or(n)},i.prototype.xor=function(e){return typeof e=="number"?this.xorS(e):this.xorM(e)},i.prototype.xorS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)^e);return this},i.prototype.xorM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)^e.get(n,s));return this},i.xor=function(e,n){return new t(e).xor(n)},i.prototype.leftShift=function(e){return typeof e=="number"?this.leftShiftS(e):this.leftShiftM(e)},i.prototype.leftShiftS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)<<e);return this},i.prototype.leftShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)<<e.get(n,s));return this},i.leftShift=function(e,n){return new t(e).leftShift(n)},i.prototype.signPropagatingRightShift=function(e){return typeof e=="number"?this.signPropagatingRightShiftS(e):this.signPropagatingRightShiftM(e)},i.prototype.signPropagatingRightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)>>e);return this},i.prototype.signPropagatingRightShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)>>e.get(n,s));return this},i.signPropagatingRightShift=function(e,n){return new t(e).signPropagatingRightShift(n)},i.prototype.rightShift=function(e){return typeof e=="number"?this.rightShiftS(e):this.rightShiftM(e)},i.prototype.rightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)>>>e);return this},i.prototype.rightShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)>>>e.get(n,s));return this},i.rightShift=function(e,n){return new t(e).rightShift(n)},i.prototype.zeroFillRightShift=i.prototype.rightShift,i.prototype.zeroFillRightShiftS=i.prototype.rightShiftS,i.prototype.zeroFillRightShiftM=i.prototype.rightShiftM,i.zeroFillRightShift=i.rightShift,i.prototype.not=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,~this.get(e,n));return this},i.not=function(e){return new t(e).not()},i.prototype.abs=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.abs(this.get(e,n)));return this},i.abs=function(e){return new t(e).abs()},i.prototype.acos=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.acos(this.get(e,n)));return this},i.acos=function(e){return new t(e).acos()},i.prototype.acosh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.acosh(this.get(e,n)));return this},i.acosh=function(e){return new t(e).acosh()},i.prototype.asin=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.asin(this.get(e,n)));return this},i.asin=function(e){return new t(e).asin()},i.prototype.asinh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.asinh(this.get(e,n)));return this},i.asinh=function(e){return new t(e).asinh()},i.prototype.atan=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.atan(this.get(e,n)));return this},i.atan=function(e){return new t(e).atan()},i.prototype.atanh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.atanh(this.get(e,n)));return this},i.atanh=function(e){return new t(e).atanh()},i.prototype.cbrt=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.cbrt(this.get(e,n)));return this},i.cbrt=function(e){return new t(e).cbrt()},i.prototype.ceil=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.ceil(this.get(e,n)));return this},i.ceil=function(e){return new t(e).ceil()},i.prototype.clz32=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.clz32(this.get(e,n)));return this},i.clz32=function(e){return new t(e).clz32()},i.prototype.cos=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.cos(this.get(e,n)));return this},i.cos=function(e){return new t(e).cos()},i.prototype.cosh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.cosh(this.get(e,n)));return this},i.cosh=function(e){return new t(e).cosh()},i.prototype.exp=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.exp(this.get(e,n)));return this},i.exp=function(e){return new t(e).exp()},i.prototype.expm1=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.expm1(this.get(e,n)));return this},i.expm1=function(e){return new t(e).expm1()},i.prototype.floor=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.floor(this.get(e,n)));return this},i.floor=function(e){return new t(e).floor()},i.prototype.fround=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.fround(this.get(e,n)));return this},i.fround=function(e){return new t(e).fround()},i.prototype.log=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log(this.get(e,n)));return this},i.log=function(e){return new t(e).log()},i.prototype.log1p=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log1p(this.get(e,n)));return this},i.log1p=function(e){return new t(e).log1p()},i.prototype.log10=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log10(this.get(e,n)));return this},i.log10=function(e){return new t(e).log10()},i.prototype.log2=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log2(this.get(e,n)));return this},i.log2=function(e){return new t(e).log2()},i.prototype.round=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.round(this.get(e,n)));return this},i.round=function(e){return new t(e).round()},i.prototype.sign=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sign(this.get(e,n)));return this},i.sign=function(e){return new t(e).sign()},i.prototype.sin=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sin(this.get(e,n)));return this},i.sin=function(e){return new t(e).sin()},i.prototype.sinh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sinh(this.get(e,n)));return this},i.sinh=function(e){return new t(e).sinh()},i.prototype.sqrt=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sqrt(this.get(e,n)));return this},i.sqrt=function(e){return new t(e).sqrt()},i.prototype.tan=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.tan(this.get(e,n)));return this},i.tan=function(e){return new t(e).tan()},i.prototype.tanh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.tanh(this.get(e,n)));return this},i.tanh=function(e){return new t(e).tanh()},i.prototype.trunc=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.trunc(this.get(e,n)));return this},i.trunc=function(e){return new t(e).trunc()},i.pow=function(e,n){return new t(e).pow(n)},i.prototype.pow=function(e){return typeof e=="number"?this.powS(e):this.powM(e)},i.prototype.powS=function(e){for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)**e);return this},i.prototype.powM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.set(n,s,this.get(n,s)**e.get(n,s));return this}}function wt(i,t,e){let n=e?i.rows:i.rows-1;if(t<0||t>n)throw new RangeError("Row index out of range")}function dt(i,t,e){let n=e?i.columns:i.columns-1;if(t<0||t>n)throw new RangeError("Column index out of range")}function Ot(i,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==i.columns)throw new RangeError("vector size must be the same as the number of columns");return t}function qt(i,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==i.rows)throw new RangeError("vector size must be the same as the number of rows");return t}function Ye(i,t){if(!ft.isAnyArray(t))throw new TypeError("row indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=i.rows)throw new RangeError("row indices are out of range")}function Xe(i,t){if(!ft.isAnyArray(t))throw new TypeError("column indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=i.columns)throw new RangeError("column indices are out of range")}function We(i,t,e,n,s){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Pe("startRow",t),Pe("endRow",e),Pe("startColumn",n),Pe("endColumn",s),t>e||n>s||t<0||t>=i.rows||e<0||e>=i.rows||n<0||n>=i.columns||s<0||s>=i.columns)throw new RangeError("Submatrix indices are out of range")}function de(i,t=0){let e=[];for(let n=0;n<i;n++)e.push(t);return e}function Pe(i,t){if(typeof t!="number")throw new TypeError(`${i} must be a number`)}function zt(i){if(i.isEmpty())throw new Error("Empty matrix has no elements to index")}function Ss(i){let t=de(i.rows);for(let e=0;e<i.rows;++e)for(let n=0;n<i.columns;++n)t[e]+=i.get(e,n);return t}function ks(i){let t=de(i.columns);for(let e=0;e<i.rows;++e)for(let n=0;n<i.columns;++n)t[n]+=i.get(e,n);return t}function Cs(i){let t=0;for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)t+=i.get(e,n);return t}function Rs(i){let t=de(i.rows,1);for(let e=0;e<i.rows;++e)for(let n=0;n<i.columns;++n)t[e]*=i.get(e,n);return t}function Ts(i){let t=de(i.columns,1);for(let e=0;e<i.rows;++e)for(let n=0;n<i.columns;++n)t[n]*=i.get(e,n);return t}function $s(i){let t=1;for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)t*=i.get(e,n);return t}function Fs(i,t,e){const n=i.rows,s=i.columns,r=[];for(let o=0;o<n;o++){let a=0,h=0,l=0;for(let c=0;c<s;c++)l=i.get(o,c)-e[o],a+=l,h+=l*l;t?r.push((h-a*a/s)/(s-1)):r.push((h-a*a/s)/s)}return r}function _s(i,t,e){const n=i.rows,s=i.columns,r=[];for(let o=0;o<s;o++){let a=0,h=0,l=0;for(let c=0;c<n;c++)l=i.get(c,o)-e[o],a+=l,h+=l*l;t?r.push((h-a*a/n)/(n-1)):r.push((h-a*a/n)/n)}return r}function Ls(i,t,e){const n=i.rows,s=i.columns,r=n*s;let o=0,a=0,h=0;for(let l=0;l<n;l++)for(let c=0;c<s;c++)h=i.get(l,c)-e,o+=h,a+=h*h;return t?(a-o*o/r)/(r-1):(a-o*o/r)/r}function Bs(i,t){for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)i.set(e,n,i.get(e,n)-t[e])}function Vs(i,t){for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)i.set(e,n,i.get(e,n)-t[n])}function js(i,t){for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)i.set(e,n,i.get(e,n)-t)}function Os(i){const t=[];for(let e=0;e<i.rows;e++){let n=0;for(let s=0;s<i.columns;s++)n+=i.get(e,s)**2/(i.columns-1);t.push(Math.sqrt(n))}return t}function qs(i,t){for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)i.set(e,n,i.get(e,n)/t[e])}function zs(i){const t=[];for(let e=0;e<i.columns;e++){let n=0;for(let s=0;s<i.rows;s++)n+=i.get(s,e)**2/(i.rows-1);t.push(Math.sqrt(n))}return t}function Ns(i,t){for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)i.set(e,n,i.get(e,n)/t[n])}function Ds(i){const t=i.size-1;let e=0;for(let n=0;n<i.columns;n++)for(let s=0;s<i.rows;s++)e+=i.get(s,n)**2/t;return Math.sqrt(e)}function Us(i,t){for(let e=0;e<i.rows;e++)for(let n=0;n<i.columns;n++)i.set(e,n,i.get(e,n)/t)}class j{static from1DArray(t,e,n){if(t*e!==n.length)throw new RangeError("data length does not match given dimensions");let s=new E(t,e);for(let r=0;r<t;r++)for(let o=0;o<e;o++)s.set(r,o,n[r*e+o]);return s}static rowVector(t){let e=new E(1,t.length);for(let n=0;n<t.length;n++)e.set(0,n,t[n]);return e}static columnVector(t){let e=new E(t.length,1);for(let n=0;n<t.length;n++)e.set(n,0,t[n]);return e}static zeros(t,e){return new E(t,e)}static ones(t,e){return new E(t,e).fill(1)}static rand(t,e,n={}){if(typeof n!="object")throw new TypeError("options must be an object");const{random:s=Math.random}=n;let r=new E(t,e);for(let o=0;o<t;o++)for(let a=0;a<e;a++)r.set(o,a,s());return r}static randInt(t,e,n={}){if(typeof n!="object")throw new TypeError("options must be an object");const{min:s=0,max:r=1e3,random:o=Math.random}=n;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(r))throw new TypeError("max must be an integer");if(s>=r)throw new RangeError("min must be smaller than max");let a=r-s,h=new E(t,e);for(let l=0;l<t;l++)for(let c=0;c<e;c++){let u=s+Math.round(o()*a);h.set(l,c,u)}return h}static eye(t,e,n){e===void 0&&(e=t),n===void 0&&(n=1);let s=Math.min(t,e),r=this.zeros(t,e);for(let o=0;o<s;o++)r.set(o,o,n);return r}static diag(t,e,n){let s=t.length;e===void 0&&(e=s),n===void 0&&(n=e);let r=Math.min(s,e,n),o=this.zeros(e,n);for(let a=0;a<r;a++)o.set(a,a,t[a]);return o}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,s=t.columns,r=new E(n,s);for(let o=0;o<n;o++)for(let a=0;a<s;a++)r.set(o,a,Math.min(t.get(o,a),e.get(o,a)));return r}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,s=t.columns,r=new this(n,s);for(let o=0;o<n;o++)for(let a=0;a<s;a++)r.set(o,a,Math.max(t.get(o,a),e.get(o,a)));return r}static checkMatrix(t){return j.isMatrix(t)?t:new E(t)}static isMatrix(t){return t!=null&&t.klass==="Matrix"}get size(){return this.rows*this.columns}apply(t){if(typeof t!="function")throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.call(this,e,n);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.push(this.get(e,n));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let n=0;n<this.columns;n++)t[e].push(this.get(e,n))}return t}toJSON(){return this.to2DArray()}isRowVector(){return this.rows===1}isColumnVector(){return this.columns===1}isVector(){return this.rows===1||this.columns===1}isSquare(){return this.rows===this.columns}isEmpty(){return this.rows===0||this.columns===0}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(this.get(t,t)!==0)return!1;return!0}isEchelonForm(){let t=0,e=0,n=-1,s=!0,r=!1;for(;t<this.rows&&s;){for(e=0,r=!1;e<this.columns&&r===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>n?(r=!0,n=e):(s=!1,r=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,n=-1,s=!0,r=!1;for(;t<this.rows&&s;){for(e=0,r=!1;e<this.columns&&r===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>n?(r=!0,n=e):(s=!1,r=!0);for(let o=e+1;o<this.rows;o++)this.get(t,o)!==0&&(s=!1);t++}return s}echelonForm(){let t=this.clone(),e=0,n=0;for(;e<t.rows&&n<t.columns;){let s=e;for(let r=e;r<t.rows;r++)t.get(r,n)>t.get(s,n)&&(s=r);if(t.get(s,n)===0)n++;else{t.swapRows(e,s);let r=t.get(e,n);for(let o=n;o<t.columns;o++)t.set(e,o,t.get(e,o)/r);for(let o=e+1;o<t.rows;o++){let a=t.get(o,n)/t.get(e,n);t.set(o,n,0);for(let h=n+1;h<t.columns;h++)t.set(o,h,t.get(o,h)-t.get(e,h)*a)}e++,n++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,n=t.rows,s=n-1;for(;s>=0;)if(t.maxRow(s)===0)s--;else{let r=0,o=!1;for(;r<n&&o===!1;)t.get(s,r)===1?o=!0:r++;for(let a=0;a<s;a++){let h=t.get(a,r);for(let l=r;l<e;l++){let c=t.get(a,l)-h*t.get(s,l);t.set(a,l,c)}}s--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{rows:e=1,columns:n=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(n)||n<=0)throw new TypeError("columns must be a positive integer");let s=new E(this.rows*e,this.columns*n);for(let r=0;r<e;r++)for(let o=0;o<n;o++)s.setSubMatrix(this,this.rows*r,this.columns*o);return s}fill(t){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,t);return this}neg(){return this.mulS(-1)}getRow(t){wt(this,t);let e=[];for(let n=0;n<this.columns;n++)e.push(this.get(t,n));return e}getRowVector(t){return E.rowVector(this.getRow(t))}setRow(t,e){wt(this,t),e=Ot(this,e);for(let n=0;n<this.columns;n++)this.set(t,n,e[n]);return this}swapRows(t,e){wt(this,t),wt(this,e);for(let n=0;n<this.columns;n++){let s=this.get(t,n);this.set(t,n,this.get(e,n)),this.set(e,n,s)}return this}getColumn(t){dt(this,t);let e=[];for(let n=0;n<this.rows;n++)e.push(this.get(n,t));return e}getColumnVector(t){return E.columnVector(this.getColumn(t))}setColumn(t,e){dt(this,t),e=qt(this,e);for(let n=0;n<this.rows;n++)this.set(n,t,e[n]);return this}swapColumns(t,e){dt(this,t),dt(this,e);for(let n=0;n<this.rows;n++){let s=this.get(n,t);this.set(n,t,this.get(n,e)),this.set(n,e,s)}return this}addRowVector(t){t=Ot(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[n]);return this}subRowVector(t){t=Ot(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[n]);return this}mulRowVector(t){t=Ot(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[n]);return this}divRowVector(t){t=Ot(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[n]);return this}addColumnVector(t){t=qt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[e]);return this}subColumnVector(t){t=qt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[e]);return this}mulColumnVector(t){t=qt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[e]);return this}divColumnVector(t){t=qt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[e]);return this}mulRow(t,e){wt(this,t);for(let n=0;n<this.columns;n++)this.set(t,n,this.get(t,n)*e);return this}mulColumn(t,e){dt(this,t);for(let n=0;n<this.rows;n++)this.set(n,t,this.get(n,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)>e[n]&&(e[n]=this.get(n,s));return e}case"column":{const e=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)>e[s]&&(e[s]=this.get(n,s));return e}case void 0:{let e=this.get(0,0);for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)>e&&(e=this.get(n,s));return e}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){zt(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)>t&&(t=this.get(n,s),e[0]=n,e[1]=s);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)<e[n]&&(e[n]=this.get(n,s));return e}case"column":{const e=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)<e[s]&&(e[s]=this.get(n,s));return e}case void 0:{let e=this.get(0,0);for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)<e&&(e=this.get(n,s));return e}default:throw new Error(`invalid option: ${t}`)}}minIndex(){zt(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let s=0;s<this.columns;s++)this.get(n,s)<t&&(t=this.get(n,s),e[0]=n,e[1]=s);return e}maxRow(t){if(wt(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n));return e}maxRowIndex(t){wt(this,t),zt(this);let e=this.get(t,0),n=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)>e&&(e=this.get(t,s),n[1]=s);return n}minRow(t){if(wt(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n));return e}minRowIndex(t){wt(this,t),zt(this);let e=this.get(t,0),n=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)<e&&(e=this.get(t,s),n[1]=s);return n}maxColumn(t){if(dt(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t));return e}maxColumnIndex(t){dt(this,t),zt(this);let e=this.get(0,t),n=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)>e&&(e=this.get(s,t),n[0]=s);return n}minColumn(t){if(dt(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t));return e}minColumnIndex(t){dt(this,t),zt(this);let e=this.get(0,t),n=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)<e&&(e=this.get(s,t),n[0]=s);return n}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let n=0;n<t;n++)e.push(this.get(n,n));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t+=this.get(e,n),this.set(e,n,t);return this}dot(t){j.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let n=0;for(let s=0;s<e.length;s++)n+=e[s]*t[s];return n}mmul(t){t=E.checkMatrix(t);let e=this.rows,n=this.columns,s=t.columns,r=new E(e,s),o=new Float64Array(n);for(let a=0;a<s;a++){for(let h=0;h<n;h++)o[h]=t.get(h,a);for(let h=0;h<e;h++){let l=0;for(let c=0;c<n;c++)l+=this.get(h,c)*o[c];r.set(h,a,l)}}return r}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=E.eye(this.rows),n=this;for(let s=t;s>1;s/=2)s&1&&(e=e.mmul(n)),n=n.mmul(n);return e}strassen2x2(t){t=E.checkMatrix(t);let e=new E(2,2);const n=this.get(0,0),s=t.get(0,0),r=this.get(0,1),o=t.get(0,1),a=this.get(1,0),h=t.get(1,0),l=this.get(1,1),c=t.get(1,1),u=(n+l)*(s+c),m=(a+l)*s,w=n*(o-c),g=l*(h-s),p=(n+r)*c,M=(a-n)*(s+o),f=(r-l)*(h+c),P=u+g-p+f,b=w+p,I=m+g,C=u-m+w+M;return e.set(0,0,P),e.set(0,1,b),e.set(1,0,I),e.set(1,1,C),e}strassen3x3(t){t=E.checkMatrix(t);let e=new E(3,3);const n=this.get(0,0),s=this.get(0,1),r=this.get(0,2),o=this.get(1,0),a=this.get(1,1),h=this.get(1,2),l=this.get(2,0),c=this.get(2,1),u=this.get(2,2),m=t.get(0,0),w=t.get(0,1),g=t.get(0,2),p=t.get(1,0),M=t.get(1,1),f=t.get(1,2),P=t.get(2,0),b=t.get(2,1),I=t.get(2,2),C=(n+s+r-o-a-c-u)*M,V=(n-o)*(-w+M),v=a*(-m+w+p-M-f-P+I),k=(-n+o+a)*(m-w+M),O=(o+a)*(-m+w),d=n*m,x=(-n+l+c)*(m-g+f),A=(-n+l)*(g-f),y=(l+c)*(-m+g),T=(n+s+r-a-h-l-c)*f,$=c*(-m+g+p-M-f-P+b),F=(-r+c+u)*(M+P-b),Q=(r-u)*(M-b),L=r*P,N=(c+u)*(-P+b),B=(-r+a+h)*(f+P-I),X=(r-h)*(f-I),G=(a+h)*(-P+I),R=s*p,Y=h*b,nt=o*g,K=l*w,J=u*I,dn=d+L+R,Pn=C+k+O+d+F+L+N,Mn=d+x+y+T+L+B+G,yn=V+v+k+d+L+B+X,_e=V+k+O+d+Y,Le=L+B+X+G+nt,Be=d+x+A+$+F+Q+L,Ve=F+Q+L+N+K,je=d+x+A+y+J;return e.set(0,0,dn),e.set(0,1,Pn),e.set(0,2,Mn),e.set(1,0,yn),e.set(1,1,_e),e.set(1,2,Le),e.set(2,0,Be),e.set(2,1,Ve),e.set(2,2,je),e}mmulStrassen(t){t=E.checkMatrix(t);let e=this.clone(),n=e.rows,s=e.columns,r=t.rows,o=t.columns;s!==r&&console.warn(`Multiplying ${n} x ${s} and ${r} x ${o} matrix: dimensions do not match