4.2.8.1 - Vectors, translation and dot products (Java)

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 [2.0,3.14159,−1.0,2.718281828][2.0,3.14159,-1.0,2.718281828] is a four-vector over R\mathbb{R}, written as an element of R4\mathbb{R}^{4}. 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 f:S→Rf:S\to\mathbb{R}, where S={0,1,2,3}S=\{0,1,2,3\}. Its mappings are 0↦2.00\mapsto2.0, 1↦3.141591\mapsto3.14159, 2↦−1.02\mapsto-1.0 and 3↦2.7182818283\mapsto2.718281828. The symbol ↦\mapsto 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.

Java
// 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 u=[2,3]\mathbf{u}=[2,3] is drawn with tail at the origin and head at (2,3)(2,3): 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).]
Diagram

The magnitude is ∥u∥=22+32=13\lVert\mathbf{u}\rVert=\sqrt{2^2+3^2}=\sqrt{13}. 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.

u+v=[u1+v1,…,un+vn],cu=[cu1,…,cun].\mathbf{u}+\mathbf{v}=[u_1+v_1,\ldots,u_n+v_n],\qquad c\mathbf{u}=[cu_1,\ldots,cu_n].

For u=[2,3]\mathbf{u}=[2,3] and v=[4,1]\mathbf{v}=[4,1], their sum is [6,4][6,4]. Adding v\mathbf{v} translates the point (2,3)(2,3) by four across and one up. Multiplying u\mathbf{u} by 2 gives [4,6][4,6], scaling its magnitude by 2; a negative scalar also reverses direction and zero gives the zero vector.

A convex combination is αu+βv\alpha\mathbf{u}+\beta\mathbf{v}, where α,β≥0\alpha,\beta\geq0 and α+β=1\alpha+\beta=1. It gives a point on the segment between the two vector positions, including the endpoints. For α=0.25\alpha=0.25 and β=0.75\beta=0.75, the result is [0.5+3,0.75+0.75]=[3.5,1.5][0.5+3,0.75+0.75]=[3.5,1.5]. Coefficients summing to one alone are insufficient: a negative coefficient is not convex.

Java
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