Example usage for java.lang Float NaN

List of usage examples for java.lang Float NaN

Introduction

In this page you can find the example usage for java.lang Float NaN.

Prototype

float NaN

To view the source code for java.lang Float NaN.

Click Source Link

Document

A constant holding a Not-a-Number (NaN) value of type float .

Usage

From source file:org.broad.igv.tools.ListAccumulator.java

public void finish() {

    if (isFinished) {
        return;/*ww w .  j a va  2 s. com*/
    }

    mean = Float.isNaN(sum) ? Float.NaN : sum / basesCovered;

    if (values != null) {
        if (nPts == 1) {
            for (WindowFunction wf : quantileFunctions) {
                setValue(wf, mean);
            }
        } else {
            if (values.size() > 1) {
                computePercentiles();
            }
            for (WindowFunction wf : quantileFunctions) {

                List<PercentileValue> pList = percentiles.get(wf);
                float v = Float.NaN; // <= Default,
                if (pList != null && pList.size() > 0) {
                    double weightedSum = 0;
                    double sumOfWeights = 0;
                    for (PercentileValue pv : pList) {
                        double weight = (double) pv.nPoints / nPts;
                        sumOfWeights += weight;
                        weightedSum += weight * pv.value;
                    }
                    v = (float) (weightedSum / sumOfWeights);
                }
                setValue(wf, v);

            }

        }
    }
    values = null;
    isFinished = true;

}

From source file:juicebox.tools.utils.juicer.hiccups.GPUController.java

