Find a basis for each eigenspace!
Related
The challenge is just as it seems; you are to find a basis for each eigenspace of a matrix. If you have taken Linear Algebra 1, you'll understand what this means, but this is an attempted full explanation of eigenvalues, eigenvectors, eigenspaces, what a basis is, and how to obtain all of these.
This is the challenge; the explanations are below so you can skip them if you already know how to do this.
Input
The input will be a single matrix given in any reasonable format according to PPCG conventions.
Output
The output will be an array of bases. There will be one basis for each eigenspace. You can choose whether or not to repeat eigenvalues with an arithmetic multiplicity of more than 1. This can be output in any reasonable format, probably a 3D array.
Vectors
In Computer Science terms, a vector is an array of values. For the purposes of this, we are assuming that all of these values are in Q
; that is, they are all rational numbers. Wikipedia: Vector space
Matrices
In Computer Science terms, a matrix is a 2D array of values. For the purposes of this, we are assuming that all of these values are in Q
; that is, they are all rational numbers. Wikipedia: Matrix
Terminology
Let A
be an m x n
matrix (with m
rows and n
columns) and let x
be a size-k
vector. Then, Aij
is the element in the i
-th row in the j
-th column of A
with 0 < i <= m
and 0 < j <= n
(they are 1-indexed). Also, xi
is the i
-th element of the vector x
with 0 < i <= k
.
The Zero Vector and the Zero Matrix
The zero vector is the vector containing all zeros. Similarly, the zero matrix is the matrix containing all zeros. It is important to note that the size still needs to be considered.
Scalar Multiplication
Scalar multiplication by a value s
(again we are assuming it is rational) is quite easy to understand; simply multiply each element by s
. Thus, we say that x
is a scalar multiple of y
if and only if x = sy
. Note that the zero-vector is a scalar multiple of any vector but all non-zero vectors are not scalar multiples of the zero vector.
Matrix-Vector Multiplication (MathInsight)
Let A
be an m x n
matrix (with m
rows and n
columns) and let x
be a size-n
vector. Then, Ax = y
where y
is a size-m
vector defined as such:
yi = Ai1*x1 + Ai2*x2 + ... + Ain*xn
That is, the i
-th element in the result is the dot product of the i
-th row of A
and x
.
Let λ != 0
be any real number, A
be an m x n
matrix, and x != 0
be a size-n
vector (not the number 0
, but the 0
-vector, which contains all zeros). Then, if Ax = λx
, we say that x
is an eigenvector of A
corresponding to λ
, and similarly, we say that λ
is an eigenvalue of A
corresponding to x
.
Determining Eigenvectors
It's easy to determine if x
is an eigenvector of A
. Simply check if Ax
is a scalar multiple of x
, which also gives us the corresponding eigenvalue λ
. However, it is harder to find eigenvectors than to check it a vector is one.
Finding Eigenvalues
Conversely, it's easier to find the eigenvalues but harder to check if a value is one, which is fortunate for this challenge. Firstly, we must look at determinants.
Determinants
The determinant of a 2 x 2
matrix [[a, b], [c, d]]
is ad - bc
. This is a specific definition of the determinant, so in order to explain the recursive definition of a determinant, I will say that the determinant of a 1 x 1
matrix is that value. Note that only square matrices have a determinant.
The recursive definition of a determinant
In order to give the recursive definition of a determinant, I first need to explain what A(i,j)
means. A(i,j)
is the matrix formed by removing the i
-th row and the j
-th column from A
; thus, if A
is an n x n
matrix, A(i,j)
is an n-1 x n-1
matrix. Thus, we recursively define the determinant of an n x n
matrix A
, which we call det A
, as sum x=1->n (-1)^(x+1)*(det A(1,n))
. In other words, we go down the left-most column and take the values obtained from multiplying this value with the matrix formed by taking out the row and column it is in, and then alternate adding and subtracting those values. See the Wikipedia link for more information. This definition is recursive.
Also note that we do not have to expand on the left-most column, we can also expand on any column or any row.
So, how do determinants help us?
Well, I'm not going to prove it, but if we have C(λ) = 0
, then λ
is an eigenvalue of A
. C
is the characteristic polynomial function, which is gives the determinant of A - λI
, where λI
is scalar-multiplication between scalar λ
and the Identity Matrix I
, which has all 1
s on its main diagonal and 0
s everywhere else.
So, given an eigenvalue λ
, we can find the RREF of A - λI
. Let this matrix be M
. Then, the eigenspace of λ
corresponding to A
is the set of all vectors x
such that Mx
is the zero-vector.
We have finally arrived at the task. Your task is to output, for each eigenvalue, a basis of the corresponding eigenspace.
Linear Combination
A linear combinations of vectors x1, x2, ..., xn
with scalars s1, s2, ..., sn
is s1x1 + ... + snxn
. When adding vectors, you add each individual element.
Spanning Set
S
is a spanning set of B
if and only if every vector in B
can be written as a linear combination of S
with any real numbers as scalars.
Linearly Independent Set
L
is a linearly independent set if and only if no vector is a linear combination of the other vectors (thus L
cannot contain the zero-vector), and it is linearly dependent otherwise.
Basis: a linearly independent spanning set
A basis is simply a spanning set that's linearly independent. All possible bases will have the same number of vectors (proof omitted).
The Challenge
Now that you hopefully understood my explanations, we arrive at the following challenge:
Given an n x n
matrix with 0 < n <= 20
, for each eigenvalue (there will be at most n
distinct ones), output a basis for the eigenspace of the eigenvalue. You can choose whether or not to output duplicates. You can choose the format for input and output within reasonable conditions. My recommendation is a 2D array for input and a 3D array for output. The input matrix is guaranteed to have only real eigenvalues, and is guaranteed to be square.
I will add test cases if this challenge is posted because test cases are quite hard to create for this challenge!
Fun Fact: "eigen" means "special" in German!
code-golf math matrix