SimpleStepFunctionCompressor implements StepFunctionCompressor<T> { @Override public StepFunction<T> compress(StepFunction<T> function) { double[] arguments = function.getX(); T[] values = function.getY(); if (arguments.length >= minSizeToBeCompressed) { List<StepFunctionPoint> points = new ArrayList<>(); for (int i = 0; i < arguments.length; i++) points.add(new StepFunctionPoint(arguments[i], values[i])); points = compress(points); double[] resX = new double[points.size()]; T[] resY = Arrays.copyOf(values, points.size()); for (int i = 0; i < points.size(); i++) { StepFunctionPoint pnt = points.get(i); resX[i] = pnt.x; resY[i] = pnt.y; } return new StepFunction<>(resX, resY); } return function; } SimpleStepFunctionCompressor();  SimpleStepFunctionCompressor(int minSizeToBeCompressed, double minImpurityIncreaseForRecord,
        double minImpurityDecreaseForRecord); @Override StepFunction<T> compress(StepFunction<T> function);  }
@Test @SuppressWarnings("unchecked") public void testDefaultCompress() { StepFunction<TestImpurityMeasure> function = new StepFunction<>( new double[]{1, 2, 3, 4}, TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4) ); SimpleStepFunctionCompressor<TestImpurityMeasure> compressor = new SimpleStepFunctionCompressor<>(); StepFunction<TestImpurityMeasure> resFunction = compressor.compress(new StepFunction[] {function})[0]; assertArrayEquals(new double[]{1, 2, 3, 4}, resFunction.getX(), 1e-10); assertArrayEquals(TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4), resFunction.getY()); }
@Test public void testDefaults() { StepFunction<TestImpurityMeasure> function = new StepFunction<>( new double[]{1, 2, 3, 4}, TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4) ); SimpleStepFunctionCompressor<TestImpurityMeasure> compressor = new SimpleStepFunctionCompressor<>(); StepFunction<TestImpurityMeasure> resFunction = compressor.compress(function); assertArrayEquals(new double[]{1, 2, 3, 4}, resFunction.getX(), 1e-10); assertArrayEquals(TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4), resFunction.getY()); }
@Test public void testCompressSmallFunction() { StepFunction<TestImpurityMeasure> function = new StepFunction<>( new double[]{1, 2, 3, 4}, TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4) ); SimpleStepFunctionCompressor<TestImpurityMeasure> compressor = new SimpleStepFunctionCompressor<>(5, 0, 0); StepFunction<TestImpurityMeasure> resFunction = compressor.compress(function); assertArrayEquals(new double[]{1, 2, 3, 4}, resFunction.getX(), 1e-10); assertArrayEquals(TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4), resFunction.getY()); }
@Test public void testCompressIncreasingFunction() { StepFunction<TestImpurityMeasure> function = new StepFunction<>( new double[]{1, 2, 3, 4, 5}, TestImpurityMeasure.asTestImpurityMeasures(1, 2, 3, 4, 5) ); SimpleStepFunctionCompressor<TestImpurityMeasure> compressor = new SimpleStepFunctionCompressor<>(1, 0.4, 0); StepFunction<TestImpurityMeasure> resFunction = compressor.compress(function); assertArrayEquals(new double[]{1, 3, 5}, resFunction.getX(), 1e-10); assertArrayEquals(TestImpurityMeasure.asTestImpurityMeasures(1, 3, 5), resFunction.getY()); }
@Test public void testCompressDecreasingFunction() { StepFunction<TestImpurityMeasure> function = new StepFunction<>( new double[]{1, 2, 3, 4, 5}, TestImpurityMeasure.asTestImpurityMeasures(5, 4, 3, 2, 1) ); SimpleStepFunctionCompressor<TestImpurityMeasure> compressor = new SimpleStepFunctionCompressor<>(1, 0, 0.4); StepFunction<TestImpurityMeasure> resFunction = compressor.compress(function); assertArrayEquals(new double[]{1, 3, 5}, resFunction.getX(), 1e-10); assertArrayEquals(TestImpurityMeasure.asTestImpurityMeasures(5, 3, 1), resFunction.getY()); }