Playing with Vectors

Vectors: Sums, Differences, & Scalar Multiples

A two-dimensional vector, $\bar{v} = \begin{pmatrix}x\\y\end{pmatrix}$, can be thought of representing the displacement that, when starting at the origin, would move something to the point $(x,y)$.

Consequently, a natural interpretation for the sum of two vectors would be the total displacement that results from their individual applications. This can be seen in the picture below, where the red vector represents the total displacement resulting from the application of the blue and green vectors. Fortunately, as seen below, whether we follow the the displacement arising from the blue vector with that from the green vector (the solid segments), or vice-versa (the dashed segments), the overall displacement is the same. (Hence, the addition of vectors is commutative.)

As indicated in the picture, this suggests how we might find the sum of two vectors algebraically as well:
$$\begin{pmatrix}x_1\\y_1\end{pmatrix} + \begin{pmatrix}x_2\\y_2\end{pmatrix} = \begin{pmatrix}x_1+x_2\\y_1+y_2\end{pmatrix}$$
The above then forces how differences of two such vectors must be calculated:
$$\begin{pmatrix}x_1\\y_1\end{pmatrix} - \begin{pmatrix}x_2\\y_2\end{pmatrix} = \begin{pmatrix}x_1-x_2\\y_1-y_2\end{pmatrix}$$
This also suggests what we should mean by a constant (or scalar) multiple of a vector. For example, it would be natural to assume that $3\begin{pmatrix}x\\y\end{pmatrix}$ is a displacement three times as large as $\begin{pmatrix}x\\y\end{pmatrix}$. Consequently, $$3\begin{pmatrix}x\\y\end{pmatrix} = \begin{pmatrix}x\\y\end{pmatrix} + \begin{pmatrix}x\\y\end{pmatrix} + \begin{pmatrix}x\\y\end{pmatrix} = \begin{pmatrix}x+x+x\\y+y+y\end{pmatrix} = \begin{pmatrix}3x\\3y\end{pmatrix}$$
In keeping with this example, we define a scalar multiple of a vector, for any real value $c$ (which is called the "scalar" in question) to be
$$c\begin{pmatrix}x\\y\end{pmatrix} = \begin{pmatrix}cx\\cy\end{pmatrix}$$

Of course, this concept easily generalizes beyond 2 dimensions. In n-dimensional space, we consider a vector be an n-tuple of the following form (which we write horizontally, in the interests of space)
Then, following the example above -- sums, differences, and scalar multiples of vectors can be found in the following way:

$$(a_1,a_2,a_3,\ldots,a_n) + (b_1,b_2,b_3\ldots,b_n) = (a_1+b_1,a_2+b_2,a_3+b_3,\ldots,a_n+b_n)$$
$$(a_1,a_2,a_3,\ldots,a_n) - (b_1,b_2,b_3\ldots,b_n) = (a_1-b_1,a_2-b_2,a_3-b_3,\ldots,a_n-b_n)$$
$$c(a_1,a_2,a_3,\ldots,a_n) = (ca_1,ca_2,ca_3,\ldots,ca_n)$$

The Dot Product

