How to extract numerator and denominator from polynomial without evaluating?

最后都变了- 提交于 2020-01-14 08:54:08

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/39130964/how-to-extract-numerator-and-denominator-from-polynomial-without-evaluating

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!