pantograph2d 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{QuadraticBezier-DxieHk9z.js → QuadraticBezier-B2g_Iyyl.js} +15 -14
- package/dist/{QuadraticBezier-DxieHk9z.js.map → QuadraticBezier-B2g_Iyyl.js.map} +1 -1
- package/dist/{QuadraticBezier--UZr_xcV.cjs → QuadraticBezier-CuRsIP_D.cjs} +4 -4
- package/dist/{QuadraticBezier--UZr_xcV.cjs.map → QuadraticBezier-CuRsIP_D.cjs.map} +1 -1
- package/dist/{draw-BUUNobTS.cjs → draw-9Elv4xz6.cjs} +2 -2
- package/dist/{draw-BUUNobTS.cjs.map → draw-9Elv4xz6.cjs.map} +1 -1
- package/dist/{draw-lOWnIN1o.js → draw-BJW5kfm9.js} +3 -3
- package/dist/{draw-lOWnIN1o.js.map → draw-BJW5kfm9.js.map} +1 -1
- package/dist/{models-DCkOI0hV.js → models-DdZq-waE.js} +2 -2
- package/dist/{models-DCkOI0hV.js.map → models-DdZq-waE.js.map} +1 -1
- package/dist/{models-CCo90hZc.cjs → models-LHGiMarC.cjs} +2 -2
- package/dist/{models-CCo90hZc.cjs.map → models-LHGiMarC.cjs.map} +1 -1
- package/dist/pantograph/drawShape.cjs +1 -1
- package/dist/pantograph/drawShape.cjs.map +1 -1
- package/dist/pantograph/drawShape.js +374 -14
- package/dist/pantograph/drawShape.js.map +1 -1
- package/dist/pantograph/models.cjs +1 -1
- package/dist/pantograph/models.js +2 -2
- package/dist/pantograph/svg.cjs +1 -1
- package/dist/pantograph/svg.js +1 -1
- package/dist/pantograph.cjs +2 -2
- package/dist/pantograph.cjs.map +1 -1
- package/dist/pantograph.js +674 -568
- package/dist/pantograph.js.map +1 -1
- package/dist/{svg-BY5h3CDD.cjs → svg-BzloQ9l1.cjs} +2 -2
- package/dist/{svg-BY5h3CDD.cjs.map → svg-BzloQ9l1.cjs.map} +1 -1
- package/dist/{svg-BIphc_zE.js → svg-D8vwkQf7.js} +2 -2
- package/dist/{svg-BIphc_zE.js.map → svg-D8vwkQf7.js.map} +1 -1
- package/dist/types/src/algorithms/tesselate/tesselateSegment.d.ts +7 -0
- package/dist/types/src/api/drawShape.d.ts +3 -0
- package/dist/types/src/drawShape/index.d.ts +1 -1
- package/dist/types/src/main.d.ts +1 -1
- package/dist/types/src/operations.d.ts +1 -0
- package/dist/types/src/tesselationOperations.d.ts +15 -0
- package/package.json +3 -3
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";var Ge=Object.defineProperty;var be=r=>{throw TypeError(r)};var Qe=(r,s,t)=>s in r?Ge(r,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[s]=t;var q=(r,s,t)=>Qe(r,typeof s!="symbol"?s+"":s,t),he=(r,s,t)=>s.has(r)||be("Cannot "+t);var ht=(r,s,t)=>(he(r,s,"read from private field"),t?t.call(r):s.get(r)),ae=(r,s,t)=>s.has(r)?be("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(r):s.set(r,t),Bt=(r,s,t,e)=>(he(r,s,"write to private field"),e?e.call(r,t):s.set(r,t),t),ue=(r,s,t)=>(he(r,s,"access private method"),t);function Ot(r,s,t,e){return r<=e&&s>=t}class ft{constructor(s=1/0,t=1/0,e=-1/0,n=-1/0){q(this,"xMin");q(this,"yMin");q(this,"xMax");q(this,"yMax");this.xMin=s,this.yMin=t,this.xMax=e,this.yMax=n}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(s){return new ft(this.xMin-s,this.yMin-s,this.xMax+s,this.yMax+s)}contains(s){const[t,e]=s;return Ot(this.xMin,this.xMax,t,t)&&Ot(this.yMin,this.yMax,e,e)}overlaps(s){return Ot(this.xMin,this.xMax,s.xMin,s.xMax)&&Ot(this.yMin,this.yMax,s.yMin,s.yMax)}addPoint(s){const[t,e]=s;return new ft(Math.min(this.xMin,t),Math.min(this.yMin,e),Math.max(this.xMax,t),Math.max(this.yMax,e))}merge(s){return new ft(Math.min(this.xMin,s.xMin),Math.min(this.yMin,s.yMin),Math.max(this.xMax,s.xMax),Math.max(this.yMax,s.yMax))}intersection(s){return new ft(Math.max(this.xMin,s.xMin),Math.max(this.yMin,s.yMin),Math.min(this.xMax,s.xMax),Math.min(this.yMax,s.yMax))}}function ge(r){return r.reduce((s,t)=>s.addPoint(t),new ft)}const Ee=(r,s=1e-9)=>{let t=r;return Math.abs(r)<s&&(t=0),t.toFixed(-Math.log10(s))};function we(r,s=1e-9){return Array.from(new Map(r.map(([t,e])=>[`[${Ee(t,s)},${Ee(e,s)}]`,[t,e]])).values())}const Ht=Math.PI/180,Ce=180/Math.PI,Z=r=>`[${r[0]}, ${r[1]}]`,O=([r,s],[t,e],n=1e-9)=>Math.abs(r-t)<=n&&Math.abs(s-e)<=n,it=([r,s],[t,e])=>[r+t,s+e],G=([r,s],[t,e])=>[r-t,s-e],Tt=([r,s])=>r*r+s*s,Xe=([r,s])=>Math.sqrt(Tt([r,s])),nt=([r,s],t)=>[r*t,s*t],Et=([r,s],[t,e]=[0,0])=>(r-t)**2+(s-e)**2,J=(r,s=[0,0])=>Math.sqrt(Et(r,s));function yt([r,s],[t,e]){return r*e-s*t}function ce([r,s],[t,e]){return r*t+s*e}function Ze([r,s],[t,e]=[0,0]){return Math.atan2(e*r-s*t,r*t+s*e)}function K([r,s]){const t=J([r,s]);return[r/t,s/t]}function vt(r,s){const t=Math.cos(s)*r,e=Math.sin(s)*r;return[t,e]}function qe([r,s]){return Math.atan2(s,r)}function Fe(r){const s=J(r),t=qe(r);return[s,t]}function pe(r,s,t=1e-9){const e=yt(r,s),n=Tt(r),i=Tt(s);return e*e<n*i*t*t}function H(r){return[-r[1],r[0]]}function Ct(r){return[r[1],-r[0]]}function Je(r,s,t){return O(t,r)||pe(G(t,r),s)}const _t=(r,s)=>{const[t,e,n,i,o,l,h,a,u]=r,[c,g,d,w,p,P,f,M,E]=s;return[t*c+e*w+n*f,t*g+e*p+n*M,t*d+e*P+n*E,i*c+o*w+l*f,i*g+o*p+l*M,i*d+o*P+l*E,h*c+a*w+u*f,h*g+a*p+u*M,h*d+a*P+u*E]},Ke=r=>{const[s,t,e,n,i,o,l,h,a]=r,u=s*(i*a-o*h)-t*(n*a-o*l)+e*(n*h-i*l);return[(i*a-o*h)/u,(e*h-t*a)/u,(t*o-e*i)/u,(o*l-n*a)/u,(s*a-e*l)/u,(e*n-s*o)/u,(n*h-i*l)/u,(t*l-s*h)/u,(s*i-t*n)/u]},He=r=>{const[s,t,e,n,i,o,l,h,a]=r;return[s,n,l,t,i,h,e,o,a]};class et{constructor(s){q(this,"_matrix",[1,0,0,0,1,0,0,0,1]);s&&(this._matrix=[...s])}clone(){return new et(this._matrix)}transpose(){return this._matrix=He(this._matrix),this}inverse(){return this._matrix=Ke(this._matrix),this}translate(s,t){return this._matrix=_t([1,0,s,0,1,t,0,0,1],this._matrix),this}rotate(s,t){const e=Math.cos(s),n=Math.sin(s),i=[e,-n,0,n,e,0,0,0,1];return t&&this.translate(-t[0],-t[1]),this._matrix=_t(i,this._matrix),t&&this.translate(t[0],t[1]),this}mirrorX(){return this._matrix=_t([1,0,0,0,-1,0,0,0,1],this._matrix),this}mirrorY(){return this._matrix=_t([-1,0,0,0,1,0,0,0,1],this._matrix),this}mirrorLine(s,t){const[e,n]=s,i=Math.atan2(n,e);return t&&this.translate(-t[0],-t[1]),this.rotate(-i),this.mirrorX(),this.rotate(i),t&&this.translate(t[0],t[1]),this}mirrorCenter(s){return s&&this.translate(-s[0],-s[1]),this._matrix=_t([-1,0,0,0,-1,0,0,0,1],this._matrix),s&&this.translate(s[0],s[1]),this}scale(s,t){return t&&this.translate(-t[0],-t[1]),this._matrix=_t([s,0,0,0,s,0,0,0,1],this._matrix),t&&this.translate(t[0],t[1]),this}transform(s){const[t,e]=s,[n,i,o,l,h,a]=this._matrix;return[n*t+i*e+o,l*t+h*e+a]}transformAngle(s){const[t,e]=this.transform([Math.cos(s),Math.sin(s)]),[n,i]=this.transform([0,0]);return Math.atan2(e-i,t-n)}keepsOrientation(){const[s,,,,t]=this._matrix;return s*t>0}scaleFactor(){const[s,,,t]=this._matrix;return Math.sqrt(s*s+t*t)}}class Ae{translateX(s){const t=new et().translate(s,0);return this.transform(t)}translateY(s){const t=new et().translate(0,s);return this.transform(t)}translate(s,t){const e=new et().translate(s,t);return this.transform(e)}translateTo([s,t]){const e=new et().translate(s,t);return this.transform(e)}rotate(s,t){const e=new et().rotate(s*Ht,t);return this.transform(e)}scale(s,t){const e=new et().scale(s,t);return this.transform(e)}mirrorCenter(s){const t=new et().mirrorCenter(s);return this.transform(t)}mirror(s="x",t){const e=new et;return s==="x"?e.mirrorX():s==="y"?e.mirrorY():e.mirrorLine(s,t),this.transform(e)}}class xt extends Ae{constructor(t,e){super();q(this,"precision",1e-9);this.firstPoint=t,this.lastPoint=e,this.firstPoint=t,this.lastPoint=e}get repr(){return`${this.segmentType} ${Z(this.firstPoint)} - ${Z(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}class at extends xt{constructor(){super(...arguments);q(this,"segmentType","LINE");q(this,"_V",null);q(this,"_slope",null);q(this,"_yIntercept",null);q(this,"_boundingBox",null)}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return it(this.firstPoint,nt(this.V,t))}get length(){return J(this.firstPoint,this.lastPoint)}get squareLength(){return Et(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=G(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 it(this.firstPoint,nt(this.V,.5))}isSame(t){return t instanceof at?O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)||O(this.lastPoint,t.firstPoint)&&O(this.firstPoint,t.lastPoint):!1}clone(){return new at(this.firstPoint,this.lastPoint)}reverse(){return new at(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new ft(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=G(t,this.firstPoint),n=ce(e,this.V)/this.squareLength;if(n<0)return J(t,this.firstPoint);if(n>1)return J(t,this.lastPoint);const i=this.paramPoint(n);return J(t,i)}isOnSegment(t){if(O(t,this.firstPoint,this.precision))return!0;const e=G(t,this.firstPoint);if(!pe(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 K(this.V)}get normalVector(){return H(K(this.V))}get tangentAtFirstPoint(){return K(this.V)}get tangentAtLastPoint(){return K(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(a=>{if(!this.isOnSegment(a))throw new Error(`Point ${Z(a)} is not on segment ${this.repr}`)});const n=[this.firstPoint,...e,this.lastPoint],i=we(n),o=this.lastPoint[0]-this.firstPoint[0];let l=Math.sign(o),h=0;return Math.abs(o)<this.precision&&(l=Math.sign(this.lastPoint[1]-this.firstPoint[1]),h=1),i.sort((a,u)=>l*(a[h]-u[h])),i.flatMap((a,u)=>u===i.length-1?[]:new at(a,i[u+1]))}transform(t){return new at(t.transform(this.firstPoint),t.transform(this.lastPoint))}}function ts(r){return Array.from(Array(r).keys())}function Vt(r){const s=Math.min(...r.map(t=>t.length));return ts(s).map(t=>r.map(e=>e[t]))}function dt(r,s=1e-9){return r<0?r+2*Math.PI:r>=2*Math.PI?r%(2*Math.PI):r>2*Math.PI-s?0:r}function Jt(r,s,t,e=1e-9){let n=s-r;return t&&(n=-n),n<0&&(n+=2*Math.PI),n>2*Math.PI-e?0:n}const te=(r,s,t)=>{const e=yt(r.V,s.V),n=Tt(r.V),i=Tt(s.V),o=t?t*t:r.precision*s.precision;if(e*e<n*i*o)return"parallel";const l=G(s.firstPoint,r.firstPoint),h=yt(l,s.V)/e,a=yt(l,r.V)/e;return{intersectionParam1:h,intersectionParam2:a}};function es(r,s,t=!1,e){const n=te(r,s,e);if(n==="parallel"){if(!t)return null;if(r.isSame(s))return r;const l=we([s.isOnSegment(r.firstPoint)?r.firstPoint:null,s.isOnSegment(r.lastPoint)?r.lastPoint:null,r.isOnSegment(s.firstPoint)?s.firstPoint:null,r.isOnSegment(s.lastPoint)?s.lastPoint:null].filter(h=>h!==null)).sort((h,a)=>h[0]-a[0]);if(l.length===0)return null;if(l.length===1)return null;if(l.length===2)return new at(l[0],l[1]);throw console.error(l),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:i,intersectionParam2:o}=n;return!r.isValidParameter(i)||!s.isValidParameter(o)?null:r.paramPoint(i)}const Ut=(r,s)=>{const t=G(r,s);return Fe(t)};class ut extends xt{constructor(t,e,n,i=!1,{ignoreChecks:o=!1}={}){super(t,e);q(this,"segmentType","ARC");q(this,"center");q(this,"clockwise");q(this,"_coefficients",null);q(this,"_angularLength",null);q(this,"_radius",null);q(this,"_firstAngle",null);q(this,"_lastAngle",null);q(this,"_boundingBox",null);if(this.center=n,this.clockwise=i,!o){if(O(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 ${Z(t)} and ${Z(e)}} (center ${Z(n)})`)}}get info(){return`ARC(${Z(this.firstPoint)}, ${Z(this.lastPoint)}, ${Z(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 Jt(this.firstAngle,dt(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=Jt(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return it(this.center,vt(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,n]=Ut(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error(`Point ${Z(t)} is not on segment ${this.repr}`);const i=this.angleToParam(n);if(!this.isValidParameter(i))throw new Error(`Point ${Z(t)} is not on segment ${this.repr}`);return i}get radius(){return this._radius===null&&(this._radius=J(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=G(this.firstPoint,this.center);this._firstAngle=dt(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=G(this.lastPoint,this.center);this._lastAngle=dt(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 ut)||!O(this.center,t.center)?!1:O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||O(this.lastPoint,t.firstPoint)&&O(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new ut(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new ut(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 ft(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]=Ut(t,this.center);return this.isValidParameter(this.angleToParam(n))?Math.abs(e-this.radius):Math.sqrt(Math.min(Et(t,this.firstPoint),Et(t,this.lastPoint)))}isOnSegment(t){if(O(t,this.firstPoint)||O(t,this.lastPoint))return!0;const[e,n]=Ut(t,this.center);if(Math.abs(e-this.radius)>this.precision)return!1;const i=this.angleToParam(n);return this.isValidParameter(i)}gradientAt(t){const e=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),n=this.radius*this.angularLength,i=-n*Math.sin(e),o=n*Math.cos(e);return this.clockwise?[-i,-o]:[i,o]}tangentAt(t){const[e,n]=Ut(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error("Point is not on the arc");const i=this.angleToParam(n);if(!this.isValidParameter(i))throw new Error("Point is not on the arc");const o=vt(1,n);return(this.clockwise?Ct:H)(K(o))}get tangentAtFirstPoint(){const t=vt(1,this.firstAngle);return(this.clockwise?Ct:H)(K(t))}get tangentAtLastPoint(){const t=vt(1,this.lastAngle);return(this.clockwise?Ct:H)(K(t))}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const i=[0,1,...e.map(h=>this.pointToParam(h))],o=new Map(Vt([i,[this.firstPoint,this.lastPoint,...e]]));i.sort((h,a)=>h-a);let l=null;return i.flatMap((h,a)=>{if(a===i.length-1)return[];const u=i[a+1];if(u-h<this.precision)return l===null&&(l=h),[];const c=l===null?h:l,g=new ut(o.get(c)||this.paramPoint(c),o.get(u)||this.paramPoint(u),this.center,this.clockwise);return l=null,g})}transform(t){return new ut(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}}function ss(r,s,t){const e=new at(s,r),n=new at(s,t),i=H(e.tangentAtFirstPoint),o=H(n.tangentAtLastPoint),l=te({firstPoint:e.midPoint,V:i,precision:1e-9},{firstPoint:n.midPoint,V:o,precision:1e-9});if(l==="parallel")throw new Error("Cannot create an arc from three colinear points");const h=yt(G(r,s),G(t,s))>0;return new ut(r,t,it(e.midPoint,nt(i,l.intersectionParam1)),h,{ignoreChecks:!0})}function ns(r,s,t){const e=new at(r,s),n=H(e.tangentAtFirstPoint),i=te({firstPoint:e.midPoint,V:n,precision:1e-9},{firstPoint:r,V:H(t),precision:1e-9});if(i==="parallel")throw new Error("Cannot create an arc from three colinear points");const o=it(e.midPoint,nt(n,i.intersectionParam1)),l=yt(G(o,r),G(o,it(r,t)))<0;return new ut(r,s,o,l,{ignoreChecks:!0})}const _e=1e-21,Yt=1.618034;function je(r,s=0,t=1,e=110,n=1e3){let i,o,l,h,a,u,c,g,d,w,p,P,f;for(o=r(s),l=r(t),o<l&&([s,t]=[t,s],[o,l]=[l,o]),f=t+Yt*(t-s),h=r(f),a=3,c=0;h<l;){if(g=(t-s)*(l-h),d=(t-f)*(l-o),w=d-g,Math.abs(w)<_e?i=2*_e:i=2*w,p=t-((t-f)*d-(t-s)*g)/i,P=t+e*(f-t),c>n)throw new Error("Too many iterations.");if(c+=1,(p-f)*(t-p)>0){if(u=r(p),a+=1,u<h)return s=t,t=p,o=l,l=u,[s,t,f,o,l,h,a];if(u>l)return f=p,h=u,[s,t,f,o,l,h,a];p=f+Yt*(f-t),u=r(p),a+=1}else(p-P)*(P-f)>=0?(p=P,u=r(p),a+=1):(p-P)*(f-p)>0?(u=r(p),a+=1,u<h&&(t=f,f=p,p=f+Yt*(f-t),l=h,h=u,u=r(p),a+=1)):(p=f+Yt*(f-t),u=r(p),a+=1);s=t,t=f,f=p,o=l,l=h,h=u}return[s,t,f,o,l,h,a]}const Se=.381966,is=1e-11;class rs{constructor(s,t=148e-10,e=500){q(this,"xmin");q(this,"fval");q(this,"iter");q(this,"funcalls");q(this,"brack");this.func=s,this.tol=t,this.maxiter=e,this.func=s,this.tol=t,this.maxiter=e,this.xmin=1/0,this.fval=1/0,this.iter=0,this.funcalls=0,this.brack=null}setBracket(s=null){this.brack=s}getBracketInfo(){let s,t,e,n,i,o,l;const h=this.func,a=this.brack;if(a===null)[i,o,l,s,t,e,n]=je(h);else if(a.length===2)[i,o,l,s,t,e,n]=je(h,a[0],a[1]);else if(a.length===3){if([i,o,l]=a,i>l&&([l,i]=[i,l]),!(i<o&&o<l))throw new Error("Not a bracketing interval.");if(s=h(i),t=h(o),e=h(l),!(t<s&&t<e))throw new Error("Not a bracketing interval.");n=3}else throw new Error("Bracketing interval must be length 2 or 3 sequence.");return[i,o,l,s,t,e,n]}optimize(){const s=this.func;let[t,e,n,,,,i]=this.getBracketInfo(),o=e,l=e,h=e,a=s(o),u=a,c=a,g,d;t<n?(g=t,d=n):(g=n,d=t);let w=0;i=1;let p=0,P=0,f;for(;p<this.maxiter;){const M=this.tol*Math.abs(o)+is,E=2*M,v=.5*(g+d);if(Math.abs(o-v)<E-.5*(d-g))break;if(Math.abs(w)<=M)o>=v?w=g-o:w=d-o,P=Se*w;else{const D=(o-l)*(c-u);let S=(o-h)*(c-a),k=(o-h)*S-(o-l)*D;S=2*(S-D),S>0&&(k=-k),S=Math.abs(S);const L=w;w=P,k>S*(g-o)&&k<S*(d-o)&&Math.abs(k)<Math.abs(.5*S*L)?(P=k*1/S,f=o+P,(f-g<E||d-f<E)&&(v-o>=0?P=M:P=-M)):(o>=v?w=g-o:w=d-o,P=Se*w)}Math.abs(P)<M?P>=0?f=o+M:f=o-M:f=o+P;const R=s(f);i+=1,R>c?(f<o?g=f:d=f,R<=a||l===o?(h=l,l=f,u=a,a=R):(R<=u||h===o||h===l)&&(h=f,u=R)):(f>=o?g=o:d=o,h=l,l=o,o=f,u=a,a=c,c=R),p+=1}this.xmin=o,this.fval=c,this.iter=p,this.funcalls=i}results(){return{argMin:this.xmin,fMin:this.fval,iterations:this.iter,funcCalls:this.funcalls}}run(){return this.optimize(),this.results()}}function xe(r,s=1e-8,t=1e3){const e=new rs(r,s,t);return e.setBracket([0,1]),e.run()}function Ve(r){if(r.__esModule)return r;var s=r.default;if(typeof s=="function"){var t=function e(){return this instanceof e?Reflect.construct(s,arguments,this.constructor):s.apply(this,arguments)};t.prototype=s.prototype}else t={};return Object.defineProperty(t,"__esModule",{value:!0}),Object.keys(r).forEach(function(e){var n=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(t,e,n.get?n:{enumerable:!0,get:function(){return r[e]}})}),t}var $={};const os=Object.prototype.toString;function qt(r){const s=os.call(r);return s.endsWith("Array]")&&!s.includes("Big")}const ls=Object.freeze(Object.defineProperty({__proto__:null,isAnyArray:qt},Symbol.toStringTag,{value:"Module"})),hs=Ve(ls);function as(r){var s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!qt(r))throw new TypeError("input must be an array");if(r.length===0)throw new TypeError("input must not be empty");var t=s.fromIndex,e=t===void 0?0:t,n=s.toIndex,i=n===void 0?r.length:n;if(e<0||e>=r.length||!Number.isInteger(e))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=e||i>r.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=r[e],l=e+1;l<i;l++)r[l]>o&&(o=r[l]);return o}function us(r){var s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!qt(r))throw new TypeError("input must be an array");if(r.length===0)throw new TypeError("input must not be empty");var t=s.fromIndex,e=t===void 0?0:t,n=s.toIndex,i=n===void 0?r.length:n;if(e<0||e>=r.length||!Number.isInteger(e))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=e||i>r.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=r[e],l=e+1;l<i;l++)r[l]<o&&(o=r[l]);return o}function cs(r){var s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(qt(r)){if(r.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var t;if(s.output!==void 0){if(!qt(s.output))throw new TypeError("output option must be an array if specified");t=s.output}else t=new Array(r.length);var e=us(r),n=as(r);if(e===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=s.min,o=i===void 0?s.autoMinMax?e:0:i,l=s.max,h=l===void 0?s.autoMinMax?n:1:l;if(o>=h)throw new RangeError("min option must be smaller than max option");for(var a=(h-o)/(n-e),u=0;u<r.length;u++)t[u]=(r[u]-e)*a+o;return t}const fs=Object.freeze(Object.defineProperty({__proto__:null,default:cs},Symbol.toStringTag,{value:"Module"})),ms=Ve(fs);Object.defineProperty($,"__esModule",{value:!0});var st=hs,ke=ms;const Wt=" ".repeat(2),Ne=" ".repeat(4);function gs(){return $e(this)}function $e(r,s={}){const{maxRows:t=15,maxColumns:e=10,maxNumSize:n=8,padMinus:i="auto"}=s;return`${r.constructor.name} {
|
|
1
|
+
"use strict";var Qe=Object.defineProperty;var be=r=>{throw TypeError(r)};var Xe=(r,s,t)=>s in r?Qe(r,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[s]=t;var q=(r,s,t)=>Xe(r,typeof s!="symbol"?s+"":s,t),he=(r,s,t)=>s.has(r)||be("Cannot "+t);var ht=(r,s,t)=>(he(r,s,"read from private field"),t?t.call(r):s.get(r)),ae=(r,s,t)=>s.has(r)?be("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(r):s.set(r,t),Bt=(r,s,t,e)=>(he(r,s,"write to private field"),e?e.call(r,t):s.set(r,t),t),ue=(r,s,t)=>(he(r,s,"access private method"),t);function Ot(r,s,t,e){return r<=e&&s>=t}class ft{constructor(s=1/0,t=1/0,e=-1/0,n=-1/0){q(this,"xMin");q(this,"yMin");q(this,"xMax");q(this,"yMax");this.xMin=s,this.yMin=t,this.xMax=e,this.yMax=n}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(s){return new ft(this.xMin-s,this.yMin-s,this.xMax+s,this.yMax+s)}contains(s){const[t,e]=s;return Ot(this.xMin,this.xMax,t,t)&&Ot(this.yMin,this.yMax,e,e)}overlaps(s){return Ot(this.xMin,this.xMax,s.xMin,s.xMax)&&Ot(this.yMin,this.yMax,s.yMin,s.yMax)}addPoint(s){const[t,e]=s;return new ft(Math.min(this.xMin,t),Math.min(this.yMin,e),Math.max(this.xMax,t),Math.max(this.yMax,e))}merge(s){return new ft(Math.min(this.xMin,s.xMin),Math.min(this.yMin,s.yMin),Math.max(this.xMax,s.xMax),Math.max(this.yMax,s.yMax))}intersection(s){return new ft(Math.max(this.xMin,s.xMin),Math.max(this.yMin,s.yMin),Math.min(this.xMax,s.xMax),Math.min(this.yMax,s.yMax))}}function ge(r){return r.reduce((s,t)=>s.addPoint(t),new ft)}const Ee=(r,s=1e-9)=>{let t=r;return Math.abs(r)<s&&(t=0),t.toFixed(-Math.log10(s))};function we(r,s=1e-9){return Array.from(new Map(r.map(([t,e])=>[`[${Ee(t,s)},${Ee(e,s)}]`,[t,e]])).values())}const Ht=Math.PI/180,Ce=180/Math.PI,Z=r=>`[${r[0]}, ${r[1]}]`,O=([r,s],[t,e],n=1e-9)=>Math.abs(r-t)<=n&&Math.abs(s-e)<=n,it=([r,s],[t,e])=>[r+t,s+e],G=([r,s],[t,e])=>[r-t,s-e],Tt=([r,s])=>r*r+s*s,Ze=([r,s])=>Math.sqrt(Tt([r,s])),nt=([r,s],t)=>[r*t,s*t],Et=([r,s],[t,e]=[0,0])=>(r-t)**2+(s-e)**2,J=(r,s=[0,0])=>Math.sqrt(Et(r,s));function yt([r,s],[t,e]){return r*e-s*t}function ce([r,s],[t,e]){return r*t+s*e}function Je([r,s],[t,e]=[0,0]){return Math.atan2(e*r-s*t,r*t+s*e)}function K([r,s]){const t=J([r,s]);return[r/t,s/t]}function vt(r,s){const t=Math.cos(s)*r,e=Math.sin(s)*r;return[t,e]}function qe([r,s]){return Math.atan2(s,r)}function Fe(r){const s=J(r),t=qe(r);return[s,t]}function pe(r,s,t=1e-9){const e=yt(r,s),n=Tt(r),i=Tt(s);return e*e<n*i*t*t}function H(r){return[-r[1],r[0]]}function Ct(r){return[r[1],-r[0]]}function Ke(r,s,t){return O(t,r)||pe(G(t,r),s)}const _t=(r,s)=>{const[t,e,n,i,o,l,h,a,u]=r,[c,g,d,w,p,P,f,M,E]=s;return[t*c+e*w+n*f,t*g+e*p+n*M,t*d+e*P+n*E,i*c+o*w+l*f,i*g+o*p+l*M,i*d+o*P+l*E,h*c+a*w+u*f,h*g+a*p+u*M,h*d+a*P+u*E]},He=r=>{const[s,t,e,n,i,o,l,h,a]=r,u=s*(i*a-o*h)-t*(n*a-o*l)+e*(n*h-i*l);return[(i*a-o*h)/u,(e*h-t*a)/u,(t*o-e*i)/u,(o*l-n*a)/u,(s*a-e*l)/u,(e*n-s*o)/u,(n*h-i*l)/u,(t*l-s*h)/u,(s*i-t*n)/u]},ts=r=>{const[s,t,e,n,i,o,l,h,a]=r;return[s,n,l,t,i,h,e,o,a]};class et{constructor(s){q(this,"_matrix",[1,0,0,0,1,0,0,0,1]);s&&(this._matrix=[...s])}clone(){return new et(this._matrix)}transpose(){return this._matrix=ts(this._matrix),this}inverse(){return this._matrix=He(this._matrix),this}translate(s,t){return this._matrix=_t([1,0,s,0,1,t,0,0,1],this._matrix),this}rotate(s,t){const e=Math.cos(s),n=Math.sin(s),i=[e,-n,0,n,e,0,0,0,1];return t&&this.translate(-t[0],-t[1]),this._matrix=_t(i,this._matrix),t&&this.translate(t[0],t[1]),this}mirrorX(){return this._matrix=_t([1,0,0,0,-1,0,0,0,1],this._matrix),this}mirrorY(){return this._matrix=_t([-1,0,0,0,1,0,0,0,1],this._matrix),this}mirrorLine(s,t){const[e,n]=s,i=Math.atan2(n,e);return t&&this.translate(-t[0],-t[1]),this.rotate(-i),this.mirrorX(),this.rotate(i),t&&this.translate(t[0],t[1]),this}mirrorCenter(s){return s&&this.translate(-s[0],-s[1]),this._matrix=_t([-1,0,0,0,-1,0,0,0,1],this._matrix),s&&this.translate(s[0],s[1]),this}scale(s,t){return t&&this.translate(-t[0],-t[1]),this._matrix=_t([s,0,0,0,s,0,0,0,1],this._matrix),t&&this.translate(t[0],t[1]),this}transform(s){const[t,e]=s,[n,i,o,l,h,a]=this._matrix;return[n*t+i*e+o,l*t+h*e+a]}transformAngle(s){const[t,e]=this.transform([Math.cos(s),Math.sin(s)]),[n,i]=this.transform([0,0]);return Math.atan2(e-i,t-n)}keepsOrientation(){const[s,,,,t]=this._matrix;return s*t>0}scaleFactor(){const[s,,,t]=this._matrix;return Math.sqrt(s*s+t*t)}}class Ae{translateX(s){const t=new et().translate(s,0);return this.transform(t)}translateY(s){const t=new et().translate(0,s);return this.transform(t)}translate(s,t){const e=new et().translate(s,t);return this.transform(e)}translateTo([s,t]){const e=new et().translate(s,t);return this.transform(e)}rotate(s,t){const e=new et().rotate(s*Ht,t);return this.transform(e)}scale(s,t){const e=new et().scale(s,t);return this.transform(e)}mirrorCenter(s){const t=new et().mirrorCenter(s);return this.transform(t)}mirror(s="x",t){const e=new et;return s==="x"?e.mirrorX():s==="y"?e.mirrorY():e.mirrorLine(s,t),this.transform(e)}}class xt extends Ae{constructor(t,e){super();q(this,"precision",1e-9);this.firstPoint=t,this.lastPoint=e,this.firstPoint=t,this.lastPoint=e}get repr(){return`${this.segmentType} ${Z(this.firstPoint)} - ${Z(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}class at extends xt{constructor(){super(...arguments);q(this,"segmentType","LINE");q(this,"_V",null);q(this,"_slope",null);q(this,"_yIntercept",null);q(this,"_boundingBox",null)}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return it(this.firstPoint,nt(this.V,t))}get length(){return J(this.firstPoint,this.lastPoint)}get squareLength(){return Et(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=G(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 it(this.firstPoint,nt(this.V,.5))}isSame(t){return t instanceof at?O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)||O(this.lastPoint,t.firstPoint)&&O(this.firstPoint,t.lastPoint):!1}clone(){return new at(this.firstPoint,this.lastPoint)}reverse(){return new at(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new ft(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=G(t,this.firstPoint),n=ce(e,this.V)/this.squareLength;if(n<0)return J(t,this.firstPoint);if(n>1)return J(t,this.lastPoint);const i=this.paramPoint(n);return J(t,i)}isOnSegment(t){if(O(t,this.firstPoint,this.precision))return!0;const e=G(t,this.firstPoint);if(!pe(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 K(this.V)}get normalVector(){return H(K(this.V))}get tangentAtFirstPoint(){return K(this.V)}get tangentAtLastPoint(){return K(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(a=>{if(!this.isOnSegment(a))throw new Error(`Point ${Z(a)} is not on segment ${this.repr}`)});const n=[this.firstPoint,...e,this.lastPoint],i=we(n),o=this.lastPoint[0]-this.firstPoint[0];let l=Math.sign(o),h=0;return Math.abs(o)<this.precision&&(l=Math.sign(this.lastPoint[1]-this.firstPoint[1]),h=1),i.sort((a,u)=>l*(a[h]-u[h])),i.flatMap((a,u)=>u===i.length-1?[]:new at(a,i[u+1]))}transform(t){return new at(t.transform(this.firstPoint),t.transform(this.lastPoint))}}function xe(r){return Array.from(Array(r).keys())}function Vt(r){const s=Math.min(...r.map(t=>t.length));return xe(s).map(t=>r.map(e=>e[t]))}function dt(r,s=1e-9){return r<0?r+2*Math.PI:r>=2*Math.PI?r%(2*Math.PI):r>2*Math.PI-s?0:r}function Jt(r,s,t,e=1e-9){let n=s-r;return t&&(n=-n),n<0&&(n+=2*Math.PI),n>2*Math.PI-e?0:n}const te=(r,s,t)=>{const e=yt(r.V,s.V),n=Tt(r.V),i=Tt(s.V),o=t?t*t:r.precision*s.precision;if(e*e<n*i*o)return"parallel";const l=G(s.firstPoint,r.firstPoint),h=yt(l,s.V)/e,a=yt(l,r.V)/e;return{intersectionParam1:h,intersectionParam2:a}};function es(r,s,t=!1,e){const n=te(r,s,e);if(n==="parallel"){if(!t)return null;if(r.isSame(s))return r;const l=we([s.isOnSegment(r.firstPoint)?r.firstPoint:null,s.isOnSegment(r.lastPoint)?r.lastPoint:null,r.isOnSegment(s.firstPoint)?s.firstPoint:null,r.isOnSegment(s.lastPoint)?s.lastPoint:null].filter(h=>h!==null)).sort((h,a)=>h[0]-a[0]);if(l.length===0)return null;if(l.length===1)return null;if(l.length===2)return new at(l[0],l[1]);throw console.error(l),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:i,intersectionParam2:o}=n;return!r.isValidParameter(i)||!s.isValidParameter(o)?null:r.paramPoint(i)}const Ut=(r,s)=>{const t=G(r,s);return Fe(t)};class ut extends xt{constructor(t,e,n,i=!1,{ignoreChecks:o=!1}={}){super(t,e);q(this,"segmentType","ARC");q(this,"center");q(this,"clockwise");q(this,"_coefficients",null);q(this,"_angularLength",null);q(this,"_radius",null);q(this,"_firstAngle",null);q(this,"_lastAngle",null);q(this,"_boundingBox",null);if(this.center=n,this.clockwise=i,!o){if(O(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 ${Z(t)} and ${Z(e)}} (center ${Z(n)})`)}}get info(){return`ARC(${Z(this.firstPoint)}, ${Z(this.lastPoint)}, ${Z(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 Jt(this.firstAngle,dt(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=Jt(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return it(this.center,vt(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,n]=Ut(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error(`Point ${Z(t)} is not on segment ${this.repr}`);const i=this.angleToParam(n);if(!this.isValidParameter(i))throw new Error(`Point ${Z(t)} is not on segment ${this.repr}`);return i}get radius(){return this._radius===null&&(this._radius=J(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=G(this.firstPoint,this.center);this._firstAngle=dt(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=G(this.lastPoint,this.center);this._lastAngle=dt(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 ut)||!O(this.center,t.center)?!1:O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||O(this.lastPoint,t.firstPoint)&&O(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new ut(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new ut(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 ft(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]=Ut(t,this.center);return this.isValidParameter(this.angleToParam(n))?Math.abs(e-this.radius):Math.sqrt(Math.min(Et(t,this.firstPoint),Et(t,this.lastPoint)))}isOnSegment(t){if(O(t,this.firstPoint)||O(t,this.lastPoint))return!0;const[e,n]=Ut(t,this.center);if(Math.abs(e-this.radius)>this.precision)return!1;const i=this.angleToParam(n);return this.isValidParameter(i)}gradientAt(t){const e=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),n=this.radius*this.angularLength,i=-n*Math.sin(e),o=n*Math.cos(e);return this.clockwise?[-i,-o]:[i,o]}tangentAt(t){const[e,n]=Ut(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error("Point is not on the arc");const i=this.angleToParam(n);if(!this.isValidParameter(i))throw new Error("Point is not on the arc");const o=vt(1,n);return(this.clockwise?Ct:H)(K(o))}get tangentAtFirstPoint(){const t=vt(1,this.firstAngle);return(this.clockwise?Ct:H)(K(t))}get tangentAtLastPoint(){const t=vt(1,this.lastAngle);return(this.clockwise?Ct:H)(K(t))}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const i=[0,1,...e.map(h=>this.pointToParam(h))],o=new Map(Vt([i,[this.firstPoint,this.lastPoint,...e]]));i.sort((h,a)=>h-a);let l=null;return i.flatMap((h,a)=>{if(a===i.length-1)return[];const u=i[a+1];if(u-h<this.precision)return l===null&&(l=h),[];const c=l===null?h:l,g=new ut(o.get(c)||this.paramPoint(c),o.get(u)||this.paramPoint(u),this.center,this.clockwise);return l=null,g})}transform(t){return new ut(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}}function ss(r,s,t){const e=new at(s,r),n=new at(s,t),i=H(e.tangentAtFirstPoint),o=H(n.tangentAtLastPoint),l=te({firstPoint:e.midPoint,V:i,precision:1e-9},{firstPoint:n.midPoint,V:o,precision:1e-9});if(l==="parallel")throw new Error("Cannot create an arc from three colinear points");const h=yt(G(r,s),G(t,s))>0;return new ut(r,t,it(e.midPoint,nt(i,l.intersectionParam1)),h,{ignoreChecks:!0})}function ns(r,s,t){const e=new at(r,s),n=H(e.tangentAtFirstPoint),i=te({firstPoint:e.midPoint,V:n,precision:1e-9},{firstPoint:r,V:H(t),precision:1e-9});if(i==="parallel")throw new Error("Cannot create an arc from three colinear points");const o=it(e.midPoint,nt(n,i.intersectionParam1)),l=yt(G(o,r),G(o,it(r,t)))<0;return new ut(r,s,o,l,{ignoreChecks:!0})}const _e=1e-21,Yt=1.618034;function je(r,s=0,t=1,e=110,n=1e3){let i,o,l,h,a,u,c,g,d,w,p,P,f;for(o=r(s),l=r(t),o<l&&([s,t]=[t,s],[o,l]=[l,o]),f=t+Yt*(t-s),h=r(f),a=3,c=0;h<l;){if(g=(t-s)*(l-h),d=(t-f)*(l-o),w=d-g,Math.abs(w)<_e?i=2*_e:i=2*w,p=t-((t-f)*d-(t-s)*g)/i,P=t+e*(f-t),c>n)throw new Error("Too many iterations.");if(c+=1,(p-f)*(t-p)>0){if(u=r(p),a+=1,u<h)return s=t,t=p,o=l,l=u,[s,t,f,o,l,h,a];if(u>l)return f=p,h=u,[s,t,f,o,l,h,a];p=f+Yt*(f-t),u=r(p),a+=1}else(p-P)*(P-f)>=0?(p=P,u=r(p),a+=1):(p-P)*(f-p)>0?(u=r(p),a+=1,u<h&&(t=f,f=p,p=f+Yt*(f-t),l=h,h=u,u=r(p),a+=1)):(p=f+Yt*(f-t),u=r(p),a+=1);s=t,t=f,f=p,o=l,l=h,h=u}return[s,t,f,o,l,h,a]}const Se=.381966,is=1e-11;class rs{constructor(s,t=148e-10,e=500){q(this,"xmin");q(this,"fval");q(this,"iter");q(this,"funcalls");q(this,"brack");this.func=s,this.tol=t,this.maxiter=e,this.func=s,this.tol=t,this.maxiter=e,this.xmin=1/0,this.fval=1/0,this.iter=0,this.funcalls=0,this.brack=null}setBracket(s=null){this.brack=s}getBracketInfo(){let s,t,e,n,i,o,l;const h=this.func,a=this.brack;if(a===null)[i,o,l,s,t,e,n]=je(h);else if(a.length===2)[i,o,l,s,t,e,n]=je(h,a[0],a[1]);else if(a.length===3){if([i,o,l]=a,i>l&&([l,i]=[i,l]),!(i<o&&o<l))throw new Error("Not a bracketing interval.");if(s=h(i),t=h(o),e=h(l),!(t<s&&t<e))throw new Error("Not a bracketing interval.");n=3}else throw new Error("Bracketing interval must be length 2 or 3 sequence.");return[i,o,l,s,t,e,n]}optimize(){const s=this.func;let[t,e,n,,,,i]=this.getBracketInfo(),o=e,l=e,h=e,a=s(o),u=a,c=a,g,d;t<n?(g=t,d=n):(g=n,d=t);let w=0;i=1;let p=0,P=0,f;for(;p<this.maxiter;){const M=this.tol*Math.abs(o)+is,E=2*M,v=.5*(g+d);if(Math.abs(o-v)<E-.5*(d-g))break;if(Math.abs(w)<=M)o>=v?w=g-o:w=d-o,P=Se*w;else{const D=(o-l)*(c-u);let S=(o-h)*(c-a),k=(o-h)*S-(o-l)*D;S=2*(S-D),S>0&&(k=-k),S=Math.abs(S);const L=w;w=P,k>S*(g-o)&&k<S*(d-o)&&Math.abs(k)<Math.abs(.5*S*L)?(P=k*1/S,f=o+P,(f-g<E||d-f<E)&&(v-o>=0?P=M:P=-M)):(o>=v?w=g-o:w=d-o,P=Se*w)}Math.abs(P)<M?P>=0?f=o+M:f=o-M:f=o+P;const R=s(f);i+=1,R>c?(f<o?g=f:d=f,R<=a||l===o?(h=l,l=f,u=a,a=R):(R<=u||h===o||h===l)&&(h=f,u=R)):(f>=o?g=o:d=o,h=l,l=o,o=f,u=a,a=c,c=R),p+=1}this.xmin=o,this.fval=c,this.iter=p,this.funcalls=i}results(){return{argMin:this.xmin,fMin:this.fval,iterations:this.iter,funcCalls:this.funcalls}}run(){return this.optimize(),this.results()}}function Ve(r,s=1e-8,t=1e3){const e=new rs(r,s,t);return e.setBracket([0,1]),e.run()}function Ne(r){if(r.__esModule)return r;var s=r.default;if(typeof s=="function"){var t=function e(){return this instanceof e?Reflect.construct(s,arguments,this.constructor):s.apply(this,arguments)};t.prototype=s.prototype}else t={};return Object.defineProperty(t,"__esModule",{value:!0}),Object.keys(r).forEach(function(e){var n=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(t,e,n.get?n:{enumerable:!0,get:function(){return r[e]}})}),t}var $={};const os=Object.prototype.toString;function qt(r){const s=os.call(r);return s.endsWith("Array]")&&!s.includes("Big")}const ls=Object.freeze(Object.defineProperty({__proto__:null,isAnyArray:qt},Symbol.toStringTag,{value:"Module"})),hs=Ne(ls);function as(r){var s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!qt(r))throw new TypeError("input must be an array");if(r.length===0)throw new TypeError("input must not be empty");var t=s.fromIndex,e=t===void 0?0:t,n=s.toIndex,i=n===void 0?r.length:n;if(e<0||e>=r.length||!Number.isInteger(e))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=e||i>r.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=r[e],l=e+1;l<i;l++)r[l]>o&&(o=r[l]);return o}function us(r){var s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!qt(r))throw new TypeError("input must be an array");if(r.length===0)throw new TypeError("input must not be empty");var t=s.fromIndex,e=t===void 0?0:t,n=s.toIndex,i=n===void 0?r.length:n;if(e<0||e>=r.length||!Number.isInteger(e))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=e||i>r.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=r[e],l=e+1;l<i;l++)r[l]<o&&(o=r[l]);return o}function cs(r){var s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(qt(r)){if(r.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var t;if(s.output!==void 0){if(!qt(s.output))throw new TypeError("output option must be an array if specified");t=s.output}else t=new Array(r.length);var e=us(r),n=as(r);if(e===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=s.min,o=i===void 0?s.autoMinMax?e:0:i,l=s.max,h=l===void 0?s.autoMinMax?n:1:l;if(o>=h)throw new RangeError("min option must be smaller than max option");for(var a=(h-o)/(n-e),u=0;u<r.length;u++)t[u]=(r[u]-e)*a+o;return t}const fs=Object.freeze(Object.defineProperty({__proto__:null,default:cs},Symbol.toStringTag,{value:"Module"})),ms=Ne(fs);Object.defineProperty($,"__esModule",{value:!0});var st=hs,ke=ms;const Wt=" ".repeat(2),$e=" ".repeat(4);function gs(){return De(this)}function De(r,s={}){const{maxRows:t=15,maxColumns:e=10,maxNumSize:n=8,padMinus:i="auto"}=s;return`${r.constructor.name} {
|
|
2
2
|
${Wt}[
|
|
3
|
-
${
|
|
3
|
+
${$e}${ws(r,t,e,n,i)}
|
|
4
4
|
${Wt}]
|
|
5
5
|
${Wt}rows: ${r.rows}
|
|
6
6
|
${Wt}columns: ${r.columns}
|
|
7
7
|
}`}function ws(r,s,t,e,n){const{rows:i,columns:o}=r,l=Math.min(i,s),h=Math.min(o,t),a=[];if(n==="auto"){n=!1;t:for(let u=0;u<l;u++)for(let c=0;c<h;c++)if(r.get(u,c)<0){n=!0;break t}}for(let u=0;u<l;u++){let c=[];for(let g=0;g<h;g++)c.push(ps(r.get(u,g),e,n));a.push(`${c.join(" ")}`)}return h!==o&&(a[a.length-1]+=` ... ${o-t} more columns`),l!==i&&a.push(`... ${i-s} more rows`),a.join(`
|
|
8
|
-
${Ne}`)}function ps(r,s,t){return(r>=0&&t?` ${ve(r,s-1)}`:ve(r,s)).padEnd(s)}function ve(r,s){let t=r.toString();if(t.length<=s)return t;let e=r.toFixed(s);if(e.length>s&&(e=r.toFixed(Math.max(0,s-(e.length-s)))),e.length<=s&&!e.startsWith("0.000")&&!e.startsWith("-0.000"))return e;let n=r.toExponential(s);return n.length>s&&(n=r.toExponential(Math.max(0,s-(n.length-s)))),n.slice(0)}function ds(r,s){r.prototype.add=function(e){return typeof e=="number"?this.addS(e):this.addM(e)},r.prototype.addS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)+e);return this},r.prototype.addM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)+e.get(n,i));return this},r.add=function(e,n){return new s(e).add(n)},r.prototype.sub=function(e){return typeof e=="number"?this.subS(e):this.subM(e)},r.prototype.subS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)-e);return this},r.prototype.subM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)-e.get(n,i));return this},r.sub=function(e,n){return new s(e).sub(n)},r.prototype.subtract=r.prototype.sub,r.prototype.subtractS=r.prototype.subS,r.prototype.subtractM=r.prototype.subM,r.subtract=r.sub,r.prototype.mul=function(e){return typeof e=="number"?this.mulS(e):this.mulM(e)},r.prototype.mulS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)*e);return this},r.prototype.mulM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)*e.get(n,i));return this},r.mul=function(e,n){return new s(e).mul(n)},r.prototype.multiply=r.prototype.mul,r.prototype.multiplyS=r.prototype.mulS,r.prototype.multiplyM=r.prototype.mulM,r.multiply=r.mul,r.prototype.div=function(e){return typeof e=="number"?this.divS(e):this.divM(e)},r.prototype.divS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)/e);return this},r.prototype.divM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)/e.get(n,i));return this},r.div=function(e,n){return new s(e).div(n)},r.prototype.divide=r.prototype.div,r.prototype.divideS=r.prototype.divS,r.prototype.divideM=r.prototype.divM,r.divide=r.div,r.prototype.mod=function(e){return typeof e=="number"?this.modS(e):this.modM(e)},r.prototype.modS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)%e);return this},r.prototype.modM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)%e.get(n,i));return this},r.mod=function(e,n){return new s(e).mod(n)},r.prototype.modulus=r.prototype.mod,r.prototype.modulusS=r.prototype.modS,r.prototype.modulusM=r.prototype.modM,r.modulus=r.mod,r.prototype.and=function(e){return typeof e=="number"?this.andS(e):this.andM(e)},r.prototype.andS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)&e);return this},r.prototype.andM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)&e.get(n,i));return this},r.and=function(e,n){return new s(e).and(n)},r.prototype.or=function(e){return typeof e=="number"?this.orS(e):this.orM(e)},r.prototype.orS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)|e);return this},r.prototype.orM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)|e.get(n,i));return this},r.or=function(e,n){return new s(e).or(n)},r.prototype.xor=function(e){return typeof e=="number"?this.xorS(e):this.xorM(e)},r.prototype.xorS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)^e);return this},r.prototype.xorM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)^e.get(n,i));return this},r.xor=function(e,n){return new s(e).xor(n)},r.prototype.leftShift=function(e){return typeof e=="number"?this.leftShiftS(e):this.leftShiftM(e)},r.prototype.leftShiftS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)<<e);return this},r.prototype.leftShiftM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)<<e.get(n,i));return this},r.leftShift=function(e,n){return new s(e).leftShift(n)},r.prototype.signPropagatingRightShift=function(e){return typeof e=="number"?this.signPropagatingRightShiftS(e):this.signPropagatingRightShiftM(e)},r.prototype.signPropagatingRightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>e);return this},r.prototype.signPropagatingRightShiftM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>e.get(n,i));return this},r.signPropagatingRightShift=function(e,n){return new s(e).signPropagatingRightShift(n)},r.prototype.rightShift=function(e){return typeof e=="number"?this.rightShiftS(e):this.rightShiftM(e)},r.prototype.rightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>>e);return this},r.prototype.rightShiftM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>>e.get(n,i));return this},r.rightShift=function(e,n){return new s(e).rightShift(n)},r.prototype.zeroFillRightShift=r.prototype.rightShift,r.prototype.zeroFillRightShiftS=r.prototype.rightShiftS,r.prototype.zeroFillRightShiftM=r.prototype.rightShiftM,r.zeroFillRightShift=r.rightShift,r.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},r.not=function(e){return new s(e).not()},r.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},r.abs=function(e){return new s(e).abs()},r.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},r.acos=function(e){return new s(e).acos()},r.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},r.acosh=function(e){return new s(e).acosh()},r.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},r.asin=function(e){return new s(e).asin()},r.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},r.asinh=function(e){return new s(e).asinh()},r.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},r.atan=function(e){return new s(e).atan()},r.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},r.atanh=function(e){return new s(e).atanh()},r.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},r.cbrt=function(e){return new s(e).cbrt()},r.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},r.ceil=function(e){return new s(e).ceil()},r.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},r.clz32=function(e){return new s(e).clz32()},r.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},r.cos=function(e){return new s(e).cos()},r.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},r.cosh=function(e){return new s(e).cosh()},r.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},r.exp=function(e){return new s(e).exp()},r.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},r.expm1=function(e){return new s(e).expm1()},r.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},r.floor=function(e){return new s(e).floor()},r.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},r.fround=function(e){return new s(e).fround()},r.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},r.log=function(e){return new s(e).log()},r.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},r.log1p=function(e){return new s(e).log1p()},r.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},r.log10=function(e){return new s(e).log10()},r.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},r.log2=function(e){return new s(e).log2()},r.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},r.round=function(e){return new s(e).round()},r.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},r.sign=function(e){return new s(e).sign()},r.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},r.sin=function(e){return new s(e).sin()},r.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},r.sinh=function(e){return new s(e).sinh()},r.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},r.sqrt=function(e){return new s(e).sqrt()},r.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},r.tan=function(e){return new s(e).tan()},r.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},r.tanh=function(e){return new s(e).tanh()},r.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},r.trunc=function(e){return new s(e).trunc()},r.pow=function(e,n){return new s(e).pow(n)},r.prototype.pow=function(e){return typeof e=="number"?this.powS(e):this.powM(e)},r.prototype.powS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)**e);return this},r.prototype.powM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)**e.get(n,i));return this}}function ot(r,s,t){let e=t?r.rows:r.rows-1;if(s<0||s>e)throw new RangeError("Row index out of range")}function lt(r,s,t){let e=t?r.columns:r.columns-1;if(s<0||s>e)throw new RangeError("Column index out of range")}function St(r,s){if(s.to1DArray&&(s=s.to1DArray()),s.length!==r.columns)throw new RangeError("vector size must be the same as the number of columns");return s}function kt(r,s){if(s.to1DArray&&(s=s.to1DArray()),s.length!==r.rows)throw new RangeError("vector size must be the same as the number of rows");return s}function de(r,s){if(!st.isAnyArray(s))throw new TypeError("row indices must be an array");for(let t=0;t<s.length;t++)if(s[t]<0||s[t]>=r.rows)throw new RangeError("row indices are out of range")}function Me(r,s){if(!st.isAnyArray(s))throw new TypeError("column indices must be an array");for(let t=0;t<s.length;t++)if(s[t]<0||s[t]>=r.columns)throw new RangeError("column indices are out of range")}function fe(r,s,t,e,n){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Gt("startRow",s),Gt("endRow",t),Gt("startColumn",e),Gt("endColumn",n),s>t||e>n||s<0||s>=r.rows||t<0||t>=r.rows||e<0||e>=r.columns||n<0||n>=r.columns)throw new RangeError("Submatrix indices are out of range")}function ee(r,s=0){let t=[];for(let e=0;e<r;e++)t.push(s);return t}function Gt(r,s){if(typeof s!="number")throw new TypeError(`${r} must be a number`)}function jt(r){if(r.isEmpty())throw new Error("Empty matrix has no elements to index")}function Ms(r){let s=ee(r.rows);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[t]+=r.get(t,e);return s}function ys(r){let s=ee(r.columns);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[e]+=r.get(t,e);return s}function Ps(r){let s=0;for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)s+=r.get(t,e);return s}function bs(r){let s=ee(r.rows,1);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[t]*=r.get(t,e);return s}function Es(r){let s=ee(r.columns,1);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[e]*=r.get(t,e);return s}function _s(r){let s=1;for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)s*=r.get(t,e);return s}function js(r,s,t){const e=r.rows,n=r.columns,i=[];for(let o=0;o<e;o++){let l=0,h=0,a=0;for(let u=0;u<n;u++)a=r.get(o,u)-t[o],l+=a,h+=a*a;s?i.push((h-l*l/n)/(n-1)):i.push((h-l*l/n)/n)}return i}function Ss(r,s,t){const e=r.rows,n=r.columns,i=[];for(let o=0;o<n;o++){let l=0,h=0,a=0;for(let u=0;u<e;u++)a=r.get(u,o)-t[o],l+=a,h+=a*a;s?i.push((h-l*l/e)/(e-1)):i.push((h-l*l/e)/e)}return i}function ks(r,s,t){const e=r.rows,n=r.columns,i=e*n;let o=0,l=0,h=0;for(let a=0;a<e;a++)for(let u=0;u<n;u++)h=r.get(a,u)-t,o+=h,l+=h*h;return s?(l-o*o/i)/(i-1):(l-o*o/i)/i}function vs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)-s[t])}function Rs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)-s[e])}function Ts(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)-s)}function Is(r){const s=[];for(let t=0;t<r.rows;t++){let e=0;for(let n=0;n<r.columns;n++)e+=r.get(t,n)**2/(r.columns-1);s.push(Math.sqrt(e))}return s}function Cs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)/s[t])}function qs(r){const s=[];for(let t=0;t<r.columns;t++){let e=0;for(let n=0;n<r.rows;n++)e+=r.get(n,t)**2/(r.rows-1);s.push(Math.sqrt(e))}return s}function Fs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)/s[e])}function As(r){const s=r.size-1;let t=0;for(let e=0;e<r.columns;e++)for(let n=0;n<r.rows;n++)t+=r.get(n,e)**2/s;return Math.sqrt(t)}function xs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)/s)}class V{static from1DArray(s,t,e){if(s*t!==e.length)throw new RangeError("data length does not match given dimensions");let i=new _(s,t);for(let o=0;o<s;o++)for(let l=0;l<t;l++)i.set(o,l,e[o*t+l]);return i}static rowVector(s){let t=new _(1,s.length);for(let e=0;e<s.length;e++)t.set(0,e,s[e]);return t}static columnVector(s){let t=new _(s.length,1);for(let e=0;e<s.length;e++)t.set(e,0,s[e]);return t}static zeros(s,t){return new _(s,t)}static ones(s,t){return new _(s,t).fill(1)}static rand(s,t,e={}){if(typeof e!="object")throw new TypeError("options must be an object");const{random:n=Math.random}=e;let i=new _(s,t);for(let o=0;o<s;o++)for(let l=0;l<t;l++)i.set(o,l,n());return i}static randInt(s,t,e={}){if(typeof e!="object")throw new TypeError("options must be an object");const{min:n=0,max:i=1e3,random:o=Math.random}=e;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(i))throw new TypeError("max must be an integer");if(n>=i)throw new RangeError("min must be smaller than max");let l=i-n,h=new _(s,t);for(let a=0;a<s;a++)for(let u=0;u<t;u++){let c=n+Math.round(o()*l);h.set(a,u,c)}return h}static eye(s,t,e){t===void 0&&(t=s),e===void 0&&(e=1);let n=Math.min(s,t),i=this.zeros(s,t);for(let o=0;o<n;o++)i.set(o,o,e);return i}static diag(s,t,e){let n=s.length;t===void 0&&(t=n),e===void 0&&(e=t);let i=Math.min(n,t,e),o=this.zeros(t,e);for(let l=0;l<i;l++)o.set(l,l,s[l]);return o}static min(s,t){s=this.checkMatrix(s),t=this.checkMatrix(t);let e=s.rows,n=s.columns,i=new _(e,n);for(let o=0;o<e;o++)for(let l=0;l<n;l++)i.set(o,l,Math.min(s.get(o,l),t.get(o,l)));return i}static max(s,t){s=this.checkMatrix(s),t=this.checkMatrix(t);let e=s.rows,n=s.columns,i=new this(e,n);for(let o=0;o<e;o++)for(let l=0;l<n;l++)i.set(o,l,Math.max(s.get(o,l),t.get(o,l)));return i}static checkMatrix(s){return V.isMatrix(s)?s:new _(s)}static isMatrix(s){return s!=null&&s.klass==="Matrix"}get size(){return this.rows*this.columns}apply(s){if(typeof s!="function")throw new TypeError("callback must be a function");for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s.call(this,t,e);return this}to1DArray(){let s=[];for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s.push(this.get(t,e));return s}to2DArray(){let s=[];for(let t=0;t<this.rows;t++){s.push([]);for(let e=0;e<this.columns;e++)s[t].push(this.get(t,e))}return s}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 s=0;s<this.rows;s++)for(let t=0;t<=s;t++)if(this.get(s,t)!==this.get(t,s))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let s=0;s<this.rows;s++)if(this.get(s,s)!==0)return!1;return!0}isEchelonForm(){let s=0,t=0,e=-1,n=!0,i=!1;for(;s<this.rows&&n;){for(t=0,i=!1;t<this.columns&&i===!1;)this.get(s,t)===0?t++:this.get(s,t)===1&&t>e?(i=!0,e=t):(n=!1,i=!0);s++}return n}isReducedEchelonForm(){let s=0,t=0,e=-1,n=!0,i=!1;for(;s<this.rows&&n;){for(t=0,i=!1;t<this.columns&&i===!1;)this.get(s,t)===0?t++:this.get(s,t)===1&&t>e?(i=!0,e=t):(n=!1,i=!0);for(let o=t+1;o<this.rows;o++)this.get(s,o)!==0&&(n=!1);s++}return n}echelonForm(){let s=this.clone(),t=0,e=0;for(;t<s.rows&&e<s.columns;){let n=t;for(let i=t;i<s.rows;i++)s.get(i,e)>s.get(n,e)&&(n=i);if(s.get(n,e)===0)e++;else{s.swapRows(t,n);let i=s.get(t,e);for(let o=e;o<s.columns;o++)s.set(t,o,s.get(t,o)/i);for(let o=t+1;o<s.rows;o++){let l=s.get(o,e)/s.get(t,e);s.set(o,e,0);for(let h=e+1;h<s.columns;h++)s.set(o,h,s.get(o,h)-s.get(t,h)*l)}t++,e++}}return s}reducedEchelonForm(){let s=this.echelonForm(),t=s.columns,e=s.rows,n=e-1;for(;n>=0;)if(s.maxRow(n)===0)n--;else{let i=0,o=!1;for(;i<e&&o===!1;)s.get(n,i)===1?o=!0:i++;for(let l=0;l<n;l++){let h=s.get(l,i);for(let a=i;a<t;a++){let u=s.get(l,a)-h*s.get(n,a);s.set(l,a,u)}}n--}return s}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{rows:t=1,columns:e=1}=s;if(!Number.isInteger(t)||t<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(e)||e<=0)throw new TypeError("columns must be a positive integer");let n=new _(this.rows*t,this.columns*e);for(let i=0;i<t;i++)for(let o=0;o<e;o++)n.setSubMatrix(this,this.rows*i,this.columns*o);return n}fill(s){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,s);return this}neg(){return this.mulS(-1)}getRow(s){ot(this,s);let t=[];for(let e=0;e<this.columns;e++)t.push(this.get(s,e));return t}getRowVector(s){return _.rowVector(this.getRow(s))}setRow(s,t){ot(this,s),t=St(this,t);for(let e=0;e<this.columns;e++)this.set(s,e,t[e]);return this}swapRows(s,t){ot(this,s),ot(this,t);for(let e=0;e<this.columns;e++){let n=this.get(s,e);this.set(s,e,this.get(t,e)),this.set(t,e,n)}return this}getColumn(s){lt(this,s);let t=[];for(let e=0;e<this.rows;e++)t.push(this.get(e,s));return t}getColumnVector(s){return _.columnVector(this.getColumn(s))}setColumn(s,t){lt(this,s),t=kt(this,t);for(let e=0;e<this.rows;e++)this.set(e,s,t[e]);return this}swapColumns(s,t){lt(this,s),lt(this,t);for(let e=0;e<this.rows;e++){let n=this.get(e,s);this.set(e,s,this.get(e,t)),this.set(e,t,n)}return this}addRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)+s[e]);return this}subRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)-s[e]);return this}mulRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)*s[e]);return this}divRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)/s[e]);return this}addColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)+s[t]);return this}subColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)-s[t]);return this}mulColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)*s[t]);return this}divColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)/s[t]);return this}mulRow(s,t){ot(this,s);for(let e=0;e<this.columns;e++)this.set(s,e,this.get(s,e)*t);return this}mulColumn(s,t){lt(this,s);for(let e=0;e<this.rows;e++)this.set(e,s,this.get(e,s)*t);return this}max(s){if(this.isEmpty())return NaN;switch(s){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${s}`)}}maxIndex(){jt(this);let s=this.get(0,0),t=[0,0];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>s&&(s=this.get(e,n),t[0]=e,t[1]=n);return t}min(s){if(this.isEmpty())return NaN;switch(s){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${s}`)}}minIndex(){jt(this);let s=this.get(0,0),t=[0,0];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<s&&(s=this.get(e,n),t[0]=e,t[1]=n);return t}maxRow(s){if(ot(this,s),this.isEmpty())return NaN;let t=this.get(s,0);for(let e=1;e<this.columns;e++)this.get(s,e)>t&&(t=this.get(s,e));return t}maxRowIndex(s){ot(this,s),jt(this);let t=this.get(s,0),e=[s,0];for(let n=1;n<this.columns;n++)this.get(s,n)>t&&(t=this.get(s,n),e[1]=n);return e}minRow(s){if(ot(this,s),this.isEmpty())return NaN;let t=this.get(s,0);for(let e=1;e<this.columns;e++)this.get(s,e)<t&&(t=this.get(s,e));return t}minRowIndex(s){ot(this,s),jt(this);let t=this.get(s,0),e=[s,0];for(let n=1;n<this.columns;n++)this.get(s,n)<t&&(t=this.get(s,n),e[1]=n);return e}maxColumn(s){if(lt(this,s),this.isEmpty())return NaN;let t=this.get(0,s);for(let e=1;e<this.rows;e++)this.get(e,s)>t&&(t=this.get(e,s));return t}maxColumnIndex(s){lt(this,s),jt(this);let t=this.get(0,s),e=[0,s];for(let n=1;n<this.rows;n++)this.get(n,s)>t&&(t=this.get(n,s),e[0]=n);return e}minColumn(s){if(lt(this,s),this.isEmpty())return NaN;let t=this.get(0,s);for(let e=1;e<this.rows;e++)this.get(e,s)<t&&(t=this.get(e,s));return t}minColumnIndex(s){lt(this,s),jt(this);let t=this.get(0,s),e=[0,s];for(let n=1;n<this.rows;n++)this.get(n,s)<t&&(t=this.get(n,s),e[0]=n);return e}diag(){let s=Math.min(this.rows,this.columns),t=[];for(let e=0;e<s;e++)t.push(this.get(e,e));return t}norm(s="frobenius"){switch(s){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${s}`)}}cumulativeSum(){let s=0;for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s+=this.get(t,e),this.set(t,e,s);return this}dot(s){V.isMatrix(s)&&(s=s.to1DArray());let t=this.to1DArray();if(t.length!==s.length)throw new RangeError("vectors do not have the same size");let e=0;for(let n=0;n<t.length;n++)e+=t[n]*s[n];return e}mmul(s){s=_.checkMatrix(s);let t=this.rows,e=this.columns,n=s.columns,i=new _(t,n),o=new Float64Array(e);for(let l=0;l<n;l++){for(let h=0;h<e;h++)o[h]=s.get(h,l);for(let h=0;h<t;h++){let a=0;for(let u=0;u<e;u++)a+=this.get(h,u)*o[u];i.set(h,l,a)}}return i}mpow(s){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(s)||s<0)throw new RangeError("Exponent must be a non-negative integer");let t=_.eye(this.rows),e=this;for(let n=s;n>1;n/=2)n&1&&(t=t.mmul(e)),e=e.mmul(e);return t}strassen2x2(s){s=_.checkMatrix(s);let t=new _(2,2);const e=this.get(0,0),n=s.get(0,0),i=this.get(0,1),o=s.get(0,1),l=this.get(1,0),h=s.get(1,0),a=this.get(1,1),u=s.get(1,1),c=(e+a)*(n+u),g=(l+a)*n,d=e*(o-u),w=a*(h-n),p=(e+i)*u,P=(l-e)*(n+o),f=(i-a)*(h+u),M=c+w-p+f,E=d+p,v=g+w,R=c-g+d+P;return t.set(0,0,M),t.set(0,1,E),t.set(1,0,v),t.set(1,1,R),t}strassen3x3(s){s=_.checkMatrix(s);let t=new _(3,3);const e=this.get(0,0),n=this.get(0,1),i=this.get(0,2),o=this.get(1,0),l=this.get(1,1),h=this.get(1,2),a=this.get(2,0),u=this.get(2,1),c=this.get(2,2),g=s.get(0,0),d=s.get(0,1),w=s.get(0,2),p=s.get(1,0),P=s.get(1,1),f=s.get(1,2),M=s.get(2,0),E=s.get(2,1),v=s.get(2,2),R=(e+n+i-o-l-u-c)*P,D=(e-o)*(-d+P),S=l*(-g+d+p-P-f-M+v),k=(-e+o+l)*(g-d+P),L=(o+l)*(-g+d),m=e*g,b=(-e+a+u)*(g-w+f),j=(-e+a)*(w-f),y=(a+u)*(-g+w),C=(e+n+i-l-h-a-u)*f,F=u*(-g+w+p-P-f-M+E),I=(-i+u+c)*(P+M-E),U=(i-c)*(P-E),A=i*M,N=(u+c)*(-M+E),x=(-i+l+h)*(f+M-v),B=(i-h)*(f-v),Y=(l+h)*(-M+v),T=n*p,z=h*E,X=o*w,Q=a*d,W=c*v,ie=m+A+T,re=R+k+L+m+I+A+N,oe=m+b+y+C+A+x+Y,le=D+S+k+m+A+x+B,Nt=D+k+L+m+z,$t=A+x+B+Y+X,Dt=m+b+j+F+I+U+A,Lt=I+U+A+N+Q,zt=m+b+j+y+W;return t.set(0,0,ie),t.set(0,1,re),t.set(0,2,oe),t.set(1,0,le),t.set(1,1,Nt),t.set(1,2,$t),t.set(2,0,Dt),t.set(2,1,Lt),t.set(2,2,zt),t}mmulStrassen(s){s=_.checkMatrix(s);let t=this.clone(),e=t.rows,n=t.columns,i=s.rows,o=s.columns;n!==i&&console.warn(`Multiplying ${e} x ${n} and ${i} x ${o} matrix: dimensions do not match.`);function l(c,g,d){let w=c.rows,p=c.columns;if(w===g&&p===d)return c;{let P=V.zeros(g,d);return P=P.setSubMatrix(c,0,0),P}}let h=Math.max(e,i),a=Math.max(n,o);t=l(t,h,a),s=l(s,h,a);function u(c,g,d,w){if(d<=512||w<=512)return c.mmul(g);d%2===1&&w%2===1?(c=l(c,d+1,w+1),g=l(g,d+1,w+1)):d%2===1?(c=l(c,d+1,w),g=l(g,d+1,w)):w%2===1&&(c=l(c,d,w+1),g=l(g,d,w+1));let p=parseInt(c.rows/2,10),P=parseInt(c.columns/2,10),f=c.subMatrix(0,p-1,0,P-1),M=g.subMatrix(0,p-1,0,P-1),E=c.subMatrix(0,p-1,P,c.columns-1),v=g.subMatrix(0,p-1,P,g.columns-1),R=c.subMatrix(p,c.rows-1,0,P-1),D=g.subMatrix(p,g.rows-1,0,P-1),S=c.subMatrix(p,c.rows-1,P,c.columns-1),k=g.subMatrix(p,g.rows-1,P,g.columns-1),L=u(V.add(f,S),V.add(M,k),p,P),m=u(V.add(R,S),M,p,P),b=u(f,V.sub(v,k),p,P),j=u(S,V.sub(D,M),p,P),y=u(V.add(f,E),k,p,P),C=u(V.sub(R,f),V.add(M,v),p,P),F=u(V.sub(E,S),V.add(D,k),p,P),I=V.add(L,j);I.sub(y),I.add(F);let U=V.add(b,y),A=V.add(m,j),N=V.sub(L,m);N.add(b),N.add(C);let x=V.zeros(2*I.rows,2*I.columns);return x=x.setSubMatrix(I,0,0),x=x.setSubMatrix(U,I.rows,0),x=x.setSubMatrix(A,0,I.columns),x=x.setSubMatrix(N,I.rows,I.columns),x.subMatrix(0,d-1,0,w-1)}return u(t,s,h,a)}scaleRows(s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{min:t=0,max:e=1}=s;if(!Number.isFinite(t))throw new TypeError("min must be a number");if(!Number.isFinite(e))throw new TypeError("max must be a number");if(t>=e)throw new RangeError("min must be smaller than max");let n=new _(this.rows,this.columns);for(let i=0;i<this.rows;i++){const o=this.getRow(i);o.length>0&&ke(o,{min:t,max:e,output:o}),n.setRow(i,o)}return n}scaleColumns(s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{min:t=0,max:e=1}=s;if(!Number.isFinite(t))throw new TypeError("min must be a number");if(!Number.isFinite(e))throw new TypeError("max must be a number");if(t>=e)throw new RangeError("min must be smaller than max");let n=new _(this.rows,this.columns);for(let i=0;i<this.columns;i++){const o=this.getColumn(i);o.length&&ke(o,{min:t,max:e,output:o}),n.setColumn(i,o)}return n}flipRows(){const s=Math.ceil(this.columns/2);for(let t=0;t<this.rows;t++)for(let e=0;e<s;e++){let n=this.get(t,e),i=this.get(t,this.columns-1-e);this.set(t,e,i),this.set(t,this.columns-1-e,n)}return this}flipColumns(){const s=Math.ceil(this.rows/2);for(let t=0;t<this.columns;t++)for(let e=0;e<s;e++){let n=this.get(e,t),i=this.get(this.rows-1-e,t);this.set(e,t,i),this.set(this.rows-1-e,t,n)}return this}kroneckerProduct(s){s=_.checkMatrix(s);let t=this.rows,e=this.columns,n=s.rows,i=s.columns,o=new _(t*n,e*i);for(let l=0;l<t;l++)for(let h=0;h<e;h++)for(let a=0;a<n;a++)for(let u=0;u<i;u++)o.set(n*l+a,i*h+u,this.get(l,h)*s.get(a,u));return o}kroneckerSum(s){if(s=_.checkMatrix(s),!this.isSquare()||!s.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let t=this.rows,e=s.rows,n=this.kroneckerProduct(_.eye(e,e)),i=_.eye(t,t).kroneckerProduct(s);return n.add(i)}transpose(){let s=new _(this.columns,this.rows);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s.set(e,t,this.get(t,e));return s}sortRows(s=Re){for(let t=0;t<this.rows;t++)this.setRow(t,this.getRow(t).sort(s));return this}sortColumns(s=Re){for(let t=0;t<this.columns;t++)this.setColumn(t,this.getColumn(t).sort(s));return this}subMatrix(s,t,e,n){fe(this,s,t,e,n);let i=new _(t-s+1,n-e+1);for(let o=s;o<=t;o++)for(let l=e;l<=n;l++)i.set(o-s,l-e,this.get(o,l));return i}subMatrixRow(s,t,e){if(t===void 0&&(t=0),e===void 0&&(e=this.columns-1),t>e||t<0||t>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");let n=new _(s.length,e-t+1);for(let i=0;i<s.length;i++)for(let o=t;o<=e;o++){if(s[i]<0||s[i]>=this.rows)throw new RangeError(`Row index out of range: ${s[i]}`);n.set(i,o-t,this.get(s[i],o))}return n}subMatrixColumn(s,t,e){if(t===void 0&&(t=0),e===void 0&&(e=this.rows-1),t>e||t<0||t>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");let n=new _(e-t+1,s.length);for(let i=0;i<s.length;i++)for(let o=t;o<=e;o++){if(s[i]<0||s[i]>=this.columns)throw new RangeError(`Column index out of range: ${s[i]}`);n.set(o-t,i,this.get(o,s[i]))}return n}setSubMatrix(s,t,e){if(s=_.checkMatrix(s),s.isEmpty())return this;let n=t+s.rows-1,i=e+s.columns-1;fe(this,t,n,e,i);for(let o=0;o<s.rows;o++)for(let l=0;l<s.columns;l++)this.set(t+o,e+l,s.get(o,l));return this}selection(s,t){de(this,s),Me(this,t);let e=new _(s.length,t.length);for(let n=0;n<s.length;n++){let i=s[n];for(let o=0;o<t.length;o++){let l=t[o];e.set(n,o,this.get(i,l))}}return e}trace(){let s=Math.min(this.rows,this.columns),t=0;for(let e=0;e<s;e++)t+=this.get(e,e);return t}clone(){return this.constructor.copy(this,new _(this.rows,this.columns))}static copy(s,t){for(const[e,n,i]of s.entries())t.set(e,n,i);return t}sum(s){switch(s){case"row":return Ms(this);case"column":return ys(this);case void 0:return Ps(this);default:throw new Error(`invalid option: ${s}`)}}product(s){switch(s){case"row":return bs(this);case"column":return Es(this);case void 0:return _s(this);default:throw new Error(`invalid option: ${s}`)}}mean(s){const t=this.sum(s);switch(s){case"row":{for(let e=0;e<this.rows;e++)t[e]/=this.columns;return t}case"column":{for(let e=0;e<this.columns;e++)t[e]/=this.rows;return t}case void 0:return t/this.size;default:throw new Error(`invalid option: ${s}`)}}variance(s,t={}){if(typeof s=="object"&&(t=s,s=void 0),typeof t!="object")throw new TypeError("options must be an object");const{unbiased:e=!0,mean:n=this.mean(s)}=t;if(typeof e!="boolean")throw new TypeError("unbiased must be a boolean");switch(s){case"row":{if(!st.isAnyArray(n))throw new TypeError("mean must be an array");return js(this,e,n)}case"column":{if(!st.isAnyArray(n))throw new TypeError("mean must be an array");return Ss(this,e,n)}case void 0:{if(typeof n!="number")throw new TypeError("mean must be a number");return ks(this,e,n)}default:throw new Error(`invalid option: ${s}`)}}standardDeviation(s,t){typeof s=="object"&&(t=s,s=void 0);const e=this.variance(s,t);if(s===void 0)return Math.sqrt(e);for(let n=0;n<e.length;n++)e[n]=Math.sqrt(e[n]);return e}center(s,t={}){if(typeof s=="object"&&(t=s,s=void 0),typeof t!="object")throw new TypeError("options must be an object");const{center:e=this.mean(s)}=t;switch(s){case"row":{if(!st.isAnyArray(e))throw new TypeError("center must be an array");return vs(this,e),this}case"column":{if(!st.isAnyArray(e))throw new TypeError("center must be an array");return Rs(this,e),this}case void 0:{if(typeof e!="number")throw new TypeError("center must be a number");return Ts(this,e),this}default:throw new Error(`invalid option: ${s}`)}}scale(s,t={}){if(typeof s=="object"&&(t=s,s=void 0),typeof t!="object")throw new TypeError("options must be an object");let e=t.scale;switch(s){case"row":{if(e===void 0)e=Is(this);else if(!st.isAnyArray(e))throw new TypeError("scale must be an array");return Cs(this,e),this}case"column":{if(e===void 0)e=qs(this);else if(!st.isAnyArray(e))throw new TypeError("scale must be an array");return Fs(this,e),this}case void 0:{if(e===void 0)e=As(this);else if(typeof e!="number")throw new TypeError("scale must be a number");return xs(this,e),this}default:throw new Error(`invalid option: ${s}`)}}toString(s){return $e(this,s)}[Symbol.iterator](){return this.entries()}*entries(){for(let s=0;s<this.rows;s++)for(let t=0;t<this.columns;t++)yield[s,t,this.get(s,t)]}*values(){for(let s=0;s<this.rows;s++)for(let t=0;t<this.columns;t++)yield this.get(s,t)}}V.prototype.klass="Matrix";typeof Symbol<"u"&&(V.prototype[Symbol.for("nodejs.util.inspect.custom")]=gs);function Re(r,s){return r-s}function Vs(r){return r.every(s=>typeof s=="number")}V.random=V.rand;V.randomInt=V.randInt;V.diagonal=V.diag;V.prototype.diagonal=V.prototype.diag;V.identity=V.eye;V.prototype.negate=V.prototype.neg;V.prototype.tensorProduct=V.prototype.kroneckerProduct;var At,me,bt;let _=(bt=class extends V{constructor(t,e){super();ae(this,At);q(this,"data");if(bt.isMatrix(t))ue(this,At,me).call(this,t.rows,t.columns),bt.copy(t,this);else if(Number.isInteger(t)&&t>=0)ue(this,At,me).call(this,t,e);else if(st.isAnyArray(t)){const n=t;if(t=n.length,e=t?n[0].length:0,typeof e!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let i=0;i<t;i++){if(n[i].length!==e)throw new RangeError("Inconsistent array dimensions");if(!Vs(n[i]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(n[i]))}this.rows=t,this.columns=e}else throw new TypeError("First argument must be a positive number or an array")}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}removeRow(t){return ot(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return e===void 0&&(e=t,t=this.rows),ot(this,t,!0),e=Float64Array.from(St(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){lt(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns-1);for(let i=0;i<t;i++)n[i]=this.data[e][i];for(let i=t+1;i<this.columns;i++)n[i-1]=this.data[e][i];this.data[e]=n}return this.columns-=1,this}addColumn(t,e){typeof e>"u"&&(e=t,t=this.columns),lt(this,t,!0),e=kt(this,e);for(let n=0;n<this.rows;n++){const i=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)i[o]=this.data[n][o];for(i[o++]=e[n];o<this.columns+1;o++)i[o]=this.data[n][o-1];this.data[n]=i}return this.columns+=1,this}},At=new WeakSet,me=function(t,e){if(this.data=[],Number.isInteger(e)&&e>=0)for(let n=0;n<t;n++)this.data.push(new Float64Array(e));else throw new TypeError("nColumns must be a positive integer");this.rows=t,this.columns=e},bt);ds(V,_);var tt;const Kt=class Kt extends V{constructor(t){super();ae(this,tt);if(_.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");Bt(this,tt,_.copy(t,new _(t.rows,t.rows)))}else if(Number.isInteger(t)&&t>=0)Bt(this,tt,new _(t,t));else if(Bt(this,tt,new _(t)),!this.isSymmetric())throw new TypeError("not symmetric data")}get size(){return ht(this,tt).size}get rows(){return ht(this,tt).rows}get columns(){return ht(this,tt).columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return _.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 Kt(this.diagonalSize);for(const[e,n,i]of this.upperRightEntries())t.set(e,n,i);return t}toMatrix(){return new _(this)}get(t,e){return ht(this,tt).get(t,e)}set(t,e,n){return ht(this,tt).set(t,e,n),ht(this,tt).set(e,t,n),this}removeCross(t){return ht(this,tt).removeRow(t),ht(this,tt).removeColumn(t),this}addCross(t,e){e===void 0&&(e=t,t=this.diagonalSize);const n=e.slice();return n.splice(t,1),ht(this,tt).addRow(t,n),ht(this,tt).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,i]of t.entries())i||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,i=0,o=0;o<e.length;o++)e[o]=this.get(i,n),++n>=t&&(n=++i);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 i=new Kt(n);for(let o=0,l=0,h=0;h<e;h++)i.set(o,l,t[h]),++o>=n&&(o=++l);return i}*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)}};tt=new WeakMap;let Pt=Kt;Pt.prototype.klassType="SymmetricMatrix";class se extends Pt{static isDistanceMatrix(s){return Pt.isSymmetricMatrix(s)&&s.klassSubType==="DistanceMatrix"}constructor(s){if(super(s),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(s,t,e){return s===t&&(e=0),super.set(s,t,e)}addCross(s,t){return t===void 0&&(t=s,s=this.diagonalSize),t=t.slice(),t[s]=0,super.addCross(s,t)}toSymmetricMatrix(){return new Pt(this)}clone(){const s=new se(this.diagonalSize);for(const[t,e,n]of this.upperRightEntries())t!==e&&s.set(t,e,n);return s}toCompact(){const{diagonalSize:s}=this,t=(s-1)*s/2,e=new Array(t);for(let n=1,i=0,o=0;o<e.length;o++)e[o]=this.get(i,n),++n>=s&&(n=++i+1);return e}static fromCompact(s){const t=s.length;if(t===0)return new this(0);const e=(Math.sqrt(8*t+1)+1)/2;if(!Number.isInteger(e))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(s)}`);const n=new this(e);for(let i=1,o=0,l=0;l<t;l++)n.set(i,o,s[l]),++i>=e&&(i=++o+1);return n}}se.prototype.klassSubType="DistanceMatrix";class mt extends V{constructor(s,t,e){super(),this.matrix=s,this.rows=t,this.columns=e}}class Ns extends mt{constructor(s,t){lt(s,t),super(s,s.rows,1),this.column=t}set(s,t,e){return this.matrix.set(s,this.column,e),this}get(s){return this.matrix.get(s,this.column)}}class $s extends mt{constructor(s,t){Me(s,t),super(s,s.rows,t.length),this.columnIndices=t}set(s,t,e){return this.matrix.set(s,this.columnIndices[t],e),this}get(s,t){return this.matrix.get(s,this.columnIndices[t])}}class Ds extends mt{constructor(s){super(s,s.rows,s.columns)}set(s,t,e){return this.matrix.set(s,this.columns-t-1,e),this}get(s,t){return this.matrix.get(s,this.columns-t-1)}}class Ls extends mt{constructor(s){super(s,s.rows,s.columns)}set(s,t,e){return this.matrix.set(this.rows-s-1,t,e),this}get(s,t){return this.matrix.get(this.rows-s-1,t)}}class zs extends mt{constructor(s,t){ot(s,t),super(s,1,s.columns),this.row=t}set(s,t,e){return this.matrix.set(this.row,t,e),this}get(s,t){return this.matrix.get(this.row,t)}}class Bs extends mt{constructor(s,t){de(s,t),super(s,t.length,s.columns),this.rowIndices=t}set(s,t,e){return this.matrix.set(this.rowIndices[s],t,e),this}get(s,t){return this.matrix.get(this.rowIndices[s],t)}}class Xt extends mt{constructor(s,t,e){de(s,t),Me(s,e),super(s,t.length,e.length),this.rowIndices=t,this.columnIndices=e}set(s,t,e){return this.matrix.set(this.rowIndices[s],this.columnIndices[t],e),this}get(s,t){return this.matrix.get(this.rowIndices[s],this.columnIndices[t])}}class Os extends mt{constructor(s,t,e,n,i){fe(s,t,e,n,i),super(s,e-t+1,i-n+1),this.startRow=t,this.startColumn=n}set(s,t,e){return this.matrix.set(this.startRow+s,this.startColumn+t,e),this}get(s,t){return this.matrix.get(this.startRow+s,this.startColumn+t)}}class Us extends mt{constructor(s){super(s,s.columns,s.rows)}set(s,t,e){return this.matrix.set(t,s,e),this}get(s,t){return this.matrix.get(t,s)}}class De extends V{constructor(s,t={}){const{rows:e=1}=t;if(s.length%e!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=e,this.columns=s.length/e,this.data=s}set(s,t,e){let n=this._calculateIndex(s,t);return this.data[n]=e,this}get(s,t){let e=this._calculateIndex(s,t);return this.data[e]}_calculateIndex(s,t){return s*this.columns+t}}class rt extends V{constructor(s){super(),this.data=s,this.rows=s.length,this.columns=s[0].length}set(s,t,e){return this.data[s][t]=e,this}get(s,t){return this.data[s][t]}}function Ys(r,s){if(st.isAnyArray(r))return r[0]&&st.isAnyArray(r[0])?new rt(r):new De(r,s);throw new Error("the argument is not an array")}class ne{constructor(s){s=rt.checkMatrix(s);let t=s.clone(),e=t.rows,n=t.columns,i=new Float64Array(e),o=1,l,h,a,u,c,g,d,w,p;for(l=0;l<e;l++)i[l]=l;for(w=new Float64Array(e),h=0;h<n;h++){for(l=0;l<e;l++)w[l]=t.get(l,h);for(l=0;l<e;l++){for(p=Math.min(l,h),c=0,a=0;a<p;a++)c+=t.get(l,a)*w[a];w[l]-=c,t.set(l,h,w[l])}for(u=h,l=h+1;l<e;l++)Math.abs(w[l])>Math.abs(w[u])&&(u=l);if(u!==h){for(a=0;a<n;a++)g=t.get(u,a),t.set(u,a,t.get(h,a)),t.set(h,a,g);d=i[u],i[u]=i[h],i[h]=d,o=-o}if(h<e&&t.get(h,h)!==0)for(l=h+1;l<e;l++)t.set(l,h,t.get(l,h)/t.get(h,h))}this.LU=t,this.pivotVector=i,this.pivotSign=o}isSingular(){let s=this.LU,t=s.columns;for(let e=0;e<t;e++)if(s.get(e,e)===0)return!0;return!1}solve(s){s=_.checkMatrix(s);let t=this.LU;if(t.rows!==s.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let n=s.columns,i=s.subMatrixRow(this.pivotVector,0,n-1),o=t.columns,l,h,a;for(a=0;a<o;a++)for(l=a+1;l<o;l++)for(h=0;h<n;h++)i.set(l,h,i.get(l,h)-i.get(a,h)*t.get(l,a));for(a=o-1;a>=0;a--){for(h=0;h<n;h++)i.set(a,h,i.get(a,h)/t.get(a,a));for(l=0;l<a;l++)for(h=0;h<n;h++)i.set(l,h,i.get(l,h)-i.get(a,h)*t.get(l,a))}return i}get determinant(){let s=this.LU;if(!s.isSquare())throw new Error("Matrix must be square");let t=this.pivotSign,e=s.columns;for(let n=0;n<e;n++)t*=s.get(n,n);return t}get lowerTriangularMatrix(){let s=this.LU,t=s.rows,e=s.columns,n=new _(t,e);for(let i=0;i<t;i++)for(let o=0;o<e;o++)i>o?n.set(i,o,s.get(i,o)):i===o?n.set(i,o,1):n.set(i,o,0);return n}get upperTriangularMatrix(){let s=this.LU,t=s.rows,e=s.columns,n=new _(t,e);for(let i=0;i<t;i++)for(let o=0;o<e;o++)i<=o?n.set(i,o,s.get(i,o)):n.set(i,o,0);return n}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function ct(r,s){let t=0;return Math.abs(r)>Math.abs(s)?(t=s/r,Math.abs(r)*Math.sqrt(1+t*t)):s!==0?(t=r/s,Math.abs(s)*Math.sqrt(1+t*t)):0}class ye{constructor(s){s=rt.checkMatrix(s);let t=s.clone(),e=s.rows,n=s.columns,i=new Float64Array(n),o,l,h,a;for(h=0;h<n;h++){let u=0;for(o=h;o<e;o++)u=ct(u,t.get(o,h));if(u!==0){for(t.get(h,h)<0&&(u=-u),o=h;o<e;o++)t.set(o,h,t.get(o,h)/u);for(t.set(h,h,t.get(h,h)+1),l=h+1;l<n;l++){for(a=0,o=h;o<e;o++)a+=t.get(o,h)*t.get(o,l);for(a=-a/t.get(h,h),o=h;o<e;o++)t.set(o,l,t.get(o,l)+a*t.get(o,h))}}i[h]=-u}this.QR=t,this.Rdiag=i}solve(s){s=_.checkMatrix(s);let t=this.QR,e=t.rows;if(s.rows!==e)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let n=s.columns,i=s.clone(),o=t.columns,l,h,a,u;for(a=0;a<o;a++)for(h=0;h<n;h++){for(u=0,l=a;l<e;l++)u+=t.get(l,a)*i.get(l,h);for(u=-u/t.get(a,a),l=a;l<e;l++)i.set(l,h,i.get(l,h)+u*t.get(l,a))}for(a=o-1;a>=0;a--){for(h=0;h<n;h++)i.set(a,h,i.get(a,h)/this.Rdiag[a]);for(l=0;l<a;l++)for(h=0;h<n;h++)i.set(l,h,i.get(l,h)-i.get(a,h)*t.get(l,a))}return i.subMatrix(0,o-1,0,n-1)}isFullRank(){let s=this.QR.columns;for(let t=0;t<s;t++)if(this.Rdiag[t]===0)return!1;return!0}get upperTriangularMatrix(){let s=this.QR,t=s.columns,e=new _(t,t),n,i;for(n=0;n<t;n++)for(i=0;i<t;i++)n<i?e.set(n,i,s.get(n,i)):n===i?e.set(n,i,this.Rdiag[n]):e.set(n,i,0);return e}get orthogonalMatrix(){let s=this.QR,t=s.rows,e=s.columns,n=new _(t,e),i,o,l,h;for(l=e-1;l>=0;l--){for(i=0;i<t;i++)n.set(i,l,0);for(n.set(l,l,1),o=l;o<e;o++)if(s.get(l,l)!==0){for(h=0,i=l;i<t;i++)h+=s.get(i,l)*n.get(i,o);for(h=-h/s.get(l,l),i=l;i<t;i++)n.set(i,o,n.get(i,o)+h*s.get(i,l))}}return n}}class It{constructor(s,t={}){if(s=rt.checkMatrix(s),s.isEmpty())throw new Error("Matrix must be non-empty");let e=s.rows,n=s.columns;const{computeLeftSingularVectors:i=!0,computeRightSingularVectors:o=!0,autoTranspose:l=!1}=t;let h=!!i,a=!!o,u=!1,c;if(e<n)if(!l)c=s.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else{c=s.transpose(),e=c.rows,n=c.columns,u=!0;let m=h;h=a,a=m}else c=s.clone();let g=Math.min(e,n),d=Math.min(e+1,n),w=new Float64Array(d),p=new _(e,g),P=new _(n,n),f=new Float64Array(n),M=new Float64Array(e),E=new Float64Array(d);for(let m=0;m<d;m++)E[m]=m;let v=Math.min(e-1,n),R=Math.max(0,Math.min(n-2,e)),D=Math.max(v,R);for(let m=0;m<D;m++){if(m<v){w[m]=0;for(let b=m;b<e;b++)w[m]=ct(w[m],c.get(b,m));if(w[m]!==0){c.get(m,m)<0&&(w[m]=-w[m]);for(let b=m;b<e;b++)c.set(b,m,c.get(b,m)/w[m]);c.set(m,m,c.get(m,m)+1)}w[m]=-w[m]}for(let b=m+1;b<n;b++){if(m<v&&w[m]!==0){let j=0;for(let y=m;y<e;y++)j+=c.get(y,m)*c.get(y,b);j=-j/c.get(m,m);for(let y=m;y<e;y++)c.set(y,b,c.get(y,b)+j*c.get(y,m))}f[b]=c.get(m,b)}if(h&&m<v)for(let b=m;b<e;b++)p.set(b,m,c.get(b,m));if(m<R){f[m]=0;for(let b=m+1;b<n;b++)f[m]=ct(f[m],f[b]);if(f[m]!==0){f[m+1]<0&&(f[m]=0-f[m]);for(let b=m+1;b<n;b++)f[b]/=f[m];f[m+1]+=1}if(f[m]=-f[m],m+1<e&&f[m]!==0){for(let b=m+1;b<e;b++)M[b]=0;for(let b=m+1;b<e;b++)for(let j=m+1;j<n;j++)M[b]+=f[j]*c.get(b,j);for(let b=m+1;b<n;b++){let j=-f[b]/f[m+1];for(let y=m+1;y<e;y++)c.set(y,b,c.get(y,b)+j*M[y])}}if(a)for(let b=m+1;b<n;b++)P.set(b,m,f[b])}}let S=Math.min(n,e+1);if(v<n&&(w[v]=c.get(v,v)),e<S&&(w[S-1]=0),R+1<S&&(f[R]=c.get(R,S-1)),f[S-1]=0,h){for(let m=v;m<g;m++){for(let b=0;b<e;b++)p.set(b,m,0);p.set(m,m,1)}for(let m=v-1;m>=0;m--)if(w[m]!==0){for(let b=m+1;b<g;b++){let j=0;for(let y=m;y<e;y++)j+=p.get(y,m)*p.get(y,b);j=-j/p.get(m,m);for(let y=m;y<e;y++)p.set(y,b,p.get(y,b)+j*p.get(y,m))}for(let b=m;b<e;b++)p.set(b,m,-p.get(b,m));p.set(m,m,1+p.get(m,m));for(let b=0;b<m-1;b++)p.set(b,m,0)}else{for(let b=0;b<e;b++)p.set(b,m,0);p.set(m,m,1)}}if(a)for(let m=n-1;m>=0;m--){if(m<R&&f[m]!==0)for(let b=m+1;b<n;b++){let j=0;for(let y=m+1;y<n;y++)j+=P.get(y,m)*P.get(y,b);j=-j/P.get(m+1,m);for(let y=m+1;y<n;y++)P.set(y,b,P.get(y,b)+j*P.get(y,m))}for(let b=0;b<n;b++)P.set(b,m,0);P.set(m,m,1)}let k=S-1,L=Number.EPSILON;for(;S>0;){let m,b;for(m=S-2;m>=-1&&m!==-1;m--){const j=Number.MIN_VALUE+L*Math.abs(w[m]+Math.abs(w[m+1]));if(Math.abs(f[m])<=j||Number.isNaN(f[m])){f[m]=0;break}}if(m===S-2)b=4;else{let j;for(j=S-1;j>=m&&j!==m;j--){let y=(j!==S?Math.abs(f[j]):0)+(j!==m+1?Math.abs(f[j-1]):0);if(Math.abs(w[j])<=L*y){w[j]=0;break}}j===m?b=3:j===S-1?b=1:(b=2,m=j)}switch(m++,b){case 1:{let j=f[S-2];f[S-2]=0;for(let y=S-2;y>=m;y--){let C=ct(w[y],j),F=w[y]/C,I=j/C;if(w[y]=C,y!==m&&(j=-I*f[y-1],f[y-1]=F*f[y-1]),a)for(let U=0;U<n;U++)C=F*P.get(U,y)+I*P.get(U,S-1),P.set(U,S-1,-I*P.get(U,y)+F*P.get(U,S-1)),P.set(U,y,C)}break}case 2:{let j=f[m-1];f[m-1]=0;for(let y=m;y<S;y++){let C=ct(w[y],j),F=w[y]/C,I=j/C;if(w[y]=C,j=-I*f[y],f[y]=F*f[y],h)for(let U=0;U<e;U++)C=F*p.get(U,y)+I*p.get(U,m-1),p.set(U,m-1,-I*p.get(U,y)+F*p.get(U,m-1)),p.set(U,y,C)}break}case 3:{const j=Math.max(Math.abs(w[S-1]),Math.abs(w[S-2]),Math.abs(f[S-2]),Math.abs(w[m]),Math.abs(f[m])),y=w[S-1]/j,C=w[S-2]/j,F=f[S-2]/j,I=w[m]/j,U=f[m]/j,A=((C+y)*(C-y)+F*F)/2,N=y*F*(y*F);let x=0;(A!==0||N!==0)&&(A<0?x=0-Math.sqrt(A*A+N):x=Math.sqrt(A*A+N),x=N/(A+x));let B=(I+y)*(I-y)+x,Y=I*U;for(let T=m;T<S-1;T++){let z=ct(B,Y);z===0&&(z=Number.MIN_VALUE);let X=B/z,Q=Y/z;if(T!==m&&(f[T-1]=z),B=X*w[T]+Q*f[T],f[T]=X*f[T]-Q*w[T],Y=Q*w[T+1],w[T+1]=X*w[T+1],a)for(let W=0;W<n;W++)z=X*P.get(W,T)+Q*P.get(W,T+1),P.set(W,T+1,-Q*P.get(W,T)+X*P.get(W,T+1)),P.set(W,T,z);if(z=ct(B,Y),z===0&&(z=Number.MIN_VALUE),X=B/z,Q=Y/z,w[T]=z,B=X*f[T]+Q*w[T+1],w[T+1]=-Q*f[T]+X*w[T+1],Y=Q*f[T+1],f[T+1]=X*f[T+1],h&&T<e-1)for(let W=0;W<e;W++)z=X*p.get(W,T)+Q*p.get(W,T+1),p.set(W,T+1,-Q*p.get(W,T)+X*p.get(W,T+1)),p.set(W,T,z)}f[S-2]=B;break}case 4:{if(w[m]<=0&&(w[m]=w[m]<0?-w[m]:0,a))for(let j=0;j<=k;j++)P.set(j,m,-P.get(j,m));for(;m<k&&!(w[m]>=w[m+1]);){let j=w[m];if(w[m]=w[m+1],w[m+1]=j,a&&m<n-1)for(let y=0;y<n;y++)j=P.get(y,m+1),P.set(y,m+1,P.get(y,m)),P.set(y,m,j);if(h&&m<e-1)for(let y=0;y<e;y++)j=p.get(y,m+1),p.set(y,m+1,p.get(y,m)),p.set(y,m,j);m++}S--;break}}}if(u){let m=P;P=p,p=m}this.m=e,this.n=n,this.s=w,this.U=p,this.V=P}solve(s){let t=s,e=this.threshold,n=this.s.length,i=_.zeros(n,n);for(let g=0;g<n;g++)Math.abs(this.s[g])<=e?i.set(g,g,0):i.set(g,g,1/this.s[g]);let o=this.U,l=this.rightSingularVectors,h=l.mmul(i),a=l.rows,u=o.rows,c=_.zeros(a,u);for(let g=0;g<a;g++)for(let d=0;d<u;d++){let w=0;for(let p=0;p<n;p++)w+=h.get(g,p)*o.get(d,p);c.set(g,d,w)}return c.mmul(t)}solveForDiagonal(s){return this.solve(_.diag(s))}inverse(){let s=this.V,t=this.threshold,e=s.rows,n=s.columns,i=new _(e,this.s.length);for(let u=0;u<e;u++)for(let c=0;c<n;c++)Math.abs(this.s[c])>t&&i.set(u,c,s.get(u,c)/this.s[c]);let o=this.U,l=o.rows,h=o.columns,a=new _(e,l);for(let u=0;u<e;u++)for(let c=0;c<l;c++){let g=0;for(let d=0;d<h;d++)g+=i.get(u,d)*o.get(c,d);a.set(u,c,g)}return a}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let s=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,t=0,e=this.s;for(let n=0,i=e.length;n<i;n++)e[n]>s&&t++;return t}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 _.diag(this.s)}}function Ws(r,s=!1){return r=rt.checkMatrix(r),s?new It(r).inverse():Le(r,_.eye(r.rows))}function Le(r,s,t=!1){return r=rt.checkMatrix(r),s=rt.checkMatrix(s),t?new It(r).solve(s):r.isSquare()?new ne(r).solve(s):new ye(r).solve(s)}function Zt(r){if(r=_.checkMatrix(r),r.isSquare()){if(r.columns===0)return 1;let s,t,e,n;if(r.columns===2)return s=r.get(0,0),t=r.get(0,1),e=r.get(1,0),n=r.get(1,1),s*n-t*e;if(r.columns===3){let i,o,l;return i=new Xt(r,[1,2],[1,2]),o=new Xt(r,[1,2],[0,2]),l=new Xt(r,[1,2],[0,1]),s=r.get(0,0),t=r.get(0,1),e=r.get(0,2),s*Zt(i)-t*Zt(o)+e*Zt(l)}else return new ne(r).determinant}else throw Error("determinant can only be calculated for a square matrix")}function Gs(r,s){let t=[];for(let e=0;e<r;e++)e!==s&&t.push(e);return t}function Qs(r,s,t,e=1e-9,n=1e-9){if(r>n)return new Array(s.rows+1).fill(0);{let i=s.addRow(t,[0]);for(let o=0;o<i.rows;o++)Math.abs(i.get(o,0))<e&&i.set(o,0,0);return i.to1DArray()}}function Xs(r,s={}){const{thresholdValue:t=1e-9,thresholdError:e=1e-9}=s;r=_.checkMatrix(r);let n=r.rows,i=new _(n,n);for(let o=0;o<n;o++){let l=_.columnVector(r.getRow(o)),h=r.subMatrixRow(Gs(n,o)).transpose(),u=new It(h).solve(l),c=_.sub(l,h.mmul(u)).abs().max();i.setRow(o,Qs(c,u,o,t,e))}return i}function Zs(r,s=Number.EPSILON){if(r=_.checkMatrix(r),r.isEmpty())return r.transpose();let t=new It(r,{autoTranspose:!0}),e=t.leftSingularVectors,n=t.rightSingularVectors,i=t.diagonal;for(let o=0;o<i.length;o++)Math.abs(i[o])>s?i[o]=1/i[o]:i[o]=0;return n.mmul(_.diag(i).mmul(e.transpose()))}function Js(r,s=r,t={}){r=new _(r);let e=!1;if(typeof s=="object"&&!_.isMatrix(s)&&!st.isAnyArray(s)?(t=s,s=r,e=!0):s=new _(s),r.rows!==s.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0}=t;n&&(r=r.center("column"),e||(s=s.center("column")));const i=r.transpose().mmul(s);for(let o=0;o<i.rows;o++)for(let l=0;l<i.columns;l++)i.set(o,l,i.get(o,l)*(1/(r.rows-1)));return i}function Ks(r,s=r,t={}){r=new _(r);let e=!1;if(typeof s=="object"&&!_.isMatrix(s)&&!st.isAnyArray(s)?(t=s,s=r,e=!0):s=new _(s),r.rows!==s.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0,scale:i=!0}=t;n&&(r.center("column"),e||s.center("column")),i&&(r.scale("column"),e||s.scale("column"));const o=r.standardDeviation("column",{unbiased:!0}),l=e?o:s.standardDeviation("column",{unbiased:!0}),h=r.transpose().mmul(s);for(let a=0;a<h.rows;a++)for(let u=0;u<h.columns;u++)h.set(a,u,h.get(a,u)*(1/(o[a]*l[u]))*(1/(r.rows-1)));return h}let ze=class{constructor(s,t={}){const{assumeSymmetric:e=!1}=t;if(s=rt.checkMatrix(s),!s.isSquare())throw new Error("Matrix is not a square matrix");if(s.isEmpty())throw new Error("Matrix must be non-empty");let n=s.columns,i=new _(n,n),o=new Float64Array(n),l=new Float64Array(n),h=s,a,u,c=!1;if(e?c=!0:c=s.isSymmetric(),c){for(a=0;a<n;a++)for(u=0;u<n;u++)i.set(a,u,h.get(a,u));Hs(n,l,o,i),tn(n,l,o,i)}else{let g=new _(n,n),d=new Float64Array(n);for(u=0;u<n;u++)for(a=0;a<n;a++)g.set(a,u,h.get(a,u));en(n,g,d,i),sn(n,l,o,i,g)}this.n=n,this.e=l,this.d=o,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let s=this.n,t=this.e,e=this.d,n=new _(s,s),i,o;for(i=0;i<s;i++){for(o=0;o<s;o++)n.set(i,o,0);n.set(i,i,e[i]),t[i]>0?n.set(i,i+1,t[i]):t[i]<0&&n.set(i,i-1,t[i])}return n}};function Hs(r,s,t,e){let n,i,o,l,h,a,u,c;for(h=0;h<r;h++)t[h]=e.get(r-1,h);for(l=r-1;l>0;l--){for(c=0,o=0,a=0;a<l;a++)c=c+Math.abs(t[a]);if(c===0)for(s[l]=t[l-1],h=0;h<l;h++)t[h]=e.get(l-1,h),e.set(l,h,0),e.set(h,l,0);else{for(a=0;a<l;a++)t[a]/=c,o+=t[a]*t[a];for(n=t[l-1],i=Math.sqrt(o),n>0&&(i=-i),s[l]=c*i,o=o-n*i,t[l-1]=n-i,h=0;h<l;h++)s[h]=0;for(h=0;h<l;h++){for(n=t[h],e.set(h,l,n),i=s[h]+e.get(h,h)*n,a=h+1;a<=l-1;a++)i+=e.get(a,h)*t[a],s[a]+=e.get(a,h)*n;s[h]=i}for(n=0,h=0;h<l;h++)s[h]/=o,n+=s[h]*t[h];for(u=n/(o+o),h=0;h<l;h++)s[h]-=u*t[h];for(h=0;h<l;h++){for(n=t[h],i=s[h],a=h;a<=l-1;a++)e.set(a,h,e.get(a,h)-(n*s[a]+i*t[a]));t[h]=e.get(l-1,h),e.set(l,h,0)}}t[l]=o}for(l=0;l<r-1;l++){if(e.set(r-1,l,e.get(l,l)),e.set(l,l,1),o=t[l+1],o!==0){for(a=0;a<=l;a++)t[a]=e.get(a,l+1)/o;for(h=0;h<=l;h++){for(i=0,a=0;a<=l;a++)i+=e.get(a,l+1)*e.get(a,h);for(a=0;a<=l;a++)e.set(a,h,e.get(a,h)-i*t[a])}}for(a=0;a<=l;a++)e.set(a,l+1,0)}for(h=0;h<r;h++)t[h]=e.get(r-1,h),e.set(r-1,h,0);e.set(r-1,r-1,1),s[0]=0}function tn(r,s,t,e){let n,i,o,l,h,a,u,c,g,d,w,p,P,f,M,E;for(o=1;o<r;o++)s[o-1]=s[o];s[r-1]=0;let v=0,R=0,D=Number.EPSILON;for(a=0;a<r;a++){for(R=Math.max(R,Math.abs(t[a])+Math.abs(s[a])),u=a;u<r&&!(Math.abs(s[u])<=D*R);)u++;if(u>a)do{for(n=t[a],c=(t[a+1]-n)/(2*s[a]),g=ct(c,1),c<0&&(g=-g),t[a]=s[a]/(c+g),t[a+1]=s[a]*(c+g),d=t[a+1],i=n-t[a],o=a+2;o<r;o++)t[o]-=i;for(v=v+i,c=t[u],w=1,p=w,P=w,f=s[a+1],M=0,E=0,o=u-1;o>=a;o--)for(P=p,p=w,E=M,n=w*s[o],i=w*c,g=ct(c,s[o]),s[o+1]=M*g,M=s[o]/g,w=c/g,c=w*t[o]-M*n,t[o+1]=i+M*(w*n+M*t[o]),h=0;h<r;h++)i=e.get(h,o+1),e.set(h,o+1,M*e.get(h,o)+w*i),e.set(h,o,w*e.get(h,o)-M*i);c=-M*E*P*f*s[a]/d,s[a]=M*c,t[a]=w*c}while(Math.abs(s[a])>D*R);t[a]=t[a]+v,s[a]=0}for(o=0;o<r-1;o++){for(h=o,c=t[o],l=o+1;l<r;l++)t[l]<c&&(h=l,c=t[l]);if(h!==o)for(t[h]=t[o],t[o]=c,l=0;l<r;l++)c=e.get(l,o),e.set(l,o,e.get(l,h)),e.set(l,h,c)}}function en(r,s,t,e){let n=0,i=r-1,o,l,h,a,u,c,g;for(c=n+1;c<=i-1;c++){for(g=0,a=c;a<=i;a++)g=g+Math.abs(s.get(a,c-1));if(g!==0){for(h=0,a=i;a>=c;a--)t[a]=s.get(a,c-1)/g,h+=t[a]*t[a];for(l=Math.sqrt(h),t[c]>0&&(l=-l),h=h-t[c]*l,t[c]=t[c]-l,u=c;u<r;u++){for(o=0,a=i;a>=c;a--)o+=t[a]*s.get(a,u);for(o=o/h,a=c;a<=i;a++)s.set(a,u,s.get(a,u)-o*t[a])}for(a=0;a<=i;a++){for(o=0,u=i;u>=c;u--)o+=t[u]*s.get(a,u);for(o=o/h,u=c;u<=i;u++)s.set(a,u,s.get(a,u)-o*t[u])}t[c]=g*t[c],s.set(c,c-1,g*l)}}for(a=0;a<r;a++)for(u=0;u<r;u++)e.set(a,u,a===u?1:0);for(c=i-1;c>=n+1;c--)if(s.get(c,c-1)!==0){for(a=c+1;a<=i;a++)t[a]=s.get(a,c-1);for(u=c;u<=i;u++){for(l=0,a=c;a<=i;a++)l+=t[a]*e.get(a,u);for(l=l/t[c]/s.get(c,c-1),a=c;a<=i;a++)e.set(a,u,e.get(a,u)+l*t[a])}}}function sn(r,s,t,e,n){let i=r-1,o=0,l=r-1,h=Number.EPSILON,a=0,u=0,c=0,g=0,d=0,w=0,p=0,P=0,f,M,E,v,R,D,S,k,L,m,b,j,y,C,F;for(f=0;f<r;f++)for((f<o||f>l)&&(t[f]=n.get(f,f),s[f]=0),M=Math.max(f-1,0);M<r;M++)u=u+Math.abs(n.get(f,M));for(;i>=o;){for(v=i;v>o&&(w=Math.abs(n.get(v-1,v-1))+Math.abs(n.get(v,v)),w===0&&(w=u),!(Math.abs(n.get(v,v-1))<h*w));)v--;if(v===i)n.set(i,i,n.get(i,i)+a),t[i]=n.get(i,i),s[i]=0,i--,P=0;else if(v===i-1){if(S=n.get(i,i-1)*n.get(i-1,i),c=(n.get(i-1,i-1)-n.get(i,i))/2,g=c*c+S,p=Math.sqrt(Math.abs(g)),n.set(i,i,n.get(i,i)+a),n.set(i-1,i-1,n.get(i-1,i-1)+a),k=n.get(i,i),g>=0){for(p=c>=0?c+p:c-p,t[i-1]=k+p,t[i]=t[i-1],p!==0&&(t[i]=k-S/p),s[i-1]=0,s[i]=0,k=n.get(i,i-1),w=Math.abs(k)+Math.abs(p),c=k/w,g=p/w,d=Math.sqrt(c*c+g*g),c=c/d,g=g/d,M=i-1;M<r;M++)p=n.get(i-1,M),n.set(i-1,M,g*p+c*n.get(i,M)),n.set(i,M,g*n.get(i,M)-c*p);for(f=0;f<=i;f++)p=n.get(f,i-1),n.set(f,i-1,g*p+c*n.get(f,i)),n.set(f,i,g*n.get(f,i)-c*p);for(f=o;f<=l;f++)p=e.get(f,i-1),e.set(f,i-1,g*p+c*e.get(f,i)),e.set(f,i,g*e.get(f,i)-c*p)}else t[i-1]=k+c,t[i]=k+c,s[i-1]=p,s[i]=-p;i=i-2,P=0}else{if(k=n.get(i,i),L=0,S=0,v<i&&(L=n.get(i-1,i-1),S=n.get(i,i-1)*n.get(i-1,i)),P===10){for(a+=k,f=o;f<=i;f++)n.set(f,f,n.get(f,f)-k);w=Math.abs(n.get(i,i-1))+Math.abs(n.get(i-1,i-2)),k=L=.75*w,S=-.4375*w*w}if(P===30&&(w=(L-k)/2,w=w*w+S,w>0)){for(w=Math.sqrt(w),L<k&&(w=-w),w=k-S/((L-k)/2+w),f=o;f<=i;f++)n.set(f,f,n.get(f,f)-w);a+=w,k=L=S=.964}for(P=P+1,R=i-2;R>=v&&(p=n.get(R,R),d=k-p,w=L-p,c=(d*w-S)/n.get(R+1,R)+n.get(R,R+1),g=n.get(R+1,R+1)-p-d-w,d=n.get(R+2,R+1),w=Math.abs(c)+Math.abs(g)+Math.abs(d),c=c/w,g=g/w,d=d/w,!(R===v||Math.abs(n.get(R,R-1))*(Math.abs(g)+Math.abs(d))<h*(Math.abs(c)*(Math.abs(n.get(R-1,R-1))+Math.abs(p)+Math.abs(n.get(R+1,R+1))))));)R--;for(f=R+2;f<=i;f++)n.set(f,f-2,0),f>R+2&&n.set(f,f-3,0);for(E=R;E<=i-1&&(C=E!==i-1,E!==R&&(c=n.get(E,E-1),g=n.get(E+1,E-1),d=C?n.get(E+2,E-1):0,k=Math.abs(c)+Math.abs(g)+Math.abs(d),k!==0&&(c=c/k,g=g/k,d=d/k)),k!==0);E++)if(w=Math.sqrt(c*c+g*g+d*d),c<0&&(w=-w),w!==0){for(E!==R?n.set(E,E-1,-w*k):v!==R&&n.set(E,E-1,-n.get(E,E-1)),c=c+w,k=c/w,L=g/w,p=d/w,g=g/c,d=d/c,M=E;M<r;M++)c=n.get(E,M)+g*n.get(E+1,M),C&&(c=c+d*n.get(E+2,M),n.set(E+2,M,n.get(E+2,M)-c*p)),n.set(E,M,n.get(E,M)-c*k),n.set(E+1,M,n.get(E+1,M)-c*L);for(f=0;f<=Math.min(i,E+3);f++)c=k*n.get(f,E)+L*n.get(f,E+1),C&&(c=c+p*n.get(f,E+2),n.set(f,E+2,n.get(f,E+2)-c*d)),n.set(f,E,n.get(f,E)-c),n.set(f,E+1,n.get(f,E+1)-c*g);for(f=o;f<=l;f++)c=k*e.get(f,E)+L*e.get(f,E+1),C&&(c=c+p*e.get(f,E+2),e.set(f,E+2,e.get(f,E+2)-c*d)),e.set(f,E,e.get(f,E)-c),e.set(f,E+1,e.get(f,E+1)-c*g)}}}if(u!==0){for(i=r-1;i>=0;i--)if(c=t[i],g=s[i],g===0)for(v=i,n.set(i,i,1),f=i-1;f>=0;f--){for(S=n.get(f,f)-c,d=0,M=v;M<=i;M++)d=d+n.get(f,M)*n.get(M,i);if(s[f]<0)p=S,w=d;else if(v=f,s[f]===0?n.set(f,i,S!==0?-d/S:-d/(h*u)):(k=n.get(f,f+1),L=n.get(f+1,f),g=(t[f]-c)*(t[f]-c)+s[f]*s[f],D=(k*w-p*d)/g,n.set(f,i,D),n.set(f+1,i,Math.abs(k)>Math.abs(p)?(-d-S*D)/k:(-w-L*D)/p)),D=Math.abs(n.get(f,i)),h*D*D>1)for(M=f;M<=i;M++)n.set(M,i,n.get(M,i)/D)}else if(g<0)for(v=i-1,Math.abs(n.get(i,i-1))>Math.abs(n.get(i-1,i))?(n.set(i-1,i-1,g/n.get(i,i-1)),n.set(i-1,i,-(n.get(i,i)-c)/n.get(i,i-1))):(F=Qt(0,-n.get(i-1,i),n.get(i-1,i-1)-c,g),n.set(i-1,i-1,F[0]),n.set(i-1,i,F[1])),n.set(i,i-1,0),n.set(i,i,1),f=i-2;f>=0;f--){for(m=0,b=0,M=v;M<=i;M++)m=m+n.get(f,M)*n.get(M,i-1),b=b+n.get(f,M)*n.get(M,i);if(S=n.get(f,f)-c,s[f]<0)p=S,d=m,w=b;else if(v=f,s[f]===0?(F=Qt(-m,-b,S,g),n.set(f,i-1,F[0]),n.set(f,i,F[1])):(k=n.get(f,f+1),L=n.get(f+1,f),j=(t[f]-c)*(t[f]-c)+s[f]*s[f]-g*g,y=(t[f]-c)*2*g,j===0&&y===0&&(j=h*u*(Math.abs(S)+Math.abs(g)+Math.abs(k)+Math.abs(L)+Math.abs(p))),F=Qt(k*d-p*m+g*b,k*w-p*b-g*m,j,y),n.set(f,i-1,F[0]),n.set(f,i,F[1]),Math.abs(k)>Math.abs(p)+Math.abs(g)?(n.set(f+1,i-1,(-m-S*n.get(f,i-1)+g*n.get(f,i))/k),n.set(f+1,i,(-b-S*n.get(f,i)-g*n.get(f,i-1))/k)):(F=Qt(-d-L*n.get(f,i-1),-w-L*n.get(f,i),p,g),n.set(f+1,i-1,F[0]),n.set(f+1,i,F[1]))),D=Math.max(Math.abs(n.get(f,i-1)),Math.abs(n.get(f,i))),h*D*D>1)for(M=f;M<=i;M++)n.set(M,i-1,n.get(M,i-1)/D),n.set(M,i,n.get(M,i)/D)}for(f=0;f<r;f++)if(f<o||f>l)for(M=f;M<r;M++)e.set(f,M,n.get(f,M));for(M=r-1;M>=o;M--)for(f=o;f<=l;f++){for(p=0,E=o;E<=Math.min(M,l);E++)p=p+e.get(f,E)*n.get(E,M);e.set(f,M,p)}}}function Qt(r,s,t,e){let n,i;return Math.abs(t)>Math.abs(e)?(n=e/t,i=t+n*e,[(r+n*s)/i,(s-n*r)/i]):(n=t/e,i=e+n*t,[(n*r+s)/i,(n*s-r)/i])}class Be{constructor(s){if(s=rt.checkMatrix(s),!s.isSymmetric())throw new Error("Matrix is not symmetric");let t=s,e=t.rows,n=new _(e,e),i=!0,o,l,h;for(l=0;l<e;l++){let a=0;for(h=0;h<l;h++){let u=0;for(o=0;o<h;o++)u+=n.get(h,o)*n.get(l,o);u=(t.get(l,h)-u)/n.get(h,h),n.set(l,h,u),a=a+u*u}for(a=t.get(l,l)-a,i&&(i=a>0),n.set(l,l,Math.sqrt(Math.max(a,0))),h=l+1;h<e;h++)n.set(l,h,0)}this.L=n,this.positiveDefinite=i}isPositiveDefinite(){return this.positiveDefinite}solve(s){s=rt.checkMatrix(s);let t=this.L,e=t.rows;if(s.rows!==e)throw new Error("Matrix dimensions do not match");if(this.isPositiveDefinite()===!1)throw new Error("Matrix is not positive definite");let n=s.columns,i=s.clone(),o,l,h;for(h=0;h<e;h++)for(l=0;l<n;l++){for(o=0;o<h;o++)i.set(h,l,i.get(h,l)-i.get(o,l)*t.get(h,o));i.set(h,l,i.get(h,l)/t.get(h,h))}for(h=e-1;h>=0;h--)for(l=0;l<n;l++){for(o=h+1;o<e;o++)i.set(h,l,i.get(h,l)-i.get(o,l)*t.get(o,h));i.set(h,l,i.get(h,l)/t.get(h,h))}return i}get lowerTriangularMatrix(){return this.L}}class Oe{constructor(s,t={}){s=rt.checkMatrix(s);let{Y:e}=t;const{scaleScores:n=!1,maxIterations:i=1e3,terminationCriteria:o=1e-10}=t;let l;if(e){if(st.isAnyArray(e)&&typeof e[0]=="number"?e=_.columnVector(e):e=rt.checkMatrix(e),e.rows!==s.rows)throw new Error("Y should have the same number of rows as X");l=e.getColumnVector(0)}else l=s.getColumnVector(0);let h=1,a,u,c,g;for(let d=0;d<i&&h>o;d++)c=s.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0)),c=c.div(c.norm()),a=s.mmul(c).div(c.transpose().mmul(c).get(0,0)),d>0&&(h=a.clone().sub(g).pow(2).sum()),g=a.clone(),e?(u=e.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),u=u.div(u.norm()),l=e.mmul(u).div(u.transpose().mmul(u).get(0,0))):l=a;if(e){let d=s.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0));d=d.div(d.norm());let w=s.clone().sub(a.clone().mmul(d.transpose())),p=l.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),P=e.clone().sub(a.clone().mulS(p.get(0,0)).mmul(u.transpose()));this.t=a,this.p=d.transpose(),this.w=c.transpose(),this.q=u,this.u=l,this.s=a.transpose().mmul(a),this.xResidual=w,this.yResidual=P,this.betas=p}else this.w=c.transpose(),this.s=a.transpose().mmul(a).sqrt(),n?this.t=a.clone().div(this.s.get(0,0)):this.t=a,this.xResidual=s.sub(a.mmul(c.transpose()))}}$.AbstractMatrix=V;$.CHO=Be;$.CholeskyDecomposition=Be;$.DistanceMatrix=se;$.EVD=ze;var nn=$.EigenvalueDecomposition=ze;$.LU=ne;$.LuDecomposition=ne;var Ue=$.Matrix=_;$.MatrixColumnSelectionView=$s;$.MatrixColumnView=Ns;$.MatrixFlipColumnView=Ds;$.MatrixFlipRowView=Ls;$.MatrixRowSelectionView=Bs;$.MatrixRowView=zs;$.MatrixSelectionView=Xt;$.MatrixSubView=Os;$.MatrixTransposeView=Us;$.NIPALS=Oe;$.Nipals=Oe;$.QR=ye;$.QrDecomposition=ye;$.SVD=It;$.SingularValueDecomposition=It;$.SymmetricMatrix=Pt;$.WrapperMatrix1D=De;$.WrapperMatrix2D=rt;$.correlation=Ks;$.covariance=Js;var Te=$.default=_;$.determinant=Zt;$.inverse=Ws;$.linearDependencies=Xs;$.pseudoInverse=Zs;$.solve=Le;$.wrap=Ys;const rn=nn,on=Ue;Te.Matrix&&Te.Matrix;function Ft(r,s,t){const e=r/t,n=s/t;if(!(isFinite(e)&&isFinite(n))){const h=-r/s;return isFinite(h)?[h]:r==0&&s==0?[0]:[]}const i=n*n-4*e;let o=0;if(isFinite(i)){if(i<0)return[];if(i==0)return[-.5*n];o=-.5*(n+Rt(Math.sqrt(i),n))}else o=-n;const l=e/o;return isFinite(l)?l>o?[o,l]:[l,o]:[o]}function Ye(r,s,t,e){const n=t/(3*e),i=s/(3*e),o=r/e;if(!(isFinite(o)&&isFinite(i)&&isFinite(n)))return Ft(r,s,t);const l=-n*n+i,h=-i*n+o,a=n*o-i*i,u=4*l*a-h*h,c=-2*n*l+h;if(u<0){const g=Math.sqrt(-.25*u),d=-.5*c;return[Math.cbrt(d+g)+Math.cbrt(d-g)-n]}else if(u==0){const g=Rt(Math.sqrt(-l),c);return[g-n,-2*g-n]}else{const g=Math.atan2(Math.sqrt(u),-c)/3,d=Math.cos(g),w=Math.sin(g)*Math.sqrt(3),p=.5*(-d+w),P=.5*(-d-w),f=2*Math.sqrt(-l);return[f*d-n,f*p-n,f*P-n]}}function ln(r,s,t,e,n){if(n==0)return Ye(r,s,t,e);const i=e/n,o=t/n,l=s/n,h=r/n;let a=Ie(i,o,l,h,!1);if(a!==null)return a;const u=716e74;for(let c=0;c<2;c++)if(a=Ie(i/u,o/(u*u),l/(u*u*u),h/(u*u*u*u),c!=0),a!==null){for(let g=0;g<a.length;g++)a[g]*=u;return a}return[]}function gt(r,s){return s==0?Math.abs(r):Math.abs((r-s)/s)}function Ie(r,s,t,e,n){const i=hn(r,s,t,e,n);if(i!==null&&i.length==4){let o=[];for(let l=0;l<2;l++){const h=i[l*2],a=i[l*2+1];o=o.concat(Ft(a,h,1))}return o}return null}function hn(r,s,t,e,n){function i(A,N,x,B){const Y=gt(A+x,r),T=gt(N+A*x+B,s),z=gt(N*x+A*B,t);return Y+T+z}function o(A,N,x,B){return i(A,N,x,B)+gt(N*B,e)}const l=9*r*r-24*s,h=l>=0?-2*s/(3*r+Rt(Math.sqrt(l),r)):-.25*r,a=r+4*h,u=s+3*h*(r+2*h),c=t+h*(2*s+h*(3*r+4*h)),g=e+h*(t+h*(s+h*(r+h)));let d=0,w=0;const p=349e100;if(n){const A=a/p,N=u/p,x=c/p,B=g/p;d=A*x-4/p*B-1/3*N*N,w=(A*x-8/p*B-2/9*N*N)*(1/3)*N-x*(x/p)-A*A*B}else d=a*c-4*g-1/3*u*u,w=(a*c+8*g-2/9*u*u)*(1/3)*u-c*c-a*a*g;if(!isFinite(d)&&isFinite(w))return null;let P=an(d,w);n&&(P*=p);const f=r*.5,M=1/6*s+.5*P,E=t-r*M,v=2/3*s-P-f*f,R=.5*E/v,D=2*(e-M*M)/E,S=.5*E/D;let k=0,L=0,m=0;for(let A=0;A<3;A++){const N=A==1?S:v,x=A==0?R:D,B=gt(N+f*f+2*M,s),Y=gt(2*(N*x+f*M),t),T=gt(N*x*x+M*M,e),z=B+Y+T;(A==0||z<m)&&(k=N,L=x,m=z)}const b=k,j=L;let y=0,C=0,F=0,I=0;if(b<0){const A=Math.sqrt(-b);if(y=f+A,C=M+A*j,F=f-A,I=M-A*j,Math.abs(I)<Math.abs(C)?I=e/C:Math.abs(I)>Math.abs(C)&&(C=e/I),Math.abs(y)!=Math.abs(F)){let N=null,x=null;if(Math.abs(y)<Math.abs(F)){const Y=(t-C*F)/I,T=(s-I-C)/F;N=[r-F,Y,T],x=[F,F,F]}else{const Y=(t-y*I)/C,T=(s-I-C)/y,z=r-y;N=[y,y,y],x=[z,Y,T]}let B=0;for(let Y=0;Y<3;Y++){const T=N[Y],z=x[Y];if(isFinite(T)&&isFinite(z)){const X=i(T,C,z,I);(Y==0||X<B)&&(y=T,F=z,B=X)}}}}else if(b==0){const A=e-M*M;y=f,C=M+Math.sqrt(-A),F=f,I=M-Math.sqrt(-A),Math.abs(C)>Math.abs(I)?I=e/C:Math.abs(I)>Math.abs(C)&&(C=e/I)}else return[];let U=o(y,C,F,I);for(let A=0;A<8&&U!=0;A++){const N=C*I-e,x=C*F+y*I-t,B=C+y*F+I-s,Y=y+F-r,T=y-F,z=C*C-C*(F*T+2*I)+I*(y*T+I);if(z==0)break;const X=1/z,Q=I-C,W=C*F-y*I,ie=T*N+Q*x+W*B-(C*Q+y*W)*Y,re=(y*T+Q)*N-C*(T*x+Q*B+W*Y),oe=-T*N-Q*x-W*B+(F*W+I*Q)*Y,le=-(F*T+Q)*N+I*(T*x+Q*B+W*Y),Nt=y-X*ie,$t=C-X*re,Dt=F-X*oe,Lt=I-X*le,zt=o(Nt,$t,Dt,Lt);if(zt<U)y=Nt,C=$t,F=Dt,I=Lt,U=zt;else break}return[y,C,F,I]}function an(r,s){const t=-.3333333333333333*r,e=.5*s;let n,i=null;if((Math.abs(t)>=1e102||Math.abs(e)>=1e164)&&(Math.abs(t)<Math.abs(e)?i=1-t*(t/e)*(t/e):i=Math.sign(t)*(e/t*(e/t)/t-1)),i!==null&&e==0)r>0?n=0:n=Math.sqrt(-r);else if(i!==null?i<0:e*e<t*t*t){const a=i!==null?e/t/Math.sqrt(t):e/Math.sqrt(t*t*t);n=-2*Math.sqrt(t)*Rt(Math.cos(Math.acos(Math.abs(a))*(1/3)),a)}else{let a;i!==null?Math.abs(t)<Math.abs(e)?a=-e*(1+Math.sqrt(i)):a=-e-Rt(Math.sqrt(Math.abs(t))*t*Math.sqrt(i),e):a=Math.cbrt(-e-Rt(Math.sqrt(e*e-t*t*t),e));const u=a==0?0:t/a;n=a+u}let o=n,l=(o*o+r)*o+s;if(Math.abs(l)<222045e-21*Math.max(o*o*o,r*o,s))return o;for(let a=0;a<8;a++){const u=3*o*o+r;if(u==0)break;const c=o-l/u,g=(c*c+r)*c+s;if(g==0)return c;if(Math.abs(g)>=Math.abs(l))break;o=c,l=g}return o}function Rt(r,s){const t=Math.abs(r);return s<0?-t:t}function We(r,s=1e-9){if(r[r.length-1]==0)return We(r.slice(0,-1),s);const t=new on([r.slice(0,-1).reverse().map(n=>-n/r[r.length-1]),...Array.from({length:r.length-2},(n,i)=>{const o=new Array(r.length-1).fill(0);return o[i]=1,o})]),e=new rn(t);return e.realEigenvalues.filter((n,i)=>Math.abs(e.imaginaryEigenvalues[i])<s)}const un=(r,s,t)=>[r[0]*(1-t)+s[0]*t,r[1]*(1-t)+s[1]*t];function cn(r){const s=[];for(let t=0;t<r.length-1;t++)s.push([r[t],r[t+1]]);return s}function Pe(r,s){const t=cn(r).map(([e,n])=>un(e,n,s));return r.length===2?[t]:[...Pe(t,s),t]}class wt extends xt{constructor(t,e,n,i){super(t,e);q(this,"segmentType","CUBIC_BEZIER");q(this,"firstControlPoint");q(this,"lastControlPoint");q(this,"_boundingBox",null);q(this,"_polynomialCoefficients",null);this.firstControlPoint=n,this.lastControlPoint=i}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,i,o]=[this.firstPoint[t],this.firstControlPoint[t],this.lastControlPoint[t],this.lastPoint[t]],l=-e+3*n-3*i+o,h=2*e-4*n+2*i,a=-e+n;return Ft(a,h,l).filter(u=>u>=-this.precision&&u<=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=G(this.lastPoint,this.firstPoint),n=Math.atan2(e[1],e[0]),i=new et;return i.translate(t[0],t[1]),i.rotate(-n),this.transform(i)}getInflexionParameters(){const t=this.alignedCurve,[e,n]=t.firstControlPoint,[i,o]=t.lastControlPoint,[l,h]=t.lastPoint,a=i*n,u=l*n,c=e*o,g=l*o,d=-3*a+2*u+3*c-g,w=3*a-u-3*c,p=c-a;return Ft(p,w,d).filter(P=>P>=0&&P<=1)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=ge([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new wt(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new wt(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)&&O(this.firstControlPoint,t.firstControlPoint)&&O(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const n=xe(i=>Et(this.paramPoint(i),t),this.precision);return n.argMin<-this.precision||n.argMin>1+this.precision?Math.min(J(this.firstPoint,t),J(this.lastPoint,t)):Math.sqrt(n.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,i=t*t,o=3*n,l=6*e*t,h=3*i,a=G(this.firstControlPoint,this.firstPoint),u=G(this.lastControlPoint,this.firstControlPoint),c=G(this.lastPoint,this.lastControlPoint);return[o*a[0]+l*u[0]+h*c[0],o*a[1]+l*u[1]+h*c[1]]}secondDerivativeAt(t){const e=it(this.firstPoint,this.lastControlPoint),n=nt(this.firstControlPoint,2),i=it(this.firstControlPoint,this.lastPoint),o=nt(this.lastControlPoint,2),l=nt(G(e,n),6*(1-t)),h=nt(G(i,o),6*t);return it(l,h)}tangentAt(t){const e=this.pointToParam(t);return K(this.gradientAt(e))}get tangentAtFirstPoint(){return K(G(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return K(G(this.lastPoint,this.lastControlPoint))}normalAt(t){const e=this.tangentAt(t);return H(e)}get normalAtFirstPoint(){return H(this.tangentAtFirstPoint)}get normalAtLastPoint(){return H(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((o,l)=>o-l);let i={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return n.flatMap((o,l)=>{if(o-i.originalParam<this.precision)return[];let h=!1;o>1-this.precision&&(h=!0);const a=(o-i.originalParam)/(1-i.originalParam),u=Pe([i.p0,i.p1,i.p2,this.lastPoint],h?1:a),c=h?this.lastPoint:(e==null?void 0:e.get(o))??u[0][0],g=u[1][0],d=u[2][0],w=u[1][1],p=u[2][2],P=new wt(i.p0,c,d,g);return i={param:a,originalParam:o,p0:c,p1:w,p2:p},l===n.length-1&&!h?[P,new wt(i.p0,this.lastPoint,i.p1,i.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(o=>this.pointToParam(o)),i=new Map(Vt([n,e]));return this.splitAtParameters(n,i)}transform(t){return new wt(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,i=t*t,o=n*e,l=3*n*t,h=3*e*i,a=i*t;return[o*this.firstPoint[0]+l*this.firstControlPoint[0]+h*this.lastControlPoint[0]+a*this.lastPoint[0],o*this.firstPoint[1]+l*this.firstControlPoint[1]+h*this.lastControlPoint[1]+a*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],i=this.firstControlPoint[e],o=this.lastControlPoint[e],l=this.lastPoint[e],h=-n+3*i-3*o+l,a=3*n-6*i+3*o,u=-3*n+3*i;return[n,u,a,h]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,i,o]=this.polynomialCoefficients[1];return Ye(e-t,n,i,o).filter(l=>l>=-this.precision&&l<=1+this.precision)}pointToParam(t){if(O(t,this.firstPoint,this.precision))return 0;if(O(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=o=>Math.abs(this.paramPoint(o)[0]-t[0])<=this.precision,i=e.find(n);if(i===void 0)throw new Error("Point is not on the curve");return i}}class Mt extends xt{constructor(t,e,n,i,o,l,h=!1,{ignoreChecks:a=!1,angleUnits:u="deg"}={}){super(t,e);q(this,"segmentType","ELLIPSE_ARC");q(this,"precision",1e-6);q(this,"majorRadius");q(this,"minorRadius");q(this,"center");q(this,"tiltAngle");q(this,"clockwise");q(this,"_coefficients",null);q(this,"_boundingBox");q(this,"_linearExentricity");q(this,"_exentricity");q(this,"_focals");q(this,"_ellipseReferenceFrameTransform");q(this,"_reverseEllipseReferenceFrameTransform");q(this,"_rotateFromEllipseReferenceFrame");q(this,"_firstAngle");q(this,"_lastAngle");q(this,"_deltaAngle");this.center=n;const c=i>=o;this.majorRadius=c?i:o,this.minorRadius=c?o:i;const g=u==="deg"?l*Ht:l;if(this.tiltAngle=dt(c?g:g+Math.PI/2),this.clockwise=h,!a){if(O(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${Z(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${Z(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,i=this.minorRadius*this.minorRadius,o=Math.cos(this.tiltAngle),l=Math.sin(this.tiltAngle),h=n*l*l+i*o*o,a=2*(i-n)*l*o,u=n*o*o+i*l*l;this._coefficients={x2:h,xy:a,y2:u,x:-2*h*t-a*e,y:-2*u*e-a*t,c:h*t*t+a*t*e+u*e*e-n*i}}return this._coefficients}get info(){return`ELLIPSE_ARC(${Z(this.firstPoint)}, ${Z(this.lastPoint)}, ${Z(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*Ce}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new Mt(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new Mt(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),i=Math.atan(e/t);return[n,Math.PI+n,i,Math.PI+i].map(l=>this.thetaToParam(l)).filter(l=>this.isValidParameter(l)).map(l=>this.paramPoint(l))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=ge(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=[it(this.center,nt(t,this.linearExentricity)),it(this.center,nt(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return vt(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 ${Z(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${Z(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 O(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)&&(O(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||O(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 dt(n)}thetaToParam(t){return Jt(this.firstAngle,dt(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,n]=this.focals,i=J(t,e),o=J(t,n);return Math.abs(2*this.majorRadius-i-o)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(O(t,this.center)?e=it(this.center,nt(H(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(gn(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return J(t,e);if(Je(t,this.majorAxis,this.center)){const n=dt(2*Math.PI-this.pointTheta(t)),i=this.thetaToParam(n);if(this.isValidParameter(i))return J(t,this.paramPoint(i))}return Math.min(J(t,this.firstPoint),J(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new et().translate(-this.center[0],-this.center[1]).rotate(-this.tiltAngle)),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new et().rotate(this.tiltAngle).translate(this.center[0],this.center[1])),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new et().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=Jt(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?H(e):Ct(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),n=-this.majorRadius*this.deltaAngle*Math.sin(e),i=this.minorRadius*this.deltaAngle*Math.cos(e),o=this.clockwise?[-n,-i]:[n,i];return this.rotateFromEllipseReferenceFrame.transform(o)}tangentAt(t){const e=this.pointTheta(t),n=-this.majorRadius*Math.sin(e),i=this.minorRadius*Math.cos(e),o=this.clockwise?[-n,-i]:[n,i];return K(this.rotateFromEllipseReferenceFrame.transform(o))}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 K(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 K(this.rotateFromEllipseReferenceFrame.transform(n))}transform(t){const e=t.transformAngle(this.tiltAngle),n=t.scaleFactor();return new Mt(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 i=[0,1,...e.map(h=>this.pointToParam(h))],o=new Map(Vt([i,[this.firstPoint,this.lastPoint,...e]]));i.sort((h,a)=>h-a);let l=null;return i.flatMap((h,a)=>{if(a===i.length-1)return[];const u=i[a+1];if(u-h<this.precision)return l===null&&(l=h),[];const c=l===null?h:l,g=new Mt(o.get(c)||this.paramPoint(c),o.get(u)||this.paramPoint(u),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return l=null,g})}}function fn(r,s,t,e,n,i,o){const{center:l,rx:h,ry:a}=mn(r,s,t,e,n*Ht,!i,o);return Math.abs(h-a)<1e-9?new ut(r,s,l,o):new Mt(r,s,l,h,a,n,o)}function mn([r,s],[t,e],n,i,o,l,h){if(n<0&&(n=-n),i<0&&(i=-i),n==0||i==0)throw Error("rx and ry can not be 0");const a=Math.sin(o),u=Math.cos(o),c=(r-t)/2,g=(s-e)/2,d=(r+t)/2,w=(s+e)/2,p=u*c+a*g,P=u*g-a*c,f=p*p/(n*n)+P*P/(i*i);f>1&&(n=n*Math.sqrt(f),i=i*Math.sqrt(f));const M=n*i,E=n*P,v=i*p,R=E*E+v*v;if(!R)throw Error("start point can not be same as end point");let D=Math.sqrt(Math.abs((M*M-R)/R));l==h&&(D=-D);const S=D*E/i,k=-D*v/n,L=u*S-a*k+d,m=a*S+u*k+w;return{center:[L,m],rx:n,ry:i}}function gn(r,s,t){const e=Math.abs(t[0]),n=Math.abs(t[1]);let i=.707,o=.707;const l=r,h=s;for(let a=0;a<3;a++){const u=l*i,c=h*o,g=(l*l-h*h)*i**3/l,d=(h*h-l*l)*o**3/h,w=u-g,p=c-d,P=e-g,f=n-d,M=Math.hypot(w,p),E=Math.hypot(P,f);i=Math.min(1,Math.max(0,(P*M/E+g)/l)),o=Math.min(1,Math.max(0,(f*M/E+d)/h));const v=Math.hypot(i,o);i/=v,o/=v}return[l*i*Math.sign(t[0]),h*o*Math.sign(t[1])]}class pt extends xt{constructor(t,e,n){super(t,e);q(this,"segmentType","QUADRATIC_BEZIER");q(this,"controlPoint");q(this,"_boundingBox",null);q(this,"_polynomialCoefficients",null);this.controlPoint=n}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,i]=[this.firstPoint[t],this.controlPoint[t],this.lastPoint[t]],o=e-2*n+i;if(!o)return[];const l=(e-n)/o;return l>=-this.precision&&l<=1+this.precision?[l]:[]}getParametersOfExtrema(){return Array.from(new Set(this._extremaInDirection(0).concat(this._extremaInDirection(1))))}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=ge([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new pt(this.firstPoint,this.lastPoint,this.controlPoint)}reverse(){return new pt(this.lastPoint,this.firstPoint,this.controlPoint)}isSame(t){return t.segmentType!=="QUADRATIC_BEZIER"?!1:O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)&&O(this.controlPoint,t.controlPoint)}distanceFrom(t){const n=xe(i=>Et(this.paramPoint(i),t),this.precision);return n.argMin<-this.precision||n.argMin>1+this.precision?Math.min(J(this.firstPoint,t),J(this.lastPoint,t)):Math.sqrt(n.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 i=this.firstPoint[n],o=this.controlPoint[n],l=this.lastPoint[n];return 2*(t*(i-2*o+l)-i+o)};return[e(0),e(1)]}secondDerivativeAt(t){return nt(G(it(this.firstPoint,this.lastPoint),nt(this.controlPoint,2)),2)}tangentAt(t){const e=this.pointToParam(t);return K(this.gradientAt(e))}get tangentAtFirstPoint(){return K(G(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return K(G(this.lastPoint,this.controlPoint))}normalAt(t){const e=this.tangentAt(t);return H(e)}get normalAtFirstPoint(){return H(this.tangentAtFirstPoint)}get normalAtLastPoint(){return H(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((o,l)=>o-l);let i={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return n.flatMap((o,l)=>{if(o-i.originalParam<this.precision)return[];let h=!1;o>1-this.precision&&(h=!0);const a=(o-i.originalParam)/(1-i.originalParam),u=Pe([i.p0,i.p1,this.lastPoint],h?1:a),c=h?this.lastPoint:(e==null?void 0:e.get(o))??u[0][0],g=u[1][0],d=u[1][1],w=new pt(i.p0,c,g);return i={param:a,originalParam:o,p0:c,p1:d},l===n.length-1&&!h?[w,new pt(i.p0,this.lastPoint,i.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(o=>this.pointToParam(o)),i=new Map(Vt([n,e]));return this.splitAtParameters(n,i)}transform(t){return new pt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const e=1-t,n=e*e,i=2*e*t,o=t*t;return[n*this.firstPoint[0]+i*this.controlPoint[0]+o*this.lastPoint[0],n*this.firstPoint[1]+i*this.controlPoint[1]+o*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],i=this.controlPoint[e],o=this.lastPoint[e],l=n-2*i+o,h=2*(i-n);return[n,h,l]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,i]=this.polynomialCoefficients[1];return Ft(e-t,n,i).filter(o=>o>=-this.precision&&o<=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=o=>Math.abs(this.paramPoint(o)[0]-t[0])<=this.precision,i=e.find(n);if(i===void 0)throw new Error("Point is not on the curve");return i}}exports.Arc=ut;exports.BoundingBox=ft;exports.CubicBezier=wt;exports.DEG2RAD=Ht;exports.EllipseArc=Mt;exports.Line=at;exports.QuadraticBezier=pt;exports.RAD2DEG=Ce;exports.Transformable=Ae;exports.TransformationMatrix=et;exports.add=it;exports.angle=Ze;exports.cartesianToPolar=Fe;exports.crossProduct=yt;exports.distance=J;exports.dotProduct=ce;exports.length=Xe;exports.lineLineIntersection=es;exports.lineLineParams=te;exports.normalize=K;exports.parallel=pe;exports.perpendicular=H;exports.perpendicularClockwise=Ct;exports.polarAngle=qe;exports.polarToCartesian=vt;exports.removeDuplicatePoints=we;exports.reprVector=Z;exports.sameVector=O;exports.scalarMultiply=nt;exports.solveGenericPolynomial=We;exports.solveQuartic=ln;exports.squareDistance=Et;exports.squareLength=Tt;exports.subtract=G;exports.svgEllipse=fn;exports.tangentArc=ns;exports.threePointsArc=ss;exports.zip=Vt;
|
|
9
|
-
//# sourceMappingURL=QuadraticBezier
|
|
8
|
+
${$e}`)}function ps(r,s,t){return(r>=0&&t?` ${ve(r,s-1)}`:ve(r,s)).padEnd(s)}function ve(r,s){let t=r.toString();if(t.length<=s)return t;let e=r.toFixed(s);if(e.length>s&&(e=r.toFixed(Math.max(0,s-(e.length-s)))),e.length<=s&&!e.startsWith("0.000")&&!e.startsWith("-0.000"))return e;let n=r.toExponential(s);return n.length>s&&(n=r.toExponential(Math.max(0,s-(n.length-s)))),n.slice(0)}function ds(r,s){r.prototype.add=function(e){return typeof e=="number"?this.addS(e):this.addM(e)},r.prototype.addS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)+e);return this},r.prototype.addM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)+e.get(n,i));return this},r.add=function(e,n){return new s(e).add(n)},r.prototype.sub=function(e){return typeof e=="number"?this.subS(e):this.subM(e)},r.prototype.subS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)-e);return this},r.prototype.subM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)-e.get(n,i));return this},r.sub=function(e,n){return new s(e).sub(n)},r.prototype.subtract=r.prototype.sub,r.prototype.subtractS=r.prototype.subS,r.prototype.subtractM=r.prototype.subM,r.subtract=r.sub,r.prototype.mul=function(e){return typeof e=="number"?this.mulS(e):this.mulM(e)},r.prototype.mulS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)*e);return this},r.prototype.mulM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)*e.get(n,i));return this},r.mul=function(e,n){return new s(e).mul(n)},r.prototype.multiply=r.prototype.mul,r.prototype.multiplyS=r.prototype.mulS,r.prototype.multiplyM=r.prototype.mulM,r.multiply=r.mul,r.prototype.div=function(e){return typeof e=="number"?this.divS(e):this.divM(e)},r.prototype.divS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)/e);return this},r.prototype.divM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)/e.get(n,i));return this},r.div=function(e,n){return new s(e).div(n)},r.prototype.divide=r.prototype.div,r.prototype.divideS=r.prototype.divS,r.prototype.divideM=r.prototype.divM,r.divide=r.div,r.prototype.mod=function(e){return typeof e=="number"?this.modS(e):this.modM(e)},r.prototype.modS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)%e);return this},r.prototype.modM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)%e.get(n,i));return this},r.mod=function(e,n){return new s(e).mod(n)},r.prototype.modulus=r.prototype.mod,r.prototype.modulusS=r.prototype.modS,r.prototype.modulusM=r.prototype.modM,r.modulus=r.mod,r.prototype.and=function(e){return typeof e=="number"?this.andS(e):this.andM(e)},r.prototype.andS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)&e);return this},r.prototype.andM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)&e.get(n,i));return this},r.and=function(e,n){return new s(e).and(n)},r.prototype.or=function(e){return typeof e=="number"?this.orS(e):this.orM(e)},r.prototype.orS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)|e);return this},r.prototype.orM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)|e.get(n,i));return this},r.or=function(e,n){return new s(e).or(n)},r.prototype.xor=function(e){return typeof e=="number"?this.xorS(e):this.xorM(e)},r.prototype.xorS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)^e);return this},r.prototype.xorM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)^e.get(n,i));return this},r.xor=function(e,n){return new s(e).xor(n)},r.prototype.leftShift=function(e){return typeof e=="number"?this.leftShiftS(e):this.leftShiftM(e)},r.prototype.leftShiftS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)<<e);return this},r.prototype.leftShiftM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)<<e.get(n,i));return this},r.leftShift=function(e,n){return new s(e).leftShift(n)},r.prototype.signPropagatingRightShift=function(e){return typeof e=="number"?this.signPropagatingRightShiftS(e):this.signPropagatingRightShiftM(e)},r.prototype.signPropagatingRightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>e);return this},r.prototype.signPropagatingRightShiftM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>e.get(n,i));return this},r.signPropagatingRightShift=function(e,n){return new s(e).signPropagatingRightShift(n)},r.prototype.rightShift=function(e){return typeof e=="number"?this.rightShiftS(e):this.rightShiftM(e)},r.prototype.rightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>>e);return this},r.prototype.rightShiftM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)>>>e.get(n,i));return this},r.rightShift=function(e,n){return new s(e).rightShift(n)},r.prototype.zeroFillRightShift=r.prototype.rightShift,r.prototype.zeroFillRightShiftS=r.prototype.rightShiftS,r.prototype.zeroFillRightShiftM=r.prototype.rightShiftM,r.zeroFillRightShift=r.rightShift,r.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},r.not=function(e){return new s(e).not()},r.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},r.abs=function(e){return new s(e).abs()},r.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},r.acos=function(e){return new s(e).acos()},r.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},r.acosh=function(e){return new s(e).acosh()},r.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},r.asin=function(e){return new s(e).asin()},r.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},r.asinh=function(e){return new s(e).asinh()},r.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},r.atan=function(e){return new s(e).atan()},r.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},r.atanh=function(e){return new s(e).atanh()},r.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},r.cbrt=function(e){return new s(e).cbrt()},r.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},r.ceil=function(e){return new s(e).ceil()},r.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},r.clz32=function(e){return new s(e).clz32()},r.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},r.cos=function(e){return new s(e).cos()},r.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},r.cosh=function(e){return new s(e).cosh()},r.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},r.exp=function(e){return new s(e).exp()},r.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},r.expm1=function(e){return new s(e).expm1()},r.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},r.floor=function(e){return new s(e).floor()},r.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},r.fround=function(e){return new s(e).fround()},r.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},r.log=function(e){return new s(e).log()},r.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},r.log1p=function(e){return new s(e).log1p()},r.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},r.log10=function(e){return new s(e).log10()},r.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},r.log2=function(e){return new s(e).log2()},r.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},r.round=function(e){return new s(e).round()},r.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},r.sign=function(e){return new s(e).sign()},r.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},r.sin=function(e){return new s(e).sin()},r.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},r.sinh=function(e){return new s(e).sinh()},r.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},r.sqrt=function(e){return new s(e).sqrt()},r.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},r.tan=function(e){return new s(e).tan()},r.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},r.tanh=function(e){return new s(e).tanh()},r.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},r.trunc=function(e){return new s(e).trunc()},r.pow=function(e,n){return new s(e).pow(n)},r.prototype.pow=function(e){return typeof e=="number"?this.powS(e):this.powM(e)},r.prototype.powS=function(e){for(let n=0;n<this.rows;n++)for(let i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)**e);return this},r.prototype.powM=function(e){if(e=s.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 i=0;i<this.columns;i++)this.set(n,i,this.get(n,i)**e.get(n,i));return this}}function ot(r,s,t){let e=t?r.rows:r.rows-1;if(s<0||s>e)throw new RangeError("Row index out of range")}function lt(r,s,t){let e=t?r.columns:r.columns-1;if(s<0||s>e)throw new RangeError("Column index out of range")}function St(r,s){if(s.to1DArray&&(s=s.to1DArray()),s.length!==r.columns)throw new RangeError("vector size must be the same as the number of columns");return s}function kt(r,s){if(s.to1DArray&&(s=s.to1DArray()),s.length!==r.rows)throw new RangeError("vector size must be the same as the number of rows");return s}function de(r,s){if(!st.isAnyArray(s))throw new TypeError("row indices must be an array");for(let t=0;t<s.length;t++)if(s[t]<0||s[t]>=r.rows)throw new RangeError("row indices are out of range")}function Me(r,s){if(!st.isAnyArray(s))throw new TypeError("column indices must be an array");for(let t=0;t<s.length;t++)if(s[t]<0||s[t]>=r.columns)throw new RangeError("column indices are out of range")}function fe(r,s,t,e,n){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Gt("startRow",s),Gt("endRow",t),Gt("startColumn",e),Gt("endColumn",n),s>t||e>n||s<0||s>=r.rows||t<0||t>=r.rows||e<0||e>=r.columns||n<0||n>=r.columns)throw new RangeError("Submatrix indices are out of range")}function ee(r,s=0){let t=[];for(let e=0;e<r;e++)t.push(s);return t}function Gt(r,s){if(typeof s!="number")throw new TypeError(`${r} must be a number`)}function jt(r){if(r.isEmpty())throw new Error("Empty matrix has no elements to index")}function Ms(r){let s=ee(r.rows);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[t]+=r.get(t,e);return s}function ys(r){let s=ee(r.columns);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[e]+=r.get(t,e);return s}function Ps(r){let s=0;for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)s+=r.get(t,e);return s}function bs(r){let s=ee(r.rows,1);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[t]*=r.get(t,e);return s}function Es(r){let s=ee(r.columns,1);for(let t=0;t<r.rows;++t)for(let e=0;e<r.columns;++e)s[e]*=r.get(t,e);return s}function _s(r){let s=1;for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)s*=r.get(t,e);return s}function js(r,s,t){const e=r.rows,n=r.columns,i=[];for(let o=0;o<e;o++){let l=0,h=0,a=0;for(let u=0;u<n;u++)a=r.get(o,u)-t[o],l+=a,h+=a*a;s?i.push((h-l*l/n)/(n-1)):i.push((h-l*l/n)/n)}return i}function Ss(r,s,t){const e=r.rows,n=r.columns,i=[];for(let o=0;o<n;o++){let l=0,h=0,a=0;for(let u=0;u<e;u++)a=r.get(u,o)-t[o],l+=a,h+=a*a;s?i.push((h-l*l/e)/(e-1)):i.push((h-l*l/e)/e)}return i}function ks(r,s,t){const e=r.rows,n=r.columns,i=e*n;let o=0,l=0,h=0;for(let a=0;a<e;a++)for(let u=0;u<n;u++)h=r.get(a,u)-t,o+=h,l+=h*h;return s?(l-o*o/i)/(i-1):(l-o*o/i)/i}function vs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)-s[t])}function Rs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)-s[e])}function Ts(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)-s)}function Is(r){const s=[];for(let t=0;t<r.rows;t++){let e=0;for(let n=0;n<r.columns;n++)e+=r.get(t,n)**2/(r.columns-1);s.push(Math.sqrt(e))}return s}function Cs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)/s[t])}function qs(r){const s=[];for(let t=0;t<r.columns;t++){let e=0;for(let n=0;n<r.rows;n++)e+=r.get(n,t)**2/(r.rows-1);s.push(Math.sqrt(e))}return s}function Fs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)/s[e])}function As(r){const s=r.size-1;let t=0;for(let e=0;e<r.columns;e++)for(let n=0;n<r.rows;n++)t+=r.get(n,e)**2/s;return Math.sqrt(t)}function xs(r,s){for(let t=0;t<r.rows;t++)for(let e=0;e<r.columns;e++)r.set(t,e,r.get(t,e)/s)}class V{static from1DArray(s,t,e){if(s*t!==e.length)throw new RangeError("data length does not match given dimensions");let i=new _(s,t);for(let o=0;o<s;o++)for(let l=0;l<t;l++)i.set(o,l,e[o*t+l]);return i}static rowVector(s){let t=new _(1,s.length);for(let e=0;e<s.length;e++)t.set(0,e,s[e]);return t}static columnVector(s){let t=new _(s.length,1);for(let e=0;e<s.length;e++)t.set(e,0,s[e]);return t}static zeros(s,t){return new _(s,t)}static ones(s,t){return new _(s,t).fill(1)}static rand(s,t,e={}){if(typeof e!="object")throw new TypeError("options must be an object");const{random:n=Math.random}=e;let i=new _(s,t);for(let o=0;o<s;o++)for(let l=0;l<t;l++)i.set(o,l,n());return i}static randInt(s,t,e={}){if(typeof e!="object")throw new TypeError("options must be an object");const{min:n=0,max:i=1e3,random:o=Math.random}=e;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(i))throw new TypeError("max must be an integer");if(n>=i)throw new RangeError("min must be smaller than max");let l=i-n,h=new _(s,t);for(let a=0;a<s;a++)for(let u=0;u<t;u++){let c=n+Math.round(o()*l);h.set(a,u,c)}return h}static eye(s,t,e){t===void 0&&(t=s),e===void 0&&(e=1);let n=Math.min(s,t),i=this.zeros(s,t);for(let o=0;o<n;o++)i.set(o,o,e);return i}static diag(s,t,e){let n=s.length;t===void 0&&(t=n),e===void 0&&(e=t);let i=Math.min(n,t,e),o=this.zeros(t,e);for(let l=0;l<i;l++)o.set(l,l,s[l]);return o}static min(s,t){s=this.checkMatrix(s),t=this.checkMatrix(t);let e=s.rows,n=s.columns,i=new _(e,n);for(let o=0;o<e;o++)for(let l=0;l<n;l++)i.set(o,l,Math.min(s.get(o,l),t.get(o,l)));return i}static max(s,t){s=this.checkMatrix(s),t=this.checkMatrix(t);let e=s.rows,n=s.columns,i=new this(e,n);for(let o=0;o<e;o++)for(let l=0;l<n;l++)i.set(o,l,Math.max(s.get(o,l),t.get(o,l)));return i}static checkMatrix(s){return V.isMatrix(s)?s:new _(s)}static isMatrix(s){return s!=null&&s.klass==="Matrix"}get size(){return this.rows*this.columns}apply(s){if(typeof s!="function")throw new TypeError("callback must be a function");for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s.call(this,t,e);return this}to1DArray(){let s=[];for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s.push(this.get(t,e));return s}to2DArray(){let s=[];for(let t=0;t<this.rows;t++){s.push([]);for(let e=0;e<this.columns;e++)s[t].push(this.get(t,e))}return s}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 s=0;s<this.rows;s++)for(let t=0;t<=s;t++)if(this.get(s,t)!==this.get(t,s))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let s=0;s<this.rows;s++)if(this.get(s,s)!==0)return!1;return!0}isEchelonForm(){let s=0,t=0,e=-1,n=!0,i=!1;for(;s<this.rows&&n;){for(t=0,i=!1;t<this.columns&&i===!1;)this.get(s,t)===0?t++:this.get(s,t)===1&&t>e?(i=!0,e=t):(n=!1,i=!0);s++}return n}isReducedEchelonForm(){let s=0,t=0,e=-1,n=!0,i=!1;for(;s<this.rows&&n;){for(t=0,i=!1;t<this.columns&&i===!1;)this.get(s,t)===0?t++:this.get(s,t)===1&&t>e?(i=!0,e=t):(n=!1,i=!0);for(let o=t+1;o<this.rows;o++)this.get(s,o)!==0&&(n=!1);s++}return n}echelonForm(){let s=this.clone(),t=0,e=0;for(;t<s.rows&&e<s.columns;){let n=t;for(let i=t;i<s.rows;i++)s.get(i,e)>s.get(n,e)&&(n=i);if(s.get(n,e)===0)e++;else{s.swapRows(t,n);let i=s.get(t,e);for(let o=e;o<s.columns;o++)s.set(t,o,s.get(t,o)/i);for(let o=t+1;o<s.rows;o++){let l=s.get(o,e)/s.get(t,e);s.set(o,e,0);for(let h=e+1;h<s.columns;h++)s.set(o,h,s.get(o,h)-s.get(t,h)*l)}t++,e++}}return s}reducedEchelonForm(){let s=this.echelonForm(),t=s.columns,e=s.rows,n=e-1;for(;n>=0;)if(s.maxRow(n)===0)n--;else{let i=0,o=!1;for(;i<e&&o===!1;)s.get(n,i)===1?o=!0:i++;for(let l=0;l<n;l++){let h=s.get(l,i);for(let a=i;a<t;a++){let u=s.get(l,a)-h*s.get(n,a);s.set(l,a,u)}}n--}return s}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{rows:t=1,columns:e=1}=s;if(!Number.isInteger(t)||t<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(e)||e<=0)throw new TypeError("columns must be a positive integer");let n=new _(this.rows*t,this.columns*e);for(let i=0;i<t;i++)for(let o=0;o<e;o++)n.setSubMatrix(this,this.rows*i,this.columns*o);return n}fill(s){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,s);return this}neg(){return this.mulS(-1)}getRow(s){ot(this,s);let t=[];for(let e=0;e<this.columns;e++)t.push(this.get(s,e));return t}getRowVector(s){return _.rowVector(this.getRow(s))}setRow(s,t){ot(this,s),t=St(this,t);for(let e=0;e<this.columns;e++)this.set(s,e,t[e]);return this}swapRows(s,t){ot(this,s),ot(this,t);for(let e=0;e<this.columns;e++){let n=this.get(s,e);this.set(s,e,this.get(t,e)),this.set(t,e,n)}return this}getColumn(s){lt(this,s);let t=[];for(let e=0;e<this.rows;e++)t.push(this.get(e,s));return t}getColumnVector(s){return _.columnVector(this.getColumn(s))}setColumn(s,t){lt(this,s),t=kt(this,t);for(let e=0;e<this.rows;e++)this.set(e,s,t[e]);return this}swapColumns(s,t){lt(this,s),lt(this,t);for(let e=0;e<this.rows;e++){let n=this.get(e,s);this.set(e,s,this.get(e,t)),this.set(e,t,n)}return this}addRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)+s[e]);return this}subRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)-s[e]);return this}mulRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)*s[e]);return this}divRowVector(s){s=St(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)/s[e]);return this}addColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)+s[t]);return this}subColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)-s[t]);return this}mulColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)*s[t]);return this}divColumnVector(s){s=kt(this,s);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)/s[t]);return this}mulRow(s,t){ot(this,s);for(let e=0;e<this.columns;e++)this.set(s,e,this.get(s,e)*t);return this}mulColumn(s,t){lt(this,s);for(let e=0;e<this.rows;e++)this.set(e,s,this.get(e,s)*t);return this}max(s){if(this.isEmpty())return NaN;switch(s){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${s}`)}}maxIndex(){jt(this);let s=this.get(0,0),t=[0,0];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>s&&(s=this.get(e,n),t[0]=e,t[1]=n);return t}min(s){if(this.isEmpty())return NaN;switch(s){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${s}`)}}minIndex(){jt(this);let s=this.get(0,0),t=[0,0];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<s&&(s=this.get(e,n),t[0]=e,t[1]=n);return t}maxRow(s){if(ot(this,s),this.isEmpty())return NaN;let t=this.get(s,0);for(let e=1;e<this.columns;e++)this.get(s,e)>t&&(t=this.get(s,e));return t}maxRowIndex(s){ot(this,s),jt(this);let t=this.get(s,0),e=[s,0];for(let n=1;n<this.columns;n++)this.get(s,n)>t&&(t=this.get(s,n),e[1]=n);return e}minRow(s){if(ot(this,s),this.isEmpty())return NaN;let t=this.get(s,0);for(let e=1;e<this.columns;e++)this.get(s,e)<t&&(t=this.get(s,e));return t}minRowIndex(s){ot(this,s),jt(this);let t=this.get(s,0),e=[s,0];for(let n=1;n<this.columns;n++)this.get(s,n)<t&&(t=this.get(s,n),e[1]=n);return e}maxColumn(s){if(lt(this,s),this.isEmpty())return NaN;let t=this.get(0,s);for(let e=1;e<this.rows;e++)this.get(e,s)>t&&(t=this.get(e,s));return t}maxColumnIndex(s){lt(this,s),jt(this);let t=this.get(0,s),e=[0,s];for(let n=1;n<this.rows;n++)this.get(n,s)>t&&(t=this.get(n,s),e[0]=n);return e}minColumn(s){if(lt(this,s),this.isEmpty())return NaN;let t=this.get(0,s);for(let e=1;e<this.rows;e++)this.get(e,s)<t&&(t=this.get(e,s));return t}minColumnIndex(s){lt(this,s),jt(this);let t=this.get(0,s),e=[0,s];for(let n=1;n<this.rows;n++)this.get(n,s)<t&&(t=this.get(n,s),e[0]=n);return e}diag(){let s=Math.min(this.rows,this.columns),t=[];for(let e=0;e<s;e++)t.push(this.get(e,e));return t}norm(s="frobenius"){switch(s){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${s}`)}}cumulativeSum(){let s=0;for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s+=this.get(t,e),this.set(t,e,s);return this}dot(s){V.isMatrix(s)&&(s=s.to1DArray());let t=this.to1DArray();if(t.length!==s.length)throw new RangeError("vectors do not have the same size");let e=0;for(let n=0;n<t.length;n++)e+=t[n]*s[n];return e}mmul(s){s=_.checkMatrix(s);let t=this.rows,e=this.columns,n=s.columns,i=new _(t,n),o=new Float64Array(e);for(let l=0;l<n;l++){for(let h=0;h<e;h++)o[h]=s.get(h,l);for(let h=0;h<t;h++){let a=0;for(let u=0;u<e;u++)a+=this.get(h,u)*o[u];i.set(h,l,a)}}return i}mpow(s){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(s)||s<0)throw new RangeError("Exponent must be a non-negative integer");let t=_.eye(this.rows),e=this;for(let n=s;n>1;n/=2)n&1&&(t=t.mmul(e)),e=e.mmul(e);return t}strassen2x2(s){s=_.checkMatrix(s);let t=new _(2,2);const e=this.get(0,0),n=s.get(0,0),i=this.get(0,1),o=s.get(0,1),l=this.get(1,0),h=s.get(1,0),a=this.get(1,1),u=s.get(1,1),c=(e+a)*(n+u),g=(l+a)*n,d=e*(o-u),w=a*(h-n),p=(e+i)*u,P=(l-e)*(n+o),f=(i-a)*(h+u),M=c+w-p+f,E=d+p,v=g+w,R=c-g+d+P;return t.set(0,0,M),t.set(0,1,E),t.set(1,0,v),t.set(1,1,R),t}strassen3x3(s){s=_.checkMatrix(s);let t=new _(3,3);const e=this.get(0,0),n=this.get(0,1),i=this.get(0,2),o=this.get(1,0),l=this.get(1,1),h=this.get(1,2),a=this.get(2,0),u=this.get(2,1),c=this.get(2,2),g=s.get(0,0),d=s.get(0,1),w=s.get(0,2),p=s.get(1,0),P=s.get(1,1),f=s.get(1,2),M=s.get(2,0),E=s.get(2,1),v=s.get(2,2),R=(e+n+i-o-l-u-c)*P,D=(e-o)*(-d+P),S=l*(-g+d+p-P-f-M+v),k=(-e+o+l)*(g-d+P),L=(o+l)*(-g+d),m=e*g,b=(-e+a+u)*(g-w+f),j=(-e+a)*(w-f),y=(a+u)*(-g+w),C=(e+n+i-l-h-a-u)*f,F=u*(-g+w+p-P-f-M+E),I=(-i+u+c)*(P+M-E),U=(i-c)*(P-E),A=i*M,N=(u+c)*(-M+E),x=(-i+l+h)*(f+M-v),B=(i-h)*(f-v),Y=(l+h)*(-M+v),T=n*p,z=h*E,X=o*w,Q=a*d,W=c*v,ie=m+A+T,re=R+k+L+m+I+A+N,oe=m+b+y+C+A+x+Y,le=D+S+k+m+A+x+B,Nt=D+k+L+m+z,$t=A+x+B+Y+X,Dt=m+b+j+F+I+U+A,Lt=I+U+A+N+Q,zt=m+b+j+y+W;return t.set(0,0,ie),t.set(0,1,re),t.set(0,2,oe),t.set(1,0,le),t.set(1,1,Nt),t.set(1,2,$t),t.set(2,0,Dt),t.set(2,1,Lt),t.set(2,2,zt),t}mmulStrassen(s){s=_.checkMatrix(s);let t=this.clone(),e=t.rows,n=t.columns,i=s.rows,o=s.columns;n!==i&&console.warn(`Multiplying ${e} x ${n} and ${i} x ${o} matrix: dimensions do not match.`);function l(c,g,d){let w=c.rows,p=c.columns;if(w===g&&p===d)return c;{let P=V.zeros(g,d);return P=P.setSubMatrix(c,0,0),P}}let h=Math.max(e,i),a=Math.max(n,o);t=l(t,h,a),s=l(s,h,a);function u(c,g,d,w){if(d<=512||w<=512)return c.mmul(g);d%2===1&&w%2===1?(c=l(c,d+1,w+1),g=l(g,d+1,w+1)):d%2===1?(c=l(c,d+1,w),g=l(g,d+1,w)):w%2===1&&(c=l(c,d,w+1),g=l(g,d,w+1));let p=parseInt(c.rows/2,10),P=parseInt(c.columns/2,10),f=c.subMatrix(0,p-1,0,P-1),M=g.subMatrix(0,p-1,0,P-1),E=c.subMatrix(0,p-1,P,c.columns-1),v=g.subMatrix(0,p-1,P,g.columns-1),R=c.subMatrix(p,c.rows-1,0,P-1),D=g.subMatrix(p,g.rows-1,0,P-1),S=c.subMatrix(p,c.rows-1,P,c.columns-1),k=g.subMatrix(p,g.rows-1,P,g.columns-1),L=u(V.add(f,S),V.add(M,k),p,P),m=u(V.add(R,S),M,p,P),b=u(f,V.sub(v,k),p,P),j=u(S,V.sub(D,M),p,P),y=u(V.add(f,E),k,p,P),C=u(V.sub(R,f),V.add(M,v),p,P),F=u(V.sub(E,S),V.add(D,k),p,P),I=V.add(L,j);I.sub(y),I.add(F);let U=V.add(b,y),A=V.add(m,j),N=V.sub(L,m);N.add(b),N.add(C);let x=V.zeros(2*I.rows,2*I.columns);return x=x.setSubMatrix(I,0,0),x=x.setSubMatrix(U,I.rows,0),x=x.setSubMatrix(A,0,I.columns),x=x.setSubMatrix(N,I.rows,I.columns),x.subMatrix(0,d-1,0,w-1)}return u(t,s,h,a)}scaleRows(s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{min:t=0,max:e=1}=s;if(!Number.isFinite(t))throw new TypeError("min must be a number");if(!Number.isFinite(e))throw new TypeError("max must be a number");if(t>=e)throw new RangeError("min must be smaller than max");let n=new _(this.rows,this.columns);for(let i=0;i<this.rows;i++){const o=this.getRow(i);o.length>0&&ke(o,{min:t,max:e,output:o}),n.setRow(i,o)}return n}scaleColumns(s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{min:t=0,max:e=1}=s;if(!Number.isFinite(t))throw new TypeError("min must be a number");if(!Number.isFinite(e))throw new TypeError("max must be a number");if(t>=e)throw new RangeError("min must be smaller than max");let n=new _(this.rows,this.columns);for(let i=0;i<this.columns;i++){const o=this.getColumn(i);o.length&&ke(o,{min:t,max:e,output:o}),n.setColumn(i,o)}return n}flipRows(){const s=Math.ceil(this.columns/2);for(let t=0;t<this.rows;t++)for(let e=0;e<s;e++){let n=this.get(t,e),i=this.get(t,this.columns-1-e);this.set(t,e,i),this.set(t,this.columns-1-e,n)}return this}flipColumns(){const s=Math.ceil(this.rows/2);for(let t=0;t<this.columns;t++)for(let e=0;e<s;e++){let n=this.get(e,t),i=this.get(this.rows-1-e,t);this.set(e,t,i),this.set(this.rows-1-e,t,n)}return this}kroneckerProduct(s){s=_.checkMatrix(s);let t=this.rows,e=this.columns,n=s.rows,i=s.columns,o=new _(t*n,e*i);for(let l=0;l<t;l++)for(let h=0;h<e;h++)for(let a=0;a<n;a++)for(let u=0;u<i;u++)o.set(n*l+a,i*h+u,this.get(l,h)*s.get(a,u));return o}kroneckerSum(s){if(s=_.checkMatrix(s),!this.isSquare()||!s.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let t=this.rows,e=s.rows,n=this.kroneckerProduct(_.eye(e,e)),i=_.eye(t,t).kroneckerProduct(s);return n.add(i)}transpose(){let s=new _(this.columns,this.rows);for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)s.set(e,t,this.get(t,e));return s}sortRows(s=Re){for(let t=0;t<this.rows;t++)this.setRow(t,this.getRow(t).sort(s));return this}sortColumns(s=Re){for(let t=0;t<this.columns;t++)this.setColumn(t,this.getColumn(t).sort(s));return this}subMatrix(s,t,e,n){fe(this,s,t,e,n);let i=new _(t-s+1,n-e+1);for(let o=s;o<=t;o++)for(let l=e;l<=n;l++)i.set(o-s,l-e,this.get(o,l));return i}subMatrixRow(s,t,e){if(t===void 0&&(t=0),e===void 0&&(e=this.columns-1),t>e||t<0||t>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");let n=new _(s.length,e-t+1);for(let i=0;i<s.length;i++)for(let o=t;o<=e;o++){if(s[i]<0||s[i]>=this.rows)throw new RangeError(`Row index out of range: ${s[i]}`);n.set(i,o-t,this.get(s[i],o))}return n}subMatrixColumn(s,t,e){if(t===void 0&&(t=0),e===void 0&&(e=this.rows-1),t>e||t<0||t>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");let n=new _(e-t+1,s.length);for(let i=0;i<s.length;i++)for(let o=t;o<=e;o++){if(s[i]<0||s[i]>=this.columns)throw new RangeError(`Column index out of range: ${s[i]}`);n.set(o-t,i,this.get(o,s[i]))}return n}setSubMatrix(s,t,e){if(s=_.checkMatrix(s),s.isEmpty())return this;let n=t+s.rows-1,i=e+s.columns-1;fe(this,t,n,e,i);for(let o=0;o<s.rows;o++)for(let l=0;l<s.columns;l++)this.set(t+o,e+l,s.get(o,l));return this}selection(s,t){de(this,s),Me(this,t);let e=new _(s.length,t.length);for(let n=0;n<s.length;n++){let i=s[n];for(let o=0;o<t.length;o++){let l=t[o];e.set(n,o,this.get(i,l))}}return e}trace(){let s=Math.min(this.rows,this.columns),t=0;for(let e=0;e<s;e++)t+=this.get(e,e);return t}clone(){return this.constructor.copy(this,new _(this.rows,this.columns))}static copy(s,t){for(const[e,n,i]of s.entries())t.set(e,n,i);return t}sum(s){switch(s){case"row":return Ms(this);case"column":return ys(this);case void 0:return Ps(this);default:throw new Error(`invalid option: ${s}`)}}product(s){switch(s){case"row":return bs(this);case"column":return Es(this);case void 0:return _s(this);default:throw new Error(`invalid option: ${s}`)}}mean(s){const t=this.sum(s);switch(s){case"row":{for(let e=0;e<this.rows;e++)t[e]/=this.columns;return t}case"column":{for(let e=0;e<this.columns;e++)t[e]/=this.rows;return t}case void 0:return t/this.size;default:throw new Error(`invalid option: ${s}`)}}variance(s,t={}){if(typeof s=="object"&&(t=s,s=void 0),typeof t!="object")throw new TypeError("options must be an object");const{unbiased:e=!0,mean:n=this.mean(s)}=t;if(typeof e!="boolean")throw new TypeError("unbiased must be a boolean");switch(s){case"row":{if(!st.isAnyArray(n))throw new TypeError("mean must be an array");return js(this,e,n)}case"column":{if(!st.isAnyArray(n))throw new TypeError("mean must be an array");return Ss(this,e,n)}case void 0:{if(typeof n!="number")throw new TypeError("mean must be a number");return ks(this,e,n)}default:throw new Error(`invalid option: ${s}`)}}standardDeviation(s,t){typeof s=="object"&&(t=s,s=void 0);const e=this.variance(s,t);if(s===void 0)return Math.sqrt(e);for(let n=0;n<e.length;n++)e[n]=Math.sqrt(e[n]);return e}center(s,t={}){if(typeof s=="object"&&(t=s,s=void 0),typeof t!="object")throw new TypeError("options must be an object");const{center:e=this.mean(s)}=t;switch(s){case"row":{if(!st.isAnyArray(e))throw new TypeError("center must be an array");return vs(this,e),this}case"column":{if(!st.isAnyArray(e))throw new TypeError("center must be an array");return Rs(this,e),this}case void 0:{if(typeof e!="number")throw new TypeError("center must be a number");return Ts(this,e),this}default:throw new Error(`invalid option: ${s}`)}}scale(s,t={}){if(typeof s=="object"&&(t=s,s=void 0),typeof t!="object")throw new TypeError("options must be an object");let e=t.scale;switch(s){case"row":{if(e===void 0)e=Is(this);else if(!st.isAnyArray(e))throw new TypeError("scale must be an array");return Cs(this,e),this}case"column":{if(e===void 0)e=qs(this);else if(!st.isAnyArray(e))throw new TypeError("scale must be an array");return Fs(this,e),this}case void 0:{if(e===void 0)e=As(this);else if(typeof e!="number")throw new TypeError("scale must be a number");return xs(this,e),this}default:throw new Error(`invalid option: ${s}`)}}toString(s){return De(this,s)}[Symbol.iterator](){return this.entries()}*entries(){for(let s=0;s<this.rows;s++)for(let t=0;t<this.columns;t++)yield[s,t,this.get(s,t)]}*values(){for(let s=0;s<this.rows;s++)for(let t=0;t<this.columns;t++)yield this.get(s,t)}}V.prototype.klass="Matrix";typeof Symbol<"u"&&(V.prototype[Symbol.for("nodejs.util.inspect.custom")]=gs);function Re(r,s){return r-s}function Vs(r){return r.every(s=>typeof s=="number")}V.random=V.rand;V.randomInt=V.randInt;V.diagonal=V.diag;V.prototype.diagonal=V.prototype.diag;V.identity=V.eye;V.prototype.negate=V.prototype.neg;V.prototype.tensorProduct=V.prototype.kroneckerProduct;var At,me,bt;let _=(bt=class extends V{constructor(t,e){super();ae(this,At);q(this,"data");if(bt.isMatrix(t))ue(this,At,me).call(this,t.rows,t.columns),bt.copy(t,this);else if(Number.isInteger(t)&&t>=0)ue(this,At,me).call(this,t,e);else if(st.isAnyArray(t)){const n=t;if(t=n.length,e=t?n[0].length:0,typeof e!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let i=0;i<t;i++){if(n[i].length!==e)throw new RangeError("Inconsistent array dimensions");if(!Vs(n[i]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(n[i]))}this.rows=t,this.columns=e}else throw new TypeError("First argument must be a positive number or an array")}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}removeRow(t){return ot(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return e===void 0&&(e=t,t=this.rows),ot(this,t,!0),e=Float64Array.from(St(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){lt(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns-1);for(let i=0;i<t;i++)n[i]=this.data[e][i];for(let i=t+1;i<this.columns;i++)n[i-1]=this.data[e][i];this.data[e]=n}return this.columns-=1,this}addColumn(t,e){typeof e>"u"&&(e=t,t=this.columns),lt(this,t,!0),e=kt(this,e);for(let n=0;n<this.rows;n++){const i=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)i[o]=this.data[n][o];for(i[o++]=e[n];o<this.columns+1;o++)i[o]=this.data[n][o-1];this.data[n]=i}return this.columns+=1,this}},At=new WeakSet,me=function(t,e){if(this.data=[],Number.isInteger(e)&&e>=0)for(let n=0;n<t;n++)this.data.push(new Float64Array(e));else throw new TypeError("nColumns must be a positive integer");this.rows=t,this.columns=e},bt);ds(V,_);var tt;const Kt=class Kt extends V{constructor(t){super();ae(this,tt);if(_.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");Bt(this,tt,_.copy(t,new _(t.rows,t.rows)))}else if(Number.isInteger(t)&&t>=0)Bt(this,tt,new _(t,t));else if(Bt(this,tt,new _(t)),!this.isSymmetric())throw new TypeError("not symmetric data")}get size(){return ht(this,tt).size}get rows(){return ht(this,tt).rows}get columns(){return ht(this,tt).columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return _.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 Kt(this.diagonalSize);for(const[e,n,i]of this.upperRightEntries())t.set(e,n,i);return t}toMatrix(){return new _(this)}get(t,e){return ht(this,tt).get(t,e)}set(t,e,n){return ht(this,tt).set(t,e,n),ht(this,tt).set(e,t,n),this}removeCross(t){return ht(this,tt).removeRow(t),ht(this,tt).removeColumn(t),this}addCross(t,e){e===void 0&&(e=t,t=this.diagonalSize);const n=e.slice();return n.splice(t,1),ht(this,tt).addRow(t,n),ht(this,tt).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,i]of t.entries())i||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,i=0,o=0;o<e.length;o++)e[o]=this.get(i,n),++n>=t&&(n=++i);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 i=new Kt(n);for(let o=0,l=0,h=0;h<e;h++)i.set(o,l,t[h]),++o>=n&&(o=++l);return i}*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)}};tt=new WeakMap;let Pt=Kt;Pt.prototype.klassType="SymmetricMatrix";class se extends Pt{static isDistanceMatrix(s){return Pt.isSymmetricMatrix(s)&&s.klassSubType==="DistanceMatrix"}constructor(s){if(super(s),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(s,t,e){return s===t&&(e=0),super.set(s,t,e)}addCross(s,t){return t===void 0&&(t=s,s=this.diagonalSize),t=t.slice(),t[s]=0,super.addCross(s,t)}toSymmetricMatrix(){return new Pt(this)}clone(){const s=new se(this.diagonalSize);for(const[t,e,n]of this.upperRightEntries())t!==e&&s.set(t,e,n);return s}toCompact(){const{diagonalSize:s}=this,t=(s-1)*s/2,e=new Array(t);for(let n=1,i=0,o=0;o<e.length;o++)e[o]=this.get(i,n),++n>=s&&(n=++i+1);return e}static fromCompact(s){const t=s.length;if(t===0)return new this(0);const e=(Math.sqrt(8*t+1)+1)/2;if(!Number.isInteger(e))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(s)}`);const n=new this(e);for(let i=1,o=0,l=0;l<t;l++)n.set(i,o,s[l]),++i>=e&&(i=++o+1);return n}}se.prototype.klassSubType="DistanceMatrix";class mt extends V{constructor(s,t,e){super(),this.matrix=s,this.rows=t,this.columns=e}}class Ns extends mt{constructor(s,t){lt(s,t),super(s,s.rows,1),this.column=t}set(s,t,e){return this.matrix.set(s,this.column,e),this}get(s){return this.matrix.get(s,this.column)}}class $s extends mt{constructor(s,t){Me(s,t),super(s,s.rows,t.length),this.columnIndices=t}set(s,t,e){return this.matrix.set(s,this.columnIndices[t],e),this}get(s,t){return this.matrix.get(s,this.columnIndices[t])}}class Ds extends mt{constructor(s){super(s,s.rows,s.columns)}set(s,t,e){return this.matrix.set(s,this.columns-t-1,e),this}get(s,t){return this.matrix.get(s,this.columns-t-1)}}class Ls extends mt{constructor(s){super(s,s.rows,s.columns)}set(s,t,e){return this.matrix.set(this.rows-s-1,t,e),this}get(s,t){return this.matrix.get(this.rows-s-1,t)}}class zs extends mt{constructor(s,t){ot(s,t),super(s,1,s.columns),this.row=t}set(s,t,e){return this.matrix.set(this.row,t,e),this}get(s,t){return this.matrix.get(this.row,t)}}class Bs extends mt{constructor(s,t){de(s,t),super(s,t.length,s.columns),this.rowIndices=t}set(s,t,e){return this.matrix.set(this.rowIndices[s],t,e),this}get(s,t){return this.matrix.get(this.rowIndices[s],t)}}class Xt extends mt{constructor(s,t,e){de(s,t),Me(s,e),super(s,t.length,e.length),this.rowIndices=t,this.columnIndices=e}set(s,t,e){return this.matrix.set(this.rowIndices[s],this.columnIndices[t],e),this}get(s,t){return this.matrix.get(this.rowIndices[s],this.columnIndices[t])}}class Os extends mt{constructor(s,t,e,n,i){fe(s,t,e,n,i),super(s,e-t+1,i-n+1),this.startRow=t,this.startColumn=n}set(s,t,e){return this.matrix.set(this.startRow+s,this.startColumn+t,e),this}get(s,t){return this.matrix.get(this.startRow+s,this.startColumn+t)}}class Us extends mt{constructor(s){super(s,s.columns,s.rows)}set(s,t,e){return this.matrix.set(t,s,e),this}get(s,t){return this.matrix.get(t,s)}}class Le extends V{constructor(s,t={}){const{rows:e=1}=t;if(s.length%e!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=e,this.columns=s.length/e,this.data=s}set(s,t,e){let n=this._calculateIndex(s,t);return this.data[n]=e,this}get(s,t){let e=this._calculateIndex(s,t);return this.data[e]}_calculateIndex(s,t){return s*this.columns+t}}class rt extends V{constructor(s){super(),this.data=s,this.rows=s.length,this.columns=s[0].length}set(s,t,e){return this.data[s][t]=e,this}get(s,t){return this.data[s][t]}}function Ys(r,s){if(st.isAnyArray(r))return r[0]&&st.isAnyArray(r[0])?new rt(r):new Le(r,s);throw new Error("the argument is not an array")}class ne{constructor(s){s=rt.checkMatrix(s);let t=s.clone(),e=t.rows,n=t.columns,i=new Float64Array(e),o=1,l,h,a,u,c,g,d,w,p;for(l=0;l<e;l++)i[l]=l;for(w=new Float64Array(e),h=0;h<n;h++){for(l=0;l<e;l++)w[l]=t.get(l,h);for(l=0;l<e;l++){for(p=Math.min(l,h),c=0,a=0;a<p;a++)c+=t.get(l,a)*w[a];w[l]-=c,t.set(l,h,w[l])}for(u=h,l=h+1;l<e;l++)Math.abs(w[l])>Math.abs(w[u])&&(u=l);if(u!==h){for(a=0;a<n;a++)g=t.get(u,a),t.set(u,a,t.get(h,a)),t.set(h,a,g);d=i[u],i[u]=i[h],i[h]=d,o=-o}if(h<e&&t.get(h,h)!==0)for(l=h+1;l<e;l++)t.set(l,h,t.get(l,h)/t.get(h,h))}this.LU=t,this.pivotVector=i,this.pivotSign=o}isSingular(){let s=this.LU,t=s.columns;for(let e=0;e<t;e++)if(s.get(e,e)===0)return!0;return!1}solve(s){s=_.checkMatrix(s);let t=this.LU;if(t.rows!==s.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let n=s.columns,i=s.subMatrixRow(this.pivotVector,0,n-1),o=t.columns,l,h,a;for(a=0;a<o;a++)for(l=a+1;l<o;l++)for(h=0;h<n;h++)i.set(l,h,i.get(l,h)-i.get(a,h)*t.get(l,a));for(a=o-1;a>=0;a--){for(h=0;h<n;h++)i.set(a,h,i.get(a,h)/t.get(a,a));for(l=0;l<a;l++)for(h=0;h<n;h++)i.set(l,h,i.get(l,h)-i.get(a,h)*t.get(l,a))}return i}get determinant(){let s=this.LU;if(!s.isSquare())throw new Error("Matrix must be square");let t=this.pivotSign,e=s.columns;for(let n=0;n<e;n++)t*=s.get(n,n);return t}get lowerTriangularMatrix(){let s=this.LU,t=s.rows,e=s.columns,n=new _(t,e);for(let i=0;i<t;i++)for(let o=0;o<e;o++)i>o?n.set(i,o,s.get(i,o)):i===o?n.set(i,o,1):n.set(i,o,0);return n}get upperTriangularMatrix(){let s=this.LU,t=s.rows,e=s.columns,n=new _(t,e);for(let i=0;i<t;i++)for(let o=0;o<e;o++)i<=o?n.set(i,o,s.get(i,o)):n.set(i,o,0);return n}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function ct(r,s){let t=0;return Math.abs(r)>Math.abs(s)?(t=s/r,Math.abs(r)*Math.sqrt(1+t*t)):s!==0?(t=r/s,Math.abs(s)*Math.sqrt(1+t*t)):0}class ye{constructor(s){s=rt.checkMatrix(s);let t=s.clone(),e=s.rows,n=s.columns,i=new Float64Array(n),o,l,h,a;for(h=0;h<n;h++){let u=0;for(o=h;o<e;o++)u=ct(u,t.get(o,h));if(u!==0){for(t.get(h,h)<0&&(u=-u),o=h;o<e;o++)t.set(o,h,t.get(o,h)/u);for(t.set(h,h,t.get(h,h)+1),l=h+1;l<n;l++){for(a=0,o=h;o<e;o++)a+=t.get(o,h)*t.get(o,l);for(a=-a/t.get(h,h),o=h;o<e;o++)t.set(o,l,t.get(o,l)+a*t.get(o,h))}}i[h]=-u}this.QR=t,this.Rdiag=i}solve(s){s=_.checkMatrix(s);let t=this.QR,e=t.rows;if(s.rows!==e)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let n=s.columns,i=s.clone(),o=t.columns,l,h,a,u;for(a=0;a<o;a++)for(h=0;h<n;h++){for(u=0,l=a;l<e;l++)u+=t.get(l,a)*i.get(l,h);for(u=-u/t.get(a,a),l=a;l<e;l++)i.set(l,h,i.get(l,h)+u*t.get(l,a))}for(a=o-1;a>=0;a--){for(h=0;h<n;h++)i.set(a,h,i.get(a,h)/this.Rdiag[a]);for(l=0;l<a;l++)for(h=0;h<n;h++)i.set(l,h,i.get(l,h)-i.get(a,h)*t.get(l,a))}return i.subMatrix(0,o-1,0,n-1)}isFullRank(){let s=this.QR.columns;for(let t=0;t<s;t++)if(this.Rdiag[t]===0)return!1;return!0}get upperTriangularMatrix(){let s=this.QR,t=s.columns,e=new _(t,t),n,i;for(n=0;n<t;n++)for(i=0;i<t;i++)n<i?e.set(n,i,s.get(n,i)):n===i?e.set(n,i,this.Rdiag[n]):e.set(n,i,0);return e}get orthogonalMatrix(){let s=this.QR,t=s.rows,e=s.columns,n=new _(t,e),i,o,l,h;for(l=e-1;l>=0;l--){for(i=0;i<t;i++)n.set(i,l,0);for(n.set(l,l,1),o=l;o<e;o++)if(s.get(l,l)!==0){for(h=0,i=l;i<t;i++)h+=s.get(i,l)*n.get(i,o);for(h=-h/s.get(l,l),i=l;i<t;i++)n.set(i,o,n.get(i,o)+h*s.get(i,l))}}return n}}class It{constructor(s,t={}){if(s=rt.checkMatrix(s),s.isEmpty())throw new Error("Matrix must be non-empty");let e=s.rows,n=s.columns;const{computeLeftSingularVectors:i=!0,computeRightSingularVectors:o=!0,autoTranspose:l=!1}=t;let h=!!i,a=!!o,u=!1,c;if(e<n)if(!l)c=s.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else{c=s.transpose(),e=c.rows,n=c.columns,u=!0;let m=h;h=a,a=m}else c=s.clone();let g=Math.min(e,n),d=Math.min(e+1,n),w=new Float64Array(d),p=new _(e,g),P=new _(n,n),f=new Float64Array(n),M=new Float64Array(e),E=new Float64Array(d);for(let m=0;m<d;m++)E[m]=m;let v=Math.min(e-1,n),R=Math.max(0,Math.min(n-2,e)),D=Math.max(v,R);for(let m=0;m<D;m++){if(m<v){w[m]=0;for(let b=m;b<e;b++)w[m]=ct(w[m],c.get(b,m));if(w[m]!==0){c.get(m,m)<0&&(w[m]=-w[m]);for(let b=m;b<e;b++)c.set(b,m,c.get(b,m)/w[m]);c.set(m,m,c.get(m,m)+1)}w[m]=-w[m]}for(let b=m+1;b<n;b++){if(m<v&&w[m]!==0){let j=0;for(let y=m;y<e;y++)j+=c.get(y,m)*c.get(y,b);j=-j/c.get(m,m);for(let y=m;y<e;y++)c.set(y,b,c.get(y,b)+j*c.get(y,m))}f[b]=c.get(m,b)}if(h&&m<v)for(let b=m;b<e;b++)p.set(b,m,c.get(b,m));if(m<R){f[m]=0;for(let b=m+1;b<n;b++)f[m]=ct(f[m],f[b]);if(f[m]!==0){f[m+1]<0&&(f[m]=0-f[m]);for(let b=m+1;b<n;b++)f[b]/=f[m];f[m+1]+=1}if(f[m]=-f[m],m+1<e&&f[m]!==0){for(let b=m+1;b<e;b++)M[b]=0;for(let b=m+1;b<e;b++)for(let j=m+1;j<n;j++)M[b]+=f[j]*c.get(b,j);for(let b=m+1;b<n;b++){let j=-f[b]/f[m+1];for(let y=m+1;y<e;y++)c.set(y,b,c.get(y,b)+j*M[y])}}if(a)for(let b=m+1;b<n;b++)P.set(b,m,f[b])}}let S=Math.min(n,e+1);if(v<n&&(w[v]=c.get(v,v)),e<S&&(w[S-1]=0),R+1<S&&(f[R]=c.get(R,S-1)),f[S-1]=0,h){for(let m=v;m<g;m++){for(let b=0;b<e;b++)p.set(b,m,0);p.set(m,m,1)}for(let m=v-1;m>=0;m--)if(w[m]!==0){for(let b=m+1;b<g;b++){let j=0;for(let y=m;y<e;y++)j+=p.get(y,m)*p.get(y,b);j=-j/p.get(m,m);for(let y=m;y<e;y++)p.set(y,b,p.get(y,b)+j*p.get(y,m))}for(let b=m;b<e;b++)p.set(b,m,-p.get(b,m));p.set(m,m,1+p.get(m,m));for(let b=0;b<m-1;b++)p.set(b,m,0)}else{for(let b=0;b<e;b++)p.set(b,m,0);p.set(m,m,1)}}if(a)for(let m=n-1;m>=0;m--){if(m<R&&f[m]!==0)for(let b=m+1;b<n;b++){let j=0;for(let y=m+1;y<n;y++)j+=P.get(y,m)*P.get(y,b);j=-j/P.get(m+1,m);for(let y=m+1;y<n;y++)P.set(y,b,P.get(y,b)+j*P.get(y,m))}for(let b=0;b<n;b++)P.set(b,m,0);P.set(m,m,1)}let k=S-1,L=Number.EPSILON;for(;S>0;){let m,b;for(m=S-2;m>=-1&&m!==-1;m--){const j=Number.MIN_VALUE+L*Math.abs(w[m]+Math.abs(w[m+1]));if(Math.abs(f[m])<=j||Number.isNaN(f[m])){f[m]=0;break}}if(m===S-2)b=4;else{let j;for(j=S-1;j>=m&&j!==m;j--){let y=(j!==S?Math.abs(f[j]):0)+(j!==m+1?Math.abs(f[j-1]):0);if(Math.abs(w[j])<=L*y){w[j]=0;break}}j===m?b=3:j===S-1?b=1:(b=2,m=j)}switch(m++,b){case 1:{let j=f[S-2];f[S-2]=0;for(let y=S-2;y>=m;y--){let C=ct(w[y],j),F=w[y]/C,I=j/C;if(w[y]=C,y!==m&&(j=-I*f[y-1],f[y-1]=F*f[y-1]),a)for(let U=0;U<n;U++)C=F*P.get(U,y)+I*P.get(U,S-1),P.set(U,S-1,-I*P.get(U,y)+F*P.get(U,S-1)),P.set(U,y,C)}break}case 2:{let j=f[m-1];f[m-1]=0;for(let y=m;y<S;y++){let C=ct(w[y],j),F=w[y]/C,I=j/C;if(w[y]=C,j=-I*f[y],f[y]=F*f[y],h)for(let U=0;U<e;U++)C=F*p.get(U,y)+I*p.get(U,m-1),p.set(U,m-1,-I*p.get(U,y)+F*p.get(U,m-1)),p.set(U,y,C)}break}case 3:{const j=Math.max(Math.abs(w[S-1]),Math.abs(w[S-2]),Math.abs(f[S-2]),Math.abs(w[m]),Math.abs(f[m])),y=w[S-1]/j,C=w[S-2]/j,F=f[S-2]/j,I=w[m]/j,U=f[m]/j,A=((C+y)*(C-y)+F*F)/2,N=y*F*(y*F);let x=0;(A!==0||N!==0)&&(A<0?x=0-Math.sqrt(A*A+N):x=Math.sqrt(A*A+N),x=N/(A+x));let B=(I+y)*(I-y)+x,Y=I*U;for(let T=m;T<S-1;T++){let z=ct(B,Y);z===0&&(z=Number.MIN_VALUE);let X=B/z,Q=Y/z;if(T!==m&&(f[T-1]=z),B=X*w[T]+Q*f[T],f[T]=X*f[T]-Q*w[T],Y=Q*w[T+1],w[T+1]=X*w[T+1],a)for(let W=0;W<n;W++)z=X*P.get(W,T)+Q*P.get(W,T+1),P.set(W,T+1,-Q*P.get(W,T)+X*P.get(W,T+1)),P.set(W,T,z);if(z=ct(B,Y),z===0&&(z=Number.MIN_VALUE),X=B/z,Q=Y/z,w[T]=z,B=X*f[T]+Q*w[T+1],w[T+1]=-Q*f[T]+X*w[T+1],Y=Q*f[T+1],f[T+1]=X*f[T+1],h&&T<e-1)for(let W=0;W<e;W++)z=X*p.get(W,T)+Q*p.get(W,T+1),p.set(W,T+1,-Q*p.get(W,T)+X*p.get(W,T+1)),p.set(W,T,z)}f[S-2]=B;break}case 4:{if(w[m]<=0&&(w[m]=w[m]<0?-w[m]:0,a))for(let j=0;j<=k;j++)P.set(j,m,-P.get(j,m));for(;m<k&&!(w[m]>=w[m+1]);){let j=w[m];if(w[m]=w[m+1],w[m+1]=j,a&&m<n-1)for(let y=0;y<n;y++)j=P.get(y,m+1),P.set(y,m+1,P.get(y,m)),P.set(y,m,j);if(h&&m<e-1)for(let y=0;y<e;y++)j=p.get(y,m+1),p.set(y,m+1,p.get(y,m)),p.set(y,m,j);m++}S--;break}}}if(u){let m=P;P=p,p=m}this.m=e,this.n=n,this.s=w,this.U=p,this.V=P}solve(s){let t=s,e=this.threshold,n=this.s.length,i=_.zeros(n,n);for(let g=0;g<n;g++)Math.abs(this.s[g])<=e?i.set(g,g,0):i.set(g,g,1/this.s[g]);let o=this.U,l=this.rightSingularVectors,h=l.mmul(i),a=l.rows,u=o.rows,c=_.zeros(a,u);for(let g=0;g<a;g++)for(let d=0;d<u;d++){let w=0;for(let p=0;p<n;p++)w+=h.get(g,p)*o.get(d,p);c.set(g,d,w)}return c.mmul(t)}solveForDiagonal(s){return this.solve(_.diag(s))}inverse(){let s=this.V,t=this.threshold,e=s.rows,n=s.columns,i=new _(e,this.s.length);for(let u=0;u<e;u++)for(let c=0;c<n;c++)Math.abs(this.s[c])>t&&i.set(u,c,s.get(u,c)/this.s[c]);let o=this.U,l=o.rows,h=o.columns,a=new _(e,l);for(let u=0;u<e;u++)for(let c=0;c<l;c++){let g=0;for(let d=0;d<h;d++)g+=i.get(u,d)*o.get(c,d);a.set(u,c,g)}return a}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let s=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,t=0,e=this.s;for(let n=0,i=e.length;n<i;n++)e[n]>s&&t++;return t}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 _.diag(this.s)}}function Ws(r,s=!1){return r=rt.checkMatrix(r),s?new It(r).inverse():ze(r,_.eye(r.rows))}function ze(r,s,t=!1){return r=rt.checkMatrix(r),s=rt.checkMatrix(s),t?new It(r).solve(s):r.isSquare()?new ne(r).solve(s):new ye(r).solve(s)}function Zt(r){if(r=_.checkMatrix(r),r.isSquare()){if(r.columns===0)return 1;let s,t,e,n;if(r.columns===2)return s=r.get(0,0),t=r.get(0,1),e=r.get(1,0),n=r.get(1,1),s*n-t*e;if(r.columns===3){let i,o,l;return i=new Xt(r,[1,2],[1,2]),o=new Xt(r,[1,2],[0,2]),l=new Xt(r,[1,2],[0,1]),s=r.get(0,0),t=r.get(0,1),e=r.get(0,2),s*Zt(i)-t*Zt(o)+e*Zt(l)}else return new ne(r).determinant}else throw Error("determinant can only be calculated for a square matrix")}function Gs(r,s){let t=[];for(let e=0;e<r;e++)e!==s&&t.push(e);return t}function Qs(r,s,t,e=1e-9,n=1e-9){if(r>n)return new Array(s.rows+1).fill(0);{let i=s.addRow(t,[0]);for(let o=0;o<i.rows;o++)Math.abs(i.get(o,0))<e&&i.set(o,0,0);return i.to1DArray()}}function Xs(r,s={}){const{thresholdValue:t=1e-9,thresholdError:e=1e-9}=s;r=_.checkMatrix(r);let n=r.rows,i=new _(n,n);for(let o=0;o<n;o++){let l=_.columnVector(r.getRow(o)),h=r.subMatrixRow(Gs(n,o)).transpose(),u=new It(h).solve(l),c=_.sub(l,h.mmul(u)).abs().max();i.setRow(o,Qs(c,u,o,t,e))}return i}function Zs(r,s=Number.EPSILON){if(r=_.checkMatrix(r),r.isEmpty())return r.transpose();let t=new It(r,{autoTranspose:!0}),e=t.leftSingularVectors,n=t.rightSingularVectors,i=t.diagonal;for(let o=0;o<i.length;o++)Math.abs(i[o])>s?i[o]=1/i[o]:i[o]=0;return n.mmul(_.diag(i).mmul(e.transpose()))}function Js(r,s=r,t={}){r=new _(r);let e=!1;if(typeof s=="object"&&!_.isMatrix(s)&&!st.isAnyArray(s)?(t=s,s=r,e=!0):s=new _(s),r.rows!==s.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0}=t;n&&(r=r.center("column"),e||(s=s.center("column")));const i=r.transpose().mmul(s);for(let o=0;o<i.rows;o++)for(let l=0;l<i.columns;l++)i.set(o,l,i.get(o,l)*(1/(r.rows-1)));return i}function Ks(r,s=r,t={}){r=new _(r);let e=!1;if(typeof s=="object"&&!_.isMatrix(s)&&!st.isAnyArray(s)?(t=s,s=r,e=!0):s=new _(s),r.rows!==s.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0,scale:i=!0}=t;n&&(r.center("column"),e||s.center("column")),i&&(r.scale("column"),e||s.scale("column"));const o=r.standardDeviation("column",{unbiased:!0}),l=e?o:s.standardDeviation("column",{unbiased:!0}),h=r.transpose().mmul(s);for(let a=0;a<h.rows;a++)for(let u=0;u<h.columns;u++)h.set(a,u,h.get(a,u)*(1/(o[a]*l[u]))*(1/(r.rows-1)));return h}let Be=class{constructor(s,t={}){const{assumeSymmetric:e=!1}=t;if(s=rt.checkMatrix(s),!s.isSquare())throw new Error("Matrix is not a square matrix");if(s.isEmpty())throw new Error("Matrix must be non-empty");let n=s.columns,i=new _(n,n),o=new Float64Array(n),l=new Float64Array(n),h=s,a,u,c=!1;if(e?c=!0:c=s.isSymmetric(),c){for(a=0;a<n;a++)for(u=0;u<n;u++)i.set(a,u,h.get(a,u));Hs(n,l,o,i),tn(n,l,o,i)}else{let g=new _(n,n),d=new Float64Array(n);for(u=0;u<n;u++)for(a=0;a<n;a++)g.set(a,u,h.get(a,u));en(n,g,d,i),sn(n,l,o,i,g)}this.n=n,this.e=l,this.d=o,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let s=this.n,t=this.e,e=this.d,n=new _(s,s),i,o;for(i=0;i<s;i++){for(o=0;o<s;o++)n.set(i,o,0);n.set(i,i,e[i]),t[i]>0?n.set(i,i+1,t[i]):t[i]<0&&n.set(i,i-1,t[i])}return n}};function Hs(r,s,t,e){let n,i,o,l,h,a,u,c;for(h=0;h<r;h++)t[h]=e.get(r-1,h);for(l=r-1;l>0;l--){for(c=0,o=0,a=0;a<l;a++)c=c+Math.abs(t[a]);if(c===0)for(s[l]=t[l-1],h=0;h<l;h++)t[h]=e.get(l-1,h),e.set(l,h,0),e.set(h,l,0);else{for(a=0;a<l;a++)t[a]/=c,o+=t[a]*t[a];for(n=t[l-1],i=Math.sqrt(o),n>0&&(i=-i),s[l]=c*i,o=o-n*i,t[l-1]=n-i,h=0;h<l;h++)s[h]=0;for(h=0;h<l;h++){for(n=t[h],e.set(h,l,n),i=s[h]+e.get(h,h)*n,a=h+1;a<=l-1;a++)i+=e.get(a,h)*t[a],s[a]+=e.get(a,h)*n;s[h]=i}for(n=0,h=0;h<l;h++)s[h]/=o,n+=s[h]*t[h];for(u=n/(o+o),h=0;h<l;h++)s[h]-=u*t[h];for(h=0;h<l;h++){for(n=t[h],i=s[h],a=h;a<=l-1;a++)e.set(a,h,e.get(a,h)-(n*s[a]+i*t[a]));t[h]=e.get(l-1,h),e.set(l,h,0)}}t[l]=o}for(l=0;l<r-1;l++){if(e.set(r-1,l,e.get(l,l)),e.set(l,l,1),o=t[l+1],o!==0){for(a=0;a<=l;a++)t[a]=e.get(a,l+1)/o;for(h=0;h<=l;h++){for(i=0,a=0;a<=l;a++)i+=e.get(a,l+1)*e.get(a,h);for(a=0;a<=l;a++)e.set(a,h,e.get(a,h)-i*t[a])}}for(a=0;a<=l;a++)e.set(a,l+1,0)}for(h=0;h<r;h++)t[h]=e.get(r-1,h),e.set(r-1,h,0);e.set(r-1,r-1,1),s[0]=0}function tn(r,s,t,e){let n,i,o,l,h,a,u,c,g,d,w,p,P,f,M,E;for(o=1;o<r;o++)s[o-1]=s[o];s[r-1]=0;let v=0,R=0,D=Number.EPSILON;for(a=0;a<r;a++){for(R=Math.max(R,Math.abs(t[a])+Math.abs(s[a])),u=a;u<r&&!(Math.abs(s[u])<=D*R);)u++;if(u>a)do{for(n=t[a],c=(t[a+1]-n)/(2*s[a]),g=ct(c,1),c<0&&(g=-g),t[a]=s[a]/(c+g),t[a+1]=s[a]*(c+g),d=t[a+1],i=n-t[a],o=a+2;o<r;o++)t[o]-=i;for(v=v+i,c=t[u],w=1,p=w,P=w,f=s[a+1],M=0,E=0,o=u-1;o>=a;o--)for(P=p,p=w,E=M,n=w*s[o],i=w*c,g=ct(c,s[o]),s[o+1]=M*g,M=s[o]/g,w=c/g,c=w*t[o]-M*n,t[o+1]=i+M*(w*n+M*t[o]),h=0;h<r;h++)i=e.get(h,o+1),e.set(h,o+1,M*e.get(h,o)+w*i),e.set(h,o,w*e.get(h,o)-M*i);c=-M*E*P*f*s[a]/d,s[a]=M*c,t[a]=w*c}while(Math.abs(s[a])>D*R);t[a]=t[a]+v,s[a]=0}for(o=0;o<r-1;o++){for(h=o,c=t[o],l=o+1;l<r;l++)t[l]<c&&(h=l,c=t[l]);if(h!==o)for(t[h]=t[o],t[o]=c,l=0;l<r;l++)c=e.get(l,o),e.set(l,o,e.get(l,h)),e.set(l,h,c)}}function en(r,s,t,e){let n=0,i=r-1,o,l,h,a,u,c,g;for(c=n+1;c<=i-1;c++){for(g=0,a=c;a<=i;a++)g=g+Math.abs(s.get(a,c-1));if(g!==0){for(h=0,a=i;a>=c;a--)t[a]=s.get(a,c-1)/g,h+=t[a]*t[a];for(l=Math.sqrt(h),t[c]>0&&(l=-l),h=h-t[c]*l,t[c]=t[c]-l,u=c;u<r;u++){for(o=0,a=i;a>=c;a--)o+=t[a]*s.get(a,u);for(o=o/h,a=c;a<=i;a++)s.set(a,u,s.get(a,u)-o*t[a])}for(a=0;a<=i;a++){for(o=0,u=i;u>=c;u--)o+=t[u]*s.get(a,u);for(o=o/h,u=c;u<=i;u++)s.set(a,u,s.get(a,u)-o*t[u])}t[c]=g*t[c],s.set(c,c-1,g*l)}}for(a=0;a<r;a++)for(u=0;u<r;u++)e.set(a,u,a===u?1:0);for(c=i-1;c>=n+1;c--)if(s.get(c,c-1)!==0){for(a=c+1;a<=i;a++)t[a]=s.get(a,c-1);for(u=c;u<=i;u++){for(l=0,a=c;a<=i;a++)l+=t[a]*e.get(a,u);for(l=l/t[c]/s.get(c,c-1),a=c;a<=i;a++)e.set(a,u,e.get(a,u)+l*t[a])}}}function sn(r,s,t,e,n){let i=r-1,o=0,l=r-1,h=Number.EPSILON,a=0,u=0,c=0,g=0,d=0,w=0,p=0,P=0,f,M,E,v,R,D,S,k,L,m,b,j,y,C,F;for(f=0;f<r;f++)for((f<o||f>l)&&(t[f]=n.get(f,f),s[f]=0),M=Math.max(f-1,0);M<r;M++)u=u+Math.abs(n.get(f,M));for(;i>=o;){for(v=i;v>o&&(w=Math.abs(n.get(v-1,v-1))+Math.abs(n.get(v,v)),w===0&&(w=u),!(Math.abs(n.get(v,v-1))<h*w));)v--;if(v===i)n.set(i,i,n.get(i,i)+a),t[i]=n.get(i,i),s[i]=0,i--,P=0;else if(v===i-1){if(S=n.get(i,i-1)*n.get(i-1,i),c=(n.get(i-1,i-1)-n.get(i,i))/2,g=c*c+S,p=Math.sqrt(Math.abs(g)),n.set(i,i,n.get(i,i)+a),n.set(i-1,i-1,n.get(i-1,i-1)+a),k=n.get(i,i),g>=0){for(p=c>=0?c+p:c-p,t[i-1]=k+p,t[i]=t[i-1],p!==0&&(t[i]=k-S/p),s[i-1]=0,s[i]=0,k=n.get(i,i-1),w=Math.abs(k)+Math.abs(p),c=k/w,g=p/w,d=Math.sqrt(c*c+g*g),c=c/d,g=g/d,M=i-1;M<r;M++)p=n.get(i-1,M),n.set(i-1,M,g*p+c*n.get(i,M)),n.set(i,M,g*n.get(i,M)-c*p);for(f=0;f<=i;f++)p=n.get(f,i-1),n.set(f,i-1,g*p+c*n.get(f,i)),n.set(f,i,g*n.get(f,i)-c*p);for(f=o;f<=l;f++)p=e.get(f,i-1),e.set(f,i-1,g*p+c*e.get(f,i)),e.set(f,i,g*e.get(f,i)-c*p)}else t[i-1]=k+c,t[i]=k+c,s[i-1]=p,s[i]=-p;i=i-2,P=0}else{if(k=n.get(i,i),L=0,S=0,v<i&&(L=n.get(i-1,i-1),S=n.get(i,i-1)*n.get(i-1,i)),P===10){for(a+=k,f=o;f<=i;f++)n.set(f,f,n.get(f,f)-k);w=Math.abs(n.get(i,i-1))+Math.abs(n.get(i-1,i-2)),k=L=.75*w,S=-.4375*w*w}if(P===30&&(w=(L-k)/2,w=w*w+S,w>0)){for(w=Math.sqrt(w),L<k&&(w=-w),w=k-S/((L-k)/2+w),f=o;f<=i;f++)n.set(f,f,n.get(f,f)-w);a+=w,k=L=S=.964}for(P=P+1,R=i-2;R>=v&&(p=n.get(R,R),d=k-p,w=L-p,c=(d*w-S)/n.get(R+1,R)+n.get(R,R+1),g=n.get(R+1,R+1)-p-d-w,d=n.get(R+2,R+1),w=Math.abs(c)+Math.abs(g)+Math.abs(d),c=c/w,g=g/w,d=d/w,!(R===v||Math.abs(n.get(R,R-1))*(Math.abs(g)+Math.abs(d))<h*(Math.abs(c)*(Math.abs(n.get(R-1,R-1))+Math.abs(p)+Math.abs(n.get(R+1,R+1))))));)R--;for(f=R+2;f<=i;f++)n.set(f,f-2,0),f>R+2&&n.set(f,f-3,0);for(E=R;E<=i-1&&(C=E!==i-1,E!==R&&(c=n.get(E,E-1),g=n.get(E+1,E-1),d=C?n.get(E+2,E-1):0,k=Math.abs(c)+Math.abs(g)+Math.abs(d),k!==0&&(c=c/k,g=g/k,d=d/k)),k!==0);E++)if(w=Math.sqrt(c*c+g*g+d*d),c<0&&(w=-w),w!==0){for(E!==R?n.set(E,E-1,-w*k):v!==R&&n.set(E,E-1,-n.get(E,E-1)),c=c+w,k=c/w,L=g/w,p=d/w,g=g/c,d=d/c,M=E;M<r;M++)c=n.get(E,M)+g*n.get(E+1,M),C&&(c=c+d*n.get(E+2,M),n.set(E+2,M,n.get(E+2,M)-c*p)),n.set(E,M,n.get(E,M)-c*k),n.set(E+1,M,n.get(E+1,M)-c*L);for(f=0;f<=Math.min(i,E+3);f++)c=k*n.get(f,E)+L*n.get(f,E+1),C&&(c=c+p*n.get(f,E+2),n.set(f,E+2,n.get(f,E+2)-c*d)),n.set(f,E,n.get(f,E)-c),n.set(f,E+1,n.get(f,E+1)-c*g);for(f=o;f<=l;f++)c=k*e.get(f,E)+L*e.get(f,E+1),C&&(c=c+p*e.get(f,E+2),e.set(f,E+2,e.get(f,E+2)-c*d)),e.set(f,E,e.get(f,E)-c),e.set(f,E+1,e.get(f,E+1)-c*g)}}}if(u!==0){for(i=r-1;i>=0;i--)if(c=t[i],g=s[i],g===0)for(v=i,n.set(i,i,1),f=i-1;f>=0;f--){for(S=n.get(f,f)-c,d=0,M=v;M<=i;M++)d=d+n.get(f,M)*n.get(M,i);if(s[f]<0)p=S,w=d;else if(v=f,s[f]===0?n.set(f,i,S!==0?-d/S:-d/(h*u)):(k=n.get(f,f+1),L=n.get(f+1,f),g=(t[f]-c)*(t[f]-c)+s[f]*s[f],D=(k*w-p*d)/g,n.set(f,i,D),n.set(f+1,i,Math.abs(k)>Math.abs(p)?(-d-S*D)/k:(-w-L*D)/p)),D=Math.abs(n.get(f,i)),h*D*D>1)for(M=f;M<=i;M++)n.set(M,i,n.get(M,i)/D)}else if(g<0)for(v=i-1,Math.abs(n.get(i,i-1))>Math.abs(n.get(i-1,i))?(n.set(i-1,i-1,g/n.get(i,i-1)),n.set(i-1,i,-(n.get(i,i)-c)/n.get(i,i-1))):(F=Qt(0,-n.get(i-1,i),n.get(i-1,i-1)-c,g),n.set(i-1,i-1,F[0]),n.set(i-1,i,F[1])),n.set(i,i-1,0),n.set(i,i,1),f=i-2;f>=0;f--){for(m=0,b=0,M=v;M<=i;M++)m=m+n.get(f,M)*n.get(M,i-1),b=b+n.get(f,M)*n.get(M,i);if(S=n.get(f,f)-c,s[f]<0)p=S,d=m,w=b;else if(v=f,s[f]===0?(F=Qt(-m,-b,S,g),n.set(f,i-1,F[0]),n.set(f,i,F[1])):(k=n.get(f,f+1),L=n.get(f+1,f),j=(t[f]-c)*(t[f]-c)+s[f]*s[f]-g*g,y=(t[f]-c)*2*g,j===0&&y===0&&(j=h*u*(Math.abs(S)+Math.abs(g)+Math.abs(k)+Math.abs(L)+Math.abs(p))),F=Qt(k*d-p*m+g*b,k*w-p*b-g*m,j,y),n.set(f,i-1,F[0]),n.set(f,i,F[1]),Math.abs(k)>Math.abs(p)+Math.abs(g)?(n.set(f+1,i-1,(-m-S*n.get(f,i-1)+g*n.get(f,i))/k),n.set(f+1,i,(-b-S*n.get(f,i)-g*n.get(f,i-1))/k)):(F=Qt(-d-L*n.get(f,i-1),-w-L*n.get(f,i),p,g),n.set(f+1,i-1,F[0]),n.set(f+1,i,F[1]))),D=Math.max(Math.abs(n.get(f,i-1)),Math.abs(n.get(f,i))),h*D*D>1)for(M=f;M<=i;M++)n.set(M,i-1,n.get(M,i-1)/D),n.set(M,i,n.get(M,i)/D)}for(f=0;f<r;f++)if(f<o||f>l)for(M=f;M<r;M++)e.set(f,M,n.get(f,M));for(M=r-1;M>=o;M--)for(f=o;f<=l;f++){for(p=0,E=o;E<=Math.min(M,l);E++)p=p+e.get(f,E)*n.get(E,M);e.set(f,M,p)}}}function Qt(r,s,t,e){let n,i;return Math.abs(t)>Math.abs(e)?(n=e/t,i=t+n*e,[(r+n*s)/i,(s-n*r)/i]):(n=t/e,i=e+n*t,[(n*r+s)/i,(n*s-r)/i])}class Oe{constructor(s){if(s=rt.checkMatrix(s),!s.isSymmetric())throw new Error("Matrix is not symmetric");let t=s,e=t.rows,n=new _(e,e),i=!0,o,l,h;for(l=0;l<e;l++){let a=0;for(h=0;h<l;h++){let u=0;for(o=0;o<h;o++)u+=n.get(h,o)*n.get(l,o);u=(t.get(l,h)-u)/n.get(h,h),n.set(l,h,u),a=a+u*u}for(a=t.get(l,l)-a,i&&(i=a>0),n.set(l,l,Math.sqrt(Math.max(a,0))),h=l+1;h<e;h++)n.set(l,h,0)}this.L=n,this.positiveDefinite=i}isPositiveDefinite(){return this.positiveDefinite}solve(s){s=rt.checkMatrix(s);let t=this.L,e=t.rows;if(s.rows!==e)throw new Error("Matrix dimensions do not match");if(this.isPositiveDefinite()===!1)throw new Error("Matrix is not positive definite");let n=s.columns,i=s.clone(),o,l,h;for(h=0;h<e;h++)for(l=0;l<n;l++){for(o=0;o<h;o++)i.set(h,l,i.get(h,l)-i.get(o,l)*t.get(h,o));i.set(h,l,i.get(h,l)/t.get(h,h))}for(h=e-1;h>=0;h--)for(l=0;l<n;l++){for(o=h+1;o<e;o++)i.set(h,l,i.get(h,l)-i.get(o,l)*t.get(o,h));i.set(h,l,i.get(h,l)/t.get(h,h))}return i}get lowerTriangularMatrix(){return this.L}}class Ue{constructor(s,t={}){s=rt.checkMatrix(s);let{Y:e}=t;const{scaleScores:n=!1,maxIterations:i=1e3,terminationCriteria:o=1e-10}=t;let l;if(e){if(st.isAnyArray(e)&&typeof e[0]=="number"?e=_.columnVector(e):e=rt.checkMatrix(e),e.rows!==s.rows)throw new Error("Y should have the same number of rows as X");l=e.getColumnVector(0)}else l=s.getColumnVector(0);let h=1,a,u,c,g;for(let d=0;d<i&&h>o;d++)c=s.transpose().mmul(l).div(l.transpose().mmul(l).get(0,0)),c=c.div(c.norm()),a=s.mmul(c).div(c.transpose().mmul(c).get(0,0)),d>0&&(h=a.clone().sub(g).pow(2).sum()),g=a.clone(),e?(u=e.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),u=u.div(u.norm()),l=e.mmul(u).div(u.transpose().mmul(u).get(0,0))):l=a;if(e){let d=s.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0));d=d.div(d.norm());let w=s.clone().sub(a.clone().mmul(d.transpose())),p=l.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),P=e.clone().sub(a.clone().mulS(p.get(0,0)).mmul(u.transpose()));this.t=a,this.p=d.transpose(),this.w=c.transpose(),this.q=u,this.u=l,this.s=a.transpose().mmul(a),this.xResidual=w,this.yResidual=P,this.betas=p}else this.w=c.transpose(),this.s=a.transpose().mmul(a).sqrt(),n?this.t=a.clone().div(this.s.get(0,0)):this.t=a,this.xResidual=s.sub(a.mmul(c.transpose()))}}$.AbstractMatrix=V;$.CHO=Oe;$.CholeskyDecomposition=Oe;$.DistanceMatrix=se;$.EVD=Be;var nn=$.EigenvalueDecomposition=Be;$.LU=ne;$.LuDecomposition=ne;var Ye=$.Matrix=_;$.MatrixColumnSelectionView=$s;$.MatrixColumnView=Ns;$.MatrixFlipColumnView=Ds;$.MatrixFlipRowView=Ls;$.MatrixRowSelectionView=Bs;$.MatrixRowView=zs;$.MatrixSelectionView=Xt;$.MatrixSubView=Os;$.MatrixTransposeView=Us;$.NIPALS=Ue;$.Nipals=Ue;$.QR=ye;$.QrDecomposition=ye;$.SVD=It;$.SingularValueDecomposition=It;$.SymmetricMatrix=Pt;$.WrapperMatrix1D=Le;$.WrapperMatrix2D=rt;$.correlation=Ks;$.covariance=Js;var Te=$.default=_;$.determinant=Zt;$.inverse=Ws;$.linearDependencies=Xs;$.pseudoInverse=Zs;$.solve=ze;$.wrap=Ys;const rn=nn,on=Ye;Te.Matrix&&Te.Matrix;function Ft(r,s,t){const e=r/t,n=s/t;if(!(isFinite(e)&&isFinite(n))){const h=-r/s;return isFinite(h)?[h]:r==0&&s==0?[0]:[]}const i=n*n-4*e;let o=0;if(isFinite(i)){if(i<0)return[];if(i==0)return[-.5*n];o=-.5*(n+Rt(Math.sqrt(i),n))}else o=-n;const l=e/o;return isFinite(l)?l>o?[o,l]:[l,o]:[o]}function We(r,s,t,e){const n=t/(3*e),i=s/(3*e),o=r/e;if(!(isFinite(o)&&isFinite(i)&&isFinite(n)))return Ft(r,s,t);const l=-n*n+i,h=-i*n+o,a=n*o-i*i,u=4*l*a-h*h,c=-2*n*l+h;if(u<0){const g=Math.sqrt(-.25*u),d=-.5*c;return[Math.cbrt(d+g)+Math.cbrt(d-g)-n]}else if(u==0){const g=Rt(Math.sqrt(-l),c);return[g-n,-2*g-n]}else{const g=Math.atan2(Math.sqrt(u),-c)/3,d=Math.cos(g),w=Math.sin(g)*Math.sqrt(3),p=.5*(-d+w),P=.5*(-d-w),f=2*Math.sqrt(-l);return[f*d-n,f*p-n,f*P-n]}}function ln(r,s,t,e,n){if(n==0)return We(r,s,t,e);const i=e/n,o=t/n,l=s/n,h=r/n;let a=Ie(i,o,l,h,!1);if(a!==null)return a;const u=716e74;for(let c=0;c<2;c++)if(a=Ie(i/u,o/(u*u),l/(u*u*u),h/(u*u*u*u),c!=0),a!==null){for(let g=0;g<a.length;g++)a[g]*=u;return a}return[]}function gt(r,s){return s==0?Math.abs(r):Math.abs((r-s)/s)}function Ie(r,s,t,e,n){const i=hn(r,s,t,e,n);if(i!==null&&i.length==4){let o=[];for(let l=0;l<2;l++){const h=i[l*2],a=i[l*2+1];o=o.concat(Ft(a,h,1))}return o}return null}function hn(r,s,t,e,n){function i(A,N,x,B){const Y=gt(A+x,r),T=gt(N+A*x+B,s),z=gt(N*x+A*B,t);return Y+T+z}function o(A,N,x,B){return i(A,N,x,B)+gt(N*B,e)}const l=9*r*r-24*s,h=l>=0?-2*s/(3*r+Rt(Math.sqrt(l),r)):-.25*r,a=r+4*h,u=s+3*h*(r+2*h),c=t+h*(2*s+h*(3*r+4*h)),g=e+h*(t+h*(s+h*(r+h)));let d=0,w=0;const p=349e100;if(n){const A=a/p,N=u/p,x=c/p,B=g/p;d=A*x-4/p*B-1/3*N*N,w=(A*x-8/p*B-2/9*N*N)*(1/3)*N-x*(x/p)-A*A*B}else d=a*c-4*g-1/3*u*u,w=(a*c+8*g-2/9*u*u)*(1/3)*u-c*c-a*a*g;if(!isFinite(d)&&isFinite(w))return null;let P=an(d,w);n&&(P*=p);const f=r*.5,M=1/6*s+.5*P,E=t-r*M,v=2/3*s-P-f*f,R=.5*E/v,D=2*(e-M*M)/E,S=.5*E/D;let k=0,L=0,m=0;for(let A=0;A<3;A++){const N=A==1?S:v,x=A==0?R:D,B=gt(N+f*f+2*M,s),Y=gt(2*(N*x+f*M),t),T=gt(N*x*x+M*M,e),z=B+Y+T;(A==0||z<m)&&(k=N,L=x,m=z)}const b=k,j=L;let y=0,C=0,F=0,I=0;if(b<0){const A=Math.sqrt(-b);if(y=f+A,C=M+A*j,F=f-A,I=M-A*j,Math.abs(I)<Math.abs(C)?I=e/C:Math.abs(I)>Math.abs(C)&&(C=e/I),Math.abs(y)!=Math.abs(F)){let N=null,x=null;if(Math.abs(y)<Math.abs(F)){const Y=(t-C*F)/I,T=(s-I-C)/F;N=[r-F,Y,T],x=[F,F,F]}else{const Y=(t-y*I)/C,T=(s-I-C)/y,z=r-y;N=[y,y,y],x=[z,Y,T]}let B=0;for(let Y=0;Y<3;Y++){const T=N[Y],z=x[Y];if(isFinite(T)&&isFinite(z)){const X=i(T,C,z,I);(Y==0||X<B)&&(y=T,F=z,B=X)}}}}else if(b==0){const A=e-M*M;y=f,C=M+Math.sqrt(-A),F=f,I=M-Math.sqrt(-A),Math.abs(C)>Math.abs(I)?I=e/C:Math.abs(I)>Math.abs(C)&&(C=e/I)}else return[];let U=o(y,C,F,I);for(let A=0;A<8&&U!=0;A++){const N=C*I-e,x=C*F+y*I-t,B=C+y*F+I-s,Y=y+F-r,T=y-F,z=C*C-C*(F*T+2*I)+I*(y*T+I);if(z==0)break;const X=1/z,Q=I-C,W=C*F-y*I,ie=T*N+Q*x+W*B-(C*Q+y*W)*Y,re=(y*T+Q)*N-C*(T*x+Q*B+W*Y),oe=-T*N-Q*x-W*B+(F*W+I*Q)*Y,le=-(F*T+Q)*N+I*(T*x+Q*B+W*Y),Nt=y-X*ie,$t=C-X*re,Dt=F-X*oe,Lt=I-X*le,zt=o(Nt,$t,Dt,Lt);if(zt<U)y=Nt,C=$t,F=Dt,I=Lt,U=zt;else break}return[y,C,F,I]}function an(r,s){const t=-.3333333333333333*r,e=.5*s;let n,i=null;if((Math.abs(t)>=1e102||Math.abs(e)>=1e164)&&(Math.abs(t)<Math.abs(e)?i=1-t*(t/e)*(t/e):i=Math.sign(t)*(e/t*(e/t)/t-1)),i!==null&&e==0)r>0?n=0:n=Math.sqrt(-r);else if(i!==null?i<0:e*e<t*t*t){const a=i!==null?e/t/Math.sqrt(t):e/Math.sqrt(t*t*t);n=-2*Math.sqrt(t)*Rt(Math.cos(Math.acos(Math.abs(a))*(1/3)),a)}else{let a;i!==null?Math.abs(t)<Math.abs(e)?a=-e*(1+Math.sqrt(i)):a=-e-Rt(Math.sqrt(Math.abs(t))*t*Math.sqrt(i),e):a=Math.cbrt(-e-Rt(Math.sqrt(e*e-t*t*t),e));const u=a==0?0:t/a;n=a+u}let o=n,l=(o*o+r)*o+s;if(Math.abs(l)<222045e-21*Math.max(o*o*o,r*o,s))return o;for(let a=0;a<8;a++){const u=3*o*o+r;if(u==0)break;const c=o-l/u,g=(c*c+r)*c+s;if(g==0)return c;if(Math.abs(g)>=Math.abs(l))break;o=c,l=g}return o}function Rt(r,s){const t=Math.abs(r);return s<0?-t:t}function Ge(r,s=1e-9){if(r[r.length-1]==0)return Ge(r.slice(0,-1),s);const t=new on([r.slice(0,-1).reverse().map(n=>-n/r[r.length-1]),...Array.from({length:r.length-2},(n,i)=>{const o=new Array(r.length-1).fill(0);return o[i]=1,o})]),e=new rn(t);return e.realEigenvalues.filter((n,i)=>Math.abs(e.imaginaryEigenvalues[i])<s)}const un=(r,s,t)=>[r[0]*(1-t)+s[0]*t,r[1]*(1-t)+s[1]*t];function cn(r){const s=[];for(let t=0;t<r.length-1;t++)s.push([r[t],r[t+1]]);return s}function Pe(r,s){const t=cn(r).map(([e,n])=>un(e,n,s));return r.length===2?[t]:[...Pe(t,s),t]}class wt extends xt{constructor(t,e,n,i){super(t,e);q(this,"segmentType","CUBIC_BEZIER");q(this,"firstControlPoint");q(this,"lastControlPoint");q(this,"_boundingBox",null);q(this,"_polynomialCoefficients",null);this.firstControlPoint=n,this.lastControlPoint=i}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,i,o]=[this.firstPoint[t],this.firstControlPoint[t],this.lastControlPoint[t],this.lastPoint[t]],l=-e+3*n-3*i+o,h=2*e-4*n+2*i,a=-e+n;return Ft(a,h,l).filter(u=>u>=-this.precision&&u<=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=G(this.lastPoint,this.firstPoint),n=Math.atan2(e[1],e[0]),i=new et;return i.translate(t[0],t[1]),i.rotate(-n),this.transform(i)}getInflexionParameters(){const t=this.alignedCurve,[e,n]=t.firstControlPoint,[i,o]=t.lastControlPoint,[l,h]=t.lastPoint,a=i*n,u=l*n,c=e*o,g=l*o,d=-3*a+2*u+3*c-g,w=3*a-u-3*c,p=c-a;return Ft(p,w,d).filter(P=>P>=0&&P<=1)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=ge([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new wt(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new wt(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)&&O(this.firstControlPoint,t.firstControlPoint)&&O(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const n=Ve(i=>Et(this.paramPoint(i),t),this.precision);return n.argMin<-this.precision||n.argMin>1+this.precision?Math.min(J(this.firstPoint,t),J(this.lastPoint,t)):Math.sqrt(n.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,i=t*t,o=3*n,l=6*e*t,h=3*i,a=G(this.firstControlPoint,this.firstPoint),u=G(this.lastControlPoint,this.firstControlPoint),c=G(this.lastPoint,this.lastControlPoint);return[o*a[0]+l*u[0]+h*c[0],o*a[1]+l*u[1]+h*c[1]]}secondDerivativeAt(t){const e=it(this.firstPoint,this.lastControlPoint),n=nt(this.firstControlPoint,2),i=it(this.firstControlPoint,this.lastPoint),o=nt(this.lastControlPoint,2),l=nt(G(e,n),6*(1-t)),h=nt(G(i,o),6*t);return it(l,h)}tangentAt(t){const e=this.pointToParam(t);return K(this.gradientAt(e))}get tangentAtFirstPoint(){return K(G(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return K(G(this.lastPoint,this.lastControlPoint))}normalAt(t){const e=this.tangentAt(t);return H(e)}get normalAtFirstPoint(){return H(this.tangentAtFirstPoint)}get normalAtLastPoint(){return H(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((o,l)=>o-l);let i={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return n.flatMap((o,l)=>{if(o-i.originalParam<this.precision)return[];let h=!1;o>1-this.precision&&(h=!0);const a=(o-i.originalParam)/(1-i.originalParam),u=Pe([i.p0,i.p1,i.p2,this.lastPoint],h?1:a),c=h?this.lastPoint:(e==null?void 0:e.get(o))??u[0][0],g=u[1][0],d=u[2][0],w=u[1][1],p=u[2][2],P=new wt(i.p0,c,d,g);return i={param:a,originalParam:o,p0:c,p1:w,p2:p},l===n.length-1&&!h?[P,new wt(i.p0,this.lastPoint,i.p1,i.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(o=>this.pointToParam(o)),i=new Map(Vt([n,e]));return this.splitAtParameters(n,i)}transform(t){return new wt(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,i=t*t,o=n*e,l=3*n*t,h=3*e*i,a=i*t;return[o*this.firstPoint[0]+l*this.firstControlPoint[0]+h*this.lastControlPoint[0]+a*this.lastPoint[0],o*this.firstPoint[1]+l*this.firstControlPoint[1]+h*this.lastControlPoint[1]+a*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],i=this.firstControlPoint[e],o=this.lastControlPoint[e],l=this.lastPoint[e],h=-n+3*i-3*o+l,a=3*n-6*i+3*o,u=-3*n+3*i;return[n,u,a,h]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,i,o]=this.polynomialCoefficients[1];return We(e-t,n,i,o).filter(l=>l>=-this.precision&&l<=1+this.precision)}pointToParam(t){if(O(t,this.firstPoint,this.precision))return 0;if(O(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=o=>Math.abs(this.paramPoint(o)[0]-t[0])<=this.precision,i=e.find(n);if(i===void 0)throw new Error("Point is not on the curve");return i}}class Mt extends xt{constructor(t,e,n,i,o,l,h=!1,{ignoreChecks:a=!1,angleUnits:u="deg"}={}){super(t,e);q(this,"segmentType","ELLIPSE_ARC");q(this,"precision",1e-6);q(this,"majorRadius");q(this,"minorRadius");q(this,"center");q(this,"tiltAngle");q(this,"clockwise");q(this,"_coefficients",null);q(this,"_boundingBox");q(this,"_linearExentricity");q(this,"_exentricity");q(this,"_focals");q(this,"_ellipseReferenceFrameTransform");q(this,"_reverseEllipseReferenceFrameTransform");q(this,"_rotateFromEllipseReferenceFrame");q(this,"_firstAngle");q(this,"_lastAngle");q(this,"_deltaAngle");this.center=n;const c=i>=o;this.majorRadius=c?i:o,this.minorRadius=c?o:i;const g=u==="deg"?l*Ht:l;if(this.tiltAngle=dt(c?g:g+Math.PI/2),this.clockwise=h,!a){if(O(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${Z(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${Z(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,i=this.minorRadius*this.minorRadius,o=Math.cos(this.tiltAngle),l=Math.sin(this.tiltAngle),h=n*l*l+i*o*o,a=2*(i-n)*l*o,u=n*o*o+i*l*l;this._coefficients={x2:h,xy:a,y2:u,x:-2*h*t-a*e,y:-2*u*e-a*t,c:h*t*t+a*t*e+u*e*e-n*i}}return this._coefficients}get info(){return`ELLIPSE_ARC(${Z(this.firstPoint)}, ${Z(this.lastPoint)}, ${Z(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*Ce}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new Mt(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new Mt(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),i=Math.atan(e/t);return[n,Math.PI+n,i,Math.PI+i].map(l=>this.thetaToParam(l)).filter(l=>this.isValidParameter(l)).map(l=>this.paramPoint(l))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=ge(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=[it(this.center,nt(t,this.linearExentricity)),it(this.center,nt(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return vt(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 ${Z(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${Z(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 O(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)&&(O(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||O(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 dt(n)}thetaToParam(t){return Jt(this.firstAngle,dt(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,n]=this.focals,i=J(t,e),o=J(t,n);return Math.abs(2*this.majorRadius-i-o)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(O(t,this.center)?e=it(this.center,nt(H(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(gn(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return J(t,e);if(Ke(t,this.majorAxis,this.center)){const n=dt(2*Math.PI-this.pointTheta(t)),i=this.thetaToParam(n);if(this.isValidParameter(i))return J(t,this.paramPoint(i))}return Math.min(J(t,this.firstPoint),J(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new et().translate(-this.center[0],-this.center[1]).rotate(-this.tiltAngle)),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new et().rotate(this.tiltAngle).translate(this.center[0],this.center[1])),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new et().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=Jt(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?H(e):Ct(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),n=-this.majorRadius*this.deltaAngle*Math.sin(e),i=this.minorRadius*this.deltaAngle*Math.cos(e),o=this.clockwise?[-n,-i]:[n,i];return this.rotateFromEllipseReferenceFrame.transform(o)}tangentAt(t){const e=this.pointTheta(t),n=-this.majorRadius*Math.sin(e),i=this.minorRadius*Math.cos(e),o=this.clockwise?[-n,-i]:[n,i];return K(this.rotateFromEllipseReferenceFrame.transform(o))}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 K(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 K(this.rotateFromEllipseReferenceFrame.transform(n))}transform(t){const e=t.transformAngle(this.tiltAngle),n=t.scaleFactor();return new Mt(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 i=[0,1,...e.map(h=>this.pointToParam(h))],o=new Map(Vt([i,[this.firstPoint,this.lastPoint,...e]]));i.sort((h,a)=>h-a);let l=null;return i.flatMap((h,a)=>{if(a===i.length-1)return[];const u=i[a+1];if(u-h<this.precision)return l===null&&(l=h),[];const c=l===null?h:l,g=new Mt(o.get(c)||this.paramPoint(c),o.get(u)||this.paramPoint(u),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return l=null,g})}}function fn(r,s,t,e,n,i,o){const{center:l,rx:h,ry:a}=mn(r,s,t,e,n*Ht,!i,o);return Math.abs(h-a)<1e-9?new ut(r,s,l,o):new Mt(r,s,l,h,a,n,o)}function mn([r,s],[t,e],n,i,o,l,h){if(n<0&&(n=-n),i<0&&(i=-i),n==0||i==0)throw Error("rx and ry can not be 0");const a=Math.sin(o),u=Math.cos(o),c=(r-t)/2,g=(s-e)/2,d=(r+t)/2,w=(s+e)/2,p=u*c+a*g,P=u*g-a*c,f=p*p/(n*n)+P*P/(i*i);f>1&&(n=n*Math.sqrt(f),i=i*Math.sqrt(f));const M=n*i,E=n*P,v=i*p,R=E*E+v*v;if(!R)throw Error("start point can not be same as end point");let D=Math.sqrt(Math.abs((M*M-R)/R));l==h&&(D=-D);const S=D*E/i,k=-D*v/n,L=u*S-a*k+d,m=a*S+u*k+w;return{center:[L,m],rx:n,ry:i}}function gn(r,s,t){const e=Math.abs(t[0]),n=Math.abs(t[1]);let i=.707,o=.707;const l=r,h=s;for(let a=0;a<3;a++){const u=l*i,c=h*o,g=(l*l-h*h)*i**3/l,d=(h*h-l*l)*o**3/h,w=u-g,p=c-d,P=e-g,f=n-d,M=Math.hypot(w,p),E=Math.hypot(P,f);i=Math.min(1,Math.max(0,(P*M/E+g)/l)),o=Math.min(1,Math.max(0,(f*M/E+d)/h));const v=Math.hypot(i,o);i/=v,o/=v}return[l*i*Math.sign(t[0]),h*o*Math.sign(t[1])]}class pt extends xt{constructor(t,e,n){super(t,e);q(this,"segmentType","QUADRATIC_BEZIER");q(this,"controlPoint");q(this,"_boundingBox",null);q(this,"_polynomialCoefficients",null);this.controlPoint=n}get midPoint(){return this.paramPoint(.5)}_extremaInDirection(t){const[e,n,i]=[this.firstPoint[t],this.controlPoint[t],this.lastPoint[t]],o=e-2*n+i;if(!o)return[];const l=(e-n)/o;return l>=-this.precision&&l<=1+this.precision?[l]:[]}getParametersOfExtrema(){return Array.from(new Set(this._extremaInDirection(0).concat(this._extremaInDirection(1))))}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=ge([this.firstPoint,...this.getParametersOfExtrema().map(t=>this.paramPoint(t)),this.lastPoint]).grow(this.precision)),this._boundingBox}clone(){return new pt(this.firstPoint,this.lastPoint,this.controlPoint)}reverse(){return new pt(this.lastPoint,this.firstPoint,this.controlPoint)}isSame(t){return t.segmentType!=="QUADRATIC_BEZIER"?!1:O(this.firstPoint,t.firstPoint)&&O(this.lastPoint,t.lastPoint)&&O(this.controlPoint,t.controlPoint)}distanceFrom(t){const n=Ve(i=>Et(this.paramPoint(i),t),this.precision);return n.argMin<-this.precision||n.argMin>1+this.precision?Math.min(J(this.firstPoint,t),J(this.lastPoint,t)):Math.sqrt(n.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 i=this.firstPoint[n],o=this.controlPoint[n],l=this.lastPoint[n];return 2*(t*(i-2*o+l)-i+o)};return[e(0),e(1)]}secondDerivativeAt(t){return nt(G(it(this.firstPoint,this.lastPoint),nt(this.controlPoint,2)),2)}tangentAt(t){const e=this.pointToParam(t);return K(this.gradientAt(e))}get tangentAtFirstPoint(){return K(G(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return K(G(this.lastPoint,this.controlPoint))}normalAt(t){const e=this.tangentAt(t);return H(e)}get normalAtFirstPoint(){return H(this.tangentAtFirstPoint)}get normalAtLastPoint(){return H(this.tangentAtLastPoint)}splitAtParameters(t,e=null){const n=[...t];n.sort((o,l)=>o-l);let i={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return n.flatMap((o,l)=>{if(o-i.originalParam<this.precision)return[];let h=!1;o>1-this.precision&&(h=!0);const a=(o-i.originalParam)/(1-i.originalParam),u=Pe([i.p0,i.p1,this.lastPoint],h?1:a),c=h?this.lastPoint:(e==null?void 0:e.get(o))??u[0][0],g=u[1][0],d=u[1][1],w=new pt(i.p0,c,g);return i={param:a,originalParam:o,p0:c,p1:d},l===n.length-1&&!h?[w,new pt(i.p0,this.lastPoint,i.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(o=>this.pointToParam(o)),i=new Map(Vt([n,e]));return this.splitAtParameters(n,i)}transform(t){return new pt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const e=1-t,n=e*e,i=2*e*t,o=t*t;return[n*this.firstPoint[0]+i*this.controlPoint[0]+o*this.lastPoint[0],n*this.firstPoint[1]+i*this.controlPoint[1]+o*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],i=this.controlPoint[e],o=this.lastPoint[e],l=n-2*i+o,h=2*(i-n);return[n,h,l]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,i]=this.polynomialCoefficients[1];return Ft(e-t,n,i).filter(o=>o>=-this.precision&&o<=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=o=>Math.abs(this.paramPoint(o)[0]-t[0])<=this.precision,i=e.find(n);if(i===void 0)throw new Error("Point is not on the curve");return i}}exports.Arc=ut;exports.BoundingBox=ft;exports.CubicBezier=wt;exports.DEG2RAD=Ht;exports.EllipseArc=Mt;exports.Line=at;exports.QuadraticBezier=pt;exports.RAD2DEG=Ce;exports.Transformable=Ae;exports.TransformationMatrix=et;exports.add=it;exports.angle=Je;exports.cartesianToPolar=Fe;exports.crossProduct=yt;exports.distance=J;exports.dotProduct=ce;exports.length=Ze;exports.lineLineIntersection=es;exports.lineLineParams=te;exports.normalize=K;exports.parallel=pe;exports.perpendicular=H;exports.perpendicularClockwise=Ct;exports.polarAngle=qe;exports.polarToCartesian=vt;exports.range=xe;exports.removeDuplicatePoints=we;exports.reprVector=Z;exports.sameVector=O;exports.scalarMultiply=nt;exports.solveGenericPolynomial=Ge;exports.solveQuartic=ln;exports.squareDistance=Et;exports.squareLength=Tt;exports.subtract=G;exports.svgEllipse=fn;exports.tangentArc=ns;exports.threePointsArc=ss;exports.zip=Vt;
|
|
9
|
+
//# sourceMappingURL=QuadraticBezier-CuRsIP_D.cjs.map
|