【问题标题】:Sympy: Is there a function that gives all the factors of an expression but does not work sums?Sympy:是否有一个函数可以给出表达式的所有因式但不计算总和?
【发布时间】:2023-01-26 00:08:47
【问题描述】:

我有一个仅由因子组成的表达式(例如 (x**2+1)*(x**2)*(x+4)。我想使用带有 if 条件的函数 .args 从中删除因子 x**2。但是,如果我有以下等式 x**2+1+x+4,则.args 认为我在表达式中有 x**2 这是不正确的(我只有一个因素)。我有下面的代码。:

if q**2 in expr.args:        
   expr = expr.func(*[term for term in expr.args if term != q**2])
else:
   expr = expr*2

【问题讨论】:

    标签: sympy


    【解决方案1】:

    通过使用 Mul.make_args(expr) 如果表达式不是乘积,您将得到一个单例,否则将得到一个因子元组:

    >>> from sympy.abc import x, y
    >>> from sympy import Mul
    >>> Mul.make_args(x + y)
    (x + y,)
    >>> Mul.make_args(x*y)
    (x, y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多