public GPUOutputContainer process(MatrixZoomData zd, double[] normalizationVector, double[] expectedVector,
        int[] rowBounds, int[] columnBounds, int matrixSize, float[] thresholdBL, float[] thresholdDonut,
        float[] thresholdH, float[] thresholdV, NormalizationType normalizationType)
        throws NegativeArraySizeException, IOException {

    RealMatrix localizedRegionData = HiCFileTools.extractLocalBoundedRegion(zd, rowBounds[0], rowBounds[1],
            columnBounds[0], columnBounds[1], matrixSize, matrixSize, normalizationType);

    float[] observedVals = Floats
            .toArray(Doubles.asList(MatrixTools.flattenedRowMajorOrderMatrix(localizedRegionData)));

    // slice KR vector to localized region
    float[] distanceExpectedKRVector = Floats.toArray(Doubles.asList(expectedVector));

    float[] kr1CPU = Floats
            .toArray(Doubles.asList(Arrays.copyOfRange(normalizationVector, rowBounds[0], rowBounds[1])));
    float[] kr2CPU = Floats
            .toArray(Doubles.asList(Arrays.copyOfRange(normalizationVector, columnBounds[0], columnBounds[1])));

    if (kr1CPU.length < matrixSize)
        kr1CPU = ArrayTools.padEndOfArray(kr1CPU, matrixSize, Float.NaN);
    if (kr2CPU.length < matrixSize)
        kr2CPU = ArrayTools.padEndOfArray(kr2CPU, matrixSize, Float.NaN);

    float[] boundRowIndex = new float[1];
    boundRowIndex[0] = rowBounds[0];/*from   w  ww .  jav  a  2s. c  o  m*/
    float[] boundColumnIndex = new float[1];
    boundColumnIndex[0] = columnBounds[0];

    //long gpu_time1 = System.currentTimeMillis();

    // transfer host (CPU) memory to device (GPU) memory
    CUdeviceptr observedKRGPU = GPUHelper.allocateInput(observedVals);
    CUdeviceptr expectedDistanceVectorGPU = GPUHelper.allocateInput(distanceExpectedKRVector);
    CUdeviceptr kr1GPU = GPUHelper.allocateInput(kr1CPU);
    CUdeviceptr kr2GPU = GPUHelper.allocateInput(kr2CPU);
    CUdeviceptr thresholdBLGPU = GPUHelper.allocateInput(thresholdBL);
    CUdeviceptr thresholdDonutGPU = GPUHelper.allocateInput(thresholdDonut);
    CUdeviceptr thresholdHGPU = GPUHelper.allocateInput(thresholdH);
    CUdeviceptr thresholdVGPU = GPUHelper.allocateInput(thresholdV);
    CUdeviceptr boundRowIndexGPU = GPUHelper.allocateInput(boundRowIndex);
    CUdeviceptr boundColumnIndexGPU = GPUHelper.allocateInput(boundColumnIndex);

    // create empty gpu arrays for the results
    int flattenedSize = matrixSize * matrixSize;
    CUdeviceptr expectedBLGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr expectedDonutGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr expectedHGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr expectedVGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr binBLGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr binDonutGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr binHGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr binVGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr observedGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);
    CUdeviceptr peakGPU = GPUHelper.allocateOutput(flattenedSize, Sizeof.FLOAT);

    // call the kernel on the card
    kernelLauncher.call(
            // inputs
            observedKRGPU,
            // output
            expectedBLGPU, expectedDonutGPU, expectedHGPU, expectedVGPU, observedGPU, binBLGPU, binDonutGPU,
            binHGPU, binVGPU, peakGPU,
            // thresholds
            thresholdBLGPU, thresholdDonutGPU, thresholdHGPU, thresholdVGPU,
            // distance expected
            expectedDistanceVectorGPU,
            // kr
            kr1GPU, kr2GPU,
            // bounds
            boundRowIndexGPU, boundColumnIndexGPU);

    // initialize memory to store GPU results
    float[] expectedBLResult = new float[flattenedSize];
    float[] expectedDonutResult = new float[flattenedSize];
    float[] expectedHResult = new float[flattenedSize];
    float[] expectedVResult = new float[flattenedSize];
    float[] binBLResult = new float[flattenedSize];
    float[] binDonutResult = new float[flattenedSize];
    float[] binHResult = new float[flattenedSize];
    float[] binVResult = new float[flattenedSize];
    float[] observedResult = new float[flattenedSize];
    float[] peakResult = new float[flattenedSize];

    // transfer device (GPU) memory to host (CPU) memory
    cuMemcpyDtoH(Pointer.to(expectedBLResult), expectedBLGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(expectedDonutResult), expectedDonutGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(expectedHResult), expectedHGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(expectedVResult), expectedVGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(binBLResult), binBLGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(binDonutResult), binDonutGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(binHResult), binHGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(binVResult), binVGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(observedResult), observedGPU, flattenedSize * Sizeof.FLOAT);
    cuMemcpyDtoH(Pointer.to(peakResult), peakGPU, flattenedSize * Sizeof.FLOAT);

    //long gpu_time2 = System.currentTimeMillis();
    //System.out.println("GPU Time: " + (gpu_time2-gpu_time1));

    int finalWidthX = rowBounds[5] - rowBounds[4];
    int finalWidthY = columnBounds[5] - columnBounds[4];

    // x2, y2 not inclusive here
    int x1 = rowBounds[2];
    int y1 = columnBounds[2];
    int x2 = x1 + finalWidthX;
    int y2 = y1 + finalWidthY;

    //System.out.println("flat = "+flattenedSize+" n = "+matrixSize+" x1 = "+x1+" x2 = "+x2+" y1 = "+y1+" y2 ="+y2);
    float[][] observedDenseCPU = GPUHelper.GPUArraytoCPUMatrix(observedResult, matrixSize, x1, x2, y1, y2);
    float[][] peakDenseCPU = GPUHelper.GPUArraytoCPUMatrix(peakResult, matrixSize, x1, x2, y1, y2);
    float[][] binBLDenseCPU = GPUHelper.GPUArraytoCPUMatrix(binBLResult, matrixSize, x1, x2, y1, y2);
    float[][] binDonutDenseCPU = GPUHelper.GPUArraytoCPUMatrix(binDonutResult, matrixSize, x1, x2, y1, y2);
    float[][] binHDenseCPU = GPUHelper.GPUArraytoCPUMatrix(binHResult, matrixSize, x1, x2, y1, y2);
    float[][] binVDenseCPU = GPUHelper.GPUArraytoCPUMatrix(binVResult, matrixSize, x1, x2, y1, y2);
    float[][] expectedBLDenseCPU = GPUHelper.GPUArraytoCPUMatrix(expectedBLResult, matrixSize, x1, x2, y1, y2);
    float[][] expectedDonutDenseCPU = GPUHelper.GPUArraytoCPUMatrix(expectedDonutResult, matrixSize, x1, x2, y1,
            y2);
    float[][] expectedHDenseCPU = GPUHelper.GPUArraytoCPUMatrix(expectedHResult, matrixSize, x1, x2, y1, y2);
    float[][] expectedVDenseCPU = GPUHelper.GPUArraytoCPUMatrix(expectedVResult, matrixSize, x1, x2, y1, y2);

    GPUHelper.freeUpMemory(new CUdeviceptr[] { observedKRGPU, expectedDistanceVectorGPU, kr1GPU, kr2GPU,
            thresholdBLGPU, thresholdDonutGPU, thresholdHGPU, thresholdVGPU, boundRowIndexGPU,
            boundColumnIndexGPU, expectedBLGPU, expectedDonutGPU, expectedHGPU, expectedVGPU, binBLGPU,
            binDonutGPU, binHGPU, binVGPU, observedGPU, peakGPU });

    return new GPUOutputContainer(observedDenseCPU, peakDenseCPU, binBLDenseCPU, binDonutDenseCPU, binHDenseCPU,
            binVDenseCPU, expectedBLDenseCPU, expectedDonutDenseCPU, expectedHDenseCPU, expectedVDenseCPU);
}