Suppose we have two $n$-dimensional vectors $x$ and $y$ as shown below:
$$x = (x_1,x_2,x_3,\ldots,x_n) \quad \quad \textrm{and} \quad \quad y = (y_1,y_2,y_3,\ldots,y_n)$$
We define the dot product of these two vectors, denoted $x \cdot y$ in the following way
$$x \cdot y = x_1 y_1 + x_2 y_2 + x_3 y_3 + \cdots + x_n y_n$$
First, note that we can easily show the dot product is commutative:
x \cdot y & = & x_1 y_1 + x_2 y_2 \cdots + x_n y_n \\
& = & y_1 x_1 + y_2 x_2 \cdots + y_n x_n \\
& = & y \cdot x
Almost just as easily, we can show that the dot product distributes over vector sums:
x \cdot (y + z) & = & x_1 (y_1 + z_1) + x_2 (y_2 + z_2) + \cdots x_n (y_n + z_n)\\
& = & x_1 y_1 + x_1 z_1 + x_2 y_2 + x_2 z_2 + \cdots + x_n y_n + x_n z_n\\
& = & (x_1 y_1 + x_2 y_2 \cdots + x_n y_n) + (x_1 z_1 + x_2 z_2 \cdots + x_n z_n)\\
& = & x \cdot y + x \cdot z
As one more observation, note that for any scalar $k$, we have
(kx) \cdot y & = & kx_1 y_1 + kx_2 y_2 + \cdots + kx_n y_n\\
& = & k(x_1 y_1 + x_2 y_2 \cdots + x_n y_n)\\
& = & k(x \cdot y)
We will use these three properties in our calculation of the angle betwee vectors below.

The Norm of a Vector, and Normalizing Vectors

We define the norm of a vector $x$, denoted by $|x|$, by
$$|x| = \sqrt{x_1^2 + x_2^2 + \cdots + x_n^2}$$
This is meant to be geometrically interpreted as the length of the vector, or equivalently, the distance between the points $(0,0,...,0)$ and $(x_1,x_2,...,x_n)$, and in two dimensions is easily seen to be a direct result of the Pythagorean Theorem.

Interestingly, we note that this can be written in a much shorter way by invoking the dot product:
$$|x| = \sqrt{x \cdot x}$$

When we multiply a vector by a scalar that results in the norm of the new vector being one, we are said to be normalizing the vector.

The Angle between Two Vectors

Now, armed with the ideas of the dot product and the norm of a vector, suppose we are interested in finding the angle between two given vectors, $x$ and $y$.

Recall that the Law of Cosines, a generalization on the Pythagorean Theorem, gives us the relationship between the side lengths of an arbitrary triangle. Specifically, if a triangle has side lengths $a$, $b$, and $c$, then
$$a^2 + b^2 - 2ab\cos \theta = c^2$$
where $\theta$ is the angle between the sides of length $a$ and $b$.

Consider the triangle that can be formed from the vectors $x$, $y$, and $x-y$.

Applying the Law of Cosines to this triangle, we have
$$|x|^2 + |y|^2 - 2|x||y|\cos \theta = |x-y|^2$$
But this implies, using our observations about the dot product made above, that
(x \cdot x) + (y \cdot y) - 2|x||y|\cos \theta & = & (x-y) \cdot (x-y)\\
& = & x \cdot (x-y) - y \cdot (x-y)\\
& = & (x \cdot x) - (x \cdot y) - (y \cdot x) + (y \cdot y)\\
& = & (x \cdot x) - (x \cdot y) - (x \cdot y) + (y \cdot y)\\
& = & (x \cdot x) - 2(x \cdot y) + (y \cdot y)\\
Subtracting the common $(x \cdot x)$ and $(y \cdot y)$ from both sides, we find
$$- 2|x||y|\cos \theta = - 2(x \cdot y)$$
Which, solving for $\cos \theta$ tells us
$$\cos \theta = \frac{x \cdot y}{|x||y|}$$

The Cross Product

Finally, for 3-dimensional vectors $x = (x_1,x_2,x_3)$ and $y=y_1,y_2,y_3)$, we can additionally define the cross product in the following way:
$$x \times y = (x_2 y_3 - x_3 y_2, x_3 y_1 - x_1 y_3, x_1 y_2 - x_2 y_1)$$

Note: there is no analogous definition for the cross product of two 2-dimensional vectors! Nor is there an analogous definition for the cross product of two vectors of dimension higher than 3!

...although there is a way to take an analogous "cross product" of $(n-1)$ n-dimensional vectors -- but we won't consider this last possibility here, in the interest of staying focused...

