public SparseVector add2()

in wayang-ml4all/src/main/java/org/apache/wayang/ml4all/utils/SparseVector.java [120:194]


    public SparseVector add2 (SparseVector vector) {

        double fractionofSmallArray = 0.2;
        //take the largest vector and add a % of the other vector
        int estimatedSize = (vector.size() < this.size()) ? this.size() + (int) Math.ceil(fractionofSmallArray * vector.size())  : vector.size() + (int) Math.ceil(fractionofSmallArray * this.size());

        int[] currentInd = new int[estimatedSize];
        double[] currentVal = new double[estimatedSize];
        int[] previousInd = null;
        double[] previousVal = null;

        int index1 = 0;
        int index2 = 0;
        int i = 0;
        boolean overflow = false;
        while (index1 < this.size() && index2 < vector.size()) {
            if (this.indices[index1] < vector.getIndices()[index2]) {
                currentInd[i] = this.indices[index1];
                currentVal[i] = this.values[index1];
                index1++;
            }
            else if (this.indices[index1] > vector.getIndices()[index2]) {
                currentInd[i] = vector.getIndices()[index2];
                currentVal[i] = vector.getValues()[index2];
                index2++;
            }
            else { //they are equal
                currentInd[i] = this.indices[index1];
                currentVal[i] = this.values[index1] + vector.getValues()[index2];
                index1++; index2++;
            }
            i++;

            if (!overflow && i == estimatedSize) { //reached estimated size
                overflow = true;
                previousInd = currentInd;
                previousVal = currentVal;
                currentInd = new int[this.size() - index1 + vector.size() - index2];
                currentVal = new double[this.size() - index1 + vector.size() - index2];
                i = 0;
            }
        }

        for (int j = index1; j < this.size(); j++) {
            currentInd[i] = this.indices[j];
            currentVal[i] = this.values[j];
            i++;
            if (!overflow && i == estimatedSize) { //reached estimated size
                overflow = true;
                previousInd = currentInd;
                previousVal = currentVal;
                currentInd = new int[this.size() - index1 + vector.size() - index2];
                currentVal = new double[this.size() - index1 + vector.size() - index2];
                i = 0;
            }
        }
        for (int j = index2; j < vector.size(); j++) {
            currentInd[i] = vector.getIndices()[j];
            currentVal[i] = vector.getValues()[j];
            i++;
            if (!overflow && i == estimatedSize) { //reached estimated size
                overflow = true;
                previousInd = currentInd;
                previousVal = currentVal;
                currentInd = new int[this.size() - index1 + vector.size() - index2];
                currentVal = new double[this.size() - index1 + vector.size() - index2];
                i = 0;
            }
        }

        if (overflow)
            return new SparseVector(this.getLabel() + vector.getLabel(), concat(previousInd, Arrays.copyOf(currentInd,i)), concat(previousVal, Arrays.copyOf(currentVal, i)));
        else
            return new SparseVector(this.getLabel() + vector.getLabel(), Arrays.copyOf(currentInd, i), Arrays.copyOf(currentVal, i));
    }