Ask Your Question

# How to print the numeric part of a symbolic expression with arbitrary precision?

How to print the numeric part of a symbolic expression with arbitrary precision?

I have a matrix Hf, it is a 3 by 3 matrix. It's a symbolic matrix, with just one variable 'E'. But on printing it, I get the as shown below. Basically what I want is to approximate the numerical part of the expression to arbitrary precision (let's say 3 decimal points), so that I can get a much cleaner expression.

sage: Hf.str()
[(𝟼.𝟶𝟿𝟶𝟻𝟸𝟼𝟺𝟼𝟻𝟿𝟺𝟺𝟿𝟽𝚎⎯𝟼)*(𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒))/𝙴⎯𝟶.𝟶𝟶𝟶𝟸𝟷𝟽𝟼𝟷𝟹𝟾𝟸𝟻𝟷𝟶𝟶𝟶𝟶𝟼*(𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒))/𝙴+𝟷𝟶𝟶𝟶⎯𝟶.𝟶𝟶𝟶𝟸𝟷𝟽𝟼𝟷𝟹𝟾𝟸𝟻𝟷𝟶𝟶𝟶𝟶𝟼*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))/𝙴+(𝟼.𝟶𝟿𝟶𝟻𝟸𝟼𝟺𝟼𝟻𝟿𝟺𝟺𝟿𝟽𝚎⎯𝟼)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))/𝙴𝟶.𝟶𝟶𝟶𝟸𝟷𝟷𝟻𝟸𝟹𝟸𝟿𝟾𝟼𝟹𝟺𝟶𝟼𝟷*𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴][⎯𝟶.𝟶𝟶𝟶𝟷𝟻𝟼𝟸𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶*𝚜𝚚𝚛𝚝(𝟸)*(𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒))*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴+(𝟺.𝟼𝟾𝟽𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝚎⎯𝟼)*(𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒))*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))/𝙴⎯𝟶.𝟶𝟶𝟶𝟷𝟻𝟼𝟸𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶*𝚜𝚚𝚛𝚝(𝟸)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴+(𝟺.𝟼𝟾𝟽𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝚎⎯𝟼)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))ˆ𝟸/𝙴⎯(𝟺.𝟼𝟾𝟽𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝚎⎯𝟼)*𝚜𝚚𝚛𝚝(𝟸)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴+𝟶.𝟶𝟶𝟶𝟹𝟷𝟸𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)ˆ𝟸/𝙴][⎯𝟶.𝟶𝟶𝟶𝟷𝟻𝟼𝟸𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶*𝚜𝚚𝚛𝚝(𝟸)*(𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒))*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴+(𝟺.𝟼𝟾𝟽𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝚎⎯𝟼)*(𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒))*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))/𝙴⎯𝟶.𝟶𝟶𝟶𝟷𝟻𝟼𝟸𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶*𝚜𝚚𝚛𝚝(𝟸)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴+(𝟺.𝟼𝟾𝟽𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝚎⎯𝟼)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)⎯𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))/𝙴⎯(𝟺.𝟼𝟾𝟽𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝚎⎯𝟼)*𝚜𝚚𝚛𝚝(𝟸)*(𝚜𝚚𝚛𝚝(𝟸)*𝚜𝚒𝚗(𝟽/𝟹𝟼*𝚙𝚒)*𝚜𝚒𝚗(𝟷/𝟷𝟾*𝚙𝚒)+𝚜𝚚𝚛𝚝(𝟸)*𝚌𝚘𝚜(𝟽/𝟹𝟼*𝚙𝚒))*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)/𝙴+𝟶.𝟶𝟶𝟶𝟹𝟷𝟸𝟻𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟶*𝚌𝚘𝚜(𝟷/𝟷𝟾*𝚙𝚒)ˆ𝟸/𝙴]

edit retag close merge delete

## Comments

It would be interesting to get some Sage commands that allow to create this matrix.

If not the original commands, then at least one way to get hold of this matrix.

To share the matrix but not the original way it was produced, you could run

sage: sage_input(Hf)


and if that works you could copy and paste what it produces, which will give a way for others to define the same matrix and try things out on it.

