在Sympy中防止乘法表达式求值 [英] Preventing a multiplication expression evaluating in Sympy
问题描述
我正在生成一个包含两个分数的表达式,并希望使用LaTeX将其漂亮地打印成一个完整的表达式,然后放在工作表上.
例如格式:
(5/7)*(3/4).
但是,当我执行以下操作时:
fract1 = sympy.sympify(Fraction(5,7))fract2 = sympy.sympify(分数(3,4))表达式= sympy.Mul(fract1,fract2,evaluate = False)
返回
5 * 3/(7 * 4)
很明显,它是在合并分数,但并未进行实际评估,但我希望能够以适合作为数学工作表问题的格式来产生分数.
下一个SymPy版本将具有 UnevaluatedExpr
:
在[4]中:uexpr = UnevaluatedExpr(S.One * 5/7)* UnevaluatedExpr(S.One * 3/4)在[7]中:uexpr出[7]:5/7⋅3/4
要发布和评估它,只需使用 .doit()
:
在[8]中:uexpr.doit()出[8]:15──28岁
LaTeX输出如下:
在[10]中:print(latex(uexpr))\ frac {5} {7} \ frac {3} {4}
此功能自SymPy 1.1起可用.请参阅文档以了解更多信息.>
I am generating an expression with two fractions, and want to pretty print as a whole expression with LaTeX, to then put on a worksheet.
E.g. in the form:
(5/7) * (3/4).
However, when I do the following:
fract1 = sympy.sympify(Fraction(5,7))
fract2 = sympy.sympify(Fraction(3,4))
expression = sympy.Mul(fract1,fract2,evaluate=False)
It returns
5*3/(7*4)
Clearly it is combining the fraction but not actually evaluating, but I want to be able to produce it in a format suitable as a question for a maths worksheet.
The next SymPy version will have UnevaluatedExpr
:
In [4]: uexpr = UnevaluatedExpr(S.One*5/7)*UnevaluatedExpr(S.One*3/4)
In [7]: uexpr
Out[7]: 5/7⋅3/4
To release and evaluate it, just use .doit()
:
In [8]: uexpr.doit()
Out[8]:
15
──
28
LaTeX output looks like:
In [10]: print(latex(uexpr))
\frac{5}{7} \frac{3}{4}
This feature is available since SymPy 1.1. See the documentation to find out more.
这篇关于在Sympy中防止乘法表达式求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!