Generate independent Bernoulli random variables above the diagonal and set everything else to zero. Taking the strict upper triangle of a random square array gives each allowed entry probability of being one:
import numpy as np
rng = np.random.default_rng(2024)
def rand_triu(n):
return np.triu(rng.integers(0, 2, size=(n, n)), k=1)
example = rand_triu(10)
print(example)One reproducible strictly upper triangular matrix is:
0 1 0 0 0 0 1 1 1 1
0 0 1 0 0 0 1 0 0 0
0 0 0 1 1 0 0 1 1 0
0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0Every matrix returned in this way is a nilpotent matrix; randomness affects its nilpotency index, not the fact of the nilpotent property.
Starting with the identity matrix, multiply by once per iteration and test the resulting power. Using integer objects keeps the calculation exact and avoids fixed-width integer overflow accidentally turning a nonzero power into zero:
def kzero(matrix,kmax=100):
a=np.asarray(matrix,dtype=object)
if a.ndim!=2 or a.shape[0]!=a.shape[1]:raise ValueError('square matrix required')
power=np.eye(len(a),dtype=object)
for k in range(1,kmax+1):
power=power@a
if np.equal(power,0).all():return k
return None
print([kzero(rand_triu(10)) for _ in range(5)])Continuing the random-number stream from the displayed example gives . These are the least positive zero powers, rather than the number of nonzero powers; in particular, a zero matrix has nilpotency index one.
For a strictly upper triangular matrix, a nonzero entry of can only lie at least positions above the diagonal. Thus , and every one of these examples must return an integer at most ten. The code interprets “up to” as including
kmax; if the final strict inequality in the question is intended instead, replace range(1,kmax+1) by range(1,kmax). Articles by others on the same topic
There are currently no matching articles.