# is it possible to round numbers in symbolic expression

to display only 3 digits

transform that: 0.0870000000000000F_A + X_H + X_K + 0.706825181105366Z_B - 0.753724599483948

in: 0.087F_A + X_H + X_K + 0.707Z_B - 0.754

edit retag close merge delete

Sort by ยป oldest newest most voted

Yes, you can use an expression tree walker:

from sage.symbolic.expression_conversions import ExpressionTreeWalker

class SubstituteNumericalApprox(ExpressionTreeWalker):
def __init__(self, **kwds):
"""
A class that walks the tree and replaces numbers by numerical
approximations with the given keywords to numerical_approx.
EXAMPLES::
sage: var('F_A,X_H,X_K,Z_B')
sage: expr = 0.0870000000000000*F_A + X_H + X_K + 0.706825181105366*Z_B - 0.753724599483948
sage: SubstituteNumericalApprox(digits=3)(expr)
0.0870*F_A + X_H + X_K + 0.707*Z_B - 0.754
"""
self.kwds = kwds

def pyobject(self, ex, obj):
if hasattr(obj, 'numerical_approx'):
return obj.numerical_approx(**self.kwds)
else:
return obj

def __call__(self, ex):
result = super().__call__(ex)
return result.numerical_approx(**self.kwds) if result in CC else result


Example usage:

sage: var('F_A,X_H,X_K,Z_B')
sage: expr = 0.0870000000000000*F_A + X_H + X_K + 0.706825181105366*Z_B - 0.753724599483948
sage: SubstituteNumericalApprox(digits=3)(expr)
0.0870*F_A + X_H + X_K + 0.707*Z_B - 0.754
sage: SubstituteNumericalApprox(digits=5)(1.5*2^x/log(2))
2.1640*2.0000^x

more

thank you ! it works very well

( 2019-04-09 22:08:48 +0200 )edit

A small improvement to the code proposed so as to prevent integers from being cast to float as I noticed that this was the case for me:

def pyobject(self, ex, obj):
if hasattr(obj, 'numerical_approx'):
if hasattr(obj, 'parent'):
if obj.parent()==sg.IntegerRing():
return obj
return obj.numerical_approx(**self.kwds)
else:
return obj

( 2021-05-27 23:33:16 +0200 )edit