A vector in physics is a quantity that has both a size (magnitude) and a direction, such as velocity, force or displacement. In code, a vector is usually an ordered list of numbers, one per axis, and many languages also use the word "vector" for a plain growable array.
Those are two related ideas that share a name. The first half of this article covers the physics meaning, then each section shows how it turns into code.
What is a vector in physics?
OpenStax's University Physics defines a vector quantity as one that is specified completely by giving a number of units (the magnitude) and a direction. A quantity that needs only a number and units is a scalar.
| Scalar (magnitude only) | Vector (magnitude and direction) |
|---|---|
| time, mass, distance, length, volume, temperature, energy | displacement, velocity, position, force, torque |
Both lists come from the OpenStax chapter. Distance and displacement show the difference well. Walking 3 m east and then 3 m back west covers a distance of 6 m, but the displacement is zero, because it only compares the start and end points.
A vector is drawn as an arrow from an initial point to a terminal point. The magnitude is the length of the arrow and is always a positive scalar. The direction is where the arrow points.
How a vector becomes a list of numbers
A drawing is no use to a program, so a vector is split into components, one per axis. A velocity of 3 m/s along x and 4 m/s along y becomes the pair (3, 4). The direction is now encoded in the signs and relative sizes of the numbers, and the magnitude is computed from them.
For two components, the magnitude is the Pythagorean length: the square root of 3 squared plus 4 squared, which is 5 m/s. You can check that arithmetic by hand. The same rule extends to any number of components: square each one, add them, take the square root.
This is why vectors in code are lists of numbers. A 3D position is three numbers. A color can be three numbers. A text embedding can be hundreds of numbers, which is the idea behind vector databases, though those are an application of the math rather than a physics quantity.
Vector math in Python with NumPy
NumPy stores vectors as 1-D arrays. The two operations you meet first are the magnitude and the dot product.
The NumPy documentation says numpy.linalg.norm returns the 2-norm of a 1-D array by default, which is the magnitude described above:
import numpy as np
velocity = np.array([3, 4])
print(np.linalg.norm(velocity))
Example output:
5.0
The NumPy documentation also states that numpy.dot on two 1-D arrays is the inner product of the vectors, the sum of the element-wise products:
import numpy as np
print(np.dot([1, 2, 3], [4, 5, 6]))
Example output:
32
By hand that is 1×4 + 2×5 + 3×6 = 32. In physics, the dot product of a force vector and a displacement vector gives work, which is a scalar. Note that the NumPy page says numpy.dot does not apply complex conjugation to its first argument; numpy.vdot does.
Adding vectors works component by component: np.array([3, 4]) + np.array([1, -2]) gives [4, 2]. That is the code version of placing arrows head to tail.
What "vector" means in C++, Rust and Java
In these languages a vector is a data structure, not a physical quantity. The name comes from the same mathematical sense of an ordered sequence of values, but the structure carries no notion of direction or magnitude. You can have a vector of strings.
| Language | Type | What the documentation says |
|---|---|---|
| C++ | std::vector<T> | A sequence container that encapsulates dynamic size arrays. Elements are stored contiguously. |
| Rust | Vec<T> | A contiguous growable array type, short for "vector". Heap allocated once it has capacity. |
| Java | java.util.Vector | A growable array of objects, and unlike newer collections it is synchronized. |
C++ std::vector
Because the elements are contiguous, a pointer to the first element can be passed to any function that expects a plain array pointer. Random access is constant time, and adding or removing at the end is amortized constant time. When the allocated memory runs out, the vector reallocates, which cppreference calls usually costly. If you know the size in advance, reserve() avoids the repeated reallocations. capacity() reports how much memory is allocated, which is usually more than the number of elements.
One exception: std::vector<bool> is a specialization that may be optimized for space, and cppreference lists the general container guarantees as applying to types other than bool. Do not hand its storage to code that expects a plain bool array.
Rust Vec
The Rust documentation separates length (elements stored) from capacity (space allocated):
let mut numbers = vec![1, 2, 3];
numbers.push(4);
assert_eq!(numbers.len(), 4);
let buffer: Vec<i32> = Vec::with_capacity(10);
assert_eq!(buffer.len(), 0);
assert!(buffer.capacity() >= 10);
push only reallocates when length equals capacity, and the growth strategy is not guaranteed. A Vec never shrinks by itself; call shrink_to_fit to release spare capacity.
Java Vector
Java's Vector predates the collections framework redesign. Its documentation says it is synchronized and recommends ArrayList in its place when thread safety is not needed. New code almost always uses ArrayList.
Which meaning do you need?
- Doing physics, graphics or machine learning math: you want a fixed-length list of numbers with arithmetic defined on it. Use NumPy arrays in Python, or a math library's vector types in other languages.
- Storing a collection that grows: you want
std::vector,VecorArrayList. The elements need not be numbers. - Reading about images: "vector" there means shapes described by math rather than pixels, a third usage. See vector vs raster images.
If the two meanings get mixed up
A common slip is treating a growable-array "vector" as if it had vector arithmetic. In C++, a + b on two std::vector<double> objects does not compile, because the type defines no addition. Loop over the elements, use std::transform, or use a numeric library. The reverse slip is storing a physical vector as separate loose variables; keep the components together in one array so the operations above apply to them as a unit.
Also keep units consistent. Components of one vector must be in the same unit, and a magnitude carries that unit: 5 m/s, not 5.
Frequently asked questions
- Is a vector in physics the same as a vector in programming?
- Not exactly. A physics vector is a quantity with a magnitude and a direction, such as velocity. In numerical code you store it as an ordered list of numbers, one per axis, so the two meet in libraries like NumPy. A programming 'vector' such as C++ std::vector or Rust Vec is often just a growable array that holds any type of element and has no direction at all.
- Is speed a vector?
- No. Speed is a scalar: a number with units and no direction. Velocity is the vector version, because it adds a direction. A car doing 20 m/s north and a car doing 20 m/s south have the same speed and different velocities.
- Can a vector have a negative magnitude?
- No. Magnitude is the length of the vector, so it is never negative. A negative sign on a component, such as -3 on the x axis, means the vector points toward negative x, not that its length is negative.
- Why is Java's Vector class discouraged?
- Java's Vector is synchronized, so every operation pays for thread safety. The Java SE documentation recommends ArrayList instead when thread safety is not needed. Vector remains in the standard library for older code.