From source file:org.broad.igv.Accumulator.java

public void finish() {

    if (isFinished) {
        return;/*w  ww.java 2  s.  c o  m*/
    }

    mean = Float.isNaN(sum) ? Float.NaN : sum / nPts;

    if (values != null) {
        if (nPts == 1) {
            for (WindowFunction wf : quantileFunctions) {
                setValue(wf, mean);
            }
        } else {
            if (values.size() > 1) {
                computePercentiles();
            }
            for (WindowFunction wf : quantileFunctions) {

                List<PercentileValue> pList = percentiles.get(wf);
                float v = Float.NaN; // <= Default,
                if (pList != null && pList.size() > 0) {
                    double weightedSum = 0;
                    double sumOfWeights = 0;
                    for (PercentileValue pv : pList) {
                        double weight = (double) pv.nPoints / nPts;
                        sumOfWeights += weight;
                        weightedSum += weight * pv.value;
                    }
                    v = (float) (weightedSum / sumOfWeights);
                }
                setValue(wf, v);

            }

        }
    }
    values = null;
    isFinished = true;

}

From source file:org.caleydo.view.tourguide.impl.PAGEAlgorithm.java

@Override
protected float computeImpl(Set<Integer> geneSet, IProgressMonitor monitor) {
    float sum = 0;
    int m = 0;// www. j a  v  a2 s.c om
    for (Integer gene : geneSet) {
        Float f = foldChanges.get(gene);
        if (f == null)
            continue;
        m++;
        sum += f;
    }
    if (m == 0)
        return Float.NaN;
    float Sm = sum / m;

    float z = (float) ((Sm - foldChangesMean) * Math.sqrt(m) / foldChangesSD);
    return z;
}

From source file:eu.itesla_project.modules.histo.tools.HistoDbPrintVoltageRangeTool.java

