4.2.8.1 - Vectors, translation and dot products (Java)
Represent vectors as lists, arrays and functions, calculate translation, scaling and convex combinations, and use a dot product to find an angle.
Java route status: incomplete: runnable support pending. Java source tasks are written practice.
List, array and function representations
A vector is an ordered collection of components drawn from the same field, such as the real numbers.
The vector is a four-vector over , written as an element of . Its dimension is four, regardless of the values or the amount of memory used. A Java double approximates real values with finite precision; it does not implement every real number exactly.
The same vector can be interpreted as a function , where . Its mappings are , , and . The symbol means maps to. The input is a component index and the output is that component's value; this is not a function taking an arbitrary real input.
// import java.util.*;
double[] array = {2.0, 3.14159, -1.0, 2.718281828};
List<Double> list = Arrays.asList(2.0, 3.14159, -1.0, 2.718281828);
Map<Integer, Double> function = new HashMap<>();
for (int i = 0; i < array.length; i++) function.put(i, array[i]);
double third = function.get(2); // -1.0
The array and ordered list preserve component order directly. The dictionary preserves the mapping through keys even if iteration order changes. To represent this whole four-vector, every index in the domain must have its specified component; an omitted key is not automatically zero. The representation is Java-specific, while the vector and function meanings are mathematical.
Geometric vectors
A two-vector can represent a point or a displacement in a coordinate plane.
The vector is drawn with tail at the origin and head at : two units across and three up. Moving the arrow's tail does not change the displacement if its direction and length remain the same. A point uses a position relative to an origin; a displacement describes a change in position.
[DIAGRAM: asset_slug: vector_coordinates; description: Exact coordinate plot of u equals (2,3), v equals (4,1), their sum (6,4), and convex point (3.5,1.5).]

The magnitude is . Negative components point in the corresponding negative direction. A zero vector has zero magnitude and no direction, so an angle involving it cannot be found by the nonzero-vector formula.
Addition, scaling and convex combinations
Vector operations combine corresponding components and require matching dimensions.
For and , their sum is . Adding translates the point by four across and one up. Multiplying by 2 gives , scaling its magnitude by 2; a negative scalar also reverses direction and zero gives the zero vector.
A convex combination is , where and . It gives a point on the segment between the two vector positions, including the endpoints. For and , the result is . Coefficients summing to one alone are insufficient: a negative coefficient is not convex.
static double[] add(double[] u, double[] v) {
if (u.length != v.length) throw new IllegalArgumentException("Dimension");
double[] result = new double[u.length];
for (int i = 0; i < u.length; i++) result[i] = u[i] + v[i];
return result;
}
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account