Chris R.
Chris R.

Reputation: 11

Prevent simplification/flattening/distribution of Simply

I am only really trying to use sympy to produce full equations using Latex. I don't need it to find me any answers necessarily. I have the following expression:

expr = Eq(S(DeltaX), ((V1 + V2) / 2) * DeltaT)

When it displays in latex it distributes the 2 denominator to both V1 and V2.

(V1/2 + V2/2)

When I use UnevaluatedExpr() it converts it to a negative exponent:

(V1 + V2)*2**(-1)

And I haven't been able to use evaluate=false in any way it's happy with the syntax. Which is the only other possible solution I've found.

Upvotes: 1

Views: 79

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14500

The default behaviour in SymPy is to distribute a Rational in a 2-arg Mul:

In [9]: (x + y) / 2
Out[9]: 
x   y
─ + ─
2   2

You can either control this using evaluate=False or the distribute context manager:

In [10]: Mul(Rational(1, 2), x + y, evaluate=False)
Out[10]: 
x + y
─────
  2 

In [13]: from sympy.core.parameters import distribute

In [14]: with distribute(False):
    ...:     e = (x + y) / 2
    ...: 

In [15]: e
Out[15]: 
x + y
─────
  2 

Upvotes: 3

Related Questions