@Override
public void run(CommandLine line) throws Exception {
    Interval interval = Interval.parse(line.getOptionValue("interval"));
    Path caseFile = Paths.get(line.getOptionValue("case-file"));
    Map<String, VoltageStats> ranges = new HashMap<>();

    Network network = Importers.loadNetwork(caseFile);
    if (network == null) {
        throw new RuntimeException("Case '" + caseFile + "' not found");
    }/*from   ww  w.j a  v  a2s. co  m*/
    network.getStateManager().allowStateMultiThreadAccess(true);

    OfflineConfig config = OfflineConfig.load();
    try (HistoDbClient histoDbClient = config.getHistoDbClientFactoryClass().newInstance().create()) {
        Set<HistoDbAttributeId> attrIds = new LinkedHashSet<>();
        for (VoltageLevel vl : network.getVoltageLevels()) {
            attrIds.add(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.V));
        }
        HistoDbStats stats = histoDbClient.queryStats(attrIds, interval, HistoDbHorizon.SN, false);
        for (VoltageLevel vl : network.getVoltageLevels()) {
            HistoDbNetworkAttributeId attrId = new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.V);
            float min = stats.getValue(HistoDbStatsType.MIN, attrId, Float.NaN) / vl.getNominalV();
            float max = stats.getValue(HistoDbStatsType.MAX, attrId, Float.NaN) / vl.getNominalV();
            int count = (int) stats.getValue(HistoDbStatsType.COUNT, attrId, 0);
            VoltageStats vstats = new VoltageStats(Range.closed(min, max), count, vl.getNominalV());
            for (Generator g : vl.getGenerators()) {
                vstats.pmax += g.getMaxP();
            }
            ranges.put(vl.getId(), vstats);
        }
    }
    Table table = new Table(7, BorderStyle.CLASSIC_WIDE);
    table.addCell("ID");
    table.addCell("vnom");
    table.addCell("range");
    table.addCell("min");
    table.addCell("max");
    table.addCell("count");
    table.addCell("pmax");
    ranges.entrySet().stream().sorted((e1, e2) -> {
        VoltageStats stats1 = e1.getValue();
        VoltageStats stats2 = e2.getValue();
        Range<Float> r1 = stats1.range;
        Range<Float> r2 = stats2.range;
        float s1 = r1.upperEndpoint() - r1.lowerEndpoint();
        float s2 = r2.upperEndpoint() - r2.lowerEndpoint();
        return Float.compare(s1, s2);
    }).forEach(e -> {
        String vlId = e.getKey();
        VoltageStats stats = e.getValue();
        Range<Float> r = stats.range;
        float s = r.upperEndpoint() - r.lowerEndpoint();
        table.addCell(vlId);
        table.addCell(Float.toString(stats.vnom));
        table.addCell(Float.toString(s));
        table.addCell(Float.toString(r.lowerEndpoint()));
        table.addCell(Float.toString(r.upperEndpoint()));
        table.addCell(Integer.toString(stats.count));
        table.addCell(Float.toString(stats.pmax));
    });
    System.out.println(table.render());
}

From source file:org.broad.igv.tdf.Accumulator.java

public void finish() {

    if (isFinished) {
        return;//from   www .j a v a  2  s .c o m
    }

    if (windowFunction == WindowFunction.mean) {
        value = Float.isNaN(sum) ? Float.NaN : sum / basesCovered;
    } else if (values != null) {
        if (values.size() == 1) {
            value = (float) values.get(0);
        } else {
            if (values.size() > 1) {
                computePercentiles();
            }

            float v = Float.NaN; // <= Default,
            if (percentiles != null && percentiles.size() > 0) {
                double weightedSum = 0;
                double sumOfWeights = 0;
                for (PercentileValue pv : percentiles) {
                    double weight = (double) pv.nPoints / basesCovered;
                    sumOfWeights += weight;
                    weightedSum += weight * pv.value;
                }
                v = (float) (weightedSum / sumOfWeights);
            }
            value = v;

        }
    }
    values = null;
    isFinished = true;

}

From source file:savant.data.sources.TDFDataSource.java

@Override
public List<GenericContinuousRecord> getRecords(String ref, RangeAdapter r, Resolution res,
        RecordFilterAdapter filt) throws IOException, InterruptedException {
    List<GenericContinuousRecord> result = new ArrayList<GenericContinuousRecord>();
    TDFDataset ds = getTDFDataset(ref, (Range) r);
    if (ds != null) {
        int nextPos = r.getFrom();
        int rangeEnd = r.getTo() + 1;
        int usefulStep = Math.max(1, r.getLength() / NOTIONAL_SCREEN_WIDTH); // No need for more points than we have pixels.
        List<TDFTile> tiles = ds.getTiles(r.getFrom(), rangeEnd);
        for (TDFTile t : tiles) {
            for (int i = 0; i < t.getSize() && nextPos <= rangeEnd; i++) {
                int datumEnd = t.getEndPosition(i);
                if (nextPos < datumEnd) {
                    int datumStart = t.getStartPosition(i);
                    // If there's a gap before the data starts, fill it with NaNs.
                    if (datumStart == nextPos + 1 && usefulStep > 2) {
                        // Special case.  TDF formatter occasionally leaves a gap of one base between tiles.  This isn't a real NaN.
                        LOG.debug("Skipping NaN hole at " + nextPos);
                    } else {
                        while (nextPos < datumStart && nextPos <= rangeEnd) {
                            result.add(GenericContinuousRecord.valueOf(ref, nextPos, Float.NaN));
                            nextPos += usefulStep;
                        }//  www.j  av a2  s  .  co m
                    }
                    float datum = t.getValue(0, i);
                    LOG.debug("Tile " + i + " from " + datumStart + " to " + datumEnd + "=" + datum);
                    while (nextPos < datumEnd && nextPos <= rangeEnd) {
                        result.add(GenericContinuousRecord.valueOf(ref, nextPos, datum));
                        nextPos += usefulStep;
                    }
                }
            }
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
        }
    }
    return result;
}

