private void general(ICollection <NodeDistance <KDTreeNode <int> > > neighbors, double[] result) { Array.Clear(result, 0, result.Length); double sum = 0; double h = Bandwidth * Bandwidth; // Compute weighted mean foreach (var neighbor in neighbors) { double distance = neighbor.Distance; // ||(x-xi)|| double[] point = neighbor.Node.Position; int weight = neighbor.Node.Value; // count double u = distance * distance; // Compute g = -k'(|| (x - xi) / h ||²) double g = -kernel.Derivative(u / h) * weight; for (int i = 0; i < result.Length; i++) { result[i] += g * point[i]; } sum += g; } // Normalize if (sum != 0) { for (int i = 0; i < result.Length; i++) { result[i] /= sum; } } }
private void computeMeanShift(double[] x, double[] shift) { // Get points near the current point ICollection <KDTreeNodeDistance <int> > neighbors; if (maximum > 0) { neighbors = tree.Nearest(x, Bandwidth * 3, maximum); } else { neighbors = tree.Nearest(x, Bandwidth * 3); } double sum = 0; Array.Clear(shift, 0, shift.Length); // Compute weighted mean foreach (KDTreeNodeDistance <int> neighbor in neighbors) { double distance = neighbor.Distance; double[] p = neighbor.Node.Position; double u = distance / Bandwidth; // Compute g = -k'(||(x-xi)/h||²) double g = -kernel.Derivative(u * u); for (int i = 0; i < shift.Length; i++) { shift[i] += g * p[i]; } sum += g; } // Normalize if (sum != 0) { for (int i = 0; i < shift.Length; i++) { shift[i] /= sum; } } }