A scalar is a “rank-0” tensor (a single number). A vector is a “rank-1” tensor (an array). A matrix is a “rank-2” tensor (a grid). Beyond these lie higher-rank tensors—multi-dimensional arrays that follow specific transformation rules. Tensors are the natural language of General Relativity, Quantum Mechanics, and modern Deep Learning.
What is a Tensor?
While computer scientists often define a tensor as “a multidimensional array,” mathematicians define it by how it transforms or what it does.
A tensor is a multilinear map. Just as a matrix represents a linear map , a tensor takes multiple vectors as input and produces a scalar (or another vector). For example, a rank-2 tensor is a function that is linear in AND linear in .
Covariance and Contravariance
In physics, we distinguish between:
Contravariant vectors (): Things like velocity or position that scale “with” the coordinate system.
Covariant vectors (): Things like gradients or dual vectors that scale “against” the coordinate changes.
A general tensor can have both types of indices. This distinction is crucial for ensuring that physical laws remain the same regardless of the units or axes we choose.
The Tensor Product
The tensor product is a way to combine two vector spaces and into a larger space . If and , then is an element of the product space.
If and , the tensor product has dimension . You can think of this as the space of all possible matrices.
Interactive Lab
import numpy as np
# Create two vectors
v = np.array([1, 2, 3])
w = np.array([4, 5])
# Compute the Outer Product (a simple tensor product)
# This results in a Rank-2 tensor (Matrix)
T = np.outer(v, w)
print("Vector v (Rank 1):", v)
print("Vector w (Rank 1):", w)
print("\nTensor Product v ⊗ w (Rank 2 Matrix):\n", T)
# Higher-order tensor via expansion
# A Rank-3 tensor could be representing interactions
# between three spaces.
T3 = np.zeros((3, 2, 2))
T3[0, :, :] = np.eye(2)
T3[1, :, :] = np.ones((2, 2))
T3[2, :, :] = np.zeros((2, 2))
print("\nRank-3 Tensor (3x2x2):\n", T3)
print("\nAccessing a slice T3[0]:\n", T3[0])
python
1import numpy as np
2
3# Create two vectors
4v = np.array([1, 2, 3])
5w = np.array([4, 5])
6
7# Compute the Outer Product (a simple tensor product)
8# This results in a Rank-2 tensor (Matrix)
9T = np.outer(v, w)
10
11print("Vector v (Rank 1):", v)
12print("Vector w (Rank 1):", w)
13print("\nTensor Product v ⊗ w (Rank 2 Matrix):\n", T)
14
15# Higher-order tensor via expansion
16# A Rank-3 tensor could be representing interactions
17# between three spaces.
18T3 = np.zeros((3, 2, 2))
19T3[0, :, :]= np.eye(2)
20T3[1, :, :]= np.ones((2, 2))
21T3[2, :, :]= np.zeros((2, 2))
22
23print("\nRank-3 Tensor (3x2x2):\n", T3)
24print("\nAccessing a slice T3[0]:\n", T3[0])
25
Multilinear Forms and Determinants
The determinant is the most famous example of a multilinear form. It is an alternating -tensor. “Alternating” means that if you swap two input vectors, the sign of the output flips. This property is what allows the determinant to measure signed volume.
Einstein Summation Notation
In tensor calculus, we often drop the summation sign . If an index appears twice (once up, once down), it is summed over.
Instead of:
We write:
This compact notation is the standard in engineering and theoretical physics.
Exercises
Knowledge Check
How is a rank-3 tensor typically visualized in computer science?
Answer: A 'cube' or 'stack' of matrices.
While Rank-1 is a line and Rank-2 is a grid, Rank-3 adds a third dimension, effectively creating a 3D block of numerical data.
How is a rank-3 tensor typically visualized in computer science?
Knowledge Check
What does it mean for a map to be 'multilinear'?
Answer: It is linear with respect to each of its input arguments independently.
In a multilinear map f(u, v, w), if you hold v and w constant, f behaves as a linear function of u. The same applies for v and w individually.
What does it mean for a map to be 'multilinear'?
Knowledge Check
In Einstein notation, what does 'R_ii' (repeated index) imply?
Answer: A summation over the index i: R_11 + R_22 + ... + R_nn.
The repetition of an index in a single term indicates an implicit summation over all possible values of that index (the trace of the tensor).
In Einstein notation, what does 'R_ii' (repeated index) imply?