ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 29 Jun 2013 02:08:29 -0500Get a matrix to display answers as decimals/floats, not fraction?https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/I searched and couldn't find a viable solution:
I successfully executed the following:
<pre>
ma = matrix([[25, 5, 1], [49, 7, 1], [81, 9, 1]])
mb = matrix([[1121], [626], [967]])
show(ma)
show(mb)
ms = ma^-1 * mb
show(ms)
</pre>
Unfotunately though, the statement, show(ms), displays the answer/values as fractions, meaning I have to manually enter each of the value 3 separate times, as part of a float function, i.e., float(fraction/number).
How can I force show(ms), to display the values as decimals?
Also I find the matrix more readable/writable if I can enter each row's values by their own line, instead of the same line, as seen on the the, ma= & mb= statements. I remember I could this with ease in MatLab. I'd appreciate knowing how to that as well.
This post also has an unanswered question if anyone is up for the challenge, :P, j/k:
http://ask.sagemath.org/question/2732/display-y-intercept
Sat, 29 Jun 2013 01:07:37 -0500https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/Answer by pkoprowski for <p>I searched and couldn't find a viable solution:</p>
<p>I successfully executed the following:</p>
<pre>ma = matrix([[25, 5, 1], [49, 7, 1], [81, 9, 1]])
mb = matrix([[1121], [626], [967]])
show(ma)
show(mb)
ms = ma^-1 * mb
show(ms)
</pre>
<p>Unfotunately though, the statement, show(ms), displays the answer/values as fractions, meaning I have to manually enter each of the value 3 separate times, as part of a float function, i.e., float(fraction/number).</p>
<p>How can I force show(ms), to display the values as decimals?</p>
<p>Also I find the matrix more readable/writable if I can enter each row's values by their own line, instead of the same line, as seen on the the, ma= & mb= statements. I remember I could this with ease in MatLab. I'd appreciate knowing how to that as well.</p>
<p>This post also has an unanswered question if anyone is up for the challenge, :P, j/k:
<a href="http://ask.sagemath.org/question/2732/display-y-intercept">http://ask.sagemath.org/question/2732...</a></p>
https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/?answer=15157#post-id-151571) You can easily write matrix "multilined". By default Python joins lines if the first one contains an opening bracket/parenthesis. Hence you can easily write:
ma = matrix([
[25, 5, 1],
[49, 7, 1],
[81, 9, 1]])
mb = matrix([
[1121],
[626],
[967]])
2) In order to display matrix entries as decimals you may convert a matrix base field to `RDF` like this:
ms = ma^-1 * mb
show(matrix(RDF, ms))
or alternatively:
show(ms.change_ring(RDF))Sat, 29 Jun 2013 01:52:58 -0500https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/?answer=15157#post-id-15157Comment by ndomes for <p>1) You can easily write matrix "multilined". By default Python joins lines if the first one contains an opening bracket/parenthesis. Hence you can easily write:</p>
<pre><code>ma = matrix([
[25, 5, 1],
[49, 7, 1],
[81, 9, 1]])
mb = matrix([
[1121],
[626],
[967]])
</code></pre>
<p>2) In order to display matrix entries as decimals you may convert a matrix base field to <code>RDF</code> like this:</p>
<pre><code>ms = ma^-1 * mb
show(matrix(RDF, ms))
</code></pre>
<p>or alternatively:</p>
<pre><code>show(ms.change_ring(RDF))
</code></pre>
https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/?comment=17425#post-id-17425another simple way: ms.n()Sat, 29 Jun 2013 02:08:29 -0500https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/?comment=17425#post-id-17425Comment by bxdin for <p>1) You can easily write matrix "multilined". By default Python joins lines if the first one contains an opening bracket/parenthesis. Hence you can easily write:</p>
<pre><code>ma = matrix([
[25, 5, 1],
[49, 7, 1],
[81, 9, 1]])
mb = matrix([
[1121],
[626],
[967]])
</code></pre>
<p>2) In order to display matrix entries as decimals you may convert a matrix base field to <code>RDF</code> like this:</p>
<pre><code>ms = ma^-1 * mb
show(matrix(RDF, ms))
</code></pre>
<p>or alternatively:</p>
<pre><code>show(ms.change_ring(RDF))
</code></pre>
https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/?comment=17426#post-id-17426Thanks :D.Sat, 29 Jun 2013 02:07:47 -0500https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/?comment=17426#post-id-17426