Sympy solve 在添加正数限制后返回空集。

huangapple go评论50阅读模式
英文:

Sympy solve returns empty set after adding positive restriction

问题

当我在Python中运行以下代码并输入"x^2y^2"、"xy"等内容时,我得到一个空集:

from sympy import *
x, y, u = symbols('x y u', positive=True)
utility = sympify(input())
utility_function = Eq(u, utility)
solve(utility_function, y)

当我移除"positive = True"时,我得到解决方案。如何更改我的代码以仅获得正解?

英文:

when I'm running the following code in python and entering "x^2y^2", "xy" and so on, I'm getting an empty set:

from sympy import *
x, y, u = symbols('x y u',positive=True)
utility = sympify(input())
utility_function = Eq(u,utility)
solve(utility_function, y)

When I remove the positive = True, I'm getting solutions. How can I change my code that I only get the positive solution?

答案1

得分: 1

I don't have the same issue, but you should really pass the symbols defined as positive to the sympify function; if you don't, then symbols without assumptions will be created.

sympify('v', locals={'v': var('v', positive=True)}).is_positive

u = var('u', positive=True)  # now part of locals()
sympify('u', locals=locals()).is_positive
英文:

I don't have the same issue, but you should really pass the symbols defined as positive to the sympify function; if you don't, then symbols without assumptions will be created.

sympify('v',locals={'v':var('v',positive=True)}).is_positive

u = var('u', positive=True)  # now part of locals()
sympify('u',locals=locals()).is_positive

huangapple
  • 本文由 发表于 2023年2月7日 01:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定