The definition of cross product supplied above perhaps feels a bit arbitrary here, but motivating it properly would take us too far into the realm of linear algebra for our purposes. Suffice it to say, when defined in the above way, some very useful applications result -- especially in physics.

Finally, the Task...

Write classes named Vector, Vector2d, and Vector3d, with appropriate constructors and methods, where Vector2d and Vector3d are subclasses of the Vector class, and so that the class VectorTester shown below produces the output given. Be efficient in your code -- when you can, use existing methods to help code later methods. Also, choose carefully where you define your methods -- either in the Vector class or in one of its subclasses. You will save yourself a considerable bit of time and coding if you do these two things well.

public class VectorTester {

	public static void main(String[] args) {
		double[] v1Array = {3,-4};
		double[] v3Array = {-1,3,5};
		double[] v4Array = {3,4,5,6};
		double[] v5Array = {1,2,-1,3};
		//testing all of the constructors...
		Vector2d v0 = new Vector2d(0.8,0.6);			
		Vector2d v1 = new Vector2d(v1Array);  			
		Vector3d v2 = new Vector3d(4,2,3);    			
		Vector3d v3 = new Vector3d(v3Array);  			
		Vector v4 = new Vector(v4Array);      			
		Vector v5 = new Vector(v5Array);
		Vector v6 = new Vector3d(4,2,3);
		//test toString() methods in each class
		System.out.println("v0 = " + v0);
		System.out.println("v1 = " + v1);
		System.out.println("v2 = " + v2);
		System.out.println("v3 = " + v3);
		System.out.println("v4 = " + v4);
		System.out.println("v5 = " + v5);
		//test add() methods in each class
		System.out.println("v0 + v1 = " + v0.add(v1));
		System.out.println("v2 + v3 = " + v2.add(v3));
		System.out.println("v4 + v5 = " + v4.add(v5));
		//test scale() methods in each class
		System.out.println("3 * v0 = " + v0.scale(3));
		System.out.println("4 * v2 = " + v2.scale(4));
		System.out.println("-5 * v4 = " + v4.scale(-5));
		//test subtract() method in Vector class
		System.out.println("v4 - v5 = " + v4.subtract(v5));
		//test dot() method in Vector class
		System.out.println("v4 . v5 = " +;
		//test norm() method (which gives the length of the vector)
		System.out.println("length of v1 = " + v1.norm());
		//test angle() method 
		System.out.println("angle between v0 and v1 (in radians) = " + v1.angle(v0));

		//test cross() method of Vector3d class
		System.out.println("v2 x v3 = " + v2.cross(v3));
		//test normalize() method of Vector class
		System.out.println("v1 normalized = " + v1.normalize());
		//test equals() method of Vector class
		System.out.println("v2 = v6 is a " + v2.equals(v6) + " statement");
                System.out.println("v3 = v6 is a " + v3.equals(v6) + " statement");



v0 = (0.8, 0.6)
v1 = (3.0, -4.0)
v2 = (4.0, 2.0, 3.0)
v3 = (-1.0, 3.0, 5.0)
v4 = (3.0, 4.0, 5.0, 6.0)
v5 = (1.0, 2.0, -1.0, 3.0)
v0 + v1 = (3.8, -3.4)
v2 + v3 = (3.0, 5.0, 8.0)
v4 + v5 = (4.0, 6.0, 4.0, 9.0)
3 * v0 = (2.4000000000000004, 1.7999999999999998)
4 * v2 = (16.0, 8.0, 12.0)
-5 * v4 = (-15.0, -20.0, -25.0, -30.0)
v4 - v5 = (2.0, 2.0, 6.0, 3.0)
v4 . v5 = 24.0
length of v1 = 5.0
angle between v0 and v1 (in radians) = 1.5707963267948966
v2 x v3 = (1.0, -23.0, 14.0)
v1 normalized = (0.6000000000000001, -0.8)
v2 = v6 is a true statement
v3 = v6 is a false statement