GiniImpurityMeasure implements ImpurityMeasure<GiniImpurityMeasure> { @Override public GiniImpurityMeasure subtract(GiniImpurityMeasure b) { assert left.length == b.left.length : "Subtracted measure has to have length " + left.length; assert left.length == b.right.length : "Subtracted measure has to have length " + left.length; long[] leftRes = new long[left.length]; long[] rightRes = new long[left.length]; for (int i = 0; i < left.length; i++) { leftRes[i] = left[i] - b.left[i]; rightRes[i] = right[i] - b.right[i]; } return new GiniImpurityMeasure(leftRes, rightRes); } GiniImpurityMeasure(long[] left, long[] right); @Override double impurity(); @Override GiniImpurityMeasure add(GiniImpurityMeasure b); @Override GiniImpurityMeasure subtract(GiniImpurityMeasure b); long[] getLeft(); long[] getRight();  }
@Test public void testSubtract() { Random rnd = new Random(0); GiniImpurityMeasure a = new GiniImpurityMeasure( new long[]{randCnt(rnd), randCnt(rnd), randCnt(rnd)}, new long[]{randCnt(rnd), randCnt(rnd), randCnt(rnd)} ); GiniImpurityMeasure b = new GiniImpurityMeasure( new long[]{randCnt(rnd), randCnt(rnd), randCnt(rnd)}, new long[]{randCnt(rnd), randCnt(rnd), randCnt(rnd)} ); GiniImpurityMeasure c = a.subtract(b); assertEquals(a.getLeft()[0] - b.getLeft()[0], c.getLeft()[0]); assertEquals(a.getLeft()[1] - b.getLeft()[1], c.getLeft()[1]); assertEquals(a.getLeft()[2] - b.getLeft()[2], c.getLeft()[2]); assertEquals(a.getRight()[0] - b.getRight()[0], c.getRight()[0]); assertEquals(a.getRight()[1] - b.getRight()[1], c.getRight()[1]); assertEquals(a.getRight()[2] - b.getRight()[2], c.getRight()[2]); }