# How to pick a matrix uniformly from a certain matrix space?

Hello, I hope everyone is fine. I have a small question. Suppose M denotes the set of all 2x2 matrices with integer entries. I know how to randomly pick an element from this space (by using the sage command .random_element().

My question is:

How can I uniformly pick a matrix from this space. I am not able to find any command for that? Kindly help.

edit retag close merge delete

From a countable set, what would it mean to pick uniformly at random?

( 2022-08-24 22:02:33 +0200 )edit

Sort by » oldest newest most voted

Let's create the space M:

sage: M = MatrixSpace(ZZ,2,2)
sage: M
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring


The default behavior is:

sage: M.random_element()
[-2  1]
[-3  0]


One may give an interval for the values:

sage: M.random_element(x=1000, y=2000)
[1308 1271]
[1451 1352]


One may increase the density of zero entries in the elements:

sage: M.random_element(x=1000, y=2000, density=.9)
[   0 1366]
[   0 1275]


One may provide a distribution, for instance the gaussian distribution with parameter sigma=x centered at zero. Below, I translate the center at 10^9:

sage: M.random_element(distribution='gaussian', x=10^9) + 10^9 * M((1,1,1,1))
[-203915342 -852236679]
[ 222400125 1463169355]


For further information and other possible inputs, read the below documentation :

sage: M.random_element?
sage: z = M.zero_matrix()
sage: z.randomize?
sage: ZZ.random_element?

more

Thank you for your response. I understand your answer, but Gaussian distribution is not what I require. I am working in Lattice based cryptography and I need some method to pick elements from the matrix space under the "uniform" distribution.

( 2022-08-27 11:59:20 +0200 )edit

As @slelievre said, there is no such thing as uniform distribution for integer matrices, which form an infinite countable set. If the probability of every such matrix is the same, since there are infinitely such matrices, and the total probability is 1, the common probability of every single matrix should be zero. But since there are countably integer matrices, then the total probability is 0 (because the measure is sigma-additive), not 1.

( 2022-08-27 14:15:18 +0200 )edit