简体   繁体   English

枚举系数

[英]Enumerate coefficients

I want to make a program to detect a correct expanding.我想制作一个程序来检测正确的扩展。 For example: I want to expand (x + 2)*(x - 3) .例如:我想扩展(x + 2)*(x - 3) The solution is x*x -x -6 But x*x +2*x -3*x -6 is a correct solution.解是x*x -x -6x*x +2*x -3*x -6是正确的解。 I want to detect such correct (but unsimplified) expansions.我想检测这种正确(但未简化)的扩展。

If you allow a user to input the expression as a string and parse the expression with evaluate=False as shown here you can compare the number of arguments in what is entered with the fully simplified version.如果您允许用户将表达式作为字符串输入并使用evaluate=False解析表达式,如此处所示您可以将输入的 arguments 的数量与完全简化的版本进行比较。

>>> expr = (x - 3)*(x + 2)
>>> expanded = expand(expr)
>>> ans = 'x*x +2*x -3*x -6'  # obtained from user
>>> if S(ans) == expanded:  # it's right
...     if len(parse_expr(ans, evaluate=False).args) != len(expanded.args):
...         print('right, but not simplified')

The unsimplified ans will have 4 arguments while the expanded form will have 3.未简化的ans将有 4 个 arguments 而扩展形式将有 3 个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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