From source file:org.caleydo.view.tourguide.impl.PAGEAlgorithm.java

@Override
protected float computePValueImpl(Set<Integer> geneSet, IProgressMonitor monitor) {
    float z = compute(geneSet, monitor);
    if (Float.isNaN(z))
        return Float.NaN;
    int m = Sets.intersection(foldChanges.keySet(), geneSet).size();
    if (m == 0)//from w w  w  .  jav a2  s . c o  m
        return Float.NaN;
    TDistributionImpl t = new TDistributionImpl(m);
    float pValue = (float) t.density(z);
    return pValue;
}

From source file:gdsc.core.utils.MedianWindowInt.java

private float updateMedian() {
    invalid = false;// ww w.j  a v  a2 s.c om
    if (position >= data.length)
        return Float.NaN;

    // Special cases
    if (data.length == 1)
        return data[0];
    if (radius == 0)
        return data[position];
    // The position could be updated and then reset to the same position
    if (cachePosition == position)
        return median;

    // The position should always be above the cache position
    assert cachePosition < position : "Cache position is greater than the position";

    // The cache contains the sorted window from the cachePosition. The cache should cover
    // a set of the data that requires updating:
    //                cachePosition
    //                     |   Position
    //                     |     |  
    // Old     -------------------------
    // New           =========================
    // Remove  ------
    // Keep          +++++++++++++++++++
    // Add                              ======

    final int newStart = FastMath.max(0, position - radius);
    final int newEnd = FastMath.min(position + radius + 1, data.length);
    final int newLength = newEnd - newStart;

    // Speed tests have shown that if the total increment is more than half the radius it 
    // is faster to recompute
    if (cache == null || position - cachePosition > radius / 2 || cache.length != newLength) {
        cache = new int[newLength];
        for (int i = newStart, j = 0; i < newEnd; i++, j++)
            cache[j] = data[i];
    } else {
        // This point is only reached when we have a set of sorted numbers in the cache 
        // and we want to replace N of them with N new numbers.

        final int cacheStart = FastMath.max(0, cachePosition - radius);
        final int cacheEnd = FastMath.min(cachePosition + radius + 1, data.length);
        final int middle = cache.length / 2;
        final int middleValue = cache[middle];

        if (sortedScan) {
            // Method using search of the cached array with sorted numbers to remove

            // Extract numbers to remove
            int[] dataToRemove = new int[position - cachePosition];
            for (int remove = cacheStart, i = 0; remove < newStart; remove++, i++)
                dataToRemove[i] = data[remove];
            Arrays.sort(dataToRemove);

            for (int remove = 0, add = cacheEnd, cachePosition = 0; remove < dataToRemove.length; remove++) {
                final int toRemove = dataToRemove[remove];
                final int add2 = add;

                // Find the number in the cache
                for (; cachePosition < cache.length; cachePosition++) {
                    if (cache[cachePosition] == toRemove) {
                        // Replace with new data 
                        cache[cachePosition++] = data[add++];
                        break;
                    }
                }

                if (add == add2) {
                    // This is bad. Just recompute the entire cache
                    System.out.printf("MedianWindow : Failed to replace data in the cache\n");
                    cache = new int[newLength];
                    for (int i = newStart, j = 0; i < newEnd; i++, j++)
                        cache[j] = data[i];
                    break;
                }
            }
        } else {
            // Method using direct search of the cached array

            // Iterate over numbers to remove
            for (int remove = cacheStart, add = cacheEnd; remove < newStart; remove++) {
                final int toRemove = data[remove];
                final int add2 = add;
                // Find the number in the cache
                if (toRemove > middleValue) {
                    for (int i = cache.length; i-- > 0;) {
                        if (cache[i] == toRemove) {
                            // Replace with new data 
                            cache[i] = data[add++];
                            break;
                        }
                    }
                } else {
                    for (int i = 0; i < cache.length; i++) {
                        if (cache[i] == toRemove) {
                            // Replace with new data 
                            cache[i] = data[add++];
                            break;
                        }
                    }
                }

                if (add == add2) {
                    // This is bad. Just recompute the entire cache
                    System.out.printf("MedianWindow : Failed to replace data in the cache\n");
                    cache = new int[newLength];
                    for (int i = newStart, j = 0; i < newEnd; i++, j++)
                        cache[j] = data[i];
                    break;
                }
            }
        }
    }

    Arrays.sort(cache);
    cachePosition = position;

    return (cache[(cache.length - 1) / 2] + cache[cache.length / 2]) * 0.5f;
}

