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