@thi.ng/math
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.5.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/math@1.4.2...@thi.ng/math@1.5.0) (2019-11-09) | ||
### Features | ||
* **math:** add mixCubicHermite & tangent fns ([d6b4b37](https://github.com/thi-ng/umbrella/commit/d6b4b3710b80fa1366cb40c193ad745bc63d4253)) | ||
## [1.4.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/math@1.4.1...@thi.ng/math@1.4.2) (2019-07-31) | ||
@@ -8,0 +19,0 @@ |
@@ -46,3 +46,3 @@ /** | ||
*/ | ||
export declare const minimaIndices: (values: number[], from?: number, to?: number) => IterableIterator<number>; | ||
export declare const minimaIndices: (values: number[], from?: number, to?: number) => Generator<number, void, unknown>; | ||
/** | ||
@@ -57,2 +57,2 @@ * Returns an iterator yielding all maxima indices in given `values` | ||
*/ | ||
export declare const maximaIndices: (values: number[], from?: number, to?: number) => IterableIterator<number>; | ||
export declare const maximaIndices: (values: number[], from?: number, to?: number) => Generator<number, void, unknown>; |
@@ -281,2 +281,19 @@ 'use strict'; | ||
}; | ||
const mixHermite = (a, b, c, d, t) => { | ||
const y1 = 0.5 * (c - a); | ||
const y2 = 1.5 * (b - c) + 0.5 * (d - a); | ||
return ((y2 * t + a - b + y1 - y2) * t + y1) * t + b; | ||
}; | ||
const mixCubicHermite = (a, ta, b, tb, t) => { | ||
const s = t - 1; | ||
const t2 = t * t; | ||
const s2 = s * s; | ||
const h00 = (1 + 2 * t) * s2; | ||
const h10 = t * s2; | ||
const h01 = t2 * (3 - 2 * t); | ||
const h11 = t2 * s; | ||
return h00 * a + h10 * ta + h01 * b + h11 * tb; | ||
}; | ||
const tangentCardinal = (prev, next, scale = 0.5, ta = 0, tc = 2) => scale * ((next - prev) / (tc - ta)); | ||
const tangentDiff3 = (prev, curr, next, ta = 0, tb = 1, tc = 2) => 0.5 * ((next - curr) / (tc - tb) + (curr - prev) / (tb - ta)); | ||
const tween = (f, from, to) => (t) => mix(from, to, f(t)); | ||
@@ -497,2 +514,4 @@ const circular = (t) => { | ||
exports.mixCubic = mixCubic; | ||
exports.mixCubicHermite = mixCubicHermite; | ||
exports.mixHermite = mixHermite; | ||
exports.mixQuadratic = mixQuadratic; | ||
@@ -554,2 +573,4 @@ exports.muli16 = muli16; | ||
exports.subu8 = subu8; | ||
exports.tangentCardinal = tangentCardinal; | ||
exports.tangentDiff3 = tangentDiff3; | ||
exports.trunc = trunc; | ||
@@ -556,0 +577,0 @@ exports.tween = tween; |
@@ -1,1 +0,1 @@ | ||
!function(t,a){"object"==typeof exports&&"undefined"!=typeof module?a(exports):"function"==typeof define&&define.amd?define(["exports"],a):a(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.math={}))}(this,function(t){"use strict";const a=Math.PI,i=2*a,s=a/2,n=a/3,e=a/4,r=a/6,o=1/a,h=1/i,u=1/s,c=a/180,M=180/a,d=(1+Math.sqrt(5))/2,f=Math.SQRT2,l=Math.sqrt(3),m=f/2,b=l/2;var p;(p=t.Crossing||(t.Crossing={}))[p.EQUAL=0]="EQUAL",p[p.FLAT=1]="FLAT",p[p.UNDER=2]="UNDER",p[p.OVER=3]="OVER",p[p.OTHER=4]="OTHER";const x=t=>(t%=i,t<0?i+t:t),g=t=>(t=Math.abs(t),t>a?i-t:t),I=t=>{const a=t*t;return.99940307+a*(.03679168*a-.49558072)},R=t=>{switch((t%=i)<0&&(t=-t),t*u|0){case 0:return I(t);case 1:return-I(a-t);case 2:return-I(t-a);default:return I(i-t)}},T=Math.abs,E=Math.max,v=(t,a,i=1e-6)=>T(t-a)<=i*E(1,T(t),T(a)),A=(t,a,i,s)=>t<i&&a>s,D=(t,a,i,s)=>t>i&&a<s,P=(t,a,i)=>t>a&&a<i,_=(t,a,i)=>t<a&&a>i,q=(t,a,i=0,s=a.length)=>{s--;for(let n=i+1;n<s;n++)if(t(a[n-1],a[n],a[n+1]))return n;return-1},S=(t,a=0,i=t.length)=>q(P,t,a,i);function*w(t,a,i=0,s=a.length){for(;i<s;){const n=t(a,i,s);if(n<0)return;yield n,i=n+1}}const C=t=>t<0?0:t>1?1:t,H=t=>t<-1?-1:t>1?1:t,Q=(t,a,i)=>O(t,a,-i),O=(t,a,i)=>{const s=Math.exp(t*i),n=Math.exp(a*i);return(t*s+a*n)/(s+n)},U=(t,a,i)=>i!==a?(t-a)/(i-a):0,y=255,L=65535,F=t=>(t&=y,128&t?t|~y:t),N=t=>(t&=L,32768&t?t|~L:t),V=(t,a,i,s=16,n=8,e=0,r=1,o=1e-6)=>{if(n<=0)return(e+r)/2;const h=(r-e)/s;let u=e,c=1/0;for(let n=0;n<=s;n++){const s=e+n*h,r=a(i,t(s));if(r<c){if(r<=o)return s;c=r,u=s}}return V(t,a,i,s,n-1,Math.max(u-h,0),Math.min(u+h,1))},j=(t,a,i)=>t+(a-t)*i,G=t=>t-Math.floor(t);t.DEG2RAD=c,t.EPS=1e-6,t.HALF_PI=s,t.INV_HALF_PI=u,t.INV_PI=o,t.INV_TAU=h,t.PHI=d,t.PI=a,t.QUARTER_PI=e,t.RAD2DEG=M,t.SIXTH=1/6,t.SIXTH_PI=r,t.SQRT2=f,t.SQRT2_2=m,t.SQRT2_3=b,t.SQRT3=l,t.TAU=i,t.THIRD=1/3,t.THIRD_PI=n,t.TWO_THIRD=2/3,t.absDiff=(t,a)=>Math.abs(t-a),t.absInnerAngle=g,t.absMax=(t,a)=>Math.abs(t)>Math.abs(a)?t:a,t.absMin=(t,a)=>Math.abs(t)<Math.abs(a)?t:a,t.absTheta=x,t.addi16=(t,a)=>N((0|t)+(0|a)),t.addi32=(t,a)=>(0|t)+(0|a)|0,t.addi8=(t,a)=>F((0|t)+(0|a)),t.addu16=(t,a)=>(t&L)+(a&L)&L,t.addu32=(t,a)=>(t>>>0)+(a>>>0)>>>0,t.addu8=(t,a)=>(t&y)+(a&y)&y,t.andi16=(t,a)=>N((0|t)&(0|a)),t.andi32=(t,a)=>(0|t)&(0|a),t.andi8=(t,a)=>F((0|t)&(0|a)),t.andu16=(t,a)=>t&L&a&L&L,t.andu32=(t,a)=>(t>>>0&a>>>0)>>>0,t.andu8=(t,a)=>t&y&a&y&y,t.angleDist=(t,a)=>g(x(a%i-t%i)),t.atan2Abs=(t,a)=>x(Math.atan2(t,a)),t.bounce=(t,a,i)=>{const n=i*t;return 1-a*Math.sin(n)/n*Math.cos(i*s)},t.circular=t=>(t=1-t,Math.sqrt(1-t*t)),t.clamp=(t,a,i)=>t<a?a:t>i?i:t,t.clamp01=C,t.clamp11=H,t.classifyCrossing=(t,a,i,s,n=1e-6)=>A(t,a,i,s)?3:D(t,a,i,s)?2:v(t,i,n)&&v(a,s,n)?v(t,s,n)?1:0:4,t.cosine=t=>1-(.5*Math.cos(t*a)+.5),t.cossin=(t,a=1)=>[Math.cos(t)*a,Math.sin(t)*a],t.cot=t=>1/Math.tan(t),t.csc=t=>1/Math.sin(t),t.cubicPulse=(t,a,i)=>(i=Math.abs(i-a))>t?0:1-(i/=t)*i*(3-2*i),t.decimated=(t,a)=>Math.floor(a*t)/t,t.deg=t=>t*M,t.derivative=(t,a=1e-6)=>i=>(t(i+a)-t(i))/a,t.divi16=(t,a)=>N((0|t)/(0|a)),t.divi32=(t,a)=>(0|t)/(0|a)|0,t.divi8=(t,a)=>F((0|t)/(0|a)),t.divu16=(t,a)=>(t&L)/(a&L)&L,t.divu32=(t,a)=>(t>>>0)/(a>>>0)>>>0,t.divu8=(t,a)=>(t&y)/(a&y)&y,t.ease=(t,a)=>Math.pow(a,t),t.eqDelta=v,t.eqDeltaFixed=(t,a,i=1e-6)=>T(t-a)<=i,t.expStep=(t,a,i)=>1-Math.exp(-t*Math.pow(i,a)),t.fastCos=R,t.fastSin=t=>R(s-t),t.fit=(t,a,i,s,n)=>s+(n-s)*U(t,a,i),t.fit01=(t,a,i)=>a+(i-a)*C(t),t.fit10=(t,a,i)=>i+(a-i)*C(t),t.fit11=(t,a,i)=>a+(i-a)*(.5+.5*H(t)),t.fitClamped=(t,a,i,s,n)=>s+(n-s)*C(U(t,a,i)),t.fmod=(t,a)=>t-a*Math.floor(t/a),t.foldback=(t,a)=>a<-t||a>t?Math.abs(Math.abs((a-t)%(4*t))-2*t)-t:a,t.fract=G,t.gain=(t,a)=>a<.5?.5*Math.pow(2*a,t):1-.5*Math.pow(2-2*a,t),t.impulse=(t,a)=>{const i=t*a;return i*Math.exp(1-i)},t.inOpenRange=(t,a,i)=>t>a&&t<i,t.inRange=(t,a,i)=>t>=a&&t<=i,t.isCrossOver=A,t.isCrossUnder=D,t.isMaxima=_,t.isMinima=P,t.loc=(t,a,i)=>Math.sqrt(t*t+a*a-2*t*a*Math.cos(i)),t.lshifti16=(t,a)=>N((0|t)<<(0|a)),t.lshifti32=(t,a)=>(0|t)<<(0|a),t.lshifti8=(t,a)=>F((0|t)<<(0|a)),t.lshiftu16=(t,a)=>(t&L)<<(a&L)&L,t.lshiftu32=(t,a)=>t>>>0<<(a>>>0)>>>0,t.lshiftu8=(t,a)=>(t&y)<<(a&y)&y,t.max2id=(t,a)=>t>=a?0:1,t.max3id=(t,a,i)=>t>=a?t>=i?0:2:a>=i?1:2,t.max4id=(t,a,i,s)=>t>=a?t>=i?t>=s?0:3:i>=s?2:3:a>=i?a>=s?1:3:i>=s?2:3,t.maximaIndex=(t,a=0,i=t.length)=>q(_,t,a,i),t.maximaIndices=(t,a=0,i=t.length)=>w(S,t,a,i),t.min2id=(t,a)=>t<=a?0:1,t.min3id=(t,a,i)=>t<=a?t<=i?0:2:a<=i?1:2,t.min4id=(t,a,i,s)=>t<=a?t<=i?t<=s?0:3:i<=s?2:3:a<=i?a<=s?1:3:i<=s?2:3,t.minError=V,t.minimaIndex=S,t.minimaIndices=(t,a=0,i=t.length)=>w(S,t,a,i),t.mix=j,t.mixBilinear=(t,a,i,s,n,e)=>j(j(t,a,n),j(i,s,n),e),t.mixCubic=(t,a,i,s,n)=>{const e=n*n,r=1-n,o=r*r;return t*o*r+3*a*o*n+3*i*e*r+s*e*n},t.mixQuadratic=(t,a,i,s)=>{const n=1-s;return t*n*n+2*a*n*s+i*s*s},t.muli16=(t,a)=>N((0|t)*(0|a)),t.muli32=(t,a)=>(0|t)*(0|a)|0,t.muli8=(t,a)=>F((0|t)*(0|a)),t.mulu16=(t,a)=>(t&L)*(a&L)&L,t.mulu32=(t,a)=>(t>>>0)*(a>>>0)>>>0,t.mulu8=(t,a)=>(t&y)*(a&y)&y,t.norm=U,t.normCos=t=>{const a=t*t;return 1+a*(2*a-4)},t.noti16=t=>N(~t),t.noti32=t=>~t,t.noti8=t=>F(~t),t.notu16=t=>~t&L,t.notu32=t=>~t>>>0,t.notu8=t=>~t&y,t.ori16=(t,a)=>N(0|t|a),t.ori32=(t,a)=>0|t|a,t.ori8=(t,a)=>F(0|t|a),t.oru16=(t,a)=>(t&L|a&L)&L,t.oru32=(t,a)=>(t>>>0|a>>>0)>>>0,t.oru8=(t,a)=>(t&y|a&y)&y,t.parabola=(t,a)=>Math.pow(4*a*(1-a),t),t.quadrant=t=>x(t)*u|0,t.rad=t=>t*c,t.roundEps=(t,a=1e-6)=>{const i=G(t);return i<=a||i>=1-a?Math.round(t):t},t.roundTo=(t,a=1)=>Math.round(t/a)*a,t.rshifti16=(t,a)=>N((0|t)>>(0|a)),t.rshifti32=(t,a)=>(0|t)>>(0|a),t.rshifti8=(t,a)=>F((0|t)>>(0|a)),t.rshiftu16=(t,a)=>(t&L)>>>(a&L)&L,t.rshiftu32=(t,a)=>t>>>0>>>(a>>>0)>>>0,t.rshiftu8=(t,a)=>(t&y)>>>(a&y)&y,t.sclamp=(t,a,i,s)=>Q(O(t,a,s),i,s),t.sec=t=>1/Math.cos(t),t.sigmoid=(t,a)=>1/(1+Math.exp(-t*(2*a-1))),t.sigmoid11=(t,a)=>1/(1+Math.exp(-t*a)),t.sign=(t,a=1e-6)=>t>a?1:t<-a?-1:0,t.signExtend16=N,t.signExtend8=F,t.simplifyRatio=(t,a)=>{let i=Math.abs(t),s=Math.abs(a);for(;;){if(i<s){const t=i;i=s,s=t}const n=i%s;if(!n)return[t/s,a/s];i=n}},t.sinc=(t,i)=>(i=a*(t*i-1),Math.sin(i)/i),t.sincos=(t,a=1)=>[Math.sin(t)*a,Math.cos(t)*a],t.smax=O,t.smin=Q,t.smoothStep=(t,a,i)=>(3-2*(i=C((i-t)/(a-t))))*i*i,t.smootherStep=(t,a,i)=>(i=C((i-t)/(a-t)))*i*i*(i*(6*i-15)+10),t.solveCubic=(t,a,i,s,n=1e-9)=>{const e=t*t,r=a*a,o=a/(3*t),h=(3*t*i-r)/(3*e),u=(2*r*a-9*t*a*i+27*e*s)/(27*e*t);if(Math.abs(h)<n)return[Math.cbrt(-u)-o];if(Math.abs(u)<n)return h<0?[-Math.sqrt(-h)-o,-o,Math.sqrt(-h)-o]:[-o];{const t=u*u/4+h*h*h/27;if(Math.abs(t)<n)return[-1.5*u/h-o,3*u/h-o];if(t>0){const a=Math.cbrt(-u/2-Math.sqrt(t));return[a-h/(3*a)-o]}{const t=2*Math.sqrt(-h/3),a=Math.acos(3*u/h/t)/3,i=2*Math.PI/3;return[t*Math.cos(a)-o,t*Math.cos(a-i)-o,t*Math.cos(a-2*i)-o]}}},t.solveLinear=(t,a)=>-a/t,t.solveQuadratic=(t,a,i,s=1e-9)=>{const n=2*t;let e=a*a-4*t*i;return e<0?[]:e<s?[-a/n]:[(-a-(e=Math.sqrt(e)))/n,(-a+e)/n]},t.step=(t,a)=>a<t?0:1,t.subi16=(t,a)=>N((0|t)-(0|a)),t.subi32=(t,a)=>(0|t)-(0|a)|0,t.subi8=(t,a)=>F((0|t)-(0|a)),t.subu16=(t,a)=>(t&L)-(a&L)&L,t.subu32=(t,a)=>(t>>>0)-(a>>>0)>>>0,t.subu8=(t,a)=>(t&y)-(a&y)&y,t.trunc=t=>t<0?Math.ceil(t):Math.floor(t),t.tween=(t,a,i)=>s=>j(a,i,t(s)),t.wrap=(t,a,i)=>t<a?t-a+i:t>=i?t-i+a:t,t.wrap01=t=>t<0?t+1:t>=1?t-1:t,t.wrap11=t=>t<-1?t+2:t>=1?t-2:t,t.xori16=(t,a)=>N((0|t)^(0|a)),t.xori32=(t,a)=>(0|t)^(0|a),t.xori8=(t,a)=>F((0|t)^(0|a)),t.xoru16=(t,a)=>(t&L^a&L)&L,t.xoru32=(t,a)=>(t>>>0^a>>>0)>>>0,t.xoru8=(t,a)=>(t&y^a&y)&y,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,a){"object"==typeof exports&&"undefined"!=typeof module?a(exports):"function"==typeof define&&define.amd?define(["exports"],a):a(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.math={}))}(this,(function(t){"use strict";const a=Math.PI,i=2*a,n=a/2,s=a/3,e=a/4,r=a/6,o=1/a,h=1/i,u=1/n,c=a/180,M=180/a,d=(1+Math.sqrt(5))/2,f=Math.SQRT2,l=Math.sqrt(3),m=f/2,b=l/2;var x;(x=t.Crossing||(t.Crossing={}))[x.EQUAL=0]="EQUAL",x[x.FLAT=1]="FLAT",x[x.UNDER=2]="UNDER",x[x.OVER=3]="OVER",x[x.OTHER=4]="OTHER";const p=t=>(t%=i,t<0?i+t:t),g=t=>(t=Math.abs(t),t>a?i-t:t),I=t=>{const a=t*t;return.99940307+a*(.03679168*a-.49558072)},R=t=>{switch((t%=i)<0&&(t=-t),t*u|0){case 0:return I(t);case 1:return-I(a-t);case 2:return-I(t-a);default:return I(i-t)}},T=Math.abs,E=Math.max,v=(t,a,i=1e-6)=>T(t-a)<=i*E(1,T(t),T(a)),D=(t,a,i,n)=>t<i&&a>n,A=(t,a,i,n)=>t>i&&a<n,P=(t,a,i)=>t>a&&a<i,_=(t,a,i)=>t<a&&a>i,q=(t,a,i=0,n=a.length)=>{n--;for(let s=i+1;s<n;s++)if(t(a[s-1],a[s],a[s+1]))return s;return-1},C=(t,a=0,i=t.length)=>q(P,t,a,i);function*H(t,a,i=0,n=a.length){for(;i<n;){const s=t(a,i,n);if(s<0)return;yield s,i=s+1}}const S=t=>t<0?0:t>1?1:t,w=t=>t<-1?-1:t>1?1:t,Q=(t,a,i)=>O(t,a,-i),O=(t,a,i)=>{const n=Math.exp(t*i),s=Math.exp(a*i);return(t*n+a*s)/(n+s)},U=(t,a,i)=>i!==a?(t-a)/(i-a):0,y=255,L=65535,F=t=>(t&=y,128&t?t|~y:t),N=t=>(t&=L,32768&t?t|~L:t),V=(t,a,i,n=16,s=8,e=0,r=1,o=1e-6)=>{if(s<=0)return(e+r)/2;const h=(r-e)/n;let u=e,c=1/0;for(let s=0;s<=n;s++){const n=e+s*h,r=a(i,t(n));if(r<c){if(r<=o)return n;c=r,u=n}}return V(t,a,i,n,s-1,Math.max(u-h,0),Math.min(u+h,1))},j=(t,a,i)=>t+(a-t)*i,G=t=>t-Math.floor(t);t.DEG2RAD=c,t.EPS=1e-6,t.HALF_PI=n,t.INV_HALF_PI=u,t.INV_PI=o,t.INV_TAU=h,t.PHI=d,t.PI=a,t.QUARTER_PI=e,t.RAD2DEG=M,t.SIXTH=1/6,t.SIXTH_PI=r,t.SQRT2=f,t.SQRT2_2=m,t.SQRT2_3=b,t.SQRT3=l,t.TAU=i,t.THIRD=1/3,t.THIRD_PI=s,t.TWO_THIRD=2/3,t.absDiff=(t,a)=>Math.abs(t-a),t.absInnerAngle=g,t.absMax=(t,a)=>Math.abs(t)>Math.abs(a)?t:a,t.absMin=(t,a)=>Math.abs(t)<Math.abs(a)?t:a,t.absTheta=p,t.addi16=(t,a)=>N((0|t)+(0|a)),t.addi32=(t,a)=>(0|t)+(0|a)|0,t.addi8=(t,a)=>F((0|t)+(0|a)),t.addu16=(t,a)=>(t&L)+(a&L)&L,t.addu32=(t,a)=>(t>>>0)+(a>>>0)>>>0,t.addu8=(t,a)=>(t&y)+(a&y)&y,t.andi16=(t,a)=>N((0|t)&(0|a)),t.andi32=(t,a)=>(0|t)&(0|a),t.andi8=(t,a)=>F((0|t)&(0|a)),t.andu16=(t,a)=>t&L&a&L&L,t.andu32=(t,a)=>(t>>>0&a>>>0)>>>0,t.andu8=(t,a)=>t&y&a&y&y,t.angleDist=(t,a)=>g(p(a%i-t%i)),t.atan2Abs=(t,a)=>p(Math.atan2(t,a)),t.bounce=(t,a,i)=>{const s=i*t;return 1-a*Math.sin(s)/s*Math.cos(i*n)},t.circular=t=>(t=1-t,Math.sqrt(1-t*t)),t.clamp=(t,a,i)=>t<a?a:t>i?i:t,t.clamp01=S,t.clamp11=w,t.classifyCrossing=(t,a,i,n,s=1e-6)=>D(t,a,i,n)?3:A(t,a,i,n)?2:v(t,i,s)&&v(a,n,s)?v(t,n,s)?1:0:4,t.cosine=t=>1-(.5*Math.cos(t*a)+.5),t.cossin=(t,a=1)=>[Math.cos(t)*a,Math.sin(t)*a],t.cot=t=>1/Math.tan(t),t.csc=t=>1/Math.sin(t),t.cubicPulse=(t,a,i)=>(i=Math.abs(i-a))>t?0:1-(i/=t)*i*(3-2*i),t.decimated=(t,a)=>Math.floor(a*t)/t,t.deg=t=>t*M,t.derivative=(t,a=1e-6)=>i=>(t(i+a)-t(i))/a,t.divi16=(t,a)=>N((0|t)/(0|a)),t.divi32=(t,a)=>(0|t)/(0|a)|0,t.divi8=(t,a)=>F((0|t)/(0|a)),t.divu16=(t,a)=>(t&L)/(a&L)&L,t.divu32=(t,a)=>(t>>>0)/(a>>>0)>>>0,t.divu8=(t,a)=>(t&y)/(a&y)&y,t.ease=(t,a)=>Math.pow(a,t),t.eqDelta=v,t.eqDeltaFixed=(t,a,i=1e-6)=>T(t-a)<=i,t.expStep=(t,a,i)=>1-Math.exp(-t*Math.pow(i,a)),t.fastCos=R,t.fastSin=t=>R(n-t),t.fit=(t,a,i,n,s)=>n+(s-n)*U(t,a,i),t.fit01=(t,a,i)=>a+(i-a)*S(t),t.fit10=(t,a,i)=>i+(a-i)*S(t),t.fit11=(t,a,i)=>a+(i-a)*(.5+.5*w(t)),t.fitClamped=(t,a,i,n,s)=>n+(s-n)*S(U(t,a,i)),t.fmod=(t,a)=>t-a*Math.floor(t/a),t.foldback=(t,a)=>a<-t||a>t?Math.abs(Math.abs((a-t)%(4*t))-2*t)-t:a,t.fract=G,t.gain=(t,a)=>a<.5?.5*Math.pow(2*a,t):1-.5*Math.pow(2-2*a,t),t.impulse=(t,a)=>{const i=t*a;return i*Math.exp(1-i)},t.inOpenRange=(t,a,i)=>t>a&&t<i,t.inRange=(t,a,i)=>t>=a&&t<=i,t.isCrossOver=D,t.isCrossUnder=A,t.isMaxima=_,t.isMinima=P,t.loc=(t,a,i)=>Math.sqrt(t*t+a*a-2*t*a*Math.cos(i)),t.lshifti16=(t,a)=>N((0|t)<<(0|a)),t.lshifti32=(t,a)=>(0|t)<<(0|a),t.lshifti8=(t,a)=>F((0|t)<<(0|a)),t.lshiftu16=(t,a)=>(t&L)<<(a&L)&L,t.lshiftu32=(t,a)=>t>>>0<<(a>>>0)>>>0,t.lshiftu8=(t,a)=>(t&y)<<(a&y)&y,t.max2id=(t,a)=>t>=a?0:1,t.max3id=(t,a,i)=>t>=a?t>=i?0:2:a>=i?1:2,t.max4id=(t,a,i,n)=>t>=a?t>=i?t>=n?0:3:i>=n?2:3:a>=i?a>=n?1:3:i>=n?2:3,t.maximaIndex=(t,a=0,i=t.length)=>q(_,t,a,i),t.maximaIndices=(t,a=0,i=t.length)=>H(C,t,a,i),t.min2id=(t,a)=>t<=a?0:1,t.min3id=(t,a,i)=>t<=a?t<=i?0:2:a<=i?1:2,t.min4id=(t,a,i,n)=>t<=a?t<=i?t<=n?0:3:i<=n?2:3:a<=i?a<=n?1:3:i<=n?2:3,t.minError=V,t.minimaIndex=C,t.minimaIndices=(t,a=0,i=t.length)=>H(C,t,a,i),t.mix=j,t.mixBilinear=(t,a,i,n,s,e)=>j(j(t,a,s),j(i,n,s),e),t.mixCubic=(t,a,i,n,s)=>{const e=s*s,r=1-s,o=r*r;return t*o*r+3*a*o*s+3*i*e*r+n*e*s},t.mixCubicHermite=(t,a,i,n,s)=>{const e=s-1,r=s*s,o=e*e;return(1+2*s)*o*t+s*o*a+r*(3-2*s)*i+r*e*n},t.mixHermite=(t,a,i,n,s)=>{const e=.5*(i-t),r=1.5*(a-i)+.5*(n-t);return((r*s+t-a+e-r)*s+e)*s+a},t.mixQuadratic=(t,a,i,n)=>{const s=1-n;return t*s*s+2*a*s*n+i*n*n},t.muli16=(t,a)=>N((0|t)*(0|a)),t.muli32=(t,a)=>(0|t)*(0|a)|0,t.muli8=(t,a)=>F((0|t)*(0|a)),t.mulu16=(t,a)=>(t&L)*(a&L)&L,t.mulu32=(t,a)=>(t>>>0)*(a>>>0)>>>0,t.mulu8=(t,a)=>(t&y)*(a&y)&y,t.norm=U,t.normCos=t=>{const a=t*t;return 1+a*(2*a-4)},t.noti16=t=>N(~t),t.noti32=t=>~t,t.noti8=t=>F(~t),t.notu16=t=>~t&L,t.notu32=t=>~t>>>0,t.notu8=t=>~t&y,t.ori16=(t,a)=>N(0|t|a),t.ori32=(t,a)=>0|t|a,t.ori8=(t,a)=>F(0|t|a),t.oru16=(t,a)=>(t&L|a&L)&L,t.oru32=(t,a)=>(t>>>0|a>>>0)>>>0,t.oru8=(t,a)=>(t&y|a&y)&y,t.parabola=(t,a)=>Math.pow(4*a*(1-a),t),t.quadrant=t=>p(t)*u|0,t.rad=t=>t*c,t.roundEps=(t,a=1e-6)=>{const i=G(t);return i<=a||i>=1-a?Math.round(t):t},t.roundTo=(t,a=1)=>Math.round(t/a)*a,t.rshifti16=(t,a)=>N((0|t)>>(0|a)),t.rshifti32=(t,a)=>(0|t)>>(0|a),t.rshifti8=(t,a)=>F((0|t)>>(0|a)),t.rshiftu16=(t,a)=>(t&L)>>>(a&L)&L,t.rshiftu32=(t,a)=>t>>>0>>>(a>>>0)>>>0,t.rshiftu8=(t,a)=>(t&y)>>>(a&y)&y,t.sclamp=(t,a,i,n)=>Q(O(t,a,n),i,n),t.sec=t=>1/Math.cos(t),t.sigmoid=(t,a)=>1/(1+Math.exp(-t*(2*a-1))),t.sigmoid11=(t,a)=>1/(1+Math.exp(-t*a)),t.sign=(t,a=1e-6)=>t>a?1:t<-a?-1:0,t.signExtend16=N,t.signExtend8=F,t.simplifyRatio=(t,a)=>{let i=Math.abs(t),n=Math.abs(a);for(;;){if(i<n){const t=i;i=n,n=t}const s=i%n;if(!s)return[t/n,a/n];i=s}},t.sinc=(t,i)=>(i=a*(t*i-1),Math.sin(i)/i),t.sincos=(t,a=1)=>[Math.sin(t)*a,Math.cos(t)*a],t.smax=O,t.smin=Q,t.smoothStep=(t,a,i)=>(3-2*(i=S((i-t)/(a-t))))*i*i,t.smootherStep=(t,a,i)=>(i=S((i-t)/(a-t)))*i*i*(i*(6*i-15)+10),t.solveCubic=(t,a,i,n,s=1e-9)=>{const e=t*t,r=a*a,o=a/(3*t),h=(3*t*i-r)/(3*e),u=(2*r*a-9*t*a*i+27*e*n)/(27*e*t);if(Math.abs(h)<s)return[Math.cbrt(-u)-o];if(Math.abs(u)<s)return h<0?[-Math.sqrt(-h)-o,-o,Math.sqrt(-h)-o]:[-o];{const t=u*u/4+h*h*h/27;if(Math.abs(t)<s)return[-1.5*u/h-o,3*u/h-o];if(t>0){const a=Math.cbrt(-u/2-Math.sqrt(t));return[a-h/(3*a)-o]}{const t=2*Math.sqrt(-h/3),a=Math.acos(3*u/h/t)/3,i=2*Math.PI/3;return[t*Math.cos(a)-o,t*Math.cos(a-i)-o,t*Math.cos(a-2*i)-o]}}},t.solveLinear=(t,a)=>-a/t,t.solveQuadratic=(t,a,i,n=1e-9)=>{const s=2*t;let e=a*a-4*t*i;return e<0?[]:e<n?[-a/s]:[(-a-(e=Math.sqrt(e)))/s,(-a+e)/s]},t.step=(t,a)=>a<t?0:1,t.subi16=(t,a)=>N((0|t)-(0|a)),t.subi32=(t,a)=>(0|t)-(0|a)|0,t.subi8=(t,a)=>F((0|t)-(0|a)),t.subu16=(t,a)=>(t&L)-(a&L)&L,t.subu32=(t,a)=>(t>>>0)-(a>>>0)>>>0,t.subu8=(t,a)=>(t&y)-(a&y)&y,t.tangentCardinal=(t,a,i=.5,n=0,s=2)=>i*((a-t)/(s-n)),t.tangentDiff3=(t,a,i,n=0,s=1,e=2)=>.5*((i-a)/(e-s)+(a-t)/(s-n)),t.trunc=t=>t<0?Math.ceil(t):Math.floor(t),t.tween=(t,a,i)=>n=>j(a,i,t(n)),t.wrap=(t,a,i)=>t<a?t-a+i:t>=i?t-i+a:t,t.wrap01=t=>t<0?t+1:t>=1?t-1:t,t.wrap11=t=>t<-1?t+2:t>=1?t-2:t,t.xori16=(t,a)=>N((0|t)^(0|a)),t.xori32=(t,a)=>(0|t)^(0|a),t.xori8=(t,a)=>F((0|t)^(0|a)),t.xoru16=(t,a)=>(t&L^a&L)&L,t.xoru32=(t,a)=>(t>>>0^a>>>0)>>>0,t.xoru8=(t,a)=>(t&y^a&y)&y,Object.defineProperty(t,"__esModule",{value:!0})})); |
89
mix.d.ts
@@ -21,2 +21,91 @@ export declare const mix: (a: number, b: number, t: number) => number; | ||
export declare const mixCubic: (a: number, b: number, c: number, d: number, t: number) => number; | ||
/** | ||
* Returns hermite interpolation of `a, b, c, d` at normalized position | ||
* `t`, where `a` and `d` are used as predecessor/successor of `b` / `c` | ||
* and only inform the tangent of the interpolation curve. The | ||
* interpolated result is that of `b` and `c`. | ||
* | ||
* Assumes all inputs are uniformly spaced. If that's not the case, use | ||
* `mixCubicHermite()` with one of the tangent generators supporting | ||
* non-uniform spacing of points. | ||
* | ||
* See: https://www.desmos.com/calculator/j4gf8g9vkr | ||
* | ||
* Source: | ||
* https://www.musicdsp.org/en/latest/Other/93-hermite-interpollation.html | ||
* | ||
* @see mixCubicHermite | ||
* @see tangentCardinal | ||
* @see tangentDiff3 | ||
* | ||
* @param a | ||
* @param b | ||
* @param c | ||
* @param d | ||
* @param t | ||
*/ | ||
export declare const mixHermite: (a: number, b: number, c: number, d: number, t: number) => number; | ||
/** | ||
* Computes cubic-hermite interpolation between `a` / `b` at normalized | ||
* time `t` and using respective tangents `ta` / `tb`. | ||
* | ||
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline | ||
* | ||
* @see mixHermite | ||
* @see tangentCardinal | ||
* @see tangentDiff3 | ||
* | ||
* @param a | ||
* @param ta | ||
* @param b | ||
* @param tb | ||
* @param t | ||
*/ | ||
export declare const mixCubicHermite: (a: number, ta: number, b: number, tb: number, t: number) => number; | ||
/** | ||
* Helper function for `mixCubicHermite()`. Computes cardinal tangents | ||
* based on point neighbors of a point B (not given), i.e. `a` | ||
* (predecessor) and `c` (successor) and their times (defaults to | ||
* uniformly spaced). The optional `tension` parameter can be used to | ||
* scale the tangent where 0.0 produces a Cardinal spline tangent and | ||
* 1.0 a Catmull-Rom (opposite to the Wikipedia ref). | ||
* | ||
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline | ||
* | ||
* @param prev | ||
* @param next | ||
* @param scale | ||
* @param ta | ||
* @param tc | ||
*/ | ||
export declare const tangentCardinal: (prev: number, next: number, scale?: number, ta?: number, tc?: number) => number; | ||
/** | ||
* Helper function for `mixCubicHermite()`. Computes tangent for `curr`, | ||
* based on 3-point finite difference, where `prev` & `next` are | ||
* `curr`'s neighbors and the `tX` the three points' respective time | ||
* values. The latter are equally spaced by default (each 1.0 apart). | ||
* | ||
* Using this function with equal spacing of 1.0 and together with | ||
* `mixCubicHermite()` will produce same results as the somewhat | ||
* optimized variant `mixHermite()`. | ||
* | ||
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference | ||
* | ||
* @param prev | ||
* @param curr | ||
* @param next | ||
* @param ta | ||
* @param tb | ||
* @param tc | ||
*/ | ||
export declare const tangentDiff3: (prev: number, curr: number, next: number, ta?: number, tb?: number, tc?: number) => number; | ||
/** | ||
* HOF interpolator. Takes a timing function `f` and interval `[from, | ||
* to]`. Returns function which takes normalized time as single arg and | ||
* returns interpolated value. | ||
* | ||
* @param f | ||
* @param from | ||
* @param to | ||
*/ | ||
export declare const tween: (f: (t: number) => number, from: number, to: number) => (t: number) => number; | ||
@@ -23,0 +112,0 @@ /** |
102
mix.js
@@ -30,2 +30,104 @@ import { HALF_PI, PI } from "./api"; | ||
}; | ||
/** | ||
* Returns hermite interpolation of `a, b, c, d` at normalized position | ||
* `t`, where `a` and `d` are used as predecessor/successor of `b` / `c` | ||
* and only inform the tangent of the interpolation curve. The | ||
* interpolated result is that of `b` and `c`. | ||
* | ||
* Assumes all inputs are uniformly spaced. If that's not the case, use | ||
* `mixCubicHermite()` with one of the tangent generators supporting | ||
* non-uniform spacing of points. | ||
* | ||
* See: https://www.desmos.com/calculator/j4gf8g9vkr | ||
* | ||
* Source: | ||
* https://www.musicdsp.org/en/latest/Other/93-hermite-interpollation.html | ||
* | ||
* @see mixCubicHermite | ||
* @see tangentCardinal | ||
* @see tangentDiff3 | ||
* | ||
* @param a | ||
* @param b | ||
* @param c | ||
* @param d | ||
* @param t | ||
*/ | ||
export const mixHermite = (a, b, c, d, t) => { | ||
const y1 = 0.5 * (c - a); | ||
const y2 = 1.5 * (b - c) + 0.5 * (d - a); | ||
return ((y2 * t + a - b + y1 - y2) * t + y1) * t + b; | ||
}; | ||
/** | ||
* Computes cubic-hermite interpolation between `a` / `b` at normalized | ||
* time `t` and using respective tangents `ta` / `tb`. | ||
* | ||
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline | ||
* | ||
* @see mixHermite | ||
* @see tangentCardinal | ||
* @see tangentDiff3 | ||
* | ||
* @param a | ||
* @param ta | ||
* @param b | ||
* @param tb | ||
* @param t | ||
*/ | ||
export const mixCubicHermite = (a, ta, b, tb, t) => { | ||
const s = t - 1; | ||
const t2 = t * t; | ||
const s2 = s * s; | ||
const h00 = (1 + 2 * t) * s2; | ||
const h10 = t * s2; | ||
const h01 = t2 * (3 - 2 * t); | ||
const h11 = t2 * s; | ||
return h00 * a + h10 * ta + h01 * b + h11 * tb; | ||
}; | ||
/** | ||
* Helper function for `mixCubicHermite()`. Computes cardinal tangents | ||
* based on point neighbors of a point B (not given), i.e. `a` | ||
* (predecessor) and `c` (successor) and their times (defaults to | ||
* uniformly spaced). The optional `tension` parameter can be used to | ||
* scale the tangent where 0.0 produces a Cardinal spline tangent and | ||
* 1.0 a Catmull-Rom (opposite to the Wikipedia ref). | ||
* | ||
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline | ||
* | ||
* @param prev | ||
* @param next | ||
* @param scale | ||
* @param ta | ||
* @param tc | ||
*/ | ||
export const tangentCardinal = (prev, next, scale = 0.5, ta = 0, tc = 2) => scale * ((next - prev) / (tc - ta)); | ||
/** | ||
* Helper function for `mixCubicHermite()`. Computes tangent for `curr`, | ||
* based on 3-point finite difference, where `prev` & `next` are | ||
* `curr`'s neighbors and the `tX` the three points' respective time | ||
* values. The latter are equally spaced by default (each 1.0 apart). | ||
* | ||
* Using this function with equal spacing of 1.0 and together with | ||
* `mixCubicHermite()` will produce same results as the somewhat | ||
* optimized variant `mixHermite()`. | ||
* | ||
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference | ||
* | ||
* @param prev | ||
* @param curr | ||
* @param next | ||
* @param ta | ||
* @param tb | ||
* @param tc | ||
*/ | ||
export const tangentDiff3 = (prev, curr, next, ta = 0, tb = 1, tc = 2) => 0.5 * ((next - curr) / (tc - tb) + (curr - prev) / (tb - ta)); | ||
/** | ||
* HOF interpolator. Takes a timing function `f` and interval `[from, | ||
* to]`. Returns function which takes normalized time as single arg and | ||
* returns interpolated value. | ||
* | ||
* @param f | ||
* @param from | ||
* @param to | ||
*/ | ||
export const tween = (f, from, to) => (t) => mix(from, to, f(t)); | ||
@@ -32,0 +134,0 @@ /** |
{ | ||
"name": "@thi.ng/math", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "Assorted common math functions & utilities", | ||
@@ -32,4 +32,4 @@ "module": "./index.js", | ||
"nyc": "^14.0.0", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.5.3" | ||
"typedoc": "^0.15.0", | ||
"typescript": "^3.6.4" | ||
}, | ||
@@ -49,3 +49,3 @@ "keywords": [ | ||
"sideEffects": false, | ||
"gitHead": "53eec7988c378fc37ae140e7174f36ef9b6208fe" | ||
"gitHead": "97add769f24aa32a1a5e13c5c941605e1b9eb569" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
154395
2258