From source file:gdsc.core.utils.MedianWindowFloat.java

private float updateMedian() {
    invalid = false;// w  ww  .  ja  v  a 2s . c  om
    if (position >= data.length)
        return Float.NaN;

    // Special cases
    if (data.length == 1)
        return data[0];
    if (radius == 0)
        return data[position];
    // The position could be updated and then reset to the same position
    if (cachePosition == position)
        return median;

    // The position should always be above the cache position
    assert cachePosition < position : "Cache position is greater than the position";

    // The cache contains the sorted window from the cachePosition. The cache should cover
    // a set of the data that requires updating:
    //                cachePosition
    //                     |   Position
    //                     |     |  
    // Old     -------------------------
    // New           =========================
    // Remove  ------
    // Keep          +++++++++++++++++++
    // Add                              ======

    final int newStart = FastMath.max(0, position - radius);
    final int newEnd = FastMath.min(position + radius + 1, data.length);
    final int newLength = newEnd - newStart;

    // Speed tests have shown that if the total increment is more than half the radius it 
    // is faster to recompute
    if (cache == null || position - cachePosition > radius / 2 || cache.length != newLength) {
        cache = new float[newLength];
        for (int i = newStart, j = 0; i < newEnd; i++, j++)
            cache[j] = data[i];
    } else {
        // This point is only reached when we have a set of sorted numbers in the cache 
        // and we want to replace N of them with N new numbers.

        final int cacheStart = FastMath.max(0, cachePosition - radius);
        final int cacheEnd = FastMath.min(cachePosition + radius + 1, data.length);
        final int middle = cache.length / 2;
        final float middleValue = cache[middle];

        if (sortedScan) {
            // Method using search of the cached array with sorted numbers to remove

            // Extract numbers to remove
            float[] dataToRemove = new float[position - cachePosition];
            for (int remove = cacheStart, i = 0; remove < newStart; remove++, i++)
                dataToRemove[i] = data[remove];
            Arrays.sort(dataToRemove);

            for (int remove = 0, add = cacheEnd, cachePosition = 0; remove < dataToRemove.length; remove++) {
                final float toRemove = dataToRemove[remove];
                final int add2 = add;

                // Find the number in the cache
                for (; cachePosition < cache.length; cachePosition++) {
                    if (cache[cachePosition] == toRemove) {
                        // Replace with new data 
                        cache[cachePosition++] = data[add++];
                        break;
                    }
                }

                if (add == add2) {
                    // This is bad. Just recompute the entire cache
                    System.out.printf("MedianWindow : Failed to replace data in the cache\n");
                    cache = new float[newLength];
                    for (int i = newStart, j = 0; i < newEnd; i++, j++)
                        cache[j] = data[i];
                    break;
                }
            }
        } else {
            // Method using direct search of the cached array

            // Iterate over numbers to remove
            for (int remove = cacheStart, add = cacheEnd; remove < newStart; remove++) {
                final float toRemove = data[remove];
                final int add2 = add;
                // Find the number in the cache
                if (toRemove > middleValue) {
                    for (int i = cache.length; i-- > 0;) {
                        if (cache[i] == toRemove) {
                            // Replace with new data 
                            cache[i] = data[add++];
                            break;
                        }
                    }
                } else {
                    for (int i = 0; i < cache.length; i++) {
                        if (cache[i] == toRemove) {
                            // Replace with new data 
                            cache[i] = data[add++];
                            break;
                        }
                    }
                }

                if (add == add2) {
                    // This is bad. Just recompute the entire cache
                    System.out.printf("MedianWindow : Failed to replace data in the cache\n");
                    cache = new float[newLength];
                    for (int i = newStart, j = 0; i < newEnd; i++, j++)
                        cache[j] = data[i];
                    break;
                }
            }
        }
    }

    Arrays.sort(cache);
    cachePosition = position;

    return (cache[(cache.length - 1) / 2] + cache[cache.length / 2]) * 0.5f;
}