ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 29 Nov 2017 02:39:50 -0600matrix with fractional numberhttp://ask.sagemath.org/question/39828/matrix-with-fractional-number/**m = matrix(ZZ, 3, 3, lambda i, j: 1/(i + j) ); m**
Sage tells me the following:
**Traceback (click to the left of this block for traceback)**
...
**ZeroDivisionError: rational division by zero**
why? it really confused me.Tue, 28 Nov 2017 08:32:59 -0600http://ask.sagemath.org/question/39828/matrix-with-fractional-number/Answer by dan_fulea for <p><strong>m = matrix(ZZ, 3, 3, lambda i, j: 1/(i + j) ); m</strong></p>
<p>Sage tells me the following:</p>
<p><strong>Traceback (click to the left of this block for traceback)</strong></p>
<p>...</p>
<p><strong>ZeroDivisionError: rational division by zero</strong></p>
<p>why? it really confused me.</p>
http://ask.sagemath.org/question/39828/matrix-with-fractional-number/?answer=39836#post-id-39836Alternatively, the matrix constructor can get an explicit list of lists (or the flat list associated to this one), instead of a function, and there is no confusion any longer. (Well, also using `i` will overwrite the default value of this variable in sage, which corresponds to $\sqrt{-1}$, i will use `k` and `n` instead of `i, j` .)
For instance:
sage: [ 1/(k+n) for k in [1..3] for n in [1..3] ]
[1/2, 1/3, 1/4, 1/3, 1/4, 1/5, 1/4, 1/5, 1/6]
sage: [ [ 1/(k+n) for k in [1..3] ] for n in [1..3] ]
[[1/2, 1/3, 1/4], [1/3, 1/4, 1/5], [1/4, 1/5, 1/6]]
sage: matrix( QQ, [ 1/(k+n) for k in [1..3] for n in [1..3] ] ) # bad
[1/2 1/3 1/4 1/3 1/4 1/5 1/4 1/5 1/6]
sage: matrix( QQ, 3, [ 1/(k+n) for k in [1..3] for n in [1..3] ] )
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]
sage: matrix( QQ, 3, 3, [ 1/(k+n) for k in [1..3] for n in [1..3] ] )
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]
sage: matrix( QQ, [ [ 1/(k+n) for k in [1..3] ] for n in [1..3] ] )
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]
sage: matrix( QQ, 3, [ [ 1/(k+n) for k in [1..3] ] for n in [1..3] ] )
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]
sage: matrix( QQ, 3, 3, [ [ 1/(k+n) for k in [1..3] ] for n in [1..3] ] )
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]Wed, 29 Nov 2017 02:39:50 -0600http://ask.sagemath.org/question/39828/matrix-with-fractional-number/?answer=39836#post-id-39836Answer by slelievre for <p><strong>m = matrix(ZZ, 3, 3, lambda i, j: 1/(i + j) ); m</strong></p>
<p>Sage tells me the following:</p>
<p><strong>Traceback (click to the left of this block for traceback)</strong></p>
<p>...</p>
<p><strong>ZeroDivisionError: rational division by zero</strong></p>
<p>why? it really confused me.</p>
http://ask.sagemath.org/question/39828/matrix-with-fractional-number/?answer=39829#post-id-39829The problem is that matrix rows and columns are numbered from 0.
So the top left element of that matrix would be 1 / (0 + 0).
If you are thinking of rows and columns as numbered from 1, do this instead:
sage: m = matrix(QQ, 3, 3, lambda i, j: 1 / (i + j + 2)); m
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]
or you can use another base ring such as RR or R in your example,
if you defined R as a ring beforehand (in Sage, by default, R stand
for the interpreter for the R statistics language).
Tue, 28 Nov 2017 11:04:23 -0600http://ask.sagemath.org/question/39828/matrix-with-fractional-number/?answer=39829#post-id-39829Comment by lijianing for <p>The problem is that matrix rows and columns are numbered from 0.</p>
<p>So the top left element of that matrix would be 1 / (0 + 0).</p>
<p>If you are thinking of rows and columns as numbered from 1, do this instead:</p>
<pre><code>sage: m = matrix(QQ, 3, 3, lambda i, j: 1 / (i + j + 2)); m
[1/2 1/3 1/4]
[1/3 1/4 1/5]
[1/4 1/5 1/6]
</code></pre>
<p>or you can use another base ring such as RR or R in your example,
if you defined R as a ring beforehand (in Sage, by default, R stand
for the interpreter for the R statistics language).</p>
http://ask.sagemath.org/question/39828/matrix-with-fractional-number/?comment=39833#post-id-39833Thank you very much. I see the problem and I change R to ZZ in my question.Tue, 28 Nov 2017 19:04:35 -0600http://ask.sagemath.org/question/39828/matrix-with-fractional-number/?comment=39833#post-id-39833