@tubular/math
Version:
Miscellaneous math functions
35 lines (34 loc) • 16.2 kB
JavaScript
;const t=Math.abs,e=Math.acos,n=Math.acosh;const s=Math.asin,r=Math.asinh;const i=Math.atan,o=Math.atan2,a=Math.atanh;const h=Math.cbrt;const u=Math.clz32,x=Math.cos,c=Math.cosh;const l=Math.E,p=Math.exp,M=Math.expm1;function _(t,e=1){return 1===e?Math.floor(t):t-C(t,e)}const g=Math.fround,N=Math.hypot,d=Math.imul;function I(t,e,n,s,r){return t===n?s:s+(e-t)*(r-s)/(n-t)}function A(t,e,n,s,r){let i=0;for(let o=s;o<=r;++o){let a=1;for(let e=s;e<=r;++e)e!==o&&(a*=(n-t[e])/(t[o]-t[e]));i+=a*e[o]}
return i}function E(t,e,n,s,r){const i=Math.min(t.length,e.length);let o=-1,a=i-1;for(let e=0;e<i;++e){const n=t[e];if(o<0&&n>=r-s&&(o=e),n>=r+s){a=e;break}}return A(t,e,n,Math.max(o,0),a)}const S=function(t,e=.01){return t<-1-e?-1:t>1+e?1:t<-1?-1:t>1?1:t},f=Math.LN10,R=Math.LN2,T=Math.LOG10E,G=Math.LOG2E,U=Math.PI/2,m=Math.log,D=Math.log10,w=Math.log1p,O=Math.log2,y=Math.max,L=Math.min;function C(t,e){const n=t%e;return n<0&&e>0||n>0&&e<0?e+n:n}function v(t,e){let n=t-Math.floor(t/e)*e
;return n>=e/2&&(n-=e),n}const b=Math.PI,F=Math.pow;function H(t,e=1){return 1===e?Math.round(t):t+e/2-C(t+e/2,e)}const P=Math.sign;const k=Math.sin,V=Math.sinh;const z=Math.sqrt,X=Math.SQRT1_2,Z=Math.SQRT2;const q=Math.tan,Y=Math.tanh;const Q=Math.trunc,$=2*Math.PI;var j,W,B,J;"function"==typeof SuppressedError&&SuppressedError,exports.Unit=void 0,(B=exports.Unit||(exports.Unit={}))[B.RADIANS=0]="RADIANS",B[B.DEGREES=1]="DEGREES",B[B.ARC_MINUTES=2]="ARC_MINUTES",B[B.ARC_SECONDS=3]="ARC_SECONDS",
B[B.HOURS=4]="HOURS",B[B.HOUR_ANGLE_MINUTES=5]="HOUR_ANGLE_MINUTES",B[B.HOUR_ANGLE_SECONDS=6]="HOUR_ANGLE_SECONDS",B[B.ROTATIONS=7]="ROTATIONS",B[B.GRADS=8]="GRADS",exports.Mode=void 0,(J=exports.Mode||(exports.Mode={}))[J.RANGE_LIMIT_SIGNED=0]="RANGE_LIMIT_SIGNED",J[J.RANGE_LIMIT_NONNEGATIVE=1]="RANGE_LIMIT_NONNEGATIVE",J[J.RANGE_UNLIMITED=2]="RANGE_UNLIMITED";const K=Math.PI,tt=K/2,et=2*K;function nt(t,e){switch(e){case exports.Unit.RADIANS:return t;case exports.Unit.DEGREES:return t/180*K
;case exports.Unit.ARC_MINUTES:return t/10800*K;case exports.Unit.ARC_SECONDS:return t/648e3*K;case exports.Unit.HOURS:return t/12*K;case exports.Unit.HOUR_ANGLE_MINUTES:return t/720*K;case exports.Unit.HOUR_ANGLE_SECONDS:return t/43200*K;case exports.Unit.ROTATIONS:return t*et;case exports.Unit.GRADS:return t/200*K}return NaN}function st(t,e){switch(e){case exports.Unit.RADIANS:return t;case exports.Unit.DEGREES:return 180*t/K;case exports.Unit.ARC_MINUTES:return 10800*t/K;case exports.Unit.ARC_SECONDS:
return 648e3*t/K;case exports.Unit.HOURS:return 12*t/K;case exports.Unit.HOUR_ANGLE_MINUTES:return 720*t/K;case exports.Unit.HOUR_ANGLE_SECONDS:return 43200*t/K;case exports.Unit.ROTATIONS:return t/et;case exports.Unit.GRADS:return 200*t/K}return NaN}class rt{static asin(t){return new j(Math.asin(t))}static asin_nonneg(t){return new j(Math.asin(t),exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}static acos(t){return new j(Math.acos(t))}static atan(t){return new j(Math.atan(t))}
static atan_nonneg(t){return new j(Math.atan(t),exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}static atan2(t,e){return new j(Math.atan2(t,e))}static atan2_nonneg(t,e){return new j(Math.atan2(t,e),exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}static parse(t,e=!1){const n=function(t,e,n,s){if("a"===n&&!s)throw new TypeError("Private accessor was defined without a getter")
;if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?s:"a"===n?s.call(t):s?s.value:e.get(t)}(j,j,"m",W).call(j,t);if(null==n&&e)throw new Error("Invalid angle: "+t);return n}constructor(t=0,e,n=exports.Mode.RANGE_LIMIT_SIGNED){return this.cached_sin=2,this.cached_cos=2,this.cached_tan=0,void 0===e&&(e=exports.Unit.RADIANS),
0===t&&j.ZERO?j.ZERO:(n===exports.Mode.RANGE_LIMIT_SIGNED?this.angle=v(nt(t,e),et):n===exports.Mode.RANGE_LIMIT_NONNEGATIVE?this.angle=C(nt(t,e),et):this.angle=nt(t,e),j.RIGHT&&this.angle===j.RIGHT.angle?j.RIGHT:j.STRAIGHT&&this.angle===j.STRAIGHT.angle?j.STRAIGHT:void 0)}get radians(){return this.angle}get degrees(){return st(this.angle,exports.Unit.DEGREES)}get arcMinutes(){return st(this.angle,exports.Unit.ARC_MINUTES)}get arcSeconds(){return st(this.angle,exports.Unit.ARC_SECONDS)}get hours(){
return st(this.angle,exports.Unit.HOURS)}get rotations(){return st(this.angle,exports.Unit.ROTATIONS)}get grads(){return st(this.angle,exports.Unit.GRADS)}getAngle(t=exports.Unit.RADIANS){return st(this.angle,t)}get sin(){return this.cached_sin>1&&(this.cached_sin=Math.sin(this.angle)),this.cached_sin}get cos(){return this.cached_cos>1&&(this.cached_cos=Math.cos(this.angle)),this.cached_cos}get tan(){return 0===this.angle?0:(0===this.cached_tan&&(this.cached_tan=Math.tan(this.angle)),this.cached_tan)}
add(t,e=exports.Mode.RANGE_LIMIT_SIGNED){return new j(this.angle+t.angle,exports.Unit.RADIANS,e)}add_nonneg(t){return new j(this.angle+t.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}subtract(t,e=exports.Mode.RANGE_LIMIT_SIGNED){return new j(this.angle-t.angle,exports.Unit.RADIANS,e)}subtract_nonneg(t){return new j(this.angle-t.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}complement(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(tt-this.angle,exports.Unit.RADIANS,t)}
complement_nonneg(){return new j(tt-this.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}supplement(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(K-this.angle,exports.Unit.RADIANS,t)}supplement_nonneg(){return new j(K-this.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}opposite(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(this.angle+K,exports.Unit.RADIANS,t)}opposite_nonneg(){return new j(this.angle+K,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}
negate(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(-this.angle,exports.Unit.RADIANS,t)}negate_nonneg(){return new j(-this.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}multiply(t,e=exports.Mode.RANGE_LIMIT_SIGNED){return new j(this.angle*t,exports.Unit.RADIANS,e)}multiply_nonneg(t){return new j(this.angle*t,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}divide(t,e=exports.Mode.RANGE_LIMIT_SIGNED){return new j(this.angle/t,exports.Unit.RADIANS,e)}divide_nonneg(t){
return new j(this.angle/t,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}toString(t,e){return j.toStringAux(this.degrees,"°","'",'"',t,e)}toSuffixedString(e,n,s,r){return s=-17&(s||0),j.toStringAux(t(this.degrees),"°","'",'"',s,r)+(this.degrees<0?n:e)}toHourString(t,e){return t=-3&(t||0),j.toStringAux(this.hours,"h","m","s",t,e)}toTimeString(t,e){return 8&(t=-3&(t||0))||(t|=4),j.toStringAux(this.hours,":",4===t?"":":","",t,e,2)}static toStringAux(e,n,s,r,i,o,a=0){const h=!!(12&(i=i||0))
;1&i?a=2:2&i&&(a=3),null==o&&(o=null!=i&&h?0:3);const u=Math.sign(e);let x;if(e=t(e),h){const t=F(10,o);if(4&i){let r=H(60*e*t)/t;e=_(r/60),r%=60,x=e+n+(r<10?"0":"")+r.toFixed(o)+s}else{let i=H(3600*e*t)/t,a=_(i/60);i%=60,e=_(a/60),a%=60,x=e+n+(a<10?"0":"")+a+s+(i<10?"0":"")+i.toFixed(o)+r}}else x=e.toFixed(o)+n;if(a){const t=a-/^(\d+)\D/.exec(x)[1].length;for(let e=0;e<t;++e)x="0"+x}return u<0?x="-"+x:16&i&&(x="+"+x),x}}j=rt,W=function(t){const e=(t||"").split(/([-+hms'"’”°:new])/i)
;let n=1,s=exports.Unit.DEGREES;e[0]||"+"!==e[1]&&"-"!==e[1]||("-"===e[1]&&(n=-1),e.splice(0,2));const r=e.filter(((t,e)=>e%2==0)).map((t=>parseFloat(t.trim()))),i=e.filter(((t,e)=>e%2==1)).map((t=>t.trim().toLowerCase()));return r.length>1&&isNaN(r.at(-1))&&r.splice(r.length-1,1),r.findIndex((t=>isNaN(t)))>=0?null:(i.find((t=>"h"===t||"m"===t||":"===t))?s=exports.Unit.HOURS:"s"===i.at(-1)&&(n*=-1),new j(n*(r[0]+(r[1]||0)/60+(r[2]||0)/3600),s,exports.Mode.RANGE_UNLIMITED))},rt.ZERO=new j(0),
rt.RIGHT=new j(tt),rt.STRAIGHT=new j(K);const it=.381966011250105;class ot{constructor(t=0,e=0,n=exports.Unit.RADIANS,s){null==s&&(s=n),this._longitude="number"==typeof t?new rt(t,n,exports.Mode.RANGE_LIMIT_NONNEGATIVE):t,this._latitude="number"==typeof e?new rt(e,s):e}get longitude(){return this._longitude}get rightAscension(){return this._longitude}get altitude(){return this._latitude}get azimuth(){return this._longitude}get latitude(){return this._latitude}get declination(){return this._latitude}
distanceFrom(n){let s=e(S(this._latitude.sin*n._latitude.sin+this._latitude.cos*n._latitude.cos*this._longitude.subtract(n._longitude).cos));return s=t(v(s,$)),new rt(s)}toString(){return`lon: ${this.longitude}, lat: ${this.latitude}`}}class at extends ot{static convertRectangular(t,e,n){let s;if("number"==typeof t){if(s=t,void 0===e||void 0===n)throw new Error("Invalid arguments")}else s=t.x,e=t.y,n=t.z;return new at(rt.atan2_nonneg(e,s),rt.atan2(n,Math.sqrt(s*s+e*e)),Math.sqrt(s*s+e*e+n*n))}
static from2D(t,e){return new at(t.longitude,t.latitude,e)}constructor(t,e,n=0,s,r){super(t,e,s,r),this._radius=n}get radius(){return this._radius}get xyz(){return this._xyz||(this._xyz={x:this._radius*this._latitude.cos*this._longitude.cos,y:this._radius*this._latitude.cos*this._longitude.sin,z:this._radius*this._latitude.sin}),this._xyz}translate(t){
const e=t.longitude,n=t.latitude,s=t.radius,r=this.longitude,i=this.latitude,o=this.radius,a=o*i.cos*r.cos-s*n.cos*e.cos,h=o*i.cos*r.sin-s*n.cos*e.sin,u=o*i.sin-s*n.sin;return at.convertRectangular(a,h,u)}toString(){return super.toString()+", rad: "+this.radius.toFixed(5)}}exports.Angle=rt,exports.E=l,exports.FMT_DD=1,exports.FMT_DDD=2,exports.FMT_HH=1,exports.FMT_MINS=4,exports.FMT_SECS=8,exports.FMT_SIGNED=16,exports.HALF_PI=U,exports.LN10=f,exports.LN2=R,exports.LOG10E=T,exports.LOG2E=G,
exports.MinMaxFinder=class{constructor(t,e,n,s,r,i){this.minMaxSeekingFunction=t,this.tolerance=e,this.maxIterations=n,this.xa=s,this.xb=r,this.xc=i,this._iterationCount=0,this.isMin=!0,this._resolved=!1}getXAtMinMax(){if(null!=this._x)return this._x;let e,n,s,r,i,o,a,h,u,x,c,l,p,M,_,g,N=0,d=0,I=1;for(e=Math.min(this.xa,this.xc),n=Math.max(this.xa,this.xc),_=M=p=this.xb,this.fx=this.minMaxSeekingFunction(_),this.fx>this.minMaxSeekingFunction(this.xa)?(this.isMin=!1,this.fx*=-1,I=-1):this.isMin=!0,
o=i=this.fx,this._iterationCount=0;++this._iterationCount<=this.maxIterations;){if(g=.5*(e+n),x=this.tolerance*t(_)+1e-20,c=2*x,t(_-g)<=c-.5*(n-e))return this.fx*=I,this._resolved=!0,this._x=_,_;t(d)>x?(u=(_-M)*(this.fx-i),h=(_-p)*(this.fx-o),a=(_-p)*h-(_-M)*u,h=2*(h-u),h>0&&(a=-a),h=t(h),s=d,d=N,t(a)>=t(.5*h*s)||a<=h*(e-_)||a>=h*(n-_)?(d=_>=g?e-_:n-_,N=it*d):(N=a/h,l=_+N,(l-e<c||n-l<c)&&(N=Math.sign(g-_)*x))):(d=_>=g?e-_:n-_,N=it*d),l=t(N)>=x?_+N:_+Math.sign(N)*x,r=I*this.minMaxSeekingFunction(l),
r<=this.fx?(l>=_?e=_:n=_,p=M,M=_,_=l,i=o,o=this.fx,this.fx=r):(l<_?e=l:n=l,r<=o||M===_?(p=M,M=l,i=o,o=r):(r<=i||p===_||p===M)&&(p=l,i=r))}return this.fx*=I,this._x=_,_}get foundMaximum(){return null==this._x&&this.getXAtMinMax(),!this.isMin}get foundMinimum(){return null==this._x&&this.getXAtMinMax(),this.isMin}get lastY(){return null==this._x&&this.getXAtMinMax(),this.fx}get iterationCount(){return null==this._x&&this.getXAtMinMax(),this._iterationCount}get resolved(){
return null==this._x&&this.getXAtMinMax(),this._resolved&&isFinite(this.fx)&&!isNaN(this.fx)}},exports.PI=b,exports.SQRT1_2=X,exports.SQRT2=Z,exports.SphericalPosition=ot,exports.SphericalPosition3D=at,exports.TWO_PI=$,exports.ZeroFinder=class{constructor(t,e,n,s,r,i,o,a){this.zeroSeekingFunction=t,this.tolerance=e,this.maxIterations=n,this.x1=s,this.y1=r,this._iterationCount=0,this._resolved=!1,this.maxError=0,null==o?(this.x2=r,this.y1=t(s),this.y2=t(r),null!=i&&(this.maxError=i)):(this.x2=i,this.y2=o,
this.maxError=a||0)}getXAtZero(){if(null!=this._x)return this._x;let t,e=0,n=this.maxIterations/2,s=!0,r=0;this._iterationCount=1;t:for(;!this._resolved;){for(--this._iterationCount;++this._iterationCount<=n&&!this._resolved;){const n=(this.y2-this.y1)/(this.x2-this.x1);if(t=n?this.x1-this.y1/n:(this.x1+this.x2)/2,this.y=this.zeroSeekingFunction(t),this.y===e){if(++r>=3){s=!1;break}}else r=0;if(e=this.y,Math.abs(this.y)<=this.tolerance){this._resolved=Math.abs(this.x2-this.x1)<=this.tolerance,
this.xForY=this._x=t;break}this.y1<this.y2&&this.y<0||this.y1>this.y2&&this.y>0?(this.x1=t,this.y1=this.y):(this.x2=t,this.y2=this.y)}if(this._resolved||!s)break;{let e=this.x1,r=this.x2;for(--this._iterationCount,s=!1,n=this.maxIterations;++this._iterationCount<=n;){const s=(r-e)/10;let i=1,o=t=e,a=this.zeroSeekingFunction(t),h=Math.abs(a),u=Math.sign(a);do{if(Math.abs(a)<=this.tolerance&&s<=this.tolerance){this._resolved=!0,this.y=a,this.xForY=t;break t}t+=s,a=this.zeroSeekingFunction(t),
Math.abs(a)<h&&(o=t,h=Math.abs(a));if(Math.sign(a)!==u){e=t-s,r=t,t-=s/2;break}}while(++i<=10);if(i>10){if(!(this.maxError&&h<this.maxError)){n=this._iterationCount;break}e=o-s,r=h+s,n===this.maxIterations&&(n=Math.min(this._iterationCount+2,this.maxIterations))}}this.xForY!==t&&(this.y=this.zeroSeekingFunction(t))}}return this.maxError&&Math.abs(this.y)>this.maxError&&(t=NaN,this.y=NaN),this._x=t,t}get lastY(){return null==this._x&&this.getXAtZero(),this.y}get iterationCount(){
return null==this._x&&this.getXAtZero(),this._iterationCount}get resolved(){return null==this._x&&this.getXAtZero(),this._resolved&&isFinite(this.y)&&!isNaN(this.y)}},exports.abs=t,exports.acos=e,exports.acos_deg=function(t){return Math.acos(t)/Math.PI*180},exports.acosh=n,exports.acot=function(t){return Math.PI/2-Math.atan(t)},exports.acot2=function(t,e){return Math.PI/2-Math.atan2(t,e)},exports.acot2_deg=function(t,e){return 180-Math.atan2(t,e)/Math.PI*180},exports.acot_deg=function(t){
return 180-Math.atan(t)/Math.PI*180},exports.asin=s,exports.asin_deg=function(t){return Math.asin(t)/Math.PI*180},exports.asinh=r,exports.atan=i,exports.atan2=o,exports.atan2_deg=function(t,e){return Math.atan2(t,e)/Math.PI*180},exports.atan_deg=function(t){return Math.atan(t)/Math.PI*180},exports.atanh=a,exports.cbrt=h,exports.ceil=function(t,e=1){return 1===e?Math.ceil(t):-(-t-C(-t,e))},exports.clz32=u,exports.convertFromRadians=st,exports.convertToRadians=nt,exports.cos=x,exports.cos_deg=function(t){
return Math.cos(t/180*Math.PI)},exports.cosh=c,exports.div_rd=function(t,e){return Math.floor(t/e)},exports.div_tt0=function(t,e){const n=t/e;return n>=0?Math.floor(n):Math.ceil(n)},exports.exp=p,exports.expm1=M,exports.floor=_,exports.fround=g,exports.hypot=N,exports.imul=d,exports.interpolate=I,exports.interpolateModular=function(t,e,n,s,r,i,o=!1){const a=i/2;for((s<0||s>=i)&&(s=C(s,i));r<s-a;)r+=i;for(;r>=s+a;)r-=i;let h=I(t,e,n,s,r);return o&&(h<a||h>=a)?h=v(h,i):(h<0||h>=i)&&(h=C(h,i)),h},
exports.interpolateTabular=function(t,e,n,s=0){const r=Math.min(t.length,e.length);if(s<=0)return A(t,e,n,0,r-1);let i=-1,o=-1;for(let s=0;s<r;++s){const r=t[s];if(r===n)return e[s];if(r<n)i=s;else if(r>n){o=s;break}}if(i<0)return e[0];if(o<0)return e[r-1];const a=t[i],h=t[o],u=(n-a)/(h-a);return E(t,e,n,s,a)*(1-u)+E(t,e,n,s,h)*u},exports.intersects=function(t,e){return t.x<e.x+e.w&&t.x+t.w>e.x&&t.y<e.y+e.h&&t.y+t.h>e.y},exports.irandom=function(t,e){
return 1===arguments.length?1+Math.floor(Math.random()*t):t+Math.floor(Math.random()*(e-t+1))},exports.limitNeg1to1=S,exports.log=m,exports.log10=D,exports.log1p=w,exports.log2=O,exports.max=y,exports.min=L,exports.mod=C,exports.mod2=v,exports.pow=F,exports.random=function(t,e){switch(arguments.length){case 1:return Math.random()*t;case 2:return t+Math.random()*(e-t)}return Math.random()},exports.round=H,exports.sign=P,exports.signZN=function(t){return t>0?1:-1},exports.signZP=function(t){return t<0?-1:1},
exports.sin=k,exports.sin_deg=function(t){return Math.sin(t/180*Math.PI)},exports.sinh=V,exports.sqrt=z,exports.squared=function(t){return t*t},exports.tan=q,exports.tan_deg=function(t){return Math.tan(t/180*Math.PI)},exports.tanh=Y,exports.to_degree=function(t){return 180*t/Math.PI},exports.to_radian=function(t){return t*Math.PI/180},exports.trunc=Q,exports.union=function(t,e){const n=L(t.x,e.x),s=L(t.y,e.y);return{x:n,y:s,w:y(t.x+t.w,e.x+e.w)-n,h:y(t.y+t.h,e.y+e.h)-s}};
//# sourceMappingURL=index.min.cjs.map