@tubular/math
Version:
Miscellaneous math functions
29 lines (28 loc) • 13.2 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t=Math.abs,e=Math.acos,n=Math.acosh;const r=Math.asin,s=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 p=Math.exp,M=Math.expm1;function _(t,e=1){return 1===e?Math.floor(t):t-D(t,e)}const l=Math.fround,N=Math.hypot,I=Math.imul;function g(t,e,n,r,s){return t===n?r:r+(e-t)*(s-r)/(n-t)}function d(t,e,n,r,s){let i=0;for(let o=r;o<=s;++o){let a=1
;for(let e=r;e<=s;++e)e!==o&&(a*=(n-t[e])/(t[o]-t[e]));i+=a*e[o]}return i}function A(t,e,n,r,s){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>=s-r&&(o=e),n>=s+r){a=e;break}}return d(t,e,n,Math.max(o,0),a)}function E(t,e=.01){return t<-1-e?-1:t>1+e?1:t<-1?-1:t>1?1:t}const S=Math.log,R=Math.log10,f=Math.log1p,G=Math.log2,T=Math.max,U=Math.min;function D(t,e){const n=t%e;return n<0&&e>0||n>0&&e<0?e+n:n}function m(t,e){let n=t-Math.floor(t/e)*e
;return n>=e/2&&(n-=e),n}const O=Math.pow;function w(t,e=1){return 1===e?Math.round(t):t+e/2-D(t+e/2,e)}const y=Math.sign;const L=Math.sin,C=Math.sinh;const P=Math.sqrt;const F=Math.tan,H=Math.tanh;const b=Math.trunc,v=2*Math.PI;var V,k;exports.Unit=void 0,(V=exports.Unit||(exports.Unit={}))[V.RADIANS=0]="RADIANS",V[V.DEGREES=1]="DEGREES",V[V.ARC_MINUTES=2]="ARC_MINUTES",V[V.ARC_SECONDS=3]="ARC_SECONDS",V[V.HOURS=4]="HOURS",V[V.HOUR_ANGLE_MINUTES=5]="HOUR_ANGLE_MINUTES",
V[V.HOUR_ANGLE_SECONDS=6]="HOUR_ANGLE_SECONDS",V[V.ROTATIONS=7]="ROTATIONS",V[V.GRADS=8]="GRADS",exports.Mode=void 0,(k=exports.Mode||(exports.Mode={}))[k.RANGE_LIMIT_SIGNED=0]="RANGE_LIMIT_SIGNED",k[k.RANGE_LIMIT_NONNEGATIVE=1]="RANGE_LIMIT_NONNEGATIVE",k[k.RANGE_UNLIMITED=2]="RANGE_UNLIMITED";const z=Math.PI,q=z/2,Z=2*z;function X(t,e){switch(e){case exports.Unit.RADIANS:return t;case exports.Unit.DEGREES:return t/180*z;case exports.Unit.ARC_MINUTES:return t/10800*z;case exports.Unit.ARC_SECONDS:
return t/648e3*z;case exports.Unit.HOURS:return t/12*z;case exports.Unit.HOUR_ANGLE_MINUTES:return t/720*z;case exports.Unit.HOUR_ANGLE_SECONDS:return t/43200*z;case exports.Unit.ROTATIONS:return t*Z;case exports.Unit.GRADS:return t/200*z}return NaN}function Y(t,e){switch(e){case exports.Unit.RADIANS:return t;case exports.Unit.DEGREES:return 180*t/z;case exports.Unit.ARC_MINUTES:return 10800*t/z;case exports.Unit.ARC_SECONDS:return 648e3*t/z;case exports.Unit.HOURS:return 12*t/z
;case exports.Unit.HOUR_ANGLE_MINUTES:return 720*t/z;case exports.Unit.HOUR_ANGLE_SECONDS:return 43200*t/z;case exports.Unit.ROTATIONS:return t/Z;case exports.Unit.GRADS:return 200*t/z}return NaN}class j{constructor(t=0,e,n=exports.Mode.RANGE_LIMIT_SIGNED){this.cached_sin=2,this.cached_cos=2,this.cached_tan=0,void 0===e&&(e=exports.Unit.RADIANS),n===exports.Mode.RANGE_LIMIT_SIGNED?this.angle=m(X(t,e),Z):n===exports.Mode.RANGE_LIMIT_NONNEGATIVE?this.angle=D(X(t,e),Z):this.angle=X(t,e)}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)}get radians(){
return this.angle}get degrees(){return Y(this.angle,exports.Unit.DEGREES)}get arcMinutes(){return Y(this.angle,exports.Unit.ARC_MINUTES)}get arcSeconds(){return Y(this.angle,exports.Unit.ARC_SECONDS)}get hours(){return Y(this.angle,exports.Unit.HOURS)}get rotations(){return Y(this.angle,exports.Unit.ROTATIONS)}get grads(){return Y(this.angle,exports.Unit.GRADS)}getAngle(t=exports.Unit.RADIANS){return Y(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(q-this.angle,exports.Unit.RADIANS,t)}complement_nonneg(){return new j(q-this.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}supplement(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(z-this.angle,exports.Unit.RADIANS,t)}supplement_nonneg(){return new j(z-this.angle,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)
}opposite(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(this.angle+z,exports.Unit.RADIANS,t)}opposite_nonneg(){return new j(this.angle+z,exports.Unit.RADIANS,exports.Mode.RANGE_LIMIT_NONNEGATIVE)}negate(t=exports.Mode.RANGE_LIMIT_SIGNED){return new j(-this.angle,exports.Unit.RADIANS,void 0===t?exports.Mode.RANGE_UNLIMITED: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,r,s){
return j.toStringAux(t(this.degrees),"°","'",'"',r,s)+(this.degrees<0?n:e)}toHourString(t,e){return j.toStringAux(this.hours,"h","m","s",t,e)}toTimeString(t,e){return j.toStringAux(this.hours,":",4===t?"":":","",t,e,2)}static toStringAux(e,n,r,s,i,o,a=0){const h=0!=(12&(i=i||0));0!=(1&i)?a=2:0!=(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=O(10,o);if(0!=(4&i)){let s=w(60*e*t)/t;e=_(s/60),s%=60,x=e+n+(s<10?"0":"")+s.toFixed(o)+r}else{let i=w(3600*e*t)/t,a=_(i/60)
;i%=60,e=_(a/60),a%=60,x=e+n+(a<10?"0":"")+a+r+(i<10?"0":"")+i.toFixed(o)+s}}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:0!=(16&i)&&(x="+"+x),x}}j.ZERO=new j(0),j.RIGHT=new j(q),j.STRAIGHT=new j(z);class W{constructor(t=0,e=0,n=exports.Unit.RADIANS,r=exports.Unit.RADIANS){this._longitude="number"==typeof t?new j(t,n,exports.Mode.RANGE_LIMIT_NONNEGATIVE):t,this._latitude="number"==typeof e?new j(e,r):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 r=e(E(this._latitude.sin*n._latitude.sin+this._latitude.cos*n._latitude.cos*this._longitude.subtract(n._longitude).cos));return r=t(m(r,v)),new j(r)}toString(){return"lon: "+this.longitude+", lat: "+this.latitude}}class B extends W{constructor(t,e,n=0,r,s){super(t,e,r,s),
this._radius=n}static convertRectangular(t,e,n){let r;if("number"==typeof t){if(r=t,void 0===e||void 0===n)throw new Error("Invalid arguments")}else r=t.x,e=t.y,n=t.z;return new B(j.atan2_nonneg(e,r),j.atan2(n,Math.sqrt(r*r+e*e)),Math.sqrt(r*r+e*e+n*n))}static from2D(t,e){return new B(t.longitude,t.latitude,e)}get radius(){return this._radius}get xyz(){return{x:this._radius*this._latitude.cos*this._longitude.cos,y:this._radius*this._latitude.cos*this._longitude.sin,z:this._radius*this._latitude.sin}}
translate(t){const e=t.longitude,n=t.latitude,r=t.radius,s=this.longitude,i=this.latitude,o=this.radius,a=o*i.cos*s.cos-r*n.cos*e.cos,h=o*i.cos*s.sin-r*n.cos*e.sin,u=o*i.sin-r*n.sin;return B.convertRectangular(a,h,u)}toString(){return super.toString()+", rad: "+this.radius.toFixed(5)}}exports.Angle=j,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=q,exports.MinMaxFinder=class{constructor(t,e,n,r,s,i){
this.minMaxSeekingFunction=t,this.tolerance=e,this.maxIterations=n,this.ax=r,this.bx=s,this.cx=i,this._iterationCount=0,this.isMin=!0}getXAtMinMax(){let e,n,r,s,i,o,a,h,u,x,c,p,M,_,l,N,I=0,g=0,d=1;for(e=this.ax<this.cx?this.ax:this.cx,n=this.ax>this.cx?this.ax:this.cx,l=_=M=this.bx,this.fx=this.minMaxSeekingFunction(l),this.fx>this.minMaxSeekingFunction(this.ax)?(this.isMin=!1,this.fx*=-1,d=-1):this.isMin=!0,o=i=this.fx,this._iterationCount=0;++this._iterationCount<=this.maxIterations;){if(N=.5*(e+n),
x=this.tolerance*t(l)+1e-20,c=2*x,t(l-N)<=c-.5*(n-e))return this.fx*=d,l;t(g)>x?(u=(l-_)*(this.fx-i),h=(l-M)*(this.fx-o),a=(l-M)*h-(l-_)*u,h=2*(h-u),h>0&&(a=-a),h=t(h),r=g,g=I,t(a)>=t(.5*h*r)||a<=h*(e-l)||a>=h*(n-l)?(g=l>=N?e-l:n-l,I=.381966011250105*g):(I=a/h,p=l+I,(p-e<c||n-p<c)&&(I=Math.sign(N-l)*x))):(g=l>=N?e-l:n-l,I=.381966011250105*g),p=t(I)>=x?l+I:l+Math.sign(I)*x,s=d*this.minMaxSeekingFunction(p),s<=this.fx?(p>=l?e=l:n=l,M=_,_=l,l=p,i=o,o=this.fx,this.fx=s):(p<l?e=p:n=p,s<=o||_===l?(M=_,_=p,i=o,
o=s):(s<=i||M===l||M===_)&&(M=p,i=s))}return this.fx*=d,l}get foundMaximum(){return!this.isMin}get foundMinimum(){return this.isMin}get lastY(){return this.fx}get iterationCount(){return this._iterationCount}},exports.PI=z,exports.SphericalPosition=W,exports.SphericalPosition3D=B,exports.TWO_PI=Z,exports.ZeroFinder=class{constructor(t,e,n,r,s,i,o){this.zeroSeekingFunction=t,this.tolerance=e,this.maxIterations=n,this.x1=r,this.y1=s,this._iterationCount=0,null==i?(this.x2=s,this.y1=t(r),
this.y2=t(s)):(this.x2=i,this.y2=o)}getXAtZero(){let t;for(this._iterationCount=0;++this._iterationCount<=this.maxIterations&&(t=this.x1-this.y1/(this.y2-this.y1)*(this.x2-this.x1),this.y=this.zeroSeekingFunction(t),!(Math.abs(this.y)<=this.tolerance));)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);return t}get lastY(){return this.y}get iterationCount(){return this._iterationCount}},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=r,exports.asin_deg=function(t){return Math.asin(t)/Math.PI*180},exports.asinh=s,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-D(-t,e))},exports.clz32=u,exports.convertFromRadians=Y,exports.convertToRadians=X,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=l,exports.hypot=N,exports.imul=I,exports.interpolate=g,exports.interpolateModular=function(t,e,n,r,s,i,o=!1){const a=i/2;for((r<0||r>=i)&&(r=D(r,i));s<r-a;)s+=i;for(;s>=r+a;)s-=i;let h=g(t,e,n,r,s);return o&&(h<a||h>=a)?h=m(h,i):(h<0||h>=i)&&(h=D(h,i)),h},exports.interpolateTabular=function(t,e,n,r){const s=Math.min(t.length,e.length);if(r<=0)return d(t,e,n,0,s-1);let i=-1,o=-1;for(let r=0;r<s;++r){const s=t[r];if(s===n)return e[r];if(s<n)i=r;else if(s>n){o=r;break}}if(i<0)return e[0]
;if(o<0)return e[s-1];const a=t[i],h=t[o],u=(n-a)/(h-a);return A(t,e,n,r,a)*(1-u)+A(t,e,n,r,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=E,exports.log=S,exports.log10=R,exports.log1p=f,exports.log2=G,exports.max=T,exports.min=U,exports.mod=D,exports.mod2=m,exports.pow=O,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=w,exports.sign=y,exports.signZN=function(t){return t>0?1:-1},exports.signZP=function(t){return t<0?-1:1},exports.sin=L,exports.sin_deg=function(t){return Math.sin(t/180*Math.PI)},exports.sinh=C,exports.sqrt=P,exports.squared=function(t){return t*t},exports.tan=F,exports.tan_deg=function(t){return Math.tan(t/180*Math.PI)},exports.tanh=H,exports.to_degree=function(t){
return 180*t/Math.PI},exports.to_radian=function(t){return t*Math.PI/180},exports.trunc=b,exports.union=function(t,e){const n=U(t.x,e.x),r=U(t.y,e.y);return{x:n,y:r,w:T(t.x+t.w,e.x+e.w)-n,h:T(t.y+t.h,e.y+e.h)-r}};
//# sourceMappingURL=index.js.map