Linear Combinations
A linear combination mixes vectors using the only two operations we have: scale each one, then add. Almost every question in linear algebra comes down to linear combinations: can this vector be built from those? In how many ways? And asking that question turns out to be the same as solving a system of linear equations.
The definition
Given vectors in and scalars , the vector
is a linear combination of with coefficients (or weights) . Example:
Building every vector from the standard basis
The standard basis vectors of are , and . Every vector is a linear combination of them, with its own components as coefficients:
The same holds in with . Other sets of vectors may or may not be able to build everything; that is the topic of Spans.
Is b a linear combination? Solve a system
Is a linear combination of , and ? We need with . Comparing components gives one equation per component:
The columns of the coefficient matrix are exactly , and the right-hand side is . So:
is a linear combination of the system with augmented matrix is consistent.
from sympy import Matrix
v1, v2, v3 = Matrix([1, 0, 3]), Matrix([2, 1, 1]), Matrix([0, 1, 2])
b = Matrix([4, 2, 9])
A = Matrix.hstack(v1, v2, v3) # vectors as columns
c = A.solve(b)
print("coefficients:", list(c))
print("check:", list(c[0] * v1 + c[1] * v2 + c[2] * v3))coefficients: [2, 1, 1]
check: [4, 2, 9]
So . When the system is inconsistent, is not a combination; when it has infinitely many solutions, can be built in infinitely many ways.
from sympy import Matrix, symbols, linsolve
c1, c2 = symbols("c1 c2")
u1, u2 = Matrix([1, 2, 0]), Matrix([0, 1, 1])
for b in [Matrix([2, 7, 3]), Matrix([1, 1, 1])]:
aug = Matrix.hstack(u1, u2, b)
print(list(b), "->", linsolve(aug, [c1, c2]))[2, 7, 3] -> {(2, 3)}
[1, 1, 1] -> EmptySet
, but cannot be built from and : the two vectors only reach a plane through the origin in .
is a linear combination of the columns of A
This observation is worth remembering forever. For a matrix with columns and a vector :
import numpy as np
A = np.array([[1, 2, 0], [0, 1, 1], [3, 1, 2]])
x = np.array([2, 1, 1])
print("A @ x :", A @ x)
print("sum of x_j * col_j:", sum(x[j] * A[:, j] for j in range(3)))A @ x : [4 2 9]
sum of x_j * col_j: [4 2 9]
So "solve " and "write as a combination of 's columns" are the same problem, seen from two sides. You will use this view again and again, e.g. in Column Space, Row Space and Null Space.
In software
- Colour mixing: every screen colour is a combination of three primary vectors. Converting RGB to another colour space (YUV for video) changes which three vectors you combine.
- Graphics: barycentric coordinates write a point inside a triangle as with ; GPUs use them to blend vertex colours and texture coordinates. Bézier curves are combinations of control points.
- ML: a linear model's prediction is a linear combination of features; an attention layer outputs a weighted combination of value vectors (Attention as Matrix Products).
- Diet and blending problems: which mix of foods (each a vector of nutrients) meets a target vector of nutrients?
Exercises
Exercise 1: Compute a combination
Compute .
Answer
.
Solution
Exercise 2: Find the coefficients
Write as a combination of and .
Answer
.
Solution
Exercise 3: Not reachable
Show that is not a linear combination of and .
Answer
The system is inconsistent.
Solution
Exercise 4: Paint mixing (code)
Three paints contain (red, yellow, blue) pigment fractions , and . How many litres of each make a mix with litres of pigment? Use SymPy with exact fractions.
Answer
2 L of paint 1, 3 L of paint 2 and 2.5 L of paint 3.
Solution
Put the paints as columns and solve. Rational("0.6") keeps 0.6 exact.
from sympy import Matrix, Rational as R
p1 = Matrix([R("0.6"), R("0.3"), R("0.1")])
p2 = Matrix([R("0.1"), R("0.7"), R("0.2")])
p3 = Matrix([R("0.2"), R("0.2"), R("0.6")])
target = Matrix([R("2.0"), R("3.2"), R("2.3")])
print(list(Matrix.hstack(p1, p2, p3).solve(target)))[2, 3, 5/2]
Test yourself
is a linear combination of exactly when…
- is shorter than all three
- is perpendicular to
- are parallel
- the system with columns and right-hand side is consistent
If has columns and , what is ?
What are the coefficients of in the standard basis ?
- 1, 1, 1
- 5, -2, 7
- 7, -2, 5
- They do not exist