I'm trying to solve the following equation for variable x
(in dependence on other symbols b, c, d
):
from sympy import symbols, sin, cos, solve, solveset, S
b, c, d, x = symbols('b c d x', real=True)
expr = sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x) # find `x` such that `expr == 0`
There exist additional constraints which apply to the various symbols, namely:
0 < d < b < d+x < c
So I tried to use solve
for incorporating these inequality constraints:
result = solve(
[
sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x),
0 < d,
d < b,
b < d+x,
d+x < c,
],
x,
)
However, I get the following error:
Traceback (most recent call last):
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 211, in _parallel_dict_from_expr_if_gens
monom[indices[base]] = exp
KeyError: sin(c - x)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 818, in _solve_inequality
p = Poly(expr, s)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 181, in __new__
return cls._from_expr(rep, opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 310, in _from_expr
rep, opt = _dict_from_expr(rep, opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 368, in _dict_from_expr
rep, gens = _dict_from_expr_if_gens(expr, opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 307, in _dict_from_expr_if_gens
(poly,), gens = _parallel_dict_from_expr_if_gens((expr,), opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: sin(c - x) contains an element of the set of generators.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 211, in _parallel_dict_from_expr_if_gens
monom[indices[base]] = exp
KeyError: sin(c - x)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 246, in reduce_rational_inequalities
(numer, denom), opt = parallel_poly_from_expr(
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 4414, in parallel_poly_from_expr
return _parallel_poly_from_expr(exprs, opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 4467, in _parallel_poly_from_expr
reps, opt = _parallel_dict_from_expr(exprs, opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 332, in _parallel_dict_from_expr
reps, gens = _parallel_dict_from_expr_if_gens(exprs, opt)
File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: sin(c - x) contains an element of the set of generators.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 827, in _solve_inequality
rv = reduce_rational_inequalities([[ie]], s)
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 249, in reduce_rational_inequalities
raise PolynomialError(filldedent('''
sympy.polys.polyerrors.PolynomialError:
only polynomials and rational functions are supported in this context.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test2.py", line 5, in <module>
result = solve(
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/solvers.py", line 920, in solve
return reduce_inequalities(f, symbols=symbols)
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 996, in reduce_inequalities
rv = _reduce_inequalities(inequalities, symbols)
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 912, in _reduce_inequalities
other.append(_solve_inequality(Relational(expr, 0, rel), gen))
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 829, in _solve_inequality
rv = solve_univariate_inequality(ie, s)
File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 500, in solve_univariate_inequality
frange = function_range(e, gen, domain)
File "/path/to/venv/lib/python3.9/site-packages/sympy/calculus/util.py", line 208, in function_range
for critical_point in critical_points:
File "/path/to/venv/lib/python3.9/site-packages/sympy/utilities/iterables.py", line 2881, in roundrobin
yield nxt()
File "/path/to/venv/lib/python3.9/site-packages/sympy/sets/sets.py", line 1439, in __iter__
raise TypeError(
TypeError: The computation had not completed because of the undecidable set membership is found in every candidates.
I'm not sure what that error means. Does it mean that there exists no solution for the given inequality constraints?
(sympy version: 1.10.1)
solveset
I also tried using solveset
(however, it doesn't seem to be possible to include the inequality constraints here):
result = solveset(
sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x),
x,
domain=S.Reals,
)
However, after a long computation, it just returns the original expression:
ConditionSet(x, Eq(-sin(x)*sin(-c + 2*d + x) + sin(c - x)*sin(-2*b + 2*d + x), 0), Reals)
matlab
I also tried using Matlab for solving the equation:
syms b c d x
eq1 = sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x) == 0;
eq2 = 0 < d;
eq3 = d < b;
eq4 = b < d+x;
eq5 = d+x < c;
result = solve([eq1 eq2 eq3 eq4 eq5], x, "Real",true, "ReturnConditions",true);
However, the result is something entirely different, involving a fourth order polynomial of which I can't make any sense:
>> result.x
ans =
2*pi*k + atan2(y, 1) - atan2(-y, 1)
>> result.conditions
ans =
d < b & b + atan2(-y, 1) < d + atan2(y, 1) + 2*pi*k & d + atan2(y, 1) + 2*pi*k < c + atan2(-y, 1) & cos(b)*sin(b) + 2*cos(b)^2*cos(d)*sin(d) ~= cos(d)*sin(d) + 2*cos(b)*cos(d)^2*sin(b) & in(k, 'integer') & ~in(c/pi, 'integer') & in(y, 'real') & 4*y^2*cos(c - 2*d) + 2*y^3*sin(c - 2*d) + sin(2*b - 2*d)*sin(c) + y^2*cos(c - 2*b + 2*d) + 3*y^2*cos(2*b + c - 2*d) + 2*y^3*sin(2*b + c - 2*d) + y^4*sin(2*b - 2*d)*sin(c) == 2*y*sin(2*b + c - 2*d) + 2*y*sin(c - 2*d) & 0 < d