9

I have the following expression

A=Symbol('A')
x=Symbol('x')
B=Symbol('B')
C=Symbol('C')
D=Symbol('D')
expression=((A**x-B-C)/(D-1))*(D-1)
n,d=fraction(expression)

I am getting following result:

n=A**x-B-C
d=1

My expected result is

n=(A**x-B-C)*(D-1)
d=(D-1)

Is there way in sympy or need to write customize function to handle that

Tom
  • 904
  • 1
  • 7
  • 21
  • `expression` is `(A**x-B-C)`, since Sympy canceled the `D-1` term. As far as I know there's no easy way to prevent the canceling. – Dietrich Aug 24 '16 at 20:50

1 Answers1

7

Use UnevaluatedExpr() to prevent the expression from being evaluated.

from sympy import symbols, fraction, UnevaluatedExpr

A,x,B,C,D = symbols('A x B C D')

expression = (A**x-B-C)/(D-1)*UnevaluatedExpr(D-1)
n,d = fraction(expression)
print(n)
print(d)

This returns

(A**x - B - C)*(D - 1)
D - 1

See the Sympy Advanced Expression Manipulation doc page for more details.

cameronroytaylor
  • 1,578
  • 1
  • 17
  • 18