英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论