static/js/numeric.js
/**
 * Calculate pairwise distances of all data points.
 * @function pdist
 * @param {number[][]} arr - input data
 * @return {number[]} condensed distance matrix
 * @see scipy.spatial.distance.pdist
 * @description This is a deep-optimized, less flexible implementation. It uses
 * a condensed distance matrix, inline Euclidean distance calculation, etc. to
 * accelerate computation. It is beneficial because this is one of the most
 * expensive computations in the analysis.
 */
Calculate pairwise distances of all data points.
Calculate pairwise distances of all data points .
function pdist(arr) {
  const n = arr.length;
  const m = arr[0].length;
  const res = Array(n * (n - 1) / 2).fill();
  let ii,  
      xi,  
      xj,  
      sum; 
  let j, k;
  for (let i = 0; i < n; i++) {
    xi = arr[i];
    ii = n * i - i * (i + 3) / 2 - 1;
    for (j = i + 1; j < n; j++) {
      xj = arr[j];
      sum = 0;
      for (k = 0; k < m; k++) {
        sum += (xi[k] - xj[k]) ** 2;
      }
      res[ii + j] = Math.sqrt(sum);
    }
  }
  return res;
}
function pdist ( arr ) { const n = arr . length ; const m = arr [ 0 ] . length ; const res = Array ( n * ( n - 1 ) / 2 ) . fill ( ) ; let ii , xi , xj , sum ; let j , k ; for ( let i = 0 ; i < n ; i ++ ) { xi = arr [ i ] ; ii = n * i - i * ( i + 3 ) / 2 - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { xj = arr [ j ] ; sum = 0 ; for ( k = 0 ; k < m ; k ++ ) { sum += ( xi [ k ] - xj [ k ] ) ** 2 ; } res [ ii + j ] = Math . sqrt ( sum ) ; } } return res ; }
Calculate pairwise distances of all data points.
Calculate pairwise distances of all data points .
// initiate condensed distance matrix // intermediates // part of index // left data point // right data point // sum of square distances // convert square matrix index to condensed matrix index // no more expansion so to avoid half-number arithmetic errors // only calculate upper triangle // calculate Euclidean distance // written so to avoid expensive function calls, otherwise it can be: // res[ii + j] = euclidean(xi, xj);