Ask Your Question

Decrease accuracy for floats (decrease decimal places reported)

asked 2012-01-24 18:33:23 +0200

richtopia gravatar image

updated 2012-01-24 20:10:20 +0200

kcrisman gravatar image

When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?

edit retag flag offensive close merge delete


As an update, the quickest method of beautifying my matrices was taking jdc's advice. I'm plotting Markov Chains, which I should have mentioned earlier, however n.(digits) can be used even inside DiGraph. For example: DiGraph(My_Matrix.n(digits=3),format="weighted_adjacency_matrix")

richtopia gravatar imagerichtopia ( 2012-01-25 19:33:54 +0200 )edit

4 Answers

Sort by ยป oldest newest most voted

answered 2012-01-25 17:37:41 +0200

jdc gravatar image

updated 2012-01-25 17:39:24 +0200

Do you mean printing (instead of plotting) the matrices? (Not sure what it would mean to plot a matrix.) If you're just trying to get a floating point number to display with fewer digits, you might try something like

foo.n(digits = 3)

Unfortunately, this simple approach doesn't always give expressions of a uniform "width", in terms of the number of characters. For example:

print pi.n(digits = 3)
print (pi*100000).n(digits = 3)
print (pi*100000000).n(digits = 3)
print (pi/100000).n(digits = 3)
print (pi/100000000).n(digits = 3)


edit flag offensive delete link more


Ah... I see. Thanks.

jdc gravatar imagejdc ( 2012-01-26 10:04:52 +0200 )edit

answered 2012-01-25 10:59:10 +0200

kcrisman gravatar image

This is just Shashank's answer partly converted to Sage.

sage: P = plot(sin(0.1*pi*x)*e^(-.01*x),(x,0,100))
sage: from matplotlib.ticker import FormatStrFormatter
sage: xFormatter = FormatStrFormatter('%d')
sage: yFormatter = FormatStrFormatter('%.2f')

It's definitely possible to format things quite a bit from within Sage if you want to. See the show() documentation.

However, this probably doesn't answer the original question about matrices of floats.

edit flag offensive delete link more

answered 2012-01-24 20:47:59 +0200

Shashank gravatar image

I normally don't use sage's default plotting. But, I have pasted below a matplotlib example I found online and modified a few lines for clarity. The format of the x and y ticks is given is c-style format. Please let me know if you have any question. In general matplotlib is far more versatile than sage's default plotter and I use is to make plots.

from pylab import *
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

majorLocator   = MultipleLocator(20)
xFormatter = FormatStrFormatter('%d')
yFormatter = FormatStrFormatter('%.2f')
minorLocator   = MultipleLocator(5)

t = arange(0.0, 100.0, 0.1)
s = sin(0.1*pi*t)*exp(-t*0.01)

ax = subplot(111)


#for the minor ticks, use no labels; default NullFormatter

edit flag offensive delete link more


If you are serious about absolute control in simplification and symbolics, use Maxima directly (or its methods) in Sage; similarly for plotting and matplotlib. It depends on how power of a user you are.

kcrisman gravatar imagekcrisman ( 2012-01-25 10:57:09 +0200 )edit

answered 2012-01-25 19:52:50 +0200

Volker Braun gravatar image

You can round floating-point matrices easily:

sage: random_matrix(RDF,4)         
[  0.249460263401   0.560301434409  -0.663582797018   0.932897872107]
[  0.334365993609  -0.868997217291  -0.546936618217  -0.636108817037]
[ -0.555384416581   0.961681767214  -0.146447558808   0.700186827417]
[-0.0701126985289   0.611616686035   0.890061111172   -0.22607396116]
sage: random_matrix(RDF,4).round(3)
[ 0.246  0.673 -0.168  0.239]
[ 0.869 -0.043  0.646  -0.36]
[ 0.467 -0.309   0.25  -0.35]
[ 0.508  0.671 -0.597 -0.583]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-01-24 18:33:23 +0200

Seen: 9,938 times

Last updated: Jan 25 '12