Ask Your Question
2

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

asked 2019-05-06 22:18:37 +0100

jonespanicker gravatar image

updated 2019-05-06 23:45:59 +0100

slelievre gravatar image

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 flag offensive 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.

slelievre gravatar imageslelievre ( 2019-05-06 23:58:54 +0100 )edit

1 Answer

Sort by Β» oldest newest most voted
2

answered 2019-05-06 23:55:37 +0100

slelievre gravatar image

updated 2019-05-07 00:20:24 +0100

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]
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

1 follower

Stats

Asked: 2019-05-06 22:18:37 +0100

Seen: 1,206 times

Last updated: May 07 '19