private void assertSmallDiff(DoubleVector v1, DoubleVector v2) { assertEquals(v1.getLength(), v2.getLength()); for (int i = 0; i < v2.getLength(); i++) { double d1 = v2.get(i); assertEquals(v1.get(i), d1, 1E-5); } }
@Test public void testOnes() { DoubleVector vec = DenseDoubleVector.ones(10); for (int i = 0; i < vec.getDimension(); ++i) { assertEquals(1, vec.get(i), 0.000001); } }
public double getActiveLearningRatio(double qbcEntropy, DoubleVector combinedVote) { int maxIndex = combinedVote.maxIndex(); int ensembleLength = this.ensemble.length; double maxVote = combinedVote.getValue(maxIndex); double activeLearningRatio = (qbcEntropy) * (maxVote / ensembleLength); return activeLearningRatio; }
@Test public void testMinMax() { double[] vals = {11, 2, 3, 4, 15, 6, 7, 8}; DoubleVector vec = new DenseDoubleVector(vals); assertEquals(15, vec.max(), 0.000001); assertEquals(2, vec.min(), 0.000001); }
@Test(expected = IllegalArgumentException.class) public void testVectorMultiplyMatrixAbnormal() { DoubleVector vec = new DenseDoubleVector(new double[] {1, 2, 3}); DoubleMatrix mat = new DenseDoubleMatrix( new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}); vec.multiply(mat); }
@Test public void invokeMethod() throws ScriptException, NoSuchMethodException { Object obj = engine.eval("list(execute=sqrt)"); DoubleVector result = (DoubleVector) invocableEngine.invokeMethod(obj, "execute", 16); assertThat(result.length(), equalTo(1)); assertThat(result.get(0), equalTo(4d)); }
@Test(expected = IllegalArgumentException.class) public void testDotWithException() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0}; DoubleVector vec1 = new DenseDoubleVector(vals1); DoubleVector vec2 = new DenseDoubleVector(vals2); vec1.dot(vec2); }
@Test public void invokeFunction() throws ScriptException, NoSuchMethodException { engine.eval("f <- function(x) sqrt(x)"); DoubleVector result = (DoubleVector) invocableEngine.invokeFunction("f", 4); assertThat(result.length(), equalTo(1)); assertThat(result.get(0), equalTo(2d)); }
@Test public void testDotNormal() { double[] arr1 = new double[] {1, 2, 3}; double[] arr2 = new double[] {4, 5, 6}; DoubleVector vec1 = new DenseDoubleVector(arr1); DoubleVector vec2 = new DenseDoubleVector(arr2); assertEquals(32.0, vec1.dot(vec2), 0.000001); }
@Test(expected = IllegalArgumentException.class) public void testDotAbnormal() { double[] arr1 = new double[] {1, 2, 3}; double[] arr2 = new double[] {4, 5}; DoubleVector vec1 = new DenseDoubleVector(arr1); DoubleVector vec2 = new DenseDoubleVector(arr2); vec1.add(vec2); }
@Test public void testAddNormal() { double[] arr1 = new double[] {1, 2, 3}; double[] arr2 = new double[] {4, 5, 6}; DoubleVector vec1 = new DenseDoubleVector(arr1); DoubleVector vec2 = new DenseDoubleVector(arr2); double[] arrExp = new double[] {5, 7, 9}; assertArrayEquals(arrExp, vec1.add(vec2).toArray(), 0.000001); }
@Test public void testMultiplyNormal() { double[] arr1 = new double[] {1, 2, 3}; double[] arr2 = new double[] {4, 5, 6}; DoubleVector vec1 = new DenseDoubleVector(arr1); DoubleVector vec2 = new DenseDoubleVector(arr2); double[] arrExp = new double[] {4, 10, 18}; assertArrayEquals(arrExp, vec1.multiply(vec2).toArray(), 0.000001); }
@Test public void testVectorMultiplyMatrix() { DoubleVector vec = new DenseDoubleVector(new double[] {1, 2, 3}); DoubleMatrix mat = new DenseDoubleMatrix(new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}); double[] expectedRes = new double[] {38, 44, 50, 56}; assertArrayEquals(expectedRes, vec.multiply(mat).toArray(), 0.000001); }
@Test public void testSubtractNormal() { double[] arr1 = new double[] {1, 2, 3}; double[] arr2 = new double[] {4, 5, 6}; DoubleVector vec1 = new DenseDoubleVector(arr1); DoubleVector vec2 = new DenseDoubleVector(arr2); double[] arrExp = new double[] {-3, -3, -3}; assertArrayEquals(arrExp, vec1.subtract(vec2).toArray(), 0.000001); }
@Test public void testSlice() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; double[] vals2 = {2, 3, 4, 5, 6}; DoubleVector vec1 = new DenseDoubleVector(vals1); DoubleVector vec2 = new DenseDoubleVector(vals2); DoubleVector vec3 = vec1.sliceUnsafe(1, 5); assertEquals(vec2, vec3); }
@Test public void testSliceNormal() { double[] arr1 = new double[] {2, 3, 4, 5, 6}; double[] arr2 = new double[] {4, 5, 6}; double[] arr3 = new double[] {2, 3, 4}; DoubleVector vec1 = new DenseDoubleVector(arr1); assertArrayEquals(arr2, vec1.slice(2, 4).toArray(), 0.000001); DoubleVector vec2 = new DenseDoubleVector(arr1); assertArrayEquals(arr3, vec2.slice(3).toArray(), 0.000001); }
/** * Returns {@code true} if the two vectors are equal to one another. Two {@code Vector} insances * are considered equal if they contain the same number of elements and all corresponding pairs of * {@code double} values are equal. */ public static boolean equals(DoubleVector v1, DoubleVector v2) { if (v1.length() == v2.length()) { int length = v1.length(); for (int i = 0; i < length; ++i) { if (v1.get(i) != v2.get(i)) return false; } return true; } return false; }
@Test public void testSqrt() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; DoubleVector vec1 = new DenseDoubleVector(vals1); DoubleVector vec3 = vec1.sqrt(); for (int i = 0; i < vals1.length; ++i) { assertEquals(Math.sqrt(vec1.get(i)), vec3.get(i), 0.000001); } }
@Test public void testAbs() { double[] vals = {1, 2, 3, 4, 5, 6, 7, 8}; DoubleVector vec = new DenseDoubleVector(vals); DoubleVector vec2 = vec.abs(); for (int i = 0; i < vals.length; ++i) { assertEquals(Math.abs(vec.get(i)), vec2.get(i), 0.000001); } }
@Test public void testPow() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; DoubleVector vec1 = new DenseDoubleVector(vals1); int constant = 5; DoubleVector vec3 = vec1.pow(constant); for (int i = 0; i < vals1.length; ++i) { assertEquals(Math.pow(vec1.get(i), 5), vec3.get(i), 0.000001); } }
@Test public void testDivide() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; DoubleVector vec1 = new DenseDoubleVector(vals1); double constant = 10; DoubleVector vec3 = vec1.divide(constant); for (int i = 0; i < vals1.length; ++i) { assertEquals(vec1.get(i) / constant, vec3.get(i), 0.000001); } }
@Test(expected = AssertionError.class) public void deepCopy() { double[] vals1 = {0, 1, 2, 3, 4, 5, 6, 7, 8}; double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0}; DoubleVector vec1 = new DenseDoubleVector(vals1); DoubleVector vec2 = new DenseDoubleVector(vals2); DoubleVector vec3 = vec1.deepCopy(); vec1 = vec1.add(vec2); assertEquals(vec1, vec3); }
@Test public void testSum() { double[] vals = {1, 2, 3, 4, 5, 6, 7, 8}; DoubleVector vec = new DenseDoubleVector(vals); double expected = 0; double res = vec.sum(); for (int i = 0; i < vals.length; ++i) { expected += vec.get(i); } assertEquals(expected, res, 0.000001); }
@Test public void testSubtractFrom() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; DoubleVector vec1 = new DenseDoubleVector(vals1); double constant = 10; DoubleVector vec3 = vec1.subtractFrom(constant); for (int i = 0; i < vals1.length; ++i) { assertEquals(constant - vec1.get(i), vec3.get(i), 0.000001); } }
/** * Creates a copy of a given {@code DoubleVector} with the same type as the original. * * @param source The {@code Vector} to copy. * @return A copy of {@code source} with the same type. */ public static DoubleVector copyOf(DoubleVector source) { if (source == null) return null; DoubleVector result = null; if (source instanceof SparseDoubleVector) { result = new CompactSparseVector(source.length()); copyFromSparseVector(result, source); } else if (source instanceof DenseVector || source instanceof ScaledDoubleVector) { result = new DenseVector(source.length()); for (int i = 0; i < source.length(); ++i) result.set(i, source.get(i)); } else if (source instanceof AmortizedSparseVector) { result = new AmortizedSparseVector(source.length()); copyFromSparseVector(result, source); } else if (source instanceof DoubleVectorView) { DoubleVectorView view = (DoubleVectorView) source; return copyOf(view.getOriginalVector()); } else { // Create a copy of the given class using reflection. This code // assumes that the given implemenation of Vector has a constructor // which accepts another Vector. try { Class<? extends DoubleVector> sourceClazz = source.getClass(); Constructor<? extends DoubleVector> constructor = sourceClazz.getConstructor(DoubleVector.class); result = (DoubleVector) constructor.newInstance(source); } catch (Exception e) { throw new Error(e); } } return result; }
@Test public void testDot() { double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8}; double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1}; DoubleVector vec1 = new DenseDoubleVector(vals1); DoubleVector vec2 = new DenseDoubleVector(vals2); double expected = 0.0; double res = vec1.dotUnsafe(vec2); for (int i = 0; i < vals1.length; ++i) { expected += vec1.get(i) * vec2.get(i); } assertEquals(expected, res, 0.000001); }
public TimeValue(DoubleVector month) { dVal = Double.NaN; monthVal = month; currentValue = monthVal.get(0); probVal = null; monthProbVal = null; }
@Test public void testFromUpTo() { double from = 11; double to = 111.5; double stepsize = 2.5; DoubleVector vec = DenseDoubleVector.fromUpTo(from, to, stepsize); int curIndex = 0; double cur = 11; while (cur <= to) { assertEquals(cur, vec.get(curIndex), 0.000001); cur += stepsize; ++curIndex; } }
public String toString() { if (!Double.isNaN(dVal)) return Double.toString(dVal); if (monthVal != null) return monthVal.toString(); if (probVal != null) return probVal.toString(); return monthProbVal.toString(); }
public DoubleVector crossProduct(DoubleVector... multiplicands) { DoubleVector[] newMultiplicands = new DoubleVector[multiplicands.length + 1]; newMultiplicands[0] = asDoubleVector(); System.arraycopy(multiplicands, 0, newMultiplicands, 1, multiplicands.length); return DoubleVector.crossProductAll(newMultiplicands); }