( 2019-05-06 16:58:54 -0500 )edit

## 1 answer

Sort by » oldest newest most voted

It seems your matrix was defined with entries in Sage's symbolic ring.

Elements in the symbolic ring are symbolic expressions, which don't get automatically simplified.

One way to make the matrix more readable would be to think of it as a matrix whose entries are rational functions in E with coefficients in the real algebraic numbers.

Try this:

sage: Hf.change_ring(AA['E'].fraction_field())


If that does not work, then maybe rational functions in the (floating-point) "reals".

sage: Hf.change_ring(RR['E'].fraction_field())


Maybe the matrix was the following?

sage: Hf = matrix([
[(6.09052646594497e-6)*(𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒))/𝙴,
-0.000217613825100006*(𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒))/𝙴
+1000-0.000217613825100006*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))/𝙴
+(6.09052646594497e-6)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))/𝙴,
0.000211523298634061*𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴],
[-0.000156250000000000*𝚜𝚚𝚛𝚝(2)*(𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒))*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴
+(4.68750000000000e-6)*(𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒))*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))/𝙴,
-0.000156250000000000*𝚜𝚚𝚛𝚝(2)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴
+(4.68750000000000e-6)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))^2/𝙴,
-(4.68750000000000e-6)*𝚜𝚚𝚛𝚝(2)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴
+0.000312500000000000*𝚌𝚘𝚜(1/18*𝚙𝚒)^2/𝙴],
[-0.000156250000000000*𝚜𝚚𝚛𝚝(2)*(𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒))*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴
+(4.68750000000000e-6)*(𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒))*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))/𝙴,
-0.000156250000000000*𝚜𝚚𝚛𝚝(2)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴
+(4.68750000000000e-6)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)-𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))/𝙴,
-(4.68750000000000e-6)*𝚜𝚚𝚛𝚝(2)*(𝚜𝚚𝚛𝚝(2)*𝚜𝚒𝚗(7/36*𝚙𝚒)*𝚜𝚒𝚗(1/18*𝚙𝚒)+𝚜𝚚𝚛𝚝(2)*𝚌𝚘𝚜(7/36*𝚙𝚒))*𝚌𝚘𝚜(1/18*𝚙𝚒)/𝙴
+0.000312500000000000*𝚌𝚘𝚜(1/18*𝚙𝚒)^2/𝙴],
])


in which case rational functions over RR give:

sage: Hf.change_ring(RR['E'].fraction_field())
[                        6.16558207374834e-6/E (1000.00000000000*E - 0.000156202516960468)/E                        0.000294594522326585/E]
[                       0.000127914742222187/E                       -0.000277894374679915/E                        0.000309720283903677/E]
[                       0.000138909104469688/E                       -0.000288946039356407/E                        0.000294594522326585/E]


and rational functions over RDF give:

sage: Hf.change_ring(RDF['E'].fraction_field())
[              6.165582073748336e-06/E (1000.0*E - 0.00015620251696046755)/E               0.0002945945223265847/E]
[             0.00012791474222218708/E              -0.0002778943746799149/E              0.00030972028390367713/E]
[              0.0001389091044696876/E             -0.00028894603935640675/E              0.00029459452232658467/E]


Using a version of floating-point numbers with less bits (only 20 instead of the usual 53):

sage: Hf.change_ring(RealField(20)['E'].fraction_field())
[              6.1656e-6/E (1000.0*E - 0.00015620)/E              0.00029459/E]
[             0.00012791/E             -0.00027790/E              0.00030972/E]
[             0.00013891/E             -0.00028895/E              0.00029459/E]


Going down to 12 bits gives roughly three decimal digits as required in the question:

sage: Hf.change_ring(RealField(12)['E'].fraction_field())
[             6.17e-6/E (1000.*E - 0.000156)/E             0.000295/E]
[            0.000128/E            -0.000278/E             0.000310/E]
[            0.000139/E            -0.000289/E             0.000295/E]

more

## Your Answer

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

Add Answer

## Stats

Asked: 2019-05-06 15:18:37 -0500

Seen: 32 times

Last updated: May 06