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.Wed, 22 Nov 2017 10:41:35 +0100Rounding entries of a random vectorhttps://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code
`[round(4*random()-2,2)for i in[1 .. 8]]`
that produces a list of values that I need of size 8. However, I am struggling to combine it with a command `diagonal_matrix` and insert it there.
Also, I don't really understand why do we need to multiply it by 4 in here
`[round(4*random()-2,2)for i in[1 .. 8]]`
and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.Tue, 21 Nov 2017 14:53:11 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/Comment by Xenia for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39703#post-id-39703Thank you for your comment, but it seems that your suggestion doesn't work the way it should.Tue, 21 Nov 2017 18:04:04 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39703#post-id-39703Comment by FrédéricC for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39701#post-id-39701* diagonal_matrix(RR,[round(4*random()-2,2) for i in [1 .. 8]])
* random() gives a number between 0 and 1Tue, 21 Nov 2017 17:26:54 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39701#post-id-39701Comment by B r u n o for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39704#post-id-39704Xenia, you should be more precise. Frédéric's solution does what you (seem to) ask; It returns a diagonal matrix with random elements rounded to two digits, with approx. as many positive elements as negative ones, and without zero with high probability. Where is the problem?Tue, 21 Nov 2017 18:09:29 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39704#post-id-39704Comment by Xenia for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39709#post-id-39709The problem is, it is not rounded. Unfortunately, I do not get rounded result. And I would like to leave not diagonal entries as they are and get 0 there not 0.00 if it is possible. So, work only with a vector on a diagonal.Tue, 21 Nov 2017 18:40:08 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39709#post-id-39709Comment by dan_fulea for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39712#post-id-39712An other possibility is to import and use the **module** `random`. Then take care, the sage built in `random()` will no longer work. Sample code that uses a random choice from a user defined set would be like this:
import random
X = [ k/100. for k in [-10000..+10000] if k ]
diagonal_matrix( [ random.choice(X) for _ in [1..8] ] )
(Feel free to use other set / list `X` of possible values for the diagonal entries.)Tue, 21 Nov 2017 20:59:36 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39712#post-id-39712Comment by tmonteil for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39719#post-id-39719Note that the probability to get 0 is positive but very small !Wed, 22 Nov 2017 10:41:35 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39719#post-id-39719Comment by dan_fulea for <p>Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>that produces a list of values that I need of size 8. However, I am struggling to combine it with a command <code>diagonal_matrix</code> and insert it there.</p>
<p>Also, I don't really understand why do we need to multiply it by 4 in here </p>
<p><code>[round(4*random()-2,2)for i in[1 .. 8]]</code> </p>
<p>and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.</p>
https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39711#post-id-39711The values are indeed rounded, for instance:
sage: [ round(4*random()-2,2) for i in[1 .. 8] ]
[-0.34, -1.03, 1.04, 0.75, -1.47, 1.05, -0.31, 1.78]
(this time). As explained above:
- `random()` gives a random number in the interval $[0,1]$.
- So `4*random()` gives a random number in the interval $[0,4]$.
- So `4*random()-2` gives a random number in the interval $[0-2,4-2]=[-2,+2]$.
Yes, zero comes in the list with positive probability. A possibility to avoid zero is to use a "function in between" like:
sage: def myrandom():
....: randy = round(4*random()-2,2)
....: if randy:
....: return randy
....: return myrandom()
sage: myrandom()
-1.8
Then `diagonal_matrix( [ myrandom() for _ in range(8) ] )` does the job.Tue, 21 Nov 2017 20:55:14 +0100https://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/?comment=39711#post-id-39711