@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 5.78 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import r from"./../ndarray/array.js";import n from"./../math/utils/linspace.js";import e from"./../utils/define-read-only-property.js";import t from"./../assert/is-numeric-array.js";import a from"./../assert/is-matrix-like.js";import i from"./../math/base/special/pow.js";import o from"./../math/base/special/min.js";import s from"./../constants/math/float64-eps.js";import m from"./../math/base/special/sqrt.js";import u from"./../math/base/special/floor.js";import f from"./../assert/is-string.js";import p from"./../assert/has-own-property.js";import l from"./../assert/is-plain-object.js";import h from"./../assert/is-positive-integer.js";import c from"./../assert/is-positive-number-array.js";import y from"./../assert/is-function.js";import v from"./../assert/is-error.js";import g from"./../assert/is-number.js";import x from"./../assert/is-nan.js";import b from"./../constants/math/float64-sqrt-two-pi.js";import w from"./../math/base/special/exp.js";import M from"./../math/base/special/abs.js";import j from"./../constants/math/float64-fourth-pi.js";import d from"./../constants/math/float64-half-pi.js";import E from"./../math/base/special/cos.js";var N=m;function k(r,n){var e,t,a,i,o;for(e=0,t=0,a=0,i=0;i<r.shape[0];i++)a+=(e=(o=r.get(i,n))-t)*(o-(t+=e/(i+1)));return N(a/(i-1))}var T=u;function O(r,n){return r-n}function V(r,n){var e,t,a=r.length;return r[e=T(t=(a-1)*n+1)-1]+(t-T(t))*(r[e+1]-r[e])}function q(r,n){var e,t;for(e=new Array(r.shape[0]),t=0;t<r.shape[0];t++)e[t]=r.get(t,n);return e.sort(O),V(e,.75)-V(e,.25)}var F=i,z=o,A=s,K=k,R=q;function S(r,n){var e,t,a,i;return a=R(r,n)/1.34,t=K(r,n),e=z(a,t),0===(i=1.06*F(r.shape[0],-.2)*e)&&(i=A),i}var P=x,U=i,B=b,C=w,D=1/B;function G(r){return P(r)?NaN:D*C(-.5*U(r,2))}var H=M,I=x,J=i;function L(r){return I(r)?NaN:H(r)>1?0:.75*(1-J(r,2))}var Q=M,W=x,X=j,Y=d,Z=E;function $(r){return W(r)?NaN:Q(r)>1?0:X*Z(Y*r)}var _=M,rr=x,nr=i;function er(r){return rr(r)?NaN:_(r)>1?0:15/16*nr(1-nr(r,2),2)}var tr=M,ar=x;function ir(r){var n;return ar(r)?NaN:(n=tr(r))>1?0:1-n}var or=M,sr=x,mr=i,ur=70/81;function fr(r){var n;return sr(r)?NaN:(n=or(r))>1?0:ur*mr(1-mr(n,3),3)}var pr=M,lr=x,hr=i;function cr(r){return lr(r)?NaN:pr(r)>1?0:35/32*hr(1-hr(r,2),3)}var yr=x;function vr(r){return yr(r)?NaN:r<0||r>1?0:.5}var gr={};gr.gaussian=G,gr.epanechnikov=L,gr.cosine=$,gr.quartic=er,gr.triangular=ir,gr.tricube=fr,gr.triweight=cr,gr.uniform=vr;var xr=gr;function br(r){var n=xr[r];return void 0===n?new Error("invalid argument. Unsupported/unrecognized kernel. Value: `"+r+"`."):n}var wr=f,Mr=p,jr=l,dr=h,Er=c,Nr=y,kr=v,Tr=g.isPrimitive,Or=x,Vr=br;function qr(r,n){if(!jr(n))return new TypeError("invalid argument. Options must be an object. Value: `"+n+"`.");if(Mr(n,"h")){if(r.h=n.h,!Er(r.h))return new TypeError("invalid option. `h` must be an array of positive values. Option: `"+r.n+"`.");if(2!==r.h.length)return new TypeError("invalid option. `h` must be an array of length two. Option: `"+r.n+"`.")}if(Mr(n,"n")&&(r.n=n.n,!dr(r.n)))return new TypeError("invalid option. `n` option must be a positive integer. Option: `"+r.n+"`.");if(Mr(n,"xMax")&&(r.xMax=n.xMax,!Tr(r.xMax)||Or(r.xMax)))return new TypeError("invalid option. `xMax` must be a number. Option: `"+r.xMax+"`.");if(Mr(n,"xMin")&&(r.xMin=n.xMin,!Tr(r.xMin)||Or(r.xMin)))return new TypeError("invalid option. `xMin` must be a number. Option: `"+r.xMin+"`.");if(Mr(n,"yMax")&&(r.yMax=n.yMax,!Tr(r.yMax)||Or(r.yMax)))return new TypeError("invalid option. `yMax` must be a number. Option: `"+r.yMax+"`.");if(Mr(n,"yMin")&&(r.yMin=n.yMin,!Tr(r.yMin)||Or(r.yMin)))return new TypeError("invalid option. `yMin` must be a number. Option: `"+r.yMin+"`.");if(Mr(n,"kernel"))if(r.kernel=n.kernel,wr(r.kernel)){if(r.kernel=Vr(r.kernel),kr(r.kernel))return r.kernel}else if(!Nr(r.kernel))return new TypeError("Kernel is not a function from getKernel");return null}var Fr=t;function zr(r,n){if(!Fr(r))throw new TypeError("First argument must be a numeric array");if(!Fr(n))throw new TypeError("Second argument must be a numeric array");if(r.length!==n.length)throw new Error("First and second argument must be of same length");return{get:function(e,t){if(0===t)return r[e];return n[e]},shape:[r.length,2]}}function Ar(r,n,e){var t,a,i;for(t=r.get(0,n),i=1;i<e;i++)(a=r.get(i,n))<t&&(t=a);return t}function Kr(r,n,e){var t,a,i;for(t=r.get(0,n),i=1;i<e;i++)(a=r.get(i,n))>t&&(t=a);return t}var Rr=r,Sr=n,Pr=e,Ur=t,Br=a,Cr=S,Dr=qr,Gr=zr,Hr=Ar,Ir=Kr,Jr=G;function Lr(){var r,n,e,t,a,i,o,s,m,u,f,p,l,h,c,y,v,g,x,b,w,M,j,d,E,N;if(f={},Br(arguments[0]))p=arguments[0],n=1;else{if(j=arguments[1],!Ur(M=arguments[0]))throw new TypeError("invalid argument. First argument `x` must be a numeric array. Value: `"+M+"`.");if(!Ur(j))throw new TypeError("invalid argument. Second argument `y` must be a numeric array. Value: `"+j+"`.");if(M.length!==j.length)throw new Error("invalid arguments. Arguments `x` and `y` must be arrays of the same length");p=Gr(M,j),n=2}if(arguments.length>n&&(l=Dr(f,arguments[n])))throw l;if(f.h?(g=f.h[0],x=f.h[1]):(g=Cr(p,0),x=Cr(p,1)),E=f.n||25,i=f.xMin||Hr(p,0,p.shape[0]),o=f.xMax||Ir(p,0,p.shape[0]),s=f.yMin||Hr(p,1,p.shape[0]),m=f.yMax||Ir(p,1,p.shape[0]),i>=o)throw new RangeError("`x` min must be strictly less than max");if(s>=m)throw new RangeError("`y` min must be strictly less than max");for(r=f.kernel||Jr,N=Rr({shape:[E,E]}),t=Sr(i,o,E),a=Sr(s,m,E),b=0;b<t.length;b++)for(y=t[b],w=0;w<a.length;w++){for(v=a[w],h=0,d=0;d<p.shape[0];d++)u=p.get(d,0),e=(p.get(d,1)-v)/x,h+=1/g*r((u-y)/g)*(1/x*r(e));N.set(b,w,h/p.shape[0])}return Pr(c={},"x",t),Pr(c,"y",a),Pr(c,"z",N),c}var Qr=Lr;export default Qr;
//# sourceMappingURL=kde2d.js.map