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