ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 27 Aug 2022 14:15:18 +0200How to pick a matrix uniformly from a certain matrix space?https://ask.sagemath.org/question/63741/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.Wed, 24 Aug 2022 12:59:53 +0200https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/Comment by slelievre for <p>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 <code>.random_element()</code>.</p>
<p>My question is:</p>
<p>How can I uniformly pick a matrix from this space. I am not able to find any command for that? Kindly help.</p>
https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?comment=63756#post-id-63756From a countable set, what would it mean to pick uniformly at random?Wed, 24 Aug 2022 22:02:33 +0200https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?comment=63756#post-id-63756Answer by Sébastien for <p>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 <code>.random_element()</code>.</p>
<p>My question is:</p>
<p>How can I uniformly pick a matrix from this space. I am not able to find any command for that? Kindly help.</p>
https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?answer=63747#post-id-63747Let'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?Wed, 24 Aug 2022 15:40:47 +0200https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?answer=63747#post-id-63747Comment by tmonteil for <p>Let's create the space M:</p>
<pre><code>sage: M = MatrixSpace(ZZ,2,2)
sage: M
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
</code></pre>
<p>The default behavior is:</p>
<pre><code>sage: M.random_element()
[-2 1]
[-3 0]
</code></pre>
<p>One may give an interval for the values:</p>
<pre><code>sage: M.random_element(x=1000, y=2000)
[1308 1271]
[1451 1352]
</code></pre>
<p>One may increase the <em>density</em> of zero entries in the elements:</p>
<pre><code>sage: M.random_element(x=1000, y=2000, density=.9)
[ 0 1366]
[ 0 1275]
</code></pre>
<p>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:</p>
<pre><code>sage: M.random_element(distribution='gaussian', x=10^9) + 10^9 * M((1,1,1,1))
[-203915342 -852236679]
[ 222400125 1463169355]
</code></pre>
<p>For further information and other possible inputs, read the below documentation :</p>
<pre><code>sage: M.random_element?
sage: z = M.zero_matrix()
sage: z.randomize?
sage: ZZ.random_element?
</code></pre>
https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?comment=63795#post-id-63795As @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.Sat, 27 Aug 2022 14:15:18 +0200https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?comment=63795#post-id-63795Comment by Sri1729 for <p>Let's create the space M:</p>
<pre><code>sage: M = MatrixSpace(ZZ,2,2)
sage: M
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
</code></pre>
<p>The default behavior is:</p>
<pre><code>sage: M.random_element()
[-2 1]
[-3 0]
</code></pre>
<p>One may give an interval for the values:</p>
<pre><code>sage: M.random_element(x=1000, y=2000)
[1308 1271]
[1451 1352]
</code></pre>
<p>One may increase the <em>density</em> of zero entries in the elements:</p>
<pre><code>sage: M.random_element(x=1000, y=2000, density=.9)
[ 0 1366]
[ 0 1275]
</code></pre>
<p>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:</p>
<pre><code>sage: M.random_element(distribution='gaussian', x=10^9) + 10^9 * M((1,1,1,1))
[-203915342 -852236679]
[ 222400125 1463169355]
</code></pre>
<p>For further information and other possible inputs, read the below documentation :</p>
<pre><code>sage: M.random_element?
sage: z = M.zero_matrix()
sage: z.randomize?
sage: ZZ.random_element?
</code></pre>
https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?comment=63793#post-id-63793Thank 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.Sat, 27 Aug 2022 11:59:20 +0200https://ask.sagemath.org/question/63741/how-to-pick-a-matrix-uniformly-from-a-certain-matrix-space/?comment=63793#post-id-63793