# Revision history [back]

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


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


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