CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Avatar for Support and Testing.

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

| Download
Views: 47
Image: ubuntu2204

This use of solve works fine with one variable:

var('x') print("x^2=1 only") show(solve(x^2==1, x))
x x^2=1 only
[x=1\displaystyle x = 1]

If you use assume and solve together with more than one variable, then the assumptions just get ignored.

var("x,y") assume(x>0) assume(y>0) print("x^2=1 and y^2=1") show(solve([x^2==1, y^2==1], x, y))
(x, y) x^2=1 and y^2=1
[[x=1\displaystyle x = 1, y=1\displaystyle y = 1], [x=(1)\displaystyle x = \left(-1\right), y=1\displaystyle y = 1], [x=1\displaystyle x = 1, y=(1)\displaystyle y = \left(-1\right)], [x=(1)\displaystyle x = \left(-1\right), y=(1)\displaystyle y = \left(-1\right)]]

Using algorithm='sympy' uses sympy to solve, but doesn't take into account assumptions, evidently (because we never implemented that):

show(solve([x^2==1,y^2==1], x, y, algorithm='sympy'))
[{x:1,y:1}\displaystyle \left\{x : -1, y : -1\right\}, {x:1,y:1}\displaystyle \left\{x : -1, y : 1\right\}, {x:1,y:1}\displaystyle \left\{x : 1, y : -1\right\}, {x:1,y:1}\displaystyle \left\{x : 1, y : 1\right\}]

Directly imposing constraints with sympy doesn't doesn't help (because probably the print name of var doesn't matter?):

import sympy sympy.Symbol('x', positive=True) sympy.Symbol('y', positive=True) show(solve([x^2==1,y^2==1], x, y, algorithm='sympy'))
x y
[{x:1,y:1}\displaystyle \left\{x : -1, y : -1\right\}, {x:1,y:1}\displaystyle \left\{x : -1, y : 1\right\}, {x:1,y:1}\displaystyle \left\{x : 1, y : -1\right\}, {x:1,y:1}\displaystyle \left\{x : 1, y : 1\right\}]

You can use Sympy entirely for this and that works fine (note that it solves for setting each expression to 0):

import sympy x = sympy.Symbol('x', positive=True) y = sympy.Symbol('y', positive=True) sympy.solve([x^2-1, y^2-1], x, y)
[(1, 1)]
# Here is a more accurate translation of the question into sympy (via chatgpt, which # clearly knows sympy better than I do!) Not the use of "Eq" from sympy import * x, y = symbols('x y', positive=True) eq1 = Eq(x**2, 1) eq2 = Eq(y**2, 1) sol = solve((eq1, eq2), (x, y)) print(sol)
[(1, 1)]