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, 29 Sep 2010 17:50:40 +0200Changing number of decimal digits displayed in outputhttps://ask.sagemath.org/question/7713/changing-number-of-decimal-digits-displayed-in-output/I am doing some computations with large matrices over the complex numbers. When these matrices are outputted, the entries appear, for example, as
[ 0.03661165235? - 0.08838834765?*I 0.21338834765? + 0.08838834765?*I 0.03661165235? + 0.08838834765?*I etc
I would like to change it so that the output only displays each entry to 3 decimal places, so that they are easier to see. Is there a way to do this? Is there a way to do this that doesn't alter the number of digits stored? For reference, my input is
P8 = diagonal_matrix([1,1,1,0,0,0,0,1,1,0,0,0, 0,1,1,1])
U*P8*U.inverse()
where U is a previously defined matrix with complex entries.
Thanks!Wed, 29 Sep 2010 16:37:13 +0200https://ask.sagemath.org/question/7713/changing-number-of-decimal-digits-displayed-in-output/Answer by niles for <p>I am doing some computations with large matrices over the complex numbers. When these matrices are outputted, the entries appear, for example, as </p>
<p>[ 0.03661165235? - 0.08838834765?<em>I 0.21338834765? + 0.08838834765?</em>I 0.03661165235? + 0.08838834765?*I etc</p>
<p>I would like to change it so that the output only displays each entry to 3 decimal places, so that they are easier to see. Is there a way to do this? Is there a way to do this that doesn't alter the number of digits stored? For reference, my input is</p>
<p>P8 = diagonal_matrix([1,1,1,0,0,0,0,1,1,0,0,0, 0,1,1,1])</p>
<p>U<em>P8</em>U.inverse()</p>
<p>where U is a previously defined matrix with complex entries.</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/7713/changing-number-of-decimal-digits-displayed-in-output/?answer=11701#post-id-11701Here's one idea: pass the matrix to a low-precision ring before printing it -- this won't change the original matrix, just the printed version:
sage: CC10 = ComplexField(prec=10)
sage: MatPrint = MatrixSpace(CC10,3,3)
sage: M = Matrix(CC,3,3,[sqrt(-n) for n in range(9)])
sage: M.parent()
Full MatrixSpace of 3 by 3 dense matrices over Complex Field with 53 bits of precision
sage: M
[ 0 1.00000000000000*I 8.65956056235493e-17 + 1.41421356237309*I]
[1.06057523872491e-16 + 1.73205080756888*I 2.00000000000000*I 1.36919674566051e-16 + 2.23606797749979*I]
[1.49987988652185e-16 + 2.44948974278318*I 1.62005543721758e-16 + 2.64575131106459*I 1.73191211247099e-16 + 2.82842712474619*I]
sage: MatPrint(M)
[ 0 1.0*I 8.7e-17 + 1.4*I]
[1.1e-16 + 1.7*I 2.0*I 1.4e-16 + 2.2*I]
[1.5e-16 + 2.4*I 1.6e-16 + 2.6*I 1.7e-16 + 2.8*I]
sage: MatPrint(M).parent()
Full MatrixSpace of 3 by 3 dense matrices over Complex Field with 10 bits of precision
sage: M.parent()
Full MatrixSpace of 3 by 3 dense matrices over Complex Field with 53 bits of precision
There is also a [Trac ticket for this][1], with a patch, but work on it seems to have stalled . . . if you decide you need to use it, you can check the developer guide for how to [apply patches to Sage][2].
[1]:http://trac.sagemath.org/sage_trac/ticket/7682
[2]: http://www.sagemath.org/doc/developer/walk_through.html#reviewing-a-patchWed, 29 Sep 2010 17:50:40 +0200https://ask.sagemath.org/question/7713/changing-number-of-decimal-digits-displayed-in-output/?answer=11701#post-id-11701