# How do I determine if a matrix is nilpotent using matlab?

113 views (last 30 days)
Amy Olivier on 10 Apr 2017
Edited: Bruno Luong on 15 Apr 2024
I tried using matrix manipulation to determine x which will determine whether A is nilpotent by using the following code:
A =[0 1 2 3 4 5 6 7;0 0 8 9 10 11 12 13;0 0 0 1 2 3 4 5; 0 0 0 0 6 7 8 9;0 0 0 0 0 1 2 3;0 0 0 0 0 0 4 5; 0 0 0 0 0 0 0 1;0 0 0 0 0 0 0 0];
B_A = zeros(8);
syms x
eqn = A.^x == B_A
solx = solve(eqn,x)
But I keep getting solx = Empty sym: 0-by-1
How do I get a solid solution for x? Or any other method to calculate the nilpotent will be helpful.

Torsten on 10 Apr 2017
eqn = A^x==B_A
But you should not check for nilpotency this way.
In your case, it's obvious by inspection that A is nilpotent since it is upper triangular.
For the general case, I'd check whether A has only 0 as eigenvalue :
help eig
Or - provided n is small - just calculate A^n for an (nxn)-matrix A and see whether it's the null matrix.
Best wishes
Torsten.
Bruno Luong on 14 Apr 2024
Here is another way to find the degree of Nilpotet matrix, ie smallest x such that A^x = 0
A =[0 1 2 3 4 5 6 7;
0 0 8 9 10 11 12 13;
0 0 0 1 2 3 4 5;
0 0 0 0 6 7 8 9;
0 0 0 0 0 1 2 3;
0 0 0 0 0 0 4 5;
0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0];
J = jordan(A); % required symbolic toolbox
d0 = diag(J);
isnilpotent = all(d0 == 0);
if isnilpotent
d1 = diag(J,1); %
% length of the longest sequence of nonzero above the diagonal
% == jordan block size
j = diff([0; d1~=0; 0]); % positions where transition to/from 0 occur
l = diff(find(j)); % length of consecutive non-zeros
maxl = max(l);
x = sum([maxl 1]); % it return 1 if l/maxl is empty, add 1 otherwise
else
x = [];
end
x
x = 8
Bruno Luong on 15 Apr 2024
Edited: Bruno Luong on 15 Apr 2024
It looks to me the EIG methods (both standarc and generalized) is difficult to be used for middle/large size nilpotetnt matrix. They seem to have difficulty to estimate 0 eigenvalues with huge errors.
Multiple order eigen values estimation is challenging to handle numerically.
n = 100; % 1000
A = diag(ones(1,n-1), 1);
[P,~]=qr(randn(size(A)));
B=P*A*P';
% B should be nimpotetnt, this is small
max(abs(B^n), [], "all")/norm(B)
ans = 1.7070e-15
% check eigen value of A
lambdaA = eig(A,eye(n),'qz','vector');
abs(lambdaA) % ok they are all 0Z
ans = 100x1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
norm(lambdaA,'inf')
ans = 0
% Do the same for B
% check eigen value of B, orthogonal transform of A
lambdaB = eig(B,eye(n),'qz','vector');
sort(abs(lambdaB)) % However none of them is close to 0!!!
ans = 100x1
0.4155 0.4155 0.5856 0.6826 0.6826 0.6879 0.6879 0.6891 0.6891 0.6922
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
norm(lambdaB,'inf')
ans = 0.7013