public void testCosineOfVectors() { try { double result = VectorUtils.cosineOfVectors(new int[] {1, 2, 3}, null); fail("Null argument allowed"); } catch (IllegalArgumentException e) { // expected } try { double result = VectorUtils.cosineOfVectors(null, new int[] {1, 2, 3}); fail("Null argument allowed"); } catch (IllegalArgumentException e) { // expected } try { double result = VectorUtils.cosineOfVectors(new int[] {1}, new int[] {1, 2, 3}); fail("Arguments of different size allowed"); } catch (IllegalArgumentException e) { // expected } int[] one = new int[] {1, 1, 1}; int[] two = new int[] {1, 1, 1}; assertEquals(1d, VectorUtils.cosineOfVectors(one, two), 0.001); }
/** * Returns true iff the line segments overlap; weaker than <code>theSame</code>. * * @param other * @return true iff this and other overlap */ public boolean isOverlapping(LineSegment other) { if (!VectorUtils.areEqual(m, other.m) && !VectorUtils.areEqual(b, other.b)) return false; // same line, now check for overlap--- // the start point of one should be inside the other return containsPoint(other.start) || other.containsPoint(start); }
private boolean isPointInSegment(float x, float y) { return ((x >= Math.min(start.x, end.x) && x <= Math.max(start.x, end.x)) || VectorUtils.isZero(x - start.x) || VectorUtils.isZero(x - end.x)) && ((y >= Math.min(start.y, end.y) && y <= Math.max(start.y, end.y)) || VectorUtils.isZero(y - start.y) || VectorUtils.isZero(y - end.y)); }
/** * Returns true if point is contained in this line segment. * * @param point * @return true if point is contained in this line segment */ public boolean containsPoint(PointF point) { if (!VectorUtils.areEqual(point.x * m + b, point.y)) return false; // we know the point is on the line; now, if it is in the square denoted by the start and end // point, then it is in the segment return point.x < Math.max(start.x, end.x) && point.x > Math.min(start.x, end.x) && point.y < Math.max(start.y, end.y) && point.y > Math.min(start.y, end.y) || ((VectorUtils.areEqual(point.x, start.x) || VectorUtils.areEqual(point.x, end.x)) && (VectorUtils.areEqual(point.y, start.y) || VectorUtils.areEqual(point.y, end.y))); }
public void testVectorLength() { try { double result = VectorUtils.vectorLength(null); fail("Null argument allowed"); } catch (IllegalArgumentException e) { // expected } assertEquals(Math.sqrt(2), VectorUtils.vectorLength(new int[] {1, 1}), 0.001d); assertEquals(Math.sqrt(3), VectorUtils.vectorLength(new int[] {1, 1, 1}), 0.001d); assertEquals(Math.sqrt(12), VectorUtils.vectorLength(new int[] {2, 2, 2}), 0.001d); }
/** * Calculates a unit vector parallel to this line segment, with the same direction. * * @return unit vector parallel to this line segment */ public float[] findUnitVector() { float[] ret = new float[2]; ret[0] = end.x - start.x; ret[1] = end.y - start.y; VectorUtils.convertToUnitVector(ret, ret); return ret; }
public void testScalarProduct() { try { double result = VectorUtils.scalarProduct(new int[] {1, 2, 3}, null); fail("Null argument allowed"); } catch (IllegalArgumentException e) { // expected } try { double result = VectorUtils.scalarProduct(null, new int[] {1, 2, 3}); fail("Null argument allowed"); } catch (IllegalArgumentException e) { // expected } try { double result = VectorUtils.scalarProduct(new int[] {1}, new int[] {1, 2, 3}); fail("Arguments of different size allowed"); } catch (IllegalArgumentException e) { // expected } assertEquals(3, VectorUtils.scalarProduct(new int[] {1, 1, 1}, new int[] {1, 1, 1})); assertEquals(6, VectorUtils.scalarProduct(new int[] {1, 1, 1}, new int[] {1, 2, 3})); assertEquals(14, VectorUtils.scalarProduct(new int[] {1, 2, 3}, new int[] {1, 2, 3})); assertEquals(0, VectorUtils.scalarProduct(new int[] {0, 0, 0}, new int[] {1, 2, 3})); }
@Override public double computeSimilarity(String s1, String s2) { if (!Utilities.checkInputs(s1, s2)) { return Utilities.SIMILARITY_EMPTY_EMPTY; } computeTokens(s1, s2); if (s1.length() == 0 || s2.length() == 0) { return 0.0; } double i = 0.0; double mVector1 = 0.0; double mVector2 = 0.0; if (takeKeyset) { i = VectorUtils.intersect(keys1, keys2); mVector1 = VectorUtils.size(keys1); mVector2 = VectorUtils.size(keys2); } else { i = VectorUtils.intersect(vec1, vec2); mVector1 = VectorUtils.size(vec1); mVector2 = VectorUtils.size(vec2); } double overlapCoefficient = i / Math.min(mVector1, mVector2); return overlapCoefficient; }
@Override public void checkSchema(ResourceSchema schema) throws IOException { SchemaUtils.claim( schema, 0, DataType.INTEGER, DataType.CHARARRAY, DataType.UNKNOWN); // the part name (usually will be int, but generally accept any type) ResourceSchema.ResourceFieldSchema bag = SchemaUtils.claim(schema, 1, DataType.BAG); // the bag ResourceSchema.ResourceFieldSchema tuple = SchemaUtils.claim(bag, 0, DataType.TUPLE); // the tuple of (id, vector) SchemaUtils.claim(tuple, 0, DataType.INTEGER); // the id tuple = SchemaUtils.claim(tuple, 1, DataType.TUPLE); // the vector Env.setProperty( GroupedVectorStore.class, "vector.type", VectorUtils.typeOfVector(tuple.getSchema())); }
/** * Returns whether the other line segment is the same as this one, modulo direction. It would be * inappropriate to override <code>.equals()</code> because we ignore direction. * * @param other the other line segment. * @return true iff this and other are equal, modulo direction */ public boolean theSame(LineSegment other) { if (!(other instanceof LineSegment)) return false; if (!(VectorUtils.areEqual(start, other.start) && VectorUtils.areEqual(end, other.end) || VectorUtils.areEqual(start, other.end) && VectorUtils.areEqual(end, other.start))) { return false; } if (!VectorUtils.areEqual(m, other.m) || !VectorUtils.areEqual(b, other.b)) { return false; } return true; }
/** * Calculates the length of this line segment. * * @return the length of this line segment, in meters. */ public float length() { return VectorUtils.distance(start, end); }