Returns the Tanimoto Coefficient of the two {@code IntegerVector}
instances

@throws IllegalArgumentException when the length of the two vectors are
not the same.
186477
@SuppressWarnings("unchecked")
    public static double tanimotoCoefficient(IntegerVector a, IntegerVector b) {
        check(a,b);

        // IMPLEMENTATION NOTE: The Tanimoto coefficient uses the squart of the
        // vector magnitudes, which we could compute by just summing the square
        // of the vector values.  This would save a .sqrt() call from the
        // .magnitude() call.  However, we expect that this method might be
        // called multiple times.  Therefore, we square the .magnitude() which
        // should only be two multiplications instaned of |nz| multiplications
        // on the second call (assuming the vector instances cache their
        // magnitude, which almost all do).
        double aMagnitude = a.magnitude();
        double bMagnitude = b.magnitude();

        if (aMagnitude == 0 || bMagnitude == 0)
            return 0;

        int dotProduct = VectorMath.dotProduct(a, b);        
        double aMagSq = aMagnitude * aMagnitude;
        double bMagSq = bMagnitude * bMagnitude;

        return dotProduct / (aMagSq + bMagSq - dotProduct);
    }
103739
public int dot(LongIntVector y) {
        if (y instanceof LongIntSortedVector) {
            LongIntSortedVector other = ((LongIntSortedVector) y);
            int dot = 0;
            int oc = 0;
            for (int c = 0; c < used; c++) {
                while (oc < other.used) {
                    if (other.indices[oc] < indices[c]) {
                        oc++;
                    } else if (indices[c] == other.indices[oc]) {
                        dot += values[c] * other.values[oc];
                        break;
                    } else {
                        break;
                    }
                }
            }
            return dot;
        } else {
            int dot = 0;
            for (int c = 0; c < used; c++) {
                dot += this.values[c] * y.get(indices[c]);
            }
            return dot;
        }
    }
1067014
@SuppressWarnings("unchecked")
    @Override
    public Vector<Long> readVectorUInt() {
        log.debug("readVectorUInt");
        int type = readInteger();
        if ((type & 1) == 0) {
            return (Vector<Long>) getReference(type >> 1);
        }
        int len = type >> 1;
        Vector<Long> array = new Vector<Long>(len);
        storeReference(array);
        @SuppressWarnings("unused")
        int ref2 = readInteger();
        for (int j = 0; j < len; ++j) {
            long value = (buf.get() & 0xff) << 24L;
            value += (buf.get() & 0xff) << 16L;
            value += (buf.get() & 0xff) << 8L;
            value += (buf.get() & 0xff);
            array.add(value);
        }
        return array;
    }
1088266
@SuppressWarnings("unchecked")
    @Override
    public Vector<Integer> readVectorInt() {
        log.debug("readVectorInt");
        int type = readInteger();
        if ((type & 1) == 0) {
            return (Vector<Integer>) getReference(type >> 1);
        }
        int len = type >> 1;
        Vector<Integer> array = new Vector<Integer>(len);
        storeReference(array);
        @SuppressWarnings("unused")
        int ref2 = readInteger();
        for (int j = 0; j < len; ++j) {
            array.add(buf.getInt());
        }
        return array;
    }
1334992
@Override
  public double distance(NumberVector v1, NumberVector v2) {
    if(v1.getDimensionality() != v2.getDimensionality()) {
      throw new IllegalArgumentException("Different dimensionality of FeatureVectors\n  " + "first argument: " + v1 + "\n  " + "second argument: " + v2);
    }

    double sqrDist = 0;
    for(int d = BitsUtil.nextSetBit(dimensions, 0); d >= 0; d = BitsUtil.nextSetBit(dimensions, d + 1)) {
      final double delta = v1.doubleValue(d) - v2.doubleValue(d);
      sqrDist += delta * delta;
    }
    return FastMath.sqrt(sqrDist);
  }
853693
public static double sumOfAbsolutes (double[] vector) {
        // Declare and initialize the accumulator:
        double total = 0.0;

        // Iterate over the vector:
        for (int i = 0; i < vector.length; i++) {
            total += Math.abs(vector[i]);
        }

        // Done, return:
        return total;
    }
1212005
public IntFloatSortedVector getSum(IntFloatVector other) {
        IntFloatSortedVector sum = new IntFloatSortedVector(this);
        sum.add(other);
        return sum;
    }
1197179
public IntIntSortedVector getProd(IntIntVector other) {
        IntIntSortedVector prod = new IntIntSortedVector(this);
        prod.product(other);
        return prod;
    }
763870
public static double angleSparseDense(SparseNumberVector v1, NumberVector v2) {
    // TODO: exploit precomputed length, when available.
    final int dim2 = v2.getDimensionality();
    double l1 = 0., l2 = 0., cross = 0.;
    int i1 = v1.iter(), d2 = 0;
    while(v1.iterValid(i1)) {
      final int d1 = v1.iterDim(i1);
      while(d2 < d1 && d2 < dim2) {
        final double val = v2.doubleValue(d2);
        l2 += val * val;
        ++d2;
      }
      if(d2 < dim2) {
        assert (d1 == d2) : "Dimensions not ordered";
        final double val1 = v1.iterDoubleValue(i1);
        final double val2 = v2.doubleValue(d2);
        l1 += val1 * val1;
        l2 += val2 * val2;
        cross += val1 * val2;
        i1 = v1.iterAdvance(i1);
        ++d2;
      }
      else {
        final double val = v1.iterDoubleValue(i1);
        l1 += val * val;
        i1 = v1.iterAdvance(i1);
      }
    }
    while(d2 < dim2) {
      final double val = v2.doubleValue(d2);
      l2 += val * val;
      ++d2;
    }
    final double a = (cross == 0.) ? 0. : //
        (l1 == 0. || l2 == 0.) ? 1. : //
            FastMath.sqrt((cross / l1) * (cross / l2));
    return (a < 1.) ? a : 1.;
  }
