简体   繁体   中英

Render a sympy expression literally, with no simplification

I wonder how I can make sympy behave such that the expression is rendered literally and no defactoring occurs (4 under the sqrt)

from sympy import init_session
init_session(quiet=true)
from sympy.interactive import printing
printing.init_printing(use_latex=True)
a=sqrt(4*A**2*Z**2)
a

gives

2 * sqrt(a**2*Z**2)

I'd rather would have it written as initially written (in the course of an educational text). So how does one prevent sympy from doing any simplifications or resolving?

Use an "evaluate(False)" block to stop the simplifaction/evaluation rules:

(I am using Python 3.4, sympy 0.7)

with evaluate(False):
    print(sqrt(4*x))

sqrt(4*x)
>>>

As of now, there is no way to do this with sympy. It parses the input sqrt(4*A**2*Z**2) and stores the expression tree, along the way sorting the terms as it sees fit, combining all rational factors into one, etc. There is no information left about the original input.

Reference: Issue #3846: Allow manipulation of raw expression trees , which was open in 2008. The issue does not appear likely to be resolved any time soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM