A faster implementation of the Minowski distance for positive integer < 25.
    @note do not use this function directly, but the general @link minowski() method.
    @note the passed histograms must be scipy arrays.
346878
def __minowski_low_positive_integer_p(h1, h2, p = 2): # 11..43 us for p = 1..24 \w 100 bins
    
    mult = scipy.absolute(h1 - h2)
    dif = mult
    for _ in range(p - 1): dif = scipy.multiply(dif, mult)
    return math.pow(scipy.sum(dif), 1./p)
512557
public void setRange( double minValue , double maxValue ) {
		this.maxValue = maxValue;
		this.minValue = minValue;

		divisor = 1.00001*(maxValue-minValue)/numBins;

		histIndexes.resize(numBins);

		if( histObjs.length < numBins ) {
		histObjs = new FastQueue[ numBins ];
			for (int i = 0; i < numBins; i++) {
				histObjs[i] = new FastQueue<SortableParameter_F64>(SortableParameter_F64.class,true);
			}
		}
	}
630411
public static double[] range2Bins( double min, double max, int binsNum ) {
        double delta = (max - min) / binsNum;
        double[] bins = new double[binsNum + 1];
        int count = 0;
        double running = min;
        for( int i = 0; i < binsNum; i++ ) {
            bins[count] = running;
            running = running + delta;
            count++;
        }
        bins[binsNum] = max;
        return bins;
    }
282833
private static double findPrototype(DistanceQuery<?> dq, DBIDs cx, DBIDs cy, DBIDVar prototype, double minMaxDist) {
    for(DBIDIter i = cx.iter(); i.valid(); i.advance()) {
      // Maximum distance of i to all elements in cy
      double maxDist = findMax(dq, i, cy, 0., minMaxDist);
      if(maxDist >= minMaxDist) {
        // We already have an at least equally good candidate.
        continue;
      }
      // Maximum distance of i to all elements in cx
      maxDist = findMax(dq, i, cx, maxDist, minMaxDist);

      // New best solution?
      if(maxDist < minMaxDist) {
        minMaxDist = maxDist;
        prototype.set(i);
      }
    }
    return minMaxDist;
  }
997108
def k_closest(points, k, origin=(0, 0)):
    # Time: O(k+(n-k)logk)
    # Space: O(k)
    
    heap = [(-distance(p, origin), p) for p in points[:k]]
    heapify(heap)

    """
    For every point p in points[k:],
    check if p is smaller than the root of the max heap;
    if it is, add p to heap and remove root. Reheapify.
    """
    for p in points[k:]:
        d = distance(p, origin)

        heappushpop(heap, (-d, p))  # heappushpop does conditional check
        """Same as:
            if d < -heap[0][0]:
                heappush(heap, (-d,p))
                heappop(heap)

        Note: heappushpop is more efficient than separate push and pop calls.
        Each heappushpop call takes O(logk) time.
        """

    return [p for nd, p in heap]
454945
public T min() {
        TObjectIntIterator<T> iter = counts.iterator();
        int minCount = Integer.MAX_VALUE;
        T min = null;
        while (iter.hasNext()) {
            iter.advance();
            int count = iter.value();
            if (count < minCount) {
                min = iter.key();
                minCount = count;
            }
        }
        return min;
    }
230801
public static float distanceEuclidean(HashSparseVector sv1 ,HashSparseVector sv2) {
		float dist = 0.0f;
		TIntFloatIterator it1 = sv1.data.iterator();
		TIntFloatIterator it2 = sv2.data.iterator();
		int increa = 0;
		while(it1.hasNext()&&it2.hasNext()){
			if(increa==0){
				it1.advance();
				it2.advance();
			}else if(increa==1){
				it1.advance();
			}else if(increa==2){
				it2.advance();
			}
			if(it1.key()<it2.key()){
				dist += it1.value()*it1.value();
				increa = 1;
			}else if(it1.key()>it2.key()){
				dist += it2.value()*it2.value();
				increa = 2;
			}else{
				float t = it1.value() - it2.value();
				dist += t*t;
				increa = 0;
			}
		}
		while(it1.hasNext()){
			it1.advance();
			dist += it1.value()*it1.value();			
		}
		while(it2.hasNext()){
			it2.advance();
			dist += it2.value()*it2.value();

		}
		return dist;
	}
