# Random matrix with density.

I have a little question of comprehension

random_matrix(ZZ, 5, 4, x=0,y=11, density = 0.33)


generate a result like

[0 6 0 0]
[0 5 0 0]
[5 0 0 0]
[0 0 0 4]


but this gives 5 non zero enters which correspond, as I undeerstand it, to a density of 5/25 which is very far from .33 ? As always I suppose I have not clearly understood the meaning of density.

Should somebody explain this ?

edit retag close merge delete

Sort by » oldest newest most voted

According to the documentation,

The density keyword is used to compute the number of entries per row that will be nonzero, but the same entry may be selected more than once. So the value provided will be an upper bound for the density of the created matrix.

Since there are only 4 entries in each row, a density of 1/3 per row yields one nonzero entry per row.

If you want a matrix with specified global density, you could create a 1x20 matrix with the specified density and then convert it:

M_one_row = random_matrix(ZZ, 1, 20, x=0,y=11, density = 0.33)
M = matrix(5, 4, M_one_row.list())

more