public static Vector zeroVector (int size) { double[] coords = new double[size]; for (int i=0; i<size; ++i) coords[i] = 0.0; return new Vector(coords); }
public static Vector zeroVector (int size) { double[] coords = new double[size]; for (int i=0; i<size; ++i) coords[i] = 0.0; return new Vector(coords); }
public static Vector toUnitVector (double theta, double phi) { double sphi = Math.sin(phi); return new Vector(Math.cos(theta) * sphi, Math.sin(theta) * sphi, Math.cos(phi)); } private static double thetaFromUnitVector (Vector v) {
public static Vector toUnitVector (double theta, double phi) { double sphi = Math.sin(phi); return new Vector(Math.cos(theta) * sphi, Math.sin(theta) * sphi, Math.cos(phi)); } private static double thetaFromUnitVector (Vector v) {
static private Vector getCoordinateVector(List<Vector> Vs, int coordinate) { int n = Vs.size(); double[] data = new double[n]; for (int i=0; i<n; ++i) { data[i] = Vs.get(i).coord(coordinate); } return new Vector(data); }
static private Vector getCoordinateVector(List<Vector> Vs, int coordinate) { int n = Vs.size(); double[] data = new double[n]; for (int i=0; i<n; ++i) { data[i] = Vs.get(i).coord(coordinate); } return new Vector(data); }
public Vector scale (double scale) { int len = size(); double[] coords = new double[len]; for (int i=0; i<len; ++i) coords[i] = _data[i]*scale; return new Vector(coords); }
/** * Calculate a scaled version of this vector. * * @param scale The scale to apply. * @return A new vector whose value is a scaled version of this vector. */ public Vector scale (double scale) { int len = size(); double[] coords = new double[len]; for (int i=0; i<len; ++i) coords[i] = _data[i]*scale; return new Vector(coords); }
public Vector add (Vector v) { int len = size(); if (v.size() != len) throw new IllegalArgumentException("Attempt to add vectors of different lengths"); double[] result = new double[len]; for (int i=0; i<len; ++i) { result[i] = _data[i] + v._data[i]; } return new Vector(result); }
public Vector subtract (Vector v) { int len = size(); if (v.size() != len) throw new IllegalArgumentException("Attempt to subtract vectors of different lengths"); double[] result = new double[len]; for (int i=0; i<len; ++i) { result[i] = _data[i] - v._data[i]; } return new Vector(result); }
/** * Calculate the difference of this and another vector. Both vectors must * have the same number of dimensions. Neither input vector is altered. * * @param that The other vector * @return The difference between the two vectors (this - that) */ public Vector subtract (Vector that) { int len = size(); if (that.size() != len) throw new IllegalArgumentException("Attempt to subtract vectors of different lengths"); double[] result = new double[len]; for (int i=0; i<len; ++i) { result[i] = _data[i] - that._data[i]; } return new Vector(result); }
/** * Calculate the sum of this and another vector. Both vectors must have the * same number of dimensions. Neither input vector is altered. * * @param that The other vector * @return The sum of the two vectors. */ public Vector add (Vector that) { int len = size(); if (that.size() != len) throw new IllegalArgumentException("Attempt to add vectors of different lengths"); double[] result = new double[len]; for (int i=0; i<len; ++i) { result[i] = _data[i] + that._data[i]; } return new Vector(result); }
@Test public void testVectorEquality () { Assert.assertFalse(new Vector(Double.NaN).equals(new Vector(0))); Assert.assertFalse(new Vector(0).equals(new Vector(Double.NaN))); Assert.assertTrue(new Vector(0).equals(new Vector(0))); Assert.assertTrue(new Vector(Double.NaN).equals(new Vector(Double.NaN))); }
@Test public void testSphereUnitVectorCalculation () { Assert.assertEquals(new Vector( 0, 0, 1), SphereUtilities.toUnitVector(0, 0)); Assert.assertEquals(new Vector( 0, 0, -1), SphereUtilities.toUnitVector(0, Math.PI)); Assert.assertEquals(new Vector( 1, 0, 0), SphereUtilities.toUnitVector(0, Math.PI/2.0)); Assert.assertEquals(new Vector(-1, 0, 0), SphereUtilities.toUnitVector(Math.PI, Math.PI/2.0)); Assert.assertEquals(new Vector( 0, 1, 0), SphereUtilities.toUnitVector(Math.PI/2.0, Math.PI/2.0)); Assert.assertEquals(new Vector( 0, -1, 0), SphereUtilities.toUnitVector(-Math.PI/2.0, Math.PI/2.0)); }
@Test public void testInterpolation () { Assert.assertEquals(new Vector(0.00*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.0)); Assert.assertEquals(new Vector(0.10*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.2)); Assert.assertEquals(new Vector(0.20*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.4)); Assert.assertEquals(new Vector(0.30*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.6)); Assert.assertEquals(new Vector(0.40*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.8)); Assert.assertEquals(new Vector(0.50*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 0.50*Math.PI, 1.0)); Assert.assertEquals(new Vector(0.00*Math.PI, 0.5*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.00*Math.PI, 0.00*Math.PI, 0.0)); Assert.assertEquals(new Vector(0.00*Math.PI, 0.4*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.00*Math.PI, 0.00*Math.PI, 0.2)); Assert.assertEquals(new Vector(0.00*Math.PI, 0.3*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.00*Math.PI, 0.00*Math.PI, 0.4)); Assert.assertEquals(new Vector(0.00*Math.PI, 0.2*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.00*Math.PI, 0.00*Math.PI, 0.6)); Assert.assertEquals(new Vector(0.00*Math.PI, 0.1*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.00*Math.PI, 0.00*Math.PI, 0.8)); Assert.assertEquals(new Vector(0.00*Math.PI, 0.0*Math.PI), SphereUtilities.interpolate(0.00*Math.PI, 0.50*Math.PI, 0.00*Math.PI, 0.00*Math.PI, 1.0)); } }
@Override protected double getRelativeError (Position a, Position b, Position c) { Vector vA = new Vector(a.getLongitude(), a.getLatitude()); Vector vB = new Vector(b.getLongitude(), b.getLatitude()); Vector vC = new Vector(c.getLongitude(), c.getLatitude()); // a, b, and c really should be Positions, but we're treating them as // straight vectors anyway. Vector ac = vC.subtract(vA); Vector ab = vB.subtract(vA); double lac2 = ac.vectorLengthSquared(); Vector bPerp = ab.subtract(ac.scale(ab.dot(ac)/lac2)); double lbPerp = bPerp.vectorLength(); double lac = Math.sqrt(lac2); return lbPerp/lac; }
@Override protected double getRelativeError (Position a, Position b, Position c) { Vector vA = new Vector(a.getLongitude(), a.getLatitude()); Vector vB = new Vector(b.getLongitude(), b.getLatitude()); Vector vC = new Vector(c.getLongitude(), c.getLatitude()); // a, b, and c really should be Positions, but we're treating them as // straight vectors anyway. Vector ac = vC.subtract(vA); Vector ab = vB.subtract(vA); double lac2 = ac.vectorLengthSquared(); Vector bPerp = ab.subtract(ac.scale(ab.dot(ac)/lac2)); double lbPerp = bPerp.vectorLength(); double lac = Math.sqrt(lac2); return lbPerp/lac; }
public Vector cross (Vector v) { if (3 != size() || 3 != v.size()) throw new IllegalArgumentException("Attempt to take the cross product of non-3-vectors"); double xa = coord(0); double ya = coord(1); double za = coord(2); double xb = v.coord(0); double yb = v.coord(1); double zb = v.coord(2); return new Vector(ya*zb-za*yb, za*xb-xa*zb, xa*yb-ya*xb); }
@Test public void testCrossProduct () { Vector X = new Vector(1, 0, 0); Vector Y = new Vector(0, 1, 0); Vector Z = new Vector(0, 0, 1); Assert.assertEquals(Z, X.cross(Y)); Assert.assertEquals(Y, Z.cross(X)); Assert.assertEquals(X, Y.cross(Z)); Assert.assertEquals(Z.scale(-1.0), Y.cross(X)); Assert.assertEquals(Y.scale(-1.0), X.cross(Z)); Assert.assertEquals(X.scale(-1.0), Z.cross(Y)); } }
/** * Calculate the cross product of two vectors. Both vectors must be of 3 components. * @param v cross product operand * @return The cross product */ public Vector cross (Vector v) { if (3 != size() || 3 != v.size()) throw new IllegalArgumentException("Attempt to take the cross product of non-3-vectors"); double xa = coord(0); double ya = coord(1); double za = coord(2); double xb = v.coord(0); double yb = v.coord(1); double zb = v.coord(2); return new Vector(ya*zb-za*yb, za*xb-xa*zb, xa*yb-ya*xb); }