private Vector addAnchorString(Vector list, String field, double diff) { Vector topIndex = new Vector(); Hashtable topIndexParms, currEntry; double lastValue, currValue; if (list.size() == 0) return null; currEntry = (Hashtable) list.get(0); lastValue = Common.parseDouble((String) currEntry.get(field)) + diff; for (int i = 1; i < list.size(); i++) { currEntry = (Hashtable) list.get(i); currValue = Common.parseDouble((String) currEntry.get(field)); if (currValue >= lastValue) { // Values for navigation line topIndexParms = new Hashtable(); topIndexParms.put("HREF", Convert.toString(i)); topIndexParms.put("TEXT", Convert.toString(lastValue)); topIndex.add(topIndexParms); // add anchor entry to list currEntry.put("ANCHORNAME", Convert.toString(i)); currEntry.put("ANCHORTEXT", Convert.toString(lastValue)); lastValue = currValue + diff; } else { // clear value from previous run currEntry.put("ANCHORNAME", ""); currEntry.put("ANCHORTEXT", ""); } list.set(i, currEntry); } return topIndex; }
private Vector addAnchorString(Vector list, String field, boolean fullCompare) { Vector topIndex = new Vector(); Hashtable topIndexParms, currEntry; String lastValue, currValue; if (list.size() == 0) return null; currEntry = (Hashtable) list.get(0); lastValue = (String) currEntry.get(field); if (lastValue == null || lastValue.length() == 0) lastValue = " "; lastValue = lastValue.toUpperCase(); for (int i = 1; i < list.size(); i++) { currEntry = (Hashtable) list.get(i); currValue = (String) currEntry.get(field); currValue = currValue.toUpperCase(); if (currValue == null || currValue == "") continue; try { if (fullCompare) { if (lastValue.compareTo(currValue) != 0) { // Values for navigation line topIndexParms = new Hashtable(); topIndexParms.put("HREF", Convert.toString(i)); topIndexParms.put("TEXT", currValue); topIndex.add(topIndexParms); // add anchor entry to list currEntry.put("ANCHORNAME", Convert.toString(i)); currEntry.put("ANCHORTEXT", currValue); } else { // clear value from previous run currEntry.put("ANCHORNAME", ""); currEntry.put("ANCHORTEXT", ""); } } else { if (lastValue.charAt(0) != currValue.charAt(0)) { // Values for navigation line topIndexParms = new Hashtable(); topIndexParms.put("HREF", Convert.toString(i)); topIndexParms.put("TEXT", currValue.charAt(0) + " "); topIndex.add(topIndexParms); // add anchor entry to list currEntry.put("ANCHORNAME", Convert.toString(i)); currEntry.put("ANCHORTEXT", currValue.charAt(0) + " "); } else { // clear value from previous run currEntry.put("ANCHORNAME", ""); currEntry.put("ANCHORTEXT", ""); } } list.set(i, currEntry); lastValue = currValue; } catch (Exception e) { continue; } } return topIndex; }
public void testTimesVector() throws Exception { Vector val = test.times(test); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2] * values[i / 2], val.get(i)); } } }
public void testPlusDouble() throws Exception { Vector val = test.plus(1); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 1.0, val.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2] + 1.0, val.get(i)); } } }
public void testDivideDouble() throws Exception { Vector val = test.divide(3); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2] / 3.0, val.get(i), EPSILON); } } }
public String toString() { StringBuffer buf = new StringBuffer("{RG "); int len = refs.size(); if (len > 3) len = 3; for (int i = 0; i < len; i++) { if (i > 0) buf.append(','); buf.append(refs.elementAt(i)); } if (len < refs.size()) buf.append(", ..."); buf.append('}'); return buf.toString(); }
@Override public Vector assign(Vector other) { if (size() != other.size()) { throw new CardinalityException(size(), other.size()); } values.clear(); Iterator<Element> it = other.iterateNonZero(); Element e; while (it.hasNext() && (e = it.next()) != null) { setQuick(e.index(), e.get()); } return this; }
@Override public Matrix assignRow(int row, Vector other) { if (columnSize() != other.size()) { throw new CardinalityException(columnSize(), other.size()); } if (row < 0 || row >= rowSize()) { throw new IndexException(row, rowSize()); } for (int col = 0; col < columnSize(); col++) { values[row][col] = other.getQuick(col); } return this; }
@Override public Matrix assignColumn(int column, Vector other) { if (rowSize() != other.size()) { throw new CardinalityException(rowSize(), other.size()); } if (column < 0 || column >= columnSize()) { throw new IndexException(column, columnSize()); } for (int row = 0; row < rowSize(); row++) { values[row][column] = other.getQuick(row); } return this; }
public void testMinus() throws Exception { Vector val = test.minus(test); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } val = test.minus(test).minus(test); assertEquals("cardinality", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { assertEquals("get [" + i + ']', 0.0, val.get(i) + test.get(i)); } Vector val1 = test.plus(1); val = val1.minus(test); for (int i = 0; i < test.size(); i++) { assertEquals("get [" + i + ']', 1.0, val.get(i)); } val1 = test.plus(-1); val = val1.minus(test); for (int i = 0; i < test.size(); i++) { assertEquals("get [" + i + ']', -1.0, val.get(i)); } }
public void testSparseDoubleVectorInt() throws Exception { Vector val = new RandomAccessSparseVector(4); assertEquals("size", 4, val.size()); for (int i = 0; i < 4; i++) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } }
public void testViewPart() throws Exception { Vector part = test.viewPart(1, 2); assertEquals("part size", 2, part.getNumNondefaultElements()); for (int i = 0; i < part.size(); i++) { assertEquals("part[" + i + ']', test.get(i + 1), part.get(i)); } }
public void testGetOver() { try { test.get(test.size()); fail("expected exception"); } catch (IndexException e) { } }
public void testAssignVector() throws Exception { Vector other = new DenseVector(test.size()); test.assign(other); for (int i = 0; i < values.length; i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i)); } }
public void testTimesVectorCardinality() { try { test.times(new DenseVector(test.size() + 1)); fail("expected exception"); } catch (CardinalityException e) { } }
public void testAssignDoubleArray() throws Exception { double[] array = new double[test.size()]; test.assign(array); for (int i = 0; i < values.length; i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i)); } }
public void testAssignDoubleArrayCardinality() { double[] array = new double[test.size() + 1]; try { test.assign(array); fail("cardinality exception expected"); } catch (CardinalityException e) { } }
public RandomAccessSparseVector(Vector other) { this(other.size(), other.getNumNondefaultElements()); Iterator<Element> it = other.iterateNonZero(); Element e; while (it.hasNext() && (e = it.next()) != null) { values.put(e.index(), e.get()); } }
public void testAssignVectorCardinality() { Vector other = new DenseVector(test.size() - 1); try { test.assign(other); fail("cardinality exception expected"); } catch (CardinalityException e) { } }
public void testGet() throws Exception { for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2], test.get(i)); } } }
public static void writeVector(DataOutput out, Vector vector, boolean laxPrecision) throws IOException { boolean dense = vector.isDense(); boolean sequential = vector.isSequentialAccess(); boolean named = vector instanceof NamedVector; out.writeByte( (dense ? FLAG_DENSE : 0) | (sequential ? FLAG_SEQUENTIAL : 0) | (named ? FLAG_NAMED : 0) | (laxPrecision ? FLAG_LAX_PRECISION : 0)); Varint.writeUnsignedVarInt(vector.size(), out); if (dense) { for (Vector.Element element : vector) { if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } else { Varint.writeUnsignedVarInt(vector.getNumNondefaultElements(), out); Iterator<Vector.Element> iter = vector.iterateNonZero(); if (sequential) { int lastIndex = 0; while (iter.hasNext()) { Vector.Element element = iter.next(); int thisIndex = element.index(); // Delta-code indices: Varint.writeUnsignedVarInt(thisIndex - lastIndex, out); lastIndex = thisIndex; if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } else { while (iter.hasNext()) { Vector.Element element = iter.next(); Varint.writeUnsignedVarInt(element.index(), out); if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } } if (named) { String name = ((NamedVector) vector).getName(); out.writeUTF(name == null ? "" : name); } }
public void testGetDistanceSquared() { Vector other = new RandomAccessSparseVector(test.size()); other.set(1, -2); other.set(2, -5); other.set(3, -9); other.set(4, 1); double expected = test.minus(other).getLengthSquared(); assertTrue( "a.getDistanceSquared(b) != a.minus(b).getLengthSquared", Math.abs(expected - test.getDistanceSquared(other)) < 10.0E-7); }
public void testNormalize() throws Exception { Vector val = test.normalize(); double mag = Math.sqrt(1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } else { assertEquals("dot", values[i / 2] / mag, val.get(i)); } } }
public void testSet() throws Exception { test.set(3, 4.5); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i)); } else if (i == 3) { assertEquals("set [" + i + ']', 4.5, test.get(i)); } else { assertEquals("set [" + i + ']', values[i / 2], test.get(i)); } } }
public void testCrossProduct() { Matrix result = test.cross(test); assertEquals("row size", test.size(), result.size()[0]); assertEquals("col size", test.size(), result.size()[1]); for (int row = 0; row < result.size()[0]; row++) { for (int col = 0; col < result.size()[1]; col++) { assertEquals( "cross[" + row + "][" + col + ']', test.getQuick(row) * test.getQuick(col), result.getQuick(row, col)); } } }
public void getHash() { for (Vector v : vectors) { for (int i = 1; i < v.size(); i++) { if (sets.containsKey(v.get(i))) { ((ArrayList<Integer>) sets.get(v.get(i))).add(v.get(0)); } else { ArrayList<Integer> firstElement = new ArrayList<Integer>(); firstElement.add(v.get(0)); sets.put(v.get(i), firstElement); } } } }
private Cost computeRefCostSum(LoopHeaderChord loop, Vector<RefGroup> refGroups) { Cost lc = new Cost(); int l = refGroups.size(); for (int i = 0; i < l; i++) { RefGroup rg = refGroups.elementAt(i); SubscriptExpr se = rg.getRepresentative(); Cost cost = computeRefCost(loop, se); if (cost == null) continue; lc.add(cost); } return lc; }
/** * Creates a Regression instance that operates on the passed data structures. * * @param inertia the inertia of this regression; must be in (0,1]; a value of 1.0 corresponds to * an ordinary regression and lower values cause an exponential forgetting of earlier * measurements * @param coefficients a vector of initial parameters for this regression; must have at least one * entry * @param precisionMatrix the precision matrix of this calibration; must have the same row and * column dimension as the parameters vector */ public Regression(final double inertia, final Vector coefficients, final Matrix precisionMatrix) { if (coefficients == null) { throw new IllegalArgumentException("initial coefficent vector is null"); } if (precisionMatrix == null) { throw new IllegalArgumentException("precisionMatrix is null"); } if (precisionMatrix.rowSize() != coefficients.size() || precisionMatrix.columnSize() != coefficients.size()) { throw new IllegalArgumentException( "dimension of precision matrix is inconsistent with " + "size of coefficient vector"); } this.avgInputs = new ArrayList<SignalSmoother>(coefficients.size()); for (int i = 0; i < coefficients.size(); i++) { this.avgInputs.add(null); } this.setInertia(inertia); this.coefficients = coefficients; this.precisionMatrix = precisionMatrix; }
/** * Create a representation of a range from min to max. * * @param min is the lower bound * @param max is the upper bound */ public static Bound create(Expression min, Expression max) { if (max == null) { if (min == null) return noBound; max = LiteralMap.put(0x7ffffff, Machine.currentMachine.getIntegerCalcType()); } if (bounds != null) { int n = bounds.size(); for (int i = 0; i < n; i++) { Bound ta = bounds.elementAt(i); if (!min.equivalent(ta.min)) continue; if (!max.equivalent(ta.max)) continue; return ta; } } Bound a = new Bound(min, max); return a; }
@Test public void testInitialization() { // start with super clusterable data List<? extends WeightedVector> data = cubishTestData(0.01); // just do initialization of ball k-means. This should drop a point into each of the clusters BallKMeans r = new BallKMeans(new BruteSearch(new EuclideanDistanceMeasure()), 6, 20); r.cluster(data); // put the centroids into a matrix Matrix x = new DenseMatrix(6, 5); int row = 0; for (Centroid c : r) { x.viewRow(row).assign(c.viewPart(0, 5)); row++; } // verify that each column looks right. Should contain zeros except for a single 6. final Vector columnNorms = x.aggregateColumns( new VectorFunction() { @Override public double apply(Vector f) { // return the sum of three discrepancy measures return Math.abs(f.minValue()) + Math.abs(f.maxValue() - 6) + Math.abs(f.norm(1) - 6); } }); // verify all errors are nearly zero assertEquals(0, columnNorms.norm(1) / columnNorms.size(), 0.1); // verify that the centroids are a permutation of the original ones SingularValueDecomposition svd = new SingularValueDecomposition(x); Vector s = svd.getS().viewDiagonal().assign(Functions.div(6)); assertEquals(5, s.getLengthSquared(), 0.05); assertEquals(5, s.norm(1), 0.05); }