1336789
public static Vector add(Vector vector1, Vector vector2) {
        if (vector2.length() != vector1.length())
            throw new IllegalArgumentException(
                    "Vectors of different sizes cannot be added");
        if (vector2 instanceof IntegerVector &&
            vector1 instanceof DoubleVector)
            return add(vector1, Vectors.asDouble(vector2));
        if (vector2 instanceof SparseVector)
            addSparseValues(vector1, vector2);
        else {
            int length = vector2.length();
            for (int i = 0; i < length; ++i) {
                double value = vector2.getValue(i).doubleValue() +
                               vector1.getValue(i).doubleValue();
                vector1.set(i, value);
            }
        }

        return vector1;
    }
333800
public static double angleSparse(SparseNumberVector v1, SparseNumberVector v2) {
    // TODO: exploit precomputed length, when available?
    double l1 = 0., l2 = 0., cross = 0.;
    int i1 = v1.iter(), i2 = v2.iter();
    while(v1.iterValid(i1) && v2.iterValid(i2)) {
      final int d1 = v1.iterDim(i1), d2 = v2.iterDim(i2);
      if(d1 < d2) {
        final double val = v1.iterDoubleValue(i1);
        l1 += val * val;
        i1 = v1.iterAdvance(i1);
      }
      else if(d2 < d1) {
        final double val = v2.iterDoubleValue(i2);
        l2 += val * val;
        i2 = v2.iterAdvance(i2);
      }
      else { // d1 == d2
        final double val1 = v1.iterDoubleValue(i1);
        final double val2 = v2.iterDoubleValue(i2);
        l1 += val1 * val1;
        l2 += val2 * val2;
        cross += val1 * val2;
        i1 = v1.iterAdvance(i1);
        i2 = v2.iterAdvance(i2);
      }
    }
    while(v1.iterValid(i1)) {
      final double val = v1.iterDoubleValue(i1);
      l1 += val * val;
      i1 = v1.iterAdvance(i1);
    }
    while(v2.iterValid(i2)) {
      final double val = v2.iterDoubleValue(i2);
      l2 += val * val;
      i2 = v2.iterAdvance(i2);
    }
    final double a = (cross == 0.) ? 0. : //
        (l1 == 0. || l2 == 0.) ? 1. : //
            FastMath.sqrt((cross / l1) * (cross / l2));
    return (a < 1.) ? a : 1.;
  }
425878
public double interpolate(double... x) {
        if (x.length != this.x[0].length) {
            throw new IllegalArgumentException(String.format("Invalid input vector size: %d, expected: %d", x.length, this.x[0].length));
        }

        double weight = 0.0, sum = 0.0;
        for (int i = 0; i < this.x.length; i++) {
            double r = Math.distance(x, this.x[i]);
            if (r == 0.0) {
                return y[i];
            }
            double w = Math.pow(r, p);
            weight += w;
            sum += w * y[i];
        }

        return sum / weight;
    }
469936
public static IntegerVector copyOf(IntegerVector source) {
        IntegerVector result = null;

        if (source instanceof TernaryVector) {
            TernaryVector v = (TernaryVector) source;
            int[] pos = v.positiveDimensions();
            int[] neg = v.negativeDimensions();
            result = new TernaryVector(source.length(),
                                       Arrays.copyOf(pos, pos.length),
                                       Arrays.copyOf(neg, neg.length));
        } else if (source instanceof SparseVector) {
            result = new SparseHashIntegerVector(source.length());
            copyFromSparseVector(result, source);
        } else {
            result = new DenseIntVector(source.length());
            for (int i = 0; i < source.length(); ++i)
                result.set(i, source.get(i));
        }
        return result;
    }
708325
public double[] getBasisVector( int which ) {
        if( which < 0 || which >= numComponents )
            throw new IllegalArgumentException("Invalid component");

        DMatrixRMaj v = new DMatrixRMaj(1,A.numCols);
        CommonOps_DDRM.extract(V_t,which,which+1,0,A.numCols,v,0,0);

        return v.data;
    }
618009
public Vector getMidpoint(Vector other) {
        double x = (this.x + other.x) / 2;
        double y = (this.y + other.y) / 2;
        double z = (this.z + other.z) / 2;
        return new Vector(x, y, z);
    }
934534
public static double kendall(int[] x, int[] y) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("Input vector sizes are different.");
        }

        int is = 0, n2 = 0, n1 = 0, n = x.length;
        double aa, a2, a1;
        for (int j = 0; j < n - 1; j++) {
            for (int k = j + 1; k < n; k++) {
                a1 = x[j] - x[k];
                a2 = y[j] - y[k];
                aa = a1 * a2;
                if (aa != 0.0) {
                    ++n1;
                    ++n2;
                    if (aa > 0) {
                        ++is;
                    } else {
                        --is;
                    }

                } else {
                    if (a1 != 0.0) {
                        ++n1;
                    }
                    if (a2 != 0.0) {
                        ++n2;
                    }
                }
            }
        }

        double tau = is / (Math.sqrt(n1) * Math.sqrt(n2));
        return tau;
    }
615112
public static IntegerVector immutable(IntegerVector vector) {
        if (vector == null)
            throw new NullPointerException("Cannot create an immutable " +
                                           "null vector");
        return new IntegerVectorView(vector, true);
    }