1141707
def histogram_distance(arr1, arr2, bins=None):
    
    eps = 1e-6
    assert arr1.min() > 0 - eps
    assert arr1.max() < 1 + eps
    assert arr2.min() > 0 - eps
    assert arr2.max() < 1 + eps

    if not bins:
        bins = [x / 10 for x in range(11)]
    hist1 = np.histogram(arr1, bins=bins)[0] / arr1.size
    hist2 = np.histogram(arr2, bins=bins)[0] / arr2.size

    assert abs(hist1.sum() - 1.0) < eps
    assert abs(hist2.sum() - 1.0) < eps

    sqerr = (hist1 - hist2) ** 2
    return sqerr.sum()
433807
def _optimize_binning(x, range, method='Freedman'):
    
    N = x.shape[0]
    if method.lower() == 'freedman':
        s = sort(x)
        IQR = s[int(N * .75)] - s[int(
            N * .25)]  # Interquantile range (75% -25%)
        width = 2 * IQR * N ** (-1. / 3)

    elif method.lower() == 'scott':
        width = 3.49 * x.std() * N ** (-1. / 3)
    else:
        raise ValueError('Method must be Scott or Freedman', method)
    return int(diff(range) / width)
799548
public static <T extends Comparable<T>> T min(T obj1, T obj2) {
    return obj1.compareTo(obj2) <= 0 ? obj1 : obj2;
  }
608909
private static int compare(byte[] s1, int start1, int lens1, byte[] s2, int start2, int lens2) {
    final int max = Math.min(lens1, lens2);

    for (int i = 0; i < max; i++) {
      final byte c1 = s1[start1++];
      final byte c2 = s2[start2++];
      if (c1 != c2)
        return (c1 & 0xff) - (c2 & 0xff);
    }

    return lens1 - lens2;
  }
149131
public static void histogram( GrayU16 input , int minValue , int histogram[] ) {
		if( BoofConcurrency.USE_CONCURRENT ) {
			ImplImageStatistics_MT.histogram(input,minValue,histogram);
		} else {
			ImplImageStatistics.histogram(input,minValue,histogram);
		}
	}
674893
public static List<ByteBuffer> minComponents(List<ByteBuffer> minSeen, Composite candidate, CellNameType comparator)
    {
        // For a cell name, no reason to look more than the clustering prefix
        // (and comparing the collection element would actually crash)
        int size = Math.min(candidate.size(), comparator.clusteringPrefixSize());

        if (minSeen.isEmpty())
            return getComponents(candidate, size);

        // In most case maxSeen is big enough to hold the result so update it in place in those cases
        minSeen = maybeGrow(minSeen, size);

        for (int i = 0; i < size; i++)
            minSeen.set(i, min(minSeen.get(i), candidate.get(i), comparator.subtype(i)));

        return minSeen;
    }
28801
public static float min(float[] x) {
        float m = Float.POSITIVE_INFINITY;

        for (float n : x) {
            if (n < m) {
                m = n;
            }
        }

        return m;
    }
565854
public static double Minkowski(double[] u, double[] v, double p) {
        double distance = 0;
        for (int i = 0; i < u.length; i++) {
            distance += Math.pow(Math.abs(u[i] - v[i]), p);
        }
        return Math.pow(distance, 1 / p);
    }
219387
public int min() {
    int min = Integer.MAX_VALUE;
    for (E key : map.keySet()) {
      min = Math.min(min, getIntCount(key));
    }
    return min;
  }
1305888
protected void linearScanBatchKNN(List<O> objs, List<KNNHeap> heaps) {
    final PrimitiveDistanceFunction<? super O> rawdist = this.rawdist;
    final Relation<? extends O> relation = getRelation();
    final int size = objs.size();
    // Linear scan style KNN.
    for(DBIDIter iter = relation.getDBIDs().iter(); iter.valid(); iter.advance()) {
      O candidate = relation.get(iter);
      for(int index = 0; index < size; index++) {
        final KNNHeap heap = heaps.get(index);
        final double dist = rawdist.distance(objs.get(index), candidate);
        if(dist <= heap.getKNNDistance()) {
          heap.insert(dist, iter);
        }
      }
    }
  }