Q. TSE: Math: How to calculate the eigenvalues and eigenvectors of a given matrix? Method: QR-Algorithm: PowerShell
Hello,
1. -See also optionally:
2. -To run the program:
1. -Compile the attached program
getmaepo.s
2. -Run the program
3. -Input the given matrix
(must be square)
4. -For example
Let say the matrix is
1 2
3 4
The first number is the dimension of the matrix, e.g. here 2
The next 4 numbers are the elements of that matrix
thus here 1 2 3 4 (separated by spaces.
So concatenating this would give as input
2 1 2 3 4
5. -For example
Let say the matrix is
1 2 3
4 5 6
7 8 9
The first number is the dimension of the matrix, e.g. here 3
The next 9 numbers are the elements of that matrix
thus here 1 2 3 4 5 6 7 8 9 (separated by spaces.
So concatenating this would give as input
3 1 2 3 4 5 6 7 8 9
6. -The output will show
1. -The eigenvalues
2. -The eigenvectors
of that given matrix
E.g.
Eigenvalues
5.37228132326902
-0.372281323269014
Eigenvectors
0.415973557919284,0.909376709132124
-0.824564840132394,0.565767464968992
7. -Tested successfully
on
Microsoft Windows 10 Professional (64 bits)
running
TSE for Microsoft Windows version 4.50 RC24
3. -To calculate the eigenvalues and eigenvectors of a given matrix
here the QR-Algorithm is used, implemented in PowerShell
Source: Artificial Intelligence (=AI): ChatGPT
Tried also Claude (less correct answers), DeepSeek and Gemini (too many iterations necessary), Mistral (too many errors)
4. -My first idea was to use the fact that to find the eigenvalues one
has to solve a Nth degree equation, e.g. of the quadratic 2nd, cubic
3rd, quartic 4th, quintic 5th, ... degree.
This approach because I had already created TSE programs to get the
roots of 1st, 2nd, 3rd, 4th and 5th degree equations.
So that would come very useful here.
The coefficients e.g. a, b, c, d, e, f of the Nth degree follow from working out
a 2x2, 3x3, 4x4, 5x5, ... determinant.
Albeit working out a 2x2 determinant is simple,
You get then in general as coefficients of the quadratic equation a . x^2 + b . x + c = 0
a = 1
b = -x11 -x12
c = x11 . x22 - x12 . x21
When working out a 3x3 determinant is already more involved.
then calculating a . x^3 + b . x^2 + c . x + d = 0
where
long calculations ...
a = ...
b = ...
c = ...
d = ...
When working out a 4x4 determinant is already even more involved.
then calculating a . x^4 + b . x^3 + c . x^2 + d . x + e = 0
where
long calculations ...
a = ...
b = ...
c = ...
d = ...
e = ...
When working out a 5x5 determinant is already even much more involved.
then calculating a . x^5 + b . x^4 + c . x^3 + d . x^2 + e . x + f = 0
where
long calculations ...
a = ...
b = ...
c = ...
d = ...
e = ...
f = ...
5. -So as a next step instead of using a more exact approach to use a numerical
approach.
The QR-algorithm seemed a good method, so asked the Artificial
Intelligence (=AI) to implement that in PowerShell.
That worked out OK, with very good output coming only from ChatGPT this time.
6. -Some background
1. -A matrix is nothing more than a 'bare bones' representation
of a set of (linear) equations.
Typically the variable names (e.g. x, y, z, ...) are left out
completely, only the numbers (e.g. 0.56, 1.23, ...) are
shown and are left.
For example
2 . x + 3 . y = 0
4 . x + 5 . y = 0
would give the matrix
2 3
4 5
2. -In general a multiplication of a matrix and a vector gives a new
vector but and rotated and stretched.
The eigenvector is a very special and simpler new vector that is not
rotated but only stretched.
You get a simpler system thus usually.
3. -All you need to begin with is a given (square, thus NxN) matrix.
Nothing else to begin with.
From that you calculate its N eigenvalues.
Typically by solving an Nth degree equation.
From those eigenvalues you calculate its eigenvectors.
4. -From that given (square) matrix you can calculate some special
vectors that only stretch but do not rotate when multiplied
with that given matrix.
5. -That explains also the German word 'eigen' here, which means 'coming
from itself', thus here coming intrinsically and inherently from
(only) that given (square) matrix 'itself' thus.
6. -A 2x2 matrix could give 2 eigenvalues, 3x3 could give 3 eigenvalues,
4x4 could give 4 eigenvalues, ..., NxN matrix could give N eigenvalues.
You will have to solve an Nth degree equation to find
the N eigenvalues.
Then from these N eigenvalues you can usually determine the
N eigenvectors.
7. -In general why would you multiply a matrix and a vector? Because you
want to perform a transformation (like a translation, a rotation,
...).
For example:
Rotate all single points of a given figure over 45 degrees then at
the end that whole figure is indeed rotated over 45 degrees.
And each single point is thus a vector.
And in case of this rotation of all points of a given figure you
would go for that special matrix that ONLY rotates but NOT
stretches. Because if it should stretch then the points or thus
vectors should be distorted as end result.
8. -So in general given a given matrix to multiply with then 4 special
new vector endresults are possible:
1. vector ONLY ROTATES but does NOT stretch.
2. vector ROTATES AND STRETCHES.
3. vector NO CHANGE at all.
4. vector NO rotation but ONLY STRETCHES.
This last situation is an eigenvector thus.
The amount that vector is stretched (or compressed) is the
eigenvalue thus.
9. -Usage of eigenvalues: Simplifying complex transformations: Imagine
trying to understand a complicated process that rotates, stretches,
and shears things in many different ways. Eigenvectors act like
special compass needles that point in directions where the
transformation is simplest - just a stretch or compression. This
makes analyzing and predicting the behavior of the system much
easier.
with friendly greetings
Knud van Eeden