# Printing matrices

Trying to format the output of a matrix, B. Sage reports: type 'print obj.str()' to see all of the entries) But print B.str() doesn't give me any format options. Is there a way control output like a regular print statement?

Thanks, -d

edit retag close merge delete

Sort by ยป oldest newest most voted

If you want to print only, say, 3 significant digits, you can do:

sage: B = random_matrix(RDF,5,5)
sage: B
[   0.23432333232  -0.227616100926  -0.599156411098    0.41166189832   0.764641434688]
[  0.439380654181  -0.320536940171   0.320947963409  -0.847122064875   0.404266447713]
[ -0.908186671809   0.622848454479   0.219215554761  -0.872284101116  -0.286278011806]
[  -0.68809928219   0.779797277388    0.12146772553   0.864262076944 -0.0156592746181]
[  0.807522456479  -0.294491803547  -0.569575247674  -0.850921782049  -0.521678331812]

sage: print B.str(rep_mapping=lambda x : str(x.n(digits=3)))
[  0.234  -0.228  -0.599   0.412   0.765]
[  0.439  -0.321   0.321  -0.847   0.404]
[ -0.908   0.623   0.219  -0.872  -0.286]
[ -0.688   0.780   0.121   0.864 -0.0157]
[  0.807  -0.294  -0.570  -0.851  -0.522]

more

This is what I'm trying to describe. The formatting is all over the place. Just trying to control the number of significant digits.

-d

print B
[              10.0      7.79934088913      5.73251431925      3.89141577098      2.31956702459      1.02571695446                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0]

[              10.0      8.05299001202      6.19851770151      4.50910708097      3.03012495201      1.78330079324     0.774303525753                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0]

[              10.0      8.21410145744      6.49945939383      4.91636989937      3.50852490926      2.30305774077      1.31391330977     0.546179897277                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0                0.0]

more

Type

sage: B.str?


to see some options like this:

sage: B = matrix(5,5)
sage: print B.str(zero="Hi Mom")
[Hi Mom Hi Mom Hi Mom Hi Mom Hi Mom]
[Hi Mom Hi Mom Hi Mom Hi Mom Hi Mom]
[Hi Mom Hi Mom Hi Mom Hi Mom Hi Mom]
[Hi Mom Hi Mom Hi Mom Hi Mom Hi Mom]
[Hi Mom Hi Mom Hi Mom Hi Mom Hi Mom]


But perhaps you are talking about other formatting options? More details could be helpful here.

more