Ask Your Question
2

is it possible to round numbers in symbolic expression

asked 2019-04-09 04:52:46 -0500

vini42 gravatar image

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2019-04-09 08:20:00 -0500

rburing gravatar image

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
edit flag offensive delete link more

Comments

thank you ! it works very well

vini42 gravatar imagevini42 ( 2019-04-09 15:08:48 -0500 )edit

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-04-09 04:52:46 -0500

Seen: 51 times

Last updated: Apr 09