@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 2.37 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"./../../assert/is-positive-integer.js";import e from"./../../assert/is-square-matrix.js";import t from"./../../assert/is-vector-like.js";import a from"./../../array/float64.js";import n from"./../../ndarray/ctor.js";import i from"./../../ndarray/base/ctor.js";import o from"./../../ndarray/base/numel.js";var s=r.isPrimitive,m=e,u=t,f=a,l=n,c=i,d=o,h={codegen:!1};function p(r,e){return(e?c:l)("float64",2,h)(new f(r*r),[r,r],[r,1],0,"row-major")}function g(r){return c("float64",1,h)(new f(r),[r],[1],0,"row-major")}function v(r,e){var t,a,n,i,o,l;if(l=0,s(r))a=p(t=r,!1);else{if(!m(r))throw new TypeError("invalid argument. First argument must either specify the order of the covariance matrix or be a square 2-dimensional ndarray for storing the covariance matrix. Value: `"+r+"`.");t=r.shape[0],a=r}if(o=new f(t),i=p(t,!0),arguments.length>1){if(!u(e))throw new TypeError("invalid argument. Second argument must be a 1-dimensional ndarray. Value: `"+e+"`.");if(d(e.shape)!==t)throw new Error("invalid argument. The number of elements (means) in the second argument must match covariance matrix dimensions. Expected: "+t+". Actual: "+d(e.shape)+".");return n=e,h}return n=g(t),c;function c(r){var e,s,m,f,c,d,h,p,g;if(0===arguments.length)return 0===l?null:a;if(!u(r))throw new TypeError("invalid argument. Must provide a 1-dimensional ndarray. Value: `"+r+"`.");if(r.shape[0]!==t)throw new Error("invalid argument. Vector length must match covariance matrix dimensions. Expected: "+t+". Actual: "+r.shape[0]+".");for(h=(d=l)/(l+=1),s=d||1,p=0;p<t;p++)for(c=n.get(p),o[p]=r.get(p)-c,c+=o[p]/l,n.set(p,c),m=h*o[p],g=0;g<=p;g++)f=i.get(p,g)+m*o[g],i.set(p,g,f),i.set(g,p,f),e=f/s,a.set(p,g,e),a.set(g,p,e);return a}function h(r){var e,s,m,f,c;if(0===arguments.length)return 0===l?null:a;if(!u(r))throw new TypeError("invalid argument. Must provide a 1-dimensional ndarray. Value: `"+r+"`.");if(r.shape[0]!==t)throw new Error("invalid argument. Vector length must match covariance matrix dimensions. Expected: "+t+". Actual: "+r.shape[0]+".");for(l+=1,f=0;f<t;f++)for(o[f]=r.get(f)-n.get(f),m=o[f],c=0;c<=f;c++)s=i.get(f,c)+m*o[c],i.set(f,c,s),i.set(c,f,s),e=s/l,a.set(f,c,e),a.set(c,f,e);return a}}var w=v;export default w;
//# sourceMappingURL=covmat.js.map