@Override
    public SGDVector add(SGDVector other) {
        if (other.size() != size) {
            throw new IllegalArgumentException("Can't add two vectors of different dimension, this = " + size + ", other = " + other.size());
        }
        if (other instanceof DenseVector) {
            return other.add(this);
        } 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 add() {
        SparseVector a = generateVectorA();
        SparseVector b = generateVectorB();
        SparseVector c = generateVectorC();
        SparseVector empty = generateEmptyVector();

        assertEquals(a,a.add(empty), "A + empty");
        assertEquals(b,b.add(empty), "B + empty");
        assertEquals(c,c.add(empty), "C + empty");

        assertEquals(scale(a,2.0),a.add(a), "A * 2");
        assertEquals(scale(b,2.0),b.add(b), "B * 2");
        assertEquals(scale(c,2.0),c.add(c), "C * 2");

        SparseVector aAddB = generateVectorAAddB();
        SparseVector aAddC = generateVectorAAddC();
        SparseVector bAddC = generateVectorBAddC();

        assertEquals(aAddB, a.add(b), "A + B");
        assertEquals(aAddC, a.add(c), "A + C");
        assertEquals(aAddB, b.add(a), "B + A");
        assertEquals(bAddC, b.add(c), "B + C");
        assertEquals(aAddC, c.add(a), "C + A");
        assertEquals(bAddC, c.add(b), "C + B");
    }