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.Fri, 17 Oct 2014 22:08:04 +0200N(): is it possible to display less digits than those use to compute?https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/Is there a way to do numerical computations in a certain precision, while displaying a lower number of digits, for example if I want to output a matrix of a certain dimension, and I would prefer a consistent layout?
An example with another CAS is *format short* in Matlab and Octave, which allows to compute in double precision (15 or 16 digits) by displaying only 5 digits.
Thanks in advance.Fri, 17 Oct 2014 00:42:39 +0200https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/Answer by ndomes for <p>Is there a way to do numerical computations in a certain precision, while displaying a lower number of digits, for example if I want to output a matrix of a certain dimension, and I would prefer a consistent layout?</p>
<p>An example with another CAS is <em>format short</em> in Matlab and Octave, which allows to compute in double precision (15 or 16 digits) by displaying only 5 digits.</p>
<p>Thanks in advance.</p>
https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/?answer=24509#post-id-24509An example:
M = matrix(RR,3,3,range(1,10))
M ; M.n(digits=4) Fri, 17 Oct 2014 08:47:56 +0200https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/?answer=24509#post-id-24509Comment by slelievre for <p>An example:</p>
<pre><code>M = matrix(RR,3,3,range(1,10))
M ; M.n(digits=4)
</code></pre>
https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/?comment=24516#post-id-24516If you are satisfied with the answer, you should click the tick mark to the left of the answer, to mark it as accepted.Fri, 17 Oct 2014 22:08:04 +0200https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/?comment=24516#post-id-24516Comment by afullo for <p>An example:</p>
<pre><code>M = matrix(RR,3,3,range(1,10))
M ; M.n(digits=4)
</code></pre>
https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/?comment=24514#post-id-24514It works, manies of thanks. :)
By the way, is there a similar command in order to display only the real part of the matrix elements? I have some computations in the complex field where imaginary parts should have cancellation, but due to roundoff they remain with a decimal magnitude of -13 or -14.
Usual commands real and imag, along with their synonyms real_part and imag_part, both by writing real(x) and x.real(), seem not to work for matrices. Thanks again...
EDIT: a workaround like (1/2)*A+(1/2)*A.conjugate() seems to work...Fri, 17 Oct 2014 17:35:46 +0200https://ask.sagemath.org/question/24507/n-is-it-possible-to-display-less-digits-than-those-use-to-compute/?comment=24514#post-id-24514