dielines 0.10.1 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dielines.js +1394 -1342
- package/dist/dielines.js.map +1 -1
- package/dist/dielines.umd.cjs +13 -13
- package/dist/dielines.umd.cjs.map +1 -1
- package/package.json +3 -3
package/dist/dielines.umd.cjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
(function(it,gt){typeof exports=="object"&&typeof module<"u"?gt(exports):typeof define=="function"&&define.amd?define(["exports"],gt):(it=typeof globalThis<"u"?globalThis:it||self,gt(it.Dielines={}))})(this,function(it){"use strict";var Io=Object.defineProperty;var $o=(it,gt,It)=>gt in it?Io(it,gt,{enumerable:!0,configurable:!0,writable:!0,value:It}):it[gt]=It;var Tt=(it,gt,It)=>$o(it,typeof gt!="symbol"?gt+"":gt,It);var gt=Object.defineProperty,It=i=>{throw TypeError(i)},Hi=(i,t,e)=>t in i?gt(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,_=(i,t,e)=>Hi(i,typeof t!="symbol"?t+"":t,e),Oe=(i,t,e)=>t.has(i)||It("Cannot "+e),bt=(i,t,e)=>(Oe(i,t,"read from private field"),e?e.call(i):t.get(i)),vn=(i,t,e)=>t.has(i)?It("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(i):t.set(i,e),ze=(i,t,e,n)=>(Oe(i,t,"write to private field"),t.set(i,e),e),An=(i,t,e)=>(Oe(i,t,"access private method"),e);function ce(i,t,e,n){return i<=n&&t>=e}let Zt=class he{constructor(t=1/0,e=1/0,n=-1/0,s=-1/0){_(this,"xMin"),_(this,"yMin"),_(this,"xMax"),_(this,"yMax"),this.xMin=t,this.yMin=e,this.xMax=n,this.yMax=s}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 he(this.xMin-t,this.yMin-t,this.xMax+t,this.yMax+t)}contains(t){const[e,n]=t;return ce(this.xMin,this.xMax,e,e)&&ce(this.yMin,this.yMax,n,n)}overlaps(t){return ce(this.xMin,this.xMax,t.xMin,t.xMax)&&ce(this.yMin,this.yMax,t.yMin,t.yMax)}addPoint(t){const[e,n]=t;return new he(Math.min(this.xMin,e),Math.min(this.yMin,n),Math.max(this.xMax,e),Math.max(this.yMax,n))}merge(t){return new he(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 he(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 De(i){return i.reduce((t,e)=>t.addPoint(e),new Zt)}const En=(i,t=1e-9)=>{let e=i;return Math.abs(i)<t&&(e=0),e.toFixed(-Math.log10(t))};function At(i,t=1e-9){return Array.from(new Map(i.map(([e,n])=>[`[${En(e,t)},${En(n,t)}]`,[e,n]])).values())}const $t=Math.PI/180,Ue=180/Math.PI,st=i=>`[${i[0]}, ${i[1]}]`,k=([i,t],[e,n],s=1e-9)=>Math.abs(i-e)<=s&&Math.abs(t-n)<=s,X=([i,t],[e,n])=>[i+e,t+n],q=([i,t],[e,n])=>[i-e,t-n],xt=([i,t])=>i*i+t*t,Qi=([i,t])=>Math.sqrt(xt([i,t])),W=([i,t],e)=>[i*e,t*e],Ft=([i,t],[e,n]=[0,0])=>(i-e)**2+(t-n)**2,J=(i,t=[0,0])=>Math.sqrt(Ft(i,t));function vt([i,t],[e,n]){return i*n-t*e}function fe([i,t],[e,n]){return i*e+t*n}function rt([i,t]){const e=J([i,t]);return[i/e,t/e]}function Et(i,t){const e=Math.cos(t)*i,n=Math.sin(t)*i;return[e,n]}function Gi([i,t]){return Math.atan2(t,i)}function Sn(i){const t=J(i),e=Gi(i);return[t,e]}function Ye(i,t,e=1e-9){const n=vt(i,t),s=xt(i),r=xt(t);return n*n<s*r*e*e}function tt(i){return[-i[1],i[0]]}function Kt(i){return[i[1],-i[0]]}function Ji(i,t,e){return k(e,i)||Ye(q(e,i),t)}const qt=(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]},Zi=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]},Ki=i=>{const[t,e,n,s,r,o,a,h,l]=i;return[t,s,a,e,r,h,n,o,l]};let ft=class Xi{constructor(t){_(this,"_matrix",[1,0,0,0,1,0,0,0,1]),t&&(this._matrix=[...t])}clone(){return new Xi(this._matrix)}transpose(){return this._matrix=Ki(this._matrix),this}inverse(){return this._matrix=Zi(this._matrix),this}translate(t,e){return this._matrix=qt([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=qt(r,this._matrix),e&&this.translate(e[0],e[1]),this}mirrorX(){return this._matrix=qt([1,0,0,0,-1,0,0,0,1],this._matrix),this}mirrorY(){return this._matrix=qt([-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=qt([-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=qt([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 _t{translateX(t){const e=new ft().translate(t,0);return this.transform(e)}translateY(t){const e=new ft().translate(0,t);return this.transform(e)}translate(t,e){const n=new ft().translate(t,e);return this.transform(n)}translateTo([t,e]){const n=new ft().translate(t,e);return this.transform(n)}rotate(t,e){const n=new ft().rotate(t*$t,e);return this.transform(n)}scale(t,e){const n=new ft().scale(t,e);return this.transform(n)}mirrorCenter(t){const e=new ft().mirrorCenter(t);return this.transform(e)}mirror(t="x",e){const n=new ft;return t==="x"?n.mirrorX():t==="y"?n.mirrorY():n.mirrorLine(t,e),this.transform(n)}}let te=class extends _t{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}},D=class Wt extends te{constructor(){super(...arguments),_(this,"segmentType","LINE"),_(this,"_V",null),_(this,"_slope",null),_(this,"_yIntercept",null),_(this,"_boundingBox",null)}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return X(this.firstPoint,W(this.V,t))}get length(){return J(this.firstPoint,this.lastPoint)}get squareLength(){return Ft(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 X(this.firstPoint,W(this.V,.5))}isSame(t){return t instanceof Wt?k(this.firstPoint,t.firstPoint)&&k(this.lastPoint,t.lastPoint)||k(this.lastPoint,t.firstPoint)&&k(this.firstPoint,t.lastPoint):!1}clone(){return new Wt(this.firstPoint,this.lastPoint)}reverse(){return new Wt(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new Zt(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=fe(e,this.V)/this.squareLength;if(n<0)return J(t,this.firstPoint);if(n>1)return J(t,this.lastPoint);const s=this.paramPoint(n);return J(t,s)}isOnSegment(t){if(k(t,this.firstPoint,this.precision))return!0;const e=q(t,this.firstPoint);if(!Ye(this.V,e))return!1;const n=fe(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=At(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 Wt(h,s[l+1]))}transform(t){return new Wt(t.transform(this.firstPoint),t.transform(this.lastPoint))}};function ts(i){return Array.from(Array(i).keys())}function yt(i){const t=Math.min(...i.map(e=>e.length));return ts(t).map(e=>i.map(n=>n[e]))}function Lt(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 me(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 ge=(i,t,e)=>{const n=vt(i.V,t.V),s=xt(i.V),r=xt(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=vt(a,t.V)/n,l=vt(a,i.V)/n;return{intersectionParam1:h,intersectionParam2:l}};function kn(i,t,e=!1,n){const s=ge(i,t,n);if(s==="parallel"){if(!e)return null;if(i.isSame(t))return i;const a=At([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 D(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 pe=(i,t)=>{const e=q(i,t);return Sn(e)};let z=class Ht extends te{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),this.center=n,this.clockwise=s,!r){if(k(t,e))throw new Error("Invalid arc, cannot be a full circle");if(Math.abs(this.radius-J(this.lastPoint,this.center))>this.precision)throw new Error(`Invalid arc, radius does not match between ${st(t)} and ${st(e)}} (center ${st(n)})`)}}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 me(this.firstAngle,Lt(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=me(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return X(this.center,Et(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,n]=pe(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=J(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=q(this.firstPoint,this.center);this._firstAngle=Lt(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=q(this.lastPoint,this.center);this._lastAngle=Lt(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!(t instanceof Ht)||!k(this.center,t.center)?!1:k(this.firstPoint,t.firstPoint)&&k(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||k(this.lastPoint,t.firstPoint)&&k(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new Ht(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new Ht(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 Zt(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]=pe(t,this.center);return this.isValidParameter(this.angleToParam(n))?Math.abs(e-this.radius):Math.sqrt(Math.min(Ft(t,this.firstPoint),Ft(t,this.lastPoint)))}isOnSegment(t){if(k(t,this.firstPoint)||k(t,this.lastPoint))return!0;const[e,n]=pe(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]=pe(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=Et(1,n);return(this.clockwise?Kt:tt)(rt(r))}get tangentAtFirstPoint(){const t=Et(1,this.firstAngle);return(this.clockwise?Kt:tt)(rt(t))}get tangentAtLastPoint(){const t=Et(1,this.lastAngle);return(this.clockwise?Kt: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(yt([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 Ht(s.get(l)||this.paramPoint(l),s.get(h)||this.paramPoint(h),this.center,this.clockwise);return r=null,c})}transform(t){return new Ht(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}};function es(i,t,e){const n=new D(t,i),s=new D(t,e),r=tt(n.tangentAtFirstPoint),o=tt(s.tangentAtLastPoint),a=ge({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=vt(q(i,t),q(e,t))>0;return new z(i,e,X(n.midPoint,W(r,a.intersectionParam1)),h,{ignoreChecks:!0})}function Rn(i,t,e){const n=new D(i,t),s=tt(n.tangentAtFirstPoint),r=ge({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=X(n.midPoint,W(s,r.intersectionParam1)),a=vt(q(o,i),q(o,X(i,e)))<0;return new z(i,t,o,a,{ignoreChecks:!0})}const Cn=1e-21,we=1.618034;function Tn(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+we*(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)<Cn?r=2*Cn: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+we*(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+we*(f-e),a=h,h=c,c=i(p),l+=1)):(p=f+we*(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 In=.381966,ns=1e-11;class is{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]=Tn(h);else if(l.length===2)[r,o,a,t,e,n,s]=Tn(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)+ns,b=2*P,S=.5*(m+w);if(Math.abs(o-S)<b-.5*(w-m))break;if(Math.abs(g)<=P)o>=S?g=m-o:g=w-o,M=In*g;else{const V=(o-a)*(u-c);let v=(o-h)*(u-l),R=(o-h)*v-(o-a)*V;v=2*(v-V),v>0&&(R=-R),v=Math.abs(v);const j=g;g=M,R>v*(m-o)&&R<v*(w-o)&&Math.abs(R)<Math.abs(.5*v*j)?(M=R*1/v,f=o+M,(f-m<b||w-f<b)&&(S-o>=0?M=P:M=-P)):(o>=S?g=m-o:g=w-o,M=In*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 $n(i,t=1e-8,e=1e3){const n=new is(i,t,e);return n.setBracket([0,1]),n.run()}function Fn(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 N={};const ss=Object.prototype.toString;function ee(i){const t=ss.call(i);return t.endsWith("Array]")&&!t.includes("Big")}const rs=Object.freeze(Object.defineProperty({__proto__:null,isAnyArray:ee},Symbol.toStringTag,{value:"Module"})),os=Fn(rs);function as(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!ee(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 hs(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!ee(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 ls(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(ee(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(!ee(t.output))throw new TypeError("output option must be an array if specified");e=t.output}else e=new Array(i.length);var n=hs(i),s=as(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 us=Object.freeze(Object.defineProperty({__proto__:null,default:ls},Symbol.toStringTag,{value:"Module"})),cs=Fn(us);Object.defineProperty(N,"__esModule",{value:!0});var mt=os,_n=cs;const de=" ".repeat(2),Ln=" ".repeat(4);function fs(){return Bn(this)}function Bn(i,t={}){const{maxRows:e=15,maxColumns:n=10,maxNumSize:s=8,padMinus:r="auto"}=t;return`${i.constructor.name} {
|
|
2
|
-
${
|
|
3
|
-
${
|
|
4
|
-
${
|
|
5
|
-
${
|
|
6
|
-
${
|
|
7
|
-
}`}function
|
|
8
|
-
${Ln}`)}function gs(i,t,e){return(i>=0&&e?` ${Vn(i,t-1)}`:Vn(i,t)).padEnd(t)}function Vn(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 ps(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 dt(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 Pt(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 Nt(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 Ot(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 Xe(i,t){if(!mt.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 We(i,t){if(!mt.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 He(i,t,e,n,s){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Me("startRow",t),Me("endRow",e),Me("startColumn",n),Me("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 Pe(i,t=0){let e=[];for(let n=0;n<i;n++)e.push(t);return e}function Me(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 ws(i){let t=Pe(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 ds(i){let t=Pe(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 Ps(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 Ms(i){let t=Pe(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 ys(i){let t=Pe(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 bs(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 xs(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 vs(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 As(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 Es(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 Ss(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 ks(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 Rs(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 Cs(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 Ts(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 Is(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 $s(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 Fs(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)}let Z=class at{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 at.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){dt(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){dt(this,t),e=Nt(this,e);for(let n=0;n<this.columns;n++)this.set(t,n,e[n]);return this}swapRows(t,e){dt(this,t),dt(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){Pt(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){Pt(this,t),e=Ot(this,e);for(let n=0;n<this.rows;n++)this.set(n,t,e[n]);return this}swapColumns(t,e){Pt(this,t),Pt(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=Nt(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=Nt(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=Nt(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=Nt(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=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[e]);return this}subColumnVector(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[e]);return this}mulColumnVector(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[e]);return this}divColumnVector(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[e]);return this}mulRow(t,e){dt(this,t);for(let n=0;n<this.columns;n++)this.set(t,n,this.get(t,n)*e);return this}mulColumn(t,e){Pt(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(dt(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){dt(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(dt(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){dt(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(Pt(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){Pt(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(Pt(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){Pt(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){at.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,S=m+g,C=u-m+w+M;return e.set(0,0,P),e.set(0,1,b),e.set(1,0,S),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),S=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+S),R=(-n+o+a)*(m-w+M),j=(o+a)*(-m+w),d=n*m,x=(-n+l+c)*(m-g+f),A=(-n+l)*(g-f),y=(l+c)*(-m+g),I=(n+s+r-a-h-l-c)*f,$=c*(-m+g+p-M-f-P+b),F=(-r+c+u)*(M+P-b),H=(r-u)*(M-b),L=r*P,O=(c+u)*(-P+b),B=(-r+a+h)*(f+P-S),Y=(r-h)*(f-S),Q=(a+h)*(-P+S),T=s*p,U=h*b,nt=o*g,K=l*w,G=u*S,Pn=d+L+T,Mn=C+R+j+d+F+L+O,yn=d+x+y+I+L+B+Q,bn=V+v+R+d+L+B+Y,Le=V+R+j+d+U,Be=L+B+Y+Q+nt,Ve=d+x+A+$+F+H+L,je=F+H+L+O+K,qe=d+x+A+y+G;return e.set(0,0,Pn),e.set(0,1,Mn),e.set(0,2,yn),e.set(1,0,bn),e.set(1,1,Le),e.set(1,2,Be),e.set(2,0,Ve),e.set(2,1,je),e.set(2,2,qe),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.`);function a(u,m,w){let g=u.rows,p=u.columns;if(g===m&&p===w)return u;{let M=at.zeros(m,w);return M=M.setSubMatrix(u,0,0),M}}let h=Math.max(n,r),l=Math.max(s,o);e=a(e,h,l),t=a(t,h,l);function c(u,m,w,g){if(w<=512||g<=512)return u.mmul(m);w%2===1&&g%2===1?(u=a(u,w+1,g+1),m=a(m,w+1,g+1)):w%2===1?(u=a(u,w+1,g),m=a(m,w+1,g)):g%2===1&&(u=a(u,w,g+1),m=a(m,w,g+1));let p=parseInt(u.rows/2,10),M=parseInt(u.columns/2,10),f=u.subMatrix(0,p-1,0,M-1),P=m.subMatrix(0,p-1,0,M-1),b=u.subMatrix(0,p-1,M,u.columns-1),S=m.subMatrix(0,p-1,M,m.columns-1),C=u.subMatrix(p,u.rows-1,0,M-1),V=m.subMatrix(p,m.rows-1,0,M-1),v=u.subMatrix(p,u.rows-1,M,u.columns-1),R=m.subMatrix(p,m.rows-1,M,m.columns-1),j=c(at.add(f,v),at.add(P,R),p,M),d=c(at.add(C,v),P,p,M),x=c(f,at.sub(S,R),p,M),A=c(v,at.sub(V,P),p,M),y=c(at.add(f,b),R,p,M),I=c(at.sub(C,f),at.add(P,S),p,M),$=c(at.sub(b,v),at.add(V,R),p,M),F=at.add(j,A);F.sub(y),F.add($);let H=at.add(x,y),L=at.add(d,A),O=at.sub(j,d);O.add(x),O.add(I);let B=at.zeros(2*F.rows,2*F.columns);return B=B.setSubMatrix(F,0,0),B=B.setSubMatrix(H,F.rows,0),B=B.setSubMatrix(L,0,F.columns),B=B.setSubMatrix(O,F.rows,F.columns),B.subMatrix(0,w-1,0,g-1)}return c(e,t,h,l)}scaleRows(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let r=0;r<this.rows;r++){const o=this.getRow(r);o.length>0&&_n(o,{min:e,max:n,output:o}),s.setRow(r,o)}return s}scaleColumns(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let r=0;r<this.columns;r++){const o=this.getColumn(r);o.length&&_n(o,{min:e,max:n,output:o}),s.setColumn(r,o)}return s}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let n=0;n<t;n++){let s=this.get(e,n),r=this.get(e,this.columns-1-n);this.set(e,n,r),this.set(e,this.columns-1-n,s)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let n=0;n<t;n++){let s=this.get(n,e),r=this.get(this.rows-1-n,e);this.set(n,e,r),this.set(this.rows-1-n,e,s)}return this}kroneckerProduct(t){t=E.checkMatrix(t);let e=this.rows,n=this.columns,s=t.rows,r=t.columns,o=new E(e*s,n*r);for(let a=0;a<e;a++)for(let h=0;h<n;h++)for(let l=0;l<s;l++)for(let c=0;c<r;c++)o.set(s*a+l,r*h+c,this.get(a,h)*t.get(l,c));return o}kroneckerSum(t){if(t=E.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,n=t.rows,s=this.kroneckerProduct(E.eye(n,n)),r=E.eye(e,e).kroneckerProduct(t);return s.add(r)}transpose(){let t=new E(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.set(n,e,this.get(e,n));return t}sortRows(t=jn){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=jn){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,n,s){He(this,t,e,n,s);let r=new E(e-t+1,s-n+1);for(let o=t;o<=e;o++)for(let a=n;a<=s;a++)r.set(o-t,a-n,this.get(o,a));return r}subMatrixRow(t,e,n){if(e===void 0&&(e=0),n===void 0&&(n=this.columns-1),e>n||e<0||e>=this.columns||n<0||n>=this.columns)throw new RangeError("Argument out of range");let s=new E(t.length,n-e+1);for(let r=0;r<t.length;r++)for(let o=e;o<=n;o++){if(t[r]<0||t[r]>=this.rows)throw new RangeError(`Row index out of range: ${t[r]}`);s.set(r,o-e,this.get(t[r],o))}return s}subMatrixColumn(t,e,n){if(e===void 0&&(e=0),n===void 0&&(n=this.rows-1),e>n||e<0||e>=this.rows||n<0||n>=this.rows)throw new RangeError("Argument out of range");let s=new E(n-e+1,t.length);for(let r=0;r<t.length;r++)for(let o=e;o<=n;o++){if(t[r]<0||t[r]>=this.columns)throw new RangeError(`Column index out of range: ${t[r]}`);s.set(o-e,r,this.get(o,t[r]))}return s}setSubMatrix(t,e,n){if(t=E.checkMatrix(t),t.isEmpty())return this;let s=e+t.rows-1,r=n+t.columns-1;He(this,e,s,n,r);for(let o=0;o<t.rows;o++)for(let a=0;a<t.columns;a++)this.set(e+o,n+a,t.get(o,a));return this}selection(t,e){Xe(this,t),We(this,e);let n=new E(t.length,e.length);for(let s=0;s<t.length;s++){let r=t[s];for(let o=0;o<e.length;o++){let a=e[o];n.set(s,o,this.get(r,a))}}return n}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let n=0;n<t;n++)e+=this.get(n,n);return e}clone(){return this.constructor.copy(this,new E(this.rows,this.columns))}static copy(t,e){for(const[n,s,r]of t.entries())e.set(n,s,r);return e}sum(t){switch(t){case"row":return ws(this);case"column":return ds(this);case void 0:return Ps(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return Ms(this);case"column":return ys(this);case void 0:return bs(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":{for(let n=0;n<this.rows;n++)e[n]/=this.columns;return e}case"column":{for(let n=0;n<this.columns;n++)e[n]/=this.rows;return e}case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{unbiased:n=!0,mean:s=this.mean(t)}=e;if(typeof n!="boolean")throw new TypeError("unbiased must be a boolean");switch(t){case"row":{if(!mt.isAnyArray(s))throw new TypeError("mean must be an array");return xs(this,n,s)}case"column":{if(!mt.isAnyArray(s))throw new TypeError("mean must be an array");return vs(this,n,s)}case void 0:{if(typeof s!="number")throw new TypeError("mean must be a number");return As(this,n,s)}default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){typeof t=="object"&&(e=t,t=void 0);const n=this.variance(t,e);if(t===void 0)return Math.sqrt(n);for(let s=0;s<n.length;s++)n[s]=Math.sqrt(n[s]);return n}center(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{center:n=this.mean(t)}=e;switch(t){case"row":{if(!mt.isAnyArray(n))throw new TypeError("center must be an array");return Es(this,n),this}case"column":{if(!mt.isAnyArray(n))throw new TypeError("center must be an array");return Ss(this,n),this}case void 0:{if(typeof n!="number")throw new TypeError("center must be a number");return ks(this,n),this}default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");let n=e.scale;switch(t){case"row":{if(n===void 0)n=Rs(this);else if(!mt.isAnyArray(n))throw new TypeError("scale must be an array");return Cs(this,n),this}case"column":{if(n===void 0)n=Ts(this);else if(!mt.isAnyArray(n))throw new TypeError("scale must be an array");return Is(this,n),this}case void 0:{if(n===void 0)n=$s(this);else if(typeof n!="number")throw new TypeError("scale must be a number");return Fs(this,n),this}default:throw new Error(`invalid option: ${t}`)}}toString(t){return Bn(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}};Z.prototype.klass="Matrix",typeof Symbol<"u"&&(Z.prototype[Symbol.for("nodejs.util.inspect.custom")]=fs);function jn(i,t){return i-t}function _s(i){return i.every(t=>typeof t=="number")}Z.random=Z.rand,Z.randomInt=Z.randInt,Z.diagonal=Z.diag,Z.prototype.diagonal=Z.prototype.diag,Z.identity=Z.eye,Z.prototype.negate=Z.prototype.neg,Z.prototype.tensorProduct=Z.prototype.kroneckerProduct;var ye,Qe,be;let E=(be=class extends Z{constructor(i,t){if(super(),vn(this,ye),_(this,"data"),be.isMatrix(i))An(this,ye,Qe).call(this,i.rows,i.columns),be.copy(i,this);else if(Number.isInteger(i)&&i>=0)An(this,ye,Qe).call(this,i,t);else if(mt.isAnyArray(i)){const e=i;if(i=e.length,t=i?e[0].length:0,typeof t!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<i;n++){if(e[n].length!==t)throw new RangeError("Inconsistent array dimensions");if(!_s(e[n]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(e[n]))}this.rows=i,this.columns=t}else throw new TypeError("First argument must be a positive number or an array")}set(i,t,e){return this.data[i][t]=e,this}get(i,t){return this.data[i][t]}removeRow(i){return dt(this,i),this.data.splice(i,1),this.rows-=1,this}addRow(i,t){return t===void 0&&(t=i,i=this.rows),dt(this,i,!0),t=Float64Array.from(Nt(this,t)),this.data.splice(i,0,t),this.rows+=1,this}removeColumn(i){Pt(this,i);for(let t=0;t<this.rows;t++){const e=new Float64Array(this.columns-1);for(let n=0;n<i;n++)e[n]=this.data[t][n];for(let n=i+1;n<this.columns;n++)e[n-1]=this.data[t][n];this.data[t]=e}return this.columns-=1,this}addColumn(i,t){typeof t>"u"&&(t=i,i=this.columns),Pt(this,i,!0),t=Ot(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns+1);let s=0;for(;s<i;s++)n[s]=this.data[e][s];for(n[s++]=t[e];s<this.columns+1;s++)n[s]=this.data[e][s-1];this.data[e]=n}return this.columns+=1,this}},ye=new WeakSet,Qe=function(i,t){if(this.data=[],Number.isInteger(t)&&t>=0)for(let e=0;e<i;e++)this.data.push(new Float64Array(t));else throw new TypeError("nColumns must be a positive integer");this.rows=i,this.columns=t},be);ps(Z,E);var lt;const Ls=class xn extends Z{constructor(t){if(super(),vn(this,lt),E.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");ze(this,lt,E.copy(t,new E(t.rows,t.rows)))}else if(Number.isInteger(t)&&t>=0)ze(this,lt,new E(t,t));else if(ze(this,lt,new E(t)),!this.isSymmetric())throw new TypeError("not symmetric data")}get size(){return bt(this,lt).size}get rows(){return bt(this,lt).rows}get columns(){return bt(this,lt).columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return E.isMatrix(t)&&t.klassType==="SymmetricMatrix"}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}clone(){const t=new xn(this.diagonalSize);for(const[e,n,s]of this.upperRightEntries())t.set(e,n,s);return t}toMatrix(){return new E(this)}get(t,e){return bt(this,lt).get(t,e)}set(t,e,n){return bt(this,lt).set(t,e,n),bt(this,lt).set(e,t,n),this}removeCross(t){return bt(this,lt).removeRow(t),bt(this,lt).removeColumn(t),this}addCross(t,e){e===void 0&&(e=t,t=this.diagonalSize);const n=e.slice();return n.splice(t,1),bt(this,lt).addRow(t,n),bt(this,lt).addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[n,s]of t.entries())s||e.push(n);e.reverse();for(const n of e)this.removeCross(n);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let n=0,s=0,r=0;r<e.length;r++)e[r]=this.get(s,n),++n>=t&&(n=++s);return e}static fromCompact(t){const e=t.length,n=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const s=new xn(n);for(let r=0,o=0,a=0;a<e;a++)s.set(r,o,t[a]),++r>=n&&(r=++o);return s}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const n=this.get(t,e);yield[t,e,n],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0)yield this.get(t,e),++e>=this.diagonalSize&&(e=++t)}};lt=new WeakMap;let ne=Ls;ne.prototype.klassType="SymmetricMatrix";let qn=class Wi extends ne{static isDistanceMatrix(t){return ne.isSymmetricMatrix(t)&&t.klassSubType==="DistanceMatrix"}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,n){return t===e&&(n=0),super.set(t,e,n)}addCross(t,e){return e===void 0&&(e=t,t=this.diagonalSize),e=e.slice(),e[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new ne(this)}clone(){const t=new Wi(this.diagonalSize);for(const[e,n,s]of this.upperRightEntries())e!==n&&t.set(e,n,s);return t}toCompact(){const{diagonalSize:t}=this,e=(t-1)*t/2,n=new Array(e);for(let s=1,r=0,o=0;o<n.length;o++)n[o]=this.get(r,s),++s>=t&&(s=++r+1);return n}static fromCompact(t){const e=t.length;if(e===0)return new this(0);const n=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const s=new this(n);for(let r=1,o=0,a=0;a<e;a++)s.set(r,o,t[a]),++r>=n&&(r=++o+1);return s}};qn.prototype.klassSubType="DistanceMatrix";let St=class extends Z{constructor(t,e,n){super(),this.matrix=t,this.rows=e,this.columns=n}};class Bs extends St{constructor(t,e){Pt(t,e),super(t,t.rows,1),this.column=e}set(t,e,n){return this.matrix.set(t,this.column,n),this}get(t){return this.matrix.get(t,this.column)}}class Vs extends St{constructor(t,e){We(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,n){return this.matrix.set(t,this.columnIndices[e],n),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}}class js extends St{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(t,this.columns-e-1,n),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}}class qs extends St{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(this.rows-t-1,e,n),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}}class Ns extends St{constructor(t,e){dt(t,e),super(t,1,t.columns),this.row=e}set(t,e,n){return this.matrix.set(this.row,e,n),this}get(t,e){return this.matrix.get(this.row,e)}}class Os extends St{constructor(t,e){Xe(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,n){return this.matrix.set(this.rowIndices[t],e,n),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}}let xe=class extends St{constructor(t,e,n){Xe(t,e),We(t,n),super(t,e.length,n.length),this.rowIndices=e,this.columnIndices=n}set(t,e,n){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],n),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}};class zs extends St{constructor(t,e,n,s,r){He(t,e,n,s,r),super(t,n-e+1,r-s+1),this.startRow=e,this.startColumn=s}set(t,e,n){return this.matrix.set(this.startRow+t,this.startColumn+e,n),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}}class Ds extends St{constructor(t){super(t,t.columns,t.rows)}set(t,e,n){return this.matrix.set(e,t,n),this}get(t,e){return this.matrix.get(e,t)}}class Nn extends Z{constructor(t,e={}){const{rows:n=1}=e;if(t.length%n!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=n,this.columns=t.length/n,this.data=t}set(t,e,n){let s=this._calculateIndex(t,e);return this.data[s]=n,this}get(t,e){let n=this._calculateIndex(t,e);return this.data[n]}_calculateIndex(t,e){return t*this.columns+e}}let pt=class extends Z{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}};function Us(i,t){if(mt.isAnyArray(i))return i[0]&&mt.isAnyArray(i[0])?new pt(i):new Nn(i,t);throw new Error("the argument is not an array")}let ve=class{constructor(t){t=pt.checkMatrix(t);let e=t.clone(),n=e.rows,s=e.columns,r=new Float64Array(n),o=1,a,h,l,c,u,m,w,g,p;for(a=0;a<n;a++)r[a]=a;for(g=new Float64Array(n),h=0;h<s;h++){for(a=0;a<n;a++)g[a]=e.get(a,h);for(a=0;a<n;a++){for(p=Math.min(a,h),u=0,l=0;l<p;l++)u+=e.get(a,l)*g[l];g[a]-=u,e.set(a,h,g[a])}for(c=h,a=h+1;a<n;a++)Math.abs(g[a])>Math.abs(g[c])&&(c=a);if(c!==h){for(l=0;l<s;l++)m=e.get(c,l),e.set(c,l,e.get(h,l)),e.set(h,l,m);w=r[c],r[c]=r[h],r[h]=w,o=-o}if(h<n&&e.get(h,h)!==0)for(a=h+1;a<n;a++)e.set(a,h,e.get(a,h)/e.get(h,h))}this.LU=e,this.pivotVector=r,this.pivotSign=o}isSingular(){let t=this.LU,e=t.columns;for(let n=0;n<e;n++)if(t.get(n,n)===0)return!0;return!1}solve(t){t=E.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let n=t.columns,s=t.subMatrixRow(this.pivotVector,0,n-1),r=e.columns,o,a,h;for(h=0;h<r;h++)for(o=h+1;o<r;o++)for(a=0;a<n;a++)s.set(o,a,s.get(o,a)-s.get(h,a)*e.get(o,h));for(h=r-1;h>=0;h--){for(a=0;a<n;a++)s.set(h,a,s.get(h,a)/e.get(h,h));for(o=0;o<h;o++)for(a=0;a<n;a++)s.set(o,a,s.get(o,a)-s.get(h,a)*e.get(o,h))}return s}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,n=t.columns;for(let s=0;s<n;s++)e*=t.get(s,s);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,s=new E(e,n);for(let r=0;r<e;r++)for(let o=0;o<n;o++)r>o?s.set(r,o,t.get(r,o)):r===o?s.set(r,o,1):s.set(r,o,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,s=new E(e,n);for(let r=0;r<e;r++)for(let o=0;o<n;o++)r<=o?s.set(r,o,t.get(r,o)):s.set(r,o,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}};function kt(i,t){let e=0;return Math.abs(i)>Math.abs(t)?(e=t/i,Math.abs(i)*Math.sqrt(1+e*e)):t!==0?(e=i/t,Math.abs(t)*Math.sqrt(1+e*e)):0}class Ge{constructor(t){t=pt.checkMatrix(t);let e=t.clone(),n=t.rows,s=t.columns,r=new Float64Array(s),o,a,h,l;for(h=0;h<s;h++){let c=0;for(o=h;o<n;o++)c=kt(c,e.get(o,h));if(c!==0){for(e.get(h,h)<0&&(c=-c),o=h;o<n;o++)e.set(o,h,e.get(o,h)/c);for(e.set(h,h,e.get(h,h)+1),a=h+1;a<s;a++){for(l=0,o=h;o<n;o++)l+=e.get(o,h)*e.get(o,a);for(l=-l/e.get(h,h),o=h;o<n;o++)e.set(o,a,e.get(o,a)+l*e.get(o,h))}}r[h]=-c}this.QR=e,this.Rdiag=r}solve(t){t=E.checkMatrix(t);let e=this.QR,n=e.rows;if(t.rows!==n)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let s=t.columns,r=t.clone(),o=e.columns,a,h,l,c;for(l=0;l<o;l++)for(h=0;h<s;h++){for(c=0,a=l;a<n;a++)c+=e.get(a,l)*r.get(a,h);for(c=-c/e.get(l,l),a=l;a<n;a++)r.set(a,h,r.get(a,h)+c*e.get(a,l))}for(l=o-1;l>=0;l--){for(h=0;h<s;h++)r.set(l,h,r.get(l,h)/this.Rdiag[l]);for(a=0;a<l;a++)for(h=0;h<s;h++)r.set(a,h,r.get(a,h)-r.get(l,h)*e.get(a,l))}return r.subMatrix(0,o-1,0,s-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(this.Rdiag[e]===0)return!1;return!0}get upperTriangularMatrix(){let t=this.QR,e=t.columns,n=new E(e,e),s,r;for(s=0;s<e;s++)for(r=0;r<e;r++)s<r?n.set(s,r,t.get(s,r)):s===r?n.set(s,r,this.Rdiag[s]):n.set(s,r,0);return n}get orthogonalMatrix(){let t=this.QR,e=t.rows,n=t.columns,s=new E(e,n),r,o,a,h;for(a=n-1;a>=0;a--){for(r=0;r<e;r++)s.set(r,a,0);for(s.set(a,a,1),o=a;o<n;o++)if(t.get(a,a)!==0){for(h=0,r=a;r<e;r++)h+=t.get(r,a)*s.get(r,o);for(h=-h/t.get(a,a),r=a;r<e;r++)s.set(r,o,s.get(r,o)+h*t.get(r,a))}}return s}}let Dt=class{constructor(t,e={}){if(t=pt.checkMatrix(t),t.isEmpty())throw new Error("Matrix must be non-empty");let n=t.rows,s=t.columns;const{computeLeftSingularVectors:r=!0,computeRightSingularVectors:o=!0,autoTranspose:a=!1}=e;let h=!!r,l=!!o,c=!1,u;if(n<s)if(!a)u=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else{u=t.transpose(),n=u.rows,s=u.columns,c=!0;let d=h;h=l,l=d}else u=t.clone();let m=Math.min(n,s),w=Math.min(n+1,s),g=new Float64Array(w),p=new E(n,m),M=new E(s,s),f=new Float64Array(s),P=new Float64Array(n),b=new Float64Array(w);for(let d=0;d<w;d++)b[d]=d;let S=Math.min(n-1,s),C=Math.max(0,Math.min(s-2,n)),V=Math.max(S,C);for(let d=0;d<V;d++){if(d<S){g[d]=0;for(let x=d;x<n;x++)g[d]=kt(g[d],u.get(x,d));if(g[d]!==0){u.get(d,d)<0&&(g[d]=-g[d]);for(let x=d;x<n;x++)u.set(x,d,u.get(x,d)/g[d]);u.set(d,d,u.get(d,d)+1)}g[d]=-g[d]}for(let x=d+1;x<s;x++){if(d<S&&g[d]!==0){let A=0;for(let y=d;y<n;y++)A+=u.get(y,d)*u.get(y,x);A=-A/u.get(d,d);for(let y=d;y<n;y++)u.set(y,x,u.get(y,x)+A*u.get(y,d))}f[x]=u.get(d,x)}if(h&&d<S)for(let x=d;x<n;x++)p.set(x,d,u.get(x,d));if(d<C){f[d]=0;for(let x=d+1;x<s;x++)f[d]=kt(f[d],f[x]);if(f[d]!==0){f[d+1]<0&&(f[d]=0-f[d]);for(let x=d+1;x<s;x++)f[x]/=f[d];f[d+1]+=1}if(f[d]=-f[d],d+1<n&&f[d]!==0){for(let x=d+1;x<n;x++)P[x]=0;for(let x=d+1;x<n;x++)for(let A=d+1;A<s;A++)P[x]+=f[A]*u.get(x,A);for(let x=d+1;x<s;x++){let A=-f[x]/f[d+1];for(let y=d+1;y<n;y++)u.set(y,x,u.get(y,x)+A*P[y])}}if(l)for(let x=d+1;x<s;x++)M.set(x,d,f[x])}}let v=Math.min(s,n+1);if(S<s&&(g[S]=u.get(S,S)),n<v&&(g[v-1]=0),C+1<v&&(f[C]=u.get(C,v-1)),f[v-1]=0,h){for(let d=S;d<m;d++){for(let x=0;x<n;x++)p.set(x,d,0);p.set(d,d,1)}for(let d=S-1;d>=0;d--)if(g[d]!==0){for(let x=d+1;x<m;x++){let A=0;for(let y=d;y<n;y++)A+=p.get(y,d)*p.get(y,x);A=-A/p.get(d,d);for(let y=d;y<n;y++)p.set(y,x,p.get(y,x)+A*p.get(y,d))}for(let x=d;x<n;x++)p.set(x,d,-p.get(x,d));p.set(d,d,1+p.get(d,d));for(let x=0;x<d-1;x++)p.set(x,d,0)}else{for(let x=0;x<n;x++)p.set(x,d,0);p.set(d,d,1)}}if(l)for(let d=s-1;d>=0;d--){if(d<C&&f[d]!==0)for(let x=d+1;x<s;x++){let A=0;for(let y=d+1;y<s;y++)A+=M.get(y,d)*M.get(y,x);A=-A/M.get(d+1,d);for(let y=d+1;y<s;y++)M.set(y,x,M.get(y,x)+A*M.get(y,d))}for(let x=0;x<s;x++)M.set(x,d,0);M.set(d,d,1)}let R=v-1,j=Number.EPSILON;for(;v>0;){let d,x;for(d=v-2;d>=-1&&d!==-1;d--){const A=Number.MIN_VALUE+j*Math.abs(g[d]+Math.abs(g[d+1]));if(Math.abs(f[d])<=A||Number.isNaN(f[d])){f[d]=0;break}}if(d===v-2)x=4;else{let A;for(A=v-1;A>=d&&A!==d;A--){let y=(A!==v?Math.abs(f[A]):0)+(A!==d+1?Math.abs(f[A-1]):0);if(Math.abs(g[A])<=j*y){g[A]=0;break}}A===d?x=3:A===v-1?x=1:(x=2,d=A)}switch(d++,x){case 1:{let A=f[v-2];f[v-2]=0;for(let y=v-2;y>=d;y--){let I=kt(g[y],A),$=g[y]/I,F=A/I;if(g[y]=I,y!==d&&(A=-F*f[y-1],f[y-1]=$*f[y-1]),l)for(let H=0;H<s;H++)I=$*M.get(H,y)+F*M.get(H,v-1),M.set(H,v-1,-F*M.get(H,y)+$*M.get(H,v-1)),M.set(H,y,I)}break}case 2:{let A=f[d-1];f[d-1]=0;for(let y=d;y<v;y++){let I=kt(g[y],A),$=g[y]/I,F=A/I;if(g[y]=I,A=-F*f[y],f[y]=$*f[y],h)for(let H=0;H<n;H++)I=$*p.get(H,y)+F*p.get(H,d-1),p.set(H,d-1,-F*p.get(H,y)+$*p.get(H,d-1)),p.set(H,y,I)}break}case 3:{const A=Math.max(Math.abs(g[v-1]),Math.abs(g[v-2]),Math.abs(f[v-2]),Math.abs(g[d]),Math.abs(f[d])),y=g[v-1]/A,I=g[v-2]/A,$=f[v-2]/A,F=g[d]/A,H=f[d]/A,L=((I+y)*(I-y)+$*$)/2,O=y*$*(y*$);let B=0;(L!==0||O!==0)&&(L<0?B=0-Math.sqrt(L*L+O):B=Math.sqrt(L*L+O),B=O/(L+B));let Y=(F+y)*(F-y)+B,Q=F*H;for(let T=d;T<v-1;T++){let U=kt(Y,Q);U===0&&(U=Number.MIN_VALUE);let nt=Y/U,K=Q/U;if(T!==d&&(f[T-1]=U),Y=nt*g[T]+K*f[T],f[T]=nt*f[T]-K*g[T],Q=K*g[T+1],g[T+1]=nt*g[T+1],l)for(let G=0;G<s;G++)U=nt*M.get(G,T)+K*M.get(G,T+1),M.set(G,T+1,-K*M.get(G,T)+nt*M.get(G,T+1)),M.set(G,T,U);if(U=kt(Y,Q),U===0&&(U=Number.MIN_VALUE),nt=Y/U,K=Q/U,g[T]=U,Y=nt*f[T]+K*g[T+1],g[T+1]=-K*f[T]+nt*g[T+1],Q=K*f[T+1],f[T+1]=nt*f[T+1],h&&T<n-1)for(let G=0;G<n;G++)U=nt*p.get(G,T)+K*p.get(G,T+1),p.set(G,T+1,-K*p.get(G,T)+nt*p.get(G,T+1)),p.set(G,T,U)}f[v-2]=Y;break}case 4:{if(g[d]<=0&&(g[d]=g[d]<0?-g[d]:0,l))for(let A=0;A<=R;A++)M.set(A,d,-M.get(A,d));for(;d<R&&!(g[d]>=g[d+1]);){let A=g[d];if(g[d]=g[d+1],g[d+1]=A,l&&d<s-1)for(let y=0;y<s;y++)A=M.get(y,d+1),M.set(y,d+1,M.get(y,d)),M.set(y,d,A);if(h&&d<n-1)for(let y=0;y<n;y++)A=p.get(y,d+1),p.set(y,d+1,p.get(y,d)),p.set(y,d,A);d++}v--;break}}}if(c){let d=M;M=p,p=d}this.m=n,this.n=s,this.s=g,this.U=p,this.V=M}solve(t){let e=t,n=this.threshold,s=this.s.length,r=E.zeros(s,s);for(let m=0;m<s;m++)Math.abs(this.s[m])<=n?r.set(m,m,0):r.set(m,m,1/this.s[m]);let o=this.U,a=this.rightSingularVectors,h=a.mmul(r),l=a.rows,c=o.rows,u=E.zeros(l,c);for(let m=0;m<l;m++)for(let w=0;w<c;w++){let g=0;for(let p=0;p<s;p++)g+=h.get(m,p)*o.get(w,p);u.set(m,w,g)}return u.mmul(e)}solveForDiagonal(t){return this.solve(E.diag(t))}inverse(){let t=this.V,e=this.threshold,n=t.rows,s=t.columns,r=new E(n,this.s.length);for(let c=0;c<n;c++)for(let u=0;u<s;u++)Math.abs(this.s[u])>e&&r.set(c,u,t.get(c,u)/this.s[u]);let o=this.U,a=o.rows,h=o.columns,l=new E(n,a);for(let c=0;c<n;c++)for(let u=0;u<a;u++){let m=0;for(let w=0;w<h;w++)m+=r.get(c,w)*o.get(u,w);l.set(c,u,m)}return l}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,n=this.s;for(let s=0,r=n.length;s<r;s++)n[s]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return E.diag(this.s)}};function Ys(i,t=!1){return i=pt.checkMatrix(i),t?new Dt(i).inverse():On(i,E.eye(i.rows))}function On(i,t,e=!1){return i=pt.checkMatrix(i),t=pt.checkMatrix(t),e?new Dt(i).solve(t):i.isSquare()?new ve(i).solve(t):new Ge(i).solve(t)}function Ae(i){if(i=E.checkMatrix(i),i.isSquare()){if(i.columns===0)return 1;let t,e,n,s;if(i.columns===2)return t=i.get(0,0),e=i.get(0,1),n=i.get(1,0),s=i.get(1,1),t*s-e*n;if(i.columns===3){let r,o,a;return r=new xe(i,[1,2],[1,2]),o=new xe(i,[1,2],[0,2]),a=new xe(i,[1,2],[0,1]),t=i.get(0,0),e=i.get(0,1),n=i.get(0,2),t*Ae(r)-e*Ae(o)+n*Ae(a)}else return new ve(i).determinant}else throw Error("determinant can only be calculated for a square matrix")}function Xs(i,t){let e=[];for(let n=0;n<i;n++)n!==t&&e.push(n);return e}function Ws(i,t,e,n=1e-9,s=1e-9){if(i>s)return new Array(t.rows+1).fill(0);{let r=t.addRow(e,[0]);for(let o=0;o<r.rows;o++)Math.abs(r.get(o,0))<n&&r.set(o,0,0);return r.to1DArray()}}function Hs(i,t={}){const{thresholdValue:e=1e-9,thresholdError:n=1e-9}=t;i=E.checkMatrix(i);let s=i.rows,r=new E(s,s);for(let o=0;o<s;o++){let a=E.columnVector(i.getRow(o)),h=i.subMatrixRow(Xs(s,o)).transpose(),l=new Dt(h).solve(a),c=E.sub(a,h.mmul(l)).abs().max();r.setRow(o,Ws(c,l,o,e,n))}return r}function Qs(i,t=Number.EPSILON){if(i=E.checkMatrix(i),i.isEmpty())return i.transpose();let e=new Dt(i,{autoTranspose:!0}),n=e.leftSingularVectors,s=e.rightSingularVectors,r=e.diagonal;for(let o=0;o<r.length;o++)Math.abs(r[o])>t?r[o]=1/r[o]:r[o]=0;return s.mmul(E.diag(r).mmul(n.transpose()))}function Gs(i,t=i,e={}){i=new E(i);let n=!1;if(typeof t=="object"&&!E.isMatrix(t)&&!mt.isAnyArray(t)?(e=t,t=i,n=!0):t=new E(t),i.rows!==t.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:s=!0}=e;s&&(i=i.center("column"),n||(t=t.center("column")));const r=i.transpose().mmul(t);for(let o=0;o<r.rows;o++)for(let a=0;a<r.columns;a++)r.set(o,a,r.get(o,a)*(1/(i.rows-1)));return r}function Js(i,t=i,e={}){i=new E(i);let n=!1;if(typeof t=="object"&&!E.isMatrix(t)&&!mt.isAnyArray(t)?(e=t,t=i,n=!0):t=new E(t),i.rows!==t.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:s=!0,scale:r=!0}=e;s&&(i.center("column"),n||t.center("column")),r&&(i.scale("column"),n||t.scale("column"));const o=i.standardDeviation("column",{unbiased:!0}),a=n?o:t.standardDeviation("column",{unbiased:!0}),h=i.transpose().mmul(t);for(let l=0;l<h.rows;l++)for(let c=0;c<h.columns;c++)h.set(l,c,h.get(l,c)*(1/(o[l]*a[c]))*(1/(i.rows-1)));return h}let zn=class{constructor(i,t={}){const{assumeSymmetric:e=!1}=t;if(i=pt.checkMatrix(i),!i.isSquare())throw new Error("Matrix is not a square matrix");if(i.isEmpty())throw new Error("Matrix must be non-empty");let n=i.columns,s=new E(n,n),r=new Float64Array(n),o=new Float64Array(n),a=i,h,l,c=!1;if(e?c=!0:c=i.isSymmetric(),c){for(h=0;h<n;h++)for(l=0;l<n;l++)s.set(h,l,a.get(h,l));Zs(n,o,r,s),Ks(n,o,r,s)}else{let u=new E(n,n),m=new Float64Array(n);for(l=0;l<n;l++)for(h=0;h<n;h++)u.set(h,l,a.get(h,l));tr(n,u,m,s),er(n,o,r,s,u)}this.n=n,this.e=o,this.d=r,this.V=s}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let i=this.n,t=this.e,e=this.d,n=new E(i,i),s,r;for(s=0;s<i;s++){for(r=0;r<i;r++)n.set(s,r,0);n.set(s,s,e[s]),t[s]>0?n.set(s,s+1,t[s]):t[s]<0&&n.set(s,s-1,t[s])}return n}};function Zs(i,t,e,n){let s,r,o,a,h,l,c,u;for(h=0;h<i;h++)e[h]=n.get(i-1,h);for(a=i-1;a>0;a--){for(u=0,o=0,l=0;l<a;l++)u=u+Math.abs(e[l]);if(u===0)for(t[a]=e[a-1],h=0;h<a;h++)e[h]=n.get(a-1,h),n.set(a,h,0),n.set(h,a,0);else{for(l=0;l<a;l++)e[l]/=u,o+=e[l]*e[l];for(s=e[a-1],r=Math.sqrt(o),s>0&&(r=-r),t[a]=u*r,o=o-s*r,e[a-1]=s-r,h=0;h<a;h++)t[h]=0;for(h=0;h<a;h++){for(s=e[h],n.set(h,a,s),r=t[h]+n.get(h,h)*s,l=h+1;l<=a-1;l++)r+=n.get(l,h)*e[l],t[l]+=n.get(l,h)*s;t[h]=r}for(s=0,h=0;h<a;h++)t[h]/=o,s+=t[h]*e[h];for(c=s/(o+o),h=0;h<a;h++)t[h]-=c*e[h];for(h=0;h<a;h++){for(s=e[h],r=t[h],l=h;l<=a-1;l++)n.set(l,h,n.get(l,h)-(s*t[l]+r*e[l]));e[h]=n.get(a-1,h),n.set(a,h,0)}}e[a]=o}for(a=0;a<i-1;a++){if(n.set(i-1,a,n.get(a,a)),n.set(a,a,1),o=e[a+1],o!==0){for(l=0;l<=a;l++)e[l]=n.get(l,a+1)/o;for(h=0;h<=a;h++){for(r=0,l=0;l<=a;l++)r+=n.get(l,a+1)*n.get(l,h);for(l=0;l<=a;l++)n.set(l,h,n.get(l,h)-r*e[l])}}for(l=0;l<=a;l++)n.set(l,a+1,0)}for(h=0;h<i;h++)e[h]=n.get(i-1,h),n.set(i-1,h,0);n.set(i-1,i-1,1),t[0]=0}function Ks(i,t,e,n){let s,r,o,a,h,l,c,u,m,w,g,p,M,f,P,b;for(o=1;o<i;o++)t[o-1]=t[o];t[i-1]=0;let S=0,C=0,V=Number.EPSILON;for(l=0;l<i;l++){for(C=Math.max(C,Math.abs(e[l])+Math.abs(t[l])),c=l;c<i&&!(Math.abs(t[c])<=V*C);)c++;if(c>l)do{for(s=e[l],u=(e[l+1]-s)/(2*t[l]),m=kt(u,1),u<0&&(m=-m),e[l]=t[l]/(u+m),e[l+1]=t[l]*(u+m),w=e[l+1],r=s-e[l],o=l+2;o<i;o++)e[o]-=r;for(S=S+r,u=e[c],g=1,p=g,M=g,f=t[l+1],P=0,b=0,o=c-1;o>=l;o--)for(M=p,p=g,b=P,s=g*t[o],r=g*u,m=kt(u,t[o]),t[o+1]=P*m,P=t[o]/m,g=u/m,u=g*e[o]-P*s,e[o+1]=r+P*(g*s+P*e[o]),h=0;h<i;h++)r=n.get(h,o+1),n.set(h,o+1,P*n.get(h,o)+g*r),n.set(h,o,g*n.get(h,o)-P*r);u=-P*b*M*f*t[l]/w,t[l]=P*u,e[l]=g*u}while(Math.abs(t[l])>V*C);e[l]=e[l]+S,t[l]=0}for(o=0;o<i-1;o++){for(h=o,u=e[o],a=o+1;a<i;a++)e[a]<u&&(h=a,u=e[a]);if(h!==o)for(e[h]=e[o],e[o]=u,a=0;a<i;a++)u=n.get(a,o),n.set(a,o,n.get(a,h)),n.set(a,h,u)}}function tr(i,t,e,n){let s=0,r=i-1,o,a,h,l,c,u,m;for(u=s+1;u<=r-1;u++){for(m=0,l=u;l<=r;l++)m=m+Math.abs(t.get(l,u-1));if(m!==0){for(h=0,l=r;l>=u;l--)e[l]=t.get(l,u-1)/m,h+=e[l]*e[l];for(a=Math.sqrt(h),e[u]>0&&(a=-a),h=h-e[u]*a,e[u]=e[u]-a,c=u;c<i;c++){for(o=0,l=r;l>=u;l--)o+=e[l]*t.get(l,c);for(o=o/h,l=u;l<=r;l++)t.set(l,c,t.get(l,c)-o*e[l])}for(l=0;l<=r;l++){for(o=0,c=r;c>=u;c--)o+=e[c]*t.get(l,c);for(o=o/h,c=u;c<=r;c++)t.set(l,c,t.get(l,c)-o*e[c])}e[u]=m*e[u],t.set(u,u-1,m*a)}}for(l=0;l<i;l++)for(c=0;c<i;c++)n.set(l,c,l===c?1:0);for(u=r-1;u>=s+1;u--)if(t.get(u,u-1)!==0){for(l=u+1;l<=r;l++)e[l]=t.get(l,u-1);for(c=u;c<=r;c++){for(a=0,l=u;l<=r;l++)a+=e[l]*n.get(l,c);for(a=a/e[u]/t.get(u,u-1),l=u;l<=r;l++)n.set(l,c,n.get(l,c)+a*e[l])}}}function er(i,t,e,n,s){let r=i-1,o=0,a=i-1,h=Number.EPSILON,l=0,c=0,u=0,m=0,w=0,g=0,p=0,M=0,f,P,b,S,C,V,v,R,j,d,x,A,y,I,$;for(f=0;f<i;f++)for((f<o||f>a)&&(e[f]=s.get(f,f),t[f]=0),P=Math.max(f-1,0);P<i;P++)c=c+Math.abs(s.get(f,P));for(;r>=o;){for(S=r;S>o&&(g=Math.abs(s.get(S-1,S-1))+Math.abs(s.get(S,S)),g===0&&(g=c),!(Math.abs(s.get(S,S-1))<h*g));)S--;if(S===r)s.set(r,r,s.get(r,r)+l),e[r]=s.get(r,r),t[r]=0,r--,M=0;else if(S===r-1){if(v=s.get(r,r-1)*s.get(r-1,r),u=(s.get(r-1,r-1)-s.get(r,r))/2,m=u*u+v,p=Math.sqrt(Math.abs(m)),s.set(r,r,s.get(r,r)+l),s.set(r-1,r-1,s.get(r-1,r-1)+l),R=s.get(r,r),m>=0){for(p=u>=0?u+p:u-p,e[r-1]=R+p,e[r]=e[r-1],p!==0&&(e[r]=R-v/p),t[r-1]=0,t[r]=0,R=s.get(r,r-1),g=Math.abs(R)+Math.abs(p),u=R/g,m=p/g,w=Math.sqrt(u*u+m*m),u=u/w,m=m/w,P=r-1;P<i;P++)p=s.get(r-1,P),s.set(r-1,P,m*p+u*s.get(r,P)),s.set(r,P,m*s.get(r,P)-u*p);for(f=0;f<=r;f++)p=s.get(f,r-1),s.set(f,r-1,m*p+u*s.get(f,r)),s.set(f,r,m*s.get(f,r)-u*p);for(f=o;f<=a;f++)p=n.get(f,r-1),n.set(f,r-1,m*p+u*n.get(f,r)),n.set(f,r,m*n.get(f,r)-u*p)}else e[r-1]=R+u,e[r]=R+u,t[r-1]=p,t[r]=-p;r=r-2,M=0}else{if(R=s.get(r,r),j=0,v=0,S<r&&(j=s.get(r-1,r-1),v=s.get(r,r-1)*s.get(r-1,r)),M===10){for(l+=R,f=o;f<=r;f++)s.set(f,f,s.get(f,f)-R);g=Math.abs(s.get(r,r-1))+Math.abs(s.get(r-1,r-2)),R=j=.75*g,v=-.4375*g*g}if(M===30&&(g=(j-R)/2,g=g*g+v,g>0)){for(g=Math.sqrt(g),j<R&&(g=-g),g=R-v/((j-R)/2+g),f=o;f<=r;f++)s.set(f,f,s.get(f,f)-g);l+=g,R=j=v=.964}for(M=M+1,C=r-2;C>=S&&(p=s.get(C,C),w=R-p,g=j-p,u=(w*g-v)/s.get(C+1,C)+s.get(C,C+1),m=s.get(C+1,C+1)-p-w-g,w=s.get(C+2,C+1),g=Math.abs(u)+Math.abs(m)+Math.abs(w),u=u/g,m=m/g,w=w/g,!(C===S||Math.abs(s.get(C,C-1))*(Math.abs(m)+Math.abs(w))<h*(Math.abs(u)*(Math.abs(s.get(C-1,C-1))+Math.abs(p)+Math.abs(s.get(C+1,C+1))))));)C--;for(f=C+2;f<=r;f++)s.set(f,f-2,0),f>C+2&&s.set(f,f-3,0);for(b=C;b<=r-1&&(I=b!==r-1,b!==C&&(u=s.get(b,b-1),m=s.get(b+1,b-1),w=I?s.get(b+2,b-1):0,R=Math.abs(u)+Math.abs(m)+Math.abs(w),R!==0&&(u=u/R,m=m/R,w=w/R)),R!==0);b++)if(g=Math.sqrt(u*u+m*m+w*w),u<0&&(g=-g),g!==0){for(b!==C?s.set(b,b-1,-g*R):S!==C&&s.set(b,b-1,-s.get(b,b-1)),u=u+g,R=u/g,j=m/g,p=w/g,m=m/u,w=w/u,P=b;P<i;P++)u=s.get(b,P)+m*s.get(b+1,P),I&&(u=u+w*s.get(b+2,P),s.set(b+2,P,s.get(b+2,P)-u*p)),s.set(b,P,s.get(b,P)-u*R),s.set(b+1,P,s.get(b+1,P)-u*j);for(f=0;f<=Math.min(r,b+3);f++)u=R*s.get(f,b)+j*s.get(f,b+1),I&&(u=u+p*s.get(f,b+2),s.set(f,b+2,s.get(f,b+2)-u*w)),s.set(f,b,s.get(f,b)-u),s.set(f,b+1,s.get(f,b+1)-u*m);for(f=o;f<=a;f++)u=R*n.get(f,b)+j*n.get(f,b+1),I&&(u=u+p*n.get(f,b+2),n.set(f,b+2,n.get(f,b+2)-u*w)),n.set(f,b,n.get(f,b)-u),n.set(f,b+1,n.get(f,b+1)-u*m)}}}if(c!==0){for(r=i-1;r>=0;r--)if(u=e[r],m=t[r],m===0)for(S=r,s.set(r,r,1),f=r-1;f>=0;f--){for(v=s.get(f,f)-u,w=0,P=S;P<=r;P++)w=w+s.get(f,P)*s.get(P,r);if(t[f]<0)p=v,g=w;else if(S=f,t[f]===0?s.set(f,r,v!==0?-w/v:-w/(h*c)):(R=s.get(f,f+1),j=s.get(f+1,f),m=(e[f]-u)*(e[f]-u)+t[f]*t[f],V=(R*g-p*w)/m,s.set(f,r,V),s.set(f+1,r,Math.abs(R)>Math.abs(p)?(-w-v*V)/R:(-g-j*V)/p)),V=Math.abs(s.get(f,r)),h*V*V>1)for(P=f;P<=r;P++)s.set(P,r,s.get(P,r)/V)}else if(m<0)for(S=r-1,Math.abs(s.get(r,r-1))>Math.abs(s.get(r-1,r))?(s.set(r-1,r-1,m/s.get(r,r-1)),s.set(r-1,r,-(s.get(r,r)-u)/s.get(r,r-1))):($=Ee(0,-s.get(r-1,r),s.get(r-1,r-1)-u,m),s.set(r-1,r-1,$[0]),s.set(r-1,r,$[1])),s.set(r,r-1,0),s.set(r,r,1),f=r-2;f>=0;f--){for(d=0,x=0,P=S;P<=r;P++)d=d+s.get(f,P)*s.get(P,r-1),x=x+s.get(f,P)*s.get(P,r);if(v=s.get(f,f)-u,t[f]<0)p=v,w=d,g=x;else if(S=f,t[f]===0?($=Ee(-d,-x,v,m),s.set(f,r-1,$[0]),s.set(f,r,$[1])):(R=s.get(f,f+1),j=s.get(f+1,f),A=(e[f]-u)*(e[f]-u)+t[f]*t[f]-m*m,y=(e[f]-u)*2*m,A===0&&y===0&&(A=h*c*(Math.abs(v)+Math.abs(m)+Math.abs(R)+Math.abs(j)+Math.abs(p))),$=Ee(R*w-p*d+m*x,R*g-p*x-m*d,A,y),s.set(f,r-1,$[0]),s.set(f,r,$[1]),Math.abs(R)>Math.abs(p)+Math.abs(m)?(s.set(f+1,r-1,(-d-v*s.get(f,r-1)+m*s.get(f,r))/R),s.set(f+1,r,(-x-v*s.get(f,r)-m*s.get(f,r-1))/R)):($=Ee(-w-j*s.get(f,r-1),-g-j*s.get(f,r),p,m),s.set(f+1,r-1,$[0]),s.set(f+1,r,$[1]))),V=Math.max(Math.abs(s.get(f,r-1)),Math.abs(s.get(f,r))),h*V*V>1)for(P=f;P<=r;P++)s.set(P,r-1,s.get(P,r-1)/V),s.set(P,r,s.get(P,r)/V)}for(f=0;f<i;f++)if(f<o||f>a)for(P=f;P<i;P++)n.set(f,P,s.get(f,P));for(P=i-1;P>=o;P--)for(f=o;f<=a;f++){for(p=0,b=o;b<=Math.min(P,a);b++)p=p+n.get(f,b)*s.get(b,P);n.set(f,P,p)}}}function Ee(i,t,e,n){let s,r;return Math.abs(e)>Math.abs(n)?(s=n/e,r=e+s*n,[(i+s*t)/r,(t-s*i)/r]):(s=e/n,r=n+s*e,[(s*i+t)/r,(s*t-i)/r])}class Dn{constructor(t){if(t=pt.checkMatrix(t),!t.isSymmetric())throw new Error("Matrix is not symmetric");let e=t,n=e.rows,s=new E(n,n),r=!0,o,a,h;for(a=0;a<n;a++){let l=0;for(h=0;h<a;h++){let c=0;for(o=0;o<h;o++)c+=s.get(h,o)*s.get(a,o);c=(e.get(a,h)-c)/s.get(h,h),s.set(a,h,c),l=l+c*c}for(l=e.get(a,a)-l,r&&(r=l>0),s.set(a,a,Math.sqrt(Math.max(l,0))),h=a+1;h<n;h++)s.set(a,h,0)}this.L=s,this.positiveDefinite=r}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=pt.checkMatrix(t);let e=this.L,n=e.rows;if(t.rows!==n)throw new Error("Matrix dimensions do not match");if(this.isPositiveDefinite()===!1)throw new Error("Matrix is not positive definite");let s=t.columns,r=t.clone(),o,a,h;for(h=0;h<n;h++)for(a=0;a<s;a++){for(o=0;o<h;o++)r.set(h,a,r.get(h,a)-r.get(o,a)*e.get(h,o));r.set(h,a,r.get(h,a)/e.get(h,h))}for(h=n-1;h>=0;h--)for(a=0;a<s;a++){for(o=h+1;o<n;o++)r.set(h,a,r.get(h,a)-r.get(o,a)*e.get(o,h));r.set(h,a,r.get(h,a)/e.get(h,h))}return r}get lowerTriangularMatrix(){return this.L}}class Un{constructor(t,e={}){t=pt.checkMatrix(t);let{Y:n}=e;const{scaleScores:s=!1,maxIterations:r=1e3,terminationCriteria:o=1e-10}=e;let a;if(n){if(mt.isAnyArray(n)&&typeof n[0]=="number"?n=E.columnVector(n):n=pt.checkMatrix(n),n.rows!==t.rows)throw new Error("Y should have the same number of rows as X");a=n.getColumnVector(0)}else a=t.getColumnVector(0);let h=1,l,c,u,m;for(let w=0;w<r&&h>o;w++)u=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),u=u.div(u.norm()),l=t.mmul(u).div(u.transpose().mmul(u).get(0,0)),w>0&&(h=l.clone().sub(m).pow(2).sum()),m=l.clone(),n?(c=n.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0)),c=c.div(c.norm()),a=n.mmul(c).div(c.transpose().mmul(c).get(0,0))):a=l;if(n){let w=t.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0));w=w.div(w.norm());let g=t.clone().sub(l.clone().mmul(w.transpose())),p=a.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0)),M=n.clone().sub(l.clone().mulS(p.get(0,0)).mmul(c.transpose()));this.t=l,this.p=w.transpose(),this.w=u.transpose(),this.q=c,this.u=a,this.s=l.transpose().mmul(l),this.xResidual=g,this.yResidual=M,this.betas=p}else this.w=u.transpose(),this.s=l.transpose().mmul(l).sqrt(),s?this.t=l.clone().div(this.s.get(0,0)):this.t=l,this.xResidual=t.sub(l.mmul(u.transpose()))}}N.AbstractMatrix=Z,N.CHO=Dn,N.CholeskyDecomposition=Dn,N.DistanceMatrix=qn,N.EVD=zn;var nr=N.EigenvalueDecomposition=zn;N.LU=ve,N.LuDecomposition=ve;var ir=N.Matrix=E;N.MatrixColumnSelectionView=Vs,N.MatrixColumnView=Bs,N.MatrixFlipColumnView=js,N.MatrixFlipRowView=qs,N.MatrixRowSelectionView=Os,N.MatrixRowView=Ns,N.MatrixSelectionView=xe,N.MatrixSubView=zs,N.MatrixTransposeView=Ds,N.NIPALS=Un,N.Nipals=Un,N.QR=Ge,N.QrDecomposition=Ge,N.SVD=Dt,N.SingularValueDecomposition=Dt,N.SymmetricMatrix=ne,N.WrapperMatrix1D=Nn,N.WrapperMatrix2D=pt,N.correlation=Js,N.covariance=Gs;var Yn=N.default=E;N.determinant=Ae,N.inverse=Ys,N.linearDependencies=Hs,N.pseudoInverse=Qs,N.solve=On,N.wrap=Us;const sr=nr,rr=ir;Yn.Matrix&&Yn.Matrix;function ie(i,t,e){const n=i/e,s=t/e;if(!(isFinite(n)&&isFinite(s))){const h=-i/t;return isFinite(h)?[h]:i==0&&t==0?[0]:[]}const r=s*s-4*n;let o=0;if(isFinite(r)){if(r<0)return[];if(r==0)return[-.5*s];o=-.5*(s+Ut(Math.sqrt(r),s))}else o=-s;const a=n/o;return isFinite(a)?a>o?[o,a]:[a,o]:[o]}function Xn(i,t,e,n){const s=e/(3*n),r=t/(3*n),o=i/n;if(!(isFinite(o)&&isFinite(r)&&isFinite(s)))return ie(i,t,e);const a=-s*s+r,h=-r*s+o,l=s*o-r*r,c=4*a*l-h*h,u=-2*s*a+h;if(c<0){const m=Math.sqrt(-.25*c),w=-.5*u;return[Math.cbrt(w+m)+Math.cbrt(w-m)-s]}else if(c==0){const m=Ut(Math.sqrt(-a),u);return[m-s,-2*m-s]}else{const m=Math.atan2(Math.sqrt(c),-u)/3,w=Math.cos(m),g=Math.sin(m)*Math.sqrt(3),p=.5*(-w+g),M=.5*(-w-g),f=2*Math.sqrt(-a);return[f*w-s,f*p-s,f*M-s]}}function or(i,t,e,n,s){if(s==0)return Xn(i,t,e,n);const r=n/s,o=e/s,a=t/s,h=i/s;let l=Wn(r,o,a,h,!1);if(l!==null)return l;const c=716e74;for(let u=0;u<2;u++)if(l=Wn(r/c,o/(c*c),a/(c*c*c),h/(c*c*c*c),u!=0),l!==null){for(let m=0;m<l.length;m++)l[m]*=c;return l}return[]}function Bt(i,t){return t==0?Math.abs(i):Math.abs((i-t)/t)}function Wn(i,t,e,n,s){const r=ar(i,t,e,n,s);if(r!==null&&r.length==4){let o=[];for(let a=0;a<2;a++){const h=r[a*2],l=r[a*2+1];o=o.concat(ie(l,h,1))}return o}return null}function ar(i,t,e,n,s){function r(L,O,B,Y){const Q=Bt(L+B,i),T=Bt(O+L*B+Y,t),U=Bt(O*B+L*Y,e);return Q+T+U}function o(L,O,B,Y){return r(L,O,B,Y)+Bt(O*Y,n)}const a=9*i*i-24*t,h=a>=0?-2*t/(3*i+Ut(Math.sqrt(a),i)):-.25*i,l=i+4*h,c=t+3*h*(i+2*h),u=e+h*(2*t+h*(3*i+4*h)),m=n+h*(e+h*(t+h*(i+h)));let w=0,g=0;const p=349e100;if(s){const L=l/p,O=c/p,B=u/p,Y=m/p;w=L*B-4/p*Y-1/3*O*O,g=(L*B-8/p*Y-2/9*O*O)*(1/3)*O-B*(B/p)-L*L*Y}else w=l*u-4*m-1/3*c*c,g=(l*u+8*m-2/9*c*c)*(1/3)*c-u*u-l*l*m;if(!isFinite(w)&&isFinite(g))return null;let M=hr(w,g);s&&(M*=p);const f=i*.5,P=1/6*t+.5*M,b=e-i*P,S=2/3*t-M-f*f,C=.5*b/S,V=2*(n-P*P)/b,v=.5*b/V;let R=0,j=0,d=0;for(let L=0;L<3;L++){const O=L==1?v:S,B=L==0?C:V,Y=Bt(O+f*f+2*P,t),Q=Bt(2*(O*B+f*P),e),T=Bt(O*B*B+P*P,n),U=Y+Q+T;(L==0||U<d)&&(R=O,j=B,d=U)}const x=R,A=j;let y=0,I=0,$=0,F=0;if(x<0){const L=Math.sqrt(-x);if(y=f+L,I=P+L*A,$=f-L,F=P-L*A,Math.abs(F)<Math.abs(I)?F=n/I:Math.abs(F)>Math.abs(I)&&(I=n/F),Math.abs(y)!=Math.abs($)){let O=null,B=null;if(Math.abs(y)<Math.abs($)){const Q=(e-I*$)/F,T=(t-F-I)/$;O=[i-$,Q,T],B=[$,$,$]}else{const Q=(e-y*F)/I,T=(t-F-I)/y,U=i-y;O=[y,y,y],B=[U,Q,T]}let Y=0;for(let Q=0;Q<3;Q++){const T=O[Q],U=B[Q];if(isFinite(T)&&isFinite(U)){const nt=r(T,I,U,F);(Q==0||nt<Y)&&(y=T,$=U,Y=nt)}}}}else if(x==0){const L=n-P*P;y=f,I=P+Math.sqrt(-L),$=f,F=P-Math.sqrt(-L),Math.abs(I)>Math.abs(F)?F=n/I:Math.abs(F)>Math.abs(I)&&(I=n/F)}else return[];let H=o(y,I,$,F);for(let L=0;L<8&&H!=0;L++){const O=I*F-n,B=I*$+y*F-e,Y=I+y*$+F-t,Q=y+$-i,T=y-$,U=I*I-I*($*T+2*F)+F*(y*T+F);if(U==0)break;const nt=1/U,K=F-I,G=I*$-y*F,Pn=T*O+K*B+G*Y-(I*K+y*G)*Q,Mn=(y*T+K)*O-I*(T*B+K*Y+G*Q),yn=-T*O-K*B-G*Y+($*G+F*K)*Q,bn=-($*T+K)*O+F*(T*B+K*Y+G*Q),Le=y-nt*Pn,Be=I-nt*Mn,Ve=$-nt*yn,je=F-nt*bn,qe=o(Le,Be,Ve,je);if(qe<H)y=Le,I=Be,$=Ve,F=je,H=qe;else break}return[y,I,$,F]}function hr(i,t){const e=-.3333333333333333*i,n=.5*t;let s,r=null;if((Math.abs(e)>=1e102||Math.abs(n)>=1e164)&&(Math.abs(e)<Math.abs(n)?r=1-e*(e/n)*(e/n):r=Math.sign(e)*(n/e*(n/e)/e-1)),r!==null&&n==0)i>0?s=0:s=Math.sqrt(-i);else if(r!==null?r<0:n*n<e*e*e){const h=r!==null?n/e/Math.sqrt(e):n/Math.sqrt(e*e*e);s=-2*Math.sqrt(e)*Ut(Math.cos(Math.acos(Math.abs(h))*(1/3)),h)}else{let h;r!==null?Math.abs(e)<Math.abs(n)?h=-n*(1+Math.sqrt(r)):h=-n-Ut(Math.sqrt(Math.abs(e))*e*Math.sqrt(r),n):h=Math.cbrt(-n-Ut(Math.sqrt(n*n-e*e*e),n));const l=h==0?0:e/h;s=h+l}let o=s,a=(o*o+i)*o+t;if(Math.abs(a)<222045e-21*Math.max(o*o*o,i*o,t))return o;for(let h=0;h<8;h++){const l=3*o*o+i;if(l==0)break;const c=o-a/l,u=(c*c+i)*c+t;if(u==0)return c;if(Math.abs(u)>=Math.abs(a))break;o=c,a=u}return o}function Ut(i,t){const e=Math.abs(i);return t<0?-e:e}function Je(i,t=1e-9){if(i[i.length-1]==0)return Je(i.slice(0,-1),t);const e=new rr([i.slice(0,-1).reverse().map(s=>-s/i[i.length-1]),...Array.from({length:i.length-2},(s,r)=>{const o=new Array(i.length-1).fill(0);return o[r]=1,o})]),n=new sr(e);return n.realEigenvalues.filter((s,r)=>Math.abs(n.imaginaryEigenvalues[r])<t)}const lr=(i,t,e)=>[i[0]*(1-e)+t[0]*e,i[1]*(1-e)+t[1]*e];function ur(i){const t=[];for(let e=0;e<i.length-1;e++)t.push([i[e],i[e+1]]);return t}function Ze(i,t){const e=ur(i).map(([n,s])=>lr(n,s,t));return i.length===2?[e]:[...Ze(e,t),e]}let ot=class Qt extends te{constructor(t,e,n,s){super(t,e),_(this,"segmentType","CUBIC_BEZIER"),_(this,"firstControlPoint"),_(this,"lastControlPoint"),_(this,"_boundingBox",null),_(this,"_polynomialCoefficients",null),this.firstControlPoint=n,this.lastControlPoint=s}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,s,r]=[this.firstPoint[t],this.firstControlPoint[t],this.lastControlPoint[t],this.lastPoint[t]],o=-e+3*n-3*s+r,a=2*e-4*n+2*s,h=-e+n;return ie(h,a,o).filter(l=>l>=-this.precision&&l<=1+this.precision)}getParametersOfExtrema(){return Array.from(new Set(this._extremaInDirection(0).concat(this._extremaInDirection(1))))}get alignedCurve(){const t=[-this.firstPoint[0],-this.firstPoint[1]],e=q(this.lastPoint,this.firstPoint),n=Math.atan2(e[1],e[0]),s=new ft;return s.translate(t[0],t[1]),s.rotate(-n),this.transform(s)}getInflexionParameters(){const t=this.alignedCurve,[e,n]=t.firstControlPoint,[s,r]=t.lastControlPoint,[o,a]=t.lastPoint,h=s*n,l=o*n,c=e*r,u=o*r,m=-3*h+2*l+3*c-u,w=3*h-l-3*c,g=c-h;return ie(g,w,m).filter(p=>p>=0&&p<=1)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=De([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new Qt(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new Qt(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:k(this.firstPoint,t.firstPoint)&&k(this.lastPoint,t.lastPoint)&&k(this.firstControlPoint,t.firstControlPoint)&&k(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const e=$n(n=>Ft(this.paramPoint(n),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(J(this.firstPoint,t),J(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=1-t,n=e*e,s=t*t,r=3*n,o=6*e*t,a=3*s,h=q(this.firstControlPoint,this.firstPoint),l=q(this.lastControlPoint,this.firstControlPoint),c=q(this.lastPoint,this.lastControlPoint);return[r*h[0]+o*l[0]+a*c[0],r*h[1]+o*l[1]+a*c[1]]}secondDerivativeAt(t){const e=X(this.firstPoint,this.lastControlPoint),n=W(this.firstControlPoint,2),s=X(this.firstControlPoint,this.lastPoint),r=W(this.lastControlPoint,2),o=W(q(e,n),6*(1-t)),a=W(q(s,r),6*t);return X(o,a)}tangentAt(t){const e=this.pointToParam(t);return rt(this.gradientAt(e))}get tangentAtFirstPoint(){return rt(q(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return rt(q(this.lastPoint,this.lastControlPoint))}normalAt(t){const e=this.tangentAt(t);return tt(e)}get normalAtFirstPoint(){return tt(this.tangentAtFirstPoint)}get normalAtLastPoint(){return tt(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((r,o)=>r-o);let s={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return n.flatMap((r,o)=>{if(r-s.originalParam<this.precision)return[];let a=!1;r>1-this.precision&&(a=!0);const h=(r-s.originalParam)/(1-s.originalParam),l=Ze([s.p0,s.p1,s.p2,this.lastPoint],a?1:h),c=a?this.lastPoint:(e==null?void 0:e.get(r))??l[0][0],u=l[1][0],m=l[2][0],w=l[1][1],g=l[2][2],p=new Qt(s.p0,c,m,u);return s={param:h,originalParam:r,p0:c,p1:w,p2:g},o===n.length-1&&!a?[p,new Qt(s.p0,this.lastPoint,s.p1,s.p2)]:p})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=e.map(r=>this.pointToParam(r)),s=new Map(yt([n,e]));return this.splitAtParameters(n,s)}transform(t){return new Qt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.firstControlPoint),t.transform(this.lastControlPoint))}paramPoint(t){const e=1-t,n=e*e,s=t*t,r=n*e,o=3*n*t,a=3*e*s,h=s*t;return[r*this.firstPoint[0]+o*this.firstControlPoint[0]+a*this.lastControlPoint[0]+h*this.lastPoint[0],r*this.firstPoint[1]+o*this.firstControlPoint[1]+a*this.lastControlPoint[1]+h*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],s=this.firstControlPoint[e],r=this.lastControlPoint[e],o=this.lastPoint[e],a=-n+3*s-3*r+o,h=3*n-6*s+3*r,l=-3*n+3*s;return[n,l,h,a]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,s,r]=this.polynomialCoefficients[1];return Xn(e-t,n,s,r).filter(o=>o>=-this.precision&&o<=1+this.precision)}pointToParam(t){if(k(t,this.firstPoint,this.precision))return 0;if(k(t,this.lastPoint,this.precision))return 1;const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve");const n=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,s=e.find(n);if(s===void 0)throw new Error("Point is not on the curve");return s}},ht=class le extends te{constructor(t,e,n,s,r,o,a=!1,{ignoreChecks:h=!1,angleUnits:l="deg"}={}){super(t,e),_(this,"segmentType","ELLIPSE_ARC"),_(this,"precision",1e-6),_(this,"majorRadius"),_(this,"minorRadius"),_(this,"center"),_(this,"tiltAngle"),_(this,"clockwise"),_(this,"_coefficients",null),_(this,"_boundingBox"),_(this,"_linearExentricity"),_(this,"_exentricity"),_(this,"_focals"),_(this,"_ellipseReferenceFrameTransform"),_(this,"_reverseEllipseReferenceFrameTransform"),_(this,"_rotateFromEllipseReferenceFrame"),_(this,"_firstAngle"),_(this,"_lastAngle"),_(this,"_deltaAngle"),this.center=n;const c=s>=r;this.majorRadius=c?s:r,this.minorRadius=c?r:s;const u=l==="deg"?o*$t:o;if(this.tiltAngle=Lt(c?u:u+Math.PI/2),this.clockwise=a,!h){if(k(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${st(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${st(e)} not on the ellipse defined by ${this.info}`);if(Math.abs(this.majorRadius-this.minorRadius)<this.precision)throw new Error("Both radii should be different, create an arc instead")}}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,n=this.majorRadius*this.majorRadius,s=this.minorRadius*this.minorRadius,r=Math.cos(this.tiltAngle),o=Math.sin(this.tiltAngle),a=n*o*o+s*r*r,h=2*(s-n)*o*r,l=n*r*r+s*o*o;this._coefficients={x2:a,xy:h,y2:l,x:-2*a*t-h*e,y:-2*l*e-h*t,c:a*t*t+h*t*e+l*e*e-n*s}}return this._coefficients}get info(){return`ELLIPSE_ARC(${st(this.firstPoint)}, ${st(this.lastPoint)}, ${st(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*Ue}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new le(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new le(this.firstPoint,this.lastPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}computeEllipseExtrema(){const t=Math.tan(this.tiltAngle),e=this.minorRadius/this.majorRadius,n=-Math.atan(e*t),s=Math.atan(e/t);return[n,Math.PI+n,s,Math.PI+s].map(r=>this.thetaToParam(r)).filter(r=>this.isValidParameter(r)).map(r=>this.paramPoint(r))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=De(e).grow(this.precision)}return this._boundingBox}get linearExentricity(){return this._linearExentricity===void 0&&(this._linearExentricity=Math.sqrt(this.majorRadius*this.majorRadius-this.minorRadius*this.minorRadius)),this._linearExentricity}get exentricity(){return this._exentricity===void 0&&(this._exentricity=this.linearExentricity/this.majorRadius),this._exentricity}get focals(){if(this._focals===void 0){const t=this.majorAxis;this._focals=[X(this.center,W(t,this.linearExentricity)),X(this.center,W(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return Et(1,this.tiltAngle)}paramPoint(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1);return this.reverseEllipseReferenceFrameTransform.transform([this.majorRadius*Math.cos(e),this.minorRadius*Math.sin(e)])}pointToParam(t){if(!this.isPointOnEllipse(t))throw new Error(`Point ${st(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${st(t)} is not on segment ${this.repr}`);return e}get midPoint(){return this.paramPoint(.5)}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}isSame(t){return k(this.center,t.center)&&Math.abs(this.majorRadius-t.majorRadius)<this.precision&&Math.abs(this.minorRadius-t.minorRadius)<this.precision&&(Math.abs(this.tiltAngle-t.tiltAngle)<this.precision||Math.abs(Math.abs(this.tiltAngle-t.tiltAngle)-Math.PI)<this.precision)&&(k(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||k(this.firstPoint,t.lastPoint)&&this.clockwise!==t.clockwise)}pointTheta(t){const e=this.ellipseReferenceFrameTransform.transform(t),n=Math.atan2(e[1]/this.minorRadius,e[0]/this.majorRadius);return Lt(n)}thetaToParam(t){return me(this.firstAngle,Lt(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,n]=this.focals,s=J(t,e),r=J(t,n);return Math.abs(2*this.majorRadius-s-r)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(k(t,this.center)?e=X(this.center,W(tt(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(mr(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return J(t,e);if(Ji(t,this.majorAxis,this.center)){const n=Lt(2*Math.PI-this.pointTheta(t)),s=this.thetaToParam(n);if(this.isValidParameter(s))return J(t,this.paramPoint(s))}return Math.min(J(t,this.firstPoint),J(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new ft().translate(-this.center[0],-this.center[1]).rotate(-this.tiltAngle)),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new ft().rotate(this.tiltAngle).translate(this.center[0],this.center[1])),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new ft().rotate(this.tiltAngle)),this._rotateFromEllipseReferenceFrame}get firstAngle(){return this._firstAngle===void 0&&(this._firstAngle=this.pointTheta(this.firstPoint)),this._firstAngle}get lastAngle(){return this._lastAngle===void 0&&(this._lastAngle=this.pointTheta(this.lastPoint)),this._lastAngle}get deltaAngle(){return this._deltaAngle===void 0&&(this._deltaAngle=me(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?tt(e):Kt(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),n=-this.majorRadius*this.deltaAngle*Math.sin(e),s=this.minorRadius*this.deltaAngle*Math.cos(e),r=this.clockwise?[-n,-s]:[n,s];return this.rotateFromEllipseReferenceFrame.transform(r)}tangentAt(t){const e=this.pointTheta(t),n=-this.majorRadius*Math.sin(e),s=this.minorRadius*Math.cos(e),r=this.clockwise?[-n,-s]:[n,s];return rt(this.rotateFromEllipseReferenceFrame.transform(r))}get tangentAtFirstPoint(){const t=-this.majorRadius*Math.sin(this.firstAngle),e=this.minorRadius*Math.cos(this.firstAngle),n=this.clockwise?[-t,-e]:[t,e];return rt(this.rotateFromEllipseReferenceFrame.transform(n))}get tangentAtLastPoint(){const t=-this.majorRadius*Math.sin(this.lastAngle),e=this.minorRadius*Math.cos(this.lastAngle),n=this.clockwise?[-t,-e]:[t,e];return rt(this.rotateFromEllipseReferenceFrame.transform(n))}transform(t){const e=t.transformAngle(this.tiltAngle),n=t.scaleFactor();return new le(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),this.majorRadius*n,this.minorRadius*n,e,t.keepsOrientation()?this.clockwise:!this.clockwise,{angleUnits:"rad"})}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(yt([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 le(s.get(l)||this.paramPoint(l),s.get(h)||this.paramPoint(h),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return r=null,c})}};function cr(i,t,e,n,s,r,o){const{center:a,rx:h,ry:l}=fr(i,t,e,n,s*$t,!r,o);return Math.abs(h-l)<1e-9?new z(i,t,a,o):new ht(i,t,a,h,l,s,o)}function fr([i,t],[e,n],s,r,o,a,h){if(s<0&&(s=-s),r<0&&(r=-r),s==0||r==0)throw Error("rx and ry can not be 0");const l=Math.sin(o),c=Math.cos(o),u=(i-e)/2,m=(t-n)/2,w=(i+e)/2,g=(t+n)/2,p=c*u+l*m,M=c*m-l*u,f=p*p/(s*s)+M*M/(r*r);f>1&&(s=s*Math.sqrt(f),r=r*Math.sqrt(f));const P=s*r,b=s*M,S=r*p,C=b*b+S*S;if(!C)throw Error("start point can not be same as end point");let V=Math.sqrt(Math.abs((P*P-C)/C));a==h&&(V=-V);const v=V*b/r,R=-V*S/s,j=c*v-l*R+w,d=l*v+c*R+g;return{center:[j,d],rx:s,ry:r}}function mr(i,t,e){const n=Math.abs(e[0]),s=Math.abs(e[1]);let r=.707,o=.707;const a=i,h=t;for(let l=0;l<3;l++){const c=a*r,u=h*o,m=(a*a-h*h)*r**3/a,w=(h*h-a*a)*o**3/h,g=c-m,p=u-w,M=n-m,f=s-w,P=Math.hypot(g,p),b=Math.hypot(M,f);r=Math.min(1,Math.max(0,(M*P/b+m)/a)),o=Math.min(1,Math.max(0,(f*P/b+w)/h));const S=Math.hypot(r,o);r/=S,o/=S}return[a*r*Math.sign(e[0]),h*o*Math.sign(e[1])]}let et=class Gt extends te{constructor(t,e,n){super(t,e),_(this,"segmentType","QUADRATIC_BEZIER"),_(this,"controlPoint"),_(this,"_boundingBox",null),_(this,"_polynomialCoefficients",null),this.controlPoint=n}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,s]=[this.firstPoint[t],this.controlPoint[t],this.lastPoint[t]],r=e-2*n+s;if(!r)return[];const o=(e-n)/r;return o>=-this.precision&&o<=1+this.precision?[o]:[]}getParametersOfExtrema(){return Array.from(new Set(this._extremaInDirection(0).concat(this._extremaInDirection(1))))}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=De([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new Gt(this.firstPoint,this.lastPoint,this.controlPoint)}reverse(){return new Gt(this.lastPoint,this.firstPoint,this.controlPoint)}isSame(t){return t.segmentType!=="QUADRATIC_BEZIER"?!1:k(this.firstPoint,t.firstPoint)&&k(this.lastPoint,t.lastPoint)&&k(this.controlPoint,t.controlPoint)}distanceFrom(t){const e=$n(n=>Ft(this.paramPoint(n),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(J(this.firstPoint,t),J(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=n=>{const s=this.firstPoint[n],r=this.controlPoint[n],o=this.lastPoint[n];return 2*(t*(s-2*r+o)-s+r)};return[e(0),e(1)]}secondDerivativeAt(t){return W(q(X(this.firstPoint,this.lastPoint),W(this.controlPoint,2)),2)}tangentAt(t){const e=this.pointToParam(t);return rt(this.gradientAt(e))}get tangentAtFirstPoint(){return rt(q(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return rt(q(this.lastPoint,this.controlPoint))}normalAt(t){const e=this.tangentAt(t);return tt(e)}get normalAtFirstPoint(){return tt(this.tangentAtFirstPoint)}get normalAtLastPoint(){return tt(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((r,o)=>r-o);let s={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return n.flatMap((r,o)=>{if(r-s.originalParam<this.precision)return[];let a=!1;r>1-this.precision&&(a=!0);const h=(r-s.originalParam)/(1-s.originalParam),l=Ze([s.p0,s.p1,this.lastPoint],a?1:h),c=a?this.lastPoint:(e==null?void 0:e.get(r))??l[0][0],u=l[1][0],m=l[1][1],w=new Gt(s.p0,c,u);return s={param:h,originalParam:r,p0:c,p1:m},o===n.length-1&&!a?[w,new Gt(s.p0,this.lastPoint,s.p1)]:w})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=e.map(r=>this.pointToParam(r)),s=new Map(yt([n,e]));return this.splitAtParameters(n,s)}transform(t){return new Gt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const e=1-t,n=e*e,s=2*e*t,r=t*t;return[n*this.firstPoint[0]+s*this.controlPoint[0]+r*this.lastPoint[0],n*this.firstPoint[1]+s*this.controlPoint[1]+r*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],s=this.controlPoint[e],r=this.lastPoint[e],o=n-2*s+r,a=2*(s-n);return[n,a,o]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,s]=this.polynomialCoefficients[1];return ie(e-t,n,s).filter(r=>r>=-this.precision&&r<=1+this.precision)}pointToParam(t){const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve!");const n=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,s=e.find(n);if(s===void 0)throw new Error("Point is not on the curve");return s}};var gr=Object.defineProperty,pr=(i,t,e)=>t in i?gr(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,ut=(i,t,e)=>pr(i,typeof t!="symbol"?t+"":t,e);function wr(i,t){const e=q(t,i.firstPoint),n=fe(e,i.V)/i.squareLength;return i.paramPoint(n)}function Se(i,t,e){const n=e||i.precision,s=wr(i,t.center),r=J(s,t.center);if(r>t.radius+n)return[];if(Math.abs(r-t.radius)<n){const u=s;return i.isOnSegment(u)&&t.isOnSegment(u)?[u]:[]}const o=[],a=Math.sqrt(t.radius*t.radius-r*r),h=i.tangentAtFirstPoint,l=X(s,W(h,a));i.isOnSegment(l)&&t.isOnSegment(l)&&o.push(l);const c=X(s,W(h,-a));return i.isOnSegment(c)&&t.isOnSegment(c)&&o.push(c),o}const dr=i=>{const{firstPoint:t,lastPoint:e,center:n,clockwise:s}=i;return new z(e,t,n,s,{ignoreChecks:!0})},Pr=(i,t)=>{if(i.isSame(t))return[i];const e=At([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(n=>n!==null)).sort((n,s)=>i.pointToParam(n)-i.pointToParam(s));if(e.length===0)return[];if(e.length===1)return[];if(e.length===2)return i.isSame(dr(t))?[]:[new z(e[0],e[1],i.center,i.clockwise)];if(e.length===3){const n=k(e[0],t.lastPoint)||k(e[0],t.firstPoint)?1:0;return[new z(e[0+n],e[1+n],i.center,i.clockwise)]}else if(e.length===4)return[new z(e[0],e[1],i.center,i.clockwise),new z(e[2],e[3],i.center,i.clockwise)];throw new Error("Bug in the arc arc overlap algorithm")};function Hn(i,t,e=!1,n){const s=n||i.precision,r=J(i.center,t.center),o=i.radius+t.radius;if(r>o+s)return[];const a=Math.abs(i.radius-t.radius);if(r<a-s)return[];if(r<s)return a>s?[]:e?Pr(i,t):[];const h=rt(q(t.center,i.center)),l=r>o-s;if(l||Math.abs(r-a)<s){const f=l||i.radius>t.radius?1:-1,P=X(i.center,W(h,f*i.radius));return i.isOnSegment(P)&&t.isOnSegment(P)?[P]:[]}const c=i.radius*i.radius/(2*r)-t.radius*t.radius/(2*r)+r/2,u=X(i.center,W(h,c)),m=Math.sqrt(i.radius*i.radius-c*c),w=tt(h),g=X(u,W(w,m)),p=X(u,W(w,-m)),M=[];return i.isOnSegment(g)&&t.isOnSegment(g)&&M.push(g),i.isOnSegment(p)&&t.isOnSegment(p)&&M.push(p),M}function Ke(i,t,e=1e-9){const n=i.transform(t.ellipseReferenceFrameTransform),s=n.slope,r=n.yIntercept,o=t.majorRadius*t.majorRadius,a=t.minorRadius*t.minorRadius,h=t.majorRadius*t.minorRadius,l=n.slope*n.slope,c=n.yIntercept*n.yIntercept,u=f=>f.map(P=>t.reverseEllipseReferenceFrameTransform.transform(P)).filter(P=>i.isOnSegment(P)&&t.isOnSegment(P));if(!Number.isFinite(s)){const f=n.firstPoint[0];if(Math.abs(f)-t.majorRadius>e)return[];if(Math.abs(Math.abs(f)-t.majorRadius)<e)return u([[f,0]]);const P=t.minorRadius*Math.sqrt(1-f*f/o),b=[f,P],S=[f,-P];return u([b,S])}const m=o*l+a-c;if(m<-e)return[];const w=o*l+a;if(Math.abs(m)<e){const f=-(o*s*r)/w,P=a*r/w;return u([[f,P]])}const g=Math.sqrt(m),p=[-(o*s*r+h*g)/w,(a*r-h*s*g)/w],M=[-(o*s*r-h*g)/w,(a*r+h*s*g)/w];return u([p,M])}function Qn(i,t){const e=Math.max(i.precision,t.precision),n=i.coefficients,s=n.x2,r=n.xy,o=n.y2,a=n.x,h=n.y,l=n.c,c=t.coefficients,u=c.x2,m=c.xy,w=c.y2,g=c.x,p=c.y,M=c.c,f={z0:l*s*g*g+s*s*M*M-a*s*g*M+u*u*l*l-2*s*M*u*l-a*g*u*l+u*a*a*M,z1:p*a*a*u-M*g*s*r-2*s*M*u*h-l*u*m*a+2*g*m*s*l+2*p*M*s*s+g*g*s*h-p*g*s*a-2*s*p*u*l-l*u*g*r+2*l*h*u*u-M*m*s*a-h*u*g*a+2*M*r*u*a,z2:p*p*s*s+2*w*M*s*s-h*u*g*r+M*u*r*r-h*u*m*a-M*m*s*r-2*s*p*u*h+2*g*m*s*h-w*g*s*a-2*s*w*u*l+m*m*s*l+2*p*r*u*a+h*h*u*u-o*u*g*a-p*m*s*a+2*l*o*u*u-l*u*m*r+w*a*a*u+g*g*s*o-p*g*s*r-2*s*M*u*o,z3:-2*s*u*o*p+p*u*r*r+2*w*r*u*a-o*u*m*a+m*m*s*h-p*m*s*r-2*s*w*u*h-h*u*m*r-w*m*s*a+2*p*w*s*s+2*h*o*u*u-o*u*g*r+2*g*m*s*o-w*g*s*r,z4:s*s*w*w-2*s*w*u*o+u*u*o*o-r*s*m*w-r*m*u*o+r*r*u*w+o*s*m*m},P=Je([f.z0,f.z1,f.z2,f.z3,f.z4],e).flatMap(b=>{const S=s*m*b+s*g-u*r*b-u*a;if(S)return[[-(s*M+s*w*b*b-u*o*b*b+s*p*b-u*h*b-u*l)/S,b]];const C=r*b+a,V=-C/(2*s),v=o*b*b+h*b+l,R=C*C/(4*s*s)-v/s;if(Math.abs(R)<e)return[[V,b]];if(R>0){const j=Math.sqrt(R);return[[V+j,b],[V-j,b]]}return[]});return At(P,e)}function Gn(i,t){return Qn(i,t).filter(e=>i.isOnSegment(e)&&t.isOnSegment(e))}const Mr=i=>{const{firstPoint:t,lastPoint:e,center:n,majorRadius:s,minorRadius:r,tiltAngle:o,clockwise:a}=i;return new ht(e,t,n,s,r,o,a,{ignoreChecks:!0,angleUnits:"rad"})},yr=(i,t)=>{if(i.isSame(t))return[i];const e=(s,r)=>new ht(s,r,i.center,i.majorRadius,i.minorRadius,i.tiltAngle,i.clockwise,{ignoreChecks:!0,angleUnits:"rad"}),n=At([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(s=>s!==null)).sort((s,r)=>i.pointToParam(s)-i.pointToParam(r));if(n.length===0)return[];if(n.length===1)return[];if(n.length===2)return i.isSame(Mr(t))?[]:[e(n[0],n[1])];if(n.length===3){const s=k(n[0],t.lastPoint)||k(n[0],t.firstPoint)?1:0;return[e(n[0+s],n[1+s])]}else if(n.length===4)return[e(n[0],n[1]),e(n[2],n[3])];throw new Error("Bug in the ellipse arc ellipse arc overlap algorithm")};function br(i,t,e=!1){const n=Math.max(i.precision,t.precision);return k(i.center,t.center)&&Math.abs(i.majorRadius-t.majorRadius)<n&&Math.abs(i.minorRadius-t.minorRadius)<n&&(Math.abs(i.tiltAngle-t.tiltAngle)<n||Math.abs(Math.abs(i.tiltAngle-t.tiltAngle)-Math.PI)<n)?e?yr(i,t):[]:Qn(i,t).filter(s=>i.isOnSegment(s)&&t.isOnSegment(s))}function Jn(i,t){const[e,n]=i.firstPoint,[s,r]=i.lastPoint,o=new ft().translate(-e,-n).rotate(-Math.atan2(r-n,s-e)),a=o.clone().inverse(),h=t.transform(o);return h.paramsAtY(0).map(l=>h.paramPoint(l)).map(l=>a.transform(l)).filter(l=>i.isOnSegment(l))}const xr=(i,t=1e-9)=>{let e=i;return Math.abs(i)<t&&(e=0),e.toFixed(-Math.log10(t))};function Zn(i,t=1e-9){return Array.from(new Map(i.map(e=>[xr(e,t),e])).values())}const vr=(i,t)=>{const[[e,n,s,r],[o,a,h,l]]=t.polynomialCoefficients,c=i.coefficients,u=c.x2,m=c.xy,w=c.y2,g=c.x,p=c.y,M=c.c,f=e*e,P=n*n,b=s*s,S=r*r,C=o*o,V=a*a,v=h*h,R=l*l,j=M+g*e+u*f+p*o+m*e*o+w*C,d=g*n+2*u*e*n+m*n*o+p*a+m*e*a+2*w*o*a,x=u*P+g*s+2*u*e*s+m*s*o+m*n*a+w*V+p*h+m*e*h+2*w*o*h,A=2*u*n*s+g*r+2*u*e*r+m*r*o+m*s*a+m*n*h+2*w*a*h+p*l+m*e*l+2*w*o*l,y=u*b+2*u*n*r+m*r*a+m*s*h+w*v+m*n*l+2*w*a*l,I=2*u*s*r+m*r*h+m*s*l+2*w*h*l,$=u*S+m*r*l+w*R;return[j,d,x,A,y,I,$]};function Kn(i,t){const e=Math.max(i.precision,t.precision),n=vr(i,t),s=Je(n,e).filter(r=>r>=-t.precision&&r<=1+t.precision);return Zn(s,e).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}const Ar=(i,t)=>{const[[e,n,s],[r,o,a]]=t.polynomialCoefficients,h=i.coefficients,l=h.x2,c=h.xy,u=h.y2,m=h.x,w=h.y,g=h.c,p=e*e,M=n*n,f=s*s,P=r*r,b=o*o,S=a*a,C=l*p+c*e*r+u*P+m*e+w*r+g,V=2*l*e*n+c*e*o+c*n*r+2*u*r*o+m*n+w*o,v=2*l*e*s+l*M+c*e*a+c*n*o+c*s*r+2*u*r*a+u*b+m*s+w*a,R=2*l*n*s+c*n*a+c*s*o+2*u*o*a,j=l*f+c*s*a+u*S;return[C,V,v,R,j]};function ti(i,t){const e=Math.max(i.precision,t.precision),n=Ar(i,t),s=or(...n).filter(r=>r>=-t.precision&&r<=1+t.precision);return Zn(s,e).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}function ct(i,{firstPoint:t,lastPoint:e},n=1e-9){const s=q(e,t);return Math.abs(s[0])<n?s[1]>0?t[0]-i[0]:i[0]-t[0]:Math.abs(s[1])<n?s[0]>0?i[1]-t[1]:t[1]-i[1]:vt(s,q(i,t))/Qi(s)}let tn=class{constructor(t,e,n,s){this.firstPoint=t,this.lastPoint=e,this.negativeThickness=n,this.positiveThickness=s}get width(){return this.positiveThickness-this.negativeThickness}};const Er=3/4,Sr=4/9;function kr(i){const t=ct(i.firstControlPoint,i),e=ct(i.lastControlPoint,i),n=t*e>0?Er:Sr;return new tn(i.firstPoint,i.lastPoint,n*Math.min(0,t,e),n*Math.max(0,t,e))}function Rr(i){const t=ct(i.controlPoint,i);return new tn(i.firstPoint,i.lastPoint,Math.min(0,t/2),Math.max(0,t/2))}function Cr(i){if(i instanceof ot)return kr(i);if(i instanceof et)return Rr(i);throw new Error("Not implemented")}function Tr(i){const t=i.paramPoint(.5),e=tt(q(t,i.firstPoint)),n=X(t,e),s={firstPoint:t,lastPoint:n},r=[ct(i.firstPoint,s),ct(i.lastPoint,s)];return i instanceof ot?r.push(ct(i.firstControlPoint,s),ct(i.lastControlPoint,s)):i instanceof et&&r.push(ct(i.controlPoint,s)),new tn(t,n,Math.min(...r),Math.max(...r))}function ei(i,t){const e=[];for(let n=1;n<i.length;n++){const s=i[n];if(s[1]===t){e.push(s[0]);continue}const r=i[n-1],o=t-r[1],a=t-s[1];if(o*a<0){e.push(r[0]+(t-r[1])*(s[0]-r[0])/(s[1]-r[1]));continue}}return e}class se{constructor(t,e){this.from=t,this.to=e}get size(){return this.from==="start"?this.to==="end"?1:this.to:this.to==="end"?1-this.from:Math.abs(this.from-this.to)}clipCurve(t){return this.from==="start"?this.to==="end"?t:t.splitAtParameters([this.to])[0]:this.to==="end"?t.splitAtParameters([this.from])[1]:t.splitAtParameters([this.from,this.to])[1]}}function Ir(i,t){if(i instanceof ot)return new Fr([ct(i.firstPoint,t),ct(i.firstControlPoint,t),ct(i.lastControlPoint,t),ct(i.lastPoint,t)]);if(i instanceof et)return new $r([ct(i.firstPoint,t),ct(i.controlPoint,t),ct(i.lastPoint,t)]);throw new Error("Not implemented")}class $r{constructor(t){ut(this,"topHull",[]),ut(this,"bottomHull",[]),this.distances=t;const[e,n,s]=t,r=[0,e],o=[1/2,n],a=[1,s],h=s-e,l=e;n-(h*(1/2)+l)>0?(this.topHull=[r,o,a],this.bottomHull=[r,a]):(this.topHull=[r,a],this.bottomHull=[r,o,a])}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[2]}}class Fr{constructor(t){ut(this,"topHull",[]),ut(this,"bottomHull",[]),this.distances=t;const[e,n,s,r]=t,o=[0,e],a=[1/3,n],h=[2/3,s],l=[1,r],c=r-e,u=e,m=n-(c*(1/3)+u),w=s-(c*(2/3)+u);let g=null,p=null;if(m*w<0)g=[o,a,l],p=[o,h,l];else{const M=m/w;M>=2?(g=[o,a,l],p=[o,l]):M<=.5?(g=[o,h,l],p=[o,l]):(g=[o,a,h,l],p=[o,l])}m<0&&([g,p]=[p,g]),this.topHull=g,this.bottomHull=p}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[3]}}function ni(i,t){const e=Ir(t,i),n=ei(e.topHull,i.negativeThickness),s=ei(e.bottomHull,i.positiveThickness),r=e.endDistance>=i.negativeThickness&&e.endDistance<=i.positiveThickness;if(!n.length&&!s.length)return r?new se("start","end"):null;if(n.length===1&&s.length===1)return new se(n[0],s[0]);if(n.length===2&&s.length===2)throw new Error("Bug in the clipping algorithm, unexpected number of crossing points");const o=n.length?n:s;return o.length===2?new se(o[0],o[1]):r?new se(o[0],"end"):new se("start",o[0])}function ii(i,t){const e=Cr(i),n=ni(e,t);if(!n)return null;const s=Tr(i),r=ni(s,t);return r?n.size>r.size?r.clipCurve(t):n.clipCurve(t):null}const ke=i=>i instanceof et?xt(q(i.controlPoint,i.firstPoint))+xt(q(i.controlPoint,i.lastPoint)):xt(q(i.firstControlPoint,i.firstPoint))+xt(q(i.lastControlPoint,i.firstControlPoint))+xt(q(i.lastControlPoint,i.lastPoint));function Vt(i,t,e=1e-9,{maxIterations:n=100}={}){const s=Math.max(e*e,Number.EPSILON*10);let r=i,o=t,a=ke(r),h=ke(o);for(let l=0;l<n;l++){const c=a>s?ii(o,r):r;if(!c)return[];const u=ke(c),m=h>s?ii(c,o):o;if(!m)return[];const w=ke(m);if(u<=s&&w<=s)return[c.boundingBox.intersection(m.boundingBox).center];if(k(c.firstPoint,c.lastPoint)&&m.isOnSegment(c.firstPoint))return[c.firstPoint];if(k(m.firstPoint,m.lastPoint)&&c.isOnSegment(m.firstPoint))return[m.firstPoint];if(u>.8*a&&w>.8*h)if(u/a>w/h){const[g,p]=c.splitAtParameters([.5]);return At([...Vt(g,m,e,{maxIterations:n-l}),...Vt(p,m,e,{maxIterations:n-l})],e)}else{const[g,p]=m.splitAtParameters([.5]);return At([...Vt(c,g,e,{maxIterations:n-l}),...Vt(c,p,e,{maxIterations:n-l})],e)}r=c,o=m,a=u,h=w}throw new Error("Bézier clip: Maximum number of iterations reached")}function _r(i,t){const e=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([n,s])=>{s.isOnSegment(n)&&e.push(n)}),e.length<2)return null;if(e.length===2)return[i.splitAt(e)[1]];if(e.length===3)return k(e[0],i.firstPoint)&&k(e[1],i.lastPoint)?[i]:[t];if(e.length===4)return[i]}function Lr(i,t,e=!1){const n=Math.max(i.precision,t.precision);if(e){const s=_r(i,t);if(s)return s}return Vt(i,t,n)}function Br(i,t){const e=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([n,s])=>{s.isOnSegment(n)&&e.push(n)}),e.length<2)return null;if(e.length===2)return[i.splitAt(e)[1]];if(e.length===3)return k(e[0],i.firstPoint)&&k(e[1],i.lastPoint)?[i]:[t];if(e.length===4)return[i]}function Vr(i,t,e=!1){const n=Math.max(i.precision,t.precision);if(e){const s=Br(i,t);if(s)return s}return Vt(i,t,n)}function jr(i,t,e){if(i instanceof D&&t instanceof D){const n=kn(i,t,!1,e);return n===null?[]:[n]}if(i instanceof D&&t instanceof z)return Se(i,t,e);if(i instanceof z&&t instanceof D)return Se(t,i,e);if(i instanceof z&&t instanceof z)return Hn(i,t,!1,e);throw new Error("Not implemented")}function re(i,t,e){if(i instanceof D&&t instanceof D){const n=kn(i,t,!0,e);return n===null?{intersections:[],overlaps:[],count:0}:n instanceof D?{intersections:[],overlaps:[n],count:1}:{intersections:[n],overlaps:[],count:1}}if(!i.boundingBox.overlaps(t.boundingBox))return{intersections:[],overlaps:[],count:0};if(i instanceof D&&t instanceof z){const n=Se(i,t,e);return{intersections:n,overlaps:[],count:n.length}}if(i instanceof z&&t instanceof D){const n=Se(t,i,e);return{intersections:n,overlaps:[],count:n.length}}if(i instanceof z&&t instanceof z){const n=Hn(i,t,!0,e);return n.length?n[0]instanceof z?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof D&&t instanceof ht){const n=Ke(i,t,e);return{intersections:n,overlaps:[],count:n.length}}if(t instanceof D&&i instanceof ht){const n=Ke(t,i,e);return{intersections:n,overlaps:[],count:n.length}}if(i instanceof z&&t instanceof ht){const n=Gn(i,t);return{intersections:n,overlaps:[],count:n.length}}if(t instanceof z&&i instanceof ht){const n=Gn(t,i);return{intersections:n,overlaps:[],count:n.length}}if(i instanceof ht&&t instanceof ht){const n=br(i,t,!0);return n.length?n[0]instanceof ht?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof D&&(t instanceof ot||t instanceof et)){const n=Jn(i,t);return{intersections:n,overlaps:[],count:n.length}}if(t instanceof D&&(i instanceof ot||i instanceof et)){const n=Jn(t,i);return{intersections:n,overlaps:[],count:n.length}}if((i instanceof z||i instanceof ht)&&t instanceof et){const n=ti(i,t);return{intersections:n,overlaps:[],count:n.length}}if((t instanceof z||t instanceof ht)&&i instanceof et){const n=ti(t,i);return{intersections:n,overlaps:[],count:n.length}}if((i instanceof z||i instanceof ht)&&t instanceof ot){const n=Kn(i,t);return{intersections:n,overlaps:[],count:n.length}}if((t instanceof z||t instanceof ht)&&i instanceof ot){const n=Kn(t,i);return{intersections:n,overlaps:[],count:n.length}}if(i instanceof et&&t instanceof et){const n=Vr(i,t);return n.length?n[0]instanceof et?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof et&&t instanceof ot||t instanceof et&&i instanceof ot){const n=Vt(i,t);return{intersections:n,overlaps:[],count:n.length}}if(i instanceof ot&&t instanceof ot){const n=Lr(i,t);return n.length?n[0]instanceof ot?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}throw new Error("Not implemented")}function si(i){const t=[];for(let e=0;e<i;e++)for(let n=0;n<=e;n++)t.push([e,n]);return t}function*en(i){for(const[t,e]of si(i.length))t!==e&&(yield[i[t],i[e]])}class ri extends _t{constructor(t,{ignoreChecks:e=!1}={}){super(),ut(this,"segments"),ut(this,"_boundingBox",null),e||oi(t),this.segments=t}get repr(){return this.segments.map(t=>t.repr).join(`
|
|
1
|
+
(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} {
|
|
2
|
+
${we}[
|
|
3
|
+
${jn}${As(i,e,n,s,r)}
|
|
4
|
+
${we}]
|
|
5
|
+
${we}rows: ${i.rows}
|
|
6
|
+
${we}columns: ${i.columns}
|
|
7
|
+
}`}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(`
|
|
8
|
+
${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.`);function a(u,m,w){let g=u.rows,p=u.columns;if(g===m&&p===w)return u;{let M=j.zeros(m,w);return M=M.setSubMatrix(u,0,0),M}}let h=Math.max(n,r),l=Math.max(s,o);e=a(e,h,l),t=a(t,h,l);function c(u,m,w,g){if(w<=512||g<=512)return u.mmul(m);w%2===1&&g%2===1?(u=a(u,w+1,g+1),m=a(m,w+1,g+1)):w%2===1?(u=a(u,w+1,g),m=a(m,w+1,g)):g%2===1&&(u=a(u,w,g+1),m=a(m,w,g+1));let p=parseInt(u.rows/2,10),M=parseInt(u.columns/2,10),f=u.subMatrix(0,p-1,0,M-1),P=m.subMatrix(0,p-1,0,M-1),b=u.subMatrix(0,p-1,M,u.columns-1),I=m.subMatrix(0,p-1,M,m.columns-1),C=u.subMatrix(p,u.rows-1,0,M-1),V=m.subMatrix(p,m.rows-1,0,M-1),v=u.subMatrix(p,u.rows-1,M,u.columns-1),k=m.subMatrix(p,m.rows-1,M,m.columns-1),O=c(j.add(f,v),j.add(P,k),p,M),d=c(j.add(C,v),P,p,M),x=c(f,j.sub(I,k),p,M),A=c(v,j.sub(V,P),p,M),y=c(j.add(f,b),k,p,M),T=c(j.sub(C,f),j.add(P,I),p,M),$=c(j.sub(b,v),j.add(V,k),p,M),F=j.add(O,A);F.sub(y),F.add($);let Q=j.add(x,y),L=j.add(d,A),N=j.sub(O,d);N.add(x),N.add(T);let B=j.zeros(2*F.rows,2*F.columns);return B=B.setSubMatrix(F,0,0),B=B.setSubMatrix(Q,F.rows,0),B=B.setSubMatrix(L,0,F.columns),B=B.setSubMatrix(N,F.rows,F.columns),B.subMatrix(0,w-1,0,g-1)}return c(e,t,h,l)}scaleRows(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let r=0;r<this.rows;r++){const o=this.getRow(r);o.length>0&&Vn(o,{min:e,max:n,output:o}),s.setRow(r,o)}return s}scaleColumns(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let r=0;r<this.columns;r++){const o=this.getColumn(r);o.length&&Vn(o,{min:e,max:n,output:o}),s.setColumn(r,o)}return s}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let n=0;n<t;n++){let s=this.get(e,n),r=this.get(e,this.columns-1-n);this.set(e,n,r),this.set(e,this.columns-1-n,s)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let n=0;n<t;n++){let s=this.get(n,e),r=this.get(this.rows-1-n,e);this.set(n,e,r),this.set(this.rows-1-n,e,s)}return this}kroneckerProduct(t){t=E.checkMatrix(t);let e=this.rows,n=this.columns,s=t.rows,r=t.columns,o=new E(e*s,n*r);for(let a=0;a<e;a++)for(let h=0;h<n;h++)for(let l=0;l<s;l++)for(let c=0;c<r;c++)o.set(s*a+l,r*h+c,this.get(a,h)*t.get(l,c));return o}kroneckerSum(t){if(t=E.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,n=t.rows,s=this.kroneckerProduct(E.eye(n,n)),r=E.eye(e,e).kroneckerProduct(t);return s.add(r)}transpose(){let t=new E(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.set(n,e,this.get(e,n));return t}sortRows(t=zn){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=zn){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,n,s){We(this,t,e,n,s);let r=new E(e-t+1,s-n+1);for(let o=t;o<=e;o++)for(let a=n;a<=s;a++)r.set(o-t,a-n,this.get(o,a));return r}subMatrixRow(t,e,n){if(e===void 0&&(e=0),n===void 0&&(n=this.columns-1),e>n||e<0||e>=this.columns||n<0||n>=this.columns)throw new RangeError("Argument out of range");let s=new E(t.length,n-e+1);for(let r=0;r<t.length;r++)for(let o=e;o<=n;o++){if(t[r]<0||t[r]>=this.rows)throw new RangeError(`Row index out of range: ${t[r]}`);s.set(r,o-e,this.get(t[r],o))}return s}subMatrixColumn(t,e,n){if(e===void 0&&(e=0),n===void 0&&(n=this.rows-1),e>n||e<0||e>=this.rows||n<0||n>=this.rows)throw new RangeError("Argument out of range");let s=new E(n-e+1,t.length);for(let r=0;r<t.length;r++)for(let o=e;o<=n;o++){if(t[r]<0||t[r]>=this.columns)throw new RangeError(`Column index out of range: ${t[r]}`);s.set(o-e,r,this.get(o,t[r]))}return s}setSubMatrix(t,e,n){if(t=E.checkMatrix(t),t.isEmpty())return this;let s=e+t.rows-1,r=n+t.columns-1;We(this,e,s,n,r);for(let o=0;o<t.rows;o++)for(let a=0;a<t.columns;a++)this.set(e+o,n+a,t.get(o,a));return this}selection(t,e){Ye(this,t),Xe(this,e);let n=new E(t.length,e.length);for(let s=0;s<t.length;s++){let r=t[s];for(let o=0;o<e.length;o++){let a=e[o];n.set(s,o,this.get(r,a))}}return n}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let n=0;n<t;n++)e+=this.get(n,n);return e}clone(){return this.constructor.copy(this,new E(this.rows,this.columns))}static copy(t,e){for(const[n,s,r]of t.entries())e.set(n,s,r);return e}sum(t){switch(t){case"row":return Ss(this);case"column":return ks(this);case void 0:return Cs(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return Rs(this);case"column":return Ts(this);case void 0:return $s(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":{for(let n=0;n<this.rows;n++)e[n]/=this.columns;return e}case"column":{for(let n=0;n<this.columns;n++)e[n]/=this.rows;return e}case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{unbiased:n=!0,mean:s=this.mean(t)}=e;if(typeof n!="boolean")throw new TypeError("unbiased must be a boolean");switch(t){case"row":{if(!ft.isAnyArray(s))throw new TypeError("mean must be an array");return Fs(this,n,s)}case"column":{if(!ft.isAnyArray(s))throw new TypeError("mean must be an array");return _s(this,n,s)}case void 0:{if(typeof s!="number")throw new TypeError("mean must be a number");return Ls(this,n,s)}default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){typeof t=="object"&&(e=t,t=void 0);const n=this.variance(t,e);if(t===void 0)return Math.sqrt(n);for(let s=0;s<n.length;s++)n[s]=Math.sqrt(n[s]);return n}center(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{center:n=this.mean(t)}=e;switch(t){case"row":{if(!ft.isAnyArray(n))throw new TypeError("center must be an array");return Bs(this,n),this}case"column":{if(!ft.isAnyArray(n))throw new TypeError("center must be an array");return Vs(this,n),this}case void 0:{if(typeof n!="number")throw new TypeError("center must be a number");return js(this,n),this}default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");let n=e.scale;switch(t){case"row":{if(n===void 0)n=Os(this);else if(!ft.isAnyArray(n))throw new TypeError("scale must be an array");return qs(this,n),this}case"column":{if(n===void 0)n=zs(this);else if(!ft.isAnyArray(n))throw new TypeError("scale must be an array");return Ns(this,n),this}case void 0:{if(n===void 0)n=Ds(this);else if(typeof n!="number")throw new TypeError("scale must be a number");return Us(this,n),this}default:throw new Error(`invalid option: ${t}`)}}toString(t){return On(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}j.prototype.klass="Matrix",typeof Symbol<"u"&&(j.prototype[Symbol.for("nodejs.util.inspect.custom")]=vs);function zn(i,t){return i-t}function Ys(i){return i.every(t=>typeof t=="number")}j.random=j.rand,j.randomInt=j.randInt,j.diagonal=j.diag,j.prototype.diagonal=j.prototype.diag,j.identity=j.eye,j.prototype.negate=j.prototype.neg,j.prototype.tensorProduct=j.prototype.kroneckerProduct;var Me,He,ye;let E=(ye=class extends j{constructor(i,t){if(super(),xn(this,Me),_(this,"data"),ye.isMatrix(i))vn(this,Me,He).call(this,i.rows,i.columns),ye.copy(i,this);else if(Number.isInteger(i)&&i>=0)vn(this,Me,He).call(this,i,t);else if(ft.isAnyArray(i)){const e=i;if(i=e.length,t=i?e[0].length:0,typeof t!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<i;n++){if(e[n].length!==t)throw new RangeError("Inconsistent array dimensions");if(!Ys(e[n]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(e[n]))}this.rows=i,this.columns=t}else throw new TypeError("First argument must be a positive number or an array")}set(i,t,e){return this.data[i][t]=e,this}get(i,t){return this.data[i][t]}removeRow(i){return wt(this,i),this.data.splice(i,1),this.rows-=1,this}addRow(i,t){return t===void 0&&(t=i,i=this.rows),wt(this,i,!0),t=Float64Array.from(Ot(this,t)),this.data.splice(i,0,t),this.rows+=1,this}removeColumn(i){dt(this,i);for(let t=0;t<this.rows;t++){const e=new Float64Array(this.columns-1);for(let n=0;n<i;n++)e[n]=this.data[t][n];for(let n=i+1;n<this.columns;n++)e[n-1]=this.data[t][n];this.data[t]=e}return this.columns-=1,this}addColumn(i,t){typeof t>"u"&&(t=i,i=this.columns),dt(this,i,!0),t=qt(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns+1);let s=0;for(;s<i;s++)n[s]=this.data[e][s];for(n[s++]=t[e];s<this.columns+1;s++)n[s]=this.data[e][s-1];this.data[e]=n}return this.columns+=1,this}},Me=new WeakSet,He=function(i,t){if(this.data=[],Number.isInteger(t)&&t>=0)for(let e=0;e<i;e++)this.data.push(new Float64Array(t));else throw new TypeError("nColumns must be a positive integer");this.rows=i,this.columns=t},ye);Is(j,E);var ht;const Xs=class bn extends j{constructor(t){if(super(),xn(this,ht),E.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");ze(this,ht,E.copy(t,new E(t.rows,t.rows)))}else if(Number.isInteger(t)&&t>=0)ze(this,ht,new E(t,t));else if(ze(this,ht,new E(t)),!this.isSymmetric())throw new TypeError("not symmetric data")}get size(){return yt(this,ht).size}get rows(){return yt(this,ht).rows}get columns(){return yt(this,ht).columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return E.isMatrix(t)&&t.klassType==="SymmetricMatrix"}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}clone(){const t=new bn(this.diagonalSize);for(const[e,n,s]of this.upperRightEntries())t.set(e,n,s);return t}toMatrix(){return new E(this)}get(t,e){return yt(this,ht).get(t,e)}set(t,e,n){return yt(this,ht).set(t,e,n),yt(this,ht).set(e,t,n),this}removeCross(t){return yt(this,ht).removeRow(t),yt(this,ht).removeColumn(t),this}addCross(t,e){e===void 0&&(e=t,t=this.diagonalSize);const n=e.slice();return n.splice(t,1),yt(this,ht).addRow(t,n),yt(this,ht).addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[n,s]of t.entries())s||e.push(n);e.reverse();for(const n of e)this.removeCross(n);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let n=0,s=0,r=0;r<e.length;r++)e[r]=this.get(s,n),++n>=t&&(n=++s);return e}static fromCompact(t){const e=t.length,n=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const s=new bn(n);for(let r=0,o=0,a=0;a<e;a++)s.set(r,o,t[a]),++r>=n&&(r=++o);return s}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const n=this.get(t,e);yield[t,e,n],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0)yield this.get(t,e),++e>=this.diagonalSize&&(e=++t)}};ht=new WeakMap;let ee=Xs;ee.prototype.klassType="SymmetricMatrix";let Nn=class ss extends ee{static isDistanceMatrix(t){return ee.isSymmetricMatrix(t)&&t.klassSubType==="DistanceMatrix"}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,n){return t===e&&(n=0),super.set(t,e,n)}addCross(t,e){return e===void 0&&(e=t,t=this.diagonalSize),e=e.slice(),e[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new ee(this)}clone(){const t=new ss(this.diagonalSize);for(const[e,n,s]of this.upperRightEntries())e!==n&&t.set(e,n,s);return t}toCompact(){const{diagonalSize:t}=this,e=(t-1)*t/2,n=new Array(e);for(let s=1,r=0,o=0;o<n.length;o++)n[o]=this.get(r,s),++s>=t&&(s=++r+1);return n}static fromCompact(t){const e=t.length;if(e===0)return new this(0);const n=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const s=new this(n);for(let r=1,o=0,a=0;a<e;a++)s.set(r,o,t[a]),++r>=n&&(r=++o+1);return s}};Nn.prototype.klassSubType="DistanceMatrix";let Et=class extends j{constructor(t,e,n){super(),this.matrix=t,this.rows=e,this.columns=n}};class Ws extends Et{constructor(t,e){dt(t,e),super(t,t.rows,1),this.column=e}set(t,e,n){return this.matrix.set(t,this.column,n),this}get(t){return this.matrix.get(t,this.column)}}class Hs extends Et{constructor(t,e){Xe(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,n){return this.matrix.set(t,this.columnIndices[e],n),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}}class Qs extends Et{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(t,this.columns-e-1,n),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}}class Gs extends Et{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(this.rows-t-1,e,n),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}}class Js extends Et{constructor(t,e){wt(t,e),super(t,1,t.columns),this.row=e}set(t,e,n){return this.matrix.set(this.row,e,n),this}get(t,e){return this.matrix.get(this.row,e)}}class Zs extends Et{constructor(t,e){Ye(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,n){return this.matrix.set(this.rowIndices[t],e,n),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}}let be=class extends Et{constructor(t,e,n){Ye(t,e),Xe(t,n),super(t,e.length,n.length),this.rowIndices=e,this.columnIndices=n}set(t,e,n){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],n),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}};class Ks extends Et{constructor(t,e,n,s,r){We(t,e,n,s,r),super(t,n-e+1,r-s+1),this.startRow=e,this.startColumn=s}set(t,e,n){return this.matrix.set(this.startRow+t,this.startColumn+e,n),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}}class tr extends Et{constructor(t){super(t,t.columns,t.rows)}set(t,e,n){return this.matrix.set(e,t,n),this}get(t,e){return this.matrix.get(e,t)}}class Dn extends j{constructor(t,e={}){const{rows:n=1}=e;if(t.length%n!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=n,this.columns=t.length/n,this.data=t}set(t,e,n){let s=this._calculateIndex(t,e);return this.data[s]=n,this}get(t,e){let n=this._calculateIndex(t,e);return this.data[n]}_calculateIndex(t,e){return t*this.columns+e}}let gt=class extends j{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}};function er(i,t){if(ft.isAnyArray(i))return i[0]&&ft.isAnyArray(i[0])?new gt(i):new Dn(i,t);throw new Error("the argument is not an array")}let xe=class{constructor(t){t=gt.checkMatrix(t);let e=t.clone(),n=e.rows,s=e.columns,r=new Float64Array(n),o=1,a,h,l,c,u,m,w,g,p;for(a=0;a<n;a++)r[a]=a;for(g=new Float64Array(n),h=0;h<s;h++){for(a=0;a<n;a++)g[a]=e.get(a,h);for(a=0;a<n;a++){for(p=Math.min(a,h),u=0,l=0;l<p;l++)u+=e.get(a,l)*g[l];g[a]-=u,e.set(a,h,g[a])}for(c=h,a=h+1;a<n;a++)Math.abs(g[a])>Math.abs(g[c])&&(c=a);if(c!==h){for(l=0;l<s;l++)m=e.get(c,l),e.set(c,l,e.get(h,l)),e.set(h,l,m);w=r[c],r[c]=r[h],r[h]=w,o=-o}if(h<n&&e.get(h,h)!==0)for(a=h+1;a<n;a++)e.set(a,h,e.get(a,h)/e.get(h,h))}this.LU=e,this.pivotVector=r,this.pivotSign=o}isSingular(){let t=this.LU,e=t.columns;for(let n=0;n<e;n++)if(t.get(n,n)===0)return!0;return!1}solve(t){t=E.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let n=t.columns,s=t.subMatrixRow(this.pivotVector,0,n-1),r=e.columns,o,a,h;for(h=0;h<r;h++)for(o=h+1;o<r;o++)for(a=0;a<n;a++)s.set(o,a,s.get(o,a)-s.get(h,a)*e.get(o,h));for(h=r-1;h>=0;h--){for(a=0;a<n;a++)s.set(h,a,s.get(h,a)/e.get(h,h));for(o=0;o<h;o++)for(a=0;a<n;a++)s.set(o,a,s.get(o,a)-s.get(h,a)*e.get(o,h))}return s}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,n=t.columns;for(let s=0;s<n;s++)e*=t.get(s,s);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,s=new E(e,n);for(let r=0;r<e;r++)for(let o=0;o<n;o++)r>o?s.set(r,o,t.get(r,o)):r===o?s.set(r,o,1):s.set(r,o,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,s=new E(e,n);for(let r=0;r<e;r++)for(let o=0;o<n;o++)r<=o?s.set(r,o,t.get(r,o)):s.set(r,o,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}};function It(i,t){let e=0;return Math.abs(i)>Math.abs(t)?(e=t/i,Math.abs(i)*Math.sqrt(1+e*e)):t!==0?(e=i/t,Math.abs(t)*Math.sqrt(1+e*e)):0}let Qe=class{constructor(t){t=gt.checkMatrix(t);let e=t.clone(),n=t.rows,s=t.columns,r=new Float64Array(s),o,a,h,l;for(h=0;h<s;h++){let c=0;for(o=h;o<n;o++)c=It(c,e.get(o,h));if(c!==0){for(e.get(h,h)<0&&(c=-c),o=h;o<n;o++)e.set(o,h,e.get(o,h)/c);for(e.set(h,h,e.get(h,h)+1),a=h+1;a<s;a++){for(l=0,o=h;o<n;o++)l+=e.get(o,h)*e.get(o,a);for(l=-l/e.get(h,h),o=h;o<n;o++)e.set(o,a,e.get(o,a)+l*e.get(o,h))}}r[h]=-c}this.QR=e,this.Rdiag=r}solve(t){t=E.checkMatrix(t);let e=this.QR,n=e.rows;if(t.rows!==n)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let s=t.columns,r=t.clone(),o=e.columns,a,h,l,c;for(l=0;l<o;l++)for(h=0;h<s;h++){for(c=0,a=l;a<n;a++)c+=e.get(a,l)*r.get(a,h);for(c=-c/e.get(l,l),a=l;a<n;a++)r.set(a,h,r.get(a,h)+c*e.get(a,l))}for(l=o-1;l>=0;l--){for(h=0;h<s;h++)r.set(l,h,r.get(l,h)/this.Rdiag[l]);for(a=0;a<l;a++)for(h=0;h<s;h++)r.set(a,h,r.get(a,h)-r.get(l,h)*e.get(a,l))}return r.subMatrix(0,o-1,0,s-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(this.Rdiag[e]===0)return!1;return!0}get upperTriangularMatrix(){let t=this.QR,e=t.columns,n=new E(e,e),s,r;for(s=0;s<e;s++)for(r=0;r<e;r++)s<r?n.set(s,r,t.get(s,r)):s===r?n.set(s,r,this.Rdiag[s]):n.set(s,r,0);return n}get orthogonalMatrix(){let t=this.QR,e=t.rows,n=t.columns,s=new E(e,n),r,o,a,h;for(a=n-1;a>=0;a--){for(r=0;r<e;r++)s.set(r,a,0);for(s.set(a,a,1),o=a;o<n;o++)if(t.get(a,a)!==0){for(h=0,r=a;r<e;r++)h+=t.get(r,a)*s.get(r,o);for(h=-h/t.get(a,a),r=a;r<e;r++)s.set(r,o,s.get(r,o)+h*t.get(r,a))}}return s}},Nt=class{constructor(t,e={}){if(t=gt.checkMatrix(t),t.isEmpty())throw new Error("Matrix must be non-empty");let n=t.rows,s=t.columns;const{computeLeftSingularVectors:r=!0,computeRightSingularVectors:o=!0,autoTranspose:a=!1}=e;let h=!!r,l=!!o,c=!1,u;if(n<s)if(!a)u=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else{u=t.transpose(),n=u.rows,s=u.columns,c=!0;let d=h;h=l,l=d}else u=t.clone();let m=Math.min(n,s),w=Math.min(n+1,s),g=new Float64Array(w),p=new E(n,m),M=new E(s,s),f=new Float64Array(s),P=new Float64Array(n),b=new Float64Array(w);for(let d=0;d<w;d++)b[d]=d;let I=Math.min(n-1,s),C=Math.max(0,Math.min(s-2,n)),V=Math.max(I,C);for(let d=0;d<V;d++){if(d<I){g[d]=0;for(let x=d;x<n;x++)g[d]=It(g[d],u.get(x,d));if(g[d]!==0){u.get(d,d)<0&&(g[d]=-g[d]);for(let x=d;x<n;x++)u.set(x,d,u.get(x,d)/g[d]);u.set(d,d,u.get(d,d)+1)}g[d]=-g[d]}for(let x=d+1;x<s;x++){if(d<I&&g[d]!==0){let A=0;for(let y=d;y<n;y++)A+=u.get(y,d)*u.get(y,x);A=-A/u.get(d,d);for(let y=d;y<n;y++)u.set(y,x,u.get(y,x)+A*u.get(y,d))}f[x]=u.get(d,x)}if(h&&d<I)for(let x=d;x<n;x++)p.set(x,d,u.get(x,d));if(d<C){f[d]=0;for(let x=d+1;x<s;x++)f[d]=It(f[d],f[x]);if(f[d]!==0){f[d+1]<0&&(f[d]=0-f[d]);for(let x=d+1;x<s;x++)f[x]/=f[d];f[d+1]+=1}if(f[d]=-f[d],d+1<n&&f[d]!==0){for(let x=d+1;x<n;x++)P[x]=0;for(let x=d+1;x<n;x++)for(let A=d+1;A<s;A++)P[x]+=f[A]*u.get(x,A);for(let x=d+1;x<s;x++){let A=-f[x]/f[d+1];for(let y=d+1;y<n;y++)u.set(y,x,u.get(y,x)+A*P[y])}}if(l)for(let x=d+1;x<s;x++)M.set(x,d,f[x])}}let v=Math.min(s,n+1);if(I<s&&(g[I]=u.get(I,I)),n<v&&(g[v-1]=0),C+1<v&&(f[C]=u.get(C,v-1)),f[v-1]=0,h){for(let d=I;d<m;d++){for(let x=0;x<n;x++)p.set(x,d,0);p.set(d,d,1)}for(let d=I-1;d>=0;d--)if(g[d]!==0){for(let x=d+1;x<m;x++){let A=0;for(let y=d;y<n;y++)A+=p.get(y,d)*p.get(y,x);A=-A/p.get(d,d);for(let y=d;y<n;y++)p.set(y,x,p.get(y,x)+A*p.get(y,d))}for(let x=d;x<n;x++)p.set(x,d,-p.get(x,d));p.set(d,d,1+p.get(d,d));for(let x=0;x<d-1;x++)p.set(x,d,0)}else{for(let x=0;x<n;x++)p.set(x,d,0);p.set(d,d,1)}}if(l)for(let d=s-1;d>=0;d--){if(d<C&&f[d]!==0)for(let x=d+1;x<s;x++){let A=0;for(let y=d+1;y<s;y++)A+=M.get(y,d)*M.get(y,x);A=-A/M.get(d+1,d);for(let y=d+1;y<s;y++)M.set(y,x,M.get(y,x)+A*M.get(y,d))}for(let x=0;x<s;x++)M.set(x,d,0);M.set(d,d,1)}let k=v-1,O=Number.EPSILON;for(;v>0;){let d,x;for(d=v-2;d>=-1&&d!==-1;d--){const A=Number.MIN_VALUE+O*Math.abs(g[d]+Math.abs(g[d+1]));if(Math.abs(f[d])<=A||Number.isNaN(f[d])){f[d]=0;break}}if(d===v-2)x=4;else{let A;for(A=v-1;A>=d&&A!==d;A--){let y=(A!==v?Math.abs(f[A]):0)+(A!==d+1?Math.abs(f[A-1]):0);if(Math.abs(g[A])<=O*y){g[A]=0;break}}A===d?x=3:A===v-1?x=1:(x=2,d=A)}switch(d++,x){case 1:{let A=f[v-2];f[v-2]=0;for(let y=v-2;y>=d;y--){let T=It(g[y],A),$=g[y]/T,F=A/T;if(g[y]=T,y!==d&&(A=-F*f[y-1],f[y-1]=$*f[y-1]),l)for(let Q=0;Q<s;Q++)T=$*M.get(Q,y)+F*M.get(Q,v-1),M.set(Q,v-1,-F*M.get(Q,y)+$*M.get(Q,v-1)),M.set(Q,y,T)}break}case 2:{let A=f[d-1];f[d-1]=0;for(let y=d;y<v;y++){let T=It(g[y],A),$=g[y]/T,F=A/T;if(g[y]=T,A=-F*f[y],f[y]=$*f[y],h)for(let Q=0;Q<n;Q++)T=$*p.get(Q,y)+F*p.get(Q,d-1),p.set(Q,d-1,-F*p.get(Q,y)+$*p.get(Q,d-1)),p.set(Q,y,T)}break}case 3:{const A=Math.max(Math.abs(g[v-1]),Math.abs(g[v-2]),Math.abs(f[v-2]),Math.abs(g[d]),Math.abs(f[d])),y=g[v-1]/A,T=g[v-2]/A,$=f[v-2]/A,F=g[d]/A,Q=f[d]/A,L=((T+y)*(T-y)+$*$)/2,N=y*$*(y*$);let B=0;(L!==0||N!==0)&&(L<0?B=0-Math.sqrt(L*L+N):B=Math.sqrt(L*L+N),B=N/(L+B));let X=(F+y)*(F-y)+B,G=F*Q;for(let R=d;R<v-1;R++){let Y=It(X,G);Y===0&&(Y=Number.MIN_VALUE);let nt=X/Y,K=G/Y;if(R!==d&&(f[R-1]=Y),X=nt*g[R]+K*f[R],f[R]=nt*f[R]-K*g[R],G=K*g[R+1],g[R+1]=nt*g[R+1],l)for(let J=0;J<s;J++)Y=nt*M.get(J,R)+K*M.get(J,R+1),M.set(J,R+1,-K*M.get(J,R)+nt*M.get(J,R+1)),M.set(J,R,Y);if(Y=It(X,G),Y===0&&(Y=Number.MIN_VALUE),nt=X/Y,K=G/Y,g[R]=Y,X=nt*f[R]+K*g[R+1],g[R+1]=-K*f[R]+nt*g[R+1],G=K*f[R+1],f[R+1]=nt*f[R+1],h&&R<n-1)for(let J=0;J<n;J++)Y=nt*p.get(J,R)+K*p.get(J,R+1),p.set(J,R+1,-K*p.get(J,R)+nt*p.get(J,R+1)),p.set(J,R,Y)}f[v-2]=X;break}case 4:{if(g[d]<=0&&(g[d]=g[d]<0?-g[d]:0,l))for(let A=0;A<=k;A++)M.set(A,d,-M.get(A,d));for(;d<k&&!(g[d]>=g[d+1]);){let A=g[d];if(g[d]=g[d+1],g[d+1]=A,l&&d<s-1)for(let y=0;y<s;y++)A=M.get(y,d+1),M.set(y,d+1,M.get(y,d)),M.set(y,d,A);if(h&&d<n-1)for(let y=0;y<n;y++)A=p.get(y,d+1),p.set(y,d+1,p.get(y,d)),p.set(y,d,A);d++}v--;break}}}if(c){let d=M;M=p,p=d}this.m=n,this.n=s,this.s=g,this.U=p,this.V=M}solve(t){let e=t,n=this.threshold,s=this.s.length,r=E.zeros(s,s);for(let m=0;m<s;m++)Math.abs(this.s[m])<=n?r.set(m,m,0):r.set(m,m,1/this.s[m]);let o=this.U,a=this.rightSingularVectors,h=a.mmul(r),l=a.rows,c=o.rows,u=E.zeros(l,c);for(let m=0;m<l;m++)for(let w=0;w<c;w++){let g=0;for(let p=0;p<s;p++)g+=h.get(m,p)*o.get(w,p);u.set(m,w,g)}return u.mmul(e)}solveForDiagonal(t){return this.solve(E.diag(t))}inverse(){let t=this.V,e=this.threshold,n=t.rows,s=t.columns,r=new E(n,this.s.length);for(let c=0;c<n;c++)for(let u=0;u<s;u++)Math.abs(this.s[u])>e&&r.set(c,u,t.get(c,u)/this.s[u]);let o=this.U,a=o.rows,h=o.columns,l=new E(n,a);for(let c=0;c<n;c++)for(let u=0;u<a;u++){let m=0;for(let w=0;w<h;w++)m+=r.get(c,w)*o.get(u,w);l.set(c,u,m)}return l}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,n=this.s;for(let s=0,r=n.length;s<r;s++)n[s]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return E.diag(this.s)}};function nr(i,t=!1){return i=gt.checkMatrix(i),t?new Nt(i).inverse():Un(i,E.eye(i.rows))}function Un(i,t,e=!1){return i=gt.checkMatrix(i),t=gt.checkMatrix(t),e?new Nt(i).solve(t):i.isSquare()?new xe(i).solve(t):new Qe(i).solve(t)}function ve(i){if(i=E.checkMatrix(i),i.isSquare()){if(i.columns===0)return 1;let t,e,n,s;if(i.columns===2)return t=i.get(0,0),e=i.get(0,1),n=i.get(1,0),s=i.get(1,1),t*s-e*n;if(i.columns===3){let r,o,a;return r=new be(i,[1,2],[1,2]),o=new be(i,[1,2],[0,2]),a=new be(i,[1,2],[0,1]),t=i.get(0,0),e=i.get(0,1),n=i.get(0,2),t*ve(r)-e*ve(o)+n*ve(a)}else return new xe(i).determinant}else throw Error("determinant can only be calculated for a square matrix")}function ir(i,t){let e=[];for(let n=0;n<i;n++)n!==t&&e.push(n);return e}function sr(i,t,e,n=1e-9,s=1e-9){if(i>s)return new Array(t.rows+1).fill(0);{let r=t.addRow(e,[0]);for(let o=0;o<r.rows;o++)Math.abs(r.get(o,0))<n&&r.set(o,0,0);return r.to1DArray()}}function rr(i,t={}){const{thresholdValue:e=1e-9,thresholdError:n=1e-9}=t;i=E.checkMatrix(i);let s=i.rows,r=new E(s,s);for(let o=0;o<s;o++){let a=E.columnVector(i.getRow(o)),h=i.subMatrixRow(ir(s,o)).transpose(),l=new Nt(h).solve(a),c=E.sub(a,h.mmul(l)).abs().max();r.setRow(o,sr(c,l,o,e,n))}return r}function or(i,t=Number.EPSILON){if(i=E.checkMatrix(i),i.isEmpty())return i.transpose();let e=new Nt(i,{autoTranspose:!0}),n=e.leftSingularVectors,s=e.rightSingularVectors,r=e.diagonal;for(let o=0;o<r.length;o++)Math.abs(r[o])>t?r[o]=1/r[o]:r[o]=0;return s.mmul(E.diag(r).mmul(n.transpose()))}function ar(i,t=i,e={}){i=new E(i);let n=!1;if(typeof t=="object"&&!E.isMatrix(t)&&!ft.isAnyArray(t)?(e=t,t=i,n=!0):t=new E(t),i.rows!==t.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:s=!0}=e;s&&(i=i.center("column"),n||(t=t.center("column")));const r=i.transpose().mmul(t);for(let o=0;o<r.rows;o++)for(let a=0;a<r.columns;a++)r.set(o,a,r.get(o,a)*(1/(i.rows-1)));return r}function hr(i,t=i,e={}){i=new E(i);let n=!1;if(typeof t=="object"&&!E.isMatrix(t)&&!ft.isAnyArray(t)?(e=t,t=i,n=!0):t=new E(t),i.rows!==t.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:s=!0,scale:r=!0}=e;s&&(i.center("column"),n||t.center("column")),r&&(i.scale("column"),n||t.scale("column"));const o=i.standardDeviation("column",{unbiased:!0}),a=n?o:t.standardDeviation("column",{unbiased:!0}),h=i.transpose().mmul(t);for(let l=0;l<h.rows;l++)for(let c=0;c<h.columns;c++)h.set(l,c,h.get(l,c)*(1/(o[l]*a[c]))*(1/(i.rows-1)));return h}let Yn=class{constructor(i,t={}){const{assumeSymmetric:e=!1}=t;if(i=gt.checkMatrix(i),!i.isSquare())throw new Error("Matrix is not a square matrix");if(i.isEmpty())throw new Error("Matrix must be non-empty");let n=i.columns,s=new E(n,n),r=new Float64Array(n),o=new Float64Array(n),a=i,h,l,c=!1;if(e?c=!0:c=i.isSymmetric(),c){for(h=0;h<n;h++)for(l=0;l<n;l++)s.set(h,l,a.get(h,l));lr(n,o,r,s),ur(n,o,r,s)}else{let u=new E(n,n),m=new Float64Array(n);for(l=0;l<n;l++)for(h=0;h<n;h++)u.set(h,l,a.get(h,l));cr(n,u,m,s),fr(n,o,r,s,u)}this.n=n,this.e=o,this.d=r,this.V=s}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let i=this.n,t=this.e,e=this.d,n=new E(i,i),s,r;for(s=0;s<i;s++){for(r=0;r<i;r++)n.set(s,r,0);n.set(s,s,e[s]),t[s]>0?n.set(s,s+1,t[s]):t[s]<0&&n.set(s,s-1,t[s])}return n}};function lr(i,t,e,n){let s,r,o,a,h,l,c,u;for(h=0;h<i;h++)e[h]=n.get(i-1,h);for(a=i-1;a>0;a--){for(u=0,o=0,l=0;l<a;l++)u=u+Math.abs(e[l]);if(u===0)for(t[a]=e[a-1],h=0;h<a;h++)e[h]=n.get(a-1,h),n.set(a,h,0),n.set(h,a,0);else{for(l=0;l<a;l++)e[l]/=u,o+=e[l]*e[l];for(s=e[a-1],r=Math.sqrt(o),s>0&&(r=-r),t[a]=u*r,o=o-s*r,e[a-1]=s-r,h=0;h<a;h++)t[h]=0;for(h=0;h<a;h++){for(s=e[h],n.set(h,a,s),r=t[h]+n.get(h,h)*s,l=h+1;l<=a-1;l++)r+=n.get(l,h)*e[l],t[l]+=n.get(l,h)*s;t[h]=r}for(s=0,h=0;h<a;h++)t[h]/=o,s+=t[h]*e[h];for(c=s/(o+o),h=0;h<a;h++)t[h]-=c*e[h];for(h=0;h<a;h++){for(s=e[h],r=t[h],l=h;l<=a-1;l++)n.set(l,h,n.get(l,h)-(s*t[l]+r*e[l]));e[h]=n.get(a-1,h),n.set(a,h,0)}}e[a]=o}for(a=0;a<i-1;a++){if(n.set(i-1,a,n.get(a,a)),n.set(a,a,1),o=e[a+1],o!==0){for(l=0;l<=a;l++)e[l]=n.get(l,a+1)/o;for(h=0;h<=a;h++){for(r=0,l=0;l<=a;l++)r+=n.get(l,a+1)*n.get(l,h);for(l=0;l<=a;l++)n.set(l,h,n.get(l,h)-r*e[l])}}for(l=0;l<=a;l++)n.set(l,a+1,0)}for(h=0;h<i;h++)e[h]=n.get(i-1,h),n.set(i-1,h,0);n.set(i-1,i-1,1),t[0]=0}function ur(i,t,e,n){let s,r,o,a,h,l,c,u,m,w,g,p,M,f,P,b;for(o=1;o<i;o++)t[o-1]=t[o];t[i-1]=0;let I=0,C=0,V=Number.EPSILON;for(l=0;l<i;l++){for(C=Math.max(C,Math.abs(e[l])+Math.abs(t[l])),c=l;c<i&&!(Math.abs(t[c])<=V*C);)c++;if(c>l)do{for(s=e[l],u=(e[l+1]-s)/(2*t[l]),m=It(u,1),u<0&&(m=-m),e[l]=t[l]/(u+m),e[l+1]=t[l]*(u+m),w=e[l+1],r=s-e[l],o=l+2;o<i;o++)e[o]-=r;for(I=I+r,u=e[c],g=1,p=g,M=g,f=t[l+1],P=0,b=0,o=c-1;o>=l;o--)for(M=p,p=g,b=P,s=g*t[o],r=g*u,m=It(u,t[o]),t[o+1]=P*m,P=t[o]/m,g=u/m,u=g*e[o]-P*s,e[o+1]=r+P*(g*s+P*e[o]),h=0;h<i;h++)r=n.get(h,o+1),n.set(h,o+1,P*n.get(h,o)+g*r),n.set(h,o,g*n.get(h,o)-P*r);u=-P*b*M*f*t[l]/w,t[l]=P*u,e[l]=g*u}while(Math.abs(t[l])>V*C);e[l]=e[l]+I,t[l]=0}for(o=0;o<i-1;o++){for(h=o,u=e[o],a=o+1;a<i;a++)e[a]<u&&(h=a,u=e[a]);if(h!==o)for(e[h]=e[o],e[o]=u,a=0;a<i;a++)u=n.get(a,o),n.set(a,o,n.get(a,h)),n.set(a,h,u)}}function cr(i,t,e,n){let s=0,r=i-1,o,a,h,l,c,u,m;for(u=s+1;u<=r-1;u++){for(m=0,l=u;l<=r;l++)m=m+Math.abs(t.get(l,u-1));if(m!==0){for(h=0,l=r;l>=u;l--)e[l]=t.get(l,u-1)/m,h+=e[l]*e[l];for(a=Math.sqrt(h),e[u]>0&&(a=-a),h=h-e[u]*a,e[u]=e[u]-a,c=u;c<i;c++){for(o=0,l=r;l>=u;l--)o+=e[l]*t.get(l,c);for(o=o/h,l=u;l<=r;l++)t.set(l,c,t.get(l,c)-o*e[l])}for(l=0;l<=r;l++){for(o=0,c=r;c>=u;c--)o+=e[c]*t.get(l,c);for(o=o/h,c=u;c<=r;c++)t.set(l,c,t.get(l,c)-o*e[c])}e[u]=m*e[u],t.set(u,u-1,m*a)}}for(l=0;l<i;l++)for(c=0;c<i;c++)n.set(l,c,l===c?1:0);for(u=r-1;u>=s+1;u--)if(t.get(u,u-1)!==0){for(l=u+1;l<=r;l++)e[l]=t.get(l,u-1);for(c=u;c<=r;c++){for(a=0,l=u;l<=r;l++)a+=e[l]*n.get(l,c);for(a=a/e[u]/t.get(u,u-1),l=u;l<=r;l++)n.set(l,c,n.get(l,c)+a*e[l])}}}function fr(i,t,e,n,s){let r=i-1,o=0,a=i-1,h=Number.EPSILON,l=0,c=0,u=0,m=0,w=0,g=0,p=0,M=0,f,P,b,I,C,V,v,k,O,d,x,A,y,T,$;for(f=0;f<i;f++)for((f<o||f>a)&&(e[f]=s.get(f,f),t[f]=0),P=Math.max(f-1,0);P<i;P++)c=c+Math.abs(s.get(f,P));for(;r>=o;){for(I=r;I>o&&(g=Math.abs(s.get(I-1,I-1))+Math.abs(s.get(I,I)),g===0&&(g=c),!(Math.abs(s.get(I,I-1))<h*g));)I--;if(I===r)s.set(r,r,s.get(r,r)+l),e[r]=s.get(r,r),t[r]=0,r--,M=0;else if(I===r-1){if(v=s.get(r,r-1)*s.get(r-1,r),u=(s.get(r-1,r-1)-s.get(r,r))/2,m=u*u+v,p=Math.sqrt(Math.abs(m)),s.set(r,r,s.get(r,r)+l),s.set(r-1,r-1,s.get(r-1,r-1)+l),k=s.get(r,r),m>=0){for(p=u>=0?u+p:u-p,e[r-1]=k+p,e[r]=e[r-1],p!==0&&(e[r]=k-v/p),t[r-1]=0,t[r]=0,k=s.get(r,r-1),g=Math.abs(k)+Math.abs(p),u=k/g,m=p/g,w=Math.sqrt(u*u+m*m),u=u/w,m=m/w,P=r-1;P<i;P++)p=s.get(r-1,P),s.set(r-1,P,m*p+u*s.get(r,P)),s.set(r,P,m*s.get(r,P)-u*p);for(f=0;f<=r;f++)p=s.get(f,r-1),s.set(f,r-1,m*p+u*s.get(f,r)),s.set(f,r,m*s.get(f,r)-u*p);for(f=o;f<=a;f++)p=n.get(f,r-1),n.set(f,r-1,m*p+u*n.get(f,r)),n.set(f,r,m*n.get(f,r)-u*p)}else e[r-1]=k+u,e[r]=k+u,t[r-1]=p,t[r]=-p;r=r-2,M=0}else{if(k=s.get(r,r),O=0,v=0,I<r&&(O=s.get(r-1,r-1),v=s.get(r,r-1)*s.get(r-1,r)),M===10){for(l+=k,f=o;f<=r;f++)s.set(f,f,s.get(f,f)-k);g=Math.abs(s.get(r,r-1))+Math.abs(s.get(r-1,r-2)),k=O=.75*g,v=-.4375*g*g}if(M===30&&(g=(O-k)/2,g=g*g+v,g>0)){for(g=Math.sqrt(g),O<k&&(g=-g),g=k-v/((O-k)/2+g),f=o;f<=r;f++)s.set(f,f,s.get(f,f)-g);l+=g,k=O=v=.964}for(M=M+1,C=r-2;C>=I&&(p=s.get(C,C),w=k-p,g=O-p,u=(w*g-v)/s.get(C+1,C)+s.get(C,C+1),m=s.get(C+1,C+1)-p-w-g,w=s.get(C+2,C+1),g=Math.abs(u)+Math.abs(m)+Math.abs(w),u=u/g,m=m/g,w=w/g,!(C===I||Math.abs(s.get(C,C-1))*(Math.abs(m)+Math.abs(w))<h*(Math.abs(u)*(Math.abs(s.get(C-1,C-1))+Math.abs(p)+Math.abs(s.get(C+1,C+1))))));)C--;for(f=C+2;f<=r;f++)s.set(f,f-2,0),f>C+2&&s.set(f,f-3,0);for(b=C;b<=r-1&&(T=b!==r-1,b!==C&&(u=s.get(b,b-1),m=s.get(b+1,b-1),w=T?s.get(b+2,b-1):0,k=Math.abs(u)+Math.abs(m)+Math.abs(w),k!==0&&(u=u/k,m=m/k,w=w/k)),k!==0);b++)if(g=Math.sqrt(u*u+m*m+w*w),u<0&&(g=-g),g!==0){for(b!==C?s.set(b,b-1,-g*k):I!==C&&s.set(b,b-1,-s.get(b,b-1)),u=u+g,k=u/g,O=m/g,p=w/g,m=m/u,w=w/u,P=b;P<i;P++)u=s.get(b,P)+m*s.get(b+1,P),T&&(u=u+w*s.get(b+2,P),s.set(b+2,P,s.get(b+2,P)-u*p)),s.set(b,P,s.get(b,P)-u*k),s.set(b+1,P,s.get(b+1,P)-u*O);for(f=0;f<=Math.min(r,b+3);f++)u=k*s.get(f,b)+O*s.get(f,b+1),T&&(u=u+p*s.get(f,b+2),s.set(f,b+2,s.get(f,b+2)-u*w)),s.set(f,b,s.get(f,b)-u),s.set(f,b+1,s.get(f,b+1)-u*m);for(f=o;f<=a;f++)u=k*n.get(f,b)+O*n.get(f,b+1),T&&(u=u+p*n.get(f,b+2),n.set(f,b+2,n.get(f,b+2)-u*w)),n.set(f,b,n.get(f,b)-u),n.set(f,b+1,n.get(f,b+1)-u*m)}}}if(c!==0){for(r=i-1;r>=0;r--)if(u=e[r],m=t[r],m===0)for(I=r,s.set(r,r,1),f=r-1;f>=0;f--){for(v=s.get(f,f)-u,w=0,P=I;P<=r;P++)w=w+s.get(f,P)*s.get(P,r);if(t[f]<0)p=v,g=w;else if(I=f,t[f]===0?s.set(f,r,v!==0?-w/v:-w/(h*c)):(k=s.get(f,f+1),O=s.get(f+1,f),m=(e[f]-u)*(e[f]-u)+t[f]*t[f],V=(k*g-p*w)/m,s.set(f,r,V),s.set(f+1,r,Math.abs(k)>Math.abs(p)?(-w-v*V)/k:(-g-O*V)/p)),V=Math.abs(s.get(f,r)),h*V*V>1)for(P=f;P<=r;P++)s.set(P,r,s.get(P,r)/V)}else if(m<0)for(I=r-1,Math.abs(s.get(r,r-1))>Math.abs(s.get(r-1,r))?(s.set(r-1,r-1,m/s.get(r,r-1)),s.set(r-1,r,-(s.get(r,r)-u)/s.get(r,r-1))):($=Ae(0,-s.get(r-1,r),s.get(r-1,r-1)-u,m),s.set(r-1,r-1,$[0]),s.set(r-1,r,$[1])),s.set(r,r-1,0),s.set(r,r,1),f=r-2;f>=0;f--){for(d=0,x=0,P=I;P<=r;P++)d=d+s.get(f,P)*s.get(P,r-1),x=x+s.get(f,P)*s.get(P,r);if(v=s.get(f,f)-u,t[f]<0)p=v,w=d,g=x;else if(I=f,t[f]===0?($=Ae(-d,-x,v,m),s.set(f,r-1,$[0]),s.set(f,r,$[1])):(k=s.get(f,f+1),O=s.get(f+1,f),A=(e[f]-u)*(e[f]-u)+t[f]*t[f]-m*m,y=(e[f]-u)*2*m,A===0&&y===0&&(A=h*c*(Math.abs(v)+Math.abs(m)+Math.abs(k)+Math.abs(O)+Math.abs(p))),$=Ae(k*w-p*d+m*x,k*g-p*x-m*d,A,y),s.set(f,r-1,$[0]),s.set(f,r,$[1]),Math.abs(k)>Math.abs(p)+Math.abs(m)?(s.set(f+1,r-1,(-d-v*s.get(f,r-1)+m*s.get(f,r))/k),s.set(f+1,r,(-x-v*s.get(f,r)-m*s.get(f,r-1))/k)):($=Ae(-w-O*s.get(f,r-1),-g-O*s.get(f,r),p,m),s.set(f+1,r-1,$[0]),s.set(f+1,r,$[1]))),V=Math.max(Math.abs(s.get(f,r-1)),Math.abs(s.get(f,r))),h*V*V>1)for(P=f;P<=r;P++)s.set(P,r-1,s.get(P,r-1)/V),s.set(P,r,s.get(P,r)/V)}for(f=0;f<i;f++)if(f<o||f>a)for(P=f;P<i;P++)n.set(f,P,s.get(f,P));for(P=i-1;P>=o;P--)for(f=o;f<=a;f++){for(p=0,b=o;b<=Math.min(P,a);b++)p=p+n.get(f,b)*s.get(b,P);n.set(f,P,p)}}}function Ae(i,t,e,n){let s,r;return Math.abs(e)>Math.abs(n)?(s=n/e,r=e+s*n,[(i+s*t)/r,(t-s*i)/r]):(s=e/n,r=n+s*e,[(s*i+t)/r,(s*t-i)/r])}class Xn{constructor(t){if(t=gt.checkMatrix(t),!t.isSymmetric())throw new Error("Matrix is not symmetric");let e=t,n=e.rows,s=new E(n,n),r=!0,o,a,h;for(a=0;a<n;a++){let l=0;for(h=0;h<a;h++){let c=0;for(o=0;o<h;o++)c+=s.get(h,o)*s.get(a,o);c=(e.get(a,h)-c)/s.get(h,h),s.set(a,h,c),l=l+c*c}for(l=e.get(a,a)-l,r&&(r=l>0),s.set(a,a,Math.sqrt(Math.max(l,0))),h=a+1;h<n;h++)s.set(a,h,0)}this.L=s,this.positiveDefinite=r}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=gt.checkMatrix(t);let e=this.L,n=e.rows;if(t.rows!==n)throw new Error("Matrix dimensions do not match");if(this.isPositiveDefinite()===!1)throw new Error("Matrix is not positive definite");let s=t.columns,r=t.clone(),o,a,h;for(h=0;h<n;h++)for(a=0;a<s;a++){for(o=0;o<h;o++)r.set(h,a,r.get(h,a)-r.get(o,a)*e.get(h,o));r.set(h,a,r.get(h,a)/e.get(h,h))}for(h=n-1;h>=0;h--)for(a=0;a<s;a++){for(o=h+1;o<n;o++)r.set(h,a,r.get(h,a)-r.get(o,a)*e.get(o,h));r.set(h,a,r.get(h,a)/e.get(h,h))}return r}get lowerTriangularMatrix(){return this.L}}class Wn{constructor(t,e={}){t=gt.checkMatrix(t);let{Y:n}=e;const{scaleScores:s=!1,maxIterations:r=1e3,terminationCriteria:o=1e-10}=e;let a;if(n){if(ft.isAnyArray(n)&&typeof n[0]=="number"?n=E.columnVector(n):n=gt.checkMatrix(n),n.rows!==t.rows)throw new Error("Y should have the same number of rows as X");a=n.getColumnVector(0)}else a=t.getColumnVector(0);let h=1,l,c,u,m;for(let w=0;w<r&&h>o;w++)u=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),u=u.div(u.norm()),l=t.mmul(u).div(u.transpose().mmul(u).get(0,0)),w>0&&(h=l.clone().sub(m).pow(2).sum()),m=l.clone(),n?(c=n.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0)),c=c.div(c.norm()),a=n.mmul(c).div(c.transpose().mmul(c).get(0,0))):a=l;if(n){let w=t.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0));w=w.div(w.norm());let g=t.clone().sub(l.clone().mmul(w.transpose())),p=a.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0)),M=n.clone().sub(l.clone().mulS(p.get(0,0)).mmul(c.transpose()));this.t=l,this.p=w.transpose(),this.w=u.transpose(),this.q=c,this.u=a,this.s=l.transpose().mmul(l),this.xResidual=g,this.yResidual=M,this.betas=p}else this.w=u.transpose(),this.s=l.transpose().mmul(l).sqrt(),s?this.t=l.clone().div(this.s.get(0,0)):this.t=l,this.xResidual=t.sub(l.mmul(u.transpose()))}}z.AbstractMatrix=j,z.CHO=Xn,z.CholeskyDecomposition=Xn,z.DistanceMatrix=Nn,z.EVD=Yn;var mr=z.EigenvalueDecomposition=Yn;z.LU=xe,z.LuDecomposition=xe;var gr=z.Matrix=E;z.MatrixColumnSelectionView=Hs,z.MatrixColumnView=Ws,z.MatrixFlipColumnView=Qs,z.MatrixFlipRowView=Gs,z.MatrixRowSelectionView=Zs,z.MatrixRowView=Js,z.MatrixSelectionView=be,z.MatrixSubView=Ks,z.MatrixTransposeView=tr,z.NIPALS=Wn,z.Nipals=Wn,z.QR=Qe,z.QrDecomposition=Qe,z.SVD=Nt,z.SingularValueDecomposition=Nt,z.SymmetricMatrix=ee,z.WrapperMatrix1D=Dn,z.WrapperMatrix2D=gt,z.correlation=hr,z.covariance=ar;var Hn=z.default=E;z.determinant=ve,z.inverse=nr,z.linearDependencies=rr,z.pseudoInverse=or,z.solve=Un,z.wrap=er;const pr=mr,wr=gr;Hn.Matrix&&Hn.Matrix;function ne(i,t,e){const n=i/e,s=t/e;if(!(isFinite(n)&&isFinite(s))){const h=-i/t;return isFinite(h)?[h]:i==0&&t==0?[0]:[]}const r=s*s-4*n;let o=0;if(isFinite(r)){if(r<0)return[];if(r==0)return[-.5*s];o=-.5*(s+Dt(Math.sqrt(r),s))}else o=-s;const a=n/o;return isFinite(a)?a>o?[o,a]:[a,o]:[o]}function Qn(i,t,e,n){const s=e/(3*n),r=t/(3*n),o=i/n;if(!(isFinite(o)&&isFinite(r)&&isFinite(s)))return ne(i,t,e);const a=-s*s+r,h=-r*s+o,l=s*o-r*r,c=4*a*l-h*h,u=-2*s*a+h;if(c<0){const m=Math.sqrt(-.25*c),w=-.5*u;return[Math.cbrt(w+m)+Math.cbrt(w-m)-s]}else if(c==0){const m=Dt(Math.sqrt(-a),u);return[m-s,-2*m-s]}else{const m=Math.atan2(Math.sqrt(c),-u)/3,w=Math.cos(m),g=Math.sin(m)*Math.sqrt(3),p=.5*(-w+g),M=.5*(-w-g),f=2*Math.sqrt(-a);return[f*w-s,f*p-s,f*M-s]}}function dr(i,t,e,n,s){if(s==0)return Qn(i,t,e,n);const r=n/s,o=e/s,a=t/s,h=i/s;let l=Gn(r,o,a,h,!1);if(l!==null)return l;const c=716e74;for(let u=0;u<2;u++)if(l=Gn(r/c,o/(c*c),a/(c*c*c),h/(c*c*c*c),u!=0),l!==null){for(let m=0;m<l.length;m++)l[m]*=c;return l}return[]}function Lt(i,t){return t==0?Math.abs(i):Math.abs((i-t)/t)}function Gn(i,t,e,n,s){const r=Pr(i,t,e,n,s);if(r!==null&&r.length==4){let o=[];for(let a=0;a<2;a++){const h=r[a*2],l=r[a*2+1];o=o.concat(ne(l,h,1))}return o}return null}function Pr(i,t,e,n,s){function r(L,N,B,X){const G=Lt(L+B,i),R=Lt(N+L*B+X,t),Y=Lt(N*B+L*X,e);return G+R+Y}function o(L,N,B,X){return r(L,N,B,X)+Lt(N*X,n)}const a=9*i*i-24*t,h=a>=0?-2*t/(3*i+Dt(Math.sqrt(a),i)):-.25*i,l=i+4*h,c=t+3*h*(i+2*h),u=e+h*(2*t+h*(3*i+4*h)),m=n+h*(e+h*(t+h*(i+h)));let w=0,g=0;const p=349e100;if(s){const L=l/p,N=c/p,B=u/p,X=m/p;w=L*B-4/p*X-1/3*N*N,g=(L*B-8/p*X-2/9*N*N)*(1/3)*N-B*(B/p)-L*L*X}else w=l*u-4*m-1/3*c*c,g=(l*u+8*m-2/9*c*c)*(1/3)*c-u*u-l*l*m;if(!isFinite(w)&&isFinite(g))return null;let M=Mr(w,g);s&&(M*=p);const f=i*.5,P=1/6*t+.5*M,b=e-i*P,I=2/3*t-M-f*f,C=.5*b/I,V=2*(n-P*P)/b,v=.5*b/V;let k=0,O=0,d=0;for(let L=0;L<3;L++){const N=L==1?v:I,B=L==0?C:V,X=Lt(N+f*f+2*P,t),G=Lt(2*(N*B+f*P),e),R=Lt(N*B*B+P*P,n),Y=X+G+R;(L==0||Y<d)&&(k=N,O=B,d=Y)}const x=k,A=O;let y=0,T=0,$=0,F=0;if(x<0){const L=Math.sqrt(-x);if(y=f+L,T=P+L*A,$=f-L,F=P-L*A,Math.abs(F)<Math.abs(T)?F=n/T:Math.abs(F)>Math.abs(T)&&(T=n/F),Math.abs(y)!=Math.abs($)){let N=null,B=null;if(Math.abs(y)<Math.abs($)){const G=(e-T*$)/F,R=(t-F-T)/$;N=[i-$,G,R],B=[$,$,$]}else{const G=(e-y*F)/T,R=(t-F-T)/y,Y=i-y;N=[y,y,y],B=[Y,G,R]}let X=0;for(let G=0;G<3;G++){const R=N[G],Y=B[G];if(isFinite(R)&&isFinite(Y)){const nt=r(R,T,Y,F);(G==0||nt<X)&&(y=R,$=Y,X=nt)}}}}else if(x==0){const L=n-P*P;y=f,T=P+Math.sqrt(-L),$=f,F=P-Math.sqrt(-L),Math.abs(T)>Math.abs(F)?F=n/T:Math.abs(F)>Math.abs(T)&&(T=n/F)}else return[];let Q=o(y,T,$,F);for(let L=0;L<8&&Q!=0;L++){const N=T*F-n,B=T*$+y*F-e,X=T+y*$+F-t,G=y+$-i,R=y-$,Y=T*T-T*($*R+2*F)+F*(y*R+F);if(Y==0)break;const nt=1/Y,K=F-T,J=T*$-y*F,dn=R*N+K*B+J*X-(T*K+y*J)*G,Pn=(y*R+K)*N-T*(R*B+K*X+J*G),Mn=-R*N-K*B-J*X+($*J+F*K)*G,yn=-($*R+K)*N+F*(R*B+K*X+J*G),_e=y-nt*dn,Le=T-nt*Pn,Be=$-nt*Mn,Ve=F-nt*yn,je=o(_e,Le,Be,Ve);if(je<Q)y=_e,T=Le,$=Be,F=Ve,Q=je;else break}return[y,T,$,F]}function Mr(i,t){const e=-.3333333333333333*i,n=.5*t;let s,r=null;if((Math.abs(e)>=1e102||Math.abs(n)>=1e164)&&(Math.abs(e)<Math.abs(n)?r=1-e*(e/n)*(e/n):r=Math.sign(e)*(n/e*(n/e)/e-1)),r!==null&&n==0)i>0?s=0:s=Math.sqrt(-i);else if(r!==null?r<0:n*n<e*e*e){const h=r!==null?n/e/Math.sqrt(e):n/Math.sqrt(e*e*e);s=-2*Math.sqrt(e)*Dt(Math.cos(Math.acos(Math.abs(h))*(1/3)),h)}else{let h;r!==null?Math.abs(e)<Math.abs(n)?h=-n*(1+Math.sqrt(r)):h=-n-Dt(Math.sqrt(Math.abs(e))*e*Math.sqrt(r),n):h=Math.cbrt(-n-Dt(Math.sqrt(n*n-e*e*e),n));const l=h==0?0:e/h;s=h+l}let o=s,a=(o*o+i)*o+t;if(Math.abs(a)<222045e-21*Math.max(o*o*o,i*o,t))return o;for(let h=0;h<8;h++){const l=3*o*o+i;if(l==0)break;const c=o-a/l,u=(c*c+i)*c+t;if(u==0)return c;if(Math.abs(u)>=Math.abs(a))break;o=c,a=u}return o}function Dt(i,t){const e=Math.abs(i);return t<0?-e:e}function Ge(i,t=1e-9){if(i[i.length-1]==0)return Ge(i.slice(0,-1),t);const e=new wr([i.slice(0,-1).reverse().map(s=>-s/i[i.length-1]),...Array.from({length:i.length-2},(s,r)=>{const o=new Array(i.length-1).fill(0);return o[r]=1,o})]),n=new pr(e);return n.realEigenvalues.filter((s,r)=>Math.abs(n.imaginaryEigenvalues[r])<t)}const yr=(i,t,e)=>[i[0]*(1-e)+t[0]*e,i[1]*(1-e)+t[1]*e];function br(i){const t=[];for(let e=0;e<i.length-1;e++)t.push([i[e],i[e+1]]);return t}function Je(i,t){const e=br(i).map(([n,s])=>yr(n,s,t));return i.length===2?[e]:[...Je(e,t),e]}const Jn=Symbol.for("pantograph:CubicBezier");let ot=class Ht extends Kt{constructor(t,e,n,s){super(t,e),_(this,"segmentType","CUBIC_BEZIER"),_(this,"firstControlPoint"),_(this,"lastControlPoint"),_(this,"_boundingBox",null),_(this,"_polynomialCoefficients",null),Object.defineProperty(this,Jn,{value:!0}),this.firstControlPoint=n,this.lastControlPoint=s}static isInstance(t){return!!t&&t[Jn]===!0}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,s,r]=[this.firstPoint[t],this.firstControlPoint[t],this.lastControlPoint[t],this.lastPoint[t]],o=-e+3*n-3*s+r,a=2*e-4*n+2*s,h=-e+n;return ne(h,a,o).filter(l=>l>=-this.precision&&l<=1+this.precision)}getParametersOfExtrema(){return Array.from(new Set(this._extremaInDirection(0).concat(this._extremaInDirection(1))))}get alignedCurve(){const t=[-this.firstPoint[0],-this.firstPoint[1]],e=q(this.lastPoint,this.firstPoint),n=Math.atan2(e[1],e[0]),s=new ct;return s.translate(t[0],t[1]),s.rotate(-n),this.transform(s)}getInflexionParameters(){const t=this.alignedCurve,[e,n]=t.firstControlPoint,[s,r]=t.lastControlPoint,[o,a]=t.lastPoint,h=s*n,l=o*n,c=e*r,u=o*r,m=-3*h+2*l+3*c-u,w=3*h-l-3*c,g=c-h;return ne(g,w,m).filter(p=>p>=0&&p<=1)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=Ne([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new Ht(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new Ht(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:S(this.firstPoint,t.firstPoint)&&S(this.lastPoint,t.lastPoint)&&S(this.firstControlPoint,t.firstControlPoint)&&S(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const e=Ln(n=>$t(this.paramPoint(n),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(Z(this.firstPoint,t),Z(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=1-t,n=e*e,s=t*t,r=3*n,o=6*e*t,a=3*s,h=q(this.firstControlPoint,this.firstPoint),l=q(this.lastControlPoint,this.firstControlPoint),c=q(this.lastPoint,this.lastControlPoint);return[r*h[0]+o*l[0]+a*c[0],r*h[1]+o*l[1]+a*c[1]]}secondDerivativeAt(t){const e=W(this.firstPoint,this.lastControlPoint),n=H(this.firstControlPoint,2),s=W(this.firstControlPoint,this.lastPoint),r=H(this.lastControlPoint,2),o=H(q(e,n),6*(1-t)),a=H(q(s,r),6*t);return W(o,a)}tangentAt(t){const e=this.pointToParam(t);return rt(this.gradientAt(e))}get tangentAtFirstPoint(){return rt(q(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return rt(q(this.lastPoint,this.lastControlPoint))}normalAt(t){const e=this.tangentAt(t);return tt(e)}get normalAtFirstPoint(){return tt(this.tangentAtFirstPoint)}get normalAtLastPoint(){return tt(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((r,o)=>r-o);let s={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return n.flatMap((r,o)=>{if(r-s.originalParam<this.precision)return[];let a=!1;r>1-this.precision&&(a=!0);const h=(r-s.originalParam)/(1-s.originalParam),l=Je([s.p0,s.p1,s.p2,this.lastPoint],a?1:h),c=a?this.lastPoint:(e==null?void 0:e.get(r))??l[0][0],u=l[1][0],m=l[2][0],w=l[1][1],g=l[2][2],p=new Ht(s.p0,c,m,u);return s={param:h,originalParam:r,p0:c,p1:w,p2:g},o===n.length-1&&!a?[p,new Ht(s.p0,this.lastPoint,s.p1,s.p2)]:p})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=e.map(r=>this.pointToParam(r)),s=new Map(Mt([n,e]));return this.splitAtParameters(n,s)}transform(t){return new Ht(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.firstControlPoint),t.transform(this.lastControlPoint))}paramPoint(t){const e=1-t,n=e*e,s=t*t,r=n*e,o=3*n*t,a=3*e*s,h=s*t;return[r*this.firstPoint[0]+o*this.firstControlPoint[0]+a*this.lastControlPoint[0]+h*this.lastPoint[0],r*this.firstPoint[1]+o*this.firstControlPoint[1]+a*this.lastControlPoint[1]+h*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],s=this.firstControlPoint[e],r=this.lastControlPoint[e],o=this.lastPoint[e],a=-n+3*s-3*r+o,h=3*n-6*s+3*r,l=-3*n+3*s;return[n,l,h,a]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,s,r]=this.polynomialCoefficients[1];return Qn(e-t,n,s,r).filter(o=>o>=-this.precision&&o<=1+this.precision)}pointToParam(t){if(S(t,this.firstPoint,this.precision))return 0;if(S(t,this.lastPoint,this.precision))return 1;const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve");const n=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,s=e.find(n);if(s===void 0)throw new Error("Point is not on the curve");return s}};const Zn=Symbol.for("pantograph:EllipseArc");let at=class he extends Kt{constructor(t,e,n,s,r,o,a=!1,{ignoreChecks:h=!1,angleUnits:l="deg"}={}){super(t,e),_(this,"segmentType","ELLIPSE_ARC"),_(this,"precision",1e-6),_(this,"majorRadius"),_(this,"minorRadius"),_(this,"center"),_(this,"tiltAngle"),_(this,"clockwise"),_(this,"_coefficients",null),_(this,"_boundingBox"),_(this,"_linearExentricity"),_(this,"_exentricity"),_(this,"_focals"),_(this,"_ellipseReferenceFrameTransform"),_(this,"_reverseEllipseReferenceFrameTransform"),_(this,"_rotateFromEllipseReferenceFrame"),_(this,"_firstAngle"),_(this,"_lastAngle"),_(this,"_deltaAngle"),Object.defineProperty(this,Zn,{value:!0}),this.center=n;const c=s>=r;this.majorRadius=c?s:r,this.minorRadius=c?r:s;const u=l==="deg"?o*Tt:o;if(this.tiltAngle=_t(c?u:u+Math.PI/2),this.clockwise=a,!h){if(S(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${st(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${st(e)} not on the ellipse defined by ${this.info}`);if(Math.abs(this.majorRadius-this.minorRadius)<this.precision)throw new Error("Both radii should be different, create an arc instead")}}static isInstance(t){return!!t&&t[Zn]===!0}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,n=this.majorRadius*this.majorRadius,s=this.minorRadius*this.minorRadius,r=Math.cos(this.tiltAngle),o=Math.sin(this.tiltAngle),a=n*o*o+s*r*r,h=2*(s-n)*o*r,l=n*r*r+s*o*o;this._coefficients={x2:a,xy:h,y2:l,x:-2*a*t-h*e,y:-2*l*e-h*t,c:a*t*t+h*t*e+l*e*e-n*s}}return this._coefficients}get info(){return`ELLIPSE_ARC(${st(this.firstPoint)}, ${st(this.lastPoint)}, ${st(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*De}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new he(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new he(this.firstPoint,this.lastPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}computeEllipseExtrema(){const t=Math.tan(this.tiltAngle),e=this.minorRadius/this.majorRadius,n=-Math.atan(e*t),s=Math.atan(e/t);return[n,Math.PI+n,s,Math.PI+s].map(r=>this.thetaToParam(r)).filter(r=>this.isValidParameter(r)).map(r=>this.paramPoint(r))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=Ne(e).grow(this.precision)}return this._boundingBox}get linearExentricity(){return this._linearExentricity===void 0&&(this._linearExentricity=Math.sqrt(this.majorRadius*this.majorRadius-this.minorRadius*this.minorRadius)),this._linearExentricity}get exentricity(){return this._exentricity===void 0&&(this._exentricity=this.linearExentricity/this.majorRadius),this._exentricity}get focals(){if(this._focals===void 0){const t=this.majorAxis;this._focals=[W(this.center,H(t,this.linearExentricity)),W(this.center,H(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return At(1,this.tiltAngle)}paramPoint(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1);return this.reverseEllipseReferenceFrameTransform.transform([this.majorRadius*Math.cos(e),this.minorRadius*Math.sin(e)])}pointToParam(t){if(!this.isPointOnEllipse(t))throw new Error(`Point ${st(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${st(t)} is not on segment ${this.repr}`);return e}get midPoint(){return this.paramPoint(.5)}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}isSame(t){return S(this.center,t.center)&&Math.abs(this.majorRadius-t.majorRadius)<this.precision&&Math.abs(this.minorRadius-t.minorRadius)<this.precision&&(Math.abs(this.tiltAngle-t.tiltAngle)<this.precision||Math.abs(Math.abs(this.tiltAngle-t.tiltAngle)-Math.PI)<this.precision)&&(S(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||S(this.firstPoint,t.lastPoint)&&this.clockwise!==t.clockwise)}pointTheta(t){const e=this.ellipseReferenceFrameTransform.transform(t),n=Math.atan2(e[1]/this.minorRadius,e[0]/this.majorRadius);return _t(n)}thetaToParam(t){return fe(this.firstAngle,_t(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,n]=this.focals,s=Z(t,e),r=Z(t,n);return Math.abs(2*this.majorRadius-s-r)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(S(t,this.center)?e=W(this.center,H(tt(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(Ar(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return Z(t,e);if(hs(t,this.majorAxis,this.center)){const n=_t(2*Math.PI-this.pointTheta(t)),s=this.thetaToParam(n);if(this.isValidParameter(s))return Z(t,this.paramPoint(s))}return Math.min(Z(t,this.firstPoint),Z(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new ct().translate(-this.center[0],-this.center[1]).rotate(-this.tiltAngle)),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new ct().rotate(this.tiltAngle).translate(this.center[0],this.center[1])),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new ct().rotate(this.tiltAngle)),this._rotateFromEllipseReferenceFrame}get firstAngle(){return this._firstAngle===void 0&&(this._firstAngle=this.pointTheta(this.firstPoint)),this._firstAngle}get lastAngle(){return this._lastAngle===void 0&&(this._lastAngle=this.pointTheta(this.lastPoint)),this._lastAngle}get deltaAngle(){return this._deltaAngle===void 0&&(this._deltaAngle=fe(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?tt(e):Zt(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),n=-this.majorRadius*this.deltaAngle*Math.sin(e),s=this.minorRadius*this.deltaAngle*Math.cos(e),r=this.clockwise?[-n,-s]:[n,s];return this.rotateFromEllipseReferenceFrame.transform(r)}tangentAt(t){const e=this.pointTheta(t),n=-this.majorRadius*Math.sin(e),s=this.minorRadius*Math.cos(e),r=this.clockwise?[-n,-s]:[n,s];return rt(this.rotateFromEllipseReferenceFrame.transform(r))}get tangentAtFirstPoint(){const t=-this.majorRadius*Math.sin(this.firstAngle),e=this.minorRadius*Math.cos(this.firstAngle),n=this.clockwise?[-t,-e]:[t,e];return rt(this.rotateFromEllipseReferenceFrame.transform(n))}get tangentAtLastPoint(){const t=-this.majorRadius*Math.sin(this.lastAngle),e=this.minorRadius*Math.cos(this.lastAngle),n=this.clockwise?[-t,-e]:[t,e];return rt(this.rotateFromEllipseReferenceFrame.transform(n))}transform(t){const e=t.transformAngle(this.tiltAngle),n=t.scaleFactor();return new he(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),this.majorRadius*n,this.minorRadius*n,e,t.keepsOrientation()?this.clockwise:!this.clockwise,{angleUnits:"rad"})}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 he(s.get(l)||this.paramPoint(l),s.get(h)||this.paramPoint(h),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return r=null,c})}};function xr(i,t,e,n,s,r,o){const{center:a,rx:h,ry:l}=vr(i,t,e,n,s*Tt,!r,o);return Math.abs(h-l)<1e-9?new D(i,t,a,o):new at(i,t,a,h,l,s,o)}function vr([i,t],[e,n],s,r,o,a,h){if(s<0&&(s=-s),r<0&&(r=-r),s==0||r==0)throw Error("rx and ry can not be 0");const l=Math.sin(o),c=Math.cos(o),u=(i-e)/2,m=(t-n)/2,w=(i+e)/2,g=(t+n)/2,p=c*u+l*m,M=c*m-l*u,f=p*p/(s*s)+M*M/(r*r);f>1&&(s=s*Math.sqrt(f),r=r*Math.sqrt(f));const P=s*r,b=s*M,I=r*p,C=b*b+I*I;if(!C)throw Error("start point can not be same as end point");let V=Math.sqrt(Math.abs((P*P-C)/C));a==h&&(V=-V);const v=V*b/r,k=-V*I/s,O=c*v-l*k+w,d=l*v+c*k+g;return{center:[O,d],rx:s,ry:r}}function Ar(i,t,e){const n=Math.abs(e[0]),s=Math.abs(e[1]);let r=.707,o=.707;const a=i,h=t;for(let l=0;l<3;l++){const c=a*r,u=h*o,m=(a*a-h*h)*r**3/a,w=(h*h-a*a)*o**3/h,g=c-m,p=u-w,M=n-m,f=s-w,P=Math.hypot(g,p),b=Math.hypot(M,f);r=Math.min(1,Math.max(0,(M*P/b+m)/a)),o=Math.min(1,Math.max(0,(f*P/b+w)/h));const I=Math.hypot(r,o);r/=I,o/=I}return[a*r*Math.sign(e[0]),h*o*Math.sign(e[1])]}const Kn=Symbol.for("pantograph:QuadraticBezier");let et=class Qt extends Kt{constructor(t,e,n){super(t,e),_(this,"segmentType","QUADRATIC_BEZIER"),_(this,"controlPoint"),_(this,"_boundingBox",null),_(this,"_polynomialCoefficients",null),Object.defineProperty(this,Kn,{value:!0}),this.controlPoint=n}static isInstance(t){return!!t&&t[Kn]===!0}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,s]=[this.firstPoint[t],this.controlPoint[t],this.lastPoint[t]],r=e-2*n+s;if(!r)return[];const o=(e-n)/r;return o>=-this.precision&&o<=1+this.precision?[o]:[]}getParametersOfExtrema(){return Array.from(new Set(this._extremaInDirection(0).concat(this._extremaInDirection(1))))}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=Ne([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new Qt(this.firstPoint,this.lastPoint,this.controlPoint)}reverse(){return new Qt(this.lastPoint,this.firstPoint,this.controlPoint)}isSame(t){return t.segmentType!=="QUADRATIC_BEZIER"?!1:S(this.firstPoint,t.firstPoint)&&S(this.lastPoint,t.lastPoint)&&S(this.controlPoint,t.controlPoint)}distanceFrom(t){const e=Ln(n=>$t(this.paramPoint(n),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(Z(this.firstPoint,t),Z(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=n=>{const s=this.firstPoint[n],r=this.controlPoint[n],o=this.lastPoint[n];return 2*(t*(s-2*r+o)-s+r)};return[e(0),e(1)]}secondDerivativeAt(t){return H(q(W(this.firstPoint,this.lastPoint),H(this.controlPoint,2)),2)}tangentAt(t){const e=this.pointToParam(t);return rt(this.gradientAt(e))}get tangentAtFirstPoint(){return rt(q(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return rt(q(this.lastPoint,this.controlPoint))}normalAt(t){const e=this.tangentAt(t);return tt(e)}get normalAtFirstPoint(){return tt(this.tangentAtFirstPoint)}get normalAtLastPoint(){return tt(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((r,o)=>r-o);let s={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return n.flatMap((r,o)=>{if(r-s.originalParam<this.precision)return[];let a=!1;r>1-this.precision&&(a=!0);const h=(r-s.originalParam)/(1-s.originalParam),l=Je([s.p0,s.p1,this.lastPoint],a?1:h),c=a?this.lastPoint:(e==null?void 0:e.get(r))??l[0][0],u=l[1][0],m=l[1][1],w=new Qt(s.p0,c,u);return s={param:h,originalParam:r,p0:c,p1:m},o===n.length-1&&!a?[w,new Qt(s.p0,this.lastPoint,s.p1)]:w})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=e.map(r=>this.pointToParam(r)),s=new Map(Mt([n,e]));return this.splitAtParameters(n,s)}transform(t){return new Qt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const e=1-t,n=e*e,s=2*e*t,r=t*t;return[n*this.firstPoint[0]+s*this.controlPoint[0]+r*this.lastPoint[0],n*this.firstPoint[1]+s*this.controlPoint[1]+r*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],s=this.controlPoint[e],r=this.lastPoint[e],o=n-2*s+r,a=2*(s-n);return[n,a,o]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,s]=this.polynomialCoefficients[1];return ne(e-t,n,s).filter(r=>r>=-this.precision&&r<=1+this.precision)}pointToParam(t){const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve!");const n=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,s=e.find(n);if(s===void 0)throw new Error("Point is not on the curve");return s}};var Er=Object.defineProperty,Ir=(i,t,e)=>t in i?Er(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,lt=(i,t,e)=>Ir(i,typeof t!="symbol"?t+"":t,e);function Sr(i,t){const e=q(t,i.firstPoint),n=ce(e,i.V)/i.squareLength;return i.paramPoint(n)}function Ee(i,t,e){const n=e||i.precision,s=Sr(i,t.center),r=Z(s,t.center);if(r>t.radius+n)return[];if(Math.abs(r-t.radius)<n){const u=s;return i.isOnSegment(u)&&t.isOnSegment(u)?[u]:[]}const o=[],a=Math.sqrt(t.radius*t.radius-r*r),h=i.tangentAtFirstPoint,l=W(s,H(h,a));i.isOnSegment(l)&&t.isOnSegment(l)&&o.push(l);const c=W(s,H(h,-a));return i.isOnSegment(c)&&t.isOnSegment(c)&&o.push(c),o}const kr=i=>{const{firstPoint:t,lastPoint:e,center:n,clockwise:s}=i;return new D(e,t,n,s,{ignoreChecks:!0})},Cr=(i,t)=>{if(i.isSame(t))return[i];const e=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(n=>n!==null)).sort((n,s)=>i.pointToParam(n)-i.pointToParam(s));if(e.length===0)return[];if(e.length===1)return[];if(e.length===2)return i.isSame(kr(t))?[]:[new D(e[0],e[1],i.center,i.clockwise)];if(e.length===3){const n=S(e[0],t.lastPoint)||S(e[0],t.firstPoint)?1:0;return[new D(e[0+n],e[1+n],i.center,i.clockwise)]}else if(e.length===4)return[new D(e[0],e[1],i.center,i.clockwise),new D(e[2],e[3],i.center,i.clockwise)];throw new Error("Bug in the arc arc overlap algorithm")};function ti(i,t,e=!1,n){const s=n||i.precision,r=Z(i.center,t.center),o=i.radius+t.radius;if(r>o+s)return[];const a=Math.abs(i.radius-t.radius);if(r<a-s)return[];if(r<s)return a>s?[]:e?Cr(i,t):[];const h=rt(q(t.center,i.center)),l=r>o-s;if(l||Math.abs(r-a)<s){const f=l||i.radius>t.radius?1:-1,P=W(i.center,H(h,f*i.radius));return i.isOnSegment(P)&&t.isOnSegment(P)?[P]:[]}const c=i.radius*i.radius/(2*r)-t.radius*t.radius/(2*r)+r/2,u=W(i.center,H(h,c)),m=Math.sqrt(i.radius*i.radius-c*c),w=tt(h),g=W(u,H(w,m)),p=W(u,H(w,-m)),M=[];return i.isOnSegment(g)&&t.isOnSegment(g)&&M.push(g),i.isOnSegment(p)&&t.isOnSegment(p)&&M.push(p),M}function Ze(i,t,e=1e-9){const n=i.transform(t.ellipseReferenceFrameTransform),s=n.slope,r=n.yIntercept,o=t.majorRadius*t.majorRadius,a=t.minorRadius*t.minorRadius,h=t.majorRadius*t.minorRadius,l=n.slope*n.slope,c=n.yIntercept*n.yIntercept,u=f=>f.map(P=>t.reverseEllipseReferenceFrameTransform.transform(P)).filter(P=>i.isOnSegment(P)&&t.isOnSegment(P));if(!Number.isFinite(s)){const f=n.firstPoint[0];if(Math.abs(f)-t.majorRadius>e)return[];if(Math.abs(Math.abs(f)-t.majorRadius)<e)return u([[f,0]]);const P=t.minorRadius*Math.sqrt(1-f*f/o),b=[f,P],I=[f,-P];return u([b,I])}const m=o*l+a-c;if(m<-e)return[];const w=o*l+a;if(Math.abs(m)<e){const f=-(o*s*r)/w,P=a*r/w;return u([[f,P]])}const g=Math.sqrt(m),p=[-(o*s*r+h*g)/w,(a*r-h*s*g)/w],M=[-(o*s*r-h*g)/w,(a*r+h*s*g)/w];return u([p,M])}function ei(i,t){const e=Math.max(i.precision,t.precision),n=i.coefficients,s=n.x2,r=n.xy,o=n.y2,a=n.x,h=n.y,l=n.c,c=t.coefficients,u=c.x2,m=c.xy,w=c.y2,g=c.x,p=c.y,M=c.c,f={z0:l*s*g*g+s*s*M*M-a*s*g*M+u*u*l*l-2*s*M*u*l-a*g*u*l+u*a*a*M,z1:p*a*a*u-M*g*s*r-2*s*M*u*h-l*u*m*a+2*g*m*s*l+2*p*M*s*s+g*g*s*h-p*g*s*a-2*s*p*u*l-l*u*g*r+2*l*h*u*u-M*m*s*a-h*u*g*a+2*M*r*u*a,z2:p*p*s*s+2*w*M*s*s-h*u*g*r+M*u*r*r-h*u*m*a-M*m*s*r-2*s*p*u*h+2*g*m*s*h-w*g*s*a-2*s*w*u*l+m*m*s*l+2*p*r*u*a+h*h*u*u-o*u*g*a-p*m*s*a+2*l*o*u*u-l*u*m*r+w*a*a*u+g*g*s*o-p*g*s*r-2*s*M*u*o,z3:-2*s*u*o*p+p*u*r*r+2*w*r*u*a-o*u*m*a+m*m*s*h-p*m*s*r-2*s*w*u*h-h*u*m*r-w*m*s*a+2*p*w*s*s+2*h*o*u*u-o*u*g*r+2*g*m*s*o-w*g*s*r,z4:s*s*w*w-2*s*w*u*o+u*u*o*o-r*s*m*w-r*m*u*o+r*r*u*w+o*s*m*m},P=Ge([f.z0,f.z1,f.z2,f.z3,f.z4],e).flatMap(b=>{const I=s*m*b+s*g-u*r*b-u*a;if(I)return[[-(s*M+s*w*b*b-u*o*b*b+s*p*b-u*h*b-u*l)/I,b]];const C=r*b+a,V=-C/(2*s),v=o*b*b+h*b+l,k=C*C/(4*s*s)-v/s;if(Math.abs(k)<e)return[[V,b]];if(k>0){const O=Math.sqrt(k);return[[V+O,b],[V-O,b]]}return[]});return vt(P,e)}function ni(i,t){return ei(i,t).filter(e=>i.isOnSegment(e)&&t.isOnSegment(e))}const Rr=i=>{const{firstPoint:t,lastPoint:e,center:n,majorRadius:s,minorRadius:r,tiltAngle:o,clockwise:a}=i;return new at(e,t,n,s,r,o,a,{ignoreChecks:!0,angleUnits:"rad"})},Tr=(i,t)=>{if(i.isSame(t))return[i];const e=(s,r)=>new at(s,r,i.center,i.majorRadius,i.minorRadius,i.tiltAngle,i.clockwise,{ignoreChecks:!0,angleUnits:"rad"}),n=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(s=>s!==null)).sort((s,r)=>i.pointToParam(s)-i.pointToParam(r));if(n.length===0)return[];if(n.length===1)return[];if(n.length===2)return i.isSame(Rr(t))?[]:[e(n[0],n[1])];if(n.length===3){const s=S(n[0],t.lastPoint)||S(n[0],t.firstPoint)?1:0;return[e(n[0+s],n[1+s])]}else if(n.length===4)return[e(n[0],n[1]),e(n[2],n[3])];throw new Error("Bug in the ellipse arc ellipse arc overlap algorithm")};function $r(i,t,e=!1){const n=Math.max(i.precision,t.precision);return S(i.center,t.center)&&Math.abs(i.majorRadius-t.majorRadius)<n&&Math.abs(i.minorRadius-t.minorRadius)<n&&(Math.abs(i.tiltAngle-t.tiltAngle)<n||Math.abs(Math.abs(i.tiltAngle-t.tiltAngle)-Math.PI)<n)?e?Tr(i,t):[]:ei(i,t).filter(s=>i.isOnSegment(s)&&t.isOnSegment(s))}function ii(i,t){const[e,n]=i.firstPoint,[s,r]=i.lastPoint,o=new ct().translate(-e,-n).rotate(-Math.atan2(r-n,s-e)),a=o.clone().inverse(),h=t.transform(o);return h.paramsAtY(0).map(l=>h.paramPoint(l)).map(l=>a.transform(l)).filter(l=>i.isOnSegment(l))}const Fr=(i,t=1e-9)=>{let e=i;return Math.abs(i)<t&&(e=0),e.toFixed(-Math.log10(t))};function si(i,t=1e-9){return Array.from(new Map(i.map(e=>[Fr(e,t),e])).values())}const _r=(i,t)=>{const[[e,n,s,r],[o,a,h,l]]=t.polynomialCoefficients,c=i.coefficients,u=c.x2,m=c.xy,w=c.y2,g=c.x,p=c.y,M=c.c,f=e*e,P=n*n,b=s*s,I=r*r,C=o*o,V=a*a,v=h*h,k=l*l,O=M+g*e+u*f+p*o+m*e*o+w*C,d=g*n+2*u*e*n+m*n*o+p*a+m*e*a+2*w*o*a,x=u*P+g*s+2*u*e*s+m*s*o+m*n*a+w*V+p*h+m*e*h+2*w*o*h,A=2*u*n*s+g*r+2*u*e*r+m*r*o+m*s*a+m*n*h+2*w*a*h+p*l+m*e*l+2*w*o*l,y=u*b+2*u*n*r+m*r*a+m*s*h+w*v+m*n*l+2*w*a*l,T=2*u*s*r+m*r*h+m*s*l+2*w*h*l,$=u*I+m*r*l+w*k;return[O,d,x,A,y,T,$]};function ri(i,t){const e=Math.max(i.precision,t.precision),n=_r(i,t),s=Ge(n,e).filter(r=>r>=-t.precision&&r<=1+t.precision);return si(s,e).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}const Lr=(i,t)=>{const[[e,n,s],[r,o,a]]=t.polynomialCoefficients,h=i.coefficients,l=h.x2,c=h.xy,u=h.y2,m=h.x,w=h.y,g=h.c,p=e*e,M=n*n,f=s*s,P=r*r,b=o*o,I=a*a,C=l*p+c*e*r+u*P+m*e+w*r+g,V=2*l*e*n+c*e*o+c*n*r+2*u*r*o+m*n+w*o,v=2*l*e*s+l*M+c*e*a+c*n*o+c*s*r+2*u*r*a+u*b+m*s+w*a,k=2*l*n*s+c*n*a+c*s*o+2*u*o*a,O=l*f+c*s*a+u*I;return[C,V,v,k,O]};function oi(i,t){const e=Math.max(i.precision,t.precision),n=Lr(i,t),s=dr(...n).filter(r=>r>=-t.precision&&r<=1+t.precision);return si(s,e).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}function ut(i,{firstPoint:t,lastPoint:e},n=1e-9){const s=q(e,t);return Math.abs(s[0])<n?s[1]>0?t[0]-i[0]:i[0]-t[0]:Math.abs(s[1])<n?s[0]>0?i[1]-t[1]:t[1]-i[1]:xt(s,q(i,t))/os(s)}let Ke=class{constructor(t,e,n,s){this.firstPoint=t,this.lastPoint=e,this.negativeThickness=n,this.positiveThickness=s}get width(){return this.positiveThickness-this.negativeThickness}};const Br=3/4,Vr=4/9;function jr(i){const t=ut(i.firstControlPoint,i),e=ut(i.lastControlPoint,i),n=t*e>0?Br:Vr;return new Ke(i.firstPoint,i.lastPoint,n*Math.min(0,t,e),n*Math.max(0,t,e))}function Or(i){const t=ut(i.controlPoint,i);return new Ke(i.firstPoint,i.lastPoint,Math.min(0,t/2),Math.max(0,t/2))}function qr(i){if(ot.isInstance(i))return jr(i);if(et.isInstance(i))return Or(i);throw new Error("Not implemented")}function zr(i){const t=i.paramPoint(.5),e=tt(q(t,i.firstPoint)),n=W(t,e),s={firstPoint:t,lastPoint:n},r=[ut(i.firstPoint,s),ut(i.lastPoint,s)];return ot.isInstance(i)?r.push(ut(i.firstControlPoint,s),ut(i.lastControlPoint,s)):et.isInstance(i)&&r.push(ut(i.controlPoint,s)),new Ke(t,n,Math.min(...r),Math.max(...r))}function ai(i,t){const e=[];for(let n=1;n<i.length;n++){const s=i[n];if(s[1]===t){e.push(s[0]);continue}const r=i[n-1],o=t-r[1],a=t-s[1];if(o*a<0){e.push(r[0]+(t-r[1])*(s[0]-r[0])/(s[1]-r[1]));continue}}return e}class ie{constructor(t,e){this.from=t,this.to=e}get size(){return this.from==="start"?this.to==="end"?1:this.to:this.to==="end"?1-this.from:Math.abs(this.from-this.to)}clipCurve(t){return this.from==="start"?this.to==="end"?t:t.splitAtParameters([this.to])[0]:this.to==="end"?t.splitAtParameters([this.from])[1]:t.splitAtParameters([this.from,this.to])[1]}}function Nr(i,t){if(ot.isInstance(i))return new Ur([ut(i.firstPoint,t),ut(i.firstControlPoint,t),ut(i.lastControlPoint,t),ut(i.lastPoint,t)]);if(et.isInstance(i))return new Dr([ut(i.firstPoint,t),ut(i.controlPoint,t),ut(i.lastPoint,t)]);throw new Error("Not implemented")}class Dr{constructor(t){lt(this,"topHull",[]),lt(this,"bottomHull",[]),this.distances=t;const[e,n,s]=t,r=[0,e],o=[1/2,n],a=[1,s],h=s-e,l=e;n-(h*(1/2)+l)>0?(this.topHull=[r,o,a],this.bottomHull=[r,a]):(this.topHull=[r,a],this.bottomHull=[r,o,a])}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[2]}}class Ur{constructor(t){lt(this,"topHull",[]),lt(this,"bottomHull",[]),this.distances=t;const[e,n,s,r]=t,o=[0,e],a=[1/3,n],h=[2/3,s],l=[1,r],c=r-e,u=e,m=n-(c*(1/3)+u),w=s-(c*(2/3)+u);let g=null,p=null;if(m*w<0)g=[o,a,l],p=[o,h,l];else{const M=m/w;M>=2?(g=[o,a,l],p=[o,l]):M<=.5?(g=[o,h,l],p=[o,l]):(g=[o,a,h,l],p=[o,l])}m<0&&([g,p]=[p,g]),this.topHull=g,this.bottomHull=p}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[3]}}function hi(i,t){const e=Nr(t,i),n=ai(e.topHull,i.negativeThickness),s=ai(e.bottomHull,i.positiveThickness),r=e.endDistance>=i.negativeThickness&&e.endDistance<=i.positiveThickness;if(!n.length&&!s.length)return r?new ie("start","end"):null;if(n.length===1&&s.length===1)return new ie(n[0],s[0]);if(n.length===2&&s.length===2)throw new Error("Bug in the clipping algorithm, unexpected number of crossing points");const o=n.length?n:s;return o.length===2?new ie(o[0],o[1]):r?new ie(o[0],"end"):new ie("start",o[0])}function li(i,t){const e=qr(i),n=hi(e,t);if(!n)return null;const s=zr(i),r=hi(s,t);return r?n.size>r.size?r.clipCurve(t):n.clipCurve(t):null}const Ie=i=>et.isInstance(i)?bt(q(i.controlPoint,i.firstPoint))+bt(q(i.controlPoint,i.lastPoint)):bt(q(i.firstControlPoint,i.firstPoint))+bt(q(i.lastControlPoint,i.firstControlPoint))+bt(q(i.lastControlPoint,i.lastPoint));function Bt(i,t,e=1e-9,{maxIterations:n=100}={}){const s=Math.max(e*e,Number.EPSILON*10);let r=i,o=t,a=Ie(r),h=Ie(o);for(let l=0;l<n;l++){const c=a>s?li(o,r):r;if(!c)return[];const u=Ie(c),m=h>s?li(c,o):o;if(!m)return[];const w=Ie(m);if(u<=s&&w<=s)return[c.boundingBox.intersection(m.boundingBox).center];if(S(c.firstPoint,c.lastPoint)&&m.isOnSegment(c.firstPoint))return[c.firstPoint];if(S(m.firstPoint,m.lastPoint)&&c.isOnSegment(m.firstPoint))return[m.firstPoint];if(u>.8*a&&w>.8*h)if(u/a>w/h){const[g,p]=c.splitAtParameters([.5]);return vt([...Bt(g,m,e,{maxIterations:n-l}),...Bt(p,m,e,{maxIterations:n-l})],e)}else{const[g,p]=m.splitAtParameters([.5]);return vt([...Bt(c,g,e,{maxIterations:n-l}),...Bt(c,p,e,{maxIterations:n-l})],e)}r=c,o=m,a=u,h=w}throw new Error("Bézier clip: Maximum number of iterations reached")}function Yr(i,t){const e=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([n,s])=>{s.isOnSegment(n)&&e.push(n)}),e.length<2)return null;if(e.length===2)return[i.splitAt(e)[1]];if(e.length===3)return S(e[0],i.firstPoint)&&S(e[1],i.lastPoint)?[i]:[t];if(e.length===4)return[i]}function Xr(i,t,e=!1){const n=Math.max(i.precision,t.precision);if(e){const s=Yr(i,t);if(s)return s}return Bt(i,t,n)}function Wr(i,t){const e=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([n,s])=>{s.isOnSegment(n)&&e.push(n)}),e.length<2)return null;if(e.length===2)return[i.splitAt(e)[1]];if(e.length===3)return S(e[0],i.firstPoint)&&S(e[1],i.lastPoint)?[i]:[t];if(e.length===4)return[i]}function Hr(i,t,e=!1){const n=Math.max(i.precision,t.precision);if(e){const s=Wr(i,t);if(s)return s}return Bt(i,t,n)}function Qr(i,t,e){if(U.isInstance(i)&&U.isInstance(t)){const n=Cn(i,t,!1,e);return n===null?[]:[n]}if(U.isInstance(i)&&D.isInstance(t))return Ee(i,t,e);if(D.isInstance(i)&&U.isInstance(t))return Ee(t,i,e);if(D.isInstance(i)&&D.isInstance(t))return ti(i,t,!1,e);throw new Error("Not implemented")}function se(i,t,e){if(U.isInstance(i)&&U.isInstance(t)){const n=Cn(i,t,!0,e);return n===null?{intersections:[],overlaps:[],count:0}:U.isInstance(n)?{intersections:[],overlaps:[n],count:1}:{intersections:[n],overlaps:[],count:1}}if(!i.boundingBox.overlaps(t.boundingBox))return{intersections:[],overlaps:[],count:0};if(U.isInstance(i)&&D.isInstance(t)){const n=Ee(i,t,e);return{intersections:n,overlaps:[],count:n.length}}if(D.isInstance(i)&&U.isInstance(t)){const n=Ee(t,i,e);return{intersections:n,overlaps:[],count:n.length}}if(D.isInstance(i)&&D.isInstance(t)){const n=ti(i,t,!0,e);return n.length?D.isInstance(n[0])?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(U.isInstance(i)&&at.isInstance(t)){const n=Ze(i,t,e);return{intersections:n,overlaps:[],count:n.length}}if(U.isInstance(t)&&at.isInstance(i)){const n=Ze(t,i,e);return{intersections:n,overlaps:[],count:n.length}}if(D.isInstance(i)&&at.isInstance(t)){const n=ni(i,t);return{intersections:n,overlaps:[],count:n.length}}if(D.isInstance(t)&&at.isInstance(i)){const n=ni(t,i);return{intersections:n,overlaps:[],count:n.length}}if(at.isInstance(i)&&at.isInstance(t)){const n=$r(i,t,!0);return n.length?at.isInstance(n[0])?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(U.isInstance(i)&&(ot.isInstance(t)||et.isInstance(t))){const n=ii(i,t);return{intersections:n,overlaps:[],count:n.length}}if(U.isInstance(t)&&(ot.isInstance(i)||et.isInstance(i))){const n=ii(t,i);return{intersections:n,overlaps:[],count:n.length}}if((D.isInstance(i)||at.isInstance(i))&&et.isInstance(t)){const n=oi(i,t);return{intersections:n,overlaps:[],count:n.length}}if((D.isInstance(t)||at.isInstance(t))&&et.isInstance(i)){const n=oi(t,i);return{intersections:n,overlaps:[],count:n.length}}if((D.isInstance(i)||at.isInstance(i))&&ot.isInstance(t)){const n=ri(i,t);return{intersections:n,overlaps:[],count:n.length}}if((D.isInstance(t)||at.isInstance(t))&&ot.isInstance(i)){const n=ri(t,i);return{intersections:n,overlaps:[],count:n.length}}if(et.isInstance(i)&&et.isInstance(t)){const n=Hr(i,t);return n.length?et.isInstance(n[0])?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(et.isInstance(i)&&ot.isInstance(t)||et.isInstance(t)&&ot.isInstance(i)){const n=Bt(i,t);return{intersections:n,overlaps:[],count:n.length}}if(ot.isInstance(i)&&ot.isInstance(t)){const n=Xr(i,t);return n.length?ot.isInstance(n[0])?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}throw new Error("Not implemented")}function ui(i){const t=[];for(let e=0;e<i;e++)for(let n=0;n<=e;n++)t.push([e,n]);return t}function*tn(i){for(const[t,e]of ui(i.length))t!==e&&(yield[i[t],i[e]])}class ci extends Ft{constructor(t,{ignoreChecks:e=!1}={}){super(),lt(this,"segments"),lt(this,"_boundingBox",null),e||fi(t),this.segments=t}get repr(){return this.segments.map(t=>t.repr).join(`
|
|
9
9
|
`)+`
|
|
10
|
-
`}get info(){return this.repr}get firstPoint(){return this.segments[0].firstPoint}get lastPoint(){return this.segments[this.segments.length-1].lastPoint}get segmentsCount(){return this.segments.length}onStroke(t){return this.segments.some(e=>e.isOnSegment(t))}intersects(t){return this.boundingBox.overlaps(t.boundingBox)?this.segments.some(e=>t.segments.some(n=>re(e,n).count>0)):!1}overlappingSegments(t){return this.segments.flatMap(e=>t.segments.flatMap(n=>e.boundingBox.overlaps(n.boundingBox)?re(e,n).overlaps:[]))}get boundingBox(){if(this._boundingBox===null){let t=this.segments[0].boundingBox;this.segments.slice(1).forEach(e=>{t=t.merge(e.boundingBox)}),this._boundingBox=t}return this._boundingBox}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}function qr(i,t="Stroke"){si(i.length).forEach(([e,n])=>{if(e===n)return;const s=i[e],r=i[n],o=re(s,r),a=Math.max(s.precision,r.precision);if(o.count!==0){if(o.count===1&&!o.overlaps.length){const h=e-n,l=o.intersections[0];if(h===1&&k(s.firstPoint,l,a)||h===-1&&k(s.lastPoint,l,a)||h===i.length-1&&k(s.lastPoint,l,a)&&k(r.firstPoint,l,a)||-h===i.length-1&&k(s.firstPoint,l,a)&&k(r.lastPoint,l,a))return}if(!(o.count===2&&i.length===2&&(k(s.firstPoint,o.intersections[0],a)&&k(s.lastPoint,o.intersections[1],a)||k(s.firstPoint,o.intersections[1],a)&&k(s.lastPoint,o.intersections[0],a))))throw new Error(`${t} segments must not intersect, but segments ${s.info} and ${r.info} do at ${JSON.stringify(o.intersections)}`)}})}function oi(i,t="Stroke"){if(i.length===0)throw new Error(`${t} must have at least one segment`);yt([i.slice(0,-1),i.slice(1)]).forEach(([e,n])=>{if(!k(e.lastPoint,n.firstPoint))throw new Error(`${t} segments must be connected, but ${e.info} and ${n.info} are not`)}),qr(i,t)}function ai(i,t){return!!(i instanceof D&&t instanceof D&&Ye(i.V,t.V)||i instanceof z&&t instanceof z&&k(i.center,t.center)&&i.radius-t.radius<i.precision)}function hi(i,t){if(i instanceof D&&t instanceof D)return new D(i.firstPoint,t.lastPoint);if(i instanceof z&&t instanceof z)return new z(i.firstPoint,t.lastPoint,i.center,i.clockwise);throw new Error("Not implemented")}function li(i){let t=!1;const e=[];for(const n of i.segments){if(e.length===0){e.push(n);continue}const s=e[e.length-1];ai(s,n)?(t=!0,e.pop(),e.push(hi(s,n))):e.push(n)}if(k(i.firstPoint,i.lastPoint)&&ai(e[0],e[e.length-1])){t=!0;const n=e.pop();e[0]=hi(n,e[0])}return t?e:null}let Rt=class Jt extends ri{constructor(){super(...arguments),ut(this,"strokeType","STRAND")}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new Jt(t,{ignoreChecks:!0})}clone(){return new Jt(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}extend(t){if(!k(this.lastPoint,t.firstPoint))throw console.error(this.repr,t.repr),new Error("Cannot extend strand: connection point is not the same");return new Jt([...this.segments,...t.segments])}simplify(){const t=li(this);return t?new Jt(t,{ignoreChecks:!0}):this}transform(t){return new Jt(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}};const Nr=(i,t)=>{const e=ge(t,{V:[1,0],firstPoint:i,precision:t.precision});if(e==="parallel")return 0;const{intersectionParam1:n,intersectionParam2:s}=e;if(!t.isValidParameter(n)||s<=-t.precision)return 0;if(Math.abs(n)<t.precision||Math.abs(n-1)<t.precision){const[,r]=t.midPoint;return i[1]-r<0?1:0}return 1};let nn=class{constructor(t){ut(this,"_count",0),ut(this,"segment"),this.segment=t}update(t,e=!1){!e&&!this.segment.isOnSegment(t)||(k(t,this.segment.firstPoint)?this._count+=this.segment.tangentAtFirstPoint[1]>0?1:0:k(t,this.segment.lastPoint)?this._count+=this.segment.tangentAtLastPoint[1]>0?0:1:this._count+=1)}get count(){return this._count}};const Or=(i,t)=>{const e=t.precision,n=Math.abs(i[1]-t.center[1]);if(n>t.radius+e)return 0;const s=Ft(i,t.center),r=t.radius*t.radius,o=e*e;if(Math.abs(s-r)<o&&t.isOnSegment(i))return 0;const a=s-r>o;if(a&&t.center[0]<i[0])return 0;const h=Math.sqrt(t.radius*t.radius-n*n),l=new nn(t);return l.update([t.center[0]+h,i[1]]),a&&l.update([t.center[0]-h,i[1]]),l.count},zr=(i,t)=>{const e=t.boundingBox.xMax+t.boundingBox.width/2,n=new D(i,[e,i[1]]),s=new nn(t);return Ke(n,t).forEach(r=>{s.update(r,!0)}),s.count},Dr=(i,t)=>{const e=new nn(t);return t.paramsAtY(i[1]).map(n=>{try{return t.paramPoint(n)}catch{return null}}).filter(n=>n!==null).filter(n=>{const[s]=n;return s>=i[0]}).forEach(n=>{e.update(n,!0)}),e.count};function Ur(i,t){if(t instanceof D)return Nr(i,t);if(t instanceof z)return Or(i,t);if(t instanceof ht)return zr(i,t);if(t instanceof ot||t instanceof et)return Dr(i,t);throw new Error("Not implemented")}let jt=class ue extends ri{constructor(t,{ignoreChecks:e=!1}={}){super(t,{ignoreChecks:!0}),ut(this,"strokeType","LOOP"),ut(this,"_clockwise",null),e||Yr(t)}get clockwise(){if(this._clockwise===null){const t=this.segments.flatMap(n=>n instanceof D?[n.firstPoint]:[n.firstPoint,n.paramPoint(.5)]),e=t.map((n,s)=>{const r=t[(s+1)%t.length];return(r[0]-n[0])*(r[1]+n[1])}).reduce((n,s)=>n+s,0);this._clockwise=e>0}return this._clockwise}clone(){return new ue(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new ue(t,{ignoreChecks:!0})}transform(t){return new ue(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}contains(t,{strokeIsInside:e=!1}={}){return this.onStroke(t)?e:this.boundingBox.contains(t)?this.segments.reduce((n,s)=>n+Ur(t,s),0)%2===1:!1}simplify(){const t=li(this);return t?new ue(t,{ignoreChecks:!0}):this}};function Yr(i){if(oi(i,"Loop"),!k(i[0].firstPoint,i[i.length-1].lastPoint))throw new Error("Loop segment must be closed")}const Xr=[D,z,ht,et,ot];function ui(i){return Xr.some(t=>i instanceof t)}function ci(i){if(i instanceof D)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint};if(i instanceof z)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise};if(i instanceof ht)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise,majorRadius:i.majorRadius,minorRadius:i.minorRadius,tiltAngle:i.tiltAngle};if(i instanceof et)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,controlPoint:i.controlPoint};if(i instanceof ot)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,firstControlPoint:i.firstControlPoint,lastControlPoint:i.lastControlPoint};throw new Error("Unknown segment type")}function sn(i){return{type:"LOOP",segments:i.segments.map(ci)}}function fi(i){return{type:"FIGURE",contour:sn(i.contour),holes:i.holes.map(sn)}}function Wr(i){return{type:"DIAGRAM",figures:i.figures.map(fi)}}function mi(i){if(i instanceof Mt)return Wr(i);if(i instanceof wt)return fi(i);if(i instanceof jt)return sn(i);if(ui(i))return ci(i);throw new Error("Unknown shape type")}class Hr{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,e){let n=this.length++;for(;n>0;){const s=n-1>>1,r=this.values[s];if(e>=r)break;this.ids[n]=this.ids[s],this.values[n]=r,n=s}this.ids[n]=t,this.values[n]=e}pop(){if(this.length===0)return;const t=this.ids[0];if(this.length--,this.length>0){const e=this.ids[0]=this.ids[this.length],n=this.values[0]=this.values[this.length],s=this.length>>1;let r=0;for(;r<s;){let o=(r<<1)+1;const a=o+1;let h=this.ids[o],l=this.values[o];const c=this.values[a];if(a<this.length&&c<l&&(o=a,h=this.ids[a],l=c),l>=n)break;this.ids[r]=h,this.values[r]=l,r=o}this.ids[r]=e,this.values[r]=n}return t}peek(){if(this.length!==0)return this.ids[0]}peekValue(){if(this.length!==0)return this.values[0]}shrink(){this.ids.length=this.values.length=this.length}}const gi=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],rn=3;class on{static from(t,e=0){if(e%8!==0)throw new Error("byteOffset must be 8-byte aligned.");if(!t||t.byteLength===void 0||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[n,s]=new Uint8Array(t,e+0,2);if(n!==251)throw new Error("Data does not appear to be in a Flatbush format.");const r=s>>4;if(r!==rn)throw new Error(`Got v${r} data when expected v${rn}.`);const o=gi[s&15];if(!o)throw new Error("Unrecognized array type.");const[a]=new Uint16Array(t,e+2,1),[h]=new Uint32Array(t,e+4,1);return new on(h,a,o,void 0,t,e)}constructor(t,e=16,n=Float64Array,s=ArrayBuffer,r,o=0){if(t===void 0)throw new Error("Missing required argument: numItems.");if(isNaN(t)||t<=0)throw new Error(`Unexpected numItems value: ${t}.`);this.numItems=+t,this.nodeSize=Math.min(Math.max(+e,2),65535),this.byteOffset=o;let a=t,h=a;this._levelBounds=[a*4];do a=Math.ceil(a/this.nodeSize),h+=a,this._levelBounds.push(h*4);while(a!==1);this.ArrayType=n,this.IndexArrayType=h<16384?Uint16Array:Uint32Array;const l=gi.indexOf(this.ArrayType),c=h*4*this.ArrayType.BYTES_PER_ELEMENT;if(l<0)throw new Error(`Unexpected typed array class: ${n}.`);r&&r.byteLength!==void 0&&!r.buffer?(this.data=r,this._boxes=new this.ArrayType(this.data,o+8,h*4),this._indices=new this.IndexArrayType(this.data,o+8+c,h),this._pos=h*4,this.minX=this._boxes[this._pos-4],this.minY=this._boxes[this._pos-3],this.maxX=this._boxes[this._pos-2],this.maxY=this._boxes[this._pos-1]):(this.data=new s(8+c+h*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+c,h),this._pos=0,this.minX=1/0,this.minY=1/0,this.maxX=-1/0,this.maxY=-1/0,new Uint8Array(this.data,0,2).set([251,(rn<<4)+l]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=t),this._queue=new Hr}add(t,e,n=t,s=e){const r=this._pos>>2,o=this._boxes;return this._indices[r]=r,o[this._pos++]=t,o[this._pos++]=e,o[this._pos++]=n,o[this._pos++]=s,t<this.minX&&(this.minX=t),e<this.minY&&(this.minY=e),n>this.maxX&&(this.maxX=n),s>this.maxY&&(this.maxY=s),r}finish(){if(this._pos>>2!==this.numItems)throw new Error(`Added ${this._pos>>2} items when expected ${this.numItems}.`);const t=this._boxes;if(this.numItems<=this.nodeSize){t[this._pos++]=this.minX,t[this._pos++]=this.minY,t[this._pos++]=this.maxX,t[this._pos++]=this.maxY;return}const e=this.maxX-this.minX||1,n=this.maxY-this.minY||1,s=new Uint32Array(this.numItems),r=65535;for(let o=0,a=0;o<this.numItems;o++){const h=t[a++],l=t[a++],c=t[a++],u=t[a++],m=Math.floor(r*((h+c)/2-this.minX)/e),w=Math.floor(r*((l+u)/2-this.minY)/n);s[o]=Gr(m,w)}an(s,t,this._indices,0,this.numItems-1,this.nodeSize);for(let o=0,a=0;o<this._levelBounds.length-1;o++){const h=this._levelBounds[o];for(;a<h;){const l=a;let c=t[a++],u=t[a++],m=t[a++],w=t[a++];for(let g=1;g<this.nodeSize&&a<h;g++)c=Math.min(c,t[a++]),u=Math.min(u,t[a++]),m=Math.max(m,t[a++]),w=Math.max(w,t[a++]);this._indices[this._pos>>2]=l,t[this._pos++]=c,t[this._pos++]=u,t[this._pos++]=m,t[this._pos++]=w}}}search(t,e,n,s,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const a=[],h=[];for(;o!==void 0;){const l=Math.min(o+this.nodeSize*4,wi(o,this._levelBounds));for(let c=o;c<l;c+=4){if(n<this._boxes[c]||s<this._boxes[c+1]||t>this._boxes[c+2]||e>this._boxes[c+3])continue;const u=this._indices[c>>2]|0;o>=this.numItems*4?a.push(u):(r===void 0||r(u))&&h.push(u)}o=a.pop()}return h}neighbors(t,e,n=1/0,s=1/0,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const a=this._queue,h=[],l=s*s;t:for(;o!==void 0;){const c=Math.min(o+this.nodeSize*4,wi(o,this._levelBounds));for(let u=o;u<c;u+=4){const m=this._indices[u>>2]|0,w=pi(t,this._boxes[u],this._boxes[u+2]),g=pi(e,this._boxes[u+1],this._boxes[u+3]),p=w*w+g*g;p>l||(o>=this.numItems*4?a.push(m<<1,p):(r===void 0||r(m))&&a.push((m<<1)+1,p))}for(;a.length&&a.peek()&1;)if(a.peekValue()>l||(h.push(a.pop()>>1),h.length===n))break t;o=a.length?a.pop()>>1:void 0}return a.clear(),h}}function pi(i,t,e){return i<t?t-i:i<=e?0:i-e}function wi(i,t){let e=0,n=t.length-1;for(;e<n;){const s=e+n>>1;t[s]>i?n=s:e=s+1}return t[e]}function an(i,t,e,n,s,r){if(Math.floor(n/r)>=Math.floor(s/r))return;const o=i[n+s>>1];let a=n-1,h=s+1;for(;;){do a++;while(i[a]<o);do h--;while(i[h]>o);if(a>=h)break;Qr(i,t,e,a,h)}an(i,t,e,n,h,r),an(i,t,e,h+1,s,r)}function Qr(i,t,e,n,s){const r=i[n];i[n]=i[s],i[s]=r;const o=4*n,a=4*s,h=t[o],l=t[o+1],c=t[o+2],u=t[o+3];t[o]=t[a],t[o+1]=t[a+1],t[o+2]=t[a+2],t[o+3]=t[a+3],t[a]=h,t[a+1]=l,t[a+2]=c,t[a+3]=u;const m=e[n];e[n]=e[s],e[s]=m}function Gr(i,t){let e=i^t,n=65535^e,s=65535^(i|t),r=i&(t^65535),o=e|n>>1,a=e>>1^e,h=s>>1^n&r>>1^s,l=e&s>>1^r>>1^r;e=o,n=a,s=h,r=l,o=e&e>>2^n&n>>2,a=e&n>>2^n&(e^n)>>2,h^=e&s>>2^n&r>>2,l^=n&s>>2^(e^n)&r>>2,e=o,n=a,s=h,r=l,o=e&e>>4^n&n>>4,a=e&n>>4^n&(e^n)>>4,h^=e&s>>4^n&r>>4,l^=n&s>>4^(e^n)&r>>4,e=o,n=a,s=h,r=l,h^=e&s>>8^n&r>>8,l^=n&s>>8^(e^n)&r>>8,e=h^h>>1,n=l^l>>1;let c=i^t,u=n|65535^(c|e);return c=(c|c<<8)&16711935,c=(c|c<<4)&252645135,c=(c|c<<2)&858993459,c=(c|c<<1)&1431655765,u=(u|u<<8)&16711935,u=(u|u<<4)&252645135,u=(u|u<<2)&858993459,u=(u|u<<1)&1431655765,(u<<1|c)>>>0}function di(i,t=1e-7){if(i.length===0)return[];if(i.length===1)return[i];const e=new on(i.length);i.forEach(r=>{const[o,a]=r.firstPoint;e.add(o-t,a-t,o+t,a+t)}),e.finish();const n=[],s=new Set;return i.forEach((r,o)=>{if(s.has(o))return;const a=[r];let h=o;s.add(o);let l=i.length;for(;;){if(l--<0)throw new Error("Infinite loop detected");const c=a[a.length-1].lastPoint,[u,m]=c,w=e.search(u-t,m-t,u+t,m+t),g=P=>Math.abs((h-P)%i.length),p=w.filter(P=>!s.has(P)).map(P=>[i[P],P,g(P)]).sort(([,,P],[,,b])=>g(P)-g(b));if(p.length===0){n.push(a);break}const[M,f]=p[0];a.push(M),s.add(f),h=f}}),n}let wt=class Ne extends _t{constructor(t,e=[],{ignoreChecks:n=!1}={}){super(),ut(this,"contour"),ut(this,"holes"),n||Jr(t,e),this.contour=t,this.holes=e}get boundingBox(){return this.contour.boundingBox}get isFull(){return this.holes.length===0}get allLoops(){return[this.contour,...this.holes]}clone(){return new Ne(this.contour.clone(),this.holes.map(t=>t.clone()))}transform(t){return new Ne(this.contour.transform(t),this.holes.map(e=>e.transform(t)))}contains(t,{strokeIsInside:e=!1}={}){return this.contour.contains(t,{strokeIsInside:e})&&!this.holes.some(n=>n.contains(t,{strokeIsInside:e}))}intersects(t){return this.allLoops.some(e=>t.allLoops.some(n=>e.intersects(n)))}overlappingStrands(t){const e=t instanceof Ne?t.allLoops:[t],n=this.allLoops.flatMap(s=>e.flatMap(r=>s.overlappingSegments(r)));return di(n).map(s=>new Rt(s))}};function Jr(i,t=[]){if(!i)throw new Error("Figure must have a contour");for(const[e,n]of en([i,...t]))if(e.intersects(n))throw new Error("Loops in a figure must not intersect");if(t.some(e=>!i.contains(e.firstPoint)&&!i.onStroke(e.firstPoint)))throw new Error("Holes must be inside the contour");for(const[e,n]of en(t))if(e.contains(n.firstPoint))throw console.error(mi(e),mi(n)),new Error("Holes must not be inside other holes")}const Re=(i,t,e=1e-7)=>Math.abs(i-t)<=e,Zr=(i,t)=>{const e=i.boundingBox,n=t.boundingBox;return Re(e.xMin,n.xMin)&&Re(e.yMin,n.yMin)&&Re(e.xMax,n.xMax)&&Re(e.yMax,n.yMax)},Kr=(i,t)=>{if(i.segmentsCount!==t.segmentsCount||!Zr(i,t))return!1;const e=i.segments,n=t.segments,s=e.length,r=(o,a)=>{for(let h=0;h<s;h+=1){const l=(o+a*h+s)%s;if(!e[h].isSame(n[l]))return!1}return!0};for(let o=0;o<s;o+=1)if(e[0].isSame(n[o])&&(r(o,1)||r(o,-1)))return!0;return!1},to=i=>{const t=[];return i.forEach(e=>{t.some(n=>Kr(e,n))||t.push(e)}),t},eo=i=>{const t=i.map((s,r)=>i.slice(r+1).map((o,a)=>[a+r+1,o]).filter(([,o])=>s.boundingBox.overlaps(o.boundingBox)).map(([o])=>o)),e=[],n=Array(t.length);return t.forEach((s,r)=>{let o=n[r];o||(o=[],e.push(o)),o.push(i[r]),s.length&&s.forEach(a=>{n[a]=o})}),e},Pi=i=>i.map((t,e)=>{const n=t.segments[0].midPoint,s=i.filter((r,o)=>e===o?!1:r.contains(n));return{loop:t,isIn:s}}),no=(i,t)=>i.flatMap(({loop:e})=>hn(t.filter(({loop:n,isIn:s})=>n===e||s.indexOf(e)!==-1))),io=(i,t)=>{const e=t.filter(({isIn:s})=>s.length<=1),n=hn(Pi(i.map(({loop:s})=>s)));return[e,...n]},hn=i=>{if(!i.length)return[];const t=i.filter(({isIn:n})=>!n.length),e=i.filter(({isIn:n})=>n.length>1);return t.length===1&&e.length===0?[i]:t.length>1?no(t,i):io(e,i)};function oe(i){const t=to(i);return eo(t).map(Pi).flatMap(hn).map(e=>{if(e.length===1)return new wt(e[0].loop);e.sort((r,o)=>r.isIn.length-o.isIn.length);const[n,...s]=e.map(({loop:r})=>r);return new wt(n,s)})}function so(i,t){const e=[];for(const n of i)for(const s of t)e.push([n,s]);return e}function*ln(i,t,e){const n=a=>t.some(h=>k(h,a.lastPoint)),s=(a,h)=>a.segmentType!==h.segmentType||!h.isOnSegment(a.firstPoint)||!h.isOnSegment(a.lastPoint)?!1:a.segmentType!=="LINE"?h.isOnSegment(a.midPoint):!0,r=a=>e.some(h=>a.isSame(h)||s(a,h));let o=[];for(const a of i)n(a)?(o.push(a),yield new Rt(o,{ignoreChecks:!0}),o=[]):r(a)?(o.length&&(yield new Rt(o,{ignoreChecks:!0}),o=[]),yield new Rt([a],{ignoreChecks:!0})):o.push(a);o.length&&(yield new Rt(o,{ignoreChecks:!0}))}const Mi=(i,t)=>{const e=i.findIndex(s=>k(t,s.firstPoint)),n=i.slice(0,e);return i.slice(e).concat(n)},yi=(i,t)=>{let e=i;const n=o=>k(o.firstPoint,t.firstPoint)&&k(o.lastPoint,t.lastPoint);let s=i.findIndex(n);if(s===-1){const o=i.map(a=>a.reverse());if(o.reverse(),s=o.findIndex(n),s===-1)throw console.error(o.map(a=>a.repr),t.repr),new Error("Failed to rotate to segment start");e=o}const r=e.slice(0,s);return e.slice(s).concat(r)};function ro(i,t,e){return i.filter(n=>{const s=t.filter(o=>k(o.firstPoint,n)||k(o.lastPoint,n));if(s.length%2)throw new Error("Bug in the intersection algo on non crossing point");const r=s.map(o=>e.contains(o.midPoint));return!(r.every(o=>o)||!r.some(o=>o))})}function oo(i,t,e,n=!1){let s=[];const r=[],o=new Array(i.segments.length).fill(0).map(()=>[]),a=new Array(t.segments.length).fill(0).map(()=>[]);if(i.segments.forEach((w,g)=>{t.segments.forEach((p,M)=>{const{intersections:f,overlaps:P}=re(w,p,e);s.push(...f),o[g].push(...f),a[M].push(...f),r.push(...P);const b=P.flatMap(S=>[S.firstPoint,S.lastPoint]);s.push(...b),o[g].push(...b),a[M].push(...b)})}),s=At(s,e),!s.length||s.length===1)return null;const h=([w,g])=>g.length?w.splitAt(g):[w];let l=yt([i.segments,o]).flatMap(h),c=yt([t.segments,a]).flatMap(h);if(s=ro(s,l,t),!s.length&&!r.length)return null;if(r.length){const w=r[0];l=yi(l,w),c=yi(c,w)}else{const w=s[0];l=Mi(l,w),c=Mi(c,w)}let u=Array.from(ln(l,s,r)),m=Array.from(ln(c,s,r));return(!k(m[0].lastPoint,u[0].lastPoint)||r.length>0&&m[0].segmentsCount!==1)&&(m=m.map(w=>w.reverse()).reverse(),k(m[0].lastPoint,u[0].lastPoint)||(u=u.map(w=>w.reverse()).reverse())),yt([u,m]).map(([w,g])=>{if(n){if((p=>p.segments.every(M=>r.some(f=>M.isSame(f)||M.segmentType===f.segmentType&&f.isOnSegment(M.firstPoint)&&f.isOnSegment(M.lastPoint)&&(M.segmentType==="LINE"||f.isOnSegment(M.midPoint)))))(w))return[w,"same"]}else if(w.segmentsCount===1&&r.some(p=>w.segments[0].isSame(p)))return[w,"same"];return[w,g]})}function bi(i){let t=i[0];for(const e of i.slice(1))t=t.extend(e);if(!k(t.firstPoint,t.lastPoint))throw console.error(st(t.firstPoint),st(t.lastPoint)),new Error("Bug in the intersection algo on non closing strand");return new jt(t.segments)}function ao(i,t){const e=yt([t.slice(0,-1),t.slice(1)]).map(([s,r])=>bi(i.slice(s,r)));let n=i.slice(t[t.length-1]);return t[0]!==0&&(n=n.concat(i.slice(0,t[0]))),e.push(bi(n)),e}function ho(i){if(!i.length)return[];const t=i.map(s=>s.firstPoint);let e=i.map(s=>s.lastPoint);e=e.slice(-1).concat(e.slice(0,-1));const n=yt([t,e]).flatMap(([s,r],o)=>k(s,r)?[]:o);try{return ao(i,n)}catch{return di(i.flatMap(s=>s.segments)).filter(s=>s.length>1).filter(s=>k(s[0].firstPoint,s.at(-1).lastPoint)).map(s=>new jt(s))}}const xi=(i,t)=>{if(i.length===0)return[t];const e=i.at(-1);return k(e.lastPoint,t.firstPoint)?i.slice(0,-1).concat([e.extend(t)]):k(e.lastPoint,t.lastPoint)?i.slice(0,-1).concat([e.extend(t.reverse())]):i.concat([t])},lo=(i,t)=>i.length===0?[t]:k(i[0].firstPoint,t.lastPoint)?[t.extend(i[0])].concat(i.slice(1)):[t].concat(i);function un(i,t,{firstInside:e,secondInside:n,firstBoundaryInside:s=!1,secondBoundaryInside:r=!1}){const o=oo(i,t,void 0,s||r);if(!o){const c=i.segments[0].midPoint,u=t.contains(c,{strokeIsInside:r}),m=t.segments[0].midPoint,w=i.contains(m,{strokeIsInside:s});return{identical:!1,firstCurveInSecond:u,secondCurveInFirst:w}}if(o.every(([,c])=>c==="same"))return{identical:!0};let a=null,h=null;const l=o.flatMap(([c,u])=>{let m=[],w=0;if(u==="same")return h===1?(h=1,c):h===2||h===0?(h=null,[]):h===null?(a?a=a.extend(c):a=c,[]):(console.error("weird situation"),[]);const g=c.segments[0].midPoint,p=t.contains(g,{strokeIsInside:r});(e==="keep"&&p||e==="remove"&&!p)&&(w+=1,m=xi(m,c));const M=u.segments[0].midPoint,f=i.contains(M,{strokeIsInside:s});if(n==="keep"&&f||n==="remove"&&!f){const P=u;w+=1,w===2&&m.length?(m=xi(m,P),a=null):m=[P]}return h===null&&w===1&&a&&(m=lo(m,a)),w===1&&(h=w,a=null),m.length?m:(a=null,[])});return ho(l)}const uo=(i,t,e)=>{const n=un(i,t,{firstInside:"remove",secondInside:"remove",...e});return Array.isArray(n)?n:n.identical?[i]:n.firstCurveInSecond?[t]:n.secondCurveInFirst?[i]:[i,t]},Ce=(i,t,e)=>{const n=un(i,t,{firstInside:"remove",secondInside:"keep",...e});return Array.isArray(n)?n:n.identical?[]:n.firstCurveInSecond?[]:n.secondCurveInFirst?[i,t]:[i]},cn=(i,t,e)=>{const n=(e==null?void 0:e.firstBoundaryInside)??!1,s=(e==null?void 0:e.secondBoundaryInside)??!1,r=n||s,o=(h,l,c)=>h.segments.every(u=>l.contains(u.midPoint,{strokeIsInside:c}));if(r){if(o(i,t,s))return[i];if(o(t,i,n))return[t]}const a=un(i,t,{firstInside:"keep",secondInside:"keep",...e});return Array.isArray(a)?a:a.identical?[i]:a.firstCurveInSecond?[i]:a.secondCurveInFirst?[t]:[]};function co(i){const t=new Map,e=[];return i.forEach((n,s)=>{let r;t.has(s)?r=t.get(s):(r={current:[n],fusedWith:new Set([s])},e.push(r)),i.slice(s+1).forEach((o,a)=>{const h=r.current,l=s+a+1;if(r.fusedWith.has(l))return;let c=[o],u=!1;if(t.has(l)&&(c=t.get(l).current,u=!0),!h.some(w=>c.some(g=>w.intersects(g))))return;let m;h.length>1||c.length>1?m=ae(h,c):m=vi(h[0],c[0]),r.fusedWith.add(l),r.current=m,u||t.set(l,r)})}),e.flatMap(({current:n})=>n)}function vi(i,t){const e=uo(i.contour,t.contour),n=t.holes.flatMap(o=>Ce(o,i.contour)),s=i.holes.flatMap(o=>Ce(o,t.contour)),r=so(i.holes,t.holes).flatMap(([o,a])=>cn(o,a));return oe([...e,...n,...s,...r])}function Te(i,t){if(i.isFull&&t.isFull)return oe(Ce(i.contour,t.contour));if(i.isFull){const n=Ce(i.contour,t.contour),s=t.holes.flatMap(r=>cn(r,i.contour,{firstBoundaryInside:!0}));return oe([...n,...s])}else if(t.isFull&&!i.contour.intersects(t.contour))if(i.contour.contains(t.contour.firstPoint)){const n=ae(i.holes.map(s=>new wt(s)),[t]);return oe([i.contour,...n.flatMap(s=>s.allLoops)])}else return[i];let e=Te(new wt(i.contour),t);return i.holes.forEach(n=>{e=e.flatMap(s=>Te(s,new wt(n)))}),e}function fo(i,t){const e=cn(i.contour,t.contour);if(!e.length)return[];let n=oe(e);return n=Yt(n,i.holes.map(s=>new wt(s))),Yt(n,t.holes.map(s=>new wt(s)))}function ae(i,t){if(!i.length)return t;if(!t.length)return i;if(i.length===1&&t.length>1||t.length===1&&i.length>1)return co([...i,...t]);if(i.length>1&&t.length>1){let e=ae([i[0]],t);return i.slice(1).forEach(n=>{e=ae([n],e)}),e}return i.length===1&&t.length===1?vi(i[0],t[0]):[]}function Yt(i,t){if(!i.length)return[];if(!t.length)return i;if(i.length===1&&t.length===1)return Te(i[0],t[0]);if(i.length>1)return i.flatMap(n=>Yt([n],t));let e=Te(i[0],t[0]);return t.slice(1).forEach(n=>{e=Yt(e,[n])}),e}function fn(i,t){return!i.length||!t.length?[]:i.length===1&&t.length===1?fo(i[0],t[0]):i.length>1?i.flatMap(e=>fn([e],t)):t.flatMap(e=>fn(i,[e]))}class Mt extends _t{constructor(t=[],{ignoreChecks:e=!1}={}){super(),ut(this,"figures"),ut(this,"_boundingBox",null),e||mo(t),this.figures=t}get isEmpty(){return this.figures.length===0}get boundingBox(){if(this.isEmpty)return new Zt;if(this._boundingBox===null){let t=this.figures[0].boundingBox;for(const e of this.figures.slice(1))t=t.merge(e.boundingBox);this._boundingBox=t}return this._boundingBox}clone(){return new Mt(this.figures.map(t=>t.clone()))}transform(t){return new Mt(this.figures.map(e=>e.transform(t)))}contains(t,{strokeIsInside:e=!1}={}){return this.figures.some(n=>n.contains(t,{strokeIsInside:e}))}intersects(t){return this.figures.some(e=>t.figures.some(n=>e.intersects(n)))}overlappingStrands(t){return this.figures.flatMap(e=>t instanceof Mt?t.figures.flatMap(n=>e.overlappingStrands(n)):e.overlappingStrands(t))}fuse(t){return new Mt(ae(this.figures,t.figures))}cut(t){return new Mt(Yt(this.figures,t.figures))}intersect(t){return new Mt(fn(this.figures,t.figures))}}function mo(i){for(const[t,e]of en(i))if(t.intersects(e))throw new Error("Diagram figures must not intersect")}var go=Object.defineProperty,po=(i,t,e)=>t in i?go(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,Ie=(i,t,e)=>po(i,typeof t!="symbol"?t+"":t,e);const Ai=(i,t,e)=>{const n=vt(i.V,t.V),s=i.precision*t.precision;if(n*n<s)return"parallel";const r=q(t.firstPoint,i.firstPoint),o=vt(r,t.V)/n;return X(i.firstPoint,W(i.V,o))};class $e{constructor(t,e){this.firstPoint=t,this.lastPoint=e}}function Ei(i,t){if(i instanceof D)return wo(i,t);if(i instanceof z)return Po(i,t);if(i instanceof et||i instanceof ot)return Mo(i,t);throw new Error("Not implemented")}function wo(i,t){const{firstPoint:e,lastPoint:n}=i,s=i.normalVector;return new D(X(e,W(s,t)),X(n,W(s,t)))}function Po(i,t){const e=X(i.firstPoint,W(tt(i.tangentAtFirstPoint),t)),n=X(i.lastPoint,W(tt(i.tangentAtLastPoint),t)),s=t*(i.clockwise?1:-1);return i.radius+s<i.precision?new $e(e,n):new z(e,n,i.center,i.clockwise)}function mn(i,t,e,n,s){const r=Ai({V:e,firstPoint:n,precision:s},{V:q(t,i),firstPoint:i,precision:s});if(r==="parallel")throw new Error("Parallel lines not expected in safe bezier offset control point calculation");return r}function Mo(i,t){const{firstPoint:e,lastPoint:n,normalAtFirstPoint:s,normalAtLastPoint:r}=i,o=Ai({V:s,firstPoint:e,precision:i.precision},{V:r,firstPoint:n,precision:i.precision}),a=X(e,W(s,t)),h=X(n,W(r,t));if(o==="parallel")throw new Error("Parallel lines not expected in safe bezier offset");if(fe(q(o,e),s)*t>0&&Math.min(J(e,o),J(n,o))<t)return new $e(a,h);if(i instanceof et){const u=mn(o,i.controlPoint,i.tangentAtFirstPoint,a,i.precision);return new et(a,h,u)}const l=mn(o,i.firstControlPoint,i.tangentAtFirstPoint,a,i.precision),c=mn(o,i.lastControlPoint,i.tangentAtLastPoint,h,i.precision);return new ot(a,h,l,c)}function Si(i,t,e){const n=vt(i.tangentAtLastPoint,t.tangentAtFirstPoint);if(Math.abs(n)<1e-10)return null;const s=n>0?1:-1,r=Math.abs(e)*s,o=Ei(i,r),a=Ei(t,r);if(o instanceof $e||a instanceof $e)return null;let h;try{h=jr(o,a,1e-9).at(-1)}catch{return null}if(!h)return null;const l=h,c=(w,g)=>{const p=g.tangentAt(l),M=Kt(p),f=X(l,W(M,r));return w.splitAt(f)},[u]=c(i,o),[,m]=c(t,a);return{first:u,second:m,center:l}}function ki(i,t,e){const n=Si(i,t,e);if(!n)return console.warn("Cannot fillet between segments",i.repr,t.repr),[i,t];const{first:s,second:r}=n;return[s,Rn(s.lastPoint,r.firstPoint,s.tangentAtLastPoint),r]}function Ri(i,t,e){const n=Si(i,t,e);if(!n)return console.warn("Cannot chamfer between segments",i.repr,t.repr),[i,t];const{first:s,second:r}=n;return[s,new D(s.lastPoint,r.firstPoint),r]}const yo=i=>{let t;i?typeof i=="number"||Array.isArray(i)&&i.length===2?t={endTangent:i}:t={endTangent:0,...i}:t={endTangent:[1,0]};const{endTangent:e,startFactor:n=1,endFactor:s=1,startTangent:r}=t;let o;typeof e=="number"?o=Et(1,e*$t):o=e;let a;return typeof r=="number"?a=Et(1,r*$t):a=r,{endTangent:o,startFactor:n,endFactor:s,startTangent:a}};function Ci(i,{ignoreChecks:t=!1}={}){return new Mt([new wt(new jt([...i],{ignoreChecks:t}))])}let bo=class{constructor(t=[0,0]){Ie(this,"pointer"),Ie(this,"firstPoint"),Ie(this,"pendingSegments"),Ie(this,"_nextCorner"),this.pointer=t,this.firstPoint=t,this.pendingSegments=[],this._nextCorner=null}movePointerTo(t){if(this.pendingSegments.length)throw new Error("You can only move the pointer if there is no segment defined");return this.pointer=t,this.firstPoint=t,this}saveSegment(t){if(k(t.firstPoint,t.lastPoint))throw new Error(`Segment has no length, ${t.repr}`);if(!this._nextCorner)return this.pendingSegments.push(t),this;const e=this.pendingSegments.pop();if(!e)throw new Error("bug in the custom corner algorithm");const n=this._nextCorner.mode==="chamfer"?Ri:ki;return this.pendingSegments.push(...n(e,t,this._nextCorner.radius)),this._nextCorner=null,this}lineTo(t){const e=new D(this.pointer,t);return this.pointer=t,this.saveSegment(e)}line(t,e){return this.lineTo([this.pointer[0]+t,this.pointer[1]+e])}vLine(t){return this.line(0,t)}hLine(t){return this.line(t,0)}vLineTo(t){return this.lineTo([this.pointer[0],t])}hLineTo(t){return this.lineTo([t,this.pointer[1]])}polarLineTo([t,e]){const n=e*$t,s=Et(t,n);return this.lineTo(s)}polarLine(t,e){const n=e*$t,[s,r]=Et(t,n);return this.line(s,r)}tangentLine(t){const e=this.pendingSegments.at(-1);if(!e)throw new Error("You need a previous segment to sketch a tangent line");const[n,s]=e.tangentAtLastPoint;return this.line(n*t,s*t)}threePointsArcTo(t,e){return this.saveSegment(es(this.pointer,e,t)),this.pointer=t,this}threePointsArc(t,e,n,s){const[r,o]=this.pointer;return this.threePointsArcTo([r+t,o+e],[r+n,o+s])}sagittaArcTo(t,e){if(!e)return this.lineTo(t);const n=new D(this.pointer,t),s=tt(n.tangentAtFirstPoint),r=X(n.midPoint,W(s,e));return this.threePointsArcTo(t,r)}sagittaArc(t,e,n){return this.sagittaArcTo([t+this.pointer[0],e+this.pointer[1]],n)}vSagittaArc(t,e){return this.sagittaArc(0,t,e)}hSagittaArc(t,e){return this.sagittaArc(t,0,e)}bulgeArcTo(t,e){if(!e)return this.lineTo(t);const n=J(this.pointer,t)/2,s=-e*n;return this.sagittaArcTo(t,s)}bulgeArc(t,e,n){return this.bulgeArcTo([t+this.pointer[0],e+this.pointer[1]],n)}vBulgeArc(t,e){return this.bulgeArc(0,t,e)}hBulgeArc(t,e){return this.bulgeArc(t,0,e)}tangentArcTo(t,e){const n=this.pendingSegments.at(-1);if(!n)throw new Error("You need a previous curve to sketch a tangent arc");return this.saveSegment(Rn(this.pointer,t,e??n.tangentAtLastPoint)),this.pointer=t,this}tangentArc(t,e,n){const[s,r]=this.pointer;return this.tangentArcTo([t+s,e+r],n)}ellipseTo(t,e,n,s,r,o){return this.saveSegment(cr(this.pointer,t,e,n,s,r,o)),this.pointer=t,this}ellipse(t,e,n,s,r,o,a){return this.ellipseTo([t+this.pointer[0],e+this.pointer[1]],n,s,r,o,a)}halfEllipseTo(t,e){const[n,s]=Sn(q(t,this.pointer));return this.ellipseTo(t,n/2,Math.abs(e),s*Ue,!0,e>0)}halfEllipse(t,e,n){return this.halfEllipseTo([t+this.pointer[0],e+this.pointer[1]],n)}cubicBezierCurveTo(t,e,n){return this.saveSegment(new ot(this.pointer,t,e,n)),this.pointer=t,this}quadraticBezierCurveTo(t,e){return this.saveSegment(new et(this.pointer,t,e)),this.pointer=t,this}smoothCurveTo(t,e){const{endTangent:n,startTangent:s,startFactor:r,endFactor:o}=yo(e),a=this.pendingSegments.length?this.pendingSegments[this.pendingSegments.length-1]:null,h=J(this.pointer,t)/3;let l;s?l=s:a?l=a.tangentAtLastPoint:l=[1,0],l=rt(l);const c=[this.pointer[0]+l[0]*r*h,this.pointer[1]+l[1]*r*h];let u=n;u=rt(u);const m=[t[0]-u[0]*o*h,t[1]-u[1]*o*h];return this.cubicBezierCurveTo(t,c,m)}smoothCurve(t,e,n){return this.smoothCurveTo([t+this.pointer[0],e+this.pointer[1]],n)}customCorner(t,e="fillet"){if(!this.pendingSegments.length)throw new Error("You need a segment defined to fillet the angle");return t?(this._nextCorner={mode:e,radius:t},this):this}_customCornerLastWithFirst(t,e="fillet"){if(!t)return;const n=this.pendingSegments.pop(),s=this.pendingSegments.shift();if(!n||!s)throw new Error("Not enough curves to close and fillet");const r=e==="chamfer"?Ri:ki;this.pendingSegments.push(...r(n,s,t))}close(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],n=this.pendingSegments.at(-1);return k(e.firstPoint,n.lastPoint)||this.lineTo(e.firstPoint),this._nextCorner!==null&&(this._customCornerLastWithFirst(this._nextCorner.radius,this._nextCorner.mode),this._nextCorner=null),Ci(this.pendingSegments,{ignoreChecks:t})}closeWithMirror(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],n=this.pendingSegments.at(-1),s=q(n.lastPoint,e.firstPoint),r=new ft().mirrorLine(s,e.firstPoint),o=this.pendingSegments.map(a=>a.transform(r).reverse());return o.reverse(),Ci([...this.pendingSegments,...o],{ignoreChecks:t})}asStrand(){return new Rt([...this.pendingSegments])}get isClosed(){var t;return k(this.pointer,(t=this.pendingSegments[0])==null?void 0:t.firstPoint)}};function Ti(i=[0,0]){return new bo(i)}function Ct([i,t]){return`${i} ${t}`}function gn(i){if(i instanceof D)return`L ${Ct(i.lastPoint)}`;if(i instanceof z)return`A ${i.radius} ${i.radius} 0 ${i.angularLength>Math.PI?"1":"0"} ${i.clockwise?"0":"1"} ${Ct(i.lastPoint)}`;if(i instanceof ht)return`A ${i.majorRadius} ${i.minorRadius} ${i.tiltAngle*Ue} ${i.deltaAngle>Math.PI?"1":"0"} ${i.clockwise?"0":"1"} ${Ct(i.lastPoint)}`;if(i instanceof et)return`Q ${[Ct(i.controlPoint),Ct(i.lastPoint)].join(" ")}`;if(i instanceof ot)return`C ${[Ct(i.firstControlPoint),Ct(i.lastControlPoint),Ct(i.lastPoint)].join(" ")}`;throw new Error("Unknown segment type")}function Ii(i){const t=`M ${i.firstPoint.join(" ")}`,e=i.segments.map(gn).join(" ");return`${t} ${e} Z`}function $i(i){return`<path d="${i.allLoops.map(Ii).join(" ")}" />`}function xo(i){return`<g>
|
|
11
|
-
${i.figures.map(
|
|
10
|
+
`}get info(){return this.repr}get firstPoint(){return this.segments[0].firstPoint}get lastPoint(){return this.segments[this.segments.length-1].lastPoint}get segmentsCount(){return this.segments.length}onStroke(t){return this.segments.some(e=>e.isOnSegment(t))}intersects(t){return this.boundingBox.overlaps(t.boundingBox)?this.segments.some(e=>t.segments.some(n=>se(e,n).count>0)):!1}overlappingSegments(t){return this.segments.flatMap(e=>t.segments.flatMap(n=>e.boundingBox.overlaps(n.boundingBox)?se(e,n).overlaps:[]))}get boundingBox(){if(this._boundingBox===null){let t=this.segments[0].boundingBox;this.segments.slice(1).forEach(e=>{t=t.merge(e.boundingBox)}),this._boundingBox=t}return this._boundingBox}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}function Gr(i,t="Stroke"){ui(i.length).forEach(([e,n])=>{if(e===n)return;const s=i[e],r=i[n],o=se(s,r),a=Math.max(s.precision,r.precision);if(o.count!==0){if(o.count===1&&!o.overlaps.length){const h=e-n,l=o.intersections[0];if(h===1&&S(s.firstPoint,l,a)||h===-1&&S(s.lastPoint,l,a)||h===i.length-1&&S(s.lastPoint,l,a)&&S(r.firstPoint,l,a)||-h===i.length-1&&S(s.firstPoint,l,a)&&S(r.lastPoint,l,a))return}if(!(o.count===2&&i.length===2&&(S(s.firstPoint,o.intersections[0],a)&&S(s.lastPoint,o.intersections[1],a)||S(s.firstPoint,o.intersections[1],a)&&S(s.lastPoint,o.intersections[0],a))))throw new Error(`${t} segments must not intersect, but segments ${s.info} and ${r.info} do at ${JSON.stringify(o.intersections)}`)}})}function fi(i,t="Stroke"){if(i.length===0)throw new Error(`${t} must have at least one segment`);Mt([i.slice(0,-1),i.slice(1)]).forEach(([e,n])=>{if(!S(e.lastPoint,n.firstPoint))throw new Error(`${t} segments must be connected, but ${e.info} and ${n.info} are not`)}),Gr(i,t)}function mi(i,t){return!!(U.isInstance(i)&&U.isInstance(t)&&Ue(i.V,t.V)||D.isInstance(i)&&D.isInstance(t)&&S(i.center,t.center)&&i.radius-t.radius<i.precision)}function gi(i,t){if(U.isInstance(i)&&U.isInstance(t))return new U(i.firstPoint,t.lastPoint);if(D.isInstance(i)&&D.isInstance(t))return new D(i.firstPoint,t.lastPoint,i.center,i.clockwise);throw new Error("Not implemented")}function pi(i){let t=!1;const e=[];for(const n of i.segments){if(e.length===0){e.push(n);continue}const s=e[e.length-1];mi(s,n)?(t=!0,e.pop(),e.push(gi(s,n))):e.push(n)}if(S(i.firstPoint,i.lastPoint)&&mi(e[0],e[e.length-1])){t=!0;const n=e.pop();e[0]=gi(n,e[0])}return t?e:null}const wi=Symbol.for("pantograph:Strand");let St=class Gt extends ci{constructor(t,{ignoreChecks:e=!1}={}){super(t,{ignoreChecks:e}),lt(this,"strokeType","STRAND"),Object.defineProperty(this,wi,{value:!0})}static isInstance(t){return!!t&&t[wi]===!0}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new Gt(t,{ignoreChecks:!0})}clone(){return new Gt(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}extend(t){if(!S(this.lastPoint,t.firstPoint))throw console.error(this.repr,t.repr),new Error("Cannot extend strand: connection point is not the same");return new Gt([...this.segments,...t.segments])}simplify(){const t=pi(this);return t?new Gt(t,{ignoreChecks:!0}):this}transform(t){return new Gt(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}};const Jr=(i,t)=>{const e=me(t,{V:[1,0],firstPoint:i,precision:t.precision});if(e==="parallel")return 0;const{intersectionParam1:n,intersectionParam2:s}=e;if(!t.isValidParameter(n)||s<=-t.precision)return 0;if(Math.abs(n)<t.precision||Math.abs(n-1)<t.precision){const[,r]=t.midPoint;return i[1]-r<0?1:0}return 1};let en=class{constructor(t){lt(this,"_count",0),lt(this,"segment"),this.segment=t}update(t,e=!1){!e&&!this.segment.isOnSegment(t)||(S(t,this.segment.firstPoint)?this._count+=this.segment.tangentAtFirstPoint[1]>0?1:0:S(t,this.segment.lastPoint)?this._count+=this.segment.tangentAtLastPoint[1]>0?0:1:this._count+=1)}get count(){return this._count}};const Zr=(i,t)=>{const e=t.precision,n=Math.abs(i[1]-t.center[1]);if(n>t.radius+e)return 0;const s=$t(i,t.center),r=t.radius*t.radius,o=e*e;if(Math.abs(s-r)<o&&t.isOnSegment(i))return 0;const a=s-r>o;if(a&&t.center[0]<i[0])return 0;const h=Math.sqrt(t.radius*t.radius-n*n),l=new en(t);return l.update([t.center[0]+h,i[1]]),a&&l.update([t.center[0]-h,i[1]]),l.count},Kr=(i,t)=>{const e=t.boundingBox.xMax+t.boundingBox.width/2,n=new U(i,[e,i[1]]),s=new en(t);return Ze(n,t).forEach(r=>{s.update(r,!0)}),s.count},to=(i,t)=>{const e=new en(t);return t.paramsAtY(i[1]).map(n=>{try{return t.paramPoint(n)}catch{return null}}).filter(n=>n!==null).filter(n=>{const[s]=n;return s>=i[0]}).forEach(n=>{e.update(n,!0)}),e.count};function eo(i,t){if(U.isInstance(t))return Jr(i,t);if(D.isInstance(t))return Zr(i,t);if(at.isInstance(t))return Kr(i,t);if(ot.isInstance(t)||et.isInstance(t))return to(i,t);throw new Error("Not implemented")}const di=Symbol.for("pantograph:Loop");let Vt=class le extends ci{constructor(t,{ignoreChecks:e=!1}={}){super(t,{ignoreChecks:!0}),lt(this,"strokeType","LOOP"),lt(this,"_clockwise",null),Object.defineProperty(this,di,{value:!0}),e||no(t)}static isInstance(t){return!!t&&t[di]===!0}get clockwise(){if(this._clockwise===null){const t=this.segments.flatMap(n=>U.isInstance(n)?[n.firstPoint]:[n.firstPoint,n.paramPoint(.5)]),e=t.map((n,s)=>{const r=t[(s+1)%t.length];return(r[0]-n[0])*(r[1]+n[1])}).reduce((n,s)=>n+s,0);this._clockwise=e>0}return this._clockwise}clone(){return new le(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new le(t,{ignoreChecks:!0})}transform(t){return new le(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}contains(t,{strokeIsInside:e=!1}={}){return this.onStroke(t)?e:this.boundingBox.contains(t)?this.segments.reduce((n,s)=>n+eo(t,s),0)%2===1:!1}simplify(){const t=pi(this);return t?new le(t,{ignoreChecks:!0}):this}};function no(i){if(fi(i,"Loop"),!S(i[0].firstPoint,i[i.length-1].lastPoint))throw new Error("Loop segment must be closed")}const io=[U,D,at,et,ot];function Pi(i){return io.some(t=>t.isInstance(i))}function Mi(i){if(U.isInstance(i))return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint};if(D.isInstance(i))return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise};if(at.isInstance(i))return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise,majorRadius:i.majorRadius,minorRadius:i.minorRadius,tiltAngle:i.tiltAngle};if(et.isInstance(i))return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,controlPoint:i.controlPoint};if(ot.isInstance(i))return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,firstControlPoint:i.firstControlPoint,lastControlPoint:i.lastControlPoint};throw new Error("Unknown segment type")}function nn(i){return{type:"LOOP",segments:i.segments.map(Mi)}}function yi(i){return{type:"FIGURE",contour:nn(i.contour),holes:i.holes.map(nn)}}function so(i){return{type:"DIAGRAM",figures:i.figures.map(yi)}}function bi(i){if(Pt.isInstance(i))return so(i);if(pt.isInstance(i))return yi(i);if(Vt.isInstance(i))return nn(i);if(Pi(i))return Mi(i);throw new Error("Unknown shape type")}class ro{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,e){let n=this.length++;for(;n>0;){const s=n-1>>1,r=this.values[s];if(e>=r)break;this.ids[n]=this.ids[s],this.values[n]=r,n=s}this.ids[n]=t,this.values[n]=e}pop(){if(this.length===0)return;const t=this.ids[0];if(this.length--,this.length>0){const e=this.ids[0]=this.ids[this.length],n=this.values[0]=this.values[this.length],s=this.length>>1;let r=0;for(;r<s;){let o=(r<<1)+1;const a=o+1;let h=this.ids[o],l=this.values[o];const c=this.values[a];if(a<this.length&&c<l&&(o=a,h=this.ids[a],l=c),l>=n)break;this.ids[r]=h,this.values[r]=l,r=o}this.ids[r]=e,this.values[r]=n}return t}peek(){if(this.length!==0)return this.ids[0]}peekValue(){if(this.length!==0)return this.values[0]}shrink(){this.ids.length=this.values.length=this.length}}const xi=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],sn=3;class rn{static from(t,e=0){if(e%8!==0)throw new Error("byteOffset must be 8-byte aligned.");if(!t||t.byteLength===void 0||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[n,s]=new Uint8Array(t,e+0,2);if(n!==251)throw new Error("Data does not appear to be in a Flatbush format.");const r=s>>4;if(r!==sn)throw new Error(`Got v${r} data when expected v${sn}.`);const o=xi[s&15];if(!o)throw new Error("Unrecognized array type.");const[a]=new Uint16Array(t,e+2,1),[h]=new Uint32Array(t,e+4,1);return new rn(h,a,o,void 0,t,e)}constructor(t,e=16,n=Float64Array,s=ArrayBuffer,r,o=0){if(t===void 0)throw new Error("Missing required argument: numItems.");if(isNaN(t)||t<=0)throw new Error(`Unexpected numItems value: ${t}.`);this.numItems=+t,this.nodeSize=Math.min(Math.max(+e,2),65535),this.byteOffset=o;let a=t,h=a;this._levelBounds=[a*4];do a=Math.ceil(a/this.nodeSize),h+=a,this._levelBounds.push(h*4);while(a!==1);this.ArrayType=n,this.IndexArrayType=h<16384?Uint16Array:Uint32Array;const l=xi.indexOf(this.ArrayType),c=h*4*this.ArrayType.BYTES_PER_ELEMENT;if(l<0)throw new Error(`Unexpected typed array class: ${n}.`);r&&r.byteLength!==void 0&&!r.buffer?(this.data=r,this._boxes=new this.ArrayType(this.data,o+8,h*4),this._indices=new this.IndexArrayType(this.data,o+8+c,h),this._pos=h*4,this.minX=this._boxes[this._pos-4],this.minY=this._boxes[this._pos-3],this.maxX=this._boxes[this._pos-2],this.maxY=this._boxes[this._pos-1]):(this.data=new s(8+c+h*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+c,h),this._pos=0,this.minX=1/0,this.minY=1/0,this.maxX=-1/0,this.maxY=-1/0,new Uint8Array(this.data,0,2).set([251,(sn<<4)+l]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=t),this._queue=new ro}add(t,e,n=t,s=e){const r=this._pos>>2,o=this._boxes;return this._indices[r]=r,o[this._pos++]=t,o[this._pos++]=e,o[this._pos++]=n,o[this._pos++]=s,t<this.minX&&(this.minX=t),e<this.minY&&(this.minY=e),n>this.maxX&&(this.maxX=n),s>this.maxY&&(this.maxY=s),r}finish(){if(this._pos>>2!==this.numItems)throw new Error(`Added ${this._pos>>2} items when expected ${this.numItems}.`);const t=this._boxes;if(this.numItems<=this.nodeSize){t[this._pos++]=this.minX,t[this._pos++]=this.minY,t[this._pos++]=this.maxX,t[this._pos++]=this.maxY;return}const e=this.maxX-this.minX||1,n=this.maxY-this.minY||1,s=new Uint32Array(this.numItems),r=65535;for(let o=0,a=0;o<this.numItems;o++){const h=t[a++],l=t[a++],c=t[a++],u=t[a++],m=Math.floor(r*((h+c)/2-this.minX)/e),w=Math.floor(r*((l+u)/2-this.minY)/n);s[o]=ao(m,w)}on(s,t,this._indices,0,this.numItems-1,this.nodeSize);for(let o=0,a=0;o<this._levelBounds.length-1;o++){const h=this._levelBounds[o];for(;a<h;){const l=a;let c=t[a++],u=t[a++],m=t[a++],w=t[a++];for(let g=1;g<this.nodeSize&&a<h;g++)c=Math.min(c,t[a++]),u=Math.min(u,t[a++]),m=Math.max(m,t[a++]),w=Math.max(w,t[a++]);this._indices[this._pos>>2]=l,t[this._pos++]=c,t[this._pos++]=u,t[this._pos++]=m,t[this._pos++]=w}}}search(t,e,n,s,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const a=[],h=[];for(;o!==void 0;){const l=Math.min(o+this.nodeSize*4,Ai(o,this._levelBounds));for(let c=o;c<l;c+=4){if(n<this._boxes[c]||s<this._boxes[c+1]||t>this._boxes[c+2]||e>this._boxes[c+3])continue;const u=this._indices[c>>2]|0;o>=this.numItems*4?a.push(u):(r===void 0||r(u))&&h.push(u)}o=a.pop()}return h}neighbors(t,e,n=1/0,s=1/0,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const a=this._queue,h=[],l=s*s;t:for(;o!==void 0;){const c=Math.min(o+this.nodeSize*4,Ai(o,this._levelBounds));for(let u=o;u<c;u+=4){const m=this._indices[u>>2]|0,w=vi(t,this._boxes[u],this._boxes[u+2]),g=vi(e,this._boxes[u+1],this._boxes[u+3]),p=w*w+g*g;p>l||(o>=this.numItems*4?a.push(m<<1,p):(r===void 0||r(m))&&a.push((m<<1)+1,p))}for(;a.length&&a.peek()&1;)if(a.peekValue()>l||(h.push(a.pop()>>1),h.length===n))break t;o=a.length?a.pop()>>1:void 0}return a.clear(),h}}function vi(i,t,e){return i<t?t-i:i<=e?0:i-e}function Ai(i,t){let e=0,n=t.length-1;for(;e<n;){const s=e+n>>1;t[s]>i?n=s:e=s+1}return t[e]}function on(i,t,e,n,s,r){if(Math.floor(n/r)>=Math.floor(s/r))return;const o=i[n+s>>1];let a=n-1,h=s+1;for(;;){do a++;while(i[a]<o);do h--;while(i[h]>o);if(a>=h)break;oo(i,t,e,a,h)}on(i,t,e,n,h,r),on(i,t,e,h+1,s,r)}function oo(i,t,e,n,s){const r=i[n];i[n]=i[s],i[s]=r;const o=4*n,a=4*s,h=t[o],l=t[o+1],c=t[o+2],u=t[o+3];t[o]=t[a],t[o+1]=t[a+1],t[o+2]=t[a+2],t[o+3]=t[a+3],t[a]=h,t[a+1]=l,t[a+2]=c,t[a+3]=u;const m=e[n];e[n]=e[s],e[s]=m}function ao(i,t){let e=i^t,n=65535^e,s=65535^(i|t),r=i&(t^65535),o=e|n>>1,a=e>>1^e,h=s>>1^n&r>>1^s,l=e&s>>1^r>>1^r;e=o,n=a,s=h,r=l,o=e&e>>2^n&n>>2,a=e&n>>2^n&(e^n)>>2,h^=e&s>>2^n&r>>2,l^=n&s>>2^(e^n)&r>>2,e=o,n=a,s=h,r=l,o=e&e>>4^n&n>>4,a=e&n>>4^n&(e^n)>>4,h^=e&s>>4^n&r>>4,l^=n&s>>4^(e^n)&r>>4,e=o,n=a,s=h,r=l,h^=e&s>>8^n&r>>8,l^=n&s>>8^(e^n)&r>>8,e=h^h>>1,n=l^l>>1;let c=i^t,u=n|65535^(c|e);return c=(c|c<<8)&16711935,c=(c|c<<4)&252645135,c=(c|c<<2)&858993459,c=(c|c<<1)&1431655765,u=(u|u<<8)&16711935,u=(u|u<<4)&252645135,u=(u|u<<2)&858993459,u=(u|u<<1)&1431655765,(u<<1|c)>>>0}function Ei(i,t=1e-7){if(i.length===0)return[];if(i.length===1)return[i];const e=new rn(i.length);i.forEach(r=>{const[o,a]=r.firstPoint;e.add(o-t,a-t,o+t,a+t)}),e.finish();const n=[],s=new Set;return i.forEach((r,o)=>{if(s.has(o))return;const a=[r];let h=o;s.add(o);let l=i.length;for(;;){if(l--<0)throw new Error("Infinite loop detected");const c=a[a.length-1].lastPoint,[u,m]=c,w=e.search(u-t,m-t,u+t,m+t),g=P=>Math.abs((h-P)%i.length),p=w.filter(P=>!s.has(P)).map(P=>[i[P],P,g(P)]).sort(([,,P],[,,b])=>g(P)-g(b));if(p.length===0){n.push(a);break}const[M,f]=p[0];a.push(M),s.add(f),h=f}}),n}const Ii=Symbol.for("pantograph:Figure");let pt=class Oe extends Ft{constructor(t,e=[],{ignoreChecks:n=!1}={}){super(),lt(this,"contour"),lt(this,"holes"),Object.defineProperty(this,Ii,{value:!0}),n||ho(t,e),this.contour=t,this.holes=e}static isInstance(t){return!!t&&t[Ii]===!0}get boundingBox(){return this.contour.boundingBox}get isFull(){return this.holes.length===0}get allLoops(){return[this.contour,...this.holes]}clone(){return new Oe(this.contour.clone(),this.holes.map(t=>t.clone()))}transform(t){return new Oe(this.contour.transform(t),this.holes.map(e=>e.transform(t)))}contains(t,{strokeIsInside:e=!1}={}){return this.contour.contains(t,{strokeIsInside:e})&&!this.holes.some(n=>n.contains(t,{strokeIsInside:e}))}intersects(t){return this.allLoops.some(e=>t.allLoops.some(n=>e.intersects(n)))}overlappingStrands(t){const e=Oe.isInstance(t)?t.allLoops:[t],n=this.allLoops.flatMap(s=>e.flatMap(r=>s.overlappingSegments(r)));return Ei(n).map(s=>new St(s))}};function ho(i,t=[]){if(!i)throw new Error("Figure must have a contour");for(const[e,n]of tn([i,...t]))if(e.intersects(n))throw new Error("Loops in a figure must not intersect");if(t.some(e=>!i.contains(e.firstPoint)&&!i.onStroke(e.firstPoint)))throw new Error("Holes must be inside the contour");for(const[e,n]of tn(t))if(e.contains(n.firstPoint))throw console.error(bi(e),bi(n)),new Error("Holes must not be inside other holes")}const Se=(i,t,e=1e-7)=>Math.abs(i-t)<=e,lo=(i,t)=>{const e=i.boundingBox,n=t.boundingBox;return Se(e.xMin,n.xMin)&&Se(e.yMin,n.yMin)&&Se(e.xMax,n.xMax)&&Se(e.yMax,n.yMax)},uo=(i,t)=>{if(i.segmentsCount!==t.segmentsCount||!lo(i,t))return!1;const e=i.segments,n=t.segments,s=e.length,r=(o,a)=>{for(let h=0;h<s;h+=1){const l=(o+a*h+s)%s;if(!e[h].isSame(n[l]))return!1}return!0};for(let o=0;o<s;o+=1)if(e[0].isSame(n[o])&&(r(o,1)||r(o,-1)))return!0;return!1},co=i=>{const t=[];return i.forEach(e=>{t.some(n=>uo(e,n))||t.push(e)}),t},fo=i=>{const t=i.map((s,r)=>i.slice(r+1).map((o,a)=>[a+r+1,o]).filter(([,o])=>s.boundingBox.overlaps(o.boundingBox)).map(([o])=>o)),e=[],n=Array(t.length);return t.forEach((s,r)=>{let o=n[r];o||(o=[],e.push(o)),o.push(i[r]),s.length&&s.forEach(a=>{n[a]=o})}),e},Si=i=>i.map((t,e)=>{const n=t.segments[0].midPoint,s=i.filter((r,o)=>e===o?!1:r.contains(n));return{loop:t,isIn:s}}),mo=(i,t)=>i.flatMap(({loop:e})=>an(t.filter(({loop:n,isIn:s})=>n===e||s.indexOf(e)!==-1))),go=(i,t)=>{const e=t.filter(({isIn:s})=>s.length<=1),n=an(Si(i.map(({loop:s})=>s)));return[e,...n]},an=i=>{if(!i.length)return[];const t=i.filter(({isIn:n})=>!n.length),e=i.filter(({isIn:n})=>n.length>1);return t.length===1&&e.length===0?[i]:t.length>1?mo(t,i):go(e,i)};function re(i){const t=co(i);return fo(t).map(Si).flatMap(an).map(e=>{if(e.length===1)return new pt(e[0].loop);e.sort((r,o)=>r.isIn.length-o.isIn.length);const[n,...s]=e.map(({loop:r})=>r);return new pt(n,s)})}function po(i,t){const e=[];for(const n of i)for(const s of t)e.push([n,s]);return e}function*hn(i,t,e){const n=a=>t.some(h=>S(h,a.lastPoint)),s=(a,h)=>a.segmentType!==h.segmentType||!h.isOnSegment(a.firstPoint)||!h.isOnSegment(a.lastPoint)?!1:a.segmentType!=="LINE"?h.isOnSegment(a.midPoint):!0,r=a=>e.some(h=>a.isSame(h)||s(a,h));let o=[];for(const a of i)n(a)?(o.push(a),yield new St(o,{ignoreChecks:!0}),o=[]):r(a)?(o.length&&(yield new St(o,{ignoreChecks:!0}),o=[]),yield new St([a],{ignoreChecks:!0})):o.push(a);o.length&&(yield new St(o,{ignoreChecks:!0}))}const ki=(i,t)=>{const e=i.findIndex(s=>S(t,s.firstPoint)),n=i.slice(0,e);return i.slice(e).concat(n)},Ci=(i,t)=>{let e=i;const n=o=>S(o.firstPoint,t.firstPoint)&&S(o.lastPoint,t.lastPoint);let s=i.findIndex(n);if(s===-1){const o=i.map(a=>a.reverse());if(o.reverse(),s=o.findIndex(n),s===-1)throw console.error(o.map(a=>a.repr),t.repr),new Error("Failed to rotate to segment start");e=o}const r=e.slice(0,s);return e.slice(s).concat(r)};function wo(i,t,e){return i.filter(n=>{const s=t.filter(o=>S(o.firstPoint,n)||S(o.lastPoint,n));if(s.length%2)throw new Error("Bug in the intersection algo on non crossing point");const r=s.map(o=>e.contains(o.midPoint));return!(r.every(o=>o)||!r.some(o=>o))})}function Po(i,t,e,n=!1){let s=[];const r=[],o=new Array(i.segments.length).fill(0).map(()=>[]),a=new Array(t.segments.length).fill(0).map(()=>[]);if(i.segments.forEach((w,g)=>{t.segments.forEach((p,M)=>{const{intersections:f,overlaps:P}=se(w,p,e);s.push(...f),o[g].push(...f),a[M].push(...f),r.push(...P);const b=P.flatMap(I=>[I.firstPoint,I.lastPoint]);s.push(...b),o[g].push(...b),a[M].push(...b)})}),s=vt(s,e),!s.length||s.length===1)return null;const h=([w,g])=>g.length?w.splitAt(g):[w];let l=Mt([i.segments,o]).flatMap(h),c=Mt([t.segments,a]).flatMap(h);if(s=wo(s,l,t),!s.length&&!r.length)return null;if(r.length){const w=r[0];l=Ci(l,w),c=Ci(c,w)}else{const w=s[0];l=ki(l,w),c=ki(c,w)}let u=Array.from(hn(l,s,r)),m=Array.from(hn(c,s,r));return(!S(m[0].lastPoint,u[0].lastPoint)||r.length>0&&m[0].segmentsCount!==1)&&(m=m.map(w=>w.reverse()).reverse(),S(m[0].lastPoint,u[0].lastPoint)||(u=u.map(w=>w.reverse()).reverse())),Mt([u,m]).map(([w,g])=>{if(n){if((p=>p.segments.every(M=>r.some(f=>M.isSame(f)||M.segmentType===f.segmentType&&f.isOnSegment(M.firstPoint)&&f.isOnSegment(M.lastPoint)&&(M.segmentType==="LINE"||f.isOnSegment(M.midPoint)))))(w))return[w,"same"]}else if(w.segmentsCount===1&&r.some(p=>w.segments[0].isSame(p)))return[w,"same"];return[w,g]})}function Ri(i){let t=i[0];for(const e of i.slice(1))t=t.extend(e);if(!S(t.firstPoint,t.lastPoint))throw console.error(st(t.firstPoint),st(t.lastPoint)),new Error("Bug in the intersection algo on non closing strand");return new Vt(t.segments)}function Mo(i,t){const e=Mt([t.slice(0,-1),t.slice(1)]).map(([s,r])=>Ri(i.slice(s,r)));let n=i.slice(t[t.length-1]);return t[0]!==0&&(n=n.concat(i.slice(0,t[0]))),e.push(Ri(n)),e}function yo(i){if(!i.length)return[];const t=i.map(s=>s.firstPoint);let e=i.map(s=>s.lastPoint);e=e.slice(-1).concat(e.slice(0,-1));const n=Mt([t,e]).flatMap(([s,r],o)=>S(s,r)?[]:o);try{return Mo(i,n)}catch{return Ei(i.flatMap(s=>s.segments)).filter(s=>s.length>1).filter(s=>S(s[0].firstPoint,s.at(-1).lastPoint)).map(s=>new Vt(s))}}const Ti=(i,t)=>{if(i.length===0)return[t];const e=i.at(-1);return S(e.lastPoint,t.firstPoint)?i.slice(0,-1).concat([e.extend(t)]):S(e.lastPoint,t.lastPoint)?i.slice(0,-1).concat([e.extend(t.reverse())]):i.concat([t])},bo=(i,t)=>i.length===0?[t]:S(i[0].firstPoint,t.lastPoint)?[t.extend(i[0])].concat(i.slice(1)):[t].concat(i);function ln(i,t,{firstInside:e,secondInside:n,firstBoundaryInside:s=!1,secondBoundaryInside:r=!1}){const o=Po(i,t,void 0,s||r);if(!o){const c=i.segments[0].midPoint,u=t.contains(c,{strokeIsInside:r}),m=t.segments[0].midPoint,w=i.contains(m,{strokeIsInside:s});return{identical:!1,firstCurveInSecond:u,secondCurveInFirst:w}}if(o.every(([,c])=>c==="same"))return{identical:!0};let a=null,h=null;const l=o.flatMap(([c,u])=>{let m=[],w=0;if(u==="same")return h===1?(h=1,c):h===2||h===0?(h=null,[]):h===null?(a?a=a.extend(c):a=c,[]):(console.error("weird situation"),[]);const g=c.segments[0].midPoint,p=t.contains(g,{strokeIsInside:r});(e==="keep"&&p||e==="remove"&&!p)&&(w+=1,m=Ti(m,c));const M=u.segments[0].midPoint,f=i.contains(M,{strokeIsInside:s});if(n==="keep"&&f||n==="remove"&&!f){const P=u;w+=1,w===2&&m.length?(m=Ti(m,P),a=null):m=[P]}return h===null&&w===1&&a&&(m=bo(m,a)),w===1&&(h=w,a=null),m.length?m:(a=null,[])});return yo(l)}const xo=(i,t,e)=>{const n=ln(i,t,{firstInside:"remove",secondInside:"remove",...e});return Array.isArray(n)?n:n.identical?[i]:n.firstCurveInSecond?[t]:n.secondCurveInFirst?[i]:[i,t]},ke=(i,t,e)=>{const n=ln(i,t,{firstInside:"remove",secondInside:"keep",...e});return Array.isArray(n)?n:n.identical?[]:n.firstCurveInSecond?[]:n.secondCurveInFirst?[i,t]:[i]},un=(i,t,e)=>{const n=(e==null?void 0:e.firstBoundaryInside)??!1,s=(e==null?void 0:e.secondBoundaryInside)??!1,r=n||s,o=(h,l,c)=>h.segments.every(u=>l.contains(u.midPoint,{strokeIsInside:c}));if(r){if(o(i,t,s))return[i];if(o(t,i,n))return[t]}const a=ln(i,t,{firstInside:"keep",secondInside:"keep",...e});return Array.isArray(a)?a:a.identical?[i]:a.firstCurveInSecond?[i]:a.secondCurveInFirst?[t]:[]};function vo(i){const t=new Map,e=[];return i.forEach((n,s)=>{let r;t.has(s)?r=t.get(s):(r={current:[n],fusedWith:new Set([s])},e.push(r)),i.slice(s+1).forEach((o,a)=>{const h=r.current,l=s+a+1;if(r.fusedWith.has(l))return;let c=[o],u=!1;if(t.has(l)&&(c=t.get(l).current,u=!0),!h.some(w=>c.some(g=>w.intersects(g))))return;let m;h.length>1||c.length>1?m=oe(h,c):m=$i(h[0],c[0]),r.fusedWith.add(l),r.current=m,u||t.set(l,r)})}),e.flatMap(({current:n})=>n)}function $i(i,t){const e=xo(i.contour,t.contour),n=t.holes.flatMap(o=>ke(o,i.contour)),s=i.holes.flatMap(o=>ke(o,t.contour)),r=po(i.holes,t.holes).flatMap(([o,a])=>un(o,a));return re([...e,...n,...s,...r])}function Ce(i,t){if(i.isFull&&t.isFull)return re(ke(i.contour,t.contour));if(i.isFull){const n=ke(i.contour,t.contour),s=t.holes.flatMap(r=>un(r,i.contour,{firstBoundaryInside:!0}));return re([...n,...s])}else if(t.isFull&&!i.contour.intersects(t.contour))if(i.contour.contains(t.contour.firstPoint)){const n=oe(i.holes.map(s=>new pt(s)),[t]);return re([i.contour,...n.flatMap(s=>s.allLoops)])}else return[i];let e=Ce(new pt(i.contour),t);return i.holes.forEach(n=>{e=e.flatMap(s=>Ce(s,new pt(n)))}),e}function Ao(i,t){const e=un(i.contour,t.contour);if(!e.length)return[];let n=re(e);return n=Ut(n,i.holes.map(s=>new pt(s))),Ut(n,t.holes.map(s=>new pt(s)))}function oe(i,t){if(!i.length)return t;if(!t.length)return i;if(i.length===1&&t.length>1||t.length===1&&i.length>1)return vo([...i,...t]);if(i.length>1&&t.length>1){let e=oe([i[0]],t);return i.slice(1).forEach(n=>{e=oe([n],e)}),e}return i.length===1&&t.length===1?$i(i[0],t[0]):[]}function Ut(i,t){if(!i.length)return[];if(!t.length)return i;if(i.length===1&&t.length===1)return Ce(i[0],t[0]);if(i.length>1)return i.flatMap(n=>Ut([n],t));let e=Ce(i[0],t[0]);return t.slice(1).forEach(n=>{e=Ut(e,[n])}),e}function cn(i,t){return!i.length||!t.length?[]:i.length===1&&t.length===1?Ao(i[0],t[0]):i.length>1?i.flatMap(e=>cn([e],t)):t.flatMap(e=>cn(i,[e]))}const Fi=Symbol.for("pantograph:Diagram");class Pt extends Ft{constructor(t=[],{ignoreChecks:e=!1}={}){super(),lt(this,"figures"),lt(this,"_boundingBox",null),Object.defineProperty(this,Fi,{value:!0}),e||Eo(t),this.figures=t}static isInstance(t){return!!t&&t[Fi]===!0}get isEmpty(){return this.figures.length===0}get boundingBox(){if(this.isEmpty)return new Jt;if(this._boundingBox===null){let t=this.figures[0].boundingBox;for(const e of this.figures.slice(1))t=t.merge(e.boundingBox);this._boundingBox=t}return this._boundingBox}clone(){return new Pt(this.figures.map(t=>t.clone()))}transform(t){return new Pt(this.figures.map(e=>e.transform(t)))}contains(t,{strokeIsInside:e=!1}={}){return this.figures.some(n=>n.contains(t,{strokeIsInside:e}))}intersects(t){return this.figures.some(e=>t.figures.some(n=>e.intersects(n)))}overlappingStrands(t){return this.figures.flatMap(e=>Pt.isInstance(t)?t.figures.flatMap(n=>e.overlappingStrands(n)):e.overlappingStrands(t))}fuse(t){return new Pt(oe(this.figures,t.figures))}cut(t){return new Pt(Ut(this.figures,t.figures))}intersect(t){return new Pt(cn(this.figures,t.figures))}}function Eo(i){for(const[t,e]of tn(i))if(t.intersects(e))throw new Error("Diagram figures must not intersect")}var Io=Object.defineProperty,So=(i,t,e)=>t in i?Io(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,Re=(i,t,e)=>So(i,typeof t!="symbol"?t+"":t,e);const _i=(i,t,e)=>{const n=xt(i.V,t.V),s=i.precision*t.precision;if(n*n<s)return"parallel";const r=q(t.firstPoint,i.firstPoint),o=xt(r,t.V)/n;return W(i.firstPoint,H(i.V,o))},Li=Symbol.for("pantograph:DegenerateSegment");class Te{constructor(t,e){this.firstPoint=t,this.lastPoint=e,Object.defineProperty(this,Li,{value:!0})}static isInstance(t){return!!t&&t[Li]===!0}}function Bi(i,t){if(U.isInstance(i))return ko(i,t);if(D.isInstance(i))return Co(i,t);if(et.isInstance(i)||ot.isInstance(i))return Ro(i,t);throw new Error("Not implemented")}function ko(i,t){const{firstPoint:e,lastPoint:n}=i,s=i.normalVector;return new U(W(e,H(s,t)),W(n,H(s,t)))}function Co(i,t){const e=W(i.firstPoint,H(tt(i.tangentAtFirstPoint),t)),n=W(i.lastPoint,H(tt(i.tangentAtLastPoint),t)),s=t*(i.clockwise?1:-1);return i.radius+s<i.precision?new Te(e,n):new D(e,n,i.center,i.clockwise)}function fn(i,t,e,n,s){const r=_i({V:e,firstPoint:n,precision:s},{V:q(t,i),firstPoint:i,precision:s});if(r==="parallel")throw new Error("Parallel lines not expected in safe bezier offset control point calculation");return r}function Ro(i,t){const{firstPoint:e,lastPoint:n,normalAtFirstPoint:s,normalAtLastPoint:r}=i,o=_i({V:s,firstPoint:e,precision:i.precision},{V:r,firstPoint:n,precision:i.precision}),a=W(e,H(s,t)),h=W(n,H(r,t));if(o==="parallel")throw new Error("Parallel lines not expected in safe bezier offset");if(ce(q(o,e),s)*t>0&&Math.min(Z(e,o),Z(n,o))<t)return new Te(a,h);if(et.isInstance(i)){const u=fn(o,i.controlPoint,i.tangentAtFirstPoint,a,i.precision);return new et(a,h,u)}const l=fn(o,i.firstControlPoint,i.tangentAtFirstPoint,a,i.precision),c=fn(o,i.lastControlPoint,i.tangentAtLastPoint,h,i.precision);return new ot(a,h,l,c)}function Vi(i,t,e){const n=xt(i.tangentAtLastPoint,t.tangentAtFirstPoint);if(Math.abs(n)<1e-10)return null;const s=n>0?1:-1,r=Math.abs(e)*s,o=Bi(i,r),a=Bi(t,r);if(Te.isInstance(o)||Te.isInstance(a))return null;let h;try{h=Qr(o,a,1e-9).at(-1)}catch{return null}if(!h)return null;const l=h,c=(w,g)=>{const p=g.tangentAt(l),M=Zt(p),f=W(l,H(M,r));return w.splitAt(f)},[u]=c(i,o),[,m]=c(t,a);return{first:u,second:m,center:l}}function ji(i,t,e){const n=Vi(i,t,e);if(!n)return console.warn("Cannot fillet between segments",i.repr,t.repr),[i,t];const{first:s,second:r}=n;return[s,Tn(s.lastPoint,r.firstPoint,s.tangentAtLastPoint),r]}function Oi(i,t,e){const n=Vi(i,t,e);if(!n)return console.warn("Cannot chamfer between segments",i.repr,t.repr),[i,t];const{first:s,second:r}=n;return[s,new U(s.lastPoint,r.firstPoint),r]}const To=i=>{let t;i?typeof i=="number"||Array.isArray(i)&&i.length===2?t={endTangent:i}:t={endTangent:0,...i}:t={endTangent:[1,0]};const{endTangent:e,startFactor:n=1,endFactor:s=1,startTangent:r}=t;let o;typeof e=="number"?o=At(1,e*Tt):o=e;let a;return typeof r=="number"?a=At(1,r*Tt):a=r,{endTangent:o,startFactor:n,endFactor:s,startTangent:a}};function qi(i,{ignoreChecks:t=!1}={}){return new Pt([new pt(new Vt([...i],{ignoreChecks:t}))])}class $o{constructor(t=[0,0]){Re(this,"pointer"),Re(this,"firstPoint"),Re(this,"pendingSegments"),Re(this,"_nextCorner"),this.pointer=t,this.firstPoint=t,this.pendingSegments=[],this._nextCorner=null}movePointerTo(t){if(this.pendingSegments.length)throw new Error("You can only move the pointer if there is no segment defined");return this.pointer=t,this.firstPoint=t,this}saveSegment(t){if(S(t.firstPoint,t.lastPoint))throw new Error(`Segment has no length, ${t.repr}`);if(!this._nextCorner)return this.pendingSegments.push(t),this;const e=this.pendingSegments.pop();if(!e)throw new Error("bug in the custom corner algorithm");const n=this._nextCorner.mode==="chamfer"?Oi:ji;return this.pendingSegments.push(...n(e,t,this._nextCorner.radius)),this._nextCorner=null,this}lineTo(t){const e=new U(this.pointer,t);return this.pointer=t,this.saveSegment(e)}line(t,e){return this.lineTo([this.pointer[0]+t,this.pointer[1]+e])}vLine(t){return this.line(0,t)}hLine(t){return this.line(t,0)}vLineTo(t){return this.lineTo([this.pointer[0],t])}hLineTo(t){return this.lineTo([t,this.pointer[1]])}polarLineTo([t,e]){const n=e*Tt,s=At(t,n);return this.lineTo(s)}polarLine(t,e){const n=e*Tt,[s,r]=At(t,n);return this.line(s,r)}tangentLine(t){const e=this.pendingSegments.at(-1);if(!e)throw new Error("You need a previous segment to sketch a tangent line");const[n,s]=e.tangentAtLastPoint;return this.line(n*t,s*t)}threePointsArcTo(t,e){return this.saveSegment(fs(this.pointer,e,t)),this.pointer=t,this}threePointsArc(t,e,n,s){const[r,o]=this.pointer;return this.threePointsArcTo([r+t,o+e],[r+n,o+s])}sagittaArcTo(t,e){if(!e)return this.lineTo(t);const n=new U(this.pointer,t),s=tt(n.tangentAtFirstPoint),r=W(n.midPoint,H(s,e));return this.threePointsArcTo(t,r)}sagittaArc(t,e,n){return this.sagittaArcTo([t+this.pointer[0],e+this.pointer[1]],n)}vSagittaArc(t,e){return this.sagittaArc(0,t,e)}hSagittaArc(t,e){return this.sagittaArc(t,0,e)}bulgeArcTo(t,e){if(!e)return this.lineTo(t);const n=Z(this.pointer,t)/2,s=-e*n;return this.sagittaArcTo(t,s)}bulgeArc(t,e,n){return this.bulgeArcTo([t+this.pointer[0],e+this.pointer[1]],n)}vBulgeArc(t,e){return this.bulgeArc(0,t,e)}hBulgeArc(t,e){return this.bulgeArc(t,0,e)}tangentArcTo(t,e){const n=this.pendingSegments.at(-1);if(!n)throw new Error("You need a previous curve to sketch a tangent arc");return this.saveSegment(Tn(this.pointer,t,e??n.tangentAtLastPoint)),this.pointer=t,this}tangentArc(t,e,n){const[s,r]=this.pointer;return this.tangentArcTo([t+s,e+r],n)}ellipseTo(t,e,n,s,r,o){return this.saveSegment(xr(this.pointer,t,e,n,s,r,o)),this.pointer=t,this}ellipse(t,e,n,s,r,o,a){return this.ellipseTo([t+this.pointer[0],e+this.pointer[1]],n,s,r,o,a)}halfEllipseTo(t,e){const[n,s]=In(q(t,this.pointer));return this.ellipseTo(t,n/2,Math.abs(e),s*De,!0,e>0)}halfEllipse(t,e,n){return this.halfEllipseTo([t+this.pointer[0],e+this.pointer[1]],n)}cubicBezierCurveTo(t,e,n){return this.saveSegment(new ot(this.pointer,t,e,n)),this.pointer=t,this}quadraticBezierCurveTo(t,e){return this.saveSegment(new et(this.pointer,t,e)),this.pointer=t,this}smoothCurveTo(t,e){const{endTangent:n,startTangent:s,startFactor:r,endFactor:o}=To(e),a=this.pendingSegments.length?this.pendingSegments[this.pendingSegments.length-1]:null,h=Z(this.pointer,t)/3;let l;s?l=s:a?l=a.tangentAtLastPoint:l=[1,0],l=rt(l);const c=[this.pointer[0]+l[0]*r*h,this.pointer[1]+l[1]*r*h];let u=n;u=rt(u);const m=[t[0]-u[0]*o*h,t[1]-u[1]*o*h];return this.cubicBezierCurveTo(t,c,m)}smoothCurve(t,e,n){return this.smoothCurveTo([t+this.pointer[0],e+this.pointer[1]],n)}customCorner(t,e="fillet"){if(!this.pendingSegments.length)throw new Error("You need a segment defined to fillet the angle");return t?(this._nextCorner={mode:e,radius:t},this):this}_customCornerLastWithFirst(t,e="fillet"){if(!t)return;const n=this.pendingSegments.pop(),s=this.pendingSegments.shift();if(!n||!s)throw new Error("Not enough curves to close and fillet");const r=e==="chamfer"?Oi:ji;this.pendingSegments.push(...r(n,s,t))}close(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],n=this.pendingSegments.at(-1);return S(e.firstPoint,n.lastPoint)||this.lineTo(e.firstPoint),this._nextCorner!==null&&(this._customCornerLastWithFirst(this._nextCorner.radius,this._nextCorner.mode),this._nextCorner=null),qi(this.pendingSegments,{ignoreChecks:t})}closeWithMirror(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],n=this.pendingSegments.at(-1),s=q(n.lastPoint,e.firstPoint),r=new ct().mirrorLine(s,e.firstPoint),o=this.pendingSegments.map(a=>a.transform(r).reverse());return o.reverse(),qi([...this.pendingSegments,...o],{ignoreChecks:t})}asStrand(){return new St([...this.pendingSegments])}get isClosed(){var t;return S(this.pointer,(t=this.pendingSegments[0])==null?void 0:t.firstPoint)}}function zi(i=[0,0]){return new $o(i)}function kt([i,t]){return`${i} ${t}`}function mn(i){if(U.isInstance(i))return`L ${kt(i.lastPoint)}`;if(D.isInstance(i))return`A ${i.radius} ${i.radius} 0 ${i.angularLength>Math.PI?"1":"0"} ${i.clockwise?"0":"1"} ${kt(i.lastPoint)}`;if(at.isInstance(i))return`A ${i.majorRadius} ${i.minorRadius} ${i.tiltAngle*De} ${i.deltaAngle>Math.PI?"1":"0"} ${i.clockwise?"0":"1"} ${kt(i.lastPoint)}`;if(et.isInstance(i))return`Q ${[kt(i.controlPoint),kt(i.lastPoint)].join(" ")}`;if(ot.isInstance(i))return`C ${[kt(i.firstControlPoint),kt(i.lastControlPoint),kt(i.lastPoint)].join(" ")}`;throw new Error("Unknown segment type")}function Ni(i){const t=`M ${i.firstPoint.join(" ")}`,e=i.segments.map(mn).join(" ");return`${t} ${e} Z`}function Di(i){return`<path d="${i.allLoops.map(Ni).join(" ")}" />`}function Fo(i){return`<g>
|
|
11
|
+
${i.figures.map(Di).join(`
|
|
12
12
|
`)}
|
|
13
|
-
</g>`}function
|
|
13
|
+
</g>`}function _o(i){const t=`M ${i.firstPoint.join(" ")}`,e=i.segments.map(mn).join(" ");return`${t} ${e}`}function Lo(i,t=1){const e=i.xMin-t,n=i.yMin-t;return`${e} ${n} ${i.width+2*t} ${i.height+2*t}`}function Ui(i,t,e=1,n){const s=Lo(t,e),r=n?`width="${t.width+2*e}${n}" height="${t.height+2*e}${n}"`:"";return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
14
14
|
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="${s}" fill="none" stroke="grey" stroke-width="0.2%" vector-effect="non-scaling-stroke" ${r}>
|
|
15
15
|
${i}
|
|
16
|
-
</svg>`}function
|
|
17
|
-
`),a=r.slice(1).reduce((h,l)=>h.merge(l.boundingBox),r[0].boundingBox);return
|
|
16
|
+
</svg>`}function Yi(i){if(pt.isInstance(i))return[i];if(Vt.isInstance(i))return[new pt(i)];if(Pt.isInstance(i))return i.figures;throw new Error("Unknown shape")}function Xi(i,t,e=1e-9){let n=[];const s=[],r=new Array(t.segments.length).fill(0).map(()=>[]);t.segments.forEach((a,h)=>{i.segments.forEach(l=>{const{intersections:c,overlaps:u}=se(a,l,e);n.push(...c),r[h].push(...c),s.push(...u);const m=u.flatMap(w=>[w.firstPoint,w.lastPoint]);n.push(...m),r[h].push(...m)})}),n=vt(n,e);const o=Mt([t.segments,r]).flatMap(([a,h])=>h.length?a.splitAt(h):[a]);return Array.from(hn(o,n,s))}function Wi(i,t,e=!1){return Xi(t,i).filter(n=>{const s=n.segments[0].midPoint;return t.onStroke(s)?!e:!t.contains(s)})}function Bo(i,t,e=!1){return Xi(t,i).filter(n=>{const s=n.segments[0].midPoint;return t.onStroke(s)?!e:t.contains(s)})}function Hi(i,t,e=!1){const n=Wi(i,t.contour,e),s=t.holes.flatMap(r=>Bo(i,r,e));return[...n,...s]}function Vo(i,t){return new Pt(Ut(Yi(i),Yi(t)))}function gn(i,t,e=!0){if(Vt.isInstance(t))return Wi(i,t,e);if(pt.isInstance(t))return Hi(i,t,e);let n=[new St([...i.segments])];return t.figures.forEach(s=>{n=n.flatMap(r=>Hi(r,s,e))}),n}function Qi(i){if(Pt.isInstance(i))return Fo(i);if(pt.isInstance(i))return Di(i);if(Vt.isInstance(i))return`<path d="${Ni(i)}" />`;if(St.isInstance(i))return`<path d="${_o(i)}" />`;if(Pi(i))return`<path d="${`M ${i.firstPoint.join(" ")}`} ${mn(i)}" />`;throw new Error("Unknown shape type")}const Gi=i=>"shape"in i?i.shape:i,Ji=(i,t)=>{if(!("shape"in i))return t;const{color:e}=i;return e?`<g stroke="${e}">${t}</g>`:t},Zi=i=>new Jt(i.xMin,-i.yMax,i.xMax,-i.yMin);function jo(i,{margin:t=1,unit:e=null,viewBox:n}={}){if(Array.isArray(i)){const r=i.map(h=>Gi(h).mirror()),o=r.map((h,l)=>Ji(i[l],Qi(h))).join(`
|
|
17
|
+
`),a=r.slice(1).reduce((h,l)=>h.merge(l.boundingBox),r[0].boundingBox);return Ui(o,n?Zi(n):a,t,e)}const s=Gi(i).mirror();return Ui(Ji(i,Qi(s)),n?Zi(n):s.boundingBox,t,e)}class Yt extends Ft{constructor(e,{cutLines:n=[],foldLines:s=[],foldLinesForwards:r=[],foldLinesBackwards:o=[]}={}){super();Ct(this,"body");Ct(this,"cutLines");Ct(this,"foldLinesBackwards");Ct(this,"foldLinesForwards");this.body=e,this.cutLines=[...n],this.foldLinesBackwards=[...s,...o],this.foldLinesForwards=[...r]}get foldLines(){return[...this.foldLinesForwards,...this.foldLinesBackwards]}clone(){return new Yt(this.body,{cutLines:[...this.cutLines],foldLinesForwards:[...this.foldLinesForwards],foldLinesBackwards:[...this.foldLinesBackwards]})}addCutLine(e){return this.cutLines.push(e),this}addFoldLine(e,n="backwards"){n==="forwards"?this.foldLinesForwards.push(e):this.foldLinesBackwards.push(e)}fuseFold(e,n="backwards"){const s=e instanceof Yt?e.body:e,r=this.body.overlappingStrands(s);return n==="forwards"?this.foldLinesForwards.push(...r):this.foldLinesBackwards.push(...r),this.fuseBody(e),this}fuseBody(e){let n;return e instanceof Yt?(n=e.body,this.cutLines.push(...e.cutLines),this.foldLinesForwards.push(...e.foldLinesForwards),this.foldLinesBackwards.push(...e.foldLinesBackwards)):n=e,this.body=this.body.fuse(n),this}cutShape(e){return this.body=Vo(this.body,e),this.cutLines=this.cutLines.flatMap(n=>gn(n,e,!0)),this.eraseFolds(e),this}eraseFolds(e){this.foldLinesBackwards=this.foldLinesBackwards.flatMap(n=>gn(n,e,!0)),this.foldLinesForwards=this.foldLinesForwards.flatMap(n=>gn(n,e,!0))}transform(e){return new Yt(this.body.transform(e),{cutLines:this.cutLines.map(s=>s.transform(e)),foldLinesForwards:this.foldLinesForwards.map(s=>s.transform(e)),foldLinesBackwards:this.foldLinesBackwards.map(s=>s.transform(e))})}asSVG(){const e=[];return this.body&&e.push({shape:this.body,color:"red"}),this.cutLines.length&&this.cutLines.map(n=>e.push({shape:n,color:"red"})),this.foldLinesBackwards.length&&this.foldLinesBackwards.map(n=>e.push({shape:n,color:"green"})),this.foldLinesForwards.length&&this.foldLinesForwards.map(n=>e.push({shape:n,color:"blue"})),jo(e,{unit:"mm"})}}function pn(i,t,{fillet:e=1,contraction:n=1,contractionMode:s="rounded",contractionLeft:r,contractionRight:o,leftContractionMode:a,rightContractionMode:h}={}){const l=r??n,c=o??n,u=zi([-i/2,0]).hLine(i);return c?(h??s)==="rounded"?(u.tangentArc(-c,c,[-1,0]),u.vLine(t-c)):u.line(-c,t):u.vLine(t),u.customCorner(e),u.hLine(-i+l+c),u.customCorner(e),l?(a??s)==="rounded"?(u.vLine(-t+l),u.tangentArcTo([-i/2,0])):u.lineTo([-i/2,0]):u.vLine(-t),u.close()}function Ki(i,t,e={}){return pn(i,t,e)}function ts(i,t,e={}){return pn(i,t,e).mirror("x")}function wn(i,t,{contractionTop:e,contractionBottom:n,topContractionMode:s,bottomContractionMode:r,...o}={}){return pn(i,t,{contractionLeft:e,contractionRight:n,leftContractionMode:s,rightContractionMode:r,...o}).rotate(90)}function es(i,t,e={}){return wn(i,t,e).mirror("y")}const Oo={right:es,left:wn,top:Ki,bottom:ts},ns=(i,t)=>zi([-i/2,0]).line(t,-t).hLine(i-2*t).line(t,t);class $e extends Ft{constructor(e,n=.2){super();Ct(this,"cut");Ct(this,"unfold");Ct(this,"bump");if(typeof e!="number"){this.cut=e.cut,this.unfold=e.unfold,this.bump=e.bump;return}const s=ns(e,n*2.5);this.cut=s.asStrand(),this.unfold=s.close(),this.bump=ns(e-5*n,5*n).close().mirror("x")}clone(){return new $e(this)}transform(e){return new $e({cut:this.cut.transform(e),unfold:this.unfold.transform(e),bump:this.bump.transform(e)})}makeCut(e){e.eraseFolds(this.unfold),e.addCutLine(this.cut)}fuseBump(e){e.fuseBody(this.bump)}}class Fe extends Ft{constructor(t){super(),this.points=t,this.points=t}transform(t){return new Fe(this.points.map(t.transform))}distribute(t){return this.points.map(e=>t.translateTo(e))}cloneDistribute(t){return this.points.map(e=>t.clone().translateTo(e))}}function qo(i,t,e=3,n=0){if(i<=t*2)throw new Error("totalWidth must be greater than twice the objectWidth");const s=Math.max(Math.floor((i-2*n)/(t*(e+1))),2),o=(i-2*n-s*t)/(s-1),a=[];for(let h=0;h<s;h++)a.push(n+h*(t+o)-i/2+t/2);return new Fe(a.map(h=>[h,0]))}function zo(i,t,e="x"){const n=i*(t-1),s=[];for(let r=0;r<t;r++)s.push(r*i-n/2);return new Fe(s.map(r=>e.toLowerCase()==="x"?[r,0]:[0,r]))}it.Dieline=Yt,it.FoldLockBump=$e,it.bottomFlap=ts,it.drawFlaps=Oo,it.leftFlap=wn,it.linearDistribution=qo,it.linearSpread=zo,it.rightFlap=es,it.topFlap=Ki,Object.defineProperty(it,Symbol.toStringTag,{value:"Module"})});
|
|
18
18
|
//# sourceMappingURL=dielines.umd.cjs.map
|