@Override
    public SGDVector subtract(SGDVector other) {
        if (other.size() != size) {
            throw new IllegalArgumentException("Can't subtract two vectors of different dimension, this = " + size + ", other = " + other.size());
        }
        if (other instanceof DenseVector) {
            DenseVector output = ((DenseVector)other).copy();
            for (VectorTuple tuple : this) {
                output.set(tuple.index,tuple.value-output.get(tuple.index));
            }
            return output;
        } else if (other instanceof SparseVector) {
            Map<Integer, Double> values = new HashMap<>();
            for (VectorTuple tuple : this) {
                values.put(tuple.index, tuple.value);
            }
            for (VectorTuple tuple : other) {
                values.merge(tuple.index, -tuple.value, Double::sum);
            }
            return createSparseVector(size, values);
        } else {
            throw new IllegalArgumentException("Vector other is not dense or sparse.");
        }
    }
@Test
    public void subtract() {
        SparseVector a = generateVectorA();
        SparseVector b = generateVectorB();
        SparseVector c = generateVectorC();
        SparseVector empty = generateEmptyVector();

        assertEquals(a,a.subtract(empty), "A - empty");
        assertEquals(invert(a),empty.subtract(a), "empty - A");
        assertEquals(b,b.subtract(empty), "B - empty");
        assertEquals(invert(b),empty.subtract(b), "empty - B");
        assertEquals(c,c.subtract(empty), "C - empty");
        assertEquals(invert(c),empty.subtract(c), "empty - C");

        assertEquals(zero(a),a.subtract(a), "A - A");
        assertEquals(zero(b),b.subtract(b), "B - B");
        assertEquals(zero(c),c.subtract(c), "C - C");

        SparseVector aSubB = generateVectorASubB();
        SparseVector aSubC = generateVectorASubC();
        SparseVector bSubA = generateVectorBSubA();
        SparseVector bSubC = generateVectorBSubC();
        SparseVector cSubA = generateVectorCSubA();
        SparseVector cSubB = generateVectorCSubB();

        assertEquals(aSubB, a.subtract(b), "A - B");
        assertEquals(aSubC, a.subtract(c), "A - C");
        assertEquals(bSubA, b.subtract(a), "B - A");
        assertEquals(bSubC, b.subtract(c), "B - C");
        assertEquals(cSubA, c.subtract(a), "C - A");
        assertEquals(cSubB, c.subtract(b), "C - B");
    }